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 | |
20 | * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com> | |
bf0f6f24 IM |
21 | */ |
22 | ||
9745512c | 23 | #include <linux/latencytop.h> |
1983a922 | 24 | #include <linux/sched.h> |
3436ae12 | 25 | #include <linux/cpumask.h> |
029632fb PZ |
26 | #include <linux/slab.h> |
27 | #include <linux/profile.h> | |
28 | #include <linux/interrupt.h> | |
29 | ||
30 | #include <trace/events/sched.h> | |
31 | ||
32 | #include "sched.h" | |
9745512c | 33 | |
bf0f6f24 | 34 | /* |
21805085 | 35 | * Targeted preemption latency for CPU-bound tasks: |
864616ee | 36 | * (default: 6ms * (1 + ilog(ncpus)), units: nanoseconds) |
bf0f6f24 | 37 | * |
21805085 | 38 | * NOTE: this latency value is not the same as the concept of |
d274a4ce IM |
39 | * 'timeslice length' - timeslices in CFS are of variable length |
40 | * and have no persistent notion like in traditional, time-slice | |
41 | * based scheduling concepts. | |
bf0f6f24 | 42 | * |
d274a4ce IM |
43 | * (to see the precise effective timeslice length of your workload, |
44 | * run vmstat and monitor the context-switches (cs) field) | |
bf0f6f24 | 45 | */ |
21406928 MG |
46 | unsigned int sysctl_sched_latency = 6000000ULL; |
47 | unsigned int normalized_sysctl_sched_latency = 6000000ULL; | |
2bd8e6d4 | 48 | |
1983a922 CE |
49 | /* |
50 | * The initial- and re-scaling of tunables is configurable | |
51 | * (default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus)) | |
52 | * | |
53 | * Options are: | |
54 | * SCHED_TUNABLESCALING_NONE - unscaled, always *1 | |
55 | * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus) | |
56 | * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus | |
57 | */ | |
58 | enum sched_tunable_scaling sysctl_sched_tunable_scaling | |
59 | = SCHED_TUNABLESCALING_LOG; | |
60 | ||
2bd8e6d4 | 61 | /* |
b2be5e96 | 62 | * Minimal preemption granularity for CPU-bound tasks: |
864616ee | 63 | * (default: 0.75 msec * (1 + ilog(ncpus)), units: nanoseconds) |
2bd8e6d4 | 64 | */ |
0bf377bb IM |
65 | unsigned int sysctl_sched_min_granularity = 750000ULL; |
66 | unsigned int normalized_sysctl_sched_min_granularity = 750000ULL; | |
21805085 PZ |
67 | |
68 | /* | |
b2be5e96 PZ |
69 | * is kept at sysctl_sched_latency / sysctl_sched_min_granularity |
70 | */ | |
0bf377bb | 71 | static unsigned int sched_nr_latency = 8; |
b2be5e96 PZ |
72 | |
73 | /* | |
2bba22c5 | 74 | * After fork, child runs first. If set to 0 (default) then |
b2be5e96 | 75 | * parent will (try to) run first. |
21805085 | 76 | */ |
2bba22c5 | 77 | unsigned int sysctl_sched_child_runs_first __read_mostly; |
bf0f6f24 | 78 | |
bf0f6f24 IM |
79 | /* |
80 | * SCHED_OTHER wake-up granularity. | |
172e082a | 81 | * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds) |
bf0f6f24 IM |
82 | * |
83 | * This option delays the preemption effects of decoupled workloads | |
84 | * and reduces their over-scheduling. Synchronous workloads will still | |
85 | * have immediate wakeup/sleep latencies. | |
86 | */ | |
172e082a | 87 | unsigned int sysctl_sched_wakeup_granularity = 1000000UL; |
0bcdcf28 | 88 | unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL; |
bf0f6f24 | 89 | |
da84d961 IM |
90 | const_debug unsigned int sysctl_sched_migration_cost = 500000UL; |
91 | ||
a7a4f8a7 PT |
92 | /* |
93 | * The exponential sliding window over which load is averaged for shares | |
94 | * distribution. | |
95 | * (default: 10msec) | |
96 | */ | |
97 | unsigned int __read_mostly sysctl_sched_shares_window = 10000000UL; | |
98 | ||
ec12cb7f PT |
99 | #ifdef CONFIG_CFS_BANDWIDTH |
100 | /* | |
101 | * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool | |
102 | * each time a cfs_rq requests quota. | |
103 | * | |
104 | * Note: in the case that the slice exceeds the runtime remaining (either due | |
105 | * to consumption or the quota being specified to be smaller than the slice) | |
106 | * we will always only issue the remaining available time. | |
107 | * | |
108 | * default: 5 msec, units: microseconds | |
109 | */ | |
110 | unsigned int sysctl_sched_cfs_bandwidth_slice = 5000UL; | |
111 | #endif | |
112 | ||
029632fb PZ |
113 | /* |
114 | * Increase the granularity value when there are more CPUs, | |
115 | * because with more CPUs the 'effective latency' as visible | |
116 | * to users decreases. But the relationship is not linear, | |
117 | * so pick a second-best guess by going with the log2 of the | |
118 | * number of CPUs. | |
119 | * | |
120 | * This idea comes from the SD scheduler of Con Kolivas: | |
121 | */ | |
122 | static int get_update_sysctl_factor(void) | |
123 | { | |
124 | unsigned int cpus = min_t(int, num_online_cpus(), 8); | |
125 | unsigned int factor; | |
126 | ||
127 | switch (sysctl_sched_tunable_scaling) { | |
128 | case SCHED_TUNABLESCALING_NONE: | |
129 | factor = 1; | |
130 | break; | |
131 | case SCHED_TUNABLESCALING_LINEAR: | |
132 | factor = cpus; | |
133 | break; | |
134 | case SCHED_TUNABLESCALING_LOG: | |
135 | default: | |
136 | factor = 1 + ilog2(cpus); | |
137 | break; | |
138 | } | |
139 | ||
140 | return factor; | |
141 | } | |
142 | ||
143 | static void update_sysctl(void) | |
144 | { | |
145 | unsigned int factor = get_update_sysctl_factor(); | |
146 | ||
147 | #define SET_SYSCTL(name) \ | |
148 | (sysctl_##name = (factor) * normalized_sysctl_##name) | |
149 | SET_SYSCTL(sched_min_granularity); | |
150 | SET_SYSCTL(sched_latency); | |
151 | SET_SYSCTL(sched_wakeup_granularity); | |
152 | #undef SET_SYSCTL | |
153 | } | |
154 | ||
155 | void sched_init_granularity(void) | |
156 | { | |
157 | update_sysctl(); | |
158 | } | |
159 | ||
160 | #if BITS_PER_LONG == 32 | |
161 | # define WMULT_CONST (~0UL) | |
162 | #else | |
163 | # define WMULT_CONST (1UL << 32) | |
164 | #endif | |
165 | ||
166 | #define WMULT_SHIFT 32 | |
167 | ||
168 | /* | |
169 | * Shift right and round: | |
170 | */ | |
171 | #define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y)) | |
172 | ||
173 | /* | |
174 | * delta *= weight / lw | |
175 | */ | |
176 | static unsigned long | |
177 | calc_delta_mine(unsigned long delta_exec, unsigned long weight, | |
178 | struct load_weight *lw) | |
179 | { | |
180 | u64 tmp; | |
181 | ||
182 | /* | |
183 | * weight can be less than 2^SCHED_LOAD_RESOLUTION for task group sched | |
184 | * entities since MIN_SHARES = 2. Treat weight as 1 if less than | |
185 | * 2^SCHED_LOAD_RESOLUTION. | |
186 | */ | |
187 | if (likely(weight > (1UL << SCHED_LOAD_RESOLUTION))) | |
188 | tmp = (u64)delta_exec * scale_load_down(weight); | |
189 | else | |
190 | tmp = (u64)delta_exec; | |
191 | ||
192 | if (!lw->inv_weight) { | |
193 | unsigned long w = scale_load_down(lw->weight); | |
194 | ||
195 | if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST)) | |
196 | lw->inv_weight = 1; | |
197 | else if (unlikely(!w)) | |
198 | lw->inv_weight = WMULT_CONST; | |
199 | else | |
200 | lw->inv_weight = WMULT_CONST / w; | |
201 | } | |
202 | ||
203 | /* | |
204 | * Check whether we'd overflow the 64-bit multiplication: | |
205 | */ | |
206 | if (unlikely(tmp > WMULT_CONST)) | |
207 | tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight, | |
208 | WMULT_SHIFT/2); | |
209 | else | |
210 | tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT); | |
211 | ||
212 | return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX); | |
213 | } | |
214 | ||
215 | ||
216 | const struct sched_class fair_sched_class; | |
a4c2f00f | 217 | |
bf0f6f24 IM |
218 | /************************************************************** |
219 | * CFS operations on generic schedulable entities: | |
220 | */ | |
221 | ||
62160e3f | 222 | #ifdef CONFIG_FAIR_GROUP_SCHED |
bf0f6f24 | 223 | |
62160e3f | 224 | /* cpu runqueue to which this cfs_rq is attached */ |
bf0f6f24 IM |
225 | static inline struct rq *rq_of(struct cfs_rq *cfs_rq) |
226 | { | |
62160e3f | 227 | return cfs_rq->rq; |
bf0f6f24 IM |
228 | } |
229 | ||
62160e3f IM |
230 | /* An entity is a task if it doesn't "own" a runqueue */ |
231 | #define entity_is_task(se) (!se->my_q) | |
bf0f6f24 | 232 | |
8f48894f PZ |
233 | static inline struct task_struct *task_of(struct sched_entity *se) |
234 | { | |
235 | #ifdef CONFIG_SCHED_DEBUG | |
236 | WARN_ON_ONCE(!entity_is_task(se)); | |
237 | #endif | |
238 | return container_of(se, struct task_struct, se); | |
239 | } | |
240 | ||
b758149c PZ |
241 | /* Walk up scheduling entities hierarchy */ |
242 | #define for_each_sched_entity(se) \ | |
243 | for (; se; se = se->parent) | |
244 | ||
245 | static inline struct cfs_rq *task_cfs_rq(struct task_struct *p) | |
246 | { | |
247 | return p->se.cfs_rq; | |
248 | } | |
249 | ||
250 | /* runqueue on which this entity is (to be) queued */ | |
251 | static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se) | |
252 | { | |
253 | return se->cfs_rq; | |
254 | } | |
255 | ||
256 | /* runqueue "owned" by this group */ | |
257 | static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp) | |
258 | { | |
259 | return grp->my_q; | |
260 | } | |
261 | ||
aff3e498 PT |
262 | static void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq, |
263 | int force_update); | |
9ee474f5 | 264 | |
3d4b47b4 PZ |
265 | static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq) |
266 | { | |
267 | if (!cfs_rq->on_list) { | |
67e86250 PT |
268 | /* |
269 | * Ensure we either appear before our parent (if already | |
270 | * enqueued) or force our parent to appear after us when it is | |
271 | * enqueued. The fact that we always enqueue bottom-up | |
272 | * reduces this to two cases. | |
273 | */ | |
274 | if (cfs_rq->tg->parent && | |
275 | cfs_rq->tg->parent->cfs_rq[cpu_of(rq_of(cfs_rq))]->on_list) { | |
276 | list_add_rcu(&cfs_rq->leaf_cfs_rq_list, | |
277 | &rq_of(cfs_rq)->leaf_cfs_rq_list); | |
278 | } else { | |
279 | list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list, | |
3d4b47b4 | 280 | &rq_of(cfs_rq)->leaf_cfs_rq_list); |
67e86250 | 281 | } |
3d4b47b4 PZ |
282 | |
283 | cfs_rq->on_list = 1; | |
9ee474f5 | 284 | /* We should have no load, but we need to update last_decay. */ |
aff3e498 | 285 | update_cfs_rq_blocked_load(cfs_rq, 0); |
3d4b47b4 PZ |
286 | } |
287 | } | |
288 | ||
289 | static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq) | |
290 | { | |
291 | if (cfs_rq->on_list) { | |
292 | list_del_rcu(&cfs_rq->leaf_cfs_rq_list); | |
293 | cfs_rq->on_list = 0; | |
294 | } | |
295 | } | |
296 | ||
b758149c PZ |
297 | /* Iterate thr' all leaf cfs_rq's on a runqueue */ |
298 | #define for_each_leaf_cfs_rq(rq, cfs_rq) \ | |
299 | list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list) | |
300 | ||
301 | /* Do the two (enqueued) entities belong to the same group ? */ | |
302 | static inline int | |
303 | is_same_group(struct sched_entity *se, struct sched_entity *pse) | |
304 | { | |
305 | if (se->cfs_rq == pse->cfs_rq) | |
306 | return 1; | |
307 | ||
308 | return 0; | |
309 | } | |
310 | ||
311 | static inline struct sched_entity *parent_entity(struct sched_entity *se) | |
312 | { | |
313 | return se->parent; | |
314 | } | |
315 | ||
464b7527 PZ |
316 | /* return depth at which a sched entity is present in the hierarchy */ |
317 | static inline int depth_se(struct sched_entity *se) | |
318 | { | |
319 | int depth = 0; | |
320 | ||
321 | for_each_sched_entity(se) | |
322 | depth++; | |
323 | ||
324 | return depth; | |
325 | } | |
326 | ||
327 | static void | |
328 | find_matching_se(struct sched_entity **se, struct sched_entity **pse) | |
329 | { | |
330 | int se_depth, pse_depth; | |
331 | ||
332 | /* | |
333 | * preemption test can be made between sibling entities who are in the | |
334 | * same cfs_rq i.e who have a common parent. Walk up the hierarchy of | |
335 | * both tasks until we find their ancestors who are siblings of common | |
336 | * parent. | |
337 | */ | |
338 | ||
339 | /* First walk up until both entities are at same depth */ | |
340 | se_depth = depth_se(*se); | |
341 | pse_depth = depth_se(*pse); | |
342 | ||
343 | while (se_depth > pse_depth) { | |
344 | se_depth--; | |
345 | *se = parent_entity(*se); | |
346 | } | |
347 | ||
348 | while (pse_depth > se_depth) { | |
349 | pse_depth--; | |
350 | *pse = parent_entity(*pse); | |
351 | } | |
352 | ||
353 | while (!is_same_group(*se, *pse)) { | |
354 | *se = parent_entity(*se); | |
355 | *pse = parent_entity(*pse); | |
356 | } | |
357 | } | |
358 | ||
8f48894f PZ |
359 | #else /* !CONFIG_FAIR_GROUP_SCHED */ |
360 | ||
361 | static inline struct task_struct *task_of(struct sched_entity *se) | |
362 | { | |
363 | return container_of(se, struct task_struct, se); | |
364 | } | |
bf0f6f24 | 365 | |
62160e3f IM |
366 | static inline struct rq *rq_of(struct cfs_rq *cfs_rq) |
367 | { | |
368 | return container_of(cfs_rq, struct rq, cfs); | |
bf0f6f24 IM |
369 | } |
370 | ||
371 | #define entity_is_task(se) 1 | |
372 | ||
b758149c PZ |
373 | #define for_each_sched_entity(se) \ |
374 | for (; se; se = NULL) | |
bf0f6f24 | 375 | |
b758149c | 376 | static inline struct cfs_rq *task_cfs_rq(struct task_struct *p) |
bf0f6f24 | 377 | { |
b758149c | 378 | return &task_rq(p)->cfs; |
bf0f6f24 IM |
379 | } |
380 | ||
b758149c PZ |
381 | static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se) |
382 | { | |
383 | struct task_struct *p = task_of(se); | |
384 | struct rq *rq = task_rq(p); | |
385 | ||
386 | return &rq->cfs; | |
387 | } | |
388 | ||
389 | /* runqueue "owned" by this group */ | |
390 | static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp) | |
391 | { | |
392 | return NULL; | |
393 | } | |
394 | ||
3d4b47b4 PZ |
395 | static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq) |
396 | { | |
397 | } | |
398 | ||
399 | static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq) | |
400 | { | |
401 | } | |
402 | ||
b758149c PZ |
403 | #define for_each_leaf_cfs_rq(rq, cfs_rq) \ |
404 | for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL) | |
405 | ||
406 | static inline int | |
407 | is_same_group(struct sched_entity *se, struct sched_entity *pse) | |
408 | { | |
409 | return 1; | |
410 | } | |
411 | ||
412 | static inline struct sched_entity *parent_entity(struct sched_entity *se) | |
413 | { | |
414 | return NULL; | |
415 | } | |
416 | ||
464b7527 PZ |
417 | static inline void |
418 | find_matching_se(struct sched_entity **se, struct sched_entity **pse) | |
419 | { | |
420 | } | |
421 | ||
b758149c PZ |
422 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
423 | ||
6c16a6dc PZ |
424 | static __always_inline |
425 | void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec); | |
bf0f6f24 IM |
426 | |
427 | /************************************************************** | |
428 | * Scheduling class tree data structure manipulation methods: | |
429 | */ | |
430 | ||
0702e3eb | 431 | static inline u64 max_vruntime(u64 min_vruntime, u64 vruntime) |
02e0431a | 432 | { |
368059a9 PZ |
433 | s64 delta = (s64)(vruntime - min_vruntime); |
434 | if (delta > 0) | |
02e0431a PZ |
435 | min_vruntime = vruntime; |
436 | ||
437 | return min_vruntime; | |
438 | } | |
439 | ||
0702e3eb | 440 | static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime) |
b0ffd246 PZ |
441 | { |
442 | s64 delta = (s64)(vruntime - min_vruntime); | |
443 | if (delta < 0) | |
444 | min_vruntime = vruntime; | |
445 | ||
446 | return min_vruntime; | |
447 | } | |
448 | ||
54fdc581 FC |
449 | static inline int entity_before(struct sched_entity *a, |
450 | struct sched_entity *b) | |
451 | { | |
452 | return (s64)(a->vruntime - b->vruntime) < 0; | |
453 | } | |
454 | ||
1af5f730 PZ |
455 | static void update_min_vruntime(struct cfs_rq *cfs_rq) |
456 | { | |
457 | u64 vruntime = cfs_rq->min_vruntime; | |
458 | ||
459 | if (cfs_rq->curr) | |
460 | vruntime = cfs_rq->curr->vruntime; | |
461 | ||
462 | if (cfs_rq->rb_leftmost) { | |
463 | struct sched_entity *se = rb_entry(cfs_rq->rb_leftmost, | |
464 | struct sched_entity, | |
465 | run_node); | |
466 | ||
e17036da | 467 | if (!cfs_rq->curr) |
1af5f730 PZ |
468 | vruntime = se->vruntime; |
469 | else | |
470 | vruntime = min_vruntime(vruntime, se->vruntime); | |
471 | } | |
472 | ||
473 | cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime); | |
3fe1698b PZ |
474 | #ifndef CONFIG_64BIT |
475 | smp_wmb(); | |
476 | cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime; | |
477 | #endif | |
1af5f730 PZ |
478 | } |
479 | ||
bf0f6f24 IM |
480 | /* |
481 | * Enqueue an entity into the rb-tree: | |
482 | */ | |
0702e3eb | 483 | static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 IM |
484 | { |
485 | struct rb_node **link = &cfs_rq->tasks_timeline.rb_node; | |
486 | struct rb_node *parent = NULL; | |
487 | struct sched_entity *entry; | |
bf0f6f24 IM |
488 | int leftmost = 1; |
489 | ||
490 | /* | |
491 | * Find the right place in the rbtree: | |
492 | */ | |
493 | while (*link) { | |
494 | parent = *link; | |
495 | entry = rb_entry(parent, struct sched_entity, run_node); | |
496 | /* | |
497 | * We dont care about collisions. Nodes with | |
498 | * the same key stay together. | |
499 | */ | |
2bd2d6f2 | 500 | if (entity_before(se, entry)) { |
bf0f6f24 IM |
501 | link = &parent->rb_left; |
502 | } else { | |
503 | link = &parent->rb_right; | |
504 | leftmost = 0; | |
505 | } | |
506 | } | |
507 | ||
508 | /* | |
509 | * Maintain a cache of leftmost tree entries (it is frequently | |
510 | * used): | |
511 | */ | |
1af5f730 | 512 | if (leftmost) |
57cb499d | 513 | cfs_rq->rb_leftmost = &se->run_node; |
bf0f6f24 IM |
514 | |
515 | rb_link_node(&se->run_node, parent, link); | |
516 | rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline); | |
bf0f6f24 IM |
517 | } |
518 | ||
0702e3eb | 519 | static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 | 520 | { |
3fe69747 PZ |
521 | if (cfs_rq->rb_leftmost == &se->run_node) { |
522 | struct rb_node *next_node; | |
3fe69747 PZ |
523 | |
524 | next_node = rb_next(&se->run_node); | |
525 | cfs_rq->rb_leftmost = next_node; | |
3fe69747 | 526 | } |
e9acbff6 | 527 | |
bf0f6f24 | 528 | rb_erase(&se->run_node, &cfs_rq->tasks_timeline); |
bf0f6f24 IM |
529 | } |
530 | ||
029632fb | 531 | struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq) |
bf0f6f24 | 532 | { |
f4b6755f PZ |
533 | struct rb_node *left = cfs_rq->rb_leftmost; |
534 | ||
535 | if (!left) | |
536 | return NULL; | |
537 | ||
538 | return rb_entry(left, struct sched_entity, run_node); | |
bf0f6f24 IM |
539 | } |
540 | ||
ac53db59 RR |
541 | static struct sched_entity *__pick_next_entity(struct sched_entity *se) |
542 | { | |
543 | struct rb_node *next = rb_next(&se->run_node); | |
544 | ||
545 | if (!next) | |
546 | return NULL; | |
547 | ||
548 | return rb_entry(next, struct sched_entity, run_node); | |
549 | } | |
550 | ||
551 | #ifdef CONFIG_SCHED_DEBUG | |
029632fb | 552 | struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq) |
aeb73b04 | 553 | { |
7eee3e67 | 554 | struct rb_node *last = rb_last(&cfs_rq->tasks_timeline); |
aeb73b04 | 555 | |
70eee74b BS |
556 | if (!last) |
557 | return NULL; | |
7eee3e67 IM |
558 | |
559 | return rb_entry(last, struct sched_entity, run_node); | |
aeb73b04 PZ |
560 | } |
561 | ||
bf0f6f24 IM |
562 | /************************************************************** |
563 | * Scheduling class statistics methods: | |
564 | */ | |
565 | ||
acb4a848 | 566 | int sched_proc_update_handler(struct ctl_table *table, int write, |
8d65af78 | 567 | void __user *buffer, size_t *lenp, |
b2be5e96 PZ |
568 | loff_t *ppos) |
569 | { | |
8d65af78 | 570 | int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
acb4a848 | 571 | int factor = get_update_sysctl_factor(); |
b2be5e96 PZ |
572 | |
573 | if (ret || !write) | |
574 | return ret; | |
575 | ||
576 | sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency, | |
577 | sysctl_sched_min_granularity); | |
578 | ||
acb4a848 CE |
579 | #define WRT_SYSCTL(name) \ |
580 | (normalized_sysctl_##name = sysctl_##name / (factor)) | |
581 | WRT_SYSCTL(sched_min_granularity); | |
582 | WRT_SYSCTL(sched_latency); | |
583 | WRT_SYSCTL(sched_wakeup_granularity); | |
acb4a848 CE |
584 | #undef WRT_SYSCTL |
585 | ||
b2be5e96 PZ |
586 | return 0; |
587 | } | |
588 | #endif | |
647e7cac | 589 | |
a7be37ac | 590 | /* |
f9c0b095 | 591 | * delta /= w |
a7be37ac PZ |
592 | */ |
593 | static inline unsigned long | |
594 | calc_delta_fair(unsigned long delta, struct sched_entity *se) | |
595 | { | |
f9c0b095 PZ |
596 | if (unlikely(se->load.weight != NICE_0_LOAD)) |
597 | delta = calc_delta_mine(delta, NICE_0_LOAD, &se->load); | |
a7be37ac PZ |
598 | |
599 | return delta; | |
600 | } | |
601 | ||
647e7cac IM |
602 | /* |
603 | * The idea is to set a period in which each task runs once. | |
604 | * | |
532b1858 | 605 | * When there are too many tasks (sched_nr_latency) we have to stretch |
647e7cac IM |
606 | * this period because otherwise the slices get too small. |
607 | * | |
608 | * p = (nr <= nl) ? l : l*nr/nl | |
609 | */ | |
4d78e7b6 PZ |
610 | static u64 __sched_period(unsigned long nr_running) |
611 | { | |
612 | u64 period = sysctl_sched_latency; | |
b2be5e96 | 613 | unsigned long nr_latency = sched_nr_latency; |
4d78e7b6 PZ |
614 | |
615 | if (unlikely(nr_running > nr_latency)) { | |
4bf0b771 | 616 | period = sysctl_sched_min_granularity; |
4d78e7b6 | 617 | period *= nr_running; |
4d78e7b6 PZ |
618 | } |
619 | ||
620 | return period; | |
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 | } | |
646 | slice = calc_delta_mine(slice, se->load.weight, load); | |
647 | } | |
648 | return slice; | |
bf0f6f24 IM |
649 | } |
650 | ||
647e7cac | 651 | /* |
ac884dec | 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 | ||
d6b55918 | 661 | static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update); |
6d5ab293 | 662 | static void update_cfs_shares(struct cfs_rq *cfs_rq); |
3b3d190e | 663 | |
bf0f6f24 IM |
664 | /* |
665 | * Update the current task's runtime statistics. Skip current tasks that | |
666 | * are not in our scheduling class. | |
667 | */ | |
668 | static inline void | |
8ebc91d9 IM |
669 | __update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr, |
670 | unsigned long delta_exec) | |
bf0f6f24 | 671 | { |
bbdba7c0 | 672 | unsigned long delta_exec_weighted; |
bf0f6f24 | 673 | |
41acab88 LDM |
674 | schedstat_set(curr->statistics.exec_max, |
675 | max((u64)delta_exec, curr->statistics.exec_max)); | |
bf0f6f24 IM |
676 | |
677 | curr->sum_exec_runtime += delta_exec; | |
7a62eabc | 678 | schedstat_add(cfs_rq, exec_clock, delta_exec); |
a7be37ac | 679 | delta_exec_weighted = calc_delta_fair(delta_exec, curr); |
88ec22d3 | 680 | |
e9acbff6 | 681 | curr->vruntime += delta_exec_weighted; |
1af5f730 | 682 | update_min_vruntime(cfs_rq); |
3b3d190e | 683 | |
70caf8a6 | 684 | #if defined CONFIG_SMP && defined CONFIG_FAIR_GROUP_SCHED |
3b3d190e | 685 | cfs_rq->load_unacc_exec_time += delta_exec; |
3b3d190e | 686 | #endif |
bf0f6f24 IM |
687 | } |
688 | ||
b7cc0896 | 689 | static void update_curr(struct cfs_rq *cfs_rq) |
bf0f6f24 | 690 | { |
429d43bc | 691 | struct sched_entity *curr = cfs_rq->curr; |
305e6835 | 692 | u64 now = rq_of(cfs_rq)->clock_task; |
bf0f6f24 IM |
693 | unsigned long delta_exec; |
694 | ||
695 | if (unlikely(!curr)) | |
696 | return; | |
697 | ||
698 | /* | |
699 | * Get the amount of time the current task was running | |
700 | * since the last time we changed load (this cannot | |
701 | * overflow on 32 bits): | |
702 | */ | |
8ebc91d9 | 703 | delta_exec = (unsigned long)(now - curr->exec_start); |
34f28ecd PZ |
704 | if (!delta_exec) |
705 | return; | |
bf0f6f24 | 706 | |
8ebc91d9 IM |
707 | __update_curr(cfs_rq, curr, delta_exec); |
708 | curr->exec_start = now; | |
d842de87 SV |
709 | |
710 | if (entity_is_task(curr)) { | |
711 | struct task_struct *curtask = task_of(curr); | |
712 | ||
f977bb49 | 713 | trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime); |
d842de87 | 714 | cpuacct_charge(curtask, delta_exec); |
f06febc9 | 715 | account_group_exec_runtime(curtask, delta_exec); |
d842de87 | 716 | } |
ec12cb7f PT |
717 | |
718 | account_cfs_rq_runtime(cfs_rq, delta_exec); | |
bf0f6f24 IM |
719 | } |
720 | ||
721 | static inline void | |
5870db5b | 722 | update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 | 723 | { |
41acab88 | 724 | schedstat_set(se->statistics.wait_start, rq_of(cfs_rq)->clock); |
bf0f6f24 IM |
725 | } |
726 | ||
bf0f6f24 IM |
727 | /* |
728 | * Task is being enqueued - update stats: | |
729 | */ | |
d2417e5a | 730 | static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 | 731 | { |
bf0f6f24 IM |
732 | /* |
733 | * Are we enqueueing a waiting task? (for current tasks | |
734 | * a dequeue/enqueue event is a NOP) | |
735 | */ | |
429d43bc | 736 | if (se != cfs_rq->curr) |
5870db5b | 737 | update_stats_wait_start(cfs_rq, se); |
bf0f6f24 IM |
738 | } |
739 | ||
bf0f6f24 | 740 | static void |
9ef0a961 | 741 | update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 | 742 | { |
41acab88 LDM |
743 | schedstat_set(se->statistics.wait_max, max(se->statistics.wait_max, |
744 | rq_of(cfs_rq)->clock - se->statistics.wait_start)); | |
745 | schedstat_set(se->statistics.wait_count, se->statistics.wait_count + 1); | |
746 | schedstat_set(se->statistics.wait_sum, se->statistics.wait_sum + | |
747 | rq_of(cfs_rq)->clock - se->statistics.wait_start); | |
768d0c27 PZ |
748 | #ifdef CONFIG_SCHEDSTATS |
749 | if (entity_is_task(se)) { | |
750 | trace_sched_stat_wait(task_of(se), | |
41acab88 | 751 | rq_of(cfs_rq)->clock - se->statistics.wait_start); |
768d0c27 PZ |
752 | } |
753 | #endif | |
41acab88 | 754 | schedstat_set(se->statistics.wait_start, 0); |
bf0f6f24 IM |
755 | } |
756 | ||
757 | static inline void | |
19b6a2e3 | 758 | update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 | 759 | { |
bf0f6f24 IM |
760 | /* |
761 | * Mark the end of the wait period if dequeueing a | |
762 | * waiting task: | |
763 | */ | |
429d43bc | 764 | if (se != cfs_rq->curr) |
9ef0a961 | 765 | update_stats_wait_end(cfs_rq, se); |
bf0f6f24 IM |
766 | } |
767 | ||
768 | /* | |
769 | * We are picking a new current task - update its stats: | |
770 | */ | |
771 | static inline void | |
79303e9e | 772 | update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 IM |
773 | { |
774 | /* | |
775 | * We are starting a new run period: | |
776 | */ | |
305e6835 | 777 | se->exec_start = rq_of(cfs_rq)->clock_task; |
bf0f6f24 IM |
778 | } |
779 | ||
bf0f6f24 IM |
780 | /************************************************** |
781 | * Scheduling class queueing methods: | |
782 | */ | |
783 | ||
30cfdcfc DA |
784 | static void |
785 | account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
786 | { | |
787 | update_load_add(&cfs_rq->load, se->load.weight); | |
c09595f6 | 788 | if (!parent_entity(se)) |
029632fb | 789 | update_load_add(&rq_of(cfs_rq)->load, se->load.weight); |
367456c7 PZ |
790 | #ifdef CONFIG_SMP |
791 | if (entity_is_task(se)) | |
eb95308e | 792 | list_add(&se->group_node, &rq_of(cfs_rq)->cfs_tasks); |
367456c7 | 793 | #endif |
30cfdcfc | 794 | cfs_rq->nr_running++; |
30cfdcfc DA |
795 | } |
796 | ||
797 | static void | |
798 | account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
799 | { | |
800 | update_load_sub(&cfs_rq->load, se->load.weight); | |
c09595f6 | 801 | if (!parent_entity(se)) |
029632fb | 802 | update_load_sub(&rq_of(cfs_rq)->load, se->load.weight); |
367456c7 | 803 | if (entity_is_task(se)) |
b87f1724 | 804 | list_del_init(&se->group_node); |
30cfdcfc | 805 | cfs_rq->nr_running--; |
30cfdcfc DA |
806 | } |
807 | ||
3ff6dcac | 808 | #ifdef CONFIG_FAIR_GROUP_SCHED |
64660c86 PT |
809 | /* we need this in update_cfs_load and load-balance functions below */ |
810 | static inline int throttled_hierarchy(struct cfs_rq *cfs_rq); | |
3ff6dcac | 811 | # ifdef CONFIG_SMP |
d6b55918 PT |
812 | static void update_cfs_rq_load_contribution(struct cfs_rq *cfs_rq, |
813 | int global_update) | |
814 | { | |
815 | struct task_group *tg = cfs_rq->tg; | |
816 | long load_avg; | |
817 | ||
818 | load_avg = div64_u64(cfs_rq->load_avg, cfs_rq->load_period+1); | |
819 | load_avg -= cfs_rq->load_contribution; | |
820 | ||
821 | if (global_update || abs(load_avg) > cfs_rq->load_contribution / 8) { | |
822 | atomic_add(load_avg, &tg->load_weight); | |
823 | cfs_rq->load_contribution += load_avg; | |
824 | } | |
825 | } | |
826 | ||
827 | static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update) | |
2069dd75 | 828 | { |
a7a4f8a7 | 829 | u64 period = sysctl_sched_shares_window; |
2069dd75 | 830 | u64 now, delta; |
e33078ba | 831 | unsigned long load = cfs_rq->load.weight; |
2069dd75 | 832 | |
64660c86 | 833 | if (cfs_rq->tg == &root_task_group || throttled_hierarchy(cfs_rq)) |
2069dd75 PZ |
834 | return; |
835 | ||
05ca62c6 | 836 | now = rq_of(cfs_rq)->clock_task; |
2069dd75 PZ |
837 | delta = now - cfs_rq->load_stamp; |
838 | ||
e33078ba PT |
839 | /* truncate load history at 4 idle periods */ |
840 | if (cfs_rq->load_stamp > cfs_rq->load_last && | |
841 | now - cfs_rq->load_last > 4 * period) { | |
842 | cfs_rq->load_period = 0; | |
843 | cfs_rq->load_avg = 0; | |
f07333bf | 844 | delta = period - 1; |
e33078ba PT |
845 | } |
846 | ||
2069dd75 | 847 | cfs_rq->load_stamp = now; |
3b3d190e | 848 | cfs_rq->load_unacc_exec_time = 0; |
2069dd75 | 849 | cfs_rq->load_period += delta; |
e33078ba PT |
850 | if (load) { |
851 | cfs_rq->load_last = now; | |
852 | cfs_rq->load_avg += delta * load; | |
853 | } | |
2069dd75 | 854 | |
d6b55918 PT |
855 | /* consider updating load contribution on each fold or truncate */ |
856 | if (global_update || cfs_rq->load_period > period | |
857 | || !cfs_rq->load_period) | |
858 | update_cfs_rq_load_contribution(cfs_rq, global_update); | |
859 | ||
2069dd75 PZ |
860 | while (cfs_rq->load_period > period) { |
861 | /* | |
862 | * Inline assembly required to prevent the compiler | |
863 | * optimising this loop into a divmod call. | |
864 | * See __iter_div_u64_rem() for another example of this. | |
865 | */ | |
866 | asm("" : "+rm" (cfs_rq->load_period)); | |
867 | cfs_rq->load_period /= 2; | |
868 | cfs_rq->load_avg /= 2; | |
869 | } | |
3d4b47b4 | 870 | |
e33078ba PT |
871 | if (!cfs_rq->curr && !cfs_rq->nr_running && !cfs_rq->load_avg) |
872 | list_del_leaf_cfs_rq(cfs_rq); | |
2069dd75 PZ |
873 | } |
874 | ||
cf5f0acf PZ |
875 | static inline long calc_tg_weight(struct task_group *tg, struct cfs_rq *cfs_rq) |
876 | { | |
877 | long tg_weight; | |
878 | ||
879 | /* | |
880 | * Use this CPU's actual weight instead of the last load_contribution | |
881 | * to gain a more accurate current total weight. See | |
882 | * update_cfs_rq_load_contribution(). | |
883 | */ | |
884 | tg_weight = atomic_read(&tg->load_weight); | |
885 | tg_weight -= cfs_rq->load_contribution; | |
886 | tg_weight += cfs_rq->load.weight; | |
887 | ||
888 | return tg_weight; | |
889 | } | |
890 | ||
6d5ab293 | 891 | static long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg) |
3ff6dcac | 892 | { |
cf5f0acf | 893 | long tg_weight, load, shares; |
3ff6dcac | 894 | |
cf5f0acf | 895 | tg_weight = calc_tg_weight(tg, cfs_rq); |
6d5ab293 | 896 | load = cfs_rq->load.weight; |
3ff6dcac | 897 | |
3ff6dcac | 898 | shares = (tg->shares * load); |
cf5f0acf PZ |
899 | if (tg_weight) |
900 | shares /= tg_weight; | |
3ff6dcac YZ |
901 | |
902 | if (shares < MIN_SHARES) | |
903 | shares = MIN_SHARES; | |
904 | if (shares > tg->shares) | |
905 | shares = tg->shares; | |
906 | ||
907 | return shares; | |
908 | } | |
909 | ||
910 | static void update_entity_shares_tick(struct cfs_rq *cfs_rq) | |
911 | { | |
912 | if (cfs_rq->load_unacc_exec_time > sysctl_sched_shares_window) { | |
913 | update_cfs_load(cfs_rq, 0); | |
6d5ab293 | 914 | update_cfs_shares(cfs_rq); |
3ff6dcac YZ |
915 | } |
916 | } | |
917 | # else /* CONFIG_SMP */ | |
918 | static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update) | |
919 | { | |
920 | } | |
921 | ||
6d5ab293 | 922 | static inline long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg) |
3ff6dcac YZ |
923 | { |
924 | return tg->shares; | |
925 | } | |
926 | ||
927 | static inline void update_entity_shares_tick(struct cfs_rq *cfs_rq) | |
928 | { | |
929 | } | |
930 | # endif /* CONFIG_SMP */ | |
2069dd75 PZ |
931 | static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, |
932 | unsigned long weight) | |
933 | { | |
19e5eebb PT |
934 | if (se->on_rq) { |
935 | /* commit outstanding execution time */ | |
936 | if (cfs_rq->curr == se) | |
937 | update_curr(cfs_rq); | |
2069dd75 | 938 | account_entity_dequeue(cfs_rq, se); |
19e5eebb | 939 | } |
2069dd75 PZ |
940 | |
941 | update_load_set(&se->load, weight); | |
942 | ||
943 | if (se->on_rq) | |
944 | account_entity_enqueue(cfs_rq, se); | |
945 | } | |
946 | ||
6d5ab293 | 947 | static void update_cfs_shares(struct cfs_rq *cfs_rq) |
2069dd75 PZ |
948 | { |
949 | struct task_group *tg; | |
950 | struct sched_entity *se; | |
3ff6dcac | 951 | long shares; |
2069dd75 | 952 | |
2069dd75 PZ |
953 | tg = cfs_rq->tg; |
954 | se = tg->se[cpu_of(rq_of(cfs_rq))]; | |
64660c86 | 955 | if (!se || throttled_hierarchy(cfs_rq)) |
2069dd75 | 956 | return; |
3ff6dcac YZ |
957 | #ifndef CONFIG_SMP |
958 | if (likely(se->load.weight == tg->shares)) | |
959 | return; | |
960 | #endif | |
6d5ab293 | 961 | shares = calc_cfs_shares(cfs_rq, tg); |
2069dd75 PZ |
962 | |
963 | reweight_entity(cfs_rq_of(se), se, shares); | |
964 | } | |
965 | #else /* CONFIG_FAIR_GROUP_SCHED */ | |
d6b55918 | 966 | static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update) |
2069dd75 PZ |
967 | { |
968 | } | |
969 | ||
6d5ab293 | 970 | static inline void update_cfs_shares(struct cfs_rq *cfs_rq) |
2069dd75 PZ |
971 | { |
972 | } | |
43365bd7 PT |
973 | |
974 | static inline void update_entity_shares_tick(struct cfs_rq *cfs_rq) | |
975 | { | |
976 | } | |
2069dd75 PZ |
977 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
978 | ||
9d85f21c PT |
979 | #ifdef CONFIG_SMP |
980 | /* | |
981 | * Approximate: | |
982 | * val * y^n, where y^32 ~= 0.5 (~1 scheduling period) | |
983 | */ | |
984 | static __always_inline u64 decay_load(u64 val, u64 n) | |
985 | { | |
986 | for (; n && val; n--) { | |
987 | val *= 4008; | |
988 | val >>= 12; | |
989 | } | |
990 | ||
991 | return val; | |
992 | } | |
993 | ||
994 | /* | |
995 | * We can represent the historical contribution to runnable average as the | |
996 | * coefficients of a geometric series. To do this we sub-divide our runnable | |
997 | * history into segments of approximately 1ms (1024us); label the segment that | |
998 | * occurred N-ms ago p_N, with p_0 corresponding to the current period, e.g. | |
999 | * | |
1000 | * [<- 1024us ->|<- 1024us ->|<- 1024us ->| ... | |
1001 | * p0 p1 p2 | |
1002 | * (now) (~1ms ago) (~2ms ago) | |
1003 | * | |
1004 | * Let u_i denote the fraction of p_i that the entity was runnable. | |
1005 | * | |
1006 | * We then designate the fractions u_i as our co-efficients, yielding the | |
1007 | * following representation of historical load: | |
1008 | * u_0 + u_1*y + u_2*y^2 + u_3*y^3 + ... | |
1009 | * | |
1010 | * We choose y based on the with of a reasonably scheduling period, fixing: | |
1011 | * y^32 = 0.5 | |
1012 | * | |
1013 | * This means that the contribution to load ~32ms ago (u_32) will be weighted | |
1014 | * approximately half as much as the contribution to load within the last ms | |
1015 | * (u_0). | |
1016 | * | |
1017 | * When a period "rolls over" and we have new u_0`, multiplying the previous | |
1018 | * sum again by y is sufficient to update: | |
1019 | * load_avg = u_0` + y*(u_0 + u_1*y + u_2*y^2 + ... ) | |
1020 | * = u_0 + u_1*y + u_2*y^2 + ... [re-labeling u_i --> u_{i+1}] | |
1021 | */ | |
1022 | static __always_inline int __update_entity_runnable_avg(u64 now, | |
1023 | struct sched_avg *sa, | |
1024 | int runnable) | |
1025 | { | |
1026 | u64 delta; | |
1027 | int delta_w, decayed = 0; | |
1028 | ||
1029 | delta = now - sa->last_runnable_update; | |
1030 | /* | |
1031 | * This should only happen when time goes backwards, which it | |
1032 | * unfortunately does during sched clock init when we swap over to TSC. | |
1033 | */ | |
1034 | if ((s64)delta < 0) { | |
1035 | sa->last_runnable_update = now; | |
1036 | return 0; | |
1037 | } | |
1038 | ||
1039 | /* | |
1040 | * Use 1024ns as the unit of measurement since it's a reasonable | |
1041 | * approximation of 1us and fast to compute. | |
1042 | */ | |
1043 | delta >>= 10; | |
1044 | if (!delta) | |
1045 | return 0; | |
1046 | sa->last_runnable_update = now; | |
1047 | ||
1048 | /* delta_w is the amount already accumulated against our next period */ | |
1049 | delta_w = sa->runnable_avg_period % 1024; | |
1050 | if (delta + delta_w >= 1024) { | |
1051 | /* period roll-over */ | |
1052 | decayed = 1; | |
1053 | ||
1054 | /* | |
1055 | * Now that we know we're crossing a period boundary, figure | |
1056 | * out how much from delta we need to complete the current | |
1057 | * period and accrue it. | |
1058 | */ | |
1059 | delta_w = 1024 - delta_w; | |
1060 | BUG_ON(delta_w > delta); | |
1061 | do { | |
1062 | if (runnable) | |
1063 | sa->runnable_avg_sum += delta_w; | |
1064 | sa->runnable_avg_period += delta_w; | |
1065 | ||
1066 | /* | |
1067 | * Remainder of delta initiates a new period, roll over | |
1068 | * the previous. | |
1069 | */ | |
1070 | sa->runnable_avg_sum = | |
1071 | decay_load(sa->runnable_avg_sum, 1); | |
1072 | sa->runnable_avg_period = | |
1073 | decay_load(sa->runnable_avg_period, 1); | |
1074 | ||
1075 | delta -= delta_w; | |
1076 | /* New period is empty */ | |
1077 | delta_w = 1024; | |
1078 | } while (delta >= 1024); | |
1079 | } | |
1080 | ||
1081 | /* Remainder of delta accrued against u_0` */ | |
1082 | if (runnable) | |
1083 | sa->runnable_avg_sum += delta; | |
1084 | sa->runnable_avg_period += delta; | |
1085 | ||
1086 | return decayed; | |
1087 | } | |
1088 | ||
9ee474f5 | 1089 | /* Synchronize an entity's decay with its parenting cfs_rq.*/ |
aff3e498 | 1090 | static inline u64 __synchronize_entity_decay(struct sched_entity *se) |
9ee474f5 PT |
1091 | { |
1092 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
1093 | u64 decays = atomic64_read(&cfs_rq->decay_counter); | |
1094 | ||
1095 | decays -= se->avg.decay_count; | |
1096 | if (!decays) | |
aff3e498 | 1097 | return 0; |
9ee474f5 PT |
1098 | |
1099 | se->avg.load_avg_contrib = decay_load(se->avg.load_avg_contrib, decays); | |
1100 | se->avg.decay_count = 0; | |
aff3e498 PT |
1101 | |
1102 | return decays; | |
9ee474f5 PT |
1103 | } |
1104 | ||
c566e8e9 PT |
1105 | #ifdef CONFIG_FAIR_GROUP_SCHED |
1106 | static inline void __update_cfs_rq_tg_load_contrib(struct cfs_rq *cfs_rq, | |
1107 | int force_update) | |
1108 | { | |
1109 | struct task_group *tg = cfs_rq->tg; | |
1110 | s64 tg_contrib; | |
1111 | ||
1112 | tg_contrib = cfs_rq->runnable_load_avg + cfs_rq->blocked_load_avg; | |
1113 | tg_contrib -= cfs_rq->tg_load_contrib; | |
1114 | ||
1115 | if (force_update || abs64(tg_contrib) > cfs_rq->tg_load_contrib / 8) { | |
1116 | atomic64_add(tg_contrib, &tg->load_avg); | |
1117 | cfs_rq->tg_load_contrib += tg_contrib; | |
1118 | } | |
1119 | } | |
8165e145 | 1120 | |
bb17f655 PT |
1121 | /* |
1122 | * Aggregate cfs_rq runnable averages into an equivalent task_group | |
1123 | * representation for computing load contributions. | |
1124 | */ | |
1125 | static inline void __update_tg_runnable_avg(struct sched_avg *sa, | |
1126 | struct cfs_rq *cfs_rq) | |
1127 | { | |
1128 | struct task_group *tg = cfs_rq->tg; | |
1129 | long contrib; | |
1130 | ||
1131 | /* The fraction of a cpu used by this cfs_rq */ | |
1132 | contrib = div_u64(sa->runnable_avg_sum << NICE_0_SHIFT, | |
1133 | sa->runnable_avg_period + 1); | |
1134 | contrib -= cfs_rq->tg_runnable_contrib; | |
1135 | ||
1136 | if (abs(contrib) > cfs_rq->tg_runnable_contrib / 64) { | |
1137 | atomic_add(contrib, &tg->runnable_avg); | |
1138 | cfs_rq->tg_runnable_contrib += contrib; | |
1139 | } | |
1140 | } | |
1141 | ||
8165e145 PT |
1142 | static inline void __update_group_entity_contrib(struct sched_entity *se) |
1143 | { | |
1144 | struct cfs_rq *cfs_rq = group_cfs_rq(se); | |
1145 | struct task_group *tg = cfs_rq->tg; | |
bb17f655 PT |
1146 | int runnable_avg; |
1147 | ||
8165e145 PT |
1148 | u64 contrib; |
1149 | ||
1150 | contrib = cfs_rq->tg_load_contrib * tg->shares; | |
1151 | se->avg.load_avg_contrib = div64_u64(contrib, | |
1152 | atomic64_read(&tg->load_avg) + 1); | |
bb17f655 PT |
1153 | |
1154 | /* | |
1155 | * For group entities we need to compute a correction term in the case | |
1156 | * that they are consuming <1 cpu so that we would contribute the same | |
1157 | * load as a task of equal weight. | |
1158 | * | |
1159 | * Explicitly co-ordinating this measurement would be expensive, but | |
1160 | * fortunately the sum of each cpus contribution forms a usable | |
1161 | * lower-bound on the true value. | |
1162 | * | |
1163 | * Consider the aggregate of 2 contributions. Either they are disjoint | |
1164 | * (and the sum represents true value) or they are disjoint and we are | |
1165 | * understating by the aggregate of their overlap. | |
1166 | * | |
1167 | * Extending this to N cpus, for a given overlap, the maximum amount we | |
1168 | * understand is then n_i(n_i+1)/2 * w_i where n_i is the number of | |
1169 | * cpus that overlap for this interval and w_i is the interval width. | |
1170 | * | |
1171 | * On a small machine; the first term is well-bounded which bounds the | |
1172 | * total error since w_i is a subset of the period. Whereas on a | |
1173 | * larger machine, while this first term can be larger, if w_i is the | |
1174 | * of consequential size guaranteed to see n_i*w_i quickly converge to | |
1175 | * our upper bound of 1-cpu. | |
1176 | */ | |
1177 | runnable_avg = atomic_read(&tg->runnable_avg); | |
1178 | if (runnable_avg < NICE_0_LOAD) { | |
1179 | se->avg.load_avg_contrib *= runnable_avg; | |
1180 | se->avg.load_avg_contrib >>= NICE_0_SHIFT; | |
1181 | } | |
8165e145 | 1182 | } |
c566e8e9 PT |
1183 | #else |
1184 | static inline void __update_cfs_rq_tg_load_contrib(struct cfs_rq *cfs_rq, | |
1185 | int force_update) {} | |
bb17f655 PT |
1186 | static inline void __update_tg_runnable_avg(struct sched_avg *sa, |
1187 | struct cfs_rq *cfs_rq) {} | |
8165e145 | 1188 | static inline void __update_group_entity_contrib(struct sched_entity *se) {} |
c566e8e9 PT |
1189 | #endif |
1190 | ||
8165e145 PT |
1191 | static inline void __update_task_entity_contrib(struct sched_entity *se) |
1192 | { | |
1193 | u32 contrib; | |
1194 | ||
1195 | /* avoid overflowing a 32-bit type w/ SCHED_LOAD_SCALE */ | |
1196 | contrib = se->avg.runnable_avg_sum * scale_load_down(se->load.weight); | |
1197 | contrib /= (se->avg.runnable_avg_period + 1); | |
1198 | se->avg.load_avg_contrib = scale_load(contrib); | |
1199 | } | |
1200 | ||
2dac754e PT |
1201 | /* Compute the current contribution to load_avg by se, return any delta */ |
1202 | static long __update_entity_load_avg_contrib(struct sched_entity *se) | |
1203 | { | |
1204 | long old_contrib = se->avg.load_avg_contrib; | |
1205 | ||
8165e145 PT |
1206 | if (entity_is_task(se)) { |
1207 | __update_task_entity_contrib(se); | |
1208 | } else { | |
bb17f655 | 1209 | __update_tg_runnable_avg(&se->avg, group_cfs_rq(se)); |
8165e145 PT |
1210 | __update_group_entity_contrib(se); |
1211 | } | |
2dac754e PT |
1212 | |
1213 | return se->avg.load_avg_contrib - old_contrib; | |
1214 | } | |
1215 | ||
9ee474f5 PT |
1216 | static inline void subtract_blocked_load_contrib(struct cfs_rq *cfs_rq, |
1217 | long load_contrib) | |
1218 | { | |
1219 | if (likely(load_contrib < cfs_rq->blocked_load_avg)) | |
1220 | cfs_rq->blocked_load_avg -= load_contrib; | |
1221 | else | |
1222 | cfs_rq->blocked_load_avg = 0; | |
1223 | } | |
1224 | ||
f1b17280 PT |
1225 | static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq); |
1226 | ||
9d85f21c | 1227 | /* Update a sched_entity's runnable average */ |
9ee474f5 PT |
1228 | static inline void update_entity_load_avg(struct sched_entity *se, |
1229 | int update_cfs_rq) | |
9d85f21c | 1230 | { |
2dac754e PT |
1231 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
1232 | long contrib_delta; | |
f1b17280 | 1233 | u64 now; |
2dac754e | 1234 | |
f1b17280 PT |
1235 | /* |
1236 | * For a group entity we need to use their owned cfs_rq_clock_task() in | |
1237 | * case they are the parent of a throttled hierarchy. | |
1238 | */ | |
1239 | if (entity_is_task(se)) | |
1240 | now = cfs_rq_clock_task(cfs_rq); | |
1241 | else | |
1242 | now = cfs_rq_clock_task(group_cfs_rq(se)); | |
1243 | ||
1244 | if (!__update_entity_runnable_avg(now, &se->avg, se->on_rq)) | |
2dac754e PT |
1245 | return; |
1246 | ||
1247 | contrib_delta = __update_entity_load_avg_contrib(se); | |
9ee474f5 PT |
1248 | |
1249 | if (!update_cfs_rq) | |
1250 | return; | |
1251 | ||
2dac754e PT |
1252 | if (se->on_rq) |
1253 | cfs_rq->runnable_load_avg += contrib_delta; | |
9ee474f5 PT |
1254 | else |
1255 | subtract_blocked_load_contrib(cfs_rq, -contrib_delta); | |
1256 | } | |
1257 | ||
1258 | /* | |
1259 | * Decay the load contributed by all blocked children and account this so that | |
1260 | * their contribution may appropriately discounted when they wake up. | |
1261 | */ | |
aff3e498 | 1262 | static void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq, int force_update) |
9ee474f5 | 1263 | { |
f1b17280 | 1264 | u64 now = cfs_rq_clock_task(cfs_rq) >> 20; |
9ee474f5 PT |
1265 | u64 decays; |
1266 | ||
1267 | decays = now - cfs_rq->last_decay; | |
aff3e498 | 1268 | if (!decays && !force_update) |
9ee474f5 PT |
1269 | return; |
1270 | ||
aff3e498 PT |
1271 | if (atomic64_read(&cfs_rq->removed_load)) { |
1272 | u64 removed_load = atomic64_xchg(&cfs_rq->removed_load, 0); | |
1273 | subtract_blocked_load_contrib(cfs_rq, removed_load); | |
1274 | } | |
9ee474f5 | 1275 | |
aff3e498 PT |
1276 | if (decays) { |
1277 | cfs_rq->blocked_load_avg = decay_load(cfs_rq->blocked_load_avg, | |
1278 | decays); | |
1279 | atomic64_add(decays, &cfs_rq->decay_counter); | |
1280 | cfs_rq->last_decay = now; | |
1281 | } | |
c566e8e9 PT |
1282 | |
1283 | __update_cfs_rq_tg_load_contrib(cfs_rq, force_update); | |
9d85f21c | 1284 | } |
18bf2805 BS |
1285 | |
1286 | static inline void update_rq_runnable_avg(struct rq *rq, int runnable) | |
1287 | { | |
1288 | __update_entity_runnable_avg(rq->clock_task, &rq->avg, runnable); | |
bb17f655 | 1289 | __update_tg_runnable_avg(&rq->avg, &rq->cfs); |
18bf2805 | 1290 | } |
2dac754e PT |
1291 | |
1292 | /* Add the load generated by se into cfs_rq's child load-average */ | |
1293 | static inline void enqueue_entity_load_avg(struct cfs_rq *cfs_rq, | |
9ee474f5 PT |
1294 | struct sched_entity *se, |
1295 | int wakeup) | |
2dac754e | 1296 | { |
aff3e498 PT |
1297 | /* |
1298 | * We track migrations using entity decay_count <= 0, on a wake-up | |
1299 | * migration we use a negative decay count to track the remote decays | |
1300 | * accumulated while sleeping. | |
1301 | */ | |
1302 | if (unlikely(se->avg.decay_count <= 0)) { | |
9ee474f5 | 1303 | se->avg.last_runnable_update = rq_of(cfs_rq)->clock_task; |
aff3e498 PT |
1304 | if (se->avg.decay_count) { |
1305 | /* | |
1306 | * In a wake-up migration we have to approximate the | |
1307 | * time sleeping. This is because we can't synchronize | |
1308 | * clock_task between the two cpus, and it is not | |
1309 | * guaranteed to be read-safe. Instead, we can | |
1310 | * approximate this using our carried decays, which are | |
1311 | * explicitly atomically readable. | |
1312 | */ | |
1313 | se->avg.last_runnable_update -= (-se->avg.decay_count) | |
1314 | << 20; | |
1315 | update_entity_load_avg(se, 0); | |
1316 | /* Indicate that we're now synchronized and on-rq */ | |
1317 | se->avg.decay_count = 0; | |
1318 | } | |
9ee474f5 PT |
1319 | wakeup = 0; |
1320 | } else { | |
1321 | __synchronize_entity_decay(se); | |
1322 | } | |
1323 | ||
aff3e498 PT |
1324 | /* migrated tasks did not contribute to our blocked load */ |
1325 | if (wakeup) { | |
9ee474f5 | 1326 | subtract_blocked_load_contrib(cfs_rq, se->avg.load_avg_contrib); |
aff3e498 PT |
1327 | update_entity_load_avg(se, 0); |
1328 | } | |
9ee474f5 | 1329 | |
2dac754e | 1330 | cfs_rq->runnable_load_avg += se->avg.load_avg_contrib; |
aff3e498 PT |
1331 | /* we force update consideration on load-balancer moves */ |
1332 | update_cfs_rq_blocked_load(cfs_rq, !wakeup); | |
2dac754e PT |
1333 | } |
1334 | ||
9ee474f5 PT |
1335 | /* |
1336 | * Remove se's load from this cfs_rq child load-average, if the entity is | |
1337 | * transitioning to a blocked state we track its projected decay using | |
1338 | * blocked_load_avg. | |
1339 | */ | |
2dac754e | 1340 | static inline void dequeue_entity_load_avg(struct cfs_rq *cfs_rq, |
9ee474f5 PT |
1341 | struct sched_entity *se, |
1342 | int sleep) | |
2dac754e | 1343 | { |
9ee474f5 | 1344 | update_entity_load_avg(se, 1); |
aff3e498 PT |
1345 | /* we force update consideration on load-balancer moves */ |
1346 | update_cfs_rq_blocked_load(cfs_rq, !sleep); | |
9ee474f5 | 1347 | |
2dac754e | 1348 | cfs_rq->runnable_load_avg -= se->avg.load_avg_contrib; |
9ee474f5 PT |
1349 | if (sleep) { |
1350 | cfs_rq->blocked_load_avg += se->avg.load_avg_contrib; | |
1351 | se->avg.decay_count = atomic64_read(&cfs_rq->decay_counter); | |
1352 | } /* migrations, e.g. sleep=0 leave decay_count == 0 */ | |
2dac754e | 1353 | } |
9d85f21c | 1354 | #else |
9ee474f5 PT |
1355 | static inline void update_entity_load_avg(struct sched_entity *se, |
1356 | int update_cfs_rq) {} | |
18bf2805 | 1357 | static inline void update_rq_runnable_avg(struct rq *rq, int runnable) {} |
2dac754e | 1358 | static inline void enqueue_entity_load_avg(struct cfs_rq *cfs_rq, |
9ee474f5 PT |
1359 | struct sched_entity *se, |
1360 | int wakeup) {} | |
2dac754e | 1361 | static inline void dequeue_entity_load_avg(struct cfs_rq *cfs_rq, |
9ee474f5 PT |
1362 | struct sched_entity *se, |
1363 | int sleep) {} | |
aff3e498 PT |
1364 | static inline void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq, |
1365 | int force_update) {} | |
9d85f21c PT |
1366 | #endif |
1367 | ||
2396af69 | 1368 | static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 | 1369 | { |
bf0f6f24 | 1370 | #ifdef CONFIG_SCHEDSTATS |
e414314c PZ |
1371 | struct task_struct *tsk = NULL; |
1372 | ||
1373 | if (entity_is_task(se)) | |
1374 | tsk = task_of(se); | |
1375 | ||
41acab88 LDM |
1376 | if (se->statistics.sleep_start) { |
1377 | u64 delta = rq_of(cfs_rq)->clock - se->statistics.sleep_start; | |
bf0f6f24 IM |
1378 | |
1379 | if ((s64)delta < 0) | |
1380 | delta = 0; | |
1381 | ||
41acab88 LDM |
1382 | if (unlikely(delta > se->statistics.sleep_max)) |
1383 | se->statistics.sleep_max = delta; | |
bf0f6f24 | 1384 | |
8c79a045 | 1385 | se->statistics.sleep_start = 0; |
41acab88 | 1386 | se->statistics.sum_sleep_runtime += delta; |
9745512c | 1387 | |
768d0c27 | 1388 | if (tsk) { |
e414314c | 1389 | account_scheduler_latency(tsk, delta >> 10, 1); |
768d0c27 PZ |
1390 | trace_sched_stat_sleep(tsk, delta); |
1391 | } | |
bf0f6f24 | 1392 | } |
41acab88 LDM |
1393 | if (se->statistics.block_start) { |
1394 | u64 delta = rq_of(cfs_rq)->clock - se->statistics.block_start; | |
bf0f6f24 IM |
1395 | |
1396 | if ((s64)delta < 0) | |
1397 | delta = 0; | |
1398 | ||
41acab88 LDM |
1399 | if (unlikely(delta > se->statistics.block_max)) |
1400 | se->statistics.block_max = delta; | |
bf0f6f24 | 1401 | |
8c79a045 | 1402 | se->statistics.block_start = 0; |
41acab88 | 1403 | se->statistics.sum_sleep_runtime += delta; |
30084fbd | 1404 | |
e414314c | 1405 | if (tsk) { |
8f0dfc34 | 1406 | if (tsk->in_iowait) { |
41acab88 LDM |
1407 | se->statistics.iowait_sum += delta; |
1408 | se->statistics.iowait_count++; | |
768d0c27 | 1409 | trace_sched_stat_iowait(tsk, delta); |
8f0dfc34 AV |
1410 | } |
1411 | ||
b781a602 AV |
1412 | trace_sched_stat_blocked(tsk, delta); |
1413 | ||
e414314c PZ |
1414 | /* |
1415 | * Blocking time is in units of nanosecs, so shift by | |
1416 | * 20 to get a milliseconds-range estimation of the | |
1417 | * amount of time that the task spent sleeping: | |
1418 | */ | |
1419 | if (unlikely(prof_on == SLEEP_PROFILING)) { | |
1420 | profile_hits(SLEEP_PROFILING, | |
1421 | (void *)get_wchan(tsk), | |
1422 | delta >> 20); | |
1423 | } | |
1424 | account_scheduler_latency(tsk, delta >> 10, 0); | |
30084fbd | 1425 | } |
bf0f6f24 IM |
1426 | } |
1427 | #endif | |
1428 | } | |
1429 | ||
ddc97297 PZ |
1430 | static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se) |
1431 | { | |
1432 | #ifdef CONFIG_SCHED_DEBUG | |
1433 | s64 d = se->vruntime - cfs_rq->min_vruntime; | |
1434 | ||
1435 | if (d < 0) | |
1436 | d = -d; | |
1437 | ||
1438 | if (d > 3*sysctl_sched_latency) | |
1439 | schedstat_inc(cfs_rq, nr_spread_over); | |
1440 | #endif | |
1441 | } | |
1442 | ||
aeb73b04 PZ |
1443 | static void |
1444 | place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial) | |
1445 | { | |
1af5f730 | 1446 | u64 vruntime = cfs_rq->min_vruntime; |
94dfb5e7 | 1447 | |
2cb8600e PZ |
1448 | /* |
1449 | * The 'current' period is already promised to the current tasks, | |
1450 | * however the extra weight of the new task will slow them down a | |
1451 | * little, place the new task so that it fits in the slot that | |
1452 | * stays open at the end. | |
1453 | */ | |
94dfb5e7 | 1454 | if (initial && sched_feat(START_DEBIT)) |
f9c0b095 | 1455 | vruntime += sched_vslice(cfs_rq, se); |
aeb73b04 | 1456 | |
a2e7a7eb | 1457 | /* sleeps up to a single latency don't count. */ |
5ca9880c | 1458 | if (!initial) { |
a2e7a7eb | 1459 | unsigned long thresh = sysctl_sched_latency; |
a7be37ac | 1460 | |
a2e7a7eb MG |
1461 | /* |
1462 | * Halve their sleep time's effect, to allow | |
1463 | * for a gentler effect of sleepers: | |
1464 | */ | |
1465 | if (sched_feat(GENTLE_FAIR_SLEEPERS)) | |
1466 | thresh >>= 1; | |
51e0304c | 1467 | |
a2e7a7eb | 1468 | vruntime -= thresh; |
aeb73b04 PZ |
1469 | } |
1470 | ||
b5d9d734 MG |
1471 | /* ensure we never gain time by being placed backwards. */ |
1472 | vruntime = max_vruntime(se->vruntime, vruntime); | |
1473 | ||
67e9fb2a | 1474 | se->vruntime = vruntime; |
aeb73b04 PZ |
1475 | } |
1476 | ||
d3d9dc33 PT |
1477 | static void check_enqueue_throttle(struct cfs_rq *cfs_rq); |
1478 | ||
bf0f6f24 | 1479 | static void |
88ec22d3 | 1480 | enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
bf0f6f24 | 1481 | { |
88ec22d3 PZ |
1482 | /* |
1483 | * Update the normalized vruntime before updating min_vruntime | |
1484 | * through callig update_curr(). | |
1485 | */ | |
371fd7e7 | 1486 | if (!(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_WAKING)) |
88ec22d3 PZ |
1487 | se->vruntime += cfs_rq->min_vruntime; |
1488 | ||
bf0f6f24 | 1489 | /* |
a2a2d680 | 1490 | * Update run-time statistics of the 'current'. |
bf0f6f24 | 1491 | */ |
b7cc0896 | 1492 | update_curr(cfs_rq); |
d6b55918 | 1493 | update_cfs_load(cfs_rq, 0); |
9ee474f5 | 1494 | enqueue_entity_load_avg(cfs_rq, se, flags & ENQUEUE_WAKEUP); |
a992241d | 1495 | account_entity_enqueue(cfs_rq, se); |
6d5ab293 | 1496 | update_cfs_shares(cfs_rq); |
bf0f6f24 | 1497 | |
88ec22d3 | 1498 | if (flags & ENQUEUE_WAKEUP) { |
aeb73b04 | 1499 | place_entity(cfs_rq, se, 0); |
2396af69 | 1500 | enqueue_sleeper(cfs_rq, se); |
e9acbff6 | 1501 | } |
bf0f6f24 | 1502 | |
d2417e5a | 1503 | update_stats_enqueue(cfs_rq, se); |
ddc97297 | 1504 | check_spread(cfs_rq, se); |
83b699ed SV |
1505 | if (se != cfs_rq->curr) |
1506 | __enqueue_entity(cfs_rq, se); | |
2069dd75 | 1507 | se->on_rq = 1; |
3d4b47b4 | 1508 | |
d3d9dc33 | 1509 | if (cfs_rq->nr_running == 1) { |
3d4b47b4 | 1510 | list_add_leaf_cfs_rq(cfs_rq); |
d3d9dc33 PT |
1511 | check_enqueue_throttle(cfs_rq); |
1512 | } | |
bf0f6f24 IM |
1513 | } |
1514 | ||
2c13c919 | 1515 | static void __clear_buddies_last(struct sched_entity *se) |
2002c695 | 1516 | { |
2c13c919 RR |
1517 | for_each_sched_entity(se) { |
1518 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
1519 | if (cfs_rq->last == se) | |
1520 | cfs_rq->last = NULL; | |
1521 | else | |
1522 | break; | |
1523 | } | |
1524 | } | |
2002c695 | 1525 | |
2c13c919 RR |
1526 | static void __clear_buddies_next(struct sched_entity *se) |
1527 | { | |
1528 | for_each_sched_entity(se) { | |
1529 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
1530 | if (cfs_rq->next == se) | |
1531 | cfs_rq->next = NULL; | |
1532 | else | |
1533 | break; | |
1534 | } | |
2002c695 PZ |
1535 | } |
1536 | ||
ac53db59 RR |
1537 | static void __clear_buddies_skip(struct sched_entity *se) |
1538 | { | |
1539 | for_each_sched_entity(se) { | |
1540 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
1541 | if (cfs_rq->skip == se) | |
1542 | cfs_rq->skip = NULL; | |
1543 | else | |
1544 | break; | |
1545 | } | |
1546 | } | |
1547 | ||
a571bbea PZ |
1548 | static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se) |
1549 | { | |
2c13c919 RR |
1550 | if (cfs_rq->last == se) |
1551 | __clear_buddies_last(se); | |
1552 | ||
1553 | if (cfs_rq->next == se) | |
1554 | __clear_buddies_next(se); | |
ac53db59 RR |
1555 | |
1556 | if (cfs_rq->skip == se) | |
1557 | __clear_buddies_skip(se); | |
a571bbea PZ |
1558 | } |
1559 | ||
6c16a6dc | 1560 | static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq); |
d8b4986d | 1561 | |
bf0f6f24 | 1562 | static void |
371fd7e7 | 1563 | dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
bf0f6f24 | 1564 | { |
a2a2d680 DA |
1565 | /* |
1566 | * Update run-time statistics of the 'current'. | |
1567 | */ | |
1568 | update_curr(cfs_rq); | |
9ee474f5 | 1569 | dequeue_entity_load_avg(cfs_rq, se, flags & DEQUEUE_SLEEP); |
a2a2d680 | 1570 | |
19b6a2e3 | 1571 | update_stats_dequeue(cfs_rq, se); |
371fd7e7 | 1572 | if (flags & DEQUEUE_SLEEP) { |
67e9fb2a | 1573 | #ifdef CONFIG_SCHEDSTATS |
bf0f6f24 IM |
1574 | if (entity_is_task(se)) { |
1575 | struct task_struct *tsk = task_of(se); | |
1576 | ||
1577 | if (tsk->state & TASK_INTERRUPTIBLE) | |
41acab88 | 1578 | se->statistics.sleep_start = rq_of(cfs_rq)->clock; |
bf0f6f24 | 1579 | if (tsk->state & TASK_UNINTERRUPTIBLE) |
41acab88 | 1580 | se->statistics.block_start = rq_of(cfs_rq)->clock; |
bf0f6f24 | 1581 | } |
db36cc7d | 1582 | #endif |
67e9fb2a PZ |
1583 | } |
1584 | ||
2002c695 | 1585 | clear_buddies(cfs_rq, se); |
4793241b | 1586 | |
83b699ed | 1587 | if (se != cfs_rq->curr) |
30cfdcfc | 1588 | __dequeue_entity(cfs_rq, se); |
2069dd75 | 1589 | se->on_rq = 0; |
d6b55918 | 1590 | update_cfs_load(cfs_rq, 0); |
30cfdcfc | 1591 | account_entity_dequeue(cfs_rq, se); |
88ec22d3 PZ |
1592 | |
1593 | /* | |
1594 | * Normalize the entity after updating the min_vruntime because the | |
1595 | * update can refer to the ->curr item and we need to reflect this | |
1596 | * movement in our normalized position. | |
1597 | */ | |
371fd7e7 | 1598 | if (!(flags & DEQUEUE_SLEEP)) |
88ec22d3 | 1599 | se->vruntime -= cfs_rq->min_vruntime; |
1e876231 | 1600 | |
d8b4986d PT |
1601 | /* return excess runtime on last dequeue */ |
1602 | return_cfs_rq_runtime(cfs_rq); | |
1603 | ||
1e876231 PZ |
1604 | update_min_vruntime(cfs_rq); |
1605 | update_cfs_shares(cfs_rq); | |
bf0f6f24 IM |
1606 | } |
1607 | ||
1608 | /* | |
1609 | * Preempt the current task with a newly woken task if needed: | |
1610 | */ | |
7c92e54f | 1611 | static void |
2e09bf55 | 1612 | check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr) |
bf0f6f24 | 1613 | { |
11697830 | 1614 | unsigned long ideal_runtime, delta_exec; |
f4cfb33e WX |
1615 | struct sched_entity *se; |
1616 | s64 delta; | |
11697830 | 1617 | |
6d0f0ebd | 1618 | ideal_runtime = sched_slice(cfs_rq, curr); |
11697830 | 1619 | delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime; |
a9f3e2b5 | 1620 | if (delta_exec > ideal_runtime) { |
bf0f6f24 | 1621 | resched_task(rq_of(cfs_rq)->curr); |
a9f3e2b5 MG |
1622 | /* |
1623 | * The current task ran long enough, ensure it doesn't get | |
1624 | * re-elected due to buddy favours. | |
1625 | */ | |
1626 | clear_buddies(cfs_rq, curr); | |
f685ceac MG |
1627 | return; |
1628 | } | |
1629 | ||
1630 | /* | |
1631 | * Ensure that a task that missed wakeup preemption by a | |
1632 | * narrow margin doesn't have to wait for a full slice. | |
1633 | * This also mitigates buddy induced latencies under load. | |
1634 | */ | |
f685ceac MG |
1635 | if (delta_exec < sysctl_sched_min_granularity) |
1636 | return; | |
1637 | ||
f4cfb33e WX |
1638 | se = __pick_first_entity(cfs_rq); |
1639 | delta = curr->vruntime - se->vruntime; | |
f685ceac | 1640 | |
f4cfb33e WX |
1641 | if (delta < 0) |
1642 | return; | |
d7d82944 | 1643 | |
f4cfb33e WX |
1644 | if (delta > ideal_runtime) |
1645 | resched_task(rq_of(cfs_rq)->curr); | |
bf0f6f24 IM |
1646 | } |
1647 | ||
83b699ed | 1648 | static void |
8494f412 | 1649 | set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 | 1650 | { |
83b699ed SV |
1651 | /* 'current' is not kept within the tree. */ |
1652 | if (se->on_rq) { | |
1653 | /* | |
1654 | * Any task has to be enqueued before it get to execute on | |
1655 | * a CPU. So account for the time it spent waiting on the | |
1656 | * runqueue. | |
1657 | */ | |
1658 | update_stats_wait_end(cfs_rq, se); | |
1659 | __dequeue_entity(cfs_rq, se); | |
1660 | } | |
1661 | ||
79303e9e | 1662 | update_stats_curr_start(cfs_rq, se); |
429d43bc | 1663 | cfs_rq->curr = se; |
eba1ed4b IM |
1664 | #ifdef CONFIG_SCHEDSTATS |
1665 | /* | |
1666 | * Track our maximum slice length, if the CPU's load is at | |
1667 | * least twice that of our own weight (i.e. dont track it | |
1668 | * when there are only lesser-weight tasks around): | |
1669 | */ | |
495eca49 | 1670 | if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) { |
41acab88 | 1671 | se->statistics.slice_max = max(se->statistics.slice_max, |
eba1ed4b IM |
1672 | se->sum_exec_runtime - se->prev_sum_exec_runtime); |
1673 | } | |
1674 | #endif | |
4a55b450 | 1675 | se->prev_sum_exec_runtime = se->sum_exec_runtime; |
bf0f6f24 IM |
1676 | } |
1677 | ||
3f3a4904 PZ |
1678 | static int |
1679 | wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se); | |
1680 | ||
ac53db59 RR |
1681 | /* |
1682 | * Pick the next process, keeping these things in mind, in this order: | |
1683 | * 1) keep things fair between processes/task groups | |
1684 | * 2) pick the "next" process, since someone really wants that to run | |
1685 | * 3) pick the "last" process, for cache locality | |
1686 | * 4) do not run the "skip" process, if something else is available | |
1687 | */ | |
f4b6755f | 1688 | static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq) |
aa2ac252 | 1689 | { |
ac53db59 | 1690 | struct sched_entity *se = __pick_first_entity(cfs_rq); |
f685ceac | 1691 | struct sched_entity *left = se; |
f4b6755f | 1692 | |
ac53db59 RR |
1693 | /* |
1694 | * Avoid running the skip buddy, if running something else can | |
1695 | * be done without getting too unfair. | |
1696 | */ | |
1697 | if (cfs_rq->skip == se) { | |
1698 | struct sched_entity *second = __pick_next_entity(se); | |
1699 | if (second && wakeup_preempt_entity(second, left) < 1) | |
1700 | se = second; | |
1701 | } | |
aa2ac252 | 1702 | |
f685ceac MG |
1703 | /* |
1704 | * Prefer last buddy, try to return the CPU to a preempted task. | |
1705 | */ | |
1706 | if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1) | |
1707 | se = cfs_rq->last; | |
1708 | ||
ac53db59 RR |
1709 | /* |
1710 | * Someone really wants this to run. If it's not unfair, run it. | |
1711 | */ | |
1712 | if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1) | |
1713 | se = cfs_rq->next; | |
1714 | ||
f685ceac | 1715 | clear_buddies(cfs_rq, se); |
4793241b PZ |
1716 | |
1717 | return se; | |
aa2ac252 PZ |
1718 | } |
1719 | ||
d3d9dc33 PT |
1720 | static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq); |
1721 | ||
ab6cde26 | 1722 | static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev) |
bf0f6f24 IM |
1723 | { |
1724 | /* | |
1725 | * If still on the runqueue then deactivate_task() | |
1726 | * was not called and update_curr() has to be done: | |
1727 | */ | |
1728 | if (prev->on_rq) | |
b7cc0896 | 1729 | update_curr(cfs_rq); |
bf0f6f24 | 1730 | |
d3d9dc33 PT |
1731 | /* throttle cfs_rqs exceeding runtime */ |
1732 | check_cfs_rq_runtime(cfs_rq); | |
1733 | ||
ddc97297 | 1734 | check_spread(cfs_rq, prev); |
30cfdcfc | 1735 | if (prev->on_rq) { |
5870db5b | 1736 | update_stats_wait_start(cfs_rq, prev); |
30cfdcfc DA |
1737 | /* Put 'current' back into the tree. */ |
1738 | __enqueue_entity(cfs_rq, prev); | |
9d85f21c | 1739 | /* in !on_rq case, update occurred at dequeue */ |
9ee474f5 | 1740 | update_entity_load_avg(prev, 1); |
30cfdcfc | 1741 | } |
429d43bc | 1742 | cfs_rq->curr = NULL; |
bf0f6f24 IM |
1743 | } |
1744 | ||
8f4d37ec PZ |
1745 | static void |
1746 | entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued) | |
bf0f6f24 | 1747 | { |
bf0f6f24 | 1748 | /* |
30cfdcfc | 1749 | * Update run-time statistics of the 'current'. |
bf0f6f24 | 1750 | */ |
30cfdcfc | 1751 | update_curr(cfs_rq); |
bf0f6f24 | 1752 | |
9d85f21c PT |
1753 | /* |
1754 | * Ensure that runnable average is periodically updated. | |
1755 | */ | |
9ee474f5 | 1756 | update_entity_load_avg(curr, 1); |
aff3e498 | 1757 | update_cfs_rq_blocked_load(cfs_rq, 1); |
9d85f21c | 1758 | |
43365bd7 PT |
1759 | /* |
1760 | * Update share accounting for long-running entities. | |
1761 | */ | |
1762 | update_entity_shares_tick(cfs_rq); | |
1763 | ||
8f4d37ec PZ |
1764 | #ifdef CONFIG_SCHED_HRTICK |
1765 | /* | |
1766 | * queued ticks are scheduled to match the slice, so don't bother | |
1767 | * validating it and just reschedule. | |
1768 | */ | |
983ed7a6 HH |
1769 | if (queued) { |
1770 | resched_task(rq_of(cfs_rq)->curr); | |
1771 | return; | |
1772 | } | |
8f4d37ec PZ |
1773 | /* |
1774 | * don't let the period tick interfere with the hrtick preemption | |
1775 | */ | |
1776 | if (!sched_feat(DOUBLE_TICK) && | |
1777 | hrtimer_active(&rq_of(cfs_rq)->hrtick_timer)) | |
1778 | return; | |
1779 | #endif | |
1780 | ||
2c2efaed | 1781 | if (cfs_rq->nr_running > 1) |
2e09bf55 | 1782 | check_preempt_tick(cfs_rq, curr); |
bf0f6f24 IM |
1783 | } |
1784 | ||
ab84d31e PT |
1785 | |
1786 | /************************************************** | |
1787 | * CFS bandwidth control machinery | |
1788 | */ | |
1789 | ||
1790 | #ifdef CONFIG_CFS_BANDWIDTH | |
029632fb PZ |
1791 | |
1792 | #ifdef HAVE_JUMP_LABEL | |
c5905afb | 1793 | static struct static_key __cfs_bandwidth_used; |
029632fb PZ |
1794 | |
1795 | static inline bool cfs_bandwidth_used(void) | |
1796 | { | |
c5905afb | 1797 | return static_key_false(&__cfs_bandwidth_used); |
029632fb PZ |
1798 | } |
1799 | ||
1800 | void account_cfs_bandwidth_used(int enabled, int was_enabled) | |
1801 | { | |
1802 | /* only need to count groups transitioning between enabled/!enabled */ | |
1803 | if (enabled && !was_enabled) | |
c5905afb | 1804 | static_key_slow_inc(&__cfs_bandwidth_used); |
029632fb | 1805 | else if (!enabled && was_enabled) |
c5905afb | 1806 | static_key_slow_dec(&__cfs_bandwidth_used); |
029632fb PZ |
1807 | } |
1808 | #else /* HAVE_JUMP_LABEL */ | |
1809 | static bool cfs_bandwidth_used(void) | |
1810 | { | |
1811 | return true; | |
1812 | } | |
1813 | ||
1814 | void account_cfs_bandwidth_used(int enabled, int was_enabled) {} | |
1815 | #endif /* HAVE_JUMP_LABEL */ | |
1816 | ||
ab84d31e PT |
1817 | /* |
1818 | * default period for cfs group bandwidth. | |
1819 | * default: 0.1s, units: nanoseconds | |
1820 | */ | |
1821 | static inline u64 default_cfs_period(void) | |
1822 | { | |
1823 | return 100000000ULL; | |
1824 | } | |
ec12cb7f PT |
1825 | |
1826 | static inline u64 sched_cfs_bandwidth_slice(void) | |
1827 | { | |
1828 | return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC; | |
1829 | } | |
1830 | ||
a9cf55b2 PT |
1831 | /* |
1832 | * Replenish runtime according to assigned quota and update expiration time. | |
1833 | * We use sched_clock_cpu directly instead of rq->clock to avoid adding | |
1834 | * additional synchronization around rq->lock. | |
1835 | * | |
1836 | * requires cfs_b->lock | |
1837 | */ | |
029632fb | 1838 | void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b) |
a9cf55b2 PT |
1839 | { |
1840 | u64 now; | |
1841 | ||
1842 | if (cfs_b->quota == RUNTIME_INF) | |
1843 | return; | |
1844 | ||
1845 | now = sched_clock_cpu(smp_processor_id()); | |
1846 | cfs_b->runtime = cfs_b->quota; | |
1847 | cfs_b->runtime_expires = now + ktime_to_ns(cfs_b->period); | |
1848 | } | |
1849 | ||
029632fb PZ |
1850 | static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg) |
1851 | { | |
1852 | return &tg->cfs_bandwidth; | |
1853 | } | |
1854 | ||
f1b17280 PT |
1855 | /* rq->task_clock normalized against any time this cfs_rq has spent throttled */ |
1856 | static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq) | |
1857 | { | |
1858 | if (unlikely(cfs_rq->throttle_count)) | |
1859 | return cfs_rq->throttled_clock_task; | |
1860 | ||
1861 | return rq_of(cfs_rq)->clock_task - cfs_rq->throttled_clock_task_time; | |
1862 | } | |
1863 | ||
85dac906 PT |
1864 | /* returns 0 on failure to allocate runtime */ |
1865 | static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
ec12cb7f PT |
1866 | { |
1867 | struct task_group *tg = cfs_rq->tg; | |
1868 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg); | |
a9cf55b2 | 1869 | u64 amount = 0, min_amount, expires; |
ec12cb7f PT |
1870 | |
1871 | /* note: this is a positive sum as runtime_remaining <= 0 */ | |
1872 | min_amount = sched_cfs_bandwidth_slice() - cfs_rq->runtime_remaining; | |
1873 | ||
1874 | raw_spin_lock(&cfs_b->lock); | |
1875 | if (cfs_b->quota == RUNTIME_INF) | |
1876 | amount = min_amount; | |
58088ad0 | 1877 | else { |
a9cf55b2 PT |
1878 | /* |
1879 | * If the bandwidth pool has become inactive, then at least one | |
1880 | * period must have elapsed since the last consumption. | |
1881 | * Refresh the global state and ensure bandwidth timer becomes | |
1882 | * active. | |
1883 | */ | |
1884 | if (!cfs_b->timer_active) { | |
1885 | __refill_cfs_bandwidth_runtime(cfs_b); | |
58088ad0 | 1886 | __start_cfs_bandwidth(cfs_b); |
a9cf55b2 | 1887 | } |
58088ad0 PT |
1888 | |
1889 | if (cfs_b->runtime > 0) { | |
1890 | amount = min(cfs_b->runtime, min_amount); | |
1891 | cfs_b->runtime -= amount; | |
1892 | cfs_b->idle = 0; | |
1893 | } | |
ec12cb7f | 1894 | } |
a9cf55b2 | 1895 | expires = cfs_b->runtime_expires; |
ec12cb7f PT |
1896 | raw_spin_unlock(&cfs_b->lock); |
1897 | ||
1898 | cfs_rq->runtime_remaining += amount; | |
a9cf55b2 PT |
1899 | /* |
1900 | * we may have advanced our local expiration to account for allowed | |
1901 | * spread between our sched_clock and the one on which runtime was | |
1902 | * issued. | |
1903 | */ | |
1904 | if ((s64)(expires - cfs_rq->runtime_expires) > 0) | |
1905 | cfs_rq->runtime_expires = expires; | |
85dac906 PT |
1906 | |
1907 | return cfs_rq->runtime_remaining > 0; | |
ec12cb7f PT |
1908 | } |
1909 | ||
a9cf55b2 PT |
1910 | /* |
1911 | * Note: This depends on the synchronization provided by sched_clock and the | |
1912 | * fact that rq->clock snapshots this value. | |
1913 | */ | |
1914 | static void expire_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
ec12cb7f | 1915 | { |
a9cf55b2 PT |
1916 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); |
1917 | struct rq *rq = rq_of(cfs_rq); | |
1918 | ||
1919 | /* if the deadline is ahead of our clock, nothing to do */ | |
1920 | if (likely((s64)(rq->clock - cfs_rq->runtime_expires) < 0)) | |
ec12cb7f PT |
1921 | return; |
1922 | ||
a9cf55b2 PT |
1923 | if (cfs_rq->runtime_remaining < 0) |
1924 | return; | |
1925 | ||
1926 | /* | |
1927 | * If the local deadline has passed we have to consider the | |
1928 | * possibility that our sched_clock is 'fast' and the global deadline | |
1929 | * has not truly expired. | |
1930 | * | |
1931 | * Fortunately we can check determine whether this the case by checking | |
1932 | * whether the global deadline has advanced. | |
1933 | */ | |
1934 | ||
1935 | if ((s64)(cfs_rq->runtime_expires - cfs_b->runtime_expires) >= 0) { | |
1936 | /* extend local deadline, drift is bounded above by 2 ticks */ | |
1937 | cfs_rq->runtime_expires += TICK_NSEC; | |
1938 | } else { | |
1939 | /* global deadline is ahead, expiration has passed */ | |
1940 | cfs_rq->runtime_remaining = 0; | |
1941 | } | |
1942 | } | |
1943 | ||
1944 | static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq, | |
1945 | unsigned long delta_exec) | |
1946 | { | |
1947 | /* dock delta_exec before expiring quota (as it could span periods) */ | |
ec12cb7f | 1948 | cfs_rq->runtime_remaining -= delta_exec; |
a9cf55b2 PT |
1949 | expire_cfs_rq_runtime(cfs_rq); |
1950 | ||
1951 | if (likely(cfs_rq->runtime_remaining > 0)) | |
ec12cb7f PT |
1952 | return; |
1953 | ||
85dac906 PT |
1954 | /* |
1955 | * if we're unable to extend our runtime we resched so that the active | |
1956 | * hierarchy can be throttled | |
1957 | */ | |
1958 | if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr)) | |
1959 | resched_task(rq_of(cfs_rq)->curr); | |
ec12cb7f PT |
1960 | } |
1961 | ||
6c16a6dc PZ |
1962 | static __always_inline |
1963 | void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec) | |
ec12cb7f | 1964 | { |
56f570e5 | 1965 | if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled) |
ec12cb7f PT |
1966 | return; |
1967 | ||
1968 | __account_cfs_rq_runtime(cfs_rq, delta_exec); | |
1969 | } | |
1970 | ||
85dac906 PT |
1971 | static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq) |
1972 | { | |
56f570e5 | 1973 | return cfs_bandwidth_used() && cfs_rq->throttled; |
85dac906 PT |
1974 | } |
1975 | ||
64660c86 PT |
1976 | /* check whether cfs_rq, or any parent, is throttled */ |
1977 | static inline int throttled_hierarchy(struct cfs_rq *cfs_rq) | |
1978 | { | |
56f570e5 | 1979 | return cfs_bandwidth_used() && cfs_rq->throttle_count; |
64660c86 PT |
1980 | } |
1981 | ||
1982 | /* | |
1983 | * Ensure that neither of the group entities corresponding to src_cpu or | |
1984 | * dest_cpu are members of a throttled hierarchy when performing group | |
1985 | * load-balance operations. | |
1986 | */ | |
1987 | static inline int throttled_lb_pair(struct task_group *tg, | |
1988 | int src_cpu, int dest_cpu) | |
1989 | { | |
1990 | struct cfs_rq *src_cfs_rq, *dest_cfs_rq; | |
1991 | ||
1992 | src_cfs_rq = tg->cfs_rq[src_cpu]; | |
1993 | dest_cfs_rq = tg->cfs_rq[dest_cpu]; | |
1994 | ||
1995 | return throttled_hierarchy(src_cfs_rq) || | |
1996 | throttled_hierarchy(dest_cfs_rq); | |
1997 | } | |
1998 | ||
1999 | /* updated child weight may affect parent so we have to do this bottom up */ | |
2000 | static int tg_unthrottle_up(struct task_group *tg, void *data) | |
2001 | { | |
2002 | struct rq *rq = data; | |
2003 | struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; | |
2004 | ||
2005 | cfs_rq->throttle_count--; | |
2006 | #ifdef CONFIG_SMP | |
2007 | if (!cfs_rq->throttle_count) { | |
2008 | u64 delta = rq->clock_task - cfs_rq->load_stamp; | |
2009 | ||
2010 | /* leaving throttled state, advance shares averaging windows */ | |
2011 | cfs_rq->load_stamp += delta; | |
2012 | cfs_rq->load_last += delta; | |
2013 | ||
f1b17280 PT |
2014 | /* adjust cfs_rq_clock_task() */ |
2015 | cfs_rq->throttled_clock_task_time += rq->clock_task - | |
2016 | cfs_rq->throttled_clock_task; | |
2017 | ||
64660c86 PT |
2018 | /* update entity weight now that we are on_rq again */ |
2019 | update_cfs_shares(cfs_rq); | |
2020 | } | |
2021 | #endif | |
2022 | ||
2023 | return 0; | |
2024 | } | |
2025 | ||
2026 | static int tg_throttle_down(struct task_group *tg, void *data) | |
2027 | { | |
2028 | struct rq *rq = data; | |
2029 | struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; | |
2030 | ||
2031 | /* group is entering throttled state, record last load */ | |
f1b17280 | 2032 | if (!cfs_rq->throttle_count) { |
64660c86 | 2033 | update_cfs_load(cfs_rq, 0); |
f1b17280 PT |
2034 | cfs_rq->throttled_clock_task = rq->clock_task; |
2035 | } | |
64660c86 PT |
2036 | cfs_rq->throttle_count++; |
2037 | ||
2038 | return 0; | |
2039 | } | |
2040 | ||
d3d9dc33 | 2041 | static void throttle_cfs_rq(struct cfs_rq *cfs_rq) |
85dac906 PT |
2042 | { |
2043 | struct rq *rq = rq_of(cfs_rq); | |
2044 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); | |
2045 | struct sched_entity *se; | |
2046 | long task_delta, dequeue = 1; | |
2047 | ||
2048 | se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))]; | |
2049 | ||
f1b17280 | 2050 | /* freeze hierarchy runnable averages while throttled */ |
64660c86 PT |
2051 | rcu_read_lock(); |
2052 | walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq); | |
2053 | rcu_read_unlock(); | |
85dac906 PT |
2054 | |
2055 | task_delta = cfs_rq->h_nr_running; | |
2056 | for_each_sched_entity(se) { | |
2057 | struct cfs_rq *qcfs_rq = cfs_rq_of(se); | |
2058 | /* throttled entity or throttle-on-deactivate */ | |
2059 | if (!se->on_rq) | |
2060 | break; | |
2061 | ||
2062 | if (dequeue) | |
2063 | dequeue_entity(qcfs_rq, se, DEQUEUE_SLEEP); | |
2064 | qcfs_rq->h_nr_running -= task_delta; | |
2065 | ||
2066 | if (qcfs_rq->load.weight) | |
2067 | dequeue = 0; | |
2068 | } | |
2069 | ||
2070 | if (!se) | |
2071 | rq->nr_running -= task_delta; | |
2072 | ||
2073 | cfs_rq->throttled = 1; | |
f1b17280 | 2074 | cfs_rq->throttled_clock = rq->clock; |
85dac906 PT |
2075 | raw_spin_lock(&cfs_b->lock); |
2076 | list_add_tail_rcu(&cfs_rq->throttled_list, &cfs_b->throttled_cfs_rq); | |
2077 | raw_spin_unlock(&cfs_b->lock); | |
2078 | } | |
2079 | ||
029632fb | 2080 | void unthrottle_cfs_rq(struct cfs_rq *cfs_rq) |
671fd9da PT |
2081 | { |
2082 | struct rq *rq = rq_of(cfs_rq); | |
2083 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); | |
2084 | struct sched_entity *se; | |
2085 | int enqueue = 1; | |
2086 | long task_delta; | |
2087 | ||
2088 | se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))]; | |
2089 | ||
2090 | cfs_rq->throttled = 0; | |
2091 | raw_spin_lock(&cfs_b->lock); | |
f1b17280 | 2092 | cfs_b->throttled_time += rq->clock - cfs_rq->throttled_clock; |
671fd9da PT |
2093 | list_del_rcu(&cfs_rq->throttled_list); |
2094 | raw_spin_unlock(&cfs_b->lock); | |
2095 | ||
64660c86 PT |
2096 | update_rq_clock(rq); |
2097 | /* update hierarchical throttle state */ | |
2098 | walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq); | |
2099 | ||
671fd9da PT |
2100 | if (!cfs_rq->load.weight) |
2101 | return; | |
2102 | ||
2103 | task_delta = cfs_rq->h_nr_running; | |
2104 | for_each_sched_entity(se) { | |
2105 | if (se->on_rq) | |
2106 | enqueue = 0; | |
2107 | ||
2108 | cfs_rq = cfs_rq_of(se); | |
2109 | if (enqueue) | |
2110 | enqueue_entity(cfs_rq, se, ENQUEUE_WAKEUP); | |
2111 | cfs_rq->h_nr_running += task_delta; | |
2112 | ||
2113 | if (cfs_rq_throttled(cfs_rq)) | |
2114 | break; | |
2115 | } | |
2116 | ||
2117 | if (!se) | |
2118 | rq->nr_running += task_delta; | |
2119 | ||
2120 | /* determine whether we need to wake up potentially idle cpu */ | |
2121 | if (rq->curr == rq->idle && rq->cfs.nr_running) | |
2122 | resched_task(rq->curr); | |
2123 | } | |
2124 | ||
2125 | static u64 distribute_cfs_runtime(struct cfs_bandwidth *cfs_b, | |
2126 | u64 remaining, u64 expires) | |
2127 | { | |
2128 | struct cfs_rq *cfs_rq; | |
2129 | u64 runtime = remaining; | |
2130 | ||
2131 | rcu_read_lock(); | |
2132 | list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq, | |
2133 | throttled_list) { | |
2134 | struct rq *rq = rq_of(cfs_rq); | |
2135 | ||
2136 | raw_spin_lock(&rq->lock); | |
2137 | if (!cfs_rq_throttled(cfs_rq)) | |
2138 | goto next; | |
2139 | ||
2140 | runtime = -cfs_rq->runtime_remaining + 1; | |
2141 | if (runtime > remaining) | |
2142 | runtime = remaining; | |
2143 | remaining -= runtime; | |
2144 | ||
2145 | cfs_rq->runtime_remaining += runtime; | |
2146 | cfs_rq->runtime_expires = expires; | |
2147 | ||
2148 | /* we check whether we're throttled above */ | |
2149 | if (cfs_rq->runtime_remaining > 0) | |
2150 | unthrottle_cfs_rq(cfs_rq); | |
2151 | ||
2152 | next: | |
2153 | raw_spin_unlock(&rq->lock); | |
2154 | ||
2155 | if (!remaining) | |
2156 | break; | |
2157 | } | |
2158 | rcu_read_unlock(); | |
2159 | ||
2160 | return remaining; | |
2161 | } | |
2162 | ||
58088ad0 PT |
2163 | /* |
2164 | * Responsible for refilling a task_group's bandwidth and unthrottling its | |
2165 | * cfs_rqs as appropriate. If there has been no activity within the last | |
2166 | * period the timer is deactivated until scheduling resumes; cfs_b->idle is | |
2167 | * used to track this state. | |
2168 | */ | |
2169 | static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun) | |
2170 | { | |
671fd9da PT |
2171 | u64 runtime, runtime_expires; |
2172 | int idle = 1, throttled; | |
58088ad0 PT |
2173 | |
2174 | raw_spin_lock(&cfs_b->lock); | |
2175 | /* no need to continue the timer with no bandwidth constraint */ | |
2176 | if (cfs_b->quota == RUNTIME_INF) | |
2177 | goto out_unlock; | |
2178 | ||
671fd9da PT |
2179 | throttled = !list_empty(&cfs_b->throttled_cfs_rq); |
2180 | /* idle depends on !throttled (for the case of a large deficit) */ | |
2181 | idle = cfs_b->idle && !throttled; | |
e8da1b18 | 2182 | cfs_b->nr_periods += overrun; |
671fd9da | 2183 | |
a9cf55b2 PT |
2184 | /* if we're going inactive then everything else can be deferred */ |
2185 | if (idle) | |
2186 | goto out_unlock; | |
2187 | ||
2188 | __refill_cfs_bandwidth_runtime(cfs_b); | |
2189 | ||
671fd9da PT |
2190 | if (!throttled) { |
2191 | /* mark as potentially idle for the upcoming period */ | |
2192 | cfs_b->idle = 1; | |
2193 | goto out_unlock; | |
2194 | } | |
2195 | ||
e8da1b18 NR |
2196 | /* account preceding periods in which throttling occurred */ |
2197 | cfs_b->nr_throttled += overrun; | |
2198 | ||
671fd9da PT |
2199 | /* |
2200 | * There are throttled entities so we must first use the new bandwidth | |
2201 | * to unthrottle them before making it generally available. This | |
2202 | * ensures that all existing debts will be paid before a new cfs_rq is | |
2203 | * allowed to run. | |
2204 | */ | |
2205 | runtime = cfs_b->runtime; | |
2206 | runtime_expires = cfs_b->runtime_expires; | |
2207 | cfs_b->runtime = 0; | |
2208 | ||
2209 | /* | |
2210 | * This check is repeated as we are holding onto the new bandwidth | |
2211 | * while we unthrottle. This can potentially race with an unthrottled | |
2212 | * group trying to acquire new bandwidth from the global pool. | |
2213 | */ | |
2214 | while (throttled && runtime > 0) { | |
2215 | raw_spin_unlock(&cfs_b->lock); | |
2216 | /* we can't nest cfs_b->lock while distributing bandwidth */ | |
2217 | runtime = distribute_cfs_runtime(cfs_b, runtime, | |
2218 | runtime_expires); | |
2219 | raw_spin_lock(&cfs_b->lock); | |
2220 | ||
2221 | throttled = !list_empty(&cfs_b->throttled_cfs_rq); | |
2222 | } | |
58088ad0 | 2223 | |
671fd9da PT |
2224 | /* return (any) remaining runtime */ |
2225 | cfs_b->runtime = runtime; | |
2226 | /* | |
2227 | * While we are ensured activity in the period following an | |
2228 | * unthrottle, this also covers the case in which the new bandwidth is | |
2229 | * insufficient to cover the existing bandwidth deficit. (Forcing the | |
2230 | * timer to remain active while there are any throttled entities.) | |
2231 | */ | |
2232 | cfs_b->idle = 0; | |
58088ad0 PT |
2233 | out_unlock: |
2234 | if (idle) | |
2235 | cfs_b->timer_active = 0; | |
2236 | raw_spin_unlock(&cfs_b->lock); | |
2237 | ||
2238 | return idle; | |
2239 | } | |
d3d9dc33 | 2240 | |
d8b4986d PT |
2241 | /* a cfs_rq won't donate quota below this amount */ |
2242 | static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC; | |
2243 | /* minimum remaining period time to redistribute slack quota */ | |
2244 | static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC; | |
2245 | /* how long we wait to gather additional slack before distributing */ | |
2246 | static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC; | |
2247 | ||
2248 | /* are we near the end of the current quota period? */ | |
2249 | static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire) | |
2250 | { | |
2251 | struct hrtimer *refresh_timer = &cfs_b->period_timer; | |
2252 | u64 remaining; | |
2253 | ||
2254 | /* if the call-back is running a quota refresh is already occurring */ | |
2255 | if (hrtimer_callback_running(refresh_timer)) | |
2256 | return 1; | |
2257 | ||
2258 | /* is a quota refresh about to occur? */ | |
2259 | remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer)); | |
2260 | if (remaining < min_expire) | |
2261 | return 1; | |
2262 | ||
2263 | return 0; | |
2264 | } | |
2265 | ||
2266 | static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b) | |
2267 | { | |
2268 | u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration; | |
2269 | ||
2270 | /* if there's a quota refresh soon don't bother with slack */ | |
2271 | if (runtime_refresh_within(cfs_b, min_left)) | |
2272 | return; | |
2273 | ||
2274 | start_bandwidth_timer(&cfs_b->slack_timer, | |
2275 | ns_to_ktime(cfs_bandwidth_slack_period)); | |
2276 | } | |
2277 | ||
2278 | /* we know any runtime found here is valid as update_curr() precedes return */ | |
2279 | static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
2280 | { | |
2281 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); | |
2282 | s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime; | |
2283 | ||
2284 | if (slack_runtime <= 0) | |
2285 | return; | |
2286 | ||
2287 | raw_spin_lock(&cfs_b->lock); | |
2288 | if (cfs_b->quota != RUNTIME_INF && | |
2289 | cfs_rq->runtime_expires == cfs_b->runtime_expires) { | |
2290 | cfs_b->runtime += slack_runtime; | |
2291 | ||
2292 | /* we are under rq->lock, defer unthrottling using a timer */ | |
2293 | if (cfs_b->runtime > sched_cfs_bandwidth_slice() && | |
2294 | !list_empty(&cfs_b->throttled_cfs_rq)) | |
2295 | start_cfs_slack_bandwidth(cfs_b); | |
2296 | } | |
2297 | raw_spin_unlock(&cfs_b->lock); | |
2298 | ||
2299 | /* even if it's not valid for return we don't want to try again */ | |
2300 | cfs_rq->runtime_remaining -= slack_runtime; | |
2301 | } | |
2302 | ||
2303 | static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
2304 | { | |
56f570e5 PT |
2305 | if (!cfs_bandwidth_used()) |
2306 | return; | |
2307 | ||
fccfdc6f | 2308 | if (!cfs_rq->runtime_enabled || cfs_rq->nr_running) |
d8b4986d PT |
2309 | return; |
2310 | ||
2311 | __return_cfs_rq_runtime(cfs_rq); | |
2312 | } | |
2313 | ||
2314 | /* | |
2315 | * This is done with a timer (instead of inline with bandwidth return) since | |
2316 | * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs. | |
2317 | */ | |
2318 | static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b) | |
2319 | { | |
2320 | u64 runtime = 0, slice = sched_cfs_bandwidth_slice(); | |
2321 | u64 expires; | |
2322 | ||
2323 | /* confirm we're still not at a refresh boundary */ | |
2324 | if (runtime_refresh_within(cfs_b, min_bandwidth_expiration)) | |
2325 | return; | |
2326 | ||
2327 | raw_spin_lock(&cfs_b->lock); | |
2328 | if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) { | |
2329 | runtime = cfs_b->runtime; | |
2330 | cfs_b->runtime = 0; | |
2331 | } | |
2332 | expires = cfs_b->runtime_expires; | |
2333 | raw_spin_unlock(&cfs_b->lock); | |
2334 | ||
2335 | if (!runtime) | |
2336 | return; | |
2337 | ||
2338 | runtime = distribute_cfs_runtime(cfs_b, runtime, expires); | |
2339 | ||
2340 | raw_spin_lock(&cfs_b->lock); | |
2341 | if (expires == cfs_b->runtime_expires) | |
2342 | cfs_b->runtime = runtime; | |
2343 | raw_spin_unlock(&cfs_b->lock); | |
2344 | } | |
2345 | ||
d3d9dc33 PT |
2346 | /* |
2347 | * When a group wakes up we want to make sure that its quota is not already | |
2348 | * expired/exceeded, otherwise it may be allowed to steal additional ticks of | |
2349 | * runtime as update_curr() throttling can not not trigger until it's on-rq. | |
2350 | */ | |
2351 | static void check_enqueue_throttle(struct cfs_rq *cfs_rq) | |
2352 | { | |
56f570e5 PT |
2353 | if (!cfs_bandwidth_used()) |
2354 | return; | |
2355 | ||
d3d9dc33 PT |
2356 | /* an active group must be handled by the update_curr()->put() path */ |
2357 | if (!cfs_rq->runtime_enabled || cfs_rq->curr) | |
2358 | return; | |
2359 | ||
2360 | /* ensure the group is not already throttled */ | |
2361 | if (cfs_rq_throttled(cfs_rq)) | |
2362 | return; | |
2363 | ||
2364 | /* update runtime allocation */ | |
2365 | account_cfs_rq_runtime(cfs_rq, 0); | |
2366 | if (cfs_rq->runtime_remaining <= 0) | |
2367 | throttle_cfs_rq(cfs_rq); | |
2368 | } | |
2369 | ||
2370 | /* conditionally throttle active cfs_rq's from put_prev_entity() */ | |
2371 | static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
2372 | { | |
56f570e5 PT |
2373 | if (!cfs_bandwidth_used()) |
2374 | return; | |
2375 | ||
d3d9dc33 PT |
2376 | if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0)) |
2377 | return; | |
2378 | ||
2379 | /* | |
2380 | * it's possible for a throttled entity to be forced into a running | |
2381 | * state (e.g. set_curr_task), in this case we're finished. | |
2382 | */ | |
2383 | if (cfs_rq_throttled(cfs_rq)) | |
2384 | return; | |
2385 | ||
2386 | throttle_cfs_rq(cfs_rq); | |
2387 | } | |
029632fb PZ |
2388 | |
2389 | static inline u64 default_cfs_period(void); | |
2390 | static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun); | |
2391 | static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b); | |
2392 | ||
2393 | static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer) | |
2394 | { | |
2395 | struct cfs_bandwidth *cfs_b = | |
2396 | container_of(timer, struct cfs_bandwidth, slack_timer); | |
2397 | do_sched_cfs_slack_timer(cfs_b); | |
2398 | ||
2399 | return HRTIMER_NORESTART; | |
2400 | } | |
2401 | ||
2402 | static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer) | |
2403 | { | |
2404 | struct cfs_bandwidth *cfs_b = | |
2405 | container_of(timer, struct cfs_bandwidth, period_timer); | |
2406 | ktime_t now; | |
2407 | int overrun; | |
2408 | int idle = 0; | |
2409 | ||
2410 | for (;;) { | |
2411 | now = hrtimer_cb_get_time(timer); | |
2412 | overrun = hrtimer_forward(timer, now, cfs_b->period); | |
2413 | ||
2414 | if (!overrun) | |
2415 | break; | |
2416 | ||
2417 | idle = do_sched_cfs_period_timer(cfs_b, overrun); | |
2418 | } | |
2419 | ||
2420 | return idle ? HRTIMER_NORESTART : HRTIMER_RESTART; | |
2421 | } | |
2422 | ||
2423 | void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) | |
2424 | { | |
2425 | raw_spin_lock_init(&cfs_b->lock); | |
2426 | cfs_b->runtime = 0; | |
2427 | cfs_b->quota = RUNTIME_INF; | |
2428 | cfs_b->period = ns_to_ktime(default_cfs_period()); | |
2429 | ||
2430 | INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq); | |
2431 | hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); | |
2432 | cfs_b->period_timer.function = sched_cfs_period_timer; | |
2433 | hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); | |
2434 | cfs_b->slack_timer.function = sched_cfs_slack_timer; | |
2435 | } | |
2436 | ||
2437 | static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
2438 | { | |
2439 | cfs_rq->runtime_enabled = 0; | |
2440 | INIT_LIST_HEAD(&cfs_rq->throttled_list); | |
2441 | } | |
2442 | ||
2443 | /* requires cfs_b->lock, may release to reprogram timer */ | |
2444 | void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b) | |
2445 | { | |
2446 | /* | |
2447 | * The timer may be active because we're trying to set a new bandwidth | |
2448 | * period or because we're racing with the tear-down path | |
2449 | * (timer_active==0 becomes visible before the hrtimer call-back | |
2450 | * terminates). In either case we ensure that it's re-programmed | |
2451 | */ | |
2452 | while (unlikely(hrtimer_active(&cfs_b->period_timer))) { | |
2453 | raw_spin_unlock(&cfs_b->lock); | |
2454 | /* ensure cfs_b->lock is available while we wait */ | |
2455 | hrtimer_cancel(&cfs_b->period_timer); | |
2456 | ||
2457 | raw_spin_lock(&cfs_b->lock); | |
2458 | /* if someone else restarted the timer then we're done */ | |
2459 | if (cfs_b->timer_active) | |
2460 | return; | |
2461 | } | |
2462 | ||
2463 | cfs_b->timer_active = 1; | |
2464 | start_bandwidth_timer(&cfs_b->period_timer, cfs_b->period); | |
2465 | } | |
2466 | ||
2467 | static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) | |
2468 | { | |
2469 | hrtimer_cancel(&cfs_b->period_timer); | |
2470 | hrtimer_cancel(&cfs_b->slack_timer); | |
2471 | } | |
2472 | ||
a4c96ae3 | 2473 | static void unthrottle_offline_cfs_rqs(struct rq *rq) |
029632fb PZ |
2474 | { |
2475 | struct cfs_rq *cfs_rq; | |
2476 | ||
2477 | for_each_leaf_cfs_rq(rq, cfs_rq) { | |
2478 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); | |
2479 | ||
2480 | if (!cfs_rq->runtime_enabled) | |
2481 | continue; | |
2482 | ||
2483 | /* | |
2484 | * clock_task is not advancing so we just need to make sure | |
2485 | * there's some valid quota amount | |
2486 | */ | |
2487 | cfs_rq->runtime_remaining = cfs_b->quota; | |
2488 | if (cfs_rq_throttled(cfs_rq)) | |
2489 | unthrottle_cfs_rq(cfs_rq); | |
2490 | } | |
2491 | } | |
2492 | ||
2493 | #else /* CONFIG_CFS_BANDWIDTH */ | |
f1b17280 PT |
2494 | static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq) |
2495 | { | |
2496 | return rq_of(cfs_rq)->clock_task; | |
2497 | } | |
2498 | ||
2499 | static void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, | |
2500 | unsigned long delta_exec) {} | |
d3d9dc33 PT |
2501 | static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq) {} |
2502 | static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {} | |
6c16a6dc | 2503 | static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {} |
85dac906 PT |
2504 | |
2505 | static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq) | |
2506 | { | |
2507 | return 0; | |
2508 | } | |
64660c86 PT |
2509 | |
2510 | static inline int throttled_hierarchy(struct cfs_rq *cfs_rq) | |
2511 | { | |
2512 | return 0; | |
2513 | } | |
2514 | ||
2515 | static inline int throttled_lb_pair(struct task_group *tg, | |
2516 | int src_cpu, int dest_cpu) | |
2517 | { | |
2518 | return 0; | |
2519 | } | |
029632fb PZ |
2520 | |
2521 | void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {} | |
2522 | ||
2523 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
2524 | static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {} | |
ab84d31e PT |
2525 | #endif |
2526 | ||
029632fb PZ |
2527 | static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg) |
2528 | { | |
2529 | return NULL; | |
2530 | } | |
2531 | static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {} | |
a4c96ae3 | 2532 | static inline void unthrottle_offline_cfs_rqs(struct rq *rq) {} |
029632fb PZ |
2533 | |
2534 | #endif /* CONFIG_CFS_BANDWIDTH */ | |
2535 | ||
bf0f6f24 IM |
2536 | /************************************************** |
2537 | * CFS operations on tasks: | |
2538 | */ | |
2539 | ||
8f4d37ec PZ |
2540 | #ifdef CONFIG_SCHED_HRTICK |
2541 | static void hrtick_start_fair(struct rq *rq, struct task_struct *p) | |
2542 | { | |
8f4d37ec PZ |
2543 | struct sched_entity *se = &p->se; |
2544 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
2545 | ||
2546 | WARN_ON(task_rq(p) != rq); | |
2547 | ||
b39e66ea | 2548 | if (cfs_rq->nr_running > 1) { |
8f4d37ec PZ |
2549 | u64 slice = sched_slice(cfs_rq, se); |
2550 | u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime; | |
2551 | s64 delta = slice - ran; | |
2552 | ||
2553 | if (delta < 0) { | |
2554 | if (rq->curr == p) | |
2555 | resched_task(p); | |
2556 | return; | |
2557 | } | |
2558 | ||
2559 | /* | |
2560 | * Don't schedule slices shorter than 10000ns, that just | |
2561 | * doesn't make sense. Rely on vruntime for fairness. | |
2562 | */ | |
31656519 | 2563 | if (rq->curr != p) |
157124c1 | 2564 | delta = max_t(s64, 10000LL, delta); |
8f4d37ec | 2565 | |
31656519 | 2566 | hrtick_start(rq, delta); |
8f4d37ec PZ |
2567 | } |
2568 | } | |
a4c2f00f PZ |
2569 | |
2570 | /* | |
2571 | * called from enqueue/dequeue and updates the hrtick when the | |
2572 | * current task is from our class and nr_running is low enough | |
2573 | * to matter. | |
2574 | */ | |
2575 | static void hrtick_update(struct rq *rq) | |
2576 | { | |
2577 | struct task_struct *curr = rq->curr; | |
2578 | ||
b39e66ea | 2579 | if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class) |
a4c2f00f PZ |
2580 | return; |
2581 | ||
2582 | if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency) | |
2583 | hrtick_start_fair(rq, curr); | |
2584 | } | |
55e12e5e | 2585 | #else /* !CONFIG_SCHED_HRTICK */ |
8f4d37ec PZ |
2586 | static inline void |
2587 | hrtick_start_fair(struct rq *rq, struct task_struct *p) | |
2588 | { | |
2589 | } | |
a4c2f00f PZ |
2590 | |
2591 | static inline void hrtick_update(struct rq *rq) | |
2592 | { | |
2593 | } | |
8f4d37ec PZ |
2594 | #endif |
2595 | ||
bf0f6f24 IM |
2596 | /* |
2597 | * The enqueue_task method is called before nr_running is | |
2598 | * increased. Here we update the fair scheduling stats and | |
2599 | * then put the task into the rbtree: | |
2600 | */ | |
ea87bb78 | 2601 | static void |
371fd7e7 | 2602 | enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags) |
bf0f6f24 IM |
2603 | { |
2604 | struct cfs_rq *cfs_rq; | |
62fb1851 | 2605 | struct sched_entity *se = &p->se; |
bf0f6f24 IM |
2606 | |
2607 | for_each_sched_entity(se) { | |
62fb1851 | 2608 | if (se->on_rq) |
bf0f6f24 IM |
2609 | break; |
2610 | cfs_rq = cfs_rq_of(se); | |
88ec22d3 | 2611 | enqueue_entity(cfs_rq, se, flags); |
85dac906 PT |
2612 | |
2613 | /* | |
2614 | * end evaluation on encountering a throttled cfs_rq | |
2615 | * | |
2616 | * note: in the case of encountering a throttled cfs_rq we will | |
2617 | * post the final h_nr_running increment below. | |
2618 | */ | |
2619 | if (cfs_rq_throttled(cfs_rq)) | |
2620 | break; | |
953bfcd1 | 2621 | cfs_rq->h_nr_running++; |
85dac906 | 2622 | |
88ec22d3 | 2623 | flags = ENQUEUE_WAKEUP; |
bf0f6f24 | 2624 | } |
8f4d37ec | 2625 | |
2069dd75 | 2626 | for_each_sched_entity(se) { |
0f317143 | 2627 | cfs_rq = cfs_rq_of(se); |
953bfcd1 | 2628 | cfs_rq->h_nr_running++; |
2069dd75 | 2629 | |
85dac906 PT |
2630 | if (cfs_rq_throttled(cfs_rq)) |
2631 | break; | |
2632 | ||
d6b55918 | 2633 | update_cfs_load(cfs_rq, 0); |
6d5ab293 | 2634 | update_cfs_shares(cfs_rq); |
9ee474f5 | 2635 | update_entity_load_avg(se, 1); |
2069dd75 PZ |
2636 | } |
2637 | ||
18bf2805 BS |
2638 | if (!se) { |
2639 | update_rq_runnable_avg(rq, rq->nr_running); | |
85dac906 | 2640 | inc_nr_running(rq); |
18bf2805 | 2641 | } |
a4c2f00f | 2642 | hrtick_update(rq); |
bf0f6f24 IM |
2643 | } |
2644 | ||
2f36825b VP |
2645 | static void set_next_buddy(struct sched_entity *se); |
2646 | ||
bf0f6f24 IM |
2647 | /* |
2648 | * The dequeue_task method is called before nr_running is | |
2649 | * decreased. We remove the task from the rbtree and | |
2650 | * update the fair scheduling stats: | |
2651 | */ | |
371fd7e7 | 2652 | static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags) |
bf0f6f24 IM |
2653 | { |
2654 | struct cfs_rq *cfs_rq; | |
62fb1851 | 2655 | struct sched_entity *se = &p->se; |
2f36825b | 2656 | int task_sleep = flags & DEQUEUE_SLEEP; |
bf0f6f24 IM |
2657 | |
2658 | for_each_sched_entity(se) { | |
2659 | cfs_rq = cfs_rq_of(se); | |
371fd7e7 | 2660 | dequeue_entity(cfs_rq, se, flags); |
85dac906 PT |
2661 | |
2662 | /* | |
2663 | * end evaluation on encountering a throttled cfs_rq | |
2664 | * | |
2665 | * note: in the case of encountering a throttled cfs_rq we will | |
2666 | * post the final h_nr_running decrement below. | |
2667 | */ | |
2668 | if (cfs_rq_throttled(cfs_rq)) | |
2669 | break; | |
953bfcd1 | 2670 | cfs_rq->h_nr_running--; |
2069dd75 | 2671 | |
bf0f6f24 | 2672 | /* Don't dequeue parent if it has other entities besides us */ |
2f36825b VP |
2673 | if (cfs_rq->load.weight) { |
2674 | /* | |
2675 | * Bias pick_next to pick a task from this cfs_rq, as | |
2676 | * p is sleeping when it is within its sched_slice. | |
2677 | */ | |
2678 | if (task_sleep && parent_entity(se)) | |
2679 | set_next_buddy(parent_entity(se)); | |
9598c82d PT |
2680 | |
2681 | /* avoid re-evaluating load for this entity */ | |
2682 | se = parent_entity(se); | |
bf0f6f24 | 2683 | break; |
2f36825b | 2684 | } |
371fd7e7 | 2685 | flags |= DEQUEUE_SLEEP; |
bf0f6f24 | 2686 | } |
8f4d37ec | 2687 | |
2069dd75 | 2688 | for_each_sched_entity(se) { |
0f317143 | 2689 | cfs_rq = cfs_rq_of(se); |
953bfcd1 | 2690 | cfs_rq->h_nr_running--; |
2069dd75 | 2691 | |
85dac906 PT |
2692 | if (cfs_rq_throttled(cfs_rq)) |
2693 | break; | |
2694 | ||
d6b55918 | 2695 | update_cfs_load(cfs_rq, 0); |
6d5ab293 | 2696 | update_cfs_shares(cfs_rq); |
9ee474f5 | 2697 | update_entity_load_avg(se, 1); |
2069dd75 PZ |
2698 | } |
2699 | ||
18bf2805 | 2700 | if (!se) { |
85dac906 | 2701 | dec_nr_running(rq); |
18bf2805 BS |
2702 | update_rq_runnable_avg(rq, 1); |
2703 | } | |
a4c2f00f | 2704 | hrtick_update(rq); |
bf0f6f24 IM |
2705 | } |
2706 | ||
e7693a36 | 2707 | #ifdef CONFIG_SMP |
029632fb PZ |
2708 | /* Used instead of source_load when we know the type == 0 */ |
2709 | static unsigned long weighted_cpuload(const int cpu) | |
2710 | { | |
2711 | return cpu_rq(cpu)->load.weight; | |
2712 | } | |
2713 | ||
2714 | /* | |
2715 | * Return a low guess at the load of a migration-source cpu weighted | |
2716 | * according to the scheduling class and "nice" value. | |
2717 | * | |
2718 | * We want to under-estimate the load of migration sources, to | |
2719 | * balance conservatively. | |
2720 | */ | |
2721 | static unsigned long source_load(int cpu, int type) | |
2722 | { | |
2723 | struct rq *rq = cpu_rq(cpu); | |
2724 | unsigned long total = weighted_cpuload(cpu); | |
2725 | ||
2726 | if (type == 0 || !sched_feat(LB_BIAS)) | |
2727 | return total; | |
2728 | ||
2729 | return min(rq->cpu_load[type-1], total); | |
2730 | } | |
2731 | ||
2732 | /* | |
2733 | * Return a high guess at the load of a migration-target cpu weighted | |
2734 | * according to the scheduling class and "nice" value. | |
2735 | */ | |
2736 | static unsigned long target_load(int cpu, int type) | |
2737 | { | |
2738 | struct rq *rq = cpu_rq(cpu); | |
2739 | unsigned long total = weighted_cpuload(cpu); | |
2740 | ||
2741 | if (type == 0 || !sched_feat(LB_BIAS)) | |
2742 | return total; | |
2743 | ||
2744 | return max(rq->cpu_load[type-1], total); | |
2745 | } | |
2746 | ||
2747 | static unsigned long power_of(int cpu) | |
2748 | { | |
2749 | return cpu_rq(cpu)->cpu_power; | |
2750 | } | |
2751 | ||
2752 | static unsigned long cpu_avg_load_per_task(int cpu) | |
2753 | { | |
2754 | struct rq *rq = cpu_rq(cpu); | |
2755 | unsigned long nr_running = ACCESS_ONCE(rq->nr_running); | |
2756 | ||
2757 | if (nr_running) | |
2758 | return rq->load.weight / nr_running; | |
2759 | ||
2760 | return 0; | |
2761 | } | |
2762 | ||
098fb9db | 2763 | |
74f8e4b2 | 2764 | static void task_waking_fair(struct task_struct *p) |
88ec22d3 PZ |
2765 | { |
2766 | struct sched_entity *se = &p->se; | |
2767 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
3fe1698b PZ |
2768 | u64 min_vruntime; |
2769 | ||
2770 | #ifndef CONFIG_64BIT | |
2771 | u64 min_vruntime_copy; | |
88ec22d3 | 2772 | |
3fe1698b PZ |
2773 | do { |
2774 | min_vruntime_copy = cfs_rq->min_vruntime_copy; | |
2775 | smp_rmb(); | |
2776 | min_vruntime = cfs_rq->min_vruntime; | |
2777 | } while (min_vruntime != min_vruntime_copy); | |
2778 | #else | |
2779 | min_vruntime = cfs_rq->min_vruntime; | |
2780 | #endif | |
88ec22d3 | 2781 | |
3fe1698b | 2782 | se->vruntime -= min_vruntime; |
88ec22d3 PZ |
2783 | } |
2784 | ||
bb3469ac | 2785 | #ifdef CONFIG_FAIR_GROUP_SCHED |
f5bfb7d9 PZ |
2786 | /* |
2787 | * effective_load() calculates the load change as seen from the root_task_group | |
2788 | * | |
2789 | * Adding load to a group doesn't make a group heavier, but can cause movement | |
2790 | * of group shares between cpus. Assuming the shares were perfectly aligned one | |
2791 | * can calculate the shift in shares. | |
cf5f0acf PZ |
2792 | * |
2793 | * Calculate the effective load difference if @wl is added (subtracted) to @tg | |
2794 | * on this @cpu and results in a total addition (subtraction) of @wg to the | |
2795 | * total group weight. | |
2796 | * | |
2797 | * Given a runqueue weight distribution (rw_i) we can compute a shares | |
2798 | * distribution (s_i) using: | |
2799 | * | |
2800 | * s_i = rw_i / \Sum rw_j (1) | |
2801 | * | |
2802 | * Suppose we have 4 CPUs and our @tg is a direct child of the root group and | |
2803 | * has 7 equal weight tasks, distributed as below (rw_i), with the resulting | |
2804 | * shares distribution (s_i): | |
2805 | * | |
2806 | * rw_i = { 2, 4, 1, 0 } | |
2807 | * s_i = { 2/7, 4/7, 1/7, 0 } | |
2808 | * | |
2809 | * As per wake_affine() we're interested in the load of two CPUs (the CPU the | |
2810 | * task used to run on and the CPU the waker is running on), we need to | |
2811 | * compute the effect of waking a task on either CPU and, in case of a sync | |
2812 | * wakeup, compute the effect of the current task going to sleep. | |
2813 | * | |
2814 | * So for a change of @wl to the local @cpu with an overall group weight change | |
2815 | * of @wl we can compute the new shares distribution (s'_i) using: | |
2816 | * | |
2817 | * s'_i = (rw_i + @wl) / (@wg + \Sum rw_j) (2) | |
2818 | * | |
2819 | * Suppose we're interested in CPUs 0 and 1, and want to compute the load | |
2820 | * differences in waking a task to CPU 0. The additional task changes the | |
2821 | * weight and shares distributions like: | |
2822 | * | |
2823 | * rw'_i = { 3, 4, 1, 0 } | |
2824 | * s'_i = { 3/8, 4/8, 1/8, 0 } | |
2825 | * | |
2826 | * We can then compute the difference in effective weight by using: | |
2827 | * | |
2828 | * dw_i = S * (s'_i - s_i) (3) | |
2829 | * | |
2830 | * Where 'S' is the group weight as seen by its parent. | |
2831 | * | |
2832 | * Therefore the effective change in loads on CPU 0 would be 5/56 (3/8 - 2/7) | |
2833 | * times the weight of the group. The effect on CPU 1 would be -4/56 (4/8 - | |
2834 | * 4/7) times the weight of the group. | |
f5bfb7d9 | 2835 | */ |
2069dd75 | 2836 | static long effective_load(struct task_group *tg, int cpu, long wl, long wg) |
bb3469ac | 2837 | { |
4be9daaa | 2838 | struct sched_entity *se = tg->se[cpu]; |
f1d239f7 | 2839 | |
cf5f0acf | 2840 | if (!tg->parent) /* the trivial, non-cgroup case */ |
f1d239f7 PZ |
2841 | return wl; |
2842 | ||
4be9daaa | 2843 | for_each_sched_entity(se) { |
cf5f0acf | 2844 | long w, W; |
4be9daaa | 2845 | |
977dda7c | 2846 | tg = se->my_q->tg; |
bb3469ac | 2847 | |
cf5f0acf PZ |
2848 | /* |
2849 | * W = @wg + \Sum rw_j | |
2850 | */ | |
2851 | W = wg + calc_tg_weight(tg, se->my_q); | |
4be9daaa | 2852 | |
cf5f0acf PZ |
2853 | /* |
2854 | * w = rw_i + @wl | |
2855 | */ | |
2856 | w = se->my_q->load.weight + wl; | |
940959e9 | 2857 | |
cf5f0acf PZ |
2858 | /* |
2859 | * wl = S * s'_i; see (2) | |
2860 | */ | |
2861 | if (W > 0 && w < W) | |
2862 | wl = (w * tg->shares) / W; | |
977dda7c PT |
2863 | else |
2864 | wl = tg->shares; | |
940959e9 | 2865 | |
cf5f0acf PZ |
2866 | /* |
2867 | * Per the above, wl is the new se->load.weight value; since | |
2868 | * those are clipped to [MIN_SHARES, ...) do so now. See | |
2869 | * calc_cfs_shares(). | |
2870 | */ | |
977dda7c PT |
2871 | if (wl < MIN_SHARES) |
2872 | wl = MIN_SHARES; | |
cf5f0acf PZ |
2873 | |
2874 | /* | |
2875 | * wl = dw_i = S * (s'_i - s_i); see (3) | |
2876 | */ | |
977dda7c | 2877 | wl -= se->load.weight; |
cf5f0acf PZ |
2878 | |
2879 | /* | |
2880 | * Recursively apply this logic to all parent groups to compute | |
2881 | * the final effective load change on the root group. Since | |
2882 | * only the @tg group gets extra weight, all parent groups can | |
2883 | * only redistribute existing shares. @wl is the shift in shares | |
2884 | * resulting from this level per the above. | |
2885 | */ | |
4be9daaa | 2886 | wg = 0; |
4be9daaa | 2887 | } |
bb3469ac | 2888 | |
4be9daaa | 2889 | return wl; |
bb3469ac PZ |
2890 | } |
2891 | #else | |
4be9daaa | 2892 | |
83378269 PZ |
2893 | static inline unsigned long effective_load(struct task_group *tg, int cpu, |
2894 | unsigned long wl, unsigned long wg) | |
4be9daaa | 2895 | { |
83378269 | 2896 | return wl; |
bb3469ac | 2897 | } |
4be9daaa | 2898 | |
bb3469ac PZ |
2899 | #endif |
2900 | ||
c88d5910 | 2901 | static int wake_affine(struct sched_domain *sd, struct task_struct *p, int sync) |
098fb9db | 2902 | { |
e37b6a7b | 2903 | s64 this_load, load; |
c88d5910 | 2904 | int idx, this_cpu, prev_cpu; |
098fb9db | 2905 | unsigned long tl_per_task; |
c88d5910 | 2906 | struct task_group *tg; |
83378269 | 2907 | unsigned long weight; |
b3137bc8 | 2908 | int balanced; |
098fb9db | 2909 | |
c88d5910 PZ |
2910 | idx = sd->wake_idx; |
2911 | this_cpu = smp_processor_id(); | |
2912 | prev_cpu = task_cpu(p); | |
2913 | load = source_load(prev_cpu, idx); | |
2914 | this_load = target_load(this_cpu, idx); | |
098fb9db | 2915 | |
b3137bc8 MG |
2916 | /* |
2917 | * If sync wakeup then subtract the (maximum possible) | |
2918 | * effect of the currently running task from the load | |
2919 | * of the current CPU: | |
2920 | */ | |
83378269 PZ |
2921 | if (sync) { |
2922 | tg = task_group(current); | |
2923 | weight = current->se.load.weight; | |
2924 | ||
c88d5910 | 2925 | this_load += effective_load(tg, this_cpu, -weight, -weight); |
83378269 PZ |
2926 | load += effective_load(tg, prev_cpu, 0, -weight); |
2927 | } | |
b3137bc8 | 2928 | |
83378269 PZ |
2929 | tg = task_group(p); |
2930 | weight = p->se.load.weight; | |
b3137bc8 | 2931 | |
71a29aa7 PZ |
2932 | /* |
2933 | * In low-load situations, where prev_cpu is idle and this_cpu is idle | |
c88d5910 PZ |
2934 | * due to the sync cause above having dropped this_load to 0, we'll |
2935 | * always have an imbalance, but there's really nothing you can do | |
2936 | * about that, so that's good too. | |
71a29aa7 PZ |
2937 | * |
2938 | * Otherwise check if either cpus are near enough in load to allow this | |
2939 | * task to be woken on this_cpu. | |
2940 | */ | |
e37b6a7b PT |
2941 | if (this_load > 0) { |
2942 | s64 this_eff_load, prev_eff_load; | |
e51fd5e2 PZ |
2943 | |
2944 | this_eff_load = 100; | |
2945 | this_eff_load *= power_of(prev_cpu); | |
2946 | this_eff_load *= this_load + | |
2947 | effective_load(tg, this_cpu, weight, weight); | |
2948 | ||
2949 | prev_eff_load = 100 + (sd->imbalance_pct - 100) / 2; | |
2950 | prev_eff_load *= power_of(this_cpu); | |
2951 | prev_eff_load *= load + effective_load(tg, prev_cpu, 0, weight); | |
2952 | ||
2953 | balanced = this_eff_load <= prev_eff_load; | |
2954 | } else | |
2955 | balanced = true; | |
b3137bc8 | 2956 | |
098fb9db | 2957 | /* |
4ae7d5ce IM |
2958 | * If the currently running task will sleep within |
2959 | * a reasonable amount of time then attract this newly | |
2960 | * woken task: | |
098fb9db | 2961 | */ |
2fb7635c PZ |
2962 | if (sync && balanced) |
2963 | return 1; | |
098fb9db | 2964 | |
41acab88 | 2965 | schedstat_inc(p, se.statistics.nr_wakeups_affine_attempts); |
098fb9db IM |
2966 | tl_per_task = cpu_avg_load_per_task(this_cpu); |
2967 | ||
c88d5910 PZ |
2968 | if (balanced || |
2969 | (this_load <= load && | |
2970 | this_load + target_load(prev_cpu, idx) <= tl_per_task)) { | |
098fb9db IM |
2971 | /* |
2972 | * This domain has SD_WAKE_AFFINE and | |
2973 | * p is cache cold in this domain, and | |
2974 | * there is no bad imbalance. | |
2975 | */ | |
c88d5910 | 2976 | schedstat_inc(sd, ttwu_move_affine); |
41acab88 | 2977 | schedstat_inc(p, se.statistics.nr_wakeups_affine); |
098fb9db IM |
2978 | |
2979 | return 1; | |
2980 | } | |
2981 | return 0; | |
2982 | } | |
2983 | ||
aaee1203 PZ |
2984 | /* |
2985 | * find_idlest_group finds and returns the least busy CPU group within the | |
2986 | * domain. | |
2987 | */ | |
2988 | static struct sched_group * | |
78e7ed53 | 2989 | find_idlest_group(struct sched_domain *sd, struct task_struct *p, |
5158f4e4 | 2990 | int this_cpu, int load_idx) |
e7693a36 | 2991 | { |
b3bd3de6 | 2992 | struct sched_group *idlest = NULL, *group = sd->groups; |
aaee1203 | 2993 | unsigned long min_load = ULONG_MAX, this_load = 0; |
aaee1203 | 2994 | int imbalance = 100 + (sd->imbalance_pct-100)/2; |
e7693a36 | 2995 | |
aaee1203 PZ |
2996 | do { |
2997 | unsigned long load, avg_load; | |
2998 | int local_group; | |
2999 | int i; | |
e7693a36 | 3000 | |
aaee1203 PZ |
3001 | /* Skip over this group if it has no CPUs allowed */ |
3002 | if (!cpumask_intersects(sched_group_cpus(group), | |
fa17b507 | 3003 | tsk_cpus_allowed(p))) |
aaee1203 PZ |
3004 | continue; |
3005 | ||
3006 | local_group = cpumask_test_cpu(this_cpu, | |
3007 | sched_group_cpus(group)); | |
3008 | ||
3009 | /* Tally up the load of all CPUs in the group */ | |
3010 | avg_load = 0; | |
3011 | ||
3012 | for_each_cpu(i, sched_group_cpus(group)) { | |
3013 | /* Bias balancing toward cpus of our domain */ | |
3014 | if (local_group) | |
3015 | load = source_load(i, load_idx); | |
3016 | else | |
3017 | load = target_load(i, load_idx); | |
3018 | ||
3019 | avg_load += load; | |
3020 | } | |
3021 | ||
3022 | /* Adjust by relative CPU power of the group */ | |
9c3f75cb | 3023 | avg_load = (avg_load * SCHED_POWER_SCALE) / group->sgp->power; |
aaee1203 PZ |
3024 | |
3025 | if (local_group) { | |
3026 | this_load = avg_load; | |
aaee1203 PZ |
3027 | } else if (avg_load < min_load) { |
3028 | min_load = avg_load; | |
3029 | idlest = group; | |
3030 | } | |
3031 | } while (group = group->next, group != sd->groups); | |
3032 | ||
3033 | if (!idlest || 100*this_load < imbalance*min_load) | |
3034 | return NULL; | |
3035 | return idlest; | |
3036 | } | |
3037 | ||
3038 | /* | |
3039 | * find_idlest_cpu - find the idlest cpu among the cpus in group. | |
3040 | */ | |
3041 | static int | |
3042 | find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu) | |
3043 | { | |
3044 | unsigned long load, min_load = ULONG_MAX; | |
3045 | int idlest = -1; | |
3046 | int i; | |
3047 | ||
3048 | /* Traverse only the allowed CPUs */ | |
fa17b507 | 3049 | for_each_cpu_and(i, sched_group_cpus(group), tsk_cpus_allowed(p)) { |
aaee1203 PZ |
3050 | load = weighted_cpuload(i); |
3051 | ||
3052 | if (load < min_load || (load == min_load && i == this_cpu)) { | |
3053 | min_load = load; | |
3054 | idlest = i; | |
e7693a36 GH |
3055 | } |
3056 | } | |
3057 | ||
aaee1203 PZ |
3058 | return idlest; |
3059 | } | |
e7693a36 | 3060 | |
a50bde51 PZ |
3061 | /* |
3062 | * Try and locate an idle CPU in the sched_domain. | |
3063 | */ | |
99bd5e2f | 3064 | static int select_idle_sibling(struct task_struct *p, int target) |
a50bde51 PZ |
3065 | { |
3066 | int cpu = smp_processor_id(); | |
3067 | int prev_cpu = task_cpu(p); | |
99bd5e2f | 3068 | struct sched_domain *sd; |
37407ea7 LT |
3069 | struct sched_group *sg; |
3070 | int i; | |
a50bde51 PZ |
3071 | |
3072 | /* | |
99bd5e2f SS |
3073 | * If the task is going to be woken-up on this cpu and if it is |
3074 | * already idle, then it is the right target. | |
a50bde51 | 3075 | */ |
99bd5e2f SS |
3076 | if (target == cpu && idle_cpu(cpu)) |
3077 | return cpu; | |
3078 | ||
3079 | /* | |
3080 | * If the task is going to be woken-up on the cpu where it previously | |
3081 | * ran and if it is currently idle, then it the right target. | |
3082 | */ | |
3083 | if (target == prev_cpu && idle_cpu(prev_cpu)) | |
fe3bcfe1 | 3084 | return prev_cpu; |
a50bde51 PZ |
3085 | |
3086 | /* | |
37407ea7 | 3087 | * Otherwise, iterate the domains and find an elegible idle cpu. |
a50bde51 | 3088 | */ |
518cd623 | 3089 | sd = rcu_dereference(per_cpu(sd_llc, target)); |
970e1789 | 3090 | for_each_lower_domain(sd) { |
37407ea7 LT |
3091 | sg = sd->groups; |
3092 | do { | |
3093 | if (!cpumask_intersects(sched_group_cpus(sg), | |
3094 | tsk_cpus_allowed(p))) | |
3095 | goto next; | |
3096 | ||
3097 | for_each_cpu(i, sched_group_cpus(sg)) { | |
3098 | if (!idle_cpu(i)) | |
3099 | goto next; | |
3100 | } | |
970e1789 | 3101 | |
37407ea7 LT |
3102 | target = cpumask_first_and(sched_group_cpus(sg), |
3103 | tsk_cpus_allowed(p)); | |
3104 | goto done; | |
3105 | next: | |
3106 | sg = sg->next; | |
3107 | } while (sg != sd->groups); | |
3108 | } | |
3109 | done: | |
a50bde51 PZ |
3110 | return target; |
3111 | } | |
3112 | ||
aaee1203 PZ |
3113 | /* |
3114 | * sched_balance_self: balance the current task (running on cpu) in domains | |
3115 | * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and | |
3116 | * SD_BALANCE_EXEC. | |
3117 | * | |
3118 | * Balance, ie. select the least loaded group. | |
3119 | * | |
3120 | * Returns the target CPU number, or the same CPU if no balancing is needed. | |
3121 | * | |
3122 | * preempt must be disabled. | |
3123 | */ | |
0017d735 | 3124 | static int |
7608dec2 | 3125 | select_task_rq_fair(struct task_struct *p, int sd_flag, int wake_flags) |
aaee1203 | 3126 | { |
29cd8bae | 3127 | struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL; |
c88d5910 PZ |
3128 | int cpu = smp_processor_id(); |
3129 | int prev_cpu = task_cpu(p); | |
3130 | int new_cpu = cpu; | |
99bd5e2f | 3131 | int want_affine = 0; |
5158f4e4 | 3132 | int sync = wake_flags & WF_SYNC; |
c88d5910 | 3133 | |
29baa747 | 3134 | if (p->nr_cpus_allowed == 1) |
76854c7e MG |
3135 | return prev_cpu; |
3136 | ||
0763a660 | 3137 | if (sd_flag & SD_BALANCE_WAKE) { |
fa17b507 | 3138 | if (cpumask_test_cpu(cpu, tsk_cpus_allowed(p))) |
c88d5910 PZ |
3139 | want_affine = 1; |
3140 | new_cpu = prev_cpu; | |
3141 | } | |
aaee1203 | 3142 | |
dce840a0 | 3143 | rcu_read_lock(); |
aaee1203 | 3144 | for_each_domain(cpu, tmp) { |
e4f42888 PZ |
3145 | if (!(tmp->flags & SD_LOAD_BALANCE)) |
3146 | continue; | |
3147 | ||
fe3bcfe1 | 3148 | /* |
99bd5e2f SS |
3149 | * If both cpu and prev_cpu are part of this domain, |
3150 | * cpu is a valid SD_WAKE_AFFINE target. | |
fe3bcfe1 | 3151 | */ |
99bd5e2f SS |
3152 | if (want_affine && (tmp->flags & SD_WAKE_AFFINE) && |
3153 | cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) { | |
3154 | affine_sd = tmp; | |
29cd8bae | 3155 | break; |
f03542a7 | 3156 | } |
29cd8bae | 3157 | |
f03542a7 | 3158 | if (tmp->flags & sd_flag) |
29cd8bae PZ |
3159 | sd = tmp; |
3160 | } | |
3161 | ||
8b911acd | 3162 | if (affine_sd) { |
f03542a7 | 3163 | if (cpu != prev_cpu && wake_affine(affine_sd, p, sync)) |
dce840a0 PZ |
3164 | prev_cpu = cpu; |
3165 | ||
3166 | new_cpu = select_idle_sibling(p, prev_cpu); | |
3167 | goto unlock; | |
8b911acd | 3168 | } |
e7693a36 | 3169 | |
aaee1203 | 3170 | while (sd) { |
5158f4e4 | 3171 | int load_idx = sd->forkexec_idx; |
aaee1203 | 3172 | struct sched_group *group; |
c88d5910 | 3173 | int weight; |
098fb9db | 3174 | |
0763a660 | 3175 | if (!(sd->flags & sd_flag)) { |
aaee1203 PZ |
3176 | sd = sd->child; |
3177 | continue; | |
3178 | } | |
098fb9db | 3179 | |
5158f4e4 PZ |
3180 | if (sd_flag & SD_BALANCE_WAKE) |
3181 | load_idx = sd->wake_idx; | |
098fb9db | 3182 | |
5158f4e4 | 3183 | group = find_idlest_group(sd, p, cpu, load_idx); |
aaee1203 PZ |
3184 | if (!group) { |
3185 | sd = sd->child; | |
3186 | continue; | |
3187 | } | |
4ae7d5ce | 3188 | |
d7c33c49 | 3189 | new_cpu = find_idlest_cpu(group, p, cpu); |
aaee1203 PZ |
3190 | if (new_cpu == -1 || new_cpu == cpu) { |
3191 | /* Now try balancing at a lower domain level of cpu */ | |
3192 | sd = sd->child; | |
3193 | continue; | |
e7693a36 | 3194 | } |
aaee1203 PZ |
3195 | |
3196 | /* Now try balancing at a lower domain level of new_cpu */ | |
3197 | cpu = new_cpu; | |
669c55e9 | 3198 | weight = sd->span_weight; |
aaee1203 PZ |
3199 | sd = NULL; |
3200 | for_each_domain(cpu, tmp) { | |
669c55e9 | 3201 | if (weight <= tmp->span_weight) |
aaee1203 | 3202 | break; |
0763a660 | 3203 | if (tmp->flags & sd_flag) |
aaee1203 PZ |
3204 | sd = tmp; |
3205 | } | |
3206 | /* while loop will break here if sd == NULL */ | |
e7693a36 | 3207 | } |
dce840a0 PZ |
3208 | unlock: |
3209 | rcu_read_unlock(); | |
e7693a36 | 3210 | |
c88d5910 | 3211 | return new_cpu; |
e7693a36 | 3212 | } |
0a74bef8 PT |
3213 | |
3214 | /* | |
3215 | * Called immediately before a task is migrated to a new cpu; task_cpu(p) and | |
3216 | * cfs_rq_of(p) references at time of call are still valid and identify the | |
3217 | * previous cpu. However, the caller only guarantees p->pi_lock is held; no | |
3218 | * other assumptions, including the state of rq->lock, should be made. | |
3219 | */ | |
3220 | static void | |
3221 | migrate_task_rq_fair(struct task_struct *p, int next_cpu) | |
3222 | { | |
aff3e498 PT |
3223 | struct sched_entity *se = &p->se; |
3224 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
3225 | ||
3226 | /* | |
3227 | * Load tracking: accumulate removed load so that it can be processed | |
3228 | * when we next update owning cfs_rq under rq->lock. Tasks contribute | |
3229 | * to blocked load iff they have a positive decay-count. It can never | |
3230 | * be negative here since on-rq tasks have decay-count == 0. | |
3231 | */ | |
3232 | if (se->avg.decay_count) { | |
3233 | se->avg.decay_count = -__synchronize_entity_decay(se); | |
3234 | atomic64_add(se->avg.load_avg_contrib, &cfs_rq->removed_load); | |
3235 | } | |
0a74bef8 | 3236 | } |
e7693a36 GH |
3237 | #endif /* CONFIG_SMP */ |
3238 | ||
e52fb7c0 PZ |
3239 | static unsigned long |
3240 | wakeup_gran(struct sched_entity *curr, struct sched_entity *se) | |
0bbd3336 PZ |
3241 | { |
3242 | unsigned long gran = sysctl_sched_wakeup_granularity; | |
3243 | ||
3244 | /* | |
e52fb7c0 PZ |
3245 | * Since its curr running now, convert the gran from real-time |
3246 | * to virtual-time in his units. | |
13814d42 MG |
3247 | * |
3248 | * By using 'se' instead of 'curr' we penalize light tasks, so | |
3249 | * they get preempted easier. That is, if 'se' < 'curr' then | |
3250 | * the resulting gran will be larger, therefore penalizing the | |
3251 | * lighter, if otoh 'se' > 'curr' then the resulting gran will | |
3252 | * be smaller, again penalizing the lighter task. | |
3253 | * | |
3254 | * This is especially important for buddies when the leftmost | |
3255 | * task is higher priority than the buddy. | |
0bbd3336 | 3256 | */ |
f4ad9bd2 | 3257 | return calc_delta_fair(gran, se); |
0bbd3336 PZ |
3258 | } |
3259 | ||
464b7527 PZ |
3260 | /* |
3261 | * Should 'se' preempt 'curr'. | |
3262 | * | |
3263 | * |s1 | |
3264 | * |s2 | |
3265 | * |s3 | |
3266 | * g | |
3267 | * |<--->|c | |
3268 | * | |
3269 | * w(c, s1) = -1 | |
3270 | * w(c, s2) = 0 | |
3271 | * w(c, s3) = 1 | |
3272 | * | |
3273 | */ | |
3274 | static int | |
3275 | wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se) | |
3276 | { | |
3277 | s64 gran, vdiff = curr->vruntime - se->vruntime; | |
3278 | ||
3279 | if (vdiff <= 0) | |
3280 | return -1; | |
3281 | ||
e52fb7c0 | 3282 | gran = wakeup_gran(curr, se); |
464b7527 PZ |
3283 | if (vdiff > gran) |
3284 | return 1; | |
3285 | ||
3286 | return 0; | |
3287 | } | |
3288 | ||
02479099 PZ |
3289 | static void set_last_buddy(struct sched_entity *se) |
3290 | { | |
69c80f3e VP |
3291 | if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE)) |
3292 | return; | |
3293 | ||
3294 | for_each_sched_entity(se) | |
3295 | cfs_rq_of(se)->last = se; | |
02479099 PZ |
3296 | } |
3297 | ||
3298 | static void set_next_buddy(struct sched_entity *se) | |
3299 | { | |
69c80f3e VP |
3300 | if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE)) |
3301 | return; | |
3302 | ||
3303 | for_each_sched_entity(se) | |
3304 | cfs_rq_of(se)->next = se; | |
02479099 PZ |
3305 | } |
3306 | ||
ac53db59 RR |
3307 | static void set_skip_buddy(struct sched_entity *se) |
3308 | { | |
69c80f3e VP |
3309 | for_each_sched_entity(se) |
3310 | cfs_rq_of(se)->skip = se; | |
ac53db59 RR |
3311 | } |
3312 | ||
bf0f6f24 IM |
3313 | /* |
3314 | * Preempt the current task with a newly woken task if needed: | |
3315 | */ | |
5a9b86f6 | 3316 | static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags) |
bf0f6f24 IM |
3317 | { |
3318 | struct task_struct *curr = rq->curr; | |
8651a86c | 3319 | struct sched_entity *se = &curr->se, *pse = &p->se; |
03e89e45 | 3320 | struct cfs_rq *cfs_rq = task_cfs_rq(curr); |
f685ceac | 3321 | int scale = cfs_rq->nr_running >= sched_nr_latency; |
2f36825b | 3322 | int next_buddy_marked = 0; |
bf0f6f24 | 3323 | |
4ae7d5ce IM |
3324 | if (unlikely(se == pse)) |
3325 | return; | |
3326 | ||
5238cdd3 | 3327 | /* |
ddcdf6e7 | 3328 | * This is possible from callers such as move_task(), in which we |
5238cdd3 PT |
3329 | * unconditionally check_prempt_curr() after an enqueue (which may have |
3330 | * lead to a throttle). This both saves work and prevents false | |
3331 | * next-buddy nomination below. | |
3332 | */ | |
3333 | if (unlikely(throttled_hierarchy(cfs_rq_of(pse)))) | |
3334 | return; | |
3335 | ||
2f36825b | 3336 | if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) { |
3cb63d52 | 3337 | set_next_buddy(pse); |
2f36825b VP |
3338 | next_buddy_marked = 1; |
3339 | } | |
57fdc26d | 3340 | |
aec0a514 BR |
3341 | /* |
3342 | * We can come here with TIF_NEED_RESCHED already set from new task | |
3343 | * wake up path. | |
5238cdd3 PT |
3344 | * |
3345 | * Note: this also catches the edge-case of curr being in a throttled | |
3346 | * group (e.g. via set_curr_task), since update_curr() (in the | |
3347 | * enqueue of curr) will have resulted in resched being set. This | |
3348 | * prevents us from potentially nominating it as a false LAST_BUDDY | |
3349 | * below. | |
aec0a514 BR |
3350 | */ |
3351 | if (test_tsk_need_resched(curr)) | |
3352 | return; | |
3353 | ||
a2f5c9ab DH |
3354 | /* Idle tasks are by definition preempted by non-idle tasks. */ |
3355 | if (unlikely(curr->policy == SCHED_IDLE) && | |
3356 | likely(p->policy != SCHED_IDLE)) | |
3357 | goto preempt; | |
3358 | ||
91c234b4 | 3359 | /* |
a2f5c9ab DH |
3360 | * Batch and idle tasks do not preempt non-idle tasks (their preemption |
3361 | * is driven by the tick): | |
91c234b4 | 3362 | */ |
6bc912b7 | 3363 | if (unlikely(p->policy != SCHED_NORMAL)) |
91c234b4 | 3364 | return; |
bf0f6f24 | 3365 | |
464b7527 | 3366 | find_matching_se(&se, &pse); |
9bbd7374 | 3367 | update_curr(cfs_rq_of(se)); |
002f128b | 3368 | BUG_ON(!pse); |
2f36825b VP |
3369 | if (wakeup_preempt_entity(se, pse) == 1) { |
3370 | /* | |
3371 | * Bias pick_next to pick the sched entity that is | |
3372 | * triggering this preemption. | |
3373 | */ | |
3374 | if (!next_buddy_marked) | |
3375 | set_next_buddy(pse); | |
3a7e73a2 | 3376 | goto preempt; |
2f36825b | 3377 | } |
464b7527 | 3378 | |
3a7e73a2 | 3379 | return; |
a65ac745 | 3380 | |
3a7e73a2 PZ |
3381 | preempt: |
3382 | resched_task(curr); | |
3383 | /* | |
3384 | * Only set the backward buddy when the current task is still | |
3385 | * on the rq. This can happen when a wakeup gets interleaved | |
3386 | * with schedule on the ->pre_schedule() or idle_balance() | |
3387 | * point, either of which can * drop the rq lock. | |
3388 | * | |
3389 | * Also, during early boot the idle thread is in the fair class, | |
3390 | * for obvious reasons its a bad idea to schedule back to it. | |
3391 | */ | |
3392 | if (unlikely(!se->on_rq || curr == rq->idle)) | |
3393 | return; | |
3394 | ||
3395 | if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se)) | |
3396 | set_last_buddy(se); | |
bf0f6f24 IM |
3397 | } |
3398 | ||
fb8d4724 | 3399 | static struct task_struct *pick_next_task_fair(struct rq *rq) |
bf0f6f24 | 3400 | { |
8f4d37ec | 3401 | struct task_struct *p; |
bf0f6f24 IM |
3402 | struct cfs_rq *cfs_rq = &rq->cfs; |
3403 | struct sched_entity *se; | |
3404 | ||
36ace27e | 3405 | if (!cfs_rq->nr_running) |
bf0f6f24 IM |
3406 | return NULL; |
3407 | ||
3408 | do { | |
9948f4b2 | 3409 | se = pick_next_entity(cfs_rq); |
f4b6755f | 3410 | set_next_entity(cfs_rq, se); |
bf0f6f24 IM |
3411 | cfs_rq = group_cfs_rq(se); |
3412 | } while (cfs_rq); | |
3413 | ||
8f4d37ec | 3414 | p = task_of(se); |
b39e66ea MG |
3415 | if (hrtick_enabled(rq)) |
3416 | hrtick_start_fair(rq, p); | |
8f4d37ec PZ |
3417 | |
3418 | return p; | |
bf0f6f24 IM |
3419 | } |
3420 | ||
3421 | /* | |
3422 | * Account for a descheduled task: | |
3423 | */ | |
31ee529c | 3424 | static void put_prev_task_fair(struct rq *rq, struct task_struct *prev) |
bf0f6f24 IM |
3425 | { |
3426 | struct sched_entity *se = &prev->se; | |
3427 | struct cfs_rq *cfs_rq; | |
3428 | ||
3429 | for_each_sched_entity(se) { | |
3430 | cfs_rq = cfs_rq_of(se); | |
ab6cde26 | 3431 | put_prev_entity(cfs_rq, se); |
bf0f6f24 IM |
3432 | } |
3433 | } | |
3434 | ||
ac53db59 RR |
3435 | /* |
3436 | * sched_yield() is very simple | |
3437 | * | |
3438 | * The magic of dealing with the ->skip buddy is in pick_next_entity. | |
3439 | */ | |
3440 | static void yield_task_fair(struct rq *rq) | |
3441 | { | |
3442 | struct task_struct *curr = rq->curr; | |
3443 | struct cfs_rq *cfs_rq = task_cfs_rq(curr); | |
3444 | struct sched_entity *se = &curr->se; | |
3445 | ||
3446 | /* | |
3447 | * Are we the only task in the tree? | |
3448 | */ | |
3449 | if (unlikely(rq->nr_running == 1)) | |
3450 | return; | |
3451 | ||
3452 | clear_buddies(cfs_rq, se); | |
3453 | ||
3454 | if (curr->policy != SCHED_BATCH) { | |
3455 | update_rq_clock(rq); | |
3456 | /* | |
3457 | * Update run-time statistics of the 'current'. | |
3458 | */ | |
3459 | update_curr(cfs_rq); | |
916671c0 MG |
3460 | /* |
3461 | * Tell update_rq_clock() that we've just updated, | |
3462 | * so we don't do microscopic update in schedule() | |
3463 | * and double the fastpath cost. | |
3464 | */ | |
3465 | rq->skip_clock_update = 1; | |
ac53db59 RR |
3466 | } |
3467 | ||
3468 | set_skip_buddy(se); | |
3469 | } | |
3470 | ||
d95f4122 MG |
3471 | static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt) |
3472 | { | |
3473 | struct sched_entity *se = &p->se; | |
3474 | ||
5238cdd3 PT |
3475 | /* throttled hierarchies are not runnable */ |
3476 | if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se))) | |
d95f4122 MG |
3477 | return false; |
3478 | ||
3479 | /* Tell the scheduler that we'd really like pse to run next. */ | |
3480 | set_next_buddy(se); | |
3481 | ||
d95f4122 MG |
3482 | yield_task_fair(rq); |
3483 | ||
3484 | return true; | |
3485 | } | |
3486 | ||
681f3e68 | 3487 | #ifdef CONFIG_SMP |
bf0f6f24 IM |
3488 | /************************************************** |
3489 | * Fair scheduling class load-balancing methods: | |
3490 | */ | |
3491 | ||
ed387b78 HS |
3492 | static unsigned long __read_mostly max_load_balance_interval = HZ/10; |
3493 | ||
ddcdf6e7 | 3494 | #define LBF_ALL_PINNED 0x01 |
367456c7 | 3495 | #define LBF_NEED_BREAK 0x02 |
88b8dac0 | 3496 | #define LBF_SOME_PINNED 0x04 |
ddcdf6e7 PZ |
3497 | |
3498 | struct lb_env { | |
3499 | struct sched_domain *sd; | |
3500 | ||
ddcdf6e7 | 3501 | struct rq *src_rq; |
85c1e7da | 3502 | int src_cpu; |
ddcdf6e7 PZ |
3503 | |
3504 | int dst_cpu; | |
3505 | struct rq *dst_rq; | |
3506 | ||
88b8dac0 SV |
3507 | struct cpumask *dst_grpmask; |
3508 | int new_dst_cpu; | |
ddcdf6e7 | 3509 | enum cpu_idle_type idle; |
bd939f45 | 3510 | long imbalance; |
b9403130 MW |
3511 | /* The set of CPUs under consideration for load-balancing */ |
3512 | struct cpumask *cpus; | |
3513 | ||
ddcdf6e7 | 3514 | unsigned int flags; |
367456c7 PZ |
3515 | |
3516 | unsigned int loop; | |
3517 | unsigned int loop_break; | |
3518 | unsigned int loop_max; | |
ddcdf6e7 PZ |
3519 | }; |
3520 | ||
1e3c88bd | 3521 | /* |
ddcdf6e7 | 3522 | * move_task - move a task from one runqueue to another runqueue. |
1e3c88bd PZ |
3523 | * Both runqueues must be locked. |
3524 | */ | |
ddcdf6e7 | 3525 | static void move_task(struct task_struct *p, struct lb_env *env) |
1e3c88bd | 3526 | { |
ddcdf6e7 PZ |
3527 | deactivate_task(env->src_rq, p, 0); |
3528 | set_task_cpu(p, env->dst_cpu); | |
3529 | activate_task(env->dst_rq, p, 0); | |
3530 | check_preempt_curr(env->dst_rq, p, 0); | |
1e3c88bd PZ |
3531 | } |
3532 | ||
029632fb PZ |
3533 | /* |
3534 | * Is this task likely cache-hot: | |
3535 | */ | |
3536 | static int | |
3537 | task_hot(struct task_struct *p, u64 now, struct sched_domain *sd) | |
3538 | { | |
3539 | s64 delta; | |
3540 | ||
3541 | if (p->sched_class != &fair_sched_class) | |
3542 | return 0; | |
3543 | ||
3544 | if (unlikely(p->policy == SCHED_IDLE)) | |
3545 | return 0; | |
3546 | ||
3547 | /* | |
3548 | * Buddy candidates are cache hot: | |
3549 | */ | |
3550 | if (sched_feat(CACHE_HOT_BUDDY) && this_rq()->nr_running && | |
3551 | (&p->se == cfs_rq_of(&p->se)->next || | |
3552 | &p->se == cfs_rq_of(&p->se)->last)) | |
3553 | return 1; | |
3554 | ||
3555 | if (sysctl_sched_migration_cost == -1) | |
3556 | return 1; | |
3557 | if (sysctl_sched_migration_cost == 0) | |
3558 | return 0; | |
3559 | ||
3560 | delta = now - p->se.exec_start; | |
3561 | ||
3562 | return delta < (s64)sysctl_sched_migration_cost; | |
3563 | } | |
3564 | ||
1e3c88bd PZ |
3565 | /* |
3566 | * can_migrate_task - may task p from runqueue rq be migrated to this_cpu? | |
3567 | */ | |
3568 | static | |
8e45cb54 | 3569 | int can_migrate_task(struct task_struct *p, struct lb_env *env) |
1e3c88bd PZ |
3570 | { |
3571 | int tsk_cache_hot = 0; | |
3572 | /* | |
3573 | * We do not migrate tasks that are: | |
3574 | * 1) running (obviously), or | |
3575 | * 2) cannot be migrated to this CPU due to cpus_allowed, or | |
3576 | * 3) are cache-hot on their current CPU. | |
3577 | */ | |
ddcdf6e7 | 3578 | if (!cpumask_test_cpu(env->dst_cpu, tsk_cpus_allowed(p))) { |
88b8dac0 SV |
3579 | int new_dst_cpu; |
3580 | ||
41acab88 | 3581 | schedstat_inc(p, se.statistics.nr_failed_migrations_affine); |
88b8dac0 SV |
3582 | |
3583 | /* | |
3584 | * Remember if this task can be migrated to any other cpu in | |
3585 | * our sched_group. We may want to revisit it if we couldn't | |
3586 | * meet load balance goals by pulling other tasks on src_cpu. | |
3587 | * | |
3588 | * Also avoid computing new_dst_cpu if we have already computed | |
3589 | * one in current iteration. | |
3590 | */ | |
3591 | if (!env->dst_grpmask || (env->flags & LBF_SOME_PINNED)) | |
3592 | return 0; | |
3593 | ||
3594 | new_dst_cpu = cpumask_first_and(env->dst_grpmask, | |
3595 | tsk_cpus_allowed(p)); | |
3596 | if (new_dst_cpu < nr_cpu_ids) { | |
3597 | env->flags |= LBF_SOME_PINNED; | |
3598 | env->new_dst_cpu = new_dst_cpu; | |
3599 | } | |
1e3c88bd PZ |
3600 | return 0; |
3601 | } | |
88b8dac0 SV |
3602 | |
3603 | /* Record that we found atleast one task that could run on dst_cpu */ | |
8e45cb54 | 3604 | env->flags &= ~LBF_ALL_PINNED; |
1e3c88bd | 3605 | |
ddcdf6e7 | 3606 | if (task_running(env->src_rq, p)) { |
41acab88 | 3607 | schedstat_inc(p, se.statistics.nr_failed_migrations_running); |
1e3c88bd PZ |
3608 | return 0; |
3609 | } | |
3610 | ||
3611 | /* | |
3612 | * Aggressive migration if: | |
3613 | * 1) task is cache cold, or | |
3614 | * 2) too many balance attempts have failed. | |
3615 | */ | |
3616 | ||
ddcdf6e7 | 3617 | tsk_cache_hot = task_hot(p, env->src_rq->clock_task, env->sd); |
1e3c88bd | 3618 | if (!tsk_cache_hot || |
8e45cb54 | 3619 | env->sd->nr_balance_failed > env->sd->cache_nice_tries) { |
1e3c88bd PZ |
3620 | #ifdef CONFIG_SCHEDSTATS |
3621 | if (tsk_cache_hot) { | |
8e45cb54 | 3622 | schedstat_inc(env->sd, lb_hot_gained[env->idle]); |
41acab88 | 3623 | schedstat_inc(p, se.statistics.nr_forced_migrations); |
1e3c88bd PZ |
3624 | } |
3625 | #endif | |
3626 | return 1; | |
3627 | } | |
3628 | ||
3629 | if (tsk_cache_hot) { | |
41acab88 | 3630 | schedstat_inc(p, se.statistics.nr_failed_migrations_hot); |
1e3c88bd PZ |
3631 | return 0; |
3632 | } | |
3633 | return 1; | |
3634 | } | |
3635 | ||
897c395f PZ |
3636 | /* |
3637 | * move_one_task tries to move exactly one task from busiest to this_rq, as | |
3638 | * part of active balancing operations within "domain". | |
3639 | * Returns 1 if successful and 0 otherwise. | |
3640 | * | |
3641 | * Called with both runqueues locked. | |
3642 | */ | |
8e45cb54 | 3643 | static int move_one_task(struct lb_env *env) |
897c395f PZ |
3644 | { |
3645 | struct task_struct *p, *n; | |
897c395f | 3646 | |
367456c7 PZ |
3647 | list_for_each_entry_safe(p, n, &env->src_rq->cfs_tasks, se.group_node) { |
3648 | if (throttled_lb_pair(task_group(p), env->src_rq->cpu, env->dst_cpu)) | |
3649 | continue; | |
897c395f | 3650 | |
367456c7 PZ |
3651 | if (!can_migrate_task(p, env)) |
3652 | continue; | |
897c395f | 3653 | |
367456c7 PZ |
3654 | move_task(p, env); |
3655 | /* | |
3656 | * Right now, this is only the second place move_task() | |
3657 | * is called, so we can safely collect move_task() | |
3658 | * stats here rather than inside move_task(). | |
3659 | */ | |
3660 | schedstat_inc(env->sd, lb_gained[env->idle]); | |
3661 | return 1; | |
897c395f | 3662 | } |
897c395f PZ |
3663 | return 0; |
3664 | } | |
3665 | ||
367456c7 PZ |
3666 | static unsigned long task_h_load(struct task_struct *p); |
3667 | ||
eb95308e PZ |
3668 | static const unsigned int sched_nr_migrate_break = 32; |
3669 | ||
5d6523eb | 3670 | /* |
bd939f45 | 3671 | * move_tasks tries to move up to imbalance weighted load from busiest to |
5d6523eb PZ |
3672 | * this_rq, as part of a balancing operation within domain "sd". |
3673 | * Returns 1 if successful and 0 otherwise. | |
3674 | * | |
3675 | * Called with both runqueues locked. | |
3676 | */ | |
3677 | static int move_tasks(struct lb_env *env) | |
1e3c88bd | 3678 | { |
5d6523eb PZ |
3679 | struct list_head *tasks = &env->src_rq->cfs_tasks; |
3680 | struct task_struct *p; | |
367456c7 PZ |
3681 | unsigned long load; |
3682 | int pulled = 0; | |
1e3c88bd | 3683 | |
bd939f45 | 3684 | if (env->imbalance <= 0) |
5d6523eb | 3685 | return 0; |
1e3c88bd | 3686 | |
5d6523eb PZ |
3687 | while (!list_empty(tasks)) { |
3688 | p = list_first_entry(tasks, struct task_struct, se.group_node); | |
1e3c88bd | 3689 | |
367456c7 PZ |
3690 | env->loop++; |
3691 | /* We've more or less seen every task there is, call it quits */ | |
5d6523eb | 3692 | if (env->loop > env->loop_max) |
367456c7 | 3693 | break; |
5d6523eb PZ |
3694 | |
3695 | /* take a breather every nr_migrate tasks */ | |
367456c7 | 3696 | if (env->loop > env->loop_break) { |
eb95308e | 3697 | env->loop_break += sched_nr_migrate_break; |
8e45cb54 | 3698 | env->flags |= LBF_NEED_BREAK; |
ee00e66f | 3699 | break; |
a195f004 | 3700 | } |
1e3c88bd | 3701 | |
5d6523eb | 3702 | if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu)) |
367456c7 PZ |
3703 | goto next; |
3704 | ||
3705 | load = task_h_load(p); | |
5d6523eb | 3706 | |
eb95308e | 3707 | if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed) |
367456c7 PZ |
3708 | goto next; |
3709 | ||
bd939f45 | 3710 | if ((load / 2) > env->imbalance) |
367456c7 | 3711 | goto next; |
1e3c88bd | 3712 | |
367456c7 PZ |
3713 | if (!can_migrate_task(p, env)) |
3714 | goto next; | |
1e3c88bd | 3715 | |
ddcdf6e7 | 3716 | move_task(p, env); |
ee00e66f | 3717 | pulled++; |
bd939f45 | 3718 | env->imbalance -= load; |
1e3c88bd PZ |
3719 | |
3720 | #ifdef CONFIG_PREEMPT | |
ee00e66f PZ |
3721 | /* |
3722 | * NEWIDLE balancing is a source of latency, so preemptible | |
3723 | * kernels will stop after the first task is pulled to minimize | |
3724 | * the critical section. | |
3725 | */ | |
5d6523eb | 3726 | if (env->idle == CPU_NEWLY_IDLE) |
ee00e66f | 3727 | break; |
1e3c88bd PZ |
3728 | #endif |
3729 | ||
ee00e66f PZ |
3730 | /* |
3731 | * We only want to steal up to the prescribed amount of | |
3732 | * weighted load. | |
3733 | */ | |
bd939f45 | 3734 | if (env->imbalance <= 0) |
ee00e66f | 3735 | break; |
367456c7 PZ |
3736 | |
3737 | continue; | |
3738 | next: | |
5d6523eb | 3739 | list_move_tail(&p->se.group_node, tasks); |
1e3c88bd | 3740 | } |
5d6523eb | 3741 | |
1e3c88bd | 3742 | /* |
ddcdf6e7 PZ |
3743 | * Right now, this is one of only two places move_task() is called, |
3744 | * so we can safely collect move_task() stats here rather than | |
3745 | * inside move_task(). | |
1e3c88bd | 3746 | */ |
8e45cb54 | 3747 | schedstat_add(env->sd, lb_gained[env->idle], pulled); |
1e3c88bd | 3748 | |
5d6523eb | 3749 | return pulled; |
1e3c88bd PZ |
3750 | } |
3751 | ||
230059de | 3752 | #ifdef CONFIG_FAIR_GROUP_SCHED |
9e3081ca PZ |
3753 | /* |
3754 | * update tg->load_weight by folding this cpu's load_avg | |
3755 | */ | |
67e86250 | 3756 | static int update_shares_cpu(struct task_group *tg, int cpu) |
9e3081ca PZ |
3757 | { |
3758 | struct cfs_rq *cfs_rq; | |
3759 | unsigned long flags; | |
3760 | struct rq *rq; | |
9e3081ca PZ |
3761 | |
3762 | if (!tg->se[cpu]) | |
3763 | return 0; | |
3764 | ||
3765 | rq = cpu_rq(cpu); | |
3766 | cfs_rq = tg->cfs_rq[cpu]; | |
3767 | ||
3768 | raw_spin_lock_irqsave(&rq->lock, flags); | |
3769 | ||
3770 | update_rq_clock(rq); | |
d6b55918 | 3771 | update_cfs_load(cfs_rq, 1); |
aff3e498 | 3772 | update_cfs_rq_blocked_load(cfs_rq, 1); |
9e3081ca PZ |
3773 | |
3774 | /* | |
3775 | * We need to update shares after updating tg->load_weight in | |
3776 | * order to adjust the weight of groups with long running tasks. | |
3777 | */ | |
6d5ab293 | 3778 | update_cfs_shares(cfs_rq); |
9e3081ca PZ |
3779 | |
3780 | raw_spin_unlock_irqrestore(&rq->lock, flags); | |
3781 | ||
3782 | return 0; | |
3783 | } | |
3784 | ||
3785 | static void update_shares(int cpu) | |
3786 | { | |
3787 | struct cfs_rq *cfs_rq; | |
3788 | struct rq *rq = cpu_rq(cpu); | |
3789 | ||
3790 | rcu_read_lock(); | |
9763b67f PZ |
3791 | /* |
3792 | * Iterates the task_group tree in a bottom up fashion, see | |
3793 | * list_add_leaf_cfs_rq() for details. | |
3794 | */ | |
64660c86 PT |
3795 | for_each_leaf_cfs_rq(rq, cfs_rq) { |
3796 | /* throttled entities do not contribute to load */ | |
3797 | if (throttled_hierarchy(cfs_rq)) | |
3798 | continue; | |
3799 | ||
67e86250 | 3800 | update_shares_cpu(cfs_rq->tg, cpu); |
64660c86 | 3801 | } |
9e3081ca PZ |
3802 | rcu_read_unlock(); |
3803 | } | |
3804 | ||
9763b67f PZ |
3805 | /* |
3806 | * Compute the cpu's hierarchical load factor for each task group. | |
3807 | * This needs to be done in a top-down fashion because the load of a child | |
3808 | * group is a fraction of its parents load. | |
3809 | */ | |
3810 | static int tg_load_down(struct task_group *tg, void *data) | |
3811 | { | |
3812 | unsigned long load; | |
3813 | long cpu = (long)data; | |
3814 | ||
3815 | if (!tg->parent) { | |
3816 | load = cpu_rq(cpu)->load.weight; | |
3817 | } else { | |
3818 | load = tg->parent->cfs_rq[cpu]->h_load; | |
3819 | load *= tg->se[cpu]->load.weight; | |
3820 | load /= tg->parent->cfs_rq[cpu]->load.weight + 1; | |
3821 | } | |
3822 | ||
3823 | tg->cfs_rq[cpu]->h_load = load; | |
3824 | ||
3825 | return 0; | |
3826 | } | |
3827 | ||
3828 | static void update_h_load(long cpu) | |
3829 | { | |
a35b6466 PZ |
3830 | struct rq *rq = cpu_rq(cpu); |
3831 | unsigned long now = jiffies; | |
3832 | ||
3833 | if (rq->h_load_throttle == now) | |
3834 | return; | |
3835 | ||
3836 | rq->h_load_throttle = now; | |
3837 | ||
367456c7 | 3838 | rcu_read_lock(); |
9763b67f | 3839 | walk_tg_tree(tg_load_down, tg_nop, (void *)cpu); |
367456c7 | 3840 | rcu_read_unlock(); |
9763b67f PZ |
3841 | } |
3842 | ||
367456c7 | 3843 | static unsigned long task_h_load(struct task_struct *p) |
230059de | 3844 | { |
367456c7 PZ |
3845 | struct cfs_rq *cfs_rq = task_cfs_rq(p); |
3846 | unsigned long load; | |
230059de | 3847 | |
367456c7 PZ |
3848 | load = p->se.load.weight; |
3849 | load = div_u64(load * cfs_rq->h_load, cfs_rq->load.weight + 1); | |
230059de | 3850 | |
367456c7 | 3851 | return load; |
230059de PZ |
3852 | } |
3853 | #else | |
9e3081ca PZ |
3854 | static inline void update_shares(int cpu) |
3855 | { | |
3856 | } | |
3857 | ||
367456c7 | 3858 | static inline void update_h_load(long cpu) |
230059de | 3859 | { |
230059de | 3860 | } |
230059de | 3861 | |
367456c7 | 3862 | static unsigned long task_h_load(struct task_struct *p) |
1e3c88bd | 3863 | { |
367456c7 | 3864 | return p->se.load.weight; |
1e3c88bd | 3865 | } |
230059de | 3866 | #endif |
1e3c88bd | 3867 | |
1e3c88bd PZ |
3868 | /********** Helpers for find_busiest_group ************************/ |
3869 | /* | |
3870 | * sd_lb_stats - Structure to store the statistics of a sched_domain | |
3871 | * during load balancing. | |
3872 | */ | |
3873 | struct sd_lb_stats { | |
3874 | struct sched_group *busiest; /* Busiest group in this sd */ | |
3875 | struct sched_group *this; /* Local group in this sd */ | |
3876 | unsigned long total_load; /* Total load of all groups in sd */ | |
3877 | unsigned long total_pwr; /* Total power of all groups in sd */ | |
3878 | unsigned long avg_load; /* Average load across all groups in sd */ | |
3879 | ||
3880 | /** Statistics of this group */ | |
3881 | unsigned long this_load; | |
3882 | unsigned long this_load_per_task; | |
3883 | unsigned long this_nr_running; | |
fab47622 | 3884 | unsigned long this_has_capacity; |
aae6d3dd | 3885 | unsigned int this_idle_cpus; |
1e3c88bd PZ |
3886 | |
3887 | /* Statistics of the busiest group */ | |
aae6d3dd | 3888 | unsigned int busiest_idle_cpus; |
1e3c88bd PZ |
3889 | unsigned long max_load; |
3890 | unsigned long busiest_load_per_task; | |
3891 | unsigned long busiest_nr_running; | |
dd5feea1 | 3892 | unsigned long busiest_group_capacity; |
fab47622 | 3893 | unsigned long busiest_has_capacity; |
aae6d3dd | 3894 | unsigned int busiest_group_weight; |
1e3c88bd PZ |
3895 | |
3896 | int group_imb; /* Is there imbalance in this sd */ | |
1e3c88bd PZ |
3897 | }; |
3898 | ||
3899 | /* | |
3900 | * sg_lb_stats - stats of a sched_group required for load_balancing | |
3901 | */ | |
3902 | struct sg_lb_stats { | |
3903 | unsigned long avg_load; /*Avg load across the CPUs of the group */ | |
3904 | unsigned long group_load; /* Total load over the CPUs of the group */ | |
3905 | unsigned long sum_nr_running; /* Nr tasks running in the group */ | |
3906 | unsigned long sum_weighted_load; /* Weighted load of group's tasks */ | |
3907 | unsigned long group_capacity; | |
aae6d3dd SS |
3908 | unsigned long idle_cpus; |
3909 | unsigned long group_weight; | |
1e3c88bd | 3910 | int group_imb; /* Is there an imbalance in the group ? */ |
fab47622 | 3911 | int group_has_capacity; /* Is there extra capacity in the group? */ |
1e3c88bd PZ |
3912 | }; |
3913 | ||
1e3c88bd PZ |
3914 | /** |
3915 | * get_sd_load_idx - Obtain the load index for a given sched domain. | |
3916 | * @sd: The sched_domain whose load_idx is to be obtained. | |
3917 | * @idle: The Idle status of the CPU for whose sd load_icx is obtained. | |
3918 | */ | |
3919 | static inline int get_sd_load_idx(struct sched_domain *sd, | |
3920 | enum cpu_idle_type idle) | |
3921 | { | |
3922 | int load_idx; | |
3923 | ||
3924 | switch (idle) { | |
3925 | case CPU_NOT_IDLE: | |
3926 | load_idx = sd->busy_idx; | |
3927 | break; | |
3928 | ||
3929 | case CPU_NEWLY_IDLE: | |
3930 | load_idx = sd->newidle_idx; | |
3931 | break; | |
3932 | default: | |
3933 | load_idx = sd->idle_idx; | |
3934 | break; | |
3935 | } | |
3936 | ||
3937 | return load_idx; | |
3938 | } | |
3939 | ||
1e3c88bd PZ |
3940 | unsigned long default_scale_freq_power(struct sched_domain *sd, int cpu) |
3941 | { | |
1399fa78 | 3942 | return SCHED_POWER_SCALE; |
1e3c88bd PZ |
3943 | } |
3944 | ||
3945 | unsigned long __weak arch_scale_freq_power(struct sched_domain *sd, int cpu) | |
3946 | { | |
3947 | return default_scale_freq_power(sd, cpu); | |
3948 | } | |
3949 | ||
3950 | unsigned long default_scale_smt_power(struct sched_domain *sd, int cpu) | |
3951 | { | |
669c55e9 | 3952 | unsigned long weight = sd->span_weight; |
1e3c88bd PZ |
3953 | unsigned long smt_gain = sd->smt_gain; |
3954 | ||
3955 | smt_gain /= weight; | |
3956 | ||
3957 | return smt_gain; | |
3958 | } | |
3959 | ||
3960 | unsigned long __weak arch_scale_smt_power(struct sched_domain *sd, int cpu) | |
3961 | { | |
3962 | return default_scale_smt_power(sd, cpu); | |
3963 | } | |
3964 | ||
3965 | unsigned long scale_rt_power(int cpu) | |
3966 | { | |
3967 | struct rq *rq = cpu_rq(cpu); | |
b654f7de | 3968 | u64 total, available, age_stamp, avg; |
1e3c88bd | 3969 | |
b654f7de PZ |
3970 | /* |
3971 | * Since we're reading these variables without serialization make sure | |
3972 | * we read them once before doing sanity checks on them. | |
3973 | */ | |
3974 | age_stamp = ACCESS_ONCE(rq->age_stamp); | |
3975 | avg = ACCESS_ONCE(rq->rt_avg); | |
3976 | ||
3977 | total = sched_avg_period() + (rq->clock - age_stamp); | |
aa483808 | 3978 | |
b654f7de | 3979 | if (unlikely(total < avg)) { |
aa483808 VP |
3980 | /* Ensures that power won't end up being negative */ |
3981 | available = 0; | |
3982 | } else { | |
b654f7de | 3983 | available = total - avg; |
aa483808 | 3984 | } |
1e3c88bd | 3985 | |
1399fa78 NR |
3986 | if (unlikely((s64)total < SCHED_POWER_SCALE)) |
3987 | total = SCHED_POWER_SCALE; | |
1e3c88bd | 3988 | |
1399fa78 | 3989 | total >>= SCHED_POWER_SHIFT; |
1e3c88bd PZ |
3990 | |
3991 | return div_u64(available, total); | |
3992 | } | |
3993 | ||
3994 | static void update_cpu_power(struct sched_domain *sd, int cpu) | |
3995 | { | |
669c55e9 | 3996 | unsigned long weight = sd->span_weight; |
1399fa78 | 3997 | unsigned long power = SCHED_POWER_SCALE; |
1e3c88bd PZ |
3998 | struct sched_group *sdg = sd->groups; |
3999 | ||
1e3c88bd PZ |
4000 | if ((sd->flags & SD_SHARE_CPUPOWER) && weight > 1) { |
4001 | if (sched_feat(ARCH_POWER)) | |
4002 | power *= arch_scale_smt_power(sd, cpu); | |
4003 | else | |
4004 | power *= default_scale_smt_power(sd, cpu); | |
4005 | ||
1399fa78 | 4006 | power >>= SCHED_POWER_SHIFT; |
1e3c88bd PZ |
4007 | } |
4008 | ||
9c3f75cb | 4009 | sdg->sgp->power_orig = power; |
9d5efe05 SV |
4010 | |
4011 | if (sched_feat(ARCH_POWER)) | |
4012 | power *= arch_scale_freq_power(sd, cpu); | |
4013 | else | |
4014 | power *= default_scale_freq_power(sd, cpu); | |
4015 | ||
1399fa78 | 4016 | power >>= SCHED_POWER_SHIFT; |
9d5efe05 | 4017 | |
1e3c88bd | 4018 | power *= scale_rt_power(cpu); |
1399fa78 | 4019 | power >>= SCHED_POWER_SHIFT; |
1e3c88bd PZ |
4020 | |
4021 | if (!power) | |
4022 | power = 1; | |
4023 | ||
e51fd5e2 | 4024 | cpu_rq(cpu)->cpu_power = power; |
9c3f75cb | 4025 | sdg->sgp->power = power; |
1e3c88bd PZ |
4026 | } |
4027 | ||
029632fb | 4028 | void update_group_power(struct sched_domain *sd, int cpu) |
1e3c88bd PZ |
4029 | { |
4030 | struct sched_domain *child = sd->child; | |
4031 | struct sched_group *group, *sdg = sd->groups; | |
4032 | unsigned long power; | |
4ec4412e VG |
4033 | unsigned long interval; |
4034 | ||
4035 | interval = msecs_to_jiffies(sd->balance_interval); | |
4036 | interval = clamp(interval, 1UL, max_load_balance_interval); | |
4037 | sdg->sgp->next_update = jiffies + interval; | |
1e3c88bd PZ |
4038 | |
4039 | if (!child) { | |
4040 | update_cpu_power(sd, cpu); | |
4041 | return; | |
4042 | } | |
4043 | ||
4044 | power = 0; | |
4045 | ||
74a5ce20 PZ |
4046 | if (child->flags & SD_OVERLAP) { |
4047 | /* | |
4048 | * SD_OVERLAP domains cannot assume that child groups | |
4049 | * span the current group. | |
4050 | */ | |
4051 | ||
4052 | for_each_cpu(cpu, sched_group_cpus(sdg)) | |
4053 | power += power_of(cpu); | |
4054 | } else { | |
4055 | /* | |
4056 | * !SD_OVERLAP domains can assume that child groups | |
4057 | * span the current group. | |
4058 | */ | |
4059 | ||
4060 | group = child->groups; | |
4061 | do { | |
4062 | power += group->sgp->power; | |
4063 | group = group->next; | |
4064 | } while (group != child->groups); | |
4065 | } | |
1e3c88bd | 4066 | |
c3decf0d | 4067 | sdg->sgp->power_orig = sdg->sgp->power = power; |
1e3c88bd PZ |
4068 | } |
4069 | ||
9d5efe05 SV |
4070 | /* |
4071 | * Try and fix up capacity for tiny siblings, this is needed when | |
4072 | * things like SD_ASYM_PACKING need f_b_g to select another sibling | |
4073 | * which on its own isn't powerful enough. | |
4074 | * | |
4075 | * See update_sd_pick_busiest() and check_asym_packing(). | |
4076 | */ | |
4077 | static inline int | |
4078 | fix_small_capacity(struct sched_domain *sd, struct sched_group *group) | |
4079 | { | |
4080 | /* | |
1399fa78 | 4081 | * Only siblings can have significantly less than SCHED_POWER_SCALE |
9d5efe05 | 4082 | */ |
a6c75f2f | 4083 | if (!(sd->flags & SD_SHARE_CPUPOWER)) |
9d5efe05 SV |
4084 | return 0; |
4085 | ||
4086 | /* | |
4087 | * If ~90% of the cpu_power is still there, we're good. | |
4088 | */ | |
9c3f75cb | 4089 | if (group->sgp->power * 32 > group->sgp->power_orig * 29) |
9d5efe05 SV |
4090 | return 1; |
4091 | ||
4092 | return 0; | |
4093 | } | |
4094 | ||
1e3c88bd PZ |
4095 | /** |
4096 | * update_sg_lb_stats - Update sched_group's statistics for load balancing. | |
cd96891d | 4097 | * @env: The load balancing environment. |
1e3c88bd | 4098 | * @group: sched_group whose statistics are to be updated. |
1e3c88bd | 4099 | * @load_idx: Load index of sched_domain of this_cpu for load calc. |
1e3c88bd | 4100 | * @local_group: Does group contain this_cpu. |
1e3c88bd PZ |
4101 | * @balance: Should we balance. |
4102 | * @sgs: variable to hold the statistics for this group. | |
4103 | */ | |
bd939f45 PZ |
4104 | static inline void update_sg_lb_stats(struct lb_env *env, |
4105 | struct sched_group *group, int load_idx, | |
b9403130 | 4106 | int local_group, int *balance, struct sg_lb_stats *sgs) |
1e3c88bd | 4107 | { |
e44bc5c5 PZ |
4108 | unsigned long nr_running, max_nr_running, min_nr_running; |
4109 | unsigned long load, max_cpu_load, min_cpu_load; | |
04f733b4 | 4110 | unsigned int balance_cpu = -1, first_idle_cpu = 0; |
dd5feea1 | 4111 | unsigned long avg_load_per_task = 0; |
bd939f45 | 4112 | int i; |
1e3c88bd | 4113 | |
871e35bc | 4114 | if (local_group) |
c1174876 | 4115 | balance_cpu = group_balance_cpu(group); |
1e3c88bd PZ |
4116 | |
4117 | /* Tally up the load of all CPUs in the group */ | |
1e3c88bd PZ |
4118 | max_cpu_load = 0; |
4119 | min_cpu_load = ~0UL; | |
2582f0eb | 4120 | max_nr_running = 0; |
e44bc5c5 | 4121 | min_nr_running = ~0UL; |
1e3c88bd | 4122 | |
b9403130 | 4123 | for_each_cpu_and(i, sched_group_cpus(group), env->cpus) { |
1e3c88bd PZ |
4124 | struct rq *rq = cpu_rq(i); |
4125 | ||
e44bc5c5 PZ |
4126 | nr_running = rq->nr_running; |
4127 | ||
1e3c88bd PZ |
4128 | /* Bias balancing toward cpus of our domain */ |
4129 | if (local_group) { | |
c1174876 PZ |
4130 | if (idle_cpu(i) && !first_idle_cpu && |
4131 | cpumask_test_cpu(i, sched_group_mask(group))) { | |
04f733b4 | 4132 | first_idle_cpu = 1; |
1e3c88bd PZ |
4133 | balance_cpu = i; |
4134 | } | |
04f733b4 PZ |
4135 | |
4136 | load = target_load(i, load_idx); | |
1e3c88bd PZ |
4137 | } else { |
4138 | load = source_load(i, load_idx); | |
e44bc5c5 | 4139 | if (load > max_cpu_load) |
1e3c88bd PZ |
4140 | max_cpu_load = load; |
4141 | if (min_cpu_load > load) | |
4142 | min_cpu_load = load; | |
e44bc5c5 PZ |
4143 | |
4144 | if (nr_running > max_nr_running) | |
4145 | max_nr_running = nr_running; | |
4146 | if (min_nr_running > nr_running) | |
4147 | min_nr_running = nr_running; | |
1e3c88bd PZ |
4148 | } |
4149 | ||
4150 | sgs->group_load += load; | |
e44bc5c5 | 4151 | sgs->sum_nr_running += nr_running; |
1e3c88bd | 4152 | sgs->sum_weighted_load += weighted_cpuload(i); |
aae6d3dd SS |
4153 | if (idle_cpu(i)) |
4154 | sgs->idle_cpus++; | |
1e3c88bd PZ |
4155 | } |
4156 | ||
4157 | /* | |
4158 | * First idle cpu or the first cpu(busiest) in this sched group | |
4159 | * is eligible for doing load balancing at this and above | |
4160 | * domains. In the newly idle case, we will allow all the cpu's | |
4161 | * to do the newly idle load balance. | |
4162 | */ | |
4ec4412e | 4163 | if (local_group) { |
bd939f45 | 4164 | if (env->idle != CPU_NEWLY_IDLE) { |
04f733b4 | 4165 | if (balance_cpu != env->dst_cpu) { |
4ec4412e VG |
4166 | *balance = 0; |
4167 | return; | |
4168 | } | |
bd939f45 | 4169 | update_group_power(env->sd, env->dst_cpu); |
4ec4412e | 4170 | } else if (time_after_eq(jiffies, group->sgp->next_update)) |
bd939f45 | 4171 | update_group_power(env->sd, env->dst_cpu); |
1e3c88bd PZ |
4172 | } |
4173 | ||
4174 | /* Adjust by relative CPU power of the group */ | |
9c3f75cb | 4175 | sgs->avg_load = (sgs->group_load*SCHED_POWER_SCALE) / group->sgp->power; |
1e3c88bd | 4176 | |
1e3c88bd PZ |
4177 | /* |
4178 | * Consider the group unbalanced when the imbalance is larger | |
866ab43e | 4179 | * than the average weight of a task. |
1e3c88bd PZ |
4180 | * |
4181 | * APZ: with cgroup the avg task weight can vary wildly and | |
4182 | * might not be a suitable number - should we keep a | |
4183 | * normalized nr_running number somewhere that negates | |
4184 | * the hierarchy? | |
4185 | */ | |
dd5feea1 SS |
4186 | if (sgs->sum_nr_running) |
4187 | avg_load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running; | |
1e3c88bd | 4188 | |
e44bc5c5 PZ |
4189 | if ((max_cpu_load - min_cpu_load) >= avg_load_per_task && |
4190 | (max_nr_running - min_nr_running) > 1) | |
1e3c88bd PZ |
4191 | sgs->group_imb = 1; |
4192 | ||
9c3f75cb | 4193 | sgs->group_capacity = DIV_ROUND_CLOSEST(group->sgp->power, |
1399fa78 | 4194 | SCHED_POWER_SCALE); |
9d5efe05 | 4195 | if (!sgs->group_capacity) |
bd939f45 | 4196 | sgs->group_capacity = fix_small_capacity(env->sd, group); |
aae6d3dd | 4197 | sgs->group_weight = group->group_weight; |
fab47622 NR |
4198 | |
4199 | if (sgs->group_capacity > sgs->sum_nr_running) | |
4200 | sgs->group_has_capacity = 1; | |
1e3c88bd PZ |
4201 | } |
4202 | ||
532cb4c4 MN |
4203 | /** |
4204 | * update_sd_pick_busiest - return 1 on busiest group | |
cd96891d | 4205 | * @env: The load balancing environment. |
532cb4c4 MN |
4206 | * @sds: sched_domain statistics |
4207 | * @sg: sched_group candidate to be checked for being the busiest | |
b6b12294 | 4208 | * @sgs: sched_group statistics |
532cb4c4 MN |
4209 | * |
4210 | * Determine if @sg is a busier group than the previously selected | |
4211 | * busiest group. | |
4212 | */ | |
bd939f45 | 4213 | static bool update_sd_pick_busiest(struct lb_env *env, |
532cb4c4 MN |
4214 | struct sd_lb_stats *sds, |
4215 | struct sched_group *sg, | |
bd939f45 | 4216 | struct sg_lb_stats *sgs) |
532cb4c4 MN |
4217 | { |
4218 | if (sgs->avg_load <= sds->max_load) | |
4219 | return false; | |
4220 | ||
4221 | if (sgs->sum_nr_running > sgs->group_capacity) | |
4222 | return true; | |
4223 | ||
4224 | if (sgs->group_imb) | |
4225 | return true; | |
4226 | ||
4227 | /* | |
4228 | * ASYM_PACKING needs to move all the work to the lowest | |
4229 | * numbered CPUs in the group, therefore mark all groups | |
4230 | * higher than ourself as busy. | |
4231 | */ | |
bd939f45 PZ |
4232 | if ((env->sd->flags & SD_ASYM_PACKING) && sgs->sum_nr_running && |
4233 | env->dst_cpu < group_first_cpu(sg)) { | |
532cb4c4 MN |
4234 | if (!sds->busiest) |
4235 | return true; | |
4236 | ||
4237 | if (group_first_cpu(sds->busiest) > group_first_cpu(sg)) | |
4238 | return true; | |
4239 | } | |
4240 | ||
4241 | return false; | |
4242 | } | |
4243 | ||
1e3c88bd | 4244 | /** |
461819ac | 4245 | * update_sd_lb_stats - Update sched_domain's statistics for load balancing. |
cd96891d | 4246 | * @env: The load balancing environment. |
1e3c88bd PZ |
4247 | * @balance: Should we balance. |
4248 | * @sds: variable to hold the statistics for this sched_domain. | |
4249 | */ | |
bd939f45 | 4250 | static inline void update_sd_lb_stats(struct lb_env *env, |
b9403130 | 4251 | int *balance, struct sd_lb_stats *sds) |
1e3c88bd | 4252 | { |
bd939f45 PZ |
4253 | struct sched_domain *child = env->sd->child; |
4254 | struct sched_group *sg = env->sd->groups; | |
1e3c88bd PZ |
4255 | struct sg_lb_stats sgs; |
4256 | int load_idx, prefer_sibling = 0; | |
4257 | ||
4258 | if (child && child->flags & SD_PREFER_SIBLING) | |
4259 | prefer_sibling = 1; | |
4260 | ||
bd939f45 | 4261 | load_idx = get_sd_load_idx(env->sd, env->idle); |
1e3c88bd PZ |
4262 | |
4263 | do { | |
4264 | int local_group; | |
4265 | ||
bd939f45 | 4266 | local_group = cpumask_test_cpu(env->dst_cpu, sched_group_cpus(sg)); |
1e3c88bd | 4267 | memset(&sgs, 0, sizeof(sgs)); |
b9403130 | 4268 | update_sg_lb_stats(env, sg, load_idx, local_group, balance, &sgs); |
1e3c88bd | 4269 | |
8f190fb3 | 4270 | if (local_group && !(*balance)) |
1e3c88bd PZ |
4271 | return; |
4272 | ||
4273 | sds->total_load += sgs.group_load; | |
9c3f75cb | 4274 | sds->total_pwr += sg->sgp->power; |
1e3c88bd PZ |
4275 | |
4276 | /* | |
4277 | * In case the child domain prefers tasks go to siblings | |
532cb4c4 | 4278 | * first, lower the sg capacity to one so that we'll try |
75dd321d NR |
4279 | * and move all the excess tasks away. We lower the capacity |
4280 | * of a group only if the local group has the capacity to fit | |
4281 | * these excess tasks, i.e. nr_running < group_capacity. The | |
4282 | * extra check prevents the case where you always pull from the | |
4283 | * heaviest group when it is already under-utilized (possible | |
4284 | * with a large weight task outweighs the tasks on the system). | |
1e3c88bd | 4285 | */ |
75dd321d | 4286 | if (prefer_sibling && !local_group && sds->this_has_capacity) |
1e3c88bd PZ |
4287 | sgs.group_capacity = min(sgs.group_capacity, 1UL); |
4288 | ||
4289 | if (local_group) { | |
4290 | sds->this_load = sgs.avg_load; | |
532cb4c4 | 4291 | sds->this = sg; |
1e3c88bd PZ |
4292 | sds->this_nr_running = sgs.sum_nr_running; |
4293 | sds->this_load_per_task = sgs.sum_weighted_load; | |
fab47622 | 4294 | sds->this_has_capacity = sgs.group_has_capacity; |
aae6d3dd | 4295 | sds->this_idle_cpus = sgs.idle_cpus; |
bd939f45 | 4296 | } else if (update_sd_pick_busiest(env, sds, sg, &sgs)) { |
1e3c88bd | 4297 | sds->max_load = sgs.avg_load; |
532cb4c4 | 4298 | sds->busiest = sg; |
1e3c88bd | 4299 | sds->busiest_nr_running = sgs.sum_nr_running; |
aae6d3dd | 4300 | sds->busiest_idle_cpus = sgs.idle_cpus; |
dd5feea1 | 4301 | sds->busiest_group_capacity = sgs.group_capacity; |
1e3c88bd | 4302 | sds->busiest_load_per_task = sgs.sum_weighted_load; |
fab47622 | 4303 | sds->busiest_has_capacity = sgs.group_has_capacity; |
aae6d3dd | 4304 | sds->busiest_group_weight = sgs.group_weight; |
1e3c88bd PZ |
4305 | sds->group_imb = sgs.group_imb; |
4306 | } | |
4307 | ||
532cb4c4 | 4308 | sg = sg->next; |
bd939f45 | 4309 | } while (sg != env->sd->groups); |
532cb4c4 MN |
4310 | } |
4311 | ||
532cb4c4 MN |
4312 | /** |
4313 | * check_asym_packing - Check to see if the group is packed into the | |
4314 | * sched doman. | |
4315 | * | |
4316 | * This is primarily intended to used at the sibling level. Some | |
4317 | * cores like POWER7 prefer to use lower numbered SMT threads. In the | |
4318 | * case of POWER7, it can move to lower SMT modes only when higher | |
4319 | * threads are idle. When in lower SMT modes, the threads will | |
4320 | * perform better since they share less core resources. Hence when we | |
4321 | * have idle threads, we want them to be the higher ones. | |
4322 | * | |
4323 | * This packing function is run on idle threads. It checks to see if | |
4324 | * the busiest CPU in this domain (core in the P7 case) has a higher | |
4325 | * CPU number than the packing function is being run on. Here we are | |
4326 | * assuming lower CPU number will be equivalent to lower a SMT thread | |
4327 | * number. | |
4328 | * | |
b6b12294 MN |
4329 | * Returns 1 when packing is required and a task should be moved to |
4330 | * this CPU. The amount of the imbalance is returned in *imbalance. | |
4331 | * | |
cd96891d | 4332 | * @env: The load balancing environment. |
532cb4c4 | 4333 | * @sds: Statistics of the sched_domain which is to be packed |
532cb4c4 | 4334 | */ |
bd939f45 | 4335 | static int check_asym_packing(struct lb_env *env, struct sd_lb_stats *sds) |
532cb4c4 MN |
4336 | { |
4337 | int busiest_cpu; | |
4338 | ||
bd939f45 | 4339 | if (!(env->sd->flags & SD_ASYM_PACKING)) |
532cb4c4 MN |
4340 | return 0; |
4341 | ||
4342 | if (!sds->busiest) | |
4343 | return 0; | |
4344 | ||
4345 | busiest_cpu = group_first_cpu(sds->busiest); | |
bd939f45 | 4346 | if (env->dst_cpu > busiest_cpu) |
532cb4c4 MN |
4347 | return 0; |
4348 | ||
bd939f45 PZ |
4349 | env->imbalance = DIV_ROUND_CLOSEST( |
4350 | sds->max_load * sds->busiest->sgp->power, SCHED_POWER_SCALE); | |
4351 | ||
532cb4c4 | 4352 | return 1; |
1e3c88bd PZ |
4353 | } |
4354 | ||
4355 | /** | |
4356 | * fix_small_imbalance - Calculate the minor imbalance that exists | |
4357 | * amongst the groups of a sched_domain, during | |
4358 | * load balancing. | |
cd96891d | 4359 | * @env: The load balancing environment. |
1e3c88bd | 4360 | * @sds: Statistics of the sched_domain whose imbalance is to be calculated. |
1e3c88bd | 4361 | */ |
bd939f45 PZ |
4362 | static inline |
4363 | void fix_small_imbalance(struct lb_env *env, struct sd_lb_stats *sds) | |
1e3c88bd PZ |
4364 | { |
4365 | unsigned long tmp, pwr_now = 0, pwr_move = 0; | |
4366 | unsigned int imbn = 2; | |
dd5feea1 | 4367 | unsigned long scaled_busy_load_per_task; |
1e3c88bd PZ |
4368 | |
4369 | if (sds->this_nr_running) { | |
4370 | sds->this_load_per_task /= sds->this_nr_running; | |
4371 | if (sds->busiest_load_per_task > | |
4372 | sds->this_load_per_task) | |
4373 | imbn = 1; | |
bd939f45 | 4374 | } else { |
1e3c88bd | 4375 | sds->this_load_per_task = |
bd939f45 PZ |
4376 | cpu_avg_load_per_task(env->dst_cpu); |
4377 | } | |
1e3c88bd | 4378 | |
dd5feea1 | 4379 | scaled_busy_load_per_task = sds->busiest_load_per_task |
1399fa78 | 4380 | * SCHED_POWER_SCALE; |
9c3f75cb | 4381 | scaled_busy_load_per_task /= sds->busiest->sgp->power; |
dd5feea1 SS |
4382 | |
4383 | if (sds->max_load - sds->this_load + scaled_busy_load_per_task >= | |
4384 | (scaled_busy_load_per_task * imbn)) { | |
bd939f45 | 4385 | env->imbalance = sds->busiest_load_per_task; |
1e3c88bd PZ |
4386 | return; |
4387 | } | |
4388 | ||
4389 | /* | |
4390 | * OK, we don't have enough imbalance to justify moving tasks, | |
4391 | * however we may be able to increase total CPU power used by | |
4392 | * moving them. | |
4393 | */ | |
4394 | ||
9c3f75cb | 4395 | pwr_now += sds->busiest->sgp->power * |
1e3c88bd | 4396 | min(sds->busiest_load_per_task, sds->max_load); |
9c3f75cb | 4397 | pwr_now += sds->this->sgp->power * |
1e3c88bd | 4398 | min(sds->this_load_per_task, sds->this_load); |
1399fa78 | 4399 | pwr_now /= SCHED_POWER_SCALE; |
1e3c88bd PZ |
4400 | |
4401 | /* Amount of load we'd subtract */ | |
1399fa78 | 4402 | tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) / |
9c3f75cb | 4403 | sds->busiest->sgp->power; |
1e3c88bd | 4404 | if (sds->max_load > tmp) |
9c3f75cb | 4405 | pwr_move += sds->busiest->sgp->power * |
1e3c88bd PZ |
4406 | min(sds->busiest_load_per_task, sds->max_load - tmp); |
4407 | ||
4408 | /* Amount of load we'd add */ | |
9c3f75cb | 4409 | if (sds->max_load * sds->busiest->sgp->power < |
1399fa78 | 4410 | sds->busiest_load_per_task * SCHED_POWER_SCALE) |
9c3f75cb PZ |
4411 | tmp = (sds->max_load * sds->busiest->sgp->power) / |
4412 | sds->this->sgp->power; | |
1e3c88bd | 4413 | else |
1399fa78 | 4414 | tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) / |
9c3f75cb PZ |
4415 | sds->this->sgp->power; |
4416 | pwr_move += sds->this->sgp->power * | |
1e3c88bd | 4417 | min(sds->this_load_per_task, sds->this_load + tmp); |
1399fa78 | 4418 | pwr_move /= SCHED_POWER_SCALE; |
1e3c88bd PZ |
4419 | |
4420 | /* Move if we gain throughput */ | |
4421 | if (pwr_move > pwr_now) | |
bd939f45 | 4422 | env->imbalance = sds->busiest_load_per_task; |
1e3c88bd PZ |
4423 | } |
4424 | ||
4425 | /** | |
4426 | * calculate_imbalance - Calculate the amount of imbalance present within the | |
4427 | * groups of a given sched_domain during load balance. | |
bd939f45 | 4428 | * @env: load balance environment |
1e3c88bd | 4429 | * @sds: statistics of the sched_domain whose imbalance is to be calculated. |
1e3c88bd | 4430 | */ |
bd939f45 | 4431 | static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *sds) |
1e3c88bd | 4432 | { |
dd5feea1 SS |
4433 | unsigned long max_pull, load_above_capacity = ~0UL; |
4434 | ||
4435 | sds->busiest_load_per_task /= sds->busiest_nr_running; | |
4436 | if (sds->group_imb) { | |
4437 | sds->busiest_load_per_task = | |
4438 | min(sds->busiest_load_per_task, sds->avg_load); | |
4439 | } | |
4440 | ||
1e3c88bd PZ |
4441 | /* |
4442 | * In the presence of smp nice balancing, certain scenarios can have | |
4443 | * max load less than avg load(as we skip the groups at or below | |
4444 | * its cpu_power, while calculating max_load..) | |
4445 | */ | |
4446 | if (sds->max_load < sds->avg_load) { | |
bd939f45 PZ |
4447 | env->imbalance = 0; |
4448 | return fix_small_imbalance(env, sds); | |
1e3c88bd PZ |
4449 | } |
4450 | ||
dd5feea1 SS |
4451 | if (!sds->group_imb) { |
4452 | /* | |
4453 | * Don't want to pull so many tasks that a group would go idle. | |
4454 | */ | |
4455 | load_above_capacity = (sds->busiest_nr_running - | |
4456 | sds->busiest_group_capacity); | |
4457 | ||
1399fa78 | 4458 | load_above_capacity *= (SCHED_LOAD_SCALE * SCHED_POWER_SCALE); |
dd5feea1 | 4459 | |
9c3f75cb | 4460 | load_above_capacity /= sds->busiest->sgp->power; |
dd5feea1 SS |
4461 | } |
4462 | ||
4463 | /* | |
4464 | * We're trying to get all the cpus to the average_load, so we don't | |
4465 | * want to push ourselves above the average load, nor do we wish to | |
4466 | * reduce the max loaded cpu below the average load. At the same time, | |
4467 | * we also don't want to reduce the group load below the group capacity | |
4468 | * (so that we can implement power-savings policies etc). Thus we look | |
4469 | * for the minimum possible imbalance. | |
4470 | * Be careful of negative numbers as they'll appear as very large values | |
4471 | * with unsigned longs. | |
4472 | */ | |
4473 | max_pull = min(sds->max_load - sds->avg_load, load_above_capacity); | |
1e3c88bd PZ |
4474 | |
4475 | /* How much load to actually move to equalise the imbalance */ | |
bd939f45 | 4476 | env->imbalance = min(max_pull * sds->busiest->sgp->power, |
9c3f75cb | 4477 | (sds->avg_load - sds->this_load) * sds->this->sgp->power) |
1399fa78 | 4478 | / SCHED_POWER_SCALE; |
1e3c88bd PZ |
4479 | |
4480 | /* | |
4481 | * if *imbalance is less than the average load per runnable task | |
25985edc | 4482 | * there is no guarantee that any tasks will be moved so we'll have |
1e3c88bd PZ |
4483 | * a think about bumping its value to force at least one task to be |
4484 | * moved | |
4485 | */ | |
bd939f45 PZ |
4486 | if (env->imbalance < sds->busiest_load_per_task) |
4487 | return fix_small_imbalance(env, sds); | |
1e3c88bd PZ |
4488 | |
4489 | } | |
fab47622 | 4490 | |
1e3c88bd PZ |
4491 | /******* find_busiest_group() helpers end here *********************/ |
4492 | ||
4493 | /** | |
4494 | * find_busiest_group - Returns the busiest group within the sched_domain | |
4495 | * if there is an imbalance. If there isn't an imbalance, and | |
4496 | * the user has opted for power-savings, it returns a group whose | |
4497 | * CPUs can be put to idle by rebalancing those tasks elsewhere, if | |
4498 | * such a group exists. | |
4499 | * | |
4500 | * Also calculates the amount of weighted load which should be moved | |
4501 | * to restore balance. | |
4502 | * | |
cd96891d | 4503 | * @env: The load balancing environment. |
1e3c88bd PZ |
4504 | * @balance: Pointer to a variable indicating if this_cpu |
4505 | * is the appropriate cpu to perform load balancing at this_level. | |
4506 | * | |
4507 | * Returns: - the busiest group if imbalance exists. | |
4508 | * - If no imbalance and user has opted for power-savings balance, | |
4509 | * return the least loaded group whose CPUs can be | |
4510 | * put to idle by rebalancing its tasks onto our group. | |
4511 | */ | |
4512 | static struct sched_group * | |
b9403130 | 4513 | find_busiest_group(struct lb_env *env, int *balance) |
1e3c88bd PZ |
4514 | { |
4515 | struct sd_lb_stats sds; | |
4516 | ||
4517 | memset(&sds, 0, sizeof(sds)); | |
4518 | ||
4519 | /* | |
4520 | * Compute the various statistics relavent for load balancing at | |
4521 | * this level. | |
4522 | */ | |
b9403130 | 4523 | update_sd_lb_stats(env, balance, &sds); |
1e3c88bd | 4524 | |
cc57aa8f PZ |
4525 | /* |
4526 | * this_cpu is not the appropriate cpu to perform load balancing at | |
4527 | * this level. | |
1e3c88bd | 4528 | */ |
8f190fb3 | 4529 | if (!(*balance)) |
1e3c88bd PZ |
4530 | goto ret; |
4531 | ||
bd939f45 PZ |
4532 | if ((env->idle == CPU_IDLE || env->idle == CPU_NEWLY_IDLE) && |
4533 | check_asym_packing(env, &sds)) | |
532cb4c4 MN |
4534 | return sds.busiest; |
4535 | ||
cc57aa8f | 4536 | /* There is no busy sibling group to pull tasks from */ |
1e3c88bd PZ |
4537 | if (!sds.busiest || sds.busiest_nr_running == 0) |
4538 | goto out_balanced; | |
4539 | ||
1399fa78 | 4540 | sds.avg_load = (SCHED_POWER_SCALE * sds.total_load) / sds.total_pwr; |
b0432d8f | 4541 | |
866ab43e PZ |
4542 | /* |
4543 | * If the busiest group is imbalanced the below checks don't | |
4544 | * work because they assumes all things are equal, which typically | |
4545 | * isn't true due to cpus_allowed constraints and the like. | |
4546 | */ | |
4547 | if (sds.group_imb) | |
4548 | goto force_balance; | |
4549 | ||
cc57aa8f | 4550 | /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */ |
bd939f45 | 4551 | if (env->idle == CPU_NEWLY_IDLE && sds.this_has_capacity && |
fab47622 NR |
4552 | !sds.busiest_has_capacity) |
4553 | goto force_balance; | |
4554 | ||
cc57aa8f PZ |
4555 | /* |
4556 | * If the local group is more busy than the selected busiest group | |
4557 | * don't try and pull any tasks. | |
4558 | */ | |
1e3c88bd PZ |
4559 | if (sds.this_load >= sds.max_load) |
4560 | goto out_balanced; | |
4561 | ||
cc57aa8f PZ |
4562 | /* |
4563 | * Don't pull any tasks if this group is already above the domain | |
4564 | * average load. | |
4565 | */ | |
1e3c88bd PZ |
4566 | if (sds.this_load >= sds.avg_load) |
4567 | goto out_balanced; | |
4568 | ||
bd939f45 | 4569 | if (env->idle == CPU_IDLE) { |
aae6d3dd SS |
4570 | /* |
4571 | * This cpu is idle. If the busiest group load doesn't | |
4572 | * have more tasks than the number of available cpu's and | |
4573 | * there is no imbalance between this and busiest group | |
4574 | * wrt to idle cpu's, it is balanced. | |
4575 | */ | |
c186fafe | 4576 | if ((sds.this_idle_cpus <= sds.busiest_idle_cpus + 1) && |
aae6d3dd SS |
4577 | sds.busiest_nr_running <= sds.busiest_group_weight) |
4578 | goto out_balanced; | |
c186fafe PZ |
4579 | } else { |
4580 | /* | |
4581 | * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use | |
4582 | * imbalance_pct to be conservative. | |
4583 | */ | |
bd939f45 | 4584 | if (100 * sds.max_load <= env->sd->imbalance_pct * sds.this_load) |
c186fafe | 4585 | goto out_balanced; |
aae6d3dd | 4586 | } |
1e3c88bd | 4587 | |
fab47622 | 4588 | force_balance: |
1e3c88bd | 4589 | /* Looks like there is an imbalance. Compute it */ |
bd939f45 | 4590 | calculate_imbalance(env, &sds); |
1e3c88bd PZ |
4591 | return sds.busiest; |
4592 | ||
4593 | out_balanced: | |
1e3c88bd | 4594 | ret: |
bd939f45 | 4595 | env->imbalance = 0; |
1e3c88bd PZ |
4596 | return NULL; |
4597 | } | |
4598 | ||
4599 | /* | |
4600 | * find_busiest_queue - find the busiest runqueue among the cpus in group. | |
4601 | */ | |
bd939f45 | 4602 | static struct rq *find_busiest_queue(struct lb_env *env, |
b9403130 | 4603 | struct sched_group *group) |
1e3c88bd PZ |
4604 | { |
4605 | struct rq *busiest = NULL, *rq; | |
4606 | unsigned long max_load = 0; | |
4607 | int i; | |
4608 | ||
4609 | for_each_cpu(i, sched_group_cpus(group)) { | |
4610 | unsigned long power = power_of(i); | |
1399fa78 NR |
4611 | unsigned long capacity = DIV_ROUND_CLOSEST(power, |
4612 | SCHED_POWER_SCALE); | |
1e3c88bd PZ |
4613 | unsigned long wl; |
4614 | ||
9d5efe05 | 4615 | if (!capacity) |
bd939f45 | 4616 | capacity = fix_small_capacity(env->sd, group); |
9d5efe05 | 4617 | |
b9403130 | 4618 | if (!cpumask_test_cpu(i, env->cpus)) |
1e3c88bd PZ |
4619 | continue; |
4620 | ||
4621 | rq = cpu_rq(i); | |
6e40f5bb | 4622 | wl = weighted_cpuload(i); |
1e3c88bd | 4623 | |
6e40f5bb TG |
4624 | /* |
4625 | * When comparing with imbalance, use weighted_cpuload() | |
4626 | * which is not scaled with the cpu power. | |
4627 | */ | |
bd939f45 | 4628 | if (capacity && rq->nr_running == 1 && wl > env->imbalance) |
1e3c88bd PZ |
4629 | continue; |
4630 | ||
6e40f5bb TG |
4631 | /* |
4632 | * For the load comparisons with the other cpu's, consider | |
4633 | * the weighted_cpuload() scaled with the cpu power, so that | |
4634 | * the load can be moved away from the cpu that is potentially | |
4635 | * running at a lower capacity. | |
4636 | */ | |
1399fa78 | 4637 | wl = (wl * SCHED_POWER_SCALE) / power; |
6e40f5bb | 4638 | |
1e3c88bd PZ |
4639 | if (wl > max_load) { |
4640 | max_load = wl; | |
4641 | busiest = rq; | |
4642 | } | |
4643 | } | |
4644 | ||
4645 | return busiest; | |
4646 | } | |
4647 | ||
4648 | /* | |
4649 | * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but | |
4650 | * so long as it is large enough. | |
4651 | */ | |
4652 | #define MAX_PINNED_INTERVAL 512 | |
4653 | ||
4654 | /* Working cpumask for load_balance and load_balance_newidle. */ | |
029632fb | 4655 | DEFINE_PER_CPU(cpumask_var_t, load_balance_tmpmask); |
1e3c88bd | 4656 | |
bd939f45 | 4657 | static int need_active_balance(struct lb_env *env) |
1af3ed3d | 4658 | { |
bd939f45 PZ |
4659 | struct sched_domain *sd = env->sd; |
4660 | ||
4661 | if (env->idle == CPU_NEWLY_IDLE) { | |
532cb4c4 MN |
4662 | |
4663 | /* | |
4664 | * ASYM_PACKING needs to force migrate tasks from busy but | |
4665 | * higher numbered CPUs in order to pack all tasks in the | |
4666 | * lowest numbered CPUs. | |
4667 | */ | |
bd939f45 | 4668 | if ((sd->flags & SD_ASYM_PACKING) && env->src_cpu > env->dst_cpu) |
532cb4c4 | 4669 | return 1; |
1af3ed3d PZ |
4670 | } |
4671 | ||
4672 | return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2); | |
4673 | } | |
4674 | ||
969c7921 TH |
4675 | static int active_load_balance_cpu_stop(void *data); |
4676 | ||
1e3c88bd PZ |
4677 | /* |
4678 | * Check this_cpu to ensure it is balanced within domain. Attempt to move | |
4679 | * tasks if there is an imbalance. | |
4680 | */ | |
4681 | static int load_balance(int this_cpu, struct rq *this_rq, | |
4682 | struct sched_domain *sd, enum cpu_idle_type idle, | |
4683 | int *balance) | |
4684 | { | |
88b8dac0 SV |
4685 | int ld_moved, cur_ld_moved, active_balance = 0; |
4686 | int lb_iterations, max_lb_iterations; | |
1e3c88bd | 4687 | struct sched_group *group; |
1e3c88bd PZ |
4688 | struct rq *busiest; |
4689 | unsigned long flags; | |
4690 | struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask); | |
4691 | ||
8e45cb54 PZ |
4692 | struct lb_env env = { |
4693 | .sd = sd, | |
ddcdf6e7 PZ |
4694 | .dst_cpu = this_cpu, |
4695 | .dst_rq = this_rq, | |
88b8dac0 | 4696 | .dst_grpmask = sched_group_cpus(sd->groups), |
8e45cb54 | 4697 | .idle = idle, |
eb95308e | 4698 | .loop_break = sched_nr_migrate_break, |
b9403130 | 4699 | .cpus = cpus, |
8e45cb54 PZ |
4700 | }; |
4701 | ||
1e3c88bd | 4702 | cpumask_copy(cpus, cpu_active_mask); |
88b8dac0 | 4703 | max_lb_iterations = cpumask_weight(env.dst_grpmask); |
1e3c88bd | 4704 | |
1e3c88bd PZ |
4705 | schedstat_inc(sd, lb_count[idle]); |
4706 | ||
4707 | redo: | |
b9403130 | 4708 | group = find_busiest_group(&env, balance); |
1e3c88bd PZ |
4709 | |
4710 | if (*balance == 0) | |
4711 | goto out_balanced; | |
4712 | ||
4713 | if (!group) { | |
4714 | schedstat_inc(sd, lb_nobusyg[idle]); | |
4715 | goto out_balanced; | |
4716 | } | |
4717 | ||
b9403130 | 4718 | busiest = find_busiest_queue(&env, group); |
1e3c88bd PZ |
4719 | if (!busiest) { |
4720 | schedstat_inc(sd, lb_nobusyq[idle]); | |
4721 | goto out_balanced; | |
4722 | } | |
4723 | ||
78feefc5 | 4724 | BUG_ON(busiest == env.dst_rq); |
1e3c88bd | 4725 | |
bd939f45 | 4726 | schedstat_add(sd, lb_imbalance[idle], env.imbalance); |
1e3c88bd PZ |
4727 | |
4728 | ld_moved = 0; | |
88b8dac0 | 4729 | lb_iterations = 1; |
1e3c88bd PZ |
4730 | if (busiest->nr_running > 1) { |
4731 | /* | |
4732 | * Attempt to move tasks. If find_busiest_group has found | |
4733 | * an imbalance but busiest->nr_running <= 1, the group is | |
4734 | * still unbalanced. ld_moved simply stays zero, so it is | |
4735 | * correctly treated as an imbalance. | |
4736 | */ | |
8e45cb54 | 4737 | env.flags |= LBF_ALL_PINNED; |
c82513e5 PZ |
4738 | env.src_cpu = busiest->cpu; |
4739 | env.src_rq = busiest; | |
4740 | env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running); | |
8e45cb54 | 4741 | |
a35b6466 | 4742 | update_h_load(env.src_cpu); |
5d6523eb | 4743 | more_balance: |
1e3c88bd | 4744 | local_irq_save(flags); |
78feefc5 | 4745 | double_rq_lock(env.dst_rq, busiest); |
88b8dac0 SV |
4746 | |
4747 | /* | |
4748 | * cur_ld_moved - load moved in current iteration | |
4749 | * ld_moved - cumulative load moved across iterations | |
4750 | */ | |
4751 | cur_ld_moved = move_tasks(&env); | |
4752 | ld_moved += cur_ld_moved; | |
78feefc5 | 4753 | double_rq_unlock(env.dst_rq, busiest); |
1e3c88bd PZ |
4754 | local_irq_restore(flags); |
4755 | ||
5d6523eb PZ |
4756 | if (env.flags & LBF_NEED_BREAK) { |
4757 | env.flags &= ~LBF_NEED_BREAK; | |
4758 | goto more_balance; | |
4759 | } | |
4760 | ||
1e3c88bd PZ |
4761 | /* |
4762 | * some other cpu did the load balance for us. | |
4763 | */ | |
88b8dac0 SV |
4764 | if (cur_ld_moved && env.dst_cpu != smp_processor_id()) |
4765 | resched_cpu(env.dst_cpu); | |
4766 | ||
4767 | /* | |
4768 | * Revisit (affine) tasks on src_cpu that couldn't be moved to | |
4769 | * us and move them to an alternate dst_cpu in our sched_group | |
4770 | * where they can run. The upper limit on how many times we | |
4771 | * iterate on same src_cpu is dependent on number of cpus in our | |
4772 | * sched_group. | |
4773 | * | |
4774 | * This changes load balance semantics a bit on who can move | |
4775 | * load to a given_cpu. In addition to the given_cpu itself | |
4776 | * (or a ilb_cpu acting on its behalf where given_cpu is | |
4777 | * nohz-idle), we now have balance_cpu in a position to move | |
4778 | * load to given_cpu. In rare situations, this may cause | |
4779 | * conflicts (balance_cpu and given_cpu/ilb_cpu deciding | |
4780 | * _independently_ and at _same_ time to move some load to | |
4781 | * given_cpu) causing exceess load to be moved to given_cpu. | |
4782 | * This however should not happen so much in practice and | |
4783 | * moreover subsequent load balance cycles should correct the | |
4784 | * excess load moved. | |
4785 | */ | |
4786 | if ((env.flags & LBF_SOME_PINNED) && env.imbalance > 0 && | |
4787 | lb_iterations++ < max_lb_iterations) { | |
4788 | ||
78feefc5 | 4789 | env.dst_rq = cpu_rq(env.new_dst_cpu); |
88b8dac0 SV |
4790 | env.dst_cpu = env.new_dst_cpu; |
4791 | env.flags &= ~LBF_SOME_PINNED; | |
4792 | env.loop = 0; | |
4793 | env.loop_break = sched_nr_migrate_break; | |
4794 | /* | |
4795 | * Go back to "more_balance" rather than "redo" since we | |
4796 | * need to continue with same src_cpu. | |
4797 | */ | |
4798 | goto more_balance; | |
4799 | } | |
1e3c88bd PZ |
4800 | |
4801 | /* All tasks on this runqueue were pinned by CPU affinity */ | |
8e45cb54 | 4802 | if (unlikely(env.flags & LBF_ALL_PINNED)) { |
1e3c88bd | 4803 | cpumask_clear_cpu(cpu_of(busiest), cpus); |
bbf18b19 PN |
4804 | if (!cpumask_empty(cpus)) { |
4805 | env.loop = 0; | |
4806 | env.loop_break = sched_nr_migrate_break; | |
1e3c88bd | 4807 | goto redo; |
bbf18b19 | 4808 | } |
1e3c88bd PZ |
4809 | goto out_balanced; |
4810 | } | |
4811 | } | |
4812 | ||
4813 | if (!ld_moved) { | |
4814 | schedstat_inc(sd, lb_failed[idle]); | |
58b26c4c VP |
4815 | /* |
4816 | * Increment the failure counter only on periodic balance. | |
4817 | * We do not want newidle balance, which can be very | |
4818 | * frequent, pollute the failure counter causing | |
4819 | * excessive cache_hot migrations and active balances. | |
4820 | */ | |
4821 | if (idle != CPU_NEWLY_IDLE) | |
4822 | sd->nr_balance_failed++; | |
1e3c88bd | 4823 | |
bd939f45 | 4824 | if (need_active_balance(&env)) { |
1e3c88bd PZ |
4825 | raw_spin_lock_irqsave(&busiest->lock, flags); |
4826 | ||
969c7921 TH |
4827 | /* don't kick the active_load_balance_cpu_stop, |
4828 | * if the curr task on busiest cpu can't be | |
4829 | * moved to this_cpu | |
1e3c88bd PZ |
4830 | */ |
4831 | if (!cpumask_test_cpu(this_cpu, | |
fa17b507 | 4832 | tsk_cpus_allowed(busiest->curr))) { |
1e3c88bd PZ |
4833 | raw_spin_unlock_irqrestore(&busiest->lock, |
4834 | flags); | |
8e45cb54 | 4835 | env.flags |= LBF_ALL_PINNED; |
1e3c88bd PZ |
4836 | goto out_one_pinned; |
4837 | } | |
4838 | ||
969c7921 TH |
4839 | /* |
4840 | * ->active_balance synchronizes accesses to | |
4841 | * ->active_balance_work. Once set, it's cleared | |
4842 | * only after active load balance is finished. | |
4843 | */ | |
1e3c88bd PZ |
4844 | if (!busiest->active_balance) { |
4845 | busiest->active_balance = 1; | |
4846 | busiest->push_cpu = this_cpu; | |
4847 | active_balance = 1; | |
4848 | } | |
4849 | raw_spin_unlock_irqrestore(&busiest->lock, flags); | |
969c7921 | 4850 | |
bd939f45 | 4851 | if (active_balance) { |
969c7921 TH |
4852 | stop_one_cpu_nowait(cpu_of(busiest), |
4853 | active_load_balance_cpu_stop, busiest, | |
4854 | &busiest->active_balance_work); | |
bd939f45 | 4855 | } |
1e3c88bd PZ |
4856 | |
4857 | /* | |
4858 | * We've kicked active balancing, reset the failure | |
4859 | * counter. | |
4860 | */ | |
4861 | sd->nr_balance_failed = sd->cache_nice_tries+1; | |
4862 | } | |
4863 | } else | |
4864 | sd->nr_balance_failed = 0; | |
4865 | ||
4866 | if (likely(!active_balance)) { | |
4867 | /* We were unbalanced, so reset the balancing interval */ | |
4868 | sd->balance_interval = sd->min_interval; | |
4869 | } else { | |
4870 | /* | |
4871 | * If we've begun active balancing, start to back off. This | |
4872 | * case may not be covered by the all_pinned logic if there | |
4873 | * is only 1 task on the busy runqueue (because we don't call | |
4874 | * move_tasks). | |
4875 | */ | |
4876 | if (sd->balance_interval < sd->max_interval) | |
4877 | sd->balance_interval *= 2; | |
4878 | } | |
4879 | ||
1e3c88bd PZ |
4880 | goto out; |
4881 | ||
4882 | out_balanced: | |
4883 | schedstat_inc(sd, lb_balanced[idle]); | |
4884 | ||
4885 | sd->nr_balance_failed = 0; | |
4886 | ||
4887 | out_one_pinned: | |
4888 | /* tune up the balancing interval */ | |
8e45cb54 | 4889 | if (((env.flags & LBF_ALL_PINNED) && |
5b54b56b | 4890 | sd->balance_interval < MAX_PINNED_INTERVAL) || |
1e3c88bd PZ |
4891 | (sd->balance_interval < sd->max_interval)) |
4892 | sd->balance_interval *= 2; | |
4893 | ||
46e49b38 | 4894 | ld_moved = 0; |
1e3c88bd | 4895 | out: |
1e3c88bd PZ |
4896 | return ld_moved; |
4897 | } | |
4898 | ||
1e3c88bd PZ |
4899 | /* |
4900 | * idle_balance is called by schedule() if this_cpu is about to become | |
4901 | * idle. Attempts to pull tasks from other CPUs. | |
4902 | */ | |
029632fb | 4903 | void idle_balance(int this_cpu, struct rq *this_rq) |
1e3c88bd PZ |
4904 | { |
4905 | struct sched_domain *sd; | |
4906 | int pulled_task = 0; | |
4907 | unsigned long next_balance = jiffies + HZ; | |
4908 | ||
4909 | this_rq->idle_stamp = this_rq->clock; | |
4910 | ||
4911 | if (this_rq->avg_idle < sysctl_sched_migration_cost) | |
4912 | return; | |
4913 | ||
18bf2805 BS |
4914 | update_rq_runnable_avg(this_rq, 1); |
4915 | ||
f492e12e PZ |
4916 | /* |
4917 | * Drop the rq->lock, but keep IRQ/preempt disabled. | |
4918 | */ | |
4919 | raw_spin_unlock(&this_rq->lock); | |
4920 | ||
c66eaf61 | 4921 | update_shares(this_cpu); |
dce840a0 | 4922 | rcu_read_lock(); |
1e3c88bd PZ |
4923 | for_each_domain(this_cpu, sd) { |
4924 | unsigned long interval; | |
f492e12e | 4925 | int balance = 1; |
1e3c88bd PZ |
4926 | |
4927 | if (!(sd->flags & SD_LOAD_BALANCE)) | |
4928 | continue; | |
4929 | ||
f492e12e | 4930 | if (sd->flags & SD_BALANCE_NEWIDLE) { |
1e3c88bd | 4931 | /* If we've pulled tasks over stop searching: */ |
f492e12e PZ |
4932 | pulled_task = load_balance(this_cpu, this_rq, |
4933 | sd, CPU_NEWLY_IDLE, &balance); | |
4934 | } | |
1e3c88bd PZ |
4935 | |
4936 | interval = msecs_to_jiffies(sd->balance_interval); | |
4937 | if (time_after(next_balance, sd->last_balance + interval)) | |
4938 | next_balance = sd->last_balance + interval; | |
d5ad140b NR |
4939 | if (pulled_task) { |
4940 | this_rq->idle_stamp = 0; | |
1e3c88bd | 4941 | break; |
d5ad140b | 4942 | } |
1e3c88bd | 4943 | } |
dce840a0 | 4944 | rcu_read_unlock(); |
f492e12e PZ |
4945 | |
4946 | raw_spin_lock(&this_rq->lock); | |
4947 | ||
1e3c88bd PZ |
4948 | if (pulled_task || time_after(jiffies, this_rq->next_balance)) { |
4949 | /* | |
4950 | * We are going idle. next_balance may be set based on | |
4951 | * a busy processor. So reset next_balance. | |
4952 | */ | |
4953 | this_rq->next_balance = next_balance; | |
4954 | } | |
4955 | } | |
4956 | ||
4957 | /* | |
969c7921 TH |
4958 | * active_load_balance_cpu_stop is run by cpu stopper. It pushes |
4959 | * running tasks off the busiest CPU onto idle CPUs. It requires at | |
4960 | * least 1 task to be running on each physical CPU where possible, and | |
4961 | * avoids physical / logical imbalances. | |
1e3c88bd | 4962 | */ |
969c7921 | 4963 | static int active_load_balance_cpu_stop(void *data) |
1e3c88bd | 4964 | { |
969c7921 TH |
4965 | struct rq *busiest_rq = data; |
4966 | int busiest_cpu = cpu_of(busiest_rq); | |
1e3c88bd | 4967 | int target_cpu = busiest_rq->push_cpu; |
969c7921 | 4968 | struct rq *target_rq = cpu_rq(target_cpu); |
1e3c88bd | 4969 | struct sched_domain *sd; |
969c7921 TH |
4970 | |
4971 | raw_spin_lock_irq(&busiest_rq->lock); | |
4972 | ||
4973 | /* make sure the requested cpu hasn't gone down in the meantime */ | |
4974 | if (unlikely(busiest_cpu != smp_processor_id() || | |
4975 | !busiest_rq->active_balance)) | |
4976 | goto out_unlock; | |
1e3c88bd PZ |
4977 | |
4978 | /* Is there any task to move? */ | |
4979 | if (busiest_rq->nr_running <= 1) | |
969c7921 | 4980 | goto out_unlock; |
1e3c88bd PZ |
4981 | |
4982 | /* | |
4983 | * This condition is "impossible", if it occurs | |
4984 | * we need to fix it. Originally reported by | |
4985 | * Bjorn Helgaas on a 128-cpu setup. | |
4986 | */ | |
4987 | BUG_ON(busiest_rq == target_rq); | |
4988 | ||
4989 | /* move a task from busiest_rq to target_rq */ | |
4990 | double_lock_balance(busiest_rq, target_rq); | |
1e3c88bd PZ |
4991 | |
4992 | /* Search for an sd spanning us and the target CPU. */ | |
dce840a0 | 4993 | rcu_read_lock(); |
1e3c88bd PZ |
4994 | for_each_domain(target_cpu, sd) { |
4995 | if ((sd->flags & SD_LOAD_BALANCE) && | |
4996 | cpumask_test_cpu(busiest_cpu, sched_domain_span(sd))) | |
4997 | break; | |
4998 | } | |
4999 | ||
5000 | if (likely(sd)) { | |
8e45cb54 PZ |
5001 | struct lb_env env = { |
5002 | .sd = sd, | |
ddcdf6e7 PZ |
5003 | .dst_cpu = target_cpu, |
5004 | .dst_rq = target_rq, | |
5005 | .src_cpu = busiest_rq->cpu, | |
5006 | .src_rq = busiest_rq, | |
8e45cb54 PZ |
5007 | .idle = CPU_IDLE, |
5008 | }; | |
5009 | ||
1e3c88bd PZ |
5010 | schedstat_inc(sd, alb_count); |
5011 | ||
8e45cb54 | 5012 | if (move_one_task(&env)) |
1e3c88bd PZ |
5013 | schedstat_inc(sd, alb_pushed); |
5014 | else | |
5015 | schedstat_inc(sd, alb_failed); | |
5016 | } | |
dce840a0 | 5017 | rcu_read_unlock(); |
1e3c88bd | 5018 | double_unlock_balance(busiest_rq, target_rq); |
969c7921 TH |
5019 | out_unlock: |
5020 | busiest_rq->active_balance = 0; | |
5021 | raw_spin_unlock_irq(&busiest_rq->lock); | |
5022 | return 0; | |
1e3c88bd PZ |
5023 | } |
5024 | ||
5025 | #ifdef CONFIG_NO_HZ | |
83cd4fe2 VP |
5026 | /* |
5027 | * idle load balancing details | |
83cd4fe2 VP |
5028 | * - When one of the busy CPUs notice that there may be an idle rebalancing |
5029 | * needed, they will kick the idle load balancer, which then does idle | |
5030 | * load balancing for all the idle CPUs. | |
5031 | */ | |
1e3c88bd | 5032 | static struct { |
83cd4fe2 | 5033 | cpumask_var_t idle_cpus_mask; |
0b005cf5 | 5034 | atomic_t nr_cpus; |
83cd4fe2 VP |
5035 | unsigned long next_balance; /* in jiffy units */ |
5036 | } nohz ____cacheline_aligned; | |
1e3c88bd | 5037 | |
8e7fbcbc | 5038 | static inline int find_new_ilb(int call_cpu) |
1e3c88bd | 5039 | { |
0b005cf5 | 5040 | int ilb = cpumask_first(nohz.idle_cpus_mask); |
1e3c88bd | 5041 | |
786d6dc7 SS |
5042 | if (ilb < nr_cpu_ids && idle_cpu(ilb)) |
5043 | return ilb; | |
5044 | ||
5045 | return nr_cpu_ids; | |
1e3c88bd | 5046 | } |
1e3c88bd | 5047 | |
83cd4fe2 VP |
5048 | /* |
5049 | * Kick a CPU to do the nohz balancing, if it is time for it. We pick the | |
5050 | * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle | |
5051 | * CPU (if there is one). | |
5052 | */ | |
5053 | static void nohz_balancer_kick(int cpu) | |
5054 | { | |
5055 | int ilb_cpu; | |
5056 | ||
5057 | nohz.next_balance++; | |
5058 | ||
0b005cf5 | 5059 | ilb_cpu = find_new_ilb(cpu); |
83cd4fe2 | 5060 | |
0b005cf5 SS |
5061 | if (ilb_cpu >= nr_cpu_ids) |
5062 | return; | |
83cd4fe2 | 5063 | |
cd490c5b | 5064 | if (test_and_set_bit(NOHZ_BALANCE_KICK, nohz_flags(ilb_cpu))) |
1c792db7 SS |
5065 | return; |
5066 | /* | |
5067 | * Use smp_send_reschedule() instead of resched_cpu(). | |
5068 | * This way we generate a sched IPI on the target cpu which | |
5069 | * is idle. And the softirq performing nohz idle load balance | |
5070 | * will be run before returning from the IPI. | |
5071 | */ | |
5072 | smp_send_reschedule(ilb_cpu); | |
83cd4fe2 VP |
5073 | return; |
5074 | } | |
5075 | ||
c1cc017c | 5076 | static inline void nohz_balance_exit_idle(int cpu) |
71325960 SS |
5077 | { |
5078 | if (unlikely(test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))) { | |
5079 | cpumask_clear_cpu(cpu, nohz.idle_cpus_mask); | |
5080 | atomic_dec(&nohz.nr_cpus); | |
5081 | clear_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)); | |
5082 | } | |
5083 | } | |
5084 | ||
69e1e811 SS |
5085 | static inline void set_cpu_sd_state_busy(void) |
5086 | { | |
5087 | struct sched_domain *sd; | |
5088 | int cpu = smp_processor_id(); | |
5089 | ||
5090 | if (!test_bit(NOHZ_IDLE, nohz_flags(cpu))) | |
5091 | return; | |
5092 | clear_bit(NOHZ_IDLE, nohz_flags(cpu)); | |
5093 | ||
5094 | rcu_read_lock(); | |
5095 | for_each_domain(cpu, sd) | |
5096 | atomic_inc(&sd->groups->sgp->nr_busy_cpus); | |
5097 | rcu_read_unlock(); | |
5098 | } | |
5099 | ||
5100 | void set_cpu_sd_state_idle(void) | |
5101 | { | |
5102 | struct sched_domain *sd; | |
5103 | int cpu = smp_processor_id(); | |
5104 | ||
5105 | if (test_bit(NOHZ_IDLE, nohz_flags(cpu))) | |
5106 | return; | |
5107 | set_bit(NOHZ_IDLE, nohz_flags(cpu)); | |
5108 | ||
5109 | rcu_read_lock(); | |
5110 | for_each_domain(cpu, sd) | |
5111 | atomic_dec(&sd->groups->sgp->nr_busy_cpus); | |
5112 | rcu_read_unlock(); | |
5113 | } | |
5114 | ||
1e3c88bd | 5115 | /* |
c1cc017c | 5116 | * This routine will record that the cpu is going idle with tick stopped. |
0b005cf5 | 5117 | * This info will be used in performing idle load balancing in the future. |
1e3c88bd | 5118 | */ |
c1cc017c | 5119 | void nohz_balance_enter_idle(int cpu) |
1e3c88bd | 5120 | { |
71325960 SS |
5121 | /* |
5122 | * If this cpu is going down, then nothing needs to be done. | |
5123 | */ | |
5124 | if (!cpu_active(cpu)) | |
5125 | return; | |
5126 | ||
c1cc017c AS |
5127 | if (test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu))) |
5128 | return; | |
1e3c88bd | 5129 | |
c1cc017c AS |
5130 | cpumask_set_cpu(cpu, nohz.idle_cpus_mask); |
5131 | atomic_inc(&nohz.nr_cpus); | |
5132 | set_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)); | |
1e3c88bd | 5133 | } |
71325960 SS |
5134 | |
5135 | static int __cpuinit sched_ilb_notifier(struct notifier_block *nfb, | |
5136 | unsigned long action, void *hcpu) | |
5137 | { | |
5138 | switch (action & ~CPU_TASKS_FROZEN) { | |
5139 | case CPU_DYING: | |
c1cc017c | 5140 | nohz_balance_exit_idle(smp_processor_id()); |
71325960 SS |
5141 | return NOTIFY_OK; |
5142 | default: | |
5143 | return NOTIFY_DONE; | |
5144 | } | |
5145 | } | |
1e3c88bd PZ |
5146 | #endif |
5147 | ||
5148 | static DEFINE_SPINLOCK(balancing); | |
5149 | ||
49c022e6 PZ |
5150 | /* |
5151 | * Scale the max load_balance interval with the number of CPUs in the system. | |
5152 | * This trades load-balance latency on larger machines for less cross talk. | |
5153 | */ | |
029632fb | 5154 | void update_max_interval(void) |
49c022e6 PZ |
5155 | { |
5156 | max_load_balance_interval = HZ*num_online_cpus()/10; | |
5157 | } | |
5158 | ||
1e3c88bd PZ |
5159 | /* |
5160 | * It checks each scheduling domain to see if it is due to be balanced, | |
5161 | * and initiates a balancing operation if so. | |
5162 | * | |
5163 | * Balancing parameters are set up in arch_init_sched_domains. | |
5164 | */ | |
5165 | static void rebalance_domains(int cpu, enum cpu_idle_type idle) | |
5166 | { | |
5167 | int balance = 1; | |
5168 | struct rq *rq = cpu_rq(cpu); | |
5169 | unsigned long interval; | |
04f733b4 | 5170 | struct sched_domain *sd; |
1e3c88bd PZ |
5171 | /* Earliest time when we have to do rebalance again */ |
5172 | unsigned long next_balance = jiffies + 60*HZ; | |
5173 | int update_next_balance = 0; | |
5174 | int need_serialize; | |
5175 | ||
2069dd75 PZ |
5176 | update_shares(cpu); |
5177 | ||
dce840a0 | 5178 | rcu_read_lock(); |
1e3c88bd PZ |
5179 | for_each_domain(cpu, sd) { |
5180 | if (!(sd->flags & SD_LOAD_BALANCE)) | |
5181 | continue; | |
5182 | ||
5183 | interval = sd->balance_interval; | |
5184 | if (idle != CPU_IDLE) | |
5185 | interval *= sd->busy_factor; | |
5186 | ||
5187 | /* scale ms to jiffies */ | |
5188 | interval = msecs_to_jiffies(interval); | |
49c022e6 | 5189 | interval = clamp(interval, 1UL, max_load_balance_interval); |
1e3c88bd PZ |
5190 | |
5191 | need_serialize = sd->flags & SD_SERIALIZE; | |
5192 | ||
5193 | if (need_serialize) { | |
5194 | if (!spin_trylock(&balancing)) | |
5195 | goto out; | |
5196 | } | |
5197 | ||
5198 | if (time_after_eq(jiffies, sd->last_balance + interval)) { | |
5199 | if (load_balance(cpu, rq, sd, idle, &balance)) { | |
5200 | /* | |
5201 | * We've pulled tasks over so either we're no | |
c186fafe | 5202 | * longer idle. |
1e3c88bd PZ |
5203 | */ |
5204 | idle = CPU_NOT_IDLE; | |
5205 | } | |
5206 | sd->last_balance = jiffies; | |
5207 | } | |
5208 | if (need_serialize) | |
5209 | spin_unlock(&balancing); | |
5210 | out: | |
5211 | if (time_after(next_balance, sd->last_balance + interval)) { | |
5212 | next_balance = sd->last_balance + interval; | |
5213 | update_next_balance = 1; | |
5214 | } | |
5215 | ||
5216 | /* | |
5217 | * Stop the load balance at this level. There is another | |
5218 | * CPU in our sched group which is doing load balancing more | |
5219 | * actively. | |
5220 | */ | |
5221 | if (!balance) | |
5222 | break; | |
5223 | } | |
dce840a0 | 5224 | rcu_read_unlock(); |
1e3c88bd PZ |
5225 | |
5226 | /* | |
5227 | * next_balance will be updated only when there is a need. | |
5228 | * When the cpu is attached to null domain for ex, it will not be | |
5229 | * updated. | |
5230 | */ | |
5231 | if (likely(update_next_balance)) | |
5232 | rq->next_balance = next_balance; | |
5233 | } | |
5234 | ||
83cd4fe2 | 5235 | #ifdef CONFIG_NO_HZ |
1e3c88bd | 5236 | /* |
83cd4fe2 | 5237 | * In CONFIG_NO_HZ case, the idle balance kickee will do the |
1e3c88bd PZ |
5238 | * rebalancing for all the cpus for whom scheduler ticks are stopped. |
5239 | */ | |
83cd4fe2 VP |
5240 | static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle) |
5241 | { | |
5242 | struct rq *this_rq = cpu_rq(this_cpu); | |
5243 | struct rq *rq; | |
5244 | int balance_cpu; | |
5245 | ||
1c792db7 SS |
5246 | if (idle != CPU_IDLE || |
5247 | !test_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu))) | |
5248 | goto end; | |
83cd4fe2 VP |
5249 | |
5250 | for_each_cpu(balance_cpu, nohz.idle_cpus_mask) { | |
8a6d42d1 | 5251 | if (balance_cpu == this_cpu || !idle_cpu(balance_cpu)) |
83cd4fe2 VP |
5252 | continue; |
5253 | ||
5254 | /* | |
5255 | * If this cpu gets work to do, stop the load balancing | |
5256 | * work being done for other cpus. Next load | |
5257 | * balancing owner will pick it up. | |
5258 | */ | |
1c792db7 | 5259 | if (need_resched()) |
83cd4fe2 | 5260 | break; |
83cd4fe2 | 5261 | |
5ed4f1d9 VG |
5262 | rq = cpu_rq(balance_cpu); |
5263 | ||
5264 | raw_spin_lock_irq(&rq->lock); | |
5265 | update_rq_clock(rq); | |
5266 | update_idle_cpu_load(rq); | |
5267 | raw_spin_unlock_irq(&rq->lock); | |
83cd4fe2 VP |
5268 | |
5269 | rebalance_domains(balance_cpu, CPU_IDLE); | |
5270 | ||
83cd4fe2 VP |
5271 | if (time_after(this_rq->next_balance, rq->next_balance)) |
5272 | this_rq->next_balance = rq->next_balance; | |
5273 | } | |
5274 | nohz.next_balance = this_rq->next_balance; | |
1c792db7 SS |
5275 | end: |
5276 | clear_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu)); | |
83cd4fe2 VP |
5277 | } |
5278 | ||
5279 | /* | |
0b005cf5 SS |
5280 | * Current heuristic for kicking the idle load balancer in the presence |
5281 | * of an idle cpu is the system. | |
5282 | * - This rq has more than one task. | |
5283 | * - At any scheduler domain level, this cpu's scheduler group has multiple | |
5284 | * busy cpu's exceeding the group's power. | |
5285 | * - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler | |
5286 | * domain span are idle. | |
83cd4fe2 VP |
5287 | */ |
5288 | static inline int nohz_kick_needed(struct rq *rq, int cpu) | |
5289 | { | |
5290 | unsigned long now = jiffies; | |
0b005cf5 | 5291 | struct sched_domain *sd; |
83cd4fe2 | 5292 | |
1c792db7 | 5293 | if (unlikely(idle_cpu(cpu))) |
83cd4fe2 VP |
5294 | return 0; |
5295 | ||
1c792db7 SS |
5296 | /* |
5297 | * We may be recently in ticked or tickless idle mode. At the first | |
5298 | * busy tick after returning from idle, we will update the busy stats. | |
5299 | */ | |
69e1e811 | 5300 | set_cpu_sd_state_busy(); |
c1cc017c | 5301 | nohz_balance_exit_idle(cpu); |
0b005cf5 SS |
5302 | |
5303 | /* | |
5304 | * None are in tickless mode and hence no need for NOHZ idle load | |
5305 | * balancing. | |
5306 | */ | |
5307 | if (likely(!atomic_read(&nohz.nr_cpus))) | |
5308 | return 0; | |
1c792db7 SS |
5309 | |
5310 | if (time_before(now, nohz.next_balance)) | |
83cd4fe2 VP |
5311 | return 0; |
5312 | ||
0b005cf5 SS |
5313 | if (rq->nr_running >= 2) |
5314 | goto need_kick; | |
83cd4fe2 | 5315 | |
067491b7 | 5316 | rcu_read_lock(); |
0b005cf5 SS |
5317 | for_each_domain(cpu, sd) { |
5318 | struct sched_group *sg = sd->groups; | |
5319 | struct sched_group_power *sgp = sg->sgp; | |
5320 | int nr_busy = atomic_read(&sgp->nr_busy_cpus); | |
83cd4fe2 | 5321 | |
0b005cf5 | 5322 | if (sd->flags & SD_SHARE_PKG_RESOURCES && nr_busy > 1) |
067491b7 | 5323 | goto need_kick_unlock; |
0b005cf5 SS |
5324 | |
5325 | if (sd->flags & SD_ASYM_PACKING && nr_busy != sg->group_weight | |
5326 | && (cpumask_first_and(nohz.idle_cpus_mask, | |
5327 | sched_domain_span(sd)) < cpu)) | |
067491b7 | 5328 | goto need_kick_unlock; |
0b005cf5 SS |
5329 | |
5330 | if (!(sd->flags & (SD_SHARE_PKG_RESOURCES | SD_ASYM_PACKING))) | |
5331 | break; | |
83cd4fe2 | 5332 | } |
067491b7 | 5333 | rcu_read_unlock(); |
83cd4fe2 | 5334 | return 0; |
067491b7 PZ |
5335 | |
5336 | need_kick_unlock: | |
5337 | rcu_read_unlock(); | |
0b005cf5 SS |
5338 | need_kick: |
5339 | return 1; | |
83cd4fe2 VP |
5340 | } |
5341 | #else | |
5342 | static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle) { } | |
5343 | #endif | |
5344 | ||
5345 | /* | |
5346 | * run_rebalance_domains is triggered when needed from the scheduler tick. | |
5347 | * Also triggered for nohz idle balancing (with nohz_balancing_kick set). | |
5348 | */ | |
1e3c88bd PZ |
5349 | static void run_rebalance_domains(struct softirq_action *h) |
5350 | { | |
5351 | int this_cpu = smp_processor_id(); | |
5352 | struct rq *this_rq = cpu_rq(this_cpu); | |
6eb57e0d | 5353 | enum cpu_idle_type idle = this_rq->idle_balance ? |
1e3c88bd PZ |
5354 | CPU_IDLE : CPU_NOT_IDLE; |
5355 | ||
5356 | rebalance_domains(this_cpu, idle); | |
5357 | ||
1e3c88bd | 5358 | /* |
83cd4fe2 | 5359 | * If this cpu has a pending nohz_balance_kick, then do the |
1e3c88bd PZ |
5360 | * balancing on behalf of the other idle cpus whose ticks are |
5361 | * stopped. | |
5362 | */ | |
83cd4fe2 | 5363 | nohz_idle_balance(this_cpu, idle); |
1e3c88bd PZ |
5364 | } |
5365 | ||
5366 | static inline int on_null_domain(int cpu) | |
5367 | { | |
90a6501f | 5368 | return !rcu_dereference_sched(cpu_rq(cpu)->sd); |
1e3c88bd PZ |
5369 | } |
5370 | ||
5371 | /* | |
5372 | * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing. | |
1e3c88bd | 5373 | */ |
029632fb | 5374 | void trigger_load_balance(struct rq *rq, int cpu) |
1e3c88bd | 5375 | { |
1e3c88bd PZ |
5376 | /* Don't need to rebalance while attached to NULL domain */ |
5377 | if (time_after_eq(jiffies, rq->next_balance) && | |
5378 | likely(!on_null_domain(cpu))) | |
5379 | raise_softirq(SCHED_SOFTIRQ); | |
83cd4fe2 | 5380 | #ifdef CONFIG_NO_HZ |
1c792db7 | 5381 | if (nohz_kick_needed(rq, cpu) && likely(!on_null_domain(cpu))) |
83cd4fe2 VP |
5382 | nohz_balancer_kick(cpu); |
5383 | #endif | |
1e3c88bd PZ |
5384 | } |
5385 | ||
0bcdcf28 CE |
5386 | static void rq_online_fair(struct rq *rq) |
5387 | { | |
5388 | update_sysctl(); | |
5389 | } | |
5390 | ||
5391 | static void rq_offline_fair(struct rq *rq) | |
5392 | { | |
5393 | update_sysctl(); | |
a4c96ae3 PB |
5394 | |
5395 | /* Ensure any throttled groups are reachable by pick_next_task */ | |
5396 | unthrottle_offline_cfs_rqs(rq); | |
0bcdcf28 CE |
5397 | } |
5398 | ||
55e12e5e | 5399 | #endif /* CONFIG_SMP */ |
e1d1484f | 5400 | |
bf0f6f24 IM |
5401 | /* |
5402 | * scheduler tick hitting a task of our scheduling class: | |
5403 | */ | |
8f4d37ec | 5404 | static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued) |
bf0f6f24 IM |
5405 | { |
5406 | struct cfs_rq *cfs_rq; | |
5407 | struct sched_entity *se = &curr->se; | |
5408 | ||
5409 | for_each_sched_entity(se) { | |
5410 | cfs_rq = cfs_rq_of(se); | |
8f4d37ec | 5411 | entity_tick(cfs_rq, se, queued); |
bf0f6f24 | 5412 | } |
18bf2805 BS |
5413 | |
5414 | update_rq_runnable_avg(rq, 1); | |
bf0f6f24 IM |
5415 | } |
5416 | ||
5417 | /* | |
cd29fe6f PZ |
5418 | * called on fork with the child task as argument from the parent's context |
5419 | * - child not yet on the tasklist | |
5420 | * - preemption disabled | |
bf0f6f24 | 5421 | */ |
cd29fe6f | 5422 | static void task_fork_fair(struct task_struct *p) |
bf0f6f24 | 5423 | { |
4fc420c9 DN |
5424 | struct cfs_rq *cfs_rq; |
5425 | struct sched_entity *se = &p->se, *curr; | |
00bf7bfc | 5426 | int this_cpu = smp_processor_id(); |
cd29fe6f PZ |
5427 | struct rq *rq = this_rq(); |
5428 | unsigned long flags; | |
5429 | ||
05fa785c | 5430 | raw_spin_lock_irqsave(&rq->lock, flags); |
bf0f6f24 | 5431 | |
861d034e PZ |
5432 | update_rq_clock(rq); |
5433 | ||
4fc420c9 DN |
5434 | cfs_rq = task_cfs_rq(current); |
5435 | curr = cfs_rq->curr; | |
5436 | ||
b0a0f667 PM |
5437 | if (unlikely(task_cpu(p) != this_cpu)) { |
5438 | rcu_read_lock(); | |
cd29fe6f | 5439 | __set_task_cpu(p, this_cpu); |
b0a0f667 PM |
5440 | rcu_read_unlock(); |
5441 | } | |
bf0f6f24 | 5442 | |
7109c442 | 5443 | update_curr(cfs_rq); |
cd29fe6f | 5444 | |
b5d9d734 MG |
5445 | if (curr) |
5446 | se->vruntime = curr->vruntime; | |
aeb73b04 | 5447 | place_entity(cfs_rq, se, 1); |
4d78e7b6 | 5448 | |
cd29fe6f | 5449 | if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) { |
87fefa38 | 5450 | /* |
edcb60a3 IM |
5451 | * Upon rescheduling, sched_class::put_prev_task() will place |
5452 | * 'current' within the tree based on its new key value. | |
5453 | */ | |
4d78e7b6 | 5454 | swap(curr->vruntime, se->vruntime); |
aec0a514 | 5455 | resched_task(rq->curr); |
4d78e7b6 | 5456 | } |
bf0f6f24 | 5457 | |
88ec22d3 PZ |
5458 | se->vruntime -= cfs_rq->min_vruntime; |
5459 | ||
05fa785c | 5460 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
bf0f6f24 IM |
5461 | } |
5462 | ||
cb469845 SR |
5463 | /* |
5464 | * Priority of the task has changed. Check to see if we preempt | |
5465 | * the current task. | |
5466 | */ | |
da7a735e PZ |
5467 | static void |
5468 | prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio) | |
cb469845 | 5469 | { |
da7a735e PZ |
5470 | if (!p->se.on_rq) |
5471 | return; | |
5472 | ||
cb469845 SR |
5473 | /* |
5474 | * Reschedule if we are currently running on this runqueue and | |
5475 | * our priority decreased, or if we are not currently running on | |
5476 | * this runqueue and our priority is higher than the current's | |
5477 | */ | |
da7a735e | 5478 | if (rq->curr == p) { |
cb469845 SR |
5479 | if (p->prio > oldprio) |
5480 | resched_task(rq->curr); | |
5481 | } else | |
15afe09b | 5482 | check_preempt_curr(rq, p, 0); |
cb469845 SR |
5483 | } |
5484 | ||
da7a735e PZ |
5485 | static void switched_from_fair(struct rq *rq, struct task_struct *p) |
5486 | { | |
5487 | struct sched_entity *se = &p->se; | |
5488 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
5489 | ||
5490 | /* | |
5491 | * Ensure the task's vruntime is normalized, so that when its | |
5492 | * switched back to the fair class the enqueue_entity(.flags=0) will | |
5493 | * do the right thing. | |
5494 | * | |
5495 | * If it was on_rq, then the dequeue_entity(.flags=0) will already | |
5496 | * have normalized the vruntime, if it was !on_rq, then only when | |
5497 | * the task is sleeping will it still have non-normalized vruntime. | |
5498 | */ | |
5499 | if (!se->on_rq && p->state != TASK_RUNNING) { | |
5500 | /* | |
5501 | * Fix up our vruntime so that the current sleep doesn't | |
5502 | * cause 'unlimited' sleep bonus. | |
5503 | */ | |
5504 | place_entity(cfs_rq, se, 0); | |
5505 | se->vruntime -= cfs_rq->min_vruntime; | |
5506 | } | |
9ee474f5 PT |
5507 | |
5508 | #if defined(CONFIG_FAIR_GROUP_SCHED) && defined(CONFIG_SMP) | |
5509 | /* | |
5510 | * Remove our load from contribution when we leave sched_fair | |
5511 | * and ensure we don't carry in an old decay_count if we | |
5512 | * switch back. | |
5513 | */ | |
5514 | if (p->se.avg.decay_count) { | |
5515 | struct cfs_rq *cfs_rq = cfs_rq_of(&p->se); | |
5516 | __synchronize_entity_decay(&p->se); | |
5517 | subtract_blocked_load_contrib(cfs_rq, | |
5518 | p->se.avg.load_avg_contrib); | |
5519 | } | |
5520 | #endif | |
da7a735e PZ |
5521 | } |
5522 | ||
cb469845 SR |
5523 | /* |
5524 | * We switched to the sched_fair class. | |
5525 | */ | |
da7a735e | 5526 | static void switched_to_fair(struct rq *rq, struct task_struct *p) |
cb469845 | 5527 | { |
da7a735e PZ |
5528 | if (!p->se.on_rq) |
5529 | return; | |
5530 | ||
cb469845 SR |
5531 | /* |
5532 | * We were most likely switched from sched_rt, so | |
5533 | * kick off the schedule if running, otherwise just see | |
5534 | * if we can still preempt the current task. | |
5535 | */ | |
da7a735e | 5536 | if (rq->curr == p) |
cb469845 SR |
5537 | resched_task(rq->curr); |
5538 | else | |
15afe09b | 5539 | check_preempt_curr(rq, p, 0); |
cb469845 SR |
5540 | } |
5541 | ||
83b699ed SV |
5542 | /* Account for a task changing its policy or group. |
5543 | * | |
5544 | * This routine is mostly called to set cfs_rq->curr field when a task | |
5545 | * migrates between groups/classes. | |
5546 | */ | |
5547 | static void set_curr_task_fair(struct rq *rq) | |
5548 | { | |
5549 | struct sched_entity *se = &rq->curr->se; | |
5550 | ||
ec12cb7f PT |
5551 | for_each_sched_entity(se) { |
5552 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
5553 | ||
5554 | set_next_entity(cfs_rq, se); | |
5555 | /* ensure bandwidth has been allocated on our new cfs_rq */ | |
5556 | account_cfs_rq_runtime(cfs_rq, 0); | |
5557 | } | |
83b699ed SV |
5558 | } |
5559 | ||
029632fb PZ |
5560 | void init_cfs_rq(struct cfs_rq *cfs_rq) |
5561 | { | |
5562 | cfs_rq->tasks_timeline = RB_ROOT; | |
029632fb PZ |
5563 | cfs_rq->min_vruntime = (u64)(-(1LL << 20)); |
5564 | #ifndef CONFIG_64BIT | |
5565 | cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime; | |
5566 | #endif | |
9ee474f5 PT |
5567 | #if defined(CONFIG_FAIR_GROUP_SCHED) && defined(CONFIG_SMP) |
5568 | atomic64_set(&cfs_rq->decay_counter, 1); | |
aff3e498 | 5569 | atomic64_set(&cfs_rq->removed_load, 0); |
9ee474f5 | 5570 | #endif |
029632fb PZ |
5571 | } |
5572 | ||
810b3817 | 5573 | #ifdef CONFIG_FAIR_GROUP_SCHED |
b2b5ce02 | 5574 | static void task_move_group_fair(struct task_struct *p, int on_rq) |
810b3817 | 5575 | { |
aff3e498 | 5576 | struct cfs_rq *cfs_rq; |
b2b5ce02 PZ |
5577 | /* |
5578 | * If the task was not on the rq at the time of this cgroup movement | |
5579 | * it must have been asleep, sleeping tasks keep their ->vruntime | |
5580 | * absolute on their old rq until wakeup (needed for the fair sleeper | |
5581 | * bonus in place_entity()). | |
5582 | * | |
5583 | * If it was on the rq, we've just 'preempted' it, which does convert | |
5584 | * ->vruntime to a relative base. | |
5585 | * | |
5586 | * Make sure both cases convert their relative position when migrating | |
5587 | * to another cgroup's rq. This does somewhat interfere with the | |
5588 | * fair sleeper stuff for the first placement, but who cares. | |
5589 | */ | |
7ceff013 DN |
5590 | /* |
5591 | * When !on_rq, vruntime of the task has usually NOT been normalized. | |
5592 | * But there are some cases where it has already been normalized: | |
5593 | * | |
5594 | * - Moving a forked child which is waiting for being woken up by | |
5595 | * wake_up_new_task(). | |
62af3783 DN |
5596 | * - Moving a task which has been woken up by try_to_wake_up() and |
5597 | * waiting for actually being woken up by sched_ttwu_pending(). | |
7ceff013 DN |
5598 | * |
5599 | * To prevent boost or penalty in the new cfs_rq caused by delta | |
5600 | * min_vruntime between the two cfs_rqs, we skip vruntime adjustment. | |
5601 | */ | |
62af3783 | 5602 | if (!on_rq && (!p->se.sum_exec_runtime || p->state == TASK_WAKING)) |
7ceff013 DN |
5603 | on_rq = 1; |
5604 | ||
b2b5ce02 PZ |
5605 | if (!on_rq) |
5606 | p->se.vruntime -= cfs_rq_of(&p->se)->min_vruntime; | |
5607 | set_task_rq(p, task_cpu(p)); | |
aff3e498 PT |
5608 | if (!on_rq) { |
5609 | cfs_rq = cfs_rq_of(&p->se); | |
5610 | p->se.vruntime += cfs_rq->min_vruntime; | |
5611 | #ifdef CONFIG_SMP | |
5612 | /* | |
5613 | * migrate_task_rq_fair() will have removed our previous | |
5614 | * contribution, but we must synchronize for ongoing future | |
5615 | * decay. | |
5616 | */ | |
5617 | p->se.avg.decay_count = atomic64_read(&cfs_rq->decay_counter); | |
5618 | cfs_rq->blocked_load_avg += p->se.avg.load_avg_contrib; | |
5619 | #endif | |
5620 | } | |
810b3817 | 5621 | } |
029632fb PZ |
5622 | |
5623 | void free_fair_sched_group(struct task_group *tg) | |
5624 | { | |
5625 | int i; | |
5626 | ||
5627 | destroy_cfs_bandwidth(tg_cfs_bandwidth(tg)); | |
5628 | ||
5629 | for_each_possible_cpu(i) { | |
5630 | if (tg->cfs_rq) | |
5631 | kfree(tg->cfs_rq[i]); | |
5632 | if (tg->se) | |
5633 | kfree(tg->se[i]); | |
5634 | } | |
5635 | ||
5636 | kfree(tg->cfs_rq); | |
5637 | kfree(tg->se); | |
5638 | } | |
5639 | ||
5640 | int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent) | |
5641 | { | |
5642 | struct cfs_rq *cfs_rq; | |
5643 | struct sched_entity *se; | |
5644 | int i; | |
5645 | ||
5646 | tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL); | |
5647 | if (!tg->cfs_rq) | |
5648 | goto err; | |
5649 | tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL); | |
5650 | if (!tg->se) | |
5651 | goto err; | |
5652 | ||
5653 | tg->shares = NICE_0_LOAD; | |
5654 | ||
5655 | init_cfs_bandwidth(tg_cfs_bandwidth(tg)); | |
5656 | ||
5657 | for_each_possible_cpu(i) { | |
5658 | cfs_rq = kzalloc_node(sizeof(struct cfs_rq), | |
5659 | GFP_KERNEL, cpu_to_node(i)); | |
5660 | if (!cfs_rq) | |
5661 | goto err; | |
5662 | ||
5663 | se = kzalloc_node(sizeof(struct sched_entity), | |
5664 | GFP_KERNEL, cpu_to_node(i)); | |
5665 | if (!se) | |
5666 | goto err_free_rq; | |
5667 | ||
5668 | init_cfs_rq(cfs_rq); | |
5669 | init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]); | |
5670 | } | |
5671 | ||
5672 | return 1; | |
5673 | ||
5674 | err_free_rq: | |
5675 | kfree(cfs_rq); | |
5676 | err: | |
5677 | return 0; | |
5678 | } | |
5679 | ||
5680 | void unregister_fair_sched_group(struct task_group *tg, int cpu) | |
5681 | { | |
5682 | struct rq *rq = cpu_rq(cpu); | |
5683 | unsigned long flags; | |
5684 | ||
5685 | /* | |
5686 | * Only empty task groups can be destroyed; so we can speculatively | |
5687 | * check on_list without danger of it being re-added. | |
5688 | */ | |
5689 | if (!tg->cfs_rq[cpu]->on_list) | |
5690 | return; | |
5691 | ||
5692 | raw_spin_lock_irqsave(&rq->lock, flags); | |
5693 | list_del_leaf_cfs_rq(tg->cfs_rq[cpu]); | |
5694 | raw_spin_unlock_irqrestore(&rq->lock, flags); | |
5695 | } | |
5696 | ||
5697 | void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq, | |
5698 | struct sched_entity *se, int cpu, | |
5699 | struct sched_entity *parent) | |
5700 | { | |
5701 | struct rq *rq = cpu_rq(cpu); | |
5702 | ||
5703 | cfs_rq->tg = tg; | |
5704 | cfs_rq->rq = rq; | |
5705 | #ifdef CONFIG_SMP | |
5706 | /* allow initial update_cfs_load() to truncate */ | |
5707 | cfs_rq->load_stamp = 1; | |
810b3817 | 5708 | #endif |
029632fb PZ |
5709 | init_cfs_rq_runtime(cfs_rq); |
5710 | ||
5711 | tg->cfs_rq[cpu] = cfs_rq; | |
5712 | tg->se[cpu] = se; | |
5713 | ||
5714 | /* se could be NULL for root_task_group */ | |
5715 | if (!se) | |
5716 | return; | |
5717 | ||
5718 | if (!parent) | |
5719 | se->cfs_rq = &rq->cfs; | |
5720 | else | |
5721 | se->cfs_rq = parent->my_q; | |
5722 | ||
5723 | se->my_q = cfs_rq; | |
5724 | update_load_set(&se->load, 0); | |
5725 | se->parent = parent; | |
5726 | } | |
5727 | ||
5728 | static DEFINE_MUTEX(shares_mutex); | |
5729 | ||
5730 | int sched_group_set_shares(struct task_group *tg, unsigned long shares) | |
5731 | { | |
5732 | int i; | |
5733 | unsigned long flags; | |
5734 | ||
5735 | /* | |
5736 | * We can't change the weight of the root cgroup. | |
5737 | */ | |
5738 | if (!tg->se[0]) | |
5739 | return -EINVAL; | |
5740 | ||
5741 | shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES)); | |
5742 | ||
5743 | mutex_lock(&shares_mutex); | |
5744 | if (tg->shares == shares) | |
5745 | goto done; | |
5746 | ||
5747 | tg->shares = shares; | |
5748 | for_each_possible_cpu(i) { | |
5749 | struct rq *rq = cpu_rq(i); | |
5750 | struct sched_entity *se; | |
5751 | ||
5752 | se = tg->se[i]; | |
5753 | /* Propagate contribution to hierarchy */ | |
5754 | raw_spin_lock_irqsave(&rq->lock, flags); | |
5755 | for_each_sched_entity(se) | |
5756 | update_cfs_shares(group_cfs_rq(se)); | |
5757 | raw_spin_unlock_irqrestore(&rq->lock, flags); | |
5758 | } | |
5759 | ||
5760 | done: | |
5761 | mutex_unlock(&shares_mutex); | |
5762 | return 0; | |
5763 | } | |
5764 | #else /* CONFIG_FAIR_GROUP_SCHED */ | |
5765 | ||
5766 | void free_fair_sched_group(struct task_group *tg) { } | |
5767 | ||
5768 | int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent) | |
5769 | { | |
5770 | return 1; | |
5771 | } | |
5772 | ||
5773 | void unregister_fair_sched_group(struct task_group *tg, int cpu) { } | |
5774 | ||
5775 | #endif /* CONFIG_FAIR_GROUP_SCHED */ | |
5776 | ||
810b3817 | 5777 | |
6d686f45 | 5778 | static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task) |
0d721cea PW |
5779 | { |
5780 | struct sched_entity *se = &task->se; | |
0d721cea PW |
5781 | unsigned int rr_interval = 0; |
5782 | ||
5783 | /* | |
5784 | * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise | |
5785 | * idle runqueue: | |
5786 | */ | |
0d721cea PW |
5787 | if (rq->cfs.load.weight) |
5788 | rr_interval = NS_TO_JIFFIES(sched_slice(&rq->cfs, se)); | |
0d721cea PW |
5789 | |
5790 | return rr_interval; | |
5791 | } | |
5792 | ||
bf0f6f24 IM |
5793 | /* |
5794 | * All the scheduling class methods: | |
5795 | */ | |
029632fb | 5796 | const struct sched_class fair_sched_class = { |
5522d5d5 | 5797 | .next = &idle_sched_class, |
bf0f6f24 IM |
5798 | .enqueue_task = enqueue_task_fair, |
5799 | .dequeue_task = dequeue_task_fair, | |
5800 | .yield_task = yield_task_fair, | |
d95f4122 | 5801 | .yield_to_task = yield_to_task_fair, |
bf0f6f24 | 5802 | |
2e09bf55 | 5803 | .check_preempt_curr = check_preempt_wakeup, |
bf0f6f24 IM |
5804 | |
5805 | .pick_next_task = pick_next_task_fair, | |
5806 | .put_prev_task = put_prev_task_fair, | |
5807 | ||
681f3e68 | 5808 | #ifdef CONFIG_SMP |
4ce72a2c | 5809 | .select_task_rq = select_task_rq_fair, |
0a74bef8 | 5810 | .migrate_task_rq = migrate_task_rq_fair, |
4ce72a2c | 5811 | |
0bcdcf28 CE |
5812 | .rq_online = rq_online_fair, |
5813 | .rq_offline = rq_offline_fair, | |
88ec22d3 PZ |
5814 | |
5815 | .task_waking = task_waking_fair, | |
681f3e68 | 5816 | #endif |
bf0f6f24 | 5817 | |
83b699ed | 5818 | .set_curr_task = set_curr_task_fair, |
bf0f6f24 | 5819 | .task_tick = task_tick_fair, |
cd29fe6f | 5820 | .task_fork = task_fork_fair, |
cb469845 SR |
5821 | |
5822 | .prio_changed = prio_changed_fair, | |
da7a735e | 5823 | .switched_from = switched_from_fair, |
cb469845 | 5824 | .switched_to = switched_to_fair, |
810b3817 | 5825 | |
0d721cea PW |
5826 | .get_rr_interval = get_rr_interval_fair, |
5827 | ||
810b3817 | 5828 | #ifdef CONFIG_FAIR_GROUP_SCHED |
b2b5ce02 | 5829 | .task_move_group = task_move_group_fair, |
810b3817 | 5830 | #endif |
bf0f6f24 IM |
5831 | }; |
5832 | ||
5833 | #ifdef CONFIG_SCHED_DEBUG | |
029632fb | 5834 | void print_cfs_stats(struct seq_file *m, int cpu) |
bf0f6f24 | 5835 | { |
bf0f6f24 IM |
5836 | struct cfs_rq *cfs_rq; |
5837 | ||
5973e5b9 | 5838 | rcu_read_lock(); |
c3b64f1e | 5839 | for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq) |
5cef9eca | 5840 | print_cfs_rq(m, cpu, cfs_rq); |
5973e5b9 | 5841 | rcu_read_unlock(); |
bf0f6f24 IM |
5842 | } |
5843 | #endif | |
029632fb PZ |
5844 | |
5845 | __init void init_sched_fair_class(void) | |
5846 | { | |
5847 | #ifdef CONFIG_SMP | |
5848 | open_softirq(SCHED_SOFTIRQ, run_rebalance_domains); | |
5849 | ||
5850 | #ifdef CONFIG_NO_HZ | |
554cecaf | 5851 | nohz.next_balance = jiffies; |
029632fb | 5852 | zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT); |
71325960 | 5853 | cpu_notifier(sched_ilb_notifier, 0); |
029632fb PZ |
5854 | #endif |
5855 | #endif /* SMP */ | |
5856 | ||
5857 | } |