]>
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 | |
05289b90 TG |
89 | int sched_thermal_decay_shift; |
90 | static int __init setup_sched_thermal_decay_shift(char *str) | |
91 | { | |
92 | int _shift = 0; | |
93 | ||
94 | if (kstrtoint(str, 0, &_shift)) | |
95 | pr_warn("Unable to set scheduler thermal pressure decay shift parameter\n"); | |
96 | ||
97 | sched_thermal_decay_shift = clamp(_shift, 0, 10); | |
98 | return 1; | |
99 | } | |
100 | __setup("sched_thermal_decay_shift=", setup_sched_thermal_decay_shift); | |
101 | ||
afe06efd TC |
102 | #ifdef CONFIG_SMP |
103 | /* | |
97fb7a0a | 104 | * For asym packing, by default the lower numbered CPU has higher priority. |
afe06efd TC |
105 | */ |
106 | int __weak arch_asym_cpu_priority(int cpu) | |
107 | { | |
108 | return -cpu; | |
109 | } | |
6d101ba6 OJ |
110 | |
111 | /* | |
60e17f5c | 112 | * The margin used when comparing utilization with CPU capacity. |
6d101ba6 OJ |
113 | * |
114 | * (default: ~20%) | |
115 | */ | |
60e17f5c VK |
116 | #define fits_capacity(cap, max) ((cap) * 1280 < (max) * 1024) |
117 | ||
afe06efd TC |
118 | #endif |
119 | ||
ec12cb7f PT |
120 | #ifdef CONFIG_CFS_BANDWIDTH |
121 | /* | |
122 | * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool | |
123 | * each time a cfs_rq requests quota. | |
124 | * | |
125 | * Note: in the case that the slice exceeds the runtime remaining (either due | |
126 | * to consumption or the quota being specified to be smaller than the slice) | |
127 | * we will always only issue the remaining available time. | |
128 | * | |
2b4d5b25 IM |
129 | * (default: 5 msec, units: microseconds) |
130 | */ | |
131 | unsigned int sysctl_sched_cfs_bandwidth_slice = 5000UL; | |
ec12cb7f PT |
132 | #endif |
133 | ||
8527632d PG |
134 | static inline void update_load_add(struct load_weight *lw, unsigned long inc) |
135 | { | |
136 | lw->weight += inc; | |
137 | lw->inv_weight = 0; | |
138 | } | |
139 | ||
140 | static inline void update_load_sub(struct load_weight *lw, unsigned long dec) | |
141 | { | |
142 | lw->weight -= dec; | |
143 | lw->inv_weight = 0; | |
144 | } | |
145 | ||
146 | static inline void update_load_set(struct load_weight *lw, unsigned long w) | |
147 | { | |
148 | lw->weight = w; | |
149 | lw->inv_weight = 0; | |
150 | } | |
151 | ||
029632fb PZ |
152 | /* |
153 | * Increase the granularity value when there are more CPUs, | |
154 | * because with more CPUs the 'effective latency' as visible | |
155 | * to users decreases. But the relationship is not linear, | |
156 | * so pick a second-best guess by going with the log2 of the | |
157 | * number of CPUs. | |
158 | * | |
159 | * This idea comes from the SD scheduler of Con Kolivas: | |
160 | */ | |
58ac93e4 | 161 | static unsigned int get_update_sysctl_factor(void) |
029632fb | 162 | { |
58ac93e4 | 163 | unsigned int cpus = min_t(unsigned int, num_online_cpus(), 8); |
029632fb PZ |
164 | unsigned int factor; |
165 | ||
166 | switch (sysctl_sched_tunable_scaling) { | |
167 | case SCHED_TUNABLESCALING_NONE: | |
168 | factor = 1; | |
169 | break; | |
170 | case SCHED_TUNABLESCALING_LINEAR: | |
171 | factor = cpus; | |
172 | break; | |
173 | case SCHED_TUNABLESCALING_LOG: | |
174 | default: | |
175 | factor = 1 + ilog2(cpus); | |
176 | break; | |
177 | } | |
178 | ||
179 | return factor; | |
180 | } | |
181 | ||
182 | static void update_sysctl(void) | |
183 | { | |
184 | unsigned int factor = get_update_sysctl_factor(); | |
185 | ||
186 | #define SET_SYSCTL(name) \ | |
187 | (sysctl_##name = (factor) * normalized_sysctl_##name) | |
188 | SET_SYSCTL(sched_min_granularity); | |
189 | SET_SYSCTL(sched_latency); | |
190 | SET_SYSCTL(sched_wakeup_granularity); | |
191 | #undef SET_SYSCTL | |
192 | } | |
193 | ||
f38f12d1 | 194 | void __init sched_init_granularity(void) |
029632fb PZ |
195 | { |
196 | update_sysctl(); | |
197 | } | |
198 | ||
9dbdb155 | 199 | #define WMULT_CONST (~0U) |
029632fb PZ |
200 | #define WMULT_SHIFT 32 |
201 | ||
9dbdb155 PZ |
202 | static void __update_inv_weight(struct load_weight *lw) |
203 | { | |
204 | unsigned long w; | |
205 | ||
206 | if (likely(lw->inv_weight)) | |
207 | return; | |
208 | ||
209 | w = scale_load_down(lw->weight); | |
210 | ||
211 | if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST)) | |
212 | lw->inv_weight = 1; | |
213 | else if (unlikely(!w)) | |
214 | lw->inv_weight = WMULT_CONST; | |
215 | else | |
216 | lw->inv_weight = WMULT_CONST / w; | |
217 | } | |
029632fb PZ |
218 | |
219 | /* | |
9dbdb155 PZ |
220 | * delta_exec * weight / lw.weight |
221 | * OR | |
222 | * (delta_exec * (weight * lw->inv_weight)) >> WMULT_SHIFT | |
223 | * | |
1c3de5e1 | 224 | * Either weight := NICE_0_LOAD and lw \e sched_prio_to_wmult[], in which case |
9dbdb155 PZ |
225 | * we're guaranteed shift stays positive because inv_weight is guaranteed to |
226 | * fit 32 bits, and NICE_0_LOAD gives another 10 bits; therefore shift >= 22. | |
227 | * | |
228 | * Or, weight =< lw.weight (because lw.weight is the runqueue weight), thus | |
229 | * weight/lw.weight <= 1, and therefore our shift will also be positive. | |
029632fb | 230 | */ |
9dbdb155 | 231 | static u64 __calc_delta(u64 delta_exec, unsigned long weight, struct load_weight *lw) |
029632fb | 232 | { |
9dbdb155 PZ |
233 | u64 fact = scale_load_down(weight); |
234 | int shift = WMULT_SHIFT; | |
029632fb | 235 | |
9dbdb155 | 236 | __update_inv_weight(lw); |
029632fb | 237 | |
9dbdb155 PZ |
238 | if (unlikely(fact >> 32)) { |
239 | while (fact >> 32) { | |
240 | fact >>= 1; | |
241 | shift--; | |
242 | } | |
029632fb PZ |
243 | } |
244 | ||
2eeb01a2 | 245 | fact = mul_u32_u32(fact, lw->inv_weight); |
029632fb | 246 | |
9dbdb155 PZ |
247 | while (fact >> 32) { |
248 | fact >>= 1; | |
249 | shift--; | |
250 | } | |
029632fb | 251 | |
9dbdb155 | 252 | return mul_u64_u32_shr(delta_exec, fact, shift); |
029632fb PZ |
253 | } |
254 | ||
255 | ||
256 | const struct sched_class fair_sched_class; | |
a4c2f00f | 257 | |
bf0f6f24 IM |
258 | /************************************************************** |
259 | * CFS operations on generic schedulable entities: | |
260 | */ | |
261 | ||
62160e3f | 262 | #ifdef CONFIG_FAIR_GROUP_SCHED |
8f48894f PZ |
263 | static inline struct task_struct *task_of(struct sched_entity *se) |
264 | { | |
9148a3a1 | 265 | SCHED_WARN_ON(!entity_is_task(se)); |
8f48894f PZ |
266 | return container_of(se, struct task_struct, se); |
267 | } | |
268 | ||
b758149c PZ |
269 | /* Walk up scheduling entities hierarchy */ |
270 | #define for_each_sched_entity(se) \ | |
271 | for (; se; se = se->parent) | |
272 | ||
273 | static inline struct cfs_rq *task_cfs_rq(struct task_struct *p) | |
274 | { | |
275 | return p->se.cfs_rq; | |
276 | } | |
277 | ||
278 | /* runqueue on which this entity is (to be) queued */ | |
279 | static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se) | |
280 | { | |
281 | return se->cfs_rq; | |
282 | } | |
283 | ||
284 | /* runqueue "owned" by this group */ | |
285 | static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp) | |
286 | { | |
287 | return grp->my_q; | |
288 | } | |
289 | ||
3c93a0c0 QY |
290 | static inline void cfs_rq_tg_path(struct cfs_rq *cfs_rq, char *path, int len) |
291 | { | |
292 | if (!path) | |
293 | return; | |
294 | ||
295 | if (cfs_rq && task_group_is_autogroup(cfs_rq->tg)) | |
296 | autogroup_path(cfs_rq->tg, path, len); | |
297 | else if (cfs_rq && cfs_rq->tg->css.cgroup) | |
298 | cgroup_path(cfs_rq->tg->css.cgroup, path, len); | |
299 | else | |
300 | strlcpy(path, "(null)", len); | |
301 | } | |
302 | ||
f6783319 | 303 | static inline bool list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq) |
3d4b47b4 | 304 | { |
5d299eab PZ |
305 | struct rq *rq = rq_of(cfs_rq); |
306 | int cpu = cpu_of(rq); | |
307 | ||
308 | if (cfs_rq->on_list) | |
f6783319 | 309 | return rq->tmp_alone_branch == &rq->leaf_cfs_rq_list; |
5d299eab PZ |
310 | |
311 | cfs_rq->on_list = 1; | |
312 | ||
313 | /* | |
314 | * Ensure we either appear before our parent (if already | |
315 | * enqueued) or force our parent to appear after us when it is | |
316 | * enqueued. The fact that we always enqueue bottom-up | |
317 | * reduces this to two cases and a special case for the root | |
318 | * cfs_rq. Furthermore, it also means that we will always reset | |
319 | * tmp_alone_branch either when the branch is connected | |
320 | * to a tree or when we reach the top of the tree | |
321 | */ | |
322 | if (cfs_rq->tg->parent && | |
323 | cfs_rq->tg->parent->cfs_rq[cpu]->on_list) { | |
67e86250 | 324 | /* |
5d299eab PZ |
325 | * If parent is already on the list, we add the child |
326 | * just before. Thanks to circular linked property of | |
327 | * the list, this means to put the child at the tail | |
328 | * of the list that starts by parent. | |
67e86250 | 329 | */ |
5d299eab PZ |
330 | list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list, |
331 | &(cfs_rq->tg->parent->cfs_rq[cpu]->leaf_cfs_rq_list)); | |
332 | /* | |
333 | * The branch is now connected to its tree so we can | |
334 | * reset tmp_alone_branch to the beginning of the | |
335 | * list. | |
336 | */ | |
337 | rq->tmp_alone_branch = &rq->leaf_cfs_rq_list; | |
f6783319 | 338 | return true; |
5d299eab | 339 | } |
3d4b47b4 | 340 | |
5d299eab PZ |
341 | if (!cfs_rq->tg->parent) { |
342 | /* | |
343 | * cfs rq without parent should be put | |
344 | * at the tail of the list. | |
345 | */ | |
346 | list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list, | |
347 | &rq->leaf_cfs_rq_list); | |
348 | /* | |
349 | * We have reach the top of a tree so we can reset | |
350 | * tmp_alone_branch to the beginning of the list. | |
351 | */ | |
352 | rq->tmp_alone_branch = &rq->leaf_cfs_rq_list; | |
f6783319 | 353 | return true; |
3d4b47b4 | 354 | } |
5d299eab PZ |
355 | |
356 | /* | |
357 | * The parent has not already been added so we want to | |
358 | * make sure that it will be put after us. | |
359 | * tmp_alone_branch points to the begin of the branch | |
360 | * where we will add parent. | |
361 | */ | |
362 | list_add_rcu(&cfs_rq->leaf_cfs_rq_list, rq->tmp_alone_branch); | |
363 | /* | |
364 | * update tmp_alone_branch to points to the new begin | |
365 | * of the branch | |
366 | */ | |
367 | rq->tmp_alone_branch = &cfs_rq->leaf_cfs_rq_list; | |
f6783319 | 368 | return false; |
3d4b47b4 PZ |
369 | } |
370 | ||
371 | static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq) | |
372 | { | |
373 | if (cfs_rq->on_list) { | |
31bc6aea VG |
374 | struct rq *rq = rq_of(cfs_rq); |
375 | ||
376 | /* | |
377 | * With cfs_rq being unthrottled/throttled during an enqueue, | |
378 | * it can happen the tmp_alone_branch points the a leaf that | |
379 | * we finally want to del. In this case, tmp_alone_branch moves | |
380 | * to the prev element but it will point to rq->leaf_cfs_rq_list | |
381 | * at the end of the enqueue. | |
382 | */ | |
383 | if (rq->tmp_alone_branch == &cfs_rq->leaf_cfs_rq_list) | |
384 | rq->tmp_alone_branch = cfs_rq->leaf_cfs_rq_list.prev; | |
385 | ||
3d4b47b4 PZ |
386 | list_del_rcu(&cfs_rq->leaf_cfs_rq_list); |
387 | cfs_rq->on_list = 0; | |
388 | } | |
389 | } | |
390 | ||
5d299eab PZ |
391 | static inline void assert_list_leaf_cfs_rq(struct rq *rq) |
392 | { | |
393 | SCHED_WARN_ON(rq->tmp_alone_branch != &rq->leaf_cfs_rq_list); | |
394 | } | |
395 | ||
039ae8bc VG |
396 | /* Iterate thr' all leaf cfs_rq's on a runqueue */ |
397 | #define for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) \ | |
398 | list_for_each_entry_safe(cfs_rq, pos, &rq->leaf_cfs_rq_list, \ | |
399 | leaf_cfs_rq_list) | |
b758149c PZ |
400 | |
401 | /* Do the two (enqueued) entities belong to the same group ? */ | |
fed14d45 | 402 | static inline struct cfs_rq * |
b758149c PZ |
403 | is_same_group(struct sched_entity *se, struct sched_entity *pse) |
404 | { | |
405 | if (se->cfs_rq == pse->cfs_rq) | |
fed14d45 | 406 | return se->cfs_rq; |
b758149c | 407 | |
fed14d45 | 408 | return NULL; |
b758149c PZ |
409 | } |
410 | ||
411 | static inline struct sched_entity *parent_entity(struct sched_entity *se) | |
412 | { | |
413 | return se->parent; | |
414 | } | |
415 | ||
464b7527 PZ |
416 | static void |
417 | find_matching_se(struct sched_entity **se, struct sched_entity **pse) | |
418 | { | |
419 | int se_depth, pse_depth; | |
420 | ||
421 | /* | |
422 | * preemption test can be made between sibling entities who are in the | |
423 | * same cfs_rq i.e who have a common parent. Walk up the hierarchy of | |
424 | * both tasks until we find their ancestors who are siblings of common | |
425 | * parent. | |
426 | */ | |
427 | ||
428 | /* First walk up until both entities are at same depth */ | |
fed14d45 PZ |
429 | se_depth = (*se)->depth; |
430 | pse_depth = (*pse)->depth; | |
464b7527 PZ |
431 | |
432 | while (se_depth > pse_depth) { | |
433 | se_depth--; | |
434 | *se = parent_entity(*se); | |
435 | } | |
436 | ||
437 | while (pse_depth > se_depth) { | |
438 | pse_depth--; | |
439 | *pse = parent_entity(*pse); | |
440 | } | |
441 | ||
442 | while (!is_same_group(*se, *pse)) { | |
443 | *se = parent_entity(*se); | |
444 | *pse = parent_entity(*pse); | |
445 | } | |
446 | } | |
447 | ||
8f48894f PZ |
448 | #else /* !CONFIG_FAIR_GROUP_SCHED */ |
449 | ||
450 | static inline struct task_struct *task_of(struct sched_entity *se) | |
451 | { | |
452 | return container_of(se, struct task_struct, se); | |
453 | } | |
bf0f6f24 | 454 | |
b758149c PZ |
455 | #define for_each_sched_entity(se) \ |
456 | for (; se; se = NULL) | |
bf0f6f24 | 457 | |
b758149c | 458 | static inline struct cfs_rq *task_cfs_rq(struct task_struct *p) |
bf0f6f24 | 459 | { |
b758149c | 460 | return &task_rq(p)->cfs; |
bf0f6f24 IM |
461 | } |
462 | ||
b758149c PZ |
463 | static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se) |
464 | { | |
465 | struct task_struct *p = task_of(se); | |
466 | struct rq *rq = task_rq(p); | |
467 | ||
468 | return &rq->cfs; | |
469 | } | |
470 | ||
471 | /* runqueue "owned" by this group */ | |
472 | static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp) | |
473 | { | |
474 | return NULL; | |
475 | } | |
476 | ||
3c93a0c0 QY |
477 | static inline void cfs_rq_tg_path(struct cfs_rq *cfs_rq, char *path, int len) |
478 | { | |
479 | if (path) | |
480 | strlcpy(path, "(null)", len); | |
481 | } | |
482 | ||
f6783319 | 483 | static inline bool list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq) |
3d4b47b4 | 484 | { |
f6783319 | 485 | return true; |
3d4b47b4 PZ |
486 | } |
487 | ||
488 | static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq) | |
489 | { | |
490 | } | |
491 | ||
5d299eab PZ |
492 | static inline void assert_list_leaf_cfs_rq(struct rq *rq) |
493 | { | |
494 | } | |
495 | ||
039ae8bc VG |
496 | #define for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) \ |
497 | for (cfs_rq = &rq->cfs, pos = NULL; cfs_rq; cfs_rq = pos) | |
b758149c | 498 | |
b758149c PZ |
499 | static inline struct sched_entity *parent_entity(struct sched_entity *se) |
500 | { | |
501 | return NULL; | |
502 | } | |
503 | ||
464b7527 PZ |
504 | static inline void |
505 | find_matching_se(struct sched_entity **se, struct sched_entity **pse) | |
506 | { | |
507 | } | |
508 | ||
b758149c PZ |
509 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
510 | ||
6c16a6dc | 511 | static __always_inline |
9dbdb155 | 512 | void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec); |
bf0f6f24 IM |
513 | |
514 | /************************************************************** | |
515 | * Scheduling class tree data structure manipulation methods: | |
516 | */ | |
517 | ||
1bf08230 | 518 | static inline u64 max_vruntime(u64 max_vruntime, u64 vruntime) |
02e0431a | 519 | { |
1bf08230 | 520 | s64 delta = (s64)(vruntime - max_vruntime); |
368059a9 | 521 | if (delta > 0) |
1bf08230 | 522 | max_vruntime = vruntime; |
02e0431a | 523 | |
1bf08230 | 524 | return max_vruntime; |
02e0431a PZ |
525 | } |
526 | ||
0702e3eb | 527 | static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime) |
b0ffd246 PZ |
528 | { |
529 | s64 delta = (s64)(vruntime - min_vruntime); | |
530 | if (delta < 0) | |
531 | min_vruntime = vruntime; | |
532 | ||
533 | return min_vruntime; | |
534 | } | |
535 | ||
54fdc581 FC |
536 | static inline int entity_before(struct sched_entity *a, |
537 | struct sched_entity *b) | |
538 | { | |
539 | return (s64)(a->vruntime - b->vruntime) < 0; | |
540 | } | |
541 | ||
1af5f730 PZ |
542 | static void update_min_vruntime(struct cfs_rq *cfs_rq) |
543 | { | |
b60205c7 | 544 | struct sched_entity *curr = cfs_rq->curr; |
bfb06889 | 545 | struct rb_node *leftmost = rb_first_cached(&cfs_rq->tasks_timeline); |
b60205c7 | 546 | |
1af5f730 PZ |
547 | u64 vruntime = cfs_rq->min_vruntime; |
548 | ||
b60205c7 PZ |
549 | if (curr) { |
550 | if (curr->on_rq) | |
551 | vruntime = curr->vruntime; | |
552 | else | |
553 | curr = NULL; | |
554 | } | |
1af5f730 | 555 | |
bfb06889 DB |
556 | if (leftmost) { /* non-empty tree */ |
557 | struct sched_entity *se; | |
558 | se = rb_entry(leftmost, struct sched_entity, run_node); | |
1af5f730 | 559 | |
b60205c7 | 560 | if (!curr) |
1af5f730 PZ |
561 | vruntime = se->vruntime; |
562 | else | |
563 | vruntime = min_vruntime(vruntime, se->vruntime); | |
564 | } | |
565 | ||
1bf08230 | 566 | /* ensure we never gain time by being placed backwards. */ |
1af5f730 | 567 | cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime); |
3fe1698b PZ |
568 | #ifndef CONFIG_64BIT |
569 | smp_wmb(); | |
570 | cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime; | |
571 | #endif | |
1af5f730 PZ |
572 | } |
573 | ||
bf0f6f24 IM |
574 | /* |
575 | * Enqueue an entity into the rb-tree: | |
576 | */ | |
0702e3eb | 577 | static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 | 578 | { |
bfb06889 | 579 | struct rb_node **link = &cfs_rq->tasks_timeline.rb_root.rb_node; |
bf0f6f24 IM |
580 | struct rb_node *parent = NULL; |
581 | struct sched_entity *entry; | |
bfb06889 | 582 | bool leftmost = true; |
bf0f6f24 IM |
583 | |
584 | /* | |
585 | * Find the right place in the rbtree: | |
586 | */ | |
587 | while (*link) { | |
588 | parent = *link; | |
589 | entry = rb_entry(parent, struct sched_entity, run_node); | |
590 | /* | |
591 | * We dont care about collisions. Nodes with | |
592 | * the same key stay together. | |
593 | */ | |
2bd2d6f2 | 594 | if (entity_before(se, entry)) { |
bf0f6f24 IM |
595 | link = &parent->rb_left; |
596 | } else { | |
597 | link = &parent->rb_right; | |
bfb06889 | 598 | leftmost = false; |
bf0f6f24 IM |
599 | } |
600 | } | |
601 | ||
bf0f6f24 | 602 | rb_link_node(&se->run_node, parent, link); |
bfb06889 DB |
603 | rb_insert_color_cached(&se->run_node, |
604 | &cfs_rq->tasks_timeline, leftmost); | |
bf0f6f24 IM |
605 | } |
606 | ||
0702e3eb | 607 | static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 | 608 | { |
bfb06889 | 609 | rb_erase_cached(&se->run_node, &cfs_rq->tasks_timeline); |
bf0f6f24 IM |
610 | } |
611 | ||
029632fb | 612 | struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq) |
bf0f6f24 | 613 | { |
bfb06889 | 614 | struct rb_node *left = rb_first_cached(&cfs_rq->tasks_timeline); |
f4b6755f PZ |
615 | |
616 | if (!left) | |
617 | return NULL; | |
618 | ||
619 | return rb_entry(left, struct sched_entity, run_node); | |
bf0f6f24 IM |
620 | } |
621 | ||
ac53db59 RR |
622 | static struct sched_entity *__pick_next_entity(struct sched_entity *se) |
623 | { | |
624 | struct rb_node *next = rb_next(&se->run_node); | |
625 | ||
626 | if (!next) | |
627 | return NULL; | |
628 | ||
629 | return rb_entry(next, struct sched_entity, run_node); | |
630 | } | |
631 | ||
632 | #ifdef CONFIG_SCHED_DEBUG | |
029632fb | 633 | struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq) |
aeb73b04 | 634 | { |
bfb06889 | 635 | struct rb_node *last = rb_last(&cfs_rq->tasks_timeline.rb_root); |
aeb73b04 | 636 | |
70eee74b BS |
637 | if (!last) |
638 | return NULL; | |
7eee3e67 IM |
639 | |
640 | return rb_entry(last, struct sched_entity, run_node); | |
aeb73b04 PZ |
641 | } |
642 | ||
bf0f6f24 IM |
643 | /************************************************************** |
644 | * Scheduling class statistics methods: | |
645 | */ | |
646 | ||
acb4a848 | 647 | int sched_proc_update_handler(struct ctl_table *table, int write, |
32927393 | 648 | void *buffer, size_t *lenp, loff_t *ppos) |
b2be5e96 | 649 | { |
8d65af78 | 650 | int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
58ac93e4 | 651 | unsigned int factor = get_update_sysctl_factor(); |
b2be5e96 PZ |
652 | |
653 | if (ret || !write) | |
654 | return ret; | |
655 | ||
656 | sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency, | |
657 | sysctl_sched_min_granularity); | |
658 | ||
acb4a848 CE |
659 | #define WRT_SYSCTL(name) \ |
660 | (normalized_sysctl_##name = sysctl_##name / (factor)) | |
661 | WRT_SYSCTL(sched_min_granularity); | |
662 | WRT_SYSCTL(sched_latency); | |
663 | WRT_SYSCTL(sched_wakeup_granularity); | |
acb4a848 CE |
664 | #undef WRT_SYSCTL |
665 | ||
b2be5e96 PZ |
666 | return 0; |
667 | } | |
668 | #endif | |
647e7cac | 669 | |
a7be37ac | 670 | /* |
f9c0b095 | 671 | * delta /= w |
a7be37ac | 672 | */ |
9dbdb155 | 673 | static inline u64 calc_delta_fair(u64 delta, struct sched_entity *se) |
a7be37ac | 674 | { |
f9c0b095 | 675 | if (unlikely(se->load.weight != NICE_0_LOAD)) |
9dbdb155 | 676 | delta = __calc_delta(delta, NICE_0_LOAD, &se->load); |
a7be37ac PZ |
677 | |
678 | return delta; | |
679 | } | |
680 | ||
647e7cac IM |
681 | /* |
682 | * The idea is to set a period in which each task runs once. | |
683 | * | |
532b1858 | 684 | * When there are too many tasks (sched_nr_latency) we have to stretch |
647e7cac IM |
685 | * this period because otherwise the slices get too small. |
686 | * | |
687 | * p = (nr <= nl) ? l : l*nr/nl | |
688 | */ | |
4d78e7b6 PZ |
689 | static u64 __sched_period(unsigned long nr_running) |
690 | { | |
8e2b0bf3 BF |
691 | if (unlikely(nr_running > sched_nr_latency)) |
692 | return nr_running * sysctl_sched_min_granularity; | |
693 | else | |
694 | return sysctl_sched_latency; | |
4d78e7b6 PZ |
695 | } |
696 | ||
647e7cac IM |
697 | /* |
698 | * We calculate the wall-time slice from the period by taking a part | |
699 | * proportional to the weight. | |
700 | * | |
f9c0b095 | 701 | * s = p*P[w/rw] |
647e7cac | 702 | */ |
6d0f0ebd | 703 | static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se) |
21805085 | 704 | { |
0a582440 | 705 | u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq); |
f9c0b095 | 706 | |
0a582440 | 707 | for_each_sched_entity(se) { |
6272d68c | 708 | struct load_weight *load; |
3104bf03 | 709 | struct load_weight lw; |
6272d68c LM |
710 | |
711 | cfs_rq = cfs_rq_of(se); | |
712 | load = &cfs_rq->load; | |
f9c0b095 | 713 | |
0a582440 | 714 | if (unlikely(!se->on_rq)) { |
3104bf03 | 715 | lw = cfs_rq->load; |
0a582440 MG |
716 | |
717 | update_load_add(&lw, se->load.weight); | |
718 | load = &lw; | |
719 | } | |
9dbdb155 | 720 | slice = __calc_delta(slice, se->load.weight, load); |
0a582440 MG |
721 | } |
722 | return slice; | |
bf0f6f24 IM |
723 | } |
724 | ||
647e7cac | 725 | /* |
660cc00f | 726 | * We calculate the vruntime slice of a to-be-inserted task. |
647e7cac | 727 | * |
f9c0b095 | 728 | * vs = s/w |
647e7cac | 729 | */ |
f9c0b095 | 730 | static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se) |
67e9fb2a | 731 | { |
f9c0b095 | 732 | return calc_delta_fair(sched_slice(cfs_rq, se), se); |
a7be37ac PZ |
733 | } |
734 | ||
c0796298 | 735 | #include "pelt.h" |
23127296 | 736 | #ifdef CONFIG_SMP |
283e2ed3 | 737 | |
772bd008 | 738 | static int select_idle_sibling(struct task_struct *p, int prev_cpu, int cpu); |
fb13c7ee | 739 | static unsigned long task_h_load(struct task_struct *p); |
3b1baa64 | 740 | static unsigned long capacity_of(int cpu); |
fb13c7ee | 741 | |
540247fb YD |
742 | /* Give new sched_entity start runnable values to heavy its load in infant time */ |
743 | void init_entity_runnable_average(struct sched_entity *se) | |
a75cdaa9 | 744 | { |
540247fb | 745 | struct sched_avg *sa = &se->avg; |
a75cdaa9 | 746 | |
f207934f PZ |
747 | memset(sa, 0, sizeof(*sa)); |
748 | ||
b5a9b340 | 749 | /* |
dfcb245e | 750 | * Tasks are initialized with full load to be seen as heavy tasks until |
b5a9b340 | 751 | * they get a chance to stabilize to their real load level. |
dfcb245e | 752 | * Group entities are initialized with zero load to reflect the fact that |
b5a9b340 VG |
753 | * nothing has been attached to the task group yet. |
754 | */ | |
755 | if (entity_is_task(se)) | |
0dacee1b | 756 | sa->load_avg = scale_load_down(se->load.weight); |
f207934f | 757 | |
9d89c257 | 758 | /* when this task enqueue'ed, it will contribute to its cfs_rq's load_avg */ |
a75cdaa9 | 759 | } |
7ea241af | 760 | |
df217913 | 761 | static void attach_entity_cfs_rq(struct sched_entity *se); |
7dc603c9 | 762 | |
2b8c41da YD |
763 | /* |
764 | * With new tasks being created, their initial util_avgs are extrapolated | |
765 | * based on the cfs_rq's current util_avg: | |
766 | * | |
767 | * util_avg = cfs_rq->util_avg / (cfs_rq->load_avg + 1) * se.load.weight | |
768 | * | |
769 | * However, in many cases, the above util_avg does not give a desired | |
770 | * value. Moreover, the sum of the util_avgs may be divergent, such | |
771 | * as when the series is a harmonic series. | |
772 | * | |
773 | * To solve this problem, we also cap the util_avg of successive tasks to | |
774 | * only 1/2 of the left utilization budget: | |
775 | * | |
8fe5c5a9 | 776 | * util_avg_cap = (cpu_scale - cfs_rq->avg.util_avg) / 2^n |
2b8c41da | 777 | * |
8fe5c5a9 | 778 | * where n denotes the nth task and cpu_scale the CPU capacity. |
2b8c41da | 779 | * |
8fe5c5a9 QP |
780 | * For example, for a CPU with 1024 of capacity, a simplest series from |
781 | * the beginning would be like: | |
2b8c41da YD |
782 | * |
783 | * task util_avg: 512, 256, 128, 64, 32, 16, 8, ... | |
784 | * cfs_rq util_avg: 512, 768, 896, 960, 992, 1008, 1016, ... | |
785 | * | |
786 | * Finally, that extrapolated util_avg is clamped to the cap (util_avg_cap) | |
787 | * if util_avg > util_avg_cap. | |
788 | */ | |
d0fe0b9c | 789 | void post_init_entity_util_avg(struct task_struct *p) |
2b8c41da | 790 | { |
d0fe0b9c | 791 | struct sched_entity *se = &p->se; |
2b8c41da YD |
792 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
793 | struct sched_avg *sa = &se->avg; | |
8ec59c0f | 794 | long cpu_scale = arch_scale_cpu_capacity(cpu_of(rq_of(cfs_rq))); |
8fe5c5a9 | 795 | long cap = (long)(cpu_scale - cfs_rq->avg.util_avg) / 2; |
2b8c41da YD |
796 | |
797 | if (cap > 0) { | |
798 | if (cfs_rq->avg.util_avg != 0) { | |
799 | sa->util_avg = cfs_rq->avg.util_avg * se->load.weight; | |
800 | sa->util_avg /= (cfs_rq->avg.load_avg + 1); | |
801 | ||
802 | if (sa->util_avg > cap) | |
803 | sa->util_avg = cap; | |
804 | } else { | |
805 | sa->util_avg = cap; | |
806 | } | |
2b8c41da | 807 | } |
7dc603c9 | 808 | |
9f683953 VG |
809 | sa->runnable_avg = cpu_scale; |
810 | ||
d0fe0b9c DE |
811 | if (p->sched_class != &fair_sched_class) { |
812 | /* | |
813 | * For !fair tasks do: | |
814 | * | |
815 | update_cfs_rq_load_avg(now, cfs_rq); | |
a4f9a0e5 | 816 | attach_entity_load_avg(cfs_rq, se); |
d0fe0b9c DE |
817 | switched_from_fair(rq, p); |
818 | * | |
819 | * such that the next switched_to_fair() has the | |
820 | * expected state. | |
821 | */ | |
822 | se->avg.last_update_time = cfs_rq_clock_pelt(cfs_rq); | |
823 | return; | |
7dc603c9 PZ |
824 | } |
825 | ||
df217913 | 826 | attach_entity_cfs_rq(se); |
2b8c41da YD |
827 | } |
828 | ||
7dc603c9 | 829 | #else /* !CONFIG_SMP */ |
540247fb | 830 | void init_entity_runnable_average(struct sched_entity *se) |
a75cdaa9 AS |
831 | { |
832 | } | |
d0fe0b9c | 833 | void post_init_entity_util_avg(struct task_struct *p) |
2b8c41da YD |
834 | { |
835 | } | |
3d30544f PZ |
836 | static void update_tg_load_avg(struct cfs_rq *cfs_rq, int force) |
837 | { | |
838 | } | |
7dc603c9 | 839 | #endif /* CONFIG_SMP */ |
a75cdaa9 | 840 | |
bf0f6f24 | 841 | /* |
9dbdb155 | 842 | * Update the current task's runtime statistics. |
bf0f6f24 | 843 | */ |
b7cc0896 | 844 | static void update_curr(struct cfs_rq *cfs_rq) |
bf0f6f24 | 845 | { |
429d43bc | 846 | struct sched_entity *curr = cfs_rq->curr; |
78becc27 | 847 | u64 now = rq_clock_task(rq_of(cfs_rq)); |
9dbdb155 | 848 | u64 delta_exec; |
bf0f6f24 IM |
849 | |
850 | if (unlikely(!curr)) | |
851 | return; | |
852 | ||
9dbdb155 PZ |
853 | delta_exec = now - curr->exec_start; |
854 | if (unlikely((s64)delta_exec <= 0)) | |
34f28ecd | 855 | return; |
bf0f6f24 | 856 | |
8ebc91d9 | 857 | curr->exec_start = now; |
d842de87 | 858 | |
9dbdb155 PZ |
859 | schedstat_set(curr->statistics.exec_max, |
860 | max(delta_exec, curr->statistics.exec_max)); | |
861 | ||
862 | curr->sum_exec_runtime += delta_exec; | |
ae92882e | 863 | schedstat_add(cfs_rq->exec_clock, delta_exec); |
9dbdb155 PZ |
864 | |
865 | curr->vruntime += calc_delta_fair(delta_exec, curr); | |
866 | update_min_vruntime(cfs_rq); | |
867 | ||
d842de87 SV |
868 | if (entity_is_task(curr)) { |
869 | struct task_struct *curtask = task_of(curr); | |
870 | ||
f977bb49 | 871 | trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime); |
d2cc5ed6 | 872 | cgroup_account_cputime(curtask, delta_exec); |
f06febc9 | 873 | account_group_exec_runtime(curtask, delta_exec); |
d842de87 | 874 | } |
ec12cb7f PT |
875 | |
876 | account_cfs_rq_runtime(cfs_rq, delta_exec); | |
bf0f6f24 IM |
877 | } |
878 | ||
6e998916 SG |
879 | static void update_curr_fair(struct rq *rq) |
880 | { | |
881 | update_curr(cfs_rq_of(&rq->curr->se)); | |
882 | } | |
883 | ||
bf0f6f24 | 884 | static inline void |
5870db5b | 885 | update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 | 886 | { |
4fa8d299 JP |
887 | u64 wait_start, prev_wait_start; |
888 | ||
889 | if (!schedstat_enabled()) | |
890 | return; | |
891 | ||
892 | wait_start = rq_clock(rq_of(cfs_rq)); | |
893 | prev_wait_start = schedstat_val(se->statistics.wait_start); | |
3ea94de1 JP |
894 | |
895 | if (entity_is_task(se) && task_on_rq_migrating(task_of(se)) && | |
4fa8d299 JP |
896 | likely(wait_start > prev_wait_start)) |
897 | wait_start -= prev_wait_start; | |
3ea94de1 | 898 | |
2ed41a55 | 899 | __schedstat_set(se->statistics.wait_start, wait_start); |
bf0f6f24 IM |
900 | } |
901 | ||
4fa8d299 | 902 | static inline void |
3ea94de1 JP |
903 | update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se) |
904 | { | |
905 | struct task_struct *p; | |
cb251765 MG |
906 | u64 delta; |
907 | ||
4fa8d299 JP |
908 | if (!schedstat_enabled()) |
909 | return; | |
910 | ||
911 | delta = rq_clock(rq_of(cfs_rq)) - schedstat_val(se->statistics.wait_start); | |
3ea94de1 JP |
912 | |
913 | if (entity_is_task(se)) { | |
914 | p = task_of(se); | |
915 | if (task_on_rq_migrating(p)) { | |
916 | /* | |
917 | * Preserve migrating task's wait time so wait_start | |
918 | * time stamp can be adjusted to accumulate wait time | |
919 | * prior to migration. | |
920 | */ | |
2ed41a55 | 921 | __schedstat_set(se->statistics.wait_start, delta); |
3ea94de1 JP |
922 | return; |
923 | } | |
924 | trace_sched_stat_wait(p, delta); | |
925 | } | |
926 | ||
2ed41a55 | 927 | __schedstat_set(se->statistics.wait_max, |
4fa8d299 | 928 | max(schedstat_val(se->statistics.wait_max), delta)); |
2ed41a55 PZ |
929 | __schedstat_inc(se->statistics.wait_count); |
930 | __schedstat_add(se->statistics.wait_sum, delta); | |
931 | __schedstat_set(se->statistics.wait_start, 0); | |
3ea94de1 | 932 | } |
3ea94de1 | 933 | |
4fa8d299 | 934 | static inline void |
1a3d027c JP |
935 | update_stats_enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se) |
936 | { | |
937 | struct task_struct *tsk = NULL; | |
4fa8d299 JP |
938 | u64 sleep_start, block_start; |
939 | ||
940 | if (!schedstat_enabled()) | |
941 | return; | |
942 | ||
943 | sleep_start = schedstat_val(se->statistics.sleep_start); | |
944 | block_start = schedstat_val(se->statistics.block_start); | |
1a3d027c JP |
945 | |
946 | if (entity_is_task(se)) | |
947 | tsk = task_of(se); | |
948 | ||
4fa8d299 JP |
949 | if (sleep_start) { |
950 | u64 delta = rq_clock(rq_of(cfs_rq)) - sleep_start; | |
1a3d027c JP |
951 | |
952 | if ((s64)delta < 0) | |
953 | delta = 0; | |
954 | ||
4fa8d299 | 955 | if (unlikely(delta > schedstat_val(se->statistics.sleep_max))) |
2ed41a55 | 956 | __schedstat_set(se->statistics.sleep_max, delta); |
1a3d027c | 957 | |
2ed41a55 PZ |
958 | __schedstat_set(se->statistics.sleep_start, 0); |
959 | __schedstat_add(se->statistics.sum_sleep_runtime, delta); | |
1a3d027c JP |
960 | |
961 | if (tsk) { | |
962 | account_scheduler_latency(tsk, delta >> 10, 1); | |
963 | trace_sched_stat_sleep(tsk, delta); | |
964 | } | |
965 | } | |
4fa8d299 JP |
966 | if (block_start) { |
967 | u64 delta = rq_clock(rq_of(cfs_rq)) - block_start; | |
1a3d027c JP |
968 | |
969 | if ((s64)delta < 0) | |
970 | delta = 0; | |
971 | ||
4fa8d299 | 972 | if (unlikely(delta > schedstat_val(se->statistics.block_max))) |
2ed41a55 | 973 | __schedstat_set(se->statistics.block_max, delta); |
1a3d027c | 974 | |
2ed41a55 PZ |
975 | __schedstat_set(se->statistics.block_start, 0); |
976 | __schedstat_add(se->statistics.sum_sleep_runtime, delta); | |
1a3d027c JP |
977 | |
978 | if (tsk) { | |
979 | if (tsk->in_iowait) { | |
2ed41a55 PZ |
980 | __schedstat_add(se->statistics.iowait_sum, delta); |
981 | __schedstat_inc(se->statistics.iowait_count); | |
1a3d027c JP |
982 | trace_sched_stat_iowait(tsk, delta); |
983 | } | |
984 | ||
985 | trace_sched_stat_blocked(tsk, delta); | |
986 | ||
987 | /* | |
988 | * Blocking time is in units of nanosecs, so shift by | |
989 | * 20 to get a milliseconds-range estimation of the | |
990 | * amount of time that the task spent sleeping: | |
991 | */ | |
992 | if (unlikely(prof_on == SLEEP_PROFILING)) { | |
993 | profile_hits(SLEEP_PROFILING, | |
994 | (void *)get_wchan(tsk), | |
995 | delta >> 20); | |
996 | } | |
997 | account_scheduler_latency(tsk, delta >> 10, 0); | |
998 | } | |
999 | } | |
3ea94de1 | 1000 | } |
3ea94de1 | 1001 | |
bf0f6f24 IM |
1002 | /* |
1003 | * Task is being enqueued - update stats: | |
1004 | */ | |
cb251765 | 1005 | static inline void |
1a3d027c | 1006 | update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
bf0f6f24 | 1007 | { |
4fa8d299 JP |
1008 | if (!schedstat_enabled()) |
1009 | return; | |
1010 | ||
bf0f6f24 IM |
1011 | /* |
1012 | * Are we enqueueing a waiting task? (for current tasks | |
1013 | * a dequeue/enqueue event is a NOP) | |
1014 | */ | |
429d43bc | 1015 | if (se != cfs_rq->curr) |
5870db5b | 1016 | update_stats_wait_start(cfs_rq, se); |
1a3d027c JP |
1017 | |
1018 | if (flags & ENQUEUE_WAKEUP) | |
1019 | update_stats_enqueue_sleeper(cfs_rq, se); | |
bf0f6f24 IM |
1020 | } |
1021 | ||
bf0f6f24 | 1022 | static inline void |
cb251765 | 1023 | update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
bf0f6f24 | 1024 | { |
4fa8d299 JP |
1025 | |
1026 | if (!schedstat_enabled()) | |
1027 | return; | |
1028 | ||
bf0f6f24 IM |
1029 | /* |
1030 | * Mark the end of the wait period if dequeueing a | |
1031 | * waiting task: | |
1032 | */ | |
429d43bc | 1033 | if (se != cfs_rq->curr) |
9ef0a961 | 1034 | update_stats_wait_end(cfs_rq, se); |
cb251765 | 1035 | |
4fa8d299 JP |
1036 | if ((flags & DEQUEUE_SLEEP) && entity_is_task(se)) { |
1037 | struct task_struct *tsk = task_of(se); | |
cb251765 | 1038 | |
4fa8d299 | 1039 | if (tsk->state & TASK_INTERRUPTIBLE) |
2ed41a55 | 1040 | __schedstat_set(se->statistics.sleep_start, |
4fa8d299 JP |
1041 | rq_clock(rq_of(cfs_rq))); |
1042 | if (tsk->state & TASK_UNINTERRUPTIBLE) | |
2ed41a55 | 1043 | __schedstat_set(se->statistics.block_start, |
4fa8d299 | 1044 | rq_clock(rq_of(cfs_rq))); |
cb251765 | 1045 | } |
cb251765 MG |
1046 | } |
1047 | ||
bf0f6f24 IM |
1048 | /* |
1049 | * We are picking a new current task - update its stats: | |
1050 | */ | |
1051 | static inline void | |
79303e9e | 1052 | update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 IM |
1053 | { |
1054 | /* | |
1055 | * We are starting a new run period: | |
1056 | */ | |
78becc27 | 1057 | se->exec_start = rq_clock_task(rq_of(cfs_rq)); |
bf0f6f24 IM |
1058 | } |
1059 | ||
bf0f6f24 IM |
1060 | /************************************************** |
1061 | * Scheduling class queueing methods: | |
1062 | */ | |
1063 | ||
cbee9f88 PZ |
1064 | #ifdef CONFIG_NUMA_BALANCING |
1065 | /* | |
598f0ec0 MG |
1066 | * Approximate time to scan a full NUMA task in ms. The task scan period is |
1067 | * calculated based on the tasks virtual memory size and | |
1068 | * numa_balancing_scan_size. | |
cbee9f88 | 1069 | */ |
598f0ec0 MG |
1070 | unsigned int sysctl_numa_balancing_scan_period_min = 1000; |
1071 | unsigned int sysctl_numa_balancing_scan_period_max = 60000; | |
6e5fb223 PZ |
1072 | |
1073 | /* Portion of address space to scan in MB */ | |
1074 | unsigned int sysctl_numa_balancing_scan_size = 256; | |
cbee9f88 | 1075 | |
4b96a29b PZ |
1076 | /* Scan @scan_size MB every @scan_period after an initial @scan_delay in ms */ |
1077 | unsigned int sysctl_numa_balancing_scan_delay = 1000; | |
1078 | ||
b5dd77c8 | 1079 | struct numa_group { |
c45a7795 | 1080 | refcount_t refcount; |
b5dd77c8 RR |
1081 | |
1082 | spinlock_t lock; /* nr_tasks, tasks */ | |
1083 | int nr_tasks; | |
1084 | pid_t gid; | |
1085 | int active_nodes; | |
1086 | ||
1087 | struct rcu_head rcu; | |
1088 | unsigned long total_faults; | |
1089 | unsigned long max_faults_cpu; | |
1090 | /* | |
1091 | * Faults_cpu is used to decide whether memory should move | |
1092 | * towards the CPU. As a consequence, these stats are weighted | |
1093 | * more by CPU use than by memory faults. | |
1094 | */ | |
1095 | unsigned long *faults_cpu; | |
04f5c362 | 1096 | unsigned long faults[]; |
b5dd77c8 RR |
1097 | }; |
1098 | ||
cb361d8c JH |
1099 | /* |
1100 | * For functions that can be called in multiple contexts that permit reading | |
1101 | * ->numa_group (see struct task_struct for locking rules). | |
1102 | */ | |
1103 | static struct numa_group *deref_task_numa_group(struct task_struct *p) | |
1104 | { | |
1105 | return rcu_dereference_check(p->numa_group, p == current || | |
1106 | (lockdep_is_held(&task_rq(p)->lock) && !READ_ONCE(p->on_cpu))); | |
1107 | } | |
1108 | ||
1109 | static struct numa_group *deref_curr_numa_group(struct task_struct *p) | |
1110 | { | |
1111 | return rcu_dereference_protected(p->numa_group, p == current); | |
1112 | } | |
1113 | ||
b5dd77c8 RR |
1114 | static inline unsigned long group_faults_priv(struct numa_group *ng); |
1115 | static inline unsigned long group_faults_shared(struct numa_group *ng); | |
1116 | ||
598f0ec0 MG |
1117 | static unsigned int task_nr_scan_windows(struct task_struct *p) |
1118 | { | |
1119 | unsigned long rss = 0; | |
1120 | unsigned long nr_scan_pages; | |
1121 | ||
1122 | /* | |
1123 | * Calculations based on RSS as non-present and empty pages are skipped | |
1124 | * by the PTE scanner and NUMA hinting faults should be trapped based | |
1125 | * on resident pages | |
1126 | */ | |
1127 | nr_scan_pages = sysctl_numa_balancing_scan_size << (20 - PAGE_SHIFT); | |
1128 | rss = get_mm_rss(p->mm); | |
1129 | if (!rss) | |
1130 | rss = nr_scan_pages; | |
1131 | ||
1132 | rss = round_up(rss, nr_scan_pages); | |
1133 | return rss / nr_scan_pages; | |
1134 | } | |
1135 | ||
1136 | /* For sanitys sake, never scan more PTEs than MAX_SCAN_WINDOW MB/sec. */ | |
1137 | #define MAX_SCAN_WINDOW 2560 | |
1138 | ||
1139 | static unsigned int task_scan_min(struct task_struct *p) | |
1140 | { | |
316c1608 | 1141 | unsigned int scan_size = READ_ONCE(sysctl_numa_balancing_scan_size); |
598f0ec0 MG |
1142 | unsigned int scan, floor; |
1143 | unsigned int windows = 1; | |
1144 | ||
64192658 KT |
1145 | if (scan_size < MAX_SCAN_WINDOW) |
1146 | windows = MAX_SCAN_WINDOW / scan_size; | |
598f0ec0 MG |
1147 | floor = 1000 / windows; |
1148 | ||
1149 | scan = sysctl_numa_balancing_scan_period_min / task_nr_scan_windows(p); | |
1150 | return max_t(unsigned int, floor, scan); | |
1151 | } | |
1152 | ||
b5dd77c8 RR |
1153 | static unsigned int task_scan_start(struct task_struct *p) |
1154 | { | |
1155 | unsigned long smin = task_scan_min(p); | |
1156 | unsigned long period = smin; | |
cb361d8c | 1157 | struct numa_group *ng; |
b5dd77c8 RR |
1158 | |
1159 | /* Scale the maximum scan period with the amount of shared memory. */ | |
cb361d8c JH |
1160 | rcu_read_lock(); |
1161 | ng = rcu_dereference(p->numa_group); | |
1162 | if (ng) { | |
b5dd77c8 RR |
1163 | unsigned long shared = group_faults_shared(ng); |
1164 | unsigned long private = group_faults_priv(ng); | |
1165 | ||
c45a7795 | 1166 | period *= refcount_read(&ng->refcount); |
b5dd77c8 RR |
1167 | period *= shared + 1; |
1168 | period /= private + shared + 1; | |
1169 | } | |
cb361d8c | 1170 | rcu_read_unlock(); |
b5dd77c8 RR |
1171 | |
1172 | return max(smin, period); | |
1173 | } | |
1174 | ||
598f0ec0 MG |
1175 | static unsigned int task_scan_max(struct task_struct *p) |
1176 | { | |
b5dd77c8 RR |
1177 | unsigned long smin = task_scan_min(p); |
1178 | unsigned long smax; | |
cb361d8c | 1179 | struct numa_group *ng; |
598f0ec0 MG |
1180 | |
1181 | /* Watch for min being lower than max due to floor calculations */ | |
1182 | smax = sysctl_numa_balancing_scan_period_max / task_nr_scan_windows(p); | |
b5dd77c8 RR |
1183 | |
1184 | /* Scale the maximum scan period with the amount of shared memory. */ | |
cb361d8c JH |
1185 | ng = deref_curr_numa_group(p); |
1186 | if (ng) { | |
b5dd77c8 RR |
1187 | unsigned long shared = group_faults_shared(ng); |
1188 | unsigned long private = group_faults_priv(ng); | |
1189 | unsigned long period = smax; | |
1190 | ||
c45a7795 | 1191 | period *= refcount_read(&ng->refcount); |
b5dd77c8 RR |
1192 | period *= shared + 1; |
1193 | period /= private + shared + 1; | |
1194 | ||
1195 | smax = max(smax, period); | |
1196 | } | |
1197 | ||
598f0ec0 MG |
1198 | return max(smin, smax); |
1199 | } | |
1200 | ||
0ec8aa00 PZ |
1201 | static void account_numa_enqueue(struct rq *rq, struct task_struct *p) |
1202 | { | |
98fa15f3 | 1203 | rq->nr_numa_running += (p->numa_preferred_nid != NUMA_NO_NODE); |
0ec8aa00 PZ |
1204 | rq->nr_preferred_running += (p->numa_preferred_nid == task_node(p)); |
1205 | } | |
1206 | ||
1207 | static void account_numa_dequeue(struct rq *rq, struct task_struct *p) | |
1208 | { | |
98fa15f3 | 1209 | rq->nr_numa_running -= (p->numa_preferred_nid != NUMA_NO_NODE); |
0ec8aa00 PZ |
1210 | rq->nr_preferred_running -= (p->numa_preferred_nid == task_node(p)); |
1211 | } | |
1212 | ||
be1e4e76 RR |
1213 | /* Shared or private faults. */ |
1214 | #define NR_NUMA_HINT_FAULT_TYPES 2 | |
1215 | ||
1216 | /* Memory and CPU locality */ | |
1217 | #define NR_NUMA_HINT_FAULT_STATS (NR_NUMA_HINT_FAULT_TYPES * 2) | |
1218 | ||
1219 | /* Averaged statistics, and temporary buffers. */ | |
1220 | #define NR_NUMA_HINT_FAULT_BUCKETS (NR_NUMA_HINT_FAULT_STATS * 2) | |
1221 | ||
e29cf08b MG |
1222 | pid_t task_numa_group_id(struct task_struct *p) |
1223 | { | |
cb361d8c JH |
1224 | struct numa_group *ng; |
1225 | pid_t gid = 0; | |
1226 | ||
1227 | rcu_read_lock(); | |
1228 | ng = rcu_dereference(p->numa_group); | |
1229 | if (ng) | |
1230 | gid = ng->gid; | |
1231 | rcu_read_unlock(); | |
1232 | ||
1233 | return gid; | |
e29cf08b MG |
1234 | } |
1235 | ||
44dba3d5 | 1236 | /* |
97fb7a0a | 1237 | * The averaged statistics, shared & private, memory & CPU, |
44dba3d5 IM |
1238 | * occupy the first half of the array. The second half of the |
1239 | * array is for current counters, which are averaged into the | |
1240 | * first set by task_numa_placement. | |
1241 | */ | |
1242 | static inline int task_faults_idx(enum numa_faults_stats s, int nid, int priv) | |
ac8e895b | 1243 | { |
44dba3d5 | 1244 | return NR_NUMA_HINT_FAULT_TYPES * (s * nr_node_ids + nid) + priv; |
ac8e895b MG |
1245 | } |
1246 | ||
1247 | static inline unsigned long task_faults(struct task_struct *p, int nid) | |
1248 | { | |
44dba3d5 | 1249 | if (!p->numa_faults) |
ac8e895b MG |
1250 | return 0; |
1251 | ||
44dba3d5 IM |
1252 | return p->numa_faults[task_faults_idx(NUMA_MEM, nid, 0)] + |
1253 | p->numa_faults[task_faults_idx(NUMA_MEM, nid, 1)]; | |
ac8e895b MG |
1254 | } |
1255 | ||
83e1d2cd MG |
1256 | static inline unsigned long group_faults(struct task_struct *p, int nid) |
1257 | { | |
cb361d8c JH |
1258 | struct numa_group *ng = deref_task_numa_group(p); |
1259 | ||
1260 | if (!ng) | |
83e1d2cd MG |
1261 | return 0; |
1262 | ||
cb361d8c JH |
1263 | return ng->faults[task_faults_idx(NUMA_MEM, nid, 0)] + |
1264 | ng->faults[task_faults_idx(NUMA_MEM, nid, 1)]; | |
83e1d2cd MG |
1265 | } |
1266 | ||
20e07dea RR |
1267 | static inline unsigned long group_faults_cpu(struct numa_group *group, int nid) |
1268 | { | |
44dba3d5 IM |
1269 | return group->faults_cpu[task_faults_idx(NUMA_MEM, nid, 0)] + |
1270 | group->faults_cpu[task_faults_idx(NUMA_MEM, nid, 1)]; | |
20e07dea RR |
1271 | } |
1272 | ||
b5dd77c8 RR |
1273 | static inline unsigned long group_faults_priv(struct numa_group *ng) |
1274 | { | |
1275 | unsigned long faults = 0; | |
1276 | int node; | |
1277 | ||
1278 | for_each_online_node(node) { | |
1279 | faults += ng->faults[task_faults_idx(NUMA_MEM, node, 1)]; | |
1280 | } | |
1281 | ||
1282 | return faults; | |
1283 | } | |
1284 | ||
1285 | static inline unsigned long group_faults_shared(struct numa_group *ng) | |
1286 | { | |
1287 | unsigned long faults = 0; | |
1288 | int node; | |
1289 | ||
1290 | for_each_online_node(node) { | |
1291 | faults += ng->faults[task_faults_idx(NUMA_MEM, node, 0)]; | |
1292 | } | |
1293 | ||
1294 | return faults; | |
1295 | } | |
1296 | ||
4142c3eb RR |
1297 | /* |
1298 | * A node triggering more than 1/3 as many NUMA faults as the maximum is | |
1299 | * considered part of a numa group's pseudo-interleaving set. Migrations | |
1300 | * between these nodes are slowed down, to allow things to settle down. | |
1301 | */ | |
1302 | #define ACTIVE_NODE_FRACTION 3 | |
1303 | ||
1304 | static bool numa_is_active_node(int nid, struct numa_group *ng) | |
1305 | { | |
1306 | return group_faults_cpu(ng, nid) * ACTIVE_NODE_FRACTION > ng->max_faults_cpu; | |
1307 | } | |
1308 | ||
6c6b1193 RR |
1309 | /* Handle placement on systems where not all nodes are directly connected. */ |
1310 | static unsigned long score_nearby_nodes(struct task_struct *p, int nid, | |
1311 | int maxdist, bool task) | |
1312 | { | |
1313 | unsigned long score = 0; | |
1314 | int node; | |
1315 | ||
1316 | /* | |
1317 | * All nodes are directly connected, and the same distance | |
1318 | * from each other. No need for fancy placement algorithms. | |
1319 | */ | |
1320 | if (sched_numa_topology_type == NUMA_DIRECT) | |
1321 | return 0; | |
1322 | ||
1323 | /* | |
1324 | * This code is called for each node, introducing N^2 complexity, | |
1325 | * which should be ok given the number of nodes rarely exceeds 8. | |
1326 | */ | |
1327 | for_each_online_node(node) { | |
1328 | unsigned long faults; | |
1329 | int dist = node_distance(nid, node); | |
1330 | ||
1331 | /* | |
1332 | * The furthest away nodes in the system are not interesting | |
1333 | * for placement; nid was already counted. | |
1334 | */ | |
1335 | if (dist == sched_max_numa_distance || node == nid) | |
1336 | continue; | |
1337 | ||
1338 | /* | |
1339 | * On systems with a backplane NUMA topology, compare groups | |
1340 | * of nodes, and move tasks towards the group with the most | |
1341 | * memory accesses. When comparing two nodes at distance | |
1342 | * "hoplimit", only nodes closer by than "hoplimit" are part | |
1343 | * of each group. Skip other nodes. | |
1344 | */ | |
1345 | if (sched_numa_topology_type == NUMA_BACKPLANE && | |
0ee7e74d | 1346 | dist >= maxdist) |
6c6b1193 RR |
1347 | continue; |
1348 | ||
1349 | /* Add up the faults from nearby nodes. */ | |
1350 | if (task) | |
1351 | faults = task_faults(p, node); | |
1352 | else | |
1353 | faults = group_faults(p, node); | |
1354 | ||
1355 | /* | |
1356 | * On systems with a glueless mesh NUMA topology, there are | |
1357 | * no fixed "groups of nodes". Instead, nodes that are not | |
1358 | * directly connected bounce traffic through intermediate | |
1359 | * nodes; a numa_group can occupy any set of nodes. | |
1360 | * The further away a node is, the less the faults count. | |
1361 | * This seems to result in good task placement. | |
1362 | */ | |
1363 | if (sched_numa_topology_type == NUMA_GLUELESS_MESH) { | |
1364 | faults *= (sched_max_numa_distance - dist); | |
1365 | faults /= (sched_max_numa_distance - LOCAL_DISTANCE); | |
1366 | } | |
1367 | ||
1368 | score += faults; | |
1369 | } | |
1370 | ||
1371 | return score; | |
1372 | } | |
1373 | ||
83e1d2cd MG |
1374 | /* |
1375 | * These return the fraction of accesses done by a particular task, or | |
1376 | * task group, on a particular numa node. The group weight is given a | |
1377 | * larger multiplier, in order to group tasks together that are almost | |
1378 | * evenly spread out between numa nodes. | |
1379 | */ | |
7bd95320 RR |
1380 | static inline unsigned long task_weight(struct task_struct *p, int nid, |
1381 | int dist) | |
83e1d2cd | 1382 | { |
7bd95320 | 1383 | unsigned long faults, total_faults; |
83e1d2cd | 1384 | |
44dba3d5 | 1385 | if (!p->numa_faults) |
83e1d2cd MG |
1386 | return 0; |
1387 | ||
1388 | total_faults = p->total_numa_faults; | |
1389 | ||
1390 | if (!total_faults) | |
1391 | return 0; | |
1392 | ||
7bd95320 | 1393 | faults = task_faults(p, nid); |
6c6b1193 RR |
1394 | faults += score_nearby_nodes(p, nid, dist, true); |
1395 | ||
7bd95320 | 1396 | return 1000 * faults / total_faults; |
83e1d2cd MG |
1397 | } |
1398 | ||
7bd95320 RR |
1399 | static inline unsigned long group_weight(struct task_struct *p, int nid, |
1400 | int dist) | |
83e1d2cd | 1401 | { |
cb361d8c | 1402 | struct numa_group *ng = deref_task_numa_group(p); |
7bd95320 RR |
1403 | unsigned long faults, total_faults; |
1404 | ||
cb361d8c | 1405 | if (!ng) |
7bd95320 RR |
1406 | return 0; |
1407 | ||
cb361d8c | 1408 | total_faults = ng->total_faults; |
7bd95320 RR |
1409 | |
1410 | if (!total_faults) | |
83e1d2cd MG |
1411 | return 0; |
1412 | ||
7bd95320 | 1413 | faults = group_faults(p, nid); |
6c6b1193 RR |
1414 | faults += score_nearby_nodes(p, nid, dist, false); |
1415 | ||
7bd95320 | 1416 | return 1000 * faults / total_faults; |
83e1d2cd MG |
1417 | } |
1418 | ||
10f39042 RR |
1419 | bool should_numa_migrate_memory(struct task_struct *p, struct page * page, |
1420 | int src_nid, int dst_cpu) | |
1421 | { | |
cb361d8c | 1422 | struct numa_group *ng = deref_curr_numa_group(p); |
10f39042 RR |
1423 | int dst_nid = cpu_to_node(dst_cpu); |
1424 | int last_cpupid, this_cpupid; | |
1425 | ||
1426 | this_cpupid = cpu_pid_to_cpupid(dst_cpu, current->pid); | |
37355bdc MG |
1427 | last_cpupid = page_cpupid_xchg_last(page, this_cpupid); |
1428 | ||
1429 | /* | |
1430 | * Allow first faults or private faults to migrate immediately early in | |
1431 | * the lifetime of a task. The magic number 4 is based on waiting for | |
1432 | * two full passes of the "multi-stage node selection" test that is | |
1433 | * executed below. | |
1434 | */ | |
98fa15f3 | 1435 | if ((p->numa_preferred_nid == NUMA_NO_NODE || p->numa_scan_seq <= 4) && |
37355bdc MG |
1436 | (cpupid_pid_unset(last_cpupid) || cpupid_match_pid(p, last_cpupid))) |
1437 | return true; | |
10f39042 RR |
1438 | |
1439 | /* | |
1440 | * Multi-stage node selection is used in conjunction with a periodic | |
1441 | * migration fault to build a temporal task<->page relation. By using | |
1442 | * a two-stage filter we remove short/unlikely relations. | |
1443 | * | |
1444 | * Using P(p) ~ n_p / n_t as per frequentist probability, we can equate | |
1445 | * a task's usage of a particular page (n_p) per total usage of this | |
1446 | * page (n_t) (in a given time-span) to a probability. | |
1447 | * | |
1448 | * Our periodic faults will sample this probability and getting the | |
1449 | * same result twice in a row, given these samples are fully | |
1450 | * independent, is then given by P(n)^2, provided our sample period | |
1451 | * is sufficiently short compared to the usage pattern. | |
1452 | * | |
1453 | * This quadric squishes small probabilities, making it less likely we | |
1454 | * act on an unlikely task<->page relation. | |
1455 | */ | |
10f39042 RR |
1456 | if (!cpupid_pid_unset(last_cpupid) && |
1457 | cpupid_to_nid(last_cpupid) != dst_nid) | |
1458 | return false; | |
1459 | ||
1460 | /* Always allow migrate on private faults */ | |
1461 | if (cpupid_match_pid(p, last_cpupid)) | |
1462 | return true; | |
1463 | ||
1464 | /* A shared fault, but p->numa_group has not been set up yet. */ | |
1465 | if (!ng) | |
1466 | return true; | |
1467 | ||
1468 | /* | |
4142c3eb RR |
1469 | * Destination node is much more heavily used than the source |
1470 | * node? Allow migration. | |
10f39042 | 1471 | */ |
4142c3eb RR |
1472 | if (group_faults_cpu(ng, dst_nid) > group_faults_cpu(ng, src_nid) * |
1473 | ACTIVE_NODE_FRACTION) | |
10f39042 RR |
1474 | return true; |
1475 | ||
1476 | /* | |
4142c3eb RR |
1477 | * Distribute memory according to CPU & memory use on each node, |
1478 | * with 3/4 hysteresis to avoid unnecessary memory migrations: | |
1479 | * | |
1480 | * faults_cpu(dst) 3 faults_cpu(src) | |
1481 | * --------------- * - > --------------- | |
1482 | * faults_mem(dst) 4 faults_mem(src) | |
10f39042 | 1483 | */ |
4142c3eb RR |
1484 | return group_faults_cpu(ng, dst_nid) * group_faults(p, src_nid) * 3 > |
1485 | group_faults_cpu(ng, src_nid) * group_faults(p, dst_nid) * 4; | |
10f39042 RR |
1486 | } |
1487 | ||
6499b1b2 VG |
1488 | /* |
1489 | * 'numa_type' describes the node at the moment of load balancing. | |
1490 | */ | |
1491 | enum numa_type { | |
1492 | /* The node has spare capacity that can be used to run more tasks. */ | |
1493 | node_has_spare = 0, | |
1494 | /* | |
1495 | * The node is fully used and the tasks don't compete for more CPU | |
1496 | * cycles. Nevertheless, some tasks might wait before running. | |
1497 | */ | |
1498 | node_fully_busy, | |
1499 | /* | |
1500 | * The node is overloaded and can't provide expected CPU cycles to all | |
1501 | * tasks. | |
1502 | */ | |
1503 | node_overloaded | |
1504 | }; | |
58d081b5 | 1505 | |
fb13c7ee | 1506 | /* Cached statistics for all CPUs within a node */ |
58d081b5 MG |
1507 | struct numa_stats { |
1508 | unsigned long load; | |
6499b1b2 | 1509 | unsigned long util; |
fb13c7ee | 1510 | /* Total compute capacity of CPUs on a node */ |
5ef20ca1 | 1511 | unsigned long compute_capacity; |
6499b1b2 VG |
1512 | unsigned int nr_running; |
1513 | unsigned int weight; | |
1514 | enum numa_type node_type; | |
ff7db0bf | 1515 | int idle_cpu; |
58d081b5 | 1516 | }; |
e6628d5b | 1517 | |
ff7db0bf MG |
1518 | static inline bool is_core_idle(int cpu) |
1519 | { | |
1520 | #ifdef CONFIG_SCHED_SMT | |
1521 | int sibling; | |
1522 | ||
1523 | for_each_cpu(sibling, cpu_smt_mask(cpu)) { | |
1524 | if (cpu == sibling) | |
1525 | continue; | |
1526 | ||
1527 | if (!idle_cpu(cpu)) | |
1528 | return false; | |
1529 | } | |
1530 | #endif | |
1531 | ||
1532 | return true; | |
1533 | } | |
1534 | ||
58d081b5 MG |
1535 | struct task_numa_env { |
1536 | struct task_struct *p; | |
e6628d5b | 1537 | |
58d081b5 MG |
1538 | int src_cpu, src_nid; |
1539 | int dst_cpu, dst_nid; | |
e6628d5b | 1540 | |
58d081b5 | 1541 | struct numa_stats src_stats, dst_stats; |
e6628d5b | 1542 | |
40ea2b42 | 1543 | int imbalance_pct; |
7bd95320 | 1544 | int dist; |
fb13c7ee MG |
1545 | |
1546 | struct task_struct *best_task; | |
1547 | long best_imp; | |
58d081b5 MG |
1548 | int best_cpu; |
1549 | }; | |
1550 | ||
6499b1b2 VG |
1551 | static unsigned long cpu_load(struct rq *rq); |
1552 | static unsigned long cpu_util(int cpu); | |
fb86f5b2 | 1553 | static inline long adjust_numa_imbalance(int imbalance, int src_nr_running); |
6499b1b2 VG |
1554 | |
1555 | static inline enum | |
1556 | numa_type numa_classify(unsigned int imbalance_pct, | |
1557 | struct numa_stats *ns) | |
1558 | { | |
1559 | if ((ns->nr_running > ns->weight) && | |
1560 | ((ns->compute_capacity * 100) < (ns->util * imbalance_pct))) | |
1561 | return node_overloaded; | |
1562 | ||
1563 | if ((ns->nr_running < ns->weight) || | |
1564 | ((ns->compute_capacity * 100) > (ns->util * imbalance_pct))) | |
1565 | return node_has_spare; | |
1566 | ||
1567 | return node_fully_busy; | |
1568 | } | |
1569 | ||
76c389ab VS |
1570 | #ifdef CONFIG_SCHED_SMT |
1571 | /* Forward declarations of select_idle_sibling helpers */ | |
1572 | static inline bool test_idle_cores(int cpu, bool def); | |
ff7db0bf MG |
1573 | static inline int numa_idle_core(int idle_core, int cpu) |
1574 | { | |
ff7db0bf MG |
1575 | if (!static_branch_likely(&sched_smt_present) || |
1576 | idle_core >= 0 || !test_idle_cores(cpu, false)) | |
1577 | return idle_core; | |
1578 | ||
1579 | /* | |
1580 | * Prefer cores instead of packing HT siblings | |
1581 | * and triggering future load balancing. | |
1582 | */ | |
1583 | if (is_core_idle(cpu)) | |
1584 | idle_core = cpu; | |
ff7db0bf MG |
1585 | |
1586 | return idle_core; | |
1587 | } | |
76c389ab VS |
1588 | #else |
1589 | static inline int numa_idle_core(int idle_core, int cpu) | |
1590 | { | |
1591 | return idle_core; | |
1592 | } | |
1593 | #endif | |
ff7db0bf | 1594 | |
6499b1b2 | 1595 | /* |
ff7db0bf MG |
1596 | * Gather all necessary information to make NUMA balancing placement |
1597 | * decisions that are compatible with standard load balancer. This | |
1598 | * borrows code and logic from update_sg_lb_stats but sharing a | |
1599 | * common implementation is impractical. | |
6499b1b2 VG |
1600 | */ |
1601 | static void update_numa_stats(struct task_numa_env *env, | |
ff7db0bf MG |
1602 | struct numa_stats *ns, int nid, |
1603 | bool find_idle) | |
6499b1b2 | 1604 | { |
ff7db0bf | 1605 | int cpu, idle_core = -1; |
6499b1b2 VG |
1606 | |
1607 | memset(ns, 0, sizeof(*ns)); | |
ff7db0bf MG |
1608 | ns->idle_cpu = -1; |
1609 | ||
0621df31 | 1610 | rcu_read_lock(); |
6499b1b2 VG |
1611 | for_each_cpu(cpu, cpumask_of_node(nid)) { |
1612 | struct rq *rq = cpu_rq(cpu); | |
1613 | ||
1614 | ns->load += cpu_load(rq); | |
1615 | ns->util += cpu_util(cpu); | |
1616 | ns->nr_running += rq->cfs.h_nr_running; | |
1617 | ns->compute_capacity += capacity_of(cpu); | |
ff7db0bf MG |
1618 | |
1619 | if (find_idle && !rq->nr_running && idle_cpu(cpu)) { | |
1620 | if (READ_ONCE(rq->numa_migrate_on) || | |
1621 | !cpumask_test_cpu(cpu, env->p->cpus_ptr)) | |
1622 | continue; | |
1623 | ||
1624 | if (ns->idle_cpu == -1) | |
1625 | ns->idle_cpu = cpu; | |
1626 | ||
1627 | idle_core = numa_idle_core(idle_core, cpu); | |
1628 | } | |
6499b1b2 | 1629 | } |
0621df31 | 1630 | rcu_read_unlock(); |
6499b1b2 VG |
1631 | |
1632 | ns->weight = cpumask_weight(cpumask_of_node(nid)); | |
1633 | ||
1634 | ns->node_type = numa_classify(env->imbalance_pct, ns); | |
ff7db0bf MG |
1635 | |
1636 | if (idle_core >= 0) | |
1637 | ns->idle_cpu = idle_core; | |
6499b1b2 VG |
1638 | } |
1639 | ||
fb13c7ee MG |
1640 | static void task_numa_assign(struct task_numa_env *env, |
1641 | struct task_struct *p, long imp) | |
1642 | { | |
a4739eca SD |
1643 | struct rq *rq = cpu_rq(env->dst_cpu); |
1644 | ||
5fb52dd9 MG |
1645 | /* Check if run-queue part of active NUMA balance. */ |
1646 | if (env->best_cpu != env->dst_cpu && xchg(&rq->numa_migrate_on, 1)) { | |
1647 | int cpu; | |
1648 | int start = env->dst_cpu; | |
1649 | ||
1650 | /* Find alternative idle CPU. */ | |
1651 | for_each_cpu_wrap(cpu, cpumask_of_node(env->dst_nid), start) { | |
1652 | if (cpu == env->best_cpu || !idle_cpu(cpu) || | |
1653 | !cpumask_test_cpu(cpu, env->p->cpus_ptr)) { | |
1654 | continue; | |
1655 | } | |
1656 | ||
1657 | env->dst_cpu = cpu; | |
1658 | rq = cpu_rq(env->dst_cpu); | |
1659 | if (!xchg(&rq->numa_migrate_on, 1)) | |
1660 | goto assign; | |
1661 | } | |
1662 | ||
1663 | /* Failed to find an alternative idle CPU */ | |
a4739eca | 1664 | return; |
5fb52dd9 | 1665 | } |
a4739eca | 1666 | |
5fb52dd9 | 1667 | assign: |
a4739eca SD |
1668 | /* |
1669 | * Clear previous best_cpu/rq numa-migrate flag, since task now | |
1670 | * found a better CPU to move/swap. | |
1671 | */ | |
5fb52dd9 | 1672 | if (env->best_cpu != -1 && env->best_cpu != env->dst_cpu) { |
a4739eca SD |
1673 | rq = cpu_rq(env->best_cpu); |
1674 | WRITE_ONCE(rq->numa_migrate_on, 0); | |
1675 | } | |
1676 | ||
fb13c7ee MG |
1677 | if (env->best_task) |
1678 | put_task_struct(env->best_task); | |
bac78573 ON |
1679 | if (p) |
1680 | get_task_struct(p); | |
fb13c7ee MG |
1681 | |
1682 | env->best_task = p; | |
1683 | env->best_imp = imp; | |
1684 | env->best_cpu = env->dst_cpu; | |
1685 | } | |
1686 | ||
28a21745 | 1687 | static bool load_too_imbalanced(long src_load, long dst_load, |
e63da036 RR |
1688 | struct task_numa_env *env) |
1689 | { | |
e4991b24 RR |
1690 | long imb, old_imb; |
1691 | long orig_src_load, orig_dst_load; | |
28a21745 RR |
1692 | long src_capacity, dst_capacity; |
1693 | ||
1694 | /* | |
1695 | * The load is corrected for the CPU capacity available on each node. | |
1696 | * | |
1697 | * src_load dst_load | |
1698 | * ------------ vs --------- | |
1699 | * src_capacity dst_capacity | |
1700 | */ | |
1701 | src_capacity = env->src_stats.compute_capacity; | |
1702 | dst_capacity = env->dst_stats.compute_capacity; | |
e63da036 | 1703 | |
5f95ba7a | 1704 | imb = abs(dst_load * src_capacity - src_load * dst_capacity); |
e63da036 | 1705 | |
28a21745 | 1706 | orig_src_load = env->src_stats.load; |
e4991b24 | 1707 | orig_dst_load = env->dst_stats.load; |
28a21745 | 1708 | |
5f95ba7a | 1709 | old_imb = abs(orig_dst_load * src_capacity - orig_src_load * dst_capacity); |
e4991b24 RR |
1710 | |
1711 | /* Would this change make things worse? */ | |
1712 | return (imb > old_imb); | |
e63da036 RR |
1713 | } |
1714 | ||
6fd98e77 SD |
1715 | /* |
1716 | * Maximum NUMA importance can be 1998 (2*999); | |
1717 | * SMALLIMP @ 30 would be close to 1998/64. | |
1718 | * Used to deter task migration. | |
1719 | */ | |
1720 | #define SMALLIMP 30 | |
1721 | ||
fb13c7ee MG |
1722 | /* |
1723 | * This checks if the overall compute and NUMA accesses of the system would | |
1724 | * be improved if the source tasks was migrated to the target dst_cpu taking | |
1725 | * into account that it might be best if task running on the dst_cpu should | |
1726 | * be exchanged with the source task | |
1727 | */ | |
a0f03b61 | 1728 | static bool task_numa_compare(struct task_numa_env *env, |
305c1fac | 1729 | long taskimp, long groupimp, bool maymove) |
fb13c7ee | 1730 | { |
cb361d8c | 1731 | struct numa_group *cur_ng, *p_ng = deref_curr_numa_group(env->p); |
fb13c7ee | 1732 | struct rq *dst_rq = cpu_rq(env->dst_cpu); |
cb361d8c | 1733 | long imp = p_ng ? groupimp : taskimp; |
fb13c7ee | 1734 | struct task_struct *cur; |
28a21745 | 1735 | long src_load, dst_load; |
7bd95320 | 1736 | int dist = env->dist; |
cb361d8c JH |
1737 | long moveimp = imp; |
1738 | long load; | |
a0f03b61 | 1739 | bool stopsearch = false; |
fb13c7ee | 1740 | |
a4739eca | 1741 | if (READ_ONCE(dst_rq->numa_migrate_on)) |
a0f03b61 | 1742 | return false; |
a4739eca | 1743 | |
fb13c7ee | 1744 | rcu_read_lock(); |
154abafc | 1745 | cur = rcu_dereference(dst_rq->curr); |
bac78573 | 1746 | if (cur && ((cur->flags & PF_EXITING) || is_idle_task(cur))) |
fb13c7ee MG |
1747 | cur = NULL; |
1748 | ||
7af68335 PZ |
1749 | /* |
1750 | * Because we have preemption enabled we can get migrated around and | |
1751 | * end try selecting ourselves (current == env->p) as a swap candidate. | |
1752 | */ | |
a0f03b61 MG |
1753 | if (cur == env->p) { |
1754 | stopsearch = true; | |
7af68335 | 1755 | goto unlock; |
a0f03b61 | 1756 | } |
7af68335 | 1757 | |
305c1fac | 1758 | if (!cur) { |
6fd98e77 | 1759 | if (maymove && moveimp >= env->best_imp) |
305c1fac SD |
1760 | goto assign; |
1761 | else | |
1762 | goto unlock; | |
1763 | } | |
1764 | ||
88cca72c MG |
1765 | /* Skip this swap candidate if cannot move to the source cpu. */ |
1766 | if (!cpumask_test_cpu(env->src_cpu, cur->cpus_ptr)) | |
1767 | goto unlock; | |
1768 | ||
1769 | /* | |
1770 | * Skip this swap candidate if it is not moving to its preferred | |
1771 | * node and the best task is. | |
1772 | */ | |
1773 | if (env->best_task && | |
1774 | env->best_task->numa_preferred_nid == env->src_nid && | |
1775 | cur->numa_preferred_nid != env->src_nid) { | |
1776 | goto unlock; | |
1777 | } | |
1778 | ||
fb13c7ee MG |
1779 | /* |
1780 | * "imp" is the fault differential for the source task between the | |
1781 | * source and destination node. Calculate the total differential for | |
1782 | * the source task and potential destination task. The more negative | |
305c1fac | 1783 | * the value is, the more remote accesses that would be expected to |
fb13c7ee | 1784 | * be incurred if the tasks were swapped. |
88cca72c | 1785 | * |
305c1fac SD |
1786 | * If dst and source tasks are in the same NUMA group, or not |
1787 | * in any group then look only at task weights. | |
1788 | */ | |
cb361d8c JH |
1789 | cur_ng = rcu_dereference(cur->numa_group); |
1790 | if (cur_ng == p_ng) { | |
305c1fac SD |
1791 | imp = taskimp + task_weight(cur, env->src_nid, dist) - |
1792 | task_weight(cur, env->dst_nid, dist); | |
887c290e | 1793 | /* |
305c1fac SD |
1794 | * Add some hysteresis to prevent swapping the |
1795 | * tasks within a group over tiny differences. | |
887c290e | 1796 | */ |
cb361d8c | 1797 | if (cur_ng) |
305c1fac SD |
1798 | imp -= imp / 16; |
1799 | } else { | |
1800 | /* | |
1801 | * Compare the group weights. If a task is all by itself | |
1802 | * (not part of a group), use the task weight instead. | |
1803 | */ | |
cb361d8c | 1804 | if (cur_ng && p_ng) |
305c1fac SD |
1805 | imp += group_weight(cur, env->src_nid, dist) - |
1806 | group_weight(cur, env->dst_nid, dist); | |
1807 | else | |
1808 | imp += task_weight(cur, env->src_nid, dist) - | |
1809 | task_weight(cur, env->dst_nid, dist); | |
fb13c7ee MG |
1810 | } |
1811 | ||
88cca72c MG |
1812 | /* Discourage picking a task already on its preferred node */ |
1813 | if (cur->numa_preferred_nid == env->dst_nid) | |
1814 | imp -= imp / 16; | |
1815 | ||
1816 | /* | |
1817 | * Encourage picking a task that moves to its preferred node. | |
1818 | * This potentially makes imp larger than it's maximum of | |
1819 | * 1998 (see SMALLIMP and task_weight for why) but in this | |
1820 | * case, it does not matter. | |
1821 | */ | |
1822 | if (cur->numa_preferred_nid == env->src_nid) | |
1823 | imp += imp / 8; | |
1824 | ||
305c1fac | 1825 | if (maymove && moveimp > imp && moveimp > env->best_imp) { |
6fd98e77 | 1826 | imp = moveimp; |
305c1fac | 1827 | cur = NULL; |
fb13c7ee | 1828 | goto assign; |
305c1fac | 1829 | } |
fb13c7ee | 1830 | |
88cca72c MG |
1831 | /* |
1832 | * Prefer swapping with a task moving to its preferred node over a | |
1833 | * task that is not. | |
1834 | */ | |
1835 | if (env->best_task && cur->numa_preferred_nid == env->src_nid && | |
1836 | env->best_task->numa_preferred_nid != env->src_nid) { | |
1837 | goto assign; | |
1838 | } | |
1839 | ||
6fd98e77 SD |
1840 | /* |
1841 | * If the NUMA importance is less than SMALLIMP, | |
1842 | * task migration might only result in ping pong | |
1843 | * of tasks and also hurt performance due to cache | |
1844 | * misses. | |
1845 | */ | |
1846 | if (imp < SMALLIMP || imp <= env->best_imp + SMALLIMP / 2) | |
1847 | goto unlock; | |
1848 | ||
fb13c7ee MG |
1849 | /* |
1850 | * In the overloaded case, try and keep the load balanced. | |
1851 | */ | |
305c1fac SD |
1852 | load = task_h_load(env->p) - task_h_load(cur); |
1853 | if (!load) | |
1854 | goto assign; | |
1855 | ||
e720fff6 PZ |
1856 | dst_load = env->dst_stats.load + load; |
1857 | src_load = env->src_stats.load - load; | |
fb13c7ee | 1858 | |
28a21745 | 1859 | if (load_too_imbalanced(src_load, dst_load, env)) |
fb13c7ee MG |
1860 | goto unlock; |
1861 | ||
305c1fac | 1862 | assign: |
ff7db0bf | 1863 | /* Evaluate an idle CPU for a task numa move. */ |
10e2f1ac | 1864 | if (!cur) { |
ff7db0bf MG |
1865 | int cpu = env->dst_stats.idle_cpu; |
1866 | ||
1867 | /* Nothing cached so current CPU went idle since the search. */ | |
1868 | if (cpu < 0) | |
1869 | cpu = env->dst_cpu; | |
1870 | ||
10e2f1ac | 1871 | /* |
ff7db0bf MG |
1872 | * If the CPU is no longer truly idle and the previous best CPU |
1873 | * is, keep using it. | |
10e2f1ac | 1874 | */ |
ff7db0bf MG |
1875 | if (!idle_cpu(cpu) && env->best_cpu >= 0 && |
1876 | idle_cpu(env->best_cpu)) { | |
1877 | cpu = env->best_cpu; | |
1878 | } | |
1879 | ||
ff7db0bf | 1880 | env->dst_cpu = cpu; |
10e2f1ac | 1881 | } |
ba7e5a27 | 1882 | |
fb13c7ee | 1883 | task_numa_assign(env, cur, imp); |
a0f03b61 MG |
1884 | |
1885 | /* | |
1886 | * If a move to idle is allowed because there is capacity or load | |
1887 | * balance improves then stop the search. While a better swap | |
1888 | * candidate may exist, a search is not free. | |
1889 | */ | |
1890 | if (maymove && !cur && env->best_cpu >= 0 && idle_cpu(env->best_cpu)) | |
1891 | stopsearch = true; | |
1892 | ||
1893 | /* | |
1894 | * If a swap candidate must be identified and the current best task | |
1895 | * moves its preferred node then stop the search. | |
1896 | */ | |
1897 | if (!maymove && env->best_task && | |
1898 | env->best_task->numa_preferred_nid == env->src_nid) { | |
1899 | stopsearch = true; | |
1900 | } | |
fb13c7ee MG |
1901 | unlock: |
1902 | rcu_read_unlock(); | |
a0f03b61 MG |
1903 | |
1904 | return stopsearch; | |
fb13c7ee MG |
1905 | } |
1906 | ||
887c290e RR |
1907 | static void task_numa_find_cpu(struct task_numa_env *env, |
1908 | long taskimp, long groupimp) | |
2c8a50aa | 1909 | { |
305c1fac | 1910 | bool maymove = false; |
2c8a50aa MG |
1911 | int cpu; |
1912 | ||
305c1fac | 1913 | /* |
fb86f5b2 MG |
1914 | * If dst node has spare capacity, then check if there is an |
1915 | * imbalance that would be overruled by the load balancer. | |
305c1fac | 1916 | */ |
fb86f5b2 MG |
1917 | if (env->dst_stats.node_type == node_has_spare) { |
1918 | unsigned int imbalance; | |
1919 | int src_running, dst_running; | |
1920 | ||
1921 | /* | |
1922 | * Would movement cause an imbalance? Note that if src has | |
1923 | * more running tasks that the imbalance is ignored as the | |
1924 | * move improves the imbalance from the perspective of the | |
1925 | * CPU load balancer. | |
1926 | * */ | |
1927 | src_running = env->src_stats.nr_running - 1; | |
1928 | dst_running = env->dst_stats.nr_running + 1; | |
1929 | imbalance = max(0, dst_running - src_running); | |
1930 | imbalance = adjust_numa_imbalance(imbalance, src_running); | |
1931 | ||
1932 | /* Use idle CPU if there is no imbalance */ | |
ff7db0bf | 1933 | if (!imbalance) { |
fb86f5b2 | 1934 | maymove = true; |
ff7db0bf MG |
1935 | if (env->dst_stats.idle_cpu >= 0) { |
1936 | env->dst_cpu = env->dst_stats.idle_cpu; | |
1937 | task_numa_assign(env, NULL, 0); | |
1938 | return; | |
1939 | } | |
1940 | } | |
fb86f5b2 MG |
1941 | } else { |
1942 | long src_load, dst_load, load; | |
1943 | /* | |
1944 | * If the improvement from just moving env->p direction is better | |
1945 | * than swapping tasks around, check if a move is possible. | |
1946 | */ | |
1947 | load = task_h_load(env->p); | |
1948 | dst_load = env->dst_stats.load + load; | |
1949 | src_load = env->src_stats.load - load; | |
1950 | maymove = !load_too_imbalanced(src_load, dst_load, env); | |
1951 | } | |
305c1fac | 1952 | |
2c8a50aa MG |
1953 | for_each_cpu(cpu, cpumask_of_node(env->dst_nid)) { |
1954 | /* Skip this CPU if the source task cannot migrate */ | |
3bd37062 | 1955 | if (!cpumask_test_cpu(cpu, env->p->cpus_ptr)) |
2c8a50aa MG |
1956 | continue; |
1957 | ||
1958 | env->dst_cpu = cpu; | |
a0f03b61 MG |
1959 | if (task_numa_compare(env, taskimp, groupimp, maymove)) |
1960 | break; | |
2c8a50aa MG |
1961 | } |
1962 | } | |
1963 | ||
58d081b5 MG |
1964 | static int task_numa_migrate(struct task_struct *p) |
1965 | { | |
58d081b5 MG |
1966 | struct task_numa_env env = { |
1967 | .p = p, | |
fb13c7ee | 1968 | |
58d081b5 | 1969 | .src_cpu = task_cpu(p), |
b32e86b4 | 1970 | .src_nid = task_node(p), |
fb13c7ee MG |
1971 | |
1972 | .imbalance_pct = 112, | |
1973 | ||
1974 | .best_task = NULL, | |
1975 | .best_imp = 0, | |
4142c3eb | 1976 | .best_cpu = -1, |
58d081b5 | 1977 | }; |
cb361d8c | 1978 | unsigned long taskweight, groupweight; |
58d081b5 | 1979 | struct sched_domain *sd; |
cb361d8c JH |
1980 | long taskimp, groupimp; |
1981 | struct numa_group *ng; | |
a4739eca | 1982 | struct rq *best_rq; |
7bd95320 | 1983 | int nid, ret, dist; |
e6628d5b | 1984 | |
58d081b5 | 1985 | /* |
fb13c7ee MG |
1986 | * Pick the lowest SD_NUMA domain, as that would have the smallest |
1987 | * imbalance and would be the first to start moving tasks about. | |
1988 | * | |
1989 | * And we want to avoid any moving of tasks about, as that would create | |
1990 | * random movement of tasks -- counter the numa conditions we're trying | |
1991 | * to satisfy here. | |
58d081b5 MG |
1992 | */ |
1993 | rcu_read_lock(); | |
fb13c7ee | 1994 | sd = rcu_dereference(per_cpu(sd_numa, env.src_cpu)); |
46a73e8a RR |
1995 | if (sd) |
1996 | env.imbalance_pct = 100 + (sd->imbalance_pct - 100) / 2; | |
e6628d5b MG |
1997 | rcu_read_unlock(); |
1998 | ||
46a73e8a RR |
1999 | /* |
2000 | * Cpusets can break the scheduler domain tree into smaller | |
2001 | * balance domains, some of which do not cross NUMA boundaries. | |
2002 | * Tasks that are "trapped" in such domains cannot be migrated | |
2003 | * elsewhere, so there is no point in (re)trying. | |
2004 | */ | |
2005 | if (unlikely(!sd)) { | |
8cd45eee | 2006 | sched_setnuma(p, task_node(p)); |
46a73e8a RR |
2007 | return -EINVAL; |
2008 | } | |
2009 | ||
2c8a50aa | 2010 | env.dst_nid = p->numa_preferred_nid; |
7bd95320 RR |
2011 | dist = env.dist = node_distance(env.src_nid, env.dst_nid); |
2012 | taskweight = task_weight(p, env.src_nid, dist); | |
2013 | groupweight = group_weight(p, env.src_nid, dist); | |
ff7db0bf | 2014 | update_numa_stats(&env, &env.src_stats, env.src_nid, false); |
7bd95320 RR |
2015 | taskimp = task_weight(p, env.dst_nid, dist) - taskweight; |
2016 | groupimp = group_weight(p, env.dst_nid, dist) - groupweight; | |
ff7db0bf | 2017 | update_numa_stats(&env, &env.dst_stats, env.dst_nid, true); |
58d081b5 | 2018 | |
a43455a1 | 2019 | /* Try to find a spot on the preferred nid. */ |
2d4056fa | 2020 | task_numa_find_cpu(&env, taskimp, groupimp); |
e1dda8a7 | 2021 | |
9de05d48 RR |
2022 | /* |
2023 | * Look at other nodes in these cases: | |
2024 | * - there is no space available on the preferred_nid | |
2025 | * - the task is part of a numa_group that is interleaved across | |
2026 | * multiple NUMA nodes; in order to better consolidate the group, | |
2027 | * we need to check other locations. | |
2028 | */ | |
cb361d8c JH |
2029 | ng = deref_curr_numa_group(p); |
2030 | if (env.best_cpu == -1 || (ng && ng->active_nodes > 1)) { | |
2c8a50aa MG |
2031 | for_each_online_node(nid) { |
2032 | if (nid == env.src_nid || nid == p->numa_preferred_nid) | |
2033 | continue; | |
58d081b5 | 2034 | |
7bd95320 | 2035 | dist = node_distance(env.src_nid, env.dst_nid); |
6c6b1193 RR |
2036 | if (sched_numa_topology_type == NUMA_BACKPLANE && |
2037 | dist != env.dist) { | |
2038 | taskweight = task_weight(p, env.src_nid, dist); | |
2039 | groupweight = group_weight(p, env.src_nid, dist); | |
2040 | } | |
7bd95320 | 2041 | |
83e1d2cd | 2042 | /* Only consider nodes where both task and groups benefit */ |
7bd95320 RR |
2043 | taskimp = task_weight(p, nid, dist) - taskweight; |
2044 | groupimp = group_weight(p, nid, dist) - groupweight; | |
887c290e | 2045 | if (taskimp < 0 && groupimp < 0) |
fb13c7ee MG |
2046 | continue; |
2047 | ||
7bd95320 | 2048 | env.dist = dist; |
2c8a50aa | 2049 | env.dst_nid = nid; |
ff7db0bf | 2050 | update_numa_stats(&env, &env.dst_stats, env.dst_nid, true); |
2d4056fa | 2051 | task_numa_find_cpu(&env, taskimp, groupimp); |
58d081b5 MG |
2052 | } |
2053 | } | |
2054 | ||
68d1b02a RR |
2055 | /* |
2056 | * If the task is part of a workload that spans multiple NUMA nodes, | |
2057 | * and is migrating into one of the workload's active nodes, remember | |
2058 | * this node as the task's preferred numa node, so the workload can | |
2059 | * settle down. | |
2060 | * A task that migrated to a second choice node will be better off | |
2061 | * trying for a better one later. Do not set the preferred node here. | |
2062 | */ | |
cb361d8c | 2063 | if (ng) { |
db015dae RR |
2064 | if (env.best_cpu == -1) |
2065 | nid = env.src_nid; | |
2066 | else | |
8cd45eee | 2067 | nid = cpu_to_node(env.best_cpu); |
db015dae | 2068 | |
8cd45eee SD |
2069 | if (nid != p->numa_preferred_nid) |
2070 | sched_setnuma(p, nid); | |
db015dae RR |
2071 | } |
2072 | ||
2073 | /* No better CPU than the current one was found. */ | |
f22aef4a | 2074 | if (env.best_cpu == -1) { |
b2b2042b | 2075 | trace_sched_stick_numa(p, env.src_cpu, NULL, -1); |
db015dae | 2076 | return -EAGAIN; |
f22aef4a | 2077 | } |
0ec8aa00 | 2078 | |
a4739eca | 2079 | best_rq = cpu_rq(env.best_cpu); |
fb13c7ee | 2080 | if (env.best_task == NULL) { |
286549dc | 2081 | ret = migrate_task_to(p, env.best_cpu); |
a4739eca | 2082 | WRITE_ONCE(best_rq->numa_migrate_on, 0); |
286549dc | 2083 | if (ret != 0) |
b2b2042b | 2084 | trace_sched_stick_numa(p, env.src_cpu, NULL, env.best_cpu); |
fb13c7ee MG |
2085 | return ret; |
2086 | } | |
2087 | ||
0ad4e3df | 2088 | ret = migrate_swap(p, env.best_task, env.best_cpu, env.src_cpu); |
a4739eca | 2089 | WRITE_ONCE(best_rq->numa_migrate_on, 0); |
0ad4e3df | 2090 | |
286549dc | 2091 | if (ret != 0) |
b2b2042b | 2092 | trace_sched_stick_numa(p, env.src_cpu, env.best_task, env.best_cpu); |
fb13c7ee MG |
2093 | put_task_struct(env.best_task); |
2094 | return ret; | |
e6628d5b MG |
2095 | } |
2096 | ||
6b9a7460 MG |
2097 | /* Attempt to migrate a task to a CPU on the preferred node. */ |
2098 | static void numa_migrate_preferred(struct task_struct *p) | |
2099 | { | |
5085e2a3 RR |
2100 | unsigned long interval = HZ; |
2101 | ||
2739d3ee | 2102 | /* This task has no NUMA fault statistics yet */ |
98fa15f3 | 2103 | if (unlikely(p->numa_preferred_nid == NUMA_NO_NODE || !p->numa_faults)) |
6b9a7460 MG |
2104 | return; |
2105 | ||
2739d3ee | 2106 | /* Periodically retry migrating the task to the preferred node */ |
5085e2a3 | 2107 | interval = min(interval, msecs_to_jiffies(p->numa_scan_period) / 16); |
789ba280 | 2108 | p->numa_migrate_retry = jiffies + interval; |
2739d3ee RR |
2109 | |
2110 | /* Success if task is already running on preferred CPU */ | |
de1b301a | 2111 | if (task_node(p) == p->numa_preferred_nid) |
6b9a7460 MG |
2112 | return; |
2113 | ||
2114 | /* Otherwise, try migrate to a CPU on the preferred node */ | |
2739d3ee | 2115 | task_numa_migrate(p); |
6b9a7460 MG |
2116 | } |
2117 | ||
20e07dea | 2118 | /* |
4142c3eb | 2119 | * Find out how many nodes on the workload is actively running on. Do this by |
20e07dea RR |
2120 | * tracking the nodes from which NUMA hinting faults are triggered. This can |
2121 | * be different from the set of nodes where the workload's memory is currently | |
2122 | * located. | |
20e07dea | 2123 | */ |
4142c3eb | 2124 | static void numa_group_count_active_nodes(struct numa_group *numa_group) |
20e07dea RR |
2125 | { |
2126 | unsigned long faults, max_faults = 0; | |
4142c3eb | 2127 | int nid, active_nodes = 0; |
20e07dea RR |
2128 | |
2129 | for_each_online_node(nid) { | |
2130 | faults = group_faults_cpu(numa_group, nid); | |
2131 | if (faults > max_faults) | |
2132 | max_faults = faults; | |
2133 | } | |
2134 | ||
2135 | for_each_online_node(nid) { | |
2136 | faults = group_faults_cpu(numa_group, nid); | |
4142c3eb RR |
2137 | if (faults * ACTIVE_NODE_FRACTION > max_faults) |
2138 | active_nodes++; | |
20e07dea | 2139 | } |
4142c3eb RR |
2140 | |
2141 | numa_group->max_faults_cpu = max_faults; | |
2142 | numa_group->active_nodes = active_nodes; | |
20e07dea RR |
2143 | } |
2144 | ||
04bb2f94 RR |
2145 | /* |
2146 | * When adapting the scan rate, the period is divided into NUMA_PERIOD_SLOTS | |
2147 | * increments. The more local the fault statistics are, the higher the scan | |
a22b4b01 RR |
2148 | * period will be for the next scan window. If local/(local+remote) ratio is |
2149 | * below NUMA_PERIOD_THRESHOLD (where range of ratio is 1..NUMA_PERIOD_SLOTS) | |
2150 | * the scan period will decrease. Aim for 70% local accesses. | |
04bb2f94 RR |
2151 | */ |
2152 | #define NUMA_PERIOD_SLOTS 10 | |
a22b4b01 | 2153 | #define NUMA_PERIOD_THRESHOLD 7 |
04bb2f94 RR |
2154 | |
2155 | /* | |
2156 | * Increase the scan period (slow down scanning) if the majority of | |
2157 | * our memory is already on our local node, or if the majority of | |
2158 | * the page accesses are shared with other processes. | |
2159 | * Otherwise, decrease the scan period. | |
2160 | */ | |
2161 | static void update_task_scan_period(struct task_struct *p, | |
2162 | unsigned long shared, unsigned long private) | |
2163 | { | |
2164 | unsigned int period_slot; | |
37ec97de | 2165 | int lr_ratio, ps_ratio; |
04bb2f94 RR |
2166 | int diff; |
2167 | ||
2168 | unsigned long remote = p->numa_faults_locality[0]; | |
2169 | unsigned long local = p->numa_faults_locality[1]; | |
2170 | ||
2171 | /* | |
2172 | * If there were no record hinting faults then either the task is | |
2173 | * completely idle or all activity is areas that are not of interest | |
074c2381 MG |
2174 | * to automatic numa balancing. Related to that, if there were failed |
2175 | * migration then it implies we are migrating too quickly or the local | |
2176 | * node is overloaded. In either case, scan slower | |
04bb2f94 | 2177 | */ |
074c2381 | 2178 | if (local + shared == 0 || p->numa_faults_locality[2]) { |
04bb2f94 RR |
2179 | p->numa_scan_period = min(p->numa_scan_period_max, |
2180 | p->numa_scan_period << 1); | |
2181 | ||
2182 | p->mm->numa_next_scan = jiffies + | |
2183 | msecs_to_jiffies(p->numa_scan_period); | |
2184 | ||
2185 | return; | |
2186 | } | |
2187 | ||
2188 | /* | |
2189 | * Prepare to scale scan period relative to the current period. | |
2190 | * == NUMA_PERIOD_THRESHOLD scan period stays the same | |
2191 | * < NUMA_PERIOD_THRESHOLD scan period decreases (scan faster) | |
2192 | * >= NUMA_PERIOD_THRESHOLD scan period increases (scan slower) | |
2193 | */ | |
2194 | period_slot = DIV_ROUND_UP(p->numa_scan_period, NUMA_PERIOD_SLOTS); | |
37ec97de RR |
2195 | lr_ratio = (local * NUMA_PERIOD_SLOTS) / (local + remote); |
2196 | ps_ratio = (private * NUMA_PERIOD_SLOTS) / (private + shared); | |
2197 | ||
2198 | if (ps_ratio >= NUMA_PERIOD_THRESHOLD) { | |
2199 | /* | |
2200 | * Most memory accesses are local. There is no need to | |
2201 | * do fast NUMA scanning, since memory is already local. | |
2202 | */ | |
2203 | int slot = ps_ratio - NUMA_PERIOD_THRESHOLD; | |
2204 | if (!slot) | |
2205 | slot = 1; | |
2206 | diff = slot * period_slot; | |
2207 | } else if (lr_ratio >= NUMA_PERIOD_THRESHOLD) { | |
2208 | /* | |
2209 | * Most memory accesses are shared with other tasks. | |
2210 | * There is no point in continuing fast NUMA scanning, | |
2211 | * since other tasks may just move the memory elsewhere. | |
2212 | */ | |
2213 | int slot = lr_ratio - NUMA_PERIOD_THRESHOLD; | |
04bb2f94 RR |
2214 | if (!slot) |
2215 | slot = 1; | |
2216 | diff = slot * period_slot; | |
2217 | } else { | |
04bb2f94 | 2218 | /* |
37ec97de RR |
2219 | * Private memory faults exceed (SLOTS-THRESHOLD)/SLOTS, |
2220 | * yet they are not on the local NUMA node. Speed up | |
2221 | * NUMA scanning to get the memory moved over. | |
04bb2f94 | 2222 | */ |
37ec97de RR |
2223 | int ratio = max(lr_ratio, ps_ratio); |
2224 | diff = -(NUMA_PERIOD_THRESHOLD - ratio) * period_slot; | |
04bb2f94 RR |
2225 | } |
2226 | ||
2227 | p->numa_scan_period = clamp(p->numa_scan_period + diff, | |
2228 | task_scan_min(p), task_scan_max(p)); | |
2229 | memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); | |
2230 | } | |
2231 | ||
7e2703e6 RR |
2232 | /* |
2233 | * Get the fraction of time the task has been running since the last | |
2234 | * NUMA placement cycle. The scheduler keeps similar statistics, but | |
2235 | * decays those on a 32ms period, which is orders of magnitude off | |
2236 | * from the dozens-of-seconds NUMA balancing period. Use the scheduler | |
2237 | * stats only if the task is so new there are no NUMA statistics yet. | |
2238 | */ | |
2239 | static u64 numa_get_avg_runtime(struct task_struct *p, u64 *period) | |
2240 | { | |
2241 | u64 runtime, delta, now; | |
2242 | /* Use the start of this time slice to avoid calculations. */ | |
2243 | now = p->se.exec_start; | |
2244 | runtime = p->se.sum_exec_runtime; | |
2245 | ||
2246 | if (p->last_task_numa_placement) { | |
2247 | delta = runtime - p->last_sum_exec_runtime; | |
2248 | *period = now - p->last_task_numa_placement; | |
a860fa7b XX |
2249 | |
2250 | /* Avoid time going backwards, prevent potential divide error: */ | |
2251 | if (unlikely((s64)*period < 0)) | |
2252 | *period = 0; | |
7e2703e6 | 2253 | } else { |
c7b50216 | 2254 | delta = p->se.avg.load_sum; |
9d89c257 | 2255 | *period = LOAD_AVG_MAX; |
7e2703e6 RR |
2256 | } |
2257 | ||
2258 | p->last_sum_exec_runtime = runtime; | |
2259 | p->last_task_numa_placement = now; | |
2260 | ||
2261 | return delta; | |
2262 | } | |
2263 | ||
54009416 RR |
2264 | /* |
2265 | * Determine the preferred nid for a task in a numa_group. This needs to | |
2266 | * be done in a way that produces consistent results with group_weight, | |
2267 | * otherwise workloads might not converge. | |
2268 | */ | |
2269 | static int preferred_group_nid(struct task_struct *p, int nid) | |
2270 | { | |
2271 | nodemask_t nodes; | |
2272 | int dist; | |
2273 | ||
2274 | /* Direct connections between all NUMA nodes. */ | |
2275 | if (sched_numa_topology_type == NUMA_DIRECT) | |
2276 | return nid; | |
2277 | ||
2278 | /* | |
2279 | * On a system with glueless mesh NUMA topology, group_weight | |
2280 | * scores nodes according to the number of NUMA hinting faults on | |
2281 | * both the node itself, and on nearby nodes. | |
2282 | */ | |
2283 | if (sched_numa_topology_type == NUMA_GLUELESS_MESH) { | |
2284 | unsigned long score, max_score = 0; | |
2285 | int node, max_node = nid; | |
2286 | ||
2287 | dist = sched_max_numa_distance; | |
2288 | ||
2289 | for_each_online_node(node) { | |
2290 | score = group_weight(p, node, dist); | |
2291 | if (score > max_score) { | |
2292 | max_score = score; | |
2293 | max_node = node; | |
2294 | } | |
2295 | } | |
2296 | return max_node; | |
2297 | } | |
2298 | ||
2299 | /* | |
2300 | * Finding the preferred nid in a system with NUMA backplane | |
2301 | * interconnect topology is more involved. The goal is to locate | |
2302 | * tasks from numa_groups near each other in the system, and | |
2303 | * untangle workloads from different sides of the system. This requires | |
2304 | * searching down the hierarchy of node groups, recursively searching | |
2305 | * inside the highest scoring group of nodes. The nodemask tricks | |
2306 | * keep the complexity of the search down. | |
2307 | */ | |
2308 | nodes = node_online_map; | |
2309 | for (dist = sched_max_numa_distance; dist > LOCAL_DISTANCE; dist--) { | |
2310 | unsigned long max_faults = 0; | |
81907478 | 2311 | nodemask_t max_group = NODE_MASK_NONE; |
54009416 RR |
2312 | int a, b; |
2313 | ||
2314 | /* Are there nodes at this distance from each other? */ | |
2315 | if (!find_numa_distance(dist)) | |
2316 | continue; | |
2317 | ||
2318 | for_each_node_mask(a, nodes) { | |
2319 | unsigned long faults = 0; | |
2320 | nodemask_t this_group; | |
2321 | nodes_clear(this_group); | |
2322 | ||
2323 | /* Sum group's NUMA faults; includes a==b case. */ | |
2324 | for_each_node_mask(b, nodes) { | |
2325 | if (node_distance(a, b) < dist) { | |
2326 | faults += group_faults(p, b); | |
2327 | node_set(b, this_group); | |
2328 | node_clear(b, nodes); | |
2329 | } | |
2330 | } | |
2331 | ||
2332 | /* Remember the top group. */ | |
2333 | if (faults > max_faults) { | |
2334 | max_faults = faults; | |
2335 | max_group = this_group; | |
2336 | /* | |
2337 | * subtle: at the smallest distance there is | |
2338 | * just one node left in each "group", the | |
2339 | * winner is the preferred nid. | |
2340 | */ | |
2341 | nid = a; | |
2342 | } | |
2343 | } | |
2344 | /* Next round, evaluate the nodes within max_group. */ | |
890a5409 JB |
2345 | if (!max_faults) |
2346 | break; | |
54009416 RR |
2347 | nodes = max_group; |
2348 | } | |
2349 | return nid; | |
2350 | } | |
2351 | ||
cbee9f88 PZ |
2352 | static void task_numa_placement(struct task_struct *p) |
2353 | { | |
98fa15f3 | 2354 | int seq, nid, max_nid = NUMA_NO_NODE; |
f03bb676 | 2355 | unsigned long max_faults = 0; |
04bb2f94 | 2356 | unsigned long fault_types[2] = { 0, 0 }; |
7e2703e6 RR |
2357 | unsigned long total_faults; |
2358 | u64 runtime, period; | |
7dbd13ed | 2359 | spinlock_t *group_lock = NULL; |
cb361d8c | 2360 | struct numa_group *ng; |
cbee9f88 | 2361 | |
7e5a2c17 JL |
2362 | /* |
2363 | * The p->mm->numa_scan_seq field gets updated without | |
2364 | * exclusive access. Use READ_ONCE() here to ensure | |
2365 | * that the field is read in a single access: | |
2366 | */ | |
316c1608 | 2367 | seq = READ_ONCE(p->mm->numa_scan_seq); |
cbee9f88 PZ |
2368 | if (p->numa_scan_seq == seq) |
2369 | return; | |
2370 | p->numa_scan_seq = seq; | |
598f0ec0 | 2371 | p->numa_scan_period_max = task_scan_max(p); |
cbee9f88 | 2372 | |
7e2703e6 RR |
2373 | total_faults = p->numa_faults_locality[0] + |
2374 | p->numa_faults_locality[1]; | |
2375 | runtime = numa_get_avg_runtime(p, &period); | |
2376 | ||
7dbd13ed | 2377 | /* If the task is part of a group prevent parallel updates to group stats */ |
cb361d8c JH |
2378 | ng = deref_curr_numa_group(p); |
2379 | if (ng) { | |
2380 | group_lock = &ng->lock; | |
60e69eed | 2381 | spin_lock_irq(group_lock); |
7dbd13ed MG |
2382 | } |
2383 | ||
688b7585 MG |
2384 | /* Find the node with the highest number of faults */ |
2385 | for_each_online_node(nid) { | |
44dba3d5 IM |
2386 | /* Keep track of the offsets in numa_faults array */ |
2387 | int mem_idx, membuf_idx, cpu_idx, cpubuf_idx; | |
83e1d2cd | 2388 | unsigned long faults = 0, group_faults = 0; |
44dba3d5 | 2389 | int priv; |
745d6147 | 2390 | |
be1e4e76 | 2391 | for (priv = 0; priv < NR_NUMA_HINT_FAULT_TYPES; priv++) { |
7e2703e6 | 2392 | long diff, f_diff, f_weight; |
8c8a743c | 2393 | |
44dba3d5 IM |
2394 | mem_idx = task_faults_idx(NUMA_MEM, nid, priv); |
2395 | membuf_idx = task_faults_idx(NUMA_MEMBUF, nid, priv); | |
2396 | cpu_idx = task_faults_idx(NUMA_CPU, nid, priv); | |
2397 | cpubuf_idx = task_faults_idx(NUMA_CPUBUF, nid, priv); | |
745d6147 | 2398 | |
ac8e895b | 2399 | /* Decay existing window, copy faults since last scan */ |
44dba3d5 IM |
2400 | diff = p->numa_faults[membuf_idx] - p->numa_faults[mem_idx] / 2; |
2401 | fault_types[priv] += p->numa_faults[membuf_idx]; | |
2402 | p->numa_faults[membuf_idx] = 0; | |
fb13c7ee | 2403 | |
7e2703e6 RR |
2404 | /* |
2405 | * Normalize the faults_from, so all tasks in a group | |
2406 | * count according to CPU use, instead of by the raw | |
2407 | * number of faults. Tasks with little runtime have | |
2408 | * little over-all impact on throughput, and thus their | |
2409 | * faults are less important. | |
2410 | */ | |
2411 | f_weight = div64_u64(runtime << 16, period + 1); | |
44dba3d5 | 2412 | f_weight = (f_weight * p->numa_faults[cpubuf_idx]) / |
7e2703e6 | 2413 | (total_faults + 1); |
44dba3d5 IM |
2414 | f_diff = f_weight - p->numa_faults[cpu_idx] / 2; |
2415 | p->numa_faults[cpubuf_idx] = 0; | |
50ec8a40 | 2416 | |
44dba3d5 IM |
2417 | p->numa_faults[mem_idx] += diff; |
2418 | p->numa_faults[cpu_idx] += f_diff; | |
2419 | faults += p->numa_faults[mem_idx]; | |
83e1d2cd | 2420 | p->total_numa_faults += diff; |
cb361d8c | 2421 | if (ng) { |
44dba3d5 IM |
2422 | /* |
2423 | * safe because we can only change our own group | |
2424 | * | |
2425 | * mem_idx represents the offset for a given | |
2426 | * nid and priv in a specific region because it | |
2427 | * is at the beginning of the numa_faults array. | |
2428 | */ | |
cb361d8c JH |
2429 | ng->faults[mem_idx] += diff; |
2430 | ng->faults_cpu[mem_idx] += f_diff; | |
2431 | ng->total_faults += diff; | |
2432 | group_faults += ng->faults[mem_idx]; | |
8c8a743c | 2433 | } |
ac8e895b MG |
2434 | } |
2435 | ||
cb361d8c | 2436 | if (!ng) { |
f03bb676 SD |
2437 | if (faults > max_faults) { |
2438 | max_faults = faults; | |
2439 | max_nid = nid; | |
2440 | } | |
2441 | } else if (group_faults > max_faults) { | |
2442 | max_faults = group_faults; | |
688b7585 MG |
2443 | max_nid = nid; |
2444 | } | |
83e1d2cd MG |
2445 | } |
2446 | ||
cb361d8c JH |
2447 | if (ng) { |
2448 | numa_group_count_active_nodes(ng); | |
60e69eed | 2449 | spin_unlock_irq(group_lock); |
f03bb676 | 2450 | max_nid = preferred_group_nid(p, max_nid); |
688b7585 MG |
2451 | } |
2452 | ||
bb97fc31 RR |
2453 | if (max_faults) { |
2454 | /* Set the new preferred node */ | |
2455 | if (max_nid != p->numa_preferred_nid) | |
2456 | sched_setnuma(p, max_nid); | |
3a7053b3 | 2457 | } |
30619c89 SD |
2458 | |
2459 | update_task_scan_period(p, fault_types[0], fault_types[1]); | |
cbee9f88 PZ |
2460 | } |
2461 | ||
8c8a743c PZ |
2462 | static inline int get_numa_group(struct numa_group *grp) |
2463 | { | |
c45a7795 | 2464 | return refcount_inc_not_zero(&grp->refcount); |
8c8a743c PZ |
2465 | } |
2466 | ||
2467 | static inline void put_numa_group(struct numa_group *grp) | |
2468 | { | |
c45a7795 | 2469 | if (refcount_dec_and_test(&grp->refcount)) |
8c8a743c PZ |
2470 | kfree_rcu(grp, rcu); |
2471 | } | |
2472 | ||
3e6a9418 MG |
2473 | static void task_numa_group(struct task_struct *p, int cpupid, int flags, |
2474 | int *priv) | |
8c8a743c PZ |
2475 | { |
2476 | struct numa_group *grp, *my_grp; | |
2477 | struct task_struct *tsk; | |
2478 | bool join = false; | |
2479 | int cpu = cpupid_to_cpu(cpupid); | |
2480 | int i; | |
2481 | ||
cb361d8c | 2482 | if (unlikely(!deref_curr_numa_group(p))) { |
8c8a743c | 2483 | unsigned int size = sizeof(struct numa_group) + |
50ec8a40 | 2484 | 4*nr_node_ids*sizeof(unsigned long); |
8c8a743c PZ |
2485 | |
2486 | grp = kzalloc(size, GFP_KERNEL | __GFP_NOWARN); | |
2487 | if (!grp) | |
2488 | return; | |
2489 | ||
c45a7795 | 2490 | refcount_set(&grp->refcount, 1); |
4142c3eb RR |
2491 | grp->active_nodes = 1; |
2492 | grp->max_faults_cpu = 0; | |
8c8a743c | 2493 | spin_lock_init(&grp->lock); |
e29cf08b | 2494 | grp->gid = p->pid; |
50ec8a40 | 2495 | /* Second half of the array tracks nids where faults happen */ |
be1e4e76 RR |
2496 | grp->faults_cpu = grp->faults + NR_NUMA_HINT_FAULT_TYPES * |
2497 | nr_node_ids; | |
8c8a743c | 2498 | |
be1e4e76 | 2499 | for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) |
44dba3d5 | 2500 | grp->faults[i] = p->numa_faults[i]; |
8c8a743c | 2501 | |
989348b5 | 2502 | grp->total_faults = p->total_numa_faults; |
83e1d2cd | 2503 | |
8c8a743c PZ |
2504 | grp->nr_tasks++; |
2505 | rcu_assign_pointer(p->numa_group, grp); | |
2506 | } | |
2507 | ||
2508 | rcu_read_lock(); | |
316c1608 | 2509 | tsk = READ_ONCE(cpu_rq(cpu)->curr); |
8c8a743c PZ |
2510 | |
2511 | if (!cpupid_match_pid(tsk, cpupid)) | |
3354781a | 2512 | goto no_join; |
8c8a743c PZ |
2513 | |
2514 | grp = rcu_dereference(tsk->numa_group); | |
2515 | if (!grp) | |
3354781a | 2516 | goto no_join; |
8c8a743c | 2517 | |
cb361d8c | 2518 | my_grp = deref_curr_numa_group(p); |
8c8a743c | 2519 | if (grp == my_grp) |
3354781a | 2520 | goto no_join; |
8c8a743c PZ |
2521 | |
2522 | /* | |
2523 | * Only join the other group if its bigger; if we're the bigger group, | |
2524 | * the other task will join us. | |
2525 | */ | |
2526 | if (my_grp->nr_tasks > grp->nr_tasks) | |
3354781a | 2527 | goto no_join; |
8c8a743c PZ |
2528 | |
2529 | /* | |
2530 | * Tie-break on the grp address. | |
2531 | */ | |
2532 | if (my_grp->nr_tasks == grp->nr_tasks && my_grp > grp) | |
3354781a | 2533 | goto no_join; |
8c8a743c | 2534 | |
dabe1d99 RR |
2535 | /* Always join threads in the same process. */ |
2536 | if (tsk->mm == current->mm) | |
2537 | join = true; | |
2538 | ||
2539 | /* Simple filter to avoid false positives due to PID collisions */ | |
2540 | if (flags & TNF_SHARED) | |
2541 | join = true; | |
8c8a743c | 2542 | |
3e6a9418 MG |
2543 | /* Update priv based on whether false sharing was detected */ |
2544 | *priv = !join; | |
2545 | ||
dabe1d99 | 2546 | if (join && !get_numa_group(grp)) |
3354781a | 2547 | goto no_join; |
8c8a743c | 2548 | |
8c8a743c PZ |
2549 | rcu_read_unlock(); |
2550 | ||
2551 | if (!join) | |
2552 | return; | |
2553 | ||
60e69eed MG |
2554 | BUG_ON(irqs_disabled()); |
2555 | double_lock_irq(&my_grp->lock, &grp->lock); | |
989348b5 | 2556 | |
be1e4e76 | 2557 | for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) { |
44dba3d5 IM |
2558 | my_grp->faults[i] -= p->numa_faults[i]; |
2559 | grp->faults[i] += p->numa_faults[i]; | |
8c8a743c | 2560 | } |
989348b5 MG |
2561 | my_grp->total_faults -= p->total_numa_faults; |
2562 | grp->total_faults += p->total_numa_faults; | |
8c8a743c | 2563 | |
8c8a743c PZ |
2564 | my_grp->nr_tasks--; |
2565 | grp->nr_tasks++; | |
2566 | ||
2567 | spin_unlock(&my_grp->lock); | |
60e69eed | 2568 | spin_unlock_irq(&grp->lock); |
8c8a743c PZ |
2569 | |
2570 | rcu_assign_pointer(p->numa_group, grp); | |
2571 | ||
2572 | put_numa_group(my_grp); | |
3354781a PZ |
2573 | return; |
2574 | ||
2575 | no_join: | |
2576 | rcu_read_unlock(); | |
2577 | return; | |
8c8a743c PZ |
2578 | } |
2579 | ||
16d51a59 JH |
2580 | /* |
2581 | * Get rid of NUMA staticstics associated with a task (either current or dead). | |
2582 | * If @final is set, the task is dead and has reached refcount zero, so we can | |
2583 | * safely free all relevant data structures. Otherwise, there might be | |
2584 | * concurrent reads from places like load balancing and procfs, and we should | |
2585 | * reset the data back to default state without freeing ->numa_faults. | |
2586 | */ | |
2587 | void task_numa_free(struct task_struct *p, bool final) | |
8c8a743c | 2588 | { |
cb361d8c JH |
2589 | /* safe: p either is current or is being freed by current */ |
2590 | struct numa_group *grp = rcu_dereference_raw(p->numa_group); | |
16d51a59 | 2591 | unsigned long *numa_faults = p->numa_faults; |
e9dd685c SR |
2592 | unsigned long flags; |
2593 | int i; | |
8c8a743c | 2594 | |
16d51a59 JH |
2595 | if (!numa_faults) |
2596 | return; | |
2597 | ||
8c8a743c | 2598 | if (grp) { |
e9dd685c | 2599 | spin_lock_irqsave(&grp->lock, flags); |
be1e4e76 | 2600 | for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) |
44dba3d5 | 2601 | grp->faults[i] -= p->numa_faults[i]; |
989348b5 | 2602 | grp->total_faults -= p->total_numa_faults; |
83e1d2cd | 2603 | |
8c8a743c | 2604 | grp->nr_tasks--; |
e9dd685c | 2605 | spin_unlock_irqrestore(&grp->lock, flags); |
35b123e2 | 2606 | RCU_INIT_POINTER(p->numa_group, NULL); |
8c8a743c PZ |
2607 | put_numa_group(grp); |
2608 | } | |
2609 | ||
16d51a59 JH |
2610 | if (final) { |
2611 | p->numa_faults = NULL; | |
2612 | kfree(numa_faults); | |
2613 | } else { | |
2614 | p->total_numa_faults = 0; | |
2615 | for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) | |
2616 | numa_faults[i] = 0; | |
2617 | } | |
8c8a743c PZ |
2618 | } |
2619 | ||
cbee9f88 PZ |
2620 | /* |
2621 | * Got a PROT_NONE fault for a page on @node. | |
2622 | */ | |
58b46da3 | 2623 | void task_numa_fault(int last_cpupid, int mem_node, int pages, int flags) |
cbee9f88 PZ |
2624 | { |
2625 | struct task_struct *p = current; | |
6688cc05 | 2626 | bool migrated = flags & TNF_MIGRATED; |
58b46da3 | 2627 | int cpu_node = task_node(current); |
792568ec | 2628 | int local = !!(flags & TNF_FAULT_LOCAL); |
4142c3eb | 2629 | struct numa_group *ng; |
ac8e895b | 2630 | int priv; |
cbee9f88 | 2631 | |
2a595721 | 2632 | if (!static_branch_likely(&sched_numa_balancing)) |
1a687c2e MG |
2633 | return; |
2634 | ||
9ff1d9ff MG |
2635 | /* for example, ksmd faulting in a user's mm */ |
2636 | if (!p->mm) | |
2637 | return; | |
2638 | ||
f809ca9a | 2639 | /* Allocate buffer to track faults on a per-node basis */ |
44dba3d5 IM |
2640 | if (unlikely(!p->numa_faults)) { |
2641 | int size = sizeof(*p->numa_faults) * | |
be1e4e76 | 2642 | NR_NUMA_HINT_FAULT_BUCKETS * nr_node_ids; |
f809ca9a | 2643 | |
44dba3d5 IM |
2644 | p->numa_faults = kzalloc(size, GFP_KERNEL|__GFP_NOWARN); |
2645 | if (!p->numa_faults) | |
f809ca9a | 2646 | return; |
745d6147 | 2647 | |
83e1d2cd | 2648 | p->total_numa_faults = 0; |
04bb2f94 | 2649 | memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); |
f809ca9a | 2650 | } |
cbee9f88 | 2651 | |
8c8a743c PZ |
2652 | /* |
2653 | * First accesses are treated as private, otherwise consider accesses | |
2654 | * to be private if the accessing pid has not changed | |
2655 | */ | |
2656 | if (unlikely(last_cpupid == (-1 & LAST_CPUPID_MASK))) { | |
2657 | priv = 1; | |
2658 | } else { | |
2659 | priv = cpupid_match_pid(p, last_cpupid); | |
6688cc05 | 2660 | if (!priv && !(flags & TNF_NO_GROUP)) |
3e6a9418 | 2661 | task_numa_group(p, last_cpupid, flags, &priv); |
8c8a743c PZ |
2662 | } |
2663 | ||
792568ec RR |
2664 | /* |
2665 | * If a workload spans multiple NUMA nodes, a shared fault that | |
2666 | * occurs wholly within the set of nodes that the workload is | |
2667 | * actively using should be counted as local. This allows the | |
2668 | * scan rate to slow down when a workload has settled down. | |
2669 | */ | |
cb361d8c | 2670 | ng = deref_curr_numa_group(p); |
4142c3eb RR |
2671 | if (!priv && !local && ng && ng->active_nodes > 1 && |
2672 | numa_is_active_node(cpu_node, ng) && | |
2673 | numa_is_active_node(mem_node, ng)) | |
792568ec RR |
2674 | local = 1; |
2675 | ||
2739d3ee | 2676 | /* |
e1ff516a YW |
2677 | * Retry to migrate task to preferred node periodically, in case it |
2678 | * previously failed, or the scheduler moved us. | |
2739d3ee | 2679 | */ |
b6a60cf3 SD |
2680 | if (time_after(jiffies, p->numa_migrate_retry)) { |
2681 | task_numa_placement(p); | |
6b9a7460 | 2682 | numa_migrate_preferred(p); |
b6a60cf3 | 2683 | } |
6b9a7460 | 2684 | |
b32e86b4 IM |
2685 | if (migrated) |
2686 | p->numa_pages_migrated += pages; | |
074c2381 MG |
2687 | if (flags & TNF_MIGRATE_FAIL) |
2688 | p->numa_faults_locality[2] += pages; | |
b32e86b4 | 2689 | |
44dba3d5 IM |
2690 | p->numa_faults[task_faults_idx(NUMA_MEMBUF, mem_node, priv)] += pages; |
2691 | p->numa_faults[task_faults_idx(NUMA_CPUBUF, cpu_node, priv)] += pages; | |
792568ec | 2692 | p->numa_faults_locality[local] += pages; |
cbee9f88 PZ |
2693 | } |
2694 | ||
6e5fb223 PZ |
2695 | static void reset_ptenuma_scan(struct task_struct *p) |
2696 | { | |
7e5a2c17 JL |
2697 | /* |
2698 | * We only did a read acquisition of the mmap sem, so | |
2699 | * p->mm->numa_scan_seq is written to without exclusive access | |
2700 | * and the update is not guaranteed to be atomic. That's not | |
2701 | * much of an issue though, since this is just used for | |
2702 | * statistical sampling. Use READ_ONCE/WRITE_ONCE, which are not | |
2703 | * expensive, to avoid any form of compiler optimizations: | |
2704 | */ | |
316c1608 | 2705 | WRITE_ONCE(p->mm->numa_scan_seq, READ_ONCE(p->mm->numa_scan_seq) + 1); |
6e5fb223 PZ |
2706 | p->mm->numa_scan_offset = 0; |
2707 | } | |
2708 | ||
cbee9f88 PZ |
2709 | /* |
2710 | * The expensive part of numa migration is done from task_work context. | |
2711 | * Triggered from task_tick_numa(). | |
2712 | */ | |
9434f9f5 | 2713 | static void task_numa_work(struct callback_head *work) |
cbee9f88 PZ |
2714 | { |
2715 | unsigned long migrate, next_scan, now = jiffies; | |
2716 | struct task_struct *p = current; | |
2717 | struct mm_struct *mm = p->mm; | |
51170840 | 2718 | u64 runtime = p->se.sum_exec_runtime; |
6e5fb223 | 2719 | struct vm_area_struct *vma; |
9f40604c | 2720 | unsigned long start, end; |
598f0ec0 | 2721 | unsigned long nr_pte_updates = 0; |
4620f8c1 | 2722 | long pages, virtpages; |
cbee9f88 | 2723 | |
9148a3a1 | 2724 | SCHED_WARN_ON(p != container_of(work, struct task_struct, numa_work)); |
cbee9f88 | 2725 | |
b34920d4 | 2726 | work->next = work; |
cbee9f88 PZ |
2727 | /* |
2728 | * Who cares about NUMA placement when they're dying. | |
2729 | * | |
2730 | * NOTE: make sure not to dereference p->mm before this check, | |
2731 | * exit_task_work() happens _after_ exit_mm() so we could be called | |
2732 | * without p->mm even though we still had it when we enqueued this | |
2733 | * work. | |
2734 | */ | |
2735 | if (p->flags & PF_EXITING) | |
2736 | return; | |
2737 | ||
930aa174 | 2738 | if (!mm->numa_next_scan) { |
7e8d16b6 MG |
2739 | mm->numa_next_scan = now + |
2740 | msecs_to_jiffies(sysctl_numa_balancing_scan_delay); | |
b8593bfd MG |
2741 | } |
2742 | ||
cbee9f88 PZ |
2743 | /* |
2744 | * Enforce maximal scan/migration frequency.. | |
2745 | */ | |
2746 | migrate = mm->numa_next_scan; | |
2747 | if (time_before(now, migrate)) | |
2748 | return; | |
2749 | ||
598f0ec0 MG |
2750 | if (p->numa_scan_period == 0) { |
2751 | p->numa_scan_period_max = task_scan_max(p); | |
b5dd77c8 | 2752 | p->numa_scan_period = task_scan_start(p); |
598f0ec0 | 2753 | } |
cbee9f88 | 2754 | |
fb003b80 | 2755 | next_scan = now + msecs_to_jiffies(p->numa_scan_period); |
cbee9f88 PZ |
2756 | if (cmpxchg(&mm->numa_next_scan, migrate, next_scan) != migrate) |
2757 | return; | |
2758 | ||
19a78d11 PZ |
2759 | /* |
2760 | * Delay this task enough that another task of this mm will likely win | |
2761 | * the next time around. | |
2762 | */ | |
2763 | p->node_stamp += 2 * TICK_NSEC; | |
2764 | ||
9f40604c MG |
2765 | start = mm->numa_scan_offset; |
2766 | pages = sysctl_numa_balancing_scan_size; | |
2767 | pages <<= 20 - PAGE_SHIFT; /* MB in pages */ | |
4620f8c1 | 2768 | virtpages = pages * 8; /* Scan up to this much virtual space */ |
9f40604c MG |
2769 | if (!pages) |
2770 | return; | |
cbee9f88 | 2771 | |
4620f8c1 | 2772 | |
d8ed45c5 | 2773 | if (!mmap_read_trylock(mm)) |
8655d549 | 2774 | return; |
9f40604c | 2775 | vma = find_vma(mm, start); |
6e5fb223 PZ |
2776 | if (!vma) { |
2777 | reset_ptenuma_scan(p); | |
9f40604c | 2778 | start = 0; |
6e5fb223 PZ |
2779 | vma = mm->mmap; |
2780 | } | |
9f40604c | 2781 | for (; vma; vma = vma->vm_next) { |
6b79c57b | 2782 | if (!vma_migratable(vma) || !vma_policy_mof(vma) || |
8e76d4ee | 2783 | is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_MIXEDMAP)) { |
6e5fb223 | 2784 | continue; |
6b79c57b | 2785 | } |
6e5fb223 | 2786 | |
4591ce4f MG |
2787 | /* |
2788 | * Shared library pages mapped by multiple processes are not | |
2789 | * migrated as it is expected they are cache replicated. Avoid | |
2790 | * hinting faults in read-only file-backed mappings or the vdso | |
2791 | * as migrating the pages will be of marginal benefit. | |
2792 | */ | |
2793 | if (!vma->vm_mm || | |
2794 | (vma->vm_file && (vma->vm_flags & (VM_READ|VM_WRITE)) == (VM_READ))) | |
2795 | continue; | |
2796 | ||
3c67f474 MG |
2797 | /* |
2798 | * Skip inaccessible VMAs to avoid any confusion between | |
2799 | * PROT_NONE and NUMA hinting ptes | |
2800 | */ | |
3122e80e | 2801 | if (!vma_is_accessible(vma)) |
3c67f474 | 2802 | continue; |
4591ce4f | 2803 | |
9f40604c MG |
2804 | do { |
2805 | start = max(start, vma->vm_start); | |
2806 | end = ALIGN(start + (pages << PAGE_SHIFT), HPAGE_SIZE); | |
2807 | end = min(end, vma->vm_end); | |
4620f8c1 | 2808 | nr_pte_updates = change_prot_numa(vma, start, end); |
598f0ec0 MG |
2809 | |
2810 | /* | |
4620f8c1 RR |
2811 | * Try to scan sysctl_numa_balancing_size worth of |
2812 | * hpages that have at least one present PTE that | |
2813 | * is not already pte-numa. If the VMA contains | |
2814 | * areas that are unused or already full of prot_numa | |
2815 | * PTEs, scan up to virtpages, to skip through those | |
2816 | * areas faster. | |
598f0ec0 MG |
2817 | */ |
2818 | if (nr_pte_updates) | |
2819 | pages -= (end - start) >> PAGE_SHIFT; | |
4620f8c1 | 2820 | virtpages -= (end - start) >> PAGE_SHIFT; |
6e5fb223 | 2821 | |
9f40604c | 2822 | start = end; |
4620f8c1 | 2823 | if (pages <= 0 || virtpages <= 0) |
9f40604c | 2824 | goto out; |
3cf1962c RR |
2825 | |
2826 | cond_resched(); | |
9f40604c | 2827 | } while (end != vma->vm_end); |
cbee9f88 | 2828 | } |
6e5fb223 | 2829 | |
9f40604c | 2830 | out: |
6e5fb223 | 2831 | /* |
c69307d5 PZ |
2832 | * It is possible to reach the end of the VMA list but the last few |
2833 | * VMAs are not guaranteed to the vma_migratable. If they are not, we | |
2834 | * would find the !migratable VMA on the next scan but not reset the | |
2835 | * scanner to the start so check it now. | |
6e5fb223 PZ |
2836 | */ |
2837 | if (vma) | |
9f40604c | 2838 | mm->numa_scan_offset = start; |
6e5fb223 PZ |
2839 | else |
2840 | reset_ptenuma_scan(p); | |
d8ed45c5 | 2841 | mmap_read_unlock(mm); |
51170840 RR |
2842 | |
2843 | /* | |
2844 | * Make sure tasks use at least 32x as much time to run other code | |
2845 | * than they used here, to limit NUMA PTE scanning overhead to 3% max. | |
2846 | * Usually update_task_scan_period slows down scanning enough; on an | |
2847 | * overloaded system we need to limit overhead on a per task basis. | |
2848 | */ | |
2849 | if (unlikely(p->se.sum_exec_runtime != runtime)) { | |
2850 | u64 diff = p->se.sum_exec_runtime - runtime; | |
2851 | p->node_stamp += 32 * diff; | |
2852 | } | |
cbee9f88 PZ |
2853 | } |
2854 | ||
d35927a1 VS |
2855 | void init_numa_balancing(unsigned long clone_flags, struct task_struct *p) |
2856 | { | |
2857 | int mm_users = 0; | |
2858 | struct mm_struct *mm = p->mm; | |
2859 | ||
2860 | if (mm) { | |
2861 | mm_users = atomic_read(&mm->mm_users); | |
2862 | if (mm_users == 1) { | |
2863 | mm->numa_next_scan = jiffies + msecs_to_jiffies(sysctl_numa_balancing_scan_delay); | |
2864 | mm->numa_scan_seq = 0; | |
2865 | } | |
2866 | } | |
2867 | p->node_stamp = 0; | |
2868 | p->numa_scan_seq = mm ? mm->numa_scan_seq : 0; | |
2869 | p->numa_scan_period = sysctl_numa_balancing_scan_delay; | |
b34920d4 | 2870 | /* Protect against double add, see task_tick_numa and task_numa_work */ |
d35927a1 VS |
2871 | p->numa_work.next = &p->numa_work; |
2872 | p->numa_faults = NULL; | |
2873 | RCU_INIT_POINTER(p->numa_group, NULL); | |
2874 | p->last_task_numa_placement = 0; | |
2875 | p->last_sum_exec_runtime = 0; | |
2876 | ||
b34920d4 VS |
2877 | init_task_work(&p->numa_work, task_numa_work); |
2878 | ||
d35927a1 VS |
2879 | /* New address space, reset the preferred nid */ |
2880 | if (!(clone_flags & CLONE_VM)) { | |
2881 | p->numa_preferred_nid = NUMA_NO_NODE; | |
2882 | return; | |
2883 | } | |
2884 | ||
2885 | /* | |
2886 | * New thread, keep existing numa_preferred_nid which should be copied | |
2887 | * already by arch_dup_task_struct but stagger when scans start. | |
2888 | */ | |
2889 | if (mm) { | |
2890 | unsigned int delay; | |
2891 | ||
2892 | delay = min_t(unsigned int, task_scan_max(current), | |
2893 | current->numa_scan_period * mm_users * NSEC_PER_MSEC); | |
2894 | delay += 2 * TICK_NSEC; | |
2895 | p->node_stamp = delay; | |
2896 | } | |
2897 | } | |
2898 | ||
cbee9f88 PZ |
2899 | /* |
2900 | * Drive the periodic memory faults.. | |
2901 | */ | |
b1546edc | 2902 | static void task_tick_numa(struct rq *rq, struct task_struct *curr) |
cbee9f88 PZ |
2903 | { |
2904 | struct callback_head *work = &curr->numa_work; | |
2905 | u64 period, now; | |
2906 | ||
2907 | /* | |
2908 | * We don't care about NUMA placement if we don't have memory. | |
2909 | */ | |
18f855e5 | 2910 | if ((curr->flags & (PF_EXITING | PF_KTHREAD)) || work->next != work) |
cbee9f88 PZ |
2911 | return; |
2912 | ||
2913 | /* | |
2914 | * Using runtime rather than walltime has the dual advantage that | |
2915 | * we (mostly) drive the selection from busy threads and that the | |
2916 | * task needs to have done some actual work before we bother with | |
2917 | * NUMA placement. | |
2918 | */ | |
2919 | now = curr->se.sum_exec_runtime; | |
2920 | period = (u64)curr->numa_scan_period * NSEC_PER_MSEC; | |
2921 | ||
25b3e5a3 | 2922 | if (now > curr->node_stamp + period) { |
4b96a29b | 2923 | if (!curr->node_stamp) |
b5dd77c8 | 2924 | curr->numa_scan_period = task_scan_start(curr); |
19a78d11 | 2925 | curr->node_stamp += period; |
cbee9f88 | 2926 | |
b34920d4 | 2927 | if (!time_before(jiffies, curr->mm->numa_next_scan)) |
cbee9f88 | 2928 | task_work_add(curr, work, true); |
cbee9f88 PZ |
2929 | } |
2930 | } | |
3fed382b | 2931 | |
3f9672ba SD |
2932 | static void update_scan_period(struct task_struct *p, int new_cpu) |
2933 | { | |
2934 | int src_nid = cpu_to_node(task_cpu(p)); | |
2935 | int dst_nid = cpu_to_node(new_cpu); | |
2936 | ||
05cbdf4f MG |
2937 | if (!static_branch_likely(&sched_numa_balancing)) |
2938 | return; | |
2939 | ||
3f9672ba SD |
2940 | if (!p->mm || !p->numa_faults || (p->flags & PF_EXITING)) |
2941 | return; | |
2942 | ||
05cbdf4f MG |
2943 | if (src_nid == dst_nid) |
2944 | return; | |
2945 | ||
2946 | /* | |
2947 | * Allow resets if faults have been trapped before one scan | |
2948 | * has completed. This is most likely due to a new task that | |
2949 | * is pulled cross-node due to wakeups or load balancing. | |
2950 | */ | |
2951 | if (p->numa_scan_seq) { | |
2952 | /* | |
2953 | * Avoid scan adjustments if moving to the preferred | |
2954 | * node or if the task was not previously running on | |
2955 | * the preferred node. | |
2956 | */ | |
2957 | if (dst_nid == p->numa_preferred_nid || | |
98fa15f3 AK |
2958 | (p->numa_preferred_nid != NUMA_NO_NODE && |
2959 | src_nid != p->numa_preferred_nid)) | |
05cbdf4f MG |
2960 | return; |
2961 | } | |
2962 | ||
2963 | p->numa_scan_period = task_scan_start(p); | |
3f9672ba SD |
2964 | } |
2965 | ||
cbee9f88 PZ |
2966 | #else |
2967 | static void task_tick_numa(struct rq *rq, struct task_struct *curr) | |
2968 | { | |
2969 | } | |
0ec8aa00 PZ |
2970 | |
2971 | static inline void account_numa_enqueue(struct rq *rq, struct task_struct *p) | |
2972 | { | |
2973 | } | |
2974 | ||
2975 | static inline void account_numa_dequeue(struct rq *rq, struct task_struct *p) | |
2976 | { | |
2977 | } | |
3fed382b | 2978 | |
3f9672ba SD |
2979 | static inline void update_scan_period(struct task_struct *p, int new_cpu) |
2980 | { | |
2981 | } | |
2982 | ||
cbee9f88 PZ |
2983 | #endif /* CONFIG_NUMA_BALANCING */ |
2984 | ||
30cfdcfc DA |
2985 | static void |
2986 | account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
2987 | { | |
2988 | update_load_add(&cfs_rq->load, se->load.weight); | |
367456c7 | 2989 | #ifdef CONFIG_SMP |
0ec8aa00 PZ |
2990 | if (entity_is_task(se)) { |
2991 | struct rq *rq = rq_of(cfs_rq); | |
2992 | ||
2993 | account_numa_enqueue(rq, task_of(se)); | |
2994 | list_add(&se->group_node, &rq->cfs_tasks); | |
2995 | } | |
367456c7 | 2996 | #endif |
30cfdcfc | 2997 | cfs_rq->nr_running++; |
30cfdcfc DA |
2998 | } |
2999 | ||
3000 | static void | |
3001 | account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
3002 | { | |
3003 | update_load_sub(&cfs_rq->load, se->load.weight); | |
bfdb198c | 3004 | #ifdef CONFIG_SMP |
0ec8aa00 PZ |
3005 | if (entity_is_task(se)) { |
3006 | account_numa_dequeue(rq_of(cfs_rq), task_of(se)); | |
b87f1724 | 3007 | list_del_init(&se->group_node); |
0ec8aa00 | 3008 | } |
bfdb198c | 3009 | #endif |
30cfdcfc | 3010 | cfs_rq->nr_running--; |
30cfdcfc DA |
3011 | } |
3012 | ||
8d5b9025 PZ |
3013 | /* |
3014 | * Signed add and clamp on underflow. | |
3015 | * | |
3016 | * Explicitly do a load-store to ensure the intermediate value never hits | |
3017 | * memory. This allows lockless observations without ever seeing the negative | |
3018 | * values. | |
3019 | */ | |
3020 | #define add_positive(_ptr, _val) do { \ | |
3021 | typeof(_ptr) ptr = (_ptr); \ | |
3022 | typeof(_val) val = (_val); \ | |
3023 | typeof(*ptr) res, var = READ_ONCE(*ptr); \ | |
3024 | \ | |
3025 | res = var + val; \ | |
3026 | \ | |
3027 | if (val < 0 && res > var) \ | |
3028 | res = 0; \ | |
3029 | \ | |
3030 | WRITE_ONCE(*ptr, res); \ | |
3031 | } while (0) | |
3032 | ||
3033 | /* | |
3034 | * Unsigned subtract and clamp on underflow. | |
3035 | * | |
3036 | * Explicitly do a load-store to ensure the intermediate value never hits | |
3037 | * memory. This allows lockless observations without ever seeing the negative | |
3038 | * values. | |
3039 | */ | |
3040 | #define sub_positive(_ptr, _val) do { \ | |
3041 | typeof(_ptr) ptr = (_ptr); \ | |
3042 | typeof(*ptr) val = (_val); \ | |
3043 | typeof(*ptr) res, var = READ_ONCE(*ptr); \ | |
3044 | res = var - val; \ | |
3045 | if (res > var) \ | |
3046 | res = 0; \ | |
3047 | WRITE_ONCE(*ptr, res); \ | |
3048 | } while (0) | |
3049 | ||
b5c0ce7b PB |
3050 | /* |
3051 | * Remove and clamp on negative, from a local variable. | |
3052 | * | |
3053 | * A variant of sub_positive(), which does not use explicit load-store | |
3054 | * and is thus optimized for local variable updates. | |
3055 | */ | |
3056 | #define lsub_positive(_ptr, _val) do { \ | |
3057 | typeof(_ptr) ptr = (_ptr); \ | |
3058 | *ptr -= min_t(typeof(*ptr), *ptr, _val); \ | |
3059 | } while (0) | |
3060 | ||
8d5b9025 | 3061 | #ifdef CONFIG_SMP |
8d5b9025 PZ |
3062 | static inline void |
3063 | enqueue_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
3064 | { | |
3065 | cfs_rq->avg.load_avg += se->avg.load_avg; | |
3066 | cfs_rq->avg.load_sum += se_weight(se) * se->avg.load_sum; | |
3067 | } | |
3068 | ||
3069 | static inline void | |
3070 | dequeue_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
3071 | { | |
3072 | sub_positive(&cfs_rq->avg.load_avg, se->avg.load_avg); | |
3073 | sub_positive(&cfs_rq->avg.load_sum, se_weight(se) * se->avg.load_sum); | |
3074 | } | |
3075 | #else | |
3076 | static inline void | |
8d5b9025 PZ |
3077 | enqueue_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) { } |
3078 | static inline void | |
3079 | dequeue_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) { } | |
3080 | #endif | |
3081 | ||
9059393e | 3082 | static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, |
0dacee1b | 3083 | unsigned long weight) |
9059393e VG |
3084 | { |
3085 | if (se->on_rq) { | |
3086 | /* commit outstanding execution time */ | |
3087 | if (cfs_rq->curr == se) | |
3088 | update_curr(cfs_rq); | |
3089 | account_entity_dequeue(cfs_rq, se); | |
9059393e VG |
3090 | } |
3091 | dequeue_load_avg(cfs_rq, se); | |
3092 | ||
3093 | update_load_set(&se->load, weight); | |
3094 | ||
3095 | #ifdef CONFIG_SMP | |
1ea6c46a | 3096 | do { |
87e867b4 | 3097 | u32 divider = get_pelt_divider(&se->avg); |
1ea6c46a PZ |
3098 | |
3099 | se->avg.load_avg = div_u64(se_weight(se) * se->avg.load_sum, divider); | |
1ea6c46a | 3100 | } while (0); |
9059393e VG |
3101 | #endif |
3102 | ||
3103 | enqueue_load_avg(cfs_rq, se); | |
0dacee1b | 3104 | if (se->on_rq) |
9059393e | 3105 | account_entity_enqueue(cfs_rq, se); |
0dacee1b | 3106 | |
9059393e VG |
3107 | } |
3108 | ||
3109 | void reweight_task(struct task_struct *p, int prio) | |
3110 | { | |
3111 | struct sched_entity *se = &p->se; | |
3112 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
3113 | struct load_weight *load = &se->load; | |
3114 | unsigned long weight = scale_load(sched_prio_to_weight[prio]); | |
3115 | ||
0dacee1b | 3116 | reweight_entity(cfs_rq, se, weight); |
9059393e VG |
3117 | load->inv_weight = sched_prio_to_wmult[prio]; |
3118 | } | |
3119 | ||
3ff6dcac | 3120 | #ifdef CONFIG_FAIR_GROUP_SCHED |
387f77cc | 3121 | #ifdef CONFIG_SMP |
cef27403 PZ |
3122 | /* |
3123 | * All this does is approximate the hierarchical proportion which includes that | |
3124 | * global sum we all love to hate. | |
3125 | * | |
3126 | * That is, the weight of a group entity, is the proportional share of the | |
3127 | * group weight based on the group runqueue weights. That is: | |
3128 | * | |
3129 | * tg->weight * grq->load.weight | |
3130 | * ge->load.weight = ----------------------------- (1) | |
3131 | * \Sum grq->load.weight | |
3132 | * | |
3133 | * Now, because computing that sum is prohibitively expensive to compute (been | |
3134 | * there, done that) we approximate it with this average stuff. The average | |
3135 | * moves slower and therefore the approximation is cheaper and more stable. | |
3136 | * | |
3137 | * So instead of the above, we substitute: | |
3138 | * | |
3139 | * grq->load.weight -> grq->avg.load_avg (2) | |
3140 | * | |
3141 | * which yields the following: | |
3142 | * | |
3143 | * tg->weight * grq->avg.load_avg | |
3144 | * ge->load.weight = ------------------------------ (3) | |
3145 | * tg->load_avg | |
3146 | * | |
3147 | * Where: tg->load_avg ~= \Sum grq->avg.load_avg | |
3148 | * | |
3149 | * That is shares_avg, and it is right (given the approximation (2)). | |
3150 | * | |
3151 | * The problem with it is that because the average is slow -- it was designed | |
3152 | * to be exactly that of course -- this leads to transients in boundary | |
3153 | * conditions. In specific, the case where the group was idle and we start the | |
3154 | * one task. It takes time for our CPU's grq->avg.load_avg to build up, | |
3155 | * yielding bad latency etc.. | |
3156 | * | |
3157 | * Now, in that special case (1) reduces to: | |
3158 | * | |
3159 | * tg->weight * grq->load.weight | |
17de4ee0 | 3160 | * ge->load.weight = ----------------------------- = tg->weight (4) |
cef27403 PZ |
3161 | * grp->load.weight |
3162 | * | |
3163 | * That is, the sum collapses because all other CPUs are idle; the UP scenario. | |
3164 | * | |
3165 | * So what we do is modify our approximation (3) to approach (4) in the (near) | |
3166 | * UP case, like: | |
3167 | * | |
3168 | * ge->load.weight = | |
3169 | * | |
3170 | * tg->weight * grq->load.weight | |
3171 | * --------------------------------------------------- (5) | |
3172 | * tg->load_avg - grq->avg.load_avg + grq->load.weight | |
3173 | * | |
17de4ee0 PZ |
3174 | * But because grq->load.weight can drop to 0, resulting in a divide by zero, |
3175 | * we need to use grq->avg.load_avg as its lower bound, which then gives: | |
3176 | * | |
3177 | * | |
3178 | * tg->weight * grq->load.weight | |
3179 | * ge->load.weight = ----------------------------- (6) | |
3180 | * tg_load_avg' | |
3181 | * | |
3182 | * Where: | |
3183 | * | |
3184 | * tg_load_avg' = tg->load_avg - grq->avg.load_avg + | |
3185 | * max(grq->load.weight, grq->avg.load_avg) | |
cef27403 PZ |
3186 | * |
3187 | * And that is shares_weight and is icky. In the (near) UP case it approaches | |
3188 | * (4) while in the normal case it approaches (3). It consistently | |
3189 | * overestimates the ge->load.weight and therefore: | |
3190 | * | |
3191 | * \Sum ge->load.weight >= tg->weight | |
3192 | * | |
3193 | * hence icky! | |
3194 | */ | |
2c8e4dce | 3195 | static long calc_group_shares(struct cfs_rq *cfs_rq) |
cf5f0acf | 3196 | { |
7c80cfc9 PZ |
3197 | long tg_weight, tg_shares, load, shares; |
3198 | struct task_group *tg = cfs_rq->tg; | |
3199 | ||
3200 | tg_shares = READ_ONCE(tg->shares); | |
cf5f0acf | 3201 | |
3d4b60d3 | 3202 | load = max(scale_load_down(cfs_rq->load.weight), cfs_rq->avg.load_avg); |
cf5f0acf | 3203 | |
ea1dc6fc | 3204 | tg_weight = atomic_long_read(&tg->load_avg); |
3ff6dcac | 3205 | |
ea1dc6fc PZ |
3206 | /* Ensure tg_weight >= load */ |
3207 | tg_weight -= cfs_rq->tg_load_avg_contrib; | |
3208 | tg_weight += load; | |
3ff6dcac | 3209 | |
7c80cfc9 | 3210 | shares = (tg_shares * load); |
cf5f0acf PZ |
3211 | if (tg_weight) |
3212 | shares /= tg_weight; | |
3ff6dcac | 3213 | |
b8fd8423 DE |
3214 | /* |
3215 | * MIN_SHARES has to be unscaled here to support per-CPU partitioning | |
3216 | * of a group with small tg->shares value. It is a floor value which is | |
3217 | * assigned as a minimum load.weight to the sched_entity representing | |
3218 | * the group on a CPU. | |
3219 | * | |
3220 | * E.g. on 64-bit for a group with tg->shares of scale_load(15)=15*1024 | |
3221 | * on an 8-core system with 8 tasks each runnable on one CPU shares has | |
3222 | * to be 15*1024*1/8=1920 instead of scale_load(MIN_SHARES)=2*1024. In | |
3223 | * case no task is runnable on a CPU MIN_SHARES=2 should be returned | |
3224 | * instead of 0. | |
3225 | */ | |
7c80cfc9 | 3226 | return clamp_t(long, shares, MIN_SHARES, tg_shares); |
3ff6dcac | 3227 | } |
387f77cc | 3228 | #endif /* CONFIG_SMP */ |
ea1dc6fc | 3229 | |
82958366 PT |
3230 | static inline int throttled_hierarchy(struct cfs_rq *cfs_rq); |
3231 | ||
1ea6c46a PZ |
3232 | /* |
3233 | * Recomputes the group entity based on the current state of its group | |
3234 | * runqueue. | |
3235 | */ | |
3236 | static void update_cfs_group(struct sched_entity *se) | |
2069dd75 | 3237 | { |
1ea6c46a | 3238 | struct cfs_rq *gcfs_rq = group_cfs_rq(se); |
0dacee1b | 3239 | long shares; |
2069dd75 | 3240 | |
1ea6c46a | 3241 | if (!gcfs_rq) |
89ee048f VG |
3242 | return; |
3243 | ||
1ea6c46a | 3244 | if (throttled_hierarchy(gcfs_rq)) |
2069dd75 | 3245 | return; |
89ee048f | 3246 | |
3ff6dcac | 3247 | #ifndef CONFIG_SMP |
0dacee1b | 3248 | shares = READ_ONCE(gcfs_rq->tg->shares); |
7c80cfc9 PZ |
3249 | |
3250 | if (likely(se->load.weight == shares)) | |
3ff6dcac | 3251 | return; |
7c80cfc9 | 3252 | #else |
2c8e4dce | 3253 | shares = calc_group_shares(gcfs_rq); |
3ff6dcac | 3254 | #endif |
2069dd75 | 3255 | |
0dacee1b | 3256 | reweight_entity(cfs_rq_of(se), se, shares); |
2069dd75 | 3257 | } |
89ee048f | 3258 | |
2069dd75 | 3259 | #else /* CONFIG_FAIR_GROUP_SCHED */ |
1ea6c46a | 3260 | static inline void update_cfs_group(struct sched_entity *se) |
2069dd75 PZ |
3261 | { |
3262 | } | |
3263 | #endif /* CONFIG_FAIR_GROUP_SCHED */ | |
3264 | ||
ea14b57e | 3265 | static inline void cfs_rq_util_change(struct cfs_rq *cfs_rq, int flags) |
a030d738 | 3266 | { |
43964409 LT |
3267 | struct rq *rq = rq_of(cfs_rq); |
3268 | ||
a4f9a0e5 | 3269 | if (&rq->cfs == cfs_rq) { |
a030d738 VK |
3270 | /* |
3271 | * There are a few boundary cases this might miss but it should | |
3272 | * get called often enough that that should (hopefully) not be | |
9783be2c | 3273 | * a real problem. |
a030d738 VK |
3274 | * |
3275 | * It will not get called when we go idle, because the idle | |
3276 | * thread is a different class (!fair), nor will the utilization | |
3277 | * number include things like RT tasks. | |
3278 | * | |
3279 | * As is, the util number is not freq-invariant (we'd have to | |
3280 | * implement arch_scale_freq_capacity() for that). | |
3281 | * | |
3282 | * See cpu_util(). | |
3283 | */ | |
ea14b57e | 3284 | cpufreq_update_util(rq, flags); |
a030d738 VK |
3285 | } |
3286 | } | |
3287 | ||
141965c7 | 3288 | #ifdef CONFIG_SMP |
c566e8e9 | 3289 | #ifdef CONFIG_FAIR_GROUP_SCHED |
7c3edd2c PZ |
3290 | /** |
3291 | * update_tg_load_avg - update the tg's load avg | |
3292 | * @cfs_rq: the cfs_rq whose avg changed | |
3293 | * @force: update regardless of how small the difference | |
3294 | * | |
3295 | * This function 'ensures': tg->load_avg := \Sum tg->cfs_rq[]->avg.load. | |
3296 | * However, because tg->load_avg is a global value there are performance | |
3297 | * considerations. | |
3298 | * | |
3299 | * In order to avoid having to look at the other cfs_rq's, we use a | |
3300 | * differential update where we store the last value we propagated. This in | |
3301 | * turn allows skipping updates if the differential is 'small'. | |
3302 | * | |
815abf5a | 3303 | * Updating tg's load_avg is necessary before update_cfs_share(). |
bb17f655 | 3304 | */ |
9d89c257 | 3305 | static inline void update_tg_load_avg(struct cfs_rq *cfs_rq, int force) |
bb17f655 | 3306 | { |
9d89c257 | 3307 | long delta = cfs_rq->avg.load_avg - cfs_rq->tg_load_avg_contrib; |
bb17f655 | 3308 | |
aa0b7ae0 WL |
3309 | /* |
3310 | * No need to update load_avg for root_task_group as it is not used. | |
3311 | */ | |
3312 | if (cfs_rq->tg == &root_task_group) | |
3313 | return; | |
3314 | ||
9d89c257 YD |
3315 | if (force || abs(delta) > cfs_rq->tg_load_avg_contrib / 64) { |
3316 | atomic_long_add(delta, &cfs_rq->tg->load_avg); | |
3317 | cfs_rq->tg_load_avg_contrib = cfs_rq->avg.load_avg; | |
bb17f655 | 3318 | } |
8165e145 | 3319 | } |
f5f9739d | 3320 | |
ad936d86 | 3321 | /* |
97fb7a0a | 3322 | * Called within set_task_rq() right before setting a task's CPU. The |
ad936d86 BP |
3323 | * caller only guarantees p->pi_lock is held; no other assumptions, |
3324 | * including the state of rq->lock, should be made. | |
3325 | */ | |
3326 | void set_task_rq_fair(struct sched_entity *se, | |
3327 | struct cfs_rq *prev, struct cfs_rq *next) | |
3328 | { | |
0ccb977f PZ |
3329 | u64 p_last_update_time; |
3330 | u64 n_last_update_time; | |
3331 | ||
ad936d86 BP |
3332 | if (!sched_feat(ATTACH_AGE_LOAD)) |
3333 | return; | |
3334 | ||
3335 | /* | |
3336 | * We are supposed to update the task to "current" time, then its up to | |
3337 | * date and ready to go to new CPU/cfs_rq. But we have difficulty in | |
3338 | * getting what current time is, so simply throw away the out-of-date | |
3339 | * time. This will result in the wakee task is less decayed, but giving | |
3340 | * the wakee more load sounds not bad. | |
3341 | */ | |
0ccb977f PZ |
3342 | if (!(se->avg.last_update_time && prev)) |
3343 | return; | |
ad936d86 BP |
3344 | |
3345 | #ifndef CONFIG_64BIT | |
0ccb977f | 3346 | { |
ad936d86 BP |
3347 | u64 p_last_update_time_copy; |
3348 | u64 n_last_update_time_copy; | |
3349 | ||
3350 | do { | |
3351 | p_last_update_time_copy = prev->load_last_update_time_copy; | |
3352 | n_last_update_time_copy = next->load_last_update_time_copy; | |
3353 | ||
3354 | smp_rmb(); | |
3355 | ||
3356 | p_last_update_time = prev->avg.last_update_time; | |
3357 | n_last_update_time = next->avg.last_update_time; | |
3358 | ||
3359 | } while (p_last_update_time != p_last_update_time_copy || | |
3360 | n_last_update_time != n_last_update_time_copy); | |
0ccb977f | 3361 | } |
ad936d86 | 3362 | #else |
0ccb977f PZ |
3363 | p_last_update_time = prev->avg.last_update_time; |
3364 | n_last_update_time = next->avg.last_update_time; | |
ad936d86 | 3365 | #endif |
23127296 | 3366 | __update_load_avg_blocked_se(p_last_update_time, se); |
0ccb977f | 3367 | se->avg.last_update_time = n_last_update_time; |
ad936d86 | 3368 | } |
09a43ace | 3369 | |
0e2d2aaa PZ |
3370 | |
3371 | /* | |
3372 | * When on migration a sched_entity joins/leaves the PELT hierarchy, we need to | |
3373 | * propagate its contribution. The key to this propagation is the invariant | |
3374 | * that for each group: | |
3375 | * | |
3376 | * ge->avg == grq->avg (1) | |
3377 | * | |
3378 | * _IFF_ we look at the pure running and runnable sums. Because they | |
3379 | * represent the very same entity, just at different points in the hierarchy. | |
3380 | * | |
9f683953 VG |
3381 | * Per the above update_tg_cfs_util() and update_tg_cfs_runnable() are trivial |
3382 | * and simply copies the running/runnable sum over (but still wrong, because | |
3383 | * the group entity and group rq do not have their PELT windows aligned). | |
0e2d2aaa | 3384 | * |
0dacee1b | 3385 | * However, update_tg_cfs_load() is more complex. So we have: |
0e2d2aaa PZ |
3386 | * |
3387 | * ge->avg.load_avg = ge->load.weight * ge->avg.runnable_avg (2) | |
3388 | * | |
3389 | * And since, like util, the runnable part should be directly transferable, | |
3390 | * the following would _appear_ to be the straight forward approach: | |
3391 | * | |
a4c3c049 | 3392 | * grq->avg.load_avg = grq->load.weight * grq->avg.runnable_avg (3) |
0e2d2aaa PZ |
3393 | * |
3394 | * And per (1) we have: | |
3395 | * | |
a4c3c049 | 3396 | * ge->avg.runnable_avg == grq->avg.runnable_avg |
0e2d2aaa PZ |
3397 | * |
3398 | * Which gives: | |
3399 | * | |
3400 | * ge->load.weight * grq->avg.load_avg | |
3401 | * ge->avg.load_avg = ----------------------------------- (4) | |
3402 | * grq->load.weight | |
3403 | * | |
3404 | * Except that is wrong! | |
3405 | * | |
3406 | * Because while for entities historical weight is not important and we | |
3407 | * really only care about our future and therefore can consider a pure | |
3408 | * runnable sum, runqueues can NOT do this. | |
3409 | * | |
3410 | * We specifically want runqueues to have a load_avg that includes | |
3411 | * historical weights. Those represent the blocked load, the load we expect | |
3412 | * to (shortly) return to us. This only works by keeping the weights as | |
3413 | * integral part of the sum. We therefore cannot decompose as per (3). | |
3414 | * | |
a4c3c049 VG |
3415 | * Another reason this doesn't work is that runnable isn't a 0-sum entity. |
3416 | * Imagine a rq with 2 tasks that each are runnable 2/3 of the time. Then the | |
3417 | * rq itself is runnable anywhere between 2/3 and 1 depending on how the | |
3418 | * runnable section of these tasks overlap (or not). If they were to perfectly | |
3419 | * align the rq as a whole would be runnable 2/3 of the time. If however we | |
3420 | * always have at least 1 runnable task, the rq as a whole is always runnable. | |
0e2d2aaa | 3421 | * |
a4c3c049 | 3422 | * So we'll have to approximate.. :/ |
0e2d2aaa | 3423 | * |
a4c3c049 | 3424 | * Given the constraint: |
0e2d2aaa | 3425 | * |
a4c3c049 | 3426 | * ge->avg.running_sum <= ge->avg.runnable_sum <= LOAD_AVG_MAX |
0e2d2aaa | 3427 | * |
a4c3c049 VG |
3428 | * We can construct a rule that adds runnable to a rq by assuming minimal |
3429 | * overlap. | |
0e2d2aaa | 3430 | * |
a4c3c049 | 3431 | * On removal, we'll assume each task is equally runnable; which yields: |
0e2d2aaa | 3432 | * |
a4c3c049 | 3433 | * grq->avg.runnable_sum = grq->avg.load_sum / grq->load.weight |
0e2d2aaa | 3434 | * |
a4c3c049 | 3435 | * XXX: only do this for the part of runnable > running ? |
0e2d2aaa | 3436 | * |
0e2d2aaa PZ |
3437 | */ |
3438 | ||
09a43ace | 3439 | static inline void |
0e2d2aaa | 3440 | update_tg_cfs_util(struct cfs_rq *cfs_rq, struct sched_entity *se, struct cfs_rq *gcfs_rq) |
09a43ace | 3441 | { |
09a43ace | 3442 | long delta = gcfs_rq->avg.util_avg - se->avg.util_avg; |
87e867b4 | 3443 | u32 divider; |
09a43ace VG |
3444 | |
3445 | /* Nothing to update */ | |
3446 | if (!delta) | |
3447 | return; | |
3448 | ||
87e867b4 VG |
3449 | /* |
3450 | * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. | |
3451 | * See ___update_load_avg() for details. | |
3452 | */ | |
3453 | divider = get_pelt_divider(&cfs_rq->avg); | |
3454 | ||
09a43ace VG |
3455 | /* Set new sched_entity's utilization */ |
3456 | se->avg.util_avg = gcfs_rq->avg.util_avg; | |
95d68593 | 3457 | se->avg.util_sum = se->avg.util_avg * divider; |
09a43ace VG |
3458 | |
3459 | /* Update parent cfs_rq utilization */ | |
3460 | add_positive(&cfs_rq->avg.util_avg, delta); | |
95d68593 | 3461 | cfs_rq->avg.util_sum = cfs_rq->avg.util_avg * divider; |
09a43ace VG |
3462 | } |
3463 | ||
9f683953 VG |
3464 | static inline void |
3465 | update_tg_cfs_runnable(struct cfs_rq *cfs_rq, struct sched_entity *se, struct cfs_rq *gcfs_rq) | |
3466 | { | |
3467 | long delta = gcfs_rq->avg.runnable_avg - se->avg.runnable_avg; | |
87e867b4 | 3468 | u32 divider; |
9f683953 VG |
3469 | |
3470 | /* Nothing to update */ | |
3471 | if (!delta) | |
3472 | return; | |
3473 | ||
87e867b4 VG |
3474 | /* |
3475 | * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. | |
3476 | * See ___update_load_avg() for details. | |
3477 | */ | |
3478 | divider = get_pelt_divider(&cfs_rq->avg); | |
3479 | ||
9f683953 VG |
3480 | /* Set new sched_entity's runnable */ |
3481 | se->avg.runnable_avg = gcfs_rq->avg.runnable_avg; | |
95d68593 | 3482 | se->avg.runnable_sum = se->avg.runnable_avg * divider; |
9f683953 VG |
3483 | |
3484 | /* Update parent cfs_rq runnable */ | |
3485 | add_positive(&cfs_rq->avg.runnable_avg, delta); | |
95d68593 | 3486 | cfs_rq->avg.runnable_sum = cfs_rq->avg.runnable_avg * divider; |
9f683953 VG |
3487 | } |
3488 | ||
09a43ace | 3489 | static inline void |
0dacee1b | 3490 | update_tg_cfs_load(struct cfs_rq *cfs_rq, struct sched_entity *se, struct cfs_rq *gcfs_rq) |
09a43ace | 3491 | { |
a4c3c049 | 3492 | long delta_avg, running_sum, runnable_sum = gcfs_rq->prop_runnable_sum; |
0dacee1b VG |
3493 | unsigned long load_avg; |
3494 | u64 load_sum = 0; | |
a4c3c049 | 3495 | s64 delta_sum; |
95d68593 | 3496 | u32 divider; |
09a43ace | 3497 | |
0e2d2aaa PZ |
3498 | if (!runnable_sum) |
3499 | return; | |
09a43ace | 3500 | |
0e2d2aaa | 3501 | gcfs_rq->prop_runnable_sum = 0; |
09a43ace | 3502 | |
95d68593 VG |
3503 | /* |
3504 | * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. | |
3505 | * See ___update_load_avg() for details. | |
3506 | */ | |
87e867b4 | 3507 | divider = get_pelt_divider(&cfs_rq->avg); |
95d68593 | 3508 | |
a4c3c049 VG |
3509 | if (runnable_sum >= 0) { |
3510 | /* | |
3511 | * Add runnable; clip at LOAD_AVG_MAX. Reflects that until | |
3512 | * the CPU is saturated running == runnable. | |
3513 | */ | |
3514 | runnable_sum += se->avg.load_sum; | |
95d68593 | 3515 | runnable_sum = min_t(long, runnable_sum, divider); |
a4c3c049 VG |
3516 | } else { |
3517 | /* | |
3518 | * Estimate the new unweighted runnable_sum of the gcfs_rq by | |
3519 | * assuming all tasks are equally runnable. | |
3520 | */ | |
3521 | if (scale_load_down(gcfs_rq->load.weight)) { | |
3522 | load_sum = div_s64(gcfs_rq->avg.load_sum, | |
3523 | scale_load_down(gcfs_rq->load.weight)); | |
3524 | } | |
3525 | ||
3526 | /* But make sure to not inflate se's runnable */ | |
3527 | runnable_sum = min(se->avg.load_sum, load_sum); | |
3528 | } | |
3529 | ||
3530 | /* | |
3531 | * runnable_sum can't be lower than running_sum | |
23127296 VG |
3532 | * Rescale running sum to be in the same range as runnable sum |
3533 | * running_sum is in [0 : LOAD_AVG_MAX << SCHED_CAPACITY_SHIFT] | |
3534 | * runnable_sum is in [0 : LOAD_AVG_MAX] | |
a4c3c049 | 3535 | */ |
23127296 | 3536 | running_sum = se->avg.util_sum >> SCHED_CAPACITY_SHIFT; |
a4c3c049 VG |
3537 | runnable_sum = max(runnable_sum, running_sum); |
3538 | ||
0e2d2aaa | 3539 | load_sum = (s64)se_weight(se) * runnable_sum; |
95d68593 | 3540 | load_avg = div_s64(load_sum, divider); |
09a43ace | 3541 | |
a4c3c049 VG |
3542 | delta_sum = load_sum - (s64)se_weight(se) * se->avg.load_sum; |
3543 | delta_avg = load_avg - se->avg.load_avg; | |
09a43ace | 3544 | |
a4c3c049 VG |
3545 | se->avg.load_sum = runnable_sum; |
3546 | se->avg.load_avg = load_avg; | |
3547 | add_positive(&cfs_rq->avg.load_avg, delta_avg); | |
3548 | add_positive(&cfs_rq->avg.load_sum, delta_sum); | |
09a43ace VG |
3549 | } |
3550 | ||
0e2d2aaa | 3551 | static inline void add_tg_cfs_propagate(struct cfs_rq *cfs_rq, long runnable_sum) |
09a43ace | 3552 | { |
0e2d2aaa PZ |
3553 | cfs_rq->propagate = 1; |
3554 | cfs_rq->prop_runnable_sum += runnable_sum; | |
09a43ace VG |
3555 | } |
3556 | ||
3557 | /* Update task and its cfs_rq load average */ | |
3558 | static inline int propagate_entity_load_avg(struct sched_entity *se) | |
3559 | { | |
0e2d2aaa | 3560 | struct cfs_rq *cfs_rq, *gcfs_rq; |
09a43ace VG |
3561 | |
3562 | if (entity_is_task(se)) | |
3563 | return 0; | |
3564 | ||
0e2d2aaa PZ |
3565 | gcfs_rq = group_cfs_rq(se); |
3566 | if (!gcfs_rq->propagate) | |
09a43ace VG |
3567 | return 0; |
3568 | ||
0e2d2aaa PZ |
3569 | gcfs_rq->propagate = 0; |
3570 | ||
09a43ace VG |
3571 | cfs_rq = cfs_rq_of(se); |
3572 | ||
0e2d2aaa | 3573 | add_tg_cfs_propagate(cfs_rq, gcfs_rq->prop_runnable_sum); |
09a43ace | 3574 | |
0e2d2aaa | 3575 | update_tg_cfs_util(cfs_rq, se, gcfs_rq); |
9f683953 | 3576 | update_tg_cfs_runnable(cfs_rq, se, gcfs_rq); |
0dacee1b | 3577 | update_tg_cfs_load(cfs_rq, se, gcfs_rq); |
09a43ace | 3578 | |
ba19f51f | 3579 | trace_pelt_cfs_tp(cfs_rq); |
8de6242c | 3580 | trace_pelt_se_tp(se); |
ba19f51f | 3581 | |
09a43ace VG |
3582 | return 1; |
3583 | } | |
3584 | ||
bc427898 VG |
3585 | /* |
3586 | * Check if we need to update the load and the utilization of a blocked | |
3587 | * group_entity: | |
3588 | */ | |
3589 | static inline bool skip_blocked_update(struct sched_entity *se) | |
3590 | { | |
3591 | struct cfs_rq *gcfs_rq = group_cfs_rq(se); | |
3592 | ||
3593 | /* | |
3594 | * If sched_entity still have not zero load or utilization, we have to | |
3595 | * decay it: | |
3596 | */ | |
3597 | if (se->avg.load_avg || se->avg.util_avg) | |
3598 | return false; | |
3599 | ||
3600 | /* | |
3601 | * If there is a pending propagation, we have to update the load and | |
3602 | * the utilization of the sched_entity: | |
3603 | */ | |
0e2d2aaa | 3604 | if (gcfs_rq->propagate) |
bc427898 VG |
3605 | return false; |
3606 | ||
3607 | /* | |
3608 | * Otherwise, the load and the utilization of the sched_entity is | |
3609 | * already zero and there is no pending propagation, so it will be a | |
3610 | * waste of time to try to decay it: | |
3611 | */ | |
3612 | return true; | |
3613 | } | |
3614 | ||
6e83125c | 3615 | #else /* CONFIG_FAIR_GROUP_SCHED */ |
09a43ace | 3616 | |
9d89c257 | 3617 | static inline void update_tg_load_avg(struct cfs_rq *cfs_rq, int force) {} |
09a43ace VG |
3618 | |
3619 | static inline int propagate_entity_load_avg(struct sched_entity *se) | |
3620 | { | |
3621 | return 0; | |
3622 | } | |
3623 | ||
0e2d2aaa | 3624 | static inline void add_tg_cfs_propagate(struct cfs_rq *cfs_rq, long runnable_sum) {} |
09a43ace | 3625 | |
6e83125c | 3626 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
c566e8e9 | 3627 | |
3d30544f PZ |
3628 | /** |
3629 | * update_cfs_rq_load_avg - update the cfs_rq's load/util averages | |
23127296 | 3630 | * @now: current time, as per cfs_rq_clock_pelt() |
3d30544f | 3631 | * @cfs_rq: cfs_rq to update |
3d30544f PZ |
3632 | * |
3633 | * The cfs_rq avg is the direct sum of all its entities (blocked and runnable) | |
3634 | * avg. The immediate corollary is that all (fair) tasks must be attached, see | |
3635 | * post_init_entity_util_avg(). | |
3636 | * | |
3637 | * cfs_rq->avg is used for task_h_load() and update_cfs_share() for example. | |
3638 | * | |
7c3edd2c PZ |
3639 | * Returns true if the load decayed or we removed load. |
3640 | * | |
3641 | * Since both these conditions indicate a changed cfs_rq->avg.load we should | |
3642 | * call update_tg_load_avg() when this function returns true. | |
3d30544f | 3643 | */ |
a2c6c91f | 3644 | static inline int |
3a123bbb | 3645 | update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq) |
2dac754e | 3646 | { |
9f683953 | 3647 | unsigned long removed_load = 0, removed_util = 0, removed_runnable = 0; |
9d89c257 | 3648 | struct sched_avg *sa = &cfs_rq->avg; |
2a2f5d4e | 3649 | int decayed = 0; |
2dac754e | 3650 | |
2a2f5d4e PZ |
3651 | if (cfs_rq->removed.nr) { |
3652 | unsigned long r; | |
87e867b4 | 3653 | u32 divider = get_pelt_divider(&cfs_rq->avg); |
2a2f5d4e PZ |
3654 | |
3655 | raw_spin_lock(&cfs_rq->removed.lock); | |
3656 | swap(cfs_rq->removed.util_avg, removed_util); | |
3657 | swap(cfs_rq->removed.load_avg, removed_load); | |
9f683953 | 3658 | swap(cfs_rq->removed.runnable_avg, removed_runnable); |
2a2f5d4e PZ |
3659 | cfs_rq->removed.nr = 0; |
3660 | raw_spin_unlock(&cfs_rq->removed.lock); | |
3661 | ||
2a2f5d4e | 3662 | r = removed_load; |
89741892 | 3663 | sub_positive(&sa->load_avg, r); |
9a2dd585 | 3664 | sub_positive(&sa->load_sum, r * divider); |
2dac754e | 3665 | |
2a2f5d4e | 3666 | r = removed_util; |
89741892 | 3667 | sub_positive(&sa->util_avg, r); |
9a2dd585 | 3668 | sub_positive(&sa->util_sum, r * divider); |
2a2f5d4e | 3669 | |
9f683953 VG |
3670 | r = removed_runnable; |
3671 | sub_positive(&sa->runnable_avg, r); | |
3672 | sub_positive(&sa->runnable_sum, r * divider); | |
3673 | ||
3674 | /* | |
3675 | * removed_runnable is the unweighted version of removed_load so we | |
3676 | * can use it to estimate removed_load_sum. | |
3677 | */ | |
3678 | add_tg_cfs_propagate(cfs_rq, | |
3679 | -(long)(removed_runnable * divider) >> SCHED_CAPACITY_SHIFT); | |
2a2f5d4e PZ |
3680 | |
3681 | decayed = 1; | |
9d89c257 | 3682 | } |
36ee28e4 | 3683 | |
23127296 | 3684 | decayed |= __update_load_avg_cfs_rq(now, cfs_rq); |
36ee28e4 | 3685 | |
9d89c257 YD |
3686 | #ifndef CONFIG_64BIT |
3687 | smp_wmb(); | |
3688 | cfs_rq->load_last_update_time_copy = sa->last_update_time; | |
3689 | #endif | |
36ee28e4 | 3690 | |
2a2f5d4e | 3691 | return decayed; |
21e96f88 SM |
3692 | } |
3693 | ||
3d30544f PZ |
3694 | /** |
3695 | * attach_entity_load_avg - attach this entity to its cfs_rq load avg | |
3696 | * @cfs_rq: cfs_rq to attach to | |
3697 | * @se: sched_entity to attach | |
3698 | * | |
3699 | * Must call update_cfs_rq_load_avg() before this, since we rely on | |
3700 | * cfs_rq->avg.last_update_time being current. | |
3701 | */ | |
a4f9a0e5 | 3702 | static void attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) |
a05e8c51 | 3703 | { |
95d68593 VG |
3704 | /* |
3705 | * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. | |
3706 | * See ___update_load_avg() for details. | |
3707 | */ | |
87e867b4 | 3708 | u32 divider = get_pelt_divider(&cfs_rq->avg); |
f207934f PZ |
3709 | |
3710 | /* | |
3711 | * When we attach the @se to the @cfs_rq, we must align the decay | |
3712 | * window because without that, really weird and wonderful things can | |
3713 | * happen. | |
3714 | * | |
3715 | * XXX illustrate | |
3716 | */ | |
a05e8c51 | 3717 | se->avg.last_update_time = cfs_rq->avg.last_update_time; |
f207934f PZ |
3718 | se->avg.period_contrib = cfs_rq->avg.period_contrib; |
3719 | ||
3720 | /* | |
3721 | * Hell(o) Nasty stuff.. we need to recompute _sum based on the new | |
3722 | * period_contrib. This isn't strictly correct, but since we're | |
3723 | * entirely outside of the PELT hierarchy, nobody cares if we truncate | |
3724 | * _sum a little. | |
3725 | */ | |
3726 | se->avg.util_sum = se->avg.util_avg * divider; | |
3727 | ||
9f683953 VG |
3728 | se->avg.runnable_sum = se->avg.runnable_avg * divider; |
3729 | ||
f207934f PZ |
3730 | se->avg.load_sum = divider; |
3731 | if (se_weight(se)) { | |
3732 | se->avg.load_sum = | |
3733 | div_u64(se->avg.load_avg * se->avg.load_sum, se_weight(se)); | |
3734 | } | |
3735 | ||
8d5b9025 | 3736 | enqueue_load_avg(cfs_rq, se); |
a05e8c51 BP |
3737 | cfs_rq->avg.util_avg += se->avg.util_avg; |
3738 | cfs_rq->avg.util_sum += se->avg.util_sum; | |
9f683953 VG |
3739 | cfs_rq->avg.runnable_avg += se->avg.runnable_avg; |
3740 | cfs_rq->avg.runnable_sum += se->avg.runnable_sum; | |
0e2d2aaa PZ |
3741 | |
3742 | add_tg_cfs_propagate(cfs_rq, se->avg.load_sum); | |
a2c6c91f | 3743 | |
a4f9a0e5 | 3744 | cfs_rq_util_change(cfs_rq, 0); |
ba19f51f QY |
3745 | |
3746 | trace_pelt_cfs_tp(cfs_rq); | |
a05e8c51 BP |
3747 | } |
3748 | ||
3d30544f PZ |
3749 | /** |
3750 | * detach_entity_load_avg - detach this entity from its cfs_rq load avg | |
3751 | * @cfs_rq: cfs_rq to detach from | |
3752 | * @se: sched_entity to detach | |
3753 | * | |
3754 | * Must call update_cfs_rq_load_avg() before this, since we rely on | |
3755 | * cfs_rq->avg.last_update_time being current. | |
3756 | */ | |
a05e8c51 BP |
3757 | static void detach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) |
3758 | { | |
8d5b9025 | 3759 | dequeue_load_avg(cfs_rq, se); |
89741892 PZ |
3760 | sub_positive(&cfs_rq->avg.util_avg, se->avg.util_avg); |
3761 | sub_positive(&cfs_rq->avg.util_sum, se->avg.util_sum); | |
9f683953 VG |
3762 | sub_positive(&cfs_rq->avg.runnable_avg, se->avg.runnable_avg); |
3763 | sub_positive(&cfs_rq->avg.runnable_sum, se->avg.runnable_sum); | |
0e2d2aaa PZ |
3764 | |
3765 | add_tg_cfs_propagate(cfs_rq, -se->avg.load_sum); | |
a2c6c91f | 3766 | |
ea14b57e | 3767 | cfs_rq_util_change(cfs_rq, 0); |
ba19f51f QY |
3768 | |
3769 | trace_pelt_cfs_tp(cfs_rq); | |
a05e8c51 BP |
3770 | } |
3771 | ||
b382a531 PZ |
3772 | /* |
3773 | * Optional action to be done while updating the load average | |
3774 | */ | |
3775 | #define UPDATE_TG 0x1 | |
3776 | #define SKIP_AGE_LOAD 0x2 | |
3777 | #define DO_ATTACH 0x4 | |
3778 | ||
3779 | /* Update task and its cfs_rq load average */ | |
3780 | static inline void update_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) | |
3781 | { | |
23127296 | 3782 | u64 now = cfs_rq_clock_pelt(cfs_rq); |
b382a531 PZ |
3783 | int decayed; |
3784 | ||
3785 | /* | |
3786 | * Track task load average for carrying it to new CPU after migrated, and | |
3787 | * track group sched_entity load average for task_h_load calc in migration | |
3788 | */ | |
3789 | if (se->avg.last_update_time && !(flags & SKIP_AGE_LOAD)) | |
23127296 | 3790 | __update_load_avg_se(now, cfs_rq, se); |
b382a531 PZ |
3791 | |
3792 | decayed = update_cfs_rq_load_avg(now, cfs_rq); | |
3793 | decayed |= propagate_entity_load_avg(se); | |
3794 | ||
3795 | if (!se->avg.last_update_time && (flags & DO_ATTACH)) { | |
3796 | ||
ea14b57e PZ |
3797 | /* |
3798 | * DO_ATTACH means we're here from enqueue_entity(). | |
3799 | * !last_update_time means we've passed through | |
3800 | * migrate_task_rq_fair() indicating we migrated. | |
3801 | * | |
3802 | * IOW we're enqueueing a task on a new CPU. | |
3803 | */ | |
a4f9a0e5 | 3804 | attach_entity_load_avg(cfs_rq, se); |
b382a531 PZ |
3805 | update_tg_load_avg(cfs_rq, 0); |
3806 | ||
bef69dd8 VG |
3807 | } else if (decayed) { |
3808 | cfs_rq_util_change(cfs_rq, 0); | |
3809 | ||
3810 | if (flags & UPDATE_TG) | |
3811 | update_tg_load_avg(cfs_rq, 0); | |
3812 | } | |
b382a531 PZ |
3813 | } |
3814 | ||
9d89c257 | 3815 | #ifndef CONFIG_64BIT |
0905f04e YD |
3816 | static inline u64 cfs_rq_last_update_time(struct cfs_rq *cfs_rq) |
3817 | { | |
9d89c257 | 3818 | u64 last_update_time_copy; |
0905f04e | 3819 | u64 last_update_time; |
9ee474f5 | 3820 | |
9d89c257 YD |
3821 | do { |
3822 | last_update_time_copy = cfs_rq->load_last_update_time_copy; | |
3823 | smp_rmb(); | |
3824 | last_update_time = cfs_rq->avg.last_update_time; | |
3825 | } while (last_update_time != last_update_time_copy); | |
0905f04e YD |
3826 | |
3827 | return last_update_time; | |
3828 | } | |
9d89c257 | 3829 | #else |
0905f04e YD |
3830 | static inline u64 cfs_rq_last_update_time(struct cfs_rq *cfs_rq) |
3831 | { | |
3832 | return cfs_rq->avg.last_update_time; | |
3833 | } | |
9d89c257 YD |
3834 | #endif |
3835 | ||
104cb16d MR |
3836 | /* |
3837 | * Synchronize entity load avg of dequeued entity without locking | |
3838 | * the previous rq. | |
3839 | */ | |
71b47eaf | 3840 | static void sync_entity_load_avg(struct sched_entity *se) |
104cb16d MR |
3841 | { |
3842 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
3843 | u64 last_update_time; | |
3844 | ||
3845 | last_update_time = cfs_rq_last_update_time(cfs_rq); | |
23127296 | 3846 | __update_load_avg_blocked_se(last_update_time, se); |
104cb16d MR |
3847 | } |
3848 | ||
0905f04e YD |
3849 | /* |
3850 | * Task first catches up with cfs_rq, and then subtract | |
3851 | * itself from the cfs_rq (task must be off the queue now). | |
3852 | */ | |
71b47eaf | 3853 | static void remove_entity_load_avg(struct sched_entity *se) |
0905f04e YD |
3854 | { |
3855 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
2a2f5d4e | 3856 | unsigned long flags; |
0905f04e YD |
3857 | |
3858 | /* | |
7dc603c9 PZ |
3859 | * tasks cannot exit without having gone through wake_up_new_task() -> |
3860 | * post_init_entity_util_avg() which will have added things to the | |
3861 | * cfs_rq, so we can remove unconditionally. | |
0905f04e | 3862 | */ |
0905f04e | 3863 | |
104cb16d | 3864 | sync_entity_load_avg(se); |
2a2f5d4e PZ |
3865 | |
3866 | raw_spin_lock_irqsave(&cfs_rq->removed.lock, flags); | |
3867 | ++cfs_rq->removed.nr; | |
3868 | cfs_rq->removed.util_avg += se->avg.util_avg; | |
3869 | cfs_rq->removed.load_avg += se->avg.load_avg; | |
9f683953 | 3870 | cfs_rq->removed.runnable_avg += se->avg.runnable_avg; |
2a2f5d4e | 3871 | raw_spin_unlock_irqrestore(&cfs_rq->removed.lock, flags); |
2dac754e | 3872 | } |
642dbc39 | 3873 | |
9f683953 VG |
3874 | static inline unsigned long cfs_rq_runnable_avg(struct cfs_rq *cfs_rq) |
3875 | { | |
3876 | return cfs_rq->avg.runnable_avg; | |
3877 | } | |
3878 | ||
7ea241af YD |
3879 | static inline unsigned long cfs_rq_load_avg(struct cfs_rq *cfs_rq) |
3880 | { | |
3881 | return cfs_rq->avg.load_avg; | |
3882 | } | |
3883 | ||
d91cecc1 CY |
3884 | static int newidle_balance(struct rq *this_rq, struct rq_flags *rf); |
3885 | ||
7f65ea42 PB |
3886 | static inline unsigned long task_util(struct task_struct *p) |
3887 | { | |
3888 | return READ_ONCE(p->se.avg.util_avg); | |
3889 | } | |
3890 | ||
3891 | static inline unsigned long _task_util_est(struct task_struct *p) | |
3892 | { | |
3893 | struct util_est ue = READ_ONCE(p->se.avg.util_est); | |
3894 | ||
92a801e5 | 3895 | return (max(ue.ewma, ue.enqueued) | UTIL_AVG_UNCHANGED); |
7f65ea42 PB |
3896 | } |
3897 | ||
3898 | static inline unsigned long task_util_est(struct task_struct *p) | |
3899 | { | |
3900 | return max(task_util(p), _task_util_est(p)); | |
3901 | } | |
3902 | ||
a7008c07 VS |
3903 | #ifdef CONFIG_UCLAMP_TASK |
3904 | static inline unsigned long uclamp_task_util(struct task_struct *p) | |
3905 | { | |
3906 | return clamp(task_util_est(p), | |
3907 | uclamp_eff_value(p, UCLAMP_MIN), | |
3908 | uclamp_eff_value(p, UCLAMP_MAX)); | |
3909 | } | |
3910 | #else | |
3911 | static inline unsigned long uclamp_task_util(struct task_struct *p) | |
3912 | { | |
3913 | return task_util_est(p); | |
3914 | } | |
3915 | #endif | |
3916 | ||
7f65ea42 PB |
3917 | static inline void util_est_enqueue(struct cfs_rq *cfs_rq, |
3918 | struct task_struct *p) | |
3919 | { | |
3920 | unsigned int enqueued; | |
3921 | ||
3922 | if (!sched_feat(UTIL_EST)) | |
3923 | return; | |
3924 | ||
3925 | /* Update root cfs_rq's estimated utilization */ | |
3926 | enqueued = cfs_rq->avg.util_est.enqueued; | |
92a801e5 | 3927 | enqueued += _task_util_est(p); |
7f65ea42 | 3928 | WRITE_ONCE(cfs_rq->avg.util_est.enqueued, enqueued); |
4581bea8 VD |
3929 | |
3930 | trace_sched_util_est_cfs_tp(cfs_rq); | |
7f65ea42 PB |
3931 | } |
3932 | ||
3933 | /* | |
3934 | * Check if a (signed) value is within a specified (unsigned) margin, | |
3935 | * based on the observation that: | |
3936 | * | |
3937 | * abs(x) < y := (unsigned)(x + y - 1) < (2 * y - 1) | |
3938 | * | |
3939 | * NOTE: this only works when value + maring < INT_MAX. | |
3940 | */ | |
3941 | static inline bool within_margin(int value, int margin) | |
3942 | { | |
3943 | return ((unsigned int)(value + margin - 1) < (2 * margin - 1)); | |
3944 | } | |
3945 | ||
3946 | static void | |
3947 | util_est_dequeue(struct cfs_rq *cfs_rq, struct task_struct *p, bool task_sleep) | |
3948 | { | |
3949 | long last_ewma_diff; | |
3950 | struct util_est ue; | |
10a35e68 | 3951 | int cpu; |
7f65ea42 PB |
3952 | |
3953 | if (!sched_feat(UTIL_EST)) | |
3954 | return; | |
3955 | ||
3482d98b VG |
3956 | /* Update root cfs_rq's estimated utilization */ |
3957 | ue.enqueued = cfs_rq->avg.util_est.enqueued; | |
92a801e5 | 3958 | ue.enqueued -= min_t(unsigned int, ue.enqueued, _task_util_est(p)); |
7f65ea42 PB |
3959 | WRITE_ONCE(cfs_rq->avg.util_est.enqueued, ue.enqueued); |
3960 | ||
4581bea8 VD |
3961 | trace_sched_util_est_cfs_tp(cfs_rq); |
3962 | ||
7f65ea42 PB |
3963 | /* |
3964 | * Skip update of task's estimated utilization when the task has not | |
3965 | * yet completed an activation, e.g. being migrated. | |
3966 | */ | |
3967 | if (!task_sleep) | |
3968 | return; | |
3969 | ||
d519329f PB |
3970 | /* |
3971 | * If the PELT values haven't changed since enqueue time, | |
3972 | * skip the util_est update. | |
3973 | */ | |
3974 | ue = p->se.avg.util_est; | |
3975 | if (ue.enqueued & UTIL_AVG_UNCHANGED) | |
3976 | return; | |
3977 | ||
b8c96361 PB |
3978 | /* |
3979 | * Reset EWMA on utilization increases, the moving average is used only | |
3980 | * to smooth utilization decreases. | |
3981 | */ | |
3982 | ue.enqueued = (task_util(p) | UTIL_AVG_UNCHANGED); | |
3983 | if (sched_feat(UTIL_EST_FASTUP)) { | |
3984 | if (ue.ewma < ue.enqueued) { | |
3985 | ue.ewma = ue.enqueued; | |
3986 | goto done; | |
3987 | } | |
3988 | } | |
3989 | ||
7f65ea42 PB |
3990 | /* |
3991 | * Skip update of task's estimated utilization when its EWMA is | |
3992 | * already ~1% close to its last activation value. | |
3993 | */ | |
7f65ea42 PB |
3994 | last_ewma_diff = ue.enqueued - ue.ewma; |
3995 | if (within_margin(last_ewma_diff, (SCHED_CAPACITY_SCALE / 100))) | |
3996 | return; | |
3997 | ||
10a35e68 VG |
3998 | /* |
3999 | * To avoid overestimation of actual task utilization, skip updates if | |
4000 | * we cannot grant there is idle time in this CPU. | |
4001 | */ | |
4002 | cpu = cpu_of(rq_of(cfs_rq)); | |
4003 | if (task_util(p) > capacity_orig_of(cpu)) | |
4004 | return; | |
4005 | ||
7f65ea42 PB |
4006 | /* |
4007 | * Update Task's estimated utilization | |
4008 | * | |
4009 | * When *p completes an activation we can consolidate another sample | |
4010 | * of the task size. This is done by storing the current PELT value | |
4011 | * as ue.enqueued and by using this value to update the Exponential | |
4012 | * Weighted Moving Average (EWMA): | |
4013 | * | |
4014 | * ewma(t) = w * task_util(p) + (1-w) * ewma(t-1) | |
4015 | * = w * task_util(p) + ewma(t-1) - w * ewma(t-1) | |
4016 | * = w * (task_util(p) - ewma(t-1)) + ewma(t-1) | |
4017 | * = w * ( last_ewma_diff ) + ewma(t-1) | |
4018 | * = w * (last_ewma_diff + ewma(t-1) / w) | |
4019 | * | |
4020 | * Where 'w' is the weight of new samples, which is configured to be | |
4021 | * 0.25, thus making w=1/4 ( >>= UTIL_EST_WEIGHT_SHIFT) | |
4022 | */ | |
4023 | ue.ewma <<= UTIL_EST_WEIGHT_SHIFT; | |
4024 | ue.ewma += last_ewma_diff; | |
4025 | ue.ewma >>= UTIL_EST_WEIGHT_SHIFT; | |
b8c96361 | 4026 | done: |
7f65ea42 | 4027 | WRITE_ONCE(p->se.avg.util_est, ue); |
4581bea8 VD |
4028 | |
4029 | trace_sched_util_est_se_tp(&p->se); | |
7f65ea42 PB |
4030 | } |
4031 | ||
3b1baa64 MR |
4032 | static inline int task_fits_capacity(struct task_struct *p, long capacity) |
4033 | { | |
a7008c07 | 4034 | return fits_capacity(uclamp_task_util(p), capacity); |
3b1baa64 MR |
4035 | } |
4036 | ||
4037 | static inline void update_misfit_status(struct task_struct *p, struct rq *rq) | |
4038 | { | |
4039 | if (!static_branch_unlikely(&sched_asym_cpucapacity)) | |
4040 | return; | |
4041 | ||
4042 | if (!p) { | |
4043 | rq->misfit_task_load = 0; | |
4044 | return; | |
4045 | } | |
4046 | ||
4047 | if (task_fits_capacity(p, capacity_of(cpu_of(rq)))) { | |
4048 | rq->misfit_task_load = 0; | |
4049 | return; | |
4050 | } | |
4051 | ||
4052 | rq->misfit_task_load = task_h_load(p); | |
4053 | } | |
4054 | ||
38033c37 PZ |
4055 | #else /* CONFIG_SMP */ |
4056 | ||
d31b1a66 VG |
4057 | #define UPDATE_TG 0x0 |
4058 | #define SKIP_AGE_LOAD 0x0 | |
b382a531 | 4059 | #define DO_ATTACH 0x0 |
d31b1a66 | 4060 | |
88c0616e | 4061 | static inline void update_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se, int not_used1) |
536bd00c | 4062 | { |
ea14b57e | 4063 | cfs_rq_util_change(cfs_rq, 0); |
536bd00c RW |
4064 | } |
4065 | ||
9d89c257 | 4066 | static inline void remove_entity_load_avg(struct sched_entity *se) {} |
6e83125c | 4067 | |
a05e8c51 | 4068 | static inline void |
a4f9a0e5 | 4069 | attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {} |
a05e8c51 BP |
4070 | static inline void |
4071 | detach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {} | |
4072 | ||
d91cecc1 | 4073 | static inline int newidle_balance(struct rq *rq, struct rq_flags *rf) |
6e83125c PZ |
4074 | { |
4075 | return 0; | |
4076 | } | |
4077 | ||
7f65ea42 PB |
4078 | static inline void |
4079 | util_est_enqueue(struct cfs_rq *cfs_rq, struct task_struct *p) {} | |
4080 | ||
4081 | static inline void | |
4082 | util_est_dequeue(struct cfs_rq *cfs_rq, struct task_struct *p, | |
4083 | bool task_sleep) {} | |
3b1baa64 | 4084 | static inline void update_misfit_status(struct task_struct *p, struct rq *rq) {} |
7f65ea42 | 4085 | |
38033c37 | 4086 | #endif /* CONFIG_SMP */ |
9d85f21c | 4087 | |
ddc97297 PZ |
4088 | static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se) |
4089 | { | |
4090 | #ifdef CONFIG_SCHED_DEBUG | |
4091 | s64 d = se->vruntime - cfs_rq->min_vruntime; | |
4092 | ||
4093 | if (d < 0) | |
4094 | d = -d; | |
4095 | ||
4096 | if (d > 3*sysctl_sched_latency) | |
ae92882e | 4097 | schedstat_inc(cfs_rq->nr_spread_over); |
ddc97297 PZ |
4098 | #endif |
4099 | } | |
4100 | ||
aeb73b04 PZ |
4101 | static void |
4102 | place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial) | |
4103 | { | |
1af5f730 | 4104 | u64 vruntime = cfs_rq->min_vruntime; |
94dfb5e7 | 4105 | |
2cb8600e PZ |
4106 | /* |
4107 | * The 'current' period is already promised to the current tasks, | |
4108 | * however the extra weight of the new task will slow them down a | |
4109 | * little, place the new task so that it fits in the slot that | |
4110 | * stays open at the end. | |
4111 | */ | |
94dfb5e7 | 4112 | if (initial && sched_feat(START_DEBIT)) |
f9c0b095 | 4113 | vruntime += sched_vslice(cfs_rq, se); |
aeb73b04 | 4114 | |
a2e7a7eb | 4115 | /* sleeps up to a single latency don't count. */ |
5ca9880c | 4116 | if (!initial) { |
a2e7a7eb | 4117 | unsigned long thresh = sysctl_sched_latency; |
a7be37ac | 4118 | |
a2e7a7eb MG |
4119 | /* |
4120 | * Halve their sleep time's effect, to allow | |
4121 | * for a gentler effect of sleepers: | |
4122 | */ | |
4123 | if (sched_feat(GENTLE_FAIR_SLEEPERS)) | |
4124 | thresh >>= 1; | |
51e0304c | 4125 | |
a2e7a7eb | 4126 | vruntime -= thresh; |
aeb73b04 PZ |
4127 | } |
4128 | ||
b5d9d734 | 4129 | /* ensure we never gain time by being placed backwards. */ |
16c8f1c7 | 4130 | se->vruntime = max_vruntime(se->vruntime, vruntime); |
aeb73b04 PZ |
4131 | } |
4132 | ||
d3d9dc33 PT |
4133 | static void check_enqueue_throttle(struct cfs_rq *cfs_rq); |
4134 | ||
cb251765 MG |
4135 | static inline void check_schedstat_required(void) |
4136 | { | |
4137 | #ifdef CONFIG_SCHEDSTATS | |
4138 | if (schedstat_enabled()) | |
4139 | return; | |
4140 | ||
4141 | /* Force schedstat enabled if a dependent tracepoint is active */ | |
4142 | if (trace_sched_stat_wait_enabled() || | |
4143 | trace_sched_stat_sleep_enabled() || | |
4144 | trace_sched_stat_iowait_enabled() || | |
4145 | trace_sched_stat_blocked_enabled() || | |
4146 | trace_sched_stat_runtime_enabled()) { | |
eda8dca5 | 4147 | printk_deferred_once("Scheduler tracepoints stat_sleep, stat_iowait, " |
cb251765 | 4148 | "stat_blocked and stat_runtime require the " |
f67abed5 | 4149 | "kernel parameter schedstats=enable or " |
cb251765 MG |
4150 | "kernel.sched_schedstats=1\n"); |
4151 | } | |
4152 | #endif | |
4153 | } | |
4154 | ||
fe61468b | 4155 | static inline bool cfs_bandwidth_used(void); |
b5179ac7 PZ |
4156 | |
4157 | /* | |
4158 | * MIGRATION | |
4159 | * | |
4160 | * dequeue | |
4161 | * update_curr() | |
4162 | * update_min_vruntime() | |
4163 | * vruntime -= min_vruntime | |
4164 | * | |
4165 | * enqueue | |
4166 | * update_curr() | |
4167 | * update_min_vruntime() | |
4168 | * vruntime += min_vruntime | |
4169 | * | |
4170 | * this way the vruntime transition between RQs is done when both | |
4171 | * min_vruntime are up-to-date. | |
4172 | * | |
4173 | * WAKEUP (remote) | |
4174 | * | |
59efa0ba | 4175 | * ->migrate_task_rq_fair() (p->state == TASK_WAKING) |
b5179ac7 PZ |
4176 | * vruntime -= min_vruntime |
4177 | * | |
4178 | * enqueue | |
4179 | * update_curr() | |
4180 | * update_min_vruntime() | |
4181 | * vruntime += min_vruntime | |
4182 | * | |
4183 | * this way we don't have the most up-to-date min_vruntime on the originating | |
4184 | * CPU and an up-to-date min_vruntime on the destination CPU. | |
4185 | */ | |
4186 | ||
bf0f6f24 | 4187 | static void |
88ec22d3 | 4188 | enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
bf0f6f24 | 4189 | { |
2f950354 PZ |
4190 | bool renorm = !(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_MIGRATED); |
4191 | bool curr = cfs_rq->curr == se; | |
4192 | ||
88ec22d3 | 4193 | /* |
2f950354 PZ |
4194 | * If we're the current task, we must renormalise before calling |
4195 | * update_curr(). | |
88ec22d3 | 4196 | */ |
2f950354 | 4197 | if (renorm && curr) |
88ec22d3 PZ |
4198 | se->vruntime += cfs_rq->min_vruntime; |
4199 | ||
2f950354 PZ |
4200 | update_curr(cfs_rq); |
4201 | ||
bf0f6f24 | 4202 | /* |
2f950354 PZ |
4203 | * Otherwise, renormalise after, such that we're placed at the current |
4204 | * moment in time, instead of some random moment in the past. Being | |
4205 | * placed in the past could significantly boost this task to the | |
4206 | * fairness detriment of existing tasks. | |
bf0f6f24 | 4207 | */ |
2f950354 PZ |
4208 | if (renorm && !curr) |
4209 | se->vruntime += cfs_rq->min_vruntime; | |
4210 | ||
89ee048f VG |
4211 | /* |
4212 | * When enqueuing a sched_entity, we must: | |
4213 | * - Update loads to have both entity and cfs_rq synced with now. | |
9f683953 | 4214 | * - Add its load to cfs_rq->runnable_avg |
89ee048f VG |
4215 | * - For group_entity, update its weight to reflect the new share of |
4216 | * its group cfs_rq | |
4217 | * - Add its new weight to cfs_rq->load.weight | |
4218 | */ | |
b382a531 | 4219 | update_load_avg(cfs_rq, se, UPDATE_TG | DO_ATTACH); |
9f683953 | 4220 | se_update_runnable(se); |
1ea6c46a | 4221 | update_cfs_group(se); |
17bc14b7 | 4222 | account_entity_enqueue(cfs_rq, se); |
bf0f6f24 | 4223 | |
1a3d027c | 4224 | if (flags & ENQUEUE_WAKEUP) |
aeb73b04 | 4225 | place_entity(cfs_rq, se, 0); |
bf0f6f24 | 4226 | |
cb251765 | 4227 | check_schedstat_required(); |
4fa8d299 JP |
4228 | update_stats_enqueue(cfs_rq, se, flags); |
4229 | check_spread(cfs_rq, se); | |
2f950354 | 4230 | if (!curr) |
83b699ed | 4231 | __enqueue_entity(cfs_rq, se); |
2069dd75 | 4232 | se->on_rq = 1; |
3d4b47b4 | 4233 | |
fe61468b VG |
4234 | /* |
4235 | * When bandwidth control is enabled, cfs might have been removed | |
4236 | * because of a parent been throttled but cfs->nr_running > 1. Try to | |
4237 | * add it unconditionnally. | |
4238 | */ | |
4239 | if (cfs_rq->nr_running == 1 || cfs_bandwidth_used()) | |
3d4b47b4 | 4240 | list_add_leaf_cfs_rq(cfs_rq); |
fe61468b VG |
4241 | |
4242 | if (cfs_rq->nr_running == 1) | |
d3d9dc33 | 4243 | check_enqueue_throttle(cfs_rq); |
bf0f6f24 IM |
4244 | } |
4245 | ||
2c13c919 | 4246 | static void __clear_buddies_last(struct sched_entity *se) |
2002c695 | 4247 | { |
2c13c919 RR |
4248 | for_each_sched_entity(se) { |
4249 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
f1044799 | 4250 | if (cfs_rq->last != se) |
2c13c919 | 4251 | break; |
f1044799 PZ |
4252 | |
4253 | cfs_rq->last = NULL; | |
2c13c919 RR |
4254 | } |
4255 | } | |
2002c695 | 4256 | |
2c13c919 RR |
4257 | static void __clear_buddies_next(struct sched_entity *se) |
4258 | { | |
4259 | for_each_sched_entity(se) { | |
4260 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
f1044799 | 4261 | if (cfs_rq->next != se) |
2c13c919 | 4262 | break; |
f1044799 PZ |
4263 | |
4264 | cfs_rq->next = NULL; | |
2c13c919 | 4265 | } |
2002c695 PZ |
4266 | } |
4267 | ||
ac53db59 RR |
4268 | static void __clear_buddies_skip(struct sched_entity *se) |
4269 | { | |
4270 | for_each_sched_entity(se) { | |
4271 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
f1044799 | 4272 | if (cfs_rq->skip != se) |
ac53db59 | 4273 | break; |
f1044799 PZ |
4274 | |
4275 | cfs_rq->skip = NULL; | |
ac53db59 RR |
4276 | } |
4277 | } | |
4278 | ||
a571bbea PZ |
4279 | static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se) |
4280 | { | |
2c13c919 RR |
4281 | if (cfs_rq->last == se) |
4282 | __clear_buddies_last(se); | |
4283 | ||
4284 | if (cfs_rq->next == se) | |
4285 | __clear_buddies_next(se); | |
ac53db59 RR |
4286 | |
4287 | if (cfs_rq->skip == se) | |
4288 | __clear_buddies_skip(se); | |
a571bbea PZ |
4289 | } |
4290 | ||
6c16a6dc | 4291 | static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq); |
d8b4986d | 4292 | |
bf0f6f24 | 4293 | static void |
371fd7e7 | 4294 | dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
bf0f6f24 | 4295 | { |
a2a2d680 DA |
4296 | /* |
4297 | * Update run-time statistics of the 'current'. | |
4298 | */ | |
4299 | update_curr(cfs_rq); | |
89ee048f VG |
4300 | |
4301 | /* | |
4302 | * When dequeuing a sched_entity, we must: | |
4303 | * - Update loads to have both entity and cfs_rq synced with now. | |
9f683953 | 4304 | * - Subtract its load from the cfs_rq->runnable_avg. |
dfcb245e | 4305 | * - Subtract its previous weight from cfs_rq->load.weight. |
89ee048f VG |
4306 | * - For group entity, update its weight to reflect the new share |
4307 | * of its group cfs_rq. | |
4308 | */ | |
88c0616e | 4309 | update_load_avg(cfs_rq, se, UPDATE_TG); |
9f683953 | 4310 | se_update_runnable(se); |
a2a2d680 | 4311 | |
4fa8d299 | 4312 | update_stats_dequeue(cfs_rq, se, flags); |
67e9fb2a | 4313 | |
2002c695 | 4314 | clear_buddies(cfs_rq, se); |
4793241b | 4315 | |
83b699ed | 4316 | if (se != cfs_rq->curr) |
30cfdcfc | 4317 | __dequeue_entity(cfs_rq, se); |
17bc14b7 | 4318 | se->on_rq = 0; |
30cfdcfc | 4319 | account_entity_dequeue(cfs_rq, se); |
88ec22d3 PZ |
4320 | |
4321 | /* | |
b60205c7 PZ |
4322 | * Normalize after update_curr(); which will also have moved |
4323 | * min_vruntime if @se is the one holding it back. But before doing | |
4324 | * update_min_vruntime() again, which will discount @se's position and | |
4325 | * can move min_vruntime forward still more. | |
88ec22d3 | 4326 | */ |
371fd7e7 | 4327 | if (!(flags & DEQUEUE_SLEEP)) |
88ec22d3 | 4328 | se->vruntime -= cfs_rq->min_vruntime; |
1e876231 | 4329 | |
d8b4986d PT |
4330 | /* return excess runtime on last dequeue */ |
4331 | return_cfs_rq_runtime(cfs_rq); | |
4332 | ||
1ea6c46a | 4333 | update_cfs_group(se); |
b60205c7 PZ |
4334 | |
4335 | /* | |
4336 | * Now advance min_vruntime if @se was the entity holding it back, | |
4337 | * except when: DEQUEUE_SAVE && !DEQUEUE_MOVE, in this case we'll be | |
4338 | * put back on, and if we advance min_vruntime, we'll be placed back | |
4339 | * further than we started -- ie. we'll be penalized. | |
4340 | */ | |
9845c49c | 4341 | if ((flags & (DEQUEUE_SAVE | DEQUEUE_MOVE)) != DEQUEUE_SAVE) |
b60205c7 | 4342 | update_min_vruntime(cfs_rq); |
bf0f6f24 IM |
4343 | } |
4344 | ||
4345 | /* | |
4346 | * Preempt the current task with a newly woken task if needed: | |
4347 | */ | |
7c92e54f | 4348 | static void |
2e09bf55 | 4349 | check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr) |
bf0f6f24 | 4350 | { |
11697830 | 4351 | unsigned long ideal_runtime, delta_exec; |
f4cfb33e WX |
4352 | struct sched_entity *se; |
4353 | s64 delta; | |
11697830 | 4354 | |
6d0f0ebd | 4355 | ideal_runtime = sched_slice(cfs_rq, curr); |
11697830 | 4356 | delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime; |
a9f3e2b5 | 4357 | if (delta_exec > ideal_runtime) { |
8875125e | 4358 | resched_curr(rq_of(cfs_rq)); |
a9f3e2b5 MG |
4359 | /* |
4360 | * The current task ran long enough, ensure it doesn't get | |
4361 | * re-elected due to buddy favours. | |
4362 | */ | |
4363 | clear_buddies(cfs_rq, curr); | |
f685ceac MG |
4364 | return; |
4365 | } | |
4366 | ||
4367 | /* | |
4368 | * Ensure that a task that missed wakeup preemption by a | |
4369 | * narrow margin doesn't have to wait for a full slice. | |
4370 | * This also mitigates buddy induced latencies under load. | |
4371 | */ | |
f685ceac MG |
4372 | if (delta_exec < sysctl_sched_min_granularity) |
4373 | return; | |
4374 | ||
f4cfb33e WX |
4375 | se = __pick_first_entity(cfs_rq); |
4376 | delta = curr->vruntime - se->vruntime; | |
f685ceac | 4377 | |
f4cfb33e WX |
4378 | if (delta < 0) |
4379 | return; | |
d7d82944 | 4380 | |
f4cfb33e | 4381 | if (delta > ideal_runtime) |
8875125e | 4382 | resched_curr(rq_of(cfs_rq)); |
bf0f6f24 IM |
4383 | } |
4384 | ||
83b699ed | 4385 | static void |
8494f412 | 4386 | set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 | 4387 | { |
83b699ed SV |
4388 | /* 'current' is not kept within the tree. */ |
4389 | if (se->on_rq) { | |
4390 | /* | |
4391 | * Any task has to be enqueued before it get to execute on | |
4392 | * a CPU. So account for the time it spent waiting on the | |
4393 | * runqueue. | |
4394 | */ | |
4fa8d299 | 4395 | update_stats_wait_end(cfs_rq, se); |
83b699ed | 4396 | __dequeue_entity(cfs_rq, se); |
88c0616e | 4397 | update_load_avg(cfs_rq, se, UPDATE_TG); |
83b699ed SV |
4398 | } |
4399 | ||
79303e9e | 4400 | update_stats_curr_start(cfs_rq, se); |
429d43bc | 4401 | cfs_rq->curr = se; |
4fa8d299 | 4402 | |
eba1ed4b IM |
4403 | /* |
4404 | * Track our maximum slice length, if the CPU's load is at | |
4405 | * least twice that of our own weight (i.e. dont track it | |
4406 | * when there are only lesser-weight tasks around): | |
4407 | */ | |
f2bedc47 DE |
4408 | if (schedstat_enabled() && |
4409 | rq_of(cfs_rq)->cfs.load.weight >= 2*se->load.weight) { | |
4fa8d299 JP |
4410 | schedstat_set(se->statistics.slice_max, |
4411 | max((u64)schedstat_val(se->statistics.slice_max), | |
4412 | se->sum_exec_runtime - se->prev_sum_exec_runtime)); | |
eba1ed4b | 4413 | } |
4fa8d299 | 4414 | |
4a55b450 | 4415 | se->prev_sum_exec_runtime = se->sum_exec_runtime; |
bf0f6f24 IM |
4416 | } |
4417 | ||
3f3a4904 PZ |
4418 | static int |
4419 | wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se); | |
4420 | ||
ac53db59 RR |
4421 | /* |
4422 | * Pick the next process, keeping these things in mind, in this order: | |
4423 | * 1) keep things fair between processes/task groups | |
4424 | * 2) pick the "next" process, since someone really wants that to run | |
4425 | * 3) pick the "last" process, for cache locality | |
4426 | * 4) do not run the "skip" process, if something else is available | |
4427 | */ | |
678d5718 PZ |
4428 | static struct sched_entity * |
4429 | pick_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *curr) | |
aa2ac252 | 4430 | { |
678d5718 PZ |
4431 | struct sched_entity *left = __pick_first_entity(cfs_rq); |
4432 | struct sched_entity *se; | |
4433 | ||
4434 | /* | |
4435 | * If curr is set we have to see if its left of the leftmost entity | |
4436 | * still in the tree, provided there was anything in the tree at all. | |
4437 | */ | |
4438 | if (!left || (curr && entity_before(curr, left))) | |
4439 | left = curr; | |
4440 | ||
4441 | se = left; /* ideally we run the leftmost entity */ | |
f4b6755f | 4442 | |
ac53db59 RR |
4443 | /* |
4444 | * Avoid running the skip buddy, if running something else can | |
4445 | * be done without getting too unfair. | |
4446 | */ | |
4447 | if (cfs_rq->skip == se) { | |
678d5718 PZ |
4448 | struct sched_entity *second; |
4449 | ||
4450 | if (se == curr) { | |
4451 | second = __pick_first_entity(cfs_rq); | |
4452 | } else { | |
4453 | second = __pick_next_entity(se); | |
4454 | if (!second || (curr && entity_before(curr, second))) | |
4455 | second = curr; | |
4456 | } | |
4457 | ||
ac53db59 RR |
4458 | if (second && wakeup_preempt_entity(second, left) < 1) |
4459 | se = second; | |
4460 | } | |
aa2ac252 | 4461 | |
f685ceac MG |
4462 | /* |
4463 | * Prefer last buddy, try to return the CPU to a preempted task. | |
4464 | */ | |
4465 | if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1) | |
4466 | se = cfs_rq->last; | |
4467 | ||
ac53db59 RR |
4468 | /* |
4469 | * Someone really wants this to run. If it's not unfair, run it. | |
4470 | */ | |
4471 | if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1) | |
4472 | se = cfs_rq->next; | |
4473 | ||
f685ceac | 4474 | clear_buddies(cfs_rq, se); |
4793241b PZ |
4475 | |
4476 | return se; | |
aa2ac252 PZ |
4477 | } |
4478 | ||
678d5718 | 4479 | static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq); |
d3d9dc33 | 4480 | |
ab6cde26 | 4481 | static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev) |
bf0f6f24 IM |
4482 | { |
4483 | /* | |
4484 | * If still on the runqueue then deactivate_task() | |
4485 | * was not called and update_curr() has to be done: | |
4486 | */ | |
4487 | if (prev->on_rq) | |
b7cc0896 | 4488 | update_curr(cfs_rq); |
bf0f6f24 | 4489 | |
d3d9dc33 PT |
4490 | /* throttle cfs_rqs exceeding runtime */ |
4491 | check_cfs_rq_runtime(cfs_rq); | |
4492 | ||
4fa8d299 | 4493 | check_spread(cfs_rq, prev); |
cb251765 | 4494 | |
30cfdcfc | 4495 | if (prev->on_rq) { |
4fa8d299 | 4496 | update_stats_wait_start(cfs_rq, prev); |
30cfdcfc DA |
4497 | /* Put 'current' back into the tree. */ |
4498 | __enqueue_entity(cfs_rq, prev); | |
9d85f21c | 4499 | /* in !on_rq case, update occurred at dequeue */ |
88c0616e | 4500 | update_load_avg(cfs_rq, prev, 0); |
30cfdcfc | 4501 | } |
429d43bc | 4502 | cfs_rq->curr = NULL; |
bf0f6f24 IM |
4503 | } |
4504 | ||
8f4d37ec PZ |
4505 | static void |
4506 | entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued) | |
bf0f6f24 | 4507 | { |
bf0f6f24 | 4508 | /* |
30cfdcfc | 4509 | * Update run-time statistics of the 'current'. |
bf0f6f24 | 4510 | */ |
30cfdcfc | 4511 | update_curr(cfs_rq); |
bf0f6f24 | 4512 | |
9d85f21c PT |
4513 | /* |
4514 | * Ensure that runnable average is periodically updated. | |
4515 | */ | |
88c0616e | 4516 | update_load_avg(cfs_rq, curr, UPDATE_TG); |
1ea6c46a | 4517 | update_cfs_group(curr); |
9d85f21c | 4518 | |
8f4d37ec PZ |
4519 | #ifdef CONFIG_SCHED_HRTICK |
4520 | /* | |
4521 | * queued ticks are scheduled to match the slice, so don't bother | |
4522 | * validating it and just reschedule. | |
4523 | */ | |
983ed7a6 | 4524 | if (queued) { |
8875125e | 4525 | resched_curr(rq_of(cfs_rq)); |
983ed7a6 HH |
4526 | return; |
4527 | } | |
8f4d37ec PZ |
4528 | /* |
4529 | * don't let the period tick interfere with the hrtick preemption | |
4530 | */ | |
4531 | if (!sched_feat(DOUBLE_TICK) && | |
4532 | hrtimer_active(&rq_of(cfs_rq)->hrtick_timer)) | |
4533 | return; | |
4534 | #endif | |
4535 | ||
2c2efaed | 4536 | if (cfs_rq->nr_running > 1) |
2e09bf55 | 4537 | check_preempt_tick(cfs_rq, curr); |
bf0f6f24 IM |
4538 | } |
4539 | ||
ab84d31e PT |
4540 | |
4541 | /************************************************** | |
4542 | * CFS bandwidth control machinery | |
4543 | */ | |
4544 | ||
4545 | #ifdef CONFIG_CFS_BANDWIDTH | |
029632fb | 4546 | |
e9666d10 | 4547 | #ifdef CONFIG_JUMP_LABEL |
c5905afb | 4548 | static struct static_key __cfs_bandwidth_used; |
029632fb PZ |
4549 | |
4550 | static inline bool cfs_bandwidth_used(void) | |
4551 | { | |
c5905afb | 4552 | return static_key_false(&__cfs_bandwidth_used); |
029632fb PZ |
4553 | } |
4554 | ||
1ee14e6c | 4555 | void cfs_bandwidth_usage_inc(void) |
029632fb | 4556 | { |
ce48c146 | 4557 | static_key_slow_inc_cpuslocked(&__cfs_bandwidth_used); |
1ee14e6c BS |
4558 | } |
4559 | ||
4560 | void cfs_bandwidth_usage_dec(void) | |
4561 | { | |
ce48c146 | 4562 | static_key_slow_dec_cpuslocked(&__cfs_bandwidth_used); |
029632fb | 4563 | } |
e9666d10 | 4564 | #else /* CONFIG_JUMP_LABEL */ |
029632fb PZ |
4565 | static bool cfs_bandwidth_used(void) |
4566 | { | |
4567 | return true; | |
4568 | } | |
4569 | ||
1ee14e6c BS |
4570 | void cfs_bandwidth_usage_inc(void) {} |
4571 | void cfs_bandwidth_usage_dec(void) {} | |
e9666d10 | 4572 | #endif /* CONFIG_JUMP_LABEL */ |
029632fb | 4573 | |
ab84d31e PT |
4574 | /* |
4575 | * default period for cfs group bandwidth. | |
4576 | * default: 0.1s, units: nanoseconds | |
4577 | */ | |
4578 | static inline u64 default_cfs_period(void) | |
4579 | { | |
4580 | return 100000000ULL; | |
4581 | } | |
ec12cb7f PT |
4582 | |
4583 | static inline u64 sched_cfs_bandwidth_slice(void) | |
4584 | { | |
4585 | return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC; | |
4586 | } | |
4587 | ||
a9cf55b2 | 4588 | /* |
763a9ec0 QC |
4589 | * Replenish runtime according to assigned quota. We use sched_clock_cpu |
4590 | * directly instead of rq->clock to avoid adding additional synchronization | |
4591 | * around rq->lock. | |
a9cf55b2 PT |
4592 | * |
4593 | * requires cfs_b->lock | |
4594 | */ | |
029632fb | 4595 | void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b) |
a9cf55b2 | 4596 | { |
763a9ec0 QC |
4597 | if (cfs_b->quota != RUNTIME_INF) |
4598 | cfs_b->runtime = cfs_b->quota; | |
a9cf55b2 PT |
4599 | } |
4600 | ||
029632fb PZ |
4601 | static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg) |
4602 | { | |
4603 | return &tg->cfs_bandwidth; | |
4604 | } | |
4605 | ||
85dac906 | 4606 | /* returns 0 on failure to allocate runtime */ |
e98fa02c PT |
4607 | static int __assign_cfs_rq_runtime(struct cfs_bandwidth *cfs_b, |
4608 | struct cfs_rq *cfs_rq, u64 target_runtime) | |
ec12cb7f | 4609 | { |
e98fa02c PT |
4610 | u64 min_amount, amount = 0; |
4611 | ||
4612 | lockdep_assert_held(&cfs_b->lock); | |
ec12cb7f PT |
4613 | |
4614 | /* note: this is a positive sum as runtime_remaining <= 0 */ | |
e98fa02c | 4615 | min_amount = target_runtime - cfs_rq->runtime_remaining; |
ec12cb7f | 4616 | |
ec12cb7f PT |
4617 | if (cfs_b->quota == RUNTIME_INF) |
4618 | amount = min_amount; | |
58088ad0 | 4619 | else { |
77a4d1a1 | 4620 | start_cfs_bandwidth(cfs_b); |
58088ad0 PT |
4621 | |
4622 | if (cfs_b->runtime > 0) { | |
4623 | amount = min(cfs_b->runtime, min_amount); | |
4624 | cfs_b->runtime -= amount; | |
4625 | cfs_b->idle = 0; | |
4626 | } | |
ec12cb7f | 4627 | } |
ec12cb7f PT |
4628 | |
4629 | cfs_rq->runtime_remaining += amount; | |
85dac906 PT |
4630 | |
4631 | return cfs_rq->runtime_remaining > 0; | |
ec12cb7f PT |
4632 | } |
4633 | ||
e98fa02c PT |
4634 | /* returns 0 on failure to allocate runtime */ |
4635 | static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
4636 | { | |
4637 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); | |
4638 | int ret; | |
4639 | ||
4640 | raw_spin_lock(&cfs_b->lock); | |
4641 | ret = __assign_cfs_rq_runtime(cfs_b, cfs_rq, sched_cfs_bandwidth_slice()); | |
4642 | raw_spin_unlock(&cfs_b->lock); | |
4643 | ||
4644 | return ret; | |
4645 | } | |
4646 | ||
9dbdb155 | 4647 | static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec) |
a9cf55b2 PT |
4648 | { |
4649 | /* dock delta_exec before expiring quota (as it could span periods) */ | |
ec12cb7f | 4650 | cfs_rq->runtime_remaining -= delta_exec; |
a9cf55b2 PT |
4651 | |
4652 | if (likely(cfs_rq->runtime_remaining > 0)) | |
ec12cb7f PT |
4653 | return; |
4654 | ||
5e2d2cc2 L |
4655 | if (cfs_rq->throttled) |
4656 | return; | |
85dac906 PT |
4657 | /* |
4658 | * if we're unable to extend our runtime we resched so that the active | |
4659 | * hierarchy can be throttled | |
4660 | */ | |
4661 | if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr)) | |
8875125e | 4662 | resched_curr(rq_of(cfs_rq)); |
ec12cb7f PT |
4663 | } |
4664 | ||
6c16a6dc | 4665 | static __always_inline |
9dbdb155 | 4666 | void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec) |
ec12cb7f | 4667 | { |
56f570e5 | 4668 | if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled) |
ec12cb7f PT |
4669 | return; |
4670 | ||
4671 | __account_cfs_rq_runtime(cfs_rq, delta_exec); | |
4672 | } | |
4673 | ||
85dac906 PT |
4674 | static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq) |
4675 | { | |
56f570e5 | 4676 | return cfs_bandwidth_used() && cfs_rq->throttled; |
85dac906 PT |
4677 | } |
4678 | ||
64660c86 PT |
4679 | /* check whether cfs_rq, or any parent, is throttled */ |
4680 | static inline int throttled_hierarchy(struct cfs_rq *cfs_rq) | |
4681 | { | |
56f570e5 | 4682 | return cfs_bandwidth_used() && cfs_rq->throttle_count; |
64660c86 PT |
4683 | } |
4684 | ||
4685 | /* | |
4686 | * Ensure that neither of the group entities corresponding to src_cpu or | |
4687 | * dest_cpu are members of a throttled hierarchy when performing group | |
4688 | * load-balance operations. | |
4689 | */ | |
4690 | static inline int throttled_lb_pair(struct task_group *tg, | |
4691 | int src_cpu, int dest_cpu) | |
4692 | { | |
4693 | struct cfs_rq *src_cfs_rq, *dest_cfs_rq; | |
4694 | ||
4695 | src_cfs_rq = tg->cfs_rq[src_cpu]; | |
4696 | dest_cfs_rq = tg->cfs_rq[dest_cpu]; | |
4697 | ||
4698 | return throttled_hierarchy(src_cfs_rq) || | |
4699 | throttled_hierarchy(dest_cfs_rq); | |
4700 | } | |
4701 | ||
64660c86 PT |
4702 | static int tg_unthrottle_up(struct task_group *tg, void *data) |
4703 | { | |
4704 | struct rq *rq = data; | |
4705 | struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; | |
4706 | ||
4707 | cfs_rq->throttle_count--; | |
64660c86 | 4708 | if (!cfs_rq->throttle_count) { |
78becc27 | 4709 | cfs_rq->throttled_clock_task_time += rq_clock_task(rq) - |
f1b17280 | 4710 | cfs_rq->throttled_clock_task; |
31bc6aea VG |
4711 | |
4712 | /* Add cfs_rq with already running entity in the list */ | |
4713 | if (cfs_rq->nr_running >= 1) | |
4714 | list_add_leaf_cfs_rq(cfs_rq); | |
64660c86 | 4715 | } |
64660c86 PT |
4716 | |
4717 | return 0; | |
4718 | } | |
4719 | ||
4720 | static int tg_throttle_down(struct task_group *tg, void *data) | |
4721 | { | |
4722 | struct rq *rq = data; | |
4723 | struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; | |
4724 | ||
82958366 | 4725 | /* group is entering throttled state, stop time */ |
31bc6aea | 4726 | if (!cfs_rq->throttle_count) { |
78becc27 | 4727 | cfs_rq->throttled_clock_task = rq_clock_task(rq); |
31bc6aea VG |
4728 | list_del_leaf_cfs_rq(cfs_rq); |
4729 | } | |
64660c86 PT |
4730 | cfs_rq->throttle_count++; |
4731 | ||
4732 | return 0; | |
4733 | } | |
4734 | ||
e98fa02c | 4735 | static bool throttle_cfs_rq(struct cfs_rq *cfs_rq) |
85dac906 PT |
4736 | { |
4737 | struct rq *rq = rq_of(cfs_rq); | |
4738 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); | |
4739 | struct sched_entity *se; | |
43e9f7f2 | 4740 | long task_delta, idle_task_delta, dequeue = 1; |
e98fa02c PT |
4741 | |
4742 | raw_spin_lock(&cfs_b->lock); | |
4743 | /* This will start the period timer if necessary */ | |
4744 | if (__assign_cfs_rq_runtime(cfs_b, cfs_rq, 1)) { | |
4745 | /* | |
4746 | * We have raced with bandwidth becoming available, and if we | |
4747 | * actually throttled the timer might not unthrottle us for an | |
4748 | * entire period. We additionally needed to make sure that any | |
4749 | * subsequent check_cfs_rq_runtime calls agree not to throttle | |
4750 | * us, as we may commit to do cfs put_prev+pick_next, so we ask | |
4751 | * for 1ns of runtime rather than just check cfs_b. | |
4752 | */ | |
4753 | dequeue = 0; | |
4754 | } else { | |
4755 | list_add_tail_rcu(&cfs_rq->throttled_list, | |
4756 | &cfs_b->throttled_cfs_rq); | |
4757 | } | |
4758 | raw_spin_unlock(&cfs_b->lock); | |
4759 | ||
4760 | if (!dequeue) | |
4761 | return false; /* Throttle no longer required. */ | |
85dac906 PT |
4762 | |
4763 | se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))]; | |
4764 | ||
f1b17280 | 4765 | /* freeze hierarchy runnable averages while throttled */ |
64660c86 PT |
4766 | rcu_read_lock(); |
4767 | walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq); | |
4768 | rcu_read_unlock(); | |
85dac906 PT |
4769 | |
4770 | task_delta = cfs_rq->h_nr_running; | |
43e9f7f2 | 4771 | idle_task_delta = cfs_rq->idle_h_nr_running; |
85dac906 PT |
4772 | for_each_sched_entity(se) { |
4773 | struct cfs_rq *qcfs_rq = cfs_rq_of(se); | |
4774 | /* throttled entity or throttle-on-deactivate */ | |
4775 | if (!se->on_rq) | |
4776 | break; | |
4777 | ||
6212437f | 4778 | if (dequeue) { |
85dac906 | 4779 | dequeue_entity(qcfs_rq, se, DEQUEUE_SLEEP); |
6212437f VG |
4780 | } else { |
4781 | update_load_avg(qcfs_rq, se, 0); | |
4782 | se_update_runnable(se); | |
4783 | } | |
4784 | ||
85dac906 | 4785 | qcfs_rq->h_nr_running -= task_delta; |
43e9f7f2 | 4786 | qcfs_rq->idle_h_nr_running -= idle_task_delta; |
85dac906 PT |
4787 | |
4788 | if (qcfs_rq->load.weight) | |
4789 | dequeue = 0; | |
4790 | } | |
4791 | ||
4792 | if (!se) | |
72465447 | 4793 | sub_nr_running(rq, task_delta); |
85dac906 | 4794 | |
c06f04c7 | 4795 | /* |
e98fa02c PT |
4796 | * Note: distribution will already see us throttled via the |
4797 | * throttled-list. rq->lock protects completion. | |
c06f04c7 | 4798 | */ |
e98fa02c PT |
4799 | cfs_rq->throttled = 1; |
4800 | cfs_rq->throttled_clock = rq_clock(rq); | |
4801 | return true; | |
85dac906 PT |
4802 | } |
4803 | ||
029632fb | 4804 | void unthrottle_cfs_rq(struct cfs_rq *cfs_rq) |
671fd9da PT |
4805 | { |
4806 | struct rq *rq = rq_of(cfs_rq); | |
4807 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); | |
4808 | struct sched_entity *se; | |
43e9f7f2 | 4809 | long task_delta, idle_task_delta; |
671fd9da | 4810 | |
22b958d8 | 4811 | se = cfs_rq->tg->se[cpu_of(rq)]; |
671fd9da PT |
4812 | |
4813 | cfs_rq->throttled = 0; | |
1a55af2e FW |
4814 | |
4815 | update_rq_clock(rq); | |
4816 | ||
671fd9da | 4817 | raw_spin_lock(&cfs_b->lock); |
78becc27 | 4818 | cfs_b->throttled_time += rq_clock(rq) - cfs_rq->throttled_clock; |
671fd9da PT |
4819 | list_del_rcu(&cfs_rq->throttled_list); |
4820 | raw_spin_unlock(&cfs_b->lock); | |
4821 | ||
64660c86 PT |
4822 | /* update hierarchical throttle state */ |
4823 | walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq); | |
4824 | ||
671fd9da PT |
4825 | if (!cfs_rq->load.weight) |
4826 | return; | |
4827 | ||
4828 | task_delta = cfs_rq->h_nr_running; | |
43e9f7f2 | 4829 | idle_task_delta = cfs_rq->idle_h_nr_running; |
671fd9da PT |
4830 | for_each_sched_entity(se) { |
4831 | if (se->on_rq) | |
39f23ce0 VG |
4832 | break; |
4833 | cfs_rq = cfs_rq_of(se); | |
4834 | enqueue_entity(cfs_rq, se, ENQUEUE_WAKEUP); | |
4835 | ||
4836 | cfs_rq->h_nr_running += task_delta; | |
4837 | cfs_rq->idle_h_nr_running += idle_task_delta; | |
4838 | ||
4839 | /* end evaluation on encountering a throttled cfs_rq */ | |
4840 | if (cfs_rq_throttled(cfs_rq)) | |
4841 | goto unthrottle_throttle; | |
4842 | } | |
671fd9da | 4843 | |
39f23ce0 | 4844 | for_each_sched_entity(se) { |
671fd9da | 4845 | cfs_rq = cfs_rq_of(se); |
39f23ce0 VG |
4846 | |
4847 | update_load_avg(cfs_rq, se, UPDATE_TG); | |
4848 | se_update_runnable(se); | |
6212437f | 4849 | |
671fd9da | 4850 | cfs_rq->h_nr_running += task_delta; |
43e9f7f2 | 4851 | cfs_rq->idle_h_nr_running += idle_task_delta; |
671fd9da | 4852 | |
39f23ce0 VG |
4853 | |
4854 | /* end evaluation on encountering a throttled cfs_rq */ | |
671fd9da | 4855 | if (cfs_rq_throttled(cfs_rq)) |
39f23ce0 VG |
4856 | goto unthrottle_throttle; |
4857 | ||
4858 | /* | |
4859 | * One parent has been throttled and cfs_rq removed from the | |
4860 | * list. Add it back to not break the leaf list. | |
4861 | */ | |
4862 | if (throttled_hierarchy(cfs_rq)) | |
4863 | list_add_leaf_cfs_rq(cfs_rq); | |
671fd9da PT |
4864 | } |
4865 | ||
39f23ce0 VG |
4866 | /* At this point se is NULL and we are at root level*/ |
4867 | add_nr_running(rq, task_delta); | |
671fd9da | 4868 | |
39f23ce0 | 4869 | unthrottle_throttle: |
fe61468b VG |
4870 | /* |
4871 | * The cfs_rq_throttled() breaks in the above iteration can result in | |
4872 | * incomplete leaf list maintenance, resulting in triggering the | |
4873 | * assertion below. | |
4874 | */ | |
4875 | for_each_sched_entity(se) { | |
4876 | cfs_rq = cfs_rq_of(se); | |
4877 | ||
39f23ce0 VG |
4878 | if (list_add_leaf_cfs_rq(cfs_rq)) |
4879 | break; | |
fe61468b VG |
4880 | } |
4881 | ||
4882 | assert_list_leaf_cfs_rq(rq); | |
4883 | ||
97fb7a0a | 4884 | /* Determine whether we need to wake up potentially idle CPU: */ |
671fd9da | 4885 | if (rq->curr == rq->idle && rq->cfs.nr_running) |
8875125e | 4886 | resched_curr(rq); |
671fd9da PT |
4887 | } |
4888 | ||
26a8b127 | 4889 | static void distribute_cfs_runtime(struct cfs_bandwidth *cfs_b) |
671fd9da PT |
4890 | { |
4891 | struct cfs_rq *cfs_rq; | |
26a8b127 | 4892 | u64 runtime, remaining = 1; |
671fd9da PT |
4893 | |
4894 | rcu_read_lock(); | |
4895 | list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq, | |
4896 | throttled_list) { | |
4897 | struct rq *rq = rq_of(cfs_rq); | |
8a8c69c3 | 4898 | struct rq_flags rf; |
671fd9da | 4899 | |
c0ad4aa4 | 4900 | rq_lock_irqsave(rq, &rf); |
671fd9da PT |
4901 | if (!cfs_rq_throttled(cfs_rq)) |
4902 | goto next; | |
4903 | ||
5e2d2cc2 L |
4904 | /* By the above check, this should never be true */ |
4905 | SCHED_WARN_ON(cfs_rq->runtime_remaining > 0); | |
4906 | ||
26a8b127 | 4907 | raw_spin_lock(&cfs_b->lock); |
671fd9da | 4908 | runtime = -cfs_rq->runtime_remaining + 1; |
26a8b127 HC |
4909 | if (runtime > cfs_b->runtime) |
4910 | runtime = cfs_b->runtime; | |
4911 | cfs_b->runtime -= runtime; | |
4912 | remaining = cfs_b->runtime; | |
4913 | raw_spin_unlock(&cfs_b->lock); | |
671fd9da PT |
4914 | |
4915 | cfs_rq->runtime_remaining += runtime; | |
671fd9da PT |
4916 | |
4917 | /* we check whether we're throttled above */ | |
4918 | if (cfs_rq->runtime_remaining > 0) | |
4919 | unthrottle_cfs_rq(cfs_rq); | |
4920 | ||
4921 | next: | |
c0ad4aa4 | 4922 | rq_unlock_irqrestore(rq, &rf); |
671fd9da PT |
4923 | |
4924 | if (!remaining) | |
4925 | break; | |
4926 | } | |
4927 | rcu_read_unlock(); | |
671fd9da PT |
4928 | } |
4929 | ||
58088ad0 PT |
4930 | /* |
4931 | * Responsible for refilling a task_group's bandwidth and unthrottling its | |
4932 | * cfs_rqs as appropriate. If there has been no activity within the last | |
4933 | * period the timer is deactivated until scheduling resumes; cfs_b->idle is | |
4934 | * used to track this state. | |
4935 | */ | |
c0ad4aa4 | 4936 | static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun, unsigned long flags) |
58088ad0 | 4937 | { |
51f2176d | 4938 | int throttled; |
58088ad0 | 4939 | |
58088ad0 PT |
4940 | /* no need to continue the timer with no bandwidth constraint */ |
4941 | if (cfs_b->quota == RUNTIME_INF) | |
51f2176d | 4942 | goto out_deactivate; |
58088ad0 | 4943 | |
671fd9da | 4944 | throttled = !list_empty(&cfs_b->throttled_cfs_rq); |
e8da1b18 | 4945 | cfs_b->nr_periods += overrun; |
671fd9da | 4946 | |
51f2176d BS |
4947 | /* |
4948 | * idle depends on !throttled (for the case of a large deficit), and if | |
4949 | * we're going inactive then everything else can be deferred | |
4950 | */ | |
4951 | if (cfs_b->idle && !throttled) | |
4952 | goto out_deactivate; | |
a9cf55b2 PT |
4953 | |
4954 | __refill_cfs_bandwidth_runtime(cfs_b); | |
4955 | ||
671fd9da PT |
4956 | if (!throttled) { |
4957 | /* mark as potentially idle for the upcoming period */ | |
4958 | cfs_b->idle = 1; | |
51f2176d | 4959 | return 0; |
671fd9da PT |
4960 | } |
4961 | ||
e8da1b18 NR |
4962 | /* account preceding periods in which throttling occurred */ |
4963 | cfs_b->nr_throttled += overrun; | |
4964 | ||
671fd9da | 4965 | /* |
26a8b127 | 4966 | * This check is repeated as we release cfs_b->lock while we unthrottle. |
671fd9da | 4967 | */ |
ab93a4bc | 4968 | while (throttled && cfs_b->runtime > 0) { |
c0ad4aa4 | 4969 | raw_spin_unlock_irqrestore(&cfs_b->lock, flags); |
671fd9da | 4970 | /* we can't nest cfs_b->lock while distributing bandwidth */ |
26a8b127 | 4971 | distribute_cfs_runtime(cfs_b); |
c0ad4aa4 | 4972 | raw_spin_lock_irqsave(&cfs_b->lock, flags); |
671fd9da PT |
4973 | |
4974 | throttled = !list_empty(&cfs_b->throttled_cfs_rq); | |
4975 | } | |
58088ad0 | 4976 | |
671fd9da PT |
4977 | /* |
4978 | * While we are ensured activity in the period following an | |
4979 | * unthrottle, this also covers the case in which the new bandwidth is | |
4980 | * insufficient to cover the existing bandwidth deficit. (Forcing the | |
4981 | * timer to remain active while there are any throttled entities.) | |
4982 | */ | |
4983 | cfs_b->idle = 0; | |
58088ad0 | 4984 | |
51f2176d BS |
4985 | return 0; |
4986 | ||
4987 | out_deactivate: | |
51f2176d | 4988 | return 1; |
58088ad0 | 4989 | } |
d3d9dc33 | 4990 | |
d8b4986d PT |
4991 | /* a cfs_rq won't donate quota below this amount */ |
4992 | static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC; | |
4993 | /* minimum remaining period time to redistribute slack quota */ | |
4994 | static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC; | |
4995 | /* how long we wait to gather additional slack before distributing */ | |
4996 | static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC; | |
4997 | ||
db06e78c BS |
4998 | /* |
4999 | * Are we near the end of the current quota period? | |
5000 | * | |
5001 | * Requires cfs_b->lock for hrtimer_expires_remaining to be safe against the | |
4961b6e1 | 5002 | * hrtimer base being cleared by hrtimer_start. In the case of |
db06e78c BS |
5003 | * migrate_hrtimers, base is never cleared, so we are fine. |
5004 | */ | |
d8b4986d PT |
5005 | static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire) |
5006 | { | |
5007 | struct hrtimer *refresh_timer = &cfs_b->period_timer; | |
5008 | u64 remaining; | |
5009 | ||
5010 | /* if the call-back is running a quota refresh is already occurring */ | |
5011 | if (hrtimer_callback_running(refresh_timer)) | |
5012 | return 1; | |
5013 | ||
5014 | /* is a quota refresh about to occur? */ | |
5015 | remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer)); | |
5016 | if (remaining < min_expire) | |
5017 | return 1; | |
5018 | ||
5019 | return 0; | |
5020 | } | |
5021 | ||
5022 | static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b) | |
5023 | { | |
5024 | u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration; | |
5025 | ||
5026 | /* if there's a quota refresh soon don't bother with slack */ | |
5027 | if (runtime_refresh_within(cfs_b, min_left)) | |
5028 | return; | |
5029 | ||
66567fcb | 5030 | /* don't push forwards an existing deferred unthrottle */ |
5031 | if (cfs_b->slack_started) | |
5032 | return; | |
5033 | cfs_b->slack_started = true; | |
5034 | ||
4cfafd30 PZ |
5035 | hrtimer_start(&cfs_b->slack_timer, |
5036 | ns_to_ktime(cfs_bandwidth_slack_period), | |
5037 | HRTIMER_MODE_REL); | |
d8b4986d PT |
5038 | } |
5039 | ||
5040 | /* we know any runtime found here is valid as update_curr() precedes return */ | |
5041 | static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
5042 | { | |
5043 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); | |
5044 | s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime; | |
5045 | ||
5046 | if (slack_runtime <= 0) | |
5047 | return; | |
5048 | ||
5049 | raw_spin_lock(&cfs_b->lock); | |
de53fd7a | 5050 | if (cfs_b->quota != RUNTIME_INF) { |
d8b4986d PT |
5051 | cfs_b->runtime += slack_runtime; |
5052 | ||
5053 | /* we are under rq->lock, defer unthrottling using a timer */ | |
5054 | if (cfs_b->runtime > sched_cfs_bandwidth_slice() && | |
5055 | !list_empty(&cfs_b->throttled_cfs_rq)) | |
5056 | start_cfs_slack_bandwidth(cfs_b); | |
5057 | } | |
5058 | raw_spin_unlock(&cfs_b->lock); | |
5059 | ||
5060 | /* even if it's not valid for return we don't want to try again */ | |
5061 | cfs_rq->runtime_remaining -= slack_runtime; | |
5062 | } | |
5063 | ||
5064 | static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
5065 | { | |
56f570e5 PT |
5066 | if (!cfs_bandwidth_used()) |
5067 | return; | |
5068 | ||
fccfdc6f | 5069 | if (!cfs_rq->runtime_enabled || cfs_rq->nr_running) |
d8b4986d PT |
5070 | return; |
5071 | ||
5072 | __return_cfs_rq_runtime(cfs_rq); | |
5073 | } | |
5074 | ||
5075 | /* | |
5076 | * This is done with a timer (instead of inline with bandwidth return) since | |
5077 | * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs. | |
5078 | */ | |
5079 | static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b) | |
5080 | { | |
5081 | u64 runtime = 0, slice = sched_cfs_bandwidth_slice(); | |
c0ad4aa4 | 5082 | unsigned long flags; |
d8b4986d PT |
5083 | |
5084 | /* confirm we're still not at a refresh boundary */ | |
c0ad4aa4 | 5085 | raw_spin_lock_irqsave(&cfs_b->lock, flags); |
66567fcb | 5086 | cfs_b->slack_started = false; |
baa9be4f | 5087 | |
db06e78c | 5088 | if (runtime_refresh_within(cfs_b, min_bandwidth_expiration)) { |
c0ad4aa4 | 5089 | raw_spin_unlock_irqrestore(&cfs_b->lock, flags); |
d8b4986d | 5090 | return; |
db06e78c | 5091 | } |
d8b4986d | 5092 | |
c06f04c7 | 5093 | if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) |
d8b4986d | 5094 | runtime = cfs_b->runtime; |
c06f04c7 | 5095 | |
c0ad4aa4 | 5096 | raw_spin_unlock_irqrestore(&cfs_b->lock, flags); |
d8b4986d PT |
5097 | |
5098 | if (!runtime) | |
5099 | return; | |
5100 | ||
26a8b127 | 5101 | distribute_cfs_runtime(cfs_b); |
d8b4986d | 5102 | |
c0ad4aa4 | 5103 | raw_spin_lock_irqsave(&cfs_b->lock, flags); |
c0ad4aa4 | 5104 | raw_spin_unlock_irqrestore(&cfs_b->lock, flags); |
d8b4986d PT |
5105 | } |
5106 | ||
d3d9dc33 PT |
5107 | /* |
5108 | * When a group wakes up we want to make sure that its quota is not already | |
5109 | * expired/exceeded, otherwise it may be allowed to steal additional ticks of | |
5110 | * runtime as update_curr() throttling can not not trigger until it's on-rq. | |
5111 | */ | |
5112 | static void check_enqueue_throttle(struct cfs_rq *cfs_rq) | |
5113 | { | |
56f570e5 PT |
5114 | if (!cfs_bandwidth_used()) |
5115 | return; | |
5116 | ||
d3d9dc33 PT |
5117 | /* an active group must be handled by the update_curr()->put() path */ |
5118 | if (!cfs_rq->runtime_enabled || cfs_rq->curr) | |
5119 | return; | |
5120 | ||
5121 | /* ensure the group is not already throttled */ | |
5122 | if (cfs_rq_throttled(cfs_rq)) | |
5123 | return; | |
5124 | ||
5125 | /* update runtime allocation */ | |
5126 | account_cfs_rq_runtime(cfs_rq, 0); | |
5127 | if (cfs_rq->runtime_remaining <= 0) | |
5128 | throttle_cfs_rq(cfs_rq); | |
5129 | } | |
5130 | ||
55e16d30 PZ |
5131 | static void sync_throttle(struct task_group *tg, int cpu) |
5132 | { | |
5133 | struct cfs_rq *pcfs_rq, *cfs_rq; | |
5134 | ||
5135 | if (!cfs_bandwidth_used()) | |
5136 | return; | |
5137 | ||
5138 | if (!tg->parent) | |
5139 | return; | |
5140 | ||
5141 | cfs_rq = tg->cfs_rq[cpu]; | |
5142 | pcfs_rq = tg->parent->cfs_rq[cpu]; | |
5143 | ||
5144 | cfs_rq->throttle_count = pcfs_rq->throttle_count; | |
b8922125 | 5145 | cfs_rq->throttled_clock_task = rq_clock_task(cpu_rq(cpu)); |
55e16d30 PZ |
5146 | } |
5147 | ||
d3d9dc33 | 5148 | /* conditionally throttle active cfs_rq's from put_prev_entity() */ |
678d5718 | 5149 | static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq) |
d3d9dc33 | 5150 | { |
56f570e5 | 5151 | if (!cfs_bandwidth_used()) |
678d5718 | 5152 | return false; |
56f570e5 | 5153 | |
d3d9dc33 | 5154 | if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0)) |
678d5718 | 5155 | return false; |
d3d9dc33 PT |
5156 | |
5157 | /* | |
5158 | * it's possible for a throttled entity to be forced into a running | |
5159 | * state (e.g. set_curr_task), in this case we're finished. | |
5160 | */ | |
5161 | if (cfs_rq_throttled(cfs_rq)) | |
678d5718 | 5162 | return true; |
d3d9dc33 | 5163 | |
e98fa02c | 5164 | return throttle_cfs_rq(cfs_rq); |
d3d9dc33 | 5165 | } |
029632fb | 5166 | |
029632fb PZ |
5167 | static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer) |
5168 | { | |
5169 | struct cfs_bandwidth *cfs_b = | |
5170 | container_of(timer, struct cfs_bandwidth, slack_timer); | |
77a4d1a1 | 5171 | |
029632fb PZ |
5172 | do_sched_cfs_slack_timer(cfs_b); |
5173 | ||
5174 | return HRTIMER_NORESTART; | |
5175 | } | |
5176 | ||
2e8e1922 PA |
5177 | extern const u64 max_cfs_quota_period; |
5178 | ||
029632fb PZ |
5179 | static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer) |
5180 | { | |
5181 | struct cfs_bandwidth *cfs_b = | |
5182 | container_of(timer, struct cfs_bandwidth, period_timer); | |
c0ad4aa4 | 5183 | unsigned long flags; |
029632fb PZ |
5184 | int overrun; |
5185 | int idle = 0; | |
2e8e1922 | 5186 | int count = 0; |
029632fb | 5187 | |
c0ad4aa4 | 5188 | raw_spin_lock_irqsave(&cfs_b->lock, flags); |
029632fb | 5189 | for (;;) { |
77a4d1a1 | 5190 | overrun = hrtimer_forward_now(timer, cfs_b->period); |
029632fb PZ |
5191 | if (!overrun) |
5192 | break; | |
5193 | ||
5a6d6a6c HC |
5194 | idle = do_sched_cfs_period_timer(cfs_b, overrun, flags); |
5195 | ||
2e8e1922 PA |
5196 | if (++count > 3) { |
5197 | u64 new, old = ktime_to_ns(cfs_b->period); | |
5198 | ||
4929a4e6 XZ |
5199 | /* |
5200 | * Grow period by a factor of 2 to avoid losing precision. | |
5201 | * Precision loss in the quota/period ratio can cause __cfs_schedulable | |
5202 | * to fail. | |
5203 | */ | |
5204 | new = old * 2; | |
5205 | if (new < max_cfs_quota_period) { | |
5206 | cfs_b->period = ns_to_ktime(new); | |
5207 | cfs_b->quota *= 2; | |
5208 | ||
5209 | pr_warn_ratelimited( | |
5210 | "cfs_period_timer[cpu%d]: period too short, scaling up (new cfs_period_us = %lld, cfs_quota_us = %lld)\n", | |
5211 | smp_processor_id(), | |
5212 | div_u64(new, NSEC_PER_USEC), | |
5213 | div_u64(cfs_b->quota, NSEC_PER_USEC)); | |
5214 | } else { | |
5215 | pr_warn_ratelimited( | |
5216 | "cfs_period_timer[cpu%d]: period too short, but cannot scale up without losing precision (cfs_period_us = %lld, cfs_quota_us = %lld)\n", | |
5217 | smp_processor_id(), | |
5218 | div_u64(old, NSEC_PER_USEC), | |
5219 | div_u64(cfs_b->quota, NSEC_PER_USEC)); | |
5220 | } | |
2e8e1922 PA |
5221 | |
5222 | /* reset count so we don't come right back in here */ | |
5223 | count = 0; | |
5224 | } | |
029632fb | 5225 | } |
4cfafd30 PZ |
5226 | if (idle) |
5227 | cfs_b->period_active = 0; | |
c0ad4aa4 | 5228 | raw_spin_unlock_irqrestore(&cfs_b->lock, flags); |
029632fb PZ |
5229 | |
5230 | return idle ? HRTIMER_NORESTART : HRTIMER_RESTART; | |
5231 | } | |
5232 | ||
5233 | void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) | |
5234 | { | |
5235 | raw_spin_lock_init(&cfs_b->lock); | |
5236 | cfs_b->runtime = 0; | |
5237 | cfs_b->quota = RUNTIME_INF; | |
5238 | cfs_b->period = ns_to_ktime(default_cfs_period()); | |
5239 | ||
5240 | INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq); | |
4cfafd30 | 5241 | hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED); |
029632fb PZ |
5242 | cfs_b->period_timer.function = sched_cfs_period_timer; |
5243 | hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); | |
5244 | cfs_b->slack_timer.function = sched_cfs_slack_timer; | |
66567fcb | 5245 | cfs_b->slack_started = false; |
029632fb PZ |
5246 | } |
5247 | ||
5248 | static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
5249 | { | |
5250 | cfs_rq->runtime_enabled = 0; | |
5251 | INIT_LIST_HEAD(&cfs_rq->throttled_list); | |
5252 | } | |
5253 | ||
77a4d1a1 | 5254 | void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b) |
029632fb | 5255 | { |
4cfafd30 | 5256 | lockdep_assert_held(&cfs_b->lock); |
029632fb | 5257 | |
f1d1be8a XP |
5258 | if (cfs_b->period_active) |
5259 | return; | |
5260 | ||
5261 | cfs_b->period_active = 1; | |
763a9ec0 | 5262 | hrtimer_forward_now(&cfs_b->period_timer, cfs_b->period); |
f1d1be8a | 5263 | hrtimer_start_expires(&cfs_b->period_timer, HRTIMER_MODE_ABS_PINNED); |
029632fb PZ |
5264 | } |
5265 | ||
5266 | static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) | |
5267 | { | |
7f1a169b TH |
5268 | /* init_cfs_bandwidth() was not called */ |
5269 | if (!cfs_b->throttled_cfs_rq.next) | |
5270 | return; | |
5271 | ||
029632fb PZ |
5272 | hrtimer_cancel(&cfs_b->period_timer); |
5273 | hrtimer_cancel(&cfs_b->slack_timer); | |
5274 | } | |
5275 | ||
502ce005 | 5276 | /* |
97fb7a0a | 5277 | * Both these CPU hotplug callbacks race against unregister_fair_sched_group() |
502ce005 PZ |
5278 | * |
5279 | * The race is harmless, since modifying bandwidth settings of unhooked group | |
5280 | * bits doesn't do much. | |
5281 | */ | |
5282 | ||
5283 | /* cpu online calback */ | |
0e59bdae KT |
5284 | static void __maybe_unused update_runtime_enabled(struct rq *rq) |
5285 | { | |
502ce005 | 5286 | struct task_group *tg; |
0e59bdae | 5287 | |
502ce005 PZ |
5288 | lockdep_assert_held(&rq->lock); |
5289 | ||
5290 | rcu_read_lock(); | |
5291 | list_for_each_entry_rcu(tg, &task_groups, list) { | |
5292 | struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth; | |
5293 | struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; | |
0e59bdae KT |
5294 | |
5295 | raw_spin_lock(&cfs_b->lock); | |
5296 | cfs_rq->runtime_enabled = cfs_b->quota != RUNTIME_INF; | |
5297 | raw_spin_unlock(&cfs_b->lock); | |
5298 | } | |
502ce005 | 5299 | rcu_read_unlock(); |
0e59bdae KT |
5300 | } |
5301 | ||
502ce005 | 5302 | /* cpu offline callback */ |
38dc3348 | 5303 | static void __maybe_unused unthrottle_offline_cfs_rqs(struct rq *rq) |
029632fb | 5304 | { |
502ce005 PZ |
5305 | struct task_group *tg; |
5306 | ||
5307 | lockdep_assert_held(&rq->lock); | |
5308 | ||
5309 | rcu_read_lock(); | |
5310 | list_for_each_entry_rcu(tg, &task_groups, list) { | |
5311 | struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; | |
029632fb | 5312 | |
029632fb PZ |
5313 | if (!cfs_rq->runtime_enabled) |
5314 | continue; | |
5315 | ||
5316 | /* | |
5317 | * clock_task is not advancing so we just need to make sure | |
5318 | * there's some valid quota amount | |
5319 | */ | |
51f2176d | 5320 | cfs_rq->runtime_remaining = 1; |
0e59bdae | 5321 | /* |
97fb7a0a | 5322 | * Offline rq is schedulable till CPU is completely disabled |
0e59bdae KT |
5323 | * in take_cpu_down(), so we prevent new cfs throttling here. |
5324 | */ | |
5325 | cfs_rq->runtime_enabled = 0; | |
5326 | ||
029632fb PZ |
5327 | if (cfs_rq_throttled(cfs_rq)) |
5328 | unthrottle_cfs_rq(cfs_rq); | |
5329 | } | |
502ce005 | 5330 | rcu_read_unlock(); |
029632fb PZ |
5331 | } |
5332 | ||
5333 | #else /* CONFIG_CFS_BANDWIDTH */ | |
f6783319 VG |
5334 | |
5335 | static inline bool cfs_bandwidth_used(void) | |
5336 | { | |
5337 | return false; | |
5338 | } | |
5339 | ||
9dbdb155 | 5340 | static void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec) {} |
678d5718 | 5341 | static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq) { return false; } |
d3d9dc33 | 5342 | static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {} |
55e16d30 | 5343 | static inline void sync_throttle(struct task_group *tg, int cpu) {} |
6c16a6dc | 5344 | static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {} |
85dac906 PT |
5345 | |
5346 | static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq) | |
5347 | { | |
5348 | return 0; | |
5349 | } | |
64660c86 PT |
5350 | |
5351 | static inline int throttled_hierarchy(struct cfs_rq *cfs_rq) | |
5352 | { | |
5353 | return 0; | |
5354 | } | |
5355 | ||
5356 | static inline int throttled_lb_pair(struct task_group *tg, | |
5357 | int src_cpu, int dest_cpu) | |
5358 | { | |
5359 | return 0; | |
5360 | } | |
029632fb PZ |
5361 | |
5362 | void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {} | |
5363 | ||
5364 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
5365 | static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {} | |
ab84d31e PT |
5366 | #endif |
5367 | ||
029632fb PZ |
5368 | static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg) |
5369 | { | |
5370 | return NULL; | |
5371 | } | |
5372 | static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {} | |
0e59bdae | 5373 | static inline void update_runtime_enabled(struct rq *rq) {} |
a4c96ae3 | 5374 | static inline void unthrottle_offline_cfs_rqs(struct rq *rq) {} |
029632fb PZ |
5375 | |
5376 | #endif /* CONFIG_CFS_BANDWIDTH */ | |
5377 | ||
bf0f6f24 IM |
5378 | /************************************************** |
5379 | * CFS operations on tasks: | |
5380 | */ | |
5381 | ||
8f4d37ec PZ |
5382 | #ifdef CONFIG_SCHED_HRTICK |
5383 | static void hrtick_start_fair(struct rq *rq, struct task_struct *p) | |
5384 | { | |
8f4d37ec PZ |
5385 | struct sched_entity *se = &p->se; |
5386 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
5387 | ||
9148a3a1 | 5388 | SCHED_WARN_ON(task_rq(p) != rq); |
8f4d37ec | 5389 | |
8bf46a39 | 5390 | if (rq->cfs.h_nr_running > 1) { |
8f4d37ec PZ |
5391 | u64 slice = sched_slice(cfs_rq, se); |
5392 | u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime; | |
5393 | s64 delta = slice - ran; | |
5394 | ||
5395 | if (delta < 0) { | |
5396 | if (rq->curr == p) | |
8875125e | 5397 | resched_curr(rq); |
8f4d37ec PZ |
5398 | return; |
5399 | } | |
31656519 | 5400 | hrtick_start(rq, delta); |
8f4d37ec PZ |
5401 | } |
5402 | } | |
a4c2f00f PZ |
5403 | |
5404 | /* | |
5405 | * called from enqueue/dequeue and updates the hrtick when the | |
5406 | * current task is from our class and nr_running is low enough | |
5407 | * to matter. | |
5408 | */ | |
5409 | static void hrtick_update(struct rq *rq) | |
5410 | { | |
5411 | struct task_struct *curr = rq->curr; | |
5412 | ||
b39e66ea | 5413 | if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class) |
a4c2f00f PZ |
5414 | return; |
5415 | ||
5416 | if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency) | |
5417 | hrtick_start_fair(rq, curr); | |
5418 | } | |
55e12e5e | 5419 | #else /* !CONFIG_SCHED_HRTICK */ |
8f4d37ec PZ |
5420 | static inline void |
5421 | hrtick_start_fair(struct rq *rq, struct task_struct *p) | |
5422 | { | |
5423 | } | |
a4c2f00f PZ |
5424 | |
5425 | static inline void hrtick_update(struct rq *rq) | |
5426 | { | |
5427 | } | |
8f4d37ec PZ |
5428 | #endif |
5429 | ||
2802bf3c MR |
5430 | #ifdef CONFIG_SMP |
5431 | static inline unsigned long cpu_util(int cpu); | |
2802bf3c MR |
5432 | |
5433 | static inline bool cpu_overutilized(int cpu) | |
5434 | { | |
60e17f5c | 5435 | return !fits_capacity(cpu_util(cpu), capacity_of(cpu)); |
2802bf3c MR |
5436 | } |
5437 | ||
5438 | static inline void update_overutilized_status(struct rq *rq) | |
5439 | { | |
f9f240f9 | 5440 | if (!READ_ONCE(rq->rd->overutilized) && cpu_overutilized(rq->cpu)) { |
2802bf3c | 5441 | WRITE_ONCE(rq->rd->overutilized, SG_OVERUTILIZED); |
f9f240f9 QY |
5442 | trace_sched_overutilized_tp(rq->rd, SG_OVERUTILIZED); |
5443 | } | |
2802bf3c MR |
5444 | } |
5445 | #else | |
5446 | static inline void update_overutilized_status(struct rq *rq) { } | |
5447 | #endif | |
5448 | ||
323af6de VK |
5449 | /* Runqueue only has SCHED_IDLE tasks enqueued */ |
5450 | static int sched_idle_rq(struct rq *rq) | |
5451 | { | |
5452 | return unlikely(rq->nr_running == rq->cfs.idle_h_nr_running && | |
5453 | rq->nr_running); | |
5454 | } | |
5455 | ||
afa70d94 | 5456 | #ifdef CONFIG_SMP |
323af6de VK |
5457 | static int sched_idle_cpu(int cpu) |
5458 | { | |
5459 | return sched_idle_rq(cpu_rq(cpu)); | |
5460 | } | |
afa70d94 | 5461 | #endif |
323af6de | 5462 | |
bf0f6f24 IM |
5463 | /* |
5464 | * The enqueue_task method is called before nr_running is | |
5465 | * increased. Here we update the fair scheduling stats and | |
5466 | * then put the task into the rbtree: | |
5467 | */ | |
ea87bb78 | 5468 | static void |
371fd7e7 | 5469 | enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags) |
bf0f6f24 IM |
5470 | { |
5471 | struct cfs_rq *cfs_rq; | |
62fb1851 | 5472 | struct sched_entity *se = &p->se; |
43e9f7f2 | 5473 | int idle_h_nr_running = task_has_idle_policy(p); |
bf0f6f24 | 5474 | |
2539fc82 PB |
5475 | /* |
5476 | * The code below (indirectly) updates schedutil which looks at | |
5477 | * the cfs_rq utilization to select a frequency. | |
5478 | * Let's add the task's estimated utilization to the cfs_rq's | |
5479 | * estimated utilization, before we update schedutil. | |
5480 | */ | |
5481 | util_est_enqueue(&rq->cfs, p); | |
5482 | ||
8c34ab19 RW |
5483 | /* |
5484 | * If in_iowait is set, the code below may not trigger any cpufreq | |
5485 | * utilization updates, so do it here explicitly with the IOWAIT flag | |
5486 | * passed. | |
5487 | */ | |
5488 | if (p->in_iowait) | |
674e7541 | 5489 | cpufreq_update_util(rq, SCHED_CPUFREQ_IOWAIT); |
8c34ab19 | 5490 | |
bf0f6f24 | 5491 | for_each_sched_entity(se) { |
62fb1851 | 5492 | if (se->on_rq) |
bf0f6f24 IM |
5493 | break; |
5494 | cfs_rq = cfs_rq_of(se); | |
88ec22d3 | 5495 | enqueue_entity(cfs_rq, se, flags); |
85dac906 | 5496 | |
953bfcd1 | 5497 | cfs_rq->h_nr_running++; |
43e9f7f2 | 5498 | cfs_rq->idle_h_nr_running += idle_h_nr_running; |
85dac906 | 5499 | |
6d4d2246 VG |
5500 | /* end evaluation on encountering a throttled cfs_rq */ |
5501 | if (cfs_rq_throttled(cfs_rq)) | |
5502 | goto enqueue_throttle; | |
5503 | ||
88ec22d3 | 5504 | flags = ENQUEUE_WAKEUP; |
bf0f6f24 | 5505 | } |
8f4d37ec | 5506 | |
2069dd75 | 5507 | for_each_sched_entity(se) { |
0f317143 | 5508 | cfs_rq = cfs_rq_of(se); |
2069dd75 | 5509 | |
88c0616e | 5510 | update_load_avg(cfs_rq, se, UPDATE_TG); |
9f683953 | 5511 | se_update_runnable(se); |
1ea6c46a | 5512 | update_cfs_group(se); |
6d4d2246 VG |
5513 | |
5514 | cfs_rq->h_nr_running++; | |
5515 | cfs_rq->idle_h_nr_running += idle_h_nr_running; | |
5ab297ba VG |
5516 | |
5517 | /* end evaluation on encountering a throttled cfs_rq */ | |
5518 | if (cfs_rq_throttled(cfs_rq)) | |
5519 | goto enqueue_throttle; | |
b34cb07d PA |
5520 | |
5521 | /* | |
5522 | * One parent has been throttled and cfs_rq removed from the | |
5523 | * list. Add it back to not break the leaf list. | |
5524 | */ | |
5525 | if (throttled_hierarchy(cfs_rq)) | |
5526 | list_add_leaf_cfs_rq(cfs_rq); | |
2069dd75 PZ |
5527 | } |
5528 | ||
7d148be6 VG |
5529 | /* At this point se is NULL and we are at root level*/ |
5530 | add_nr_running(rq, 1); | |
2802bf3c | 5531 | |
7d148be6 VG |
5532 | /* |
5533 | * Since new tasks are assigned an initial util_avg equal to | |
5534 | * half of the spare capacity of their CPU, tiny tasks have the | |
5535 | * ability to cross the overutilized threshold, which will | |
5536 | * result in the load balancer ruining all the task placement | |
5537 | * done by EAS. As a way to mitigate that effect, do not account | |
5538 | * for the first enqueue operation of new tasks during the | |
5539 | * overutilized flag detection. | |
5540 | * | |
5541 | * A better way of solving this problem would be to wait for | |
5542 | * the PELT signals of tasks to converge before taking them | |
5543 | * into account, but that is not straightforward to implement, | |
5544 | * and the following generally works well enough in practice. | |
5545 | */ | |
5546 | if (flags & ENQUEUE_WAKEUP) | |
5547 | update_overutilized_status(rq); | |
cd126afe | 5548 | |
7d148be6 | 5549 | enqueue_throttle: |
f6783319 VG |
5550 | if (cfs_bandwidth_used()) { |
5551 | /* | |
5552 | * When bandwidth control is enabled; the cfs_rq_throttled() | |
5553 | * breaks in the above iteration can result in incomplete | |
5554 | * leaf list maintenance, resulting in triggering the assertion | |
5555 | * below. | |
5556 | */ | |
5557 | for_each_sched_entity(se) { | |
5558 | cfs_rq = cfs_rq_of(se); | |
5559 | ||
5560 | if (list_add_leaf_cfs_rq(cfs_rq)) | |
5561 | break; | |
5562 | } | |
5563 | } | |
5564 | ||
5d299eab PZ |
5565 | assert_list_leaf_cfs_rq(rq); |
5566 | ||
a4c2f00f | 5567 | hrtick_update(rq); |
bf0f6f24 IM |
5568 | } |
5569 | ||
2f36825b VP |
5570 | static void set_next_buddy(struct sched_entity *se); |
5571 | ||
bf0f6f24 IM |
5572 | /* |
5573 | * The dequeue_task method is called before nr_running is | |
5574 | * decreased. We remove the task from the rbtree and | |
5575 | * update the fair scheduling stats: | |
5576 | */ | |
371fd7e7 | 5577 | static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags) |
bf0f6f24 IM |
5578 | { |
5579 | struct cfs_rq *cfs_rq; | |
62fb1851 | 5580 | struct sched_entity *se = &p->se; |
2f36825b | 5581 | int task_sleep = flags & DEQUEUE_SLEEP; |
43e9f7f2 | 5582 | int idle_h_nr_running = task_has_idle_policy(p); |
323af6de | 5583 | bool was_sched_idle = sched_idle_rq(rq); |
bf0f6f24 IM |
5584 | |
5585 | for_each_sched_entity(se) { | |
5586 | cfs_rq = cfs_rq_of(se); | |
371fd7e7 | 5587 | dequeue_entity(cfs_rq, se, flags); |
85dac906 | 5588 | |
953bfcd1 | 5589 | cfs_rq->h_nr_running--; |
43e9f7f2 | 5590 | cfs_rq->idle_h_nr_running -= idle_h_nr_running; |
2069dd75 | 5591 | |
6d4d2246 VG |
5592 | /* end evaluation on encountering a throttled cfs_rq */ |
5593 | if (cfs_rq_throttled(cfs_rq)) | |
5594 | goto dequeue_throttle; | |
5595 | ||
bf0f6f24 | 5596 | /* Don't dequeue parent if it has other entities besides us */ |
2f36825b | 5597 | if (cfs_rq->load.weight) { |
754bd598 KK |
5598 | /* Avoid re-evaluating load for this entity: */ |
5599 | se = parent_entity(se); | |
2f36825b VP |
5600 | /* |
5601 | * Bias pick_next to pick a task from this cfs_rq, as | |
5602 | * p is sleeping when it is within its sched_slice. | |
5603 | */ | |
754bd598 KK |
5604 | if (task_sleep && se && !throttled_hierarchy(cfs_rq)) |
5605 | set_next_buddy(se); | |
bf0f6f24 | 5606 | break; |
2f36825b | 5607 | } |
371fd7e7 | 5608 | flags |= DEQUEUE_SLEEP; |
bf0f6f24 | 5609 | } |
8f4d37ec | 5610 | |
2069dd75 | 5611 | for_each_sched_entity(se) { |
0f317143 | 5612 | cfs_rq = cfs_rq_of(se); |
2069dd75 | 5613 | |
88c0616e | 5614 | update_load_avg(cfs_rq, se, UPDATE_TG); |
9f683953 | 5615 | se_update_runnable(se); |
1ea6c46a | 5616 | update_cfs_group(se); |
6d4d2246 VG |
5617 | |
5618 | cfs_rq->h_nr_running--; | |
5619 | cfs_rq->idle_h_nr_running -= idle_h_nr_running; | |
5ab297ba VG |
5620 | |
5621 | /* end evaluation on encountering a throttled cfs_rq */ | |
5622 | if (cfs_rq_throttled(cfs_rq)) | |
5623 | goto dequeue_throttle; | |
5624 | ||
2069dd75 PZ |
5625 | } |
5626 | ||
6d4d2246 | 5627 | dequeue_throttle: |
cd126afe | 5628 | if (!se) |
72465447 | 5629 | sub_nr_running(rq, 1); |
cd126afe | 5630 | |
323af6de VK |
5631 | /* balance early to pull high priority tasks */ |
5632 | if (unlikely(!was_sched_idle && sched_idle_rq(rq))) | |
5633 | rq->next_balance = jiffies; | |
5634 | ||
7f65ea42 | 5635 | util_est_dequeue(&rq->cfs, p, task_sleep); |
a4c2f00f | 5636 | hrtick_update(rq); |
bf0f6f24 IM |
5637 | } |
5638 | ||
e7693a36 | 5639 | #ifdef CONFIG_SMP |
10e2f1ac PZ |
5640 | |
5641 | /* Working cpumask for: load_balance, load_balance_newidle. */ | |
5642 | DEFINE_PER_CPU(cpumask_var_t, load_balance_mask); | |
5643 | DEFINE_PER_CPU(cpumask_var_t, select_idle_mask); | |
5644 | ||
9fd81dd5 | 5645 | #ifdef CONFIG_NO_HZ_COMMON |
e022e0d3 PZ |
5646 | |
5647 | static struct { | |
5648 | cpumask_var_t idle_cpus_mask; | |
5649 | atomic_t nr_cpus; | |
f643ea22 | 5650 | int has_blocked; /* Idle CPUS has blocked load */ |
e022e0d3 | 5651 | unsigned long next_balance; /* in jiffy units */ |
f643ea22 | 5652 | unsigned long next_blocked; /* Next update of blocked load in jiffies */ |
e022e0d3 PZ |
5653 | } nohz ____cacheline_aligned; |
5654 | ||
9fd81dd5 | 5655 | #endif /* CONFIG_NO_HZ_COMMON */ |
3289bdb4 | 5656 | |
b0fb1eb4 VG |
5657 | static unsigned long cpu_load(struct rq *rq) |
5658 | { | |
5659 | return cfs_rq_load_avg(&rq->cfs); | |
5660 | } | |
5661 | ||
3318544b VG |
5662 | /* |
5663 | * cpu_load_without - compute CPU load without any contributions from *p | |
5664 | * @cpu: the CPU which load is requested | |
5665 | * @p: the task which load should be discounted | |
5666 | * | |
5667 | * The load of a CPU is defined by the load of tasks currently enqueued on that | |
5668 | * CPU as well as tasks which are currently sleeping after an execution on that | |
5669 | * CPU. | |
5670 | * | |
5671 | * This method returns the load of the specified CPU by discounting the load of | |
5672 | * the specified task, whenever the task is currently contributing to the CPU | |
5673 | * load. | |
5674 | */ | |
5675 | static unsigned long cpu_load_without(struct rq *rq, struct task_struct *p) | |
5676 | { | |
5677 | struct cfs_rq *cfs_rq; | |
5678 | unsigned int load; | |
5679 | ||
5680 | /* Task has no contribution or is new */ | |
5681 | if (cpu_of(rq) != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) | |
5682 | return cpu_load(rq); | |
5683 | ||
5684 | cfs_rq = &rq->cfs; | |
5685 | load = READ_ONCE(cfs_rq->avg.load_avg); | |
5686 | ||
5687 | /* Discount task's util from CPU's util */ | |
5688 | lsub_positive(&load, task_h_load(p)); | |
5689 | ||
5690 | return load; | |
5691 | } | |
5692 | ||
9f683953 VG |
5693 | static unsigned long cpu_runnable(struct rq *rq) |
5694 | { | |
5695 | return cfs_rq_runnable_avg(&rq->cfs); | |
5696 | } | |
5697 | ||
070f5e86 VG |
5698 | static unsigned long cpu_runnable_without(struct rq *rq, struct task_struct *p) |
5699 | { | |
5700 | struct cfs_rq *cfs_rq; | |
5701 | unsigned int runnable; | |
5702 | ||
5703 | /* Task has no contribution or is new */ | |
5704 | if (cpu_of(rq) != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) | |
5705 | return cpu_runnable(rq); | |
5706 | ||
5707 | cfs_rq = &rq->cfs; | |
5708 | runnable = READ_ONCE(cfs_rq->avg.runnable_avg); | |
5709 | ||
5710 | /* Discount task's runnable from CPU's runnable */ | |
5711 | lsub_positive(&runnable, p->se.avg.runnable_avg); | |
5712 | ||
5713 | return runnable; | |
5714 | } | |
5715 | ||
ced549fa | 5716 | static unsigned long capacity_of(int cpu) |
029632fb | 5717 | { |
ced549fa | 5718 | return cpu_rq(cpu)->cpu_capacity; |
029632fb PZ |
5719 | } |
5720 | ||
c58d25f3 PZ |
5721 | static void record_wakee(struct task_struct *p) |
5722 | { | |
5723 | /* | |
5724 | * Only decay a single time; tasks that have less then 1 wakeup per | |
5725 | * jiffy will not have built up many flips. | |
5726 | */ | |
5727 | if (time_after(jiffies, current->wakee_flip_decay_ts + HZ)) { | |
5728 | current->wakee_flips >>= 1; | |
5729 | current->wakee_flip_decay_ts = jiffies; | |
5730 | } | |
5731 | ||
5732 | if (current->last_wakee != p) { | |
5733 | current->last_wakee = p; | |
5734 | current->wakee_flips++; | |
5735 | } | |
5736 | } | |
5737 | ||
63b0e9ed MG |
5738 | /* |
5739 | * Detect M:N waker/wakee relationships via a switching-frequency heuristic. | |
c58d25f3 | 5740 | * |
63b0e9ed | 5741 | * A waker of many should wake a different task than the one last awakened |
c58d25f3 PZ |
5742 | * at a frequency roughly N times higher than one of its wakees. |
5743 | * | |
5744 | * In order to determine whether we should let the load spread vs consolidating | |
5745 | * to shared cache, we look for a minimum 'flip' frequency of llc_size in one | |
5746 | * partner, and a factor of lls_size higher frequency in the other. | |
5747 | * | |
5748 | * With both conditions met, we can be relatively sure that the relationship is | |
5749 | * non-monogamous, with partner count exceeding socket size. | |
5750 | * | |
5751 | * Waker/wakee being client/server, worker/dispatcher, interrupt source or | |
5752 | * whatever is irrelevant, spread criteria is apparent partner count exceeds | |
5753 | * socket size. | |
63b0e9ed | 5754 | */ |
62470419 MW |
5755 | static int wake_wide(struct task_struct *p) |
5756 | { | |
63b0e9ed MG |
5757 | unsigned int master = current->wakee_flips; |
5758 | unsigned int slave = p->wakee_flips; | |
17c891ab | 5759 | int factor = __this_cpu_read(sd_llc_size); |
62470419 | 5760 | |
63b0e9ed MG |
5761 | if (master < slave) |
5762 | swap(master, slave); | |
5763 | if (slave < factor || master < slave * factor) | |
5764 | return 0; | |
5765 | return 1; | |
62470419 MW |
5766 | } |
5767 | ||
90001d67 | 5768 | /* |
d153b153 PZ |
5769 | * The purpose of wake_affine() is to quickly determine on which CPU we can run |
5770 | * soonest. For the purpose of speed we only consider the waking and previous | |
5771 | * CPU. | |
90001d67 | 5772 | * |
7332dec0 MG |
5773 | * wake_affine_idle() - only considers 'now', it check if the waking CPU is |
5774 | * cache-affine and is (or will be) idle. | |
f2cdd9cc PZ |
5775 | * |
5776 | * wake_affine_weight() - considers the weight to reflect the average | |
5777 | * scheduling latency of the CPUs. This seems to work | |
5778 | * for the overloaded case. | |
90001d67 | 5779 | */ |
3b76c4a3 | 5780 | static int |
89a55f56 | 5781 | wake_affine_idle(int this_cpu, int prev_cpu, int sync) |
90001d67 | 5782 | { |
7332dec0 MG |
5783 | /* |
5784 | * If this_cpu is idle, it implies the wakeup is from interrupt | |
5785 | * context. Only allow the move if cache is shared. Otherwise an | |
5786 | * interrupt intensive workload could force all tasks onto one | |
5787 | * node depending on the IO topology or IRQ affinity settings. | |
806486c3 MG |
5788 | * |
5789 | * If the prev_cpu is idle and cache affine then avoid a migration. | |
5790 | * There is no guarantee that the cache hot data from an interrupt | |
5791 | * is more important than cache hot data on the prev_cpu and from | |
5792 | * a cpufreq perspective, it's better to have higher utilisation | |
5793 | * on one CPU. | |
7332dec0 | 5794 | */ |
943d355d RJ |
5795 | if (available_idle_cpu(this_cpu) && cpus_share_cache(this_cpu, prev_cpu)) |
5796 | return available_idle_cpu(prev_cpu) ? prev_cpu : this_cpu; | |
90001d67 | 5797 | |
d153b153 | 5798 | if (sync && cpu_rq(this_cpu)->nr_running == 1) |
3b76c4a3 | 5799 | return this_cpu; |
90001d67 | 5800 | |
3b76c4a3 | 5801 | return nr_cpumask_bits; |
90001d67 PZ |
5802 | } |
5803 | ||
3b76c4a3 | 5804 | static int |
f2cdd9cc PZ |
5805 | wake_affine_weight(struct sched_domain *sd, struct task_struct *p, |
5806 | int this_cpu, int prev_cpu, int sync) | |
90001d67 | 5807 | { |
90001d67 PZ |
5808 | s64 this_eff_load, prev_eff_load; |
5809 | unsigned long task_load; | |
5810 | ||
11f10e54 | 5811 | this_eff_load = cpu_load(cpu_rq(this_cpu)); |
90001d67 | 5812 | |
90001d67 PZ |
5813 | if (sync) { |
5814 | unsigned long current_load = task_h_load(current); | |
5815 | ||
f2cdd9cc | 5816 | if (current_load > this_eff_load) |
3b76c4a3 | 5817 | return this_cpu; |
90001d67 | 5818 | |
f2cdd9cc | 5819 | this_eff_load -= current_load; |
90001d67 PZ |
5820 | } |
5821 | ||
90001d67 PZ |
5822 | task_load = task_h_load(p); |
5823 | ||
f2cdd9cc PZ |
5824 | this_eff_load += task_load; |
5825 | if (sched_feat(WA_BIAS)) | |
5826 | this_eff_load *= 100; | |
5827 | this_eff_load *= capacity_of(prev_cpu); | |
90001d67 | 5828 | |
11f10e54 | 5829 | prev_eff_load = cpu_load(cpu_rq(prev_cpu)); |
f2cdd9cc PZ |
5830 | prev_eff_load -= task_load; |
5831 | if (sched_feat(WA_BIAS)) | |
5832 | prev_eff_load *= 100 + (sd->imbalance_pct - 100) / 2; | |
5833 | prev_eff_load *= capacity_of(this_cpu); | |
90001d67 | 5834 | |
082f764a MG |
5835 | /* |
5836 | * If sync, adjust the weight of prev_eff_load such that if | |
5837 | * prev_eff == this_eff that select_idle_sibling() will consider | |
5838 | * stacking the wakee on top of the waker if no other CPU is | |
5839 | * idle. | |
5840 | */ | |
5841 | if (sync) | |
5842 | prev_eff_load += 1; | |
5843 | ||
5844 | return this_eff_load < prev_eff_load ? this_cpu : nr_cpumask_bits; | |
90001d67 PZ |
5845 | } |
5846 | ||
772bd008 | 5847 | static int wake_affine(struct sched_domain *sd, struct task_struct *p, |
7ebb66a1 | 5848 | int this_cpu, int prev_cpu, int sync) |
098fb9db | 5849 | { |
3b76c4a3 | 5850 | int target = nr_cpumask_bits; |
098fb9db | 5851 | |
89a55f56 | 5852 | if (sched_feat(WA_IDLE)) |
3b76c4a3 | 5853 | target = wake_affine_idle(this_cpu, prev_cpu, sync); |
90001d67 | 5854 | |
3b76c4a3 MG |
5855 | if (sched_feat(WA_WEIGHT) && target == nr_cpumask_bits) |
5856 | target = wake_affine_weight(sd, p, this_cpu, prev_cpu, sync); | |
098fb9db | 5857 | |
ae92882e | 5858 | schedstat_inc(p->se.statistics.nr_wakeups_affine_attempts); |
3b76c4a3 MG |
5859 | if (target == nr_cpumask_bits) |
5860 | return prev_cpu; | |
098fb9db | 5861 | |
3b76c4a3 MG |
5862 | schedstat_inc(sd->ttwu_move_affine); |
5863 | schedstat_inc(p->se.statistics.nr_wakeups_affine); | |
5864 | return target; | |
098fb9db IM |
5865 | } |
5866 | ||
aaee1203 | 5867 | static struct sched_group * |
45da2773 | 5868 | find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu); |
aaee1203 PZ |
5869 | |
5870 | /* | |
97fb7a0a | 5871 | * find_idlest_group_cpu - find the idlest CPU among the CPUs in the group. |
aaee1203 PZ |
5872 | */ |
5873 | static int | |
18bd1b4b | 5874 | find_idlest_group_cpu(struct sched_group *group, struct task_struct *p, int this_cpu) |
aaee1203 PZ |
5875 | { |
5876 | unsigned long load, min_load = ULONG_MAX; | |
83a0a96a NP |
5877 | unsigned int min_exit_latency = UINT_MAX; |
5878 | u64 latest_idle_timestamp = 0; | |
5879 | int least_loaded_cpu = this_cpu; | |
17346452 | 5880 | int shallowest_idle_cpu = -1; |
aaee1203 PZ |
5881 | int i; |
5882 | ||
eaecf41f MR |
5883 | /* Check if we have any choice: */ |
5884 | if (group->group_weight == 1) | |
ae4df9d6 | 5885 | return cpumask_first(sched_group_span(group)); |
eaecf41f | 5886 | |
aaee1203 | 5887 | /* Traverse only the allowed CPUs */ |
3bd37062 | 5888 | for_each_cpu_and(i, sched_group_span(group), p->cpus_ptr) { |
17346452 VK |
5889 | if (sched_idle_cpu(i)) |
5890 | return i; | |
5891 | ||
943d355d | 5892 | if (available_idle_cpu(i)) { |
83a0a96a NP |
5893 | struct rq *rq = cpu_rq(i); |
5894 | struct cpuidle_state *idle = idle_get_state(rq); | |
5895 | if (idle && idle->exit_latency < min_exit_latency) { | |
5896 | /* | |
5897 | * We give priority to a CPU whose idle state | |
5898 | * has the smallest exit latency irrespective | |
5899 | * of any idle timestamp. | |
5900 | */ | |
5901 | min_exit_latency = idle->exit_latency; | |
5902 | latest_idle_timestamp = rq->idle_stamp; | |
5903 | shallowest_idle_cpu = i; | |
5904 | } else if ((!idle || idle->exit_latency == min_exit_latency) && | |
5905 | rq->idle_stamp > latest_idle_timestamp) { | |
5906 | /* | |
5907 | * If equal or no active idle state, then | |
5908 | * the most recently idled CPU might have | |
5909 | * a warmer cache. | |
5910 | */ | |
5911 | latest_idle_timestamp = rq->idle_stamp; | |
5912 | shallowest_idle_cpu = i; | |
5913 | } | |
17346452 | 5914 | } else if (shallowest_idle_cpu == -1) { |
11f10e54 | 5915 | load = cpu_load(cpu_rq(i)); |
18cec7e0 | 5916 | if (load < min_load) { |
83a0a96a NP |
5917 | min_load = load; |
5918 | least_loaded_cpu = i; | |
5919 | } | |
e7693a36 GH |
5920 | } |
5921 | } | |
5922 | ||
17346452 | 5923 | return shallowest_idle_cpu != -1 ? shallowest_idle_cpu : least_loaded_cpu; |
aaee1203 | 5924 | } |
e7693a36 | 5925 | |
18bd1b4b BJ |
5926 | static inline int find_idlest_cpu(struct sched_domain *sd, struct task_struct *p, |
5927 | int cpu, int prev_cpu, int sd_flag) | |
5928 | { | |
93f50f90 | 5929 | int new_cpu = cpu; |
18bd1b4b | 5930 | |
3bd37062 | 5931 | if (!cpumask_intersects(sched_domain_span(sd), p->cpus_ptr)) |
6fee85cc BJ |
5932 | return prev_cpu; |
5933 | ||
c976a862 | 5934 | /* |
57abff06 | 5935 | * We need task's util for cpu_util_without, sync it up to |
c469933e | 5936 | * prev_cpu's last_update_time. |
c976a862 VK |
5937 | */ |
5938 | if (!(sd_flag & SD_BALANCE_FORK)) | |
5939 | sync_entity_load_avg(&p->se); | |
5940 | ||
18bd1b4b BJ |
5941 | while (sd) { |
5942 | struct sched_group *group; | |
5943 | struct sched_domain *tmp; | |
5944 | int weight; | |
5945 | ||
5946 | if (!(sd->flags & sd_flag)) { | |
5947 | sd = sd->child; | |
5948 | continue; | |
5949 | } | |
5950 | ||
45da2773 | 5951 | group = find_idlest_group(sd, p, cpu); |
18bd1b4b BJ |
5952 | if (!group) { |
5953 | sd = sd->child; | |
5954 | continue; | |
5955 | } | |
5956 | ||
5957 | new_cpu = find_idlest_group_cpu(group, p, cpu); | |
e90381ea | 5958 | if (new_cpu == cpu) { |
97fb7a0a | 5959 | /* Now try balancing at a lower domain level of 'cpu': */ |
18bd1b4b BJ |
5960 | sd = sd->child; |
5961 | continue; | |
5962 | } | |
5963 | ||
97fb7a0a | 5964 | /* Now try balancing at a lower domain level of 'new_cpu': */ |
18bd1b4b BJ |
5965 | cpu = new_cpu; |
5966 | weight = sd->span_weight; | |
5967 | sd = NULL; | |
5968 | for_each_domain(cpu, tmp) { | |
5969 | if (weight <= tmp->span_weight) | |
5970 | break; | |
5971 | if (tmp->flags & sd_flag) | |
5972 | sd = tmp; | |
5973 | } | |
18bd1b4b BJ |
5974 | } |
5975 | ||
5976 | return new_cpu; | |
5977 | } | |
5978 | ||
10e2f1ac | 5979 | #ifdef CONFIG_SCHED_SMT |
ba2591a5 | 5980 | DEFINE_STATIC_KEY_FALSE(sched_smt_present); |
b284909a | 5981 | EXPORT_SYMBOL_GPL(sched_smt_present); |
10e2f1ac PZ |
5982 | |
5983 | static inline void set_idle_cores(int cpu, int val) | |
5984 | { | |
5985 | struct sched_domain_shared *sds; | |
5986 | ||
5987 | sds = rcu_dereference(per_cpu(sd_llc_shared, cpu)); | |
5988 | if (sds) | |
5989 | WRITE_ONCE(sds->has_idle_cores, val); | |
5990 | } | |
5991 | ||
5992 | static inline bool test_idle_cores(int cpu, bool def) | |
5993 | { | |
5994 | struct sched_domain_shared *sds; | |
5995 | ||
5996 | sds = rcu_dereference(per_cpu(sd_llc_shared, cpu)); | |
5997 | if (sds) | |
5998 | return READ_ONCE(sds->has_idle_cores); | |
5999 | ||
6000 | return def; | |
6001 | } | |
6002 | ||
6003 | /* | |
6004 | * Scans the local SMT mask to see if the entire core is idle, and records this | |
6005 | * information in sd_llc_shared->has_idle_cores. | |
6006 | * | |
6007 | * Since SMT siblings share all cache levels, inspecting this limited remote | |
6008 | * state should be fairly cheap. | |
6009 | */ | |
1b568f0a | 6010 | void __update_idle_core(struct rq *rq) |
10e2f1ac PZ |
6011 | { |
6012 | int core = cpu_of(rq); | |
6013 | int cpu; | |
6014 | ||
6015 | rcu_read_lock(); | |
6016 | if (test_idle_cores(core, true)) | |
6017 | goto unlock; | |
6018 | ||
6019 | for_each_cpu(cpu, cpu_smt_mask(core)) { | |
6020 | if (cpu == core) | |
6021 | continue; | |
6022 | ||
943d355d | 6023 | if (!available_idle_cpu(cpu)) |
10e2f1ac PZ |
6024 | goto unlock; |
6025 | } | |
6026 | ||
6027 | set_idle_cores(core, 1); | |
6028 | unlock: | |
6029 | rcu_read_unlock(); | |
6030 | } | |
6031 | ||
6032 | /* | |
6033 | * Scan the entire LLC domain for idle cores; this dynamically switches off if | |
6034 | * there are no idle cores left in the system; tracked through | |
6035 | * sd_llc->shared->has_idle_cores and enabled through update_idle_core() above. | |
6036 | */ | |
6037 | static int select_idle_core(struct task_struct *p, struct sched_domain *sd, int target) | |
6038 | { | |
6039 | struct cpumask *cpus = this_cpu_cpumask_var_ptr(select_idle_mask); | |
c743f0a5 | 6040 | int core, cpu; |
10e2f1ac | 6041 | |
1b568f0a PZ |
6042 | if (!static_branch_likely(&sched_smt_present)) |
6043 | return -1; | |
6044 | ||
10e2f1ac PZ |
6045 | if (!test_idle_cores(target, false)) |
6046 | return -1; | |
6047 | ||
3bd37062 | 6048 | cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr); |
10e2f1ac | 6049 | |
c743f0a5 | 6050 | for_each_cpu_wrap(core, cpus, target) { |
10e2f1ac PZ |
6051 | bool idle = true; |
6052 | ||
6053 | for_each_cpu(cpu, cpu_smt_mask(core)) { | |
bec2860a | 6054 | if (!available_idle_cpu(cpu)) { |
10e2f1ac | 6055 | idle = false; |
bec2860a SD |
6056 | break; |
6057 | } | |
10e2f1ac | 6058 | } |
bec2860a | 6059 | cpumask_andnot(cpus, cpus, cpu_smt_mask(core)); |
10e2f1ac PZ |
6060 | |
6061 | if (idle) | |
6062 | return core; | |
6063 | } | |
6064 | ||
6065 | /* | |
6066 | * Failed to find an idle core; stop looking for one. | |
6067 | */ | |
6068 | set_idle_cores(target, 0); | |
6069 | ||
6070 | return -1; | |
6071 | } | |
6072 | ||
6073 | /* | |
6074 | * Scan the local SMT mask for idle CPUs. | |
6075 | */ | |
1b5500d7 | 6076 | static int select_idle_smt(struct task_struct *p, int target) |
10e2f1ac | 6077 | { |
17346452 | 6078 | int cpu; |
10e2f1ac | 6079 | |
1b568f0a PZ |
6080 | if (!static_branch_likely(&sched_smt_present)) |
6081 | return -1; | |
6082 | ||
10e2f1ac | 6083 | for_each_cpu(cpu, cpu_smt_mask(target)) { |
3bd37062 | 6084 | if (!cpumask_test_cpu(cpu, p->cpus_ptr)) |
10e2f1ac | 6085 | continue; |
17346452 | 6086 | if (available_idle_cpu(cpu) || sched_idle_cpu(cpu)) |
10e2f1ac PZ |
6087 | return cpu; |
6088 | } | |
6089 | ||
17346452 | 6090 | return -1; |
10e2f1ac PZ |
6091 | } |
6092 | ||
6093 | #else /* CONFIG_SCHED_SMT */ | |
6094 | ||
6095 | static inline int select_idle_core(struct task_struct *p, struct sched_domain *sd, int target) | |
6096 | { | |
6097 | return -1; | |
6098 | } | |
6099 | ||
1b5500d7 | 6100 | static inline int select_idle_smt(struct task_struct *p, int target) |
10e2f1ac PZ |
6101 | { |
6102 | return -1; | |
6103 | } | |
6104 | ||
6105 | #endif /* CONFIG_SCHED_SMT */ | |
6106 | ||
6107 | /* | |
6108 | * Scan the LLC domain for idle CPUs; this is dynamically regulated by | |
6109 | * comparing the average scan cost (tracked in sd->avg_scan_cost) against the | |
6110 | * average idle time for this rq (as found in rq->avg_idle). | |
a50bde51 | 6111 | */ |
10e2f1ac PZ |
6112 | static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, int target) |
6113 | { | |
60588bfa | 6114 | struct cpumask *cpus = this_cpu_cpumask_var_ptr(select_idle_mask); |
9cfb38a7 | 6115 | struct sched_domain *this_sd; |
1ad3aaf3 | 6116 | u64 avg_cost, avg_idle; |
d76343c6 | 6117 | u64 time; |
8dc2d993 | 6118 | int this = smp_processor_id(); |
17346452 | 6119 | int cpu, nr = INT_MAX; |
10e2f1ac | 6120 | |
9cfb38a7 WL |
6121 | this_sd = rcu_dereference(*this_cpu_ptr(&sd_llc)); |
6122 | if (!this_sd) | |
6123 | return -1; | |
6124 | ||
10e2f1ac PZ |
6125 | /* |
6126 | * Due to large variance we need a large fuzz factor; hackbench in | |
6127 | * particularly is sensitive here. | |
6128 | */ | |
1ad3aaf3 PZ |
6129 | avg_idle = this_rq()->avg_idle / 512; |
6130 | avg_cost = this_sd->avg_scan_cost + 1; | |
6131 | ||
6132 | if (sched_feat(SIS_AVG_CPU) && avg_idle < avg_cost) | |
10e2f1ac PZ |
6133 | return -1; |
6134 | ||
1ad3aaf3 PZ |
6135 | if (sched_feat(SIS_PROP)) { |
6136 | u64 span_avg = sd->span_weight * avg_idle; | |
6137 | if (span_avg > 4*avg_cost) | |
6138 | nr = div_u64(span_avg, avg_cost); | |
6139 | else | |
6140 | nr = 4; | |
6141 | } | |
6142 | ||
8dc2d993 | 6143 | time = cpu_clock(this); |
10e2f1ac | 6144 | |
60588bfa CJ |
6145 | cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr); |
6146 | ||
6147 | for_each_cpu_wrap(cpu, cpus, target) { | |
1ad3aaf3 | 6148 | if (!--nr) |
17346452 VK |
6149 | return -1; |
6150 | if (available_idle_cpu(cpu) || sched_idle_cpu(cpu)) | |
10e2f1ac PZ |
6151 | break; |
6152 | } | |
6153 | ||
8dc2d993 | 6154 | time = cpu_clock(this) - time; |
d76343c6 | 6155 | update_avg(&this_sd->avg_scan_cost, time); |
10e2f1ac PZ |
6156 | |
6157 | return cpu; | |
6158 | } | |
6159 | ||
b7a33161 MR |
6160 | /* |
6161 | * Scan the asym_capacity domain for idle CPUs; pick the first idle one on which | |
6162 | * the task fits. If no CPU is big enough, but there are idle ones, try to | |
6163 | * maximize capacity. | |
6164 | */ | |
6165 | static int | |
6166 | select_idle_capacity(struct task_struct *p, struct sched_domain *sd, int target) | |
6167 | { | |
6168 | unsigned long best_cap = 0; | |
6169 | int cpu, best_cpu = -1; | |
6170 | struct cpumask *cpus; | |
6171 | ||
6172 | sync_entity_load_avg(&p->se); | |
6173 | ||
6174 | cpus = this_cpu_cpumask_var_ptr(select_idle_mask); | |
6175 | cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr); | |
6176 | ||
6177 | for_each_cpu_wrap(cpu, cpus, target) { | |
6178 | unsigned long cpu_cap = capacity_of(cpu); | |
6179 | ||
6180 | if (!available_idle_cpu(cpu) && !sched_idle_cpu(cpu)) | |
6181 | continue; | |
6182 | if (task_fits_capacity(p, cpu_cap)) | |
6183 | return cpu; | |
6184 | ||
6185 | if (cpu_cap > best_cap) { | |
6186 | best_cap = cpu_cap; | |
6187 | best_cpu = cpu; | |
6188 | } | |
6189 | } | |
6190 | ||
6191 | return best_cpu; | |
6192 | } | |
6193 | ||
10e2f1ac PZ |
6194 | /* |
6195 | * Try and locate an idle core/thread in the LLC cache domain. | |
a50bde51 | 6196 | */ |
772bd008 | 6197 | static int select_idle_sibling(struct task_struct *p, int prev, int target) |
a50bde51 | 6198 | { |
99bd5e2f | 6199 | struct sched_domain *sd; |
32e839dd | 6200 | int i, recent_used_cpu; |
a50bde51 | 6201 | |
b7a33161 MR |
6202 | /* |
6203 | * For asymmetric CPU capacity systems, our domain of interest is | |
6204 | * sd_asym_cpucapacity rather than sd_llc. | |
6205 | */ | |
6206 | if (static_branch_unlikely(&sched_asym_cpucapacity)) { | |
6207 | sd = rcu_dereference(per_cpu(sd_asym_cpucapacity, target)); | |
6208 | /* | |
6209 | * On an asymmetric CPU capacity system where an exclusive | |
6210 | * cpuset defines a symmetric island (i.e. one unique | |
6211 | * capacity_orig value through the cpuset), the key will be set | |
6212 | * but the CPUs within that cpuset will not have a domain with | |
6213 | * SD_ASYM_CPUCAPACITY. These should follow the usual symmetric | |
6214 | * capacity path. | |
6215 | */ | |
6216 | if (!sd) | |
6217 | goto symmetric; | |
6218 | ||
6219 | i = select_idle_capacity(p, sd, target); | |
6220 | return ((unsigned)i < nr_cpumask_bits) ? i : target; | |
6221 | } | |
6222 | ||
6223 | symmetric: | |
3c29e651 | 6224 | if (available_idle_cpu(target) || sched_idle_cpu(target)) |
e0a79f52 | 6225 | return target; |
99bd5e2f SS |
6226 | |
6227 | /* | |
97fb7a0a | 6228 | * If the previous CPU is cache affine and idle, don't be stupid: |
99bd5e2f | 6229 | */ |
3c29e651 VK |
6230 | if (prev != target && cpus_share_cache(prev, target) && |
6231 | (available_idle_cpu(prev) || sched_idle_cpu(prev))) | |
772bd008 | 6232 | return prev; |
a50bde51 | 6233 | |
52262ee5 MG |
6234 | /* |
6235 | * Allow a per-cpu kthread to stack with the wakee if the | |
6236 | * kworker thread and the tasks previous CPUs are the same. | |
6237 | * The assumption is that the wakee queued work for the | |
6238 | * per-cpu kthread that is now complete and the wakeup is | |
6239 | * essentially a sync wakeup. An obvious example of this | |
6240 | * pattern is IO completions. | |
6241 | */ | |
6242 | if (is_per_cpu_kthread(current) && | |
6243 | prev == smp_processor_id() && | |
6244 | this_rq()->nr_running <= 1) { | |
6245 | return prev; | |
6246 | } | |
6247 | ||
97fb7a0a | 6248 | /* Check a recently used CPU as a potential idle candidate: */ |
32e839dd MG |
6249 | recent_used_cpu = p->recent_used_cpu; |
6250 | if (recent_used_cpu != prev && | |
6251 | recent_used_cpu != target && | |
6252 | cpus_share_cache(recent_used_cpu, target) && | |
3c29e651 | 6253 | (available_idle_cpu(recent_used_cpu) || sched_idle_cpu(recent_used_cpu)) && |
3bd37062 | 6254 | cpumask_test_cpu(p->recent_used_cpu, p->cpus_ptr)) { |
32e839dd MG |
6255 | /* |
6256 | * Replace recent_used_cpu with prev as it is a potential | |
97fb7a0a | 6257 | * candidate for the next wake: |
32e839dd MG |
6258 | */ |
6259 | p->recent_used_cpu = prev; | |
6260 | return recent_used_cpu; | |
6261 | } | |
6262 | ||
518cd623 | 6263 | sd = rcu_dereference(per_cpu(sd_llc, target)); |
10e2f1ac PZ |
6264 | if (!sd) |
6265 | return target; | |
772bd008 | 6266 | |
10e2f1ac PZ |
6267 | i = select_idle_core(p, sd, target); |
6268 | if ((unsigned)i < nr_cpumask_bits) | |
6269 | return i; | |
37407ea7 | 6270 | |
10e2f1ac PZ |
6271 | i = select_idle_cpu(p, sd, target); |
6272 | if ((unsigned)i < nr_cpumask_bits) | |
6273 | return i; | |
6274 | ||
1b5500d7 | 6275 | i = select_idle_smt(p, target); |
10e2f1ac PZ |
6276 | if ((unsigned)i < nr_cpumask_bits) |
6277 | return i; | |
970e1789 | 6278 | |
a50bde51 PZ |
6279 | return target; |
6280 | } | |
231678b7 | 6281 | |
f9be3e59 PB |
6282 | /** |
6283 | * Amount of capacity of a CPU that is (estimated to be) used by CFS tasks | |
6284 | * @cpu: the CPU to get the utilization of | |
6285 | * | |
6286 | * The unit of the return value must be the one of capacity so we can compare | |
6287 | * the utilization with the capacity of the CPU that is available for CFS task | |
6288 | * (ie cpu_capacity). | |
231678b7 DE |
6289 | * |
6290 | * cfs_rq.avg.util_avg is the sum of running time of runnable tasks plus the | |
6291 | * recent utilization of currently non-runnable tasks on a CPU. It represents | |
6292 | * the amount of utilization of a CPU in the range [0..capacity_orig] where | |
6293 | * capacity_orig is the cpu_capacity available at the highest frequency | |
6294 | * (arch_scale_freq_capacity()). | |
6295 | * The utilization of a CPU converges towards a sum equal to or less than the | |
6296 | * current capacity (capacity_curr <= capacity_orig) of the CPU because it is | |
6297 | * the running time on this CPU scaled by capacity_curr. | |
6298 | * | |
f9be3e59 PB |
6299 | * The estimated utilization of a CPU is defined to be the maximum between its |
6300 | * cfs_rq.avg.util_avg and the sum of the estimated utilization of the tasks | |
6301 | * currently RUNNABLE on that CPU. | |
6302 | * This allows to properly represent the expected utilization of a CPU which | |
6303 | * has just got a big task running since a long sleep period. At the same time | |
6304 | * however it preserves the benefits of the "blocked utilization" in | |
6305 | * describing the potential for other tasks waking up on the same CPU. | |
6306 | * | |
231678b7 DE |
6307 | * Nevertheless, cfs_rq.avg.util_avg can be higher than capacity_curr or even |
6308 | * higher than capacity_orig because of unfortunate rounding in | |
6309 | * cfs.avg.util_avg or just after migrating tasks and new task wakeups until | |
6310 | * the average stabilizes with the new running time. We need to check that the | |
6311 | * utilization stays within the range of [0..capacity_orig] and cap it if | |
6312 | * necessary. Without utilization capping, a group could be seen as overloaded | |
6313 | * (CPU0 utilization at 121% + CPU1 utilization at 80%) whereas CPU1 has 20% of | |
6314 | * available capacity. We allow utilization to overshoot capacity_curr (but not | |
6315 | * capacity_orig) as it useful for predicting the capacity required after task | |
6316 | * migrations (scheduler-driven DVFS). | |
f9be3e59 PB |
6317 | * |
6318 | * Return: the (estimated) utilization for the specified CPU | |
8bb5b00c | 6319 | */ |
f9be3e59 | 6320 | static inline unsigned long cpu_util(int cpu) |
8bb5b00c | 6321 | { |
f9be3e59 PB |
6322 | struct cfs_rq *cfs_rq; |
6323 | unsigned int util; | |
6324 | ||
6325 | cfs_rq = &cpu_rq(cpu)->cfs; | |
6326 | util = READ_ONCE(cfs_rq->avg.util_avg); | |
6327 | ||
6328 | if (sched_feat(UTIL_EST)) | |
6329 | util = max(util, READ_ONCE(cfs_rq->avg.util_est.enqueued)); | |
8bb5b00c | 6330 | |
f9be3e59 | 6331 | return min_t(unsigned long, util, capacity_orig_of(cpu)); |
8bb5b00c | 6332 | } |
a50bde51 | 6333 | |
104cb16d | 6334 | /* |
c469933e PB |
6335 | * cpu_util_without: compute cpu utilization without any contributions from *p |
6336 | * @cpu: the CPU which utilization is requested | |
6337 | * @p: the task which utilization should be discounted | |
6338 | * | |
6339 | * The utilization of a CPU is defined by the utilization of tasks currently | |
6340 | * enqueued on that CPU as well as tasks which are currently sleeping after an | |
6341 | * execution on that CPU. | |
6342 | * | |
6343 | * This method returns the utilization of the specified CPU by discounting the | |
6344 | * utilization of the specified task, whenever the task is currently | |
6345 | * contributing to the CPU utilization. | |
104cb16d | 6346 | */ |
c469933e | 6347 | static unsigned long cpu_util_without(int cpu, struct task_struct *p) |
104cb16d | 6348 | { |
f9be3e59 PB |
6349 | struct cfs_rq *cfs_rq; |
6350 | unsigned int util; | |
104cb16d MR |
6351 | |
6352 | /* Task has no contribution or is new */ | |
f9be3e59 | 6353 | if (cpu != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) |
104cb16d MR |
6354 | return cpu_util(cpu); |
6355 | ||
f9be3e59 PB |
6356 | cfs_rq = &cpu_rq(cpu)->cfs; |
6357 | util = READ_ONCE(cfs_rq->avg.util_avg); | |
6358 | ||
c469933e | 6359 | /* Discount task's util from CPU's util */ |
b5c0ce7b | 6360 | lsub_positive(&util, task_util(p)); |
104cb16d | 6361 | |
f9be3e59 PB |
6362 | /* |
6363 | * Covered cases: | |
6364 | * | |
6365 | * a) if *p is the only task sleeping on this CPU, then: | |
6366 | * cpu_util (== task_util) > util_est (== 0) | |
6367 | * and thus we return: | |
c469933e | 6368 | * cpu_util_without = (cpu_util - task_util) = 0 |
f9be3e59 PB |
6369 | * |
6370 | * b) if other tasks are SLEEPING on this CPU, which is now exiting | |
6371 | * IDLE, then: | |
6372 | * cpu_util >= task_util | |
6373 | * cpu_util > util_est (== 0) | |
6374 | * and thus we discount *p's blocked utilization to return: | |
c469933e | 6375 | * cpu_util_without = (cpu_util - task_util) >= 0 |
f9be3e59 PB |
6376 | * |
6377 | * c) if other tasks are RUNNABLE on that CPU and | |
6378 | * util_est > cpu_util | |
6379 | * then we use util_est since it returns a more restrictive | |
6380 | * estimation of the spare capacity on that CPU, by just | |
6381 | * considering the expected utilization of tasks already | |
6382 | * runnable on that CPU. | |
6383 | * | |
6384 | * Cases a) and b) are covered by the above code, while case c) is | |
6385 | * covered by the following code when estimated utilization is | |
6386 | * enabled. | |
6387 | */ | |
c469933e PB |
6388 | if (sched_feat(UTIL_EST)) { |
6389 | unsigned int estimated = | |
6390 | READ_ONCE(cfs_rq->avg.util_est.enqueued); | |
6391 | ||
6392 | /* | |
6393 | * Despite the following checks we still have a small window | |
6394 | * for a possible race, when an execl's select_task_rq_fair() | |
6395 | * races with LB's detach_task(): | |
6396 | * | |
6397 | * detach_task() | |
6398 | * p->on_rq = TASK_ON_RQ_MIGRATING; | |
6399 | * ---------------------------------- A | |
6400 | * deactivate_task() \ | |
6401 | * dequeue_task() + RaceTime | |
6402 | * util_est_dequeue() / | |
6403 | * ---------------------------------- B | |
6404 | * | |
6405 | * The additional check on "current == p" it's required to | |
6406 | * properly fix the execl regression and it helps in further | |
6407 | * reducing the chances for the above race. | |
6408 | */ | |
b5c0ce7b PB |
6409 | if (unlikely(task_on_rq_queued(p) || current == p)) |
6410 | lsub_positive(&estimated, _task_util_est(p)); | |
6411 | ||
c469933e PB |
6412 | util = max(util, estimated); |
6413 | } | |
f9be3e59 PB |
6414 | |
6415 | /* | |
6416 | * Utilization (estimated) can exceed the CPU capacity, thus let's | |
6417 | * clamp to the maximum CPU capacity to ensure consistency with | |
6418 | * the cpu_util call. | |
6419 | */ | |
6420 | return min_t(unsigned long, util, capacity_orig_of(cpu)); | |
104cb16d MR |
6421 | } |
6422 | ||
390031e4 QP |
6423 | /* |
6424 | * Predicts what cpu_util(@cpu) would return if @p was migrated (and enqueued) | |
6425 | * to @dst_cpu. | |
6426 | */ | |
6427 | static unsigned long cpu_util_next(int cpu, struct task_struct *p, int dst_cpu) | |
6428 | { | |
6429 | struct cfs_rq *cfs_rq = &cpu_rq(cpu)->cfs; | |
6430 | unsigned long util_est, util = READ_ONCE(cfs_rq->avg.util_avg); | |
6431 | ||
6432 | /* | |
6433 | * If @p migrates from @cpu to another, remove its contribution. Or, | |
6434 | * if @p migrates from another CPU to @cpu, add its contribution. In | |
6435 | * the other cases, @cpu is not impacted by the migration, so the | |
6436 | * util_avg should already be correct. | |
6437 | */ | |
6438 | if (task_cpu(p) == cpu && dst_cpu != cpu) | |
6439 | sub_positive(&util, task_util(p)); | |
6440 | else if (task_cpu(p) != cpu && dst_cpu == cpu) | |
6441 | util += task_util(p); | |
6442 | ||
6443 | if (sched_feat(UTIL_EST)) { | |
6444 | util_est = READ_ONCE(cfs_rq->avg.util_est.enqueued); | |
6445 | ||
6446 | /* | |
6447 | * During wake-up, the task isn't enqueued yet and doesn't | |
6448 | * appear in the cfs_rq->avg.util_est.enqueued of any rq, | |
6449 | * so just add it (if needed) to "simulate" what will be | |
6450 | * cpu_util() after the task has been enqueued. | |
6451 | */ | |
6452 | if (dst_cpu == cpu) | |
6453 | util_est += _task_util_est(p); | |
6454 | ||
6455 | util = max(util, util_est); | |
6456 | } | |
6457 | ||
6458 | return min(util, capacity_orig_of(cpu)); | |
6459 | } | |
6460 | ||
6461 | /* | |
eb92692b | 6462 | * compute_energy(): Estimates the energy that @pd would consume if @p was |
390031e4 | 6463 | * migrated to @dst_cpu. compute_energy() predicts what will be the utilization |
eb92692b | 6464 | * landscape of @pd's CPUs after the task migration, and uses the Energy Model |
390031e4 QP |
6465 | * to compute what would be the energy if we decided to actually migrate that |
6466 | * task. | |
6467 | */ | |
6468 | static long | |
6469 | compute_energy(struct task_struct *p, int dst_cpu, struct perf_domain *pd) | |
6470 | { | |
eb92692b QP |
6471 | struct cpumask *pd_mask = perf_domain_span(pd); |
6472 | unsigned long cpu_cap = arch_scale_cpu_capacity(cpumask_first(pd_mask)); | |
6473 | unsigned long max_util = 0, sum_util = 0; | |
390031e4 QP |
6474 | int cpu; |
6475 | ||
eb92692b QP |
6476 | /* |
6477 | * The capacity state of CPUs of the current rd can be driven by CPUs | |
6478 | * of another rd if they belong to the same pd. So, account for the | |
6479 | * utilization of these CPUs too by masking pd with cpu_online_mask | |
6480 | * instead of the rd span. | |
6481 | * | |
6482 | * If an entire pd is outside of the current rd, it will not appear in | |
6483 | * its pd list and will not be accounted by compute_energy(). | |
6484 | */ | |
6485 | for_each_cpu_and(cpu, pd_mask, cpu_online_mask) { | |
6486 | unsigned long cpu_util, util_cfs = cpu_util_next(cpu, p, dst_cpu); | |
6487 | struct task_struct *tsk = cpu == dst_cpu ? p : NULL; | |
af24bde8 PB |
6488 | |
6489 | /* | |
eb92692b QP |
6490 | * Busy time computation: utilization clamping is not |
6491 | * required since the ratio (sum_util / cpu_capacity) | |
6492 | * is already enough to scale the EM reported power | |
6493 | * consumption at the (eventually clamped) cpu_capacity. | |
af24bde8 | 6494 | */ |
eb92692b QP |
6495 | sum_util += schedutil_cpu_util(cpu, util_cfs, cpu_cap, |
6496 | ENERGY_UTIL, NULL); | |
af24bde8 | 6497 | |
390031e4 | 6498 | /* |
eb92692b QP |
6499 | * Performance domain frequency: utilization clamping |
6500 | * must be considered since it affects the selection | |
6501 | * of the performance domain frequency. | |
6502 | * NOTE: in case RT tasks are running, by default the | |
6503 | * FREQUENCY_UTIL's utilization can be max OPP. | |
390031e4 | 6504 | */ |
eb92692b QP |
6505 | cpu_util = schedutil_cpu_util(cpu, util_cfs, cpu_cap, |
6506 | FREQUENCY_UTIL, tsk); | |
6507 | max_util = max(max_util, cpu_util); | |
390031e4 QP |
6508 | } |
6509 | ||
eb92692b | 6510 | return em_pd_energy(pd->em_pd, max_util, sum_util); |
390031e4 QP |
6511 | } |
6512 | ||
732cd75b QP |
6513 | /* |
6514 | * find_energy_efficient_cpu(): Find most energy-efficient target CPU for the | |
6515 | * waking task. find_energy_efficient_cpu() looks for the CPU with maximum | |
6516 | * spare capacity in each performance domain and uses it as a potential | |
6517 | * candidate to execute the task. Then, it uses the Energy Model to figure | |
6518 | * out which of the CPU candidates is the most energy-efficient. | |
6519 | * | |
6520 | * The rationale for this heuristic is as follows. In a performance domain, | |
6521 | * all the most energy efficient CPU candidates (according to the Energy | |
6522 | * Model) are those for which we'll request a low frequency. When there are | |
6523 | * several CPUs for which the frequency request will be the same, we don't | |
6524 | * have enough data to break the tie between them, because the Energy Model | |
6525 | * only includes active power costs. With this model, if we assume that | |
6526 | * frequency requests follow utilization (e.g. using schedutil), the CPU with | |
6527 | * the maximum spare capacity in a performance domain is guaranteed to be among | |
6528 | * the best candidates of the performance domain. | |
6529 | * | |
6530 | * In practice, it could be preferable from an energy standpoint to pack | |
6531 | * small tasks on a CPU in order to let other CPUs go in deeper idle states, | |
6532 | * but that could also hurt our chances to go cluster idle, and we have no | |
6533 | * ways to tell with the current Energy Model if this is actually a good | |
6534 | * idea or not. So, find_energy_efficient_cpu() basically favors | |
6535 | * cluster-packing, and spreading inside a cluster. That should at least be | |
6536 | * a good thing for latency, and this is consistent with the idea that most | |
6537 | * of the energy savings of EAS come from the asymmetry of the system, and | |
6538 | * not so much from breaking the tie between identical CPUs. That's also the | |
6539 | * reason why EAS is enabled in the topology code only for systems where | |
6540 | * SD_ASYM_CPUCAPACITY is set. | |
6541 | * | |
6542 | * NOTE: Forkees are not accepted in the energy-aware wake-up path because | |
6543 | * they don't have any useful utilization data yet and it's not possible to | |
6544 | * forecast their impact on energy consumption. Consequently, they will be | |
6545 | * placed by find_idlest_cpu() on the least loaded CPU, which might turn out | |
6546 | * to be energy-inefficient in some use-cases. The alternative would be to | |
6547 | * bias new tasks towards specific types of CPUs first, or to try to infer | |
6548 | * their util_avg from the parent task, but those heuristics could hurt | |
6549 | * other use-cases too. So, until someone finds a better way to solve this, | |
6550 | * let's keep things simple by re-using the existing slow path. | |
6551 | */ | |
732cd75b QP |
6552 | static int find_energy_efficient_cpu(struct task_struct *p, int prev_cpu) |
6553 | { | |
eb92692b | 6554 | unsigned long prev_delta = ULONG_MAX, best_delta = ULONG_MAX; |
732cd75b | 6555 | struct root_domain *rd = cpu_rq(smp_processor_id())->rd; |
eb92692b | 6556 | unsigned long cpu_cap, util, base_energy = 0; |
732cd75b | 6557 | int cpu, best_energy_cpu = prev_cpu; |
732cd75b | 6558 | struct sched_domain *sd; |
eb92692b | 6559 | struct perf_domain *pd; |
732cd75b QP |
6560 | |
6561 | rcu_read_lock(); | |
6562 | pd = rcu_dereference(rd->pd); | |
6563 | if (!pd || READ_ONCE(rd->overutilized)) | |
6564 | goto fail; | |
732cd75b QP |
6565 | |
6566 | /* | |
6567 | * Energy-aware wake-up happens on the lowest sched_domain starting | |
6568 | * from sd_asym_cpucapacity spanning over this_cpu and prev_cpu. | |
6569 | */ | |
6570 | sd = rcu_dereference(*this_cpu_ptr(&sd_asym_cpucapacity)); | |
6571 | while (sd && !cpumask_test_cpu(prev_cpu, sched_domain_span(sd))) | |
6572 | sd = sd->parent; | |
6573 | if (!sd) | |
6574 | goto fail; | |
6575 | ||
6576 | sync_entity_load_avg(&p->se); | |
6577 | if (!task_util_est(p)) | |
6578 | goto unlock; | |
6579 | ||
6580 | for (; pd; pd = pd->next) { | |
eb92692b QP |
6581 | unsigned long cur_delta, spare_cap, max_spare_cap = 0; |
6582 | unsigned long base_energy_pd; | |
732cd75b QP |
6583 | int max_spare_cap_cpu = -1; |
6584 | ||
eb92692b QP |
6585 | /* Compute the 'base' energy of the pd, without @p */ |
6586 | base_energy_pd = compute_energy(p, -1, pd); | |
6587 | base_energy += base_energy_pd; | |
6588 | ||
732cd75b | 6589 | for_each_cpu_and(cpu, perf_domain_span(pd), sched_domain_span(sd)) { |
3bd37062 | 6590 | if (!cpumask_test_cpu(cpu, p->cpus_ptr)) |
732cd75b QP |
6591 | continue; |
6592 | ||
732cd75b QP |
6593 | util = cpu_util_next(cpu, p, cpu); |
6594 | cpu_cap = capacity_of(cpu); | |
1d42509e VS |
6595 | spare_cap = cpu_cap - util; |
6596 | ||
6597 | /* | |
6598 | * Skip CPUs that cannot satisfy the capacity request. | |
6599 | * IOW, placing the task there would make the CPU | |
6600 | * overutilized. Take uclamp into account to see how | |
6601 | * much capacity we can get out of the CPU; this is | |
6602 | * aligned with schedutil_cpu_util(). | |
6603 | */ | |
6604 | util = uclamp_rq_util_with(cpu_rq(cpu), util, p); | |
60e17f5c | 6605 | if (!fits_capacity(util, cpu_cap)) |
732cd75b QP |
6606 | continue; |
6607 | ||
6608 | /* Always use prev_cpu as a candidate. */ | |
6609 | if (cpu == prev_cpu) { | |
eb92692b QP |
6610 | prev_delta = compute_energy(p, prev_cpu, pd); |
6611 | prev_delta -= base_energy_pd; | |
6612 | best_delta = min(best_delta, prev_delta); | |
732cd75b QP |
6613 | } |
6614 | ||
6615 | /* | |
6616 | * Find the CPU with the maximum spare capacity in | |
6617 | * the performance domain | |
6618 | */ | |
732cd75b QP |
6619 | if (spare_cap > max_spare_cap) { |
6620 | max_spare_cap = spare_cap; | |
6621 | max_spare_cap_cpu = cpu; | |
6622 | } | |
6623 | } | |
6624 | ||
6625 | /* Evaluate the energy impact of using this CPU. */ | |
4892f51a | 6626 | if (max_spare_cap_cpu >= 0 && max_spare_cap_cpu != prev_cpu) { |
eb92692b QP |
6627 | cur_delta = compute_energy(p, max_spare_cap_cpu, pd); |
6628 | cur_delta -= base_energy_pd; | |
6629 | if (cur_delta < best_delta) { | |
6630 | best_delta = cur_delta; | |
732cd75b QP |
6631 | best_energy_cpu = max_spare_cap_cpu; |
6632 | } | |
6633 | } | |
6634 | } | |
6635 | unlock: | |
6636 | rcu_read_unlock(); | |
6637 | ||
6638 | /* | |
6639 | * Pick the best CPU if prev_cpu cannot be used, or if it saves at | |
6640 | * least 6% of the energy used by prev_cpu. | |
6641 | */ | |
eb92692b | 6642 | if (prev_delta == ULONG_MAX) |
732cd75b QP |
6643 | return best_energy_cpu; |
6644 | ||
eb92692b | 6645 | if ((prev_delta - best_delta) > ((prev_delta + base_energy) >> 4)) |
732cd75b QP |
6646 | return best_energy_cpu; |
6647 | ||
6648 | return prev_cpu; | |
6649 | ||
6650 | fail: | |
6651 | rcu_read_unlock(); | |
6652 | ||
6653 | return -1; | |
6654 | } | |
6655 | ||
aaee1203 | 6656 | /* |
de91b9cb MR |
6657 | * select_task_rq_fair: Select target runqueue for the waking task in domains |
6658 | * that have the 'sd_flag' flag set. In practice, this is SD_BALANCE_WAKE, | |
6659 | * SD_BALANCE_FORK, or SD_BALANCE_EXEC. | |
aaee1203 | 6660 | * |
97fb7a0a IM |
6661 | * Balances load by selecting the idlest CPU in the idlest group, or under |
6662 | * certain conditions an idle sibling CPU if the domain has SD_WAKE_AFFINE set. | |
aaee1203 | 6663 | * |
97fb7a0a | 6664 | * Returns the target CPU number. |
aaee1203 PZ |
6665 | * |
6666 | * preempt must be disabled. | |
6667 | */ | |
0017d735 | 6668 | static int |
ac66f547 | 6669 | select_task_rq_fair(struct task_struct *p, int prev_cpu, int sd_flag, int wake_flags) |
aaee1203 | 6670 | { |
f1d88b44 | 6671 | struct sched_domain *tmp, *sd = NULL; |
c88d5910 | 6672 | int cpu = smp_processor_id(); |
63b0e9ed | 6673 | int new_cpu = prev_cpu; |
99bd5e2f | 6674 | int want_affine = 0; |
24d0c1d6 | 6675 | int sync = (wake_flags & WF_SYNC) && !(current->flags & PF_EXITING); |
c88d5910 | 6676 | |
c58d25f3 PZ |
6677 | if (sd_flag & SD_BALANCE_WAKE) { |
6678 | record_wakee(p); | |
732cd75b | 6679 | |
f8a696f2 | 6680 | if (sched_energy_enabled()) { |
732cd75b QP |
6681 | new_cpu = find_energy_efficient_cpu(p, prev_cpu); |
6682 | if (new_cpu >= 0) | |
6683 | return new_cpu; | |
6684 | new_cpu = prev_cpu; | |
6685 | } | |
6686 | ||
00061968 | 6687 | want_affine = !wake_wide(p) && cpumask_test_cpu(cpu, p->cpus_ptr); |
c58d25f3 | 6688 | } |
aaee1203 | 6689 | |
dce840a0 | 6690 | rcu_read_lock(); |
aaee1203 | 6691 | for_each_domain(cpu, tmp) { |
fe3bcfe1 | 6692 | /* |
97fb7a0a | 6693 | * If both 'cpu' and 'prev_cpu' are part of this domain, |
99bd5e2f | 6694 | * cpu is a valid SD_WAKE_AFFINE target. |
fe3bcfe1 | 6695 | */ |
99bd5e2f SS |
6696 | if (want_affine && (tmp->flags & SD_WAKE_AFFINE) && |
6697 | cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) { | |
f1d88b44 VK |
6698 | if (cpu != prev_cpu) |
6699 | new_cpu = wake_affine(tmp, p, cpu, prev_cpu, sync); | |
6700 | ||
6701 | sd = NULL; /* Prefer wake_affine over balance flags */ | |
29cd8bae | 6702 | break; |
f03542a7 | 6703 | } |
29cd8bae | 6704 | |
f03542a7 | 6705 | if (tmp->flags & sd_flag) |
29cd8bae | 6706 | sd = tmp; |
63b0e9ed MG |
6707 | else if (!want_affine) |
6708 | break; | |
29cd8bae PZ |
6709 | } |
6710 | ||
f1d88b44 VK |
6711 | if (unlikely(sd)) { |
6712 | /* Slow path */ | |
18bd1b4b | 6713 | new_cpu = find_idlest_cpu(sd, p, cpu, prev_cpu, sd_flag); |
f1d88b44 VK |
6714 | } else if (sd_flag & SD_BALANCE_WAKE) { /* XXX always ? */ |
6715 | /* Fast path */ | |
6716 | ||
6717 | new_cpu = select_idle_sibling(p, prev_cpu, new_cpu); | |
6718 | ||
6719 | if (want_affine) | |
6720 | current->recent_used_cpu = cpu; | |
e7693a36 | 6721 | } |
dce840a0 | 6722 | rcu_read_unlock(); |
e7693a36 | 6723 | |
c88d5910 | 6724 | return new_cpu; |
e7693a36 | 6725 | } |
0a74bef8 | 6726 | |
144d8487 PZ |
6727 | static void detach_entity_cfs_rq(struct sched_entity *se); |
6728 | ||
0a74bef8 | 6729 | /* |
97fb7a0a | 6730 | * Called immediately before a task is migrated to a new CPU; task_cpu(p) and |
0a74bef8 | 6731 | * cfs_rq_of(p) references at time of call are still valid and identify the |
97fb7a0a | 6732 | * previous CPU. The caller guarantees p->pi_lock or task_rq(p)->lock is held. |
0a74bef8 | 6733 | */ |
3f9672ba | 6734 | static void migrate_task_rq_fair(struct task_struct *p, int new_cpu) |
0a74bef8 | 6735 | { |
59efa0ba PZ |
6736 | /* |
6737 | * As blocked tasks retain absolute vruntime the migration needs to | |
6738 | * deal with this by subtracting the old and adding the new | |
6739 | * min_vruntime -- the latter is done by enqueue_entity() when placing | |
6740 | * the task on the new runqueue. | |
6741 | */ | |
6742 | if (p->state == TASK_WAKING) { | |
6743 | struct sched_entity *se = &p->se; | |
6744 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
6745 | u64 min_vruntime; | |
6746 | ||
6747 | #ifndef CONFIG_64BIT | |
6748 | u64 min_vruntime_copy; | |
6749 | ||
6750 | do { | |
6751 | min_vruntime_copy = cfs_rq->min_vruntime_copy; | |
6752 | smp_rmb(); | |
6753 | min_vruntime = cfs_rq->min_vruntime; | |
6754 | } while (min_vruntime != min_vruntime_copy); | |
6755 | #else | |
6756 | min_vruntime = cfs_rq->min_vruntime; | |
6757 | #endif | |
6758 | ||
6759 | se->vruntime -= min_vruntime; | |
6760 | } | |
6761 | ||
144d8487 PZ |
6762 | if (p->on_rq == TASK_ON_RQ_MIGRATING) { |
6763 | /* | |
6764 | * In case of TASK_ON_RQ_MIGRATING we in fact hold the 'old' | |
6765 | * rq->lock and can modify state directly. | |
6766 | */ | |
6767 | lockdep_assert_held(&task_rq(p)->lock); | |
6768 | detach_entity_cfs_rq(&p->se); | |
6769 | ||
6770 | } else { | |
6771 | /* | |
6772 | * We are supposed to update the task to "current" time, then | |
6773 | * its up to date and ready to go to new CPU/cfs_rq. But we | |
6774 | * have difficulty in getting what current time is, so simply | |
6775 | * throw away the out-of-date time. This will result in the | |
6776 | * wakee task is less decayed, but giving the wakee more load | |
6777 | * sounds not bad. | |
6778 | */ | |
6779 | remove_entity_load_avg(&p->se); | |
6780 | } | |
9d89c257 YD |
6781 | |
6782 | /* Tell new CPU we are migrated */ | |
6783 | p->se.avg.last_update_time = 0; | |
3944a927 BS |
6784 | |
6785 | /* We have migrated, no longer consider this task hot */ | |
9d89c257 | 6786 | p->se.exec_start = 0; |
3f9672ba SD |
6787 | |
6788 | update_scan_period(p, new_cpu); | |
0a74bef8 | 6789 | } |
12695578 YD |
6790 | |
6791 | static void task_dead_fair(struct task_struct *p) | |
6792 | { | |
6793 | remove_entity_load_avg(&p->se); | |
6794 | } | |
6e2df058 PZ |
6795 | |
6796 | static int | |
6797 | balance_fair(struct rq *rq, struct task_struct *prev, struct rq_flags *rf) | |
6798 | { | |
6799 | if (rq->nr_running) | |
6800 | return 1; | |
6801 | ||
6802 | return newidle_balance(rq, rf) != 0; | |
6803 | } | |
e7693a36 GH |
6804 | #endif /* CONFIG_SMP */ |
6805 | ||
a555e9d8 | 6806 | static unsigned long wakeup_gran(struct sched_entity *se) |
0bbd3336 PZ |
6807 | { |
6808 | unsigned long gran = sysctl_sched_wakeup_granularity; | |
6809 | ||
6810 | /* | |
e52fb7c0 PZ |
6811 | * Since its curr running now, convert the gran from real-time |
6812 | * to virtual-time in his units. | |
13814d42 MG |
6813 | * |
6814 | * By using 'se' instead of 'curr' we penalize light tasks, so | |
6815 | * they get preempted easier. That is, if 'se' < 'curr' then | |
6816 | * the resulting gran will be larger, therefore penalizing the | |
6817 | * lighter, if otoh 'se' > 'curr' then the resulting gran will | |
6818 | * be smaller, again penalizing the lighter task. | |
6819 | * | |
6820 | * This is especially important for buddies when the leftmost | |
6821 | * task is higher priority than the buddy. | |
0bbd3336 | 6822 | */ |
f4ad9bd2 | 6823 | return calc_delta_fair(gran, se); |
0bbd3336 PZ |
6824 | } |
6825 | ||
464b7527 PZ |
6826 | /* |
6827 | * Should 'se' preempt 'curr'. | |
6828 | * | |
6829 | * |s1 | |
6830 | * |s2 | |
6831 | * |s3 | |
6832 | * g | |
6833 | * |<--->|c | |
6834 | * | |
6835 | * w(c, s1) = -1 | |
6836 | * w(c, s2) = 0 | |
6837 | * w(c, s3) = 1 | |
6838 | * | |
6839 | */ | |
6840 | static int | |
6841 | wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se) | |
6842 | { | |
6843 | s64 gran, vdiff = curr->vruntime - se->vruntime; | |
6844 | ||
6845 | if (vdiff <= 0) | |
6846 | return -1; | |
6847 | ||
a555e9d8 | 6848 | gran = wakeup_gran(se); |
464b7527 PZ |
6849 | if (vdiff > gran) |
6850 | return 1; | |
6851 | ||
6852 | return 0; | |
6853 | } | |
6854 | ||
02479099 PZ |
6855 | static void set_last_buddy(struct sched_entity *se) |
6856 | { | |
1da1843f | 6857 | if (entity_is_task(se) && unlikely(task_has_idle_policy(task_of(se)))) |
69c80f3e VP |
6858 | return; |
6859 | ||
c5ae366e DA |
6860 | for_each_sched_entity(se) { |
6861 | if (SCHED_WARN_ON(!se->on_rq)) | |
6862 | return; | |
69c80f3e | 6863 | cfs_rq_of(se)->last = se; |
c5ae366e | 6864 | } |
02479099 PZ |
6865 | } |
6866 | ||
6867 | static void set_next_buddy(struct sched_entity *se) | |
6868 | { | |
1da1843f | 6869 | if (entity_is_task(se) && unlikely(task_has_idle_policy(task_of(se)))) |
69c80f3e VP |
6870 | return; |
6871 | ||
c5ae366e DA |
6872 | for_each_sched_entity(se) { |
6873 | if (SCHED_WARN_ON(!se->on_rq)) | |
6874 | return; | |
69c80f3e | 6875 | cfs_rq_of(se)->next = se; |
c5ae366e | 6876 | } |
02479099 PZ |
6877 | } |
6878 | ||
ac53db59 RR |
6879 | static void set_skip_buddy(struct sched_entity *se) |
6880 | { | |
69c80f3e VP |
6881 | for_each_sched_entity(se) |
6882 | cfs_rq_of(se)->skip = se; | |
ac53db59 RR |
6883 | } |
6884 | ||
bf0f6f24 IM |
6885 | /* |
6886 | * Preempt the current task with a newly woken task if needed: | |
6887 | */ | |
5a9b86f6 | 6888 | static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags) |
bf0f6f24 IM |
6889 | { |
6890 | struct task_struct *curr = rq->curr; | |
8651a86c | 6891 | struct sched_entity *se = &curr->se, *pse = &p->se; |
03e89e45 | 6892 | struct cfs_rq *cfs_rq = task_cfs_rq(curr); |
f685ceac | 6893 | int scale = cfs_rq->nr_running >= sched_nr_latency; |
2f36825b | 6894 | int next_buddy_marked = 0; |
bf0f6f24 | 6895 | |
4ae7d5ce IM |
6896 | if (unlikely(se == pse)) |
6897 | return; | |
6898 | ||
5238cdd3 | 6899 | /* |
163122b7 | 6900 | * This is possible from callers such as attach_tasks(), in which we |
5238cdd3 PT |
6901 | * unconditionally check_prempt_curr() after an enqueue (which may have |
6902 | * lead to a throttle). This both saves work and prevents false | |
6903 | * next-buddy nomination below. | |
6904 | */ | |
6905 | if (unlikely(throttled_hierarchy(cfs_rq_of(pse)))) | |
6906 | return; | |
6907 | ||
2f36825b | 6908 | if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) { |
3cb63d52 | 6909 | set_next_buddy(pse); |
2f36825b VP |
6910 | next_buddy_marked = 1; |
6911 | } | |
57fdc26d | 6912 | |
aec0a514 BR |
6913 | /* |
6914 | * We can come here with TIF_NEED_RESCHED already set from new task | |
6915 | * wake up path. | |
5238cdd3 PT |
6916 | * |
6917 | * Note: this also catches the edge-case of curr being in a throttled | |
6918 | * group (e.g. via set_curr_task), since update_curr() (in the | |
6919 | * enqueue of curr) will have resulted in resched being set. This | |
6920 | * prevents us from potentially nominating it as a false LAST_BUDDY | |
6921 | * below. | |
aec0a514 BR |
6922 | */ |
6923 | if (test_tsk_need_resched(curr)) | |
6924 | return; | |
6925 | ||
a2f5c9ab | 6926 | /* Idle tasks are by definition preempted by non-idle tasks. */ |
1da1843f VK |
6927 | if (unlikely(task_has_idle_policy(curr)) && |
6928 | likely(!task_has_idle_policy(p))) | |
a2f5c9ab DH |
6929 | goto preempt; |
6930 | ||
91c234b4 | 6931 | /* |
a2f5c9ab DH |
6932 | * Batch and idle tasks do not preempt non-idle tasks (their preemption |
6933 | * is driven by the tick): | |
91c234b4 | 6934 | */ |
8ed92e51 | 6935 | if (unlikely(p->policy != SCHED_NORMAL) || !sched_feat(WAKEUP_PREEMPTION)) |
91c234b4 | 6936 | return; |
bf0f6f24 | 6937 | |
464b7527 | 6938 | find_matching_se(&se, &pse); |
9bbd7374 | 6939 | update_curr(cfs_rq_of(se)); |
002f128b | 6940 | BUG_ON(!pse); |
2f36825b VP |
6941 | if (wakeup_preempt_entity(se, pse) == 1) { |
6942 | /* | |
6943 | * Bias pick_next to pick the sched entity that is | |
6944 | * triggering this preemption. | |
6945 | */ | |
6946 | if (!next_buddy_marked) | |
6947 | set_next_buddy(pse); | |
3a7e73a2 | 6948 | goto preempt; |
2f36825b | 6949 | } |
464b7527 | 6950 | |
3a7e73a2 | 6951 | return; |
a65ac745 | 6952 | |
3a7e73a2 | 6953 | preempt: |
8875125e | 6954 | resched_curr(rq); |
3a7e73a2 PZ |
6955 | /* |
6956 | * Only set the backward buddy when the current task is still | |
6957 | * on the rq. This can happen when a wakeup gets interleaved | |
6958 | * with schedule on the ->pre_schedule() or idle_balance() | |
6959 | * point, either of which can * drop the rq lock. | |
6960 | * | |
6961 | * Also, during early boot the idle thread is in the fair class, | |
6962 | * for obvious reasons its a bad idea to schedule back to it. | |
6963 | */ | |
6964 | if (unlikely(!se->on_rq || curr == rq->idle)) | |
6965 | return; | |
6966 | ||
6967 | if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se)) | |
6968 | set_last_buddy(se); | |
bf0f6f24 IM |
6969 | } |
6970 | ||
5d7d6056 | 6971 | struct task_struct * |
d8ac8971 | 6972 | pick_next_task_fair(struct rq *rq, struct task_struct *prev, struct rq_flags *rf) |
bf0f6f24 IM |
6973 | { |
6974 | struct cfs_rq *cfs_rq = &rq->cfs; | |
6975 | struct sched_entity *se; | |
678d5718 | 6976 | struct task_struct *p; |
37e117c0 | 6977 | int new_tasks; |
678d5718 | 6978 | |
6e83125c | 6979 | again: |
6e2df058 | 6980 | if (!sched_fair_runnable(rq)) |
38033c37 | 6981 | goto idle; |
678d5718 | 6982 | |
9674f5ca | 6983 | #ifdef CONFIG_FAIR_GROUP_SCHED |
67692435 | 6984 | if (!prev || prev->sched_class != &fair_sched_class) |
678d5718 PZ |
6985 | goto simple; |
6986 | ||
6987 | /* | |
6988 | * Because of the set_next_buddy() in dequeue_task_fair() it is rather | |
6989 | * likely that a next task is from the same cgroup as the current. | |
6990 | * | |
6991 | * Therefore attempt to avoid putting and setting the entire cgroup | |
6992 | * hierarchy, only change the part that actually changes. | |
6993 | */ | |
6994 | ||
6995 | do { | |
6996 | struct sched_entity *curr = cfs_rq->curr; | |
6997 | ||
6998 | /* | |
6999 | * Since we got here without doing put_prev_entity() we also | |
7000 | * have to consider cfs_rq->curr. If it is still a runnable | |
7001 | * entity, update_curr() will update its vruntime, otherwise | |
7002 | * forget we've ever seen it. | |
7003 | */ | |
54d27365 BS |
7004 | if (curr) { |
7005 | if (curr->on_rq) | |
7006 | update_curr(cfs_rq); | |
7007 | else | |
7008 | curr = NULL; | |
678d5718 | 7009 | |
54d27365 BS |
7010 | /* |
7011 | * This call to check_cfs_rq_runtime() will do the | |
7012 | * throttle and dequeue its entity in the parent(s). | |
9674f5ca | 7013 | * Therefore the nr_running test will indeed |
54d27365 BS |
7014 | * be correct. |
7015 | */ | |
9674f5ca VK |
7016 | if (unlikely(check_cfs_rq_runtime(cfs_rq))) { |
7017 | cfs_rq = &rq->cfs; | |
7018 | ||
7019 | if (!cfs_rq->nr_running) | |
7020 | goto idle; | |
7021 | ||
54d27365 | 7022 | goto simple; |
9674f5ca | 7023 | } |
54d27365 | 7024 | } |
678d5718 PZ |
7025 | |
7026 | se = pick_next_entity(cfs_rq, curr); | |
7027 | cfs_rq = group_cfs_rq(se); | |
7028 | } while (cfs_rq); | |
7029 | ||
7030 | p = task_of(se); | |
7031 | ||
7032 | /* | |
7033 | * Since we haven't yet done put_prev_entity and if the selected task | |
7034 | * is a different task than we started out with, try and touch the | |
7035 | * least amount of cfs_rqs. | |
7036 | */ | |
7037 | if (prev != p) { | |
7038 | struct sched_entity *pse = &prev->se; | |
7039 | ||
7040 | while (!(cfs_rq = is_same_group(se, pse))) { | |
7041 | int se_depth = se->depth; | |
7042 | int pse_depth = pse->depth; | |
7043 | ||
7044 | if (se_depth <= pse_depth) { | |
7045 | put_prev_entity(cfs_rq_of(pse), pse); | |
7046 | pse = parent_entity(pse); | |
7047 | } | |
7048 | if (se_depth >= pse_depth) { | |
7049 | set_next_entity(cfs_rq_of(se), se); | |
7050 | se = parent_entity(se); | |
7051 | } | |
7052 | } | |
7053 | ||
7054 | put_prev_entity(cfs_rq, pse); | |
7055 | set_next_entity(cfs_rq, se); | |
7056 | } | |
7057 | ||
93824900 | 7058 | goto done; |
678d5718 | 7059 | simple: |
678d5718 | 7060 | #endif |
67692435 PZ |
7061 | if (prev) |
7062 | put_prev_task(rq, prev); | |
606dba2e | 7063 | |
bf0f6f24 | 7064 | do { |
678d5718 | 7065 | se = pick_next_entity(cfs_rq, NULL); |
f4b6755f | 7066 | set_next_entity(cfs_rq, se); |
bf0f6f24 IM |
7067 | cfs_rq = group_cfs_rq(se); |
7068 | } while (cfs_rq); | |
7069 | ||
8f4d37ec | 7070 | p = task_of(se); |
678d5718 | 7071 | |
13a453c2 | 7072 | done: __maybe_unused; |
93824900 UR |
7073 | #ifdef CONFIG_SMP |
7074 | /* | |
7075 | * Move the next running task to the front of | |
7076 | * the list, so our cfs_tasks list becomes MRU | |
7077 | * one. | |
7078 | */ | |
7079 | list_move(&p->se.group_node, &rq->cfs_tasks); | |
7080 | #endif | |
7081 | ||
b39e66ea MG |
7082 | if (hrtick_enabled(rq)) |
7083 | hrtick_start_fair(rq, p); | |
8f4d37ec | 7084 | |
3b1baa64 MR |
7085 | update_misfit_status(p, rq); |
7086 | ||
8f4d37ec | 7087 | return p; |
38033c37 PZ |
7088 | |
7089 | idle: | |
67692435 PZ |
7090 | if (!rf) |
7091 | return NULL; | |
7092 | ||
5ba553ef | 7093 | new_tasks = newidle_balance(rq, rf); |
46f69fa3 | 7094 | |
37e117c0 | 7095 | /* |
5ba553ef | 7096 | * Because newidle_balance() releases (and re-acquires) rq->lock, it is |
37e117c0 PZ |
7097 | * possible for any higher priority task to appear. In that case we |
7098 | * must re-start the pick_next_entity() loop. | |
7099 | */ | |
e4aa358b | 7100 | if (new_tasks < 0) |
37e117c0 PZ |
7101 | return RETRY_TASK; |
7102 | ||
e4aa358b | 7103 | if (new_tasks > 0) |
38033c37 | 7104 | goto again; |
38033c37 | 7105 | |
23127296 VG |
7106 | /* |
7107 | * rq is about to be idle, check if we need to update the | |
7108 | * lost_idle_time of clock_pelt | |
7109 | */ | |
7110 | update_idle_rq_clock_pelt(rq); | |
7111 | ||
38033c37 | 7112 | return NULL; |
bf0f6f24 IM |
7113 | } |
7114 | ||
98c2f700 PZ |
7115 | static struct task_struct *__pick_next_task_fair(struct rq *rq) |
7116 | { | |
7117 | return pick_next_task_fair(rq, NULL, NULL); | |
7118 | } | |
7119 | ||
bf0f6f24 IM |
7120 | /* |
7121 | * Account for a descheduled task: | |
7122 | */ | |
6e2df058 | 7123 | static void put_prev_task_fair(struct rq *rq, struct task_struct *prev) |
bf0f6f24 IM |
7124 | { |
7125 | struct sched_entity *se = &prev->se; | |
7126 | struct cfs_rq *cfs_rq; | |
7127 | ||
7128 | for_each_sched_entity(se) { | |
7129 | cfs_rq = cfs_rq_of(se); | |
ab6cde26 | 7130 | put_prev_entity(cfs_rq, se); |
bf0f6f24 IM |
7131 | } |
7132 | } | |
7133 | ||
ac53db59 RR |
7134 | /* |
7135 | * sched_yield() is very simple | |
7136 | * | |
7137 | * The magic of dealing with the ->skip buddy is in pick_next_entity. | |
7138 | */ | |
7139 | static void yield_task_fair(struct rq *rq) | |
7140 | { | |
7141 | struct task_struct *curr = rq->curr; | |
7142 | struct cfs_rq *cfs_rq = task_cfs_rq(curr); | |
7143 | struct sched_entity *se = &curr->se; | |
7144 | ||
7145 | /* | |
7146 | * Are we the only task in the tree? | |
7147 | */ | |
7148 | if (unlikely(rq->nr_running == 1)) | |
7149 | return; | |
7150 | ||
7151 | clear_buddies(cfs_rq, se); | |
7152 | ||
7153 | if (curr->policy != SCHED_BATCH) { | |
7154 | update_rq_clock(rq); | |
7155 | /* | |
7156 | * Update run-time statistics of the 'current'. | |
7157 | */ | |
7158 | update_curr(cfs_rq); | |
916671c0 MG |
7159 | /* |
7160 | * Tell update_rq_clock() that we've just updated, | |
7161 | * so we don't do microscopic update in schedule() | |
7162 | * and double the fastpath cost. | |
7163 | */ | |
adcc8da8 | 7164 | rq_clock_skip_update(rq); |
ac53db59 RR |
7165 | } |
7166 | ||
7167 | set_skip_buddy(se); | |
7168 | } | |
7169 | ||
0900acf2 | 7170 | static bool yield_to_task_fair(struct rq *rq, struct task_struct *p) |
d95f4122 MG |
7171 | { |
7172 | struct sched_entity *se = &p->se; | |
7173 | ||
5238cdd3 PT |
7174 | /* throttled hierarchies are not runnable */ |
7175 | if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se))) | |
d95f4122 MG |
7176 | return false; |
7177 | ||
7178 | /* Tell the scheduler that we'd really like pse to run next. */ | |
7179 | set_next_buddy(se); | |
7180 | ||
d95f4122 MG |
7181 | yield_task_fair(rq); |
7182 | ||
7183 | return true; | |
7184 | } | |
7185 | ||
681f3e68 | 7186 | #ifdef CONFIG_SMP |
bf0f6f24 | 7187 | /************************************************** |
e9c84cb8 PZ |
7188 | * Fair scheduling class load-balancing methods. |
7189 | * | |
7190 | * BASICS | |
7191 | * | |
7192 | * The purpose of load-balancing is to achieve the same basic fairness the | |
97fb7a0a | 7193 | * per-CPU scheduler provides, namely provide a proportional amount of compute |
e9c84cb8 PZ |
7194 | * time to each task. This is expressed in the following equation: |
7195 | * | |
7196 | * W_i,n/P_i == W_j,n/P_j for all i,j (1) | |
7197 | * | |
97fb7a0a | 7198 | * Where W_i,n is the n-th weight average for CPU i. The instantaneous weight |
e9c84cb8 PZ |
7199 | * W_i,0 is defined as: |
7200 | * | |
7201 | * W_i,0 = \Sum_j w_i,j (2) | |
7202 | * | |
97fb7a0a | 7203 | * Where w_i,j is the weight of the j-th runnable task on CPU i. This weight |
1c3de5e1 | 7204 | * is derived from the nice value as per sched_prio_to_weight[]. |
e9c84cb8 PZ |
7205 | * |
7206 | * The weight average is an exponential decay average of the instantaneous | |
7207 | * weight: | |
7208 | * | |
7209 | * W'_i,n = (2^n - 1) / 2^n * W_i,n + 1 / 2^n * W_i,0 (3) | |
7210 | * | |
97fb7a0a | 7211 | * C_i is the compute capacity of CPU i, typically it is the |
e9c84cb8 PZ |
7212 | * fraction of 'recent' time available for SCHED_OTHER task execution. But it |
7213 | * can also include other factors [XXX]. | |
7214 | * | |
7215 | * To achieve this balance we define a measure of imbalance which follows | |
7216 | * directly from (1): | |
7217 | * | |
ced549fa | 7218 | * imb_i,j = max{ avg(W/C), W_i/C_i } - min{ avg(W/C), W_j/C_j } (4) |
e9c84cb8 PZ |
7219 | * |
7220 | * We them move tasks around to minimize the imbalance. In the continuous | |
7221 | * function space it is obvious this converges, in the discrete case we get | |
7222 | * a few fun cases generally called infeasible weight scenarios. | |
7223 | * | |
7224 | * [XXX expand on: | |
7225 | * - infeasible weights; | |
7226 | * - local vs global optima in the discrete case. ] | |
7227 | * | |
7228 | * | |
7229 | * SCHED DOMAINS | |
7230 | * | |
7231 | * In order to solve the imbalance equation (4), and avoid the obvious O(n^2) | |
97fb7a0a | 7232 | * for all i,j solution, we create a tree of CPUs that follows the hardware |
e9c84cb8 | 7233 | * topology where each level pairs two lower groups (or better). This results |
97fb7a0a | 7234 | * in O(log n) layers. Furthermore we reduce the number of CPUs going up the |
e9c84cb8 | 7235 | * tree to only the first of the previous level and we decrease the frequency |
97fb7a0a | 7236 | * of load-balance at each level inv. proportional to the number of CPUs in |
e9c84cb8 PZ |
7237 | * the groups. |
7238 | * | |
7239 | * This yields: | |
7240 | * | |
7241 | * log_2 n 1 n | |
7242 | * \Sum { --- * --- * 2^i } = O(n) (5) | |
7243 | * i = 0 2^i 2^i | |
7244 | * `- size of each group | |
97fb7a0a | 7245 | * | | `- number of CPUs doing load-balance |
e9c84cb8 PZ |
7246 | * | `- freq |
7247 | * `- sum over all levels | |
7248 | * | |
7249 | * Coupled with a limit on how many tasks we can migrate every balance pass, | |
7250 | * this makes (5) the runtime complexity of the balancer. | |
7251 | * | |
7252 | * An important property here is that each CPU is still (indirectly) connected | |
97fb7a0a | 7253 | * to every other CPU in at most O(log n) steps: |
e9c84cb8 PZ |
7254 | * |
7255 | * The adjacency matrix of the resulting graph is given by: | |
7256 | * | |
97a7142f | 7257 | * log_2 n |
e9c84cb8 PZ |
7258 | * A_i,j = \Union (i % 2^k == 0) && i / 2^(k+1) == j / 2^(k+1) (6) |
7259 | * k = 0 | |
7260 | * | |
7261 | * And you'll find that: | |
7262 | * | |
7263 | * A^(log_2 n)_i,j != 0 for all i,j (7) | |
7264 | * | |
97fb7a0a | 7265 | * Showing there's indeed a path between every CPU in at most O(log n) steps. |
e9c84cb8 PZ |
7266 | * The task movement gives a factor of O(m), giving a convergence complexity |
7267 | * of: | |
7268 | * | |
7269 | * O(nm log n), n := nr_cpus, m := nr_tasks (8) | |
7270 | * | |
7271 | * | |
7272 | * WORK CONSERVING | |
7273 | * | |
7274 | * In order to avoid CPUs going idle while there's still work to do, new idle | |
97fb7a0a | 7275 | * balancing is more aggressive and has the newly idle CPU iterate up the domain |
e9c84cb8 PZ |
7276 | * tree itself instead of relying on other CPUs to bring it work. |
7277 | * | |
7278 | * This adds some complexity to both (5) and (8) but it reduces the total idle | |
7279 | * time. | |
7280 | * | |
7281 | * [XXX more?] | |
7282 | * | |
7283 | * | |
7284 | * CGROUPS | |
7285 | * | |
7286 | * Cgroups make a horror show out of (2), instead of a simple sum we get: | |
7287 | * | |
7288 | * s_k,i | |
7289 | * W_i,0 = \Sum_j \Prod_k w_k * ----- (9) | |
7290 | * S_k | |
7291 | * | |
7292 | * Where | |
7293 | * | |
7294 | * s_k,i = \Sum_j w_i,j,k and S_k = \Sum_i s_k,i (10) | |
7295 | * | |
97fb7a0a | 7296 | * w_i,j,k is the weight of the j-th runnable task in the k-th cgroup on CPU i. |
e9c84cb8 PZ |
7297 | * |
7298 | * The big problem is S_k, its a global sum needed to compute a local (W_i) | |
7299 | * property. | |
7300 | * | |
7301 | * [XXX write more on how we solve this.. _after_ merging pjt's patches that | |
7302 | * rewrite all of this once again.] | |
97a7142f | 7303 | */ |
bf0f6f24 | 7304 | |
ed387b78 HS |
7305 | static unsigned long __read_mostly max_load_balance_interval = HZ/10; |
7306 | ||
0ec8aa00 PZ |
7307 | enum fbq_type { regular, remote, all }; |
7308 | ||
0b0695f2 | 7309 | /* |
a9723389 VG |
7310 | * 'group_type' describes the group of CPUs at the moment of load balancing. |
7311 | * | |
0b0695f2 | 7312 | * The enum is ordered by pulling priority, with the group with lowest priority |
a9723389 VG |
7313 | * first so the group_type can simply be compared when selecting the busiest |
7314 | * group. See update_sd_pick_busiest(). | |
0b0695f2 | 7315 | */ |
3b1baa64 | 7316 | enum group_type { |
a9723389 | 7317 | /* The group has spare capacity that can be used to run more tasks. */ |
0b0695f2 | 7318 | group_has_spare = 0, |
a9723389 VG |
7319 | /* |
7320 | * The group is fully used and the tasks don't compete for more CPU | |
7321 | * cycles. Nevertheless, some tasks might wait before running. | |
7322 | */ | |
0b0695f2 | 7323 | group_fully_busy, |
a9723389 VG |
7324 | /* |
7325 | * SD_ASYM_CPUCAPACITY only: One task doesn't fit with CPU's capacity | |
7326 | * and must be migrated to a more powerful CPU. | |
7327 | */ | |
3b1baa64 | 7328 | group_misfit_task, |
a9723389 VG |
7329 | /* |
7330 | * SD_ASYM_PACKING only: One local CPU with higher capacity is available, | |
7331 | * and the task should be migrated to it instead of running on the | |
7332 | * current CPU. | |
7333 | */ | |
0b0695f2 | 7334 | group_asym_packing, |
a9723389 VG |
7335 | /* |
7336 | * The tasks' affinity constraints previously prevented the scheduler | |
7337 | * from balancing the load across the system. | |
7338 | */ | |
3b1baa64 | 7339 | group_imbalanced, |
a9723389 VG |
7340 | /* |
7341 | * The CPU is overloaded and can't provide expected CPU cycles to all | |
7342 | * tasks. | |
7343 | */ | |
0b0695f2 VG |
7344 | group_overloaded |
7345 | }; | |
7346 | ||
7347 | enum migration_type { | |
7348 | migrate_load = 0, | |
7349 | migrate_util, | |
7350 | migrate_task, | |
7351 | migrate_misfit | |
3b1baa64 MR |
7352 | }; |
7353 | ||
ddcdf6e7 | 7354 | #define LBF_ALL_PINNED 0x01 |
367456c7 | 7355 | #define LBF_NEED_BREAK 0x02 |
6263322c PZ |
7356 | #define LBF_DST_PINNED 0x04 |
7357 | #define LBF_SOME_PINNED 0x08 | |
e022e0d3 | 7358 | #define LBF_NOHZ_STATS 0x10 |
f643ea22 | 7359 | #define LBF_NOHZ_AGAIN 0x20 |
ddcdf6e7 PZ |
7360 | |
7361 | struct lb_env { | |
7362 | struct sched_domain *sd; | |
7363 | ||
ddcdf6e7 | 7364 | struct rq *src_rq; |
85c1e7da | 7365 | int src_cpu; |
ddcdf6e7 PZ |
7366 | |
7367 | int dst_cpu; | |
7368 | struct rq *dst_rq; | |
7369 | ||
88b8dac0 SV |
7370 | struct cpumask *dst_grpmask; |
7371 | int new_dst_cpu; | |
ddcdf6e7 | 7372 | enum cpu_idle_type idle; |
bd939f45 | 7373 | long imbalance; |
b9403130 MW |
7374 | /* The set of CPUs under consideration for load-balancing */ |
7375 | struct cpumask *cpus; | |
7376 | ||
ddcdf6e7 | 7377 | unsigned int flags; |
367456c7 PZ |
7378 | |
7379 | unsigned int loop; | |
7380 | unsigned int loop_break; | |
7381 | unsigned int loop_max; | |
0ec8aa00 PZ |
7382 | |
7383 | enum fbq_type fbq_type; | |
0b0695f2 | 7384 | enum migration_type migration_type; |
163122b7 | 7385 | struct list_head tasks; |
ddcdf6e7 PZ |
7386 | }; |
7387 | ||
029632fb PZ |
7388 | /* |
7389 | * Is this task likely cache-hot: | |
7390 | */ | |
5d5e2b1b | 7391 | static int task_hot(struct task_struct *p, struct lb_env *env) |
029632fb PZ |
7392 | { |
7393 | s64 delta; | |
7394 | ||
e5673f28 KT |
7395 | lockdep_assert_held(&env->src_rq->lock); |
7396 | ||
029632fb PZ |
7397 | if (p->sched_class != &fair_sched_class) |
7398 | return 0; | |
7399 | ||
1da1843f | 7400 | if (unlikely(task_has_idle_policy(p))) |
029632fb PZ |
7401 | return 0; |
7402 | ||
7403 | /* | |
7404 | * Buddy candidates are cache hot: | |
7405 | */ | |
5d5e2b1b | 7406 | if (sched_feat(CACHE_HOT_BUDDY) && env->dst_rq->nr_running && |
029632fb PZ |
7407 | (&p->se == cfs_rq_of(&p->se)->next || |
7408 | &p->se == cfs_rq_of(&p->se)->last)) | |
7409 | return 1; | |
7410 | ||
7411 | if (sysctl_sched_migration_cost == -1) | |
7412 | return 1; | |
7413 | if (sysctl_sched_migration_cost == 0) | |
7414 | return 0; | |
7415 | ||
5d5e2b1b | 7416 | delta = rq_clock_task(env->src_rq) - p->se.exec_start; |
029632fb PZ |
7417 | |
7418 | return delta < (s64)sysctl_sched_migration_cost; | |
7419 | } | |
7420 | ||
3a7053b3 | 7421 | #ifdef CONFIG_NUMA_BALANCING |
c1ceac62 | 7422 | /* |
2a1ed24c SD |
7423 | * Returns 1, if task migration degrades locality |
7424 | * Returns 0, if task migration improves locality i.e migration preferred. | |
7425 | * Returns -1, if task migration is not affected by locality. | |
c1ceac62 | 7426 | */ |
2a1ed24c | 7427 | static int migrate_degrades_locality(struct task_struct *p, struct lb_env *env) |
3a7053b3 | 7428 | { |
b1ad065e | 7429 | struct numa_group *numa_group = rcu_dereference(p->numa_group); |
f35678b6 SD |
7430 | unsigned long src_weight, dst_weight; |
7431 | int src_nid, dst_nid, dist; | |
3a7053b3 | 7432 | |
2a595721 | 7433 | if (!static_branch_likely(&sched_numa_balancing)) |
2a1ed24c SD |
7434 | return -1; |
7435 | ||
c3b9bc5b | 7436 | if (!p->numa_faults || !(env->sd->flags & SD_NUMA)) |
2a1ed24c | 7437 | return -1; |
7a0f3083 MG |
7438 | |
7439 | src_nid = cpu_to_node(env->src_cpu); | |
7440 | dst_nid = cpu_to_node(env->dst_cpu); | |
7441 | ||
83e1d2cd | 7442 | if (src_nid == dst_nid) |
2a1ed24c | 7443 | return -1; |
7a0f3083 | 7444 | |
2a1ed24c SD |
7445 | /* Migrating away from the preferred node is always bad. */ |
7446 | if (src_nid == p->numa_preferred_nid) { | |
7447 | if (env->src_rq->nr_running > env->src_rq->nr_preferred_running) | |
7448 | return 1; | |
7449 | else | |
7450 | return -1; | |
7451 | } | |
b1ad065e | 7452 | |
c1ceac62 RR |
7453 | /* Encourage migration to the preferred node. */ |
7454 | if (dst_nid == p->numa_preferred_nid) | |
2a1ed24c | 7455 | return 0; |
b1ad065e | 7456 | |
739294fb | 7457 | /* Leaving a core idle is often worse than degrading locality. */ |
f35678b6 | 7458 | if (env->idle == CPU_IDLE) |
739294fb RR |
7459 | return -1; |
7460 | ||
f35678b6 | 7461 | dist = node_distance(src_nid, dst_nid); |
c1ceac62 | 7462 | if (numa_group) { |
f35678b6 SD |
7463 | src_weight = group_weight(p, src_nid, dist); |
7464 | dst_weight = group_weight(p, dst_nid, dist); | |
c1ceac62 | 7465 | } else { |
f35678b6 SD |
7466 | src_weight = task_weight(p, src_nid, dist); |
7467 | dst_weight = task_weight(p, dst_nid, dist); | |
b1ad065e RR |
7468 | } |
7469 | ||
f35678b6 | 7470 | return dst_weight < src_weight; |
7a0f3083 MG |
7471 | } |
7472 | ||
3a7053b3 | 7473 | #else |
2a1ed24c | 7474 | static inline int migrate_degrades_locality(struct task_struct *p, |
3a7053b3 MG |
7475 | struct lb_env *env) |
7476 | { | |
2a1ed24c | 7477 | return -1; |
7a0f3083 | 7478 | } |
3a7053b3 MG |
7479 | #endif |
7480 | ||
1e3c88bd PZ |
7481 | /* |
7482 | * can_migrate_task - may task p from runqueue rq be migrated to this_cpu? | |
7483 | */ | |
7484 | static | |
8e45cb54 | 7485 | int can_migrate_task(struct task_struct *p, struct lb_env *env) |
1e3c88bd | 7486 | { |
2a1ed24c | 7487 | int tsk_cache_hot; |
e5673f28 KT |
7488 | |
7489 | lockdep_assert_held(&env->src_rq->lock); | |
7490 | ||
1e3c88bd PZ |
7491 | /* |
7492 | * We do not migrate tasks that are: | |
d3198084 | 7493 | * 1) throttled_lb_pair, or |
3bd37062 | 7494 | * 2) cannot be migrated to this CPU due to cpus_ptr, or |
d3198084 JK |
7495 | * 3) running (obviously), or |
7496 | * 4) are cache-hot on their current CPU. | |
1e3c88bd | 7497 | */ |
d3198084 JK |
7498 | if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu)) |
7499 | return 0; | |
7500 | ||
3bd37062 | 7501 | if (!cpumask_test_cpu(env->dst_cpu, p->cpus_ptr)) { |
e02e60c1 | 7502 | int cpu; |
88b8dac0 | 7503 | |
ae92882e | 7504 | schedstat_inc(p->se.statistics.nr_failed_migrations_affine); |
88b8dac0 | 7505 | |
6263322c PZ |
7506 | env->flags |= LBF_SOME_PINNED; |
7507 | ||
88b8dac0 | 7508 | /* |
97fb7a0a | 7509 | * Remember if this task can be migrated to any other CPU in |
88b8dac0 SV |
7510 | * our sched_group. We may want to revisit it if we couldn't |
7511 | * meet load balance goals by pulling other tasks on src_cpu. | |
7512 | * | |
65a4433a JH |
7513 | * Avoid computing new_dst_cpu for NEWLY_IDLE or if we have |
7514 | * already computed one in current iteration. | |
88b8dac0 | 7515 | */ |
65a4433a | 7516 | if (env->idle == CPU_NEWLY_IDLE || (env->flags & LBF_DST_PINNED)) |
88b8dac0 SV |
7517 | return 0; |
7518 | ||
97fb7a0a | 7519 | /* Prevent to re-select dst_cpu via env's CPUs: */ |
e02e60c1 | 7520 | for_each_cpu_and(cpu, env->dst_grpmask, env->cpus) { |
3bd37062 | 7521 | if (cpumask_test_cpu(cpu, p->cpus_ptr)) { |
6263322c | 7522 | env->flags |= LBF_DST_PINNED; |
e02e60c1 JK |
7523 | env->new_dst_cpu = cpu; |
7524 | break; | |
7525 | } | |
88b8dac0 | 7526 | } |
e02e60c1 | 7527 | |
1e3c88bd PZ |
7528 | return 0; |
7529 | } | |
88b8dac0 SV |
7530 | |
7531 | /* Record that we found atleast one task that could run on dst_cpu */ | |
8e45cb54 | 7532 | env->flags &= ~LBF_ALL_PINNED; |
1e3c88bd | 7533 | |
ddcdf6e7 | 7534 | if (task_running(env->src_rq, p)) { |
ae92882e | 7535 | schedstat_inc(p->se.statistics.nr_failed_migrations_running); |
1e3c88bd PZ |
7536 | return 0; |
7537 | } | |
7538 | ||
7539 | /* | |
7540 | * Aggressive migration if: | |
3a7053b3 MG |
7541 | * 1) destination numa is preferred |
7542 | * 2) task is cache cold, or | |
7543 | * 3) too many balance attempts have failed. | |
1e3c88bd | 7544 | */ |
2a1ed24c SD |
7545 | tsk_cache_hot = migrate_degrades_locality(p, env); |
7546 | if (tsk_cache_hot == -1) | |
7547 | tsk_cache_hot = task_hot(p, env); | |
3a7053b3 | 7548 | |
2a1ed24c | 7549 | if (tsk_cache_hot <= 0 || |
7a96c231 | 7550 | env->sd->nr_balance_failed > env->sd->cache_nice_tries) { |
2a1ed24c | 7551 | if (tsk_cache_hot == 1) { |
ae92882e JP |
7552 | schedstat_inc(env->sd->lb_hot_gained[env->idle]); |
7553 | schedstat_inc(p->se.statistics.nr_forced_migrations); | |
3a7053b3 | 7554 | } |
1e3c88bd PZ |
7555 | return 1; |
7556 | } | |
7557 | ||
ae92882e | 7558 | schedstat_inc(p->se.statistics.nr_failed_migrations_hot); |
4e2dcb73 | 7559 | return 0; |
1e3c88bd PZ |
7560 | } |
7561 | ||
897c395f | 7562 | /* |
163122b7 KT |
7563 | * detach_task() -- detach the task for the migration specified in env |
7564 | */ | |
7565 | static void detach_task(struct task_struct *p, struct lb_env *env) | |
7566 | { | |
7567 | lockdep_assert_held(&env->src_rq->lock); | |
7568 | ||
5704ac0a | 7569 | deactivate_task(env->src_rq, p, DEQUEUE_NOCLOCK); |
163122b7 KT |
7570 | set_task_cpu(p, env->dst_cpu); |
7571 | } | |
7572 | ||
897c395f | 7573 | /* |
e5673f28 | 7574 | * detach_one_task() -- tries to dequeue exactly one task from env->src_rq, as |
897c395f | 7575 | * part of active balancing operations within "domain". |
897c395f | 7576 | * |
e5673f28 | 7577 | * Returns a task if successful and NULL otherwise. |
897c395f | 7578 | */ |
e5673f28 | 7579 | static struct task_struct *detach_one_task(struct lb_env *env) |
897c395f | 7580 | { |
93824900 | 7581 | struct task_struct *p; |
897c395f | 7582 | |
e5673f28 KT |
7583 | lockdep_assert_held(&env->src_rq->lock); |
7584 | ||
93824900 UR |
7585 | list_for_each_entry_reverse(p, |
7586 | &env->src_rq->cfs_tasks, se.group_node) { | |
367456c7 PZ |
7587 | if (!can_migrate_task(p, env)) |
7588 | continue; | |
897c395f | 7589 | |
163122b7 | 7590 | detach_task(p, env); |
e5673f28 | 7591 | |
367456c7 | 7592 | /* |
e5673f28 | 7593 | * Right now, this is only the second place where |
163122b7 | 7594 | * lb_gained[env->idle] is updated (other is detach_tasks) |
e5673f28 | 7595 | * so we can safely collect stats here rather than |
163122b7 | 7596 | * inside detach_tasks(). |
367456c7 | 7597 | */ |
ae92882e | 7598 | schedstat_inc(env->sd->lb_gained[env->idle]); |
e5673f28 | 7599 | return p; |
897c395f | 7600 | } |
e5673f28 | 7601 | return NULL; |
897c395f PZ |
7602 | } |
7603 | ||
eb95308e PZ |
7604 | static const unsigned int sched_nr_migrate_break = 32; |
7605 | ||
5d6523eb | 7606 | /* |
0b0695f2 | 7607 | * detach_tasks() -- tries to detach up to imbalance load/util/tasks from |
163122b7 | 7608 | * busiest_rq, as part of a balancing operation within domain "sd". |
5d6523eb | 7609 | * |
163122b7 | 7610 | * Returns number of detached tasks if successful and 0 otherwise. |
5d6523eb | 7611 | */ |
163122b7 | 7612 | static int detach_tasks(struct lb_env *env) |
1e3c88bd | 7613 | { |
5d6523eb | 7614 | struct list_head *tasks = &env->src_rq->cfs_tasks; |
0b0695f2 | 7615 | unsigned long util, load; |
5d6523eb | 7616 | struct task_struct *p; |
163122b7 KT |
7617 | int detached = 0; |
7618 | ||
7619 | lockdep_assert_held(&env->src_rq->lock); | |
1e3c88bd | 7620 | |
bd939f45 | 7621 | if (env->imbalance <= 0) |
5d6523eb | 7622 | return 0; |
1e3c88bd | 7623 | |
5d6523eb | 7624 | while (!list_empty(tasks)) { |
985d3a4c YD |
7625 | /* |
7626 | * We don't want to steal all, otherwise we may be treated likewise, | |
7627 | * which could at worst lead to a livelock crash. | |
7628 | */ | |
7629 | if (env->idle != CPU_NOT_IDLE && env->src_rq->nr_running <= 1) | |
7630 | break; | |
7631 | ||
93824900 | 7632 | p = list_last_entry(tasks, struct task_struct, se.group_node); |
1e3c88bd | 7633 | |
367456c7 PZ |
7634 | env->loop++; |
7635 | /* We've more or less seen every task there is, call it quits */ | |
5d6523eb | 7636 | if (env->loop > env->loop_max) |
367456c7 | 7637 | break; |
5d6523eb PZ |
7638 | |
7639 | /* take a breather every nr_migrate tasks */ | |
367456c7 | 7640 | if (env->loop > env->loop_break) { |
eb95308e | 7641 | env->loop_break += sched_nr_migrate_break; |
8e45cb54 | 7642 | env->flags |= LBF_NEED_BREAK; |
ee00e66f | 7643 | break; |
a195f004 | 7644 | } |
1e3c88bd | 7645 | |
d3198084 | 7646 | if (!can_migrate_task(p, env)) |
367456c7 PZ |
7647 | goto next; |
7648 | ||
0b0695f2 VG |
7649 | switch (env->migration_type) { |
7650 | case migrate_load: | |
7651 | load = task_h_load(p); | |
5d6523eb | 7652 | |
0b0695f2 VG |
7653 | if (sched_feat(LB_MIN) && |
7654 | load < 16 && !env->sd->nr_balance_failed) | |
7655 | goto next; | |
367456c7 | 7656 | |
6cf82d55 VG |
7657 | /* |
7658 | * Make sure that we don't migrate too much load. | |
7659 | * Nevertheless, let relax the constraint if | |
7660 | * scheduler fails to find a good waiting task to | |
7661 | * migrate. | |
7662 | */ | |
7663 | if (load/2 > env->imbalance && | |
7664 | env->sd->nr_balance_failed <= env->sd->cache_nice_tries) | |
0b0695f2 VG |
7665 | goto next; |
7666 | ||
7667 | env->imbalance -= load; | |
7668 | break; | |
7669 | ||
7670 | case migrate_util: | |
7671 | util = task_util_est(p); | |
7672 | ||
7673 | if (util > env->imbalance) | |
7674 | goto next; | |
7675 | ||
7676 | env->imbalance -= util; | |
7677 | break; | |
7678 | ||
7679 | case migrate_task: | |
7680 | env->imbalance--; | |
7681 | break; | |
7682 | ||
7683 | case migrate_misfit: | |
c63be7be VG |
7684 | /* This is not a misfit task */ |
7685 | if (task_fits_capacity(p, capacity_of(env->src_cpu))) | |
0b0695f2 VG |
7686 | goto next; |
7687 | ||
7688 | env->imbalance = 0; | |
7689 | break; | |
7690 | } | |
1e3c88bd | 7691 | |
163122b7 KT |
7692 | detach_task(p, env); |
7693 | list_add(&p->se.group_node, &env->tasks); | |
7694 | ||
7695 | detached++; | |
1e3c88bd | 7696 | |
c1a280b6 | 7697 | #ifdef CONFIG_PREEMPTION |
ee00e66f PZ |
7698 | /* |
7699 | * NEWIDLE balancing is a source of latency, so preemptible | |
163122b7 | 7700 | * kernels will stop after the first task is detached to minimize |
ee00e66f PZ |
7701 | * the critical section. |
7702 | */ | |
5d6523eb | 7703 | if (env->idle == CPU_NEWLY_IDLE) |
ee00e66f | 7704 | break; |
1e3c88bd PZ |
7705 | #endif |
7706 | ||
ee00e66f PZ |
7707 | /* |
7708 | * We only want to steal up to the prescribed amount of | |
0b0695f2 | 7709 | * load/util/tasks. |
ee00e66f | 7710 | */ |
bd939f45 | 7711 | if (env->imbalance <= 0) |
ee00e66f | 7712 | break; |
367456c7 PZ |
7713 | |
7714 | continue; | |
7715 | next: | |
93824900 | 7716 | list_move(&p->se.group_node, tasks); |
1e3c88bd | 7717 | } |
5d6523eb | 7718 | |
1e3c88bd | 7719 | /* |
163122b7 KT |
7720 | * Right now, this is one of only two places we collect this stat |
7721 | * so we can safely collect detach_one_task() stats here rather | |
7722 | * than inside detach_one_task(). | |
1e3c88bd | 7723 | */ |
ae92882e | 7724 | schedstat_add(env->sd->lb_gained[env->idle], detached); |
1e3c88bd | 7725 | |
163122b7 KT |
7726 | return detached; |
7727 | } | |
7728 | ||
7729 | /* | |
7730 | * attach_task() -- attach the task detached by detach_task() to its new rq. | |
7731 | */ | |
7732 | static void attach_task(struct rq *rq, struct task_struct *p) | |
7733 | { | |
7734 | lockdep_assert_held(&rq->lock); | |
7735 | ||
7736 | BUG_ON(task_rq(p) != rq); | |
5704ac0a | 7737 | activate_task(rq, p, ENQUEUE_NOCLOCK); |
163122b7 KT |
7738 | check_preempt_curr(rq, p, 0); |
7739 | } | |
7740 | ||
7741 | /* | |
7742 | * attach_one_task() -- attaches the task returned from detach_one_task() to | |
7743 | * its new rq. | |
7744 | */ | |
7745 | static void attach_one_task(struct rq *rq, struct task_struct *p) | |
7746 | { | |
8a8c69c3 PZ |
7747 | struct rq_flags rf; |
7748 | ||
7749 | rq_lock(rq, &rf); | |
5704ac0a | 7750 | update_rq_clock(rq); |
163122b7 | 7751 | attach_task(rq, p); |
8a8c69c3 | 7752 | rq_unlock(rq, &rf); |
163122b7 KT |
7753 | } |
7754 | ||
7755 | /* | |
7756 | * attach_tasks() -- attaches all tasks detached by detach_tasks() to their | |
7757 | * new rq. | |
7758 | */ | |
7759 | static void attach_tasks(struct lb_env *env) | |
7760 | { | |
7761 | struct list_head *tasks = &env->tasks; | |
7762 | struct task_struct *p; | |
8a8c69c3 | 7763 | struct rq_flags rf; |
163122b7 | 7764 | |
8a8c69c3 | 7765 | rq_lock(env->dst_rq, &rf); |
5704ac0a | 7766 | update_rq_clock(env->dst_rq); |
163122b7 KT |
7767 | |
7768 | while (!list_empty(tasks)) { | |
7769 | p = list_first_entry(tasks, struct task_struct, se.group_node); | |
7770 | list_del_init(&p->se.group_node); | |
1e3c88bd | 7771 | |
163122b7 KT |
7772 | attach_task(env->dst_rq, p); |
7773 | } | |
7774 | ||
8a8c69c3 | 7775 | rq_unlock(env->dst_rq, &rf); |
1e3c88bd PZ |
7776 | } |
7777 | ||
b0c79224 | 7778 | #ifdef CONFIG_NO_HZ_COMMON |
1936c53c VG |
7779 | static inline bool cfs_rq_has_blocked(struct cfs_rq *cfs_rq) |
7780 | { | |
7781 | if (cfs_rq->avg.load_avg) | |
7782 | return true; | |
7783 | ||
7784 | if (cfs_rq->avg.util_avg) | |
7785 | return true; | |
7786 | ||
7787 | return false; | |
7788 | } | |
7789 | ||
91c27493 | 7790 | static inline bool others_have_blocked(struct rq *rq) |
371bf427 VG |
7791 | { |
7792 | if (READ_ONCE(rq->avg_rt.util_avg)) | |
7793 | return true; | |
7794 | ||
3727e0e1 VG |
7795 | if (READ_ONCE(rq->avg_dl.util_avg)) |
7796 | return true; | |
7797 | ||
b4eccf5f TG |
7798 | if (thermal_load_avg(rq)) |
7799 | return true; | |
7800 | ||
11d4afd4 | 7801 | #ifdef CONFIG_HAVE_SCHED_AVG_IRQ |
91c27493 VG |
7802 | if (READ_ONCE(rq->avg_irq.util_avg)) |
7803 | return true; | |
7804 | #endif | |
7805 | ||
371bf427 VG |
7806 | return false; |
7807 | } | |
7808 | ||
b0c79224 VS |
7809 | static inline void update_blocked_load_status(struct rq *rq, bool has_blocked) |
7810 | { | |
7811 | rq->last_blocked_load_update_tick = jiffies; | |
7812 | ||
7813 | if (!has_blocked) | |
7814 | rq->has_blocked_load = 0; | |
7815 | } | |
7816 | #else | |
7817 | static inline bool cfs_rq_has_blocked(struct cfs_rq *cfs_rq) { return false; } | |
7818 | static inline bool others_have_blocked(struct rq *rq) { return false; } | |
7819 | static inline void update_blocked_load_status(struct rq *rq, bool has_blocked) {} | |
7820 | #endif | |
7821 | ||
bef69dd8 VG |
7822 | static bool __update_blocked_others(struct rq *rq, bool *done) |
7823 | { | |
7824 | const struct sched_class *curr_class; | |
7825 | u64 now = rq_clock_pelt(rq); | |
b4eccf5f | 7826 | unsigned long thermal_pressure; |
bef69dd8 VG |
7827 | bool decayed; |
7828 | ||
7829 | /* | |
7830 | * update_load_avg() can call cpufreq_update_util(). Make sure that RT, | |
7831 | * DL and IRQ signals have been updated before updating CFS. | |
7832 | */ | |
7833 | curr_class = rq->curr->sched_class; | |
7834 | ||
b4eccf5f TG |
7835 | thermal_pressure = arch_scale_thermal_pressure(cpu_of(rq)); |
7836 | ||
bef69dd8 VG |
7837 | decayed = update_rt_rq_load_avg(now, rq, curr_class == &rt_sched_class) | |
7838 | update_dl_rq_load_avg(now, rq, curr_class == &dl_sched_class) | | |
05289b90 | 7839 | update_thermal_load_avg(rq_clock_thermal(rq), rq, thermal_pressure) | |
bef69dd8 VG |
7840 | update_irq_load_avg(rq, 0); |
7841 | ||
7842 | if (others_have_blocked(rq)) | |
7843 | *done = false; | |
7844 | ||
7845 | return decayed; | |
7846 | } | |
7847 | ||
1936c53c VG |
7848 | #ifdef CONFIG_FAIR_GROUP_SCHED |
7849 | ||
039ae8bc VG |
7850 | static inline bool cfs_rq_is_decayed(struct cfs_rq *cfs_rq) |
7851 | { | |
7852 | if (cfs_rq->load.weight) | |
7853 | return false; | |
7854 | ||
7855 | if (cfs_rq->avg.load_sum) | |
7856 | return false; | |
7857 | ||
7858 | if (cfs_rq->avg.util_sum) | |
7859 | return false; | |
7860 | ||
9f683953 VG |
7861 | if (cfs_rq->avg.runnable_sum) |
7862 | return false; | |
7863 | ||
039ae8bc VG |
7864 | return true; |
7865 | } | |
7866 | ||
bef69dd8 | 7867 | static bool __update_blocked_fair(struct rq *rq, bool *done) |
9e3081ca | 7868 | { |
039ae8bc | 7869 | struct cfs_rq *cfs_rq, *pos; |
bef69dd8 VG |
7870 | bool decayed = false; |
7871 | int cpu = cpu_of(rq); | |
b90f7c9d | 7872 | |
9763b67f PZ |
7873 | /* |
7874 | * Iterates the task_group tree in a bottom up fashion, see | |
7875 | * list_add_leaf_cfs_rq() for details. | |
7876 | */ | |
039ae8bc | 7877 | for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) { |
bc427898 VG |
7878 | struct sched_entity *se; |
7879 | ||
bef69dd8 | 7880 | if (update_cfs_rq_load_avg(cfs_rq_clock_pelt(cfs_rq), cfs_rq)) { |
9d89c257 | 7881 | update_tg_load_avg(cfs_rq, 0); |
4e516076 | 7882 | |
bef69dd8 VG |
7883 | if (cfs_rq == &rq->cfs) |
7884 | decayed = true; | |
7885 | } | |
7886 | ||
bc427898 VG |
7887 | /* Propagate pending load changes to the parent, if any: */ |
7888 | se = cfs_rq->tg->se[cpu]; | |
7889 | if (se && !skip_blocked_update(se)) | |
88c0616e | 7890 | update_load_avg(cfs_rq_of(se), se, 0); |
a9e7f654 | 7891 | |
039ae8bc VG |
7892 | /* |
7893 | * There can be a lot of idle CPU cgroups. Don't let fully | |
7894 | * decayed cfs_rqs linger on the list. | |
7895 | */ | |
7896 | if (cfs_rq_is_decayed(cfs_rq)) | |
7897 | list_del_leaf_cfs_rq(cfs_rq); | |
7898 | ||
1936c53c VG |
7899 | /* Don't need periodic decay once load/util_avg are null */ |
7900 | if (cfs_rq_has_blocked(cfs_rq)) | |
bef69dd8 | 7901 | *done = false; |
9d89c257 | 7902 | } |
12b04875 | 7903 | |
bef69dd8 | 7904 | return decayed; |
9e3081ca PZ |
7905 | } |
7906 | ||
9763b67f | 7907 | /* |
68520796 | 7908 | * Compute the hierarchical load factor for cfs_rq and all its ascendants. |
9763b67f PZ |
7909 | * This needs to be done in a top-down fashion because the load of a child |
7910 | * group is a fraction of its parents load. | |
7911 | */ | |
68520796 | 7912 | static void update_cfs_rq_h_load(struct cfs_rq *cfs_rq) |
9763b67f | 7913 | { |
68520796 VD |
7914 | struct rq *rq = rq_of(cfs_rq); |
7915 | struct sched_entity *se = cfs_rq->tg->se[cpu_of(rq)]; | |
a35b6466 | 7916 | unsigned long now = jiffies; |
68520796 | 7917 | unsigned long load; |
a35b6466 | 7918 | |
68520796 | 7919 | if (cfs_rq->last_h_load_update == now) |
a35b6466 PZ |
7920 | return; |
7921 | ||
0e9f0245 | 7922 | WRITE_ONCE(cfs_rq->h_load_next, NULL); |
68520796 VD |
7923 | for_each_sched_entity(se) { |
7924 | cfs_rq = cfs_rq_of(se); | |
0e9f0245 | 7925 | WRITE_ONCE(cfs_rq->h_load_next, se); |
68520796 VD |
7926 | if (cfs_rq->last_h_load_update == now) |
7927 | break; | |
7928 | } | |
a35b6466 | 7929 | |
68520796 | 7930 | if (!se) { |
7ea241af | 7931 | cfs_rq->h_load = cfs_rq_load_avg(cfs_rq); |
68520796 VD |
7932 | cfs_rq->last_h_load_update = now; |
7933 | } | |
7934 | ||
0e9f0245 | 7935 | while ((se = READ_ONCE(cfs_rq->h_load_next)) != NULL) { |
68520796 | 7936 | load = cfs_rq->h_load; |
7ea241af YD |
7937 | load = div64_ul(load * se->avg.load_avg, |
7938 | cfs_rq_load_avg(cfs_rq) + 1); | |
68520796 VD |
7939 | cfs_rq = group_cfs_rq(se); |
7940 | cfs_rq->h_load = load; | |
7941 | cfs_rq->last_h_load_update = now; | |
7942 | } | |
9763b67f PZ |
7943 | } |
7944 | ||
367456c7 | 7945 | static unsigned long task_h_load(struct task_struct *p) |
230059de | 7946 | { |
367456c7 | 7947 | struct cfs_rq *cfs_rq = task_cfs_rq(p); |
230059de | 7948 | |
68520796 | 7949 | update_cfs_rq_h_load(cfs_rq); |
9d89c257 | 7950 | return div64_ul(p->se.avg.load_avg * cfs_rq->h_load, |
7ea241af | 7951 | cfs_rq_load_avg(cfs_rq) + 1); |
230059de PZ |
7952 | } |
7953 | #else | |
bef69dd8 | 7954 | static bool __update_blocked_fair(struct rq *rq, bool *done) |
9e3081ca | 7955 | { |
6c1d47c0 | 7956 | struct cfs_rq *cfs_rq = &rq->cfs; |
bef69dd8 | 7957 | bool decayed; |
b90f7c9d | 7958 | |
bef69dd8 VG |
7959 | decayed = update_cfs_rq_load_avg(cfs_rq_clock_pelt(cfs_rq), cfs_rq); |
7960 | if (cfs_rq_has_blocked(cfs_rq)) | |
7961 | *done = false; | |
b90f7c9d | 7962 | |
bef69dd8 | 7963 | return decayed; |
9e3081ca PZ |
7964 | } |
7965 | ||
367456c7 | 7966 | static unsigned long task_h_load(struct task_struct *p) |
1e3c88bd | 7967 | { |
9d89c257 | 7968 | return p->se.avg.load_avg; |
1e3c88bd | 7969 | } |
230059de | 7970 | #endif |
1e3c88bd | 7971 | |
bef69dd8 VG |
7972 | static void update_blocked_averages(int cpu) |
7973 | { | |
7974 | bool decayed = false, done = true; | |
7975 | struct rq *rq = cpu_rq(cpu); | |
7976 | struct rq_flags rf; | |
7977 | ||
7978 | rq_lock_irqsave(rq, &rf); | |
7979 | update_rq_clock(rq); | |
7980 | ||
7981 | decayed |= __update_blocked_others(rq, &done); | |
7982 | decayed |= __update_blocked_fair(rq, &done); | |
7983 | ||
7984 | update_blocked_load_status(rq, !done); | |
7985 | if (decayed) | |
7986 | cpufreq_update_util(rq, 0); | |
7987 | rq_unlock_irqrestore(rq, &rf); | |
7988 | } | |
7989 | ||
1e3c88bd | 7990 | /********** Helpers for find_busiest_group ************************/ |
caeb178c | 7991 | |
1e3c88bd PZ |
7992 | /* |
7993 | * sg_lb_stats - stats of a sched_group required for load_balancing | |
7994 | */ | |
7995 | struct sg_lb_stats { | |
7996 | unsigned long avg_load; /*Avg load across the CPUs of the group */ | |
7997 | unsigned long group_load; /* Total load over the CPUs of the group */ | |
63b2ca30 | 7998 | unsigned long group_capacity; |
070f5e86 VG |
7999 | unsigned long group_util; /* Total utilization over the CPUs of the group */ |
8000 | unsigned long group_runnable; /* Total runnable time over the CPUs of the group */ | |
5e23e474 | 8001 | unsigned int sum_nr_running; /* Nr of tasks running in the group */ |
a3498347 | 8002 | unsigned int sum_h_nr_running; /* Nr of CFS tasks running in the group */ |
147c5fc2 PZ |
8003 | unsigned int idle_cpus; |
8004 | unsigned int group_weight; | |
caeb178c | 8005 | enum group_type group_type; |
490ba971 | 8006 | unsigned int group_asym_packing; /* Tasks should be moved to preferred CPU */ |
3b1baa64 | 8007 | unsigned long group_misfit_task_load; /* A CPU has a task too big for its capacity */ |
0ec8aa00 PZ |
8008 | #ifdef CONFIG_NUMA_BALANCING |
8009 | unsigned int nr_numa_running; | |
8010 | unsigned int nr_preferred_running; | |
8011 | #endif | |
1e3c88bd PZ |
8012 | }; |
8013 | ||
56cf515b JK |
8014 | /* |
8015 | * sd_lb_stats - Structure to store the statistics of a sched_domain | |
8016 | * during load balancing. | |
8017 | */ | |
8018 | struct sd_lb_stats { | |
8019 | struct sched_group *busiest; /* Busiest group in this sd */ | |
8020 | struct sched_group *local; /* Local group in this sd */ | |
8021 | unsigned long total_load; /* Total load of all groups in sd */ | |
63b2ca30 | 8022 | unsigned long total_capacity; /* Total capacity of all groups in sd */ |
56cf515b | 8023 | unsigned long avg_load; /* Average load across all groups in sd */ |
0b0695f2 | 8024 | unsigned int prefer_sibling; /* tasks should go to sibling first */ |
56cf515b | 8025 | |
56cf515b | 8026 | struct sg_lb_stats busiest_stat;/* Statistics of the busiest group */ |
147c5fc2 | 8027 | struct sg_lb_stats local_stat; /* Statistics of the local group */ |
56cf515b JK |
8028 | }; |
8029 | ||
147c5fc2 PZ |
8030 | static inline void init_sd_lb_stats(struct sd_lb_stats *sds) |
8031 | { | |
8032 | /* | |
8033 | * Skimp on the clearing to avoid duplicate work. We can avoid clearing | |
8034 | * local_stat because update_sg_lb_stats() does a full clear/assignment. | |
0b0695f2 VG |
8035 | * We must however set busiest_stat::group_type and |
8036 | * busiest_stat::idle_cpus to the worst busiest group because | |
8037 | * update_sd_pick_busiest() reads these before assignment. | |
147c5fc2 PZ |
8038 | */ |
8039 | *sds = (struct sd_lb_stats){ | |
8040 | .busiest = NULL, | |
8041 | .local = NULL, | |
8042 | .total_load = 0UL, | |
63b2ca30 | 8043 | .total_capacity = 0UL, |
147c5fc2 | 8044 | .busiest_stat = { |
0b0695f2 VG |
8045 | .idle_cpus = UINT_MAX, |
8046 | .group_type = group_has_spare, | |
147c5fc2 PZ |
8047 | }, |
8048 | }; | |
8049 | } | |
8050 | ||
1ca2034e | 8051 | static unsigned long scale_rt_capacity(int cpu) |
1e3c88bd PZ |
8052 | { |
8053 | struct rq *rq = cpu_rq(cpu); | |
8ec59c0f | 8054 | unsigned long max = arch_scale_cpu_capacity(cpu); |
523e979d | 8055 | unsigned long used, free; |
523e979d | 8056 | unsigned long irq; |
b654f7de | 8057 | |
2e62c474 | 8058 | irq = cpu_util_irq(rq); |
cadefd3d | 8059 | |
523e979d VG |
8060 | if (unlikely(irq >= max)) |
8061 | return 1; | |
aa483808 | 8062 | |
467b7d01 TG |
8063 | /* |
8064 | * avg_rt.util_avg and avg_dl.util_avg track binary signals | |
8065 | * (running and not running) with weights 0 and 1024 respectively. | |
8066 | * avg_thermal.load_avg tracks thermal pressure and the weighted | |
8067 | * average uses the actual delta max capacity(load). | |
8068 | */ | |
523e979d VG |
8069 | used = READ_ONCE(rq->avg_rt.util_avg); |
8070 | used += READ_ONCE(rq->avg_dl.util_avg); | |
467b7d01 | 8071 | used += thermal_load_avg(rq); |
1e3c88bd | 8072 | |
523e979d VG |
8073 | if (unlikely(used >= max)) |
8074 | return 1; | |
1e3c88bd | 8075 | |
523e979d | 8076 | free = max - used; |
2e62c474 VG |
8077 | |
8078 | return scale_irq_capacity(free, irq, max); | |
1e3c88bd PZ |
8079 | } |
8080 | ||
ced549fa | 8081 | static void update_cpu_capacity(struct sched_domain *sd, int cpu) |
1e3c88bd | 8082 | { |
1ca2034e | 8083 | unsigned long capacity = scale_rt_capacity(cpu); |
1e3c88bd PZ |
8084 | struct sched_group *sdg = sd->groups; |
8085 | ||
8ec59c0f | 8086 | cpu_rq(cpu)->cpu_capacity_orig = arch_scale_cpu_capacity(cpu); |
1e3c88bd | 8087 | |
ced549fa NP |
8088 | if (!capacity) |
8089 | capacity = 1; | |
1e3c88bd | 8090 | |
ced549fa NP |
8091 | cpu_rq(cpu)->cpu_capacity = capacity; |
8092 | sdg->sgc->capacity = capacity; | |
bf475ce0 | 8093 | sdg->sgc->min_capacity = capacity; |
e3d6d0cb | 8094 | sdg->sgc->max_capacity = capacity; |
1e3c88bd PZ |
8095 | } |
8096 | ||
63b2ca30 | 8097 | void update_group_capacity(struct sched_domain *sd, int cpu) |
1e3c88bd PZ |
8098 | { |
8099 | struct sched_domain *child = sd->child; | |
8100 | struct sched_group *group, *sdg = sd->groups; | |
e3d6d0cb | 8101 | unsigned long capacity, min_capacity, max_capacity; |
4ec4412e VG |
8102 | unsigned long interval; |
8103 | ||
8104 | interval = msecs_to_jiffies(sd->balance_interval); | |
8105 | interval = clamp(interval, 1UL, max_load_balance_interval); | |
63b2ca30 | 8106 | sdg->sgc->next_update = jiffies + interval; |
1e3c88bd PZ |
8107 | |
8108 | if (!child) { | |
ced549fa | 8109 | update_cpu_capacity(sd, cpu); |
1e3c88bd PZ |
8110 | return; |
8111 | } | |
8112 | ||
dc7ff76e | 8113 | capacity = 0; |
bf475ce0 | 8114 | min_capacity = ULONG_MAX; |
e3d6d0cb | 8115 | max_capacity = 0; |
1e3c88bd | 8116 | |
74a5ce20 PZ |
8117 | if (child->flags & SD_OVERLAP) { |
8118 | /* | |
8119 | * SD_OVERLAP domains cannot assume that child groups | |
8120 | * span the current group. | |
8121 | */ | |
8122 | ||
ae4df9d6 | 8123 | for_each_cpu(cpu, sched_group_span(sdg)) { |
4c58f57f | 8124 | unsigned long cpu_cap = capacity_of(cpu); |
863bffc8 | 8125 | |
4c58f57f PL |
8126 | capacity += cpu_cap; |
8127 | min_capacity = min(cpu_cap, min_capacity); | |
8128 | max_capacity = max(cpu_cap, max_capacity); | |
863bffc8 | 8129 | } |
74a5ce20 PZ |
8130 | } else { |
8131 | /* | |
8132 | * !SD_OVERLAP domains can assume that child groups | |
8133 | * span the current group. | |
97a7142f | 8134 | */ |
74a5ce20 PZ |
8135 | |
8136 | group = child->groups; | |
8137 | do { | |
bf475ce0 MR |
8138 | struct sched_group_capacity *sgc = group->sgc; |
8139 | ||
8140 | capacity += sgc->capacity; | |
8141 | min_capacity = min(sgc->min_capacity, min_capacity); | |
e3d6d0cb | 8142 | max_capacity = max(sgc->max_capacity, max_capacity); |
74a5ce20 PZ |
8143 | group = group->next; |
8144 | } while (group != child->groups); | |
8145 | } | |
1e3c88bd | 8146 | |
63b2ca30 | 8147 | sdg->sgc->capacity = capacity; |
bf475ce0 | 8148 | sdg->sgc->min_capacity = min_capacity; |
e3d6d0cb | 8149 | sdg->sgc->max_capacity = max_capacity; |
1e3c88bd PZ |
8150 | } |
8151 | ||
9d5efe05 | 8152 | /* |
ea67821b VG |
8153 | * Check whether the capacity of the rq has been noticeably reduced by side |
8154 | * activity. The imbalance_pct is used for the threshold. | |
8155 | * Return true is the capacity is reduced | |
9d5efe05 SV |
8156 | */ |
8157 | static inline int | |
ea67821b | 8158 | check_cpu_capacity(struct rq *rq, struct sched_domain *sd) |
9d5efe05 | 8159 | { |
ea67821b VG |
8160 | return ((rq->cpu_capacity * sd->imbalance_pct) < |
8161 | (rq->cpu_capacity_orig * 100)); | |
9d5efe05 SV |
8162 | } |
8163 | ||
a0fe2cf0 VS |
8164 | /* |
8165 | * Check whether a rq has a misfit task and if it looks like we can actually | |
8166 | * help that task: we can migrate the task to a CPU of higher capacity, or | |
8167 | * the task's current CPU is heavily pressured. | |
8168 | */ | |
8169 | static inline int check_misfit_status(struct rq *rq, struct sched_domain *sd) | |
8170 | { | |
8171 | return rq->misfit_task_load && | |
8172 | (rq->cpu_capacity_orig < rq->rd->max_cpu_capacity || | |
8173 | check_cpu_capacity(rq, sd)); | |
8174 | } | |
8175 | ||
30ce5dab PZ |
8176 | /* |
8177 | * Group imbalance indicates (and tries to solve) the problem where balancing | |
3bd37062 | 8178 | * groups is inadequate due to ->cpus_ptr constraints. |
30ce5dab | 8179 | * |
97fb7a0a IM |
8180 | * Imagine a situation of two groups of 4 CPUs each and 4 tasks each with a |
8181 | * cpumask covering 1 CPU of the first group and 3 CPUs of the second group. | |
30ce5dab PZ |
8182 | * Something like: |
8183 | * | |
2b4d5b25 IM |
8184 | * { 0 1 2 3 } { 4 5 6 7 } |
8185 | * * * * * | |
30ce5dab PZ |
8186 | * |
8187 | * If we were to balance group-wise we'd place two tasks in the first group and | |
8188 | * two tasks in the second group. Clearly this is undesired as it will overload | |
97fb7a0a | 8189 | * cpu 3 and leave one of the CPUs in the second group unused. |
30ce5dab PZ |
8190 | * |
8191 | * The current solution to this issue is detecting the skew in the first group | |
6263322c PZ |
8192 | * by noticing the lower domain failed to reach balance and had difficulty |
8193 | * moving tasks due to affinity constraints. | |
30ce5dab PZ |
8194 | * |
8195 | * When this is so detected; this group becomes a candidate for busiest; see | |
ed1b7732 | 8196 | * update_sd_pick_busiest(). And calculate_imbalance() and |
6263322c | 8197 | * find_busiest_group() avoid some of the usual balance conditions to allow it |
30ce5dab PZ |
8198 | * to create an effective group imbalance. |
8199 | * | |
8200 | * This is a somewhat tricky proposition since the next run might not find the | |
8201 | * group imbalance and decide the groups need to be balanced again. A most | |
8202 | * subtle and fragile situation. | |
8203 | */ | |
8204 | ||
6263322c | 8205 | static inline int sg_imbalanced(struct sched_group *group) |
30ce5dab | 8206 | { |
63b2ca30 | 8207 | return group->sgc->imbalance; |
30ce5dab PZ |
8208 | } |
8209 | ||
b37d9316 | 8210 | /* |
ea67821b VG |
8211 | * group_has_capacity returns true if the group has spare capacity that could |
8212 | * be used by some tasks. | |
8213 | * We consider that a group has spare capacity if the * number of task is | |
9e91d61d DE |
8214 | * smaller than the number of CPUs or if the utilization is lower than the |
8215 | * available capacity for CFS tasks. | |
ea67821b VG |
8216 | * For the latter, we use a threshold to stabilize the state, to take into |
8217 | * account the variance of the tasks' load and to return true if the available | |
8218 | * capacity in meaningful for the load balancer. | |
8219 | * As an example, an available capacity of 1% can appear but it doesn't make | |
8220 | * any benefit for the load balance. | |
b37d9316 | 8221 | */ |
ea67821b | 8222 | static inline bool |
57abff06 | 8223 | group_has_capacity(unsigned int imbalance_pct, struct sg_lb_stats *sgs) |
b37d9316 | 8224 | { |
5e23e474 | 8225 | if (sgs->sum_nr_running < sgs->group_weight) |
ea67821b | 8226 | return true; |
c61037e9 | 8227 | |
070f5e86 VG |
8228 | if ((sgs->group_capacity * imbalance_pct) < |
8229 | (sgs->group_runnable * 100)) | |
8230 | return false; | |
8231 | ||
ea67821b | 8232 | if ((sgs->group_capacity * 100) > |
57abff06 | 8233 | (sgs->group_util * imbalance_pct)) |
ea67821b | 8234 | return true; |
b37d9316 | 8235 | |
ea67821b VG |
8236 | return false; |
8237 | } | |
8238 | ||
8239 | /* | |
8240 | * group_is_overloaded returns true if the group has more tasks than it can | |
8241 | * handle. | |
8242 | * group_is_overloaded is not equals to !group_has_capacity because a group | |
8243 | * with the exact right number of tasks, has no more spare capacity but is not | |
8244 | * overloaded so both group_has_capacity and group_is_overloaded return | |
8245 | * false. | |
8246 | */ | |
8247 | static inline bool | |
57abff06 | 8248 | group_is_overloaded(unsigned int imbalance_pct, struct sg_lb_stats *sgs) |
ea67821b | 8249 | { |
5e23e474 | 8250 | if (sgs->sum_nr_running <= sgs->group_weight) |
ea67821b | 8251 | return false; |
b37d9316 | 8252 | |
ea67821b | 8253 | if ((sgs->group_capacity * 100) < |
57abff06 | 8254 | (sgs->group_util * imbalance_pct)) |
ea67821b | 8255 | return true; |
b37d9316 | 8256 | |
070f5e86 VG |
8257 | if ((sgs->group_capacity * imbalance_pct) < |
8258 | (sgs->group_runnable * 100)) | |
8259 | return true; | |
8260 | ||
ea67821b | 8261 | return false; |
b37d9316 PZ |
8262 | } |
8263 | ||
9e0994c0 | 8264 | /* |
e3d6d0cb | 8265 | * group_smaller_min_cpu_capacity: Returns true if sched_group sg has smaller |
9e0994c0 MR |
8266 | * per-CPU capacity than sched_group ref. |
8267 | */ | |
8268 | static inline bool | |
e3d6d0cb | 8269 | group_smaller_min_cpu_capacity(struct sched_group *sg, struct sched_group *ref) |
9e0994c0 | 8270 | { |
60e17f5c | 8271 | return fits_capacity(sg->sgc->min_capacity, ref->sgc->min_capacity); |
9e0994c0 MR |
8272 | } |
8273 | ||
e3d6d0cb MR |
8274 | /* |
8275 | * group_smaller_max_cpu_capacity: Returns true if sched_group sg has smaller | |
8276 | * per-CPU capacity_orig than sched_group ref. | |
8277 | */ | |
8278 | static inline bool | |
8279 | group_smaller_max_cpu_capacity(struct sched_group *sg, struct sched_group *ref) | |
8280 | { | |
60e17f5c | 8281 | return fits_capacity(sg->sgc->max_capacity, ref->sgc->max_capacity); |
e3d6d0cb MR |
8282 | } |
8283 | ||
79a89f92 | 8284 | static inline enum |
57abff06 | 8285 | group_type group_classify(unsigned int imbalance_pct, |
0b0695f2 | 8286 | struct sched_group *group, |
79a89f92 | 8287 | struct sg_lb_stats *sgs) |
caeb178c | 8288 | { |
57abff06 | 8289 | if (group_is_overloaded(imbalance_pct, sgs)) |
caeb178c RR |
8290 | return group_overloaded; |
8291 | ||
8292 | if (sg_imbalanced(group)) | |
8293 | return group_imbalanced; | |
8294 | ||
0b0695f2 VG |
8295 | if (sgs->group_asym_packing) |
8296 | return group_asym_packing; | |
8297 | ||
3b1baa64 MR |
8298 | if (sgs->group_misfit_task_load) |
8299 | return group_misfit_task; | |
8300 | ||
57abff06 | 8301 | if (!group_has_capacity(imbalance_pct, sgs)) |
0b0695f2 VG |
8302 | return group_fully_busy; |
8303 | ||
8304 | return group_has_spare; | |
caeb178c RR |
8305 | } |
8306 | ||
63928384 | 8307 | static bool update_nohz_stats(struct rq *rq, bool force) |
e022e0d3 PZ |
8308 | { |
8309 | #ifdef CONFIG_NO_HZ_COMMON | |
8310 | unsigned int cpu = rq->cpu; | |
8311 | ||
f643ea22 VG |
8312 | if (!rq->has_blocked_load) |
8313 | return false; | |
8314 | ||
e022e0d3 | 8315 | if (!cpumask_test_cpu(cpu, nohz.idle_cpus_mask)) |
f643ea22 | 8316 | return false; |
e022e0d3 | 8317 | |
63928384 | 8318 | if (!force && !time_after(jiffies, rq->last_blocked_load_update_tick)) |
f643ea22 | 8319 | return true; |
e022e0d3 PZ |
8320 | |
8321 | update_blocked_averages(cpu); | |
f643ea22 VG |
8322 | |
8323 | return rq->has_blocked_load; | |
8324 | #else | |
8325 | return false; | |
e022e0d3 PZ |
8326 | #endif |
8327 | } | |
8328 | ||
1e3c88bd PZ |
8329 | /** |
8330 | * update_sg_lb_stats - Update sched_group's statistics for load balancing. | |
cd96891d | 8331 | * @env: The load balancing environment. |
1e3c88bd | 8332 | * @group: sched_group whose statistics are to be updated. |
1e3c88bd | 8333 | * @sgs: variable to hold the statistics for this group. |
630246a0 | 8334 | * @sg_status: Holds flag indicating the status of the sched_group |
1e3c88bd | 8335 | */ |
bd939f45 | 8336 | static inline void update_sg_lb_stats(struct lb_env *env, |
630246a0 QP |
8337 | struct sched_group *group, |
8338 | struct sg_lb_stats *sgs, | |
8339 | int *sg_status) | |
1e3c88bd | 8340 | { |
0b0695f2 | 8341 | int i, nr_running, local_group; |
1e3c88bd | 8342 | |
b72ff13c PZ |
8343 | memset(sgs, 0, sizeof(*sgs)); |
8344 | ||
0b0695f2 VG |
8345 | local_group = cpumask_test_cpu(env->dst_cpu, sched_group_span(group)); |
8346 | ||
ae4df9d6 | 8347 | for_each_cpu_and(i, sched_group_span(group), env->cpus) { |
1e3c88bd PZ |
8348 | struct rq *rq = cpu_rq(i); |
8349 | ||
63928384 | 8350 | if ((env->flags & LBF_NOHZ_STATS) && update_nohz_stats(rq, false)) |
f643ea22 | 8351 | env->flags |= LBF_NOHZ_AGAIN; |
e022e0d3 | 8352 | |
b0fb1eb4 | 8353 | sgs->group_load += cpu_load(rq); |
9e91d61d | 8354 | sgs->group_util += cpu_util(i); |
070f5e86 | 8355 | sgs->group_runnable += cpu_runnable(rq); |
a3498347 | 8356 | sgs->sum_h_nr_running += rq->cfs.h_nr_running; |
4486edd1 | 8357 | |
a426f99c | 8358 | nr_running = rq->nr_running; |
5e23e474 VG |
8359 | sgs->sum_nr_running += nr_running; |
8360 | ||
a426f99c | 8361 | if (nr_running > 1) |
630246a0 | 8362 | *sg_status |= SG_OVERLOAD; |
4486edd1 | 8363 | |
2802bf3c MR |
8364 | if (cpu_overutilized(i)) |
8365 | *sg_status |= SG_OVERUTILIZED; | |
4486edd1 | 8366 | |
0ec8aa00 PZ |
8367 | #ifdef CONFIG_NUMA_BALANCING |
8368 | sgs->nr_numa_running += rq->nr_numa_running; | |
8369 | sgs->nr_preferred_running += rq->nr_preferred_running; | |
8370 | #endif | |
a426f99c WL |
8371 | /* |
8372 | * No need to call idle_cpu() if nr_running is not 0 | |
8373 | */ | |
0b0695f2 | 8374 | if (!nr_running && idle_cpu(i)) { |
aae6d3dd | 8375 | sgs->idle_cpus++; |
0b0695f2 VG |
8376 | /* Idle cpu can't have misfit task */ |
8377 | continue; | |
8378 | } | |
8379 | ||
8380 | if (local_group) | |
8381 | continue; | |
3b1baa64 | 8382 | |
0b0695f2 | 8383 | /* Check for a misfit task on the cpu */ |
3b1baa64 | 8384 | if (env->sd->flags & SD_ASYM_CPUCAPACITY && |
757ffdd7 | 8385 | sgs->group_misfit_task_load < rq->misfit_task_load) { |
3b1baa64 | 8386 | sgs->group_misfit_task_load = rq->misfit_task_load; |
630246a0 | 8387 | *sg_status |= SG_OVERLOAD; |
757ffdd7 | 8388 | } |
1e3c88bd PZ |
8389 | } |
8390 | ||
0b0695f2 VG |
8391 | /* Check if dst CPU is idle and preferred to this group */ |
8392 | if (env->sd->flags & SD_ASYM_PACKING && | |
8393 | env->idle != CPU_NOT_IDLE && | |
8394 | sgs->sum_h_nr_running && | |
8395 | sched_asym_prefer(env->dst_cpu, group->asym_prefer_cpu)) { | |
8396 | sgs->group_asym_packing = 1; | |
8397 | } | |
8398 | ||
63b2ca30 | 8399 | sgs->group_capacity = group->sgc->capacity; |
1e3c88bd | 8400 | |
aae6d3dd | 8401 | sgs->group_weight = group->group_weight; |
b37d9316 | 8402 | |
57abff06 | 8403 | sgs->group_type = group_classify(env->sd->imbalance_pct, group, sgs); |
0b0695f2 VG |
8404 | |
8405 | /* Computing avg_load makes sense only when group is overloaded */ | |
8406 | if (sgs->group_type == group_overloaded) | |
8407 | sgs->avg_load = (sgs->group_load * SCHED_CAPACITY_SCALE) / | |
8408 | sgs->group_capacity; | |
1e3c88bd PZ |
8409 | } |
8410 | ||
532cb4c4 MN |
8411 | /** |
8412 | * update_sd_pick_busiest - return 1 on busiest group | |
cd96891d | 8413 | * @env: The load balancing environment. |
532cb4c4 MN |
8414 | * @sds: sched_domain statistics |
8415 | * @sg: sched_group candidate to be checked for being the busiest | |
b6b12294 | 8416 | * @sgs: sched_group statistics |
532cb4c4 MN |
8417 | * |
8418 | * Determine if @sg is a busier group than the previously selected | |
8419 | * busiest group. | |
e69f6186 YB |
8420 | * |
8421 | * Return: %true if @sg is a busier group than the previously selected | |
8422 | * busiest group. %false otherwise. | |
532cb4c4 | 8423 | */ |
bd939f45 | 8424 | static bool update_sd_pick_busiest(struct lb_env *env, |
532cb4c4 MN |
8425 | struct sd_lb_stats *sds, |
8426 | struct sched_group *sg, | |
bd939f45 | 8427 | struct sg_lb_stats *sgs) |
532cb4c4 | 8428 | { |
caeb178c | 8429 | struct sg_lb_stats *busiest = &sds->busiest_stat; |
532cb4c4 | 8430 | |
0b0695f2 VG |
8431 | /* Make sure that there is at least one task to pull */ |
8432 | if (!sgs->sum_h_nr_running) | |
8433 | return false; | |
8434 | ||
cad68e55 MR |
8435 | /* |
8436 | * Don't try to pull misfit tasks we can't help. | |
8437 | * We can use max_capacity here as reduction in capacity on some | |
8438 | * CPUs in the group should either be possible to resolve | |
8439 | * internally or be covered by avg_load imbalance (eventually). | |
8440 | */ | |
8441 | if (sgs->group_type == group_misfit_task && | |
8442 | (!group_smaller_max_cpu_capacity(sg, sds->local) || | |
0b0695f2 | 8443 | sds->local_stat.group_type != group_has_spare)) |
cad68e55 MR |
8444 | return false; |
8445 | ||
caeb178c | 8446 | if (sgs->group_type > busiest->group_type) |
532cb4c4 MN |
8447 | return true; |
8448 | ||
caeb178c RR |
8449 | if (sgs->group_type < busiest->group_type) |
8450 | return false; | |
8451 | ||
9e0994c0 | 8452 | /* |
0b0695f2 VG |
8453 | * The candidate and the current busiest group are the same type of |
8454 | * group. Let check which one is the busiest according to the type. | |
9e0994c0 | 8455 | */ |
9e0994c0 | 8456 | |
0b0695f2 VG |
8457 | switch (sgs->group_type) { |
8458 | case group_overloaded: | |
8459 | /* Select the overloaded group with highest avg_load. */ | |
8460 | if (sgs->avg_load <= busiest->avg_load) | |
8461 | return false; | |
8462 | break; | |
8463 | ||
8464 | case group_imbalanced: | |
8465 | /* | |
8466 | * Select the 1st imbalanced group as we don't have any way to | |
8467 | * choose one more than another. | |
8468 | */ | |
9e0994c0 MR |
8469 | return false; |
8470 | ||
0b0695f2 VG |
8471 | case group_asym_packing: |
8472 | /* Prefer to move from lowest priority CPU's work */ | |
8473 | if (sched_asym_prefer(sg->asym_prefer_cpu, sds->busiest->asym_prefer_cpu)) | |
8474 | return false; | |
8475 | break; | |
532cb4c4 | 8476 | |
0b0695f2 VG |
8477 | case group_misfit_task: |
8478 | /* | |
8479 | * If we have more than one misfit sg go with the biggest | |
8480 | * misfit. | |
8481 | */ | |
8482 | if (sgs->group_misfit_task_load < busiest->group_misfit_task_load) | |
8483 | return false; | |
8484 | break; | |
532cb4c4 | 8485 | |
0b0695f2 VG |
8486 | case group_fully_busy: |
8487 | /* | |
8488 | * Select the fully busy group with highest avg_load. In | |
8489 | * theory, there is no need to pull task from such kind of | |
8490 | * group because tasks have all compute capacity that they need | |
8491 | * but we can still improve the overall throughput by reducing | |
8492 | * contention when accessing shared HW resources. | |
8493 | * | |
8494 | * XXX for now avg_load is not computed and always 0 so we | |
8495 | * select the 1st one. | |
8496 | */ | |
8497 | if (sgs->avg_load <= busiest->avg_load) | |
8498 | return false; | |
8499 | break; | |
8500 | ||
8501 | case group_has_spare: | |
8502 | /* | |
5f68eb19 VG |
8503 | * Select not overloaded group with lowest number of idle cpus |
8504 | * and highest number of running tasks. We could also compare | |
8505 | * the spare capacity which is more stable but it can end up | |
8506 | * that the group has less spare capacity but finally more idle | |
0b0695f2 VG |
8507 | * CPUs which means less opportunity to pull tasks. |
8508 | */ | |
5f68eb19 | 8509 | if (sgs->idle_cpus > busiest->idle_cpus) |
0b0695f2 | 8510 | return false; |
5f68eb19 VG |
8511 | else if ((sgs->idle_cpus == busiest->idle_cpus) && |
8512 | (sgs->sum_nr_running <= busiest->sum_nr_running)) | |
8513 | return false; | |
8514 | ||
0b0695f2 | 8515 | break; |
532cb4c4 MN |
8516 | } |
8517 | ||
0b0695f2 VG |
8518 | /* |
8519 | * Candidate sg has no more than one task per CPU and has higher | |
8520 | * per-CPU capacity. Migrating tasks to less capable CPUs may harm | |
8521 | * throughput. Maximize throughput, power/energy consequences are not | |
8522 | * considered. | |
8523 | */ | |
8524 | if ((env->sd->flags & SD_ASYM_CPUCAPACITY) && | |
8525 | (sgs->group_type <= group_fully_busy) && | |
8526 | (group_smaller_min_cpu_capacity(sds->local, sg))) | |
8527 | return false; | |
8528 | ||
8529 | return true; | |
532cb4c4 MN |
8530 | } |
8531 | ||
0ec8aa00 PZ |
8532 | #ifdef CONFIG_NUMA_BALANCING |
8533 | static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs) | |
8534 | { | |
a3498347 | 8535 | if (sgs->sum_h_nr_running > sgs->nr_numa_running) |
0ec8aa00 | 8536 | return regular; |
a3498347 | 8537 | if (sgs->sum_h_nr_running > sgs->nr_preferred_running) |
0ec8aa00 PZ |
8538 | return remote; |
8539 | return all; | |
8540 | } | |
8541 | ||
8542 | static inline enum fbq_type fbq_classify_rq(struct rq *rq) | |
8543 | { | |
8544 | if (rq->nr_running > rq->nr_numa_running) | |
8545 | return regular; | |
8546 | if (rq->nr_running > rq->nr_preferred_running) | |
8547 | return remote; | |
8548 | return all; | |
8549 | } | |
8550 | #else | |
8551 | static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs) | |
8552 | { | |
8553 | return all; | |
8554 | } | |
8555 | ||
8556 | static inline enum fbq_type fbq_classify_rq(struct rq *rq) | |
8557 | { | |
8558 | return regular; | |
8559 | } | |
8560 | #endif /* CONFIG_NUMA_BALANCING */ | |
8561 | ||
57abff06 VG |
8562 | |
8563 | struct sg_lb_stats; | |
8564 | ||
3318544b VG |
8565 | /* |
8566 | * task_running_on_cpu - return 1 if @p is running on @cpu. | |
8567 | */ | |
8568 | ||
8569 | static unsigned int task_running_on_cpu(int cpu, struct task_struct *p) | |
8570 | { | |
8571 | /* Task has no contribution or is new */ | |
8572 | if (cpu != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) | |
8573 | return 0; | |
8574 | ||
8575 | if (task_on_rq_queued(p)) | |
8576 | return 1; | |
8577 | ||
8578 | return 0; | |
8579 | } | |
8580 | ||
8581 | /** | |
8582 | * idle_cpu_without - would a given CPU be idle without p ? | |
8583 | * @cpu: the processor on which idleness is tested. | |
8584 | * @p: task which should be ignored. | |
8585 | * | |
8586 | * Return: 1 if the CPU would be idle. 0 otherwise. | |
8587 | */ | |
8588 | static int idle_cpu_without(int cpu, struct task_struct *p) | |
8589 | { | |
8590 | struct rq *rq = cpu_rq(cpu); | |
8591 | ||
8592 | if (rq->curr != rq->idle && rq->curr != p) | |
8593 | return 0; | |
8594 | ||
8595 | /* | |
8596 | * rq->nr_running can't be used but an updated version without the | |
8597 | * impact of p on cpu must be used instead. The updated nr_running | |
8598 | * be computed and tested before calling idle_cpu_without(). | |
8599 | */ | |
8600 | ||
8601 | #ifdef CONFIG_SMP | |
126c2092 | 8602 | if (rq->ttwu_pending) |
3318544b VG |
8603 | return 0; |
8604 | #endif | |
8605 | ||
8606 | return 1; | |
8607 | } | |
8608 | ||
57abff06 VG |
8609 | /* |
8610 | * update_sg_wakeup_stats - Update sched_group's statistics for wakeup. | |
3318544b | 8611 | * @sd: The sched_domain level to look for idlest group. |
57abff06 VG |
8612 | * @group: sched_group whose statistics are to be updated. |
8613 | * @sgs: variable to hold the statistics for this group. | |
3318544b | 8614 | * @p: The task for which we look for the idlest group/CPU. |
57abff06 VG |
8615 | */ |
8616 | static inline void update_sg_wakeup_stats(struct sched_domain *sd, | |
8617 | struct sched_group *group, | |
8618 | struct sg_lb_stats *sgs, | |
8619 | struct task_struct *p) | |
8620 | { | |
8621 | int i, nr_running; | |
8622 | ||
8623 | memset(sgs, 0, sizeof(*sgs)); | |
8624 | ||
8625 | for_each_cpu(i, sched_group_span(group)) { | |
8626 | struct rq *rq = cpu_rq(i); | |
3318544b | 8627 | unsigned int local; |
57abff06 | 8628 | |
3318544b | 8629 | sgs->group_load += cpu_load_without(rq, p); |
57abff06 | 8630 | sgs->group_util += cpu_util_without(i, p); |
070f5e86 | 8631 | sgs->group_runnable += cpu_runnable_without(rq, p); |
3318544b VG |
8632 | local = task_running_on_cpu(i, p); |
8633 | sgs->sum_h_nr_running += rq->cfs.h_nr_running - local; | |
57abff06 | 8634 | |
3318544b | 8635 | nr_running = rq->nr_running - local; |
57abff06 VG |
8636 | sgs->sum_nr_running += nr_running; |
8637 | ||
8638 | /* | |
3318544b | 8639 | * No need to call idle_cpu_without() if nr_running is not 0 |
57abff06 | 8640 | */ |
3318544b | 8641 | if (!nr_running && idle_cpu_without(i, p)) |
57abff06 VG |
8642 | sgs->idle_cpus++; |
8643 | ||
57abff06 VG |
8644 | } |
8645 | ||
8646 | /* Check if task fits in the group */ | |
8647 | if (sd->flags & SD_ASYM_CPUCAPACITY && | |
8648 | !task_fits_capacity(p, group->sgc->max_capacity)) { | |
8649 | sgs->group_misfit_task_load = 1; | |
8650 | } | |
8651 | ||
8652 | sgs->group_capacity = group->sgc->capacity; | |
8653 | ||
289de359 VG |
8654 | sgs->group_weight = group->group_weight; |
8655 | ||
57abff06 VG |
8656 | sgs->group_type = group_classify(sd->imbalance_pct, group, sgs); |
8657 | ||
8658 | /* | |
8659 | * Computing avg_load makes sense only when group is fully busy or | |
8660 | * overloaded | |
8661 | */ | |
6c8116c9 TZ |
8662 | if (sgs->group_type == group_fully_busy || |
8663 | sgs->group_type == group_overloaded) | |
57abff06 VG |
8664 | sgs->avg_load = (sgs->group_load * SCHED_CAPACITY_SCALE) / |
8665 | sgs->group_capacity; | |
8666 | } | |
8667 | ||
8668 | static bool update_pick_idlest(struct sched_group *idlest, | |
8669 | struct sg_lb_stats *idlest_sgs, | |
8670 | struct sched_group *group, | |
8671 | struct sg_lb_stats *sgs) | |
8672 | { | |
8673 | if (sgs->group_type < idlest_sgs->group_type) | |
8674 | return true; | |
8675 | ||
8676 | if (sgs->group_type > idlest_sgs->group_type) | |
8677 | return false; | |
8678 | ||
8679 | /* | |
8680 | * The candidate and the current idlest group are the same type of | |
8681 | * group. Let check which one is the idlest according to the type. | |
8682 | */ | |
8683 | ||
8684 | switch (sgs->group_type) { | |
8685 | case group_overloaded: | |
8686 | case group_fully_busy: | |
8687 | /* Select the group with lowest avg_load. */ | |
8688 | if (idlest_sgs->avg_load <= sgs->avg_load) | |
8689 | return false; | |
8690 | break; | |
8691 | ||
8692 | case group_imbalanced: | |
8693 | case group_asym_packing: | |
8694 | /* Those types are not used in the slow wakeup path */ | |
8695 | return false; | |
8696 | ||
8697 | case group_misfit_task: | |
8698 | /* Select group with the highest max capacity */ | |
8699 | if (idlest->sgc->max_capacity >= group->sgc->max_capacity) | |
8700 | return false; | |
8701 | break; | |
8702 | ||
8703 | case group_has_spare: | |
8704 | /* Select group with most idle CPUs */ | |
8705 | if (idlest_sgs->idle_cpus >= sgs->idle_cpus) | |
8706 | return false; | |
8707 | break; | |
8708 | } | |
8709 | ||
8710 | return true; | |
8711 | } | |
8712 | ||
8713 | /* | |
8714 | * find_idlest_group() finds and returns the least busy CPU group within the | |
8715 | * domain. | |
8716 | * | |
8717 | * Assumes p is allowed on at least one CPU in sd. | |
8718 | */ | |
8719 | static struct sched_group * | |
45da2773 | 8720 | find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu) |
57abff06 VG |
8721 | { |
8722 | struct sched_group *idlest = NULL, *local = NULL, *group = sd->groups; | |
8723 | struct sg_lb_stats local_sgs, tmp_sgs; | |
8724 | struct sg_lb_stats *sgs; | |
8725 | unsigned long imbalance; | |
8726 | struct sg_lb_stats idlest_sgs = { | |
8727 | .avg_load = UINT_MAX, | |
8728 | .group_type = group_overloaded, | |
8729 | }; | |
8730 | ||
8731 | imbalance = scale_load_down(NICE_0_LOAD) * | |
8732 | (sd->imbalance_pct-100) / 100; | |
8733 | ||
8734 | do { | |
8735 | int local_group; | |
8736 | ||
8737 | /* Skip over this group if it has no CPUs allowed */ | |
8738 | if (!cpumask_intersects(sched_group_span(group), | |
8739 | p->cpus_ptr)) | |
8740 | continue; | |
8741 | ||
8742 | local_group = cpumask_test_cpu(this_cpu, | |
8743 | sched_group_span(group)); | |
8744 | ||
8745 | if (local_group) { | |
8746 | sgs = &local_sgs; | |
8747 | local = group; | |
8748 | } else { | |
8749 | sgs = &tmp_sgs; | |
8750 | } | |
8751 | ||
8752 | update_sg_wakeup_stats(sd, group, sgs, p); | |
8753 | ||
8754 | if (!local_group && update_pick_idlest(idlest, &idlest_sgs, group, sgs)) { | |
8755 | idlest = group; | |
8756 | idlest_sgs = *sgs; | |
8757 | } | |
8758 | ||
8759 | } while (group = group->next, group != sd->groups); | |
8760 | ||
8761 | ||
8762 | /* There is no idlest group to push tasks to */ | |
8763 | if (!idlest) | |
8764 | return NULL; | |
8765 | ||
7ed735c3 VG |
8766 | /* The local group has been skipped because of CPU affinity */ |
8767 | if (!local) | |
8768 | return idlest; | |
8769 | ||
57abff06 VG |
8770 | /* |
8771 | * If the local group is idler than the selected idlest group | |
8772 | * don't try and push the task. | |
8773 | */ | |
8774 | if (local_sgs.group_type < idlest_sgs.group_type) | |
8775 | return NULL; | |
8776 | ||
8777 | /* | |
8778 | * If the local group is busier than the selected idlest group | |
8779 | * try and push the task. | |
8780 | */ | |
8781 | if (local_sgs.group_type > idlest_sgs.group_type) | |
8782 | return idlest; | |
8783 | ||
8784 | switch (local_sgs.group_type) { | |
8785 | case group_overloaded: | |
8786 | case group_fully_busy: | |
8787 | /* | |
8788 | * When comparing groups across NUMA domains, it's possible for | |
8789 | * the local domain to be very lightly loaded relative to the | |
8790 | * remote domains but "imbalance" skews the comparison making | |
8791 | * remote CPUs look much more favourable. When considering | |
8792 | * cross-domain, add imbalance to the load on the remote node | |
8793 | * and consider staying local. | |
8794 | */ | |
8795 | ||
8796 | if ((sd->flags & SD_NUMA) && | |
8797 | ((idlest_sgs.avg_load + imbalance) >= local_sgs.avg_load)) | |
8798 | return NULL; | |
8799 | ||
8800 | /* | |
8801 | * If the local group is less loaded than the selected | |
8802 | * idlest group don't try and push any tasks. | |
8803 | */ | |
8804 | if (idlest_sgs.avg_load >= (local_sgs.avg_load + imbalance)) | |
8805 | return NULL; | |
8806 | ||
8807 | if (100 * local_sgs.avg_load <= sd->imbalance_pct * idlest_sgs.avg_load) | |
8808 | return NULL; | |
8809 | break; | |
8810 | ||
8811 | case group_imbalanced: | |
8812 | case group_asym_packing: | |
8813 | /* Those type are not used in the slow wakeup path */ | |
8814 | return NULL; | |
8815 | ||
8816 | case group_misfit_task: | |
8817 | /* Select group with the highest max capacity */ | |
8818 | if (local->sgc->max_capacity >= idlest->sgc->max_capacity) | |
8819 | return NULL; | |
8820 | break; | |
8821 | ||
8822 | case group_has_spare: | |
8823 | if (sd->flags & SD_NUMA) { | |
8824 | #ifdef CONFIG_NUMA_BALANCING | |
8825 | int idlest_cpu; | |
8826 | /* | |
8827 | * If there is spare capacity at NUMA, try to select | |
8828 | * the preferred node | |
8829 | */ | |
8830 | if (cpu_to_node(this_cpu) == p->numa_preferred_nid) | |
8831 | return NULL; | |
8832 | ||
8833 | idlest_cpu = cpumask_first(sched_group_span(idlest)); | |
8834 | if (cpu_to_node(idlest_cpu) == p->numa_preferred_nid) | |
8835 | return idlest; | |
8836 | #endif | |
8837 | /* | |
8838 | * Otherwise, keep the task on this node to stay close | |
8839 | * its wakeup source and improve locality. If there is | |
8840 | * a real need of migration, periodic load balance will | |
8841 | * take care of it. | |
8842 | */ | |
8843 | if (local_sgs.idle_cpus) | |
8844 | return NULL; | |
8845 | } | |
8846 | ||
8847 | /* | |
8848 | * Select group with highest number of idle CPUs. We could also | |
8849 | * compare the utilization which is more stable but it can end | |
8850 | * up that the group has less spare capacity but finally more | |
8851 | * idle CPUs which means more opportunity to run task. | |
8852 | */ | |
8853 | if (local_sgs.idle_cpus >= idlest_sgs.idle_cpus) | |
8854 | return NULL; | |
8855 | break; | |
8856 | } | |
8857 | ||
8858 | return idlest; | |
8859 | } | |
8860 | ||
1e3c88bd | 8861 | /** |
461819ac | 8862 | * update_sd_lb_stats - Update sched_domain's statistics for load balancing. |
cd96891d | 8863 | * @env: The load balancing environment. |
1e3c88bd PZ |
8864 | * @sds: variable to hold the statistics for this sched_domain. |
8865 | */ | |
0b0695f2 | 8866 | |
0ec8aa00 | 8867 | static inline void update_sd_lb_stats(struct lb_env *env, struct sd_lb_stats *sds) |
1e3c88bd | 8868 | { |
bd939f45 PZ |
8869 | struct sched_domain *child = env->sd->child; |
8870 | struct sched_group *sg = env->sd->groups; | |
05b40e05 | 8871 | struct sg_lb_stats *local = &sds->local_stat; |
56cf515b | 8872 | struct sg_lb_stats tmp_sgs; |
630246a0 | 8873 | int sg_status = 0; |
1e3c88bd | 8874 | |
e022e0d3 | 8875 | #ifdef CONFIG_NO_HZ_COMMON |
f643ea22 | 8876 | if (env->idle == CPU_NEWLY_IDLE && READ_ONCE(nohz.has_blocked)) |
e022e0d3 | 8877 | env->flags |= LBF_NOHZ_STATS; |
e022e0d3 PZ |
8878 | #endif |
8879 | ||
1e3c88bd | 8880 | do { |
56cf515b | 8881 | struct sg_lb_stats *sgs = &tmp_sgs; |
1e3c88bd PZ |
8882 | int local_group; |
8883 | ||
ae4df9d6 | 8884 | local_group = cpumask_test_cpu(env->dst_cpu, sched_group_span(sg)); |
56cf515b JK |
8885 | if (local_group) { |
8886 | sds->local = sg; | |
05b40e05 | 8887 | sgs = local; |
b72ff13c PZ |
8888 | |
8889 | if (env->idle != CPU_NEWLY_IDLE || | |
63b2ca30 NP |
8890 | time_after_eq(jiffies, sg->sgc->next_update)) |
8891 | update_group_capacity(env->sd, env->dst_cpu); | |
56cf515b | 8892 | } |
1e3c88bd | 8893 | |
630246a0 | 8894 | update_sg_lb_stats(env, sg, sgs, &sg_status); |
1e3c88bd | 8895 | |
b72ff13c PZ |
8896 | if (local_group) |
8897 | goto next_group; | |
8898 | ||
1e3c88bd | 8899 | |
b72ff13c | 8900 | if (update_sd_pick_busiest(env, sds, sg, sgs)) { |
532cb4c4 | 8901 | sds->busiest = sg; |
56cf515b | 8902 | sds->busiest_stat = *sgs; |
1e3c88bd PZ |
8903 | } |
8904 | ||
b72ff13c PZ |
8905 | next_group: |
8906 | /* Now, start updating sd_lb_stats */ | |
8907 | sds->total_load += sgs->group_load; | |
63b2ca30 | 8908 | sds->total_capacity += sgs->group_capacity; |
b72ff13c | 8909 | |
532cb4c4 | 8910 | sg = sg->next; |
bd939f45 | 8911 | } while (sg != env->sd->groups); |
0ec8aa00 | 8912 | |
0b0695f2 VG |
8913 | /* Tag domain that child domain prefers tasks go to siblings first */ |
8914 | sds->prefer_sibling = child && child->flags & SD_PREFER_SIBLING; | |
8915 | ||
f643ea22 VG |
8916 | #ifdef CONFIG_NO_HZ_COMMON |
8917 | if ((env->flags & LBF_NOHZ_AGAIN) && | |
8918 | cpumask_subset(nohz.idle_cpus_mask, sched_domain_span(env->sd))) { | |
8919 | ||
8920 | WRITE_ONCE(nohz.next_blocked, | |
8921 | jiffies + msecs_to_jiffies(LOAD_AVG_PERIOD)); | |
8922 | } | |
8923 | #endif | |
8924 | ||
0ec8aa00 PZ |
8925 | if (env->sd->flags & SD_NUMA) |
8926 | env->fbq_type = fbq_classify_group(&sds->busiest_stat); | |
4486edd1 TC |
8927 | |
8928 | if (!env->sd->parent) { | |
2802bf3c MR |
8929 | struct root_domain *rd = env->dst_rq->rd; |
8930 | ||
4486edd1 | 8931 | /* update overload indicator if we are at root domain */ |
2802bf3c MR |
8932 | WRITE_ONCE(rd->overload, sg_status & SG_OVERLOAD); |
8933 | ||
8934 | /* Update over-utilization (tipping point, U >= 0) indicator */ | |
8935 | WRITE_ONCE(rd->overutilized, sg_status & SG_OVERUTILIZED); | |
f9f240f9 | 8936 | trace_sched_overutilized_tp(rd, sg_status & SG_OVERUTILIZED); |
2802bf3c | 8937 | } else if (sg_status & SG_OVERUTILIZED) { |
f9f240f9 QY |
8938 | struct root_domain *rd = env->dst_rq->rd; |
8939 | ||
8940 | WRITE_ONCE(rd->overutilized, SG_OVERUTILIZED); | |
8941 | trace_sched_overutilized_tp(rd, SG_OVERUTILIZED); | |
4486edd1 | 8942 | } |
532cb4c4 MN |
8943 | } |
8944 | ||
fb86f5b2 MG |
8945 | static inline long adjust_numa_imbalance(int imbalance, int src_nr_running) |
8946 | { | |
8947 | unsigned int imbalance_min; | |
8948 | ||
8949 | /* | |
8950 | * Allow a small imbalance based on a simple pair of communicating | |
8951 | * tasks that remain local when the source domain is almost idle. | |
8952 | */ | |
8953 | imbalance_min = 2; | |
8954 | if (src_nr_running <= imbalance_min) | |
8955 | return 0; | |
8956 | ||
8957 | return imbalance; | |
8958 | } | |
8959 | ||
1e3c88bd PZ |
8960 | /** |
8961 | * calculate_imbalance - Calculate the amount of imbalance present within the | |
8962 | * groups of a given sched_domain during load balance. | |
bd939f45 | 8963 | * @env: load balance environment |
1e3c88bd | 8964 | * @sds: statistics of the sched_domain whose imbalance is to be calculated. |
1e3c88bd | 8965 | */ |
bd939f45 | 8966 | static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *sds) |
1e3c88bd | 8967 | { |
56cf515b JK |
8968 | struct sg_lb_stats *local, *busiest; |
8969 | ||
8970 | local = &sds->local_stat; | |
56cf515b | 8971 | busiest = &sds->busiest_stat; |
dd5feea1 | 8972 | |
0b0695f2 VG |
8973 | if (busiest->group_type == group_misfit_task) { |
8974 | /* Set imbalance to allow misfit tasks to be balanced. */ | |
8975 | env->migration_type = migrate_misfit; | |
c63be7be | 8976 | env->imbalance = 1; |
0b0695f2 VG |
8977 | return; |
8978 | } | |
8979 | ||
8980 | if (busiest->group_type == group_asym_packing) { | |
8981 | /* | |
8982 | * In case of asym capacity, we will try to migrate all load to | |
8983 | * the preferred CPU. | |
8984 | */ | |
8985 | env->migration_type = migrate_task; | |
8986 | env->imbalance = busiest->sum_h_nr_running; | |
8987 | return; | |
8988 | } | |
8989 | ||
8990 | if (busiest->group_type == group_imbalanced) { | |
8991 | /* | |
8992 | * In the group_imb case we cannot rely on group-wide averages | |
8993 | * to ensure CPU-load equilibrium, try to move any task to fix | |
8994 | * the imbalance. The next load balance will take care of | |
8995 | * balancing back the system. | |
8996 | */ | |
8997 | env->migration_type = migrate_task; | |
8998 | env->imbalance = 1; | |
490ba971 VG |
8999 | return; |
9000 | } | |
9001 | ||
1e3c88bd | 9002 | /* |
0b0695f2 | 9003 | * Try to use spare capacity of local group without overloading it or |
a9723389 | 9004 | * emptying busiest. |
1e3c88bd | 9005 | */ |
0b0695f2 VG |
9006 | if (local->group_type == group_has_spare) { |
9007 | if (busiest->group_type > group_fully_busy) { | |
9008 | /* | |
9009 | * If busiest is overloaded, try to fill spare | |
9010 | * capacity. This might end up creating spare capacity | |
9011 | * in busiest or busiest still being overloaded but | |
9012 | * there is no simple way to directly compute the | |
9013 | * amount of load to migrate in order to balance the | |
9014 | * system. | |
9015 | */ | |
9016 | env->migration_type = migrate_util; | |
9017 | env->imbalance = max(local->group_capacity, local->group_util) - | |
9018 | local->group_util; | |
9019 | ||
9020 | /* | |
9021 | * In some cases, the group's utilization is max or even | |
9022 | * higher than capacity because of migrations but the | |
9023 | * local CPU is (newly) idle. There is at least one | |
9024 | * waiting task in this overloaded busiest group. Let's | |
9025 | * try to pull it. | |
9026 | */ | |
9027 | if (env->idle != CPU_NOT_IDLE && env->imbalance == 0) { | |
9028 | env->migration_type = migrate_task; | |
9029 | env->imbalance = 1; | |
9030 | } | |
9031 | ||
9032 | return; | |
9033 | } | |
9034 | ||
9035 | if (busiest->group_weight == 1 || sds->prefer_sibling) { | |
5e23e474 | 9036 | unsigned int nr_diff = busiest->sum_nr_running; |
0b0695f2 VG |
9037 | /* |
9038 | * When prefer sibling, evenly spread running tasks on | |
9039 | * groups. | |
9040 | */ | |
9041 | env->migration_type = migrate_task; | |
5e23e474 | 9042 | lsub_positive(&nr_diff, local->sum_nr_running); |
0b0695f2 | 9043 | env->imbalance = nr_diff >> 1; |
b396f523 | 9044 | } else { |
0b0695f2 | 9045 | |
b396f523 MG |
9046 | /* |
9047 | * If there is no overload, we just want to even the number of | |
9048 | * idle cpus. | |
9049 | */ | |
9050 | env->migration_type = migrate_task; | |
9051 | env->imbalance = max_t(long, 0, (local->idle_cpus - | |
0b0695f2 | 9052 | busiest->idle_cpus) >> 1); |
b396f523 MG |
9053 | } |
9054 | ||
9055 | /* Consider allowing a small imbalance between NUMA groups */ | |
fb86f5b2 MG |
9056 | if (env->sd->flags & SD_NUMA) |
9057 | env->imbalance = adjust_numa_imbalance(env->imbalance, | |
9058 | busiest->sum_nr_running); | |
b396f523 | 9059 | |
fcf0553d | 9060 | return; |
1e3c88bd PZ |
9061 | } |
9062 | ||
9a5d9ba6 | 9063 | /* |
0b0695f2 VG |
9064 | * Local is fully busy but has to take more load to relieve the |
9065 | * busiest group | |
9a5d9ba6 | 9066 | */ |
0b0695f2 VG |
9067 | if (local->group_type < group_overloaded) { |
9068 | /* | |
9069 | * Local will become overloaded so the avg_load metrics are | |
9070 | * finally needed. | |
9071 | */ | |
9072 | ||
9073 | local->avg_load = (local->group_load * SCHED_CAPACITY_SCALE) / | |
9074 | local->group_capacity; | |
9075 | ||
9076 | sds->avg_load = (sds->total_load * SCHED_CAPACITY_SCALE) / | |
9077 | sds->total_capacity; | |
111688ca AL |
9078 | /* |
9079 | * If the local group is more loaded than the selected | |
9080 | * busiest group don't try to pull any tasks. | |
9081 | */ | |
9082 | if (local->avg_load >= busiest->avg_load) { | |
9083 | env->imbalance = 0; | |
9084 | return; | |
9085 | } | |
dd5feea1 SS |
9086 | } |
9087 | ||
9088 | /* | |
0b0695f2 VG |
9089 | * Both group are or will become overloaded and we're trying to get all |
9090 | * the CPUs to the average_load, so we don't want to push ourselves | |
9091 | * above the average load, nor do we wish to reduce the max loaded CPU | |
9092 | * below the average load. At the same time, we also don't want to | |
9093 | * reduce the group load below the group capacity. Thus we look for | |
9094 | * the minimum possible imbalance. | |
dd5feea1 | 9095 | */ |
0b0695f2 | 9096 | env->migration_type = migrate_load; |
56cf515b | 9097 | env->imbalance = min( |
0b0695f2 | 9098 | (busiest->avg_load - sds->avg_load) * busiest->group_capacity, |
63b2ca30 | 9099 | (sds->avg_load - local->avg_load) * local->group_capacity |
ca8ce3d0 | 9100 | ) / SCHED_CAPACITY_SCALE; |
1e3c88bd | 9101 | } |
fab47622 | 9102 | |
1e3c88bd PZ |
9103 | /******* find_busiest_group() helpers end here *********************/ |
9104 | ||
0b0695f2 VG |
9105 | /* |
9106 | * Decision matrix according to the local and busiest group type: | |
9107 | * | |
9108 | * busiest \ local has_spare fully_busy misfit asym imbalanced overloaded | |
9109 | * has_spare nr_idle balanced N/A N/A balanced balanced | |
9110 | * fully_busy nr_idle nr_idle N/A N/A balanced balanced | |
9111 | * misfit_task force N/A N/A N/A force force | |
9112 | * asym_packing force force N/A N/A force force | |
9113 | * imbalanced force force N/A N/A force force | |
9114 | * overloaded force force N/A N/A force avg_load | |
9115 | * | |
9116 | * N/A : Not Applicable because already filtered while updating | |
9117 | * statistics. | |
9118 | * balanced : The system is balanced for these 2 groups. | |
9119 | * force : Calculate the imbalance as load migration is probably needed. | |
9120 | * avg_load : Only if imbalance is significant enough. | |
9121 | * nr_idle : dst_cpu is not busy and the number of idle CPUs is quite | |
9122 | * different in groups. | |
9123 | */ | |
9124 | ||
1e3c88bd PZ |
9125 | /** |
9126 | * find_busiest_group - Returns the busiest group within the sched_domain | |
0a9b23ce | 9127 | * if there is an imbalance. |
1e3c88bd | 9128 | * |
a3df0679 | 9129 | * Also calculates the amount of runnable load which should be moved |
1e3c88bd PZ |
9130 | * to restore balance. |
9131 | * | |
cd96891d | 9132 | * @env: The load balancing environment. |
1e3c88bd | 9133 | * |
e69f6186 | 9134 | * Return: - The busiest group if imbalance exists. |
1e3c88bd | 9135 | */ |
56cf515b | 9136 | static struct sched_group *find_busiest_group(struct lb_env *env) |
1e3c88bd | 9137 | { |
56cf515b | 9138 | struct sg_lb_stats *local, *busiest; |
1e3c88bd PZ |
9139 | struct sd_lb_stats sds; |
9140 | ||
147c5fc2 | 9141 | init_sd_lb_stats(&sds); |
1e3c88bd PZ |
9142 | |
9143 | /* | |
b0fb1eb4 | 9144 | * Compute the various statistics relevant for load balancing at |
1e3c88bd PZ |
9145 | * this level. |
9146 | */ | |
23f0d209 | 9147 | update_sd_lb_stats(env, &sds); |
2802bf3c | 9148 | |
f8a696f2 | 9149 | if (sched_energy_enabled()) { |
2802bf3c MR |
9150 | struct root_domain *rd = env->dst_rq->rd; |
9151 | ||
9152 | if (rcu_dereference(rd->pd) && !READ_ONCE(rd->overutilized)) | |
9153 | goto out_balanced; | |
9154 | } | |
9155 | ||
56cf515b JK |
9156 | local = &sds.local_stat; |
9157 | busiest = &sds.busiest_stat; | |
1e3c88bd | 9158 | |
cc57aa8f | 9159 | /* There is no busy sibling group to pull tasks from */ |
0b0695f2 | 9160 | if (!sds.busiest) |
1e3c88bd PZ |
9161 | goto out_balanced; |
9162 | ||
0b0695f2 VG |
9163 | /* Misfit tasks should be dealt with regardless of the avg load */ |
9164 | if (busiest->group_type == group_misfit_task) | |
9165 | goto force_balance; | |
9166 | ||
9167 | /* ASYM feature bypasses nice load balance check */ | |
9168 | if (busiest->group_type == group_asym_packing) | |
9169 | goto force_balance; | |
b0432d8f | 9170 | |
866ab43e PZ |
9171 | /* |
9172 | * If the busiest group is imbalanced the below checks don't | |
30ce5dab | 9173 | * work because they assume all things are equal, which typically |
3bd37062 | 9174 | * isn't true due to cpus_ptr constraints and the like. |
866ab43e | 9175 | */ |
caeb178c | 9176 | if (busiest->group_type == group_imbalanced) |
866ab43e PZ |
9177 | goto force_balance; |
9178 | ||
cc57aa8f | 9179 | /* |
9c58c79a | 9180 | * If the local group is busier than the selected busiest group |
cc57aa8f PZ |
9181 | * don't try and pull any tasks. |
9182 | */ | |
0b0695f2 | 9183 | if (local->group_type > busiest->group_type) |
1e3c88bd PZ |
9184 | goto out_balanced; |
9185 | ||
cc57aa8f | 9186 | /* |
0b0695f2 VG |
9187 | * When groups are overloaded, use the avg_load to ensure fairness |
9188 | * between tasks. | |
cc57aa8f | 9189 | */ |
0b0695f2 VG |
9190 | if (local->group_type == group_overloaded) { |
9191 | /* | |
9192 | * If the local group is more loaded than the selected | |
9193 | * busiest group don't try to pull any tasks. | |
9194 | */ | |
9195 | if (local->avg_load >= busiest->avg_load) | |
9196 | goto out_balanced; | |
9197 | ||
9198 | /* XXX broken for overlapping NUMA groups */ | |
9199 | sds.avg_load = (sds.total_load * SCHED_CAPACITY_SCALE) / | |
9200 | sds.total_capacity; | |
1e3c88bd | 9201 | |
aae6d3dd | 9202 | /* |
0b0695f2 VG |
9203 | * Don't pull any tasks if this group is already above the |
9204 | * domain average load. | |
aae6d3dd | 9205 | */ |
0b0695f2 | 9206 | if (local->avg_load >= sds.avg_load) |
aae6d3dd | 9207 | goto out_balanced; |
0b0695f2 | 9208 | |
c186fafe | 9209 | /* |
0b0695f2 VG |
9210 | * If the busiest group is more loaded, use imbalance_pct to be |
9211 | * conservative. | |
c186fafe | 9212 | */ |
56cf515b JK |
9213 | if (100 * busiest->avg_load <= |
9214 | env->sd->imbalance_pct * local->avg_load) | |
c186fafe | 9215 | goto out_balanced; |
aae6d3dd | 9216 | } |
1e3c88bd | 9217 | |
0b0695f2 VG |
9218 | /* Try to move all excess tasks to child's sibling domain */ |
9219 | if (sds.prefer_sibling && local->group_type == group_has_spare && | |
5e23e474 | 9220 | busiest->sum_nr_running > local->sum_nr_running + 1) |
0b0695f2 VG |
9221 | goto force_balance; |
9222 | ||
2ab4092f VG |
9223 | if (busiest->group_type != group_overloaded) { |
9224 | if (env->idle == CPU_NOT_IDLE) | |
9225 | /* | |
9226 | * If the busiest group is not overloaded (and as a | |
9227 | * result the local one too) but this CPU is already | |
9228 | * busy, let another idle CPU try to pull task. | |
9229 | */ | |
9230 | goto out_balanced; | |
9231 | ||
9232 | if (busiest->group_weight > 1 && | |
9233 | local->idle_cpus <= (busiest->idle_cpus + 1)) | |
9234 | /* | |
9235 | * If the busiest group is not overloaded | |
9236 | * and there is no imbalance between this and busiest | |
9237 | * group wrt idle CPUs, it is balanced. The imbalance | |
9238 | * becomes significant if the diff is greater than 1 | |
9239 | * otherwise we might end up to just move the imbalance | |
9240 | * on another group. Of course this applies only if | |
9241 | * there is more than 1 CPU per group. | |
9242 | */ | |
9243 | goto out_balanced; | |
9244 | ||
9245 | if (busiest->sum_h_nr_running == 1) | |
9246 | /* | |
9247 | * busiest doesn't have any tasks waiting to run | |
9248 | */ | |
9249 | goto out_balanced; | |
9250 | } | |
0b0695f2 | 9251 | |
fab47622 | 9252 | force_balance: |
1e3c88bd | 9253 | /* Looks like there is an imbalance. Compute it */ |
bd939f45 | 9254 | calculate_imbalance(env, &sds); |
bb3485c8 | 9255 | return env->imbalance ? sds.busiest : NULL; |
1e3c88bd PZ |
9256 | |
9257 | out_balanced: | |
bd939f45 | 9258 | env->imbalance = 0; |
1e3c88bd PZ |
9259 | return NULL; |
9260 | } | |
9261 | ||
9262 | /* | |
97fb7a0a | 9263 | * find_busiest_queue - find the busiest runqueue among the CPUs in the group. |
1e3c88bd | 9264 | */ |
bd939f45 | 9265 | static struct rq *find_busiest_queue(struct lb_env *env, |
b9403130 | 9266 | struct sched_group *group) |
1e3c88bd PZ |
9267 | { |
9268 | struct rq *busiest = NULL, *rq; | |
0b0695f2 VG |
9269 | unsigned long busiest_util = 0, busiest_load = 0, busiest_capacity = 1; |
9270 | unsigned int busiest_nr = 0; | |
1e3c88bd PZ |
9271 | int i; |
9272 | ||
ae4df9d6 | 9273 | for_each_cpu_and(i, sched_group_span(group), env->cpus) { |
0b0695f2 VG |
9274 | unsigned long capacity, load, util; |
9275 | unsigned int nr_running; | |
0ec8aa00 PZ |
9276 | enum fbq_type rt; |
9277 | ||
9278 | rq = cpu_rq(i); | |
9279 | rt = fbq_classify_rq(rq); | |
1e3c88bd | 9280 | |
0ec8aa00 PZ |
9281 | /* |
9282 | * We classify groups/runqueues into three groups: | |
9283 | * - regular: there are !numa tasks | |
9284 | * - remote: there are numa tasks that run on the 'wrong' node | |
9285 | * - all: there is no distinction | |
9286 | * | |
9287 | * In order to avoid migrating ideally placed numa tasks, | |
9288 | * ignore those when there's better options. | |
9289 | * | |
9290 | * If we ignore the actual busiest queue to migrate another | |
9291 | * task, the next balance pass can still reduce the busiest | |
9292 | * queue by moving tasks around inside the node. | |
9293 | * | |
9294 | * If we cannot move enough load due to this classification | |
9295 | * the next pass will adjust the group classification and | |
9296 | * allow migration of more tasks. | |
9297 | * | |
9298 | * Both cases only affect the total convergence complexity. | |
9299 | */ | |
9300 | if (rt > env->fbq_type) | |
9301 | continue; | |
9302 | ||
ced549fa | 9303 | capacity = capacity_of(i); |
0b0695f2 | 9304 | nr_running = rq->cfs.h_nr_running; |
9d5efe05 | 9305 | |
4ad3831a CR |
9306 | /* |
9307 | * For ASYM_CPUCAPACITY domains, don't pick a CPU that could | |
9308 | * eventually lead to active_balancing high->low capacity. | |
9309 | * Higher per-CPU capacity is considered better than balancing | |
9310 | * average load. | |
9311 | */ | |
9312 | if (env->sd->flags & SD_ASYM_CPUCAPACITY && | |
9313 | capacity_of(env->dst_cpu) < capacity && | |
0b0695f2 | 9314 | nr_running == 1) |
4ad3831a CR |
9315 | continue; |
9316 | ||
0b0695f2 VG |
9317 | switch (env->migration_type) { |
9318 | case migrate_load: | |
9319 | /* | |
b0fb1eb4 VG |
9320 | * When comparing with load imbalance, use cpu_load() |
9321 | * which is not scaled with the CPU capacity. | |
0b0695f2 | 9322 | */ |
b0fb1eb4 | 9323 | load = cpu_load(rq); |
1e3c88bd | 9324 | |
0b0695f2 VG |
9325 | if (nr_running == 1 && load > env->imbalance && |
9326 | !check_cpu_capacity(rq, env->sd)) | |
9327 | break; | |
ea67821b | 9328 | |
0b0695f2 VG |
9329 | /* |
9330 | * For the load comparisons with the other CPUs, | |
b0fb1eb4 VG |
9331 | * consider the cpu_load() scaled with the CPU |
9332 | * capacity, so that the load can be moved away | |
9333 | * from the CPU that is potentially running at a | |
9334 | * lower capacity. | |
0b0695f2 VG |
9335 | * |
9336 | * Thus we're looking for max(load_i / capacity_i), | |
9337 | * crosswise multiplication to rid ourselves of the | |
9338 | * division works out to: | |
9339 | * load_i * capacity_j > load_j * capacity_i; | |
9340 | * where j is our previous maximum. | |
9341 | */ | |
9342 | if (load * busiest_capacity > busiest_load * capacity) { | |
9343 | busiest_load = load; | |
9344 | busiest_capacity = capacity; | |
9345 | busiest = rq; | |
9346 | } | |
9347 | break; | |
9348 | ||
9349 | case migrate_util: | |
9350 | util = cpu_util(cpu_of(rq)); | |
9351 | ||
c32b4308 VG |
9352 | /* |
9353 | * Don't try to pull utilization from a CPU with one | |
9354 | * running task. Whatever its utilization, we will fail | |
9355 | * detach the task. | |
9356 | */ | |
9357 | if (nr_running <= 1) | |
9358 | continue; | |
9359 | ||
0b0695f2 VG |
9360 | if (busiest_util < util) { |
9361 | busiest_util = util; | |
9362 | busiest = rq; | |
9363 | } | |
9364 | break; | |
9365 | ||
9366 | case migrate_task: | |
9367 | if (busiest_nr < nr_running) { | |
9368 | busiest_nr = nr_running; | |
9369 | busiest = rq; | |
9370 | } | |
9371 | break; | |
9372 | ||
9373 | case migrate_misfit: | |
9374 | /* | |
9375 | * For ASYM_CPUCAPACITY domains with misfit tasks we | |
9376 | * simply seek the "biggest" misfit task. | |
9377 | */ | |
9378 | if (rq->misfit_task_load > busiest_load) { | |
9379 | busiest_load = rq->misfit_task_load; | |
9380 | busiest = rq; | |
9381 | } | |
9382 | ||
9383 | break; | |
1e3c88bd | 9384 | |
1e3c88bd PZ |
9385 | } |
9386 | } | |
9387 | ||
9388 | return busiest; | |
9389 | } | |
9390 | ||
9391 | /* | |
9392 | * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but | |
9393 | * so long as it is large enough. | |
9394 | */ | |
9395 | #define MAX_PINNED_INTERVAL 512 | |
9396 | ||
46a745d9 VG |
9397 | static inline bool |
9398 | asym_active_balance(struct lb_env *env) | |
1af3ed3d | 9399 | { |
46a745d9 VG |
9400 | /* |
9401 | * ASYM_PACKING needs to force migrate tasks from busy but | |
9402 | * lower priority CPUs in order to pack all tasks in the | |
9403 | * highest priority CPUs. | |
9404 | */ | |
9405 | return env->idle != CPU_NOT_IDLE && (env->sd->flags & SD_ASYM_PACKING) && | |
9406 | sched_asym_prefer(env->dst_cpu, env->src_cpu); | |
9407 | } | |
bd939f45 | 9408 | |
46a745d9 VG |
9409 | static inline bool |
9410 | voluntary_active_balance(struct lb_env *env) | |
9411 | { | |
9412 | struct sched_domain *sd = env->sd; | |
532cb4c4 | 9413 | |
46a745d9 VG |
9414 | if (asym_active_balance(env)) |
9415 | return 1; | |
1af3ed3d | 9416 | |
1aaf90a4 VG |
9417 | /* |
9418 | * The dst_cpu is idle and the src_cpu CPU has only 1 CFS task. | |
9419 | * It's worth migrating the task if the src_cpu's capacity is reduced | |
9420 | * because of other sched_class or IRQs if more capacity stays | |
9421 | * available on dst_cpu. | |
9422 | */ | |
9423 | if ((env->idle != CPU_NOT_IDLE) && | |
9424 | (env->src_rq->cfs.h_nr_running == 1)) { | |
9425 | if ((check_cpu_capacity(env->src_rq, sd)) && | |
9426 | (capacity_of(env->src_cpu)*sd->imbalance_pct < capacity_of(env->dst_cpu)*100)) | |
9427 | return 1; | |
9428 | } | |
9429 | ||
0b0695f2 | 9430 | if (env->migration_type == migrate_misfit) |
cad68e55 MR |
9431 | return 1; |
9432 | ||
46a745d9 VG |
9433 | return 0; |
9434 | } | |
9435 | ||
9436 | static int need_active_balance(struct lb_env *env) | |
9437 | { | |
9438 | struct sched_domain *sd = env->sd; | |
9439 | ||
9440 | if (voluntary_active_balance(env)) | |
9441 | return 1; | |
9442 | ||
1af3ed3d PZ |
9443 | return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2); |
9444 | } | |
9445 | ||
969c7921 TH |
9446 | static int active_load_balance_cpu_stop(void *data); |
9447 | ||
23f0d209 JK |
9448 | static int should_we_balance(struct lb_env *env) |
9449 | { | |
9450 | struct sched_group *sg = env->sd->groups; | |
64297f2b | 9451 | int cpu; |
23f0d209 | 9452 | |
024c9d2f PZ |
9453 | /* |
9454 | * Ensure the balancing environment is consistent; can happen | |
9455 | * when the softirq triggers 'during' hotplug. | |
9456 | */ | |
9457 | if (!cpumask_test_cpu(env->dst_cpu, env->cpus)) | |
9458 | return 0; | |
9459 | ||
23f0d209 | 9460 | /* |
97fb7a0a | 9461 | * In the newly idle case, we will allow all the CPUs |
23f0d209 JK |
9462 | * to do the newly idle load balance. |
9463 | */ | |
9464 | if (env->idle == CPU_NEWLY_IDLE) | |
9465 | return 1; | |
9466 | ||
97fb7a0a | 9467 | /* Try to find first idle CPU */ |
e5c14b1f | 9468 | for_each_cpu_and(cpu, group_balance_mask(sg), env->cpus) { |
af218122 | 9469 | if (!idle_cpu(cpu)) |
23f0d209 JK |
9470 | continue; |
9471 | ||
64297f2b PW |
9472 | /* Are we the first idle CPU? */ |
9473 | return cpu == env->dst_cpu; | |
23f0d209 JK |
9474 | } |
9475 | ||
64297f2b PW |
9476 | /* Are we the first CPU of this group ? */ |
9477 | return group_balance_cpu(sg) == env->dst_cpu; | |
23f0d209 JK |
9478 | } |
9479 | ||
1e3c88bd PZ |
9480 | /* |
9481 | * Check this_cpu to ensure it is balanced within domain. Attempt to move | |
9482 | * tasks if there is an imbalance. | |
9483 | */ | |
9484 | static int load_balance(int this_cpu, struct rq *this_rq, | |
9485 | struct sched_domain *sd, enum cpu_idle_type idle, | |
23f0d209 | 9486 | int *continue_balancing) |
1e3c88bd | 9487 | { |
88b8dac0 | 9488 | int ld_moved, cur_ld_moved, active_balance = 0; |
6263322c | 9489 | struct sched_domain *sd_parent = sd->parent; |
1e3c88bd | 9490 | struct sched_group *group; |
1e3c88bd | 9491 | struct rq *busiest; |
8a8c69c3 | 9492 | struct rq_flags rf; |
4ba29684 | 9493 | struct cpumask *cpus = this_cpu_cpumask_var_ptr(load_balance_mask); |
1e3c88bd | 9494 | |
8e45cb54 PZ |
9495 | struct lb_env env = { |
9496 | .sd = sd, | |
ddcdf6e7 PZ |
9497 | .dst_cpu = this_cpu, |
9498 | .dst_rq = this_rq, | |
ae4df9d6 | 9499 | .dst_grpmask = sched_group_span(sd->groups), |
8e45cb54 | 9500 | .idle = idle, |
eb95308e | 9501 | .loop_break = sched_nr_migrate_break, |
b9403130 | 9502 | .cpus = cpus, |
0ec8aa00 | 9503 | .fbq_type = all, |
163122b7 | 9504 | .tasks = LIST_HEAD_INIT(env.tasks), |
8e45cb54 PZ |
9505 | }; |
9506 | ||
65a4433a | 9507 | cpumask_and(cpus, sched_domain_span(sd), cpu_active_mask); |
1e3c88bd | 9508 | |
ae92882e | 9509 | schedstat_inc(sd->lb_count[idle]); |
1e3c88bd PZ |
9510 | |
9511 | redo: | |
23f0d209 JK |
9512 | if (!should_we_balance(&env)) { |
9513 | *continue_balancing = 0; | |
1e3c88bd | 9514 | goto out_balanced; |
23f0d209 | 9515 | } |
1e3c88bd | 9516 | |
23f0d209 | 9517 | group = find_busiest_group(&env); |
1e3c88bd | 9518 | if (!group) { |
ae92882e | 9519 | schedstat_inc(sd->lb_nobusyg[idle]); |
1e3c88bd PZ |
9520 | goto out_balanced; |
9521 | } | |
9522 | ||
b9403130 | 9523 | busiest = find_busiest_queue(&env, group); |
1e3c88bd | 9524 | if (!busiest) { |
ae92882e | 9525 | schedstat_inc(sd->lb_nobusyq[idle]); |
1e3c88bd PZ |
9526 | goto out_balanced; |
9527 | } | |
9528 | ||
78feefc5 | 9529 | BUG_ON(busiest == env.dst_rq); |
1e3c88bd | 9530 | |
ae92882e | 9531 | schedstat_add(sd->lb_imbalance[idle], env.imbalance); |
1e3c88bd | 9532 | |
1aaf90a4 VG |
9533 | env.src_cpu = busiest->cpu; |
9534 | env.src_rq = busiest; | |
9535 | ||
1e3c88bd PZ |
9536 | ld_moved = 0; |
9537 | if (busiest->nr_running > 1) { | |
9538 | /* | |
9539 | * Attempt to move tasks. If find_busiest_group has found | |
9540 | * an imbalance but busiest->nr_running <= 1, the group is | |
9541 | * still unbalanced. ld_moved simply stays zero, so it is | |
9542 | * correctly treated as an imbalance. | |
9543 | */ | |
8e45cb54 | 9544 | env.flags |= LBF_ALL_PINNED; |
c82513e5 | 9545 | env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running); |
8e45cb54 | 9546 | |
5d6523eb | 9547 | more_balance: |
8a8c69c3 | 9548 | rq_lock_irqsave(busiest, &rf); |
3bed5e21 | 9549 | update_rq_clock(busiest); |
88b8dac0 SV |
9550 | |
9551 | /* | |
9552 | * cur_ld_moved - load moved in current iteration | |
9553 | * ld_moved - cumulative load moved across iterations | |
9554 | */ | |
163122b7 | 9555 | cur_ld_moved = detach_tasks(&env); |
1e3c88bd PZ |
9556 | |
9557 | /* | |
163122b7 KT |
9558 | * We've detached some tasks from busiest_rq. Every |
9559 | * task is masked "TASK_ON_RQ_MIGRATING", so we can safely | |
9560 | * unlock busiest->lock, and we are able to be sure | |
9561 | * that nobody can manipulate the tasks in parallel. | |
9562 | * See task_rq_lock() family for the details. | |
1e3c88bd | 9563 | */ |
163122b7 | 9564 | |
8a8c69c3 | 9565 | rq_unlock(busiest, &rf); |
163122b7 KT |
9566 | |
9567 | if (cur_ld_moved) { | |
9568 | attach_tasks(&env); | |
9569 | ld_moved += cur_ld_moved; | |
9570 | } | |
9571 | ||
8a8c69c3 | 9572 | local_irq_restore(rf.flags); |
88b8dac0 | 9573 | |
f1cd0858 JK |
9574 | if (env.flags & LBF_NEED_BREAK) { |
9575 | env.flags &= ~LBF_NEED_BREAK; | |
9576 | goto more_balance; | |
9577 | } | |
9578 | ||
88b8dac0 SV |
9579 | /* |
9580 | * Revisit (affine) tasks on src_cpu that couldn't be moved to | |
9581 | * us and move them to an alternate dst_cpu in our sched_group | |
9582 | * where they can run. The upper limit on how many times we | |
97fb7a0a | 9583 | * iterate on same src_cpu is dependent on number of CPUs in our |
88b8dac0 SV |
9584 | * sched_group. |
9585 | * | |
9586 | * This changes load balance semantics a bit on who can move | |
9587 | * load to a given_cpu. In addition to the given_cpu itself | |
9588 | * (or a ilb_cpu acting on its behalf where given_cpu is | |
9589 | * nohz-idle), we now have balance_cpu in a position to move | |
9590 | * load to given_cpu. In rare situations, this may cause | |
9591 | * conflicts (balance_cpu and given_cpu/ilb_cpu deciding | |
9592 | * _independently_ and at _same_ time to move some load to | |
9593 | * given_cpu) causing exceess load to be moved to given_cpu. | |
9594 | * This however should not happen so much in practice and | |
9595 | * moreover subsequent load balance cycles should correct the | |
9596 | * excess load moved. | |
9597 | */ | |
6263322c | 9598 | if ((env.flags & LBF_DST_PINNED) && env.imbalance > 0) { |
88b8dac0 | 9599 | |
97fb7a0a | 9600 | /* Prevent to re-select dst_cpu via env's CPUs */ |
c89d92ed | 9601 | __cpumask_clear_cpu(env.dst_cpu, env.cpus); |
7aff2e3a | 9602 | |
78feefc5 | 9603 | env.dst_rq = cpu_rq(env.new_dst_cpu); |
88b8dac0 | 9604 | env.dst_cpu = env.new_dst_cpu; |
6263322c | 9605 | env.flags &= ~LBF_DST_PINNED; |
88b8dac0 SV |
9606 | env.loop = 0; |
9607 | env.loop_break = sched_nr_migrate_break; | |
e02e60c1 | 9608 | |
88b8dac0 SV |
9609 | /* |
9610 | * Go back to "more_balance" rather than "redo" since we | |
9611 | * need to continue with same src_cpu. | |
9612 | */ | |
9613 | goto more_balance; | |
9614 | } | |
1e3c88bd | 9615 | |
6263322c PZ |
9616 | /* |
9617 | * We failed to reach balance because of affinity. | |
9618 | */ | |
9619 | if (sd_parent) { | |
63b2ca30 | 9620 | int *group_imbalance = &sd_parent->groups->sgc->imbalance; |
6263322c | 9621 | |
afdeee05 | 9622 | if ((env.flags & LBF_SOME_PINNED) && env.imbalance > 0) |
6263322c | 9623 | *group_imbalance = 1; |
6263322c PZ |
9624 | } |
9625 | ||
1e3c88bd | 9626 | /* All tasks on this runqueue were pinned by CPU affinity */ |
8e45cb54 | 9627 | if (unlikely(env.flags & LBF_ALL_PINNED)) { |
c89d92ed | 9628 | __cpumask_clear_cpu(cpu_of(busiest), cpus); |
65a4433a JH |
9629 | /* |
9630 | * Attempting to continue load balancing at the current | |
9631 | * sched_domain level only makes sense if there are | |
9632 | * active CPUs remaining as possible busiest CPUs to | |
9633 | * pull load from which are not contained within the | |
9634 | * destination group that is receiving any migrated | |
9635 | * load. | |
9636 | */ | |
9637 | if (!cpumask_subset(cpus, env.dst_grpmask)) { | |
bbf18b19 PN |
9638 | env.loop = 0; |
9639 | env.loop_break = sched_nr_migrate_break; | |
1e3c88bd | 9640 | goto redo; |
bbf18b19 | 9641 | } |
afdeee05 | 9642 | goto out_all_pinned; |
1e3c88bd PZ |
9643 | } |
9644 | } | |
9645 | ||
9646 | if (!ld_moved) { | |
ae92882e | 9647 | schedstat_inc(sd->lb_failed[idle]); |
58b26c4c VP |
9648 | /* |
9649 | * Increment the failure counter only on periodic balance. | |
9650 | * We do not want newidle balance, which can be very | |
9651 | * frequent, pollute the failure counter causing | |
9652 | * excessive cache_hot migrations and active balances. | |
9653 | */ | |
9654 | if (idle != CPU_NEWLY_IDLE) | |
9655 | sd->nr_balance_failed++; | |
1e3c88bd | 9656 | |
bd939f45 | 9657 | if (need_active_balance(&env)) { |
8a8c69c3 PZ |
9658 | unsigned long flags; |
9659 | ||
1e3c88bd PZ |
9660 | raw_spin_lock_irqsave(&busiest->lock, flags); |
9661 | ||
97fb7a0a IM |
9662 | /* |
9663 | * Don't kick the active_load_balance_cpu_stop, | |
9664 | * if the curr task on busiest CPU can't be | |
9665 | * moved to this_cpu: | |
1e3c88bd | 9666 | */ |
3bd37062 | 9667 | if (!cpumask_test_cpu(this_cpu, busiest->curr->cpus_ptr)) { |
1e3c88bd PZ |
9668 | raw_spin_unlock_irqrestore(&busiest->lock, |
9669 | flags); | |
8e45cb54 | 9670 | env.flags |= LBF_ALL_PINNED; |
1e3c88bd PZ |
9671 | goto out_one_pinned; |
9672 | } | |
9673 | ||
969c7921 TH |
9674 | /* |
9675 | * ->active_balance synchronizes accesses to | |
9676 | * ->active_balance_work. Once set, it's cleared | |
9677 | * only after active load balance is finished. | |
9678 | */ | |
1e3c88bd PZ |
9679 | if (!busiest->active_balance) { |
9680 | busiest->active_balance = 1; | |
9681 | busiest->push_cpu = this_cpu; | |
9682 | active_balance = 1; | |
9683 | } | |
9684 | raw_spin_unlock_irqrestore(&busiest->lock, flags); | |
969c7921 | 9685 | |
bd939f45 | 9686 | if (active_balance) { |
969c7921 TH |
9687 | stop_one_cpu_nowait(cpu_of(busiest), |
9688 | active_load_balance_cpu_stop, busiest, | |
9689 | &busiest->active_balance_work); | |
bd939f45 | 9690 | } |
1e3c88bd | 9691 | |
d02c0711 | 9692 | /* We've kicked active balancing, force task migration. */ |
1e3c88bd PZ |
9693 | sd->nr_balance_failed = sd->cache_nice_tries+1; |
9694 | } | |
9695 | } else | |
9696 | sd->nr_balance_failed = 0; | |
9697 | ||
46a745d9 | 9698 | if (likely(!active_balance) || voluntary_active_balance(&env)) { |
1e3c88bd PZ |
9699 | /* We were unbalanced, so reset the balancing interval */ |
9700 | sd->balance_interval = sd->min_interval; | |
9701 | } else { | |
9702 | /* | |
9703 | * If we've begun active balancing, start to back off. This | |
9704 | * case may not be covered by the all_pinned logic if there | |
9705 | * is only 1 task on the busy runqueue (because we don't call | |
163122b7 | 9706 | * detach_tasks). |
1e3c88bd PZ |
9707 | */ |
9708 | if (sd->balance_interval < sd->max_interval) | |
9709 | sd->balance_interval *= 2; | |
9710 | } | |
9711 | ||
1e3c88bd PZ |
9712 | goto out; |
9713 | ||
9714 | out_balanced: | |
afdeee05 VG |
9715 | /* |
9716 | * We reach balance although we may have faced some affinity | |
f6cad8df VG |
9717 | * constraints. Clear the imbalance flag only if other tasks got |
9718 | * a chance to move and fix the imbalance. | |
afdeee05 | 9719 | */ |
f6cad8df | 9720 | if (sd_parent && !(env.flags & LBF_ALL_PINNED)) { |
afdeee05 VG |
9721 | int *group_imbalance = &sd_parent->groups->sgc->imbalance; |
9722 | ||
9723 | if (*group_imbalance) | |
9724 | *group_imbalance = 0; | |
9725 | } | |
9726 | ||
9727 | out_all_pinned: | |
9728 | /* | |
9729 | * We reach balance because all tasks are pinned at this level so | |
9730 | * we can't migrate them. Let the imbalance flag set so parent level | |
9731 | * can try to migrate them. | |
9732 | */ | |
ae92882e | 9733 | schedstat_inc(sd->lb_balanced[idle]); |
1e3c88bd PZ |
9734 | |
9735 | sd->nr_balance_failed = 0; | |
9736 | ||
9737 | out_one_pinned: | |
3f130a37 VS |
9738 | ld_moved = 0; |
9739 | ||
9740 | /* | |
5ba553ef PZ |
9741 | * newidle_balance() disregards balance intervals, so we could |
9742 | * repeatedly reach this code, which would lead to balance_interval | |
9743 | * skyrocketting in a short amount of time. Skip the balance_interval | |
9744 | * increase logic to avoid that. | |
3f130a37 VS |
9745 | */ |
9746 | if (env.idle == CPU_NEWLY_IDLE) | |
9747 | goto out; | |
9748 | ||
1e3c88bd | 9749 | /* tune up the balancing interval */ |
47b7aee1 VS |
9750 | if ((env.flags & LBF_ALL_PINNED && |
9751 | sd->balance_interval < MAX_PINNED_INTERVAL) || | |
9752 | sd->balance_interval < sd->max_interval) | |
1e3c88bd | 9753 | sd->balance_interval *= 2; |
1e3c88bd | 9754 | out: |
1e3c88bd PZ |
9755 | return ld_moved; |
9756 | } | |
9757 | ||
52a08ef1 JL |
9758 | static inline unsigned long |
9759 | get_sd_balance_interval(struct sched_domain *sd, int cpu_busy) | |
9760 | { | |
9761 | unsigned long interval = sd->balance_interval; | |
9762 | ||
9763 | if (cpu_busy) | |
9764 | interval *= sd->busy_factor; | |
9765 | ||
9766 | /* scale ms to jiffies */ | |
9767 | interval = msecs_to_jiffies(interval); | |
9768 | interval = clamp(interval, 1UL, max_load_balance_interval); | |
9769 | ||
9770 | return interval; | |
9771 | } | |
9772 | ||
9773 | static inline void | |
31851a98 | 9774 | update_next_balance(struct sched_domain *sd, unsigned long *next_balance) |
52a08ef1 JL |
9775 | { |
9776 | unsigned long interval, next; | |
9777 | ||
31851a98 LY |
9778 | /* used by idle balance, so cpu_busy = 0 */ |
9779 | interval = get_sd_balance_interval(sd, 0); | |
52a08ef1 JL |
9780 | next = sd->last_balance + interval; |
9781 | ||
9782 | if (time_after(*next_balance, next)) | |
9783 | *next_balance = next; | |
9784 | } | |
9785 | ||
1e3c88bd | 9786 | /* |
97fb7a0a | 9787 | * active_load_balance_cpu_stop is run by the CPU stopper. It pushes |
969c7921 TH |
9788 | * running tasks off the busiest CPU onto idle CPUs. It requires at |
9789 | * least 1 task to be running on each physical CPU where possible, and | |
9790 | * avoids physical / logical imbalances. | |
1e3c88bd | 9791 | */ |
969c7921 | 9792 | static int active_load_balance_cpu_stop(void *data) |
1e3c88bd | 9793 | { |
969c7921 TH |
9794 | struct rq *busiest_rq = data; |
9795 | int busiest_cpu = cpu_of(busiest_rq); | |
1e3c88bd | 9796 | int target_cpu = busiest_rq->push_cpu; |
969c7921 | 9797 | struct rq *target_rq = cpu_rq(target_cpu); |
1e3c88bd | 9798 | struct sched_domain *sd; |
e5673f28 | 9799 | struct task_struct *p = NULL; |
8a8c69c3 | 9800 | struct rq_flags rf; |
969c7921 | 9801 | |
8a8c69c3 | 9802 | rq_lock_irq(busiest_rq, &rf); |
edd8e41d PZ |
9803 | /* |
9804 | * Between queueing the stop-work and running it is a hole in which | |
9805 | * CPUs can become inactive. We should not move tasks from or to | |
9806 | * inactive CPUs. | |
9807 | */ | |
9808 | if (!cpu_active(busiest_cpu) || !cpu_active(target_cpu)) | |
9809 | goto out_unlock; | |
969c7921 | 9810 | |
97fb7a0a | 9811 | /* Make sure the requested CPU hasn't gone down in the meantime: */ |
969c7921 TH |
9812 | if (unlikely(busiest_cpu != smp_processor_id() || |
9813 | !busiest_rq->active_balance)) | |
9814 | goto out_unlock; | |
1e3c88bd PZ |
9815 | |
9816 | /* Is there any task to move? */ | |
9817 | if (busiest_rq->nr_running <= 1) | |
969c7921 | 9818 | goto out_unlock; |
1e3c88bd PZ |
9819 | |
9820 | /* | |
9821 | * This condition is "impossible", if it occurs | |
9822 | * we need to fix it. Originally reported by | |
97fb7a0a | 9823 | * Bjorn Helgaas on a 128-CPU setup. |
1e3c88bd PZ |
9824 | */ |
9825 | BUG_ON(busiest_rq == target_rq); | |
9826 | ||
1e3c88bd | 9827 | /* Search for an sd spanning us and the target CPU. */ |
dce840a0 | 9828 | rcu_read_lock(); |
1e3c88bd | 9829 | for_each_domain(target_cpu, sd) { |
e669ac8a VS |
9830 | if (cpumask_test_cpu(busiest_cpu, sched_domain_span(sd))) |
9831 | break; | |
1e3c88bd PZ |
9832 | } |
9833 | ||
9834 | if (likely(sd)) { | |
8e45cb54 PZ |
9835 | struct lb_env env = { |
9836 | .sd = sd, | |
ddcdf6e7 PZ |
9837 | .dst_cpu = target_cpu, |
9838 | .dst_rq = target_rq, | |
9839 | .src_cpu = busiest_rq->cpu, | |
9840 | .src_rq = busiest_rq, | |
8e45cb54 | 9841 | .idle = CPU_IDLE, |
65a4433a JH |
9842 | /* |
9843 | * can_migrate_task() doesn't need to compute new_dst_cpu | |
9844 | * for active balancing. Since we have CPU_IDLE, but no | |
9845 | * @dst_grpmask we need to make that test go away with lying | |
9846 | * about DST_PINNED. | |
9847 | */ | |
9848 | .flags = LBF_DST_PINNED, | |
8e45cb54 PZ |
9849 | }; |
9850 | ||
ae92882e | 9851 | schedstat_inc(sd->alb_count); |
3bed5e21 | 9852 | update_rq_clock(busiest_rq); |
1e3c88bd | 9853 | |
e5673f28 | 9854 | p = detach_one_task(&env); |
d02c0711 | 9855 | if (p) { |
ae92882e | 9856 | schedstat_inc(sd->alb_pushed); |
d02c0711 SD |
9857 | /* Active balancing done, reset the failure counter. */ |
9858 | sd->nr_balance_failed = 0; | |
9859 | } else { | |
ae92882e | 9860 | schedstat_inc(sd->alb_failed); |
d02c0711 | 9861 | } |
1e3c88bd | 9862 | } |
dce840a0 | 9863 | rcu_read_unlock(); |
969c7921 TH |
9864 | out_unlock: |
9865 | busiest_rq->active_balance = 0; | |
8a8c69c3 | 9866 | rq_unlock(busiest_rq, &rf); |
e5673f28 KT |
9867 | |
9868 | if (p) | |
9869 | attach_one_task(target_rq, p); | |
9870 | ||
9871 | local_irq_enable(); | |
9872 | ||
969c7921 | 9873 | return 0; |
1e3c88bd PZ |
9874 | } |
9875 | ||
af3fe03c PZ |
9876 | static DEFINE_SPINLOCK(balancing); |
9877 | ||
9878 | /* | |
9879 | * Scale the max load_balance interval with the number of CPUs in the system. | |
9880 | * This trades load-balance latency on larger machines for less cross talk. | |
9881 | */ | |
9882 | void update_max_interval(void) | |
9883 | { | |
9884 | max_load_balance_interval = HZ*num_online_cpus()/10; | |
9885 | } | |
9886 | ||
9887 | /* | |
9888 | * It checks each scheduling domain to see if it is due to be balanced, | |
9889 | * and initiates a balancing operation if so. | |
9890 | * | |
9891 | * Balancing parameters are set up in init_sched_domains. | |
9892 | */ | |
9893 | static void rebalance_domains(struct rq *rq, enum cpu_idle_type idle) | |
9894 | { | |
9895 | int continue_balancing = 1; | |
9896 | int cpu = rq->cpu; | |
323af6de | 9897 | int busy = idle != CPU_IDLE && !sched_idle_cpu(cpu); |
af3fe03c PZ |
9898 | unsigned long interval; |
9899 | struct sched_domain *sd; | |
9900 | /* Earliest time when we have to do rebalance again */ | |
9901 | unsigned long next_balance = jiffies + 60*HZ; | |
9902 | int update_next_balance = 0; | |
9903 | int need_serialize, need_decay = 0; | |
9904 | u64 max_cost = 0; | |
9905 | ||
9906 | rcu_read_lock(); | |
9907 | for_each_domain(cpu, sd) { | |
9908 | /* | |
9909 | * Decay the newidle max times here because this is a regular | |
9910 | * visit to all the domains. Decay ~1% per second. | |
9911 | */ | |
9912 | if (time_after(jiffies, sd->next_decay_max_lb_cost)) { | |
9913 | sd->max_newidle_lb_cost = | |
9914 | (sd->max_newidle_lb_cost * 253) / 256; | |
9915 | sd->next_decay_max_lb_cost = jiffies + HZ; | |
9916 | need_decay = 1; | |
9917 | } | |
9918 | max_cost += sd->max_newidle_lb_cost; | |
9919 | ||
af3fe03c PZ |
9920 | /* |
9921 | * Stop the load balance at this level. There is another | |
9922 | * CPU in our sched group which is doing load balancing more | |
9923 | * actively. | |
9924 | */ | |
9925 | if (!continue_balancing) { | |
9926 | if (need_decay) | |
9927 | continue; | |
9928 | break; | |
9929 | } | |
9930 | ||
323af6de | 9931 | interval = get_sd_balance_interval(sd, busy); |
af3fe03c PZ |
9932 | |
9933 | need_serialize = sd->flags & SD_SERIALIZE; | |
9934 | if (need_serialize) { | |
9935 | if (!spin_trylock(&balancing)) | |
9936 | goto out; | |
9937 | } | |
9938 | ||
9939 | if (time_after_eq(jiffies, sd->last_balance + interval)) { | |
9940 | if (load_balance(cpu, rq, sd, idle, &continue_balancing)) { | |
9941 | /* | |
9942 | * The LBF_DST_PINNED logic could have changed | |
9943 | * env->dst_cpu, so we can't know our idle | |
9944 | * state even if we migrated tasks. Update it. | |
9945 | */ | |
9946 | idle = idle_cpu(cpu) ? CPU_IDLE : CPU_NOT_IDLE; | |
323af6de | 9947 | busy = idle != CPU_IDLE && !sched_idle_cpu(cpu); |
af3fe03c PZ |
9948 | } |
9949 | sd->last_balance = jiffies; | |
323af6de | 9950 | interval = get_sd_balance_interval(sd, busy); |
af3fe03c PZ |
9951 | } |
9952 | if (need_serialize) | |
9953 | spin_unlock(&balancing); | |
9954 | out: | |
9955 | if (time_after(next_balance, sd->last_balance + interval)) { | |
9956 | next_balance = sd->last_balance + interval; | |
9957 | update_next_balance = 1; | |
9958 | } | |
9959 | } | |
9960 | if (need_decay) { | |
9961 | /* | |
9962 | * Ensure the rq-wide value also decays but keep it at a | |
9963 | * reasonable floor to avoid funnies with rq->avg_idle. | |
9964 | */ | |
9965 | rq->max_idle_balance_cost = | |
9966 | max((u64)sysctl_sched_migration_cost, max_cost); | |
9967 | } | |
9968 | rcu_read_unlock(); | |
9969 | ||
9970 | /* | |
9971 | * next_balance will be updated only when there is a need. | |
9972 | * When the cpu is attached to null domain for ex, it will not be | |
9973 | * updated. | |
9974 | */ | |
9975 | if (likely(update_next_balance)) { | |
9976 | rq->next_balance = next_balance; | |
9977 | ||
9978 | #ifdef CONFIG_NO_HZ_COMMON | |
9979 | /* | |
9980 | * If this CPU has been elected to perform the nohz idle | |
9981 | * balance. Other idle CPUs have already rebalanced with | |
9982 | * nohz_idle_balance() and nohz.next_balance has been | |
9983 | * updated accordingly. This CPU is now running the idle load | |
9984 | * balance for itself and we need to update the | |
9985 | * nohz.next_balance accordingly. | |
9986 | */ | |
9987 | if ((idle == CPU_IDLE) && time_after(nohz.next_balance, rq->next_balance)) | |
9988 | nohz.next_balance = rq->next_balance; | |
9989 | #endif | |
9990 | } | |
9991 | } | |
9992 | ||
d987fc7f MG |
9993 | static inline int on_null_domain(struct rq *rq) |
9994 | { | |
9995 | return unlikely(!rcu_dereference_sched(rq->sd)); | |
9996 | } | |
9997 | ||
3451d024 | 9998 | #ifdef CONFIG_NO_HZ_COMMON |
83cd4fe2 VP |
9999 | /* |
10000 | * idle load balancing details | |
83cd4fe2 VP |
10001 | * - When one of the busy CPUs notice that there may be an idle rebalancing |
10002 | * needed, they will kick the idle load balancer, which then does idle | |
10003 | * load balancing for all the idle CPUs. | |
9b019acb NP |
10004 | * - HK_FLAG_MISC CPUs are used for this task, because HK_FLAG_SCHED not set |
10005 | * anywhere yet. | |
83cd4fe2 | 10006 | */ |
1e3c88bd | 10007 | |
3dd0337d | 10008 | static inline int find_new_ilb(void) |
1e3c88bd | 10009 | { |
9b019acb | 10010 | int ilb; |
1e3c88bd | 10011 | |
9b019acb NP |
10012 | for_each_cpu_and(ilb, nohz.idle_cpus_mask, |
10013 | housekeeping_cpumask(HK_FLAG_MISC)) { | |
10014 | if (idle_cpu(ilb)) | |
10015 | return ilb; | |
10016 | } | |
786d6dc7 SS |
10017 | |
10018 | return nr_cpu_ids; | |
1e3c88bd | 10019 | } |
1e3c88bd | 10020 | |
83cd4fe2 | 10021 | /* |
9b019acb NP |
10022 | * Kick a CPU to do the nohz balancing, if it is time for it. We pick any |
10023 | * idle CPU in the HK_FLAG_MISC housekeeping set (if there is one). | |
83cd4fe2 | 10024 | */ |
a4064fb6 | 10025 | static void kick_ilb(unsigned int flags) |
83cd4fe2 VP |
10026 | { |
10027 | int ilb_cpu; | |
10028 | ||
3ea2f097 VG |
10029 | /* |
10030 | * Increase nohz.next_balance only when if full ilb is triggered but | |
10031 | * not if we only update stats. | |
10032 | */ | |
10033 | if (flags & NOHZ_BALANCE_KICK) | |
10034 | nohz.next_balance = jiffies+1; | |
83cd4fe2 | 10035 | |
3dd0337d | 10036 | ilb_cpu = find_new_ilb(); |
83cd4fe2 | 10037 | |
0b005cf5 SS |
10038 | if (ilb_cpu >= nr_cpu_ids) |
10039 | return; | |
83cd4fe2 | 10040 | |
19a1f5ec PZ |
10041 | /* |
10042 | * Access to rq::nohz_csd is serialized by NOHZ_KICK_MASK; he who sets | |
10043 | * the first flag owns it; cleared by nohz_csd_func(). | |
10044 | */ | |
a4064fb6 | 10045 | flags = atomic_fetch_or(flags, nohz_flags(ilb_cpu)); |
b7031a02 | 10046 | if (flags & NOHZ_KICK_MASK) |
1c792db7 | 10047 | return; |
4550487a | 10048 | |
1c792db7 | 10049 | /* |
90b5363a | 10050 | * This way we generate an IPI on the target CPU which |
1c792db7 SS |
10051 | * is idle. And the softirq performing nohz idle load balance |
10052 | * will be run before returning from the IPI. | |
10053 | */ | |
90b5363a | 10054 | smp_call_function_single_async(ilb_cpu, &cpu_rq(ilb_cpu)->nohz_csd); |
4550487a PZ |
10055 | } |
10056 | ||
10057 | /* | |
9f132742 VS |
10058 | * Current decision point for kicking the idle load balancer in the presence |
10059 | * of idle CPUs in the system. | |
4550487a PZ |
10060 | */ |
10061 | static void nohz_balancer_kick(struct rq *rq) | |
10062 | { | |
10063 | unsigned long now = jiffies; | |
10064 | struct sched_domain_shared *sds; | |
10065 | struct sched_domain *sd; | |
10066 | int nr_busy, i, cpu = rq->cpu; | |
a4064fb6 | 10067 | unsigned int flags = 0; |
4550487a PZ |
10068 | |
10069 | if (unlikely(rq->idle_balance)) | |
10070 | return; | |
10071 | ||
10072 | /* | |
10073 | * We may be recently in ticked or tickless idle mode. At the first | |
10074 | * busy tick after returning from idle, we will update the busy stats. | |
10075 | */ | |
00357f5e | 10076 | nohz_balance_exit_idle(rq); |
4550487a PZ |
10077 | |
10078 | /* | |
10079 | * None are in tickless mode and hence no need for NOHZ idle load | |
10080 | * balancing. | |
10081 | */ | |
10082 | if (likely(!atomic_read(&nohz.nr_cpus))) | |
10083 | return; | |
10084 | ||
f643ea22 VG |
10085 | if (READ_ONCE(nohz.has_blocked) && |
10086 | time_after(now, READ_ONCE(nohz.next_blocked))) | |
a4064fb6 PZ |
10087 | flags = NOHZ_STATS_KICK; |
10088 | ||
4550487a | 10089 | if (time_before(now, nohz.next_balance)) |
a4064fb6 | 10090 | goto out; |
4550487a | 10091 | |
a0fe2cf0 | 10092 | if (rq->nr_running >= 2) { |
a4064fb6 | 10093 | flags = NOHZ_KICK_MASK; |
4550487a PZ |
10094 | goto out; |
10095 | } | |
10096 | ||
10097 | rcu_read_lock(); | |
4550487a PZ |
10098 | |
10099 | sd = rcu_dereference(rq->sd); | |
10100 | if (sd) { | |
e25a7a94 VS |
10101 | /* |
10102 | * If there's a CFS task and the current CPU has reduced | |
10103 | * capacity; kick the ILB to see if there's a better CPU to run | |
10104 | * on. | |
10105 | */ | |
10106 | if (rq->cfs.h_nr_running >= 1 && check_cpu_capacity(rq, sd)) { | |
a4064fb6 | 10107 | flags = NOHZ_KICK_MASK; |
4550487a PZ |
10108 | goto unlock; |
10109 | } | |
10110 | } | |
10111 | ||
011b27bb | 10112 | sd = rcu_dereference(per_cpu(sd_asym_packing, cpu)); |
4550487a | 10113 | if (sd) { |
b9a7b883 VS |
10114 | /* |
10115 | * When ASYM_PACKING; see if there's a more preferred CPU | |
10116 | * currently idle; in which case, kick the ILB to move tasks | |
10117 | * around. | |
10118 | */ | |
7edab78d | 10119 | for_each_cpu_and(i, sched_domain_span(sd), nohz.idle_cpus_mask) { |
4550487a | 10120 | if (sched_asym_prefer(i, cpu)) { |
a4064fb6 | 10121 | flags = NOHZ_KICK_MASK; |
4550487a PZ |
10122 | goto unlock; |
10123 | } | |
10124 | } | |
10125 | } | |
b9a7b883 | 10126 | |
a0fe2cf0 VS |
10127 | sd = rcu_dereference(per_cpu(sd_asym_cpucapacity, cpu)); |
10128 | if (sd) { | |
10129 | /* | |
10130 | * When ASYM_CPUCAPACITY; see if there's a higher capacity CPU | |
10131 | * to run the misfit task on. | |
10132 | */ | |
10133 | if (check_misfit_status(rq, sd)) { | |
10134 | flags = NOHZ_KICK_MASK; | |
10135 | goto unlock; | |
10136 | } | |
b9a7b883 VS |
10137 | |
10138 | /* | |
10139 | * For asymmetric systems, we do not want to nicely balance | |
10140 | * cache use, instead we want to embrace asymmetry and only | |
10141 | * ensure tasks have enough CPU capacity. | |
10142 | * | |
10143 | * Skip the LLC logic because it's not relevant in that case. | |
10144 | */ | |
10145 | goto unlock; | |
a0fe2cf0 VS |
10146 | } |
10147 | ||
b9a7b883 VS |
10148 | sds = rcu_dereference(per_cpu(sd_llc_shared, cpu)); |
10149 | if (sds) { | |
e25a7a94 | 10150 | /* |
b9a7b883 VS |
10151 | * If there is an imbalance between LLC domains (IOW we could |
10152 | * increase the overall cache use), we need some less-loaded LLC | |
10153 | * domain to pull some load. Likewise, we may need to spread | |
10154 | * load within the current LLC domain (e.g. packed SMT cores but | |
10155 | * other CPUs are idle). We can't really know from here how busy | |
10156 | * the others are - so just get a nohz balance going if it looks | |
10157 | * like this LLC domain has tasks we could move. | |
e25a7a94 | 10158 | */ |
b9a7b883 VS |
10159 | nr_busy = atomic_read(&sds->nr_busy_cpus); |
10160 | if (nr_busy > 1) { | |
10161 | flags = NOHZ_KICK_MASK; | |
10162 | goto unlock; | |
4550487a PZ |
10163 | } |
10164 | } | |
10165 | unlock: | |
10166 | rcu_read_unlock(); | |
10167 | out: | |
a4064fb6 PZ |
10168 | if (flags) |
10169 | kick_ilb(flags); | |
83cd4fe2 VP |
10170 | } |
10171 | ||
00357f5e | 10172 | static void set_cpu_sd_state_busy(int cpu) |
71325960 | 10173 | { |
00357f5e | 10174 | struct sched_domain *sd; |
a22e47a4 | 10175 | |
00357f5e PZ |
10176 | rcu_read_lock(); |
10177 | sd = rcu_dereference(per_cpu(sd_llc, cpu)); | |
a22e47a4 | 10178 | |
00357f5e PZ |
10179 | if (!sd || !sd->nohz_idle) |
10180 | goto unlock; | |
10181 | sd->nohz_idle = 0; | |
10182 | ||
10183 | atomic_inc(&sd->shared->nr_busy_cpus); | |
10184 | unlock: | |
10185 | rcu_read_unlock(); | |
71325960 SS |
10186 | } |
10187 | ||
00357f5e PZ |
10188 | void nohz_balance_exit_idle(struct rq *rq) |
10189 | { | |
10190 | SCHED_WARN_ON(rq != this_rq()); | |
10191 | ||
10192 | if (likely(!rq->nohz_tick_stopped)) | |
10193 | return; | |
10194 | ||
10195 | rq->nohz_tick_stopped = 0; | |
10196 | cpumask_clear_cpu(rq->cpu, nohz.idle_cpus_mask); | |
10197 | atomic_dec(&nohz.nr_cpus); | |
10198 | ||
10199 | set_cpu_sd_state_busy(rq->cpu); | |
10200 | } | |
10201 | ||
10202 | static void set_cpu_sd_state_idle(int cpu) | |
69e1e811 SS |
10203 | { |
10204 | struct sched_domain *sd; | |
69e1e811 | 10205 | |
69e1e811 | 10206 | rcu_read_lock(); |
0e369d75 | 10207 | sd = rcu_dereference(per_cpu(sd_llc, cpu)); |
25f55d9d VG |
10208 | |
10209 | if (!sd || sd->nohz_idle) | |
10210 | goto unlock; | |
10211 | sd->nohz_idle = 1; | |
10212 | ||
0e369d75 | 10213 | atomic_dec(&sd->shared->nr_busy_cpus); |
25f55d9d | 10214 | unlock: |
69e1e811 SS |
10215 | rcu_read_unlock(); |
10216 | } | |
10217 | ||
1e3c88bd | 10218 | /* |
97fb7a0a | 10219 | * This routine will record that the CPU is going idle with tick stopped. |
0b005cf5 | 10220 | * This info will be used in performing idle load balancing in the future. |
1e3c88bd | 10221 | */ |
c1cc017c | 10222 | void nohz_balance_enter_idle(int cpu) |
1e3c88bd | 10223 | { |
00357f5e PZ |
10224 | struct rq *rq = cpu_rq(cpu); |
10225 | ||
10226 | SCHED_WARN_ON(cpu != smp_processor_id()); | |
10227 | ||
97fb7a0a | 10228 | /* If this CPU is going down, then nothing needs to be done: */ |
71325960 SS |
10229 | if (!cpu_active(cpu)) |
10230 | return; | |
10231 | ||
387bc8b5 | 10232 | /* Spare idle load balancing on CPUs that don't want to be disturbed: */ |
de201559 | 10233 | if (!housekeeping_cpu(cpu, HK_FLAG_SCHED)) |
387bc8b5 FW |
10234 | return; |
10235 | ||
f643ea22 VG |
10236 | /* |
10237 | * Can be set safely without rq->lock held | |
10238 | * If a clear happens, it will have evaluated last additions because | |
10239 | * rq->lock is held during the check and the clear | |
10240 | */ | |
10241 | rq->has_blocked_load = 1; | |
10242 | ||
10243 | /* | |
10244 | * The tick is still stopped but load could have been added in the | |
10245 | * meantime. We set the nohz.has_blocked flag to trig a check of the | |
10246 | * *_avg. The CPU is already part of nohz.idle_cpus_mask so the clear | |
10247 | * of nohz.has_blocked can only happen after checking the new load | |
10248 | */ | |
00357f5e | 10249 | if (rq->nohz_tick_stopped) |
f643ea22 | 10250 | goto out; |
1e3c88bd | 10251 | |
97fb7a0a | 10252 | /* If we're a completely isolated CPU, we don't play: */ |
00357f5e | 10253 | if (on_null_domain(rq)) |
d987fc7f MG |
10254 | return; |
10255 | ||
00357f5e PZ |
10256 | rq->nohz_tick_stopped = 1; |
10257 | ||
c1cc017c AS |
10258 | cpumask_set_cpu(cpu, nohz.idle_cpus_mask); |
10259 | atomic_inc(&nohz.nr_cpus); | |
00357f5e | 10260 | |
f643ea22 VG |
10261 | /* |
10262 | * Ensures that if nohz_idle_balance() fails to observe our | |
10263 | * @idle_cpus_mask store, it must observe the @has_blocked | |
10264 | * store. | |
10265 | */ | |
10266 | smp_mb__after_atomic(); | |
10267 | ||
00357f5e | 10268 | set_cpu_sd_state_idle(cpu); |
f643ea22 VG |
10269 | |
10270 | out: | |
10271 | /* | |
10272 | * Each time a cpu enter idle, we assume that it has blocked load and | |
10273 | * enable the periodic update of the load of idle cpus | |
10274 | */ | |
10275 | WRITE_ONCE(nohz.has_blocked, 1); | |
1e3c88bd | 10276 | } |
1e3c88bd | 10277 | |
1e3c88bd | 10278 | /* |
31e77c93 VG |
10279 | * Internal function that runs load balance for all idle cpus. The load balance |
10280 | * can be a simple update of blocked load or a complete load balance with | |
10281 | * tasks movement depending of flags. | |
10282 | * The function returns false if the loop has stopped before running | |
10283 | * through all idle CPUs. | |
1e3c88bd | 10284 | */ |
31e77c93 VG |
10285 | static bool _nohz_idle_balance(struct rq *this_rq, unsigned int flags, |
10286 | enum cpu_idle_type idle) | |
83cd4fe2 | 10287 | { |
c5afb6a8 | 10288 | /* Earliest time when we have to do rebalance again */ |
a4064fb6 PZ |
10289 | unsigned long now = jiffies; |
10290 | unsigned long next_balance = now + 60*HZ; | |
f643ea22 | 10291 | bool has_blocked_load = false; |
c5afb6a8 | 10292 | int update_next_balance = 0; |
b7031a02 | 10293 | int this_cpu = this_rq->cpu; |
b7031a02 | 10294 | int balance_cpu; |
31e77c93 | 10295 | int ret = false; |
b7031a02 | 10296 | struct rq *rq; |
83cd4fe2 | 10297 | |
b7031a02 | 10298 | SCHED_WARN_ON((flags & NOHZ_KICK_MASK) == NOHZ_BALANCE_KICK); |
83cd4fe2 | 10299 | |
f643ea22 VG |
10300 | /* |
10301 | * We assume there will be no idle load after this update and clear | |
10302 | * the has_blocked flag. If a cpu enters idle in the mean time, it will | |
10303 | * set the has_blocked flag and trig another update of idle load. | |
10304 | * Because a cpu that becomes idle, is added to idle_cpus_mask before | |
10305 | * setting the flag, we are sure to not clear the state and not | |
10306 | * check the load of an idle cpu. | |
10307 | */ | |
10308 | WRITE_ONCE(nohz.has_blocked, 0); | |
10309 | ||
10310 | /* | |
10311 | * Ensures that if we miss the CPU, we must see the has_blocked | |
10312 | * store from nohz_balance_enter_idle(). | |
10313 | */ | |
10314 | smp_mb(); | |
10315 | ||
83cd4fe2 | 10316 | for_each_cpu(balance_cpu, nohz.idle_cpus_mask) { |
8a6d42d1 | 10317 | if (balance_cpu == this_cpu || !idle_cpu(balance_cpu)) |
83cd4fe2 VP |
10318 | continue; |
10319 | ||
10320 | /* | |
97fb7a0a IM |
10321 | * If this CPU gets work to do, stop the load balancing |
10322 | * work being done for other CPUs. Next load | |
83cd4fe2 VP |
10323 | * balancing owner will pick it up. |
10324 | */ | |
f643ea22 VG |
10325 | if (need_resched()) { |
10326 | has_blocked_load = true; | |
10327 | goto abort; | |
10328 | } | |
83cd4fe2 | 10329 | |
5ed4f1d9 VG |
10330 | rq = cpu_rq(balance_cpu); |
10331 | ||
63928384 | 10332 | has_blocked_load |= update_nohz_stats(rq, true); |
f643ea22 | 10333 | |
ed61bbc6 TC |
10334 | /* |
10335 | * If time for next balance is due, | |
10336 | * do the balance. | |
10337 | */ | |
10338 | if (time_after_eq(jiffies, rq->next_balance)) { | |
8a8c69c3 PZ |
10339 | struct rq_flags rf; |
10340 | ||
31e77c93 | 10341 | rq_lock_irqsave(rq, &rf); |
ed61bbc6 | 10342 | update_rq_clock(rq); |
31e77c93 | 10343 | rq_unlock_irqrestore(rq, &rf); |
8a8c69c3 | 10344 | |
b7031a02 PZ |
10345 | if (flags & NOHZ_BALANCE_KICK) |
10346 | rebalance_domains(rq, CPU_IDLE); | |
ed61bbc6 | 10347 | } |
83cd4fe2 | 10348 | |
c5afb6a8 VG |
10349 | if (time_after(next_balance, rq->next_balance)) { |
10350 | next_balance = rq->next_balance; | |
10351 | update_next_balance = 1; | |
10352 | } | |
83cd4fe2 | 10353 | } |
c5afb6a8 | 10354 | |
3ea2f097 VG |
10355 | /* |
10356 | * next_balance will be updated only when there is a need. | |
10357 | * When the CPU is attached to null domain for ex, it will not be | |
10358 | * updated. | |
10359 | */ | |
10360 | if (likely(update_next_balance)) | |
10361 | nohz.next_balance = next_balance; | |
10362 | ||
31e77c93 VG |
10363 | /* Newly idle CPU doesn't need an update */ |
10364 | if (idle != CPU_NEWLY_IDLE) { | |
10365 | update_blocked_averages(this_cpu); | |
10366 | has_blocked_load |= this_rq->has_blocked_load; | |
10367 | } | |
10368 | ||
b7031a02 PZ |
10369 | if (flags & NOHZ_BALANCE_KICK) |
10370 | rebalance_domains(this_rq, CPU_IDLE); | |
10371 | ||
f643ea22 VG |
10372 | WRITE_ONCE(nohz.next_blocked, |
10373 | now + msecs_to_jiffies(LOAD_AVG_PERIOD)); | |
10374 | ||
31e77c93 VG |
10375 | /* The full idle balance loop has been done */ |
10376 | ret = true; | |
10377 | ||
f643ea22 VG |
10378 | abort: |
10379 | /* There is still blocked load, enable periodic update */ | |
10380 | if (has_blocked_load) | |
10381 | WRITE_ONCE(nohz.has_blocked, 1); | |
a4064fb6 | 10382 | |
31e77c93 VG |
10383 | return ret; |
10384 | } | |
10385 | ||
10386 | /* | |
10387 | * In CONFIG_NO_HZ_COMMON case, the idle balance kickee will do the | |
10388 | * rebalancing for all the cpus for whom scheduler ticks are stopped. | |
10389 | */ | |
10390 | static bool nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle) | |
10391 | { | |
19a1f5ec | 10392 | unsigned int flags = this_rq->nohz_idle_balance; |
31e77c93 | 10393 | |
19a1f5ec | 10394 | if (!flags) |
31e77c93 VG |
10395 | return false; |
10396 | ||
19a1f5ec | 10397 | this_rq->nohz_idle_balance = 0; |
31e77c93 | 10398 | |
19a1f5ec | 10399 | if (idle != CPU_IDLE) |
31e77c93 VG |
10400 | return false; |
10401 | ||
10402 | _nohz_idle_balance(this_rq, flags, idle); | |
10403 | ||
b7031a02 | 10404 | return true; |
83cd4fe2 | 10405 | } |
31e77c93 VG |
10406 | |
10407 | static void nohz_newidle_balance(struct rq *this_rq) | |
10408 | { | |
10409 | int this_cpu = this_rq->cpu; | |
10410 | ||
10411 | /* | |
10412 | * This CPU doesn't want to be disturbed by scheduler | |
10413 | * housekeeping | |
10414 | */ | |
10415 | if (!housekeeping_cpu(this_cpu, HK_FLAG_SCHED)) | |
10416 | return; | |
10417 | ||
10418 | /* Will wake up very soon. No time for doing anything else*/ | |
10419 | if (this_rq->avg_idle < sysctl_sched_migration_cost) | |
10420 | return; | |
10421 | ||
10422 | /* Don't need to update blocked load of idle CPUs*/ | |
10423 | if (!READ_ONCE(nohz.has_blocked) || | |
10424 | time_before(jiffies, READ_ONCE(nohz.next_blocked))) | |
10425 | return; | |
10426 | ||
10427 | raw_spin_unlock(&this_rq->lock); | |
10428 | /* | |
10429 | * This CPU is going to be idle and blocked load of idle CPUs | |
10430 | * need to be updated. Run the ilb locally as it is a good | |
10431 | * candidate for ilb instead of waking up another idle CPU. | |
10432 | * Kick an normal ilb if we failed to do the update. | |
10433 | */ | |
10434 | if (!_nohz_idle_balance(this_rq, NOHZ_STATS_KICK, CPU_NEWLY_IDLE)) | |
10435 | kick_ilb(NOHZ_STATS_KICK); | |
10436 | raw_spin_lock(&this_rq->lock); | |
10437 | } | |
10438 | ||
dd707247 PZ |
10439 | #else /* !CONFIG_NO_HZ_COMMON */ |
10440 | static inline void nohz_balancer_kick(struct rq *rq) { } | |
10441 | ||
31e77c93 | 10442 | static inline bool nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle) |
b7031a02 PZ |
10443 | { |
10444 | return false; | |
10445 | } | |
31e77c93 VG |
10446 | |
10447 | static inline void nohz_newidle_balance(struct rq *this_rq) { } | |
dd707247 | 10448 | #endif /* CONFIG_NO_HZ_COMMON */ |
83cd4fe2 | 10449 | |
47ea5412 PZ |
10450 | /* |
10451 | * idle_balance is called by schedule() if this_cpu is about to become | |
10452 | * idle. Attempts to pull tasks from other CPUs. | |
7277a34c PZ |
10453 | * |
10454 | * Returns: | |
10455 | * < 0 - we released the lock and there are !fair tasks present | |
10456 | * 0 - failed, no new tasks | |
10457 | * > 0 - success, new (fair) tasks present | |
47ea5412 | 10458 | */ |
d91cecc1 | 10459 | static int newidle_balance(struct rq *this_rq, struct rq_flags *rf) |
47ea5412 PZ |
10460 | { |
10461 | unsigned long next_balance = jiffies + HZ; | |
10462 | int this_cpu = this_rq->cpu; | |
10463 | struct sched_domain *sd; | |
10464 | int pulled_task = 0; | |
10465 | u64 curr_cost = 0; | |
10466 | ||
5ba553ef | 10467 | update_misfit_status(NULL, this_rq); |
47ea5412 PZ |
10468 | /* |
10469 | * We must set idle_stamp _before_ calling idle_balance(), such that we | |
10470 | * measure the duration of idle_balance() as idle time. | |
10471 | */ | |
10472 | this_rq->idle_stamp = rq_clock(this_rq); | |
10473 | ||
10474 | /* | |
10475 | * Do not pull tasks towards !active CPUs... | |
10476 | */ | |
10477 | if (!cpu_active(this_cpu)) | |
10478 | return 0; | |
10479 | ||
10480 | /* | |
10481 | * This is OK, because current is on_cpu, which avoids it being picked | |
10482 | * for load-balance and preemption/IRQs are still disabled avoiding | |
10483 | * further scheduler activity on it and we're being very careful to | |
10484 | * re-start the picking loop. | |
10485 | */ | |
10486 | rq_unpin_lock(this_rq, rf); | |
10487 | ||
10488 | if (this_rq->avg_idle < sysctl_sched_migration_cost || | |
e90c8fe1 | 10489 | !READ_ONCE(this_rq->rd->overload)) { |
31e77c93 | 10490 | |
47ea5412 PZ |
10491 | rcu_read_lock(); |
10492 | sd = rcu_dereference_check_sched_domain(this_rq->sd); | |
10493 | if (sd) | |
10494 | update_next_balance(sd, &next_balance); | |
10495 | rcu_read_unlock(); | |
10496 | ||
31e77c93 VG |
10497 | nohz_newidle_balance(this_rq); |
10498 | ||
47ea5412 PZ |
10499 | goto out; |
10500 | } | |
10501 | ||
10502 | raw_spin_unlock(&this_rq->lock); | |
10503 | ||
10504 | update_blocked_averages(this_cpu); | |
10505 | rcu_read_lock(); | |
10506 | for_each_domain(this_cpu, sd) { | |
10507 | int continue_balancing = 1; | |
10508 | u64 t0, domain_cost; | |
10509 | ||
47ea5412 PZ |
10510 | if (this_rq->avg_idle < curr_cost + sd->max_newidle_lb_cost) { |
10511 | update_next_balance(sd, &next_balance); | |
10512 | break; | |
10513 | } | |
10514 | ||
10515 | if (sd->flags & SD_BALANCE_NEWIDLE) { | |
10516 | t0 = sched_clock_cpu(this_cpu); | |
10517 | ||
10518 | pulled_task = load_balance(this_cpu, this_rq, | |
10519 | sd, CPU_NEWLY_IDLE, | |
10520 | &continue_balancing); | |
10521 | ||
10522 | domain_cost = sched_clock_cpu(this_cpu) - t0; | |
10523 | if (domain_cost > sd->max_newidle_lb_cost) | |
10524 | sd->max_newidle_lb_cost = domain_cost; | |
10525 | ||
10526 | curr_cost += domain_cost; | |
10527 | } | |
10528 | ||
10529 | update_next_balance(sd, &next_balance); | |
10530 | ||
10531 | /* | |
10532 | * Stop searching for tasks to pull if there are | |
10533 | * now runnable tasks on this rq. | |
10534 | */ | |
10535 | if (pulled_task || this_rq->nr_running > 0) | |
10536 | break; | |
10537 | } | |
10538 | rcu_read_unlock(); | |
10539 | ||
10540 | raw_spin_lock(&this_rq->lock); | |
10541 | ||
10542 | if (curr_cost > this_rq->max_idle_balance_cost) | |
10543 | this_rq->max_idle_balance_cost = curr_cost; | |
10544 | ||
457be908 | 10545 | out: |
47ea5412 PZ |
10546 | /* |
10547 | * While browsing the domains, we released the rq lock, a task could | |
10548 | * have been enqueued in the meantime. Since we're not going idle, | |
10549 | * pretend we pulled a task. | |
10550 | */ | |
10551 | if (this_rq->cfs.h_nr_running && !pulled_task) | |
10552 | pulled_task = 1; | |
10553 | ||
47ea5412 PZ |
10554 | /* Move the next balance forward */ |
10555 | if (time_after(this_rq->next_balance, next_balance)) | |
10556 | this_rq->next_balance = next_balance; | |
10557 | ||
10558 | /* Is there a task of a high priority class? */ | |
10559 | if (this_rq->nr_running != this_rq->cfs.h_nr_running) | |
10560 | pulled_task = -1; | |
10561 | ||
10562 | if (pulled_task) | |
10563 | this_rq->idle_stamp = 0; | |
10564 | ||
10565 | rq_repin_lock(this_rq, rf); | |
10566 | ||
10567 | return pulled_task; | |
10568 | } | |
10569 | ||
83cd4fe2 VP |
10570 | /* |
10571 | * run_rebalance_domains is triggered when needed from the scheduler tick. | |
10572 | * Also triggered for nohz idle balancing (with nohz_balancing_kick set). | |
10573 | */ | |
0766f788 | 10574 | static __latent_entropy void run_rebalance_domains(struct softirq_action *h) |
1e3c88bd | 10575 | { |
208cb16b | 10576 | struct rq *this_rq = this_rq(); |
6eb57e0d | 10577 | enum cpu_idle_type idle = this_rq->idle_balance ? |
1e3c88bd PZ |
10578 | CPU_IDLE : CPU_NOT_IDLE; |
10579 | ||
1e3c88bd | 10580 | /* |
97fb7a0a IM |
10581 | * If this CPU has a pending nohz_balance_kick, then do the |
10582 | * balancing on behalf of the other idle CPUs whose ticks are | |
d4573c3e | 10583 | * stopped. Do nohz_idle_balance *before* rebalance_domains to |
97fb7a0a | 10584 | * give the idle CPUs a chance to load balance. Else we may |
d4573c3e PM |
10585 | * load balance only within the local sched_domain hierarchy |
10586 | * and abort nohz_idle_balance altogether if we pull some load. | |
1e3c88bd | 10587 | */ |
b7031a02 PZ |
10588 | if (nohz_idle_balance(this_rq, idle)) |
10589 | return; | |
10590 | ||
10591 | /* normal load balance */ | |
10592 | update_blocked_averages(this_rq->cpu); | |
d4573c3e | 10593 | rebalance_domains(this_rq, idle); |
1e3c88bd PZ |
10594 | } |
10595 | ||
1e3c88bd PZ |
10596 | /* |
10597 | * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing. | |
1e3c88bd | 10598 | */ |
7caff66f | 10599 | void trigger_load_balance(struct rq *rq) |
1e3c88bd | 10600 | { |
1e3c88bd | 10601 | /* Don't need to rebalance while attached to NULL domain */ |
c726099e DL |
10602 | if (unlikely(on_null_domain(rq))) |
10603 | return; | |
10604 | ||
10605 | if (time_after_eq(jiffies, rq->next_balance)) | |
1e3c88bd | 10606 | raise_softirq(SCHED_SOFTIRQ); |
4550487a PZ |
10607 | |
10608 | nohz_balancer_kick(rq); | |
1e3c88bd PZ |
10609 | } |
10610 | ||
0bcdcf28 CE |
10611 | static void rq_online_fair(struct rq *rq) |
10612 | { | |
10613 | update_sysctl(); | |
0e59bdae KT |
10614 | |
10615 | update_runtime_enabled(rq); | |
0bcdcf28 CE |
10616 | } |
10617 | ||
10618 | static void rq_offline_fair(struct rq *rq) | |
10619 | { | |
10620 | update_sysctl(); | |
a4c96ae3 PB |
10621 | |
10622 | /* Ensure any throttled groups are reachable by pick_next_task */ | |
10623 | unthrottle_offline_cfs_rqs(rq); | |
0bcdcf28 CE |
10624 | } |
10625 | ||
55e12e5e | 10626 | #endif /* CONFIG_SMP */ |
e1d1484f | 10627 | |
bf0f6f24 | 10628 | /* |
d84b3131 FW |
10629 | * scheduler tick hitting a task of our scheduling class. |
10630 | * | |
10631 | * NOTE: This function can be called remotely by the tick offload that | |
10632 | * goes along full dynticks. Therefore no local assumption can be made | |
10633 | * and everything must be accessed through the @rq and @curr passed in | |
10634 | * parameters. | |
bf0f6f24 | 10635 | */ |
8f4d37ec | 10636 | static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued) |
bf0f6f24 IM |
10637 | { |
10638 | struct cfs_rq *cfs_rq; | |
10639 | struct sched_entity *se = &curr->se; | |
10640 | ||
10641 | for_each_sched_entity(se) { | |
10642 | cfs_rq = cfs_rq_of(se); | |
8f4d37ec | 10643 | entity_tick(cfs_rq, se, queued); |
bf0f6f24 | 10644 | } |
18bf2805 | 10645 | |
b52da86e | 10646 | if (static_branch_unlikely(&sched_numa_balancing)) |
cbee9f88 | 10647 | task_tick_numa(rq, curr); |
3b1baa64 MR |
10648 | |
10649 | update_misfit_status(curr, rq); | |
2802bf3c | 10650 | update_overutilized_status(task_rq(curr)); |
bf0f6f24 IM |
10651 | } |
10652 | ||
10653 | /* | |
cd29fe6f PZ |
10654 | * called on fork with the child task as argument from the parent's context |
10655 | * - child not yet on the tasklist | |
10656 | * - preemption disabled | |
bf0f6f24 | 10657 | */ |
cd29fe6f | 10658 | static void task_fork_fair(struct task_struct *p) |
bf0f6f24 | 10659 | { |
4fc420c9 DN |
10660 | struct cfs_rq *cfs_rq; |
10661 | struct sched_entity *se = &p->se, *curr; | |
cd29fe6f | 10662 | struct rq *rq = this_rq(); |
8a8c69c3 | 10663 | struct rq_flags rf; |
bf0f6f24 | 10664 | |
8a8c69c3 | 10665 | rq_lock(rq, &rf); |
861d034e PZ |
10666 | update_rq_clock(rq); |
10667 | ||
4fc420c9 DN |
10668 | cfs_rq = task_cfs_rq(current); |
10669 | curr = cfs_rq->curr; | |
e210bffd PZ |
10670 | if (curr) { |
10671 | update_curr(cfs_rq); | |
b5d9d734 | 10672 | se->vruntime = curr->vruntime; |
e210bffd | 10673 | } |
aeb73b04 | 10674 | place_entity(cfs_rq, se, 1); |
4d78e7b6 | 10675 | |
cd29fe6f | 10676 | if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) { |
87fefa38 | 10677 | /* |
edcb60a3 IM |
10678 | * Upon rescheduling, sched_class::put_prev_task() will place |
10679 | * 'current' within the tree based on its new key value. | |
10680 | */ | |
4d78e7b6 | 10681 | swap(curr->vruntime, se->vruntime); |
8875125e | 10682 | resched_curr(rq); |
4d78e7b6 | 10683 | } |
bf0f6f24 | 10684 | |
88ec22d3 | 10685 | se->vruntime -= cfs_rq->min_vruntime; |
8a8c69c3 | 10686 | rq_unlock(rq, &rf); |
bf0f6f24 IM |
10687 | } |
10688 | ||
cb469845 SR |
10689 | /* |
10690 | * Priority of the task has changed. Check to see if we preempt | |
10691 | * the current task. | |
10692 | */ | |
da7a735e PZ |
10693 | static void |
10694 | prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio) | |
cb469845 | 10695 | { |
da0c1e65 | 10696 | if (!task_on_rq_queued(p)) |
da7a735e PZ |
10697 | return; |
10698 | ||
7c2e8bbd FW |
10699 | if (rq->cfs.nr_running == 1) |
10700 | return; | |
10701 | ||
cb469845 SR |
10702 | /* |
10703 | * Reschedule if we are currently running on this runqueue and | |
10704 | * our priority decreased, or if we are not currently running on | |
10705 | * this runqueue and our priority is higher than the current's | |
10706 | */ | |
da7a735e | 10707 | if (rq->curr == p) { |
cb469845 | 10708 | if (p->prio > oldprio) |
8875125e | 10709 | resched_curr(rq); |
cb469845 | 10710 | } else |
15afe09b | 10711 | check_preempt_curr(rq, p, 0); |
cb469845 SR |
10712 | } |
10713 | ||
daa59407 | 10714 | static inline bool vruntime_normalized(struct task_struct *p) |
da7a735e PZ |
10715 | { |
10716 | struct sched_entity *se = &p->se; | |
da7a735e PZ |
10717 | |
10718 | /* | |
daa59407 BP |
10719 | * In both the TASK_ON_RQ_QUEUED and TASK_ON_RQ_MIGRATING cases, |
10720 | * the dequeue_entity(.flags=0) will already have normalized the | |
10721 | * vruntime. | |
10722 | */ | |
10723 | if (p->on_rq) | |
10724 | return true; | |
10725 | ||
10726 | /* | |
10727 | * When !on_rq, vruntime of the task has usually NOT been normalized. | |
10728 | * But there are some cases where it has already been normalized: | |
da7a735e | 10729 | * |
daa59407 BP |
10730 | * - A forked child which is waiting for being woken up by |
10731 | * wake_up_new_task(). | |
10732 | * - A task which has been woken up by try_to_wake_up() and | |
10733 | * waiting for actually being woken up by sched_ttwu_pending(). | |
da7a735e | 10734 | */ |
d0cdb3ce SM |
10735 | if (!se->sum_exec_runtime || |
10736 | (p->state == TASK_WAKING && p->sched_remote_wakeup)) | |
daa59407 BP |
10737 | return true; |
10738 | ||
10739 | return false; | |
10740 | } | |
10741 | ||
09a43ace VG |
10742 | #ifdef CONFIG_FAIR_GROUP_SCHED |
10743 | /* | |
10744 | * Propagate the changes of the sched_entity across the tg tree to make it | |
10745 | * visible to the root | |
10746 | */ | |
10747 | static void propagate_entity_cfs_rq(struct sched_entity *se) | |
10748 | { | |
10749 | struct cfs_rq *cfs_rq; | |
10750 | ||
10751 | /* Start to propagate at parent */ | |
10752 | se = se->parent; | |
10753 | ||
10754 | for_each_sched_entity(se) { | |
10755 | cfs_rq = cfs_rq_of(se); | |
10756 | ||
10757 | if (cfs_rq_throttled(cfs_rq)) | |
10758 | break; | |
10759 | ||
88c0616e | 10760 | update_load_avg(cfs_rq, se, UPDATE_TG); |
09a43ace VG |
10761 | } |
10762 | } | |
10763 | #else | |
10764 | static void propagate_entity_cfs_rq(struct sched_entity *se) { } | |
10765 | #endif | |
10766 | ||
df217913 | 10767 | static void detach_entity_cfs_rq(struct sched_entity *se) |
daa59407 | 10768 | { |
daa59407 BP |
10769 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
10770 | ||
9d89c257 | 10771 | /* Catch up with the cfs_rq and remove our load when we leave */ |
88c0616e | 10772 | update_load_avg(cfs_rq, se, 0); |
a05e8c51 | 10773 | detach_entity_load_avg(cfs_rq, se); |
7c3edd2c | 10774 | update_tg_load_avg(cfs_rq, false); |
09a43ace | 10775 | propagate_entity_cfs_rq(se); |
da7a735e PZ |
10776 | } |
10777 | ||
df217913 | 10778 | static void attach_entity_cfs_rq(struct sched_entity *se) |
cb469845 | 10779 | { |
daa59407 | 10780 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
7855a35a BP |
10781 | |
10782 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
eb7a59b2 M |
10783 | /* |
10784 | * Since the real-depth could have been changed (only FAIR | |
10785 | * class maintain depth value), reset depth properly. | |
10786 | */ | |
10787 | se->depth = se->parent ? se->parent->depth + 1 : 0; | |
10788 | #endif | |
7855a35a | 10789 | |
df217913 | 10790 | /* Synchronize entity with its cfs_rq */ |
88c0616e | 10791 | update_load_avg(cfs_rq, se, sched_feat(ATTACH_AGE_LOAD) ? 0 : SKIP_AGE_LOAD); |
a4f9a0e5 | 10792 | attach_entity_load_avg(cfs_rq, se); |
7c3edd2c | 10793 | update_tg_load_avg(cfs_rq, false); |
09a43ace | 10794 | propagate_entity_cfs_rq(se); |
df217913 VG |
10795 | } |
10796 | ||
10797 | static void detach_task_cfs_rq(struct task_struct *p) | |
10798 | { | |
10799 | struct sched_entity *se = &p->se; | |
10800 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
10801 | ||
10802 | if (!vruntime_normalized(p)) { | |
10803 | /* | |
10804 | * Fix up our vruntime so that the current sleep doesn't | |
10805 | * cause 'unlimited' sleep bonus. | |
10806 | */ | |
10807 | place_entity(cfs_rq, se, 0); | |
10808 | se->vruntime -= cfs_rq->min_vruntime; | |
10809 | } | |
10810 | ||
10811 | detach_entity_cfs_rq(se); | |
10812 | } | |
10813 | ||
10814 | static void attach_task_cfs_rq(struct task_struct *p) | |
10815 | { | |
10816 | struct sched_entity *se = &p->se; | |
10817 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
10818 | ||
10819 | attach_entity_cfs_rq(se); | |
daa59407 BP |
10820 | |
10821 | if (!vruntime_normalized(p)) | |
10822 | se->vruntime += cfs_rq->min_vruntime; | |
10823 | } | |
6efdb105 | 10824 | |
daa59407 BP |
10825 | static void switched_from_fair(struct rq *rq, struct task_struct *p) |
10826 | { | |
10827 | detach_task_cfs_rq(p); | |
10828 | } | |
10829 | ||
10830 | static void switched_to_fair(struct rq *rq, struct task_struct *p) | |
10831 | { | |
10832 | attach_task_cfs_rq(p); | |
7855a35a | 10833 | |
daa59407 | 10834 | if (task_on_rq_queued(p)) { |
7855a35a | 10835 | /* |
daa59407 BP |
10836 | * We were most likely switched from sched_rt, so |
10837 | * kick off the schedule if running, otherwise just see | |
10838 | * if we can still preempt the current task. | |
7855a35a | 10839 | */ |
daa59407 BP |
10840 | if (rq->curr == p) |
10841 | resched_curr(rq); | |
10842 | else | |
10843 | check_preempt_curr(rq, p, 0); | |
7855a35a | 10844 | } |
cb469845 SR |
10845 | } |
10846 | ||
83b699ed SV |
10847 | /* Account for a task changing its policy or group. |
10848 | * | |
10849 | * This routine is mostly called to set cfs_rq->curr field when a task | |
10850 | * migrates between groups/classes. | |
10851 | */ | |
a0e813f2 | 10852 | static void set_next_task_fair(struct rq *rq, struct task_struct *p, bool first) |
83b699ed | 10853 | { |
03b7fad1 PZ |
10854 | struct sched_entity *se = &p->se; |
10855 | ||
10856 | #ifdef CONFIG_SMP | |
10857 | if (task_on_rq_queued(p)) { | |
10858 | /* | |
10859 | * Move the next running task to the front of the list, so our | |
10860 | * cfs_tasks list becomes MRU one. | |
10861 | */ | |
10862 | list_move(&se->group_node, &rq->cfs_tasks); | |
10863 | } | |
10864 | #endif | |
83b699ed | 10865 | |
ec12cb7f PT |
10866 | for_each_sched_entity(se) { |
10867 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
10868 | ||
10869 | set_next_entity(cfs_rq, se); | |
10870 | /* ensure bandwidth has been allocated on our new cfs_rq */ | |
10871 | account_cfs_rq_runtime(cfs_rq, 0); | |
10872 | } | |
83b699ed SV |
10873 | } |
10874 | ||
029632fb PZ |
10875 | void init_cfs_rq(struct cfs_rq *cfs_rq) |
10876 | { | |
bfb06889 | 10877 | cfs_rq->tasks_timeline = RB_ROOT_CACHED; |
029632fb PZ |
10878 | cfs_rq->min_vruntime = (u64)(-(1LL << 20)); |
10879 | #ifndef CONFIG_64BIT | |
10880 | cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime; | |
10881 | #endif | |
141965c7 | 10882 | #ifdef CONFIG_SMP |
2a2f5d4e | 10883 | raw_spin_lock_init(&cfs_rq->removed.lock); |
9ee474f5 | 10884 | #endif |
029632fb PZ |
10885 | } |
10886 | ||
810b3817 | 10887 | #ifdef CONFIG_FAIR_GROUP_SCHED |
ea86cb4b VG |
10888 | static void task_set_group_fair(struct task_struct *p) |
10889 | { | |
10890 | struct sched_entity *se = &p->se; | |
10891 | ||
10892 | set_task_rq(p, task_cpu(p)); | |
10893 | se->depth = se->parent ? se->parent->depth + 1 : 0; | |
10894 | } | |
10895 | ||
bc54da21 | 10896 | static void task_move_group_fair(struct task_struct *p) |
810b3817 | 10897 | { |
daa59407 | 10898 | detach_task_cfs_rq(p); |
b2b5ce02 | 10899 | set_task_rq(p, task_cpu(p)); |
6efdb105 BP |
10900 | |
10901 | #ifdef CONFIG_SMP | |
10902 | /* Tell se's cfs_rq has been changed -- migrated */ | |
10903 | p->se.avg.last_update_time = 0; | |
10904 | #endif | |
daa59407 | 10905 | attach_task_cfs_rq(p); |
810b3817 | 10906 | } |
029632fb | 10907 | |
ea86cb4b VG |
10908 | static void task_change_group_fair(struct task_struct *p, int type) |
10909 | { | |
10910 | switch (type) { | |
10911 | case TASK_SET_GROUP: | |
10912 | task_set_group_fair(p); | |
10913 | break; | |
10914 | ||
10915 | case TASK_MOVE_GROUP: | |
10916 | task_move_group_fair(p); | |
10917 | break; | |
10918 | } | |
10919 | } | |
10920 | ||
029632fb PZ |
10921 | void free_fair_sched_group(struct task_group *tg) |
10922 | { | |
10923 | int i; | |
10924 | ||
10925 | destroy_cfs_bandwidth(tg_cfs_bandwidth(tg)); | |
10926 | ||
10927 | for_each_possible_cpu(i) { | |
10928 | if (tg->cfs_rq) | |
10929 | kfree(tg->cfs_rq[i]); | |
6fe1f348 | 10930 | if (tg->se) |
029632fb PZ |
10931 | kfree(tg->se[i]); |
10932 | } | |
10933 | ||
10934 | kfree(tg->cfs_rq); | |
10935 | kfree(tg->se); | |
10936 | } | |
10937 | ||
10938 | int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent) | |
10939 | { | |
029632fb | 10940 | struct sched_entity *se; |
b7fa30c9 | 10941 | struct cfs_rq *cfs_rq; |
029632fb PZ |
10942 | int i; |
10943 | ||
6396bb22 | 10944 | tg->cfs_rq = kcalloc(nr_cpu_ids, sizeof(cfs_rq), GFP_KERNEL); |
029632fb PZ |
10945 | if (!tg->cfs_rq) |
10946 | goto err; | |
6396bb22 | 10947 | tg->se = kcalloc(nr_cpu_ids, sizeof(se), GFP_KERNEL); |
029632fb PZ |
10948 | if (!tg->se) |
10949 | goto err; | |
10950 | ||
10951 | tg->shares = NICE_0_LOAD; | |
10952 | ||
10953 | init_cfs_bandwidth(tg_cfs_bandwidth(tg)); | |
10954 | ||
10955 | for_each_possible_cpu(i) { | |
10956 | cfs_rq = kzalloc_node(sizeof(struct cfs_rq), | |
10957 | GFP_KERNEL, cpu_to_node(i)); | |
10958 | if (!cfs_rq) | |
10959 | goto err; | |
10960 | ||
10961 | se = kzalloc_node(sizeof(struct sched_entity), | |
10962 | GFP_KERNEL, cpu_to_node(i)); | |
10963 | if (!se) | |
10964 | goto err_free_rq; | |
10965 | ||
10966 | init_cfs_rq(cfs_rq); | |
10967 | init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]); | |
540247fb | 10968 | init_entity_runnable_average(se); |
029632fb PZ |
10969 | } |
10970 | ||
10971 | return 1; | |
10972 | ||
10973 | err_free_rq: | |
10974 | kfree(cfs_rq); | |
10975 | err: | |
10976 | return 0; | |
10977 | } | |
10978 | ||
8663e24d PZ |
10979 | void online_fair_sched_group(struct task_group *tg) |
10980 | { | |
10981 | struct sched_entity *se; | |
a46d14ec | 10982 | struct rq_flags rf; |
8663e24d PZ |
10983 | struct rq *rq; |
10984 | int i; | |
10985 | ||
10986 | for_each_possible_cpu(i) { | |
10987 | rq = cpu_rq(i); | |
10988 | se = tg->se[i]; | |
a46d14ec | 10989 | rq_lock_irq(rq, &rf); |
4126bad6 | 10990 | update_rq_clock(rq); |
d0326691 | 10991 | attach_entity_cfs_rq(se); |
55e16d30 | 10992 | sync_throttle(tg, i); |
a46d14ec | 10993 | rq_unlock_irq(rq, &rf); |
8663e24d PZ |
10994 | } |
10995 | } | |
10996 | ||
6fe1f348 | 10997 | void unregister_fair_sched_group(struct task_group *tg) |
029632fb | 10998 | { |
029632fb | 10999 | unsigned long flags; |
6fe1f348 PZ |
11000 | struct rq *rq; |
11001 | int cpu; | |
029632fb | 11002 | |
6fe1f348 PZ |
11003 | for_each_possible_cpu(cpu) { |
11004 | if (tg->se[cpu]) | |
11005 | remove_entity_load_avg(tg->se[cpu]); | |
029632fb | 11006 | |
6fe1f348 PZ |
11007 | /* |
11008 | * Only empty task groups can be destroyed; so we can speculatively | |
11009 | * check on_list without danger of it being re-added. | |
11010 | */ | |
11011 | if (!tg->cfs_rq[cpu]->on_list) | |
11012 | continue; | |
11013 | ||
11014 | rq = cpu_rq(cpu); | |
11015 | ||
11016 | raw_spin_lock_irqsave(&rq->lock, flags); | |
11017 | list_del_leaf_cfs_rq(tg->cfs_rq[cpu]); | |
11018 | raw_spin_unlock_irqrestore(&rq->lock, flags); | |
11019 | } | |
029632fb PZ |
11020 | } |
11021 | ||
11022 | void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq, | |
11023 | struct sched_entity *se, int cpu, | |
11024 | struct sched_entity *parent) | |
11025 | { | |
11026 | struct rq *rq = cpu_rq(cpu); | |
11027 | ||
11028 | cfs_rq->tg = tg; | |
11029 | cfs_rq->rq = rq; | |
029632fb PZ |
11030 | init_cfs_rq_runtime(cfs_rq); |
11031 | ||
11032 | tg->cfs_rq[cpu] = cfs_rq; | |
11033 | tg->se[cpu] = se; | |
11034 | ||
11035 | /* se could be NULL for root_task_group */ | |
11036 | if (!se) | |
11037 | return; | |
11038 | ||
fed14d45 | 11039 | if (!parent) { |
029632fb | 11040 | se->cfs_rq = &rq->cfs; |
fed14d45 PZ |
11041 | se->depth = 0; |
11042 | } else { | |
029632fb | 11043 | se->cfs_rq = parent->my_q; |
fed14d45 PZ |
11044 | se->depth = parent->depth + 1; |
11045 | } | |
029632fb PZ |
11046 | |
11047 | se->my_q = cfs_rq; | |
0ac9b1c2 PT |
11048 | /* guarantee group entities always have weight */ |
11049 | update_load_set(&se->load, NICE_0_LOAD); | |
029632fb PZ |
11050 | se->parent = parent; |
11051 | } | |
11052 | ||
11053 | static DEFINE_MUTEX(shares_mutex); | |
11054 | ||
11055 | int sched_group_set_shares(struct task_group *tg, unsigned long shares) | |
11056 | { | |
11057 | int i; | |
029632fb PZ |
11058 | |
11059 | /* | |
11060 | * We can't change the weight of the root cgroup. | |
11061 | */ | |
11062 | if (!tg->se[0]) | |
11063 | return -EINVAL; | |
11064 | ||
11065 | shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES)); | |
11066 | ||
11067 | mutex_lock(&shares_mutex); | |
11068 | if (tg->shares == shares) | |
11069 | goto done; | |
11070 | ||
11071 | tg->shares = shares; | |
11072 | for_each_possible_cpu(i) { | |
11073 | struct rq *rq = cpu_rq(i); | |
8a8c69c3 PZ |
11074 | struct sched_entity *se = tg->se[i]; |
11075 | struct rq_flags rf; | |
029632fb | 11076 | |
029632fb | 11077 | /* Propagate contribution to hierarchy */ |
8a8c69c3 | 11078 | rq_lock_irqsave(rq, &rf); |
71b1da46 | 11079 | update_rq_clock(rq); |
89ee048f | 11080 | for_each_sched_entity(se) { |
88c0616e | 11081 | update_load_avg(cfs_rq_of(se), se, UPDATE_TG); |
1ea6c46a | 11082 | update_cfs_group(se); |
89ee048f | 11083 | } |
8a8c69c3 | 11084 | rq_unlock_irqrestore(rq, &rf); |
029632fb PZ |
11085 | } |
11086 | ||
11087 | done: | |
11088 | mutex_unlock(&shares_mutex); | |
11089 | return 0; | |
11090 | } | |
11091 | #else /* CONFIG_FAIR_GROUP_SCHED */ | |
11092 | ||
11093 | void free_fair_sched_group(struct task_group *tg) { } | |
11094 | ||
11095 | int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent) | |
11096 | { | |
11097 | return 1; | |
11098 | } | |
11099 | ||
8663e24d PZ |
11100 | void online_fair_sched_group(struct task_group *tg) { } |
11101 | ||
6fe1f348 | 11102 | void unregister_fair_sched_group(struct task_group *tg) { } |
029632fb PZ |
11103 | |
11104 | #endif /* CONFIG_FAIR_GROUP_SCHED */ | |
11105 | ||
810b3817 | 11106 | |
6d686f45 | 11107 | static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task) |
0d721cea PW |
11108 | { |
11109 | struct sched_entity *se = &task->se; | |
0d721cea PW |
11110 | unsigned int rr_interval = 0; |
11111 | ||
11112 | /* | |
11113 | * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise | |
11114 | * idle runqueue: | |
11115 | */ | |
0d721cea | 11116 | if (rq->cfs.load.weight) |
a59f4e07 | 11117 | rr_interval = NS_TO_JIFFIES(sched_slice(cfs_rq_of(se), se)); |
0d721cea PW |
11118 | |
11119 | return rr_interval; | |
11120 | } | |
11121 | ||
bf0f6f24 IM |
11122 | /* |
11123 | * All the scheduling class methods: | |
11124 | */ | |
590d6979 SRV |
11125 | const struct sched_class fair_sched_class |
11126 | __attribute__((section("__fair_sched_class"))) = { | |
bf0f6f24 IM |
11127 | .enqueue_task = enqueue_task_fair, |
11128 | .dequeue_task = dequeue_task_fair, | |
11129 | .yield_task = yield_task_fair, | |
d95f4122 | 11130 | .yield_to_task = yield_to_task_fair, |
bf0f6f24 | 11131 | |
2e09bf55 | 11132 | .check_preempt_curr = check_preempt_wakeup, |
bf0f6f24 | 11133 | |
98c2f700 | 11134 | .pick_next_task = __pick_next_task_fair, |
bf0f6f24 | 11135 | .put_prev_task = put_prev_task_fair, |
03b7fad1 | 11136 | .set_next_task = set_next_task_fair, |
bf0f6f24 | 11137 | |
681f3e68 | 11138 | #ifdef CONFIG_SMP |
6e2df058 | 11139 | .balance = balance_fair, |
4ce72a2c | 11140 | .select_task_rq = select_task_rq_fair, |
0a74bef8 | 11141 | .migrate_task_rq = migrate_task_rq_fair, |
141965c7 | 11142 | |
0bcdcf28 CE |
11143 | .rq_online = rq_online_fair, |
11144 | .rq_offline = rq_offline_fair, | |
88ec22d3 | 11145 | |
12695578 | 11146 | .task_dead = task_dead_fair, |
c5b28038 | 11147 | .set_cpus_allowed = set_cpus_allowed_common, |
681f3e68 | 11148 | #endif |
bf0f6f24 | 11149 | |
bf0f6f24 | 11150 | .task_tick = task_tick_fair, |
cd29fe6f | 11151 | .task_fork = task_fork_fair, |
cb469845 SR |
11152 | |
11153 | .prio_changed = prio_changed_fair, | |
da7a735e | 11154 | .switched_from = switched_from_fair, |
cb469845 | 11155 | .switched_to = switched_to_fair, |
810b3817 | 11156 | |
0d721cea PW |
11157 | .get_rr_interval = get_rr_interval_fair, |
11158 | ||
6e998916 SG |
11159 | .update_curr = update_curr_fair, |
11160 | ||
810b3817 | 11161 | #ifdef CONFIG_FAIR_GROUP_SCHED |
ea86cb4b | 11162 | .task_change_group = task_change_group_fair, |
810b3817 | 11163 | #endif |
982d9cdc PB |
11164 | |
11165 | #ifdef CONFIG_UCLAMP_TASK | |
11166 | .uclamp_enabled = 1, | |
11167 | #endif | |
bf0f6f24 IM |
11168 | }; |
11169 | ||
11170 | #ifdef CONFIG_SCHED_DEBUG | |
029632fb | 11171 | void print_cfs_stats(struct seq_file *m, int cpu) |
bf0f6f24 | 11172 | { |
039ae8bc | 11173 | struct cfs_rq *cfs_rq, *pos; |
bf0f6f24 | 11174 | |
5973e5b9 | 11175 | rcu_read_lock(); |
039ae8bc | 11176 | for_each_leaf_cfs_rq_safe(cpu_rq(cpu), cfs_rq, pos) |
5cef9eca | 11177 | print_cfs_rq(m, cpu, cfs_rq); |
5973e5b9 | 11178 | rcu_read_unlock(); |
bf0f6f24 | 11179 | } |
397f2378 SD |
11180 | |
11181 | #ifdef CONFIG_NUMA_BALANCING | |
11182 | void show_numa_stats(struct task_struct *p, struct seq_file *m) | |
11183 | { | |
11184 | int node; | |
11185 | unsigned long tsf = 0, tpf = 0, gsf = 0, gpf = 0; | |
cb361d8c | 11186 | struct numa_group *ng; |
397f2378 | 11187 | |
cb361d8c JH |
11188 | rcu_read_lock(); |
11189 | ng = rcu_dereference(p->numa_group); | |
397f2378 SD |
11190 | for_each_online_node(node) { |
11191 | if (p->numa_faults) { | |
11192 | tsf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 0)]; | |
11193 | tpf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 1)]; | |
11194 | } | |
cb361d8c JH |
11195 | if (ng) { |
11196 | gsf = ng->faults[task_faults_idx(NUMA_MEM, node, 0)], | |
11197 | gpf = ng->faults[task_faults_idx(NUMA_MEM, node, 1)]; | |
397f2378 SD |
11198 | } |
11199 | print_numa_stats(m, node, tsf, tpf, gsf, gpf); | |
11200 | } | |
cb361d8c | 11201 | rcu_read_unlock(); |
397f2378 SD |
11202 | } |
11203 | #endif /* CONFIG_NUMA_BALANCING */ | |
11204 | #endif /* CONFIG_SCHED_DEBUG */ | |
029632fb PZ |
11205 | |
11206 | __init void init_sched_fair_class(void) | |
11207 | { | |
11208 | #ifdef CONFIG_SMP | |
11209 | open_softirq(SCHED_SOFTIRQ, run_rebalance_domains); | |
11210 | ||
3451d024 | 11211 | #ifdef CONFIG_NO_HZ_COMMON |
554cecaf | 11212 | nohz.next_balance = jiffies; |
f643ea22 | 11213 | nohz.next_blocked = jiffies; |
029632fb | 11214 | zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT); |
029632fb PZ |
11215 | #endif |
11216 | #endif /* SMP */ | |
11217 | ||
11218 | } | |
3c93a0c0 QY |
11219 | |
11220 | /* | |
11221 | * Helper functions to facilitate extracting info from tracepoints. | |
11222 | */ | |
11223 | ||
11224 | const struct sched_avg *sched_trace_cfs_rq_avg(struct cfs_rq *cfs_rq) | |
11225 | { | |
11226 | #ifdef CONFIG_SMP | |
11227 | return cfs_rq ? &cfs_rq->avg : NULL; | |
11228 | #else | |
11229 | return NULL; | |
11230 | #endif | |
11231 | } | |
11232 | EXPORT_SYMBOL_GPL(sched_trace_cfs_rq_avg); | |
11233 | ||
11234 | char *sched_trace_cfs_rq_path(struct cfs_rq *cfs_rq, char *str, int len) | |
11235 | { | |
11236 | if (!cfs_rq) { | |
11237 | if (str) | |
11238 | strlcpy(str, "(null)", len); | |
11239 | else | |
11240 | return NULL; | |
11241 | } | |
11242 | ||
11243 | cfs_rq_tg_path(cfs_rq, str, len); | |
11244 | return str; | |
11245 | } | |
11246 | EXPORT_SYMBOL_GPL(sched_trace_cfs_rq_path); | |
11247 | ||
11248 | int sched_trace_cfs_rq_cpu(struct cfs_rq *cfs_rq) | |
11249 | { | |
11250 | return cfs_rq ? cpu_of(rq_of(cfs_rq)) : -1; | |
11251 | } | |
11252 | EXPORT_SYMBOL_GPL(sched_trace_cfs_rq_cpu); | |
11253 | ||
11254 | const struct sched_avg *sched_trace_rq_avg_rt(struct rq *rq) | |
11255 | { | |
11256 | #ifdef CONFIG_SMP | |
11257 | return rq ? &rq->avg_rt : NULL; | |
11258 | #else | |
11259 | return NULL; | |
11260 | #endif | |
11261 | } | |
11262 | EXPORT_SYMBOL_GPL(sched_trace_rq_avg_rt); | |
11263 | ||
11264 | const struct sched_avg *sched_trace_rq_avg_dl(struct rq *rq) | |
11265 | { | |
11266 | #ifdef CONFIG_SMP | |
11267 | return rq ? &rq->avg_dl : NULL; | |
11268 | #else | |
11269 | return NULL; | |
11270 | #endif | |
11271 | } | |
11272 | EXPORT_SYMBOL_GPL(sched_trace_rq_avg_dl); | |
11273 | ||
11274 | const struct sched_avg *sched_trace_rq_avg_irq(struct rq *rq) | |
11275 | { | |
11276 | #if defined(CONFIG_SMP) && defined(CONFIG_HAVE_SCHED_AVG_IRQ) | |
11277 | return rq ? &rq->avg_irq : NULL; | |
11278 | #else | |
11279 | return NULL; | |
11280 | #endif | |
11281 | } | |
11282 | EXPORT_SYMBOL_GPL(sched_trace_rq_avg_irq); | |
11283 | ||
11284 | int sched_trace_rq_cpu(struct rq *rq) | |
11285 | { | |
11286 | return rq ? cpu_of(rq) : -1; | |
11287 | } | |
11288 | EXPORT_SYMBOL_GPL(sched_trace_rq_cpu); | |
11289 | ||
11290 | const struct cpumask *sched_trace_rd_span(struct root_domain *rd) | |
11291 | { | |
11292 | #ifdef CONFIG_SMP | |
11293 | return rd ? rd->span : NULL; | |
11294 | #else | |
11295 | return NULL; | |
11296 | #endif | |
11297 | } | |
11298 | EXPORT_SYMBOL_GPL(sched_trace_rd_span); |