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 IM |
40 | unsigned int sysctl_sched_latency = 6000000ULL; |
41 | 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 | */ |
2b4d5b25 IM |
61 | unsigned int sysctl_sched_min_granularity = 750000ULL; |
62 | 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 | */ |
2b4d5b25 IM |
84 | unsigned int sysctl_sched_wakeup_granularity = 1000000UL; |
85 | unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL; | |
bf0f6f24 | 86 | |
2b4d5b25 | 87 | const_debug unsigned int sysctl_sched_migration_cost = 500000UL; |
da84d961 | 88 | |
afe06efd TC |
89 | #ifdef CONFIG_SMP |
90 | /* | |
97fb7a0a | 91 | * For asym packing, by default the lower numbered CPU has higher priority. |
afe06efd TC |
92 | */ |
93 | int __weak arch_asym_cpu_priority(int cpu) | |
94 | { | |
95 | return -cpu; | |
96 | } | |
97 | #endif | |
98 | ||
ec12cb7f PT |
99 | #ifdef CONFIG_CFS_BANDWIDTH |
100 | /* | |
101 | * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool | |
102 | * each time a cfs_rq requests quota. | |
103 | * | |
104 | * Note: in the case that the slice exceeds the runtime remaining (either due | |
105 | * to consumption or the quota being specified to be smaller than the slice) | |
106 | * we will always only issue the remaining available time. | |
107 | * | |
2b4d5b25 IM |
108 | * (default: 5 msec, units: microseconds) |
109 | */ | |
110 | unsigned int sysctl_sched_cfs_bandwidth_slice = 5000UL; | |
ec12cb7f PT |
111 | #endif |
112 | ||
3273163c MR |
113 | /* |
114 | * The margin used when comparing utilization with CPU capacity: | |
893c5d22 | 115 | * util * margin < capacity * 1024 |
2b4d5b25 IM |
116 | * |
117 | * (default: ~20%) | |
3273163c | 118 | */ |
2b4d5b25 | 119 | unsigned int capacity_margin = 1280; |
3273163c | 120 | |
8527632d PG |
121 | static inline void update_load_add(struct load_weight *lw, unsigned long inc) |
122 | { | |
123 | lw->weight += inc; | |
124 | lw->inv_weight = 0; | |
125 | } | |
126 | ||
127 | static inline void update_load_sub(struct load_weight *lw, unsigned long dec) | |
128 | { | |
129 | lw->weight -= dec; | |
130 | lw->inv_weight = 0; | |
131 | } | |
132 | ||
133 | static inline void update_load_set(struct load_weight *lw, unsigned long w) | |
134 | { | |
135 | lw->weight = w; | |
136 | lw->inv_weight = 0; | |
137 | } | |
138 | ||
029632fb PZ |
139 | /* |
140 | * Increase the granularity value when there are more CPUs, | |
141 | * because with more CPUs the 'effective latency' as visible | |
142 | * to users decreases. But the relationship is not linear, | |
143 | * so pick a second-best guess by going with the log2 of the | |
144 | * number of CPUs. | |
145 | * | |
146 | * This idea comes from the SD scheduler of Con Kolivas: | |
147 | */ | |
58ac93e4 | 148 | static unsigned int get_update_sysctl_factor(void) |
029632fb | 149 | { |
58ac93e4 | 150 | unsigned int cpus = min_t(unsigned int, num_online_cpus(), 8); |
029632fb PZ |
151 | unsigned int factor; |
152 | ||
153 | switch (sysctl_sched_tunable_scaling) { | |
154 | case SCHED_TUNABLESCALING_NONE: | |
155 | factor = 1; | |
156 | break; | |
157 | case SCHED_TUNABLESCALING_LINEAR: | |
158 | factor = cpus; | |
159 | break; | |
160 | case SCHED_TUNABLESCALING_LOG: | |
161 | default: | |
162 | factor = 1 + ilog2(cpus); | |
163 | break; | |
164 | } | |
165 | ||
166 | return factor; | |
167 | } | |
168 | ||
169 | static void update_sysctl(void) | |
170 | { | |
171 | unsigned int factor = get_update_sysctl_factor(); | |
172 | ||
173 | #define SET_SYSCTL(name) \ | |
174 | (sysctl_##name = (factor) * normalized_sysctl_##name) | |
175 | SET_SYSCTL(sched_min_granularity); | |
176 | SET_SYSCTL(sched_latency); | |
177 | SET_SYSCTL(sched_wakeup_granularity); | |
178 | #undef SET_SYSCTL | |
179 | } | |
180 | ||
181 | void sched_init_granularity(void) | |
182 | { | |
183 | update_sysctl(); | |
184 | } | |
185 | ||
9dbdb155 | 186 | #define WMULT_CONST (~0U) |
029632fb PZ |
187 | #define WMULT_SHIFT 32 |
188 | ||
9dbdb155 PZ |
189 | static void __update_inv_weight(struct load_weight *lw) |
190 | { | |
191 | unsigned long w; | |
192 | ||
193 | if (likely(lw->inv_weight)) | |
194 | return; | |
195 | ||
196 | w = scale_load_down(lw->weight); | |
197 | ||
198 | if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST)) | |
199 | lw->inv_weight = 1; | |
200 | else if (unlikely(!w)) | |
201 | lw->inv_weight = WMULT_CONST; | |
202 | else | |
203 | lw->inv_weight = WMULT_CONST / w; | |
204 | } | |
029632fb PZ |
205 | |
206 | /* | |
9dbdb155 PZ |
207 | * delta_exec * weight / lw.weight |
208 | * OR | |
209 | * (delta_exec * (weight * lw->inv_weight)) >> WMULT_SHIFT | |
210 | * | |
1c3de5e1 | 211 | * Either weight := NICE_0_LOAD and lw \e sched_prio_to_wmult[], in which case |
9dbdb155 PZ |
212 | * we're guaranteed shift stays positive because inv_weight is guaranteed to |
213 | * fit 32 bits, and NICE_0_LOAD gives another 10 bits; therefore shift >= 22. | |
214 | * | |
215 | * Or, weight =< lw.weight (because lw.weight is the runqueue weight), thus | |
216 | * weight/lw.weight <= 1, and therefore our shift will also be positive. | |
029632fb | 217 | */ |
9dbdb155 | 218 | static u64 __calc_delta(u64 delta_exec, unsigned long weight, struct load_weight *lw) |
029632fb | 219 | { |
9dbdb155 PZ |
220 | u64 fact = scale_load_down(weight); |
221 | int shift = WMULT_SHIFT; | |
029632fb | 222 | |
9dbdb155 | 223 | __update_inv_weight(lw); |
029632fb | 224 | |
9dbdb155 PZ |
225 | if (unlikely(fact >> 32)) { |
226 | while (fact >> 32) { | |
227 | fact >>= 1; | |
228 | shift--; | |
229 | } | |
029632fb PZ |
230 | } |
231 | ||
9dbdb155 PZ |
232 | /* hint to use a 32x32->64 mul */ |
233 | fact = (u64)(u32)fact * lw->inv_weight; | |
029632fb | 234 | |
9dbdb155 PZ |
235 | while (fact >> 32) { |
236 | fact >>= 1; | |
237 | shift--; | |
238 | } | |
029632fb | 239 | |
9dbdb155 | 240 | return mul_u64_u32_shr(delta_exec, fact, shift); |
029632fb PZ |
241 | } |
242 | ||
243 | ||
244 | const struct sched_class fair_sched_class; | |
a4c2f00f | 245 | |
bf0f6f24 IM |
246 | /************************************************************** |
247 | * CFS operations on generic schedulable entities: | |
248 | */ | |
249 | ||
62160e3f | 250 | #ifdef CONFIG_FAIR_GROUP_SCHED |
bf0f6f24 | 251 | |
62160e3f | 252 | /* cpu runqueue to which this cfs_rq is attached */ |
bf0f6f24 IM |
253 | static inline struct rq *rq_of(struct cfs_rq *cfs_rq) |
254 | { | |
62160e3f | 255 | return cfs_rq->rq; |
bf0f6f24 IM |
256 | } |
257 | ||
62160e3f IM |
258 | /* An entity is a task if it doesn't "own" a runqueue */ |
259 | #define entity_is_task(se) (!se->my_q) | |
bf0f6f24 | 260 | |
8f48894f PZ |
261 | static inline struct task_struct *task_of(struct sched_entity *se) |
262 | { | |
9148a3a1 | 263 | SCHED_WARN_ON(!entity_is_task(se)); |
8f48894f PZ |
264 | return container_of(se, struct task_struct, se); |
265 | } | |
266 | ||
b758149c PZ |
267 | /* Walk up scheduling entities hierarchy */ |
268 | #define for_each_sched_entity(se) \ | |
269 | for (; se; se = se->parent) | |
270 | ||
271 | static inline struct cfs_rq *task_cfs_rq(struct task_struct *p) | |
272 | { | |
273 | return p->se.cfs_rq; | |
274 | } | |
275 | ||
276 | /* runqueue on which this entity is (to be) queued */ | |
277 | static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se) | |
278 | { | |
279 | return se->cfs_rq; | |
280 | } | |
281 | ||
282 | /* runqueue "owned" by this group */ | |
283 | static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp) | |
284 | { | |
285 | return grp->my_q; | |
286 | } | |
287 | ||
3d4b47b4 PZ |
288 | static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq) |
289 | { | |
290 | if (!cfs_rq->on_list) { | |
9c2791f9 VG |
291 | struct rq *rq = rq_of(cfs_rq); |
292 | int cpu = cpu_of(rq); | |
67e86250 PT |
293 | /* |
294 | * Ensure we either appear before our parent (if already | |
295 | * enqueued) or force our parent to appear after us when it is | |
9c2791f9 VG |
296 | * enqueued. The fact that we always enqueue bottom-up |
297 | * reduces this to two cases and a special case for the root | |
298 | * cfs_rq. Furthermore, it also means that we will always reset | |
299 | * tmp_alone_branch either when the branch is connected | |
300 | * to a tree or when we reach the beg of the tree | |
67e86250 PT |
301 | */ |
302 | if (cfs_rq->tg->parent && | |
9c2791f9 VG |
303 | cfs_rq->tg->parent->cfs_rq[cpu]->on_list) { |
304 | /* | |
305 | * If parent is already on the list, we add the child | |
306 | * just before. Thanks to circular linked property of | |
307 | * the list, this means to put the child at the tail | |
308 | * of the list that starts by parent. | |
309 | */ | |
310 | list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list, | |
311 | &(cfs_rq->tg->parent->cfs_rq[cpu]->leaf_cfs_rq_list)); | |
312 | /* | |
313 | * The branch is now connected to its tree so we can | |
314 | * reset tmp_alone_branch to the beginning of the | |
315 | * list. | |
316 | */ | |
317 | rq->tmp_alone_branch = &rq->leaf_cfs_rq_list; | |
318 | } else if (!cfs_rq->tg->parent) { | |
319 | /* | |
320 | * cfs rq without parent should be put | |
321 | * at the tail of the list. | |
322 | */ | |
67e86250 | 323 | list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list, |
9c2791f9 VG |
324 | &rq->leaf_cfs_rq_list); |
325 | /* | |
326 | * We have reach the beg of a tree so we can reset | |
327 | * tmp_alone_branch to the beginning of the list. | |
328 | */ | |
329 | rq->tmp_alone_branch = &rq->leaf_cfs_rq_list; | |
330 | } else { | |
331 | /* | |
332 | * The parent has not already been added so we want to | |
333 | * make sure that it will be put after us. | |
334 | * tmp_alone_branch points to the beg of the branch | |
335 | * where we will add parent. | |
336 | */ | |
337 | list_add_rcu(&cfs_rq->leaf_cfs_rq_list, | |
338 | rq->tmp_alone_branch); | |
339 | /* | |
340 | * update tmp_alone_branch to points to the new beg | |
341 | * of the branch | |
342 | */ | |
343 | rq->tmp_alone_branch = &cfs_rq->leaf_cfs_rq_list; | |
67e86250 | 344 | } |
3d4b47b4 PZ |
345 | |
346 | cfs_rq->on_list = 1; | |
347 | } | |
348 | } | |
349 | ||
350 | static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq) | |
351 | { | |
352 | if (cfs_rq->on_list) { | |
353 | list_del_rcu(&cfs_rq->leaf_cfs_rq_list); | |
354 | cfs_rq->on_list = 0; | |
355 | } | |
356 | } | |
357 | ||
b758149c | 358 | /* Iterate thr' all leaf cfs_rq's on a runqueue */ |
a9e7f654 TH |
359 | #define for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) \ |
360 | list_for_each_entry_safe(cfs_rq, pos, &rq->leaf_cfs_rq_list, \ | |
361 | leaf_cfs_rq_list) | |
b758149c PZ |
362 | |
363 | /* Do the two (enqueued) entities belong to the same group ? */ | |
fed14d45 | 364 | static inline struct cfs_rq * |
b758149c PZ |
365 | is_same_group(struct sched_entity *se, struct sched_entity *pse) |
366 | { | |
367 | if (se->cfs_rq == pse->cfs_rq) | |
fed14d45 | 368 | return se->cfs_rq; |
b758149c | 369 | |
fed14d45 | 370 | return NULL; |
b758149c PZ |
371 | } |
372 | ||
373 | static inline struct sched_entity *parent_entity(struct sched_entity *se) | |
374 | { | |
375 | return se->parent; | |
376 | } | |
377 | ||
464b7527 PZ |
378 | static void |
379 | find_matching_se(struct sched_entity **se, struct sched_entity **pse) | |
380 | { | |
381 | int se_depth, pse_depth; | |
382 | ||
383 | /* | |
384 | * preemption test can be made between sibling entities who are in the | |
385 | * same cfs_rq i.e who have a common parent. Walk up the hierarchy of | |
386 | * both tasks until we find their ancestors who are siblings of common | |
387 | * parent. | |
388 | */ | |
389 | ||
390 | /* First walk up until both entities are at same depth */ | |
fed14d45 PZ |
391 | se_depth = (*se)->depth; |
392 | pse_depth = (*pse)->depth; | |
464b7527 PZ |
393 | |
394 | while (se_depth > pse_depth) { | |
395 | se_depth--; | |
396 | *se = parent_entity(*se); | |
397 | } | |
398 | ||
399 | while (pse_depth > se_depth) { | |
400 | pse_depth--; | |
401 | *pse = parent_entity(*pse); | |
402 | } | |
403 | ||
404 | while (!is_same_group(*se, *pse)) { | |
405 | *se = parent_entity(*se); | |
406 | *pse = parent_entity(*pse); | |
407 | } | |
408 | } | |
409 | ||
8f48894f PZ |
410 | #else /* !CONFIG_FAIR_GROUP_SCHED */ |
411 | ||
412 | static inline struct task_struct *task_of(struct sched_entity *se) | |
413 | { | |
414 | return container_of(se, struct task_struct, se); | |
415 | } | |
bf0f6f24 | 416 | |
62160e3f IM |
417 | static inline struct rq *rq_of(struct cfs_rq *cfs_rq) |
418 | { | |
419 | return container_of(cfs_rq, struct rq, cfs); | |
bf0f6f24 IM |
420 | } |
421 | ||
422 | #define entity_is_task(se) 1 | |
423 | ||
b758149c PZ |
424 | #define for_each_sched_entity(se) \ |
425 | for (; se; se = NULL) | |
bf0f6f24 | 426 | |
b758149c | 427 | static inline struct cfs_rq *task_cfs_rq(struct task_struct *p) |
bf0f6f24 | 428 | { |
b758149c | 429 | return &task_rq(p)->cfs; |
bf0f6f24 IM |
430 | } |
431 | ||
b758149c PZ |
432 | static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se) |
433 | { | |
434 | struct task_struct *p = task_of(se); | |
435 | struct rq *rq = task_rq(p); | |
436 | ||
437 | return &rq->cfs; | |
438 | } | |
439 | ||
440 | /* runqueue "owned" by this group */ | |
441 | static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp) | |
442 | { | |
443 | return NULL; | |
444 | } | |
445 | ||
3d4b47b4 PZ |
446 | static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq) |
447 | { | |
448 | } | |
449 | ||
450 | static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq) | |
451 | { | |
452 | } | |
453 | ||
a9e7f654 TH |
454 | #define for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) \ |
455 | for (cfs_rq = &rq->cfs, pos = NULL; cfs_rq; cfs_rq = pos) | |
b758149c | 456 | |
b758149c PZ |
457 | static inline struct sched_entity *parent_entity(struct sched_entity *se) |
458 | { | |
459 | return NULL; | |
460 | } | |
461 | ||
464b7527 PZ |
462 | static inline void |
463 | find_matching_se(struct sched_entity **se, struct sched_entity **pse) | |
464 | { | |
465 | } | |
466 | ||
b758149c PZ |
467 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
468 | ||
6c16a6dc | 469 | static __always_inline |
9dbdb155 | 470 | void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec); |
bf0f6f24 IM |
471 | |
472 | /************************************************************** | |
473 | * Scheduling class tree data structure manipulation methods: | |
474 | */ | |
475 | ||
1bf08230 | 476 | static inline u64 max_vruntime(u64 max_vruntime, u64 vruntime) |
02e0431a | 477 | { |
1bf08230 | 478 | s64 delta = (s64)(vruntime - max_vruntime); |
368059a9 | 479 | if (delta > 0) |
1bf08230 | 480 | max_vruntime = vruntime; |
02e0431a | 481 | |
1bf08230 | 482 | return max_vruntime; |
02e0431a PZ |
483 | } |
484 | ||
0702e3eb | 485 | static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime) |
b0ffd246 PZ |
486 | { |
487 | s64 delta = (s64)(vruntime - min_vruntime); | |
488 | if (delta < 0) | |
489 | min_vruntime = vruntime; | |
490 | ||
491 | return min_vruntime; | |
492 | } | |
493 | ||
54fdc581 FC |
494 | static inline int entity_before(struct sched_entity *a, |
495 | struct sched_entity *b) | |
496 | { | |
497 | return (s64)(a->vruntime - b->vruntime) < 0; | |
498 | } | |
499 | ||
1af5f730 PZ |
500 | static void update_min_vruntime(struct cfs_rq *cfs_rq) |
501 | { | |
b60205c7 | 502 | struct sched_entity *curr = cfs_rq->curr; |
bfb06889 | 503 | struct rb_node *leftmost = rb_first_cached(&cfs_rq->tasks_timeline); |
b60205c7 | 504 | |
1af5f730 PZ |
505 | u64 vruntime = cfs_rq->min_vruntime; |
506 | ||
b60205c7 PZ |
507 | if (curr) { |
508 | if (curr->on_rq) | |
509 | vruntime = curr->vruntime; | |
510 | else | |
511 | curr = NULL; | |
512 | } | |
1af5f730 | 513 | |
bfb06889 DB |
514 | if (leftmost) { /* non-empty tree */ |
515 | struct sched_entity *se; | |
516 | se = rb_entry(leftmost, struct sched_entity, run_node); | |
1af5f730 | 517 | |
b60205c7 | 518 | if (!curr) |
1af5f730 PZ |
519 | vruntime = se->vruntime; |
520 | else | |
521 | vruntime = min_vruntime(vruntime, se->vruntime); | |
522 | } | |
523 | ||
1bf08230 | 524 | /* ensure we never gain time by being placed backwards. */ |
1af5f730 | 525 | cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime); |
3fe1698b PZ |
526 | #ifndef CONFIG_64BIT |
527 | smp_wmb(); | |
528 | cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime; | |
529 | #endif | |
1af5f730 PZ |
530 | } |
531 | ||
bf0f6f24 IM |
532 | /* |
533 | * Enqueue an entity into the rb-tree: | |
534 | */ | |
0702e3eb | 535 | static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 | 536 | { |
bfb06889 | 537 | struct rb_node **link = &cfs_rq->tasks_timeline.rb_root.rb_node; |
bf0f6f24 IM |
538 | struct rb_node *parent = NULL; |
539 | struct sched_entity *entry; | |
bfb06889 | 540 | bool leftmost = true; |
bf0f6f24 IM |
541 | |
542 | /* | |
543 | * Find the right place in the rbtree: | |
544 | */ | |
545 | while (*link) { | |
546 | parent = *link; | |
547 | entry = rb_entry(parent, struct sched_entity, run_node); | |
548 | /* | |
549 | * We dont care about collisions. Nodes with | |
550 | * the same key stay together. | |
551 | */ | |
2bd2d6f2 | 552 | if (entity_before(se, entry)) { |
bf0f6f24 IM |
553 | link = &parent->rb_left; |
554 | } else { | |
555 | link = &parent->rb_right; | |
bfb06889 | 556 | leftmost = false; |
bf0f6f24 IM |
557 | } |
558 | } | |
559 | ||
bf0f6f24 | 560 | rb_link_node(&se->run_node, parent, link); |
bfb06889 DB |
561 | rb_insert_color_cached(&se->run_node, |
562 | &cfs_rq->tasks_timeline, leftmost); | |
bf0f6f24 IM |
563 | } |
564 | ||
0702e3eb | 565 | static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 | 566 | { |
bfb06889 | 567 | rb_erase_cached(&se->run_node, &cfs_rq->tasks_timeline); |
bf0f6f24 IM |
568 | } |
569 | ||
029632fb | 570 | struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq) |
bf0f6f24 | 571 | { |
bfb06889 | 572 | struct rb_node *left = rb_first_cached(&cfs_rq->tasks_timeline); |
f4b6755f PZ |
573 | |
574 | if (!left) | |
575 | return NULL; | |
576 | ||
577 | return rb_entry(left, struct sched_entity, run_node); | |
bf0f6f24 IM |
578 | } |
579 | ||
ac53db59 RR |
580 | static struct sched_entity *__pick_next_entity(struct sched_entity *se) |
581 | { | |
582 | struct rb_node *next = rb_next(&se->run_node); | |
583 | ||
584 | if (!next) | |
585 | return NULL; | |
586 | ||
587 | return rb_entry(next, struct sched_entity, run_node); | |
588 | } | |
589 | ||
590 | #ifdef CONFIG_SCHED_DEBUG | |
029632fb | 591 | struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq) |
aeb73b04 | 592 | { |
bfb06889 | 593 | struct rb_node *last = rb_last(&cfs_rq->tasks_timeline.rb_root); |
aeb73b04 | 594 | |
70eee74b BS |
595 | if (!last) |
596 | return NULL; | |
7eee3e67 IM |
597 | |
598 | return rb_entry(last, struct sched_entity, run_node); | |
aeb73b04 PZ |
599 | } |
600 | ||
bf0f6f24 IM |
601 | /************************************************************** |
602 | * Scheduling class statistics methods: | |
603 | */ | |
604 | ||
acb4a848 | 605 | int sched_proc_update_handler(struct ctl_table *table, int write, |
8d65af78 | 606 | void __user *buffer, size_t *lenp, |
b2be5e96 PZ |
607 | loff_t *ppos) |
608 | { | |
8d65af78 | 609 | int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
58ac93e4 | 610 | unsigned int factor = get_update_sysctl_factor(); |
b2be5e96 PZ |
611 | |
612 | if (ret || !write) | |
613 | return ret; | |
614 | ||
615 | sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency, | |
616 | sysctl_sched_min_granularity); | |
617 | ||
acb4a848 CE |
618 | #define WRT_SYSCTL(name) \ |
619 | (normalized_sysctl_##name = sysctl_##name / (factor)) | |
620 | WRT_SYSCTL(sched_min_granularity); | |
621 | WRT_SYSCTL(sched_latency); | |
622 | WRT_SYSCTL(sched_wakeup_granularity); | |
acb4a848 CE |
623 | #undef WRT_SYSCTL |
624 | ||
b2be5e96 PZ |
625 | return 0; |
626 | } | |
627 | #endif | |
647e7cac | 628 | |
a7be37ac | 629 | /* |
f9c0b095 | 630 | * delta /= w |
a7be37ac | 631 | */ |
9dbdb155 | 632 | static inline u64 calc_delta_fair(u64 delta, struct sched_entity *se) |
a7be37ac | 633 | { |
f9c0b095 | 634 | if (unlikely(se->load.weight != NICE_0_LOAD)) |
9dbdb155 | 635 | delta = __calc_delta(delta, NICE_0_LOAD, &se->load); |
a7be37ac PZ |
636 | |
637 | return delta; | |
638 | } | |
639 | ||
647e7cac IM |
640 | /* |
641 | * The idea is to set a period in which each task runs once. | |
642 | * | |
532b1858 | 643 | * When there are too many tasks (sched_nr_latency) we have to stretch |
647e7cac IM |
644 | * this period because otherwise the slices get too small. |
645 | * | |
646 | * p = (nr <= nl) ? l : l*nr/nl | |
647 | */ | |
4d78e7b6 PZ |
648 | static u64 __sched_period(unsigned long nr_running) |
649 | { | |
8e2b0bf3 BF |
650 | if (unlikely(nr_running > sched_nr_latency)) |
651 | return nr_running * sysctl_sched_min_granularity; | |
652 | else | |
653 | return sysctl_sched_latency; | |
4d78e7b6 PZ |
654 | } |
655 | ||
647e7cac IM |
656 | /* |
657 | * We calculate the wall-time slice from the period by taking a part | |
658 | * proportional to the weight. | |
659 | * | |
f9c0b095 | 660 | * s = p*P[w/rw] |
647e7cac | 661 | */ |
6d0f0ebd | 662 | static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se) |
21805085 | 663 | { |
0a582440 | 664 | u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq); |
f9c0b095 | 665 | |
0a582440 | 666 | for_each_sched_entity(se) { |
6272d68c | 667 | struct load_weight *load; |
3104bf03 | 668 | struct load_weight lw; |
6272d68c LM |
669 | |
670 | cfs_rq = cfs_rq_of(se); | |
671 | load = &cfs_rq->load; | |
f9c0b095 | 672 | |
0a582440 | 673 | if (unlikely(!se->on_rq)) { |
3104bf03 | 674 | lw = cfs_rq->load; |
0a582440 MG |
675 | |
676 | update_load_add(&lw, se->load.weight); | |
677 | load = &lw; | |
678 | } | |
9dbdb155 | 679 | slice = __calc_delta(slice, se->load.weight, load); |
0a582440 MG |
680 | } |
681 | return slice; | |
bf0f6f24 IM |
682 | } |
683 | ||
647e7cac | 684 | /* |
660cc00f | 685 | * We calculate the vruntime slice of a to-be-inserted task. |
647e7cac | 686 | * |
f9c0b095 | 687 | * vs = s/w |
647e7cac | 688 | */ |
f9c0b095 | 689 | static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se) |
67e9fb2a | 690 | { |
f9c0b095 | 691 | return calc_delta_fair(sched_slice(cfs_rq, se), se); |
a7be37ac PZ |
692 | } |
693 | ||
a75cdaa9 | 694 | #ifdef CONFIG_SMP |
283e2ed3 PZ |
695 | |
696 | #include "sched-pelt.h" | |
697 | ||
772bd008 | 698 | static int select_idle_sibling(struct task_struct *p, int prev_cpu, int cpu); |
fb13c7ee MG |
699 | static unsigned long task_h_load(struct task_struct *p); |
700 | ||
540247fb YD |
701 | /* Give new sched_entity start runnable values to heavy its load in infant time */ |
702 | void init_entity_runnable_average(struct sched_entity *se) | |
a75cdaa9 | 703 | { |
540247fb | 704 | struct sched_avg *sa = &se->avg; |
a75cdaa9 | 705 | |
f207934f PZ |
706 | memset(sa, 0, sizeof(*sa)); |
707 | ||
b5a9b340 VG |
708 | /* |
709 | * Tasks are intialized with full load to be seen as heavy tasks until | |
710 | * they get a chance to stabilize to their real load level. | |
711 | * Group entities are intialized with zero load to reflect the fact that | |
712 | * nothing has been attached to the task group yet. | |
713 | */ | |
714 | if (entity_is_task(se)) | |
1ea6c46a | 715 | sa->runnable_load_avg = sa->load_avg = scale_load_down(se->load.weight); |
1ea6c46a | 716 | |
f207934f PZ |
717 | se->runnable_weight = se->load.weight; |
718 | ||
9d89c257 | 719 | /* when this task enqueue'ed, it will contribute to its cfs_rq's load_avg */ |
a75cdaa9 | 720 | } |
7ea241af | 721 | |
7dc603c9 | 722 | static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq); |
df217913 | 723 | static void attach_entity_cfs_rq(struct sched_entity *se); |
7dc603c9 | 724 | |
2b8c41da YD |
725 | /* |
726 | * With new tasks being created, their initial util_avgs are extrapolated | |
727 | * based on the cfs_rq's current util_avg: | |
728 | * | |
729 | * util_avg = cfs_rq->util_avg / (cfs_rq->load_avg + 1) * se.load.weight | |
730 | * | |
731 | * However, in many cases, the above util_avg does not give a desired | |
732 | * value. Moreover, the sum of the util_avgs may be divergent, such | |
733 | * as when the series is a harmonic series. | |
734 | * | |
735 | * To solve this problem, we also cap the util_avg of successive tasks to | |
736 | * only 1/2 of the left utilization budget: | |
737 | * | |
738 | * util_avg_cap = (1024 - cfs_rq->avg.util_avg) / 2^n | |
739 | * | |
740 | * where n denotes the nth task. | |
741 | * | |
742 | * For example, a simplest series from the beginning would be like: | |
743 | * | |
744 | * task util_avg: 512, 256, 128, 64, 32, 16, 8, ... | |
745 | * cfs_rq util_avg: 512, 768, 896, 960, 992, 1008, 1016, ... | |
746 | * | |
747 | * Finally, that extrapolated util_avg is clamped to the cap (util_avg_cap) | |
748 | * if util_avg > util_avg_cap. | |
749 | */ | |
750 | void post_init_entity_util_avg(struct sched_entity *se) | |
751 | { | |
752 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
753 | struct sched_avg *sa = &se->avg; | |
172895e6 | 754 | long cap = (long)(SCHED_CAPACITY_SCALE - cfs_rq->avg.util_avg) / 2; |
2b8c41da YD |
755 | |
756 | if (cap > 0) { | |
757 | if (cfs_rq->avg.util_avg != 0) { | |
758 | sa->util_avg = cfs_rq->avg.util_avg * se->load.weight; | |
759 | sa->util_avg /= (cfs_rq->avg.load_avg + 1); | |
760 | ||
761 | if (sa->util_avg > cap) | |
762 | sa->util_avg = cap; | |
763 | } else { | |
764 | sa->util_avg = cap; | |
765 | } | |
2b8c41da | 766 | } |
7dc603c9 PZ |
767 | |
768 | if (entity_is_task(se)) { | |
769 | struct task_struct *p = task_of(se); | |
770 | if (p->sched_class != &fair_sched_class) { | |
771 | /* | |
772 | * For !fair tasks do: | |
773 | * | |
3a123bbb | 774 | update_cfs_rq_load_avg(now, cfs_rq); |
ea14b57e | 775 | attach_entity_load_avg(cfs_rq, se, 0); |
7dc603c9 PZ |
776 | switched_from_fair(rq, p); |
777 | * | |
778 | * such that the next switched_to_fair() has the | |
779 | * expected state. | |
780 | */ | |
df217913 | 781 | se->avg.last_update_time = cfs_rq_clock_task(cfs_rq); |
7dc603c9 PZ |
782 | return; |
783 | } | |
784 | } | |
785 | ||
df217913 | 786 | attach_entity_cfs_rq(se); |
2b8c41da YD |
787 | } |
788 | ||
7dc603c9 | 789 | #else /* !CONFIG_SMP */ |
540247fb | 790 | void init_entity_runnable_average(struct sched_entity *se) |
a75cdaa9 AS |
791 | { |
792 | } | |
2b8c41da YD |
793 | void post_init_entity_util_avg(struct sched_entity *se) |
794 | { | |
795 | } | |
3d30544f PZ |
796 | static void update_tg_load_avg(struct cfs_rq *cfs_rq, int force) |
797 | { | |
798 | } | |
7dc603c9 | 799 | #endif /* CONFIG_SMP */ |
a75cdaa9 | 800 | |
bf0f6f24 | 801 | /* |
9dbdb155 | 802 | * Update the current task's runtime statistics. |
bf0f6f24 | 803 | */ |
b7cc0896 | 804 | static void update_curr(struct cfs_rq *cfs_rq) |
bf0f6f24 | 805 | { |
429d43bc | 806 | struct sched_entity *curr = cfs_rq->curr; |
78becc27 | 807 | u64 now = rq_clock_task(rq_of(cfs_rq)); |
9dbdb155 | 808 | u64 delta_exec; |
bf0f6f24 IM |
809 | |
810 | if (unlikely(!curr)) | |
811 | return; | |
812 | ||
9dbdb155 PZ |
813 | delta_exec = now - curr->exec_start; |
814 | if (unlikely((s64)delta_exec <= 0)) | |
34f28ecd | 815 | return; |
bf0f6f24 | 816 | |
8ebc91d9 | 817 | curr->exec_start = now; |
d842de87 | 818 | |
9dbdb155 PZ |
819 | schedstat_set(curr->statistics.exec_max, |
820 | max(delta_exec, curr->statistics.exec_max)); | |
821 | ||
822 | curr->sum_exec_runtime += delta_exec; | |
ae92882e | 823 | schedstat_add(cfs_rq->exec_clock, delta_exec); |
9dbdb155 PZ |
824 | |
825 | curr->vruntime += calc_delta_fair(delta_exec, curr); | |
826 | update_min_vruntime(cfs_rq); | |
827 | ||
d842de87 SV |
828 | if (entity_is_task(curr)) { |
829 | struct task_struct *curtask = task_of(curr); | |
830 | ||
f977bb49 | 831 | trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime); |
d2cc5ed6 | 832 | cgroup_account_cputime(curtask, delta_exec); |
f06febc9 | 833 | account_group_exec_runtime(curtask, delta_exec); |
d842de87 | 834 | } |
ec12cb7f PT |
835 | |
836 | account_cfs_rq_runtime(cfs_rq, delta_exec); | |
bf0f6f24 IM |
837 | } |
838 | ||
6e998916 SG |
839 | static void update_curr_fair(struct rq *rq) |
840 | { | |
841 | update_curr(cfs_rq_of(&rq->curr->se)); | |
842 | } | |
843 | ||
bf0f6f24 | 844 | static inline void |
5870db5b | 845 | update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 | 846 | { |
4fa8d299 JP |
847 | u64 wait_start, prev_wait_start; |
848 | ||
849 | if (!schedstat_enabled()) | |
850 | return; | |
851 | ||
852 | wait_start = rq_clock(rq_of(cfs_rq)); | |
853 | prev_wait_start = schedstat_val(se->statistics.wait_start); | |
3ea94de1 JP |
854 | |
855 | if (entity_is_task(se) && task_on_rq_migrating(task_of(se)) && | |
4fa8d299 JP |
856 | likely(wait_start > prev_wait_start)) |
857 | wait_start -= prev_wait_start; | |
3ea94de1 | 858 | |
2ed41a55 | 859 | __schedstat_set(se->statistics.wait_start, wait_start); |
bf0f6f24 IM |
860 | } |
861 | ||
4fa8d299 | 862 | static inline void |
3ea94de1 JP |
863 | update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se) |
864 | { | |
865 | struct task_struct *p; | |
cb251765 MG |
866 | u64 delta; |
867 | ||
4fa8d299 JP |
868 | if (!schedstat_enabled()) |
869 | return; | |
870 | ||
871 | delta = rq_clock(rq_of(cfs_rq)) - schedstat_val(se->statistics.wait_start); | |
3ea94de1 JP |
872 | |
873 | if (entity_is_task(se)) { | |
874 | p = task_of(se); | |
875 | if (task_on_rq_migrating(p)) { | |
876 | /* | |
877 | * Preserve migrating task's wait time so wait_start | |
878 | * time stamp can be adjusted to accumulate wait time | |
879 | * prior to migration. | |
880 | */ | |
2ed41a55 | 881 | __schedstat_set(se->statistics.wait_start, delta); |
3ea94de1 JP |
882 | return; |
883 | } | |
884 | trace_sched_stat_wait(p, delta); | |
885 | } | |
886 | ||
2ed41a55 | 887 | __schedstat_set(se->statistics.wait_max, |
4fa8d299 | 888 | max(schedstat_val(se->statistics.wait_max), delta)); |
2ed41a55 PZ |
889 | __schedstat_inc(se->statistics.wait_count); |
890 | __schedstat_add(se->statistics.wait_sum, delta); | |
891 | __schedstat_set(se->statistics.wait_start, 0); | |
3ea94de1 | 892 | } |
3ea94de1 | 893 | |
4fa8d299 | 894 | static inline void |
1a3d027c JP |
895 | update_stats_enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se) |
896 | { | |
897 | struct task_struct *tsk = NULL; | |
4fa8d299 JP |
898 | u64 sleep_start, block_start; |
899 | ||
900 | if (!schedstat_enabled()) | |
901 | return; | |
902 | ||
903 | sleep_start = schedstat_val(se->statistics.sleep_start); | |
904 | block_start = schedstat_val(se->statistics.block_start); | |
1a3d027c JP |
905 | |
906 | if (entity_is_task(se)) | |
907 | tsk = task_of(se); | |
908 | ||
4fa8d299 JP |
909 | if (sleep_start) { |
910 | u64 delta = rq_clock(rq_of(cfs_rq)) - sleep_start; | |
1a3d027c JP |
911 | |
912 | if ((s64)delta < 0) | |
913 | delta = 0; | |
914 | ||
4fa8d299 | 915 | if (unlikely(delta > schedstat_val(se->statistics.sleep_max))) |
2ed41a55 | 916 | __schedstat_set(se->statistics.sleep_max, delta); |
1a3d027c | 917 | |
2ed41a55 PZ |
918 | __schedstat_set(se->statistics.sleep_start, 0); |
919 | __schedstat_add(se->statistics.sum_sleep_runtime, delta); | |
1a3d027c JP |
920 | |
921 | if (tsk) { | |
922 | account_scheduler_latency(tsk, delta >> 10, 1); | |
923 | trace_sched_stat_sleep(tsk, delta); | |
924 | } | |
925 | } | |
4fa8d299 JP |
926 | if (block_start) { |
927 | u64 delta = rq_clock(rq_of(cfs_rq)) - block_start; | |
1a3d027c JP |
928 | |
929 | if ((s64)delta < 0) | |
930 | delta = 0; | |
931 | ||
4fa8d299 | 932 | if (unlikely(delta > schedstat_val(se->statistics.block_max))) |
2ed41a55 | 933 | __schedstat_set(se->statistics.block_max, delta); |
1a3d027c | 934 | |
2ed41a55 PZ |
935 | __schedstat_set(se->statistics.block_start, 0); |
936 | __schedstat_add(se->statistics.sum_sleep_runtime, delta); | |
1a3d027c JP |
937 | |
938 | if (tsk) { | |
939 | if (tsk->in_iowait) { | |
2ed41a55 PZ |
940 | __schedstat_add(se->statistics.iowait_sum, delta); |
941 | __schedstat_inc(se->statistics.iowait_count); | |
1a3d027c JP |
942 | trace_sched_stat_iowait(tsk, delta); |
943 | } | |
944 | ||
945 | trace_sched_stat_blocked(tsk, delta); | |
946 | ||
947 | /* | |
948 | * Blocking time is in units of nanosecs, so shift by | |
949 | * 20 to get a milliseconds-range estimation of the | |
950 | * amount of time that the task spent sleeping: | |
951 | */ | |
952 | if (unlikely(prof_on == SLEEP_PROFILING)) { | |
953 | profile_hits(SLEEP_PROFILING, | |
954 | (void *)get_wchan(tsk), | |
955 | delta >> 20); | |
956 | } | |
957 | account_scheduler_latency(tsk, delta >> 10, 0); | |
958 | } | |
959 | } | |
3ea94de1 | 960 | } |
3ea94de1 | 961 | |
bf0f6f24 IM |
962 | /* |
963 | * Task is being enqueued - update stats: | |
964 | */ | |
cb251765 | 965 | static inline void |
1a3d027c | 966 | update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
bf0f6f24 | 967 | { |
4fa8d299 JP |
968 | if (!schedstat_enabled()) |
969 | return; | |
970 | ||
bf0f6f24 IM |
971 | /* |
972 | * Are we enqueueing a waiting task? (for current tasks | |
973 | * a dequeue/enqueue event is a NOP) | |
974 | */ | |
429d43bc | 975 | if (se != cfs_rq->curr) |
5870db5b | 976 | update_stats_wait_start(cfs_rq, se); |
1a3d027c JP |
977 | |
978 | if (flags & ENQUEUE_WAKEUP) | |
979 | update_stats_enqueue_sleeper(cfs_rq, se); | |
bf0f6f24 IM |
980 | } |
981 | ||
bf0f6f24 | 982 | static inline void |
cb251765 | 983 | update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
bf0f6f24 | 984 | { |
4fa8d299 JP |
985 | |
986 | if (!schedstat_enabled()) | |
987 | return; | |
988 | ||
bf0f6f24 IM |
989 | /* |
990 | * Mark the end of the wait period if dequeueing a | |
991 | * waiting task: | |
992 | */ | |
429d43bc | 993 | if (se != cfs_rq->curr) |
9ef0a961 | 994 | update_stats_wait_end(cfs_rq, se); |
cb251765 | 995 | |
4fa8d299 JP |
996 | if ((flags & DEQUEUE_SLEEP) && entity_is_task(se)) { |
997 | struct task_struct *tsk = task_of(se); | |
cb251765 | 998 | |
4fa8d299 | 999 | if (tsk->state & TASK_INTERRUPTIBLE) |
2ed41a55 | 1000 | __schedstat_set(se->statistics.sleep_start, |
4fa8d299 JP |
1001 | rq_clock(rq_of(cfs_rq))); |
1002 | if (tsk->state & TASK_UNINTERRUPTIBLE) | |
2ed41a55 | 1003 | __schedstat_set(se->statistics.block_start, |
4fa8d299 | 1004 | rq_clock(rq_of(cfs_rq))); |
cb251765 | 1005 | } |
cb251765 MG |
1006 | } |
1007 | ||
bf0f6f24 IM |
1008 | /* |
1009 | * We are picking a new current task - update its stats: | |
1010 | */ | |
1011 | static inline void | |
79303e9e | 1012 | update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 IM |
1013 | { |
1014 | /* | |
1015 | * We are starting a new run period: | |
1016 | */ | |
78becc27 | 1017 | se->exec_start = rq_clock_task(rq_of(cfs_rq)); |
bf0f6f24 IM |
1018 | } |
1019 | ||
bf0f6f24 IM |
1020 | /************************************************** |
1021 | * Scheduling class queueing methods: | |
1022 | */ | |
1023 | ||
cbee9f88 PZ |
1024 | #ifdef CONFIG_NUMA_BALANCING |
1025 | /* | |
598f0ec0 MG |
1026 | * Approximate time to scan a full NUMA task in ms. The task scan period is |
1027 | * calculated based on the tasks virtual memory size and | |
1028 | * numa_balancing_scan_size. | |
cbee9f88 | 1029 | */ |
598f0ec0 MG |
1030 | unsigned int sysctl_numa_balancing_scan_period_min = 1000; |
1031 | unsigned int sysctl_numa_balancing_scan_period_max = 60000; | |
6e5fb223 PZ |
1032 | |
1033 | /* Portion of address space to scan in MB */ | |
1034 | unsigned int sysctl_numa_balancing_scan_size = 256; | |
cbee9f88 | 1035 | |
4b96a29b PZ |
1036 | /* Scan @scan_size MB every @scan_period after an initial @scan_delay in ms */ |
1037 | unsigned int sysctl_numa_balancing_scan_delay = 1000; | |
1038 | ||
b5dd77c8 RR |
1039 | struct numa_group { |
1040 | atomic_t refcount; | |
1041 | ||
1042 | spinlock_t lock; /* nr_tasks, tasks */ | |
1043 | int nr_tasks; | |
1044 | pid_t gid; | |
1045 | int active_nodes; | |
1046 | ||
1047 | struct rcu_head rcu; | |
1048 | unsigned long total_faults; | |
1049 | unsigned long max_faults_cpu; | |
1050 | /* | |
1051 | * Faults_cpu is used to decide whether memory should move | |
1052 | * towards the CPU. As a consequence, these stats are weighted | |
1053 | * more by CPU use than by memory faults. | |
1054 | */ | |
1055 | unsigned long *faults_cpu; | |
1056 | unsigned long faults[0]; | |
1057 | }; | |
1058 | ||
1059 | static inline unsigned long group_faults_priv(struct numa_group *ng); | |
1060 | static inline unsigned long group_faults_shared(struct numa_group *ng); | |
1061 | ||
598f0ec0 MG |
1062 | static unsigned int task_nr_scan_windows(struct task_struct *p) |
1063 | { | |
1064 | unsigned long rss = 0; | |
1065 | unsigned long nr_scan_pages; | |
1066 | ||
1067 | /* | |
1068 | * Calculations based on RSS as non-present and empty pages are skipped | |
1069 | * by the PTE scanner and NUMA hinting faults should be trapped based | |
1070 | * on resident pages | |
1071 | */ | |
1072 | nr_scan_pages = sysctl_numa_balancing_scan_size << (20 - PAGE_SHIFT); | |
1073 | rss = get_mm_rss(p->mm); | |
1074 | if (!rss) | |
1075 | rss = nr_scan_pages; | |
1076 | ||
1077 | rss = round_up(rss, nr_scan_pages); | |
1078 | return rss / nr_scan_pages; | |
1079 | } | |
1080 | ||
1081 | /* For sanitys sake, never scan more PTEs than MAX_SCAN_WINDOW MB/sec. */ | |
1082 | #define MAX_SCAN_WINDOW 2560 | |
1083 | ||
1084 | static unsigned int task_scan_min(struct task_struct *p) | |
1085 | { | |
316c1608 | 1086 | unsigned int scan_size = READ_ONCE(sysctl_numa_balancing_scan_size); |
598f0ec0 MG |
1087 | unsigned int scan, floor; |
1088 | unsigned int windows = 1; | |
1089 | ||
64192658 KT |
1090 | if (scan_size < MAX_SCAN_WINDOW) |
1091 | windows = MAX_SCAN_WINDOW / scan_size; | |
598f0ec0 MG |
1092 | floor = 1000 / windows; |
1093 | ||
1094 | scan = sysctl_numa_balancing_scan_period_min / task_nr_scan_windows(p); | |
1095 | return max_t(unsigned int, floor, scan); | |
1096 | } | |
1097 | ||
b5dd77c8 RR |
1098 | static unsigned int task_scan_start(struct task_struct *p) |
1099 | { | |
1100 | unsigned long smin = task_scan_min(p); | |
1101 | unsigned long period = smin; | |
1102 | ||
1103 | /* Scale the maximum scan period with the amount of shared memory. */ | |
1104 | if (p->numa_group) { | |
1105 | struct numa_group *ng = p->numa_group; | |
1106 | unsigned long shared = group_faults_shared(ng); | |
1107 | unsigned long private = group_faults_priv(ng); | |
1108 | ||
1109 | period *= atomic_read(&ng->refcount); | |
1110 | period *= shared + 1; | |
1111 | period /= private + shared + 1; | |
1112 | } | |
1113 | ||
1114 | return max(smin, period); | |
1115 | } | |
1116 | ||
598f0ec0 MG |
1117 | static unsigned int task_scan_max(struct task_struct *p) |
1118 | { | |
b5dd77c8 RR |
1119 | unsigned long smin = task_scan_min(p); |
1120 | unsigned long smax; | |
598f0ec0 MG |
1121 | |
1122 | /* Watch for min being lower than max due to floor calculations */ | |
1123 | smax = sysctl_numa_balancing_scan_period_max / task_nr_scan_windows(p); | |
b5dd77c8 RR |
1124 | |
1125 | /* Scale the maximum scan period with the amount of shared memory. */ | |
1126 | if (p->numa_group) { | |
1127 | struct numa_group *ng = p->numa_group; | |
1128 | unsigned long shared = group_faults_shared(ng); | |
1129 | unsigned long private = group_faults_priv(ng); | |
1130 | unsigned long period = smax; | |
1131 | ||
1132 | period *= atomic_read(&ng->refcount); | |
1133 | period *= shared + 1; | |
1134 | period /= private + shared + 1; | |
1135 | ||
1136 | smax = max(smax, period); | |
1137 | } | |
1138 | ||
598f0ec0 MG |
1139 | return max(smin, smax); |
1140 | } | |
1141 | ||
0ec8aa00 PZ |
1142 | static void account_numa_enqueue(struct rq *rq, struct task_struct *p) |
1143 | { | |
1144 | rq->nr_numa_running += (p->numa_preferred_nid != -1); | |
1145 | rq->nr_preferred_running += (p->numa_preferred_nid == task_node(p)); | |
1146 | } | |
1147 | ||
1148 | static void account_numa_dequeue(struct rq *rq, struct task_struct *p) | |
1149 | { | |
1150 | rq->nr_numa_running -= (p->numa_preferred_nid != -1); | |
1151 | rq->nr_preferred_running -= (p->numa_preferred_nid == task_node(p)); | |
1152 | } | |
1153 | ||
be1e4e76 RR |
1154 | /* Shared or private faults. */ |
1155 | #define NR_NUMA_HINT_FAULT_TYPES 2 | |
1156 | ||
1157 | /* Memory and CPU locality */ | |
1158 | #define NR_NUMA_HINT_FAULT_STATS (NR_NUMA_HINT_FAULT_TYPES * 2) | |
1159 | ||
1160 | /* Averaged statistics, and temporary buffers. */ | |
1161 | #define NR_NUMA_HINT_FAULT_BUCKETS (NR_NUMA_HINT_FAULT_STATS * 2) | |
1162 | ||
e29cf08b MG |
1163 | pid_t task_numa_group_id(struct task_struct *p) |
1164 | { | |
1165 | return p->numa_group ? p->numa_group->gid : 0; | |
1166 | } | |
1167 | ||
44dba3d5 | 1168 | /* |
97fb7a0a | 1169 | * The averaged statistics, shared & private, memory & CPU, |
44dba3d5 IM |
1170 | * occupy the first half of the array. The second half of the |
1171 | * array is for current counters, which are averaged into the | |
1172 | * first set by task_numa_placement. | |
1173 | */ | |
1174 | static inline int task_faults_idx(enum numa_faults_stats s, int nid, int priv) | |
ac8e895b | 1175 | { |
44dba3d5 | 1176 | return NR_NUMA_HINT_FAULT_TYPES * (s * nr_node_ids + nid) + priv; |
ac8e895b MG |
1177 | } |
1178 | ||
1179 | static inline unsigned long task_faults(struct task_struct *p, int nid) | |
1180 | { | |
44dba3d5 | 1181 | if (!p->numa_faults) |
ac8e895b MG |
1182 | return 0; |
1183 | ||
44dba3d5 IM |
1184 | return p->numa_faults[task_faults_idx(NUMA_MEM, nid, 0)] + |
1185 | p->numa_faults[task_faults_idx(NUMA_MEM, nid, 1)]; | |
ac8e895b MG |
1186 | } |
1187 | ||
83e1d2cd MG |
1188 | static inline unsigned long group_faults(struct task_struct *p, int nid) |
1189 | { | |
1190 | if (!p->numa_group) | |
1191 | return 0; | |
1192 | ||
44dba3d5 IM |
1193 | return p->numa_group->faults[task_faults_idx(NUMA_MEM, nid, 0)] + |
1194 | p->numa_group->faults[task_faults_idx(NUMA_MEM, nid, 1)]; | |
83e1d2cd MG |
1195 | } |
1196 | ||
20e07dea RR |
1197 | static inline unsigned long group_faults_cpu(struct numa_group *group, int nid) |
1198 | { | |
44dba3d5 IM |
1199 | return group->faults_cpu[task_faults_idx(NUMA_MEM, nid, 0)] + |
1200 | group->faults_cpu[task_faults_idx(NUMA_MEM, nid, 1)]; | |
20e07dea RR |
1201 | } |
1202 | ||
b5dd77c8 RR |
1203 | static inline unsigned long group_faults_priv(struct numa_group *ng) |
1204 | { | |
1205 | unsigned long faults = 0; | |
1206 | int node; | |
1207 | ||
1208 | for_each_online_node(node) { | |
1209 | faults += ng->faults[task_faults_idx(NUMA_MEM, node, 1)]; | |
1210 | } | |
1211 | ||
1212 | return faults; | |
1213 | } | |
1214 | ||
1215 | static inline unsigned long group_faults_shared(struct numa_group *ng) | |
1216 | { | |
1217 | unsigned long faults = 0; | |
1218 | int node; | |
1219 | ||
1220 | for_each_online_node(node) { | |
1221 | faults += ng->faults[task_faults_idx(NUMA_MEM, node, 0)]; | |
1222 | } | |
1223 | ||
1224 | return faults; | |
1225 | } | |
1226 | ||
4142c3eb RR |
1227 | /* |
1228 | * A node triggering more than 1/3 as many NUMA faults as the maximum is | |
1229 | * considered part of a numa group's pseudo-interleaving set. Migrations | |
1230 | * between these nodes are slowed down, to allow things to settle down. | |
1231 | */ | |
1232 | #define ACTIVE_NODE_FRACTION 3 | |
1233 | ||
1234 | static bool numa_is_active_node(int nid, struct numa_group *ng) | |
1235 | { | |
1236 | return group_faults_cpu(ng, nid) * ACTIVE_NODE_FRACTION > ng->max_faults_cpu; | |
1237 | } | |
1238 | ||
6c6b1193 RR |
1239 | /* Handle placement on systems where not all nodes are directly connected. */ |
1240 | static unsigned long score_nearby_nodes(struct task_struct *p, int nid, | |
1241 | int maxdist, bool task) | |
1242 | { | |
1243 | unsigned long score = 0; | |
1244 | int node; | |
1245 | ||
1246 | /* | |
1247 | * All nodes are directly connected, and the same distance | |
1248 | * from each other. No need for fancy placement algorithms. | |
1249 | */ | |
1250 | if (sched_numa_topology_type == NUMA_DIRECT) | |
1251 | return 0; | |
1252 | ||
1253 | /* | |
1254 | * This code is called for each node, introducing N^2 complexity, | |
1255 | * which should be ok given the number of nodes rarely exceeds 8. | |
1256 | */ | |
1257 | for_each_online_node(node) { | |
1258 | unsigned long faults; | |
1259 | int dist = node_distance(nid, node); | |
1260 | ||
1261 | /* | |
1262 | * The furthest away nodes in the system are not interesting | |
1263 | * for placement; nid was already counted. | |
1264 | */ | |
1265 | if (dist == sched_max_numa_distance || node == nid) | |
1266 | continue; | |
1267 | ||
1268 | /* | |
1269 | * On systems with a backplane NUMA topology, compare groups | |
1270 | * of nodes, and move tasks towards the group with the most | |
1271 | * memory accesses. When comparing two nodes at distance | |
1272 | * "hoplimit", only nodes closer by than "hoplimit" are part | |
1273 | * of each group. Skip other nodes. | |
1274 | */ | |
1275 | if (sched_numa_topology_type == NUMA_BACKPLANE && | |
1276 | dist > maxdist) | |
1277 | continue; | |
1278 | ||
1279 | /* Add up the faults from nearby nodes. */ | |
1280 | if (task) | |
1281 | faults = task_faults(p, node); | |
1282 | else | |
1283 | faults = group_faults(p, node); | |
1284 | ||
1285 | /* | |
1286 | * On systems with a glueless mesh NUMA topology, there are | |
1287 | * no fixed "groups of nodes". Instead, nodes that are not | |
1288 | * directly connected bounce traffic through intermediate | |
1289 | * nodes; a numa_group can occupy any set of nodes. | |
1290 | * The further away a node is, the less the faults count. | |
1291 | * This seems to result in good task placement. | |
1292 | */ | |
1293 | if (sched_numa_topology_type == NUMA_GLUELESS_MESH) { | |
1294 | faults *= (sched_max_numa_distance - dist); | |
1295 | faults /= (sched_max_numa_distance - LOCAL_DISTANCE); | |
1296 | } | |
1297 | ||
1298 | score += faults; | |
1299 | } | |
1300 | ||
1301 | return score; | |
1302 | } | |
1303 | ||
83e1d2cd MG |
1304 | /* |
1305 | * These return the fraction of accesses done by a particular task, or | |
1306 | * task group, on a particular numa node. The group weight is given a | |
1307 | * larger multiplier, in order to group tasks together that are almost | |
1308 | * evenly spread out between numa nodes. | |
1309 | */ | |
7bd95320 RR |
1310 | static inline unsigned long task_weight(struct task_struct *p, int nid, |
1311 | int dist) | |
83e1d2cd | 1312 | { |
7bd95320 | 1313 | unsigned long faults, total_faults; |
83e1d2cd | 1314 | |
44dba3d5 | 1315 | if (!p->numa_faults) |
83e1d2cd MG |
1316 | return 0; |
1317 | ||
1318 | total_faults = p->total_numa_faults; | |
1319 | ||
1320 | if (!total_faults) | |
1321 | return 0; | |
1322 | ||
7bd95320 | 1323 | faults = task_faults(p, nid); |
6c6b1193 RR |
1324 | faults += score_nearby_nodes(p, nid, dist, true); |
1325 | ||
7bd95320 | 1326 | return 1000 * faults / total_faults; |
83e1d2cd MG |
1327 | } |
1328 | ||
7bd95320 RR |
1329 | static inline unsigned long group_weight(struct task_struct *p, int nid, |
1330 | int dist) | |
83e1d2cd | 1331 | { |
7bd95320 RR |
1332 | unsigned long faults, total_faults; |
1333 | ||
1334 | if (!p->numa_group) | |
1335 | return 0; | |
1336 | ||
1337 | total_faults = p->numa_group->total_faults; | |
1338 | ||
1339 | if (!total_faults) | |
83e1d2cd MG |
1340 | return 0; |
1341 | ||
7bd95320 | 1342 | faults = group_faults(p, nid); |
6c6b1193 RR |
1343 | faults += score_nearby_nodes(p, nid, dist, false); |
1344 | ||
7bd95320 | 1345 | return 1000 * faults / total_faults; |
83e1d2cd MG |
1346 | } |
1347 | ||
10f39042 RR |
1348 | bool should_numa_migrate_memory(struct task_struct *p, struct page * page, |
1349 | int src_nid, int dst_cpu) | |
1350 | { | |
1351 | struct numa_group *ng = p->numa_group; | |
1352 | int dst_nid = cpu_to_node(dst_cpu); | |
1353 | int last_cpupid, this_cpupid; | |
1354 | ||
1355 | this_cpupid = cpu_pid_to_cpupid(dst_cpu, current->pid); | |
1356 | ||
1357 | /* | |
1358 | * Multi-stage node selection is used in conjunction with a periodic | |
1359 | * migration fault to build a temporal task<->page relation. By using | |
1360 | * a two-stage filter we remove short/unlikely relations. | |
1361 | * | |
1362 | * Using P(p) ~ n_p / n_t as per frequentist probability, we can equate | |
1363 | * a task's usage of a particular page (n_p) per total usage of this | |
1364 | * page (n_t) (in a given time-span) to a probability. | |
1365 | * | |
1366 | * Our periodic faults will sample this probability and getting the | |
1367 | * same result twice in a row, given these samples are fully | |
1368 | * independent, is then given by P(n)^2, provided our sample period | |
1369 | * is sufficiently short compared to the usage pattern. | |
1370 | * | |
1371 | * This quadric squishes small probabilities, making it less likely we | |
1372 | * act on an unlikely task<->page relation. | |
1373 | */ | |
1374 | last_cpupid = page_cpupid_xchg_last(page, this_cpupid); | |
1375 | if (!cpupid_pid_unset(last_cpupid) && | |
1376 | cpupid_to_nid(last_cpupid) != dst_nid) | |
1377 | return false; | |
1378 | ||
1379 | /* Always allow migrate on private faults */ | |
1380 | if (cpupid_match_pid(p, last_cpupid)) | |
1381 | return true; | |
1382 | ||
1383 | /* A shared fault, but p->numa_group has not been set up yet. */ | |
1384 | if (!ng) | |
1385 | return true; | |
1386 | ||
1387 | /* | |
4142c3eb RR |
1388 | * Destination node is much more heavily used than the source |
1389 | * node? Allow migration. | |
10f39042 | 1390 | */ |
4142c3eb RR |
1391 | if (group_faults_cpu(ng, dst_nid) > group_faults_cpu(ng, src_nid) * |
1392 | ACTIVE_NODE_FRACTION) | |
10f39042 RR |
1393 | return true; |
1394 | ||
1395 | /* | |
4142c3eb RR |
1396 | * Distribute memory according to CPU & memory use on each node, |
1397 | * with 3/4 hysteresis to avoid unnecessary memory migrations: | |
1398 | * | |
1399 | * faults_cpu(dst) 3 faults_cpu(src) | |
1400 | * --------------- * - > --------------- | |
1401 | * faults_mem(dst) 4 faults_mem(src) | |
10f39042 | 1402 | */ |
4142c3eb RR |
1403 | return group_faults_cpu(ng, dst_nid) * group_faults(p, src_nid) * 3 > |
1404 | group_faults_cpu(ng, src_nid) * group_faults(p, dst_nid) * 4; | |
10f39042 RR |
1405 | } |
1406 | ||
c7132dd6 | 1407 | static unsigned long weighted_cpuload(struct rq *rq); |
58d081b5 MG |
1408 | static unsigned long source_load(int cpu, int type); |
1409 | static unsigned long target_load(int cpu, int type); | |
ced549fa | 1410 | static unsigned long capacity_of(int cpu); |
58d081b5 | 1411 | |
fb13c7ee | 1412 | /* Cached statistics for all CPUs within a node */ |
58d081b5 | 1413 | struct numa_stats { |
fb13c7ee | 1414 | unsigned long nr_running; |
58d081b5 | 1415 | unsigned long load; |
fb13c7ee MG |
1416 | |
1417 | /* Total compute capacity of CPUs on a node */ | |
5ef20ca1 | 1418 | unsigned long compute_capacity; |
fb13c7ee MG |
1419 | |
1420 | /* Approximate capacity in terms of runnable tasks on a node */ | |
5ef20ca1 | 1421 | unsigned long task_capacity; |
1b6a7495 | 1422 | int has_free_capacity; |
58d081b5 | 1423 | }; |
e6628d5b | 1424 | |
fb13c7ee MG |
1425 | /* |
1426 | * XXX borrowed from update_sg_lb_stats | |
1427 | */ | |
1428 | static void update_numa_stats(struct numa_stats *ns, int nid) | |
1429 | { | |
83d7f242 RR |
1430 | int smt, cpu, cpus = 0; |
1431 | unsigned long capacity; | |
fb13c7ee MG |
1432 | |
1433 | memset(ns, 0, sizeof(*ns)); | |
1434 | for_each_cpu(cpu, cpumask_of_node(nid)) { | |
1435 | struct rq *rq = cpu_rq(cpu); | |
1436 | ||
1437 | ns->nr_running += rq->nr_running; | |
c7132dd6 | 1438 | ns->load += weighted_cpuload(rq); |
ced549fa | 1439 | ns->compute_capacity += capacity_of(cpu); |
5eca82a9 PZ |
1440 | |
1441 | cpus++; | |
fb13c7ee MG |
1442 | } |
1443 | ||
5eca82a9 PZ |
1444 | /* |
1445 | * If we raced with hotplug and there are no CPUs left in our mask | |
1446 | * the @ns structure is NULL'ed and task_numa_compare() will | |
1447 | * not find this node attractive. | |
1448 | * | |
1b6a7495 NP |
1449 | * We'll either bail at !has_free_capacity, or we'll detect a huge |
1450 | * imbalance and bail there. | |
5eca82a9 PZ |
1451 | */ |
1452 | if (!cpus) | |
1453 | return; | |
1454 | ||
83d7f242 RR |
1455 | /* smt := ceil(cpus / capacity), assumes: 1 < smt_power < 2 */ |
1456 | smt = DIV_ROUND_UP(SCHED_CAPACITY_SCALE * cpus, ns->compute_capacity); | |
1457 | capacity = cpus / smt; /* cores */ | |
1458 | ||
1459 | ns->task_capacity = min_t(unsigned, capacity, | |
1460 | DIV_ROUND_CLOSEST(ns->compute_capacity, SCHED_CAPACITY_SCALE)); | |
1b6a7495 | 1461 | ns->has_free_capacity = (ns->nr_running < ns->task_capacity); |
fb13c7ee MG |
1462 | } |
1463 | ||
58d081b5 MG |
1464 | struct task_numa_env { |
1465 | struct task_struct *p; | |
e6628d5b | 1466 | |
58d081b5 MG |
1467 | int src_cpu, src_nid; |
1468 | int dst_cpu, dst_nid; | |
e6628d5b | 1469 | |
58d081b5 | 1470 | struct numa_stats src_stats, dst_stats; |
e6628d5b | 1471 | |
40ea2b42 | 1472 | int imbalance_pct; |
7bd95320 | 1473 | int dist; |
fb13c7ee MG |
1474 | |
1475 | struct task_struct *best_task; | |
1476 | long best_imp; | |
58d081b5 MG |
1477 | int best_cpu; |
1478 | }; | |
1479 | ||
fb13c7ee MG |
1480 | static void task_numa_assign(struct task_numa_env *env, |
1481 | struct task_struct *p, long imp) | |
1482 | { | |
1483 | if (env->best_task) | |
1484 | put_task_struct(env->best_task); | |
bac78573 ON |
1485 | if (p) |
1486 | get_task_struct(p); | |
fb13c7ee MG |
1487 | |
1488 | env->best_task = p; | |
1489 | env->best_imp = imp; | |
1490 | env->best_cpu = env->dst_cpu; | |
1491 | } | |
1492 | ||
28a21745 | 1493 | static bool load_too_imbalanced(long src_load, long dst_load, |
e63da036 RR |
1494 | struct task_numa_env *env) |
1495 | { | |
e4991b24 RR |
1496 | long imb, old_imb; |
1497 | long orig_src_load, orig_dst_load; | |
28a21745 RR |
1498 | long src_capacity, dst_capacity; |
1499 | ||
1500 | /* | |
1501 | * The load is corrected for the CPU capacity available on each node. | |
1502 | * | |
1503 | * src_load dst_load | |
1504 | * ------------ vs --------- | |
1505 | * src_capacity dst_capacity | |
1506 | */ | |
1507 | src_capacity = env->src_stats.compute_capacity; | |
1508 | dst_capacity = env->dst_stats.compute_capacity; | |
e63da036 RR |
1509 | |
1510 | /* We care about the slope of the imbalance, not the direction. */ | |
e4991b24 RR |
1511 | if (dst_load < src_load) |
1512 | swap(dst_load, src_load); | |
e63da036 RR |
1513 | |
1514 | /* Is the difference below the threshold? */ | |
e4991b24 RR |
1515 | imb = dst_load * src_capacity * 100 - |
1516 | src_load * dst_capacity * env->imbalance_pct; | |
e63da036 RR |
1517 | if (imb <= 0) |
1518 | return false; | |
1519 | ||
1520 | /* | |
1521 | * The imbalance is above the allowed threshold. | |
e4991b24 | 1522 | * Compare it with the old imbalance. |
e63da036 | 1523 | */ |
28a21745 | 1524 | orig_src_load = env->src_stats.load; |
e4991b24 | 1525 | orig_dst_load = env->dst_stats.load; |
28a21745 | 1526 | |
e4991b24 RR |
1527 | if (orig_dst_load < orig_src_load) |
1528 | swap(orig_dst_load, orig_src_load); | |
e63da036 | 1529 | |
e4991b24 RR |
1530 | old_imb = orig_dst_load * src_capacity * 100 - |
1531 | orig_src_load * dst_capacity * env->imbalance_pct; | |
1532 | ||
1533 | /* Would this change make things worse? */ | |
1534 | return (imb > old_imb); | |
e63da036 RR |
1535 | } |
1536 | ||
fb13c7ee MG |
1537 | /* |
1538 | * This checks if the overall compute and NUMA accesses of the system would | |
1539 | * be improved if the source tasks was migrated to the target dst_cpu taking | |
1540 | * into account that it might be best if task running on the dst_cpu should | |
1541 | * be exchanged with the source task | |
1542 | */ | |
887c290e RR |
1543 | static void task_numa_compare(struct task_numa_env *env, |
1544 | long taskimp, long groupimp) | |
fb13c7ee MG |
1545 | { |
1546 | struct rq *src_rq = cpu_rq(env->src_cpu); | |
1547 | struct rq *dst_rq = cpu_rq(env->dst_cpu); | |
1548 | struct task_struct *cur; | |
28a21745 | 1549 | long src_load, dst_load; |
fb13c7ee | 1550 | long load; |
1c5d3eb3 | 1551 | long imp = env->p->numa_group ? groupimp : taskimp; |
0132c3e1 | 1552 | long moveimp = imp; |
7bd95320 | 1553 | int dist = env->dist; |
fb13c7ee MG |
1554 | |
1555 | rcu_read_lock(); | |
bac78573 ON |
1556 | cur = task_rcu_dereference(&dst_rq->curr); |
1557 | if (cur && ((cur->flags & PF_EXITING) || is_idle_task(cur))) | |
fb13c7ee MG |
1558 | cur = NULL; |
1559 | ||
7af68335 PZ |
1560 | /* |
1561 | * Because we have preemption enabled we can get migrated around and | |
1562 | * end try selecting ourselves (current == env->p) as a swap candidate. | |
1563 | */ | |
1564 | if (cur == env->p) | |
1565 | goto unlock; | |
1566 | ||
fb13c7ee MG |
1567 | /* |
1568 | * "imp" is the fault differential for the source task between the | |
1569 | * source and destination node. Calculate the total differential for | |
1570 | * the source task and potential destination task. The more negative | |
1571 | * the value is, the more rmeote accesses that would be expected to | |
1572 | * be incurred if the tasks were swapped. | |
1573 | */ | |
1574 | if (cur) { | |
97fb7a0a | 1575 | /* Skip this swap candidate if cannot move to the source CPU: */ |
0c98d344 | 1576 | if (!cpumask_test_cpu(env->src_cpu, &cur->cpus_allowed)) |
fb13c7ee MG |
1577 | goto unlock; |
1578 | ||
887c290e RR |
1579 | /* |
1580 | * If dst and source tasks are in the same NUMA group, or not | |
ca28aa53 | 1581 | * in any group then look only at task weights. |
887c290e | 1582 | */ |
ca28aa53 | 1583 | if (cur->numa_group == env->p->numa_group) { |
7bd95320 RR |
1584 | imp = taskimp + task_weight(cur, env->src_nid, dist) - |
1585 | task_weight(cur, env->dst_nid, dist); | |
ca28aa53 RR |
1586 | /* |
1587 | * Add some hysteresis to prevent swapping the | |
1588 | * tasks within a group over tiny differences. | |
1589 | */ | |
1590 | if (cur->numa_group) | |
1591 | imp -= imp/16; | |
887c290e | 1592 | } else { |
ca28aa53 RR |
1593 | /* |
1594 | * Compare the group weights. If a task is all by | |
1595 | * itself (not part of a group), use the task weight | |
1596 | * instead. | |
1597 | */ | |
ca28aa53 | 1598 | if (cur->numa_group) |
7bd95320 RR |
1599 | imp += group_weight(cur, env->src_nid, dist) - |
1600 | group_weight(cur, env->dst_nid, dist); | |
ca28aa53 | 1601 | else |
7bd95320 RR |
1602 | imp += task_weight(cur, env->src_nid, dist) - |
1603 | task_weight(cur, env->dst_nid, dist); | |
887c290e | 1604 | } |
fb13c7ee MG |
1605 | } |
1606 | ||
0132c3e1 | 1607 | if (imp <= env->best_imp && moveimp <= env->best_imp) |
fb13c7ee MG |
1608 | goto unlock; |
1609 | ||
1610 | if (!cur) { | |
1611 | /* Is there capacity at our destination? */ | |
b932c03c | 1612 | if (env->src_stats.nr_running <= env->src_stats.task_capacity && |
1b6a7495 | 1613 | !env->dst_stats.has_free_capacity) |
fb13c7ee MG |
1614 | goto unlock; |
1615 | ||
1616 | goto balance; | |
1617 | } | |
1618 | ||
97fb7a0a | 1619 | /* Balance doesn't matter much if we're running a task per CPU: */ |
0132c3e1 RR |
1620 | if (imp > env->best_imp && src_rq->nr_running == 1 && |
1621 | dst_rq->nr_running == 1) | |
fb13c7ee MG |
1622 | goto assign; |
1623 | ||
1624 | /* | |
1625 | * In the overloaded case, try and keep the load balanced. | |
1626 | */ | |
1627 | balance: | |
e720fff6 PZ |
1628 | load = task_h_load(env->p); |
1629 | dst_load = env->dst_stats.load + load; | |
1630 | src_load = env->src_stats.load - load; | |
fb13c7ee | 1631 | |
0132c3e1 RR |
1632 | if (moveimp > imp && moveimp > env->best_imp) { |
1633 | /* | |
1634 | * If the improvement from just moving env->p direction is | |
1635 | * better than swapping tasks around, check if a move is | |
1636 | * possible. Store a slightly smaller score than moveimp, | |
1637 | * so an actually idle CPU will win. | |
1638 | */ | |
1639 | if (!load_too_imbalanced(src_load, dst_load, env)) { | |
1640 | imp = moveimp - 1; | |
1641 | cur = NULL; | |
1642 | goto assign; | |
1643 | } | |
1644 | } | |
1645 | ||
1646 | if (imp <= env->best_imp) | |
1647 | goto unlock; | |
1648 | ||
fb13c7ee | 1649 | if (cur) { |
e720fff6 PZ |
1650 | load = task_h_load(cur); |
1651 | dst_load -= load; | |
1652 | src_load += load; | |
fb13c7ee MG |
1653 | } |
1654 | ||
28a21745 | 1655 | if (load_too_imbalanced(src_load, dst_load, env)) |
fb13c7ee MG |
1656 | goto unlock; |
1657 | ||
ba7e5a27 RR |
1658 | /* |
1659 | * One idle CPU per node is evaluated for a task numa move. | |
1660 | * Call select_idle_sibling to maybe find a better one. | |
1661 | */ | |
10e2f1ac PZ |
1662 | if (!cur) { |
1663 | /* | |
97fb7a0a | 1664 | * select_idle_siblings() uses an per-CPU cpumask that |
10e2f1ac PZ |
1665 | * can be used from IRQ context. |
1666 | */ | |
1667 | local_irq_disable(); | |
772bd008 MR |
1668 | env->dst_cpu = select_idle_sibling(env->p, env->src_cpu, |
1669 | env->dst_cpu); | |
10e2f1ac PZ |
1670 | local_irq_enable(); |
1671 | } | |
ba7e5a27 | 1672 | |
fb13c7ee MG |
1673 | assign: |
1674 | task_numa_assign(env, cur, imp); | |
1675 | unlock: | |
1676 | rcu_read_unlock(); | |
1677 | } | |
1678 | ||
887c290e RR |
1679 | static void task_numa_find_cpu(struct task_numa_env *env, |
1680 | long taskimp, long groupimp) | |
2c8a50aa MG |
1681 | { |
1682 | int cpu; | |
1683 | ||
1684 | for_each_cpu(cpu, cpumask_of_node(env->dst_nid)) { | |
1685 | /* Skip this CPU if the source task cannot migrate */ | |
0c98d344 | 1686 | if (!cpumask_test_cpu(cpu, &env->p->cpus_allowed)) |
2c8a50aa MG |
1687 | continue; |
1688 | ||
1689 | env->dst_cpu = cpu; | |
887c290e | 1690 | task_numa_compare(env, taskimp, groupimp); |
2c8a50aa MG |
1691 | } |
1692 | } | |
1693 | ||
6f9aad0b RR |
1694 | /* Only move tasks to a NUMA node less busy than the current node. */ |
1695 | static bool numa_has_capacity(struct task_numa_env *env) | |
1696 | { | |
1697 | struct numa_stats *src = &env->src_stats; | |
1698 | struct numa_stats *dst = &env->dst_stats; | |
1699 | ||
1700 | if (src->has_free_capacity && !dst->has_free_capacity) | |
1701 | return false; | |
1702 | ||
1703 | /* | |
1704 | * Only consider a task move if the source has a higher load | |
1705 | * than the destination, corrected for CPU capacity on each node. | |
1706 | * | |
1707 | * src->load dst->load | |
1708 | * --------------------- vs --------------------- | |
1709 | * src->compute_capacity dst->compute_capacity | |
1710 | */ | |
44dcb04f SD |
1711 | if (src->load * dst->compute_capacity * env->imbalance_pct > |
1712 | ||
1713 | dst->load * src->compute_capacity * 100) | |
6f9aad0b RR |
1714 | return true; |
1715 | ||
1716 | return false; | |
1717 | } | |
1718 | ||
58d081b5 MG |
1719 | static int task_numa_migrate(struct task_struct *p) |
1720 | { | |
58d081b5 MG |
1721 | struct task_numa_env env = { |
1722 | .p = p, | |
fb13c7ee | 1723 | |
58d081b5 | 1724 | .src_cpu = task_cpu(p), |
b32e86b4 | 1725 | .src_nid = task_node(p), |
fb13c7ee MG |
1726 | |
1727 | .imbalance_pct = 112, | |
1728 | ||
1729 | .best_task = NULL, | |
1730 | .best_imp = 0, | |
4142c3eb | 1731 | .best_cpu = -1, |
58d081b5 MG |
1732 | }; |
1733 | struct sched_domain *sd; | |
887c290e | 1734 | unsigned long taskweight, groupweight; |
7bd95320 | 1735 | int nid, ret, dist; |
887c290e | 1736 | long taskimp, groupimp; |
e6628d5b | 1737 | |
58d081b5 | 1738 | /* |
fb13c7ee MG |
1739 | * Pick the lowest SD_NUMA domain, as that would have the smallest |
1740 | * imbalance and would be the first to start moving tasks about. | |
1741 | * | |
1742 | * And we want to avoid any moving of tasks about, as that would create | |
1743 | * random movement of tasks -- counter the numa conditions we're trying | |
1744 | * to satisfy here. | |
58d081b5 MG |
1745 | */ |
1746 | rcu_read_lock(); | |
fb13c7ee | 1747 | sd = rcu_dereference(per_cpu(sd_numa, env.src_cpu)); |
46a73e8a RR |
1748 | if (sd) |
1749 | env.imbalance_pct = 100 + (sd->imbalance_pct - 100) / 2; | |
e6628d5b MG |
1750 | rcu_read_unlock(); |
1751 | ||
46a73e8a RR |
1752 | /* |
1753 | * Cpusets can break the scheduler domain tree into smaller | |
1754 | * balance domains, some of which do not cross NUMA boundaries. | |
1755 | * Tasks that are "trapped" in such domains cannot be migrated | |
1756 | * elsewhere, so there is no point in (re)trying. | |
1757 | */ | |
1758 | if (unlikely(!sd)) { | |
de1b301a | 1759 | p->numa_preferred_nid = task_node(p); |
46a73e8a RR |
1760 | return -EINVAL; |
1761 | } | |
1762 | ||
2c8a50aa | 1763 | env.dst_nid = p->numa_preferred_nid; |
7bd95320 RR |
1764 | dist = env.dist = node_distance(env.src_nid, env.dst_nid); |
1765 | taskweight = task_weight(p, env.src_nid, dist); | |
1766 | groupweight = group_weight(p, env.src_nid, dist); | |
1767 | update_numa_stats(&env.src_stats, env.src_nid); | |
1768 | taskimp = task_weight(p, env.dst_nid, dist) - taskweight; | |
1769 | groupimp = group_weight(p, env.dst_nid, dist) - groupweight; | |
2c8a50aa | 1770 | update_numa_stats(&env.dst_stats, env.dst_nid); |
58d081b5 | 1771 | |
a43455a1 | 1772 | /* Try to find a spot on the preferred nid. */ |
6f9aad0b RR |
1773 | if (numa_has_capacity(&env)) |
1774 | task_numa_find_cpu(&env, taskimp, groupimp); | |
e1dda8a7 | 1775 | |
9de05d48 RR |
1776 | /* |
1777 | * Look at other nodes in these cases: | |
1778 | * - there is no space available on the preferred_nid | |
1779 | * - the task is part of a numa_group that is interleaved across | |
1780 | * multiple NUMA nodes; in order to better consolidate the group, | |
1781 | * we need to check other locations. | |
1782 | */ | |
4142c3eb | 1783 | if (env.best_cpu == -1 || (p->numa_group && p->numa_group->active_nodes > 1)) { |
2c8a50aa MG |
1784 | for_each_online_node(nid) { |
1785 | if (nid == env.src_nid || nid == p->numa_preferred_nid) | |
1786 | continue; | |
58d081b5 | 1787 | |
7bd95320 | 1788 | dist = node_distance(env.src_nid, env.dst_nid); |
6c6b1193 RR |
1789 | if (sched_numa_topology_type == NUMA_BACKPLANE && |
1790 | dist != env.dist) { | |
1791 | taskweight = task_weight(p, env.src_nid, dist); | |
1792 | groupweight = group_weight(p, env.src_nid, dist); | |
1793 | } | |
7bd95320 | 1794 | |
83e1d2cd | 1795 | /* Only consider nodes where both task and groups benefit */ |
7bd95320 RR |
1796 | taskimp = task_weight(p, nid, dist) - taskweight; |
1797 | groupimp = group_weight(p, nid, dist) - groupweight; | |
887c290e | 1798 | if (taskimp < 0 && groupimp < 0) |
fb13c7ee MG |
1799 | continue; |
1800 | ||
7bd95320 | 1801 | env.dist = dist; |
2c8a50aa MG |
1802 | env.dst_nid = nid; |
1803 | update_numa_stats(&env.dst_stats, env.dst_nid); | |
6f9aad0b RR |
1804 | if (numa_has_capacity(&env)) |
1805 | task_numa_find_cpu(&env, taskimp, groupimp); | |
58d081b5 MG |
1806 | } |
1807 | } | |
1808 | ||
68d1b02a RR |
1809 | /* |
1810 | * If the task is part of a workload that spans multiple NUMA nodes, | |
1811 | * and is migrating into one of the workload's active nodes, remember | |
1812 | * this node as the task's preferred numa node, so the workload can | |
1813 | * settle down. | |
1814 | * A task that migrated to a second choice node will be better off | |
1815 | * trying for a better one later. Do not set the preferred node here. | |
1816 | */ | |
db015dae | 1817 | if (p->numa_group) { |
4142c3eb RR |
1818 | struct numa_group *ng = p->numa_group; |
1819 | ||
db015dae RR |
1820 | if (env.best_cpu == -1) |
1821 | nid = env.src_nid; | |
1822 | else | |
1823 | nid = env.dst_nid; | |
1824 | ||
4142c3eb | 1825 | if (ng->active_nodes > 1 && numa_is_active_node(env.dst_nid, ng)) |
db015dae RR |
1826 | sched_setnuma(p, env.dst_nid); |
1827 | } | |
1828 | ||
1829 | /* No better CPU than the current one was found. */ | |
1830 | if (env.best_cpu == -1) | |
1831 | return -EAGAIN; | |
0ec8aa00 | 1832 | |
04bb2f94 RR |
1833 | /* |
1834 | * Reset the scan period if the task is being rescheduled on an | |
1835 | * alternative node to recheck if the tasks is now properly placed. | |
1836 | */ | |
b5dd77c8 | 1837 | p->numa_scan_period = task_scan_start(p); |
04bb2f94 | 1838 | |
fb13c7ee | 1839 | if (env.best_task == NULL) { |
286549dc MG |
1840 | ret = migrate_task_to(p, env.best_cpu); |
1841 | if (ret != 0) | |
1842 | trace_sched_stick_numa(p, env.src_cpu, env.best_cpu); | |
fb13c7ee MG |
1843 | return ret; |
1844 | } | |
1845 | ||
1846 | ret = migrate_swap(p, env.best_task); | |
286549dc MG |
1847 | if (ret != 0) |
1848 | trace_sched_stick_numa(p, env.src_cpu, task_cpu(env.best_task)); | |
fb13c7ee MG |
1849 | put_task_struct(env.best_task); |
1850 | return ret; | |
e6628d5b MG |
1851 | } |
1852 | ||
6b9a7460 MG |
1853 | /* Attempt to migrate a task to a CPU on the preferred node. */ |
1854 | static void numa_migrate_preferred(struct task_struct *p) | |
1855 | { | |
5085e2a3 | 1856 | unsigned long interval = HZ; |
7347fc87 | 1857 | unsigned long numa_migrate_retry; |
5085e2a3 | 1858 | |
2739d3ee | 1859 | /* This task has no NUMA fault statistics yet */ |
44dba3d5 | 1860 | if (unlikely(p->numa_preferred_nid == -1 || !p->numa_faults)) |
6b9a7460 MG |
1861 | return; |
1862 | ||
2739d3ee | 1863 | /* Periodically retry migrating the task to the preferred node */ |
5085e2a3 | 1864 | interval = min(interval, msecs_to_jiffies(p->numa_scan_period) / 16); |
7347fc87 MG |
1865 | numa_migrate_retry = jiffies + interval; |
1866 | ||
1867 | /* | |
1868 | * Check that the new retry threshold is after the current one. If | |
1869 | * the retry is in the future, it implies that wake_affine has | |
1870 | * temporarily asked NUMA balancing to backoff from placement. | |
1871 | */ | |
1872 | if (numa_migrate_retry > p->numa_migrate_retry) | |
1873 | return; | |
1874 | ||
1875 | /* Safe to try placing the task on the preferred node */ | |
1876 | p->numa_migrate_retry = numa_migrate_retry; | |
2739d3ee RR |
1877 | |
1878 | /* Success if task is already running on preferred CPU */ | |
de1b301a | 1879 | if (task_node(p) == p->numa_preferred_nid) |
6b9a7460 MG |
1880 | return; |
1881 | ||
1882 | /* Otherwise, try migrate to a CPU on the preferred node */ | |
2739d3ee | 1883 | task_numa_migrate(p); |
6b9a7460 MG |
1884 | } |
1885 | ||
20e07dea | 1886 | /* |
4142c3eb | 1887 | * Find out how many nodes on the workload is actively running on. Do this by |
20e07dea RR |
1888 | * tracking the nodes from which NUMA hinting faults are triggered. This can |
1889 | * be different from the set of nodes where the workload's memory is currently | |
1890 | * located. | |
20e07dea | 1891 | */ |
4142c3eb | 1892 | static void numa_group_count_active_nodes(struct numa_group *numa_group) |
20e07dea RR |
1893 | { |
1894 | unsigned long faults, max_faults = 0; | |
4142c3eb | 1895 | int nid, active_nodes = 0; |
20e07dea RR |
1896 | |
1897 | for_each_online_node(nid) { | |
1898 | faults = group_faults_cpu(numa_group, nid); | |
1899 | if (faults > max_faults) | |
1900 | max_faults = faults; | |
1901 | } | |
1902 | ||
1903 | for_each_online_node(nid) { | |
1904 | faults = group_faults_cpu(numa_group, nid); | |
4142c3eb RR |
1905 | if (faults * ACTIVE_NODE_FRACTION > max_faults) |
1906 | active_nodes++; | |
20e07dea | 1907 | } |
4142c3eb RR |
1908 | |
1909 | numa_group->max_faults_cpu = max_faults; | |
1910 | numa_group->active_nodes = active_nodes; | |
20e07dea RR |
1911 | } |
1912 | ||
04bb2f94 RR |
1913 | /* |
1914 | * When adapting the scan rate, the period is divided into NUMA_PERIOD_SLOTS | |
1915 | * increments. The more local the fault statistics are, the higher the scan | |
a22b4b01 RR |
1916 | * period will be for the next scan window. If local/(local+remote) ratio is |
1917 | * below NUMA_PERIOD_THRESHOLD (where range of ratio is 1..NUMA_PERIOD_SLOTS) | |
1918 | * the scan period will decrease. Aim for 70% local accesses. | |
04bb2f94 RR |
1919 | */ |
1920 | #define NUMA_PERIOD_SLOTS 10 | |
a22b4b01 | 1921 | #define NUMA_PERIOD_THRESHOLD 7 |
04bb2f94 RR |
1922 | |
1923 | /* | |
1924 | * Increase the scan period (slow down scanning) if the majority of | |
1925 | * our memory is already on our local node, or if the majority of | |
1926 | * the page accesses are shared with other processes. | |
1927 | * Otherwise, decrease the scan period. | |
1928 | */ | |
1929 | static void update_task_scan_period(struct task_struct *p, | |
1930 | unsigned long shared, unsigned long private) | |
1931 | { | |
1932 | unsigned int period_slot; | |
37ec97de | 1933 | int lr_ratio, ps_ratio; |
04bb2f94 RR |
1934 | int diff; |
1935 | ||
1936 | unsigned long remote = p->numa_faults_locality[0]; | |
1937 | unsigned long local = p->numa_faults_locality[1]; | |
1938 | ||
1939 | /* | |
1940 | * If there were no record hinting faults then either the task is | |
1941 | * completely idle or all activity is areas that are not of interest | |
074c2381 MG |
1942 | * to automatic numa balancing. Related to that, if there were failed |
1943 | * migration then it implies we are migrating too quickly or the local | |
1944 | * node is overloaded. In either case, scan slower | |
04bb2f94 | 1945 | */ |
074c2381 | 1946 | if (local + shared == 0 || p->numa_faults_locality[2]) { |
04bb2f94 RR |
1947 | p->numa_scan_period = min(p->numa_scan_period_max, |
1948 | p->numa_scan_period << 1); | |
1949 | ||
1950 | p->mm->numa_next_scan = jiffies + | |
1951 | msecs_to_jiffies(p->numa_scan_period); | |
1952 | ||
1953 | return; | |
1954 | } | |
1955 | ||
1956 | /* | |
1957 | * Prepare to scale scan period relative to the current period. | |
1958 | * == NUMA_PERIOD_THRESHOLD scan period stays the same | |
1959 | * < NUMA_PERIOD_THRESHOLD scan period decreases (scan faster) | |
1960 | * >= NUMA_PERIOD_THRESHOLD scan period increases (scan slower) | |
1961 | */ | |
1962 | period_slot = DIV_ROUND_UP(p->numa_scan_period, NUMA_PERIOD_SLOTS); | |
37ec97de RR |
1963 | lr_ratio = (local * NUMA_PERIOD_SLOTS) / (local + remote); |
1964 | ps_ratio = (private * NUMA_PERIOD_SLOTS) / (private + shared); | |
1965 | ||
1966 | if (ps_ratio >= NUMA_PERIOD_THRESHOLD) { | |
1967 | /* | |
1968 | * Most memory accesses are local. There is no need to | |
1969 | * do fast NUMA scanning, since memory is already local. | |
1970 | */ | |
1971 | int slot = ps_ratio - NUMA_PERIOD_THRESHOLD; | |
1972 | if (!slot) | |
1973 | slot = 1; | |
1974 | diff = slot * period_slot; | |
1975 | } else if (lr_ratio >= NUMA_PERIOD_THRESHOLD) { | |
1976 | /* | |
1977 | * Most memory accesses are shared with other tasks. | |
1978 | * There is no point in continuing fast NUMA scanning, | |
1979 | * since other tasks may just move the memory elsewhere. | |
1980 | */ | |
1981 | int slot = lr_ratio - NUMA_PERIOD_THRESHOLD; | |
04bb2f94 RR |
1982 | if (!slot) |
1983 | slot = 1; | |
1984 | diff = slot * period_slot; | |
1985 | } else { | |
04bb2f94 | 1986 | /* |
37ec97de RR |
1987 | * Private memory faults exceed (SLOTS-THRESHOLD)/SLOTS, |
1988 | * yet they are not on the local NUMA node. Speed up | |
1989 | * NUMA scanning to get the memory moved over. | |
04bb2f94 | 1990 | */ |
37ec97de RR |
1991 | int ratio = max(lr_ratio, ps_ratio); |
1992 | diff = -(NUMA_PERIOD_THRESHOLD - ratio) * period_slot; | |
04bb2f94 RR |
1993 | } |
1994 | ||
1995 | p->numa_scan_period = clamp(p->numa_scan_period + diff, | |
1996 | task_scan_min(p), task_scan_max(p)); | |
1997 | memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); | |
1998 | } | |
1999 | ||
7e2703e6 RR |
2000 | /* |
2001 | * Get the fraction of time the task has been running since the last | |
2002 | * NUMA placement cycle. The scheduler keeps similar statistics, but | |
2003 | * decays those on a 32ms period, which is orders of magnitude off | |
2004 | * from the dozens-of-seconds NUMA balancing period. Use the scheduler | |
2005 | * stats only if the task is so new there are no NUMA statistics yet. | |
2006 | */ | |
2007 | static u64 numa_get_avg_runtime(struct task_struct *p, u64 *period) | |
2008 | { | |
2009 | u64 runtime, delta, now; | |
2010 | /* Use the start of this time slice to avoid calculations. */ | |
2011 | now = p->se.exec_start; | |
2012 | runtime = p->se.sum_exec_runtime; | |
2013 | ||
2014 | if (p->last_task_numa_placement) { | |
2015 | delta = runtime - p->last_sum_exec_runtime; | |
2016 | *period = now - p->last_task_numa_placement; | |
2017 | } else { | |
c7b50216 | 2018 | delta = p->se.avg.load_sum; |
9d89c257 | 2019 | *period = LOAD_AVG_MAX; |
7e2703e6 RR |
2020 | } |
2021 | ||
2022 | p->last_sum_exec_runtime = runtime; | |
2023 | p->last_task_numa_placement = now; | |
2024 | ||
2025 | return delta; | |
2026 | } | |
2027 | ||
54009416 RR |
2028 | /* |
2029 | * Determine the preferred nid for a task in a numa_group. This needs to | |
2030 | * be done in a way that produces consistent results with group_weight, | |
2031 | * otherwise workloads might not converge. | |
2032 | */ | |
2033 | static int preferred_group_nid(struct task_struct *p, int nid) | |
2034 | { | |
2035 | nodemask_t nodes; | |
2036 | int dist; | |
2037 | ||
2038 | /* Direct connections between all NUMA nodes. */ | |
2039 | if (sched_numa_topology_type == NUMA_DIRECT) | |
2040 | return nid; | |
2041 | ||
2042 | /* | |
2043 | * On a system with glueless mesh NUMA topology, group_weight | |
2044 | * scores nodes according to the number of NUMA hinting faults on | |
2045 | * both the node itself, and on nearby nodes. | |
2046 | */ | |
2047 | if (sched_numa_topology_type == NUMA_GLUELESS_MESH) { | |
2048 | unsigned long score, max_score = 0; | |
2049 | int node, max_node = nid; | |
2050 | ||
2051 | dist = sched_max_numa_distance; | |
2052 | ||
2053 | for_each_online_node(node) { | |
2054 | score = group_weight(p, node, dist); | |
2055 | if (score > max_score) { | |
2056 | max_score = score; | |
2057 | max_node = node; | |
2058 | } | |
2059 | } | |
2060 | return max_node; | |
2061 | } | |
2062 | ||
2063 | /* | |
2064 | * Finding the preferred nid in a system with NUMA backplane | |
2065 | * interconnect topology is more involved. The goal is to locate | |
2066 | * tasks from numa_groups near each other in the system, and | |
2067 | * untangle workloads from different sides of the system. This requires | |
2068 | * searching down the hierarchy of node groups, recursively searching | |
2069 | * inside the highest scoring group of nodes. The nodemask tricks | |
2070 | * keep the complexity of the search down. | |
2071 | */ | |
2072 | nodes = node_online_map; | |
2073 | for (dist = sched_max_numa_distance; dist > LOCAL_DISTANCE; dist--) { | |
2074 | unsigned long max_faults = 0; | |
81907478 | 2075 | nodemask_t max_group = NODE_MASK_NONE; |
54009416 RR |
2076 | int a, b; |
2077 | ||
2078 | /* Are there nodes at this distance from each other? */ | |
2079 | if (!find_numa_distance(dist)) | |
2080 | continue; | |
2081 | ||
2082 | for_each_node_mask(a, nodes) { | |
2083 | unsigned long faults = 0; | |
2084 | nodemask_t this_group; | |
2085 | nodes_clear(this_group); | |
2086 | ||
2087 | /* Sum group's NUMA faults; includes a==b case. */ | |
2088 | for_each_node_mask(b, nodes) { | |
2089 | if (node_distance(a, b) < dist) { | |
2090 | faults += group_faults(p, b); | |
2091 | node_set(b, this_group); | |
2092 | node_clear(b, nodes); | |
2093 | } | |
2094 | } | |
2095 | ||
2096 | /* Remember the top group. */ | |
2097 | if (faults > max_faults) { | |
2098 | max_faults = faults; | |
2099 | max_group = this_group; | |
2100 | /* | |
2101 | * subtle: at the smallest distance there is | |
2102 | * just one node left in each "group", the | |
2103 | * winner is the preferred nid. | |
2104 | */ | |
2105 | nid = a; | |
2106 | } | |
2107 | } | |
2108 | /* Next round, evaluate the nodes within max_group. */ | |
890a5409 JB |
2109 | if (!max_faults) |
2110 | break; | |
54009416 RR |
2111 | nodes = max_group; |
2112 | } | |
2113 | return nid; | |
2114 | } | |
2115 | ||
cbee9f88 PZ |
2116 | static void task_numa_placement(struct task_struct *p) |
2117 | { | |
83e1d2cd MG |
2118 | int seq, nid, max_nid = -1, max_group_nid = -1; |
2119 | unsigned long max_faults = 0, max_group_faults = 0; | |
04bb2f94 | 2120 | unsigned long fault_types[2] = { 0, 0 }; |
7e2703e6 RR |
2121 | unsigned long total_faults; |
2122 | u64 runtime, period; | |
7dbd13ed | 2123 | spinlock_t *group_lock = NULL; |
cbee9f88 | 2124 | |
7e5a2c17 JL |
2125 | /* |
2126 | * The p->mm->numa_scan_seq field gets updated without | |
2127 | * exclusive access. Use READ_ONCE() here to ensure | |
2128 | * that the field is read in a single access: | |
2129 | */ | |
316c1608 | 2130 | seq = READ_ONCE(p->mm->numa_scan_seq); |
cbee9f88 PZ |
2131 | if (p->numa_scan_seq == seq) |
2132 | return; | |
2133 | p->numa_scan_seq = seq; | |
598f0ec0 | 2134 | p->numa_scan_period_max = task_scan_max(p); |
cbee9f88 | 2135 | |
7e2703e6 RR |
2136 | total_faults = p->numa_faults_locality[0] + |
2137 | p->numa_faults_locality[1]; | |
2138 | runtime = numa_get_avg_runtime(p, &period); | |
2139 | ||
7dbd13ed MG |
2140 | /* If the task is part of a group prevent parallel updates to group stats */ |
2141 | if (p->numa_group) { | |
2142 | group_lock = &p->numa_group->lock; | |
60e69eed | 2143 | spin_lock_irq(group_lock); |
7dbd13ed MG |
2144 | } |
2145 | ||
688b7585 MG |
2146 | /* Find the node with the highest number of faults */ |
2147 | for_each_online_node(nid) { | |
44dba3d5 IM |
2148 | /* Keep track of the offsets in numa_faults array */ |
2149 | int mem_idx, membuf_idx, cpu_idx, cpubuf_idx; | |
83e1d2cd | 2150 | unsigned long faults = 0, group_faults = 0; |
44dba3d5 | 2151 | int priv; |
745d6147 | 2152 | |
be1e4e76 | 2153 | for (priv = 0; priv < NR_NUMA_HINT_FAULT_TYPES; priv++) { |
7e2703e6 | 2154 | long diff, f_diff, f_weight; |
8c8a743c | 2155 | |
44dba3d5 IM |
2156 | mem_idx = task_faults_idx(NUMA_MEM, nid, priv); |
2157 | membuf_idx = task_faults_idx(NUMA_MEMBUF, nid, priv); | |
2158 | cpu_idx = task_faults_idx(NUMA_CPU, nid, priv); | |
2159 | cpubuf_idx = task_faults_idx(NUMA_CPUBUF, nid, priv); | |
745d6147 | 2160 | |
ac8e895b | 2161 | /* Decay existing window, copy faults since last scan */ |
44dba3d5 IM |
2162 | diff = p->numa_faults[membuf_idx] - p->numa_faults[mem_idx] / 2; |
2163 | fault_types[priv] += p->numa_faults[membuf_idx]; | |
2164 | p->numa_faults[membuf_idx] = 0; | |
fb13c7ee | 2165 | |
7e2703e6 RR |
2166 | /* |
2167 | * Normalize the faults_from, so all tasks in a group | |
2168 | * count according to CPU use, instead of by the raw | |
2169 | * number of faults. Tasks with little runtime have | |
2170 | * little over-all impact on throughput, and thus their | |
2171 | * faults are less important. | |
2172 | */ | |
2173 | f_weight = div64_u64(runtime << 16, period + 1); | |
44dba3d5 | 2174 | f_weight = (f_weight * p->numa_faults[cpubuf_idx]) / |
7e2703e6 | 2175 | (total_faults + 1); |
44dba3d5 IM |
2176 | f_diff = f_weight - p->numa_faults[cpu_idx] / 2; |
2177 | p->numa_faults[cpubuf_idx] = 0; | |
50ec8a40 | 2178 | |
44dba3d5 IM |
2179 | p->numa_faults[mem_idx] += diff; |
2180 | p->numa_faults[cpu_idx] += f_diff; | |
2181 | faults += p->numa_faults[mem_idx]; | |
83e1d2cd | 2182 | p->total_numa_faults += diff; |
8c8a743c | 2183 | if (p->numa_group) { |
44dba3d5 IM |
2184 | /* |
2185 | * safe because we can only change our own group | |
2186 | * | |
2187 | * mem_idx represents the offset for a given | |
2188 | * nid and priv in a specific region because it | |
2189 | * is at the beginning of the numa_faults array. | |
2190 | */ | |
2191 | p->numa_group->faults[mem_idx] += diff; | |
2192 | p->numa_group->faults_cpu[mem_idx] += f_diff; | |
989348b5 | 2193 | p->numa_group->total_faults += diff; |
44dba3d5 | 2194 | group_faults += p->numa_group->faults[mem_idx]; |
8c8a743c | 2195 | } |
ac8e895b MG |
2196 | } |
2197 | ||
688b7585 MG |
2198 | if (faults > max_faults) { |
2199 | max_faults = faults; | |
2200 | max_nid = nid; | |
2201 | } | |
83e1d2cd MG |
2202 | |
2203 | if (group_faults > max_group_faults) { | |
2204 | max_group_faults = group_faults; | |
2205 | max_group_nid = nid; | |
2206 | } | |
2207 | } | |
2208 | ||
04bb2f94 RR |
2209 | update_task_scan_period(p, fault_types[0], fault_types[1]); |
2210 | ||
7dbd13ed | 2211 | if (p->numa_group) { |
4142c3eb | 2212 | numa_group_count_active_nodes(p->numa_group); |
60e69eed | 2213 | spin_unlock_irq(group_lock); |
54009416 | 2214 | max_nid = preferred_group_nid(p, max_group_nid); |
688b7585 MG |
2215 | } |
2216 | ||
bb97fc31 RR |
2217 | if (max_faults) { |
2218 | /* Set the new preferred node */ | |
2219 | if (max_nid != p->numa_preferred_nid) | |
2220 | sched_setnuma(p, max_nid); | |
2221 | ||
2222 | if (task_node(p) != p->numa_preferred_nid) | |
2223 | numa_migrate_preferred(p); | |
3a7053b3 | 2224 | } |
cbee9f88 PZ |
2225 | } |
2226 | ||
8c8a743c PZ |
2227 | static inline int get_numa_group(struct numa_group *grp) |
2228 | { | |
2229 | return atomic_inc_not_zero(&grp->refcount); | |
2230 | } | |
2231 | ||
2232 | static inline void put_numa_group(struct numa_group *grp) | |
2233 | { | |
2234 | if (atomic_dec_and_test(&grp->refcount)) | |
2235 | kfree_rcu(grp, rcu); | |
2236 | } | |
2237 | ||
3e6a9418 MG |
2238 | static void task_numa_group(struct task_struct *p, int cpupid, int flags, |
2239 | int *priv) | |
8c8a743c PZ |
2240 | { |
2241 | struct numa_group *grp, *my_grp; | |
2242 | struct task_struct *tsk; | |
2243 | bool join = false; | |
2244 | int cpu = cpupid_to_cpu(cpupid); | |
2245 | int i; | |
2246 | ||
2247 | if (unlikely(!p->numa_group)) { | |
2248 | unsigned int size = sizeof(struct numa_group) + | |
50ec8a40 | 2249 | 4*nr_node_ids*sizeof(unsigned long); |
8c8a743c PZ |
2250 | |
2251 | grp = kzalloc(size, GFP_KERNEL | __GFP_NOWARN); | |
2252 | if (!grp) | |
2253 | return; | |
2254 | ||
2255 | atomic_set(&grp->refcount, 1); | |
4142c3eb RR |
2256 | grp->active_nodes = 1; |
2257 | grp->max_faults_cpu = 0; | |
8c8a743c | 2258 | spin_lock_init(&grp->lock); |
e29cf08b | 2259 | grp->gid = p->pid; |
50ec8a40 | 2260 | /* Second half of the array tracks nids where faults happen */ |
be1e4e76 RR |
2261 | grp->faults_cpu = grp->faults + NR_NUMA_HINT_FAULT_TYPES * |
2262 | nr_node_ids; | |
8c8a743c | 2263 | |
be1e4e76 | 2264 | for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) |
44dba3d5 | 2265 | grp->faults[i] = p->numa_faults[i]; |
8c8a743c | 2266 | |
989348b5 | 2267 | grp->total_faults = p->total_numa_faults; |
83e1d2cd | 2268 | |
8c8a743c PZ |
2269 | grp->nr_tasks++; |
2270 | rcu_assign_pointer(p->numa_group, grp); | |
2271 | } | |
2272 | ||
2273 | rcu_read_lock(); | |
316c1608 | 2274 | tsk = READ_ONCE(cpu_rq(cpu)->curr); |
8c8a743c PZ |
2275 | |
2276 | if (!cpupid_match_pid(tsk, cpupid)) | |
3354781a | 2277 | goto no_join; |
8c8a743c PZ |
2278 | |
2279 | grp = rcu_dereference(tsk->numa_group); | |
2280 | if (!grp) | |
3354781a | 2281 | goto no_join; |
8c8a743c PZ |
2282 | |
2283 | my_grp = p->numa_group; | |
2284 | if (grp == my_grp) | |
3354781a | 2285 | goto no_join; |
8c8a743c PZ |
2286 | |
2287 | /* | |
2288 | * Only join the other group if its bigger; if we're the bigger group, | |
2289 | * the other task will join us. | |
2290 | */ | |
2291 | if (my_grp->nr_tasks > grp->nr_tasks) | |
3354781a | 2292 | goto no_join; |
8c8a743c PZ |
2293 | |
2294 | /* | |
2295 | * Tie-break on the grp address. | |
2296 | */ | |
2297 | if (my_grp->nr_tasks == grp->nr_tasks && my_grp > grp) | |
3354781a | 2298 | goto no_join; |
8c8a743c | 2299 | |
dabe1d99 RR |
2300 | /* Always join threads in the same process. */ |
2301 | if (tsk->mm == current->mm) | |
2302 | join = true; | |
2303 | ||
2304 | /* Simple filter to avoid false positives due to PID collisions */ | |
2305 | if (flags & TNF_SHARED) | |
2306 | join = true; | |
8c8a743c | 2307 | |
3e6a9418 MG |
2308 | /* Update priv based on whether false sharing was detected */ |
2309 | *priv = !join; | |
2310 | ||
dabe1d99 | 2311 | if (join && !get_numa_group(grp)) |
3354781a | 2312 | goto no_join; |
8c8a743c | 2313 | |
8c8a743c PZ |
2314 | rcu_read_unlock(); |
2315 | ||
2316 | if (!join) | |
2317 | return; | |
2318 | ||
60e69eed MG |
2319 | BUG_ON(irqs_disabled()); |
2320 | double_lock_irq(&my_grp->lock, &grp->lock); | |
989348b5 | 2321 | |
be1e4e76 | 2322 | for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) { |
44dba3d5 IM |
2323 | my_grp->faults[i] -= p->numa_faults[i]; |
2324 | grp->faults[i] += p->numa_faults[i]; | |
8c8a743c | 2325 | } |
989348b5 MG |
2326 | my_grp->total_faults -= p->total_numa_faults; |
2327 | grp->total_faults += p->total_numa_faults; | |
8c8a743c | 2328 | |
8c8a743c PZ |
2329 | my_grp->nr_tasks--; |
2330 | grp->nr_tasks++; | |
2331 | ||
2332 | spin_unlock(&my_grp->lock); | |
60e69eed | 2333 | spin_unlock_irq(&grp->lock); |
8c8a743c PZ |
2334 | |
2335 | rcu_assign_pointer(p->numa_group, grp); | |
2336 | ||
2337 | put_numa_group(my_grp); | |
3354781a PZ |
2338 | return; |
2339 | ||
2340 | no_join: | |
2341 | rcu_read_unlock(); | |
2342 | return; | |
8c8a743c PZ |
2343 | } |
2344 | ||
2345 | void task_numa_free(struct task_struct *p) | |
2346 | { | |
2347 | struct numa_group *grp = p->numa_group; | |
44dba3d5 | 2348 | void *numa_faults = p->numa_faults; |
e9dd685c SR |
2349 | unsigned long flags; |
2350 | int i; | |
8c8a743c PZ |
2351 | |
2352 | if (grp) { | |
e9dd685c | 2353 | spin_lock_irqsave(&grp->lock, flags); |
be1e4e76 | 2354 | for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) |
44dba3d5 | 2355 | grp->faults[i] -= p->numa_faults[i]; |
989348b5 | 2356 | grp->total_faults -= p->total_numa_faults; |
83e1d2cd | 2357 | |
8c8a743c | 2358 | grp->nr_tasks--; |
e9dd685c | 2359 | spin_unlock_irqrestore(&grp->lock, flags); |
35b123e2 | 2360 | RCU_INIT_POINTER(p->numa_group, NULL); |
8c8a743c PZ |
2361 | put_numa_group(grp); |
2362 | } | |
2363 | ||
44dba3d5 | 2364 | p->numa_faults = NULL; |
82727018 | 2365 | kfree(numa_faults); |
8c8a743c PZ |
2366 | } |
2367 | ||
cbee9f88 PZ |
2368 | /* |
2369 | * Got a PROT_NONE fault for a page on @node. | |
2370 | */ | |
58b46da3 | 2371 | void task_numa_fault(int last_cpupid, int mem_node, int pages, int flags) |
cbee9f88 PZ |
2372 | { |
2373 | struct task_struct *p = current; | |
6688cc05 | 2374 | bool migrated = flags & TNF_MIGRATED; |
58b46da3 | 2375 | int cpu_node = task_node(current); |
792568ec | 2376 | int local = !!(flags & TNF_FAULT_LOCAL); |
4142c3eb | 2377 | struct numa_group *ng; |
ac8e895b | 2378 | int priv; |
cbee9f88 | 2379 | |
2a595721 | 2380 | if (!static_branch_likely(&sched_numa_balancing)) |
1a687c2e MG |
2381 | return; |
2382 | ||
9ff1d9ff MG |
2383 | /* for example, ksmd faulting in a user's mm */ |
2384 | if (!p->mm) | |
2385 | return; | |
2386 | ||
f809ca9a | 2387 | /* Allocate buffer to track faults on a per-node basis */ |
44dba3d5 IM |
2388 | if (unlikely(!p->numa_faults)) { |
2389 | int size = sizeof(*p->numa_faults) * | |
be1e4e76 | 2390 | NR_NUMA_HINT_FAULT_BUCKETS * nr_node_ids; |
f809ca9a | 2391 | |
44dba3d5 IM |
2392 | p->numa_faults = kzalloc(size, GFP_KERNEL|__GFP_NOWARN); |
2393 | if (!p->numa_faults) | |
f809ca9a | 2394 | return; |
745d6147 | 2395 | |
83e1d2cd | 2396 | p->total_numa_faults = 0; |
04bb2f94 | 2397 | memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); |
f809ca9a | 2398 | } |
cbee9f88 | 2399 | |
8c8a743c PZ |
2400 | /* |
2401 | * First accesses are treated as private, otherwise consider accesses | |
2402 | * to be private if the accessing pid has not changed | |
2403 | */ | |
2404 | if (unlikely(last_cpupid == (-1 & LAST_CPUPID_MASK))) { | |
2405 | priv = 1; | |
2406 | } else { | |
2407 | priv = cpupid_match_pid(p, last_cpupid); | |
6688cc05 | 2408 | if (!priv && !(flags & TNF_NO_GROUP)) |
3e6a9418 | 2409 | task_numa_group(p, last_cpupid, flags, &priv); |
8c8a743c PZ |
2410 | } |
2411 | ||
792568ec RR |
2412 | /* |
2413 | * If a workload spans multiple NUMA nodes, a shared fault that | |
2414 | * occurs wholly within the set of nodes that the workload is | |
2415 | * actively using should be counted as local. This allows the | |
2416 | * scan rate to slow down when a workload has settled down. | |
2417 | */ | |
4142c3eb RR |
2418 | ng = p->numa_group; |
2419 | if (!priv && !local && ng && ng->active_nodes > 1 && | |
2420 | numa_is_active_node(cpu_node, ng) && | |
2421 | numa_is_active_node(mem_node, ng)) | |
792568ec RR |
2422 | local = 1; |
2423 | ||
cbee9f88 | 2424 | task_numa_placement(p); |
f809ca9a | 2425 | |
2739d3ee RR |
2426 | /* |
2427 | * Retry task to preferred node migration periodically, in case it | |
2428 | * case it previously failed, or the scheduler moved us. | |
2429 | */ | |
2430 | if (time_after(jiffies, p->numa_migrate_retry)) | |
6b9a7460 MG |
2431 | numa_migrate_preferred(p); |
2432 | ||
b32e86b4 IM |
2433 | if (migrated) |
2434 | p->numa_pages_migrated += pages; | |
074c2381 MG |
2435 | if (flags & TNF_MIGRATE_FAIL) |
2436 | p->numa_faults_locality[2] += pages; | |
b32e86b4 | 2437 | |
44dba3d5 IM |
2438 | p->numa_faults[task_faults_idx(NUMA_MEMBUF, mem_node, priv)] += pages; |
2439 | p->numa_faults[task_faults_idx(NUMA_CPUBUF, cpu_node, priv)] += pages; | |
792568ec | 2440 | p->numa_faults_locality[local] += pages; |
cbee9f88 PZ |
2441 | } |
2442 | ||
6e5fb223 PZ |
2443 | static void reset_ptenuma_scan(struct task_struct *p) |
2444 | { | |
7e5a2c17 JL |
2445 | /* |
2446 | * We only did a read acquisition of the mmap sem, so | |
2447 | * p->mm->numa_scan_seq is written to without exclusive access | |
2448 | * and the update is not guaranteed to be atomic. That's not | |
2449 | * much of an issue though, since this is just used for | |
2450 | * statistical sampling. Use READ_ONCE/WRITE_ONCE, which are not | |
2451 | * expensive, to avoid any form of compiler optimizations: | |
2452 | */ | |
316c1608 | 2453 | WRITE_ONCE(p->mm->numa_scan_seq, READ_ONCE(p->mm->numa_scan_seq) + 1); |
6e5fb223 PZ |
2454 | p->mm->numa_scan_offset = 0; |
2455 | } | |
2456 | ||
cbee9f88 PZ |
2457 | /* |
2458 | * The expensive part of numa migration is done from task_work context. | |
2459 | * Triggered from task_tick_numa(). | |
2460 | */ | |
2461 | void task_numa_work(struct callback_head *work) | |
2462 | { | |
2463 | unsigned long migrate, next_scan, now = jiffies; | |
2464 | struct task_struct *p = current; | |
2465 | struct mm_struct *mm = p->mm; | |
51170840 | 2466 | u64 runtime = p->se.sum_exec_runtime; |
6e5fb223 | 2467 | struct vm_area_struct *vma; |
9f40604c | 2468 | unsigned long start, end; |
598f0ec0 | 2469 | unsigned long nr_pte_updates = 0; |
4620f8c1 | 2470 | long pages, virtpages; |
cbee9f88 | 2471 | |
9148a3a1 | 2472 | SCHED_WARN_ON(p != container_of(work, struct task_struct, numa_work)); |
cbee9f88 PZ |
2473 | |
2474 | work->next = work; /* protect against double add */ | |
2475 | /* | |
2476 | * Who cares about NUMA placement when they're dying. | |
2477 | * | |
2478 | * NOTE: make sure not to dereference p->mm before this check, | |
2479 | * exit_task_work() happens _after_ exit_mm() so we could be called | |
2480 | * without p->mm even though we still had it when we enqueued this | |
2481 | * work. | |
2482 | */ | |
2483 | if (p->flags & PF_EXITING) | |
2484 | return; | |
2485 | ||
930aa174 | 2486 | if (!mm->numa_next_scan) { |
7e8d16b6 MG |
2487 | mm->numa_next_scan = now + |
2488 | msecs_to_jiffies(sysctl_numa_balancing_scan_delay); | |
b8593bfd MG |
2489 | } |
2490 | ||
cbee9f88 PZ |
2491 | /* |
2492 | * Enforce maximal scan/migration frequency.. | |
2493 | */ | |
2494 | migrate = mm->numa_next_scan; | |
2495 | if (time_before(now, migrate)) | |
2496 | return; | |
2497 | ||
598f0ec0 MG |
2498 | if (p->numa_scan_period == 0) { |
2499 | p->numa_scan_period_max = task_scan_max(p); | |
b5dd77c8 | 2500 | p->numa_scan_period = task_scan_start(p); |
598f0ec0 | 2501 | } |
cbee9f88 | 2502 | |
fb003b80 | 2503 | next_scan = now + msecs_to_jiffies(p->numa_scan_period); |
cbee9f88 PZ |
2504 | if (cmpxchg(&mm->numa_next_scan, migrate, next_scan) != migrate) |
2505 | return; | |
2506 | ||
19a78d11 PZ |
2507 | /* |
2508 | * Delay this task enough that another task of this mm will likely win | |
2509 | * the next time around. | |
2510 | */ | |
2511 | p->node_stamp += 2 * TICK_NSEC; | |
2512 | ||
9f40604c MG |
2513 | start = mm->numa_scan_offset; |
2514 | pages = sysctl_numa_balancing_scan_size; | |
2515 | pages <<= 20 - PAGE_SHIFT; /* MB in pages */ | |
4620f8c1 | 2516 | virtpages = pages * 8; /* Scan up to this much virtual space */ |
9f40604c MG |
2517 | if (!pages) |
2518 | return; | |
cbee9f88 | 2519 | |
4620f8c1 | 2520 | |
8655d549 VB |
2521 | if (!down_read_trylock(&mm->mmap_sem)) |
2522 | return; | |
9f40604c | 2523 | vma = find_vma(mm, start); |
6e5fb223 PZ |
2524 | if (!vma) { |
2525 | reset_ptenuma_scan(p); | |
9f40604c | 2526 | start = 0; |
6e5fb223 PZ |
2527 | vma = mm->mmap; |
2528 | } | |
9f40604c | 2529 | for (; vma; vma = vma->vm_next) { |
6b79c57b | 2530 | if (!vma_migratable(vma) || !vma_policy_mof(vma) || |
8e76d4ee | 2531 | is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_MIXEDMAP)) { |
6e5fb223 | 2532 | continue; |
6b79c57b | 2533 | } |
6e5fb223 | 2534 | |
4591ce4f MG |
2535 | /* |
2536 | * Shared library pages mapped by multiple processes are not | |
2537 | * migrated as it is expected they are cache replicated. Avoid | |
2538 | * hinting faults in read-only file-backed mappings or the vdso | |
2539 | * as migrating the pages will be of marginal benefit. | |
2540 | */ | |
2541 | if (!vma->vm_mm || | |
2542 | (vma->vm_file && (vma->vm_flags & (VM_READ|VM_WRITE)) == (VM_READ))) | |
2543 | continue; | |
2544 | ||
3c67f474 MG |
2545 | /* |
2546 | * Skip inaccessible VMAs to avoid any confusion between | |
2547 | * PROT_NONE and NUMA hinting ptes | |
2548 | */ | |
2549 | if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))) | |
2550 | continue; | |
4591ce4f | 2551 | |
9f40604c MG |
2552 | do { |
2553 | start = max(start, vma->vm_start); | |
2554 | end = ALIGN(start + (pages << PAGE_SHIFT), HPAGE_SIZE); | |
2555 | end = min(end, vma->vm_end); | |
4620f8c1 | 2556 | nr_pte_updates = change_prot_numa(vma, start, end); |
598f0ec0 MG |
2557 | |
2558 | /* | |
4620f8c1 RR |
2559 | * Try to scan sysctl_numa_balancing_size worth of |
2560 | * hpages that have at least one present PTE that | |
2561 | * is not already pte-numa. If the VMA contains | |
2562 | * areas that are unused or already full of prot_numa | |
2563 | * PTEs, scan up to virtpages, to skip through those | |
2564 | * areas faster. | |
598f0ec0 MG |
2565 | */ |
2566 | if (nr_pte_updates) | |
2567 | pages -= (end - start) >> PAGE_SHIFT; | |
4620f8c1 | 2568 | virtpages -= (end - start) >> PAGE_SHIFT; |
6e5fb223 | 2569 | |
9f40604c | 2570 | start = end; |
4620f8c1 | 2571 | if (pages <= 0 || virtpages <= 0) |
9f40604c | 2572 | goto out; |
3cf1962c RR |
2573 | |
2574 | cond_resched(); | |
9f40604c | 2575 | } while (end != vma->vm_end); |
cbee9f88 | 2576 | } |
6e5fb223 | 2577 | |
9f40604c | 2578 | out: |
6e5fb223 | 2579 | /* |
c69307d5 PZ |
2580 | * It is possible to reach the end of the VMA list but the last few |
2581 | * VMAs are not guaranteed to the vma_migratable. If they are not, we | |
2582 | * would find the !migratable VMA on the next scan but not reset the | |
2583 | * scanner to the start so check it now. | |
6e5fb223 PZ |
2584 | */ |
2585 | if (vma) | |
9f40604c | 2586 | mm->numa_scan_offset = start; |
6e5fb223 PZ |
2587 | else |
2588 | reset_ptenuma_scan(p); | |
2589 | up_read(&mm->mmap_sem); | |
51170840 RR |
2590 | |
2591 | /* | |
2592 | * Make sure tasks use at least 32x as much time to run other code | |
2593 | * than they used here, to limit NUMA PTE scanning overhead to 3% max. | |
2594 | * Usually update_task_scan_period slows down scanning enough; on an | |
2595 | * overloaded system we need to limit overhead on a per task basis. | |
2596 | */ | |
2597 | if (unlikely(p->se.sum_exec_runtime != runtime)) { | |
2598 | u64 diff = p->se.sum_exec_runtime - runtime; | |
2599 | p->node_stamp += 32 * diff; | |
2600 | } | |
cbee9f88 PZ |
2601 | } |
2602 | ||
2603 | /* | |
2604 | * Drive the periodic memory faults.. | |
2605 | */ | |
2606 | void task_tick_numa(struct rq *rq, struct task_struct *curr) | |
2607 | { | |
2608 | struct callback_head *work = &curr->numa_work; | |
2609 | u64 period, now; | |
2610 | ||
2611 | /* | |
2612 | * We don't care about NUMA placement if we don't have memory. | |
2613 | */ | |
2614 | if (!curr->mm || (curr->flags & PF_EXITING) || work->next != work) | |
2615 | return; | |
2616 | ||
2617 | /* | |
2618 | * Using runtime rather than walltime has the dual advantage that | |
2619 | * we (mostly) drive the selection from busy threads and that the | |
2620 | * task needs to have done some actual work before we bother with | |
2621 | * NUMA placement. | |
2622 | */ | |
2623 | now = curr->se.sum_exec_runtime; | |
2624 | period = (u64)curr->numa_scan_period * NSEC_PER_MSEC; | |
2625 | ||
25b3e5a3 | 2626 | if (now > curr->node_stamp + period) { |
4b96a29b | 2627 | if (!curr->node_stamp) |
b5dd77c8 | 2628 | curr->numa_scan_period = task_scan_start(curr); |
19a78d11 | 2629 | curr->node_stamp += period; |
cbee9f88 PZ |
2630 | |
2631 | if (!time_before(jiffies, curr->mm->numa_next_scan)) { | |
2632 | init_task_work(work, task_numa_work); /* TODO: move this into sched_fork() */ | |
2633 | task_work_add(curr, work, true); | |
2634 | } | |
2635 | } | |
2636 | } | |
3fed382b | 2637 | |
cbee9f88 PZ |
2638 | #else |
2639 | static void task_tick_numa(struct rq *rq, struct task_struct *curr) | |
2640 | { | |
2641 | } | |
0ec8aa00 PZ |
2642 | |
2643 | static inline void account_numa_enqueue(struct rq *rq, struct task_struct *p) | |
2644 | { | |
2645 | } | |
2646 | ||
2647 | static inline void account_numa_dequeue(struct rq *rq, struct task_struct *p) | |
2648 | { | |
2649 | } | |
3fed382b | 2650 | |
cbee9f88 PZ |
2651 | #endif /* CONFIG_NUMA_BALANCING */ |
2652 | ||
30cfdcfc DA |
2653 | static void |
2654 | account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
2655 | { | |
2656 | update_load_add(&cfs_rq->load, se->load.weight); | |
c09595f6 | 2657 | if (!parent_entity(se)) |
029632fb | 2658 | update_load_add(&rq_of(cfs_rq)->load, se->load.weight); |
367456c7 | 2659 | #ifdef CONFIG_SMP |
0ec8aa00 PZ |
2660 | if (entity_is_task(se)) { |
2661 | struct rq *rq = rq_of(cfs_rq); | |
2662 | ||
2663 | account_numa_enqueue(rq, task_of(se)); | |
2664 | list_add(&se->group_node, &rq->cfs_tasks); | |
2665 | } | |
367456c7 | 2666 | #endif |
30cfdcfc | 2667 | cfs_rq->nr_running++; |
30cfdcfc DA |
2668 | } |
2669 | ||
2670 | static void | |
2671 | account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
2672 | { | |
2673 | update_load_sub(&cfs_rq->load, se->load.weight); | |
c09595f6 | 2674 | if (!parent_entity(se)) |
029632fb | 2675 | update_load_sub(&rq_of(cfs_rq)->load, se->load.weight); |
bfdb198c | 2676 | #ifdef CONFIG_SMP |
0ec8aa00 PZ |
2677 | if (entity_is_task(se)) { |
2678 | account_numa_dequeue(rq_of(cfs_rq), task_of(se)); | |
b87f1724 | 2679 | list_del_init(&se->group_node); |
0ec8aa00 | 2680 | } |
bfdb198c | 2681 | #endif |
30cfdcfc | 2682 | cfs_rq->nr_running--; |
30cfdcfc DA |
2683 | } |
2684 | ||
8d5b9025 PZ |
2685 | /* |
2686 | * Signed add and clamp on underflow. | |
2687 | * | |
2688 | * Explicitly do a load-store to ensure the intermediate value never hits | |
2689 | * memory. This allows lockless observations without ever seeing the negative | |
2690 | * values. | |
2691 | */ | |
2692 | #define add_positive(_ptr, _val) do { \ | |
2693 | typeof(_ptr) ptr = (_ptr); \ | |
2694 | typeof(_val) val = (_val); \ | |
2695 | typeof(*ptr) res, var = READ_ONCE(*ptr); \ | |
2696 | \ | |
2697 | res = var + val; \ | |
2698 | \ | |
2699 | if (val < 0 && res > var) \ | |
2700 | res = 0; \ | |
2701 | \ | |
2702 | WRITE_ONCE(*ptr, res); \ | |
2703 | } while (0) | |
2704 | ||
2705 | /* | |
2706 | * Unsigned subtract and clamp on underflow. | |
2707 | * | |
2708 | * Explicitly do a load-store to ensure the intermediate value never hits | |
2709 | * memory. This allows lockless observations without ever seeing the negative | |
2710 | * values. | |
2711 | */ | |
2712 | #define sub_positive(_ptr, _val) do { \ | |
2713 | typeof(_ptr) ptr = (_ptr); \ | |
2714 | typeof(*ptr) val = (_val); \ | |
2715 | typeof(*ptr) res, var = READ_ONCE(*ptr); \ | |
2716 | res = var - val; \ | |
2717 | if (res > var) \ | |
2718 | res = 0; \ | |
2719 | WRITE_ONCE(*ptr, res); \ | |
2720 | } while (0) | |
2721 | ||
2722 | #ifdef CONFIG_SMP | |
2723 | /* | |
1ea6c46a | 2724 | * XXX we want to get rid of these helpers and use the full load resolution. |
8d5b9025 PZ |
2725 | */ |
2726 | static inline long se_weight(struct sched_entity *se) | |
2727 | { | |
2728 | return scale_load_down(se->load.weight); | |
2729 | } | |
2730 | ||
1ea6c46a PZ |
2731 | static inline long se_runnable(struct sched_entity *se) |
2732 | { | |
2733 | return scale_load_down(se->runnable_weight); | |
2734 | } | |
2735 | ||
8d5b9025 PZ |
2736 | static inline void |
2737 | enqueue_runnable_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
2738 | { | |
1ea6c46a PZ |
2739 | cfs_rq->runnable_weight += se->runnable_weight; |
2740 | ||
2741 | cfs_rq->avg.runnable_load_avg += se->avg.runnable_load_avg; | |
2742 | cfs_rq->avg.runnable_load_sum += se_runnable(se) * se->avg.runnable_load_sum; | |
8d5b9025 PZ |
2743 | } |
2744 | ||
2745 | static inline void | |
2746 | dequeue_runnable_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
2747 | { | |
1ea6c46a PZ |
2748 | cfs_rq->runnable_weight -= se->runnable_weight; |
2749 | ||
2750 | sub_positive(&cfs_rq->avg.runnable_load_avg, se->avg.runnable_load_avg); | |
2751 | sub_positive(&cfs_rq->avg.runnable_load_sum, | |
2752 | se_runnable(se) * se->avg.runnable_load_sum); | |
8d5b9025 PZ |
2753 | } |
2754 | ||
2755 | static inline void | |
2756 | enqueue_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
2757 | { | |
2758 | cfs_rq->avg.load_avg += se->avg.load_avg; | |
2759 | cfs_rq->avg.load_sum += se_weight(se) * se->avg.load_sum; | |
2760 | } | |
2761 | ||
2762 | static inline void | |
2763 | dequeue_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
2764 | { | |
2765 | sub_positive(&cfs_rq->avg.load_avg, se->avg.load_avg); | |
2766 | sub_positive(&cfs_rq->avg.load_sum, se_weight(se) * se->avg.load_sum); | |
2767 | } | |
2768 | #else | |
2769 | static inline void | |
2770 | enqueue_runnable_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) { } | |
2771 | static inline void | |
2772 | dequeue_runnable_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) { } | |
2773 | static inline void | |
2774 | enqueue_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) { } | |
2775 | static inline void | |
2776 | dequeue_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) { } | |
2777 | #endif | |
2778 | ||
9059393e | 2779 | static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, |
1ea6c46a | 2780 | unsigned long weight, unsigned long runnable) |
9059393e VG |
2781 | { |
2782 | if (se->on_rq) { | |
2783 | /* commit outstanding execution time */ | |
2784 | if (cfs_rq->curr == se) | |
2785 | update_curr(cfs_rq); | |
2786 | account_entity_dequeue(cfs_rq, se); | |
2787 | dequeue_runnable_load_avg(cfs_rq, se); | |
2788 | } | |
2789 | dequeue_load_avg(cfs_rq, se); | |
2790 | ||
1ea6c46a | 2791 | se->runnable_weight = runnable; |
9059393e VG |
2792 | update_load_set(&se->load, weight); |
2793 | ||
2794 | #ifdef CONFIG_SMP | |
1ea6c46a PZ |
2795 | do { |
2796 | u32 divider = LOAD_AVG_MAX - 1024 + se->avg.period_contrib; | |
2797 | ||
2798 | se->avg.load_avg = div_u64(se_weight(se) * se->avg.load_sum, divider); | |
2799 | se->avg.runnable_load_avg = | |
2800 | div_u64(se_runnable(se) * se->avg.runnable_load_sum, divider); | |
2801 | } while (0); | |
9059393e VG |
2802 | #endif |
2803 | ||
2804 | enqueue_load_avg(cfs_rq, se); | |
2805 | if (se->on_rq) { | |
2806 | account_entity_enqueue(cfs_rq, se); | |
2807 | enqueue_runnable_load_avg(cfs_rq, se); | |
2808 | } | |
2809 | } | |
2810 | ||
2811 | void reweight_task(struct task_struct *p, int prio) | |
2812 | { | |
2813 | struct sched_entity *se = &p->se; | |
2814 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
2815 | struct load_weight *load = &se->load; | |
2816 | unsigned long weight = scale_load(sched_prio_to_weight[prio]); | |
2817 | ||
1ea6c46a | 2818 | reweight_entity(cfs_rq, se, weight, weight); |
9059393e VG |
2819 | load->inv_weight = sched_prio_to_wmult[prio]; |
2820 | } | |
2821 | ||
3ff6dcac | 2822 | #ifdef CONFIG_FAIR_GROUP_SCHED |
387f77cc | 2823 | #ifdef CONFIG_SMP |
cef27403 PZ |
2824 | /* |
2825 | * All this does is approximate the hierarchical proportion which includes that | |
2826 | * global sum we all love to hate. | |
2827 | * | |
2828 | * That is, the weight of a group entity, is the proportional share of the | |
2829 | * group weight based on the group runqueue weights. That is: | |
2830 | * | |
2831 | * tg->weight * grq->load.weight | |
2832 | * ge->load.weight = ----------------------------- (1) | |
2833 | * \Sum grq->load.weight | |
2834 | * | |
2835 | * Now, because computing that sum is prohibitively expensive to compute (been | |
2836 | * there, done that) we approximate it with this average stuff. The average | |
2837 | * moves slower and therefore the approximation is cheaper and more stable. | |
2838 | * | |
2839 | * So instead of the above, we substitute: | |
2840 | * | |
2841 | * grq->load.weight -> grq->avg.load_avg (2) | |
2842 | * | |
2843 | * which yields the following: | |
2844 | * | |
2845 | * tg->weight * grq->avg.load_avg | |
2846 | * ge->load.weight = ------------------------------ (3) | |
2847 | * tg->load_avg | |
2848 | * | |
2849 | * Where: tg->load_avg ~= \Sum grq->avg.load_avg | |
2850 | * | |
2851 | * That is shares_avg, and it is right (given the approximation (2)). | |
2852 | * | |
2853 | * The problem with it is that because the average is slow -- it was designed | |
2854 | * to be exactly that of course -- this leads to transients in boundary | |
2855 | * conditions. In specific, the case where the group was idle and we start the | |
2856 | * one task. It takes time for our CPU's grq->avg.load_avg to build up, | |
2857 | * yielding bad latency etc.. | |
2858 | * | |
2859 | * Now, in that special case (1) reduces to: | |
2860 | * | |
2861 | * tg->weight * grq->load.weight | |
17de4ee0 | 2862 | * ge->load.weight = ----------------------------- = tg->weight (4) |
cef27403 PZ |
2863 | * grp->load.weight |
2864 | * | |
2865 | * That is, the sum collapses because all other CPUs are idle; the UP scenario. | |
2866 | * | |
2867 | * So what we do is modify our approximation (3) to approach (4) in the (near) | |
2868 | * UP case, like: | |
2869 | * | |
2870 | * ge->load.weight = | |
2871 | * | |
2872 | * tg->weight * grq->load.weight | |
2873 | * --------------------------------------------------- (5) | |
2874 | * tg->load_avg - grq->avg.load_avg + grq->load.weight | |
2875 | * | |
17de4ee0 PZ |
2876 | * But because grq->load.weight can drop to 0, resulting in a divide by zero, |
2877 | * we need to use grq->avg.load_avg as its lower bound, which then gives: | |
2878 | * | |
2879 | * | |
2880 | * tg->weight * grq->load.weight | |
2881 | * ge->load.weight = ----------------------------- (6) | |
2882 | * tg_load_avg' | |
2883 | * | |
2884 | * Where: | |
2885 | * | |
2886 | * tg_load_avg' = tg->load_avg - grq->avg.load_avg + | |
2887 | * max(grq->load.weight, grq->avg.load_avg) | |
cef27403 PZ |
2888 | * |
2889 | * And that is shares_weight and is icky. In the (near) UP case it approaches | |
2890 | * (4) while in the normal case it approaches (3). It consistently | |
2891 | * overestimates the ge->load.weight and therefore: | |
2892 | * | |
2893 | * \Sum ge->load.weight >= tg->weight | |
2894 | * | |
2895 | * hence icky! | |
2896 | */ | |
2c8e4dce | 2897 | static long calc_group_shares(struct cfs_rq *cfs_rq) |
cf5f0acf | 2898 | { |
7c80cfc9 PZ |
2899 | long tg_weight, tg_shares, load, shares; |
2900 | struct task_group *tg = cfs_rq->tg; | |
2901 | ||
2902 | tg_shares = READ_ONCE(tg->shares); | |
cf5f0acf | 2903 | |
3d4b60d3 | 2904 | load = max(scale_load_down(cfs_rq->load.weight), cfs_rq->avg.load_avg); |
cf5f0acf | 2905 | |
ea1dc6fc | 2906 | tg_weight = atomic_long_read(&tg->load_avg); |
3ff6dcac | 2907 | |
ea1dc6fc PZ |
2908 | /* Ensure tg_weight >= load */ |
2909 | tg_weight -= cfs_rq->tg_load_avg_contrib; | |
2910 | tg_weight += load; | |
3ff6dcac | 2911 | |
7c80cfc9 | 2912 | shares = (tg_shares * load); |
cf5f0acf PZ |
2913 | if (tg_weight) |
2914 | shares /= tg_weight; | |
3ff6dcac | 2915 | |
b8fd8423 DE |
2916 | /* |
2917 | * MIN_SHARES has to be unscaled here to support per-CPU partitioning | |
2918 | * of a group with small tg->shares value. It is a floor value which is | |
2919 | * assigned as a minimum load.weight to the sched_entity representing | |
2920 | * the group on a CPU. | |
2921 | * | |
2922 | * E.g. on 64-bit for a group with tg->shares of scale_load(15)=15*1024 | |
2923 | * on an 8-core system with 8 tasks each runnable on one CPU shares has | |
2924 | * to be 15*1024*1/8=1920 instead of scale_load(MIN_SHARES)=2*1024. In | |
2925 | * case no task is runnable on a CPU MIN_SHARES=2 should be returned | |
2926 | * instead of 0. | |
2927 | */ | |
7c80cfc9 | 2928 | return clamp_t(long, shares, MIN_SHARES, tg_shares); |
3ff6dcac | 2929 | } |
2c8e4dce JB |
2930 | |
2931 | /* | |
17de4ee0 PZ |
2932 | * This calculates the effective runnable weight for a group entity based on |
2933 | * the group entity weight calculated above. | |
2934 | * | |
2935 | * Because of the above approximation (2), our group entity weight is | |
2936 | * an load_avg based ratio (3). This means that it includes blocked load and | |
2937 | * does not represent the runnable weight. | |
2938 | * | |
2939 | * Approximate the group entity's runnable weight per ratio from the group | |
2940 | * runqueue: | |
2941 | * | |
2942 | * grq->avg.runnable_load_avg | |
2943 | * ge->runnable_weight = ge->load.weight * -------------------------- (7) | |
2944 | * grq->avg.load_avg | |
2945 | * | |
2946 | * However, analogous to above, since the avg numbers are slow, this leads to | |
2947 | * transients in the from-idle case. Instead we use: | |
2948 | * | |
2949 | * ge->runnable_weight = ge->load.weight * | |
2950 | * | |
2951 | * max(grq->avg.runnable_load_avg, grq->runnable_weight) | |
2952 | * ----------------------------------------------------- (8) | |
2953 | * max(grq->avg.load_avg, grq->load.weight) | |
2954 | * | |
2955 | * Where these max() serve both to use the 'instant' values to fix the slow | |
2956 | * from-idle and avoid the /0 on to-idle, similar to (6). | |
2c8e4dce JB |
2957 | */ |
2958 | static long calc_group_runnable(struct cfs_rq *cfs_rq, long shares) | |
2959 | { | |
17de4ee0 PZ |
2960 | long runnable, load_avg; |
2961 | ||
2962 | load_avg = max(cfs_rq->avg.load_avg, | |
2963 | scale_load_down(cfs_rq->load.weight)); | |
2964 | ||
2965 | runnable = max(cfs_rq->avg.runnable_load_avg, | |
2966 | scale_load_down(cfs_rq->runnable_weight)); | |
2c8e4dce JB |
2967 | |
2968 | runnable *= shares; | |
2969 | if (load_avg) | |
2970 | runnable /= load_avg; | |
17de4ee0 | 2971 | |
2c8e4dce JB |
2972 | return clamp_t(long, runnable, MIN_SHARES, shares); |
2973 | } | |
387f77cc | 2974 | #endif /* CONFIG_SMP */ |
ea1dc6fc | 2975 | |
82958366 PT |
2976 | static inline int throttled_hierarchy(struct cfs_rq *cfs_rq); |
2977 | ||
1ea6c46a PZ |
2978 | /* |
2979 | * Recomputes the group entity based on the current state of its group | |
2980 | * runqueue. | |
2981 | */ | |
2982 | static void update_cfs_group(struct sched_entity *se) | |
2069dd75 | 2983 | { |
1ea6c46a PZ |
2984 | struct cfs_rq *gcfs_rq = group_cfs_rq(se); |
2985 | long shares, runnable; | |
2069dd75 | 2986 | |
1ea6c46a | 2987 | if (!gcfs_rq) |
89ee048f VG |
2988 | return; |
2989 | ||
1ea6c46a | 2990 | if (throttled_hierarchy(gcfs_rq)) |
2069dd75 | 2991 | return; |
89ee048f | 2992 | |
3ff6dcac | 2993 | #ifndef CONFIG_SMP |
1ea6c46a | 2994 | runnable = shares = READ_ONCE(gcfs_rq->tg->shares); |
7c80cfc9 PZ |
2995 | |
2996 | if (likely(se->load.weight == shares)) | |
3ff6dcac | 2997 | return; |
7c80cfc9 | 2998 | #else |
2c8e4dce JB |
2999 | shares = calc_group_shares(gcfs_rq); |
3000 | runnable = calc_group_runnable(gcfs_rq, shares); | |
3ff6dcac | 3001 | #endif |
2069dd75 | 3002 | |
1ea6c46a | 3003 | reweight_entity(cfs_rq_of(se), se, shares, runnable); |
2069dd75 | 3004 | } |
89ee048f | 3005 | |
2069dd75 | 3006 | #else /* CONFIG_FAIR_GROUP_SCHED */ |
1ea6c46a | 3007 | static inline void update_cfs_group(struct sched_entity *se) |
2069dd75 PZ |
3008 | { |
3009 | } | |
3010 | #endif /* CONFIG_FAIR_GROUP_SCHED */ | |
3011 | ||
ea14b57e | 3012 | static inline void cfs_rq_util_change(struct cfs_rq *cfs_rq, int flags) |
a030d738 | 3013 | { |
43964409 LT |
3014 | struct rq *rq = rq_of(cfs_rq); |
3015 | ||
ea14b57e | 3016 | if (&rq->cfs == cfs_rq || (flags & SCHED_CPUFREQ_MIGRATION)) { |
a030d738 VK |
3017 | /* |
3018 | * There are a few boundary cases this might miss but it should | |
3019 | * get called often enough that that should (hopefully) not be | |
9783be2c | 3020 | * a real problem. |
a030d738 VK |
3021 | * |
3022 | * It will not get called when we go idle, because the idle | |
3023 | * thread is a different class (!fair), nor will the utilization | |
3024 | * number include things like RT tasks. | |
3025 | * | |
3026 | * As is, the util number is not freq-invariant (we'd have to | |
3027 | * implement arch_scale_freq_capacity() for that). | |
3028 | * | |
3029 | * See cpu_util(). | |
3030 | */ | |
ea14b57e | 3031 | cpufreq_update_util(rq, flags); |
a030d738 VK |
3032 | } |
3033 | } | |
3034 | ||
141965c7 | 3035 | #ifdef CONFIG_SMP |
9d85f21c PT |
3036 | /* |
3037 | * Approximate: | |
3038 | * val * y^n, where y^32 ~= 0.5 (~1 scheduling period) | |
3039 | */ | |
a481db34 | 3040 | static u64 decay_load(u64 val, u64 n) |
9d85f21c | 3041 | { |
5b51f2f8 PT |
3042 | unsigned int local_n; |
3043 | ||
05296e75 | 3044 | if (unlikely(n > LOAD_AVG_PERIOD * 63)) |
5b51f2f8 PT |
3045 | return 0; |
3046 | ||
3047 | /* after bounds checking we can collapse to 32-bit */ | |
3048 | local_n = n; | |
3049 | ||
3050 | /* | |
3051 | * As y^PERIOD = 1/2, we can combine | |
9c58c79a ZZ |
3052 | * y^n = 1/2^(n/PERIOD) * y^(n%PERIOD) |
3053 | * With a look-up table which covers y^n (n<PERIOD) | |
5b51f2f8 PT |
3054 | * |
3055 | * To achieve constant time decay_load. | |
3056 | */ | |
3057 | if (unlikely(local_n >= LOAD_AVG_PERIOD)) { | |
3058 | val >>= local_n / LOAD_AVG_PERIOD; | |
3059 | local_n %= LOAD_AVG_PERIOD; | |
9d85f21c PT |
3060 | } |
3061 | ||
9d89c257 YD |
3062 | val = mul_u64_u32_shr(val, runnable_avg_yN_inv[local_n], 32); |
3063 | return val; | |
5b51f2f8 PT |
3064 | } |
3065 | ||
05296e75 | 3066 | static u32 __accumulate_pelt_segments(u64 periods, u32 d1, u32 d3) |
5b51f2f8 | 3067 | { |
05296e75 | 3068 | u32 c1, c2, c3 = d3; /* y^0 == 1 */ |
5b51f2f8 | 3069 | |
a481db34 | 3070 | /* |
3841cdc3 | 3071 | * c1 = d1 y^p |
a481db34 | 3072 | */ |
05296e75 | 3073 | c1 = decay_load((u64)d1, periods); |
a481db34 | 3074 | |
a481db34 | 3075 | /* |
3841cdc3 | 3076 | * p-1 |
05296e75 PZ |
3077 | * c2 = 1024 \Sum y^n |
3078 | * n=1 | |
a481db34 | 3079 | * |
05296e75 PZ |
3080 | * inf inf |
3081 | * = 1024 ( \Sum y^n - \Sum y^n - y^0 ) | |
3841cdc3 | 3082 | * n=0 n=p |
a481db34 | 3083 | */ |
05296e75 | 3084 | c2 = LOAD_AVG_MAX - decay_load(LOAD_AVG_MAX, periods) - 1024; |
a481db34 YD |
3085 | |
3086 | return c1 + c2 + c3; | |
9d85f21c PT |
3087 | } |
3088 | ||
a481db34 YD |
3089 | /* |
3090 | * Accumulate the three separate parts of the sum; d1 the remainder | |
3091 | * of the last (incomplete) period, d2 the span of full periods and d3 | |
3092 | * the remainder of the (incomplete) current period. | |
3093 | * | |
3094 | * d1 d2 d3 | |
3095 | * ^ ^ ^ | |
3096 | * | | | | |
3097 | * |<->|<----------------->|<--->| | |
3098 | * ... |---x---|------| ... |------|-----x (now) | |
3099 | * | |
3841cdc3 PZ |
3100 | * p-1 |
3101 | * u' = (u + d1) y^p + 1024 \Sum y^n + d3 y^0 | |
3102 | * n=1 | |
a481db34 | 3103 | * |
3841cdc3 | 3104 | * = u y^p + (Step 1) |
a481db34 | 3105 | * |
3841cdc3 PZ |
3106 | * p-1 |
3107 | * d1 y^p + 1024 \Sum y^n + d3 y^0 (Step 2) | |
3108 | * n=1 | |
a481db34 YD |
3109 | */ |
3110 | static __always_inline u32 | |
3111 | accumulate_sum(u64 delta, int cpu, struct sched_avg *sa, | |
1ea6c46a | 3112 | unsigned long load, unsigned long runnable, int running) |
a481db34 YD |
3113 | { |
3114 | unsigned long scale_freq, scale_cpu; | |
05296e75 | 3115 | u32 contrib = (u32)delta; /* p == 0 -> delta < 1024 */ |
a481db34 | 3116 | u64 periods; |
a481db34 | 3117 | |
7673c8a4 | 3118 | scale_freq = arch_scale_freq_capacity(cpu); |
a481db34 YD |
3119 | scale_cpu = arch_scale_cpu_capacity(NULL, cpu); |
3120 | ||
3121 | delta += sa->period_contrib; | |
3122 | periods = delta / 1024; /* A period is 1024us (~1ms) */ | |
3123 | ||
3124 | /* | |
3125 | * Step 1: decay old *_sum if we crossed period boundaries. | |
3126 | */ | |
3127 | if (periods) { | |
3128 | sa->load_sum = decay_load(sa->load_sum, periods); | |
1ea6c46a PZ |
3129 | sa->runnable_load_sum = |
3130 | decay_load(sa->runnable_load_sum, periods); | |
a481db34 | 3131 | sa->util_sum = decay_load((u64)(sa->util_sum), periods); |
a481db34 | 3132 | |
05296e75 PZ |
3133 | /* |
3134 | * Step 2 | |
3135 | */ | |
3136 | delta %= 1024; | |
3137 | contrib = __accumulate_pelt_segments(periods, | |
3138 | 1024 - sa->period_contrib, delta); | |
3139 | } | |
a481db34 YD |
3140 | sa->period_contrib = delta; |
3141 | ||
3142 | contrib = cap_scale(contrib, scale_freq); | |
1ea6c46a PZ |
3143 | if (load) |
3144 | sa->load_sum += load * contrib; | |
3145 | if (runnable) | |
3146 | sa->runnable_load_sum += runnable * contrib; | |
a481db34 YD |
3147 | if (running) |
3148 | sa->util_sum += contrib * scale_cpu; | |
3149 | ||
3150 | return periods; | |
3151 | } | |
3152 | ||
9d85f21c PT |
3153 | /* |
3154 | * We can represent the historical contribution to runnable average as the | |
3155 | * coefficients of a geometric series. To do this we sub-divide our runnable | |
3156 | * history into segments of approximately 1ms (1024us); label the segment that | |
3157 | * occurred N-ms ago p_N, with p_0 corresponding to the current period, e.g. | |
3158 | * | |
3159 | * [<- 1024us ->|<- 1024us ->|<- 1024us ->| ... | |
3160 | * p0 p1 p2 | |
3161 | * (now) (~1ms ago) (~2ms ago) | |
3162 | * | |
3163 | * Let u_i denote the fraction of p_i that the entity was runnable. | |
3164 | * | |
3165 | * We then designate the fractions u_i as our co-efficients, yielding the | |
3166 | * following representation of historical load: | |
3167 | * u_0 + u_1*y + u_2*y^2 + u_3*y^3 + ... | |
3168 | * | |
3169 | * We choose y based on the with of a reasonably scheduling period, fixing: | |
3170 | * y^32 = 0.5 | |
3171 | * | |
3172 | * This means that the contribution to load ~32ms ago (u_32) will be weighted | |
3173 | * approximately half as much as the contribution to load within the last ms | |
3174 | * (u_0). | |
3175 | * | |
3176 | * When a period "rolls over" and we have new u_0`, multiplying the previous | |
3177 | * sum again by y is sufficient to update: | |
3178 | * load_avg = u_0` + y*(u_0 + u_1*y + u_2*y^2 + ... ) | |
3179 | * = u_0 + u_1*y + u_2*y^2 + ... [re-labeling u_i --> u_{i+1}] | |
3180 | */ | |
9d89c257 | 3181 | static __always_inline int |
c7b50216 | 3182 | ___update_load_sum(u64 now, int cpu, struct sched_avg *sa, |
1ea6c46a | 3183 | unsigned long load, unsigned long runnable, int running) |
9d85f21c | 3184 | { |
a481db34 | 3185 | u64 delta; |
9d85f21c | 3186 | |
9d89c257 | 3187 | delta = now - sa->last_update_time; |
9d85f21c PT |
3188 | /* |
3189 | * This should only happen when time goes backwards, which it | |
3190 | * unfortunately does during sched clock init when we swap over to TSC. | |
3191 | */ | |
3192 | if ((s64)delta < 0) { | |
9d89c257 | 3193 | sa->last_update_time = now; |
9d85f21c PT |
3194 | return 0; |
3195 | } | |
3196 | ||
3197 | /* | |
3198 | * Use 1024ns as the unit of measurement since it's a reasonable | |
3199 | * approximation of 1us and fast to compute. | |
3200 | */ | |
3201 | delta >>= 10; | |
3202 | if (!delta) | |
3203 | return 0; | |
bb0bd044 PZ |
3204 | |
3205 | sa->last_update_time += delta << 10; | |
9d85f21c | 3206 | |
f235a54f VG |
3207 | /* |
3208 | * running is a subset of runnable (weight) so running can't be set if | |
3209 | * runnable is clear. But there are some corner cases where the current | |
3210 | * se has been already dequeued but cfs_rq->curr still points to it. | |
3211 | * This means that weight will be 0 but not running for a sched_entity | |
3212 | * but also for a cfs_rq if the latter becomes idle. As an example, | |
3213 | * this happens during idle_balance() which calls | |
3214 | * update_blocked_averages() | |
3215 | */ | |
1ea6c46a PZ |
3216 | if (!load) |
3217 | runnable = running = 0; | |
f235a54f | 3218 | |
a481db34 YD |
3219 | /* |
3220 | * Now we know we crossed measurement unit boundaries. The *_avg | |
3221 | * accrues by two steps: | |
3222 | * | |
3223 | * Step 1: accumulate *_sum since last_update_time. If we haven't | |
3224 | * crossed period boundaries, finish. | |
3225 | */ | |
1ea6c46a | 3226 | if (!accumulate_sum(delta, cpu, sa, load, runnable, running)) |
a481db34 | 3227 | return 0; |
9ee474f5 | 3228 | |
c7b50216 PZ |
3229 | return 1; |
3230 | } | |
3231 | ||
3232 | static __always_inline void | |
1ea6c46a | 3233 | ___update_load_avg(struct sched_avg *sa, unsigned long load, unsigned long runnable) |
c7b50216 PZ |
3234 | { |
3235 | u32 divider = LOAD_AVG_MAX - 1024 + sa->period_contrib; | |
3236 | ||
a481db34 YD |
3237 | /* |
3238 | * Step 2: update *_avg. | |
3239 | */ | |
1ea6c46a PZ |
3240 | sa->load_avg = div_u64(load * sa->load_sum, divider); |
3241 | sa->runnable_load_avg = div_u64(runnable * sa->runnable_load_sum, divider); | |
c7b50216 PZ |
3242 | sa->util_avg = sa->util_sum / divider; |
3243 | } | |
aff3e498 | 3244 | |
c7b50216 PZ |
3245 | /* |
3246 | * sched_entity: | |
3247 | * | |
1ea6c46a PZ |
3248 | * task: |
3249 | * se_runnable() == se_weight() | |
3250 | * | |
3251 | * group: [ see update_cfs_group() ] | |
3252 | * se_weight() = tg->weight * grq->load_avg / tg->load_avg | |
3253 | * se_runnable() = se_weight(se) * grq->runnable_load_avg / grq->load_avg | |
3254 | * | |
c7b50216 PZ |
3255 | * load_sum := runnable_sum |
3256 | * load_avg = se_weight(se) * runnable_avg | |
3257 | * | |
1ea6c46a PZ |
3258 | * runnable_load_sum := runnable_sum |
3259 | * runnable_load_avg = se_runnable(se) * runnable_avg | |
3260 | * | |
3261 | * XXX collapse load_sum and runnable_load_sum | |
3262 | * | |
c7b50216 PZ |
3263 | * cfq_rs: |
3264 | * | |
3265 | * load_sum = \Sum se_weight(se) * se->avg.load_sum | |
3266 | * load_avg = \Sum se->avg.load_avg | |
1ea6c46a PZ |
3267 | * |
3268 | * runnable_load_sum = \Sum se_runnable(se) * se->avg.runnable_load_sum | |
3269 | * runnable_load_avg = \Sum se->avg.runable_load_avg | |
c7b50216 PZ |
3270 | */ |
3271 | ||
0ccb977f PZ |
3272 | static int |
3273 | __update_load_avg_blocked_se(u64 now, int cpu, struct sched_entity *se) | |
3274 | { | |
1ea6c46a PZ |
3275 | if (entity_is_task(se)) |
3276 | se->runnable_weight = se->load.weight; | |
3277 | ||
3278 | if (___update_load_sum(now, cpu, &se->avg, 0, 0, 0)) { | |
3279 | ___update_load_avg(&se->avg, se_weight(se), se_runnable(se)); | |
c7b50216 PZ |
3280 | return 1; |
3281 | } | |
3282 | ||
3283 | return 0; | |
0ccb977f PZ |
3284 | } |
3285 | ||
3286 | static int | |
3287 | __update_load_avg_se(u64 now, int cpu, struct cfs_rq *cfs_rq, struct sched_entity *se) | |
3288 | { | |
1ea6c46a PZ |
3289 | if (entity_is_task(se)) |
3290 | se->runnable_weight = se->load.weight; | |
3291 | ||
3292 | if (___update_load_sum(now, cpu, &se->avg, !!se->on_rq, !!se->on_rq, | |
3293 | cfs_rq->curr == se)) { | |
c7b50216 | 3294 | |
1ea6c46a | 3295 | ___update_load_avg(&se->avg, se_weight(se), se_runnable(se)); |
c7b50216 PZ |
3296 | return 1; |
3297 | } | |
3298 | ||
3299 | return 0; | |
0ccb977f PZ |
3300 | } |
3301 | ||
3302 | static int | |
3303 | __update_load_avg_cfs_rq(u64 now, int cpu, struct cfs_rq *cfs_rq) | |
3304 | { | |
c7b50216 PZ |
3305 | if (___update_load_sum(now, cpu, &cfs_rq->avg, |
3306 | scale_load_down(cfs_rq->load.weight), | |
1ea6c46a PZ |
3307 | scale_load_down(cfs_rq->runnable_weight), |
3308 | cfs_rq->curr != NULL)) { | |
3309 | ||
3310 | ___update_load_avg(&cfs_rq->avg, 1, 1); | |
c7b50216 PZ |
3311 | return 1; |
3312 | } | |
3313 | ||
3314 | return 0; | |
0ccb977f PZ |
3315 | } |
3316 | ||
c566e8e9 | 3317 | #ifdef CONFIG_FAIR_GROUP_SCHED |
7c3edd2c PZ |
3318 | /** |
3319 | * update_tg_load_avg - update the tg's load avg | |
3320 | * @cfs_rq: the cfs_rq whose avg changed | |
3321 | * @force: update regardless of how small the difference | |
3322 | * | |
3323 | * This function 'ensures': tg->load_avg := \Sum tg->cfs_rq[]->avg.load. | |
3324 | * However, because tg->load_avg is a global value there are performance | |
3325 | * considerations. | |
3326 | * | |
3327 | * In order to avoid having to look at the other cfs_rq's, we use a | |
3328 | * differential update where we store the last value we propagated. This in | |
3329 | * turn allows skipping updates if the differential is 'small'. | |
3330 | * | |
815abf5a | 3331 | * Updating tg's load_avg is necessary before update_cfs_share(). |
bb17f655 | 3332 | */ |
9d89c257 | 3333 | static inline void update_tg_load_avg(struct cfs_rq *cfs_rq, int force) |
bb17f655 | 3334 | { |
9d89c257 | 3335 | long delta = cfs_rq->avg.load_avg - cfs_rq->tg_load_avg_contrib; |
bb17f655 | 3336 | |
aa0b7ae0 WL |
3337 | /* |
3338 | * No need to update load_avg for root_task_group as it is not used. | |
3339 | */ | |
3340 | if (cfs_rq->tg == &root_task_group) | |
3341 | return; | |
3342 | ||
9d89c257 YD |
3343 | if (force || abs(delta) > cfs_rq->tg_load_avg_contrib / 64) { |
3344 | atomic_long_add(delta, &cfs_rq->tg->load_avg); | |
3345 | cfs_rq->tg_load_avg_contrib = cfs_rq->avg.load_avg; | |
bb17f655 | 3346 | } |
8165e145 | 3347 | } |
f5f9739d | 3348 | |
ad936d86 | 3349 | /* |
97fb7a0a | 3350 | * Called within set_task_rq() right before setting a task's CPU. The |
ad936d86 BP |
3351 | * caller only guarantees p->pi_lock is held; no other assumptions, |
3352 | * including the state of rq->lock, should be made. | |
3353 | */ | |
3354 | void set_task_rq_fair(struct sched_entity *se, | |
3355 | struct cfs_rq *prev, struct cfs_rq *next) | |
3356 | { | |
0ccb977f PZ |
3357 | u64 p_last_update_time; |
3358 | u64 n_last_update_time; | |
3359 | ||
ad936d86 BP |
3360 | if (!sched_feat(ATTACH_AGE_LOAD)) |
3361 | return; | |
3362 | ||
3363 | /* | |
3364 | * We are supposed to update the task to "current" time, then its up to | |
3365 | * date and ready to go to new CPU/cfs_rq. But we have difficulty in | |
3366 | * getting what current time is, so simply throw away the out-of-date | |
3367 | * time. This will result in the wakee task is less decayed, but giving | |
3368 | * the wakee more load sounds not bad. | |
3369 | */ | |
0ccb977f PZ |
3370 | if (!(se->avg.last_update_time && prev)) |
3371 | return; | |
ad936d86 BP |
3372 | |
3373 | #ifndef CONFIG_64BIT | |
0ccb977f | 3374 | { |
ad936d86 BP |
3375 | u64 p_last_update_time_copy; |
3376 | u64 n_last_update_time_copy; | |
3377 | ||
3378 | do { | |
3379 | p_last_update_time_copy = prev->load_last_update_time_copy; | |
3380 | n_last_update_time_copy = next->load_last_update_time_copy; | |
3381 | ||
3382 | smp_rmb(); | |
3383 | ||
3384 | p_last_update_time = prev->avg.last_update_time; | |
3385 | n_last_update_time = next->avg.last_update_time; | |
3386 | ||
3387 | } while (p_last_update_time != p_last_update_time_copy || | |
3388 | n_last_update_time != n_last_update_time_copy); | |
0ccb977f | 3389 | } |
ad936d86 | 3390 | #else |
0ccb977f PZ |
3391 | p_last_update_time = prev->avg.last_update_time; |
3392 | n_last_update_time = next->avg.last_update_time; | |
ad936d86 | 3393 | #endif |
0ccb977f PZ |
3394 | __update_load_avg_blocked_se(p_last_update_time, cpu_of(rq_of(prev)), se); |
3395 | se->avg.last_update_time = n_last_update_time; | |
ad936d86 | 3396 | } |
09a43ace | 3397 | |
0e2d2aaa PZ |
3398 | |
3399 | /* | |
3400 | * When on migration a sched_entity joins/leaves the PELT hierarchy, we need to | |
3401 | * propagate its contribution. The key to this propagation is the invariant | |
3402 | * that for each group: | |
3403 | * | |
3404 | * ge->avg == grq->avg (1) | |
3405 | * | |
3406 | * _IFF_ we look at the pure running and runnable sums. Because they | |
3407 | * represent the very same entity, just at different points in the hierarchy. | |
3408 | * | |
a4c3c049 VG |
3409 | * Per the above update_tg_cfs_util() is trivial and simply copies the running |
3410 | * sum over (but still wrong, because the group entity and group rq do not have | |
3411 | * their PELT windows aligned). | |
0e2d2aaa PZ |
3412 | * |
3413 | * However, update_tg_cfs_runnable() is more complex. So we have: | |
3414 | * | |
3415 | * ge->avg.load_avg = ge->load.weight * ge->avg.runnable_avg (2) | |
3416 | * | |
3417 | * And since, like util, the runnable part should be directly transferable, | |
3418 | * the following would _appear_ to be the straight forward approach: | |
3419 | * | |
a4c3c049 | 3420 | * grq->avg.load_avg = grq->load.weight * grq->avg.runnable_avg (3) |
0e2d2aaa PZ |
3421 | * |
3422 | * And per (1) we have: | |
3423 | * | |
a4c3c049 | 3424 | * ge->avg.runnable_avg == grq->avg.runnable_avg |
0e2d2aaa PZ |
3425 | * |
3426 | * Which gives: | |
3427 | * | |
3428 | * ge->load.weight * grq->avg.load_avg | |
3429 | * ge->avg.load_avg = ----------------------------------- (4) | |
3430 | * grq->load.weight | |
3431 | * | |
3432 | * Except that is wrong! | |
3433 | * | |
3434 | * Because while for entities historical weight is not important and we | |
3435 | * really only care about our future and therefore can consider a pure | |
3436 | * runnable sum, runqueues can NOT do this. | |
3437 | * | |
3438 | * We specifically want runqueues to have a load_avg that includes | |
3439 | * historical weights. Those represent the blocked load, the load we expect | |
3440 | * to (shortly) return to us. This only works by keeping the weights as | |
3441 | * integral part of the sum. We therefore cannot decompose as per (3). | |
3442 | * | |
a4c3c049 VG |
3443 | * Another reason this doesn't work is that runnable isn't a 0-sum entity. |
3444 | * Imagine a rq with 2 tasks that each are runnable 2/3 of the time. Then the | |
3445 | * rq itself is runnable anywhere between 2/3 and 1 depending on how the | |
3446 | * runnable section of these tasks overlap (or not). If they were to perfectly | |
3447 | * align the rq as a whole would be runnable 2/3 of the time. If however we | |
3448 | * always have at least 1 runnable task, the rq as a whole is always runnable. | |
0e2d2aaa | 3449 | * |
a4c3c049 | 3450 | * So we'll have to approximate.. :/ |
0e2d2aaa | 3451 | * |
a4c3c049 | 3452 | * Given the constraint: |
0e2d2aaa | 3453 | * |
a4c3c049 | 3454 | * ge->avg.running_sum <= ge->avg.runnable_sum <= LOAD_AVG_MAX |
0e2d2aaa | 3455 | * |
a4c3c049 VG |
3456 | * We can construct a rule that adds runnable to a rq by assuming minimal |
3457 | * overlap. | |
0e2d2aaa | 3458 | * |
a4c3c049 | 3459 | * On removal, we'll assume each task is equally runnable; which yields: |
0e2d2aaa | 3460 | * |
a4c3c049 | 3461 | * grq->avg.runnable_sum = grq->avg.load_sum / grq->load.weight |
0e2d2aaa | 3462 | * |
a4c3c049 | 3463 | * XXX: only do this for the part of runnable > running ? |
0e2d2aaa | 3464 | * |
0e2d2aaa PZ |
3465 | */ |
3466 | ||
09a43ace | 3467 | static inline void |
0e2d2aaa | 3468 | update_tg_cfs_util(struct cfs_rq *cfs_rq, struct sched_entity *se, struct cfs_rq *gcfs_rq) |
09a43ace | 3469 | { |
09a43ace VG |
3470 | long delta = gcfs_rq->avg.util_avg - se->avg.util_avg; |
3471 | ||
3472 | /* Nothing to update */ | |
3473 | if (!delta) | |
3474 | return; | |
3475 | ||
a4c3c049 VG |
3476 | /* |
3477 | * The relation between sum and avg is: | |
3478 | * | |
3479 | * LOAD_AVG_MAX - 1024 + sa->period_contrib | |
3480 | * | |
3481 | * however, the PELT windows are not aligned between grq and gse. | |
3482 | */ | |
3483 | ||
09a43ace VG |
3484 | /* Set new sched_entity's utilization */ |
3485 | se->avg.util_avg = gcfs_rq->avg.util_avg; | |
3486 | se->avg.util_sum = se->avg.util_avg * LOAD_AVG_MAX; | |
3487 | ||
3488 | /* Update parent cfs_rq utilization */ | |
3489 | add_positive(&cfs_rq->avg.util_avg, delta); | |
3490 | cfs_rq->avg.util_sum = cfs_rq->avg.util_avg * LOAD_AVG_MAX; | |
3491 | } | |
3492 | ||
09a43ace | 3493 | static inline void |
0e2d2aaa | 3494 | update_tg_cfs_runnable(struct cfs_rq *cfs_rq, struct sched_entity *se, struct cfs_rq *gcfs_rq) |
09a43ace | 3495 | { |
a4c3c049 VG |
3496 | long delta_avg, running_sum, runnable_sum = gcfs_rq->prop_runnable_sum; |
3497 | unsigned long runnable_load_avg, load_avg; | |
3498 | u64 runnable_load_sum, load_sum = 0; | |
3499 | s64 delta_sum; | |
09a43ace | 3500 | |
0e2d2aaa PZ |
3501 | if (!runnable_sum) |
3502 | return; | |
09a43ace | 3503 | |
0e2d2aaa | 3504 | gcfs_rq->prop_runnable_sum = 0; |
09a43ace | 3505 | |
a4c3c049 VG |
3506 | if (runnable_sum >= 0) { |
3507 | /* | |
3508 | * Add runnable; clip at LOAD_AVG_MAX. Reflects that until | |
3509 | * the CPU is saturated running == runnable. | |
3510 | */ | |
3511 | runnable_sum += se->avg.load_sum; | |
3512 | runnable_sum = min(runnable_sum, (long)LOAD_AVG_MAX); | |
3513 | } else { | |
3514 | /* | |
3515 | * Estimate the new unweighted runnable_sum of the gcfs_rq by | |
3516 | * assuming all tasks are equally runnable. | |
3517 | */ | |
3518 | if (scale_load_down(gcfs_rq->load.weight)) { | |
3519 | load_sum = div_s64(gcfs_rq->avg.load_sum, | |
3520 | scale_load_down(gcfs_rq->load.weight)); | |
3521 | } | |
3522 | ||
3523 | /* But make sure to not inflate se's runnable */ | |
3524 | runnable_sum = min(se->avg.load_sum, load_sum); | |
3525 | } | |
3526 | ||
3527 | /* | |
3528 | * runnable_sum can't be lower than running_sum | |
97fb7a0a | 3529 | * As running sum is scale with CPU capacity wehreas the runnable sum |
a4c3c049 VG |
3530 | * is not we rescale running_sum 1st |
3531 | */ | |
3532 | running_sum = se->avg.util_sum / | |
3533 | arch_scale_cpu_capacity(NULL, cpu_of(rq_of(cfs_rq))); | |
3534 | runnable_sum = max(runnable_sum, running_sum); | |
3535 | ||
0e2d2aaa PZ |
3536 | load_sum = (s64)se_weight(se) * runnable_sum; |
3537 | load_avg = div_s64(load_sum, LOAD_AVG_MAX); | |
09a43ace | 3538 | |
a4c3c049 VG |
3539 | delta_sum = load_sum - (s64)se_weight(se) * se->avg.load_sum; |
3540 | delta_avg = load_avg - se->avg.load_avg; | |
09a43ace | 3541 | |
a4c3c049 VG |
3542 | se->avg.load_sum = runnable_sum; |
3543 | se->avg.load_avg = load_avg; | |
3544 | add_positive(&cfs_rq->avg.load_avg, delta_avg); | |
3545 | add_positive(&cfs_rq->avg.load_sum, delta_sum); | |
09a43ace | 3546 | |
1ea6c46a PZ |
3547 | runnable_load_sum = (s64)se_runnable(se) * runnable_sum; |
3548 | runnable_load_avg = div_s64(runnable_load_sum, LOAD_AVG_MAX); | |
a4c3c049 VG |
3549 | delta_sum = runnable_load_sum - se_weight(se) * se->avg.runnable_load_sum; |
3550 | delta_avg = runnable_load_avg - se->avg.runnable_load_avg; | |
1ea6c46a | 3551 | |
a4c3c049 VG |
3552 | se->avg.runnable_load_sum = runnable_sum; |
3553 | se->avg.runnable_load_avg = runnable_load_avg; | |
1ea6c46a | 3554 | |
09a43ace | 3555 | if (se->on_rq) { |
a4c3c049 VG |
3556 | add_positive(&cfs_rq->avg.runnable_load_avg, delta_avg); |
3557 | add_positive(&cfs_rq->avg.runnable_load_sum, delta_sum); | |
09a43ace VG |
3558 | } |
3559 | } | |
3560 | ||
0e2d2aaa | 3561 | static inline void add_tg_cfs_propagate(struct cfs_rq *cfs_rq, long runnable_sum) |
09a43ace | 3562 | { |
0e2d2aaa PZ |
3563 | cfs_rq->propagate = 1; |
3564 | cfs_rq->prop_runnable_sum += runnable_sum; | |
09a43ace VG |
3565 | } |
3566 | ||
3567 | /* Update task and its cfs_rq load average */ | |
3568 | static inline int propagate_entity_load_avg(struct sched_entity *se) | |
3569 | { | |
0e2d2aaa | 3570 | struct cfs_rq *cfs_rq, *gcfs_rq; |
09a43ace VG |
3571 | |
3572 | if (entity_is_task(se)) | |
3573 | return 0; | |
3574 | ||
0e2d2aaa PZ |
3575 | gcfs_rq = group_cfs_rq(se); |
3576 | if (!gcfs_rq->propagate) | |
09a43ace VG |
3577 | return 0; |
3578 | ||
0e2d2aaa PZ |
3579 | gcfs_rq->propagate = 0; |
3580 | ||
09a43ace VG |
3581 | cfs_rq = cfs_rq_of(se); |
3582 | ||
0e2d2aaa | 3583 | add_tg_cfs_propagate(cfs_rq, gcfs_rq->prop_runnable_sum); |
09a43ace | 3584 | |
0e2d2aaa PZ |
3585 | update_tg_cfs_util(cfs_rq, se, gcfs_rq); |
3586 | update_tg_cfs_runnable(cfs_rq, se, gcfs_rq); | |
09a43ace VG |
3587 | |
3588 | return 1; | |
3589 | } | |
3590 | ||
bc427898 VG |
3591 | /* |
3592 | * Check if we need to update the load and the utilization of a blocked | |
3593 | * group_entity: | |
3594 | */ | |
3595 | static inline bool skip_blocked_update(struct sched_entity *se) | |
3596 | { | |
3597 | struct cfs_rq *gcfs_rq = group_cfs_rq(se); | |
3598 | ||
3599 | /* | |
3600 | * If sched_entity still have not zero load or utilization, we have to | |
3601 | * decay it: | |
3602 | */ | |
3603 | if (se->avg.load_avg || se->avg.util_avg) | |
3604 | return false; | |
3605 | ||
3606 | /* | |
3607 | * If there is a pending propagation, we have to update the load and | |
3608 | * the utilization of the sched_entity: | |
3609 | */ | |
0e2d2aaa | 3610 | if (gcfs_rq->propagate) |
bc427898 VG |
3611 | return false; |
3612 | ||
3613 | /* | |
3614 | * Otherwise, the load and the utilization of the sched_entity is | |
3615 | * already zero and there is no pending propagation, so it will be a | |
3616 | * waste of time to try to decay it: | |
3617 | */ | |
3618 | return true; | |
3619 | } | |
3620 | ||
6e83125c | 3621 | #else /* CONFIG_FAIR_GROUP_SCHED */ |
09a43ace | 3622 | |
9d89c257 | 3623 | static inline void update_tg_load_avg(struct cfs_rq *cfs_rq, int force) {} |
09a43ace VG |
3624 | |
3625 | static inline int propagate_entity_load_avg(struct sched_entity *se) | |
3626 | { | |
3627 | return 0; | |
3628 | } | |
3629 | ||
0e2d2aaa | 3630 | static inline void add_tg_cfs_propagate(struct cfs_rq *cfs_rq, long runnable_sum) {} |
09a43ace | 3631 | |
6e83125c | 3632 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
c566e8e9 | 3633 | |
3d30544f PZ |
3634 | /** |
3635 | * update_cfs_rq_load_avg - update the cfs_rq's load/util averages | |
3636 | * @now: current time, as per cfs_rq_clock_task() | |
3637 | * @cfs_rq: cfs_rq to update | |
3d30544f PZ |
3638 | * |
3639 | * The cfs_rq avg is the direct sum of all its entities (blocked and runnable) | |
3640 | * avg. The immediate corollary is that all (fair) tasks must be attached, see | |
3641 | * post_init_entity_util_avg(). | |
3642 | * | |
3643 | * cfs_rq->avg is used for task_h_load() and update_cfs_share() for example. | |
3644 | * | |
7c3edd2c PZ |
3645 | * Returns true if the load decayed or we removed load. |
3646 | * | |
3647 | * Since both these conditions indicate a changed cfs_rq->avg.load we should | |
3648 | * call update_tg_load_avg() when this function returns true. | |
3d30544f | 3649 | */ |
a2c6c91f | 3650 | static inline int |
3a123bbb | 3651 | update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq) |
2dac754e | 3652 | { |
0e2d2aaa | 3653 | unsigned long removed_load = 0, removed_util = 0, removed_runnable_sum = 0; |
9d89c257 | 3654 | struct sched_avg *sa = &cfs_rq->avg; |
2a2f5d4e | 3655 | int decayed = 0; |
2dac754e | 3656 | |
2a2f5d4e PZ |
3657 | if (cfs_rq->removed.nr) { |
3658 | unsigned long r; | |
9a2dd585 | 3659 | u32 divider = LOAD_AVG_MAX - 1024 + sa->period_contrib; |
2a2f5d4e PZ |
3660 | |
3661 | raw_spin_lock(&cfs_rq->removed.lock); | |
3662 | swap(cfs_rq->removed.util_avg, removed_util); | |
3663 | swap(cfs_rq->removed.load_avg, removed_load); | |
0e2d2aaa | 3664 | swap(cfs_rq->removed.runnable_sum, removed_runnable_sum); |
2a2f5d4e PZ |
3665 | cfs_rq->removed.nr = 0; |
3666 | raw_spin_unlock(&cfs_rq->removed.lock); | |
3667 | ||
2a2f5d4e | 3668 | r = removed_load; |
89741892 | 3669 | sub_positive(&sa->load_avg, r); |
9a2dd585 | 3670 | sub_positive(&sa->load_sum, r * divider); |
2dac754e | 3671 | |
2a2f5d4e | 3672 | r = removed_util; |
89741892 | 3673 | sub_positive(&sa->util_avg, r); |
9a2dd585 | 3674 | sub_positive(&sa->util_sum, r * divider); |
2a2f5d4e | 3675 | |
0e2d2aaa | 3676 | add_tg_cfs_propagate(cfs_rq, -(long)removed_runnable_sum); |
2a2f5d4e PZ |
3677 | |
3678 | decayed = 1; | |
9d89c257 | 3679 | } |
36ee28e4 | 3680 | |
2a2f5d4e | 3681 | decayed |= __update_load_avg_cfs_rq(now, cpu_of(rq_of(cfs_rq)), cfs_rq); |
36ee28e4 | 3682 | |
9d89c257 YD |
3683 | #ifndef CONFIG_64BIT |
3684 | smp_wmb(); | |
3685 | cfs_rq->load_last_update_time_copy = sa->last_update_time; | |
3686 | #endif | |
36ee28e4 | 3687 | |
2a2f5d4e | 3688 | if (decayed) |
ea14b57e | 3689 | cfs_rq_util_change(cfs_rq, 0); |
21e96f88 | 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 | */ | |
ea14b57e | 3702 | static void attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
a05e8c51 | 3703 | { |
f207934f PZ |
3704 | u32 divider = LOAD_AVG_MAX - 1024 + cfs_rq->avg.period_contrib; |
3705 | ||
3706 | /* | |
3707 | * When we attach the @se to the @cfs_rq, we must align the decay | |
3708 | * window because without that, really weird and wonderful things can | |
3709 | * happen. | |
3710 | * | |
3711 | * XXX illustrate | |
3712 | */ | |
a05e8c51 | 3713 | se->avg.last_update_time = cfs_rq->avg.last_update_time; |
f207934f PZ |
3714 | se->avg.period_contrib = cfs_rq->avg.period_contrib; |
3715 | ||
3716 | /* | |
3717 | * Hell(o) Nasty stuff.. we need to recompute _sum based on the new | |
3718 | * period_contrib. This isn't strictly correct, but since we're | |
3719 | * entirely outside of the PELT hierarchy, nobody cares if we truncate | |
3720 | * _sum a little. | |
3721 | */ | |
3722 | se->avg.util_sum = se->avg.util_avg * divider; | |
3723 | ||
3724 | se->avg.load_sum = divider; | |
3725 | if (se_weight(se)) { | |
3726 | se->avg.load_sum = | |
3727 | div_u64(se->avg.load_avg * se->avg.load_sum, se_weight(se)); | |
3728 | } | |
3729 | ||
3730 | se->avg.runnable_load_sum = se->avg.load_sum; | |
3731 | ||
8d5b9025 | 3732 | enqueue_load_avg(cfs_rq, se); |
a05e8c51 BP |
3733 | cfs_rq->avg.util_avg += se->avg.util_avg; |
3734 | cfs_rq->avg.util_sum += se->avg.util_sum; | |
0e2d2aaa PZ |
3735 | |
3736 | add_tg_cfs_propagate(cfs_rq, se->avg.load_sum); | |
a2c6c91f | 3737 | |
ea14b57e | 3738 | cfs_rq_util_change(cfs_rq, flags); |
a05e8c51 BP |
3739 | } |
3740 | ||
3d30544f PZ |
3741 | /** |
3742 | * detach_entity_load_avg - detach this entity from its cfs_rq load avg | |
3743 | * @cfs_rq: cfs_rq to detach from | |
3744 | * @se: sched_entity to detach | |
3745 | * | |
3746 | * Must call update_cfs_rq_load_avg() before this, since we rely on | |
3747 | * cfs_rq->avg.last_update_time being current. | |
3748 | */ | |
a05e8c51 BP |
3749 | static void detach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) |
3750 | { | |
8d5b9025 | 3751 | dequeue_load_avg(cfs_rq, se); |
89741892 PZ |
3752 | sub_positive(&cfs_rq->avg.util_avg, se->avg.util_avg); |
3753 | sub_positive(&cfs_rq->avg.util_sum, se->avg.util_sum); | |
0e2d2aaa PZ |
3754 | |
3755 | add_tg_cfs_propagate(cfs_rq, -se->avg.load_sum); | |
a2c6c91f | 3756 | |
ea14b57e | 3757 | cfs_rq_util_change(cfs_rq, 0); |
a05e8c51 BP |
3758 | } |
3759 | ||
b382a531 PZ |
3760 | /* |
3761 | * Optional action to be done while updating the load average | |
3762 | */ | |
3763 | #define UPDATE_TG 0x1 | |
3764 | #define SKIP_AGE_LOAD 0x2 | |
3765 | #define DO_ATTACH 0x4 | |
3766 | ||
3767 | /* Update task and its cfs_rq load average */ | |
3768 | static inline void update_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) | |
3769 | { | |
3770 | u64 now = cfs_rq_clock_task(cfs_rq); | |
3771 | struct rq *rq = rq_of(cfs_rq); | |
3772 | int cpu = cpu_of(rq); | |
3773 | int decayed; | |
3774 | ||
3775 | /* | |
3776 | * Track task load average for carrying it to new CPU after migrated, and | |
3777 | * track group sched_entity load average for task_h_load calc in migration | |
3778 | */ | |
3779 | if (se->avg.last_update_time && !(flags & SKIP_AGE_LOAD)) | |
3780 | __update_load_avg_se(now, cpu, cfs_rq, se); | |
3781 | ||
3782 | decayed = update_cfs_rq_load_avg(now, cfs_rq); | |
3783 | decayed |= propagate_entity_load_avg(se); | |
3784 | ||
3785 | if (!se->avg.last_update_time && (flags & DO_ATTACH)) { | |
3786 | ||
ea14b57e PZ |
3787 | /* |
3788 | * DO_ATTACH means we're here from enqueue_entity(). | |
3789 | * !last_update_time means we've passed through | |
3790 | * migrate_task_rq_fair() indicating we migrated. | |
3791 | * | |
3792 | * IOW we're enqueueing a task on a new CPU. | |
3793 | */ | |
3794 | attach_entity_load_avg(cfs_rq, se, SCHED_CPUFREQ_MIGRATION); | |
b382a531 PZ |
3795 | update_tg_load_avg(cfs_rq, 0); |
3796 | ||
3797 | } else if (decayed && (flags & UPDATE_TG)) | |
3798 | update_tg_load_avg(cfs_rq, 0); | |
3799 | } | |
3800 | ||
9d89c257 | 3801 | #ifndef CONFIG_64BIT |
0905f04e YD |
3802 | static inline u64 cfs_rq_last_update_time(struct cfs_rq *cfs_rq) |
3803 | { | |
9d89c257 | 3804 | u64 last_update_time_copy; |
0905f04e | 3805 | u64 last_update_time; |
9ee474f5 | 3806 | |
9d89c257 YD |
3807 | do { |
3808 | last_update_time_copy = cfs_rq->load_last_update_time_copy; | |
3809 | smp_rmb(); | |
3810 | last_update_time = cfs_rq->avg.last_update_time; | |
3811 | } while (last_update_time != last_update_time_copy); | |
0905f04e YD |
3812 | |
3813 | return last_update_time; | |
3814 | } | |
9d89c257 | 3815 | #else |
0905f04e YD |
3816 | static inline u64 cfs_rq_last_update_time(struct cfs_rq *cfs_rq) |
3817 | { | |
3818 | return cfs_rq->avg.last_update_time; | |
3819 | } | |
9d89c257 YD |
3820 | #endif |
3821 | ||
104cb16d MR |
3822 | /* |
3823 | * Synchronize entity load avg of dequeued entity without locking | |
3824 | * the previous rq. | |
3825 | */ | |
3826 | void sync_entity_load_avg(struct sched_entity *se) | |
3827 | { | |
3828 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
3829 | u64 last_update_time; | |
3830 | ||
3831 | last_update_time = cfs_rq_last_update_time(cfs_rq); | |
0ccb977f | 3832 | __update_load_avg_blocked_se(last_update_time, cpu_of(rq_of(cfs_rq)), se); |
104cb16d MR |
3833 | } |
3834 | ||
0905f04e YD |
3835 | /* |
3836 | * Task first catches up with cfs_rq, and then subtract | |
3837 | * itself from the cfs_rq (task must be off the queue now). | |
3838 | */ | |
3839 | void remove_entity_load_avg(struct sched_entity *se) | |
3840 | { | |
3841 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
2a2f5d4e | 3842 | unsigned long flags; |
0905f04e YD |
3843 | |
3844 | /* | |
7dc603c9 PZ |
3845 | * tasks cannot exit without having gone through wake_up_new_task() -> |
3846 | * post_init_entity_util_avg() which will have added things to the | |
3847 | * cfs_rq, so we can remove unconditionally. | |
3848 | * | |
3849 | * Similarly for groups, they will have passed through | |
3850 | * post_init_entity_util_avg() before unregister_sched_fair_group() | |
3851 | * calls this. | |
0905f04e | 3852 | */ |
0905f04e | 3853 | |
104cb16d | 3854 | sync_entity_load_avg(se); |
2a2f5d4e PZ |
3855 | |
3856 | raw_spin_lock_irqsave(&cfs_rq->removed.lock, flags); | |
3857 | ++cfs_rq->removed.nr; | |
3858 | cfs_rq->removed.util_avg += se->avg.util_avg; | |
3859 | cfs_rq->removed.load_avg += se->avg.load_avg; | |
0e2d2aaa | 3860 | cfs_rq->removed.runnable_sum += se->avg.load_sum; /* == runnable_sum */ |
2a2f5d4e | 3861 | raw_spin_unlock_irqrestore(&cfs_rq->removed.lock, flags); |
2dac754e | 3862 | } |
642dbc39 | 3863 | |
7ea241af YD |
3864 | static inline unsigned long cfs_rq_runnable_load_avg(struct cfs_rq *cfs_rq) |
3865 | { | |
1ea6c46a | 3866 | return cfs_rq->avg.runnable_load_avg; |
7ea241af YD |
3867 | } |
3868 | ||
3869 | static inline unsigned long cfs_rq_load_avg(struct cfs_rq *cfs_rq) | |
3870 | { | |
3871 | return cfs_rq->avg.load_avg; | |
3872 | } | |
3873 | ||
46f69fa3 | 3874 | static int idle_balance(struct rq *this_rq, struct rq_flags *rf); |
6e83125c | 3875 | |
38033c37 PZ |
3876 | #else /* CONFIG_SMP */ |
3877 | ||
01011473 | 3878 | static inline int |
3a123bbb | 3879 | update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq) |
01011473 PZ |
3880 | { |
3881 | return 0; | |
3882 | } | |
3883 | ||
d31b1a66 VG |
3884 | #define UPDATE_TG 0x0 |
3885 | #define SKIP_AGE_LOAD 0x0 | |
b382a531 | 3886 | #define DO_ATTACH 0x0 |
d31b1a66 | 3887 | |
88c0616e | 3888 | static inline void update_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se, int not_used1) |
536bd00c | 3889 | { |
ea14b57e | 3890 | cfs_rq_util_change(cfs_rq, 0); |
536bd00c RW |
3891 | } |
3892 | ||
9d89c257 | 3893 | static inline void remove_entity_load_avg(struct sched_entity *se) {} |
6e83125c | 3894 | |
a05e8c51 | 3895 | static inline void |
ea14b57e | 3896 | attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) {} |
a05e8c51 BP |
3897 | static inline void |
3898 | detach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {} | |
3899 | ||
46f69fa3 | 3900 | static inline int idle_balance(struct rq *rq, struct rq_flags *rf) |
6e83125c PZ |
3901 | { |
3902 | return 0; | |
3903 | } | |
3904 | ||
38033c37 | 3905 | #endif /* CONFIG_SMP */ |
9d85f21c | 3906 | |
ddc97297 PZ |
3907 | static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se) |
3908 | { | |
3909 | #ifdef CONFIG_SCHED_DEBUG | |
3910 | s64 d = se->vruntime - cfs_rq->min_vruntime; | |
3911 | ||
3912 | if (d < 0) | |
3913 | d = -d; | |
3914 | ||
3915 | if (d > 3*sysctl_sched_latency) | |
ae92882e | 3916 | schedstat_inc(cfs_rq->nr_spread_over); |
ddc97297 PZ |
3917 | #endif |
3918 | } | |
3919 | ||
aeb73b04 PZ |
3920 | static void |
3921 | place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial) | |
3922 | { | |
1af5f730 | 3923 | u64 vruntime = cfs_rq->min_vruntime; |
94dfb5e7 | 3924 | |
2cb8600e PZ |
3925 | /* |
3926 | * The 'current' period is already promised to the current tasks, | |
3927 | * however the extra weight of the new task will slow them down a | |
3928 | * little, place the new task so that it fits in the slot that | |
3929 | * stays open at the end. | |
3930 | */ | |
94dfb5e7 | 3931 | if (initial && sched_feat(START_DEBIT)) |
f9c0b095 | 3932 | vruntime += sched_vslice(cfs_rq, se); |
aeb73b04 | 3933 | |
a2e7a7eb | 3934 | /* sleeps up to a single latency don't count. */ |
5ca9880c | 3935 | if (!initial) { |
a2e7a7eb | 3936 | unsigned long thresh = sysctl_sched_latency; |
a7be37ac | 3937 | |
a2e7a7eb MG |
3938 | /* |
3939 | * Halve their sleep time's effect, to allow | |
3940 | * for a gentler effect of sleepers: | |
3941 | */ | |
3942 | if (sched_feat(GENTLE_FAIR_SLEEPERS)) | |
3943 | thresh >>= 1; | |
51e0304c | 3944 | |
a2e7a7eb | 3945 | vruntime -= thresh; |
aeb73b04 PZ |
3946 | } |
3947 | ||
b5d9d734 | 3948 | /* ensure we never gain time by being placed backwards. */ |
16c8f1c7 | 3949 | se->vruntime = max_vruntime(se->vruntime, vruntime); |
aeb73b04 PZ |
3950 | } |
3951 | ||
d3d9dc33 PT |
3952 | static void check_enqueue_throttle(struct cfs_rq *cfs_rq); |
3953 | ||
cb251765 MG |
3954 | static inline void check_schedstat_required(void) |
3955 | { | |
3956 | #ifdef CONFIG_SCHEDSTATS | |
3957 | if (schedstat_enabled()) | |
3958 | return; | |
3959 | ||
3960 | /* Force schedstat enabled if a dependent tracepoint is active */ | |
3961 | if (trace_sched_stat_wait_enabled() || | |
3962 | trace_sched_stat_sleep_enabled() || | |
3963 | trace_sched_stat_iowait_enabled() || | |
3964 | trace_sched_stat_blocked_enabled() || | |
3965 | trace_sched_stat_runtime_enabled()) { | |
eda8dca5 | 3966 | printk_deferred_once("Scheduler tracepoints stat_sleep, stat_iowait, " |
cb251765 | 3967 | "stat_blocked and stat_runtime require the " |
f67abed5 | 3968 | "kernel parameter schedstats=enable or " |
cb251765 MG |
3969 | "kernel.sched_schedstats=1\n"); |
3970 | } | |
3971 | #endif | |
3972 | } | |
3973 | ||
b5179ac7 PZ |
3974 | |
3975 | /* | |
3976 | * MIGRATION | |
3977 | * | |
3978 | * dequeue | |
3979 | * update_curr() | |
3980 | * update_min_vruntime() | |
3981 | * vruntime -= min_vruntime | |
3982 | * | |
3983 | * enqueue | |
3984 | * update_curr() | |
3985 | * update_min_vruntime() | |
3986 | * vruntime += min_vruntime | |
3987 | * | |
3988 | * this way the vruntime transition between RQs is done when both | |
3989 | * min_vruntime are up-to-date. | |
3990 | * | |
3991 | * WAKEUP (remote) | |
3992 | * | |
59efa0ba | 3993 | * ->migrate_task_rq_fair() (p->state == TASK_WAKING) |
b5179ac7 PZ |
3994 | * vruntime -= min_vruntime |
3995 | * | |
3996 | * enqueue | |
3997 | * update_curr() | |
3998 | * update_min_vruntime() | |
3999 | * vruntime += min_vruntime | |
4000 | * | |
4001 | * this way we don't have the most up-to-date min_vruntime on the originating | |
4002 | * CPU and an up-to-date min_vruntime on the destination CPU. | |
4003 | */ | |
4004 | ||
bf0f6f24 | 4005 | static void |
88ec22d3 | 4006 | enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
bf0f6f24 | 4007 | { |
2f950354 PZ |
4008 | bool renorm = !(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_MIGRATED); |
4009 | bool curr = cfs_rq->curr == se; | |
4010 | ||
88ec22d3 | 4011 | /* |
2f950354 PZ |
4012 | * If we're the current task, we must renormalise before calling |
4013 | * update_curr(). | |
88ec22d3 | 4014 | */ |
2f950354 | 4015 | if (renorm && curr) |
88ec22d3 PZ |
4016 | se->vruntime += cfs_rq->min_vruntime; |
4017 | ||
2f950354 PZ |
4018 | update_curr(cfs_rq); |
4019 | ||
bf0f6f24 | 4020 | /* |
2f950354 PZ |
4021 | * Otherwise, renormalise after, such that we're placed at the current |
4022 | * moment in time, instead of some random moment in the past. Being | |
4023 | * placed in the past could significantly boost this task to the | |
4024 | * fairness detriment of existing tasks. | |
bf0f6f24 | 4025 | */ |
2f950354 PZ |
4026 | if (renorm && !curr) |
4027 | se->vruntime += cfs_rq->min_vruntime; | |
4028 | ||
89ee048f VG |
4029 | /* |
4030 | * When enqueuing a sched_entity, we must: | |
4031 | * - Update loads to have both entity and cfs_rq synced with now. | |
4032 | * - Add its load to cfs_rq->runnable_avg | |
4033 | * - For group_entity, update its weight to reflect the new share of | |
4034 | * its group cfs_rq | |
4035 | * - Add its new weight to cfs_rq->load.weight | |
4036 | */ | |
b382a531 | 4037 | update_load_avg(cfs_rq, se, UPDATE_TG | DO_ATTACH); |
1ea6c46a | 4038 | update_cfs_group(se); |
b5b3e35f | 4039 | enqueue_runnable_load_avg(cfs_rq, se); |
17bc14b7 | 4040 | account_entity_enqueue(cfs_rq, se); |
bf0f6f24 | 4041 | |
1a3d027c | 4042 | if (flags & ENQUEUE_WAKEUP) |
aeb73b04 | 4043 | place_entity(cfs_rq, se, 0); |
bf0f6f24 | 4044 | |
cb251765 | 4045 | check_schedstat_required(); |
4fa8d299 JP |
4046 | update_stats_enqueue(cfs_rq, se, flags); |
4047 | check_spread(cfs_rq, se); | |
2f950354 | 4048 | if (!curr) |
83b699ed | 4049 | __enqueue_entity(cfs_rq, se); |
2069dd75 | 4050 | se->on_rq = 1; |
3d4b47b4 | 4051 | |
d3d9dc33 | 4052 | if (cfs_rq->nr_running == 1) { |
3d4b47b4 | 4053 | list_add_leaf_cfs_rq(cfs_rq); |
d3d9dc33 PT |
4054 | check_enqueue_throttle(cfs_rq); |
4055 | } | |
bf0f6f24 IM |
4056 | } |
4057 | ||
2c13c919 | 4058 | static void __clear_buddies_last(struct sched_entity *se) |
2002c695 | 4059 | { |
2c13c919 RR |
4060 | for_each_sched_entity(se) { |
4061 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
f1044799 | 4062 | if (cfs_rq->last != se) |
2c13c919 | 4063 | break; |
f1044799 PZ |
4064 | |
4065 | cfs_rq->last = NULL; | |
2c13c919 RR |
4066 | } |
4067 | } | |
2002c695 | 4068 | |
2c13c919 RR |
4069 | static void __clear_buddies_next(struct sched_entity *se) |
4070 | { | |
4071 | for_each_sched_entity(se) { | |
4072 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
f1044799 | 4073 | if (cfs_rq->next != se) |
2c13c919 | 4074 | break; |
f1044799 PZ |
4075 | |
4076 | cfs_rq->next = NULL; | |
2c13c919 | 4077 | } |
2002c695 PZ |
4078 | } |
4079 | ||
ac53db59 RR |
4080 | static void __clear_buddies_skip(struct sched_entity *se) |
4081 | { | |
4082 | for_each_sched_entity(se) { | |
4083 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
f1044799 | 4084 | if (cfs_rq->skip != se) |
ac53db59 | 4085 | break; |
f1044799 PZ |
4086 | |
4087 | cfs_rq->skip = NULL; | |
ac53db59 RR |
4088 | } |
4089 | } | |
4090 | ||
a571bbea PZ |
4091 | static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se) |
4092 | { | |
2c13c919 RR |
4093 | if (cfs_rq->last == se) |
4094 | __clear_buddies_last(se); | |
4095 | ||
4096 | if (cfs_rq->next == se) | |
4097 | __clear_buddies_next(se); | |
ac53db59 RR |
4098 | |
4099 | if (cfs_rq->skip == se) | |
4100 | __clear_buddies_skip(se); | |
a571bbea PZ |
4101 | } |
4102 | ||
6c16a6dc | 4103 | static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq); |
d8b4986d | 4104 | |
bf0f6f24 | 4105 | static void |
371fd7e7 | 4106 | dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
bf0f6f24 | 4107 | { |
a2a2d680 DA |
4108 | /* |
4109 | * Update run-time statistics of the 'current'. | |
4110 | */ | |
4111 | update_curr(cfs_rq); | |
89ee048f VG |
4112 | |
4113 | /* | |
4114 | * When dequeuing a sched_entity, we must: | |
4115 | * - Update loads to have both entity and cfs_rq synced with now. | |
4116 | * - Substract its load from the cfs_rq->runnable_avg. | |
4117 | * - Substract its previous weight from cfs_rq->load.weight. | |
4118 | * - For group entity, update its weight to reflect the new share | |
4119 | * of its group cfs_rq. | |
4120 | */ | |
88c0616e | 4121 | update_load_avg(cfs_rq, se, UPDATE_TG); |
b5b3e35f | 4122 | dequeue_runnable_load_avg(cfs_rq, se); |
a2a2d680 | 4123 | |
4fa8d299 | 4124 | update_stats_dequeue(cfs_rq, se, flags); |
67e9fb2a | 4125 | |
2002c695 | 4126 | clear_buddies(cfs_rq, se); |
4793241b | 4127 | |
83b699ed | 4128 | if (se != cfs_rq->curr) |
30cfdcfc | 4129 | __dequeue_entity(cfs_rq, se); |
17bc14b7 | 4130 | se->on_rq = 0; |
30cfdcfc | 4131 | account_entity_dequeue(cfs_rq, se); |
88ec22d3 PZ |
4132 | |
4133 | /* | |
b60205c7 PZ |
4134 | * Normalize after update_curr(); which will also have moved |
4135 | * min_vruntime if @se is the one holding it back. But before doing | |
4136 | * update_min_vruntime() again, which will discount @se's position and | |
4137 | * can move min_vruntime forward still more. | |
88ec22d3 | 4138 | */ |
371fd7e7 | 4139 | if (!(flags & DEQUEUE_SLEEP)) |
88ec22d3 | 4140 | se->vruntime -= cfs_rq->min_vruntime; |
1e876231 | 4141 | |
d8b4986d PT |
4142 | /* return excess runtime on last dequeue */ |
4143 | return_cfs_rq_runtime(cfs_rq); | |
4144 | ||
1ea6c46a | 4145 | update_cfs_group(se); |
b60205c7 PZ |
4146 | |
4147 | /* | |
4148 | * Now advance min_vruntime if @se was the entity holding it back, | |
4149 | * except when: DEQUEUE_SAVE && !DEQUEUE_MOVE, in this case we'll be | |
4150 | * put back on, and if we advance min_vruntime, we'll be placed back | |
4151 | * further than we started -- ie. we'll be penalized. | |
4152 | */ | |
4153 | if ((flags & (DEQUEUE_SAVE | DEQUEUE_MOVE)) == DEQUEUE_SAVE) | |
4154 | update_min_vruntime(cfs_rq); | |
bf0f6f24 IM |
4155 | } |
4156 | ||
4157 | /* | |
4158 | * Preempt the current task with a newly woken task if needed: | |
4159 | */ | |
7c92e54f | 4160 | static void |
2e09bf55 | 4161 | check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr) |
bf0f6f24 | 4162 | { |
11697830 | 4163 | unsigned long ideal_runtime, delta_exec; |
f4cfb33e WX |
4164 | struct sched_entity *se; |
4165 | s64 delta; | |
11697830 | 4166 | |
6d0f0ebd | 4167 | ideal_runtime = sched_slice(cfs_rq, curr); |
11697830 | 4168 | delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime; |
a9f3e2b5 | 4169 | if (delta_exec > ideal_runtime) { |
8875125e | 4170 | resched_curr(rq_of(cfs_rq)); |
a9f3e2b5 MG |
4171 | /* |
4172 | * The current task ran long enough, ensure it doesn't get | |
4173 | * re-elected due to buddy favours. | |
4174 | */ | |
4175 | clear_buddies(cfs_rq, curr); | |
f685ceac MG |
4176 | return; |
4177 | } | |
4178 | ||
4179 | /* | |
4180 | * Ensure that a task that missed wakeup preemption by a | |
4181 | * narrow margin doesn't have to wait for a full slice. | |
4182 | * This also mitigates buddy induced latencies under load. | |
4183 | */ | |
f685ceac MG |
4184 | if (delta_exec < sysctl_sched_min_granularity) |
4185 | return; | |
4186 | ||
f4cfb33e WX |
4187 | se = __pick_first_entity(cfs_rq); |
4188 | delta = curr->vruntime - se->vruntime; | |
f685ceac | 4189 | |
f4cfb33e WX |
4190 | if (delta < 0) |
4191 | return; | |
d7d82944 | 4192 | |
f4cfb33e | 4193 | if (delta > ideal_runtime) |
8875125e | 4194 | resched_curr(rq_of(cfs_rq)); |
bf0f6f24 IM |
4195 | } |
4196 | ||
83b699ed | 4197 | static void |
8494f412 | 4198 | set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 | 4199 | { |
83b699ed SV |
4200 | /* 'current' is not kept within the tree. */ |
4201 | if (se->on_rq) { | |
4202 | /* | |
4203 | * Any task has to be enqueued before it get to execute on | |
4204 | * a CPU. So account for the time it spent waiting on the | |
4205 | * runqueue. | |
4206 | */ | |
4fa8d299 | 4207 | update_stats_wait_end(cfs_rq, se); |
83b699ed | 4208 | __dequeue_entity(cfs_rq, se); |
88c0616e | 4209 | update_load_avg(cfs_rq, se, UPDATE_TG); |
83b699ed SV |
4210 | } |
4211 | ||
79303e9e | 4212 | update_stats_curr_start(cfs_rq, se); |
429d43bc | 4213 | cfs_rq->curr = se; |
4fa8d299 | 4214 | |
eba1ed4b IM |
4215 | /* |
4216 | * Track our maximum slice length, if the CPU's load is at | |
4217 | * least twice that of our own weight (i.e. dont track it | |
4218 | * when there are only lesser-weight tasks around): | |
4219 | */ | |
cb251765 | 4220 | if (schedstat_enabled() && rq_of(cfs_rq)->load.weight >= 2*se->load.weight) { |
4fa8d299 JP |
4221 | schedstat_set(se->statistics.slice_max, |
4222 | max((u64)schedstat_val(se->statistics.slice_max), | |
4223 | se->sum_exec_runtime - se->prev_sum_exec_runtime)); | |
eba1ed4b | 4224 | } |
4fa8d299 | 4225 | |
4a55b450 | 4226 | se->prev_sum_exec_runtime = se->sum_exec_runtime; |
bf0f6f24 IM |
4227 | } |
4228 | ||
3f3a4904 PZ |
4229 | static int |
4230 | wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se); | |
4231 | ||
ac53db59 RR |
4232 | /* |
4233 | * Pick the next process, keeping these things in mind, in this order: | |
4234 | * 1) keep things fair between processes/task groups | |
4235 | * 2) pick the "next" process, since someone really wants that to run | |
4236 | * 3) pick the "last" process, for cache locality | |
4237 | * 4) do not run the "skip" process, if something else is available | |
4238 | */ | |
678d5718 PZ |
4239 | static struct sched_entity * |
4240 | pick_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *curr) | |
aa2ac252 | 4241 | { |
678d5718 PZ |
4242 | struct sched_entity *left = __pick_first_entity(cfs_rq); |
4243 | struct sched_entity *se; | |
4244 | ||
4245 | /* | |
4246 | * If curr is set we have to see if its left of the leftmost entity | |
4247 | * still in the tree, provided there was anything in the tree at all. | |
4248 | */ | |
4249 | if (!left || (curr && entity_before(curr, left))) | |
4250 | left = curr; | |
4251 | ||
4252 | se = left; /* ideally we run the leftmost entity */ | |
f4b6755f | 4253 | |
ac53db59 RR |
4254 | /* |
4255 | * Avoid running the skip buddy, if running something else can | |
4256 | * be done without getting too unfair. | |
4257 | */ | |
4258 | if (cfs_rq->skip == se) { | |
678d5718 PZ |
4259 | struct sched_entity *second; |
4260 | ||
4261 | if (se == curr) { | |
4262 | second = __pick_first_entity(cfs_rq); | |
4263 | } else { | |
4264 | second = __pick_next_entity(se); | |
4265 | if (!second || (curr && entity_before(curr, second))) | |
4266 | second = curr; | |
4267 | } | |
4268 | ||
ac53db59 RR |
4269 | if (second && wakeup_preempt_entity(second, left) < 1) |
4270 | se = second; | |
4271 | } | |
aa2ac252 | 4272 | |
f685ceac MG |
4273 | /* |
4274 | * Prefer last buddy, try to return the CPU to a preempted task. | |
4275 | */ | |
4276 | if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1) | |
4277 | se = cfs_rq->last; | |
4278 | ||
ac53db59 RR |
4279 | /* |
4280 | * Someone really wants this to run. If it's not unfair, run it. | |
4281 | */ | |
4282 | if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1) | |
4283 | se = cfs_rq->next; | |
4284 | ||
f685ceac | 4285 | clear_buddies(cfs_rq, se); |
4793241b PZ |
4286 | |
4287 | return se; | |
aa2ac252 PZ |
4288 | } |
4289 | ||
678d5718 | 4290 | static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq); |
d3d9dc33 | 4291 | |
ab6cde26 | 4292 | static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev) |
bf0f6f24 IM |
4293 | { |
4294 | /* | |
4295 | * If still on the runqueue then deactivate_task() | |
4296 | * was not called and update_curr() has to be done: | |
4297 | */ | |
4298 | if (prev->on_rq) | |
b7cc0896 | 4299 | update_curr(cfs_rq); |
bf0f6f24 | 4300 | |
d3d9dc33 PT |
4301 | /* throttle cfs_rqs exceeding runtime */ |
4302 | check_cfs_rq_runtime(cfs_rq); | |
4303 | ||
4fa8d299 | 4304 | check_spread(cfs_rq, prev); |
cb251765 | 4305 | |
30cfdcfc | 4306 | if (prev->on_rq) { |
4fa8d299 | 4307 | update_stats_wait_start(cfs_rq, prev); |
30cfdcfc DA |
4308 | /* Put 'current' back into the tree. */ |
4309 | __enqueue_entity(cfs_rq, prev); | |
9d85f21c | 4310 | /* in !on_rq case, update occurred at dequeue */ |
88c0616e | 4311 | update_load_avg(cfs_rq, prev, 0); |
30cfdcfc | 4312 | } |
429d43bc | 4313 | cfs_rq->curr = NULL; |
bf0f6f24 IM |
4314 | } |
4315 | ||
8f4d37ec PZ |
4316 | static void |
4317 | entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued) | |
bf0f6f24 | 4318 | { |
bf0f6f24 | 4319 | /* |
30cfdcfc | 4320 | * Update run-time statistics of the 'current'. |
bf0f6f24 | 4321 | */ |
30cfdcfc | 4322 | update_curr(cfs_rq); |
bf0f6f24 | 4323 | |
9d85f21c PT |
4324 | /* |
4325 | * Ensure that runnable average is periodically updated. | |
4326 | */ | |
88c0616e | 4327 | update_load_avg(cfs_rq, curr, UPDATE_TG); |
1ea6c46a | 4328 | update_cfs_group(curr); |
9d85f21c | 4329 | |
8f4d37ec PZ |
4330 | #ifdef CONFIG_SCHED_HRTICK |
4331 | /* | |
4332 | * queued ticks are scheduled to match the slice, so don't bother | |
4333 | * validating it and just reschedule. | |
4334 | */ | |
983ed7a6 | 4335 | if (queued) { |
8875125e | 4336 | resched_curr(rq_of(cfs_rq)); |
983ed7a6 HH |
4337 | return; |
4338 | } | |
8f4d37ec PZ |
4339 | /* |
4340 | * don't let the period tick interfere with the hrtick preemption | |
4341 | */ | |
4342 | if (!sched_feat(DOUBLE_TICK) && | |
4343 | hrtimer_active(&rq_of(cfs_rq)->hrtick_timer)) | |
4344 | return; | |
4345 | #endif | |
4346 | ||
2c2efaed | 4347 | if (cfs_rq->nr_running > 1) |
2e09bf55 | 4348 | check_preempt_tick(cfs_rq, curr); |
bf0f6f24 IM |
4349 | } |
4350 | ||
ab84d31e PT |
4351 | |
4352 | /************************************************** | |
4353 | * CFS bandwidth control machinery | |
4354 | */ | |
4355 | ||
4356 | #ifdef CONFIG_CFS_BANDWIDTH | |
029632fb PZ |
4357 | |
4358 | #ifdef HAVE_JUMP_LABEL | |
c5905afb | 4359 | static struct static_key __cfs_bandwidth_used; |
029632fb PZ |
4360 | |
4361 | static inline bool cfs_bandwidth_used(void) | |
4362 | { | |
c5905afb | 4363 | return static_key_false(&__cfs_bandwidth_used); |
029632fb PZ |
4364 | } |
4365 | ||
1ee14e6c | 4366 | void cfs_bandwidth_usage_inc(void) |
029632fb | 4367 | { |
ce48c146 | 4368 | static_key_slow_inc_cpuslocked(&__cfs_bandwidth_used); |
1ee14e6c BS |
4369 | } |
4370 | ||
4371 | void cfs_bandwidth_usage_dec(void) | |
4372 | { | |
ce48c146 | 4373 | static_key_slow_dec_cpuslocked(&__cfs_bandwidth_used); |
029632fb PZ |
4374 | } |
4375 | #else /* HAVE_JUMP_LABEL */ | |
4376 | static bool cfs_bandwidth_used(void) | |
4377 | { | |
4378 | return true; | |
4379 | } | |
4380 | ||
1ee14e6c BS |
4381 | void cfs_bandwidth_usage_inc(void) {} |
4382 | void cfs_bandwidth_usage_dec(void) {} | |
029632fb PZ |
4383 | #endif /* HAVE_JUMP_LABEL */ |
4384 | ||
ab84d31e PT |
4385 | /* |
4386 | * default period for cfs group bandwidth. | |
4387 | * default: 0.1s, units: nanoseconds | |
4388 | */ | |
4389 | static inline u64 default_cfs_period(void) | |
4390 | { | |
4391 | return 100000000ULL; | |
4392 | } | |
ec12cb7f PT |
4393 | |
4394 | static inline u64 sched_cfs_bandwidth_slice(void) | |
4395 | { | |
4396 | return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC; | |
4397 | } | |
4398 | ||
a9cf55b2 PT |
4399 | /* |
4400 | * Replenish runtime according to assigned quota and update expiration time. | |
4401 | * We use sched_clock_cpu directly instead of rq->clock to avoid adding | |
4402 | * additional synchronization around rq->lock. | |
4403 | * | |
4404 | * requires cfs_b->lock | |
4405 | */ | |
029632fb | 4406 | void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b) |
a9cf55b2 PT |
4407 | { |
4408 | u64 now; | |
4409 | ||
4410 | if (cfs_b->quota == RUNTIME_INF) | |
4411 | return; | |
4412 | ||
4413 | now = sched_clock_cpu(smp_processor_id()); | |
4414 | cfs_b->runtime = cfs_b->quota; | |
4415 | cfs_b->runtime_expires = now + ktime_to_ns(cfs_b->period); | |
4416 | } | |
4417 | ||
029632fb PZ |
4418 | static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg) |
4419 | { | |
4420 | return &tg->cfs_bandwidth; | |
4421 | } | |
4422 | ||
f1b17280 PT |
4423 | /* rq->task_clock normalized against any time this cfs_rq has spent throttled */ |
4424 | static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq) | |
4425 | { | |
4426 | if (unlikely(cfs_rq->throttle_count)) | |
1a99ae3f | 4427 | return cfs_rq->throttled_clock_task - cfs_rq->throttled_clock_task_time; |
f1b17280 | 4428 | |
78becc27 | 4429 | return rq_clock_task(rq_of(cfs_rq)) - cfs_rq->throttled_clock_task_time; |
f1b17280 PT |
4430 | } |
4431 | ||
85dac906 PT |
4432 | /* returns 0 on failure to allocate runtime */ |
4433 | static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
ec12cb7f PT |
4434 | { |
4435 | struct task_group *tg = cfs_rq->tg; | |
4436 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg); | |
a9cf55b2 | 4437 | u64 amount = 0, min_amount, expires; |
ec12cb7f PT |
4438 | |
4439 | /* note: this is a positive sum as runtime_remaining <= 0 */ | |
4440 | min_amount = sched_cfs_bandwidth_slice() - cfs_rq->runtime_remaining; | |
4441 | ||
4442 | raw_spin_lock(&cfs_b->lock); | |
4443 | if (cfs_b->quota == RUNTIME_INF) | |
4444 | amount = min_amount; | |
58088ad0 | 4445 | else { |
77a4d1a1 | 4446 | start_cfs_bandwidth(cfs_b); |
58088ad0 PT |
4447 | |
4448 | if (cfs_b->runtime > 0) { | |
4449 | amount = min(cfs_b->runtime, min_amount); | |
4450 | cfs_b->runtime -= amount; | |
4451 | cfs_b->idle = 0; | |
4452 | } | |
ec12cb7f | 4453 | } |
a9cf55b2 | 4454 | expires = cfs_b->runtime_expires; |
ec12cb7f PT |
4455 | raw_spin_unlock(&cfs_b->lock); |
4456 | ||
4457 | cfs_rq->runtime_remaining += amount; | |
a9cf55b2 PT |
4458 | /* |
4459 | * we may have advanced our local expiration to account for allowed | |
4460 | * spread between our sched_clock and the one on which runtime was | |
4461 | * issued. | |
4462 | */ | |
4463 | if ((s64)(expires - cfs_rq->runtime_expires) > 0) | |
4464 | cfs_rq->runtime_expires = expires; | |
85dac906 PT |
4465 | |
4466 | return cfs_rq->runtime_remaining > 0; | |
ec12cb7f PT |
4467 | } |
4468 | ||
a9cf55b2 PT |
4469 | /* |
4470 | * Note: This depends on the synchronization provided by sched_clock and the | |
4471 | * fact that rq->clock snapshots this value. | |
4472 | */ | |
4473 | static void expire_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
ec12cb7f | 4474 | { |
a9cf55b2 | 4475 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); |
a9cf55b2 PT |
4476 | |
4477 | /* if the deadline is ahead of our clock, nothing to do */ | |
78becc27 | 4478 | if (likely((s64)(rq_clock(rq_of(cfs_rq)) - cfs_rq->runtime_expires) < 0)) |
ec12cb7f PT |
4479 | return; |
4480 | ||
a9cf55b2 PT |
4481 | if (cfs_rq->runtime_remaining < 0) |
4482 | return; | |
4483 | ||
4484 | /* | |
4485 | * If the local deadline has passed we have to consider the | |
4486 | * possibility that our sched_clock is 'fast' and the global deadline | |
4487 | * has not truly expired. | |
4488 | * | |
4489 | * Fortunately we can check determine whether this the case by checking | |
51f2176d BS |
4490 | * whether the global deadline has advanced. It is valid to compare |
4491 | * cfs_b->runtime_expires without any locks since we only care about | |
4492 | * exact equality, so a partial write will still work. | |
a9cf55b2 PT |
4493 | */ |
4494 | ||
51f2176d | 4495 | if (cfs_rq->runtime_expires != cfs_b->runtime_expires) { |
a9cf55b2 PT |
4496 | /* extend local deadline, drift is bounded above by 2 ticks */ |
4497 | cfs_rq->runtime_expires += TICK_NSEC; | |
4498 | } else { | |
4499 | /* global deadline is ahead, expiration has passed */ | |
4500 | cfs_rq->runtime_remaining = 0; | |
4501 | } | |
4502 | } | |
4503 | ||
9dbdb155 | 4504 | static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec) |
a9cf55b2 PT |
4505 | { |
4506 | /* dock delta_exec before expiring quota (as it could span periods) */ | |
ec12cb7f | 4507 | cfs_rq->runtime_remaining -= delta_exec; |
a9cf55b2 PT |
4508 | expire_cfs_rq_runtime(cfs_rq); |
4509 | ||
4510 | if (likely(cfs_rq->runtime_remaining > 0)) | |
ec12cb7f PT |
4511 | return; |
4512 | ||
85dac906 PT |
4513 | /* |
4514 | * if we're unable to extend our runtime we resched so that the active | |
4515 | * hierarchy can be throttled | |
4516 | */ | |
4517 | if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr)) | |
8875125e | 4518 | resched_curr(rq_of(cfs_rq)); |
ec12cb7f PT |
4519 | } |
4520 | ||
6c16a6dc | 4521 | static __always_inline |
9dbdb155 | 4522 | void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec) |
ec12cb7f | 4523 | { |
56f570e5 | 4524 | if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled) |
ec12cb7f PT |
4525 | return; |
4526 | ||
4527 | __account_cfs_rq_runtime(cfs_rq, delta_exec); | |
4528 | } | |
4529 | ||
85dac906 PT |
4530 | static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq) |
4531 | { | |
56f570e5 | 4532 | return cfs_bandwidth_used() && cfs_rq->throttled; |
85dac906 PT |
4533 | } |
4534 | ||
64660c86 PT |
4535 | /* check whether cfs_rq, or any parent, is throttled */ |
4536 | static inline int throttled_hierarchy(struct cfs_rq *cfs_rq) | |
4537 | { | |
56f570e5 | 4538 | return cfs_bandwidth_used() && cfs_rq->throttle_count; |
64660c86 PT |
4539 | } |
4540 | ||
4541 | /* | |
4542 | * Ensure that neither of the group entities corresponding to src_cpu or | |
4543 | * dest_cpu are members of a throttled hierarchy when performing group | |
4544 | * load-balance operations. | |
4545 | */ | |
4546 | static inline int throttled_lb_pair(struct task_group *tg, | |
4547 | int src_cpu, int dest_cpu) | |
4548 | { | |
4549 | struct cfs_rq *src_cfs_rq, *dest_cfs_rq; | |
4550 | ||
4551 | src_cfs_rq = tg->cfs_rq[src_cpu]; | |
4552 | dest_cfs_rq = tg->cfs_rq[dest_cpu]; | |
4553 | ||
4554 | return throttled_hierarchy(src_cfs_rq) || | |
4555 | throttled_hierarchy(dest_cfs_rq); | |
4556 | } | |
4557 | ||
4558 | /* updated child weight may affect parent so we have to do this bottom up */ | |
4559 | static int tg_unthrottle_up(struct task_group *tg, void *data) | |
4560 | { | |
4561 | struct rq *rq = data; | |
4562 | struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; | |
4563 | ||
4564 | cfs_rq->throttle_count--; | |
64660c86 | 4565 | if (!cfs_rq->throttle_count) { |
f1b17280 | 4566 | /* adjust cfs_rq_clock_task() */ |
78becc27 | 4567 | cfs_rq->throttled_clock_task_time += rq_clock_task(rq) - |
f1b17280 | 4568 | cfs_rq->throttled_clock_task; |
64660c86 | 4569 | } |
64660c86 PT |
4570 | |
4571 | return 0; | |
4572 | } | |
4573 | ||
4574 | static int tg_throttle_down(struct task_group *tg, void *data) | |
4575 | { | |
4576 | struct rq *rq = data; | |
4577 | struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; | |
4578 | ||
82958366 PT |
4579 | /* group is entering throttled state, stop time */ |
4580 | if (!cfs_rq->throttle_count) | |
78becc27 | 4581 | cfs_rq->throttled_clock_task = rq_clock_task(rq); |
64660c86 PT |
4582 | cfs_rq->throttle_count++; |
4583 | ||
4584 | return 0; | |
4585 | } | |
4586 | ||
d3d9dc33 | 4587 | static void throttle_cfs_rq(struct cfs_rq *cfs_rq) |
85dac906 PT |
4588 | { |
4589 | struct rq *rq = rq_of(cfs_rq); | |
4590 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); | |
4591 | struct sched_entity *se; | |
4592 | long task_delta, dequeue = 1; | |
77a4d1a1 | 4593 | bool empty; |
85dac906 PT |
4594 | |
4595 | se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))]; | |
4596 | ||
f1b17280 | 4597 | /* freeze hierarchy runnable averages while throttled */ |
64660c86 PT |
4598 | rcu_read_lock(); |
4599 | walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq); | |
4600 | rcu_read_unlock(); | |
85dac906 PT |
4601 | |
4602 | task_delta = cfs_rq->h_nr_running; | |
4603 | for_each_sched_entity(se) { | |
4604 | struct cfs_rq *qcfs_rq = cfs_rq_of(se); | |
4605 | /* throttled entity or throttle-on-deactivate */ | |
4606 | if (!se->on_rq) | |
4607 | break; | |
4608 | ||
4609 | if (dequeue) | |
4610 | dequeue_entity(qcfs_rq, se, DEQUEUE_SLEEP); | |
4611 | qcfs_rq->h_nr_running -= task_delta; | |
4612 | ||
4613 | if (qcfs_rq->load.weight) | |
4614 | dequeue = 0; | |
4615 | } | |
4616 | ||
4617 | if (!se) | |
72465447 | 4618 | sub_nr_running(rq, task_delta); |
85dac906 PT |
4619 | |
4620 | cfs_rq->throttled = 1; | |
78becc27 | 4621 | cfs_rq->throttled_clock = rq_clock(rq); |
85dac906 | 4622 | raw_spin_lock(&cfs_b->lock); |
d49db342 | 4623 | empty = list_empty(&cfs_b->throttled_cfs_rq); |
77a4d1a1 | 4624 | |
c06f04c7 BS |
4625 | /* |
4626 | * Add to the _head_ of the list, so that an already-started | |
4627 | * distribute_cfs_runtime will not see us | |
4628 | */ | |
4629 | list_add_rcu(&cfs_rq->throttled_list, &cfs_b->throttled_cfs_rq); | |
77a4d1a1 PZ |
4630 | |
4631 | /* | |
4632 | * If we're the first throttled task, make sure the bandwidth | |
4633 | * timer is running. | |
4634 | */ | |
4635 | if (empty) | |
4636 | start_cfs_bandwidth(cfs_b); | |
4637 | ||
85dac906 PT |
4638 | raw_spin_unlock(&cfs_b->lock); |
4639 | } | |
4640 | ||
029632fb | 4641 | void unthrottle_cfs_rq(struct cfs_rq *cfs_rq) |
671fd9da PT |
4642 | { |
4643 | struct rq *rq = rq_of(cfs_rq); | |
4644 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); | |
4645 | struct sched_entity *se; | |
4646 | int enqueue = 1; | |
4647 | long task_delta; | |
4648 | ||
22b958d8 | 4649 | se = cfs_rq->tg->se[cpu_of(rq)]; |
671fd9da PT |
4650 | |
4651 | cfs_rq->throttled = 0; | |
1a55af2e FW |
4652 | |
4653 | update_rq_clock(rq); | |
4654 | ||
671fd9da | 4655 | raw_spin_lock(&cfs_b->lock); |
78becc27 | 4656 | cfs_b->throttled_time += rq_clock(rq) - cfs_rq->throttled_clock; |
671fd9da PT |
4657 | list_del_rcu(&cfs_rq->throttled_list); |
4658 | raw_spin_unlock(&cfs_b->lock); | |
4659 | ||
64660c86 PT |
4660 | /* update hierarchical throttle state */ |
4661 | walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq); | |
4662 | ||
671fd9da PT |
4663 | if (!cfs_rq->load.weight) |
4664 | return; | |
4665 | ||
4666 | task_delta = cfs_rq->h_nr_running; | |
4667 | for_each_sched_entity(se) { | |
4668 | if (se->on_rq) | |
4669 | enqueue = 0; | |
4670 | ||
4671 | cfs_rq = cfs_rq_of(se); | |
4672 | if (enqueue) | |
4673 | enqueue_entity(cfs_rq, se, ENQUEUE_WAKEUP); | |
4674 | cfs_rq->h_nr_running += task_delta; | |
4675 | ||
4676 | if (cfs_rq_throttled(cfs_rq)) | |
4677 | break; | |
4678 | } | |
4679 | ||
4680 | if (!se) | |
72465447 | 4681 | add_nr_running(rq, task_delta); |
671fd9da | 4682 | |
97fb7a0a | 4683 | /* Determine whether we need to wake up potentially idle CPU: */ |
671fd9da | 4684 | if (rq->curr == rq->idle && rq->cfs.nr_running) |
8875125e | 4685 | resched_curr(rq); |
671fd9da PT |
4686 | } |
4687 | ||
4688 | static u64 distribute_cfs_runtime(struct cfs_bandwidth *cfs_b, | |
4689 | u64 remaining, u64 expires) | |
4690 | { | |
4691 | struct cfs_rq *cfs_rq; | |
c06f04c7 BS |
4692 | u64 runtime; |
4693 | u64 starting_runtime = remaining; | |
671fd9da PT |
4694 | |
4695 | rcu_read_lock(); | |
4696 | list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq, | |
4697 | throttled_list) { | |
4698 | struct rq *rq = rq_of(cfs_rq); | |
8a8c69c3 | 4699 | struct rq_flags rf; |
671fd9da | 4700 | |
8a8c69c3 | 4701 | rq_lock(rq, &rf); |
671fd9da PT |
4702 | if (!cfs_rq_throttled(cfs_rq)) |
4703 | goto next; | |
4704 | ||
4705 | runtime = -cfs_rq->runtime_remaining + 1; | |
4706 | if (runtime > remaining) | |
4707 | runtime = remaining; | |
4708 | remaining -= runtime; | |
4709 | ||
4710 | cfs_rq->runtime_remaining += runtime; | |
4711 | cfs_rq->runtime_expires = expires; | |
4712 | ||
4713 | /* we check whether we're throttled above */ | |
4714 | if (cfs_rq->runtime_remaining > 0) | |
4715 | unthrottle_cfs_rq(cfs_rq); | |
4716 | ||
4717 | next: | |
8a8c69c3 | 4718 | rq_unlock(rq, &rf); |
671fd9da PT |
4719 | |
4720 | if (!remaining) | |
4721 | break; | |
4722 | } | |
4723 | rcu_read_unlock(); | |
4724 | ||
c06f04c7 | 4725 | return starting_runtime - remaining; |
671fd9da PT |
4726 | } |
4727 | ||
58088ad0 PT |
4728 | /* |
4729 | * Responsible for refilling a task_group's bandwidth and unthrottling its | |
4730 | * cfs_rqs as appropriate. If there has been no activity within the last | |
4731 | * period the timer is deactivated until scheduling resumes; cfs_b->idle is | |
4732 | * used to track this state. | |
4733 | */ | |
4734 | static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun) | |
4735 | { | |
671fd9da | 4736 | u64 runtime, runtime_expires; |
51f2176d | 4737 | int throttled; |
58088ad0 | 4738 | |
58088ad0 PT |
4739 | /* no need to continue the timer with no bandwidth constraint */ |
4740 | if (cfs_b->quota == RUNTIME_INF) | |
51f2176d | 4741 | goto out_deactivate; |
58088ad0 | 4742 | |
671fd9da | 4743 | throttled = !list_empty(&cfs_b->throttled_cfs_rq); |
e8da1b18 | 4744 | cfs_b->nr_periods += overrun; |
671fd9da | 4745 | |
51f2176d BS |
4746 | /* |
4747 | * idle depends on !throttled (for the case of a large deficit), and if | |
4748 | * we're going inactive then everything else can be deferred | |
4749 | */ | |
4750 | if (cfs_b->idle && !throttled) | |
4751 | goto out_deactivate; | |
a9cf55b2 PT |
4752 | |
4753 | __refill_cfs_bandwidth_runtime(cfs_b); | |
4754 | ||
671fd9da PT |
4755 | if (!throttled) { |
4756 | /* mark as potentially idle for the upcoming period */ | |
4757 | cfs_b->idle = 1; | |
51f2176d | 4758 | return 0; |
671fd9da PT |
4759 | } |
4760 | ||
e8da1b18 NR |
4761 | /* account preceding periods in which throttling occurred */ |
4762 | cfs_b->nr_throttled += overrun; | |
4763 | ||
671fd9da | 4764 | runtime_expires = cfs_b->runtime_expires; |
671fd9da PT |
4765 | |
4766 | /* | |
c06f04c7 BS |
4767 | * This check is repeated as we are holding onto the new bandwidth while |
4768 | * we unthrottle. This can potentially race with an unthrottled group | |
4769 | * trying to acquire new bandwidth from the global pool. This can result | |
4770 | * in us over-using our runtime if it is all used during this loop, but | |
4771 | * only by limited amounts in that extreme case. | |
671fd9da | 4772 | */ |
c06f04c7 BS |
4773 | while (throttled && cfs_b->runtime > 0) { |
4774 | runtime = cfs_b->runtime; | |
671fd9da PT |
4775 | raw_spin_unlock(&cfs_b->lock); |
4776 | /* we can't nest cfs_b->lock while distributing bandwidth */ | |
4777 | runtime = distribute_cfs_runtime(cfs_b, runtime, | |
4778 | runtime_expires); | |
4779 | raw_spin_lock(&cfs_b->lock); | |
4780 | ||
4781 | throttled = !list_empty(&cfs_b->throttled_cfs_rq); | |
c06f04c7 BS |
4782 | |
4783 | cfs_b->runtime -= min(runtime, cfs_b->runtime); | |
671fd9da | 4784 | } |
58088ad0 | 4785 | |
671fd9da PT |
4786 | /* |
4787 | * While we are ensured activity in the period following an | |
4788 | * unthrottle, this also covers the case in which the new bandwidth is | |
4789 | * insufficient to cover the existing bandwidth deficit. (Forcing the | |
4790 | * timer to remain active while there are any throttled entities.) | |
4791 | */ | |
4792 | cfs_b->idle = 0; | |
58088ad0 | 4793 | |
51f2176d BS |
4794 | return 0; |
4795 | ||
4796 | out_deactivate: | |
51f2176d | 4797 | return 1; |
58088ad0 | 4798 | } |
d3d9dc33 | 4799 | |
d8b4986d PT |
4800 | /* a cfs_rq won't donate quota below this amount */ |
4801 | static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC; | |
4802 | /* minimum remaining period time to redistribute slack quota */ | |
4803 | static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC; | |
4804 | /* how long we wait to gather additional slack before distributing */ | |
4805 | static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC; | |
4806 | ||
db06e78c BS |
4807 | /* |
4808 | * Are we near the end of the current quota period? | |
4809 | * | |
4810 | * Requires cfs_b->lock for hrtimer_expires_remaining to be safe against the | |
4961b6e1 | 4811 | * hrtimer base being cleared by hrtimer_start. In the case of |
db06e78c BS |
4812 | * migrate_hrtimers, base is never cleared, so we are fine. |
4813 | */ | |
d8b4986d PT |
4814 | static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire) |
4815 | { | |
4816 | struct hrtimer *refresh_timer = &cfs_b->period_timer; | |
4817 | u64 remaining; | |
4818 | ||
4819 | /* if the call-back is running a quota refresh is already occurring */ | |
4820 | if (hrtimer_callback_running(refresh_timer)) | |
4821 | return 1; | |
4822 | ||
4823 | /* is a quota refresh about to occur? */ | |
4824 | remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer)); | |
4825 | if (remaining < min_expire) | |
4826 | return 1; | |
4827 | ||
4828 | return 0; | |
4829 | } | |
4830 | ||
4831 | static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b) | |
4832 | { | |
4833 | u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration; | |
4834 | ||
4835 | /* if there's a quota refresh soon don't bother with slack */ | |
4836 | if (runtime_refresh_within(cfs_b, min_left)) | |
4837 | return; | |
4838 | ||
4cfafd30 PZ |
4839 | hrtimer_start(&cfs_b->slack_timer, |
4840 | ns_to_ktime(cfs_bandwidth_slack_period), | |
4841 | HRTIMER_MODE_REL); | |
d8b4986d PT |
4842 | } |
4843 | ||
4844 | /* we know any runtime found here is valid as update_curr() precedes return */ | |
4845 | static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
4846 | { | |
4847 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); | |
4848 | s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime; | |
4849 | ||
4850 | if (slack_runtime <= 0) | |
4851 | return; | |
4852 | ||
4853 | raw_spin_lock(&cfs_b->lock); | |
4854 | if (cfs_b->quota != RUNTIME_INF && | |
4855 | cfs_rq->runtime_expires == cfs_b->runtime_expires) { | |
4856 | cfs_b->runtime += slack_runtime; | |
4857 | ||
4858 | /* we are under rq->lock, defer unthrottling using a timer */ | |
4859 | if (cfs_b->runtime > sched_cfs_bandwidth_slice() && | |
4860 | !list_empty(&cfs_b->throttled_cfs_rq)) | |
4861 | start_cfs_slack_bandwidth(cfs_b); | |
4862 | } | |
4863 | raw_spin_unlock(&cfs_b->lock); | |
4864 | ||
4865 | /* even if it's not valid for return we don't want to try again */ | |
4866 | cfs_rq->runtime_remaining -= slack_runtime; | |
4867 | } | |
4868 | ||
4869 | static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
4870 | { | |
56f570e5 PT |
4871 | if (!cfs_bandwidth_used()) |
4872 | return; | |
4873 | ||
fccfdc6f | 4874 | if (!cfs_rq->runtime_enabled || cfs_rq->nr_running) |
d8b4986d PT |
4875 | return; |
4876 | ||
4877 | __return_cfs_rq_runtime(cfs_rq); | |
4878 | } | |
4879 | ||
4880 | /* | |
4881 | * This is done with a timer (instead of inline with bandwidth return) since | |
4882 | * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs. | |
4883 | */ | |
4884 | static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b) | |
4885 | { | |
4886 | u64 runtime = 0, slice = sched_cfs_bandwidth_slice(); | |
4887 | u64 expires; | |
4888 | ||
4889 | /* confirm we're still not at a refresh boundary */ | |
db06e78c BS |
4890 | raw_spin_lock(&cfs_b->lock); |
4891 | if (runtime_refresh_within(cfs_b, min_bandwidth_expiration)) { | |
4892 | raw_spin_unlock(&cfs_b->lock); | |
d8b4986d | 4893 | return; |
db06e78c | 4894 | } |
d8b4986d | 4895 | |
c06f04c7 | 4896 | if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) |
d8b4986d | 4897 | runtime = cfs_b->runtime; |
c06f04c7 | 4898 | |
d8b4986d PT |
4899 | expires = cfs_b->runtime_expires; |
4900 | raw_spin_unlock(&cfs_b->lock); | |
4901 | ||
4902 | if (!runtime) | |
4903 | return; | |
4904 | ||
4905 | runtime = distribute_cfs_runtime(cfs_b, runtime, expires); | |
4906 | ||
4907 | raw_spin_lock(&cfs_b->lock); | |
4908 | if (expires == cfs_b->runtime_expires) | |
c06f04c7 | 4909 | cfs_b->runtime -= min(runtime, cfs_b->runtime); |
d8b4986d PT |
4910 | raw_spin_unlock(&cfs_b->lock); |
4911 | } | |
4912 | ||
d3d9dc33 PT |
4913 | /* |
4914 | * When a group wakes up we want to make sure that its quota is not already | |
4915 | * expired/exceeded, otherwise it may be allowed to steal additional ticks of | |
4916 | * runtime as update_curr() throttling can not not trigger until it's on-rq. | |
4917 | */ | |
4918 | static void check_enqueue_throttle(struct cfs_rq *cfs_rq) | |
4919 | { | |
56f570e5 PT |
4920 | if (!cfs_bandwidth_used()) |
4921 | return; | |
4922 | ||
d3d9dc33 PT |
4923 | /* an active group must be handled by the update_curr()->put() path */ |
4924 | if (!cfs_rq->runtime_enabled || cfs_rq->curr) | |
4925 | return; | |
4926 | ||
4927 | /* ensure the group is not already throttled */ | |
4928 | if (cfs_rq_throttled(cfs_rq)) | |
4929 | return; | |
4930 | ||
4931 | /* update runtime allocation */ | |
4932 | account_cfs_rq_runtime(cfs_rq, 0); | |
4933 | if (cfs_rq->runtime_remaining <= 0) | |
4934 | throttle_cfs_rq(cfs_rq); | |
4935 | } | |
4936 | ||
55e16d30 PZ |
4937 | static void sync_throttle(struct task_group *tg, int cpu) |
4938 | { | |
4939 | struct cfs_rq *pcfs_rq, *cfs_rq; | |
4940 | ||
4941 | if (!cfs_bandwidth_used()) | |
4942 | return; | |
4943 | ||
4944 | if (!tg->parent) | |
4945 | return; | |
4946 | ||
4947 | cfs_rq = tg->cfs_rq[cpu]; | |
4948 | pcfs_rq = tg->parent->cfs_rq[cpu]; | |
4949 | ||
4950 | cfs_rq->throttle_count = pcfs_rq->throttle_count; | |
b8922125 | 4951 | cfs_rq->throttled_clock_task = rq_clock_task(cpu_rq(cpu)); |
55e16d30 PZ |
4952 | } |
4953 | ||
d3d9dc33 | 4954 | /* conditionally throttle active cfs_rq's from put_prev_entity() */ |
678d5718 | 4955 | static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq) |
d3d9dc33 | 4956 | { |
56f570e5 | 4957 | if (!cfs_bandwidth_used()) |
678d5718 | 4958 | return false; |
56f570e5 | 4959 | |
d3d9dc33 | 4960 | if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0)) |
678d5718 | 4961 | return false; |
d3d9dc33 PT |
4962 | |
4963 | /* | |
4964 | * it's possible for a throttled entity to be forced into a running | |
4965 | * state (e.g. set_curr_task), in this case we're finished. | |
4966 | */ | |
4967 | if (cfs_rq_throttled(cfs_rq)) | |
678d5718 | 4968 | return true; |
d3d9dc33 PT |
4969 | |
4970 | throttle_cfs_rq(cfs_rq); | |
678d5718 | 4971 | return true; |
d3d9dc33 | 4972 | } |
029632fb | 4973 | |
029632fb PZ |
4974 | static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer) |
4975 | { | |
4976 | struct cfs_bandwidth *cfs_b = | |
4977 | container_of(timer, struct cfs_bandwidth, slack_timer); | |
77a4d1a1 | 4978 | |
029632fb PZ |
4979 | do_sched_cfs_slack_timer(cfs_b); |
4980 | ||
4981 | return HRTIMER_NORESTART; | |
4982 | } | |
4983 | ||
4984 | static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer) | |
4985 | { | |
4986 | struct cfs_bandwidth *cfs_b = | |
4987 | container_of(timer, struct cfs_bandwidth, period_timer); | |
029632fb PZ |
4988 | int overrun; |
4989 | int idle = 0; | |
4990 | ||
51f2176d | 4991 | raw_spin_lock(&cfs_b->lock); |
029632fb | 4992 | for (;;) { |
77a4d1a1 | 4993 | overrun = hrtimer_forward_now(timer, cfs_b->period); |
029632fb PZ |
4994 | if (!overrun) |
4995 | break; | |
4996 | ||
4997 | idle = do_sched_cfs_period_timer(cfs_b, overrun); | |
4998 | } | |
4cfafd30 PZ |
4999 | if (idle) |
5000 | cfs_b->period_active = 0; | |
51f2176d | 5001 | raw_spin_unlock(&cfs_b->lock); |
029632fb PZ |
5002 | |
5003 | return idle ? HRTIMER_NORESTART : HRTIMER_RESTART; | |
5004 | } | |
5005 | ||
5006 | void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) | |
5007 | { | |
5008 | raw_spin_lock_init(&cfs_b->lock); | |
5009 | cfs_b->runtime = 0; | |
5010 | cfs_b->quota = RUNTIME_INF; | |
5011 | cfs_b->period = ns_to_ktime(default_cfs_period()); | |
5012 | ||
5013 | INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq); | |
4cfafd30 | 5014 | hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED); |
029632fb PZ |
5015 | cfs_b->period_timer.function = sched_cfs_period_timer; |
5016 | hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); | |
5017 | cfs_b->slack_timer.function = sched_cfs_slack_timer; | |
5018 | } | |
5019 | ||
5020 | static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
5021 | { | |
5022 | cfs_rq->runtime_enabled = 0; | |
5023 | INIT_LIST_HEAD(&cfs_rq->throttled_list); | |
5024 | } | |
5025 | ||
77a4d1a1 | 5026 | void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b) |
029632fb | 5027 | { |
4cfafd30 | 5028 | lockdep_assert_held(&cfs_b->lock); |
029632fb | 5029 | |
4cfafd30 PZ |
5030 | if (!cfs_b->period_active) { |
5031 | cfs_b->period_active = 1; | |
5032 | hrtimer_forward_now(&cfs_b->period_timer, cfs_b->period); | |
5033 | hrtimer_start_expires(&cfs_b->period_timer, HRTIMER_MODE_ABS_PINNED); | |
5034 | } | |
029632fb PZ |
5035 | } |
5036 | ||
5037 | static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) | |
5038 | { | |
7f1a169b TH |
5039 | /* init_cfs_bandwidth() was not called */ |
5040 | if (!cfs_b->throttled_cfs_rq.next) | |
5041 | return; | |
5042 | ||
029632fb PZ |
5043 | hrtimer_cancel(&cfs_b->period_timer); |
5044 | hrtimer_cancel(&cfs_b->slack_timer); | |
5045 | } | |
5046 | ||
502ce005 | 5047 | /* |
97fb7a0a | 5048 | * Both these CPU hotplug callbacks race against unregister_fair_sched_group() |
502ce005 PZ |
5049 | * |
5050 | * The race is harmless, since modifying bandwidth settings of unhooked group | |
5051 | * bits doesn't do much. | |
5052 | */ | |
5053 | ||
5054 | /* cpu online calback */ | |
0e59bdae KT |
5055 | static void __maybe_unused update_runtime_enabled(struct rq *rq) |
5056 | { | |
502ce005 | 5057 | struct task_group *tg; |
0e59bdae | 5058 | |
502ce005 PZ |
5059 | lockdep_assert_held(&rq->lock); |
5060 | ||
5061 | rcu_read_lock(); | |
5062 | list_for_each_entry_rcu(tg, &task_groups, list) { | |
5063 | struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth; | |
5064 | struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; | |
0e59bdae KT |
5065 | |
5066 | raw_spin_lock(&cfs_b->lock); | |
5067 | cfs_rq->runtime_enabled = cfs_b->quota != RUNTIME_INF; | |
5068 | raw_spin_unlock(&cfs_b->lock); | |
5069 | } | |
502ce005 | 5070 | rcu_read_unlock(); |
0e59bdae KT |
5071 | } |
5072 | ||
502ce005 | 5073 | /* cpu offline callback */ |
38dc3348 | 5074 | static void __maybe_unused unthrottle_offline_cfs_rqs(struct rq *rq) |
029632fb | 5075 | { |
502ce005 PZ |
5076 | struct task_group *tg; |
5077 | ||
5078 | lockdep_assert_held(&rq->lock); | |
5079 | ||
5080 | rcu_read_lock(); | |
5081 | list_for_each_entry_rcu(tg, &task_groups, list) { | |
5082 | struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; | |
029632fb | 5083 | |
029632fb PZ |
5084 | if (!cfs_rq->runtime_enabled) |
5085 | continue; | |
5086 | ||
5087 | /* | |
5088 | * clock_task is not advancing so we just need to make sure | |
5089 | * there's some valid quota amount | |
5090 | */ | |
51f2176d | 5091 | cfs_rq->runtime_remaining = 1; |
0e59bdae | 5092 | /* |
97fb7a0a | 5093 | * Offline rq is schedulable till CPU is completely disabled |
0e59bdae KT |
5094 | * in take_cpu_down(), so we prevent new cfs throttling here. |
5095 | */ | |
5096 | cfs_rq->runtime_enabled = 0; | |
5097 | ||
029632fb PZ |
5098 | if (cfs_rq_throttled(cfs_rq)) |
5099 | unthrottle_cfs_rq(cfs_rq); | |
5100 | } | |
502ce005 | 5101 | rcu_read_unlock(); |
029632fb PZ |
5102 | } |
5103 | ||
5104 | #else /* CONFIG_CFS_BANDWIDTH */ | |
f1b17280 PT |
5105 | static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq) |
5106 | { | |
78becc27 | 5107 | return rq_clock_task(rq_of(cfs_rq)); |
f1b17280 PT |
5108 | } |
5109 | ||
9dbdb155 | 5110 | static void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec) {} |
678d5718 | 5111 | static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq) { return false; } |
d3d9dc33 | 5112 | static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {} |
55e16d30 | 5113 | static inline void sync_throttle(struct task_group *tg, int cpu) {} |
6c16a6dc | 5114 | static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {} |
85dac906 PT |
5115 | |
5116 | static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq) | |
5117 | { | |
5118 | return 0; | |
5119 | } | |
64660c86 PT |
5120 | |
5121 | static inline int throttled_hierarchy(struct cfs_rq *cfs_rq) | |
5122 | { | |
5123 | return 0; | |
5124 | } | |
5125 | ||
5126 | static inline int throttled_lb_pair(struct task_group *tg, | |
5127 | int src_cpu, int dest_cpu) | |
5128 | { | |
5129 | return 0; | |
5130 | } | |
029632fb PZ |
5131 | |
5132 | void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {} | |
5133 | ||
5134 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
5135 | static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {} | |
ab84d31e PT |
5136 | #endif |
5137 | ||
029632fb PZ |
5138 | static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg) |
5139 | { | |
5140 | return NULL; | |
5141 | } | |
5142 | static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {} | |
0e59bdae | 5143 | static inline void update_runtime_enabled(struct rq *rq) {} |
a4c96ae3 | 5144 | static inline void unthrottle_offline_cfs_rqs(struct rq *rq) {} |
029632fb PZ |
5145 | |
5146 | #endif /* CONFIG_CFS_BANDWIDTH */ | |
5147 | ||
bf0f6f24 IM |
5148 | /************************************************** |
5149 | * CFS operations on tasks: | |
5150 | */ | |
5151 | ||
8f4d37ec PZ |
5152 | #ifdef CONFIG_SCHED_HRTICK |
5153 | static void hrtick_start_fair(struct rq *rq, struct task_struct *p) | |
5154 | { | |
8f4d37ec PZ |
5155 | struct sched_entity *se = &p->se; |
5156 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
5157 | ||
9148a3a1 | 5158 | SCHED_WARN_ON(task_rq(p) != rq); |
8f4d37ec | 5159 | |
8bf46a39 | 5160 | if (rq->cfs.h_nr_running > 1) { |
8f4d37ec PZ |
5161 | u64 slice = sched_slice(cfs_rq, se); |
5162 | u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime; | |
5163 | s64 delta = slice - ran; | |
5164 | ||
5165 | if (delta < 0) { | |
5166 | if (rq->curr == p) | |
8875125e | 5167 | resched_curr(rq); |
8f4d37ec PZ |
5168 | return; |
5169 | } | |
31656519 | 5170 | hrtick_start(rq, delta); |
8f4d37ec PZ |
5171 | } |
5172 | } | |
a4c2f00f PZ |
5173 | |
5174 | /* | |
5175 | * called from enqueue/dequeue and updates the hrtick when the | |
5176 | * current task is from our class and nr_running is low enough | |
5177 | * to matter. | |
5178 | */ | |
5179 | static void hrtick_update(struct rq *rq) | |
5180 | { | |
5181 | struct task_struct *curr = rq->curr; | |
5182 | ||
b39e66ea | 5183 | if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class) |
a4c2f00f PZ |
5184 | return; |
5185 | ||
5186 | if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency) | |
5187 | hrtick_start_fair(rq, curr); | |
5188 | } | |
55e12e5e | 5189 | #else /* !CONFIG_SCHED_HRTICK */ |
8f4d37ec PZ |
5190 | static inline void |
5191 | hrtick_start_fair(struct rq *rq, struct task_struct *p) | |
5192 | { | |
5193 | } | |
a4c2f00f PZ |
5194 | |
5195 | static inline void hrtick_update(struct rq *rq) | |
5196 | { | |
5197 | } | |
8f4d37ec PZ |
5198 | #endif |
5199 | ||
bf0f6f24 IM |
5200 | /* |
5201 | * The enqueue_task method is called before nr_running is | |
5202 | * increased. Here we update the fair scheduling stats and | |
5203 | * then put the task into the rbtree: | |
5204 | */ | |
ea87bb78 | 5205 | static void |
371fd7e7 | 5206 | enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags) |
bf0f6f24 IM |
5207 | { |
5208 | struct cfs_rq *cfs_rq; | |
62fb1851 | 5209 | struct sched_entity *se = &p->se; |
bf0f6f24 | 5210 | |
8c34ab19 RW |
5211 | /* |
5212 | * If in_iowait is set, the code below may not trigger any cpufreq | |
5213 | * utilization updates, so do it here explicitly with the IOWAIT flag | |
5214 | * passed. | |
5215 | */ | |
5216 | if (p->in_iowait) | |
674e7541 | 5217 | cpufreq_update_util(rq, SCHED_CPUFREQ_IOWAIT); |
8c34ab19 | 5218 | |
bf0f6f24 | 5219 | for_each_sched_entity(se) { |
62fb1851 | 5220 | if (se->on_rq) |
bf0f6f24 IM |
5221 | break; |
5222 | cfs_rq = cfs_rq_of(se); | |
88ec22d3 | 5223 | enqueue_entity(cfs_rq, se, flags); |
85dac906 PT |
5224 | |
5225 | /* | |
5226 | * end evaluation on encountering a throttled cfs_rq | |
5227 | * | |
5228 | * note: in the case of encountering a throttled cfs_rq we will | |
5229 | * post the final h_nr_running increment below. | |
e210bffd | 5230 | */ |
85dac906 PT |
5231 | if (cfs_rq_throttled(cfs_rq)) |
5232 | break; | |
953bfcd1 | 5233 | cfs_rq->h_nr_running++; |
85dac906 | 5234 | |
88ec22d3 | 5235 | flags = ENQUEUE_WAKEUP; |
bf0f6f24 | 5236 | } |
8f4d37ec | 5237 | |
2069dd75 | 5238 | for_each_sched_entity(se) { |
0f317143 | 5239 | cfs_rq = cfs_rq_of(se); |
953bfcd1 | 5240 | cfs_rq->h_nr_running++; |
2069dd75 | 5241 | |
85dac906 PT |
5242 | if (cfs_rq_throttled(cfs_rq)) |
5243 | break; | |
5244 | ||
88c0616e | 5245 | update_load_avg(cfs_rq, se, UPDATE_TG); |
1ea6c46a | 5246 | update_cfs_group(se); |
2069dd75 PZ |
5247 | } |
5248 | ||
cd126afe | 5249 | if (!se) |
72465447 | 5250 | add_nr_running(rq, 1); |
cd126afe | 5251 | |
a4c2f00f | 5252 | hrtick_update(rq); |
bf0f6f24 IM |
5253 | } |
5254 | ||
2f36825b VP |
5255 | static void set_next_buddy(struct sched_entity *se); |
5256 | ||
bf0f6f24 IM |
5257 | /* |
5258 | * The dequeue_task method is called before nr_running is | |
5259 | * decreased. We remove the task from the rbtree and | |
5260 | * update the fair scheduling stats: | |
5261 | */ | |
371fd7e7 | 5262 | static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags) |
bf0f6f24 IM |
5263 | { |
5264 | struct cfs_rq *cfs_rq; | |
62fb1851 | 5265 | struct sched_entity *se = &p->se; |
2f36825b | 5266 | int task_sleep = flags & DEQUEUE_SLEEP; |
bf0f6f24 IM |
5267 | |
5268 | for_each_sched_entity(se) { | |
5269 | cfs_rq = cfs_rq_of(se); | |
371fd7e7 | 5270 | dequeue_entity(cfs_rq, se, flags); |
85dac906 PT |
5271 | |
5272 | /* | |
5273 | * end evaluation on encountering a throttled cfs_rq | |
5274 | * | |
5275 | * note: in the case of encountering a throttled cfs_rq we will | |
5276 | * post the final h_nr_running decrement below. | |
5277 | */ | |
5278 | if (cfs_rq_throttled(cfs_rq)) | |
5279 | break; | |
953bfcd1 | 5280 | cfs_rq->h_nr_running--; |
2069dd75 | 5281 | |
bf0f6f24 | 5282 | /* Don't dequeue parent if it has other entities besides us */ |
2f36825b | 5283 | if (cfs_rq->load.weight) { |
754bd598 KK |
5284 | /* Avoid re-evaluating load for this entity: */ |
5285 | se = parent_entity(se); | |
2f36825b VP |
5286 | /* |
5287 | * Bias pick_next to pick a task from this cfs_rq, as | |
5288 | * p is sleeping when it is within its sched_slice. | |
5289 | */ | |
754bd598 KK |
5290 | if (task_sleep && se && !throttled_hierarchy(cfs_rq)) |
5291 | set_next_buddy(se); | |
bf0f6f24 | 5292 | break; |
2f36825b | 5293 | } |
371fd7e7 | 5294 | flags |= DEQUEUE_SLEEP; |
bf0f6f24 | 5295 | } |
8f4d37ec | 5296 | |
2069dd75 | 5297 | for_each_sched_entity(se) { |
0f317143 | 5298 | cfs_rq = cfs_rq_of(se); |
953bfcd1 | 5299 | cfs_rq->h_nr_running--; |
2069dd75 | 5300 | |
85dac906 PT |
5301 | if (cfs_rq_throttled(cfs_rq)) |
5302 | break; | |
5303 | ||
88c0616e | 5304 | update_load_avg(cfs_rq, se, UPDATE_TG); |
1ea6c46a | 5305 | update_cfs_group(se); |
2069dd75 PZ |
5306 | } |
5307 | ||
cd126afe | 5308 | if (!se) |
72465447 | 5309 | sub_nr_running(rq, 1); |
cd126afe | 5310 | |
a4c2f00f | 5311 | hrtick_update(rq); |
bf0f6f24 IM |
5312 | } |
5313 | ||
e7693a36 | 5314 | #ifdef CONFIG_SMP |
10e2f1ac PZ |
5315 | |
5316 | /* Working cpumask for: load_balance, load_balance_newidle. */ | |
5317 | DEFINE_PER_CPU(cpumask_var_t, load_balance_mask); | |
5318 | DEFINE_PER_CPU(cpumask_var_t, select_idle_mask); | |
5319 | ||
9fd81dd5 | 5320 | #ifdef CONFIG_NO_HZ_COMMON |
3289bdb4 PZ |
5321 | /* |
5322 | * per rq 'load' arrray crap; XXX kill this. | |
5323 | */ | |
5324 | ||
5325 | /* | |
d937cdc5 | 5326 | * The exact cpuload calculated at every tick would be: |
3289bdb4 | 5327 | * |
d937cdc5 PZ |
5328 | * load' = (1 - 1/2^i) * load + (1/2^i) * cur_load |
5329 | * | |
97fb7a0a IM |
5330 | * If a CPU misses updates for n ticks (as it was idle) and update gets |
5331 | * called on the n+1-th tick when CPU may be busy, then we have: | |
d937cdc5 PZ |
5332 | * |
5333 | * load_n = (1 - 1/2^i)^n * load_0 | |
5334 | * load_n+1 = (1 - 1/2^i) * load_n + (1/2^i) * cur_load | |
3289bdb4 PZ |
5335 | * |
5336 | * decay_load_missed() below does efficient calculation of | |
3289bdb4 | 5337 | * |
d937cdc5 PZ |
5338 | * load' = (1 - 1/2^i)^n * load |
5339 | * | |
5340 | * Because x^(n+m) := x^n * x^m we can decompose any x^n in power-of-2 factors. | |
5341 | * This allows us to precompute the above in said factors, thereby allowing the | |
5342 | * reduction of an arbitrary n in O(log_2 n) steps. (See also | |
5343 | * fixed_power_int()) | |
3289bdb4 | 5344 | * |
d937cdc5 | 5345 | * The calculation is approximated on a 128 point scale. |
3289bdb4 PZ |
5346 | */ |
5347 | #define DEGRADE_SHIFT 7 | |
d937cdc5 PZ |
5348 | |
5349 | static const u8 degrade_zero_ticks[CPU_LOAD_IDX_MAX] = {0, 8, 32, 64, 128}; | |
5350 | static const u8 degrade_factor[CPU_LOAD_IDX_MAX][DEGRADE_SHIFT + 1] = { | |
5351 | { 0, 0, 0, 0, 0, 0, 0, 0 }, | |
5352 | { 64, 32, 8, 0, 0, 0, 0, 0 }, | |
5353 | { 96, 72, 40, 12, 1, 0, 0, 0 }, | |
5354 | { 112, 98, 75, 43, 15, 1, 0, 0 }, | |
5355 | { 120, 112, 98, 76, 45, 16, 2, 0 } | |
5356 | }; | |
3289bdb4 PZ |
5357 | |
5358 | /* | |
5359 | * Update cpu_load for any missed ticks, due to tickless idle. The backlog | |
5360 | * would be when CPU is idle and so we just decay the old load without | |
5361 | * adding any new load. | |
5362 | */ | |
5363 | static unsigned long | |
5364 | decay_load_missed(unsigned long load, unsigned long missed_updates, int idx) | |
5365 | { | |
5366 | int j = 0; | |
5367 | ||
5368 | if (!missed_updates) | |
5369 | return load; | |
5370 | ||
5371 | if (missed_updates >= degrade_zero_ticks[idx]) | |
5372 | return 0; | |
5373 | ||
5374 | if (idx == 1) | |
5375 | return load >> missed_updates; | |
5376 | ||
5377 | while (missed_updates) { | |
5378 | if (missed_updates % 2) | |
5379 | load = (load * degrade_factor[idx][j]) >> DEGRADE_SHIFT; | |
5380 | ||
5381 | missed_updates >>= 1; | |
5382 | j++; | |
5383 | } | |
5384 | return load; | |
5385 | } | |
e022e0d3 PZ |
5386 | |
5387 | static struct { | |
5388 | cpumask_var_t idle_cpus_mask; | |
5389 | atomic_t nr_cpus; | |
f643ea22 | 5390 | int has_blocked; /* Idle CPUS has blocked load */ |
e022e0d3 | 5391 | unsigned long next_balance; /* in jiffy units */ |
f643ea22 | 5392 | unsigned long next_blocked; /* Next update of blocked load in jiffies */ |
e022e0d3 PZ |
5393 | } nohz ____cacheline_aligned; |
5394 | ||
9fd81dd5 | 5395 | #endif /* CONFIG_NO_HZ_COMMON */ |
3289bdb4 | 5396 | |
59543275 | 5397 | /** |
cee1afce | 5398 | * __cpu_load_update - update the rq->cpu_load[] statistics |
59543275 BP |
5399 | * @this_rq: The rq to update statistics for |
5400 | * @this_load: The current load | |
5401 | * @pending_updates: The number of missed updates | |
59543275 | 5402 | * |
3289bdb4 | 5403 | * Update rq->cpu_load[] statistics. This function is usually called every |
59543275 BP |
5404 | * scheduler tick (TICK_NSEC). |
5405 | * | |
5406 | * This function computes a decaying average: | |
5407 | * | |
5408 | * load[i]' = (1 - 1/2^i) * load[i] + (1/2^i) * load | |
5409 | * | |
5410 | * Because of NOHZ it might not get called on every tick which gives need for | |
5411 | * the @pending_updates argument. | |
5412 | * | |
5413 | * load[i]_n = (1 - 1/2^i) * load[i]_n-1 + (1/2^i) * load_n-1 | |
5414 | * = A * load[i]_n-1 + B ; A := (1 - 1/2^i), B := (1/2^i) * load | |
5415 | * = A * (A * load[i]_n-2 + B) + B | |
5416 | * = A * (A * (A * load[i]_n-3 + B) + B) + B | |
5417 | * = A^3 * load[i]_n-3 + (A^2 + A + 1) * B | |
5418 | * = A^n * load[i]_0 + (A^(n-1) + A^(n-2) + ... + 1) * B | |
5419 | * = A^n * load[i]_0 + ((1 - A^n) / (1 - A)) * B | |
5420 | * = (1 - 1/2^i)^n * (load[i]_0 - load) + load | |
5421 | * | |
5422 | * In the above we've assumed load_n := load, which is true for NOHZ_FULL as | |
5423 | * any change in load would have resulted in the tick being turned back on. | |
5424 | * | |
5425 | * For regular NOHZ, this reduces to: | |
5426 | * | |
5427 | * load[i]_n = (1 - 1/2^i)^n * load[i]_0 | |
5428 | * | |
5429 | * see decay_load_misses(). For NOHZ_FULL we get to subtract and add the extra | |
1f41906a | 5430 | * term. |
3289bdb4 | 5431 | */ |
1f41906a FW |
5432 | static void cpu_load_update(struct rq *this_rq, unsigned long this_load, |
5433 | unsigned long pending_updates) | |
3289bdb4 | 5434 | { |
9fd81dd5 | 5435 | unsigned long __maybe_unused tickless_load = this_rq->cpu_load[0]; |
3289bdb4 PZ |
5436 | int i, scale; |
5437 | ||
5438 | this_rq->nr_load_updates++; | |
5439 | ||
5440 | /* Update our load: */ | |
5441 | this_rq->cpu_load[0] = this_load; /* Fasttrack for idx 0 */ | |
5442 | for (i = 1, scale = 2; i < CPU_LOAD_IDX_MAX; i++, scale += scale) { | |
5443 | unsigned long old_load, new_load; | |
5444 | ||
5445 | /* scale is effectively 1 << i now, and >> i divides by scale */ | |
5446 | ||
7400d3bb | 5447 | old_load = this_rq->cpu_load[i]; |
9fd81dd5 | 5448 | #ifdef CONFIG_NO_HZ_COMMON |
3289bdb4 | 5449 | old_load = decay_load_missed(old_load, pending_updates - 1, i); |
7400d3bb BP |
5450 | if (tickless_load) { |
5451 | old_load -= decay_load_missed(tickless_load, pending_updates - 1, i); | |
5452 | /* | |
5453 | * old_load can never be a negative value because a | |
5454 | * decayed tickless_load cannot be greater than the | |
5455 | * original tickless_load. | |
5456 | */ | |
5457 | old_load += tickless_load; | |
5458 | } | |
9fd81dd5 | 5459 | #endif |
3289bdb4 PZ |
5460 | new_load = this_load; |
5461 | /* | |
5462 | * Round up the averaging division if load is increasing. This | |
5463 | * prevents us from getting stuck on 9 if the load is 10, for | |
5464 | * example. | |
5465 | */ | |
5466 | if (new_load > old_load) | |
5467 | new_load += scale - 1; | |
5468 | ||
5469 | this_rq->cpu_load[i] = (old_load * (scale - 1) + new_load) >> i; | |
5470 | } | |
5471 | ||
5472 | sched_avg_update(this_rq); | |
5473 | } | |
5474 | ||
7ea241af | 5475 | /* Used instead of source_load when we know the type == 0 */ |
c7132dd6 | 5476 | static unsigned long weighted_cpuload(struct rq *rq) |
7ea241af | 5477 | { |
c7132dd6 | 5478 | return cfs_rq_runnable_load_avg(&rq->cfs); |
7ea241af YD |
5479 | } |
5480 | ||
3289bdb4 | 5481 | #ifdef CONFIG_NO_HZ_COMMON |
1f41906a FW |
5482 | /* |
5483 | * There is no sane way to deal with nohz on smp when using jiffies because the | |
97fb7a0a | 5484 | * CPU doing the jiffies update might drift wrt the CPU doing the jiffy reading |
1f41906a FW |
5485 | * causing off-by-one errors in observed deltas; {0,2} instead of {1,1}. |
5486 | * | |
5487 | * Therefore we need to avoid the delta approach from the regular tick when | |
5488 | * possible since that would seriously skew the load calculation. This is why we | |
5489 | * use cpu_load_update_periodic() for CPUs out of nohz. However we'll rely on | |
5490 | * jiffies deltas for updates happening while in nohz mode (idle ticks, idle | |
5491 | * loop exit, nohz_idle_balance, nohz full exit...) | |
5492 | * | |
5493 | * This means we might still be one tick off for nohz periods. | |
5494 | */ | |
5495 | ||
5496 | static void cpu_load_update_nohz(struct rq *this_rq, | |
5497 | unsigned long curr_jiffies, | |
5498 | unsigned long load) | |
be68a682 FW |
5499 | { |
5500 | unsigned long pending_updates; | |
5501 | ||
5502 | pending_updates = curr_jiffies - this_rq->last_load_update_tick; | |
5503 | if (pending_updates) { | |
5504 | this_rq->last_load_update_tick = curr_jiffies; | |
5505 | /* | |
5506 | * In the regular NOHZ case, we were idle, this means load 0. | |
5507 | * In the NOHZ_FULL case, we were non-idle, we should consider | |
5508 | * its weighted load. | |
5509 | */ | |
1f41906a | 5510 | cpu_load_update(this_rq, load, pending_updates); |
be68a682 FW |
5511 | } |
5512 | } | |
5513 | ||
3289bdb4 PZ |
5514 | /* |
5515 | * Called from nohz_idle_balance() to update the load ratings before doing the | |
5516 | * idle balance. | |
5517 | */ | |
cee1afce | 5518 | static void cpu_load_update_idle(struct rq *this_rq) |
3289bdb4 | 5519 | { |
3289bdb4 PZ |
5520 | /* |
5521 | * bail if there's load or we're actually up-to-date. | |
5522 | */ | |
c7132dd6 | 5523 | if (weighted_cpuload(this_rq)) |
3289bdb4 PZ |
5524 | return; |
5525 | ||
1f41906a | 5526 | cpu_load_update_nohz(this_rq, READ_ONCE(jiffies), 0); |
3289bdb4 PZ |
5527 | } |
5528 | ||
5529 | /* | |
1f41906a FW |
5530 | * Record CPU load on nohz entry so we know the tickless load to account |
5531 | * on nohz exit. cpu_load[0] happens then to be updated more frequently | |
5532 | * than other cpu_load[idx] but it should be fine as cpu_load readers | |
5533 | * shouldn't rely into synchronized cpu_load[*] updates. | |
3289bdb4 | 5534 | */ |
1f41906a | 5535 | void cpu_load_update_nohz_start(void) |
3289bdb4 PZ |
5536 | { |
5537 | struct rq *this_rq = this_rq(); | |
1f41906a FW |
5538 | |
5539 | /* | |
5540 | * This is all lockless but should be fine. If weighted_cpuload changes | |
5541 | * concurrently we'll exit nohz. And cpu_load write can race with | |
5542 | * cpu_load_update_idle() but both updater would be writing the same. | |
5543 | */ | |
c7132dd6 | 5544 | this_rq->cpu_load[0] = weighted_cpuload(this_rq); |
1f41906a FW |
5545 | } |
5546 | ||
5547 | /* | |
5548 | * Account the tickless load in the end of a nohz frame. | |
5549 | */ | |
5550 | void cpu_load_update_nohz_stop(void) | |
5551 | { | |
316c1608 | 5552 | unsigned long curr_jiffies = READ_ONCE(jiffies); |
1f41906a FW |
5553 | struct rq *this_rq = this_rq(); |
5554 | unsigned long load; | |
8a8c69c3 | 5555 | struct rq_flags rf; |
3289bdb4 PZ |
5556 | |
5557 | if (curr_jiffies == this_rq->last_load_update_tick) | |
5558 | return; | |
5559 | ||
c7132dd6 | 5560 | load = weighted_cpuload(this_rq); |
8a8c69c3 | 5561 | rq_lock(this_rq, &rf); |
b52fad2d | 5562 | update_rq_clock(this_rq); |
1f41906a | 5563 | cpu_load_update_nohz(this_rq, curr_jiffies, load); |
8a8c69c3 | 5564 | rq_unlock(this_rq, &rf); |
3289bdb4 | 5565 | } |
1f41906a FW |
5566 | #else /* !CONFIG_NO_HZ_COMMON */ |
5567 | static inline void cpu_load_update_nohz(struct rq *this_rq, | |
5568 | unsigned long curr_jiffies, | |
5569 | unsigned long load) { } | |
5570 | #endif /* CONFIG_NO_HZ_COMMON */ | |
5571 | ||
5572 | static void cpu_load_update_periodic(struct rq *this_rq, unsigned long load) | |
5573 | { | |
9fd81dd5 | 5574 | #ifdef CONFIG_NO_HZ_COMMON |
1f41906a FW |
5575 | /* See the mess around cpu_load_update_nohz(). */ |
5576 | this_rq->last_load_update_tick = READ_ONCE(jiffies); | |
9fd81dd5 | 5577 | #endif |
1f41906a FW |
5578 | cpu_load_update(this_rq, load, 1); |
5579 | } | |
3289bdb4 PZ |
5580 | |
5581 | /* | |
5582 | * Called from scheduler_tick() | |
5583 | */ | |
cee1afce | 5584 | void cpu_load_update_active(struct rq *this_rq) |
3289bdb4 | 5585 | { |
c7132dd6 | 5586 | unsigned long load = weighted_cpuload(this_rq); |
1f41906a FW |
5587 | |
5588 | if (tick_nohz_tick_stopped()) | |
5589 | cpu_load_update_nohz(this_rq, READ_ONCE(jiffies), load); | |
5590 | else | |
5591 | cpu_load_update_periodic(this_rq, load); | |
3289bdb4 PZ |
5592 | } |
5593 | ||
029632fb | 5594 | /* |
97fb7a0a | 5595 | * Return a low guess at the load of a migration-source CPU weighted |
029632fb PZ |
5596 | * according to the scheduling class and "nice" value. |
5597 | * | |
5598 | * We want to under-estimate the load of migration sources, to | |
5599 | * balance conservatively. | |
5600 | */ | |
5601 | static unsigned long source_load(int cpu, int type) | |
5602 | { | |
5603 | struct rq *rq = cpu_rq(cpu); | |
c7132dd6 | 5604 | unsigned long total = weighted_cpuload(rq); |
029632fb PZ |
5605 | |
5606 | if (type == 0 || !sched_feat(LB_BIAS)) | |
5607 | return total; | |
5608 | ||
5609 | return min(rq->cpu_load[type-1], total); | |
5610 | } | |
5611 | ||
5612 | /* | |
97fb7a0a | 5613 | * Return a high guess at the load of a migration-target CPU weighted |
029632fb PZ |
5614 | * according to the scheduling class and "nice" value. |
5615 | */ | |
5616 | static unsigned long target_load(int cpu, int type) | |
5617 | { | |
5618 | struct rq *rq = cpu_rq(cpu); | |
c7132dd6 | 5619 | unsigned long total = weighted_cpuload(rq); |
029632fb PZ |
5620 | |
5621 | if (type == 0 || !sched_feat(LB_BIAS)) | |
5622 | return total; | |
5623 | ||
5624 | return max(rq->cpu_load[type-1], total); | |
5625 | } | |
5626 | ||
ced549fa | 5627 | static unsigned long capacity_of(int cpu) |
029632fb | 5628 | { |
ced549fa | 5629 | return cpu_rq(cpu)->cpu_capacity; |
029632fb PZ |
5630 | } |
5631 | ||
ca6d75e6 VG |
5632 | static unsigned long capacity_orig_of(int cpu) |
5633 | { | |
5634 | return cpu_rq(cpu)->cpu_capacity_orig; | |
5635 | } | |
5636 | ||
029632fb PZ |
5637 | static unsigned long cpu_avg_load_per_task(int cpu) |
5638 | { | |
5639 | struct rq *rq = cpu_rq(cpu); | |
316c1608 | 5640 | unsigned long nr_running = READ_ONCE(rq->cfs.h_nr_running); |
c7132dd6 | 5641 | unsigned long load_avg = weighted_cpuload(rq); |
029632fb PZ |
5642 | |
5643 | if (nr_running) | |
b92486cb | 5644 | return load_avg / nr_running; |
029632fb PZ |
5645 | |
5646 | return 0; | |
5647 | } | |
5648 | ||
c58d25f3 PZ |
5649 | static void record_wakee(struct task_struct *p) |
5650 | { | |
5651 | /* | |
5652 | * Only decay a single time; tasks that have less then 1 wakeup per | |
5653 | * jiffy will not have built up many flips. | |
5654 | */ | |
5655 | if (time_after(jiffies, current->wakee_flip_decay_ts + HZ)) { | |
5656 | current->wakee_flips >>= 1; | |
5657 | current->wakee_flip_decay_ts = jiffies; | |
5658 | } | |
5659 | ||
5660 | if (current->last_wakee != p) { | |
5661 | current->last_wakee = p; | |
5662 | current->wakee_flips++; | |
5663 | } | |
5664 | } | |
5665 | ||
63b0e9ed MG |
5666 | /* |
5667 | * Detect M:N waker/wakee relationships via a switching-frequency heuristic. | |
c58d25f3 | 5668 | * |
63b0e9ed | 5669 | * A waker of many should wake a different task than the one last awakened |
c58d25f3 PZ |
5670 | * at a frequency roughly N times higher than one of its wakees. |
5671 | * | |
5672 | * In order to determine whether we should let the load spread vs consolidating | |
5673 | * to shared cache, we look for a minimum 'flip' frequency of llc_size in one | |
5674 | * partner, and a factor of lls_size higher frequency in the other. | |
5675 | * | |
5676 | * With both conditions met, we can be relatively sure that the relationship is | |
5677 | * non-monogamous, with partner count exceeding socket size. | |
5678 | * | |
5679 | * Waker/wakee being client/server, worker/dispatcher, interrupt source or | |
5680 | * whatever is irrelevant, spread criteria is apparent partner count exceeds | |
5681 | * socket size. | |
63b0e9ed | 5682 | */ |
62470419 MW |
5683 | static int wake_wide(struct task_struct *p) |
5684 | { | |
63b0e9ed MG |
5685 | unsigned int master = current->wakee_flips; |
5686 | unsigned int slave = p->wakee_flips; | |
7d9ffa89 | 5687 | int factor = this_cpu_read(sd_llc_size); |
62470419 | 5688 | |
63b0e9ed MG |
5689 | if (master < slave) |
5690 | swap(master, slave); | |
5691 | if (slave < factor || master < slave * factor) | |
5692 | return 0; | |
5693 | return 1; | |
62470419 MW |
5694 | } |
5695 | ||
90001d67 | 5696 | /* |
d153b153 PZ |
5697 | * The purpose of wake_affine() is to quickly determine on which CPU we can run |
5698 | * soonest. For the purpose of speed we only consider the waking and previous | |
5699 | * CPU. | |
90001d67 | 5700 | * |
7332dec0 MG |
5701 | * wake_affine_idle() - only considers 'now', it check if the waking CPU is |
5702 | * cache-affine and is (or will be) idle. | |
f2cdd9cc PZ |
5703 | * |
5704 | * wake_affine_weight() - considers the weight to reflect the average | |
5705 | * scheduling latency of the CPUs. This seems to work | |
5706 | * for the overloaded case. | |
90001d67 | 5707 | */ |
3b76c4a3 | 5708 | static int |
89a55f56 | 5709 | wake_affine_idle(int this_cpu, int prev_cpu, int sync) |
90001d67 | 5710 | { |
7332dec0 MG |
5711 | /* |
5712 | * If this_cpu is idle, it implies the wakeup is from interrupt | |
5713 | * context. Only allow the move if cache is shared. Otherwise an | |
5714 | * interrupt intensive workload could force all tasks onto one | |
5715 | * node depending on the IO topology or IRQ affinity settings. | |
806486c3 MG |
5716 | * |
5717 | * If the prev_cpu is idle and cache affine then avoid a migration. | |
5718 | * There is no guarantee that the cache hot data from an interrupt | |
5719 | * is more important than cache hot data on the prev_cpu and from | |
5720 | * a cpufreq perspective, it's better to have higher utilisation | |
5721 | * on one CPU. | |
7332dec0 MG |
5722 | */ |
5723 | if (idle_cpu(this_cpu) && cpus_share_cache(this_cpu, prev_cpu)) | |
806486c3 | 5724 | return idle_cpu(prev_cpu) ? prev_cpu : this_cpu; |
90001d67 | 5725 | |
d153b153 | 5726 | if (sync && cpu_rq(this_cpu)->nr_running == 1) |
3b76c4a3 | 5727 | return this_cpu; |
90001d67 | 5728 | |
3b76c4a3 | 5729 | return nr_cpumask_bits; |
90001d67 PZ |
5730 | } |
5731 | ||
3b76c4a3 | 5732 | static int |
f2cdd9cc PZ |
5733 | wake_affine_weight(struct sched_domain *sd, struct task_struct *p, |
5734 | int this_cpu, int prev_cpu, int sync) | |
90001d67 | 5735 | { |
90001d67 PZ |
5736 | s64 this_eff_load, prev_eff_load; |
5737 | unsigned long task_load; | |
5738 | ||
f2cdd9cc | 5739 | this_eff_load = target_load(this_cpu, sd->wake_idx); |
90001d67 | 5740 | |
90001d67 PZ |
5741 | if (sync) { |
5742 | unsigned long current_load = task_h_load(current); | |
5743 | ||
f2cdd9cc | 5744 | if (current_load > this_eff_load) |
3b76c4a3 | 5745 | return this_cpu; |
90001d67 | 5746 | |
f2cdd9cc | 5747 | this_eff_load -= current_load; |
90001d67 PZ |
5748 | } |
5749 | ||
90001d67 PZ |
5750 | task_load = task_h_load(p); |
5751 | ||
f2cdd9cc PZ |
5752 | this_eff_load += task_load; |
5753 | if (sched_feat(WA_BIAS)) | |
5754 | this_eff_load *= 100; | |
5755 | this_eff_load *= capacity_of(prev_cpu); | |
90001d67 | 5756 | |
eeb60398 | 5757 | prev_eff_load = source_load(prev_cpu, sd->wake_idx); |
f2cdd9cc PZ |
5758 | prev_eff_load -= task_load; |
5759 | if (sched_feat(WA_BIAS)) | |
5760 | prev_eff_load *= 100 + (sd->imbalance_pct - 100) / 2; | |
5761 | prev_eff_load *= capacity_of(this_cpu); | |
90001d67 | 5762 | |
082f764a MG |
5763 | /* |
5764 | * If sync, adjust the weight of prev_eff_load such that if | |
5765 | * prev_eff == this_eff that select_idle_sibling() will consider | |
5766 | * stacking the wakee on top of the waker if no other CPU is | |
5767 | * idle. | |
5768 | */ | |
5769 | if (sync) | |
5770 | prev_eff_load += 1; | |
5771 | ||
5772 | return this_eff_load < prev_eff_load ? this_cpu : nr_cpumask_bits; | |
90001d67 PZ |
5773 | } |
5774 | ||
7347fc87 MG |
5775 | #ifdef CONFIG_NUMA_BALANCING |
5776 | static void | |
5777 | update_wa_numa_placement(struct task_struct *p, int prev_cpu, int target) | |
5778 | { | |
5779 | unsigned long interval; | |
5780 | ||
5781 | if (!static_branch_likely(&sched_numa_balancing)) | |
5782 | return; | |
5783 | ||
5784 | /* If balancing has no preference then continue gathering data */ | |
5785 | if (p->numa_preferred_nid == -1) | |
5786 | return; | |
5787 | ||
5788 | /* | |
5789 | * If the wakeup is not affecting locality then it is neutral from | |
5790 | * the perspective of NUMA balacing so continue gathering data. | |
5791 | */ | |
5792 | if (cpu_to_node(prev_cpu) == cpu_to_node(target)) | |
5793 | return; | |
5794 | ||
5795 | /* | |
5796 | * Temporarily prevent NUMA balancing trying to place waker/wakee after | |
5797 | * wakee has been moved by wake_affine. This will potentially allow | |
5798 | * related tasks to converge and update their data placement. The | |
5799 | * 4 * numa_scan_period is to allow the two-pass filter to migrate | |
5800 | * hot data to the wakers node. | |
5801 | */ | |
5802 | interval = max(sysctl_numa_balancing_scan_delay, | |
5803 | p->numa_scan_period << 2); | |
5804 | p->numa_migrate_retry = jiffies + msecs_to_jiffies(interval); | |
5805 | ||
5806 | interval = max(sysctl_numa_balancing_scan_delay, | |
5807 | current->numa_scan_period << 2); | |
5808 | current->numa_migrate_retry = jiffies + msecs_to_jiffies(interval); | |
5809 | } | |
5810 | #else | |
5811 | static void | |
5812 | update_wa_numa_placement(struct task_struct *p, int prev_cpu, int target) | |
5813 | { | |
5814 | } | |
5815 | #endif | |
5816 | ||
772bd008 | 5817 | static int wake_affine(struct sched_domain *sd, struct task_struct *p, |
7ebb66a1 | 5818 | int this_cpu, int prev_cpu, int sync) |
098fb9db | 5819 | { |
3b76c4a3 | 5820 | int target = nr_cpumask_bits; |
098fb9db | 5821 | |
89a55f56 | 5822 | if (sched_feat(WA_IDLE)) |
3b76c4a3 | 5823 | target = wake_affine_idle(this_cpu, prev_cpu, sync); |
90001d67 | 5824 | |
3b76c4a3 MG |
5825 | if (sched_feat(WA_WEIGHT) && target == nr_cpumask_bits) |
5826 | target = wake_affine_weight(sd, p, this_cpu, prev_cpu, sync); | |
098fb9db | 5827 | |
ae92882e | 5828 | schedstat_inc(p->se.statistics.nr_wakeups_affine_attempts); |
3b76c4a3 MG |
5829 | if (target == nr_cpumask_bits) |
5830 | return prev_cpu; | |
098fb9db | 5831 | |
7347fc87 | 5832 | update_wa_numa_placement(p, prev_cpu, target); |
3b76c4a3 MG |
5833 | schedstat_inc(sd->ttwu_move_affine); |
5834 | schedstat_inc(p->se.statistics.nr_wakeups_affine); | |
5835 | return target; | |
098fb9db IM |
5836 | } |
5837 | ||
f01415fd PB |
5838 | static inline unsigned long task_util(struct task_struct *p); |
5839 | static unsigned long cpu_util_wake(int cpu, struct task_struct *p); | |
6a0b19c0 MR |
5840 | |
5841 | static unsigned long capacity_spare_wake(int cpu, struct task_struct *p) | |
5842 | { | |
f453ae22 | 5843 | return max_t(long, capacity_of(cpu) - cpu_util_wake(cpu, p), 0); |
6a0b19c0 MR |
5844 | } |
5845 | ||
aaee1203 PZ |
5846 | /* |
5847 | * find_idlest_group finds and returns the least busy CPU group within the | |
5848 | * domain. | |
6fee85cc BJ |
5849 | * |
5850 | * Assumes p is allowed on at least one CPU in sd. | |
aaee1203 PZ |
5851 | */ |
5852 | static struct sched_group * | |
78e7ed53 | 5853 | find_idlest_group(struct sched_domain *sd, struct task_struct *p, |
c44f2a02 | 5854 | int this_cpu, int sd_flag) |
e7693a36 | 5855 | { |
b3bd3de6 | 5856 | struct sched_group *idlest = NULL, *group = sd->groups; |
6a0b19c0 | 5857 | struct sched_group *most_spare_sg = NULL; |
0d10ab95 BJ |
5858 | unsigned long min_runnable_load = ULONG_MAX; |
5859 | unsigned long this_runnable_load = ULONG_MAX; | |
5860 | unsigned long min_avg_load = ULONG_MAX, this_avg_load = ULONG_MAX; | |
6a0b19c0 | 5861 | unsigned long most_spare = 0, this_spare = 0; |
c44f2a02 | 5862 | int load_idx = sd->forkexec_idx; |
6b94780e VG |
5863 | int imbalance_scale = 100 + (sd->imbalance_pct-100)/2; |
5864 | unsigned long imbalance = scale_load_down(NICE_0_LOAD) * | |
5865 | (sd->imbalance_pct-100) / 100; | |
e7693a36 | 5866 | |
c44f2a02 VG |
5867 | if (sd_flag & SD_BALANCE_WAKE) |
5868 | load_idx = sd->wake_idx; | |
5869 | ||
aaee1203 | 5870 | do { |
6b94780e VG |
5871 | unsigned long load, avg_load, runnable_load; |
5872 | unsigned long spare_cap, max_spare_cap; | |
aaee1203 PZ |
5873 | int local_group; |
5874 | int i; | |
e7693a36 | 5875 | |
aaee1203 | 5876 | /* Skip over this group if it has no CPUs allowed */ |
ae4df9d6 | 5877 | if (!cpumask_intersects(sched_group_span(group), |
0c98d344 | 5878 | &p->cpus_allowed)) |
aaee1203 PZ |
5879 | continue; |
5880 | ||
5881 | local_group = cpumask_test_cpu(this_cpu, | |
ae4df9d6 | 5882 | sched_group_span(group)); |
aaee1203 | 5883 | |
6a0b19c0 MR |
5884 | /* |
5885 | * Tally up the load of all CPUs in the group and find | |
5886 | * the group containing the CPU with most spare capacity. | |
5887 | */ | |
aaee1203 | 5888 | avg_load = 0; |
6b94780e | 5889 | runnable_load = 0; |
6a0b19c0 | 5890 | max_spare_cap = 0; |
aaee1203 | 5891 | |
ae4df9d6 | 5892 | for_each_cpu(i, sched_group_span(group)) { |
97fb7a0a | 5893 | /* Bias balancing toward CPUs of our domain */ |
aaee1203 PZ |
5894 | if (local_group) |
5895 | load = source_load(i, load_idx); | |
5896 | else | |
5897 | load = target_load(i, load_idx); | |
5898 | ||
6b94780e VG |
5899 | runnable_load += load; |
5900 | ||
5901 | avg_load += cfs_rq_load_avg(&cpu_rq(i)->cfs); | |
6a0b19c0 MR |
5902 | |
5903 | spare_cap = capacity_spare_wake(i, p); | |
5904 | ||
5905 | if (spare_cap > max_spare_cap) | |
5906 | max_spare_cap = spare_cap; | |
aaee1203 PZ |
5907 | } |
5908 | ||
63b2ca30 | 5909 | /* Adjust by relative CPU capacity of the group */ |
6b94780e VG |
5910 | avg_load = (avg_load * SCHED_CAPACITY_SCALE) / |
5911 | group->sgc->capacity; | |
5912 | runnable_load = (runnable_load * SCHED_CAPACITY_SCALE) / | |
5913 | group->sgc->capacity; | |
aaee1203 PZ |
5914 | |
5915 | if (local_group) { | |
6b94780e VG |
5916 | this_runnable_load = runnable_load; |
5917 | this_avg_load = avg_load; | |
6a0b19c0 MR |
5918 | this_spare = max_spare_cap; |
5919 | } else { | |
6b94780e VG |
5920 | if (min_runnable_load > (runnable_load + imbalance)) { |
5921 | /* | |
5922 | * The runnable load is significantly smaller | |
97fb7a0a | 5923 | * so we can pick this new CPU: |
6b94780e VG |
5924 | */ |
5925 | min_runnable_load = runnable_load; | |
5926 | min_avg_load = avg_load; | |
5927 | idlest = group; | |
5928 | } else if ((runnable_load < (min_runnable_load + imbalance)) && | |
5929 | (100*min_avg_load > imbalance_scale*avg_load)) { | |
5930 | /* | |
5931 | * The runnable loads are close so take the | |
97fb7a0a | 5932 | * blocked load into account through avg_load: |
6b94780e VG |
5933 | */ |
5934 | min_avg_load = avg_load; | |
6a0b19c0 MR |
5935 | idlest = group; |
5936 | } | |
5937 | ||
5938 | if (most_spare < max_spare_cap) { | |
5939 | most_spare = max_spare_cap; | |
5940 | most_spare_sg = group; | |
5941 | } | |
aaee1203 PZ |
5942 | } |
5943 | } while (group = group->next, group != sd->groups); | |
5944 | ||
6a0b19c0 MR |
5945 | /* |
5946 | * The cross-over point between using spare capacity or least load | |
5947 | * is too conservative for high utilization tasks on partially | |
5948 | * utilized systems if we require spare_capacity > task_util(p), | |
5949 | * so we allow for some task stuffing by using | |
5950 | * spare_capacity > task_util(p)/2. | |
f519a3f1 VG |
5951 | * |
5952 | * Spare capacity can't be used for fork because the utilization has | |
5953 | * not been set yet, we must first select a rq to compute the initial | |
5954 | * utilization. | |
6a0b19c0 | 5955 | */ |
f519a3f1 VG |
5956 | if (sd_flag & SD_BALANCE_FORK) |
5957 | goto skip_spare; | |
5958 | ||
6a0b19c0 | 5959 | if (this_spare > task_util(p) / 2 && |
6b94780e | 5960 | imbalance_scale*this_spare > 100*most_spare) |
6a0b19c0 | 5961 | return NULL; |
6b94780e VG |
5962 | |
5963 | if (most_spare > task_util(p) / 2) | |
6a0b19c0 MR |
5964 | return most_spare_sg; |
5965 | ||
f519a3f1 | 5966 | skip_spare: |
6b94780e VG |
5967 | if (!idlest) |
5968 | return NULL; | |
5969 | ||
2c833627 MG |
5970 | /* |
5971 | * When comparing groups across NUMA domains, it's possible for the | |
5972 | * local domain to be very lightly loaded relative to the remote | |
5973 | * domains but "imbalance" skews the comparison making remote CPUs | |
5974 | * look much more favourable. When considering cross-domain, add | |
5975 | * imbalance to the runnable load on the remote node and consider | |
5976 | * staying local. | |
5977 | */ | |
5978 | if ((sd->flags & SD_NUMA) && | |
5979 | min_runnable_load + imbalance >= this_runnable_load) | |
5980 | return NULL; | |
5981 | ||
6b94780e | 5982 | if (min_runnable_load > (this_runnable_load + imbalance)) |
aaee1203 | 5983 | return NULL; |
6b94780e VG |
5984 | |
5985 | if ((this_runnable_load < (min_runnable_load + imbalance)) && | |
5986 | (100*this_avg_load < imbalance_scale*min_avg_load)) | |
5987 | return NULL; | |
5988 | ||
aaee1203 PZ |
5989 | return idlest; |
5990 | } | |
5991 | ||
5992 | /* | |
97fb7a0a | 5993 | * find_idlest_group_cpu - find the idlest CPU among the CPUs in the group. |
aaee1203 PZ |
5994 | */ |
5995 | static int | |
18bd1b4b | 5996 | find_idlest_group_cpu(struct sched_group *group, struct task_struct *p, int this_cpu) |
aaee1203 PZ |
5997 | { |
5998 | unsigned long load, min_load = ULONG_MAX; | |
83a0a96a NP |
5999 | unsigned int min_exit_latency = UINT_MAX; |
6000 | u64 latest_idle_timestamp = 0; | |
6001 | int least_loaded_cpu = this_cpu; | |
6002 | int shallowest_idle_cpu = -1; | |
aaee1203 PZ |
6003 | int i; |
6004 | ||
eaecf41f MR |
6005 | /* Check if we have any choice: */ |
6006 | if (group->group_weight == 1) | |
ae4df9d6 | 6007 | return cpumask_first(sched_group_span(group)); |
eaecf41f | 6008 | |
aaee1203 | 6009 | /* Traverse only the allowed CPUs */ |
ae4df9d6 | 6010 | for_each_cpu_and(i, sched_group_span(group), &p->cpus_allowed) { |
83a0a96a NP |
6011 | if (idle_cpu(i)) { |
6012 | struct rq *rq = cpu_rq(i); | |
6013 | struct cpuidle_state *idle = idle_get_state(rq); | |
6014 | if (idle && idle->exit_latency < min_exit_latency) { | |
6015 | /* | |
6016 | * We give priority to a CPU whose idle state | |
6017 | * has the smallest exit latency irrespective | |
6018 | * of any idle timestamp. | |
6019 | */ | |
6020 | min_exit_latency = idle->exit_latency; | |
6021 | latest_idle_timestamp = rq->idle_stamp; | |
6022 | shallowest_idle_cpu = i; | |
6023 | } else if ((!idle || idle->exit_latency == min_exit_latency) && | |
6024 | rq->idle_stamp > latest_idle_timestamp) { | |
6025 | /* | |
6026 | * If equal or no active idle state, then | |
6027 | * the most recently idled CPU might have | |
6028 | * a warmer cache. | |
6029 | */ | |
6030 | latest_idle_timestamp = rq->idle_stamp; | |
6031 | shallowest_idle_cpu = i; | |
6032 | } | |
9f96742a | 6033 | } else if (shallowest_idle_cpu == -1) { |
c7132dd6 | 6034 | load = weighted_cpuload(cpu_rq(i)); |
18cec7e0 | 6035 | if (load < min_load) { |
83a0a96a NP |
6036 | min_load = load; |
6037 | least_loaded_cpu = i; | |
6038 | } | |
e7693a36 GH |
6039 | } |
6040 | } | |
6041 | ||
83a0a96a | 6042 | return shallowest_idle_cpu != -1 ? shallowest_idle_cpu : least_loaded_cpu; |
aaee1203 | 6043 | } |
e7693a36 | 6044 | |
18bd1b4b BJ |
6045 | static inline int find_idlest_cpu(struct sched_domain *sd, struct task_struct *p, |
6046 | int cpu, int prev_cpu, int sd_flag) | |
6047 | { | |
93f50f90 | 6048 | int new_cpu = cpu; |
18bd1b4b | 6049 | |
6fee85cc BJ |
6050 | if (!cpumask_intersects(sched_domain_span(sd), &p->cpus_allowed)) |
6051 | return prev_cpu; | |
6052 | ||
18bd1b4b BJ |
6053 | while (sd) { |
6054 | struct sched_group *group; | |
6055 | struct sched_domain *tmp; | |
6056 | int weight; | |
6057 | ||
6058 | if (!(sd->flags & sd_flag)) { | |
6059 | sd = sd->child; | |
6060 | continue; | |
6061 | } | |
6062 | ||
6063 | group = find_idlest_group(sd, p, cpu, sd_flag); | |
6064 | if (!group) { | |
6065 | sd = sd->child; | |
6066 | continue; | |
6067 | } | |
6068 | ||
6069 | new_cpu = find_idlest_group_cpu(group, p, cpu); | |
e90381ea | 6070 | if (new_cpu == cpu) { |
97fb7a0a | 6071 | /* Now try balancing at a lower domain level of 'cpu': */ |
18bd1b4b BJ |
6072 | sd = sd->child; |
6073 | continue; | |
6074 | } | |
6075 | ||
97fb7a0a | 6076 | /* Now try balancing at a lower domain level of 'new_cpu': */ |
18bd1b4b BJ |
6077 | cpu = new_cpu; |
6078 | weight = sd->span_weight; | |
6079 | sd = NULL; | |
6080 | for_each_domain(cpu, tmp) { | |
6081 | if (weight <= tmp->span_weight) | |
6082 | break; | |
6083 | if (tmp->flags & sd_flag) | |
6084 | sd = tmp; | |
6085 | } | |
18bd1b4b BJ |
6086 | } |
6087 | ||
6088 | return new_cpu; | |
6089 | } | |
6090 | ||
10e2f1ac PZ |
6091 | #ifdef CONFIG_SCHED_SMT |
6092 | ||
6093 | static inline void set_idle_cores(int cpu, int val) | |
6094 | { | |
6095 | struct sched_domain_shared *sds; | |
6096 | ||
6097 | sds = rcu_dereference(per_cpu(sd_llc_shared, cpu)); | |
6098 | if (sds) | |
6099 | WRITE_ONCE(sds->has_idle_cores, val); | |
6100 | } | |
6101 | ||
6102 | static inline bool test_idle_cores(int cpu, bool def) | |
6103 | { | |
6104 | struct sched_domain_shared *sds; | |
6105 | ||
6106 | sds = rcu_dereference(per_cpu(sd_llc_shared, cpu)); | |
6107 | if (sds) | |
6108 | return READ_ONCE(sds->has_idle_cores); | |
6109 | ||
6110 | return def; | |
6111 | } | |
6112 | ||
6113 | /* | |
6114 | * Scans the local SMT mask to see if the entire core is idle, and records this | |
6115 | * information in sd_llc_shared->has_idle_cores. | |
6116 | * | |
6117 | * Since SMT siblings share all cache levels, inspecting this limited remote | |
6118 | * state should be fairly cheap. | |
6119 | */ | |
1b568f0a | 6120 | void __update_idle_core(struct rq *rq) |
10e2f1ac PZ |
6121 | { |
6122 | int core = cpu_of(rq); | |
6123 | int cpu; | |
6124 | ||
6125 | rcu_read_lock(); | |
6126 | if (test_idle_cores(core, true)) | |
6127 | goto unlock; | |
6128 | ||
6129 | for_each_cpu(cpu, cpu_smt_mask(core)) { | |
6130 | if (cpu == core) | |
6131 | continue; | |
6132 | ||
6133 | if (!idle_cpu(cpu)) | |
6134 | goto unlock; | |
6135 | } | |
6136 | ||
6137 | set_idle_cores(core, 1); | |
6138 | unlock: | |
6139 | rcu_read_unlock(); | |
6140 | } | |
6141 | ||
6142 | /* | |
6143 | * Scan the entire LLC domain for idle cores; this dynamically switches off if | |
6144 | * there are no idle cores left in the system; tracked through | |
6145 | * sd_llc->shared->has_idle_cores and enabled through update_idle_core() above. | |
6146 | */ | |
6147 | static int select_idle_core(struct task_struct *p, struct sched_domain *sd, int target) | |
6148 | { | |
6149 | struct cpumask *cpus = this_cpu_cpumask_var_ptr(select_idle_mask); | |
c743f0a5 | 6150 | int core, cpu; |
10e2f1ac | 6151 | |
1b568f0a PZ |
6152 | if (!static_branch_likely(&sched_smt_present)) |
6153 | return -1; | |
6154 | ||
10e2f1ac PZ |
6155 | if (!test_idle_cores(target, false)) |
6156 | return -1; | |
6157 | ||
0c98d344 | 6158 | cpumask_and(cpus, sched_domain_span(sd), &p->cpus_allowed); |
10e2f1ac | 6159 | |
c743f0a5 | 6160 | for_each_cpu_wrap(core, cpus, target) { |
10e2f1ac PZ |
6161 | bool idle = true; |
6162 | ||
6163 | for_each_cpu(cpu, cpu_smt_mask(core)) { | |
6164 | cpumask_clear_cpu(cpu, cpus); | |
6165 | if (!idle_cpu(cpu)) | |
6166 | idle = false; | |
6167 | } | |
6168 | ||
6169 | if (idle) | |
6170 | return core; | |
6171 | } | |
6172 | ||
6173 | /* | |
6174 | * Failed to find an idle core; stop looking for one. | |
6175 | */ | |
6176 | set_idle_cores(target, 0); | |
6177 | ||
6178 | return -1; | |
6179 | } | |
6180 | ||
6181 | /* | |
6182 | * Scan the local SMT mask for idle CPUs. | |
6183 | */ | |
6184 | static int select_idle_smt(struct task_struct *p, struct sched_domain *sd, int target) | |
6185 | { | |
6186 | int cpu; | |
6187 | ||
1b568f0a PZ |
6188 | if (!static_branch_likely(&sched_smt_present)) |
6189 | return -1; | |
6190 | ||
10e2f1ac | 6191 | for_each_cpu(cpu, cpu_smt_mask(target)) { |
0c98d344 | 6192 | if (!cpumask_test_cpu(cpu, &p->cpus_allowed)) |
10e2f1ac PZ |
6193 | continue; |
6194 | if (idle_cpu(cpu)) | |
6195 | return cpu; | |
6196 | } | |
6197 | ||
6198 | return -1; | |
6199 | } | |
6200 | ||
6201 | #else /* CONFIG_SCHED_SMT */ | |
6202 | ||
6203 | static inline int select_idle_core(struct task_struct *p, struct sched_domain *sd, int target) | |
6204 | { | |
6205 | return -1; | |
6206 | } | |
6207 | ||
6208 | static inline int select_idle_smt(struct task_struct *p, struct sched_domain *sd, int target) | |
6209 | { | |
6210 | return -1; | |
6211 | } | |
6212 | ||
6213 | #endif /* CONFIG_SCHED_SMT */ | |
6214 | ||
6215 | /* | |
6216 | * Scan the LLC domain for idle CPUs; this is dynamically regulated by | |
6217 | * comparing the average scan cost (tracked in sd->avg_scan_cost) against the | |
6218 | * average idle time for this rq (as found in rq->avg_idle). | |
a50bde51 | 6219 | */ |
10e2f1ac PZ |
6220 | static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, int target) |
6221 | { | |
9cfb38a7 | 6222 | struct sched_domain *this_sd; |
1ad3aaf3 | 6223 | u64 avg_cost, avg_idle; |
10e2f1ac PZ |
6224 | u64 time, cost; |
6225 | s64 delta; | |
1ad3aaf3 | 6226 | int cpu, nr = INT_MAX; |
10e2f1ac | 6227 | |
9cfb38a7 WL |
6228 | this_sd = rcu_dereference(*this_cpu_ptr(&sd_llc)); |
6229 | if (!this_sd) | |
6230 | return -1; | |
6231 | ||
10e2f1ac PZ |
6232 | /* |
6233 | * Due to large variance we need a large fuzz factor; hackbench in | |
6234 | * particularly is sensitive here. | |
6235 | */ | |
1ad3aaf3 PZ |
6236 | avg_idle = this_rq()->avg_idle / 512; |
6237 | avg_cost = this_sd->avg_scan_cost + 1; | |
6238 | ||
6239 | if (sched_feat(SIS_AVG_CPU) && avg_idle < avg_cost) | |
10e2f1ac PZ |
6240 | return -1; |
6241 | ||
1ad3aaf3 PZ |
6242 | if (sched_feat(SIS_PROP)) { |
6243 | u64 span_avg = sd->span_weight * avg_idle; | |
6244 | if (span_avg > 4*avg_cost) | |
6245 | nr = div_u64(span_avg, avg_cost); | |
6246 | else | |
6247 | nr = 4; | |
6248 | } | |
6249 | ||
10e2f1ac PZ |
6250 | time = local_clock(); |
6251 | ||
c743f0a5 | 6252 | for_each_cpu_wrap(cpu, sched_domain_span(sd), target) { |
1ad3aaf3 PZ |
6253 | if (!--nr) |
6254 | return -1; | |
0c98d344 | 6255 | if (!cpumask_test_cpu(cpu, &p->cpus_allowed)) |
10e2f1ac PZ |
6256 | continue; |
6257 | if (idle_cpu(cpu)) | |
6258 | break; | |
6259 | } | |
6260 | ||
6261 | time = local_clock() - time; | |
6262 | cost = this_sd->avg_scan_cost; | |
6263 | delta = (s64)(time - cost) / 8; | |
6264 | this_sd->avg_scan_cost += delta; | |
6265 | ||
6266 | return cpu; | |
6267 | } | |
6268 | ||
6269 | /* | |
6270 | * Try and locate an idle core/thread in the LLC cache domain. | |
a50bde51 | 6271 | */ |
772bd008 | 6272 | static int select_idle_sibling(struct task_struct *p, int prev, int target) |
a50bde51 | 6273 | { |
99bd5e2f | 6274 | struct sched_domain *sd; |
32e839dd | 6275 | int i, recent_used_cpu; |
a50bde51 | 6276 | |
e0a79f52 MG |
6277 | if (idle_cpu(target)) |
6278 | return target; | |
99bd5e2f SS |
6279 | |
6280 | /* | |
97fb7a0a | 6281 | * If the previous CPU is cache affine and idle, don't be stupid: |
99bd5e2f | 6282 | */ |
772bd008 MR |
6283 | if (prev != target && cpus_share_cache(prev, target) && idle_cpu(prev)) |
6284 | return prev; | |
a50bde51 | 6285 | |
97fb7a0a | 6286 | /* Check a recently used CPU as a potential idle candidate: */ |
32e839dd MG |
6287 | recent_used_cpu = p->recent_used_cpu; |
6288 | if (recent_used_cpu != prev && | |
6289 | recent_used_cpu != target && | |
6290 | cpus_share_cache(recent_used_cpu, target) && | |
6291 | idle_cpu(recent_used_cpu) && | |
6292 | cpumask_test_cpu(p->recent_used_cpu, &p->cpus_allowed)) { | |
6293 | /* | |
6294 | * Replace recent_used_cpu with prev as it is a potential | |
97fb7a0a | 6295 | * candidate for the next wake: |
32e839dd MG |
6296 | */ |
6297 | p->recent_used_cpu = prev; | |
6298 | return recent_used_cpu; | |
6299 | } | |
6300 | ||
518cd623 | 6301 | sd = rcu_dereference(per_cpu(sd_llc, target)); |
10e2f1ac PZ |
6302 | if (!sd) |
6303 | return target; | |
772bd008 | 6304 | |
10e2f1ac PZ |
6305 | i = select_idle_core(p, sd, target); |
6306 | if ((unsigned)i < nr_cpumask_bits) | |
6307 | return i; | |
37407ea7 | 6308 | |
10e2f1ac PZ |
6309 | i = select_idle_cpu(p, sd, target); |
6310 | if ((unsigned)i < nr_cpumask_bits) | |
6311 | return i; | |
6312 | ||
6313 | i = select_idle_smt(p, sd, target); | |
6314 | if ((unsigned)i < nr_cpumask_bits) | |
6315 | return i; | |
970e1789 | 6316 | |
a50bde51 PZ |
6317 | return target; |
6318 | } | |
231678b7 | 6319 | |
8bb5b00c | 6320 | /* |
9e91d61d | 6321 | * cpu_util returns the amount of capacity of a CPU that is used by CFS |
8bb5b00c | 6322 | * tasks. The unit of the return value must be the one of capacity so we can |
9e91d61d DE |
6323 | * compare the utilization with the capacity of the CPU that is available for |
6324 | * CFS task (ie cpu_capacity). | |
231678b7 DE |
6325 | * |
6326 | * cfs_rq.avg.util_avg is the sum of running time of runnable tasks plus the | |
6327 | * recent utilization of currently non-runnable tasks on a CPU. It represents | |
6328 | * the amount of utilization of a CPU in the range [0..capacity_orig] where | |
6329 | * capacity_orig is the cpu_capacity available at the highest frequency | |
6330 | * (arch_scale_freq_capacity()). | |
6331 | * The utilization of a CPU converges towards a sum equal to or less than the | |
6332 | * current capacity (capacity_curr <= capacity_orig) of the CPU because it is | |
6333 | * the running time on this CPU scaled by capacity_curr. | |
6334 | * | |
6335 | * Nevertheless, cfs_rq.avg.util_avg can be higher than capacity_curr or even | |
6336 | * higher than capacity_orig because of unfortunate rounding in | |
6337 | * cfs.avg.util_avg or just after migrating tasks and new task wakeups until | |
6338 | * the average stabilizes with the new running time. We need to check that the | |
6339 | * utilization stays within the range of [0..capacity_orig] and cap it if | |
6340 | * necessary. Without utilization capping, a group could be seen as overloaded | |
6341 | * (CPU0 utilization at 121% + CPU1 utilization at 80%) whereas CPU1 has 20% of | |
6342 | * available capacity. We allow utilization to overshoot capacity_curr (but not | |
6343 | * capacity_orig) as it useful for predicting the capacity required after task | |
6344 | * migrations (scheduler-driven DVFS). | |
8bb5b00c | 6345 | */ |
f01415fd | 6346 | static unsigned long cpu_util(int cpu) |
8bb5b00c | 6347 | { |
9e91d61d | 6348 | unsigned long util = cpu_rq(cpu)->cfs.avg.util_avg; |
8bb5b00c VG |
6349 | unsigned long capacity = capacity_orig_of(cpu); |
6350 | ||
231678b7 | 6351 | return (util >= capacity) ? capacity : util; |
8bb5b00c | 6352 | } |
a50bde51 | 6353 | |
f01415fd | 6354 | static inline unsigned long task_util(struct task_struct *p) |
3273163c MR |
6355 | { |
6356 | return p->se.avg.util_avg; | |
6357 | } | |
6358 | ||
104cb16d | 6359 | /* |
97fb7a0a | 6360 | * cpu_util_wake: Compute CPU utilization with any contributions from |
104cb16d MR |
6361 | * the waking task p removed. |
6362 | */ | |
f01415fd | 6363 | static unsigned long cpu_util_wake(int cpu, struct task_struct *p) |
104cb16d MR |
6364 | { |
6365 | unsigned long util, capacity; | |
6366 | ||
6367 | /* Task has no contribution or is new */ | |
6368 | if (cpu != task_cpu(p) || !p->se.avg.last_update_time) | |
6369 | return cpu_util(cpu); | |
6370 | ||
6371 | capacity = capacity_orig_of(cpu); | |
6372 | util = max_t(long, cpu_rq(cpu)->cfs.avg.util_avg - task_util(p), 0); | |
6373 | ||
6374 | return (util >= capacity) ? capacity : util; | |
6375 | } | |
6376 | ||
3273163c MR |
6377 | /* |
6378 | * Disable WAKE_AFFINE in the case where task @p doesn't fit in the | |
6379 | * capacity of either the waking CPU @cpu or the previous CPU @prev_cpu. | |
6380 | * | |
6381 | * In that case WAKE_AFFINE doesn't make sense and we'll let | |
6382 | * BALANCE_WAKE sort things out. | |
6383 | */ | |
6384 | static int wake_cap(struct task_struct *p, int cpu, int prev_cpu) | |
6385 | { | |
6386 | long min_cap, max_cap; | |
6387 | ||
6388 | min_cap = min(capacity_orig_of(prev_cpu), capacity_orig_of(cpu)); | |
6389 | max_cap = cpu_rq(cpu)->rd->max_cpu_capacity; | |
6390 | ||
6391 | /* Minimum capacity is close to max, no need to abort wake_affine */ | |
6392 | if (max_cap - min_cap < max_cap >> 3) | |
6393 | return 0; | |
6394 | ||
104cb16d MR |
6395 | /* Bring task utilization in sync with prev_cpu */ |
6396 | sync_entity_load_avg(&p->se); | |
6397 | ||
3273163c MR |
6398 | return min_cap * 1024 < task_util(p) * capacity_margin; |
6399 | } | |
6400 | ||
aaee1203 | 6401 | /* |
de91b9cb MR |
6402 | * select_task_rq_fair: Select target runqueue for the waking task in domains |
6403 | * that have the 'sd_flag' flag set. In practice, this is SD_BALANCE_WAKE, | |
6404 | * SD_BALANCE_FORK, or SD_BALANCE_EXEC. | |
aaee1203 | 6405 | * |
97fb7a0a IM |
6406 | * Balances load by selecting the idlest CPU in the idlest group, or under |
6407 | * certain conditions an idle sibling CPU if the domain has SD_WAKE_AFFINE set. | |
aaee1203 | 6408 | * |
97fb7a0a | 6409 | * Returns the target CPU number. |
aaee1203 PZ |
6410 | * |
6411 | * preempt must be disabled. | |
6412 | */ | |
0017d735 | 6413 | static int |
ac66f547 | 6414 | select_task_rq_fair(struct task_struct *p, int prev_cpu, int sd_flag, int wake_flags) |
aaee1203 | 6415 | { |
29cd8bae | 6416 | struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL; |
c88d5910 | 6417 | int cpu = smp_processor_id(); |
63b0e9ed | 6418 | int new_cpu = prev_cpu; |
99bd5e2f | 6419 | int want_affine = 0; |
24d0c1d6 | 6420 | int sync = (wake_flags & WF_SYNC) && !(current->flags & PF_EXITING); |
c88d5910 | 6421 | |
c58d25f3 PZ |
6422 | if (sd_flag & SD_BALANCE_WAKE) { |
6423 | record_wakee(p); | |
3273163c | 6424 | want_affine = !wake_wide(p) && !wake_cap(p, cpu, prev_cpu) |
0c98d344 | 6425 | && cpumask_test_cpu(cpu, &p->cpus_allowed); |
c58d25f3 | 6426 | } |
aaee1203 | 6427 | |
dce840a0 | 6428 | rcu_read_lock(); |
aaee1203 | 6429 | for_each_domain(cpu, tmp) { |
e4f42888 | 6430 | if (!(tmp->flags & SD_LOAD_BALANCE)) |
63b0e9ed | 6431 | break; |
e4f42888 | 6432 | |
fe3bcfe1 | 6433 | /* |
97fb7a0a | 6434 | * If both 'cpu' and 'prev_cpu' are part of this domain, |
99bd5e2f | 6435 | * cpu is a valid SD_WAKE_AFFINE target. |
fe3bcfe1 | 6436 | */ |
99bd5e2f SS |
6437 | if (want_affine && (tmp->flags & SD_WAKE_AFFINE) && |
6438 | cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) { | |
6439 | affine_sd = tmp; | |
29cd8bae | 6440 | break; |
f03542a7 | 6441 | } |
29cd8bae | 6442 | |
f03542a7 | 6443 | if (tmp->flags & sd_flag) |
29cd8bae | 6444 | sd = tmp; |
63b0e9ed MG |
6445 | else if (!want_affine) |
6446 | break; | |
29cd8bae PZ |
6447 | } |
6448 | ||
63b0e9ed MG |
6449 | if (affine_sd) { |
6450 | sd = NULL; /* Prefer wake_affine over balance flags */ | |
7d894e6e RR |
6451 | if (cpu == prev_cpu) |
6452 | goto pick_cpu; | |
6453 | ||
7ebb66a1 | 6454 | new_cpu = wake_affine(affine_sd, p, cpu, prev_cpu, sync); |
8b911acd | 6455 | } |
e7693a36 | 6456 | |
ea16f0ea BJ |
6457 | if (sd && !(sd_flag & SD_BALANCE_FORK)) { |
6458 | /* | |
6459 | * We're going to need the task's util for capacity_spare_wake | |
6460 | * in find_idlest_group. Sync it up to prev_cpu's | |
6461 | * last_update_time. | |
6462 | */ | |
6463 | sync_entity_load_avg(&p->se); | |
6464 | } | |
6465 | ||
63b0e9ed | 6466 | if (!sd) { |
ea16f0ea | 6467 | pick_cpu: |
32e839dd | 6468 | if (sd_flag & SD_BALANCE_WAKE) { /* XXX always ? */ |
772bd008 | 6469 | new_cpu = select_idle_sibling(p, prev_cpu, new_cpu); |
63b0e9ed | 6470 | |
32e839dd MG |
6471 | if (want_affine) |
6472 | current->recent_used_cpu = cpu; | |
6473 | } | |
18bd1b4b BJ |
6474 | } else { |
6475 | new_cpu = find_idlest_cpu(sd, p, cpu, prev_cpu, sd_flag); | |
e7693a36 | 6476 | } |
dce840a0 | 6477 | rcu_read_unlock(); |
e7693a36 | 6478 | |
c88d5910 | 6479 | return new_cpu; |
e7693a36 | 6480 | } |
0a74bef8 | 6481 | |
144d8487 PZ |
6482 | static void detach_entity_cfs_rq(struct sched_entity *se); |
6483 | ||
0a74bef8 | 6484 | /* |
97fb7a0a | 6485 | * Called immediately before a task is migrated to a new CPU; task_cpu(p) and |
0a74bef8 | 6486 | * cfs_rq_of(p) references at time of call are still valid and identify the |
97fb7a0a | 6487 | * previous CPU. The caller guarantees p->pi_lock or task_rq(p)->lock is held. |
0a74bef8 | 6488 | */ |
5a4fd036 | 6489 | static void migrate_task_rq_fair(struct task_struct *p) |
0a74bef8 | 6490 | { |
59efa0ba PZ |
6491 | /* |
6492 | * As blocked tasks retain absolute vruntime the migration needs to | |
6493 | * deal with this by subtracting the old and adding the new | |
6494 | * min_vruntime -- the latter is done by enqueue_entity() when placing | |
6495 | * the task on the new runqueue. | |
6496 | */ | |
6497 | if (p->state == TASK_WAKING) { | |
6498 | struct sched_entity *se = &p->se; | |
6499 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
6500 | u64 min_vruntime; | |
6501 | ||
6502 | #ifndef CONFIG_64BIT | |
6503 | u64 min_vruntime_copy; | |
6504 | ||
6505 | do { | |
6506 | min_vruntime_copy = cfs_rq->min_vruntime_copy; | |
6507 | smp_rmb(); | |
6508 | min_vruntime = cfs_rq->min_vruntime; | |
6509 | } while (min_vruntime != min_vruntime_copy); | |
6510 | #else | |
6511 | min_vruntime = cfs_rq->min_vruntime; | |
6512 | #endif | |
6513 | ||
6514 | se->vruntime -= min_vruntime; | |
6515 | } | |
6516 | ||
144d8487 PZ |
6517 | if (p->on_rq == TASK_ON_RQ_MIGRATING) { |
6518 | /* | |
6519 | * In case of TASK_ON_RQ_MIGRATING we in fact hold the 'old' | |
6520 | * rq->lock and can modify state directly. | |
6521 | */ | |
6522 | lockdep_assert_held(&task_rq(p)->lock); | |
6523 | detach_entity_cfs_rq(&p->se); | |
6524 | ||
6525 | } else { | |
6526 | /* | |
6527 | * We are supposed to update the task to "current" time, then | |
6528 | * its up to date and ready to go to new CPU/cfs_rq. But we | |
6529 | * have difficulty in getting what current time is, so simply | |
6530 | * throw away the out-of-date time. This will result in the | |
6531 | * wakee task is less decayed, but giving the wakee more load | |
6532 | * sounds not bad. | |
6533 | */ | |
6534 | remove_entity_load_avg(&p->se); | |
6535 | } | |
9d89c257 YD |
6536 | |
6537 | /* Tell new CPU we are migrated */ | |
6538 | p->se.avg.last_update_time = 0; | |
3944a927 BS |
6539 | |
6540 | /* We have migrated, no longer consider this task hot */ | |
9d89c257 | 6541 | p->se.exec_start = 0; |
0a74bef8 | 6542 | } |
12695578 YD |
6543 | |
6544 | static void task_dead_fair(struct task_struct *p) | |
6545 | { | |
6546 | remove_entity_load_avg(&p->se); | |
6547 | } | |
e7693a36 GH |
6548 | #endif /* CONFIG_SMP */ |
6549 | ||
a555e9d8 | 6550 | static unsigned long wakeup_gran(struct sched_entity *se) |
0bbd3336 PZ |
6551 | { |
6552 | unsigned long gran = sysctl_sched_wakeup_granularity; | |
6553 | ||
6554 | /* | |
e52fb7c0 PZ |
6555 | * Since its curr running now, convert the gran from real-time |
6556 | * to virtual-time in his units. | |
13814d42 MG |
6557 | * |
6558 | * By using 'se' instead of 'curr' we penalize light tasks, so | |
6559 | * they get preempted easier. That is, if 'se' < 'curr' then | |
6560 | * the resulting gran will be larger, therefore penalizing the | |
6561 | * lighter, if otoh 'se' > 'curr' then the resulting gran will | |
6562 | * be smaller, again penalizing the lighter task. | |
6563 | * | |
6564 | * This is especially important for buddies when the leftmost | |
6565 | * task is higher priority than the buddy. | |
0bbd3336 | 6566 | */ |
f4ad9bd2 | 6567 | return calc_delta_fair(gran, se); |
0bbd3336 PZ |
6568 | } |
6569 | ||
464b7527 PZ |
6570 | /* |
6571 | * Should 'se' preempt 'curr'. | |
6572 | * | |
6573 | * |s1 | |
6574 | * |s2 | |
6575 | * |s3 | |
6576 | * g | |
6577 | * |<--->|c | |
6578 | * | |
6579 | * w(c, s1) = -1 | |
6580 | * w(c, s2) = 0 | |
6581 | * w(c, s3) = 1 | |
6582 | * | |
6583 | */ | |
6584 | static int | |
6585 | wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se) | |
6586 | { | |
6587 | s64 gran, vdiff = curr->vruntime - se->vruntime; | |
6588 | ||
6589 | if (vdiff <= 0) | |
6590 | return -1; | |
6591 | ||
a555e9d8 | 6592 | gran = wakeup_gran(se); |
464b7527 PZ |
6593 | if (vdiff > gran) |
6594 | return 1; | |
6595 | ||
6596 | return 0; | |
6597 | } | |
6598 | ||
02479099 PZ |
6599 | static void set_last_buddy(struct sched_entity *se) |
6600 | { | |
69c80f3e VP |
6601 | if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE)) |
6602 | return; | |
6603 | ||
c5ae366e DA |
6604 | for_each_sched_entity(se) { |
6605 | if (SCHED_WARN_ON(!se->on_rq)) | |
6606 | return; | |
69c80f3e | 6607 | cfs_rq_of(se)->last = se; |
c5ae366e | 6608 | } |
02479099 PZ |
6609 | } |
6610 | ||
6611 | static void set_next_buddy(struct sched_entity *se) | |
6612 | { | |
69c80f3e VP |
6613 | if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE)) |
6614 | return; | |
6615 | ||
c5ae366e DA |
6616 | for_each_sched_entity(se) { |
6617 | if (SCHED_WARN_ON(!se->on_rq)) | |
6618 | return; | |
69c80f3e | 6619 | cfs_rq_of(se)->next = se; |
c5ae366e | 6620 | } |
02479099 PZ |
6621 | } |
6622 | ||
ac53db59 RR |
6623 | static void set_skip_buddy(struct sched_entity *se) |
6624 | { | |
69c80f3e VP |
6625 | for_each_sched_entity(se) |
6626 | cfs_rq_of(se)->skip = se; | |
ac53db59 RR |
6627 | } |
6628 | ||
bf0f6f24 IM |
6629 | /* |
6630 | * Preempt the current task with a newly woken task if needed: | |
6631 | */ | |
5a9b86f6 | 6632 | static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags) |
bf0f6f24 IM |
6633 | { |
6634 | struct task_struct *curr = rq->curr; | |
8651a86c | 6635 | struct sched_entity *se = &curr->se, *pse = &p->se; |
03e89e45 | 6636 | struct cfs_rq *cfs_rq = task_cfs_rq(curr); |
f685ceac | 6637 | int scale = cfs_rq->nr_running >= sched_nr_latency; |
2f36825b | 6638 | int next_buddy_marked = 0; |
bf0f6f24 | 6639 | |
4ae7d5ce IM |
6640 | if (unlikely(se == pse)) |
6641 | return; | |
6642 | ||
5238cdd3 | 6643 | /* |
163122b7 | 6644 | * This is possible from callers such as attach_tasks(), in which we |
5238cdd3 PT |
6645 | * unconditionally check_prempt_curr() after an enqueue (which may have |
6646 | * lead to a throttle). This both saves work and prevents false | |
6647 | * next-buddy nomination below. | |
6648 | */ | |
6649 | if (unlikely(throttled_hierarchy(cfs_rq_of(pse)))) | |
6650 | return; | |
6651 | ||
2f36825b | 6652 | if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) { |
3cb63d52 | 6653 | set_next_buddy(pse); |
2f36825b VP |
6654 | next_buddy_marked = 1; |
6655 | } | |
57fdc26d | 6656 | |
aec0a514 BR |
6657 | /* |
6658 | * We can come here with TIF_NEED_RESCHED already set from new task | |
6659 | * wake up path. | |
5238cdd3 PT |
6660 | * |
6661 | * Note: this also catches the edge-case of curr being in a throttled | |
6662 | * group (e.g. via set_curr_task), since update_curr() (in the | |
6663 | * enqueue of curr) will have resulted in resched being set. This | |
6664 | * prevents us from potentially nominating it as a false LAST_BUDDY | |
6665 | * below. | |
aec0a514 BR |
6666 | */ |
6667 | if (test_tsk_need_resched(curr)) | |
6668 | return; | |
6669 | ||
a2f5c9ab DH |
6670 | /* Idle tasks are by definition preempted by non-idle tasks. */ |
6671 | if (unlikely(curr->policy == SCHED_IDLE) && | |
6672 | likely(p->policy != SCHED_IDLE)) | |
6673 | goto preempt; | |
6674 | ||
91c234b4 | 6675 | /* |
a2f5c9ab DH |
6676 | * Batch and idle tasks do not preempt non-idle tasks (their preemption |
6677 | * is driven by the tick): | |
91c234b4 | 6678 | */ |
8ed92e51 | 6679 | if (unlikely(p->policy != SCHED_NORMAL) || !sched_feat(WAKEUP_PREEMPTION)) |
91c234b4 | 6680 | return; |
bf0f6f24 | 6681 | |
464b7527 | 6682 | find_matching_se(&se, &pse); |
9bbd7374 | 6683 | update_curr(cfs_rq_of(se)); |
002f128b | 6684 | BUG_ON(!pse); |
2f36825b VP |
6685 | if (wakeup_preempt_entity(se, pse) == 1) { |
6686 | /* | |
6687 | * Bias pick_next to pick the sched entity that is | |
6688 | * triggering this preemption. | |
6689 | */ | |
6690 | if (!next_buddy_marked) | |
6691 | set_next_buddy(pse); | |
3a7e73a2 | 6692 | goto preempt; |
2f36825b | 6693 | } |
464b7527 | 6694 | |
3a7e73a2 | 6695 | return; |
a65ac745 | 6696 | |
3a7e73a2 | 6697 | preempt: |
8875125e | 6698 | resched_curr(rq); |
3a7e73a2 PZ |
6699 | /* |
6700 | * Only set the backward buddy when the current task is still | |
6701 | * on the rq. This can happen when a wakeup gets interleaved | |
6702 | * with schedule on the ->pre_schedule() or idle_balance() | |
6703 | * point, either of which can * drop the rq lock. | |
6704 | * | |
6705 | * Also, during early boot the idle thread is in the fair class, | |
6706 | * for obvious reasons its a bad idea to schedule back to it. | |
6707 | */ | |
6708 | if (unlikely(!se->on_rq || curr == rq->idle)) | |
6709 | return; | |
6710 | ||
6711 | if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se)) | |
6712 | set_last_buddy(se); | |
bf0f6f24 IM |
6713 | } |
6714 | ||
606dba2e | 6715 | static struct task_struct * |
d8ac8971 | 6716 | pick_next_task_fair(struct rq *rq, struct task_struct *prev, struct rq_flags *rf) |
bf0f6f24 IM |
6717 | { |
6718 | struct cfs_rq *cfs_rq = &rq->cfs; | |
6719 | struct sched_entity *se; | |
678d5718 | 6720 | struct task_struct *p; |
37e117c0 | 6721 | int new_tasks; |
678d5718 | 6722 | |
6e83125c | 6723 | again: |
678d5718 | 6724 | if (!cfs_rq->nr_running) |
38033c37 | 6725 | goto idle; |
678d5718 | 6726 | |
9674f5ca | 6727 | #ifdef CONFIG_FAIR_GROUP_SCHED |
3f1d2a31 | 6728 | if (prev->sched_class != &fair_sched_class) |
678d5718 PZ |
6729 | goto simple; |
6730 | ||
6731 | /* | |
6732 | * Because of the set_next_buddy() in dequeue_task_fair() it is rather | |
6733 | * likely that a next task is from the same cgroup as the current. | |
6734 | * | |
6735 | * Therefore attempt to avoid putting and setting the entire cgroup | |
6736 | * hierarchy, only change the part that actually changes. | |
6737 | */ | |
6738 | ||
6739 | do { | |
6740 | struct sched_entity *curr = cfs_rq->curr; | |
6741 | ||
6742 | /* | |
6743 | * Since we got here without doing put_prev_entity() we also | |
6744 | * have to consider cfs_rq->curr. If it is still a runnable | |
6745 | * entity, update_curr() will update its vruntime, otherwise | |
6746 | * forget we've ever seen it. | |
6747 | */ | |
54d27365 BS |
6748 | if (curr) { |
6749 | if (curr->on_rq) | |
6750 | update_curr(cfs_rq); | |
6751 | else | |
6752 | curr = NULL; | |
678d5718 | 6753 | |
54d27365 BS |
6754 | /* |
6755 | * This call to check_cfs_rq_runtime() will do the | |
6756 | * throttle and dequeue its entity in the parent(s). | |
9674f5ca | 6757 | * Therefore the nr_running test will indeed |
54d27365 BS |
6758 | * be correct. |
6759 | */ | |
9674f5ca VK |
6760 | if (unlikely(check_cfs_rq_runtime(cfs_rq))) { |
6761 | cfs_rq = &rq->cfs; | |
6762 | ||
6763 | if (!cfs_rq->nr_running) | |
6764 | goto idle; | |
6765 | ||
54d27365 | 6766 | goto simple; |
9674f5ca | 6767 | } |
54d27365 | 6768 | } |
678d5718 PZ |
6769 | |
6770 | se = pick_next_entity(cfs_rq, curr); | |
6771 | cfs_rq = group_cfs_rq(se); | |
6772 | } while (cfs_rq); | |
6773 | ||
6774 | p = task_of(se); | |
6775 | ||
6776 | /* | |
6777 | * Since we haven't yet done put_prev_entity and if the selected task | |
6778 | * is a different task than we started out with, try and touch the | |
6779 | * least amount of cfs_rqs. | |
6780 | */ | |
6781 | if (prev != p) { | |
6782 | struct sched_entity *pse = &prev->se; | |
6783 | ||
6784 | while (!(cfs_rq = is_same_group(se, pse))) { | |
6785 | int se_depth = se->depth; | |
6786 | int pse_depth = pse->depth; | |
6787 | ||
6788 | if (se_depth <= pse_depth) { | |
6789 | put_prev_entity(cfs_rq_of(pse), pse); | |
6790 | pse = parent_entity(pse); | |
6791 | } | |
6792 | if (se_depth >= pse_depth) { | |
6793 | set_next_entity(cfs_rq_of(se), se); | |
6794 | se = parent_entity(se); | |
6795 | } | |
6796 | } | |
6797 | ||
6798 | put_prev_entity(cfs_rq, pse); | |
6799 | set_next_entity(cfs_rq, se); | |
6800 | } | |
6801 | ||
93824900 | 6802 | goto done; |
678d5718 | 6803 | simple: |
678d5718 | 6804 | #endif |
bf0f6f24 | 6805 | |
3f1d2a31 | 6806 | put_prev_task(rq, prev); |
606dba2e | 6807 | |
bf0f6f24 | 6808 | do { |
678d5718 | 6809 | se = pick_next_entity(cfs_rq, NULL); |
f4b6755f | 6810 | set_next_entity(cfs_rq, se); |
bf0f6f24 IM |
6811 | cfs_rq = group_cfs_rq(se); |
6812 | } while (cfs_rq); | |
6813 | ||
8f4d37ec | 6814 | p = task_of(se); |
678d5718 | 6815 | |
13a453c2 | 6816 | done: __maybe_unused; |
93824900 UR |
6817 | #ifdef CONFIG_SMP |
6818 | /* | |
6819 | * Move the next running task to the front of | |
6820 | * the list, so our cfs_tasks list becomes MRU | |
6821 | * one. | |
6822 | */ | |
6823 | list_move(&p->se.group_node, &rq->cfs_tasks); | |
6824 | #endif | |
6825 | ||
b39e66ea MG |
6826 | if (hrtick_enabled(rq)) |
6827 | hrtick_start_fair(rq, p); | |
8f4d37ec PZ |
6828 | |
6829 | return p; | |
38033c37 PZ |
6830 | |
6831 | idle: | |
46f69fa3 MF |
6832 | new_tasks = idle_balance(rq, rf); |
6833 | ||
37e117c0 PZ |
6834 | /* |
6835 | * Because idle_balance() releases (and re-acquires) rq->lock, it is | |
6836 | * possible for any higher priority task to appear. In that case we | |
6837 | * must re-start the pick_next_entity() loop. | |
6838 | */ | |
e4aa358b | 6839 | if (new_tasks < 0) |
37e117c0 PZ |
6840 | return RETRY_TASK; |
6841 | ||
e4aa358b | 6842 | if (new_tasks > 0) |
38033c37 | 6843 | goto again; |
38033c37 PZ |
6844 | |
6845 | return NULL; | |
bf0f6f24 IM |
6846 | } |
6847 | ||
6848 | /* | |
6849 | * Account for a descheduled task: | |
6850 | */ | |
31ee529c | 6851 | static void put_prev_task_fair(struct rq *rq, struct task_struct *prev) |
bf0f6f24 IM |
6852 | { |
6853 | struct sched_entity *se = &prev->se; | |
6854 | struct cfs_rq *cfs_rq; | |
6855 | ||
6856 | for_each_sched_entity(se) { | |
6857 | cfs_rq = cfs_rq_of(se); | |
ab6cde26 | 6858 | put_prev_entity(cfs_rq, se); |
bf0f6f24 IM |
6859 | } |
6860 | } | |
6861 | ||
ac53db59 RR |
6862 | /* |
6863 | * sched_yield() is very simple | |
6864 | * | |
6865 | * The magic of dealing with the ->skip buddy is in pick_next_entity. | |
6866 | */ | |
6867 | static void yield_task_fair(struct rq *rq) | |
6868 | { | |
6869 | struct task_struct *curr = rq->curr; | |
6870 | struct cfs_rq *cfs_rq = task_cfs_rq(curr); | |
6871 | struct sched_entity *se = &curr->se; | |
6872 | ||
6873 | /* | |
6874 | * Are we the only task in the tree? | |
6875 | */ | |
6876 | if (unlikely(rq->nr_running == 1)) | |
6877 | return; | |
6878 | ||
6879 | clear_buddies(cfs_rq, se); | |
6880 | ||
6881 | if (curr->policy != SCHED_BATCH) { | |
6882 | update_rq_clock(rq); | |
6883 | /* | |
6884 | * Update run-time statistics of the 'current'. | |
6885 | */ | |
6886 | update_curr(cfs_rq); | |
916671c0 MG |
6887 | /* |
6888 | * Tell update_rq_clock() that we've just updated, | |
6889 | * so we don't do microscopic update in schedule() | |
6890 | * and double the fastpath cost. | |
6891 | */ | |
9edfbfed | 6892 | rq_clock_skip_update(rq, true); |
ac53db59 RR |
6893 | } |
6894 | ||
6895 | set_skip_buddy(se); | |
6896 | } | |
6897 | ||
d95f4122 MG |
6898 | static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt) |
6899 | { | |
6900 | struct sched_entity *se = &p->se; | |
6901 | ||
5238cdd3 PT |
6902 | /* throttled hierarchies are not runnable */ |
6903 | if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se))) | |
d95f4122 MG |
6904 | return false; |
6905 | ||
6906 | /* Tell the scheduler that we'd really like pse to run next. */ | |
6907 | set_next_buddy(se); | |
6908 | ||
d95f4122 MG |
6909 | yield_task_fair(rq); |
6910 | ||
6911 | return true; | |
6912 | } | |
6913 | ||
681f3e68 | 6914 | #ifdef CONFIG_SMP |
bf0f6f24 | 6915 | /************************************************** |
e9c84cb8 PZ |
6916 | * Fair scheduling class load-balancing methods. |
6917 | * | |
6918 | * BASICS | |
6919 | * | |
6920 | * The purpose of load-balancing is to achieve the same basic fairness the | |
97fb7a0a | 6921 | * per-CPU scheduler provides, namely provide a proportional amount of compute |
e9c84cb8 PZ |
6922 | * time to each task. This is expressed in the following equation: |
6923 | * | |
6924 | * W_i,n/P_i == W_j,n/P_j for all i,j (1) | |
6925 | * | |
97fb7a0a | 6926 | * Where W_i,n is the n-th weight average for CPU i. The instantaneous weight |
e9c84cb8 PZ |
6927 | * W_i,0 is defined as: |
6928 | * | |
6929 | * W_i,0 = \Sum_j w_i,j (2) | |
6930 | * | |
97fb7a0a | 6931 | * Where w_i,j is the weight of the j-th runnable task on CPU i. This weight |
1c3de5e1 | 6932 | * is derived from the nice value as per sched_prio_to_weight[]. |
e9c84cb8 PZ |
6933 | * |
6934 | * The weight average is an exponential decay average of the instantaneous | |
6935 | * weight: | |
6936 | * | |
6937 | * W'_i,n = (2^n - 1) / 2^n * W_i,n + 1 / 2^n * W_i,0 (3) | |
6938 | * | |
97fb7a0a | 6939 | * C_i is the compute capacity of CPU i, typically it is the |
e9c84cb8 PZ |
6940 | * fraction of 'recent' time available for SCHED_OTHER task execution. But it |
6941 | * can also include other factors [XXX]. | |
6942 | * | |
6943 | * To achieve this balance we define a measure of imbalance which follows | |
6944 | * directly from (1): | |
6945 | * | |
ced549fa | 6946 | * imb_i,j = max{ avg(W/C), W_i/C_i } - min{ avg(W/C), W_j/C_j } (4) |
e9c84cb8 PZ |
6947 | * |
6948 | * We them move tasks around to minimize the imbalance. In the continuous | |
6949 | * function space it is obvious this converges, in the discrete case we get | |
6950 | * a few fun cases generally called infeasible weight scenarios. | |
6951 | * | |
6952 | * [XXX expand on: | |
6953 | * - infeasible weights; | |
6954 | * - local vs global optima in the discrete case. ] | |
6955 | * | |
6956 | * | |
6957 | * SCHED DOMAINS | |
6958 | * | |
6959 | * In order to solve the imbalance equation (4), and avoid the obvious O(n^2) | |
97fb7a0a | 6960 | * for all i,j solution, we create a tree of CPUs that follows the hardware |
e9c84cb8 | 6961 | * topology where each level pairs two lower groups (or better). This results |
97fb7a0a | 6962 | * in O(log n) layers. Furthermore we reduce the number of CPUs going up the |
e9c84cb8 | 6963 | * tree to only the first of the previous level and we decrease the frequency |
97fb7a0a | 6964 | * of load-balance at each level inv. proportional to the number of CPUs in |
e9c84cb8 PZ |
6965 | * the groups. |
6966 | * | |
6967 | * This yields: | |
6968 | * | |
6969 | * log_2 n 1 n | |
6970 | * \Sum { --- * --- * 2^i } = O(n) (5) | |
6971 | * i = 0 2^i 2^i | |
6972 | * `- size of each group | |
97fb7a0a | 6973 | * | | `- number of CPUs doing load-balance |
e9c84cb8 PZ |
6974 | * | `- freq |
6975 | * `- sum over all levels | |
6976 | * | |
6977 | * Coupled with a limit on how many tasks we can migrate every balance pass, | |
6978 | * this makes (5) the runtime complexity of the balancer. | |
6979 | * | |
6980 | * An important property here is that each CPU is still (indirectly) connected | |
97fb7a0a | 6981 | * to every other CPU in at most O(log n) steps: |
e9c84cb8 PZ |
6982 | * |
6983 | * The adjacency matrix of the resulting graph is given by: | |
6984 | * | |
97a7142f | 6985 | * log_2 n |
e9c84cb8 PZ |
6986 | * A_i,j = \Union (i % 2^k == 0) && i / 2^(k+1) == j / 2^(k+1) (6) |
6987 | * k = 0 | |
6988 | * | |
6989 | * And you'll find that: | |
6990 | * | |
6991 | * A^(log_2 n)_i,j != 0 for all i,j (7) | |
6992 | * | |
97fb7a0a | 6993 | * Showing there's indeed a path between every CPU in at most O(log n) steps. |
e9c84cb8 PZ |
6994 | * The task movement gives a factor of O(m), giving a convergence complexity |
6995 | * of: | |
6996 | * | |
6997 | * O(nm log n), n := nr_cpus, m := nr_tasks (8) | |
6998 | * | |
6999 | * | |
7000 | * WORK CONSERVING | |
7001 | * | |
7002 | * In order to avoid CPUs going idle while there's still work to do, new idle | |
97fb7a0a | 7003 | * balancing is more aggressive and has the newly idle CPU iterate up the domain |
e9c84cb8 PZ |
7004 | * tree itself instead of relying on other CPUs to bring it work. |
7005 | * | |
7006 | * This adds some complexity to both (5) and (8) but it reduces the total idle | |
7007 | * time. | |
7008 | * | |
7009 | * [XXX more?] | |
7010 | * | |
7011 | * | |
7012 | * CGROUPS | |
7013 | * | |
7014 | * Cgroups make a horror show out of (2), instead of a simple sum we get: | |
7015 | * | |
7016 | * s_k,i | |
7017 | * W_i,0 = \Sum_j \Prod_k w_k * ----- (9) | |
7018 | * S_k | |
7019 | * | |
7020 | * Where | |
7021 | * | |
7022 | * s_k,i = \Sum_j w_i,j,k and S_k = \Sum_i s_k,i (10) | |
7023 | * | |
97fb7a0a | 7024 | * w_i,j,k is the weight of the j-th runnable task in the k-th cgroup on CPU i. |
e9c84cb8 PZ |
7025 | * |
7026 | * The big problem is S_k, its a global sum needed to compute a local (W_i) | |
7027 | * property. | |
7028 | * | |
7029 | * [XXX write more on how we solve this.. _after_ merging pjt's patches that | |
7030 | * rewrite all of this once again.] | |
97a7142f | 7031 | */ |
bf0f6f24 | 7032 | |
ed387b78 HS |
7033 | static unsigned long __read_mostly max_load_balance_interval = HZ/10; |
7034 | ||
0ec8aa00 PZ |
7035 | enum fbq_type { regular, remote, all }; |
7036 | ||
ddcdf6e7 | 7037 | #define LBF_ALL_PINNED 0x01 |
367456c7 | 7038 | #define LBF_NEED_BREAK 0x02 |
6263322c PZ |
7039 | #define LBF_DST_PINNED 0x04 |
7040 | #define LBF_SOME_PINNED 0x08 | |
e022e0d3 | 7041 | #define LBF_NOHZ_STATS 0x10 |
f643ea22 | 7042 | #define LBF_NOHZ_AGAIN 0x20 |
ddcdf6e7 PZ |
7043 | |
7044 | struct lb_env { | |
7045 | struct sched_domain *sd; | |
7046 | ||
ddcdf6e7 | 7047 | struct rq *src_rq; |
85c1e7da | 7048 | int src_cpu; |
ddcdf6e7 PZ |
7049 | |
7050 | int dst_cpu; | |
7051 | struct rq *dst_rq; | |
7052 | ||
88b8dac0 SV |
7053 | struct cpumask *dst_grpmask; |
7054 | int new_dst_cpu; | |
ddcdf6e7 | 7055 | enum cpu_idle_type idle; |
bd939f45 | 7056 | long imbalance; |
b9403130 MW |
7057 | /* The set of CPUs under consideration for load-balancing */ |
7058 | struct cpumask *cpus; | |
7059 | ||
ddcdf6e7 | 7060 | unsigned int flags; |
367456c7 PZ |
7061 | |
7062 | unsigned int loop; | |
7063 | unsigned int loop_break; | |
7064 | unsigned int loop_max; | |
0ec8aa00 PZ |
7065 | |
7066 | enum fbq_type fbq_type; | |
163122b7 | 7067 | struct list_head tasks; |
ddcdf6e7 PZ |
7068 | }; |
7069 | ||
029632fb PZ |
7070 | /* |
7071 | * Is this task likely cache-hot: | |
7072 | */ | |
5d5e2b1b | 7073 | static int task_hot(struct task_struct *p, struct lb_env *env) |
029632fb PZ |
7074 | { |
7075 | s64 delta; | |
7076 | ||
e5673f28 KT |
7077 | lockdep_assert_held(&env->src_rq->lock); |
7078 | ||
029632fb PZ |
7079 | if (p->sched_class != &fair_sched_class) |
7080 | return 0; | |
7081 | ||
7082 | if (unlikely(p->policy == SCHED_IDLE)) | |
7083 | return 0; | |
7084 | ||
7085 | /* | |
7086 | * Buddy candidates are cache hot: | |
7087 | */ | |
5d5e2b1b | 7088 | if (sched_feat(CACHE_HOT_BUDDY) && env->dst_rq->nr_running && |
029632fb PZ |
7089 | (&p->se == cfs_rq_of(&p->se)->next || |
7090 | &p->se == cfs_rq_of(&p->se)->last)) | |
7091 | return 1; | |
7092 | ||
7093 | if (sysctl_sched_migration_cost == -1) | |
7094 | return 1; | |
7095 | if (sysctl_sched_migration_cost == 0) | |
7096 | return 0; | |
7097 | ||
5d5e2b1b | 7098 | delta = rq_clock_task(env->src_rq) - p->se.exec_start; |
029632fb PZ |
7099 | |
7100 | return delta < (s64)sysctl_sched_migration_cost; | |
7101 | } | |
7102 | ||
3a7053b3 | 7103 | #ifdef CONFIG_NUMA_BALANCING |
c1ceac62 | 7104 | /* |
2a1ed24c SD |
7105 | * Returns 1, if task migration degrades locality |
7106 | * Returns 0, if task migration improves locality i.e migration preferred. | |
7107 | * Returns -1, if task migration is not affected by locality. | |
c1ceac62 | 7108 | */ |
2a1ed24c | 7109 | static int migrate_degrades_locality(struct task_struct *p, struct lb_env *env) |
3a7053b3 | 7110 | { |
b1ad065e | 7111 | struct numa_group *numa_group = rcu_dereference(p->numa_group); |
c1ceac62 | 7112 | unsigned long src_faults, dst_faults; |
3a7053b3 MG |
7113 | int src_nid, dst_nid; |
7114 | ||
2a595721 | 7115 | if (!static_branch_likely(&sched_numa_balancing)) |
2a1ed24c SD |
7116 | return -1; |
7117 | ||
c3b9bc5b | 7118 | if (!p->numa_faults || !(env->sd->flags & SD_NUMA)) |
2a1ed24c | 7119 | return -1; |
7a0f3083 MG |
7120 | |
7121 | src_nid = cpu_to_node(env->src_cpu); | |
7122 | dst_nid = cpu_to_node(env->dst_cpu); | |
7123 | ||
83e1d2cd | 7124 | if (src_nid == dst_nid) |
2a1ed24c | 7125 | return -1; |
7a0f3083 | 7126 | |
2a1ed24c SD |
7127 | /* Migrating away from the preferred node is always bad. */ |
7128 | if (src_nid == p->numa_preferred_nid) { | |
7129 | if (env->src_rq->nr_running > env->src_rq->nr_preferred_running) | |
7130 | return 1; | |
7131 | else | |
7132 | return -1; | |
7133 | } | |
b1ad065e | 7134 | |
c1ceac62 RR |
7135 | /* Encourage migration to the preferred node. */ |
7136 | if (dst_nid == p->numa_preferred_nid) | |
2a1ed24c | 7137 | return 0; |
b1ad065e | 7138 | |
739294fb RR |
7139 | /* Leaving a core idle is often worse than degrading locality. */ |
7140 | if (env->idle != CPU_NOT_IDLE) | |
7141 | return -1; | |
7142 | ||
c1ceac62 RR |
7143 | if (numa_group) { |
7144 | src_faults = group_faults(p, src_nid); | |
7145 | dst_faults = group_faults(p, dst_nid); | |
7146 | } else { | |
7147 | src_faults = task_faults(p, src_nid); | |
7148 | dst_faults = task_faults(p, dst_nid); | |
b1ad065e RR |
7149 | } |
7150 | ||
c1ceac62 | 7151 | return dst_faults < src_faults; |
7a0f3083 MG |
7152 | } |
7153 | ||
3a7053b3 | 7154 | #else |
2a1ed24c | 7155 | static inline int migrate_degrades_locality(struct task_struct *p, |
3a7053b3 MG |
7156 | struct lb_env *env) |
7157 | { | |
2a1ed24c | 7158 | return -1; |
7a0f3083 | 7159 | } |
3a7053b3 MG |
7160 | #endif |
7161 | ||
1e3c88bd PZ |
7162 | /* |
7163 | * can_migrate_task - may task p from runqueue rq be migrated to this_cpu? | |
7164 | */ | |
7165 | static | |
8e45cb54 | 7166 | int can_migrate_task(struct task_struct *p, struct lb_env *env) |
1e3c88bd | 7167 | { |
2a1ed24c | 7168 | int tsk_cache_hot; |
e5673f28 KT |
7169 | |
7170 | lockdep_assert_held(&env->src_rq->lock); | |
7171 | ||
1e3c88bd PZ |
7172 | /* |
7173 | * We do not migrate tasks that are: | |
d3198084 | 7174 | * 1) throttled_lb_pair, or |
1e3c88bd | 7175 | * 2) cannot be migrated to this CPU due to cpus_allowed, or |
d3198084 JK |
7176 | * 3) running (obviously), or |
7177 | * 4) are cache-hot on their current CPU. | |
1e3c88bd | 7178 | */ |
d3198084 JK |
7179 | if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu)) |
7180 | return 0; | |
7181 | ||
0c98d344 | 7182 | if (!cpumask_test_cpu(env->dst_cpu, &p->cpus_allowed)) { |
e02e60c1 | 7183 | int cpu; |
88b8dac0 | 7184 | |
ae92882e | 7185 | schedstat_inc(p->se.statistics.nr_failed_migrations_affine); |
88b8dac0 | 7186 | |
6263322c PZ |
7187 | env->flags |= LBF_SOME_PINNED; |
7188 | ||
88b8dac0 | 7189 | /* |
97fb7a0a | 7190 | * Remember if this task can be migrated to any other CPU in |
88b8dac0 SV |
7191 | * our sched_group. We may want to revisit it if we couldn't |
7192 | * meet load balance goals by pulling other tasks on src_cpu. | |
7193 | * | |
65a4433a JH |
7194 | * Avoid computing new_dst_cpu for NEWLY_IDLE or if we have |
7195 | * already computed one in current iteration. | |
88b8dac0 | 7196 | */ |
65a4433a | 7197 | if (env->idle == CPU_NEWLY_IDLE || (env->flags & LBF_DST_PINNED)) |
88b8dac0 SV |
7198 | return 0; |
7199 | ||
97fb7a0a | 7200 | /* Prevent to re-select dst_cpu via env's CPUs: */ |
e02e60c1 | 7201 | for_each_cpu_and(cpu, env->dst_grpmask, env->cpus) { |
0c98d344 | 7202 | if (cpumask_test_cpu(cpu, &p->cpus_allowed)) { |
6263322c | 7203 | env->flags |= LBF_DST_PINNED; |
e02e60c1 JK |
7204 | env->new_dst_cpu = cpu; |
7205 | break; | |
7206 | } | |
88b8dac0 | 7207 | } |
e02e60c1 | 7208 | |
1e3c88bd PZ |
7209 | return 0; |
7210 | } | |
88b8dac0 SV |
7211 | |
7212 | /* Record that we found atleast one task that could run on dst_cpu */ | |
8e45cb54 | 7213 | env->flags &= ~LBF_ALL_PINNED; |
1e3c88bd | 7214 | |
ddcdf6e7 | 7215 | if (task_running(env->src_rq, p)) { |
ae92882e | 7216 | schedstat_inc(p->se.statistics.nr_failed_migrations_running); |
1e3c88bd PZ |
7217 | return 0; |
7218 | } | |
7219 | ||
7220 | /* | |
7221 | * Aggressive migration if: | |
3a7053b3 MG |
7222 | * 1) destination numa is preferred |
7223 | * 2) task is cache cold, or | |
7224 | * 3) too many balance attempts have failed. | |
1e3c88bd | 7225 | */ |
2a1ed24c SD |
7226 | tsk_cache_hot = migrate_degrades_locality(p, env); |
7227 | if (tsk_cache_hot == -1) | |
7228 | tsk_cache_hot = task_hot(p, env); | |
3a7053b3 | 7229 | |
2a1ed24c | 7230 | if (tsk_cache_hot <= 0 || |
7a96c231 | 7231 | env->sd->nr_balance_failed > env->sd->cache_nice_tries) { |
2a1ed24c | 7232 | if (tsk_cache_hot == 1) { |
ae92882e JP |
7233 | schedstat_inc(env->sd->lb_hot_gained[env->idle]); |
7234 | schedstat_inc(p->se.statistics.nr_forced_migrations); | |
3a7053b3 | 7235 | } |
1e3c88bd PZ |
7236 | return 1; |
7237 | } | |
7238 | ||
ae92882e | 7239 | schedstat_inc(p->se.statistics.nr_failed_migrations_hot); |
4e2dcb73 | 7240 | return 0; |
1e3c88bd PZ |
7241 | } |
7242 | ||
897c395f | 7243 | /* |
163122b7 KT |
7244 | * detach_task() -- detach the task for the migration specified in env |
7245 | */ | |
7246 | static void detach_task(struct task_struct *p, struct lb_env *env) | |
7247 | { | |
7248 | lockdep_assert_held(&env->src_rq->lock); | |
7249 | ||
163122b7 | 7250 | p->on_rq = TASK_ON_RQ_MIGRATING; |
5704ac0a | 7251 | deactivate_task(env->src_rq, p, DEQUEUE_NOCLOCK); |
163122b7 KT |
7252 | set_task_cpu(p, env->dst_cpu); |
7253 | } | |
7254 | ||
897c395f | 7255 | /* |
e5673f28 | 7256 | * detach_one_task() -- tries to dequeue exactly one task from env->src_rq, as |
897c395f | 7257 | * part of active balancing operations within "domain". |
897c395f | 7258 | * |
e5673f28 | 7259 | * Returns a task if successful and NULL otherwise. |
897c395f | 7260 | */ |
e5673f28 | 7261 | static struct task_struct *detach_one_task(struct lb_env *env) |
897c395f | 7262 | { |
93824900 | 7263 | struct task_struct *p; |
897c395f | 7264 | |
e5673f28 KT |
7265 | lockdep_assert_held(&env->src_rq->lock); |
7266 | ||
93824900 UR |
7267 | list_for_each_entry_reverse(p, |
7268 | &env->src_rq->cfs_tasks, se.group_node) { | |
367456c7 PZ |
7269 | if (!can_migrate_task(p, env)) |
7270 | continue; | |
897c395f | 7271 | |
163122b7 | 7272 | detach_task(p, env); |
e5673f28 | 7273 | |
367456c7 | 7274 | /* |
e5673f28 | 7275 | * Right now, this is only the second place where |
163122b7 | 7276 | * lb_gained[env->idle] is updated (other is detach_tasks) |
e5673f28 | 7277 | * so we can safely collect stats here rather than |
163122b7 | 7278 | * inside detach_tasks(). |
367456c7 | 7279 | */ |
ae92882e | 7280 | schedstat_inc(env->sd->lb_gained[env->idle]); |
e5673f28 | 7281 | return p; |
897c395f | 7282 | } |
e5673f28 | 7283 | return NULL; |
897c395f PZ |
7284 | } |
7285 | ||
eb95308e PZ |
7286 | static const unsigned int sched_nr_migrate_break = 32; |
7287 | ||
5d6523eb | 7288 | /* |
163122b7 KT |
7289 | * detach_tasks() -- tries to detach up to imbalance weighted load from |
7290 | * busiest_rq, as part of a balancing operation within domain "sd". | |
5d6523eb | 7291 | * |
163122b7 | 7292 | * Returns number of detached tasks if successful and 0 otherwise. |
5d6523eb | 7293 | */ |
163122b7 | 7294 | static int detach_tasks(struct lb_env *env) |
1e3c88bd | 7295 | { |
5d6523eb PZ |
7296 | struct list_head *tasks = &env->src_rq->cfs_tasks; |
7297 | struct task_struct *p; | |
367456c7 | 7298 | unsigned long load; |
163122b7 KT |
7299 | int detached = 0; |
7300 | ||
7301 | lockdep_assert_held(&env->src_rq->lock); | |
1e3c88bd | 7302 | |
bd939f45 | 7303 | if (env->imbalance <= 0) |
5d6523eb | 7304 | return 0; |
1e3c88bd | 7305 | |
5d6523eb | 7306 | while (!list_empty(tasks)) { |
985d3a4c YD |
7307 | /* |
7308 | * We don't want to steal all, otherwise we may be treated likewise, | |
7309 | * which could at worst lead to a livelock crash. | |
7310 | */ | |
7311 | if (env->idle != CPU_NOT_IDLE && env->src_rq->nr_running <= 1) | |
7312 | break; | |
7313 | ||
93824900 | 7314 | p = list_last_entry(tasks, struct task_struct, se.group_node); |
1e3c88bd | 7315 | |
367456c7 PZ |
7316 | env->loop++; |
7317 | /* We've more or less seen every task there is, call it quits */ | |
5d6523eb | 7318 | if (env->loop > env->loop_max) |
367456c7 | 7319 | break; |
5d6523eb PZ |
7320 | |
7321 | /* take a breather every nr_migrate tasks */ | |
367456c7 | 7322 | if (env->loop > env->loop_break) { |
eb95308e | 7323 | env->loop_break += sched_nr_migrate_break; |
8e45cb54 | 7324 | env->flags |= LBF_NEED_BREAK; |
ee00e66f | 7325 | break; |
a195f004 | 7326 | } |
1e3c88bd | 7327 | |
d3198084 | 7328 | if (!can_migrate_task(p, env)) |
367456c7 PZ |
7329 | goto next; |
7330 | ||
7331 | load = task_h_load(p); | |
5d6523eb | 7332 | |
eb95308e | 7333 | if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed) |
367456c7 PZ |
7334 | goto next; |
7335 | ||
bd939f45 | 7336 | if ((load / 2) > env->imbalance) |
367456c7 | 7337 | goto next; |
1e3c88bd | 7338 | |
163122b7 KT |
7339 | detach_task(p, env); |
7340 | list_add(&p->se.group_node, &env->tasks); | |
7341 | ||
7342 | detached++; | |
bd939f45 | 7343 | env->imbalance -= load; |
1e3c88bd PZ |
7344 | |
7345 | #ifdef CONFIG_PREEMPT | |
ee00e66f PZ |
7346 | /* |
7347 | * NEWIDLE balancing is a source of latency, so preemptible | |
163122b7 | 7348 | * kernels will stop after the first task is detached to minimize |
ee00e66f PZ |
7349 | * the critical section. |
7350 | */ | |
5d6523eb | 7351 | if (env->idle == CPU_NEWLY_IDLE) |
ee00e66f | 7352 | break; |
1e3c88bd PZ |
7353 | #endif |
7354 | ||
ee00e66f PZ |
7355 | /* |
7356 | * We only want to steal up to the prescribed amount of | |
7357 | * weighted load. | |
7358 | */ | |
bd939f45 | 7359 | if (env->imbalance <= 0) |
ee00e66f | 7360 | break; |
367456c7 PZ |
7361 | |
7362 | continue; | |
7363 | next: | |
93824900 | 7364 | list_move(&p->se.group_node, tasks); |
1e3c88bd | 7365 | } |
5d6523eb | 7366 | |
1e3c88bd | 7367 | /* |
163122b7 KT |
7368 | * Right now, this is one of only two places we collect this stat |
7369 | * so we can safely collect detach_one_task() stats here rather | |
7370 | * than inside detach_one_task(). | |
1e3c88bd | 7371 | */ |
ae92882e | 7372 | schedstat_add(env->sd->lb_gained[env->idle], detached); |
1e3c88bd | 7373 | |
163122b7 KT |
7374 | return detached; |
7375 | } | |
7376 | ||
7377 | /* | |
7378 | * attach_task() -- attach the task detached by detach_task() to its new rq. | |
7379 | */ | |
7380 | static void attach_task(struct rq *rq, struct task_struct *p) | |
7381 | { | |
7382 | lockdep_assert_held(&rq->lock); | |
7383 | ||
7384 | BUG_ON(task_rq(p) != rq); | |
5704ac0a | 7385 | activate_task(rq, p, ENQUEUE_NOCLOCK); |
3ea94de1 | 7386 | p->on_rq = TASK_ON_RQ_QUEUED; |
163122b7 KT |
7387 | check_preempt_curr(rq, p, 0); |
7388 | } | |
7389 | ||
7390 | /* | |
7391 | * attach_one_task() -- attaches the task returned from detach_one_task() to | |
7392 | * its new rq. | |
7393 | */ | |
7394 | static void attach_one_task(struct rq *rq, struct task_struct *p) | |
7395 | { | |
8a8c69c3 PZ |
7396 | struct rq_flags rf; |
7397 | ||
7398 | rq_lock(rq, &rf); | |
5704ac0a | 7399 | update_rq_clock(rq); |
163122b7 | 7400 | attach_task(rq, p); |
8a8c69c3 | 7401 | rq_unlock(rq, &rf); |
163122b7 KT |
7402 | } |
7403 | ||
7404 | /* | |
7405 | * attach_tasks() -- attaches all tasks detached by detach_tasks() to their | |
7406 | * new rq. | |
7407 | */ | |
7408 | static void attach_tasks(struct lb_env *env) | |
7409 | { | |
7410 | struct list_head *tasks = &env->tasks; | |
7411 | struct task_struct *p; | |
8a8c69c3 | 7412 | struct rq_flags rf; |
163122b7 | 7413 | |
8a8c69c3 | 7414 | rq_lock(env->dst_rq, &rf); |
5704ac0a | 7415 | update_rq_clock(env->dst_rq); |
163122b7 KT |
7416 | |
7417 | while (!list_empty(tasks)) { | |
7418 | p = list_first_entry(tasks, struct task_struct, se.group_node); | |
7419 | list_del_init(&p->se.group_node); | |
1e3c88bd | 7420 | |
163122b7 KT |
7421 | attach_task(env->dst_rq, p); |
7422 | } | |
7423 | ||
8a8c69c3 | 7424 | rq_unlock(env->dst_rq, &rf); |
1e3c88bd PZ |
7425 | } |
7426 | ||
1936c53c VG |
7427 | static inline bool cfs_rq_has_blocked(struct cfs_rq *cfs_rq) |
7428 | { | |
7429 | if (cfs_rq->avg.load_avg) | |
7430 | return true; | |
7431 | ||
7432 | if (cfs_rq->avg.util_avg) | |
7433 | return true; | |
7434 | ||
7435 | return false; | |
7436 | } | |
7437 | ||
7438 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
7439 | ||
a9e7f654 TH |
7440 | static inline bool cfs_rq_is_decayed(struct cfs_rq *cfs_rq) |
7441 | { | |
7442 | if (cfs_rq->load.weight) | |
7443 | return false; | |
7444 | ||
7445 | if (cfs_rq->avg.load_sum) | |
7446 | return false; | |
7447 | ||
7448 | if (cfs_rq->avg.util_sum) | |
7449 | return false; | |
7450 | ||
1ea6c46a | 7451 | if (cfs_rq->avg.runnable_load_sum) |
a9e7f654 TH |
7452 | return false; |
7453 | ||
7454 | return true; | |
7455 | } | |
7456 | ||
48a16753 | 7457 | static void update_blocked_averages(int cpu) |
9e3081ca | 7458 | { |
9e3081ca | 7459 | struct rq *rq = cpu_rq(cpu); |
a9e7f654 | 7460 | struct cfs_rq *cfs_rq, *pos; |
8a8c69c3 | 7461 | struct rq_flags rf; |
f643ea22 | 7462 | bool done = true; |
9e3081ca | 7463 | |
8a8c69c3 | 7464 | rq_lock_irqsave(rq, &rf); |
48a16753 | 7465 | update_rq_clock(rq); |
9d89c257 | 7466 | |
9763b67f PZ |
7467 | /* |
7468 | * Iterates the task_group tree in a bottom up fashion, see | |
7469 | * list_add_leaf_cfs_rq() for details. | |
7470 | */ | |
a9e7f654 | 7471 | for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) { |
bc427898 VG |
7472 | struct sched_entity *se; |
7473 | ||
9d89c257 YD |
7474 | /* throttled entities do not contribute to load */ |
7475 | if (throttled_hierarchy(cfs_rq)) | |
7476 | continue; | |
48a16753 | 7477 | |
3a123bbb | 7478 | if (update_cfs_rq_load_avg(cfs_rq_clock_task(cfs_rq), cfs_rq)) |
9d89c257 | 7479 | update_tg_load_avg(cfs_rq, 0); |
4e516076 | 7480 | |
bc427898 VG |
7481 | /* Propagate pending load changes to the parent, if any: */ |
7482 | se = cfs_rq->tg->se[cpu]; | |
7483 | if (se && !skip_blocked_update(se)) | |
88c0616e | 7484 | update_load_avg(cfs_rq_of(se), se, 0); |
a9e7f654 TH |
7485 | |
7486 | /* | |
7487 | * There can be a lot of idle CPU cgroups. Don't let fully | |
7488 | * decayed cfs_rqs linger on the list. | |
7489 | */ | |
7490 | if (cfs_rq_is_decayed(cfs_rq)) | |
7491 | list_del_leaf_cfs_rq(cfs_rq); | |
1936c53c VG |
7492 | |
7493 | /* Don't need periodic decay once load/util_avg are null */ | |
7494 | if (cfs_rq_has_blocked(cfs_rq)) | |
f643ea22 | 7495 | done = false; |
9d89c257 | 7496 | } |
e022e0d3 PZ |
7497 | |
7498 | #ifdef CONFIG_NO_HZ_COMMON | |
7499 | rq->last_blocked_load_update_tick = jiffies; | |
f643ea22 VG |
7500 | if (done) |
7501 | rq->has_blocked_load = 0; | |
e022e0d3 | 7502 | #endif |
8a8c69c3 | 7503 | rq_unlock_irqrestore(rq, &rf); |
9e3081ca PZ |
7504 | } |
7505 | ||
9763b67f | 7506 | /* |
68520796 | 7507 | * Compute the hierarchical load factor for cfs_rq and all its ascendants. |
9763b67f PZ |
7508 | * This needs to be done in a top-down fashion because the load of a child |
7509 | * group is a fraction of its parents load. | |
7510 | */ | |
68520796 | 7511 | static void update_cfs_rq_h_load(struct cfs_rq *cfs_rq) |
9763b67f | 7512 | { |
68520796 VD |
7513 | struct rq *rq = rq_of(cfs_rq); |
7514 | struct sched_entity *se = cfs_rq->tg->se[cpu_of(rq)]; | |
a35b6466 | 7515 | unsigned long now = jiffies; |
68520796 | 7516 | unsigned long load; |
a35b6466 | 7517 | |
68520796 | 7518 | if (cfs_rq->last_h_load_update == now) |
a35b6466 PZ |
7519 | return; |
7520 | ||
68520796 VD |
7521 | cfs_rq->h_load_next = NULL; |
7522 | for_each_sched_entity(se) { | |
7523 | cfs_rq = cfs_rq_of(se); | |
7524 | cfs_rq->h_load_next = se; | |
7525 | if (cfs_rq->last_h_load_update == now) | |
7526 | break; | |
7527 | } | |
a35b6466 | 7528 | |
68520796 | 7529 | if (!se) { |
7ea241af | 7530 | cfs_rq->h_load = cfs_rq_load_avg(cfs_rq); |
68520796 VD |
7531 | cfs_rq->last_h_load_update = now; |
7532 | } | |
7533 | ||
7534 | while ((se = cfs_rq->h_load_next) != NULL) { | |
7535 | load = cfs_rq->h_load; | |
7ea241af YD |
7536 | load = div64_ul(load * se->avg.load_avg, |
7537 | cfs_rq_load_avg(cfs_rq) + 1); | |
68520796 VD |
7538 | cfs_rq = group_cfs_rq(se); |
7539 | cfs_rq->h_load = load; | |
7540 | cfs_rq->last_h_load_update = now; | |
7541 | } | |
9763b67f PZ |
7542 | } |
7543 | ||
367456c7 | 7544 | static unsigned long task_h_load(struct task_struct *p) |
230059de | 7545 | { |
367456c7 | 7546 | struct cfs_rq *cfs_rq = task_cfs_rq(p); |
230059de | 7547 | |
68520796 | 7548 | update_cfs_rq_h_load(cfs_rq); |
9d89c257 | 7549 | return div64_ul(p->se.avg.load_avg * cfs_rq->h_load, |
7ea241af | 7550 | cfs_rq_load_avg(cfs_rq) + 1); |
230059de PZ |
7551 | } |
7552 | #else | |
48a16753 | 7553 | static inline void update_blocked_averages(int cpu) |
9e3081ca | 7554 | { |
6c1d47c0 VG |
7555 | struct rq *rq = cpu_rq(cpu); |
7556 | struct cfs_rq *cfs_rq = &rq->cfs; | |
8a8c69c3 | 7557 | struct rq_flags rf; |
6c1d47c0 | 7558 | |
8a8c69c3 | 7559 | rq_lock_irqsave(rq, &rf); |
6c1d47c0 | 7560 | update_rq_clock(rq); |
3a123bbb | 7561 | update_cfs_rq_load_avg(cfs_rq_clock_task(cfs_rq), cfs_rq); |
e022e0d3 PZ |
7562 | #ifdef CONFIG_NO_HZ_COMMON |
7563 | rq->last_blocked_load_update_tick = jiffies; | |
1936c53c | 7564 | if (!cfs_rq_has_blocked(cfs_rq)) |
f643ea22 | 7565 | rq->has_blocked_load = 0; |
e022e0d3 | 7566 | #endif |
8a8c69c3 | 7567 | rq_unlock_irqrestore(rq, &rf); |
9e3081ca PZ |
7568 | } |
7569 | ||
367456c7 | 7570 | static unsigned long task_h_load(struct task_struct *p) |
1e3c88bd | 7571 | { |
9d89c257 | 7572 | return p->se.avg.load_avg; |
1e3c88bd | 7573 | } |
230059de | 7574 | #endif |
1e3c88bd | 7575 | |
1e3c88bd | 7576 | /********** Helpers for find_busiest_group ************************/ |
caeb178c RR |
7577 | |
7578 | enum group_type { | |
7579 | group_other = 0, | |
7580 | group_imbalanced, | |
7581 | group_overloaded, | |
7582 | }; | |
7583 | ||
1e3c88bd PZ |
7584 | /* |
7585 | * sg_lb_stats - stats of a sched_group required for load_balancing | |
7586 | */ | |
7587 | struct sg_lb_stats { | |
7588 | unsigned long avg_load; /*Avg load across the CPUs of the group */ | |
7589 | unsigned long group_load; /* Total load over the CPUs of the group */ | |
1e3c88bd | 7590 | unsigned long sum_weighted_load; /* Weighted load of group's tasks */ |
56cf515b | 7591 | unsigned long load_per_task; |
63b2ca30 | 7592 | unsigned long group_capacity; |
9e91d61d | 7593 | unsigned long group_util; /* Total utilization of the group */ |
147c5fc2 | 7594 | unsigned int sum_nr_running; /* Nr tasks running in the group */ |
147c5fc2 PZ |
7595 | unsigned int idle_cpus; |
7596 | unsigned int group_weight; | |
caeb178c | 7597 | enum group_type group_type; |
ea67821b | 7598 | int group_no_capacity; |
0ec8aa00 PZ |
7599 | #ifdef CONFIG_NUMA_BALANCING |
7600 | unsigned int nr_numa_running; | |
7601 | unsigned int nr_preferred_running; | |
7602 | #endif | |
1e3c88bd PZ |
7603 | }; |
7604 | ||
56cf515b JK |
7605 | /* |
7606 | * sd_lb_stats - Structure to store the statistics of a sched_domain | |
7607 | * during load balancing. | |
7608 | */ | |
7609 | struct sd_lb_stats { | |
7610 | struct sched_group *busiest; /* Busiest group in this sd */ | |
7611 | struct sched_group *local; /* Local group in this sd */ | |
90001d67 | 7612 | unsigned long total_running; |
56cf515b | 7613 | unsigned long total_load; /* Total load of all groups in sd */ |
63b2ca30 | 7614 | unsigned long total_capacity; /* Total capacity of all groups in sd */ |
56cf515b JK |
7615 | unsigned long avg_load; /* Average load across all groups in sd */ |
7616 | ||
56cf515b | 7617 | struct sg_lb_stats busiest_stat;/* Statistics of the busiest group */ |
147c5fc2 | 7618 | struct sg_lb_stats local_stat; /* Statistics of the local group */ |
56cf515b JK |
7619 | }; |
7620 | ||
147c5fc2 PZ |
7621 | static inline void init_sd_lb_stats(struct sd_lb_stats *sds) |
7622 | { | |
7623 | /* | |
7624 | * Skimp on the clearing to avoid duplicate work. We can avoid clearing | |
7625 | * local_stat because update_sg_lb_stats() does a full clear/assignment. | |
7626 | * We must however clear busiest_stat::avg_load because | |
7627 | * update_sd_pick_busiest() reads this before assignment. | |
7628 | */ | |
7629 | *sds = (struct sd_lb_stats){ | |
7630 | .busiest = NULL, | |
7631 | .local = NULL, | |
90001d67 | 7632 | .total_running = 0UL, |
147c5fc2 | 7633 | .total_load = 0UL, |
63b2ca30 | 7634 | .total_capacity = 0UL, |
147c5fc2 PZ |
7635 | .busiest_stat = { |
7636 | .avg_load = 0UL, | |
caeb178c RR |
7637 | .sum_nr_running = 0, |
7638 | .group_type = group_other, | |
147c5fc2 PZ |
7639 | }, |
7640 | }; | |
7641 | } | |
7642 | ||
1e3c88bd PZ |
7643 | /** |
7644 | * get_sd_load_idx - Obtain the load index for a given sched domain. | |
7645 | * @sd: The sched_domain whose load_idx is to be obtained. | |
ed1b7732 | 7646 | * @idle: The idle status of the CPU for whose sd load_idx is obtained. |
e69f6186 YB |
7647 | * |
7648 | * Return: The load index. | |
1e3c88bd PZ |
7649 | */ |
7650 | static inline int get_sd_load_idx(struct sched_domain *sd, | |
7651 | enum cpu_idle_type idle) | |
7652 | { | |
7653 | int load_idx; | |
7654 | ||
7655 | switch (idle) { | |
7656 | case CPU_NOT_IDLE: | |
7657 | load_idx = sd->busy_idx; | |
7658 | break; | |
7659 | ||
7660 | case CPU_NEWLY_IDLE: | |
7661 | load_idx = sd->newidle_idx; | |
7662 | break; | |
7663 | default: | |
7664 | load_idx = sd->idle_idx; | |
7665 | break; | |
7666 | } | |
7667 | ||
7668 | return load_idx; | |
7669 | } | |
7670 | ||
ced549fa | 7671 | static unsigned long scale_rt_capacity(int cpu) |
1e3c88bd PZ |
7672 | { |
7673 | struct rq *rq = cpu_rq(cpu); | |
b5b4860d | 7674 | u64 total, used, age_stamp, avg; |
cadefd3d | 7675 | s64 delta; |
1e3c88bd | 7676 | |
b654f7de PZ |
7677 | /* |
7678 | * Since we're reading these variables without serialization make sure | |
7679 | * we read them once before doing sanity checks on them. | |
7680 | */ | |
316c1608 JL |
7681 | age_stamp = READ_ONCE(rq->age_stamp); |
7682 | avg = READ_ONCE(rq->rt_avg); | |
cebde6d6 | 7683 | delta = __rq_clock_broken(rq) - age_stamp; |
b654f7de | 7684 | |
cadefd3d PZ |
7685 | if (unlikely(delta < 0)) |
7686 | delta = 0; | |
7687 | ||
7688 | total = sched_avg_period() + delta; | |
aa483808 | 7689 | |
b5b4860d | 7690 | used = div_u64(avg, total); |
1e3c88bd | 7691 | |
b5b4860d VG |
7692 | if (likely(used < SCHED_CAPACITY_SCALE)) |
7693 | return SCHED_CAPACITY_SCALE - used; | |
1e3c88bd | 7694 | |
b5b4860d | 7695 | return 1; |
1e3c88bd PZ |
7696 | } |
7697 | ||
ced549fa | 7698 | static void update_cpu_capacity(struct sched_domain *sd, int cpu) |
1e3c88bd | 7699 | { |
8cd5601c | 7700 | unsigned long capacity = arch_scale_cpu_capacity(sd, cpu); |
1e3c88bd PZ |
7701 | struct sched_group *sdg = sd->groups; |
7702 | ||
ca6d75e6 | 7703 | cpu_rq(cpu)->cpu_capacity_orig = capacity; |
9d5efe05 | 7704 | |
ced549fa | 7705 | capacity *= scale_rt_capacity(cpu); |
ca8ce3d0 | 7706 | capacity >>= SCHED_CAPACITY_SHIFT; |
1e3c88bd | 7707 | |
ced549fa NP |
7708 | if (!capacity) |
7709 | capacity = 1; | |
1e3c88bd | 7710 | |
ced549fa NP |
7711 | cpu_rq(cpu)->cpu_capacity = capacity; |
7712 | sdg->sgc->capacity = capacity; | |
bf475ce0 | 7713 | sdg->sgc->min_capacity = capacity; |
1e3c88bd PZ |
7714 | } |
7715 | ||
63b2ca30 | 7716 | void update_group_capacity(struct sched_domain *sd, int cpu) |
1e3c88bd PZ |
7717 | { |
7718 | struct sched_domain *child = sd->child; | |
7719 | struct sched_group *group, *sdg = sd->groups; | |
bf475ce0 | 7720 | unsigned long capacity, min_capacity; |
4ec4412e VG |
7721 | unsigned long interval; |
7722 | ||
7723 | interval = msecs_to_jiffies(sd->balance_interval); | |
7724 | interval = clamp(interval, 1UL, max_load_balance_interval); | |
63b2ca30 | 7725 | sdg->sgc->next_update = jiffies + interval; |
1e3c88bd PZ |
7726 | |
7727 | if (!child) { | |
ced549fa | 7728 | update_cpu_capacity(sd, cpu); |
1e3c88bd PZ |
7729 | return; |
7730 | } | |
7731 | ||
dc7ff76e | 7732 | capacity = 0; |
bf475ce0 | 7733 | min_capacity = ULONG_MAX; |
1e3c88bd | 7734 | |
74a5ce20 PZ |
7735 | if (child->flags & SD_OVERLAP) { |
7736 | /* | |
7737 | * SD_OVERLAP domains cannot assume that child groups | |
7738 | * span the current group. | |
7739 | */ | |
7740 | ||
ae4df9d6 | 7741 | for_each_cpu(cpu, sched_group_span(sdg)) { |
63b2ca30 | 7742 | struct sched_group_capacity *sgc; |
9abf24d4 | 7743 | struct rq *rq = cpu_rq(cpu); |
863bffc8 | 7744 | |
9abf24d4 | 7745 | /* |
63b2ca30 | 7746 | * build_sched_domains() -> init_sched_groups_capacity() |
9abf24d4 SD |
7747 | * gets here before we've attached the domains to the |
7748 | * runqueues. | |
7749 | * | |
ced549fa NP |
7750 | * Use capacity_of(), which is set irrespective of domains |
7751 | * in update_cpu_capacity(). | |
9abf24d4 | 7752 | * |
dc7ff76e | 7753 | * This avoids capacity from being 0 and |
9abf24d4 | 7754 | * causing divide-by-zero issues on boot. |
9abf24d4 SD |
7755 | */ |
7756 | if (unlikely(!rq->sd)) { | |
ced549fa | 7757 | capacity += capacity_of(cpu); |
bf475ce0 MR |
7758 | } else { |
7759 | sgc = rq->sd->groups->sgc; | |
7760 | capacity += sgc->capacity; | |
9abf24d4 | 7761 | } |
863bffc8 | 7762 | |
bf475ce0 | 7763 | min_capacity = min(capacity, min_capacity); |
863bffc8 | 7764 | } |
74a5ce20 PZ |
7765 | } else { |
7766 | /* | |
7767 | * !SD_OVERLAP domains can assume that child groups | |
7768 | * span the current group. | |
97a7142f | 7769 | */ |
74a5ce20 PZ |
7770 | |
7771 | group = child->groups; | |
7772 | do { | |
bf475ce0 MR |
7773 | struct sched_group_capacity *sgc = group->sgc; |
7774 | ||
7775 | capacity += sgc->capacity; | |
7776 | min_capacity = min(sgc->min_capacity, min_capacity); | |
74a5ce20 PZ |
7777 | group = group->next; |
7778 | } while (group != child->groups); | |
7779 | } | |
1e3c88bd | 7780 | |
63b2ca30 | 7781 | sdg->sgc->capacity = capacity; |
bf475ce0 | 7782 | sdg->sgc->min_capacity = min_capacity; |
1e3c88bd PZ |
7783 | } |
7784 | ||
9d5efe05 | 7785 | /* |
ea67821b VG |
7786 | * Check whether the capacity of the rq has been noticeably reduced by side |
7787 | * activity. The imbalance_pct is used for the threshold. | |
7788 | * Return true is the capacity is reduced | |
9d5efe05 SV |
7789 | */ |
7790 | static inline int | |
ea67821b | 7791 | check_cpu_capacity(struct rq *rq, struct sched_domain *sd) |
9d5efe05 | 7792 | { |
ea67821b VG |
7793 | return ((rq->cpu_capacity * sd->imbalance_pct) < |
7794 | (rq->cpu_capacity_orig * 100)); | |
9d5efe05 SV |
7795 | } |
7796 | ||
30ce5dab PZ |
7797 | /* |
7798 | * Group imbalance indicates (and tries to solve) the problem where balancing | |
0c98d344 | 7799 | * groups is inadequate due to ->cpus_allowed constraints. |
30ce5dab | 7800 | * |
97fb7a0a IM |
7801 | * Imagine a situation of two groups of 4 CPUs each and 4 tasks each with a |
7802 | * cpumask covering 1 CPU of the first group and 3 CPUs of the second group. | |
30ce5dab PZ |
7803 | * Something like: |
7804 | * | |
2b4d5b25 IM |
7805 | * { 0 1 2 3 } { 4 5 6 7 } |
7806 | * * * * * | |
30ce5dab PZ |
7807 | * |
7808 | * If we were to balance group-wise we'd place two tasks in the first group and | |
7809 | * two tasks in the second group. Clearly this is undesired as it will overload | |
97fb7a0a | 7810 | * cpu 3 and leave one of the CPUs in the second group unused. |
30ce5dab PZ |
7811 | * |
7812 | * The current solution to this issue is detecting the skew in the first group | |
6263322c PZ |
7813 | * by noticing the lower domain failed to reach balance and had difficulty |
7814 | * moving tasks due to affinity constraints. | |
30ce5dab PZ |
7815 | * |
7816 | * When this is so detected; this group becomes a candidate for busiest; see | |
ed1b7732 | 7817 | * update_sd_pick_busiest(). And calculate_imbalance() and |
6263322c | 7818 | * find_busiest_group() avoid some of the usual balance conditions to allow it |
30ce5dab PZ |
7819 | * to create an effective group imbalance. |
7820 | * | |
7821 | * This is a somewhat tricky proposition since the next run might not find the | |
7822 | * group imbalance and decide the groups need to be balanced again. A most | |
7823 | * subtle and fragile situation. | |
7824 | */ | |
7825 | ||
6263322c | 7826 | static inline int sg_imbalanced(struct sched_group *group) |
30ce5dab | 7827 | { |
63b2ca30 | 7828 | return group->sgc->imbalance; |
30ce5dab PZ |
7829 | } |
7830 | ||
b37d9316 | 7831 | /* |
ea67821b VG |
7832 | * group_has_capacity returns true if the group has spare capacity that could |
7833 | * be used by some tasks. | |
7834 | * We consider that a group has spare capacity if the * number of task is | |
9e91d61d DE |
7835 | * smaller than the number of CPUs or if the utilization is lower than the |
7836 | * available capacity for CFS tasks. | |
ea67821b VG |
7837 | * For the latter, we use a threshold to stabilize the state, to take into |
7838 | * account the variance of the tasks' load and to return true if the available | |
7839 | * capacity in meaningful for the load balancer. | |
7840 | * As an example, an available capacity of 1% can appear but it doesn't make | |
7841 | * any benefit for the load balance. | |
b37d9316 | 7842 | */ |
ea67821b VG |
7843 | static inline bool |
7844 | group_has_capacity(struct lb_env *env, struct sg_lb_stats *sgs) | |
b37d9316 | 7845 | { |
ea67821b VG |
7846 | if (sgs->sum_nr_running < sgs->group_weight) |
7847 | return true; | |
c61037e9 | 7848 | |
ea67821b | 7849 | if ((sgs->group_capacity * 100) > |
9e91d61d | 7850 | (sgs->group_util * env->sd->imbalance_pct)) |
ea67821b | 7851 | return true; |
b37d9316 | 7852 | |
ea67821b VG |
7853 | return false; |
7854 | } | |
7855 | ||
7856 | /* | |
7857 | * group_is_overloaded returns true if the group has more tasks than it can | |
7858 | * handle. | |
7859 | * group_is_overloaded is not equals to !group_has_capacity because a group | |
7860 | * with the exact right number of tasks, has no more spare capacity but is not | |
7861 | * overloaded so both group_has_capacity and group_is_overloaded return | |
7862 | * false. | |
7863 | */ | |
7864 | static inline bool | |
7865 | group_is_overloaded(struct lb_env *env, struct sg_lb_stats *sgs) | |
7866 | { | |
7867 | if (sgs->sum_nr_running <= sgs->group_weight) | |
7868 | return false; | |
b37d9316 | 7869 | |
ea67821b | 7870 | if ((sgs->group_capacity * 100) < |
9e91d61d | 7871 | (sgs->group_util * env->sd->imbalance_pct)) |
ea67821b | 7872 | return true; |
b37d9316 | 7873 | |
ea67821b | 7874 | return false; |
b37d9316 PZ |
7875 | } |
7876 | ||
9e0994c0 MR |
7877 | /* |
7878 | * group_smaller_cpu_capacity: Returns true if sched_group sg has smaller | |
7879 | * per-CPU capacity than sched_group ref. | |
7880 | */ | |
7881 | static inline bool | |
7882 | group_smaller_cpu_capacity(struct sched_group *sg, struct sched_group *ref) | |
7883 | { | |
7884 | return sg->sgc->min_capacity * capacity_margin < | |
7885 | ref->sgc->min_capacity * 1024; | |
7886 | } | |
7887 | ||
79a89f92 LY |
7888 | static inline enum |
7889 | group_type group_classify(struct sched_group *group, | |
7890 | struct sg_lb_stats *sgs) | |
caeb178c | 7891 | { |
ea67821b | 7892 | if (sgs->group_no_capacity) |
caeb178c RR |
7893 | return group_overloaded; |
7894 | ||
7895 | if (sg_imbalanced(group)) | |
7896 | return group_imbalanced; | |
7897 | ||
7898 | return group_other; | |
7899 | } | |
7900 | ||
63928384 | 7901 | static bool update_nohz_stats(struct rq *rq, bool force) |
e022e0d3 PZ |
7902 | { |
7903 | #ifdef CONFIG_NO_HZ_COMMON | |
7904 | unsigned int cpu = rq->cpu; | |
7905 | ||
f643ea22 VG |
7906 | if (!rq->has_blocked_load) |
7907 | return false; | |
7908 | ||
e022e0d3 | 7909 | if (!cpumask_test_cpu(cpu, nohz.idle_cpus_mask)) |
f643ea22 | 7910 | return false; |
e022e0d3 | 7911 | |
63928384 | 7912 | if (!force && !time_after(jiffies, rq->last_blocked_load_update_tick)) |
f643ea22 | 7913 | return true; |
e022e0d3 PZ |
7914 | |
7915 | update_blocked_averages(cpu); | |
f643ea22 VG |
7916 | |
7917 | return rq->has_blocked_load; | |
7918 | #else | |
7919 | return false; | |
e022e0d3 PZ |
7920 | #endif |
7921 | } | |
7922 | ||
1e3c88bd PZ |
7923 | /** |
7924 | * update_sg_lb_stats - Update sched_group's statistics for load balancing. | |
cd96891d | 7925 | * @env: The load balancing environment. |
1e3c88bd | 7926 | * @group: sched_group whose statistics are to be updated. |
1e3c88bd | 7927 | * @load_idx: Load index of sched_domain of this_cpu for load calc. |
1e3c88bd | 7928 | * @local_group: Does group contain this_cpu. |
1e3c88bd | 7929 | * @sgs: variable to hold the statistics for this group. |
cd3bd4e6 | 7930 | * @overload: Indicate more than one runnable task for any CPU. |
1e3c88bd | 7931 | */ |
bd939f45 PZ |
7932 | static inline void update_sg_lb_stats(struct lb_env *env, |
7933 | struct sched_group *group, int load_idx, | |
4486edd1 TC |
7934 | int local_group, struct sg_lb_stats *sgs, |
7935 | bool *overload) | |
1e3c88bd | 7936 | { |
30ce5dab | 7937 | unsigned long load; |
a426f99c | 7938 | int i, nr_running; |
1e3c88bd | 7939 | |
b72ff13c PZ |
7940 | memset(sgs, 0, sizeof(*sgs)); |
7941 | ||
ae4df9d6 | 7942 | for_each_cpu_and(i, sched_group_span(group), env->cpus) { |
1e3c88bd PZ |
7943 | struct rq *rq = cpu_rq(i); |
7944 | ||
63928384 | 7945 | if ((env->flags & LBF_NOHZ_STATS) && update_nohz_stats(rq, false)) |
f643ea22 | 7946 | env->flags |= LBF_NOHZ_AGAIN; |
e022e0d3 | 7947 | |
97fb7a0a | 7948 | /* Bias balancing toward CPUs of our domain: */ |
6263322c | 7949 | if (local_group) |
04f733b4 | 7950 | load = target_load(i, load_idx); |
6263322c | 7951 | else |
1e3c88bd | 7952 | load = source_load(i, load_idx); |
1e3c88bd PZ |
7953 | |
7954 | sgs->group_load += load; | |
9e91d61d | 7955 | sgs->group_util += cpu_util(i); |
65fdac08 | 7956 | sgs->sum_nr_running += rq->cfs.h_nr_running; |
4486edd1 | 7957 | |
a426f99c WL |
7958 | nr_running = rq->nr_running; |
7959 | if (nr_running > 1) | |
4486edd1 TC |
7960 | *overload = true; |
7961 | ||
0ec8aa00 PZ |
7962 | #ifdef CONFIG_NUMA_BALANCING |
7963 | sgs->nr_numa_running += rq->nr_numa_running; | |
7964 | sgs->nr_preferred_running += rq->nr_preferred_running; | |
7965 | #endif | |
c7132dd6 | 7966 | sgs->sum_weighted_load += weighted_cpuload(rq); |
a426f99c WL |
7967 | /* |
7968 | * No need to call idle_cpu() if nr_running is not 0 | |
7969 | */ | |
7970 | if (!nr_running && idle_cpu(i)) | |
aae6d3dd | 7971 | sgs->idle_cpus++; |
1e3c88bd PZ |
7972 | } |
7973 | ||
63b2ca30 NP |
7974 | /* Adjust by relative CPU capacity of the group */ |
7975 | sgs->group_capacity = group->sgc->capacity; | |
ca8ce3d0 | 7976 | sgs->avg_load = (sgs->group_load*SCHED_CAPACITY_SCALE) / sgs->group_capacity; |
1e3c88bd | 7977 | |
dd5feea1 | 7978 | if (sgs->sum_nr_running) |
38d0f770 | 7979 | sgs->load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running; |
1e3c88bd | 7980 | |
aae6d3dd | 7981 | sgs->group_weight = group->group_weight; |
b37d9316 | 7982 | |
ea67821b | 7983 | sgs->group_no_capacity = group_is_overloaded(env, sgs); |
79a89f92 | 7984 | sgs->group_type = group_classify(group, sgs); |
1e3c88bd PZ |
7985 | } |
7986 | ||
532cb4c4 MN |
7987 | /** |
7988 | * update_sd_pick_busiest - return 1 on busiest group | |
cd96891d | 7989 | * @env: The load balancing environment. |
532cb4c4 MN |
7990 | * @sds: sched_domain statistics |
7991 | * @sg: sched_group candidate to be checked for being the busiest | |
b6b12294 | 7992 | * @sgs: sched_group statistics |
532cb4c4 MN |
7993 | * |
7994 | * Determine if @sg is a busier group than the previously selected | |
7995 | * busiest group. | |
e69f6186 YB |
7996 | * |
7997 | * Return: %true if @sg is a busier group than the previously selected | |
7998 | * busiest group. %false otherwise. | |
532cb4c4 | 7999 | */ |
bd939f45 | 8000 | static bool update_sd_pick_busiest(struct lb_env *env, |
532cb4c4 MN |
8001 | struct sd_lb_stats *sds, |
8002 | struct sched_group *sg, | |
bd939f45 | 8003 | struct sg_lb_stats *sgs) |
532cb4c4 | 8004 | { |
caeb178c | 8005 | struct sg_lb_stats *busiest = &sds->busiest_stat; |
532cb4c4 | 8006 | |
caeb178c | 8007 | if (sgs->group_type > busiest->group_type) |
532cb4c4 MN |
8008 | return true; |
8009 | ||
caeb178c RR |
8010 | if (sgs->group_type < busiest->group_type) |
8011 | return false; | |
8012 | ||
8013 | if (sgs->avg_load <= busiest->avg_load) | |
8014 | return false; | |
8015 | ||
9e0994c0 MR |
8016 | if (!(env->sd->flags & SD_ASYM_CPUCAPACITY)) |
8017 | goto asym_packing; | |
8018 | ||
8019 | /* | |
8020 | * Candidate sg has no more than one task per CPU and | |
8021 | * has higher per-CPU capacity. Migrating tasks to less | |
8022 | * capable CPUs may harm throughput. Maximize throughput, | |
8023 | * power/energy consequences are not considered. | |
8024 | */ | |
8025 | if (sgs->sum_nr_running <= sgs->group_weight && | |
8026 | group_smaller_cpu_capacity(sds->local, sg)) | |
8027 | return false; | |
8028 | ||
8029 | asym_packing: | |
caeb178c RR |
8030 | /* This is the busiest node in its class. */ |
8031 | if (!(env->sd->flags & SD_ASYM_PACKING)) | |
532cb4c4 MN |
8032 | return true; |
8033 | ||
97fb7a0a | 8034 | /* No ASYM_PACKING if target CPU is already busy */ |
1f621e02 SD |
8035 | if (env->idle == CPU_NOT_IDLE) |
8036 | return true; | |
532cb4c4 | 8037 | /* |
afe06efd TC |
8038 | * ASYM_PACKING needs to move all the work to the highest |
8039 | * prority CPUs in the group, therefore mark all groups | |
8040 | * of lower priority than ourself as busy. | |
532cb4c4 | 8041 | */ |
afe06efd TC |
8042 | if (sgs->sum_nr_running && |
8043 | sched_asym_prefer(env->dst_cpu, sg->asym_prefer_cpu)) { | |
532cb4c4 MN |
8044 | if (!sds->busiest) |
8045 | return true; | |
8046 | ||
97fb7a0a | 8047 | /* Prefer to move from lowest priority CPU's work */ |
afe06efd TC |
8048 | if (sched_asym_prefer(sds->busiest->asym_prefer_cpu, |
8049 | sg->asym_prefer_cpu)) | |
532cb4c4 MN |
8050 | return true; |
8051 | } | |
8052 | ||
8053 | return false; | |
8054 | } | |
8055 | ||
0ec8aa00 PZ |
8056 | #ifdef CONFIG_NUMA_BALANCING |
8057 | static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs) | |
8058 | { | |
8059 | if (sgs->sum_nr_running > sgs->nr_numa_running) | |
8060 | return regular; | |
8061 | if (sgs->sum_nr_running > sgs->nr_preferred_running) | |
8062 | return remote; | |
8063 | return all; | |
8064 | } | |
8065 | ||
8066 | static inline enum fbq_type fbq_classify_rq(struct rq *rq) | |
8067 | { | |
8068 | if (rq->nr_running > rq->nr_numa_running) | |
8069 | return regular; | |
8070 | if (rq->nr_running > rq->nr_preferred_running) | |
8071 | return remote; | |
8072 | return all; | |
8073 | } | |
8074 | #else | |
8075 | static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs) | |
8076 | { | |
8077 | return all; | |
8078 | } | |
8079 | ||
8080 | static inline enum fbq_type fbq_classify_rq(struct rq *rq) | |
8081 | { | |
8082 | return regular; | |
8083 | } | |
8084 | #endif /* CONFIG_NUMA_BALANCING */ | |
8085 | ||
1e3c88bd | 8086 | /** |
461819ac | 8087 | * update_sd_lb_stats - Update sched_domain's statistics for load balancing. |
cd96891d | 8088 | * @env: The load balancing environment. |
1e3c88bd PZ |
8089 | * @sds: variable to hold the statistics for this sched_domain. |
8090 | */ | |
0ec8aa00 | 8091 | static inline void update_sd_lb_stats(struct lb_env *env, struct sd_lb_stats *sds) |
1e3c88bd | 8092 | { |
bd939f45 PZ |
8093 | struct sched_domain *child = env->sd->child; |
8094 | struct sched_group *sg = env->sd->groups; | |
05b40e05 | 8095 | struct sg_lb_stats *local = &sds->local_stat; |
56cf515b | 8096 | struct sg_lb_stats tmp_sgs; |
1e3c88bd | 8097 | int load_idx, prefer_sibling = 0; |
4486edd1 | 8098 | bool overload = false; |
1e3c88bd PZ |
8099 | |
8100 | if (child && child->flags & SD_PREFER_SIBLING) | |
8101 | prefer_sibling = 1; | |
8102 | ||
e022e0d3 | 8103 | #ifdef CONFIG_NO_HZ_COMMON |
f643ea22 | 8104 | if (env->idle == CPU_NEWLY_IDLE && READ_ONCE(nohz.has_blocked)) |
e022e0d3 | 8105 | env->flags |= LBF_NOHZ_STATS; |
e022e0d3 PZ |
8106 | #endif |
8107 | ||
bd939f45 | 8108 | load_idx = get_sd_load_idx(env->sd, env->idle); |
1e3c88bd PZ |
8109 | |
8110 | do { | |
56cf515b | 8111 | struct sg_lb_stats *sgs = &tmp_sgs; |
1e3c88bd PZ |
8112 | int local_group; |
8113 | ||
ae4df9d6 | 8114 | local_group = cpumask_test_cpu(env->dst_cpu, sched_group_span(sg)); |
56cf515b JK |
8115 | if (local_group) { |
8116 | sds->local = sg; | |
05b40e05 | 8117 | sgs = local; |
b72ff13c PZ |
8118 | |
8119 | if (env->idle != CPU_NEWLY_IDLE || | |
63b2ca30 NP |
8120 | time_after_eq(jiffies, sg->sgc->next_update)) |
8121 | update_group_capacity(env->sd, env->dst_cpu); | |
56cf515b | 8122 | } |
1e3c88bd | 8123 | |
4486edd1 TC |
8124 | update_sg_lb_stats(env, sg, load_idx, local_group, sgs, |
8125 | &overload); | |
1e3c88bd | 8126 | |
b72ff13c PZ |
8127 | if (local_group) |
8128 | goto next_group; | |
8129 | ||
1e3c88bd PZ |
8130 | /* |
8131 | * In case the child domain prefers tasks go to siblings | |
ea67821b | 8132 | * first, lower the sg capacity so that we'll try |
75dd321d NR |
8133 | * and move all the excess tasks away. We lower the capacity |
8134 | * of a group only if the local group has the capacity to fit | |
ea67821b VG |
8135 | * these excess tasks. The extra check prevents the case where |
8136 | * you always pull from the heaviest group when it is already | |
8137 | * under-utilized (possible with a large weight task outweighs | |
8138 | * the tasks on the system). | |
1e3c88bd | 8139 | */ |
b72ff13c | 8140 | if (prefer_sibling && sds->local && |
05b40e05 SD |
8141 | group_has_capacity(env, local) && |
8142 | (sgs->sum_nr_running > local->sum_nr_running + 1)) { | |
ea67821b | 8143 | sgs->group_no_capacity = 1; |
79a89f92 | 8144 | sgs->group_type = group_classify(sg, sgs); |
cb0b9f24 | 8145 | } |
1e3c88bd | 8146 | |
b72ff13c | 8147 | if (update_sd_pick_busiest(env, sds, sg, sgs)) { |
532cb4c4 | 8148 | sds->busiest = sg; |
56cf515b | 8149 | sds->busiest_stat = *sgs; |
1e3c88bd PZ |
8150 | } |
8151 | ||
b72ff13c PZ |
8152 | next_group: |
8153 | /* Now, start updating sd_lb_stats */ | |
90001d67 | 8154 | sds->total_running += sgs->sum_nr_running; |
b72ff13c | 8155 | sds->total_load += sgs->group_load; |
63b2ca30 | 8156 | sds->total_capacity += sgs->group_capacity; |
b72ff13c | 8157 | |
532cb4c4 | 8158 | sg = sg->next; |
bd939f45 | 8159 | } while (sg != env->sd->groups); |
0ec8aa00 | 8160 | |
f643ea22 VG |
8161 | #ifdef CONFIG_NO_HZ_COMMON |
8162 | if ((env->flags & LBF_NOHZ_AGAIN) && | |
8163 | cpumask_subset(nohz.idle_cpus_mask, sched_domain_span(env->sd))) { | |
8164 | ||
8165 | WRITE_ONCE(nohz.next_blocked, | |
8166 | jiffies + msecs_to_jiffies(LOAD_AVG_PERIOD)); | |
8167 | } | |
8168 | #endif | |
8169 | ||
0ec8aa00 PZ |
8170 | if (env->sd->flags & SD_NUMA) |
8171 | env->fbq_type = fbq_classify_group(&sds->busiest_stat); | |
4486edd1 TC |
8172 | |
8173 | if (!env->sd->parent) { | |
8174 | /* update overload indicator if we are at root domain */ | |
8175 | if (env->dst_rq->rd->overload != overload) | |
8176 | env->dst_rq->rd->overload = overload; | |
8177 | } | |
532cb4c4 MN |
8178 | } |
8179 | ||
532cb4c4 MN |
8180 | /** |
8181 | * check_asym_packing - Check to see if the group is packed into the | |
0ba42a59 | 8182 | * sched domain. |
532cb4c4 MN |
8183 | * |
8184 | * This is primarily intended to used at the sibling level. Some | |
8185 | * cores like POWER7 prefer to use lower numbered SMT threads. In the | |
8186 | * case of POWER7, it can move to lower SMT modes only when higher | |
8187 | * threads are idle. When in lower SMT modes, the threads will | |
8188 | * perform better since they share less core resources. Hence when we | |
8189 | * have idle threads, we want them to be the higher ones. | |
8190 | * | |
8191 | * This packing function is run on idle threads. It checks to see if | |
8192 | * the busiest CPU in this domain (core in the P7 case) has a higher | |
8193 | * CPU number than the packing function is being run on. Here we are | |
8194 | * assuming lower CPU number will be equivalent to lower a SMT thread | |
8195 | * number. | |
8196 | * | |
e69f6186 | 8197 | * Return: 1 when packing is required and a task should be moved to |
46123355 | 8198 | * this CPU. The amount of the imbalance is returned in env->imbalance. |
b6b12294 | 8199 | * |
cd96891d | 8200 | * @env: The load balancing environment. |
532cb4c4 | 8201 | * @sds: Statistics of the sched_domain which is to be packed |
532cb4c4 | 8202 | */ |
bd939f45 | 8203 | static int check_asym_packing(struct lb_env *env, struct sd_lb_stats *sds) |
532cb4c4 MN |
8204 | { |
8205 | int busiest_cpu; | |
8206 | ||
bd939f45 | 8207 | if (!(env->sd->flags & SD_ASYM_PACKING)) |
532cb4c4 MN |
8208 | return 0; |
8209 | ||
1f621e02 SD |
8210 | if (env->idle == CPU_NOT_IDLE) |
8211 | return 0; | |
8212 | ||
532cb4c4 MN |
8213 | if (!sds->busiest) |
8214 | return 0; | |
8215 | ||
afe06efd TC |
8216 | busiest_cpu = sds->busiest->asym_prefer_cpu; |
8217 | if (sched_asym_prefer(busiest_cpu, env->dst_cpu)) | |
532cb4c4 MN |
8218 | return 0; |
8219 | ||
bd939f45 | 8220 | env->imbalance = DIV_ROUND_CLOSEST( |
63b2ca30 | 8221 | sds->busiest_stat.avg_load * sds->busiest_stat.group_capacity, |
ca8ce3d0 | 8222 | SCHED_CAPACITY_SCALE); |
bd939f45 | 8223 | |
532cb4c4 | 8224 | return 1; |
1e3c88bd PZ |
8225 | } |
8226 | ||
8227 | /** | |
8228 | * fix_small_imbalance - Calculate the minor imbalance that exists | |
8229 | * amongst the groups of a sched_domain, during | |
8230 | * load balancing. | |
cd96891d | 8231 | * @env: The load balancing environment. |
1e3c88bd | 8232 | * @sds: Statistics of the sched_domain whose imbalance is to be calculated. |
1e3c88bd | 8233 | */ |
bd939f45 PZ |
8234 | static inline |
8235 | void fix_small_imbalance(struct lb_env *env, struct sd_lb_stats *sds) | |
1e3c88bd | 8236 | { |
63b2ca30 | 8237 | unsigned long tmp, capa_now = 0, capa_move = 0; |
1e3c88bd | 8238 | unsigned int imbn = 2; |
dd5feea1 | 8239 | unsigned long scaled_busy_load_per_task; |
56cf515b | 8240 | struct sg_lb_stats *local, *busiest; |
1e3c88bd | 8241 | |
56cf515b JK |
8242 | local = &sds->local_stat; |
8243 | busiest = &sds->busiest_stat; | |
1e3c88bd | 8244 | |
56cf515b JK |
8245 | if (!local->sum_nr_running) |
8246 | local->load_per_task = cpu_avg_load_per_task(env->dst_cpu); | |
8247 | else if (busiest->load_per_task > local->load_per_task) | |
8248 | imbn = 1; | |
dd5feea1 | 8249 | |
56cf515b | 8250 | scaled_busy_load_per_task = |
ca8ce3d0 | 8251 | (busiest->load_per_task * SCHED_CAPACITY_SCALE) / |
63b2ca30 | 8252 | busiest->group_capacity; |
56cf515b | 8253 | |
3029ede3 VD |
8254 | if (busiest->avg_load + scaled_busy_load_per_task >= |
8255 | local->avg_load + (scaled_busy_load_per_task * imbn)) { | |
56cf515b | 8256 | env->imbalance = busiest->load_per_task; |
1e3c88bd PZ |
8257 | return; |
8258 | } | |
8259 | ||
8260 | /* | |
8261 | * OK, we don't have enough imbalance to justify moving tasks, | |
ced549fa | 8262 | * however we may be able to increase total CPU capacity used by |
1e3c88bd PZ |
8263 | * moving them. |
8264 | */ | |
8265 | ||
63b2ca30 | 8266 | capa_now += busiest->group_capacity * |
56cf515b | 8267 | min(busiest->load_per_task, busiest->avg_load); |
63b2ca30 | 8268 | capa_now += local->group_capacity * |
56cf515b | 8269 | min(local->load_per_task, local->avg_load); |
ca8ce3d0 | 8270 | capa_now /= SCHED_CAPACITY_SCALE; |
1e3c88bd PZ |
8271 | |
8272 | /* Amount of load we'd subtract */ | |
a2cd4260 | 8273 | if (busiest->avg_load > scaled_busy_load_per_task) { |
63b2ca30 | 8274 | capa_move += busiest->group_capacity * |
56cf515b | 8275 | min(busiest->load_per_task, |
a2cd4260 | 8276 | busiest->avg_load - scaled_busy_load_per_task); |
56cf515b | 8277 | } |
1e3c88bd PZ |
8278 | |
8279 | /* Amount of load we'd add */ | |
63b2ca30 | 8280 | if (busiest->avg_load * busiest->group_capacity < |
ca8ce3d0 | 8281 | busiest->load_per_task * SCHED_CAPACITY_SCALE) { |
63b2ca30 NP |
8282 | tmp = (busiest->avg_load * busiest->group_capacity) / |
8283 | local->group_capacity; | |
56cf515b | 8284 | } else { |
ca8ce3d0 | 8285 | tmp = (busiest->load_per_task * SCHED_CAPACITY_SCALE) / |
63b2ca30 | 8286 | local->group_capacity; |
56cf515b | 8287 | } |
63b2ca30 | 8288 | capa_move += local->group_capacity * |
3ae11c90 | 8289 | min(local->load_per_task, local->avg_load + tmp); |
ca8ce3d0 | 8290 | capa_move /= SCHED_CAPACITY_SCALE; |
1e3c88bd PZ |
8291 | |
8292 | /* Move if we gain throughput */ | |
63b2ca30 | 8293 | if (capa_move > capa_now) |
56cf515b | 8294 | env->imbalance = busiest->load_per_task; |
1e3c88bd PZ |
8295 | } |
8296 | ||
8297 | /** | |
8298 | * calculate_imbalance - Calculate the amount of imbalance present within the | |
8299 | * groups of a given sched_domain during load balance. | |
bd939f45 | 8300 | * @env: load balance environment |
1e3c88bd | 8301 | * @sds: statistics of the sched_domain whose imbalance is to be calculated. |
1e3c88bd | 8302 | */ |
bd939f45 | 8303 | static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *sds) |
1e3c88bd | 8304 | { |
dd5feea1 | 8305 | unsigned long max_pull, load_above_capacity = ~0UL; |
56cf515b JK |
8306 | struct sg_lb_stats *local, *busiest; |
8307 | ||
8308 | local = &sds->local_stat; | |
56cf515b | 8309 | busiest = &sds->busiest_stat; |
dd5feea1 | 8310 | |
caeb178c | 8311 | if (busiest->group_type == group_imbalanced) { |
30ce5dab PZ |
8312 | /* |
8313 | * In the group_imb case we cannot rely on group-wide averages | |
97fb7a0a | 8314 | * to ensure CPU-load equilibrium, look at wider averages. XXX |
30ce5dab | 8315 | */ |
56cf515b JK |
8316 | busiest->load_per_task = |
8317 | min(busiest->load_per_task, sds->avg_load); | |
dd5feea1 SS |
8318 | } |
8319 | ||
1e3c88bd | 8320 | /* |
885e542c DE |
8321 | * Avg load of busiest sg can be less and avg load of local sg can |
8322 | * be greater than avg load across all sgs of sd because avg load | |
8323 | * factors in sg capacity and sgs with smaller group_type are | |
8324 | * skipped when updating the busiest sg: | |
1e3c88bd | 8325 | */ |
b1885550 VD |
8326 | if (busiest->avg_load <= sds->avg_load || |
8327 | local->avg_load >= sds->avg_load) { | |
bd939f45 PZ |
8328 | env->imbalance = 0; |
8329 | return fix_small_imbalance(env, sds); | |
1e3c88bd PZ |
8330 | } |
8331 | ||
9a5d9ba6 | 8332 | /* |
97fb7a0a | 8333 | * If there aren't any idle CPUs, avoid creating some. |
9a5d9ba6 PZ |
8334 | */ |
8335 | if (busiest->group_type == group_overloaded && | |
8336 | local->group_type == group_overloaded) { | |
1be0eb2a | 8337 | load_above_capacity = busiest->sum_nr_running * SCHED_CAPACITY_SCALE; |
cfa10334 | 8338 | if (load_above_capacity > busiest->group_capacity) { |
ea67821b | 8339 | load_above_capacity -= busiest->group_capacity; |
26656215 | 8340 | load_above_capacity *= scale_load_down(NICE_0_LOAD); |
cfa10334 MR |
8341 | load_above_capacity /= busiest->group_capacity; |
8342 | } else | |
ea67821b | 8343 | load_above_capacity = ~0UL; |
dd5feea1 SS |
8344 | } |
8345 | ||
8346 | /* | |
97fb7a0a | 8347 | * We're trying to get all the CPUs to the average_load, so we don't |
dd5feea1 | 8348 | * want to push ourselves above the average load, nor do we wish to |
97fb7a0a | 8349 | * reduce the max loaded CPU below the average load. At the same time, |
0a9b23ce DE |
8350 | * we also don't want to reduce the group load below the group |
8351 | * capacity. Thus we look for the minimum possible imbalance. | |
dd5feea1 | 8352 | */ |
30ce5dab | 8353 | max_pull = min(busiest->avg_load - sds->avg_load, load_above_capacity); |
1e3c88bd PZ |
8354 | |
8355 | /* How much load to actually move to equalise the imbalance */ | |
56cf515b | 8356 | env->imbalance = min( |
63b2ca30 NP |
8357 | max_pull * busiest->group_capacity, |
8358 | (sds->avg_load - local->avg_load) * local->group_capacity | |
ca8ce3d0 | 8359 | ) / SCHED_CAPACITY_SCALE; |
1e3c88bd PZ |
8360 | |
8361 | /* | |
8362 | * if *imbalance is less than the average load per runnable task | |
25985edc | 8363 | * there is no guarantee that any tasks will be moved so we'll have |
1e3c88bd PZ |
8364 | * a think about bumping its value to force at least one task to be |
8365 | * moved | |
8366 | */ | |
56cf515b | 8367 | if (env->imbalance < busiest->load_per_task) |
bd939f45 | 8368 | return fix_small_imbalance(env, sds); |
1e3c88bd | 8369 | } |
fab47622 | 8370 | |
1e3c88bd PZ |
8371 | /******* find_busiest_group() helpers end here *********************/ |
8372 | ||
8373 | /** | |
8374 | * find_busiest_group - Returns the busiest group within the sched_domain | |
0a9b23ce | 8375 | * if there is an imbalance. |
1e3c88bd PZ |
8376 | * |
8377 | * Also calculates the amount of weighted load which should be moved | |
8378 | * to restore balance. | |
8379 | * | |
cd96891d | 8380 | * @env: The load balancing environment. |
1e3c88bd | 8381 | * |
e69f6186 | 8382 | * Return: - The busiest group if imbalance exists. |
1e3c88bd | 8383 | */ |
56cf515b | 8384 | static struct sched_group *find_busiest_group(struct lb_env *env) |
1e3c88bd | 8385 | { |
56cf515b | 8386 | struct sg_lb_stats *local, *busiest; |
1e3c88bd PZ |
8387 | struct sd_lb_stats sds; |
8388 | ||
147c5fc2 | 8389 | init_sd_lb_stats(&sds); |
1e3c88bd PZ |
8390 | |
8391 | /* | |
8392 | * Compute the various statistics relavent for load balancing at | |
8393 | * this level. | |
8394 | */ | |
23f0d209 | 8395 | update_sd_lb_stats(env, &sds); |
56cf515b JK |
8396 | local = &sds.local_stat; |
8397 | busiest = &sds.busiest_stat; | |
1e3c88bd | 8398 | |
ea67821b | 8399 | /* ASYM feature bypasses nice load balance check */ |
1f621e02 | 8400 | if (check_asym_packing(env, &sds)) |
532cb4c4 MN |
8401 | return sds.busiest; |
8402 | ||
cc57aa8f | 8403 | /* There is no busy sibling group to pull tasks from */ |
56cf515b | 8404 | if (!sds.busiest || busiest->sum_nr_running == 0) |
1e3c88bd PZ |
8405 | goto out_balanced; |
8406 | ||
90001d67 | 8407 | /* XXX broken for overlapping NUMA groups */ |
ca8ce3d0 NP |
8408 | sds.avg_load = (SCHED_CAPACITY_SCALE * sds.total_load) |
8409 | / sds.total_capacity; | |
b0432d8f | 8410 | |
866ab43e PZ |
8411 | /* |
8412 | * If the busiest group is imbalanced the below checks don't | |
30ce5dab | 8413 | * work because they assume all things are equal, which typically |
866ab43e PZ |
8414 | * isn't true due to cpus_allowed constraints and the like. |
8415 | */ | |
caeb178c | 8416 | if (busiest->group_type == group_imbalanced) |
866ab43e PZ |
8417 | goto force_balance; |
8418 | ||
583ffd99 BJ |
8419 | /* |
8420 | * When dst_cpu is idle, prevent SMP nice and/or asymmetric group | |
8421 | * capacities from resulting in underutilization due to avg_load. | |
8422 | */ | |
8423 | if (env->idle != CPU_NOT_IDLE && group_has_capacity(env, local) && | |
ea67821b | 8424 | busiest->group_no_capacity) |
fab47622 NR |
8425 | goto force_balance; |
8426 | ||
cc57aa8f | 8427 | /* |
9c58c79a | 8428 | * If the local group is busier than the selected busiest group |
cc57aa8f PZ |
8429 | * don't try and pull any tasks. |
8430 | */ | |
56cf515b | 8431 | if (local->avg_load >= busiest->avg_load) |
1e3c88bd PZ |
8432 | goto out_balanced; |
8433 | ||
cc57aa8f PZ |
8434 | /* |
8435 | * Don't pull any tasks if this group is already above the domain | |
8436 | * average load. | |
8437 | */ | |
56cf515b | 8438 | if (local->avg_load >= sds.avg_load) |
1e3c88bd PZ |
8439 | goto out_balanced; |
8440 | ||
bd939f45 | 8441 | if (env->idle == CPU_IDLE) { |
aae6d3dd | 8442 | /* |
97fb7a0a | 8443 | * This CPU is idle. If the busiest group is not overloaded |
43f4d666 | 8444 | * and there is no imbalance between this and busiest group |
97fb7a0a | 8445 | * wrt idle CPUs, it is balanced. The imbalance becomes |
43f4d666 VG |
8446 | * significant if the diff is greater than 1 otherwise we |
8447 | * might end up to just move the imbalance on another group | |
aae6d3dd | 8448 | */ |
43f4d666 VG |
8449 | if ((busiest->group_type != group_overloaded) && |
8450 | (local->idle_cpus <= (busiest->idle_cpus + 1))) | |
aae6d3dd | 8451 | goto out_balanced; |
c186fafe PZ |
8452 | } else { |
8453 | /* | |
8454 | * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use | |
8455 | * imbalance_pct to be conservative. | |
8456 | */ | |
56cf515b JK |
8457 | if (100 * busiest->avg_load <= |
8458 | env->sd->imbalance_pct * local->avg_load) | |
c186fafe | 8459 | goto out_balanced; |
aae6d3dd | 8460 | } |
1e3c88bd | 8461 | |
fab47622 | 8462 | force_balance: |
1e3c88bd | 8463 | /* Looks like there is an imbalance. Compute it */ |
bd939f45 | 8464 | calculate_imbalance(env, &sds); |
1e3c88bd PZ |
8465 | return sds.busiest; |
8466 | ||
8467 | out_balanced: | |
bd939f45 | 8468 | env->imbalance = 0; |
1e3c88bd PZ |
8469 | return NULL; |
8470 | } | |
8471 | ||
8472 | /* | |
97fb7a0a | 8473 | * find_busiest_queue - find the busiest runqueue among the CPUs in the group. |
1e3c88bd | 8474 | */ |
bd939f45 | 8475 | static struct rq *find_busiest_queue(struct lb_env *env, |
b9403130 | 8476 | struct sched_group *group) |
1e3c88bd PZ |
8477 | { |
8478 | struct rq *busiest = NULL, *rq; | |
ced549fa | 8479 | unsigned long busiest_load = 0, busiest_capacity = 1; |
1e3c88bd PZ |
8480 | int i; |
8481 | ||
ae4df9d6 | 8482 | for_each_cpu_and(i, sched_group_span(group), env->cpus) { |
ea67821b | 8483 | unsigned long capacity, wl; |
0ec8aa00 PZ |
8484 | enum fbq_type rt; |
8485 | ||
8486 | rq = cpu_rq(i); | |
8487 | rt = fbq_classify_rq(rq); | |
1e3c88bd | 8488 | |
0ec8aa00 PZ |
8489 | /* |
8490 | * We classify groups/runqueues into three groups: | |
8491 | * - regular: there are !numa tasks | |
8492 | * - remote: there are numa tasks that run on the 'wrong' node | |
8493 | * - all: there is no distinction | |
8494 | * | |
8495 | * In order to avoid migrating ideally placed numa tasks, | |
8496 | * ignore those when there's better options. | |
8497 | * | |
8498 | * If we ignore the actual busiest queue to migrate another | |
8499 | * task, the next balance pass can still reduce the busiest | |
8500 | * queue by moving tasks around inside the node. | |
8501 | * | |
8502 | * If we cannot move enough load due to this classification | |
8503 | * the next pass will adjust the group classification and | |
8504 | * allow migration of more tasks. | |
8505 | * | |
8506 | * Both cases only affect the total convergence complexity. | |
8507 | */ | |
8508 | if (rt > env->fbq_type) | |
8509 | continue; | |
8510 | ||
ced549fa | 8511 | capacity = capacity_of(i); |
9d5efe05 | 8512 | |
c7132dd6 | 8513 | wl = weighted_cpuload(rq); |
1e3c88bd | 8514 | |
6e40f5bb TG |
8515 | /* |
8516 | * When comparing with imbalance, use weighted_cpuload() | |
97fb7a0a | 8517 | * which is not scaled with the CPU capacity. |
6e40f5bb | 8518 | */ |
ea67821b VG |
8519 | |
8520 | if (rq->nr_running == 1 && wl > env->imbalance && | |
8521 | !check_cpu_capacity(rq, env->sd)) | |
1e3c88bd PZ |
8522 | continue; |
8523 | ||
6e40f5bb | 8524 | /* |
97fb7a0a IM |
8525 | * For the load comparisons with the other CPU's, consider |
8526 | * the weighted_cpuload() scaled with the CPU capacity, so | |
8527 | * that the load can be moved away from the CPU that is | |
ced549fa | 8528 | * potentially running at a lower capacity. |
95a79b80 | 8529 | * |
ced549fa | 8530 | * Thus we're looking for max(wl_i / capacity_i), crosswise |
95a79b80 | 8531 | * multiplication to rid ourselves of the division works out |
ced549fa NP |
8532 | * to: wl_i * capacity_j > wl_j * capacity_i; where j is |
8533 | * our previous maximum. | |
6e40f5bb | 8534 | */ |
ced549fa | 8535 | if (wl * busiest_capacity > busiest_load * capacity) { |
95a79b80 | 8536 | busiest_load = wl; |
ced549fa | 8537 | busiest_capacity = capacity; |
1e3c88bd PZ |
8538 | busiest = rq; |
8539 | } | |
8540 | } | |
8541 | ||
8542 | return busiest; | |
8543 | } | |
8544 | ||
8545 | /* | |
8546 | * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but | |
8547 | * so long as it is large enough. | |
8548 | */ | |
8549 | #define MAX_PINNED_INTERVAL 512 | |
8550 | ||
bd939f45 | 8551 | static int need_active_balance(struct lb_env *env) |
1af3ed3d | 8552 | { |
bd939f45 PZ |
8553 | struct sched_domain *sd = env->sd; |
8554 | ||
8555 | if (env->idle == CPU_NEWLY_IDLE) { | |
532cb4c4 MN |
8556 | |
8557 | /* | |
8558 | * ASYM_PACKING needs to force migrate tasks from busy but | |
afe06efd TC |
8559 | * lower priority CPUs in order to pack all tasks in the |
8560 | * highest priority CPUs. | |
532cb4c4 | 8561 | */ |
afe06efd TC |
8562 | if ((sd->flags & SD_ASYM_PACKING) && |
8563 | sched_asym_prefer(env->dst_cpu, env->src_cpu)) | |
532cb4c4 | 8564 | return 1; |
1af3ed3d PZ |
8565 | } |
8566 | ||
1aaf90a4 VG |
8567 | /* |
8568 | * The dst_cpu is idle and the src_cpu CPU has only 1 CFS task. | |
8569 | * It's worth migrating the task if the src_cpu's capacity is reduced | |
8570 | * because of other sched_class or IRQs if more capacity stays | |
8571 | * available on dst_cpu. | |
8572 | */ | |
8573 | if ((env->idle != CPU_NOT_IDLE) && | |
8574 | (env->src_rq->cfs.h_nr_running == 1)) { | |
8575 | if ((check_cpu_capacity(env->src_rq, sd)) && | |
8576 | (capacity_of(env->src_cpu)*sd->imbalance_pct < capacity_of(env->dst_cpu)*100)) | |
8577 | return 1; | |
8578 | } | |
8579 | ||
1af3ed3d PZ |
8580 | return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2); |
8581 | } | |
8582 | ||
969c7921 TH |
8583 | static int active_load_balance_cpu_stop(void *data); |
8584 | ||
23f0d209 JK |
8585 | static int should_we_balance(struct lb_env *env) |
8586 | { | |
8587 | struct sched_group *sg = env->sd->groups; | |
23f0d209 JK |
8588 | int cpu, balance_cpu = -1; |
8589 | ||
024c9d2f PZ |
8590 | /* |
8591 | * Ensure the balancing environment is consistent; can happen | |
8592 | * when the softirq triggers 'during' hotplug. | |
8593 | */ | |
8594 | if (!cpumask_test_cpu(env->dst_cpu, env->cpus)) | |
8595 | return 0; | |
8596 | ||
23f0d209 | 8597 | /* |
97fb7a0a | 8598 | * In the newly idle case, we will allow all the CPUs |
23f0d209 JK |
8599 | * to do the newly idle load balance. |
8600 | */ | |
8601 | if (env->idle == CPU_NEWLY_IDLE) | |
8602 | return 1; | |
8603 | ||
97fb7a0a | 8604 | /* Try to find first idle CPU */ |
e5c14b1f | 8605 | for_each_cpu_and(cpu, group_balance_mask(sg), env->cpus) { |
af218122 | 8606 | if (!idle_cpu(cpu)) |
23f0d209 JK |
8607 | continue; |
8608 | ||
8609 | balance_cpu = cpu; | |
8610 | break; | |
8611 | } | |
8612 | ||
8613 | if (balance_cpu == -1) | |
8614 | balance_cpu = group_balance_cpu(sg); | |
8615 | ||
8616 | /* | |
97fb7a0a | 8617 | * First idle CPU or the first CPU(busiest) in this sched group |
23f0d209 JK |
8618 | * is eligible for doing load balancing at this and above domains. |
8619 | */ | |
b0cff9d8 | 8620 | return balance_cpu == env->dst_cpu; |
23f0d209 JK |
8621 | } |
8622 | ||
1e3c88bd PZ |
8623 | /* |
8624 | * Check this_cpu to ensure it is balanced within domain. Attempt to move | |
8625 | * tasks if there is an imbalance. | |
8626 | */ | |
8627 | static int load_balance(int this_cpu, struct rq *this_rq, | |
8628 | struct sched_domain *sd, enum cpu_idle_type idle, | |
23f0d209 | 8629 | int *continue_balancing) |
1e3c88bd | 8630 | { |
88b8dac0 | 8631 | int ld_moved, cur_ld_moved, active_balance = 0; |
6263322c | 8632 | struct sched_domain *sd_parent = sd->parent; |
1e3c88bd | 8633 | struct sched_group *group; |
1e3c88bd | 8634 | struct rq *busiest; |
8a8c69c3 | 8635 | struct rq_flags rf; |
4ba29684 | 8636 | struct cpumask *cpus = this_cpu_cpumask_var_ptr(load_balance_mask); |
1e3c88bd | 8637 | |
8e45cb54 PZ |
8638 | struct lb_env env = { |
8639 | .sd = sd, | |
ddcdf6e7 PZ |
8640 | .dst_cpu = this_cpu, |
8641 | .dst_rq = this_rq, | |
ae4df9d6 | 8642 | .dst_grpmask = sched_group_span(sd->groups), |
8e45cb54 | 8643 | .idle = idle, |
eb95308e | 8644 | .loop_break = sched_nr_migrate_break, |
b9403130 | 8645 | .cpus = cpus, |
0ec8aa00 | 8646 | .fbq_type = all, |
163122b7 | 8647 | .tasks = LIST_HEAD_INIT(env.tasks), |
8e45cb54 PZ |
8648 | }; |
8649 | ||
65a4433a | 8650 | cpumask_and(cpus, sched_domain_span(sd), cpu_active_mask); |
1e3c88bd | 8651 | |
ae92882e | 8652 | schedstat_inc(sd->lb_count[idle]); |
1e3c88bd PZ |
8653 | |
8654 | redo: | |
23f0d209 JK |
8655 | if (!should_we_balance(&env)) { |
8656 | *continue_balancing = 0; | |
1e3c88bd | 8657 | goto out_balanced; |
23f0d209 | 8658 | } |
1e3c88bd | 8659 | |
23f0d209 | 8660 | group = find_busiest_group(&env); |
1e3c88bd | 8661 | if (!group) { |
ae92882e | 8662 | schedstat_inc(sd->lb_nobusyg[idle]); |
1e3c88bd PZ |
8663 | goto out_balanced; |
8664 | } | |
8665 | ||
b9403130 | 8666 | busiest = find_busiest_queue(&env, group); |
1e3c88bd | 8667 | if (!busiest) { |
ae92882e | 8668 | schedstat_inc(sd->lb_nobusyq[idle]); |
1e3c88bd PZ |
8669 | goto out_balanced; |
8670 | } | |
8671 | ||
78feefc5 | 8672 | BUG_ON(busiest == env.dst_rq); |
1e3c88bd | 8673 | |
ae92882e | 8674 | schedstat_add(sd->lb_imbalance[idle], env.imbalance); |
1e3c88bd | 8675 | |
1aaf90a4 VG |
8676 | env.src_cpu = busiest->cpu; |
8677 | env.src_rq = busiest; | |
8678 | ||
1e3c88bd PZ |
8679 | ld_moved = 0; |
8680 | if (busiest->nr_running > 1) { | |
8681 | /* | |
8682 | * Attempt to move tasks. If find_busiest_group has found | |
8683 | * an imbalance but busiest->nr_running <= 1, the group is | |
8684 | * still unbalanced. ld_moved simply stays zero, so it is | |
8685 | * correctly treated as an imbalance. | |
8686 | */ | |
8e45cb54 | 8687 | env.flags |= LBF_ALL_PINNED; |
c82513e5 | 8688 | env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running); |
8e45cb54 | 8689 | |
5d6523eb | 8690 | more_balance: |
8a8c69c3 | 8691 | rq_lock_irqsave(busiest, &rf); |
3bed5e21 | 8692 | update_rq_clock(busiest); |
88b8dac0 SV |
8693 | |
8694 | /* | |
8695 | * cur_ld_moved - load moved in current iteration | |
8696 | * ld_moved - cumulative load moved across iterations | |
8697 | */ | |
163122b7 | 8698 | cur_ld_moved = detach_tasks(&env); |
1e3c88bd PZ |
8699 | |
8700 | /* | |
163122b7 KT |
8701 | * We've detached some tasks from busiest_rq. Every |
8702 | * task is masked "TASK_ON_RQ_MIGRATING", so we can safely | |
8703 | * unlock busiest->lock, and we are able to be sure | |
8704 | * that nobody can manipulate the tasks in parallel. | |
8705 | * See task_rq_lock() family for the details. | |
1e3c88bd | 8706 | */ |
163122b7 | 8707 | |
8a8c69c3 | 8708 | rq_unlock(busiest, &rf); |
163122b7 KT |
8709 | |
8710 | if (cur_ld_moved) { | |
8711 | attach_tasks(&env); | |
8712 | ld_moved += cur_ld_moved; | |
8713 | } | |
8714 | ||
8a8c69c3 | 8715 | local_irq_restore(rf.flags); |
88b8dac0 | 8716 | |
f1cd0858 JK |
8717 | if (env.flags & LBF_NEED_BREAK) { |
8718 | env.flags &= ~LBF_NEED_BREAK; | |
8719 | goto more_balance; | |
8720 | } | |
8721 | ||
88b8dac0 SV |
8722 | /* |
8723 | * Revisit (affine) tasks on src_cpu that couldn't be moved to | |
8724 | * us and move them to an alternate dst_cpu in our sched_group | |
8725 | * where they can run. The upper limit on how many times we | |
97fb7a0a | 8726 | * iterate on same src_cpu is dependent on number of CPUs in our |
88b8dac0 SV |
8727 | * sched_group. |
8728 | * | |
8729 | * This changes load balance semantics a bit on who can move | |
8730 | * load to a given_cpu. In addition to the given_cpu itself | |
8731 | * (or a ilb_cpu acting on its behalf where given_cpu is | |
8732 | * nohz-idle), we now have balance_cpu in a position to move | |
8733 | * load to given_cpu. In rare situations, this may cause | |
8734 | * conflicts (balance_cpu and given_cpu/ilb_cpu deciding | |
8735 | * _independently_ and at _same_ time to move some load to | |
8736 | * given_cpu) causing exceess load to be moved to given_cpu. | |
8737 | * This however should not happen so much in practice and | |
8738 | * moreover subsequent load balance cycles should correct the | |
8739 | * excess load moved. | |
8740 | */ | |
6263322c | 8741 | if ((env.flags & LBF_DST_PINNED) && env.imbalance > 0) { |
88b8dac0 | 8742 | |
97fb7a0a | 8743 | /* Prevent to re-select dst_cpu via env's CPUs */ |
7aff2e3a VD |
8744 | cpumask_clear_cpu(env.dst_cpu, env.cpus); |
8745 | ||
78feefc5 | 8746 | env.dst_rq = cpu_rq(env.new_dst_cpu); |
88b8dac0 | 8747 | env.dst_cpu = env.new_dst_cpu; |
6263322c | 8748 | env.flags &= ~LBF_DST_PINNED; |
88b8dac0 SV |
8749 | env.loop = 0; |
8750 | env.loop_break = sched_nr_migrate_break; | |
e02e60c1 | 8751 | |
88b8dac0 SV |
8752 | /* |
8753 | * Go back to "more_balance" rather than "redo" since we | |
8754 | * need to continue with same src_cpu. | |
8755 | */ | |
8756 | goto more_balance; | |
8757 | } | |
1e3c88bd | 8758 | |
6263322c PZ |
8759 | /* |
8760 | * We failed to reach balance because of affinity. | |
8761 | */ | |
8762 | if (sd_parent) { | |
63b2ca30 | 8763 | int *group_imbalance = &sd_parent->groups->sgc->imbalance; |
6263322c | 8764 | |
afdeee05 | 8765 | if ((env.flags & LBF_SOME_PINNED) && env.imbalance > 0) |
6263322c | 8766 | *group_imbalance = 1; |
6263322c PZ |
8767 | } |
8768 | ||
1e3c88bd | 8769 | /* All tasks on this runqueue were pinned by CPU affinity */ |
8e45cb54 | 8770 | if (unlikely(env.flags & LBF_ALL_PINNED)) { |
1e3c88bd | 8771 | cpumask_clear_cpu(cpu_of(busiest), cpus); |
65a4433a JH |
8772 | /* |
8773 | * Attempting to continue load balancing at the current | |
8774 | * sched_domain level only makes sense if there are | |
8775 | * active CPUs remaining as possible busiest CPUs to | |
8776 | * pull load from which are not contained within the | |
8777 | * destination group that is receiving any migrated | |
8778 | * load. | |
8779 | */ | |
8780 | if (!cpumask_subset(cpus, env.dst_grpmask)) { | |
bbf18b19 PN |
8781 | env.loop = 0; |
8782 | env.loop_break = sched_nr_migrate_break; | |
1e3c88bd | 8783 | goto redo; |
bbf18b19 | 8784 | } |
afdeee05 | 8785 | goto out_all_pinned; |
1e3c88bd PZ |
8786 | } |
8787 | } | |
8788 | ||
8789 | if (!ld_moved) { | |
ae92882e | 8790 | schedstat_inc(sd->lb_failed[idle]); |
58b26c4c VP |
8791 | /* |
8792 | * Increment the failure counter only on periodic balance. | |
8793 | * We do not want newidle balance, which can be very | |
8794 | * frequent, pollute the failure counter causing | |
8795 | * excessive cache_hot migrations and active balances. | |
8796 | */ | |
8797 | if (idle != CPU_NEWLY_IDLE) | |
8798 | sd->nr_balance_failed++; | |
1e3c88bd | 8799 | |
bd939f45 | 8800 | if (need_active_balance(&env)) { |
8a8c69c3 PZ |
8801 | unsigned long flags; |
8802 | ||
1e3c88bd PZ |
8803 | raw_spin_lock_irqsave(&busiest->lock, flags); |
8804 | ||
97fb7a0a IM |
8805 | /* |
8806 | * Don't kick the active_load_balance_cpu_stop, | |
8807 | * if the curr task on busiest CPU can't be | |
8808 | * moved to this_cpu: | |
1e3c88bd | 8809 | */ |
0c98d344 | 8810 | if (!cpumask_test_cpu(this_cpu, &busiest->curr->cpus_allowed)) { |
1e3c88bd PZ |
8811 | raw_spin_unlock_irqrestore(&busiest->lock, |
8812 | flags); | |
8e45cb54 | 8813 | env.flags |= LBF_ALL_PINNED; |
1e3c88bd PZ |
8814 | goto out_one_pinned; |
8815 | } | |
8816 | ||
969c7921 TH |
8817 | /* |
8818 | * ->active_balance synchronizes accesses to | |
8819 | * ->active_balance_work. Once set, it's cleared | |
8820 | * only after active load balance is finished. | |
8821 | */ | |
1e3c88bd PZ |
8822 | if (!busiest->active_balance) { |
8823 | busiest->active_balance = 1; | |
8824 | busiest->push_cpu = this_cpu; | |
8825 | active_balance = 1; | |
8826 | } | |
8827 | raw_spin_unlock_irqrestore(&busiest->lock, flags); | |
969c7921 | 8828 | |
bd939f45 | 8829 | if (active_balance) { |
969c7921 TH |
8830 | stop_one_cpu_nowait(cpu_of(busiest), |
8831 | active_load_balance_cpu_stop, busiest, | |
8832 | &busiest->active_balance_work); | |
bd939f45 | 8833 | } |
1e3c88bd | 8834 | |
d02c0711 | 8835 | /* We've kicked active balancing, force task migration. */ |
1e3c88bd PZ |
8836 | sd->nr_balance_failed = sd->cache_nice_tries+1; |
8837 | } | |
8838 | } else | |
8839 | sd->nr_balance_failed = 0; | |
8840 | ||
8841 | if (likely(!active_balance)) { | |
8842 | /* We were unbalanced, so reset the balancing interval */ | |
8843 | sd->balance_interval = sd->min_interval; | |
8844 | } else { | |
8845 | /* | |
8846 | * If we've begun active balancing, start to back off. This | |
8847 | * case may not be covered by the all_pinned logic if there | |
8848 | * is only 1 task on the busy runqueue (because we don't call | |
163122b7 | 8849 | * detach_tasks). |
1e3c88bd PZ |
8850 | */ |
8851 | if (sd->balance_interval < sd->max_interval) | |
8852 | sd->balance_interval *= 2; | |
8853 | } | |
8854 | ||
1e3c88bd PZ |
8855 | goto out; |
8856 | ||
8857 | out_balanced: | |
afdeee05 VG |
8858 | /* |
8859 | * We reach balance although we may have faced some affinity | |
8860 | * constraints. Clear the imbalance flag if it was set. | |
8861 | */ | |
8862 | if (sd_parent) { | |
8863 | int *group_imbalance = &sd_parent->groups->sgc->imbalance; | |
8864 | ||
8865 | if (*group_imbalance) | |
8866 | *group_imbalance = 0; | |
8867 | } | |
8868 | ||
8869 | out_all_pinned: | |
8870 | /* | |
8871 | * We reach balance because all tasks are pinned at this level so | |
8872 | * we can't migrate them. Let the imbalance flag set so parent level | |
8873 | * can try to migrate them. | |
8874 | */ | |
ae92882e | 8875 | schedstat_inc(sd->lb_balanced[idle]); |
1e3c88bd PZ |
8876 | |
8877 | sd->nr_balance_failed = 0; | |
8878 | ||
8879 | out_one_pinned: | |
8880 | /* tune up the balancing interval */ | |
8e45cb54 | 8881 | if (((env.flags & LBF_ALL_PINNED) && |
5b54b56b | 8882 | sd->balance_interval < MAX_PINNED_INTERVAL) || |
1e3c88bd PZ |
8883 | (sd->balance_interval < sd->max_interval)) |
8884 | sd->balance_interval *= 2; | |
8885 | ||
46e49b38 | 8886 | ld_moved = 0; |
1e3c88bd | 8887 | out: |
1e3c88bd PZ |
8888 | return ld_moved; |
8889 | } | |
8890 | ||
52a08ef1 JL |
8891 | static inline unsigned long |
8892 | get_sd_balance_interval(struct sched_domain *sd, int cpu_busy) | |
8893 | { | |
8894 | unsigned long interval = sd->balance_interval; | |
8895 | ||
8896 | if (cpu_busy) | |
8897 | interval *= sd->busy_factor; | |
8898 | ||
8899 | /* scale ms to jiffies */ | |
8900 | interval = msecs_to_jiffies(interval); | |
8901 | interval = clamp(interval, 1UL, max_load_balance_interval); | |
8902 | ||
8903 | return interval; | |
8904 | } | |
8905 | ||
8906 | static inline void | |
31851a98 | 8907 | update_next_balance(struct sched_domain *sd, unsigned long *next_balance) |
52a08ef1 JL |
8908 | { |
8909 | unsigned long interval, next; | |
8910 | ||
31851a98 LY |
8911 | /* used by idle balance, so cpu_busy = 0 */ |
8912 | interval = get_sd_balance_interval(sd, 0); | |
52a08ef1 JL |
8913 | next = sd->last_balance + interval; |
8914 | ||
8915 | if (time_after(*next_balance, next)) | |
8916 | *next_balance = next; | |
8917 | } | |
8918 | ||
1e3c88bd | 8919 | /* |
97fb7a0a | 8920 | * active_load_balance_cpu_stop is run by the CPU stopper. It pushes |
969c7921 TH |
8921 | * running tasks off the busiest CPU onto idle CPUs. It requires at |
8922 | * least 1 task to be running on each physical CPU where possible, and | |
8923 | * avoids physical / logical imbalances. | |
1e3c88bd | 8924 | */ |
969c7921 | 8925 | static int active_load_balance_cpu_stop(void *data) |
1e3c88bd | 8926 | { |
969c7921 TH |
8927 | struct rq *busiest_rq = data; |
8928 | int busiest_cpu = cpu_of(busiest_rq); | |
1e3c88bd | 8929 | int target_cpu = busiest_rq->push_cpu; |
969c7921 | 8930 | struct rq *target_rq = cpu_rq(target_cpu); |
1e3c88bd | 8931 | struct sched_domain *sd; |
e5673f28 | 8932 | struct task_struct *p = NULL; |
8a8c69c3 | 8933 | struct rq_flags rf; |
969c7921 | 8934 | |
8a8c69c3 | 8935 | rq_lock_irq(busiest_rq, &rf); |
edd8e41d PZ |
8936 | /* |
8937 | * Between queueing the stop-work and running it is a hole in which | |
8938 | * CPUs can become inactive. We should not move tasks from or to | |
8939 | * inactive CPUs. | |
8940 | */ | |
8941 | if (!cpu_active(busiest_cpu) || !cpu_active(target_cpu)) | |
8942 | goto out_unlock; | |
969c7921 | 8943 | |
97fb7a0a | 8944 | /* Make sure the requested CPU hasn't gone down in the meantime: */ |
969c7921 TH |
8945 | if (unlikely(busiest_cpu != smp_processor_id() || |
8946 | !busiest_rq->active_balance)) | |
8947 | goto out_unlock; | |
1e3c88bd PZ |
8948 | |
8949 | /* Is there any task to move? */ | |
8950 | if (busiest_rq->nr_running <= 1) | |
969c7921 | 8951 | goto out_unlock; |
1e3c88bd PZ |
8952 | |
8953 | /* | |
8954 | * This condition is "impossible", if it occurs | |
8955 | * we need to fix it. Originally reported by | |
97fb7a0a | 8956 | * Bjorn Helgaas on a 128-CPU setup. |
1e3c88bd PZ |
8957 | */ |
8958 | BUG_ON(busiest_rq == target_rq); | |
8959 | ||
1e3c88bd | 8960 | /* Search for an sd spanning us and the target CPU. */ |
dce840a0 | 8961 | rcu_read_lock(); |
1e3c88bd PZ |
8962 | for_each_domain(target_cpu, sd) { |
8963 | if ((sd->flags & SD_LOAD_BALANCE) && | |
8964 | cpumask_test_cpu(busiest_cpu, sched_domain_span(sd))) | |
8965 | break; | |
8966 | } | |
8967 | ||
8968 | if (likely(sd)) { | |
8e45cb54 PZ |
8969 | struct lb_env env = { |
8970 | .sd = sd, | |
ddcdf6e7 PZ |
8971 | .dst_cpu = target_cpu, |
8972 | .dst_rq = target_rq, | |
8973 | .src_cpu = busiest_rq->cpu, | |
8974 | .src_rq = busiest_rq, | |
8e45cb54 | 8975 | .idle = CPU_IDLE, |
65a4433a JH |
8976 | /* |
8977 | * can_migrate_task() doesn't need to compute new_dst_cpu | |
8978 | * for active balancing. Since we have CPU_IDLE, but no | |
8979 | * @dst_grpmask we need to make that test go away with lying | |
8980 | * about DST_PINNED. | |
8981 | */ | |
8982 | .flags = LBF_DST_PINNED, | |
8e45cb54 PZ |
8983 | }; |
8984 | ||
ae92882e | 8985 | schedstat_inc(sd->alb_count); |
3bed5e21 | 8986 | update_rq_clock(busiest_rq); |
1e3c88bd | 8987 | |
e5673f28 | 8988 | p = detach_one_task(&env); |
d02c0711 | 8989 | if (p) { |
ae92882e | 8990 | schedstat_inc(sd->alb_pushed); |
d02c0711 SD |
8991 | /* Active balancing done, reset the failure counter. */ |
8992 | sd->nr_balance_failed = 0; | |
8993 | } else { | |
ae92882e | 8994 | schedstat_inc(sd->alb_failed); |
d02c0711 | 8995 | } |
1e3c88bd | 8996 | } |
dce840a0 | 8997 | rcu_read_unlock(); |
969c7921 TH |
8998 | out_unlock: |
8999 | busiest_rq->active_balance = 0; | |
8a8c69c3 | 9000 | rq_unlock(busiest_rq, &rf); |
e5673f28 KT |
9001 | |
9002 | if (p) | |
9003 | attach_one_task(target_rq, p); | |
9004 | ||
9005 | local_irq_enable(); | |
9006 | ||
969c7921 | 9007 | return 0; |
1e3c88bd PZ |
9008 | } |
9009 | ||
af3fe03c PZ |
9010 | static DEFINE_SPINLOCK(balancing); |
9011 | ||
9012 | /* | |
9013 | * Scale the max load_balance interval with the number of CPUs in the system. | |
9014 | * This trades load-balance latency on larger machines for less cross talk. | |
9015 | */ | |
9016 | void update_max_interval(void) | |
9017 | { | |
9018 | max_load_balance_interval = HZ*num_online_cpus()/10; | |
9019 | } | |
9020 | ||
9021 | /* | |
9022 | * It checks each scheduling domain to see if it is due to be balanced, | |
9023 | * and initiates a balancing operation if so. | |
9024 | * | |
9025 | * Balancing parameters are set up in init_sched_domains. | |
9026 | */ | |
9027 | static void rebalance_domains(struct rq *rq, enum cpu_idle_type idle) | |
9028 | { | |
9029 | int continue_balancing = 1; | |
9030 | int cpu = rq->cpu; | |
9031 | unsigned long interval; | |
9032 | struct sched_domain *sd; | |
9033 | /* Earliest time when we have to do rebalance again */ | |
9034 | unsigned long next_balance = jiffies + 60*HZ; | |
9035 | int update_next_balance = 0; | |
9036 | int need_serialize, need_decay = 0; | |
9037 | u64 max_cost = 0; | |
9038 | ||
9039 | rcu_read_lock(); | |
9040 | for_each_domain(cpu, sd) { | |
9041 | /* | |
9042 | * Decay the newidle max times here because this is a regular | |
9043 | * visit to all the domains. Decay ~1% per second. | |
9044 | */ | |
9045 | if (time_after(jiffies, sd->next_decay_max_lb_cost)) { | |
9046 | sd->max_newidle_lb_cost = | |
9047 | (sd->max_newidle_lb_cost * 253) / 256; | |
9048 | sd->next_decay_max_lb_cost = jiffies + HZ; | |
9049 | need_decay = 1; | |
9050 | } | |
9051 | max_cost += sd->max_newidle_lb_cost; | |
9052 | ||
9053 | if (!(sd->flags & SD_LOAD_BALANCE)) | |
9054 | continue; | |
9055 | ||
9056 | /* | |
9057 | * Stop the load balance at this level. There is another | |
9058 | * CPU in our sched group which is doing load balancing more | |
9059 | * actively. | |
9060 | */ | |
9061 | if (!continue_balancing) { | |
9062 | if (need_decay) | |
9063 | continue; | |
9064 | break; | |
9065 | } | |
9066 | ||
9067 | interval = get_sd_balance_interval(sd, idle != CPU_IDLE); | |
9068 | ||
9069 | need_serialize = sd->flags & SD_SERIALIZE; | |
9070 | if (need_serialize) { | |
9071 | if (!spin_trylock(&balancing)) | |
9072 | goto out; | |
9073 | } | |
9074 | ||
9075 | if (time_after_eq(jiffies, sd->last_balance + interval)) { | |
9076 | if (load_balance(cpu, rq, sd, idle, &continue_balancing)) { | |
9077 | /* | |
9078 | * The LBF_DST_PINNED logic could have changed | |
9079 | * env->dst_cpu, so we can't know our idle | |
9080 | * state even if we migrated tasks. Update it. | |
9081 | */ | |
9082 | idle = idle_cpu(cpu) ? CPU_IDLE : CPU_NOT_IDLE; | |
9083 | } | |
9084 | sd->last_balance = jiffies; | |
9085 | interval = get_sd_balance_interval(sd, idle != CPU_IDLE); | |
9086 | } | |
9087 | if (need_serialize) | |
9088 | spin_unlock(&balancing); | |
9089 | out: | |
9090 | if (time_after(next_balance, sd->last_balance + interval)) { | |
9091 | next_balance = sd->last_balance + interval; | |
9092 | update_next_balance = 1; | |
9093 | } | |
9094 | } | |
9095 | if (need_decay) { | |
9096 | /* | |
9097 | * Ensure the rq-wide value also decays but keep it at a | |
9098 | * reasonable floor to avoid funnies with rq->avg_idle. | |
9099 | */ | |
9100 | rq->max_idle_balance_cost = | |
9101 | max((u64)sysctl_sched_migration_cost, max_cost); | |
9102 | } | |
9103 | rcu_read_unlock(); | |
9104 | ||
9105 | /* | |
9106 | * next_balance will be updated only when there is a need. | |
9107 | * When the cpu is attached to null domain for ex, it will not be | |
9108 | * updated. | |
9109 | */ | |
9110 | if (likely(update_next_balance)) { | |
9111 | rq->next_balance = next_balance; | |
9112 | ||
9113 | #ifdef CONFIG_NO_HZ_COMMON | |
9114 | /* | |
9115 | * If this CPU has been elected to perform the nohz idle | |
9116 | * balance. Other idle CPUs have already rebalanced with | |
9117 | * nohz_idle_balance() and nohz.next_balance has been | |
9118 | * updated accordingly. This CPU is now running the idle load | |
9119 | * balance for itself and we need to update the | |
9120 | * nohz.next_balance accordingly. | |
9121 | */ | |
9122 | if ((idle == CPU_IDLE) && time_after(nohz.next_balance, rq->next_balance)) | |
9123 | nohz.next_balance = rq->next_balance; | |
9124 | #endif | |
9125 | } | |
9126 | } | |
9127 | ||
d987fc7f MG |
9128 | static inline int on_null_domain(struct rq *rq) |
9129 | { | |
9130 | return unlikely(!rcu_dereference_sched(rq->sd)); | |
9131 | } | |
9132 | ||
3451d024 | 9133 | #ifdef CONFIG_NO_HZ_COMMON |
83cd4fe2 VP |
9134 | /* |
9135 | * idle load balancing details | |
83cd4fe2 VP |
9136 | * - When one of the busy CPUs notice that there may be an idle rebalancing |
9137 | * needed, they will kick the idle load balancer, which then does idle | |
9138 | * load balancing for all the idle CPUs. | |
9139 | */ | |
1e3c88bd | 9140 | |
3dd0337d | 9141 | static inline int find_new_ilb(void) |
1e3c88bd | 9142 | { |
0b005cf5 | 9143 | int ilb = cpumask_first(nohz.idle_cpus_mask); |
1e3c88bd | 9144 | |
786d6dc7 SS |
9145 | if (ilb < nr_cpu_ids && idle_cpu(ilb)) |
9146 | return ilb; | |
9147 | ||
9148 | return nr_cpu_ids; | |
1e3c88bd | 9149 | } |
1e3c88bd | 9150 | |
83cd4fe2 VP |
9151 | /* |
9152 | * Kick a CPU to do the nohz balancing, if it is time for it. We pick the | |
9153 | * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle | |
9154 | * CPU (if there is one). | |
9155 | */ | |
a4064fb6 | 9156 | static void kick_ilb(unsigned int flags) |
83cd4fe2 VP |
9157 | { |
9158 | int ilb_cpu; | |
9159 | ||
9160 | nohz.next_balance++; | |
9161 | ||
3dd0337d | 9162 | ilb_cpu = find_new_ilb(); |
83cd4fe2 | 9163 | |
0b005cf5 SS |
9164 | if (ilb_cpu >= nr_cpu_ids) |
9165 | return; | |
83cd4fe2 | 9166 | |
a4064fb6 | 9167 | flags = atomic_fetch_or(flags, nohz_flags(ilb_cpu)); |
b7031a02 | 9168 | if (flags & NOHZ_KICK_MASK) |
1c792db7 | 9169 | return; |
4550487a | 9170 | |
1c792db7 SS |
9171 | /* |
9172 | * Use smp_send_reschedule() instead of resched_cpu(). | |
97fb7a0a | 9173 | * This way we generate a sched IPI on the target CPU which |
1c792db7 SS |
9174 | * is idle. And the softirq performing nohz idle load balance |
9175 | * will be run before returning from the IPI. | |
9176 | */ | |
9177 | smp_send_reschedule(ilb_cpu); | |
4550487a PZ |
9178 | } |
9179 | ||
9180 | /* | |
9181 | * Current heuristic for kicking the idle load balancer in the presence | |
9182 | * of an idle cpu in the system. | |
9183 | * - This rq has more than one task. | |
9184 | * - This rq has at least one CFS task and the capacity of the CPU is | |
9185 | * significantly reduced because of RT tasks or IRQs. | |
9186 | * - At parent of LLC scheduler domain level, this cpu's scheduler group has | |
9187 | * multiple busy cpu. | |
9188 | * - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler | |
9189 | * domain span are idle. | |
9190 | */ | |
9191 | static void nohz_balancer_kick(struct rq *rq) | |
9192 | { | |
9193 | unsigned long now = jiffies; | |
9194 | struct sched_domain_shared *sds; | |
9195 | struct sched_domain *sd; | |
9196 | int nr_busy, i, cpu = rq->cpu; | |
a4064fb6 | 9197 | unsigned int flags = 0; |
4550487a PZ |
9198 | |
9199 | if (unlikely(rq->idle_balance)) | |
9200 | return; | |
9201 | ||
9202 | /* | |
9203 | * We may be recently in ticked or tickless idle mode. At the first | |
9204 | * busy tick after returning from idle, we will update the busy stats. | |
9205 | */ | |
00357f5e | 9206 | nohz_balance_exit_idle(rq); |
4550487a PZ |
9207 | |
9208 | /* | |
9209 | * None are in tickless mode and hence no need for NOHZ idle load | |
9210 | * balancing. | |
9211 | */ | |
9212 | if (likely(!atomic_read(&nohz.nr_cpus))) | |
9213 | return; | |
9214 | ||
f643ea22 VG |
9215 | if (READ_ONCE(nohz.has_blocked) && |
9216 | time_after(now, READ_ONCE(nohz.next_blocked))) | |
a4064fb6 PZ |
9217 | flags = NOHZ_STATS_KICK; |
9218 | ||
4550487a | 9219 | if (time_before(now, nohz.next_balance)) |
a4064fb6 | 9220 | goto out; |
4550487a PZ |
9221 | |
9222 | if (rq->nr_running >= 2) { | |
a4064fb6 | 9223 | flags = NOHZ_KICK_MASK; |
4550487a PZ |
9224 | goto out; |
9225 | } | |
9226 | ||
9227 | rcu_read_lock(); | |
9228 | sds = rcu_dereference(per_cpu(sd_llc_shared, cpu)); | |
9229 | if (sds) { | |
9230 | /* | |
9231 | * XXX: write a coherent comment on why we do this. | |
9232 | * See also: http://lkml.kernel.org/r/20111202010832.602203411@sbsiddha-desk.sc.intel.com | |
9233 | */ | |
9234 | nr_busy = atomic_read(&sds->nr_busy_cpus); | |
9235 | if (nr_busy > 1) { | |
a4064fb6 | 9236 | flags = NOHZ_KICK_MASK; |
4550487a PZ |
9237 | goto unlock; |
9238 | } | |
9239 | ||
9240 | } | |
9241 | ||
9242 | sd = rcu_dereference(rq->sd); | |
9243 | if (sd) { | |
9244 | if ((rq->cfs.h_nr_running >= 1) && | |
9245 | check_cpu_capacity(rq, sd)) { | |
a4064fb6 | 9246 | flags = NOHZ_KICK_MASK; |
4550487a PZ |
9247 | goto unlock; |
9248 | } | |
9249 | } | |
9250 | ||
9251 | sd = rcu_dereference(per_cpu(sd_asym, cpu)); | |
9252 | if (sd) { | |
9253 | for_each_cpu(i, sched_domain_span(sd)) { | |
9254 | if (i == cpu || | |
9255 | !cpumask_test_cpu(i, nohz.idle_cpus_mask)) | |
9256 | continue; | |
9257 | ||
9258 | if (sched_asym_prefer(i, cpu)) { | |
a4064fb6 | 9259 | flags = NOHZ_KICK_MASK; |
4550487a PZ |
9260 | goto unlock; |
9261 | } | |
9262 | } | |
9263 | } | |
9264 | unlock: | |
9265 | rcu_read_unlock(); | |
9266 | out: | |
a4064fb6 PZ |
9267 | if (flags) |
9268 | kick_ilb(flags); | |
83cd4fe2 VP |
9269 | } |
9270 | ||
00357f5e | 9271 | static void set_cpu_sd_state_busy(int cpu) |
71325960 | 9272 | { |
00357f5e | 9273 | struct sched_domain *sd; |
a22e47a4 | 9274 | |
00357f5e PZ |
9275 | rcu_read_lock(); |
9276 | sd = rcu_dereference(per_cpu(sd_llc, cpu)); | |
a22e47a4 | 9277 | |
00357f5e PZ |
9278 | if (!sd || !sd->nohz_idle) |
9279 | goto unlock; | |
9280 | sd->nohz_idle = 0; | |
9281 | ||
9282 | atomic_inc(&sd->shared->nr_busy_cpus); | |
9283 | unlock: | |
9284 | rcu_read_unlock(); | |
71325960 SS |
9285 | } |
9286 | ||
00357f5e PZ |
9287 | void nohz_balance_exit_idle(struct rq *rq) |
9288 | { | |
9289 | SCHED_WARN_ON(rq != this_rq()); | |
9290 | ||
9291 | if (likely(!rq->nohz_tick_stopped)) | |
9292 | return; | |
9293 | ||
9294 | rq->nohz_tick_stopped = 0; | |
9295 | cpumask_clear_cpu(rq->cpu, nohz.idle_cpus_mask); | |
9296 | atomic_dec(&nohz.nr_cpus); | |
9297 | ||
9298 | set_cpu_sd_state_busy(rq->cpu); | |
9299 | } | |
9300 | ||
9301 | static void set_cpu_sd_state_idle(int cpu) | |
69e1e811 SS |
9302 | { |
9303 | struct sched_domain *sd; | |
69e1e811 | 9304 | |
69e1e811 | 9305 | rcu_read_lock(); |
0e369d75 | 9306 | sd = rcu_dereference(per_cpu(sd_llc, cpu)); |
25f55d9d VG |
9307 | |
9308 | if (!sd || sd->nohz_idle) | |
9309 | goto unlock; | |
9310 | sd->nohz_idle = 1; | |
9311 | ||
0e369d75 | 9312 | atomic_dec(&sd->shared->nr_busy_cpus); |
25f55d9d | 9313 | unlock: |
69e1e811 SS |
9314 | rcu_read_unlock(); |
9315 | } | |
9316 | ||
1e3c88bd | 9317 | /* |
97fb7a0a | 9318 | * This routine will record that the CPU is going idle with tick stopped. |
0b005cf5 | 9319 | * This info will be used in performing idle load balancing in the future. |
1e3c88bd | 9320 | */ |
c1cc017c | 9321 | void nohz_balance_enter_idle(int cpu) |
1e3c88bd | 9322 | { |
00357f5e PZ |
9323 | struct rq *rq = cpu_rq(cpu); |
9324 | ||
9325 | SCHED_WARN_ON(cpu != smp_processor_id()); | |
9326 | ||
97fb7a0a | 9327 | /* If this CPU is going down, then nothing needs to be done: */ |
71325960 SS |
9328 | if (!cpu_active(cpu)) |
9329 | return; | |
9330 | ||
387bc8b5 | 9331 | /* Spare idle load balancing on CPUs that don't want to be disturbed: */ |
de201559 | 9332 | if (!housekeeping_cpu(cpu, HK_FLAG_SCHED)) |
387bc8b5 FW |
9333 | return; |
9334 | ||
f643ea22 VG |
9335 | /* |
9336 | * Can be set safely without rq->lock held | |
9337 | * If a clear happens, it will have evaluated last additions because | |
9338 | * rq->lock is held during the check and the clear | |
9339 | */ | |
9340 | rq->has_blocked_load = 1; | |
9341 | ||
9342 | /* | |
9343 | * The tick is still stopped but load could have been added in the | |
9344 | * meantime. We set the nohz.has_blocked flag to trig a check of the | |
9345 | * *_avg. The CPU is already part of nohz.idle_cpus_mask so the clear | |
9346 | * of nohz.has_blocked can only happen after checking the new load | |
9347 | */ | |
00357f5e | 9348 | if (rq->nohz_tick_stopped) |
f643ea22 | 9349 | goto out; |
1e3c88bd | 9350 | |
97fb7a0a | 9351 | /* If we're a completely isolated CPU, we don't play: */ |
00357f5e | 9352 | if (on_null_domain(rq)) |
d987fc7f MG |
9353 | return; |
9354 | ||
00357f5e PZ |
9355 | rq->nohz_tick_stopped = 1; |
9356 | ||
c1cc017c AS |
9357 | cpumask_set_cpu(cpu, nohz.idle_cpus_mask); |
9358 | atomic_inc(&nohz.nr_cpus); | |
00357f5e | 9359 | |
f643ea22 VG |
9360 | /* |
9361 | * Ensures that if nohz_idle_balance() fails to observe our | |
9362 | * @idle_cpus_mask store, it must observe the @has_blocked | |
9363 | * store. | |
9364 | */ | |
9365 | smp_mb__after_atomic(); | |
9366 | ||
00357f5e | 9367 | set_cpu_sd_state_idle(cpu); |
f643ea22 VG |
9368 | |
9369 | out: | |
9370 | /* | |
9371 | * Each time a cpu enter idle, we assume that it has blocked load and | |
9372 | * enable the periodic update of the load of idle cpus | |
9373 | */ | |
9374 | WRITE_ONCE(nohz.has_blocked, 1); | |
1e3c88bd | 9375 | } |
1e3c88bd | 9376 | |
1e3c88bd | 9377 | /* |
3451d024 | 9378 | * In CONFIG_NO_HZ_COMMON case, the idle balance kickee will do the |
97fb7a0a | 9379 | * rebalancing for all the CPUs for whom scheduler ticks are stopped. |
1e3c88bd | 9380 | */ |
b7031a02 | 9381 | static bool nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle) |
83cd4fe2 | 9382 | { |
c5afb6a8 | 9383 | /* Earliest time when we have to do rebalance again */ |
a4064fb6 PZ |
9384 | unsigned long now = jiffies; |
9385 | unsigned long next_balance = now + 60*HZ; | |
f643ea22 | 9386 | bool has_blocked_load = false; |
c5afb6a8 | 9387 | int update_next_balance = 0; |
b7031a02 PZ |
9388 | int this_cpu = this_rq->cpu; |
9389 | unsigned int flags; | |
9390 | int balance_cpu; | |
9391 | struct rq *rq; | |
83cd4fe2 | 9392 | |
b7031a02 PZ |
9393 | if (!(atomic_read(nohz_flags(this_cpu)) & NOHZ_KICK_MASK)) |
9394 | return false; | |
a22e47a4 | 9395 | |
b7031a02 PZ |
9396 | if (idle != CPU_IDLE) { |
9397 | atomic_andnot(NOHZ_KICK_MASK, nohz_flags(this_cpu)); | |
9398 | return false; | |
9399 | } | |
9400 | ||
9401 | flags = atomic_fetch_andnot(NOHZ_KICK_MASK, nohz_flags(this_cpu)); | |
9402 | ||
9403 | SCHED_WARN_ON((flags & NOHZ_KICK_MASK) == NOHZ_BALANCE_KICK); | |
83cd4fe2 | 9404 | |
f643ea22 VG |
9405 | /* |
9406 | * We assume there will be no idle load after this update and clear | |
9407 | * the has_blocked flag. If a cpu enters idle in the mean time, it will | |
9408 | * set the has_blocked flag and trig another update of idle load. | |
9409 | * Because a cpu that becomes idle, is added to idle_cpus_mask before | |
9410 | * setting the flag, we are sure to not clear the state and not | |
9411 | * check the load of an idle cpu. | |
9412 | */ | |
9413 | WRITE_ONCE(nohz.has_blocked, 0); | |
9414 | ||
9415 | /* | |
9416 | * Ensures that if we miss the CPU, we must see the has_blocked | |
9417 | * store from nohz_balance_enter_idle(). | |
9418 | */ | |
9419 | smp_mb(); | |
9420 | ||
83cd4fe2 | 9421 | for_each_cpu(balance_cpu, nohz.idle_cpus_mask) { |
8a6d42d1 | 9422 | if (balance_cpu == this_cpu || !idle_cpu(balance_cpu)) |
83cd4fe2 VP |
9423 | continue; |
9424 | ||
9425 | /* | |
97fb7a0a IM |
9426 | * If this CPU gets work to do, stop the load balancing |
9427 | * work being done for other CPUs. Next load | |
83cd4fe2 VP |
9428 | * balancing owner will pick it up. |
9429 | */ | |
f643ea22 VG |
9430 | if (need_resched()) { |
9431 | has_blocked_load = true; | |
9432 | goto abort; | |
9433 | } | |
83cd4fe2 | 9434 | |
5ed4f1d9 VG |
9435 | rq = cpu_rq(balance_cpu); |
9436 | ||
63928384 | 9437 | has_blocked_load |= update_nohz_stats(rq, true); |
f643ea22 | 9438 | |
ed61bbc6 TC |
9439 | /* |
9440 | * If time for next balance is due, | |
9441 | * do the balance. | |
9442 | */ | |
9443 | if (time_after_eq(jiffies, rq->next_balance)) { | |
8a8c69c3 PZ |
9444 | struct rq_flags rf; |
9445 | ||
9446 | rq_lock_irq(rq, &rf); | |
ed61bbc6 | 9447 | update_rq_clock(rq); |
cee1afce | 9448 | cpu_load_update_idle(rq); |
8a8c69c3 PZ |
9449 | rq_unlock_irq(rq, &rf); |
9450 | ||
b7031a02 PZ |
9451 | if (flags & NOHZ_BALANCE_KICK) |
9452 | rebalance_domains(rq, CPU_IDLE); | |
ed61bbc6 | 9453 | } |
83cd4fe2 | 9454 | |
c5afb6a8 VG |
9455 | if (time_after(next_balance, rq->next_balance)) { |
9456 | next_balance = rq->next_balance; | |
9457 | update_next_balance = 1; | |
9458 | } | |
83cd4fe2 | 9459 | } |
c5afb6a8 | 9460 | |
b7031a02 PZ |
9461 | update_blocked_averages(this_cpu); |
9462 | if (flags & NOHZ_BALANCE_KICK) | |
9463 | rebalance_domains(this_rq, CPU_IDLE); | |
9464 | ||
f643ea22 VG |
9465 | WRITE_ONCE(nohz.next_blocked, |
9466 | now + msecs_to_jiffies(LOAD_AVG_PERIOD)); | |
9467 | ||
9468 | abort: | |
9469 | /* There is still blocked load, enable periodic update */ | |
9470 | if (has_blocked_load) | |
9471 | WRITE_ONCE(nohz.has_blocked, 1); | |
a4064fb6 | 9472 | |
c5afb6a8 VG |
9473 | /* |
9474 | * next_balance will be updated only when there is a need. | |
9475 | * When the CPU is attached to null domain for ex, it will not be | |
9476 | * updated. | |
9477 | */ | |
9478 | if (likely(update_next_balance)) | |
9479 | nohz.next_balance = next_balance; | |
b7031a02 PZ |
9480 | |
9481 | return true; | |
83cd4fe2 | 9482 | } |
dd707247 PZ |
9483 | #else /* !CONFIG_NO_HZ_COMMON */ |
9484 | static inline void nohz_balancer_kick(struct rq *rq) { } | |
9485 | ||
b7031a02 PZ |
9486 | static bool nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle) |
9487 | { | |
9488 | return false; | |
9489 | } | |
dd707247 | 9490 | #endif /* CONFIG_NO_HZ_COMMON */ |
83cd4fe2 | 9491 | |
47ea5412 PZ |
9492 | /* |
9493 | * idle_balance is called by schedule() if this_cpu is about to become | |
9494 | * idle. Attempts to pull tasks from other CPUs. | |
9495 | */ | |
9496 | static int idle_balance(struct rq *this_rq, struct rq_flags *rf) | |
9497 | { | |
9498 | unsigned long next_balance = jiffies + HZ; | |
9499 | int this_cpu = this_rq->cpu; | |
9500 | struct sched_domain *sd; | |
9501 | int pulled_task = 0; | |
9502 | u64 curr_cost = 0; | |
9503 | ||
9504 | /* | |
9505 | * We must set idle_stamp _before_ calling idle_balance(), such that we | |
9506 | * measure the duration of idle_balance() as idle time. | |
9507 | */ | |
9508 | this_rq->idle_stamp = rq_clock(this_rq); | |
9509 | ||
9510 | /* | |
9511 | * Do not pull tasks towards !active CPUs... | |
9512 | */ | |
9513 | if (!cpu_active(this_cpu)) | |
9514 | return 0; | |
9515 | ||
9516 | /* | |
9517 | * This is OK, because current is on_cpu, which avoids it being picked | |
9518 | * for load-balance and preemption/IRQs are still disabled avoiding | |
9519 | * further scheduler activity on it and we're being very careful to | |
9520 | * re-start the picking loop. | |
9521 | */ | |
9522 | rq_unpin_lock(this_rq, rf); | |
9523 | ||
9524 | if (this_rq->avg_idle < sysctl_sched_migration_cost || | |
9525 | !this_rq->rd->overload) { | |
9526 | rcu_read_lock(); | |
9527 | sd = rcu_dereference_check_sched_domain(this_rq->sd); | |
9528 | if (sd) | |
9529 | update_next_balance(sd, &next_balance); | |
9530 | rcu_read_unlock(); | |
9531 | ||
9532 | goto out; | |
9533 | } | |
9534 | ||
9535 | raw_spin_unlock(&this_rq->lock); | |
9536 | ||
9537 | update_blocked_averages(this_cpu); | |
9538 | rcu_read_lock(); | |
9539 | for_each_domain(this_cpu, sd) { | |
9540 | int continue_balancing = 1; | |
9541 | u64 t0, domain_cost; | |
9542 | ||
9543 | if (!(sd->flags & SD_LOAD_BALANCE)) | |
9544 | continue; | |
9545 | ||
9546 | if (this_rq->avg_idle < curr_cost + sd->max_newidle_lb_cost) { | |
9547 | update_next_balance(sd, &next_balance); | |
9548 | break; | |
9549 | } | |
9550 | ||
9551 | if (sd->flags & SD_BALANCE_NEWIDLE) { | |
9552 | t0 = sched_clock_cpu(this_cpu); | |
9553 | ||
9554 | pulled_task = load_balance(this_cpu, this_rq, | |
9555 | sd, CPU_NEWLY_IDLE, | |
9556 | &continue_balancing); | |
9557 | ||
9558 | domain_cost = sched_clock_cpu(this_cpu) - t0; | |
9559 | if (domain_cost > sd->max_newidle_lb_cost) | |
9560 | sd->max_newidle_lb_cost = domain_cost; | |
9561 | ||
9562 | curr_cost += domain_cost; | |
9563 | } | |
9564 | ||
9565 | update_next_balance(sd, &next_balance); | |
9566 | ||
9567 | /* | |
9568 | * Stop searching for tasks to pull if there are | |
9569 | * now runnable tasks on this rq. | |
9570 | */ | |
9571 | if (pulled_task || this_rq->nr_running > 0) | |
9572 | break; | |
9573 | } | |
9574 | rcu_read_unlock(); | |
9575 | ||
9576 | raw_spin_lock(&this_rq->lock); | |
9577 | ||
9578 | if (curr_cost > this_rq->max_idle_balance_cost) | |
9579 | this_rq->max_idle_balance_cost = curr_cost; | |
9580 | ||
9581 | /* | |
9582 | * While browsing the domains, we released the rq lock, a task could | |
9583 | * have been enqueued in the meantime. Since we're not going idle, | |
9584 | * pretend we pulled a task. | |
9585 | */ | |
9586 | if (this_rq->cfs.h_nr_running && !pulled_task) | |
9587 | pulled_task = 1; | |
9588 | ||
9589 | out: | |
9590 | /* Move the next balance forward */ | |
9591 | if (time_after(this_rq->next_balance, next_balance)) | |
9592 | this_rq->next_balance = next_balance; | |
9593 | ||
9594 | /* Is there a task of a high priority class? */ | |
9595 | if (this_rq->nr_running != this_rq->cfs.h_nr_running) | |
9596 | pulled_task = -1; | |
9597 | ||
9598 | if (pulled_task) | |
9599 | this_rq->idle_stamp = 0; | |
9600 | ||
9601 | rq_repin_lock(this_rq, rf); | |
9602 | ||
9603 | return pulled_task; | |
9604 | } | |
9605 | ||
83cd4fe2 VP |
9606 | /* |
9607 | * run_rebalance_domains is triggered when needed from the scheduler tick. | |
9608 | * Also triggered for nohz idle balancing (with nohz_balancing_kick set). | |
9609 | */ | |
0766f788 | 9610 | static __latent_entropy void run_rebalance_domains(struct softirq_action *h) |
1e3c88bd | 9611 | { |
208cb16b | 9612 | struct rq *this_rq = this_rq(); |
6eb57e0d | 9613 | enum cpu_idle_type idle = this_rq->idle_balance ? |
1e3c88bd PZ |
9614 | CPU_IDLE : CPU_NOT_IDLE; |
9615 | ||
1e3c88bd | 9616 | /* |
97fb7a0a IM |
9617 | * If this CPU has a pending nohz_balance_kick, then do the |
9618 | * balancing on behalf of the other idle CPUs whose ticks are | |
d4573c3e | 9619 | * stopped. Do nohz_idle_balance *before* rebalance_domains to |
97fb7a0a | 9620 | * give the idle CPUs a chance to load balance. Else we may |
d4573c3e PM |
9621 | * load balance only within the local sched_domain hierarchy |
9622 | * and abort nohz_idle_balance altogether if we pull some load. | |
1e3c88bd | 9623 | */ |
b7031a02 PZ |
9624 | if (nohz_idle_balance(this_rq, idle)) |
9625 | return; | |
9626 | ||
9627 | /* normal load balance */ | |
9628 | update_blocked_averages(this_rq->cpu); | |
d4573c3e | 9629 | rebalance_domains(this_rq, idle); |
1e3c88bd PZ |
9630 | } |
9631 | ||
1e3c88bd PZ |
9632 | /* |
9633 | * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing. | |
1e3c88bd | 9634 | */ |
7caff66f | 9635 | void trigger_load_balance(struct rq *rq) |
1e3c88bd | 9636 | { |
1e3c88bd | 9637 | /* Don't need to rebalance while attached to NULL domain */ |
c726099e DL |
9638 | if (unlikely(on_null_domain(rq))) |
9639 | return; | |
9640 | ||
9641 | if (time_after_eq(jiffies, rq->next_balance)) | |
1e3c88bd | 9642 | raise_softirq(SCHED_SOFTIRQ); |
4550487a PZ |
9643 | |
9644 | nohz_balancer_kick(rq); | |
1e3c88bd PZ |
9645 | } |
9646 | ||
0bcdcf28 CE |
9647 | static void rq_online_fair(struct rq *rq) |
9648 | { | |
9649 | update_sysctl(); | |
0e59bdae KT |
9650 | |
9651 | update_runtime_enabled(rq); | |
0bcdcf28 CE |
9652 | } |
9653 | ||
9654 | static void rq_offline_fair(struct rq *rq) | |
9655 | { | |
9656 | update_sysctl(); | |
a4c96ae3 PB |
9657 | |
9658 | /* Ensure any throttled groups are reachable by pick_next_task */ | |
9659 | unthrottle_offline_cfs_rqs(rq); | |
0bcdcf28 CE |
9660 | } |
9661 | ||
55e12e5e | 9662 | #endif /* CONFIG_SMP */ |
e1d1484f | 9663 | |
bf0f6f24 | 9664 | /* |
d84b3131 FW |
9665 | * scheduler tick hitting a task of our scheduling class. |
9666 | * | |
9667 | * NOTE: This function can be called remotely by the tick offload that | |
9668 | * goes along full dynticks. Therefore no local assumption can be made | |
9669 | * and everything must be accessed through the @rq and @curr passed in | |
9670 | * parameters. | |
bf0f6f24 | 9671 | */ |
8f4d37ec | 9672 | static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued) |
bf0f6f24 IM |
9673 | { |
9674 | struct cfs_rq *cfs_rq; | |
9675 | struct sched_entity *se = &curr->se; | |
9676 | ||
9677 | for_each_sched_entity(se) { | |
9678 | cfs_rq = cfs_rq_of(se); | |
8f4d37ec | 9679 | entity_tick(cfs_rq, se, queued); |
bf0f6f24 | 9680 | } |
18bf2805 | 9681 | |
b52da86e | 9682 | if (static_branch_unlikely(&sched_numa_balancing)) |
cbee9f88 | 9683 | task_tick_numa(rq, curr); |
bf0f6f24 IM |
9684 | } |
9685 | ||
9686 | /* | |
cd29fe6f PZ |
9687 | * called on fork with the child task as argument from the parent's context |
9688 | * - child not yet on the tasklist | |
9689 | * - preemption disabled | |
bf0f6f24 | 9690 | */ |
cd29fe6f | 9691 | static void task_fork_fair(struct task_struct *p) |
bf0f6f24 | 9692 | { |
4fc420c9 DN |
9693 | struct cfs_rq *cfs_rq; |
9694 | struct sched_entity *se = &p->se, *curr; | |
cd29fe6f | 9695 | struct rq *rq = this_rq(); |
8a8c69c3 | 9696 | struct rq_flags rf; |
bf0f6f24 | 9697 | |
8a8c69c3 | 9698 | rq_lock(rq, &rf); |
861d034e PZ |
9699 | update_rq_clock(rq); |
9700 | ||
4fc420c9 DN |
9701 | cfs_rq = task_cfs_rq(current); |
9702 | curr = cfs_rq->curr; | |
e210bffd PZ |
9703 | if (curr) { |
9704 | update_curr(cfs_rq); | |
b5d9d734 | 9705 | se->vruntime = curr->vruntime; |
e210bffd | 9706 | } |
aeb73b04 | 9707 | place_entity(cfs_rq, se, 1); |
4d78e7b6 | 9708 | |
cd29fe6f | 9709 | if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) { |
87fefa38 | 9710 | /* |
edcb60a3 IM |
9711 | * Upon rescheduling, sched_class::put_prev_task() will place |
9712 | * 'current' within the tree based on its new key value. | |
9713 | */ | |
4d78e7b6 | 9714 | swap(curr->vruntime, se->vruntime); |
8875125e | 9715 | resched_curr(rq); |
4d78e7b6 | 9716 | } |
bf0f6f24 | 9717 | |
88ec22d3 | 9718 | se->vruntime -= cfs_rq->min_vruntime; |
8a8c69c3 | 9719 | rq_unlock(rq, &rf); |
bf0f6f24 IM |
9720 | } |
9721 | ||
cb469845 SR |
9722 | /* |
9723 | * Priority of the task has changed. Check to see if we preempt | |
9724 | * the current task. | |
9725 | */ | |
da7a735e PZ |
9726 | static void |
9727 | prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio) | |
cb469845 | 9728 | { |
da0c1e65 | 9729 | if (!task_on_rq_queued(p)) |
da7a735e PZ |
9730 | return; |
9731 | ||
cb469845 SR |
9732 | /* |
9733 | * Reschedule if we are currently running on this runqueue and | |
9734 | * our priority decreased, or if we are not currently running on | |
9735 | * this runqueue and our priority is higher than the current's | |
9736 | */ | |
da7a735e | 9737 | if (rq->curr == p) { |
cb469845 | 9738 | if (p->prio > oldprio) |
8875125e | 9739 | resched_curr(rq); |
cb469845 | 9740 | } else |
15afe09b | 9741 | check_preempt_curr(rq, p, 0); |
cb469845 SR |
9742 | } |
9743 | ||
daa59407 | 9744 | static inline bool vruntime_normalized(struct task_struct *p) |
da7a735e PZ |
9745 | { |
9746 | struct sched_entity *se = &p->se; | |
da7a735e PZ |
9747 | |
9748 | /* | |
daa59407 BP |
9749 | * In both the TASK_ON_RQ_QUEUED and TASK_ON_RQ_MIGRATING cases, |
9750 | * the dequeue_entity(.flags=0) will already have normalized the | |
9751 | * vruntime. | |
9752 | */ | |
9753 | if (p->on_rq) | |
9754 | return true; | |
9755 | ||
9756 | /* | |
9757 | * When !on_rq, vruntime of the task has usually NOT been normalized. | |
9758 | * But there are some cases where it has already been normalized: | |
da7a735e | 9759 | * |
daa59407 BP |
9760 | * - A forked child which is waiting for being woken up by |
9761 | * wake_up_new_task(). | |
9762 | * - A task which has been woken up by try_to_wake_up() and | |
9763 | * waiting for actually being woken up by sched_ttwu_pending(). | |
da7a735e | 9764 | */ |
daa59407 BP |
9765 | if (!se->sum_exec_runtime || p->state == TASK_WAKING) |
9766 | return true; | |
9767 | ||
9768 | return false; | |
9769 | } | |
9770 | ||
09a43ace VG |
9771 | #ifdef CONFIG_FAIR_GROUP_SCHED |
9772 | /* | |
9773 | * Propagate the changes of the sched_entity across the tg tree to make it | |
9774 | * visible to the root | |
9775 | */ | |
9776 | static void propagate_entity_cfs_rq(struct sched_entity *se) | |
9777 | { | |
9778 | struct cfs_rq *cfs_rq; | |
9779 | ||
9780 | /* Start to propagate at parent */ | |
9781 | se = se->parent; | |
9782 | ||
9783 | for_each_sched_entity(se) { | |
9784 | cfs_rq = cfs_rq_of(se); | |
9785 | ||
9786 | if (cfs_rq_throttled(cfs_rq)) | |
9787 | break; | |
9788 | ||
88c0616e | 9789 | update_load_avg(cfs_rq, se, UPDATE_TG); |
09a43ace VG |
9790 | } |
9791 | } | |
9792 | #else | |
9793 | static void propagate_entity_cfs_rq(struct sched_entity *se) { } | |
9794 | #endif | |
9795 | ||
df217913 | 9796 | static void detach_entity_cfs_rq(struct sched_entity *se) |
daa59407 | 9797 | { |
daa59407 BP |
9798 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
9799 | ||
9d89c257 | 9800 | /* Catch up with the cfs_rq and remove our load when we leave */ |
88c0616e | 9801 | update_load_avg(cfs_rq, se, 0); |
a05e8c51 | 9802 | detach_entity_load_avg(cfs_rq, se); |
7c3edd2c | 9803 | update_tg_load_avg(cfs_rq, false); |
09a43ace | 9804 | propagate_entity_cfs_rq(se); |
da7a735e PZ |
9805 | } |
9806 | ||
df217913 | 9807 | static void attach_entity_cfs_rq(struct sched_entity *se) |
cb469845 | 9808 | { |
daa59407 | 9809 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
7855a35a BP |
9810 | |
9811 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
eb7a59b2 M |
9812 | /* |
9813 | * Since the real-depth could have been changed (only FAIR | |
9814 | * class maintain depth value), reset depth properly. | |
9815 | */ | |
9816 | se->depth = se->parent ? se->parent->depth + 1 : 0; | |
9817 | #endif | |
7855a35a | 9818 | |
df217913 | 9819 | /* Synchronize entity with its cfs_rq */ |
88c0616e | 9820 | update_load_avg(cfs_rq, se, sched_feat(ATTACH_AGE_LOAD) ? 0 : SKIP_AGE_LOAD); |
ea14b57e | 9821 | attach_entity_load_avg(cfs_rq, se, 0); |
7c3edd2c | 9822 | update_tg_load_avg(cfs_rq, false); |
09a43ace | 9823 | propagate_entity_cfs_rq(se); |
df217913 VG |
9824 | } |
9825 | ||
9826 | static void detach_task_cfs_rq(struct task_struct *p) | |
9827 | { | |
9828 | struct sched_entity *se = &p->se; | |
9829 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
9830 | ||
9831 | if (!vruntime_normalized(p)) { | |
9832 | /* | |
9833 | * Fix up our vruntime so that the current sleep doesn't | |
9834 | * cause 'unlimited' sleep bonus. | |
9835 | */ | |
9836 | place_entity(cfs_rq, se, 0); | |
9837 | se->vruntime -= cfs_rq->min_vruntime; | |
9838 | } | |
9839 | ||
9840 | detach_entity_cfs_rq(se); | |
9841 | } | |
9842 | ||
9843 | static void attach_task_cfs_rq(struct task_struct *p) | |
9844 | { | |
9845 | struct sched_entity *se = &p->se; | |
9846 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
9847 | ||
9848 | attach_entity_cfs_rq(se); | |
daa59407 BP |
9849 | |
9850 | if (!vruntime_normalized(p)) | |
9851 | se->vruntime += cfs_rq->min_vruntime; | |
9852 | } | |
6efdb105 | 9853 | |
daa59407 BP |
9854 | static void switched_from_fair(struct rq *rq, struct task_struct *p) |
9855 | { | |
9856 | detach_task_cfs_rq(p); | |
9857 | } | |
9858 | ||
9859 | static void switched_to_fair(struct rq *rq, struct task_struct *p) | |
9860 | { | |
9861 | attach_task_cfs_rq(p); | |
7855a35a | 9862 | |
daa59407 | 9863 | if (task_on_rq_queued(p)) { |
7855a35a | 9864 | /* |
daa59407 BP |
9865 | * We were most likely switched from sched_rt, so |
9866 | * kick off the schedule if running, otherwise just see | |
9867 | * if we can still preempt the current task. | |
7855a35a | 9868 | */ |
daa59407 BP |
9869 | if (rq->curr == p) |
9870 | resched_curr(rq); | |
9871 | else | |
9872 | check_preempt_curr(rq, p, 0); | |
7855a35a | 9873 | } |
cb469845 SR |
9874 | } |
9875 | ||
83b699ed SV |
9876 | /* Account for a task changing its policy or group. |
9877 | * | |
9878 | * This routine is mostly called to set cfs_rq->curr field when a task | |
9879 | * migrates between groups/classes. | |
9880 | */ | |
9881 | static void set_curr_task_fair(struct rq *rq) | |
9882 | { | |
9883 | struct sched_entity *se = &rq->curr->se; | |
9884 | ||
ec12cb7f PT |
9885 | for_each_sched_entity(se) { |
9886 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
9887 | ||
9888 | set_next_entity(cfs_rq, se); | |
9889 | /* ensure bandwidth has been allocated on our new cfs_rq */ | |
9890 | account_cfs_rq_runtime(cfs_rq, 0); | |
9891 | } | |
83b699ed SV |
9892 | } |
9893 | ||
029632fb PZ |
9894 | void init_cfs_rq(struct cfs_rq *cfs_rq) |
9895 | { | |
bfb06889 | 9896 | cfs_rq->tasks_timeline = RB_ROOT_CACHED; |
029632fb PZ |
9897 | cfs_rq->min_vruntime = (u64)(-(1LL << 20)); |
9898 | #ifndef CONFIG_64BIT | |
9899 | cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime; | |
9900 | #endif | |
141965c7 | 9901 | #ifdef CONFIG_SMP |
2a2f5d4e | 9902 | raw_spin_lock_init(&cfs_rq->removed.lock); |
9ee474f5 | 9903 | #endif |
029632fb PZ |
9904 | } |
9905 | ||
810b3817 | 9906 | #ifdef CONFIG_FAIR_GROUP_SCHED |
ea86cb4b VG |
9907 | static void task_set_group_fair(struct task_struct *p) |
9908 | { | |
9909 | struct sched_entity *se = &p->se; | |
9910 | ||
9911 | set_task_rq(p, task_cpu(p)); | |
9912 | se->depth = se->parent ? se->parent->depth + 1 : 0; | |
9913 | } | |
9914 | ||
bc54da21 | 9915 | static void task_move_group_fair(struct task_struct *p) |
810b3817 | 9916 | { |
daa59407 | 9917 | detach_task_cfs_rq(p); |
b2b5ce02 | 9918 | set_task_rq(p, task_cpu(p)); |
6efdb105 BP |
9919 | |
9920 | #ifdef CONFIG_SMP | |
9921 | /* Tell se's cfs_rq has been changed -- migrated */ | |
9922 | p->se.avg.last_update_time = 0; | |
9923 | #endif | |
daa59407 | 9924 | attach_task_cfs_rq(p); |
810b3817 | 9925 | } |
029632fb | 9926 | |
ea86cb4b VG |
9927 | static void task_change_group_fair(struct task_struct *p, int type) |
9928 | { | |
9929 | switch (type) { | |
9930 | case TASK_SET_GROUP: | |
9931 | task_set_group_fair(p); | |
9932 | break; | |
9933 | ||
9934 | case TASK_MOVE_GROUP: | |
9935 | task_move_group_fair(p); | |
9936 | break; | |
9937 | } | |
9938 | } | |
9939 | ||
029632fb PZ |
9940 | void free_fair_sched_group(struct task_group *tg) |
9941 | { | |
9942 | int i; | |
9943 | ||
9944 | destroy_cfs_bandwidth(tg_cfs_bandwidth(tg)); | |
9945 | ||
9946 | for_each_possible_cpu(i) { | |
9947 | if (tg->cfs_rq) | |
9948 | kfree(tg->cfs_rq[i]); | |
6fe1f348 | 9949 | if (tg->se) |
029632fb PZ |
9950 | kfree(tg->se[i]); |
9951 | } | |
9952 | ||
9953 | kfree(tg->cfs_rq); | |
9954 | kfree(tg->se); | |
9955 | } | |
9956 | ||
9957 | int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent) | |
9958 | { | |
029632fb | 9959 | struct sched_entity *se; |
b7fa30c9 | 9960 | struct cfs_rq *cfs_rq; |
029632fb PZ |
9961 | int i; |
9962 | ||
9963 | tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL); | |
9964 | if (!tg->cfs_rq) | |
9965 | goto err; | |
9966 | tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL); | |
9967 | if (!tg->se) | |
9968 | goto err; | |
9969 | ||
9970 | tg->shares = NICE_0_LOAD; | |
9971 | ||
9972 | init_cfs_bandwidth(tg_cfs_bandwidth(tg)); | |
9973 | ||
9974 | for_each_possible_cpu(i) { | |
9975 | cfs_rq = kzalloc_node(sizeof(struct cfs_rq), | |
9976 | GFP_KERNEL, cpu_to_node(i)); | |
9977 | if (!cfs_rq) | |
9978 | goto err; | |
9979 | ||
9980 | se = kzalloc_node(sizeof(struct sched_entity), | |
9981 | GFP_KERNEL, cpu_to_node(i)); | |
9982 | if (!se) | |
9983 | goto err_free_rq; | |
9984 | ||
9985 | init_cfs_rq(cfs_rq); | |
9986 | init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]); | |
540247fb | 9987 | init_entity_runnable_average(se); |
029632fb PZ |
9988 | } |
9989 | ||
9990 | return 1; | |
9991 | ||
9992 | err_free_rq: | |
9993 | kfree(cfs_rq); | |
9994 | err: | |
9995 | return 0; | |
9996 | } | |
9997 | ||
8663e24d PZ |
9998 | void online_fair_sched_group(struct task_group *tg) |
9999 | { | |
10000 | struct sched_entity *se; | |
10001 | struct rq *rq; | |
10002 | int i; | |
10003 | ||
10004 | for_each_possible_cpu(i) { | |
10005 | rq = cpu_rq(i); | |
10006 | se = tg->se[i]; | |
10007 | ||
10008 | raw_spin_lock_irq(&rq->lock); | |
4126bad6 | 10009 | update_rq_clock(rq); |
d0326691 | 10010 | attach_entity_cfs_rq(se); |
55e16d30 | 10011 | sync_throttle(tg, i); |
8663e24d PZ |
10012 | raw_spin_unlock_irq(&rq->lock); |
10013 | } | |
10014 | } | |
10015 | ||
6fe1f348 | 10016 | void unregister_fair_sched_group(struct task_group *tg) |
029632fb | 10017 | { |
029632fb | 10018 | unsigned long flags; |
6fe1f348 PZ |
10019 | struct rq *rq; |
10020 | int cpu; | |
029632fb | 10021 | |
6fe1f348 PZ |
10022 | for_each_possible_cpu(cpu) { |
10023 | if (tg->se[cpu]) | |
10024 | remove_entity_load_avg(tg->se[cpu]); | |
029632fb | 10025 | |
6fe1f348 PZ |
10026 | /* |
10027 | * Only empty task groups can be destroyed; so we can speculatively | |
10028 | * check on_list without danger of it being re-added. | |
10029 | */ | |
10030 | if (!tg->cfs_rq[cpu]->on_list) | |
10031 | continue; | |
10032 | ||
10033 | rq = cpu_rq(cpu); | |
10034 | ||
10035 | raw_spin_lock_irqsave(&rq->lock, flags); | |
10036 | list_del_leaf_cfs_rq(tg->cfs_rq[cpu]); | |
10037 | raw_spin_unlock_irqrestore(&rq->lock, flags); | |
10038 | } | |
029632fb PZ |
10039 | } |
10040 | ||
10041 | void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq, | |
10042 | struct sched_entity *se, int cpu, | |
10043 | struct sched_entity *parent) | |
10044 | { | |
10045 | struct rq *rq = cpu_rq(cpu); | |
10046 | ||
10047 | cfs_rq->tg = tg; | |
10048 | cfs_rq->rq = rq; | |
029632fb PZ |
10049 | init_cfs_rq_runtime(cfs_rq); |
10050 | ||
10051 | tg->cfs_rq[cpu] = cfs_rq; | |
10052 | tg->se[cpu] = se; | |
10053 | ||
10054 | /* se could be NULL for root_task_group */ | |
10055 | if (!se) | |
10056 | return; | |
10057 | ||
fed14d45 | 10058 | if (!parent) { |
029632fb | 10059 | se->cfs_rq = &rq->cfs; |
fed14d45 PZ |
10060 | se->depth = 0; |
10061 | } else { | |
029632fb | 10062 | se->cfs_rq = parent->my_q; |
fed14d45 PZ |
10063 | se->depth = parent->depth + 1; |
10064 | } | |
029632fb PZ |
10065 | |
10066 | se->my_q = cfs_rq; | |
0ac9b1c2 PT |
10067 | /* guarantee group entities always have weight */ |
10068 | update_load_set(&se->load, NICE_0_LOAD); | |
029632fb PZ |
10069 | se->parent = parent; |
10070 | } | |
10071 | ||
10072 | static DEFINE_MUTEX(shares_mutex); | |
10073 | ||
10074 | int sched_group_set_shares(struct task_group *tg, unsigned long shares) | |
10075 | { | |
10076 | int i; | |
029632fb PZ |
10077 | |
10078 | /* | |
10079 | * We can't change the weight of the root cgroup. | |
10080 | */ | |
10081 | if (!tg->se[0]) | |
10082 | return -EINVAL; | |
10083 | ||
10084 | shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES)); | |
10085 | ||
10086 | mutex_lock(&shares_mutex); | |
10087 | if (tg->shares == shares) | |
10088 | goto done; | |
10089 | ||
10090 | tg->shares = shares; | |
10091 | for_each_possible_cpu(i) { | |
10092 | struct rq *rq = cpu_rq(i); | |
8a8c69c3 PZ |
10093 | struct sched_entity *se = tg->se[i]; |
10094 | struct rq_flags rf; | |
029632fb | 10095 | |
029632fb | 10096 | /* Propagate contribution to hierarchy */ |
8a8c69c3 | 10097 | rq_lock_irqsave(rq, &rf); |
71b1da46 | 10098 | update_rq_clock(rq); |
89ee048f | 10099 | for_each_sched_entity(se) { |
88c0616e | 10100 | update_load_avg(cfs_rq_of(se), se, UPDATE_TG); |
1ea6c46a | 10101 | update_cfs_group(se); |
89ee048f | 10102 | } |
8a8c69c3 | 10103 | rq_unlock_irqrestore(rq, &rf); |
029632fb PZ |
10104 | } |
10105 | ||
10106 | done: | |
10107 | mutex_unlock(&shares_mutex); | |
10108 | return 0; | |
10109 | } | |
10110 | #else /* CONFIG_FAIR_GROUP_SCHED */ | |
10111 | ||
10112 | void free_fair_sched_group(struct task_group *tg) { } | |
10113 | ||
10114 | int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent) | |
10115 | { | |
10116 | return 1; | |
10117 | } | |
10118 | ||
8663e24d PZ |
10119 | void online_fair_sched_group(struct task_group *tg) { } |
10120 | ||
6fe1f348 | 10121 | void unregister_fair_sched_group(struct task_group *tg) { } |
029632fb PZ |
10122 | |
10123 | #endif /* CONFIG_FAIR_GROUP_SCHED */ | |
10124 | ||
810b3817 | 10125 | |
6d686f45 | 10126 | static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task) |
0d721cea PW |
10127 | { |
10128 | struct sched_entity *se = &task->se; | |
0d721cea PW |
10129 | unsigned int rr_interval = 0; |
10130 | ||
10131 | /* | |
10132 | * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise | |
10133 | * idle runqueue: | |
10134 | */ | |
0d721cea | 10135 | if (rq->cfs.load.weight) |
a59f4e07 | 10136 | rr_interval = NS_TO_JIFFIES(sched_slice(cfs_rq_of(se), se)); |
0d721cea PW |
10137 | |
10138 | return rr_interval; | |
10139 | } | |
10140 | ||
bf0f6f24 IM |
10141 | /* |
10142 | * All the scheduling class methods: | |
10143 | */ | |
029632fb | 10144 | const struct sched_class fair_sched_class = { |
5522d5d5 | 10145 | .next = &idle_sched_class, |
bf0f6f24 IM |
10146 | .enqueue_task = enqueue_task_fair, |
10147 | .dequeue_task = dequeue_task_fair, | |
10148 | .yield_task = yield_task_fair, | |
d95f4122 | 10149 | .yield_to_task = yield_to_task_fair, |
bf0f6f24 | 10150 | |
2e09bf55 | 10151 | .check_preempt_curr = check_preempt_wakeup, |
bf0f6f24 IM |
10152 | |
10153 | .pick_next_task = pick_next_task_fair, | |
10154 | .put_prev_task = put_prev_task_fair, | |
10155 | ||
681f3e68 | 10156 | #ifdef CONFIG_SMP |
4ce72a2c | 10157 | .select_task_rq = select_task_rq_fair, |
0a74bef8 | 10158 | .migrate_task_rq = migrate_task_rq_fair, |
141965c7 | 10159 | |
0bcdcf28 CE |
10160 | .rq_online = rq_online_fair, |
10161 | .rq_offline = rq_offline_fair, | |
88ec22d3 | 10162 | |
12695578 | 10163 | .task_dead = task_dead_fair, |
c5b28038 | 10164 | .set_cpus_allowed = set_cpus_allowed_common, |
681f3e68 | 10165 | #endif |
bf0f6f24 | 10166 | |
83b699ed | 10167 | .set_curr_task = set_curr_task_fair, |
bf0f6f24 | 10168 | .task_tick = task_tick_fair, |
cd29fe6f | 10169 | .task_fork = task_fork_fair, |
cb469845 SR |
10170 | |
10171 | .prio_changed = prio_changed_fair, | |
da7a735e | 10172 | .switched_from = switched_from_fair, |
cb469845 | 10173 | .switched_to = switched_to_fair, |
810b3817 | 10174 | |
0d721cea PW |
10175 | .get_rr_interval = get_rr_interval_fair, |
10176 | ||
6e998916 SG |
10177 | .update_curr = update_curr_fair, |
10178 | ||
810b3817 | 10179 | #ifdef CONFIG_FAIR_GROUP_SCHED |
ea86cb4b | 10180 | .task_change_group = task_change_group_fair, |
810b3817 | 10181 | #endif |
bf0f6f24 IM |
10182 | }; |
10183 | ||
10184 | #ifdef CONFIG_SCHED_DEBUG | |
029632fb | 10185 | void print_cfs_stats(struct seq_file *m, int cpu) |
bf0f6f24 | 10186 | { |
a9e7f654 | 10187 | struct cfs_rq *cfs_rq, *pos; |
bf0f6f24 | 10188 | |
5973e5b9 | 10189 | rcu_read_lock(); |
a9e7f654 | 10190 | for_each_leaf_cfs_rq_safe(cpu_rq(cpu), cfs_rq, pos) |
5cef9eca | 10191 | print_cfs_rq(m, cpu, cfs_rq); |
5973e5b9 | 10192 | rcu_read_unlock(); |
bf0f6f24 | 10193 | } |
397f2378 SD |
10194 | |
10195 | #ifdef CONFIG_NUMA_BALANCING | |
10196 | void show_numa_stats(struct task_struct *p, struct seq_file *m) | |
10197 | { | |
10198 | int node; | |
10199 | unsigned long tsf = 0, tpf = 0, gsf = 0, gpf = 0; | |
10200 | ||
10201 | for_each_online_node(node) { | |
10202 | if (p->numa_faults) { | |
10203 | tsf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 0)]; | |
10204 | tpf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 1)]; | |
10205 | } | |
10206 | if (p->numa_group) { | |
10207 | gsf = p->numa_group->faults[task_faults_idx(NUMA_MEM, node, 0)], | |
10208 | gpf = p->numa_group->faults[task_faults_idx(NUMA_MEM, node, 1)]; | |
10209 | } | |
10210 | print_numa_stats(m, node, tsf, tpf, gsf, gpf); | |
10211 | } | |
10212 | } | |
10213 | #endif /* CONFIG_NUMA_BALANCING */ | |
10214 | #endif /* CONFIG_SCHED_DEBUG */ | |
029632fb PZ |
10215 | |
10216 | __init void init_sched_fair_class(void) | |
10217 | { | |
10218 | #ifdef CONFIG_SMP | |
10219 | open_softirq(SCHED_SOFTIRQ, run_rebalance_domains); | |
10220 | ||
3451d024 | 10221 | #ifdef CONFIG_NO_HZ_COMMON |
554cecaf | 10222 | nohz.next_balance = jiffies; |
f643ea22 | 10223 | nohz.next_blocked = jiffies; |
029632fb | 10224 | zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT); |
029632fb PZ |
10225 | #endif |
10226 | #endif /* SMP */ | |
10227 | ||
10228 | } |