]>
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 | ||
9745512c | 23 | #include <linux/latencytop.h> |
1983a922 | 24 | #include <linux/sched.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 | ||
a7a4f8a7 PT |
96 | /* |
97 | * The exponential sliding window over which load is averaged for shares | |
98 | * distribution. | |
99 | * (default: 10msec) | |
100 | */ | |
101 | unsigned int __read_mostly sysctl_sched_shares_window = 10000000UL; | |
102 | ||
ec12cb7f PT |
103 | #ifdef CONFIG_CFS_BANDWIDTH |
104 | /* | |
105 | * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool | |
106 | * each time a cfs_rq requests quota. | |
107 | * | |
108 | * Note: in the case that the slice exceeds the runtime remaining (either due | |
109 | * to consumption or the quota being specified to be smaller than the slice) | |
110 | * we will always only issue the remaining available time. | |
111 | * | |
112 | * default: 5 msec, units: microseconds | |
113 | */ | |
114 | unsigned int sysctl_sched_cfs_bandwidth_slice = 5000UL; | |
115 | #endif | |
116 | ||
8527632d PG |
117 | static inline void update_load_add(struct load_weight *lw, unsigned long inc) |
118 | { | |
119 | lw->weight += inc; | |
120 | lw->inv_weight = 0; | |
121 | } | |
122 | ||
123 | static inline void update_load_sub(struct load_weight *lw, unsigned long dec) | |
124 | { | |
125 | lw->weight -= dec; | |
126 | lw->inv_weight = 0; | |
127 | } | |
128 | ||
129 | static inline void update_load_set(struct load_weight *lw, unsigned long w) | |
130 | { | |
131 | lw->weight = w; | |
132 | lw->inv_weight = 0; | |
133 | } | |
134 | ||
029632fb PZ |
135 | /* |
136 | * Increase the granularity value when there are more CPUs, | |
137 | * because with more CPUs the 'effective latency' as visible | |
138 | * to users decreases. But the relationship is not linear, | |
139 | * so pick a second-best guess by going with the log2 of the | |
140 | * number of CPUs. | |
141 | * | |
142 | * This idea comes from the SD scheduler of Con Kolivas: | |
143 | */ | |
58ac93e4 | 144 | static unsigned int get_update_sysctl_factor(void) |
029632fb | 145 | { |
58ac93e4 | 146 | unsigned int cpus = min_t(unsigned int, num_online_cpus(), 8); |
029632fb PZ |
147 | unsigned int factor; |
148 | ||
149 | switch (sysctl_sched_tunable_scaling) { | |
150 | case SCHED_TUNABLESCALING_NONE: | |
151 | factor = 1; | |
152 | break; | |
153 | case SCHED_TUNABLESCALING_LINEAR: | |
154 | factor = cpus; | |
155 | break; | |
156 | case SCHED_TUNABLESCALING_LOG: | |
157 | default: | |
158 | factor = 1 + ilog2(cpus); | |
159 | break; | |
160 | } | |
161 | ||
162 | return factor; | |
163 | } | |
164 | ||
165 | static void update_sysctl(void) | |
166 | { | |
167 | unsigned int factor = get_update_sysctl_factor(); | |
168 | ||
169 | #define SET_SYSCTL(name) \ | |
170 | (sysctl_##name = (factor) * normalized_sysctl_##name) | |
171 | SET_SYSCTL(sched_min_granularity); | |
172 | SET_SYSCTL(sched_latency); | |
173 | SET_SYSCTL(sched_wakeup_granularity); | |
174 | #undef SET_SYSCTL | |
175 | } | |
176 | ||
177 | void sched_init_granularity(void) | |
178 | { | |
179 | update_sysctl(); | |
180 | } | |
181 | ||
9dbdb155 | 182 | #define WMULT_CONST (~0U) |
029632fb PZ |
183 | #define WMULT_SHIFT 32 |
184 | ||
9dbdb155 PZ |
185 | static void __update_inv_weight(struct load_weight *lw) |
186 | { | |
187 | unsigned long w; | |
188 | ||
189 | if (likely(lw->inv_weight)) | |
190 | return; | |
191 | ||
192 | w = scale_load_down(lw->weight); | |
193 | ||
194 | if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST)) | |
195 | lw->inv_weight = 1; | |
196 | else if (unlikely(!w)) | |
197 | lw->inv_weight = WMULT_CONST; | |
198 | else | |
199 | lw->inv_weight = WMULT_CONST / w; | |
200 | } | |
029632fb PZ |
201 | |
202 | /* | |
9dbdb155 PZ |
203 | * delta_exec * weight / lw.weight |
204 | * OR | |
205 | * (delta_exec * (weight * lw->inv_weight)) >> WMULT_SHIFT | |
206 | * | |
207 | * Either weight := NICE_0_LOAD and lw \e prio_to_wmult[], in which case | |
208 | * we're guaranteed shift stays positive because inv_weight is guaranteed to | |
209 | * fit 32 bits, and NICE_0_LOAD gives another 10 bits; therefore shift >= 22. | |
210 | * | |
211 | * Or, weight =< lw.weight (because lw.weight is the runqueue weight), thus | |
212 | * weight/lw.weight <= 1, and therefore our shift will also be positive. | |
029632fb | 213 | */ |
9dbdb155 | 214 | static u64 __calc_delta(u64 delta_exec, unsigned long weight, struct load_weight *lw) |
029632fb | 215 | { |
9dbdb155 PZ |
216 | u64 fact = scale_load_down(weight); |
217 | int shift = WMULT_SHIFT; | |
029632fb | 218 | |
9dbdb155 | 219 | __update_inv_weight(lw); |
029632fb | 220 | |
9dbdb155 PZ |
221 | if (unlikely(fact >> 32)) { |
222 | while (fact >> 32) { | |
223 | fact >>= 1; | |
224 | shift--; | |
225 | } | |
029632fb PZ |
226 | } |
227 | ||
9dbdb155 PZ |
228 | /* hint to use a 32x32->64 mul */ |
229 | fact = (u64)(u32)fact * lw->inv_weight; | |
029632fb | 230 | |
9dbdb155 PZ |
231 | while (fact >> 32) { |
232 | fact >>= 1; | |
233 | shift--; | |
234 | } | |
029632fb | 235 | |
9dbdb155 | 236 | return mul_u64_u32_shr(delta_exec, fact, shift); |
029632fb PZ |
237 | } |
238 | ||
239 | ||
240 | const struct sched_class fair_sched_class; | |
a4c2f00f | 241 | |
bf0f6f24 IM |
242 | /************************************************************** |
243 | * CFS operations on generic schedulable entities: | |
244 | */ | |
245 | ||
62160e3f | 246 | #ifdef CONFIG_FAIR_GROUP_SCHED |
bf0f6f24 | 247 | |
62160e3f | 248 | /* cpu runqueue to which this cfs_rq is attached */ |
bf0f6f24 IM |
249 | static inline struct rq *rq_of(struct cfs_rq *cfs_rq) |
250 | { | |
62160e3f | 251 | return cfs_rq->rq; |
bf0f6f24 IM |
252 | } |
253 | ||
62160e3f IM |
254 | /* An entity is a task if it doesn't "own" a runqueue */ |
255 | #define entity_is_task(se) (!se->my_q) | |
bf0f6f24 | 256 | |
8f48894f PZ |
257 | static inline struct task_struct *task_of(struct sched_entity *se) |
258 | { | |
259 | #ifdef CONFIG_SCHED_DEBUG | |
260 | WARN_ON_ONCE(!entity_is_task(se)); | |
261 | #endif | |
262 | return container_of(se, struct task_struct, se); | |
263 | } | |
264 | ||
b758149c PZ |
265 | /* Walk up scheduling entities hierarchy */ |
266 | #define for_each_sched_entity(se) \ | |
267 | for (; se; se = se->parent) | |
268 | ||
269 | static inline struct cfs_rq *task_cfs_rq(struct task_struct *p) | |
270 | { | |
271 | return p->se.cfs_rq; | |
272 | } | |
273 | ||
274 | /* runqueue on which this entity is (to be) queued */ | |
275 | static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se) | |
276 | { | |
277 | return se->cfs_rq; | |
278 | } | |
279 | ||
280 | /* runqueue "owned" by this group */ | |
281 | static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp) | |
282 | { | |
283 | return grp->my_q; | |
284 | } | |
285 | ||
3d4b47b4 PZ |
286 | static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq) |
287 | { | |
288 | if (!cfs_rq->on_list) { | |
67e86250 PT |
289 | /* |
290 | * Ensure we either appear before our parent (if already | |
291 | * enqueued) or force our parent to appear after us when it is | |
292 | * enqueued. The fact that we always enqueue bottom-up | |
293 | * reduces this to two cases. | |
294 | */ | |
295 | if (cfs_rq->tg->parent && | |
296 | cfs_rq->tg->parent->cfs_rq[cpu_of(rq_of(cfs_rq))]->on_list) { | |
297 | list_add_rcu(&cfs_rq->leaf_cfs_rq_list, | |
298 | &rq_of(cfs_rq)->leaf_cfs_rq_list); | |
299 | } else { | |
300 | list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list, | |
3d4b47b4 | 301 | &rq_of(cfs_rq)->leaf_cfs_rq_list); |
67e86250 | 302 | } |
3d4b47b4 PZ |
303 | |
304 | cfs_rq->on_list = 1; | |
305 | } | |
306 | } | |
307 | ||
308 | static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq) | |
309 | { | |
310 | if (cfs_rq->on_list) { | |
311 | list_del_rcu(&cfs_rq->leaf_cfs_rq_list); | |
312 | cfs_rq->on_list = 0; | |
313 | } | |
314 | } | |
315 | ||
b758149c PZ |
316 | /* Iterate thr' all leaf cfs_rq's on a runqueue */ |
317 | #define for_each_leaf_cfs_rq(rq, cfs_rq) \ | |
318 | list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list) | |
319 | ||
320 | /* Do the two (enqueued) entities belong to the same group ? */ | |
fed14d45 | 321 | static inline struct cfs_rq * |
b758149c PZ |
322 | is_same_group(struct sched_entity *se, struct sched_entity *pse) |
323 | { | |
324 | if (se->cfs_rq == pse->cfs_rq) | |
fed14d45 | 325 | return se->cfs_rq; |
b758149c | 326 | |
fed14d45 | 327 | return NULL; |
b758149c PZ |
328 | } |
329 | ||
330 | static inline struct sched_entity *parent_entity(struct sched_entity *se) | |
331 | { | |
332 | return se->parent; | |
333 | } | |
334 | ||
464b7527 PZ |
335 | static void |
336 | find_matching_se(struct sched_entity **se, struct sched_entity **pse) | |
337 | { | |
338 | int se_depth, pse_depth; | |
339 | ||
340 | /* | |
341 | * preemption test can be made between sibling entities who are in the | |
342 | * same cfs_rq i.e who have a common parent. Walk up the hierarchy of | |
343 | * both tasks until we find their ancestors who are siblings of common | |
344 | * parent. | |
345 | */ | |
346 | ||
347 | /* First walk up until both entities are at same depth */ | |
fed14d45 PZ |
348 | se_depth = (*se)->depth; |
349 | pse_depth = (*pse)->depth; | |
464b7527 PZ |
350 | |
351 | while (se_depth > pse_depth) { | |
352 | se_depth--; | |
353 | *se = parent_entity(*se); | |
354 | } | |
355 | ||
356 | while (pse_depth > se_depth) { | |
357 | pse_depth--; | |
358 | *pse = parent_entity(*pse); | |
359 | } | |
360 | ||
361 | while (!is_same_group(*se, *pse)) { | |
362 | *se = parent_entity(*se); | |
363 | *pse = parent_entity(*pse); | |
364 | } | |
365 | } | |
366 | ||
8f48894f PZ |
367 | #else /* !CONFIG_FAIR_GROUP_SCHED */ |
368 | ||
369 | static inline struct task_struct *task_of(struct sched_entity *se) | |
370 | { | |
371 | return container_of(se, struct task_struct, se); | |
372 | } | |
bf0f6f24 | 373 | |
62160e3f IM |
374 | static inline struct rq *rq_of(struct cfs_rq *cfs_rq) |
375 | { | |
376 | return container_of(cfs_rq, struct rq, cfs); | |
bf0f6f24 IM |
377 | } |
378 | ||
379 | #define entity_is_task(se) 1 | |
380 | ||
b758149c PZ |
381 | #define for_each_sched_entity(se) \ |
382 | for (; se; se = NULL) | |
bf0f6f24 | 383 | |
b758149c | 384 | static inline struct cfs_rq *task_cfs_rq(struct task_struct *p) |
bf0f6f24 | 385 | { |
b758149c | 386 | return &task_rq(p)->cfs; |
bf0f6f24 IM |
387 | } |
388 | ||
b758149c PZ |
389 | static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se) |
390 | { | |
391 | struct task_struct *p = task_of(se); | |
392 | struct rq *rq = task_rq(p); | |
393 | ||
394 | return &rq->cfs; | |
395 | } | |
396 | ||
397 | /* runqueue "owned" by this group */ | |
398 | static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp) | |
399 | { | |
400 | return NULL; | |
401 | } | |
402 | ||
3d4b47b4 PZ |
403 | static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq) |
404 | { | |
405 | } | |
406 | ||
407 | static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq) | |
408 | { | |
409 | } | |
410 | ||
b758149c PZ |
411 | #define for_each_leaf_cfs_rq(rq, cfs_rq) \ |
412 | for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL) | |
413 | ||
b758149c PZ |
414 | static inline struct sched_entity *parent_entity(struct sched_entity *se) |
415 | { | |
416 | return NULL; | |
417 | } | |
418 | ||
464b7527 PZ |
419 | static inline void |
420 | find_matching_se(struct sched_entity **se, struct sched_entity **pse) | |
421 | { | |
422 | } | |
423 | ||
b758149c PZ |
424 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
425 | ||
6c16a6dc | 426 | static __always_inline |
9dbdb155 | 427 | void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec); |
bf0f6f24 IM |
428 | |
429 | /************************************************************** | |
430 | * Scheduling class tree data structure manipulation methods: | |
431 | */ | |
432 | ||
1bf08230 | 433 | static inline u64 max_vruntime(u64 max_vruntime, u64 vruntime) |
02e0431a | 434 | { |
1bf08230 | 435 | s64 delta = (s64)(vruntime - max_vruntime); |
368059a9 | 436 | if (delta > 0) |
1bf08230 | 437 | max_vruntime = vruntime; |
02e0431a | 438 | |
1bf08230 | 439 | return max_vruntime; |
02e0431a PZ |
440 | } |
441 | ||
0702e3eb | 442 | static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime) |
b0ffd246 PZ |
443 | { |
444 | s64 delta = (s64)(vruntime - min_vruntime); | |
445 | if (delta < 0) | |
446 | min_vruntime = vruntime; | |
447 | ||
448 | return min_vruntime; | |
449 | } | |
450 | ||
54fdc581 FC |
451 | static inline int entity_before(struct sched_entity *a, |
452 | struct sched_entity *b) | |
453 | { | |
454 | return (s64)(a->vruntime - b->vruntime) < 0; | |
455 | } | |
456 | ||
1af5f730 PZ |
457 | static void update_min_vruntime(struct cfs_rq *cfs_rq) |
458 | { | |
459 | u64 vruntime = cfs_rq->min_vruntime; | |
460 | ||
461 | if (cfs_rq->curr) | |
462 | vruntime = cfs_rq->curr->vruntime; | |
463 | ||
464 | if (cfs_rq->rb_leftmost) { | |
465 | struct sched_entity *se = rb_entry(cfs_rq->rb_leftmost, | |
466 | struct sched_entity, | |
467 | run_node); | |
468 | ||
e17036da | 469 | if (!cfs_rq->curr) |
1af5f730 PZ |
470 | vruntime = se->vruntime; |
471 | else | |
472 | vruntime = min_vruntime(vruntime, se->vruntime); | |
473 | } | |
474 | ||
1bf08230 | 475 | /* ensure we never gain time by being placed backwards. */ |
1af5f730 | 476 | cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime); |
3fe1698b PZ |
477 | #ifndef CONFIG_64BIT |
478 | smp_wmb(); | |
479 | cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime; | |
480 | #endif | |
1af5f730 PZ |
481 | } |
482 | ||
bf0f6f24 IM |
483 | /* |
484 | * Enqueue an entity into the rb-tree: | |
485 | */ | |
0702e3eb | 486 | static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 IM |
487 | { |
488 | struct rb_node **link = &cfs_rq->tasks_timeline.rb_node; | |
489 | struct rb_node *parent = NULL; | |
490 | struct sched_entity *entry; | |
bf0f6f24 IM |
491 | int leftmost = 1; |
492 | ||
493 | /* | |
494 | * Find the right place in the rbtree: | |
495 | */ | |
496 | while (*link) { | |
497 | parent = *link; | |
498 | entry = rb_entry(parent, struct sched_entity, run_node); | |
499 | /* | |
500 | * We dont care about collisions. Nodes with | |
501 | * the same key stay together. | |
502 | */ | |
2bd2d6f2 | 503 | if (entity_before(se, entry)) { |
bf0f6f24 IM |
504 | link = &parent->rb_left; |
505 | } else { | |
506 | link = &parent->rb_right; | |
507 | leftmost = 0; | |
508 | } | |
509 | } | |
510 | ||
511 | /* | |
512 | * Maintain a cache of leftmost tree entries (it is frequently | |
513 | * used): | |
514 | */ | |
1af5f730 | 515 | if (leftmost) |
57cb499d | 516 | cfs_rq->rb_leftmost = &se->run_node; |
bf0f6f24 IM |
517 | |
518 | rb_link_node(&se->run_node, parent, link); | |
519 | rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline); | |
bf0f6f24 IM |
520 | } |
521 | ||
0702e3eb | 522 | static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 | 523 | { |
3fe69747 PZ |
524 | if (cfs_rq->rb_leftmost == &se->run_node) { |
525 | struct rb_node *next_node; | |
3fe69747 PZ |
526 | |
527 | next_node = rb_next(&se->run_node); | |
528 | cfs_rq->rb_leftmost = next_node; | |
3fe69747 | 529 | } |
e9acbff6 | 530 | |
bf0f6f24 | 531 | rb_erase(&se->run_node, &cfs_rq->tasks_timeline); |
bf0f6f24 IM |
532 | } |
533 | ||
029632fb | 534 | struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq) |
bf0f6f24 | 535 | { |
f4b6755f PZ |
536 | struct rb_node *left = cfs_rq->rb_leftmost; |
537 | ||
538 | if (!left) | |
539 | return NULL; | |
540 | ||
541 | return rb_entry(left, struct sched_entity, run_node); | |
bf0f6f24 IM |
542 | } |
543 | ||
ac53db59 RR |
544 | static struct sched_entity *__pick_next_entity(struct sched_entity *se) |
545 | { | |
546 | struct rb_node *next = rb_next(&se->run_node); | |
547 | ||
548 | if (!next) | |
549 | return NULL; | |
550 | ||
551 | return rb_entry(next, struct sched_entity, run_node); | |
552 | } | |
553 | ||
554 | #ifdef CONFIG_SCHED_DEBUG | |
029632fb | 555 | struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq) |
aeb73b04 | 556 | { |
7eee3e67 | 557 | struct rb_node *last = rb_last(&cfs_rq->tasks_timeline); |
aeb73b04 | 558 | |
70eee74b BS |
559 | if (!last) |
560 | return NULL; | |
7eee3e67 IM |
561 | |
562 | return rb_entry(last, struct sched_entity, run_node); | |
aeb73b04 PZ |
563 | } |
564 | ||
bf0f6f24 IM |
565 | /************************************************************** |
566 | * Scheduling class statistics methods: | |
567 | */ | |
568 | ||
acb4a848 | 569 | int sched_proc_update_handler(struct ctl_table *table, int write, |
8d65af78 | 570 | void __user *buffer, size_t *lenp, |
b2be5e96 PZ |
571 | loff_t *ppos) |
572 | { | |
8d65af78 | 573 | int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
58ac93e4 | 574 | unsigned int factor = get_update_sysctl_factor(); |
b2be5e96 PZ |
575 | |
576 | if (ret || !write) | |
577 | return ret; | |
578 | ||
579 | sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency, | |
580 | sysctl_sched_min_granularity); | |
581 | ||
acb4a848 CE |
582 | #define WRT_SYSCTL(name) \ |
583 | (normalized_sysctl_##name = sysctl_##name / (factor)) | |
584 | WRT_SYSCTL(sched_min_granularity); | |
585 | WRT_SYSCTL(sched_latency); | |
586 | WRT_SYSCTL(sched_wakeup_granularity); | |
acb4a848 CE |
587 | #undef WRT_SYSCTL |
588 | ||
b2be5e96 PZ |
589 | return 0; |
590 | } | |
591 | #endif | |
647e7cac | 592 | |
a7be37ac | 593 | /* |
f9c0b095 | 594 | * delta /= w |
a7be37ac | 595 | */ |
9dbdb155 | 596 | static inline u64 calc_delta_fair(u64 delta, struct sched_entity *se) |
a7be37ac | 597 | { |
f9c0b095 | 598 | if (unlikely(se->load.weight != NICE_0_LOAD)) |
9dbdb155 | 599 | delta = __calc_delta(delta, NICE_0_LOAD, &se->load); |
a7be37ac PZ |
600 | |
601 | return delta; | |
602 | } | |
603 | ||
647e7cac IM |
604 | /* |
605 | * The idea is to set a period in which each task runs once. | |
606 | * | |
532b1858 | 607 | * When there are too many tasks (sched_nr_latency) we have to stretch |
647e7cac IM |
608 | * this period because otherwise the slices get too small. |
609 | * | |
610 | * p = (nr <= nl) ? l : l*nr/nl | |
611 | */ | |
4d78e7b6 PZ |
612 | static u64 __sched_period(unsigned long nr_running) |
613 | { | |
8e2b0bf3 BF |
614 | if (unlikely(nr_running > sched_nr_latency)) |
615 | return nr_running * sysctl_sched_min_granularity; | |
616 | else | |
617 | return sysctl_sched_latency; | |
4d78e7b6 PZ |
618 | } |
619 | ||
647e7cac IM |
620 | /* |
621 | * We calculate the wall-time slice from the period by taking a part | |
622 | * proportional to the weight. | |
623 | * | |
f9c0b095 | 624 | * s = p*P[w/rw] |
647e7cac | 625 | */ |
6d0f0ebd | 626 | static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se) |
21805085 | 627 | { |
0a582440 | 628 | u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq); |
f9c0b095 | 629 | |
0a582440 | 630 | for_each_sched_entity(se) { |
6272d68c | 631 | struct load_weight *load; |
3104bf03 | 632 | struct load_weight lw; |
6272d68c LM |
633 | |
634 | cfs_rq = cfs_rq_of(se); | |
635 | load = &cfs_rq->load; | |
f9c0b095 | 636 | |
0a582440 | 637 | if (unlikely(!se->on_rq)) { |
3104bf03 | 638 | lw = cfs_rq->load; |
0a582440 MG |
639 | |
640 | update_load_add(&lw, se->load.weight); | |
641 | load = &lw; | |
642 | } | |
9dbdb155 | 643 | slice = __calc_delta(slice, se->load.weight, load); |
0a582440 MG |
644 | } |
645 | return slice; | |
bf0f6f24 IM |
646 | } |
647 | ||
647e7cac | 648 | /* |
660cc00f | 649 | * We calculate the vruntime slice of a to-be-inserted task. |
647e7cac | 650 | * |
f9c0b095 | 651 | * vs = s/w |
647e7cac | 652 | */ |
f9c0b095 | 653 | static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se) |
67e9fb2a | 654 | { |
f9c0b095 | 655 | return calc_delta_fair(sched_slice(cfs_rq, se), se); |
a7be37ac PZ |
656 | } |
657 | ||
a75cdaa9 | 658 | #ifdef CONFIG_SMP |
ba7e5a27 | 659 | static int select_idle_sibling(struct task_struct *p, int cpu); |
fb13c7ee MG |
660 | static unsigned long task_h_load(struct task_struct *p); |
661 | ||
9d89c257 YD |
662 | /* |
663 | * We choose a half-life close to 1 scheduling period. | |
84fb5a18 LY |
664 | * Note: The tables runnable_avg_yN_inv and runnable_avg_yN_sum are |
665 | * dependent on this value. | |
9d89c257 YD |
666 | */ |
667 | #define LOAD_AVG_PERIOD 32 | |
668 | #define LOAD_AVG_MAX 47742 /* maximum possible load avg */ | |
84fb5a18 | 669 | #define LOAD_AVG_MAX_N 345 /* number of full periods to produce LOAD_AVG_MAX */ |
a75cdaa9 | 670 | |
540247fb YD |
671 | /* Give new sched_entity start runnable values to heavy its load in infant time */ |
672 | void init_entity_runnable_average(struct sched_entity *se) | |
a75cdaa9 | 673 | { |
540247fb | 674 | struct sched_avg *sa = &se->avg; |
a75cdaa9 | 675 | |
9d89c257 YD |
676 | sa->last_update_time = 0; |
677 | /* | |
678 | * sched_avg's period_contrib should be strictly less then 1024, so | |
679 | * we give it 1023 to make sure it is almost a period (1024us), and | |
680 | * will definitely be update (after enqueue). | |
681 | */ | |
682 | sa->period_contrib = 1023; | |
540247fb | 683 | sa->load_avg = scale_load_down(se->load.weight); |
9d89c257 YD |
684 | sa->load_sum = sa->load_avg * LOAD_AVG_MAX; |
685 | sa->util_avg = scale_load_down(SCHED_LOAD_SCALE); | |
006cdf02 | 686 | sa->util_sum = sa->util_avg * LOAD_AVG_MAX; |
9d89c257 | 687 | /* when this task enqueue'ed, it will contribute to its cfs_rq's load_avg */ |
a75cdaa9 | 688 | } |
7ea241af YD |
689 | |
690 | static inline unsigned long cfs_rq_runnable_load_avg(struct cfs_rq *cfs_rq); | |
691 | static inline unsigned long cfs_rq_load_avg(struct cfs_rq *cfs_rq); | |
a75cdaa9 | 692 | #else |
540247fb | 693 | void init_entity_runnable_average(struct sched_entity *se) |
a75cdaa9 AS |
694 | { |
695 | } | |
696 | #endif | |
697 | ||
bf0f6f24 | 698 | /* |
9dbdb155 | 699 | * Update the current task's runtime statistics. |
bf0f6f24 | 700 | */ |
b7cc0896 | 701 | static void update_curr(struct cfs_rq *cfs_rq) |
bf0f6f24 | 702 | { |
429d43bc | 703 | struct sched_entity *curr = cfs_rq->curr; |
78becc27 | 704 | u64 now = rq_clock_task(rq_of(cfs_rq)); |
9dbdb155 | 705 | u64 delta_exec; |
bf0f6f24 IM |
706 | |
707 | if (unlikely(!curr)) | |
708 | return; | |
709 | ||
9dbdb155 PZ |
710 | delta_exec = now - curr->exec_start; |
711 | if (unlikely((s64)delta_exec <= 0)) | |
34f28ecd | 712 | return; |
bf0f6f24 | 713 | |
8ebc91d9 | 714 | curr->exec_start = now; |
d842de87 | 715 | |
9dbdb155 PZ |
716 | schedstat_set(curr->statistics.exec_max, |
717 | max(delta_exec, curr->statistics.exec_max)); | |
718 | ||
719 | curr->sum_exec_runtime += delta_exec; | |
720 | schedstat_add(cfs_rq, exec_clock, delta_exec); | |
721 | ||
722 | curr->vruntime += calc_delta_fair(delta_exec, curr); | |
723 | update_min_vruntime(cfs_rq); | |
724 | ||
d842de87 SV |
725 | if (entity_is_task(curr)) { |
726 | struct task_struct *curtask = task_of(curr); | |
727 | ||
f977bb49 | 728 | trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime); |
d842de87 | 729 | cpuacct_charge(curtask, delta_exec); |
f06febc9 | 730 | account_group_exec_runtime(curtask, delta_exec); |
d842de87 | 731 | } |
ec12cb7f PT |
732 | |
733 | account_cfs_rq_runtime(cfs_rq, delta_exec); | |
bf0f6f24 IM |
734 | } |
735 | ||
6e998916 SG |
736 | static void update_curr_fair(struct rq *rq) |
737 | { | |
738 | update_curr(cfs_rq_of(&rq->curr->se)); | |
739 | } | |
740 | ||
3ea94de1 | 741 | #ifdef CONFIG_SCHEDSTATS |
bf0f6f24 | 742 | static inline void |
5870db5b | 743 | update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 | 744 | { |
3ea94de1 JP |
745 | u64 wait_start = rq_clock(rq_of(cfs_rq)); |
746 | ||
747 | if (entity_is_task(se) && task_on_rq_migrating(task_of(se)) && | |
748 | likely(wait_start > se->statistics.wait_start)) | |
749 | wait_start -= se->statistics.wait_start; | |
750 | ||
751 | se->statistics.wait_start = wait_start; | |
bf0f6f24 IM |
752 | } |
753 | ||
3ea94de1 JP |
754 | static void |
755 | update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
756 | { | |
757 | struct task_struct *p; | |
758 | u64 delta = rq_clock(rq_of(cfs_rq)) - se->statistics.wait_start; | |
759 | ||
760 | if (entity_is_task(se)) { | |
761 | p = task_of(se); | |
762 | if (task_on_rq_migrating(p)) { | |
763 | /* | |
764 | * Preserve migrating task's wait time so wait_start | |
765 | * time stamp can be adjusted to accumulate wait time | |
766 | * prior to migration. | |
767 | */ | |
768 | se->statistics.wait_start = delta; | |
769 | return; | |
770 | } | |
771 | trace_sched_stat_wait(p, delta); | |
772 | } | |
773 | ||
774 | se->statistics.wait_max = max(se->statistics.wait_max, delta); | |
775 | se->statistics.wait_count++; | |
776 | se->statistics.wait_sum += delta; | |
777 | se->statistics.wait_start = 0; | |
778 | } | |
779 | #else | |
780 | static inline void | |
781 | update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
782 | { | |
783 | } | |
784 | ||
785 | static inline void | |
786 | update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
787 | { | |
788 | } | |
789 | #endif | |
790 | ||
bf0f6f24 IM |
791 | /* |
792 | * Task is being enqueued - update stats: | |
793 | */ | |
d2417e5a | 794 | static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 | 795 | { |
bf0f6f24 IM |
796 | /* |
797 | * Are we enqueueing a waiting task? (for current tasks | |
798 | * a dequeue/enqueue event is a NOP) | |
799 | */ | |
429d43bc | 800 | if (se != cfs_rq->curr) |
5870db5b | 801 | update_stats_wait_start(cfs_rq, se); |
bf0f6f24 IM |
802 | } |
803 | ||
bf0f6f24 | 804 | static inline void |
19b6a2e3 | 805 | update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 | 806 | { |
bf0f6f24 IM |
807 | /* |
808 | * Mark the end of the wait period if dequeueing a | |
809 | * waiting task: | |
810 | */ | |
429d43bc | 811 | if (se != cfs_rq->curr) |
9ef0a961 | 812 | update_stats_wait_end(cfs_rq, se); |
bf0f6f24 IM |
813 | } |
814 | ||
815 | /* | |
816 | * We are picking a new current task - update its stats: | |
817 | */ | |
818 | static inline void | |
79303e9e | 819 | update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 IM |
820 | { |
821 | /* | |
822 | * We are starting a new run period: | |
823 | */ | |
78becc27 | 824 | se->exec_start = rq_clock_task(rq_of(cfs_rq)); |
bf0f6f24 IM |
825 | } |
826 | ||
bf0f6f24 IM |
827 | /************************************************** |
828 | * Scheduling class queueing methods: | |
829 | */ | |
830 | ||
cbee9f88 PZ |
831 | #ifdef CONFIG_NUMA_BALANCING |
832 | /* | |
598f0ec0 MG |
833 | * Approximate time to scan a full NUMA task in ms. The task scan period is |
834 | * calculated based on the tasks virtual memory size and | |
835 | * numa_balancing_scan_size. | |
cbee9f88 | 836 | */ |
598f0ec0 MG |
837 | unsigned int sysctl_numa_balancing_scan_period_min = 1000; |
838 | unsigned int sysctl_numa_balancing_scan_period_max = 60000; | |
6e5fb223 PZ |
839 | |
840 | /* Portion of address space to scan in MB */ | |
841 | unsigned int sysctl_numa_balancing_scan_size = 256; | |
cbee9f88 | 842 | |
4b96a29b PZ |
843 | /* Scan @scan_size MB every @scan_period after an initial @scan_delay in ms */ |
844 | unsigned int sysctl_numa_balancing_scan_delay = 1000; | |
845 | ||
598f0ec0 MG |
846 | static unsigned int task_nr_scan_windows(struct task_struct *p) |
847 | { | |
848 | unsigned long rss = 0; | |
849 | unsigned long nr_scan_pages; | |
850 | ||
851 | /* | |
852 | * Calculations based on RSS as non-present and empty pages are skipped | |
853 | * by the PTE scanner and NUMA hinting faults should be trapped based | |
854 | * on resident pages | |
855 | */ | |
856 | nr_scan_pages = sysctl_numa_balancing_scan_size << (20 - PAGE_SHIFT); | |
857 | rss = get_mm_rss(p->mm); | |
858 | if (!rss) | |
859 | rss = nr_scan_pages; | |
860 | ||
861 | rss = round_up(rss, nr_scan_pages); | |
862 | return rss / nr_scan_pages; | |
863 | } | |
864 | ||
865 | /* For sanitys sake, never scan more PTEs than MAX_SCAN_WINDOW MB/sec. */ | |
866 | #define MAX_SCAN_WINDOW 2560 | |
867 | ||
868 | static unsigned int task_scan_min(struct task_struct *p) | |
869 | { | |
316c1608 | 870 | unsigned int scan_size = READ_ONCE(sysctl_numa_balancing_scan_size); |
598f0ec0 MG |
871 | unsigned int scan, floor; |
872 | unsigned int windows = 1; | |
873 | ||
64192658 KT |
874 | if (scan_size < MAX_SCAN_WINDOW) |
875 | windows = MAX_SCAN_WINDOW / scan_size; | |
598f0ec0 MG |
876 | floor = 1000 / windows; |
877 | ||
878 | scan = sysctl_numa_balancing_scan_period_min / task_nr_scan_windows(p); | |
879 | return max_t(unsigned int, floor, scan); | |
880 | } | |
881 | ||
882 | static unsigned int task_scan_max(struct task_struct *p) | |
883 | { | |
884 | unsigned int smin = task_scan_min(p); | |
885 | unsigned int smax; | |
886 | ||
887 | /* Watch for min being lower than max due to floor calculations */ | |
888 | smax = sysctl_numa_balancing_scan_period_max / task_nr_scan_windows(p); | |
889 | return max(smin, smax); | |
890 | } | |
891 | ||
0ec8aa00 PZ |
892 | static void account_numa_enqueue(struct rq *rq, struct task_struct *p) |
893 | { | |
894 | rq->nr_numa_running += (p->numa_preferred_nid != -1); | |
895 | rq->nr_preferred_running += (p->numa_preferred_nid == task_node(p)); | |
896 | } | |
897 | ||
898 | static void account_numa_dequeue(struct rq *rq, struct task_struct *p) | |
899 | { | |
900 | rq->nr_numa_running -= (p->numa_preferred_nid != -1); | |
901 | rq->nr_preferred_running -= (p->numa_preferred_nid == task_node(p)); | |
902 | } | |
903 | ||
8c8a743c PZ |
904 | struct numa_group { |
905 | atomic_t refcount; | |
906 | ||
907 | spinlock_t lock; /* nr_tasks, tasks */ | |
908 | int nr_tasks; | |
e29cf08b | 909 | pid_t gid; |
8c8a743c PZ |
910 | |
911 | struct rcu_head rcu; | |
20e07dea | 912 | nodemask_t active_nodes; |
989348b5 | 913 | unsigned long total_faults; |
7e2703e6 RR |
914 | /* |
915 | * Faults_cpu is used to decide whether memory should move | |
916 | * towards the CPU. As a consequence, these stats are weighted | |
917 | * more by CPU use than by memory faults. | |
918 | */ | |
50ec8a40 | 919 | unsigned long *faults_cpu; |
989348b5 | 920 | unsigned long faults[0]; |
8c8a743c PZ |
921 | }; |
922 | ||
be1e4e76 RR |
923 | /* Shared or private faults. */ |
924 | #define NR_NUMA_HINT_FAULT_TYPES 2 | |
925 | ||
926 | /* Memory and CPU locality */ | |
927 | #define NR_NUMA_HINT_FAULT_STATS (NR_NUMA_HINT_FAULT_TYPES * 2) | |
928 | ||
929 | /* Averaged statistics, and temporary buffers. */ | |
930 | #define NR_NUMA_HINT_FAULT_BUCKETS (NR_NUMA_HINT_FAULT_STATS * 2) | |
931 | ||
e29cf08b MG |
932 | pid_t task_numa_group_id(struct task_struct *p) |
933 | { | |
934 | return p->numa_group ? p->numa_group->gid : 0; | |
935 | } | |
936 | ||
44dba3d5 IM |
937 | /* |
938 | * The averaged statistics, shared & private, memory & cpu, | |
939 | * occupy the first half of the array. The second half of the | |
940 | * array is for current counters, which are averaged into the | |
941 | * first set by task_numa_placement. | |
942 | */ | |
943 | static inline int task_faults_idx(enum numa_faults_stats s, int nid, int priv) | |
ac8e895b | 944 | { |
44dba3d5 | 945 | return NR_NUMA_HINT_FAULT_TYPES * (s * nr_node_ids + nid) + priv; |
ac8e895b MG |
946 | } |
947 | ||
948 | static inline unsigned long task_faults(struct task_struct *p, int nid) | |
949 | { | |
44dba3d5 | 950 | if (!p->numa_faults) |
ac8e895b MG |
951 | return 0; |
952 | ||
44dba3d5 IM |
953 | return p->numa_faults[task_faults_idx(NUMA_MEM, nid, 0)] + |
954 | p->numa_faults[task_faults_idx(NUMA_MEM, nid, 1)]; | |
ac8e895b MG |
955 | } |
956 | ||
83e1d2cd MG |
957 | static inline unsigned long group_faults(struct task_struct *p, int nid) |
958 | { | |
959 | if (!p->numa_group) | |
960 | return 0; | |
961 | ||
44dba3d5 IM |
962 | return p->numa_group->faults[task_faults_idx(NUMA_MEM, nid, 0)] + |
963 | p->numa_group->faults[task_faults_idx(NUMA_MEM, nid, 1)]; | |
83e1d2cd MG |
964 | } |
965 | ||
20e07dea RR |
966 | static inline unsigned long group_faults_cpu(struct numa_group *group, int nid) |
967 | { | |
44dba3d5 IM |
968 | return group->faults_cpu[task_faults_idx(NUMA_MEM, nid, 0)] + |
969 | group->faults_cpu[task_faults_idx(NUMA_MEM, nid, 1)]; | |
20e07dea RR |
970 | } |
971 | ||
6c6b1193 RR |
972 | /* Handle placement on systems where not all nodes are directly connected. */ |
973 | static unsigned long score_nearby_nodes(struct task_struct *p, int nid, | |
974 | int maxdist, bool task) | |
975 | { | |
976 | unsigned long score = 0; | |
977 | int node; | |
978 | ||
979 | /* | |
980 | * All nodes are directly connected, and the same distance | |
981 | * from each other. No need for fancy placement algorithms. | |
982 | */ | |
983 | if (sched_numa_topology_type == NUMA_DIRECT) | |
984 | return 0; | |
985 | ||
986 | /* | |
987 | * This code is called for each node, introducing N^2 complexity, | |
988 | * which should be ok given the number of nodes rarely exceeds 8. | |
989 | */ | |
990 | for_each_online_node(node) { | |
991 | unsigned long faults; | |
992 | int dist = node_distance(nid, node); | |
993 | ||
994 | /* | |
995 | * The furthest away nodes in the system are not interesting | |
996 | * for placement; nid was already counted. | |
997 | */ | |
998 | if (dist == sched_max_numa_distance || node == nid) | |
999 | continue; | |
1000 | ||
1001 | /* | |
1002 | * On systems with a backplane NUMA topology, compare groups | |
1003 | * of nodes, and move tasks towards the group with the most | |
1004 | * memory accesses. When comparing two nodes at distance | |
1005 | * "hoplimit", only nodes closer by than "hoplimit" are part | |
1006 | * of each group. Skip other nodes. | |
1007 | */ | |
1008 | if (sched_numa_topology_type == NUMA_BACKPLANE && | |
1009 | dist > maxdist) | |
1010 | continue; | |
1011 | ||
1012 | /* Add up the faults from nearby nodes. */ | |
1013 | if (task) | |
1014 | faults = task_faults(p, node); | |
1015 | else | |
1016 | faults = group_faults(p, node); | |
1017 | ||
1018 | /* | |
1019 | * On systems with a glueless mesh NUMA topology, there are | |
1020 | * no fixed "groups of nodes". Instead, nodes that are not | |
1021 | * directly connected bounce traffic through intermediate | |
1022 | * nodes; a numa_group can occupy any set of nodes. | |
1023 | * The further away a node is, the less the faults count. | |
1024 | * This seems to result in good task placement. | |
1025 | */ | |
1026 | if (sched_numa_topology_type == NUMA_GLUELESS_MESH) { | |
1027 | faults *= (sched_max_numa_distance - dist); | |
1028 | faults /= (sched_max_numa_distance - LOCAL_DISTANCE); | |
1029 | } | |
1030 | ||
1031 | score += faults; | |
1032 | } | |
1033 | ||
1034 | return score; | |
1035 | } | |
1036 | ||
83e1d2cd MG |
1037 | /* |
1038 | * These return the fraction of accesses done by a particular task, or | |
1039 | * task group, on a particular numa node. The group weight is given a | |
1040 | * larger multiplier, in order to group tasks together that are almost | |
1041 | * evenly spread out between numa nodes. | |
1042 | */ | |
7bd95320 RR |
1043 | static inline unsigned long task_weight(struct task_struct *p, int nid, |
1044 | int dist) | |
83e1d2cd | 1045 | { |
7bd95320 | 1046 | unsigned long faults, total_faults; |
83e1d2cd | 1047 | |
44dba3d5 | 1048 | if (!p->numa_faults) |
83e1d2cd MG |
1049 | return 0; |
1050 | ||
1051 | total_faults = p->total_numa_faults; | |
1052 | ||
1053 | if (!total_faults) | |
1054 | return 0; | |
1055 | ||
7bd95320 | 1056 | faults = task_faults(p, nid); |
6c6b1193 RR |
1057 | faults += score_nearby_nodes(p, nid, dist, true); |
1058 | ||
7bd95320 | 1059 | return 1000 * faults / total_faults; |
83e1d2cd MG |
1060 | } |
1061 | ||
7bd95320 RR |
1062 | static inline unsigned long group_weight(struct task_struct *p, int nid, |
1063 | int dist) | |
83e1d2cd | 1064 | { |
7bd95320 RR |
1065 | unsigned long faults, total_faults; |
1066 | ||
1067 | if (!p->numa_group) | |
1068 | return 0; | |
1069 | ||
1070 | total_faults = p->numa_group->total_faults; | |
1071 | ||
1072 | if (!total_faults) | |
83e1d2cd MG |
1073 | return 0; |
1074 | ||
7bd95320 | 1075 | faults = group_faults(p, nid); |
6c6b1193 RR |
1076 | faults += score_nearby_nodes(p, nid, dist, false); |
1077 | ||
7bd95320 | 1078 | return 1000 * faults / total_faults; |
83e1d2cd MG |
1079 | } |
1080 | ||
10f39042 RR |
1081 | bool should_numa_migrate_memory(struct task_struct *p, struct page * page, |
1082 | int src_nid, int dst_cpu) | |
1083 | { | |
1084 | struct numa_group *ng = p->numa_group; | |
1085 | int dst_nid = cpu_to_node(dst_cpu); | |
1086 | int last_cpupid, this_cpupid; | |
1087 | ||
1088 | this_cpupid = cpu_pid_to_cpupid(dst_cpu, current->pid); | |
1089 | ||
1090 | /* | |
1091 | * Multi-stage node selection is used in conjunction with a periodic | |
1092 | * migration fault to build a temporal task<->page relation. By using | |
1093 | * a two-stage filter we remove short/unlikely relations. | |
1094 | * | |
1095 | * Using P(p) ~ n_p / n_t as per frequentist probability, we can equate | |
1096 | * a task's usage of a particular page (n_p) per total usage of this | |
1097 | * page (n_t) (in a given time-span) to a probability. | |
1098 | * | |
1099 | * Our periodic faults will sample this probability and getting the | |
1100 | * same result twice in a row, given these samples are fully | |
1101 | * independent, is then given by P(n)^2, provided our sample period | |
1102 | * is sufficiently short compared to the usage pattern. | |
1103 | * | |
1104 | * This quadric squishes small probabilities, making it less likely we | |
1105 | * act on an unlikely task<->page relation. | |
1106 | */ | |
1107 | last_cpupid = page_cpupid_xchg_last(page, this_cpupid); | |
1108 | if (!cpupid_pid_unset(last_cpupid) && | |
1109 | cpupid_to_nid(last_cpupid) != dst_nid) | |
1110 | return false; | |
1111 | ||
1112 | /* Always allow migrate on private faults */ | |
1113 | if (cpupid_match_pid(p, last_cpupid)) | |
1114 | return true; | |
1115 | ||
1116 | /* A shared fault, but p->numa_group has not been set up yet. */ | |
1117 | if (!ng) | |
1118 | return true; | |
1119 | ||
1120 | /* | |
1121 | * Do not migrate if the destination is not a node that | |
1122 | * is actively used by this numa group. | |
1123 | */ | |
1124 | if (!node_isset(dst_nid, ng->active_nodes)) | |
1125 | return false; | |
1126 | ||
1127 | /* | |
1128 | * Source is a node that is not actively used by this | |
1129 | * numa group, while the destination is. Migrate. | |
1130 | */ | |
1131 | if (!node_isset(src_nid, ng->active_nodes)) | |
1132 | return true; | |
1133 | ||
1134 | /* | |
1135 | * Both source and destination are nodes in active | |
1136 | * use by this numa group. Maximize memory bandwidth | |
1137 | * by migrating from more heavily used groups, to less | |
1138 | * heavily used ones, spreading the load around. | |
1139 | * Use a 1/4 hysteresis to avoid spurious page movement. | |
1140 | */ | |
1141 | return group_faults(p, dst_nid) < (group_faults(p, src_nid) * 3 / 4); | |
1142 | } | |
1143 | ||
e6628d5b | 1144 | static unsigned long weighted_cpuload(const int cpu); |
58d081b5 MG |
1145 | static unsigned long source_load(int cpu, int type); |
1146 | static unsigned long target_load(int cpu, int type); | |
ced549fa | 1147 | static unsigned long capacity_of(int cpu); |
58d081b5 MG |
1148 | static long effective_load(struct task_group *tg, int cpu, long wl, long wg); |
1149 | ||
fb13c7ee | 1150 | /* Cached statistics for all CPUs within a node */ |
58d081b5 | 1151 | struct numa_stats { |
fb13c7ee | 1152 | unsigned long nr_running; |
58d081b5 | 1153 | unsigned long load; |
fb13c7ee MG |
1154 | |
1155 | /* Total compute capacity of CPUs on a node */ | |
5ef20ca1 | 1156 | unsigned long compute_capacity; |
fb13c7ee MG |
1157 | |
1158 | /* Approximate capacity in terms of runnable tasks on a node */ | |
5ef20ca1 | 1159 | unsigned long task_capacity; |
1b6a7495 | 1160 | int has_free_capacity; |
58d081b5 | 1161 | }; |
e6628d5b | 1162 | |
fb13c7ee MG |
1163 | /* |
1164 | * XXX borrowed from update_sg_lb_stats | |
1165 | */ | |
1166 | static void update_numa_stats(struct numa_stats *ns, int nid) | |
1167 | { | |
83d7f242 RR |
1168 | int smt, cpu, cpus = 0; |
1169 | unsigned long capacity; | |
fb13c7ee MG |
1170 | |
1171 | memset(ns, 0, sizeof(*ns)); | |
1172 | for_each_cpu(cpu, cpumask_of_node(nid)) { | |
1173 | struct rq *rq = cpu_rq(cpu); | |
1174 | ||
1175 | ns->nr_running += rq->nr_running; | |
1176 | ns->load += weighted_cpuload(cpu); | |
ced549fa | 1177 | ns->compute_capacity += capacity_of(cpu); |
5eca82a9 PZ |
1178 | |
1179 | cpus++; | |
fb13c7ee MG |
1180 | } |
1181 | ||
5eca82a9 PZ |
1182 | /* |
1183 | * If we raced with hotplug and there are no CPUs left in our mask | |
1184 | * the @ns structure is NULL'ed and task_numa_compare() will | |
1185 | * not find this node attractive. | |
1186 | * | |
1b6a7495 NP |
1187 | * We'll either bail at !has_free_capacity, or we'll detect a huge |
1188 | * imbalance and bail there. | |
5eca82a9 PZ |
1189 | */ |
1190 | if (!cpus) | |
1191 | return; | |
1192 | ||
83d7f242 RR |
1193 | /* smt := ceil(cpus / capacity), assumes: 1 < smt_power < 2 */ |
1194 | smt = DIV_ROUND_UP(SCHED_CAPACITY_SCALE * cpus, ns->compute_capacity); | |
1195 | capacity = cpus / smt; /* cores */ | |
1196 | ||
1197 | ns->task_capacity = min_t(unsigned, capacity, | |
1198 | DIV_ROUND_CLOSEST(ns->compute_capacity, SCHED_CAPACITY_SCALE)); | |
1b6a7495 | 1199 | ns->has_free_capacity = (ns->nr_running < ns->task_capacity); |
fb13c7ee MG |
1200 | } |
1201 | ||
58d081b5 MG |
1202 | struct task_numa_env { |
1203 | struct task_struct *p; | |
e6628d5b | 1204 | |
58d081b5 MG |
1205 | int src_cpu, src_nid; |
1206 | int dst_cpu, dst_nid; | |
e6628d5b | 1207 | |
58d081b5 | 1208 | struct numa_stats src_stats, dst_stats; |
e6628d5b | 1209 | |
40ea2b42 | 1210 | int imbalance_pct; |
7bd95320 | 1211 | int dist; |
fb13c7ee MG |
1212 | |
1213 | struct task_struct *best_task; | |
1214 | long best_imp; | |
58d081b5 MG |
1215 | int best_cpu; |
1216 | }; | |
1217 | ||
fb13c7ee MG |
1218 | static void task_numa_assign(struct task_numa_env *env, |
1219 | struct task_struct *p, long imp) | |
1220 | { | |
1221 | if (env->best_task) | |
1222 | put_task_struct(env->best_task); | |
1223 | if (p) | |
1224 | get_task_struct(p); | |
1225 | ||
1226 | env->best_task = p; | |
1227 | env->best_imp = imp; | |
1228 | env->best_cpu = env->dst_cpu; | |
1229 | } | |
1230 | ||
28a21745 | 1231 | static bool load_too_imbalanced(long src_load, long dst_load, |
e63da036 RR |
1232 | struct task_numa_env *env) |
1233 | { | |
e4991b24 RR |
1234 | long imb, old_imb; |
1235 | long orig_src_load, orig_dst_load; | |
28a21745 RR |
1236 | long src_capacity, dst_capacity; |
1237 | ||
1238 | /* | |
1239 | * The load is corrected for the CPU capacity available on each node. | |
1240 | * | |
1241 | * src_load dst_load | |
1242 | * ------------ vs --------- | |
1243 | * src_capacity dst_capacity | |
1244 | */ | |
1245 | src_capacity = env->src_stats.compute_capacity; | |
1246 | dst_capacity = env->dst_stats.compute_capacity; | |
e63da036 RR |
1247 | |
1248 | /* We care about the slope of the imbalance, not the direction. */ | |
e4991b24 RR |
1249 | if (dst_load < src_load) |
1250 | swap(dst_load, src_load); | |
e63da036 RR |
1251 | |
1252 | /* Is the difference below the threshold? */ | |
e4991b24 RR |
1253 | imb = dst_load * src_capacity * 100 - |
1254 | src_load * dst_capacity * env->imbalance_pct; | |
e63da036 RR |
1255 | if (imb <= 0) |
1256 | return false; | |
1257 | ||
1258 | /* | |
1259 | * The imbalance is above the allowed threshold. | |
e4991b24 | 1260 | * Compare it with the old imbalance. |
e63da036 | 1261 | */ |
28a21745 | 1262 | orig_src_load = env->src_stats.load; |
e4991b24 | 1263 | orig_dst_load = env->dst_stats.load; |
28a21745 | 1264 | |
e4991b24 RR |
1265 | if (orig_dst_load < orig_src_load) |
1266 | swap(orig_dst_load, orig_src_load); | |
e63da036 | 1267 | |
e4991b24 RR |
1268 | old_imb = orig_dst_load * src_capacity * 100 - |
1269 | orig_src_load * dst_capacity * env->imbalance_pct; | |
1270 | ||
1271 | /* Would this change make things worse? */ | |
1272 | return (imb > old_imb); | |
e63da036 RR |
1273 | } |
1274 | ||
fb13c7ee MG |
1275 | /* |
1276 | * This checks if the overall compute and NUMA accesses of the system would | |
1277 | * be improved if the source tasks was migrated to the target dst_cpu taking | |
1278 | * into account that it might be best if task running on the dst_cpu should | |
1279 | * be exchanged with the source task | |
1280 | */ | |
887c290e RR |
1281 | static void task_numa_compare(struct task_numa_env *env, |
1282 | long taskimp, long groupimp) | |
fb13c7ee MG |
1283 | { |
1284 | struct rq *src_rq = cpu_rq(env->src_cpu); | |
1285 | struct rq *dst_rq = cpu_rq(env->dst_cpu); | |
1286 | struct task_struct *cur; | |
28a21745 | 1287 | long src_load, dst_load; |
fb13c7ee | 1288 | long load; |
1c5d3eb3 | 1289 | long imp = env->p->numa_group ? groupimp : taskimp; |
0132c3e1 | 1290 | long moveimp = imp; |
7bd95320 | 1291 | int dist = env->dist; |
fb13c7ee MG |
1292 | |
1293 | rcu_read_lock(); | |
1effd9f1 KT |
1294 | |
1295 | raw_spin_lock_irq(&dst_rq->lock); | |
1296 | cur = dst_rq->curr; | |
1297 | /* | |
1298 | * No need to move the exiting task, and this ensures that ->curr | |
1299 | * wasn't reaped and thus get_task_struct() in task_numa_assign() | |
1300 | * is safe under RCU read lock. | |
1301 | * Note that rcu_read_lock() itself can't protect from the final | |
1302 | * put_task_struct() after the last schedule(). | |
1303 | */ | |
1304 | if ((cur->flags & PF_EXITING) || is_idle_task(cur)) | |
fb13c7ee | 1305 | cur = NULL; |
1effd9f1 | 1306 | raw_spin_unlock_irq(&dst_rq->lock); |
fb13c7ee | 1307 | |
7af68335 PZ |
1308 | /* |
1309 | * Because we have preemption enabled we can get migrated around and | |
1310 | * end try selecting ourselves (current == env->p) as a swap candidate. | |
1311 | */ | |
1312 | if (cur == env->p) | |
1313 | goto unlock; | |
1314 | ||
fb13c7ee MG |
1315 | /* |
1316 | * "imp" is the fault differential for the source task between the | |
1317 | * source and destination node. Calculate the total differential for | |
1318 | * the source task and potential destination task. The more negative | |
1319 | * the value is, the more rmeote accesses that would be expected to | |
1320 | * be incurred if the tasks were swapped. | |
1321 | */ | |
1322 | if (cur) { | |
1323 | /* Skip this swap candidate if cannot move to the source cpu */ | |
1324 | if (!cpumask_test_cpu(env->src_cpu, tsk_cpus_allowed(cur))) | |
1325 | goto unlock; | |
1326 | ||
887c290e RR |
1327 | /* |
1328 | * If dst and source tasks are in the same NUMA group, or not | |
ca28aa53 | 1329 | * in any group then look only at task weights. |
887c290e | 1330 | */ |
ca28aa53 | 1331 | if (cur->numa_group == env->p->numa_group) { |
7bd95320 RR |
1332 | imp = taskimp + task_weight(cur, env->src_nid, dist) - |
1333 | task_weight(cur, env->dst_nid, dist); | |
ca28aa53 RR |
1334 | /* |
1335 | * Add some hysteresis to prevent swapping the | |
1336 | * tasks within a group over tiny differences. | |
1337 | */ | |
1338 | if (cur->numa_group) | |
1339 | imp -= imp/16; | |
887c290e | 1340 | } else { |
ca28aa53 RR |
1341 | /* |
1342 | * Compare the group weights. If a task is all by | |
1343 | * itself (not part of a group), use the task weight | |
1344 | * instead. | |
1345 | */ | |
ca28aa53 | 1346 | if (cur->numa_group) |
7bd95320 RR |
1347 | imp += group_weight(cur, env->src_nid, dist) - |
1348 | group_weight(cur, env->dst_nid, dist); | |
ca28aa53 | 1349 | else |
7bd95320 RR |
1350 | imp += task_weight(cur, env->src_nid, dist) - |
1351 | task_weight(cur, env->dst_nid, dist); | |
887c290e | 1352 | } |
fb13c7ee MG |
1353 | } |
1354 | ||
0132c3e1 | 1355 | if (imp <= env->best_imp && moveimp <= env->best_imp) |
fb13c7ee MG |
1356 | goto unlock; |
1357 | ||
1358 | if (!cur) { | |
1359 | /* Is there capacity at our destination? */ | |
b932c03c | 1360 | if (env->src_stats.nr_running <= env->src_stats.task_capacity && |
1b6a7495 | 1361 | !env->dst_stats.has_free_capacity) |
fb13c7ee MG |
1362 | goto unlock; |
1363 | ||
1364 | goto balance; | |
1365 | } | |
1366 | ||
1367 | /* Balance doesn't matter much if we're running a task per cpu */ | |
0132c3e1 RR |
1368 | if (imp > env->best_imp && src_rq->nr_running == 1 && |
1369 | dst_rq->nr_running == 1) | |
fb13c7ee MG |
1370 | goto assign; |
1371 | ||
1372 | /* | |
1373 | * In the overloaded case, try and keep the load balanced. | |
1374 | */ | |
1375 | balance: | |
e720fff6 PZ |
1376 | load = task_h_load(env->p); |
1377 | dst_load = env->dst_stats.load + load; | |
1378 | src_load = env->src_stats.load - load; | |
fb13c7ee | 1379 | |
0132c3e1 RR |
1380 | if (moveimp > imp && moveimp > env->best_imp) { |
1381 | /* | |
1382 | * If the improvement from just moving env->p direction is | |
1383 | * better than swapping tasks around, check if a move is | |
1384 | * possible. Store a slightly smaller score than moveimp, | |
1385 | * so an actually idle CPU will win. | |
1386 | */ | |
1387 | if (!load_too_imbalanced(src_load, dst_load, env)) { | |
1388 | imp = moveimp - 1; | |
1389 | cur = NULL; | |
1390 | goto assign; | |
1391 | } | |
1392 | } | |
1393 | ||
1394 | if (imp <= env->best_imp) | |
1395 | goto unlock; | |
1396 | ||
fb13c7ee | 1397 | if (cur) { |
e720fff6 PZ |
1398 | load = task_h_load(cur); |
1399 | dst_load -= load; | |
1400 | src_load += load; | |
fb13c7ee MG |
1401 | } |
1402 | ||
28a21745 | 1403 | if (load_too_imbalanced(src_load, dst_load, env)) |
fb13c7ee MG |
1404 | goto unlock; |
1405 | ||
ba7e5a27 RR |
1406 | /* |
1407 | * One idle CPU per node is evaluated for a task numa move. | |
1408 | * Call select_idle_sibling to maybe find a better one. | |
1409 | */ | |
1410 | if (!cur) | |
1411 | env->dst_cpu = select_idle_sibling(env->p, env->dst_cpu); | |
1412 | ||
fb13c7ee MG |
1413 | assign: |
1414 | task_numa_assign(env, cur, imp); | |
1415 | unlock: | |
1416 | rcu_read_unlock(); | |
1417 | } | |
1418 | ||
887c290e RR |
1419 | static void task_numa_find_cpu(struct task_numa_env *env, |
1420 | long taskimp, long groupimp) | |
2c8a50aa MG |
1421 | { |
1422 | int cpu; | |
1423 | ||
1424 | for_each_cpu(cpu, cpumask_of_node(env->dst_nid)) { | |
1425 | /* Skip this CPU if the source task cannot migrate */ | |
1426 | if (!cpumask_test_cpu(cpu, tsk_cpus_allowed(env->p))) | |
1427 | continue; | |
1428 | ||
1429 | env->dst_cpu = cpu; | |
887c290e | 1430 | task_numa_compare(env, taskimp, groupimp); |
2c8a50aa MG |
1431 | } |
1432 | } | |
1433 | ||
6f9aad0b RR |
1434 | /* Only move tasks to a NUMA node less busy than the current node. */ |
1435 | static bool numa_has_capacity(struct task_numa_env *env) | |
1436 | { | |
1437 | struct numa_stats *src = &env->src_stats; | |
1438 | struct numa_stats *dst = &env->dst_stats; | |
1439 | ||
1440 | if (src->has_free_capacity && !dst->has_free_capacity) | |
1441 | return false; | |
1442 | ||
1443 | /* | |
1444 | * Only consider a task move if the source has a higher load | |
1445 | * than the destination, corrected for CPU capacity on each node. | |
1446 | * | |
1447 | * src->load dst->load | |
1448 | * --------------------- vs --------------------- | |
1449 | * src->compute_capacity dst->compute_capacity | |
1450 | */ | |
44dcb04f SD |
1451 | if (src->load * dst->compute_capacity * env->imbalance_pct > |
1452 | ||
1453 | dst->load * src->compute_capacity * 100) | |
6f9aad0b RR |
1454 | return true; |
1455 | ||
1456 | return false; | |
1457 | } | |
1458 | ||
58d081b5 MG |
1459 | static int task_numa_migrate(struct task_struct *p) |
1460 | { | |
58d081b5 MG |
1461 | struct task_numa_env env = { |
1462 | .p = p, | |
fb13c7ee | 1463 | |
58d081b5 | 1464 | .src_cpu = task_cpu(p), |
b32e86b4 | 1465 | .src_nid = task_node(p), |
fb13c7ee MG |
1466 | |
1467 | .imbalance_pct = 112, | |
1468 | ||
1469 | .best_task = NULL, | |
1470 | .best_imp = 0, | |
1471 | .best_cpu = -1 | |
58d081b5 MG |
1472 | }; |
1473 | struct sched_domain *sd; | |
887c290e | 1474 | unsigned long taskweight, groupweight; |
7bd95320 | 1475 | int nid, ret, dist; |
887c290e | 1476 | long taskimp, groupimp; |
e6628d5b | 1477 | |
58d081b5 | 1478 | /* |
fb13c7ee MG |
1479 | * Pick the lowest SD_NUMA domain, as that would have the smallest |
1480 | * imbalance and would be the first to start moving tasks about. | |
1481 | * | |
1482 | * And we want to avoid any moving of tasks about, as that would create | |
1483 | * random movement of tasks -- counter the numa conditions we're trying | |
1484 | * to satisfy here. | |
58d081b5 MG |
1485 | */ |
1486 | rcu_read_lock(); | |
fb13c7ee | 1487 | sd = rcu_dereference(per_cpu(sd_numa, env.src_cpu)); |
46a73e8a RR |
1488 | if (sd) |
1489 | env.imbalance_pct = 100 + (sd->imbalance_pct - 100) / 2; | |
e6628d5b MG |
1490 | rcu_read_unlock(); |
1491 | ||
46a73e8a RR |
1492 | /* |
1493 | * Cpusets can break the scheduler domain tree into smaller | |
1494 | * balance domains, some of which do not cross NUMA boundaries. | |
1495 | * Tasks that are "trapped" in such domains cannot be migrated | |
1496 | * elsewhere, so there is no point in (re)trying. | |
1497 | */ | |
1498 | if (unlikely(!sd)) { | |
de1b301a | 1499 | p->numa_preferred_nid = task_node(p); |
46a73e8a RR |
1500 | return -EINVAL; |
1501 | } | |
1502 | ||
2c8a50aa | 1503 | env.dst_nid = p->numa_preferred_nid; |
7bd95320 RR |
1504 | dist = env.dist = node_distance(env.src_nid, env.dst_nid); |
1505 | taskweight = task_weight(p, env.src_nid, dist); | |
1506 | groupweight = group_weight(p, env.src_nid, dist); | |
1507 | update_numa_stats(&env.src_stats, env.src_nid); | |
1508 | taskimp = task_weight(p, env.dst_nid, dist) - taskweight; | |
1509 | groupimp = group_weight(p, env.dst_nid, dist) - groupweight; | |
2c8a50aa | 1510 | update_numa_stats(&env.dst_stats, env.dst_nid); |
58d081b5 | 1511 | |
a43455a1 | 1512 | /* Try to find a spot on the preferred nid. */ |
6f9aad0b RR |
1513 | if (numa_has_capacity(&env)) |
1514 | task_numa_find_cpu(&env, taskimp, groupimp); | |
e1dda8a7 | 1515 | |
9de05d48 RR |
1516 | /* |
1517 | * Look at other nodes in these cases: | |
1518 | * - there is no space available on the preferred_nid | |
1519 | * - the task is part of a numa_group that is interleaved across | |
1520 | * multiple NUMA nodes; in order to better consolidate the group, | |
1521 | * we need to check other locations. | |
1522 | */ | |
1523 | if (env.best_cpu == -1 || (p->numa_group && | |
1524 | nodes_weight(p->numa_group->active_nodes) > 1)) { | |
2c8a50aa MG |
1525 | for_each_online_node(nid) { |
1526 | if (nid == env.src_nid || nid == p->numa_preferred_nid) | |
1527 | continue; | |
58d081b5 | 1528 | |
7bd95320 | 1529 | dist = node_distance(env.src_nid, env.dst_nid); |
6c6b1193 RR |
1530 | if (sched_numa_topology_type == NUMA_BACKPLANE && |
1531 | dist != env.dist) { | |
1532 | taskweight = task_weight(p, env.src_nid, dist); | |
1533 | groupweight = group_weight(p, env.src_nid, dist); | |
1534 | } | |
7bd95320 | 1535 | |
83e1d2cd | 1536 | /* Only consider nodes where both task and groups benefit */ |
7bd95320 RR |
1537 | taskimp = task_weight(p, nid, dist) - taskweight; |
1538 | groupimp = group_weight(p, nid, dist) - groupweight; | |
887c290e | 1539 | if (taskimp < 0 && groupimp < 0) |
fb13c7ee MG |
1540 | continue; |
1541 | ||
7bd95320 | 1542 | env.dist = dist; |
2c8a50aa MG |
1543 | env.dst_nid = nid; |
1544 | update_numa_stats(&env.dst_stats, env.dst_nid); | |
6f9aad0b RR |
1545 | if (numa_has_capacity(&env)) |
1546 | task_numa_find_cpu(&env, taskimp, groupimp); | |
58d081b5 MG |
1547 | } |
1548 | } | |
1549 | ||
68d1b02a RR |
1550 | /* |
1551 | * If the task is part of a workload that spans multiple NUMA nodes, | |
1552 | * and is migrating into one of the workload's active nodes, remember | |
1553 | * this node as the task's preferred numa node, so the workload can | |
1554 | * settle down. | |
1555 | * A task that migrated to a second choice node will be better off | |
1556 | * trying for a better one later. Do not set the preferred node here. | |
1557 | */ | |
db015dae RR |
1558 | if (p->numa_group) { |
1559 | if (env.best_cpu == -1) | |
1560 | nid = env.src_nid; | |
1561 | else | |
1562 | nid = env.dst_nid; | |
1563 | ||
1564 | if (node_isset(nid, p->numa_group->active_nodes)) | |
1565 | sched_setnuma(p, env.dst_nid); | |
1566 | } | |
1567 | ||
1568 | /* No better CPU than the current one was found. */ | |
1569 | if (env.best_cpu == -1) | |
1570 | return -EAGAIN; | |
0ec8aa00 | 1571 | |
04bb2f94 RR |
1572 | /* |
1573 | * Reset the scan period if the task is being rescheduled on an | |
1574 | * alternative node to recheck if the tasks is now properly placed. | |
1575 | */ | |
1576 | p->numa_scan_period = task_scan_min(p); | |
1577 | ||
fb13c7ee | 1578 | if (env.best_task == NULL) { |
286549dc MG |
1579 | ret = migrate_task_to(p, env.best_cpu); |
1580 | if (ret != 0) | |
1581 | trace_sched_stick_numa(p, env.src_cpu, env.best_cpu); | |
fb13c7ee MG |
1582 | return ret; |
1583 | } | |
1584 | ||
1585 | ret = migrate_swap(p, env.best_task); | |
286549dc MG |
1586 | if (ret != 0) |
1587 | trace_sched_stick_numa(p, env.src_cpu, task_cpu(env.best_task)); | |
fb13c7ee MG |
1588 | put_task_struct(env.best_task); |
1589 | return ret; | |
e6628d5b MG |
1590 | } |
1591 | ||
6b9a7460 MG |
1592 | /* Attempt to migrate a task to a CPU on the preferred node. */ |
1593 | static void numa_migrate_preferred(struct task_struct *p) | |
1594 | { | |
5085e2a3 RR |
1595 | unsigned long interval = HZ; |
1596 | ||
2739d3ee | 1597 | /* This task has no NUMA fault statistics yet */ |
44dba3d5 | 1598 | if (unlikely(p->numa_preferred_nid == -1 || !p->numa_faults)) |
6b9a7460 MG |
1599 | return; |
1600 | ||
2739d3ee | 1601 | /* Periodically retry migrating the task to the preferred node */ |
5085e2a3 RR |
1602 | interval = min(interval, msecs_to_jiffies(p->numa_scan_period) / 16); |
1603 | p->numa_migrate_retry = jiffies + interval; | |
2739d3ee RR |
1604 | |
1605 | /* Success if task is already running on preferred CPU */ | |
de1b301a | 1606 | if (task_node(p) == p->numa_preferred_nid) |
6b9a7460 MG |
1607 | return; |
1608 | ||
1609 | /* Otherwise, try migrate to a CPU on the preferred node */ | |
2739d3ee | 1610 | task_numa_migrate(p); |
6b9a7460 MG |
1611 | } |
1612 | ||
20e07dea RR |
1613 | /* |
1614 | * Find the nodes on which the workload is actively running. We do this by | |
1615 | * tracking the nodes from which NUMA hinting faults are triggered. This can | |
1616 | * be different from the set of nodes where the workload's memory is currently | |
1617 | * located. | |
1618 | * | |
1619 | * The bitmask is used to make smarter decisions on when to do NUMA page | |
1620 | * migrations, To prevent flip-flopping, and excessive page migrations, nodes | |
1621 | * are added when they cause over 6/16 of the maximum number of faults, but | |
1622 | * only removed when they drop below 3/16. | |
1623 | */ | |
1624 | static void update_numa_active_node_mask(struct numa_group *numa_group) | |
1625 | { | |
1626 | unsigned long faults, max_faults = 0; | |
1627 | int nid; | |
1628 | ||
1629 | for_each_online_node(nid) { | |
1630 | faults = group_faults_cpu(numa_group, nid); | |
1631 | if (faults > max_faults) | |
1632 | max_faults = faults; | |
1633 | } | |
1634 | ||
1635 | for_each_online_node(nid) { | |
1636 | faults = group_faults_cpu(numa_group, nid); | |
1637 | if (!node_isset(nid, numa_group->active_nodes)) { | |
1638 | if (faults > max_faults * 6 / 16) | |
1639 | node_set(nid, numa_group->active_nodes); | |
1640 | } else if (faults < max_faults * 3 / 16) | |
1641 | node_clear(nid, numa_group->active_nodes); | |
1642 | } | |
1643 | } | |
1644 | ||
04bb2f94 RR |
1645 | /* |
1646 | * When adapting the scan rate, the period is divided into NUMA_PERIOD_SLOTS | |
1647 | * increments. The more local the fault statistics are, the higher the scan | |
a22b4b01 RR |
1648 | * period will be for the next scan window. If local/(local+remote) ratio is |
1649 | * below NUMA_PERIOD_THRESHOLD (where range of ratio is 1..NUMA_PERIOD_SLOTS) | |
1650 | * the scan period will decrease. Aim for 70% local accesses. | |
04bb2f94 RR |
1651 | */ |
1652 | #define NUMA_PERIOD_SLOTS 10 | |
a22b4b01 | 1653 | #define NUMA_PERIOD_THRESHOLD 7 |
04bb2f94 RR |
1654 | |
1655 | /* | |
1656 | * Increase the scan period (slow down scanning) if the majority of | |
1657 | * our memory is already on our local node, or if the majority of | |
1658 | * the page accesses are shared with other processes. | |
1659 | * Otherwise, decrease the scan period. | |
1660 | */ | |
1661 | static void update_task_scan_period(struct task_struct *p, | |
1662 | unsigned long shared, unsigned long private) | |
1663 | { | |
1664 | unsigned int period_slot; | |
1665 | int ratio; | |
1666 | int diff; | |
1667 | ||
1668 | unsigned long remote = p->numa_faults_locality[0]; | |
1669 | unsigned long local = p->numa_faults_locality[1]; | |
1670 | ||
1671 | /* | |
1672 | * If there were no record hinting faults then either the task is | |
1673 | * completely idle or all activity is areas that are not of interest | |
074c2381 MG |
1674 | * to automatic numa balancing. Related to that, if there were failed |
1675 | * migration then it implies we are migrating too quickly or the local | |
1676 | * node is overloaded. In either case, scan slower | |
04bb2f94 | 1677 | */ |
074c2381 | 1678 | if (local + shared == 0 || p->numa_faults_locality[2]) { |
04bb2f94 RR |
1679 | p->numa_scan_period = min(p->numa_scan_period_max, |
1680 | p->numa_scan_period << 1); | |
1681 | ||
1682 | p->mm->numa_next_scan = jiffies + | |
1683 | msecs_to_jiffies(p->numa_scan_period); | |
1684 | ||
1685 | return; | |
1686 | } | |
1687 | ||
1688 | /* | |
1689 | * Prepare to scale scan period relative to the current period. | |
1690 | * == NUMA_PERIOD_THRESHOLD scan period stays the same | |
1691 | * < NUMA_PERIOD_THRESHOLD scan period decreases (scan faster) | |
1692 | * >= NUMA_PERIOD_THRESHOLD scan period increases (scan slower) | |
1693 | */ | |
1694 | period_slot = DIV_ROUND_UP(p->numa_scan_period, NUMA_PERIOD_SLOTS); | |
1695 | ratio = (local * NUMA_PERIOD_SLOTS) / (local + remote); | |
1696 | if (ratio >= NUMA_PERIOD_THRESHOLD) { | |
1697 | int slot = ratio - NUMA_PERIOD_THRESHOLD; | |
1698 | if (!slot) | |
1699 | slot = 1; | |
1700 | diff = slot * period_slot; | |
1701 | } else { | |
1702 | diff = -(NUMA_PERIOD_THRESHOLD - ratio) * period_slot; | |
1703 | ||
1704 | /* | |
1705 | * Scale scan rate increases based on sharing. There is an | |
1706 | * inverse relationship between the degree of sharing and | |
1707 | * the adjustment made to the scanning period. Broadly | |
1708 | * speaking the intent is that there is little point | |
1709 | * scanning faster if shared accesses dominate as it may | |
1710 | * simply bounce migrations uselessly | |
1711 | */ | |
2847c90e | 1712 | ratio = DIV_ROUND_UP(private * NUMA_PERIOD_SLOTS, (private + shared + 1)); |
04bb2f94 RR |
1713 | diff = (diff * ratio) / NUMA_PERIOD_SLOTS; |
1714 | } | |
1715 | ||
1716 | p->numa_scan_period = clamp(p->numa_scan_period + diff, | |
1717 | task_scan_min(p), task_scan_max(p)); | |
1718 | memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); | |
1719 | } | |
1720 | ||
7e2703e6 RR |
1721 | /* |
1722 | * Get the fraction of time the task has been running since the last | |
1723 | * NUMA placement cycle. The scheduler keeps similar statistics, but | |
1724 | * decays those on a 32ms period, which is orders of magnitude off | |
1725 | * from the dozens-of-seconds NUMA balancing period. Use the scheduler | |
1726 | * stats only if the task is so new there are no NUMA statistics yet. | |
1727 | */ | |
1728 | static u64 numa_get_avg_runtime(struct task_struct *p, u64 *period) | |
1729 | { | |
1730 | u64 runtime, delta, now; | |
1731 | /* Use the start of this time slice to avoid calculations. */ | |
1732 | now = p->se.exec_start; | |
1733 | runtime = p->se.sum_exec_runtime; | |
1734 | ||
1735 | if (p->last_task_numa_placement) { | |
1736 | delta = runtime - p->last_sum_exec_runtime; | |
1737 | *period = now - p->last_task_numa_placement; | |
1738 | } else { | |
9d89c257 YD |
1739 | delta = p->se.avg.load_sum / p->se.load.weight; |
1740 | *period = LOAD_AVG_MAX; | |
7e2703e6 RR |
1741 | } |
1742 | ||
1743 | p->last_sum_exec_runtime = runtime; | |
1744 | p->last_task_numa_placement = now; | |
1745 | ||
1746 | return delta; | |
1747 | } | |
1748 | ||
54009416 RR |
1749 | /* |
1750 | * Determine the preferred nid for a task in a numa_group. This needs to | |
1751 | * be done in a way that produces consistent results with group_weight, | |
1752 | * otherwise workloads might not converge. | |
1753 | */ | |
1754 | static int preferred_group_nid(struct task_struct *p, int nid) | |
1755 | { | |
1756 | nodemask_t nodes; | |
1757 | int dist; | |
1758 | ||
1759 | /* Direct connections between all NUMA nodes. */ | |
1760 | if (sched_numa_topology_type == NUMA_DIRECT) | |
1761 | return nid; | |
1762 | ||
1763 | /* | |
1764 | * On a system with glueless mesh NUMA topology, group_weight | |
1765 | * scores nodes according to the number of NUMA hinting faults on | |
1766 | * both the node itself, and on nearby nodes. | |
1767 | */ | |
1768 | if (sched_numa_topology_type == NUMA_GLUELESS_MESH) { | |
1769 | unsigned long score, max_score = 0; | |
1770 | int node, max_node = nid; | |
1771 | ||
1772 | dist = sched_max_numa_distance; | |
1773 | ||
1774 | for_each_online_node(node) { | |
1775 | score = group_weight(p, node, dist); | |
1776 | if (score > max_score) { | |
1777 | max_score = score; | |
1778 | max_node = node; | |
1779 | } | |
1780 | } | |
1781 | return max_node; | |
1782 | } | |
1783 | ||
1784 | /* | |
1785 | * Finding the preferred nid in a system with NUMA backplane | |
1786 | * interconnect topology is more involved. The goal is to locate | |
1787 | * tasks from numa_groups near each other in the system, and | |
1788 | * untangle workloads from different sides of the system. This requires | |
1789 | * searching down the hierarchy of node groups, recursively searching | |
1790 | * inside the highest scoring group of nodes. The nodemask tricks | |
1791 | * keep the complexity of the search down. | |
1792 | */ | |
1793 | nodes = node_online_map; | |
1794 | for (dist = sched_max_numa_distance; dist > LOCAL_DISTANCE; dist--) { | |
1795 | unsigned long max_faults = 0; | |
81907478 | 1796 | nodemask_t max_group = NODE_MASK_NONE; |
54009416 RR |
1797 | int a, b; |
1798 | ||
1799 | /* Are there nodes at this distance from each other? */ | |
1800 | if (!find_numa_distance(dist)) | |
1801 | continue; | |
1802 | ||
1803 | for_each_node_mask(a, nodes) { | |
1804 | unsigned long faults = 0; | |
1805 | nodemask_t this_group; | |
1806 | nodes_clear(this_group); | |
1807 | ||
1808 | /* Sum group's NUMA faults; includes a==b case. */ | |
1809 | for_each_node_mask(b, nodes) { | |
1810 | if (node_distance(a, b) < dist) { | |
1811 | faults += group_faults(p, b); | |
1812 | node_set(b, this_group); | |
1813 | node_clear(b, nodes); | |
1814 | } | |
1815 | } | |
1816 | ||
1817 | /* Remember the top group. */ | |
1818 | if (faults > max_faults) { | |
1819 | max_faults = faults; | |
1820 | max_group = this_group; | |
1821 | /* | |
1822 | * subtle: at the smallest distance there is | |
1823 | * just one node left in each "group", the | |
1824 | * winner is the preferred nid. | |
1825 | */ | |
1826 | nid = a; | |
1827 | } | |
1828 | } | |
1829 | /* Next round, evaluate the nodes within max_group. */ | |
890a5409 JB |
1830 | if (!max_faults) |
1831 | break; | |
54009416 RR |
1832 | nodes = max_group; |
1833 | } | |
1834 | return nid; | |
1835 | } | |
1836 | ||
cbee9f88 PZ |
1837 | static void task_numa_placement(struct task_struct *p) |
1838 | { | |
83e1d2cd MG |
1839 | int seq, nid, max_nid = -1, max_group_nid = -1; |
1840 | unsigned long max_faults = 0, max_group_faults = 0; | |
04bb2f94 | 1841 | unsigned long fault_types[2] = { 0, 0 }; |
7e2703e6 RR |
1842 | unsigned long total_faults; |
1843 | u64 runtime, period; | |
7dbd13ed | 1844 | spinlock_t *group_lock = NULL; |
cbee9f88 | 1845 | |
7e5a2c17 JL |
1846 | /* |
1847 | * The p->mm->numa_scan_seq field gets updated without | |
1848 | * exclusive access. Use READ_ONCE() here to ensure | |
1849 | * that the field is read in a single access: | |
1850 | */ | |
316c1608 | 1851 | seq = READ_ONCE(p->mm->numa_scan_seq); |
cbee9f88 PZ |
1852 | if (p->numa_scan_seq == seq) |
1853 | return; | |
1854 | p->numa_scan_seq = seq; | |
598f0ec0 | 1855 | p->numa_scan_period_max = task_scan_max(p); |
cbee9f88 | 1856 | |
7e2703e6 RR |
1857 | total_faults = p->numa_faults_locality[0] + |
1858 | p->numa_faults_locality[1]; | |
1859 | runtime = numa_get_avg_runtime(p, &period); | |
1860 | ||
7dbd13ed MG |
1861 | /* If the task is part of a group prevent parallel updates to group stats */ |
1862 | if (p->numa_group) { | |
1863 | group_lock = &p->numa_group->lock; | |
60e69eed | 1864 | spin_lock_irq(group_lock); |
7dbd13ed MG |
1865 | } |
1866 | ||
688b7585 MG |
1867 | /* Find the node with the highest number of faults */ |
1868 | for_each_online_node(nid) { | |
44dba3d5 IM |
1869 | /* Keep track of the offsets in numa_faults array */ |
1870 | int mem_idx, membuf_idx, cpu_idx, cpubuf_idx; | |
83e1d2cd | 1871 | unsigned long faults = 0, group_faults = 0; |
44dba3d5 | 1872 | int priv; |
745d6147 | 1873 | |
be1e4e76 | 1874 | for (priv = 0; priv < NR_NUMA_HINT_FAULT_TYPES; priv++) { |
7e2703e6 | 1875 | long diff, f_diff, f_weight; |
8c8a743c | 1876 | |
44dba3d5 IM |
1877 | mem_idx = task_faults_idx(NUMA_MEM, nid, priv); |
1878 | membuf_idx = task_faults_idx(NUMA_MEMBUF, nid, priv); | |
1879 | cpu_idx = task_faults_idx(NUMA_CPU, nid, priv); | |
1880 | cpubuf_idx = task_faults_idx(NUMA_CPUBUF, nid, priv); | |
745d6147 | 1881 | |
ac8e895b | 1882 | /* Decay existing window, copy faults since last scan */ |
44dba3d5 IM |
1883 | diff = p->numa_faults[membuf_idx] - p->numa_faults[mem_idx] / 2; |
1884 | fault_types[priv] += p->numa_faults[membuf_idx]; | |
1885 | p->numa_faults[membuf_idx] = 0; | |
fb13c7ee | 1886 | |
7e2703e6 RR |
1887 | /* |
1888 | * Normalize the faults_from, so all tasks in a group | |
1889 | * count according to CPU use, instead of by the raw | |
1890 | * number of faults. Tasks with little runtime have | |
1891 | * little over-all impact on throughput, and thus their | |
1892 | * faults are less important. | |
1893 | */ | |
1894 | f_weight = div64_u64(runtime << 16, period + 1); | |
44dba3d5 | 1895 | f_weight = (f_weight * p->numa_faults[cpubuf_idx]) / |
7e2703e6 | 1896 | (total_faults + 1); |
44dba3d5 IM |
1897 | f_diff = f_weight - p->numa_faults[cpu_idx] / 2; |
1898 | p->numa_faults[cpubuf_idx] = 0; | |
50ec8a40 | 1899 | |
44dba3d5 IM |
1900 | p->numa_faults[mem_idx] += diff; |
1901 | p->numa_faults[cpu_idx] += f_diff; | |
1902 | faults += p->numa_faults[mem_idx]; | |
83e1d2cd | 1903 | p->total_numa_faults += diff; |
8c8a743c | 1904 | if (p->numa_group) { |
44dba3d5 IM |
1905 | /* |
1906 | * safe because we can only change our own group | |
1907 | * | |
1908 | * mem_idx represents the offset for a given | |
1909 | * nid and priv in a specific region because it | |
1910 | * is at the beginning of the numa_faults array. | |
1911 | */ | |
1912 | p->numa_group->faults[mem_idx] += diff; | |
1913 | p->numa_group->faults_cpu[mem_idx] += f_diff; | |
989348b5 | 1914 | p->numa_group->total_faults += diff; |
44dba3d5 | 1915 | group_faults += p->numa_group->faults[mem_idx]; |
8c8a743c | 1916 | } |
ac8e895b MG |
1917 | } |
1918 | ||
688b7585 MG |
1919 | if (faults > max_faults) { |
1920 | max_faults = faults; | |
1921 | max_nid = nid; | |
1922 | } | |
83e1d2cd MG |
1923 | |
1924 | if (group_faults > max_group_faults) { | |
1925 | max_group_faults = group_faults; | |
1926 | max_group_nid = nid; | |
1927 | } | |
1928 | } | |
1929 | ||
04bb2f94 RR |
1930 | update_task_scan_period(p, fault_types[0], fault_types[1]); |
1931 | ||
7dbd13ed | 1932 | if (p->numa_group) { |
20e07dea | 1933 | update_numa_active_node_mask(p->numa_group); |
60e69eed | 1934 | spin_unlock_irq(group_lock); |
54009416 | 1935 | max_nid = preferred_group_nid(p, max_group_nid); |
688b7585 MG |
1936 | } |
1937 | ||
bb97fc31 RR |
1938 | if (max_faults) { |
1939 | /* Set the new preferred node */ | |
1940 | if (max_nid != p->numa_preferred_nid) | |
1941 | sched_setnuma(p, max_nid); | |
1942 | ||
1943 | if (task_node(p) != p->numa_preferred_nid) | |
1944 | numa_migrate_preferred(p); | |
3a7053b3 | 1945 | } |
cbee9f88 PZ |
1946 | } |
1947 | ||
8c8a743c PZ |
1948 | static inline int get_numa_group(struct numa_group *grp) |
1949 | { | |
1950 | return atomic_inc_not_zero(&grp->refcount); | |
1951 | } | |
1952 | ||
1953 | static inline void put_numa_group(struct numa_group *grp) | |
1954 | { | |
1955 | if (atomic_dec_and_test(&grp->refcount)) | |
1956 | kfree_rcu(grp, rcu); | |
1957 | } | |
1958 | ||
3e6a9418 MG |
1959 | static void task_numa_group(struct task_struct *p, int cpupid, int flags, |
1960 | int *priv) | |
8c8a743c PZ |
1961 | { |
1962 | struct numa_group *grp, *my_grp; | |
1963 | struct task_struct *tsk; | |
1964 | bool join = false; | |
1965 | int cpu = cpupid_to_cpu(cpupid); | |
1966 | int i; | |
1967 | ||
1968 | if (unlikely(!p->numa_group)) { | |
1969 | unsigned int size = sizeof(struct numa_group) + | |
50ec8a40 | 1970 | 4*nr_node_ids*sizeof(unsigned long); |
8c8a743c PZ |
1971 | |
1972 | grp = kzalloc(size, GFP_KERNEL | __GFP_NOWARN); | |
1973 | if (!grp) | |
1974 | return; | |
1975 | ||
1976 | atomic_set(&grp->refcount, 1); | |
1977 | spin_lock_init(&grp->lock); | |
e29cf08b | 1978 | grp->gid = p->pid; |
50ec8a40 | 1979 | /* Second half of the array tracks nids where faults happen */ |
be1e4e76 RR |
1980 | grp->faults_cpu = grp->faults + NR_NUMA_HINT_FAULT_TYPES * |
1981 | nr_node_ids; | |
8c8a743c | 1982 | |
20e07dea RR |
1983 | node_set(task_node(current), grp->active_nodes); |
1984 | ||
be1e4e76 | 1985 | for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) |
44dba3d5 | 1986 | grp->faults[i] = p->numa_faults[i]; |
8c8a743c | 1987 | |
989348b5 | 1988 | grp->total_faults = p->total_numa_faults; |
83e1d2cd | 1989 | |
8c8a743c PZ |
1990 | grp->nr_tasks++; |
1991 | rcu_assign_pointer(p->numa_group, grp); | |
1992 | } | |
1993 | ||
1994 | rcu_read_lock(); | |
316c1608 | 1995 | tsk = READ_ONCE(cpu_rq(cpu)->curr); |
8c8a743c PZ |
1996 | |
1997 | if (!cpupid_match_pid(tsk, cpupid)) | |
3354781a | 1998 | goto no_join; |
8c8a743c PZ |
1999 | |
2000 | grp = rcu_dereference(tsk->numa_group); | |
2001 | if (!grp) | |
3354781a | 2002 | goto no_join; |
8c8a743c PZ |
2003 | |
2004 | my_grp = p->numa_group; | |
2005 | if (grp == my_grp) | |
3354781a | 2006 | goto no_join; |
8c8a743c PZ |
2007 | |
2008 | /* | |
2009 | * Only join the other group if its bigger; if we're the bigger group, | |
2010 | * the other task will join us. | |
2011 | */ | |
2012 | if (my_grp->nr_tasks > grp->nr_tasks) | |
3354781a | 2013 | goto no_join; |
8c8a743c PZ |
2014 | |
2015 | /* | |
2016 | * Tie-break on the grp address. | |
2017 | */ | |
2018 | if (my_grp->nr_tasks == grp->nr_tasks && my_grp > grp) | |
3354781a | 2019 | goto no_join; |
8c8a743c | 2020 | |
dabe1d99 RR |
2021 | /* Always join threads in the same process. */ |
2022 | if (tsk->mm == current->mm) | |
2023 | join = true; | |
2024 | ||
2025 | /* Simple filter to avoid false positives due to PID collisions */ | |
2026 | if (flags & TNF_SHARED) | |
2027 | join = true; | |
8c8a743c | 2028 | |
3e6a9418 MG |
2029 | /* Update priv based on whether false sharing was detected */ |
2030 | *priv = !join; | |
2031 | ||
dabe1d99 | 2032 | if (join && !get_numa_group(grp)) |
3354781a | 2033 | goto no_join; |
8c8a743c | 2034 | |
8c8a743c PZ |
2035 | rcu_read_unlock(); |
2036 | ||
2037 | if (!join) | |
2038 | return; | |
2039 | ||
60e69eed MG |
2040 | BUG_ON(irqs_disabled()); |
2041 | double_lock_irq(&my_grp->lock, &grp->lock); | |
989348b5 | 2042 | |
be1e4e76 | 2043 | for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) { |
44dba3d5 IM |
2044 | my_grp->faults[i] -= p->numa_faults[i]; |
2045 | grp->faults[i] += p->numa_faults[i]; | |
8c8a743c | 2046 | } |
989348b5 MG |
2047 | my_grp->total_faults -= p->total_numa_faults; |
2048 | grp->total_faults += p->total_numa_faults; | |
8c8a743c | 2049 | |
8c8a743c PZ |
2050 | my_grp->nr_tasks--; |
2051 | grp->nr_tasks++; | |
2052 | ||
2053 | spin_unlock(&my_grp->lock); | |
60e69eed | 2054 | spin_unlock_irq(&grp->lock); |
8c8a743c PZ |
2055 | |
2056 | rcu_assign_pointer(p->numa_group, grp); | |
2057 | ||
2058 | put_numa_group(my_grp); | |
3354781a PZ |
2059 | return; |
2060 | ||
2061 | no_join: | |
2062 | rcu_read_unlock(); | |
2063 | return; | |
8c8a743c PZ |
2064 | } |
2065 | ||
2066 | void task_numa_free(struct task_struct *p) | |
2067 | { | |
2068 | struct numa_group *grp = p->numa_group; | |
44dba3d5 | 2069 | void *numa_faults = p->numa_faults; |
e9dd685c SR |
2070 | unsigned long flags; |
2071 | int i; | |
8c8a743c PZ |
2072 | |
2073 | if (grp) { | |
e9dd685c | 2074 | spin_lock_irqsave(&grp->lock, flags); |
be1e4e76 | 2075 | for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) |
44dba3d5 | 2076 | grp->faults[i] -= p->numa_faults[i]; |
989348b5 | 2077 | grp->total_faults -= p->total_numa_faults; |
83e1d2cd | 2078 | |
8c8a743c | 2079 | grp->nr_tasks--; |
e9dd685c | 2080 | spin_unlock_irqrestore(&grp->lock, flags); |
35b123e2 | 2081 | RCU_INIT_POINTER(p->numa_group, NULL); |
8c8a743c PZ |
2082 | put_numa_group(grp); |
2083 | } | |
2084 | ||
44dba3d5 | 2085 | p->numa_faults = NULL; |
82727018 | 2086 | kfree(numa_faults); |
8c8a743c PZ |
2087 | } |
2088 | ||
cbee9f88 PZ |
2089 | /* |
2090 | * Got a PROT_NONE fault for a page on @node. | |
2091 | */ | |
58b46da3 | 2092 | void task_numa_fault(int last_cpupid, int mem_node, int pages, int flags) |
cbee9f88 PZ |
2093 | { |
2094 | struct task_struct *p = current; | |
6688cc05 | 2095 | bool migrated = flags & TNF_MIGRATED; |
58b46da3 | 2096 | int cpu_node = task_node(current); |
792568ec | 2097 | int local = !!(flags & TNF_FAULT_LOCAL); |
ac8e895b | 2098 | int priv; |
cbee9f88 | 2099 | |
2a595721 | 2100 | if (!static_branch_likely(&sched_numa_balancing)) |
1a687c2e MG |
2101 | return; |
2102 | ||
9ff1d9ff MG |
2103 | /* for example, ksmd faulting in a user's mm */ |
2104 | if (!p->mm) | |
2105 | return; | |
2106 | ||
f809ca9a | 2107 | /* Allocate buffer to track faults on a per-node basis */ |
44dba3d5 IM |
2108 | if (unlikely(!p->numa_faults)) { |
2109 | int size = sizeof(*p->numa_faults) * | |
be1e4e76 | 2110 | NR_NUMA_HINT_FAULT_BUCKETS * nr_node_ids; |
f809ca9a | 2111 | |
44dba3d5 IM |
2112 | p->numa_faults = kzalloc(size, GFP_KERNEL|__GFP_NOWARN); |
2113 | if (!p->numa_faults) | |
f809ca9a | 2114 | return; |
745d6147 | 2115 | |
83e1d2cd | 2116 | p->total_numa_faults = 0; |
04bb2f94 | 2117 | memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); |
f809ca9a | 2118 | } |
cbee9f88 | 2119 | |
8c8a743c PZ |
2120 | /* |
2121 | * First accesses are treated as private, otherwise consider accesses | |
2122 | * to be private if the accessing pid has not changed | |
2123 | */ | |
2124 | if (unlikely(last_cpupid == (-1 & LAST_CPUPID_MASK))) { | |
2125 | priv = 1; | |
2126 | } else { | |
2127 | priv = cpupid_match_pid(p, last_cpupid); | |
6688cc05 | 2128 | if (!priv && !(flags & TNF_NO_GROUP)) |
3e6a9418 | 2129 | task_numa_group(p, last_cpupid, flags, &priv); |
8c8a743c PZ |
2130 | } |
2131 | ||
792568ec RR |
2132 | /* |
2133 | * If a workload spans multiple NUMA nodes, a shared fault that | |
2134 | * occurs wholly within the set of nodes that the workload is | |
2135 | * actively using should be counted as local. This allows the | |
2136 | * scan rate to slow down when a workload has settled down. | |
2137 | */ | |
2138 | if (!priv && !local && p->numa_group && | |
2139 | node_isset(cpu_node, p->numa_group->active_nodes) && | |
2140 | node_isset(mem_node, p->numa_group->active_nodes)) | |
2141 | local = 1; | |
2142 | ||
cbee9f88 | 2143 | task_numa_placement(p); |
f809ca9a | 2144 | |
2739d3ee RR |
2145 | /* |
2146 | * Retry task to preferred node migration periodically, in case it | |
2147 | * case it previously failed, or the scheduler moved us. | |
2148 | */ | |
2149 | if (time_after(jiffies, p->numa_migrate_retry)) | |
6b9a7460 MG |
2150 | numa_migrate_preferred(p); |
2151 | ||
b32e86b4 IM |
2152 | if (migrated) |
2153 | p->numa_pages_migrated += pages; | |
074c2381 MG |
2154 | if (flags & TNF_MIGRATE_FAIL) |
2155 | p->numa_faults_locality[2] += pages; | |
b32e86b4 | 2156 | |
44dba3d5 IM |
2157 | p->numa_faults[task_faults_idx(NUMA_MEMBUF, mem_node, priv)] += pages; |
2158 | p->numa_faults[task_faults_idx(NUMA_CPUBUF, cpu_node, priv)] += pages; | |
792568ec | 2159 | p->numa_faults_locality[local] += pages; |
cbee9f88 PZ |
2160 | } |
2161 | ||
6e5fb223 PZ |
2162 | static void reset_ptenuma_scan(struct task_struct *p) |
2163 | { | |
7e5a2c17 JL |
2164 | /* |
2165 | * We only did a read acquisition of the mmap sem, so | |
2166 | * p->mm->numa_scan_seq is written to without exclusive access | |
2167 | * and the update is not guaranteed to be atomic. That's not | |
2168 | * much of an issue though, since this is just used for | |
2169 | * statistical sampling. Use READ_ONCE/WRITE_ONCE, which are not | |
2170 | * expensive, to avoid any form of compiler optimizations: | |
2171 | */ | |
316c1608 | 2172 | WRITE_ONCE(p->mm->numa_scan_seq, READ_ONCE(p->mm->numa_scan_seq) + 1); |
6e5fb223 PZ |
2173 | p->mm->numa_scan_offset = 0; |
2174 | } | |
2175 | ||
cbee9f88 PZ |
2176 | /* |
2177 | * The expensive part of numa migration is done from task_work context. | |
2178 | * Triggered from task_tick_numa(). | |
2179 | */ | |
2180 | void task_numa_work(struct callback_head *work) | |
2181 | { | |
2182 | unsigned long migrate, next_scan, now = jiffies; | |
2183 | struct task_struct *p = current; | |
2184 | struct mm_struct *mm = p->mm; | |
51170840 | 2185 | u64 runtime = p->se.sum_exec_runtime; |
6e5fb223 | 2186 | struct vm_area_struct *vma; |
9f40604c | 2187 | unsigned long start, end; |
598f0ec0 | 2188 | unsigned long nr_pte_updates = 0; |
4620f8c1 | 2189 | long pages, virtpages; |
cbee9f88 PZ |
2190 | |
2191 | WARN_ON_ONCE(p != container_of(work, struct task_struct, numa_work)); | |
2192 | ||
2193 | work->next = work; /* protect against double add */ | |
2194 | /* | |
2195 | * Who cares about NUMA placement when they're dying. | |
2196 | * | |
2197 | * NOTE: make sure not to dereference p->mm before this check, | |
2198 | * exit_task_work() happens _after_ exit_mm() so we could be called | |
2199 | * without p->mm even though we still had it when we enqueued this | |
2200 | * work. | |
2201 | */ | |
2202 | if (p->flags & PF_EXITING) | |
2203 | return; | |
2204 | ||
930aa174 | 2205 | if (!mm->numa_next_scan) { |
7e8d16b6 MG |
2206 | mm->numa_next_scan = now + |
2207 | msecs_to_jiffies(sysctl_numa_balancing_scan_delay); | |
b8593bfd MG |
2208 | } |
2209 | ||
cbee9f88 PZ |
2210 | /* |
2211 | * Enforce maximal scan/migration frequency.. | |
2212 | */ | |
2213 | migrate = mm->numa_next_scan; | |
2214 | if (time_before(now, migrate)) | |
2215 | return; | |
2216 | ||
598f0ec0 MG |
2217 | if (p->numa_scan_period == 0) { |
2218 | p->numa_scan_period_max = task_scan_max(p); | |
2219 | p->numa_scan_period = task_scan_min(p); | |
2220 | } | |
cbee9f88 | 2221 | |
fb003b80 | 2222 | next_scan = now + msecs_to_jiffies(p->numa_scan_period); |
cbee9f88 PZ |
2223 | if (cmpxchg(&mm->numa_next_scan, migrate, next_scan) != migrate) |
2224 | return; | |
2225 | ||
19a78d11 PZ |
2226 | /* |
2227 | * Delay this task enough that another task of this mm will likely win | |
2228 | * the next time around. | |
2229 | */ | |
2230 | p->node_stamp += 2 * TICK_NSEC; | |
2231 | ||
9f40604c MG |
2232 | start = mm->numa_scan_offset; |
2233 | pages = sysctl_numa_balancing_scan_size; | |
2234 | pages <<= 20 - PAGE_SHIFT; /* MB in pages */ | |
4620f8c1 | 2235 | virtpages = pages * 8; /* Scan up to this much virtual space */ |
9f40604c MG |
2236 | if (!pages) |
2237 | return; | |
cbee9f88 | 2238 | |
4620f8c1 | 2239 | |
6e5fb223 | 2240 | down_read(&mm->mmap_sem); |
9f40604c | 2241 | vma = find_vma(mm, start); |
6e5fb223 PZ |
2242 | if (!vma) { |
2243 | reset_ptenuma_scan(p); | |
9f40604c | 2244 | start = 0; |
6e5fb223 PZ |
2245 | vma = mm->mmap; |
2246 | } | |
9f40604c | 2247 | for (; vma; vma = vma->vm_next) { |
6b79c57b | 2248 | if (!vma_migratable(vma) || !vma_policy_mof(vma) || |
8e76d4ee | 2249 | is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_MIXEDMAP)) { |
6e5fb223 | 2250 | continue; |
6b79c57b | 2251 | } |
6e5fb223 | 2252 | |
4591ce4f MG |
2253 | /* |
2254 | * Shared library pages mapped by multiple processes are not | |
2255 | * migrated as it is expected they are cache replicated. Avoid | |
2256 | * hinting faults in read-only file-backed mappings or the vdso | |
2257 | * as migrating the pages will be of marginal benefit. | |
2258 | */ | |
2259 | if (!vma->vm_mm || | |
2260 | (vma->vm_file && (vma->vm_flags & (VM_READ|VM_WRITE)) == (VM_READ))) | |
2261 | continue; | |
2262 | ||
3c67f474 MG |
2263 | /* |
2264 | * Skip inaccessible VMAs to avoid any confusion between | |
2265 | * PROT_NONE and NUMA hinting ptes | |
2266 | */ | |
2267 | if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))) | |
2268 | continue; | |
4591ce4f | 2269 | |
9f40604c MG |
2270 | do { |
2271 | start = max(start, vma->vm_start); | |
2272 | end = ALIGN(start + (pages << PAGE_SHIFT), HPAGE_SIZE); | |
2273 | end = min(end, vma->vm_end); | |
4620f8c1 | 2274 | nr_pte_updates = change_prot_numa(vma, start, end); |
598f0ec0 MG |
2275 | |
2276 | /* | |
4620f8c1 RR |
2277 | * Try to scan sysctl_numa_balancing_size worth of |
2278 | * hpages that have at least one present PTE that | |
2279 | * is not already pte-numa. If the VMA contains | |
2280 | * areas that are unused or already full of prot_numa | |
2281 | * PTEs, scan up to virtpages, to skip through those | |
2282 | * areas faster. | |
598f0ec0 MG |
2283 | */ |
2284 | if (nr_pte_updates) | |
2285 | pages -= (end - start) >> PAGE_SHIFT; | |
4620f8c1 | 2286 | virtpages -= (end - start) >> PAGE_SHIFT; |
6e5fb223 | 2287 | |
9f40604c | 2288 | start = end; |
4620f8c1 | 2289 | if (pages <= 0 || virtpages <= 0) |
9f40604c | 2290 | goto out; |
3cf1962c RR |
2291 | |
2292 | cond_resched(); | |
9f40604c | 2293 | } while (end != vma->vm_end); |
cbee9f88 | 2294 | } |
6e5fb223 | 2295 | |
9f40604c | 2296 | out: |
6e5fb223 | 2297 | /* |
c69307d5 PZ |
2298 | * It is possible to reach the end of the VMA list but the last few |
2299 | * VMAs are not guaranteed to the vma_migratable. If they are not, we | |
2300 | * would find the !migratable VMA on the next scan but not reset the | |
2301 | * scanner to the start so check it now. | |
6e5fb223 PZ |
2302 | */ |
2303 | if (vma) | |
9f40604c | 2304 | mm->numa_scan_offset = start; |
6e5fb223 PZ |
2305 | else |
2306 | reset_ptenuma_scan(p); | |
2307 | up_read(&mm->mmap_sem); | |
51170840 RR |
2308 | |
2309 | /* | |
2310 | * Make sure tasks use at least 32x as much time to run other code | |
2311 | * than they used here, to limit NUMA PTE scanning overhead to 3% max. | |
2312 | * Usually update_task_scan_period slows down scanning enough; on an | |
2313 | * overloaded system we need to limit overhead on a per task basis. | |
2314 | */ | |
2315 | if (unlikely(p->se.sum_exec_runtime != runtime)) { | |
2316 | u64 diff = p->se.sum_exec_runtime - runtime; | |
2317 | p->node_stamp += 32 * diff; | |
2318 | } | |
cbee9f88 PZ |
2319 | } |
2320 | ||
2321 | /* | |
2322 | * Drive the periodic memory faults.. | |
2323 | */ | |
2324 | void task_tick_numa(struct rq *rq, struct task_struct *curr) | |
2325 | { | |
2326 | struct callback_head *work = &curr->numa_work; | |
2327 | u64 period, now; | |
2328 | ||
2329 | /* | |
2330 | * We don't care about NUMA placement if we don't have memory. | |
2331 | */ | |
2332 | if (!curr->mm || (curr->flags & PF_EXITING) || work->next != work) | |
2333 | return; | |
2334 | ||
2335 | /* | |
2336 | * Using runtime rather than walltime has the dual advantage that | |
2337 | * we (mostly) drive the selection from busy threads and that the | |
2338 | * task needs to have done some actual work before we bother with | |
2339 | * NUMA placement. | |
2340 | */ | |
2341 | now = curr->se.sum_exec_runtime; | |
2342 | period = (u64)curr->numa_scan_period * NSEC_PER_MSEC; | |
2343 | ||
25b3e5a3 | 2344 | if (now > curr->node_stamp + period) { |
4b96a29b | 2345 | if (!curr->node_stamp) |
598f0ec0 | 2346 | curr->numa_scan_period = task_scan_min(curr); |
19a78d11 | 2347 | curr->node_stamp += period; |
cbee9f88 PZ |
2348 | |
2349 | if (!time_before(jiffies, curr->mm->numa_next_scan)) { | |
2350 | init_task_work(work, task_numa_work); /* TODO: move this into sched_fork() */ | |
2351 | task_work_add(curr, work, true); | |
2352 | } | |
2353 | } | |
2354 | } | |
2355 | #else | |
2356 | static void task_tick_numa(struct rq *rq, struct task_struct *curr) | |
2357 | { | |
2358 | } | |
0ec8aa00 PZ |
2359 | |
2360 | static inline void account_numa_enqueue(struct rq *rq, struct task_struct *p) | |
2361 | { | |
2362 | } | |
2363 | ||
2364 | static inline void account_numa_dequeue(struct rq *rq, struct task_struct *p) | |
2365 | { | |
2366 | } | |
cbee9f88 PZ |
2367 | #endif /* CONFIG_NUMA_BALANCING */ |
2368 | ||
30cfdcfc DA |
2369 | static void |
2370 | account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
2371 | { | |
2372 | update_load_add(&cfs_rq->load, se->load.weight); | |
c09595f6 | 2373 | if (!parent_entity(se)) |
029632fb | 2374 | update_load_add(&rq_of(cfs_rq)->load, se->load.weight); |
367456c7 | 2375 | #ifdef CONFIG_SMP |
0ec8aa00 PZ |
2376 | if (entity_is_task(se)) { |
2377 | struct rq *rq = rq_of(cfs_rq); | |
2378 | ||
2379 | account_numa_enqueue(rq, task_of(se)); | |
2380 | list_add(&se->group_node, &rq->cfs_tasks); | |
2381 | } | |
367456c7 | 2382 | #endif |
30cfdcfc | 2383 | cfs_rq->nr_running++; |
30cfdcfc DA |
2384 | } |
2385 | ||
2386 | static void | |
2387 | account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
2388 | { | |
2389 | update_load_sub(&cfs_rq->load, se->load.weight); | |
c09595f6 | 2390 | if (!parent_entity(se)) |
029632fb | 2391 | update_load_sub(&rq_of(cfs_rq)->load, se->load.weight); |
0ec8aa00 PZ |
2392 | if (entity_is_task(se)) { |
2393 | account_numa_dequeue(rq_of(cfs_rq), task_of(se)); | |
b87f1724 | 2394 | list_del_init(&se->group_node); |
0ec8aa00 | 2395 | } |
30cfdcfc | 2396 | cfs_rq->nr_running--; |
30cfdcfc DA |
2397 | } |
2398 | ||
3ff6dcac YZ |
2399 | #ifdef CONFIG_FAIR_GROUP_SCHED |
2400 | # ifdef CONFIG_SMP | |
cf5f0acf PZ |
2401 | static inline long calc_tg_weight(struct task_group *tg, struct cfs_rq *cfs_rq) |
2402 | { | |
2403 | long tg_weight; | |
2404 | ||
2405 | /* | |
9d89c257 YD |
2406 | * Use this CPU's real-time load instead of the last load contribution |
2407 | * as the updating of the contribution is delayed, and we will use the | |
2408 | * the real-time load to calc the share. See update_tg_load_avg(). | |
cf5f0acf | 2409 | */ |
bf5b986e | 2410 | tg_weight = atomic_long_read(&tg->load_avg); |
9d89c257 | 2411 | tg_weight -= cfs_rq->tg_load_avg_contrib; |
fde7d22e | 2412 | tg_weight += cfs_rq->load.weight; |
cf5f0acf PZ |
2413 | |
2414 | return tg_weight; | |
2415 | } | |
2416 | ||
6d5ab293 | 2417 | static long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg) |
3ff6dcac | 2418 | { |
cf5f0acf | 2419 | long tg_weight, load, shares; |
3ff6dcac | 2420 | |
cf5f0acf | 2421 | tg_weight = calc_tg_weight(tg, cfs_rq); |
fde7d22e | 2422 | load = cfs_rq->load.weight; |
3ff6dcac | 2423 | |
3ff6dcac | 2424 | shares = (tg->shares * load); |
cf5f0acf PZ |
2425 | if (tg_weight) |
2426 | shares /= tg_weight; | |
3ff6dcac YZ |
2427 | |
2428 | if (shares < MIN_SHARES) | |
2429 | shares = MIN_SHARES; | |
2430 | if (shares > tg->shares) | |
2431 | shares = tg->shares; | |
2432 | ||
2433 | return shares; | |
2434 | } | |
3ff6dcac | 2435 | # else /* CONFIG_SMP */ |
6d5ab293 | 2436 | static inline long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg) |
3ff6dcac YZ |
2437 | { |
2438 | return tg->shares; | |
2439 | } | |
3ff6dcac | 2440 | # endif /* CONFIG_SMP */ |
2069dd75 PZ |
2441 | static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, |
2442 | unsigned long weight) | |
2443 | { | |
19e5eebb PT |
2444 | if (se->on_rq) { |
2445 | /* commit outstanding execution time */ | |
2446 | if (cfs_rq->curr == se) | |
2447 | update_curr(cfs_rq); | |
2069dd75 | 2448 | account_entity_dequeue(cfs_rq, se); |
19e5eebb | 2449 | } |
2069dd75 PZ |
2450 | |
2451 | update_load_set(&se->load, weight); | |
2452 | ||
2453 | if (se->on_rq) | |
2454 | account_entity_enqueue(cfs_rq, se); | |
2455 | } | |
2456 | ||
82958366 PT |
2457 | static inline int throttled_hierarchy(struct cfs_rq *cfs_rq); |
2458 | ||
6d5ab293 | 2459 | static void update_cfs_shares(struct cfs_rq *cfs_rq) |
2069dd75 PZ |
2460 | { |
2461 | struct task_group *tg; | |
2462 | struct sched_entity *se; | |
3ff6dcac | 2463 | long shares; |
2069dd75 | 2464 | |
2069dd75 PZ |
2465 | tg = cfs_rq->tg; |
2466 | se = tg->se[cpu_of(rq_of(cfs_rq))]; | |
64660c86 | 2467 | if (!se || throttled_hierarchy(cfs_rq)) |
2069dd75 | 2468 | return; |
3ff6dcac YZ |
2469 | #ifndef CONFIG_SMP |
2470 | if (likely(se->load.weight == tg->shares)) | |
2471 | return; | |
2472 | #endif | |
6d5ab293 | 2473 | shares = calc_cfs_shares(cfs_rq, tg); |
2069dd75 PZ |
2474 | |
2475 | reweight_entity(cfs_rq_of(se), se, shares); | |
2476 | } | |
2477 | #else /* CONFIG_FAIR_GROUP_SCHED */ | |
6d5ab293 | 2478 | static inline void update_cfs_shares(struct cfs_rq *cfs_rq) |
2069dd75 PZ |
2479 | { |
2480 | } | |
2481 | #endif /* CONFIG_FAIR_GROUP_SCHED */ | |
2482 | ||
141965c7 | 2483 | #ifdef CONFIG_SMP |
5b51f2f8 PT |
2484 | /* Precomputed fixed inverse multiplies for multiplication by y^n */ |
2485 | static const u32 runnable_avg_yN_inv[] = { | |
2486 | 0xffffffff, 0xfa83b2da, 0xf5257d14, 0xefe4b99a, 0xeac0c6e6, 0xe5b906e6, | |
2487 | 0xe0ccdeeb, 0xdbfbb796, 0xd744fcc9, 0xd2a81d91, 0xce248c14, 0xc9b9bd85, | |
2488 | 0xc5672a10, 0xc12c4cc9, 0xbd08a39e, 0xb8fbaf46, 0xb504f333, 0xb123f581, | |
2489 | 0xad583ee9, 0xa9a15ab4, 0xa5fed6a9, 0xa2704302, 0x9ef5325f, 0x9b8d39b9, | |
2490 | 0x9837f050, 0x94f4efa8, 0x91c3d373, 0x8ea4398a, 0x8b95c1e3, 0x88980e80, | |
2491 | 0x85aac367, 0x82cd8698, | |
2492 | }; | |
2493 | ||
2494 | /* | |
2495 | * Precomputed \Sum y^k { 1<=k<=n }. These are floor(true_value) to prevent | |
2496 | * over-estimates when re-combining. | |
2497 | */ | |
2498 | static const u32 runnable_avg_yN_sum[] = { | |
2499 | 0, 1002, 1982, 2941, 3880, 4798, 5697, 6576, 7437, 8279, 9103, | |
2500 | 9909,10698,11470,12226,12966,13690,14398,15091,15769,16433,17082, | |
2501 | 17718,18340,18949,19545,20128,20698,21256,21802,22336,22859,23371, | |
2502 | }; | |
2503 | ||
9d85f21c PT |
2504 | /* |
2505 | * Approximate: | |
2506 | * val * y^n, where y^32 ~= 0.5 (~1 scheduling period) | |
2507 | */ | |
2508 | static __always_inline u64 decay_load(u64 val, u64 n) | |
2509 | { | |
5b51f2f8 PT |
2510 | unsigned int local_n; |
2511 | ||
2512 | if (!n) | |
2513 | return val; | |
2514 | else if (unlikely(n > LOAD_AVG_PERIOD * 63)) | |
2515 | return 0; | |
2516 | ||
2517 | /* after bounds checking we can collapse to 32-bit */ | |
2518 | local_n = n; | |
2519 | ||
2520 | /* | |
2521 | * As y^PERIOD = 1/2, we can combine | |
9c58c79a ZZ |
2522 | * y^n = 1/2^(n/PERIOD) * y^(n%PERIOD) |
2523 | * With a look-up table which covers y^n (n<PERIOD) | |
5b51f2f8 PT |
2524 | * |
2525 | * To achieve constant time decay_load. | |
2526 | */ | |
2527 | if (unlikely(local_n >= LOAD_AVG_PERIOD)) { | |
2528 | val >>= local_n / LOAD_AVG_PERIOD; | |
2529 | local_n %= LOAD_AVG_PERIOD; | |
9d85f21c PT |
2530 | } |
2531 | ||
9d89c257 YD |
2532 | val = mul_u64_u32_shr(val, runnable_avg_yN_inv[local_n], 32); |
2533 | return val; | |
5b51f2f8 PT |
2534 | } |
2535 | ||
2536 | /* | |
2537 | * For updates fully spanning n periods, the contribution to runnable | |
2538 | * average will be: \Sum 1024*y^n | |
2539 | * | |
2540 | * We can compute this reasonably efficiently by combining: | |
2541 | * y^PERIOD = 1/2 with precomputed \Sum 1024*y^n {for n <PERIOD} | |
2542 | */ | |
2543 | static u32 __compute_runnable_contrib(u64 n) | |
2544 | { | |
2545 | u32 contrib = 0; | |
2546 | ||
2547 | if (likely(n <= LOAD_AVG_PERIOD)) | |
2548 | return runnable_avg_yN_sum[n]; | |
2549 | else if (unlikely(n >= LOAD_AVG_MAX_N)) | |
2550 | return LOAD_AVG_MAX; | |
2551 | ||
2552 | /* Compute \Sum k^n combining precomputed values for k^i, \Sum k^j */ | |
2553 | do { | |
2554 | contrib /= 2; /* y^LOAD_AVG_PERIOD = 1/2 */ | |
2555 | contrib += runnable_avg_yN_sum[LOAD_AVG_PERIOD]; | |
2556 | ||
2557 | n -= LOAD_AVG_PERIOD; | |
2558 | } while (n > LOAD_AVG_PERIOD); | |
2559 | ||
2560 | contrib = decay_load(contrib, n); | |
2561 | return contrib + runnable_avg_yN_sum[n]; | |
9d85f21c PT |
2562 | } |
2563 | ||
006cdf02 PZ |
2564 | #if (SCHED_LOAD_SHIFT - SCHED_LOAD_RESOLUTION) != 10 || SCHED_CAPACITY_SHIFT != 10 |
2565 | #error "load tracking assumes 2^10 as unit" | |
2566 | #endif | |
2567 | ||
54a21385 | 2568 | #define cap_scale(v, s) ((v)*(s) >> SCHED_CAPACITY_SHIFT) |
e0f5f3af | 2569 | |
9d85f21c PT |
2570 | /* |
2571 | * We can represent the historical contribution to runnable average as the | |
2572 | * coefficients of a geometric series. To do this we sub-divide our runnable | |
2573 | * history into segments of approximately 1ms (1024us); label the segment that | |
2574 | * occurred N-ms ago p_N, with p_0 corresponding to the current period, e.g. | |
2575 | * | |
2576 | * [<- 1024us ->|<- 1024us ->|<- 1024us ->| ... | |
2577 | * p0 p1 p2 | |
2578 | * (now) (~1ms ago) (~2ms ago) | |
2579 | * | |
2580 | * Let u_i denote the fraction of p_i that the entity was runnable. | |
2581 | * | |
2582 | * We then designate the fractions u_i as our co-efficients, yielding the | |
2583 | * following representation of historical load: | |
2584 | * u_0 + u_1*y + u_2*y^2 + u_3*y^3 + ... | |
2585 | * | |
2586 | * We choose y based on the with of a reasonably scheduling period, fixing: | |
2587 | * y^32 = 0.5 | |
2588 | * | |
2589 | * This means that the contribution to load ~32ms ago (u_32) will be weighted | |
2590 | * approximately half as much as the contribution to load within the last ms | |
2591 | * (u_0). | |
2592 | * | |
2593 | * When a period "rolls over" and we have new u_0`, multiplying the previous | |
2594 | * sum again by y is sufficient to update: | |
2595 | * load_avg = u_0` + y*(u_0 + u_1*y + u_2*y^2 + ... ) | |
2596 | * = u_0 + u_1*y + u_2*y^2 + ... [re-labeling u_i --> u_{i+1}] | |
2597 | */ | |
9d89c257 YD |
2598 | static __always_inline int |
2599 | __update_load_avg(u64 now, int cpu, struct sched_avg *sa, | |
13962234 | 2600 | unsigned long weight, int running, struct cfs_rq *cfs_rq) |
9d85f21c | 2601 | { |
e0f5f3af | 2602 | u64 delta, scaled_delta, periods; |
9d89c257 | 2603 | u32 contrib; |
6115c793 | 2604 | unsigned int delta_w, scaled_delta_w, decayed = 0; |
6f2b0452 | 2605 | unsigned long scale_freq, scale_cpu; |
9d85f21c | 2606 | |
9d89c257 | 2607 | delta = now - sa->last_update_time; |
9d85f21c PT |
2608 | /* |
2609 | * This should only happen when time goes backwards, which it | |
2610 | * unfortunately does during sched clock init when we swap over to TSC. | |
2611 | */ | |
2612 | if ((s64)delta < 0) { | |
9d89c257 | 2613 | sa->last_update_time = now; |
9d85f21c PT |
2614 | return 0; |
2615 | } | |
2616 | ||
2617 | /* | |
2618 | * Use 1024ns as the unit of measurement since it's a reasonable | |
2619 | * approximation of 1us and fast to compute. | |
2620 | */ | |
2621 | delta >>= 10; | |
2622 | if (!delta) | |
2623 | return 0; | |
9d89c257 | 2624 | sa->last_update_time = now; |
9d85f21c | 2625 | |
6f2b0452 DE |
2626 | scale_freq = arch_scale_freq_capacity(NULL, cpu); |
2627 | scale_cpu = arch_scale_cpu_capacity(NULL, cpu); | |
2628 | ||
9d85f21c | 2629 | /* delta_w is the amount already accumulated against our next period */ |
9d89c257 | 2630 | delta_w = sa->period_contrib; |
9d85f21c | 2631 | if (delta + delta_w >= 1024) { |
9d85f21c PT |
2632 | decayed = 1; |
2633 | ||
9d89c257 YD |
2634 | /* how much left for next period will start over, we don't know yet */ |
2635 | sa->period_contrib = 0; | |
2636 | ||
9d85f21c PT |
2637 | /* |
2638 | * Now that we know we're crossing a period boundary, figure | |
2639 | * out how much from delta we need to complete the current | |
2640 | * period and accrue it. | |
2641 | */ | |
2642 | delta_w = 1024 - delta_w; | |
54a21385 | 2643 | scaled_delta_w = cap_scale(delta_w, scale_freq); |
13962234 | 2644 | if (weight) { |
e0f5f3af DE |
2645 | sa->load_sum += weight * scaled_delta_w; |
2646 | if (cfs_rq) { | |
2647 | cfs_rq->runnable_load_sum += | |
2648 | weight * scaled_delta_w; | |
2649 | } | |
13962234 | 2650 | } |
36ee28e4 | 2651 | if (running) |
006cdf02 | 2652 | sa->util_sum += scaled_delta_w * scale_cpu; |
5b51f2f8 PT |
2653 | |
2654 | delta -= delta_w; | |
2655 | ||
2656 | /* Figure out how many additional periods this update spans */ | |
2657 | periods = delta / 1024; | |
2658 | delta %= 1024; | |
2659 | ||
9d89c257 | 2660 | sa->load_sum = decay_load(sa->load_sum, periods + 1); |
13962234 YD |
2661 | if (cfs_rq) { |
2662 | cfs_rq->runnable_load_sum = | |
2663 | decay_load(cfs_rq->runnable_load_sum, periods + 1); | |
2664 | } | |
9d89c257 | 2665 | sa->util_sum = decay_load((u64)(sa->util_sum), periods + 1); |
5b51f2f8 PT |
2666 | |
2667 | /* Efficiently calculate \sum (1..n_period) 1024*y^i */ | |
9d89c257 | 2668 | contrib = __compute_runnable_contrib(periods); |
54a21385 | 2669 | contrib = cap_scale(contrib, scale_freq); |
13962234 | 2670 | if (weight) { |
9d89c257 | 2671 | sa->load_sum += weight * contrib; |
13962234 YD |
2672 | if (cfs_rq) |
2673 | cfs_rq->runnable_load_sum += weight * contrib; | |
2674 | } | |
36ee28e4 | 2675 | if (running) |
006cdf02 | 2676 | sa->util_sum += contrib * scale_cpu; |
9d85f21c PT |
2677 | } |
2678 | ||
2679 | /* Remainder of delta accrued against u_0` */ | |
54a21385 | 2680 | scaled_delta = cap_scale(delta, scale_freq); |
13962234 | 2681 | if (weight) { |
e0f5f3af | 2682 | sa->load_sum += weight * scaled_delta; |
13962234 | 2683 | if (cfs_rq) |
e0f5f3af | 2684 | cfs_rq->runnable_load_sum += weight * scaled_delta; |
13962234 | 2685 | } |
36ee28e4 | 2686 | if (running) |
006cdf02 | 2687 | sa->util_sum += scaled_delta * scale_cpu; |
9ee474f5 | 2688 | |
9d89c257 | 2689 | sa->period_contrib += delta; |
9ee474f5 | 2690 | |
9d89c257 YD |
2691 | if (decayed) { |
2692 | sa->load_avg = div_u64(sa->load_sum, LOAD_AVG_MAX); | |
13962234 YD |
2693 | if (cfs_rq) { |
2694 | cfs_rq->runnable_load_avg = | |
2695 | div_u64(cfs_rq->runnable_load_sum, LOAD_AVG_MAX); | |
2696 | } | |
006cdf02 | 2697 | sa->util_avg = sa->util_sum / LOAD_AVG_MAX; |
9d89c257 | 2698 | } |
aff3e498 | 2699 | |
9d89c257 | 2700 | return decayed; |
9ee474f5 PT |
2701 | } |
2702 | ||
c566e8e9 | 2703 | #ifdef CONFIG_FAIR_GROUP_SCHED |
bb17f655 | 2704 | /* |
9d89c257 YD |
2705 | * Updating tg's load_avg is necessary before update_cfs_share (which is done) |
2706 | * and effective_load (which is not done because it is too costly). | |
bb17f655 | 2707 | */ |
9d89c257 | 2708 | static inline void update_tg_load_avg(struct cfs_rq *cfs_rq, int force) |
bb17f655 | 2709 | { |
9d89c257 | 2710 | long delta = cfs_rq->avg.load_avg - cfs_rq->tg_load_avg_contrib; |
bb17f655 | 2711 | |
aa0b7ae0 WL |
2712 | /* |
2713 | * No need to update load_avg for root_task_group as it is not used. | |
2714 | */ | |
2715 | if (cfs_rq->tg == &root_task_group) | |
2716 | return; | |
2717 | ||
9d89c257 YD |
2718 | if (force || abs(delta) > cfs_rq->tg_load_avg_contrib / 64) { |
2719 | atomic_long_add(delta, &cfs_rq->tg->load_avg); | |
2720 | cfs_rq->tg_load_avg_contrib = cfs_rq->avg.load_avg; | |
bb17f655 | 2721 | } |
8165e145 | 2722 | } |
f5f9739d | 2723 | |
ad936d86 BP |
2724 | /* |
2725 | * Called within set_task_rq() right before setting a task's cpu. The | |
2726 | * caller only guarantees p->pi_lock is held; no other assumptions, | |
2727 | * including the state of rq->lock, should be made. | |
2728 | */ | |
2729 | void set_task_rq_fair(struct sched_entity *se, | |
2730 | struct cfs_rq *prev, struct cfs_rq *next) | |
2731 | { | |
2732 | if (!sched_feat(ATTACH_AGE_LOAD)) | |
2733 | return; | |
2734 | ||
2735 | /* | |
2736 | * We are supposed to update the task to "current" time, then its up to | |
2737 | * date and ready to go to new CPU/cfs_rq. But we have difficulty in | |
2738 | * getting what current time is, so simply throw away the out-of-date | |
2739 | * time. This will result in the wakee task is less decayed, but giving | |
2740 | * the wakee more load sounds not bad. | |
2741 | */ | |
2742 | if (se->avg.last_update_time && prev) { | |
2743 | u64 p_last_update_time; | |
2744 | u64 n_last_update_time; | |
2745 | ||
2746 | #ifndef CONFIG_64BIT | |
2747 | u64 p_last_update_time_copy; | |
2748 | u64 n_last_update_time_copy; | |
2749 | ||
2750 | do { | |
2751 | p_last_update_time_copy = prev->load_last_update_time_copy; | |
2752 | n_last_update_time_copy = next->load_last_update_time_copy; | |
2753 | ||
2754 | smp_rmb(); | |
2755 | ||
2756 | p_last_update_time = prev->avg.last_update_time; | |
2757 | n_last_update_time = next->avg.last_update_time; | |
2758 | ||
2759 | } while (p_last_update_time != p_last_update_time_copy || | |
2760 | n_last_update_time != n_last_update_time_copy); | |
2761 | #else | |
2762 | p_last_update_time = prev->avg.last_update_time; | |
2763 | n_last_update_time = next->avg.last_update_time; | |
2764 | #endif | |
2765 | __update_load_avg(p_last_update_time, cpu_of(rq_of(prev)), | |
2766 | &se->avg, 0, 0, NULL); | |
2767 | se->avg.last_update_time = n_last_update_time; | |
2768 | } | |
2769 | } | |
6e83125c | 2770 | #else /* CONFIG_FAIR_GROUP_SCHED */ |
9d89c257 | 2771 | static inline void update_tg_load_avg(struct cfs_rq *cfs_rq, int force) {} |
6e83125c | 2772 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
c566e8e9 | 2773 | |
9d89c257 | 2774 | static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq); |
8165e145 | 2775 | |
9d89c257 YD |
2776 | /* Group cfs_rq's load_avg is used for task_h_load and update_cfs_share */ |
2777 | static inline int update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq) | |
2dac754e | 2778 | { |
9d89c257 | 2779 | struct sched_avg *sa = &cfs_rq->avg; |
3e386d56 | 2780 | int decayed, removed = 0; |
2dac754e | 2781 | |
9d89c257 | 2782 | if (atomic_long_read(&cfs_rq->removed_load_avg)) { |
9e0e83a1 | 2783 | s64 r = atomic_long_xchg(&cfs_rq->removed_load_avg, 0); |
9d89c257 YD |
2784 | sa->load_avg = max_t(long, sa->load_avg - r, 0); |
2785 | sa->load_sum = max_t(s64, sa->load_sum - r * LOAD_AVG_MAX, 0); | |
3e386d56 | 2786 | removed = 1; |
8165e145 | 2787 | } |
2dac754e | 2788 | |
9d89c257 YD |
2789 | if (atomic_long_read(&cfs_rq->removed_util_avg)) { |
2790 | long r = atomic_long_xchg(&cfs_rq->removed_util_avg, 0); | |
2791 | sa->util_avg = max_t(long, sa->util_avg - r, 0); | |
006cdf02 | 2792 | sa->util_sum = max_t(s32, sa->util_sum - r * LOAD_AVG_MAX, 0); |
9d89c257 | 2793 | } |
36ee28e4 | 2794 | |
9d89c257 | 2795 | decayed = __update_load_avg(now, cpu_of(rq_of(cfs_rq)), sa, |
13962234 | 2796 | scale_load_down(cfs_rq->load.weight), cfs_rq->curr != NULL, cfs_rq); |
36ee28e4 | 2797 | |
9d89c257 YD |
2798 | #ifndef CONFIG_64BIT |
2799 | smp_wmb(); | |
2800 | cfs_rq->load_last_update_time_copy = sa->last_update_time; | |
2801 | #endif | |
36ee28e4 | 2802 | |
3e386d56 | 2803 | return decayed || removed; |
9ee474f5 PT |
2804 | } |
2805 | ||
9d89c257 YD |
2806 | /* Update task and its cfs_rq load average */ |
2807 | static inline void update_load_avg(struct sched_entity *se, int update_tg) | |
9d85f21c | 2808 | { |
2dac754e | 2809 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
9d89c257 | 2810 | u64 now = cfs_rq_clock_task(cfs_rq); |
a05e8c51 | 2811 | int cpu = cpu_of(rq_of(cfs_rq)); |
2dac754e | 2812 | |
f1b17280 | 2813 | /* |
9d89c257 YD |
2814 | * Track task load average for carrying it to new CPU after migrated, and |
2815 | * track group sched_entity load average for task_h_load calc in migration | |
f1b17280 | 2816 | */ |
9d89c257 | 2817 | __update_load_avg(now, cpu, &se->avg, |
a05e8c51 BP |
2818 | se->on_rq * scale_load_down(se->load.weight), |
2819 | cfs_rq->curr == se, NULL); | |
f1b17280 | 2820 | |
9d89c257 YD |
2821 | if (update_cfs_rq_load_avg(now, cfs_rq) && update_tg) |
2822 | update_tg_load_avg(cfs_rq, 0); | |
9ee474f5 PT |
2823 | } |
2824 | ||
a05e8c51 BP |
2825 | static void attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) |
2826 | { | |
a9280514 PZ |
2827 | if (!sched_feat(ATTACH_AGE_LOAD)) |
2828 | goto skip_aging; | |
2829 | ||
6efdb105 BP |
2830 | /* |
2831 | * If we got migrated (either between CPUs or between cgroups) we'll | |
2832 | * have aged the average right before clearing @last_update_time. | |
2833 | */ | |
2834 | if (se->avg.last_update_time) { | |
2835 | __update_load_avg(cfs_rq->avg.last_update_time, cpu_of(rq_of(cfs_rq)), | |
2836 | &se->avg, 0, 0, NULL); | |
2837 | ||
2838 | /* | |
2839 | * XXX: we could have just aged the entire load away if we've been | |
2840 | * absent from the fair class for too long. | |
2841 | */ | |
2842 | } | |
2843 | ||
a9280514 | 2844 | skip_aging: |
a05e8c51 BP |
2845 | se->avg.last_update_time = cfs_rq->avg.last_update_time; |
2846 | cfs_rq->avg.load_avg += se->avg.load_avg; | |
2847 | cfs_rq->avg.load_sum += se->avg.load_sum; | |
2848 | cfs_rq->avg.util_avg += se->avg.util_avg; | |
2849 | cfs_rq->avg.util_sum += se->avg.util_sum; | |
2850 | } | |
2851 | ||
2852 | static void detach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
2853 | { | |
2854 | __update_load_avg(cfs_rq->avg.last_update_time, cpu_of(rq_of(cfs_rq)), | |
2855 | &se->avg, se->on_rq * scale_load_down(se->load.weight), | |
2856 | cfs_rq->curr == se, NULL); | |
2857 | ||
2858 | cfs_rq->avg.load_avg = max_t(long, cfs_rq->avg.load_avg - se->avg.load_avg, 0); | |
2859 | cfs_rq->avg.load_sum = max_t(s64, cfs_rq->avg.load_sum - se->avg.load_sum, 0); | |
2860 | cfs_rq->avg.util_avg = max_t(long, cfs_rq->avg.util_avg - se->avg.util_avg, 0); | |
2861 | cfs_rq->avg.util_sum = max_t(s32, cfs_rq->avg.util_sum - se->avg.util_sum, 0); | |
2862 | } | |
2863 | ||
9d89c257 YD |
2864 | /* Add the load generated by se into cfs_rq's load average */ |
2865 | static inline void | |
2866 | enqueue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
9ee474f5 | 2867 | { |
9d89c257 YD |
2868 | struct sched_avg *sa = &se->avg; |
2869 | u64 now = cfs_rq_clock_task(cfs_rq); | |
a05e8c51 | 2870 | int migrated, decayed; |
9ee474f5 | 2871 | |
a05e8c51 BP |
2872 | migrated = !sa->last_update_time; |
2873 | if (!migrated) { | |
9d89c257 | 2874 | __update_load_avg(now, cpu_of(rq_of(cfs_rq)), sa, |
13962234 YD |
2875 | se->on_rq * scale_load_down(se->load.weight), |
2876 | cfs_rq->curr == se, NULL); | |
aff3e498 | 2877 | } |
c566e8e9 | 2878 | |
9d89c257 | 2879 | decayed = update_cfs_rq_load_avg(now, cfs_rq); |
18bf2805 | 2880 | |
13962234 YD |
2881 | cfs_rq->runnable_load_avg += sa->load_avg; |
2882 | cfs_rq->runnable_load_sum += sa->load_sum; | |
2883 | ||
a05e8c51 BP |
2884 | if (migrated) |
2885 | attach_entity_load_avg(cfs_rq, se); | |
9ee474f5 | 2886 | |
9d89c257 YD |
2887 | if (decayed || migrated) |
2888 | update_tg_load_avg(cfs_rq, 0); | |
2dac754e PT |
2889 | } |
2890 | ||
13962234 YD |
2891 | /* Remove the runnable load generated by se from cfs_rq's runnable load average */ |
2892 | static inline void | |
2893 | dequeue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
2894 | { | |
2895 | update_load_avg(se, 1); | |
2896 | ||
2897 | cfs_rq->runnable_load_avg = | |
2898 | max_t(long, cfs_rq->runnable_load_avg - se->avg.load_avg, 0); | |
2899 | cfs_rq->runnable_load_sum = | |
a05e8c51 | 2900 | max_t(s64, cfs_rq->runnable_load_sum - se->avg.load_sum, 0); |
13962234 YD |
2901 | } |
2902 | ||
9ee474f5 | 2903 | /* |
9d89c257 YD |
2904 | * Task first catches up with cfs_rq, and then subtract |
2905 | * itself from the cfs_rq (task must be off the queue now). | |
9ee474f5 | 2906 | */ |
9d89c257 | 2907 | void remove_entity_load_avg(struct sched_entity *se) |
2dac754e | 2908 | { |
9d89c257 YD |
2909 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
2910 | u64 last_update_time; | |
2911 | ||
2912 | #ifndef CONFIG_64BIT | |
2913 | u64 last_update_time_copy; | |
9ee474f5 | 2914 | |
9d89c257 YD |
2915 | do { |
2916 | last_update_time_copy = cfs_rq->load_last_update_time_copy; | |
2917 | smp_rmb(); | |
2918 | last_update_time = cfs_rq->avg.last_update_time; | |
2919 | } while (last_update_time != last_update_time_copy); | |
2920 | #else | |
2921 | last_update_time = cfs_rq->avg.last_update_time; | |
2922 | #endif | |
2923 | ||
13962234 | 2924 | __update_load_avg(last_update_time, cpu_of(rq_of(cfs_rq)), &se->avg, 0, 0, NULL); |
9d89c257 YD |
2925 | atomic_long_add(se->avg.load_avg, &cfs_rq->removed_load_avg); |
2926 | atomic_long_add(se->avg.util_avg, &cfs_rq->removed_util_avg); | |
2dac754e | 2927 | } |
642dbc39 | 2928 | |
7ea241af YD |
2929 | static inline unsigned long cfs_rq_runnable_load_avg(struct cfs_rq *cfs_rq) |
2930 | { | |
2931 | return cfs_rq->runnable_load_avg; | |
2932 | } | |
2933 | ||
2934 | static inline unsigned long cfs_rq_load_avg(struct cfs_rq *cfs_rq) | |
2935 | { | |
2936 | return cfs_rq->avg.load_avg; | |
2937 | } | |
2938 | ||
6e83125c PZ |
2939 | static int idle_balance(struct rq *this_rq); |
2940 | ||
38033c37 PZ |
2941 | #else /* CONFIG_SMP */ |
2942 | ||
9d89c257 YD |
2943 | static inline void update_load_avg(struct sched_entity *se, int update_tg) {} |
2944 | static inline void | |
2945 | enqueue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {} | |
13962234 YD |
2946 | static inline void |
2947 | dequeue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {} | |
9d89c257 | 2948 | static inline void remove_entity_load_avg(struct sched_entity *se) {} |
6e83125c | 2949 | |
a05e8c51 BP |
2950 | static inline void |
2951 | attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {} | |
2952 | static inline void | |
2953 | detach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {} | |
2954 | ||
6e83125c PZ |
2955 | static inline int idle_balance(struct rq *rq) |
2956 | { | |
2957 | return 0; | |
2958 | } | |
2959 | ||
38033c37 | 2960 | #endif /* CONFIG_SMP */ |
9d85f21c | 2961 | |
2396af69 | 2962 | static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 | 2963 | { |
bf0f6f24 | 2964 | #ifdef CONFIG_SCHEDSTATS |
e414314c PZ |
2965 | struct task_struct *tsk = NULL; |
2966 | ||
2967 | if (entity_is_task(se)) | |
2968 | tsk = task_of(se); | |
2969 | ||
41acab88 | 2970 | if (se->statistics.sleep_start) { |
78becc27 | 2971 | u64 delta = rq_clock(rq_of(cfs_rq)) - se->statistics.sleep_start; |
bf0f6f24 IM |
2972 | |
2973 | if ((s64)delta < 0) | |
2974 | delta = 0; | |
2975 | ||
41acab88 LDM |
2976 | if (unlikely(delta > se->statistics.sleep_max)) |
2977 | se->statistics.sleep_max = delta; | |
bf0f6f24 | 2978 | |
8c79a045 | 2979 | se->statistics.sleep_start = 0; |
41acab88 | 2980 | se->statistics.sum_sleep_runtime += delta; |
9745512c | 2981 | |
768d0c27 | 2982 | if (tsk) { |
e414314c | 2983 | account_scheduler_latency(tsk, delta >> 10, 1); |
768d0c27 PZ |
2984 | trace_sched_stat_sleep(tsk, delta); |
2985 | } | |
bf0f6f24 | 2986 | } |
41acab88 | 2987 | if (se->statistics.block_start) { |
78becc27 | 2988 | u64 delta = rq_clock(rq_of(cfs_rq)) - se->statistics.block_start; |
bf0f6f24 IM |
2989 | |
2990 | if ((s64)delta < 0) | |
2991 | delta = 0; | |
2992 | ||
41acab88 LDM |
2993 | if (unlikely(delta > se->statistics.block_max)) |
2994 | se->statistics.block_max = delta; | |
bf0f6f24 | 2995 | |
8c79a045 | 2996 | se->statistics.block_start = 0; |
41acab88 | 2997 | se->statistics.sum_sleep_runtime += delta; |
30084fbd | 2998 | |
e414314c | 2999 | if (tsk) { |
8f0dfc34 | 3000 | if (tsk->in_iowait) { |
41acab88 LDM |
3001 | se->statistics.iowait_sum += delta; |
3002 | se->statistics.iowait_count++; | |
768d0c27 | 3003 | trace_sched_stat_iowait(tsk, delta); |
8f0dfc34 AV |
3004 | } |
3005 | ||
b781a602 AV |
3006 | trace_sched_stat_blocked(tsk, delta); |
3007 | ||
e414314c PZ |
3008 | /* |
3009 | * Blocking time is in units of nanosecs, so shift by | |
3010 | * 20 to get a milliseconds-range estimation of the | |
3011 | * amount of time that the task spent sleeping: | |
3012 | */ | |
3013 | if (unlikely(prof_on == SLEEP_PROFILING)) { | |
3014 | profile_hits(SLEEP_PROFILING, | |
3015 | (void *)get_wchan(tsk), | |
3016 | delta >> 20); | |
3017 | } | |
3018 | account_scheduler_latency(tsk, delta >> 10, 0); | |
30084fbd | 3019 | } |
bf0f6f24 IM |
3020 | } |
3021 | #endif | |
3022 | } | |
3023 | ||
ddc97297 PZ |
3024 | static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se) |
3025 | { | |
3026 | #ifdef CONFIG_SCHED_DEBUG | |
3027 | s64 d = se->vruntime - cfs_rq->min_vruntime; | |
3028 | ||
3029 | if (d < 0) | |
3030 | d = -d; | |
3031 | ||
3032 | if (d > 3*sysctl_sched_latency) | |
3033 | schedstat_inc(cfs_rq, nr_spread_over); | |
3034 | #endif | |
3035 | } | |
3036 | ||
aeb73b04 PZ |
3037 | static void |
3038 | place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial) | |
3039 | { | |
1af5f730 | 3040 | u64 vruntime = cfs_rq->min_vruntime; |
94dfb5e7 | 3041 | |
2cb8600e PZ |
3042 | /* |
3043 | * The 'current' period is already promised to the current tasks, | |
3044 | * however the extra weight of the new task will slow them down a | |
3045 | * little, place the new task so that it fits in the slot that | |
3046 | * stays open at the end. | |
3047 | */ | |
94dfb5e7 | 3048 | if (initial && sched_feat(START_DEBIT)) |
f9c0b095 | 3049 | vruntime += sched_vslice(cfs_rq, se); |
aeb73b04 | 3050 | |
a2e7a7eb | 3051 | /* sleeps up to a single latency don't count. */ |
5ca9880c | 3052 | if (!initial) { |
a2e7a7eb | 3053 | unsigned long thresh = sysctl_sched_latency; |
a7be37ac | 3054 | |
a2e7a7eb MG |
3055 | /* |
3056 | * Halve their sleep time's effect, to allow | |
3057 | * for a gentler effect of sleepers: | |
3058 | */ | |
3059 | if (sched_feat(GENTLE_FAIR_SLEEPERS)) | |
3060 | thresh >>= 1; | |
51e0304c | 3061 | |
a2e7a7eb | 3062 | vruntime -= thresh; |
aeb73b04 PZ |
3063 | } |
3064 | ||
b5d9d734 | 3065 | /* ensure we never gain time by being placed backwards. */ |
16c8f1c7 | 3066 | se->vruntime = max_vruntime(se->vruntime, vruntime); |
aeb73b04 PZ |
3067 | } |
3068 | ||
d3d9dc33 PT |
3069 | static void check_enqueue_throttle(struct cfs_rq *cfs_rq); |
3070 | ||
bf0f6f24 | 3071 | static void |
88ec22d3 | 3072 | enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
bf0f6f24 | 3073 | { |
88ec22d3 PZ |
3074 | /* |
3075 | * Update the normalized vruntime before updating min_vruntime | |
0fc576d5 | 3076 | * through calling update_curr(). |
88ec22d3 | 3077 | */ |
371fd7e7 | 3078 | if (!(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_WAKING)) |
88ec22d3 PZ |
3079 | se->vruntime += cfs_rq->min_vruntime; |
3080 | ||
bf0f6f24 | 3081 | /* |
a2a2d680 | 3082 | * Update run-time statistics of the 'current'. |
bf0f6f24 | 3083 | */ |
b7cc0896 | 3084 | update_curr(cfs_rq); |
9d89c257 | 3085 | enqueue_entity_load_avg(cfs_rq, se); |
17bc14b7 LT |
3086 | account_entity_enqueue(cfs_rq, se); |
3087 | update_cfs_shares(cfs_rq); | |
bf0f6f24 | 3088 | |
88ec22d3 | 3089 | if (flags & ENQUEUE_WAKEUP) { |
aeb73b04 | 3090 | place_entity(cfs_rq, se, 0); |
2396af69 | 3091 | enqueue_sleeper(cfs_rq, se); |
e9acbff6 | 3092 | } |
bf0f6f24 | 3093 | |
d2417e5a | 3094 | update_stats_enqueue(cfs_rq, se); |
ddc97297 | 3095 | check_spread(cfs_rq, se); |
83b699ed SV |
3096 | if (se != cfs_rq->curr) |
3097 | __enqueue_entity(cfs_rq, se); | |
2069dd75 | 3098 | se->on_rq = 1; |
3d4b47b4 | 3099 | |
d3d9dc33 | 3100 | if (cfs_rq->nr_running == 1) { |
3d4b47b4 | 3101 | list_add_leaf_cfs_rq(cfs_rq); |
d3d9dc33 PT |
3102 | check_enqueue_throttle(cfs_rq); |
3103 | } | |
bf0f6f24 IM |
3104 | } |
3105 | ||
2c13c919 | 3106 | static void __clear_buddies_last(struct sched_entity *se) |
2002c695 | 3107 | { |
2c13c919 RR |
3108 | for_each_sched_entity(se) { |
3109 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
f1044799 | 3110 | if (cfs_rq->last != se) |
2c13c919 | 3111 | break; |
f1044799 PZ |
3112 | |
3113 | cfs_rq->last = NULL; | |
2c13c919 RR |
3114 | } |
3115 | } | |
2002c695 | 3116 | |
2c13c919 RR |
3117 | static void __clear_buddies_next(struct sched_entity *se) |
3118 | { | |
3119 | for_each_sched_entity(se) { | |
3120 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
f1044799 | 3121 | if (cfs_rq->next != se) |
2c13c919 | 3122 | break; |
f1044799 PZ |
3123 | |
3124 | cfs_rq->next = NULL; | |
2c13c919 | 3125 | } |
2002c695 PZ |
3126 | } |
3127 | ||
ac53db59 RR |
3128 | static void __clear_buddies_skip(struct sched_entity *se) |
3129 | { | |
3130 | for_each_sched_entity(se) { | |
3131 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
f1044799 | 3132 | if (cfs_rq->skip != se) |
ac53db59 | 3133 | break; |
f1044799 PZ |
3134 | |
3135 | cfs_rq->skip = NULL; | |
ac53db59 RR |
3136 | } |
3137 | } | |
3138 | ||
a571bbea PZ |
3139 | static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se) |
3140 | { | |
2c13c919 RR |
3141 | if (cfs_rq->last == se) |
3142 | __clear_buddies_last(se); | |
3143 | ||
3144 | if (cfs_rq->next == se) | |
3145 | __clear_buddies_next(se); | |
ac53db59 RR |
3146 | |
3147 | if (cfs_rq->skip == se) | |
3148 | __clear_buddies_skip(se); | |
a571bbea PZ |
3149 | } |
3150 | ||
6c16a6dc | 3151 | static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq); |
d8b4986d | 3152 | |
bf0f6f24 | 3153 | static void |
371fd7e7 | 3154 | dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
bf0f6f24 | 3155 | { |
a2a2d680 DA |
3156 | /* |
3157 | * Update run-time statistics of the 'current'. | |
3158 | */ | |
3159 | update_curr(cfs_rq); | |
13962234 | 3160 | dequeue_entity_load_avg(cfs_rq, se); |
a2a2d680 | 3161 | |
19b6a2e3 | 3162 | update_stats_dequeue(cfs_rq, se); |
371fd7e7 | 3163 | if (flags & DEQUEUE_SLEEP) { |
67e9fb2a | 3164 | #ifdef CONFIG_SCHEDSTATS |
bf0f6f24 IM |
3165 | if (entity_is_task(se)) { |
3166 | struct task_struct *tsk = task_of(se); | |
3167 | ||
3168 | if (tsk->state & TASK_INTERRUPTIBLE) | |
78becc27 | 3169 | se->statistics.sleep_start = rq_clock(rq_of(cfs_rq)); |
bf0f6f24 | 3170 | if (tsk->state & TASK_UNINTERRUPTIBLE) |
78becc27 | 3171 | se->statistics.block_start = rq_clock(rq_of(cfs_rq)); |
bf0f6f24 | 3172 | } |
db36cc7d | 3173 | #endif |
67e9fb2a PZ |
3174 | } |
3175 | ||
2002c695 | 3176 | clear_buddies(cfs_rq, se); |
4793241b | 3177 | |
83b699ed | 3178 | if (se != cfs_rq->curr) |
30cfdcfc | 3179 | __dequeue_entity(cfs_rq, se); |
17bc14b7 | 3180 | se->on_rq = 0; |
30cfdcfc | 3181 | account_entity_dequeue(cfs_rq, se); |
88ec22d3 PZ |
3182 | |
3183 | /* | |
3184 | * Normalize the entity after updating the min_vruntime because the | |
3185 | * update can refer to the ->curr item and we need to reflect this | |
3186 | * movement in our normalized position. | |
3187 | */ | |
371fd7e7 | 3188 | if (!(flags & DEQUEUE_SLEEP)) |
88ec22d3 | 3189 | se->vruntime -= cfs_rq->min_vruntime; |
1e876231 | 3190 | |
d8b4986d PT |
3191 | /* return excess runtime on last dequeue */ |
3192 | return_cfs_rq_runtime(cfs_rq); | |
3193 | ||
1e876231 | 3194 | update_min_vruntime(cfs_rq); |
17bc14b7 | 3195 | update_cfs_shares(cfs_rq); |
bf0f6f24 IM |
3196 | } |
3197 | ||
3198 | /* | |
3199 | * Preempt the current task with a newly woken task if needed: | |
3200 | */ | |
7c92e54f | 3201 | static void |
2e09bf55 | 3202 | check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr) |
bf0f6f24 | 3203 | { |
11697830 | 3204 | unsigned long ideal_runtime, delta_exec; |
f4cfb33e WX |
3205 | struct sched_entity *se; |
3206 | s64 delta; | |
11697830 | 3207 | |
6d0f0ebd | 3208 | ideal_runtime = sched_slice(cfs_rq, curr); |
11697830 | 3209 | delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime; |
a9f3e2b5 | 3210 | if (delta_exec > ideal_runtime) { |
8875125e | 3211 | resched_curr(rq_of(cfs_rq)); |
a9f3e2b5 MG |
3212 | /* |
3213 | * The current task ran long enough, ensure it doesn't get | |
3214 | * re-elected due to buddy favours. | |
3215 | */ | |
3216 | clear_buddies(cfs_rq, curr); | |
f685ceac MG |
3217 | return; |
3218 | } | |
3219 | ||
3220 | /* | |
3221 | * Ensure that a task that missed wakeup preemption by a | |
3222 | * narrow margin doesn't have to wait for a full slice. | |
3223 | * This also mitigates buddy induced latencies under load. | |
3224 | */ | |
f685ceac MG |
3225 | if (delta_exec < sysctl_sched_min_granularity) |
3226 | return; | |
3227 | ||
f4cfb33e WX |
3228 | se = __pick_first_entity(cfs_rq); |
3229 | delta = curr->vruntime - se->vruntime; | |
f685ceac | 3230 | |
f4cfb33e WX |
3231 | if (delta < 0) |
3232 | return; | |
d7d82944 | 3233 | |
f4cfb33e | 3234 | if (delta > ideal_runtime) |
8875125e | 3235 | resched_curr(rq_of(cfs_rq)); |
bf0f6f24 IM |
3236 | } |
3237 | ||
83b699ed | 3238 | static void |
8494f412 | 3239 | set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 | 3240 | { |
83b699ed SV |
3241 | /* 'current' is not kept within the tree. */ |
3242 | if (se->on_rq) { | |
3243 | /* | |
3244 | * Any task has to be enqueued before it get to execute on | |
3245 | * a CPU. So account for the time it spent waiting on the | |
3246 | * runqueue. | |
3247 | */ | |
3248 | update_stats_wait_end(cfs_rq, se); | |
3249 | __dequeue_entity(cfs_rq, se); | |
9d89c257 | 3250 | update_load_avg(se, 1); |
83b699ed SV |
3251 | } |
3252 | ||
79303e9e | 3253 | update_stats_curr_start(cfs_rq, se); |
429d43bc | 3254 | cfs_rq->curr = se; |
eba1ed4b IM |
3255 | #ifdef CONFIG_SCHEDSTATS |
3256 | /* | |
3257 | * Track our maximum slice length, if the CPU's load is at | |
3258 | * least twice that of our own weight (i.e. dont track it | |
3259 | * when there are only lesser-weight tasks around): | |
3260 | */ | |
495eca49 | 3261 | if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) { |
41acab88 | 3262 | se->statistics.slice_max = max(se->statistics.slice_max, |
eba1ed4b IM |
3263 | se->sum_exec_runtime - se->prev_sum_exec_runtime); |
3264 | } | |
3265 | #endif | |
4a55b450 | 3266 | se->prev_sum_exec_runtime = se->sum_exec_runtime; |
bf0f6f24 IM |
3267 | } |
3268 | ||
3f3a4904 PZ |
3269 | static int |
3270 | wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se); | |
3271 | ||
ac53db59 RR |
3272 | /* |
3273 | * Pick the next process, keeping these things in mind, in this order: | |
3274 | * 1) keep things fair between processes/task groups | |
3275 | * 2) pick the "next" process, since someone really wants that to run | |
3276 | * 3) pick the "last" process, for cache locality | |
3277 | * 4) do not run the "skip" process, if something else is available | |
3278 | */ | |
678d5718 PZ |
3279 | static struct sched_entity * |
3280 | pick_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *curr) | |
aa2ac252 | 3281 | { |
678d5718 PZ |
3282 | struct sched_entity *left = __pick_first_entity(cfs_rq); |
3283 | struct sched_entity *se; | |
3284 | ||
3285 | /* | |
3286 | * If curr is set we have to see if its left of the leftmost entity | |
3287 | * still in the tree, provided there was anything in the tree at all. | |
3288 | */ | |
3289 | if (!left || (curr && entity_before(curr, left))) | |
3290 | left = curr; | |
3291 | ||
3292 | se = left; /* ideally we run the leftmost entity */ | |
f4b6755f | 3293 | |
ac53db59 RR |
3294 | /* |
3295 | * Avoid running the skip buddy, if running something else can | |
3296 | * be done without getting too unfair. | |
3297 | */ | |
3298 | if (cfs_rq->skip == se) { | |
678d5718 PZ |
3299 | struct sched_entity *second; |
3300 | ||
3301 | if (se == curr) { | |
3302 | second = __pick_first_entity(cfs_rq); | |
3303 | } else { | |
3304 | second = __pick_next_entity(se); | |
3305 | if (!second || (curr && entity_before(curr, second))) | |
3306 | second = curr; | |
3307 | } | |
3308 | ||
ac53db59 RR |
3309 | if (second && wakeup_preempt_entity(second, left) < 1) |
3310 | se = second; | |
3311 | } | |
aa2ac252 | 3312 | |
f685ceac MG |
3313 | /* |
3314 | * Prefer last buddy, try to return the CPU to a preempted task. | |
3315 | */ | |
3316 | if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1) | |
3317 | se = cfs_rq->last; | |
3318 | ||
ac53db59 RR |
3319 | /* |
3320 | * Someone really wants this to run. If it's not unfair, run it. | |
3321 | */ | |
3322 | if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1) | |
3323 | se = cfs_rq->next; | |
3324 | ||
f685ceac | 3325 | clear_buddies(cfs_rq, se); |
4793241b PZ |
3326 | |
3327 | return se; | |
aa2ac252 PZ |
3328 | } |
3329 | ||
678d5718 | 3330 | static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq); |
d3d9dc33 | 3331 | |
ab6cde26 | 3332 | static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev) |
bf0f6f24 IM |
3333 | { |
3334 | /* | |
3335 | * If still on the runqueue then deactivate_task() | |
3336 | * was not called and update_curr() has to be done: | |
3337 | */ | |
3338 | if (prev->on_rq) | |
b7cc0896 | 3339 | update_curr(cfs_rq); |
bf0f6f24 | 3340 | |
d3d9dc33 PT |
3341 | /* throttle cfs_rqs exceeding runtime */ |
3342 | check_cfs_rq_runtime(cfs_rq); | |
3343 | ||
ddc97297 | 3344 | check_spread(cfs_rq, prev); |
30cfdcfc | 3345 | if (prev->on_rq) { |
5870db5b | 3346 | update_stats_wait_start(cfs_rq, prev); |
30cfdcfc DA |
3347 | /* Put 'current' back into the tree. */ |
3348 | __enqueue_entity(cfs_rq, prev); | |
9d85f21c | 3349 | /* in !on_rq case, update occurred at dequeue */ |
9d89c257 | 3350 | update_load_avg(prev, 0); |
30cfdcfc | 3351 | } |
429d43bc | 3352 | cfs_rq->curr = NULL; |
bf0f6f24 IM |
3353 | } |
3354 | ||
8f4d37ec PZ |
3355 | static void |
3356 | entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued) | |
bf0f6f24 | 3357 | { |
bf0f6f24 | 3358 | /* |
30cfdcfc | 3359 | * Update run-time statistics of the 'current'. |
bf0f6f24 | 3360 | */ |
30cfdcfc | 3361 | update_curr(cfs_rq); |
bf0f6f24 | 3362 | |
9d85f21c PT |
3363 | /* |
3364 | * Ensure that runnable average is periodically updated. | |
3365 | */ | |
9d89c257 | 3366 | update_load_avg(curr, 1); |
bf0bd948 | 3367 | update_cfs_shares(cfs_rq); |
9d85f21c | 3368 | |
8f4d37ec PZ |
3369 | #ifdef CONFIG_SCHED_HRTICK |
3370 | /* | |
3371 | * queued ticks are scheduled to match the slice, so don't bother | |
3372 | * validating it and just reschedule. | |
3373 | */ | |
983ed7a6 | 3374 | if (queued) { |
8875125e | 3375 | resched_curr(rq_of(cfs_rq)); |
983ed7a6 HH |
3376 | return; |
3377 | } | |
8f4d37ec PZ |
3378 | /* |
3379 | * don't let the period tick interfere with the hrtick preemption | |
3380 | */ | |
3381 | if (!sched_feat(DOUBLE_TICK) && | |
3382 | hrtimer_active(&rq_of(cfs_rq)->hrtick_timer)) | |
3383 | return; | |
3384 | #endif | |
3385 | ||
2c2efaed | 3386 | if (cfs_rq->nr_running > 1) |
2e09bf55 | 3387 | check_preempt_tick(cfs_rq, curr); |
bf0f6f24 IM |
3388 | } |
3389 | ||
ab84d31e PT |
3390 | |
3391 | /************************************************** | |
3392 | * CFS bandwidth control machinery | |
3393 | */ | |
3394 | ||
3395 | #ifdef CONFIG_CFS_BANDWIDTH | |
029632fb PZ |
3396 | |
3397 | #ifdef HAVE_JUMP_LABEL | |
c5905afb | 3398 | static struct static_key __cfs_bandwidth_used; |
029632fb PZ |
3399 | |
3400 | static inline bool cfs_bandwidth_used(void) | |
3401 | { | |
c5905afb | 3402 | return static_key_false(&__cfs_bandwidth_used); |
029632fb PZ |
3403 | } |
3404 | ||
1ee14e6c | 3405 | void cfs_bandwidth_usage_inc(void) |
029632fb | 3406 | { |
1ee14e6c BS |
3407 | static_key_slow_inc(&__cfs_bandwidth_used); |
3408 | } | |
3409 | ||
3410 | void cfs_bandwidth_usage_dec(void) | |
3411 | { | |
3412 | static_key_slow_dec(&__cfs_bandwidth_used); | |
029632fb PZ |
3413 | } |
3414 | #else /* HAVE_JUMP_LABEL */ | |
3415 | static bool cfs_bandwidth_used(void) | |
3416 | { | |
3417 | return true; | |
3418 | } | |
3419 | ||
1ee14e6c BS |
3420 | void cfs_bandwidth_usage_inc(void) {} |
3421 | void cfs_bandwidth_usage_dec(void) {} | |
029632fb PZ |
3422 | #endif /* HAVE_JUMP_LABEL */ |
3423 | ||
ab84d31e PT |
3424 | /* |
3425 | * default period for cfs group bandwidth. | |
3426 | * default: 0.1s, units: nanoseconds | |
3427 | */ | |
3428 | static inline u64 default_cfs_period(void) | |
3429 | { | |
3430 | return 100000000ULL; | |
3431 | } | |
ec12cb7f PT |
3432 | |
3433 | static inline u64 sched_cfs_bandwidth_slice(void) | |
3434 | { | |
3435 | return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC; | |
3436 | } | |
3437 | ||
a9cf55b2 PT |
3438 | /* |
3439 | * Replenish runtime according to assigned quota and update expiration time. | |
3440 | * We use sched_clock_cpu directly instead of rq->clock to avoid adding | |
3441 | * additional synchronization around rq->lock. | |
3442 | * | |
3443 | * requires cfs_b->lock | |
3444 | */ | |
029632fb | 3445 | void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b) |
a9cf55b2 PT |
3446 | { |
3447 | u64 now; | |
3448 | ||
3449 | if (cfs_b->quota == RUNTIME_INF) | |
3450 | return; | |
3451 | ||
3452 | now = sched_clock_cpu(smp_processor_id()); | |
3453 | cfs_b->runtime = cfs_b->quota; | |
3454 | cfs_b->runtime_expires = now + ktime_to_ns(cfs_b->period); | |
3455 | } | |
3456 | ||
029632fb PZ |
3457 | static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg) |
3458 | { | |
3459 | return &tg->cfs_bandwidth; | |
3460 | } | |
3461 | ||
f1b17280 PT |
3462 | /* rq->task_clock normalized against any time this cfs_rq has spent throttled */ |
3463 | static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq) | |
3464 | { | |
3465 | if (unlikely(cfs_rq->throttle_count)) | |
3466 | return cfs_rq->throttled_clock_task; | |
3467 | ||
78becc27 | 3468 | return rq_clock_task(rq_of(cfs_rq)) - cfs_rq->throttled_clock_task_time; |
f1b17280 PT |
3469 | } |
3470 | ||
85dac906 PT |
3471 | /* returns 0 on failure to allocate runtime */ |
3472 | static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
ec12cb7f PT |
3473 | { |
3474 | struct task_group *tg = cfs_rq->tg; | |
3475 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg); | |
a9cf55b2 | 3476 | u64 amount = 0, min_amount, expires; |
ec12cb7f PT |
3477 | |
3478 | /* note: this is a positive sum as runtime_remaining <= 0 */ | |
3479 | min_amount = sched_cfs_bandwidth_slice() - cfs_rq->runtime_remaining; | |
3480 | ||
3481 | raw_spin_lock(&cfs_b->lock); | |
3482 | if (cfs_b->quota == RUNTIME_INF) | |
3483 | amount = min_amount; | |
58088ad0 | 3484 | else { |
77a4d1a1 | 3485 | start_cfs_bandwidth(cfs_b); |
58088ad0 PT |
3486 | |
3487 | if (cfs_b->runtime > 0) { | |
3488 | amount = min(cfs_b->runtime, min_amount); | |
3489 | cfs_b->runtime -= amount; | |
3490 | cfs_b->idle = 0; | |
3491 | } | |
ec12cb7f | 3492 | } |
a9cf55b2 | 3493 | expires = cfs_b->runtime_expires; |
ec12cb7f PT |
3494 | raw_spin_unlock(&cfs_b->lock); |
3495 | ||
3496 | cfs_rq->runtime_remaining += amount; | |
a9cf55b2 PT |
3497 | /* |
3498 | * we may have advanced our local expiration to account for allowed | |
3499 | * spread between our sched_clock and the one on which runtime was | |
3500 | * issued. | |
3501 | */ | |
3502 | if ((s64)(expires - cfs_rq->runtime_expires) > 0) | |
3503 | cfs_rq->runtime_expires = expires; | |
85dac906 PT |
3504 | |
3505 | return cfs_rq->runtime_remaining > 0; | |
ec12cb7f PT |
3506 | } |
3507 | ||
a9cf55b2 PT |
3508 | /* |
3509 | * Note: This depends on the synchronization provided by sched_clock and the | |
3510 | * fact that rq->clock snapshots this value. | |
3511 | */ | |
3512 | static void expire_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
ec12cb7f | 3513 | { |
a9cf55b2 | 3514 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); |
a9cf55b2 PT |
3515 | |
3516 | /* if the deadline is ahead of our clock, nothing to do */ | |
78becc27 | 3517 | if (likely((s64)(rq_clock(rq_of(cfs_rq)) - cfs_rq->runtime_expires) < 0)) |
ec12cb7f PT |
3518 | return; |
3519 | ||
a9cf55b2 PT |
3520 | if (cfs_rq->runtime_remaining < 0) |
3521 | return; | |
3522 | ||
3523 | /* | |
3524 | * If the local deadline has passed we have to consider the | |
3525 | * possibility that our sched_clock is 'fast' and the global deadline | |
3526 | * has not truly expired. | |
3527 | * | |
3528 | * Fortunately we can check determine whether this the case by checking | |
51f2176d BS |
3529 | * whether the global deadline has advanced. It is valid to compare |
3530 | * cfs_b->runtime_expires without any locks since we only care about | |
3531 | * exact equality, so a partial write will still work. | |
a9cf55b2 PT |
3532 | */ |
3533 | ||
51f2176d | 3534 | if (cfs_rq->runtime_expires != cfs_b->runtime_expires) { |
a9cf55b2 PT |
3535 | /* extend local deadline, drift is bounded above by 2 ticks */ |
3536 | cfs_rq->runtime_expires += TICK_NSEC; | |
3537 | } else { | |
3538 | /* global deadline is ahead, expiration has passed */ | |
3539 | cfs_rq->runtime_remaining = 0; | |
3540 | } | |
3541 | } | |
3542 | ||
9dbdb155 | 3543 | static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec) |
a9cf55b2 PT |
3544 | { |
3545 | /* dock delta_exec before expiring quota (as it could span periods) */ | |
ec12cb7f | 3546 | cfs_rq->runtime_remaining -= delta_exec; |
a9cf55b2 PT |
3547 | expire_cfs_rq_runtime(cfs_rq); |
3548 | ||
3549 | if (likely(cfs_rq->runtime_remaining > 0)) | |
ec12cb7f PT |
3550 | return; |
3551 | ||
85dac906 PT |
3552 | /* |
3553 | * if we're unable to extend our runtime we resched so that the active | |
3554 | * hierarchy can be throttled | |
3555 | */ | |
3556 | if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr)) | |
8875125e | 3557 | resched_curr(rq_of(cfs_rq)); |
ec12cb7f PT |
3558 | } |
3559 | ||
6c16a6dc | 3560 | static __always_inline |
9dbdb155 | 3561 | void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec) |
ec12cb7f | 3562 | { |
56f570e5 | 3563 | if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled) |
ec12cb7f PT |
3564 | return; |
3565 | ||
3566 | __account_cfs_rq_runtime(cfs_rq, delta_exec); | |
3567 | } | |
3568 | ||
85dac906 PT |
3569 | static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq) |
3570 | { | |
56f570e5 | 3571 | return cfs_bandwidth_used() && cfs_rq->throttled; |
85dac906 PT |
3572 | } |
3573 | ||
64660c86 PT |
3574 | /* check whether cfs_rq, or any parent, is throttled */ |
3575 | static inline int throttled_hierarchy(struct cfs_rq *cfs_rq) | |
3576 | { | |
56f570e5 | 3577 | return cfs_bandwidth_used() && cfs_rq->throttle_count; |
64660c86 PT |
3578 | } |
3579 | ||
3580 | /* | |
3581 | * Ensure that neither of the group entities corresponding to src_cpu or | |
3582 | * dest_cpu are members of a throttled hierarchy when performing group | |
3583 | * load-balance operations. | |
3584 | */ | |
3585 | static inline int throttled_lb_pair(struct task_group *tg, | |
3586 | int src_cpu, int dest_cpu) | |
3587 | { | |
3588 | struct cfs_rq *src_cfs_rq, *dest_cfs_rq; | |
3589 | ||
3590 | src_cfs_rq = tg->cfs_rq[src_cpu]; | |
3591 | dest_cfs_rq = tg->cfs_rq[dest_cpu]; | |
3592 | ||
3593 | return throttled_hierarchy(src_cfs_rq) || | |
3594 | throttled_hierarchy(dest_cfs_rq); | |
3595 | } | |
3596 | ||
3597 | /* updated child weight may affect parent so we have to do this bottom up */ | |
3598 | static int tg_unthrottle_up(struct task_group *tg, void *data) | |
3599 | { | |
3600 | struct rq *rq = data; | |
3601 | struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; | |
3602 | ||
3603 | cfs_rq->throttle_count--; | |
3604 | #ifdef CONFIG_SMP | |
3605 | if (!cfs_rq->throttle_count) { | |
f1b17280 | 3606 | /* adjust cfs_rq_clock_task() */ |
78becc27 | 3607 | cfs_rq->throttled_clock_task_time += rq_clock_task(rq) - |
f1b17280 | 3608 | cfs_rq->throttled_clock_task; |
64660c86 PT |
3609 | } |
3610 | #endif | |
3611 | ||
3612 | return 0; | |
3613 | } | |
3614 | ||
3615 | static int tg_throttle_down(struct task_group *tg, void *data) | |
3616 | { | |
3617 | struct rq *rq = data; | |
3618 | struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; | |
3619 | ||
82958366 PT |
3620 | /* group is entering throttled state, stop time */ |
3621 | if (!cfs_rq->throttle_count) | |
78becc27 | 3622 | cfs_rq->throttled_clock_task = rq_clock_task(rq); |
64660c86 PT |
3623 | cfs_rq->throttle_count++; |
3624 | ||
3625 | return 0; | |
3626 | } | |
3627 | ||
d3d9dc33 | 3628 | static void throttle_cfs_rq(struct cfs_rq *cfs_rq) |
85dac906 PT |
3629 | { |
3630 | struct rq *rq = rq_of(cfs_rq); | |
3631 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); | |
3632 | struct sched_entity *se; | |
3633 | long task_delta, dequeue = 1; | |
77a4d1a1 | 3634 | bool empty; |
85dac906 PT |
3635 | |
3636 | se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))]; | |
3637 | ||
f1b17280 | 3638 | /* freeze hierarchy runnable averages while throttled */ |
64660c86 PT |
3639 | rcu_read_lock(); |
3640 | walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq); | |
3641 | rcu_read_unlock(); | |
85dac906 PT |
3642 | |
3643 | task_delta = cfs_rq->h_nr_running; | |
3644 | for_each_sched_entity(se) { | |
3645 | struct cfs_rq *qcfs_rq = cfs_rq_of(se); | |
3646 | /* throttled entity or throttle-on-deactivate */ | |
3647 | if (!se->on_rq) | |
3648 | break; | |
3649 | ||
3650 | if (dequeue) | |
3651 | dequeue_entity(qcfs_rq, se, DEQUEUE_SLEEP); | |
3652 | qcfs_rq->h_nr_running -= task_delta; | |
3653 | ||
3654 | if (qcfs_rq->load.weight) | |
3655 | dequeue = 0; | |
3656 | } | |
3657 | ||
3658 | if (!se) | |
72465447 | 3659 | sub_nr_running(rq, task_delta); |
85dac906 PT |
3660 | |
3661 | cfs_rq->throttled = 1; | |
78becc27 | 3662 | cfs_rq->throttled_clock = rq_clock(rq); |
85dac906 | 3663 | raw_spin_lock(&cfs_b->lock); |
d49db342 | 3664 | empty = list_empty(&cfs_b->throttled_cfs_rq); |
77a4d1a1 | 3665 | |
c06f04c7 BS |
3666 | /* |
3667 | * Add to the _head_ of the list, so that an already-started | |
3668 | * distribute_cfs_runtime will not see us | |
3669 | */ | |
3670 | list_add_rcu(&cfs_rq->throttled_list, &cfs_b->throttled_cfs_rq); | |
77a4d1a1 PZ |
3671 | |
3672 | /* | |
3673 | * If we're the first throttled task, make sure the bandwidth | |
3674 | * timer is running. | |
3675 | */ | |
3676 | if (empty) | |
3677 | start_cfs_bandwidth(cfs_b); | |
3678 | ||
85dac906 PT |
3679 | raw_spin_unlock(&cfs_b->lock); |
3680 | } | |
3681 | ||
029632fb | 3682 | void unthrottle_cfs_rq(struct cfs_rq *cfs_rq) |
671fd9da PT |
3683 | { |
3684 | struct rq *rq = rq_of(cfs_rq); | |
3685 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); | |
3686 | struct sched_entity *se; | |
3687 | int enqueue = 1; | |
3688 | long task_delta; | |
3689 | ||
22b958d8 | 3690 | se = cfs_rq->tg->se[cpu_of(rq)]; |
671fd9da PT |
3691 | |
3692 | cfs_rq->throttled = 0; | |
1a55af2e FW |
3693 | |
3694 | update_rq_clock(rq); | |
3695 | ||
671fd9da | 3696 | raw_spin_lock(&cfs_b->lock); |
78becc27 | 3697 | cfs_b->throttled_time += rq_clock(rq) - cfs_rq->throttled_clock; |
671fd9da PT |
3698 | list_del_rcu(&cfs_rq->throttled_list); |
3699 | raw_spin_unlock(&cfs_b->lock); | |
3700 | ||
64660c86 PT |
3701 | /* update hierarchical throttle state */ |
3702 | walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq); | |
3703 | ||
671fd9da PT |
3704 | if (!cfs_rq->load.weight) |
3705 | return; | |
3706 | ||
3707 | task_delta = cfs_rq->h_nr_running; | |
3708 | for_each_sched_entity(se) { | |
3709 | if (se->on_rq) | |
3710 | enqueue = 0; | |
3711 | ||
3712 | cfs_rq = cfs_rq_of(se); | |
3713 | if (enqueue) | |
3714 | enqueue_entity(cfs_rq, se, ENQUEUE_WAKEUP); | |
3715 | cfs_rq->h_nr_running += task_delta; | |
3716 | ||
3717 | if (cfs_rq_throttled(cfs_rq)) | |
3718 | break; | |
3719 | } | |
3720 | ||
3721 | if (!se) | |
72465447 | 3722 | add_nr_running(rq, task_delta); |
671fd9da PT |
3723 | |
3724 | /* determine whether we need to wake up potentially idle cpu */ | |
3725 | if (rq->curr == rq->idle && rq->cfs.nr_running) | |
8875125e | 3726 | resched_curr(rq); |
671fd9da PT |
3727 | } |
3728 | ||
3729 | static u64 distribute_cfs_runtime(struct cfs_bandwidth *cfs_b, | |
3730 | u64 remaining, u64 expires) | |
3731 | { | |
3732 | struct cfs_rq *cfs_rq; | |
c06f04c7 BS |
3733 | u64 runtime; |
3734 | u64 starting_runtime = remaining; | |
671fd9da PT |
3735 | |
3736 | rcu_read_lock(); | |
3737 | list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq, | |
3738 | throttled_list) { | |
3739 | struct rq *rq = rq_of(cfs_rq); | |
3740 | ||
3741 | raw_spin_lock(&rq->lock); | |
3742 | if (!cfs_rq_throttled(cfs_rq)) | |
3743 | goto next; | |
3744 | ||
3745 | runtime = -cfs_rq->runtime_remaining + 1; | |
3746 | if (runtime > remaining) | |
3747 | runtime = remaining; | |
3748 | remaining -= runtime; | |
3749 | ||
3750 | cfs_rq->runtime_remaining += runtime; | |
3751 | cfs_rq->runtime_expires = expires; | |
3752 | ||
3753 | /* we check whether we're throttled above */ | |
3754 | if (cfs_rq->runtime_remaining > 0) | |
3755 | unthrottle_cfs_rq(cfs_rq); | |
3756 | ||
3757 | next: | |
3758 | raw_spin_unlock(&rq->lock); | |
3759 | ||
3760 | if (!remaining) | |
3761 | break; | |
3762 | } | |
3763 | rcu_read_unlock(); | |
3764 | ||
c06f04c7 | 3765 | return starting_runtime - remaining; |
671fd9da PT |
3766 | } |
3767 | ||
58088ad0 PT |
3768 | /* |
3769 | * Responsible for refilling a task_group's bandwidth and unthrottling its | |
3770 | * cfs_rqs as appropriate. If there has been no activity within the last | |
3771 | * period the timer is deactivated until scheduling resumes; cfs_b->idle is | |
3772 | * used to track this state. | |
3773 | */ | |
3774 | static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun) | |
3775 | { | |
671fd9da | 3776 | u64 runtime, runtime_expires; |
51f2176d | 3777 | int throttled; |
58088ad0 | 3778 | |
58088ad0 PT |
3779 | /* no need to continue the timer with no bandwidth constraint */ |
3780 | if (cfs_b->quota == RUNTIME_INF) | |
51f2176d | 3781 | goto out_deactivate; |
58088ad0 | 3782 | |
671fd9da | 3783 | throttled = !list_empty(&cfs_b->throttled_cfs_rq); |
e8da1b18 | 3784 | cfs_b->nr_periods += overrun; |
671fd9da | 3785 | |
51f2176d BS |
3786 | /* |
3787 | * idle depends on !throttled (for the case of a large deficit), and if | |
3788 | * we're going inactive then everything else can be deferred | |
3789 | */ | |
3790 | if (cfs_b->idle && !throttled) | |
3791 | goto out_deactivate; | |
a9cf55b2 PT |
3792 | |
3793 | __refill_cfs_bandwidth_runtime(cfs_b); | |
3794 | ||
671fd9da PT |
3795 | if (!throttled) { |
3796 | /* mark as potentially idle for the upcoming period */ | |
3797 | cfs_b->idle = 1; | |
51f2176d | 3798 | return 0; |
671fd9da PT |
3799 | } |
3800 | ||
e8da1b18 NR |
3801 | /* account preceding periods in which throttling occurred */ |
3802 | cfs_b->nr_throttled += overrun; | |
3803 | ||
671fd9da | 3804 | runtime_expires = cfs_b->runtime_expires; |
671fd9da PT |
3805 | |
3806 | /* | |
c06f04c7 BS |
3807 | * This check is repeated as we are holding onto the new bandwidth while |
3808 | * we unthrottle. This can potentially race with an unthrottled group | |
3809 | * trying to acquire new bandwidth from the global pool. This can result | |
3810 | * in us over-using our runtime if it is all used during this loop, but | |
3811 | * only by limited amounts in that extreme case. | |
671fd9da | 3812 | */ |
c06f04c7 BS |
3813 | while (throttled && cfs_b->runtime > 0) { |
3814 | runtime = cfs_b->runtime; | |
671fd9da PT |
3815 | raw_spin_unlock(&cfs_b->lock); |
3816 | /* we can't nest cfs_b->lock while distributing bandwidth */ | |
3817 | runtime = distribute_cfs_runtime(cfs_b, runtime, | |
3818 | runtime_expires); | |
3819 | raw_spin_lock(&cfs_b->lock); | |
3820 | ||
3821 | throttled = !list_empty(&cfs_b->throttled_cfs_rq); | |
c06f04c7 BS |
3822 | |
3823 | cfs_b->runtime -= min(runtime, cfs_b->runtime); | |
671fd9da | 3824 | } |
58088ad0 | 3825 | |
671fd9da PT |
3826 | /* |
3827 | * While we are ensured activity in the period following an | |
3828 | * unthrottle, this also covers the case in which the new bandwidth is | |
3829 | * insufficient to cover the existing bandwidth deficit. (Forcing the | |
3830 | * timer to remain active while there are any throttled entities.) | |
3831 | */ | |
3832 | cfs_b->idle = 0; | |
58088ad0 | 3833 | |
51f2176d BS |
3834 | return 0; |
3835 | ||
3836 | out_deactivate: | |
51f2176d | 3837 | return 1; |
58088ad0 | 3838 | } |
d3d9dc33 | 3839 | |
d8b4986d PT |
3840 | /* a cfs_rq won't donate quota below this amount */ |
3841 | static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC; | |
3842 | /* minimum remaining period time to redistribute slack quota */ | |
3843 | static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC; | |
3844 | /* how long we wait to gather additional slack before distributing */ | |
3845 | static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC; | |
3846 | ||
db06e78c BS |
3847 | /* |
3848 | * Are we near the end of the current quota period? | |
3849 | * | |
3850 | * Requires cfs_b->lock for hrtimer_expires_remaining to be safe against the | |
4961b6e1 | 3851 | * hrtimer base being cleared by hrtimer_start. In the case of |
db06e78c BS |
3852 | * migrate_hrtimers, base is never cleared, so we are fine. |
3853 | */ | |
d8b4986d PT |
3854 | static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire) |
3855 | { | |
3856 | struct hrtimer *refresh_timer = &cfs_b->period_timer; | |
3857 | u64 remaining; | |
3858 | ||
3859 | /* if the call-back is running a quota refresh is already occurring */ | |
3860 | if (hrtimer_callback_running(refresh_timer)) | |
3861 | return 1; | |
3862 | ||
3863 | /* is a quota refresh about to occur? */ | |
3864 | remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer)); | |
3865 | if (remaining < min_expire) | |
3866 | return 1; | |
3867 | ||
3868 | return 0; | |
3869 | } | |
3870 | ||
3871 | static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b) | |
3872 | { | |
3873 | u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration; | |
3874 | ||
3875 | /* if there's a quota refresh soon don't bother with slack */ | |
3876 | if (runtime_refresh_within(cfs_b, min_left)) | |
3877 | return; | |
3878 | ||
4cfafd30 PZ |
3879 | hrtimer_start(&cfs_b->slack_timer, |
3880 | ns_to_ktime(cfs_bandwidth_slack_period), | |
3881 | HRTIMER_MODE_REL); | |
d8b4986d PT |
3882 | } |
3883 | ||
3884 | /* we know any runtime found here is valid as update_curr() precedes return */ | |
3885 | static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
3886 | { | |
3887 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); | |
3888 | s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime; | |
3889 | ||
3890 | if (slack_runtime <= 0) | |
3891 | return; | |
3892 | ||
3893 | raw_spin_lock(&cfs_b->lock); | |
3894 | if (cfs_b->quota != RUNTIME_INF && | |
3895 | cfs_rq->runtime_expires == cfs_b->runtime_expires) { | |
3896 | cfs_b->runtime += slack_runtime; | |
3897 | ||
3898 | /* we are under rq->lock, defer unthrottling using a timer */ | |
3899 | if (cfs_b->runtime > sched_cfs_bandwidth_slice() && | |
3900 | !list_empty(&cfs_b->throttled_cfs_rq)) | |
3901 | start_cfs_slack_bandwidth(cfs_b); | |
3902 | } | |
3903 | raw_spin_unlock(&cfs_b->lock); | |
3904 | ||
3905 | /* even if it's not valid for return we don't want to try again */ | |
3906 | cfs_rq->runtime_remaining -= slack_runtime; | |
3907 | } | |
3908 | ||
3909 | static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
3910 | { | |
56f570e5 PT |
3911 | if (!cfs_bandwidth_used()) |
3912 | return; | |
3913 | ||
fccfdc6f | 3914 | if (!cfs_rq->runtime_enabled || cfs_rq->nr_running) |
d8b4986d PT |
3915 | return; |
3916 | ||
3917 | __return_cfs_rq_runtime(cfs_rq); | |
3918 | } | |
3919 | ||
3920 | /* | |
3921 | * This is done with a timer (instead of inline with bandwidth return) since | |
3922 | * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs. | |
3923 | */ | |
3924 | static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b) | |
3925 | { | |
3926 | u64 runtime = 0, slice = sched_cfs_bandwidth_slice(); | |
3927 | u64 expires; | |
3928 | ||
3929 | /* confirm we're still not at a refresh boundary */ | |
db06e78c BS |
3930 | raw_spin_lock(&cfs_b->lock); |
3931 | if (runtime_refresh_within(cfs_b, min_bandwidth_expiration)) { | |
3932 | raw_spin_unlock(&cfs_b->lock); | |
d8b4986d | 3933 | return; |
db06e78c | 3934 | } |
d8b4986d | 3935 | |
c06f04c7 | 3936 | if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) |
d8b4986d | 3937 | runtime = cfs_b->runtime; |
c06f04c7 | 3938 | |
d8b4986d PT |
3939 | expires = cfs_b->runtime_expires; |
3940 | raw_spin_unlock(&cfs_b->lock); | |
3941 | ||
3942 | if (!runtime) | |
3943 | return; | |
3944 | ||
3945 | runtime = distribute_cfs_runtime(cfs_b, runtime, expires); | |
3946 | ||
3947 | raw_spin_lock(&cfs_b->lock); | |
3948 | if (expires == cfs_b->runtime_expires) | |
c06f04c7 | 3949 | cfs_b->runtime -= min(runtime, cfs_b->runtime); |
d8b4986d PT |
3950 | raw_spin_unlock(&cfs_b->lock); |
3951 | } | |
3952 | ||
d3d9dc33 PT |
3953 | /* |
3954 | * When a group wakes up we want to make sure that its quota is not already | |
3955 | * expired/exceeded, otherwise it may be allowed to steal additional ticks of | |
3956 | * runtime as update_curr() throttling can not not trigger until it's on-rq. | |
3957 | */ | |
3958 | static void check_enqueue_throttle(struct cfs_rq *cfs_rq) | |
3959 | { | |
56f570e5 PT |
3960 | if (!cfs_bandwidth_used()) |
3961 | return; | |
3962 | ||
d3d9dc33 PT |
3963 | /* an active group must be handled by the update_curr()->put() path */ |
3964 | if (!cfs_rq->runtime_enabled || cfs_rq->curr) | |
3965 | return; | |
3966 | ||
3967 | /* ensure the group is not already throttled */ | |
3968 | if (cfs_rq_throttled(cfs_rq)) | |
3969 | return; | |
3970 | ||
3971 | /* update runtime allocation */ | |
3972 | account_cfs_rq_runtime(cfs_rq, 0); | |
3973 | if (cfs_rq->runtime_remaining <= 0) | |
3974 | throttle_cfs_rq(cfs_rq); | |
3975 | } | |
3976 | ||
3977 | /* conditionally throttle active cfs_rq's from put_prev_entity() */ | |
678d5718 | 3978 | static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq) |
d3d9dc33 | 3979 | { |
56f570e5 | 3980 | if (!cfs_bandwidth_used()) |
678d5718 | 3981 | return false; |
56f570e5 | 3982 | |
d3d9dc33 | 3983 | if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0)) |
678d5718 | 3984 | return false; |
d3d9dc33 PT |
3985 | |
3986 | /* | |
3987 | * it's possible for a throttled entity to be forced into a running | |
3988 | * state (e.g. set_curr_task), in this case we're finished. | |
3989 | */ | |
3990 | if (cfs_rq_throttled(cfs_rq)) | |
678d5718 | 3991 | return true; |
d3d9dc33 PT |
3992 | |
3993 | throttle_cfs_rq(cfs_rq); | |
678d5718 | 3994 | return true; |
d3d9dc33 | 3995 | } |
029632fb | 3996 | |
029632fb PZ |
3997 | static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer) |
3998 | { | |
3999 | struct cfs_bandwidth *cfs_b = | |
4000 | container_of(timer, struct cfs_bandwidth, slack_timer); | |
77a4d1a1 | 4001 | |
029632fb PZ |
4002 | do_sched_cfs_slack_timer(cfs_b); |
4003 | ||
4004 | return HRTIMER_NORESTART; | |
4005 | } | |
4006 | ||
4007 | static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer) | |
4008 | { | |
4009 | struct cfs_bandwidth *cfs_b = | |
4010 | container_of(timer, struct cfs_bandwidth, period_timer); | |
029632fb PZ |
4011 | int overrun; |
4012 | int idle = 0; | |
4013 | ||
51f2176d | 4014 | raw_spin_lock(&cfs_b->lock); |
029632fb | 4015 | for (;;) { |
77a4d1a1 | 4016 | overrun = hrtimer_forward_now(timer, cfs_b->period); |
029632fb PZ |
4017 | if (!overrun) |
4018 | break; | |
4019 | ||
4020 | idle = do_sched_cfs_period_timer(cfs_b, overrun); | |
4021 | } | |
4cfafd30 PZ |
4022 | if (idle) |
4023 | cfs_b->period_active = 0; | |
51f2176d | 4024 | raw_spin_unlock(&cfs_b->lock); |
029632fb PZ |
4025 | |
4026 | return idle ? HRTIMER_NORESTART : HRTIMER_RESTART; | |
4027 | } | |
4028 | ||
4029 | void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) | |
4030 | { | |
4031 | raw_spin_lock_init(&cfs_b->lock); | |
4032 | cfs_b->runtime = 0; | |
4033 | cfs_b->quota = RUNTIME_INF; | |
4034 | cfs_b->period = ns_to_ktime(default_cfs_period()); | |
4035 | ||
4036 | INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq); | |
4cfafd30 | 4037 | hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED); |
029632fb PZ |
4038 | cfs_b->period_timer.function = sched_cfs_period_timer; |
4039 | hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); | |
4040 | cfs_b->slack_timer.function = sched_cfs_slack_timer; | |
4041 | } | |
4042 | ||
4043 | static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
4044 | { | |
4045 | cfs_rq->runtime_enabled = 0; | |
4046 | INIT_LIST_HEAD(&cfs_rq->throttled_list); | |
4047 | } | |
4048 | ||
77a4d1a1 | 4049 | void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b) |
029632fb | 4050 | { |
4cfafd30 | 4051 | lockdep_assert_held(&cfs_b->lock); |
029632fb | 4052 | |
4cfafd30 PZ |
4053 | if (!cfs_b->period_active) { |
4054 | cfs_b->period_active = 1; | |
4055 | hrtimer_forward_now(&cfs_b->period_timer, cfs_b->period); | |
4056 | hrtimer_start_expires(&cfs_b->period_timer, HRTIMER_MODE_ABS_PINNED); | |
4057 | } | |
029632fb PZ |
4058 | } |
4059 | ||
4060 | static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) | |
4061 | { | |
7f1a169b TH |
4062 | /* init_cfs_bandwidth() was not called */ |
4063 | if (!cfs_b->throttled_cfs_rq.next) | |
4064 | return; | |
4065 | ||
029632fb PZ |
4066 | hrtimer_cancel(&cfs_b->period_timer); |
4067 | hrtimer_cancel(&cfs_b->slack_timer); | |
4068 | } | |
4069 | ||
0e59bdae KT |
4070 | static void __maybe_unused update_runtime_enabled(struct rq *rq) |
4071 | { | |
4072 | struct cfs_rq *cfs_rq; | |
4073 | ||
4074 | for_each_leaf_cfs_rq(rq, cfs_rq) { | |
4075 | struct cfs_bandwidth *cfs_b = &cfs_rq->tg->cfs_bandwidth; | |
4076 | ||
4077 | raw_spin_lock(&cfs_b->lock); | |
4078 | cfs_rq->runtime_enabled = cfs_b->quota != RUNTIME_INF; | |
4079 | raw_spin_unlock(&cfs_b->lock); | |
4080 | } | |
4081 | } | |
4082 | ||
38dc3348 | 4083 | static void __maybe_unused unthrottle_offline_cfs_rqs(struct rq *rq) |
029632fb PZ |
4084 | { |
4085 | struct cfs_rq *cfs_rq; | |
4086 | ||
4087 | for_each_leaf_cfs_rq(rq, cfs_rq) { | |
029632fb PZ |
4088 | if (!cfs_rq->runtime_enabled) |
4089 | continue; | |
4090 | ||
4091 | /* | |
4092 | * clock_task is not advancing so we just need to make sure | |
4093 | * there's some valid quota amount | |
4094 | */ | |
51f2176d | 4095 | cfs_rq->runtime_remaining = 1; |
0e59bdae KT |
4096 | /* |
4097 | * Offline rq is schedulable till cpu is completely disabled | |
4098 | * in take_cpu_down(), so we prevent new cfs throttling here. | |
4099 | */ | |
4100 | cfs_rq->runtime_enabled = 0; | |
4101 | ||
029632fb PZ |
4102 | if (cfs_rq_throttled(cfs_rq)) |
4103 | unthrottle_cfs_rq(cfs_rq); | |
4104 | } | |
4105 | } | |
4106 | ||
4107 | #else /* CONFIG_CFS_BANDWIDTH */ | |
f1b17280 PT |
4108 | static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq) |
4109 | { | |
78becc27 | 4110 | return rq_clock_task(rq_of(cfs_rq)); |
f1b17280 PT |
4111 | } |
4112 | ||
9dbdb155 | 4113 | static void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec) {} |
678d5718 | 4114 | static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq) { return false; } |
d3d9dc33 | 4115 | static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {} |
6c16a6dc | 4116 | static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {} |
85dac906 PT |
4117 | |
4118 | static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq) | |
4119 | { | |
4120 | return 0; | |
4121 | } | |
64660c86 PT |
4122 | |
4123 | static inline int throttled_hierarchy(struct cfs_rq *cfs_rq) | |
4124 | { | |
4125 | return 0; | |
4126 | } | |
4127 | ||
4128 | static inline int throttled_lb_pair(struct task_group *tg, | |
4129 | int src_cpu, int dest_cpu) | |
4130 | { | |
4131 | return 0; | |
4132 | } | |
029632fb PZ |
4133 | |
4134 | void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {} | |
4135 | ||
4136 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
4137 | static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {} | |
ab84d31e PT |
4138 | #endif |
4139 | ||
029632fb PZ |
4140 | static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg) |
4141 | { | |
4142 | return NULL; | |
4143 | } | |
4144 | static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {} | |
0e59bdae | 4145 | static inline void update_runtime_enabled(struct rq *rq) {} |
a4c96ae3 | 4146 | static inline void unthrottle_offline_cfs_rqs(struct rq *rq) {} |
029632fb PZ |
4147 | |
4148 | #endif /* CONFIG_CFS_BANDWIDTH */ | |
4149 | ||
bf0f6f24 IM |
4150 | /************************************************** |
4151 | * CFS operations on tasks: | |
4152 | */ | |
4153 | ||
8f4d37ec PZ |
4154 | #ifdef CONFIG_SCHED_HRTICK |
4155 | static void hrtick_start_fair(struct rq *rq, struct task_struct *p) | |
4156 | { | |
8f4d37ec PZ |
4157 | struct sched_entity *se = &p->se; |
4158 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
4159 | ||
4160 | WARN_ON(task_rq(p) != rq); | |
4161 | ||
b39e66ea | 4162 | if (cfs_rq->nr_running > 1) { |
8f4d37ec PZ |
4163 | u64 slice = sched_slice(cfs_rq, se); |
4164 | u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime; | |
4165 | s64 delta = slice - ran; | |
4166 | ||
4167 | if (delta < 0) { | |
4168 | if (rq->curr == p) | |
8875125e | 4169 | resched_curr(rq); |
8f4d37ec PZ |
4170 | return; |
4171 | } | |
31656519 | 4172 | hrtick_start(rq, delta); |
8f4d37ec PZ |
4173 | } |
4174 | } | |
a4c2f00f PZ |
4175 | |
4176 | /* | |
4177 | * called from enqueue/dequeue and updates the hrtick when the | |
4178 | * current task is from our class and nr_running is low enough | |
4179 | * to matter. | |
4180 | */ | |
4181 | static void hrtick_update(struct rq *rq) | |
4182 | { | |
4183 | struct task_struct *curr = rq->curr; | |
4184 | ||
b39e66ea | 4185 | if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class) |
a4c2f00f PZ |
4186 | return; |
4187 | ||
4188 | if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency) | |
4189 | hrtick_start_fair(rq, curr); | |
4190 | } | |
55e12e5e | 4191 | #else /* !CONFIG_SCHED_HRTICK */ |
8f4d37ec PZ |
4192 | static inline void |
4193 | hrtick_start_fair(struct rq *rq, struct task_struct *p) | |
4194 | { | |
4195 | } | |
a4c2f00f PZ |
4196 | |
4197 | static inline void hrtick_update(struct rq *rq) | |
4198 | { | |
4199 | } | |
8f4d37ec PZ |
4200 | #endif |
4201 | ||
bf0f6f24 IM |
4202 | /* |
4203 | * The enqueue_task method is called before nr_running is | |
4204 | * increased. Here we update the fair scheduling stats and | |
4205 | * then put the task into the rbtree: | |
4206 | */ | |
ea87bb78 | 4207 | static void |
371fd7e7 | 4208 | enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags) |
bf0f6f24 IM |
4209 | { |
4210 | struct cfs_rq *cfs_rq; | |
62fb1851 | 4211 | struct sched_entity *se = &p->se; |
bf0f6f24 IM |
4212 | |
4213 | for_each_sched_entity(se) { | |
62fb1851 | 4214 | if (se->on_rq) |
bf0f6f24 IM |
4215 | break; |
4216 | cfs_rq = cfs_rq_of(se); | |
88ec22d3 | 4217 | enqueue_entity(cfs_rq, se, flags); |
85dac906 PT |
4218 | |
4219 | /* | |
4220 | * end evaluation on encountering a throttled cfs_rq | |
4221 | * | |
4222 | * note: in the case of encountering a throttled cfs_rq we will | |
4223 | * post the final h_nr_running increment below. | |
4224 | */ | |
4225 | if (cfs_rq_throttled(cfs_rq)) | |
4226 | break; | |
953bfcd1 | 4227 | cfs_rq->h_nr_running++; |
85dac906 | 4228 | |
88ec22d3 | 4229 | flags = ENQUEUE_WAKEUP; |
bf0f6f24 | 4230 | } |
8f4d37ec | 4231 | |
2069dd75 | 4232 | for_each_sched_entity(se) { |
0f317143 | 4233 | cfs_rq = cfs_rq_of(se); |
953bfcd1 | 4234 | cfs_rq->h_nr_running++; |
2069dd75 | 4235 | |
85dac906 PT |
4236 | if (cfs_rq_throttled(cfs_rq)) |
4237 | break; | |
4238 | ||
9d89c257 | 4239 | update_load_avg(se, 1); |
17bc14b7 | 4240 | update_cfs_shares(cfs_rq); |
2069dd75 PZ |
4241 | } |
4242 | ||
cd126afe | 4243 | if (!se) |
72465447 | 4244 | add_nr_running(rq, 1); |
cd126afe | 4245 | |
a4c2f00f | 4246 | hrtick_update(rq); |
bf0f6f24 IM |
4247 | } |
4248 | ||
2f36825b VP |
4249 | static void set_next_buddy(struct sched_entity *se); |
4250 | ||
bf0f6f24 IM |
4251 | /* |
4252 | * The dequeue_task method is called before nr_running is | |
4253 | * decreased. We remove the task from the rbtree and | |
4254 | * update the fair scheduling stats: | |
4255 | */ | |
371fd7e7 | 4256 | static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags) |
bf0f6f24 IM |
4257 | { |
4258 | struct cfs_rq *cfs_rq; | |
62fb1851 | 4259 | struct sched_entity *se = &p->se; |
2f36825b | 4260 | int task_sleep = flags & DEQUEUE_SLEEP; |
bf0f6f24 IM |
4261 | |
4262 | for_each_sched_entity(se) { | |
4263 | cfs_rq = cfs_rq_of(se); | |
371fd7e7 | 4264 | dequeue_entity(cfs_rq, se, flags); |
85dac906 PT |
4265 | |
4266 | /* | |
4267 | * end evaluation on encountering a throttled cfs_rq | |
4268 | * | |
4269 | * note: in the case of encountering a throttled cfs_rq we will | |
4270 | * post the final h_nr_running decrement below. | |
4271 | */ | |
4272 | if (cfs_rq_throttled(cfs_rq)) | |
4273 | break; | |
953bfcd1 | 4274 | cfs_rq->h_nr_running--; |
2069dd75 | 4275 | |
bf0f6f24 | 4276 | /* Don't dequeue parent if it has other entities besides us */ |
2f36825b VP |
4277 | if (cfs_rq->load.weight) { |
4278 | /* | |
4279 | * Bias pick_next to pick a task from this cfs_rq, as | |
4280 | * p is sleeping when it is within its sched_slice. | |
4281 | */ | |
4282 | if (task_sleep && parent_entity(se)) | |
4283 | set_next_buddy(parent_entity(se)); | |
9598c82d PT |
4284 | |
4285 | /* avoid re-evaluating load for this entity */ | |
4286 | se = parent_entity(se); | |
bf0f6f24 | 4287 | break; |
2f36825b | 4288 | } |
371fd7e7 | 4289 | flags |= DEQUEUE_SLEEP; |
bf0f6f24 | 4290 | } |
8f4d37ec | 4291 | |
2069dd75 | 4292 | for_each_sched_entity(se) { |
0f317143 | 4293 | cfs_rq = cfs_rq_of(se); |
953bfcd1 | 4294 | cfs_rq->h_nr_running--; |
2069dd75 | 4295 | |
85dac906 PT |
4296 | if (cfs_rq_throttled(cfs_rq)) |
4297 | break; | |
4298 | ||
9d89c257 | 4299 | update_load_avg(se, 1); |
17bc14b7 | 4300 | update_cfs_shares(cfs_rq); |
2069dd75 PZ |
4301 | } |
4302 | ||
cd126afe | 4303 | if (!se) |
72465447 | 4304 | sub_nr_running(rq, 1); |
cd126afe | 4305 | |
a4c2f00f | 4306 | hrtick_update(rq); |
bf0f6f24 IM |
4307 | } |
4308 | ||
e7693a36 | 4309 | #ifdef CONFIG_SMP |
3289bdb4 PZ |
4310 | |
4311 | /* | |
4312 | * per rq 'load' arrray crap; XXX kill this. | |
4313 | */ | |
4314 | ||
4315 | /* | |
d937cdc5 | 4316 | * The exact cpuload calculated at every tick would be: |
3289bdb4 | 4317 | * |
d937cdc5 PZ |
4318 | * load' = (1 - 1/2^i) * load + (1/2^i) * cur_load |
4319 | * | |
4320 | * If a cpu misses updates for n ticks (as it was idle) and update gets | |
4321 | * called on the n+1-th tick when cpu may be busy, then we have: | |
4322 | * | |
4323 | * load_n = (1 - 1/2^i)^n * load_0 | |
4324 | * load_n+1 = (1 - 1/2^i) * load_n + (1/2^i) * cur_load | |
3289bdb4 PZ |
4325 | * |
4326 | * decay_load_missed() below does efficient calculation of | |
3289bdb4 | 4327 | * |
d937cdc5 PZ |
4328 | * load' = (1 - 1/2^i)^n * load |
4329 | * | |
4330 | * Because x^(n+m) := x^n * x^m we can decompose any x^n in power-of-2 factors. | |
4331 | * This allows us to precompute the above in said factors, thereby allowing the | |
4332 | * reduction of an arbitrary n in O(log_2 n) steps. (See also | |
4333 | * fixed_power_int()) | |
3289bdb4 | 4334 | * |
d937cdc5 | 4335 | * The calculation is approximated on a 128 point scale. |
3289bdb4 PZ |
4336 | */ |
4337 | #define DEGRADE_SHIFT 7 | |
d937cdc5 PZ |
4338 | |
4339 | static const u8 degrade_zero_ticks[CPU_LOAD_IDX_MAX] = {0, 8, 32, 64, 128}; | |
4340 | static const u8 degrade_factor[CPU_LOAD_IDX_MAX][DEGRADE_SHIFT + 1] = { | |
4341 | { 0, 0, 0, 0, 0, 0, 0, 0 }, | |
4342 | { 64, 32, 8, 0, 0, 0, 0, 0 }, | |
4343 | { 96, 72, 40, 12, 1, 0, 0, 0 }, | |
4344 | { 112, 98, 75, 43, 15, 1, 0, 0 }, | |
4345 | { 120, 112, 98, 76, 45, 16, 2, 0 } | |
4346 | }; | |
3289bdb4 PZ |
4347 | |
4348 | /* | |
4349 | * Update cpu_load for any missed ticks, due to tickless idle. The backlog | |
4350 | * would be when CPU is idle and so we just decay the old load without | |
4351 | * adding any new load. | |
4352 | */ | |
4353 | static unsigned long | |
4354 | decay_load_missed(unsigned long load, unsigned long missed_updates, int idx) | |
4355 | { | |
4356 | int j = 0; | |
4357 | ||
4358 | if (!missed_updates) | |
4359 | return load; | |
4360 | ||
4361 | if (missed_updates >= degrade_zero_ticks[idx]) | |
4362 | return 0; | |
4363 | ||
4364 | if (idx == 1) | |
4365 | return load >> missed_updates; | |
4366 | ||
4367 | while (missed_updates) { | |
4368 | if (missed_updates % 2) | |
4369 | load = (load * degrade_factor[idx][j]) >> DEGRADE_SHIFT; | |
4370 | ||
4371 | missed_updates >>= 1; | |
4372 | j++; | |
4373 | } | |
4374 | return load; | |
4375 | } | |
4376 | ||
59543275 BP |
4377 | /** |
4378 | * __update_cpu_load - update the rq->cpu_load[] statistics | |
4379 | * @this_rq: The rq to update statistics for | |
4380 | * @this_load: The current load | |
4381 | * @pending_updates: The number of missed updates | |
4382 | * @active: !0 for NOHZ_FULL | |
4383 | * | |
3289bdb4 | 4384 | * Update rq->cpu_load[] statistics. This function is usually called every |
59543275 BP |
4385 | * scheduler tick (TICK_NSEC). |
4386 | * | |
4387 | * This function computes a decaying average: | |
4388 | * | |
4389 | * load[i]' = (1 - 1/2^i) * load[i] + (1/2^i) * load | |
4390 | * | |
4391 | * Because of NOHZ it might not get called on every tick which gives need for | |
4392 | * the @pending_updates argument. | |
4393 | * | |
4394 | * load[i]_n = (1 - 1/2^i) * load[i]_n-1 + (1/2^i) * load_n-1 | |
4395 | * = A * load[i]_n-1 + B ; A := (1 - 1/2^i), B := (1/2^i) * load | |
4396 | * = A * (A * load[i]_n-2 + B) + B | |
4397 | * = A * (A * (A * load[i]_n-3 + B) + B) + B | |
4398 | * = A^3 * load[i]_n-3 + (A^2 + A + 1) * B | |
4399 | * = A^n * load[i]_0 + (A^(n-1) + A^(n-2) + ... + 1) * B | |
4400 | * = A^n * load[i]_0 + ((1 - A^n) / (1 - A)) * B | |
4401 | * = (1 - 1/2^i)^n * (load[i]_0 - load) + load | |
4402 | * | |
4403 | * In the above we've assumed load_n := load, which is true for NOHZ_FULL as | |
4404 | * any change in load would have resulted in the tick being turned back on. | |
4405 | * | |
4406 | * For regular NOHZ, this reduces to: | |
4407 | * | |
4408 | * load[i]_n = (1 - 1/2^i)^n * load[i]_0 | |
4409 | * | |
4410 | * see decay_load_misses(). For NOHZ_FULL we get to subtract and add the extra | |
4411 | * term. See the @active paramter. | |
3289bdb4 PZ |
4412 | */ |
4413 | static void __update_cpu_load(struct rq *this_rq, unsigned long this_load, | |
59543275 | 4414 | unsigned long pending_updates, int active) |
3289bdb4 | 4415 | { |
59543275 | 4416 | unsigned long tickless_load = active ? this_rq->cpu_load[0] : 0; |
3289bdb4 PZ |
4417 | int i, scale; |
4418 | ||
4419 | this_rq->nr_load_updates++; | |
4420 | ||
4421 | /* Update our load: */ | |
4422 | this_rq->cpu_load[0] = this_load; /* Fasttrack for idx 0 */ | |
4423 | for (i = 1, scale = 2; i < CPU_LOAD_IDX_MAX; i++, scale += scale) { | |
4424 | unsigned long old_load, new_load; | |
4425 | ||
4426 | /* scale is effectively 1 << i now, and >> i divides by scale */ | |
4427 | ||
59543275 | 4428 | old_load = this_rq->cpu_load[i] - tickless_load; |
3289bdb4 | 4429 | old_load = decay_load_missed(old_load, pending_updates - 1, i); |
59543275 | 4430 | old_load += tickless_load; |
3289bdb4 PZ |
4431 | new_load = this_load; |
4432 | /* | |
4433 | * Round up the averaging division if load is increasing. This | |
4434 | * prevents us from getting stuck on 9 if the load is 10, for | |
4435 | * example. | |
4436 | */ | |
4437 | if (new_load > old_load) | |
4438 | new_load += scale - 1; | |
4439 | ||
4440 | this_rq->cpu_load[i] = (old_load * (scale - 1) + new_load) >> i; | |
4441 | } | |
4442 | ||
4443 | sched_avg_update(this_rq); | |
4444 | } | |
4445 | ||
7ea241af YD |
4446 | /* Used instead of source_load when we know the type == 0 */ |
4447 | static unsigned long weighted_cpuload(const int cpu) | |
4448 | { | |
4449 | return cfs_rq_runnable_load_avg(&cpu_rq(cpu)->cfs); | |
4450 | } | |
4451 | ||
3289bdb4 PZ |
4452 | #ifdef CONFIG_NO_HZ_COMMON |
4453 | /* | |
4454 | * There is no sane way to deal with nohz on smp when using jiffies because the | |
4455 | * cpu doing the jiffies update might drift wrt the cpu doing the jiffy reading | |
4456 | * causing off-by-one errors in observed deltas; {0,2} instead of {1,1}. | |
4457 | * | |
4458 | * Therefore we cannot use the delta approach from the regular tick since that | |
4459 | * would seriously skew the load calculation. However we'll make do for those | |
4460 | * updates happening while idle (nohz_idle_balance) or coming out of idle | |
4461 | * (tick_nohz_idle_exit). | |
4462 | * | |
4463 | * This means we might still be one tick off for nohz periods. | |
4464 | */ | |
4465 | ||
4466 | /* | |
4467 | * Called from nohz_idle_balance() to update the load ratings before doing the | |
4468 | * idle balance. | |
4469 | */ | |
4470 | static void update_idle_cpu_load(struct rq *this_rq) | |
4471 | { | |
316c1608 | 4472 | unsigned long curr_jiffies = READ_ONCE(jiffies); |
7ea241af | 4473 | unsigned long load = weighted_cpuload(cpu_of(this_rq)); |
3289bdb4 PZ |
4474 | unsigned long pending_updates; |
4475 | ||
4476 | /* | |
4477 | * bail if there's load or we're actually up-to-date. | |
4478 | */ | |
4479 | if (load || curr_jiffies == this_rq->last_load_update_tick) | |
4480 | return; | |
4481 | ||
4482 | pending_updates = curr_jiffies - this_rq->last_load_update_tick; | |
4483 | this_rq->last_load_update_tick = curr_jiffies; | |
4484 | ||
59543275 | 4485 | __update_cpu_load(this_rq, load, pending_updates, 0); |
3289bdb4 PZ |
4486 | } |
4487 | ||
4488 | /* | |
4489 | * Called from tick_nohz_idle_exit() -- try and fix up the ticks we missed. | |
4490 | */ | |
525705d1 | 4491 | void update_cpu_load_nohz(int active) |
3289bdb4 PZ |
4492 | { |
4493 | struct rq *this_rq = this_rq(); | |
316c1608 | 4494 | unsigned long curr_jiffies = READ_ONCE(jiffies); |
525705d1 | 4495 | unsigned long load = active ? weighted_cpuload(cpu_of(this_rq)) : 0; |
3289bdb4 PZ |
4496 | unsigned long pending_updates; |
4497 | ||
4498 | if (curr_jiffies == this_rq->last_load_update_tick) | |
4499 | return; | |
4500 | ||
4501 | raw_spin_lock(&this_rq->lock); | |
4502 | pending_updates = curr_jiffies - this_rq->last_load_update_tick; | |
4503 | if (pending_updates) { | |
4504 | this_rq->last_load_update_tick = curr_jiffies; | |
4505 | /* | |
525705d1 BP |
4506 | * In the regular NOHZ case, we were idle, this means load 0. |
4507 | * In the NOHZ_FULL case, we were non-idle, we should consider | |
4508 | * its weighted load. | |
3289bdb4 | 4509 | */ |
525705d1 | 4510 | __update_cpu_load(this_rq, load, pending_updates, active); |
3289bdb4 PZ |
4511 | } |
4512 | raw_spin_unlock(&this_rq->lock); | |
4513 | } | |
4514 | #endif /* CONFIG_NO_HZ */ | |
4515 | ||
4516 | /* | |
4517 | * Called from scheduler_tick() | |
4518 | */ | |
4519 | void update_cpu_load_active(struct rq *this_rq) | |
4520 | { | |
7ea241af | 4521 | unsigned long load = weighted_cpuload(cpu_of(this_rq)); |
3289bdb4 PZ |
4522 | /* |
4523 | * See the mess around update_idle_cpu_load() / update_cpu_load_nohz(). | |
4524 | */ | |
4525 | this_rq->last_load_update_tick = jiffies; | |
59543275 | 4526 | __update_cpu_load(this_rq, load, 1, 1); |
3289bdb4 PZ |
4527 | } |
4528 | ||
029632fb PZ |
4529 | /* |
4530 | * Return a low guess at the load of a migration-source cpu weighted | |
4531 | * according to the scheduling class and "nice" value. | |
4532 | * | |
4533 | * We want to under-estimate the load of migration sources, to | |
4534 | * balance conservatively. | |
4535 | */ | |
4536 | static unsigned long source_load(int cpu, int type) | |
4537 | { | |
4538 | struct rq *rq = cpu_rq(cpu); | |
4539 | unsigned long total = weighted_cpuload(cpu); | |
4540 | ||
4541 | if (type == 0 || !sched_feat(LB_BIAS)) | |
4542 | return total; | |
4543 | ||
4544 | return min(rq->cpu_load[type-1], total); | |
4545 | } | |
4546 | ||
4547 | /* | |
4548 | * Return a high guess at the load of a migration-target cpu weighted | |
4549 | * according to the scheduling class and "nice" value. | |
4550 | */ | |
4551 | static unsigned long target_load(int cpu, int type) | |
4552 | { | |
4553 | struct rq *rq = cpu_rq(cpu); | |
4554 | unsigned long total = weighted_cpuload(cpu); | |
4555 | ||
4556 | if (type == 0 || !sched_feat(LB_BIAS)) | |
4557 | return total; | |
4558 | ||
4559 | return max(rq->cpu_load[type-1], total); | |
4560 | } | |
4561 | ||
ced549fa | 4562 | static unsigned long capacity_of(int cpu) |
029632fb | 4563 | { |
ced549fa | 4564 | return cpu_rq(cpu)->cpu_capacity; |
029632fb PZ |
4565 | } |
4566 | ||
ca6d75e6 VG |
4567 | static unsigned long capacity_orig_of(int cpu) |
4568 | { | |
4569 | return cpu_rq(cpu)->cpu_capacity_orig; | |
4570 | } | |
4571 | ||
029632fb PZ |
4572 | static unsigned long cpu_avg_load_per_task(int cpu) |
4573 | { | |
4574 | struct rq *rq = cpu_rq(cpu); | |
316c1608 | 4575 | unsigned long nr_running = READ_ONCE(rq->cfs.h_nr_running); |
7ea241af | 4576 | unsigned long load_avg = weighted_cpuload(cpu); |
029632fb PZ |
4577 | |
4578 | if (nr_running) | |
b92486cb | 4579 | return load_avg / nr_running; |
029632fb PZ |
4580 | |
4581 | return 0; | |
4582 | } | |
4583 | ||
62470419 MW |
4584 | static void record_wakee(struct task_struct *p) |
4585 | { | |
4586 | /* | |
4587 | * Rough decay (wiping) for cost saving, don't worry | |
4588 | * about the boundary, really active task won't care | |
4589 | * about the loss. | |
4590 | */ | |
2538d960 | 4591 | if (time_after(jiffies, current->wakee_flip_decay_ts + HZ)) { |
096aa338 | 4592 | current->wakee_flips >>= 1; |
62470419 MW |
4593 | current->wakee_flip_decay_ts = jiffies; |
4594 | } | |
4595 | ||
4596 | if (current->last_wakee != p) { | |
4597 | current->last_wakee = p; | |
4598 | current->wakee_flips++; | |
4599 | } | |
4600 | } | |
098fb9db | 4601 | |
74f8e4b2 | 4602 | static void task_waking_fair(struct task_struct *p) |
88ec22d3 PZ |
4603 | { |
4604 | struct sched_entity *se = &p->se; | |
4605 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
3fe1698b PZ |
4606 | u64 min_vruntime; |
4607 | ||
4608 | #ifndef CONFIG_64BIT | |
4609 | u64 min_vruntime_copy; | |
88ec22d3 | 4610 | |
3fe1698b PZ |
4611 | do { |
4612 | min_vruntime_copy = cfs_rq->min_vruntime_copy; | |
4613 | smp_rmb(); | |
4614 | min_vruntime = cfs_rq->min_vruntime; | |
4615 | } while (min_vruntime != min_vruntime_copy); | |
4616 | #else | |
4617 | min_vruntime = cfs_rq->min_vruntime; | |
4618 | #endif | |
88ec22d3 | 4619 | |
3fe1698b | 4620 | se->vruntime -= min_vruntime; |
62470419 | 4621 | record_wakee(p); |
88ec22d3 PZ |
4622 | } |
4623 | ||
bb3469ac | 4624 | #ifdef CONFIG_FAIR_GROUP_SCHED |
f5bfb7d9 PZ |
4625 | /* |
4626 | * effective_load() calculates the load change as seen from the root_task_group | |
4627 | * | |
4628 | * Adding load to a group doesn't make a group heavier, but can cause movement | |
4629 | * of group shares between cpus. Assuming the shares were perfectly aligned one | |
4630 | * can calculate the shift in shares. | |
cf5f0acf PZ |
4631 | * |
4632 | * Calculate the effective load difference if @wl is added (subtracted) to @tg | |
4633 | * on this @cpu and results in a total addition (subtraction) of @wg to the | |
4634 | * total group weight. | |
4635 | * | |
4636 | * Given a runqueue weight distribution (rw_i) we can compute a shares | |
4637 | * distribution (s_i) using: | |
4638 | * | |
4639 | * s_i = rw_i / \Sum rw_j (1) | |
4640 | * | |
4641 | * Suppose we have 4 CPUs and our @tg is a direct child of the root group and | |
4642 | * has 7 equal weight tasks, distributed as below (rw_i), with the resulting | |
4643 | * shares distribution (s_i): | |
4644 | * | |
4645 | * rw_i = { 2, 4, 1, 0 } | |
4646 | * s_i = { 2/7, 4/7, 1/7, 0 } | |
4647 | * | |
4648 | * As per wake_affine() we're interested in the load of two CPUs (the CPU the | |
4649 | * task used to run on and the CPU the waker is running on), we need to | |
4650 | * compute the effect of waking a task on either CPU and, in case of a sync | |
4651 | * wakeup, compute the effect of the current task going to sleep. | |
4652 | * | |
4653 | * So for a change of @wl to the local @cpu with an overall group weight change | |
4654 | * of @wl we can compute the new shares distribution (s'_i) using: | |
4655 | * | |
4656 | * s'_i = (rw_i + @wl) / (@wg + \Sum rw_j) (2) | |
4657 | * | |
4658 | * Suppose we're interested in CPUs 0 and 1, and want to compute the load | |
4659 | * differences in waking a task to CPU 0. The additional task changes the | |
4660 | * weight and shares distributions like: | |
4661 | * | |
4662 | * rw'_i = { 3, 4, 1, 0 } | |
4663 | * s'_i = { 3/8, 4/8, 1/8, 0 } | |
4664 | * | |
4665 | * We can then compute the difference in effective weight by using: | |
4666 | * | |
4667 | * dw_i = S * (s'_i - s_i) (3) | |
4668 | * | |
4669 | * Where 'S' is the group weight as seen by its parent. | |
4670 | * | |
4671 | * Therefore the effective change in loads on CPU 0 would be 5/56 (3/8 - 2/7) | |
4672 | * times the weight of the group. The effect on CPU 1 would be -4/56 (4/8 - | |
4673 | * 4/7) times the weight of the group. | |
f5bfb7d9 | 4674 | */ |
2069dd75 | 4675 | static long effective_load(struct task_group *tg, int cpu, long wl, long wg) |
bb3469ac | 4676 | { |
4be9daaa | 4677 | struct sched_entity *se = tg->se[cpu]; |
f1d239f7 | 4678 | |
9722c2da | 4679 | if (!tg->parent) /* the trivial, non-cgroup case */ |
f1d239f7 PZ |
4680 | return wl; |
4681 | ||
4be9daaa | 4682 | for_each_sched_entity(se) { |
cf5f0acf | 4683 | long w, W; |
4be9daaa | 4684 | |
977dda7c | 4685 | tg = se->my_q->tg; |
bb3469ac | 4686 | |
cf5f0acf PZ |
4687 | /* |
4688 | * W = @wg + \Sum rw_j | |
4689 | */ | |
4690 | W = wg + calc_tg_weight(tg, se->my_q); | |
4be9daaa | 4691 | |
cf5f0acf PZ |
4692 | /* |
4693 | * w = rw_i + @wl | |
4694 | */ | |
7ea241af | 4695 | w = cfs_rq_load_avg(se->my_q) + wl; |
940959e9 | 4696 | |
cf5f0acf PZ |
4697 | /* |
4698 | * wl = S * s'_i; see (2) | |
4699 | */ | |
4700 | if (W > 0 && w < W) | |
32a8df4e | 4701 | wl = (w * (long)tg->shares) / W; |
977dda7c PT |
4702 | else |
4703 | wl = tg->shares; | |
940959e9 | 4704 | |
cf5f0acf PZ |
4705 | /* |
4706 | * Per the above, wl is the new se->load.weight value; since | |
4707 | * those are clipped to [MIN_SHARES, ...) do so now. See | |
4708 | * calc_cfs_shares(). | |
4709 | */ | |
977dda7c PT |
4710 | if (wl < MIN_SHARES) |
4711 | wl = MIN_SHARES; | |
cf5f0acf PZ |
4712 | |
4713 | /* | |
4714 | * wl = dw_i = S * (s'_i - s_i); see (3) | |
4715 | */ | |
9d89c257 | 4716 | wl -= se->avg.load_avg; |
cf5f0acf PZ |
4717 | |
4718 | /* | |
4719 | * Recursively apply this logic to all parent groups to compute | |
4720 | * the final effective load change on the root group. Since | |
4721 | * only the @tg group gets extra weight, all parent groups can | |
4722 | * only redistribute existing shares. @wl is the shift in shares | |
4723 | * resulting from this level per the above. | |
4724 | */ | |
4be9daaa | 4725 | wg = 0; |
4be9daaa | 4726 | } |
bb3469ac | 4727 | |
4be9daaa | 4728 | return wl; |
bb3469ac PZ |
4729 | } |
4730 | #else | |
4be9daaa | 4731 | |
58d081b5 | 4732 | static long effective_load(struct task_group *tg, int cpu, long wl, long wg) |
4be9daaa | 4733 | { |
83378269 | 4734 | return wl; |
bb3469ac | 4735 | } |
4be9daaa | 4736 | |
bb3469ac PZ |
4737 | #endif |
4738 | ||
63b0e9ed MG |
4739 | /* |
4740 | * Detect M:N waker/wakee relationships via a switching-frequency heuristic. | |
4741 | * A waker of many should wake a different task than the one last awakened | |
4742 | * at a frequency roughly N times higher than one of its wakees. In order | |
4743 | * to determine whether we should let the load spread vs consolodating to | |
4744 | * shared cache, we look for a minimum 'flip' frequency of llc_size in one | |
4745 | * partner, and a factor of lls_size higher frequency in the other. With | |
4746 | * both conditions met, we can be relatively sure that the relationship is | |
4747 | * non-monogamous, with partner count exceeding socket size. Waker/wakee | |
4748 | * being client/server, worker/dispatcher, interrupt source or whatever is | |
4749 | * irrelevant, spread criteria is apparent partner count exceeds socket size. | |
4750 | */ | |
62470419 MW |
4751 | static int wake_wide(struct task_struct *p) |
4752 | { | |
63b0e9ed MG |
4753 | unsigned int master = current->wakee_flips; |
4754 | unsigned int slave = p->wakee_flips; | |
7d9ffa89 | 4755 | int factor = this_cpu_read(sd_llc_size); |
62470419 | 4756 | |
63b0e9ed MG |
4757 | if (master < slave) |
4758 | swap(master, slave); | |
4759 | if (slave < factor || master < slave * factor) | |
4760 | return 0; | |
4761 | return 1; | |
62470419 MW |
4762 | } |
4763 | ||
c88d5910 | 4764 | static int wake_affine(struct sched_domain *sd, struct task_struct *p, int sync) |
098fb9db | 4765 | { |
e37b6a7b | 4766 | s64 this_load, load; |
bd61c98f | 4767 | s64 this_eff_load, prev_eff_load; |
c88d5910 | 4768 | int idx, this_cpu, prev_cpu; |
c88d5910 | 4769 | struct task_group *tg; |
83378269 | 4770 | unsigned long weight; |
b3137bc8 | 4771 | int balanced; |
098fb9db | 4772 | |
c88d5910 PZ |
4773 | idx = sd->wake_idx; |
4774 | this_cpu = smp_processor_id(); | |
4775 | prev_cpu = task_cpu(p); | |
4776 | load = source_load(prev_cpu, idx); | |
4777 | this_load = target_load(this_cpu, idx); | |
098fb9db | 4778 | |
b3137bc8 MG |
4779 | /* |
4780 | * If sync wakeup then subtract the (maximum possible) | |
4781 | * effect of the currently running task from the load | |
4782 | * of the current CPU: | |
4783 | */ | |
83378269 PZ |
4784 | if (sync) { |
4785 | tg = task_group(current); | |
9d89c257 | 4786 | weight = current->se.avg.load_avg; |
83378269 | 4787 | |
c88d5910 | 4788 | this_load += effective_load(tg, this_cpu, -weight, -weight); |
83378269 PZ |
4789 | load += effective_load(tg, prev_cpu, 0, -weight); |
4790 | } | |
b3137bc8 | 4791 | |
83378269 | 4792 | tg = task_group(p); |
9d89c257 | 4793 | weight = p->se.avg.load_avg; |
b3137bc8 | 4794 | |
71a29aa7 PZ |
4795 | /* |
4796 | * In low-load situations, where prev_cpu is idle and this_cpu is idle | |
c88d5910 PZ |
4797 | * due to the sync cause above having dropped this_load to 0, we'll |
4798 | * always have an imbalance, but there's really nothing you can do | |
4799 | * about that, so that's good too. | |
71a29aa7 PZ |
4800 | * |
4801 | * Otherwise check if either cpus are near enough in load to allow this | |
4802 | * task to be woken on this_cpu. | |
4803 | */ | |
bd61c98f VG |
4804 | this_eff_load = 100; |
4805 | this_eff_load *= capacity_of(prev_cpu); | |
e51fd5e2 | 4806 | |
bd61c98f VG |
4807 | prev_eff_load = 100 + (sd->imbalance_pct - 100) / 2; |
4808 | prev_eff_load *= capacity_of(this_cpu); | |
e51fd5e2 | 4809 | |
bd61c98f | 4810 | if (this_load > 0) { |
e51fd5e2 PZ |
4811 | this_eff_load *= this_load + |
4812 | effective_load(tg, this_cpu, weight, weight); | |
4813 | ||
e51fd5e2 | 4814 | prev_eff_load *= load + effective_load(tg, prev_cpu, 0, weight); |
bd61c98f | 4815 | } |
e51fd5e2 | 4816 | |
bd61c98f | 4817 | balanced = this_eff_load <= prev_eff_load; |
098fb9db | 4818 | |
41acab88 | 4819 | schedstat_inc(p, se.statistics.nr_wakeups_affine_attempts); |
098fb9db | 4820 | |
05bfb65f VG |
4821 | if (!balanced) |
4822 | return 0; | |
098fb9db | 4823 | |
05bfb65f VG |
4824 | schedstat_inc(sd, ttwu_move_affine); |
4825 | schedstat_inc(p, se.statistics.nr_wakeups_affine); | |
4826 | ||
4827 | return 1; | |
098fb9db IM |
4828 | } |
4829 | ||
aaee1203 PZ |
4830 | /* |
4831 | * find_idlest_group finds and returns the least busy CPU group within the | |
4832 | * domain. | |
4833 | */ | |
4834 | static struct sched_group * | |
78e7ed53 | 4835 | find_idlest_group(struct sched_domain *sd, struct task_struct *p, |
c44f2a02 | 4836 | int this_cpu, int sd_flag) |
e7693a36 | 4837 | { |
b3bd3de6 | 4838 | struct sched_group *idlest = NULL, *group = sd->groups; |
aaee1203 | 4839 | unsigned long min_load = ULONG_MAX, this_load = 0; |
c44f2a02 | 4840 | int load_idx = sd->forkexec_idx; |
aaee1203 | 4841 | int imbalance = 100 + (sd->imbalance_pct-100)/2; |
e7693a36 | 4842 | |
c44f2a02 VG |
4843 | if (sd_flag & SD_BALANCE_WAKE) |
4844 | load_idx = sd->wake_idx; | |
4845 | ||
aaee1203 PZ |
4846 | do { |
4847 | unsigned long load, avg_load; | |
4848 | int local_group; | |
4849 | int i; | |
e7693a36 | 4850 | |
aaee1203 PZ |
4851 | /* Skip over this group if it has no CPUs allowed */ |
4852 | if (!cpumask_intersects(sched_group_cpus(group), | |
fa17b507 | 4853 | tsk_cpus_allowed(p))) |
aaee1203 PZ |
4854 | continue; |
4855 | ||
4856 | local_group = cpumask_test_cpu(this_cpu, | |
4857 | sched_group_cpus(group)); | |
4858 | ||
4859 | /* Tally up the load of all CPUs in the group */ | |
4860 | avg_load = 0; | |
4861 | ||
4862 | for_each_cpu(i, sched_group_cpus(group)) { | |
4863 | /* Bias balancing toward cpus of our domain */ | |
4864 | if (local_group) | |
4865 | load = source_load(i, load_idx); | |
4866 | else | |
4867 | load = target_load(i, load_idx); | |
4868 | ||
4869 | avg_load += load; | |
4870 | } | |
4871 | ||
63b2ca30 | 4872 | /* Adjust by relative CPU capacity of the group */ |
ca8ce3d0 | 4873 | avg_load = (avg_load * SCHED_CAPACITY_SCALE) / group->sgc->capacity; |
aaee1203 PZ |
4874 | |
4875 | if (local_group) { | |
4876 | this_load = avg_load; | |
aaee1203 PZ |
4877 | } else if (avg_load < min_load) { |
4878 | min_load = avg_load; | |
4879 | idlest = group; | |
4880 | } | |
4881 | } while (group = group->next, group != sd->groups); | |
4882 | ||
4883 | if (!idlest || 100*this_load < imbalance*min_load) | |
4884 | return NULL; | |
4885 | return idlest; | |
4886 | } | |
4887 | ||
4888 | /* | |
4889 | * find_idlest_cpu - find the idlest cpu among the cpus in group. | |
4890 | */ | |
4891 | static int | |
4892 | find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu) | |
4893 | { | |
4894 | unsigned long load, min_load = ULONG_MAX; | |
83a0a96a NP |
4895 | unsigned int min_exit_latency = UINT_MAX; |
4896 | u64 latest_idle_timestamp = 0; | |
4897 | int least_loaded_cpu = this_cpu; | |
4898 | int shallowest_idle_cpu = -1; | |
aaee1203 PZ |
4899 | int i; |
4900 | ||
4901 | /* Traverse only the allowed CPUs */ | |
fa17b507 | 4902 | for_each_cpu_and(i, sched_group_cpus(group), tsk_cpus_allowed(p)) { |
83a0a96a NP |
4903 | if (idle_cpu(i)) { |
4904 | struct rq *rq = cpu_rq(i); | |
4905 | struct cpuidle_state *idle = idle_get_state(rq); | |
4906 | if (idle && idle->exit_latency < min_exit_latency) { | |
4907 | /* | |
4908 | * We give priority to a CPU whose idle state | |
4909 | * has the smallest exit latency irrespective | |
4910 | * of any idle timestamp. | |
4911 | */ | |
4912 | min_exit_latency = idle->exit_latency; | |
4913 | latest_idle_timestamp = rq->idle_stamp; | |
4914 | shallowest_idle_cpu = i; | |
4915 | } else if ((!idle || idle->exit_latency == min_exit_latency) && | |
4916 | rq->idle_stamp > latest_idle_timestamp) { | |
4917 | /* | |
4918 | * If equal or no active idle state, then | |
4919 | * the most recently idled CPU might have | |
4920 | * a warmer cache. | |
4921 | */ | |
4922 | latest_idle_timestamp = rq->idle_stamp; | |
4923 | shallowest_idle_cpu = i; | |
4924 | } | |
9f96742a | 4925 | } else if (shallowest_idle_cpu == -1) { |
83a0a96a NP |
4926 | load = weighted_cpuload(i); |
4927 | if (load < min_load || (load == min_load && i == this_cpu)) { | |
4928 | min_load = load; | |
4929 | least_loaded_cpu = i; | |
4930 | } | |
e7693a36 GH |
4931 | } |
4932 | } | |
4933 | ||
83a0a96a | 4934 | return shallowest_idle_cpu != -1 ? shallowest_idle_cpu : least_loaded_cpu; |
aaee1203 | 4935 | } |
e7693a36 | 4936 | |
a50bde51 PZ |
4937 | /* |
4938 | * Try and locate an idle CPU in the sched_domain. | |
4939 | */ | |
99bd5e2f | 4940 | static int select_idle_sibling(struct task_struct *p, int target) |
a50bde51 | 4941 | { |
99bd5e2f | 4942 | struct sched_domain *sd; |
37407ea7 | 4943 | struct sched_group *sg; |
e0a79f52 | 4944 | int i = task_cpu(p); |
a50bde51 | 4945 | |
e0a79f52 MG |
4946 | if (idle_cpu(target)) |
4947 | return target; | |
99bd5e2f SS |
4948 | |
4949 | /* | |
e0a79f52 | 4950 | * If the prevous cpu is cache affine and idle, don't be stupid. |
99bd5e2f | 4951 | */ |
e0a79f52 MG |
4952 | if (i != target && cpus_share_cache(i, target) && idle_cpu(i)) |
4953 | return i; | |
a50bde51 PZ |
4954 | |
4955 | /* | |
37407ea7 | 4956 | * Otherwise, iterate the domains and find an elegible idle cpu. |
a50bde51 | 4957 | */ |
518cd623 | 4958 | sd = rcu_dereference(per_cpu(sd_llc, target)); |
970e1789 | 4959 | for_each_lower_domain(sd) { |
37407ea7 LT |
4960 | sg = sd->groups; |
4961 | do { | |
4962 | if (!cpumask_intersects(sched_group_cpus(sg), | |
4963 | tsk_cpus_allowed(p))) | |
4964 | goto next; | |
4965 | ||
4966 | for_each_cpu(i, sched_group_cpus(sg)) { | |
e0a79f52 | 4967 | if (i == target || !idle_cpu(i)) |
37407ea7 LT |
4968 | goto next; |
4969 | } | |
970e1789 | 4970 | |
37407ea7 LT |
4971 | target = cpumask_first_and(sched_group_cpus(sg), |
4972 | tsk_cpus_allowed(p)); | |
4973 | goto done; | |
4974 | next: | |
4975 | sg = sg->next; | |
4976 | } while (sg != sd->groups); | |
4977 | } | |
4978 | done: | |
a50bde51 PZ |
4979 | return target; |
4980 | } | |
231678b7 | 4981 | |
8bb5b00c | 4982 | /* |
9e91d61d | 4983 | * cpu_util returns the amount of capacity of a CPU that is used by CFS |
8bb5b00c | 4984 | * tasks. The unit of the return value must be the one of capacity so we can |
9e91d61d DE |
4985 | * compare the utilization with the capacity of the CPU that is available for |
4986 | * CFS task (ie cpu_capacity). | |
231678b7 DE |
4987 | * |
4988 | * cfs_rq.avg.util_avg is the sum of running time of runnable tasks plus the | |
4989 | * recent utilization of currently non-runnable tasks on a CPU. It represents | |
4990 | * the amount of utilization of a CPU in the range [0..capacity_orig] where | |
4991 | * capacity_orig is the cpu_capacity available at the highest frequency | |
4992 | * (arch_scale_freq_capacity()). | |
4993 | * The utilization of a CPU converges towards a sum equal to or less than the | |
4994 | * current capacity (capacity_curr <= capacity_orig) of the CPU because it is | |
4995 | * the running time on this CPU scaled by capacity_curr. | |
4996 | * | |
4997 | * Nevertheless, cfs_rq.avg.util_avg can be higher than capacity_curr or even | |
4998 | * higher than capacity_orig because of unfortunate rounding in | |
4999 | * cfs.avg.util_avg or just after migrating tasks and new task wakeups until | |
5000 | * the average stabilizes with the new running time. We need to check that the | |
5001 | * utilization stays within the range of [0..capacity_orig] and cap it if | |
5002 | * necessary. Without utilization capping, a group could be seen as overloaded | |
5003 | * (CPU0 utilization at 121% + CPU1 utilization at 80%) whereas CPU1 has 20% of | |
5004 | * available capacity. We allow utilization to overshoot capacity_curr (but not | |
5005 | * capacity_orig) as it useful for predicting the capacity required after task | |
5006 | * migrations (scheduler-driven DVFS). | |
8bb5b00c | 5007 | */ |
9e91d61d | 5008 | static int cpu_util(int cpu) |
8bb5b00c | 5009 | { |
9e91d61d | 5010 | unsigned long util = cpu_rq(cpu)->cfs.avg.util_avg; |
8bb5b00c VG |
5011 | unsigned long capacity = capacity_orig_of(cpu); |
5012 | ||
231678b7 | 5013 | return (util >= capacity) ? capacity : util; |
8bb5b00c | 5014 | } |
a50bde51 | 5015 | |
aaee1203 | 5016 | /* |
de91b9cb MR |
5017 | * select_task_rq_fair: Select target runqueue for the waking task in domains |
5018 | * that have the 'sd_flag' flag set. In practice, this is SD_BALANCE_WAKE, | |
5019 | * SD_BALANCE_FORK, or SD_BALANCE_EXEC. | |
aaee1203 | 5020 | * |
de91b9cb MR |
5021 | * Balances load by selecting the idlest cpu in the idlest group, or under |
5022 | * certain conditions an idle sibling cpu if the domain has SD_WAKE_AFFINE set. | |
aaee1203 | 5023 | * |
de91b9cb | 5024 | * Returns the target cpu number. |
aaee1203 PZ |
5025 | * |
5026 | * preempt must be disabled. | |
5027 | */ | |
0017d735 | 5028 | static int |
ac66f547 | 5029 | select_task_rq_fair(struct task_struct *p, int prev_cpu, int sd_flag, int wake_flags) |
aaee1203 | 5030 | { |
29cd8bae | 5031 | struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL; |
c88d5910 | 5032 | int cpu = smp_processor_id(); |
63b0e9ed | 5033 | int new_cpu = prev_cpu; |
99bd5e2f | 5034 | int want_affine = 0; |
5158f4e4 | 5035 | int sync = wake_flags & WF_SYNC; |
c88d5910 | 5036 | |
a8edd075 | 5037 | if (sd_flag & SD_BALANCE_WAKE) |
63b0e9ed | 5038 | want_affine = !wake_wide(p) && cpumask_test_cpu(cpu, tsk_cpus_allowed(p)); |
aaee1203 | 5039 | |
dce840a0 | 5040 | rcu_read_lock(); |
aaee1203 | 5041 | for_each_domain(cpu, tmp) { |
e4f42888 | 5042 | if (!(tmp->flags & SD_LOAD_BALANCE)) |
63b0e9ed | 5043 | break; |
e4f42888 | 5044 | |
fe3bcfe1 | 5045 | /* |
99bd5e2f SS |
5046 | * If both cpu and prev_cpu are part of this domain, |
5047 | * cpu is a valid SD_WAKE_AFFINE target. | |
fe3bcfe1 | 5048 | */ |
99bd5e2f SS |
5049 | if (want_affine && (tmp->flags & SD_WAKE_AFFINE) && |
5050 | cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) { | |
5051 | affine_sd = tmp; | |
29cd8bae | 5052 | break; |
f03542a7 | 5053 | } |
29cd8bae | 5054 | |
f03542a7 | 5055 | if (tmp->flags & sd_flag) |
29cd8bae | 5056 | sd = tmp; |
63b0e9ed MG |
5057 | else if (!want_affine) |
5058 | break; | |
29cd8bae PZ |
5059 | } |
5060 | ||
63b0e9ed MG |
5061 | if (affine_sd) { |
5062 | sd = NULL; /* Prefer wake_affine over balance flags */ | |
5063 | if (cpu != prev_cpu && wake_affine(affine_sd, p, sync)) | |
5064 | new_cpu = cpu; | |
8b911acd | 5065 | } |
e7693a36 | 5066 | |
63b0e9ed MG |
5067 | if (!sd) { |
5068 | if (sd_flag & SD_BALANCE_WAKE) /* XXX always ? */ | |
5069 | new_cpu = select_idle_sibling(p, new_cpu); | |
5070 | ||
5071 | } else while (sd) { | |
aaee1203 | 5072 | struct sched_group *group; |
c88d5910 | 5073 | int weight; |
098fb9db | 5074 | |
0763a660 | 5075 | if (!(sd->flags & sd_flag)) { |
aaee1203 PZ |
5076 | sd = sd->child; |
5077 | continue; | |
5078 | } | |
098fb9db | 5079 | |
c44f2a02 | 5080 | group = find_idlest_group(sd, p, cpu, sd_flag); |
aaee1203 PZ |
5081 | if (!group) { |
5082 | sd = sd->child; | |
5083 | continue; | |
5084 | } | |
4ae7d5ce | 5085 | |
d7c33c49 | 5086 | new_cpu = find_idlest_cpu(group, p, cpu); |
aaee1203 PZ |
5087 | if (new_cpu == -1 || new_cpu == cpu) { |
5088 | /* Now try balancing at a lower domain level of cpu */ | |
5089 | sd = sd->child; | |
5090 | continue; | |
e7693a36 | 5091 | } |
aaee1203 PZ |
5092 | |
5093 | /* Now try balancing at a lower domain level of new_cpu */ | |
5094 | cpu = new_cpu; | |
669c55e9 | 5095 | weight = sd->span_weight; |
aaee1203 PZ |
5096 | sd = NULL; |
5097 | for_each_domain(cpu, tmp) { | |
669c55e9 | 5098 | if (weight <= tmp->span_weight) |
aaee1203 | 5099 | break; |
0763a660 | 5100 | if (tmp->flags & sd_flag) |
aaee1203 PZ |
5101 | sd = tmp; |
5102 | } | |
5103 | /* while loop will break here if sd == NULL */ | |
e7693a36 | 5104 | } |
dce840a0 | 5105 | rcu_read_unlock(); |
e7693a36 | 5106 | |
c88d5910 | 5107 | return new_cpu; |
e7693a36 | 5108 | } |
0a74bef8 PT |
5109 | |
5110 | /* | |
5111 | * Called immediately before a task is migrated to a new cpu; task_cpu(p) and | |
5112 | * cfs_rq_of(p) references at time of call are still valid and identify the | |
525628c7 | 5113 | * previous cpu. The caller guarantees p->pi_lock or task_rq(p)->lock is held. |
0a74bef8 | 5114 | */ |
5a4fd036 | 5115 | static void migrate_task_rq_fair(struct task_struct *p) |
0a74bef8 | 5116 | { |
aff3e498 | 5117 | /* |
9d89c257 YD |
5118 | * We are supposed to update the task to "current" time, then its up to date |
5119 | * and ready to go to new CPU/cfs_rq. But we have difficulty in getting | |
5120 | * what current time is, so simply throw away the out-of-date time. This | |
5121 | * will result in the wakee task is less decayed, but giving the wakee more | |
5122 | * load sounds not bad. | |
aff3e498 | 5123 | */ |
9d89c257 YD |
5124 | remove_entity_load_avg(&p->se); |
5125 | ||
5126 | /* Tell new CPU we are migrated */ | |
5127 | p->se.avg.last_update_time = 0; | |
3944a927 BS |
5128 | |
5129 | /* We have migrated, no longer consider this task hot */ | |
9d89c257 | 5130 | p->se.exec_start = 0; |
0a74bef8 | 5131 | } |
12695578 YD |
5132 | |
5133 | static void task_dead_fair(struct task_struct *p) | |
5134 | { | |
5135 | remove_entity_load_avg(&p->se); | |
5136 | } | |
e7693a36 GH |
5137 | #endif /* CONFIG_SMP */ |
5138 | ||
e52fb7c0 PZ |
5139 | static unsigned long |
5140 | wakeup_gran(struct sched_entity *curr, struct sched_entity *se) | |
0bbd3336 PZ |
5141 | { |
5142 | unsigned long gran = sysctl_sched_wakeup_granularity; | |
5143 | ||
5144 | /* | |
e52fb7c0 PZ |
5145 | * Since its curr running now, convert the gran from real-time |
5146 | * to virtual-time in his units. | |
13814d42 MG |
5147 | * |
5148 | * By using 'se' instead of 'curr' we penalize light tasks, so | |
5149 | * they get preempted easier. That is, if 'se' < 'curr' then | |
5150 | * the resulting gran will be larger, therefore penalizing the | |
5151 | * lighter, if otoh 'se' > 'curr' then the resulting gran will | |
5152 | * be smaller, again penalizing the lighter task. | |
5153 | * | |
5154 | * This is especially important for buddies when the leftmost | |
5155 | * task is higher priority than the buddy. | |
0bbd3336 | 5156 | */ |
f4ad9bd2 | 5157 | return calc_delta_fair(gran, se); |
0bbd3336 PZ |
5158 | } |
5159 | ||
464b7527 PZ |
5160 | /* |
5161 | * Should 'se' preempt 'curr'. | |
5162 | * | |
5163 | * |s1 | |
5164 | * |s2 | |
5165 | * |s3 | |
5166 | * g | |
5167 | * |<--->|c | |
5168 | * | |
5169 | * w(c, s1) = -1 | |
5170 | * w(c, s2) = 0 | |
5171 | * w(c, s3) = 1 | |
5172 | * | |
5173 | */ | |
5174 | static int | |
5175 | wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se) | |
5176 | { | |
5177 | s64 gran, vdiff = curr->vruntime - se->vruntime; | |
5178 | ||
5179 | if (vdiff <= 0) | |
5180 | return -1; | |
5181 | ||
e52fb7c0 | 5182 | gran = wakeup_gran(curr, se); |
464b7527 PZ |
5183 | if (vdiff > gran) |
5184 | return 1; | |
5185 | ||
5186 | return 0; | |
5187 | } | |
5188 | ||
02479099 PZ |
5189 | static void set_last_buddy(struct sched_entity *se) |
5190 | { | |
69c80f3e VP |
5191 | if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE)) |
5192 | return; | |
5193 | ||
5194 | for_each_sched_entity(se) | |
5195 | cfs_rq_of(se)->last = se; | |
02479099 PZ |
5196 | } |
5197 | ||
5198 | static void set_next_buddy(struct sched_entity *se) | |
5199 | { | |
69c80f3e VP |
5200 | if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE)) |
5201 | return; | |
5202 | ||
5203 | for_each_sched_entity(se) | |
5204 | cfs_rq_of(se)->next = se; | |
02479099 PZ |
5205 | } |
5206 | ||
ac53db59 RR |
5207 | static void set_skip_buddy(struct sched_entity *se) |
5208 | { | |
69c80f3e VP |
5209 | for_each_sched_entity(se) |
5210 | cfs_rq_of(se)->skip = se; | |
ac53db59 RR |
5211 | } |
5212 | ||
bf0f6f24 IM |
5213 | /* |
5214 | * Preempt the current task with a newly woken task if needed: | |
5215 | */ | |
5a9b86f6 | 5216 | static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags) |
bf0f6f24 IM |
5217 | { |
5218 | struct task_struct *curr = rq->curr; | |
8651a86c | 5219 | struct sched_entity *se = &curr->se, *pse = &p->se; |
03e89e45 | 5220 | struct cfs_rq *cfs_rq = task_cfs_rq(curr); |
f685ceac | 5221 | int scale = cfs_rq->nr_running >= sched_nr_latency; |
2f36825b | 5222 | int next_buddy_marked = 0; |
bf0f6f24 | 5223 | |
4ae7d5ce IM |
5224 | if (unlikely(se == pse)) |
5225 | return; | |
5226 | ||
5238cdd3 | 5227 | /* |
163122b7 | 5228 | * This is possible from callers such as attach_tasks(), in which we |
5238cdd3 PT |
5229 | * unconditionally check_prempt_curr() after an enqueue (which may have |
5230 | * lead to a throttle). This both saves work and prevents false | |
5231 | * next-buddy nomination below. | |
5232 | */ | |
5233 | if (unlikely(throttled_hierarchy(cfs_rq_of(pse)))) | |
5234 | return; | |
5235 | ||
2f36825b | 5236 | if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) { |
3cb63d52 | 5237 | set_next_buddy(pse); |
2f36825b VP |
5238 | next_buddy_marked = 1; |
5239 | } | |
57fdc26d | 5240 | |
aec0a514 BR |
5241 | /* |
5242 | * We can come here with TIF_NEED_RESCHED already set from new task | |
5243 | * wake up path. | |
5238cdd3 PT |
5244 | * |
5245 | * Note: this also catches the edge-case of curr being in a throttled | |
5246 | * group (e.g. via set_curr_task), since update_curr() (in the | |
5247 | * enqueue of curr) will have resulted in resched being set. This | |
5248 | * prevents us from potentially nominating it as a false LAST_BUDDY | |
5249 | * below. | |
aec0a514 BR |
5250 | */ |
5251 | if (test_tsk_need_resched(curr)) | |
5252 | return; | |
5253 | ||
a2f5c9ab DH |
5254 | /* Idle tasks are by definition preempted by non-idle tasks. */ |
5255 | if (unlikely(curr->policy == SCHED_IDLE) && | |
5256 | likely(p->policy != SCHED_IDLE)) | |
5257 | goto preempt; | |
5258 | ||
91c234b4 | 5259 | /* |
a2f5c9ab DH |
5260 | * Batch and idle tasks do not preempt non-idle tasks (their preemption |
5261 | * is driven by the tick): | |
91c234b4 | 5262 | */ |
8ed92e51 | 5263 | if (unlikely(p->policy != SCHED_NORMAL) || !sched_feat(WAKEUP_PREEMPTION)) |
91c234b4 | 5264 | return; |
bf0f6f24 | 5265 | |
464b7527 | 5266 | find_matching_se(&se, &pse); |
9bbd7374 | 5267 | update_curr(cfs_rq_of(se)); |
002f128b | 5268 | BUG_ON(!pse); |
2f36825b VP |
5269 | if (wakeup_preempt_entity(se, pse) == 1) { |
5270 | /* | |
5271 | * Bias pick_next to pick the sched entity that is | |
5272 | * triggering this preemption. | |
5273 | */ | |
5274 | if (!next_buddy_marked) | |
5275 | set_next_buddy(pse); | |
3a7e73a2 | 5276 | goto preempt; |
2f36825b | 5277 | } |
464b7527 | 5278 | |
3a7e73a2 | 5279 | return; |
a65ac745 | 5280 | |
3a7e73a2 | 5281 | preempt: |
8875125e | 5282 | resched_curr(rq); |
3a7e73a2 PZ |
5283 | /* |
5284 | * Only set the backward buddy when the current task is still | |
5285 | * on the rq. This can happen when a wakeup gets interleaved | |
5286 | * with schedule on the ->pre_schedule() or idle_balance() | |
5287 | * point, either of which can * drop the rq lock. | |
5288 | * | |
5289 | * Also, during early boot the idle thread is in the fair class, | |
5290 | * for obvious reasons its a bad idea to schedule back to it. | |
5291 | */ | |
5292 | if (unlikely(!se->on_rq || curr == rq->idle)) | |
5293 | return; | |
5294 | ||
5295 | if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se)) | |
5296 | set_last_buddy(se); | |
bf0f6f24 IM |
5297 | } |
5298 | ||
606dba2e PZ |
5299 | static struct task_struct * |
5300 | pick_next_task_fair(struct rq *rq, struct task_struct *prev) | |
bf0f6f24 IM |
5301 | { |
5302 | struct cfs_rq *cfs_rq = &rq->cfs; | |
5303 | struct sched_entity *se; | |
678d5718 | 5304 | struct task_struct *p; |
37e117c0 | 5305 | int new_tasks; |
678d5718 | 5306 | |
6e83125c | 5307 | again: |
678d5718 PZ |
5308 | #ifdef CONFIG_FAIR_GROUP_SCHED |
5309 | if (!cfs_rq->nr_running) | |
38033c37 | 5310 | goto idle; |
678d5718 | 5311 | |
3f1d2a31 | 5312 | if (prev->sched_class != &fair_sched_class) |
678d5718 PZ |
5313 | goto simple; |
5314 | ||
5315 | /* | |
5316 | * Because of the set_next_buddy() in dequeue_task_fair() it is rather | |
5317 | * likely that a next task is from the same cgroup as the current. | |
5318 | * | |
5319 | * Therefore attempt to avoid putting and setting the entire cgroup | |
5320 | * hierarchy, only change the part that actually changes. | |
5321 | */ | |
5322 | ||
5323 | do { | |
5324 | struct sched_entity *curr = cfs_rq->curr; | |
5325 | ||
5326 | /* | |
5327 | * Since we got here without doing put_prev_entity() we also | |
5328 | * have to consider cfs_rq->curr. If it is still a runnable | |
5329 | * entity, update_curr() will update its vruntime, otherwise | |
5330 | * forget we've ever seen it. | |
5331 | */ | |
54d27365 BS |
5332 | if (curr) { |
5333 | if (curr->on_rq) | |
5334 | update_curr(cfs_rq); | |
5335 | else | |
5336 | curr = NULL; | |
678d5718 | 5337 | |
54d27365 BS |
5338 | /* |
5339 | * This call to check_cfs_rq_runtime() will do the | |
5340 | * throttle and dequeue its entity in the parent(s). | |
5341 | * Therefore the 'simple' nr_running test will indeed | |
5342 | * be correct. | |
5343 | */ | |
5344 | if (unlikely(check_cfs_rq_runtime(cfs_rq))) | |
5345 | goto simple; | |
5346 | } | |
678d5718 PZ |
5347 | |
5348 | se = pick_next_entity(cfs_rq, curr); | |
5349 | cfs_rq = group_cfs_rq(se); | |
5350 | } while (cfs_rq); | |
5351 | ||
5352 | p = task_of(se); | |
5353 | ||
5354 | /* | |
5355 | * Since we haven't yet done put_prev_entity and if the selected task | |
5356 | * is a different task than we started out with, try and touch the | |
5357 | * least amount of cfs_rqs. | |
5358 | */ | |
5359 | if (prev != p) { | |
5360 | struct sched_entity *pse = &prev->se; | |
5361 | ||
5362 | while (!(cfs_rq = is_same_group(se, pse))) { | |
5363 | int se_depth = se->depth; | |
5364 | int pse_depth = pse->depth; | |
5365 | ||
5366 | if (se_depth <= pse_depth) { | |
5367 | put_prev_entity(cfs_rq_of(pse), pse); | |
5368 | pse = parent_entity(pse); | |
5369 | } | |
5370 | if (se_depth >= pse_depth) { | |
5371 | set_next_entity(cfs_rq_of(se), se); | |
5372 | se = parent_entity(se); | |
5373 | } | |
5374 | } | |
5375 | ||
5376 | put_prev_entity(cfs_rq, pse); | |
5377 | set_next_entity(cfs_rq, se); | |
5378 | } | |
5379 | ||
5380 | if (hrtick_enabled(rq)) | |
5381 | hrtick_start_fair(rq, p); | |
5382 | ||
5383 | return p; | |
5384 | simple: | |
5385 | cfs_rq = &rq->cfs; | |
5386 | #endif | |
bf0f6f24 | 5387 | |
36ace27e | 5388 | if (!cfs_rq->nr_running) |
38033c37 | 5389 | goto idle; |
bf0f6f24 | 5390 | |
3f1d2a31 | 5391 | put_prev_task(rq, prev); |
606dba2e | 5392 | |
bf0f6f24 | 5393 | do { |
678d5718 | 5394 | se = pick_next_entity(cfs_rq, NULL); |
f4b6755f | 5395 | set_next_entity(cfs_rq, se); |
bf0f6f24 IM |
5396 | cfs_rq = group_cfs_rq(se); |
5397 | } while (cfs_rq); | |
5398 | ||
8f4d37ec | 5399 | p = task_of(se); |
678d5718 | 5400 | |
b39e66ea MG |
5401 | if (hrtick_enabled(rq)) |
5402 | hrtick_start_fair(rq, p); | |
8f4d37ec PZ |
5403 | |
5404 | return p; | |
38033c37 PZ |
5405 | |
5406 | idle: | |
cbce1a68 PZ |
5407 | /* |
5408 | * This is OK, because current is on_cpu, which avoids it being picked | |
5409 | * for load-balance and preemption/IRQs are still disabled avoiding | |
5410 | * further scheduler activity on it and we're being very careful to | |
5411 | * re-start the picking loop. | |
5412 | */ | |
5413 | lockdep_unpin_lock(&rq->lock); | |
e4aa358b | 5414 | new_tasks = idle_balance(rq); |
cbce1a68 | 5415 | lockdep_pin_lock(&rq->lock); |
37e117c0 PZ |
5416 | /* |
5417 | * Because idle_balance() releases (and re-acquires) rq->lock, it is | |
5418 | * possible for any higher priority task to appear. In that case we | |
5419 | * must re-start the pick_next_entity() loop. | |
5420 | */ | |
e4aa358b | 5421 | if (new_tasks < 0) |
37e117c0 PZ |
5422 | return RETRY_TASK; |
5423 | ||
e4aa358b | 5424 | if (new_tasks > 0) |
38033c37 | 5425 | goto again; |
38033c37 PZ |
5426 | |
5427 | return NULL; | |
bf0f6f24 IM |
5428 | } |
5429 | ||
5430 | /* | |
5431 | * Account for a descheduled task: | |
5432 | */ | |
31ee529c | 5433 | static void put_prev_task_fair(struct rq *rq, struct task_struct *prev) |
bf0f6f24 IM |
5434 | { |
5435 | struct sched_entity *se = &prev->se; | |
5436 | struct cfs_rq *cfs_rq; | |
5437 | ||
5438 | for_each_sched_entity(se) { | |
5439 | cfs_rq = cfs_rq_of(se); | |
ab6cde26 | 5440 | put_prev_entity(cfs_rq, se); |
bf0f6f24 IM |
5441 | } |
5442 | } | |
5443 | ||
ac53db59 RR |
5444 | /* |
5445 | * sched_yield() is very simple | |
5446 | * | |
5447 | * The magic of dealing with the ->skip buddy is in pick_next_entity. | |
5448 | */ | |
5449 | static void yield_task_fair(struct rq *rq) | |
5450 | { | |
5451 | struct task_struct *curr = rq->curr; | |
5452 | struct cfs_rq *cfs_rq = task_cfs_rq(curr); | |
5453 | struct sched_entity *se = &curr->se; | |
5454 | ||
5455 | /* | |
5456 | * Are we the only task in the tree? | |
5457 | */ | |
5458 | if (unlikely(rq->nr_running == 1)) | |
5459 | return; | |
5460 | ||
5461 | clear_buddies(cfs_rq, se); | |
5462 | ||
5463 | if (curr->policy != SCHED_BATCH) { | |
5464 | update_rq_clock(rq); | |
5465 | /* | |
5466 | * Update run-time statistics of the 'current'. | |
5467 | */ | |
5468 | update_curr(cfs_rq); | |
916671c0 MG |
5469 | /* |
5470 | * Tell update_rq_clock() that we've just updated, | |
5471 | * so we don't do microscopic update in schedule() | |
5472 | * and double the fastpath cost. | |
5473 | */ | |
9edfbfed | 5474 | rq_clock_skip_update(rq, true); |
ac53db59 RR |
5475 | } |
5476 | ||
5477 | set_skip_buddy(se); | |
5478 | } | |
5479 | ||
d95f4122 MG |
5480 | static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt) |
5481 | { | |
5482 | struct sched_entity *se = &p->se; | |
5483 | ||
5238cdd3 PT |
5484 | /* throttled hierarchies are not runnable */ |
5485 | if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se))) | |
d95f4122 MG |
5486 | return false; |
5487 | ||
5488 | /* Tell the scheduler that we'd really like pse to run next. */ | |
5489 | set_next_buddy(se); | |
5490 | ||
d95f4122 MG |
5491 | yield_task_fair(rq); |
5492 | ||
5493 | return true; | |
5494 | } | |
5495 | ||
681f3e68 | 5496 | #ifdef CONFIG_SMP |
bf0f6f24 | 5497 | /************************************************** |
e9c84cb8 PZ |
5498 | * Fair scheduling class load-balancing methods. |
5499 | * | |
5500 | * BASICS | |
5501 | * | |
5502 | * The purpose of load-balancing is to achieve the same basic fairness the | |
5503 | * per-cpu scheduler provides, namely provide a proportional amount of compute | |
5504 | * time to each task. This is expressed in the following equation: | |
5505 | * | |
5506 | * W_i,n/P_i == W_j,n/P_j for all i,j (1) | |
5507 | * | |
5508 | * Where W_i,n is the n-th weight average for cpu i. The instantaneous weight | |
5509 | * W_i,0 is defined as: | |
5510 | * | |
5511 | * W_i,0 = \Sum_j w_i,j (2) | |
5512 | * | |
5513 | * Where w_i,j is the weight of the j-th runnable task on cpu i. This weight | |
5514 | * is derived from the nice value as per prio_to_weight[]. | |
5515 | * | |
5516 | * The weight average is an exponential decay average of the instantaneous | |
5517 | * weight: | |
5518 | * | |
5519 | * W'_i,n = (2^n - 1) / 2^n * W_i,n + 1 / 2^n * W_i,0 (3) | |
5520 | * | |
ced549fa | 5521 | * C_i is the compute capacity of cpu i, typically it is the |
e9c84cb8 PZ |
5522 | * fraction of 'recent' time available for SCHED_OTHER task execution. But it |
5523 | * can also include other factors [XXX]. | |
5524 | * | |
5525 | * To achieve this balance we define a measure of imbalance which follows | |
5526 | * directly from (1): | |
5527 | * | |
ced549fa | 5528 | * imb_i,j = max{ avg(W/C), W_i/C_i } - min{ avg(W/C), W_j/C_j } (4) |
e9c84cb8 PZ |
5529 | * |
5530 | * We them move tasks around to minimize the imbalance. In the continuous | |
5531 | * function space it is obvious this converges, in the discrete case we get | |
5532 | * a few fun cases generally called infeasible weight scenarios. | |
5533 | * | |
5534 | * [XXX expand on: | |
5535 | * - infeasible weights; | |
5536 | * - local vs global optima in the discrete case. ] | |
5537 | * | |
5538 | * | |
5539 | * SCHED DOMAINS | |
5540 | * | |
5541 | * In order to solve the imbalance equation (4), and avoid the obvious O(n^2) | |
5542 | * for all i,j solution, we create a tree of cpus that follows the hardware | |
5543 | * topology where each level pairs two lower groups (or better). This results | |
5544 | * in O(log n) layers. Furthermore we reduce the number of cpus going up the | |
5545 | * tree to only the first of the previous level and we decrease the frequency | |
5546 | * of load-balance at each level inv. proportional to the number of cpus in | |
5547 | * the groups. | |
5548 | * | |
5549 | * This yields: | |
5550 | * | |
5551 | * log_2 n 1 n | |
5552 | * \Sum { --- * --- * 2^i } = O(n) (5) | |
5553 | * i = 0 2^i 2^i | |
5554 | * `- size of each group | |
5555 | * | | `- number of cpus doing load-balance | |
5556 | * | `- freq | |
5557 | * `- sum over all levels | |
5558 | * | |
5559 | * Coupled with a limit on how many tasks we can migrate every balance pass, | |
5560 | * this makes (5) the runtime complexity of the balancer. | |
5561 | * | |
5562 | * An important property here is that each CPU is still (indirectly) connected | |
5563 | * to every other cpu in at most O(log n) steps: | |
5564 | * | |
5565 | * The adjacency matrix of the resulting graph is given by: | |
5566 | * | |
5567 | * log_2 n | |
5568 | * A_i,j = \Union (i % 2^k == 0) && i / 2^(k+1) == j / 2^(k+1) (6) | |
5569 | * k = 0 | |
5570 | * | |
5571 | * And you'll find that: | |
5572 | * | |
5573 | * A^(log_2 n)_i,j != 0 for all i,j (7) | |
5574 | * | |
5575 | * Showing there's indeed a path between every cpu in at most O(log n) steps. | |
5576 | * The task movement gives a factor of O(m), giving a convergence complexity | |
5577 | * of: | |
5578 | * | |
5579 | * O(nm log n), n := nr_cpus, m := nr_tasks (8) | |
5580 | * | |
5581 | * | |
5582 | * WORK CONSERVING | |
5583 | * | |
5584 | * In order to avoid CPUs going idle while there's still work to do, new idle | |
5585 | * balancing is more aggressive and has the newly idle cpu iterate up the domain | |
5586 | * tree itself instead of relying on other CPUs to bring it work. | |
5587 | * | |
5588 | * This adds some complexity to both (5) and (8) but it reduces the total idle | |
5589 | * time. | |
5590 | * | |
5591 | * [XXX more?] | |
5592 | * | |
5593 | * | |
5594 | * CGROUPS | |
5595 | * | |
5596 | * Cgroups make a horror show out of (2), instead of a simple sum we get: | |
5597 | * | |
5598 | * s_k,i | |
5599 | * W_i,0 = \Sum_j \Prod_k w_k * ----- (9) | |
5600 | * S_k | |
5601 | * | |
5602 | * Where | |
5603 | * | |
5604 | * s_k,i = \Sum_j w_i,j,k and S_k = \Sum_i s_k,i (10) | |
5605 | * | |
5606 | * w_i,j,k is the weight of the j-th runnable task in the k-th cgroup on cpu i. | |
5607 | * | |
5608 | * The big problem is S_k, its a global sum needed to compute a local (W_i) | |
5609 | * property. | |
5610 | * | |
5611 | * [XXX write more on how we solve this.. _after_ merging pjt's patches that | |
5612 | * rewrite all of this once again.] | |
5613 | */ | |
bf0f6f24 | 5614 | |
ed387b78 HS |
5615 | static unsigned long __read_mostly max_load_balance_interval = HZ/10; |
5616 | ||
0ec8aa00 PZ |
5617 | enum fbq_type { regular, remote, all }; |
5618 | ||
ddcdf6e7 | 5619 | #define LBF_ALL_PINNED 0x01 |
367456c7 | 5620 | #define LBF_NEED_BREAK 0x02 |
6263322c PZ |
5621 | #define LBF_DST_PINNED 0x04 |
5622 | #define LBF_SOME_PINNED 0x08 | |
ddcdf6e7 PZ |
5623 | |
5624 | struct lb_env { | |
5625 | struct sched_domain *sd; | |
5626 | ||
ddcdf6e7 | 5627 | struct rq *src_rq; |
85c1e7da | 5628 | int src_cpu; |
ddcdf6e7 PZ |
5629 | |
5630 | int dst_cpu; | |
5631 | struct rq *dst_rq; | |
5632 | ||
88b8dac0 SV |
5633 | struct cpumask *dst_grpmask; |
5634 | int new_dst_cpu; | |
ddcdf6e7 | 5635 | enum cpu_idle_type idle; |
bd939f45 | 5636 | long imbalance; |
b9403130 MW |
5637 | /* The set of CPUs under consideration for load-balancing */ |
5638 | struct cpumask *cpus; | |
5639 | ||
ddcdf6e7 | 5640 | unsigned int flags; |
367456c7 PZ |
5641 | |
5642 | unsigned int loop; | |
5643 | unsigned int loop_break; | |
5644 | unsigned int loop_max; | |
0ec8aa00 PZ |
5645 | |
5646 | enum fbq_type fbq_type; | |
163122b7 | 5647 | struct list_head tasks; |
ddcdf6e7 PZ |
5648 | }; |
5649 | ||
029632fb PZ |
5650 | /* |
5651 | * Is this task likely cache-hot: | |
5652 | */ | |
5d5e2b1b | 5653 | static int task_hot(struct task_struct *p, struct lb_env *env) |
029632fb PZ |
5654 | { |
5655 | s64 delta; | |
5656 | ||
e5673f28 KT |
5657 | lockdep_assert_held(&env->src_rq->lock); |
5658 | ||
029632fb PZ |
5659 | if (p->sched_class != &fair_sched_class) |
5660 | return 0; | |
5661 | ||
5662 | if (unlikely(p->policy == SCHED_IDLE)) | |
5663 | return 0; | |
5664 | ||
5665 | /* | |
5666 | * Buddy candidates are cache hot: | |
5667 | */ | |
5d5e2b1b | 5668 | if (sched_feat(CACHE_HOT_BUDDY) && env->dst_rq->nr_running && |
029632fb PZ |
5669 | (&p->se == cfs_rq_of(&p->se)->next || |
5670 | &p->se == cfs_rq_of(&p->se)->last)) | |
5671 | return 1; | |
5672 | ||
5673 | if (sysctl_sched_migration_cost == -1) | |
5674 | return 1; | |
5675 | if (sysctl_sched_migration_cost == 0) | |
5676 | return 0; | |
5677 | ||
5d5e2b1b | 5678 | delta = rq_clock_task(env->src_rq) - p->se.exec_start; |
029632fb PZ |
5679 | |
5680 | return delta < (s64)sysctl_sched_migration_cost; | |
5681 | } | |
5682 | ||
3a7053b3 | 5683 | #ifdef CONFIG_NUMA_BALANCING |
c1ceac62 | 5684 | /* |
2a1ed24c SD |
5685 | * Returns 1, if task migration degrades locality |
5686 | * Returns 0, if task migration improves locality i.e migration preferred. | |
5687 | * Returns -1, if task migration is not affected by locality. | |
c1ceac62 | 5688 | */ |
2a1ed24c | 5689 | static int migrate_degrades_locality(struct task_struct *p, struct lb_env *env) |
3a7053b3 | 5690 | { |
b1ad065e | 5691 | struct numa_group *numa_group = rcu_dereference(p->numa_group); |
c1ceac62 | 5692 | unsigned long src_faults, dst_faults; |
3a7053b3 MG |
5693 | int src_nid, dst_nid; |
5694 | ||
2a595721 | 5695 | if (!static_branch_likely(&sched_numa_balancing)) |
2a1ed24c SD |
5696 | return -1; |
5697 | ||
c3b9bc5b | 5698 | if (!p->numa_faults || !(env->sd->flags & SD_NUMA)) |
2a1ed24c | 5699 | return -1; |
7a0f3083 MG |
5700 | |
5701 | src_nid = cpu_to_node(env->src_cpu); | |
5702 | dst_nid = cpu_to_node(env->dst_cpu); | |
5703 | ||
83e1d2cd | 5704 | if (src_nid == dst_nid) |
2a1ed24c | 5705 | return -1; |
7a0f3083 | 5706 | |
2a1ed24c SD |
5707 | /* Migrating away from the preferred node is always bad. */ |
5708 | if (src_nid == p->numa_preferred_nid) { | |
5709 | if (env->src_rq->nr_running > env->src_rq->nr_preferred_running) | |
5710 | return 1; | |
5711 | else | |
5712 | return -1; | |
5713 | } | |
b1ad065e | 5714 | |
c1ceac62 RR |
5715 | /* Encourage migration to the preferred node. */ |
5716 | if (dst_nid == p->numa_preferred_nid) | |
2a1ed24c | 5717 | return 0; |
b1ad065e | 5718 | |
c1ceac62 RR |
5719 | if (numa_group) { |
5720 | src_faults = group_faults(p, src_nid); | |
5721 | dst_faults = group_faults(p, dst_nid); | |
5722 | } else { | |
5723 | src_faults = task_faults(p, src_nid); | |
5724 | dst_faults = task_faults(p, dst_nid); | |
b1ad065e RR |
5725 | } |
5726 | ||
c1ceac62 | 5727 | return dst_faults < src_faults; |
7a0f3083 MG |
5728 | } |
5729 | ||
3a7053b3 | 5730 | #else |
2a1ed24c | 5731 | static inline int migrate_degrades_locality(struct task_struct *p, |
3a7053b3 MG |
5732 | struct lb_env *env) |
5733 | { | |
2a1ed24c | 5734 | return -1; |
7a0f3083 | 5735 | } |
3a7053b3 MG |
5736 | #endif |
5737 | ||
1e3c88bd PZ |
5738 | /* |
5739 | * can_migrate_task - may task p from runqueue rq be migrated to this_cpu? | |
5740 | */ | |
5741 | static | |
8e45cb54 | 5742 | int can_migrate_task(struct task_struct *p, struct lb_env *env) |
1e3c88bd | 5743 | { |
2a1ed24c | 5744 | int tsk_cache_hot; |
e5673f28 KT |
5745 | |
5746 | lockdep_assert_held(&env->src_rq->lock); | |
5747 | ||
1e3c88bd PZ |
5748 | /* |
5749 | * We do not migrate tasks that are: | |
d3198084 | 5750 | * 1) throttled_lb_pair, or |
1e3c88bd | 5751 | * 2) cannot be migrated to this CPU due to cpus_allowed, or |
d3198084 JK |
5752 | * 3) running (obviously), or |
5753 | * 4) are cache-hot on their current CPU. | |
1e3c88bd | 5754 | */ |
d3198084 JK |
5755 | if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu)) |
5756 | return 0; | |
5757 | ||
ddcdf6e7 | 5758 | if (!cpumask_test_cpu(env->dst_cpu, tsk_cpus_allowed(p))) { |
e02e60c1 | 5759 | int cpu; |
88b8dac0 | 5760 | |
41acab88 | 5761 | schedstat_inc(p, se.statistics.nr_failed_migrations_affine); |
88b8dac0 | 5762 | |
6263322c PZ |
5763 | env->flags |= LBF_SOME_PINNED; |
5764 | ||
88b8dac0 SV |
5765 | /* |
5766 | * Remember if this task can be migrated to any other cpu in | |
5767 | * our sched_group. We may want to revisit it if we couldn't | |
5768 | * meet load balance goals by pulling other tasks on src_cpu. | |
5769 | * | |
5770 | * Also avoid computing new_dst_cpu if we have already computed | |
5771 | * one in current iteration. | |
5772 | */ | |
6263322c | 5773 | if (!env->dst_grpmask || (env->flags & LBF_DST_PINNED)) |
88b8dac0 SV |
5774 | return 0; |
5775 | ||
e02e60c1 JK |
5776 | /* Prevent to re-select dst_cpu via env's cpus */ |
5777 | for_each_cpu_and(cpu, env->dst_grpmask, env->cpus) { | |
5778 | if (cpumask_test_cpu(cpu, tsk_cpus_allowed(p))) { | |
6263322c | 5779 | env->flags |= LBF_DST_PINNED; |
e02e60c1 JK |
5780 | env->new_dst_cpu = cpu; |
5781 | break; | |
5782 | } | |
88b8dac0 | 5783 | } |
e02e60c1 | 5784 | |
1e3c88bd PZ |
5785 | return 0; |
5786 | } | |
88b8dac0 SV |
5787 | |
5788 | /* Record that we found atleast one task that could run on dst_cpu */ | |
8e45cb54 | 5789 | env->flags &= ~LBF_ALL_PINNED; |
1e3c88bd | 5790 | |
ddcdf6e7 | 5791 | if (task_running(env->src_rq, p)) { |
41acab88 | 5792 | schedstat_inc(p, se.statistics.nr_failed_migrations_running); |
1e3c88bd PZ |
5793 | return 0; |
5794 | } | |
5795 | ||
5796 | /* | |
5797 | * Aggressive migration if: | |
3a7053b3 MG |
5798 | * 1) destination numa is preferred |
5799 | * 2) task is cache cold, or | |
5800 | * 3) too many balance attempts have failed. | |
1e3c88bd | 5801 | */ |
2a1ed24c SD |
5802 | tsk_cache_hot = migrate_degrades_locality(p, env); |
5803 | if (tsk_cache_hot == -1) | |
5804 | tsk_cache_hot = task_hot(p, env); | |
3a7053b3 | 5805 | |
2a1ed24c | 5806 | if (tsk_cache_hot <= 0 || |
7a96c231 | 5807 | env->sd->nr_balance_failed > env->sd->cache_nice_tries) { |
2a1ed24c | 5808 | if (tsk_cache_hot == 1) { |
3a7053b3 MG |
5809 | schedstat_inc(env->sd, lb_hot_gained[env->idle]); |
5810 | schedstat_inc(p, se.statistics.nr_forced_migrations); | |
5811 | } | |
1e3c88bd PZ |
5812 | return 1; |
5813 | } | |
5814 | ||
4e2dcb73 ZH |
5815 | schedstat_inc(p, se.statistics.nr_failed_migrations_hot); |
5816 | return 0; | |
1e3c88bd PZ |
5817 | } |
5818 | ||
897c395f | 5819 | /* |
163122b7 KT |
5820 | * detach_task() -- detach the task for the migration specified in env |
5821 | */ | |
5822 | static void detach_task(struct task_struct *p, struct lb_env *env) | |
5823 | { | |
5824 | lockdep_assert_held(&env->src_rq->lock); | |
5825 | ||
163122b7 | 5826 | p->on_rq = TASK_ON_RQ_MIGRATING; |
3ea94de1 | 5827 | deactivate_task(env->src_rq, p, 0); |
163122b7 KT |
5828 | set_task_cpu(p, env->dst_cpu); |
5829 | } | |
5830 | ||
897c395f | 5831 | /* |
e5673f28 | 5832 | * detach_one_task() -- tries to dequeue exactly one task from env->src_rq, as |
897c395f | 5833 | * part of active balancing operations within "domain". |
897c395f | 5834 | * |
e5673f28 | 5835 | * Returns a task if successful and NULL otherwise. |
897c395f | 5836 | */ |
e5673f28 | 5837 | static struct task_struct *detach_one_task(struct lb_env *env) |
897c395f PZ |
5838 | { |
5839 | struct task_struct *p, *n; | |
897c395f | 5840 | |
e5673f28 KT |
5841 | lockdep_assert_held(&env->src_rq->lock); |
5842 | ||
367456c7 | 5843 | list_for_each_entry_safe(p, n, &env->src_rq->cfs_tasks, se.group_node) { |
367456c7 PZ |
5844 | if (!can_migrate_task(p, env)) |
5845 | continue; | |
897c395f | 5846 | |
163122b7 | 5847 | detach_task(p, env); |
e5673f28 | 5848 | |
367456c7 | 5849 | /* |
e5673f28 | 5850 | * Right now, this is only the second place where |
163122b7 | 5851 | * lb_gained[env->idle] is updated (other is detach_tasks) |
e5673f28 | 5852 | * so we can safely collect stats here rather than |
163122b7 | 5853 | * inside detach_tasks(). |
367456c7 PZ |
5854 | */ |
5855 | schedstat_inc(env->sd, lb_gained[env->idle]); | |
e5673f28 | 5856 | return p; |
897c395f | 5857 | } |
e5673f28 | 5858 | return NULL; |
897c395f PZ |
5859 | } |
5860 | ||
eb95308e PZ |
5861 | static const unsigned int sched_nr_migrate_break = 32; |
5862 | ||
5d6523eb | 5863 | /* |
163122b7 KT |
5864 | * detach_tasks() -- tries to detach up to imbalance weighted load from |
5865 | * busiest_rq, as part of a balancing operation within domain "sd". | |
5d6523eb | 5866 | * |
163122b7 | 5867 | * Returns number of detached tasks if successful and 0 otherwise. |
5d6523eb | 5868 | */ |
163122b7 | 5869 | static int detach_tasks(struct lb_env *env) |
1e3c88bd | 5870 | { |
5d6523eb PZ |
5871 | struct list_head *tasks = &env->src_rq->cfs_tasks; |
5872 | struct task_struct *p; | |
367456c7 | 5873 | unsigned long load; |
163122b7 KT |
5874 | int detached = 0; |
5875 | ||
5876 | lockdep_assert_held(&env->src_rq->lock); | |
1e3c88bd | 5877 | |
bd939f45 | 5878 | if (env->imbalance <= 0) |
5d6523eb | 5879 | return 0; |
1e3c88bd | 5880 | |
5d6523eb | 5881 | while (!list_empty(tasks)) { |
985d3a4c YD |
5882 | /* |
5883 | * We don't want to steal all, otherwise we may be treated likewise, | |
5884 | * which could at worst lead to a livelock crash. | |
5885 | */ | |
5886 | if (env->idle != CPU_NOT_IDLE && env->src_rq->nr_running <= 1) | |
5887 | break; | |
5888 | ||
5d6523eb | 5889 | p = list_first_entry(tasks, struct task_struct, se.group_node); |
1e3c88bd | 5890 | |
367456c7 PZ |
5891 | env->loop++; |
5892 | /* We've more or less seen every task there is, call it quits */ | |
5d6523eb | 5893 | if (env->loop > env->loop_max) |
367456c7 | 5894 | break; |
5d6523eb PZ |
5895 | |
5896 | /* take a breather every nr_migrate tasks */ | |
367456c7 | 5897 | if (env->loop > env->loop_break) { |
eb95308e | 5898 | env->loop_break += sched_nr_migrate_break; |
8e45cb54 | 5899 | env->flags |= LBF_NEED_BREAK; |
ee00e66f | 5900 | break; |
a195f004 | 5901 | } |
1e3c88bd | 5902 | |
d3198084 | 5903 | if (!can_migrate_task(p, env)) |
367456c7 PZ |
5904 | goto next; |
5905 | ||
5906 | load = task_h_load(p); | |
5d6523eb | 5907 | |
eb95308e | 5908 | if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed) |
367456c7 PZ |
5909 | goto next; |
5910 | ||
bd939f45 | 5911 | if ((load / 2) > env->imbalance) |
367456c7 | 5912 | goto next; |
1e3c88bd | 5913 | |
163122b7 KT |
5914 | detach_task(p, env); |
5915 | list_add(&p->se.group_node, &env->tasks); | |
5916 | ||
5917 | detached++; | |
bd939f45 | 5918 | env->imbalance -= load; |
1e3c88bd PZ |
5919 | |
5920 | #ifdef CONFIG_PREEMPT | |
ee00e66f PZ |
5921 | /* |
5922 | * NEWIDLE balancing is a source of latency, so preemptible | |
163122b7 | 5923 | * kernels will stop after the first task is detached to minimize |
ee00e66f PZ |
5924 | * the critical section. |
5925 | */ | |
5d6523eb | 5926 | if (env->idle == CPU_NEWLY_IDLE) |
ee00e66f | 5927 | break; |
1e3c88bd PZ |
5928 | #endif |
5929 | ||
ee00e66f PZ |
5930 | /* |
5931 | * We only want to steal up to the prescribed amount of | |
5932 | * weighted load. | |
5933 | */ | |
bd939f45 | 5934 | if (env->imbalance <= 0) |
ee00e66f | 5935 | break; |
367456c7 PZ |
5936 | |
5937 | continue; | |
5938 | next: | |
5d6523eb | 5939 | list_move_tail(&p->se.group_node, tasks); |
1e3c88bd | 5940 | } |
5d6523eb | 5941 | |
1e3c88bd | 5942 | /* |
163122b7 KT |
5943 | * Right now, this is one of only two places we collect this stat |
5944 | * so we can safely collect detach_one_task() stats here rather | |
5945 | * than inside detach_one_task(). | |
1e3c88bd | 5946 | */ |
163122b7 | 5947 | schedstat_add(env->sd, lb_gained[env->idle], detached); |
1e3c88bd | 5948 | |
163122b7 KT |
5949 | return detached; |
5950 | } | |
5951 | ||
5952 | /* | |
5953 | * attach_task() -- attach the task detached by detach_task() to its new rq. | |
5954 | */ | |
5955 | static void attach_task(struct rq *rq, struct task_struct *p) | |
5956 | { | |
5957 | lockdep_assert_held(&rq->lock); | |
5958 | ||
5959 | BUG_ON(task_rq(p) != rq); | |
163122b7 | 5960 | activate_task(rq, p, 0); |
3ea94de1 | 5961 | p->on_rq = TASK_ON_RQ_QUEUED; |
163122b7 KT |
5962 | check_preempt_curr(rq, p, 0); |
5963 | } | |
5964 | ||
5965 | /* | |
5966 | * attach_one_task() -- attaches the task returned from detach_one_task() to | |
5967 | * its new rq. | |
5968 | */ | |
5969 | static void attach_one_task(struct rq *rq, struct task_struct *p) | |
5970 | { | |
5971 | raw_spin_lock(&rq->lock); | |
5972 | attach_task(rq, p); | |
5973 | raw_spin_unlock(&rq->lock); | |
5974 | } | |
5975 | ||
5976 | /* | |
5977 | * attach_tasks() -- attaches all tasks detached by detach_tasks() to their | |
5978 | * new rq. | |
5979 | */ | |
5980 | static void attach_tasks(struct lb_env *env) | |
5981 | { | |
5982 | struct list_head *tasks = &env->tasks; | |
5983 | struct task_struct *p; | |
5984 | ||
5985 | raw_spin_lock(&env->dst_rq->lock); | |
5986 | ||
5987 | while (!list_empty(tasks)) { | |
5988 | p = list_first_entry(tasks, struct task_struct, se.group_node); | |
5989 | list_del_init(&p->se.group_node); | |
1e3c88bd | 5990 | |
163122b7 KT |
5991 | attach_task(env->dst_rq, p); |
5992 | } | |
5993 | ||
5994 | raw_spin_unlock(&env->dst_rq->lock); | |
1e3c88bd PZ |
5995 | } |
5996 | ||
230059de | 5997 | #ifdef CONFIG_FAIR_GROUP_SCHED |
48a16753 | 5998 | static void update_blocked_averages(int cpu) |
9e3081ca | 5999 | { |
9e3081ca | 6000 | struct rq *rq = cpu_rq(cpu); |
48a16753 PT |
6001 | struct cfs_rq *cfs_rq; |
6002 | unsigned long flags; | |
9e3081ca | 6003 | |
48a16753 PT |
6004 | raw_spin_lock_irqsave(&rq->lock, flags); |
6005 | update_rq_clock(rq); | |
9d89c257 | 6006 | |
9763b67f PZ |
6007 | /* |
6008 | * Iterates the task_group tree in a bottom up fashion, see | |
6009 | * list_add_leaf_cfs_rq() for details. | |
6010 | */ | |
64660c86 | 6011 | for_each_leaf_cfs_rq(rq, cfs_rq) { |
9d89c257 YD |
6012 | /* throttled entities do not contribute to load */ |
6013 | if (throttled_hierarchy(cfs_rq)) | |
6014 | continue; | |
48a16753 | 6015 | |
9d89c257 YD |
6016 | if (update_cfs_rq_load_avg(cfs_rq_clock_task(cfs_rq), cfs_rq)) |
6017 | update_tg_load_avg(cfs_rq, 0); | |
6018 | } | |
48a16753 | 6019 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
9e3081ca PZ |
6020 | } |
6021 | ||
9763b67f | 6022 | /* |
68520796 | 6023 | * Compute the hierarchical load factor for cfs_rq and all its ascendants. |
9763b67f PZ |
6024 | * This needs to be done in a top-down fashion because the load of a child |
6025 | * group is a fraction of its parents load. | |
6026 | */ | |
68520796 | 6027 | static void update_cfs_rq_h_load(struct cfs_rq *cfs_rq) |
9763b67f | 6028 | { |
68520796 VD |
6029 | struct rq *rq = rq_of(cfs_rq); |
6030 | struct sched_entity *se = cfs_rq->tg->se[cpu_of(rq)]; | |
a35b6466 | 6031 | unsigned long now = jiffies; |
68520796 | 6032 | unsigned long load; |
a35b6466 | 6033 | |
68520796 | 6034 | if (cfs_rq->last_h_load_update == now) |
a35b6466 PZ |
6035 | return; |
6036 | ||
68520796 VD |
6037 | cfs_rq->h_load_next = NULL; |
6038 | for_each_sched_entity(se) { | |
6039 | cfs_rq = cfs_rq_of(se); | |
6040 | cfs_rq->h_load_next = se; | |
6041 | if (cfs_rq->last_h_load_update == now) | |
6042 | break; | |
6043 | } | |
a35b6466 | 6044 | |
68520796 | 6045 | if (!se) { |
7ea241af | 6046 | cfs_rq->h_load = cfs_rq_load_avg(cfs_rq); |
68520796 VD |
6047 | cfs_rq->last_h_load_update = now; |
6048 | } | |
6049 | ||
6050 | while ((se = cfs_rq->h_load_next) != NULL) { | |
6051 | load = cfs_rq->h_load; | |
7ea241af YD |
6052 | load = div64_ul(load * se->avg.load_avg, |
6053 | cfs_rq_load_avg(cfs_rq) + 1); | |
68520796 VD |
6054 | cfs_rq = group_cfs_rq(se); |
6055 | cfs_rq->h_load = load; | |
6056 | cfs_rq->last_h_load_update = now; | |
6057 | } | |
9763b67f PZ |
6058 | } |
6059 | ||
367456c7 | 6060 | static unsigned long task_h_load(struct task_struct *p) |
230059de | 6061 | { |
367456c7 | 6062 | struct cfs_rq *cfs_rq = task_cfs_rq(p); |
230059de | 6063 | |
68520796 | 6064 | update_cfs_rq_h_load(cfs_rq); |
9d89c257 | 6065 | return div64_ul(p->se.avg.load_avg * cfs_rq->h_load, |
7ea241af | 6066 | cfs_rq_load_avg(cfs_rq) + 1); |
230059de PZ |
6067 | } |
6068 | #else | |
48a16753 | 6069 | static inline void update_blocked_averages(int cpu) |
9e3081ca | 6070 | { |
6c1d47c0 VG |
6071 | struct rq *rq = cpu_rq(cpu); |
6072 | struct cfs_rq *cfs_rq = &rq->cfs; | |
6073 | unsigned long flags; | |
6074 | ||
6075 | raw_spin_lock_irqsave(&rq->lock, flags); | |
6076 | update_rq_clock(rq); | |
6077 | update_cfs_rq_load_avg(cfs_rq_clock_task(cfs_rq), cfs_rq); | |
6078 | raw_spin_unlock_irqrestore(&rq->lock, flags); | |
9e3081ca PZ |
6079 | } |
6080 | ||
367456c7 | 6081 | static unsigned long task_h_load(struct task_struct *p) |
1e3c88bd | 6082 | { |
9d89c257 | 6083 | return p->se.avg.load_avg; |
1e3c88bd | 6084 | } |
230059de | 6085 | #endif |
1e3c88bd | 6086 | |
1e3c88bd | 6087 | /********** Helpers for find_busiest_group ************************/ |
caeb178c RR |
6088 | |
6089 | enum group_type { | |
6090 | group_other = 0, | |
6091 | group_imbalanced, | |
6092 | group_overloaded, | |
6093 | }; | |
6094 | ||
1e3c88bd PZ |
6095 | /* |
6096 | * sg_lb_stats - stats of a sched_group required for load_balancing | |
6097 | */ | |
6098 | struct sg_lb_stats { | |
6099 | unsigned long avg_load; /*Avg load across the CPUs of the group */ | |
6100 | unsigned long group_load; /* Total load over the CPUs of the group */ | |
1e3c88bd | 6101 | unsigned long sum_weighted_load; /* Weighted load of group's tasks */ |
56cf515b | 6102 | unsigned long load_per_task; |
63b2ca30 | 6103 | unsigned long group_capacity; |
9e91d61d | 6104 | unsigned long group_util; /* Total utilization of the group */ |
147c5fc2 | 6105 | unsigned int sum_nr_running; /* Nr tasks running in the group */ |
147c5fc2 PZ |
6106 | unsigned int idle_cpus; |
6107 | unsigned int group_weight; | |
caeb178c | 6108 | enum group_type group_type; |
ea67821b | 6109 | int group_no_capacity; |
0ec8aa00 PZ |
6110 | #ifdef CONFIG_NUMA_BALANCING |
6111 | unsigned int nr_numa_running; | |
6112 | unsigned int nr_preferred_running; | |
6113 | #endif | |
1e3c88bd PZ |
6114 | }; |
6115 | ||
56cf515b JK |
6116 | /* |
6117 | * sd_lb_stats - Structure to store the statistics of a sched_domain | |
6118 | * during load balancing. | |
6119 | */ | |
6120 | struct sd_lb_stats { | |
6121 | struct sched_group *busiest; /* Busiest group in this sd */ | |
6122 | struct sched_group *local; /* Local group in this sd */ | |
6123 | unsigned long total_load; /* Total load of all groups in sd */ | |
63b2ca30 | 6124 | unsigned long total_capacity; /* Total capacity of all groups in sd */ |
56cf515b JK |
6125 | unsigned long avg_load; /* Average load across all groups in sd */ |
6126 | ||
56cf515b | 6127 | struct sg_lb_stats busiest_stat;/* Statistics of the busiest group */ |
147c5fc2 | 6128 | struct sg_lb_stats local_stat; /* Statistics of the local group */ |
56cf515b JK |
6129 | }; |
6130 | ||
147c5fc2 PZ |
6131 | static inline void init_sd_lb_stats(struct sd_lb_stats *sds) |
6132 | { | |
6133 | /* | |
6134 | * Skimp on the clearing to avoid duplicate work. We can avoid clearing | |
6135 | * local_stat because update_sg_lb_stats() does a full clear/assignment. | |
6136 | * We must however clear busiest_stat::avg_load because | |
6137 | * update_sd_pick_busiest() reads this before assignment. | |
6138 | */ | |
6139 | *sds = (struct sd_lb_stats){ | |
6140 | .busiest = NULL, | |
6141 | .local = NULL, | |
6142 | .total_load = 0UL, | |
63b2ca30 | 6143 | .total_capacity = 0UL, |
147c5fc2 PZ |
6144 | .busiest_stat = { |
6145 | .avg_load = 0UL, | |
caeb178c RR |
6146 | .sum_nr_running = 0, |
6147 | .group_type = group_other, | |
147c5fc2 PZ |
6148 | }, |
6149 | }; | |
6150 | } | |
6151 | ||
1e3c88bd PZ |
6152 | /** |
6153 | * get_sd_load_idx - Obtain the load index for a given sched domain. | |
6154 | * @sd: The sched_domain whose load_idx is to be obtained. | |
ed1b7732 | 6155 | * @idle: The idle status of the CPU for whose sd load_idx is obtained. |
e69f6186 YB |
6156 | * |
6157 | * Return: The load index. | |
1e3c88bd PZ |
6158 | */ |
6159 | static inline int get_sd_load_idx(struct sched_domain *sd, | |
6160 | enum cpu_idle_type idle) | |
6161 | { | |
6162 | int load_idx; | |
6163 | ||
6164 | switch (idle) { | |
6165 | case CPU_NOT_IDLE: | |
6166 | load_idx = sd->busy_idx; | |
6167 | break; | |
6168 | ||
6169 | case CPU_NEWLY_IDLE: | |
6170 | load_idx = sd->newidle_idx; | |
6171 | break; | |
6172 | default: | |
6173 | load_idx = sd->idle_idx; | |
6174 | break; | |
6175 | } | |
6176 | ||
6177 | return load_idx; | |
6178 | } | |
6179 | ||
ced549fa | 6180 | static unsigned long scale_rt_capacity(int cpu) |
1e3c88bd PZ |
6181 | { |
6182 | struct rq *rq = cpu_rq(cpu); | |
b5b4860d | 6183 | u64 total, used, age_stamp, avg; |
cadefd3d | 6184 | s64 delta; |
1e3c88bd | 6185 | |
b654f7de PZ |
6186 | /* |
6187 | * Since we're reading these variables without serialization make sure | |
6188 | * we read them once before doing sanity checks on them. | |
6189 | */ | |
316c1608 JL |
6190 | age_stamp = READ_ONCE(rq->age_stamp); |
6191 | avg = READ_ONCE(rq->rt_avg); | |
cebde6d6 | 6192 | delta = __rq_clock_broken(rq) - age_stamp; |
b654f7de | 6193 | |
cadefd3d PZ |
6194 | if (unlikely(delta < 0)) |
6195 | delta = 0; | |
6196 | ||
6197 | total = sched_avg_period() + delta; | |
aa483808 | 6198 | |
b5b4860d | 6199 | used = div_u64(avg, total); |
1e3c88bd | 6200 | |
b5b4860d VG |
6201 | if (likely(used < SCHED_CAPACITY_SCALE)) |
6202 | return SCHED_CAPACITY_SCALE - used; | |
1e3c88bd | 6203 | |
b5b4860d | 6204 | return 1; |
1e3c88bd PZ |
6205 | } |
6206 | ||
ced549fa | 6207 | static void update_cpu_capacity(struct sched_domain *sd, int cpu) |
1e3c88bd | 6208 | { |
8cd5601c | 6209 | unsigned long capacity = arch_scale_cpu_capacity(sd, cpu); |
1e3c88bd PZ |
6210 | struct sched_group *sdg = sd->groups; |
6211 | ||
ca6d75e6 | 6212 | cpu_rq(cpu)->cpu_capacity_orig = capacity; |
9d5efe05 | 6213 | |
ced549fa | 6214 | capacity *= scale_rt_capacity(cpu); |
ca8ce3d0 | 6215 | capacity >>= SCHED_CAPACITY_SHIFT; |
1e3c88bd | 6216 | |
ced549fa NP |
6217 | if (!capacity) |
6218 | capacity = 1; | |
1e3c88bd | 6219 | |
ced549fa NP |
6220 | cpu_rq(cpu)->cpu_capacity = capacity; |
6221 | sdg->sgc->capacity = capacity; | |
1e3c88bd PZ |
6222 | } |
6223 | ||
63b2ca30 | 6224 | void update_group_capacity(struct sched_domain *sd, int cpu) |
1e3c88bd PZ |
6225 | { |
6226 | struct sched_domain *child = sd->child; | |
6227 | struct sched_group *group, *sdg = sd->groups; | |
dc7ff76e | 6228 | unsigned long capacity; |
4ec4412e VG |
6229 | unsigned long interval; |
6230 | ||
6231 | interval = msecs_to_jiffies(sd->balance_interval); | |
6232 | interval = clamp(interval, 1UL, max_load_balance_interval); | |
63b2ca30 | 6233 | sdg->sgc->next_update = jiffies + interval; |
1e3c88bd PZ |
6234 | |
6235 | if (!child) { | |
ced549fa | 6236 | update_cpu_capacity(sd, cpu); |
1e3c88bd PZ |
6237 | return; |
6238 | } | |
6239 | ||
dc7ff76e | 6240 | capacity = 0; |
1e3c88bd | 6241 | |
74a5ce20 PZ |
6242 | if (child->flags & SD_OVERLAP) { |
6243 | /* | |
6244 | * SD_OVERLAP domains cannot assume that child groups | |
6245 | * span the current group. | |
6246 | */ | |
6247 | ||
863bffc8 | 6248 | for_each_cpu(cpu, sched_group_cpus(sdg)) { |
63b2ca30 | 6249 | struct sched_group_capacity *sgc; |
9abf24d4 | 6250 | struct rq *rq = cpu_rq(cpu); |
863bffc8 | 6251 | |
9abf24d4 | 6252 | /* |
63b2ca30 | 6253 | * build_sched_domains() -> init_sched_groups_capacity() |
9abf24d4 SD |
6254 | * gets here before we've attached the domains to the |
6255 | * runqueues. | |
6256 | * | |
ced549fa NP |
6257 | * Use capacity_of(), which is set irrespective of domains |
6258 | * in update_cpu_capacity(). | |
9abf24d4 | 6259 | * |
dc7ff76e | 6260 | * This avoids capacity from being 0 and |
9abf24d4 | 6261 | * causing divide-by-zero issues on boot. |
9abf24d4 SD |
6262 | */ |
6263 | if (unlikely(!rq->sd)) { | |
ced549fa | 6264 | capacity += capacity_of(cpu); |
9abf24d4 SD |
6265 | continue; |
6266 | } | |
863bffc8 | 6267 | |
63b2ca30 | 6268 | sgc = rq->sd->groups->sgc; |
63b2ca30 | 6269 | capacity += sgc->capacity; |
863bffc8 | 6270 | } |
74a5ce20 PZ |
6271 | } else { |
6272 | /* | |
6273 | * !SD_OVERLAP domains can assume that child groups | |
6274 | * span the current group. | |
6275 | */ | |
6276 | ||
6277 | group = child->groups; | |
6278 | do { | |
63b2ca30 | 6279 | capacity += group->sgc->capacity; |
74a5ce20 PZ |
6280 | group = group->next; |
6281 | } while (group != child->groups); | |
6282 | } | |
1e3c88bd | 6283 | |
63b2ca30 | 6284 | sdg->sgc->capacity = capacity; |
1e3c88bd PZ |
6285 | } |
6286 | ||
9d5efe05 | 6287 | /* |
ea67821b VG |
6288 | * Check whether the capacity of the rq has been noticeably reduced by side |
6289 | * activity. The imbalance_pct is used for the threshold. | |
6290 | * Return true is the capacity is reduced | |
9d5efe05 SV |
6291 | */ |
6292 | static inline int | |
ea67821b | 6293 | check_cpu_capacity(struct rq *rq, struct sched_domain *sd) |
9d5efe05 | 6294 | { |
ea67821b VG |
6295 | return ((rq->cpu_capacity * sd->imbalance_pct) < |
6296 | (rq->cpu_capacity_orig * 100)); | |
9d5efe05 SV |
6297 | } |
6298 | ||
30ce5dab PZ |
6299 | /* |
6300 | * Group imbalance indicates (and tries to solve) the problem where balancing | |
6301 | * groups is inadequate due to tsk_cpus_allowed() constraints. | |
6302 | * | |
6303 | * Imagine a situation of two groups of 4 cpus each and 4 tasks each with a | |
6304 | * cpumask covering 1 cpu of the first group and 3 cpus of the second group. | |
6305 | * Something like: | |
6306 | * | |
6307 | * { 0 1 2 3 } { 4 5 6 7 } | |
6308 | * * * * * | |
6309 | * | |
6310 | * If we were to balance group-wise we'd place two tasks in the first group and | |
6311 | * two tasks in the second group. Clearly this is undesired as it will overload | |
6312 | * cpu 3 and leave one of the cpus in the second group unused. | |
6313 | * | |
6314 | * The current solution to this issue is detecting the skew in the first group | |
6263322c PZ |
6315 | * by noticing the lower domain failed to reach balance and had difficulty |
6316 | * moving tasks due to affinity constraints. | |
30ce5dab PZ |
6317 | * |
6318 | * When this is so detected; this group becomes a candidate for busiest; see | |
ed1b7732 | 6319 | * update_sd_pick_busiest(). And calculate_imbalance() and |
6263322c | 6320 | * find_busiest_group() avoid some of the usual balance conditions to allow it |
30ce5dab PZ |
6321 | * to create an effective group imbalance. |
6322 | * | |
6323 | * This is a somewhat tricky proposition since the next run might not find the | |
6324 | * group imbalance and decide the groups need to be balanced again. A most | |
6325 | * subtle and fragile situation. | |
6326 | */ | |
6327 | ||
6263322c | 6328 | static inline int sg_imbalanced(struct sched_group *group) |
30ce5dab | 6329 | { |
63b2ca30 | 6330 | return group->sgc->imbalance; |
30ce5dab PZ |
6331 | } |
6332 | ||
b37d9316 | 6333 | /* |
ea67821b VG |
6334 | * group_has_capacity returns true if the group has spare capacity that could |
6335 | * be used by some tasks. | |
6336 | * We consider that a group has spare capacity if the * number of task is | |
9e91d61d DE |
6337 | * smaller than the number of CPUs or if the utilization is lower than the |
6338 | * available capacity for CFS tasks. | |
ea67821b VG |
6339 | * For the latter, we use a threshold to stabilize the state, to take into |
6340 | * account the variance of the tasks' load and to return true if the available | |
6341 | * capacity in meaningful for the load balancer. | |
6342 | * As an example, an available capacity of 1% can appear but it doesn't make | |
6343 | * any benefit for the load balance. | |
b37d9316 | 6344 | */ |
ea67821b VG |
6345 | static inline bool |
6346 | group_has_capacity(struct lb_env *env, struct sg_lb_stats *sgs) | |
b37d9316 | 6347 | { |
ea67821b VG |
6348 | if (sgs->sum_nr_running < sgs->group_weight) |
6349 | return true; | |
c61037e9 | 6350 | |
ea67821b | 6351 | if ((sgs->group_capacity * 100) > |
9e91d61d | 6352 | (sgs->group_util * env->sd->imbalance_pct)) |
ea67821b | 6353 | return true; |
b37d9316 | 6354 | |
ea67821b VG |
6355 | return false; |
6356 | } | |
6357 | ||
6358 | /* | |
6359 | * group_is_overloaded returns true if the group has more tasks than it can | |
6360 | * handle. | |
6361 | * group_is_overloaded is not equals to !group_has_capacity because a group | |
6362 | * with the exact right number of tasks, has no more spare capacity but is not | |
6363 | * overloaded so both group_has_capacity and group_is_overloaded return | |
6364 | * false. | |
6365 | */ | |
6366 | static inline bool | |
6367 | group_is_overloaded(struct lb_env *env, struct sg_lb_stats *sgs) | |
6368 | { | |
6369 | if (sgs->sum_nr_running <= sgs->group_weight) | |
6370 | return false; | |
b37d9316 | 6371 | |
ea67821b | 6372 | if ((sgs->group_capacity * 100) < |
9e91d61d | 6373 | (sgs->group_util * env->sd->imbalance_pct)) |
ea67821b | 6374 | return true; |
b37d9316 | 6375 | |
ea67821b | 6376 | return false; |
b37d9316 PZ |
6377 | } |
6378 | ||
79a89f92 LY |
6379 | static inline enum |
6380 | group_type group_classify(struct sched_group *group, | |
6381 | struct sg_lb_stats *sgs) | |
caeb178c | 6382 | { |
ea67821b | 6383 | if (sgs->group_no_capacity) |
caeb178c RR |
6384 | return group_overloaded; |
6385 | ||
6386 | if (sg_imbalanced(group)) | |
6387 | return group_imbalanced; | |
6388 | ||
6389 | return group_other; | |
6390 | } | |
6391 | ||
1e3c88bd PZ |
6392 | /** |
6393 | * update_sg_lb_stats - Update sched_group's statistics for load balancing. | |
cd96891d | 6394 | * @env: The load balancing environment. |
1e3c88bd | 6395 | * @group: sched_group whose statistics are to be updated. |
1e3c88bd | 6396 | * @load_idx: Load index of sched_domain of this_cpu for load calc. |
1e3c88bd | 6397 | * @local_group: Does group contain this_cpu. |
1e3c88bd | 6398 | * @sgs: variable to hold the statistics for this group. |
cd3bd4e6 | 6399 | * @overload: Indicate more than one runnable task for any CPU. |
1e3c88bd | 6400 | */ |
bd939f45 PZ |
6401 | static inline void update_sg_lb_stats(struct lb_env *env, |
6402 | struct sched_group *group, int load_idx, | |
4486edd1 TC |
6403 | int local_group, struct sg_lb_stats *sgs, |
6404 | bool *overload) | |
1e3c88bd | 6405 | { |
30ce5dab | 6406 | unsigned long load; |
a426f99c | 6407 | int i, nr_running; |
1e3c88bd | 6408 | |
b72ff13c PZ |
6409 | memset(sgs, 0, sizeof(*sgs)); |
6410 | ||
b9403130 | 6411 | for_each_cpu_and(i, sched_group_cpus(group), env->cpus) { |
1e3c88bd PZ |
6412 | struct rq *rq = cpu_rq(i); |
6413 | ||
1e3c88bd | 6414 | /* Bias balancing toward cpus of our domain */ |
6263322c | 6415 | if (local_group) |
04f733b4 | 6416 | load = target_load(i, load_idx); |
6263322c | 6417 | else |
1e3c88bd | 6418 | load = source_load(i, load_idx); |
1e3c88bd PZ |
6419 | |
6420 | sgs->group_load += load; | |
9e91d61d | 6421 | sgs->group_util += cpu_util(i); |
65fdac08 | 6422 | sgs->sum_nr_running += rq->cfs.h_nr_running; |
4486edd1 | 6423 | |
a426f99c WL |
6424 | nr_running = rq->nr_running; |
6425 | if (nr_running > 1) | |
4486edd1 TC |
6426 | *overload = true; |
6427 | ||
0ec8aa00 PZ |
6428 | #ifdef CONFIG_NUMA_BALANCING |
6429 | sgs->nr_numa_running += rq->nr_numa_running; | |
6430 | sgs->nr_preferred_running += rq->nr_preferred_running; | |
6431 | #endif | |
1e3c88bd | 6432 | sgs->sum_weighted_load += weighted_cpuload(i); |
a426f99c WL |
6433 | /* |
6434 | * No need to call idle_cpu() if nr_running is not 0 | |
6435 | */ | |
6436 | if (!nr_running && idle_cpu(i)) | |
aae6d3dd | 6437 | sgs->idle_cpus++; |
1e3c88bd PZ |
6438 | } |
6439 | ||
63b2ca30 NP |
6440 | /* Adjust by relative CPU capacity of the group */ |
6441 | sgs->group_capacity = group->sgc->capacity; | |
ca8ce3d0 | 6442 | sgs->avg_load = (sgs->group_load*SCHED_CAPACITY_SCALE) / sgs->group_capacity; |
1e3c88bd | 6443 | |
dd5feea1 | 6444 | if (sgs->sum_nr_running) |
38d0f770 | 6445 | sgs->load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running; |
1e3c88bd | 6446 | |
aae6d3dd | 6447 | sgs->group_weight = group->group_weight; |
b37d9316 | 6448 | |
ea67821b | 6449 | sgs->group_no_capacity = group_is_overloaded(env, sgs); |
79a89f92 | 6450 | sgs->group_type = group_classify(group, sgs); |
1e3c88bd PZ |
6451 | } |
6452 | ||
532cb4c4 MN |
6453 | /** |
6454 | * update_sd_pick_busiest - return 1 on busiest group | |
cd96891d | 6455 | * @env: The load balancing environment. |
532cb4c4 MN |
6456 | * @sds: sched_domain statistics |
6457 | * @sg: sched_group candidate to be checked for being the busiest | |
b6b12294 | 6458 | * @sgs: sched_group statistics |
532cb4c4 MN |
6459 | * |
6460 | * Determine if @sg is a busier group than the previously selected | |
6461 | * busiest group. | |
e69f6186 YB |
6462 | * |
6463 | * Return: %true if @sg is a busier group than the previously selected | |
6464 | * busiest group. %false otherwise. | |
532cb4c4 | 6465 | */ |
bd939f45 | 6466 | static bool update_sd_pick_busiest(struct lb_env *env, |
532cb4c4 MN |
6467 | struct sd_lb_stats *sds, |
6468 | struct sched_group *sg, | |
bd939f45 | 6469 | struct sg_lb_stats *sgs) |
532cb4c4 | 6470 | { |
caeb178c | 6471 | struct sg_lb_stats *busiest = &sds->busiest_stat; |
532cb4c4 | 6472 | |
caeb178c | 6473 | if (sgs->group_type > busiest->group_type) |
532cb4c4 MN |
6474 | return true; |
6475 | ||
caeb178c RR |
6476 | if (sgs->group_type < busiest->group_type) |
6477 | return false; | |
6478 | ||
6479 | if (sgs->avg_load <= busiest->avg_load) | |
6480 | return false; | |
6481 | ||
6482 | /* This is the busiest node in its class. */ | |
6483 | if (!(env->sd->flags & SD_ASYM_PACKING)) | |
532cb4c4 MN |
6484 | return true; |
6485 | ||
6486 | /* | |
6487 | * ASYM_PACKING needs to move all the work to the lowest | |
6488 | * numbered CPUs in the group, therefore mark all groups | |
6489 | * higher than ourself as busy. | |
6490 | */ | |
caeb178c | 6491 | if (sgs->sum_nr_running && env->dst_cpu < group_first_cpu(sg)) { |
532cb4c4 MN |
6492 | if (!sds->busiest) |
6493 | return true; | |
6494 | ||
6495 | if (group_first_cpu(sds->busiest) > group_first_cpu(sg)) | |
6496 | return true; | |
6497 | } | |
6498 | ||
6499 | return false; | |
6500 | } | |
6501 | ||
0ec8aa00 PZ |
6502 | #ifdef CONFIG_NUMA_BALANCING |
6503 | static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs) | |
6504 | { | |
6505 | if (sgs->sum_nr_running > sgs->nr_numa_running) | |
6506 | return regular; | |
6507 | if (sgs->sum_nr_running > sgs->nr_preferred_running) | |
6508 | return remote; | |
6509 | return all; | |
6510 | } | |
6511 | ||
6512 | static inline enum fbq_type fbq_classify_rq(struct rq *rq) | |
6513 | { | |
6514 | if (rq->nr_running > rq->nr_numa_running) | |
6515 | return regular; | |
6516 | if (rq->nr_running > rq->nr_preferred_running) | |
6517 | return remote; | |
6518 | return all; | |
6519 | } | |
6520 | #else | |
6521 | static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs) | |
6522 | { | |
6523 | return all; | |
6524 | } | |
6525 | ||
6526 | static inline enum fbq_type fbq_classify_rq(struct rq *rq) | |
6527 | { | |
6528 | return regular; | |
6529 | } | |
6530 | #endif /* CONFIG_NUMA_BALANCING */ | |
6531 | ||
1e3c88bd | 6532 | /** |
461819ac | 6533 | * update_sd_lb_stats - Update sched_domain's statistics for load balancing. |
cd96891d | 6534 | * @env: The load balancing environment. |
1e3c88bd PZ |
6535 | * @sds: variable to hold the statistics for this sched_domain. |
6536 | */ | |
0ec8aa00 | 6537 | static inline void update_sd_lb_stats(struct lb_env *env, struct sd_lb_stats *sds) |
1e3c88bd | 6538 | { |
bd939f45 PZ |
6539 | struct sched_domain *child = env->sd->child; |
6540 | struct sched_group *sg = env->sd->groups; | |
56cf515b | 6541 | struct sg_lb_stats tmp_sgs; |
1e3c88bd | 6542 | int load_idx, prefer_sibling = 0; |
4486edd1 | 6543 | bool overload = false; |
1e3c88bd PZ |
6544 | |
6545 | if (child && child->flags & SD_PREFER_SIBLING) | |
6546 | prefer_sibling = 1; | |
6547 | ||
bd939f45 | 6548 | load_idx = get_sd_load_idx(env->sd, env->idle); |
1e3c88bd PZ |
6549 | |
6550 | do { | |
56cf515b | 6551 | struct sg_lb_stats *sgs = &tmp_sgs; |
1e3c88bd PZ |
6552 | int local_group; |
6553 | ||
bd939f45 | 6554 | local_group = cpumask_test_cpu(env->dst_cpu, sched_group_cpus(sg)); |
56cf515b JK |
6555 | if (local_group) { |
6556 | sds->local = sg; | |
6557 | sgs = &sds->local_stat; | |
b72ff13c PZ |
6558 | |
6559 | if (env->idle != CPU_NEWLY_IDLE || | |
63b2ca30 NP |
6560 | time_after_eq(jiffies, sg->sgc->next_update)) |
6561 | update_group_capacity(env->sd, env->dst_cpu); | |
56cf515b | 6562 | } |
1e3c88bd | 6563 | |
4486edd1 TC |
6564 | update_sg_lb_stats(env, sg, load_idx, local_group, sgs, |
6565 | &overload); | |
1e3c88bd | 6566 | |
b72ff13c PZ |
6567 | if (local_group) |
6568 | goto next_group; | |
6569 | ||
1e3c88bd PZ |
6570 | /* |
6571 | * In case the child domain prefers tasks go to siblings | |
ea67821b | 6572 | * first, lower the sg capacity so that we'll try |
75dd321d NR |
6573 | * and move all the excess tasks away. We lower the capacity |
6574 | * of a group only if the local group has the capacity to fit | |
ea67821b VG |
6575 | * these excess tasks. The extra check prevents the case where |
6576 | * you always pull from the heaviest group when it is already | |
6577 | * under-utilized (possible with a large weight task outweighs | |
6578 | * the tasks on the system). | |
1e3c88bd | 6579 | */ |
b72ff13c | 6580 | if (prefer_sibling && sds->local && |
ea67821b VG |
6581 | group_has_capacity(env, &sds->local_stat) && |
6582 | (sgs->sum_nr_running > 1)) { | |
6583 | sgs->group_no_capacity = 1; | |
79a89f92 | 6584 | sgs->group_type = group_classify(sg, sgs); |
cb0b9f24 | 6585 | } |
1e3c88bd | 6586 | |
b72ff13c | 6587 | if (update_sd_pick_busiest(env, sds, sg, sgs)) { |
532cb4c4 | 6588 | sds->busiest = sg; |
56cf515b | 6589 | sds->busiest_stat = *sgs; |
1e3c88bd PZ |
6590 | } |
6591 | ||
b72ff13c PZ |
6592 | next_group: |
6593 | /* Now, start updating sd_lb_stats */ | |
6594 | sds->total_load += sgs->group_load; | |
63b2ca30 | 6595 | sds->total_capacity += sgs->group_capacity; |
b72ff13c | 6596 | |
532cb4c4 | 6597 | sg = sg->next; |
bd939f45 | 6598 | } while (sg != env->sd->groups); |
0ec8aa00 PZ |
6599 | |
6600 | if (env->sd->flags & SD_NUMA) | |
6601 | env->fbq_type = fbq_classify_group(&sds->busiest_stat); | |
4486edd1 TC |
6602 | |
6603 | if (!env->sd->parent) { | |
6604 | /* update overload indicator if we are at root domain */ | |
6605 | if (env->dst_rq->rd->overload != overload) | |
6606 | env->dst_rq->rd->overload = overload; | |
6607 | } | |
6608 | ||
532cb4c4 MN |
6609 | } |
6610 | ||
532cb4c4 MN |
6611 | /** |
6612 | * check_asym_packing - Check to see if the group is packed into the | |
6613 | * sched doman. | |
6614 | * | |
6615 | * This is primarily intended to used at the sibling level. Some | |
6616 | * cores like POWER7 prefer to use lower numbered SMT threads. In the | |
6617 | * case of POWER7, it can move to lower SMT modes only when higher | |
6618 | * threads are idle. When in lower SMT modes, the threads will | |
6619 | * perform better since they share less core resources. Hence when we | |
6620 | * have idle threads, we want them to be the higher ones. | |
6621 | * | |
6622 | * This packing function is run on idle threads. It checks to see if | |
6623 | * the busiest CPU in this domain (core in the P7 case) has a higher | |
6624 | * CPU number than the packing function is being run on. Here we are | |
6625 | * assuming lower CPU number will be equivalent to lower a SMT thread | |
6626 | * number. | |
6627 | * | |
e69f6186 | 6628 | * Return: 1 when packing is required and a task should be moved to |
b6b12294 MN |
6629 | * this CPU. The amount of the imbalance is returned in *imbalance. |
6630 | * | |
cd96891d | 6631 | * @env: The load balancing environment. |
532cb4c4 | 6632 | * @sds: Statistics of the sched_domain which is to be packed |
532cb4c4 | 6633 | */ |
bd939f45 | 6634 | static int check_asym_packing(struct lb_env *env, struct sd_lb_stats *sds) |
532cb4c4 MN |
6635 | { |
6636 | int busiest_cpu; | |
6637 | ||
bd939f45 | 6638 | if (!(env->sd->flags & SD_ASYM_PACKING)) |
532cb4c4 MN |
6639 | return 0; |
6640 | ||
6641 | if (!sds->busiest) | |
6642 | return 0; | |
6643 | ||
6644 | busiest_cpu = group_first_cpu(sds->busiest); | |
bd939f45 | 6645 | if (env->dst_cpu > busiest_cpu) |
532cb4c4 MN |
6646 | return 0; |
6647 | ||
bd939f45 | 6648 | env->imbalance = DIV_ROUND_CLOSEST( |
63b2ca30 | 6649 | sds->busiest_stat.avg_load * sds->busiest_stat.group_capacity, |
ca8ce3d0 | 6650 | SCHED_CAPACITY_SCALE); |
bd939f45 | 6651 | |
532cb4c4 | 6652 | return 1; |
1e3c88bd PZ |
6653 | } |
6654 | ||
6655 | /** | |
6656 | * fix_small_imbalance - Calculate the minor imbalance that exists | |
6657 | * amongst the groups of a sched_domain, during | |
6658 | * load balancing. | |
cd96891d | 6659 | * @env: The load balancing environment. |
1e3c88bd | 6660 | * @sds: Statistics of the sched_domain whose imbalance is to be calculated. |
1e3c88bd | 6661 | */ |
bd939f45 PZ |
6662 | static inline |
6663 | void fix_small_imbalance(struct lb_env *env, struct sd_lb_stats *sds) | |
1e3c88bd | 6664 | { |
63b2ca30 | 6665 | unsigned long tmp, capa_now = 0, capa_move = 0; |
1e3c88bd | 6666 | unsigned int imbn = 2; |
dd5feea1 | 6667 | unsigned long scaled_busy_load_per_task; |
56cf515b | 6668 | struct sg_lb_stats *local, *busiest; |
1e3c88bd | 6669 | |
56cf515b JK |
6670 | local = &sds->local_stat; |
6671 | busiest = &sds->busiest_stat; | |
1e3c88bd | 6672 | |
56cf515b JK |
6673 | if (!local->sum_nr_running) |
6674 | local->load_per_task = cpu_avg_load_per_task(env->dst_cpu); | |
6675 | else if (busiest->load_per_task > local->load_per_task) | |
6676 | imbn = 1; | |
dd5feea1 | 6677 | |
56cf515b | 6678 | scaled_busy_load_per_task = |
ca8ce3d0 | 6679 | (busiest->load_per_task * SCHED_CAPACITY_SCALE) / |
63b2ca30 | 6680 | busiest->group_capacity; |
56cf515b | 6681 | |
3029ede3 VD |
6682 | if (busiest->avg_load + scaled_busy_load_per_task >= |
6683 | local->avg_load + (scaled_busy_load_per_task * imbn)) { | |
56cf515b | 6684 | env->imbalance = busiest->load_per_task; |
1e3c88bd PZ |
6685 | return; |
6686 | } | |
6687 | ||
6688 | /* | |
6689 | * OK, we don't have enough imbalance to justify moving tasks, | |
ced549fa | 6690 | * however we may be able to increase total CPU capacity used by |
1e3c88bd PZ |
6691 | * moving them. |
6692 | */ | |
6693 | ||
63b2ca30 | 6694 | capa_now += busiest->group_capacity * |
56cf515b | 6695 | min(busiest->load_per_task, busiest->avg_load); |
63b2ca30 | 6696 | capa_now += local->group_capacity * |
56cf515b | 6697 | min(local->load_per_task, local->avg_load); |
ca8ce3d0 | 6698 | capa_now /= SCHED_CAPACITY_SCALE; |
1e3c88bd PZ |
6699 | |
6700 | /* Amount of load we'd subtract */ | |
a2cd4260 | 6701 | if (busiest->avg_load > scaled_busy_load_per_task) { |
63b2ca30 | 6702 | capa_move += busiest->group_capacity * |
56cf515b | 6703 | min(busiest->load_per_task, |
a2cd4260 | 6704 | busiest->avg_load - scaled_busy_load_per_task); |
56cf515b | 6705 | } |
1e3c88bd PZ |
6706 | |
6707 | /* Amount of load we'd add */ | |
63b2ca30 | 6708 | if (busiest->avg_load * busiest->group_capacity < |
ca8ce3d0 | 6709 | busiest->load_per_task * SCHED_CAPACITY_SCALE) { |
63b2ca30 NP |
6710 | tmp = (busiest->avg_load * busiest->group_capacity) / |
6711 | local->group_capacity; | |
56cf515b | 6712 | } else { |
ca8ce3d0 | 6713 | tmp = (busiest->load_per_task * SCHED_CAPACITY_SCALE) / |
63b2ca30 | 6714 | local->group_capacity; |
56cf515b | 6715 | } |
63b2ca30 | 6716 | capa_move += local->group_capacity * |
3ae11c90 | 6717 | min(local->load_per_task, local->avg_load + tmp); |
ca8ce3d0 | 6718 | capa_move /= SCHED_CAPACITY_SCALE; |
1e3c88bd PZ |
6719 | |
6720 | /* Move if we gain throughput */ | |
63b2ca30 | 6721 | if (capa_move > capa_now) |
56cf515b | 6722 | env->imbalance = busiest->load_per_task; |
1e3c88bd PZ |
6723 | } |
6724 | ||
6725 | /** | |
6726 | * calculate_imbalance - Calculate the amount of imbalance present within the | |
6727 | * groups of a given sched_domain during load balance. | |
bd939f45 | 6728 | * @env: load balance environment |
1e3c88bd | 6729 | * @sds: statistics of the sched_domain whose imbalance is to be calculated. |
1e3c88bd | 6730 | */ |
bd939f45 | 6731 | static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *sds) |
1e3c88bd | 6732 | { |
dd5feea1 | 6733 | unsigned long max_pull, load_above_capacity = ~0UL; |
56cf515b JK |
6734 | struct sg_lb_stats *local, *busiest; |
6735 | ||
6736 | local = &sds->local_stat; | |
56cf515b | 6737 | busiest = &sds->busiest_stat; |
dd5feea1 | 6738 | |
caeb178c | 6739 | if (busiest->group_type == group_imbalanced) { |
30ce5dab PZ |
6740 | /* |
6741 | * In the group_imb case we cannot rely on group-wide averages | |
6742 | * to ensure cpu-load equilibrium, look at wider averages. XXX | |
6743 | */ | |
56cf515b JK |
6744 | busiest->load_per_task = |
6745 | min(busiest->load_per_task, sds->avg_load); | |
dd5feea1 SS |
6746 | } |
6747 | ||
1e3c88bd PZ |
6748 | /* |
6749 | * In the presence of smp nice balancing, certain scenarios can have | |
6750 | * max load less than avg load(as we skip the groups at or below | |
ced549fa | 6751 | * its cpu_capacity, while calculating max_load..) |
1e3c88bd | 6752 | */ |
b1885550 VD |
6753 | if (busiest->avg_load <= sds->avg_load || |
6754 | local->avg_load >= sds->avg_load) { | |
bd939f45 PZ |
6755 | env->imbalance = 0; |
6756 | return fix_small_imbalance(env, sds); | |
1e3c88bd PZ |
6757 | } |
6758 | ||
9a5d9ba6 PZ |
6759 | /* |
6760 | * If there aren't any idle cpus, avoid creating some. | |
6761 | */ | |
6762 | if (busiest->group_type == group_overloaded && | |
6763 | local->group_type == group_overloaded) { | |
ea67821b VG |
6764 | load_above_capacity = busiest->sum_nr_running * |
6765 | SCHED_LOAD_SCALE; | |
6766 | if (load_above_capacity > busiest->group_capacity) | |
6767 | load_above_capacity -= busiest->group_capacity; | |
6768 | else | |
6769 | load_above_capacity = ~0UL; | |
dd5feea1 SS |
6770 | } |
6771 | ||
6772 | /* | |
6773 | * We're trying to get all the cpus to the average_load, so we don't | |
6774 | * want to push ourselves above the average load, nor do we wish to | |
6775 | * reduce the max loaded cpu below the average load. At the same time, | |
6776 | * we also don't want to reduce the group load below the group capacity | |
6777 | * (so that we can implement power-savings policies etc). Thus we look | |
6778 | * for the minimum possible imbalance. | |
dd5feea1 | 6779 | */ |
30ce5dab | 6780 | max_pull = min(busiest->avg_load - sds->avg_load, load_above_capacity); |
1e3c88bd PZ |
6781 | |
6782 | /* How much load to actually move to equalise the imbalance */ | |
56cf515b | 6783 | env->imbalance = min( |
63b2ca30 NP |
6784 | max_pull * busiest->group_capacity, |
6785 | (sds->avg_load - local->avg_load) * local->group_capacity | |
ca8ce3d0 | 6786 | ) / SCHED_CAPACITY_SCALE; |
1e3c88bd PZ |
6787 | |
6788 | /* | |
6789 | * if *imbalance is less than the average load per runnable task | |
25985edc | 6790 | * there is no guarantee that any tasks will be moved so we'll have |
1e3c88bd PZ |
6791 | * a think about bumping its value to force at least one task to be |
6792 | * moved | |
6793 | */ | |
56cf515b | 6794 | if (env->imbalance < busiest->load_per_task) |
bd939f45 | 6795 | return fix_small_imbalance(env, sds); |
1e3c88bd | 6796 | } |
fab47622 | 6797 | |
1e3c88bd PZ |
6798 | /******* find_busiest_group() helpers end here *********************/ |
6799 | ||
6800 | /** | |
6801 | * find_busiest_group - Returns the busiest group within the sched_domain | |
6802 | * if there is an imbalance. If there isn't an imbalance, and | |
6803 | * the user has opted for power-savings, it returns a group whose | |
6804 | * CPUs can be put to idle by rebalancing those tasks elsewhere, if | |
6805 | * such a group exists. | |
6806 | * | |
6807 | * Also calculates the amount of weighted load which should be moved | |
6808 | * to restore balance. | |
6809 | * | |
cd96891d | 6810 | * @env: The load balancing environment. |
1e3c88bd | 6811 | * |
e69f6186 | 6812 | * Return: - The busiest group if imbalance exists. |
1e3c88bd PZ |
6813 | * - If no imbalance and user has opted for power-savings balance, |
6814 | * return the least loaded group whose CPUs can be | |
6815 | * put to idle by rebalancing its tasks onto our group. | |
6816 | */ | |
56cf515b | 6817 | static struct sched_group *find_busiest_group(struct lb_env *env) |
1e3c88bd | 6818 | { |
56cf515b | 6819 | struct sg_lb_stats *local, *busiest; |
1e3c88bd PZ |
6820 | struct sd_lb_stats sds; |
6821 | ||
147c5fc2 | 6822 | init_sd_lb_stats(&sds); |
1e3c88bd PZ |
6823 | |
6824 | /* | |
6825 | * Compute the various statistics relavent for load balancing at | |
6826 | * this level. | |
6827 | */ | |
23f0d209 | 6828 | update_sd_lb_stats(env, &sds); |
56cf515b JK |
6829 | local = &sds.local_stat; |
6830 | busiest = &sds.busiest_stat; | |
1e3c88bd | 6831 | |
ea67821b | 6832 | /* ASYM feature bypasses nice load balance check */ |
bd939f45 PZ |
6833 | if ((env->idle == CPU_IDLE || env->idle == CPU_NEWLY_IDLE) && |
6834 | check_asym_packing(env, &sds)) | |
532cb4c4 MN |
6835 | return sds.busiest; |
6836 | ||
cc57aa8f | 6837 | /* There is no busy sibling group to pull tasks from */ |
56cf515b | 6838 | if (!sds.busiest || busiest->sum_nr_running == 0) |
1e3c88bd PZ |
6839 | goto out_balanced; |
6840 | ||
ca8ce3d0 NP |
6841 | sds.avg_load = (SCHED_CAPACITY_SCALE * sds.total_load) |
6842 | / sds.total_capacity; | |
b0432d8f | 6843 | |
866ab43e PZ |
6844 | /* |
6845 | * If the busiest group is imbalanced the below checks don't | |
30ce5dab | 6846 | * work because they assume all things are equal, which typically |
866ab43e PZ |
6847 | * isn't true due to cpus_allowed constraints and the like. |
6848 | */ | |
caeb178c | 6849 | if (busiest->group_type == group_imbalanced) |
866ab43e PZ |
6850 | goto force_balance; |
6851 | ||
cc57aa8f | 6852 | /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */ |
ea67821b VG |
6853 | if (env->idle == CPU_NEWLY_IDLE && group_has_capacity(env, local) && |
6854 | busiest->group_no_capacity) | |
fab47622 NR |
6855 | goto force_balance; |
6856 | ||
cc57aa8f | 6857 | /* |
9c58c79a | 6858 | * If the local group is busier than the selected busiest group |
cc57aa8f PZ |
6859 | * don't try and pull any tasks. |
6860 | */ | |
56cf515b | 6861 | if (local->avg_load >= busiest->avg_load) |
1e3c88bd PZ |
6862 | goto out_balanced; |
6863 | ||
cc57aa8f PZ |
6864 | /* |
6865 | * Don't pull any tasks if this group is already above the domain | |
6866 | * average load. | |
6867 | */ | |
56cf515b | 6868 | if (local->avg_load >= sds.avg_load) |
1e3c88bd PZ |
6869 | goto out_balanced; |
6870 | ||
bd939f45 | 6871 | if (env->idle == CPU_IDLE) { |
aae6d3dd | 6872 | /* |
43f4d666 VG |
6873 | * This cpu is idle. If the busiest group is not overloaded |
6874 | * and there is no imbalance between this and busiest group | |
6875 | * wrt idle cpus, it is balanced. The imbalance becomes | |
6876 | * significant if the diff is greater than 1 otherwise we | |
6877 | * might end up to just move the imbalance on another group | |
aae6d3dd | 6878 | */ |
43f4d666 VG |
6879 | if ((busiest->group_type != group_overloaded) && |
6880 | (local->idle_cpus <= (busiest->idle_cpus + 1))) | |
aae6d3dd | 6881 | goto out_balanced; |
c186fafe PZ |
6882 | } else { |
6883 | /* | |
6884 | * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use | |
6885 | * imbalance_pct to be conservative. | |
6886 | */ | |
56cf515b JK |
6887 | if (100 * busiest->avg_load <= |
6888 | env->sd->imbalance_pct * local->avg_load) | |
c186fafe | 6889 | goto out_balanced; |
aae6d3dd | 6890 | } |
1e3c88bd | 6891 | |
fab47622 | 6892 | force_balance: |
1e3c88bd | 6893 | /* Looks like there is an imbalance. Compute it */ |
bd939f45 | 6894 | calculate_imbalance(env, &sds); |
1e3c88bd PZ |
6895 | return sds.busiest; |
6896 | ||
6897 | out_balanced: | |
bd939f45 | 6898 | env->imbalance = 0; |
1e3c88bd PZ |
6899 | return NULL; |
6900 | } | |
6901 | ||
6902 | /* | |
6903 | * find_busiest_queue - find the busiest runqueue among the cpus in group. | |
6904 | */ | |
bd939f45 | 6905 | static struct rq *find_busiest_queue(struct lb_env *env, |
b9403130 | 6906 | struct sched_group *group) |
1e3c88bd PZ |
6907 | { |
6908 | struct rq *busiest = NULL, *rq; | |
ced549fa | 6909 | unsigned long busiest_load = 0, busiest_capacity = 1; |
1e3c88bd PZ |
6910 | int i; |
6911 | ||
6906a408 | 6912 | for_each_cpu_and(i, sched_group_cpus(group), env->cpus) { |
ea67821b | 6913 | unsigned long capacity, wl; |
0ec8aa00 PZ |
6914 | enum fbq_type rt; |
6915 | ||
6916 | rq = cpu_rq(i); | |
6917 | rt = fbq_classify_rq(rq); | |
1e3c88bd | 6918 | |
0ec8aa00 PZ |
6919 | /* |
6920 | * We classify groups/runqueues into three groups: | |
6921 | * - regular: there are !numa tasks | |
6922 | * - remote: there are numa tasks that run on the 'wrong' node | |
6923 | * - all: there is no distinction | |
6924 | * | |
6925 | * In order to avoid migrating ideally placed numa tasks, | |
6926 | * ignore those when there's better options. | |
6927 | * | |
6928 | * If we ignore the actual busiest queue to migrate another | |
6929 | * task, the next balance pass can still reduce the busiest | |
6930 | * queue by moving tasks around inside the node. | |
6931 | * | |
6932 | * If we cannot move enough load due to this classification | |
6933 | * the next pass will adjust the group classification and | |
6934 | * allow migration of more tasks. | |
6935 | * | |
6936 | * Both cases only affect the total convergence complexity. | |
6937 | */ | |
6938 | if (rt > env->fbq_type) | |
6939 | continue; | |
6940 | ||
ced549fa | 6941 | capacity = capacity_of(i); |
9d5efe05 | 6942 | |
6e40f5bb | 6943 | wl = weighted_cpuload(i); |
1e3c88bd | 6944 | |
6e40f5bb TG |
6945 | /* |
6946 | * When comparing with imbalance, use weighted_cpuload() | |
ced549fa | 6947 | * which is not scaled with the cpu capacity. |
6e40f5bb | 6948 | */ |
ea67821b VG |
6949 | |
6950 | if (rq->nr_running == 1 && wl > env->imbalance && | |
6951 | !check_cpu_capacity(rq, env->sd)) | |
1e3c88bd PZ |
6952 | continue; |
6953 | ||
6e40f5bb TG |
6954 | /* |
6955 | * For the load comparisons with the other cpu's, consider | |
ced549fa NP |
6956 | * the weighted_cpuload() scaled with the cpu capacity, so |
6957 | * that the load can be moved away from the cpu that is | |
6958 | * potentially running at a lower capacity. | |
95a79b80 | 6959 | * |
ced549fa | 6960 | * Thus we're looking for max(wl_i / capacity_i), crosswise |
95a79b80 | 6961 | * multiplication to rid ourselves of the division works out |
ced549fa NP |
6962 | * to: wl_i * capacity_j > wl_j * capacity_i; where j is |
6963 | * our previous maximum. | |
6e40f5bb | 6964 | */ |
ced549fa | 6965 | if (wl * busiest_capacity > busiest_load * capacity) { |
95a79b80 | 6966 | busiest_load = wl; |
ced549fa | 6967 | busiest_capacity = capacity; |
1e3c88bd PZ |
6968 | busiest = rq; |
6969 | } | |
6970 | } | |
6971 | ||
6972 | return busiest; | |
6973 | } | |
6974 | ||
6975 | /* | |
6976 | * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but | |
6977 | * so long as it is large enough. | |
6978 | */ | |
6979 | #define MAX_PINNED_INTERVAL 512 | |
6980 | ||
6981 | /* Working cpumask for load_balance and load_balance_newidle. */ | |
e6252c3e | 6982 | DEFINE_PER_CPU(cpumask_var_t, load_balance_mask); |
1e3c88bd | 6983 | |
bd939f45 | 6984 | static int need_active_balance(struct lb_env *env) |
1af3ed3d | 6985 | { |
bd939f45 PZ |
6986 | struct sched_domain *sd = env->sd; |
6987 | ||
6988 | if (env->idle == CPU_NEWLY_IDLE) { | |
532cb4c4 MN |
6989 | |
6990 | /* | |
6991 | * ASYM_PACKING needs to force migrate tasks from busy but | |
6992 | * higher numbered CPUs in order to pack all tasks in the | |
6993 | * lowest numbered CPUs. | |
6994 | */ | |
bd939f45 | 6995 | if ((sd->flags & SD_ASYM_PACKING) && env->src_cpu > env->dst_cpu) |
532cb4c4 | 6996 | return 1; |
1af3ed3d PZ |
6997 | } |
6998 | ||
1aaf90a4 VG |
6999 | /* |
7000 | * The dst_cpu is idle and the src_cpu CPU has only 1 CFS task. | |
7001 | * It's worth migrating the task if the src_cpu's capacity is reduced | |
7002 | * because of other sched_class or IRQs if more capacity stays | |
7003 | * available on dst_cpu. | |
7004 | */ | |
7005 | if ((env->idle != CPU_NOT_IDLE) && | |
7006 | (env->src_rq->cfs.h_nr_running == 1)) { | |
7007 | if ((check_cpu_capacity(env->src_rq, sd)) && | |
7008 | (capacity_of(env->src_cpu)*sd->imbalance_pct < capacity_of(env->dst_cpu)*100)) | |
7009 | return 1; | |
7010 | } | |
7011 | ||
1af3ed3d PZ |
7012 | return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2); |
7013 | } | |
7014 | ||
969c7921 TH |
7015 | static int active_load_balance_cpu_stop(void *data); |
7016 | ||
23f0d209 JK |
7017 | static int should_we_balance(struct lb_env *env) |
7018 | { | |
7019 | struct sched_group *sg = env->sd->groups; | |
7020 | struct cpumask *sg_cpus, *sg_mask; | |
7021 | int cpu, balance_cpu = -1; | |
7022 | ||
7023 | /* | |
7024 | * In the newly idle case, we will allow all the cpu's | |
7025 | * to do the newly idle load balance. | |
7026 | */ | |
7027 | if (env->idle == CPU_NEWLY_IDLE) | |
7028 | return 1; | |
7029 | ||
7030 | sg_cpus = sched_group_cpus(sg); | |
7031 | sg_mask = sched_group_mask(sg); | |
7032 | /* Try to find first idle cpu */ | |
7033 | for_each_cpu_and(cpu, sg_cpus, env->cpus) { | |
7034 | if (!cpumask_test_cpu(cpu, sg_mask) || !idle_cpu(cpu)) | |
7035 | continue; | |
7036 | ||
7037 | balance_cpu = cpu; | |
7038 | break; | |
7039 | } | |
7040 | ||
7041 | if (balance_cpu == -1) | |
7042 | balance_cpu = group_balance_cpu(sg); | |
7043 | ||
7044 | /* | |
7045 | * First idle cpu or the first cpu(busiest) in this sched group | |
7046 | * is eligible for doing load balancing at this and above domains. | |
7047 | */ | |
b0cff9d8 | 7048 | return balance_cpu == env->dst_cpu; |
23f0d209 JK |
7049 | } |
7050 | ||
1e3c88bd PZ |
7051 | /* |
7052 | * Check this_cpu to ensure it is balanced within domain. Attempt to move | |
7053 | * tasks if there is an imbalance. | |
7054 | */ | |
7055 | static int load_balance(int this_cpu, struct rq *this_rq, | |
7056 | struct sched_domain *sd, enum cpu_idle_type idle, | |
23f0d209 | 7057 | int *continue_balancing) |
1e3c88bd | 7058 | { |
88b8dac0 | 7059 | int ld_moved, cur_ld_moved, active_balance = 0; |
6263322c | 7060 | struct sched_domain *sd_parent = sd->parent; |
1e3c88bd | 7061 | struct sched_group *group; |
1e3c88bd PZ |
7062 | struct rq *busiest; |
7063 | unsigned long flags; | |
4ba29684 | 7064 | struct cpumask *cpus = this_cpu_cpumask_var_ptr(load_balance_mask); |
1e3c88bd | 7065 | |
8e45cb54 PZ |
7066 | struct lb_env env = { |
7067 | .sd = sd, | |
ddcdf6e7 PZ |
7068 | .dst_cpu = this_cpu, |
7069 | .dst_rq = this_rq, | |
88b8dac0 | 7070 | .dst_grpmask = sched_group_cpus(sd->groups), |
8e45cb54 | 7071 | .idle = idle, |
eb95308e | 7072 | .loop_break = sched_nr_migrate_break, |
b9403130 | 7073 | .cpus = cpus, |
0ec8aa00 | 7074 | .fbq_type = all, |
163122b7 | 7075 | .tasks = LIST_HEAD_INIT(env.tasks), |
8e45cb54 PZ |
7076 | }; |
7077 | ||
cfc03118 JK |
7078 | /* |
7079 | * For NEWLY_IDLE load_balancing, we don't need to consider | |
7080 | * other cpus in our group | |
7081 | */ | |
e02e60c1 | 7082 | if (idle == CPU_NEWLY_IDLE) |
cfc03118 | 7083 | env.dst_grpmask = NULL; |
cfc03118 | 7084 | |
1e3c88bd PZ |
7085 | cpumask_copy(cpus, cpu_active_mask); |
7086 | ||
1e3c88bd PZ |
7087 | schedstat_inc(sd, lb_count[idle]); |
7088 | ||
7089 | redo: | |
23f0d209 JK |
7090 | if (!should_we_balance(&env)) { |
7091 | *continue_balancing = 0; | |
1e3c88bd | 7092 | goto out_balanced; |
23f0d209 | 7093 | } |
1e3c88bd | 7094 | |
23f0d209 | 7095 | group = find_busiest_group(&env); |
1e3c88bd PZ |
7096 | if (!group) { |
7097 | schedstat_inc(sd, lb_nobusyg[idle]); | |
7098 | goto out_balanced; | |
7099 | } | |
7100 | ||
b9403130 | 7101 | busiest = find_busiest_queue(&env, group); |
1e3c88bd PZ |
7102 | if (!busiest) { |
7103 | schedstat_inc(sd, lb_nobusyq[idle]); | |
7104 | goto out_balanced; | |
7105 | } | |
7106 | ||
78feefc5 | 7107 | BUG_ON(busiest == env.dst_rq); |
1e3c88bd | 7108 | |
bd939f45 | 7109 | schedstat_add(sd, lb_imbalance[idle], env.imbalance); |
1e3c88bd | 7110 | |
1aaf90a4 VG |
7111 | env.src_cpu = busiest->cpu; |
7112 | env.src_rq = busiest; | |
7113 | ||
1e3c88bd PZ |
7114 | ld_moved = 0; |
7115 | if (busiest->nr_running > 1) { | |
7116 | /* | |
7117 | * Attempt to move tasks. If find_busiest_group has found | |
7118 | * an imbalance but busiest->nr_running <= 1, the group is | |
7119 | * still unbalanced. ld_moved simply stays zero, so it is | |
7120 | * correctly treated as an imbalance. | |
7121 | */ | |
8e45cb54 | 7122 | env.flags |= LBF_ALL_PINNED; |
c82513e5 | 7123 | env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running); |
8e45cb54 | 7124 | |
5d6523eb | 7125 | more_balance: |
163122b7 | 7126 | raw_spin_lock_irqsave(&busiest->lock, flags); |
88b8dac0 SV |
7127 | |
7128 | /* | |
7129 | * cur_ld_moved - load moved in current iteration | |
7130 | * ld_moved - cumulative load moved across iterations | |
7131 | */ | |
163122b7 | 7132 | cur_ld_moved = detach_tasks(&env); |
1e3c88bd PZ |
7133 | |
7134 | /* | |
163122b7 KT |
7135 | * We've detached some tasks from busiest_rq. Every |
7136 | * task is masked "TASK_ON_RQ_MIGRATING", so we can safely | |
7137 | * unlock busiest->lock, and we are able to be sure | |
7138 | * that nobody can manipulate the tasks in parallel. | |
7139 | * See task_rq_lock() family for the details. | |
1e3c88bd | 7140 | */ |
163122b7 KT |
7141 | |
7142 | raw_spin_unlock(&busiest->lock); | |
7143 | ||
7144 | if (cur_ld_moved) { | |
7145 | attach_tasks(&env); | |
7146 | ld_moved += cur_ld_moved; | |
7147 | } | |
7148 | ||
1e3c88bd | 7149 | local_irq_restore(flags); |
88b8dac0 | 7150 | |
f1cd0858 JK |
7151 | if (env.flags & LBF_NEED_BREAK) { |
7152 | env.flags &= ~LBF_NEED_BREAK; | |
7153 | goto more_balance; | |
7154 | } | |
7155 | ||
88b8dac0 SV |
7156 | /* |
7157 | * Revisit (affine) tasks on src_cpu that couldn't be moved to | |
7158 | * us and move them to an alternate dst_cpu in our sched_group | |
7159 | * where they can run. The upper limit on how many times we | |
7160 | * iterate on same src_cpu is dependent on number of cpus in our | |
7161 | * sched_group. | |
7162 | * | |
7163 | * This changes load balance semantics a bit on who can move | |
7164 | * load to a given_cpu. In addition to the given_cpu itself | |
7165 | * (or a ilb_cpu acting on its behalf where given_cpu is | |
7166 | * nohz-idle), we now have balance_cpu in a position to move | |
7167 | * load to given_cpu. In rare situations, this may cause | |
7168 | * conflicts (balance_cpu and given_cpu/ilb_cpu deciding | |
7169 | * _independently_ and at _same_ time to move some load to | |
7170 | * given_cpu) causing exceess load to be moved to given_cpu. | |
7171 | * This however should not happen so much in practice and | |
7172 | * moreover subsequent load balance cycles should correct the | |
7173 | * excess load moved. | |
7174 | */ | |
6263322c | 7175 | if ((env.flags & LBF_DST_PINNED) && env.imbalance > 0) { |
88b8dac0 | 7176 | |
7aff2e3a VD |
7177 | /* Prevent to re-select dst_cpu via env's cpus */ |
7178 | cpumask_clear_cpu(env.dst_cpu, env.cpus); | |
7179 | ||
78feefc5 | 7180 | env.dst_rq = cpu_rq(env.new_dst_cpu); |
88b8dac0 | 7181 | env.dst_cpu = env.new_dst_cpu; |
6263322c | 7182 | env.flags &= ~LBF_DST_PINNED; |
88b8dac0 SV |
7183 | env.loop = 0; |
7184 | env.loop_break = sched_nr_migrate_break; | |
e02e60c1 | 7185 | |
88b8dac0 SV |
7186 | /* |
7187 | * Go back to "more_balance" rather than "redo" since we | |
7188 | * need to continue with same src_cpu. | |
7189 | */ | |
7190 | goto more_balance; | |
7191 | } | |
1e3c88bd | 7192 | |
6263322c PZ |
7193 | /* |
7194 | * We failed to reach balance because of affinity. | |
7195 | */ | |
7196 | if (sd_parent) { | |
63b2ca30 | 7197 | int *group_imbalance = &sd_parent->groups->sgc->imbalance; |
6263322c | 7198 | |
afdeee05 | 7199 | if ((env.flags & LBF_SOME_PINNED) && env.imbalance > 0) |
6263322c | 7200 | *group_imbalance = 1; |
6263322c PZ |
7201 | } |
7202 | ||
1e3c88bd | 7203 | /* All tasks on this runqueue were pinned by CPU affinity */ |
8e45cb54 | 7204 | if (unlikely(env.flags & LBF_ALL_PINNED)) { |
1e3c88bd | 7205 | cpumask_clear_cpu(cpu_of(busiest), cpus); |
bbf18b19 PN |
7206 | if (!cpumask_empty(cpus)) { |
7207 | env.loop = 0; | |
7208 | env.loop_break = sched_nr_migrate_break; | |
1e3c88bd | 7209 | goto redo; |
bbf18b19 | 7210 | } |
afdeee05 | 7211 | goto out_all_pinned; |
1e3c88bd PZ |
7212 | } |
7213 | } | |
7214 | ||
7215 | if (!ld_moved) { | |
7216 | schedstat_inc(sd, lb_failed[idle]); | |
58b26c4c VP |
7217 | /* |
7218 | * Increment the failure counter only on periodic balance. | |
7219 | * We do not want newidle balance, which can be very | |
7220 | * frequent, pollute the failure counter causing | |
7221 | * excessive cache_hot migrations and active balances. | |
7222 | */ | |
7223 | if (idle != CPU_NEWLY_IDLE) | |
7224 | sd->nr_balance_failed++; | |
1e3c88bd | 7225 | |
bd939f45 | 7226 | if (need_active_balance(&env)) { |
1e3c88bd PZ |
7227 | raw_spin_lock_irqsave(&busiest->lock, flags); |
7228 | ||
969c7921 TH |
7229 | /* don't kick the active_load_balance_cpu_stop, |
7230 | * if the curr task on busiest cpu can't be | |
7231 | * moved to this_cpu | |
1e3c88bd PZ |
7232 | */ |
7233 | if (!cpumask_test_cpu(this_cpu, | |
fa17b507 | 7234 | tsk_cpus_allowed(busiest->curr))) { |
1e3c88bd PZ |
7235 | raw_spin_unlock_irqrestore(&busiest->lock, |
7236 | flags); | |
8e45cb54 | 7237 | env.flags |= LBF_ALL_PINNED; |
1e3c88bd PZ |
7238 | goto out_one_pinned; |
7239 | } | |
7240 | ||
969c7921 TH |
7241 | /* |
7242 | * ->active_balance synchronizes accesses to | |
7243 | * ->active_balance_work. Once set, it's cleared | |
7244 | * only after active load balance is finished. | |
7245 | */ | |
1e3c88bd PZ |
7246 | if (!busiest->active_balance) { |
7247 | busiest->active_balance = 1; | |
7248 | busiest->push_cpu = this_cpu; | |
7249 | active_balance = 1; | |
7250 | } | |
7251 | raw_spin_unlock_irqrestore(&busiest->lock, flags); | |
969c7921 | 7252 | |
bd939f45 | 7253 | if (active_balance) { |
969c7921 TH |
7254 | stop_one_cpu_nowait(cpu_of(busiest), |
7255 | active_load_balance_cpu_stop, busiest, | |
7256 | &busiest->active_balance_work); | |
bd939f45 | 7257 | } |
1e3c88bd PZ |
7258 | |
7259 | /* | |
7260 | * We've kicked active balancing, reset the failure | |
7261 | * counter. | |
7262 | */ | |
7263 | sd->nr_balance_failed = sd->cache_nice_tries+1; | |
7264 | } | |
7265 | } else | |
7266 | sd->nr_balance_failed = 0; | |
7267 | ||
7268 | if (likely(!active_balance)) { | |
7269 | /* We were unbalanced, so reset the balancing interval */ | |
7270 | sd->balance_interval = sd->min_interval; | |
7271 | } else { | |
7272 | /* | |
7273 | * If we've begun active balancing, start to back off. This | |
7274 | * case may not be covered by the all_pinned logic if there | |
7275 | * is only 1 task on the busy runqueue (because we don't call | |
163122b7 | 7276 | * detach_tasks). |
1e3c88bd PZ |
7277 | */ |
7278 | if (sd->balance_interval < sd->max_interval) | |
7279 | sd->balance_interval *= 2; | |
7280 | } | |
7281 | ||
1e3c88bd PZ |
7282 | goto out; |
7283 | ||
7284 | out_balanced: | |
afdeee05 VG |
7285 | /* |
7286 | * We reach balance although we may have faced some affinity | |
7287 | * constraints. Clear the imbalance flag if it was set. | |
7288 | */ | |
7289 | if (sd_parent) { | |
7290 | int *group_imbalance = &sd_parent->groups->sgc->imbalance; | |
7291 | ||
7292 | if (*group_imbalance) | |
7293 | *group_imbalance = 0; | |
7294 | } | |
7295 | ||
7296 | out_all_pinned: | |
7297 | /* | |
7298 | * We reach balance because all tasks are pinned at this level so | |
7299 | * we can't migrate them. Let the imbalance flag set so parent level | |
7300 | * can try to migrate them. | |
7301 | */ | |
1e3c88bd PZ |
7302 | schedstat_inc(sd, lb_balanced[idle]); |
7303 | ||
7304 | sd->nr_balance_failed = 0; | |
7305 | ||
7306 | out_one_pinned: | |
7307 | /* tune up the balancing interval */ | |
8e45cb54 | 7308 | if (((env.flags & LBF_ALL_PINNED) && |
5b54b56b | 7309 | sd->balance_interval < MAX_PINNED_INTERVAL) || |
1e3c88bd PZ |
7310 | (sd->balance_interval < sd->max_interval)) |
7311 | sd->balance_interval *= 2; | |
7312 | ||
46e49b38 | 7313 | ld_moved = 0; |
1e3c88bd | 7314 | out: |
1e3c88bd PZ |
7315 | return ld_moved; |
7316 | } | |
7317 | ||
52a08ef1 JL |
7318 | static inline unsigned long |
7319 | get_sd_balance_interval(struct sched_domain *sd, int cpu_busy) | |
7320 | { | |
7321 | unsigned long interval = sd->balance_interval; | |
7322 | ||
7323 | if (cpu_busy) | |
7324 | interval *= sd->busy_factor; | |
7325 | ||
7326 | /* scale ms to jiffies */ | |
7327 | interval = msecs_to_jiffies(interval); | |
7328 | interval = clamp(interval, 1UL, max_load_balance_interval); | |
7329 | ||
7330 | return interval; | |
7331 | } | |
7332 | ||
7333 | static inline void | |
7334 | update_next_balance(struct sched_domain *sd, int cpu_busy, unsigned long *next_balance) | |
7335 | { | |
7336 | unsigned long interval, next; | |
7337 | ||
7338 | interval = get_sd_balance_interval(sd, cpu_busy); | |
7339 | next = sd->last_balance + interval; | |
7340 | ||
7341 | if (time_after(*next_balance, next)) | |
7342 | *next_balance = next; | |
7343 | } | |
7344 | ||
1e3c88bd PZ |
7345 | /* |
7346 | * idle_balance is called by schedule() if this_cpu is about to become | |
7347 | * idle. Attempts to pull tasks from other CPUs. | |
7348 | */ | |
6e83125c | 7349 | static int idle_balance(struct rq *this_rq) |
1e3c88bd | 7350 | { |
52a08ef1 JL |
7351 | unsigned long next_balance = jiffies + HZ; |
7352 | int this_cpu = this_rq->cpu; | |
1e3c88bd PZ |
7353 | struct sched_domain *sd; |
7354 | int pulled_task = 0; | |
9bd721c5 | 7355 | u64 curr_cost = 0; |
1e3c88bd | 7356 | |
6e83125c PZ |
7357 | /* |
7358 | * We must set idle_stamp _before_ calling idle_balance(), such that we | |
7359 | * measure the duration of idle_balance() as idle time. | |
7360 | */ | |
7361 | this_rq->idle_stamp = rq_clock(this_rq); | |
7362 | ||
4486edd1 TC |
7363 | if (this_rq->avg_idle < sysctl_sched_migration_cost || |
7364 | !this_rq->rd->overload) { | |
52a08ef1 JL |
7365 | rcu_read_lock(); |
7366 | sd = rcu_dereference_check_sched_domain(this_rq->sd); | |
7367 | if (sd) | |
7368 | update_next_balance(sd, 0, &next_balance); | |
7369 | rcu_read_unlock(); | |
7370 | ||
6e83125c | 7371 | goto out; |
52a08ef1 | 7372 | } |
1e3c88bd | 7373 | |
f492e12e PZ |
7374 | raw_spin_unlock(&this_rq->lock); |
7375 | ||
48a16753 | 7376 | update_blocked_averages(this_cpu); |
dce840a0 | 7377 | rcu_read_lock(); |
1e3c88bd | 7378 | for_each_domain(this_cpu, sd) { |
23f0d209 | 7379 | int continue_balancing = 1; |
9bd721c5 | 7380 | u64 t0, domain_cost; |
1e3c88bd PZ |
7381 | |
7382 | if (!(sd->flags & SD_LOAD_BALANCE)) | |
7383 | continue; | |
7384 | ||
52a08ef1 JL |
7385 | if (this_rq->avg_idle < curr_cost + sd->max_newidle_lb_cost) { |
7386 | update_next_balance(sd, 0, &next_balance); | |
9bd721c5 | 7387 | break; |
52a08ef1 | 7388 | } |
9bd721c5 | 7389 | |
f492e12e | 7390 | if (sd->flags & SD_BALANCE_NEWIDLE) { |
9bd721c5 JL |
7391 | t0 = sched_clock_cpu(this_cpu); |
7392 | ||
f492e12e | 7393 | pulled_task = load_balance(this_cpu, this_rq, |
23f0d209 JK |
7394 | sd, CPU_NEWLY_IDLE, |
7395 | &continue_balancing); | |
9bd721c5 JL |
7396 | |
7397 | domain_cost = sched_clock_cpu(this_cpu) - t0; | |
7398 | if (domain_cost > sd->max_newidle_lb_cost) | |
7399 | sd->max_newidle_lb_cost = domain_cost; | |
7400 | ||
7401 | curr_cost += domain_cost; | |
f492e12e | 7402 | } |
1e3c88bd | 7403 | |
52a08ef1 | 7404 | update_next_balance(sd, 0, &next_balance); |
39a4d9ca JL |
7405 | |
7406 | /* | |
7407 | * Stop searching for tasks to pull if there are | |
7408 | * now runnable tasks on this rq. | |
7409 | */ | |
7410 | if (pulled_task || this_rq->nr_running > 0) | |
1e3c88bd | 7411 | break; |
1e3c88bd | 7412 | } |
dce840a0 | 7413 | rcu_read_unlock(); |
f492e12e PZ |
7414 | |
7415 | raw_spin_lock(&this_rq->lock); | |
7416 | ||
0e5b5337 JL |
7417 | if (curr_cost > this_rq->max_idle_balance_cost) |
7418 | this_rq->max_idle_balance_cost = curr_cost; | |
7419 | ||
e5fc6611 | 7420 | /* |
0e5b5337 JL |
7421 | * While browsing the domains, we released the rq lock, a task could |
7422 | * have been enqueued in the meantime. Since we're not going idle, | |
7423 | * pretend we pulled a task. | |
e5fc6611 | 7424 | */ |
0e5b5337 | 7425 | if (this_rq->cfs.h_nr_running && !pulled_task) |
6e83125c | 7426 | pulled_task = 1; |
e5fc6611 | 7427 | |
52a08ef1 JL |
7428 | out: |
7429 | /* Move the next balance forward */ | |
7430 | if (time_after(this_rq->next_balance, next_balance)) | |
1e3c88bd | 7431 | this_rq->next_balance = next_balance; |
9bd721c5 | 7432 | |
e4aa358b | 7433 | /* Is there a task of a high priority class? */ |
46383648 | 7434 | if (this_rq->nr_running != this_rq->cfs.h_nr_running) |
e4aa358b KT |
7435 | pulled_task = -1; |
7436 | ||
38c6ade2 | 7437 | if (pulled_task) |
6e83125c PZ |
7438 | this_rq->idle_stamp = 0; |
7439 | ||
3c4017c1 | 7440 | return pulled_task; |
1e3c88bd PZ |
7441 | } |
7442 | ||
7443 | /* | |
969c7921 TH |
7444 | * active_load_balance_cpu_stop is run by cpu stopper. It pushes |
7445 | * running tasks off the busiest CPU onto idle CPUs. It requires at | |
7446 | * least 1 task to be running on each physical CPU where possible, and | |
7447 | * avoids physical / logical imbalances. | |
1e3c88bd | 7448 | */ |
969c7921 | 7449 | static int active_load_balance_cpu_stop(void *data) |
1e3c88bd | 7450 | { |
969c7921 TH |
7451 | struct rq *busiest_rq = data; |
7452 | int busiest_cpu = cpu_of(busiest_rq); | |
1e3c88bd | 7453 | int target_cpu = busiest_rq->push_cpu; |
969c7921 | 7454 | struct rq *target_rq = cpu_rq(target_cpu); |
1e3c88bd | 7455 | struct sched_domain *sd; |
e5673f28 | 7456 | struct task_struct *p = NULL; |
969c7921 TH |
7457 | |
7458 | raw_spin_lock_irq(&busiest_rq->lock); | |
7459 | ||
7460 | /* make sure the requested cpu hasn't gone down in the meantime */ | |
7461 | if (unlikely(busiest_cpu != smp_processor_id() || | |
7462 | !busiest_rq->active_balance)) | |
7463 | goto out_unlock; | |
1e3c88bd PZ |
7464 | |
7465 | /* Is there any task to move? */ | |
7466 | if (busiest_rq->nr_running <= 1) | |
969c7921 | 7467 | goto out_unlock; |
1e3c88bd PZ |
7468 | |
7469 | /* | |
7470 | * This condition is "impossible", if it occurs | |
7471 | * we need to fix it. Originally reported by | |
7472 | * Bjorn Helgaas on a 128-cpu setup. | |
7473 | */ | |
7474 | BUG_ON(busiest_rq == target_rq); | |
7475 | ||
1e3c88bd | 7476 | /* Search for an sd spanning us and the target CPU. */ |
dce840a0 | 7477 | rcu_read_lock(); |
1e3c88bd PZ |
7478 | for_each_domain(target_cpu, sd) { |
7479 | if ((sd->flags & SD_LOAD_BALANCE) && | |
7480 | cpumask_test_cpu(busiest_cpu, sched_domain_span(sd))) | |
7481 | break; | |
7482 | } | |
7483 | ||
7484 | if (likely(sd)) { | |
8e45cb54 PZ |
7485 | struct lb_env env = { |
7486 | .sd = sd, | |
ddcdf6e7 PZ |
7487 | .dst_cpu = target_cpu, |
7488 | .dst_rq = target_rq, | |
7489 | .src_cpu = busiest_rq->cpu, | |
7490 | .src_rq = busiest_rq, | |
8e45cb54 PZ |
7491 | .idle = CPU_IDLE, |
7492 | }; | |
7493 | ||
1e3c88bd PZ |
7494 | schedstat_inc(sd, alb_count); |
7495 | ||
e5673f28 KT |
7496 | p = detach_one_task(&env); |
7497 | if (p) | |
1e3c88bd PZ |
7498 | schedstat_inc(sd, alb_pushed); |
7499 | else | |
7500 | schedstat_inc(sd, alb_failed); | |
7501 | } | |
dce840a0 | 7502 | rcu_read_unlock(); |
969c7921 TH |
7503 | out_unlock: |
7504 | busiest_rq->active_balance = 0; | |
e5673f28 KT |
7505 | raw_spin_unlock(&busiest_rq->lock); |
7506 | ||
7507 | if (p) | |
7508 | attach_one_task(target_rq, p); | |
7509 | ||
7510 | local_irq_enable(); | |
7511 | ||
969c7921 | 7512 | return 0; |
1e3c88bd PZ |
7513 | } |
7514 | ||
d987fc7f MG |
7515 | static inline int on_null_domain(struct rq *rq) |
7516 | { | |
7517 | return unlikely(!rcu_dereference_sched(rq->sd)); | |
7518 | } | |
7519 | ||
3451d024 | 7520 | #ifdef CONFIG_NO_HZ_COMMON |
83cd4fe2 VP |
7521 | /* |
7522 | * idle load balancing details | |
83cd4fe2 VP |
7523 | * - When one of the busy CPUs notice that there may be an idle rebalancing |
7524 | * needed, they will kick the idle load balancer, which then does idle | |
7525 | * load balancing for all the idle CPUs. | |
7526 | */ | |
1e3c88bd | 7527 | static struct { |
83cd4fe2 | 7528 | cpumask_var_t idle_cpus_mask; |
0b005cf5 | 7529 | atomic_t nr_cpus; |
83cd4fe2 VP |
7530 | unsigned long next_balance; /* in jiffy units */ |
7531 | } nohz ____cacheline_aligned; | |
1e3c88bd | 7532 | |
3dd0337d | 7533 | static inline int find_new_ilb(void) |
1e3c88bd | 7534 | { |
0b005cf5 | 7535 | int ilb = cpumask_first(nohz.idle_cpus_mask); |
1e3c88bd | 7536 | |
786d6dc7 SS |
7537 | if (ilb < nr_cpu_ids && idle_cpu(ilb)) |
7538 | return ilb; | |
7539 | ||
7540 | return nr_cpu_ids; | |
1e3c88bd | 7541 | } |
1e3c88bd | 7542 | |
83cd4fe2 VP |
7543 | /* |
7544 | * Kick a CPU to do the nohz balancing, if it is time for it. We pick the | |
7545 | * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle | |
7546 | * CPU (if there is one). | |
7547 | */ | |
0aeeeeba | 7548 | static void nohz_balancer_kick(void) |
83cd4fe2 VP |
7549 | { |
7550 | int ilb_cpu; | |
7551 | ||
7552 | nohz.next_balance++; | |
7553 | ||
3dd0337d | 7554 | ilb_cpu = find_new_ilb(); |
83cd4fe2 | 7555 | |
0b005cf5 SS |
7556 | if (ilb_cpu >= nr_cpu_ids) |
7557 | return; | |
83cd4fe2 | 7558 | |
cd490c5b | 7559 | if (test_and_set_bit(NOHZ_BALANCE_KICK, nohz_flags(ilb_cpu))) |
1c792db7 SS |
7560 | return; |
7561 | /* | |
7562 | * Use smp_send_reschedule() instead of resched_cpu(). | |
7563 | * This way we generate a sched IPI on the target cpu which | |
7564 | * is idle. And the softirq performing nohz idle load balance | |
7565 | * will be run before returning from the IPI. | |
7566 | */ | |
7567 | smp_send_reschedule(ilb_cpu); | |
83cd4fe2 VP |
7568 | return; |
7569 | } | |
7570 | ||
c1cc017c | 7571 | static inline void nohz_balance_exit_idle(int cpu) |
71325960 SS |
7572 | { |
7573 | if (unlikely(test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))) { | |
d987fc7f MG |
7574 | /* |
7575 | * Completely isolated CPUs don't ever set, so we must test. | |
7576 | */ | |
7577 | if (likely(cpumask_test_cpu(cpu, nohz.idle_cpus_mask))) { | |
7578 | cpumask_clear_cpu(cpu, nohz.idle_cpus_mask); | |
7579 | atomic_dec(&nohz.nr_cpus); | |
7580 | } | |
71325960 SS |
7581 | clear_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)); |
7582 | } | |
7583 | } | |
7584 | ||
69e1e811 SS |
7585 | static inline void set_cpu_sd_state_busy(void) |
7586 | { | |
7587 | struct sched_domain *sd; | |
37dc6b50 | 7588 | int cpu = smp_processor_id(); |
69e1e811 | 7589 | |
69e1e811 | 7590 | rcu_read_lock(); |
37dc6b50 | 7591 | sd = rcu_dereference(per_cpu(sd_busy, cpu)); |
25f55d9d VG |
7592 | |
7593 | if (!sd || !sd->nohz_idle) | |
7594 | goto unlock; | |
7595 | sd->nohz_idle = 0; | |
7596 | ||
63b2ca30 | 7597 | atomic_inc(&sd->groups->sgc->nr_busy_cpus); |
25f55d9d | 7598 | unlock: |
69e1e811 SS |
7599 | rcu_read_unlock(); |
7600 | } | |
7601 | ||
7602 | void set_cpu_sd_state_idle(void) | |
7603 | { | |
7604 | struct sched_domain *sd; | |
37dc6b50 | 7605 | int cpu = smp_processor_id(); |
69e1e811 | 7606 | |
69e1e811 | 7607 | rcu_read_lock(); |
37dc6b50 | 7608 | sd = rcu_dereference(per_cpu(sd_busy, cpu)); |
25f55d9d VG |
7609 | |
7610 | if (!sd || sd->nohz_idle) | |
7611 | goto unlock; | |
7612 | sd->nohz_idle = 1; | |
7613 | ||
63b2ca30 | 7614 | atomic_dec(&sd->groups->sgc->nr_busy_cpus); |
25f55d9d | 7615 | unlock: |
69e1e811 SS |
7616 | rcu_read_unlock(); |
7617 | } | |
7618 | ||
1e3c88bd | 7619 | /* |
c1cc017c | 7620 | * This routine will record that the cpu is going idle with tick stopped. |
0b005cf5 | 7621 | * This info will be used in performing idle load balancing in the future. |
1e3c88bd | 7622 | */ |
c1cc017c | 7623 | void nohz_balance_enter_idle(int cpu) |
1e3c88bd | 7624 | { |
71325960 SS |
7625 | /* |
7626 | * If this cpu is going down, then nothing needs to be done. | |
7627 | */ | |
7628 | if (!cpu_active(cpu)) | |
7629 | return; | |
7630 | ||
c1cc017c AS |
7631 | if (test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu))) |
7632 | return; | |
1e3c88bd | 7633 | |
d987fc7f MG |
7634 | /* |
7635 | * If we're a completely isolated CPU, we don't play. | |
7636 | */ | |
7637 | if (on_null_domain(cpu_rq(cpu))) | |
7638 | return; | |
7639 | ||
c1cc017c AS |
7640 | cpumask_set_cpu(cpu, nohz.idle_cpus_mask); |
7641 | atomic_inc(&nohz.nr_cpus); | |
7642 | set_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)); | |
1e3c88bd | 7643 | } |
71325960 | 7644 | |
0db0628d | 7645 | static int sched_ilb_notifier(struct notifier_block *nfb, |
71325960 SS |
7646 | unsigned long action, void *hcpu) |
7647 | { | |
7648 | switch (action & ~CPU_TASKS_FROZEN) { | |
7649 | case CPU_DYING: | |
c1cc017c | 7650 | nohz_balance_exit_idle(smp_processor_id()); |
71325960 SS |
7651 | return NOTIFY_OK; |
7652 | default: | |
7653 | return NOTIFY_DONE; | |
7654 | } | |
7655 | } | |
1e3c88bd PZ |
7656 | #endif |
7657 | ||
7658 | static DEFINE_SPINLOCK(balancing); | |
7659 | ||
49c022e6 PZ |
7660 | /* |
7661 | * Scale the max load_balance interval with the number of CPUs in the system. | |
7662 | * This trades load-balance latency on larger machines for less cross talk. | |
7663 | */ | |
029632fb | 7664 | void update_max_interval(void) |
49c022e6 PZ |
7665 | { |
7666 | max_load_balance_interval = HZ*num_online_cpus()/10; | |
7667 | } | |
7668 | ||
1e3c88bd PZ |
7669 | /* |
7670 | * It checks each scheduling domain to see if it is due to be balanced, | |
7671 | * and initiates a balancing operation if so. | |
7672 | * | |
b9b0853a | 7673 | * Balancing parameters are set up in init_sched_domains. |
1e3c88bd | 7674 | */ |
f7ed0a89 | 7675 | static void rebalance_domains(struct rq *rq, enum cpu_idle_type idle) |
1e3c88bd | 7676 | { |
23f0d209 | 7677 | int continue_balancing = 1; |
f7ed0a89 | 7678 | int cpu = rq->cpu; |
1e3c88bd | 7679 | unsigned long interval; |
04f733b4 | 7680 | struct sched_domain *sd; |
1e3c88bd PZ |
7681 | /* Earliest time when we have to do rebalance again */ |
7682 | unsigned long next_balance = jiffies + 60*HZ; | |
7683 | int update_next_balance = 0; | |
f48627e6 JL |
7684 | int need_serialize, need_decay = 0; |
7685 | u64 max_cost = 0; | |
1e3c88bd | 7686 | |
48a16753 | 7687 | update_blocked_averages(cpu); |
2069dd75 | 7688 | |
dce840a0 | 7689 | rcu_read_lock(); |
1e3c88bd | 7690 | for_each_domain(cpu, sd) { |
f48627e6 JL |
7691 | /* |
7692 | * Decay the newidle max times here because this is a regular | |
7693 | * visit to all the domains. Decay ~1% per second. | |
7694 | */ | |
7695 | if (time_after(jiffies, sd->next_decay_max_lb_cost)) { | |
7696 | sd->max_newidle_lb_cost = | |
7697 | (sd->max_newidle_lb_cost * 253) / 256; | |
7698 | sd->next_decay_max_lb_cost = jiffies + HZ; | |
7699 | need_decay = 1; | |
7700 | } | |
7701 | max_cost += sd->max_newidle_lb_cost; | |
7702 | ||
1e3c88bd PZ |
7703 | if (!(sd->flags & SD_LOAD_BALANCE)) |
7704 | continue; | |
7705 | ||
f48627e6 JL |
7706 | /* |
7707 | * Stop the load balance at this level. There is another | |
7708 | * CPU in our sched group which is doing load balancing more | |
7709 | * actively. | |
7710 | */ | |
7711 | if (!continue_balancing) { | |
7712 | if (need_decay) | |
7713 | continue; | |
7714 | break; | |
7715 | } | |
7716 | ||
52a08ef1 | 7717 | interval = get_sd_balance_interval(sd, idle != CPU_IDLE); |
1e3c88bd PZ |
7718 | |
7719 | need_serialize = sd->flags & SD_SERIALIZE; | |
1e3c88bd PZ |
7720 | if (need_serialize) { |
7721 | if (!spin_trylock(&balancing)) | |
7722 | goto out; | |
7723 | } | |
7724 | ||
7725 | if (time_after_eq(jiffies, sd->last_balance + interval)) { | |
23f0d209 | 7726 | if (load_balance(cpu, rq, sd, idle, &continue_balancing)) { |
1e3c88bd | 7727 | /* |
6263322c | 7728 | * The LBF_DST_PINNED logic could have changed |
de5eb2dd JK |
7729 | * env->dst_cpu, so we can't know our idle |
7730 | * state even if we migrated tasks. Update it. | |
1e3c88bd | 7731 | */ |
de5eb2dd | 7732 | idle = idle_cpu(cpu) ? CPU_IDLE : CPU_NOT_IDLE; |
1e3c88bd PZ |
7733 | } |
7734 | sd->last_balance = jiffies; | |
52a08ef1 | 7735 | interval = get_sd_balance_interval(sd, idle != CPU_IDLE); |
1e3c88bd PZ |
7736 | } |
7737 | if (need_serialize) | |
7738 | spin_unlock(&balancing); | |
7739 | out: | |
7740 | if (time_after(next_balance, sd->last_balance + interval)) { | |
7741 | next_balance = sd->last_balance + interval; | |
7742 | update_next_balance = 1; | |
7743 | } | |
f48627e6 JL |
7744 | } |
7745 | if (need_decay) { | |
1e3c88bd | 7746 | /* |
f48627e6 JL |
7747 | * Ensure the rq-wide value also decays but keep it at a |
7748 | * reasonable floor to avoid funnies with rq->avg_idle. | |
1e3c88bd | 7749 | */ |
f48627e6 JL |
7750 | rq->max_idle_balance_cost = |
7751 | max((u64)sysctl_sched_migration_cost, max_cost); | |
1e3c88bd | 7752 | } |
dce840a0 | 7753 | rcu_read_unlock(); |
1e3c88bd PZ |
7754 | |
7755 | /* | |
7756 | * next_balance will be updated only when there is a need. | |
7757 | * When the cpu is attached to null domain for ex, it will not be | |
7758 | * updated. | |
7759 | */ | |
c5afb6a8 | 7760 | if (likely(update_next_balance)) { |
1e3c88bd | 7761 | rq->next_balance = next_balance; |
c5afb6a8 VG |
7762 | |
7763 | #ifdef CONFIG_NO_HZ_COMMON | |
7764 | /* | |
7765 | * If this CPU has been elected to perform the nohz idle | |
7766 | * balance. Other idle CPUs have already rebalanced with | |
7767 | * nohz_idle_balance() and nohz.next_balance has been | |
7768 | * updated accordingly. This CPU is now running the idle load | |
7769 | * balance for itself and we need to update the | |
7770 | * nohz.next_balance accordingly. | |
7771 | */ | |
7772 | if ((idle == CPU_IDLE) && time_after(nohz.next_balance, rq->next_balance)) | |
7773 | nohz.next_balance = rq->next_balance; | |
7774 | #endif | |
7775 | } | |
1e3c88bd PZ |
7776 | } |
7777 | ||
3451d024 | 7778 | #ifdef CONFIG_NO_HZ_COMMON |
1e3c88bd | 7779 | /* |
3451d024 | 7780 | * In CONFIG_NO_HZ_COMMON case, the idle balance kickee will do the |
1e3c88bd PZ |
7781 | * rebalancing for all the cpus for whom scheduler ticks are stopped. |
7782 | */ | |
208cb16b | 7783 | static void nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle) |
83cd4fe2 | 7784 | { |
208cb16b | 7785 | int this_cpu = this_rq->cpu; |
83cd4fe2 VP |
7786 | struct rq *rq; |
7787 | int balance_cpu; | |
c5afb6a8 VG |
7788 | /* Earliest time when we have to do rebalance again */ |
7789 | unsigned long next_balance = jiffies + 60*HZ; | |
7790 | int update_next_balance = 0; | |
83cd4fe2 | 7791 | |
1c792db7 SS |
7792 | if (idle != CPU_IDLE || |
7793 | !test_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu))) | |
7794 | goto end; | |
83cd4fe2 VP |
7795 | |
7796 | for_each_cpu(balance_cpu, nohz.idle_cpus_mask) { | |
8a6d42d1 | 7797 | if (balance_cpu == this_cpu || !idle_cpu(balance_cpu)) |
83cd4fe2 VP |
7798 | continue; |
7799 | ||
7800 | /* | |
7801 | * If this cpu gets work to do, stop the load balancing | |
7802 | * work being done for other cpus. Next load | |
7803 | * balancing owner will pick it up. | |
7804 | */ | |
1c792db7 | 7805 | if (need_resched()) |
83cd4fe2 | 7806 | break; |
83cd4fe2 | 7807 | |
5ed4f1d9 VG |
7808 | rq = cpu_rq(balance_cpu); |
7809 | ||
ed61bbc6 TC |
7810 | /* |
7811 | * If time for next balance is due, | |
7812 | * do the balance. | |
7813 | */ | |
7814 | if (time_after_eq(jiffies, rq->next_balance)) { | |
7815 | raw_spin_lock_irq(&rq->lock); | |
7816 | update_rq_clock(rq); | |
7817 | update_idle_cpu_load(rq); | |
7818 | raw_spin_unlock_irq(&rq->lock); | |
7819 | rebalance_domains(rq, CPU_IDLE); | |
7820 | } | |
83cd4fe2 | 7821 | |
c5afb6a8 VG |
7822 | if (time_after(next_balance, rq->next_balance)) { |
7823 | next_balance = rq->next_balance; | |
7824 | update_next_balance = 1; | |
7825 | } | |
83cd4fe2 | 7826 | } |
c5afb6a8 VG |
7827 | |
7828 | /* | |
7829 | * next_balance will be updated only when there is a need. | |
7830 | * When the CPU is attached to null domain for ex, it will not be | |
7831 | * updated. | |
7832 | */ | |
7833 | if (likely(update_next_balance)) | |
7834 | nohz.next_balance = next_balance; | |
1c792db7 SS |
7835 | end: |
7836 | clear_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu)); | |
83cd4fe2 VP |
7837 | } |
7838 | ||
7839 | /* | |
0b005cf5 | 7840 | * Current heuristic for kicking the idle load balancer in the presence |
1aaf90a4 | 7841 | * of an idle cpu in the system. |
0b005cf5 | 7842 | * - This rq has more than one task. |
1aaf90a4 VG |
7843 | * - This rq has at least one CFS task and the capacity of the CPU is |
7844 | * significantly reduced because of RT tasks or IRQs. | |
7845 | * - At parent of LLC scheduler domain level, this cpu's scheduler group has | |
7846 | * multiple busy cpu. | |
0b005cf5 SS |
7847 | * - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler |
7848 | * domain span are idle. | |
83cd4fe2 | 7849 | */ |
1aaf90a4 | 7850 | static inline bool nohz_kick_needed(struct rq *rq) |
83cd4fe2 VP |
7851 | { |
7852 | unsigned long now = jiffies; | |
0b005cf5 | 7853 | struct sched_domain *sd; |
63b2ca30 | 7854 | struct sched_group_capacity *sgc; |
4a725627 | 7855 | int nr_busy, cpu = rq->cpu; |
1aaf90a4 | 7856 | bool kick = false; |
83cd4fe2 | 7857 | |
4a725627 | 7858 | if (unlikely(rq->idle_balance)) |
1aaf90a4 | 7859 | return false; |
83cd4fe2 | 7860 | |
1c792db7 SS |
7861 | /* |
7862 | * We may be recently in ticked or tickless idle mode. At the first | |
7863 | * busy tick after returning from idle, we will update the busy stats. | |
7864 | */ | |
69e1e811 | 7865 | set_cpu_sd_state_busy(); |
c1cc017c | 7866 | nohz_balance_exit_idle(cpu); |
0b005cf5 SS |
7867 | |
7868 | /* | |
7869 | * None are in tickless mode and hence no need for NOHZ idle load | |
7870 | * balancing. | |
7871 | */ | |
7872 | if (likely(!atomic_read(&nohz.nr_cpus))) | |
1aaf90a4 | 7873 | return false; |
1c792db7 SS |
7874 | |
7875 | if (time_before(now, nohz.next_balance)) | |
1aaf90a4 | 7876 | return false; |
83cd4fe2 | 7877 | |
0b005cf5 | 7878 | if (rq->nr_running >= 2) |
1aaf90a4 | 7879 | return true; |
83cd4fe2 | 7880 | |
067491b7 | 7881 | rcu_read_lock(); |
37dc6b50 | 7882 | sd = rcu_dereference(per_cpu(sd_busy, cpu)); |
37dc6b50 | 7883 | if (sd) { |
63b2ca30 NP |
7884 | sgc = sd->groups->sgc; |
7885 | nr_busy = atomic_read(&sgc->nr_busy_cpus); | |
0b005cf5 | 7886 | |
1aaf90a4 VG |
7887 | if (nr_busy > 1) { |
7888 | kick = true; | |
7889 | goto unlock; | |
7890 | } | |
7891 | ||
83cd4fe2 | 7892 | } |
37dc6b50 | 7893 | |
1aaf90a4 VG |
7894 | sd = rcu_dereference(rq->sd); |
7895 | if (sd) { | |
7896 | if ((rq->cfs.h_nr_running >= 1) && | |
7897 | check_cpu_capacity(rq, sd)) { | |
7898 | kick = true; | |
7899 | goto unlock; | |
7900 | } | |
7901 | } | |
37dc6b50 | 7902 | |
1aaf90a4 | 7903 | sd = rcu_dereference(per_cpu(sd_asym, cpu)); |
37dc6b50 | 7904 | if (sd && (cpumask_first_and(nohz.idle_cpus_mask, |
1aaf90a4 VG |
7905 | sched_domain_span(sd)) < cpu)) { |
7906 | kick = true; | |
7907 | goto unlock; | |
7908 | } | |
067491b7 | 7909 | |
1aaf90a4 | 7910 | unlock: |
067491b7 | 7911 | rcu_read_unlock(); |
1aaf90a4 | 7912 | return kick; |
83cd4fe2 VP |
7913 | } |
7914 | #else | |
208cb16b | 7915 | static void nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle) { } |
83cd4fe2 VP |
7916 | #endif |
7917 | ||
7918 | /* | |
7919 | * run_rebalance_domains is triggered when needed from the scheduler tick. | |
7920 | * Also triggered for nohz idle balancing (with nohz_balancing_kick set). | |
7921 | */ | |
1e3c88bd PZ |
7922 | static void run_rebalance_domains(struct softirq_action *h) |
7923 | { | |
208cb16b | 7924 | struct rq *this_rq = this_rq(); |
6eb57e0d | 7925 | enum cpu_idle_type idle = this_rq->idle_balance ? |
1e3c88bd PZ |
7926 | CPU_IDLE : CPU_NOT_IDLE; |
7927 | ||
1e3c88bd | 7928 | /* |
83cd4fe2 | 7929 | * If this cpu has a pending nohz_balance_kick, then do the |
1e3c88bd | 7930 | * balancing on behalf of the other idle cpus whose ticks are |
d4573c3e PM |
7931 | * stopped. Do nohz_idle_balance *before* rebalance_domains to |
7932 | * give the idle cpus a chance to load balance. Else we may | |
7933 | * load balance only within the local sched_domain hierarchy | |
7934 | * and abort nohz_idle_balance altogether if we pull some load. | |
1e3c88bd | 7935 | */ |
208cb16b | 7936 | nohz_idle_balance(this_rq, idle); |
d4573c3e | 7937 | rebalance_domains(this_rq, idle); |
1e3c88bd PZ |
7938 | } |
7939 | ||
1e3c88bd PZ |
7940 | /* |
7941 | * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing. | |
1e3c88bd | 7942 | */ |
7caff66f | 7943 | void trigger_load_balance(struct rq *rq) |
1e3c88bd | 7944 | { |
1e3c88bd | 7945 | /* Don't need to rebalance while attached to NULL domain */ |
c726099e DL |
7946 | if (unlikely(on_null_domain(rq))) |
7947 | return; | |
7948 | ||
7949 | if (time_after_eq(jiffies, rq->next_balance)) | |
1e3c88bd | 7950 | raise_softirq(SCHED_SOFTIRQ); |
3451d024 | 7951 | #ifdef CONFIG_NO_HZ_COMMON |
c726099e | 7952 | if (nohz_kick_needed(rq)) |
0aeeeeba | 7953 | nohz_balancer_kick(); |
83cd4fe2 | 7954 | #endif |
1e3c88bd PZ |
7955 | } |
7956 | ||
0bcdcf28 CE |
7957 | static void rq_online_fair(struct rq *rq) |
7958 | { | |
7959 | update_sysctl(); | |
0e59bdae KT |
7960 | |
7961 | update_runtime_enabled(rq); | |
0bcdcf28 CE |
7962 | } |
7963 | ||
7964 | static void rq_offline_fair(struct rq *rq) | |
7965 | { | |
7966 | update_sysctl(); | |
a4c96ae3 PB |
7967 | |
7968 | /* Ensure any throttled groups are reachable by pick_next_task */ | |
7969 | unthrottle_offline_cfs_rqs(rq); | |
0bcdcf28 CE |
7970 | } |
7971 | ||
55e12e5e | 7972 | #endif /* CONFIG_SMP */ |
e1d1484f | 7973 | |
bf0f6f24 IM |
7974 | /* |
7975 | * scheduler tick hitting a task of our scheduling class: | |
7976 | */ | |
8f4d37ec | 7977 | static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued) |
bf0f6f24 IM |
7978 | { |
7979 | struct cfs_rq *cfs_rq; | |
7980 | struct sched_entity *se = &curr->se; | |
7981 | ||
7982 | for_each_sched_entity(se) { | |
7983 | cfs_rq = cfs_rq_of(se); | |
8f4d37ec | 7984 | entity_tick(cfs_rq, se, queued); |
bf0f6f24 | 7985 | } |
18bf2805 | 7986 | |
b52da86e | 7987 | if (static_branch_unlikely(&sched_numa_balancing)) |
cbee9f88 | 7988 | task_tick_numa(rq, curr); |
bf0f6f24 IM |
7989 | } |
7990 | ||
7991 | /* | |
cd29fe6f PZ |
7992 | * called on fork with the child task as argument from the parent's context |
7993 | * - child not yet on the tasklist | |
7994 | * - preemption disabled | |
bf0f6f24 | 7995 | */ |
cd29fe6f | 7996 | static void task_fork_fair(struct task_struct *p) |
bf0f6f24 | 7997 | { |
4fc420c9 DN |
7998 | struct cfs_rq *cfs_rq; |
7999 | struct sched_entity *se = &p->se, *curr; | |
00bf7bfc | 8000 | int this_cpu = smp_processor_id(); |
cd29fe6f PZ |
8001 | struct rq *rq = this_rq(); |
8002 | unsigned long flags; | |
8003 | ||
05fa785c | 8004 | raw_spin_lock_irqsave(&rq->lock, flags); |
bf0f6f24 | 8005 | |
861d034e PZ |
8006 | update_rq_clock(rq); |
8007 | ||
4fc420c9 DN |
8008 | cfs_rq = task_cfs_rq(current); |
8009 | curr = cfs_rq->curr; | |
8010 | ||
6c9a27f5 DN |
8011 | /* |
8012 | * Not only the cpu but also the task_group of the parent might have | |
8013 | * been changed after parent->se.parent,cfs_rq were copied to | |
8014 | * child->se.parent,cfs_rq. So call __set_task_cpu() to make those | |
8015 | * of child point to valid ones. | |
8016 | */ | |
8017 | rcu_read_lock(); | |
8018 | __set_task_cpu(p, this_cpu); | |
8019 | rcu_read_unlock(); | |
bf0f6f24 | 8020 | |
7109c442 | 8021 | update_curr(cfs_rq); |
cd29fe6f | 8022 | |
b5d9d734 MG |
8023 | if (curr) |
8024 | se->vruntime = curr->vruntime; | |
aeb73b04 | 8025 | place_entity(cfs_rq, se, 1); |
4d78e7b6 | 8026 | |
cd29fe6f | 8027 | if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) { |
87fefa38 | 8028 | /* |
edcb60a3 IM |
8029 | * Upon rescheduling, sched_class::put_prev_task() will place |
8030 | * 'current' within the tree based on its new key value. | |
8031 | */ | |
4d78e7b6 | 8032 | swap(curr->vruntime, se->vruntime); |
8875125e | 8033 | resched_curr(rq); |
4d78e7b6 | 8034 | } |
bf0f6f24 | 8035 | |
88ec22d3 PZ |
8036 | se->vruntime -= cfs_rq->min_vruntime; |
8037 | ||
05fa785c | 8038 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
bf0f6f24 IM |
8039 | } |
8040 | ||
cb469845 SR |
8041 | /* |
8042 | * Priority of the task has changed. Check to see if we preempt | |
8043 | * the current task. | |
8044 | */ | |
da7a735e PZ |
8045 | static void |
8046 | prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio) | |
cb469845 | 8047 | { |
da0c1e65 | 8048 | if (!task_on_rq_queued(p)) |
da7a735e PZ |
8049 | return; |
8050 | ||
cb469845 SR |
8051 | /* |
8052 | * Reschedule if we are currently running on this runqueue and | |
8053 | * our priority decreased, or if we are not currently running on | |
8054 | * this runqueue and our priority is higher than the current's | |
8055 | */ | |
da7a735e | 8056 | if (rq->curr == p) { |
cb469845 | 8057 | if (p->prio > oldprio) |
8875125e | 8058 | resched_curr(rq); |
cb469845 | 8059 | } else |
15afe09b | 8060 | check_preempt_curr(rq, p, 0); |
cb469845 SR |
8061 | } |
8062 | ||
daa59407 | 8063 | static inline bool vruntime_normalized(struct task_struct *p) |
da7a735e PZ |
8064 | { |
8065 | struct sched_entity *se = &p->se; | |
da7a735e PZ |
8066 | |
8067 | /* | |
daa59407 BP |
8068 | * In both the TASK_ON_RQ_QUEUED and TASK_ON_RQ_MIGRATING cases, |
8069 | * the dequeue_entity(.flags=0) will already have normalized the | |
8070 | * vruntime. | |
8071 | */ | |
8072 | if (p->on_rq) | |
8073 | return true; | |
8074 | ||
8075 | /* | |
8076 | * When !on_rq, vruntime of the task has usually NOT been normalized. | |
8077 | * But there are some cases where it has already been normalized: | |
da7a735e | 8078 | * |
daa59407 BP |
8079 | * - A forked child which is waiting for being woken up by |
8080 | * wake_up_new_task(). | |
8081 | * - A task which has been woken up by try_to_wake_up() and | |
8082 | * waiting for actually being woken up by sched_ttwu_pending(). | |
da7a735e | 8083 | */ |
daa59407 BP |
8084 | if (!se->sum_exec_runtime || p->state == TASK_WAKING) |
8085 | return true; | |
8086 | ||
8087 | return false; | |
8088 | } | |
8089 | ||
8090 | static void detach_task_cfs_rq(struct task_struct *p) | |
8091 | { | |
8092 | struct sched_entity *se = &p->se; | |
8093 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
8094 | ||
8095 | if (!vruntime_normalized(p)) { | |
da7a735e PZ |
8096 | /* |
8097 | * Fix up our vruntime so that the current sleep doesn't | |
8098 | * cause 'unlimited' sleep bonus. | |
8099 | */ | |
8100 | place_entity(cfs_rq, se, 0); | |
8101 | se->vruntime -= cfs_rq->min_vruntime; | |
8102 | } | |
9ee474f5 | 8103 | |
9d89c257 | 8104 | /* Catch up with the cfs_rq and remove our load when we leave */ |
a05e8c51 | 8105 | detach_entity_load_avg(cfs_rq, se); |
da7a735e PZ |
8106 | } |
8107 | ||
daa59407 | 8108 | static void attach_task_cfs_rq(struct task_struct *p) |
cb469845 | 8109 | { |
f36c019c | 8110 | struct sched_entity *se = &p->se; |
daa59407 | 8111 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
7855a35a BP |
8112 | |
8113 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
eb7a59b2 M |
8114 | /* |
8115 | * Since the real-depth could have been changed (only FAIR | |
8116 | * class maintain depth value), reset depth properly. | |
8117 | */ | |
8118 | se->depth = se->parent ? se->parent->depth + 1 : 0; | |
8119 | #endif | |
7855a35a | 8120 | |
6efdb105 | 8121 | /* Synchronize task with its cfs_rq */ |
daa59407 BP |
8122 | attach_entity_load_avg(cfs_rq, se); |
8123 | ||
8124 | if (!vruntime_normalized(p)) | |
8125 | se->vruntime += cfs_rq->min_vruntime; | |
8126 | } | |
6efdb105 | 8127 | |
daa59407 BP |
8128 | static void switched_from_fair(struct rq *rq, struct task_struct *p) |
8129 | { | |
8130 | detach_task_cfs_rq(p); | |
8131 | } | |
8132 | ||
8133 | static void switched_to_fair(struct rq *rq, struct task_struct *p) | |
8134 | { | |
8135 | attach_task_cfs_rq(p); | |
7855a35a | 8136 | |
daa59407 | 8137 | if (task_on_rq_queued(p)) { |
7855a35a | 8138 | /* |
daa59407 BP |
8139 | * We were most likely switched from sched_rt, so |
8140 | * kick off the schedule if running, otherwise just see | |
8141 | * if we can still preempt the current task. | |
7855a35a | 8142 | */ |
daa59407 BP |
8143 | if (rq->curr == p) |
8144 | resched_curr(rq); | |
8145 | else | |
8146 | check_preempt_curr(rq, p, 0); | |
7855a35a | 8147 | } |
cb469845 SR |
8148 | } |
8149 | ||
83b699ed SV |
8150 | /* Account for a task changing its policy or group. |
8151 | * | |
8152 | * This routine is mostly called to set cfs_rq->curr field when a task | |
8153 | * migrates between groups/classes. | |
8154 | */ | |
8155 | static void set_curr_task_fair(struct rq *rq) | |
8156 | { | |
8157 | struct sched_entity *se = &rq->curr->se; | |
8158 | ||
ec12cb7f PT |
8159 | for_each_sched_entity(se) { |
8160 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
8161 | ||
8162 | set_next_entity(cfs_rq, se); | |
8163 | /* ensure bandwidth has been allocated on our new cfs_rq */ | |
8164 | account_cfs_rq_runtime(cfs_rq, 0); | |
8165 | } | |
83b699ed SV |
8166 | } |
8167 | ||
029632fb PZ |
8168 | void init_cfs_rq(struct cfs_rq *cfs_rq) |
8169 | { | |
8170 | cfs_rq->tasks_timeline = RB_ROOT; | |
029632fb PZ |
8171 | cfs_rq->min_vruntime = (u64)(-(1LL << 20)); |
8172 | #ifndef CONFIG_64BIT | |
8173 | cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime; | |
8174 | #endif | |
141965c7 | 8175 | #ifdef CONFIG_SMP |
9d89c257 YD |
8176 | atomic_long_set(&cfs_rq->removed_load_avg, 0); |
8177 | atomic_long_set(&cfs_rq->removed_util_avg, 0); | |
9ee474f5 | 8178 | #endif |
029632fb PZ |
8179 | } |
8180 | ||
810b3817 | 8181 | #ifdef CONFIG_FAIR_GROUP_SCHED |
bc54da21 | 8182 | static void task_move_group_fair(struct task_struct *p) |
810b3817 | 8183 | { |
daa59407 | 8184 | detach_task_cfs_rq(p); |
b2b5ce02 | 8185 | set_task_rq(p, task_cpu(p)); |
6efdb105 BP |
8186 | |
8187 | #ifdef CONFIG_SMP | |
8188 | /* Tell se's cfs_rq has been changed -- migrated */ | |
8189 | p->se.avg.last_update_time = 0; | |
8190 | #endif | |
daa59407 | 8191 | attach_task_cfs_rq(p); |
810b3817 | 8192 | } |
029632fb PZ |
8193 | |
8194 | void free_fair_sched_group(struct task_group *tg) | |
8195 | { | |
8196 | int i; | |
8197 | ||
8198 | destroy_cfs_bandwidth(tg_cfs_bandwidth(tg)); | |
8199 | ||
8200 | for_each_possible_cpu(i) { | |
8201 | if (tg->cfs_rq) | |
8202 | kfree(tg->cfs_rq[i]); | |
12695578 YD |
8203 | if (tg->se) { |
8204 | if (tg->se[i]) | |
8205 | remove_entity_load_avg(tg->se[i]); | |
029632fb | 8206 | kfree(tg->se[i]); |
12695578 | 8207 | } |
029632fb PZ |
8208 | } |
8209 | ||
8210 | kfree(tg->cfs_rq); | |
8211 | kfree(tg->se); | |
8212 | } | |
8213 | ||
8214 | int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent) | |
8215 | { | |
8216 | struct cfs_rq *cfs_rq; | |
8217 | struct sched_entity *se; | |
8218 | int i; | |
8219 | ||
8220 | tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL); | |
8221 | if (!tg->cfs_rq) | |
8222 | goto err; | |
8223 | tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL); | |
8224 | if (!tg->se) | |
8225 | goto err; | |
8226 | ||
8227 | tg->shares = NICE_0_LOAD; | |
8228 | ||
8229 | init_cfs_bandwidth(tg_cfs_bandwidth(tg)); | |
8230 | ||
8231 | for_each_possible_cpu(i) { | |
8232 | cfs_rq = kzalloc_node(sizeof(struct cfs_rq), | |
8233 | GFP_KERNEL, cpu_to_node(i)); | |
8234 | if (!cfs_rq) | |
8235 | goto err; | |
8236 | ||
8237 | se = kzalloc_node(sizeof(struct sched_entity), | |
8238 | GFP_KERNEL, cpu_to_node(i)); | |
8239 | if (!se) | |
8240 | goto err_free_rq; | |
8241 | ||
8242 | init_cfs_rq(cfs_rq); | |
8243 | init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]); | |
540247fb | 8244 | init_entity_runnable_average(se); |
029632fb PZ |
8245 | } |
8246 | ||
8247 | return 1; | |
8248 | ||
8249 | err_free_rq: | |
8250 | kfree(cfs_rq); | |
8251 | err: | |
8252 | return 0; | |
8253 | } | |
8254 | ||
8255 | void unregister_fair_sched_group(struct task_group *tg, int cpu) | |
8256 | { | |
8257 | struct rq *rq = cpu_rq(cpu); | |
8258 | unsigned long flags; | |
8259 | ||
8260 | /* | |
8261 | * Only empty task groups can be destroyed; so we can speculatively | |
8262 | * check on_list without danger of it being re-added. | |
8263 | */ | |
8264 | if (!tg->cfs_rq[cpu]->on_list) | |
8265 | return; | |
8266 | ||
8267 | raw_spin_lock_irqsave(&rq->lock, flags); | |
8268 | list_del_leaf_cfs_rq(tg->cfs_rq[cpu]); | |
8269 | raw_spin_unlock_irqrestore(&rq->lock, flags); | |
8270 | } | |
8271 | ||
8272 | void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq, | |
8273 | struct sched_entity *se, int cpu, | |
8274 | struct sched_entity *parent) | |
8275 | { | |
8276 | struct rq *rq = cpu_rq(cpu); | |
8277 | ||
8278 | cfs_rq->tg = tg; | |
8279 | cfs_rq->rq = rq; | |
029632fb PZ |
8280 | init_cfs_rq_runtime(cfs_rq); |
8281 | ||
8282 | tg->cfs_rq[cpu] = cfs_rq; | |
8283 | tg->se[cpu] = se; | |
8284 | ||
8285 | /* se could be NULL for root_task_group */ | |
8286 | if (!se) | |
8287 | return; | |
8288 | ||
fed14d45 | 8289 | if (!parent) { |
029632fb | 8290 | se->cfs_rq = &rq->cfs; |
fed14d45 PZ |
8291 | se->depth = 0; |
8292 | } else { | |
029632fb | 8293 | se->cfs_rq = parent->my_q; |
fed14d45 PZ |
8294 | se->depth = parent->depth + 1; |
8295 | } | |
029632fb PZ |
8296 | |
8297 | se->my_q = cfs_rq; | |
0ac9b1c2 PT |
8298 | /* guarantee group entities always have weight */ |
8299 | update_load_set(&se->load, NICE_0_LOAD); | |
029632fb PZ |
8300 | se->parent = parent; |
8301 | } | |
8302 | ||
8303 | static DEFINE_MUTEX(shares_mutex); | |
8304 | ||
8305 | int sched_group_set_shares(struct task_group *tg, unsigned long shares) | |
8306 | { | |
8307 | int i; | |
8308 | unsigned long flags; | |
8309 | ||
8310 | /* | |
8311 | * We can't change the weight of the root cgroup. | |
8312 | */ | |
8313 | if (!tg->se[0]) | |
8314 | return -EINVAL; | |
8315 | ||
8316 | shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES)); | |
8317 | ||
8318 | mutex_lock(&shares_mutex); | |
8319 | if (tg->shares == shares) | |
8320 | goto done; | |
8321 | ||
8322 | tg->shares = shares; | |
8323 | for_each_possible_cpu(i) { | |
8324 | struct rq *rq = cpu_rq(i); | |
8325 | struct sched_entity *se; | |
8326 | ||
8327 | se = tg->se[i]; | |
8328 | /* Propagate contribution to hierarchy */ | |
8329 | raw_spin_lock_irqsave(&rq->lock, flags); | |
71b1da46 FW |
8330 | |
8331 | /* Possible calls to update_curr() need rq clock */ | |
8332 | update_rq_clock(rq); | |
17bc14b7 | 8333 | for_each_sched_entity(se) |
029632fb PZ |
8334 | update_cfs_shares(group_cfs_rq(se)); |
8335 | raw_spin_unlock_irqrestore(&rq->lock, flags); | |
8336 | } | |
8337 | ||
8338 | done: | |
8339 | mutex_unlock(&shares_mutex); | |
8340 | return 0; | |
8341 | } | |
8342 | #else /* CONFIG_FAIR_GROUP_SCHED */ | |
8343 | ||
8344 | void free_fair_sched_group(struct task_group *tg) { } | |
8345 | ||
8346 | int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent) | |
8347 | { | |
8348 | return 1; | |
8349 | } | |
8350 | ||
8351 | void unregister_fair_sched_group(struct task_group *tg, int cpu) { } | |
8352 | ||
8353 | #endif /* CONFIG_FAIR_GROUP_SCHED */ | |
8354 | ||
810b3817 | 8355 | |
6d686f45 | 8356 | static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task) |
0d721cea PW |
8357 | { |
8358 | struct sched_entity *se = &task->se; | |
0d721cea PW |
8359 | unsigned int rr_interval = 0; |
8360 | ||
8361 | /* | |
8362 | * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise | |
8363 | * idle runqueue: | |
8364 | */ | |
0d721cea | 8365 | if (rq->cfs.load.weight) |
a59f4e07 | 8366 | rr_interval = NS_TO_JIFFIES(sched_slice(cfs_rq_of(se), se)); |
0d721cea PW |
8367 | |
8368 | return rr_interval; | |
8369 | } | |
8370 | ||
bf0f6f24 IM |
8371 | /* |
8372 | * All the scheduling class methods: | |
8373 | */ | |
029632fb | 8374 | const struct sched_class fair_sched_class = { |
5522d5d5 | 8375 | .next = &idle_sched_class, |
bf0f6f24 IM |
8376 | .enqueue_task = enqueue_task_fair, |
8377 | .dequeue_task = dequeue_task_fair, | |
8378 | .yield_task = yield_task_fair, | |
d95f4122 | 8379 | .yield_to_task = yield_to_task_fair, |
bf0f6f24 | 8380 | |
2e09bf55 | 8381 | .check_preempt_curr = check_preempt_wakeup, |
bf0f6f24 IM |
8382 | |
8383 | .pick_next_task = pick_next_task_fair, | |
8384 | .put_prev_task = put_prev_task_fair, | |
8385 | ||
681f3e68 | 8386 | #ifdef CONFIG_SMP |
4ce72a2c | 8387 | .select_task_rq = select_task_rq_fair, |
0a74bef8 | 8388 | .migrate_task_rq = migrate_task_rq_fair, |
141965c7 | 8389 | |
0bcdcf28 CE |
8390 | .rq_online = rq_online_fair, |
8391 | .rq_offline = rq_offline_fair, | |
88ec22d3 PZ |
8392 | |
8393 | .task_waking = task_waking_fair, | |
12695578 | 8394 | .task_dead = task_dead_fair, |
c5b28038 | 8395 | .set_cpus_allowed = set_cpus_allowed_common, |
681f3e68 | 8396 | #endif |
bf0f6f24 | 8397 | |
83b699ed | 8398 | .set_curr_task = set_curr_task_fair, |
bf0f6f24 | 8399 | .task_tick = task_tick_fair, |
cd29fe6f | 8400 | .task_fork = task_fork_fair, |
cb469845 SR |
8401 | |
8402 | .prio_changed = prio_changed_fair, | |
da7a735e | 8403 | .switched_from = switched_from_fair, |
cb469845 | 8404 | .switched_to = switched_to_fair, |
810b3817 | 8405 | |
0d721cea PW |
8406 | .get_rr_interval = get_rr_interval_fair, |
8407 | ||
6e998916 SG |
8408 | .update_curr = update_curr_fair, |
8409 | ||
810b3817 | 8410 | #ifdef CONFIG_FAIR_GROUP_SCHED |
b2b5ce02 | 8411 | .task_move_group = task_move_group_fair, |
810b3817 | 8412 | #endif |
bf0f6f24 IM |
8413 | }; |
8414 | ||
8415 | #ifdef CONFIG_SCHED_DEBUG | |
029632fb | 8416 | void print_cfs_stats(struct seq_file *m, int cpu) |
bf0f6f24 | 8417 | { |
bf0f6f24 IM |
8418 | struct cfs_rq *cfs_rq; |
8419 | ||
5973e5b9 | 8420 | rcu_read_lock(); |
c3b64f1e | 8421 | for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq) |
5cef9eca | 8422 | print_cfs_rq(m, cpu, cfs_rq); |
5973e5b9 | 8423 | rcu_read_unlock(); |
bf0f6f24 | 8424 | } |
397f2378 SD |
8425 | |
8426 | #ifdef CONFIG_NUMA_BALANCING | |
8427 | void show_numa_stats(struct task_struct *p, struct seq_file *m) | |
8428 | { | |
8429 | int node; | |
8430 | unsigned long tsf = 0, tpf = 0, gsf = 0, gpf = 0; | |
8431 | ||
8432 | for_each_online_node(node) { | |
8433 | if (p->numa_faults) { | |
8434 | tsf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 0)]; | |
8435 | tpf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 1)]; | |
8436 | } | |
8437 | if (p->numa_group) { | |
8438 | gsf = p->numa_group->faults[task_faults_idx(NUMA_MEM, node, 0)], | |
8439 | gpf = p->numa_group->faults[task_faults_idx(NUMA_MEM, node, 1)]; | |
8440 | } | |
8441 | print_numa_stats(m, node, tsf, tpf, gsf, gpf); | |
8442 | } | |
8443 | } | |
8444 | #endif /* CONFIG_NUMA_BALANCING */ | |
8445 | #endif /* CONFIG_SCHED_DEBUG */ | |
029632fb PZ |
8446 | |
8447 | __init void init_sched_fair_class(void) | |
8448 | { | |
8449 | #ifdef CONFIG_SMP | |
8450 | open_softirq(SCHED_SOFTIRQ, run_rebalance_domains); | |
8451 | ||
3451d024 | 8452 | #ifdef CONFIG_NO_HZ_COMMON |
554cecaf | 8453 | nohz.next_balance = jiffies; |
029632fb | 8454 | zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT); |
71325960 | 8455 | cpu_notifier(sched_ilb_notifier, 0); |
029632fb PZ |
8456 | #endif |
8457 | #endif /* SMP */ | |
8458 | ||
8459 | } |