Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
bf0f6f24 IM |
2 | /* |
3 | * Completely Fair Scheduling (CFS) Class (SCHED_NORMAL/SCHED_BATCH) | |
4 | * | |
5 | * Copyright (C) 2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> | |
6 | * | |
7 | * Interactivity improvements by Mike Galbraith | |
8 | * (C) 2007 Mike Galbraith <efault@gmx.de> | |
9 | * | |
10 | * Various enhancements by Dmitry Adamushko. | |
11 | * (C) 2007 Dmitry Adamushko <dmitry.adamushko@gmail.com> | |
12 | * | |
13 | * Group scheduling enhancements by Srivatsa Vaddagiri | |
14 | * Copyright IBM Corporation, 2007 | |
15 | * Author: Srivatsa Vaddagiri <vatsa@linux.vnet.ibm.com> | |
16 | * | |
17 | * Scaled math optimizations by Thomas Gleixner | |
18 | * Copyright (C) 2007, Thomas Gleixner <tglx@linutronix.de> | |
21805085 PZ |
19 | * |
20 | * Adaptive scheduling granularity, math enhancements by Peter Zijlstra | |
90eec103 | 21 | * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra |
bf0f6f24 IM |
22 | */ |
23 | ||
589ee628 | 24 | #include <linux/sched/mm.h> |
105ab3d8 IM |
25 | #include <linux/sched/topology.h> |
26 | ||
cb251765 | 27 | #include <linux/latencytop.h> |
3436ae12 | 28 | #include <linux/cpumask.h> |
83a0a96a | 29 | #include <linux/cpuidle.h> |
029632fb PZ |
30 | #include <linux/slab.h> |
31 | #include <linux/profile.h> | |
32 | #include <linux/interrupt.h> | |
cbee9f88 | 33 | #include <linux/mempolicy.h> |
e14808b4 | 34 | #include <linux/migrate.h> |
cbee9f88 | 35 | #include <linux/task_work.h> |
029632fb PZ |
36 | |
37 | #include <trace/events/sched.h> | |
38 | ||
39 | #include "sched.h" | |
9745512c | 40 | |
bf0f6f24 | 41 | /* |
21805085 | 42 | * Targeted preemption latency for CPU-bound tasks: |
bf0f6f24 | 43 | * |
21805085 | 44 | * NOTE: this latency value is not the same as the concept of |
d274a4ce IM |
45 | * 'timeslice length' - timeslices in CFS are of variable length |
46 | * and have no persistent notion like in traditional, time-slice | |
47 | * based scheduling concepts. | |
bf0f6f24 | 48 | * |
d274a4ce IM |
49 | * (to see the precise effective timeslice length of your workload, |
50 | * run vmstat and monitor the context-switches (cs) field) | |
2b4d5b25 IM |
51 | * |
52 | * (default: 6ms * (1 + ilog(ncpus)), units: nanoseconds) | |
bf0f6f24 | 53 | */ |
2b4d5b25 IM |
54 | unsigned int sysctl_sched_latency = 6000000ULL; |
55 | unsigned int normalized_sysctl_sched_latency = 6000000ULL; | |
2bd8e6d4 | 56 | |
1983a922 CE |
57 | /* |
58 | * The initial- and re-scaling of tunables is configurable | |
1983a922 CE |
59 | * |
60 | * Options are: | |
2b4d5b25 IM |
61 | * |
62 | * SCHED_TUNABLESCALING_NONE - unscaled, always *1 | |
63 | * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus) | |
64 | * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus | |
65 | * | |
66 | * (default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus)) | |
1983a922 | 67 | */ |
2b4d5b25 | 68 | enum sched_tunable_scaling sysctl_sched_tunable_scaling = SCHED_TUNABLESCALING_LOG; |
1983a922 | 69 | |
2bd8e6d4 | 70 | /* |
b2be5e96 | 71 | * Minimal preemption granularity for CPU-bound tasks: |
2b4d5b25 | 72 | * |
864616ee | 73 | * (default: 0.75 msec * (1 + ilog(ncpus)), units: nanoseconds) |
2bd8e6d4 | 74 | */ |
2b4d5b25 IM |
75 | unsigned int sysctl_sched_min_granularity = 750000ULL; |
76 | unsigned int normalized_sysctl_sched_min_granularity = 750000ULL; | |
21805085 PZ |
77 | |
78 | /* | |
2b4d5b25 | 79 | * This value is kept at sysctl_sched_latency/sysctl_sched_min_granularity |
b2be5e96 | 80 | */ |
0bf377bb | 81 | static unsigned int sched_nr_latency = 8; |
b2be5e96 PZ |
82 | |
83 | /* | |
2bba22c5 | 84 | * After fork, child runs first. If set to 0 (default) then |
b2be5e96 | 85 | * parent will (try to) run first. |
21805085 | 86 | */ |
2bba22c5 | 87 | unsigned int sysctl_sched_child_runs_first __read_mostly; |
bf0f6f24 | 88 | |
bf0f6f24 IM |
89 | /* |
90 | * SCHED_OTHER wake-up granularity. | |
bf0f6f24 IM |
91 | * |
92 | * This option delays the preemption effects of decoupled workloads | |
93 | * and reduces their over-scheduling. Synchronous workloads will still | |
94 | * have immediate wakeup/sleep latencies. | |
2b4d5b25 IM |
95 | * |
96 | * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds) | |
bf0f6f24 | 97 | */ |
2b4d5b25 IM |
98 | unsigned int sysctl_sched_wakeup_granularity = 1000000UL; |
99 | unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL; | |
bf0f6f24 | 100 | |
2b4d5b25 | 101 | const_debug unsigned int sysctl_sched_migration_cost = 500000UL; |
da84d961 | 102 | |
afe06efd TC |
103 | #ifdef CONFIG_SMP |
104 | /* | |
105 | * For asym packing, by default the lower numbered cpu has higher priority. | |
106 | */ | |
107 | int __weak arch_asym_cpu_priority(int cpu) | |
108 | { | |
109 | return -cpu; | |
110 | } | |
111 | #endif | |
112 | ||
ec12cb7f PT |
113 | #ifdef CONFIG_CFS_BANDWIDTH |
114 | /* | |
115 | * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool | |
116 | * each time a cfs_rq requests quota. | |
117 | * | |
118 | * Note: in the case that the slice exceeds the runtime remaining (either due | |
119 | * to consumption or the quota being specified to be smaller than the slice) | |
120 | * we will always only issue the remaining available time. | |
121 | * | |
2b4d5b25 IM |
122 | * (default: 5 msec, units: microseconds) |
123 | */ | |
124 | unsigned int sysctl_sched_cfs_bandwidth_slice = 5000UL; | |
ec12cb7f PT |
125 | #endif |
126 | ||
3273163c MR |
127 | /* |
128 | * The margin used when comparing utilization with CPU capacity: | |
893c5d22 | 129 | * util * margin < capacity * 1024 |
2b4d5b25 IM |
130 | * |
131 | * (default: ~20%) | |
3273163c | 132 | */ |
2b4d5b25 | 133 | unsigned int capacity_margin = 1280; |
3273163c | 134 | |
8527632d PG |
135 | static inline void update_load_add(struct load_weight *lw, unsigned long inc) |
136 | { | |
137 | lw->weight += inc; | |
138 | lw->inv_weight = 0; | |
139 | } | |
140 | ||
141 | static inline void update_load_sub(struct load_weight *lw, unsigned long dec) | |
142 | { | |
143 | lw->weight -= dec; | |
144 | lw->inv_weight = 0; | |
145 | } | |
146 | ||
147 | static inline void update_load_set(struct load_weight *lw, unsigned long w) | |
148 | { | |
149 | lw->weight = w; | |
150 | lw->inv_weight = 0; | |
151 | } | |
152 | ||
029632fb PZ |
153 | /* |
154 | * Increase the granularity value when there are more CPUs, | |
155 | * because with more CPUs the 'effective latency' as visible | |
156 | * to users decreases. But the relationship is not linear, | |
157 | * so pick a second-best guess by going with the log2 of the | |
158 | * number of CPUs. | |
159 | * | |
160 | * This idea comes from the SD scheduler of Con Kolivas: | |
161 | */ | |
58ac93e4 | 162 | static unsigned int get_update_sysctl_factor(void) |
029632fb | 163 | { |
58ac93e4 | 164 | unsigned int cpus = min_t(unsigned int, num_online_cpus(), 8); |
029632fb PZ |
165 | unsigned int factor; |
166 | ||
167 | switch (sysctl_sched_tunable_scaling) { | |
168 | case SCHED_TUNABLESCALING_NONE: | |
169 | factor = 1; | |
170 | break; | |
171 | case SCHED_TUNABLESCALING_LINEAR: | |
172 | factor = cpus; | |
173 | break; | |
174 | case SCHED_TUNABLESCALING_LOG: | |
175 | default: | |
176 | factor = 1 + ilog2(cpus); | |
177 | break; | |
178 | } | |
179 | ||
180 | return factor; | |
181 | } | |
182 | ||
183 | static void update_sysctl(void) | |
184 | { | |
185 | unsigned int factor = get_update_sysctl_factor(); | |
186 | ||
187 | #define SET_SYSCTL(name) \ | |
188 | (sysctl_##name = (factor) * normalized_sysctl_##name) | |
189 | SET_SYSCTL(sched_min_granularity); | |
190 | SET_SYSCTL(sched_latency); | |
191 | SET_SYSCTL(sched_wakeup_granularity); | |
192 | #undef SET_SYSCTL | |
193 | } | |
194 | ||
195 | void sched_init_granularity(void) | |
196 | { | |
197 | update_sysctl(); | |
198 | } | |
199 | ||
9dbdb155 | 200 | #define WMULT_CONST (~0U) |
029632fb PZ |
201 | #define WMULT_SHIFT 32 |
202 | ||
9dbdb155 PZ |
203 | static void __update_inv_weight(struct load_weight *lw) |
204 | { | |
205 | unsigned long w; | |
206 | ||
207 | if (likely(lw->inv_weight)) | |
208 | return; | |
209 | ||
210 | w = scale_load_down(lw->weight); | |
211 | ||
212 | if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST)) | |
213 | lw->inv_weight = 1; | |
214 | else if (unlikely(!w)) | |
215 | lw->inv_weight = WMULT_CONST; | |
216 | else | |
217 | lw->inv_weight = WMULT_CONST / w; | |
218 | } | |
029632fb PZ |
219 | |
220 | /* | |
9dbdb155 PZ |
221 | * delta_exec * weight / lw.weight |
222 | * OR | |
223 | * (delta_exec * (weight * lw->inv_weight)) >> WMULT_SHIFT | |
224 | * | |
1c3de5e1 | 225 | * Either weight := NICE_0_LOAD and lw \e sched_prio_to_wmult[], in which case |
9dbdb155 PZ |
226 | * we're guaranteed shift stays positive because inv_weight is guaranteed to |
227 | * fit 32 bits, and NICE_0_LOAD gives another 10 bits; therefore shift >= 22. | |
228 | * | |
229 | * Or, weight =< lw.weight (because lw.weight is the runqueue weight), thus | |
230 | * weight/lw.weight <= 1, and therefore our shift will also be positive. | |
029632fb | 231 | */ |
9dbdb155 | 232 | static u64 __calc_delta(u64 delta_exec, unsigned long weight, struct load_weight *lw) |
029632fb | 233 | { |
9dbdb155 PZ |
234 | u64 fact = scale_load_down(weight); |
235 | int shift = WMULT_SHIFT; | |
029632fb | 236 | |
9dbdb155 | 237 | __update_inv_weight(lw); |
029632fb | 238 | |
9dbdb155 PZ |
239 | if (unlikely(fact >> 32)) { |
240 | while (fact >> 32) { | |
241 | fact >>= 1; | |
242 | shift--; | |
243 | } | |
029632fb PZ |
244 | } |
245 | ||
9dbdb155 PZ |
246 | /* hint to use a 32x32->64 mul */ |
247 | fact = (u64)(u32)fact * lw->inv_weight; | |
029632fb | 248 | |
9dbdb155 PZ |
249 | while (fact >> 32) { |
250 | fact >>= 1; | |
251 | shift--; | |
252 | } | |
029632fb | 253 | |
9dbdb155 | 254 | return mul_u64_u32_shr(delta_exec, fact, shift); |
029632fb PZ |
255 | } |
256 | ||
257 | ||
258 | const struct sched_class fair_sched_class; | |
a4c2f00f | 259 | |
bf0f6f24 IM |
260 | /************************************************************** |
261 | * CFS operations on generic schedulable entities: | |
262 | */ | |
263 | ||
62160e3f | 264 | #ifdef CONFIG_FAIR_GROUP_SCHED |
bf0f6f24 | 265 | |
62160e3f | 266 | /* cpu runqueue to which this cfs_rq is attached */ |
bf0f6f24 IM |
267 | static inline struct rq *rq_of(struct cfs_rq *cfs_rq) |
268 | { | |
62160e3f | 269 | return cfs_rq->rq; |
bf0f6f24 IM |
270 | } |
271 | ||
62160e3f IM |
272 | /* An entity is a task if it doesn't "own" a runqueue */ |
273 | #define entity_is_task(se) (!se->my_q) | |
bf0f6f24 | 274 | |
8f48894f PZ |
275 | static inline struct task_struct *task_of(struct sched_entity *se) |
276 | { | |
9148a3a1 | 277 | SCHED_WARN_ON(!entity_is_task(se)); |
8f48894f PZ |
278 | return container_of(se, struct task_struct, se); |
279 | } | |
280 | ||
b758149c PZ |
281 | /* Walk up scheduling entities hierarchy */ |
282 | #define for_each_sched_entity(se) \ | |
283 | for (; se; se = se->parent) | |
284 | ||
285 | static inline struct cfs_rq *task_cfs_rq(struct task_struct *p) | |
286 | { | |
287 | return p->se.cfs_rq; | |
288 | } | |
289 | ||
290 | /* runqueue on which this entity is (to be) queued */ | |
291 | static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se) | |
292 | { | |
293 | return se->cfs_rq; | |
294 | } | |
295 | ||
296 | /* runqueue "owned" by this group */ | |
297 | static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp) | |
298 | { | |
299 | return grp->my_q; | |
300 | } | |
301 | ||
3d4b47b4 PZ |
302 | static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq) |
303 | { | |
304 | if (!cfs_rq->on_list) { | |
9c2791f9 VG |
305 | struct rq *rq = rq_of(cfs_rq); |
306 | int cpu = cpu_of(rq); | |
67e86250 PT |
307 | /* |
308 | * Ensure we either appear before our parent (if already | |
309 | * enqueued) or force our parent to appear after us when it is | |
9c2791f9 VG |
310 | * enqueued. The fact that we always enqueue bottom-up |
311 | * reduces this to two cases and a special case for the root | |
312 | * cfs_rq. Furthermore, it also means that we will always reset | |
313 | * tmp_alone_branch either when the branch is connected | |
314 | * to a tree or when we reach the beg of the tree | |
67e86250 PT |
315 | */ |
316 | if (cfs_rq->tg->parent && | |
9c2791f9 VG |
317 | cfs_rq->tg->parent->cfs_rq[cpu]->on_list) { |
318 | /* | |
319 | * If parent is already on the list, we add the child | |
320 | * just before. Thanks to circular linked property of | |
321 | * the list, this means to put the child at the tail | |
322 | * of the list that starts by parent. | |
323 | */ | |
324 | list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list, | |
325 | &(cfs_rq->tg->parent->cfs_rq[cpu]->leaf_cfs_rq_list)); | |
326 | /* | |
327 | * The branch is now connected to its tree so we can | |
328 | * reset tmp_alone_branch to the beginning of the | |
329 | * list. | |
330 | */ | |
331 | rq->tmp_alone_branch = &rq->leaf_cfs_rq_list; | |
332 | } else if (!cfs_rq->tg->parent) { | |
333 | /* | |
334 | * cfs rq without parent should be put | |
335 | * at the tail of the list. | |
336 | */ | |
67e86250 | 337 | list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list, |
9c2791f9 VG |
338 | &rq->leaf_cfs_rq_list); |
339 | /* | |
340 | * We have reach the beg of a tree so we can reset | |
341 | * tmp_alone_branch to the beginning of the list. | |
342 | */ | |
343 | rq->tmp_alone_branch = &rq->leaf_cfs_rq_list; | |
344 | } else { | |
345 | /* | |
346 | * The parent has not already been added so we want to | |
347 | * make sure that it will be put after us. | |
348 | * tmp_alone_branch points to the beg of the branch | |
349 | * where we will add parent. | |
350 | */ | |
351 | list_add_rcu(&cfs_rq->leaf_cfs_rq_list, | |
352 | rq->tmp_alone_branch); | |
353 | /* | |
354 | * update tmp_alone_branch to points to the new beg | |
355 | * of the branch | |
356 | */ | |
357 | rq->tmp_alone_branch = &cfs_rq->leaf_cfs_rq_list; | |
67e86250 | 358 | } |
3d4b47b4 PZ |
359 | |
360 | cfs_rq->on_list = 1; | |
361 | } | |
362 | } | |
363 | ||
364 | static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq) | |
365 | { | |
366 | if (cfs_rq->on_list) { | |
367 | list_del_rcu(&cfs_rq->leaf_cfs_rq_list); | |
368 | cfs_rq->on_list = 0; | |
369 | } | |
370 | } | |
371 | ||
b758149c | 372 | /* Iterate thr' all leaf cfs_rq's on a runqueue */ |
a9e7f654 TH |
373 | #define for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) \ |
374 | list_for_each_entry_safe(cfs_rq, pos, &rq->leaf_cfs_rq_list, \ | |
375 | leaf_cfs_rq_list) | |
b758149c PZ |
376 | |
377 | /* Do the two (enqueued) entities belong to the same group ? */ | |
fed14d45 | 378 | static inline struct cfs_rq * |
b758149c PZ |
379 | is_same_group(struct sched_entity *se, struct sched_entity *pse) |
380 | { | |
381 | if (se->cfs_rq == pse->cfs_rq) | |
fed14d45 | 382 | return se->cfs_rq; |
b758149c | 383 | |
fed14d45 | 384 | return NULL; |
b758149c PZ |
385 | } |
386 | ||
387 | static inline struct sched_entity *parent_entity(struct sched_entity *se) | |
388 | { | |
389 | return se->parent; | |
390 | } | |
391 | ||
464b7527 PZ |
392 | static void |
393 | find_matching_se(struct sched_entity **se, struct sched_entity **pse) | |
394 | { | |
395 | int se_depth, pse_depth; | |
396 | ||
397 | /* | |
398 | * preemption test can be made between sibling entities who are in the | |
399 | * same cfs_rq i.e who have a common parent. Walk up the hierarchy of | |
400 | * both tasks until we find their ancestors who are siblings of common | |
401 | * parent. | |
402 | */ | |
403 | ||
404 | /* First walk up until both entities are at same depth */ | |
fed14d45 PZ |
405 | se_depth = (*se)->depth; |
406 | pse_depth = (*pse)->depth; | |
464b7527 PZ |
407 | |
408 | while (se_depth > pse_depth) { | |
409 | se_depth--; | |
410 | *se = parent_entity(*se); | |
411 | } | |
412 | ||
413 | while (pse_depth > se_depth) { | |
414 | pse_depth--; | |
415 | *pse = parent_entity(*pse); | |
416 | } | |
417 | ||
418 | while (!is_same_group(*se, *pse)) { | |
419 | *se = parent_entity(*se); | |
420 | *pse = parent_entity(*pse); | |
421 | } | |
422 | } | |
423 | ||
8f48894f PZ |
424 | #else /* !CONFIG_FAIR_GROUP_SCHED */ |
425 | ||
426 | static inline struct task_struct *task_of(struct sched_entity *se) | |
427 | { | |
428 | return container_of(se, struct task_struct, se); | |
429 | } | |
bf0f6f24 | 430 | |
62160e3f IM |
431 | static inline struct rq *rq_of(struct cfs_rq *cfs_rq) |
432 | { | |
433 | return container_of(cfs_rq, struct rq, cfs); | |
bf0f6f24 IM |
434 | } |
435 | ||
436 | #define entity_is_task(se) 1 | |
437 | ||
b758149c PZ |
438 | #define for_each_sched_entity(se) \ |
439 | for (; se; se = NULL) | |
bf0f6f24 | 440 | |
b758149c | 441 | static inline struct cfs_rq *task_cfs_rq(struct task_struct *p) |
bf0f6f24 | 442 | { |
b758149c | 443 | return &task_rq(p)->cfs; |
bf0f6f24 IM |
444 | } |
445 | ||
b758149c PZ |
446 | static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se) |
447 | { | |
448 | struct task_struct *p = task_of(se); | |
449 | struct rq *rq = task_rq(p); | |
450 | ||
451 | return &rq->cfs; | |
452 | } | |
453 | ||
454 | /* runqueue "owned" by this group */ | |
455 | static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp) | |
456 | { | |
457 | return NULL; | |
458 | } | |
459 | ||
3d4b47b4 PZ |
460 | static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq) |
461 | { | |
462 | } | |
463 | ||
464 | static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq) | |
465 | { | |
466 | } | |
467 | ||
a9e7f654 TH |
468 | #define for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) \ |
469 | for (cfs_rq = &rq->cfs, pos = NULL; cfs_rq; cfs_rq = pos) | |
b758149c | 470 | |
b758149c PZ |
471 | static inline struct sched_entity *parent_entity(struct sched_entity *se) |
472 | { | |
473 | return NULL; | |
474 | } | |
475 | ||
464b7527 PZ |
476 | static inline void |
477 | find_matching_se(struct sched_entity **se, struct sched_entity **pse) | |
478 | { | |
479 | } | |
480 | ||
b758149c PZ |
481 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
482 | ||
6c16a6dc | 483 | static __always_inline |
9dbdb155 | 484 | void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec); |
bf0f6f24 IM |
485 | |
486 | /************************************************************** | |
487 | * Scheduling class tree data structure manipulation methods: | |
488 | */ | |
489 | ||
1bf08230 | 490 | static inline u64 max_vruntime(u64 max_vruntime, u64 vruntime) |
02e0431a | 491 | { |
1bf08230 | 492 | s64 delta = (s64)(vruntime - max_vruntime); |
368059a9 | 493 | if (delta > 0) |
1bf08230 | 494 | max_vruntime = vruntime; |
02e0431a | 495 | |
1bf08230 | 496 | return max_vruntime; |
02e0431a PZ |
497 | } |
498 | ||
0702e3eb | 499 | static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime) |
b0ffd246 PZ |
500 | { |
501 | s64 delta = (s64)(vruntime - min_vruntime); | |
502 | if (delta < 0) | |
503 | min_vruntime = vruntime; | |
504 | ||
505 | return min_vruntime; | |
506 | } | |
507 | ||
54fdc581 FC |
508 | static inline int entity_before(struct sched_entity *a, |
509 | struct sched_entity *b) | |
510 | { | |
511 | return (s64)(a->vruntime - b->vruntime) < 0; | |
512 | } | |
513 | ||
1af5f730 PZ |
514 | static void update_min_vruntime(struct cfs_rq *cfs_rq) |
515 | { | |
b60205c7 | 516 | struct sched_entity *curr = cfs_rq->curr; |
bfb06889 | 517 | struct rb_node *leftmost = rb_first_cached(&cfs_rq->tasks_timeline); |
b60205c7 | 518 | |
1af5f730 PZ |
519 | u64 vruntime = cfs_rq->min_vruntime; |
520 | ||
b60205c7 PZ |
521 | if (curr) { |
522 | if (curr->on_rq) | |
523 | vruntime = curr->vruntime; | |
524 | else | |
525 | curr = NULL; | |
526 | } | |
1af5f730 | 527 | |
bfb06889 DB |
528 | if (leftmost) { /* non-empty tree */ |
529 | struct sched_entity *se; | |
530 | se = rb_entry(leftmost, struct sched_entity, run_node); | |
1af5f730 | 531 | |
b60205c7 | 532 | if (!curr) |
1af5f730 PZ |
533 | vruntime = se->vruntime; |
534 | else | |
535 | vruntime = min_vruntime(vruntime, se->vruntime); | |
536 | } | |
537 | ||
1bf08230 | 538 | /* ensure we never gain time by being placed backwards. */ |
1af5f730 | 539 | cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime); |
3fe1698b PZ |
540 | #ifndef CONFIG_64BIT |
541 | smp_wmb(); | |
542 | cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime; | |
543 | #endif | |
1af5f730 PZ |
544 | } |
545 | ||
bf0f6f24 IM |
546 | /* |
547 | * Enqueue an entity into the rb-tree: | |
548 | */ | |
0702e3eb | 549 | static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 | 550 | { |
bfb06889 | 551 | struct rb_node **link = &cfs_rq->tasks_timeline.rb_root.rb_node; |
bf0f6f24 IM |
552 | struct rb_node *parent = NULL; |
553 | struct sched_entity *entry; | |
bfb06889 | 554 | bool leftmost = true; |
bf0f6f24 IM |
555 | |
556 | /* | |
557 | * Find the right place in the rbtree: | |
558 | */ | |
559 | while (*link) { | |
560 | parent = *link; | |
561 | entry = rb_entry(parent, struct sched_entity, run_node); | |
562 | /* | |
563 | * We dont care about collisions. Nodes with | |
564 | * the same key stay together. | |
565 | */ | |
2bd2d6f2 | 566 | if (entity_before(se, entry)) { |
bf0f6f24 IM |
567 | link = &parent->rb_left; |
568 | } else { | |
569 | link = &parent->rb_right; | |
bfb06889 | 570 | leftmost = false; |
bf0f6f24 IM |
571 | } |
572 | } | |
573 | ||
bf0f6f24 | 574 | rb_link_node(&se->run_node, parent, link); |
bfb06889 DB |
575 | rb_insert_color_cached(&se->run_node, |
576 | &cfs_rq->tasks_timeline, leftmost); | |
bf0f6f24 IM |
577 | } |
578 | ||
0702e3eb | 579 | static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 | 580 | { |
bfb06889 | 581 | rb_erase_cached(&se->run_node, &cfs_rq->tasks_timeline); |
bf0f6f24 IM |
582 | } |
583 | ||
029632fb | 584 | struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq) |
bf0f6f24 | 585 | { |
bfb06889 | 586 | struct rb_node *left = rb_first_cached(&cfs_rq->tasks_timeline); |
f4b6755f PZ |
587 | |
588 | if (!left) | |
589 | return NULL; | |
590 | ||
591 | return rb_entry(left, struct sched_entity, run_node); | |
bf0f6f24 IM |
592 | } |
593 | ||
ac53db59 RR |
594 | static struct sched_entity *__pick_next_entity(struct sched_entity *se) |
595 | { | |
596 | struct rb_node *next = rb_next(&se->run_node); | |
597 | ||
598 | if (!next) | |
599 | return NULL; | |
600 | ||
601 | return rb_entry(next, struct sched_entity, run_node); | |
602 | } | |
603 | ||
604 | #ifdef CONFIG_SCHED_DEBUG | |
029632fb | 605 | struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq) |
aeb73b04 | 606 | { |
bfb06889 | 607 | struct rb_node *last = rb_last(&cfs_rq->tasks_timeline.rb_root); |
aeb73b04 | 608 | |
70eee74b BS |
609 | if (!last) |
610 | return NULL; | |
7eee3e67 IM |
611 | |
612 | return rb_entry(last, struct sched_entity, run_node); | |
aeb73b04 PZ |
613 | } |
614 | ||
bf0f6f24 IM |
615 | /************************************************************** |
616 | * Scheduling class statistics methods: | |
617 | */ | |
618 | ||
acb4a848 | 619 | int sched_proc_update_handler(struct ctl_table *table, int write, |
8d65af78 | 620 | void __user *buffer, size_t *lenp, |
b2be5e96 PZ |
621 | loff_t *ppos) |
622 | { | |
8d65af78 | 623 | int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
58ac93e4 | 624 | unsigned int factor = get_update_sysctl_factor(); |
b2be5e96 PZ |
625 | |
626 | if (ret || !write) | |
627 | return ret; | |
628 | ||
629 | sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency, | |
630 | sysctl_sched_min_granularity); | |
631 | ||
acb4a848 CE |
632 | #define WRT_SYSCTL(name) \ |
633 | (normalized_sysctl_##name = sysctl_##name / (factor)) | |
634 | WRT_SYSCTL(sched_min_granularity); | |
635 | WRT_SYSCTL(sched_latency); | |
636 | WRT_SYSCTL(sched_wakeup_granularity); | |
acb4a848 CE |
637 | #undef WRT_SYSCTL |
638 | ||
b2be5e96 PZ |
639 | return 0; |
640 | } | |
641 | #endif | |
647e7cac | 642 | |
a7be37ac | 643 | /* |
f9c0b095 | 644 | * delta /= w |
a7be37ac | 645 | */ |
9dbdb155 | 646 | static inline u64 calc_delta_fair(u64 delta, struct sched_entity *se) |
a7be37ac | 647 | { |
f9c0b095 | 648 | if (unlikely(se->load.weight != NICE_0_LOAD)) |
9dbdb155 | 649 | delta = __calc_delta(delta, NICE_0_LOAD, &se->load); |
a7be37ac PZ |
650 | |
651 | return delta; | |
652 | } | |
653 | ||
647e7cac IM |
654 | /* |
655 | * The idea is to set a period in which each task runs once. | |
656 | * | |
532b1858 | 657 | * When there are too many tasks (sched_nr_latency) we have to stretch |
647e7cac IM |
658 | * this period because otherwise the slices get too small. |
659 | * | |
660 | * p = (nr <= nl) ? l : l*nr/nl | |
661 | */ | |
4d78e7b6 PZ |
662 | static u64 __sched_period(unsigned long nr_running) |
663 | { | |
8e2b0bf3 BF |
664 | if (unlikely(nr_running > sched_nr_latency)) |
665 | return nr_running * sysctl_sched_min_granularity; | |
666 | else | |
667 | return sysctl_sched_latency; | |
4d78e7b6 PZ |
668 | } |
669 | ||
647e7cac IM |
670 | /* |
671 | * We calculate the wall-time slice from the period by taking a part | |
672 | * proportional to the weight. | |
673 | * | |
f9c0b095 | 674 | * s = p*P[w/rw] |
647e7cac | 675 | */ |
6d0f0ebd | 676 | static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se) |
21805085 | 677 | { |
0a582440 | 678 | u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq); |
f9c0b095 | 679 | |
0a582440 | 680 | for_each_sched_entity(se) { |
6272d68c | 681 | struct load_weight *load; |
3104bf03 | 682 | struct load_weight lw; |
6272d68c LM |
683 | |
684 | cfs_rq = cfs_rq_of(se); | |
685 | load = &cfs_rq->load; | |
f9c0b095 | 686 | |
0a582440 | 687 | if (unlikely(!se->on_rq)) { |
3104bf03 | 688 | lw = cfs_rq->load; |
0a582440 MG |
689 | |
690 | update_load_add(&lw, se->load.weight); | |
691 | load = &lw; | |
692 | } | |
9dbdb155 | 693 | slice = __calc_delta(slice, se->load.weight, load); |
0a582440 MG |
694 | } |
695 | return slice; | |
bf0f6f24 IM |
696 | } |
697 | ||
647e7cac | 698 | /* |
660cc00f | 699 | * We calculate the vruntime slice of a to-be-inserted task. |
647e7cac | 700 | * |
f9c0b095 | 701 | * vs = s/w |
647e7cac | 702 | */ |
f9c0b095 | 703 | static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se) |
67e9fb2a | 704 | { |
f9c0b095 | 705 | return calc_delta_fair(sched_slice(cfs_rq, se), se); |
a7be37ac PZ |
706 | } |
707 | ||
a75cdaa9 | 708 | #ifdef CONFIG_SMP |
283e2ed3 PZ |
709 | |
710 | #include "sched-pelt.h" | |
711 | ||
772bd008 | 712 | static int select_idle_sibling(struct task_struct *p, int prev_cpu, int cpu); |
fb13c7ee MG |
713 | static unsigned long task_h_load(struct task_struct *p); |
714 | ||
540247fb YD |
715 | /* Give new sched_entity start runnable values to heavy its load in infant time */ |
716 | void init_entity_runnable_average(struct sched_entity *se) | |
a75cdaa9 | 717 | { |
540247fb | 718 | struct sched_avg *sa = &se->avg; |
a75cdaa9 | 719 | |
9d89c257 YD |
720 | sa->last_update_time = 0; |
721 | /* | |
722 | * sched_avg's period_contrib should be strictly less then 1024, so | |
723 | * we give it 1023 to make sure it is almost a period (1024us), and | |
724 | * will definitely be update (after enqueue). | |
725 | */ | |
726 | sa->period_contrib = 1023; | |
b5a9b340 VG |
727 | /* |
728 | * Tasks are intialized with full load to be seen as heavy tasks until | |
729 | * they get a chance to stabilize to their real load level. | |
730 | * Group entities are intialized with zero load to reflect the fact that | |
731 | * nothing has been attached to the task group yet. | |
732 | */ | |
733 | if (entity_is_task(se)) | |
734 | sa->load_avg = scale_load_down(se->load.weight); | |
9d89c257 | 735 | sa->load_sum = sa->load_avg * LOAD_AVG_MAX; |
2b8c41da YD |
736 | /* |
737 | * At this point, util_avg won't be used in select_task_rq_fair anyway | |
738 | */ | |
739 | sa->util_avg = 0; | |
740 | sa->util_sum = 0; | |
9d89c257 | 741 | /* when this task enqueue'ed, it will contribute to its cfs_rq's load_avg */ |
a75cdaa9 | 742 | } |
7ea241af | 743 | |
7dc603c9 | 744 | static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq); |
df217913 | 745 | static void attach_entity_cfs_rq(struct sched_entity *se); |
7dc603c9 | 746 | |
2b8c41da YD |
747 | /* |
748 | * With new tasks being created, their initial util_avgs are extrapolated | |
749 | * based on the cfs_rq's current util_avg: | |
750 | * | |
751 | * util_avg = cfs_rq->util_avg / (cfs_rq->load_avg + 1) * se.load.weight | |
752 | * | |
753 | * However, in many cases, the above util_avg does not give a desired | |
754 | * value. Moreover, the sum of the util_avgs may be divergent, such | |
755 | * as when the series is a harmonic series. | |
756 | * | |
757 | * To solve this problem, we also cap the util_avg of successive tasks to | |
758 | * only 1/2 of the left utilization budget: | |
759 | * | |
760 | * util_avg_cap = (1024 - cfs_rq->avg.util_avg) / 2^n | |
761 | * | |
762 | * where n denotes the nth task. | |
763 | * | |
764 | * For example, a simplest series from the beginning would be like: | |
765 | * | |
766 | * task util_avg: 512, 256, 128, 64, 32, 16, 8, ... | |
767 | * cfs_rq util_avg: 512, 768, 896, 960, 992, 1008, 1016, ... | |
768 | * | |
769 | * Finally, that extrapolated util_avg is clamped to the cap (util_avg_cap) | |
770 | * if util_avg > util_avg_cap. | |
771 | */ | |
772 | void post_init_entity_util_avg(struct sched_entity *se) | |
773 | { | |
774 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
775 | struct sched_avg *sa = &se->avg; | |
172895e6 | 776 | long cap = (long)(SCHED_CAPACITY_SCALE - cfs_rq->avg.util_avg) / 2; |
2b8c41da YD |
777 | |
778 | if (cap > 0) { | |
779 | if (cfs_rq->avg.util_avg != 0) { | |
780 | sa->util_avg = cfs_rq->avg.util_avg * se->load.weight; | |
781 | sa->util_avg /= (cfs_rq->avg.load_avg + 1); | |
782 | ||
783 | if (sa->util_avg > cap) | |
784 | sa->util_avg = cap; | |
785 | } else { | |
786 | sa->util_avg = cap; | |
787 | } | |
788 | sa->util_sum = sa->util_avg * LOAD_AVG_MAX; | |
789 | } | |
7dc603c9 PZ |
790 | |
791 | if (entity_is_task(se)) { | |
792 | struct task_struct *p = task_of(se); | |
793 | if (p->sched_class != &fair_sched_class) { | |
794 | /* | |
795 | * For !fair tasks do: | |
796 | * | |
3a123bbb | 797 | update_cfs_rq_load_avg(now, cfs_rq); |
7dc603c9 PZ |
798 | attach_entity_load_avg(cfs_rq, se); |
799 | switched_from_fair(rq, p); | |
800 | * | |
801 | * such that the next switched_to_fair() has the | |
802 | * expected state. | |
803 | */ | |
df217913 | 804 | se->avg.last_update_time = cfs_rq_clock_task(cfs_rq); |
7dc603c9 PZ |
805 | return; |
806 | } | |
807 | } | |
808 | ||
df217913 | 809 | attach_entity_cfs_rq(se); |
2b8c41da YD |
810 | } |
811 | ||
7dc603c9 | 812 | #else /* !CONFIG_SMP */ |
540247fb | 813 | void init_entity_runnable_average(struct sched_entity *se) |
a75cdaa9 AS |
814 | { |
815 | } | |
2b8c41da YD |
816 | void post_init_entity_util_avg(struct sched_entity *se) |
817 | { | |
818 | } | |
3d30544f PZ |
819 | static void update_tg_load_avg(struct cfs_rq *cfs_rq, int force) |
820 | { | |
821 | } | |
7dc603c9 | 822 | #endif /* CONFIG_SMP */ |
a75cdaa9 | 823 | |
bf0f6f24 | 824 | /* |
9dbdb155 | 825 | * Update the current task's runtime statistics. |
bf0f6f24 | 826 | */ |
b7cc0896 | 827 | static void update_curr(struct cfs_rq *cfs_rq) |
bf0f6f24 | 828 | { |
429d43bc | 829 | struct sched_entity *curr = cfs_rq->curr; |
78becc27 | 830 | u64 now = rq_clock_task(rq_of(cfs_rq)); |
9dbdb155 | 831 | u64 delta_exec; |
bf0f6f24 IM |
832 | |
833 | if (unlikely(!curr)) | |
834 | return; | |
835 | ||
9dbdb155 PZ |
836 | delta_exec = now - curr->exec_start; |
837 | if (unlikely((s64)delta_exec <= 0)) | |
34f28ecd | 838 | return; |
bf0f6f24 | 839 | |
8ebc91d9 | 840 | curr->exec_start = now; |
d842de87 | 841 | |
9dbdb155 PZ |
842 | schedstat_set(curr->statistics.exec_max, |
843 | max(delta_exec, curr->statistics.exec_max)); | |
844 | ||
845 | curr->sum_exec_runtime += delta_exec; | |
ae92882e | 846 | schedstat_add(cfs_rq->exec_clock, delta_exec); |
9dbdb155 PZ |
847 | |
848 | curr->vruntime += calc_delta_fair(delta_exec, curr); | |
849 | update_min_vruntime(cfs_rq); | |
850 | ||
d842de87 SV |
851 | if (entity_is_task(curr)) { |
852 | struct task_struct *curtask = task_of(curr); | |
853 | ||
f977bb49 | 854 | trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime); |
d842de87 | 855 | cpuacct_charge(curtask, delta_exec); |
f06febc9 | 856 | account_group_exec_runtime(curtask, delta_exec); |
d842de87 | 857 | } |
ec12cb7f PT |
858 | |
859 | account_cfs_rq_runtime(cfs_rq, delta_exec); | |
bf0f6f24 IM |
860 | } |
861 | ||
6e998916 SG |
862 | static void update_curr_fair(struct rq *rq) |
863 | { | |
864 | update_curr(cfs_rq_of(&rq->curr->se)); | |
865 | } | |
866 | ||
bf0f6f24 | 867 | static inline void |
5870db5b | 868 | update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 | 869 | { |
4fa8d299 JP |
870 | u64 wait_start, prev_wait_start; |
871 | ||
872 | if (!schedstat_enabled()) | |
873 | return; | |
874 | ||
875 | wait_start = rq_clock(rq_of(cfs_rq)); | |
876 | prev_wait_start = schedstat_val(se->statistics.wait_start); | |
3ea94de1 JP |
877 | |
878 | if (entity_is_task(se) && task_on_rq_migrating(task_of(se)) && | |
4fa8d299 JP |
879 | likely(wait_start > prev_wait_start)) |
880 | wait_start -= prev_wait_start; | |
3ea94de1 | 881 | |
4fa8d299 | 882 | schedstat_set(se->statistics.wait_start, wait_start); |
bf0f6f24 IM |
883 | } |
884 | ||
4fa8d299 | 885 | static inline void |
3ea94de1 JP |
886 | update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se) |
887 | { | |
888 | struct task_struct *p; | |
cb251765 MG |
889 | u64 delta; |
890 | ||
4fa8d299 JP |
891 | if (!schedstat_enabled()) |
892 | return; | |
893 | ||
894 | delta = rq_clock(rq_of(cfs_rq)) - schedstat_val(se->statistics.wait_start); | |
3ea94de1 JP |
895 | |
896 | if (entity_is_task(se)) { | |
897 | p = task_of(se); | |
898 | if (task_on_rq_migrating(p)) { | |
899 | /* | |
900 | * Preserve migrating task's wait time so wait_start | |
901 | * time stamp can be adjusted to accumulate wait time | |
902 | * prior to migration. | |
903 | */ | |
4fa8d299 | 904 | schedstat_set(se->statistics.wait_start, delta); |
3ea94de1 JP |
905 | return; |
906 | } | |
907 | trace_sched_stat_wait(p, delta); | |
908 | } | |
909 | ||
4fa8d299 JP |
910 | schedstat_set(se->statistics.wait_max, |
911 | max(schedstat_val(se->statistics.wait_max), delta)); | |
912 | schedstat_inc(se->statistics.wait_count); | |
913 | schedstat_add(se->statistics.wait_sum, delta); | |
914 | schedstat_set(se->statistics.wait_start, 0); | |
3ea94de1 | 915 | } |
3ea94de1 | 916 | |
4fa8d299 | 917 | static inline void |
1a3d027c JP |
918 | update_stats_enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se) |
919 | { | |
920 | struct task_struct *tsk = NULL; | |
4fa8d299 JP |
921 | u64 sleep_start, block_start; |
922 | ||
923 | if (!schedstat_enabled()) | |
924 | return; | |
925 | ||
926 | sleep_start = schedstat_val(se->statistics.sleep_start); | |
927 | block_start = schedstat_val(se->statistics.block_start); | |
1a3d027c JP |
928 | |
929 | if (entity_is_task(se)) | |
930 | tsk = task_of(se); | |
931 | ||
4fa8d299 JP |
932 | if (sleep_start) { |
933 | u64 delta = rq_clock(rq_of(cfs_rq)) - sleep_start; | |
1a3d027c JP |
934 | |
935 | if ((s64)delta < 0) | |
936 | delta = 0; | |
937 | ||
4fa8d299 JP |
938 | if (unlikely(delta > schedstat_val(se->statistics.sleep_max))) |
939 | schedstat_set(se->statistics.sleep_max, delta); | |
1a3d027c | 940 | |
4fa8d299 JP |
941 | schedstat_set(se->statistics.sleep_start, 0); |
942 | schedstat_add(se->statistics.sum_sleep_runtime, delta); | |
1a3d027c JP |
943 | |
944 | if (tsk) { | |
945 | account_scheduler_latency(tsk, delta >> 10, 1); | |
946 | trace_sched_stat_sleep(tsk, delta); | |
947 | } | |
948 | } | |
4fa8d299 JP |
949 | if (block_start) { |
950 | u64 delta = rq_clock(rq_of(cfs_rq)) - block_start; | |
1a3d027c JP |
951 | |
952 | if ((s64)delta < 0) | |
953 | delta = 0; | |
954 | ||
4fa8d299 JP |
955 | if (unlikely(delta > schedstat_val(se->statistics.block_max))) |
956 | schedstat_set(se->statistics.block_max, delta); | |
1a3d027c | 957 | |
4fa8d299 JP |
958 | schedstat_set(se->statistics.block_start, 0); |
959 | schedstat_add(se->statistics.sum_sleep_runtime, delta); | |
1a3d027c JP |
960 | |
961 | if (tsk) { | |
962 | if (tsk->in_iowait) { | |
4fa8d299 JP |
963 | schedstat_add(se->statistics.iowait_sum, delta); |
964 | schedstat_inc(se->statistics.iowait_count); | |
1a3d027c JP |
965 | trace_sched_stat_iowait(tsk, delta); |
966 | } | |
967 | ||
968 | trace_sched_stat_blocked(tsk, delta); | |
969 | ||
970 | /* | |
971 | * Blocking time is in units of nanosecs, so shift by | |
972 | * 20 to get a milliseconds-range estimation of the | |
973 | * amount of time that the task spent sleeping: | |
974 | */ | |
975 | if (unlikely(prof_on == SLEEP_PROFILING)) { | |
976 | profile_hits(SLEEP_PROFILING, | |
977 | (void *)get_wchan(tsk), | |
978 | delta >> 20); | |
979 | } | |
980 | account_scheduler_latency(tsk, delta >> 10, 0); | |
981 | } | |
982 | } | |
3ea94de1 | 983 | } |
3ea94de1 | 984 | |
bf0f6f24 IM |
985 | /* |
986 | * Task is being enqueued - update stats: | |
987 | */ | |
cb251765 | 988 | static inline void |
1a3d027c | 989 | update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
bf0f6f24 | 990 | { |
4fa8d299 JP |
991 | if (!schedstat_enabled()) |
992 | return; | |
993 | ||
bf0f6f24 IM |
994 | /* |
995 | * Are we enqueueing a waiting task? (for current tasks | |
996 | * a dequeue/enqueue event is a NOP) | |
997 | */ | |
429d43bc | 998 | if (se != cfs_rq->curr) |
5870db5b | 999 | update_stats_wait_start(cfs_rq, se); |
1a3d027c JP |
1000 | |
1001 | if (flags & ENQUEUE_WAKEUP) | |
1002 | update_stats_enqueue_sleeper(cfs_rq, se); | |
bf0f6f24 IM |
1003 | } |
1004 | ||
bf0f6f24 | 1005 | static inline void |
cb251765 | 1006 | update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
bf0f6f24 | 1007 | { |
4fa8d299 JP |
1008 | |
1009 | if (!schedstat_enabled()) | |
1010 | return; | |
1011 | ||
bf0f6f24 IM |
1012 | /* |
1013 | * Mark the end of the wait period if dequeueing a | |
1014 | * waiting task: | |
1015 | */ | |
429d43bc | 1016 | if (se != cfs_rq->curr) |
9ef0a961 | 1017 | update_stats_wait_end(cfs_rq, se); |
cb251765 | 1018 | |
4fa8d299 JP |
1019 | if ((flags & DEQUEUE_SLEEP) && entity_is_task(se)) { |
1020 | struct task_struct *tsk = task_of(se); | |
cb251765 | 1021 | |
4fa8d299 JP |
1022 | if (tsk->state & TASK_INTERRUPTIBLE) |
1023 | schedstat_set(se->statistics.sleep_start, | |
1024 | rq_clock(rq_of(cfs_rq))); | |
1025 | if (tsk->state & TASK_UNINTERRUPTIBLE) | |
1026 | schedstat_set(se->statistics.block_start, | |
1027 | rq_clock(rq_of(cfs_rq))); | |
cb251765 | 1028 | } |
cb251765 MG |
1029 | } |
1030 | ||
bf0f6f24 IM |
1031 | /* |
1032 | * We are picking a new current task - update its stats: | |
1033 | */ | |
1034 | static inline void | |
79303e9e | 1035 | update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 IM |
1036 | { |
1037 | /* | |
1038 | * We are starting a new run period: | |
1039 | */ | |
78becc27 | 1040 | se->exec_start = rq_clock_task(rq_of(cfs_rq)); |
bf0f6f24 IM |
1041 | } |
1042 | ||
bf0f6f24 IM |
1043 | /************************************************** |
1044 | * Scheduling class queueing methods: | |
1045 | */ | |
1046 | ||
cbee9f88 PZ |
1047 | #ifdef CONFIG_NUMA_BALANCING |
1048 | /* | |
598f0ec0 MG |
1049 | * Approximate time to scan a full NUMA task in ms. The task scan period is |
1050 | * calculated based on the tasks virtual memory size and | |
1051 | * numa_balancing_scan_size. | |
cbee9f88 | 1052 | */ |
598f0ec0 MG |
1053 | unsigned int sysctl_numa_balancing_scan_period_min = 1000; |
1054 | unsigned int sysctl_numa_balancing_scan_period_max = 60000; | |
6e5fb223 PZ |
1055 | |
1056 | /* Portion of address space to scan in MB */ | |
1057 | unsigned int sysctl_numa_balancing_scan_size = 256; | |
cbee9f88 | 1058 | |
4b96a29b PZ |
1059 | /* Scan @scan_size MB every @scan_period after an initial @scan_delay in ms */ |
1060 | unsigned int sysctl_numa_balancing_scan_delay = 1000; | |
1061 | ||
b5dd77c8 RR |
1062 | struct numa_group { |
1063 | atomic_t refcount; | |
1064 | ||
1065 | spinlock_t lock; /* nr_tasks, tasks */ | |
1066 | int nr_tasks; | |
1067 | pid_t gid; | |
1068 | int active_nodes; | |
1069 | ||
1070 | struct rcu_head rcu; | |
1071 | unsigned long total_faults; | |
1072 | unsigned long max_faults_cpu; | |
1073 | /* | |
1074 | * Faults_cpu is used to decide whether memory should move | |
1075 | * towards the CPU. As a consequence, these stats are weighted | |
1076 | * more by CPU use than by memory faults. | |
1077 | */ | |
1078 | unsigned long *faults_cpu; | |
1079 | unsigned long faults[0]; | |
1080 | }; | |
1081 | ||
1082 | static inline unsigned long group_faults_priv(struct numa_group *ng); | |
1083 | static inline unsigned long group_faults_shared(struct numa_group *ng); | |
1084 | ||
598f0ec0 MG |
1085 | static unsigned int task_nr_scan_windows(struct task_struct *p) |
1086 | { | |
1087 | unsigned long rss = 0; | |
1088 | unsigned long nr_scan_pages; | |
1089 | ||
1090 | /* | |
1091 | * Calculations based on RSS as non-present and empty pages are skipped | |
1092 | * by the PTE scanner and NUMA hinting faults should be trapped based | |
1093 | * on resident pages | |
1094 | */ | |
1095 | nr_scan_pages = sysctl_numa_balancing_scan_size << (20 - PAGE_SHIFT); | |
1096 | rss = get_mm_rss(p->mm); | |
1097 | if (!rss) | |
1098 | rss = nr_scan_pages; | |
1099 | ||
1100 | rss = round_up(rss, nr_scan_pages); | |
1101 | return rss / nr_scan_pages; | |
1102 | } | |
1103 | ||
1104 | /* For sanitys sake, never scan more PTEs than MAX_SCAN_WINDOW MB/sec. */ | |
1105 | #define MAX_SCAN_WINDOW 2560 | |
1106 | ||
1107 | static unsigned int task_scan_min(struct task_struct *p) | |
1108 | { | |
316c1608 | 1109 | unsigned int scan_size = READ_ONCE(sysctl_numa_balancing_scan_size); |
598f0ec0 MG |
1110 | unsigned int scan, floor; |
1111 | unsigned int windows = 1; | |
1112 | ||
64192658 KT |
1113 | if (scan_size < MAX_SCAN_WINDOW) |
1114 | windows = MAX_SCAN_WINDOW / scan_size; | |
598f0ec0 MG |
1115 | floor = 1000 / windows; |
1116 | ||
1117 | scan = sysctl_numa_balancing_scan_period_min / task_nr_scan_windows(p); | |
1118 | return max_t(unsigned int, floor, scan); | |
1119 | } | |
1120 | ||
b5dd77c8 RR |
1121 | static unsigned int task_scan_start(struct task_struct *p) |
1122 | { | |
1123 | unsigned long smin = task_scan_min(p); | |
1124 | unsigned long period = smin; | |
1125 | ||
1126 | /* Scale the maximum scan period with the amount of shared memory. */ | |
1127 | if (p->numa_group) { | |
1128 | struct numa_group *ng = p->numa_group; | |
1129 | unsigned long shared = group_faults_shared(ng); | |
1130 | unsigned long private = group_faults_priv(ng); | |
1131 | ||
1132 | period *= atomic_read(&ng->refcount); | |
1133 | period *= shared + 1; | |
1134 | period /= private + shared + 1; | |
1135 | } | |
1136 | ||
1137 | return max(smin, period); | |
1138 | } | |
1139 | ||
598f0ec0 MG |
1140 | static unsigned int task_scan_max(struct task_struct *p) |
1141 | { | |
b5dd77c8 RR |
1142 | unsigned long smin = task_scan_min(p); |
1143 | unsigned long smax; | |
598f0ec0 MG |
1144 | |
1145 | /* Watch for min being lower than max due to floor calculations */ | |
1146 | smax = sysctl_numa_balancing_scan_period_max / task_nr_scan_windows(p); | |
b5dd77c8 RR |
1147 | |
1148 | /* Scale the maximum scan period with the amount of shared memory. */ | |
1149 | if (p->numa_group) { | |
1150 | struct numa_group *ng = p->numa_group; | |
1151 | unsigned long shared = group_faults_shared(ng); | |
1152 | unsigned long private = group_faults_priv(ng); | |
1153 | unsigned long period = smax; | |
1154 | ||
1155 | period *= atomic_read(&ng->refcount); | |
1156 | period *= shared + 1; | |
1157 | period /= private + shared + 1; | |
1158 | ||
1159 | smax = max(smax, period); | |
1160 | } | |
1161 | ||
598f0ec0 MG |
1162 | return max(smin, smax); |
1163 | } | |
1164 | ||
0ec8aa00 PZ |
1165 | static void account_numa_enqueue(struct rq *rq, struct task_struct *p) |
1166 | { | |
1167 | rq->nr_numa_running += (p->numa_preferred_nid != -1); | |
1168 | rq->nr_preferred_running += (p->numa_preferred_nid == task_node(p)); | |
1169 | } | |
1170 | ||
1171 | static void account_numa_dequeue(struct rq *rq, struct task_struct *p) | |
1172 | { | |
1173 | rq->nr_numa_running -= (p->numa_preferred_nid != -1); | |
1174 | rq->nr_preferred_running -= (p->numa_preferred_nid == task_node(p)); | |
1175 | } | |
1176 | ||
be1e4e76 RR |
1177 | /* Shared or private faults. */ |
1178 | #define NR_NUMA_HINT_FAULT_TYPES 2 | |
1179 | ||
1180 | /* Memory and CPU locality */ | |
1181 | #define NR_NUMA_HINT_FAULT_STATS (NR_NUMA_HINT_FAULT_TYPES * 2) | |
1182 | ||
1183 | /* Averaged statistics, and temporary buffers. */ | |
1184 | #define NR_NUMA_HINT_FAULT_BUCKETS (NR_NUMA_HINT_FAULT_STATS * 2) | |
1185 | ||
e29cf08b MG |
1186 | pid_t task_numa_group_id(struct task_struct *p) |
1187 | { | |
1188 | return p->numa_group ? p->numa_group->gid : 0; | |
1189 | } | |
1190 | ||
44dba3d5 IM |
1191 | /* |
1192 | * The averaged statistics, shared & private, memory & cpu, | |
1193 | * occupy the first half of the array. The second half of the | |
1194 | * array is for current counters, which are averaged into the | |
1195 | * first set by task_numa_placement. | |
1196 | */ | |
1197 | static inline int task_faults_idx(enum numa_faults_stats s, int nid, int priv) | |
ac8e895b | 1198 | { |
44dba3d5 | 1199 | return NR_NUMA_HINT_FAULT_TYPES * (s * nr_node_ids + nid) + priv; |
ac8e895b MG |
1200 | } |
1201 | ||
1202 | static inline unsigned long task_faults(struct task_struct *p, int nid) | |
1203 | { | |
44dba3d5 | 1204 | if (!p->numa_faults) |
ac8e895b MG |
1205 | return 0; |
1206 | ||
44dba3d5 IM |
1207 | return p->numa_faults[task_faults_idx(NUMA_MEM, nid, 0)] + |
1208 | p->numa_faults[task_faults_idx(NUMA_MEM, nid, 1)]; | |
ac8e895b MG |
1209 | } |
1210 | ||
83e1d2cd MG |
1211 | static inline unsigned long group_faults(struct task_struct *p, int nid) |
1212 | { | |
1213 | if (!p->numa_group) | |
1214 | return 0; | |
1215 | ||
44dba3d5 IM |
1216 | return p->numa_group->faults[task_faults_idx(NUMA_MEM, nid, 0)] + |
1217 | p->numa_group->faults[task_faults_idx(NUMA_MEM, nid, 1)]; | |
83e1d2cd MG |
1218 | } |
1219 | ||
20e07dea RR |
1220 | static inline unsigned long group_faults_cpu(struct numa_group *group, int nid) |
1221 | { | |
44dba3d5 IM |
1222 | return group->faults_cpu[task_faults_idx(NUMA_MEM, nid, 0)] + |
1223 | group->faults_cpu[task_faults_idx(NUMA_MEM, nid, 1)]; | |
20e07dea RR |
1224 | } |
1225 | ||
b5dd77c8 RR |
1226 | static inline unsigned long group_faults_priv(struct numa_group *ng) |
1227 | { | |
1228 | unsigned long faults = 0; | |
1229 | int node; | |
1230 | ||
1231 | for_each_online_node(node) { | |
1232 | faults += ng->faults[task_faults_idx(NUMA_MEM, node, 1)]; | |
1233 | } | |
1234 | ||
1235 | return faults; | |
1236 | } | |
1237 | ||
1238 | static inline unsigned long group_faults_shared(struct numa_group *ng) | |
1239 | { | |
1240 | unsigned long faults = 0; | |
1241 | int node; | |
1242 | ||
1243 | for_each_online_node(node) { | |
1244 | faults += ng->faults[task_faults_idx(NUMA_MEM, node, 0)]; | |
1245 | } | |
1246 | ||
1247 | return faults; | |
1248 | } | |
1249 | ||
4142c3eb RR |
1250 | /* |
1251 | * A node triggering more than 1/3 as many NUMA faults as the maximum is | |
1252 | * considered part of a numa group's pseudo-interleaving set. Migrations | |
1253 | * between these nodes are slowed down, to allow things to settle down. | |
1254 | */ | |
1255 | #define ACTIVE_NODE_FRACTION 3 | |
1256 | ||
1257 | static bool numa_is_active_node(int nid, struct numa_group *ng) | |
1258 | { | |
1259 | return group_faults_cpu(ng, nid) * ACTIVE_NODE_FRACTION > ng->max_faults_cpu; | |
1260 | } | |
1261 | ||
6c6b1193 RR |
1262 | /* Handle placement on systems where not all nodes are directly connected. */ |
1263 | static unsigned long score_nearby_nodes(struct task_struct *p, int nid, | |
1264 | int maxdist, bool task) | |
1265 | { | |
1266 | unsigned long score = 0; | |
1267 | int node; | |
1268 | ||
1269 | /* | |
1270 | * All nodes are directly connected, and the same distance | |
1271 | * from each other. No need for fancy placement algorithms. | |
1272 | */ | |
1273 | if (sched_numa_topology_type == NUMA_DIRECT) | |
1274 | return 0; | |
1275 | ||
1276 | /* | |
1277 | * This code is called for each node, introducing N^2 complexity, | |
1278 | * which should be ok given the number of nodes rarely exceeds 8. | |
1279 | */ | |
1280 | for_each_online_node(node) { | |
1281 | unsigned long faults; | |
1282 | int dist = node_distance(nid, node); | |
1283 | ||
1284 | /* | |
1285 | * The furthest away nodes in the system are not interesting | |
1286 | * for placement; nid was already counted. | |
1287 | */ | |
1288 | if (dist == sched_max_numa_distance || node == nid) | |
1289 | continue; | |
1290 | ||
1291 | /* | |
1292 | * On systems with a backplane NUMA topology, compare groups | |
1293 | * of nodes, and move tasks towards the group with the most | |
1294 | * memory accesses. When comparing two nodes at distance | |
1295 | * "hoplimit", only nodes closer by than "hoplimit" are part | |
1296 | * of each group. Skip other nodes. | |
1297 | */ | |
1298 | if (sched_numa_topology_type == NUMA_BACKPLANE && | |
1299 | dist > maxdist) | |
1300 | continue; | |
1301 | ||
1302 | /* Add up the faults from nearby nodes. */ | |
1303 | if (task) | |
1304 | faults = task_faults(p, node); | |
1305 | else | |
1306 | faults = group_faults(p, node); | |
1307 | ||
1308 | /* | |
1309 | * On systems with a glueless mesh NUMA topology, there are | |
1310 | * no fixed "groups of nodes". Instead, nodes that are not | |
1311 | * directly connected bounce traffic through intermediate | |
1312 | * nodes; a numa_group can occupy any set of nodes. | |
1313 | * The further away a node is, the less the faults count. | |
1314 | * This seems to result in good task placement. | |
1315 | */ | |
1316 | if (sched_numa_topology_type == NUMA_GLUELESS_MESH) { | |
1317 | faults *= (sched_max_numa_distance - dist); | |
1318 | faults /= (sched_max_numa_distance - LOCAL_DISTANCE); | |
1319 | } | |
1320 | ||
1321 | score += faults; | |
1322 | } | |
1323 | ||
1324 | return score; | |
1325 | } | |
1326 | ||
83e1d2cd MG |
1327 | /* |
1328 | * These return the fraction of accesses done by a particular task, or | |
1329 | * task group, on a particular numa node. The group weight is given a | |
1330 | * larger multiplier, in order to group tasks together that are almost | |
1331 | * evenly spread out between numa nodes. | |
1332 | */ | |
7bd95320 RR |
1333 | static inline unsigned long task_weight(struct task_struct *p, int nid, |
1334 | int dist) | |
83e1d2cd | 1335 | { |
7bd95320 | 1336 | unsigned long faults, total_faults; |
83e1d2cd | 1337 | |
44dba3d5 | 1338 | if (!p->numa_faults) |
83e1d2cd MG |
1339 | return 0; |
1340 | ||
1341 | total_faults = p->total_numa_faults; | |
1342 | ||
1343 | if (!total_faults) | |
1344 | return 0; | |
1345 | ||
7bd95320 | 1346 | faults = task_faults(p, nid); |
6c6b1193 RR |
1347 | faults += score_nearby_nodes(p, nid, dist, true); |
1348 | ||
7bd95320 | 1349 | return 1000 * faults / total_faults; |
83e1d2cd MG |
1350 | } |
1351 | ||
7bd95320 RR |
1352 | static inline unsigned long group_weight(struct task_struct *p, int nid, |
1353 | int dist) | |
83e1d2cd | 1354 | { |
7bd95320 RR |
1355 | unsigned long faults, total_faults; |
1356 | ||
1357 | if (!p->numa_group) | |
1358 | return 0; | |
1359 | ||
1360 | total_faults = p->numa_group->total_faults; | |
1361 | ||
1362 | if (!total_faults) | |
83e1d2cd MG |
1363 | return 0; |
1364 | ||
7bd95320 | 1365 | faults = group_faults(p, nid); |
6c6b1193 RR |
1366 | faults += score_nearby_nodes(p, nid, dist, false); |
1367 | ||
7bd95320 | 1368 | return 1000 * faults / total_faults; |
83e1d2cd MG |
1369 | } |
1370 | ||
10f39042 RR |
1371 | bool should_numa_migrate_memory(struct task_struct *p, struct page * page, |
1372 | int src_nid, int dst_cpu) | |
1373 | { | |
1374 | struct numa_group *ng = p->numa_group; | |
1375 | int dst_nid = cpu_to_node(dst_cpu); | |
1376 | int last_cpupid, this_cpupid; | |
1377 | ||
1378 | this_cpupid = cpu_pid_to_cpupid(dst_cpu, current->pid); | |
1379 | ||
1380 | /* | |
1381 | * Multi-stage node selection is used in conjunction with a periodic | |
1382 | * migration fault to build a temporal task<->page relation. By using | |
1383 | * a two-stage filter we remove short/unlikely relations. | |
1384 | * | |
1385 | * Using P(p) ~ n_p / n_t as per frequentist probability, we can equate | |
1386 | * a task's usage of a particular page (n_p) per total usage of this | |
1387 | * page (n_t) (in a given time-span) to a probability. | |
1388 | * | |
1389 | * Our periodic faults will sample this probability and getting the | |
1390 | * same result twice in a row, given these samples are fully | |
1391 | * independent, is then given by P(n)^2, provided our sample period | |
1392 | * is sufficiently short compared to the usage pattern. | |
1393 | * | |
1394 | * This quadric squishes small probabilities, making it less likely we | |
1395 | * act on an unlikely task<->page relation. | |
1396 | */ | |
1397 | last_cpupid = page_cpupid_xchg_last(page, this_cpupid); | |
1398 | if (!cpupid_pid_unset(last_cpupid) && | |
1399 | cpupid_to_nid(last_cpupid) != dst_nid) | |
1400 | return false; | |
1401 | ||
1402 | /* Always allow migrate on private faults */ | |
1403 | if (cpupid_match_pid(p, last_cpupid)) | |
1404 | return true; | |
1405 | ||
1406 | /* A shared fault, but p->numa_group has not been set up yet. */ | |
1407 | if (!ng) | |
1408 | return true; | |
1409 | ||
1410 | /* | |
4142c3eb RR |
1411 | * Destination node is much more heavily used than the source |
1412 | * node? Allow migration. | |
10f39042 | 1413 | */ |
4142c3eb RR |
1414 | if (group_faults_cpu(ng, dst_nid) > group_faults_cpu(ng, src_nid) * |
1415 | ACTIVE_NODE_FRACTION) | |
10f39042 RR |
1416 | return true; |
1417 | ||
1418 | /* | |
4142c3eb RR |
1419 | * Distribute memory according to CPU & memory use on each node, |
1420 | * with 3/4 hysteresis to avoid unnecessary memory migrations: | |
1421 | * | |
1422 | * faults_cpu(dst) 3 faults_cpu(src) | |
1423 | * --------------- * - > --------------- | |
1424 | * faults_mem(dst) 4 faults_mem(src) | |
10f39042 | 1425 | */ |
4142c3eb RR |
1426 | return group_faults_cpu(ng, dst_nid) * group_faults(p, src_nid) * 3 > |
1427 | group_faults_cpu(ng, src_nid) * group_faults(p, dst_nid) * 4; | |
10f39042 RR |
1428 | } |
1429 | ||
c7132dd6 | 1430 | static unsigned long weighted_cpuload(struct rq *rq); |
58d081b5 MG |
1431 | static unsigned long source_load(int cpu, int type); |
1432 | static unsigned long target_load(int cpu, int type); | |
ced549fa | 1433 | static unsigned long capacity_of(int cpu); |
58d081b5 | 1434 | |
fb13c7ee | 1435 | /* Cached statistics for all CPUs within a node */ |
58d081b5 | 1436 | struct numa_stats { |
fb13c7ee | 1437 | unsigned long nr_running; |
58d081b5 | 1438 | unsigned long load; |
fb13c7ee MG |
1439 | |
1440 | /* Total compute capacity of CPUs on a node */ | |
5ef20ca1 | 1441 | unsigned long compute_capacity; |
fb13c7ee MG |
1442 | |
1443 | /* Approximate capacity in terms of runnable tasks on a node */ | |
5ef20ca1 | 1444 | unsigned long task_capacity; |
1b6a7495 | 1445 | int has_free_capacity; |
58d081b5 | 1446 | }; |
e6628d5b | 1447 | |
fb13c7ee MG |
1448 | /* |
1449 | * XXX borrowed from update_sg_lb_stats | |
1450 | */ | |
1451 | static void update_numa_stats(struct numa_stats *ns, int nid) | |
1452 | { | |
83d7f242 RR |
1453 | int smt, cpu, cpus = 0; |
1454 | unsigned long capacity; | |
fb13c7ee MG |
1455 | |
1456 | memset(ns, 0, sizeof(*ns)); | |
1457 | for_each_cpu(cpu, cpumask_of_node(nid)) { | |
1458 | struct rq *rq = cpu_rq(cpu); | |
1459 | ||
1460 | ns->nr_running += rq->nr_running; | |
c7132dd6 | 1461 | ns->load += weighted_cpuload(rq); |
ced549fa | 1462 | ns->compute_capacity += capacity_of(cpu); |
5eca82a9 PZ |
1463 | |
1464 | cpus++; | |
fb13c7ee MG |
1465 | } |
1466 | ||
5eca82a9 PZ |
1467 | /* |
1468 | * If we raced with hotplug and there are no CPUs left in our mask | |
1469 | * the @ns structure is NULL'ed and task_numa_compare() will | |
1470 | * not find this node attractive. | |
1471 | * | |
1b6a7495 NP |
1472 | * We'll either bail at !has_free_capacity, or we'll detect a huge |
1473 | * imbalance and bail there. | |
5eca82a9 PZ |
1474 | */ |
1475 | if (!cpus) | |
1476 | return; | |
1477 | ||
83d7f242 RR |
1478 | /* smt := ceil(cpus / capacity), assumes: 1 < smt_power < 2 */ |
1479 | smt = DIV_ROUND_UP(SCHED_CAPACITY_SCALE * cpus, ns->compute_capacity); | |
1480 | capacity = cpus / smt; /* cores */ | |
1481 | ||
1482 | ns->task_capacity = min_t(unsigned, capacity, | |
1483 | DIV_ROUND_CLOSEST(ns->compute_capacity, SCHED_CAPACITY_SCALE)); | |
1b6a7495 | 1484 | ns->has_free_capacity = (ns->nr_running < ns->task_capacity); |
fb13c7ee MG |
1485 | } |
1486 | ||
58d081b5 MG |
1487 | struct task_numa_env { |
1488 | struct task_struct *p; | |
e6628d5b | 1489 | |
58d081b5 MG |
1490 | int src_cpu, src_nid; |
1491 | int dst_cpu, dst_nid; | |
e6628d5b | 1492 | |
58d081b5 | 1493 | struct numa_stats src_stats, dst_stats; |
e6628d5b | 1494 | |
40ea2b42 | 1495 | int imbalance_pct; |
7bd95320 | 1496 | int dist; |
fb13c7ee MG |
1497 | |
1498 | struct task_struct *best_task; | |
1499 | long best_imp; | |
58d081b5 MG |
1500 | int best_cpu; |
1501 | }; | |
1502 | ||
fb13c7ee MG |
1503 | static void task_numa_assign(struct task_numa_env *env, |
1504 | struct task_struct *p, long imp) | |
1505 | { | |
1506 | if (env->best_task) | |
1507 | put_task_struct(env->best_task); | |
bac78573 ON |
1508 | if (p) |
1509 | get_task_struct(p); | |
fb13c7ee MG |
1510 | |
1511 | env->best_task = p; | |
1512 | env->best_imp = imp; | |
1513 | env->best_cpu = env->dst_cpu; | |
1514 | } | |
1515 | ||
28a21745 | 1516 | static bool load_too_imbalanced(long src_load, long dst_load, |
e63da036 RR |
1517 | struct task_numa_env *env) |
1518 | { | |
e4991b24 RR |
1519 | long imb, old_imb; |
1520 | long orig_src_load, orig_dst_load; | |
28a21745 RR |
1521 | long src_capacity, dst_capacity; |
1522 | ||
1523 | /* | |
1524 | * The load is corrected for the CPU capacity available on each node. | |
1525 | * | |
1526 | * src_load dst_load | |
1527 | * ------------ vs --------- | |
1528 | * src_capacity dst_capacity | |
1529 | */ | |
1530 | src_capacity = env->src_stats.compute_capacity; | |
1531 | dst_capacity = env->dst_stats.compute_capacity; | |
e63da036 RR |
1532 | |
1533 | /* We care about the slope of the imbalance, not the direction. */ | |
e4991b24 RR |
1534 | if (dst_load < src_load) |
1535 | swap(dst_load, src_load); | |
e63da036 RR |
1536 | |
1537 | /* Is the difference below the threshold? */ | |
e4991b24 RR |
1538 | imb = dst_load * src_capacity * 100 - |
1539 | src_load * dst_capacity * env->imbalance_pct; | |
e63da036 RR |
1540 | if (imb <= 0) |
1541 | return false; | |
1542 | ||
1543 | /* | |
1544 | * The imbalance is above the allowed threshold. | |
e4991b24 | 1545 | * Compare it with the old imbalance. |
e63da036 | 1546 | */ |
28a21745 | 1547 | orig_src_load = env->src_stats.load; |
e4991b24 | 1548 | orig_dst_load = env->dst_stats.load; |
28a21745 | 1549 | |
e4991b24 RR |
1550 | if (orig_dst_load < orig_src_load) |
1551 | swap(orig_dst_load, orig_src_load); | |
e63da036 | 1552 | |
e4991b24 RR |
1553 | old_imb = orig_dst_load * src_capacity * 100 - |
1554 | orig_src_load * dst_capacity * env->imbalance_pct; | |
1555 | ||
1556 | /* Would this change make things worse? */ | |
1557 | return (imb > old_imb); | |
e63da036 RR |
1558 | } |
1559 | ||
fb13c7ee MG |
1560 | /* |
1561 | * This checks if the overall compute and NUMA accesses of the system would | |
1562 | * be improved if the source tasks was migrated to the target dst_cpu taking | |
1563 | * into account that it might be best if task running on the dst_cpu should | |
1564 | * be exchanged with the source task | |
1565 | */ | |
887c290e RR |
1566 | static void task_numa_compare(struct task_numa_env *env, |
1567 | long taskimp, long groupimp) | |
fb13c7ee MG |
1568 | { |
1569 | struct rq *src_rq = cpu_rq(env->src_cpu); | |
1570 | struct rq *dst_rq = cpu_rq(env->dst_cpu); | |
1571 | struct task_struct *cur; | |
28a21745 | 1572 | long src_load, dst_load; |
fb13c7ee | 1573 | long load; |
1c5d3eb3 | 1574 | long imp = env->p->numa_group ? groupimp : taskimp; |
0132c3e1 | 1575 | long moveimp = imp; |
7bd95320 | 1576 | int dist = env->dist; |
fb13c7ee MG |
1577 | |
1578 | rcu_read_lock(); | |
bac78573 ON |
1579 | cur = task_rcu_dereference(&dst_rq->curr); |
1580 | if (cur && ((cur->flags & PF_EXITING) || is_idle_task(cur))) | |
fb13c7ee MG |
1581 | cur = NULL; |
1582 | ||
7af68335 PZ |
1583 | /* |
1584 | * Because we have preemption enabled we can get migrated around and | |
1585 | * end try selecting ourselves (current == env->p) as a swap candidate. | |
1586 | */ | |
1587 | if (cur == env->p) | |
1588 | goto unlock; | |
1589 | ||
fb13c7ee MG |
1590 | /* |
1591 | * "imp" is the fault differential for the source task between the | |
1592 | * source and destination node. Calculate the total differential for | |
1593 | * the source task and potential destination task. The more negative | |
1594 | * the value is, the more rmeote accesses that would be expected to | |
1595 | * be incurred if the tasks were swapped. | |
1596 | */ | |
1597 | if (cur) { | |
1598 | /* Skip this swap candidate if cannot move to the source cpu */ | |
0c98d344 | 1599 | if (!cpumask_test_cpu(env->src_cpu, &cur->cpus_allowed)) |
fb13c7ee MG |
1600 | goto unlock; |
1601 | ||
887c290e RR |
1602 | /* |
1603 | * If dst and source tasks are in the same NUMA group, or not | |
ca28aa53 | 1604 | * in any group then look only at task weights. |
887c290e | 1605 | */ |
ca28aa53 | 1606 | if (cur->numa_group == env->p->numa_group) { |
7bd95320 RR |
1607 | imp = taskimp + task_weight(cur, env->src_nid, dist) - |
1608 | task_weight(cur, env->dst_nid, dist); | |
ca28aa53 RR |
1609 | /* |
1610 | * Add some hysteresis to prevent swapping the | |
1611 | * tasks within a group over tiny differences. | |
1612 | */ | |
1613 | if (cur->numa_group) | |
1614 | imp -= imp/16; | |
887c290e | 1615 | } else { |
ca28aa53 RR |
1616 | /* |
1617 | * Compare the group weights. If a task is all by | |
1618 | * itself (not part of a group), use the task weight | |
1619 | * instead. | |
1620 | */ | |
ca28aa53 | 1621 | if (cur->numa_group) |
7bd95320 RR |
1622 | imp += group_weight(cur, env->src_nid, dist) - |
1623 | group_weight(cur, env->dst_nid, dist); | |
ca28aa53 | 1624 | else |
7bd95320 RR |
1625 | imp += task_weight(cur, env->src_nid, dist) - |
1626 | task_weight(cur, env->dst_nid, dist); | |
887c290e | 1627 | } |
fb13c7ee MG |
1628 | } |
1629 | ||
0132c3e1 | 1630 | if (imp <= env->best_imp && moveimp <= env->best_imp) |
fb13c7ee MG |
1631 | goto unlock; |
1632 | ||
1633 | if (!cur) { | |
1634 | /* Is there capacity at our destination? */ | |
b932c03c | 1635 | if (env->src_stats.nr_running <= env->src_stats.task_capacity && |
1b6a7495 | 1636 | !env->dst_stats.has_free_capacity) |
fb13c7ee MG |
1637 | goto unlock; |
1638 | ||
1639 | goto balance; | |
1640 | } | |
1641 | ||
1642 | /* Balance doesn't matter much if we're running a task per cpu */ | |
0132c3e1 RR |
1643 | if (imp > env->best_imp && src_rq->nr_running == 1 && |
1644 | dst_rq->nr_running == 1) | |
fb13c7ee MG |
1645 | goto assign; |
1646 | ||
1647 | /* | |
1648 | * In the overloaded case, try and keep the load balanced. | |
1649 | */ | |
1650 | balance: | |
e720fff6 PZ |
1651 | load = task_h_load(env->p); |
1652 | dst_load = env->dst_stats.load + load; | |
1653 | src_load = env->src_stats.load - load; | |
fb13c7ee | 1654 | |
0132c3e1 RR |
1655 | if (moveimp > imp && moveimp > env->best_imp) { |
1656 | /* | |
1657 | * If the improvement from just moving env->p direction is | |
1658 | * better than swapping tasks around, check if a move is | |
1659 | * possible. Store a slightly smaller score than moveimp, | |
1660 | * so an actually idle CPU will win. | |
1661 | */ | |
1662 | if (!load_too_imbalanced(src_load, dst_load, env)) { | |
1663 | imp = moveimp - 1; | |
1664 | cur = NULL; | |
1665 | goto assign; | |
1666 | } | |
1667 | } | |
1668 | ||
1669 | if (imp <= env->best_imp) | |
1670 | goto unlock; | |
1671 | ||
fb13c7ee | 1672 | if (cur) { |
e720fff6 PZ |
1673 | load = task_h_load(cur); |
1674 | dst_load -= load; | |
1675 | src_load += load; | |
fb13c7ee MG |
1676 | } |
1677 | ||
28a21745 | 1678 | if (load_too_imbalanced(src_load, dst_load, env)) |
fb13c7ee MG |
1679 | goto unlock; |
1680 | ||
ba7e5a27 RR |
1681 | /* |
1682 | * One idle CPU per node is evaluated for a task numa move. | |
1683 | * Call select_idle_sibling to maybe find a better one. | |
1684 | */ | |
10e2f1ac PZ |
1685 | if (!cur) { |
1686 | /* | |
1687 | * select_idle_siblings() uses an per-cpu cpumask that | |
1688 | * can be used from IRQ context. | |
1689 | */ | |
1690 | local_irq_disable(); | |
772bd008 MR |
1691 | env->dst_cpu = select_idle_sibling(env->p, env->src_cpu, |
1692 | env->dst_cpu); | |
10e2f1ac PZ |
1693 | local_irq_enable(); |
1694 | } | |
ba7e5a27 | 1695 | |
fb13c7ee MG |
1696 | assign: |
1697 | task_numa_assign(env, cur, imp); | |
1698 | unlock: | |
1699 | rcu_read_unlock(); | |
1700 | } | |
1701 | ||
887c290e RR |
1702 | static void task_numa_find_cpu(struct task_numa_env *env, |
1703 | long taskimp, long groupimp) | |
2c8a50aa MG |
1704 | { |
1705 | int cpu; | |
1706 | ||
1707 | for_each_cpu(cpu, cpumask_of_node(env->dst_nid)) { | |
1708 | /* Skip this CPU if the source task cannot migrate */ | |
0c98d344 | 1709 | if (!cpumask_test_cpu(cpu, &env->p->cpus_allowed)) |
2c8a50aa MG |
1710 | continue; |
1711 | ||
1712 | env->dst_cpu = cpu; | |
887c290e | 1713 | task_numa_compare(env, taskimp, groupimp); |
2c8a50aa MG |
1714 | } |
1715 | } | |
1716 | ||
6f9aad0b RR |
1717 | /* Only move tasks to a NUMA node less busy than the current node. */ |
1718 | static bool numa_has_capacity(struct task_numa_env *env) | |
1719 | { | |
1720 | struct numa_stats *src = &env->src_stats; | |
1721 | struct numa_stats *dst = &env->dst_stats; | |
1722 | ||
1723 | if (src->has_free_capacity && !dst->has_free_capacity) | |
1724 | return false; | |
1725 | ||
1726 | /* | |
1727 | * Only consider a task move if the source has a higher load | |
1728 | * than the destination, corrected for CPU capacity on each node. | |
1729 | * | |
1730 | * src->load dst->load | |
1731 | * --------------------- vs --------------------- | |
1732 | * src->compute_capacity dst->compute_capacity | |
1733 | */ | |
44dcb04f SD |
1734 | if (src->load * dst->compute_capacity * env->imbalance_pct > |
1735 | ||
1736 | dst->load * src->compute_capacity * 100) | |
6f9aad0b RR |
1737 | return true; |
1738 | ||
1739 | return false; | |
1740 | } | |
1741 | ||
58d081b5 MG |
1742 | static int task_numa_migrate(struct task_struct *p) |
1743 | { | |
58d081b5 MG |
1744 | struct task_numa_env env = { |
1745 | .p = p, | |
fb13c7ee | 1746 | |
58d081b5 | 1747 | .src_cpu = task_cpu(p), |
b32e86b4 | 1748 | .src_nid = task_node(p), |
fb13c7ee MG |
1749 | |
1750 | .imbalance_pct = 112, | |
1751 | ||
1752 | .best_task = NULL, | |
1753 | .best_imp = 0, | |
4142c3eb | 1754 | .best_cpu = -1, |
58d081b5 MG |
1755 | }; |
1756 | struct sched_domain *sd; | |
887c290e | 1757 | unsigned long taskweight, groupweight; |
7bd95320 | 1758 | int nid, ret, dist; |
887c290e | 1759 | long taskimp, groupimp; |
e6628d5b | 1760 | |
58d081b5 | 1761 | /* |
fb13c7ee MG |
1762 | * Pick the lowest SD_NUMA domain, as that would have the smallest |
1763 | * imbalance and would be the first to start moving tasks about. | |
1764 | * | |
1765 | * And we want to avoid any moving of tasks about, as that would create | |
1766 | * random movement of tasks -- counter the numa conditions we're trying | |
1767 | * to satisfy here. | |
58d081b5 MG |
1768 | */ |
1769 | rcu_read_lock(); | |
fb13c7ee | 1770 | sd = rcu_dereference(per_cpu(sd_numa, env.src_cpu)); |
46a73e8a RR |
1771 | if (sd) |
1772 | env.imbalance_pct = 100 + (sd->imbalance_pct - 100) / 2; | |
e6628d5b MG |
1773 | rcu_read_unlock(); |
1774 | ||
46a73e8a RR |
1775 | /* |
1776 | * Cpusets can break the scheduler domain tree into smaller | |
1777 | * balance domains, some of which do not cross NUMA boundaries. | |
1778 | * Tasks that are "trapped" in such domains cannot be migrated | |
1779 | * elsewhere, so there is no point in (re)trying. | |
1780 | */ | |
1781 | if (unlikely(!sd)) { | |
de1b301a | 1782 | p->numa_preferred_nid = task_node(p); |
46a73e8a RR |
1783 | return -EINVAL; |
1784 | } | |
1785 | ||
2c8a50aa | 1786 | env.dst_nid = p->numa_preferred_nid; |
7bd95320 RR |
1787 | dist = env.dist = node_distance(env.src_nid, env.dst_nid); |
1788 | taskweight = task_weight(p, env.src_nid, dist); | |
1789 | groupweight = group_weight(p, env.src_nid, dist); | |
1790 | update_numa_stats(&env.src_stats, env.src_nid); | |
1791 | taskimp = task_weight(p, env.dst_nid, dist) - taskweight; | |
1792 | groupimp = group_weight(p, env.dst_nid, dist) - groupweight; | |
2c8a50aa | 1793 | update_numa_stats(&env.dst_stats, env.dst_nid); |
58d081b5 | 1794 | |
a43455a1 | 1795 | /* Try to find a spot on the preferred nid. */ |
6f9aad0b RR |
1796 | if (numa_has_capacity(&env)) |
1797 | task_numa_find_cpu(&env, taskimp, groupimp); | |
e1dda8a7 | 1798 | |
9de05d48 RR |
1799 | /* |
1800 | * Look at other nodes in these cases: | |
1801 | * - there is no space available on the preferred_nid | |
1802 | * - the task is part of a numa_group that is interleaved across | |
1803 | * multiple NUMA nodes; in order to better consolidate the group, | |
1804 | * we need to check other locations. | |
1805 | */ | |
4142c3eb | 1806 | if (env.best_cpu == -1 || (p->numa_group && p->numa_group->active_nodes > 1)) { |
2c8a50aa MG |
1807 | for_each_online_node(nid) { |
1808 | if (nid == env.src_nid || nid == p->numa_preferred_nid) | |
1809 | continue; | |
58d081b5 | 1810 | |
7bd95320 | 1811 | dist = node_distance(env.src_nid, env.dst_nid); |
6c6b1193 RR |
1812 | if (sched_numa_topology_type == NUMA_BACKPLANE && |
1813 | dist != env.dist) { | |
1814 | taskweight = task_weight(p, env.src_nid, dist); | |
1815 | groupweight = group_weight(p, env.src_nid, dist); | |
1816 | } | |
7bd95320 | 1817 | |
83e1d2cd | 1818 | /* Only consider nodes where both task and groups benefit */ |
7bd95320 RR |
1819 | taskimp = task_weight(p, nid, dist) - taskweight; |
1820 | groupimp = group_weight(p, nid, dist) - groupweight; | |
887c290e | 1821 | if (taskimp < 0 && groupimp < 0) |
fb13c7ee MG |
1822 | continue; |
1823 | ||
7bd95320 | 1824 | env.dist = dist; |
2c8a50aa MG |
1825 | env.dst_nid = nid; |
1826 | update_numa_stats(&env.dst_stats, env.dst_nid); | |
6f9aad0b RR |
1827 | if (numa_has_capacity(&env)) |
1828 | task_numa_find_cpu(&env, taskimp, groupimp); | |
58d081b5 MG |
1829 | } |
1830 | } | |
1831 | ||
68d1b02a RR |
1832 | /* |
1833 | * If the task is part of a workload that spans multiple NUMA nodes, | |
1834 | * and is migrating into one of the workload's active nodes, remember | |
1835 | * this node as the task's preferred numa node, so the workload can | |
1836 | * settle down. | |
1837 | * A task that migrated to a second choice node will be better off | |
1838 | * trying for a better one later. Do not set the preferred node here. | |
1839 | */ | |
db015dae | 1840 | if (p->numa_group) { |
4142c3eb RR |
1841 | struct numa_group *ng = p->numa_group; |
1842 | ||
db015dae RR |
1843 | if (env.best_cpu == -1) |
1844 | nid = env.src_nid; | |
1845 | else | |
1846 | nid = env.dst_nid; | |
1847 | ||
4142c3eb | 1848 | if (ng->active_nodes > 1 && numa_is_active_node(env.dst_nid, ng)) |
db015dae RR |
1849 | sched_setnuma(p, env.dst_nid); |
1850 | } | |
1851 | ||
1852 | /* No better CPU than the current one was found. */ | |
1853 | if (env.best_cpu == -1) | |
1854 | return -EAGAIN; | |
0ec8aa00 | 1855 | |
04bb2f94 RR |
1856 | /* |
1857 | * Reset the scan period if the task is being rescheduled on an | |
1858 | * alternative node to recheck if the tasks is now properly placed. | |
1859 | */ | |
b5dd77c8 | 1860 | p->numa_scan_period = task_scan_start(p); |
04bb2f94 | 1861 | |
fb13c7ee | 1862 | if (env.best_task == NULL) { |
286549dc MG |
1863 | ret = migrate_task_to(p, env.best_cpu); |
1864 | if (ret != 0) | |
1865 | trace_sched_stick_numa(p, env.src_cpu, env.best_cpu); | |
fb13c7ee MG |
1866 | return ret; |
1867 | } | |
1868 | ||
1869 | ret = migrate_swap(p, env.best_task); | |
286549dc MG |
1870 | if (ret != 0) |
1871 | trace_sched_stick_numa(p, env.src_cpu, task_cpu(env.best_task)); | |
fb13c7ee MG |
1872 | put_task_struct(env.best_task); |
1873 | return ret; | |
e6628d5b MG |
1874 | } |
1875 | ||
6b9a7460 MG |
1876 | /* Attempt to migrate a task to a CPU on the preferred node. */ |
1877 | static void numa_migrate_preferred(struct task_struct *p) | |
1878 | { | |
5085e2a3 RR |
1879 | unsigned long interval = HZ; |
1880 | ||
2739d3ee | 1881 | /* This task has no NUMA fault statistics yet */ |
44dba3d5 | 1882 | if (unlikely(p->numa_preferred_nid == -1 || !p->numa_faults)) |
6b9a7460 MG |
1883 | return; |
1884 | ||
2739d3ee | 1885 | /* Periodically retry migrating the task to the preferred node */ |
5085e2a3 RR |
1886 | interval = min(interval, msecs_to_jiffies(p->numa_scan_period) / 16); |
1887 | p->numa_migrate_retry = jiffies + interval; | |
2739d3ee RR |
1888 | |
1889 | /* Success if task is already running on preferred CPU */ | |
de1b301a | 1890 | if (task_node(p) == p->numa_preferred_nid) |
6b9a7460 MG |
1891 | return; |
1892 | ||
1893 | /* Otherwise, try migrate to a CPU on the preferred node */ | |
2739d3ee | 1894 | task_numa_migrate(p); |
6b9a7460 MG |
1895 | } |
1896 | ||
20e07dea | 1897 | /* |
4142c3eb | 1898 | * Find out how many nodes on the workload is actively running on. Do this by |
20e07dea RR |
1899 | * tracking the nodes from which NUMA hinting faults are triggered. This can |
1900 | * be different from the set of nodes where the workload's memory is currently | |
1901 | * located. | |
20e07dea | 1902 | */ |
4142c3eb | 1903 | static void numa_group_count_active_nodes(struct numa_group *numa_group) |
20e07dea RR |
1904 | { |
1905 | unsigned long faults, max_faults = 0; | |
4142c3eb | 1906 | int nid, active_nodes = 0; |
20e07dea RR |
1907 | |
1908 | for_each_online_node(nid) { | |
1909 | faults = group_faults_cpu(numa_group, nid); | |
1910 | if (faults > max_faults) | |
1911 | max_faults = faults; | |
1912 | } | |
1913 | ||
1914 | for_each_online_node(nid) { | |
1915 | faults = group_faults_cpu(numa_group, nid); | |
4142c3eb RR |
1916 | if (faults * ACTIVE_NODE_FRACTION > max_faults) |
1917 | active_nodes++; | |
20e07dea | 1918 | } |
4142c3eb RR |
1919 | |
1920 | numa_group->max_faults_cpu = max_faults; | |
1921 | numa_group->active_nodes = active_nodes; | |
20e07dea RR |
1922 | } |
1923 | ||
04bb2f94 RR |
1924 | /* |
1925 | * When adapting the scan rate, the period is divided into NUMA_PERIOD_SLOTS | |
1926 | * increments. The more local the fault statistics are, the higher the scan | |
a22b4b01 RR |
1927 | * period will be for the next scan window. If local/(local+remote) ratio is |
1928 | * below NUMA_PERIOD_THRESHOLD (where range of ratio is 1..NUMA_PERIOD_SLOTS) | |
1929 | * the scan period will decrease. Aim for 70% local accesses. | |
04bb2f94 RR |
1930 | */ |
1931 | #define NUMA_PERIOD_SLOTS 10 | |
a22b4b01 | 1932 | #define NUMA_PERIOD_THRESHOLD 7 |
04bb2f94 RR |
1933 | |
1934 | /* | |
1935 | * Increase the scan period (slow down scanning) if the majority of | |
1936 | * our memory is already on our local node, or if the majority of | |
1937 | * the page accesses are shared with other processes. | |
1938 | * Otherwise, decrease the scan period. | |
1939 | */ | |
1940 | static void update_task_scan_period(struct task_struct *p, | |
1941 | unsigned long shared, unsigned long private) | |
1942 | { | |
1943 | unsigned int period_slot; | |
37ec97de | 1944 | int lr_ratio, ps_ratio; |
04bb2f94 RR |
1945 | int diff; |
1946 | ||
1947 | unsigned long remote = p->numa_faults_locality[0]; | |
1948 | unsigned long local = p->numa_faults_locality[1]; | |
1949 | ||
1950 | /* | |
1951 | * If there were no record hinting faults then either the task is | |
1952 | * completely idle or all activity is areas that are not of interest | |
074c2381 MG |
1953 | * to automatic numa balancing. Related to that, if there were failed |
1954 | * migration then it implies we are migrating too quickly or the local | |
1955 | * node is overloaded. In either case, scan slower | |
04bb2f94 | 1956 | */ |
074c2381 | 1957 | if (local + shared == 0 || p->numa_faults_locality[2]) { |
04bb2f94 RR |
1958 | p->numa_scan_period = min(p->numa_scan_period_max, |
1959 | p->numa_scan_period << 1); | |
1960 | ||
1961 | p->mm->numa_next_scan = jiffies + | |
1962 | msecs_to_jiffies(p->numa_scan_period); | |
1963 | ||
1964 | return; | |
1965 | } | |
1966 | ||
1967 | /* | |
1968 | * Prepare to scale scan period relative to the current period. | |
1969 | * == NUMA_PERIOD_THRESHOLD scan period stays the same | |
1970 | * < NUMA_PERIOD_THRESHOLD scan period decreases (scan faster) | |
1971 | * >= NUMA_PERIOD_THRESHOLD scan period increases (scan slower) | |
1972 | */ | |
1973 | period_slot = DIV_ROUND_UP(p->numa_scan_period, NUMA_PERIOD_SLOTS); | |
37ec97de RR |
1974 | lr_ratio = (local * NUMA_PERIOD_SLOTS) / (local + remote); |
1975 | ps_ratio = (private * NUMA_PERIOD_SLOTS) / (private + shared); | |
1976 | ||
1977 | if (ps_ratio >= NUMA_PERIOD_THRESHOLD) { | |
1978 | /* | |
1979 | * Most memory accesses are local. There is no need to | |
1980 | * do fast NUMA scanning, since memory is already local. | |
1981 | */ | |
1982 | int slot = ps_ratio - NUMA_PERIOD_THRESHOLD; | |
1983 | if (!slot) | |
1984 | slot = 1; | |
1985 | diff = slot * period_slot; | |
1986 | } else if (lr_ratio >= NUMA_PERIOD_THRESHOLD) { | |
1987 | /* | |
1988 | * Most memory accesses are shared with other tasks. | |
1989 | * There is no point in continuing fast NUMA scanning, | |
1990 | * since other tasks may just move the memory elsewhere. | |
1991 | */ | |
1992 | int slot = lr_ratio - NUMA_PERIOD_THRESHOLD; | |
04bb2f94 RR |
1993 | if (!slot) |
1994 | slot = 1; | |
1995 | diff = slot * period_slot; | |
1996 | } else { | |
04bb2f94 | 1997 | /* |
37ec97de RR |
1998 | * Private memory faults exceed (SLOTS-THRESHOLD)/SLOTS, |
1999 | * yet they are not on the local NUMA node. Speed up | |
2000 | * NUMA scanning to get the memory moved over. | |
04bb2f94 | 2001 | */ |
37ec97de RR |
2002 | int ratio = max(lr_ratio, ps_ratio); |
2003 | diff = -(NUMA_PERIOD_THRESHOLD - ratio) * period_slot; | |
04bb2f94 RR |
2004 | } |
2005 | ||
2006 | p->numa_scan_period = clamp(p->numa_scan_period + diff, | |
2007 | task_scan_min(p), task_scan_max(p)); | |
2008 | memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); | |
2009 | } | |
2010 | ||
7e2703e6 RR |
2011 | /* |
2012 | * Get the fraction of time the task has been running since the last | |
2013 | * NUMA placement cycle. The scheduler keeps similar statistics, but | |
2014 | * decays those on a 32ms period, which is orders of magnitude off | |
2015 | * from the dozens-of-seconds NUMA balancing period. Use the scheduler | |
2016 | * stats only if the task is so new there are no NUMA statistics yet. | |
2017 | */ | |
2018 | static u64 numa_get_avg_runtime(struct task_struct *p, u64 *period) | |
2019 | { | |
2020 | u64 runtime, delta, now; | |
2021 | /* Use the start of this time slice to avoid calculations. */ | |
2022 | now = p->se.exec_start; | |
2023 | runtime = p->se.sum_exec_runtime; | |
2024 | ||
2025 | if (p->last_task_numa_placement) { | |
2026 | delta = runtime - p->last_sum_exec_runtime; | |
2027 | *period = now - p->last_task_numa_placement; | |
2028 | } else { | |
9d89c257 YD |
2029 | delta = p->se.avg.load_sum / p->se.load.weight; |
2030 | *period = LOAD_AVG_MAX; | |
7e2703e6 RR |
2031 | } |
2032 | ||
2033 | p->last_sum_exec_runtime = runtime; | |
2034 | p->last_task_numa_placement = now; | |
2035 | ||
2036 | return delta; | |
2037 | } | |
2038 | ||
54009416 RR |
2039 | /* |
2040 | * Determine the preferred nid for a task in a numa_group. This needs to | |
2041 | * be done in a way that produces consistent results with group_weight, | |
2042 | * otherwise workloads might not converge. | |
2043 | */ | |
2044 | static int preferred_group_nid(struct task_struct *p, int nid) | |
2045 | { | |
2046 | nodemask_t nodes; | |
2047 | int dist; | |
2048 | ||
2049 | /* Direct connections between all NUMA nodes. */ | |
2050 | if (sched_numa_topology_type == NUMA_DIRECT) | |
2051 | return nid; | |
2052 | ||
2053 | /* | |
2054 | * On a system with glueless mesh NUMA topology, group_weight | |
2055 | * scores nodes according to the number of NUMA hinting faults on | |
2056 | * both the node itself, and on nearby nodes. | |
2057 | */ | |
2058 | if (sched_numa_topology_type == NUMA_GLUELESS_MESH) { | |
2059 | unsigned long score, max_score = 0; | |
2060 | int node, max_node = nid; | |
2061 | ||
2062 | dist = sched_max_numa_distance; | |
2063 | ||
2064 | for_each_online_node(node) { | |
2065 | score = group_weight(p, node, dist); | |
2066 | if (score > max_score) { | |
2067 | max_score = score; | |
2068 | max_node = node; | |
2069 | } | |
2070 | } | |
2071 | return max_node; | |
2072 | } | |
2073 | ||
2074 | /* | |
2075 | * Finding the preferred nid in a system with NUMA backplane | |
2076 | * interconnect topology is more involved. The goal is to locate | |
2077 | * tasks from numa_groups near each other in the system, and | |
2078 | * untangle workloads from different sides of the system. This requires | |
2079 | * searching down the hierarchy of node groups, recursively searching | |
2080 | * inside the highest scoring group of nodes. The nodemask tricks | |
2081 | * keep the complexity of the search down. | |
2082 | */ | |
2083 | nodes = node_online_map; | |
2084 | for (dist = sched_max_numa_distance; dist > LOCAL_DISTANCE; dist--) { | |
2085 | unsigned long max_faults = 0; | |
81907478 | 2086 | nodemask_t max_group = NODE_MASK_NONE; |
54009416 RR |
2087 | int a, b; |
2088 | ||
2089 | /* Are there nodes at this distance from each other? */ | |
2090 | if (!find_numa_distance(dist)) | |
2091 | continue; | |
2092 | ||
2093 | for_each_node_mask(a, nodes) { | |
2094 | unsigned long faults = 0; | |
2095 | nodemask_t this_group; | |
2096 | nodes_clear(this_group); | |
2097 | ||
2098 | /* Sum group's NUMA faults; includes a==b case. */ | |
2099 | for_each_node_mask(b, nodes) { | |
2100 | if (node_distance(a, b) < dist) { | |
2101 | faults += group_faults(p, b); | |
2102 | node_set(b, this_group); | |
2103 | node_clear(b, nodes); | |
2104 | } | |
2105 | } | |
2106 | ||
2107 | /* Remember the top group. */ | |
2108 | if (faults > max_faults) { | |
2109 | max_faults = faults; | |
2110 | max_group = this_group; | |
2111 | /* | |
2112 | * subtle: at the smallest distance there is | |
2113 | * just one node left in each "group", the | |
2114 | * winner is the preferred nid. | |
2115 | */ | |
2116 | nid = a; | |
2117 | } | |
2118 | } | |
2119 | /* Next round, evaluate the nodes within max_group. */ | |
890a5409 JB |
2120 | if (!max_faults) |
2121 | break; | |
54009416 RR |
2122 | nodes = max_group; |
2123 | } | |
2124 | return nid; | |
2125 | } | |
2126 | ||
cbee9f88 PZ |
2127 | static void task_numa_placement(struct task_struct *p) |
2128 | { | |
83e1d2cd MG |
2129 | int seq, nid, max_nid = -1, max_group_nid = -1; |
2130 | unsigned long max_faults = 0, max_group_faults = 0; | |
04bb2f94 | 2131 | unsigned long fault_types[2] = { 0, 0 }; |
7e2703e6 RR |
2132 | unsigned long total_faults; |
2133 | u64 runtime, period; | |
7dbd13ed | 2134 | spinlock_t *group_lock = NULL; |
cbee9f88 | 2135 | |
7e5a2c17 JL |
2136 | /* |
2137 | * The p->mm->numa_scan_seq field gets updated without | |
2138 | * exclusive access. Use READ_ONCE() here to ensure | |
2139 | * that the field is read in a single access: | |
2140 | */ | |
316c1608 | 2141 | seq = READ_ONCE(p->mm->numa_scan_seq); |
cbee9f88 PZ |
2142 | if (p->numa_scan_seq == seq) |
2143 | return; | |
2144 | p->numa_scan_seq = seq; | |
598f0ec0 | 2145 | p->numa_scan_period_max = task_scan_max(p); |
cbee9f88 | 2146 | |
7e2703e6 RR |
2147 | total_faults = p->numa_faults_locality[0] + |
2148 | p->numa_faults_locality[1]; | |
2149 | runtime = numa_get_avg_runtime(p, &period); | |
2150 | ||
7dbd13ed MG |
2151 | /* If the task is part of a group prevent parallel updates to group stats */ |
2152 | if (p->numa_group) { | |
2153 | group_lock = &p->numa_group->lock; | |
60e69eed | 2154 | spin_lock_irq(group_lock); |
7dbd13ed MG |
2155 | } |
2156 | ||
688b7585 MG |
2157 | /* Find the node with the highest number of faults */ |
2158 | for_each_online_node(nid) { | |
44dba3d5 IM |
2159 | /* Keep track of the offsets in numa_faults array */ |
2160 | int mem_idx, membuf_idx, cpu_idx, cpubuf_idx; | |
83e1d2cd | 2161 | unsigned long faults = 0, group_faults = 0; |
44dba3d5 | 2162 | int priv; |
745d6147 | 2163 | |
be1e4e76 | 2164 | for (priv = 0; priv < NR_NUMA_HINT_FAULT_TYPES; priv++) { |
7e2703e6 | 2165 | long diff, f_diff, f_weight; |
8c8a743c | 2166 | |
44dba3d5 IM |
2167 | mem_idx = task_faults_idx(NUMA_MEM, nid, priv); |
2168 | membuf_idx = task_faults_idx(NUMA_MEMBUF, nid, priv); | |
2169 | cpu_idx = task_faults_idx(NUMA_CPU, nid, priv); | |
2170 | cpubuf_idx = task_faults_idx(NUMA_CPUBUF, nid, priv); | |
745d6147 | 2171 | |
ac8e895b | 2172 | /* Decay existing window, copy faults since last scan */ |
44dba3d5 IM |
2173 | diff = p->numa_faults[membuf_idx] - p->numa_faults[mem_idx] / 2; |
2174 | fault_types[priv] += p->numa_faults[membuf_idx]; | |
2175 | p->numa_faults[membuf_idx] = 0; | |
fb13c7ee | 2176 | |
7e2703e6 RR |
2177 | /* |
2178 | * Normalize the faults_from, so all tasks in a group | |
2179 | * count according to CPU use, instead of by the raw | |
2180 | * number of faults. Tasks with little runtime have | |
2181 | * little over-all impact on throughput, and thus their | |
2182 | * faults are less important. | |
2183 | */ | |
2184 | f_weight = div64_u64(runtime << 16, period + 1); | |
44dba3d5 | 2185 | f_weight = (f_weight * p->numa_faults[cpubuf_idx]) / |
7e2703e6 | 2186 | (total_faults + 1); |
44dba3d5 IM |
2187 | f_diff = f_weight - p->numa_faults[cpu_idx] / 2; |
2188 | p->numa_faults[cpubuf_idx] = 0; | |
50ec8a40 | 2189 | |
44dba3d5 IM |
2190 | p->numa_faults[mem_idx] += diff; |
2191 | p->numa_faults[cpu_idx] += f_diff; | |
2192 | faults += p->numa_faults[mem_idx]; | |
83e1d2cd | 2193 | p->total_numa_faults += diff; |
8c8a743c | 2194 | if (p->numa_group) { |
44dba3d5 IM |
2195 | /* |
2196 | * safe because we can only change our own group | |
2197 | * | |
2198 | * mem_idx represents the offset for a given | |
2199 | * nid and priv in a specific region because it | |
2200 | * is at the beginning of the numa_faults array. | |
2201 | */ | |
2202 | p->numa_group->faults[mem_idx] += diff; | |
2203 | p->numa_group->faults_cpu[mem_idx] += f_diff; | |
989348b5 | 2204 | p->numa_group->total_faults += diff; |
44dba3d5 | 2205 | group_faults += p->numa_group->faults[mem_idx]; |
8c8a743c | 2206 | } |
ac8e895b MG |
2207 | } |
2208 | ||
688b7585 MG |
2209 | if (faults > max_faults) { |
2210 | max_faults = faults; | |
2211 | max_nid = nid; | |
2212 | } | |
83e1d2cd MG |
2213 | |
2214 | if (group_faults > max_group_faults) { | |
2215 | max_group_faults = group_faults; | |
2216 | max_group_nid = nid; | |
2217 | } | |
2218 | } | |
2219 | ||
04bb2f94 RR |
2220 | update_task_scan_period(p, fault_types[0], fault_types[1]); |
2221 | ||
7dbd13ed | 2222 | if (p->numa_group) { |
4142c3eb | 2223 | numa_group_count_active_nodes(p->numa_group); |
60e69eed | 2224 | spin_unlock_irq(group_lock); |
54009416 | 2225 | max_nid = preferred_group_nid(p, max_group_nid); |
688b7585 MG |
2226 | } |
2227 | ||
bb97fc31 RR |
2228 | if (max_faults) { |
2229 | /* Set the new preferred node */ | |
2230 | if (max_nid != p->numa_preferred_nid) | |
2231 | sched_setnuma(p, max_nid); | |
2232 | ||
2233 | if (task_node(p) != p->numa_preferred_nid) | |
2234 | numa_migrate_preferred(p); | |
3a7053b3 | 2235 | } |
cbee9f88 PZ |
2236 | } |
2237 | ||
8c8a743c PZ |
2238 | static inline int get_numa_group(struct numa_group *grp) |
2239 | { | |
2240 | return atomic_inc_not_zero(&grp->refcount); | |
2241 | } | |
2242 | ||
2243 | static inline void put_numa_group(struct numa_group *grp) | |
2244 | { | |
2245 | if (atomic_dec_and_test(&grp->refcount)) | |
2246 | kfree_rcu(grp, rcu); | |
2247 | } | |
2248 | ||
3e6a9418 MG |
2249 | static void task_numa_group(struct task_struct *p, int cpupid, int flags, |
2250 | int *priv) | |
8c8a743c PZ |
2251 | { |
2252 | struct numa_group *grp, *my_grp; | |
2253 | struct task_struct *tsk; | |
2254 | bool join = false; | |
2255 | int cpu = cpupid_to_cpu(cpupid); | |
2256 | int i; | |
2257 | ||
2258 | if (unlikely(!p->numa_group)) { | |
2259 | unsigned int size = sizeof(struct numa_group) + | |
50ec8a40 | 2260 | 4*nr_node_ids*sizeof(unsigned long); |
8c8a743c PZ |
2261 | |
2262 | grp = kzalloc(size, GFP_KERNEL | __GFP_NOWARN); | |
2263 | if (!grp) | |
2264 | return; | |
2265 | ||
2266 | atomic_set(&grp->refcount, 1); | |
4142c3eb RR |
2267 | grp->active_nodes = 1; |
2268 | grp->max_faults_cpu = 0; | |
8c8a743c | 2269 | spin_lock_init(&grp->lock); |
e29cf08b | 2270 | grp->gid = p->pid; |
50ec8a40 | 2271 | /* Second half of the array tracks nids where faults happen */ |
be1e4e76 RR |
2272 | grp->faults_cpu = grp->faults + NR_NUMA_HINT_FAULT_TYPES * |
2273 | nr_node_ids; | |
8c8a743c | 2274 | |
be1e4e76 | 2275 | for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) |
44dba3d5 | 2276 | grp->faults[i] = p->numa_faults[i]; |
8c8a743c | 2277 | |
989348b5 | 2278 | grp->total_faults = p->total_numa_faults; |
83e1d2cd | 2279 | |
8c8a743c PZ |
2280 | grp->nr_tasks++; |
2281 | rcu_assign_pointer(p->numa_group, grp); | |
2282 | } | |
2283 | ||
2284 | rcu_read_lock(); | |
316c1608 | 2285 | tsk = READ_ONCE(cpu_rq(cpu)->curr); |
8c8a743c PZ |
2286 | |
2287 | if (!cpupid_match_pid(tsk, cpupid)) | |
3354781a | 2288 | goto no_join; |
8c8a743c PZ |
2289 | |
2290 | grp = rcu_dereference(tsk->numa_group); | |
2291 | if (!grp) | |
3354781a | 2292 | goto no_join; |
8c8a743c PZ |
2293 | |
2294 | my_grp = p->numa_group; | |
2295 | if (grp == my_grp) | |
3354781a | 2296 | goto no_join; |
8c8a743c PZ |
2297 | |
2298 | /* | |
2299 | * Only join the other group if its bigger; if we're the bigger group, | |
2300 | * the other task will join us. | |
2301 | */ | |
2302 | if (my_grp->nr_tasks > grp->nr_tasks) | |
3354781a | 2303 | goto no_join; |
8c8a743c PZ |
2304 | |
2305 | /* | |
2306 | * Tie-break on the grp address. | |
2307 | */ | |
2308 | if (my_grp->nr_tasks == grp->nr_tasks && my_grp > grp) | |
3354781a | 2309 | goto no_join; |
8c8a743c | 2310 | |
dabe1d99 RR |
2311 | /* Always join threads in the same process. */ |
2312 | if (tsk->mm == current->mm) | |
2313 | join = true; | |
2314 | ||
2315 | /* Simple filter to avoid false positives due to PID collisions */ | |
2316 | if (flags & TNF_SHARED) | |
2317 | join = true; | |
8c8a743c | 2318 | |
3e6a9418 MG |
2319 | /* Update priv based on whether false sharing was detected */ |
2320 | *priv = !join; | |
2321 | ||
dabe1d99 | 2322 | if (join && !get_numa_group(grp)) |
3354781a | 2323 | goto no_join; |
8c8a743c | 2324 | |
8c8a743c PZ |
2325 | rcu_read_unlock(); |
2326 | ||
2327 | if (!join) | |
2328 | return; | |
2329 | ||
60e69eed MG |
2330 | BUG_ON(irqs_disabled()); |
2331 | double_lock_irq(&my_grp->lock, &grp->lock); | |
989348b5 | 2332 | |
be1e4e76 | 2333 | for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) { |
44dba3d5 IM |
2334 | my_grp->faults[i] -= p->numa_faults[i]; |
2335 | grp->faults[i] += p->numa_faults[i]; | |
8c8a743c | 2336 | } |
989348b5 MG |
2337 | my_grp->total_faults -= p->total_numa_faults; |
2338 | grp->total_faults += p->total_numa_faults; | |
8c8a743c | 2339 | |
8c8a743c PZ |
2340 | my_grp->nr_tasks--; |
2341 | grp->nr_tasks++; | |
2342 | ||
2343 | spin_unlock(&my_grp->lock); | |
60e69eed | 2344 | spin_unlock_irq(&grp->lock); |
8c8a743c PZ |
2345 | |
2346 | rcu_assign_pointer(p->numa_group, grp); | |
2347 | ||
2348 | put_numa_group(my_grp); | |
3354781a PZ |
2349 | return; |
2350 | ||
2351 | no_join: | |
2352 | rcu_read_unlock(); | |
2353 | return; | |
8c8a743c PZ |
2354 | } |
2355 | ||
2356 | void task_numa_free(struct task_struct *p) | |
2357 | { | |
2358 | struct numa_group *grp = p->numa_group; | |
44dba3d5 | 2359 | void *numa_faults = p->numa_faults; |
e9dd685c SR |
2360 | unsigned long flags; |
2361 | int i; | |
8c8a743c PZ |
2362 | |
2363 | if (grp) { | |
e9dd685c | 2364 | spin_lock_irqsave(&grp->lock, flags); |
be1e4e76 | 2365 | for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) |
44dba3d5 | 2366 | grp->faults[i] -= p->numa_faults[i]; |
989348b5 | 2367 | grp->total_faults -= p->total_numa_faults; |
83e1d2cd | 2368 | |
8c8a743c | 2369 | grp->nr_tasks--; |
e9dd685c | 2370 | spin_unlock_irqrestore(&grp->lock, flags); |
35b123e2 | 2371 | RCU_INIT_POINTER(p->numa_group, NULL); |
8c8a743c PZ |
2372 | put_numa_group(grp); |
2373 | } | |
2374 | ||
44dba3d5 | 2375 | p->numa_faults = NULL; |
82727018 | 2376 | kfree(numa_faults); |
8c8a743c PZ |
2377 | } |
2378 | ||
cbee9f88 PZ |
2379 | /* |
2380 | * Got a PROT_NONE fault for a page on @node. | |
2381 | */ | |
58b46da3 | 2382 | void task_numa_fault(int last_cpupid, int mem_node, int pages, int flags) |
cbee9f88 PZ |
2383 | { |
2384 | struct task_struct *p = current; | |
6688cc05 | 2385 | bool migrated = flags & TNF_MIGRATED; |
58b46da3 | 2386 | int cpu_node = task_node(current); |
792568ec | 2387 | int local = !!(flags & TNF_FAULT_LOCAL); |
4142c3eb | 2388 | struct numa_group *ng; |
ac8e895b | 2389 | int priv; |
cbee9f88 | 2390 | |
2a595721 | 2391 | if (!static_branch_likely(&sched_numa_balancing)) |
1a687c2e MG |
2392 | return; |
2393 | ||
9ff1d9ff MG |
2394 | /* for example, ksmd faulting in a user's mm */ |
2395 | if (!p->mm) | |
2396 | return; | |
2397 | ||
f809ca9a | 2398 | /* Allocate buffer to track faults on a per-node basis */ |
44dba3d5 IM |
2399 | if (unlikely(!p->numa_faults)) { |
2400 | int size = sizeof(*p->numa_faults) * | |
be1e4e76 | 2401 | NR_NUMA_HINT_FAULT_BUCKETS * nr_node_ids; |
f809ca9a | 2402 | |
44dba3d5 IM |
2403 | p->numa_faults = kzalloc(size, GFP_KERNEL|__GFP_NOWARN); |
2404 | if (!p->numa_faults) | |
f809ca9a | 2405 | return; |
745d6147 | 2406 | |
83e1d2cd | 2407 | p->total_numa_faults = 0; |
04bb2f94 | 2408 | memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); |
f809ca9a | 2409 | } |
cbee9f88 | 2410 | |
8c8a743c PZ |
2411 | /* |
2412 | * First accesses are treated as private, otherwise consider accesses | |
2413 | * to be private if the accessing pid has not changed | |
2414 | */ | |
2415 | if (unlikely(last_cpupid == (-1 & LAST_CPUPID_MASK))) { | |
2416 | priv = 1; | |
2417 | } else { | |
2418 | priv = cpupid_match_pid(p, last_cpupid); | |
6688cc05 | 2419 | if (!priv && !(flags & TNF_NO_GROUP)) |
3e6a9418 | 2420 | task_numa_group(p, last_cpupid, flags, &priv); |
8c8a743c PZ |
2421 | } |
2422 | ||
792568ec RR |
2423 | /* |
2424 | * If a workload spans multiple NUMA nodes, a shared fault that | |
2425 | * occurs wholly within the set of nodes that the workload is | |
2426 | * actively using should be counted as local. This allows the | |
2427 | * scan rate to slow down when a workload has settled down. | |
2428 | */ | |
4142c3eb RR |
2429 | ng = p->numa_group; |
2430 | if (!priv && !local && ng && ng->active_nodes > 1 && | |
2431 | numa_is_active_node(cpu_node, ng) && | |
2432 | numa_is_active_node(mem_node, ng)) | |
792568ec RR |
2433 | local = 1; |
2434 | ||
cbee9f88 | 2435 | task_numa_placement(p); |
f809ca9a | 2436 | |
2739d3ee RR |
2437 | /* |
2438 | * Retry task to preferred node migration periodically, in case it | |
2439 | * case it previously failed, or the scheduler moved us. | |
2440 | */ | |
2441 | if (time_after(jiffies, p->numa_migrate_retry)) | |
6b9a7460 MG |
2442 | numa_migrate_preferred(p); |
2443 | ||
b32e86b4 IM |
2444 | if (migrated) |
2445 | p->numa_pages_migrated += pages; | |
074c2381 MG |
2446 | if (flags & TNF_MIGRATE_FAIL) |
2447 | p->numa_faults_locality[2] += pages; | |
b32e86b4 | 2448 | |
44dba3d5 IM |
2449 | p->numa_faults[task_faults_idx(NUMA_MEMBUF, mem_node, priv)] += pages; |
2450 | p->numa_faults[task_faults_idx(NUMA_CPUBUF, cpu_node, priv)] += pages; | |
792568ec | 2451 | p->numa_faults_locality[local] += pages; |
cbee9f88 PZ |
2452 | } |
2453 | ||
6e5fb223 PZ |
2454 | static void reset_ptenuma_scan(struct task_struct *p) |
2455 | { | |
7e5a2c17 JL |
2456 | /* |
2457 | * We only did a read acquisition of the mmap sem, so | |
2458 | * p->mm->numa_scan_seq is written to without exclusive access | |
2459 | * and the update is not guaranteed to be atomic. That's not | |
2460 | * much of an issue though, since this is just used for | |
2461 | * statistical sampling. Use READ_ONCE/WRITE_ONCE, which are not | |
2462 | * expensive, to avoid any form of compiler optimizations: | |
2463 | */ | |
316c1608 | 2464 | WRITE_ONCE(p->mm->numa_scan_seq, READ_ONCE(p->mm->numa_scan_seq) + 1); |
6e5fb223 PZ |
2465 | p->mm->numa_scan_offset = 0; |
2466 | } | |
2467 | ||
cbee9f88 PZ |
2468 | /* |
2469 | * The expensive part of numa migration is done from task_work context. | |
2470 | * Triggered from task_tick_numa(). | |
2471 | */ | |
2472 | void task_numa_work(struct callback_head *work) | |
2473 | { | |
2474 | unsigned long migrate, next_scan, now = jiffies; | |
2475 | struct task_struct *p = current; | |
2476 | struct mm_struct *mm = p->mm; | |
51170840 | 2477 | u64 runtime = p->se.sum_exec_runtime; |
6e5fb223 | 2478 | struct vm_area_struct *vma; |
9f40604c | 2479 | unsigned long start, end; |
598f0ec0 | 2480 | unsigned long nr_pte_updates = 0; |
4620f8c1 | 2481 | long pages, virtpages; |
cbee9f88 | 2482 | |
9148a3a1 | 2483 | SCHED_WARN_ON(p != container_of(work, struct task_struct, numa_work)); |
cbee9f88 PZ |
2484 | |
2485 | work->next = work; /* protect against double add */ | |
2486 | /* | |
2487 | * Who cares about NUMA placement when they're dying. | |
2488 | * | |
2489 | * NOTE: make sure not to dereference p->mm before this check, | |
2490 | * exit_task_work() happens _after_ exit_mm() so we could be called | |
2491 | * without p->mm even though we still had it when we enqueued this | |
2492 | * work. | |
2493 | */ | |
2494 | if (p->flags & PF_EXITING) | |
2495 | return; | |
2496 | ||
930aa174 | 2497 | if (!mm->numa_next_scan) { |
7e8d16b6 MG |
2498 | mm->numa_next_scan = now + |
2499 | msecs_to_jiffies(sysctl_numa_balancing_scan_delay); | |
b8593bfd MG |
2500 | } |
2501 | ||
cbee9f88 PZ |
2502 | /* |
2503 | * Enforce maximal scan/migration frequency.. | |
2504 | */ | |
2505 | migrate = mm->numa_next_scan; | |
2506 | if (time_before(now, migrate)) | |
2507 | return; | |
2508 | ||
598f0ec0 MG |
2509 | if (p->numa_scan_period == 0) { |
2510 | p->numa_scan_period_max = task_scan_max(p); | |
b5dd77c8 | 2511 | p->numa_scan_period = task_scan_start(p); |
598f0ec0 | 2512 | } |
cbee9f88 | 2513 | |
fb003b80 | 2514 | next_scan = now + msecs_to_jiffies(p->numa_scan_period); |
cbee9f88 PZ |
2515 | if (cmpxchg(&mm->numa_next_scan, migrate, next_scan) != migrate) |
2516 | return; | |
2517 | ||
19a78d11 PZ |
2518 | /* |
2519 | * Delay this task enough that another task of this mm will likely win | |
2520 | * the next time around. | |
2521 | */ | |
2522 | p->node_stamp += 2 * TICK_NSEC; | |
2523 | ||
9f40604c MG |
2524 | start = mm->numa_scan_offset; |
2525 | pages = sysctl_numa_balancing_scan_size; | |
2526 | pages <<= 20 - PAGE_SHIFT; /* MB in pages */ | |
4620f8c1 | 2527 | virtpages = pages * 8; /* Scan up to this much virtual space */ |
9f40604c MG |
2528 | if (!pages) |
2529 | return; | |
cbee9f88 | 2530 | |
4620f8c1 | 2531 | |
8655d549 VB |
2532 | if (!down_read_trylock(&mm->mmap_sem)) |
2533 | return; | |
9f40604c | 2534 | vma = find_vma(mm, start); |
6e5fb223 PZ |
2535 | if (!vma) { |
2536 | reset_ptenuma_scan(p); | |
9f40604c | 2537 | start = 0; |
6e5fb223 PZ |
2538 | vma = mm->mmap; |
2539 | } | |
9f40604c | 2540 | for (; vma; vma = vma->vm_next) { |
6b79c57b | 2541 | if (!vma_migratable(vma) || !vma_policy_mof(vma) || |
8e76d4ee | 2542 | is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_MIXEDMAP)) { |
6e5fb223 | 2543 | continue; |
6b79c57b | 2544 | } |
6e5fb223 | 2545 | |
4591ce4f MG |
2546 | /* |
2547 | * Shared library pages mapped by multiple processes are not | |
2548 | * migrated as it is expected they are cache replicated. Avoid | |
2549 | * hinting faults in read-only file-backed mappings or the vdso | |
2550 | * as migrating the pages will be of marginal benefit. | |
2551 | */ | |
2552 | if (!vma->vm_mm || | |
2553 | (vma->vm_file && (vma->vm_flags & (VM_READ|VM_WRITE)) == (VM_READ))) | |
2554 | continue; | |
2555 | ||
3c67f474 MG |
2556 | /* |
2557 | * Skip inaccessible VMAs to avoid any confusion between | |
2558 | * PROT_NONE and NUMA hinting ptes | |
2559 | */ | |
2560 | if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))) | |
2561 | continue; | |
4591ce4f | 2562 | |
9f40604c MG |
2563 | do { |
2564 | start = max(start, vma->vm_start); | |
2565 | end = ALIGN(start + (pages << PAGE_SHIFT), HPAGE_SIZE); | |
2566 | end = min(end, vma->vm_end); | |
4620f8c1 | 2567 | nr_pte_updates = change_prot_numa(vma, start, end); |
598f0ec0 MG |
2568 | |
2569 | /* | |
4620f8c1 RR |
2570 | * Try to scan sysctl_numa_balancing_size worth of |
2571 | * hpages that have at least one present PTE that | |
2572 | * is not already pte-numa. If the VMA contains | |
2573 | * areas that are unused or already full of prot_numa | |
2574 | * PTEs, scan up to virtpages, to skip through those | |
2575 | * areas faster. | |
598f0ec0 MG |
2576 | */ |
2577 | if (nr_pte_updates) | |
2578 | pages -= (end - start) >> PAGE_SHIFT; | |
4620f8c1 | 2579 | virtpages -= (end - start) >> PAGE_SHIFT; |
6e5fb223 | 2580 | |
9f40604c | 2581 | start = end; |
4620f8c1 | 2582 | if (pages <= 0 || virtpages <= 0) |
9f40604c | 2583 | goto out; |
3cf1962c RR |
2584 | |
2585 | cond_resched(); | |
9f40604c | 2586 | } while (end != vma->vm_end); |
cbee9f88 | 2587 | } |
6e5fb223 | 2588 | |
9f40604c | 2589 | out: |
6e5fb223 | 2590 | /* |
c69307d5 PZ |
2591 | * It is possible to reach the end of the VMA list but the last few |
2592 | * VMAs are not guaranteed to the vma_migratable. If they are not, we | |
2593 | * would find the !migratable VMA on the next scan but not reset the | |
2594 | * scanner to the start so check it now. | |
6e5fb223 PZ |
2595 | */ |
2596 | if (vma) | |
9f40604c | 2597 | mm->numa_scan_offset = start; |
6e5fb223 PZ |
2598 | else |
2599 | reset_ptenuma_scan(p); | |
2600 | up_read(&mm->mmap_sem); | |
51170840 RR |
2601 | |
2602 | /* | |
2603 | * Make sure tasks use at least 32x as much time to run other code | |
2604 | * than they used here, to limit NUMA PTE scanning overhead to 3% max. | |
2605 | * Usually update_task_scan_period slows down scanning enough; on an | |
2606 | * overloaded system we need to limit overhead on a per task basis. | |
2607 | */ | |
2608 | if (unlikely(p->se.sum_exec_runtime != runtime)) { | |
2609 | u64 diff = p->se.sum_exec_runtime - runtime; | |
2610 | p->node_stamp += 32 * diff; | |
2611 | } | |
cbee9f88 PZ |
2612 | } |
2613 | ||
2614 | /* | |
2615 | * Drive the periodic memory faults.. | |
2616 | */ | |
2617 | void task_tick_numa(struct rq *rq, struct task_struct *curr) | |
2618 | { | |
2619 | struct callback_head *work = &curr->numa_work; | |
2620 | u64 period, now; | |
2621 | ||
2622 | /* | |
2623 | * We don't care about NUMA placement if we don't have memory. | |
2624 | */ | |
2625 | if (!curr->mm || (curr->flags & PF_EXITING) || work->next != work) | |
2626 | return; | |
2627 | ||
2628 | /* | |
2629 | * Using runtime rather than walltime has the dual advantage that | |
2630 | * we (mostly) drive the selection from busy threads and that the | |
2631 | * task needs to have done some actual work before we bother with | |
2632 | * NUMA placement. | |
2633 | */ | |
2634 | now = curr->se.sum_exec_runtime; | |
2635 | period = (u64)curr->numa_scan_period * NSEC_PER_MSEC; | |
2636 | ||
25b3e5a3 | 2637 | if (now > curr->node_stamp + period) { |
4b96a29b | 2638 | if (!curr->node_stamp) |
b5dd77c8 | 2639 | curr->numa_scan_period = task_scan_start(curr); |
19a78d11 | 2640 | curr->node_stamp += period; |
cbee9f88 PZ |
2641 | |
2642 | if (!time_before(jiffies, curr->mm->numa_next_scan)) { | |
2643 | init_task_work(work, task_numa_work); /* TODO: move this into sched_fork() */ | |
2644 | task_work_add(curr, work, true); | |
2645 | } | |
2646 | } | |
2647 | } | |
3fed382b | 2648 | |
cbee9f88 PZ |
2649 | #else |
2650 | static void task_tick_numa(struct rq *rq, struct task_struct *curr) | |
2651 | { | |
2652 | } | |
0ec8aa00 PZ |
2653 | |
2654 | static inline void account_numa_enqueue(struct rq *rq, struct task_struct *p) | |
2655 | { | |
2656 | } | |
2657 | ||
2658 | static inline void account_numa_dequeue(struct rq *rq, struct task_struct *p) | |
2659 | { | |
2660 | } | |
3fed382b | 2661 | |
cbee9f88 PZ |
2662 | #endif /* CONFIG_NUMA_BALANCING */ |
2663 | ||
30cfdcfc DA |
2664 | static void |
2665 | account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
2666 | { | |
2667 | update_load_add(&cfs_rq->load, se->load.weight); | |
c09595f6 | 2668 | if (!parent_entity(se)) |
029632fb | 2669 | update_load_add(&rq_of(cfs_rq)->load, se->load.weight); |
367456c7 | 2670 | #ifdef CONFIG_SMP |
0ec8aa00 PZ |
2671 | if (entity_is_task(se)) { |
2672 | struct rq *rq = rq_of(cfs_rq); | |
2673 | ||
2674 | account_numa_enqueue(rq, task_of(se)); | |
2675 | list_add(&se->group_node, &rq->cfs_tasks); | |
2676 | } | |
367456c7 | 2677 | #endif |
30cfdcfc | 2678 | cfs_rq->nr_running++; |
30cfdcfc DA |
2679 | } |
2680 | ||
2681 | static void | |
2682 | account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
2683 | { | |
2684 | update_load_sub(&cfs_rq->load, se->load.weight); | |
c09595f6 | 2685 | if (!parent_entity(se)) |
029632fb | 2686 | update_load_sub(&rq_of(cfs_rq)->load, se->load.weight); |
bfdb198c | 2687 | #ifdef CONFIG_SMP |
0ec8aa00 PZ |
2688 | if (entity_is_task(se)) { |
2689 | account_numa_dequeue(rq_of(cfs_rq), task_of(se)); | |
b87f1724 | 2690 | list_del_init(&se->group_node); |
0ec8aa00 | 2691 | } |
bfdb198c | 2692 | #endif |
30cfdcfc | 2693 | cfs_rq->nr_running--; |
30cfdcfc DA |
2694 | } |
2695 | ||
3ff6dcac YZ |
2696 | #ifdef CONFIG_FAIR_GROUP_SCHED |
2697 | # ifdef CONFIG_SMP | |
ea1dc6fc | 2698 | static long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg) |
cf5f0acf | 2699 | { |
ea1dc6fc | 2700 | long tg_weight, load, shares; |
cf5f0acf PZ |
2701 | |
2702 | /* | |
ea1dc6fc PZ |
2703 | * This really should be: cfs_rq->avg.load_avg, but instead we use |
2704 | * cfs_rq->load.weight, which is its upper bound. This helps ramp up | |
2705 | * the shares for small weight interactive tasks. | |
cf5f0acf | 2706 | */ |
ea1dc6fc | 2707 | load = scale_load_down(cfs_rq->load.weight); |
cf5f0acf | 2708 | |
ea1dc6fc | 2709 | tg_weight = atomic_long_read(&tg->load_avg); |
3ff6dcac | 2710 | |
ea1dc6fc PZ |
2711 | /* Ensure tg_weight >= load */ |
2712 | tg_weight -= cfs_rq->tg_load_avg_contrib; | |
2713 | tg_weight += load; | |
3ff6dcac | 2714 | |
3ff6dcac | 2715 | shares = (tg->shares * load); |
cf5f0acf PZ |
2716 | if (tg_weight) |
2717 | shares /= tg_weight; | |
3ff6dcac | 2718 | |
b8fd8423 DE |
2719 | /* |
2720 | * MIN_SHARES has to be unscaled here to support per-CPU partitioning | |
2721 | * of a group with small tg->shares value. It is a floor value which is | |
2722 | * assigned as a minimum load.weight to the sched_entity representing | |
2723 | * the group on a CPU. | |
2724 | * | |
2725 | * E.g. on 64-bit for a group with tg->shares of scale_load(15)=15*1024 | |
2726 | * on an 8-core system with 8 tasks each runnable on one CPU shares has | |
2727 | * to be 15*1024*1/8=1920 instead of scale_load(MIN_SHARES)=2*1024. In | |
2728 | * case no task is runnable on a CPU MIN_SHARES=2 should be returned | |
2729 | * instead of 0. | |
2730 | */ | |
3ff6dcac YZ |
2731 | if (shares < MIN_SHARES) |
2732 | shares = MIN_SHARES; | |
2733 | if (shares > tg->shares) | |
2734 | shares = tg->shares; | |
2735 | ||
2736 | return shares; | |
2737 | } | |
3ff6dcac | 2738 | # else /* CONFIG_SMP */ |
6d5ab293 | 2739 | static inline long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg) |
3ff6dcac YZ |
2740 | { |
2741 | return tg->shares; | |
2742 | } | |
3ff6dcac | 2743 | # endif /* CONFIG_SMP */ |
ea1dc6fc | 2744 | |
2069dd75 PZ |
2745 | static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, |
2746 | unsigned long weight) | |
2747 | { | |
19e5eebb PT |
2748 | if (se->on_rq) { |
2749 | /* commit outstanding execution time */ | |
2750 | if (cfs_rq->curr == se) | |
2751 | update_curr(cfs_rq); | |
2069dd75 | 2752 | account_entity_dequeue(cfs_rq, se); |
19e5eebb | 2753 | } |
2069dd75 PZ |
2754 | |
2755 | update_load_set(&se->load, weight); | |
2756 | ||
2757 | if (se->on_rq) | |
2758 | account_entity_enqueue(cfs_rq, se); | |
2759 | } | |
2760 | ||
82958366 PT |
2761 | static inline int throttled_hierarchy(struct cfs_rq *cfs_rq); |
2762 | ||
89ee048f | 2763 | static void update_cfs_shares(struct sched_entity *se) |
2069dd75 | 2764 | { |
89ee048f | 2765 | struct cfs_rq *cfs_rq = group_cfs_rq(se); |
2069dd75 | 2766 | struct task_group *tg; |
3ff6dcac | 2767 | long shares; |
2069dd75 | 2768 | |
89ee048f VG |
2769 | if (!cfs_rq) |
2770 | return; | |
2771 | ||
2772 | if (throttled_hierarchy(cfs_rq)) | |
2069dd75 | 2773 | return; |
89ee048f VG |
2774 | |
2775 | tg = cfs_rq->tg; | |
2776 | ||
3ff6dcac YZ |
2777 | #ifndef CONFIG_SMP |
2778 | if (likely(se->load.weight == tg->shares)) | |
2779 | return; | |
2780 | #endif | |
6d5ab293 | 2781 | shares = calc_cfs_shares(cfs_rq, tg); |
2069dd75 PZ |
2782 | |
2783 | reweight_entity(cfs_rq_of(se), se, shares); | |
2784 | } | |
89ee048f | 2785 | |
2069dd75 | 2786 | #else /* CONFIG_FAIR_GROUP_SCHED */ |
89ee048f | 2787 | static inline void update_cfs_shares(struct sched_entity *se) |
2069dd75 PZ |
2788 | { |
2789 | } | |
2790 | #endif /* CONFIG_FAIR_GROUP_SCHED */ | |
2791 | ||
a030d738 VK |
2792 | static inline void cfs_rq_util_change(struct cfs_rq *cfs_rq) |
2793 | { | |
43964409 LT |
2794 | struct rq *rq = rq_of(cfs_rq); |
2795 | ||
2796 | if (&rq->cfs == cfs_rq) { | |
a030d738 VK |
2797 | /* |
2798 | * There are a few boundary cases this might miss but it should | |
2799 | * get called often enough that that should (hopefully) not be | |
2800 | * a real problem -- added to that it only calls on the local | |
2801 | * CPU, so if we enqueue remotely we'll miss an update, but | |
2802 | * the next tick/schedule should update. | |
2803 | * | |
2804 | * It will not get called when we go idle, because the idle | |
2805 | * thread is a different class (!fair), nor will the utilization | |
2806 | * number include things like RT tasks. | |
2807 | * | |
2808 | * As is, the util number is not freq-invariant (we'd have to | |
2809 | * implement arch_scale_freq_capacity() for that). | |
2810 | * | |
2811 | * See cpu_util(). | |
2812 | */ | |
43964409 | 2813 | cpufreq_update_util(rq, 0); |
a030d738 VK |
2814 | } |
2815 | } | |
2816 | ||
141965c7 | 2817 | #ifdef CONFIG_SMP |
9d85f21c PT |
2818 | /* |
2819 | * Approximate: | |
2820 | * val * y^n, where y^32 ~= 0.5 (~1 scheduling period) | |
2821 | */ | |
a481db34 | 2822 | static u64 decay_load(u64 val, u64 n) |
9d85f21c | 2823 | { |
5b51f2f8 PT |
2824 | unsigned int local_n; |
2825 | ||
05296e75 | 2826 | if (unlikely(n > LOAD_AVG_PERIOD * 63)) |
5b51f2f8 PT |
2827 | return 0; |
2828 | ||
2829 | /* after bounds checking we can collapse to 32-bit */ | |
2830 | local_n = n; | |
2831 | ||
2832 | /* | |
2833 | * As y^PERIOD = 1/2, we can combine | |
9c58c79a ZZ |
2834 | * y^n = 1/2^(n/PERIOD) * y^(n%PERIOD) |
2835 | * With a look-up table which covers y^n (n<PERIOD) | |
5b51f2f8 PT |
2836 | * |
2837 | * To achieve constant time decay_load. | |
2838 | */ | |
2839 | if (unlikely(local_n >= LOAD_AVG_PERIOD)) { | |
2840 | val >>= local_n / LOAD_AVG_PERIOD; | |
2841 | local_n %= LOAD_AVG_PERIOD; | |
9d85f21c PT |
2842 | } |
2843 | ||
9d89c257 YD |
2844 | val = mul_u64_u32_shr(val, runnable_avg_yN_inv[local_n], 32); |
2845 | return val; | |
5b51f2f8 PT |
2846 | } |
2847 | ||
05296e75 | 2848 | static u32 __accumulate_pelt_segments(u64 periods, u32 d1, u32 d3) |
5b51f2f8 | 2849 | { |
05296e75 | 2850 | u32 c1, c2, c3 = d3; /* y^0 == 1 */ |
5b51f2f8 | 2851 | |
a481db34 | 2852 | /* |
3841cdc3 | 2853 | * c1 = d1 y^p |
a481db34 | 2854 | */ |
05296e75 | 2855 | c1 = decay_load((u64)d1, periods); |
a481db34 | 2856 | |
a481db34 | 2857 | /* |
3841cdc3 | 2858 | * p-1 |
05296e75 PZ |
2859 | * c2 = 1024 \Sum y^n |
2860 | * n=1 | |
a481db34 | 2861 | * |
05296e75 PZ |
2862 | * inf inf |
2863 | * = 1024 ( \Sum y^n - \Sum y^n - y^0 ) | |
3841cdc3 | 2864 | * n=0 n=p |
a481db34 | 2865 | */ |
05296e75 | 2866 | c2 = LOAD_AVG_MAX - decay_load(LOAD_AVG_MAX, periods) - 1024; |
a481db34 YD |
2867 | |
2868 | return c1 + c2 + c3; | |
9d85f21c PT |
2869 | } |
2870 | ||
54a21385 | 2871 | #define cap_scale(v, s) ((v)*(s) >> SCHED_CAPACITY_SHIFT) |
e0f5f3af | 2872 | |
a481db34 YD |
2873 | /* |
2874 | * Accumulate the three separate parts of the sum; d1 the remainder | |
2875 | * of the last (incomplete) period, d2 the span of full periods and d3 | |
2876 | * the remainder of the (incomplete) current period. | |
2877 | * | |
2878 | * d1 d2 d3 | |
2879 | * ^ ^ ^ | |
2880 | * | | | | |
2881 | * |<->|<----------------->|<--->| | |
2882 | * ... |---x---|------| ... |------|-----x (now) | |
2883 | * | |
3841cdc3 PZ |
2884 | * p-1 |
2885 | * u' = (u + d1) y^p + 1024 \Sum y^n + d3 y^0 | |
2886 | * n=1 | |
a481db34 | 2887 | * |
3841cdc3 | 2888 | * = u y^p + (Step 1) |
a481db34 | 2889 | * |
3841cdc3 PZ |
2890 | * p-1 |
2891 | * d1 y^p + 1024 \Sum y^n + d3 y^0 (Step 2) | |
2892 | * n=1 | |
a481db34 YD |
2893 | */ |
2894 | static __always_inline u32 | |
2895 | accumulate_sum(u64 delta, int cpu, struct sched_avg *sa, | |
2896 | unsigned long weight, int running, struct cfs_rq *cfs_rq) | |
2897 | { | |
2898 | unsigned long scale_freq, scale_cpu; | |
05296e75 | 2899 | u32 contrib = (u32)delta; /* p == 0 -> delta < 1024 */ |
a481db34 | 2900 | u64 periods; |
a481db34 YD |
2901 | |
2902 | scale_freq = arch_scale_freq_capacity(NULL, cpu); | |
2903 | scale_cpu = arch_scale_cpu_capacity(NULL, cpu); | |
2904 | ||
2905 | delta += sa->period_contrib; | |
2906 | periods = delta / 1024; /* A period is 1024us (~1ms) */ | |
2907 | ||
2908 | /* | |
2909 | * Step 1: decay old *_sum if we crossed period boundaries. | |
2910 | */ | |
2911 | if (periods) { | |
2912 | sa->load_sum = decay_load(sa->load_sum, periods); | |
2913 | if (cfs_rq) { | |
2914 | cfs_rq->runnable_load_sum = | |
2915 | decay_load(cfs_rq->runnable_load_sum, periods); | |
2916 | } | |
2917 | sa->util_sum = decay_load((u64)(sa->util_sum), periods); | |
a481db34 | 2918 | |
05296e75 PZ |
2919 | /* |
2920 | * Step 2 | |
2921 | */ | |
2922 | delta %= 1024; | |
2923 | contrib = __accumulate_pelt_segments(periods, | |
2924 | 1024 - sa->period_contrib, delta); | |
2925 | } | |
a481db34 YD |
2926 | sa->period_contrib = delta; |
2927 | ||
2928 | contrib = cap_scale(contrib, scale_freq); | |
2929 | if (weight) { | |
2930 | sa->load_sum += weight * contrib; | |
2931 | if (cfs_rq) | |
2932 | cfs_rq->runnable_load_sum += weight * contrib; | |
2933 | } | |
2934 | if (running) | |
2935 | sa->util_sum += contrib * scale_cpu; | |
2936 | ||
2937 | return periods; | |
2938 | } | |
2939 | ||
9d85f21c PT |
2940 | /* |
2941 | * We can represent the historical contribution to runnable average as the | |
2942 | * coefficients of a geometric series. To do this we sub-divide our runnable | |
2943 | * history into segments of approximately 1ms (1024us); label the segment that | |
2944 | * occurred N-ms ago p_N, with p_0 corresponding to the current period, e.g. | |
2945 | * | |
2946 | * [<- 1024us ->|<- 1024us ->|<- 1024us ->| ... | |
2947 | * p0 p1 p2 | |
2948 | * (now) (~1ms ago) (~2ms ago) | |
2949 | * | |
2950 | * Let u_i denote the fraction of p_i that the entity was runnable. | |
2951 | * | |
2952 | * We then designate the fractions u_i as our co-efficients, yielding the | |
2953 | * following representation of historical load: | |
2954 | * u_0 + u_1*y + u_2*y^2 + u_3*y^3 + ... | |
2955 | * | |
2956 | * We choose y based on the with of a reasonably scheduling period, fixing: | |
2957 | * y^32 = 0.5 | |
2958 | * | |
2959 | * This means that the contribution to load ~32ms ago (u_32) will be weighted | |
2960 | * approximately half as much as the contribution to load within the last ms | |
2961 | * (u_0). | |
2962 | * | |
2963 | * When a period "rolls over" and we have new u_0`, multiplying the previous | |
2964 | * sum again by y is sufficient to update: | |
2965 | * load_avg = u_0` + y*(u_0 + u_1*y + u_2*y^2 + ... ) | |
2966 | * = u_0 + u_1*y + u_2*y^2 + ... [re-labeling u_i --> u_{i+1}] | |
2967 | */ | |
9d89c257 | 2968 | static __always_inline int |
0ccb977f | 2969 | ___update_load_avg(u64 now, int cpu, struct sched_avg *sa, |
13962234 | 2970 | unsigned long weight, int running, struct cfs_rq *cfs_rq) |
9d85f21c | 2971 | { |
a481db34 | 2972 | u64 delta; |
9d85f21c | 2973 | |
9d89c257 | 2974 | delta = now - sa->last_update_time; |
9d85f21c PT |
2975 | /* |
2976 | * This should only happen when time goes backwards, which it | |
2977 | * unfortunately does during sched clock init when we swap over to TSC. | |
2978 | */ | |
2979 | if ((s64)delta < 0) { | |
9d89c257 | 2980 | sa->last_update_time = now; |
9d85f21c PT |
2981 | return 0; |
2982 | } | |
2983 | ||
2984 | /* | |
2985 | * Use 1024ns as the unit of measurement since it's a reasonable | |
2986 | * approximation of 1us and fast to compute. | |
2987 | */ | |
2988 | delta >>= 10; | |
2989 | if (!delta) | |
2990 | return 0; | |
bb0bd044 PZ |
2991 | |
2992 | sa->last_update_time += delta << 10; | |
9d85f21c | 2993 | |
f235a54f VG |
2994 | /* |
2995 | * running is a subset of runnable (weight) so running can't be set if | |
2996 | * runnable is clear. But there are some corner cases where the current | |
2997 | * se has been already dequeued but cfs_rq->curr still points to it. | |
2998 | * This means that weight will be 0 but not running for a sched_entity | |
2999 | * but also for a cfs_rq if the latter becomes idle. As an example, | |
3000 | * this happens during idle_balance() which calls | |
3001 | * update_blocked_averages() | |
3002 | */ | |
3003 | if (!weight) | |
3004 | running = 0; | |
3005 | ||
a481db34 YD |
3006 | /* |
3007 | * Now we know we crossed measurement unit boundaries. The *_avg | |
3008 | * accrues by two steps: | |
3009 | * | |
3010 | * Step 1: accumulate *_sum since last_update_time. If we haven't | |
3011 | * crossed period boundaries, finish. | |
3012 | */ | |
3013 | if (!accumulate_sum(delta, cpu, sa, weight, running, cfs_rq)) | |
3014 | return 0; | |
9ee474f5 | 3015 | |
a481db34 YD |
3016 | /* |
3017 | * Step 2: update *_avg. | |
3018 | */ | |
625ed2bf | 3019 | sa->load_avg = div_u64(sa->load_sum, LOAD_AVG_MAX - 1024 + sa->period_contrib); |
a481db34 YD |
3020 | if (cfs_rq) { |
3021 | cfs_rq->runnable_load_avg = | |
625ed2bf | 3022 | div_u64(cfs_rq->runnable_load_sum, LOAD_AVG_MAX - 1024 + sa->period_contrib); |
9d89c257 | 3023 | } |
625ed2bf | 3024 | sa->util_avg = sa->util_sum / (LOAD_AVG_MAX - 1024 + sa->period_contrib); |
aff3e498 | 3025 | |
a481db34 | 3026 | return 1; |
9ee474f5 PT |
3027 | } |
3028 | ||
0ccb977f PZ |
3029 | static int |
3030 | __update_load_avg_blocked_se(u64 now, int cpu, struct sched_entity *se) | |
3031 | { | |
3032 | return ___update_load_avg(now, cpu, &se->avg, 0, 0, NULL); | |
3033 | } | |
3034 | ||
3035 | static int | |
3036 | __update_load_avg_se(u64 now, int cpu, struct cfs_rq *cfs_rq, struct sched_entity *se) | |
3037 | { | |
3038 | return ___update_load_avg(now, cpu, &se->avg, | |
3039 | se->on_rq * scale_load_down(se->load.weight), | |
3040 | cfs_rq->curr == se, NULL); | |
3041 | } | |
3042 | ||
3043 | static int | |
3044 | __update_load_avg_cfs_rq(u64 now, int cpu, struct cfs_rq *cfs_rq) | |
3045 | { | |
3046 | return ___update_load_avg(now, cpu, &cfs_rq->avg, | |
3047 | scale_load_down(cfs_rq->load.weight), | |
3048 | cfs_rq->curr != NULL, cfs_rq); | |
3049 | } | |
3050 | ||
09a43ace VG |
3051 | /* |
3052 | * Signed add and clamp on underflow. | |
3053 | * | |
3054 | * Explicitly do a load-store to ensure the intermediate value never hits | |
3055 | * memory. This allows lockless observations without ever seeing the negative | |
3056 | * values. | |
3057 | */ | |
3058 | #define add_positive(_ptr, _val) do { \ | |
3059 | typeof(_ptr) ptr = (_ptr); \ | |
3060 | typeof(_val) val = (_val); \ | |
3061 | typeof(*ptr) res, var = READ_ONCE(*ptr); \ | |
3062 | \ | |
3063 | res = var + val; \ | |
3064 | \ | |
3065 | if (val < 0 && res > var) \ | |
3066 | res = 0; \ | |
3067 | \ | |
3068 | WRITE_ONCE(*ptr, res); \ | |
3069 | } while (0) | |
3070 | ||
c566e8e9 | 3071 | #ifdef CONFIG_FAIR_GROUP_SCHED |
7c3edd2c PZ |
3072 | /** |
3073 | * update_tg_load_avg - update the tg's load avg | |
3074 | * @cfs_rq: the cfs_rq whose avg changed | |
3075 | * @force: update regardless of how small the difference | |
3076 | * | |
3077 | * This function 'ensures': tg->load_avg := \Sum tg->cfs_rq[]->avg.load. | |
3078 | * However, because tg->load_avg is a global value there are performance | |
3079 | * considerations. | |
3080 | * | |
3081 | * In order to avoid having to look at the other cfs_rq's, we use a | |
3082 | * differential update where we store the last value we propagated. This in | |
3083 | * turn allows skipping updates if the differential is 'small'. | |
3084 | * | |
815abf5a | 3085 | * Updating tg's load_avg is necessary before update_cfs_share(). |
bb17f655 | 3086 | */ |
9d89c257 | 3087 | static inline void update_tg_load_avg(struct cfs_rq *cfs_rq, int force) |
bb17f655 | 3088 | { |
9d89c257 | 3089 | long delta = cfs_rq->avg.load_avg - cfs_rq->tg_load_avg_contrib; |
bb17f655 | 3090 | |
aa0b7ae0 WL |
3091 | /* |
3092 | * No need to update load_avg for root_task_group as it is not used. | |
3093 | */ | |
3094 | if (cfs_rq->tg == &root_task_group) | |
3095 | return; | |
3096 | ||
9d89c257 YD |
3097 | if (force || abs(delta) > cfs_rq->tg_load_avg_contrib / 64) { |
3098 | atomic_long_add(delta, &cfs_rq->tg->load_avg); | |
3099 | cfs_rq->tg_load_avg_contrib = cfs_rq->avg.load_avg; | |
bb17f655 | 3100 | } |
8165e145 | 3101 | } |
f5f9739d | 3102 | |
ad936d86 BP |
3103 | /* |
3104 | * Called within set_task_rq() right before setting a task's cpu. The | |
3105 | * caller only guarantees p->pi_lock is held; no other assumptions, | |
3106 | * including the state of rq->lock, should be made. | |
3107 | */ | |
3108 | void set_task_rq_fair(struct sched_entity *se, | |
3109 | struct cfs_rq *prev, struct cfs_rq *next) | |
3110 | { | |
0ccb977f PZ |
3111 | u64 p_last_update_time; |
3112 | u64 n_last_update_time; | |
3113 | ||
ad936d86 BP |
3114 | if (!sched_feat(ATTACH_AGE_LOAD)) |
3115 | return; | |
3116 | ||
3117 | /* | |
3118 | * We are supposed to update the task to "current" time, then its up to | |
3119 | * date and ready to go to new CPU/cfs_rq. But we have difficulty in | |
3120 | * getting what current time is, so simply throw away the out-of-date | |
3121 | * time. This will result in the wakee task is less decayed, but giving | |
3122 | * the wakee more load sounds not bad. | |
3123 | */ | |
0ccb977f PZ |
3124 | if (!(se->avg.last_update_time && prev)) |
3125 | return; | |
ad936d86 BP |
3126 | |
3127 | #ifndef CONFIG_64BIT | |
0ccb977f | 3128 | { |
ad936d86 BP |
3129 | u64 p_last_update_time_copy; |
3130 | u64 n_last_update_time_copy; | |
3131 | ||
3132 | do { | |
3133 | p_last_update_time_copy = prev->load_last_update_time_copy; | |
3134 | n_last_update_time_copy = next->load_last_update_time_copy; | |
3135 | ||
3136 | smp_rmb(); | |
3137 | ||
3138 | p_last_update_time = prev->avg.last_update_time; | |
3139 | n_last_update_time = next->avg.last_update_time; | |
3140 | ||
3141 | } while (p_last_update_time != p_last_update_time_copy || | |
3142 | n_last_update_time != n_last_update_time_copy); | |
0ccb977f | 3143 | } |
ad936d86 | 3144 | #else |
0ccb977f PZ |
3145 | p_last_update_time = prev->avg.last_update_time; |
3146 | n_last_update_time = next->avg.last_update_time; | |
ad936d86 | 3147 | #endif |
0ccb977f PZ |
3148 | __update_load_avg_blocked_se(p_last_update_time, cpu_of(rq_of(prev)), se); |
3149 | se->avg.last_update_time = n_last_update_time; | |
ad936d86 | 3150 | } |
09a43ace VG |
3151 | |
3152 | /* Take into account change of utilization of a child task group */ | |
3153 | static inline void | |
3154 | update_tg_cfs_util(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
3155 | { | |
3156 | struct cfs_rq *gcfs_rq = group_cfs_rq(se); | |
3157 | long delta = gcfs_rq->avg.util_avg - se->avg.util_avg; | |
3158 | ||
3159 | /* Nothing to update */ | |
3160 | if (!delta) | |
3161 | return; | |
3162 | ||
3163 | /* Set new sched_entity's utilization */ | |
3164 | se->avg.util_avg = gcfs_rq->avg.util_avg; | |
3165 | se->avg.util_sum = se->avg.util_avg * LOAD_AVG_MAX; | |
3166 | ||
3167 | /* Update parent cfs_rq utilization */ | |
3168 | add_positive(&cfs_rq->avg.util_avg, delta); | |
3169 | cfs_rq->avg.util_sum = cfs_rq->avg.util_avg * LOAD_AVG_MAX; | |
3170 | } | |
3171 | ||
3172 | /* Take into account change of load of a child task group */ | |
3173 | static inline void | |
3174 | update_tg_cfs_load(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
3175 | { | |
3176 | struct cfs_rq *gcfs_rq = group_cfs_rq(se); | |
3177 | long delta, load = gcfs_rq->avg.load_avg; | |
3178 | ||
3179 | /* | |
3180 | * If the load of group cfs_rq is null, the load of the | |
3181 | * sched_entity will also be null so we can skip the formula | |
3182 | */ | |
3183 | if (load) { | |
3184 | long tg_load; | |
3185 | ||
3186 | /* Get tg's load and ensure tg_load > 0 */ | |
3187 | tg_load = atomic_long_read(&gcfs_rq->tg->load_avg) + 1; | |
3188 | ||
3189 | /* Ensure tg_load >= load and updated with current load*/ | |
3190 | tg_load -= gcfs_rq->tg_load_avg_contrib; | |
3191 | tg_load += load; | |
3192 | ||
3193 | /* | |
3194 | * We need to compute a correction term in the case that the | |
3195 | * task group is consuming more CPU than a task of equal | |
3196 | * weight. A task with a weight equals to tg->shares will have | |
3197 | * a load less or equal to scale_load_down(tg->shares). | |
3198 | * Similarly, the sched_entities that represent the task group | |
3199 | * at parent level, can't have a load higher than | |
3200 | * scale_load_down(tg->shares). And the Sum of sched_entities' | |
3201 | * load must be <= scale_load_down(tg->shares). | |
3202 | */ | |
3203 | if (tg_load > scale_load_down(gcfs_rq->tg->shares)) { | |
3204 | /* scale gcfs_rq's load into tg's shares*/ | |
3205 | load *= scale_load_down(gcfs_rq->tg->shares); | |
3206 | load /= tg_load; | |
3207 | } | |
3208 | } | |
3209 | ||
3210 | delta = load - se->avg.load_avg; | |
3211 | ||
3212 | /* Nothing to update */ | |
3213 | if (!delta) | |
3214 | return; | |
3215 | ||
3216 | /* Set new sched_entity's load */ | |
3217 | se->avg.load_avg = load; | |
3218 | se->avg.load_sum = se->avg.load_avg * LOAD_AVG_MAX; | |
3219 | ||
3220 | /* Update parent cfs_rq load */ | |
3221 | add_positive(&cfs_rq->avg.load_avg, delta); | |
3222 | cfs_rq->avg.load_sum = cfs_rq->avg.load_avg * LOAD_AVG_MAX; | |
3223 | ||
3224 | /* | |
3225 | * If the sched_entity is already enqueued, we also have to update the | |
3226 | * runnable load avg. | |
3227 | */ | |
3228 | if (se->on_rq) { | |
3229 | /* Update parent cfs_rq runnable_load_avg */ | |
3230 | add_positive(&cfs_rq->runnable_load_avg, delta); | |
3231 | cfs_rq->runnable_load_sum = cfs_rq->runnable_load_avg * LOAD_AVG_MAX; | |
3232 | } | |
3233 | } | |
3234 | ||
3235 | static inline void set_tg_cfs_propagate(struct cfs_rq *cfs_rq) | |
3236 | { | |
3237 | cfs_rq->propagate_avg = 1; | |
3238 | } | |
3239 | ||
3240 | static inline int test_and_clear_tg_cfs_propagate(struct sched_entity *se) | |
3241 | { | |
3242 | struct cfs_rq *cfs_rq = group_cfs_rq(se); | |
3243 | ||
3244 | if (!cfs_rq->propagate_avg) | |
3245 | return 0; | |
3246 | ||
3247 | cfs_rq->propagate_avg = 0; | |
3248 | return 1; | |
3249 | } | |
3250 | ||
3251 | /* Update task and its cfs_rq load average */ | |
3252 | static inline int propagate_entity_load_avg(struct sched_entity *se) | |
3253 | { | |
3254 | struct cfs_rq *cfs_rq; | |
3255 | ||
3256 | if (entity_is_task(se)) | |
3257 | return 0; | |
3258 | ||
3259 | if (!test_and_clear_tg_cfs_propagate(se)) | |
3260 | return 0; | |
3261 | ||
3262 | cfs_rq = cfs_rq_of(se); | |
3263 | ||
3264 | set_tg_cfs_propagate(cfs_rq); | |
3265 | ||
3266 | update_tg_cfs_util(cfs_rq, se); | |
3267 | update_tg_cfs_load(cfs_rq, se); | |
3268 | ||
3269 | return 1; | |
3270 | } | |
3271 | ||
bc427898 VG |
3272 | /* |
3273 | * Check if we need to update the load and the utilization of a blocked | |
3274 | * group_entity: | |
3275 | */ | |
3276 | static inline bool skip_blocked_update(struct sched_entity *se) | |
3277 | { | |
3278 | struct cfs_rq *gcfs_rq = group_cfs_rq(se); | |
3279 | ||
3280 | /* | |
3281 | * If sched_entity still have not zero load or utilization, we have to | |
3282 | * decay it: | |
3283 | */ | |
3284 | if (se->avg.load_avg || se->avg.util_avg) | |
3285 | return false; | |
3286 | ||
3287 | /* | |
3288 | * If there is a pending propagation, we have to update the load and | |
3289 | * the utilization of the sched_entity: | |
3290 | */ | |
3291 | if (gcfs_rq->propagate_avg) | |
3292 | return false; | |
3293 | ||
3294 | /* | |
3295 | * Otherwise, the load and the utilization of the sched_entity is | |
3296 | * already zero and there is no pending propagation, so it will be a | |
3297 | * waste of time to try to decay it: | |
3298 | */ | |
3299 | return true; | |
3300 | } | |
3301 | ||
6e83125c | 3302 | #else /* CONFIG_FAIR_GROUP_SCHED */ |
09a43ace | 3303 | |
9d89c257 | 3304 | static inline void update_tg_load_avg(struct cfs_rq *cfs_rq, int force) {} |
09a43ace VG |
3305 | |
3306 | static inline int propagate_entity_load_avg(struct sched_entity *se) | |
3307 | { | |
3308 | return 0; | |
3309 | } | |
3310 | ||
3311 | static inline void set_tg_cfs_propagate(struct cfs_rq *cfs_rq) {} | |
3312 | ||
6e83125c | 3313 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
c566e8e9 | 3314 | |
89741892 PZ |
3315 | /* |
3316 | * Unsigned subtract and clamp on underflow. | |
3317 | * | |
3318 | * Explicitly do a load-store to ensure the intermediate value never hits | |
3319 | * memory. This allows lockless observations without ever seeing the negative | |
3320 | * values. | |
3321 | */ | |
3322 | #define sub_positive(_ptr, _val) do { \ | |
3323 | typeof(_ptr) ptr = (_ptr); \ | |
3324 | typeof(*ptr) val = (_val); \ | |
3325 | typeof(*ptr) res, var = READ_ONCE(*ptr); \ | |
3326 | res = var - val; \ | |
3327 | if (res > var) \ | |
3328 | res = 0; \ | |
3329 | WRITE_ONCE(*ptr, res); \ | |
3330 | } while (0) | |
3331 | ||
3d30544f PZ |
3332 | /** |
3333 | * update_cfs_rq_load_avg - update the cfs_rq's load/util averages | |
3334 | * @now: current time, as per cfs_rq_clock_task() | |
3335 | * @cfs_rq: cfs_rq to update | |
3d30544f PZ |
3336 | * |
3337 | * The cfs_rq avg is the direct sum of all its entities (blocked and runnable) | |
3338 | * avg. The immediate corollary is that all (fair) tasks must be attached, see | |
3339 | * post_init_entity_util_avg(). | |
3340 | * | |
3341 | * cfs_rq->avg is used for task_h_load() and update_cfs_share() for example. | |
3342 | * | |
7c3edd2c PZ |
3343 | * Returns true if the load decayed or we removed load. |
3344 | * | |
3345 | * Since both these conditions indicate a changed cfs_rq->avg.load we should | |
3346 | * call update_tg_load_avg() when this function returns true. | |
3d30544f | 3347 | */ |
a2c6c91f | 3348 | static inline int |
3a123bbb | 3349 | update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq) |
2dac754e | 3350 | { |
9d89c257 | 3351 | struct sched_avg *sa = &cfs_rq->avg; |
41e0d37f | 3352 | int decayed, removed_load = 0, removed_util = 0; |
2dac754e | 3353 | |
9d89c257 | 3354 | if (atomic_long_read(&cfs_rq->removed_load_avg)) { |
9e0e83a1 | 3355 | s64 r = atomic_long_xchg(&cfs_rq->removed_load_avg, 0); |
89741892 PZ |
3356 | sub_positive(&sa->load_avg, r); |
3357 | sub_positive(&sa->load_sum, r * LOAD_AVG_MAX); | |
41e0d37f | 3358 | removed_load = 1; |
4e516076 | 3359 | set_tg_cfs_propagate(cfs_rq); |
8165e145 | 3360 | } |
2dac754e | 3361 | |
9d89c257 YD |
3362 | if (atomic_long_read(&cfs_rq->removed_util_avg)) { |
3363 | long r = atomic_long_xchg(&cfs_rq->removed_util_avg, 0); | |
89741892 PZ |
3364 | sub_positive(&sa->util_avg, r); |
3365 | sub_positive(&sa->util_sum, r * LOAD_AVG_MAX); | |
41e0d37f | 3366 | removed_util = 1; |
4e516076 | 3367 | set_tg_cfs_propagate(cfs_rq); |
9d89c257 | 3368 | } |
36ee28e4 | 3369 | |
0ccb977f | 3370 | decayed = __update_load_avg_cfs_rq(now, cpu_of(rq_of(cfs_rq)), cfs_rq); |
36ee28e4 | 3371 | |
9d89c257 YD |
3372 | #ifndef CONFIG_64BIT |
3373 | smp_wmb(); | |
3374 | cfs_rq->load_last_update_time_copy = sa->last_update_time; | |
3375 | #endif | |
36ee28e4 | 3376 | |
3a123bbb | 3377 | if (decayed || removed_util) |
a2c6c91f | 3378 | cfs_rq_util_change(cfs_rq); |
21e96f88 | 3379 | |
41e0d37f | 3380 | return decayed || removed_load; |
21e96f88 SM |
3381 | } |
3382 | ||
d31b1a66 VG |
3383 | /* |
3384 | * Optional action to be done while updating the load average | |
3385 | */ | |
3386 | #define UPDATE_TG 0x1 | |
3387 | #define SKIP_AGE_LOAD 0x2 | |
3388 | ||
21e96f88 | 3389 | /* Update task and its cfs_rq load average */ |
d31b1a66 | 3390 | static inline void update_load_avg(struct sched_entity *se, int flags) |
21e96f88 SM |
3391 | { |
3392 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
3393 | u64 now = cfs_rq_clock_task(cfs_rq); | |
3394 | struct rq *rq = rq_of(cfs_rq); | |
3395 | int cpu = cpu_of(rq); | |
09a43ace | 3396 | int decayed; |
21e96f88 SM |
3397 | |
3398 | /* | |
3399 | * Track task load average for carrying it to new CPU after migrated, and | |
3400 | * track group sched_entity load average for task_h_load calc in migration | |
3401 | */ | |
0ccb977f PZ |
3402 | if (se->avg.last_update_time && !(flags & SKIP_AGE_LOAD)) |
3403 | __update_load_avg_se(now, cpu, cfs_rq, se); | |
21e96f88 | 3404 | |
3a123bbb | 3405 | decayed = update_cfs_rq_load_avg(now, cfs_rq); |
09a43ace VG |
3406 | decayed |= propagate_entity_load_avg(se); |
3407 | ||
3408 | if (decayed && (flags & UPDATE_TG)) | |
21e96f88 | 3409 | update_tg_load_avg(cfs_rq, 0); |
9ee474f5 PT |
3410 | } |
3411 | ||
3d30544f PZ |
3412 | /** |
3413 | * attach_entity_load_avg - attach this entity to its cfs_rq load avg | |
3414 | * @cfs_rq: cfs_rq to attach to | |
3415 | * @se: sched_entity to attach | |
3416 | * | |
3417 | * Must call update_cfs_rq_load_avg() before this, since we rely on | |
3418 | * cfs_rq->avg.last_update_time being current. | |
3419 | */ | |
a05e8c51 BP |
3420 | static void attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) |
3421 | { | |
3422 | se->avg.last_update_time = cfs_rq->avg.last_update_time; | |
3423 | cfs_rq->avg.load_avg += se->avg.load_avg; | |
3424 | cfs_rq->avg.load_sum += se->avg.load_sum; | |
3425 | cfs_rq->avg.util_avg += se->avg.util_avg; | |
3426 | cfs_rq->avg.util_sum += se->avg.util_sum; | |
09a43ace | 3427 | set_tg_cfs_propagate(cfs_rq); |
a2c6c91f SM |
3428 | |
3429 | cfs_rq_util_change(cfs_rq); | |
a05e8c51 BP |
3430 | } |
3431 | ||
3d30544f PZ |
3432 | /** |
3433 | * detach_entity_load_avg - detach this entity from its cfs_rq load avg | |
3434 | * @cfs_rq: cfs_rq to detach from | |
3435 | * @se: sched_entity to detach | |
3436 | * | |
3437 | * Must call update_cfs_rq_load_avg() before this, since we rely on | |
3438 | * cfs_rq->avg.last_update_time being current. | |
3439 | */ | |
a05e8c51 BP |
3440 | static void detach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) |
3441 | { | |
a05e8c51 | 3442 | |
89741892 PZ |
3443 | sub_positive(&cfs_rq->avg.load_avg, se->avg.load_avg); |
3444 | sub_positive(&cfs_rq->avg.load_sum, se->avg.load_sum); | |
3445 | sub_positive(&cfs_rq->avg.util_avg, se->avg.util_avg); | |
3446 | sub_positive(&cfs_rq->avg.util_sum, se->avg.util_sum); | |
09a43ace | 3447 | set_tg_cfs_propagate(cfs_rq); |
a2c6c91f SM |
3448 | |
3449 | cfs_rq_util_change(cfs_rq); | |
a05e8c51 BP |
3450 | } |
3451 | ||
9d89c257 YD |
3452 | /* Add the load generated by se into cfs_rq's load average */ |
3453 | static inline void | |
3454 | enqueue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
9ee474f5 | 3455 | { |
9d89c257 | 3456 | struct sched_avg *sa = &se->avg; |
18bf2805 | 3457 | |
13962234 YD |
3458 | cfs_rq->runnable_load_avg += sa->load_avg; |
3459 | cfs_rq->runnable_load_sum += sa->load_sum; | |
3460 | ||
d31b1a66 | 3461 | if (!sa->last_update_time) { |
a05e8c51 | 3462 | attach_entity_load_avg(cfs_rq, se); |
9d89c257 | 3463 | update_tg_load_avg(cfs_rq, 0); |
d31b1a66 | 3464 | } |
2dac754e PT |
3465 | } |
3466 | ||
13962234 YD |
3467 | /* Remove the runnable load generated by se from cfs_rq's runnable load average */ |
3468 | static inline void | |
3469 | dequeue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) | |
3470 | { | |
13962234 YD |
3471 | cfs_rq->runnable_load_avg = |
3472 | max_t(long, cfs_rq->runnable_load_avg - se->avg.load_avg, 0); | |
3473 | cfs_rq->runnable_load_sum = | |
a05e8c51 | 3474 | max_t(s64, cfs_rq->runnable_load_sum - se->avg.load_sum, 0); |
13962234 YD |
3475 | } |
3476 | ||
9d89c257 | 3477 | #ifndef CONFIG_64BIT |
0905f04e YD |
3478 | static inline u64 cfs_rq_last_update_time(struct cfs_rq *cfs_rq) |
3479 | { | |
9d89c257 | 3480 | u64 last_update_time_copy; |
0905f04e | 3481 | u64 last_update_time; |
9ee474f5 | 3482 | |
9d89c257 YD |
3483 | do { |
3484 | last_update_time_copy = cfs_rq->load_last_update_time_copy; | |
3485 | smp_rmb(); | |
3486 | last_update_time = cfs_rq->avg.last_update_time; | |
3487 | } while (last_update_time != last_update_time_copy); | |
0905f04e YD |
3488 | |
3489 | return last_update_time; | |
3490 | } | |
9d89c257 | 3491 | #else |
0905f04e YD |
3492 | static inline u64 cfs_rq_last_update_time(struct cfs_rq *cfs_rq) |
3493 | { | |
3494 | return cfs_rq->avg.last_update_time; | |
3495 | } | |
9d89c257 YD |
3496 | #endif |
3497 | ||
104cb16d MR |
3498 | /* |
3499 | * Synchronize entity load avg of dequeued entity without locking | |
3500 | * the previous rq. | |
3501 | */ | |
3502 | void sync_entity_load_avg(struct sched_entity *se) | |
3503 | { | |
3504 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
3505 | u64 last_update_time; | |
3506 | ||
3507 | last_update_time = cfs_rq_last_update_time(cfs_rq); | |
0ccb977f | 3508 | __update_load_avg_blocked_se(last_update_time, cpu_of(rq_of(cfs_rq)), se); |
104cb16d MR |
3509 | } |
3510 | ||
0905f04e YD |
3511 | /* |
3512 | * Task first catches up with cfs_rq, and then subtract | |
3513 | * itself from the cfs_rq (task must be off the queue now). | |
3514 | */ | |
3515 | void remove_entity_load_avg(struct sched_entity *se) | |
3516 | { | |
3517 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
0905f04e YD |
3518 | |
3519 | /* | |
7dc603c9 PZ |
3520 | * tasks cannot exit without having gone through wake_up_new_task() -> |
3521 | * post_init_entity_util_avg() which will have added things to the | |
3522 | * cfs_rq, so we can remove unconditionally. | |
3523 | * | |
3524 | * Similarly for groups, they will have passed through | |
3525 | * post_init_entity_util_avg() before unregister_sched_fair_group() | |
3526 | * calls this. | |
0905f04e | 3527 | */ |
0905f04e | 3528 | |
104cb16d | 3529 | sync_entity_load_avg(se); |
9d89c257 YD |
3530 | atomic_long_add(se->avg.load_avg, &cfs_rq->removed_load_avg); |
3531 | atomic_long_add(se->avg.util_avg, &cfs_rq->removed_util_avg); | |
2dac754e | 3532 | } |
642dbc39 | 3533 | |
7ea241af YD |
3534 | static inline unsigned long cfs_rq_runnable_load_avg(struct cfs_rq *cfs_rq) |
3535 | { | |
3536 | return cfs_rq->runnable_load_avg; | |
3537 | } | |
3538 | ||
3539 | static inline unsigned long cfs_rq_load_avg(struct cfs_rq *cfs_rq) | |
3540 | { | |
3541 | return cfs_rq->avg.load_avg; | |
3542 | } | |
3543 | ||
46f69fa3 | 3544 | static int idle_balance(struct rq *this_rq, struct rq_flags *rf); |
6e83125c | 3545 | |
38033c37 PZ |
3546 | #else /* CONFIG_SMP */ |
3547 | ||
01011473 | 3548 | static inline int |
3a123bbb | 3549 | update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq) |
01011473 PZ |
3550 | { |
3551 | return 0; | |
3552 | } | |
3553 | ||
d31b1a66 VG |
3554 | #define UPDATE_TG 0x0 |
3555 | #define SKIP_AGE_LOAD 0x0 | |
3556 | ||
3557 | static inline void update_load_avg(struct sched_entity *se, int not_used1) | |
536bd00c | 3558 | { |
a030d738 | 3559 | cfs_rq_util_change(cfs_rq_of(se)); |
536bd00c RW |
3560 | } |
3561 | ||
9d89c257 YD |
3562 | static inline void |
3563 | enqueue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {} | |
13962234 YD |
3564 | static inline void |
3565 | dequeue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {} | |
9d89c257 | 3566 | static inline void remove_entity_load_avg(struct sched_entity *se) {} |
6e83125c | 3567 | |
a05e8c51 BP |
3568 | static inline void |
3569 | attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {} | |
3570 | static inline void | |
3571 | detach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {} | |
3572 | ||
46f69fa3 | 3573 | static inline int idle_balance(struct rq *rq, struct rq_flags *rf) |
6e83125c PZ |
3574 | { |
3575 | return 0; | |
3576 | } | |
3577 | ||
38033c37 | 3578 | #endif /* CONFIG_SMP */ |
9d85f21c | 3579 | |
ddc97297 PZ |
3580 | static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se) |
3581 | { | |
3582 | #ifdef CONFIG_SCHED_DEBUG | |
3583 | s64 d = se->vruntime - cfs_rq->min_vruntime; | |
3584 | ||
3585 | if (d < 0) | |
3586 | d = -d; | |
3587 | ||
3588 | if (d > 3*sysctl_sched_latency) | |
ae92882e | 3589 | schedstat_inc(cfs_rq->nr_spread_over); |
ddc97297 PZ |
3590 | #endif |
3591 | } | |
3592 | ||
aeb73b04 PZ |
3593 | static void |
3594 | place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial) | |
3595 | { | |
1af5f730 | 3596 | u64 vruntime = cfs_rq->min_vruntime; |
94dfb5e7 | 3597 | |
2cb8600e PZ |
3598 | /* |
3599 | * The 'current' period is already promised to the current tasks, | |
3600 | * however the extra weight of the new task will slow them down a | |
3601 | * little, place the new task so that it fits in the slot that | |
3602 | * stays open at the end. | |
3603 | */ | |
94dfb5e7 | 3604 | if (initial && sched_feat(START_DEBIT)) |
f9c0b095 | 3605 | vruntime += sched_vslice(cfs_rq, se); |
aeb73b04 | 3606 | |
a2e7a7eb | 3607 | /* sleeps up to a single latency don't count. */ |
5ca9880c | 3608 | if (!initial) { |
a2e7a7eb | 3609 | unsigned long thresh = sysctl_sched_latency; |
a7be37ac | 3610 | |
a2e7a7eb MG |
3611 | /* |
3612 | * Halve their sleep time's effect, to allow | |
3613 | * for a gentler effect of sleepers: | |
3614 | */ | |
3615 | if (sched_feat(GENTLE_FAIR_SLEEPERS)) | |
3616 | thresh >>= 1; | |
51e0304c | 3617 | |
a2e7a7eb | 3618 | vruntime -= thresh; |
aeb73b04 PZ |
3619 | } |
3620 | ||
b5d9d734 | 3621 | /* ensure we never gain time by being placed backwards. */ |
16c8f1c7 | 3622 | se->vruntime = max_vruntime(se->vruntime, vruntime); |
aeb73b04 PZ |
3623 | } |
3624 | ||
d3d9dc33 PT |
3625 | static void check_enqueue_throttle(struct cfs_rq *cfs_rq); |
3626 | ||
cb251765 MG |
3627 | static inline void check_schedstat_required(void) |
3628 | { | |
3629 | #ifdef CONFIG_SCHEDSTATS | |
3630 | if (schedstat_enabled()) | |
3631 | return; | |
3632 | ||
3633 | /* Force schedstat enabled if a dependent tracepoint is active */ | |
3634 | if (trace_sched_stat_wait_enabled() || | |
3635 | trace_sched_stat_sleep_enabled() || | |
3636 | trace_sched_stat_iowait_enabled() || | |
3637 | trace_sched_stat_blocked_enabled() || | |
3638 | trace_sched_stat_runtime_enabled()) { | |
eda8dca5 | 3639 | printk_deferred_once("Scheduler tracepoints stat_sleep, stat_iowait, " |
cb251765 | 3640 | "stat_blocked and stat_runtime require the " |
f67abed5 | 3641 | "kernel parameter schedstats=enable or " |
cb251765 MG |
3642 | "kernel.sched_schedstats=1\n"); |
3643 | } | |
3644 | #endif | |
3645 | } | |
3646 | ||
b5179ac7 PZ |
3647 | |
3648 | /* | |
3649 | * MIGRATION | |
3650 | * | |
3651 | * dequeue | |
3652 | * update_curr() | |
3653 | * update_min_vruntime() | |
3654 | * vruntime -= min_vruntime | |
3655 | * | |
3656 | * enqueue | |
3657 | * update_curr() | |
3658 | * update_min_vruntime() | |
3659 | * vruntime += min_vruntime | |
3660 | * | |
3661 | * this way the vruntime transition between RQs is done when both | |
3662 | * min_vruntime are up-to-date. | |
3663 | * | |
3664 | * WAKEUP (remote) | |
3665 | * | |
59efa0ba | 3666 | * ->migrate_task_rq_fair() (p->state == TASK_WAKING) |
b5179ac7 PZ |
3667 | * vruntime -= min_vruntime |
3668 | * | |
3669 | * enqueue | |
3670 | * update_curr() | |
3671 | * update_min_vruntime() | |
3672 | * vruntime += min_vruntime | |
3673 | * | |
3674 | * this way we don't have the most up-to-date min_vruntime on the originating | |
3675 | * CPU and an up-to-date min_vruntime on the destination CPU. | |
3676 | */ | |
3677 | ||
bf0f6f24 | 3678 | static void |
88ec22d3 | 3679 | enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
bf0f6f24 | 3680 | { |
2f950354 PZ |
3681 | bool renorm = !(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_MIGRATED); |
3682 | bool curr = cfs_rq->curr == se; | |
3683 | ||
88ec22d3 | 3684 | /* |
2f950354 PZ |
3685 | * If we're the current task, we must renormalise before calling |
3686 | * update_curr(). | |
88ec22d3 | 3687 | */ |
2f950354 | 3688 | if (renorm && curr) |
88ec22d3 PZ |
3689 | se->vruntime += cfs_rq->min_vruntime; |
3690 | ||
2f950354 PZ |
3691 | update_curr(cfs_rq); |
3692 | ||
bf0f6f24 | 3693 | /* |
2f950354 PZ |
3694 | * Otherwise, renormalise after, such that we're placed at the current |
3695 | * moment in time, instead of some random moment in the past. Being | |
3696 | * placed in the past could significantly boost this task to the | |
3697 | * fairness detriment of existing tasks. | |
bf0f6f24 | 3698 | */ |
2f950354 PZ |
3699 | if (renorm && !curr) |
3700 | se->vruntime += cfs_rq->min_vruntime; | |
3701 | ||
89ee048f VG |
3702 | /* |
3703 | * When enqueuing a sched_entity, we must: | |
3704 | * - Update loads to have both entity and cfs_rq synced with now. | |
3705 | * - Add its load to cfs_rq->runnable_avg | |
3706 | * - For group_entity, update its weight to reflect the new share of | |
3707 | * its group cfs_rq | |
3708 | * - Add its new weight to cfs_rq->load.weight | |
3709 | */ | |
d31b1a66 | 3710 | update_load_avg(se, UPDATE_TG); |
9d89c257 | 3711 | enqueue_entity_load_avg(cfs_rq, se); |
89ee048f | 3712 | update_cfs_shares(se); |
17bc14b7 | 3713 | account_entity_enqueue(cfs_rq, se); |
bf0f6f24 | 3714 | |
1a3d027c | 3715 | if (flags & ENQUEUE_WAKEUP) |
aeb73b04 | 3716 | place_entity(cfs_rq, se, 0); |
bf0f6f24 | 3717 | |
cb251765 | 3718 | check_schedstat_required(); |
4fa8d299 JP |
3719 | update_stats_enqueue(cfs_rq, se, flags); |
3720 | check_spread(cfs_rq, se); | |
2f950354 | 3721 | if (!curr) |
83b699ed | 3722 | __enqueue_entity(cfs_rq, se); |
2069dd75 | 3723 | se->on_rq = 1; |
3d4b47b4 | 3724 | |
d3d9dc33 | 3725 | if (cfs_rq->nr_running == 1) { |
3d4b47b4 | 3726 | list_add_leaf_cfs_rq(cfs_rq); |
d3d9dc33 PT |
3727 | check_enqueue_throttle(cfs_rq); |
3728 | } | |
bf0f6f24 IM |
3729 | } |
3730 | ||
2c13c919 | 3731 | static void __clear_buddies_last(struct sched_entity *se) |
2002c695 | 3732 | { |
2c13c919 RR |
3733 | for_each_sched_entity(se) { |
3734 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
f1044799 | 3735 | if (cfs_rq->last != se) |
2c13c919 | 3736 | break; |
f1044799 PZ |
3737 | |
3738 | cfs_rq->last = NULL; | |
2c13c919 RR |
3739 | } |
3740 | } | |
2002c695 | 3741 | |
2c13c919 RR |
3742 | static void __clear_buddies_next(struct sched_entity *se) |
3743 | { | |
3744 | for_each_sched_entity(se) { | |
3745 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
f1044799 | 3746 | if (cfs_rq->next != se) |
2c13c919 | 3747 | break; |
f1044799 PZ |
3748 | |
3749 | cfs_rq->next = NULL; | |
2c13c919 | 3750 | } |
2002c695 PZ |
3751 | } |
3752 | ||
ac53db59 RR |
3753 | static void __clear_buddies_skip(struct sched_entity *se) |
3754 | { | |
3755 | for_each_sched_entity(se) { | |
3756 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
f1044799 | 3757 | if (cfs_rq->skip != se) |
ac53db59 | 3758 | break; |
f1044799 PZ |
3759 | |
3760 | cfs_rq->skip = NULL; | |
ac53db59 RR |
3761 | } |
3762 | } | |
3763 | ||
a571bbea PZ |
3764 | static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se) |
3765 | { | |
2c13c919 RR |
3766 | if (cfs_rq->last == se) |
3767 | __clear_buddies_last(se); | |
3768 | ||
3769 | if (cfs_rq->next == se) | |
3770 | __clear_buddies_next(se); | |
ac53db59 RR |
3771 | |
3772 | if (cfs_rq->skip == se) | |
3773 | __clear_buddies_skip(se); | |
a571bbea PZ |
3774 | } |
3775 | ||
6c16a6dc | 3776 | static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq); |
d8b4986d | 3777 | |
bf0f6f24 | 3778 | static void |
371fd7e7 | 3779 | dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
bf0f6f24 | 3780 | { |
a2a2d680 DA |
3781 | /* |
3782 | * Update run-time statistics of the 'current'. | |
3783 | */ | |
3784 | update_curr(cfs_rq); | |
89ee048f VG |
3785 | |
3786 | /* | |
3787 | * When dequeuing a sched_entity, we must: | |
3788 | * - Update loads to have both entity and cfs_rq synced with now. | |
3789 | * - Substract its load from the cfs_rq->runnable_avg. | |
3790 | * - Substract its previous weight from cfs_rq->load.weight. | |
3791 | * - For group entity, update its weight to reflect the new share | |
3792 | * of its group cfs_rq. | |
3793 | */ | |
d31b1a66 | 3794 | update_load_avg(se, UPDATE_TG); |
13962234 | 3795 | dequeue_entity_load_avg(cfs_rq, se); |
a2a2d680 | 3796 | |
4fa8d299 | 3797 | update_stats_dequeue(cfs_rq, se, flags); |
67e9fb2a | 3798 | |
2002c695 | 3799 | clear_buddies(cfs_rq, se); |
4793241b | 3800 | |
83b699ed | 3801 | if (se != cfs_rq->curr) |
30cfdcfc | 3802 | __dequeue_entity(cfs_rq, se); |
17bc14b7 | 3803 | se->on_rq = 0; |
30cfdcfc | 3804 | account_entity_dequeue(cfs_rq, se); |
88ec22d3 PZ |
3805 | |
3806 | /* | |
b60205c7 PZ |
3807 | * Normalize after update_curr(); which will also have moved |
3808 | * min_vruntime if @se is the one holding it back. But before doing | |
3809 | * update_min_vruntime() again, which will discount @se's position and | |
3810 | * can move min_vruntime forward still more. | |
88ec22d3 | 3811 | */ |
371fd7e7 | 3812 | if (!(flags & DEQUEUE_SLEEP)) |
88ec22d3 | 3813 | se->vruntime -= cfs_rq->min_vruntime; |
1e876231 | 3814 | |
d8b4986d PT |
3815 | /* return excess runtime on last dequeue */ |
3816 | return_cfs_rq_runtime(cfs_rq); | |
3817 | ||
89ee048f | 3818 | update_cfs_shares(se); |
b60205c7 PZ |
3819 | |
3820 | /* | |
3821 | * Now advance min_vruntime if @se was the entity holding it back, | |
3822 | * except when: DEQUEUE_SAVE && !DEQUEUE_MOVE, in this case we'll be | |
3823 | * put back on, and if we advance min_vruntime, we'll be placed back | |
3824 | * further than we started -- ie. we'll be penalized. | |
3825 | */ | |
3826 | if ((flags & (DEQUEUE_SAVE | DEQUEUE_MOVE)) == DEQUEUE_SAVE) | |
3827 | update_min_vruntime(cfs_rq); | |
bf0f6f24 IM |
3828 | } |
3829 | ||
3830 | /* | |
3831 | * Preempt the current task with a newly woken task if needed: | |
3832 | */ | |
7c92e54f | 3833 | static void |
2e09bf55 | 3834 | check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr) |
bf0f6f24 | 3835 | { |
11697830 | 3836 | unsigned long ideal_runtime, delta_exec; |
f4cfb33e WX |
3837 | struct sched_entity *se; |
3838 | s64 delta; | |
11697830 | 3839 | |
6d0f0ebd | 3840 | ideal_runtime = sched_slice(cfs_rq, curr); |
11697830 | 3841 | delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime; |
a9f3e2b5 | 3842 | if (delta_exec > ideal_runtime) { |
8875125e | 3843 | resched_curr(rq_of(cfs_rq)); |
a9f3e2b5 MG |
3844 | /* |
3845 | * The current task ran long enough, ensure it doesn't get | |
3846 | * re-elected due to buddy favours. | |
3847 | */ | |
3848 | clear_buddies(cfs_rq, curr); | |
f685ceac MG |
3849 | return; |
3850 | } | |
3851 | ||
3852 | /* | |
3853 | * Ensure that a task that missed wakeup preemption by a | |
3854 | * narrow margin doesn't have to wait for a full slice. | |
3855 | * This also mitigates buddy induced latencies under load. | |
3856 | */ | |
f685ceac MG |
3857 | if (delta_exec < sysctl_sched_min_granularity) |
3858 | return; | |
3859 | ||
f4cfb33e WX |
3860 | se = __pick_first_entity(cfs_rq); |
3861 | delta = curr->vruntime - se->vruntime; | |
f685ceac | 3862 | |
f4cfb33e WX |
3863 | if (delta < 0) |
3864 | return; | |
d7d82944 | 3865 | |
f4cfb33e | 3866 | if (delta > ideal_runtime) |
8875125e | 3867 | resched_curr(rq_of(cfs_rq)); |
bf0f6f24 IM |
3868 | } |
3869 | ||
83b699ed | 3870 | static void |
8494f412 | 3871 | set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
bf0f6f24 | 3872 | { |
83b699ed SV |
3873 | /* 'current' is not kept within the tree. */ |
3874 | if (se->on_rq) { | |
3875 | /* | |
3876 | * Any task has to be enqueued before it get to execute on | |
3877 | * a CPU. So account for the time it spent waiting on the | |
3878 | * runqueue. | |
3879 | */ | |
4fa8d299 | 3880 | update_stats_wait_end(cfs_rq, se); |
83b699ed | 3881 | __dequeue_entity(cfs_rq, se); |
d31b1a66 | 3882 | update_load_avg(se, UPDATE_TG); |
83b699ed SV |
3883 | } |
3884 | ||
79303e9e | 3885 | update_stats_curr_start(cfs_rq, se); |
429d43bc | 3886 | cfs_rq->curr = se; |
4fa8d299 | 3887 | |
eba1ed4b IM |
3888 | /* |
3889 | * Track our maximum slice length, if the CPU's load is at | |
3890 | * least twice that of our own weight (i.e. dont track it | |
3891 | * when there are only lesser-weight tasks around): | |
3892 | */ | |
cb251765 | 3893 | if (schedstat_enabled() && rq_of(cfs_rq)->load.weight >= 2*se->load.weight) { |
4fa8d299 JP |
3894 | schedstat_set(se->statistics.slice_max, |
3895 | max((u64)schedstat_val(se->statistics.slice_max), | |
3896 | se->sum_exec_runtime - se->prev_sum_exec_runtime)); | |
eba1ed4b | 3897 | } |
4fa8d299 | 3898 | |
4a55b450 | 3899 | se->prev_sum_exec_runtime = se->sum_exec_runtime; |
bf0f6f24 IM |
3900 | } |
3901 | ||
3f3a4904 PZ |
3902 | static int |
3903 | wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se); | |
3904 | ||
ac53db59 RR |
3905 | /* |
3906 | * Pick the next process, keeping these things in mind, in this order: | |
3907 | * 1) keep things fair between processes/task groups | |
3908 | * 2) pick the "next" process, since someone really wants that to run | |
3909 | * 3) pick the "last" process, for cache locality | |
3910 | * 4) do not run the "skip" process, if something else is available | |
3911 | */ | |
678d5718 PZ |
3912 | static struct sched_entity * |
3913 | pick_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *curr) | |
aa2ac252 | 3914 | { |
678d5718 PZ |
3915 | struct sched_entity *left = __pick_first_entity(cfs_rq); |
3916 | struct sched_entity *se; | |
3917 | ||
3918 | /* | |
3919 | * If curr is set we have to see if its left of the leftmost entity | |
3920 | * still in the tree, provided there was anything in the tree at all. | |
3921 | */ | |
3922 | if (!left || (curr && entity_before(curr, left))) | |
3923 | left = curr; | |
3924 | ||
3925 | se = left; /* ideally we run the leftmost entity */ | |
f4b6755f | 3926 | |
ac53db59 RR |
3927 | /* |
3928 | * Avoid running the skip buddy, if running something else can | |
3929 | * be done without getting too unfair. | |
3930 | */ | |
3931 | if (cfs_rq->skip == se) { | |
678d5718 PZ |
3932 | struct sched_entity *second; |
3933 | ||
3934 | if (se == curr) { | |
3935 | second = __pick_first_entity(cfs_rq); | |
3936 | } else { | |
3937 | second = __pick_next_entity(se); | |
3938 | if (!second || (curr && entity_before(curr, second))) | |
3939 | second = curr; | |
3940 | } | |
3941 | ||
ac53db59 RR |
3942 | if (second && wakeup_preempt_entity(second, left) < 1) |
3943 | se = second; | |
3944 | } | |
aa2ac252 | 3945 | |
f685ceac MG |
3946 | /* |
3947 | * Prefer last buddy, try to return the CPU to a preempted task. | |
3948 | */ | |
3949 | if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1) | |
3950 | se = cfs_rq->last; | |
3951 | ||
ac53db59 RR |
3952 | /* |
3953 | * Someone really wants this to run. If it's not unfair, run it. | |
3954 | */ | |
3955 | if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1) | |
3956 | se = cfs_rq->next; | |
3957 | ||
f685ceac | 3958 | clear_buddies(cfs_rq, se); |
4793241b PZ |
3959 | |
3960 | return se; | |
aa2ac252 PZ |
3961 | } |
3962 | ||
678d5718 | 3963 | static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq); |
d3d9dc33 | 3964 | |
ab6cde26 | 3965 | static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev) |
bf0f6f24 IM |
3966 | { |
3967 | /* | |
3968 | * If still on the runqueue then deactivate_task() | |
3969 | * was not called and update_curr() has to be done: | |
3970 | */ | |
3971 | if (prev->on_rq) | |
b7cc0896 | 3972 | update_curr(cfs_rq); |
bf0f6f24 | 3973 | |
d3d9dc33 PT |
3974 | /* throttle cfs_rqs exceeding runtime */ |
3975 | check_cfs_rq_runtime(cfs_rq); | |
3976 | ||
4fa8d299 | 3977 | check_spread(cfs_rq, prev); |
cb251765 | 3978 | |
30cfdcfc | 3979 | if (prev->on_rq) { |
4fa8d299 | 3980 | update_stats_wait_start(cfs_rq, prev); |
30cfdcfc DA |
3981 | /* Put 'current' back into the tree. */ |
3982 | __enqueue_entity(cfs_rq, prev); | |
9d85f21c | 3983 | /* in !on_rq case, update occurred at dequeue */ |
9d89c257 | 3984 | update_load_avg(prev, 0); |
30cfdcfc | 3985 | } |
429d43bc | 3986 | cfs_rq->curr = NULL; |
bf0f6f24 IM |
3987 | } |
3988 | ||
8f4d37ec PZ |
3989 | static void |
3990 | entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued) | |
bf0f6f24 | 3991 | { |
bf0f6f24 | 3992 | /* |
30cfdcfc | 3993 | * Update run-time statistics of the 'current'. |
bf0f6f24 | 3994 | */ |
30cfdcfc | 3995 | update_curr(cfs_rq); |
bf0f6f24 | 3996 | |
9d85f21c PT |
3997 | /* |
3998 | * Ensure that runnable average is periodically updated. | |
3999 | */ | |
d31b1a66 | 4000 | update_load_avg(curr, UPDATE_TG); |
89ee048f | 4001 | update_cfs_shares(curr); |
9d85f21c | 4002 | |
8f4d37ec PZ |
4003 | #ifdef CONFIG_SCHED_HRTICK |
4004 | /* | |
4005 | * queued ticks are scheduled to match the slice, so don't bother | |
4006 | * validating it and just reschedule. | |
4007 | */ | |
983ed7a6 | 4008 | if (queued) { |
8875125e | 4009 | resched_curr(rq_of(cfs_rq)); |
983ed7a6 HH |
4010 | return; |
4011 | } | |
8f4d37ec PZ |
4012 | /* |
4013 | * don't let the period tick interfere with the hrtick preemption | |
4014 | */ | |
4015 | if (!sched_feat(DOUBLE_TICK) && | |
4016 | hrtimer_active(&rq_of(cfs_rq)->hrtick_timer)) | |
4017 | return; | |
4018 | #endif | |
4019 | ||
2c2efaed | 4020 | if (cfs_rq->nr_running > 1) |
2e09bf55 | 4021 | check_preempt_tick(cfs_rq, curr); |
bf0f6f24 IM |
4022 | } |
4023 | ||
ab84d31e PT |
4024 | |
4025 | /************************************************** | |
4026 | * CFS bandwidth control machinery | |
4027 | */ | |
4028 | ||
4029 | #ifdef CONFIG_CFS_BANDWIDTH | |
029632fb PZ |
4030 | |
4031 | #ifdef HAVE_JUMP_LABEL | |
c5905afb | 4032 | static struct static_key __cfs_bandwidth_used; |
029632fb PZ |
4033 | |
4034 | static inline bool cfs_bandwidth_used(void) | |
4035 | { | |
c5905afb | 4036 | return static_key_false(&__cfs_bandwidth_used); |
029632fb PZ |
4037 | } |
4038 | ||
1ee14e6c | 4039 | void cfs_bandwidth_usage_inc(void) |
029632fb | 4040 | { |
1ee14e6c BS |
4041 | static_key_slow_inc(&__cfs_bandwidth_used); |
4042 | } | |
4043 | ||
4044 | void cfs_bandwidth_usage_dec(void) | |
4045 | { | |
4046 | static_key_slow_dec(&__cfs_bandwidth_used); | |
029632fb PZ |
4047 | } |
4048 | #else /* HAVE_JUMP_LABEL */ | |
4049 | static bool cfs_bandwidth_used(void) | |
4050 | { | |
4051 | return true; | |
4052 | } | |
4053 | ||
1ee14e6c BS |
4054 | void cfs_bandwidth_usage_inc(void) {} |
4055 | void cfs_bandwidth_usage_dec(void) {} | |
029632fb PZ |
4056 | #endif /* HAVE_JUMP_LABEL */ |
4057 | ||
ab84d31e PT |
4058 | /* |
4059 | * default period for cfs group bandwidth. | |
4060 | * default: 0.1s, units: nanoseconds | |
4061 | */ | |
4062 | static inline u64 default_cfs_period(void) | |
4063 | { | |
4064 | return 100000000ULL; | |
4065 | } | |
ec12cb7f PT |
4066 | |
4067 | static inline u64 sched_cfs_bandwidth_slice(void) | |
4068 | { | |
4069 | return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC; | |
4070 | } | |
4071 | ||
a9cf55b2 PT |
4072 | /* |
4073 | * Replenish runtime according to assigned quota and update expiration time. | |
4074 | * We use sched_clock_cpu directly instead of rq->clock to avoid adding | |
4075 | * additional synchronization around rq->lock. | |
4076 | * | |
4077 | * requires cfs_b->lock | |
4078 | */ | |
029632fb | 4079 | void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b) |
a9cf55b2 PT |
4080 | { |
4081 | u64 now; | |
4082 | ||
4083 | if (cfs_b->quota == RUNTIME_INF) | |
4084 | return; | |
4085 | ||
4086 | now = sched_clock_cpu(smp_processor_id()); | |
4087 | cfs_b->runtime = cfs_b->quota; | |
4088 | cfs_b->runtime_expires = now + ktime_to_ns(cfs_b->period); | |
4089 | } | |
4090 | ||
029632fb PZ |
4091 | static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg) |
4092 | { | |
4093 | return &tg->cfs_bandwidth; | |
4094 | } | |
4095 | ||
f1b17280 PT |
4096 | /* rq->task_clock normalized against any time this cfs_rq has spent throttled */ |
4097 | static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq) | |
4098 | { | |
4099 | if (unlikely(cfs_rq->throttle_count)) | |
1a99ae3f | 4100 | return cfs_rq->throttled_clock_task - cfs_rq->throttled_clock_task_time; |
f1b17280 | 4101 | |
78becc27 | 4102 | return rq_clock_task(rq_of(cfs_rq)) - cfs_rq->throttled_clock_task_time; |
f1b17280 PT |
4103 | } |
4104 | ||
85dac906 PT |
4105 | /* returns 0 on failure to allocate runtime */ |
4106 | static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
ec12cb7f PT |
4107 | { |
4108 | struct task_group *tg = cfs_rq->tg; | |
4109 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg); | |
a9cf55b2 | 4110 | u64 amount = 0, min_amount, expires; |
ec12cb7f PT |
4111 | |
4112 | /* note: this is a positive sum as runtime_remaining <= 0 */ | |
4113 | min_amount = sched_cfs_bandwidth_slice() - cfs_rq->runtime_remaining; | |
4114 | ||
4115 | raw_spin_lock(&cfs_b->lock); | |
4116 | if (cfs_b->quota == RUNTIME_INF) | |
4117 | amount = min_amount; | |
58088ad0 | 4118 | else { |
77a4d1a1 | 4119 | start_cfs_bandwidth(cfs_b); |
58088ad0 PT |
4120 | |
4121 | if (cfs_b->runtime > 0) { | |
4122 | amount = min(cfs_b->runtime, min_amount); | |
4123 | cfs_b->runtime -= amount; | |
4124 | cfs_b->idle = 0; | |
4125 | } | |
ec12cb7f | 4126 | } |
a9cf55b2 | 4127 | expires = cfs_b->runtime_expires; |
ec12cb7f PT |
4128 | raw_spin_unlock(&cfs_b->lock); |
4129 | ||
4130 | cfs_rq->runtime_remaining += amount; | |
a9cf55b2 PT |
4131 | /* |
4132 | * we may have advanced our local expiration to account for allowed | |
4133 | * spread between our sched_clock and the one on which runtime was | |
4134 | * issued. | |
4135 | */ | |
4136 | if ((s64)(expires - cfs_rq->runtime_expires) > 0) | |
4137 | cfs_rq->runtime_expires = expires; | |
85dac906 PT |
4138 | |
4139 | return cfs_rq->runtime_remaining > 0; | |
ec12cb7f PT |
4140 | } |
4141 | ||
a9cf55b2 PT |
4142 | /* |
4143 | * Note: This depends on the synchronization provided by sched_clock and the | |
4144 | * fact that rq->clock snapshots this value. | |
4145 | */ | |
4146 | static void expire_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
ec12cb7f | 4147 | { |
a9cf55b2 | 4148 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); |
a9cf55b2 PT |
4149 | |
4150 | /* if the deadline is ahead of our clock, nothing to do */ | |
78becc27 | 4151 | if (likely((s64)(rq_clock(rq_of(cfs_rq)) - cfs_rq->runtime_expires) < 0)) |
ec12cb7f PT |
4152 | return; |
4153 | ||
a9cf55b2 PT |
4154 | if (cfs_rq->runtime_remaining < 0) |
4155 | return; | |
4156 | ||
4157 | /* | |
4158 | * If the local deadline has passed we have to consider the | |
4159 | * possibility that our sched_clock is 'fast' and the global deadline | |
4160 | * has not truly expired. | |
4161 | * | |
4162 | * Fortunately we can check determine whether this the case by checking | |
51f2176d BS |
4163 | * whether the global deadline has advanced. It is valid to compare |
4164 | * cfs_b->runtime_expires without any locks since we only care about | |
4165 | * exact equality, so a partial write will still work. | |
a9cf55b2 PT |
4166 | */ |
4167 | ||
51f2176d | 4168 | if (cfs_rq->runtime_expires != cfs_b->runtime_expires) { |
a9cf55b2 PT |
4169 | /* extend local deadline, drift is bounded above by 2 ticks */ |
4170 | cfs_rq->runtime_expires += TICK_NSEC; | |
4171 | } else { | |
4172 | /* global deadline is ahead, expiration has passed */ | |
4173 | cfs_rq->runtime_remaining = 0; | |
4174 | } | |
4175 | } | |
4176 | ||
9dbdb155 | 4177 | static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec) |
a9cf55b2 PT |
4178 | { |
4179 | /* dock delta_exec before expiring quota (as it could span periods) */ | |
ec12cb7f | 4180 | cfs_rq->runtime_remaining -= delta_exec; |
a9cf55b2 PT |
4181 | expire_cfs_rq_runtime(cfs_rq); |
4182 | ||
4183 | if (likely(cfs_rq->runtime_remaining > 0)) | |
ec12cb7f PT |
4184 | return; |
4185 | ||
85dac906 PT |
4186 | /* |
4187 | * if we're unable to extend our runtime we resched so that the active | |
4188 | * hierarchy can be throttled | |
4189 | */ | |
4190 | if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr)) | |
8875125e | 4191 | resched_curr(rq_of(cfs_rq)); |
ec12cb7f PT |
4192 | } |
4193 | ||
6c16a6dc | 4194 | static __always_inline |
9dbdb155 | 4195 | void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec) |
ec12cb7f | 4196 | { |
56f570e5 | 4197 | if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled) |
ec12cb7f PT |
4198 | return; |
4199 | ||
4200 | __account_cfs_rq_runtime(cfs_rq, delta_exec); | |
4201 | } | |
4202 | ||
85dac906 PT |
4203 | static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq) |
4204 | { | |
56f570e5 | 4205 | return cfs_bandwidth_used() && cfs_rq->throttled; |
85dac906 PT |
4206 | } |
4207 | ||
64660c86 PT |
4208 | /* check whether cfs_rq, or any parent, is throttled */ |
4209 | static inline int throttled_hierarchy(struct cfs_rq *cfs_rq) | |
4210 | { | |
56f570e5 | 4211 | return cfs_bandwidth_used() && cfs_rq->throttle_count; |
64660c86 PT |
4212 | } |
4213 | ||
4214 | /* | |
4215 | * Ensure that neither of the group entities corresponding to src_cpu or | |
4216 | * dest_cpu are members of a throttled hierarchy when performing group | |
4217 | * load-balance operations. | |
4218 | */ | |
4219 | static inline int throttled_lb_pair(struct task_group *tg, | |
4220 | int src_cpu, int dest_cpu) | |
4221 | { | |
4222 | struct cfs_rq *src_cfs_rq, *dest_cfs_rq; | |
4223 | ||
4224 | src_cfs_rq = tg->cfs_rq[src_cpu]; | |
4225 | dest_cfs_rq = tg->cfs_rq[dest_cpu]; | |
4226 | ||
4227 | return throttled_hierarchy(src_cfs_rq) || | |
4228 | throttled_hierarchy(dest_cfs_rq); | |
4229 | } | |
4230 | ||
4231 | /* updated child weight may affect parent so we have to do this bottom up */ | |
4232 | static int tg_unthrottle_up(struct task_group *tg, void *data) | |
4233 | { | |
4234 | struct rq *rq = data; | |
4235 | struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; | |
4236 | ||
4237 | cfs_rq->throttle_count--; | |
64660c86 | 4238 | if (!cfs_rq->throttle_count) { |
f1b17280 | 4239 | /* adjust cfs_rq_clock_task() */ |
78becc27 | 4240 | cfs_rq->throttled_clock_task_time += rq_clock_task(rq) - |
f1b17280 | 4241 | cfs_rq->throttled_clock_task; |
64660c86 | 4242 | } |
64660c86 PT |
4243 | |
4244 | return 0; | |
4245 | } | |
4246 | ||
4247 | static int tg_throttle_down(struct task_group *tg, void *data) | |
4248 | { | |
4249 | struct rq *rq = data; | |
4250 | struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; | |
4251 | ||
82958366 PT |
4252 | /* group is entering throttled state, stop time */ |
4253 | if (!cfs_rq->throttle_count) | |
78becc27 | 4254 | cfs_rq->throttled_clock_task = rq_clock_task(rq); |
64660c86 PT |
4255 | cfs_rq->throttle_count++; |
4256 | ||
4257 | return 0; | |
4258 | } | |
4259 | ||
d3d9dc33 | 4260 | static void throttle_cfs_rq(struct cfs_rq *cfs_rq) |
85dac906 PT |
4261 | { |
4262 | struct rq *rq = rq_of(cfs_rq); | |
4263 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); | |
4264 | struct sched_entity *se; | |
4265 | long task_delta, dequeue = 1; | |
77a4d1a1 | 4266 | bool empty; |
85dac906 PT |
4267 | |
4268 | se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))]; | |
4269 | ||
f1b17280 | 4270 | /* freeze hierarchy runnable averages while throttled */ |
64660c86 PT |
4271 | rcu_read_lock(); |
4272 | walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq); | |
4273 | rcu_read_unlock(); | |
85dac906 PT |
4274 | |
4275 | task_delta = cfs_rq->h_nr_running; | |
4276 | for_each_sched_entity(se) { | |
4277 | struct cfs_rq *qcfs_rq = cfs_rq_of(se); | |
4278 | /* throttled entity or throttle-on-deactivate */ | |
4279 | if (!se->on_rq) | |
4280 | break; | |
4281 | ||
4282 | if (dequeue) | |
4283 | dequeue_entity(qcfs_rq, se, DEQUEUE_SLEEP); | |
4284 | qcfs_rq->h_nr_running -= task_delta; | |
4285 | ||
4286 | if (qcfs_rq->load.weight) | |
4287 | dequeue = 0; | |
4288 | } | |
4289 | ||
4290 | if (!se) | |
72465447 | 4291 | sub_nr_running(rq, task_delta); |
85dac906 PT |
4292 | |
4293 | cfs_rq->throttled = 1; | |
78becc27 | 4294 | cfs_rq->throttled_clock = rq_clock(rq); |
85dac906 | 4295 | raw_spin_lock(&cfs_b->lock); |
d49db342 | 4296 | empty = list_empty(&cfs_b->throttled_cfs_rq); |
77a4d1a1 | 4297 | |
c06f04c7 BS |
4298 | /* |
4299 | * Add to the _head_ of the list, so that an already-started | |
4300 | * distribute_cfs_runtime will not see us | |
4301 | */ | |
4302 | list_add_rcu(&cfs_rq->throttled_list, &cfs_b->throttled_cfs_rq); | |
77a4d1a1 PZ |
4303 | |
4304 | /* | |
4305 | * If we're the first throttled task, make sure the bandwidth | |
4306 | * timer is running. | |
4307 | */ | |
4308 | if (empty) | |
4309 | start_cfs_bandwidth(cfs_b); | |
4310 | ||
85dac906 PT |
4311 | raw_spin_unlock(&cfs_b->lock); |
4312 | } | |
4313 | ||
029632fb | 4314 | void unthrottle_cfs_rq(struct cfs_rq *cfs_rq) |
671fd9da PT |
4315 | { |
4316 | struct rq *rq = rq_of(cfs_rq); | |
4317 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); | |
4318 | struct sched_entity *se; | |
4319 | int enqueue = 1; | |
4320 | long task_delta; | |
4321 | ||
22b958d8 | 4322 | se = cfs_rq->tg->se[cpu_of(rq)]; |
671fd9da PT |
4323 | |
4324 | cfs_rq->throttled = 0; | |
1a55af2e FW |
4325 | |
4326 | update_rq_clock(rq); | |
4327 | ||
671fd9da | 4328 | raw_spin_lock(&cfs_b->lock); |
78becc27 | 4329 | cfs_b->throttled_time += rq_clock(rq) - cfs_rq->throttled_clock; |
671fd9da PT |
4330 | list_del_rcu(&cfs_rq->throttled_list); |
4331 | raw_spin_unlock(&cfs_b->lock); | |
4332 | ||
64660c86 PT |
4333 | /* update hierarchical throttle state */ |
4334 | walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq); | |
4335 | ||
671fd9da PT |
4336 | if (!cfs_rq->load.weight) |
4337 | return; | |
4338 | ||
4339 | task_delta = cfs_rq->h_nr_running; | |
4340 | for_each_sched_entity(se) { | |
4341 | if (se->on_rq) | |
4342 | enqueue = 0; | |
4343 | ||
4344 | cfs_rq = cfs_rq_of(se); | |
4345 | if (enqueue) | |
4346 | enqueue_entity(cfs_rq, se, ENQUEUE_WAKEUP); | |
4347 | cfs_rq->h_nr_running += task_delta; | |
4348 | ||
4349 | if (cfs_rq_throttled(cfs_rq)) | |
4350 | break; | |
4351 | } | |
4352 | ||
4353 | if (!se) | |
72465447 | 4354 | add_nr_running(rq, task_delta); |
671fd9da PT |
4355 | |
4356 | /* determine whether we need to wake up potentially idle cpu */ | |
4357 | if (rq->curr == rq->idle && rq->cfs.nr_running) | |
8875125e | 4358 | resched_curr(rq); |
671fd9da PT |
4359 | } |
4360 | ||
4361 | static u64 distribute_cfs_runtime(struct cfs_bandwidth *cfs_b, | |
4362 | u64 remaining, u64 expires) | |
4363 | { | |
4364 | struct cfs_rq *cfs_rq; | |
c06f04c7 BS |
4365 | u64 runtime; |
4366 | u64 starting_runtime = remaining; | |
671fd9da PT |
4367 | |
4368 | rcu_read_lock(); | |
4369 | list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq, | |
4370 | throttled_list) { | |
4371 | struct rq *rq = rq_of(cfs_rq); | |
8a8c69c3 | 4372 | struct rq_flags rf; |
671fd9da | 4373 | |
8a8c69c3 | 4374 | rq_lock(rq, &rf); |
671fd9da PT |
4375 | if (!cfs_rq_throttled(cfs_rq)) |
4376 | goto next; | |
4377 | ||
4378 | runtime = -cfs_rq->runtime_remaining + 1; | |
4379 | if (runtime > remaining) | |
4380 | runtime = remaining; | |
4381 | remaining -= runtime; | |
4382 | ||
4383 | cfs_rq->runtime_remaining += runtime; | |
4384 | cfs_rq->runtime_expires = expires; | |
4385 | ||
4386 | /* we check whether we're throttled above */ | |
4387 | if (cfs_rq->runtime_remaining > 0) | |
4388 | unthrottle_cfs_rq(cfs_rq); | |
4389 | ||
4390 | next: | |
8a8c69c3 | 4391 | rq_unlock(rq, &rf); |
671fd9da PT |
4392 | |
4393 | if (!remaining) | |
4394 | break; | |
4395 | } | |
4396 | rcu_read_unlock(); | |
4397 | ||
c06f04c7 | 4398 | return starting_runtime - remaining; |
671fd9da PT |
4399 | } |
4400 | ||
58088ad0 PT |
4401 | /* |
4402 | * Responsible for refilling a task_group's bandwidth and unthrottling its | |
4403 | * cfs_rqs as appropriate. If there has been no activity within the last | |
4404 | * period the timer is deactivated until scheduling resumes; cfs_b->idle is | |
4405 | * used to track this state. | |
4406 | */ | |
4407 | static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun) | |
4408 | { | |
671fd9da | 4409 | u64 runtime, runtime_expires; |
51f2176d | 4410 | int throttled; |
58088ad0 | 4411 | |
58088ad0 PT |
4412 | /* no need to continue the timer with no bandwidth constraint */ |
4413 | if (cfs_b->quota == RUNTIME_INF) | |
51f2176d | 4414 | goto out_deactivate; |
58088ad0 | 4415 | |
671fd9da | 4416 | throttled = !list_empty(&cfs_b->throttled_cfs_rq); |
e8da1b18 | 4417 | cfs_b->nr_periods += overrun; |
671fd9da | 4418 | |
51f2176d BS |
4419 | /* |
4420 | * idle depends on !throttled (for the case of a large deficit), and if | |
4421 | * we're going inactive then everything else can be deferred | |
4422 | */ | |
4423 | if (cfs_b->idle && !throttled) | |
4424 | goto out_deactivate; | |
a9cf55b2 PT |
4425 | |
4426 | __refill_cfs_bandwidth_runtime(cfs_b); | |
4427 | ||
671fd9da PT |
4428 | if (!throttled) { |
4429 | /* mark as potentially idle for the upcoming period */ | |
4430 | cfs_b->idle = 1; | |
51f2176d | 4431 | return 0; |
671fd9da PT |
4432 | } |
4433 | ||
e8da1b18 NR |
4434 | /* account preceding periods in which throttling occurred */ |
4435 | cfs_b->nr_throttled += overrun; | |
4436 | ||
671fd9da | 4437 | runtime_expires = cfs_b->runtime_expires; |
671fd9da PT |
4438 | |
4439 | /* | |
c06f04c7 BS |
4440 | * This check is repeated as we are holding onto the new bandwidth while |
4441 | * we unthrottle. This can potentially race with an unthrottled group | |
4442 | * trying to acquire new bandwidth from the global pool. This can result | |
4443 | * in us over-using our runtime if it is all used during this loop, but | |
4444 | * only by limited amounts in that extreme case. | |
671fd9da | 4445 | */ |
c06f04c7 BS |
4446 | while (throttled && cfs_b->runtime > 0) { |
4447 | runtime = cfs_b->runtime; | |
671fd9da PT |
4448 | raw_spin_unlock(&cfs_b->lock); |
4449 | /* we can't nest cfs_b->lock while distributing bandwidth */ | |
4450 | runtime = distribute_cfs_runtime(cfs_b, runtime, | |
4451 | runtime_expires); | |
4452 | raw_spin_lock(&cfs_b->lock); | |
4453 | ||
4454 | throttled = !list_empty(&cfs_b->throttled_cfs_rq); | |
c06f04c7 BS |
4455 | |
4456 | cfs_b->runtime -= min(runtime, cfs_b->runtime); | |
671fd9da | 4457 | } |
58088ad0 | 4458 | |
671fd9da PT |
4459 | /* |
4460 | * While we are ensured activity in the period following an | |
4461 | * unthrottle, this also covers the case in which the new bandwidth is | |
4462 | * insufficient to cover the existing bandwidth deficit. (Forcing the | |
4463 | * timer to remain active while there are any throttled entities.) | |
4464 | */ | |
4465 | cfs_b->idle = 0; | |
58088ad0 | 4466 | |
51f2176d BS |
4467 | return 0; |
4468 | ||
4469 | out_deactivate: | |
51f2176d | 4470 | return 1; |
58088ad0 | 4471 | } |
d3d9dc33 | 4472 | |
d8b4986d PT |
4473 | /* a cfs_rq won't donate quota below this amount */ |
4474 | static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC; | |
4475 | /* minimum remaining period time to redistribute slack quota */ | |
4476 | static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC; | |
4477 | /* how long we wait to gather additional slack before distributing */ | |
4478 | static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC; | |
4479 | ||
db06e78c BS |
4480 | /* |
4481 | * Are we near the end of the current quota period? | |
4482 | * | |
4483 | * Requires cfs_b->lock for hrtimer_expires_remaining to be safe against the | |
4961b6e1 | 4484 | * hrtimer base being cleared by hrtimer_start. In the case of |
db06e78c BS |
4485 | * migrate_hrtimers, base is never cleared, so we are fine. |
4486 | */ | |
d8b4986d PT |
4487 | static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire) |
4488 | { | |
4489 | struct hrtimer *refresh_timer = &cfs_b->period_timer; | |
4490 | u64 remaining; | |
4491 | ||
4492 | /* if the call-back is running a quota refresh is already occurring */ | |
4493 | if (hrtimer_callback_running(refresh_timer)) | |
4494 | return 1; | |
4495 | ||
4496 | /* is a quota refresh about to occur? */ | |
4497 | remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer)); | |
4498 | if (remaining < min_expire) | |
4499 | return 1; | |
4500 | ||
4501 | return 0; | |
4502 | } | |
4503 | ||
4504 | static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b) | |
4505 | { | |
4506 | u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration; | |
4507 | ||
4508 | /* if there's a quota refresh soon don't bother with slack */ | |
4509 | if (runtime_refresh_within(cfs_b, min_left)) | |
4510 | return; | |
4511 | ||
4cfafd30 PZ |
4512 | hrtimer_start(&cfs_b->slack_timer, |
4513 | ns_to_ktime(cfs_bandwidth_slack_period), | |
4514 | HRTIMER_MODE_REL); | |
d8b4986d PT |
4515 | } |
4516 | ||
4517 | /* we know any runtime found here is valid as update_curr() precedes return */ | |
4518 | static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
4519 | { | |
4520 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); | |
4521 | s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime; | |
4522 | ||
4523 | if (slack_runtime <= 0) | |
4524 | return; | |
4525 | ||
4526 | raw_spin_lock(&cfs_b->lock); | |
4527 | if (cfs_b->quota != RUNTIME_INF && | |
4528 | cfs_rq->runtime_expires == cfs_b->runtime_expires) { | |
4529 | cfs_b->runtime += slack_runtime; | |
4530 | ||
4531 | /* we are under rq->lock, defer unthrottling using a timer */ | |
4532 | if (cfs_b->runtime > sched_cfs_bandwidth_slice() && | |
4533 | !list_empty(&cfs_b->throttled_cfs_rq)) | |
4534 | start_cfs_slack_bandwidth(cfs_b); | |
4535 | } | |
4536 | raw_spin_unlock(&cfs_b->lock); | |
4537 | ||
4538 | /* even if it's not valid for return we don't want to try again */ | |
4539 | cfs_rq->runtime_remaining -= slack_runtime; | |
4540 | } | |
4541 | ||
4542 | static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
4543 | { | |
56f570e5 PT |
4544 | if (!cfs_bandwidth_used()) |
4545 | return; | |
4546 | ||
fccfdc6f | 4547 | if (!cfs_rq->runtime_enabled || cfs_rq->nr_running) |
d8b4986d PT |
4548 | return; |
4549 | ||
4550 | __return_cfs_rq_runtime(cfs_rq); | |
4551 | } | |
4552 | ||
4553 | /* | |
4554 | * This is done with a timer (instead of inline with bandwidth return) since | |
4555 | * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs. | |
4556 | */ | |
4557 | static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b) | |
4558 | { | |
4559 | u64 runtime = 0, slice = sched_cfs_bandwidth_slice(); | |
4560 | u64 expires; | |
4561 | ||
4562 | /* confirm we're still not at a refresh boundary */ | |
db06e78c BS |
4563 | raw_spin_lock(&cfs_b->lock); |
4564 | if (runtime_refresh_within(cfs_b, min_bandwidth_expiration)) { | |
4565 | raw_spin_unlock(&cfs_b->lock); | |
d8b4986d | 4566 | return; |
db06e78c | 4567 | } |
d8b4986d | 4568 | |
c06f04c7 | 4569 | if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) |
d8b4986d | 4570 | runtime = cfs_b->runtime; |
c06f04c7 | 4571 | |
d8b4986d PT |
4572 | expires = cfs_b->runtime_expires; |
4573 | raw_spin_unlock(&cfs_b->lock); | |
4574 | ||
4575 | if (!runtime) | |
4576 | return; | |
4577 | ||
4578 | runtime = distribute_cfs_runtime(cfs_b, runtime, expires); | |
4579 | ||
4580 | raw_spin_lock(&cfs_b->lock); | |
4581 | if (expires == cfs_b->runtime_expires) | |
c06f04c7 | 4582 | cfs_b->runtime -= min(runtime, cfs_b->runtime); |
d8b4986d PT |
4583 | raw_spin_unlock(&cfs_b->lock); |
4584 | } | |
4585 | ||
d3d9dc33 PT |
4586 | /* |
4587 | * When a group wakes up we want to make sure that its quota is not already | |
4588 | * expired/exceeded, otherwise it may be allowed to steal additional ticks of | |
4589 | * runtime as update_curr() throttling can not not trigger until it's on-rq. | |
4590 | */ | |
4591 | static void check_enqueue_throttle(struct cfs_rq *cfs_rq) | |
4592 | { | |
56f570e5 PT |
4593 | if (!cfs_bandwidth_used()) |
4594 | return; | |
4595 | ||
d3d9dc33 PT |
4596 | /* an active group must be handled by the update_curr()->put() path */ |
4597 | if (!cfs_rq->runtime_enabled || cfs_rq->curr) | |
4598 | return; | |
4599 | ||
4600 | /* ensure the group is not already throttled */ | |
4601 | if (cfs_rq_throttled(cfs_rq)) | |
4602 | return; | |
4603 | ||
4604 | /* update runtime allocation */ | |
4605 | account_cfs_rq_runtime(cfs_rq, 0); | |
4606 | if (cfs_rq->runtime_remaining <= 0) | |
4607 | throttle_cfs_rq(cfs_rq); | |
4608 | } | |
4609 | ||
55e16d30 PZ |
4610 | static void sync_throttle(struct task_group *tg, int cpu) |
4611 | { | |
4612 | struct cfs_rq *pcfs_rq, *cfs_rq; | |
4613 | ||
4614 | if (!cfs_bandwidth_used()) | |
4615 | return; | |
4616 | ||
4617 | if (!tg->parent) | |
4618 | return; | |
4619 | ||
4620 | cfs_rq = tg->cfs_rq[cpu]; | |
4621 | pcfs_rq = tg->parent->cfs_rq[cpu]; | |
4622 | ||
4623 | cfs_rq->throttle_count = pcfs_rq->throttle_count; | |
b8922125 | 4624 | cfs_rq->throttled_clock_task = rq_clock_task(cpu_rq(cpu)); |
55e16d30 PZ |
4625 | } |
4626 | ||
d3d9dc33 | 4627 | /* conditionally throttle active cfs_rq's from put_prev_entity() */ |
678d5718 | 4628 | static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq) |
d3d9dc33 | 4629 | { |
56f570e5 | 4630 | if (!cfs_bandwidth_used()) |
678d5718 | 4631 | return false; |
56f570e5 | 4632 | |
d3d9dc33 | 4633 | if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0)) |
678d5718 | 4634 | return false; |
d3d9dc33 PT |
4635 | |
4636 | /* | |
4637 | * it's possible for a throttled entity to be forced into a running | |
4638 | * state (e.g. set_curr_task), in this case we're finished. | |
4639 | */ | |
4640 | if (cfs_rq_throttled(cfs_rq)) | |
678d5718 | 4641 | return true; |
d3d9dc33 PT |
4642 | |
4643 | throttle_cfs_rq(cfs_rq); | |
678d5718 | 4644 | return true; |
d3d9dc33 | 4645 | } |
029632fb | 4646 | |
029632fb PZ |
4647 | static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer) |
4648 | { | |
4649 | struct cfs_bandwidth *cfs_b = | |
4650 | container_of(timer, struct cfs_bandwidth, slack_timer); | |
77a4d1a1 | 4651 | |
029632fb PZ |
4652 | do_sched_cfs_slack_timer(cfs_b); |
4653 | ||
4654 | return HRTIMER_NORESTART; | |
4655 | } | |
4656 | ||
4657 | static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer) | |
4658 | { | |
4659 | struct cfs_bandwidth *cfs_b = | |
4660 | container_of(timer, struct cfs_bandwidth, period_timer); | |
029632fb PZ |
4661 | int overrun; |
4662 | int idle = 0; | |
4663 | ||
51f2176d | 4664 | raw_spin_lock(&cfs_b->lock); |
029632fb | 4665 | for (;;) { |
77a4d1a1 | 4666 | overrun = hrtimer_forward_now(timer, cfs_b->period); |
029632fb PZ |
4667 | if (!overrun) |
4668 | break; | |
4669 | ||
4670 | idle = do_sched_cfs_period_timer(cfs_b, overrun); | |
4671 | } | |
4cfafd30 PZ |
4672 | if (idle) |
4673 | cfs_b->period_active = 0; | |
51f2176d | 4674 | raw_spin_unlock(&cfs_b->lock); |
029632fb PZ |
4675 | |
4676 | return idle ? HRTIMER_NORESTART : HRTIMER_RESTART; | |
4677 | } | |
4678 | ||
4679 | void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) | |
4680 | { | |
4681 | raw_spin_lock_init(&cfs_b->lock); | |
4682 | cfs_b->runtime = 0; | |
4683 | cfs_b->quota = RUNTIME_INF; | |
4684 | cfs_b->period = ns_to_ktime(default_cfs_period()); | |
4685 | ||
4686 | INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq); | |
4cfafd30 | 4687 | hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED); |
029632fb PZ |
4688 | cfs_b->period_timer.function = sched_cfs_period_timer; |
4689 | hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); | |
4690 | cfs_b->slack_timer.function = sched_cfs_slack_timer; | |
4691 | } | |
4692 | ||
4693 | static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
4694 | { | |
4695 | cfs_rq->runtime_enabled = 0; | |
4696 | INIT_LIST_HEAD(&cfs_rq->throttled_list); | |
4697 | } | |
4698 | ||
77a4d1a1 | 4699 | void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b) |
029632fb | 4700 | { |
4cfafd30 | 4701 | lockdep_assert_held(&cfs_b->lock); |
029632fb | 4702 | |
4cfafd30 PZ |
4703 | if (!cfs_b->period_active) { |
4704 | cfs_b->period_active = 1; | |
4705 | hrtimer_forward_now(&cfs_b->period_timer, cfs_b->period); | |
4706 | hrtimer_start_expires(&cfs_b->period_timer, HRTIMER_MODE_ABS_PINNED); | |
4707 | } | |
029632fb PZ |
4708 | } |
4709 | ||
4710 | static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) | |
4711 | { | |
7f1a169b TH |
4712 | /* init_cfs_bandwidth() was not called */ |
4713 | if (!cfs_b->throttled_cfs_rq.next) | |
4714 | return; | |
4715 | ||
029632fb PZ |
4716 | hrtimer_cancel(&cfs_b->period_timer); |
4717 | hrtimer_cancel(&cfs_b->slack_timer); | |
4718 | } | |
4719 | ||
502ce005 PZ |
4720 | /* |
4721 | * Both these cpu hotplug callbacks race against unregister_fair_sched_group() | |
4722 | * | |
4723 | * The race is harmless, since modifying bandwidth settings of unhooked group | |
4724 | * bits doesn't do much. | |
4725 | */ | |
4726 | ||
4727 | /* cpu online calback */ | |
0e59bdae KT |
4728 | static void __maybe_unused update_runtime_enabled(struct rq *rq) |
4729 | { | |
502ce005 | 4730 | struct task_group *tg; |
0e59bdae | 4731 | |
502ce005 PZ |
4732 | lockdep_assert_held(&rq->lock); |
4733 | ||
4734 | rcu_read_lock(); | |
4735 | list_for_each_entry_rcu(tg, &task_groups, list) { | |
4736 | struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth; | |
4737 | struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; | |
0e59bdae KT |
4738 | |
4739 | raw_spin_lock(&cfs_b->lock); | |
4740 | cfs_rq->runtime_enabled = cfs_b->quota != RUNTIME_INF; | |
4741 | raw_spin_unlock(&cfs_b->lock); | |
4742 | } | |
502ce005 | 4743 | rcu_read_unlock(); |
0e59bdae KT |
4744 | } |
4745 | ||
502ce005 | 4746 | /* cpu offline callback */ |
38dc3348 | 4747 | static void __maybe_unused unthrottle_offline_cfs_rqs(struct rq *rq) |
029632fb | 4748 | { |
502ce005 PZ |
4749 | struct task_group *tg; |
4750 | ||
4751 | lockdep_assert_held(&rq->lock); | |
4752 | ||
4753 | rcu_read_lock(); | |
4754 | list_for_each_entry_rcu(tg, &task_groups, list) { | |
4755 | struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; | |
029632fb | 4756 | |
029632fb PZ |
4757 | if (!cfs_rq->runtime_enabled) |
4758 | continue; | |
4759 | ||
4760 | /* | |
4761 | * clock_task is not advancing so we just need to make sure | |
4762 | * there's some valid quota amount | |
4763 | */ | |
51f2176d | 4764 | cfs_rq->runtime_remaining = 1; |
0e59bdae KT |
4765 | /* |
4766 | * Offline rq is schedulable till cpu is completely disabled | |
4767 | * in take_cpu_down(), so we prevent new cfs throttling here. | |
4768 | */ | |
4769 | cfs_rq->runtime_enabled = 0; | |
4770 | ||
029632fb PZ |
4771 | if (cfs_rq_throttled(cfs_rq)) |
4772 | unthrottle_cfs_rq(cfs_rq); | |
4773 | } | |
502ce005 | 4774 | rcu_read_unlock(); |
029632fb PZ |
4775 | } |
4776 | ||
4777 | #else /* CONFIG_CFS_BANDWIDTH */ | |
f1b17280 PT |
4778 | static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq) |
4779 | { | |
78becc27 | 4780 | return rq_clock_task(rq_of(cfs_rq)); |
f1b17280 PT |
4781 | } |
4782 | ||
9dbdb155 | 4783 | static void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec) {} |
678d5718 | 4784 | static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq) { return false; } |
d3d9dc33 | 4785 | static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {} |
55e16d30 | 4786 | static inline void sync_throttle(struct task_group *tg, int cpu) {} |
6c16a6dc | 4787 | static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {} |
85dac906 PT |
4788 | |
4789 | static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq) | |
4790 | { | |
4791 | return 0; | |
4792 | } | |
64660c86 PT |
4793 | |
4794 | static inline int throttled_hierarchy(struct cfs_rq *cfs_rq) | |
4795 | { | |
4796 | return 0; | |
4797 | } | |
4798 | ||
4799 | static inline int throttled_lb_pair(struct task_group *tg, | |
4800 | int src_cpu, int dest_cpu) | |
4801 | { | |
4802 | return 0; | |
4803 | } | |
029632fb PZ |
4804 | |
4805 | void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {} | |
4806 | ||
4807 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
4808 | static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {} | |
ab84d31e PT |
4809 | #endif |
4810 | ||
029632fb PZ |
4811 | static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg) |
4812 | { | |
4813 | return NULL; | |
4814 | } | |
4815 | static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {} | |
0e59bdae | 4816 | static inline void update_runtime_enabled(struct rq *rq) {} |
a4c96ae3 | 4817 | static inline void unthrottle_offline_cfs_rqs(struct rq *rq) {} |
029632fb PZ |
4818 | |
4819 | #endif /* CONFIG_CFS_BANDWIDTH */ | |
4820 | ||
bf0f6f24 IM |
4821 | /************************************************** |
4822 | * CFS operations on tasks: | |
4823 | */ | |
4824 | ||
8f4d37ec PZ |
4825 | #ifdef CONFIG_SCHED_HRTICK |
4826 | static void hrtick_start_fair(struct rq *rq, struct task_struct *p) | |
4827 | { | |
8f4d37ec PZ |
4828 | struct sched_entity *se = &p->se; |
4829 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
4830 | ||
9148a3a1 | 4831 | SCHED_WARN_ON(task_rq(p) != rq); |
8f4d37ec | 4832 | |
8bf46a39 | 4833 | if (rq->cfs.h_nr_running > 1) { |
8f4d37ec PZ |
4834 | u64 slice = sched_slice(cfs_rq, se); |
4835 | u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime; | |
4836 | s64 delta = slice - ran; | |
4837 | ||
4838 | if (delta < 0) { | |
4839 | if (rq->curr == p) | |
8875125e | 4840 | resched_curr(rq); |
8f4d37ec PZ |
4841 | return; |
4842 | } | |
31656519 | 4843 | hrtick_start(rq, delta); |
8f4d37ec PZ |
4844 | } |
4845 | } | |
a4c2f00f PZ |
4846 | |
4847 | /* | |
4848 | * called from enqueue/dequeue and updates the hrtick when the | |
4849 | * current task is from our class and nr_running is low enough | |
4850 | * to matter. | |
4851 | */ | |
4852 | static void hrtick_update(struct rq *rq) | |
4853 | { | |
4854 | struct task_struct *curr = rq->curr; | |
4855 | ||
b39e66ea | 4856 | if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class) |
a4c2f00f PZ |
4857 | return; |
4858 | ||
4859 | if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency) | |
4860 | hrtick_start_fair(rq, curr); | |
4861 | } | |
55e12e5e | 4862 | #else /* !CONFIG_SCHED_HRTICK */ |
8f4d37ec PZ |
4863 | static inline void |
4864 | hrtick_start_fair(struct rq *rq, struct task_struct *p) | |
4865 | { | |
4866 | } | |
a4c2f00f PZ |
4867 | |
4868 | static inline void hrtick_update(struct rq *rq) | |
4869 | { | |
4870 | } | |
8f4d37ec PZ |
4871 | #endif |
4872 | ||
bf0f6f24 IM |
4873 | /* |
4874 | * The enqueue_task method is called before nr_running is | |
4875 | * increased. Here we update the fair scheduling stats and | |
4876 | * then put the task into the rbtree: | |
4877 | */ | |
ea87bb78 | 4878 | static void |
371fd7e7 | 4879 | enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags) |
bf0f6f24 IM |
4880 | { |
4881 | struct cfs_rq *cfs_rq; | |
62fb1851 | 4882 | struct sched_entity *se = &p->se; |
bf0f6f24 | 4883 | |
8c34ab19 RW |
4884 | /* |
4885 | * If in_iowait is set, the code below may not trigger any cpufreq | |
4886 | * utilization updates, so do it here explicitly with the IOWAIT flag | |
4887 | * passed. | |
4888 | */ | |
4889 | if (p->in_iowait) | |
674e7541 | 4890 | cpufreq_update_util(rq, SCHED_CPUFREQ_IOWAIT); |
8c34ab19 | 4891 | |
bf0f6f24 | 4892 | for_each_sched_entity(se) { |
62fb1851 | 4893 | if (se->on_rq) |
bf0f6f24 IM |
4894 | break; |
4895 | cfs_rq = cfs_rq_of(se); | |
88ec22d3 | 4896 | enqueue_entity(cfs_rq, se, flags); |
85dac906 PT |
4897 | |
4898 | /* | |
4899 | * end evaluation on encountering a throttled cfs_rq | |
4900 | * | |
4901 | * note: in the case of encountering a throttled cfs_rq we will | |
4902 | * post the final h_nr_running increment below. | |
e210bffd | 4903 | */ |
85dac906 PT |
4904 | if (cfs_rq_throttled(cfs_rq)) |
4905 | break; | |
953bfcd1 | 4906 | cfs_rq->h_nr_running++; |
85dac906 | 4907 | |
88ec22d3 | 4908 | flags = ENQUEUE_WAKEUP; |
bf0f6f24 | 4909 | } |
8f4d37ec | 4910 | |
2069dd75 | 4911 | for_each_sched_entity(se) { |
0f317143 | 4912 | cfs_rq = cfs_rq_of(se); |
953bfcd1 | 4913 | cfs_rq->h_nr_running++; |
2069dd75 | 4914 | |
85dac906 PT |
4915 | if (cfs_rq_throttled(cfs_rq)) |
4916 | break; | |
4917 | ||
d31b1a66 | 4918 | update_load_avg(se, UPDATE_TG); |
89ee048f | 4919 | update_cfs_shares(se); |
2069dd75 PZ |
4920 | } |
4921 | ||
cd126afe | 4922 | if (!se) |
72465447 | 4923 | add_nr_running(rq, 1); |
cd126afe | 4924 | |
a4c2f00f | 4925 | hrtick_update(rq); |
bf0f6f24 IM |
4926 | } |
4927 | ||
2f36825b VP |
4928 | static void set_next_buddy(struct sched_entity *se); |
4929 | ||
bf0f6f24 IM |
4930 | /* |
4931 | * The dequeue_task method is called before nr_running is | |
4932 | * decreased. We remove the task from the rbtree and | |
4933 | * update the fair scheduling stats: | |
4934 | */ | |
371fd7e7 | 4935 | static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags) |
bf0f6f24 IM |
4936 | { |
4937 | struct cfs_rq *cfs_rq; | |
62fb1851 | 4938 | struct sched_entity *se = &p->se; |
2f36825b | 4939 | int task_sleep = flags & DEQUEUE_SLEEP; |
bf0f6f24 IM |
4940 | |
4941 | for_each_sched_entity(se) { | |
4942 | cfs_rq = cfs_rq_of(se); | |
371fd7e7 | 4943 | dequeue_entity(cfs_rq, se, flags); |
85dac906 PT |
4944 | |
4945 | /* | |
4946 | * end evaluation on encountering a throttled cfs_rq | |
4947 | * | |
4948 | * note: in the case of encountering a throttled cfs_rq we will | |
4949 | * post the final h_nr_running decrement below. | |
4950 | */ | |
4951 | if (cfs_rq_throttled(cfs_rq)) | |
4952 | break; | |
953bfcd1 | 4953 | cfs_rq->h_nr_running--; |
2069dd75 | 4954 | |
bf0f6f24 | 4955 | /* Don't dequeue parent if it has other entities besides us */ |
2f36825b | 4956 | if (cfs_rq->load.weight) { |
754bd598 KK |
4957 | /* Avoid re-evaluating load for this entity: */ |
4958 | se = parent_entity(se); | |
2f36825b VP |
4959 | /* |
4960 | * Bias pick_next to pick a task from this cfs_rq, as | |
4961 | * p is sleeping when it is within its sched_slice. | |
4962 | */ | |
754bd598 KK |
4963 | if (task_sleep && se && !throttled_hierarchy(cfs_rq)) |
4964 | set_next_buddy(se); | |
bf0f6f24 | 4965 | break; |
2f36825b | 4966 | } |
371fd7e7 | 4967 | flags |= DEQUEUE_SLEEP; |
bf0f6f24 | 4968 | } |
8f4d37ec | 4969 | |
2069dd75 | 4970 | for_each_sched_entity(se) { |
0f317143 | 4971 | cfs_rq = cfs_rq_of(se); |
953bfcd1 | 4972 | cfs_rq->h_nr_running--; |
2069dd75 | 4973 | |
85dac906 PT |
4974 | if (cfs_rq_throttled(cfs_rq)) |
4975 | break; | |
4976 | ||
d31b1a66 | 4977 | update_load_avg(se, UPDATE_TG); |
89ee048f | 4978 | update_cfs_shares(se); |
2069dd75 PZ |
4979 | } |
4980 | ||
cd126afe | 4981 | if (!se) |
72465447 | 4982 | sub_nr_running(rq, 1); |
cd126afe | 4983 | |
a4c2f00f | 4984 | hrtick_update(rq); |
bf0f6f24 IM |
4985 | } |
4986 | ||
e7693a36 | 4987 | #ifdef CONFIG_SMP |
10e2f1ac PZ |
4988 | |
4989 | /* Working cpumask for: load_balance, load_balance_newidle. */ | |
4990 | DEFINE_PER_CPU(cpumask_var_t, load_balance_mask); | |
4991 | DEFINE_PER_CPU(cpumask_var_t, select_idle_mask); | |
4992 | ||
9fd81dd5 | 4993 | #ifdef CONFIG_NO_HZ_COMMON |
3289bdb4 PZ |
4994 | /* |
4995 | * per rq 'load' arrray crap; XXX kill this. | |
4996 | */ | |
4997 | ||
4998 | /* | |
d937cdc5 | 4999 | * The exact cpuload calculated at every tick would be: |
3289bdb4 | 5000 | * |
d937cdc5 PZ |
5001 | * load' = (1 - 1/2^i) * load + (1/2^i) * cur_load |
5002 | * | |
5003 | * If a cpu misses updates for n ticks (as it was idle) and update gets | |
5004 | * called on the n+1-th tick when cpu may be busy, then we have: | |
5005 | * | |
5006 | * load_n = (1 - 1/2^i)^n * load_0 | |
5007 | * load_n+1 = (1 - 1/2^i) * load_n + (1/2^i) * cur_load | |
3289bdb4 PZ |
5008 | * |
5009 | * decay_load_missed() below does efficient calculation of | |
3289bdb4 | 5010 | * |
d937cdc5 PZ |
5011 | * load' = (1 - 1/2^i)^n * load |
5012 | * | |
5013 | * Because x^(n+m) := x^n * x^m we can decompose any x^n in power-of-2 factors. | |
5014 | * This allows us to precompute the above in said factors, thereby allowing the | |
5015 | * reduction of an arbitrary n in O(log_2 n) steps. (See also | |
5016 | * fixed_power_int()) | |
3289bdb4 | 5017 | * |
d937cdc5 | 5018 | * The calculation is approximated on a 128 point scale. |
3289bdb4 PZ |
5019 | */ |
5020 | #define DEGRADE_SHIFT 7 | |
d937cdc5 PZ |
5021 | |
5022 | static const u8 degrade_zero_ticks[CPU_LOAD_IDX_MAX] = {0, 8, 32, 64, 128}; | |
5023 | static const u8 degrade_factor[CPU_LOAD_IDX_MAX][DEGRADE_SHIFT + 1] = { | |
5024 | { 0, 0, 0, 0, 0, 0, 0, 0 }, | |
5025 | { 64, 32, 8, 0, 0, 0, 0, 0 }, | |
5026 | { 96, 72, 40, 12, 1, 0, 0, 0 }, | |
5027 | { 112, 98, 75, 43, 15, 1, 0, 0 }, | |
5028 | { 120, 112, 98, 76, 45, 16, 2, 0 } | |
5029 | }; | |
3289bdb4 PZ |
5030 | |
5031 | /* | |
5032 | * Update cpu_load for any missed ticks, due to tickless idle. The backlog | |
5033 | * would be when CPU is idle and so we just decay the old load without | |
5034 | * adding any new load. | |
5035 | */ | |
5036 | static unsigned long | |
5037 | decay_load_missed(unsigned long load, unsigned long missed_updates, int idx) | |
5038 | { | |
5039 | int j = 0; | |
5040 | ||
5041 | if (!missed_updates) | |
5042 | return load; | |
5043 | ||
5044 | if (missed_updates >= degrade_zero_ticks[idx]) | |
5045 | return 0; | |
5046 | ||
5047 | if (idx == 1) | |
5048 | return load >> missed_updates; | |
5049 | ||
5050 | while (missed_updates) { | |
5051 | if (missed_updates % 2) | |
5052 | load = (load * degrade_factor[idx][j]) >> DEGRADE_SHIFT; | |
5053 | ||
5054 | missed_updates >>= 1; | |
5055 | j++; | |
5056 | } | |
5057 | return load; | |
5058 | } | |
9fd81dd5 | 5059 | #endif /* CONFIG_NO_HZ_COMMON */ |
3289bdb4 | 5060 | |
59543275 | 5061 | /** |
cee1afce | 5062 | * __cpu_load_update - update the rq->cpu_load[] statistics |
59543275 BP |
5063 | * @this_rq: The rq to update statistics for |
5064 | * @this_load: The current load | |
5065 | * @pending_updates: The number of missed updates | |
59543275 | 5066 | * |
3289bdb4 | 5067 | * Update rq->cpu_load[] statistics. This function is usually called every |
59543275 BP |
5068 | * scheduler tick (TICK_NSEC). |
5069 | * | |
5070 | * This function computes a decaying average: | |
5071 | * | |
5072 | * load[i]' = (1 - 1/2^i) * load[i] + (1/2^i) * load | |
5073 | * | |
5074 | * Because of NOHZ it might not get called on every tick which gives need for | |
5075 | * the @pending_updates argument. | |
5076 | * | |
5077 | * load[i]_n = (1 - 1/2^i) * load[i]_n-1 + (1/2^i) * load_n-1 | |
5078 | * = A * load[i]_n-1 + B ; A := (1 - 1/2^i), B := (1/2^i) * load | |
5079 | * = A * (A * load[i]_n-2 + B) + B | |
5080 | * = A * (A * (A * load[i]_n-3 + B) + B) + B | |
5081 | * = A^3 * load[i]_n-3 + (A^2 + A + 1) * B | |
5082 | * = A^n * load[i]_0 + (A^(n-1) + A^(n-2) + ... + 1) * B | |
5083 | * = A^n * load[i]_0 + ((1 - A^n) / (1 - A)) * B | |
5084 | * = (1 - 1/2^i)^n * (load[i]_0 - load) + load | |
5085 | * | |
5086 | * In the above we've assumed load_n := load, which is true for NOHZ_FULL as | |
5087 | * any change in load would have resulted in the tick being turned back on. | |
5088 | * | |
5089 | * For regular NOHZ, this reduces to: | |
5090 | * | |
5091 | * load[i]_n = (1 - 1/2^i)^n * load[i]_0 | |
5092 | * | |
5093 | * see decay_load_misses(). For NOHZ_FULL we get to subtract and add the extra | |
1f41906a | 5094 | * term. |
3289bdb4 | 5095 | */ |
1f41906a FW |
5096 | static void cpu_load_update(struct rq *this_rq, unsigned long this_load, |
5097 | unsigned long pending_updates) | |
3289bdb4 | 5098 | { |
9fd81dd5 | 5099 | unsigned long __maybe_unused tickless_load = this_rq->cpu_load[0]; |
3289bdb4 PZ |
5100 | int i, scale; |
5101 | ||
5102 | this_rq->nr_load_updates++; | |
5103 | ||
5104 | /* Update our load: */ | |
5105 | this_rq->cpu_load[0] = this_load; /* Fasttrack for idx 0 */ | |
5106 | for (i = 1, scale = 2; i < CPU_LOAD_IDX_MAX; i++, scale += scale) { | |
5107 | unsigned long old_load, new_load; | |
5108 | ||
5109 | /* scale is effectively 1 << i now, and >> i divides by scale */ | |
5110 | ||
7400d3bb | 5111 | old_load = this_rq->cpu_load[i]; |
9fd81dd5 | 5112 | #ifdef CONFIG_NO_HZ_COMMON |
3289bdb4 | 5113 | old_load = decay_load_missed(old_load, pending_updates - 1, i); |
7400d3bb BP |
5114 | if (tickless_load) { |
5115 | old_load -= decay_load_missed(tickless_load, pending_updates - 1, i); | |
5116 | /* | |
5117 | * old_load can never be a negative value because a | |
5118 | * decayed tickless_load cannot be greater than the | |
5119 | * original tickless_load. | |
5120 | */ | |
5121 | old_load += tickless_load; | |
5122 | } | |
9fd81dd5 | 5123 | #endif |
3289bdb4 PZ |
5124 | new_load = this_load; |
5125 | /* | |
5126 | * Round up the averaging division if load is increasing. This | |
5127 | * prevents us from getting stuck on 9 if the load is 10, for | |
5128 | * example. | |
5129 | */ | |
5130 | if (new_load > old_load) | |
5131 | new_load += scale - 1; | |
5132 | ||
5133 | this_rq->cpu_load[i] = (old_load * (scale - 1) + new_load) >> i; | |
5134 | } | |
5135 | ||
5136 | sched_avg_update(this_rq); | |
5137 | } | |
5138 | ||
7ea241af | 5139 | /* Used instead of source_load when we know the type == 0 */ |
c7132dd6 | 5140 | static unsigned long weighted_cpuload(struct rq *rq) |
7ea241af | 5141 | { |
c7132dd6 | 5142 | return cfs_rq_runnable_load_avg(&rq->cfs); |
7ea241af YD |
5143 | } |
5144 | ||
3289bdb4 | 5145 | #ifdef CONFIG_NO_HZ_COMMON |
1f41906a FW |
5146 | /* |
5147 | * There is no sane way to deal with nohz on smp when using jiffies because the | |
5148 | * cpu doing the jiffies update might drift wrt the cpu doing the jiffy reading | |
5149 | * causing off-by-one errors in observed deltas; {0,2} instead of {1,1}. | |
5150 | * | |
5151 | * Therefore we need to avoid the delta approach from the regular tick when | |
5152 | * possible since that would seriously skew the load calculation. This is why we | |
5153 | * use cpu_load_update_periodic() for CPUs out of nohz. However we'll rely on | |
5154 | * jiffies deltas for updates happening while in nohz mode (idle ticks, idle | |
5155 | * loop exit, nohz_idle_balance, nohz full exit...) | |
5156 | * | |
5157 | * This means we might still be one tick off for nohz periods. | |
5158 | */ | |
5159 | ||
5160 | static void cpu_load_update_nohz(struct rq *this_rq, | |
5161 | unsigned long curr_jiffies, | |
5162 | unsigned long load) | |
be68a682 FW |
5163 | { |
5164 | unsigned long pending_updates; | |
5165 | ||
5166 | pending_updates = curr_jiffies - this_rq->last_load_update_tick; | |
5167 | if (pending_updates) { | |
5168 | this_rq->last_load_update_tick = curr_jiffies; | |
5169 | /* | |
5170 | * In the regular NOHZ case, we were idle, this means load 0. | |
5171 | * In the NOHZ_FULL case, we were non-idle, we should consider | |
5172 | * its weighted load. | |
5173 | */ | |
1f41906a | 5174 | cpu_load_update(this_rq, load, pending_updates); |
be68a682 FW |
5175 | } |
5176 | } | |
5177 | ||
3289bdb4 PZ |
5178 | /* |
5179 | * Called from nohz_idle_balance() to update the load ratings before doing the | |
5180 | * idle balance. | |
5181 | */ | |
cee1afce | 5182 | static void cpu_load_update_idle(struct rq *this_rq) |
3289bdb4 | 5183 | { |
3289bdb4 PZ |
5184 | /* |
5185 | * bail if there's load or we're actually up-to-date. | |
5186 | */ | |
c7132dd6 | 5187 | if (weighted_cpuload(this_rq)) |
3289bdb4 PZ |
5188 | return; |
5189 | ||
1f41906a | 5190 | cpu_load_update_nohz(this_rq, READ_ONCE(jiffies), 0); |
3289bdb4 PZ |
5191 | } |
5192 | ||
5193 | /* | |
1f41906a FW |
5194 | * Record CPU load on nohz entry so we know the tickless load to account |
5195 | * on nohz exit. cpu_load[0] happens then to be updated more frequently | |
5196 | * than other cpu_load[idx] but it should be fine as cpu_load readers | |
5197 | * shouldn't rely into synchronized cpu_load[*] updates. | |
3289bdb4 | 5198 | */ |
1f41906a | 5199 | void cpu_load_update_nohz_start(void) |
3289bdb4 PZ |
5200 | { |
5201 | struct rq *this_rq = this_rq(); | |
1f41906a FW |
5202 | |
5203 | /* | |
5204 | * This is all lockless but should be fine. If weighted_cpuload changes | |
5205 | * concurrently we'll exit nohz. And cpu_load write can race with | |
5206 | * cpu_load_update_idle() but both updater would be writing the same. | |
5207 | */ | |
c7132dd6 | 5208 | this_rq->cpu_load[0] = weighted_cpuload(this_rq); |
1f41906a FW |
5209 | } |
5210 | ||
5211 | /* | |
5212 | * Account the tickless load in the end of a nohz frame. | |
5213 | */ | |
5214 | void cpu_load_update_nohz_stop(void) | |
5215 | { | |
316c1608 | 5216 | unsigned long curr_jiffies = READ_ONCE(jiffies); |
1f41906a FW |
5217 | struct rq *this_rq = this_rq(); |
5218 | unsigned long load; | |
8a8c69c3 | 5219 | struct rq_flags rf; |
3289bdb4 PZ |
5220 | |
5221 | if (curr_jiffies == this_rq->last_load_update_tick) | |
5222 | return; | |
5223 | ||
c7132dd6 | 5224 | load = weighted_cpuload(this_rq); |
8a8c69c3 | 5225 | rq_lock(this_rq, &rf); |
b52fad2d | 5226 | update_rq_clock(this_rq); |
1f41906a | 5227 | cpu_load_update_nohz(this_rq, curr_jiffies, load); |
8a8c69c3 | 5228 | rq_unlock(this_rq, &rf); |
3289bdb4 | 5229 | } |
1f41906a FW |
5230 | #else /* !CONFIG_NO_HZ_COMMON */ |
5231 | static inline void cpu_load_update_nohz(struct rq *this_rq, | |
5232 | unsigned long curr_jiffies, | |
5233 | unsigned long load) { } | |
5234 | #endif /* CONFIG_NO_HZ_COMMON */ | |
5235 | ||
5236 | static void cpu_load_update_periodic(struct rq *this_rq, unsigned long load) | |
5237 | { | |
9fd81dd5 | 5238 | #ifdef CONFIG_NO_HZ_COMMON |
1f41906a FW |
5239 | /* See the mess around cpu_load_update_nohz(). */ |
5240 | this_rq->last_load_update_tick = READ_ONCE(jiffies); | |
9fd81dd5 | 5241 | #endif |
1f41906a FW |
5242 | cpu_load_update(this_rq, load, 1); |
5243 | } | |
3289bdb4 PZ |
5244 | |
5245 | /* | |
5246 | * Called from scheduler_tick() | |
5247 | */ | |
cee1afce | 5248 | void cpu_load_update_active(struct rq *this_rq) |
3289bdb4 | 5249 | { |
c7132dd6 | 5250 | unsigned long load = weighted_cpuload(this_rq); |
1f41906a FW |
5251 | |
5252 | if (tick_nohz_tick_stopped()) | |
5253 | cpu_load_update_nohz(this_rq, READ_ONCE(jiffies), load); | |
5254 | else | |
5255 | cpu_load_update_periodic(this_rq, load); | |
3289bdb4 PZ |
5256 | } |
5257 | ||
029632fb PZ |
5258 | /* |
5259 | * Return a low guess at the load of a migration-source cpu weighted | |
5260 | * according to the scheduling class and "nice" value. | |
5261 | * | |
5262 | * We want to under-estimate the load of migration sources, to | |
5263 | * balance conservatively. | |
5264 | */ | |
5265 | static unsigned long source_load(int cpu, int type) | |
5266 | { | |
5267 | struct rq *rq = cpu_rq(cpu); | |
c7132dd6 | 5268 | unsigned long total = weighted_cpuload(rq); |
029632fb PZ |
5269 | |
5270 | if (type == 0 || !sched_feat(LB_BIAS)) | |
5271 | return total; | |
5272 | ||
5273 | return min(rq->cpu_load[type-1], total); | |
5274 | } | |
5275 | ||
5276 | /* | |
5277 | * Return a high guess at the load of a migration-target cpu weighted | |
5278 | * according to the scheduling class and "nice" value. | |
5279 | */ | |
5280 | static unsigned long target_load(int cpu, int type) | |
5281 | { | |
5282 | struct rq *rq = cpu_rq(cpu); | |
c7132dd6 | 5283 | unsigned long total = weighted_cpuload(rq); |
029632fb PZ |
5284 | |
5285 | if (type == 0 || !sched_feat(LB_BIAS)) | |
5286 | return total; | |
5287 | ||
5288 | return max(rq->cpu_load[type-1], total); | |
5289 | } | |
5290 | ||
ced549fa | 5291 | static unsigned long capacity_of(int cpu) |
029632fb | 5292 | { |
ced549fa | 5293 | return cpu_rq(cpu)->cpu_capacity; |
029632fb PZ |
5294 | } |
5295 | ||
ca6d75e6 VG |
5296 | static unsigned long capacity_orig_of(int cpu) |
5297 | { | |
5298 | return cpu_rq(cpu)->cpu_capacity_orig; | |
5299 | } | |
5300 | ||
029632fb PZ |
5301 | static unsigned long cpu_avg_load_per_task(int cpu) |
5302 | { | |
5303 | struct rq *rq = cpu_rq(cpu); | |
316c1608 | 5304 | unsigned long nr_running = READ_ONCE(rq->cfs.h_nr_running); |
c7132dd6 | 5305 | unsigned long load_avg = weighted_cpuload(rq); |
029632fb PZ |
5306 | |
5307 | if (nr_running) | |
b92486cb | 5308 | return load_avg / nr_running; |
029632fb PZ |
5309 | |
5310 | return 0; | |
5311 | } | |
5312 | ||
c58d25f3 PZ |
5313 | static void record_wakee(struct task_struct *p) |
5314 | { | |
5315 | /* | |
5316 | * Only decay a single time; tasks that have less then 1 wakeup per | |
5317 | * jiffy will not have built up many flips. | |
5318 | */ | |
5319 | if (time_after(jiffies, current->wakee_flip_decay_ts + HZ)) { | |
5320 | current->wakee_flips >>= 1; | |
5321 | current->wakee_flip_decay_ts = jiffies; | |
5322 | } | |
5323 | ||
5324 | if (current->last_wakee != p) { | |
5325 | current->last_wakee = p; | |
5326 | current->wakee_flips++; | |
5327 | } | |
5328 | } | |
5329 | ||
63b0e9ed MG |
5330 | /* |
5331 | * Detect M:N waker/wakee relationships via a switching-frequency heuristic. | |
c58d25f3 | 5332 | * |
63b0e9ed | 5333 | * A waker of many should wake a different task than the one last awakened |
c58d25f3 PZ |
5334 | * at a frequency roughly N times higher than one of its wakees. |
5335 | * | |
5336 | * In order to determine whether we should let the load spread vs consolidating | |
5337 | * to shared cache, we look for a minimum 'flip' frequency of llc_size in one | |
5338 | * partner, and a factor of lls_size higher frequency in the other. | |
5339 | * | |
5340 | * With both conditions met, we can be relatively sure that the relationship is | |
5341 | * non-monogamous, with partner count exceeding socket size. | |
5342 | * | |
5343 | * Waker/wakee being client/server, worker/dispatcher, interrupt source or | |
5344 | * whatever is irrelevant, spread criteria is apparent partner count exceeds | |
5345 | * socket size. | |
63b0e9ed | 5346 | */ |
62470419 MW |
5347 | static int wake_wide(struct task_struct *p) |
5348 | { | |
63b0e9ed MG |
5349 | unsigned int master = current->wakee_flips; |
5350 | unsigned int slave = p->wakee_flips; | |
7d9ffa89 | 5351 | int factor = this_cpu_read(sd_llc_size); |
62470419 | 5352 | |
63b0e9ed MG |
5353 | if (master < slave) |
5354 | swap(master, slave); | |
5355 | if (slave < factor || master < slave * factor) | |
5356 | return 0; | |
5357 | return 1; | |
62470419 MW |
5358 | } |
5359 | ||
90001d67 | 5360 | /* |
d153b153 PZ |
5361 | * The purpose of wake_affine() is to quickly determine on which CPU we can run |
5362 | * soonest. For the purpose of speed we only consider the waking and previous | |
5363 | * CPU. | |
90001d67 | 5364 | * |
d153b153 PZ |
5365 | * wake_affine_idle() - only considers 'now', it check if the waking CPU is (or |
5366 | * will be) idle. | |
f2cdd9cc PZ |
5367 | * |
5368 | * wake_affine_weight() - considers the weight to reflect the average | |
5369 | * scheduling latency of the CPUs. This seems to work | |
5370 | * for the overloaded case. | |
90001d67 | 5371 | */ |
d153b153 | 5372 | |
90001d67 | 5373 | static bool |
d153b153 PZ |
5374 | wake_affine_idle(struct sched_domain *sd, struct task_struct *p, |
5375 | int this_cpu, int prev_cpu, int sync) | |
90001d67 | 5376 | { |
d153b153 | 5377 | if (idle_cpu(this_cpu)) |
90001d67 PZ |
5378 | return true; |
5379 | ||
d153b153 PZ |
5380 | if (sync && cpu_rq(this_cpu)->nr_running == 1) |
5381 | return true; | |
90001d67 | 5382 | |
d153b153 | 5383 | return false; |
90001d67 PZ |
5384 | } |
5385 | ||
f2cdd9cc PZ |
5386 | static bool |
5387 | wake_affine_weight(struct sched_domain *sd, struct task_struct *p, | |
5388 | int this_cpu, int prev_cpu, int sync) | |
5389 | { | |
5390 | s64 this_eff_load, prev_eff_load; | |
5391 | unsigned long task_load; | |
5392 | ||
5393 | this_eff_load = target_load(this_cpu, sd->wake_idx); | |
5394 | prev_eff_load = source_load(prev_cpu, sd->wake_idx); | |
5395 | ||
5396 | if (sync) { | |
5397 | unsigned long current_load = task_h_load(current); | |
5398 | ||
5399 | if (current_load > this_eff_load) | |
5400 | return true; | |
5401 | ||
5402 | this_eff_load -= current_load; | |
5403 | } | |
5404 | ||
5405 | task_load = task_h_load(p); | |
5406 | ||
5407 | this_eff_load += task_load; | |
5408 | if (sched_feat(WA_BIAS)) | |
5409 | this_eff_load *= 100; | |
5410 | this_eff_load *= capacity_of(prev_cpu); | |
5411 | ||
5412 | prev_eff_load -= task_load; | |
5413 | if (sched_feat(WA_BIAS)) | |
5414 | prev_eff_load *= 100 + (sd->imbalance_pct - 100) / 2; | |
5415 | prev_eff_load *= capacity_of(this_cpu); | |
5416 | ||
5417 | return this_eff_load <= prev_eff_load; | |
5418 | } | |
5419 | ||
772bd008 MR |
5420 | static int wake_affine(struct sched_domain *sd, struct task_struct *p, |
5421 | int prev_cpu, int sync) | |
098fb9db | 5422 | { |
3fed382b | 5423 | int this_cpu = smp_processor_id(); |
d153b153 | 5424 | bool affine = false; |
90001d67 | 5425 | |
d153b153 PZ |
5426 | if (sched_feat(WA_IDLE) && !affine) |
5427 | affine = wake_affine_idle(sd, p, this_cpu, prev_cpu, sync); | |
098fb9db | 5428 | |
f2cdd9cc PZ |
5429 | if (sched_feat(WA_WEIGHT) && !affine) |
5430 | affine = wake_affine_weight(sd, p, this_cpu, prev_cpu, sync); | |
5431 | ||
ae92882e | 5432 | schedstat_inc(p->se.statistics.nr_wakeups_affine_attempts); |
3fed382b RR |
5433 | if (affine) { |
5434 | schedstat_inc(sd->ttwu_move_affine); | |
5435 | schedstat_inc(p->se.statistics.nr_wakeups_affine); | |
5436 | } | |
098fb9db | 5437 | |
3fed382b | 5438 | return affine; |
098fb9db IM |
5439 | } |
5440 | ||
6a0b19c0 MR |
5441 | static inline int task_util(struct task_struct *p); |
5442 | static int cpu_util_wake(int cpu, struct task_struct *p); | |
5443 | ||
5444 | static unsigned long capacity_spare_wake(int cpu, struct task_struct *p) | |
5445 | { | |
5446 | return capacity_orig_of(cpu) - cpu_util_wake(cpu, p); | |
5447 | } | |
5448 | ||
aaee1203 PZ |
5449 | /* |
5450 | * find_idlest_group finds and returns the least busy CPU group within the | |
5451 | * domain. | |
5452 | */ | |
5453 | static struct sched_group * | |
78e7ed53 | 5454 | find_idlest_group(struct sched_domain *sd, struct task_struct *p, |
c44f2a02 | 5455 | int this_cpu, int sd_flag) |
e7693a36 | 5456 | { |
b3bd3de6 | 5457 | struct sched_group *idlest = NULL, *group = sd->groups; |
6a0b19c0 | 5458 | struct sched_group *most_spare_sg = NULL; |
6b94780e VG |
5459 | unsigned long min_runnable_load = ULONG_MAX, this_runnable_load = 0; |
5460 | unsigned long min_avg_load = ULONG_MAX, this_avg_load = 0; | |
6a0b19c0 | 5461 | unsigned long most_spare = 0, this_spare = 0; |
c44f2a02 | 5462 | int load_idx = sd->forkexec_idx; |
6b94780e VG |
5463 | int imbalance_scale = 100 + (sd->imbalance_pct-100)/2; |
5464 | unsigned long imbalance = scale_load_down(NICE_0_LOAD) * | |
5465 | (sd->imbalance_pct-100) / 100; | |
e7693a36 | 5466 | |
c44f2a02 VG |
5467 | if (sd_flag & SD_BALANCE_WAKE) |
5468 | load_idx = sd->wake_idx; | |
5469 | ||
aaee1203 | 5470 | do { |
6b94780e VG |
5471 | unsigned long load, avg_load, runnable_load; |
5472 | unsigned long spare_cap, max_spare_cap; | |
aaee1203 PZ |
5473 | int local_group; |
5474 | int i; | |
e7693a36 | 5475 | |
aaee1203 | 5476 | /* Skip over this group if it has no CPUs allowed */ |
ae4df9d6 | 5477 | if (!cpumask_intersects(sched_group_span(group), |
0c98d344 | 5478 | &p->cpus_allowed)) |
aaee1203 PZ |
5479 | continue; |
5480 | ||
5481 | local_group = cpumask_test_cpu(this_cpu, | |
ae4df9d6 | 5482 | sched_group_span(group)); |
aaee1203 | 5483 | |
6a0b19c0 MR |
5484 | /* |
5485 | * Tally up the load of all CPUs in the group and find | |
5486 | * the group containing the CPU with most spare capacity. | |
5487 | */ | |
aaee1203 | 5488 | avg_load = 0; |
6b94780e | 5489 | runnable_load = 0; |
6a0b19c0 | 5490 | max_spare_cap = 0; |
aaee1203 | 5491 | |
ae4df9d6 | 5492 | for_each_cpu(i, sched_group_span(group)) { |
aaee1203 PZ |
5493 | /* Bias balancing toward cpus of our domain */ |
5494 | if (local_group) | |
5495 | load = source_load(i, load_idx); | |
5496 | else | |
5497 | load = target_load(i, load_idx); | |
5498 | ||
6b94780e VG |
5499 | runnable_load += load; |
5500 | ||
5501 | avg_load += cfs_rq_load_avg(&cpu_rq(i)->cfs); | |
6a0b19c0 MR |
5502 | |
5503 | spare_cap = capacity_spare_wake(i, p); | |
5504 | ||
5505 | if (spare_cap > max_spare_cap) | |
5506 | max_spare_cap = spare_cap; | |
aaee1203 PZ |
5507 | } |
5508 | ||
63b2ca30 | 5509 | /* Adjust by relative CPU capacity of the group */ |
6b94780e VG |
5510 | avg_load = (avg_load * SCHED_CAPACITY_SCALE) / |
5511 | group->sgc->capacity; | |
5512 | runnable_load = (runnable_load * SCHED_CAPACITY_SCALE) / | |
5513 | group->sgc->capacity; | |
aaee1203 PZ |
5514 | |
5515 | if (local_group) { | |
6b94780e VG |
5516 | this_runnable_load = runnable_load; |
5517 | this_avg_load = avg_load; | |
6a0b19c0 MR |
5518 | this_spare = max_spare_cap; |
5519 | } else { | |
6b94780e VG |
5520 | if (min_runnable_load > (runnable_load + imbalance)) { |
5521 | /* | |
5522 | * The runnable load is significantly smaller | |
5523 | * so we can pick this new cpu | |
5524 | */ | |
5525 | min_runnable_load = runnable_load; | |
5526 | min_avg_load = avg_load; | |
5527 | idlest = group; | |
5528 | } else if ((runnable_load < (min_runnable_load + imbalance)) && | |
5529 | (100*min_avg_load > imbalance_scale*avg_load)) { | |
5530 | /* | |
5531 | * The runnable loads are close so take the | |
5532 | * blocked load into account through avg_load. | |
5533 | */ | |
5534 | min_avg_load = avg_load; | |
6a0b19c0 MR |
5535 | idlest = group; |
5536 | } | |
5537 | ||
5538 | if (most_spare < max_spare_cap) { | |
5539 | most_spare = max_spare_cap; | |
5540 | most_spare_sg = group; | |
5541 | } | |
aaee1203 PZ |
5542 | } |
5543 | } while (group = group->next, group != sd->groups); | |
5544 | ||
6a0b19c0 MR |
5545 | /* |
5546 | * The cross-over point between using spare capacity or least load | |
5547 | * is too conservative for high utilization tasks on partially | |
5548 | * utilized systems if we require spare_capacity > task_util(p), | |
5549 | * so we allow for some task stuffing by using | |
5550 | * spare_capacity > task_util(p)/2. | |
f519a3f1 VG |
5551 | * |
5552 | * Spare capacity can't be used for fork because the utilization has | |
5553 | * not been set yet, we must first select a rq to compute the initial | |
5554 | * utilization. | |
6a0b19c0 | 5555 | */ |
f519a3f1 VG |
5556 | if (sd_flag & SD_BALANCE_FORK) |
5557 | goto skip_spare; | |
5558 | ||
6a0b19c0 | 5559 | if (this_spare > task_util(p) / 2 && |
6b94780e | 5560 | imbalance_scale*this_spare > 100*most_spare) |
6a0b19c0 | 5561 | return NULL; |
6b94780e VG |
5562 | |
5563 | if (most_spare > task_util(p) / 2) | |
6a0b19c0 MR |
5564 | return most_spare_sg; |
5565 | ||
f519a3f1 | 5566 | skip_spare: |
6b94780e VG |
5567 | if (!idlest) |
5568 | return NULL; | |
5569 | ||
5570 | if (min_runnable_load > (this_runnable_load + imbalance)) | |
aaee1203 | 5571 | return NULL; |
6b94780e VG |
5572 | |
5573 | if ((this_runnable_load < (min_runnable_load + imbalance)) && | |
5574 | (100*this_avg_load < imbalance_scale*min_avg_load)) | |
5575 | return NULL; | |
5576 | ||
aaee1203 PZ |
5577 | return idlest; |
5578 | } | |
5579 | ||
5580 | /* | |
5581 | * find_idlest_cpu - find the idlest cpu among the cpus in group. | |
5582 | */ | |
5583 | static int | |
5584 | find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu) | |
5585 | { | |
5586 | unsigned long load, min_load = ULONG_MAX; | |
83a0a96a NP |
5587 | unsigned int min_exit_latency = UINT_MAX; |
5588 | u64 latest_idle_timestamp = 0; | |
5589 | int least_loaded_cpu = this_cpu; | |
5590 | int shallowest_idle_cpu = -1; | |
aaee1203 PZ |
5591 | int i; |
5592 | ||
eaecf41f MR |
5593 | /* Check if we have any choice: */ |
5594 | if (group->group_weight == 1) | |
ae4df9d6 | 5595 | return cpumask_first(sched_group_span(group)); |
eaecf41f | 5596 | |
aaee1203 | 5597 | /* Traverse only the allowed CPUs */ |
ae4df9d6 | 5598 | for_each_cpu_and(i, sched_group_span(group), &p->cpus_allowed) { |
83a0a96a NP |
5599 | if (idle_cpu(i)) { |
5600 | struct rq *rq = cpu_rq(i); | |
5601 | struct cpuidle_state *idle = idle_get_state(rq); | |
5602 | if (idle && idle->exit_latency < min_exit_latency) { | |
5603 | /* | |
5604 | * We give priority to a CPU whose idle state | |
5605 | * has the smallest exit latency irrespective | |
5606 | * of any idle timestamp. | |
5607 | */ | |
5608 | min_exit_latency = idle->exit_latency; | |
5609 | latest_idle_timestamp = rq->idle_stamp; | |
5610 | shallowest_idle_cpu = i; | |
5611 | } else if ((!idle || idle->exit_latency == min_exit_latency) && | |
5612 | rq->idle_stamp > latest_idle_timestamp) { | |
5613 | /* | |
5614 | * If equal or no active idle state, then | |
5615 | * the most recently idled CPU might have | |
5616 | * a warmer cache. | |
5617 | */ | |
5618 | latest_idle_timestamp = rq->idle_stamp; | |
5619 | shallowest_idle_cpu = i; | |
5620 | } | |
9f96742a | 5621 | } else if (shallowest_idle_cpu == -1) { |
c7132dd6 | 5622 | load = weighted_cpuload(cpu_rq(i)); |
83a0a96a NP |
5623 | if (load < min_load || (load == min_load && i == this_cpu)) { |
5624 | min_load = load; | |
5625 | least_loaded_cpu = i; | |
5626 | } | |
e7693a36 GH |
5627 | } |
5628 | } | |
5629 | ||
83a0a96a | 5630 | return shallowest_idle_cpu != -1 ? shallowest_idle_cpu : least_loaded_cpu; |
aaee1203 | 5631 | } |
e7693a36 | 5632 | |
10e2f1ac PZ |
5633 | #ifdef CONFIG_SCHED_SMT |
5634 | ||
5635 | static inline void set_idle_cores(int cpu, int val) | |
5636 | { | |
5637 | struct sched_domain_shared *sds; | |
5638 | ||
5639 | sds = rcu_dereference(per_cpu(sd_llc_shared, cpu)); | |
5640 | if (sds) | |
5641 | WRITE_ONCE(sds->has_idle_cores, val); | |
5642 | } | |
5643 | ||
5644 | static inline bool test_idle_cores(int cpu, bool def) | |
5645 | { | |
5646 | struct sched_domain_shared *sds; | |
5647 | ||
5648 | sds = rcu_dereference(per_cpu(sd_llc_shared, cpu)); | |
5649 | if (sds) | |
5650 | return READ_ONCE(sds->has_idle_cores); | |
5651 | ||
5652 | return def; | |
5653 | } | |
5654 | ||
5655 | /* | |
5656 | * Scans the local SMT mask to see if the entire core is idle, and records this | |
5657 | * information in sd_llc_shared->has_idle_cores. | |
5658 | * | |
5659 | * Since SMT siblings share all cache levels, inspecting this limited remote | |
5660 | * state should be fairly cheap. | |
5661 | */ | |
1b568f0a | 5662 | void __update_idle_core(struct rq *rq) |
10e2f1ac PZ |
5663 | { |
5664 | int core = cpu_of(rq); | |
5665 | int cpu; | |
5666 | ||
5667 | rcu_read_lock(); | |
5668 | if (test_idle_cores(core, true)) | |
5669 | goto unlock; | |
5670 | ||
5671 | for_each_cpu(cpu, cpu_smt_mask(core)) { | |
5672 | if (cpu == core) | |
5673 | continue; | |
5674 | ||
5675 | if (!idle_cpu(cpu)) | |
5676 | goto unlock; | |
5677 | } | |
5678 | ||
5679 | set_idle_cores(core, 1); | |
5680 | unlock: | |
5681 | rcu_read_unlock(); | |
5682 | } | |
5683 | ||
5684 | /* | |
5685 | * Scan the entire LLC domain for idle cores; this dynamically switches off if | |
5686 | * there are no idle cores left in the system; tracked through | |
5687 | * sd_llc->shared->has_idle_cores and enabled through update_idle_core() above. | |
5688 | */ | |
5689 | static int select_idle_core(struct task_struct *p, struct sched_domain *sd, int target) | |
5690 | { | |
5691 | struct cpumask *cpus = this_cpu_cpumask_var_ptr(select_idle_mask); | |
c743f0a5 | 5692 | int core, cpu; |
10e2f1ac | 5693 | |
1b568f0a PZ |
5694 | if (!static_branch_likely(&sched_smt_present)) |
5695 | return -1; | |
5696 | ||
10e2f1ac PZ |
5697 | if (!test_idle_cores(target, false)) |
5698 | return -1; | |
5699 | ||
0c98d344 | 5700 | cpumask_and(cpus, sched_domain_span(sd), &p->cpus_allowed); |
10e2f1ac | 5701 | |
c743f0a5 | 5702 | for_each_cpu_wrap(core, cpus, target) { |
10e2f1ac PZ |
5703 | bool idle = true; |
5704 | ||
5705 | for_each_cpu(cpu, cpu_smt_mask(core)) { | |
5706 | cpumask_clear_cpu(cpu, cpus); | |
5707 | if (!idle_cpu(cpu)) | |
5708 | idle = false; | |
5709 | } | |
5710 | ||
5711 | if (idle) | |
5712 | return core; | |
5713 | } | |
5714 | ||
5715 | /* | |
5716 | * Failed to find an idle core; stop looking for one. | |
5717 | */ | |
5718 | set_idle_cores(target, 0); | |
5719 | ||
5720 | return -1; | |
5721 | } | |
5722 | ||
5723 | /* | |
5724 | * Scan the local SMT mask for idle CPUs. | |
5725 | */ | |
5726 | static int select_idle_smt(struct task_struct *p, struct sched_domain *sd, int target) | |
5727 | { | |
5728 | int cpu; | |
5729 | ||
1b568f0a PZ |
5730 | if (!static_branch_likely(&sched_smt_present)) |
5731 | return -1; | |
5732 | ||
10e2f1ac | 5733 | for_each_cpu(cpu, cpu_smt_mask(target)) { |
0c98d344 | 5734 | if (!cpumask_test_cpu(cpu, &p->cpus_allowed)) |
10e2f1ac PZ |
5735 | continue; |
5736 | if (idle_cpu(cpu)) | |
5737 | return cpu; | |
5738 | } | |
5739 | ||
5740 | return -1; | |
5741 | } | |
5742 | ||
5743 | #else /* CONFIG_SCHED_SMT */ | |
5744 | ||
5745 | static inline int select_idle_core(struct task_struct *p, struct sched_domain *sd, int target) | |
5746 | { | |
5747 | return -1; | |
5748 | } | |
5749 | ||
5750 | static inline int select_idle_smt(struct task_struct *p, struct sched_domain *sd, int target) | |
5751 | { | |
5752 | return -1; | |
5753 | } | |
5754 | ||
5755 | #endif /* CONFIG_SCHED_SMT */ | |
5756 | ||
5757 | /* | |
5758 | * Scan the LLC domain for idle CPUs; this is dynamically regulated by | |
5759 | * comparing the average scan cost (tracked in sd->avg_scan_cost) against the | |
5760 | * average idle time for this rq (as found in rq->avg_idle). | |
a50bde51 | 5761 | */ |
10e2f1ac PZ |
5762 | static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, int target) |
5763 | { | |
9cfb38a7 | 5764 | struct sched_domain *this_sd; |
1ad3aaf3 | 5765 | u64 avg_cost, avg_idle; |
10e2f1ac PZ |
5766 | u64 time, cost; |
5767 | s64 delta; | |
1ad3aaf3 | 5768 | int cpu, nr = INT_MAX; |
10e2f1ac | 5769 | |
9cfb38a7 WL |
5770 | this_sd = rcu_dereference(*this_cpu_ptr(&sd_llc)); |
5771 | if (!this_sd) | |
5772 | return -1; | |
5773 | ||
10e2f1ac PZ |
5774 | /* |
5775 | * Due to large variance we need a large fuzz factor; hackbench in | |
5776 | * particularly is sensitive here. | |
5777 | */ | |
1ad3aaf3 PZ |
5778 | avg_idle = this_rq()->avg_idle / 512; |
5779 | avg_cost = this_sd->avg_scan_cost + 1; | |
5780 | ||
5781 | if (sched_feat(SIS_AVG_CPU) && avg_idle < avg_cost) | |
10e2f1ac PZ |
5782 | return -1; |
5783 | ||
1ad3aaf3 PZ |
5784 | if (sched_feat(SIS_PROP)) { |
5785 | u64 span_avg = sd->span_weight * avg_idle; | |
5786 | if (span_avg > 4*avg_cost) | |
5787 | nr = div_u64(span_avg, avg_cost); | |
5788 | else | |
5789 | nr = 4; | |
5790 | } | |
5791 | ||
10e2f1ac PZ |
5792 | time = local_clock(); |
5793 | ||
c743f0a5 | 5794 | for_each_cpu_wrap(cpu, sched_domain_span(sd), target) { |
1ad3aaf3 PZ |
5795 | if (!--nr) |
5796 | return -1; | |
0c98d344 | 5797 | if (!cpumask_test_cpu(cpu, &p->cpus_allowed)) |
10e2f1ac PZ |
5798 | continue; |
5799 | if (idle_cpu(cpu)) | |
5800 | break; | |
5801 | } | |
5802 | ||
5803 | time = local_clock() - time; | |
5804 | cost = this_sd->avg_scan_cost; | |
5805 | delta = (s64)(time - cost) / 8; | |
5806 | this_sd->avg_scan_cost += delta; | |
5807 | ||
5808 | return cpu; | |
5809 | } | |
5810 | ||
5811 | /* | |
5812 | * Try and locate an idle core/thread in the LLC cache domain. | |
a50bde51 | 5813 | */ |
772bd008 | 5814 | static int select_idle_sibling(struct task_struct *p, int prev, int target) |
a50bde51 | 5815 | { |
99bd5e2f | 5816 | struct sched_domain *sd; |
10e2f1ac | 5817 | int i; |
a50bde51 | 5818 | |
e0a79f52 MG |
5819 | if (idle_cpu(target)) |
5820 | return target; | |
99bd5e2f SS |
5821 | |
5822 | /* | |
10e2f1ac | 5823 | * If the previous cpu is cache affine and idle, don't be stupid. |
99bd5e2f | 5824 | */ |
772bd008 MR |
5825 | if (prev != target && cpus_share_cache(prev, target) && idle_cpu(prev)) |
5826 | return prev; | |
a50bde51 | 5827 | |
518cd623 | 5828 | sd = rcu_dereference(per_cpu(sd_llc, target)); |
10e2f1ac PZ |
5829 | if (!sd) |
5830 | return target; | |
772bd008 | 5831 | |
10e2f1ac PZ |
5832 | i = select_idle_core(p, sd, target); |
5833 | if ((unsigned)i < nr_cpumask_bits) | |
5834 | return i; | |
37407ea7 | 5835 | |
10e2f1ac PZ |
5836 | i = select_idle_cpu(p, sd, target); |
5837 | if ((unsigned)i < nr_cpumask_bits) | |
5838 | return i; | |
5839 | ||
5840 | i = select_idle_smt(p, sd, target); | |
5841 | if ((unsigned)i < nr_cpumask_bits) | |
5842 | return i; | |
970e1789 | 5843 | |
a50bde51 PZ |
5844 | return target; |
5845 | } | |
231678b7 | 5846 | |
8bb5b00c | 5847 | /* |
9e91d61d | 5848 | * cpu_util returns the amount of capacity of a CPU that is used by CFS |
8bb5b00c | 5849 | * tasks. The unit of the return value must be the one of capacity so we can |
9e91d61d DE |
5850 | * compare the utilization with the capacity of the CPU that is available for |
5851 | * CFS task (ie cpu_capacity). | |
231678b7 DE |
5852 | * |
5853 | * cfs_rq.avg.util_avg is the sum of running time of runnable tasks plus the | |
5854 | * recent utilization of currently non-runnable tasks on a CPU. It represents | |
5855 | * the amount of utilization of a CPU in the range [0..capacity_orig] where | |
5856 | * capacity_orig is the cpu_capacity available at the highest frequency | |
5857 | * (arch_scale_freq_capacity()). | |
5858 | * The utilization of a CPU converges towards a sum equal to or less than the | |
5859 | * current capacity (capacity_curr <= capacity_orig) of the CPU because it is | |
5860 | * the running time on this CPU scaled by capacity_curr. | |
5861 | * | |
5862 | * Nevertheless, cfs_rq.avg.util_avg can be higher than capacity_curr or even | |
5863 | * higher than capacity_orig because of unfortunate rounding in | |
5864 | * cfs.avg.util_avg or just after migrating tasks and new task wakeups until | |
5865 | * the average stabilizes with the new running time. We need to check that the | |
5866 | * utilization stays within the range of [0..capacity_orig] and cap it if | |
5867 | * necessary. Without utilization capping, a group could be seen as overloaded | |
5868 | * (CPU0 utilization at 121% + CPU1 utilization at 80%) whereas CPU1 has 20% of | |
5869 | * available capacity. We allow utilization to overshoot capacity_curr (but not | |
5870 | * capacity_orig) as it useful for predicting the capacity required after task | |
5871 | * migrations (scheduler-driven DVFS). | |
8bb5b00c | 5872 | */ |
9e91d61d | 5873 | static int cpu_util(int cpu) |
8bb5b00c | 5874 | { |
9e91d61d | 5875 | unsigned long util = cpu_rq(cpu)->cfs.avg.util_avg; |
8bb5b00c VG |
5876 | unsigned long capacity = capacity_orig_of(cpu); |
5877 | ||
231678b7 | 5878 | return (util >= capacity) ? capacity : util; |
8bb5b00c | 5879 | } |
a50bde51 | 5880 | |
3273163c MR |
5881 | static inline int task_util(struct task_struct *p) |
5882 | { | |
5883 | return p->se.avg.util_avg; | |
5884 | } | |
5885 | ||
104cb16d MR |
5886 | /* |
5887 | * cpu_util_wake: Compute cpu utilization with any contributions from | |
5888 | * the waking task p removed. | |
5889 | */ | |
5890 | static int cpu_util_wake(int cpu, struct task_struct *p) | |
5891 | { | |
5892 | unsigned long util, capacity; | |
5893 | ||
5894 | /* Task has no contribution or is new */ | |
5895 | if (cpu != task_cpu(p) || !p->se.avg.last_update_time) | |
5896 | return cpu_util(cpu); | |
5897 | ||
5898 | capacity = capacity_orig_of(cpu); | |
5899 | util = max_t(long, cpu_rq(cpu)->cfs.avg.util_avg - task_util(p), 0); | |
5900 | ||
5901 | return (util >= capacity) ? capacity : util; | |
5902 | } | |
5903 | ||
3273163c MR |
5904 | /* |
5905 | * Disable WAKE_AFFINE in the case where task @p doesn't fit in the | |
5906 | * capacity of either the waking CPU @cpu or the previous CPU @prev_cpu. | |
5907 | * | |
5908 | * In that case WAKE_AFFINE doesn't make sense and we'll let | |
5909 | * BALANCE_WAKE sort things out. | |
5910 | */ | |
5911 | static int wake_cap(struct task_struct *p, int cpu, int prev_cpu) | |
5912 | { | |
5913 | long min_cap, max_cap; | |
5914 | ||
5915 | min_cap = min(capacity_orig_of(prev_cpu), capacity_orig_of(cpu)); | |
5916 | max_cap = cpu_rq(cpu)->rd->max_cpu_capacity; | |
5917 | ||
5918 | /* Minimum capacity is close to max, no need to abort wake_affine */ | |
5919 | if (max_cap - min_cap < max_cap >> 3) | |
5920 | return 0; | |
5921 | ||
104cb16d MR |
5922 | /* Bring task utilization in sync with prev_cpu */ |
5923 | sync_entity_load_avg(&p->se); | |
5924 | ||
3273163c MR |
5925 | return min_cap * 1024 < task_util(p) * capacity_margin; |
5926 | } | |
5927 | ||
aaee1203 | 5928 | /* |
de91b9cb MR |
5929 | * select_task_rq_fair: Select target runqueue for the waking task in domains |
5930 | * that have the 'sd_flag' flag set. In practice, this is SD_BALANCE_WAKE, | |
5931 | * SD_BALANCE_FORK, or SD_BALANCE_EXEC. | |
aaee1203 | 5932 | * |
de91b9cb MR |
5933 | * Balances load by selecting the idlest cpu in the idlest group, or under |
5934 | * certain conditions an idle sibling cpu if the domain has SD_WAKE_AFFINE set. | |
aaee1203 | 5935 | * |
de91b9cb | 5936 | * Returns the target cpu number. |
aaee1203 PZ |
5937 | * |
5938 | * preempt must be disabled. | |
5939 | */ | |
0017d735 | 5940 | static int |
ac66f547 | 5941 | select_task_rq_fair(struct task_struct *p, int prev_cpu, int sd_flag, int wake_flags) |
aaee1203 | 5942 | { |
29cd8bae | 5943 | struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL; |
c88d5910 | 5944 | int cpu = smp_processor_id(); |
63b0e9ed | 5945 | int new_cpu = prev_cpu; |
99bd5e2f | 5946 | int want_affine = 0; |
5158f4e4 | 5947 | int sync = wake_flags & WF_SYNC; |
c88d5910 | 5948 | |
c58d25f3 PZ |
5949 | if (sd_flag & SD_BALANCE_WAKE) { |
5950 | record_wakee(p); | |
3273163c | 5951 | want_affine = !wake_wide(p) && !wake_cap(p, cpu, prev_cpu) |
0c98d344 | 5952 | && cpumask_test_cpu(cpu, &p->cpus_allowed); |
c58d25f3 | 5953 | } |
aaee1203 | 5954 | |
dce840a0 | 5955 | rcu_read_lock(); |
aaee1203 | 5956 | for_each_domain(cpu, tmp) { |
e4f42888 | 5957 | if (!(tmp->flags & SD_LOAD_BALANCE)) |
63b0e9ed | 5958 | break; |
e4f42888 | 5959 | |
fe3bcfe1 | 5960 | /* |
99bd5e2f SS |
5961 | * If both cpu and prev_cpu are part of this domain, |
5962 | * cpu is a valid SD_WAKE_AFFINE target. | |
fe3bcfe1 | 5963 | */ |
99bd5e2f SS |
5964 | if (want_affine && (tmp->flags & SD_WAKE_AFFINE) && |
5965 | cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) { | |
5966 | affine_sd = tmp; | |
29cd8bae | 5967 | break; |
f03542a7 | 5968 | } |
29cd8bae | 5969 | |
f03542a7 | 5970 | if (tmp->flags & sd_flag) |
29cd8bae | 5971 | sd = tmp; |
63b0e9ed MG |
5972 | else if (!want_affine) |
5973 | break; | |
29cd8bae PZ |
5974 | } |
5975 | ||
63b0e9ed MG |
5976 | if (affine_sd) { |
5977 | sd = NULL; /* Prefer wake_affine over balance flags */ | |
7d894e6e RR |
5978 | if (cpu == prev_cpu) |
5979 | goto pick_cpu; | |
5980 | ||
5981 | if (wake_affine(affine_sd, p, prev_cpu, sync)) | |
63b0e9ed | 5982 | new_cpu = cpu; |
8b911acd | 5983 | } |
e7693a36 | 5984 | |
63b0e9ed | 5985 | if (!sd) { |
7d894e6e | 5986 | pick_cpu: |
63b0e9ed | 5987 | if (sd_flag & SD_BALANCE_WAKE) /* XXX always ? */ |
772bd008 | 5988 | new_cpu = select_idle_sibling(p, prev_cpu, new_cpu); |
63b0e9ed MG |
5989 | |
5990 | } else while (sd) { | |
aaee1203 | 5991 | struct sched_group *group; |
c88d5910 | 5992 | int weight; |
098fb9db | 5993 | |
0763a660 | 5994 | if (!(sd->flags & sd_flag)) { |
aaee1203 PZ |
5995 | sd = sd->child; |
5996 | continue; | |
5997 | } | |
098fb9db | 5998 | |
c44f2a02 | 5999 | group = find_idlest_group(sd, p, cpu, sd_flag); |
aaee1203 PZ |
6000 | if (!group) { |
6001 | sd = sd->child; | |
6002 | continue; | |
6003 | } | |
4ae7d5ce | 6004 | |
d7c33c49 | 6005 | new_cpu = find_idlest_cpu(group, p, cpu); |
aaee1203 PZ |
6006 | if (new_cpu == -1 || new_cpu == cpu) { |
6007 | /* Now try balancing at a lower domain level of cpu */ | |
6008 | sd = sd->child; | |
6009 | continue; | |
e7693a36 | 6010 | } |
aaee1203 PZ |
6011 | |
6012 | /* Now try balancing at a lower domain level of new_cpu */ | |
6013 | cpu = new_cpu; | |
669c55e9 | 6014 | weight = sd->span_weight; |
aaee1203 PZ |
6015 | sd = NULL; |
6016 | for_each_domain(cpu, tmp) { | |
669c55e9 | 6017 | if (weight <= tmp->span_weight) |
aaee1203 | 6018 | break; |
0763a660 | 6019 | if (tmp->flags & sd_flag) |
aaee1203 PZ |
6020 | sd = tmp; |
6021 | } | |
6022 | /* while loop will break here if sd == NULL */ | |
e7693a36 | 6023 | } |
dce840a0 | 6024 | rcu_read_unlock(); |
e7693a36 | 6025 | |
c88d5910 | 6026 | return new_cpu; |
e7693a36 | 6027 | } |
0a74bef8 PT |
6028 | |
6029 | /* | |
6030 | * Called immediately before a task is migrated to a new cpu; task_cpu(p) and | |
6031 | * cfs_rq_of(p) references at time of call are still valid and identify the | |
525628c7 | 6032 | * previous cpu. The caller guarantees p->pi_lock or task_rq(p)->lock is held. |
0a74bef8 | 6033 | */ |
5a4fd036 | 6034 | static void migrate_task_rq_fair(struct task_struct *p) |
0a74bef8 | 6035 | { |
59efa0ba PZ |
6036 | /* |
6037 | * As blocked tasks retain absolute vruntime the migration needs to | |
6038 | * deal with this by subtracting the old and adding the new | |
6039 | * min_vruntime -- the latter is done by enqueue_entity() when placing | |
6040 | * the task on the new runqueue. | |
6041 | */ | |
6042 | if (p->state == TASK_WAKING) { | |
6043 | struct sched_entity *se = &p->se; | |
6044 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
6045 | u64 min_vruntime; | |
6046 | ||
6047 | #ifndef CONFIG_64BIT | |
6048 | u64 min_vruntime_copy; | |
6049 | ||
6050 | do { | |
6051 | min_vruntime_copy = cfs_rq->min_vruntime_copy; | |
6052 | smp_rmb(); | |
6053 | min_vruntime = cfs_rq->min_vruntime; | |
6054 | } while (min_vruntime != min_vruntime_copy); | |
6055 | #else | |
6056 | min_vruntime = cfs_rq->min_vruntime; | |
6057 | #endif | |
6058 | ||
6059 | se->vruntime -= min_vruntime; | |
6060 | } | |
6061 | ||
aff3e498 | 6062 | /* |
9d89c257 YD |
6063 | * We are supposed to update the task to "current" time, then its up to date |
6064 | * and ready to go to new CPU/cfs_rq. But we have difficulty in getting | |
6065 | * what current time is, so simply throw away the out-of-date time. This | |
6066 | * will result in the wakee task is less decayed, but giving the wakee more | |
6067 | * load sounds not bad. | |
aff3e498 | 6068 | */ |
9d89c257 YD |
6069 | remove_entity_load_avg(&p->se); |
6070 | ||
6071 | /* Tell new CPU we are migrated */ | |
6072 | p->se.avg.last_update_time = 0; | |
3944a927 BS |
6073 | |
6074 | /* We have migrated, no longer consider this task hot */ | |
9d89c257 | 6075 | p->se.exec_start = 0; |
0a74bef8 | 6076 | } |
12695578 YD |
6077 | |
6078 | static void task_dead_fair(struct task_struct *p) | |
6079 | { | |
6080 | remove_entity_load_avg(&p->se); | |
6081 | } | |
e7693a36 GH |
6082 | #endif /* CONFIG_SMP */ |
6083 | ||
e52fb7c0 PZ |
6084 | static unsigned long |
6085 | wakeup_gran(struct sched_entity *curr, struct sched_entity *se) | |
0bbd3336 PZ |
6086 | { |
6087 | unsigned long gran = sysctl_sched_wakeup_granularity; | |
6088 | ||
6089 | /* | |
e52fb7c0 PZ |
6090 | * Since its curr running now, convert the gran from real-time |
6091 | * to virtual-time in his units. | |
13814d42 MG |
6092 | * |
6093 | * By using 'se' instead of 'curr' we penalize light tasks, so | |
6094 | * they get preempted easier. That is, if 'se' < 'curr' then | |
6095 | * the resulting gran will be larger, therefore penalizing the | |
6096 | * lighter, if otoh 'se' > 'curr' then the resulting gran will | |
6097 | * be smaller, again penalizing the lighter task. | |
6098 | * | |
6099 | * This is especially important for buddies when the leftmost | |
6100 | * task is higher priority than the buddy. | |
0bbd3336 | 6101 | */ |
f4ad9bd2 | 6102 | return calc_delta_fair(gran, se); |
0bbd3336 PZ |
6103 | } |
6104 | ||
464b7527 PZ |
6105 | /* |
6106 | * Should 'se' preempt 'curr'. | |
6107 | * | |
6108 | * |s1 | |
6109 | * |s2 | |
6110 | * |s3 | |
6111 | * g | |
6112 | * |<--->|c | |
6113 | * | |
6114 | * w(c, s1) = -1 | |
6115 | * w(c, s2) = 0 | |
6116 | * w(c, s3) = 1 | |
6117 | * | |
6118 | */ | |
6119 | static int | |
6120 | wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se) | |
6121 | { | |
6122 | s64 gran, vdiff = curr->vruntime - se->vruntime; | |
6123 | ||
6124 | if (vdiff <= 0) | |
6125 | return -1; | |
6126 | ||
e52fb7c0 | 6127 | gran = wakeup_gran(curr, se); |
464b7527 PZ |
6128 | if (vdiff > gran) |
6129 | return 1; | |
6130 | ||
6131 | return 0; | |
6132 | } | |
6133 | ||
02479099 PZ |
6134 | static void set_last_buddy(struct sched_entity *se) |
6135 | { | |
69c80f3e VP |
6136 | if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE)) |
6137 | return; | |
6138 | ||
c5ae366e DA |
6139 | for_each_sched_entity(se) { |
6140 | if (SCHED_WARN_ON(!se->on_rq)) | |
6141 | return; | |
69c80f3e | 6142 | cfs_rq_of(se)->last = se; |
c5ae366e | 6143 | } |
02479099 PZ |
6144 | } |
6145 | ||
6146 | static void set_next_buddy(struct sched_entity *se) | |
6147 | { | |
69c80f3e VP |
6148 | if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE)) |
6149 | return; | |
6150 | ||
c5ae366e DA |
6151 | for_each_sched_entity(se) { |
6152 | if (SCHED_WARN_ON(!se->on_rq)) | |
6153 | return; | |
69c80f3e | 6154 | cfs_rq_of(se)->next = se; |
c5ae366e | 6155 | } |
02479099 PZ |
6156 | } |
6157 | ||
ac53db59 RR |
6158 | static void set_skip_buddy(struct sched_entity *se) |
6159 | { | |
69c80f3e VP |
6160 | for_each_sched_entity(se) |
6161 | cfs_rq_of(se)->skip = se; | |
ac53db59 RR |
6162 | } |
6163 | ||
bf0f6f24 IM |
6164 | /* |
6165 | * Preempt the current task with a newly woken task if needed: | |
6166 | */ | |
5a9b86f6 | 6167 | static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags) |
bf0f6f24 IM |
6168 | { |
6169 | struct task_struct *curr = rq->curr; | |
8651a86c | 6170 | struct sched_entity *se = &curr->se, *pse = &p->se; |
03e89e45 | 6171 | struct cfs_rq *cfs_rq = task_cfs_rq(curr); |
f685ceac | 6172 | int scale = cfs_rq->nr_running >= sched_nr_latency; |
2f36825b | 6173 | int next_buddy_marked = 0; |
bf0f6f24 | 6174 | |
4ae7d5ce IM |
6175 | if (unlikely(se == pse)) |
6176 | return; | |
6177 | ||
5238cdd3 | 6178 | /* |
163122b7 | 6179 | * This is possible from callers such as attach_tasks(), in which we |
5238cdd3 PT |
6180 | * unconditionally check_prempt_curr() after an enqueue (which may have |
6181 | * lead to a throttle). This both saves work and prevents false | |
6182 | * next-buddy nomination below. | |
6183 | */ | |
6184 | if (unlikely(throttled_hierarchy(cfs_rq_of(pse)))) | |
6185 | return; | |
6186 | ||
2f36825b | 6187 | if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) { |
3cb63d52 | 6188 | set_next_buddy(pse); |
2f36825b VP |
6189 | next_buddy_marked = 1; |
6190 | } | |
57fdc26d | 6191 | |
aec0a514 BR |
6192 | /* |
6193 | * We can come here with TIF_NEED_RESCHED already set from new task | |
6194 | * wake up path. | |
5238cdd3 PT |
6195 | * |
6196 | * Note: this also catches the edge-case of curr being in a throttled | |
6197 | * group (e.g. via set_curr_task), since update_curr() (in the | |
6198 | * enqueue of curr) will have resulted in resched being set. This | |
6199 | * prevents us from potentially nominating it as a false LAST_BUDDY | |
6200 | * below. | |
aec0a514 BR |
6201 | */ |
6202 | if (test_tsk_need_resched(curr)) | |
6203 | return; | |
6204 | ||
a2f5c9ab DH |
6205 | /* Idle tasks are by definition preempted by non-idle tasks. */ |
6206 | if (unlikely(curr->policy == SCHED_IDLE) && | |
6207 | likely(p->policy != SCHED_IDLE)) | |
6208 | goto preempt; | |
6209 | ||
91c234b4 | 6210 | /* |
a2f5c9ab DH |
6211 | * Batch and idle tasks do not preempt non-idle tasks (their preemption |
6212 | * is driven by the tick): | |
91c234b4 | 6213 | */ |
8ed92e51 | 6214 | if (unlikely(p->policy != SCHED_NORMAL) || !sched_feat(WAKEUP_PREEMPTION)) |
91c234b4 | 6215 | return; |
bf0f6f24 | 6216 | |
464b7527 | 6217 | find_matching_se(&se, &pse); |
9bbd7374 | 6218 | update_curr(cfs_rq_of(se)); |
002f128b | 6219 | BUG_ON(!pse); |
2f36825b VP |
6220 | if (wakeup_preempt_entity(se, pse) == 1) { |
6221 | /* | |
6222 | * Bias pick_next to pick the sched entity that is | |
6223 | * triggering this preemption. | |
6224 | */ | |
6225 | if (!next_buddy_marked) | |
6226 | set_next_buddy(pse); | |
3a7e73a2 | 6227 | goto preempt; |
2f36825b | 6228 | } |
464b7527 | 6229 | |
3a7e73a2 | 6230 | return; |
a65ac745 | 6231 | |
3a7e73a2 | 6232 | preempt: |
8875125e | 6233 | resched_curr(rq); |
3a7e73a2 PZ |
6234 | /* |
6235 | * Only set the backward buddy when the current task is still | |
6236 | * on the rq. This can happen when a wakeup gets interleaved | |
6237 | * with schedule on the ->pre_schedule() or idle_balance() | |
6238 | * point, either of which can * drop the rq lock. | |
6239 | * | |
6240 | * Also, during early boot the idle thread is in the fair class, | |
6241 | * for obvious reasons its a bad idea to schedule back to it. | |
6242 | */ | |
6243 | if (unlikely(!se->on_rq || curr == rq->idle)) | |
6244 | return; | |
6245 | ||
6246 | if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se)) | |
6247 | set_last_buddy(se); | |
bf0f6f24 IM |
6248 | } |
6249 | ||
606dba2e | 6250 | static struct task_struct * |
d8ac8971 | 6251 | pick_next_task_fair(struct rq *rq, struct task_struct *prev, struct rq_flags *rf) |
bf0f6f24 IM |
6252 | { |
6253 | struct cfs_rq *cfs_rq = &rq->cfs; | |
6254 | struct sched_entity *se; | |
678d5718 | 6255 | struct task_struct *p; |
37e117c0 | 6256 | int new_tasks; |
678d5718 | 6257 | |
6e83125c | 6258 | again: |
678d5718 | 6259 | if (!cfs_rq->nr_running) |
38033c37 | 6260 | goto idle; |
678d5718 | 6261 | |
9674f5ca | 6262 | #ifdef CONFIG_FAIR_GROUP_SCHED |
3f1d2a31 | 6263 | if (prev->sched_class != &fair_sched_class) |
678d5718 PZ |
6264 | goto simple; |
6265 | ||
6266 | /* | |
6267 | * Because of the set_next_buddy() in dequeue_task_fair() it is rather | |
6268 | * likely that a next task is from the same cgroup as the current. | |
6269 | * | |
6270 | * Therefore attempt to avoid putting and setting the entire cgroup | |
6271 | * hierarchy, only change the part that actually changes. | |
6272 | */ | |
6273 | ||
6274 | do { | |
6275 | struct sched_entity *curr = cfs_rq->curr; | |
6276 | ||
6277 | /* | |
6278 | * Since we got here without doing put_prev_entity() we also | |
6279 | * have to consider cfs_rq->curr. If it is still a runnable | |
6280 | * entity, update_curr() will update its vruntime, otherwise | |
6281 | * forget we've ever seen it. | |
6282 | */ | |
54d27365 BS |
6283 | if (curr) { |
6284 | if (curr->on_rq) | |
6285 | update_curr(cfs_rq); | |
6286 | else | |
6287 | curr = NULL; | |
678d5718 | 6288 | |
54d27365 BS |
6289 | /* |
6290 | * This call to check_cfs_rq_runtime() will do the | |
6291 | * throttle and dequeue its entity in the parent(s). | |
9674f5ca | 6292 | * Therefore the nr_running test will indeed |
54d27365 BS |
6293 | * be correct. |
6294 | */ | |
9674f5ca VK |
6295 | if (unlikely(check_cfs_rq_runtime(cfs_rq))) { |
6296 | cfs_rq = &rq->cfs; | |
6297 | ||
6298 | if (!cfs_rq->nr_running) | |
6299 | goto idle; | |
6300 | ||
54d27365 | 6301 | goto simple; |
9674f5ca | 6302 | } |
54d27365 | 6303 | } |
678d5718 PZ |
6304 | |
6305 | se = pick_next_entity(cfs_rq, curr); | |
6306 | cfs_rq = group_cfs_rq(se); | |
6307 | } while (cfs_rq); | |
6308 | ||
6309 | p = task_of(se); | |
6310 | ||
6311 | /* | |
6312 | * Since we haven't yet done put_prev_entity and if the selected task | |
6313 | * is a different task than we started out with, try and touch the | |
6314 | * least amount of cfs_rqs. | |
6315 | */ | |
6316 | if (prev != p) { | |
6317 | struct sched_entity *pse = &prev->se; | |
6318 | ||
6319 | while (!(cfs_rq = is_same_group(se, pse))) { | |
6320 | int se_depth = se->depth; | |
6321 | int pse_depth = pse->depth; | |
6322 | ||
6323 | if (se_depth <= pse_depth) { | |
6324 | put_prev_entity(cfs_rq_of(pse), pse); | |
6325 | pse = parent_entity(pse); | |
6326 | } | |
6327 | if (se_depth >= pse_depth) { | |
6328 | set_next_entity(cfs_rq_of(se), se); | |
6329 | se = parent_entity(se); | |
6330 | } | |
6331 | } | |
6332 | ||
6333 | put_prev_entity(cfs_rq, pse); | |
6334 | set_next_entity(cfs_rq, se); | |
6335 | } | |
6336 | ||
6337 | if (hrtick_enabled(rq)) | |
6338 | hrtick_start_fair(rq, p); | |
6339 | ||
6340 | return p; | |
6341 | simple: | |
678d5718 | 6342 | #endif |
bf0f6f24 | 6343 | |
3f1d2a31 | 6344 | put_prev_task(rq, prev); |
606dba2e | 6345 | |
bf0f6f24 | 6346 | do { |
678d5718 | 6347 | se = pick_next_entity(cfs_rq, NULL); |
f4b6755f | 6348 | set_next_entity(cfs_rq, se); |
bf0f6f24 IM |
6349 | cfs_rq = group_cfs_rq(se); |
6350 | } while (cfs_rq); | |
6351 | ||
8f4d37ec | 6352 | p = task_of(se); |
678d5718 | 6353 | |
b39e66ea MG |
6354 | if (hrtick_enabled(rq)) |
6355 | hrtick_start_fair(rq, p); | |
8f4d37ec PZ |
6356 | |
6357 | return p; | |
38033c37 PZ |
6358 | |
6359 | idle: | |
46f69fa3 MF |
6360 | new_tasks = idle_balance(rq, rf); |
6361 | ||
37e117c0 PZ |
6362 | /* |
6363 | * Because idle_balance() releases (and re-acquires) rq->lock, it is | |
6364 | * possible for any higher priority task to appear. In that case we | |
6365 | * must re-start the pick_next_entity() loop. | |
6366 | */ | |
e4aa358b | 6367 | if (new_tasks < 0) |
37e117c0 PZ |
6368 | return RETRY_TASK; |
6369 | ||
e4aa358b | 6370 | if (new_tasks > 0) |
38033c37 | 6371 | goto again; |
38033c37 PZ |
6372 | |
6373 | return NULL; | |
bf0f6f24 IM |
6374 | } |
6375 | ||
6376 | /* | |
6377 | * Account for a descheduled task: | |
6378 | */ | |
31ee529c | 6379 | static void put_prev_task_fair(struct rq *rq, struct task_struct *prev) |
bf0f6f24 IM |
6380 | { |
6381 | struct sched_entity *se = &prev->se; | |
6382 | struct cfs_rq *cfs_rq; | |
6383 | ||
6384 | for_each_sched_entity(se) { | |
6385 | cfs_rq = cfs_rq_of(se); | |
ab6cde26 | 6386 | put_prev_entity(cfs_rq, se); |
bf0f6f24 IM |
6387 | } |
6388 | } | |
6389 | ||
ac53db59 RR |
6390 | /* |
6391 | * sched_yield() is very simple | |
6392 | * | |
6393 | * The magic of dealing with the ->skip buddy is in pick_next_entity. | |
6394 | */ | |
6395 | static void yield_task_fair(struct rq *rq) | |
6396 | { | |
6397 | struct task_struct *curr = rq->curr; | |
6398 | struct cfs_rq *cfs_rq = task_cfs_rq(curr); | |
6399 | struct sched_entity *se = &curr->se; | |
6400 | ||
6401 | /* | |
6402 | * Are we the only task in the tree? | |
6403 | */ | |
6404 | if (unlikely(rq->nr_running == 1)) | |
6405 | return; | |
6406 | ||
6407 | clear_buddies(cfs_rq, se); | |
6408 | ||
6409 | if (curr->policy != SCHED_BATCH) { | |
6410 | update_rq_clock(rq); | |
6411 | /* | |
6412 | * Update run-time statistics of the 'current'. | |
6413 | */ | |
6414 | update_curr(cfs_rq); | |
916671c0 MG |
6415 | /* |
6416 | * Tell update_rq_clock() that we've just updated, | |
6417 | * so we don't do microscopic update in schedule() | |
6418 | * and double the fastpath cost. | |
6419 | */ | |
9edfbfed | 6420 | rq_clock_skip_update(rq, true); |
ac53db59 RR |
6421 | } |
6422 | ||
6423 | set_skip_buddy(se); | |
6424 | } | |
6425 | ||
d95f4122 MG |
6426 | static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt) |
6427 | { | |
6428 | struct sched_entity *se = &p->se; | |
6429 | ||
5238cdd3 PT |
6430 | /* throttled hierarchies are not runnable */ |
6431 | if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se))) | |
d95f4122 MG |
6432 | return false; |
6433 | ||
6434 | /* Tell the scheduler that we'd really like pse to run next. */ | |
6435 | set_next_buddy(se); | |
6436 | ||
d95f4122 MG |
6437 | yield_task_fair(rq); |
6438 | ||
6439 | return true; | |
6440 | } | |
6441 | ||
681f3e68 | 6442 | #ifdef CONFIG_SMP |
bf0f6f24 | 6443 | /************************************************** |
e9c84cb8 PZ |
6444 | * Fair scheduling class load-balancing methods. |
6445 | * | |
6446 | * BASICS | |
6447 | * | |
6448 | * The purpose of load-balancing is to achieve the same basic fairness the | |
6449 | * per-cpu scheduler provides, namely provide a proportional amount of compute | |
6450 | * time to each task. This is expressed in the following equation: | |
6451 | * | |
6452 | * W_i,n/P_i == W_j,n/P_j for all i,j (1) | |
6453 | * | |
6454 | * Where W_i,n is the n-th weight average for cpu i. The instantaneous weight | |
6455 | * W_i,0 is defined as: | |
6456 | * | |
6457 | * W_i,0 = \Sum_j w_i,j (2) | |
6458 | * | |
6459 | * Where w_i,j is the weight of the j-th runnable task on cpu i. This weight | |
1c3de5e1 | 6460 | * is derived from the nice value as per sched_prio_to_weight[]. |
e9c84cb8 PZ |
6461 | * |
6462 | * The weight average is an exponential decay average of the instantaneous | |
6463 | * weight: | |
6464 | * | |
6465 | * W'_i,n = (2^n - 1) / 2^n * W_i,n + 1 / 2^n * W_i,0 (3) | |
6466 | * | |
ced549fa | 6467 | * C_i is the compute capacity of cpu i, typically it is the |
e9c84cb8 PZ |
6468 | * fraction of 'recent' time available for SCHED_OTHER task execution. But it |
6469 | * can also include other factors [XXX]. | |
6470 | * | |
6471 | * To achieve this balance we define a measure of imbalance which follows | |
6472 | * directly from (1): | |
6473 | * | |
ced549fa | 6474 | * imb_i,j = max{ avg(W/C), W_i/C_i } - min{ avg(W/C), W_j/C_j } (4) |
e9c84cb8 PZ |
6475 | * |
6476 | * We them move tasks around to minimize the imbalance. In the continuous | |
6477 | * function space it is obvious this converges, in the discrete case we get | |
6478 | * a few fun cases generally called infeasible weight scenarios. | |
6479 | * | |
6480 | * [XXX expand on: | |
6481 | * - infeasible weights; | |
6482 | * - local vs global optima in the discrete case. ] | |
6483 | * | |
6484 | * | |
6485 | * SCHED DOMAINS | |
6486 | * | |
6487 | * In order to solve the imbalance equation (4), and avoid the obvious O(n^2) | |
6488 | * for all i,j solution, we create a tree of cpus that follows the hardware | |
6489 | * topology where each level pairs two lower groups (or better). This results | |
6490 | * in O(log n) layers. Furthermore we reduce the number of cpus going up the | |
6491 | * tree to only the first of the previous level and we decrease the frequency | |
6492 | * of load-balance at each level inv. proportional to the number of cpus in | |
6493 | * the groups. | |
6494 | * | |
6495 | * This yields: | |
6496 | * | |
6497 | * log_2 n 1 n | |
6498 | * \Sum { --- * --- * 2^i } = O(n) (5) | |
6499 | * i = 0 2^i 2^i | |
6500 | * `- size of each group | |
6501 | * | | `- number of cpus doing load-balance | |
6502 | * | `- freq | |
6503 | * `- sum over all levels | |
6504 | * | |
6505 | * Coupled with a limit on how many tasks we can migrate every balance pass, | |
6506 | * this makes (5) the runtime complexity of the balancer. | |
6507 | * | |
6508 | * An important property here is that each CPU is still (indirectly) connected | |
6509 | * to every other cpu in at most O(log n) steps: | |
6510 | * | |
6511 | * The adjacency matrix of the resulting graph is given by: | |
6512 | * | |
97a7142f | 6513 | * log_2 n |
e9c84cb8 PZ |
6514 | * A_i,j = \Union (i % 2^k == 0) && i / 2^(k+1) == j / 2^(k+1) (6) |
6515 | * k = 0 | |
6516 | * | |
6517 | * And you'll find that: | |
6518 | * | |
6519 | * A^(log_2 n)_i,j != 0 for all i,j (7) | |
6520 | * | |
6521 | * Showing there's indeed a path between every cpu in at most O(log n) steps. | |
6522 | * The task movement gives a factor of O(m), giving a convergence complexity | |
6523 | * of: | |
6524 | * | |
6525 | * O(nm log n), n := nr_cpus, m := nr_tasks (8) | |
6526 | * | |
6527 | * | |
6528 | * WORK CONSERVING | |
6529 | * | |
6530 | * In order to avoid CPUs going idle while there's still work to do, new idle | |
6531 | * balancing is more aggressive and has the newly idle cpu iterate up the domain | |
6532 | * tree itself instead of relying on other CPUs to bring it work. | |
6533 | * | |
6534 | * This adds some complexity to both (5) and (8) but it reduces the total idle | |
6535 | * time. | |
6536 | * | |
6537 | * [XXX more?] | |
6538 | * | |
6539 | * | |
6540 | * CGROUPS | |
6541 | * | |
6542 | * Cgroups make a horror show out of (2), instead of a simple sum we get: | |
6543 | * | |
6544 | * s_k,i | |
6545 | * W_i,0 = \Sum_j \Prod_k w_k * ----- (9) | |
6546 | * S_k | |
6547 | * | |
6548 | * Where | |
6549 | * | |
6550 | * s_k,i = \Sum_j w_i,j,k and S_k = \Sum_i s_k,i (10) | |
6551 | * | |
6552 | * w_i,j,k is the weight of the j-th runnable task in the k-th cgroup on cpu i. | |
6553 | * | |
6554 | * The big problem is S_k, its a global sum needed to compute a local (W_i) | |
6555 | * property. | |
6556 | * | |
6557 | * [XXX write more on how we solve this.. _after_ merging pjt's patches that | |
6558 | * rewrite all of this once again.] | |
97a7142f | 6559 | */ |
bf0f6f24 | 6560 | |
ed387b78 HS |
6561 | static unsigned long __read_mostly max_load_balance_interval = HZ/10; |
6562 | ||
0ec8aa00 PZ |
6563 | enum fbq_type { regular, remote, all }; |
6564 | ||
ddcdf6e7 | 6565 | #define LBF_ALL_PINNED 0x01 |
367456c7 | 6566 | #define LBF_NEED_BREAK 0x02 |
6263322c PZ |
6567 | #define LBF_DST_PINNED 0x04 |
6568 | #define LBF_SOME_PINNED 0x08 | |
ddcdf6e7 PZ |
6569 | |
6570 | struct lb_env { | |
6571 | struct sched_domain *sd; | |
6572 | ||
ddcdf6e7 | 6573 | struct rq *src_rq; |
85c1e7da | 6574 | int src_cpu; |
ddcdf6e7 PZ |
6575 | |
6576 | int dst_cpu; | |
6577 | struct rq *dst_rq; | |
6578 | ||
88b8dac0 SV |
6579 | struct cpumask *dst_grpmask; |
6580 | int new_dst_cpu; | |
ddcdf6e7 | 6581 | enum cpu_idle_type idle; |
bd939f45 | 6582 | long imbalance; |
b9403130 MW |
6583 | /* The set of CPUs under consideration for load-balancing */ |
6584 | struct cpumask *cpus; | |
6585 | ||
ddcdf6e7 | 6586 | unsigned int flags; |
367456c7 PZ |
6587 | |
6588 | unsigned int loop; | |
6589 | unsigned int loop_break; | |
6590 | unsigned int loop_max; | |
0ec8aa00 PZ |
6591 | |
6592 | enum fbq_type fbq_type; | |
163122b7 | 6593 | struct list_head tasks; |
ddcdf6e7 PZ |
6594 | }; |
6595 | ||
029632fb PZ |
6596 | /* |
6597 | * Is this task likely cache-hot: | |
6598 | */ | |
5d5e2b1b | 6599 | static int task_hot(struct task_struct *p, struct lb_env *env) |
029632fb PZ |
6600 | { |
6601 | s64 delta; | |
6602 | ||
e5673f28 KT |
6603 | lockdep_assert_held(&env->src_rq->lock); |
6604 | ||
029632fb PZ |
6605 | if (p->sched_class != &fair_sched_class) |
6606 | return 0; | |
6607 | ||
6608 | if (unlikely(p->policy == SCHED_IDLE)) | |
6609 | return 0; | |
6610 | ||
6611 | /* | |
6612 | * Buddy candidates are cache hot: | |
6613 | */ | |
5d5e2b1b | 6614 | if (sched_feat(CACHE_HOT_BUDDY) && env->dst_rq->nr_running && |
029632fb PZ |
6615 | (&p->se == cfs_rq_of(&p->se)->next || |
6616 | &p->se == cfs_rq_of(&p->se)->last)) | |
6617 | return 1; | |
6618 | ||
6619 | if (sysctl_sched_migration_cost == -1) | |
6620 | return 1; | |
6621 | if (sysctl_sched_migration_cost == 0) | |
6622 | return 0; | |
6623 | ||
5d5e2b1b | 6624 | delta = rq_clock_task(env->src_rq) - p->se.exec_start; |
029632fb PZ |
6625 | |
6626 | return delta < (s64)sysctl_sched_migration_cost; | |
6627 | } | |
6628 | ||
3a7053b3 | 6629 | #ifdef CONFIG_NUMA_BALANCING |
c1ceac62 | 6630 | /* |
2a1ed24c SD |
6631 | * Returns 1, if task migration degrades locality |
6632 | * Returns 0, if task migration improves locality i.e migration preferred. | |
6633 | * Returns -1, if task migration is not affected by locality. | |
c1ceac62 | 6634 | */ |
2a1ed24c | 6635 | static int migrate_degrades_locality(struct task_struct *p, struct lb_env *env) |
3a7053b3 | 6636 | { |
b1ad065e | 6637 | struct numa_group *numa_group = rcu_dereference(p->numa_group); |
c1ceac62 | 6638 | unsigned long src_faults, dst_faults; |
3a7053b3 MG |
6639 | int src_nid, dst_nid; |
6640 | ||
2a595721 | 6641 | if (!static_branch_likely(&sched_numa_balancing)) |
2a1ed24c SD |
6642 | return -1; |
6643 | ||
c3b9bc5b | 6644 | if (!p->numa_faults || !(env->sd->flags & SD_NUMA)) |
2a1ed24c | 6645 | return -1; |
7a0f3083 MG |
6646 | |
6647 | src_nid = cpu_to_node(env->src_cpu); | |
6648 | dst_nid = cpu_to_node(env->dst_cpu); | |
6649 | ||
83e1d2cd | 6650 | if (src_nid == dst_nid) |
2a1ed24c | 6651 | return -1; |
7a0f3083 | 6652 | |
2a1ed24c SD |
6653 | /* Migrating away from the preferred node is always bad. */ |
6654 | if (src_nid == p->numa_preferred_nid) { | |
6655 | if (env->src_rq->nr_running > env->src_rq->nr_preferred_running) | |
6656 | return 1; | |
6657 | else | |
6658 | return -1; | |
6659 | } | |
b1ad065e | 6660 | |
c1ceac62 RR |
6661 | /* Encourage migration to the preferred node. */ |
6662 | if (dst_nid == p->numa_preferred_nid) | |
2a1ed24c | 6663 | return 0; |
b1ad065e | 6664 | |
739294fb RR |
6665 | /* Leaving a core idle is often worse than degrading locality. */ |
6666 | if (env->idle != CPU_NOT_IDLE) | |
6667 | return -1; | |
6668 | ||
c1ceac62 RR |
6669 | if (numa_group) { |
6670 | src_faults = group_faults(p, src_nid); | |
6671 | dst_faults = group_faults(p, dst_nid); | |
6672 | } else { | |
6673 | src_faults = task_faults(p, src_nid); | |
6674 | dst_faults = task_faults(p, dst_nid); | |
b1ad065e RR |
6675 | } |
6676 | ||
c1ceac62 | 6677 | return dst_faults < src_faults; |
7a0f3083 MG |
6678 | } |
6679 | ||
3a7053b3 | 6680 | #else |
2a1ed24c | 6681 | static inline int migrate_degrades_locality(struct task_struct *p, |
3a7053b3 MG |
6682 | struct lb_env *env) |
6683 | { | |
2a1ed24c | 6684 | return -1; |
7a0f3083 | 6685 | } |
3a7053b3 MG |
6686 | #endif |
6687 | ||
1e3c88bd PZ |
6688 | /* |
6689 | * can_migrate_task - may task p from runqueue rq be migrated to this_cpu? | |
6690 | */ | |
6691 | static | |
8e45cb54 | 6692 | int can_migrate_task(struct task_struct *p, struct lb_env *env) |
1e3c88bd | 6693 | { |
2a1ed24c | 6694 | int tsk_cache_hot; |
e5673f28 KT |
6695 | |
6696 | lockdep_assert_held(&env->src_rq->lock); | |
6697 | ||
1e3c88bd PZ |
6698 | /* |
6699 | * We do not migrate tasks that are: | |
d3198084 | 6700 | * 1) throttled_lb_pair, or |
1e3c88bd | 6701 | * 2) cannot be migrated to this CPU due to cpus_allowed, or |
d3198084 JK |
6702 | * 3) running (obviously), or |
6703 | * 4) are cache-hot on their current CPU. | |
1e3c88bd | 6704 | */ |
d3198084 JK |
6705 | if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu)) |
6706 | return 0; | |
6707 | ||
0c98d344 | 6708 | if (!cpumask_test_cpu(env->dst_cpu, &p->cpus_allowed)) { |
e02e60c1 | 6709 | int cpu; |
88b8dac0 | 6710 | |
ae92882e | 6711 | schedstat_inc(p->se.statistics.nr_failed_migrations_affine); |
88b8dac0 | 6712 | |
6263322c PZ |
6713 | env->flags |= LBF_SOME_PINNED; |
6714 | ||
88b8dac0 SV |
6715 | /* |
6716 | * Remember if this task can be migrated to any other cpu in | |
6717 | * our sched_group. We may want to revisit it if we couldn't | |
6718 | * meet load balance goals by pulling other tasks on src_cpu. | |
6719 | * | |
65a4433a JH |
6720 | * Avoid computing new_dst_cpu for NEWLY_IDLE or if we have |
6721 | * already computed one in current iteration. | |
88b8dac0 | 6722 | */ |
65a4433a | 6723 | if (env->idle == CPU_NEWLY_IDLE || (env->flags & LBF_DST_PINNED)) |
88b8dac0 SV |
6724 | return 0; |
6725 | ||
e02e60c1 JK |
6726 | /* Prevent to re-select dst_cpu via env's cpus */ |
6727 | for_each_cpu_and(cpu, env->dst_grpmask, env->cpus) { | |
0c98d344 | 6728 | if (cpumask_test_cpu(cpu, &p->cpus_allowed)) { |
6263322c | 6729 | env->flags |= LBF_DST_PINNED; |
e02e60c1 JK |
6730 | env->new_dst_cpu = cpu; |
6731 | break; | |
6732 | } | |
88b8dac0 | 6733 | } |
e02e60c1 | 6734 | |
1e3c88bd PZ |
6735 | return 0; |
6736 | } | |
88b8dac0 SV |
6737 | |
6738 | /* Record that we found atleast one task that could run on dst_cpu */ | |
8e45cb54 | 6739 | env->flags &= ~LBF_ALL_PINNED; |
1e3c88bd | 6740 | |
ddcdf6e7 | 6741 | if (task_running(env->src_rq, p)) { |
ae92882e | 6742 | schedstat_inc(p->se.statistics.nr_failed_migrations_running); |
1e3c88bd PZ |
6743 | return 0; |
6744 | } | |
6745 | ||
6746 | /* | |
6747 | * Aggressive migration if: | |
3a7053b3 MG |
6748 | * 1) destination numa is preferred |
6749 | * 2) task is cache cold, or | |
6750 | * 3) too many balance attempts have failed. | |
1e3c88bd | 6751 | */ |
2a1ed24c SD |
6752 | tsk_cache_hot = migrate_degrades_locality(p, env); |
6753 | if (tsk_cache_hot == -1) | |
6754 | tsk_cache_hot = task_hot(p, env); | |
3a7053b3 | 6755 | |
2a1ed24c | 6756 | if (tsk_cache_hot <= 0 || |
7a96c231 | 6757 | env->sd->nr_balance_failed > env->sd->cache_nice_tries) { |
2a1ed24c | 6758 | if (tsk_cache_hot == 1) { |
ae92882e JP |
6759 | schedstat_inc(env->sd->lb_hot_gained[env->idle]); |
6760 | schedstat_inc(p->se.statistics.nr_forced_migrations); | |
3a7053b3 | 6761 | } |
1e3c88bd PZ |
6762 | return 1; |
6763 | } | |
6764 | ||
ae92882e | 6765 | schedstat_inc(p->se.statistics.nr_failed_migrations_hot); |
4e2dcb73 | 6766 | return 0; |
1e3c88bd PZ |
6767 | } |
6768 | ||
897c395f | 6769 | /* |
163122b7 KT |
6770 | * detach_task() -- detach the task for the migration specified in env |
6771 | */ | |
6772 | static void detach_task(struct task_struct *p, struct lb_env *env) | |
6773 | { | |
6774 | lockdep_assert_held(&env->src_rq->lock); | |
6775 | ||
163122b7 | 6776 | p->on_rq = TASK_ON_RQ_MIGRATING; |
5704ac0a | 6777 | deactivate_task(env->src_rq, p, DEQUEUE_NOCLOCK); |
163122b7 KT |
6778 | set_task_cpu(p, env->dst_cpu); |
6779 | } | |
6780 | ||
897c395f | 6781 | /* |
e5673f28 | 6782 | * detach_one_task() -- tries to dequeue exactly one task from env->src_rq, as |
897c395f | 6783 | * part of active balancing operations within "domain". |
897c395f | 6784 | * |
e5673f28 | 6785 | * Returns a task if successful and NULL otherwise. |
897c395f | 6786 | */ |
e5673f28 | 6787 | static struct task_struct *detach_one_task(struct lb_env *env) |
897c395f PZ |
6788 | { |
6789 | struct task_struct *p, *n; | |
897c395f | 6790 | |
e5673f28 KT |
6791 | lockdep_assert_held(&env->src_rq->lock); |
6792 | ||
367456c7 | 6793 | list_for_each_entry_safe(p, n, &env->src_rq->cfs_tasks, se.group_node) { |
367456c7 PZ |
6794 | if (!can_migrate_task(p, env)) |
6795 | continue; | |
897c395f | 6796 | |
163122b7 | 6797 | detach_task(p, env); |
e5673f28 | 6798 | |
367456c7 | 6799 | /* |
e5673f28 | 6800 | * Right now, this is only the second place where |
163122b7 | 6801 | * lb_gained[env->idle] is updated (other is detach_tasks) |
e5673f28 | 6802 | * so we can safely collect stats here rather than |
163122b7 | 6803 | * inside detach_tasks(). |
367456c7 | 6804 | */ |
ae92882e | 6805 | schedstat_inc(env->sd->lb_gained[env->idle]); |
e5673f28 | 6806 | return p; |
897c395f | 6807 | } |
e5673f28 | 6808 | return NULL; |
897c395f PZ |
6809 | } |
6810 | ||
eb95308e PZ |
6811 | static const unsigned int sched_nr_migrate_break = 32; |
6812 | ||
5d6523eb | 6813 | /* |
163122b7 KT |
6814 | * detach_tasks() -- tries to detach up to imbalance weighted load from |
6815 | * busiest_rq, as part of a balancing operation within domain "sd". | |
5d6523eb | 6816 | * |
163122b7 | 6817 | * Returns number of detached tasks if successful and 0 otherwise. |
5d6523eb | 6818 | */ |
163122b7 | 6819 | static int detach_tasks(struct lb_env *env) |
1e3c88bd | 6820 | { |
5d6523eb PZ |
6821 | struct list_head *tasks = &env->src_rq->cfs_tasks; |
6822 | struct task_struct *p; | |
367456c7 | 6823 | unsigned long load; |
163122b7 KT |
6824 | int detached = 0; |
6825 | ||
6826 | lockdep_assert_held(&env->src_rq->lock); | |
1e3c88bd | 6827 | |
bd939f45 | 6828 | if (env->imbalance <= 0) |
5d6523eb | 6829 | return 0; |
1e3c88bd | 6830 | |
5d6523eb | 6831 | while (!list_empty(tasks)) { |
985d3a4c YD |
6832 | /* |
6833 | * We don't want to steal all, otherwise we may be treated likewise, | |
6834 | * which could at worst lead to a livelock crash. | |
6835 | */ | |
6836 | if (env->idle != CPU_NOT_IDLE && env->src_rq->nr_running <= 1) | |
6837 | break; | |
6838 | ||
5d6523eb | 6839 | p = list_first_entry(tasks, struct task_struct, se.group_node); |
1e3c88bd | 6840 | |
367456c7 PZ |
6841 | env->loop++; |
6842 | /* We've more or less seen every task there is, call it quits */ | |
5d6523eb | 6843 | if (env->loop > env->loop_max) |
367456c7 | 6844 | break; |
5d6523eb PZ |
6845 | |
6846 | /* take a breather every nr_migrate tasks */ | |
367456c7 | 6847 | if (env->loop > env->loop_break) { |
eb95308e | 6848 | env->loop_break += sched_nr_migrate_break; |
8e45cb54 | 6849 | env->flags |= LBF_NEED_BREAK; |
ee00e66f | 6850 | break; |
a195f004 | 6851 | } |
1e3c88bd | 6852 | |
d3198084 | 6853 | if (!can_migrate_task(p, env)) |
367456c7 PZ |
6854 | goto next; |
6855 | ||
6856 | load = task_h_load(p); | |
5d6523eb | 6857 | |
eb95308e | 6858 | if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed) |
367456c7 PZ |
6859 | goto next; |
6860 | ||
bd939f45 | 6861 | if ((load / 2) > env->imbalance) |
367456c7 | 6862 | goto next; |
1e3c88bd | 6863 | |
163122b7 KT |
6864 | detach_task(p, env); |
6865 | list_add(&p->se.group_node, &env->tasks); | |
6866 | ||
6867 | detached++; | |
bd939f45 | 6868 | env->imbalance -= load; |
1e3c88bd PZ |
6869 | |
6870 | #ifdef CONFIG_PREEMPT | |
ee00e66f PZ |
6871 | /* |
6872 | * NEWIDLE balancing is a source of latency, so preemptible | |
163122b7 | 6873 | * kernels will stop after the first task is detached to minimize |
ee00e66f PZ |
6874 | * the critical section. |
6875 | */ | |
5d6523eb | 6876 | if (env->idle == CPU_NEWLY_IDLE) |
ee00e66f | 6877 | break; |
1e3c88bd PZ |
6878 | #endif |
6879 | ||
ee00e66f PZ |
6880 | /* |
6881 | * We only want to steal up to the prescribed amount of | |
6882 | * weighted load. | |
6883 | */ | |
bd939f45 | 6884 | if (env->imbalance <= 0) |
ee00e66f | 6885 | break; |
367456c7 PZ |
6886 | |
6887 | continue; | |
6888 | next: | |
5d6523eb | 6889 | list_move_tail(&p->se.group_node, tasks); |
1e3c88bd | 6890 | } |
5d6523eb | 6891 | |
1e3c88bd | 6892 | /* |
163122b7 KT |
6893 | * Right now, this is one of only two places we collect this stat |
6894 | * so we can safely collect detach_one_task() stats here rather | |
6895 | * than inside detach_one_task(). | |
1e3c88bd | 6896 | */ |
ae92882e | 6897 | schedstat_add(env->sd->lb_gained[env->idle], detached); |
1e3c88bd | 6898 | |
163122b7 KT |
6899 | return detached; |
6900 | } | |
6901 | ||
6902 | /* | |
6903 | * attach_task() -- attach the task detached by detach_task() to its new rq. | |
6904 | */ | |
6905 | static void attach_task(struct rq *rq, struct task_struct *p) | |
6906 | { | |
6907 | lockdep_assert_held(&rq->lock); | |
6908 | ||
6909 | BUG_ON(task_rq(p) != rq); | |
5704ac0a | 6910 | activate_task(rq, p, ENQUEUE_NOCLOCK); |
3ea94de1 | 6911 | p->on_rq = TASK_ON_RQ_QUEUED; |
163122b7 KT |
6912 | check_preempt_curr(rq, p, 0); |
6913 | } | |
6914 | ||
6915 | /* | |
6916 | * attach_one_task() -- attaches the task returned from detach_one_task() to | |
6917 | * its new rq. | |
6918 | */ | |
6919 | static void attach_one_task(struct rq *rq, struct task_struct *p) | |
6920 | { | |
8a8c69c3 PZ |
6921 | struct rq_flags rf; |
6922 | ||
6923 | rq_lock(rq, &rf); | |
5704ac0a | 6924 | update_rq_clock(rq); |
163122b7 | 6925 | attach_task(rq, p); |
8a8c69c3 | 6926 | rq_unlock(rq, &rf); |
163122b7 KT |
6927 | } |
6928 | ||
6929 | /* | |
6930 | * attach_tasks() -- attaches all tasks detached by detach_tasks() to their | |
6931 | * new rq. | |
6932 | */ | |
6933 | static void attach_tasks(struct lb_env *env) | |
6934 | { | |
6935 | struct list_head *tasks = &env->tasks; | |
6936 | struct task_struct *p; | |
8a8c69c3 | 6937 | struct rq_flags rf; |
163122b7 | 6938 | |
8a8c69c3 | 6939 | rq_lock(env->dst_rq, &rf); |
5704ac0a | 6940 | update_rq_clock(env->dst_rq); |
163122b7 KT |
6941 | |
6942 | while (!list_empty(tasks)) { | |
6943 | p = list_first_entry(tasks, struct task_struct, se.group_node); | |
6944 | list_del_init(&p->se.group_node); | |
1e3c88bd | 6945 | |
163122b7 KT |
6946 | attach_task(env->dst_rq, p); |
6947 | } | |
6948 | ||
8a8c69c3 | 6949 | rq_unlock(env->dst_rq, &rf); |
1e3c88bd PZ |
6950 | } |
6951 | ||
230059de | 6952 | #ifdef CONFIG_FAIR_GROUP_SCHED |
a9e7f654 TH |
6953 | |
6954 | static inline bool cfs_rq_is_decayed(struct cfs_rq *cfs_rq) | |
6955 | { | |
6956 | if (cfs_rq->load.weight) | |
6957 | return false; | |
6958 | ||
6959 | if (cfs_rq->avg.load_sum) | |
6960 | return false; | |
6961 | ||
6962 | if (cfs_rq->avg.util_sum) | |
6963 | return false; | |
6964 | ||
6965 | if (cfs_rq->runnable_load_sum) | |
6966 | return false; | |
6967 | ||
6968 | return true; | |
6969 | } | |
6970 | ||
48a16753 | 6971 | static void update_blocked_averages(int cpu) |
9e3081ca | 6972 | { |
9e3081ca | 6973 | struct rq *rq = cpu_rq(cpu); |
a9e7f654 | 6974 | struct cfs_rq *cfs_rq, *pos; |
8a8c69c3 | 6975 | struct rq_flags rf; |
9e3081ca | 6976 | |
8a8c69c3 | 6977 | rq_lock_irqsave(rq, &rf); |
48a16753 | 6978 | update_rq_clock(rq); |
9d89c257 | 6979 | |
9763b67f PZ |
6980 | /* |
6981 | * Iterates the task_group tree in a bottom up fashion, see | |
6982 | * list_add_leaf_cfs_rq() for details. | |
6983 | */ | |
a9e7f654 | 6984 | for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) { |
bc427898 VG |
6985 | struct sched_entity *se; |
6986 | ||
9d89c257 YD |
6987 | /* throttled entities do not contribute to load */ |
6988 | if (throttled_hierarchy(cfs_rq)) | |
6989 | continue; | |
48a16753 | 6990 | |
3a123bbb | 6991 | if (update_cfs_rq_load_avg(cfs_rq_clock_task(cfs_rq), cfs_rq)) |
9d89c257 | 6992 | update_tg_load_avg(cfs_rq, 0); |
4e516076 | 6993 | |
bc427898 VG |
6994 | /* Propagate pending load changes to the parent, if any: */ |
6995 | se = cfs_rq->tg->se[cpu]; | |
6996 | if (se && !skip_blocked_update(se)) | |
6997 | update_load_avg(se, 0); | |
a9e7f654 TH |
6998 | |
6999 | /* | |
7000 | * There can be a lot of idle CPU cgroups. Don't let fully | |
7001 | * decayed cfs_rqs linger on the list. | |
7002 | */ | |
7003 | if (cfs_rq_is_decayed(cfs_rq)) | |
7004 | list_del_leaf_cfs_rq(cfs_rq); | |
9d89c257 | 7005 | } |
8a8c69c3 | 7006 | rq_unlock_irqrestore(rq, &rf); |
9e3081ca PZ |
7007 | } |
7008 | ||
9763b67f | 7009 | /* |
68520796 | 7010 | * Compute the hierarchical load factor for cfs_rq and all its ascendants. |
9763b67f PZ |
7011 | * This needs to be done in a top-down fashion because the load of a child |
7012 | * group is a fraction of its parents load. | |
7013 | */ | |
68520796 | 7014 | static void update_cfs_rq_h_load(struct cfs_rq *cfs_rq) |
9763b67f | 7015 | { |
68520796 VD |
7016 | struct rq *rq = rq_of(cfs_rq); |
7017 | struct sched_entity *se = cfs_rq->tg->se[cpu_of(rq)]; | |
a35b6466 | 7018 | unsigned long now = jiffies; |
68520796 | 7019 | unsigned long load; |
a35b6466 | 7020 | |
68520796 | 7021 | if (cfs_rq->last_h_load_update == now) |
a35b6466 PZ |
7022 | return; |
7023 | ||
68520796 VD |
7024 | cfs_rq->h_load_next = NULL; |
7025 | for_each_sched_entity(se) { | |
7026 | cfs_rq = cfs_rq_of(se); | |
7027 | cfs_rq->h_load_next = se; | |
7028 | if (cfs_rq->last_h_load_update == now) | |
7029 | break; | |
7030 | } | |
a35b6466 | 7031 | |
68520796 | 7032 | if (!se) { |
7ea241af | 7033 | cfs_rq->h_load = cfs_rq_load_avg(cfs_rq); |
68520796 VD |
7034 | cfs_rq->last_h_load_update = now; |
7035 | } | |
7036 | ||
7037 | while ((se = cfs_rq->h_load_next) != NULL) { | |
7038 | load = cfs_rq->h_load; | |
7ea241af YD |
7039 | load = div64_ul(load * se->avg.load_avg, |
7040 | cfs_rq_load_avg(cfs_rq) + 1); | |
68520796 VD |
7041 | cfs_rq = group_cfs_rq(se); |
7042 | cfs_rq->h_load = load; | |
7043 | cfs_rq->last_h_load_update = now; | |
7044 | } | |
9763b67f PZ |
7045 | } |
7046 | ||
367456c7 | 7047 | static unsigned long task_h_load(struct task_struct *p) |
230059de | 7048 | { |
367456c7 | 7049 | struct cfs_rq *cfs_rq = task_cfs_rq(p); |
230059de | 7050 | |
68520796 | 7051 | update_cfs_rq_h_load(cfs_rq); |
9d89c257 | 7052 | return div64_ul(p->se.avg.load_avg * cfs_rq->h_load, |
7ea241af | 7053 | cfs_rq_load_avg(cfs_rq) + 1); |
230059de PZ |
7054 | } |
7055 | #else | |
48a16753 | 7056 | static inline void update_blocked_averages(int cpu) |
9e3081ca | 7057 | { |
6c1d47c0 VG |
7058 | struct rq *rq = cpu_rq(cpu); |
7059 | struct cfs_rq *cfs_rq = &rq->cfs; | |
8a8c69c3 | 7060 | struct rq_flags rf; |
6c1d47c0 | 7061 | |
8a8c69c3 | 7062 | rq_lock_irqsave(rq, &rf); |
6c1d47c0 | 7063 | update_rq_clock(rq); |
3a123bbb | 7064 | update_cfs_rq_load_avg(cfs_rq_clock_task(cfs_rq), cfs_rq); |
8a8c69c3 | 7065 | rq_unlock_irqrestore(rq, &rf); |
9e3081ca PZ |
7066 | } |
7067 | ||
367456c7 | 7068 | static unsigned long task_h_load(struct task_struct *p) |
1e3c88bd | 7069 | { |
9d89c257 | 7070 | return p->se.avg.load_avg; |
1e3c88bd | 7071 | } |
230059de | 7072 | #endif |
1e3c88bd | 7073 | |
1e3c88bd | 7074 | /********** Helpers for find_busiest_group ************************/ |
caeb178c RR |
7075 | |
7076 | enum group_type { | |
7077 | group_other = 0, | |
7078 | group_imbalanced, | |
7079 | group_overloaded, | |
7080 | }; | |
7081 | ||
1e3c88bd PZ |
7082 | /* |
7083 | * sg_lb_stats - stats of a sched_group required for load_balancing | |
7084 | */ | |
7085 | struct sg_lb_stats { | |
7086 | unsigned long avg_load; /*Avg load across the CPUs of the group */ | |
7087 | unsigned long group_load; /* Total load over the CPUs of the group */ | |
1e3c88bd | 7088 | unsigned long sum_weighted_load; /* Weighted load of group's tasks */ |
56cf515b | 7089 | unsigned long load_per_task; |
63b2ca30 | 7090 | unsigned long group_capacity; |
9e91d61d | 7091 | unsigned long group_util; /* Total utilization of the group */ |
147c5fc2 | 7092 | unsigned int sum_nr_running; /* Nr tasks running in the group */ |
147c5fc2 PZ |
7093 | unsigned int idle_cpus; |
7094 | unsigned int group_weight; | |
caeb178c | 7095 | enum group_type group_type; |
ea67821b | 7096 | int group_no_capacity; |
0ec8aa00 PZ |
7097 | #ifdef CONFIG_NUMA_BALANCING |
7098 | unsigned int nr_numa_running; | |
7099 | unsigned int nr_preferred_running; | |
7100 | #endif | |
1e3c88bd PZ |
7101 | }; |
7102 | ||
56cf515b JK |
7103 | /* |
7104 | * sd_lb_stats - Structure to store the statistics of a sched_domain | |
7105 | * during load balancing. | |
7106 | */ | |
7107 | struct sd_lb_stats { | |
7108 | struct sched_group *busiest; /* Busiest group in this sd */ | |
7109 | struct sched_group *local; /* Local group in this sd */ | |
90001d67 | 7110 | unsigned long total_running; |
56cf515b | 7111 | unsigned long total_load; /* Total load of all groups in sd */ |
63b2ca30 | 7112 | unsigned long total_capacity; /* Total capacity of all groups in sd */ |
56cf515b JK |
7113 | unsigned long avg_load; /* Average load across all groups in sd */ |
7114 | ||
56cf515b | 7115 | struct sg_lb_stats busiest_stat;/* Statistics of the busiest group */ |
147c5fc2 | 7116 | struct sg_lb_stats local_stat; /* Statistics of the local group */ |
56cf515b JK |
7117 | }; |
7118 | ||
147c5fc2 PZ |
7119 | static inline void init_sd_lb_stats(struct sd_lb_stats *sds) |
7120 | { | |
7121 | /* | |
7122 | * Skimp on the clearing to avoid duplicate work. We can avoid clearing | |
7123 | * local_stat because update_sg_lb_stats() does a full clear/assignment. | |
7124 | * We must however clear busiest_stat::avg_load because | |
7125 | * update_sd_pick_busiest() reads this before assignment. | |
7126 | */ | |
7127 | *sds = (struct sd_lb_stats){ | |
7128 | .busiest = NULL, | |
7129 | .local = NULL, | |
90001d67 | 7130 | .total_running = 0UL, |
147c5fc2 | 7131 | .total_load = 0UL, |
63b2ca30 | 7132 | .total_capacity = 0UL, |
147c5fc2 PZ |
7133 | .busiest_stat = { |
7134 | .avg_load = 0UL, | |
caeb178c RR |
7135 | .sum_nr_running = 0, |
7136 | .group_type = group_other, | |
147c5fc2 PZ |
7137 | }, |
7138 | }; | |
7139 | } | |
7140 | ||
1e3c88bd PZ |
7141 | /** |
7142 | * get_sd_load_idx - Obtain the load index for a given sched domain. | |
7143 | * @sd: The sched_domain whose load_idx is to be obtained. | |
ed1b7732 | 7144 | * @idle: The idle status of the CPU for whose sd load_idx is obtained. |
e69f6186 YB |
7145 | * |
7146 | * Return: The load index. | |
1e3c88bd PZ |
7147 | */ |
7148 | static inline int get_sd_load_idx(struct sched_domain *sd, | |
7149 | enum cpu_idle_type idle) | |
7150 | { | |
7151 | int load_idx; | |
7152 | ||
7153 | switch (idle) { | |
7154 | case CPU_NOT_IDLE: | |
7155 | load_idx = sd->busy_idx; | |
7156 | break; | |
7157 | ||
7158 | case CPU_NEWLY_IDLE: | |
7159 | load_idx = sd->newidle_idx; | |
7160 | break; | |
7161 | default: | |
7162 | load_idx = sd->idle_idx; | |
7163 | break; | |
7164 | } | |
7165 | ||
7166 | return load_idx; | |
7167 | } | |
7168 | ||
ced549fa | 7169 | static unsigned long scale_rt_capacity(int cpu) |
1e3c88bd PZ |
7170 | { |
7171 | struct rq *rq = cpu_rq(cpu); | |
b5b4860d | 7172 | u64 total, used, age_stamp, avg; |
cadefd3d | 7173 | s64 delta; |
1e3c88bd | 7174 | |
b654f7de PZ |
7175 | /* |
7176 | * Since we're reading these variables without serialization make sure | |
7177 | * we read them once before doing sanity checks on them. | |
7178 | */ | |
316c1608 JL |
7179 | age_stamp = READ_ONCE(rq->age_stamp); |
7180 | avg = READ_ONCE(rq->rt_avg); | |
cebde6d6 | 7181 | delta = __rq_clock_broken(rq) - age_stamp; |
b654f7de | 7182 | |
cadefd3d PZ |
7183 | if (unlikely(delta < 0)) |
7184 | delta = 0; | |
7185 | ||
7186 | total = sched_avg_period() + delta; | |
aa483808 | 7187 | |
b5b4860d | 7188 | used = div_u64(avg, total); |
1e3c88bd | 7189 | |
b5b4860d VG |
7190 | if (likely(used < SCHED_CAPACITY_SCALE)) |
7191 | return SCHED_CAPACITY_SCALE - used; | |
1e3c88bd | 7192 | |
b5b4860d | 7193 | return 1; |
1e3c88bd PZ |
7194 | } |
7195 | ||
ced549fa | 7196 | static void update_cpu_capacity(struct sched_domain *sd, int cpu) |
1e3c88bd | 7197 | { |
8cd5601c | 7198 | unsigned long capacity = arch_scale_cpu_capacity(sd, cpu); |
1e3c88bd PZ |
7199 | struct sched_group *sdg = sd->groups; |
7200 | ||
ca6d75e6 | 7201 | cpu_rq(cpu)->cpu_capacity_orig = capacity; |
9d5efe05 | 7202 | |
ced549fa | 7203 | capacity *= scale_rt_capacity(cpu); |
ca8ce3d0 | 7204 | capacity >>= SCHED_CAPACITY_SHIFT; |
1e3c88bd | 7205 | |
ced549fa NP |
7206 | if (!capacity) |
7207 | capacity = 1; | |
1e3c88bd | 7208 | |
ced549fa NP |
7209 | cpu_rq(cpu)->cpu_capacity = capacity; |
7210 | sdg->sgc->capacity = capacity; | |
bf475ce0 | 7211 | sdg->sgc->min_capacity = capacity; |
1e3c88bd PZ |
7212 | } |
7213 | ||
63b2ca30 | 7214 | void update_group_capacity(struct sched_domain *sd, int cpu) |
1e3c88bd PZ |
7215 | { |
7216 | struct sched_domain *child = sd->child; | |
7217 | struct sched_group *group, *sdg = sd->groups; | |
bf475ce0 | 7218 | unsigned long capacity, min_capacity; |
4ec4412e VG |
7219 | unsigned long interval; |
7220 | ||
7221 | interval = msecs_to_jiffies(sd->balance_interval); | |
7222 | interval = clamp(interval, 1UL, max_load_balance_interval); | |
63b2ca30 | 7223 | sdg->sgc->next_update = jiffies + interval; |
1e3c88bd PZ |
7224 | |
7225 | if (!child) { | |
ced549fa | 7226 | update_cpu_capacity(sd, cpu); |
1e3c88bd PZ |
7227 | return; |
7228 | } | |
7229 | ||
dc7ff76e | 7230 | capacity = 0; |
bf475ce0 | 7231 | min_capacity = ULONG_MAX; |
1e3c88bd | 7232 | |
74a5ce20 PZ |
7233 | if (child->flags & SD_OVERLAP) { |
7234 | /* | |
7235 | * SD_OVERLAP domains cannot assume that child groups | |
7236 | * span the current group. | |
7237 | */ | |
7238 | ||
ae4df9d6 | 7239 | for_each_cpu(cpu, sched_group_span(sdg)) { |
63b2ca30 | 7240 | struct sched_group_capacity *sgc; |
9abf24d4 | 7241 | struct rq *rq = cpu_rq(cpu); |
863bffc8 | 7242 | |
9abf24d4 | 7243 | /* |
63b2ca30 | 7244 | * build_sched_domains() -> init_sched_groups_capacity() |
9abf24d4 SD |
7245 | * gets here before we've attached the domains to the |
7246 | * runqueues. | |
7247 | * | |
ced549fa NP |
7248 | * Use capacity_of(), which is set irrespective of domains |
7249 | * in update_cpu_capacity(). | |
9abf24d4 | 7250 | * |
dc7ff76e | 7251 | * This avoids capacity from being 0 and |
9abf24d4 | 7252 | * causing divide-by-zero issues on boot. |
9abf24d4 SD |
7253 | */ |
7254 | if (unlikely(!rq->sd)) { | |
ced549fa | 7255 | capacity += capacity_of(cpu); |
bf475ce0 MR |
7256 | } else { |
7257 | sgc = rq->sd->groups->sgc; | |
7258 | capacity += sgc->capacity; | |
9abf24d4 | 7259 | } |
863bffc8 | 7260 | |
bf475ce0 | 7261 | min_capacity = min(capacity, min_capacity); |
863bffc8 | 7262 | } |
74a5ce20 PZ |
7263 | } else { |
7264 | /* | |
7265 | * !SD_OVERLAP domains can assume that child groups | |
7266 | * span the current group. | |
97a7142f | 7267 | */ |
74a5ce20 PZ |
7268 | |
7269 | group = child->groups; | |
7270 | do { | |
bf475ce0 MR |
7271 | struct sched_group_capacity *sgc = group->sgc; |
7272 | ||
7273 | capacity += sgc->capacity; | |
7274 | min_capacity = min(sgc->min_capacity, min_capacity); | |
74a5ce20 PZ |
7275 | group = group->next; |
7276 | } while (group != child->groups); | |
7277 | } | |
1e3c88bd | 7278 | |
63b2ca30 | 7279 | sdg->sgc->capacity = capacity; |
bf475ce0 | 7280 | sdg->sgc->min_capacity = min_capacity; |
1e3c88bd PZ |
7281 | } |
7282 | ||
9d5efe05 | 7283 | /* |
ea67821b VG |
7284 | * Check whether the capacity of the rq has been noticeably reduced by side |
7285 | * activity. The imbalance_pct is used for the threshold. | |
7286 | * Return true is the capacity is reduced | |
9d5efe05 SV |
7287 | */ |
7288 | static inline int | |
ea67821b | 7289 | check_cpu_capacity(struct rq *rq, struct sched_domain *sd) |
9d5efe05 | 7290 | { |
ea67821b VG |
7291 | return ((rq->cpu_capacity * sd->imbalance_pct) < |
7292 | (rq->cpu_capacity_orig * 100)); | |
9d5efe05 SV |
7293 | } |
7294 | ||
30ce5dab PZ |
7295 | /* |
7296 | * Group imbalance indicates (and tries to solve) the problem where balancing | |
0c98d344 | 7297 | * groups is inadequate due to ->cpus_allowed constraints. |
30ce5dab PZ |
7298 | * |
7299 | * Imagine a situation of two groups of 4 cpus each and 4 tasks each with a | |
7300 | * cpumask covering 1 cpu of the first group and 3 cpus of the second group. | |
7301 | * Something like: | |
7302 | * | |
2b4d5b25 IM |
7303 | * { 0 1 2 3 } { 4 5 6 7 } |
7304 | * * * * * | |
30ce5dab PZ |
7305 | * |
7306 | * If we were to balance group-wise we'd place two tasks in the first group and | |
7307 | * two tasks in the second group. Clearly this is undesired as it will overload | |
7308 | * cpu 3 and leave one of the cpus in the second group unused. | |
7309 | * | |
7310 | * The current solution to this issue is detecting the skew in the first group | |
6263322c PZ |
7311 | * by noticing the lower domain failed to reach balance and had difficulty |
7312 | * moving tasks due to affinity constraints. | |
30ce5dab PZ |
7313 | * |
7314 | * When this is so detected; this group becomes a candidate for busiest; see | |
ed1b7732 | 7315 | * update_sd_pick_busiest(). And calculate_imbalance() and |
6263322c | 7316 | * find_busiest_group() avoid some of the usual balance conditions to allow it |
30ce5dab PZ |
7317 | * to create an effective group imbalance. |
7318 | * | |
7319 | * This is a somewhat tricky proposition since the next run might not find the | |
7320 | * group imbalance and decide the groups need to be balanced again. A most | |
7321 | * subtle and fragile situation. | |
7322 | */ | |
7323 | ||
6263322c | 7324 | static inline int sg_imbalanced(struct sched_group *group) |
30ce5dab | 7325 | { |
63b2ca30 | 7326 | return group->sgc->imbalance; |
30ce5dab PZ |
7327 | } |
7328 | ||
b37d9316 | 7329 | /* |
ea67821b VG |
7330 | * group_has_capacity returns true if the group has spare capacity that could |
7331 | * be used by some tasks. | |
7332 | * We consider that a group has spare capacity if the * number of task is | |
9e91d61d DE |
7333 | * smaller than the number of CPUs or if the utilization is lower than the |
7334 | * available capacity for CFS tasks. | |
ea67821b VG |
7335 | * For the latter, we use a threshold to stabilize the state, to take into |
7336 | * account the variance of the tasks' load and to return true if the available | |
7337 | * capacity in meaningful for the load balancer. | |
7338 | * As an example, an available capacity of 1% can appear but it doesn't make | |
7339 | * any benefit for the load balance. | |
b37d9316 | 7340 | */ |
ea67821b VG |
7341 | static inline bool |
7342 | group_has_capacity(struct lb_env *env, struct sg_lb_stats *sgs) | |
b37d9316 | 7343 | { |
ea67821b VG |
7344 | if (sgs->sum_nr_running < sgs->group_weight) |
7345 | return true; | |
c61037e9 | 7346 | |
ea67821b | 7347 | if ((sgs->group_capacity * 100) > |
9e91d61d | 7348 | (sgs->group_util * env->sd->imbalance_pct)) |
ea67821b | 7349 | return true; |
b37d9316 | 7350 | |
ea67821b VG |
7351 | return false; |
7352 | } | |
7353 | ||
7354 | /* | |
7355 | * group_is_overloaded returns true if the group has more tasks than it can | |
7356 | * handle. | |
7357 | * group_is_overloaded is not equals to !group_has_capacity because a group | |
7358 | * with the exact right number of tasks, has no more spare capacity but is not | |
7359 | * overloaded so both group_has_capacity and group_is_overloaded return | |
7360 | * false. | |
7361 | */ | |
7362 | static inline bool | |
7363 | group_is_overloaded(struct lb_env *env, struct sg_lb_stats *sgs) | |
7364 | { | |
7365 | if (sgs->sum_nr_running <= sgs->group_weight) | |
7366 | return false; | |
b37d9316 | 7367 | |
ea67821b | 7368 | if ((sgs->group_capacity * 100) < |
9e91d61d | 7369 | (sgs->group_util * env->sd->imbalance_pct)) |
ea67821b | 7370 | return true; |
b37d9316 | 7371 | |
ea67821b | 7372 | return false; |
b37d9316 PZ |
7373 | } |
7374 | ||
9e0994c0 MR |
7375 | /* |
7376 | * group_smaller_cpu_capacity: Returns true if sched_group sg has smaller | |
7377 | * per-CPU capacity than sched_group ref. | |
7378 | */ | |
7379 | static inline bool | |
7380 | group_smaller_cpu_capacity(struct sched_group *sg, struct sched_group *ref) | |
7381 | { | |
7382 | return sg->sgc->min_capacity * capacity_margin < | |
7383 | ref->sgc->min_capacity * 1024; | |
7384 | } | |
7385 | ||
79a89f92 LY |
7386 | static inline enum |
7387 | group_type group_classify(struct sched_group *group, | |
7388 | struct sg_lb_stats *sgs) | |
caeb178c | 7389 | { |
ea67821b | 7390 | if (sgs->group_no_capacity) |
caeb178c RR |
7391 | return group_overloaded; |
7392 | ||
7393 | if (sg_imbalanced(group)) | |
7394 | return group_imbalanced; | |
7395 | ||
7396 | return group_other; | |
7397 | } | |
7398 | ||
1e3c88bd PZ |
7399 | /** |
7400 | * update_sg_lb_stats - Update sched_group's statistics for load balancing. | |
cd96891d | 7401 | * @env: The load balancing environment. |
1e3c88bd | 7402 | * @group: sched_group whose statistics are to be updated. |
1e3c88bd | 7403 | * @load_idx: Load index of sched_domain of this_cpu for load calc. |
1e3c88bd | 7404 | * @local_group: Does group contain this_cpu. |
1e3c88bd | 7405 | * @sgs: variable to hold the statistics for this group. |
cd3bd4e6 | 7406 | * @overload: Indicate more than one runnable task for any CPU. |
1e3c88bd | 7407 | */ |
bd939f45 PZ |
7408 | static inline void update_sg_lb_stats(struct lb_env *env, |
7409 | struct sched_group *group, int load_idx, | |
4486edd1 TC |
7410 | int local_group, struct sg_lb_stats *sgs, |
7411 | bool *overload) | |
1e3c88bd | 7412 | { |
30ce5dab | 7413 | unsigned long load; |
a426f99c | 7414 | int i, nr_running; |
1e3c88bd | 7415 | |
b72ff13c PZ |
7416 | memset(sgs, 0, sizeof(*sgs)); |
7417 | ||
ae4df9d6 | 7418 | for_each_cpu_and(i, sched_group_span(group), env->cpus) { |
1e3c88bd PZ |
7419 | struct rq *rq = cpu_rq(i); |
7420 | ||
1e3c88bd | 7421 | /* Bias balancing toward cpus of our domain */ |
6263322c | 7422 | if (local_group) |
04f733b4 | 7423 | load = target_load(i, load_idx); |
6263322c | 7424 | else |
1e3c88bd | 7425 | load = source_load(i, load_idx); |
1e3c88bd PZ |
7426 | |
7427 | sgs->group_load += load; | |
9e91d61d | 7428 | sgs->group_util += cpu_util(i); |
65fdac08 | 7429 | sgs->sum_nr_running += rq->cfs.h_nr_running; |
4486edd1 | 7430 | |
a426f99c WL |
7431 | nr_running = rq->nr_running; |
7432 | if (nr_running > 1) | |
4486edd1 TC |
7433 | *overload = true; |
7434 | ||
0ec8aa00 PZ |
7435 | #ifdef CONFIG_NUMA_BALANCING |
7436 | sgs->nr_numa_running += rq->nr_numa_running; | |
7437 | sgs->nr_preferred_running += rq->nr_preferred_running; | |
7438 | #endif | |
c7132dd6 | 7439 | sgs->sum_weighted_load += weighted_cpuload(rq); |
a426f99c WL |
7440 | /* |
7441 | * No need to call idle_cpu() if nr_running is not 0 | |
7442 | */ | |
7443 | if (!nr_running && idle_cpu(i)) | |
aae6d3dd | 7444 | sgs->idle_cpus++; |
1e3c88bd PZ |
7445 | } |
7446 | ||
63b2ca30 NP |
7447 | /* Adjust by relative CPU capacity of the group */ |
7448 | sgs->group_capacity = group->sgc->capacity; | |
ca8ce3d0 | 7449 | sgs->avg_load = (sgs->group_load*SCHED_CAPACITY_SCALE) / sgs->group_capacity; |
1e3c88bd | 7450 | |
dd5feea1 | 7451 | if (sgs->sum_nr_running) |
38d0f770 | 7452 | sgs->load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running; |
1e3c88bd | 7453 | |
aae6d3dd | 7454 | sgs->group_weight = group->group_weight; |
b37d9316 | 7455 | |
ea67821b | 7456 | sgs->group_no_capacity = group_is_overloaded(env, sgs); |
79a89f92 | 7457 | sgs->group_type = group_classify(group, sgs); |
1e3c88bd PZ |
7458 | } |
7459 | ||
532cb4c4 MN |
7460 | /** |
7461 | * update_sd_pick_busiest - return 1 on busiest group | |
cd96891d | 7462 | * @env: The load balancing environment. |
532cb4c4 MN |
7463 | * @sds: sched_domain statistics |
7464 | * @sg: sched_group candidate to be checked for being the busiest | |
b6b12294 | 7465 | * @sgs: sched_group statistics |
532cb4c4 MN |
7466 | * |
7467 | * Determine if @sg is a busier group than the previously selected | |
7468 | * busiest group. | |
e69f6186 YB |
7469 | * |
7470 | * Return: %true if @sg is a busier group than the previously selected | |
7471 | * busiest group. %false otherwise. | |
532cb4c4 | 7472 | */ |
bd939f45 | 7473 | static bool update_sd_pick_busiest(struct lb_env *env, |
532cb4c4 MN |
7474 | struct sd_lb_stats *sds, |
7475 | struct sched_group *sg, | |
bd939f45 | 7476 | struct sg_lb_stats *sgs) |
532cb4c4 | 7477 | { |
caeb178c | 7478 | struct sg_lb_stats *busiest = &sds->busiest_stat; |
532cb4c4 | 7479 | |
caeb178c | 7480 | if (sgs->group_type > busiest->group_type) |
532cb4c4 MN |
7481 | return true; |
7482 | ||
caeb178c RR |
7483 | if (sgs->group_type < busiest->group_type) |
7484 | return false; | |
7485 | ||
7486 | if (sgs->avg_load <= busiest->avg_load) | |
7487 | return false; | |
7488 | ||
9e0994c0 MR |
7489 | if (!(env->sd->flags & SD_ASYM_CPUCAPACITY)) |
7490 | goto asym_packing; | |
7491 | ||
7492 | /* | |
7493 | * Candidate sg has no more than one task per CPU and | |
7494 | * has higher per-CPU capacity. Migrating tasks to less | |
7495 | * capable CPUs may harm throughput. Maximize throughput, | |
7496 | * power/energy consequences are not considered. | |
7497 | */ | |
7498 | if (sgs->sum_nr_running <= sgs->group_weight && | |
7499 | group_smaller_cpu_capacity(sds->local, sg)) | |
7500 | return false; | |
7501 | ||
7502 | asym_packing: | |
caeb178c RR |
7503 | /* This is the busiest node in its class. */ |
7504 | if (!(env->sd->flags & SD_ASYM_PACKING)) | |
532cb4c4 MN |
7505 | return true; |
7506 | ||
1f621e02 SD |
7507 | /* No ASYM_PACKING if target cpu is already busy */ |
7508 | if (env->idle == CPU_NOT_IDLE) | |
7509 | return true; | |
532cb4c4 | 7510 | /* |
afe06efd TC |
7511 | * ASYM_PACKING needs to move all the work to the highest |
7512 | * prority CPUs in the group, therefore mark all groups | |
7513 | * of lower priority than ourself as busy. | |
532cb4c4 | 7514 | */ |
afe06efd TC |
7515 | if (sgs->sum_nr_running && |
7516 | sched_asym_prefer(env->dst_cpu, sg->asym_prefer_cpu)) { | |
532cb4c4 MN |
7517 | if (!sds->busiest) |
7518 | return true; | |
7519 | ||
afe06efd TC |
7520 | /* Prefer to move from lowest priority cpu's work */ |
7521 | if (sched_asym_prefer(sds->busiest->asym_prefer_cpu, | |
7522 | sg->asym_prefer_cpu)) | |
532cb4c4 MN |
7523 | return true; |
7524 | } | |
7525 | ||
7526 | return false; | |
7527 | } | |
7528 | ||
0ec8aa00 PZ |
7529 | #ifdef CONFIG_NUMA_BALANCING |
7530 | static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs) | |
7531 | { | |
7532 | if (sgs->sum_nr_running > sgs->nr_numa_running) | |
7533 | return regular; | |
7534 | if (sgs->sum_nr_running > sgs->nr_preferred_running) | |
7535 | return remote; | |
7536 | return all; | |
7537 | } | |
7538 | ||
7539 | static inline enum fbq_type fbq_classify_rq(struct rq *rq) | |
7540 | { | |
7541 | if (rq->nr_running > rq->nr_numa_running) | |
7542 | return regular; | |
7543 | if (rq->nr_running > rq->nr_preferred_running) | |
7544 | return remote; | |
7545 | return all; | |
7546 | } | |
7547 | #else | |
7548 | static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs) | |
7549 | { | |
7550 | return all; | |
7551 | } | |
7552 | ||
7553 | static inline enum fbq_type fbq_classify_rq(struct rq *rq) | |
7554 | { | |
7555 | return regular; | |
7556 | } | |
7557 | #endif /* CONFIG_NUMA_BALANCING */ | |
7558 | ||
1e3c88bd | 7559 | /** |
461819ac | 7560 | * update_sd_lb_stats - Update sched_domain's statistics for load balancing. |
cd96891d | 7561 | * @env: The load balancing environment. |
1e3c88bd PZ |
7562 | * @sds: variable to hold the statistics for this sched_domain. |
7563 | */ | |
0ec8aa00 | 7564 | static inline void update_sd_lb_stats(struct lb_env *env, struct sd_lb_stats *sds) |
1e3c88bd | 7565 | { |
bd939f45 PZ |
7566 | struct sched_domain *child = env->sd->child; |
7567 | struct sched_group *sg = env->sd->groups; | |
05b40e05 | 7568 | struct sg_lb_stats *local = &sds->local_stat; |
56cf515b | 7569 | struct sg_lb_stats tmp_sgs; |
1e3c88bd | 7570 | int load_idx, prefer_sibling = 0; |
4486edd1 | 7571 | bool overload = false; |
1e3c88bd PZ |
7572 | |
7573 | if (child && child->flags & SD_PREFER_SIBLING) | |
7574 | prefer_sibling = 1; | |
7575 | ||
bd939f45 | 7576 | load_idx = get_sd_load_idx(env->sd, env->idle); |
1e3c88bd PZ |
7577 | |
7578 | do { | |
56cf515b | 7579 | struct sg_lb_stats *sgs = &tmp_sgs; |
1e3c88bd PZ |
7580 | int local_group; |
7581 | ||
ae4df9d6 | 7582 | local_group = cpumask_test_cpu(env->dst_cpu, sched_group_span(sg)); |
56cf515b JK |
7583 | if (local_group) { |
7584 | sds->local = sg; | |
05b40e05 | 7585 | sgs = local; |
b72ff13c PZ |
7586 | |
7587 | if (env->idle != CPU_NEWLY_IDLE || | |
63b2ca30 NP |
7588 | time_after_eq(jiffies, sg->sgc->next_update)) |
7589 | update_group_capacity(env->sd, env->dst_cpu); | |
56cf515b | 7590 | } |
1e3c88bd | 7591 | |
4486edd1 TC |
7592 | update_sg_lb_stats(env, sg, load_idx, local_group, sgs, |
7593 | &overload); | |
1e3c88bd | 7594 | |
b72ff13c PZ |
7595 | if (local_group) |
7596 | goto next_group; | |
7597 | ||
1e3c88bd PZ |
7598 | /* |
7599 | * In case the child domain prefers tasks go to siblings | |
ea67821b | 7600 | * first, lower the sg capacity so that we'll try |
75dd321d NR |
7601 | * and move all the excess tasks away. We lower the capacity |
7602 | * of a group only if the local group has the capacity to fit | |
ea67821b VG |
7603 | * these excess tasks. The extra check prevents the case where |
7604 | * you always pull from the heaviest group when it is already | |
7605 | * under-utilized (possible with a large weight task outweighs | |
7606 | * the tasks on the system). | |
1e3c88bd | 7607 | */ |
b72ff13c | 7608 | if (prefer_sibling && sds->local && |
05b40e05 SD |
7609 | group_has_capacity(env, local) && |
7610 | (sgs->sum_nr_running > local->sum_nr_running + 1)) { | |
ea67821b | 7611 | sgs->group_no_capacity = 1; |
79a89f92 | 7612 | sgs->group_type = group_classify(sg, sgs); |
cb0b9f24 | 7613 | } |
1e3c88bd | 7614 | |
b72ff13c | 7615 | if (update_sd_pick_busiest(env, sds, sg, sgs)) { |
532cb4c4 | 7616 | sds->busiest = sg; |
56cf515b | 7617 | sds->busiest_stat = *sgs; |
1e3c88bd PZ |
7618 | } |
7619 | ||
b72ff13c PZ |
7620 | next_group: |
7621 | /* Now, start updating sd_lb_stats */ | |
90001d67 | 7622 | sds->total_running += sgs->sum_nr_running; |
b72ff13c | 7623 | sds->total_load += sgs->group_load; |
63b2ca30 | 7624 | sds->total_capacity += sgs->group_capacity; |
b72ff13c | 7625 | |
532cb4c4 | 7626 | sg = sg->next; |
bd939f45 | 7627 | } while (sg != env->sd->groups); |
0ec8aa00 PZ |
7628 | |
7629 | if (env->sd->flags & SD_NUMA) | |
7630 | env->fbq_type = fbq_classify_group(&sds->busiest_stat); | |
4486edd1 TC |
7631 | |
7632 | if (!env->sd->parent) { | |
7633 | /* update overload indicator if we are at root domain */ | |
7634 | if (env->dst_rq->rd->overload != overload) | |
7635 | env->dst_rq->rd->overload = overload; | |
7636 | } | |
532cb4c4 MN |
7637 | } |
7638 | ||
532cb4c4 MN |
7639 | /** |
7640 | * check_asym_packing - Check to see if the group is packed into the | |
0ba42a59 | 7641 | * sched domain. |
532cb4c4 MN |
7642 | * |
7643 | * This is primarily intended to used at the sibling level. Some | |
7644 | * cores like POWER7 prefer to use lower numbered SMT threads. In the | |
7645 | * case of POWER7, it can move to lower SMT modes only when higher | |
7646 | * threads are idle. When in lower SMT modes, the threads will | |
7647 | * perform better since they share less core resources. Hence when we | |
7648 | * have idle threads, we want them to be the higher ones. | |
7649 | * | |
7650 | * This packing function is run on idle threads. It checks to see if | |
7651 | * the busiest CPU in this domain (core in the P7 case) has a higher | |
7652 | * CPU number than the packing function is being run on. Here we are | |
7653 | * assuming lower CPU number will be equivalent to lower a SMT thread | |
7654 | * number. | |
7655 | * | |
e69f6186 | 7656 | * Return: 1 when packing is required and a task should be moved to |
46123355 | 7657 | * this CPU. The amount of the imbalance is returned in env->imbalance. |
b6b12294 | 7658 | * |
cd96891d | 7659 | * @env: The load balancing environment. |
532cb4c4 | 7660 | * @sds: Statistics of the sched_domain which is to be packed |
532cb4c4 | 7661 | */ |
bd939f45 | 7662 | static int check_asym_packing(struct lb_env *env, struct sd_lb_stats *sds) |
532cb4c4 MN |
7663 | { |
7664 | int busiest_cpu; | |
7665 | ||
bd939f45 | 7666 | if (!(env->sd->flags & SD_ASYM_PACKING)) |
532cb4c4 MN |
7667 | return 0; |
7668 | ||
1f621e02 SD |
7669 | if (env->idle == CPU_NOT_IDLE) |
7670 | return 0; | |
7671 | ||
532cb4c4 MN |
7672 | if (!sds->busiest) |
7673 | return 0; | |
7674 | ||
afe06efd TC |
7675 | busiest_cpu = sds->busiest->asym_prefer_cpu; |
7676 | if (sched_asym_prefer(busiest_cpu, env->dst_cpu)) | |
532cb4c4 MN |
7677 | return 0; |
7678 | ||
bd939f45 | 7679 | env->imbalance = DIV_ROUND_CLOSEST( |
63b2ca30 | 7680 | sds->busiest_stat.avg_load * sds->busiest_stat.group_capacity, |
ca8ce3d0 | 7681 | SCHED_CAPACITY_SCALE); |
bd939f45 | 7682 | |
532cb4c4 | 7683 | return 1; |
1e3c88bd PZ |
7684 | } |
7685 | ||
7686 | /** | |
7687 | * fix_small_imbalance - Calculate the minor imbalance that exists | |
7688 | * amongst the groups of a sched_domain, during | |
7689 | * load balancing. | |
cd96891d | 7690 | * @env: The load balancing environment. |
1e3c88bd | 7691 | * @sds: Statistics of the sched_domain whose imbalance is to be calculated. |
1e3c88bd | 7692 | */ |
bd939f45 PZ |
7693 | static inline |
7694 | void fix_small_imbalance(struct lb_env *env, struct sd_lb_stats *sds) | |
1e3c88bd | 7695 | { |
63b2ca30 | 7696 | unsigned long tmp, capa_now = 0, capa_move = 0; |
1e3c88bd | 7697 | unsigned int imbn = 2; |
dd5feea1 | 7698 | unsigned long scaled_busy_load_per_task; |
56cf515b | 7699 | struct sg_lb_stats *local, *busiest; |
1e3c88bd | 7700 | |
56cf515b JK |
7701 | local = &sds->local_stat; |
7702 | busiest = &sds->busiest_stat; | |
1e3c88bd | 7703 | |
56cf515b JK |
7704 | if (!local->sum_nr_running) |
7705 | local->load_per_task = cpu_avg_load_per_task(env->dst_cpu); | |
7706 | else if (busiest->load_per_task > local->load_per_task) | |
7707 | imbn = 1; | |
dd5feea1 | 7708 | |
56cf515b | 7709 | scaled_busy_load_per_task = |
ca8ce3d0 | 7710 | (busiest->load_per_task * SCHED_CAPACITY_SCALE) / |
63b2ca30 | 7711 | busiest->group_capacity; |
56cf515b | 7712 | |
3029ede3 VD |
7713 | if (busiest->avg_load + scaled_busy_load_per_task >= |
7714 | local->avg_load + (scaled_busy_load_per_task * imbn)) { | |
56cf515b | 7715 | env->imbalance = busiest->load_per_task; |
1e3c88bd PZ |
7716 | return; |
7717 | } | |
7718 | ||
7719 | /* | |
7720 | * OK, we don't have enough imbalance to justify moving tasks, | |
ced549fa | 7721 | * however we may be able to increase total CPU capacity used by |
1e3c88bd PZ |
7722 | * moving them. |
7723 | */ | |
7724 | ||
63b2ca30 | 7725 | capa_now += busiest->group_capacity * |
56cf515b | 7726 | min(busiest->load_per_task, busiest->avg_load); |
63b2ca30 | 7727 | capa_now += local->group_capacity * |
56cf515b | 7728 | min(local->load_per_task, local->avg_load); |
ca8ce3d0 | 7729 | capa_now /= SCHED_CAPACITY_SCALE; |
1e3c88bd PZ |
7730 | |
7731 | /* Amount of load we'd subtract */ | |
a2cd4260 | 7732 | if (busiest->avg_load > scaled_busy_load_per_task) { |
63b2ca30 | 7733 | capa_move += busiest->group_capacity * |
56cf515b | 7734 | min(busiest->load_per_task, |
a2cd4260 | 7735 | busiest->avg_load - scaled_busy_load_per_task); |
56cf515b | 7736 | } |
1e3c88bd PZ |
7737 | |
7738 | /* Amount of load we'd add */ | |
63b2ca30 | 7739 | if (busiest->avg_load * busiest->group_capacity < |
ca8ce3d0 | 7740 | busiest->load_per_task * SCHED_CAPACITY_SCALE) { |
63b2ca30 NP |
7741 | tmp = (busiest->avg_load * busiest->group_capacity) / |
7742 | local->group_capacity; | |
56cf515b | 7743 | } else { |
ca8ce3d0 | 7744 | tmp = (busiest->load_per_task * SCHED_CAPACITY_SCALE) / |
63b2ca30 | 7745 | local->group_capacity; |
56cf515b | 7746 | } |
63b2ca30 | 7747 | capa_move += local->group_capacity * |
3ae11c90 | 7748 | min(local->load_per_task, local->avg_load + tmp); |
ca8ce3d0 | 7749 | capa_move /= SCHED_CAPACITY_SCALE; |
1e3c88bd PZ |
7750 | |
7751 | /* Move if we gain throughput */ | |
63b2ca30 | 7752 | if (capa_move > capa_now) |
56cf515b | 7753 | env->imbalance = busiest->load_per_task; |
1e3c88bd PZ |
7754 | } |
7755 | ||
7756 | /** | |
7757 | * calculate_imbalance - Calculate the amount of imbalance present within the | |
7758 | * groups of a given sched_domain during load balance. | |
bd939f45 | 7759 | * @env: load balance environment |
1e3c88bd | 7760 | * @sds: statistics of the sched_domain whose imbalance is to be calculated. |
1e3c88bd | 7761 | */ |
bd939f45 | 7762 | static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *sds) |
1e3c88bd | 7763 | { |
dd5feea1 | 7764 | unsigned long max_pull, load_above_capacity = ~0UL; |
56cf515b JK |
7765 | struct sg_lb_stats *local, *busiest; |
7766 | ||
7767 | local = &sds->local_stat; | |
56cf515b | 7768 | busiest = &sds->busiest_stat; |
dd5feea1 | 7769 | |
caeb178c | 7770 | if (busiest->group_type == group_imbalanced) { |
30ce5dab PZ |
7771 | /* |
7772 | * In the group_imb case we cannot rely on group-wide averages | |
7773 | * to ensure cpu-load equilibrium, look at wider averages. XXX | |
7774 | */ | |
56cf515b JK |
7775 | busiest->load_per_task = |
7776 | min(busiest->load_per_task, sds->avg_load); | |
dd5feea1 SS |
7777 | } |
7778 | ||
1e3c88bd | 7779 | /* |
885e542c DE |
7780 | * Avg load of busiest sg can be less and avg load of local sg can |
7781 | * be greater than avg load across all sgs of sd because avg load | |
7782 | * factors in sg capacity and sgs with smaller group_type are | |
7783 | * skipped when updating the busiest sg: | |
1e3c88bd | 7784 | */ |
b1885550 VD |
7785 | if (busiest->avg_load <= sds->avg_load || |
7786 | local->avg_load >= sds->avg_load) { | |
bd939f45 PZ |
7787 | env->imbalance = 0; |
7788 | return fix_small_imbalance(env, sds); | |
1e3c88bd PZ |
7789 | } |
7790 | ||
9a5d9ba6 PZ |
7791 | /* |
7792 | * If there aren't any idle cpus, avoid creating some. | |
7793 | */ | |
7794 | if (busiest->group_type == group_overloaded && | |
7795 | local->group_type == group_overloaded) { | |
1be0eb2a | 7796 | load_above_capacity = busiest->sum_nr_running * SCHED_CAPACITY_SCALE; |
cfa10334 | 7797 | if (load_above_capacity > busiest->group_capacity) { |
ea67821b | 7798 | load_above_capacity -= busiest->group_capacity; |
26656215 | 7799 | load_above_capacity *= scale_load_down(NICE_0_LOAD); |
cfa10334 MR |
7800 | load_above_capacity /= busiest->group_capacity; |
7801 | } else | |
ea67821b | 7802 | load_above_capacity = ~0UL; |
dd5feea1 SS |
7803 | } |
7804 | ||
7805 | /* | |
7806 | * We're trying to get all the cpus to the average_load, so we don't | |
7807 | * want to push ourselves above the average load, nor do we wish to | |
7808 | * reduce the max loaded cpu below the average load. At the same time, | |
0a9b23ce DE |
7809 | * we also don't want to reduce the group load below the group |
7810 | * capacity. Thus we look for the minimum possible imbalance. | |
dd5feea1 | 7811 | */ |
30ce5dab | 7812 | max_pull = min(busiest->avg_load - sds->avg_load, load_above_capacity); |
1e3c88bd PZ |
7813 | |
7814 | /* How much load to actually move to equalise the imbalance */ | |
56cf515b | 7815 | env->imbalance = min( |
63b2ca30 NP |
7816 | max_pull * busiest->group_capacity, |
7817 | (sds->avg_load - local->avg_load) * local->group_capacity | |
ca8ce3d0 | 7818 | ) / SCHED_CAPACITY_SCALE; |
1e3c88bd PZ |
7819 | |
7820 | /* | |
7821 | * if *imbalance is less than the average load per runnable task | |
25985edc | 7822 | * there is no guarantee that any tasks will be moved so we'll have |
1e3c88bd PZ |
7823 | * a think about bumping its value to force at least one task to be |
7824 | * moved | |
7825 | */ | |
56cf515b | 7826 | if (env->imbalance < busiest->load_per_task) |
bd939f45 | 7827 | return fix_small_imbalance(env, sds); |
1e3c88bd | 7828 | } |
fab47622 | 7829 | |
1e3c88bd PZ |
7830 | /******* find_busiest_group() helpers end here *********************/ |
7831 | ||
7832 | /** | |
7833 | * find_busiest_group - Returns the busiest group within the sched_domain | |
0a9b23ce | 7834 | * if there is an imbalance. |
1e3c88bd PZ |
7835 | * |
7836 | * Also calculates the amount of weighted load which should be moved | |
7837 | * to restore balance. | |
7838 | * | |
cd96891d | 7839 | * @env: The load balancing environment. |
1e3c88bd | 7840 | * |
e69f6186 | 7841 | * Return: - The busiest group if imbalance exists. |
1e3c88bd | 7842 | */ |
56cf515b | 7843 | static struct sched_group *find_busiest_group(struct lb_env *env) |
1e3c88bd | 7844 | { |
56cf515b | 7845 | struct sg_lb_stats *local, *busiest; |
1e3c88bd PZ |
7846 | struct sd_lb_stats sds; |
7847 | ||
147c5fc2 | 7848 | init_sd_lb_stats(&sds); |
1e3c88bd PZ |
7849 | |
7850 | /* | |
7851 | * Compute the various statistics relavent for load balancing at | |
7852 | * this level. | |
7853 | */ | |
23f0d209 | 7854 | update_sd_lb_stats(env, &sds); |
56cf515b JK |
7855 | local = &sds.local_stat; |
7856 | busiest = &sds.busiest_stat; | |
1e3c88bd | 7857 | |
ea67821b | 7858 | /* ASYM feature bypasses nice load balance check */ |
1f621e02 | 7859 | if (check_asym_packing(env, &sds)) |
532cb4c4 MN |
7860 | return sds.busiest; |
7861 | ||
cc57aa8f | 7862 | /* There is no busy sibling group to pull tasks from */ |
56cf515b | 7863 | if (!sds.busiest || busiest->sum_nr_running == 0) |
1e3c88bd PZ |
7864 | goto out_balanced; |
7865 | ||
90001d67 | 7866 | /* XXX broken for overlapping NUMA groups */ |
ca8ce3d0 NP |
7867 | sds.avg_load = (SCHED_CAPACITY_SCALE * sds.total_load) |
7868 | / sds.total_capacity; | |
b0432d8f | 7869 | |
866ab43e PZ |
7870 | /* |
7871 | * If the busiest group is imbalanced the below checks don't | |
30ce5dab | 7872 | * work because they assume all things are equal, which typically |
866ab43e PZ |
7873 | * isn't true due to cpus_allowed constraints and the like. |
7874 | */ | |
caeb178c | 7875 | if (busiest->group_type == group_imbalanced) |
866ab43e PZ |
7876 | goto force_balance; |
7877 | ||
cc57aa8f | 7878 | /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */ |
ea67821b VG |
7879 | if (env->idle == CPU_NEWLY_IDLE && group_has_capacity(env, local) && |
7880 | busiest->group_no_capacity) | |
fab47622 NR |
7881 | goto force_balance; |
7882 | ||
cc57aa8f | 7883 | /* |
9c58c79a | 7884 | * If the local group is busier than the selected busiest group |
cc57aa8f PZ |
7885 | * don't try and pull any tasks. |
7886 | */ | |
56cf515b | 7887 | if (local->avg_load >= busiest->avg_load) |
1e3c88bd PZ |
7888 | goto out_balanced; |
7889 | ||
cc57aa8f PZ |
7890 | /* |
7891 | * Don't pull any tasks if this group is already above the domain | |
7892 | * average load. | |
7893 | */ | |
56cf515b | 7894 | if (local->avg_load >= sds.avg_load) |
1e3c88bd PZ |
7895 | goto out_balanced; |
7896 | ||
bd939f45 | 7897 | if (env->idle == CPU_IDLE) { |
aae6d3dd | 7898 | /* |
43f4d666 VG |
7899 | * This cpu is idle. If the busiest group is not overloaded |
7900 | * and there is no imbalance between this and busiest group | |
7901 | * wrt idle cpus, it is balanced. The imbalance becomes | |
7902 | * significant if the diff is greater than 1 otherwise we | |
7903 | * might end up to just move the imbalance on another group | |
aae6d3dd | 7904 | */ |
43f4d666 VG |
7905 | if ((busiest->group_type != group_overloaded) && |
7906 | (local->idle_cpus <= (busiest->idle_cpus + 1))) | |
aae6d3dd | 7907 | goto out_balanced; |
c186fafe PZ |
7908 | } else { |
7909 | /* | |
7910 | * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use | |
7911 | * imbalance_pct to be conservative. | |
7912 | */ | |
56cf515b JK |
7913 | if (100 * busiest->avg_load <= |
7914 | env->sd->imbalance_pct * local->avg_load) | |
c186fafe | 7915 | goto out_balanced; |
aae6d3dd | 7916 | } |
1e3c88bd | 7917 | |
fab47622 | 7918 | force_balance: |
1e3c88bd | 7919 | /* Looks like there is an imbalance. Compute it */ |
bd939f45 | 7920 | calculate_imbalance(env, &sds); |
1e3c88bd PZ |
7921 | return sds.busiest; |
7922 | ||
7923 | out_balanced: | |
bd939f45 | 7924 | env->imbalance = 0; |
1e3c88bd PZ |
7925 | return NULL; |
7926 | } | |
7927 | ||
7928 | /* | |
7929 | * find_busiest_queue - find the busiest runqueue among the cpus in group. | |
7930 | */ | |
bd939f45 | 7931 | static struct rq *find_busiest_queue(struct lb_env *env, |
b9403130 | 7932 | struct sched_group *group) |
1e3c88bd PZ |
7933 | { |
7934 | struct rq *busiest = NULL, *rq; | |
ced549fa | 7935 | unsigned long busiest_load = 0, busiest_capacity = 1; |
1e3c88bd PZ |
7936 | int i; |
7937 | ||
ae4df9d6 | 7938 | for_each_cpu_and(i, sched_group_span(group), env->cpus) { |
ea67821b | 7939 | unsigned long capacity, wl; |
0ec8aa00 PZ |
7940 | enum fbq_type rt; |
7941 | ||
7942 | rq = cpu_rq(i); | |
7943 | rt = fbq_classify_rq(rq); | |
1e3c88bd | 7944 | |
0ec8aa00 PZ |
7945 | /* |
7946 | * We classify groups/runqueues into three groups: | |
7947 | * - regular: there are !numa tasks | |
7948 | * - remote: there are numa tasks that run on the 'wrong' node | |
7949 | * - all: there is no distinction | |
7950 | * | |
7951 | * In order to avoid migrating ideally placed numa tasks, | |
7952 | * ignore those when there's better options. | |
7953 | * | |
7954 | * If we ignore the actual busiest queue to migrate another | |
7955 | * task, the next balance pass can still reduce the busiest | |
7956 | * queue by moving tasks around inside the node. | |
7957 | * | |
7958 | * If we cannot move enough load due to this classification | |
7959 | * the next pass will adjust the group classification and | |
7960 | * allow migration of more tasks. | |
7961 | * | |
7962 | * Both cases only affect the total convergence complexity. | |
7963 | */ | |
7964 | if (rt > env->fbq_type) | |
7965 | continue; | |
7966 | ||
ced549fa | 7967 | capacity = capacity_of(i); |
9d5efe05 | 7968 | |
c7132dd6 | 7969 | wl = weighted_cpuload(rq); |
1e3c88bd | 7970 | |
6e40f5bb TG |
7971 | /* |
7972 | * When comparing with imbalance, use weighted_cpuload() | |
ced549fa | 7973 | * which is not scaled with the cpu capacity. |
6e40f5bb | 7974 | */ |
ea67821b VG |
7975 | |
7976 | if (rq->nr_running == 1 && wl > env->imbalance && | |
7977 | !check_cpu_capacity(rq, env->sd)) | |
1e3c88bd PZ |
7978 | continue; |
7979 | ||
6e40f5bb TG |
7980 | /* |
7981 | * For the load comparisons with the other cpu's, consider | |
ced549fa NP |
7982 | * the weighted_cpuload() scaled with the cpu capacity, so |
7983 | * that the load can be moved away from the cpu that is | |
7984 | * potentially running at a lower capacity. | |
95a79b80 | 7985 | * |
ced549fa | 7986 | * Thus we're looking for max(wl_i / capacity_i), crosswise |
95a79b80 | 7987 | * multiplication to rid ourselves of the division works out |
ced549fa NP |
7988 | * to: wl_i * capacity_j > wl_j * capacity_i; where j is |
7989 | * our previous maximum. | |
6e40f5bb | 7990 | */ |
ced549fa | 7991 | if (wl * busiest_capacity > busiest_load * capacity) { |
95a79b80 | 7992 | busiest_load = wl; |
ced549fa | 7993 | busiest_capacity = capacity; |
1e3c88bd PZ |
7994 | busiest = rq; |
7995 | } | |
7996 | } | |
7997 | ||
7998 | return busiest; | |
7999 | } | |
8000 | ||
8001 | /* | |
8002 | * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but | |
8003 | * so long as it is large enough. | |
8004 | */ | |
8005 | #define MAX_PINNED_INTERVAL 512 | |
8006 | ||
bd939f45 | 8007 | static int need_active_balance(struct lb_env *env) |
1af3ed3d | 8008 | { |
bd939f45 PZ |
8009 | struct sched_domain *sd = env->sd; |
8010 | ||
8011 | if (env->idle == CPU_NEWLY_IDLE) { | |
532cb4c4 MN |
8012 | |
8013 | /* | |
8014 | * ASYM_PACKING needs to force migrate tasks from busy but | |
afe06efd TC |
8015 | * lower priority CPUs in order to pack all tasks in the |
8016 | * highest priority CPUs. | |
532cb4c4 | 8017 | */ |
afe06efd TC |
8018 | if ((sd->flags & SD_ASYM_PACKING) && |
8019 | sched_asym_prefer(env->dst_cpu, env->src_cpu)) | |
532cb4c4 | 8020 | return 1; |
1af3ed3d PZ |
8021 | } |
8022 | ||
1aaf90a4 VG |
8023 | /* |
8024 | * The dst_cpu is idle and the src_cpu CPU has only 1 CFS task. | |
8025 | * It's worth migrating the task if the src_cpu's capacity is reduced | |
8026 | * because of other sched_class or IRQs if more capacity stays | |
8027 | * available on dst_cpu. | |
8028 | */ | |
8029 | if ((env->idle != CPU_NOT_IDLE) && | |
8030 | (env->src_rq->cfs.h_nr_running == 1)) { | |
8031 | if ((check_cpu_capacity(env->src_rq, sd)) && | |
8032 | (capacity_of(env->src_cpu)*sd->imbalance_pct < capacity_of(env->dst_cpu)*100)) | |
8033 | return 1; | |
8034 | } | |
8035 | ||
1af3ed3d PZ |
8036 | return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2); |
8037 | } | |
8038 | ||
969c7921 TH |
8039 | static int active_load_balance_cpu_stop(void *data); |
8040 | ||
23f0d209 JK |
8041 | static int should_we_balance(struct lb_env *env) |
8042 | { | |
8043 | struct sched_group *sg = env->sd->groups; | |
23f0d209 JK |
8044 | int cpu, balance_cpu = -1; |
8045 | ||
024c9d2f PZ |
8046 | /* |
8047 | * Ensure the balancing environment is consistent; can happen | |
8048 | * when the softirq triggers 'during' hotplug. | |
8049 | */ | |
8050 | if (!cpumask_test_cpu(env->dst_cpu, env->cpus)) | |
8051 | return 0; | |
8052 | ||
23f0d209 JK |
8053 | /* |
8054 | * In the newly idle case, we will allow all the cpu's | |
8055 | * to do the newly idle load balance. | |
8056 | */ | |
8057 | if (env->idle == CPU_NEWLY_IDLE) | |
8058 | return 1; | |
8059 | ||
23f0d209 | 8060 | /* Try to find first idle cpu */ |
e5c14b1f | 8061 | for_each_cpu_and(cpu, group_balance_mask(sg), env->cpus) { |
af218122 | 8062 | if (!idle_cpu(cpu)) |
23f0d209 JK |
8063 | continue; |
8064 | ||
8065 | balance_cpu = cpu; | |
8066 | break; | |
8067 | } | |
8068 | ||
8069 | if (balance_cpu == -1) | |
8070 | balance_cpu = group_balance_cpu(sg); | |
8071 | ||
8072 | /* | |
8073 | * First idle cpu or the first cpu(busiest) in this sched group | |
8074 | * is eligible for doing load balancing at this and above domains. | |
8075 | */ | |
b0cff9d8 | 8076 | return balance_cpu == env->dst_cpu; |
23f0d209 JK |
8077 | } |
8078 | ||
1e3c88bd PZ |
8079 | /* |
8080 | * Check this_cpu to ensure it is balanced within domain. Attempt to move | |
8081 | * tasks if there is an imbalance. | |
8082 | */ | |
8083 | static int load_balance(int this_cpu, struct rq *this_rq, | |
8084 | struct sched_domain *sd, enum cpu_idle_type idle, | |
23f0d209 | 8085 | int *continue_balancing) |
1e3c88bd | 8086 | { |
88b8dac0 | 8087 | int ld_moved, cur_ld_moved, active_balance = 0; |
6263322c | 8088 | struct sched_domain *sd_parent = sd->parent; |
1e3c88bd | 8089 | struct sched_group *group; |
1e3c88bd | 8090 | struct rq *busiest; |
8a8c69c3 | 8091 | struct rq_flags rf; |
4ba29684 | 8092 | struct cpumask *cpus = this_cpu_cpumask_var_ptr(load_balance_mask); |
1e3c88bd | 8093 | |
8e45cb54 PZ |
8094 | struct lb_env env = { |
8095 | .sd = sd, | |
ddcdf6e7 PZ |
8096 | .dst_cpu = this_cpu, |
8097 | .dst_rq = this_rq, | |
ae4df9d6 | 8098 | .dst_grpmask = sched_group_span(sd->groups), |
8e45cb54 | 8099 | .idle = idle, |
eb95308e | 8100 | .loop_break = sched_nr_migrate_break, |
b9403130 | 8101 | .cpus = cpus, |
0ec8aa00 | 8102 | .fbq_type = all, |
163122b7 | 8103 | .tasks = LIST_HEAD_INIT(env.tasks), |
8e45cb54 PZ |
8104 | }; |
8105 | ||
65a4433a | 8106 | cpumask_and(cpus, sched_domain_span(sd), cpu_active_mask); |
1e3c88bd | 8107 | |
ae92882e | 8108 | schedstat_inc(sd->lb_count[idle]); |
1e3c88bd PZ |
8109 | |
8110 | redo: | |
23f0d209 JK |
8111 | if (!should_we_balance(&env)) { |
8112 | *continue_balancing = 0; | |
1e3c88bd | 8113 | goto out_balanced; |
23f0d209 | 8114 | } |
1e3c88bd | 8115 | |
23f0d209 | 8116 | group = find_busiest_group(&env); |
1e3c88bd | 8117 | if (!group) { |
ae92882e | 8118 | schedstat_inc(sd->lb_nobusyg[idle]); |
1e3c88bd PZ |
8119 | goto out_balanced; |
8120 | } | |
8121 | ||
b9403130 | 8122 | busiest = find_busiest_queue(&env, group); |
1e3c88bd | 8123 | if (!busiest) { |
ae92882e | 8124 | schedstat_inc(sd->lb_nobusyq[idle]); |
1e3c88bd PZ |
8125 | goto out_balanced; |
8126 | } | |
8127 | ||
78feefc5 | 8128 | BUG_ON(busiest == env.dst_rq); |
1e3c88bd | 8129 | |
ae92882e | 8130 | schedstat_add(sd->lb_imbalance[idle], env.imbalance); |
1e3c88bd | 8131 | |
1aaf90a4 VG |
8132 | env.src_cpu = busiest->cpu; |
8133 | env.src_rq = busiest; | |
8134 | ||
1e3c88bd PZ |
8135 | ld_moved = 0; |
8136 | if (busiest->nr_running > 1) { | |
8137 | /* | |
8138 | * Attempt to move tasks. If find_busiest_group has found | |
8139 | * an imbalance but busiest->nr_running <= 1, the group is | |
8140 | * still unbalanced. ld_moved simply stays zero, so it is | |
8141 | * correctly treated as an imbalance. | |
8142 | */ | |
8e45cb54 | 8143 | env.flags |= LBF_ALL_PINNED; |
c82513e5 | 8144 | env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running); |
8e45cb54 | 8145 | |
5d6523eb | 8146 | more_balance: |
8a8c69c3 | 8147 | rq_lock_irqsave(busiest, &rf); |
3bed5e21 | 8148 | update_rq_clock(busiest); |
88b8dac0 SV |
8149 | |
8150 | /* | |
8151 | * cur_ld_moved - load moved in current iteration | |
8152 | * ld_moved - cumulative load moved across iterations | |
8153 | */ | |
163122b7 | 8154 | cur_ld_moved = detach_tasks(&env); |
1e3c88bd PZ |
8155 | |
8156 | /* | |
163122b7 KT |
8157 | * We've detached some tasks from busiest_rq. Every |
8158 | * task is masked "TASK_ON_RQ_MIGRATING", so we can safely | |
8159 | * unlock busiest->lock, and we are able to be sure | |
8160 | * that nobody can manipulate the tasks in parallel. | |
8161 | * See task_rq_lock() family for the details. | |
1e3c88bd | 8162 | */ |
163122b7 | 8163 | |
8a8c69c3 | 8164 | rq_unlock(busiest, &rf); |
163122b7 KT |
8165 | |
8166 | if (cur_ld_moved) { | |
8167 | attach_tasks(&env); | |
8168 | ld_moved += cur_ld_moved; | |
8169 | } | |
8170 | ||
8a8c69c3 | 8171 | local_irq_restore(rf.flags); |
88b8dac0 | 8172 | |
f1cd0858 JK |
8173 | if (env.flags & LBF_NEED_BREAK) { |
8174 | env.flags &= ~LBF_NEED_BREAK; | |
8175 | goto more_balance; | |
8176 | } | |
8177 | ||
88b8dac0 SV |
8178 | /* |
8179 | * Revisit (affine) tasks on src_cpu that couldn't be moved to | |
8180 | * us and move them to an alternate dst_cpu in our sched_group | |
8181 | * where they can run. The upper limit on how many times we | |
8182 | * iterate on same src_cpu is dependent on number of cpus in our | |
8183 | * sched_group. | |
8184 | * | |
8185 | * This changes load balance semantics a bit on who can move | |
8186 | * load to a given_cpu. In addition to the given_cpu itself | |
8187 | * (or a ilb_cpu acting on its behalf where given_cpu is | |
8188 | * nohz-idle), we now have balance_cpu in a position to move | |
8189 | * load to given_cpu. In rare situations, this may cause | |
8190 | * conflicts (balance_cpu and given_cpu/ilb_cpu deciding | |
8191 | * _independently_ and at _same_ time to move some load to | |
8192 | * given_cpu) causing exceess load to be moved to given_cpu. | |
8193 | * This however should not happen so much in practice and | |
8194 | * moreover subsequent load balance cycles should correct the | |
8195 | * excess load moved. | |
8196 | */ | |
6263322c | 8197 | if ((env.flags & LBF_DST_PINNED) && env.imbalance > 0) { |
88b8dac0 | 8198 | |
7aff2e3a VD |
8199 | /* Prevent to re-select dst_cpu via env's cpus */ |
8200 | cpumask_clear_cpu(env.dst_cpu, env.cpus); | |
8201 | ||
78feefc5 | 8202 | env.dst_rq = cpu_rq(env.new_dst_cpu); |
88b8dac0 | 8203 | env.dst_cpu = env.new_dst_cpu; |
6263322c | 8204 | env.flags &= ~LBF_DST_PINNED; |
88b8dac0 SV |
8205 | env.loop = 0; |
8206 | env.loop_break = sched_nr_migrate_break; | |
e02e60c1 | 8207 | |
88b8dac0 SV |
8208 | /* |
8209 | * Go back to "more_balance" rather than "redo" since we | |
8210 | * need to continue with same src_cpu. | |
8211 | */ | |
8212 | goto more_balance; | |
8213 | } | |
1e3c88bd | 8214 | |
6263322c PZ |
8215 | /* |
8216 | * We failed to reach balance because of affinity. | |
8217 | */ | |
8218 | if (sd_parent) { | |
63b2ca30 | 8219 | int *group_imbalance = &sd_parent->groups->sgc->imbalance; |
6263322c | 8220 | |
afdeee05 | 8221 | if ((env.flags & LBF_SOME_PINNED) && env.imbalance > 0) |
6263322c | 8222 | *group_imbalance = 1; |
6263322c PZ |
8223 | } |
8224 | ||
1e3c88bd | 8225 | /* All tasks on this runqueue were pinned by CPU affinity */ |
8e45cb54 | 8226 | if (unlikely(env.flags & LBF_ALL_PINNED)) { |
1e3c88bd | 8227 | cpumask_clear_cpu(cpu_of(busiest), cpus); |
65a4433a JH |
8228 | /* |
8229 | * Attempting to continue load balancing at the current | |
8230 | * sched_domain level only makes sense if there are | |
8231 | * active CPUs remaining as possible busiest CPUs to | |
8232 | * pull load from which are not contained within the | |
8233 | * destination group that is receiving any migrated | |
8234 | * load. | |
8235 | */ | |
8236 | if (!cpumask_subset(cpus, env.dst_grpmask)) { | |
bbf18b19 PN |
8237 | env.loop = 0; |
8238 | env.loop_break = sched_nr_migrate_break; | |
1e3c88bd | 8239 | goto redo; |
bbf18b19 | 8240 | } |
afdeee05 | 8241 | goto out_all_pinned; |
1e3c88bd PZ |
8242 | } |
8243 | } | |
8244 | ||
8245 | if (!ld_moved) { | |
ae92882e | 8246 | schedstat_inc(sd->lb_failed[idle]); |
58b26c4c VP |
8247 | /* |
8248 | * Increment the failure counter only on periodic balance. | |
8249 | * We do not want newidle balance, which can be very | |
8250 | * frequent, pollute the failure counter causing | |
8251 | * excessive cache_hot migrations and active balances. | |
8252 | */ | |
8253 | if (idle != CPU_NEWLY_IDLE) | |
8254 | sd->nr_balance_failed++; | |
1e3c88bd | 8255 | |
bd939f45 | 8256 | if (need_active_balance(&env)) { |
8a8c69c3 PZ |
8257 | unsigned long flags; |
8258 | ||
1e3c88bd PZ |
8259 | raw_spin_lock_irqsave(&busiest->lock, flags); |
8260 | ||
969c7921 TH |
8261 | /* don't kick the active_load_balance_cpu_stop, |
8262 | * if the curr task on busiest cpu can't be | |
8263 | * moved to this_cpu | |
1e3c88bd | 8264 | */ |
0c98d344 | 8265 | if (!cpumask_test_cpu(this_cpu, &busiest->curr->cpus_allowed)) { |
1e3c88bd PZ |
8266 | raw_spin_unlock_irqrestore(&busiest->lock, |
8267 | flags); | |
8e45cb54 | 8268 | env.flags |= LBF_ALL_PINNED; |
1e3c88bd PZ |
8269 | goto out_one_pinned; |
8270 | } | |
8271 | ||
969c7921 TH |
8272 | /* |
8273 | * ->active_balance synchronizes accesses to | |
8274 | * ->active_balance_work. Once set, it's cleared | |
8275 | * only after active load balance is finished. | |
8276 | */ | |
1e3c88bd PZ |
8277 | if (!busiest->active_balance) { |
8278 | busiest->active_balance = 1; | |
8279 | busiest->push_cpu = this_cpu; | |
8280 | active_balance = 1; | |
8281 | } | |
8282 | raw_spin_unlock_irqrestore(&busiest->lock, flags); | |
969c7921 | 8283 | |
bd939f45 | 8284 | if (active_balance) { |
969c7921 TH |
8285 | stop_one_cpu_nowait(cpu_of(busiest), |
8286 | active_load_balance_cpu_stop, busiest, | |
8287 | &busiest->active_balance_work); | |
bd939f45 | 8288 | } |
1e3c88bd | 8289 | |
d02c0711 | 8290 | /* We've kicked active balancing, force task migration. */ |
1e3c88bd PZ |
8291 | sd->nr_balance_failed = sd->cache_nice_tries+1; |
8292 | } | |
8293 | } else | |
8294 | sd->nr_balance_failed = 0; | |
8295 | ||
8296 | if (likely(!active_balance)) { | |
8297 | /* We were unbalanced, so reset the balancing interval */ | |
8298 | sd->balance_interval = sd->min_interval; | |
8299 | } else { | |
8300 | /* | |
8301 | * If we've begun active balancing, start to back off. This | |
8302 | * case may not be covered by the all_pinned logic if there | |
8303 | * is only 1 task on the busy runqueue (because we don't call | |
163122b7 | 8304 | * detach_tasks). |
1e3c88bd PZ |
8305 | */ |
8306 | if (sd->balance_interval < sd->max_interval) | |
8307 | sd->balance_interval *= 2; | |
8308 | } | |
8309 | ||
1e3c88bd PZ |
8310 | goto out; |
8311 | ||
8312 | out_balanced: | |
afdeee05 VG |
8313 | /* |
8314 | * We reach balance although we may have faced some affinity | |
8315 | * constraints. Clear the imbalance flag if it was set. | |
8316 | */ | |
8317 | if (sd_parent) { | |
8318 | int *group_imbalance = &sd_parent->groups->sgc->imbalance; | |
8319 | ||
8320 | if (*group_imbalance) | |
8321 | *group_imbalance = 0; | |
8322 | } | |
8323 | ||
8324 | out_all_pinned: | |
8325 | /* | |
8326 | * We reach balance because all tasks are pinned at this level so | |
8327 | * we can't migrate them. Let the imbalance flag set so parent level | |
8328 | * can try to migrate them. | |
8329 | */ | |
ae92882e | 8330 | schedstat_inc(sd->lb_balanced[idle]); |
1e3c88bd PZ |
8331 | |
8332 | sd->nr_balance_failed = 0; | |
8333 | ||
8334 | out_one_pinned: | |
8335 | /* tune up the balancing interval */ | |
8e45cb54 | 8336 | if (((env.flags & LBF_ALL_PINNED) && |
5b54b56b | 8337 | sd->balance_interval < MAX_PINNED_INTERVAL) || |
1e3c88bd PZ |
8338 | (sd->balance_interval < sd->max_interval)) |
8339 | sd->balance_interval *= 2; | |
8340 | ||
46e49b38 | 8341 | ld_moved = 0; |
1e3c88bd | 8342 | out: |
1e3c88bd PZ |
8343 | return ld_moved; |
8344 | } | |
8345 | ||
52a08ef1 JL |
8346 | static inline unsigned long |
8347 | get_sd_balance_interval(struct sched_domain *sd, int cpu_busy) | |
8348 | { | |
8349 | unsigned long interval = sd->balance_interval; | |
8350 | ||
8351 | if (cpu_busy) | |
8352 | interval *= sd->busy_factor; | |
8353 | ||
8354 | /* scale ms to jiffies */ | |
8355 | interval = msecs_to_jiffies(interval); | |
8356 | interval = clamp(interval, 1UL, max_load_balance_interval); | |
8357 | ||
8358 | return interval; | |
8359 | } | |
8360 | ||
8361 | static inline void | |
31851a98 | 8362 | update_next_balance(struct sched_domain *sd, unsigned long *next_balance) |
52a08ef1 JL |
8363 | { |
8364 | unsigned long interval, next; | |
8365 | ||
31851a98 LY |
8366 | /* used by idle balance, so cpu_busy = 0 */ |
8367 | interval = get_sd_balance_interval(sd, 0); | |
52a08ef1 JL |
8368 | next = sd->last_balance + interval; |
8369 | ||
8370 | if (time_after(*next_balance, next)) | |
8371 | *next_balance = next; | |
8372 | } | |
8373 | ||
1e3c88bd PZ |
8374 | /* |
8375 | * idle_balance is called by schedule() if this_cpu is about to become | |
8376 | * idle. Attempts to pull tasks from other CPUs. | |
8377 | */ | |
46f69fa3 | 8378 | static int idle_balance(struct rq *this_rq, struct rq_flags *rf) |
1e3c88bd | 8379 | { |
52a08ef1 JL |
8380 | unsigned long next_balance = jiffies + HZ; |
8381 | int this_cpu = this_rq->cpu; | |
1e3c88bd PZ |
8382 | struct sched_domain *sd; |
8383 | int pulled_task = 0; | |
9bd721c5 | 8384 | u64 curr_cost = 0; |
1e3c88bd | 8385 | |
6e83125c PZ |
8386 | /* |
8387 | * We must set idle_stamp _before_ calling idle_balance(), such that we | |
8388 | * measure the duration of idle_balance() as idle time. | |
8389 | */ | |
8390 | this_rq->idle_stamp = rq_clock(this_rq); | |
8391 | ||
2800486e PZ |
8392 | /* |
8393 | * Do not pull tasks towards !active CPUs... | |
8394 | */ | |
8395 | if (!cpu_active(this_cpu)) | |
8396 | return 0; | |
8397 | ||
46f69fa3 MF |
8398 | /* |
8399 | * This is OK, because current is on_cpu, which avoids it being picked | |
8400 | * for load-balance and preemption/IRQs are still disabled avoiding | |
8401 | * further scheduler activity on it and we're being very careful to | |
8402 | * re-start the picking loop. | |
8403 | */ | |
8404 | rq_unpin_lock(this_rq, rf); | |
8405 | ||
4486edd1 TC |
8406 | if (this_rq->avg_idle < sysctl_sched_migration_cost || |
8407 | !this_rq->rd->overload) { | |
52a08ef1 JL |
8408 | rcu_read_lock(); |
8409 | sd = rcu_dereference_check_sched_domain(this_rq->sd); | |
8410 | if (sd) | |
31851a98 | 8411 | update_next_balance(sd, &next_balance); |
52a08ef1 JL |
8412 | rcu_read_unlock(); |
8413 | ||
6e83125c | 8414 | goto out; |
52a08ef1 | 8415 | } |
1e3c88bd | 8416 | |
f492e12e PZ |
8417 | raw_spin_unlock(&this_rq->lock); |
8418 | ||
48a16753 | 8419 | update_blocked_averages(this_cpu); |
dce840a0 | 8420 | rcu_read_lock(); |
1e3c88bd | 8421 | for_each_domain(this_cpu, sd) { |
23f0d209 | 8422 | int continue_balancing = 1; |
9bd721c5 | 8423 | u64 t0, domain_cost; |
1e3c88bd PZ |
8424 | |
8425 | if (!(sd->flags & SD_LOAD_BALANCE)) | |
8426 | continue; | |
8427 | ||
52a08ef1 | 8428 | if (this_rq->avg_idle < curr_cost + sd->max_newidle_lb_cost) { |
31851a98 | 8429 | update_next_balance(sd, &next_balance); |
9bd721c5 | 8430 | break; |
52a08ef1 | 8431 | } |
9bd721c5 | 8432 | |
f492e12e | 8433 | if (sd->flags & SD_BALANCE_NEWIDLE) { |
9bd721c5 JL |
8434 | t0 = sched_clock_cpu(this_cpu); |
8435 | ||
f492e12e | 8436 | pulled_task = load_balance(this_cpu, this_rq, |
23f0d209 JK |
8437 | sd, CPU_NEWLY_IDLE, |
8438 | &continue_balancing); | |
9bd721c5 JL |
8439 | |
8440 | domain_cost = sched_clock_cpu(this_cpu) - t0; | |
8441 | if (domain_cost > sd->max_newidle_lb_cost) | |
8442 | sd->max_newidle_lb_cost = domain_cost; | |
8443 | ||
8444 | curr_cost += domain_cost; | |
f492e12e | 8445 | } |
1e3c88bd | 8446 | |
31851a98 | 8447 | update_next_balance(sd, &next_balance); |
39a4d9ca JL |
8448 | |
8449 | /* | |
8450 | * Stop searching for tasks to pull if there are | |
8451 | * now runnable tasks on this rq. | |
8452 | */ | |
8453 | if (pulled_task || this_rq->nr_running > 0) | |
1e3c88bd | 8454 | break; |
1e3c88bd | 8455 | } |
dce840a0 | 8456 | rcu_read_unlock(); |
f492e12e PZ |
8457 | |
8458 | raw_spin_lock(&this_rq->lock); | |
8459 | ||
0e5b5337 JL |
8460 | if (curr_cost > this_rq->max_idle_balance_cost) |
8461 | this_rq->max_idle_balance_cost = curr_cost; | |
8462 | ||
e5fc6611 | 8463 | /* |
0e5b5337 JL |
8464 | * While browsing the domains, we released the rq lock, a task could |
8465 | * have been enqueued in the meantime. Since we're not going idle, | |
8466 | * pretend we pulled a task. | |
e5fc6611 | 8467 | */ |
0e5b5337 | 8468 | if (this_rq->cfs.h_nr_running && !pulled_task) |
6e83125c | 8469 | pulled_task = 1; |
e5fc6611 | 8470 | |
52a08ef1 JL |
8471 | out: |
8472 | /* Move the next balance forward */ | |
8473 | if (time_after(this_rq->next_balance, next_balance)) | |
1e3c88bd | 8474 | this_rq->next_balance = next_balance; |
9bd721c5 | 8475 | |
e4aa358b | 8476 | /* Is there a task of a high priority class? */ |
46383648 | 8477 | if (this_rq->nr_running != this_rq->cfs.h_nr_running) |
e4aa358b KT |
8478 | pulled_task = -1; |
8479 | ||
38c6ade2 | 8480 | if (pulled_task) |
6e83125c PZ |
8481 | this_rq->idle_stamp = 0; |
8482 | ||
46f69fa3 MF |
8483 | rq_repin_lock(this_rq, rf); |
8484 | ||
3c4017c1 | 8485 | return pulled_task; |
1e3c88bd PZ |
8486 | } |
8487 | ||
8488 | /* | |
969c7921 TH |
8489 | * active_load_balance_cpu_stop is run by cpu stopper. It pushes |
8490 | * running tasks off the busiest CPU onto idle CPUs. It requires at | |
8491 | * least 1 task to be running on each physical CPU where possible, and | |
8492 | * avoids physical / logical imbalances. | |
1e3c88bd | 8493 | */ |
969c7921 | 8494 | static int active_load_balance_cpu_stop(void *data) |
1e3c88bd | 8495 | { |
969c7921 TH |
8496 | struct rq *busiest_rq = data; |
8497 | int busiest_cpu = cpu_of(busiest_rq); | |
1e3c88bd | 8498 | int target_cpu = busiest_rq->push_cpu; |
969c7921 | 8499 | struct rq *target_rq = cpu_rq(target_cpu); |
1e3c88bd | 8500 | struct sched_domain *sd; |
e5673f28 | 8501 | struct task_struct *p = NULL; |
8a8c69c3 | 8502 | struct rq_flags rf; |
969c7921 | 8503 | |
8a8c69c3 | 8504 | rq_lock_irq(busiest_rq, &rf); |
edd8e41d PZ |
8505 | /* |
8506 | * Between queueing the stop-work and running it is a hole in which | |
8507 | * CPUs can become inactive. We should not move tasks from or to | |
8508 | * inactive CPUs. | |
8509 | */ | |
8510 | if (!cpu_active(busiest_cpu) || !cpu_active(target_cpu)) | |
8511 | goto out_unlock; | |
969c7921 TH |
8512 | |
8513 | /* make sure the requested cpu hasn't gone down in the meantime */ | |
8514 | if (unlikely(busiest_cpu != smp_processor_id() || | |
8515 | !busiest_rq->active_balance)) | |
8516 | goto out_unlock; | |
1e3c88bd PZ |
8517 | |
8518 | /* Is there any task to move? */ | |
8519 | if (busiest_rq->nr_running <= 1) | |
969c7921 | 8520 | goto out_unlock; |
1e3c88bd PZ |
8521 | |
8522 | /* | |
8523 | * This condition is "impossible", if it occurs | |
8524 | * we need to fix it. Originally reported by | |
8525 | * Bjorn Helgaas on a 128-cpu setup. | |
8526 | */ | |
8527 | BUG_ON(busiest_rq == target_rq); | |
8528 | ||
1e3c88bd | 8529 | /* Search for an sd spanning us and the target CPU. */ |
dce840a0 | 8530 | rcu_read_lock(); |
1e3c88bd PZ |
8531 | for_each_domain(target_cpu, sd) { |
8532 | if ((sd->flags & SD_LOAD_BALANCE) && | |
8533 | cpumask_test_cpu(busiest_cpu, sched_domain_span(sd))) | |
8534 | break; | |
8535 | } | |
8536 | ||
8537 | if (likely(sd)) { | |
8e45cb54 PZ |
8538 | struct lb_env env = { |
8539 | .sd = sd, | |
ddcdf6e7 PZ |
8540 | .dst_cpu = target_cpu, |
8541 | .dst_rq = target_rq, | |
8542 | .src_cpu = busiest_rq->cpu, | |
8543 | .src_rq = busiest_rq, | |
8e45cb54 | 8544 | .idle = CPU_IDLE, |
65a4433a JH |
8545 | /* |
8546 | * can_migrate_task() doesn't need to compute new_dst_cpu | |
8547 | * for active balancing. Since we have CPU_IDLE, but no | |
8548 | * @dst_grpmask we need to make that test go away with lying | |
8549 | * about DST_PINNED. | |
8550 | */ | |
8551 | .flags = LBF_DST_PINNED, | |
8e45cb54 PZ |
8552 | }; |
8553 | ||
ae92882e | 8554 | schedstat_inc(sd->alb_count); |
3bed5e21 | 8555 | update_rq_clock(busiest_rq); |
1e3c88bd | 8556 | |
e5673f28 | 8557 | p = detach_one_task(&env); |
d02c0711 | 8558 | if (p) { |
ae92882e | 8559 | schedstat_inc(sd->alb_pushed); |
d02c0711 SD |
8560 | /* Active balancing done, reset the failure counter. */ |
8561 | sd->nr_balance_failed = 0; | |
8562 | } else { | |
ae92882e | 8563 | schedstat_inc(sd->alb_failed); |
d02c0711 | 8564 | } |
1e3c88bd | 8565 | } |
dce840a0 | 8566 | rcu_read_unlock(); |
969c7921 TH |
8567 | out_unlock: |
8568 | busiest_rq->active_balance = 0; | |
8a8c69c3 | 8569 | rq_unlock(busiest_rq, &rf); |
e5673f28 KT |
8570 | |
8571 | if (p) | |
8572 | attach_one_task(target_rq, p); | |
8573 | ||
8574 | local_irq_enable(); | |
8575 | ||
969c7921 | 8576 | return 0; |
1e3c88bd PZ |
8577 | } |
8578 | ||
d987fc7f MG |
8579 | static inline int on_null_domain(struct rq *rq) |
8580 | { | |
8581 | return unlikely(!rcu_dereference_sched(rq->sd)); | |
8582 | } | |
8583 | ||
3451d024 | 8584 | #ifdef CONFIG_NO_HZ_COMMON |
83cd4fe2 VP |
8585 | /* |
8586 | * idle load balancing details | |
83cd4fe2 VP |
8587 | * - When one of the busy CPUs notice that there may be an idle rebalancing |
8588 | * needed, they will kick the idle load balancer, which then does idle | |
8589 | * load balancing for all the idle CPUs. | |
8590 | */ | |
1e3c88bd | 8591 | static struct { |
83cd4fe2 | 8592 | cpumask_var_t idle_cpus_mask; |
0b005cf5 | 8593 | atomic_t nr_cpus; |
83cd4fe2 VP |
8594 | unsigned long next_balance; /* in jiffy units */ |
8595 | } nohz ____cacheline_aligned; | |
1e3c88bd | 8596 | |
3dd0337d | 8597 | static inline int find_new_ilb(void) |
1e3c88bd | 8598 | { |
0b005cf5 | 8599 | int ilb = cpumask_first(nohz.idle_cpus_mask); |
1e3c88bd | 8600 | |
786d6dc7 SS |
8601 | if (ilb < nr_cpu_ids && idle_cpu(ilb)) |
8602 | return ilb; | |
8603 | ||
8604 | return nr_cpu_ids; | |
1e3c88bd | 8605 | } |
1e3c88bd | 8606 | |
83cd4fe2 VP |
8607 | /* |
8608 | * Kick a CPU to do the nohz balancing, if it is time for it. We pick the | |
8609 | * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle | |
8610 | * CPU (if there is one). | |
8611 | */ | |
0aeeeeba | 8612 | static void nohz_balancer_kick(void) |
83cd4fe2 VP |
8613 | { |
8614 | int ilb_cpu; | |
8615 | ||
8616 | nohz.next_balance++; | |
8617 | ||
3dd0337d | 8618 | ilb_cpu = find_new_ilb(); |
83cd4fe2 | 8619 | |
0b005cf5 SS |
8620 | if (ilb_cpu >= nr_cpu_ids) |
8621 | return; | |
83cd4fe2 | 8622 | |
cd490c5b | 8623 | if (test_and_set_bit(NOHZ_BALANCE_KICK, nohz_flags(ilb_cpu))) |
1c792db7 SS |
8624 | return; |
8625 | /* | |
8626 | * Use smp_send_reschedule() instead of resched_cpu(). | |
8627 | * This way we generate a sched IPI on the target cpu which | |
8628 | * is idle. And the softirq performing nohz idle load balance | |
8629 | * will be run before returning from the IPI. | |
8630 | */ | |
8631 | smp_send_reschedule(ilb_cpu); | |
83cd4fe2 VP |
8632 | return; |
8633 | } | |
8634 | ||
20a5c8cc | 8635 | void nohz_balance_exit_idle(unsigned int cpu) |
71325960 SS |
8636 | { |
8637 | if (unlikely(test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))) { | |
d987fc7f MG |
8638 | /* |
8639 | * Completely isolated CPUs don't ever set, so we must test. | |
8640 | */ | |
8641 | if (likely(cpumask_test_cpu(cpu, nohz.idle_cpus_mask))) { | |
8642 | cpumask_clear_cpu(cpu, nohz.idle_cpus_mask); | |
8643 | atomic_dec(&nohz.nr_cpus); | |
8644 | } | |
71325960 SS |
8645 | clear_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)); |
8646 | } | |
8647 | } | |
8648 | ||
69e1e811 SS |
8649 | static inline void set_cpu_sd_state_busy(void) |
8650 | { | |
8651 | struct sched_domain *sd; | |
37dc6b50 | 8652 | int cpu = smp_processor_id(); |
69e1e811 | 8653 | |
69e1e811 | 8654 | rcu_read_lock(); |
0e369d75 | 8655 | sd = rcu_dereference(per_cpu(sd_llc, cpu)); |
25f55d9d VG |
8656 | |
8657 | if (!sd || !sd->nohz_idle) | |
8658 | goto unlock; | |
8659 | sd->nohz_idle = 0; | |
8660 | ||
0e369d75 | 8661 | atomic_inc(&sd->shared->nr_busy_cpus); |
25f55d9d | 8662 | unlock: |
69e1e811 SS |
8663 | rcu_read_unlock(); |
8664 | } | |
8665 | ||
8666 | void set_cpu_sd_state_idle(void) | |
8667 | { | |
8668 | struct sched_domain *sd; | |
37dc6b50 | 8669 | int cpu = smp_processor_id(); |
69e1e811 | 8670 | |
69e1e811 | 8671 | rcu_read_lock(); |
0e369d75 | 8672 | sd = rcu_dereference(per_cpu(sd_llc, cpu)); |
25f55d9d VG |
8673 | |
8674 | if (!sd || sd->nohz_idle) | |
8675 | goto unlock; | |
8676 | sd->nohz_idle = 1; | |
8677 | ||
0e369d75 | 8678 | atomic_dec(&sd->shared->nr_busy_cpus); |
25f55d9d | 8679 | unlock: |
69e1e811 SS |
8680 | rcu_read_unlock(); |
8681 | } | |
8682 | ||
1e3c88bd | 8683 | /* |
c1cc017c | 8684 | * This routine will record that the cpu is going idle with tick stopped. |
0b005cf5 | 8685 | * This info will be used in performing idle load balancing in the future. |
1e3c88bd | 8686 | */ |
c1cc017c | 8687 | void nohz_balance_enter_idle(int cpu) |
1e3c88bd | 8688 | { |
71325960 SS |
8689 | /* |
8690 | * If this cpu is going down, then nothing needs to be done. | |
8691 | */ | |
8692 | if (!cpu_active(cpu)) | |
8693 | return; | |
8694 | ||
387bc8b5 FW |
8695 | /* Spare idle load balancing on CPUs that don't want to be disturbed: */ |
8696 | if (!is_housekeeping_cpu(cpu)) | |
8697 | return; | |
8698 | ||
c1cc017c AS |
8699 | if (test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu))) |
8700 | return; | |
1e3c88bd | 8701 | |
d987fc7f MG |
8702 | /* |
8703 | * If we're a completely isolated CPU, we don't play. | |
8704 | */ | |
8705 | if (on_null_domain(cpu_rq(cpu))) | |
8706 | return; | |
8707 | ||
c1cc017c AS |
8708 | cpumask_set_cpu(cpu, nohz.idle_cpus_mask); |
8709 | atomic_inc(&nohz.nr_cpus); | |
8710 | set_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)); | |
1e3c88bd PZ |
8711 | } |
8712 | #endif | |
8713 | ||
8714 | static DEFINE_SPINLOCK(balancing); | |
8715 | ||
49c022e6 PZ |
8716 | /* |
8717 | * Scale the max load_balance interval with the number of CPUs in the system. | |
8718 | * This trades load-balance latency on larger machines for less cross talk. | |
8719 | */ | |
029632fb | 8720 | void update_max_interval(void) |
49c022e6 PZ |
8721 | { |
8722 | max_load_balance_interval = HZ*num_online_cpus()/10; | |
8723 | } | |
8724 | ||
1e3c88bd PZ |
8725 | /* |
8726 | * It checks each scheduling domain to see if it is due to be balanced, | |
8727 | * and initiates a balancing operation if so. | |
8728 | * | |
b9b0853a | 8729 | * Balancing parameters are set up in init_sched_domains. |
1e3c88bd | 8730 | */ |
f7ed0a89 | 8731 | static void rebalance_domains(struct rq *rq, enum cpu_idle_type idle) |
1e3c88bd | 8732 | { |
23f0d209 | 8733 | int continue_balancing = 1; |
f7ed0a89 | 8734 | int cpu = rq->cpu; |
1e3c88bd | 8735 | unsigned long interval; |
04f733b4 | 8736 | struct sched_domain *sd; |
1e3c88bd PZ |
8737 | /* Earliest time when we have to do rebalance again */ |
8738 | unsigned long next_balance = jiffies + 60*HZ; | |
8739 | int update_next_balance = 0; | |
f48627e6 JL |
8740 | int need_serialize, need_decay = 0; |
8741 | u64 max_cost = 0; | |
1e3c88bd | 8742 | |
48a16753 | 8743 | update_blocked_averages(cpu); |
2069dd75 | 8744 | |
dce840a0 | 8745 | rcu_read_lock(); |
1e3c88bd | 8746 | for_each_domain(cpu, sd) { |
f48627e6 JL |
8747 | /* |
8748 | * Decay the newidle max times here because this is a regular | |
8749 | * visit to all the domains. Decay ~1% per second. | |
8750 | */ | |
8751 | if (time_after(jiffies, sd->next_decay_max_lb_cost)) { | |
8752 | sd->max_newidle_lb_cost = | |
8753 | (sd->max_newidle_lb_cost * 253) / 256; | |
8754 | sd->next_decay_max_lb_cost = jiffies + HZ; | |
8755 | need_decay = 1; | |
8756 | } | |
8757 | max_cost += sd->max_newidle_lb_cost; | |
8758 | ||
1e3c88bd PZ |
8759 | if (!(sd->flags & SD_LOAD_BALANCE)) |
8760 | continue; | |
8761 | ||
f48627e6 JL |
8762 | /* |
8763 | * Stop the load balance at this level. There is another | |
8764 | * CPU in our sched group which is doing load balancing more | |
8765 | * actively. | |
8766 | */ | |
8767 | if (!continue_balancing) { | |
8768 | if (need_decay) | |
8769 | continue; | |
8770 | break; | |
8771 | } | |
8772 | ||
52a08ef1 | 8773 | interval = get_sd_balance_interval(sd, idle != CPU_IDLE); |
1e3c88bd PZ |
8774 | |
8775 | need_serialize = sd->flags & SD_SERIALIZE; | |
1e3c88bd PZ |
8776 | if (need_serialize) { |
8777 | if (!spin_trylock(&balancing)) | |
8778 | goto out; | |
8779 | } | |
8780 | ||
8781 | if (time_after_eq(jiffies, sd->last_balance + interval)) { | |
23f0d209 | 8782 | if (load_balance(cpu, rq, sd, idle, &continue_balancing)) { |
1e3c88bd | 8783 | /* |
6263322c | 8784 | * The LBF_DST_PINNED logic could have changed |
de5eb2dd JK |
8785 | * env->dst_cpu, so we can't know our idle |
8786 | * state even if we migrated tasks. Update it. | |
1e3c88bd | 8787 | */ |
de5eb2dd | 8788 | idle = idle_cpu(cpu) ? CPU_IDLE : CPU_NOT_IDLE; |
1e3c88bd PZ |
8789 | } |
8790 | sd->last_balance = jiffies; | |
52a08ef1 | 8791 | interval = get_sd_balance_interval(sd, idle != CPU_IDLE); |
1e3c88bd PZ |
8792 | } |
8793 | if (need_serialize) | |
8794 | spin_unlock(&balancing); | |
8795 | out: | |
8796 | if (time_after(next_balance, sd->last_balance + interval)) { | |
8797 | next_balance = sd->last_balance + interval; | |
8798 | update_next_balance = 1; | |
8799 | } | |
f48627e6 JL |
8800 | } |
8801 | if (need_decay) { | |
1e3c88bd | 8802 | /* |
f48627e6 JL |
8803 | * Ensure the rq-wide value also decays but keep it at a |
8804 | * reasonable floor to avoid funnies with rq->avg_idle. | |
1e3c88bd | 8805 | */ |
f48627e6 JL |
8806 | rq->max_idle_balance_cost = |
8807 | max((u64)sysctl_sched_migration_cost, max_cost); | |
1e3c88bd | 8808 | } |
dce840a0 | 8809 | rcu_read_unlock(); |
1e3c88bd PZ |
8810 | |
8811 | /* | |
8812 | * next_balance will be updated only when there is a need. | |
8813 | * When the cpu is attached to null domain for ex, it will not be | |
8814 | * updated. | |
8815 | */ | |
c5afb6a8 | 8816 | if (likely(update_next_balance)) { |
1e3c88bd | 8817 | rq->next_balance = next_balance; |
c5afb6a8 VG |
8818 | |
8819 | #ifdef CONFIG_NO_HZ_COMMON | |
8820 | /* | |
8821 | * If this CPU has been elected to perform the nohz idle | |
8822 | * balance. Other idle CPUs have already rebalanced with | |
8823 | * nohz_idle_balance() and nohz.next_balance has been | |
8824 | * updated accordingly. This CPU is now running the idle load | |
8825 | * balance for itself and we need to update the | |
8826 | * nohz.next_balance accordingly. | |
8827 | */ | |
8828 | if ((idle == CPU_IDLE) && time_after(nohz.next_balance, rq->next_balance)) | |
8829 | nohz.next_balance = rq->next_balance; | |
8830 | #endif | |
8831 | } | |
1e3c88bd PZ |
8832 | } |
8833 | ||
3451d024 | 8834 | #ifdef CONFIG_NO_HZ_COMMON |
1e3c88bd | 8835 | /* |
3451d024 | 8836 | * In CONFIG_NO_HZ_COMMON case, the idle balance kickee will do the |
1e3c88bd PZ |
8837 | * rebalancing for all the cpus for whom scheduler ticks are stopped. |
8838 | */ | |
208cb16b | 8839 | static void nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle) |
83cd4fe2 | 8840 | { |
208cb16b | 8841 | int this_cpu = this_rq->cpu; |
83cd4fe2 VP |
8842 | struct rq *rq; |
8843 | int balance_cpu; | |
c5afb6a8 VG |
8844 | /* Earliest time when we have to do rebalance again */ |
8845 | unsigned long next_balance = jiffies + 60*HZ; | |
8846 | int update_next_balance = 0; | |
83cd4fe2 | 8847 | |
1c792db7 SS |
8848 | if (idle != CPU_IDLE || |
8849 | !test_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu))) | |
8850 | goto end; | |
83cd4fe2 VP |
8851 | |
8852 | for_each_cpu(balance_cpu, nohz.idle_cpus_mask) { | |
8a6d42d1 | 8853 | if (balance_cpu == this_cpu || !idle_cpu(balance_cpu)) |
83cd4fe2 VP |
8854 | continue; |
8855 | ||
8856 | /* | |
8857 | * If this cpu gets work to do, stop the load balancing | |
8858 | * work being done for other cpus. Next load | |
8859 | * balancing owner will pick it up. | |
8860 | */ | |
1c792db7 | 8861 | if (need_resched()) |
83cd4fe2 | 8862 | break; |
83cd4fe2 | 8863 | |
5ed4f1d9 VG |
8864 | rq = cpu_rq(balance_cpu); |
8865 | ||
ed61bbc6 TC |
8866 | /* |
8867 | * If time for next balance is due, | |
8868 | * do the balance. | |
8869 | */ | |
8870 | if (time_after_eq(jiffies, rq->next_balance)) { | |
8a8c69c3 PZ |
8871 | struct rq_flags rf; |
8872 | ||
8873 | rq_lock_irq(rq, &rf); | |
ed61bbc6 | 8874 | update_rq_clock(rq); |
cee1afce | 8875 | cpu_load_update_idle(rq); |
8a8c69c3 PZ |
8876 | rq_unlock_irq(rq, &rf); |
8877 | ||
ed61bbc6 TC |
8878 | rebalance_domains(rq, CPU_IDLE); |
8879 | } | |
83cd4fe2 | 8880 | |
c5afb6a8 VG |
8881 | if (time_after(next_balance, rq->next_balance)) { |
8882 | next_balance = rq->next_balance; | |
8883 | update_next_balance = 1; | |
8884 | } | |
83cd4fe2 | 8885 | } |
c5afb6a8 VG |
8886 | |
8887 | /* | |
8888 | * next_balance will be updated only when there is a need. | |
8889 | * When the CPU is attached to null domain for ex, it will not be | |
8890 | * updated. | |
8891 | */ | |
8892 | if (likely(update_next_balance)) | |
8893 | nohz.next_balance = next_balance; | |
1c792db7 SS |
8894 | end: |
8895 | clear_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu)); | |
83cd4fe2 VP |
8896 | } |
8897 | ||
8898 | /* | |
0b005cf5 | 8899 | * Current heuristic for kicking the idle load balancer in the presence |
1aaf90a4 | 8900 | * of an idle cpu in the system. |
0b005cf5 | 8901 | * - This rq has more than one task. |
1aaf90a4 VG |
8902 | * - This rq has at least one CFS task and the capacity of the CPU is |
8903 | * significantly reduced because of RT tasks or IRQs. | |
8904 | * - At parent of LLC scheduler domain level, this cpu's scheduler group has | |
8905 | * multiple busy cpu. | |
0b005cf5 SS |
8906 | * - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler |
8907 | * domain span are idle. | |
83cd4fe2 | 8908 | */ |
1aaf90a4 | 8909 | static inline bool nohz_kick_needed(struct rq *rq) |
83cd4fe2 VP |
8910 | { |
8911 | unsigned long now = jiffies; | |
0e369d75 | 8912 | struct sched_domain_shared *sds; |
0b005cf5 | 8913 | struct sched_domain *sd; |
afe06efd | 8914 | int nr_busy, i, cpu = rq->cpu; |
1aaf90a4 | 8915 | bool kick = false; |
83cd4fe2 | 8916 | |
4a725627 | 8917 | if (unlikely(rq->idle_balance)) |
1aaf90a4 | 8918 | return false; |
83cd4fe2 | 8919 | |
1c792db7 SS |
8920 | /* |
8921 | * We may be recently in ticked or tickless idle mode. At the first | |
8922 | * busy tick after returning from idle, we will update the busy stats. | |
8923 | */ | |
69e1e811 | 8924 | set_cpu_sd_state_busy(); |
c1cc017c | 8925 | nohz_balance_exit_idle(cpu); |
0b005cf5 SS |
8926 | |
8927 | /* | |
8928 | * None are in tickless mode and hence no need for NOHZ idle load | |
8929 | * balancing. | |
8930 | */ | |
8931 | if (likely(!atomic_read(&nohz.nr_cpus))) | |
1aaf90a4 | 8932 | return false; |
1c792db7 SS |
8933 | |
8934 | if (time_before(now, nohz.next_balance)) | |
1aaf90a4 | 8935 | return false; |
83cd4fe2 | 8936 | |
0b005cf5 | 8937 | if (rq->nr_running >= 2) |
1aaf90a4 | 8938 | return true; |
83cd4fe2 | 8939 | |
067491b7 | 8940 | rcu_read_lock(); |
0e369d75 PZ |
8941 | sds = rcu_dereference(per_cpu(sd_llc_shared, cpu)); |
8942 | if (sds) { | |
8943 | /* | |
8944 | * XXX: write a coherent comment on why we do this. | |
8945 | * See also: http://lkml.kernel.org/r/20111202010832.602203411@sbsiddha-desk.sc.intel.com | |
8946 | */ | |
8947 | nr_busy = atomic_read(&sds->nr_busy_cpus); | |
1aaf90a4 VG |
8948 | if (nr_busy > 1) { |
8949 | kick = true; | |
8950 | goto unlock; | |
8951 | } | |
8952 | ||
83cd4fe2 | 8953 | } |
37dc6b50 | 8954 | |
1aaf90a4 VG |
8955 | sd = rcu_dereference(rq->sd); |
8956 | if (sd) { | |
8957 | if ((rq->cfs.h_nr_running >= 1) && | |
8958 | check_cpu_capacity(rq, sd)) { | |
8959 | kick = true; | |
8960 | goto unlock; | |
8961 | } | |
8962 | } | |
37dc6b50 | 8963 | |
1aaf90a4 | 8964 | sd = rcu_dereference(per_cpu(sd_asym, cpu)); |
afe06efd TC |
8965 | if (sd) { |
8966 | for_each_cpu(i, sched_domain_span(sd)) { | |
8967 | if (i == cpu || | |
8968 | !cpumask_test_cpu(i, nohz.idle_cpus_mask)) | |
8969 | continue; | |
067491b7 | 8970 | |
afe06efd TC |
8971 | if (sched_asym_prefer(i, cpu)) { |
8972 | kick = true; | |
8973 | goto unlock; | |
8974 | } | |
8975 | } | |
8976 | } | |
1aaf90a4 | 8977 | unlock: |
067491b7 | 8978 | rcu_read_unlock(); |
1aaf90a4 | 8979 | return kick; |
83cd4fe2 VP |
8980 | } |
8981 | #else | |
208cb16b | 8982 | static void nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle) { } |
83cd4fe2 VP |
8983 | #endif |
8984 | ||
8985 | /* | |
8986 | * run_rebalance_domains is triggered when needed from the scheduler tick. | |
8987 | * Also triggered for nohz idle balancing (with nohz_balancing_kick set). | |
8988 | */ | |
0766f788 | 8989 | static __latent_entropy void run_rebalance_domains(struct softirq_action *h) |
1e3c88bd | 8990 | { |
208cb16b | 8991 | struct rq *this_rq = this_rq(); |
6eb57e0d | 8992 | enum cpu_idle_type idle = this_rq->idle_balance ? |
1e3c88bd PZ |
8993 | CPU_IDLE : CPU_NOT_IDLE; |
8994 | ||
1e3c88bd | 8995 | /* |
83cd4fe2 | 8996 | * If this cpu has a pending nohz_balance_kick, then do the |
1e3c88bd | 8997 | * balancing on behalf of the other idle cpus whose ticks are |
d4573c3e PM |
8998 | * stopped. Do nohz_idle_balance *before* rebalance_domains to |
8999 | * give the idle cpus a chance to load balance. Else we may | |
9000 | * load balance only within the local sched_domain hierarchy | |
9001 | * and abort nohz_idle_balance altogether if we pull some load. | |
1e3c88bd | 9002 | */ |
208cb16b | 9003 | nohz_idle_balance(this_rq, idle); |
d4573c3e | 9004 | rebalance_domains(this_rq, idle); |
1e3c88bd PZ |
9005 | } |
9006 | ||
1e3c88bd PZ |
9007 | /* |
9008 | * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing. | |
1e3c88bd | 9009 | */ |
7caff66f | 9010 | void trigger_load_balance(struct rq *rq) |
1e3c88bd | 9011 | { |
1e3c88bd | 9012 | /* Don't need to rebalance while attached to NULL domain */ |
c726099e DL |
9013 | if (unlikely(on_null_domain(rq))) |
9014 | return; | |
9015 | ||
9016 | if (time_after_eq(jiffies, rq->next_balance)) | |
1e3c88bd | 9017 | raise_softirq(SCHED_SOFTIRQ); |
3451d024 | 9018 | #ifdef CONFIG_NO_HZ_COMMON |
c726099e | 9019 | if (nohz_kick_needed(rq)) |
0aeeeeba | 9020 | nohz_balancer_kick(); |
83cd4fe2 | 9021 | #endif |
1e3c88bd PZ |
9022 | } |
9023 | ||
0bcdcf28 CE |
9024 | static void rq_online_fair(struct rq *rq) |
9025 | { | |
9026 | update_sysctl(); | |
0e59bdae KT |
9027 | |
9028 | update_runtime_enabled(rq); | |
0bcdcf28 CE |
9029 | } |
9030 | ||
9031 | static void rq_offline_fair(struct rq *rq) | |
9032 | { | |
9033 | update_sysctl(); | |
a4c96ae3 PB |
9034 | |
9035 | /* Ensure any throttled groups are reachable by pick_next_task */ | |
9036 | unthrottle_offline_cfs_rqs(rq); | |
0bcdcf28 CE |
9037 | } |
9038 | ||
55e12e5e | 9039 | #endif /* CONFIG_SMP */ |
e1d1484f | 9040 | |
bf0f6f24 IM |
9041 | /* |
9042 | * scheduler tick hitting a task of our scheduling class: | |
9043 | */ | |
8f4d37ec | 9044 | static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued) |
bf0f6f24 IM |
9045 | { |
9046 | struct cfs_rq *cfs_rq; | |
9047 | struct sched_entity *se = &curr->se; | |
9048 | ||
9049 | for_each_sched_entity(se) { | |
9050 | cfs_rq = cfs_rq_of(se); | |
8f4d37ec | 9051 | entity_tick(cfs_rq, se, queued); |
bf0f6f24 | 9052 | } |
18bf2805 | 9053 | |
b52da86e | 9054 | if (static_branch_unlikely(&sched_numa_balancing)) |
cbee9f88 | 9055 | task_tick_numa(rq, curr); |
bf0f6f24 IM |
9056 | } |
9057 | ||
9058 | /* | |
cd29fe6f PZ |
9059 | * called on fork with the child task as argument from the parent's context |
9060 | * - child not yet on the tasklist | |
9061 | * - preemption disabled | |
bf0f6f24 | 9062 | */ |
cd29fe6f | 9063 | static void task_fork_fair(struct task_struct *p) |
bf0f6f24 | 9064 | { |
4fc420c9 DN |
9065 | struct cfs_rq *cfs_rq; |
9066 | struct sched_entity *se = &p->se, *curr; | |
cd29fe6f | 9067 | struct rq *rq = this_rq(); |
8a8c69c3 | 9068 | struct rq_flags rf; |
bf0f6f24 | 9069 | |
8a8c69c3 | 9070 | rq_lock(rq, &rf); |
861d034e PZ |
9071 | update_rq_clock(rq); |
9072 | ||
4fc420c9 DN |
9073 | cfs_rq = task_cfs_rq(current); |
9074 | curr = cfs_rq->curr; | |
e210bffd PZ |
9075 | if (curr) { |
9076 | update_curr(cfs_rq); | |
b5d9d734 | 9077 | se->vruntime = curr->vruntime; |
e210bffd | 9078 | } |
aeb73b04 | 9079 | place_entity(cfs_rq, se, 1); |
4d78e7b6 | 9080 | |
cd29fe6f | 9081 | if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) { |
87fefa38 | 9082 | /* |
edcb60a3 IM |
9083 | * Upon rescheduling, sched_class::put_prev_task() will place |
9084 | * 'current' within the tree based on its new key value. | |
9085 | */ | |
4d78e7b6 | 9086 | swap(curr->vruntime, se->vruntime); |
8875125e | 9087 | resched_curr(rq); |
4d78e7b6 | 9088 | } |
bf0f6f24 | 9089 | |
88ec22d3 | 9090 | se->vruntime -= cfs_rq->min_vruntime; |
8a8c69c3 | 9091 | rq_unlock(rq, &rf); |
bf0f6f24 IM |
9092 | } |
9093 | ||
cb469845 SR |
9094 | /* |
9095 | * Priority of the task has changed. Check to see if we preempt | |
9096 | * the current task. | |
9097 | */ | |
da7a735e PZ |
9098 | static void |
9099 | prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio) | |
cb469845 | 9100 | { |
da0c1e65 | 9101 | if (!task_on_rq_queued(p)) |
da7a735e PZ |
9102 | return; |
9103 | ||
cb469845 SR |
9104 | /* |
9105 | * Reschedule if we are currently running on this runqueue and | |
9106 | * our priority decreased, or if we are not currently running on | |
9107 | * this runqueue and our priority is higher than the current's | |
9108 | */ | |
da7a735e | 9109 | if (rq->curr == p) { |
cb469845 | 9110 | if (p->prio > oldprio) |
8875125e | 9111 | resched_curr(rq); |
cb469845 | 9112 | } else |
15afe09b | 9113 | check_preempt_curr(rq, p, 0); |
cb469845 SR |
9114 | } |
9115 | ||
daa59407 | 9116 | static inline bool vruntime_normalized(struct task_struct *p) |
da7a735e PZ |
9117 | { |
9118 | struct sched_entity *se = &p->se; | |
da7a735e PZ |
9119 | |
9120 | /* | |
daa59407 BP |
9121 | * In both the TASK_ON_RQ_QUEUED and TASK_ON_RQ_MIGRATING cases, |
9122 | * the dequeue_entity(.flags=0) will already have normalized the | |
9123 | * vruntime. | |
9124 | */ | |
9125 | if (p->on_rq) | |
9126 | return true; | |
9127 | ||
9128 | /* | |
9129 | * When !on_rq, vruntime of the task has usually NOT been normalized. | |
9130 | * But there are some cases where it has already been normalized: | |
da7a735e | 9131 | * |
daa59407 BP |
9132 | * - A forked child which is waiting for being woken up by |
9133 | * wake_up_new_task(). | |
9134 | * - A task which has been woken up by try_to_wake_up() and | |
9135 | * waiting for actually being woken up by sched_ttwu_pending(). | |
da7a735e | 9136 | */ |
daa59407 BP |
9137 | if (!se->sum_exec_runtime || p->state == TASK_WAKING) |
9138 | return true; | |
9139 | ||
9140 | return false; | |
9141 | } | |
9142 | ||
09a43ace VG |
9143 | #ifdef CONFIG_FAIR_GROUP_SCHED |
9144 | /* | |
9145 | * Propagate the changes of the sched_entity across the tg tree to make it | |
9146 | * visible to the root | |
9147 | */ | |
9148 | static void propagate_entity_cfs_rq(struct sched_entity *se) | |
9149 | { | |
9150 | struct cfs_rq *cfs_rq; | |
9151 | ||
9152 | /* Start to propagate at parent */ | |
9153 | se = se->parent; | |
9154 | ||
9155 | for_each_sched_entity(se) { | |
9156 | cfs_rq = cfs_rq_of(se); | |
9157 | ||
9158 | if (cfs_rq_throttled(cfs_rq)) | |
9159 | break; | |
9160 | ||
9161 | update_load_avg(se, UPDATE_TG); | |
9162 | } | |
9163 | } | |
9164 | #else | |
9165 | static void propagate_entity_cfs_rq(struct sched_entity *se) { } | |
9166 | #endif | |
9167 | ||
df217913 | 9168 | static void detach_entity_cfs_rq(struct sched_entity *se) |
daa59407 | 9169 | { |
daa59407 BP |
9170 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
9171 | ||
9d89c257 | 9172 | /* Catch up with the cfs_rq and remove our load when we leave */ |
d31b1a66 | 9173 | update_load_avg(se, 0); |
a05e8c51 | 9174 | detach_entity_load_avg(cfs_rq, se); |
7c3edd2c | 9175 | update_tg_load_avg(cfs_rq, false); |
09a43ace | 9176 | propagate_entity_cfs_rq(se); |
da7a735e PZ |
9177 | } |
9178 | ||
df217913 | 9179 | static void attach_entity_cfs_rq(struct sched_entity *se) |
cb469845 | 9180 | { |
daa59407 | 9181 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
7855a35a BP |
9182 | |
9183 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
eb7a59b2 M |
9184 | /* |
9185 | * Since the real-depth could have been changed (only FAIR | |
9186 | * class maintain depth value), reset depth properly. | |
9187 | */ | |
9188 | se->depth = se->parent ? se->parent->depth + 1 : 0; | |
9189 | #endif | |
7855a35a | 9190 | |
df217913 | 9191 | /* Synchronize entity with its cfs_rq */ |
d31b1a66 | 9192 | update_load_avg(se, sched_feat(ATTACH_AGE_LOAD) ? 0 : SKIP_AGE_LOAD); |
daa59407 | 9193 | attach_entity_load_avg(cfs_rq, se); |
7c3edd2c | 9194 | update_tg_load_avg(cfs_rq, false); |
09a43ace | 9195 | propagate_entity_cfs_rq(se); |
df217913 VG |
9196 | } |
9197 | ||
9198 | static void detach_task_cfs_rq(struct task_struct *p) | |
9199 | { | |
9200 | struct sched_entity *se = &p->se; | |
9201 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
9202 | ||
9203 | if (!vruntime_normalized(p)) { | |
9204 | /* | |
9205 | * Fix up our vruntime so that the current sleep doesn't | |
9206 | * cause 'unlimited' sleep bonus. | |
9207 | */ | |
9208 | place_entity(cfs_rq, se, 0); | |
9209 | se->vruntime -= cfs_rq->min_vruntime; | |
9210 | } | |
9211 | ||
9212 | detach_entity_cfs_rq(se); | |
9213 | } | |
9214 | ||
9215 | static void attach_task_cfs_rq(struct task_struct *p) | |
9216 | { | |
9217 | struct sched_entity *se = &p->se; | |
9218 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
9219 | ||
9220 | attach_entity_cfs_rq(se); | |
daa59407 BP |
9221 | |
9222 | if (!vruntime_normalized(p)) | |
9223 | se->vruntime += cfs_rq->min_vruntime; | |
9224 | } | |
6efdb105 | 9225 | |
daa59407 BP |
9226 | static void switched_from_fair(struct rq *rq, struct task_struct *p) |
9227 | { | |
9228 | detach_task_cfs_rq(p); | |
9229 | } | |
9230 | ||
9231 | static void switched_to_fair(struct rq *rq, struct task_struct *p) | |
9232 | { | |
9233 | attach_task_cfs_rq(p); | |
7855a35a | 9234 | |
daa59407 | 9235 | if (task_on_rq_queued(p)) { |
7855a35a | 9236 | /* |
daa59407 BP |
9237 | * We were most likely switched from sched_rt, so |
9238 | * kick off the schedule if running, otherwise just see | |
9239 | * if we can still preempt the current task. | |
7855a35a | 9240 | */ |
daa59407 BP |
9241 | if (rq->curr == p) |
9242 | resched_curr(rq); | |
9243 | else | |
9244 | check_preempt_curr(rq, p, 0); | |
7855a35a | 9245 | } |
cb469845 SR |
9246 | } |
9247 | ||
83b699ed SV |
9248 | /* Account for a task changing its policy or group. |
9249 | * | |
9250 | * This routine is mostly called to set cfs_rq->curr field when a task | |
9251 | * migrates between groups/classes. | |
9252 | */ | |
9253 | static void set_curr_task_fair(struct rq *rq) | |
9254 | { | |
9255 | struct sched_entity *se = &rq->curr->se; | |
9256 | ||
ec12cb7f PT |
9257 | for_each_sched_entity(se) { |
9258 | struct cfs_rq *cfs_rq = cfs_rq_of(se); | |
9259 | ||
9260 | set_next_entity(cfs_rq, se); | |
9261 | /* ensure bandwidth has been allocated on our new cfs_rq */ | |
9262 | account_cfs_rq_runtime(cfs_rq, 0); | |
9263 | } | |
83b699ed SV |
9264 | } |
9265 | ||
029632fb PZ |
9266 | void init_cfs_rq(struct cfs_rq *cfs_rq) |
9267 | { | |
bfb06889 | 9268 | cfs_rq->tasks_timeline = RB_ROOT_CACHED; |
029632fb PZ |
9269 | cfs_rq->min_vruntime = (u64)(-(1LL << 20)); |
9270 | #ifndef CONFIG_64BIT | |
9271 | cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime; | |
9272 | #endif | |
141965c7 | 9273 | #ifdef CONFIG_SMP |
09a43ace VG |
9274 | #ifdef CONFIG_FAIR_GROUP_SCHED |
9275 | cfs_rq->propagate_avg = 0; | |
9276 | #endif | |
9d89c257 YD |
9277 | atomic_long_set(&cfs_rq->removed_load_avg, 0); |
9278 | atomic_long_set(&cfs_rq->removed_util_avg, 0); | |
9ee474f5 | 9279 | #endif |
029632fb PZ |
9280 | } |
9281 | ||
810b3817 | 9282 | #ifdef CONFIG_FAIR_GROUP_SCHED |
ea86cb4b VG |
9283 | static void task_set_group_fair(struct task_struct *p) |
9284 | { | |
9285 | struct sched_entity *se = &p->se; | |
9286 | ||
9287 | set_task_rq(p, task_cpu(p)); | |
9288 | se->depth = se->parent ? se->parent->depth + 1 : 0; | |
9289 | } | |
9290 | ||
bc54da21 | 9291 | static void task_move_group_fair(struct task_struct *p) |
810b3817 | 9292 | { |
daa59407 | 9293 | detach_task_cfs_rq(p); |
b2b5ce02 | 9294 | set_task_rq(p, task_cpu(p)); |
6efdb105 BP |
9295 | |
9296 | #ifdef CONFIG_SMP | |
9297 | /* Tell se's cfs_rq has been changed -- migrated */ | |
9298 | p->se.avg.last_update_time = 0; | |
9299 | #endif | |
daa59407 | 9300 | attach_task_cfs_rq(p); |
810b3817 | 9301 | } |
029632fb | 9302 | |
ea86cb4b VG |
9303 | static void task_change_group_fair(struct task_struct *p, int type) |
9304 | { | |
9305 | switch (type) { | |
9306 | case TASK_SET_GROUP: | |
9307 | task_set_group_fair(p); | |
9308 | break; | |
9309 | ||
9310 | case TASK_MOVE_GROUP: | |
9311 | task_move_group_fair(p); | |
9312 | break; | |
9313 | } | |
9314 | } | |
9315 | ||
029632fb PZ |
9316 | void free_fair_sched_group(struct task_group *tg) |
9317 | { | |
9318 | int i; | |
9319 | ||
9320 | destroy_cfs_bandwidth(tg_cfs_bandwidth(tg)); | |
9321 | ||
9322 | for_each_possible_cpu(i) { | |
9323 | if (tg->cfs_rq) | |
9324 | kfree(tg->cfs_rq[i]); | |
6fe1f348 | 9325 | if (tg->se) |
029632fb PZ |
9326 | kfree(tg->se[i]); |
9327 | } | |
9328 | ||
9329 | kfree(tg->cfs_rq); | |
9330 | kfree(tg->se); | |
9331 | } | |
9332 | ||
9333 | int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent) | |
9334 | { | |
029632fb | 9335 | struct sched_entity *se; |
b7fa30c9 | 9336 | struct cfs_rq *cfs_rq; |
029632fb PZ |
9337 | int i; |
9338 | ||
9339 | tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL); | |
9340 | if (!tg->cfs_rq) | |
9341 | goto err; | |
9342 | tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL); | |
9343 | if (!tg->se) | |
9344 | goto err; | |
9345 | ||
9346 | tg->shares = NICE_0_LOAD; | |
9347 | ||
9348 | init_cfs_bandwidth(tg_cfs_bandwidth(tg)); | |
9349 | ||
9350 | for_each_possible_cpu(i) { | |
9351 | cfs_rq = kzalloc_node(sizeof(struct cfs_rq), | |
9352 | GFP_KERNEL, cpu_to_node(i)); | |
9353 | if (!cfs_rq) | |
9354 | goto err; | |
9355 | ||
9356 | se = kzalloc_node(sizeof(struct sched_entity), | |
9357 | GFP_KERNEL, cpu_to_node(i)); | |
9358 | if (!se) | |
9359 | goto err_free_rq; | |
9360 | ||
9361 | init_cfs_rq(cfs_rq); | |
9362 | init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]); | |
540247fb | 9363 | init_entity_runnable_average(se); |
029632fb PZ |
9364 | } |
9365 | ||
9366 | return 1; | |
9367 | ||
9368 | err_free_rq: | |
9369 | kfree(cfs_rq); | |
9370 | err: | |
9371 | return 0; | |
9372 | } | |
9373 | ||
8663e24d PZ |
9374 | void online_fair_sched_group(struct task_group *tg) |
9375 | { | |
9376 | struct sched_entity *se; | |
9377 | struct rq *rq; | |
9378 | int i; | |
9379 | ||
9380 | for_each_possible_cpu(i) { | |
9381 | rq = cpu_rq(i); | |
9382 | se = tg->se[i]; | |
9383 | ||
9384 | raw_spin_lock_irq(&rq->lock); | |
4126bad6 | 9385 | update_rq_clock(rq); |
d0326691 | 9386 | attach_entity_cfs_rq(se); |
55e16d30 | 9387 | sync_throttle(tg, i); |
8663e24d PZ |
9388 | raw_spin_unlock_irq(&rq->lock); |
9389 | } | |
9390 | } | |
9391 | ||
6fe1f348 | 9392 | void unregister_fair_sched_group(struct task_group *tg) |
029632fb | 9393 | { |
029632fb | 9394 | unsigned long flags; |
6fe1f348 PZ |
9395 | struct rq *rq; |
9396 | int cpu; | |
029632fb | 9397 | |
6fe1f348 PZ |
9398 | for_each_possible_cpu(cpu) { |
9399 | if (tg->se[cpu]) | |
9400 | remove_entity_load_avg(tg->se[cpu]); | |
029632fb | 9401 | |
6fe1f348 PZ |
9402 | /* |
9403 | * Only empty task groups can be destroyed; so we can speculatively | |
9404 | * check on_list without danger of it being re-added. | |
9405 | */ | |
9406 | if (!tg->cfs_rq[cpu]->on_list) | |
9407 | continue; | |
9408 | ||
9409 | rq = cpu_rq(cpu); | |
9410 | ||
9411 | raw_spin_lock_irqsave(&rq->lock, flags); | |
9412 | list_del_leaf_cfs_rq(tg->cfs_rq[cpu]); | |
9413 | raw_spin_unlock_irqrestore(&rq->lock, flags); | |
9414 | } | |
029632fb PZ |
9415 | } |
9416 | ||
9417 | void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq, | |
9418 | struct sched_entity *se, int cpu, | |
9419 | struct sched_entity *parent) | |
9420 | { | |
9421 | struct rq *rq = cpu_rq(cpu); | |
9422 | ||
9423 | cfs_rq->tg = tg; | |
9424 | cfs_rq->rq = rq; | |
029632fb PZ |
9425 | init_cfs_rq_runtime(cfs_rq); |
9426 | ||
9427 | tg->cfs_rq[cpu] = cfs_rq; | |
9428 | tg->se[cpu] = se; | |
9429 | ||
9430 | /* se could be NULL for root_task_group */ | |
9431 | if (!se) | |
9432 | return; | |
9433 | ||
fed14d45 | 9434 | if (!parent) { |
029632fb | 9435 | se->cfs_rq = &rq->cfs; |
fed14d45 PZ |
9436 | se->depth = 0; |
9437 | } else { | |
029632fb | 9438 | se->cfs_rq = parent->my_q; |
fed14d45 PZ |
9439 | se->depth = parent->depth + 1; |
9440 | } | |
029632fb PZ |
9441 | |
9442 | se->my_q = cfs_rq; | |
0ac9b1c2 PT |
9443 | /* guarantee group entities always have weight */ |
9444 | update_load_set(&se->load, NICE_0_LOAD); | |
029632fb PZ |
9445 | se->parent = parent; |
9446 | } | |
9447 | ||
9448 | static DEFINE_MUTEX(shares_mutex); | |
9449 | ||
9450 | int sched_group_set_shares(struct task_group *tg, unsigned long shares) | |
9451 | { | |
9452 | int i; | |
029632fb PZ |
9453 | |
9454 | /* | |
9455 | * We can't change the weight of the root cgroup. | |
9456 | */ | |
9457 | if (!tg->se[0]) | |
9458 | return -EINVAL; | |
9459 | ||
9460 | shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES)); | |
9461 | ||
9462 | mutex_lock(&shares_mutex); | |
9463 | if (tg->shares == shares) | |
9464 | goto done; | |
9465 | ||
9466 | tg->shares = shares; | |
9467 | for_each_possible_cpu(i) { | |
9468 | struct rq *rq = cpu_rq(i); | |
8a8c69c3 PZ |
9469 | struct sched_entity *se = tg->se[i]; |
9470 | struct rq_flags rf; | |
029632fb | 9471 | |
029632fb | 9472 | /* Propagate contribution to hierarchy */ |
8a8c69c3 | 9473 | rq_lock_irqsave(rq, &rf); |
71b1da46 | 9474 | update_rq_clock(rq); |
89ee048f VG |
9475 | for_each_sched_entity(se) { |
9476 | update_load_avg(se, UPDATE_TG); | |
9477 | update_cfs_shares(se); | |
9478 | } | |
8a8c69c3 | 9479 | rq_unlock_irqrestore(rq, &rf); |
029632fb PZ |
9480 | } |
9481 | ||
9482 | done: | |
9483 | mutex_unlock(&shares_mutex); | |
9484 | return 0; | |
9485 | } | |
9486 | #else /* CONFIG_FAIR_GROUP_SCHED */ | |
9487 | ||
9488 | void free_fair_sched_group(struct task_group *tg) { } | |
9489 | ||
9490 | int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent) | |
9491 | { | |
9492 | return 1; | |
9493 | } | |
9494 | ||
8663e24d PZ |
9495 | void online_fair_sched_group(struct task_group *tg) { } |
9496 | ||
6fe1f348 | 9497 | void unregister_fair_sched_group(struct task_group *tg) { } |
029632fb PZ |
9498 | |
9499 | #endif /* CONFIG_FAIR_GROUP_SCHED */ | |
9500 | ||
810b3817 | 9501 | |
6d686f45 | 9502 | static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task) |
0d721cea PW |
9503 | { |
9504 | struct sched_entity *se = &task->se; | |
0d721cea PW |
9505 | unsigned int rr_interval = 0; |
9506 | ||
9507 | /* | |
9508 | * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise | |
9509 | * idle runqueue: | |
9510 | */ | |
0d721cea | 9511 | if (rq->cfs.load.weight) |
a59f4e07 | 9512 | rr_interval = NS_TO_JIFFIES(sched_slice(cfs_rq_of(se), se)); |
0d721cea PW |
9513 | |
9514 | return rr_interval; | |
9515 | } | |
9516 | ||
bf0f6f24 IM |
9517 | /* |
9518 | * All the scheduling class methods: | |
9519 | */ | |
029632fb | 9520 | const struct sched_class fair_sched_class = { |
5522d5d5 | 9521 | .next = &idle_sched_class, |
bf0f6f24 IM |
9522 | .enqueue_task = enqueue_task_fair, |
9523 | .dequeue_task = dequeue_task_fair, | |
9524 | .yield_task = yield_task_fair, | |
d95f4122 | 9525 | .yield_to_task = yield_to_task_fair, |
bf0f6f24 | 9526 | |
2e09bf55 | 9527 | .check_preempt_curr = check_preempt_wakeup, |
bf0f6f24 IM |
9528 | |
9529 | .pick_next_task = pick_next_task_fair, | |
9530 | .put_prev_task = put_prev_task_fair, | |
9531 | ||
681f3e68 | 9532 | #ifdef CONFIG_SMP |
4ce72a2c | 9533 | .select_task_rq = select_task_rq_fair, |
0a74bef8 | 9534 | .migrate_task_rq = migrate_task_rq_fair, |
141965c7 | 9535 | |
0bcdcf28 CE |
9536 | .rq_online = rq_online_fair, |
9537 | .rq_offline = rq_offline_fair, | |
88ec22d3 | 9538 | |
12695578 | 9539 | .task_dead = task_dead_fair, |
c5b28038 | 9540 | .set_cpus_allowed = set_cpus_allowed_common, |
681f3e68 | 9541 | #endif |
bf0f6f24 | 9542 | |
83b699ed | 9543 | .set_curr_task = set_curr_task_fair, |
bf0f6f24 | 9544 | .task_tick = task_tick_fair, |
cd29fe6f | 9545 | .task_fork = task_fork_fair, |
cb469845 SR |
9546 | |
9547 | .prio_changed = prio_changed_fair, | |
da7a735e | 9548 | .switched_from = switched_from_fair, |
cb469845 | 9549 | .switched_to = switched_to_fair, |
810b3817 | 9550 | |
0d721cea PW |
9551 | .get_rr_interval = get_rr_interval_fair, |
9552 | ||
6e998916 SG |
9553 | .update_curr = update_curr_fair, |
9554 | ||
810b3817 | 9555 | #ifdef CONFIG_FAIR_GROUP_SCHED |
ea86cb4b | 9556 | .task_change_group = task_change_group_fair, |
810b3817 | 9557 | #endif |
bf0f6f24 IM |
9558 | }; |
9559 | ||
9560 | #ifdef CONFIG_SCHED_DEBUG | |
029632fb | 9561 | void print_cfs_stats(struct seq_file *m, int cpu) |
bf0f6f24 | 9562 | { |
a9e7f654 | 9563 | struct cfs_rq *cfs_rq, *pos; |
bf0f6f24 | 9564 | |
5973e5b9 | 9565 | rcu_read_lock(); |
a9e7f654 | 9566 | for_each_leaf_cfs_rq_safe(cpu_rq(cpu), cfs_rq, pos) |
5cef9eca | 9567 | print_cfs_rq(m, cpu, cfs_rq); |
5973e5b9 | 9568 | rcu_read_unlock(); |
bf0f6f24 | 9569 | } |
397f2378 SD |
9570 | |
9571 | #ifdef CONFIG_NUMA_BALANCING | |
9572 | void show_numa_stats(struct task_struct *p, struct seq_file *m) | |
9573 | { | |
9574 | int node; | |
9575 | unsigned long tsf = 0, tpf = 0, gsf = 0, gpf = 0; | |
9576 | ||
9577 | for_each_online_node(node) { | |
9578 | if (p->numa_faults) { | |
9579 | tsf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 0)]; | |
9580 | tpf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 1)]; | |
9581 | } | |
9582 | if (p->numa_group) { | |
9583 | gsf = p->numa_group->faults[task_faults_idx(NUMA_MEM, node, 0)], | |
9584 | gpf = p->numa_group->faults[task_faults_idx(NUMA_MEM, node, 1)]; | |
9585 | } | |
9586 | print_numa_stats(m, node, tsf, tpf, gsf, gpf); | |
9587 | } | |
9588 | } | |
9589 | #endif /* CONFIG_NUMA_BALANCING */ | |
9590 | #endif /* CONFIG_SCHED_DEBUG */ | |
029632fb PZ |
9591 | |
9592 | __init void init_sched_fair_class(void) | |
9593 | { | |
9594 | #ifdef CONFIG_SMP | |
9595 | open_softirq(SCHED_SOFTIRQ, run_rebalance_domains); | |
9596 | ||
3451d024 | 9597 | #ifdef CONFIG_NO_HZ_COMMON |
554cecaf | 9598 | nohz.next_balance = jiffies; |
029632fb | 9599 | zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT); |
029632fb PZ |
9600 | #endif |
9601 | #endif /* SMP */ | |
9602 | ||
9603 | } |