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