]>
Commit | Line | Data |
---|---|---|
bf0f6f24 IM |
1 | /* |
2 | * Completely Fair Scheduling (CFS) Class (SCHED_NORMAL/SCHED_BATCH) | |
3 | * | |
4 | * Copyright (C) 2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> | |
5 | * | |
6 | * Interactivity improvements by Mike Galbraith | |
7 | * (C) 2007 Mike Galbraith <efault@gmx.de> | |
8 | * | |
9 | * Various enhancements by Dmitry Adamushko. | |
10 | * (C) 2007 Dmitry Adamushko <dmitry.adamushko@gmail.com> | |
11 | * | |
12 | * Group scheduling enhancements by Srivatsa Vaddagiri | |
13 | * Copyright IBM Corporation, 2007 | |
14 | * Author: Srivatsa Vaddagiri <vatsa@linux.vnet.ibm.com> | |
15 | * | |
16 | * Scaled math optimizations by Thomas Gleixner | |
17 | * Copyright (C) 2007, Thomas Gleixner <tglx@linutronix.de> | |
21805085 PZ |
18 | * |
19 | * Adaptive scheduling granularity, math enhancements by Peter Zijlstra | |
90eec103 | 20 | * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra |
bf0f6f24 IM |
21 | */ |
22 | ||
1983a922 | 23 | #include <linux/sched.h> |
cb251765 | 24 | #include <linux/latencytop.h> |
3436ae12 | 25 | #include <linux/cpumask.h> |
83a0a96a | 26 | #include <linux/cpuidle.h> |
029632fb PZ |
27 | #include <linux/slab.h> |
28 | #include <linux/profile.h> | |
29 | #include <linux/interrupt.h> | |
cbee9f88 | 30 | #include <linux/mempolicy.h> |
e14808b4 | 31 | #include <linux/migrate.h> |
cbee9f88 | 32 | #include <linux/task_work.h> |
029632fb PZ |
33 | |
34 | #include <trace/events/sched.h> | |
35 | ||
36 | #include "sched.h" | |
9745512c | 37 | |
bf0f6f24 | 38 | /* |
21805085 | 39 | * Targeted preemption latency for CPU-bound tasks: |
864616ee | 40 | * (default: 6ms * (1 + ilog(ncpus)), units: nanoseconds) |
bf0f6f24 | 41 | * |
21805085 | 42 | * NOTE: this latency value is not the same as the concept of |
d274a4ce IM |
43 | * 'timeslice length' - timeslices in CFS are of variable length |
44 | * and have no persistent notion like in traditional, time-slice | |
45 | * based scheduling concepts. | |
bf0f6f24 | 46 | * |
d274a4ce IM |
47 | * (to see the precise effective timeslice length of your workload, |
48 | * run vmstat and monitor the context-switches (cs) field) | |
bf0f6f24 | 49 | */ |
21406928 MG |
50 | unsigned int sysctl_sched_latency = 6000000ULL; |
51 | unsigned int normalized_sysctl_sched_latency = 6000000ULL; | |
2bd8e6d4 | 52 | |
1983a922 CE |
53 | /* |
54 | * The initial- and re-scaling of tunables is configurable | |
55 | * (default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus)) | |
56 | * | |
57 | * Options are: | |
58 | * SCHED_TUNABLESCALING_NONE - unscaled, always *1 | |
59 | * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus) | |
60 | * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus | |
61 | */ | |
62 | enum sched_tunable_scaling sysctl_sched_tunable_scaling | |
63 | = SCHED_TUNABLESCALING_LOG; | |
64 | ||
2bd8e6d4 | 65 | /* |
b2be5e96 | 66 | * Minimal preemption granularity for CPU-bound tasks: |
864616ee | 67 | * (default: 0.75 msec * (1 + ilog(ncpus)), units: nanoseconds) |
2bd8e6d4 | 68 | */ |
0bf377bb IM |
69 | unsigned int sysctl_sched_min_granularity = 750000ULL; |
70 | unsigned int normalized_sysctl_sched_min_granularity = 750000ULL; | |
21805085 PZ |
71 | |
72 | /* | |
b2be5e96 PZ |
73 | * is kept at sysctl_sched_latency / sysctl_sched_min_granularity |
74 | */ | |
0bf377bb | 75 | static unsigned int sched_nr_latency = 8; |
b2be5e96 PZ |
76 | |
77 | /* | |
2bba22c5 | 78 | * After fork, child runs first. If set to 0 (default) then |
b2be5e96 | 79 | * parent will (try to) run first. |
21805085 | 80 | */ |
2bba22c5 | 81 | unsigned int sysctl_sched_child_runs_first __read_mostly; |
bf0f6f24 | 82 | |
bf0f6f24 IM |
83 | /* |
84 | * SCHED_OTHER wake-up granularity. | |
172e082a | 85 | * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds) |
bf0f6f24 IM |
86 | * |
87 | * This option delays the preemption effects of decoupled workloads | |
88 | * and reduces their over-scheduling. Synchronous workloads will still | |
89 | * have immediate wakeup/sleep latencies. | |
90 | */ | |
172e082a | 91 | unsigned int sysctl_sched_wakeup_granularity = 1000000UL; |
0bcdcf28 | 92 | unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL; |
bf0f6f24 | 93 | |
da84d961 IM |
94 | const_debug unsigned int sysctl_sched_migration_cost = 500000UL; |
95 | ||
ec12cb7f PT |
96 | #ifdef CONFIG_CFS_BANDWIDTH |
97 | /* | |
98 | * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool | |
99 | * each time a cfs_rq requests quota. | |
100 | * | |
101 | * Note: in the case that the slice exceeds the runtime remaining (either due | |
102 | * to consumption or the quota being specified to be smaller than the slice) | |
103 | * we will always only issue the remaining available time. | |
104 | * | |
105 | * default: 5 msec, units: microseconds | |
106 | */ | |
107 | unsigned int sysctl_sched_cfs_bandwidth_slice = 5000UL; | |
108 | #endif | |
109 | ||
3273163c MR |
110 | /* |
111 | * The margin used when comparing utilization with CPU capacity: | |
893c5d22 | 112 | * util * margin < capacity * 1024 |
3273163c MR |
113 | */ |
114 | unsigned int capacity_margin = 1280; /* ~20% */ | |
115 | ||
8527632d PG |
116 | static inline void update_load_add(struct load_weight *lw, unsigned long inc) |
117 | { | |
118 | lw->weight += inc; | |
119 | lw->inv_weight = 0; | |
120 | } | |
121 | ||
122 | static inline void update_load_sub(struct load_weight *lw, unsigned long dec) | |
123 | { | |
124 | lw->weight -= dec; | |
125 | lw->inv_weight = 0; | |
126 | } | |
127 | ||
128 | static inline void update_load_set(struct load_weight *lw, unsigned long w) | |
129 | { | |
130 | lw->weight = w; | |
131 | lw->inv_weight = 0; | |
132 | } | |
133 | ||
029632fb PZ |
134 | /* |
135 | * Increase the granularity value when there are more CPUs, | |
136 | * because with more CPUs the 'effective latency' as visible | |
137 | * to users decreases. But the relationship is not linear, | |
138 | * so pick a second-best guess by going with the log2 of the | |
139 | * number of CPUs. | |
140 | * | |
141 | * This idea comes from the SD scheduler of Con Kolivas: | |
142 | */ | |
58ac93e4 | 143 | static unsigned int get_update_sysctl_factor(void) |
029632fb | 144 | { |
58ac93e4 | 145 | unsigned int cpus = min_t(unsigned int, num_online_cpus(), 8); |
029632fb PZ |
146 | unsigned int factor; |
147 | ||
148 | switch (sysctl_sched_tunable_scaling) { | |
149 | case SCHED_TUNABLESCALING_NONE: | |
150 | factor = 1; | |
151 | break; | |
152 | case SCHED_TUNABLESCALING_LINEAR: | |
153 | factor = cpus; | |
154 | break; | |
155 | case SCHED_TUNABLESCALING_LOG: | |
156 | default: | |
157 | factor = 1 + ilog2(cpus); | |
158 | break; | |
159 | } | |
160 | ||
161 | return factor; | |
162 | } | |
163 | ||
164 | static void update_sysctl(void) | |
165 | { | |
166 | unsigned int factor = get_update_sysctl_factor(); | |
167 | ||
168 | #define SET_SYSCTL(name) \ | |
169 | (sysctl_##name = (factor) * normalized_sysctl_##name) | |
170 | SET_SYSCTL(sched_min_granularity); | |
171 | SET_SYSCTL(sched_latency); | |
172 | SET_SYSCTL(sched_wakeup_granularity); | |
173 | #undef SET_SYSCTL | |
174 | } | |
175 | ||
176 | void sched_init_granularity(void) | |
177 | { | |
178 | update_sysctl(); | |
179 | } | |
180 | ||
9dbdb155 | 181 | #define WMULT_CONST (~0U) |
029632fb PZ |
182 | #define WMULT_SHIFT 32 |
183 | ||
9dbdb155 PZ |
184 | static void __update_inv_weight(struct load_weight *lw) |
185 | { | |
186 | unsigned long w; | |
187 | ||
188 | if (likely(lw->inv_weight)) | |
189 | return; | |
190 | ||
191 | w = scale_load_down(lw->weight); | |
192 | ||
193 | if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST)) | |
194 | lw->inv_weight = 1; | |
195 | else if (unlikely(!w)) | |
196 | lw->inv_weight = WMULT_CONST; | |
197 | else | |
198 | lw->inv_weight = WMULT_CONST / w; | |
199 | } | |
029632fb PZ |
200 | |
201 | /* | |
9dbdb155 PZ |
202 | * delta_exec * weight / lw.weight |
203 | * OR | |
204 | * (delta_exec * (weight * lw->inv_weight)) >> WMULT_SHIFT | |
205 | * | |
1c3de5e1 | 206 | * Either weight := NICE_0_LOAD and lw \e sched_prio_to_wmult[], in which case |
9dbdb155 PZ |
207 | * we're guaranteed shift stays positive because inv_weight is guaranteed to |
208 | * fit 32 bits, and NICE_0_LOAD gives another 10 bits; therefore shift >= 22. | |
209 | * | |
210 | * Or, weight =< lw.weight (because lw.weight is the runqueue weight), thus | |
211 | * weight/lw.weight <= 1, and therefore our shift will also be positive. | |
029632fb | 212 | */ |
9dbdb155 | 213 | static u64 __calc_delta(u64 delta_exec, unsigned long weight, struct load_weight *lw) |
029632fb | 214 | { |
9dbdb155 PZ |
215 | u64 fact = scale_load_down(weight); |
216 | int shift = WMULT_SHIFT; | |
029632fb | 217 | |
9dbdb155 | 218 | __update_inv_weight(lw); |
029632fb | 219 | |
9dbdb155 PZ |
220 | if (unlikely(fact >> 32)) { |
221 | while (fact >> 32) { | |
222 | fact >>= 1; | |
223 | shift--; | |
224 | } | |
029632fb PZ |
225 | } |
226 | ||
9dbdb155 PZ |
227 | /* hint to use a 32x32->64 mul */ |
228 | fact = (u64)(u32)fact * lw->inv_weight; | |
029632fb | 229 | |
9dbdb155 PZ |
230 | while (fact >> 32) { |
231 | fact >>= 1; | |
232 | shift--; | |
233 | } | |
029632fb | 234 | |
9dbdb155 | 235 | return mul_u64_u32_shr(delta_exec, fact, shift); |
029632fb PZ |
236 | } |
237 | ||
238 | ||
239 | const struct sched_class fair_sched_class; | |
a4c2f00f | 240 | |
bf0f6f24 IM |
241 | /************************************************************** |
242 | * CFS operations on generic schedulable entities: | |
243 | */ | |
244 | ||
62160e3f | 245 | #ifdef CONFIG_FAIR_GROUP_SCHED |
bf0f6f24 | 246 | |
62160e3f | 247 | /* cpu runqueue to which this cfs_rq is attached */ |
bf0f6f24 IM |
248 | static inline struct rq *rq_of(struct cfs_rq *cfs_rq) |
249 | { | |
62160e3f | 250 | return cfs_rq->rq; |
bf0f6f24 IM |
251 | } |
252 | ||
62160e3f IM |
253 | /* An entity is a task if it doesn't "own" a runqueue */ |
254 | #define entity_is_task(se) (!se->my_q) | |
bf0f6f24 | 255 | |
8f48894f PZ |
256 | static inline struct task_struct *task_of(struct sched_entity *se) |
257 | { | |
9148a3a1 | 258 | SCHED_WARN_ON(!entity_is_task(se)); |
8f48894f PZ |
259 | return container_of(se, struct task_struct, se); |
260 | } | |
261 | ||
b758149c PZ |
262 | /* Walk up scheduling entities hierarchy */ |
263 | #define for_each_sched_entity(se) \ | |
264 | for (; se; se = se->parent) | |
265 | ||
266 | static inline struct cfs_rq *task_cfs_rq(struct task_struct *p) | |
267 | { | |
268 | return p->se.cfs_rq; | |
269 | } | |
270 | ||
271 | /* runqueue on which this entity is (to be) queued */ | |
272 | static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se) | |
273 | { | |
274 | return se->cfs_rq; | |
275 | } | |
276 | ||
277 | /* runqueue "owned" by this group */ | |
278 | static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp) | |
279 | { | |
280 | return grp->my_q; | |
281 | } | |
282 | ||
3d4b47b4 PZ |
283 | static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq) |
284 | { | |
285 | if (!cfs_rq->on_list) { | |
9c2791f9 VG |
286 | struct rq *rq = rq_of(cfs_rq); |
287 | int cpu = cpu_of(rq); | |
67e86250 PT |
288 | /* |
289 | * Ensure we either appear before our parent (if already | |
290 | * enqueued) or force our parent to appear after us when it is | |
9c2791f9 VG |
291 | * enqueued. The fact that we always enqueue bottom-up |
292 | * reduces this to two cases and a special case for the root | |
293 | * cfs_rq. Furthermore, it also means that we will always reset | |
294 | * tmp_alone_branch either when the branch is connected | |
295 | * to a tree or when we reach the beg of the tree | |
67e86250 PT |
296 | */ |
297 | if (cfs_rq->tg->parent && | |
9c2791f9 VG |
298 | cfs_rq->tg->parent->cfs_rq[cpu]->on_list) { |
299 | /* | |
300 | * If parent is already on the list, we add the child | |
301 | * just before. Thanks to circular linked property of | |
302 | * the list, this means to put the child at the tail | |
303 | * of the list that starts by parent. | |
304 | */ | |
305 | list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list, | |
306 | &(cfs_rq->tg->parent->cfs_rq[cpu]->leaf_cfs_rq_list)); | |
307 | /* | |
308 | * The branch is now connected to its tree so we can | |
309 | * reset tmp_alone_branch to the beginning of the | |
310 | * list. | |
311 | */ | |
312 | rq->tmp_alone_branch = &rq->leaf_cfs_rq_list; | |
313 | } else if (!cfs_rq->tg->parent) { | |
314 | /* | |
315 | * cfs rq without parent should be put | |
316 | * at the tail of the list. | |
317 | */ | |
67e86250 | 318 | list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list, |
9c2791f9 VG |
319 | &rq->leaf_cfs_rq_list); |
320 | /* | |
321 | * We have reach the beg of a tree so we can reset | |
322 | * tmp_alone_branch to the beginning of the list. | |
323 | */ | |
324 | rq->tmp_alone_branch = &rq->leaf_cfs_rq_list; | |
325 | } else { | |
326 | /* | |
327 | * The parent has not already been added so we want to | |
328 | * make sure that it will be put after us. | |
329 | * tmp_alone_branch points to the beg of the branch | |
330 | * where we will add parent. | |
331 | */ | |
332 | list_add_rcu(&cfs_rq->leaf_cfs_rq_list, | |
333 | rq->tmp_alone_branch); | |
334 | /* | |
335 | * update tmp_alone_branch to points to the new beg | |
336 | * of the branch | |
337 | */ | |
338 | rq->tmp_alone_branch = &cfs_rq->leaf_cfs_rq_list; | |
67e86250 | 339 | } |
3d4b47b4 PZ |
340 | |
341 | cfs_rq->on_list = 1; | |
342 | } | |
343 | } | |
344 | ||
345 | static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq) | |
346 | { | |
347 | if (cfs_rq->on_list) { | |
348 | list_del_rcu(&cfs_rq->leaf_cfs_rq_list); | |
349 | cfs_rq->on_list = 0; | |
350 | } | |
351 | } | |
352 | ||
b758149c PZ |
353 | /* Iterate thr' all leaf cfs_rq's on a runqueue */ |
354 | #define for_each_leaf_cfs_rq(rq, cfs_rq) \ | |
355 | list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list) | |
356 | ||
357 | /* Do the two (enqueued) entities belong to the same group ? */ | |
fed14d45 | 358 | static inline struct cfs_rq * |
b758149c PZ |
359 | is_same_group(struct sched_entity *se, struct sched_entity *pse) |
360 | { | |
361 | if (se->cfs_rq == pse->cfs_rq) | |
fed14d45 | 362 | return se->cfs_rq; |
b758149c | 363 | |
fed14d45 | 364 | return NULL; |
b758149c PZ |
365 | } |
366 | ||
367 | static inline struct sched_entity *parent_entity(struct sched_entity *se) | |
368 | { | |
369 | return se->parent; | |
370 | } | |
371 | ||
464b7527 PZ |
372 | static void |
373 | find_matching_se(struct sched_entity **se, struct sched_entity **pse) | |
374 | { | |
375 | int se_depth, pse_depth; | |
376 | ||
377 | /* | |
378 | * preemption test can be made between sibling entities who are in the | |
379 | * same cfs_rq i.e who have a common parent. Walk up the hierarchy of | |
380 | * both tasks until we find their ancestors who are siblings of common | |
381 | * parent. | |
382 | */ | |
383 | ||
384 | /* First walk up until both entities are at same depth */ | |
fed14d45 PZ |
385 | se_depth = (*se)->depth; |
386 | pse_depth = (*pse)->depth; | |
464b7527 PZ |
387 | |
388 | while (se_depth > pse_depth) { | |
389 | se_depth--; | |
390 | *se = parent_entity(*se); | |
391 | } | |
392 | ||
393 | while (pse_depth > se_depth) { | |
394 | pse_depth--; | |
395 | *pse = parent_entity(*pse); | |
396 | } | |
397 | ||
398 | while (!is_same_group(*se, *pse)) { | |
399 | *se = parent_entity(*se); | |
400 | *pse = parent_entity(*pse); | |
401 | } | |
402 | } | |
403 | ||
8f48894f PZ |
404 | #else /* !CONFIG_FAIR_GROUP_SCHED */ |
405 | ||
406 | static inline struct task_struct *task_of(struct sched_entity *se) | |
407 | { | |
408 | return container_of(se, struct task_struct, se); | |
409 | } | |
bf0f6f24 | 410 | |
62160e3f IM |
411 | static inline struct rq *rq_of(struct cfs_rq *cfs_rq) |
412 | { | |
413 | return container_of(cfs_rq, struct rq, cfs); | |
bf0f6f24 IM |
414 | } |
415 | ||
416 | #define entity_is_task(se) 1 | |
417 | ||
b758149c PZ |
418 | #define for_each_sched_entity(se) \ |
419 | for (; se; se = NULL) | |
bf0f6f24 | 420 | |
b758149c | 421 | static inline struct cfs_rq *task_cfs_rq(struct task_struct *p) |
bf0f6f24 | 422 | { |
b758149c | 423 | return &task_rq(p)->cfs; |
bf0f6f24 IM |
424 | } |
425 | ||
b758149c PZ |
426 | static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se) |
427 | { | |
428 | struct task_struct *p = task_of(se); | |
429 | struct rq *rq = task_rq(p); | |
430 | ||
431 | return &rq->cfs; | |
432 | } | |
433 | ||
434 | /* runqueue "owned" by this group */ | |
435 | static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp) | |
436 | { | |
437 | return NULL; | |
438 | } | |
439 | ||
3d4b47b4 PZ |
440 | static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq) |
441 | { | |
442 | } | |
443 | ||
444 | static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq) | |
445 | { | |
446 | } | |
447 | ||
b758149c PZ |
448 | #define for_each_leaf_cfs_rq(rq, cfs_rq) \ |
449 | for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL) | |
450 | ||
b758149c PZ |
451 | static inline struct sched_entity *parent_entity(struct sched_entity *se) |
452 | { | |
453 | return NULL; | |
454 | } | |
455 | ||
464b7527 PZ |
456 | static inline void |
457 | find_matching_se(struct sched_entity **se, struct sched_entity **pse) | |
458 | { | |
459 | } | |
460 | ||
b758149c PZ |
461 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
462 | ||
6c16a6dc | 463 | static __always_inline |
9dbdb155 | 464 | void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec); |
bf0f6f24 IM |
465 | |
466 | /************************************************************** | |
467 | * Scheduling class tree data structure manipulation methods: | |
468 | */ | |
469 | ||
1bf08230 | 470 | static inline u64 max_vruntime(u64 max_vruntime, u64 vruntime) |
02e0431a | 471 | { |
1bf08230 | 472 | s64 delta = (s64)(vruntime - max_vruntime); |
368059a9 | 473 | if (delta > 0) |
1bf08230 | 474 | max_vruntime = vruntime; |
02e0431a | 475 | |
1bf08230 | 476 | return max_vruntime; |
02e0431a PZ |
477 | } |
478 | ||
0702e3eb | 479 | static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime) |
b0ffd246 PZ |
480 | { |
481 | s64 delta = (s64)(vruntime - min_vruntime); | |
482 | if (delta < 0) | |
483 | min_vruntime = vruntime; | |
484 | ||
485 | return min_vruntime; | |
486 | } | |
487 | ||
54fdc581 FC |
488 | static inline int entity_before(struct sched_entity *a, |
489 | struct sched_entity *b) | |
490 | { | |
491 | return (s64)(a->vruntime - b->vruntime) < 0; | |
492 | } | |
493 | ||
1af5f730 PZ |
494 | static void update_min_vruntime(struct cfs_rq *cfs_rq) |
495 | { | |
b60205c7 PZ |
496 | struct sched_entity *curr = cfs_rq->curr; |
497 | ||
1af5f730 PZ |
498 | u64 vruntime = cfs_rq->min_vruntime; |
499 | ||
b60205c7 PZ |
500 | if (curr) { |
501 | if (curr->on_rq) | |
502 | vruntime = curr->vruntime; | |
503 | else | |
504 | curr = NULL; | |
505 | } | |
1af5f730 PZ |
506 | |
507 | if (cfs_rq->rb_leftmost) { | |
508 | struct sched_entity *se = rb_entry(cfs_rq->rb_leftmost, | |
509 | struct sched_entity, | |
510 | run_node); | |
511 | ||
b60205c7 | 512 | if (!curr) |
1af5f730 PZ |
513 | vruntime = se->vruntime; |
514 | else | |
515 | vruntime = min_vruntime(vruntime, se->vruntime); | |
516 | } | |
517 | ||
1bf08230 | 518 | /* ensure we never gain time by being placed backwards. */ |
1af5f730 | 519 | cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime); |
3fe1698b PZ |
520 | #ifndef CONFIG_64BIT |
521 | smp_wmb(); | |
522 | cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime; | |
523 | #endif | |
1af5f730 PZ |
524 | } |
525 | ||
bf0f6f24 IM |
526 | /* |
527 | * Enqueue an entity into the rb-tree: | |
528 | */ | |
0702e3eb | 529 | static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 IM |
530 | { |
531 | struct rb_node **link = &cfs_rq->tasks_timeline.rb_node; | |
532 | struct rb_node *parent = NULL; | |
533 | struct sched_entity *entry; | |
bf0f6f24 IM |
534 | int leftmost = 1; |
535 | ||
536 | /* | |
537 | * Find the right place in the rbtree: | |
538 | */ | |
539 | while (*link) { | |
540 | parent = *link; | |
541 | entry = rb_entry(parent, struct sched_entity, run_node); | |
542 | /* | |
543 | * We dont care about collisions. Nodes with | |
544 | * the same key stay together. | |
545 | */ | |
2bd2d6f2 | 546 | if (entity_before(se, entry)) { |
bf0f6f24 IM |
547 | link = &parent->rb_left; |
548 | } else { | |
549 | link = &parent->rb_right; | |
550 | leftmost = 0; | |
551 | } | |
552 | } | |
553 | ||
554 | /* | |
555 | * Maintain a cache of leftmost tree entries (it is frequently | |
556 | * used): | |
557 | */ | |
1af5f730 | 558 | if (leftmost) |
57cb499d | 559 | cfs_rq->rb_leftmost = &se->run_node; |
bf0f6f24 IM |
560 | |
561 | rb_link_node(&se->run_node, parent, link); | |
562 | rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline); | |
bf0f6f24 IM |
563 | } |
564 | ||
0702e3eb | 565 | static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 | 566 | { |
3fe69747 PZ |
567 | if (cfs_rq->rb_leftmost == &se->run_node) { |
568 | struct rb_node *next_node; | |
3fe69747 PZ |
569 | |
570 | next_node = rb_next(&se->run_node); | |
571 | cfs_rq->rb_leftmost = next_node; | |
3fe69747 | 572 | } |
e9acbff6 | 573 | |
bf0f6f24 | 574 | rb_erase(&se->run_node, &cfs_rq->tasks_timeline); |
bf0f6f24 IM |
575 | } |
576 | ||
029632fb | 577 | struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq) |
bf0f6f24 | 578 | { |
f4b6755f PZ |
579 | struct rb_node *left = cfs_rq->rb_leftmost; |
580 | ||
581 | if (!left) | |
582 | return NULL; | |
583 | ||
584 | return rb_entry(left, struct sched_entity, run_node); | |
bf0f6f24 IM |
585 | } |
586 | ||
ac53db59 RR |
587 | static struct sched_entity *__pick_next_entity(struct sched_entity *se) |
588 | { | |
589 | struct rb_node *next = rb_next(&se->run_node); | |
590 | ||
591 | if (!next) | |
592 | return NULL; | |
593 | ||
594 | return rb_entry(next, struct sched_entity, run_node); | |
595 | } | |
596 | ||
597 | #ifdef CONFIG_SCHED_DEBUG | |
029632fb | 598 | struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq) |
aeb73b04 | 599 | { |
7eee3e67 | 600 | struct rb_node *last = rb_last(&cfs_rq->tasks_timeline); |
aeb73b04 | 601 | |
70eee74b BS |
602 | if (!last) |
603 | return NULL; | |
7eee3e67 IM |
604 | |
605 | return rb_entry(last, struct sched_entity, run_node); | |
aeb73b04 PZ |
606 | } |
607 | ||
bf0f6f24 IM |
608 | /************************************************************** |
609 | * Scheduling class statistics methods: | |
610 | */ | |
611 | ||
acb4a848 | 612 | int sched_proc_update_handler(struct ctl_table *table, int write, |
8d65af78 | 613 | void __user *buffer, size_t *lenp, |
b2be5e96 PZ |
614 | loff_t *ppos) |
615 | { | |
8d65af78 | 616 | int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
58ac93e4 | 617 | unsigned int factor = get_update_sysctl_factor(); |
b2be5e96 PZ |
618 | |
619 | if (ret || !write) | |
620 | return ret; | |
621 | ||
622 | sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency, | |
623 | sysctl_sched_min_granularity); | |
624 | ||
acb4a848 CE |
625 | #define WRT_SYSCTL(name) \ |
626 | (normalized_sysctl_##name = sysctl_##name / (factor)) | |
627 | WRT_SYSCTL(sched_min_granularity); | |
628 | WRT_SYSCTL(sched_latency); | |
629 | WRT_SYSCTL(sched_wakeup_granularity); | |
acb4a848 CE |
630 | #undef WRT_SYSCTL |
631 | ||
b2be5e96 PZ |
632 | return 0; |
633 | } | |
634 | #endif | |
647e7cac | 635 | |
a7be37ac | 636 | /* |
f9c0b095 | 637 | * delta /= w |
a7be37ac | 638 | */ |
9dbdb155 | 639 | static inline u64 calc_delta_fair(u64 delta, struct sched_entity *se) |
a7be37ac | 640 | { |
f9c0b095 | 641 | if (unlikely(se->load.weight != NICE_0_LOAD)) |
9dbdb155 | 642 | delta = __calc_delta(delta, NICE_0_LOAD, &se->load); |
a7be37ac PZ |
643 | |
644 | return delta; | |
645 | } | |
646 | ||
647e7cac IM |
647 | /* |
648 | * The idea is to set a period in which each task runs once. | |
649 | * | |
532b1858 | 650 | * When there are too many tasks (sched_nr_latency) we have to stretch |
647e7cac IM |
651 | * this period because otherwise the slices get too small. |
652 | * | |
653 | * p = (nr <= nl) ? l : l*nr/nl | |
654 | */ | |
4d78e7b6 PZ |
655 | static u64 __sched_period(unsigned long nr_running) |
656 | { | |
8e2b0bf3 BF |
657 | if (unlikely(nr_running > sched_nr_latency)) |
658 | return nr_running * sysctl_sched_min_granularity; | |
659 | else | |
660 | return sysctl_sched_latency; | |
4d78e7b6 PZ |
661 | } |
662 | ||
647e7cac IM |
663 | /* |
664 | * We calculate the wall-time slice from the period by taking a part | |
665 | * proportional to the weight. | |
666 | * | |
f9c0b095 | 667 | * s = p*P[w/rw] |
647e7cac | 668 | */ |
6d0f0ebd | 669 | static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se) |
21805085 | 670 | { |
0a582440 | 671 | u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq); |
f9c0b095 | 672 | |
0a582440 | 673 | for_each_sched_entity(se) { |
6272d68c | 674 | struct load_weight *load; |
3104bf03 | 675 | struct load_weight lw; |
6272d68c LM |
676 | |
677 | cfs_rq = cfs_rq_of(se); | |
678 | load = &cfs_rq->load; | |
f9c0b095 | 679 | |
0a582440 | 680 | if (unlikely(!se->on_rq)) { |
3104bf03 | 681 | lw = cfs_rq->load; |
0a582440 MG |
682 | |
683 | update_load_add(&lw, se->load.weight); | |
684 | load = &lw; | |
685 | } | |
9dbdb155 | 686 | slice = __calc_delta(slice, se->load.weight, load); |
0a582440 MG |
687 | } |
688 | return slice; | |
bf0f6f24 IM |
689 | } |
690 | ||
647e7cac | 691 | /* |
660cc00f | 692 | * We calculate the vruntime slice of a to-be-inserted task. |
647e7cac | 693 | * |
f9c0b095 | 694 | * vs = s/w |
647e7cac | 695 | */ |
f9c0b095 | 696 | static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se) |
67e9fb2a | 697 | { |
f9c0b095 | 698 | return calc_delta_fair(sched_slice(cfs_rq, se), se); |
a7be37ac PZ |
699 | } |
700 | ||
a75cdaa9 | 701 | #ifdef CONFIG_SMP |
772bd008 | 702 | static int select_idle_sibling(struct task_struct *p, int prev_cpu, int cpu); |
fb13c7ee MG |
703 | static unsigned long task_h_load(struct task_struct *p); |
704 | ||
9d89c257 YD |
705 | /* |
706 | * We choose a half-life close to 1 scheduling period. | |
84fb5a18 LY |
707 | * Note: The tables runnable_avg_yN_inv and runnable_avg_yN_sum are |
708 | * dependent on this value. | |
9d89c257 YD |
709 | */ |
710 | #define LOAD_AVG_PERIOD 32 | |
711 | #define LOAD_AVG_MAX 47742 /* maximum possible load avg */ | |
84fb5a18 | 712 | #define LOAD_AVG_MAX_N 345 /* number of full periods to produce LOAD_AVG_MAX */ |
a75cdaa9 | 713 | |
540247fb YD |
714 | /* Give new sched_entity start runnable values to heavy its load in infant time */ |
715 | void init_entity_runnable_average(struct sched_entity *se) | |
a75cdaa9 | 716 | { |
540247fb | 717 | struct sched_avg *sa = &se->avg; |
a75cdaa9 | 718 | |
9d89c257 YD |
719 | sa->last_update_time = 0; |
720 | /* | |
721 | * sched_avg's period_contrib should be strictly less then 1024, so | |
722 | * we give it 1023 to make sure it is almost a period (1024us), and | |
723 | * will definitely be update (after enqueue). | |
724 | */ | |
725 | sa->period_contrib = 1023; | |
b5a9b340 VG |
726 | /* |
727 | * Tasks are intialized with full load to be seen as heavy tasks until | |
728 | * they get a chance to stabilize to their real load level. | |
729 | * Group entities are intialized with zero load to reflect the fact that | |
730 | * nothing has been attached to the task group yet. | |
731 | */ | |
732 | if (entity_is_task(se)) | |
733 | sa->load_avg = scale_load_down(se->load.weight); | |
9d89c257 | 734 | sa->load_sum = sa->load_avg * LOAD_AVG_MAX; |
2b8c41da YD |
735 | /* |
736 | * At this point, util_avg won't be used in select_task_rq_fair anyway | |
737 | */ | |
738 | sa->util_avg = 0; | |
739 | sa->util_sum = 0; | |
9d89c257 | 740 | /* when this task enqueue'ed, it will contribute to its cfs_rq's load_avg */ |
a75cdaa9 | 741 | } |
7ea241af | 742 | |
7dc603c9 | 743 | static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq); |
df217913 | 744 | static void attach_entity_cfs_rq(struct sched_entity *se); |
7dc603c9 | 745 | |
2b8c41da YD |
746 | /* |
747 | * With new tasks being created, their initial util_avgs are extrapolated | |
748 | * based on the cfs_rq's current util_avg: | |
749 | * | |
750 | * util_avg = cfs_rq->util_avg / (cfs_rq->load_avg + 1) * se.load.weight | |
751 | * | |
752 | * However, in many cases, the above util_avg does not give a desired | |
753 | * value. Moreover, the sum of the util_avgs may be divergent, such | |
754 | * as when the series is a harmonic series. | |
755 | * | |
756 | * To solve this problem, we also cap the util_avg of successive tasks to | |
757 | * only 1/2 of the left utilization budget: | |
758 | * | |
759 | * util_avg_cap = (1024 - cfs_rq->avg.util_avg) / 2^n | |
760 | * | |
761 | * where n denotes the nth task. | |
762 | * | |
763 | * For example, a simplest series from the beginning would be like: | |
764 | * | |
765 | * task util_avg: 512, 256, 128, 64, 32, 16, 8, ... | |
766 | * cfs_rq util_avg: 512, 768, 896, 960, 992, 1008, 1016, ... | |
767 | * | |
768 | * Finally, that extrapolated util_avg is clamped to the cap (util_avg_cap) | |
769 | * if util_avg > util_avg_cap. | |
770 | */ | |
771 | void post_init_entity_util_avg(struct sched_entity *se) | |
772 | { | |
773 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
774 | struct sched_avg *sa = &se->avg; | |
172895e6 | 775 | long cap = (long)(SCHED_CAPACITY_SCALE - cfs_rq->avg.util_avg) / 2; |
2b8c41da YD |
776 | |
777 | if (cap > 0) { | |
778 | if (cfs_rq->avg.util_avg != 0) { | |
779 | sa->util_avg = cfs_rq->avg.util_avg * se->load.weight; | |
780 | sa->util_avg /= (cfs_rq->avg.load_avg + 1); | |
781 | ||
782 | if (sa->util_avg > cap) | |
783 | sa->util_avg = cap; | |
784 | } else { | |
785 | sa->util_avg = cap; | |
786 | } | |
787 | sa->util_sum = sa->util_avg * LOAD_AVG_MAX; | |
788 | } | |
7dc603c9 PZ |
789 | |
790 | if (entity_is_task(se)) { | |
791 | struct task_struct *p = task_of(se); | |
792 | if (p->sched_class != &fair_sched_class) { | |
793 | /* | |
794 | * For !fair tasks do: | |
795 | * | |
796 | update_cfs_rq_load_avg(now, cfs_rq, false); | |
797 | attach_entity_load_avg(cfs_rq, se); | |
798 | switched_from_fair(rq, p); | |
799 | * | |
800 | * such that the next switched_to_fair() has the | |
801 | * expected state. | |
802 | */ | |
df217913 | 803 | se->avg.last_update_time = cfs_rq_clock_task(cfs_rq); |
7dc603c9 PZ |
804 | return; |
805 | } | |
806 | } | |
807 | ||
df217913 | 808 | attach_entity_cfs_rq(se); |
2b8c41da YD |
809 | } |
810 | ||
7dc603c9 | 811 | #else /* !CONFIG_SMP */ |
540247fb | 812 | void init_entity_runnable_average(struct sched_entity *se) |
a75cdaa9 AS |
813 | { |
814 | } | |
2b8c41da YD |
815 | void post_init_entity_util_avg(struct sched_entity *se) |
816 | { | |
817 | } | |
3d30544f PZ |
818 | static void update_tg_load_avg(struct cfs_rq *cfs_rq, int force) |
819 | { | |
820 | } | |
7dc603c9 | 821 | #endif /* CONFIG_SMP */ |
a75cdaa9 | 822 | |
bf0f6f24 | 823 | /* |
9dbdb155 | 824 | * Update the current task's runtime statistics. |
bf0f6f24 | 825 | */ |
b7cc0896 | 826 | static void update_curr(struct cfs_rq *cfs_rq) |
bf0f6f24 | 827 | { |
429d43bc | 828 | struct sched_entity *curr = cfs_rq->curr; |
78becc27 | 829 | u64 now = rq_clock_task(rq_of(cfs_rq)); |
9dbdb155 | 830 | u64 delta_exec; |
bf0f6f24 IM |
831 | |
832 | if (unlikely(!curr)) | |
833 | return; | |
834 | ||
9dbdb155 PZ |
835 | delta_exec = now - curr->exec_start; |
836 | if (unlikely((s64)delta_exec <= 0)) | |
34f28ecd | 837 | return; |
bf0f6f24 | 838 | |
8ebc91d9 | 839 | curr->exec_start = now; |
d842de87 | 840 | |
9dbdb155 PZ |
841 | schedstat_set(curr->statistics.exec_max, |
842 | max(delta_exec, curr->statistics.exec_max)); | |
843 | ||
844 | curr->sum_exec_runtime += delta_exec; | |
ae92882e | 845 | schedstat_add(cfs_rq->exec_clock, delta_exec); |
9dbdb155 PZ |
846 | |
847 | curr->vruntime += calc_delta_fair(delta_exec, curr); | |
848 | update_min_vruntime(cfs_rq); | |
849 | ||
d842de87 SV |
850 | if (entity_is_task(curr)) { |
851 | struct task_struct *curtask = task_of(curr); | |
852 | ||
f977bb49 | 853 | trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime); |
d842de87 | 854 | cpuacct_charge(curtask, delta_exec); |
f06febc9 | 855 | account_group_exec_runtime(curtask, delta_exec); |
d842de87 | 856 | } |
ec12cb7f PT |
857 | |
858 | account_cfs_rq_runtime(cfs_rq, delta_exec); | |
bf0f6f24 IM |
859 | } |
860 | ||
6e998916 SG |
861 | static void update_curr_fair(struct rq *rq) |
862 | { | |
863 | update_curr(cfs_rq_of(&rq->curr->se)); | |
864 | } | |
865 | ||
bf0f6f24 | 866 | static inline void |
5870db5b | 867 | update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 | 868 | { |
4fa8d299 JP |
869 | u64 wait_start, prev_wait_start; |
870 | ||
871 | if (!schedstat_enabled()) | |
872 | return; | |
873 | ||
874 | wait_start = rq_clock(rq_of(cfs_rq)); | |
875 | prev_wait_start = schedstat_val(se->statistics.wait_start); | |
3ea94de1 JP |
876 | |
877 | if (entity_is_task(se) && task_on_rq_migrating(task_of(se)) && | |
4fa8d299 JP |
878 | likely(wait_start > prev_wait_start)) |
879 | wait_start -= prev_wait_start; | |
3ea94de1 | 880 | |
4fa8d299 | 881 | schedstat_set(se->statistics.wait_start, wait_start); |
bf0f6f24 IM |
882 | } |
883 | ||
4fa8d299 | 884 | static inline void |
3ea94de1 JP |
885 | update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se) |
886 | { | |
887 | struct task_struct *p; | |
cb251765 MG |
888 | u64 delta; |
889 | ||
4fa8d299 JP |
890 | if (!schedstat_enabled()) |
891 | return; | |
892 | ||
893 | delta = rq_clock(rq_of(cfs_rq)) - schedstat_val(se->statistics.wait_start); | |
3ea94de1 JP |
894 | |
895 | if (entity_is_task(se)) { | |
896 | p = task_of(se); | |
897 | if (task_on_rq_migrating(p)) { | |
898 | /* | |
899 | * Preserve migrating task's wait time so wait_start | |
900 | * time stamp can be adjusted to accumulate wait time | |
901 | * prior to migration. | |
902 | */ | |
4fa8d299 | 903 | schedstat_set(se->statistics.wait_start, delta); |
3ea94de1 JP |
904 | return; |
905 | } | |
906 | trace_sched_stat_wait(p, delta); | |
907 | } | |
908 | ||
4fa8d299 JP |
909 | schedstat_set(se->statistics.wait_max, |
910 | max(schedstat_val(se->statistics.wait_max), delta)); | |
911 | schedstat_inc(se->statistics.wait_count); | |
912 | schedstat_add(se->statistics.wait_sum, delta); | |
913 | schedstat_set(se->statistics.wait_start, 0); | |
3ea94de1 | 914 | } |
3ea94de1 | 915 | |
4fa8d299 | 916 | static inline void |
1a3d027c JP |
917 | update_stats_enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se) |
918 | { | |
919 | struct task_struct *tsk = NULL; | |
4fa8d299 JP |
920 | u64 sleep_start, block_start; |
921 | ||
922 | if (!schedstat_enabled()) | |
923 | return; | |
924 | ||
925 | sleep_start = schedstat_val(se->statistics.sleep_start); | |
926 | block_start = schedstat_val(se->statistics.block_start); | |
1a3d027c JP |
927 | |
928 | if (entity_is_task(se)) | |
929 | tsk = task_of(se); | |
930 | ||
4fa8d299 JP |
931 | if (sleep_start) { |
932 | u64 delta = rq_clock(rq_of(cfs_rq)) - sleep_start; | |
1a3d027c JP |
933 | |
934 | if ((s64)delta < 0) | |
935 | delta = 0; | |
936 | ||
4fa8d299 JP |
937 | if (unlikely(delta > schedstat_val(se->statistics.sleep_max))) |
938 | schedstat_set(se->statistics.sleep_max, delta); | |
1a3d027c | 939 | |
4fa8d299 JP |
940 | schedstat_set(se->statistics.sleep_start, 0); |
941 | schedstat_add(se->statistics.sum_sleep_runtime, delta); | |
1a3d027c JP |
942 | |
943 | if (tsk) { | |
944 | account_scheduler_latency(tsk, delta >> 10, 1); | |
945 | trace_sched_stat_sleep(tsk, delta); | |
946 | } | |
947 | } | |
4fa8d299 JP |
948 | if (block_start) { |
949 | u64 delta = rq_clock(rq_of(cfs_rq)) - block_start; | |
1a3d027c JP |
950 | |
951 | if ((s64)delta < 0) | |
952 | delta = 0; | |
953 | ||
4fa8d299 JP |
954 | if (unlikely(delta > schedstat_val(se->statistics.block_max))) |
955 | schedstat_set(se->statistics.block_max, delta); | |
1a3d027c | 956 | |
4fa8d299 JP |
957 | schedstat_set(se->statistics.block_start, 0); |
958 | schedstat_add(se->statistics.sum_sleep_runtime, delta); | |
1a3d027c JP |
959 | |
960 | if (tsk) { | |
961 | if (tsk->in_iowait) { | |
4fa8d299 JP |
962 | schedstat_add(se->statistics.iowait_sum, delta); |
963 | schedstat_inc(se->statistics.iowait_count); | |
1a3d027c JP |
964 | trace_sched_stat_iowait(tsk, delta); |
965 | } | |
966 | ||
967 | trace_sched_stat_blocked(tsk, delta); | |
968 | ||
969 | /* | |
970 | * Blocking time is in units of nanosecs, so shift by | |
971 | * 20 to get a milliseconds-range estimation of the | |
972 | * amount of time that the task spent sleeping: | |
973 | */ | |
974 | if (unlikely(prof_on == SLEEP_PROFILING)) { | |
975 | profile_hits(SLEEP_PROFILING, | |
976 | (void *)get_wchan(tsk), | |
977 | delta >> 20); | |
978 | } | |
979 | account_scheduler_latency(tsk, delta >> 10, 0); | |
980 | } | |
981 | } | |
3ea94de1 | 982 | } |
3ea94de1 | 983 | |
bf0f6f24 IM |
984 | /* |
985 | * Task is being enqueued - update stats: | |
986 | */ | |
cb251765 | 987 | static inline void |
1a3d027c | 988 | update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
bf0f6f24 | 989 | { |
4fa8d299 JP |
990 | if (!schedstat_enabled()) |
991 | return; | |
992 | ||
bf0f6f24 IM |
993 | /* |
994 | * Are we enqueueing a waiting task? (for current tasks | |
995 | * a dequeue/enqueue event is a NOP) | |
996 | */ | |
429d43bc | 997 | if (se != cfs_rq->curr) |
5870db5b | 998 | update_stats_wait_start(cfs_rq, se); |
1a3d027c JP |
999 | |
1000 | if (flags & ENQUEUE_WAKEUP) | |
1001 | update_stats_enqueue_sleeper(cfs_rq, se); | |
bf0f6f24 IM |
1002 | } |
1003 | ||
bf0f6f24 | 1004 | static inline void |
cb251765 | 1005 | update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
bf0f6f24 | 1006 | { |
4fa8d299 JP |
1007 | |
1008 | if (!schedstat_enabled()) | |
1009 | return; | |
1010 | ||
bf0f6f24 IM |
1011 | /* |
1012 | * Mark the end of the wait period if dequeueing a | |
1013 | * waiting task: | |
1014 | */ | |
429d43bc | 1015 | if (se != cfs_rq->curr) |
9ef0a961 | 1016 | update_stats_wait_end(cfs_rq, se); |
cb251765 | 1017 | |
4fa8d299 JP |
1018 | if ((flags & DEQUEUE_SLEEP) && entity_is_task(se)) { |
1019 | struct task_struct *tsk = task_of(se); | |
cb251765 | 1020 | |
4fa8d299 JP |
1021 | if (tsk->state & TASK_INTERRUPTIBLE) |
1022 | schedstat_set(se->statistics.sleep_start, | |
1023 | rq_clock(rq_of(cfs_rq))); | |
1024 | if (tsk->state & TASK_UNINTERRUPTIBLE) | |
1025 | schedstat_set(se->statistics.block_start, | |
1026 | rq_clock(rq_of(cfs_rq))); | |
cb251765 | 1027 | } |
cb251765 MG |
1028 | } |
1029 | ||
bf0f6f24 IM |
1030 | /* |
1031 | * We are picking a new current task - update its stats: | |
1032 | */ | |
1033 | static inline void | |
79303e9e | 1034 | update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 IM |
1035 | { |
1036 | /* | |
1037 | * We are starting a new run period: | |
1038 | */ | |
78becc27 | 1039 | se->exec_start = rq_clock_task(rq_of(cfs_rq)); |
bf0f6f24 IM |
1040 | } |
1041 | ||
bf0f6f24 IM |
1042 | /************************************************** |
1043 | * Scheduling class queueing methods: | |
1044 | */ | |
1045 | ||
cbee9f88 PZ |
1046 | #ifdef CONFIG_NUMA_BALANCING |
1047 | /* | |
598f0ec0 MG |
1048 | * Approximate time to scan a full NUMA task in ms. The task scan period is |
1049 | * calculated based on the tasks virtual memory size and | |
1050 | * numa_balancing_scan_size. | |
cbee9f88 | 1051 | */ |
598f0ec0 MG |
1052 | unsigned int sysctl_numa_balancing_scan_period_min = 1000; |
1053 | unsigned int sysctl_numa_balancing_scan_period_max = 60000; | |
6e5fb223 PZ |
1054 | |
1055 | /* Portion of address space to scan in MB */ | |
1056 | unsigned int sysctl_numa_balancing_scan_size = 256; | |
cbee9f88 | 1057 | |
4b96a29b PZ |
1058 | /* Scan @scan_size MB every @scan_period after an initial @scan_delay in ms */ |
1059 | unsigned int sysctl_numa_balancing_scan_delay = 1000; | |
1060 | ||
598f0ec0 MG |
1061 | static unsigned int task_nr_scan_windows(struct task_struct *p) |
1062 | { | |
1063 | unsigned long rss = 0; | |
1064 | unsigned long nr_scan_pages; | |
1065 | ||
1066 | /* | |
1067 | * Calculations based on RSS as non-present and empty pages are skipped | |
1068 | * by the PTE scanner and NUMA hinting faults should be trapped based | |
1069 | * on resident pages | |
1070 | */ | |
1071 | nr_scan_pages = sysctl_numa_balancing_scan_size << (20 - PAGE_SHIFT); | |
1072 | rss = get_mm_rss(p->mm); | |
1073 | if (!rss) | |
1074 | rss = nr_scan_pages; | |
1075 | ||
1076 | rss = round_up(rss, nr_scan_pages); | |
1077 | return rss / nr_scan_pages; | |
1078 | } | |
1079 | ||
1080 | /* For sanitys sake, never scan more PTEs than MAX_SCAN_WINDOW MB/sec. */ | |
1081 | #define MAX_SCAN_WINDOW 2560 | |
1082 | ||
1083 | static unsigned int task_scan_min(struct task_struct *p) | |
1084 | { | |
316c1608 | 1085 | unsigned int scan_size = READ_ONCE(sysctl_numa_balancing_scan_size); |
598f0ec0 MG |
1086 | unsigned int scan, floor; |
1087 | unsigned int windows = 1; | |
1088 | ||
64192658 KT |
1089 | if (scan_size < MAX_SCAN_WINDOW) |
1090 | windows = MAX_SCAN_WINDOW / scan_size; | |
598f0ec0 MG |
1091 | floor = 1000 / windows; |
1092 | ||
1093 | scan = sysctl_numa_balancing_scan_period_min / task_nr_scan_windows(p); | |
1094 | return max_t(unsigned int, floor, scan); | |
1095 | } | |
1096 | ||
1097 | static unsigned int task_scan_max(struct task_struct *p) | |
1098 | { | |
1099 | unsigned int smin = task_scan_min(p); | |
1100 | unsigned int smax; | |
1101 | ||
1102 | /* Watch for min being lower than max due to floor calculations */ | |
1103 | smax = sysctl_numa_balancing_scan_period_max / task_nr_scan_windows(p); | |
1104 | return max(smin, smax); | |
1105 | } | |
1106 | ||
0ec8aa00 PZ |
1107 | static void account_numa_enqueue(struct rq *rq, struct task_struct *p) |
1108 | { | |
1109 | rq->nr_numa_running += (p->numa_preferred_nid != -1); | |
1110 | rq->nr_preferred_running += (p->numa_preferred_nid == task_node(p)); | |
1111 | } | |
1112 | ||
1113 | static void account_numa_dequeue(struct rq *rq, struct task_struct *p) | |
1114 | { | |
1115 | rq->nr_numa_running -= (p->numa_preferred_nid != -1); | |
1116 | rq->nr_preferred_running -= (p->numa_preferred_nid == task_node(p)); | |
1117 | } | |
1118 | ||
8c8a743c PZ |
1119 | struct numa_group { |
1120 | atomic_t refcount; | |
1121 | ||
1122 | spinlock_t lock; /* nr_tasks, tasks */ | |
1123 | int nr_tasks; | |
e29cf08b | 1124 | pid_t gid; |
4142c3eb | 1125 | int active_nodes; |
8c8a743c PZ |
1126 | |
1127 | struct rcu_head rcu; | |
989348b5 | 1128 | unsigned long total_faults; |
4142c3eb | 1129 | unsigned long max_faults_cpu; |
7e2703e6 RR |
1130 | /* |
1131 | * Faults_cpu is used to decide whether memory should move | |
1132 | * towards the CPU. As a consequence, these stats are weighted | |
1133 | * more by CPU use than by memory faults. | |
1134 | */ | |
50ec8a40 | 1135 | unsigned long *faults_cpu; |
989348b5 | 1136 | unsigned long faults[0]; |
8c8a743c PZ |
1137 | }; |
1138 | ||
be1e4e76 RR |
1139 | /* Shared or private faults. */ |
1140 | #define NR_NUMA_HINT_FAULT_TYPES 2 | |
1141 | ||
1142 | /* Memory and CPU locality */ | |
1143 | #define NR_NUMA_HINT_FAULT_STATS (NR_NUMA_HINT_FAULT_TYPES * 2) | |
1144 | ||
1145 | /* Averaged statistics, and temporary buffers. */ | |
1146 | #define NR_NUMA_HINT_FAULT_BUCKETS (NR_NUMA_HINT_FAULT_STATS * 2) | |
1147 | ||
e29cf08b MG |
1148 | pid_t task_numa_group_id(struct task_struct *p) |
1149 | { | |
1150 | return p->numa_group ? p->numa_group->gid : 0; | |
1151 | } | |
1152 | ||
44dba3d5 IM |
1153 | /* |
1154 | * The averaged statistics, shared & private, memory & cpu, | |
1155 | * occupy the first half of the array. The second half of the | |
1156 | * array is for current counters, which are averaged into the | |
1157 | * first set by task_numa_placement. | |
1158 | */ | |
1159 | static inline int task_faults_idx(enum numa_faults_stats s, int nid, int priv) | |
ac8e895b | 1160 | { |
44dba3d5 | 1161 | return NR_NUMA_HINT_FAULT_TYPES * (s * nr_node_ids + nid) + priv; |
ac8e895b MG |
1162 | } |
1163 | ||
1164 | static inline unsigned long task_faults(struct task_struct *p, int nid) | |
1165 | { | |
44dba3d5 | 1166 | if (!p->numa_faults) |
ac8e895b MG |
1167 | return 0; |
1168 | ||
44dba3d5 IM |
1169 | return p->numa_faults[task_faults_idx(NUMA_MEM, nid, 0)] + |
1170 | p->numa_faults[task_faults_idx(NUMA_MEM, nid, 1)]; | |
ac8e895b MG |
1171 | } |
1172 | ||
83e1d2cd MG |
1173 | static inline unsigned long group_faults(struct task_struct *p, int nid) |
1174 | { | |
1175 | if (!p->numa_group) | |
1176 | return 0; | |
1177 | ||
44dba3d5 IM |
1178 | return p->numa_group->faults[task_faults_idx(NUMA_MEM, nid, 0)] + |
1179 | p->numa_group->faults[task_faults_idx(NUMA_MEM, nid, 1)]; | |
83e1d2cd MG |
1180 | } |
1181 | ||
20e07dea RR |
1182 | static inline unsigned long group_faults_cpu(struct numa_group *group, int nid) |
1183 | { | |
44dba3d5 IM |
1184 | return group->faults_cpu[task_faults_idx(NUMA_MEM, nid, 0)] + |
1185 | group->faults_cpu[task_faults_idx(NUMA_MEM, nid, 1)]; | |
20e07dea RR |
1186 | } |
1187 | ||
4142c3eb RR |
1188 | /* |
1189 | * A node triggering more than 1/3 as many NUMA faults as the maximum is | |
1190 | * considered part of a numa group's pseudo-interleaving set. Migrations | |
1191 | * between these nodes are slowed down, to allow things to settle down. | |
1192 | */ | |
1193 | #define ACTIVE_NODE_FRACTION 3 | |
1194 | ||
1195 | static bool numa_is_active_node(int nid, struct numa_group *ng) | |
1196 | { | |
1197 | return group_faults_cpu(ng, nid) * ACTIVE_NODE_FRACTION > ng->max_faults_cpu; | |
1198 | } | |
1199 | ||
6c6b1193 RR |
1200 | /* Handle placement on systems where not all nodes are directly connected. */ |
1201 | static unsigned long score_nearby_nodes(struct task_struct *p, int nid, | |
1202 | int maxdist, bool task) | |
1203 | { | |
1204 | unsigned long score = 0; | |
1205 | int node; | |
1206 | ||
1207 | /* | |
1208 | * All nodes are directly connected, and the same distance | |
1209 | * from each other. No need for fancy placement algorithms. | |
1210 | */ | |
1211 | if (sched_numa_topology_type == NUMA_DIRECT) | |
1212 | return 0; | |
1213 | ||
1214 | /* | |
1215 | * This code is called for each node, introducing N^2 complexity, | |
1216 | * which should be ok given the number of nodes rarely exceeds 8. | |
1217 | */ | |
1218 | for_each_online_node(node) { | |
1219 | unsigned long faults; | |
1220 | int dist = node_distance(nid, node); | |
1221 | ||
1222 | /* | |
1223 | * The furthest away nodes in the system are not interesting | |
1224 | * for placement; nid was already counted. | |
1225 | */ | |
1226 | if (dist == sched_max_numa_distance || node == nid) | |
1227 | continue; | |
1228 | ||
1229 | /* | |
1230 | * On systems with a backplane NUMA topology, compare groups | |
1231 | * of nodes, and move tasks towards the group with the most | |
1232 | * memory accesses. When comparing two nodes at distance | |
1233 | * "hoplimit", only nodes closer by than "hoplimit" are part | |
1234 | * of each group. Skip other nodes. | |
1235 | */ | |
1236 | if (sched_numa_topology_type == NUMA_BACKPLANE && | |
1237 | dist > maxdist) | |
1238 | continue; | |
1239 | ||
1240 | /* Add up the faults from nearby nodes. */ | |
1241 | if (task) | |
1242 | faults = task_faults(p, node); | |
1243 | else | |
1244 | faults = group_faults(p, node); | |
1245 | ||
1246 | /* | |
1247 | * On systems with a glueless mesh NUMA topology, there are | |
1248 | * no fixed "groups of nodes". Instead, nodes that are not | |
1249 | * directly connected bounce traffic through intermediate | |
1250 | * nodes; a numa_group can occupy any set of nodes. | |
1251 | * The further away a node is, the less the faults count. | |
1252 | * This seems to result in good task placement. | |
1253 | */ | |
1254 | if (sched_numa_topology_type == NUMA_GLUELESS_MESH) { | |
1255 | faults *= (sched_max_numa_distance - dist); | |
1256 | faults /= (sched_max_numa_distance - LOCAL_DISTANCE); | |
1257 | } | |
1258 | ||
1259 | score += faults; | |
1260 | } | |
1261 | ||
1262 | return score; | |
1263 | } | |
1264 | ||
83e1d2cd MG |
1265 | /* |
1266 | * These return the fraction of accesses done by a particular task, or | |
1267 | * task group, on a particular numa node. The group weight is given a | |
1268 | * larger multiplier, in order to group tasks together that are almost | |
1269 | * evenly spread out between numa nodes. | |
1270 | */ | |
7bd95320 RR |
1271 | static inline unsigned long task_weight(struct task_struct *p, int nid, |
1272 | int dist) | |
83e1d2cd | 1273 | { |
7bd95320 | 1274 | unsigned long faults, total_faults; |
83e1d2cd | 1275 | |
44dba3d5 | 1276 | if (!p->numa_faults) |
83e1d2cd MG |
1277 | return 0; |
1278 | ||
1279 | total_faults = p->total_numa_faults; | |
1280 | ||
1281 | if (!total_faults) | |
1282 | return 0; | |
1283 | ||
7bd95320 | 1284 | faults = task_faults(p, nid); |
6c6b1193 RR |
1285 | faults += score_nearby_nodes(p, nid, dist, true); |
1286 | ||
7bd95320 | 1287 | return 1000 * faults / total_faults; |
83e1d2cd MG |
1288 | } |
1289 | ||
7bd95320 RR |
1290 | static inline unsigned long group_weight(struct task_struct *p, int nid, |
1291 | int dist) | |
83e1d2cd | 1292 | { |
7bd95320 RR |
1293 | unsigned long faults, total_faults; |
1294 | ||
1295 | if (!p->numa_group) | |
1296 | return 0; | |
1297 | ||
1298 | total_faults = p->numa_group->total_faults; | |
1299 | ||
1300 | if (!total_faults) | |
83e1d2cd MG |
1301 | return 0; |
1302 | ||
7bd95320 | 1303 | faults = group_faults(p, nid); |
6c6b1193 RR |
1304 | faults += score_nearby_nodes(p, nid, dist, false); |
1305 | ||
7bd95320 | 1306 | return 1000 * faults / total_faults; |
83e1d2cd MG |
1307 | } |
1308 | ||
10f39042 RR |
1309 | bool should_numa_migrate_memory(struct task_struct *p, struct page * page, |
1310 | int src_nid, int dst_cpu) | |
1311 | { | |
1312 | struct numa_group *ng = p->numa_group; | |
1313 | int dst_nid = cpu_to_node(dst_cpu); | |
1314 | int last_cpupid, this_cpupid; | |
1315 | ||
1316 | this_cpupid = cpu_pid_to_cpupid(dst_cpu, current->pid); | |
1317 | ||
1318 | /* | |
1319 | * Multi-stage node selection is used in conjunction with a periodic | |
1320 | * migration fault to build a temporal task<->page relation. By using | |
1321 | * a two-stage filter we remove short/unlikely relations. | |
1322 | * | |
1323 | * Using P(p) ~ n_p / n_t as per frequentist probability, we can equate | |
1324 | * a task's usage of a particular page (n_p) per total usage of this | |
1325 | * page (n_t) (in a given time-span) to a probability. | |
1326 | * | |
1327 | * Our periodic faults will sample this probability and getting the | |
1328 | * same result twice in a row, given these samples are fully | |
1329 | * independent, is then given by P(n)^2, provided our sample period | |
1330 | * is sufficiently short compared to the usage pattern. | |
1331 | * | |
1332 | * This quadric squishes small probabilities, making it less likely we | |
1333 | * act on an unlikely task<->page relation. | |
1334 | */ | |
1335 | last_cpupid = page_cpupid_xchg_last(page, this_cpupid); | |
1336 | if (!cpupid_pid_unset(last_cpupid) && | |
1337 | cpupid_to_nid(last_cpupid) != dst_nid) | |
1338 | return false; | |
1339 | ||
1340 | /* Always allow migrate on private faults */ | |
1341 | if (cpupid_match_pid(p, last_cpupid)) | |
1342 | return true; | |
1343 | ||
1344 | /* A shared fault, but p->numa_group has not been set up yet. */ | |
1345 | if (!ng) | |
1346 | return true; | |
1347 | ||
1348 | /* | |
4142c3eb RR |
1349 | * Destination node is much more heavily used than the source |
1350 | * node? Allow migration. | |
10f39042 | 1351 | */ |
4142c3eb RR |
1352 | if (group_faults_cpu(ng, dst_nid) > group_faults_cpu(ng, src_nid) * |
1353 | ACTIVE_NODE_FRACTION) | |
10f39042 RR |
1354 | return true; |
1355 | ||
1356 | /* | |
4142c3eb RR |
1357 | * Distribute memory according to CPU & memory use on each node, |
1358 | * with 3/4 hysteresis to avoid unnecessary memory migrations: | |
1359 | * | |
1360 | * faults_cpu(dst) 3 faults_cpu(src) | |
1361 | * --------------- * - > --------------- | |
1362 | * faults_mem(dst) 4 faults_mem(src) | |
10f39042 | 1363 | */ |
4142c3eb RR |
1364 | return group_faults_cpu(ng, dst_nid) * group_faults(p, src_nid) * 3 > |
1365 | group_faults_cpu(ng, src_nid) * group_faults(p, dst_nid) * 4; | |
10f39042 RR |
1366 | } |
1367 | ||
e6628d5b | 1368 | static unsigned long weighted_cpuload(const int cpu); |
58d081b5 MG |
1369 | static unsigned long source_load(int cpu, int type); |
1370 | static unsigned long target_load(int cpu, int type); | |
ced549fa | 1371 | static unsigned long capacity_of(int cpu); |
58d081b5 MG |
1372 | static long effective_load(struct task_group *tg, int cpu, long wl, long wg); |
1373 | ||
fb13c7ee | 1374 | /* Cached statistics for all CPUs within a node */ |
58d081b5 | 1375 | struct numa_stats { |
fb13c7ee | 1376 | unsigned long nr_running; |
58d081b5 | 1377 | unsigned long load; |
fb13c7ee MG |
1378 | |
1379 | /* Total compute capacity of CPUs on a node */ | |
5ef20ca1 | 1380 | unsigned long compute_capacity; |
fb13c7ee MG |
1381 | |
1382 | /* Approximate capacity in terms of runnable tasks on a node */ | |
5ef20ca1 | 1383 | unsigned long task_capacity; |
1b6a7495 | 1384 | int has_free_capacity; |
58d081b5 | 1385 | }; |
e6628d5b | 1386 | |
fb13c7ee MG |
1387 | /* |
1388 | * XXX borrowed from update_sg_lb_stats | |
1389 | */ | |
1390 | static void update_numa_stats(struct numa_stats *ns, int nid) | |
1391 | { | |
83d7f242 RR |
1392 | int smt, cpu, cpus = 0; |
1393 | unsigned long capacity; | |
fb13c7ee MG |
1394 | |
1395 | memset(ns, 0, sizeof(*ns)); | |
1396 | for_each_cpu(cpu, cpumask_of_node(nid)) { | |
1397 | struct rq *rq = cpu_rq(cpu); | |
1398 | ||
1399 | ns->nr_running += rq->nr_running; | |
1400 | ns->load += weighted_cpuload(cpu); | |
ced549fa | 1401 | ns->compute_capacity += capacity_of(cpu); |
5eca82a9 PZ |
1402 | |
1403 | cpus++; | |
fb13c7ee MG |
1404 | } |
1405 | ||
5eca82a9 PZ |
1406 | /* |
1407 | * If we raced with hotplug and there are no CPUs left in our mask | |
1408 | * the @ns structure is NULL'ed and task_numa_compare() will | |
1409 | * not find this node attractive. | |
1410 | * | |
1b6a7495 NP |
1411 | * We'll either bail at !has_free_capacity, or we'll detect a huge |
1412 | * imbalance and bail there. | |
5eca82a9 PZ |
1413 | */ |
1414 | if (!cpus) | |
1415 | return; | |
1416 | ||
83d7f242 RR |
1417 | /* smt := ceil(cpus / capacity), assumes: 1 < smt_power < 2 */ |
1418 | smt = DIV_ROUND_UP(SCHED_CAPACITY_SCALE * cpus, ns->compute_capacity); | |
1419 | capacity = cpus / smt; /* cores */ | |
1420 | ||
1421 | ns->task_capacity = min_t(unsigned, capacity, | |
1422 | DIV_ROUND_CLOSEST(ns->compute_capacity, SCHED_CAPACITY_SCALE)); | |
1b6a7495 | 1423 | ns->has_free_capacity = (ns->nr_running < ns->task_capacity); |
fb13c7ee MG |
1424 | } |
1425 | ||
58d081b5 MG |
1426 | struct task_numa_env { |
1427 | struct task_struct *p; | |
e6628d5b | 1428 | |
58d081b5 MG |
1429 | int src_cpu, src_nid; |
1430 | int dst_cpu, dst_nid; | |
e6628d5b | 1431 | |
58d081b5 | 1432 | struct numa_stats src_stats, dst_stats; |
e6628d5b | 1433 | |
40ea2b42 | 1434 | int imbalance_pct; |
7bd95320 | 1435 | int dist; |
fb13c7ee MG |
1436 | |
1437 | struct task_struct *best_task; | |
1438 | long best_imp; | |
58d081b5 MG |
1439 | int best_cpu; |
1440 | }; | |
1441 | ||
fb13c7ee MG |
1442 | static void task_numa_assign(struct task_numa_env *env, |
1443 | struct task_struct *p, long imp) | |
1444 | { | |
1445 | if (env->best_task) | |
1446 | put_task_struct(env->best_task); | |
bac78573 ON |
1447 | if (p) |
1448 | get_task_struct(p); | |
fb13c7ee MG |
1449 | |
1450 | env->best_task = p; | |
1451 | env->best_imp = imp; | |
1452 | env->best_cpu = env->dst_cpu; | |
1453 | } | |
1454 | ||
28a21745 | 1455 | static bool load_too_imbalanced(long src_load, long dst_load, |
e63da036 RR |
1456 | struct task_numa_env *env) |
1457 | { | |
e4991b24 RR |
1458 | long imb, old_imb; |
1459 | long orig_src_load, orig_dst_load; | |
28a21745 RR |
1460 | long src_capacity, dst_capacity; |
1461 | ||
1462 | /* | |
1463 | * The load is corrected for the CPU capacity available on each node. | |
1464 | * | |
1465 | * src_load dst_load | |
1466 | * ------------ vs --------- | |
1467 | * src_capacity dst_capacity | |
1468 | */ | |
1469 | src_capacity = env->src_stats.compute_capacity; | |
1470 | dst_capacity = env->dst_stats.compute_capacity; | |
e63da036 RR |
1471 | |
1472 | /* We care about the slope of the imbalance, not the direction. */ | |
e4991b24 RR |
1473 | if (dst_load < src_load) |
1474 | swap(dst_load, src_load); | |
e63da036 RR |
1475 | |
1476 | /* Is the difference below the threshold? */ | |
e4991b24 RR |
1477 | imb = dst_load * src_capacity * 100 - |
1478 | src_load * dst_capacity * env->imbalance_pct; | |
e63da036 RR |
1479 | if (imb <= 0) |
1480 | return false; | |
1481 | ||
1482 | /* | |
1483 | * The imbalance is above the allowed threshold. | |
e4991b24 | 1484 | * Compare it with the old imbalance. |
e63da036 | 1485 | */ |
28a21745 | 1486 | orig_src_load = env->src_stats.load; |
e4991b24 | 1487 | orig_dst_load = env->dst_stats.load; |
28a21745 | 1488 | |
e4991b24 RR |
1489 | if (orig_dst_load < orig_src_load) |
1490 | swap(orig_dst_load, orig_src_load); | |
e63da036 | 1491 | |
e4991b24 RR |
1492 | old_imb = orig_dst_load * src_capacity * 100 - |
1493 | orig_src_load * dst_capacity * env->imbalance_pct; | |
1494 | ||
1495 | /* Would this change make things worse? */ | |
1496 | return (imb > old_imb); | |
e63da036 RR |
1497 | } |
1498 | ||
fb13c7ee MG |
1499 | /* |
1500 | * This checks if the overall compute and NUMA accesses of the system would | |
1501 | * be improved if the source tasks was migrated to the target dst_cpu taking | |
1502 | * into account that it might be best if task running on the dst_cpu should | |
1503 | * be exchanged with the source task | |
1504 | */ | |
887c290e RR |
1505 | static void task_numa_compare(struct task_numa_env *env, |
1506 | long taskimp, long groupimp) | |
fb13c7ee MG |
1507 | { |
1508 | struct rq *src_rq = cpu_rq(env->src_cpu); | |
1509 | struct rq *dst_rq = cpu_rq(env->dst_cpu); | |
1510 | struct task_struct *cur; | |
28a21745 | 1511 | long src_load, dst_load; |
fb13c7ee | 1512 | long load; |
1c5d3eb3 | 1513 | long imp = env->p->numa_group ? groupimp : taskimp; |
0132c3e1 | 1514 | long moveimp = imp; |
7bd95320 | 1515 | int dist = env->dist; |
fb13c7ee MG |
1516 | |
1517 | rcu_read_lock(); | |
bac78573 ON |
1518 | cur = task_rcu_dereference(&dst_rq->curr); |
1519 | if (cur && ((cur->flags & PF_EXITING) || is_idle_task(cur))) | |
fb13c7ee MG |
1520 | cur = NULL; |
1521 | ||
7af68335 PZ |
1522 | /* |
1523 | * Because we have preemption enabled we can get migrated around and | |
1524 | * end try selecting ourselves (current == env->p) as a swap candidate. | |
1525 | */ | |
1526 | if (cur == env->p) | |
1527 | goto unlock; | |
1528 | ||
fb13c7ee MG |
1529 | /* |
1530 | * "imp" is the fault differential for the source task between the | |
1531 | * source and destination node. Calculate the total differential for | |
1532 | * the source task and potential destination task. The more negative | |
1533 | * the value is, the more rmeote accesses that would be expected to | |
1534 | * be incurred if the tasks were swapped. | |
1535 | */ | |
1536 | if (cur) { | |
1537 | /* Skip this swap candidate if cannot move to the source cpu */ | |
1538 | if (!cpumask_test_cpu(env->src_cpu, tsk_cpus_allowed(cur))) | |
1539 | goto unlock; | |
1540 | ||
887c290e RR |
1541 | /* |
1542 | * If dst and source tasks are in the same NUMA group, or not | |
ca28aa53 | 1543 | * in any group then look only at task weights. |
887c290e | 1544 | */ |
ca28aa53 | 1545 | if (cur->numa_group == env->p->numa_group) { |
7bd95320 RR |
1546 | imp = taskimp + task_weight(cur, env->src_nid, dist) - |
1547 | task_weight(cur, env->dst_nid, dist); | |
ca28aa53 RR |
1548 | /* |
1549 | * Add some hysteresis to prevent swapping the | |
1550 | * tasks within a group over tiny differences. | |
1551 | */ | |
1552 | if (cur->numa_group) | |
1553 | imp -= imp/16; | |
887c290e | 1554 | } else { |
ca28aa53 RR |
1555 | /* |
1556 | * Compare the group weights. If a task is all by | |
1557 | * itself (not part of a group), use the task weight | |
1558 | * instead. | |
1559 | */ | |
ca28aa53 | 1560 | if (cur->numa_group) |
7bd95320 RR |
1561 | imp += group_weight(cur, env->src_nid, dist) - |
1562 | group_weight(cur, env->dst_nid, dist); | |
ca28aa53 | 1563 | else |
7bd95320 RR |
1564 | imp += task_weight(cur, env->src_nid, dist) - |
1565 | task_weight(cur, env->dst_nid, dist); | |
887c290e | 1566 | } |
fb13c7ee MG |
1567 | } |
1568 | ||
0132c3e1 | 1569 | if (imp <= env->best_imp && moveimp <= env->best_imp) |
fb13c7ee MG |
1570 | goto unlock; |
1571 | ||
1572 | if (!cur) { | |
1573 | /* Is there capacity at our destination? */ | |
b932c03c | 1574 | if (env->src_stats.nr_running <= env->src_stats.task_capacity && |
1b6a7495 | 1575 | !env->dst_stats.has_free_capacity) |
fb13c7ee MG |
1576 | goto unlock; |
1577 | ||
1578 | goto balance; | |
1579 | } | |
1580 | ||
1581 | /* Balance doesn't matter much if we're running a task per cpu */ | |
0132c3e1 RR |
1582 | if (imp > env->best_imp && src_rq->nr_running == 1 && |
1583 | dst_rq->nr_running == 1) | |
fb13c7ee MG |
1584 | goto assign; |
1585 | ||
1586 | /* | |
1587 | * In the overloaded case, try and keep the load balanced. | |
1588 | */ | |
1589 | balance: | |
e720fff6 PZ |
1590 | load = task_h_load(env->p); |
1591 | dst_load = env->dst_stats.load + load; | |
1592 | src_load = env->src_stats.load - load; | |
fb13c7ee | 1593 | |
0132c3e1 RR |
1594 | if (moveimp > imp && moveimp > env->best_imp) { |
1595 | /* | |
1596 | * If the improvement from just moving env->p direction is | |
1597 | * better than swapping tasks around, check if a move is | |
1598 | * possible. Store a slightly smaller score than moveimp, | |
1599 | * so an actually idle CPU will win. | |
1600 | */ | |
1601 | if (!load_too_imbalanced(src_load, dst_load, env)) { | |
1602 | imp = moveimp - 1; | |
1603 | cur = NULL; | |
1604 | goto assign; | |
1605 | } | |
1606 | } | |
1607 | ||
1608 | if (imp <= env->best_imp) | |
1609 | goto unlock; | |
1610 | ||
fb13c7ee | 1611 | if (cur) { |
e720fff6 PZ |
1612 | load = task_h_load(cur); |
1613 | dst_load -= load; | |
1614 | src_load += load; | |
fb13c7ee MG |
1615 | } |
1616 | ||
28a21745 | 1617 | if (load_too_imbalanced(src_load, dst_load, env)) |
fb13c7ee MG |
1618 | goto unlock; |
1619 | ||
ba7e5a27 RR |
1620 | /* |
1621 | * One idle CPU per node is evaluated for a task numa move. | |
1622 | * Call select_idle_sibling to maybe find a better one. | |
1623 | */ | |
10e2f1ac PZ |
1624 | if (!cur) { |
1625 | /* | |
1626 | * select_idle_siblings() uses an per-cpu cpumask that | |
1627 | * can be used from IRQ context. | |
1628 | */ | |
1629 | local_irq_disable(); | |
772bd008 MR |
1630 | env->dst_cpu = select_idle_sibling(env->p, env->src_cpu, |
1631 | env->dst_cpu); | |
10e2f1ac PZ |
1632 | local_irq_enable(); |
1633 | } | |
ba7e5a27 | 1634 | |
fb13c7ee MG |
1635 | assign: |
1636 | task_numa_assign(env, cur, imp); | |
1637 | unlock: | |
1638 | rcu_read_unlock(); | |
1639 | } | |
1640 | ||
887c290e RR |
1641 | static void task_numa_find_cpu(struct task_numa_env *env, |
1642 | long taskimp, long groupimp) | |
2c8a50aa MG |
1643 | { |
1644 | int cpu; | |
1645 | ||
1646 | for_each_cpu(cpu, cpumask_of_node(env->dst_nid)) { | |
1647 | /* Skip this CPU if the source task cannot migrate */ | |
1648 | if (!cpumask_test_cpu(cpu, tsk_cpus_allowed(env->p))) | |
1649 | continue; | |
1650 | ||
1651 | env->dst_cpu = cpu; | |
887c290e | 1652 | task_numa_compare(env, taskimp, groupimp); |
2c8a50aa MG |
1653 | } |
1654 | } | |
1655 | ||
6f9aad0b RR |
1656 | /* Only move tasks to a NUMA node less busy than the current node. */ |
1657 | static bool numa_has_capacity(struct task_numa_env *env) | |
1658 | { | |
1659 | struct numa_stats *src = &env->src_stats; | |
1660 | struct numa_stats *dst = &env->dst_stats; | |
1661 | ||
1662 | if (src->has_free_capacity && !dst->has_free_capacity) | |
1663 | return false; | |
1664 | ||
1665 | /* | |
1666 | * Only consider a task move if the source has a higher load | |
1667 | * than the destination, corrected for CPU capacity on each node. | |
1668 | * | |
1669 | * src->load dst->load | |
1670 | * --------------------- vs --------------------- | |
1671 | * src->compute_capacity dst->compute_capacity | |
1672 | */ | |
44dcb04f SD |
1673 | if (src->load * dst->compute_capacity * env->imbalance_pct > |
1674 | ||
1675 | dst->load * src->compute_capacity * 100) | |
6f9aad0b RR |
1676 | return true; |
1677 | ||
1678 | return false; | |
1679 | } | |
1680 | ||
58d081b5 MG |
1681 | static int task_numa_migrate(struct task_struct *p) |
1682 | { | |
58d081b5 MG |
1683 | struct task_numa_env env = { |
1684 | .p = p, | |
fb13c7ee | 1685 | |
58d081b5 | 1686 | .src_cpu = task_cpu(p), |
b32e86b4 | 1687 | .src_nid = task_node(p), |
fb13c7ee MG |
1688 | |
1689 | .imbalance_pct = 112, | |
1690 | ||
1691 | .best_task = NULL, | |
1692 | .best_imp = 0, | |
4142c3eb | 1693 | .best_cpu = -1, |
58d081b5 MG |
1694 | }; |
1695 | struct sched_domain *sd; | |
887c290e | 1696 | unsigned long taskweight, groupweight; |
7bd95320 | 1697 | int nid, ret, dist; |
887c290e | 1698 | long taskimp, groupimp; |
e6628d5b | 1699 | |
58d081b5 | 1700 | /* |
fb13c7ee MG |
1701 | * Pick the lowest SD_NUMA domain, as that would have the smallest |
1702 | * imbalance and would be the first to start moving tasks about. | |
1703 | * | |
1704 | * And we want to avoid any moving of tasks about, as that would create | |
1705 | * random movement of tasks -- counter the numa conditions we're trying | |
1706 | * to satisfy here. | |
58d081b5 MG |
1707 | */ |
1708 | rcu_read_lock(); | |
fb13c7ee | 1709 | sd = rcu_dereference(per_cpu(sd_numa, env.src_cpu)); |
46a73e8a RR |
1710 | if (sd) |
1711 | env.imbalance_pct = 100 + (sd->imbalance_pct - 100) / 2; | |
e6628d5b MG |
1712 | rcu_read_unlock(); |
1713 | ||
46a73e8a RR |
1714 | /* |
1715 | * Cpusets can break the scheduler domain tree into smaller | |
1716 | * balance domains, some of which do not cross NUMA boundaries. | |
1717 | * Tasks that are "trapped" in such domains cannot be migrated | |
1718 | * elsewhere, so there is no point in (re)trying. | |
1719 | */ | |
1720 | if (unlikely(!sd)) { | |
de1b301a | 1721 | p->numa_preferred_nid = task_node(p); |
46a73e8a RR |
1722 | return -EINVAL; |
1723 | } | |
1724 | ||
2c8a50aa | 1725 | env.dst_nid = p->numa_preferred_nid; |
7bd95320 RR |
1726 | dist = env.dist = node_distance(env.src_nid, env.dst_nid); |
1727 | taskweight = task_weight(p, env.src_nid, dist); | |
1728 | groupweight = group_weight(p, env.src_nid, dist); | |
1729 | update_numa_stats(&env.src_stats, env.src_nid); | |
1730 | taskimp = task_weight(p, env.dst_nid, dist) - taskweight; | |
1731 | groupimp = group_weight(p, env.dst_nid, dist) - groupweight; | |
2c8a50aa | 1732 | update_numa_stats(&env.dst_stats, env.dst_nid); |
58d081b5 | 1733 | |
a43455a1 | 1734 | /* Try to find a spot on the preferred nid. */ |
6f9aad0b RR |
1735 | if (numa_has_capacity(&env)) |
1736 | task_numa_find_cpu(&env, taskimp, groupimp); | |
e1dda8a7 | 1737 | |
9de05d48 RR |
1738 | /* |
1739 | * Look at other nodes in these cases: | |
1740 | * - there is no space available on the preferred_nid | |
1741 | * - the task is part of a numa_group that is interleaved across | |
1742 | * multiple NUMA nodes; in order to better consolidate the group, | |
1743 | * we need to check other locations. | |
1744 | */ | |
4142c3eb | 1745 | if (env.best_cpu == -1 || (p->numa_group && p->numa_group->active_nodes > 1)) { |
2c8a50aa MG |
1746 | for_each_online_node(nid) { |
1747 | if (nid == env.src_nid || nid == p->numa_preferred_nid) | |
1748 | continue; | |
58d081b5 | 1749 | |
7bd95320 | 1750 | dist = node_distance(env.src_nid, env.dst_nid); |
6c6b1193 RR |
1751 | if (sched_numa_topology_type == NUMA_BACKPLANE && |
1752 | dist != env.dist) { | |
1753 | taskweight = task_weight(p, env.src_nid, dist); | |
1754 | groupweight = group_weight(p, env.src_nid, dist); | |
1755 | } | |
7bd95320 | 1756 | |
83e1d2cd | 1757 | /* Only consider nodes where both task and groups benefit */ |
7bd95320 RR |
1758 | taskimp = task_weight(p, nid, dist) - taskweight; |
1759 | groupimp = group_weight(p, nid, dist) - groupweight; | |
887c290e | 1760 | if (taskimp < 0 && groupimp < 0) |
fb13c7ee MG |
1761 | continue; |
1762 | ||
7bd95320 | 1763 | env.dist = dist; |
2c8a50aa MG |
1764 | env.dst_nid = nid; |
1765 | update_numa_stats(&env.dst_stats, env.dst_nid); | |
6f9aad0b RR |
1766 | if (numa_has_capacity(&env)) |
1767 | task_numa_find_cpu(&env, taskimp, groupimp); | |
58d081b5 MG |
1768 | } |
1769 | } | |
1770 | ||
68d1b02a RR |
1771 | /* |
1772 | * If the task is part of a workload that spans multiple NUMA nodes, | |
1773 | * and is migrating into one of the workload's active nodes, remember | |
1774 | * this node as the task's preferred numa node, so the workload can | |
1775 | * settle down. | |
1776 | * A task that migrated to a second choice node will be better off | |
1777 | * trying for a better one later. Do not set the preferred node here. | |
1778 | */ | |
db015dae | 1779 | if (p->numa_group) { |
4142c3eb RR |
1780 | struct numa_group *ng = p->numa_group; |
1781 | ||
db015dae RR |
1782 | if (env.best_cpu == -1) |
1783 | nid = env.src_nid; | |
1784 | else | |
1785 | nid = env.dst_nid; | |
1786 | ||
4142c3eb | 1787 | if (ng->active_nodes > 1 && numa_is_active_node(env.dst_nid, ng)) |
db015dae RR |
1788 | sched_setnuma(p, env.dst_nid); |
1789 | } | |
1790 | ||
1791 | /* No better CPU than the current one was found. */ | |
1792 | if (env.best_cpu == -1) | |
1793 | return -EAGAIN; | |
0ec8aa00 | 1794 | |
04bb2f94 RR |
1795 | /* |
1796 | * Reset the scan period if the task is being rescheduled on an | |
1797 | * alternative node to recheck if the tasks is now properly placed. | |
1798 | */ | |
1799 | p->numa_scan_period = task_scan_min(p); | |
1800 | ||
fb13c7ee | 1801 | if (env.best_task == NULL) { |
286549dc MG |
1802 | ret = migrate_task_to(p, env.best_cpu); |
1803 | if (ret != 0) | |
1804 | trace_sched_stick_numa(p, env.src_cpu, env.best_cpu); | |
fb13c7ee MG |
1805 | return ret; |
1806 | } | |
1807 | ||
1808 | ret = migrate_swap(p, env.best_task); | |
286549dc MG |
1809 | if (ret != 0) |
1810 | trace_sched_stick_numa(p, env.src_cpu, task_cpu(env.best_task)); | |
fb13c7ee MG |
1811 | put_task_struct(env.best_task); |
1812 | return ret; | |
e6628d5b MG |
1813 | } |
1814 | ||
6b9a7460 MG |
1815 | /* Attempt to migrate a task to a CPU on the preferred node. */ |
1816 | static void numa_migrate_preferred(struct task_struct *p) | |
1817 | { | |
5085e2a3 RR |
1818 | unsigned long interval = HZ; |
1819 | ||
2739d3ee | 1820 | /* This task has no NUMA fault statistics yet */ |
44dba3d5 | 1821 | if (unlikely(p->numa_preferred_nid == -1 || !p->numa_faults)) |
6b9a7460 MG |
1822 | return; |
1823 | ||
2739d3ee | 1824 | /* Periodically retry migrating the task to the preferred node */ |
5085e2a3 RR |
1825 | interval = min(interval, msecs_to_jiffies(p->numa_scan_period) / 16); |
1826 | p->numa_migrate_retry = jiffies + interval; | |
2739d3ee RR |
1827 | |
1828 | /* Success if task is already running on preferred CPU */ | |
de1b301a | 1829 | if (task_node(p) == p->numa_preferred_nid) |
6b9a7460 MG |
1830 | return; |
1831 | ||
1832 | /* Otherwise, try migrate to a CPU on the preferred node */ | |
2739d3ee | 1833 | task_numa_migrate(p); |
6b9a7460 MG |
1834 | } |
1835 | ||
20e07dea | 1836 | /* |
4142c3eb | 1837 | * Find out how many nodes on the workload is actively running on. Do this by |
20e07dea RR |
1838 | * tracking the nodes from which NUMA hinting faults are triggered. This can |
1839 | * be different from the set of nodes where the workload's memory is currently | |
1840 | * located. | |
20e07dea | 1841 | */ |
4142c3eb | 1842 | static void numa_group_count_active_nodes(struct numa_group *numa_group) |
20e07dea RR |
1843 | { |
1844 | unsigned long faults, max_faults = 0; | |
4142c3eb | 1845 | int nid, active_nodes = 0; |
20e07dea RR |
1846 | |
1847 | for_each_online_node(nid) { | |
1848 | faults = group_faults_cpu(numa_group, nid); | |
1849 | if (faults > max_faults) | |
1850 | max_faults = faults; | |
1851 | } | |
1852 | ||
1853 | for_each_online_node(nid) { | |
1854 | faults = group_faults_cpu(numa_group, nid); | |
4142c3eb RR |
1855 | if (faults * ACTIVE_NODE_FRACTION > max_faults) |
1856 | active_nodes++; | |
20e07dea | 1857 | } |
4142c3eb RR |
1858 | |
1859 | numa_group->max_faults_cpu = max_faults; | |
1860 | numa_group->active_nodes = active_nodes; | |
20e07dea RR |
1861 | } |
1862 | ||
04bb2f94 RR |
1863 | /* |
1864 | * When adapting the scan rate, the period is divided into NUMA_PERIOD_SLOTS | |
1865 | * increments. The more local the fault statistics are, the higher the scan | |
a22b4b01 RR |
1866 | * period will be for the next scan window. If local/(local+remote) ratio is |
1867 | * below NUMA_PERIOD_THRESHOLD (where range of ratio is 1..NUMA_PERIOD_SLOTS) | |
1868 | * the scan period will decrease. Aim for 70% local accesses. | |
04bb2f94 RR |
1869 | */ |
1870 | #define NUMA_PERIOD_SLOTS 10 | |
a22b4b01 | 1871 | #define NUMA_PERIOD_THRESHOLD 7 |
04bb2f94 RR |
1872 | |
1873 | /* | |
1874 | * Increase the scan period (slow down scanning) if the majority of | |
1875 | * our memory is already on our local node, or if the majority of | |
1876 | * the page accesses are shared with other processes. | |
1877 | * Otherwise, decrease the scan period. | |
1878 | */ | |
1879 | static void update_task_scan_period(struct task_struct *p, | |
1880 | unsigned long shared, unsigned long private) | |
1881 | { | |
1882 | unsigned int period_slot; | |
1883 | int ratio; | |
1884 | int diff; | |
1885 | ||
1886 | unsigned long remote = p->numa_faults_locality[0]; | |
1887 | unsigned long local = p->numa_faults_locality[1]; | |
1888 | ||
1889 | /* | |
1890 | * If there were no record hinting faults then either the task is | |
1891 | * completely idle or all activity is areas that are not of interest | |
074c2381 MG |
1892 | * to automatic numa balancing. Related to that, if there were failed |
1893 | * migration then it implies we are migrating too quickly or the local | |
1894 | * node is overloaded. In either case, scan slower | |
04bb2f94 | 1895 | */ |
074c2381 | 1896 | if (local + shared == 0 || p->numa_faults_locality[2]) { |
04bb2f94 RR |
1897 | p->numa_scan_period = min(p->numa_scan_period_max, |
1898 | p->numa_scan_period << 1); | |
1899 | ||
1900 | p->mm->numa_next_scan = jiffies + | |
1901 | msecs_to_jiffies(p->numa_scan_period); | |
1902 | ||
1903 | return; | |
1904 | } | |
1905 | ||
1906 | /* | |
1907 | * Prepare to scale scan period relative to the current period. | |
1908 | * == NUMA_PERIOD_THRESHOLD scan period stays the same | |
1909 | * < NUMA_PERIOD_THRESHOLD scan period decreases (scan faster) | |
1910 | * >= NUMA_PERIOD_THRESHOLD scan period increases (scan slower) | |
1911 | */ | |
1912 | period_slot = DIV_ROUND_UP(p->numa_scan_period, NUMA_PERIOD_SLOTS); | |
1913 | ratio = (local * NUMA_PERIOD_SLOTS) / (local + remote); | |
1914 | if (ratio >= NUMA_PERIOD_THRESHOLD) { | |
1915 | int slot = ratio - NUMA_PERIOD_THRESHOLD; | |
1916 | if (!slot) | |
1917 | slot = 1; | |
1918 | diff = slot * period_slot; | |
1919 | } else { | |
1920 | diff = -(NUMA_PERIOD_THRESHOLD - ratio) * period_slot; | |
1921 | ||
1922 | /* | |
1923 | * Scale scan rate increases based on sharing. There is an | |
1924 | * inverse relationship between the degree of sharing and | |
1925 | * the adjustment made to the scanning period. Broadly | |
1926 | * speaking the intent is that there is little point | |
1927 | * scanning faster if shared accesses dominate as it may | |
1928 | * simply bounce migrations uselessly | |
1929 | */ | |
2847c90e | 1930 | ratio = DIV_ROUND_UP(private * NUMA_PERIOD_SLOTS, (private + shared + 1)); |
04bb2f94 RR |
1931 | diff = (diff * ratio) / NUMA_PERIOD_SLOTS; |
1932 | } | |
1933 | ||
1934 | p->numa_scan_period = clamp(p->numa_scan_period + diff, | |
1935 | task_scan_min(p), task_scan_max(p)); | |
1936 | memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); | |
1937 | } | |
1938 | ||
7e2703e6 RR |
1939 | /* |
1940 | * Get the fraction of time the task has been running since the last | |
1941 | * NUMA placement cycle. The scheduler keeps similar statistics, but | |
1942 | * decays those on a 32ms period, which is orders of magnitude off | |
1943 | * from the dozens-of-seconds NUMA balancing period. Use the scheduler | |
1944 | * stats only if the task is so new there are no NUMA statistics yet. | |
1945 | */ | |
1946 | static u64 numa_get_avg_runtime(struct task_struct *p, u64 *period) | |
1947 | { | |
1948 | u64 runtime, delta, now; | |
1949 | /* Use the start of this time slice to avoid calculations. */ | |
1950 | now = p->se.exec_start; | |
1951 | runtime = p->se.sum_exec_runtime; | |
1952 | ||
1953 | if (p->last_task_numa_placement) { | |
1954 | delta = runtime - p->last_sum_exec_runtime; | |
1955 | *period = now - p->last_task_numa_placement; | |
1956 | } else { | |
9d89c257 YD |
1957 | delta = p->se.avg.load_sum / p->se.load.weight; |
1958 | *period = LOAD_AVG_MAX; | |
7e2703e6 RR |
1959 | } |
1960 | ||
1961 | p->last_sum_exec_runtime = runtime; | |
1962 | p->last_task_numa_placement = now; | |
1963 | ||
1964 | return delta; | |
1965 | } | |
1966 | ||
54009416 RR |
1967 | /* |
1968 | * Determine the preferred nid for a task in a numa_group. This needs to | |
1969 | * be done in a way that produces consistent results with group_weight, | |
1970 | * otherwise workloads might not converge. | |
1971 | */ | |
1972 | static int preferred_group_nid(struct task_struct *p, int nid) | |
1973 | { | |
1974 | nodemask_t nodes; | |
1975 | int dist; | |
1976 | ||
1977 | /* Direct connections between all NUMA nodes. */ | |
1978 | if (sched_numa_topology_type == NUMA_DIRECT) | |
1979 | return nid; | |
1980 | ||
1981 | /* | |
1982 | * On a system with glueless mesh NUMA topology, group_weight | |
1983 | * scores nodes according to the number of NUMA hinting faults on | |
1984 | * both the node itself, and on nearby nodes. | |
1985 | */ | |
1986 | if (sched_numa_topology_type == NUMA_GLUELESS_MESH) { | |
1987 | unsigned long score, max_score = 0; | |
1988 | int node, max_node = nid; | |
1989 | ||
1990 | dist = sched_max_numa_distance; | |
1991 | ||
1992 | for_each_online_node(node) { | |
1993 | score = group_weight(p, node, dist); | |
1994 | if (score > max_score) { | |
1995 | max_score = score; | |
1996 | max_node = node; | |
1997 | } | |
1998 | } | |
1999 | return max_node; | |
2000 | } | |
2001 | ||
2002 | /* | |
2003 | * Finding the preferred nid in a system with NUMA backplane | |
2004 | * interconnect topology is more involved. The goal is to locate | |
2005 | * tasks from numa_groups near each other in the system, and | |
2006 | * untangle workloads from different sides of the system. This requires | |
2007 | * searching down the hierarchy of node groups, recursively searching | |
2008 | * inside the highest scoring group of nodes. The nodemask tricks | |
2009 | * keep the complexity of the search down. | |
2010 | */ | |
2011 | nodes = node_online_map; | |
2012 | for (dist = sched_max_numa_distance; dist > LOCAL_DISTANCE; dist--) { | |
2013 | unsigned long max_faults = 0; | |
81907478 | 2014 | nodemask_t max_group = NODE_MASK_NONE; |
54009416 RR |
2015 | int a, b; |
2016 | ||
2017 | /* Are there nodes at this distance from each other? */ | |
2018 | if (!find_numa_distance(dist)) | |
2019 | continue; | |
2020 | ||
2021 | for_each_node_mask(a, nodes) { | |
2022 | unsigned long faults = 0; | |
2023 | nodemask_t this_group; | |
2024 | nodes_clear(this_group); | |
2025 | ||
2026 | /* Sum group's NUMA faults; includes a==b case. */ | |
2027 | for_each_node_mask(b, nodes) { | |
2028 | if (node_distance(a, b) < dist) { | |
2029 | faults += group_faults(p, b); | |
2030 | node_set(b, this_group); | |
2031 | node_clear(b, nodes); | |
2032 | } | |
2033 | } | |
2034 | ||
2035 | /* Remember the top group. */ | |
2036 | if (faults > max_faults) { | |
2037 | max_faults = faults; | |
2038 | max_group = this_group; | |
2039 | /* | |
2040 | * subtle: at the smallest distance there is | |
2041 | * just one node left in each "group", the | |
2042 | * winner is the preferred nid. | |
2043 | */ | |
2044 | nid = a; | |
2045 | } | |
2046 | } | |
2047 | /* Next round, evaluate the nodes within max_group. */ | |
890a5409 JB |
2048 | if (!max_faults) |
2049 | break; | |
54009416 RR |
2050 | nodes = max_group; |
2051 | } | |
2052 | return nid; | |
2053 | } | |
2054 | ||
cbee9f88 PZ |
2055 | static void task_numa_placement(struct task_struct *p) |
2056 | { | |
83e1d2cd MG |
2057 | int seq, nid, max_nid = -1, max_group_nid = -1; |
2058 | unsigned long max_faults = 0, max_group_faults = 0; | |
04bb2f94 | 2059 | unsigned long fault_types[2] = { 0, 0 }; |
7e2703e6 RR |
2060 | unsigned long total_faults; |
2061 | u64 runtime, period; | |
7dbd13ed | 2062 | spinlock_t *group_lock = NULL; |
cbee9f88 | 2063 | |
7e5a2c17 JL |
2064 | /* |
2065 | * The p->mm->numa_scan_seq field gets updated without | |
2066 | * exclusive access. Use READ_ONCE() here to ensure | |
2067 | * that the field is read in a single access: | |
2068 | */ | |
316c1608 | 2069 | seq = READ_ONCE(p->mm->numa_scan_seq); |
cbee9f88 PZ |
2070 | if (p->numa_scan_seq == seq) |
2071 | return; | |
2072 | p->numa_scan_seq = seq; | |
598f0ec0 | 2073 | p->numa_scan_period_max = task_scan_max(p); |
cbee9f88 | 2074 | |
7e2703e6 RR |
2075 | total_faults = p->numa_faults_locality[0] + |
2076 | p->numa_faults_locality[1]; | |
2077 | runtime = numa_get_avg_runtime(p, &period); | |
2078 | ||
7dbd13ed MG |
2079 | /* If the task is part of a group prevent parallel updates to group stats */ |
2080 | if (p->numa_group) { | |
2081 | group_lock = &p->numa_group->lock; | |
60e69eed | 2082 | spin_lock_irq(group_lock); |
7dbd13ed MG |
2083 | } |
2084 | ||
688b7585 MG |
2085 | /* Find the node with the highest number of faults */ |
2086 | for_each_online_node(nid) { | |
44dba3d5 IM |
2087 | /* Keep track of the offsets in numa_faults array */ |
2088 | int mem_idx, membuf_idx, cpu_idx, cpubuf_idx; | |
83e1d2cd | 2089 | unsigned long faults = 0, group_faults = 0; |
44dba3d5 | 2090 | int priv; |
745d6147 | 2091 | |
be1e4e76 | 2092 | for (priv = 0; priv < NR_NUMA_HINT_FAULT_TYPES; priv++) { |
7e2703e6 | 2093 | long diff, f_diff, f_weight; |
8c8a743c | 2094 | |
44dba3d5 IM |
2095 | mem_idx = task_faults_idx(NUMA_MEM, nid, priv); |
2096 | membuf_idx = task_faults_idx(NUMA_MEMBUF, nid, priv); | |
2097 | cpu_idx = task_faults_idx(NUMA_CPU, nid, priv); | |
2098 | cpubuf_idx = task_faults_idx(NUMA_CPUBUF, nid, priv); | |
745d6147 | 2099 | |
ac8e895b | 2100 | /* Decay existing window, copy faults since last scan */ |
44dba3d5 IM |
2101 | diff = p->numa_faults[membuf_idx] - p->numa_faults[mem_idx] / 2; |
2102 | fault_types[priv] += p->numa_faults[membuf_idx]; | |
2103 | p->numa_faults[membuf_idx] = 0; | |
fb13c7ee | 2104 | |
7e2703e6 RR |
2105 | /* |
2106 | * Normalize the faults_from, so all tasks in a group | |
2107 | * count according to CPU use, instead of by the raw | |
2108 | * number of faults. Tasks with little runtime have | |
2109 | * little over-all impact on throughput, and thus their | |
2110 | * faults are less important. | |
2111 | */ | |
2112 | f_weight = div64_u64(runtime << 16, period + 1); | |
44dba3d5 | 2113 | f_weight = (f_weight * p->numa_faults[cpubuf_idx]) / |
7e2703e6 | 2114 | (total_faults + 1); |
44dba3d5 IM |
2115 | f_diff = f_weight - p->numa_faults[cpu_idx] / 2; |
2116 | p->numa_faults[cpubuf_idx] = 0; | |
50ec8a40 | 2117 | |
44dba3d5 IM |
2118 | p->numa_faults[mem_idx] += diff; |
2119 | p->numa_faults[cpu_idx] += f_diff; | |
2120 | faults += p->numa_faults[mem_idx]; | |
83e1d2cd | 2121 | p->total_numa_faults += diff; |
8c8a743c | 2122 | if (p->numa_group) { |
44dba3d5 IM |
2123 | /* |
2124 | * safe because we can only change our own group | |
2125 | * | |
2126 | * mem_idx represents the offset for a given | |
2127 | * nid and priv in a specific region because it | |
2128 | * is at the beginning of the numa_faults array. | |
2129 | */ | |
2130 | p->numa_group->faults[mem_idx] += diff; | |
2131 | p->numa_group->faults_cpu[mem_idx] += f_diff; | |
989348b5 | 2132 | p->numa_group->total_faults += diff; |
44dba3d5 | 2133 | group_faults += p->numa_group->faults[mem_idx]; |
8c8a743c | 2134 | } |
ac8e895b MG |
2135 | } |
2136 | ||
688b7585 MG |
2137 | if (faults > max_faults) { |
2138 | max_faults = faults; | |
2139 | max_nid = nid; | |
2140 | } | |
83e1d2cd MG |
2141 | |
2142 | if (group_faults > max_group_faults) { | |
2143 | max_group_faults = group_faults; | |
2144 | max_group_nid = nid; | |
2145 | } | |
2146 | } | |
2147 | ||
04bb2f94 RR |
2148 | update_task_scan_period(p, fault_types[0], fault_types[1]); |
2149 | ||
7dbd13ed | 2150 | if (p->numa_group) { |
4142c3eb | 2151 | numa_group_count_active_nodes(p->numa_group); |
60e69eed | 2152 | spin_unlock_irq(group_lock); |
54009416 | 2153 | max_nid = preferred_group_nid(p, max_group_nid); |
688b7585 MG |
2154 | } |
2155 | ||
bb97fc31 RR |
2156 | if (max_faults) { |
2157 | /* Set the new preferred node */ | |
2158 | if (max_nid != p->numa_preferred_nid) | |
2159 | sched_setnuma(p, max_nid); | |
2160 | ||
2161 | if (task_node(p) != p->numa_preferred_nid) | |
2162 | numa_migrate_preferred(p); | |
3a7053b3 | 2163 | } |
cbee9f88 PZ |
2164 | } |
2165 | ||
8c8a743c PZ |
2166 | static inline int get_numa_group(struct numa_group *grp) |
2167 | { | |
2168 | return atomic_inc_not_zero(&grp->refcount); | |
2169 | } | |
2170 | ||
2171 | static inline void put_numa_group(struct numa_group *grp) | |
2172 | { | |
2173 | if (atomic_dec_and_test(&grp->refcount)) | |
2174 | kfree_rcu(grp, rcu); | |
2175 | } | |
2176 | ||
3e6a9418 MG |
2177 | static void task_numa_group(struct task_struct *p, int cpupid, int flags, |
2178 | int *priv) | |
8c8a743c PZ |
2179 | { |
2180 | struct numa_group *grp, *my_grp; | |
2181 | struct task_struct *tsk; | |
2182 | bool join = false; | |
2183 | int cpu = cpupid_to_cpu(cpupid); | |
2184 | int i; | |
2185 | ||
2186 | if (unlikely(!p->numa_group)) { | |
2187 | unsigned int size = sizeof(struct numa_group) + | |
50ec8a40 | 2188 | 4*nr_node_ids*sizeof(unsigned long); |
8c8a743c PZ |
2189 | |
2190 | grp = kzalloc(size, GFP_KERNEL | __GFP_NOWARN); | |
2191 | if (!grp) | |
2192 | return; | |
2193 | ||
2194 | atomic_set(&grp->refcount, 1); | |
4142c3eb RR |
2195 | grp->active_nodes = 1; |
2196 | grp->max_faults_cpu = 0; | |
8c8a743c | 2197 | spin_lock_init(&grp->lock); |
e29cf08b | 2198 | grp->gid = p->pid; |
50ec8a40 | 2199 | /* Second half of the array tracks nids where faults happen */ |
be1e4e76 RR |
2200 | grp->faults_cpu = grp->faults + NR_NUMA_HINT_FAULT_TYPES * |
2201 | nr_node_ids; | |
8c8a743c | 2202 | |
be1e4e76 | 2203 | for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) |
44dba3d5 | 2204 | grp->faults[i] = p->numa_faults[i]; |
8c8a743c | 2205 | |
989348b5 | 2206 | grp->total_faults = p->total_numa_faults; |
83e1d2cd | 2207 | |
8c8a743c PZ |
2208 | grp->nr_tasks++; |
2209 | rcu_assign_pointer(p->numa_group, grp); | |
2210 | } | |
2211 | ||
2212 | rcu_read_lock(); | |
316c1608 | 2213 | tsk = READ_ONCE(cpu_rq(cpu)->curr); |
8c8a743c PZ |
2214 | |
2215 | if (!cpupid_match_pid(tsk, cpupid)) | |
3354781a | 2216 | goto no_join; |
8c8a743c PZ |
2217 | |
2218 | grp = rcu_dereference(tsk->numa_group); | |
2219 | if (!grp) | |
3354781a | 2220 | goto no_join; |
8c8a743c PZ |
2221 | |
2222 | my_grp = p->numa_group; | |
2223 | if (grp == my_grp) | |
3354781a | 2224 | goto no_join; |
8c8a743c PZ |
2225 | |
2226 | /* | |
2227 | * Only join the other group if its bigger; if we're the bigger group, | |
2228 | * the other task will join us. | |
2229 | */ | |
2230 | if (my_grp->nr_tasks > grp->nr_tasks) | |
3354781a | 2231 | goto no_join; |
8c8a743c PZ |
2232 | |
2233 | /* | |
2234 | * Tie-break on the grp address. | |
2235 | */ | |
2236 | if (my_grp->nr_tasks == grp->nr_tasks && my_grp > grp) | |
3354781a | 2237 | goto no_join; |
8c8a743c | 2238 | |
dabe1d99 RR |
2239 | /* Always join threads in the same process. */ |
2240 | if (tsk->mm == current->mm) | |
2241 | join = true; | |
2242 | ||
2243 | /* Simple filter to avoid false positives due to PID collisions */ | |
2244 | if (flags & TNF_SHARED) | |
2245 | join = true; | |
8c8a743c | 2246 | |
3e6a9418 MG |
2247 | /* Update priv based on whether false sharing was detected */ |
2248 | *priv = !join; | |
2249 | ||
dabe1d99 | 2250 | if (join && !get_numa_group(grp)) |
3354781a | 2251 | goto no_join; |
8c8a743c | 2252 | |
8c8a743c PZ |
2253 | rcu_read_unlock(); |
2254 | ||
2255 | if (!join) | |
2256 | return; | |
2257 | ||
60e69eed MG |
2258 | BUG_ON(irqs_disabled()); |
2259 | double_lock_irq(&my_grp->lock, &grp->lock); | |
989348b5 | 2260 | |
be1e4e76 | 2261 | for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) { |
44dba3d5 IM |
2262 | my_grp->faults[i] -= p->numa_faults[i]; |
2263 | grp->faults[i] += p->numa_faults[i]; | |
8c8a743c | 2264 | } |
989348b5 MG |
2265 | my_grp->total_faults -= p->total_numa_faults; |
2266 | grp->total_faults += p->total_numa_faults; | |
8c8a743c | 2267 | |
8c8a743c PZ |
2268 | my_grp->nr_tasks--; |
2269 | grp->nr_tasks++; | |
2270 | ||
2271 | spin_unlock(&my_grp->lock); | |
60e69eed | 2272 | spin_unlock_irq(&grp->lock); |
8c8a743c PZ |
2273 | |
2274 | rcu_assign_pointer(p->numa_group, grp); | |
2275 | ||
2276 | put_numa_group(my_grp); | |
3354781a PZ |
2277 | return; |
2278 | ||
2279 | no_join: | |
2280 | rcu_read_unlock(); | |
2281 | return; | |
8c8a743c PZ |
2282 | } |
2283 | ||
2284 | void task_numa_free(struct task_struct *p) | |
2285 | { | |
2286 | struct numa_group *grp = p->numa_group; | |
44dba3d5 | 2287 | void *numa_faults = p->numa_faults; |
e9dd685c SR |
2288 | unsigned long flags; |
2289 | int i; | |
8c8a743c PZ |
2290 | |
2291 | if (grp) { | |
e9dd685c | 2292 | spin_lock_irqsave(&grp->lock, flags); |
be1e4e76 | 2293 | for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) |
44dba3d5 | 2294 | grp->faults[i] -= p->numa_faults[i]; |
989348b5 | 2295 | grp->total_faults -= p->total_numa_faults; |
83e1d2cd | 2296 | |
8c8a743c | 2297 | grp->nr_tasks--; |
e9dd685c | 2298 | spin_unlock_irqrestore(&grp->lock, flags); |
35b123e2 | 2299 | RCU_INIT_POINTER(p->numa_group, NULL); |
8c8a743c PZ |
2300 | put_numa_group(grp); |
2301 | } | |
2302 | ||
44dba3d5 | 2303 | p->numa_faults = NULL; |
82727018 | 2304 | kfree(numa_faults); |
8c8a743c PZ |
2305 | } |
2306 | ||
cbee9f88 PZ |
2307 | /* |
2308 | * Got a PROT_NONE fault for a page on @node. | |
2309 | */ | |
58b46da3 | 2310 | void task_numa_fault(int last_cpupid, int mem_node, int pages, int flags) |
cbee9f88 PZ |
2311 | { |
2312 | struct task_struct *p = current; | |
6688cc05 | 2313 | bool migrated = flags & TNF_MIGRATED; |
58b46da3 | 2314 | int cpu_node = task_node(current); |
792568ec | 2315 | int local = !!(flags & TNF_FAULT_LOCAL); |
4142c3eb | 2316 | struct numa_group *ng; |
ac8e895b | 2317 | int priv; |
cbee9f88 | 2318 | |
2a595721 | 2319 | if (!static_branch_likely(&sched_numa_balancing)) |
1a687c2e MG |
2320 | return; |
2321 | ||
9ff1d9ff MG |
2322 | /* for example, ksmd faulting in a user's mm */ |
2323 | if (!p->mm) | |
2324 | return; | |
2325 | ||
f809ca9a | 2326 | /* Allocate buffer to track faults on a per-node basis */ |
44dba3d5 IM |
2327 | if (unlikely(!p->numa_faults)) { |
2328 | int size = sizeof(*p->numa_faults) * | |
be1e4e76 | 2329 | NR_NUMA_HINT_FAULT_BUCKETS * nr_node_ids; |
f809ca9a | 2330 | |
44dba3d5 IM |
2331 | p->numa_faults = kzalloc(size, GFP_KERNEL|__GFP_NOWARN); |
2332 | if (!p->numa_faults) | |
f809ca9a | 2333 | return; |
745d6147 | 2334 | |
83e1d2cd | 2335 | p->total_numa_faults = 0; |
04bb2f94 | 2336 | memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); |
f809ca9a | 2337 | } |
cbee9f88 | 2338 | |
8c8a743c PZ |
2339 | /* |
2340 | * First accesses are treated as private, otherwise consider accesses | |
2341 | * to be private if the accessing pid has not changed | |
2342 | */ | |
2343 | if (unlikely(last_cpupid == (-1 & LAST_CPUPID_MASK))) { | |
2344 | priv = 1; | |
2345 | } else { | |
2346 | priv = cpupid_match_pid(p, last_cpupid); | |
6688cc05 | 2347 | if (!priv && !(flags & TNF_NO_GROUP)) |
3e6a9418 | 2348 | task_numa_group(p, last_cpupid, flags, &priv); |
8c8a743c PZ |
2349 | } |
2350 | ||
792568ec RR |
2351 | /* |
2352 | * If a workload spans multiple NUMA nodes, a shared fault that | |
2353 | * occurs wholly within the set of nodes that the workload is | |
2354 | * actively using should be counted as local. This allows the | |
2355 | * scan rate to slow down when a workload has settled down. | |
2356 | */ | |
4142c3eb RR |
2357 | ng = p->numa_group; |
2358 | if (!priv && !local && ng && ng->active_nodes > 1 && | |
2359 | numa_is_active_node(cpu_node, ng) && | |
2360 | numa_is_active_node(mem_node, ng)) | |
792568ec RR |
2361 | local = 1; |
2362 | ||
cbee9f88 | 2363 | task_numa_placement(p); |
f809ca9a | 2364 | |
2739d3ee RR |
2365 | /* |
2366 | * Retry task to preferred node migration periodically, in case it | |
2367 | * case it previously failed, or the scheduler moved us. | |
2368 | */ | |
2369 | if (time_after(jiffies, p->numa_migrate_retry)) | |
6b9a7460 MG |
2370 | numa_migrate_preferred(p); |
2371 | ||
b32e86b4 IM |
2372 | if (migrated) |
2373 | p->numa_pages_migrated += pages; | |
074c2381 MG |
2374 | if (flags & TNF_MIGRATE_FAIL) |
2375 | p->numa_faults_locality[2] += pages; | |
b32e86b4 | 2376 | |
44dba3d5 IM |
2377 | p->numa_faults[task_faults_idx(NUMA_MEMBUF, mem_node, priv)] += pages; |
2378 | p->numa_faults[task_faults_idx(NUMA_CPUBUF, cpu_node, priv)] += pages; | |
792568ec | 2379 | p->numa_faults_locality[local] += pages; |
cbee9f88 PZ |
2380 | } |
2381 | ||
6e5fb223 PZ |
2382 | static void reset_ptenuma_scan(struct task_struct *p) |
2383 | { | |
7e5a2c17 JL |
2384 | /* |
2385 | * We only did a read acquisition of the mmap sem, so | |
2386 | * p->mm->numa_scan_seq is written to without exclusive access | |
2387 | * and the update is not guaranteed to be atomic. That's not | |
2388 | * much of an issue though, since this is just used for | |
2389 | * statistical sampling. Use READ_ONCE/WRITE_ONCE, which are not | |
2390 | * expensive, to avoid any form of compiler optimizations: | |
2391 | */ | |
316c1608 | 2392 | WRITE_ONCE(p->mm->numa_scan_seq, READ_ONCE(p->mm->numa_scan_seq) + 1); |
6e5fb223 PZ |
2393 | p->mm->numa_scan_offset = 0; |
2394 | } | |
2395 | ||
cbee9f88 PZ |
2396 | /* |
2397 | * The expensive part of numa migration is done from task_work context. | |
2398 | * Triggered from task_tick_numa(). | |
2399 | */ | |
2400 | void task_numa_work(struct callback_head *work) | |
2401 | { | |
2402 | unsigned long migrate, next_scan, now = jiffies; | |
2403 | struct task_struct *p = current; | |
2404 | struct mm_struct *mm = p->mm; | |
51170840 | 2405 | u64 runtime = p->se.sum_exec_runtime; |
6e5fb223 | 2406 | struct vm_area_struct *vma; |
9f40604c | 2407 | unsigned long start, end; |
598f0ec0 | 2408 | unsigned long nr_pte_updates = 0; |
4620f8c1 | 2409 | long pages, virtpages; |
cbee9f88 | 2410 | |
9148a3a1 | 2411 | SCHED_WARN_ON(p != container_of(work, struct task_struct, numa_work)); |
cbee9f88 PZ |
2412 | |
2413 | work->next = work; /* protect against double add */ | |
2414 | /* | |
2415 | * Who cares about NUMA placement when they're dying. | |
2416 | * | |
2417 | * NOTE: make sure not to dereference p->mm before this check, | |
2418 | * exit_task_work() happens _after_ exit_mm() so we could be called | |
2419 | * without p->mm even though we still had it when we enqueued this | |
2420 | * work. | |
2421 | */ | |
2422 | if (p->flags & PF_EXITING) | |
2423 | return; | |
2424 | ||
930aa174 | 2425 | if (!mm->numa_next_scan) { |
7e8d16b6 MG |
2426 | mm->numa_next_scan = now + |
2427 | msecs_to_jiffies(sysctl_numa_balancing_scan_delay); | |
b8593bfd MG |
2428 | } |
2429 | ||
cbee9f88 PZ |
2430 | /* |
2431 | * Enforce maximal scan/migration frequency.. | |
2432 | */ | |
2433 | migrate = mm->numa_next_scan; | |
2434 | if (time_before(now, migrate)) | |
2435 | return; | |
2436 | ||
598f0ec0 MG |
2437 | if (p->numa_scan_period == 0) { |
2438 | p->numa_scan_period_max = task_scan_max(p); | |
2439 | p->numa_scan_period = task_scan_min(p); | |
2440 | } | |
cbee9f88 | 2441 | |
fb003b80 | 2442 | next_scan = now + msecs_to_jiffies(p->numa_scan_period); |
cbee9f88 PZ |
2443 | if (cmpxchg(&mm->numa_next_scan, migrate, next_scan) != migrate) |
2444 | return; | |
2445 | ||
19a78d11 PZ |
2446 | /* |
2447 | * Delay this task enough that another task of this mm will likely win | |
2448 | * the next time around. | |
2449 | */ | |
2450 | p->node_stamp += 2 * TICK_NSEC; | |
2451 | ||
9f40604c MG |
2452 | start = mm->numa_scan_offset; |
2453 | pages = sysctl_numa_balancing_scan_size; | |
2454 | pages <<= 20 - PAGE_SHIFT; /* MB in pages */ | |
4620f8c1 | 2455 | virtpages = pages * 8; /* Scan up to this much virtual space */ |
9f40604c MG |
2456 | if (!pages) |
2457 | return; | |
cbee9f88 | 2458 | |
4620f8c1 | 2459 | |
6e5fb223 | 2460 | down_read(&mm->mmap_sem); |
9f40604c | 2461 | vma = find_vma(mm, start); |
6e5fb223 PZ |
2462 | if (!vma) { |
2463 | reset_ptenuma_scan(p); | |
9f40604c | 2464 | start = 0; |
6e5fb223 PZ |
2465 | vma = mm->mmap; |
2466 | } | |
9f40604c | 2467 | for (; vma; vma = vma->vm_next) { |
6b79c57b | 2468 | if (!vma_migratable(vma) || !vma_policy_mof(vma) || |
8e76d4ee | 2469 | is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_MIXEDMAP)) { |
6e5fb223 | 2470 | continue; |
6b79c57b | 2471 | } |
6e5fb223 | 2472 | |
4591ce4f MG |
2473 | /* |
2474 | * Shared library pages mapped by multiple processes are not | |
2475 | * migrated as it is expected they are cache replicated. Avoid | |
2476 | * hinting faults in read-only file-backed mappings or the vdso | |
2477 | * as migrating the pages will be of marginal benefit. | |
2478 | */ | |
2479 | if (!vma->vm_mm || | |
2480 | (vma->vm_file && (vma->vm_flags & (VM_READ|VM_WRITE)) == (VM_READ))) | |
2481 | continue; | |
2482 | ||
3c67f474 MG |
2483 | /* |
2484 | * Skip inaccessible VMAs to avoid any confusion between | |
2485 | * PROT_NONE and NUMA hinting ptes | |
2486 | */ | |
2487 | if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))) | |
2488 | continue; | |
4591ce4f | 2489 | |
9f40604c MG |
2490 | do { |
2491 | start = max(start, vma->vm_start); | |
2492 | end = ALIGN(start + (pages << PAGE_SHIFT), HPAGE_SIZE); | |
2493 | end = min(end, vma->vm_end); | |
4620f8c1 | 2494 | nr_pte_updates = change_prot_numa(vma, start, end); |
598f0ec0 MG |
2495 | |
2496 | /* | |
4620f8c1 RR |
2497 | * Try to scan sysctl_numa_balancing_size worth of |
2498 | * hpages that have at least one present PTE that | |
2499 | * is not already pte-numa. If the VMA contains | |
2500 | * areas that are unused or already full of prot_numa | |
2501 | * PTEs, scan up to virtpages, to skip through those | |
2502 | * areas faster. | |
598f0ec0 MG |
2503 | */ |
2504 | if (nr_pte_updates) | |
2505 | pages -= (end - start) >> PAGE_SHIFT; | |
4620f8c1 | 2506 | virtpages -= (end - start) >> PAGE_SHIFT; |
6e5fb223 | 2507 | |
9f40604c | 2508 | start = end; |
4620f8c1 | 2509 | if (pages <= 0 || virtpages <= 0) |
9f40604c | 2510 | goto out; |
3cf1962c RR |
2511 | |
2512 | cond_resched(); | |
9f40604c | 2513 | } while (end != vma->vm_end); |
cbee9f88 | 2514 | } |
6e5fb223 | 2515 | |
9f40604c | 2516 | out: |
6e5fb223 | 2517 | /* |
c69307d5 PZ |
2518 | * It is possible to reach the end of the VMA list but the last few |
2519 | * VMAs are not guaranteed to the vma_migratable. If they are not, we | |
2520 | * would find the !migratable VMA on the next scan but not reset the | |
2521 | * scanner to the start so check it now. | |
6e5fb223 PZ |
2522 | */ |
2523 | if (vma) | |
9f40604c | 2524 | mm->numa_scan_offset = start; |
6e5fb223 PZ |
2525 | else |
2526 | reset_ptenuma_scan(p); | |
2527 | up_read(&mm->mmap_sem); | |
51170840 RR |
2528 | |
2529 | /* | |
2530 | * Make sure tasks use at least 32x as much time to run other code | |
2531 | * than they used here, to limit NUMA PTE scanning overhead to 3% max. | |
2532 | * Usually update_task_scan_period slows down scanning enough; on an | |
2533 | * overloaded system we need to limit overhead on a per task basis. | |
2534 | */ | |
2535 | if (unlikely(p->se.sum_exec_runtime != runtime)) { | |
2536 | u64 diff = p->se.sum_exec_runtime - runtime; | |
2537 | p->node_stamp += 32 * diff; | |
2538 | } | |
cbee9f88 PZ |
2539 | } |
2540 | ||
2541 | /* | |
2542 | * Drive the periodic memory faults.. | |
2543 | */ | |
2544 | void task_tick_numa(struct rq *rq, struct task_struct *curr) | |
2545 | { | |
2546 | struct callback_head *work = &curr->numa_work; | |
2547 | u64 period, now; | |
2548 | ||
2549 | /* | |
2550 | * We don't care about NUMA placement if we don't have memory. | |
2551 | */ | |
2552 | if (!curr->mm || (curr->flags & PF_EXITING) || work->next != work) | |
2553 | return; | |
2554 | ||
2555 | /* | |
2556 | * Using runtime rather than walltime has the dual advantage that | |
2557 | * we (mostly) drive the selection from busy threads and that the | |
2558 | * task needs to have done some actual work before we bother with | |
2559 | * NUMA placement. | |
2560 | */ | |
2561 | now = curr->se.sum_exec_runtime; | |
2562 | period = (u64)curr->numa_scan_period * NSEC_PER_MSEC; | |
2563 | ||
25b3e5a3 | 2564 | if (now > curr->node_stamp + period) { |
4b96a29b | 2565 | if (!curr->node_stamp) |
598f0ec0 | 2566 | curr->numa_scan_period = task_scan_min(curr); |
19a78d11 | 2567 | curr->node_stamp += period; |
cbee9f88 PZ |
2568 | |
2569 | if (!time_before(jiffies, curr->mm->numa_next_scan)) { | |
2570 | init_task_work(work, task_numa_work); /* TODO: move this into sched_fork() */ | |
2571 | task_work_add(curr, work, true); | |
2572 | } | |
2573 | } | |
2574 | } | |
2575 | #else | |
2576 | static void task_tick_numa(struct rq *rq, struct task_struct *curr) | |
2577 | { | |
2578 | } | |
0ec8aa00 PZ |
2579 | |
2580 | static inline void account_numa_enqueue(struct rq *rq, struct task_struct *p) | |
2581 | { | |
2582 | } | |
2583 | ||
2584 | static inline void account_numa_dequeue(struct rq *rq, struct task_struct *p) | |
2585 | { | |
2586 | } | |
cbee9f88 PZ |
2587 | #endif /* CONFIG_NUMA_BALANCING */ |
2588 | ||
30cfdcfc DA |
2589 | static void |
2590 | account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
2591 | { | |
2592 | update_load_add(&cfs_rq->load, se->load.weight); | |
c09595f6 | 2593 | if (!parent_entity(se)) |
029632fb | 2594 | update_load_add(&rq_of(cfs_rq)->load, se->load.weight); |
367456c7 | 2595 | #ifdef CONFIG_SMP |
0ec8aa00 PZ |
2596 | if (entity_is_task(se)) { |
2597 | struct rq *rq = rq_of(cfs_rq); | |
2598 | ||
2599 | account_numa_enqueue(rq, task_of(se)); | |
2600 | list_add(&se->group_node, &rq->cfs_tasks); | |
2601 | } | |
367456c7 | 2602 | #endif |
30cfdcfc | 2603 | cfs_rq->nr_running++; |
30cfdcfc DA |
2604 | } |
2605 | ||
2606 | static void | |
2607 | account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
2608 | { | |
2609 | update_load_sub(&cfs_rq->load, se->load.weight); | |
c09595f6 | 2610 | if (!parent_entity(se)) |
029632fb | 2611 | update_load_sub(&rq_of(cfs_rq)->load, se->load.weight); |
bfdb198c | 2612 | #ifdef CONFIG_SMP |
0ec8aa00 PZ |
2613 | if (entity_is_task(se)) { |
2614 | account_numa_dequeue(rq_of(cfs_rq), task_of(se)); | |
b87f1724 | 2615 | list_del_init(&se->group_node); |
0ec8aa00 | 2616 | } |
bfdb198c | 2617 | #endif |
30cfdcfc | 2618 | cfs_rq->nr_running--; |
30cfdcfc DA |
2619 | } |
2620 | ||
3ff6dcac YZ |
2621 | #ifdef CONFIG_FAIR_GROUP_SCHED |
2622 | # ifdef CONFIG_SMP | |
ea1dc6fc | 2623 | static long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg) |
cf5f0acf | 2624 | { |
ea1dc6fc | 2625 | long tg_weight, load, shares; |
cf5f0acf PZ |
2626 | |
2627 | /* | |
ea1dc6fc PZ |
2628 | * This really should be: cfs_rq->avg.load_avg, but instead we use |
2629 | * cfs_rq->load.weight, which is its upper bound. This helps ramp up | |
2630 | * the shares for small weight interactive tasks. | |
cf5f0acf | 2631 | */ |
ea1dc6fc | 2632 | load = scale_load_down(cfs_rq->load.weight); |
cf5f0acf | 2633 | |
ea1dc6fc | 2634 | tg_weight = atomic_long_read(&tg->load_avg); |
3ff6dcac | 2635 | |
ea1dc6fc PZ |
2636 | /* Ensure tg_weight >= load */ |
2637 | tg_weight -= cfs_rq->tg_load_avg_contrib; | |
2638 | tg_weight += load; | |
3ff6dcac | 2639 | |
3ff6dcac | 2640 | shares = (tg->shares * load); |
cf5f0acf PZ |
2641 | if (tg_weight) |
2642 | shares /= tg_weight; | |
3ff6dcac YZ |
2643 | |
2644 | if (shares < MIN_SHARES) | |
2645 | shares = MIN_SHARES; | |
2646 | if (shares > tg->shares) | |
2647 | shares = tg->shares; | |
2648 | ||
2649 | return shares; | |
2650 | } | |
3ff6dcac | 2651 | # else /* CONFIG_SMP */ |
6d5ab293 | 2652 | static inline long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg) |
3ff6dcac YZ |
2653 | { |
2654 | return tg->shares; | |
2655 | } | |
3ff6dcac | 2656 | # endif /* CONFIG_SMP */ |
ea1dc6fc | 2657 | |
2069dd75 PZ |
2658 | static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, |
2659 | unsigned long weight) | |
2660 | { | |
19e5eebb PT |
2661 | if (se->on_rq) { |
2662 | /* commit outstanding execution time */ | |
2663 | if (cfs_rq->curr == se) | |
2664 | update_curr(cfs_rq); | |
2069dd75 | 2665 | account_entity_dequeue(cfs_rq, se); |
19e5eebb | 2666 | } |
2069dd75 PZ |
2667 | |
2668 | update_load_set(&se->load, weight); | |
2669 | ||
2670 | if (se->on_rq) | |
2671 | account_entity_enqueue(cfs_rq, se); | |
2672 | } | |
2673 | ||
82958366 PT |
2674 | static inline int throttled_hierarchy(struct cfs_rq *cfs_rq); |
2675 | ||
6d5ab293 | 2676 | static void update_cfs_shares(struct cfs_rq *cfs_rq) |
2069dd75 PZ |
2677 | { |
2678 | struct task_group *tg; | |
2679 | struct sched_entity *se; | |
3ff6dcac | 2680 | long shares; |
2069dd75 | 2681 | |
2069dd75 PZ |
2682 | tg = cfs_rq->tg; |
2683 | se = tg->se[cpu_of(rq_of(cfs_rq))]; | |
64660c86 | 2684 | if (!se || throttled_hierarchy(cfs_rq)) |
2069dd75 | 2685 | return; |
3ff6dcac YZ |
2686 | #ifndef CONFIG_SMP |
2687 | if (likely(se->load.weight == tg->shares)) | |
2688 | return; | |
2689 | #endif | |
6d5ab293 | 2690 | shares = calc_cfs_shares(cfs_rq, tg); |
2069dd75 PZ |
2691 | |
2692 | reweight_entity(cfs_rq_of(se), se, shares); | |
2693 | } | |
2694 | #else /* CONFIG_FAIR_GROUP_SCHED */ | |
6d5ab293 | 2695 | static inline void update_cfs_shares(struct cfs_rq *cfs_rq) |
2069dd75 PZ |
2696 | { |
2697 | } | |
2698 | #endif /* CONFIG_FAIR_GROUP_SCHED */ | |
2699 | ||
141965c7 | 2700 | #ifdef CONFIG_SMP |
5b51f2f8 PT |
2701 | /* Precomputed fixed inverse multiplies for multiplication by y^n */ |
2702 | static const u32 runnable_avg_yN_inv[] = { | |
2703 | 0xffffffff, 0xfa83b2da, 0xf5257d14, 0xefe4b99a, 0xeac0c6e6, 0xe5b906e6, | |
2704 | 0xe0ccdeeb, 0xdbfbb796, 0xd744fcc9, 0xd2a81d91, 0xce248c14, 0xc9b9bd85, | |
2705 | 0xc5672a10, 0xc12c4cc9, 0xbd08a39e, 0xb8fbaf46, 0xb504f333, 0xb123f581, | |
2706 | 0xad583ee9, 0xa9a15ab4, 0xa5fed6a9, 0xa2704302, 0x9ef5325f, 0x9b8d39b9, | |
2707 | 0x9837f050, 0x94f4efa8, 0x91c3d373, 0x8ea4398a, 0x8b95c1e3, 0x88980e80, | |
2708 | 0x85aac367, 0x82cd8698, | |
2709 | }; | |
2710 | ||
2711 | /* | |
2712 | * Precomputed \Sum y^k { 1<=k<=n }. These are floor(true_value) to prevent | |
2713 | * over-estimates when re-combining. | |
2714 | */ | |
2715 | static const u32 runnable_avg_yN_sum[] = { | |
2716 | 0, 1002, 1982, 2941, 3880, 4798, 5697, 6576, 7437, 8279, 9103, | |
2717 | 9909,10698,11470,12226,12966,13690,14398,15091,15769,16433,17082, | |
2718 | 17718,18340,18949,19545,20128,20698,21256,21802,22336,22859,23371, | |
2719 | }; | |
2720 | ||
7b20b916 YD |
2721 | /* |
2722 | * Precomputed \Sum y^k { 1<=k<=n, where n%32=0). Values are rolled down to | |
2723 | * lower integers. See Documentation/scheduler/sched-avg.txt how these | |
2724 | * were generated: | |
2725 | */ | |
2726 | static const u32 __accumulated_sum_N32[] = { | |
2727 | 0, 23371, 35056, 40899, 43820, 45281, | |
2728 | 46011, 46376, 46559, 46650, 46696, 46719, | |
2729 | }; | |
2730 | ||
9d85f21c PT |
2731 | /* |
2732 | * Approximate: | |
2733 | * val * y^n, where y^32 ~= 0.5 (~1 scheduling period) | |
2734 | */ | |
2735 | static __always_inline u64 decay_load(u64 val, u64 n) | |
2736 | { | |
5b51f2f8 PT |
2737 | unsigned int local_n; |
2738 | ||
2739 | if (!n) | |
2740 | return val; | |
2741 | else if (unlikely(n > LOAD_AVG_PERIOD * 63)) | |
2742 | return 0; | |
2743 | ||
2744 | /* after bounds checking we can collapse to 32-bit */ | |
2745 | local_n = n; | |
2746 | ||
2747 | /* | |
2748 | * As y^PERIOD = 1/2, we can combine | |
9c58c79a ZZ |
2749 | * y^n = 1/2^(n/PERIOD) * y^(n%PERIOD) |
2750 | * With a look-up table which covers y^n (n<PERIOD) | |
5b51f2f8 PT |
2751 | * |
2752 | * To achieve constant time decay_load. | |
2753 | */ | |
2754 | if (unlikely(local_n >= LOAD_AVG_PERIOD)) { | |
2755 | val >>= local_n / LOAD_AVG_PERIOD; | |
2756 | local_n %= LOAD_AVG_PERIOD; | |
9d85f21c PT |
2757 | } |
2758 | ||
9d89c257 YD |
2759 | val = mul_u64_u32_shr(val, runnable_avg_yN_inv[local_n], 32); |
2760 | return val; | |
5b51f2f8 PT |
2761 | } |
2762 | ||
2763 | /* | |
2764 | * For updates fully spanning n periods, the contribution to runnable | |
2765 | * average will be: \Sum 1024*y^n | |
2766 | * | |
2767 | * We can compute this reasonably efficiently by combining: | |
2768 | * y^PERIOD = 1/2 with precomputed \Sum 1024*y^n {for n <PERIOD} | |
2769 | */ | |
2770 | static u32 __compute_runnable_contrib(u64 n) | |
2771 | { | |
2772 | u32 contrib = 0; | |
2773 | ||
2774 | if (likely(n <= LOAD_AVG_PERIOD)) | |
2775 | return runnable_avg_yN_sum[n]; | |
2776 | else if (unlikely(n >= LOAD_AVG_MAX_N)) | |
2777 | return LOAD_AVG_MAX; | |
2778 | ||
7b20b916 YD |
2779 | /* Since n < LOAD_AVG_MAX_N, n/LOAD_AVG_PERIOD < 11 */ |
2780 | contrib = __accumulated_sum_N32[n/LOAD_AVG_PERIOD]; | |
2781 | n %= LOAD_AVG_PERIOD; | |
5b51f2f8 PT |
2782 | contrib = decay_load(contrib, n); |
2783 | return contrib + runnable_avg_yN_sum[n]; | |
9d85f21c PT |
2784 | } |
2785 | ||
54a21385 | 2786 | #define cap_scale(v, s) ((v)*(s) >> SCHED_CAPACITY_SHIFT) |
e0f5f3af | 2787 | |
9d85f21c PT |
2788 | /* |
2789 | * We can represent the historical contribution to runnable average as the | |
2790 | * coefficients of a geometric series. To do this we sub-divide our runnable | |
2791 | * history into segments of approximately 1ms (1024us); label the segment that | |
2792 | * occurred N-ms ago p_N, with p_0 corresponding to the current period, e.g. | |
2793 | * | |
2794 | * [<- 1024us ->|<- 1024us ->|<- 1024us ->| ... | |
2795 | * p0 p1 p2 | |
2796 | * (now) (~1ms ago) (~2ms ago) | |
2797 | * | |
2798 | * Let u_i denote the fraction of p_i that the entity was runnable. | |
2799 | * | |
2800 | * We then designate the fractions u_i as our co-efficients, yielding the | |
2801 | * following representation of historical load: | |
2802 | * u_0 + u_1*y + u_2*y^2 + u_3*y^3 + ... | |
2803 | * | |
2804 | * We choose y based on the with of a reasonably scheduling period, fixing: | |
2805 | * y^32 = 0.5 | |
2806 | * | |
2807 | * This means that the contribution to load ~32ms ago (u_32) will be weighted | |
2808 | * approximately half as much as the contribution to load within the last ms | |
2809 | * (u_0). | |
2810 | * | |
2811 | * When a period "rolls over" and we have new u_0`, multiplying the previous | |
2812 | * sum again by y is sufficient to update: | |
2813 | * load_avg = u_0` + y*(u_0 + u_1*y + u_2*y^2 + ... ) | |
2814 | * = u_0 + u_1*y + u_2*y^2 + ... [re-labeling u_i --> u_{i+1}] | |
2815 | */ | |
9d89c257 YD |
2816 | static __always_inline int |
2817 | __update_load_avg(u64 now, int cpu, struct sched_avg *sa, | |
13962234 | 2818 | unsigned long weight, int running, struct cfs_rq *cfs_rq) |
9d85f21c | 2819 | { |
e0f5f3af | 2820 | u64 delta, scaled_delta, periods; |
9d89c257 | 2821 | u32 contrib; |
6115c793 | 2822 | unsigned int delta_w, scaled_delta_w, decayed = 0; |
6f2b0452 | 2823 | unsigned long scale_freq, scale_cpu; |
9d85f21c | 2824 | |
9d89c257 | 2825 | delta = now - sa->last_update_time; |
9d85f21c PT |
2826 | /* |
2827 | * This should only happen when time goes backwards, which it | |
2828 | * unfortunately does during sched clock init when we swap over to TSC. | |
2829 | */ | |
2830 | if ((s64)delta < 0) { | |
9d89c257 | 2831 | sa->last_update_time = now; |
9d85f21c PT |
2832 | return 0; |
2833 | } | |
2834 | ||
2835 | /* | |
2836 | * Use 1024ns as the unit of measurement since it's a reasonable | |
2837 | * approximation of 1us and fast to compute. | |
2838 | */ | |
2839 | delta >>= 10; | |
2840 | if (!delta) | |
2841 | return 0; | |
9d89c257 | 2842 | sa->last_update_time = now; |
9d85f21c | 2843 | |
6f2b0452 DE |
2844 | scale_freq = arch_scale_freq_capacity(NULL, cpu); |
2845 | scale_cpu = arch_scale_cpu_capacity(NULL, cpu); | |
2846 | ||
9d85f21c | 2847 | /* delta_w is the amount already accumulated against our next period */ |
9d89c257 | 2848 | delta_w = sa->period_contrib; |
9d85f21c | 2849 | if (delta + delta_w >= 1024) { |
9d85f21c PT |
2850 | decayed = 1; |
2851 | ||
9d89c257 YD |
2852 | /* how much left for next period will start over, we don't know yet */ |
2853 | sa->period_contrib = 0; | |
2854 | ||
9d85f21c PT |
2855 | /* |
2856 | * Now that we know we're crossing a period boundary, figure | |
2857 | * out how much from delta we need to complete the current | |
2858 | * period and accrue it. | |
2859 | */ | |
2860 | delta_w = 1024 - delta_w; | |
54a21385 | 2861 | scaled_delta_w = cap_scale(delta_w, scale_freq); |
13962234 | 2862 | if (weight) { |
e0f5f3af DE |
2863 | sa->load_sum += weight * scaled_delta_w; |
2864 | if (cfs_rq) { | |
2865 | cfs_rq->runnable_load_sum += | |
2866 | weight * scaled_delta_w; | |
2867 | } | |
13962234 | 2868 | } |
36ee28e4 | 2869 | if (running) |
006cdf02 | 2870 | sa->util_sum += scaled_delta_w * scale_cpu; |
5b51f2f8 PT |
2871 | |
2872 | delta -= delta_w; | |
2873 | ||
2874 | /* Figure out how many additional periods this update spans */ | |
2875 | periods = delta / 1024; | |
2876 | delta %= 1024; | |
2877 | ||
9d89c257 | 2878 | sa->load_sum = decay_load(sa->load_sum, periods + 1); |
13962234 YD |
2879 | if (cfs_rq) { |
2880 | cfs_rq->runnable_load_sum = | |
2881 | decay_load(cfs_rq->runnable_load_sum, periods + 1); | |
2882 | } | |
9d89c257 | 2883 | sa->util_sum = decay_load((u64)(sa->util_sum), periods + 1); |
5b51f2f8 PT |
2884 | |
2885 | /* Efficiently calculate \sum (1..n_period) 1024*y^i */ | |
9d89c257 | 2886 | contrib = __compute_runnable_contrib(periods); |
54a21385 | 2887 | contrib = cap_scale(contrib, scale_freq); |
13962234 | 2888 | if (weight) { |
9d89c257 | 2889 | sa->load_sum += weight * contrib; |
13962234 YD |
2890 | if (cfs_rq) |
2891 | cfs_rq->runnable_load_sum += weight * contrib; | |
2892 | } | |
36ee28e4 | 2893 | if (running) |
006cdf02 | 2894 | sa->util_sum += contrib * scale_cpu; |
9d85f21c PT |
2895 | } |
2896 | ||
2897 | /* Remainder of delta accrued against u_0` */ | |
54a21385 | 2898 | scaled_delta = cap_scale(delta, scale_freq); |
13962234 | 2899 | if (weight) { |
e0f5f3af | 2900 | sa->load_sum += weight * scaled_delta; |
13962234 | 2901 | if (cfs_rq) |
e0f5f3af | 2902 | cfs_rq->runnable_load_sum += weight * scaled_delta; |
13962234 | 2903 | } |
36ee28e4 | 2904 | if (running) |
006cdf02 | 2905 | sa->util_sum += scaled_delta * scale_cpu; |
9ee474f5 | 2906 | |
9d89c257 | 2907 | sa->period_contrib += delta; |
9ee474f5 | 2908 | |
9d89c257 YD |
2909 | if (decayed) { |
2910 | sa->load_avg = div_u64(sa->load_sum, LOAD_AVG_MAX); | |
13962234 YD |
2911 | if (cfs_rq) { |
2912 | cfs_rq->runnable_load_avg = | |
2913 | div_u64(cfs_rq->runnable_load_sum, LOAD_AVG_MAX); | |
2914 | } | |
006cdf02 | 2915 | sa->util_avg = sa->util_sum / LOAD_AVG_MAX; |
9d89c257 | 2916 | } |
aff3e498 | 2917 | |
9d89c257 | 2918 | return decayed; |
9ee474f5 PT |
2919 | } |
2920 | ||
09a43ace VG |
2921 | /* |
2922 | * Signed add and clamp on underflow. | |
2923 | * | |
2924 | * Explicitly do a load-store to ensure the intermediate value never hits | |
2925 | * memory. This allows lockless observations without ever seeing the negative | |
2926 | * values. | |
2927 | */ | |
2928 | #define add_positive(_ptr, _val) do { \ | |
2929 | typeof(_ptr) ptr = (_ptr); \ | |
2930 | typeof(_val) val = (_val); \ | |
2931 | typeof(*ptr) res, var = READ_ONCE(*ptr); \ | |
2932 | \ | |
2933 | res = var + val; \ | |
2934 | \ | |
2935 | if (val < 0 && res > var) \ | |
2936 | res = 0; \ | |
2937 | \ | |
2938 | WRITE_ONCE(*ptr, res); \ | |
2939 | } while (0) | |
2940 | ||
c566e8e9 | 2941 | #ifdef CONFIG_FAIR_GROUP_SCHED |
7c3edd2c PZ |
2942 | /** |
2943 | * update_tg_load_avg - update the tg's load avg | |
2944 | * @cfs_rq: the cfs_rq whose avg changed | |
2945 | * @force: update regardless of how small the difference | |
2946 | * | |
2947 | * This function 'ensures': tg->load_avg := \Sum tg->cfs_rq[]->avg.load. | |
2948 | * However, because tg->load_avg is a global value there are performance | |
2949 | * considerations. | |
2950 | * | |
2951 | * In order to avoid having to look at the other cfs_rq's, we use a | |
2952 | * differential update where we store the last value we propagated. This in | |
2953 | * turn allows skipping updates if the differential is 'small'. | |
2954 | * | |
2955 | * Updating tg's load_avg is necessary before update_cfs_share() (which is | |
2956 | * done) and effective_load() (which is not done because it is too costly). | |
bb17f655 | 2957 | */ |
9d89c257 | 2958 | static inline void update_tg_load_avg(struct cfs_rq *cfs_rq, int force) |
bb17f655 | 2959 | { |
9d89c257 | 2960 | long delta = cfs_rq->avg.load_avg - cfs_rq->tg_load_avg_contrib; |
bb17f655 | 2961 | |
aa0b7ae0 WL |
2962 | /* |
2963 | * No need to update load_avg for root_task_group as it is not used. | |
2964 | */ | |
2965 | if (cfs_rq->tg == &root_task_group) | |
2966 | return; | |
2967 | ||
9d89c257 YD |
2968 | if (force || abs(delta) > cfs_rq->tg_load_avg_contrib / 64) { |
2969 | atomic_long_add(delta, &cfs_rq->tg->load_avg); | |
2970 | cfs_rq->tg_load_avg_contrib = cfs_rq->avg.load_avg; | |
bb17f655 | 2971 | } |
8165e145 | 2972 | } |
f5f9739d | 2973 | |
ad936d86 BP |
2974 | /* |
2975 | * Called within set_task_rq() right before setting a task's cpu. The | |
2976 | * caller only guarantees p->pi_lock is held; no other assumptions, | |
2977 | * including the state of rq->lock, should be made. | |
2978 | */ | |
2979 | void set_task_rq_fair(struct sched_entity *se, | |
2980 | struct cfs_rq *prev, struct cfs_rq *next) | |
2981 | { | |
2982 | if (!sched_feat(ATTACH_AGE_LOAD)) | |
2983 | return; | |
2984 | ||
2985 | /* | |
2986 | * We are supposed to update the task to "current" time, then its up to | |
2987 | * date and ready to go to new CPU/cfs_rq. But we have difficulty in | |
2988 | * getting what current time is, so simply throw away the out-of-date | |
2989 | * time. This will result in the wakee task is less decayed, but giving | |
2990 | * the wakee more load sounds not bad. | |
2991 | */ | |
2992 | if (se->avg.last_update_time && prev) { | |
2993 | u64 p_last_update_time; | |
2994 | u64 n_last_update_time; | |
2995 | ||
2996 | #ifndef CONFIG_64BIT | |
2997 | u64 p_last_update_time_copy; | |
2998 | u64 n_last_update_time_copy; | |
2999 | ||
3000 | do { | |
3001 | p_last_update_time_copy = prev->load_last_update_time_copy; | |
3002 | n_last_update_time_copy = next->load_last_update_time_copy; | |
3003 | ||
3004 | smp_rmb(); | |
3005 | ||
3006 | p_last_update_time = prev->avg.last_update_time; | |
3007 | n_last_update_time = next->avg.last_update_time; | |
3008 | ||
3009 | } while (p_last_update_time != p_last_update_time_copy || | |
3010 | n_last_update_time != n_last_update_time_copy); | |
3011 | #else | |
3012 | p_last_update_time = prev->avg.last_update_time; | |
3013 | n_last_update_time = next->avg.last_update_time; | |
3014 | #endif | |
3015 | __update_load_avg(p_last_update_time, cpu_of(rq_of(prev)), | |
3016 | &se->avg, 0, 0, NULL); | |
3017 | se->avg.last_update_time = n_last_update_time; | |
3018 | } | |
3019 | } | |
09a43ace VG |
3020 | |
3021 | /* Take into account change of utilization of a child task group */ | |
3022 | static inline void | |
3023 | update_tg_cfs_util(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
3024 | { | |
3025 | struct cfs_rq *gcfs_rq = group_cfs_rq(se); | |
3026 | long delta = gcfs_rq->avg.util_avg - se->avg.util_avg; | |
3027 | ||
3028 | /* Nothing to update */ | |
3029 | if (!delta) | |
3030 | return; | |
3031 | ||
3032 | /* Set new sched_entity's utilization */ | |
3033 | se->avg.util_avg = gcfs_rq->avg.util_avg; | |
3034 | se->avg.util_sum = se->avg.util_avg * LOAD_AVG_MAX; | |
3035 | ||
3036 | /* Update parent cfs_rq utilization */ | |
3037 | add_positive(&cfs_rq->avg.util_avg, delta); | |
3038 | cfs_rq->avg.util_sum = cfs_rq->avg.util_avg * LOAD_AVG_MAX; | |
3039 | } | |
3040 | ||
3041 | /* Take into account change of load of a child task group */ | |
3042 | static inline void | |
3043 | update_tg_cfs_load(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
3044 | { | |
3045 | struct cfs_rq *gcfs_rq = group_cfs_rq(se); | |
3046 | long delta, load = gcfs_rq->avg.load_avg; | |
3047 | ||
3048 | /* | |
3049 | * If the load of group cfs_rq is null, the load of the | |
3050 | * sched_entity will also be null so we can skip the formula | |
3051 | */ | |
3052 | if (load) { | |
3053 | long tg_load; | |
3054 | ||
3055 | /* Get tg's load and ensure tg_load > 0 */ | |
3056 | tg_load = atomic_long_read(&gcfs_rq->tg->load_avg) + 1; | |
3057 | ||
3058 | /* Ensure tg_load >= load and updated with current load*/ | |
3059 | tg_load -= gcfs_rq->tg_load_avg_contrib; | |
3060 | tg_load += load; | |
3061 | ||
3062 | /* | |
3063 | * We need to compute a correction term in the case that the | |
3064 | * task group is consuming more CPU than a task of equal | |
3065 | * weight. A task with a weight equals to tg->shares will have | |
3066 | * a load less or equal to scale_load_down(tg->shares). | |
3067 | * Similarly, the sched_entities that represent the task group | |
3068 | * at parent level, can't have a load higher than | |
3069 | * scale_load_down(tg->shares). And the Sum of sched_entities' | |
3070 | * load must be <= scale_load_down(tg->shares). | |
3071 | */ | |
3072 | if (tg_load > scale_load_down(gcfs_rq->tg->shares)) { | |
3073 | /* scale gcfs_rq's load into tg's shares*/ | |
3074 | load *= scale_load_down(gcfs_rq->tg->shares); | |
3075 | load /= tg_load; | |
3076 | } | |
3077 | } | |
3078 | ||
3079 | delta = load - se->avg.load_avg; | |
3080 | ||
3081 | /* Nothing to update */ | |
3082 | if (!delta) | |
3083 | return; | |
3084 | ||
3085 | /* Set new sched_entity's load */ | |
3086 | se->avg.load_avg = load; | |
3087 | se->avg.load_sum = se->avg.load_avg * LOAD_AVG_MAX; | |
3088 | ||
3089 | /* Update parent cfs_rq load */ | |
3090 | add_positive(&cfs_rq->avg.load_avg, delta); | |
3091 | cfs_rq->avg.load_sum = cfs_rq->avg.load_avg * LOAD_AVG_MAX; | |
3092 | ||
3093 | /* | |
3094 | * If the sched_entity is already enqueued, we also have to update the | |
3095 | * runnable load avg. | |
3096 | */ | |
3097 | if (se->on_rq) { | |
3098 | /* Update parent cfs_rq runnable_load_avg */ | |
3099 | add_positive(&cfs_rq->runnable_load_avg, delta); | |
3100 | cfs_rq->runnable_load_sum = cfs_rq->runnable_load_avg * LOAD_AVG_MAX; | |
3101 | } | |
3102 | } | |
3103 | ||
3104 | static inline void set_tg_cfs_propagate(struct cfs_rq *cfs_rq) | |
3105 | { | |
3106 | cfs_rq->propagate_avg = 1; | |
3107 | } | |
3108 | ||
3109 | static inline int test_and_clear_tg_cfs_propagate(struct sched_entity *se) | |
3110 | { | |
3111 | struct cfs_rq *cfs_rq = group_cfs_rq(se); | |
3112 | ||
3113 | if (!cfs_rq->propagate_avg) | |
3114 | return 0; | |
3115 | ||
3116 | cfs_rq->propagate_avg = 0; | |
3117 | return 1; | |
3118 | } | |
3119 | ||
3120 | /* Update task and its cfs_rq load average */ | |
3121 | static inline int propagate_entity_load_avg(struct sched_entity *se) | |
3122 | { | |
3123 | struct cfs_rq *cfs_rq; | |
3124 | ||
3125 | if (entity_is_task(se)) | |
3126 | return 0; | |
3127 | ||
3128 | if (!test_and_clear_tg_cfs_propagate(se)) | |
3129 | return 0; | |
3130 | ||
3131 | cfs_rq = cfs_rq_of(se); | |
3132 | ||
3133 | set_tg_cfs_propagate(cfs_rq); | |
3134 | ||
3135 | update_tg_cfs_util(cfs_rq, se); | |
3136 | update_tg_cfs_load(cfs_rq, se); | |
3137 | ||
3138 | return 1; | |
3139 | } | |
3140 | ||
6e83125c | 3141 | #else /* CONFIG_FAIR_GROUP_SCHED */ |
09a43ace | 3142 | |
9d89c257 | 3143 | static inline void update_tg_load_avg(struct cfs_rq *cfs_rq, int force) {} |
09a43ace VG |
3144 | |
3145 | static inline int propagate_entity_load_avg(struct sched_entity *se) | |
3146 | { | |
3147 | return 0; | |
3148 | } | |
3149 | ||
3150 | static inline void set_tg_cfs_propagate(struct cfs_rq *cfs_rq) {} | |
3151 | ||
6e83125c | 3152 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
c566e8e9 | 3153 | |
a2c6c91f SM |
3154 | static inline void cfs_rq_util_change(struct cfs_rq *cfs_rq) |
3155 | { | |
58919e83 | 3156 | if (&this_rq()->cfs == cfs_rq) { |
a2c6c91f SM |
3157 | /* |
3158 | * There are a few boundary cases this might miss but it should | |
3159 | * get called often enough that that should (hopefully) not be | |
3160 | * a real problem -- added to that it only calls on the local | |
3161 | * CPU, so if we enqueue remotely we'll miss an update, but | |
3162 | * the next tick/schedule should update. | |
3163 | * | |
3164 | * It will not get called when we go idle, because the idle | |
3165 | * thread is a different class (!fair), nor will the utilization | |
3166 | * number include things like RT tasks. | |
3167 | * | |
3168 | * As is, the util number is not freq-invariant (we'd have to | |
3169 | * implement arch_scale_freq_capacity() for that). | |
3170 | * | |
3171 | * See cpu_util(). | |
3172 | */ | |
12bde33d | 3173 | cpufreq_update_util(rq_of(cfs_rq), 0); |
a2c6c91f SM |
3174 | } |
3175 | } | |
3176 | ||
89741892 PZ |
3177 | /* |
3178 | * Unsigned subtract and clamp on underflow. | |
3179 | * | |
3180 | * Explicitly do a load-store to ensure the intermediate value never hits | |
3181 | * memory. This allows lockless observations without ever seeing the negative | |
3182 | * values. | |
3183 | */ | |
3184 | #define sub_positive(_ptr, _val) do { \ | |
3185 | typeof(_ptr) ptr = (_ptr); \ | |
3186 | typeof(*ptr) val = (_val); \ | |
3187 | typeof(*ptr) res, var = READ_ONCE(*ptr); \ | |
3188 | res = var - val; \ | |
3189 | if (res > var) \ | |
3190 | res = 0; \ | |
3191 | WRITE_ONCE(*ptr, res); \ | |
3192 | } while (0) | |
3193 | ||
3d30544f PZ |
3194 | /** |
3195 | * update_cfs_rq_load_avg - update the cfs_rq's load/util averages | |
3196 | * @now: current time, as per cfs_rq_clock_task() | |
3197 | * @cfs_rq: cfs_rq to update | |
3198 | * @update_freq: should we call cfs_rq_util_change() or will the call do so | |
3199 | * | |
3200 | * The cfs_rq avg is the direct sum of all its entities (blocked and runnable) | |
3201 | * avg. The immediate corollary is that all (fair) tasks must be attached, see | |
3202 | * post_init_entity_util_avg(). | |
3203 | * | |
3204 | * cfs_rq->avg is used for task_h_load() and update_cfs_share() for example. | |
3205 | * | |
7c3edd2c PZ |
3206 | * Returns true if the load decayed or we removed load. |
3207 | * | |
3208 | * Since both these conditions indicate a changed cfs_rq->avg.load we should | |
3209 | * call update_tg_load_avg() when this function returns true. | |
3d30544f | 3210 | */ |
a2c6c91f SM |
3211 | static inline int |
3212 | update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq, bool update_freq) | |
2dac754e | 3213 | { |
9d89c257 | 3214 | struct sched_avg *sa = &cfs_rq->avg; |
41e0d37f | 3215 | int decayed, removed_load = 0, removed_util = 0; |
2dac754e | 3216 | |
9d89c257 | 3217 | if (atomic_long_read(&cfs_rq->removed_load_avg)) { |
9e0e83a1 | 3218 | s64 r = atomic_long_xchg(&cfs_rq->removed_load_avg, 0); |
89741892 PZ |
3219 | sub_positive(&sa->load_avg, r); |
3220 | sub_positive(&sa->load_sum, r * LOAD_AVG_MAX); | |
41e0d37f | 3221 | removed_load = 1; |
4e516076 | 3222 | set_tg_cfs_propagate(cfs_rq); |
8165e145 | 3223 | } |
2dac754e | 3224 | |
9d89c257 YD |
3225 | if (atomic_long_read(&cfs_rq->removed_util_avg)) { |
3226 | long r = atomic_long_xchg(&cfs_rq->removed_util_avg, 0); | |
89741892 PZ |
3227 | sub_positive(&sa->util_avg, r); |
3228 | sub_positive(&sa->util_sum, r * LOAD_AVG_MAX); | |
41e0d37f | 3229 | removed_util = 1; |
4e516076 | 3230 | set_tg_cfs_propagate(cfs_rq); |
9d89c257 | 3231 | } |
36ee28e4 | 3232 | |
a2c6c91f | 3233 | decayed = __update_load_avg(now, cpu_of(rq_of(cfs_rq)), sa, |
13962234 | 3234 | scale_load_down(cfs_rq->load.weight), cfs_rq->curr != NULL, cfs_rq); |
36ee28e4 | 3235 | |
9d89c257 YD |
3236 | #ifndef CONFIG_64BIT |
3237 | smp_wmb(); | |
3238 | cfs_rq->load_last_update_time_copy = sa->last_update_time; | |
3239 | #endif | |
36ee28e4 | 3240 | |
a2c6c91f SM |
3241 | if (update_freq && (decayed || removed_util)) |
3242 | cfs_rq_util_change(cfs_rq); | |
21e96f88 | 3243 | |
41e0d37f | 3244 | return decayed || removed_load; |
21e96f88 SM |
3245 | } |
3246 | ||
d31b1a66 VG |
3247 | /* |
3248 | * Optional action to be done while updating the load average | |
3249 | */ | |
3250 | #define UPDATE_TG 0x1 | |
3251 | #define SKIP_AGE_LOAD 0x2 | |
3252 | ||
21e96f88 | 3253 | /* Update task and its cfs_rq load average */ |
d31b1a66 | 3254 | static inline void update_load_avg(struct sched_entity *se, int flags) |
21e96f88 SM |
3255 | { |
3256 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
3257 | u64 now = cfs_rq_clock_task(cfs_rq); | |
3258 | struct rq *rq = rq_of(cfs_rq); | |
3259 | int cpu = cpu_of(rq); | |
09a43ace | 3260 | int decayed; |
21e96f88 SM |
3261 | |
3262 | /* | |
3263 | * Track task load average for carrying it to new CPU after migrated, and | |
3264 | * track group sched_entity load average for task_h_load calc in migration | |
3265 | */ | |
d31b1a66 VG |
3266 | if (se->avg.last_update_time && !(flags & SKIP_AGE_LOAD)) { |
3267 | __update_load_avg(now, cpu, &se->avg, | |
21e96f88 SM |
3268 | se->on_rq * scale_load_down(se->load.weight), |
3269 | cfs_rq->curr == se, NULL); | |
d31b1a66 | 3270 | } |
21e96f88 | 3271 | |
09a43ace VG |
3272 | decayed = update_cfs_rq_load_avg(now, cfs_rq, true); |
3273 | decayed |= propagate_entity_load_avg(se); | |
3274 | ||
3275 | if (decayed && (flags & UPDATE_TG)) | |
21e96f88 | 3276 | update_tg_load_avg(cfs_rq, 0); |
9ee474f5 PT |
3277 | } |
3278 | ||
3d30544f PZ |
3279 | /** |
3280 | * attach_entity_load_avg - attach this entity to its cfs_rq load avg | |
3281 | * @cfs_rq: cfs_rq to attach to | |
3282 | * @se: sched_entity to attach | |
3283 | * | |
3284 | * Must call update_cfs_rq_load_avg() before this, since we rely on | |
3285 | * cfs_rq->avg.last_update_time being current. | |
3286 | */ | |
a05e8c51 BP |
3287 | static void attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) |
3288 | { | |
3289 | se->avg.last_update_time = cfs_rq->avg.last_update_time; | |
3290 | cfs_rq->avg.load_avg += se->avg.load_avg; | |
3291 | cfs_rq->avg.load_sum += se->avg.load_sum; | |
3292 | cfs_rq->avg.util_avg += se->avg.util_avg; | |
3293 | cfs_rq->avg.util_sum += se->avg.util_sum; | |
09a43ace | 3294 | set_tg_cfs_propagate(cfs_rq); |
a2c6c91f SM |
3295 | |
3296 | cfs_rq_util_change(cfs_rq); | |
a05e8c51 BP |
3297 | } |
3298 | ||
3d30544f PZ |
3299 | /** |
3300 | * detach_entity_load_avg - detach this entity from its cfs_rq load avg | |
3301 | * @cfs_rq: cfs_rq to detach from | |
3302 | * @se: sched_entity to detach | |
3303 | * | |
3304 | * Must call update_cfs_rq_load_avg() before this, since we rely on | |
3305 | * cfs_rq->avg.last_update_time being current. | |
3306 | */ | |
a05e8c51 BP |
3307 | static void detach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) |
3308 | { | |
a05e8c51 | 3309 | |
89741892 PZ |
3310 | sub_positive(&cfs_rq->avg.load_avg, se->avg.load_avg); |
3311 | sub_positive(&cfs_rq->avg.load_sum, se->avg.load_sum); | |
3312 | sub_positive(&cfs_rq->avg.util_avg, se->avg.util_avg); | |
3313 | sub_positive(&cfs_rq->avg.util_sum, se->avg.util_sum); | |
09a43ace | 3314 | set_tg_cfs_propagate(cfs_rq); |
a2c6c91f SM |
3315 | |
3316 | cfs_rq_util_change(cfs_rq); | |
a05e8c51 BP |
3317 | } |
3318 | ||
9d89c257 YD |
3319 | /* Add the load generated by se into cfs_rq's load average */ |
3320 | static inline void | |
3321 | enqueue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
9ee474f5 | 3322 | { |
9d89c257 | 3323 | struct sched_avg *sa = &se->avg; |
18bf2805 | 3324 | |
13962234 YD |
3325 | cfs_rq->runnable_load_avg += sa->load_avg; |
3326 | cfs_rq->runnable_load_sum += sa->load_sum; | |
3327 | ||
d31b1a66 | 3328 | if (!sa->last_update_time) { |
a05e8c51 | 3329 | attach_entity_load_avg(cfs_rq, se); |
9d89c257 | 3330 | update_tg_load_avg(cfs_rq, 0); |
d31b1a66 | 3331 | } |
2dac754e PT |
3332 | } |
3333 | ||
13962234 YD |
3334 | /* Remove the runnable load generated by se from cfs_rq's runnable load average */ |
3335 | static inline void | |
3336 | dequeue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
3337 | { | |
13962234 YD |
3338 | cfs_rq->runnable_load_avg = |
3339 | max_t(long, cfs_rq->runnable_load_avg - se->avg.load_avg, 0); | |
3340 | cfs_rq->runnable_load_sum = | |
a05e8c51 | 3341 | max_t(s64, cfs_rq->runnable_load_sum - se->avg.load_sum, 0); |
13962234 YD |
3342 | } |
3343 | ||
9d89c257 | 3344 | #ifndef CONFIG_64BIT |
0905f04e YD |
3345 | static inline u64 cfs_rq_last_update_time(struct cfs_rq *cfs_rq) |
3346 | { | |
9d89c257 | 3347 | u64 last_update_time_copy; |
0905f04e | 3348 | u64 last_update_time; |
9ee474f5 | 3349 | |
9d89c257 YD |
3350 | do { |
3351 | last_update_time_copy = cfs_rq->load_last_update_time_copy; | |
3352 | smp_rmb(); | |
3353 | last_update_time = cfs_rq->avg.last_update_time; | |
3354 | } while (last_update_time != last_update_time_copy); | |
0905f04e YD |
3355 | |
3356 | return last_update_time; | |
3357 | } | |
9d89c257 | 3358 | #else |
0905f04e YD |
3359 | static inline u64 cfs_rq_last_update_time(struct cfs_rq *cfs_rq) |
3360 | { | |
3361 | return cfs_rq->avg.last_update_time; | |
3362 | } | |
9d89c257 YD |
3363 | #endif |
3364 | ||
104cb16d MR |
3365 | /* |
3366 | * Synchronize entity load avg of dequeued entity without locking | |
3367 | * the previous rq. | |
3368 | */ | |
3369 | void sync_entity_load_avg(struct sched_entity *se) | |
3370 | { | |
3371 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
3372 | u64 last_update_time; | |
3373 | ||
3374 | last_update_time = cfs_rq_last_update_time(cfs_rq); | |
3375 | __update_load_avg(last_update_time, cpu_of(rq_of(cfs_rq)), &se->avg, 0, 0, NULL); | |
3376 | } | |
3377 | ||
0905f04e YD |
3378 | /* |
3379 | * Task first catches up with cfs_rq, and then subtract | |
3380 | * itself from the cfs_rq (task must be off the queue now). | |
3381 | */ | |
3382 | void remove_entity_load_avg(struct sched_entity *se) | |
3383 | { | |
3384 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
0905f04e YD |
3385 | |
3386 | /* | |
7dc603c9 PZ |
3387 | * tasks cannot exit without having gone through wake_up_new_task() -> |
3388 | * post_init_entity_util_avg() which will have added things to the | |
3389 | * cfs_rq, so we can remove unconditionally. | |
3390 | * | |
3391 | * Similarly for groups, they will have passed through | |
3392 | * post_init_entity_util_avg() before unregister_sched_fair_group() | |
3393 | * calls this. | |
0905f04e | 3394 | */ |
0905f04e | 3395 | |
104cb16d | 3396 | sync_entity_load_avg(se); |
9d89c257 YD |
3397 | atomic_long_add(se->avg.load_avg, &cfs_rq->removed_load_avg); |
3398 | atomic_long_add(se->avg.util_avg, &cfs_rq->removed_util_avg); | |
2dac754e | 3399 | } |
642dbc39 | 3400 | |
7ea241af YD |
3401 | static inline unsigned long cfs_rq_runnable_load_avg(struct cfs_rq *cfs_rq) |
3402 | { | |
3403 | return cfs_rq->runnable_load_avg; | |
3404 | } | |
3405 | ||
3406 | static inline unsigned long cfs_rq_load_avg(struct cfs_rq *cfs_rq) | |
3407 | { | |
3408 | return cfs_rq->avg.load_avg; | |
3409 | } | |
3410 | ||
6e83125c PZ |
3411 | static int idle_balance(struct rq *this_rq); |
3412 | ||
38033c37 PZ |
3413 | #else /* CONFIG_SMP */ |
3414 | ||
01011473 PZ |
3415 | static inline int |
3416 | update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq, bool update_freq) | |
3417 | { | |
3418 | return 0; | |
3419 | } | |
3420 | ||
d31b1a66 VG |
3421 | #define UPDATE_TG 0x0 |
3422 | #define SKIP_AGE_LOAD 0x0 | |
3423 | ||
3424 | static inline void update_load_avg(struct sched_entity *se, int not_used1) | |
536bd00c | 3425 | { |
12bde33d | 3426 | cpufreq_update_util(rq_of(cfs_rq_of(se)), 0); |
536bd00c RW |
3427 | } |
3428 | ||
9d89c257 YD |
3429 | static inline void |
3430 | enqueue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {} | |
13962234 YD |
3431 | static inline void |
3432 | dequeue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {} | |
9d89c257 | 3433 | static inline void remove_entity_load_avg(struct sched_entity *se) {} |
6e83125c | 3434 | |
a05e8c51 BP |
3435 | static inline void |
3436 | attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {} | |
3437 | static inline void | |
3438 | detach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {} | |
3439 | ||
6e83125c PZ |
3440 | static inline int idle_balance(struct rq *rq) |
3441 | { | |
3442 | return 0; | |
3443 | } | |
3444 | ||
38033c37 | 3445 | #endif /* CONFIG_SMP */ |
9d85f21c | 3446 | |
ddc97297 PZ |
3447 | static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se) |
3448 | { | |
3449 | #ifdef CONFIG_SCHED_DEBUG | |
3450 | s64 d = se->vruntime - cfs_rq->min_vruntime; | |
3451 | ||
3452 | if (d < 0) | |
3453 | d = -d; | |
3454 | ||
3455 | if (d > 3*sysctl_sched_latency) | |
ae92882e | 3456 | schedstat_inc(cfs_rq->nr_spread_over); |
ddc97297 PZ |
3457 | #endif |
3458 | } | |
3459 | ||
aeb73b04 PZ |
3460 | static void |
3461 | place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial) | |
3462 | { | |
1af5f730 | 3463 | u64 vruntime = cfs_rq->min_vruntime; |
94dfb5e7 | 3464 | |
2cb8600e PZ |
3465 | /* |
3466 | * The 'current' period is already promised to the current tasks, | |
3467 | * however the extra weight of the new task will slow them down a | |
3468 | * little, place the new task so that it fits in the slot that | |
3469 | * stays open at the end. | |
3470 | */ | |
94dfb5e7 | 3471 | if (initial && sched_feat(START_DEBIT)) |
f9c0b095 | 3472 | vruntime += sched_vslice(cfs_rq, se); |
aeb73b04 | 3473 | |
a2e7a7eb | 3474 | /* sleeps up to a single latency don't count. */ |
5ca9880c | 3475 | if (!initial) { |
a2e7a7eb | 3476 | unsigned long thresh = sysctl_sched_latency; |
a7be37ac | 3477 | |
a2e7a7eb MG |
3478 | /* |
3479 | * Halve their sleep time's effect, to allow | |
3480 | * for a gentler effect of sleepers: | |
3481 | */ | |
3482 | if (sched_feat(GENTLE_FAIR_SLEEPERS)) | |
3483 | thresh >>= 1; | |
51e0304c | 3484 | |
a2e7a7eb | 3485 | vruntime -= thresh; |
aeb73b04 PZ |
3486 | } |
3487 | ||
b5d9d734 | 3488 | /* ensure we never gain time by being placed backwards. */ |
16c8f1c7 | 3489 | se->vruntime = max_vruntime(se->vruntime, vruntime); |
aeb73b04 PZ |
3490 | } |
3491 | ||
d3d9dc33 PT |
3492 | static void check_enqueue_throttle(struct cfs_rq *cfs_rq); |
3493 | ||
cb251765 MG |
3494 | static inline void check_schedstat_required(void) |
3495 | { | |
3496 | #ifdef CONFIG_SCHEDSTATS | |
3497 | if (schedstat_enabled()) | |
3498 | return; | |
3499 | ||
3500 | /* Force schedstat enabled if a dependent tracepoint is active */ | |
3501 | if (trace_sched_stat_wait_enabled() || | |
3502 | trace_sched_stat_sleep_enabled() || | |
3503 | trace_sched_stat_iowait_enabled() || | |
3504 | trace_sched_stat_blocked_enabled() || | |
3505 | trace_sched_stat_runtime_enabled()) { | |
eda8dca5 | 3506 | printk_deferred_once("Scheduler tracepoints stat_sleep, stat_iowait, " |
cb251765 MG |
3507 | "stat_blocked and stat_runtime require the " |
3508 | "kernel parameter schedstats=enabled or " | |
3509 | "kernel.sched_schedstats=1\n"); | |
3510 | } | |
3511 | #endif | |
3512 | } | |
3513 | ||
b5179ac7 PZ |
3514 | |
3515 | /* | |
3516 | * MIGRATION | |
3517 | * | |
3518 | * dequeue | |
3519 | * update_curr() | |
3520 | * update_min_vruntime() | |
3521 | * vruntime -= min_vruntime | |
3522 | * | |
3523 | * enqueue | |
3524 | * update_curr() | |
3525 | * update_min_vruntime() | |
3526 | * vruntime += min_vruntime | |
3527 | * | |
3528 | * this way the vruntime transition between RQs is done when both | |
3529 | * min_vruntime are up-to-date. | |
3530 | * | |
3531 | * WAKEUP (remote) | |
3532 | * | |
59efa0ba | 3533 | * ->migrate_task_rq_fair() (p->state == TASK_WAKING) |
b5179ac7 PZ |
3534 | * vruntime -= min_vruntime |
3535 | * | |
3536 | * enqueue | |
3537 | * update_curr() | |
3538 | * update_min_vruntime() | |
3539 | * vruntime += min_vruntime | |
3540 | * | |
3541 | * this way we don't have the most up-to-date min_vruntime on the originating | |
3542 | * CPU and an up-to-date min_vruntime on the destination CPU. | |
3543 | */ | |
3544 | ||
bf0f6f24 | 3545 | static void |
88ec22d3 | 3546 | enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
bf0f6f24 | 3547 | { |
2f950354 PZ |
3548 | bool renorm = !(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_MIGRATED); |
3549 | bool curr = cfs_rq->curr == se; | |
3550 | ||
88ec22d3 | 3551 | /* |
2f950354 PZ |
3552 | * If we're the current task, we must renormalise before calling |
3553 | * update_curr(). | |
88ec22d3 | 3554 | */ |
2f950354 | 3555 | if (renorm && curr) |
88ec22d3 PZ |
3556 | se->vruntime += cfs_rq->min_vruntime; |
3557 | ||
2f950354 PZ |
3558 | update_curr(cfs_rq); |
3559 | ||
bf0f6f24 | 3560 | /* |
2f950354 PZ |
3561 | * Otherwise, renormalise after, such that we're placed at the current |
3562 | * moment in time, instead of some random moment in the past. Being | |
3563 | * placed in the past could significantly boost this task to the | |
3564 | * fairness detriment of existing tasks. | |
bf0f6f24 | 3565 | */ |
2f950354 PZ |
3566 | if (renorm && !curr) |
3567 | se->vruntime += cfs_rq->min_vruntime; | |
3568 | ||
d31b1a66 | 3569 | update_load_avg(se, UPDATE_TG); |
9d89c257 | 3570 | enqueue_entity_load_avg(cfs_rq, se); |
17bc14b7 LT |
3571 | account_entity_enqueue(cfs_rq, se); |
3572 | update_cfs_shares(cfs_rq); | |
bf0f6f24 | 3573 | |
1a3d027c | 3574 | if (flags & ENQUEUE_WAKEUP) |
aeb73b04 | 3575 | place_entity(cfs_rq, se, 0); |
bf0f6f24 | 3576 | |
cb251765 | 3577 | check_schedstat_required(); |
4fa8d299 JP |
3578 | update_stats_enqueue(cfs_rq, se, flags); |
3579 | check_spread(cfs_rq, se); | |
2f950354 | 3580 | if (!curr) |
83b699ed | 3581 | __enqueue_entity(cfs_rq, se); |
2069dd75 | 3582 | se->on_rq = 1; |
3d4b47b4 | 3583 | |
d3d9dc33 | 3584 | if (cfs_rq->nr_running == 1) { |
3d4b47b4 | 3585 | list_add_leaf_cfs_rq(cfs_rq); |
d3d9dc33 PT |
3586 | check_enqueue_throttle(cfs_rq); |
3587 | } | |
bf0f6f24 IM |
3588 | } |
3589 | ||
2c13c919 | 3590 | static void __clear_buddies_last(struct sched_entity *se) |
2002c695 | 3591 | { |
2c13c919 RR |
3592 | for_each_sched_entity(se) { |
3593 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
f1044799 | 3594 | if (cfs_rq->last != se) |
2c13c919 | 3595 | break; |
f1044799 PZ |
3596 | |
3597 | cfs_rq->last = NULL; | |
2c13c919 RR |
3598 | } |
3599 | } | |
2002c695 | 3600 | |
2c13c919 RR |
3601 | static void __clear_buddies_next(struct sched_entity *se) |
3602 | { | |
3603 | for_each_sched_entity(se) { | |
3604 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
f1044799 | 3605 | if (cfs_rq->next != se) |
2c13c919 | 3606 | break; |
f1044799 PZ |
3607 | |
3608 | cfs_rq->next = NULL; | |
2c13c919 | 3609 | } |
2002c695 PZ |
3610 | } |
3611 | ||
ac53db59 RR |
3612 | static void __clear_buddies_skip(struct sched_entity *se) |
3613 | { | |
3614 | for_each_sched_entity(se) { | |
3615 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
f1044799 | 3616 | if (cfs_rq->skip != se) |
ac53db59 | 3617 | break; |
f1044799 PZ |
3618 | |
3619 | cfs_rq->skip = NULL; | |
ac53db59 RR |
3620 | } |
3621 | } | |
3622 | ||
a571bbea PZ |
3623 | static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se) |
3624 | { | |
2c13c919 RR |
3625 | if (cfs_rq->last == se) |
3626 | __clear_buddies_last(se); | |
3627 | ||
3628 | if (cfs_rq->next == se) | |
3629 | __clear_buddies_next(se); | |
ac53db59 RR |
3630 | |
3631 | if (cfs_rq->skip == se) | |
3632 | __clear_buddies_skip(se); | |
a571bbea PZ |
3633 | } |
3634 | ||
6c16a6dc | 3635 | static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq); |
d8b4986d | 3636 | |
bf0f6f24 | 3637 | static void |
371fd7e7 | 3638 | dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
bf0f6f24 | 3639 | { |
a2a2d680 DA |
3640 | /* |
3641 | * Update run-time statistics of the 'current'. | |
3642 | */ | |
3643 | update_curr(cfs_rq); | |
d31b1a66 | 3644 | update_load_avg(se, UPDATE_TG); |
13962234 | 3645 | dequeue_entity_load_avg(cfs_rq, se); |
a2a2d680 | 3646 | |
4fa8d299 | 3647 | update_stats_dequeue(cfs_rq, se, flags); |
67e9fb2a | 3648 | |
2002c695 | 3649 | clear_buddies(cfs_rq, se); |
4793241b | 3650 | |
83b699ed | 3651 | if (se != cfs_rq->curr) |
30cfdcfc | 3652 | __dequeue_entity(cfs_rq, se); |
17bc14b7 | 3653 | se->on_rq = 0; |
30cfdcfc | 3654 | account_entity_dequeue(cfs_rq, se); |
88ec22d3 PZ |
3655 | |
3656 | /* | |
b60205c7 PZ |
3657 | * Normalize after update_curr(); which will also have moved |
3658 | * min_vruntime if @se is the one holding it back. But before doing | |
3659 | * update_min_vruntime() again, which will discount @se's position and | |
3660 | * can move min_vruntime forward still more. | |
88ec22d3 | 3661 | */ |
371fd7e7 | 3662 | if (!(flags & DEQUEUE_SLEEP)) |
88ec22d3 | 3663 | se->vruntime -= cfs_rq->min_vruntime; |
1e876231 | 3664 | |
d8b4986d PT |
3665 | /* return excess runtime on last dequeue */ |
3666 | return_cfs_rq_runtime(cfs_rq); | |
3667 | ||
17bc14b7 | 3668 | update_cfs_shares(cfs_rq); |
b60205c7 PZ |
3669 | |
3670 | /* | |
3671 | * Now advance min_vruntime if @se was the entity holding it back, | |
3672 | * except when: DEQUEUE_SAVE && !DEQUEUE_MOVE, in this case we'll be | |
3673 | * put back on, and if we advance min_vruntime, we'll be placed back | |
3674 | * further than we started -- ie. we'll be penalized. | |
3675 | */ | |
3676 | if ((flags & (DEQUEUE_SAVE | DEQUEUE_MOVE)) == DEQUEUE_SAVE) | |
3677 | update_min_vruntime(cfs_rq); | |
bf0f6f24 IM |
3678 | } |
3679 | ||
3680 | /* | |
3681 | * Preempt the current task with a newly woken task if needed: | |
3682 | */ | |
7c92e54f | 3683 | static void |
2e09bf55 | 3684 | check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr) |
bf0f6f24 | 3685 | { |
11697830 | 3686 | unsigned long ideal_runtime, delta_exec; |
f4cfb33e WX |
3687 | struct sched_entity *se; |
3688 | s64 delta; | |
11697830 | 3689 | |
6d0f0ebd | 3690 | ideal_runtime = sched_slice(cfs_rq, curr); |
11697830 | 3691 | delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime; |
a9f3e2b5 | 3692 | if (delta_exec > ideal_runtime) { |
8875125e | 3693 | resched_curr(rq_of(cfs_rq)); |
a9f3e2b5 MG |
3694 | /* |
3695 | * The current task ran long enough, ensure it doesn't get | |
3696 | * re-elected due to buddy favours. | |
3697 | */ | |
3698 | clear_buddies(cfs_rq, curr); | |
f685ceac MG |
3699 | return; |
3700 | } | |
3701 | ||
3702 | /* | |
3703 | * Ensure that a task that missed wakeup preemption by a | |
3704 | * narrow margin doesn't have to wait for a full slice. | |
3705 | * This also mitigates buddy induced latencies under load. | |
3706 | */ | |
f685ceac MG |
3707 | if (delta_exec < sysctl_sched_min_granularity) |
3708 | return; | |
3709 | ||
f4cfb33e WX |
3710 | se = __pick_first_entity(cfs_rq); |
3711 | delta = curr->vruntime - se->vruntime; | |
f685ceac | 3712 | |
f4cfb33e WX |
3713 | if (delta < 0) |
3714 | return; | |
d7d82944 | 3715 | |
f4cfb33e | 3716 | if (delta > ideal_runtime) |
8875125e | 3717 | resched_curr(rq_of(cfs_rq)); |
bf0f6f24 IM |
3718 | } |
3719 | ||
83b699ed | 3720 | static void |
8494f412 | 3721 | set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 | 3722 | { |
83b699ed SV |
3723 | /* 'current' is not kept within the tree. */ |
3724 | if (se->on_rq) { | |
3725 | /* | |
3726 | * Any task has to be enqueued before it get to execute on | |
3727 | * a CPU. So account for the time it spent waiting on the | |
3728 | * runqueue. | |
3729 | */ | |
4fa8d299 | 3730 | update_stats_wait_end(cfs_rq, se); |
83b699ed | 3731 | __dequeue_entity(cfs_rq, se); |
d31b1a66 | 3732 | update_load_avg(se, UPDATE_TG); |
83b699ed SV |
3733 | } |
3734 | ||
79303e9e | 3735 | update_stats_curr_start(cfs_rq, se); |
429d43bc | 3736 | cfs_rq->curr = se; |
4fa8d299 | 3737 | |
eba1ed4b IM |
3738 | /* |
3739 | * Track our maximum slice length, if the CPU's load is at | |
3740 | * least twice that of our own weight (i.e. dont track it | |
3741 | * when there are only lesser-weight tasks around): | |
3742 | */ | |
cb251765 | 3743 | if (schedstat_enabled() && rq_of(cfs_rq)->load.weight >= 2*se->load.weight) { |
4fa8d299 JP |
3744 | schedstat_set(se->statistics.slice_max, |
3745 | max((u64)schedstat_val(se->statistics.slice_max), | |
3746 | se->sum_exec_runtime - se->prev_sum_exec_runtime)); | |
eba1ed4b | 3747 | } |
4fa8d299 | 3748 | |
4a55b450 | 3749 | se->prev_sum_exec_runtime = se->sum_exec_runtime; |
bf0f6f24 IM |
3750 | } |
3751 | ||
3f3a4904 PZ |
3752 | static int |
3753 | wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se); | |
3754 | ||
ac53db59 RR |
3755 | /* |
3756 | * Pick the next process, keeping these things in mind, in this order: | |
3757 | * 1) keep things fair between processes/task groups | |
3758 | * 2) pick the "next" process, since someone really wants that to run | |
3759 | * 3) pick the "last" process, for cache locality | |
3760 | * 4) do not run the "skip" process, if something else is available | |
3761 | */ | |
678d5718 PZ |
3762 | static struct sched_entity * |
3763 | pick_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *curr) | |
aa2ac252 | 3764 | { |
678d5718 PZ |
3765 | struct sched_entity *left = __pick_first_entity(cfs_rq); |
3766 | struct sched_entity *se; | |
3767 | ||
3768 | /* | |
3769 | * If curr is set we have to see if its left of the leftmost entity | |
3770 | * still in the tree, provided there was anything in the tree at all. | |
3771 | */ | |
3772 | if (!left || (curr && entity_before(curr, left))) | |
3773 | left = curr; | |
3774 | ||
3775 | se = left; /* ideally we run the leftmost entity */ | |
f4b6755f | 3776 | |
ac53db59 RR |
3777 | /* |
3778 | * Avoid running the skip buddy, if running something else can | |
3779 | * be done without getting too unfair. | |
3780 | */ | |
3781 | if (cfs_rq->skip == se) { | |
678d5718 PZ |
3782 | struct sched_entity *second; |
3783 | ||
3784 | if (se == curr) { | |
3785 | second = __pick_first_entity(cfs_rq); | |
3786 | } else { | |
3787 | second = __pick_next_entity(se); | |
3788 | if (!second || (curr && entity_before(curr, second))) | |
3789 | second = curr; | |
3790 | } | |
3791 | ||
ac53db59 RR |
3792 | if (second && wakeup_preempt_entity(second, left) < 1) |
3793 | se = second; | |
3794 | } | |
aa2ac252 | 3795 | |
f685ceac MG |
3796 | /* |
3797 | * Prefer last buddy, try to return the CPU to a preempted task. | |
3798 | */ | |
3799 | if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1) | |
3800 | se = cfs_rq->last; | |
3801 | ||
ac53db59 RR |
3802 | /* |
3803 | * Someone really wants this to run. If it's not unfair, run it. | |
3804 | */ | |
3805 | if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1) | |
3806 | se = cfs_rq->next; | |
3807 | ||
f685ceac | 3808 | clear_buddies(cfs_rq, se); |
4793241b PZ |
3809 | |
3810 | return se; | |
aa2ac252 PZ |
3811 | } |
3812 | ||
678d5718 | 3813 | static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq); |
d3d9dc33 | 3814 | |
ab6cde26 | 3815 | static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev) |
bf0f6f24 IM |
3816 | { |
3817 | /* | |
3818 | * If still on the runqueue then deactivate_task() | |
3819 | * was not called and update_curr() has to be done: | |
3820 | */ | |
3821 | if (prev->on_rq) | |
b7cc0896 | 3822 | update_curr(cfs_rq); |
bf0f6f24 | 3823 | |
d3d9dc33 PT |
3824 | /* throttle cfs_rqs exceeding runtime */ |
3825 | check_cfs_rq_runtime(cfs_rq); | |
3826 | ||
4fa8d299 | 3827 | check_spread(cfs_rq, prev); |
cb251765 | 3828 | |
30cfdcfc | 3829 | if (prev->on_rq) { |
4fa8d299 | 3830 | update_stats_wait_start(cfs_rq, prev); |
30cfdcfc DA |
3831 | /* Put 'current' back into the tree. */ |
3832 | __enqueue_entity(cfs_rq, prev); | |
9d85f21c | 3833 | /* in !on_rq case, update occurred at dequeue */ |
9d89c257 | 3834 | update_load_avg(prev, 0); |
30cfdcfc | 3835 | } |
429d43bc | 3836 | cfs_rq->curr = NULL; |
bf0f6f24 IM |
3837 | } |
3838 | ||
8f4d37ec PZ |
3839 | static void |
3840 | entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued) | |
bf0f6f24 | 3841 | { |
bf0f6f24 | 3842 | /* |
30cfdcfc | 3843 | * Update run-time statistics of the 'current'. |
bf0f6f24 | 3844 | */ |
30cfdcfc | 3845 | update_curr(cfs_rq); |
bf0f6f24 | 3846 | |
9d85f21c PT |
3847 | /* |
3848 | * Ensure that runnable average is periodically updated. | |
3849 | */ | |
d31b1a66 | 3850 | update_load_avg(curr, UPDATE_TG); |
bf0bd948 | 3851 | update_cfs_shares(cfs_rq); |
9d85f21c | 3852 | |
8f4d37ec PZ |
3853 | #ifdef CONFIG_SCHED_HRTICK |
3854 | /* | |
3855 | * queued ticks are scheduled to match the slice, so don't bother | |
3856 | * validating it and just reschedule. | |
3857 | */ | |
983ed7a6 | 3858 | if (queued) { |
8875125e | 3859 | resched_curr(rq_of(cfs_rq)); |
983ed7a6 HH |
3860 | return; |
3861 | } | |
8f4d37ec PZ |
3862 | /* |
3863 | * don't let the period tick interfere with the hrtick preemption | |
3864 | */ | |
3865 | if (!sched_feat(DOUBLE_TICK) && | |
3866 | hrtimer_active(&rq_of(cfs_rq)->hrtick_timer)) | |
3867 | return; | |
3868 | #endif | |
3869 | ||
2c2efaed | 3870 | if (cfs_rq->nr_running > 1) |
2e09bf55 | 3871 | check_preempt_tick(cfs_rq, curr); |
bf0f6f24 IM |
3872 | } |
3873 | ||
ab84d31e PT |
3874 | |
3875 | /************************************************** | |
3876 | * CFS bandwidth control machinery | |
3877 | */ | |
3878 | ||
3879 | #ifdef CONFIG_CFS_BANDWIDTH | |
029632fb PZ |
3880 | |
3881 | #ifdef HAVE_JUMP_LABEL | |
c5905afb | 3882 | static struct static_key __cfs_bandwidth_used; |
029632fb PZ |
3883 | |
3884 | static inline bool cfs_bandwidth_used(void) | |
3885 | { | |
c5905afb | 3886 | return static_key_false(&__cfs_bandwidth_used); |
029632fb PZ |
3887 | } |
3888 | ||
1ee14e6c | 3889 | void cfs_bandwidth_usage_inc(void) |
029632fb | 3890 | { |
1ee14e6c BS |
3891 | static_key_slow_inc(&__cfs_bandwidth_used); |
3892 | } | |
3893 | ||
3894 | void cfs_bandwidth_usage_dec(void) | |
3895 | { | |
3896 | static_key_slow_dec(&__cfs_bandwidth_used); | |
029632fb PZ |
3897 | } |
3898 | #else /* HAVE_JUMP_LABEL */ | |
3899 | static bool cfs_bandwidth_used(void) | |
3900 | { | |
3901 | return true; | |
3902 | } | |
3903 | ||
1ee14e6c BS |
3904 | void cfs_bandwidth_usage_inc(void) {} |
3905 | void cfs_bandwidth_usage_dec(void) {} | |
029632fb PZ |
3906 | #endif /* HAVE_JUMP_LABEL */ |
3907 | ||
ab84d31e PT |
3908 | /* |
3909 | * default period for cfs group bandwidth. | |
3910 | * default: 0.1s, units: nanoseconds | |
3911 | */ | |
3912 | static inline u64 default_cfs_period(void) | |
3913 | { | |
3914 | return 100000000ULL; | |
3915 | } | |
ec12cb7f PT |
3916 | |
3917 | static inline u64 sched_cfs_bandwidth_slice(void) | |
3918 | { | |
3919 | return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC; | |
3920 | } | |
3921 | ||
a9cf55b2 PT |
3922 | /* |
3923 | * Replenish runtime according to assigned quota and update expiration time. | |
3924 | * We use sched_clock_cpu directly instead of rq->clock to avoid adding | |
3925 | * additional synchronization around rq->lock. | |
3926 | * | |
3927 | * requires cfs_b->lock | |
3928 | */ | |
029632fb | 3929 | void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b) |
a9cf55b2 PT |
3930 | { |
3931 | u64 now; | |
3932 | ||
3933 | if (cfs_b->quota == RUNTIME_INF) | |
3934 | return; | |
3935 | ||
3936 | now = sched_clock_cpu(smp_processor_id()); | |
3937 | cfs_b->runtime = cfs_b->quota; | |
3938 | cfs_b->runtime_expires = now + ktime_to_ns(cfs_b->period); | |
3939 | } | |
3940 | ||
029632fb PZ |
3941 | static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg) |
3942 | { | |
3943 | return &tg->cfs_bandwidth; | |
3944 | } | |
3945 | ||
f1b17280 PT |
3946 | /* rq->task_clock normalized against any time this cfs_rq has spent throttled */ |
3947 | static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq) | |
3948 | { | |
3949 | if (unlikely(cfs_rq->throttle_count)) | |
1a99ae3f | 3950 | return cfs_rq->throttled_clock_task - cfs_rq->throttled_clock_task_time; |
f1b17280 | 3951 | |
78becc27 | 3952 | return rq_clock_task(rq_of(cfs_rq)) - cfs_rq->throttled_clock_task_time; |
f1b17280 PT |
3953 | } |
3954 | ||
85dac906 PT |
3955 | /* returns 0 on failure to allocate runtime */ |
3956 | static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
ec12cb7f PT |
3957 | { |
3958 | struct task_group *tg = cfs_rq->tg; | |
3959 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg); | |
a9cf55b2 | 3960 | u64 amount = 0, min_amount, expires; |
ec12cb7f PT |
3961 | |
3962 | /* note: this is a positive sum as runtime_remaining <= 0 */ | |
3963 | min_amount = sched_cfs_bandwidth_slice() - cfs_rq->runtime_remaining; | |
3964 | ||
3965 | raw_spin_lock(&cfs_b->lock); | |
3966 | if (cfs_b->quota == RUNTIME_INF) | |
3967 | amount = min_amount; | |
58088ad0 | 3968 | else { |
77a4d1a1 | 3969 | start_cfs_bandwidth(cfs_b); |
58088ad0 PT |
3970 | |
3971 | if (cfs_b->runtime > 0) { | |
3972 | amount = min(cfs_b->runtime, min_amount); | |
3973 | cfs_b->runtime -= amount; | |
3974 | cfs_b->idle = 0; | |
3975 | } | |
ec12cb7f | 3976 | } |
a9cf55b2 | 3977 | expires = cfs_b->runtime_expires; |
ec12cb7f PT |
3978 | raw_spin_unlock(&cfs_b->lock); |
3979 | ||
3980 | cfs_rq->runtime_remaining += amount; | |
a9cf55b2 PT |
3981 | /* |
3982 | * we may have advanced our local expiration to account for allowed | |
3983 | * spread between our sched_clock and the one on which runtime was | |
3984 | * issued. | |
3985 | */ | |
3986 | if ((s64)(expires - cfs_rq->runtime_expires) > 0) | |
3987 | cfs_rq->runtime_expires = expires; | |
85dac906 PT |
3988 | |
3989 | return cfs_rq->runtime_remaining > 0; | |
ec12cb7f PT |
3990 | } |
3991 | ||
a9cf55b2 PT |
3992 | /* |
3993 | * Note: This depends on the synchronization provided by sched_clock and the | |
3994 | * fact that rq->clock snapshots this value. | |
3995 | */ | |
3996 | static void expire_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
ec12cb7f | 3997 | { |
a9cf55b2 | 3998 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); |
a9cf55b2 PT |
3999 | |
4000 | /* if the deadline is ahead of our clock, nothing to do */ | |
78becc27 | 4001 | if (likely((s64)(rq_clock(rq_of(cfs_rq)) - cfs_rq->runtime_expires) < 0)) |
ec12cb7f PT |
4002 | return; |
4003 | ||
a9cf55b2 PT |
4004 | if (cfs_rq->runtime_remaining < 0) |
4005 | return; | |
4006 | ||
4007 | /* | |
4008 | * If the local deadline has passed we have to consider the | |
4009 | * possibility that our sched_clock is 'fast' and the global deadline | |
4010 | * has not truly expired. | |
4011 | * | |
4012 | * Fortunately we can check determine whether this the case by checking | |
51f2176d BS |
4013 | * whether the global deadline has advanced. It is valid to compare |
4014 | * cfs_b->runtime_expires without any locks since we only care about | |
4015 | * exact equality, so a partial write will still work. | |
a9cf55b2 PT |
4016 | */ |
4017 | ||
51f2176d | 4018 | if (cfs_rq->runtime_expires != cfs_b->runtime_expires) { |
a9cf55b2 PT |
4019 | /* extend local deadline, drift is bounded above by 2 ticks */ |
4020 | cfs_rq->runtime_expires += TICK_NSEC; | |
4021 | } else { | |
4022 | /* global deadline is ahead, expiration has passed */ | |
4023 | cfs_rq->runtime_remaining = 0; | |
4024 | } | |
4025 | } | |
4026 | ||
9dbdb155 | 4027 | static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec) |
a9cf55b2 PT |
4028 | { |
4029 | /* dock delta_exec before expiring quota (as it could span periods) */ | |
ec12cb7f | 4030 | cfs_rq->runtime_remaining -= delta_exec; |
a9cf55b2 PT |
4031 | expire_cfs_rq_runtime(cfs_rq); |
4032 | ||
4033 | if (likely(cfs_rq->runtime_remaining > 0)) | |
ec12cb7f PT |
4034 | return; |
4035 | ||
85dac906 PT |
4036 | /* |
4037 | * if we're unable to extend our runtime we resched so that the active | |
4038 | * hierarchy can be throttled | |
4039 | */ | |
4040 | if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr)) | |
8875125e | 4041 | resched_curr(rq_of(cfs_rq)); |
ec12cb7f PT |
4042 | } |
4043 | ||
6c16a6dc | 4044 | static __always_inline |
9dbdb155 | 4045 | void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec) |
ec12cb7f | 4046 | { |
56f570e5 | 4047 | if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled) |
ec12cb7f PT |
4048 | return; |
4049 | ||
4050 | __account_cfs_rq_runtime(cfs_rq, delta_exec); | |
4051 | } | |
4052 | ||
85dac906 PT |
4053 | static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq) |
4054 | { | |
56f570e5 | 4055 | return cfs_bandwidth_used() && cfs_rq->throttled; |
85dac906 PT |
4056 | } |
4057 | ||
64660c86 PT |
4058 | /* check whether cfs_rq, or any parent, is throttled */ |
4059 | static inline int throttled_hierarchy(struct cfs_rq *cfs_rq) | |
4060 | { | |
56f570e5 | 4061 | return cfs_bandwidth_used() && cfs_rq->throttle_count; |
64660c86 PT |
4062 | } |
4063 | ||
4064 | /* | |
4065 | * Ensure that neither of the group entities corresponding to src_cpu or | |
4066 | * dest_cpu are members of a throttled hierarchy when performing group | |
4067 | * load-balance operations. | |
4068 | */ | |
4069 | static inline int throttled_lb_pair(struct task_group *tg, | |
4070 | int src_cpu, int dest_cpu) | |
4071 | { | |
4072 | struct cfs_rq *src_cfs_rq, *dest_cfs_rq; | |
4073 | ||
4074 | src_cfs_rq = tg->cfs_rq[src_cpu]; | |
4075 | dest_cfs_rq = tg->cfs_rq[dest_cpu]; | |
4076 | ||
4077 | return throttled_hierarchy(src_cfs_rq) || | |
4078 | throttled_hierarchy(dest_cfs_rq); | |
4079 | } | |
4080 | ||
4081 | /* updated child weight may affect parent so we have to do this bottom up */ | |
4082 | static int tg_unthrottle_up(struct task_group *tg, void *data) | |
4083 | { | |
4084 | struct rq *rq = data; | |
4085 | struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; | |
4086 | ||
4087 | cfs_rq->throttle_count--; | |
64660c86 | 4088 | if (!cfs_rq->throttle_count) { |
f1b17280 | 4089 | /* adjust cfs_rq_clock_task() */ |
78becc27 | 4090 | cfs_rq->throttled_clock_task_time += rq_clock_task(rq) - |
f1b17280 | 4091 | cfs_rq->throttled_clock_task; |
64660c86 | 4092 | } |
64660c86 PT |
4093 | |
4094 | return 0; | |
4095 | } | |
4096 | ||
4097 | static int tg_throttle_down(struct task_group *tg, void *data) | |
4098 | { | |
4099 | struct rq *rq = data; | |
4100 | struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; | |
4101 | ||
82958366 PT |
4102 | /* group is entering throttled state, stop time */ |
4103 | if (!cfs_rq->throttle_count) | |
78becc27 | 4104 | cfs_rq->throttled_clock_task = rq_clock_task(rq); |
64660c86 PT |
4105 | cfs_rq->throttle_count++; |
4106 | ||
4107 | return 0; | |
4108 | } | |
4109 | ||
d3d9dc33 | 4110 | static void throttle_cfs_rq(struct cfs_rq *cfs_rq) |
85dac906 PT |
4111 | { |
4112 | struct rq *rq = rq_of(cfs_rq); | |
4113 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); | |
4114 | struct sched_entity *se; | |
4115 | long task_delta, dequeue = 1; | |
77a4d1a1 | 4116 | bool empty; |
85dac906 PT |
4117 | |
4118 | se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))]; | |
4119 | ||
f1b17280 | 4120 | /* freeze hierarchy runnable averages while throttled */ |
64660c86 PT |
4121 | rcu_read_lock(); |
4122 | walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq); | |
4123 | rcu_read_unlock(); | |
85dac906 PT |
4124 | |
4125 | task_delta = cfs_rq->h_nr_running; | |
4126 | for_each_sched_entity(se) { | |
4127 | struct cfs_rq *qcfs_rq = cfs_rq_of(se); | |
4128 | /* throttled entity or throttle-on-deactivate */ | |
4129 | if (!se->on_rq) | |
4130 | break; | |
4131 | ||
4132 | if (dequeue) | |
4133 | dequeue_entity(qcfs_rq, se, DEQUEUE_SLEEP); | |
4134 | qcfs_rq->h_nr_running -= task_delta; | |
4135 | ||
4136 | if (qcfs_rq->load.weight) | |
4137 | dequeue = 0; | |
4138 | } | |
4139 | ||
4140 | if (!se) | |
72465447 | 4141 | sub_nr_running(rq, task_delta); |
85dac906 PT |
4142 | |
4143 | cfs_rq->throttled = 1; | |
78becc27 | 4144 | cfs_rq->throttled_clock = rq_clock(rq); |
85dac906 | 4145 | raw_spin_lock(&cfs_b->lock); |
d49db342 | 4146 | empty = list_empty(&cfs_b->throttled_cfs_rq); |
77a4d1a1 | 4147 | |
c06f04c7 BS |
4148 | /* |
4149 | * Add to the _head_ of the list, so that an already-started | |
4150 | * distribute_cfs_runtime will not see us | |
4151 | */ | |
4152 | list_add_rcu(&cfs_rq->throttled_list, &cfs_b->throttled_cfs_rq); | |
77a4d1a1 PZ |
4153 | |
4154 | /* | |
4155 | * If we're the first throttled task, make sure the bandwidth | |
4156 | * timer is running. | |
4157 | */ | |
4158 | if (empty) | |
4159 | start_cfs_bandwidth(cfs_b); | |
4160 | ||
85dac906 PT |
4161 | raw_spin_unlock(&cfs_b->lock); |
4162 | } | |
4163 | ||
029632fb | 4164 | void unthrottle_cfs_rq(struct cfs_rq *cfs_rq) |
671fd9da PT |
4165 | { |
4166 | struct rq *rq = rq_of(cfs_rq); | |
4167 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); | |
4168 | struct sched_entity *se; | |
4169 | int enqueue = 1; | |
4170 | long task_delta; | |
4171 | ||
22b958d8 | 4172 | se = cfs_rq->tg->se[cpu_of(rq)]; |
671fd9da PT |
4173 | |
4174 | cfs_rq->throttled = 0; | |
1a55af2e FW |
4175 | |
4176 | update_rq_clock(rq); | |
4177 | ||
671fd9da | 4178 | raw_spin_lock(&cfs_b->lock); |
78becc27 | 4179 | cfs_b->throttled_time += rq_clock(rq) - cfs_rq->throttled_clock; |
671fd9da PT |
4180 | list_del_rcu(&cfs_rq->throttled_list); |
4181 | raw_spin_unlock(&cfs_b->lock); | |
4182 | ||
64660c86 PT |
4183 | /* update hierarchical throttle state */ |
4184 | walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq); | |
4185 | ||
671fd9da PT |
4186 | if (!cfs_rq->load.weight) |
4187 | return; | |
4188 | ||
4189 | task_delta = cfs_rq->h_nr_running; | |
4190 | for_each_sched_entity(se) { | |
4191 | if (se->on_rq) | |
4192 | enqueue = 0; | |
4193 | ||
4194 | cfs_rq = cfs_rq_of(se); | |
4195 | if (enqueue) | |
4196 | enqueue_entity(cfs_rq, se, ENQUEUE_WAKEUP); | |
4197 | cfs_rq->h_nr_running += task_delta; | |
4198 | ||
4199 | if (cfs_rq_throttled(cfs_rq)) | |
4200 | break; | |
4201 | } | |
4202 | ||
4203 | if (!se) | |
72465447 | 4204 | add_nr_running(rq, task_delta); |
671fd9da PT |
4205 | |
4206 | /* determine whether we need to wake up potentially idle cpu */ | |
4207 | if (rq->curr == rq->idle && rq->cfs.nr_running) | |
8875125e | 4208 | resched_curr(rq); |
671fd9da PT |
4209 | } |
4210 | ||
4211 | static u64 distribute_cfs_runtime(struct cfs_bandwidth *cfs_b, | |
4212 | u64 remaining, u64 expires) | |
4213 | { | |
4214 | struct cfs_rq *cfs_rq; | |
c06f04c7 BS |
4215 | u64 runtime; |
4216 | u64 starting_runtime = remaining; | |
671fd9da PT |
4217 | |
4218 | rcu_read_lock(); | |
4219 | list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq, | |
4220 | throttled_list) { | |
4221 | struct rq *rq = rq_of(cfs_rq); | |
4222 | ||
4223 | raw_spin_lock(&rq->lock); | |
4224 | if (!cfs_rq_throttled(cfs_rq)) | |
4225 | goto next; | |
4226 | ||
4227 | runtime = -cfs_rq->runtime_remaining + 1; | |
4228 | if (runtime > remaining) | |
4229 | runtime = remaining; | |
4230 | remaining -= runtime; | |
4231 | ||
4232 | cfs_rq->runtime_remaining += runtime; | |
4233 | cfs_rq->runtime_expires = expires; | |
4234 | ||
4235 | /* we check whether we're throttled above */ | |
4236 | if (cfs_rq->runtime_remaining > 0) | |
4237 | unthrottle_cfs_rq(cfs_rq); | |
4238 | ||
4239 | next: | |
4240 | raw_spin_unlock(&rq->lock); | |
4241 | ||
4242 | if (!remaining) | |
4243 | break; | |
4244 | } | |
4245 | rcu_read_unlock(); | |
4246 | ||
c06f04c7 | 4247 | return starting_runtime - remaining; |
671fd9da PT |
4248 | } |
4249 | ||
58088ad0 PT |
4250 | /* |
4251 | * Responsible for refilling a task_group's bandwidth and unthrottling its | |
4252 | * cfs_rqs as appropriate. If there has been no activity within the last | |
4253 | * period the timer is deactivated until scheduling resumes; cfs_b->idle is | |
4254 | * used to track this state. | |
4255 | */ | |
4256 | static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun) | |
4257 | { | |
671fd9da | 4258 | u64 runtime, runtime_expires; |
51f2176d | 4259 | int throttled; |
58088ad0 | 4260 | |
58088ad0 PT |
4261 | /* no need to continue the timer with no bandwidth constraint */ |
4262 | if (cfs_b->quota == RUNTIME_INF) | |
51f2176d | 4263 | goto out_deactivate; |
58088ad0 | 4264 | |
671fd9da | 4265 | throttled = !list_empty(&cfs_b->throttled_cfs_rq); |
e8da1b18 | 4266 | cfs_b->nr_periods += overrun; |
671fd9da | 4267 | |
51f2176d BS |
4268 | /* |
4269 | * idle depends on !throttled (for the case of a large deficit), and if | |
4270 | * we're going inactive then everything else can be deferred | |
4271 | */ | |
4272 | if (cfs_b->idle && !throttled) | |
4273 | goto out_deactivate; | |
a9cf55b2 PT |
4274 | |
4275 | __refill_cfs_bandwidth_runtime(cfs_b); | |
4276 | ||
671fd9da PT |
4277 | if (!throttled) { |
4278 | /* mark as potentially idle for the upcoming period */ | |
4279 | cfs_b->idle = 1; | |
51f2176d | 4280 | return 0; |
671fd9da PT |
4281 | } |
4282 | ||
e8da1b18 NR |
4283 | /* account preceding periods in which throttling occurred */ |
4284 | cfs_b->nr_throttled += overrun; | |
4285 | ||
671fd9da | 4286 | runtime_expires = cfs_b->runtime_expires; |
671fd9da PT |
4287 | |
4288 | /* | |
c06f04c7 BS |
4289 | * This check is repeated as we are holding onto the new bandwidth while |
4290 | * we unthrottle. This can potentially race with an unthrottled group | |
4291 | * trying to acquire new bandwidth from the global pool. This can result | |
4292 | * in us over-using our runtime if it is all used during this loop, but | |
4293 | * only by limited amounts in that extreme case. | |
671fd9da | 4294 | */ |
c06f04c7 BS |
4295 | while (throttled && cfs_b->runtime > 0) { |
4296 | runtime = cfs_b->runtime; | |
671fd9da PT |
4297 | raw_spin_unlock(&cfs_b->lock); |
4298 | /* we can't nest cfs_b->lock while distributing bandwidth */ | |
4299 | runtime = distribute_cfs_runtime(cfs_b, runtime, | |
4300 | runtime_expires); | |
4301 | raw_spin_lock(&cfs_b->lock); | |
4302 | ||
4303 | throttled = !list_empty(&cfs_b->throttled_cfs_rq); | |
c06f04c7 BS |
4304 | |
4305 | cfs_b->runtime -= min(runtime, cfs_b->runtime); | |
671fd9da | 4306 | } |
58088ad0 | 4307 | |
671fd9da PT |
4308 | /* |
4309 | * While we are ensured activity in the period following an | |
4310 | * unthrottle, this also covers the case in which the new bandwidth is | |
4311 | * insufficient to cover the existing bandwidth deficit. (Forcing the | |
4312 | * timer to remain active while there are any throttled entities.) | |
4313 | */ | |
4314 | cfs_b->idle = 0; | |
58088ad0 | 4315 | |
51f2176d BS |
4316 | return 0; |
4317 | ||
4318 | out_deactivate: | |
51f2176d | 4319 | return 1; |
58088ad0 | 4320 | } |
d3d9dc33 | 4321 | |
d8b4986d PT |
4322 | /* a cfs_rq won't donate quota below this amount */ |
4323 | static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC; | |
4324 | /* minimum remaining period time to redistribute slack quota */ | |
4325 | static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC; | |
4326 | /* how long we wait to gather additional slack before distributing */ | |
4327 | static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC; | |
4328 | ||
db06e78c BS |
4329 | /* |
4330 | * Are we near the end of the current quota period? | |
4331 | * | |
4332 | * Requires cfs_b->lock for hrtimer_expires_remaining to be safe against the | |
4961b6e1 | 4333 | * hrtimer base being cleared by hrtimer_start. In the case of |
db06e78c BS |
4334 | * migrate_hrtimers, base is never cleared, so we are fine. |
4335 | */ | |
d8b4986d PT |
4336 | static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire) |
4337 | { | |
4338 | struct hrtimer *refresh_timer = &cfs_b->period_timer; | |
4339 | u64 remaining; | |
4340 | ||
4341 | /* if the call-back is running a quota refresh is already occurring */ | |
4342 | if (hrtimer_callback_running(refresh_timer)) | |
4343 | return 1; | |
4344 | ||
4345 | /* is a quota refresh about to occur? */ | |
4346 | remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer)); | |
4347 | if (remaining < min_expire) | |
4348 | return 1; | |
4349 | ||
4350 | return 0; | |
4351 | } | |
4352 | ||
4353 | static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b) | |
4354 | { | |
4355 | u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration; | |
4356 | ||
4357 | /* if there's a quota refresh soon don't bother with slack */ | |
4358 | if (runtime_refresh_within(cfs_b, min_left)) | |
4359 | return; | |
4360 | ||
4cfafd30 PZ |
4361 | hrtimer_start(&cfs_b->slack_timer, |
4362 | ns_to_ktime(cfs_bandwidth_slack_period), | |
4363 | HRTIMER_MODE_REL); | |
d8b4986d PT |
4364 | } |
4365 | ||
4366 | /* we know any runtime found here is valid as update_curr() precedes return */ | |
4367 | static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
4368 | { | |
4369 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); | |
4370 | s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime; | |
4371 | ||
4372 | if (slack_runtime <= 0) | |
4373 | return; | |
4374 | ||
4375 | raw_spin_lock(&cfs_b->lock); | |
4376 | if (cfs_b->quota != RUNTIME_INF && | |
4377 | cfs_rq->runtime_expires == cfs_b->runtime_expires) { | |
4378 | cfs_b->runtime += slack_runtime; | |
4379 | ||
4380 | /* we are under rq->lock, defer unthrottling using a timer */ | |
4381 | if (cfs_b->runtime > sched_cfs_bandwidth_slice() && | |
4382 | !list_empty(&cfs_b->throttled_cfs_rq)) | |
4383 | start_cfs_slack_bandwidth(cfs_b); | |
4384 | } | |
4385 | raw_spin_unlock(&cfs_b->lock); | |
4386 | ||
4387 | /* even if it's not valid for return we don't want to try again */ | |
4388 | cfs_rq->runtime_remaining -= slack_runtime; | |
4389 | } | |
4390 | ||
4391 | static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
4392 | { | |
56f570e5 PT |
4393 | if (!cfs_bandwidth_used()) |
4394 | return; | |
4395 | ||
fccfdc6f | 4396 | if (!cfs_rq->runtime_enabled || cfs_rq->nr_running) |
d8b4986d PT |
4397 | return; |
4398 | ||
4399 | __return_cfs_rq_runtime(cfs_rq); | |
4400 | } | |
4401 | ||
4402 | /* | |
4403 | * This is done with a timer (instead of inline with bandwidth return) since | |
4404 | * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs. | |
4405 | */ | |
4406 | static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b) | |
4407 | { | |
4408 | u64 runtime = 0, slice = sched_cfs_bandwidth_slice(); | |
4409 | u64 expires; | |
4410 | ||
4411 | /* confirm we're still not at a refresh boundary */ | |
db06e78c BS |
4412 | raw_spin_lock(&cfs_b->lock); |
4413 | if (runtime_refresh_within(cfs_b, min_bandwidth_expiration)) { | |
4414 | raw_spin_unlock(&cfs_b->lock); | |
d8b4986d | 4415 | return; |
db06e78c | 4416 | } |
d8b4986d | 4417 | |
c06f04c7 | 4418 | if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) |
d8b4986d | 4419 | runtime = cfs_b->runtime; |
c06f04c7 | 4420 | |
d8b4986d PT |
4421 | expires = cfs_b->runtime_expires; |
4422 | raw_spin_unlock(&cfs_b->lock); | |
4423 | ||
4424 | if (!runtime) | |
4425 | return; | |
4426 | ||
4427 | runtime = distribute_cfs_runtime(cfs_b, runtime, expires); | |
4428 | ||
4429 | raw_spin_lock(&cfs_b->lock); | |
4430 | if (expires == cfs_b->runtime_expires) | |
c06f04c7 | 4431 | cfs_b->runtime -= min(runtime, cfs_b->runtime); |
d8b4986d PT |
4432 | raw_spin_unlock(&cfs_b->lock); |
4433 | } | |
4434 | ||
d3d9dc33 PT |
4435 | /* |
4436 | * When a group wakes up we want to make sure that its quota is not already | |
4437 | * expired/exceeded, otherwise it may be allowed to steal additional ticks of | |
4438 | * runtime as update_curr() throttling can not not trigger until it's on-rq. | |
4439 | */ | |
4440 | static void check_enqueue_throttle(struct cfs_rq *cfs_rq) | |
4441 | { | |
56f570e5 PT |
4442 | if (!cfs_bandwidth_used()) |
4443 | return; | |
4444 | ||
d3d9dc33 PT |
4445 | /* an active group must be handled by the update_curr()->put() path */ |
4446 | if (!cfs_rq->runtime_enabled || cfs_rq->curr) | |
4447 | return; | |
4448 | ||
4449 | /* ensure the group is not already throttled */ | |
4450 | if (cfs_rq_throttled(cfs_rq)) | |
4451 | return; | |
4452 | ||
4453 | /* update runtime allocation */ | |
4454 | account_cfs_rq_runtime(cfs_rq, 0); | |
4455 | if (cfs_rq->runtime_remaining <= 0) | |
4456 | throttle_cfs_rq(cfs_rq); | |
4457 | } | |
4458 | ||
55e16d30 PZ |
4459 | static void sync_throttle(struct task_group *tg, int cpu) |
4460 | { | |
4461 | struct cfs_rq *pcfs_rq, *cfs_rq; | |
4462 | ||
4463 | if (!cfs_bandwidth_used()) | |
4464 | return; | |
4465 | ||
4466 | if (!tg->parent) | |
4467 | return; | |
4468 | ||
4469 | cfs_rq = tg->cfs_rq[cpu]; | |
4470 | pcfs_rq = tg->parent->cfs_rq[cpu]; | |
4471 | ||
4472 | cfs_rq->throttle_count = pcfs_rq->throttle_count; | |
b8922125 | 4473 | cfs_rq->throttled_clock_task = rq_clock_task(cpu_rq(cpu)); |
55e16d30 PZ |
4474 | } |
4475 | ||
d3d9dc33 | 4476 | /* conditionally throttle active cfs_rq's from put_prev_entity() */ |
678d5718 | 4477 | static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq) |
d3d9dc33 | 4478 | { |
56f570e5 | 4479 | if (!cfs_bandwidth_used()) |
678d5718 | 4480 | return false; |
56f570e5 | 4481 | |
d3d9dc33 | 4482 | if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0)) |
678d5718 | 4483 | return false; |
d3d9dc33 PT |
4484 | |
4485 | /* | |
4486 | * it's possible for a throttled entity to be forced into a running | |
4487 | * state (e.g. set_curr_task), in this case we're finished. | |
4488 | */ | |
4489 | if (cfs_rq_throttled(cfs_rq)) | |
678d5718 | 4490 | return true; |
d3d9dc33 PT |
4491 | |
4492 | throttle_cfs_rq(cfs_rq); | |
678d5718 | 4493 | return true; |
d3d9dc33 | 4494 | } |
029632fb | 4495 | |
029632fb PZ |
4496 | static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer) |
4497 | { | |
4498 | struct cfs_bandwidth *cfs_b = | |
4499 | container_of(timer, struct cfs_bandwidth, slack_timer); | |
77a4d1a1 | 4500 | |
029632fb PZ |
4501 | do_sched_cfs_slack_timer(cfs_b); |
4502 | ||
4503 | return HRTIMER_NORESTART; | |
4504 | } | |
4505 | ||
4506 | static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer) | |
4507 | { | |
4508 | struct cfs_bandwidth *cfs_b = | |
4509 | container_of(timer, struct cfs_bandwidth, period_timer); | |
029632fb PZ |
4510 | int overrun; |
4511 | int idle = 0; | |
4512 | ||
51f2176d | 4513 | raw_spin_lock(&cfs_b->lock); |
029632fb | 4514 | for (;;) { |
77a4d1a1 | 4515 | overrun = hrtimer_forward_now(timer, cfs_b->period); |
029632fb PZ |
4516 | if (!overrun) |
4517 | break; | |
4518 | ||
4519 | idle = do_sched_cfs_period_timer(cfs_b, overrun); | |
4520 | } | |
4cfafd30 PZ |
4521 | if (idle) |
4522 | cfs_b->period_active = 0; | |
51f2176d | 4523 | raw_spin_unlock(&cfs_b->lock); |
029632fb PZ |
4524 | |
4525 | return idle ? HRTIMER_NORESTART : HRTIMER_RESTART; | |
4526 | } | |
4527 | ||
4528 | void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) | |
4529 | { | |
4530 | raw_spin_lock_init(&cfs_b->lock); | |
4531 | cfs_b->runtime = 0; | |
4532 | cfs_b->quota = RUNTIME_INF; | |
4533 | cfs_b->period = ns_to_ktime(default_cfs_period()); | |
4534 | ||
4535 | INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq); | |
4cfafd30 | 4536 | hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED); |
029632fb PZ |
4537 | cfs_b->period_timer.function = sched_cfs_period_timer; |
4538 | hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); | |
4539 | cfs_b->slack_timer.function = sched_cfs_slack_timer; | |
4540 | } | |
4541 | ||
4542 | static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
4543 | { | |
4544 | cfs_rq->runtime_enabled = 0; | |
4545 | INIT_LIST_HEAD(&cfs_rq->throttled_list); | |
4546 | } | |
4547 | ||
77a4d1a1 | 4548 | void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b) |
029632fb | 4549 | { |
4cfafd30 | 4550 | lockdep_assert_held(&cfs_b->lock); |
029632fb | 4551 | |
4cfafd30 PZ |
4552 | if (!cfs_b->period_active) { |
4553 | cfs_b->period_active = 1; | |
4554 | hrtimer_forward_now(&cfs_b->period_timer, cfs_b->period); | |
4555 | hrtimer_start_expires(&cfs_b->period_timer, HRTIMER_MODE_ABS_PINNED); | |
4556 | } | |
029632fb PZ |
4557 | } |
4558 | ||
4559 | static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) | |
4560 | { | |
7f1a169b TH |
4561 | /* init_cfs_bandwidth() was not called */ |
4562 | if (!cfs_b->throttled_cfs_rq.next) | |
4563 | return; | |
4564 | ||
029632fb PZ |
4565 | hrtimer_cancel(&cfs_b->period_timer); |
4566 | hrtimer_cancel(&cfs_b->slack_timer); | |
4567 | } | |
4568 | ||
0e59bdae KT |
4569 | static void __maybe_unused update_runtime_enabled(struct rq *rq) |
4570 | { | |
4571 | struct cfs_rq *cfs_rq; | |
4572 | ||
4573 | for_each_leaf_cfs_rq(rq, cfs_rq) { | |
4574 | struct cfs_bandwidth *cfs_b = &cfs_rq->tg->cfs_bandwidth; | |
4575 | ||
4576 | raw_spin_lock(&cfs_b->lock); | |
4577 | cfs_rq->runtime_enabled = cfs_b->quota != RUNTIME_INF; | |
4578 | raw_spin_unlock(&cfs_b->lock); | |
4579 | } | |
4580 | } | |
4581 | ||
38dc3348 | 4582 | static void __maybe_unused unthrottle_offline_cfs_rqs(struct rq *rq) |
029632fb PZ |
4583 | { |
4584 | struct cfs_rq *cfs_rq; | |
4585 | ||
4586 | for_each_leaf_cfs_rq(rq, cfs_rq) { | |
029632fb PZ |
4587 | if (!cfs_rq->runtime_enabled) |
4588 | continue; | |
4589 | ||
4590 | /* | |
4591 | * clock_task is not advancing so we just need to make sure | |
4592 | * there's some valid quota amount | |
4593 | */ | |
51f2176d | 4594 | cfs_rq->runtime_remaining = 1; |
0e59bdae KT |
4595 | /* |
4596 | * Offline rq is schedulable till cpu is completely disabled | |
4597 | * in take_cpu_down(), so we prevent new cfs throttling here. | |
4598 | */ | |
4599 | cfs_rq->runtime_enabled = 0; | |
4600 | ||
029632fb PZ |
4601 | if (cfs_rq_throttled(cfs_rq)) |
4602 | unthrottle_cfs_rq(cfs_rq); | |
4603 | } | |
4604 | } | |
4605 | ||
4606 | #else /* CONFIG_CFS_BANDWIDTH */ | |
f1b17280 PT |
4607 | static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq) |
4608 | { | |
78becc27 | 4609 | return rq_clock_task(rq_of(cfs_rq)); |
f1b17280 PT |
4610 | } |
4611 | ||
9dbdb155 | 4612 | static void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec) {} |
678d5718 | 4613 | static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq) { return false; } |
d3d9dc33 | 4614 | static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {} |
55e16d30 | 4615 | static inline void sync_throttle(struct task_group *tg, int cpu) {} |
6c16a6dc | 4616 | static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {} |
85dac906 PT |
4617 | |
4618 | static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq) | |
4619 | { | |
4620 | return 0; | |
4621 | } | |
64660c86 PT |
4622 | |
4623 | static inline int throttled_hierarchy(struct cfs_rq *cfs_rq) | |
4624 | { | |
4625 | return 0; | |
4626 | } | |
4627 | ||
4628 | static inline int throttled_lb_pair(struct task_group *tg, | |
4629 | int src_cpu, int dest_cpu) | |
4630 | { | |
4631 | return 0; | |
4632 | } | |
029632fb PZ |
4633 | |
4634 | void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {} | |
4635 | ||
4636 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
4637 | static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {} | |
ab84d31e PT |
4638 | #endif |
4639 | ||
029632fb PZ |
4640 | static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg) |
4641 | { | |
4642 | return NULL; | |
4643 | } | |
4644 | static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {} | |
0e59bdae | 4645 | static inline void update_runtime_enabled(struct rq *rq) {} |
a4c96ae3 | 4646 | static inline void unthrottle_offline_cfs_rqs(struct rq *rq) {} |
029632fb PZ |
4647 | |
4648 | #endif /* CONFIG_CFS_BANDWIDTH */ | |
4649 | ||
bf0f6f24 IM |
4650 | /************************************************** |
4651 | * CFS operations on tasks: | |
4652 | */ | |
4653 | ||
8f4d37ec PZ |
4654 | #ifdef CONFIG_SCHED_HRTICK |
4655 | static void hrtick_start_fair(struct rq *rq, struct task_struct *p) | |
4656 | { | |
8f4d37ec PZ |
4657 | struct sched_entity *se = &p->se; |
4658 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
4659 | ||
9148a3a1 | 4660 | SCHED_WARN_ON(task_rq(p) != rq); |
8f4d37ec | 4661 | |
8bf46a39 | 4662 | if (rq->cfs.h_nr_running > 1) { |
8f4d37ec PZ |
4663 | u64 slice = sched_slice(cfs_rq, se); |
4664 | u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime; | |
4665 | s64 delta = slice - ran; | |
4666 | ||
4667 | if (delta < 0) { | |
4668 | if (rq->curr == p) | |
8875125e | 4669 | resched_curr(rq); |
8f4d37ec PZ |
4670 | return; |
4671 | } | |
31656519 | 4672 | hrtick_start(rq, delta); |
8f4d37ec PZ |
4673 | } |
4674 | } | |
a4c2f00f PZ |
4675 | |
4676 | /* | |
4677 | * called from enqueue/dequeue and updates the hrtick when the | |
4678 | * current task is from our class and nr_running is low enough | |
4679 | * to matter. | |
4680 | */ | |
4681 | static void hrtick_update(struct rq *rq) | |
4682 | { | |
4683 | struct task_struct *curr = rq->curr; | |
4684 | ||
b39e66ea | 4685 | if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class) |
a4c2f00f PZ |
4686 | return; |
4687 | ||
4688 | if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency) | |
4689 | hrtick_start_fair(rq, curr); | |
4690 | } | |
55e12e5e | 4691 | #else /* !CONFIG_SCHED_HRTICK */ |
8f4d37ec PZ |
4692 | static inline void |
4693 | hrtick_start_fair(struct rq *rq, struct task_struct *p) | |
4694 | { | |
4695 | } | |
a4c2f00f PZ |
4696 | |
4697 | static inline void hrtick_update(struct rq *rq) | |
4698 | { | |
4699 | } | |
8f4d37ec PZ |
4700 | #endif |
4701 | ||
bf0f6f24 IM |
4702 | /* |
4703 | * The enqueue_task method is called before nr_running is | |
4704 | * increased. Here we update the fair scheduling stats and | |
4705 | * then put the task into the rbtree: | |
4706 | */ | |
ea87bb78 | 4707 | static void |
371fd7e7 | 4708 | enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags) |
bf0f6f24 IM |
4709 | { |
4710 | struct cfs_rq *cfs_rq; | |
62fb1851 | 4711 | struct sched_entity *se = &p->se; |
bf0f6f24 | 4712 | |
8c34ab19 RW |
4713 | /* |
4714 | * If in_iowait is set, the code below may not trigger any cpufreq | |
4715 | * utilization updates, so do it here explicitly with the IOWAIT flag | |
4716 | * passed. | |
4717 | */ | |
4718 | if (p->in_iowait) | |
4719 | cpufreq_update_this_cpu(rq, SCHED_CPUFREQ_IOWAIT); | |
4720 | ||
bf0f6f24 | 4721 | for_each_sched_entity(se) { |
62fb1851 | 4722 | if (se->on_rq) |
bf0f6f24 IM |
4723 | break; |
4724 | cfs_rq = cfs_rq_of(se); | |
88ec22d3 | 4725 | enqueue_entity(cfs_rq, se, flags); |
85dac906 PT |
4726 | |
4727 | /* | |
4728 | * end evaluation on encountering a throttled cfs_rq | |
4729 | * | |
4730 | * note: in the case of encountering a throttled cfs_rq we will | |
4731 | * post the final h_nr_running increment below. | |
e210bffd | 4732 | */ |
85dac906 PT |
4733 | if (cfs_rq_throttled(cfs_rq)) |
4734 | break; | |
953bfcd1 | 4735 | cfs_rq->h_nr_running++; |
85dac906 | 4736 | |
88ec22d3 | 4737 | flags = ENQUEUE_WAKEUP; |
bf0f6f24 | 4738 | } |
8f4d37ec | 4739 | |
2069dd75 | 4740 | for_each_sched_entity(se) { |
0f317143 | 4741 | cfs_rq = cfs_rq_of(se); |
953bfcd1 | 4742 | cfs_rq->h_nr_running++; |
2069dd75 | 4743 | |
85dac906 PT |
4744 | if (cfs_rq_throttled(cfs_rq)) |
4745 | break; | |
4746 | ||
d31b1a66 | 4747 | update_load_avg(se, UPDATE_TG); |
17bc14b7 | 4748 | update_cfs_shares(cfs_rq); |
2069dd75 PZ |
4749 | } |
4750 | ||
cd126afe | 4751 | if (!se) |
72465447 | 4752 | add_nr_running(rq, 1); |
cd126afe | 4753 | |
a4c2f00f | 4754 | hrtick_update(rq); |
bf0f6f24 IM |
4755 | } |
4756 | ||
2f36825b VP |
4757 | static void set_next_buddy(struct sched_entity *se); |
4758 | ||
bf0f6f24 IM |
4759 | /* |
4760 | * The dequeue_task method is called before nr_running is | |
4761 | * decreased. We remove the task from the rbtree and | |
4762 | * update the fair scheduling stats: | |
4763 | */ | |
371fd7e7 | 4764 | static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags) |
bf0f6f24 IM |
4765 | { |
4766 | struct cfs_rq *cfs_rq; | |
62fb1851 | 4767 | struct sched_entity *se = &p->se; |
2f36825b | 4768 | int task_sleep = flags & DEQUEUE_SLEEP; |
bf0f6f24 IM |
4769 | |
4770 | for_each_sched_entity(se) { | |
4771 | cfs_rq = cfs_rq_of(se); | |
371fd7e7 | 4772 | dequeue_entity(cfs_rq, se, flags); |
85dac906 PT |
4773 | |
4774 | /* | |
4775 | * end evaluation on encountering a throttled cfs_rq | |
4776 | * | |
4777 | * note: in the case of encountering a throttled cfs_rq we will | |
4778 | * post the final h_nr_running decrement below. | |
4779 | */ | |
4780 | if (cfs_rq_throttled(cfs_rq)) | |
4781 | break; | |
953bfcd1 | 4782 | cfs_rq->h_nr_running--; |
2069dd75 | 4783 | |
bf0f6f24 | 4784 | /* Don't dequeue parent if it has other entities besides us */ |
2f36825b | 4785 | if (cfs_rq->load.weight) { |
754bd598 KK |
4786 | /* Avoid re-evaluating load for this entity: */ |
4787 | se = parent_entity(se); | |
2f36825b VP |
4788 | /* |
4789 | * Bias pick_next to pick a task from this cfs_rq, as | |
4790 | * p is sleeping when it is within its sched_slice. | |
4791 | */ | |
754bd598 KK |
4792 | if (task_sleep && se && !throttled_hierarchy(cfs_rq)) |
4793 | set_next_buddy(se); | |
bf0f6f24 | 4794 | break; |
2f36825b | 4795 | } |
371fd7e7 | 4796 | flags |= DEQUEUE_SLEEP; |
bf0f6f24 | 4797 | } |
8f4d37ec | 4798 | |
2069dd75 | 4799 | for_each_sched_entity(se) { |
0f317143 | 4800 | cfs_rq = cfs_rq_of(se); |
953bfcd1 | 4801 | cfs_rq->h_nr_running--; |
2069dd75 | 4802 | |
85dac906 PT |
4803 | if (cfs_rq_throttled(cfs_rq)) |
4804 | break; | |
4805 | ||
d31b1a66 | 4806 | update_load_avg(se, UPDATE_TG); |
17bc14b7 | 4807 | update_cfs_shares(cfs_rq); |
2069dd75 PZ |
4808 | } |
4809 | ||
cd126afe | 4810 | if (!se) |
72465447 | 4811 | sub_nr_running(rq, 1); |
cd126afe | 4812 | |
a4c2f00f | 4813 | hrtick_update(rq); |
bf0f6f24 IM |
4814 | } |
4815 | ||
e7693a36 | 4816 | #ifdef CONFIG_SMP |
10e2f1ac PZ |
4817 | |
4818 | /* Working cpumask for: load_balance, load_balance_newidle. */ | |
4819 | DEFINE_PER_CPU(cpumask_var_t, load_balance_mask); | |
4820 | DEFINE_PER_CPU(cpumask_var_t, select_idle_mask); | |
4821 | ||
9fd81dd5 | 4822 | #ifdef CONFIG_NO_HZ_COMMON |
3289bdb4 PZ |
4823 | /* |
4824 | * per rq 'load' arrray crap; XXX kill this. | |
4825 | */ | |
4826 | ||
4827 | /* | |
d937cdc5 | 4828 | * The exact cpuload calculated at every tick would be: |
3289bdb4 | 4829 | * |
d937cdc5 PZ |
4830 | * load' = (1 - 1/2^i) * load + (1/2^i) * cur_load |
4831 | * | |
4832 | * If a cpu misses updates for n ticks (as it was idle) and update gets | |
4833 | * called on the n+1-th tick when cpu may be busy, then we have: | |
4834 | * | |
4835 | * load_n = (1 - 1/2^i)^n * load_0 | |
4836 | * load_n+1 = (1 - 1/2^i) * load_n + (1/2^i) * cur_load | |
3289bdb4 PZ |
4837 | * |
4838 | * decay_load_missed() below does efficient calculation of | |
3289bdb4 | 4839 | * |
d937cdc5 PZ |
4840 | * load' = (1 - 1/2^i)^n * load |
4841 | * | |
4842 | * Because x^(n+m) := x^n * x^m we can decompose any x^n in power-of-2 factors. | |
4843 | * This allows us to precompute the above in said factors, thereby allowing the | |
4844 | * reduction of an arbitrary n in O(log_2 n) steps. (See also | |
4845 | * fixed_power_int()) | |
3289bdb4 | 4846 | * |
d937cdc5 | 4847 | * The calculation is approximated on a 128 point scale. |
3289bdb4 PZ |
4848 | */ |
4849 | #define DEGRADE_SHIFT 7 | |
d937cdc5 PZ |
4850 | |
4851 | static const u8 degrade_zero_ticks[CPU_LOAD_IDX_MAX] = {0, 8, 32, 64, 128}; | |
4852 | static const u8 degrade_factor[CPU_LOAD_IDX_MAX][DEGRADE_SHIFT + 1] = { | |
4853 | { 0, 0, 0, 0, 0, 0, 0, 0 }, | |
4854 | { 64, 32, 8, 0, 0, 0, 0, 0 }, | |
4855 | { 96, 72, 40, 12, 1, 0, 0, 0 }, | |
4856 | { 112, 98, 75, 43, 15, 1, 0, 0 }, | |
4857 | { 120, 112, 98, 76, 45, 16, 2, 0 } | |
4858 | }; | |
3289bdb4 PZ |
4859 | |
4860 | /* | |
4861 | * Update cpu_load for any missed ticks, due to tickless idle. The backlog | |
4862 | * would be when CPU is idle and so we just decay the old load without | |
4863 | * adding any new load. | |
4864 | */ | |
4865 | static unsigned long | |
4866 | decay_load_missed(unsigned long load, unsigned long missed_updates, int idx) | |
4867 | { | |
4868 | int j = 0; | |
4869 | ||
4870 | if (!missed_updates) | |
4871 | return load; | |
4872 | ||
4873 | if (missed_updates >= degrade_zero_ticks[idx]) | |
4874 | return 0; | |
4875 | ||
4876 | if (idx == 1) | |
4877 | return load >> missed_updates; | |
4878 | ||
4879 | while (missed_updates) { | |
4880 | if (missed_updates % 2) | |
4881 | load = (load * degrade_factor[idx][j]) >> DEGRADE_SHIFT; | |
4882 | ||
4883 | missed_updates >>= 1; | |
4884 | j++; | |
4885 | } | |
4886 | return load; | |
4887 | } | |
9fd81dd5 | 4888 | #endif /* CONFIG_NO_HZ_COMMON */ |
3289bdb4 | 4889 | |
59543275 | 4890 | /** |
cee1afce | 4891 | * __cpu_load_update - update the rq->cpu_load[] statistics |
59543275 BP |
4892 | * @this_rq: The rq to update statistics for |
4893 | * @this_load: The current load | |
4894 | * @pending_updates: The number of missed updates | |
59543275 | 4895 | * |
3289bdb4 | 4896 | * Update rq->cpu_load[] statistics. This function is usually called every |
59543275 BP |
4897 | * scheduler tick (TICK_NSEC). |
4898 | * | |
4899 | * This function computes a decaying average: | |
4900 | * | |
4901 | * load[i]' = (1 - 1/2^i) * load[i] + (1/2^i) * load | |
4902 | * | |
4903 | * Because of NOHZ it might not get called on every tick which gives need for | |
4904 | * the @pending_updates argument. | |
4905 | * | |
4906 | * load[i]_n = (1 - 1/2^i) * load[i]_n-1 + (1/2^i) * load_n-1 | |
4907 | * = A * load[i]_n-1 + B ; A := (1 - 1/2^i), B := (1/2^i) * load | |
4908 | * = A * (A * load[i]_n-2 + B) + B | |
4909 | * = A * (A * (A * load[i]_n-3 + B) + B) + B | |
4910 | * = A^3 * load[i]_n-3 + (A^2 + A + 1) * B | |
4911 | * = A^n * load[i]_0 + (A^(n-1) + A^(n-2) + ... + 1) * B | |
4912 | * = A^n * load[i]_0 + ((1 - A^n) / (1 - A)) * B | |
4913 | * = (1 - 1/2^i)^n * (load[i]_0 - load) + load | |
4914 | * | |
4915 | * In the above we've assumed load_n := load, which is true for NOHZ_FULL as | |
4916 | * any change in load would have resulted in the tick being turned back on. | |
4917 | * | |
4918 | * For regular NOHZ, this reduces to: | |
4919 | * | |
4920 | * load[i]_n = (1 - 1/2^i)^n * load[i]_0 | |
4921 | * | |
4922 | * see decay_load_misses(). For NOHZ_FULL we get to subtract and add the extra | |
1f41906a | 4923 | * term. |
3289bdb4 | 4924 | */ |
1f41906a FW |
4925 | static void cpu_load_update(struct rq *this_rq, unsigned long this_load, |
4926 | unsigned long pending_updates) | |
3289bdb4 | 4927 | { |
9fd81dd5 | 4928 | unsigned long __maybe_unused tickless_load = this_rq->cpu_load[0]; |
3289bdb4 PZ |
4929 | int i, scale; |
4930 | ||
4931 | this_rq->nr_load_updates++; | |
4932 | ||
4933 | /* Update our load: */ | |
4934 | this_rq->cpu_load[0] = this_load; /* Fasttrack for idx 0 */ | |
4935 | for (i = 1, scale = 2; i < CPU_LOAD_IDX_MAX; i++, scale += scale) { | |
4936 | unsigned long old_load, new_load; | |
4937 | ||
4938 | /* scale is effectively 1 << i now, and >> i divides by scale */ | |
4939 | ||
7400d3bb | 4940 | old_load = this_rq->cpu_load[i]; |
9fd81dd5 | 4941 | #ifdef CONFIG_NO_HZ_COMMON |
3289bdb4 | 4942 | old_load = decay_load_missed(old_load, pending_updates - 1, i); |
7400d3bb BP |
4943 | if (tickless_load) { |
4944 | old_load -= decay_load_missed(tickless_load, pending_updates - 1, i); | |
4945 | /* | |
4946 | * old_load can never be a negative value because a | |
4947 | * decayed tickless_load cannot be greater than the | |
4948 | * original tickless_load. | |
4949 | */ | |
4950 | old_load += tickless_load; | |
4951 | } | |
9fd81dd5 | 4952 | #endif |
3289bdb4 PZ |
4953 | new_load = this_load; |
4954 | /* | |
4955 | * Round up the averaging division if load is increasing. This | |
4956 | * prevents us from getting stuck on 9 if the load is 10, for | |
4957 | * example. | |
4958 | */ | |
4959 | if (new_load > old_load) | |
4960 | new_load += scale - 1; | |
4961 | ||
4962 | this_rq->cpu_load[i] = (old_load * (scale - 1) + new_load) >> i; | |
4963 | } | |
4964 | ||
4965 | sched_avg_update(this_rq); | |
4966 | } | |
4967 | ||
7ea241af YD |
4968 | /* Used instead of source_load when we know the type == 0 */ |
4969 | static unsigned long weighted_cpuload(const int cpu) | |
4970 | { | |
4971 | return cfs_rq_runnable_load_avg(&cpu_rq(cpu)->cfs); | |
4972 | } | |
4973 | ||
3289bdb4 | 4974 | #ifdef CONFIG_NO_HZ_COMMON |
1f41906a FW |
4975 | /* |
4976 | * There is no sane way to deal with nohz on smp when using jiffies because the | |
4977 | * cpu doing the jiffies update might drift wrt the cpu doing the jiffy reading | |
4978 | * causing off-by-one errors in observed deltas; {0,2} instead of {1,1}. | |
4979 | * | |
4980 | * Therefore we need to avoid the delta approach from the regular tick when | |
4981 | * possible since that would seriously skew the load calculation. This is why we | |
4982 | * use cpu_load_update_periodic() for CPUs out of nohz. However we'll rely on | |
4983 | * jiffies deltas for updates happening while in nohz mode (idle ticks, idle | |
4984 | * loop exit, nohz_idle_balance, nohz full exit...) | |
4985 | * | |
4986 | * This means we might still be one tick off for nohz periods. | |
4987 | */ | |
4988 | ||
4989 | static void cpu_load_update_nohz(struct rq *this_rq, | |
4990 | unsigned long curr_jiffies, | |
4991 | unsigned long load) | |
be68a682 FW |
4992 | { |
4993 | unsigned long pending_updates; | |
4994 | ||
4995 | pending_updates = curr_jiffies - this_rq->last_load_update_tick; | |
4996 | if (pending_updates) { | |
4997 | this_rq->last_load_update_tick = curr_jiffies; | |
4998 | /* | |
4999 | * In the regular NOHZ case, we were idle, this means load 0. | |
5000 | * In the NOHZ_FULL case, we were non-idle, we should consider | |
5001 | * its weighted load. | |
5002 | */ | |
1f41906a | 5003 | cpu_load_update(this_rq, load, pending_updates); |
be68a682 FW |
5004 | } |
5005 | } | |
5006 | ||
3289bdb4 PZ |
5007 | /* |
5008 | * Called from nohz_idle_balance() to update the load ratings before doing the | |
5009 | * idle balance. | |
5010 | */ | |
cee1afce | 5011 | static void cpu_load_update_idle(struct rq *this_rq) |
3289bdb4 | 5012 | { |
3289bdb4 PZ |
5013 | /* |
5014 | * bail if there's load or we're actually up-to-date. | |
5015 | */ | |
be68a682 | 5016 | if (weighted_cpuload(cpu_of(this_rq))) |
3289bdb4 PZ |
5017 | return; |
5018 | ||
1f41906a | 5019 | cpu_load_update_nohz(this_rq, READ_ONCE(jiffies), 0); |
3289bdb4 PZ |
5020 | } |
5021 | ||
5022 | /* | |
1f41906a FW |
5023 | * Record CPU load on nohz entry so we know the tickless load to account |
5024 | * on nohz exit. cpu_load[0] happens then to be updated more frequently | |
5025 | * than other cpu_load[idx] but it should be fine as cpu_load readers | |
5026 | * shouldn't rely into synchronized cpu_load[*] updates. | |
3289bdb4 | 5027 | */ |
1f41906a | 5028 | void cpu_load_update_nohz_start(void) |
3289bdb4 PZ |
5029 | { |
5030 | struct rq *this_rq = this_rq(); | |
1f41906a FW |
5031 | |
5032 | /* | |
5033 | * This is all lockless but should be fine. If weighted_cpuload changes | |
5034 | * concurrently we'll exit nohz. And cpu_load write can race with | |
5035 | * cpu_load_update_idle() but both updater would be writing the same. | |
5036 | */ | |
5037 | this_rq->cpu_load[0] = weighted_cpuload(cpu_of(this_rq)); | |
5038 | } | |
5039 | ||
5040 | /* | |
5041 | * Account the tickless load in the end of a nohz frame. | |
5042 | */ | |
5043 | void cpu_load_update_nohz_stop(void) | |
5044 | { | |
316c1608 | 5045 | unsigned long curr_jiffies = READ_ONCE(jiffies); |
1f41906a FW |
5046 | struct rq *this_rq = this_rq(); |
5047 | unsigned long load; | |
3289bdb4 PZ |
5048 | |
5049 | if (curr_jiffies == this_rq->last_load_update_tick) | |
5050 | return; | |
5051 | ||
1f41906a | 5052 | load = weighted_cpuload(cpu_of(this_rq)); |
3289bdb4 | 5053 | raw_spin_lock(&this_rq->lock); |
b52fad2d | 5054 | update_rq_clock(this_rq); |
1f41906a | 5055 | cpu_load_update_nohz(this_rq, curr_jiffies, load); |
3289bdb4 PZ |
5056 | raw_spin_unlock(&this_rq->lock); |
5057 | } | |
1f41906a FW |
5058 | #else /* !CONFIG_NO_HZ_COMMON */ |
5059 | static inline void cpu_load_update_nohz(struct rq *this_rq, | |
5060 | unsigned long curr_jiffies, | |
5061 | unsigned long load) { } | |
5062 | #endif /* CONFIG_NO_HZ_COMMON */ | |
5063 | ||
5064 | static void cpu_load_update_periodic(struct rq *this_rq, unsigned long load) | |
5065 | { | |
9fd81dd5 | 5066 | #ifdef CONFIG_NO_HZ_COMMON |
1f41906a FW |
5067 | /* See the mess around cpu_load_update_nohz(). */ |
5068 | this_rq->last_load_update_tick = READ_ONCE(jiffies); | |
9fd81dd5 | 5069 | #endif |
1f41906a FW |
5070 | cpu_load_update(this_rq, load, 1); |
5071 | } | |
3289bdb4 PZ |
5072 | |
5073 | /* | |
5074 | * Called from scheduler_tick() | |
5075 | */ | |
cee1afce | 5076 | void cpu_load_update_active(struct rq *this_rq) |
3289bdb4 | 5077 | { |
7ea241af | 5078 | unsigned long load = weighted_cpuload(cpu_of(this_rq)); |
1f41906a FW |
5079 | |
5080 | if (tick_nohz_tick_stopped()) | |
5081 | cpu_load_update_nohz(this_rq, READ_ONCE(jiffies), load); | |
5082 | else | |
5083 | cpu_load_update_periodic(this_rq, load); | |
3289bdb4 PZ |
5084 | } |
5085 | ||
029632fb PZ |
5086 | /* |
5087 | * Return a low guess at the load of a migration-source cpu weighted | |
5088 | * according to the scheduling class and "nice" value. | |
5089 | * | |
5090 | * We want to under-estimate the load of migration sources, to | |
5091 | * balance conservatively. | |
5092 | */ | |
5093 | static unsigned long source_load(int cpu, int type) | |
5094 | { | |
5095 | struct rq *rq = cpu_rq(cpu); | |
5096 | unsigned long total = weighted_cpuload(cpu); | |
5097 | ||
5098 | if (type == 0 || !sched_feat(LB_BIAS)) | |
5099 | return total; | |
5100 | ||
5101 | return min(rq->cpu_load[type-1], total); | |
5102 | } | |
5103 | ||
5104 | /* | |
5105 | * Return a high guess at the load of a migration-target cpu weighted | |
5106 | * according to the scheduling class and "nice" value. | |
5107 | */ | |
5108 | static unsigned long target_load(int cpu, int type) | |
5109 | { | |
5110 | struct rq *rq = cpu_rq(cpu); | |
5111 | unsigned long total = weighted_cpuload(cpu); | |
5112 | ||
5113 | if (type == 0 || !sched_feat(LB_BIAS)) | |
5114 | return total; | |
5115 | ||
5116 | return max(rq->cpu_load[type-1], total); | |
5117 | } | |
5118 | ||
ced549fa | 5119 | static unsigned long capacity_of(int cpu) |
029632fb | 5120 | { |
ced549fa | 5121 | return cpu_rq(cpu)->cpu_capacity; |
029632fb PZ |
5122 | } |
5123 | ||
ca6d75e6 VG |
5124 | static unsigned long capacity_orig_of(int cpu) |
5125 | { | |
5126 | return cpu_rq(cpu)->cpu_capacity_orig; | |
5127 | } | |
5128 | ||
029632fb PZ |
5129 | static unsigned long cpu_avg_load_per_task(int cpu) |
5130 | { | |
5131 | struct rq *rq = cpu_rq(cpu); | |
316c1608 | 5132 | unsigned long nr_running = READ_ONCE(rq->cfs.h_nr_running); |
7ea241af | 5133 | unsigned long load_avg = weighted_cpuload(cpu); |
029632fb PZ |
5134 | |
5135 | if (nr_running) | |
b92486cb | 5136 | return load_avg / nr_running; |
029632fb PZ |
5137 | |
5138 | return 0; | |
5139 | } | |
5140 | ||
bb3469ac | 5141 | #ifdef CONFIG_FAIR_GROUP_SCHED |
f5bfb7d9 PZ |
5142 | /* |
5143 | * effective_load() calculates the load change as seen from the root_task_group | |
5144 | * | |
5145 | * Adding load to a group doesn't make a group heavier, but can cause movement | |
5146 | * of group shares between cpus. Assuming the shares were perfectly aligned one | |
5147 | * can calculate the shift in shares. | |
cf5f0acf PZ |
5148 | * |
5149 | * Calculate the effective load difference if @wl is added (subtracted) to @tg | |
5150 | * on this @cpu and results in a total addition (subtraction) of @wg to the | |
5151 | * total group weight. | |
5152 | * | |
5153 | * Given a runqueue weight distribution (rw_i) we can compute a shares | |
5154 | * distribution (s_i) using: | |
5155 | * | |
5156 | * s_i = rw_i / \Sum rw_j (1) | |
5157 | * | |
5158 | * Suppose we have 4 CPUs and our @tg is a direct child of the root group and | |
5159 | * has 7 equal weight tasks, distributed as below (rw_i), with the resulting | |
5160 | * shares distribution (s_i): | |
5161 | * | |
5162 | * rw_i = { 2, 4, 1, 0 } | |
5163 | * s_i = { 2/7, 4/7, 1/7, 0 } | |
5164 | * | |
5165 | * As per wake_affine() we're interested in the load of two CPUs (the CPU the | |
5166 | * task used to run on and the CPU the waker is running on), we need to | |
5167 | * compute the effect of waking a task on either CPU and, in case of a sync | |
5168 | * wakeup, compute the effect of the current task going to sleep. | |
5169 | * | |
5170 | * So for a change of @wl to the local @cpu with an overall group weight change | |
5171 | * of @wl we can compute the new shares distribution (s'_i) using: | |
5172 | * | |
5173 | * s'_i = (rw_i + @wl) / (@wg + \Sum rw_j) (2) | |
5174 | * | |
5175 | * Suppose we're interested in CPUs 0 and 1, and want to compute the load | |
5176 | * differences in waking a task to CPU 0. The additional task changes the | |
5177 | * weight and shares distributions like: | |
5178 | * | |
5179 | * rw'_i = { 3, 4, 1, 0 } | |
5180 | * s'_i = { 3/8, 4/8, 1/8, 0 } | |
5181 | * | |
5182 | * We can then compute the difference in effective weight by using: | |
5183 | * | |
5184 | * dw_i = S * (s'_i - s_i) (3) | |
5185 | * | |
5186 | * Where 'S' is the group weight as seen by its parent. | |
5187 | * | |
5188 | * Therefore the effective change in loads on CPU 0 would be 5/56 (3/8 - 2/7) | |
5189 | * times the weight of the group. The effect on CPU 1 would be -4/56 (4/8 - | |
5190 | * 4/7) times the weight of the group. | |
f5bfb7d9 | 5191 | */ |
2069dd75 | 5192 | static long effective_load(struct task_group *tg, int cpu, long wl, long wg) |
bb3469ac | 5193 | { |
4be9daaa | 5194 | struct sched_entity *se = tg->se[cpu]; |
f1d239f7 | 5195 | |
9722c2da | 5196 | if (!tg->parent) /* the trivial, non-cgroup case */ |
f1d239f7 PZ |
5197 | return wl; |
5198 | ||
4be9daaa | 5199 | for_each_sched_entity(se) { |
7dd49125 PZ |
5200 | struct cfs_rq *cfs_rq = se->my_q; |
5201 | long W, w = cfs_rq_load_avg(cfs_rq); | |
4be9daaa | 5202 | |
7dd49125 | 5203 | tg = cfs_rq->tg; |
bb3469ac | 5204 | |
cf5f0acf PZ |
5205 | /* |
5206 | * W = @wg + \Sum rw_j | |
5207 | */ | |
7dd49125 PZ |
5208 | W = wg + atomic_long_read(&tg->load_avg); |
5209 | ||
5210 | /* Ensure \Sum rw_j >= rw_i */ | |
5211 | W -= cfs_rq->tg_load_avg_contrib; | |
5212 | W += w; | |
4be9daaa | 5213 | |
cf5f0acf PZ |
5214 | /* |
5215 | * w = rw_i + @wl | |
5216 | */ | |
7dd49125 | 5217 | w += wl; |
940959e9 | 5218 | |
cf5f0acf PZ |
5219 | /* |
5220 | * wl = S * s'_i; see (2) | |
5221 | */ | |
5222 | if (W > 0 && w < W) | |
ab522e33 | 5223 | wl = (w * (long)scale_load_down(tg->shares)) / W; |
977dda7c | 5224 | else |
ab522e33 | 5225 | wl = scale_load_down(tg->shares); |
940959e9 | 5226 | |
cf5f0acf PZ |
5227 | /* |
5228 | * Per the above, wl is the new se->load.weight value; since | |
5229 | * those are clipped to [MIN_SHARES, ...) do so now. See | |
5230 | * calc_cfs_shares(). | |
5231 | */ | |
977dda7c PT |
5232 | if (wl < MIN_SHARES) |
5233 | wl = MIN_SHARES; | |
cf5f0acf PZ |
5234 | |
5235 | /* | |
5236 | * wl = dw_i = S * (s'_i - s_i); see (3) | |
5237 | */ | |
9d89c257 | 5238 | wl -= se->avg.load_avg; |
cf5f0acf PZ |
5239 | |
5240 | /* | |
5241 | * Recursively apply this logic to all parent groups to compute | |
5242 | * the final effective load change on the root group. Since | |
5243 | * only the @tg group gets extra weight, all parent groups can | |
5244 | * only redistribute existing shares. @wl is the shift in shares | |
5245 | * resulting from this level per the above. | |
5246 | */ | |
4be9daaa | 5247 | wg = 0; |
4be9daaa | 5248 | } |
bb3469ac | 5249 | |
4be9daaa | 5250 | return wl; |
bb3469ac PZ |
5251 | } |
5252 | #else | |
4be9daaa | 5253 | |
58d081b5 | 5254 | static long effective_load(struct task_group *tg, int cpu, long wl, long wg) |
4be9daaa | 5255 | { |
83378269 | 5256 | return wl; |
bb3469ac | 5257 | } |
4be9daaa | 5258 | |
bb3469ac PZ |
5259 | #endif |
5260 | ||
c58d25f3 PZ |
5261 | static void record_wakee(struct task_struct *p) |
5262 | { | |
5263 | /* | |
5264 | * Only decay a single time; tasks that have less then 1 wakeup per | |
5265 | * jiffy will not have built up many flips. | |
5266 | */ | |
5267 | if (time_after(jiffies, current->wakee_flip_decay_ts + HZ)) { | |
5268 | current->wakee_flips >>= 1; | |
5269 | current->wakee_flip_decay_ts = jiffies; | |
5270 | } | |
5271 | ||
5272 | if (current->last_wakee != p) { | |
5273 | current->last_wakee = p; | |
5274 | current->wakee_flips++; | |
5275 | } | |
5276 | } | |
5277 | ||
63b0e9ed MG |
5278 | /* |
5279 | * Detect M:N waker/wakee relationships via a switching-frequency heuristic. | |
c58d25f3 | 5280 | * |
63b0e9ed | 5281 | * A waker of many should wake a different task than the one last awakened |
c58d25f3 PZ |
5282 | * at a frequency roughly N times higher than one of its wakees. |
5283 | * | |
5284 | * In order to determine whether we should let the load spread vs consolidating | |
5285 | * to shared cache, we look for a minimum 'flip' frequency of llc_size in one | |
5286 | * partner, and a factor of lls_size higher frequency in the other. | |
5287 | * | |
5288 | * With both conditions met, we can be relatively sure that the relationship is | |
5289 | * non-monogamous, with partner count exceeding socket size. | |
5290 | * | |
5291 | * Waker/wakee being client/server, worker/dispatcher, interrupt source or | |
5292 | * whatever is irrelevant, spread criteria is apparent partner count exceeds | |
5293 | * socket size. | |
63b0e9ed | 5294 | */ |
62470419 MW |
5295 | static int wake_wide(struct task_struct *p) |
5296 | { | |
63b0e9ed MG |
5297 | unsigned int master = current->wakee_flips; |
5298 | unsigned int slave = p->wakee_flips; | |
7d9ffa89 | 5299 | int factor = this_cpu_read(sd_llc_size); |
62470419 | 5300 | |
63b0e9ed MG |
5301 | if (master < slave) |
5302 | swap(master, slave); | |
5303 | if (slave < factor || master < slave * factor) | |
5304 | return 0; | |
5305 | return 1; | |
62470419 MW |
5306 | } |
5307 | ||
772bd008 MR |
5308 | static int wake_affine(struct sched_domain *sd, struct task_struct *p, |
5309 | int prev_cpu, int sync) | |
098fb9db | 5310 | { |
e37b6a7b | 5311 | s64 this_load, load; |
bd61c98f | 5312 | s64 this_eff_load, prev_eff_load; |
772bd008 | 5313 | int idx, this_cpu; |
c88d5910 | 5314 | struct task_group *tg; |
83378269 | 5315 | unsigned long weight; |
b3137bc8 | 5316 | int balanced; |
098fb9db | 5317 | |
c88d5910 PZ |
5318 | idx = sd->wake_idx; |
5319 | this_cpu = smp_processor_id(); | |
c88d5910 PZ |
5320 | load = source_load(prev_cpu, idx); |
5321 | this_load = target_load(this_cpu, idx); | |
098fb9db | 5322 | |
b3137bc8 MG |
5323 | /* |
5324 | * If sync wakeup then subtract the (maximum possible) | |
5325 | * effect of the currently running task from the load | |
5326 | * of the current CPU: | |
5327 | */ | |
83378269 PZ |
5328 | if (sync) { |
5329 | tg = task_group(current); | |
9d89c257 | 5330 | weight = current->se.avg.load_avg; |
83378269 | 5331 | |
c88d5910 | 5332 | this_load += effective_load(tg, this_cpu, -weight, -weight); |
83378269 PZ |
5333 | load += effective_load(tg, prev_cpu, 0, -weight); |
5334 | } | |
b3137bc8 | 5335 | |
83378269 | 5336 | tg = task_group(p); |
9d89c257 | 5337 | weight = p->se.avg.load_avg; |
b3137bc8 | 5338 | |
71a29aa7 PZ |
5339 | /* |
5340 | * In low-load situations, where prev_cpu is idle and this_cpu is idle | |
c88d5910 PZ |
5341 | * due to the sync cause above having dropped this_load to 0, we'll |
5342 | * always have an imbalance, but there's really nothing you can do | |
5343 | * about that, so that's good too. | |
71a29aa7 PZ |
5344 | * |
5345 | * Otherwise check if either cpus are near enough in load to allow this | |
5346 | * task to be woken on this_cpu. | |
5347 | */ | |
bd61c98f VG |
5348 | this_eff_load = 100; |
5349 | this_eff_load *= capacity_of(prev_cpu); | |
e51fd5e2 | 5350 | |
bd61c98f VG |
5351 | prev_eff_load = 100 + (sd->imbalance_pct - 100) / 2; |
5352 | prev_eff_load *= capacity_of(this_cpu); | |
e51fd5e2 | 5353 | |
bd61c98f | 5354 | if (this_load > 0) { |
e51fd5e2 PZ |
5355 | this_eff_load *= this_load + |
5356 | effective_load(tg, this_cpu, weight, weight); | |
5357 | ||
e51fd5e2 | 5358 | prev_eff_load *= load + effective_load(tg, prev_cpu, 0, weight); |
bd61c98f | 5359 | } |
e51fd5e2 | 5360 | |
bd61c98f | 5361 | balanced = this_eff_load <= prev_eff_load; |
098fb9db | 5362 | |
ae92882e | 5363 | schedstat_inc(p->se.statistics.nr_wakeups_affine_attempts); |
098fb9db | 5364 | |
05bfb65f VG |
5365 | if (!balanced) |
5366 | return 0; | |
098fb9db | 5367 | |
ae92882e JP |
5368 | schedstat_inc(sd->ttwu_move_affine); |
5369 | schedstat_inc(p->se.statistics.nr_wakeups_affine); | |
05bfb65f VG |
5370 | |
5371 | return 1; | |
098fb9db IM |
5372 | } |
5373 | ||
6a0b19c0 MR |
5374 | static inline int task_util(struct task_struct *p); |
5375 | static int cpu_util_wake(int cpu, struct task_struct *p); | |
5376 | ||
5377 | static unsigned long capacity_spare_wake(int cpu, struct task_struct *p) | |
5378 | { | |
5379 | return capacity_orig_of(cpu) - cpu_util_wake(cpu, p); | |
5380 | } | |
5381 | ||
aaee1203 PZ |
5382 | /* |
5383 | * find_idlest_group finds and returns the least busy CPU group within the | |
5384 | * domain. | |
5385 | */ | |
5386 | static struct sched_group * | |
78e7ed53 | 5387 | find_idlest_group(struct sched_domain *sd, struct task_struct *p, |
c44f2a02 | 5388 | int this_cpu, int sd_flag) |
e7693a36 | 5389 | { |
b3bd3de6 | 5390 | struct sched_group *idlest = NULL, *group = sd->groups; |
6a0b19c0 | 5391 | struct sched_group *most_spare_sg = NULL; |
aaee1203 | 5392 | unsigned long min_load = ULONG_MAX, this_load = 0; |
6a0b19c0 | 5393 | unsigned long most_spare = 0, this_spare = 0; |
c44f2a02 | 5394 | int load_idx = sd->forkexec_idx; |
aaee1203 | 5395 | int imbalance = 100 + (sd->imbalance_pct-100)/2; |
e7693a36 | 5396 | |
c44f2a02 VG |
5397 | if (sd_flag & SD_BALANCE_WAKE) |
5398 | load_idx = sd->wake_idx; | |
5399 | ||
aaee1203 | 5400 | do { |
6a0b19c0 | 5401 | unsigned long load, avg_load, spare_cap, max_spare_cap; |
aaee1203 PZ |
5402 | int local_group; |
5403 | int i; | |
e7693a36 | 5404 | |
aaee1203 PZ |
5405 | /* Skip over this group if it has no CPUs allowed */ |
5406 | if (!cpumask_intersects(sched_group_cpus(group), | |
fa17b507 | 5407 | tsk_cpus_allowed(p))) |
aaee1203 PZ |
5408 | continue; |
5409 | ||
5410 | local_group = cpumask_test_cpu(this_cpu, | |
5411 | sched_group_cpus(group)); | |
5412 | ||
6a0b19c0 MR |
5413 | /* |
5414 | * Tally up the load of all CPUs in the group and find | |
5415 | * the group containing the CPU with most spare capacity. | |
5416 | */ | |
aaee1203 | 5417 | avg_load = 0; |
6a0b19c0 | 5418 | max_spare_cap = 0; |
aaee1203 PZ |
5419 | |
5420 | for_each_cpu(i, sched_group_cpus(group)) { | |
5421 | /* Bias balancing toward cpus of our domain */ | |
5422 | if (local_group) | |
5423 | load = source_load(i, load_idx); | |
5424 | else | |
5425 | load = target_load(i, load_idx); | |
5426 | ||
5427 | avg_load += load; | |
6a0b19c0 MR |
5428 | |
5429 | spare_cap = capacity_spare_wake(i, p); | |
5430 | ||
5431 | if (spare_cap > max_spare_cap) | |
5432 | max_spare_cap = spare_cap; | |
aaee1203 PZ |
5433 | } |
5434 | ||
63b2ca30 | 5435 | /* Adjust by relative CPU capacity of the group */ |
ca8ce3d0 | 5436 | avg_load = (avg_load * SCHED_CAPACITY_SCALE) / group->sgc->capacity; |
aaee1203 PZ |
5437 | |
5438 | if (local_group) { | |
5439 | this_load = avg_load; | |
6a0b19c0 MR |
5440 | this_spare = max_spare_cap; |
5441 | } else { | |
5442 | if (avg_load < min_load) { | |
5443 | min_load = avg_load; | |
5444 | idlest = group; | |
5445 | } | |
5446 | ||
5447 | if (most_spare < max_spare_cap) { | |
5448 | most_spare = max_spare_cap; | |
5449 | most_spare_sg = group; | |
5450 | } | |
aaee1203 PZ |
5451 | } |
5452 | } while (group = group->next, group != sd->groups); | |
5453 | ||
6a0b19c0 MR |
5454 | /* |
5455 | * The cross-over point between using spare capacity or least load | |
5456 | * is too conservative for high utilization tasks on partially | |
5457 | * utilized systems if we require spare_capacity > task_util(p), | |
5458 | * so we allow for some task stuffing by using | |
5459 | * spare_capacity > task_util(p)/2. | |
5460 | */ | |
5461 | if (this_spare > task_util(p) / 2 && | |
5462 | imbalance*this_spare > 100*most_spare) | |
5463 | return NULL; | |
5464 | else if (most_spare > task_util(p) / 2) | |
5465 | return most_spare_sg; | |
5466 | ||
aaee1203 PZ |
5467 | if (!idlest || 100*this_load < imbalance*min_load) |
5468 | return NULL; | |
5469 | return idlest; | |
5470 | } | |
5471 | ||
5472 | /* | |
5473 | * find_idlest_cpu - find the idlest cpu among the cpus in group. | |
5474 | */ | |
5475 | static int | |
5476 | find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu) | |
5477 | { | |
5478 | unsigned long load, min_load = ULONG_MAX; | |
83a0a96a NP |
5479 | unsigned int min_exit_latency = UINT_MAX; |
5480 | u64 latest_idle_timestamp = 0; | |
5481 | int least_loaded_cpu = this_cpu; | |
5482 | int shallowest_idle_cpu = -1; | |
aaee1203 PZ |
5483 | int i; |
5484 | ||
eaecf41f MR |
5485 | /* Check if we have any choice: */ |
5486 | if (group->group_weight == 1) | |
5487 | return cpumask_first(sched_group_cpus(group)); | |
5488 | ||
aaee1203 | 5489 | /* Traverse only the allowed CPUs */ |
fa17b507 | 5490 | for_each_cpu_and(i, sched_group_cpus(group), tsk_cpus_allowed(p)) { |
83a0a96a NP |
5491 | if (idle_cpu(i)) { |
5492 | struct rq *rq = cpu_rq(i); | |
5493 | struct cpuidle_state *idle = idle_get_state(rq); | |
5494 | if (idle && idle->exit_latency < min_exit_latency) { | |
5495 | /* | |
5496 | * We give priority to a CPU whose idle state | |
5497 | * has the smallest exit latency irrespective | |
5498 | * of any idle timestamp. | |
5499 | */ | |
5500 | min_exit_latency = idle->exit_latency; | |
5501 | latest_idle_timestamp = rq->idle_stamp; | |
5502 | shallowest_idle_cpu = i; | |
5503 | } else if ((!idle || idle->exit_latency == min_exit_latency) && | |
5504 | rq->idle_stamp > latest_idle_timestamp) { | |
5505 | /* | |
5506 | * If equal or no active idle state, then | |
5507 | * the most recently idled CPU might have | |
5508 | * a warmer cache. | |
5509 | */ | |
5510 | latest_idle_timestamp = rq->idle_stamp; | |
5511 | shallowest_idle_cpu = i; | |
5512 | } | |
9f96742a | 5513 | } else if (shallowest_idle_cpu == -1) { |
83a0a96a NP |
5514 | load = weighted_cpuload(i); |
5515 | if (load < min_load || (load == min_load && i == this_cpu)) { | |
5516 | min_load = load; | |
5517 | least_loaded_cpu = i; | |
5518 | } | |
e7693a36 GH |
5519 | } |
5520 | } | |
5521 | ||
83a0a96a | 5522 | return shallowest_idle_cpu != -1 ? shallowest_idle_cpu : least_loaded_cpu; |
aaee1203 | 5523 | } |
e7693a36 | 5524 | |
a50bde51 | 5525 | /* |
10e2f1ac PZ |
5526 | * Implement a for_each_cpu() variant that starts the scan at a given cpu |
5527 | * (@start), and wraps around. | |
5528 | * | |
5529 | * This is used to scan for idle CPUs; such that not all CPUs looking for an | |
5530 | * idle CPU find the same CPU. The down-side is that tasks tend to cycle | |
5531 | * through the LLC domain. | |
5532 | * | |
5533 | * Especially tbench is found sensitive to this. | |
5534 | */ | |
5535 | ||
5536 | static int cpumask_next_wrap(int n, const struct cpumask *mask, int start, int *wrapped) | |
5537 | { | |
5538 | int next; | |
5539 | ||
5540 | again: | |
5541 | next = find_next_bit(cpumask_bits(mask), nr_cpumask_bits, n+1); | |
5542 | ||
5543 | if (*wrapped) { | |
5544 | if (next >= start) | |
5545 | return nr_cpumask_bits; | |
5546 | } else { | |
5547 | if (next >= nr_cpumask_bits) { | |
5548 | *wrapped = 1; | |
5549 | n = -1; | |
5550 | goto again; | |
5551 | } | |
5552 | } | |
5553 | ||
5554 | return next; | |
5555 | } | |
5556 | ||
5557 | #define for_each_cpu_wrap(cpu, mask, start, wrap) \ | |
5558 | for ((wrap) = 0, (cpu) = (start)-1; \ | |
5559 | (cpu) = cpumask_next_wrap((cpu), (mask), (start), &(wrap)), \ | |
5560 | (cpu) < nr_cpumask_bits; ) | |
5561 | ||
5562 | #ifdef CONFIG_SCHED_SMT | |
5563 | ||
5564 | static inline void set_idle_cores(int cpu, int val) | |
5565 | { | |
5566 | struct sched_domain_shared *sds; | |
5567 | ||
5568 | sds = rcu_dereference(per_cpu(sd_llc_shared, cpu)); | |
5569 | if (sds) | |
5570 | WRITE_ONCE(sds->has_idle_cores, val); | |
5571 | } | |
5572 | ||
5573 | static inline bool test_idle_cores(int cpu, bool def) | |
5574 | { | |
5575 | struct sched_domain_shared *sds; | |
5576 | ||
5577 | sds = rcu_dereference(per_cpu(sd_llc_shared, cpu)); | |
5578 | if (sds) | |
5579 | return READ_ONCE(sds->has_idle_cores); | |
5580 | ||
5581 | return def; | |
5582 | } | |
5583 | ||
5584 | /* | |
5585 | * Scans the local SMT mask to see if the entire core is idle, and records this | |
5586 | * information in sd_llc_shared->has_idle_cores. | |
5587 | * | |
5588 | * Since SMT siblings share all cache levels, inspecting this limited remote | |
5589 | * state should be fairly cheap. | |
5590 | */ | |
1b568f0a | 5591 | void __update_idle_core(struct rq *rq) |
10e2f1ac PZ |
5592 | { |
5593 | int core = cpu_of(rq); | |
5594 | int cpu; | |
5595 | ||
5596 | rcu_read_lock(); | |
5597 | if (test_idle_cores(core, true)) | |
5598 | goto unlock; | |
5599 | ||
5600 | for_each_cpu(cpu, cpu_smt_mask(core)) { | |
5601 | if (cpu == core) | |
5602 | continue; | |
5603 | ||
5604 | if (!idle_cpu(cpu)) | |
5605 | goto unlock; | |
5606 | } | |
5607 | ||
5608 | set_idle_cores(core, 1); | |
5609 | unlock: | |
5610 | rcu_read_unlock(); | |
5611 | } | |
5612 | ||
5613 | /* | |
5614 | * Scan the entire LLC domain for idle cores; this dynamically switches off if | |
5615 | * there are no idle cores left in the system; tracked through | |
5616 | * sd_llc->shared->has_idle_cores and enabled through update_idle_core() above. | |
5617 | */ | |
5618 | static int select_idle_core(struct task_struct *p, struct sched_domain *sd, int target) | |
5619 | { | |
5620 | struct cpumask *cpus = this_cpu_cpumask_var_ptr(select_idle_mask); | |
5621 | int core, cpu, wrap; | |
5622 | ||
1b568f0a PZ |
5623 | if (!static_branch_likely(&sched_smt_present)) |
5624 | return -1; | |
5625 | ||
10e2f1ac PZ |
5626 | if (!test_idle_cores(target, false)) |
5627 | return -1; | |
5628 | ||
5629 | cpumask_and(cpus, sched_domain_span(sd), tsk_cpus_allowed(p)); | |
5630 | ||
5631 | for_each_cpu_wrap(core, cpus, target, wrap) { | |
5632 | bool idle = true; | |
5633 | ||
5634 | for_each_cpu(cpu, cpu_smt_mask(core)) { | |
5635 | cpumask_clear_cpu(cpu, cpus); | |
5636 | if (!idle_cpu(cpu)) | |
5637 | idle = false; | |
5638 | } | |
5639 | ||
5640 | if (idle) | |
5641 | return core; | |
5642 | } | |
5643 | ||
5644 | /* | |
5645 | * Failed to find an idle core; stop looking for one. | |
5646 | */ | |
5647 | set_idle_cores(target, 0); | |
5648 | ||
5649 | return -1; | |
5650 | } | |
5651 | ||
5652 | /* | |
5653 | * Scan the local SMT mask for idle CPUs. | |
5654 | */ | |
5655 | static int select_idle_smt(struct task_struct *p, struct sched_domain *sd, int target) | |
5656 | { | |
5657 | int cpu; | |
5658 | ||
1b568f0a PZ |
5659 | if (!static_branch_likely(&sched_smt_present)) |
5660 | return -1; | |
5661 | ||
10e2f1ac PZ |
5662 | for_each_cpu(cpu, cpu_smt_mask(target)) { |
5663 | if (!cpumask_test_cpu(cpu, tsk_cpus_allowed(p))) | |
5664 | continue; | |
5665 | if (idle_cpu(cpu)) | |
5666 | return cpu; | |
5667 | } | |
5668 | ||
5669 | return -1; | |
5670 | } | |
5671 | ||
5672 | #else /* CONFIG_SCHED_SMT */ | |
5673 | ||
5674 | static inline int select_idle_core(struct task_struct *p, struct sched_domain *sd, int target) | |
5675 | { | |
5676 | return -1; | |
5677 | } | |
5678 | ||
5679 | static inline int select_idle_smt(struct task_struct *p, struct sched_domain *sd, int target) | |
5680 | { | |
5681 | return -1; | |
5682 | } | |
5683 | ||
5684 | #endif /* CONFIG_SCHED_SMT */ | |
5685 | ||
5686 | /* | |
5687 | * Scan the LLC domain for idle CPUs; this is dynamically regulated by | |
5688 | * comparing the average scan cost (tracked in sd->avg_scan_cost) against the | |
5689 | * average idle time for this rq (as found in rq->avg_idle). | |
a50bde51 | 5690 | */ |
10e2f1ac PZ |
5691 | static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, int target) |
5692 | { | |
9cfb38a7 WL |
5693 | struct sched_domain *this_sd; |
5694 | u64 avg_cost, avg_idle = this_rq()->avg_idle; | |
10e2f1ac PZ |
5695 | u64 time, cost; |
5696 | s64 delta; | |
5697 | int cpu, wrap; | |
5698 | ||
9cfb38a7 WL |
5699 | this_sd = rcu_dereference(*this_cpu_ptr(&sd_llc)); |
5700 | if (!this_sd) | |
5701 | return -1; | |
5702 | ||
5703 | avg_cost = this_sd->avg_scan_cost; | |
5704 | ||
10e2f1ac PZ |
5705 | /* |
5706 | * Due to large variance we need a large fuzz factor; hackbench in | |
5707 | * particularly is sensitive here. | |
5708 | */ | |
5709 | if ((avg_idle / 512) < avg_cost) | |
5710 | return -1; | |
5711 | ||
5712 | time = local_clock(); | |
5713 | ||
5714 | for_each_cpu_wrap(cpu, sched_domain_span(sd), target, wrap) { | |
5715 | if (!cpumask_test_cpu(cpu, tsk_cpus_allowed(p))) | |
5716 | continue; | |
5717 | if (idle_cpu(cpu)) | |
5718 | break; | |
5719 | } | |
5720 | ||
5721 | time = local_clock() - time; | |
5722 | cost = this_sd->avg_scan_cost; | |
5723 | delta = (s64)(time - cost) / 8; | |
5724 | this_sd->avg_scan_cost += delta; | |
5725 | ||
5726 | return cpu; | |
5727 | } | |
5728 | ||
5729 | /* | |
5730 | * Try and locate an idle core/thread in the LLC cache domain. | |
a50bde51 | 5731 | */ |
772bd008 | 5732 | static int select_idle_sibling(struct task_struct *p, int prev, int target) |
a50bde51 | 5733 | { |
99bd5e2f | 5734 | struct sched_domain *sd; |
10e2f1ac | 5735 | int i; |
a50bde51 | 5736 | |
e0a79f52 MG |
5737 | if (idle_cpu(target)) |
5738 | return target; | |
99bd5e2f SS |
5739 | |
5740 | /* | |
10e2f1ac | 5741 | * If the previous cpu is cache affine and idle, don't be stupid. |
99bd5e2f | 5742 | */ |
772bd008 MR |
5743 | if (prev != target && cpus_share_cache(prev, target) && idle_cpu(prev)) |
5744 | return prev; | |
a50bde51 | 5745 | |
518cd623 | 5746 | sd = rcu_dereference(per_cpu(sd_llc, target)); |
10e2f1ac PZ |
5747 | if (!sd) |
5748 | return target; | |
772bd008 | 5749 | |
10e2f1ac PZ |
5750 | i = select_idle_core(p, sd, target); |
5751 | if ((unsigned)i < nr_cpumask_bits) | |
5752 | return i; | |
37407ea7 | 5753 | |
10e2f1ac PZ |
5754 | i = select_idle_cpu(p, sd, target); |
5755 | if ((unsigned)i < nr_cpumask_bits) | |
5756 | return i; | |
5757 | ||
5758 | i = select_idle_smt(p, sd, target); | |
5759 | if ((unsigned)i < nr_cpumask_bits) | |
5760 | return i; | |
970e1789 | 5761 | |
a50bde51 PZ |
5762 | return target; |
5763 | } | |
231678b7 | 5764 | |
8bb5b00c | 5765 | /* |
9e91d61d | 5766 | * cpu_util returns the amount of capacity of a CPU that is used by CFS |
8bb5b00c | 5767 | * tasks. The unit of the return value must be the one of capacity so we can |
9e91d61d DE |
5768 | * compare the utilization with the capacity of the CPU that is available for |
5769 | * CFS task (ie cpu_capacity). | |
231678b7 DE |
5770 | * |
5771 | * cfs_rq.avg.util_avg is the sum of running time of runnable tasks plus the | |
5772 | * recent utilization of currently non-runnable tasks on a CPU. It represents | |
5773 | * the amount of utilization of a CPU in the range [0..capacity_orig] where | |
5774 | * capacity_orig is the cpu_capacity available at the highest frequency | |
5775 | * (arch_scale_freq_capacity()). | |
5776 | * The utilization of a CPU converges towards a sum equal to or less than the | |
5777 | * current capacity (capacity_curr <= capacity_orig) of the CPU because it is | |
5778 | * the running time on this CPU scaled by capacity_curr. | |
5779 | * | |
5780 | * Nevertheless, cfs_rq.avg.util_avg can be higher than capacity_curr or even | |
5781 | * higher than capacity_orig because of unfortunate rounding in | |
5782 | * cfs.avg.util_avg or just after migrating tasks and new task wakeups until | |
5783 | * the average stabilizes with the new running time. We need to check that the | |
5784 | * utilization stays within the range of [0..capacity_orig] and cap it if | |
5785 | * necessary. Without utilization capping, a group could be seen as overloaded | |
5786 | * (CPU0 utilization at 121% + CPU1 utilization at 80%) whereas CPU1 has 20% of | |
5787 | * available capacity. We allow utilization to overshoot capacity_curr (but not | |
5788 | * capacity_orig) as it useful for predicting the capacity required after task | |
5789 | * migrations (scheduler-driven DVFS). | |
8bb5b00c | 5790 | */ |
9e91d61d | 5791 | static int cpu_util(int cpu) |
8bb5b00c | 5792 | { |
9e91d61d | 5793 | unsigned long util = cpu_rq(cpu)->cfs.avg.util_avg; |
8bb5b00c VG |
5794 | unsigned long capacity = capacity_orig_of(cpu); |
5795 | ||
231678b7 | 5796 | return (util >= capacity) ? capacity : util; |
8bb5b00c | 5797 | } |
a50bde51 | 5798 | |
3273163c MR |
5799 | static inline int task_util(struct task_struct *p) |
5800 | { | |
5801 | return p->se.avg.util_avg; | |
5802 | } | |
5803 | ||
104cb16d MR |
5804 | /* |
5805 | * cpu_util_wake: Compute cpu utilization with any contributions from | |
5806 | * the waking task p removed. | |
5807 | */ | |
5808 | static int cpu_util_wake(int cpu, struct task_struct *p) | |
5809 | { | |
5810 | unsigned long util, capacity; | |
5811 | ||
5812 | /* Task has no contribution or is new */ | |
5813 | if (cpu != task_cpu(p) || !p->se.avg.last_update_time) | |
5814 | return cpu_util(cpu); | |
5815 | ||
5816 | capacity = capacity_orig_of(cpu); | |
5817 | util = max_t(long, cpu_rq(cpu)->cfs.avg.util_avg - task_util(p), 0); | |
5818 | ||
5819 | return (util >= capacity) ? capacity : util; | |
5820 | } | |
5821 | ||
3273163c MR |
5822 | /* |
5823 | * Disable WAKE_AFFINE in the case where task @p doesn't fit in the | |
5824 | * capacity of either the waking CPU @cpu or the previous CPU @prev_cpu. | |
5825 | * | |
5826 | * In that case WAKE_AFFINE doesn't make sense and we'll let | |
5827 | * BALANCE_WAKE sort things out. | |
5828 | */ | |
5829 | static int wake_cap(struct task_struct *p, int cpu, int prev_cpu) | |
5830 | { | |
5831 | long min_cap, max_cap; | |
5832 | ||
5833 | min_cap = min(capacity_orig_of(prev_cpu), capacity_orig_of(cpu)); | |
5834 | max_cap = cpu_rq(cpu)->rd->max_cpu_capacity; | |
5835 | ||
5836 | /* Minimum capacity is close to max, no need to abort wake_affine */ | |
5837 | if (max_cap - min_cap < max_cap >> 3) | |
5838 | return 0; | |
5839 | ||
104cb16d MR |
5840 | /* Bring task utilization in sync with prev_cpu */ |
5841 | sync_entity_load_avg(&p->se); | |
5842 | ||
3273163c MR |
5843 | return min_cap * 1024 < task_util(p) * capacity_margin; |
5844 | } | |
5845 | ||
aaee1203 | 5846 | /* |
de91b9cb MR |
5847 | * select_task_rq_fair: Select target runqueue for the waking task in domains |
5848 | * that have the 'sd_flag' flag set. In practice, this is SD_BALANCE_WAKE, | |
5849 | * SD_BALANCE_FORK, or SD_BALANCE_EXEC. | |
aaee1203 | 5850 | * |
de91b9cb MR |
5851 | * Balances load by selecting the idlest cpu in the idlest group, or under |
5852 | * certain conditions an idle sibling cpu if the domain has SD_WAKE_AFFINE set. | |
aaee1203 | 5853 | * |
de91b9cb | 5854 | * Returns the target cpu number. |
aaee1203 PZ |
5855 | * |
5856 | * preempt must be disabled. | |
5857 | */ | |
0017d735 | 5858 | static int |
ac66f547 | 5859 | select_task_rq_fair(struct task_struct *p, int prev_cpu, int sd_flag, int wake_flags) |
aaee1203 | 5860 | { |
29cd8bae | 5861 | struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL; |
c88d5910 | 5862 | int cpu = smp_processor_id(); |
63b0e9ed | 5863 | int new_cpu = prev_cpu; |
99bd5e2f | 5864 | int want_affine = 0; |
5158f4e4 | 5865 | int sync = wake_flags & WF_SYNC; |
c88d5910 | 5866 | |
c58d25f3 PZ |
5867 | if (sd_flag & SD_BALANCE_WAKE) { |
5868 | record_wakee(p); | |
3273163c MR |
5869 | want_affine = !wake_wide(p) && !wake_cap(p, cpu, prev_cpu) |
5870 | && cpumask_test_cpu(cpu, tsk_cpus_allowed(p)); | |
c58d25f3 | 5871 | } |
aaee1203 | 5872 | |
dce840a0 | 5873 | rcu_read_lock(); |
aaee1203 | 5874 | for_each_domain(cpu, tmp) { |
e4f42888 | 5875 | if (!(tmp->flags & SD_LOAD_BALANCE)) |
63b0e9ed | 5876 | break; |
e4f42888 | 5877 | |
fe3bcfe1 | 5878 | /* |
99bd5e2f SS |
5879 | * If both cpu and prev_cpu are part of this domain, |
5880 | * cpu is a valid SD_WAKE_AFFINE target. | |
fe3bcfe1 | 5881 | */ |
99bd5e2f SS |
5882 | if (want_affine && (tmp->flags & SD_WAKE_AFFINE) && |
5883 | cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) { | |
5884 | affine_sd = tmp; | |
29cd8bae | 5885 | break; |
f03542a7 | 5886 | } |
29cd8bae | 5887 | |
f03542a7 | 5888 | if (tmp->flags & sd_flag) |
29cd8bae | 5889 | sd = tmp; |
63b0e9ed MG |
5890 | else if (!want_affine) |
5891 | break; | |
29cd8bae PZ |
5892 | } |
5893 | ||
63b0e9ed MG |
5894 | if (affine_sd) { |
5895 | sd = NULL; /* Prefer wake_affine over balance flags */ | |
772bd008 | 5896 | if (cpu != prev_cpu && wake_affine(affine_sd, p, prev_cpu, sync)) |
63b0e9ed | 5897 | new_cpu = cpu; |
8b911acd | 5898 | } |
e7693a36 | 5899 | |
63b0e9ed MG |
5900 | if (!sd) { |
5901 | if (sd_flag & SD_BALANCE_WAKE) /* XXX always ? */ | |
772bd008 | 5902 | new_cpu = select_idle_sibling(p, prev_cpu, new_cpu); |
63b0e9ed MG |
5903 | |
5904 | } else while (sd) { | |
aaee1203 | 5905 | struct sched_group *group; |
c88d5910 | 5906 | int weight; |
098fb9db | 5907 | |
0763a660 | 5908 | if (!(sd->flags & sd_flag)) { |
aaee1203 PZ |
5909 | sd = sd->child; |
5910 | continue; | |
5911 | } | |
098fb9db | 5912 | |
c44f2a02 | 5913 | group = find_idlest_group(sd, p, cpu, sd_flag); |
aaee1203 PZ |
5914 | if (!group) { |
5915 | sd = sd->child; | |
5916 | continue; | |
5917 | } | |
4ae7d5ce | 5918 | |
d7c33c49 | 5919 | new_cpu = find_idlest_cpu(group, p, cpu); |
aaee1203 PZ |
5920 | if (new_cpu == -1 || new_cpu == cpu) { |
5921 | /* Now try balancing at a lower domain level of cpu */ | |
5922 | sd = sd->child; | |
5923 | continue; | |
e7693a36 | 5924 | } |
aaee1203 PZ |
5925 | |
5926 | /* Now try balancing at a lower domain level of new_cpu */ | |
5927 | cpu = new_cpu; | |
669c55e9 | 5928 | weight = sd->span_weight; |
aaee1203 PZ |
5929 | sd = NULL; |
5930 | for_each_domain(cpu, tmp) { | |
669c55e9 | 5931 | if (weight <= tmp->span_weight) |
aaee1203 | 5932 | break; |
0763a660 | 5933 | if (tmp->flags & sd_flag) |
aaee1203 PZ |
5934 | sd = tmp; |
5935 | } | |
5936 | /* while loop will break here if sd == NULL */ | |
e7693a36 | 5937 | } |
dce840a0 | 5938 | rcu_read_unlock(); |
e7693a36 | 5939 | |
c88d5910 | 5940 | return new_cpu; |
e7693a36 | 5941 | } |
0a74bef8 PT |
5942 | |
5943 | /* | |
5944 | * Called immediately before a task is migrated to a new cpu; task_cpu(p) and | |
5945 | * cfs_rq_of(p) references at time of call are still valid and identify the | |
525628c7 | 5946 | * previous cpu. The caller guarantees p->pi_lock or task_rq(p)->lock is held. |
0a74bef8 | 5947 | */ |
5a4fd036 | 5948 | static void migrate_task_rq_fair(struct task_struct *p) |
0a74bef8 | 5949 | { |
59efa0ba PZ |
5950 | /* |
5951 | * As blocked tasks retain absolute vruntime the migration needs to | |
5952 | * deal with this by subtracting the old and adding the new | |
5953 | * min_vruntime -- the latter is done by enqueue_entity() when placing | |
5954 | * the task on the new runqueue. | |
5955 | */ | |
5956 | if (p->state == TASK_WAKING) { | |
5957 | struct sched_entity *se = &p->se; | |
5958 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
5959 | u64 min_vruntime; | |
5960 | ||
5961 | #ifndef CONFIG_64BIT | |
5962 | u64 min_vruntime_copy; | |
5963 | ||
5964 | do { | |
5965 | min_vruntime_copy = cfs_rq->min_vruntime_copy; | |
5966 | smp_rmb(); | |
5967 | min_vruntime = cfs_rq->min_vruntime; | |
5968 | } while (min_vruntime != min_vruntime_copy); | |
5969 | #else | |
5970 | min_vruntime = cfs_rq->min_vruntime; | |
5971 | #endif | |
5972 | ||
5973 | se->vruntime -= min_vruntime; | |
5974 | } | |
5975 | ||
aff3e498 | 5976 | /* |
9d89c257 YD |
5977 | * We are supposed to update the task to "current" time, then its up to date |
5978 | * and ready to go to new CPU/cfs_rq. But we have difficulty in getting | |
5979 | * what current time is, so simply throw away the out-of-date time. This | |
5980 | * will result in the wakee task is less decayed, but giving the wakee more | |
5981 | * load sounds not bad. | |
aff3e498 | 5982 | */ |
9d89c257 YD |
5983 | remove_entity_load_avg(&p->se); |
5984 | ||
5985 | /* Tell new CPU we are migrated */ | |
5986 | p->se.avg.last_update_time = 0; | |
3944a927 BS |
5987 | |
5988 | /* We have migrated, no longer consider this task hot */ | |
9d89c257 | 5989 | p->se.exec_start = 0; |
0a74bef8 | 5990 | } |
12695578 YD |
5991 | |
5992 | static void task_dead_fair(struct task_struct *p) | |
5993 | { | |
5994 | remove_entity_load_avg(&p->se); | |
5995 | } | |
e7693a36 GH |
5996 | #endif /* CONFIG_SMP */ |
5997 | ||
e52fb7c0 PZ |
5998 | static unsigned long |
5999 | wakeup_gran(struct sched_entity *curr, struct sched_entity *se) | |
0bbd3336 PZ |
6000 | { |
6001 | unsigned long gran = sysctl_sched_wakeup_granularity; | |
6002 | ||
6003 | /* | |
e52fb7c0 PZ |
6004 | * Since its curr running now, convert the gran from real-time |
6005 | * to virtual-time in his units. | |
13814d42 MG |
6006 | * |
6007 | * By using 'se' instead of 'curr' we penalize light tasks, so | |
6008 | * they get preempted easier. That is, if 'se' < 'curr' then | |
6009 | * the resulting gran will be larger, therefore penalizing the | |
6010 | * lighter, if otoh 'se' > 'curr' then the resulting gran will | |
6011 | * be smaller, again penalizing the lighter task. | |
6012 | * | |
6013 | * This is especially important for buddies when the leftmost | |
6014 | * task is higher priority than the buddy. | |
0bbd3336 | 6015 | */ |
f4ad9bd2 | 6016 | return calc_delta_fair(gran, se); |
0bbd3336 PZ |
6017 | } |
6018 | ||
464b7527 PZ |
6019 | /* |
6020 | * Should 'se' preempt 'curr'. | |
6021 | * | |
6022 | * |s1 | |
6023 | * |s2 | |
6024 | * |s3 | |
6025 | * g | |
6026 | * |<--->|c | |
6027 | * | |
6028 | * w(c, s1) = -1 | |
6029 | * w(c, s2) = 0 | |
6030 | * w(c, s3) = 1 | |
6031 | * | |
6032 | */ | |
6033 | static int | |
6034 | wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se) | |
6035 | { | |
6036 | s64 gran, vdiff = curr->vruntime - se->vruntime; | |
6037 | ||
6038 | if (vdiff <= 0) | |
6039 | return -1; | |
6040 | ||
e52fb7c0 | 6041 | gran = wakeup_gran(curr, se); |
464b7527 PZ |
6042 | if (vdiff > gran) |
6043 | return 1; | |
6044 | ||
6045 | return 0; | |
6046 | } | |
6047 | ||
02479099 PZ |
6048 | static void set_last_buddy(struct sched_entity *se) |
6049 | { | |
69c80f3e VP |
6050 | if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE)) |
6051 | return; | |
6052 | ||
6053 | for_each_sched_entity(se) | |
6054 | cfs_rq_of(se)->last = se; | |
02479099 PZ |
6055 | } |
6056 | ||
6057 | static void set_next_buddy(struct sched_entity *se) | |
6058 | { | |
69c80f3e VP |
6059 | if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE)) |
6060 | return; | |
6061 | ||
6062 | for_each_sched_entity(se) | |
6063 | cfs_rq_of(se)->next = se; | |
02479099 PZ |
6064 | } |
6065 | ||
ac53db59 RR |
6066 | static void set_skip_buddy(struct sched_entity *se) |
6067 | { | |
69c80f3e VP |
6068 | for_each_sched_entity(se) |
6069 | cfs_rq_of(se)->skip = se; | |
ac53db59 RR |
6070 | } |
6071 | ||
bf0f6f24 IM |
6072 | /* |
6073 | * Preempt the current task with a newly woken task if needed: | |
6074 | */ | |
5a9b86f6 | 6075 | static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags) |
bf0f6f24 IM |
6076 | { |
6077 | struct task_struct *curr = rq->curr; | |
8651a86c | 6078 | struct sched_entity *se = &curr->se, *pse = &p->se; |
03e89e45 | 6079 | struct cfs_rq *cfs_rq = task_cfs_rq(curr); |
f685ceac | 6080 | int scale = cfs_rq->nr_running >= sched_nr_latency; |
2f36825b | 6081 | int next_buddy_marked = 0; |
bf0f6f24 | 6082 | |
4ae7d5ce IM |
6083 | if (unlikely(se == pse)) |
6084 | return; | |
6085 | ||
5238cdd3 | 6086 | /* |
163122b7 | 6087 | * This is possible from callers such as attach_tasks(), in which we |
5238cdd3 PT |
6088 | * unconditionally check_prempt_curr() after an enqueue (which may have |
6089 | * lead to a throttle). This both saves work and prevents false | |
6090 | * next-buddy nomination below. | |
6091 | */ | |
6092 | if (unlikely(throttled_hierarchy(cfs_rq_of(pse)))) | |
6093 | return; | |
6094 | ||
2f36825b | 6095 | if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) { |
3cb63d52 | 6096 | set_next_buddy(pse); |
2f36825b VP |
6097 | next_buddy_marked = 1; |
6098 | } | |
57fdc26d | 6099 | |
aec0a514 BR |
6100 | /* |
6101 | * We can come here with TIF_NEED_RESCHED already set from new task | |
6102 | * wake up path. | |
5238cdd3 PT |
6103 | * |
6104 | * Note: this also catches the edge-case of curr being in a throttled | |
6105 | * group (e.g. via set_curr_task), since update_curr() (in the | |
6106 | * enqueue of curr) will have resulted in resched being set. This | |
6107 | * prevents us from potentially nominating it as a false LAST_BUDDY | |
6108 | * below. | |
aec0a514 BR |
6109 | */ |
6110 | if (test_tsk_need_resched(curr)) | |
6111 | return; | |
6112 | ||
a2f5c9ab DH |
6113 | /* Idle tasks are by definition preempted by non-idle tasks. */ |
6114 | if (unlikely(curr->policy == SCHED_IDLE) && | |
6115 | likely(p->policy != SCHED_IDLE)) | |
6116 | goto preempt; | |
6117 | ||
91c234b4 | 6118 | /* |
a2f5c9ab DH |
6119 | * Batch and idle tasks do not preempt non-idle tasks (their preemption |
6120 | * is driven by the tick): | |
91c234b4 | 6121 | */ |
8ed92e51 | 6122 | if (unlikely(p->policy != SCHED_NORMAL) || !sched_feat(WAKEUP_PREEMPTION)) |
91c234b4 | 6123 | return; |
bf0f6f24 | 6124 | |
464b7527 | 6125 | find_matching_se(&se, &pse); |
9bbd7374 | 6126 | update_curr(cfs_rq_of(se)); |
002f128b | 6127 | BUG_ON(!pse); |
2f36825b VP |
6128 | if (wakeup_preempt_entity(se, pse) == 1) { |
6129 | /* | |
6130 | * Bias pick_next to pick the sched entity that is | |
6131 | * triggering this preemption. | |
6132 | */ | |
6133 | if (!next_buddy_marked) | |
6134 | set_next_buddy(pse); | |
3a7e73a2 | 6135 | goto preempt; |
2f36825b | 6136 | } |
464b7527 | 6137 | |
3a7e73a2 | 6138 | return; |
a65ac745 | 6139 | |
3a7e73a2 | 6140 | preempt: |
8875125e | 6141 | resched_curr(rq); |
3a7e73a2 PZ |
6142 | /* |
6143 | * Only set the backward buddy when the current task is still | |
6144 | * on the rq. This can happen when a wakeup gets interleaved | |
6145 | * with schedule on the ->pre_schedule() or idle_balance() | |
6146 | * point, either of which can * drop the rq lock. | |
6147 | * | |
6148 | * Also, during early boot the idle thread is in the fair class, | |
6149 | * for obvious reasons its a bad idea to schedule back to it. | |
6150 | */ | |
6151 | if (unlikely(!se->on_rq || curr == rq->idle)) | |
6152 | return; | |
6153 | ||
6154 | if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se)) | |
6155 | set_last_buddy(se); | |
bf0f6f24 IM |
6156 | } |
6157 | ||
606dba2e | 6158 | static struct task_struct * |
e7904a28 | 6159 | pick_next_task_fair(struct rq *rq, struct task_struct *prev, struct pin_cookie cookie) |
bf0f6f24 IM |
6160 | { |
6161 | struct cfs_rq *cfs_rq = &rq->cfs; | |
6162 | struct sched_entity *se; | |
678d5718 | 6163 | struct task_struct *p; |
37e117c0 | 6164 | int new_tasks; |
678d5718 | 6165 | |
6e83125c | 6166 | again: |
678d5718 PZ |
6167 | #ifdef CONFIG_FAIR_GROUP_SCHED |
6168 | if (!cfs_rq->nr_running) | |
38033c37 | 6169 | goto idle; |
678d5718 | 6170 | |
3f1d2a31 | 6171 | if (prev->sched_class != &fair_sched_class) |
678d5718 PZ |
6172 | goto simple; |
6173 | ||
6174 | /* | |
6175 | * Because of the set_next_buddy() in dequeue_task_fair() it is rather | |
6176 | * likely that a next task is from the same cgroup as the current. | |
6177 | * | |
6178 | * Therefore attempt to avoid putting and setting the entire cgroup | |
6179 | * hierarchy, only change the part that actually changes. | |
6180 | */ | |
6181 | ||
6182 | do { | |
6183 | struct sched_entity *curr = cfs_rq->curr; | |
6184 | ||
6185 | /* | |
6186 | * Since we got here without doing put_prev_entity() we also | |
6187 | * have to consider cfs_rq->curr. If it is still a runnable | |
6188 | * entity, update_curr() will update its vruntime, otherwise | |
6189 | * forget we've ever seen it. | |
6190 | */ | |
54d27365 BS |
6191 | if (curr) { |
6192 | if (curr->on_rq) | |
6193 | update_curr(cfs_rq); | |
6194 | else | |
6195 | curr = NULL; | |
678d5718 | 6196 | |
54d27365 BS |
6197 | /* |
6198 | * This call to check_cfs_rq_runtime() will do the | |
6199 | * throttle and dequeue its entity in the parent(s). | |
6200 | * Therefore the 'simple' nr_running test will indeed | |
6201 | * be correct. | |
6202 | */ | |
6203 | if (unlikely(check_cfs_rq_runtime(cfs_rq))) | |
6204 | goto simple; | |
6205 | } | |
678d5718 PZ |
6206 | |
6207 | se = pick_next_entity(cfs_rq, curr); | |
6208 | cfs_rq = group_cfs_rq(se); | |
6209 | } while (cfs_rq); | |
6210 | ||
6211 | p = task_of(se); | |
6212 | ||
6213 | /* | |
6214 | * Since we haven't yet done put_prev_entity and if the selected task | |
6215 | * is a different task than we started out with, try and touch the | |
6216 | * least amount of cfs_rqs. | |
6217 | */ | |
6218 | if (prev != p) { | |
6219 | struct sched_entity *pse = &prev->se; | |
6220 | ||
6221 | while (!(cfs_rq = is_same_group(se, pse))) { | |
6222 | int se_depth = se->depth; | |
6223 | int pse_depth = pse->depth; | |
6224 | ||
6225 | if (se_depth <= pse_depth) { | |
6226 | put_prev_entity(cfs_rq_of(pse), pse); | |
6227 | pse = parent_entity(pse); | |
6228 | } | |
6229 | if (se_depth >= pse_depth) { | |
6230 | set_next_entity(cfs_rq_of(se), se); | |
6231 | se = parent_entity(se); | |
6232 | } | |
6233 | } | |
6234 | ||
6235 | put_prev_entity(cfs_rq, pse); | |
6236 | set_next_entity(cfs_rq, se); | |
6237 | } | |
6238 | ||
6239 | if (hrtick_enabled(rq)) | |
6240 | hrtick_start_fair(rq, p); | |
6241 | ||
6242 | return p; | |
6243 | simple: | |
6244 | cfs_rq = &rq->cfs; | |
6245 | #endif | |
bf0f6f24 | 6246 | |
36ace27e | 6247 | if (!cfs_rq->nr_running) |
38033c37 | 6248 | goto idle; |
bf0f6f24 | 6249 | |
3f1d2a31 | 6250 | put_prev_task(rq, prev); |
606dba2e | 6251 | |
bf0f6f24 | 6252 | do { |
678d5718 | 6253 | se = pick_next_entity(cfs_rq, NULL); |
f4b6755f | 6254 | set_next_entity(cfs_rq, se); |
bf0f6f24 IM |
6255 | cfs_rq = group_cfs_rq(se); |
6256 | } while (cfs_rq); | |
6257 | ||
8f4d37ec | 6258 | p = task_of(se); |
678d5718 | 6259 | |
b39e66ea MG |
6260 | if (hrtick_enabled(rq)) |
6261 | hrtick_start_fair(rq, p); | |
8f4d37ec PZ |
6262 | |
6263 | return p; | |
38033c37 PZ |
6264 | |
6265 | idle: | |
cbce1a68 PZ |
6266 | /* |
6267 | * This is OK, because current is on_cpu, which avoids it being picked | |
6268 | * for load-balance and preemption/IRQs are still disabled avoiding | |
6269 | * further scheduler activity on it and we're being very careful to | |
6270 | * re-start the picking loop. | |
6271 | */ | |
e7904a28 | 6272 | lockdep_unpin_lock(&rq->lock, cookie); |
e4aa358b | 6273 | new_tasks = idle_balance(rq); |
e7904a28 | 6274 | lockdep_repin_lock(&rq->lock, cookie); |
37e117c0 PZ |
6275 | /* |
6276 | * Because idle_balance() releases (and re-acquires) rq->lock, it is | |
6277 | * possible for any higher priority task to appear. In that case we | |
6278 | * must re-start the pick_next_entity() loop. | |
6279 | */ | |
e4aa358b | 6280 | if (new_tasks < 0) |
37e117c0 PZ |
6281 | return RETRY_TASK; |
6282 | ||
e4aa358b | 6283 | if (new_tasks > 0) |
38033c37 | 6284 | goto again; |
38033c37 PZ |
6285 | |
6286 | return NULL; | |
bf0f6f24 IM |
6287 | } |
6288 | ||
6289 | /* | |
6290 | * Account for a descheduled task: | |
6291 | */ | |
31ee529c | 6292 | static void put_prev_task_fair(struct rq *rq, struct task_struct *prev) |
bf0f6f24 IM |
6293 | { |
6294 | struct sched_entity *se = &prev->se; | |
6295 | struct cfs_rq *cfs_rq; | |
6296 | ||
6297 | for_each_sched_entity(se) { | |
6298 | cfs_rq = cfs_rq_of(se); | |
ab6cde26 | 6299 | put_prev_entity(cfs_rq, se); |
bf0f6f24 IM |
6300 | } |
6301 | } | |
6302 | ||
ac53db59 RR |
6303 | /* |
6304 | * sched_yield() is very simple | |
6305 | * | |
6306 | * The magic of dealing with the ->skip buddy is in pick_next_entity. | |
6307 | */ | |
6308 | static void yield_task_fair(struct rq *rq) | |
6309 | { | |
6310 | struct task_struct *curr = rq->curr; | |
6311 | struct cfs_rq *cfs_rq = task_cfs_rq(curr); | |
6312 | struct sched_entity *se = &curr->se; | |
6313 | ||
6314 | /* | |
6315 | * Are we the only task in the tree? | |
6316 | */ | |
6317 | if (unlikely(rq->nr_running == 1)) | |
6318 | return; | |
6319 | ||
6320 | clear_buddies(cfs_rq, se); | |
6321 | ||
6322 | if (curr->policy != SCHED_BATCH) { | |
6323 | update_rq_clock(rq); | |
6324 | /* | |
6325 | * Update run-time statistics of the 'current'. | |
6326 | */ | |
6327 | update_curr(cfs_rq); | |
916671c0 MG |
6328 | /* |
6329 | * Tell update_rq_clock() that we've just updated, | |
6330 | * so we don't do microscopic update in schedule() | |
6331 | * and double the fastpath cost. | |
6332 | */ | |
9edfbfed | 6333 | rq_clock_skip_update(rq, true); |
ac53db59 RR |
6334 | } |
6335 | ||
6336 | set_skip_buddy(se); | |
6337 | } | |
6338 | ||
d95f4122 MG |
6339 | static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt) |
6340 | { | |
6341 | struct sched_entity *se = &p->se; | |
6342 | ||
5238cdd3 PT |
6343 | /* throttled hierarchies are not runnable */ |
6344 | if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se))) | |
d95f4122 MG |
6345 | return false; |
6346 | ||
6347 | /* Tell the scheduler that we'd really like pse to run next. */ | |
6348 | set_next_buddy(se); | |
6349 | ||
d95f4122 MG |
6350 | yield_task_fair(rq); |
6351 | ||
6352 | return true; | |
6353 | } | |
6354 | ||
681f3e68 | 6355 | #ifdef CONFIG_SMP |
bf0f6f24 | 6356 | /************************************************** |
e9c84cb8 PZ |
6357 | * Fair scheduling class load-balancing methods. |
6358 | * | |
6359 | * BASICS | |
6360 | * | |
6361 | * The purpose of load-balancing is to achieve the same basic fairness the | |
6362 | * per-cpu scheduler provides, namely provide a proportional amount of compute | |
6363 | * time to each task. This is expressed in the following equation: | |
6364 | * | |
6365 | * W_i,n/P_i == W_j,n/P_j for all i,j (1) | |
6366 | * | |
6367 | * Where W_i,n is the n-th weight average for cpu i. The instantaneous weight | |
6368 | * W_i,0 is defined as: | |
6369 | * | |
6370 | * W_i,0 = \Sum_j w_i,j (2) | |
6371 | * | |
6372 | * Where w_i,j is the weight of the j-th runnable task on cpu i. This weight | |
1c3de5e1 | 6373 | * is derived from the nice value as per sched_prio_to_weight[]. |
e9c84cb8 PZ |
6374 | * |
6375 | * The weight average is an exponential decay average of the instantaneous | |
6376 | * weight: | |
6377 | * | |
6378 | * W'_i,n = (2^n - 1) / 2^n * W_i,n + 1 / 2^n * W_i,0 (3) | |
6379 | * | |
ced549fa | 6380 | * C_i is the compute capacity of cpu i, typically it is the |
e9c84cb8 PZ |
6381 | * fraction of 'recent' time available for SCHED_OTHER task execution. But it |
6382 | * can also include other factors [XXX]. | |
6383 | * | |
6384 | * To achieve this balance we define a measure of imbalance which follows | |
6385 | * directly from (1): | |
6386 | * | |
ced549fa | 6387 | * imb_i,j = max{ avg(W/C), W_i/C_i } - min{ avg(W/C), W_j/C_j } (4) |
e9c84cb8 PZ |
6388 | * |
6389 | * We them move tasks around to minimize the imbalance. In the continuous | |
6390 | * function space it is obvious this converges, in the discrete case we get | |
6391 | * a few fun cases generally called infeasible weight scenarios. | |
6392 | * | |
6393 | * [XXX expand on: | |
6394 | * - infeasible weights; | |
6395 | * - local vs global optima in the discrete case. ] | |
6396 | * | |
6397 | * | |
6398 | * SCHED DOMAINS | |
6399 | * | |
6400 | * In order to solve the imbalance equation (4), and avoid the obvious O(n^2) | |
6401 | * for all i,j solution, we create a tree of cpus that follows the hardware | |
6402 | * topology where each level pairs two lower groups (or better). This results | |
6403 | * in O(log n) layers. Furthermore we reduce the number of cpus going up the | |
6404 | * tree to only the first of the previous level and we decrease the frequency | |
6405 | * of load-balance at each level inv. proportional to the number of cpus in | |
6406 | * the groups. | |
6407 | * | |
6408 | * This yields: | |
6409 | * | |
6410 | * log_2 n 1 n | |
6411 | * \Sum { --- * --- * 2^i } = O(n) (5) | |
6412 | * i = 0 2^i 2^i | |
6413 | * `- size of each group | |
6414 | * | | `- number of cpus doing load-balance | |
6415 | * | `- freq | |
6416 | * `- sum over all levels | |
6417 | * | |
6418 | * Coupled with a limit on how many tasks we can migrate every balance pass, | |
6419 | * this makes (5) the runtime complexity of the balancer. | |
6420 | * | |
6421 | * An important property here is that each CPU is still (indirectly) connected | |
6422 | * to every other cpu in at most O(log n) steps: | |
6423 | * | |
6424 | * The adjacency matrix of the resulting graph is given by: | |
6425 | * | |
97a7142f | 6426 | * log_2 n |
e9c84cb8 PZ |
6427 | * A_i,j = \Union (i % 2^k == 0) && i / 2^(k+1) == j / 2^(k+1) (6) |
6428 | * k = 0 | |
6429 | * | |
6430 | * And you'll find that: | |
6431 | * | |
6432 | * A^(log_2 n)_i,j != 0 for all i,j (7) | |
6433 | * | |
6434 | * Showing there's indeed a path between every cpu in at most O(log n) steps. | |
6435 | * The task movement gives a factor of O(m), giving a convergence complexity | |
6436 | * of: | |
6437 | * | |
6438 | * O(nm log n), n := nr_cpus, m := nr_tasks (8) | |
6439 | * | |
6440 | * | |
6441 | * WORK CONSERVING | |
6442 | * | |
6443 | * In order to avoid CPUs going idle while there's still work to do, new idle | |
6444 | * balancing is more aggressive and has the newly idle cpu iterate up the domain | |
6445 | * tree itself instead of relying on other CPUs to bring it work. | |
6446 | * | |
6447 | * This adds some complexity to both (5) and (8) but it reduces the total idle | |
6448 | * time. | |
6449 | * | |
6450 | * [XXX more?] | |
6451 | * | |
6452 | * | |
6453 | * CGROUPS | |
6454 | * | |
6455 | * Cgroups make a horror show out of (2), instead of a simple sum we get: | |
6456 | * | |
6457 | * s_k,i | |
6458 | * W_i,0 = \Sum_j \Prod_k w_k * ----- (9) | |
6459 | * S_k | |
6460 | * | |
6461 | * Where | |
6462 | * | |
6463 | * s_k,i = \Sum_j w_i,j,k and S_k = \Sum_i s_k,i (10) | |
6464 | * | |
6465 | * w_i,j,k is the weight of the j-th runnable task in the k-th cgroup on cpu i. | |
6466 | * | |
6467 | * The big problem is S_k, its a global sum needed to compute a local (W_i) | |
6468 | * property. | |
6469 | * | |
6470 | * [XXX write more on how we solve this.. _after_ merging pjt's patches that | |
6471 | * rewrite all of this once again.] | |
97a7142f | 6472 | */ |
bf0f6f24 | 6473 | |
ed387b78 HS |
6474 | static unsigned long __read_mostly max_load_balance_interval = HZ/10; |
6475 | ||
0ec8aa00 PZ |
6476 | enum fbq_type { regular, remote, all }; |
6477 | ||
ddcdf6e7 | 6478 | #define LBF_ALL_PINNED 0x01 |
367456c7 | 6479 | #define LBF_NEED_BREAK 0x02 |
6263322c PZ |
6480 | #define LBF_DST_PINNED 0x04 |
6481 | #define LBF_SOME_PINNED 0x08 | |
ddcdf6e7 PZ |
6482 | |
6483 | struct lb_env { | |
6484 | struct sched_domain *sd; | |
6485 | ||
ddcdf6e7 | 6486 | struct rq *src_rq; |
85c1e7da | 6487 | int src_cpu; |
ddcdf6e7 PZ |
6488 | |
6489 | int dst_cpu; | |
6490 | struct rq *dst_rq; | |
6491 | ||
88b8dac0 SV |
6492 | struct cpumask *dst_grpmask; |
6493 | int new_dst_cpu; | |
ddcdf6e7 | 6494 | enum cpu_idle_type idle; |
bd939f45 | 6495 | long imbalance; |
b9403130 MW |
6496 | /* The set of CPUs under consideration for load-balancing */ |
6497 | struct cpumask *cpus; | |
6498 | ||
ddcdf6e7 | 6499 | unsigned int flags; |
367456c7 PZ |
6500 | |
6501 | unsigned int loop; | |
6502 | unsigned int loop_break; | |
6503 | unsigned int loop_max; | |
0ec8aa00 PZ |
6504 | |
6505 | enum fbq_type fbq_type; | |
163122b7 | 6506 | struct list_head tasks; |
ddcdf6e7 PZ |
6507 | }; |
6508 | ||
029632fb PZ |
6509 | /* |
6510 | * Is this task likely cache-hot: | |
6511 | */ | |
5d5e2b1b | 6512 | static int task_hot(struct task_struct *p, struct lb_env *env) |
029632fb PZ |
6513 | { |
6514 | s64 delta; | |
6515 | ||
e5673f28 KT |
6516 | lockdep_assert_held(&env->src_rq->lock); |
6517 | ||
029632fb PZ |
6518 | if (p->sched_class != &fair_sched_class) |
6519 | return 0; | |
6520 | ||
6521 | if (unlikely(p->policy == SCHED_IDLE)) | |
6522 | return 0; | |
6523 | ||
6524 | /* | |
6525 | * Buddy candidates are cache hot: | |
6526 | */ | |
5d5e2b1b | 6527 | if (sched_feat(CACHE_HOT_BUDDY) && env->dst_rq->nr_running && |
029632fb PZ |
6528 | (&p->se == cfs_rq_of(&p->se)->next || |
6529 | &p->se == cfs_rq_of(&p->se)->last)) | |
6530 | return 1; | |
6531 | ||
6532 | if (sysctl_sched_migration_cost == -1) | |
6533 | return 1; | |
6534 | if (sysctl_sched_migration_cost == 0) | |
6535 | return 0; | |
6536 | ||
5d5e2b1b | 6537 | delta = rq_clock_task(env->src_rq) - p->se.exec_start; |
029632fb PZ |
6538 | |
6539 | return delta < (s64)sysctl_sched_migration_cost; | |
6540 | } | |
6541 | ||
3a7053b3 | 6542 | #ifdef CONFIG_NUMA_BALANCING |
c1ceac62 | 6543 | /* |
2a1ed24c SD |
6544 | * Returns 1, if task migration degrades locality |
6545 | * Returns 0, if task migration improves locality i.e migration preferred. | |
6546 | * Returns -1, if task migration is not affected by locality. | |
c1ceac62 | 6547 | */ |
2a1ed24c | 6548 | static int migrate_degrades_locality(struct task_struct *p, struct lb_env *env) |
3a7053b3 | 6549 | { |
b1ad065e | 6550 | struct numa_group *numa_group = rcu_dereference(p->numa_group); |
c1ceac62 | 6551 | unsigned long src_faults, dst_faults; |
3a7053b3 MG |
6552 | int src_nid, dst_nid; |
6553 | ||
2a595721 | 6554 | if (!static_branch_likely(&sched_numa_balancing)) |
2a1ed24c SD |
6555 | return -1; |
6556 | ||
c3b9bc5b | 6557 | if (!p->numa_faults || !(env->sd->flags & SD_NUMA)) |
2a1ed24c | 6558 | return -1; |
7a0f3083 MG |
6559 | |
6560 | src_nid = cpu_to_node(env->src_cpu); | |
6561 | dst_nid = cpu_to_node(env->dst_cpu); | |
6562 | ||
83e1d2cd | 6563 | if (src_nid == dst_nid) |
2a1ed24c | 6564 | return -1; |
7a0f3083 | 6565 | |
2a1ed24c SD |
6566 | /* Migrating away from the preferred node is always bad. */ |
6567 | if (src_nid == p->numa_preferred_nid) { | |
6568 | if (env->src_rq->nr_running > env->src_rq->nr_preferred_running) | |
6569 | return 1; | |
6570 | else | |
6571 | return -1; | |
6572 | } | |
b1ad065e | 6573 | |
c1ceac62 RR |
6574 | /* Encourage migration to the preferred node. */ |
6575 | if (dst_nid == p->numa_preferred_nid) | |
2a1ed24c | 6576 | return 0; |
b1ad065e | 6577 | |
c1ceac62 RR |
6578 | if (numa_group) { |
6579 | src_faults = group_faults(p, src_nid); | |
6580 | dst_faults = group_faults(p, dst_nid); | |
6581 | } else { | |
6582 | src_faults = task_faults(p, src_nid); | |
6583 | dst_faults = task_faults(p, dst_nid); | |
b1ad065e RR |
6584 | } |
6585 | ||
c1ceac62 | 6586 | return dst_faults < src_faults; |
7a0f3083 MG |
6587 | } |
6588 | ||
3a7053b3 | 6589 | #else |
2a1ed24c | 6590 | static inline int migrate_degrades_locality(struct task_struct *p, |
3a7053b3 MG |
6591 | struct lb_env *env) |
6592 | { | |
2a1ed24c | 6593 | return -1; |
7a0f3083 | 6594 | } |
3a7053b3 MG |
6595 | #endif |
6596 | ||
1e3c88bd PZ |
6597 | /* |
6598 | * can_migrate_task - may task p from runqueue rq be migrated to this_cpu? | |
6599 | */ | |
6600 | static | |
8e45cb54 | 6601 | int can_migrate_task(struct task_struct *p, struct lb_env *env) |
1e3c88bd | 6602 | { |
2a1ed24c | 6603 | int tsk_cache_hot; |
e5673f28 KT |
6604 | |
6605 | lockdep_assert_held(&env->src_rq->lock); | |
6606 | ||
1e3c88bd PZ |
6607 | /* |
6608 | * We do not migrate tasks that are: | |
d3198084 | 6609 | * 1) throttled_lb_pair, or |
1e3c88bd | 6610 | * 2) cannot be migrated to this CPU due to cpus_allowed, or |
d3198084 JK |
6611 | * 3) running (obviously), or |
6612 | * 4) are cache-hot on their current CPU. | |
1e3c88bd | 6613 | */ |
d3198084 JK |
6614 | if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu)) |
6615 | return 0; | |
6616 | ||
ddcdf6e7 | 6617 | if (!cpumask_test_cpu(env->dst_cpu, tsk_cpus_allowed(p))) { |
e02e60c1 | 6618 | int cpu; |
88b8dac0 | 6619 | |
ae92882e | 6620 | schedstat_inc(p->se.statistics.nr_failed_migrations_affine); |
88b8dac0 | 6621 | |
6263322c PZ |
6622 | env->flags |= LBF_SOME_PINNED; |
6623 | ||
88b8dac0 SV |
6624 | /* |
6625 | * Remember if this task can be migrated to any other cpu in | |
6626 | * our sched_group. We may want to revisit it if we couldn't | |
6627 | * meet load balance goals by pulling other tasks on src_cpu. | |
6628 | * | |
6629 | * Also avoid computing new_dst_cpu if we have already computed | |
6630 | * one in current iteration. | |
6631 | */ | |
6263322c | 6632 | if (!env->dst_grpmask || (env->flags & LBF_DST_PINNED)) |
88b8dac0 SV |
6633 | return 0; |
6634 | ||
e02e60c1 JK |
6635 | /* Prevent to re-select dst_cpu via env's cpus */ |
6636 | for_each_cpu_and(cpu, env->dst_grpmask, env->cpus) { | |
6637 | if (cpumask_test_cpu(cpu, tsk_cpus_allowed(p))) { | |
6263322c | 6638 | env->flags |= LBF_DST_PINNED; |
e02e60c1 JK |
6639 | env->new_dst_cpu = cpu; |
6640 | break; | |
6641 | } | |
88b8dac0 | 6642 | } |
e02e60c1 | 6643 | |
1e3c88bd PZ |
6644 | return 0; |
6645 | } | |
88b8dac0 SV |
6646 | |
6647 | /* Record that we found atleast one task that could run on dst_cpu */ | |
8e45cb54 | 6648 | env->flags &= ~LBF_ALL_PINNED; |
1e3c88bd | 6649 | |
ddcdf6e7 | 6650 | if (task_running(env->src_rq, p)) { |
ae92882e | 6651 | schedstat_inc(p->se.statistics.nr_failed_migrations_running); |
1e3c88bd PZ |
6652 | return 0; |
6653 | } | |
6654 | ||
6655 | /* | |
6656 | * Aggressive migration if: | |
3a7053b3 MG |
6657 | * 1) destination numa is preferred |
6658 | * 2) task is cache cold, or | |
6659 | * 3) too many balance attempts have failed. | |
1e3c88bd | 6660 | */ |
2a1ed24c SD |
6661 | tsk_cache_hot = migrate_degrades_locality(p, env); |
6662 | if (tsk_cache_hot == -1) | |
6663 | tsk_cache_hot = task_hot(p, env); | |
3a7053b3 | 6664 | |
2a1ed24c | 6665 | if (tsk_cache_hot <= 0 || |
7a96c231 | 6666 | env->sd->nr_balance_failed > env->sd->cache_nice_tries) { |
2a1ed24c | 6667 | if (tsk_cache_hot == 1) { |
ae92882e JP |
6668 | schedstat_inc(env->sd->lb_hot_gained[env->idle]); |
6669 | schedstat_inc(p->se.statistics.nr_forced_migrations); | |
3a7053b3 | 6670 | } |
1e3c88bd PZ |
6671 | return 1; |
6672 | } | |
6673 | ||
ae92882e | 6674 | schedstat_inc(p->se.statistics.nr_failed_migrations_hot); |
4e2dcb73 | 6675 | return 0; |
1e3c88bd PZ |
6676 | } |
6677 | ||
897c395f | 6678 | /* |
163122b7 KT |
6679 | * detach_task() -- detach the task for the migration specified in env |
6680 | */ | |
6681 | static void detach_task(struct task_struct *p, struct lb_env *env) | |
6682 | { | |
6683 | lockdep_assert_held(&env->src_rq->lock); | |
6684 | ||
163122b7 | 6685 | p->on_rq = TASK_ON_RQ_MIGRATING; |
3ea94de1 | 6686 | deactivate_task(env->src_rq, p, 0); |
163122b7 KT |
6687 | set_task_cpu(p, env->dst_cpu); |
6688 | } | |
6689 | ||
897c395f | 6690 | /* |
e5673f28 | 6691 | * detach_one_task() -- tries to dequeue exactly one task from env->src_rq, as |
897c395f | 6692 | * part of active balancing operations within "domain". |
897c395f | 6693 | * |
e5673f28 | 6694 | * Returns a task if successful and NULL otherwise. |
897c395f | 6695 | */ |
e5673f28 | 6696 | static struct task_struct *detach_one_task(struct lb_env *env) |
897c395f PZ |
6697 | { |
6698 | struct task_struct *p, *n; | |
897c395f | 6699 | |
e5673f28 KT |
6700 | lockdep_assert_held(&env->src_rq->lock); |
6701 | ||
367456c7 | 6702 | list_for_each_entry_safe(p, n, &env->src_rq->cfs_tasks, se.group_node) { |
367456c7 PZ |
6703 | if (!can_migrate_task(p, env)) |
6704 | continue; | |
897c395f | 6705 | |
163122b7 | 6706 | detach_task(p, env); |
e5673f28 | 6707 | |
367456c7 | 6708 | /* |
e5673f28 | 6709 | * Right now, this is only the second place where |
163122b7 | 6710 | * lb_gained[env->idle] is updated (other is detach_tasks) |
e5673f28 | 6711 | * so we can safely collect stats here rather than |
163122b7 | 6712 | * inside detach_tasks(). |
367456c7 | 6713 | */ |
ae92882e | 6714 | schedstat_inc(env->sd->lb_gained[env->idle]); |
e5673f28 | 6715 | return p; |
897c395f | 6716 | } |
e5673f28 | 6717 | return NULL; |
897c395f PZ |
6718 | } |
6719 | ||
eb95308e PZ |
6720 | static const unsigned int sched_nr_migrate_break = 32; |
6721 | ||
5d6523eb | 6722 | /* |
163122b7 KT |
6723 | * detach_tasks() -- tries to detach up to imbalance weighted load from |
6724 | * busiest_rq, as part of a balancing operation within domain "sd". | |
5d6523eb | 6725 | * |
163122b7 | 6726 | * Returns number of detached tasks if successful and 0 otherwise. |
5d6523eb | 6727 | */ |
163122b7 | 6728 | static int detach_tasks(struct lb_env *env) |
1e3c88bd | 6729 | { |
5d6523eb PZ |
6730 | struct list_head *tasks = &env->src_rq->cfs_tasks; |
6731 | struct task_struct *p; | |
367456c7 | 6732 | unsigned long load; |
163122b7 KT |
6733 | int detached = 0; |
6734 | ||
6735 | lockdep_assert_held(&env->src_rq->lock); | |
1e3c88bd | 6736 | |
bd939f45 | 6737 | if (env->imbalance <= 0) |
5d6523eb | 6738 | return 0; |
1e3c88bd | 6739 | |
5d6523eb | 6740 | while (!list_empty(tasks)) { |
985d3a4c YD |
6741 | /* |
6742 | * We don't want to steal all, otherwise we may be treated likewise, | |
6743 | * which could at worst lead to a livelock crash. | |
6744 | */ | |
6745 | if (env->idle != CPU_NOT_IDLE && env->src_rq->nr_running <= 1) | |
6746 | break; | |
6747 | ||
5d6523eb | 6748 | p = list_first_entry(tasks, struct task_struct, se.group_node); |
1e3c88bd | 6749 | |
367456c7 PZ |
6750 | env->loop++; |
6751 | /* We've more or less seen every task there is, call it quits */ | |
5d6523eb | 6752 | if (env->loop > env->loop_max) |
367456c7 | 6753 | break; |
5d6523eb PZ |
6754 | |
6755 | /* take a breather every nr_migrate tasks */ | |
367456c7 | 6756 | if (env->loop > env->loop_break) { |
eb95308e | 6757 | env->loop_break += sched_nr_migrate_break; |
8e45cb54 | 6758 | env->flags |= LBF_NEED_BREAK; |
ee00e66f | 6759 | break; |
a195f004 | 6760 | } |
1e3c88bd | 6761 | |
d3198084 | 6762 | if (!can_migrate_task(p, env)) |
367456c7 PZ |
6763 | goto next; |
6764 | ||
6765 | load = task_h_load(p); | |
5d6523eb | 6766 | |
eb95308e | 6767 | if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed) |
367456c7 PZ |
6768 | goto next; |
6769 | ||
bd939f45 | 6770 | if ((load / 2) > env->imbalance) |
367456c7 | 6771 | goto next; |
1e3c88bd | 6772 | |
163122b7 KT |
6773 | detach_task(p, env); |
6774 | list_add(&p->se.group_node, &env->tasks); | |
6775 | ||
6776 | detached++; | |
bd939f45 | 6777 | env->imbalance -= load; |
1e3c88bd PZ |
6778 | |
6779 | #ifdef CONFIG_PREEMPT | |
ee00e66f PZ |
6780 | /* |
6781 | * NEWIDLE balancing is a source of latency, so preemptible | |
163122b7 | 6782 | * kernels will stop after the first task is detached to minimize |
ee00e66f PZ |
6783 | * the critical section. |
6784 | */ | |
5d6523eb | 6785 | if (env->idle == CPU_NEWLY_IDLE) |
ee00e66f | 6786 | break; |
1e3c88bd PZ |
6787 | #endif |
6788 | ||
ee00e66f PZ |
6789 | /* |
6790 | * We only want to steal up to the prescribed amount of | |
6791 | * weighted load. | |
6792 | */ | |
bd939f45 | 6793 | if (env->imbalance <= 0) |
ee00e66f | 6794 | break; |
367456c7 PZ |
6795 | |
6796 | continue; | |
6797 | next: | |
5d6523eb | 6798 | list_move_tail(&p->se.group_node, tasks); |
1e3c88bd | 6799 | } |
5d6523eb | 6800 | |
1e3c88bd | 6801 | /* |
163122b7 KT |
6802 | * Right now, this is one of only two places we collect this stat |
6803 | * so we can safely collect detach_one_task() stats here rather | |
6804 | * than inside detach_one_task(). | |
1e3c88bd | 6805 | */ |
ae92882e | 6806 | schedstat_add(env->sd->lb_gained[env->idle], detached); |
1e3c88bd | 6807 | |
163122b7 KT |
6808 | return detached; |
6809 | } | |
6810 | ||
6811 | /* | |
6812 | * attach_task() -- attach the task detached by detach_task() to its new rq. | |
6813 | */ | |
6814 | static void attach_task(struct rq *rq, struct task_struct *p) | |
6815 | { | |
6816 | lockdep_assert_held(&rq->lock); | |
6817 | ||
6818 | BUG_ON(task_rq(p) != rq); | |
163122b7 | 6819 | activate_task(rq, p, 0); |
3ea94de1 | 6820 | p->on_rq = TASK_ON_RQ_QUEUED; |
163122b7 KT |
6821 | check_preempt_curr(rq, p, 0); |
6822 | } | |
6823 | ||
6824 | /* | |
6825 | * attach_one_task() -- attaches the task returned from detach_one_task() to | |
6826 | * its new rq. | |
6827 | */ | |
6828 | static void attach_one_task(struct rq *rq, struct task_struct *p) | |
6829 | { | |
6830 | raw_spin_lock(&rq->lock); | |
6831 | attach_task(rq, p); | |
6832 | raw_spin_unlock(&rq->lock); | |
6833 | } | |
6834 | ||
6835 | /* | |
6836 | * attach_tasks() -- attaches all tasks detached by detach_tasks() to their | |
6837 | * new rq. | |
6838 | */ | |
6839 | static void attach_tasks(struct lb_env *env) | |
6840 | { | |
6841 | struct list_head *tasks = &env->tasks; | |
6842 | struct task_struct *p; | |
6843 | ||
6844 | raw_spin_lock(&env->dst_rq->lock); | |
6845 | ||
6846 | while (!list_empty(tasks)) { | |
6847 | p = list_first_entry(tasks, struct task_struct, se.group_node); | |
6848 | list_del_init(&p->se.group_node); | |
1e3c88bd | 6849 | |
163122b7 KT |
6850 | attach_task(env->dst_rq, p); |
6851 | } | |
6852 | ||
6853 | raw_spin_unlock(&env->dst_rq->lock); | |
1e3c88bd PZ |
6854 | } |
6855 | ||
230059de | 6856 | #ifdef CONFIG_FAIR_GROUP_SCHED |
48a16753 | 6857 | static void update_blocked_averages(int cpu) |
9e3081ca | 6858 | { |
9e3081ca | 6859 | struct rq *rq = cpu_rq(cpu); |
48a16753 PT |
6860 | struct cfs_rq *cfs_rq; |
6861 | unsigned long flags; | |
9e3081ca | 6862 | |
48a16753 PT |
6863 | raw_spin_lock_irqsave(&rq->lock, flags); |
6864 | update_rq_clock(rq); | |
9d89c257 | 6865 | |
9763b67f PZ |
6866 | /* |
6867 | * Iterates the task_group tree in a bottom up fashion, see | |
6868 | * list_add_leaf_cfs_rq() for details. | |
6869 | */ | |
64660c86 | 6870 | for_each_leaf_cfs_rq(rq, cfs_rq) { |
9d89c257 YD |
6871 | /* throttled entities do not contribute to load */ |
6872 | if (throttled_hierarchy(cfs_rq)) | |
6873 | continue; | |
48a16753 | 6874 | |
a2c6c91f | 6875 | if (update_cfs_rq_load_avg(cfs_rq_clock_task(cfs_rq), cfs_rq, true)) |
9d89c257 | 6876 | update_tg_load_avg(cfs_rq, 0); |
4e516076 VG |
6877 | |
6878 | /* Propagate pending load changes to the parent */ | |
6879 | if (cfs_rq->tg->se[cpu]) | |
6880 | update_load_avg(cfs_rq->tg->se[cpu], 0); | |
9d89c257 | 6881 | } |
48a16753 | 6882 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
9e3081ca PZ |
6883 | } |
6884 | ||
9763b67f | 6885 | /* |
68520796 | 6886 | * Compute the hierarchical load factor for cfs_rq and all its ascendants. |
9763b67f PZ |
6887 | * This needs to be done in a top-down fashion because the load of a child |
6888 | * group is a fraction of its parents load. | |
6889 | */ | |
68520796 | 6890 | static void update_cfs_rq_h_load(struct cfs_rq *cfs_rq) |
9763b67f | 6891 | { |
68520796 VD |
6892 | struct rq *rq = rq_of(cfs_rq); |
6893 | struct sched_entity *se = cfs_rq->tg->se[cpu_of(rq)]; | |
a35b6466 | 6894 | unsigned long now = jiffies; |
68520796 | 6895 | unsigned long load; |
a35b6466 | 6896 | |
68520796 | 6897 | if (cfs_rq->last_h_load_update == now) |
a35b6466 PZ |
6898 | return; |
6899 | ||
68520796 VD |
6900 | cfs_rq->h_load_next = NULL; |
6901 | for_each_sched_entity(se) { | |
6902 | cfs_rq = cfs_rq_of(se); | |
6903 | cfs_rq->h_load_next = se; | |
6904 | if (cfs_rq->last_h_load_update == now) | |
6905 | break; | |
6906 | } | |
a35b6466 | 6907 | |
68520796 | 6908 | if (!se) { |
7ea241af | 6909 | cfs_rq->h_load = cfs_rq_load_avg(cfs_rq); |
68520796 VD |
6910 | cfs_rq->last_h_load_update = now; |
6911 | } | |
6912 | ||
6913 | while ((se = cfs_rq->h_load_next) != NULL) { | |
6914 | load = cfs_rq->h_load; | |
7ea241af YD |
6915 | load = div64_ul(load * se->avg.load_avg, |
6916 | cfs_rq_load_avg(cfs_rq) + 1); | |
68520796 VD |
6917 | cfs_rq = group_cfs_rq(se); |
6918 | cfs_rq->h_load = load; | |
6919 | cfs_rq->last_h_load_update = now; | |
6920 | } | |
9763b67f PZ |
6921 | } |
6922 | ||
367456c7 | 6923 | static unsigned long task_h_load(struct task_struct *p) |
230059de | 6924 | { |
367456c7 | 6925 | struct cfs_rq *cfs_rq = task_cfs_rq(p); |
230059de | 6926 | |
68520796 | 6927 | update_cfs_rq_h_load(cfs_rq); |
9d89c257 | 6928 | return div64_ul(p->se.avg.load_avg * cfs_rq->h_load, |
7ea241af | 6929 | cfs_rq_load_avg(cfs_rq) + 1); |
230059de PZ |
6930 | } |
6931 | #else | |
48a16753 | 6932 | static inline void update_blocked_averages(int cpu) |
9e3081ca | 6933 | { |
6c1d47c0 VG |
6934 | struct rq *rq = cpu_rq(cpu); |
6935 | struct cfs_rq *cfs_rq = &rq->cfs; | |
6936 | unsigned long flags; | |
6937 | ||
6938 | raw_spin_lock_irqsave(&rq->lock, flags); | |
6939 | update_rq_clock(rq); | |
a2c6c91f | 6940 | update_cfs_rq_load_avg(cfs_rq_clock_task(cfs_rq), cfs_rq, true); |
6c1d47c0 | 6941 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
9e3081ca PZ |
6942 | } |
6943 | ||
367456c7 | 6944 | static unsigned long task_h_load(struct task_struct *p) |
1e3c88bd | 6945 | { |
9d89c257 | 6946 | return p->se.avg.load_avg; |
1e3c88bd | 6947 | } |
230059de | 6948 | #endif |
1e3c88bd | 6949 | |
1e3c88bd | 6950 | /********** Helpers for find_busiest_group ************************/ |
caeb178c RR |
6951 | |
6952 | enum group_type { | |
6953 | group_other = 0, | |
6954 | group_imbalanced, | |
6955 | group_overloaded, | |
6956 | }; | |
6957 | ||
1e3c88bd PZ |
6958 | /* |
6959 | * sg_lb_stats - stats of a sched_group required for load_balancing | |
6960 | */ | |
6961 | struct sg_lb_stats { | |
6962 | unsigned long avg_load; /*Avg load across the CPUs of the group */ | |
6963 | unsigned long group_load; /* Total load over the CPUs of the group */ | |
1e3c88bd | 6964 | unsigned long sum_weighted_load; /* Weighted load of group's tasks */ |
56cf515b | 6965 | unsigned long load_per_task; |
63b2ca30 | 6966 | unsigned long group_capacity; |
9e91d61d | 6967 | unsigned long group_util; /* Total utilization of the group */ |
147c5fc2 | 6968 | unsigned int sum_nr_running; /* Nr tasks running in the group */ |
147c5fc2 PZ |
6969 | unsigned int idle_cpus; |
6970 | unsigned int group_weight; | |
caeb178c | 6971 | enum group_type group_type; |
ea67821b | 6972 | int group_no_capacity; |
0ec8aa00 PZ |
6973 | #ifdef CONFIG_NUMA_BALANCING |
6974 | unsigned int nr_numa_running; | |
6975 | unsigned int nr_preferred_running; | |
6976 | #endif | |
1e3c88bd PZ |
6977 | }; |
6978 | ||
56cf515b JK |
6979 | /* |
6980 | * sd_lb_stats - Structure to store the statistics of a sched_domain | |
6981 | * during load balancing. | |
6982 | */ | |
6983 | struct sd_lb_stats { | |
6984 | struct sched_group *busiest; /* Busiest group in this sd */ | |
6985 | struct sched_group *local; /* Local group in this sd */ | |
6986 | unsigned long total_load; /* Total load of all groups in sd */ | |
63b2ca30 | 6987 | unsigned long total_capacity; /* Total capacity of all groups in sd */ |
56cf515b JK |
6988 | unsigned long avg_load; /* Average load across all groups in sd */ |
6989 | ||
56cf515b | 6990 | struct sg_lb_stats busiest_stat;/* Statistics of the busiest group */ |
147c5fc2 | 6991 | struct sg_lb_stats local_stat; /* Statistics of the local group */ |
56cf515b JK |
6992 | }; |
6993 | ||
147c5fc2 PZ |
6994 | static inline void init_sd_lb_stats(struct sd_lb_stats *sds) |
6995 | { | |
6996 | /* | |
6997 | * Skimp on the clearing to avoid duplicate work. We can avoid clearing | |
6998 | * local_stat because update_sg_lb_stats() does a full clear/assignment. | |
6999 | * We must however clear busiest_stat::avg_load because | |
7000 | * update_sd_pick_busiest() reads this before assignment. | |
7001 | */ | |
7002 | *sds = (struct sd_lb_stats){ | |
7003 | .busiest = NULL, | |
7004 | .local = NULL, | |
7005 | .total_load = 0UL, | |
63b2ca30 | 7006 | .total_capacity = 0UL, |
147c5fc2 PZ |
7007 | .busiest_stat = { |
7008 | .avg_load = 0UL, | |
caeb178c RR |
7009 | .sum_nr_running = 0, |
7010 | .group_type = group_other, | |
147c5fc2 PZ |
7011 | }, |
7012 | }; | |
7013 | } | |
7014 | ||
1e3c88bd PZ |
7015 | /** |
7016 | * get_sd_load_idx - Obtain the load index for a given sched domain. | |
7017 | * @sd: The sched_domain whose load_idx is to be obtained. | |
ed1b7732 | 7018 | * @idle: The idle status of the CPU for whose sd load_idx is obtained. |
e69f6186 YB |
7019 | * |
7020 | * Return: The load index. | |
1e3c88bd PZ |
7021 | */ |
7022 | static inline int get_sd_load_idx(struct sched_domain *sd, | |
7023 | enum cpu_idle_type idle) | |
7024 | { | |
7025 | int load_idx; | |
7026 | ||
7027 | switch (idle) { | |
7028 | case CPU_NOT_IDLE: | |
7029 | load_idx = sd->busy_idx; | |
7030 | break; | |
7031 | ||
7032 | case CPU_NEWLY_IDLE: | |
7033 | load_idx = sd->newidle_idx; | |
7034 | break; | |
7035 | default: | |
7036 | load_idx = sd->idle_idx; | |
7037 | break; | |
7038 | } | |
7039 | ||
7040 | return load_idx; | |
7041 | } | |
7042 | ||
ced549fa | 7043 | static unsigned long scale_rt_capacity(int cpu) |
1e3c88bd PZ |
7044 | { |
7045 | struct rq *rq = cpu_rq(cpu); | |
b5b4860d | 7046 | u64 total, used, age_stamp, avg; |
cadefd3d | 7047 | s64 delta; |
1e3c88bd | 7048 | |
b654f7de PZ |
7049 | /* |
7050 | * Since we're reading these variables without serialization make sure | |
7051 | * we read them once before doing sanity checks on them. | |
7052 | */ | |
316c1608 JL |
7053 | age_stamp = READ_ONCE(rq->age_stamp); |
7054 | avg = READ_ONCE(rq->rt_avg); | |
cebde6d6 | 7055 | delta = __rq_clock_broken(rq) - age_stamp; |
b654f7de | 7056 | |
cadefd3d PZ |
7057 | if (unlikely(delta < 0)) |
7058 | delta = 0; | |
7059 | ||
7060 | total = sched_avg_period() + delta; | |
aa483808 | 7061 | |
b5b4860d | 7062 | used = div_u64(avg, total); |
1e3c88bd | 7063 | |
b5b4860d VG |
7064 | if (likely(used < SCHED_CAPACITY_SCALE)) |
7065 | return SCHED_CAPACITY_SCALE - used; | |
1e3c88bd | 7066 | |
b5b4860d | 7067 | return 1; |
1e3c88bd PZ |
7068 | } |
7069 | ||
ced549fa | 7070 | static void update_cpu_capacity(struct sched_domain *sd, int cpu) |
1e3c88bd | 7071 | { |
8cd5601c | 7072 | unsigned long capacity = arch_scale_cpu_capacity(sd, cpu); |
1e3c88bd PZ |
7073 | struct sched_group *sdg = sd->groups; |
7074 | ||
ca6d75e6 | 7075 | cpu_rq(cpu)->cpu_capacity_orig = capacity; |
9d5efe05 | 7076 | |
ced549fa | 7077 | capacity *= scale_rt_capacity(cpu); |
ca8ce3d0 | 7078 | capacity >>= SCHED_CAPACITY_SHIFT; |
1e3c88bd | 7079 | |
ced549fa NP |
7080 | if (!capacity) |
7081 | capacity = 1; | |
1e3c88bd | 7082 | |
ced549fa NP |
7083 | cpu_rq(cpu)->cpu_capacity = capacity; |
7084 | sdg->sgc->capacity = capacity; | |
bf475ce0 | 7085 | sdg->sgc->min_capacity = capacity; |
1e3c88bd PZ |
7086 | } |
7087 | ||
63b2ca30 | 7088 | void update_group_capacity(struct sched_domain *sd, int cpu) |
1e3c88bd PZ |
7089 | { |
7090 | struct sched_domain *child = sd->child; | |
7091 | struct sched_group *group, *sdg = sd->groups; | |
bf475ce0 | 7092 | unsigned long capacity, min_capacity; |
4ec4412e VG |
7093 | unsigned long interval; |
7094 | ||
7095 | interval = msecs_to_jiffies(sd->balance_interval); | |
7096 | interval = clamp(interval, 1UL, max_load_balance_interval); | |
63b2ca30 | 7097 | sdg->sgc->next_update = jiffies + interval; |
1e3c88bd PZ |
7098 | |
7099 | if (!child) { | |
ced549fa | 7100 | update_cpu_capacity(sd, cpu); |
1e3c88bd PZ |
7101 | return; |
7102 | } | |
7103 | ||
dc7ff76e | 7104 | capacity = 0; |
bf475ce0 | 7105 | min_capacity = ULONG_MAX; |
1e3c88bd | 7106 | |
74a5ce20 PZ |
7107 | if (child->flags & SD_OVERLAP) { |
7108 | /* | |
7109 | * SD_OVERLAP domains cannot assume that child groups | |
7110 | * span the current group. | |
7111 | */ | |
7112 | ||
863bffc8 | 7113 | for_each_cpu(cpu, sched_group_cpus(sdg)) { |
63b2ca30 | 7114 | struct sched_group_capacity *sgc; |
9abf24d4 | 7115 | struct rq *rq = cpu_rq(cpu); |
863bffc8 | 7116 | |
9abf24d4 | 7117 | /* |
63b2ca30 | 7118 | * build_sched_domains() -> init_sched_groups_capacity() |
9abf24d4 SD |
7119 | * gets here before we've attached the domains to the |
7120 | * runqueues. | |
7121 | * | |
ced549fa NP |
7122 | * Use capacity_of(), which is set irrespective of domains |
7123 | * in update_cpu_capacity(). | |
9abf24d4 | 7124 | * |
dc7ff76e | 7125 | * This avoids capacity from being 0 and |
9abf24d4 | 7126 | * causing divide-by-zero issues on boot. |
9abf24d4 SD |
7127 | */ |
7128 | if (unlikely(!rq->sd)) { | |
ced549fa | 7129 | capacity += capacity_of(cpu); |
bf475ce0 MR |
7130 | } else { |
7131 | sgc = rq->sd->groups->sgc; | |
7132 | capacity += sgc->capacity; | |
9abf24d4 | 7133 | } |
863bffc8 | 7134 | |
bf475ce0 | 7135 | min_capacity = min(capacity, min_capacity); |
863bffc8 | 7136 | } |
74a5ce20 PZ |
7137 | } else { |
7138 | /* | |
7139 | * !SD_OVERLAP domains can assume that child groups | |
7140 | * span the current group. | |
97a7142f | 7141 | */ |
74a5ce20 PZ |
7142 | |
7143 | group = child->groups; | |
7144 | do { | |
bf475ce0 MR |
7145 | struct sched_group_capacity *sgc = group->sgc; |
7146 | ||
7147 | capacity += sgc->capacity; | |
7148 | min_capacity = min(sgc->min_capacity, min_capacity); | |
74a5ce20 PZ |
7149 | group = group->next; |
7150 | } while (group != child->groups); | |
7151 | } | |
1e3c88bd | 7152 | |
63b2ca30 | 7153 | sdg->sgc->capacity = capacity; |
bf475ce0 | 7154 | sdg->sgc->min_capacity = min_capacity; |
1e3c88bd PZ |
7155 | } |
7156 | ||
9d5efe05 | 7157 | /* |
ea67821b VG |
7158 | * Check whether the capacity of the rq has been noticeably reduced by side |
7159 | * activity. The imbalance_pct is used for the threshold. | |
7160 | * Return true is the capacity is reduced | |
9d5efe05 SV |
7161 | */ |
7162 | static inline int | |
ea67821b | 7163 | check_cpu_capacity(struct rq *rq, struct sched_domain *sd) |
9d5efe05 | 7164 | { |
ea67821b VG |
7165 | return ((rq->cpu_capacity * sd->imbalance_pct) < |
7166 | (rq->cpu_capacity_orig * 100)); | |
9d5efe05 SV |
7167 | } |
7168 | ||
30ce5dab PZ |
7169 | /* |
7170 | * Group imbalance indicates (and tries to solve) the problem where balancing | |
7171 | * groups is inadequate due to tsk_cpus_allowed() constraints. | |
7172 | * | |
7173 | * Imagine a situation of two groups of 4 cpus each and 4 tasks each with a | |
7174 | * cpumask covering 1 cpu of the first group and 3 cpus of the second group. | |
7175 | * Something like: | |
7176 | * | |
7177 | * { 0 1 2 3 } { 4 5 6 7 } | |
7178 | * * * * * | |
7179 | * | |
7180 | * If we were to balance group-wise we'd place two tasks in the first group and | |
7181 | * two tasks in the second group. Clearly this is undesired as it will overload | |
7182 | * cpu 3 and leave one of the cpus in the second group unused. | |
7183 | * | |
7184 | * The current solution to this issue is detecting the skew in the first group | |
6263322c PZ |
7185 | * by noticing the lower domain failed to reach balance and had difficulty |
7186 | * moving tasks due to affinity constraints. | |
30ce5dab PZ |
7187 | * |
7188 | * When this is so detected; this group becomes a candidate for busiest; see | |
ed1b7732 | 7189 | * update_sd_pick_busiest(). And calculate_imbalance() and |
6263322c | 7190 | * find_busiest_group() avoid some of the usual balance conditions to allow it |
30ce5dab PZ |
7191 | * to create an effective group imbalance. |
7192 | * | |
7193 | * This is a somewhat tricky proposition since the next run might not find the | |
7194 | * group imbalance and decide the groups need to be balanced again. A most | |
7195 | * subtle and fragile situation. | |
7196 | */ | |
7197 | ||
6263322c | 7198 | static inline int sg_imbalanced(struct sched_group *group) |
30ce5dab | 7199 | { |
63b2ca30 | 7200 | return group->sgc->imbalance; |
30ce5dab PZ |
7201 | } |
7202 | ||
b37d9316 | 7203 | /* |
ea67821b VG |
7204 | * group_has_capacity returns true if the group has spare capacity that could |
7205 | * be used by some tasks. | |
7206 | * We consider that a group has spare capacity if the * number of task is | |
9e91d61d DE |
7207 | * smaller than the number of CPUs or if the utilization is lower than the |
7208 | * available capacity for CFS tasks. | |
ea67821b VG |
7209 | * For the latter, we use a threshold to stabilize the state, to take into |
7210 | * account the variance of the tasks' load and to return true if the available | |
7211 | * capacity in meaningful for the load balancer. | |
7212 | * As an example, an available capacity of 1% can appear but it doesn't make | |
7213 | * any benefit for the load balance. | |
b37d9316 | 7214 | */ |
ea67821b VG |
7215 | static inline bool |
7216 | group_has_capacity(struct lb_env *env, struct sg_lb_stats *sgs) | |
b37d9316 | 7217 | { |
ea67821b VG |
7218 | if (sgs->sum_nr_running < sgs->group_weight) |
7219 | return true; | |
c61037e9 | 7220 | |
ea67821b | 7221 | if ((sgs->group_capacity * 100) > |
9e91d61d | 7222 | (sgs->group_util * env->sd->imbalance_pct)) |
ea67821b | 7223 | return true; |
b37d9316 | 7224 | |
ea67821b VG |
7225 | return false; |
7226 | } | |
7227 | ||
7228 | /* | |
7229 | * group_is_overloaded returns true if the group has more tasks than it can | |
7230 | * handle. | |
7231 | * group_is_overloaded is not equals to !group_has_capacity because a group | |
7232 | * with the exact right number of tasks, has no more spare capacity but is not | |
7233 | * overloaded so both group_has_capacity and group_is_overloaded return | |
7234 | * false. | |
7235 | */ | |
7236 | static inline bool | |
7237 | group_is_overloaded(struct lb_env *env, struct sg_lb_stats *sgs) | |
7238 | { | |
7239 | if (sgs->sum_nr_running <= sgs->group_weight) | |
7240 | return false; | |
b37d9316 | 7241 | |
ea67821b | 7242 | if ((sgs->group_capacity * 100) < |
9e91d61d | 7243 | (sgs->group_util * env->sd->imbalance_pct)) |
ea67821b | 7244 | return true; |
b37d9316 | 7245 | |
ea67821b | 7246 | return false; |
b37d9316 PZ |
7247 | } |
7248 | ||
9e0994c0 MR |
7249 | /* |
7250 | * group_smaller_cpu_capacity: Returns true if sched_group sg has smaller | |
7251 | * per-CPU capacity than sched_group ref. | |
7252 | */ | |
7253 | static inline bool | |
7254 | group_smaller_cpu_capacity(struct sched_group *sg, struct sched_group *ref) | |
7255 | { | |
7256 | return sg->sgc->min_capacity * capacity_margin < | |
7257 | ref->sgc->min_capacity * 1024; | |
7258 | } | |
7259 | ||
79a89f92 LY |
7260 | static inline enum |
7261 | group_type group_classify(struct sched_group *group, | |
7262 | struct sg_lb_stats *sgs) | |
caeb178c | 7263 | { |
ea67821b | 7264 | if (sgs->group_no_capacity) |
caeb178c RR |
7265 | return group_overloaded; |
7266 | ||
7267 | if (sg_imbalanced(group)) | |
7268 | return group_imbalanced; | |
7269 | ||
7270 | return group_other; | |
7271 | } | |
7272 | ||
1e3c88bd PZ |
7273 | /** |
7274 | * update_sg_lb_stats - Update sched_group's statistics for load balancing. | |
cd96891d | 7275 | * @env: The load balancing environment. |
1e3c88bd | 7276 | * @group: sched_group whose statistics are to be updated. |
1e3c88bd | 7277 | * @load_idx: Load index of sched_domain of this_cpu for load calc. |
1e3c88bd | 7278 | * @local_group: Does group contain this_cpu. |
1e3c88bd | 7279 | * @sgs: variable to hold the statistics for this group. |
cd3bd4e6 | 7280 | * @overload: Indicate more than one runnable task for any CPU. |
1e3c88bd | 7281 | */ |
bd939f45 PZ |
7282 | static inline void update_sg_lb_stats(struct lb_env *env, |
7283 | struct sched_group *group, int load_idx, | |
4486edd1 TC |
7284 | int local_group, struct sg_lb_stats *sgs, |
7285 | bool *overload) | |
1e3c88bd | 7286 | { |
30ce5dab | 7287 | unsigned long load; |
a426f99c | 7288 | int i, nr_running; |
1e3c88bd | 7289 | |
b72ff13c PZ |
7290 | memset(sgs, 0, sizeof(*sgs)); |
7291 | ||
b9403130 | 7292 | for_each_cpu_and(i, sched_group_cpus(group), env->cpus) { |
1e3c88bd PZ |
7293 | struct rq *rq = cpu_rq(i); |
7294 | ||
1e3c88bd | 7295 | /* Bias balancing toward cpus of our domain */ |
6263322c | 7296 | if (local_group) |
04f733b4 | 7297 | load = target_load(i, load_idx); |
6263322c | 7298 | else |
1e3c88bd | 7299 | load = source_load(i, load_idx); |
1e3c88bd PZ |
7300 | |
7301 | sgs->group_load += load; | |
9e91d61d | 7302 | sgs->group_util += cpu_util(i); |
65fdac08 | 7303 | sgs->sum_nr_running += rq->cfs.h_nr_running; |
4486edd1 | 7304 | |
a426f99c WL |
7305 | nr_running = rq->nr_running; |
7306 | if (nr_running > 1) | |
4486edd1 TC |
7307 | *overload = true; |
7308 | ||
0ec8aa00 PZ |
7309 | #ifdef CONFIG_NUMA_BALANCING |
7310 | sgs->nr_numa_running += rq->nr_numa_running; | |
7311 | sgs->nr_preferred_running += rq->nr_preferred_running; | |
7312 | #endif | |
1e3c88bd | 7313 | sgs->sum_weighted_load += weighted_cpuload(i); |
a426f99c WL |
7314 | /* |
7315 | * No need to call idle_cpu() if nr_running is not 0 | |
7316 | */ | |
7317 | if (!nr_running && idle_cpu(i)) | |
aae6d3dd | 7318 | sgs->idle_cpus++; |
1e3c88bd PZ |
7319 | } |
7320 | ||
63b2ca30 NP |
7321 | /* Adjust by relative CPU capacity of the group */ |
7322 | sgs->group_capacity = group->sgc->capacity; | |
ca8ce3d0 | 7323 | sgs->avg_load = (sgs->group_load*SCHED_CAPACITY_SCALE) / sgs->group_capacity; |
1e3c88bd | 7324 | |
dd5feea1 | 7325 | if (sgs->sum_nr_running) |
38d0f770 | 7326 | sgs->load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running; |
1e3c88bd | 7327 | |
aae6d3dd | 7328 | sgs->group_weight = group->group_weight; |
b37d9316 | 7329 | |
ea67821b | 7330 | sgs->group_no_capacity = group_is_overloaded(env, sgs); |
79a89f92 | 7331 | sgs->group_type = group_classify(group, sgs); |
1e3c88bd PZ |
7332 | } |
7333 | ||
532cb4c4 MN |
7334 | /** |
7335 | * update_sd_pick_busiest - return 1 on busiest group | |
cd96891d | 7336 | * @env: The load balancing environment. |
532cb4c4 MN |
7337 | * @sds: sched_domain statistics |
7338 | * @sg: sched_group candidate to be checked for being the busiest | |
b6b12294 | 7339 | * @sgs: sched_group statistics |
532cb4c4 MN |
7340 | * |
7341 | * Determine if @sg is a busier group than the previously selected | |
7342 | * busiest group. | |
e69f6186 YB |
7343 | * |
7344 | * Return: %true if @sg is a busier group than the previously selected | |
7345 | * busiest group. %false otherwise. | |
532cb4c4 | 7346 | */ |
bd939f45 | 7347 | static bool update_sd_pick_busiest(struct lb_env *env, |
532cb4c4 MN |
7348 | struct sd_lb_stats *sds, |
7349 | struct sched_group *sg, | |
bd939f45 | 7350 | struct sg_lb_stats *sgs) |
532cb4c4 | 7351 | { |
caeb178c | 7352 | struct sg_lb_stats *busiest = &sds->busiest_stat; |
532cb4c4 | 7353 | |
caeb178c | 7354 | if (sgs->group_type > busiest->group_type) |
532cb4c4 MN |
7355 | return true; |
7356 | ||
caeb178c RR |
7357 | if (sgs->group_type < busiest->group_type) |
7358 | return false; | |
7359 | ||
7360 | if (sgs->avg_load <= busiest->avg_load) | |
7361 | return false; | |
7362 | ||
9e0994c0 MR |
7363 | if (!(env->sd->flags & SD_ASYM_CPUCAPACITY)) |
7364 | goto asym_packing; | |
7365 | ||
7366 | /* | |
7367 | * Candidate sg has no more than one task per CPU and | |
7368 | * has higher per-CPU capacity. Migrating tasks to less | |
7369 | * capable CPUs may harm throughput. Maximize throughput, | |
7370 | * power/energy consequences are not considered. | |
7371 | */ | |
7372 | if (sgs->sum_nr_running <= sgs->group_weight && | |
7373 | group_smaller_cpu_capacity(sds->local, sg)) | |
7374 | return false; | |
7375 | ||
7376 | asym_packing: | |
caeb178c RR |
7377 | /* This is the busiest node in its class. */ |
7378 | if (!(env->sd->flags & SD_ASYM_PACKING)) | |
532cb4c4 MN |
7379 | return true; |
7380 | ||
1f621e02 SD |
7381 | /* No ASYM_PACKING if target cpu is already busy */ |
7382 | if (env->idle == CPU_NOT_IDLE) | |
7383 | return true; | |
532cb4c4 MN |
7384 | /* |
7385 | * ASYM_PACKING needs to move all the work to the lowest | |
7386 | * numbered CPUs in the group, therefore mark all groups | |
7387 | * higher than ourself as busy. | |
7388 | */ | |
caeb178c | 7389 | if (sgs->sum_nr_running && env->dst_cpu < group_first_cpu(sg)) { |
532cb4c4 MN |
7390 | if (!sds->busiest) |
7391 | return true; | |
7392 | ||
1f621e02 SD |
7393 | /* Prefer to move from highest possible cpu's work */ |
7394 | if (group_first_cpu(sds->busiest) < group_first_cpu(sg)) | |
532cb4c4 MN |
7395 | return true; |
7396 | } | |
7397 | ||
7398 | return false; | |
7399 | } | |
7400 | ||
0ec8aa00 PZ |
7401 | #ifdef CONFIG_NUMA_BALANCING |
7402 | static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs) | |
7403 | { | |
7404 | if (sgs->sum_nr_running > sgs->nr_numa_running) | |
7405 | return regular; | |
7406 | if (sgs->sum_nr_running > sgs->nr_preferred_running) | |
7407 | return remote; | |
7408 | return all; | |
7409 | } | |
7410 | ||
7411 | static inline enum fbq_type fbq_classify_rq(struct rq *rq) | |
7412 | { | |
7413 | if (rq->nr_running > rq->nr_numa_running) | |
7414 | return regular; | |
7415 | if (rq->nr_running > rq->nr_preferred_running) | |
7416 | return remote; | |
7417 | return all; | |
7418 | } | |
7419 | #else | |
7420 | static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs) | |
7421 | { | |
7422 | return all; | |
7423 | } | |
7424 | ||
7425 | static inline enum fbq_type fbq_classify_rq(struct rq *rq) | |
7426 | { | |
7427 | return regular; | |
7428 | } | |
7429 | #endif /* CONFIG_NUMA_BALANCING */ | |
7430 | ||
1e3c88bd | 7431 | /** |
461819ac | 7432 | * update_sd_lb_stats - Update sched_domain's statistics for load balancing. |
cd96891d | 7433 | * @env: The load balancing environment. |
1e3c88bd PZ |
7434 | * @sds: variable to hold the statistics for this sched_domain. |
7435 | */ | |
0ec8aa00 | 7436 | static inline void update_sd_lb_stats(struct lb_env *env, struct sd_lb_stats *sds) |
1e3c88bd | 7437 | { |
bd939f45 PZ |
7438 | struct sched_domain *child = env->sd->child; |
7439 | struct sched_group *sg = env->sd->groups; | |
56cf515b | 7440 | struct sg_lb_stats tmp_sgs; |
1e3c88bd | 7441 | int load_idx, prefer_sibling = 0; |
4486edd1 | 7442 | bool overload = false; |
1e3c88bd PZ |
7443 | |
7444 | if (child && child->flags & SD_PREFER_SIBLING) | |
7445 | prefer_sibling = 1; | |
7446 | ||
bd939f45 | 7447 | load_idx = get_sd_load_idx(env->sd, env->idle); |
1e3c88bd PZ |
7448 | |
7449 | do { | |
56cf515b | 7450 | struct sg_lb_stats *sgs = &tmp_sgs; |
1e3c88bd PZ |
7451 | int local_group; |
7452 | ||
bd939f45 | 7453 | local_group = cpumask_test_cpu(env->dst_cpu, sched_group_cpus(sg)); |
56cf515b JK |
7454 | if (local_group) { |
7455 | sds->local = sg; | |
7456 | sgs = &sds->local_stat; | |
b72ff13c PZ |
7457 | |
7458 | if (env->idle != CPU_NEWLY_IDLE || | |
63b2ca30 NP |
7459 | time_after_eq(jiffies, sg->sgc->next_update)) |
7460 | update_group_capacity(env->sd, env->dst_cpu); | |
56cf515b | 7461 | } |
1e3c88bd | 7462 | |
4486edd1 TC |
7463 | update_sg_lb_stats(env, sg, load_idx, local_group, sgs, |
7464 | &overload); | |
1e3c88bd | 7465 | |
b72ff13c PZ |
7466 | if (local_group) |
7467 | goto next_group; | |
7468 | ||
1e3c88bd PZ |
7469 | /* |
7470 | * In case the child domain prefers tasks go to siblings | |
ea67821b | 7471 | * first, lower the sg capacity so that we'll try |
75dd321d NR |
7472 | * and move all the excess tasks away. We lower the capacity |
7473 | * of a group only if the local group has the capacity to fit | |
ea67821b VG |
7474 | * these excess tasks. The extra check prevents the case where |
7475 | * you always pull from the heaviest group when it is already | |
7476 | * under-utilized (possible with a large weight task outweighs | |
7477 | * the tasks on the system). | |
1e3c88bd | 7478 | */ |
b72ff13c | 7479 | if (prefer_sibling && sds->local && |
ea67821b VG |
7480 | group_has_capacity(env, &sds->local_stat) && |
7481 | (sgs->sum_nr_running > 1)) { | |
7482 | sgs->group_no_capacity = 1; | |
79a89f92 | 7483 | sgs->group_type = group_classify(sg, sgs); |
cb0b9f24 | 7484 | } |
1e3c88bd | 7485 | |
b72ff13c | 7486 | if (update_sd_pick_busiest(env, sds, sg, sgs)) { |
532cb4c4 | 7487 | sds->busiest = sg; |
56cf515b | 7488 | sds->busiest_stat = *sgs; |
1e3c88bd PZ |
7489 | } |
7490 | ||
b72ff13c PZ |
7491 | next_group: |
7492 | /* Now, start updating sd_lb_stats */ | |
7493 | sds->total_load += sgs->group_load; | |
63b2ca30 | 7494 | sds->total_capacity += sgs->group_capacity; |
b72ff13c | 7495 | |
532cb4c4 | 7496 | sg = sg->next; |
bd939f45 | 7497 | } while (sg != env->sd->groups); |
0ec8aa00 PZ |
7498 | |
7499 | if (env->sd->flags & SD_NUMA) | |
7500 | env->fbq_type = fbq_classify_group(&sds->busiest_stat); | |
4486edd1 TC |
7501 | |
7502 | if (!env->sd->parent) { | |
7503 | /* update overload indicator if we are at root domain */ | |
7504 | if (env->dst_rq->rd->overload != overload) | |
7505 | env->dst_rq->rd->overload = overload; | |
7506 | } | |
7507 | ||
532cb4c4 MN |
7508 | } |
7509 | ||
532cb4c4 MN |
7510 | /** |
7511 | * check_asym_packing - Check to see if the group is packed into the | |
7512 | * sched doman. | |
7513 | * | |
7514 | * This is primarily intended to used at the sibling level. Some | |
7515 | * cores like POWER7 prefer to use lower numbered SMT threads. In the | |
7516 | * case of POWER7, it can move to lower SMT modes only when higher | |
7517 | * threads are idle. When in lower SMT modes, the threads will | |
7518 | * perform better since they share less core resources. Hence when we | |
7519 | * have idle threads, we want them to be the higher ones. | |
7520 | * | |
7521 | * This packing function is run on idle threads. It checks to see if | |
7522 | * the busiest CPU in this domain (core in the P7 case) has a higher | |
7523 | * CPU number than the packing function is being run on. Here we are | |
7524 | * assuming lower CPU number will be equivalent to lower a SMT thread | |
7525 | * number. | |
7526 | * | |
e69f6186 | 7527 | * Return: 1 when packing is required and a task should be moved to |
b6b12294 MN |
7528 | * this CPU. The amount of the imbalance is returned in *imbalance. |
7529 | * | |
cd96891d | 7530 | * @env: The load balancing environment. |
532cb4c4 | 7531 | * @sds: Statistics of the sched_domain which is to be packed |
532cb4c4 | 7532 | */ |
bd939f45 | 7533 | static int check_asym_packing(struct lb_env *env, struct sd_lb_stats *sds) |
532cb4c4 MN |
7534 | { |
7535 | int busiest_cpu; | |
7536 | ||
bd939f45 | 7537 | if (!(env->sd->flags & SD_ASYM_PACKING)) |
532cb4c4 MN |
7538 | return 0; |
7539 | ||
1f621e02 SD |
7540 | if (env->idle == CPU_NOT_IDLE) |
7541 | return 0; | |
7542 | ||
532cb4c4 MN |
7543 | if (!sds->busiest) |
7544 | return 0; | |
7545 | ||
7546 | busiest_cpu = group_first_cpu(sds->busiest); | |
bd939f45 | 7547 | if (env->dst_cpu > busiest_cpu) |
532cb4c4 MN |
7548 | return 0; |
7549 | ||
bd939f45 | 7550 | env->imbalance = DIV_ROUND_CLOSEST( |
63b2ca30 | 7551 | sds->busiest_stat.avg_load * sds->busiest_stat.group_capacity, |
ca8ce3d0 | 7552 | SCHED_CAPACITY_SCALE); |
bd939f45 | 7553 | |
532cb4c4 | 7554 | return 1; |
1e3c88bd PZ |
7555 | } |
7556 | ||
7557 | /** | |
7558 | * fix_small_imbalance - Calculate the minor imbalance that exists | |
7559 | * amongst the groups of a sched_domain, during | |
7560 | * load balancing. | |
cd96891d | 7561 | * @env: The load balancing environment. |
1e3c88bd | 7562 | * @sds: Statistics of the sched_domain whose imbalance is to be calculated. |
1e3c88bd | 7563 | */ |
bd939f45 PZ |
7564 | static inline |
7565 | void fix_small_imbalance(struct lb_env *env, struct sd_lb_stats *sds) | |
1e3c88bd | 7566 | { |
63b2ca30 | 7567 | unsigned long tmp, capa_now = 0, capa_move = 0; |
1e3c88bd | 7568 | unsigned int imbn = 2; |
dd5feea1 | 7569 | unsigned long scaled_busy_load_per_task; |
56cf515b | 7570 | struct sg_lb_stats *local, *busiest; |
1e3c88bd | 7571 | |
56cf515b JK |
7572 | local = &sds->local_stat; |
7573 | busiest = &sds->busiest_stat; | |
1e3c88bd | 7574 | |
56cf515b JK |
7575 | if (!local->sum_nr_running) |
7576 | local->load_per_task = cpu_avg_load_per_task(env->dst_cpu); | |
7577 | else if (busiest->load_per_task > local->load_per_task) | |
7578 | imbn = 1; | |
dd5feea1 | 7579 | |
56cf515b | 7580 | scaled_busy_load_per_task = |
ca8ce3d0 | 7581 | (busiest->load_per_task * SCHED_CAPACITY_SCALE) / |
63b2ca30 | 7582 | busiest->group_capacity; |
56cf515b | 7583 | |
3029ede3 VD |
7584 | if (busiest->avg_load + scaled_busy_load_per_task >= |
7585 | local->avg_load + (scaled_busy_load_per_task * imbn)) { | |
56cf515b | 7586 | env->imbalance = busiest->load_per_task; |
1e3c88bd PZ |
7587 | return; |
7588 | } | |
7589 | ||
7590 | /* | |
7591 | * OK, we don't have enough imbalance to justify moving tasks, | |
ced549fa | 7592 | * however we may be able to increase total CPU capacity used by |
1e3c88bd PZ |
7593 | * moving them. |
7594 | */ | |
7595 | ||
63b2ca30 | 7596 | capa_now += busiest->group_capacity * |
56cf515b | 7597 | min(busiest->load_per_task, busiest->avg_load); |
63b2ca30 | 7598 | capa_now += local->group_capacity * |
56cf515b | 7599 | min(local->load_per_task, local->avg_load); |
ca8ce3d0 | 7600 | capa_now /= SCHED_CAPACITY_SCALE; |
1e3c88bd PZ |
7601 | |
7602 | /* Amount of load we'd subtract */ | |
a2cd4260 | 7603 | if (busiest->avg_load > scaled_busy_load_per_task) { |
63b2ca30 | 7604 | capa_move += busiest->group_capacity * |
56cf515b | 7605 | min(busiest->load_per_task, |
a2cd4260 | 7606 | busiest->avg_load - scaled_busy_load_per_task); |
56cf515b | 7607 | } |
1e3c88bd PZ |
7608 | |
7609 | /* Amount of load we'd add */ | |
63b2ca30 | 7610 | if (busiest->avg_load * busiest->group_capacity < |
ca8ce3d0 | 7611 | busiest->load_per_task * SCHED_CAPACITY_SCALE) { |
63b2ca30 NP |
7612 | tmp = (busiest->avg_load * busiest->group_capacity) / |
7613 | local->group_capacity; | |
56cf515b | 7614 | } else { |
ca8ce3d0 | 7615 | tmp = (busiest->load_per_task * SCHED_CAPACITY_SCALE) / |
63b2ca30 | 7616 | local->group_capacity; |
56cf515b | 7617 | } |
63b2ca30 | 7618 | capa_move += local->group_capacity * |
3ae11c90 | 7619 | min(local->load_per_task, local->avg_load + tmp); |
ca8ce3d0 | 7620 | capa_move /= SCHED_CAPACITY_SCALE; |
1e3c88bd PZ |
7621 | |
7622 | /* Move if we gain throughput */ | |
63b2ca30 | 7623 | if (capa_move > capa_now) |
56cf515b | 7624 | env->imbalance = busiest->load_per_task; |
1e3c88bd PZ |
7625 | } |
7626 | ||
7627 | /** | |
7628 | * calculate_imbalance - Calculate the amount of imbalance present within the | |
7629 | * groups of a given sched_domain during load balance. | |
bd939f45 | 7630 | * @env: load balance environment |
1e3c88bd | 7631 | * @sds: statistics of the sched_domain whose imbalance is to be calculated. |
1e3c88bd | 7632 | */ |
bd939f45 | 7633 | static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *sds) |
1e3c88bd | 7634 | { |
dd5feea1 | 7635 | unsigned long max_pull, load_above_capacity = ~0UL; |
56cf515b JK |
7636 | struct sg_lb_stats *local, *busiest; |
7637 | ||
7638 | local = &sds->local_stat; | |
56cf515b | 7639 | busiest = &sds->busiest_stat; |
dd5feea1 | 7640 | |
caeb178c | 7641 | if (busiest->group_type == group_imbalanced) { |
30ce5dab PZ |
7642 | /* |
7643 | * In the group_imb case we cannot rely on group-wide averages | |
7644 | * to ensure cpu-load equilibrium, look at wider averages. XXX | |
7645 | */ | |
56cf515b JK |
7646 | busiest->load_per_task = |
7647 | min(busiest->load_per_task, sds->avg_load); | |
dd5feea1 SS |
7648 | } |
7649 | ||
1e3c88bd | 7650 | /* |
885e542c DE |
7651 | * Avg load of busiest sg can be less and avg load of local sg can |
7652 | * be greater than avg load across all sgs of sd because avg load | |
7653 | * factors in sg capacity and sgs with smaller group_type are | |
7654 | * skipped when updating the busiest sg: | |
1e3c88bd | 7655 | */ |
b1885550 VD |
7656 | if (busiest->avg_load <= sds->avg_load || |
7657 | local->avg_load >= sds->avg_load) { | |
bd939f45 PZ |
7658 | env->imbalance = 0; |
7659 | return fix_small_imbalance(env, sds); | |
1e3c88bd PZ |
7660 | } |
7661 | ||
9a5d9ba6 PZ |
7662 | /* |
7663 | * If there aren't any idle cpus, avoid creating some. | |
7664 | */ | |
7665 | if (busiest->group_type == group_overloaded && | |
7666 | local->group_type == group_overloaded) { | |
1be0eb2a | 7667 | load_above_capacity = busiest->sum_nr_running * SCHED_CAPACITY_SCALE; |
cfa10334 | 7668 | if (load_above_capacity > busiest->group_capacity) { |
ea67821b | 7669 | load_above_capacity -= busiest->group_capacity; |
26656215 | 7670 | load_above_capacity *= scale_load_down(NICE_0_LOAD); |
cfa10334 MR |
7671 | load_above_capacity /= busiest->group_capacity; |
7672 | } else | |
ea67821b | 7673 | load_above_capacity = ~0UL; |
dd5feea1 SS |
7674 | } |
7675 | ||
7676 | /* | |
7677 | * We're trying to get all the cpus to the average_load, so we don't | |
7678 | * want to push ourselves above the average load, nor do we wish to | |
7679 | * reduce the max loaded cpu below the average load. At the same time, | |
0a9b23ce DE |
7680 | * we also don't want to reduce the group load below the group |
7681 | * capacity. Thus we look for the minimum possible imbalance. | |
dd5feea1 | 7682 | */ |
30ce5dab | 7683 | max_pull = min(busiest->avg_load - sds->avg_load, load_above_capacity); |
1e3c88bd PZ |
7684 | |
7685 | /* How much load to actually move to equalise the imbalance */ | |
56cf515b | 7686 | env->imbalance = min( |
63b2ca30 NP |
7687 | max_pull * busiest->group_capacity, |
7688 | (sds->avg_load - local->avg_load) * local->group_capacity | |
ca8ce3d0 | 7689 | ) / SCHED_CAPACITY_SCALE; |
1e3c88bd PZ |
7690 | |
7691 | /* | |
7692 | * if *imbalance is less than the average load per runnable task | |
25985edc | 7693 | * there is no guarantee that any tasks will be moved so we'll have |
1e3c88bd PZ |
7694 | * a think about bumping its value to force at least one task to be |
7695 | * moved | |
7696 | */ | |
56cf515b | 7697 | if (env->imbalance < busiest->load_per_task) |
bd939f45 | 7698 | return fix_small_imbalance(env, sds); |
1e3c88bd | 7699 | } |
fab47622 | 7700 | |
1e3c88bd PZ |
7701 | /******* find_busiest_group() helpers end here *********************/ |
7702 | ||
7703 | /** | |
7704 | * find_busiest_group - Returns the busiest group within the sched_domain | |
0a9b23ce | 7705 | * if there is an imbalance. |
1e3c88bd PZ |
7706 | * |
7707 | * Also calculates the amount of weighted load which should be moved | |
7708 | * to restore balance. | |
7709 | * | |
cd96891d | 7710 | * @env: The load balancing environment. |
1e3c88bd | 7711 | * |
e69f6186 | 7712 | * Return: - The busiest group if imbalance exists. |
1e3c88bd | 7713 | */ |
56cf515b | 7714 | static struct sched_group *find_busiest_group(struct lb_env *env) |
1e3c88bd | 7715 | { |
56cf515b | 7716 | struct sg_lb_stats *local, *busiest; |
1e3c88bd PZ |
7717 | struct sd_lb_stats sds; |
7718 | ||
147c5fc2 | 7719 | init_sd_lb_stats(&sds); |
1e3c88bd PZ |
7720 | |
7721 | /* | |
7722 | * Compute the various statistics relavent for load balancing at | |
7723 | * this level. | |
7724 | */ | |
23f0d209 | 7725 | update_sd_lb_stats(env, &sds); |
56cf515b JK |
7726 | local = &sds.local_stat; |
7727 | busiest = &sds.busiest_stat; | |
1e3c88bd | 7728 | |
ea67821b | 7729 | /* ASYM feature bypasses nice load balance check */ |
1f621e02 | 7730 | if (check_asym_packing(env, &sds)) |
532cb4c4 MN |
7731 | return sds.busiest; |
7732 | ||
cc57aa8f | 7733 | /* There is no busy sibling group to pull tasks from */ |
56cf515b | 7734 | if (!sds.busiest || busiest->sum_nr_running == 0) |
1e3c88bd PZ |
7735 | goto out_balanced; |
7736 | ||
ca8ce3d0 NP |
7737 | sds.avg_load = (SCHED_CAPACITY_SCALE * sds.total_load) |
7738 | / sds.total_capacity; | |
b0432d8f | 7739 | |
866ab43e PZ |
7740 | /* |
7741 | * If the busiest group is imbalanced the below checks don't | |
30ce5dab | 7742 | * work because they assume all things are equal, which typically |
866ab43e PZ |
7743 | * isn't true due to cpus_allowed constraints and the like. |
7744 | */ | |
caeb178c | 7745 | if (busiest->group_type == group_imbalanced) |
866ab43e PZ |
7746 | goto force_balance; |
7747 | ||
cc57aa8f | 7748 | /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */ |
ea67821b VG |
7749 | if (env->idle == CPU_NEWLY_IDLE && group_has_capacity(env, local) && |
7750 | busiest->group_no_capacity) | |
fab47622 NR |
7751 | goto force_balance; |
7752 | ||
cc57aa8f | 7753 | /* |
9c58c79a | 7754 | * If the local group is busier than the selected busiest group |
cc57aa8f PZ |
7755 | * don't try and pull any tasks. |
7756 | */ | |
56cf515b | 7757 | if (local->avg_load >= busiest->avg_load) |
1e3c88bd PZ |
7758 | goto out_balanced; |
7759 | ||
cc57aa8f PZ |
7760 | /* |
7761 | * Don't pull any tasks if this group is already above the domain | |
7762 | * average load. | |
7763 | */ | |
56cf515b | 7764 | if (local->avg_load >= sds.avg_load) |
1e3c88bd PZ |
7765 | goto out_balanced; |
7766 | ||
bd939f45 | 7767 | if (env->idle == CPU_IDLE) { |
aae6d3dd | 7768 | /* |
43f4d666 VG |
7769 | * This cpu is idle. If the busiest group is not overloaded |
7770 | * and there is no imbalance between this and busiest group | |
7771 | * wrt idle cpus, it is balanced. The imbalance becomes | |
7772 | * significant if the diff is greater than 1 otherwise we | |
7773 | * might end up to just move the imbalance on another group | |
aae6d3dd | 7774 | */ |
43f4d666 VG |
7775 | if ((busiest->group_type != group_overloaded) && |
7776 | (local->idle_cpus <= (busiest->idle_cpus + 1))) | |
aae6d3dd | 7777 | goto out_balanced; |
c186fafe PZ |
7778 | } else { |
7779 | /* | |
7780 | * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use | |
7781 | * imbalance_pct to be conservative. | |
7782 | */ | |
56cf515b JK |
7783 | if (100 * busiest->avg_load <= |
7784 | env->sd->imbalance_pct * local->avg_load) | |
c186fafe | 7785 | goto out_balanced; |
aae6d3dd | 7786 | } |
1e3c88bd | 7787 | |
fab47622 | 7788 | force_balance: |
1e3c88bd | 7789 | /* Looks like there is an imbalance. Compute it */ |
bd939f45 | 7790 | calculate_imbalance(env, &sds); |
1e3c88bd PZ |
7791 | return sds.busiest; |
7792 | ||
7793 | out_balanced: | |
bd939f45 | 7794 | env->imbalance = 0; |
1e3c88bd PZ |
7795 | return NULL; |
7796 | } | |
7797 | ||
7798 | /* | |
7799 | * find_busiest_queue - find the busiest runqueue among the cpus in group. | |
7800 | */ | |
bd939f45 | 7801 | static struct rq *find_busiest_queue(struct lb_env *env, |
b9403130 | 7802 | struct sched_group *group) |
1e3c88bd PZ |
7803 | { |
7804 | struct rq *busiest = NULL, *rq; | |
ced549fa | 7805 | unsigned long busiest_load = 0, busiest_capacity = 1; |
1e3c88bd PZ |
7806 | int i; |
7807 | ||
6906a408 | 7808 | for_each_cpu_and(i, sched_group_cpus(group), env->cpus) { |
ea67821b | 7809 | unsigned long capacity, wl; |
0ec8aa00 PZ |
7810 | enum fbq_type rt; |
7811 | ||
7812 | rq = cpu_rq(i); | |
7813 | rt = fbq_classify_rq(rq); | |
1e3c88bd | 7814 | |
0ec8aa00 PZ |
7815 | /* |
7816 | * We classify groups/runqueues into three groups: | |
7817 | * - regular: there are !numa tasks | |
7818 | * - remote: there are numa tasks that run on the 'wrong' node | |
7819 | * - all: there is no distinction | |
7820 | * | |
7821 | * In order to avoid migrating ideally placed numa tasks, | |
7822 | * ignore those when there's better options. | |
7823 | * | |
7824 | * If we ignore the actual busiest queue to migrate another | |
7825 | * task, the next balance pass can still reduce the busiest | |
7826 | * queue by moving tasks around inside the node. | |
7827 | * | |
7828 | * If we cannot move enough load due to this classification | |
7829 | * the next pass will adjust the group classification and | |
7830 | * allow migration of more tasks. | |
7831 | * | |
7832 | * Both cases only affect the total convergence complexity. | |
7833 | */ | |
7834 | if (rt > env->fbq_type) | |
7835 | continue; | |
7836 | ||
ced549fa | 7837 | capacity = capacity_of(i); |
9d5efe05 | 7838 | |
6e40f5bb | 7839 | wl = weighted_cpuload(i); |
1e3c88bd | 7840 | |
6e40f5bb TG |
7841 | /* |
7842 | * When comparing with imbalance, use weighted_cpuload() | |
ced549fa | 7843 | * which is not scaled with the cpu capacity. |
6e40f5bb | 7844 | */ |
ea67821b VG |
7845 | |
7846 | if (rq->nr_running == 1 && wl > env->imbalance && | |
7847 | !check_cpu_capacity(rq, env->sd)) | |
1e3c88bd PZ |
7848 | continue; |
7849 | ||
6e40f5bb TG |
7850 | /* |
7851 | * For the load comparisons with the other cpu's, consider | |
ced549fa NP |
7852 | * the weighted_cpuload() scaled with the cpu capacity, so |
7853 | * that the load can be moved away from the cpu that is | |
7854 | * potentially running at a lower capacity. | |
95a79b80 | 7855 | * |
ced549fa | 7856 | * Thus we're looking for max(wl_i / capacity_i), crosswise |
95a79b80 | 7857 | * multiplication to rid ourselves of the division works out |
ced549fa NP |
7858 | * to: wl_i * capacity_j > wl_j * capacity_i; where j is |
7859 | * our previous maximum. | |
6e40f5bb | 7860 | */ |
ced549fa | 7861 | if (wl * busiest_capacity > busiest_load * capacity) { |
95a79b80 | 7862 | busiest_load = wl; |
ced549fa | 7863 | busiest_capacity = capacity; |
1e3c88bd PZ |
7864 | busiest = rq; |
7865 | } | |
7866 | } | |
7867 | ||
7868 | return busiest; | |
7869 | } | |
7870 | ||
7871 | /* | |
7872 | * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but | |
7873 | * so long as it is large enough. | |
7874 | */ | |
7875 | #define MAX_PINNED_INTERVAL 512 | |
7876 | ||
bd939f45 | 7877 | static int need_active_balance(struct lb_env *env) |
1af3ed3d | 7878 | { |
bd939f45 PZ |
7879 | struct sched_domain *sd = env->sd; |
7880 | ||
7881 | if (env->idle == CPU_NEWLY_IDLE) { | |
532cb4c4 MN |
7882 | |
7883 | /* | |
7884 | * ASYM_PACKING needs to force migrate tasks from busy but | |
7885 | * higher numbered CPUs in order to pack all tasks in the | |
7886 | * lowest numbered CPUs. | |
7887 | */ | |
bd939f45 | 7888 | if ((sd->flags & SD_ASYM_PACKING) && env->src_cpu > env->dst_cpu) |
532cb4c4 | 7889 | return 1; |
1af3ed3d PZ |
7890 | } |
7891 | ||
1aaf90a4 VG |
7892 | /* |
7893 | * The dst_cpu is idle and the src_cpu CPU has only 1 CFS task. | |
7894 | * It's worth migrating the task if the src_cpu's capacity is reduced | |
7895 | * because of other sched_class or IRQs if more capacity stays | |
7896 | * available on dst_cpu. | |
7897 | */ | |
7898 | if ((env->idle != CPU_NOT_IDLE) && | |
7899 | (env->src_rq->cfs.h_nr_running == 1)) { | |
7900 | if ((check_cpu_capacity(env->src_rq, sd)) && | |
7901 | (capacity_of(env->src_cpu)*sd->imbalance_pct < capacity_of(env->dst_cpu)*100)) | |
7902 | return 1; | |
7903 | } | |
7904 | ||
1af3ed3d PZ |
7905 | return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2); |
7906 | } | |
7907 | ||
969c7921 TH |
7908 | static int active_load_balance_cpu_stop(void *data); |
7909 | ||
23f0d209 JK |
7910 | static int should_we_balance(struct lb_env *env) |
7911 | { | |
7912 | struct sched_group *sg = env->sd->groups; | |
7913 | struct cpumask *sg_cpus, *sg_mask; | |
7914 | int cpu, balance_cpu = -1; | |
7915 | ||
7916 | /* | |
7917 | * In the newly idle case, we will allow all the cpu's | |
7918 | * to do the newly idle load balance. | |
7919 | */ | |
7920 | if (env->idle == CPU_NEWLY_IDLE) | |
7921 | return 1; | |
7922 | ||
7923 | sg_cpus = sched_group_cpus(sg); | |
7924 | sg_mask = sched_group_mask(sg); | |
7925 | /* Try to find first idle cpu */ | |
7926 | for_each_cpu_and(cpu, sg_cpus, env->cpus) { | |
7927 | if (!cpumask_test_cpu(cpu, sg_mask) || !idle_cpu(cpu)) | |
7928 | continue; | |
7929 | ||
7930 | balance_cpu = cpu; | |
7931 | break; | |
7932 | } | |
7933 | ||
7934 | if (balance_cpu == -1) | |
7935 | balance_cpu = group_balance_cpu(sg); | |
7936 | ||
7937 | /* | |
7938 | * First idle cpu or the first cpu(busiest) in this sched group | |
7939 | * is eligible for doing load balancing at this and above domains. | |
7940 | */ | |
b0cff9d8 | 7941 | return balance_cpu == env->dst_cpu; |
23f0d209 JK |
7942 | } |
7943 | ||
1e3c88bd PZ |
7944 | /* |
7945 | * Check this_cpu to ensure it is balanced within domain. Attempt to move | |
7946 | * tasks if there is an imbalance. | |
7947 | */ | |
7948 | static int load_balance(int this_cpu, struct rq *this_rq, | |
7949 | struct sched_domain *sd, enum cpu_idle_type idle, | |
23f0d209 | 7950 | int *continue_balancing) |
1e3c88bd | 7951 | { |
88b8dac0 | 7952 | int ld_moved, cur_ld_moved, active_balance = 0; |
6263322c | 7953 | struct sched_domain *sd_parent = sd->parent; |
1e3c88bd | 7954 | struct sched_group *group; |
1e3c88bd PZ |
7955 | struct rq *busiest; |
7956 | unsigned long flags; | |
4ba29684 | 7957 | struct cpumask *cpus = this_cpu_cpumask_var_ptr(load_balance_mask); |
1e3c88bd | 7958 | |
8e45cb54 PZ |
7959 | struct lb_env env = { |
7960 | .sd = sd, | |
ddcdf6e7 PZ |
7961 | .dst_cpu = this_cpu, |
7962 | .dst_rq = this_rq, | |
88b8dac0 | 7963 | .dst_grpmask = sched_group_cpus(sd->groups), |
8e45cb54 | 7964 | .idle = idle, |
eb95308e | 7965 | .loop_break = sched_nr_migrate_break, |
b9403130 | 7966 | .cpus = cpus, |
0ec8aa00 | 7967 | .fbq_type = all, |
163122b7 | 7968 | .tasks = LIST_HEAD_INIT(env.tasks), |
8e45cb54 PZ |
7969 | }; |
7970 | ||
cfc03118 JK |
7971 | /* |
7972 | * For NEWLY_IDLE load_balancing, we don't need to consider | |
7973 | * other cpus in our group | |
7974 | */ | |
e02e60c1 | 7975 | if (idle == CPU_NEWLY_IDLE) |
cfc03118 | 7976 | env.dst_grpmask = NULL; |
cfc03118 | 7977 | |
1e3c88bd PZ |
7978 | cpumask_copy(cpus, cpu_active_mask); |
7979 | ||
ae92882e | 7980 | schedstat_inc(sd->lb_count[idle]); |
1e3c88bd PZ |
7981 | |
7982 | redo: | |
23f0d209 JK |
7983 | if (!should_we_balance(&env)) { |
7984 | *continue_balancing = 0; | |
1e3c88bd | 7985 | goto out_balanced; |
23f0d209 | 7986 | } |
1e3c88bd | 7987 | |
23f0d209 | 7988 | group = find_busiest_group(&env); |
1e3c88bd | 7989 | if (!group) { |
ae92882e | 7990 | schedstat_inc(sd->lb_nobusyg[idle]); |
1e3c88bd PZ |
7991 | goto out_balanced; |
7992 | } | |
7993 | ||
b9403130 | 7994 | busiest = find_busiest_queue(&env, group); |
1e3c88bd | 7995 | if (!busiest) { |
ae92882e | 7996 | schedstat_inc(sd->lb_nobusyq[idle]); |
1e3c88bd PZ |
7997 | goto out_balanced; |
7998 | } | |
7999 | ||
78feefc5 | 8000 | BUG_ON(busiest == env.dst_rq); |
1e3c88bd | 8001 | |
ae92882e | 8002 | schedstat_add(sd->lb_imbalance[idle], env.imbalance); |
1e3c88bd | 8003 | |
1aaf90a4 VG |
8004 | env.src_cpu = busiest->cpu; |
8005 | env.src_rq = busiest; | |
8006 | ||
1e3c88bd PZ |
8007 | ld_moved = 0; |
8008 | if (busiest->nr_running > 1) { | |
8009 | /* | |
8010 | * Attempt to move tasks. If find_busiest_group has found | |
8011 | * an imbalance but busiest->nr_running <= 1, the group is | |
8012 | * still unbalanced. ld_moved simply stays zero, so it is | |
8013 | * correctly treated as an imbalance. | |
8014 | */ | |
8e45cb54 | 8015 | env.flags |= LBF_ALL_PINNED; |
c82513e5 | 8016 | env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running); |
8e45cb54 | 8017 | |
5d6523eb | 8018 | more_balance: |
163122b7 | 8019 | raw_spin_lock_irqsave(&busiest->lock, flags); |
88b8dac0 SV |
8020 | |
8021 | /* | |
8022 | * cur_ld_moved - load moved in current iteration | |
8023 | * ld_moved - cumulative load moved across iterations | |
8024 | */ | |
163122b7 | 8025 | cur_ld_moved = detach_tasks(&env); |
1e3c88bd PZ |
8026 | |
8027 | /* | |
163122b7 KT |
8028 | * We've detached some tasks from busiest_rq. Every |
8029 | * task is masked "TASK_ON_RQ_MIGRATING", so we can safely | |
8030 | * unlock busiest->lock, and we are able to be sure | |
8031 | * that nobody can manipulate the tasks in parallel. | |
8032 | * See task_rq_lock() family for the details. | |
1e3c88bd | 8033 | */ |
163122b7 KT |
8034 | |
8035 | raw_spin_unlock(&busiest->lock); | |
8036 | ||
8037 | if (cur_ld_moved) { | |
8038 | attach_tasks(&env); | |
8039 | ld_moved += cur_ld_moved; | |
8040 | } | |
8041 | ||
1e3c88bd | 8042 | local_irq_restore(flags); |
88b8dac0 | 8043 | |
f1cd0858 JK |
8044 | if (env.flags & LBF_NEED_BREAK) { |
8045 | env.flags &= ~LBF_NEED_BREAK; | |
8046 | goto more_balance; | |
8047 | } | |
8048 | ||
88b8dac0 SV |
8049 | /* |
8050 | * Revisit (affine) tasks on src_cpu that couldn't be moved to | |
8051 | * us and move them to an alternate dst_cpu in our sched_group | |
8052 | * where they can run. The upper limit on how many times we | |
8053 | * iterate on same src_cpu is dependent on number of cpus in our | |
8054 | * sched_group. | |
8055 | * | |
8056 | * This changes load balance semantics a bit on who can move | |
8057 | * load to a given_cpu. In addition to the given_cpu itself | |
8058 | * (or a ilb_cpu acting on its behalf where given_cpu is | |
8059 | * nohz-idle), we now have balance_cpu in a position to move | |
8060 | * load to given_cpu. In rare situations, this may cause | |
8061 | * conflicts (balance_cpu and given_cpu/ilb_cpu deciding | |
8062 | * _independently_ and at _same_ time to move some load to | |
8063 | * given_cpu) causing exceess load to be moved to given_cpu. | |
8064 | * This however should not happen so much in practice and | |
8065 | * moreover subsequent load balance cycles should correct the | |
8066 | * excess load moved. | |
8067 | */ | |
6263322c | 8068 | if ((env.flags & LBF_DST_PINNED) && env.imbalance > 0) { |
88b8dac0 | 8069 | |
7aff2e3a VD |
8070 | /* Prevent to re-select dst_cpu via env's cpus */ |
8071 | cpumask_clear_cpu(env.dst_cpu, env.cpus); | |
8072 | ||
78feefc5 | 8073 | env.dst_rq = cpu_rq(env.new_dst_cpu); |
88b8dac0 | 8074 | env.dst_cpu = env.new_dst_cpu; |
6263322c | 8075 | env.flags &= ~LBF_DST_PINNED; |
88b8dac0 SV |
8076 | env.loop = 0; |
8077 | env.loop_break = sched_nr_migrate_break; | |
e02e60c1 | 8078 | |
88b8dac0 SV |
8079 | /* |
8080 | * Go back to "more_balance" rather than "redo" since we | |
8081 | * need to continue with same src_cpu. | |
8082 | */ | |
8083 | goto more_balance; | |
8084 | } | |
1e3c88bd | 8085 | |
6263322c PZ |
8086 | /* |
8087 | * We failed to reach balance because of affinity. | |
8088 | */ | |
8089 | if (sd_parent) { | |
63b2ca30 | 8090 | int *group_imbalance = &sd_parent->groups->sgc->imbalance; |
6263322c | 8091 | |
afdeee05 | 8092 | if ((env.flags & LBF_SOME_PINNED) && env.imbalance > 0) |
6263322c | 8093 | *group_imbalance = 1; |
6263322c PZ |
8094 | } |
8095 | ||
1e3c88bd | 8096 | /* All tasks on this runqueue were pinned by CPU affinity */ |
8e45cb54 | 8097 | if (unlikely(env.flags & LBF_ALL_PINNED)) { |
1e3c88bd | 8098 | cpumask_clear_cpu(cpu_of(busiest), cpus); |
bbf18b19 PN |
8099 | if (!cpumask_empty(cpus)) { |
8100 | env.loop = 0; | |
8101 | env.loop_break = sched_nr_migrate_break; | |
1e3c88bd | 8102 | goto redo; |
bbf18b19 | 8103 | } |
afdeee05 | 8104 | goto out_all_pinned; |
1e3c88bd PZ |
8105 | } |
8106 | } | |
8107 | ||
8108 | if (!ld_moved) { | |
ae92882e | 8109 | schedstat_inc(sd->lb_failed[idle]); |
58b26c4c VP |
8110 | /* |
8111 | * Increment the failure counter only on periodic balance. | |
8112 | * We do not want newidle balance, which can be very | |
8113 | * frequent, pollute the failure counter causing | |
8114 | * excessive cache_hot migrations and active balances. | |
8115 | */ | |
8116 | if (idle != CPU_NEWLY_IDLE) | |
8117 | sd->nr_balance_failed++; | |
1e3c88bd | 8118 | |
bd939f45 | 8119 | if (need_active_balance(&env)) { |
1e3c88bd PZ |
8120 | raw_spin_lock_irqsave(&busiest->lock, flags); |
8121 | ||
969c7921 TH |
8122 | /* don't kick the active_load_balance_cpu_stop, |
8123 | * if the curr task on busiest cpu can't be | |
8124 | * moved to this_cpu | |
1e3c88bd PZ |
8125 | */ |
8126 | if (!cpumask_test_cpu(this_cpu, | |
fa17b507 | 8127 | tsk_cpus_allowed(busiest->curr))) { |
1e3c88bd PZ |
8128 | raw_spin_unlock_irqrestore(&busiest->lock, |
8129 | flags); | |
8e45cb54 | 8130 | env.flags |= LBF_ALL_PINNED; |
1e3c88bd PZ |
8131 | goto out_one_pinned; |
8132 | } | |
8133 | ||
969c7921 TH |
8134 | /* |
8135 | * ->active_balance synchronizes accesses to | |
8136 | * ->active_balance_work. Once set, it's cleared | |
8137 | * only after active load balance is finished. | |
8138 | */ | |
1e3c88bd PZ |
8139 | if (!busiest->active_balance) { |
8140 | busiest->active_balance = 1; | |
8141 | busiest->push_cpu = this_cpu; | |
8142 | active_balance = 1; | |
8143 | } | |
8144 | raw_spin_unlock_irqrestore(&busiest->lock, flags); | |
969c7921 | 8145 | |
bd939f45 | 8146 | if (active_balance) { |
969c7921 TH |
8147 | stop_one_cpu_nowait(cpu_of(busiest), |
8148 | active_load_balance_cpu_stop, busiest, | |
8149 | &busiest->active_balance_work); | |
bd939f45 | 8150 | } |
1e3c88bd | 8151 | |
d02c0711 | 8152 | /* We've kicked active balancing, force task migration. */ |
1e3c88bd PZ |
8153 | sd->nr_balance_failed = sd->cache_nice_tries+1; |
8154 | } | |
8155 | } else | |
8156 | sd->nr_balance_failed = 0; | |
8157 | ||
8158 | if (likely(!active_balance)) { | |
8159 | /* We were unbalanced, so reset the balancing interval */ | |
8160 | sd->balance_interval = sd->min_interval; | |
8161 | } else { | |
8162 | /* | |
8163 | * If we've begun active balancing, start to back off. This | |
8164 | * case may not be covered by the all_pinned logic if there | |
8165 | * is only 1 task on the busy runqueue (because we don't call | |
163122b7 | 8166 | * detach_tasks). |
1e3c88bd PZ |
8167 | */ |
8168 | if (sd->balance_interval < sd->max_interval) | |
8169 | sd->balance_interval *= 2; | |
8170 | } | |
8171 | ||
1e3c88bd PZ |
8172 | goto out; |
8173 | ||
8174 | out_balanced: | |
afdeee05 VG |
8175 | /* |
8176 | * We reach balance although we may have faced some affinity | |
8177 | * constraints. Clear the imbalance flag if it was set. | |
8178 | */ | |
8179 | if (sd_parent) { | |
8180 | int *group_imbalance = &sd_parent->groups->sgc->imbalance; | |
8181 | ||
8182 | if (*group_imbalance) | |
8183 | *group_imbalance = 0; | |
8184 | } | |
8185 | ||
8186 | out_all_pinned: | |
8187 | /* | |
8188 | * We reach balance because all tasks are pinned at this level so | |
8189 | * we can't migrate them. Let the imbalance flag set so parent level | |
8190 | * can try to migrate them. | |
8191 | */ | |
ae92882e | 8192 | schedstat_inc(sd->lb_balanced[idle]); |
1e3c88bd PZ |
8193 | |
8194 | sd->nr_balance_failed = 0; | |
8195 | ||
8196 | out_one_pinned: | |
8197 | /* tune up the balancing interval */ | |
8e45cb54 | 8198 | if (((env.flags & LBF_ALL_PINNED) && |
5b54b56b | 8199 | sd->balance_interval < MAX_PINNED_INTERVAL) || |
1e3c88bd PZ |
8200 | (sd->balance_interval < sd->max_interval)) |
8201 | sd->balance_interval *= 2; | |
8202 | ||
46e49b38 | 8203 | ld_moved = 0; |
1e3c88bd | 8204 | out: |
1e3c88bd PZ |
8205 | return ld_moved; |
8206 | } | |
8207 | ||
52a08ef1 JL |
8208 | static inline unsigned long |
8209 | get_sd_balance_interval(struct sched_domain *sd, int cpu_busy) | |
8210 | { | |
8211 | unsigned long interval = sd->balance_interval; | |
8212 | ||
8213 | if (cpu_busy) | |
8214 | interval *= sd->busy_factor; | |
8215 | ||
8216 | /* scale ms to jiffies */ | |
8217 | interval = msecs_to_jiffies(interval); | |
8218 | interval = clamp(interval, 1UL, max_load_balance_interval); | |
8219 | ||
8220 | return interval; | |
8221 | } | |
8222 | ||
8223 | static inline void | |
31851a98 | 8224 | update_next_balance(struct sched_domain *sd, unsigned long *next_balance) |
52a08ef1 JL |
8225 | { |
8226 | unsigned long interval, next; | |
8227 | ||
31851a98 LY |
8228 | /* used by idle balance, so cpu_busy = 0 */ |
8229 | interval = get_sd_balance_interval(sd, 0); | |
52a08ef1 JL |
8230 | next = sd->last_balance + interval; |
8231 | ||
8232 | if (time_after(*next_balance, next)) | |
8233 | *next_balance = next; | |
8234 | } | |
8235 | ||
1e3c88bd PZ |
8236 | /* |
8237 | * idle_balance is called by schedule() if this_cpu is about to become | |
8238 | * idle. Attempts to pull tasks from other CPUs. | |
8239 | */ | |
6e83125c | 8240 | static int idle_balance(struct rq *this_rq) |
1e3c88bd | 8241 | { |
52a08ef1 JL |
8242 | unsigned long next_balance = jiffies + HZ; |
8243 | int this_cpu = this_rq->cpu; | |
1e3c88bd PZ |
8244 | struct sched_domain *sd; |
8245 | int pulled_task = 0; | |
9bd721c5 | 8246 | u64 curr_cost = 0; |
1e3c88bd | 8247 | |
6e83125c PZ |
8248 | /* |
8249 | * We must set idle_stamp _before_ calling idle_balance(), such that we | |
8250 | * measure the duration of idle_balance() as idle time. | |
8251 | */ | |
8252 | this_rq->idle_stamp = rq_clock(this_rq); | |
8253 | ||
4486edd1 TC |
8254 | if (this_rq->avg_idle < sysctl_sched_migration_cost || |
8255 | !this_rq->rd->overload) { | |
52a08ef1 JL |
8256 | rcu_read_lock(); |
8257 | sd = rcu_dereference_check_sched_domain(this_rq->sd); | |
8258 | if (sd) | |
31851a98 | 8259 | update_next_balance(sd, &next_balance); |
52a08ef1 JL |
8260 | rcu_read_unlock(); |
8261 | ||
6e83125c | 8262 | goto out; |
52a08ef1 | 8263 | } |
1e3c88bd | 8264 | |
f492e12e PZ |
8265 | raw_spin_unlock(&this_rq->lock); |
8266 | ||
48a16753 | 8267 | update_blocked_averages(this_cpu); |
dce840a0 | 8268 | rcu_read_lock(); |
1e3c88bd | 8269 | for_each_domain(this_cpu, sd) { |
23f0d209 | 8270 | int continue_balancing = 1; |
9bd721c5 | 8271 | u64 t0, domain_cost; |
1e3c88bd PZ |
8272 | |
8273 | if (!(sd->flags & SD_LOAD_BALANCE)) | |
8274 | continue; | |
8275 | ||
52a08ef1 | 8276 | if (this_rq->avg_idle < curr_cost + sd->max_newidle_lb_cost) { |
31851a98 | 8277 | update_next_balance(sd, &next_balance); |
9bd721c5 | 8278 | break; |
52a08ef1 | 8279 | } |
9bd721c5 | 8280 | |
f492e12e | 8281 | if (sd->flags & SD_BALANCE_NEWIDLE) { |
9bd721c5 JL |
8282 | t0 = sched_clock_cpu(this_cpu); |
8283 | ||
f492e12e | 8284 | pulled_task = load_balance(this_cpu, this_rq, |
23f0d209 JK |
8285 | sd, CPU_NEWLY_IDLE, |
8286 | &continue_balancing); | |
9bd721c5 JL |
8287 | |
8288 | domain_cost = sched_clock_cpu(this_cpu) - t0; | |
8289 | if (domain_cost > sd->max_newidle_lb_cost) | |
8290 | sd->max_newidle_lb_cost = domain_cost; | |
8291 | ||
8292 | curr_cost += domain_cost; | |
f492e12e | 8293 | } |
1e3c88bd | 8294 | |
31851a98 | 8295 | update_next_balance(sd, &next_balance); |
39a4d9ca JL |
8296 | |
8297 | /* | |
8298 | * Stop searching for tasks to pull if there are | |
8299 | * now runnable tasks on this rq. | |
8300 | */ | |
8301 | if (pulled_task || this_rq->nr_running > 0) | |
1e3c88bd | 8302 | break; |
1e3c88bd | 8303 | } |
dce840a0 | 8304 | rcu_read_unlock(); |
f492e12e PZ |
8305 | |
8306 | raw_spin_lock(&this_rq->lock); | |
8307 | ||
0e5b5337 JL |
8308 | if (curr_cost > this_rq->max_idle_balance_cost) |
8309 | this_rq->max_idle_balance_cost = curr_cost; | |
8310 | ||
e5fc6611 | 8311 | /* |
0e5b5337 JL |
8312 | * While browsing the domains, we released the rq lock, a task could |
8313 | * have been enqueued in the meantime. Since we're not going idle, | |
8314 | * pretend we pulled a task. | |
e5fc6611 | 8315 | */ |
0e5b5337 | 8316 | if (this_rq->cfs.h_nr_running && !pulled_task) |
6e83125c | 8317 | pulled_task = 1; |
e5fc6611 | 8318 | |
52a08ef1 JL |
8319 | out: |
8320 | /* Move the next balance forward */ | |
8321 | if (time_after(this_rq->next_balance, next_balance)) | |
1e3c88bd | 8322 | this_rq->next_balance = next_balance; |
9bd721c5 | 8323 | |
e4aa358b | 8324 | /* Is there a task of a high priority class? */ |
46383648 | 8325 | if (this_rq->nr_running != this_rq->cfs.h_nr_running) |
e4aa358b KT |
8326 | pulled_task = -1; |
8327 | ||
38c6ade2 | 8328 | if (pulled_task) |
6e83125c PZ |
8329 | this_rq->idle_stamp = 0; |
8330 | ||
3c4017c1 | 8331 | return pulled_task; |
1e3c88bd PZ |
8332 | } |
8333 | ||
8334 | /* | |
969c7921 TH |
8335 | * active_load_balance_cpu_stop is run by cpu stopper. It pushes |
8336 | * running tasks off the busiest CPU onto idle CPUs. It requires at | |
8337 | * least 1 task to be running on each physical CPU where possible, and | |
8338 | * avoids physical / logical imbalances. | |
1e3c88bd | 8339 | */ |
969c7921 | 8340 | static int active_load_balance_cpu_stop(void *data) |
1e3c88bd | 8341 | { |
969c7921 TH |
8342 | struct rq *busiest_rq = data; |
8343 | int busiest_cpu = cpu_of(busiest_rq); | |
1e3c88bd | 8344 | int target_cpu = busiest_rq->push_cpu; |
969c7921 | 8345 | struct rq *target_rq = cpu_rq(target_cpu); |
1e3c88bd | 8346 | struct sched_domain *sd; |
e5673f28 | 8347 | struct task_struct *p = NULL; |
969c7921 TH |
8348 | |
8349 | raw_spin_lock_irq(&busiest_rq->lock); | |
8350 | ||
8351 | /* make sure the requested cpu hasn't gone down in the meantime */ | |
8352 | if (unlikely(busiest_cpu != smp_processor_id() || | |
8353 | !busiest_rq->active_balance)) | |
8354 | goto out_unlock; | |
1e3c88bd PZ |
8355 | |
8356 | /* Is there any task to move? */ | |
8357 | if (busiest_rq->nr_running <= 1) | |
969c7921 | 8358 | goto out_unlock; |
1e3c88bd PZ |
8359 | |
8360 | /* | |
8361 | * This condition is "impossible", if it occurs | |
8362 | * we need to fix it. Originally reported by | |
8363 | * Bjorn Helgaas on a 128-cpu setup. | |
8364 | */ | |
8365 | BUG_ON(busiest_rq == target_rq); | |
8366 | ||
1e3c88bd | 8367 | /* Search for an sd spanning us and the target CPU. */ |
dce840a0 | 8368 | rcu_read_lock(); |
1e3c88bd PZ |
8369 | for_each_domain(target_cpu, sd) { |
8370 | if ((sd->flags & SD_LOAD_BALANCE) && | |
8371 | cpumask_test_cpu(busiest_cpu, sched_domain_span(sd))) | |
8372 | break; | |
8373 | } | |
8374 | ||
8375 | if (likely(sd)) { | |
8e45cb54 PZ |
8376 | struct lb_env env = { |
8377 | .sd = sd, | |
ddcdf6e7 PZ |
8378 | .dst_cpu = target_cpu, |
8379 | .dst_rq = target_rq, | |
8380 | .src_cpu = busiest_rq->cpu, | |
8381 | .src_rq = busiest_rq, | |
8e45cb54 PZ |
8382 | .idle = CPU_IDLE, |
8383 | }; | |
8384 | ||
ae92882e | 8385 | schedstat_inc(sd->alb_count); |
1e3c88bd | 8386 | |
e5673f28 | 8387 | p = detach_one_task(&env); |
d02c0711 | 8388 | if (p) { |
ae92882e | 8389 | schedstat_inc(sd->alb_pushed); |
d02c0711 SD |
8390 | /* Active balancing done, reset the failure counter. */ |
8391 | sd->nr_balance_failed = 0; | |
8392 | } else { | |
ae92882e | 8393 | schedstat_inc(sd->alb_failed); |
d02c0711 | 8394 | } |
1e3c88bd | 8395 | } |
dce840a0 | 8396 | rcu_read_unlock(); |
969c7921 TH |
8397 | out_unlock: |
8398 | busiest_rq->active_balance = 0; | |
e5673f28 KT |
8399 | raw_spin_unlock(&busiest_rq->lock); |
8400 | ||
8401 | if (p) | |
8402 | attach_one_task(target_rq, p); | |
8403 | ||
8404 | local_irq_enable(); | |
8405 | ||
969c7921 | 8406 | return 0; |
1e3c88bd PZ |
8407 | } |
8408 | ||
d987fc7f MG |
8409 | static inline int on_null_domain(struct rq *rq) |
8410 | { | |
8411 | return unlikely(!rcu_dereference_sched(rq->sd)); | |
8412 | } | |
8413 | ||
3451d024 | 8414 | #ifdef CONFIG_NO_HZ_COMMON |
83cd4fe2 VP |
8415 | /* |
8416 | * idle load balancing details | |
83cd4fe2 VP |
8417 | * - When one of the busy CPUs notice that there may be an idle rebalancing |
8418 | * needed, they will kick the idle load balancer, which then does idle | |
8419 | * load balancing for all the idle CPUs. | |
8420 | */ | |
1e3c88bd | 8421 | static struct { |
83cd4fe2 | 8422 | cpumask_var_t idle_cpus_mask; |
0b005cf5 | 8423 | atomic_t nr_cpus; |
83cd4fe2 VP |
8424 | unsigned long next_balance; /* in jiffy units */ |
8425 | } nohz ____cacheline_aligned; | |
1e3c88bd | 8426 | |
3dd0337d | 8427 | static inline int find_new_ilb(void) |
1e3c88bd | 8428 | { |
0b005cf5 | 8429 | int ilb = cpumask_first(nohz.idle_cpus_mask); |
1e3c88bd | 8430 | |
786d6dc7 SS |
8431 | if (ilb < nr_cpu_ids && idle_cpu(ilb)) |
8432 | return ilb; | |
8433 | ||
8434 | return nr_cpu_ids; | |
1e3c88bd | 8435 | } |
1e3c88bd | 8436 | |
83cd4fe2 VP |
8437 | /* |
8438 | * Kick a CPU to do the nohz balancing, if it is time for it. We pick the | |
8439 | * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle | |
8440 | * CPU (if there is one). | |
8441 | */ | |
0aeeeeba | 8442 | static void nohz_balancer_kick(void) |
83cd4fe2 VP |
8443 | { |
8444 | int ilb_cpu; | |
8445 | ||
8446 | nohz.next_balance++; | |
8447 | ||
3dd0337d | 8448 | ilb_cpu = find_new_ilb(); |
83cd4fe2 | 8449 | |
0b005cf5 SS |
8450 | if (ilb_cpu >= nr_cpu_ids) |
8451 | return; | |
83cd4fe2 | 8452 | |
cd490c5b | 8453 | if (test_and_set_bit(NOHZ_BALANCE_KICK, nohz_flags(ilb_cpu))) |
1c792db7 SS |
8454 | return; |
8455 | /* | |
8456 | * Use smp_send_reschedule() instead of resched_cpu(). | |
8457 | * This way we generate a sched IPI on the target cpu which | |
8458 | * is idle. And the softirq performing nohz idle load balance | |
8459 | * will be run before returning from the IPI. | |
8460 | */ | |
8461 | smp_send_reschedule(ilb_cpu); | |
83cd4fe2 VP |
8462 | return; |
8463 | } | |
8464 | ||
20a5c8cc | 8465 | void nohz_balance_exit_idle(unsigned int cpu) |
71325960 SS |
8466 | { |
8467 | if (unlikely(test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))) { | |
d987fc7f MG |
8468 | /* |
8469 | * Completely isolated CPUs don't ever set, so we must test. | |
8470 | */ | |
8471 | if (likely(cpumask_test_cpu(cpu, nohz.idle_cpus_mask))) { | |
8472 | cpumask_clear_cpu(cpu, nohz.idle_cpus_mask); | |
8473 | atomic_dec(&nohz.nr_cpus); | |
8474 | } | |
71325960 SS |
8475 | clear_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)); |
8476 | } | |
8477 | } | |
8478 | ||
69e1e811 SS |
8479 | static inline void set_cpu_sd_state_busy(void) |
8480 | { | |
8481 | struct sched_domain *sd; | |
37dc6b50 | 8482 | int cpu = smp_processor_id(); |
69e1e811 | 8483 | |
69e1e811 | 8484 | rcu_read_lock(); |
0e369d75 | 8485 | sd = rcu_dereference(per_cpu(sd_llc, cpu)); |
25f55d9d VG |
8486 | |
8487 | if (!sd || !sd->nohz_idle) | |
8488 | goto unlock; | |
8489 | sd->nohz_idle = 0; | |
8490 | ||
0e369d75 | 8491 | atomic_inc(&sd->shared->nr_busy_cpus); |
25f55d9d | 8492 | unlock: |
69e1e811 SS |
8493 | rcu_read_unlock(); |
8494 | } | |
8495 | ||
8496 | void set_cpu_sd_state_idle(void) | |
8497 | { | |
8498 | struct sched_domain *sd; | |
37dc6b50 | 8499 | int cpu = smp_processor_id(); |
69e1e811 | 8500 | |
69e1e811 | 8501 | rcu_read_lock(); |
0e369d75 | 8502 | sd = rcu_dereference(per_cpu(sd_llc, cpu)); |
25f55d9d VG |
8503 | |
8504 | if (!sd || sd->nohz_idle) | |
8505 | goto unlock; | |
8506 | sd->nohz_idle = 1; | |
8507 | ||
0e369d75 | 8508 | atomic_dec(&sd->shared->nr_busy_cpus); |
25f55d9d | 8509 | unlock: |
69e1e811 SS |
8510 | rcu_read_unlock(); |
8511 | } | |
8512 | ||
1e3c88bd | 8513 | /* |
c1cc017c | 8514 | * This routine will record that the cpu is going idle with tick stopped. |
0b005cf5 | 8515 | * This info will be used in performing idle load balancing in the future. |
1e3c88bd | 8516 | */ |
c1cc017c | 8517 | void nohz_balance_enter_idle(int cpu) |
1e3c88bd | 8518 | { |
71325960 SS |
8519 | /* |
8520 | * If this cpu is going down, then nothing needs to be done. | |
8521 | */ | |
8522 | if (!cpu_active(cpu)) | |
8523 | return; | |
8524 | ||
c1cc017c AS |
8525 | if (test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu))) |
8526 | return; | |
1e3c88bd | 8527 | |
d987fc7f MG |
8528 | /* |
8529 | * If we're a completely isolated CPU, we don't play. | |
8530 | */ | |
8531 | if (on_null_domain(cpu_rq(cpu))) | |
8532 | return; | |
8533 | ||
c1cc017c AS |
8534 | cpumask_set_cpu(cpu, nohz.idle_cpus_mask); |
8535 | atomic_inc(&nohz.nr_cpus); | |
8536 | set_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)); | |
1e3c88bd PZ |
8537 | } |
8538 | #endif | |
8539 | ||
8540 | static DEFINE_SPINLOCK(balancing); | |
8541 | ||
49c022e6 PZ |
8542 | /* |
8543 | * Scale the max load_balance interval with the number of CPUs in the system. | |
8544 | * This trades load-balance latency on larger machines for less cross talk. | |
8545 | */ | |
029632fb | 8546 | void update_max_interval(void) |
49c022e6 PZ |
8547 | { |
8548 | max_load_balance_interval = HZ*num_online_cpus()/10; | |
8549 | } | |
8550 | ||
1e3c88bd PZ |
8551 | /* |
8552 | * It checks each scheduling domain to see if it is due to be balanced, | |
8553 | * and initiates a balancing operation if so. | |
8554 | * | |
b9b0853a | 8555 | * Balancing parameters are set up in init_sched_domains. |
1e3c88bd | 8556 | */ |
f7ed0a89 | 8557 | static void rebalance_domains(struct rq *rq, enum cpu_idle_type idle) |
1e3c88bd | 8558 | { |
23f0d209 | 8559 | int continue_balancing = 1; |
f7ed0a89 | 8560 | int cpu = rq->cpu; |
1e3c88bd | 8561 | unsigned long interval; |
04f733b4 | 8562 | struct sched_domain *sd; |
1e3c88bd PZ |
8563 | /* Earliest time when we have to do rebalance again */ |
8564 | unsigned long next_balance = jiffies + 60*HZ; | |
8565 | int update_next_balance = 0; | |
f48627e6 JL |
8566 | int need_serialize, need_decay = 0; |
8567 | u64 max_cost = 0; | |
1e3c88bd | 8568 | |
48a16753 | 8569 | update_blocked_averages(cpu); |
2069dd75 | 8570 | |
dce840a0 | 8571 | rcu_read_lock(); |
1e3c88bd | 8572 | for_each_domain(cpu, sd) { |
f48627e6 JL |
8573 | /* |
8574 | * Decay the newidle max times here because this is a regular | |
8575 | * visit to all the domains. Decay ~1% per second. | |
8576 | */ | |
8577 | if (time_after(jiffies, sd->next_decay_max_lb_cost)) { | |
8578 | sd->max_newidle_lb_cost = | |
8579 | (sd->max_newidle_lb_cost * 253) / 256; | |
8580 | sd->next_decay_max_lb_cost = jiffies + HZ; | |
8581 | need_decay = 1; | |
8582 | } | |
8583 | max_cost += sd->max_newidle_lb_cost; | |
8584 | ||
1e3c88bd PZ |
8585 | if (!(sd->flags & SD_LOAD_BALANCE)) |
8586 | continue; | |
8587 | ||
f48627e6 JL |
8588 | /* |
8589 | * Stop the load balance at this level. There is another | |
8590 | * CPU in our sched group which is doing load balancing more | |
8591 | * actively. | |
8592 | */ | |
8593 | if (!continue_balancing) { | |
8594 | if (need_decay) | |
8595 | continue; | |
8596 | break; | |
8597 | } | |
8598 | ||
52a08ef1 | 8599 | interval = get_sd_balance_interval(sd, idle != CPU_IDLE); |
1e3c88bd PZ |
8600 | |
8601 | need_serialize = sd->flags & SD_SERIALIZE; | |
1e3c88bd PZ |
8602 | if (need_serialize) { |
8603 | if (!spin_trylock(&balancing)) | |
8604 | goto out; | |
8605 | } | |
8606 | ||
8607 | if (time_after_eq(jiffies, sd->last_balance + interval)) { | |
23f0d209 | 8608 | if (load_balance(cpu, rq, sd, idle, &continue_balancing)) { |
1e3c88bd | 8609 | /* |
6263322c | 8610 | * The LBF_DST_PINNED logic could have changed |
de5eb2dd JK |
8611 | * env->dst_cpu, so we can't know our idle |
8612 | * state even if we migrated tasks. Update it. | |
1e3c88bd | 8613 | */ |
de5eb2dd | 8614 | idle = idle_cpu(cpu) ? CPU_IDLE : CPU_NOT_IDLE; |
1e3c88bd PZ |
8615 | } |
8616 | sd->last_balance = jiffies; | |
52a08ef1 | 8617 | interval = get_sd_balance_interval(sd, idle != CPU_IDLE); |
1e3c88bd PZ |
8618 | } |
8619 | if (need_serialize) | |
8620 | spin_unlock(&balancing); | |
8621 | out: | |
8622 | if (time_after(next_balance, sd->last_balance + interval)) { | |
8623 | next_balance = sd->last_balance + interval; | |
8624 | update_next_balance = 1; | |
8625 | } | |
f48627e6 JL |
8626 | } |
8627 | if (need_decay) { | |
1e3c88bd | 8628 | /* |
f48627e6 JL |
8629 | * Ensure the rq-wide value also decays but keep it at a |
8630 | * reasonable floor to avoid funnies with rq->avg_idle. | |
1e3c88bd | 8631 | */ |
f48627e6 JL |
8632 | rq->max_idle_balance_cost = |
8633 | max((u64)sysctl_sched_migration_cost, max_cost); | |
1e3c88bd | 8634 | } |
dce840a0 | 8635 | rcu_read_unlock(); |
1e3c88bd PZ |
8636 | |
8637 | /* | |
8638 | * next_balance will be updated only when there is a need. | |
8639 | * When the cpu is attached to null domain for ex, it will not be | |
8640 | * updated. | |
8641 | */ | |
c5afb6a8 | 8642 | if (likely(update_next_balance)) { |
1e3c88bd | 8643 | rq->next_balance = next_balance; |
c5afb6a8 VG |
8644 | |
8645 | #ifdef CONFIG_NO_HZ_COMMON | |
8646 | /* | |
8647 | * If this CPU has been elected to perform the nohz idle | |
8648 | * balance. Other idle CPUs have already rebalanced with | |
8649 | * nohz_idle_balance() and nohz.next_balance has been | |
8650 | * updated accordingly. This CPU is now running the idle load | |
8651 | * balance for itself and we need to update the | |
8652 | * nohz.next_balance accordingly. | |
8653 | */ | |
8654 | if ((idle == CPU_IDLE) && time_after(nohz.next_balance, rq->next_balance)) | |
8655 | nohz.next_balance = rq->next_balance; | |
8656 | #endif | |
8657 | } | |
1e3c88bd PZ |
8658 | } |
8659 | ||
3451d024 | 8660 | #ifdef CONFIG_NO_HZ_COMMON |
1e3c88bd | 8661 | /* |
3451d024 | 8662 | * In CONFIG_NO_HZ_COMMON case, the idle balance kickee will do the |
1e3c88bd PZ |
8663 | * rebalancing for all the cpus for whom scheduler ticks are stopped. |
8664 | */ | |
208cb16b | 8665 | static void nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle) |
83cd4fe2 | 8666 | { |
208cb16b | 8667 | int this_cpu = this_rq->cpu; |
83cd4fe2 VP |
8668 | struct rq *rq; |
8669 | int balance_cpu; | |
c5afb6a8 VG |
8670 | /* Earliest time when we have to do rebalance again */ |
8671 | unsigned long next_balance = jiffies + 60*HZ; | |
8672 | int update_next_balance = 0; | |
83cd4fe2 | 8673 | |
1c792db7 SS |
8674 | if (idle != CPU_IDLE || |
8675 | !test_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu))) | |
8676 | goto end; | |
83cd4fe2 VP |
8677 | |
8678 | for_each_cpu(balance_cpu, nohz.idle_cpus_mask) { | |
8a6d42d1 | 8679 | if (balance_cpu == this_cpu || !idle_cpu(balance_cpu)) |
83cd4fe2 VP |
8680 | continue; |
8681 | ||
8682 | /* | |
8683 | * If this cpu gets work to do, stop the load balancing | |
8684 | * work being done for other cpus. Next load | |
8685 | * balancing owner will pick it up. | |
8686 | */ | |
1c792db7 | 8687 | if (need_resched()) |
83cd4fe2 | 8688 | break; |
83cd4fe2 | 8689 | |
5ed4f1d9 VG |
8690 | rq = cpu_rq(balance_cpu); |
8691 | ||
ed61bbc6 TC |
8692 | /* |
8693 | * If time for next balance is due, | |
8694 | * do the balance. | |
8695 | */ | |
8696 | if (time_after_eq(jiffies, rq->next_balance)) { | |
8697 | raw_spin_lock_irq(&rq->lock); | |
8698 | update_rq_clock(rq); | |
cee1afce | 8699 | cpu_load_update_idle(rq); |
ed61bbc6 TC |
8700 | raw_spin_unlock_irq(&rq->lock); |
8701 | rebalance_domains(rq, CPU_IDLE); | |
8702 | } | |
83cd4fe2 | 8703 | |
c5afb6a8 VG |
8704 | if (time_after(next_balance, rq->next_balance)) { |
8705 | next_balance = rq->next_balance; | |
8706 | update_next_balance = 1; | |
8707 | } | |
83cd4fe2 | 8708 | } |
c5afb6a8 VG |
8709 | |
8710 | /* | |
8711 | * next_balance will be updated only when there is a need. | |
8712 | * When the CPU is attached to null domain for ex, it will not be | |
8713 | * updated. | |
8714 | */ | |
8715 | if (likely(update_next_balance)) | |
8716 | nohz.next_balance = next_balance; | |
1c792db7 SS |
8717 | end: |
8718 | clear_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu)); | |
83cd4fe2 VP |
8719 | } |
8720 | ||
8721 | /* | |
0b005cf5 | 8722 | * Current heuristic for kicking the idle load balancer in the presence |
1aaf90a4 | 8723 | * of an idle cpu in the system. |
0b005cf5 | 8724 | * - This rq has more than one task. |
1aaf90a4 VG |
8725 | * - This rq has at least one CFS task and the capacity of the CPU is |
8726 | * significantly reduced because of RT tasks or IRQs. | |
8727 | * - At parent of LLC scheduler domain level, this cpu's scheduler group has | |
8728 | * multiple busy cpu. | |
0b005cf5 SS |
8729 | * - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler |
8730 | * domain span are idle. | |
83cd4fe2 | 8731 | */ |
1aaf90a4 | 8732 | static inline bool nohz_kick_needed(struct rq *rq) |
83cd4fe2 VP |
8733 | { |
8734 | unsigned long now = jiffies; | |
0e369d75 | 8735 | struct sched_domain_shared *sds; |
0b005cf5 | 8736 | struct sched_domain *sd; |
4a725627 | 8737 | int nr_busy, cpu = rq->cpu; |
1aaf90a4 | 8738 | bool kick = false; |
83cd4fe2 | 8739 | |
4a725627 | 8740 | if (unlikely(rq->idle_balance)) |
1aaf90a4 | 8741 | return false; |
83cd4fe2 | 8742 | |
1c792db7 SS |
8743 | /* |
8744 | * We may be recently in ticked or tickless idle mode. At the first | |
8745 | * busy tick after returning from idle, we will update the busy stats. | |
8746 | */ | |
69e1e811 | 8747 | set_cpu_sd_state_busy(); |
c1cc017c | 8748 | nohz_balance_exit_idle(cpu); |
0b005cf5 SS |
8749 | |
8750 | /* | |
8751 | * None are in tickless mode and hence no need for NOHZ idle load | |
8752 | * balancing. | |
8753 | */ | |
8754 | if (likely(!atomic_read(&nohz.nr_cpus))) | |
1aaf90a4 | 8755 | return false; |
1c792db7 SS |
8756 | |
8757 | if (time_before(now, nohz.next_balance)) | |
1aaf90a4 | 8758 | return false; |
83cd4fe2 | 8759 | |
0b005cf5 | 8760 | if (rq->nr_running >= 2) |
1aaf90a4 | 8761 | return true; |
83cd4fe2 | 8762 | |
067491b7 | 8763 | rcu_read_lock(); |
0e369d75 PZ |
8764 | sds = rcu_dereference(per_cpu(sd_llc_shared, cpu)); |
8765 | if (sds) { | |
8766 | /* | |
8767 | * XXX: write a coherent comment on why we do this. | |
8768 | * See also: http://lkml.kernel.org/r/20111202010832.602203411@sbsiddha-desk.sc.intel.com | |
8769 | */ | |
8770 | nr_busy = atomic_read(&sds->nr_busy_cpus); | |
1aaf90a4 VG |
8771 | if (nr_busy > 1) { |
8772 | kick = true; | |
8773 | goto unlock; | |
8774 | } | |
8775 | ||
83cd4fe2 | 8776 | } |
37dc6b50 | 8777 | |
1aaf90a4 VG |
8778 | sd = rcu_dereference(rq->sd); |
8779 | if (sd) { | |
8780 | if ((rq->cfs.h_nr_running >= 1) && | |
8781 | check_cpu_capacity(rq, sd)) { | |
8782 | kick = true; | |
8783 | goto unlock; | |
8784 | } | |
8785 | } | |
37dc6b50 | 8786 | |
1aaf90a4 | 8787 | sd = rcu_dereference(per_cpu(sd_asym, cpu)); |
37dc6b50 | 8788 | if (sd && (cpumask_first_and(nohz.idle_cpus_mask, |
1aaf90a4 VG |
8789 | sched_domain_span(sd)) < cpu)) { |
8790 | kick = true; | |
8791 | goto unlock; | |
8792 | } | |
067491b7 | 8793 | |
1aaf90a4 | 8794 | unlock: |
067491b7 | 8795 | rcu_read_unlock(); |
1aaf90a4 | 8796 | return kick; |
83cd4fe2 VP |
8797 | } |
8798 | #else | |
208cb16b | 8799 | static void nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle) { } |
83cd4fe2 VP |
8800 | #endif |
8801 | ||
8802 | /* | |
8803 | * run_rebalance_domains is triggered when needed from the scheduler tick. | |
8804 | * Also triggered for nohz idle balancing (with nohz_balancing_kick set). | |
8805 | */ | |
0766f788 | 8806 | static __latent_entropy void run_rebalance_domains(struct softirq_action *h) |
1e3c88bd | 8807 | { |
208cb16b | 8808 | struct rq *this_rq = this_rq(); |
6eb57e0d | 8809 | enum cpu_idle_type idle = this_rq->idle_balance ? |
1e3c88bd PZ |
8810 | CPU_IDLE : CPU_NOT_IDLE; |
8811 | ||
1e3c88bd | 8812 | /* |
83cd4fe2 | 8813 | * If this cpu has a pending nohz_balance_kick, then do the |
1e3c88bd | 8814 | * balancing on behalf of the other idle cpus whose ticks are |
d4573c3e PM |
8815 | * stopped. Do nohz_idle_balance *before* rebalance_domains to |
8816 | * give the idle cpus a chance to load balance. Else we may | |
8817 | * load balance only within the local sched_domain hierarchy | |
8818 | * and abort nohz_idle_balance altogether if we pull some load. | |
1e3c88bd | 8819 | */ |
208cb16b | 8820 | nohz_idle_balance(this_rq, idle); |
d4573c3e | 8821 | rebalance_domains(this_rq, idle); |
1e3c88bd PZ |
8822 | } |
8823 | ||
1e3c88bd PZ |
8824 | /* |
8825 | * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing. | |
1e3c88bd | 8826 | */ |
7caff66f | 8827 | void trigger_load_balance(struct rq *rq) |
1e3c88bd | 8828 | { |
1e3c88bd | 8829 | /* Don't need to rebalance while attached to NULL domain */ |
c726099e DL |
8830 | if (unlikely(on_null_domain(rq))) |
8831 | return; | |
8832 | ||
8833 | if (time_after_eq(jiffies, rq->next_balance)) | |
1e3c88bd | 8834 | raise_softirq(SCHED_SOFTIRQ); |
3451d024 | 8835 | #ifdef CONFIG_NO_HZ_COMMON |
c726099e | 8836 | if (nohz_kick_needed(rq)) |
0aeeeeba | 8837 | nohz_balancer_kick(); |
83cd4fe2 | 8838 | #endif |
1e3c88bd PZ |
8839 | } |
8840 | ||
0bcdcf28 CE |
8841 | static void rq_online_fair(struct rq *rq) |
8842 | { | |
8843 | update_sysctl(); | |
0e59bdae KT |
8844 | |
8845 | update_runtime_enabled(rq); | |
0bcdcf28 CE |
8846 | } |
8847 | ||
8848 | static void rq_offline_fair(struct rq *rq) | |
8849 | { | |
8850 | update_sysctl(); | |
a4c96ae3 PB |
8851 | |
8852 | /* Ensure any throttled groups are reachable by pick_next_task */ | |
8853 | unthrottle_offline_cfs_rqs(rq); | |
0bcdcf28 CE |
8854 | } |
8855 | ||
55e12e5e | 8856 | #endif /* CONFIG_SMP */ |
e1d1484f | 8857 | |
bf0f6f24 IM |
8858 | /* |
8859 | * scheduler tick hitting a task of our scheduling class: | |
8860 | */ | |
8f4d37ec | 8861 | static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued) |
bf0f6f24 IM |
8862 | { |
8863 | struct cfs_rq *cfs_rq; | |
8864 | struct sched_entity *se = &curr->se; | |
8865 | ||
8866 | for_each_sched_entity(se) { | |
8867 | cfs_rq = cfs_rq_of(se); | |
8f4d37ec | 8868 | entity_tick(cfs_rq, se, queued); |
bf0f6f24 | 8869 | } |
18bf2805 | 8870 | |
b52da86e | 8871 | if (static_branch_unlikely(&sched_numa_balancing)) |
cbee9f88 | 8872 | task_tick_numa(rq, curr); |
bf0f6f24 IM |
8873 | } |
8874 | ||
8875 | /* | |
cd29fe6f PZ |
8876 | * called on fork with the child task as argument from the parent's context |
8877 | * - child not yet on the tasklist | |
8878 | * - preemption disabled | |
bf0f6f24 | 8879 | */ |
cd29fe6f | 8880 | static void task_fork_fair(struct task_struct *p) |
bf0f6f24 | 8881 | { |
4fc420c9 DN |
8882 | struct cfs_rq *cfs_rq; |
8883 | struct sched_entity *se = &p->se, *curr; | |
cd29fe6f | 8884 | struct rq *rq = this_rq(); |
bf0f6f24 | 8885 | |
e210bffd | 8886 | raw_spin_lock(&rq->lock); |
861d034e PZ |
8887 | update_rq_clock(rq); |
8888 | ||
4fc420c9 DN |
8889 | cfs_rq = task_cfs_rq(current); |
8890 | curr = cfs_rq->curr; | |
e210bffd PZ |
8891 | if (curr) { |
8892 | update_curr(cfs_rq); | |
b5d9d734 | 8893 | se->vruntime = curr->vruntime; |
e210bffd | 8894 | } |
aeb73b04 | 8895 | place_entity(cfs_rq, se, 1); |
4d78e7b6 | 8896 | |
cd29fe6f | 8897 | if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) { |
87fefa38 | 8898 | /* |
edcb60a3 IM |
8899 | * Upon rescheduling, sched_class::put_prev_task() will place |
8900 | * 'current' within the tree based on its new key value. | |
8901 | */ | |
4d78e7b6 | 8902 | swap(curr->vruntime, se->vruntime); |
8875125e | 8903 | resched_curr(rq); |
4d78e7b6 | 8904 | } |
bf0f6f24 | 8905 | |
88ec22d3 | 8906 | se->vruntime -= cfs_rq->min_vruntime; |
e210bffd | 8907 | raw_spin_unlock(&rq->lock); |
bf0f6f24 IM |
8908 | } |
8909 | ||
cb469845 SR |
8910 | /* |
8911 | * Priority of the task has changed. Check to see if we preempt | |
8912 | * the current task. | |
8913 | */ | |
da7a735e PZ |
8914 | static void |
8915 | prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio) | |
cb469845 | 8916 | { |
da0c1e65 | 8917 | if (!task_on_rq_queued(p)) |
da7a735e PZ |
8918 | return; |
8919 | ||
cb469845 SR |
8920 | /* |
8921 | * Reschedule if we are currently running on this runqueue and | |
8922 | * our priority decreased, or if we are not currently running on | |
8923 | * this runqueue and our priority is higher than the current's | |
8924 | */ | |
da7a735e | 8925 | if (rq->curr == p) { |
cb469845 | 8926 | if (p->prio > oldprio) |
8875125e | 8927 | resched_curr(rq); |
cb469845 | 8928 | } else |
15afe09b | 8929 | check_preempt_curr(rq, p, 0); |
cb469845 SR |
8930 | } |
8931 | ||
daa59407 | 8932 | static inline bool vruntime_normalized(struct task_struct *p) |
da7a735e PZ |
8933 | { |
8934 | struct sched_entity *se = &p->se; | |
da7a735e PZ |
8935 | |
8936 | /* | |
daa59407 BP |
8937 | * In both the TASK_ON_RQ_QUEUED and TASK_ON_RQ_MIGRATING cases, |
8938 | * the dequeue_entity(.flags=0) will already have normalized the | |
8939 | * vruntime. | |
8940 | */ | |
8941 | if (p->on_rq) | |
8942 | return true; | |
8943 | ||
8944 | /* | |
8945 | * When !on_rq, vruntime of the task has usually NOT been normalized. | |
8946 | * But there are some cases where it has already been normalized: | |
da7a735e | 8947 | * |
daa59407 BP |
8948 | * - A forked child which is waiting for being woken up by |
8949 | * wake_up_new_task(). | |
8950 | * - A task which has been woken up by try_to_wake_up() and | |
8951 | * waiting for actually being woken up by sched_ttwu_pending(). | |
da7a735e | 8952 | */ |
daa59407 BP |
8953 | if (!se->sum_exec_runtime || p->state == TASK_WAKING) |
8954 | return true; | |
8955 | ||
8956 | return false; | |
8957 | } | |
8958 | ||
09a43ace VG |
8959 | #ifdef CONFIG_FAIR_GROUP_SCHED |
8960 | /* | |
8961 | * Propagate the changes of the sched_entity across the tg tree to make it | |
8962 | * visible to the root | |
8963 | */ | |
8964 | static void propagate_entity_cfs_rq(struct sched_entity *se) | |
8965 | { | |
8966 | struct cfs_rq *cfs_rq; | |
8967 | ||
8968 | /* Start to propagate at parent */ | |
8969 | se = se->parent; | |
8970 | ||
8971 | for_each_sched_entity(se) { | |
8972 | cfs_rq = cfs_rq_of(se); | |
8973 | ||
8974 | if (cfs_rq_throttled(cfs_rq)) | |
8975 | break; | |
8976 | ||
8977 | update_load_avg(se, UPDATE_TG); | |
8978 | } | |
8979 | } | |
8980 | #else | |
8981 | static void propagate_entity_cfs_rq(struct sched_entity *se) { } | |
8982 | #endif | |
8983 | ||
df217913 | 8984 | static void detach_entity_cfs_rq(struct sched_entity *se) |
daa59407 | 8985 | { |
daa59407 BP |
8986 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
8987 | ||
9d89c257 | 8988 | /* Catch up with the cfs_rq and remove our load when we leave */ |
d31b1a66 | 8989 | update_load_avg(se, 0); |
a05e8c51 | 8990 | detach_entity_load_avg(cfs_rq, se); |
7c3edd2c | 8991 | update_tg_load_avg(cfs_rq, false); |
09a43ace | 8992 | propagate_entity_cfs_rq(se); |
da7a735e PZ |
8993 | } |
8994 | ||
df217913 | 8995 | static void attach_entity_cfs_rq(struct sched_entity *se) |
cb469845 | 8996 | { |
daa59407 | 8997 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
7855a35a BP |
8998 | |
8999 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
eb7a59b2 M |
9000 | /* |
9001 | * Since the real-depth could have been changed (only FAIR | |
9002 | * class maintain depth value), reset depth properly. | |
9003 | */ | |
9004 | se->depth = se->parent ? se->parent->depth + 1 : 0; | |
9005 | #endif | |
7855a35a | 9006 | |
df217913 | 9007 | /* Synchronize entity with its cfs_rq */ |
d31b1a66 | 9008 | update_load_avg(se, sched_feat(ATTACH_AGE_LOAD) ? 0 : SKIP_AGE_LOAD); |
daa59407 | 9009 | attach_entity_load_avg(cfs_rq, se); |
7c3edd2c | 9010 | update_tg_load_avg(cfs_rq, false); |
09a43ace | 9011 | propagate_entity_cfs_rq(se); |
df217913 VG |
9012 | } |
9013 | ||
9014 | static void detach_task_cfs_rq(struct task_struct *p) | |
9015 | { | |
9016 | struct sched_entity *se = &p->se; | |
9017 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
9018 | ||
9019 | if (!vruntime_normalized(p)) { | |
9020 | /* | |
9021 | * Fix up our vruntime so that the current sleep doesn't | |
9022 | * cause 'unlimited' sleep bonus. | |
9023 | */ | |
9024 | place_entity(cfs_rq, se, 0); | |
9025 | se->vruntime -= cfs_rq->min_vruntime; | |
9026 | } | |
9027 | ||
9028 | detach_entity_cfs_rq(se); | |
9029 | } | |
9030 | ||
9031 | static void attach_task_cfs_rq(struct task_struct *p) | |
9032 | { | |
9033 | struct sched_entity *se = &p->se; | |
9034 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
9035 | ||
9036 | attach_entity_cfs_rq(se); | |
daa59407 BP |
9037 | |
9038 | if (!vruntime_normalized(p)) | |
9039 | se->vruntime += cfs_rq->min_vruntime; | |
9040 | } | |
6efdb105 | 9041 | |
daa59407 BP |
9042 | static void switched_from_fair(struct rq *rq, struct task_struct *p) |
9043 | { | |
9044 | detach_task_cfs_rq(p); | |
9045 | } | |
9046 | ||
9047 | static void switched_to_fair(struct rq *rq, struct task_struct *p) | |
9048 | { | |
9049 | attach_task_cfs_rq(p); | |
7855a35a | 9050 | |
daa59407 | 9051 | if (task_on_rq_queued(p)) { |
7855a35a | 9052 | /* |
daa59407 BP |
9053 | * We were most likely switched from sched_rt, so |
9054 | * kick off the schedule if running, otherwise just see | |
9055 | * if we can still preempt the current task. | |
7855a35a | 9056 | */ |
daa59407 BP |
9057 | if (rq->curr == p) |
9058 | resched_curr(rq); | |
9059 | else | |
9060 | check_preempt_curr(rq, p, 0); | |
7855a35a | 9061 | } |
cb469845 SR |
9062 | } |
9063 | ||
83b699ed SV |
9064 | /* Account for a task changing its policy or group. |
9065 | * | |
9066 | * This routine is mostly called to set cfs_rq->curr field when a task | |
9067 | * migrates between groups/classes. | |
9068 | */ | |
9069 | static void set_curr_task_fair(struct rq *rq) | |
9070 | { | |
9071 | struct sched_entity *se = &rq->curr->se; | |
9072 | ||
ec12cb7f PT |
9073 | for_each_sched_entity(se) { |
9074 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
9075 | ||
9076 | set_next_entity(cfs_rq, se); | |
9077 | /* ensure bandwidth has been allocated on our new cfs_rq */ | |
9078 | account_cfs_rq_runtime(cfs_rq, 0); | |
9079 | } | |
83b699ed SV |
9080 | } |
9081 | ||
029632fb PZ |
9082 | void init_cfs_rq(struct cfs_rq *cfs_rq) |
9083 | { | |
9084 | cfs_rq->tasks_timeline = RB_ROOT; | |
029632fb PZ |
9085 | cfs_rq->min_vruntime = (u64)(-(1LL << 20)); |
9086 | #ifndef CONFIG_64BIT | |
9087 | cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime; | |
9088 | #endif | |
141965c7 | 9089 | #ifdef CONFIG_SMP |
09a43ace VG |
9090 | #ifdef CONFIG_FAIR_GROUP_SCHED |
9091 | cfs_rq->propagate_avg = 0; | |
9092 | #endif | |
9d89c257 YD |
9093 | atomic_long_set(&cfs_rq->removed_load_avg, 0); |
9094 | atomic_long_set(&cfs_rq->removed_util_avg, 0); | |
9ee474f5 | 9095 | #endif |
029632fb PZ |
9096 | } |
9097 | ||
810b3817 | 9098 | #ifdef CONFIG_FAIR_GROUP_SCHED |
ea86cb4b VG |
9099 | static void task_set_group_fair(struct task_struct *p) |
9100 | { | |
9101 | struct sched_entity *se = &p->se; | |
9102 | ||
9103 | set_task_rq(p, task_cpu(p)); | |
9104 | se->depth = se->parent ? se->parent->depth + 1 : 0; | |
9105 | } | |
9106 | ||
bc54da21 | 9107 | static void task_move_group_fair(struct task_struct *p) |
810b3817 | 9108 | { |
daa59407 | 9109 | detach_task_cfs_rq(p); |
b2b5ce02 | 9110 | set_task_rq(p, task_cpu(p)); |
6efdb105 BP |
9111 | |
9112 | #ifdef CONFIG_SMP | |
9113 | /* Tell se's cfs_rq has been changed -- migrated */ | |
9114 | p->se.avg.last_update_time = 0; | |
9115 | #endif | |
daa59407 | 9116 | attach_task_cfs_rq(p); |
810b3817 | 9117 | } |
029632fb | 9118 | |
ea86cb4b VG |
9119 | static void task_change_group_fair(struct task_struct *p, int type) |
9120 | { | |
9121 | switch (type) { | |
9122 | case TASK_SET_GROUP: | |
9123 | task_set_group_fair(p); | |
9124 | break; | |
9125 | ||
9126 | case TASK_MOVE_GROUP: | |
9127 | task_move_group_fair(p); | |
9128 | break; | |
9129 | } | |
9130 | } | |
9131 | ||
029632fb PZ |
9132 | void free_fair_sched_group(struct task_group *tg) |
9133 | { | |
9134 | int i; | |
9135 | ||
9136 | destroy_cfs_bandwidth(tg_cfs_bandwidth(tg)); | |
9137 | ||
9138 | for_each_possible_cpu(i) { | |
9139 | if (tg->cfs_rq) | |
9140 | kfree(tg->cfs_rq[i]); | |
6fe1f348 | 9141 | if (tg->se) |
029632fb PZ |
9142 | kfree(tg->se[i]); |
9143 | } | |
9144 | ||
9145 | kfree(tg->cfs_rq); | |
9146 | kfree(tg->se); | |
9147 | } | |
9148 | ||
9149 | int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent) | |
9150 | { | |
029632fb | 9151 | struct sched_entity *se; |
b7fa30c9 | 9152 | struct cfs_rq *cfs_rq; |
029632fb PZ |
9153 | int i; |
9154 | ||
9155 | tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL); | |
9156 | if (!tg->cfs_rq) | |
9157 | goto err; | |
9158 | tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL); | |
9159 | if (!tg->se) | |
9160 | goto err; | |
9161 | ||
9162 | tg->shares = NICE_0_LOAD; | |
9163 | ||
9164 | init_cfs_bandwidth(tg_cfs_bandwidth(tg)); | |
9165 | ||
9166 | for_each_possible_cpu(i) { | |
9167 | cfs_rq = kzalloc_node(sizeof(struct cfs_rq), | |
9168 | GFP_KERNEL, cpu_to_node(i)); | |
9169 | if (!cfs_rq) | |
9170 | goto err; | |
9171 | ||
9172 | se = kzalloc_node(sizeof(struct sched_entity), | |
9173 | GFP_KERNEL, cpu_to_node(i)); | |
9174 | if (!se) | |
9175 | goto err_free_rq; | |
9176 | ||
9177 | init_cfs_rq(cfs_rq); | |
9178 | init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]); | |
540247fb | 9179 | init_entity_runnable_average(se); |
029632fb PZ |
9180 | } |
9181 | ||
9182 | return 1; | |
9183 | ||
9184 | err_free_rq: | |
9185 | kfree(cfs_rq); | |
9186 | err: | |
9187 | return 0; | |
9188 | } | |
9189 | ||
8663e24d PZ |
9190 | void online_fair_sched_group(struct task_group *tg) |
9191 | { | |
9192 | struct sched_entity *se; | |
9193 | struct rq *rq; | |
9194 | int i; | |
9195 | ||
9196 | for_each_possible_cpu(i) { | |
9197 | rq = cpu_rq(i); | |
9198 | se = tg->se[i]; | |
9199 | ||
9200 | raw_spin_lock_irq(&rq->lock); | |
d0326691 | 9201 | attach_entity_cfs_rq(se); |
55e16d30 | 9202 | sync_throttle(tg, i); |
8663e24d PZ |
9203 | raw_spin_unlock_irq(&rq->lock); |
9204 | } | |
9205 | } | |
9206 | ||
6fe1f348 | 9207 | void unregister_fair_sched_group(struct task_group *tg) |
029632fb | 9208 | { |
029632fb | 9209 | unsigned long flags; |
6fe1f348 PZ |
9210 | struct rq *rq; |
9211 | int cpu; | |
029632fb | 9212 | |
6fe1f348 PZ |
9213 | for_each_possible_cpu(cpu) { |
9214 | if (tg->se[cpu]) | |
9215 | remove_entity_load_avg(tg->se[cpu]); | |
029632fb | 9216 | |
6fe1f348 PZ |
9217 | /* |
9218 | * Only empty task groups can be destroyed; so we can speculatively | |
9219 | * check on_list without danger of it being re-added. | |
9220 | */ | |
9221 | if (!tg->cfs_rq[cpu]->on_list) | |
9222 | continue; | |
9223 | ||
9224 | rq = cpu_rq(cpu); | |
9225 | ||
9226 | raw_spin_lock_irqsave(&rq->lock, flags); | |
9227 | list_del_leaf_cfs_rq(tg->cfs_rq[cpu]); | |
9228 | raw_spin_unlock_irqrestore(&rq->lock, flags); | |
9229 | } | |
029632fb PZ |
9230 | } |
9231 | ||
9232 | void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq, | |
9233 | struct sched_entity *se, int cpu, | |
9234 | struct sched_entity *parent) | |
9235 | { | |
9236 | struct rq *rq = cpu_rq(cpu); | |
9237 | ||
9238 | cfs_rq->tg = tg; | |
9239 | cfs_rq->rq = rq; | |
029632fb PZ |
9240 | init_cfs_rq_runtime(cfs_rq); |
9241 | ||
9242 | tg->cfs_rq[cpu] = cfs_rq; | |
9243 | tg->se[cpu] = se; | |
9244 | ||
9245 | /* se could be NULL for root_task_group */ | |
9246 | if (!se) | |
9247 | return; | |
9248 | ||
fed14d45 | 9249 | if (!parent) { |
029632fb | 9250 | se->cfs_rq = &rq->cfs; |
fed14d45 PZ |
9251 | se->depth = 0; |
9252 | } else { | |
029632fb | 9253 | se->cfs_rq = parent->my_q; |
fed14d45 PZ |
9254 | se->depth = parent->depth + 1; |
9255 | } | |
029632fb PZ |
9256 | |
9257 | se->my_q = cfs_rq; | |
0ac9b1c2 PT |
9258 | /* guarantee group entities always have weight */ |
9259 | update_load_set(&se->load, NICE_0_LOAD); | |
029632fb PZ |
9260 | se->parent = parent; |
9261 | } | |
9262 | ||
9263 | static DEFINE_MUTEX(shares_mutex); | |
9264 | ||
9265 | int sched_group_set_shares(struct task_group *tg, unsigned long shares) | |
9266 | { | |
9267 | int i; | |
9268 | unsigned long flags; | |
9269 | ||
9270 | /* | |
9271 | * We can't change the weight of the root cgroup. | |
9272 | */ | |
9273 | if (!tg->se[0]) | |
9274 | return -EINVAL; | |
9275 | ||
9276 | shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES)); | |
9277 | ||
9278 | mutex_lock(&shares_mutex); | |
9279 | if (tg->shares == shares) | |
9280 | goto done; | |
9281 | ||
9282 | tg->shares = shares; | |
9283 | for_each_possible_cpu(i) { | |
9284 | struct rq *rq = cpu_rq(i); | |
9285 | struct sched_entity *se; | |
9286 | ||
9287 | se = tg->se[i]; | |
9288 | /* Propagate contribution to hierarchy */ | |
9289 | raw_spin_lock_irqsave(&rq->lock, flags); | |
71b1da46 FW |
9290 | |
9291 | /* Possible calls to update_curr() need rq clock */ | |
9292 | update_rq_clock(rq); | |
17bc14b7 | 9293 | for_each_sched_entity(se) |
029632fb PZ |
9294 | update_cfs_shares(group_cfs_rq(se)); |
9295 | raw_spin_unlock_irqrestore(&rq->lock, flags); | |
9296 | } | |
9297 | ||
9298 | done: | |
9299 | mutex_unlock(&shares_mutex); | |
9300 | return 0; | |
9301 | } | |
9302 | #else /* CONFIG_FAIR_GROUP_SCHED */ | |
9303 | ||
9304 | void free_fair_sched_group(struct task_group *tg) { } | |
9305 | ||
9306 | int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent) | |
9307 | { | |
9308 | return 1; | |
9309 | } | |
9310 | ||
8663e24d PZ |
9311 | void online_fair_sched_group(struct task_group *tg) { } |
9312 | ||
6fe1f348 | 9313 | void unregister_fair_sched_group(struct task_group *tg) { } |
029632fb PZ |
9314 | |
9315 | #endif /* CONFIG_FAIR_GROUP_SCHED */ | |
9316 | ||
810b3817 | 9317 | |
6d686f45 | 9318 | static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task) |
0d721cea PW |
9319 | { |
9320 | struct sched_entity *se = &task->se; | |
0d721cea PW |
9321 | unsigned int rr_interval = 0; |
9322 | ||
9323 | /* | |
9324 | * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise | |
9325 | * idle runqueue: | |
9326 | */ | |
0d721cea | 9327 | if (rq->cfs.load.weight) |
a59f4e07 | 9328 | rr_interval = NS_TO_JIFFIES(sched_slice(cfs_rq_of(se), se)); |
0d721cea PW |
9329 | |
9330 | return rr_interval; | |
9331 | } | |
9332 | ||
bf0f6f24 IM |
9333 | /* |
9334 | * All the scheduling class methods: | |
9335 | */ | |
029632fb | 9336 | const struct sched_class fair_sched_class = { |
5522d5d5 | 9337 | .next = &idle_sched_class, |
bf0f6f24 IM |
9338 | .enqueue_task = enqueue_task_fair, |
9339 | .dequeue_task = dequeue_task_fair, | |
9340 | .yield_task = yield_task_fair, | |
d95f4122 | 9341 | .yield_to_task = yield_to_task_fair, |
bf0f6f24 | 9342 | |
2e09bf55 | 9343 | .check_preempt_curr = check_preempt_wakeup, |
bf0f6f24 IM |
9344 | |
9345 | .pick_next_task = pick_next_task_fair, | |
9346 | .put_prev_task = put_prev_task_fair, | |
9347 | ||
681f3e68 | 9348 | #ifdef CONFIG_SMP |
4ce72a2c | 9349 | .select_task_rq = select_task_rq_fair, |
0a74bef8 | 9350 | .migrate_task_rq = migrate_task_rq_fair, |
141965c7 | 9351 | |
0bcdcf28 CE |
9352 | .rq_online = rq_online_fair, |
9353 | .rq_offline = rq_offline_fair, | |
88ec22d3 | 9354 | |
12695578 | 9355 | .task_dead = task_dead_fair, |
c5b28038 | 9356 | .set_cpus_allowed = set_cpus_allowed_common, |
681f3e68 | 9357 | #endif |
bf0f6f24 | 9358 | |
83b699ed | 9359 | .set_curr_task = set_curr_task_fair, |
bf0f6f24 | 9360 | .task_tick = task_tick_fair, |
cd29fe6f | 9361 | .task_fork = task_fork_fair, |
cb469845 SR |
9362 | |
9363 | .prio_changed = prio_changed_fair, | |
da7a735e | 9364 | .switched_from = switched_from_fair, |
cb469845 | 9365 | .switched_to = switched_to_fair, |
810b3817 | 9366 | |
0d721cea PW |
9367 | .get_rr_interval = get_rr_interval_fair, |
9368 | ||
6e998916 SG |
9369 | .update_curr = update_curr_fair, |
9370 | ||
810b3817 | 9371 | #ifdef CONFIG_FAIR_GROUP_SCHED |
ea86cb4b | 9372 | .task_change_group = task_change_group_fair, |
810b3817 | 9373 | #endif |
bf0f6f24 IM |
9374 | }; |
9375 | ||
9376 | #ifdef CONFIG_SCHED_DEBUG | |
029632fb | 9377 | void print_cfs_stats(struct seq_file *m, int cpu) |
bf0f6f24 | 9378 | { |
bf0f6f24 IM |
9379 | struct cfs_rq *cfs_rq; |
9380 | ||
5973e5b9 | 9381 | rcu_read_lock(); |
c3b64f1e | 9382 | for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq) |
5cef9eca | 9383 | print_cfs_rq(m, cpu, cfs_rq); |
5973e5b9 | 9384 | rcu_read_unlock(); |
bf0f6f24 | 9385 | } |
397f2378 SD |
9386 | |
9387 | #ifdef CONFIG_NUMA_BALANCING | |
9388 | void show_numa_stats(struct task_struct *p, struct seq_file *m) | |
9389 | { | |
9390 | int node; | |
9391 | unsigned long tsf = 0, tpf = 0, gsf = 0, gpf = 0; | |
9392 | ||
9393 | for_each_online_node(node) { | |
9394 | if (p->numa_faults) { | |
9395 | tsf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 0)]; | |
9396 | tpf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 1)]; | |
9397 | } | |
9398 | if (p->numa_group) { | |
9399 | gsf = p->numa_group->faults[task_faults_idx(NUMA_MEM, node, 0)], | |
9400 | gpf = p->numa_group->faults[task_faults_idx(NUMA_MEM, node, 1)]; | |
9401 | } | |
9402 | print_numa_stats(m, node, tsf, tpf, gsf, gpf); | |
9403 | } | |
9404 | } | |
9405 | #endif /* CONFIG_NUMA_BALANCING */ | |
9406 | #endif /* CONFIG_SCHED_DEBUG */ | |
029632fb PZ |
9407 | |
9408 | __init void init_sched_fair_class(void) | |
9409 | { | |
9410 | #ifdef CONFIG_SMP | |
9411 | open_softirq(SCHED_SOFTIRQ, run_rebalance_domains); | |
9412 | ||
3451d024 | 9413 | #ifdef CONFIG_NO_HZ_COMMON |
554cecaf | 9414 | nohz.next_balance = jiffies; |
029632fb | 9415 | zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT); |
029632fb PZ |
9416 | #endif |
9417 | #endif /* SMP */ | |
9418 | ||
9419 | } |