at commit 2f93bcb3

1 // SPDX-License-Identifier: GPL-2.0
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, Linutronix GmbH, Thomas Gleixner <tglx@kernel.org>
19 *
20 * Adaptive scheduling granularity, math enhancements by Peter Zijlstra
21 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra
22 */
23 #include <linux/energy_model.h>
24 #include <linux/mmap_lock.h>
25 #include <linux/hugetlb_inline.h>
26 #include <linux/jiffies.h>
27 #include <linux/mm_api.h>
28 #include <linux/highmem.h>
29 #include <linux/hrtimer.h>
30 #include <linux/hrtimer_bases.h>
31 #include <linux/spinlock_api.h>
32 #include <linux/cpumask_api.h>
33 #include <linux/lockdep_api.h>
34 #include <linux/softirq.h>
35 #include <linux/refcount_api.h>
36 #include <linux/topology.h>
37 #include <linux/sched/clock.h>
38 #include <linux/sched/cond_resched.h>
39 #include <linux/sched/cputime.h>
40 #include <linux/sched/isolation.h>
41 #include <linux/sched/nohz.h>
42 #include <linux/sched/prio.h>
43 #include <linux/static_call.h>
44
45 #include <linux/cpuidle.h>
46 #include <linux/interrupt.h>
47 #include <linux/memory-tiers.h>
48 #include <linux/mempolicy.h>
49 #include <linux/mutex_api.h>
50 #include <linux/profile.h>
51 #include <linux/psi.h>
52 #include <linux/ratelimit.h>
53 #include <linux/task_work.h>
54 #include <linux/rbtree_augmented.h>
55
56 #include <asm/switch_to.h>
57
58 #include <uapi/linux/sched/types.h>
59
60 #include "sched.h"
61 #include "stats.h"
62 #include "autogroup.h"
63
64 /*
65 * The initial- and re-scaling of tunables is configurable
66 *
67 * Options are:
68 *
69 * SCHED_TUNABLESCALING_NONE - unscaled, always *1
70 * SCHED_TUNABLESCALING_LOG - scaled logarithmically, *1+ilog(ncpus)
71 * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus
72 *
73 * (default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus))
74 */
75 unsigned int sysctl_sched_tunable_scaling = SCHED_TUNABLESCALING_LOG;
76
77 /*
78 * Default base time slice (request size r_i) for SCHED_NORMAL/SCHED_BATCH:
79 *
80 * Under EEVDF this is the request size used to compute the virtual
81 * deadline; see update_deadline().
82 *
83 * (default: 0.70 msec * (1 + ilog(ncpus)), units: nanoseconds)
84 */
85 unsigned int sysctl_sched_base_slice = 700000ULL;
86 static unsigned int normalized_sysctl_sched_base_slice = 700000ULL;
87
88 __read_mostly unsigned int sysctl_sched_migration_cost = 500000UL;
89
90 static int __init setup_sched_thermal_decay_shift(char *str)
91 {
92 pr_warn("Ignoring the deprecated sched_thermal_decay_shift= option\n");
93 return 1;
94 }
95 __setup("sched_thermal_decay_shift=", setup_sched_thermal_decay_shift);
96
97 /*
98 * For asym packing, by default the lower numbered CPU has higher priority.
99 */
100 int __weak arch_asym_cpu_priority(int cpu)
101 {
102 return -cpu;
103 }
104
105 /*
106 * The margin used when comparing utilization with CPU capacity.
107 *
108 * (default: ~20%)
109 */
110 #define fits_capacity(cap, max) ((cap) * 1280 < (max) * 1024)
111
112 /*
113 * The margin used when comparing CPU capacities.
114 * is 'cap1' noticeably greater than 'cap2'
115 *
116 * (default: ~5%)
117 */
118 #define capacity_greater(cap1, cap2) ((cap1) * 1024 > (cap2) * 1078)
119
120 #ifdef CONFIG_CFS_BANDWIDTH
121 /*
122 * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool
123 * each time a cfs_rq requests quota.
124 *
125 * Note: in the case that the slice exceeds the runtime remaining (either due
126 * to consumption or the quota being specified to be smaller than the slice)
127 * we will always only issue the remaining available time.
128 *
129 * (default: 5 msec, units: microseconds)
130 */
131 static unsigned int sysctl_sched_cfs_bandwidth_slice = 5000UL;
132 #endif
133
134 #ifdef CONFIG_NUMA_BALANCING
135 /* Restrict the NUMA promotion throughput (MB/s) for each target node. */
136 static unsigned int sysctl_numa_balancing_promote_rate_limit = 65536;
137 #endif
138
139 #ifdef CONFIG_SYSCTL
140 static const struct ctl_table sched_fair_sysctls[] = {
141 #ifdef CONFIG_CFS_BANDWIDTH
142 {
143 .procname = "sched_cfs_bandwidth_slice_us",
144 .data = &sysctl_sched_cfs_bandwidth_slice,
145 .maxlen = sizeof(unsigned int),
146 .mode = 0644,
147 .proc_handler = proc_dointvec_minmax,
148 .extra1 = SYSCTL_ONE,
149 },
150 #endif
151 #ifdef CONFIG_NUMA_BALANCING
152 {
153 .procname = "numa_balancing_promote_rate_limit_MBps",
154 .data = &sysctl_numa_balancing_promote_rate_limit,
155 .maxlen = sizeof(unsigned int),
156 .mode = 0644,
157 .proc_handler = proc_dointvec_minmax,
158 .extra1 = SYSCTL_ZERO,
159 },
160 #endif /* CONFIG_NUMA_BALANCING */
161 };
162
163 static int __init sched_fair_sysctl_init(void)
164 {
165 register_sysctl_init("kernel", sched_fair_sysctls);
166 return 0;
167 }
168 late_initcall(sched_fair_sysctl_init);
169 #endif /* CONFIG_SYSCTL */
170
171 static inline void update_load_add(struct load_weight *lw, unsigned long inc)
172 {
173 lw->weight += inc;
174 lw->inv_weight = 0;
175 }
176
177 static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
178 {
179 lw->weight -= dec;
180 lw->inv_weight = 0;
181 }
182
183 static inline void update_load_set(struct load_weight *lw, unsigned long w)
184 {
185 lw->weight = w;
186 lw->inv_weight = 0;
187 }
188
189 /*
190 * Increase the granularity value when there are more CPUs,
191 * because with more CPUs the 'effective latency' as visible
192 * to users decreases. But the relationship is not linear,
193 * so pick a second-best guess by going with the log2 of the
194 * number of CPUs.
195 *
196 * This idea comes from the SD scheduler of Con Kolivas:
197 */
198 static unsigned int get_update_sysctl_factor(void)
199 {
200 unsigned int cpus = min_t(unsigned int, num_online_cpus(), 8);
201 unsigned int factor;
202
203 switch (sysctl_sched_tunable_scaling) {
204 case SCHED_TUNABLESCALING_NONE:
205 factor = 1;
206 break;
207 case SCHED_TUNABLESCALING_LINEAR:
208 factor = cpus;
209 break;
210 case SCHED_TUNABLESCALING_LOG:
211 default:
212 factor = 1 + ilog2(cpus);
213 break;
214 }
215
216 return factor;
217 }
218
219 static void update_sysctl(void)
220 {
221 unsigned int factor = get_update_sysctl_factor();
222
223 #define SET_SYSCTL(name) \
224 (sysctl_##name = (factor) * normalized_sysctl_##name)
225 SET_SYSCTL(sched_base_slice);
226 #undef SET_SYSCTL
227 }
228
229 void __init sched_init_granularity(void)
230 {
231 update_sysctl();
232 }
233
234 #ifndef CONFIG_64BIT
235 #define WMULT_CONST (~0U)
236 #define WMULT_SHIFT 32
237
238 static void __update_inv_weight(struct load_weight *lw)
239 {
240 unsigned long w;
241
242 if (likely(lw->inv_weight))
243 return;
244
245 w = scale_load_down(lw->weight);
246
247 if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST))
248 lw->inv_weight = 1;
249 else if (unlikely(!w))
250 lw->inv_weight = WMULT_CONST;
251 else
252 lw->inv_weight = WMULT_CONST / w;
253 }
254
255 /*
256 * delta_exec * weight / lw.weight
257 * OR
258 * (delta_exec * (weight * lw->inv_weight)) >> WMULT_SHIFT
259 *
260 * Either weight := NICE_0_LOAD and lw \e sched_prio_to_wmult[], in which case
261 * we're guaranteed shift stays positive because inv_weight is guaranteed to
262 * fit 32 bits, and NICE_0_LOAD gives another 10 bits; therefore shift >= 22.
263 *
264 * Or, weight =< lw.weight (because lw.weight is the runqueue weight), thus
265 * weight/lw.weight <= 1, and therefore our shift will also be positive.
266 */
267 static u64 __calc_delta(u64 delta_exec, unsigned long weight, struct load_weight *lw)
268 {
269 u64 fact = scale_load_down(weight);
270 u32 fact_hi = (u32)(fact >> 32);
271 int shift = WMULT_SHIFT;
272 int fs;
273
274 __update_inv_weight(lw);
275
276 if (unlikely(fact_hi)) {
277 fs = fls(fact_hi);
278 shift -= fs;
279 fact >>= fs;
280 }
281
282 fact = mul_u32_u32(fact, lw->inv_weight);
283
284 fact_hi = (u32)(fact >> 32);
285 if (fact_hi) {
286 fs = fls(fact_hi);
287 shift -= fs;
288 fact >>= fs;
289 }
290
291 return mul_u64_u32_shr(delta_exec, fact, shift);
292 }
293 #else
294 static u64 __calc_delta(u64 delta_exec, unsigned long weight, struct load_weight *lw)
295 {
296 return (delta_exec * weight) / lw->weight;
297 }
298 #endif
299
300 /*
301 * delta /= w
302 */
303 static inline u64 calc_delta_fair(u64 delta, struct sched_entity *se)
304 {
305 if (se->h_load.weight != NICE_0_LOAD)
306 delta = __calc_delta(delta, NICE_0_LOAD, &se->h_load);
307
308 return delta;
309 }
310
311 const struct sched_class fair_sched_class;
312
313 /**************************************************************
314 * CFS operations on generic schedulable entities:
315 */
316
317 #ifdef CONFIG_FAIR_GROUP_SCHED
318
319 /* Walk up scheduling entities hierarchy */
320 #define for_each_sched_entity(se) \
321 for (; se; se = se->parent)
322
323 static inline bool list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
324 {
325 struct rq *rq = rq_of(cfs_rq);
326 int cpu = cpu_of(rq);
327
328 if (cfs_rq->on_list)
329 return rq->tmp_alone_branch == &rq->leaf_cfs_rq_list;
330
331 cfs_rq->on_list = 1;
332
333 /*
334 * Ensure we either appear before our parent (if already
335 * enqueued) or force our parent to appear after us when it is
336 * enqueued. The fact that we always enqueue bottom-up
337 * reduces this to two cases and a special case for the root
338 * cfs_rq. Furthermore, it also means that we will always reset
339 * tmp_alone_branch either when the branch is connected
340 * to a tree or when we reach the top of the tree
341 */
342 if (cfs_rq->tg->parent &&
343 tg_cfs_rq(cfs_rq->tg->parent, cpu)->on_list) {
344 /*
345 * If parent is already on the list, we add the child
346 * just before. Thanks to circular linked property of
347 * the list, this means to put the child at the tail
348 * of the list that starts by parent.
349 */
350 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list,
351 &(tg_cfs_rq(cfs_rq->tg->parent, cpu)->leaf_cfs_rq_list));
352 /*
353 * The branch is now connected to its tree so we can
354 * reset tmp_alone_branch to the beginning of the
355 * list.
356 */
357 rq->tmp_alone_branch = &rq->leaf_cfs_rq_list;
358 return true;
359 }
360
361 if (!cfs_rq->tg->parent) {
362 /*
363 * cfs rq without parent should be put
364 * at the tail of the list.
365 */
366 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list,
367 &rq->leaf_cfs_rq_list);
368 /*
369 * We have reach the top of a tree so we can reset
370 * tmp_alone_branch to the beginning of the list.
371 */
372 rq->tmp_alone_branch = &rq->leaf_cfs_rq_list;
373 return true;
374 }
375
376 /*
377 * The parent has not already been added so we want to
378 * make sure that it will be put after us.
379 * tmp_alone_branch points to the begin of the branch
380 * where we will add parent.
381 */
382 list_add_rcu(&cfs_rq->leaf_cfs_rq_list, rq->tmp_alone_branch);
383 /*
384 * update tmp_alone_branch to points to the new begin
385 * of the branch
386 */
387 rq->tmp_alone_branch = &cfs_rq->leaf_cfs_rq_list;
388 return false;
389 }
390
391 static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
392 {
393 if (cfs_rq->on_list) {
394 struct rq *rq = rq_of(cfs_rq);
395
396 /*
397 * With cfs_rq being unthrottled/throttled during an enqueue,
398 * it can happen the tmp_alone_branch points to the leaf that
399 * we finally want to delete. In this case, tmp_alone_branch moves
400 * to the prev element but it will point to rq->leaf_cfs_rq_list
401 * at the end of the enqueue.
402 */
403 if (rq->tmp_alone_branch == &cfs_rq->leaf_cfs_rq_list)
404 rq->tmp_alone_branch = cfs_rq->leaf_cfs_rq_list.prev;
405
406 list_del_rcu(&cfs_rq->leaf_cfs_rq_list);
407 cfs_rq->on_list = 0;
408 }
409 }
410
411 static inline void assert_list_leaf_cfs_rq(struct rq *rq)
412 {
413 WARN_ON_ONCE(rq->tmp_alone_branch != &rq->leaf_cfs_rq_list);
414 }
415
416 /* Iterate through all leaf cfs_rq's on a runqueue */
417 #define for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) \
418 list_for_each_entry_safe(cfs_rq, pos, &rq->leaf_cfs_rq_list, \
419 leaf_cfs_rq_list)
420
421 /* Do the two (enqueued) entities belong to the same group ? */
422 static inline struct cfs_rq *
423 is_same_group(struct sched_entity *se, struct sched_entity *pse)
424 {
425 if (se->cfs_rq == pse->cfs_rq)
426 return se->cfs_rq;
427
428 return NULL;
429 }
430
431 static inline struct sched_entity *parent_entity(const struct sched_entity *se)
432 {
433 return se->parent;
434 }
435
436 static int tg_is_idle(struct task_group *tg)
437 {
438 return tg->idle > 0;
439 }
440
441 static int cfs_rq_is_idle(struct cfs_rq *cfs_rq)
442 {
443 return cfs_rq->idle > 0;
444 }
445
446 static int se_is_idle(struct sched_entity *se)
447 {
448 if (entity_is_task(se))
449 return task_has_idle_policy(task_of(se));
450 return cfs_rq_is_idle(group_cfs_rq(se));
451 }
452
453 #else /* !CONFIG_FAIR_GROUP_SCHED: */
454
455 #define for_each_sched_entity(se) \
456 for (; se; se = NULL)
457
458 static inline bool list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
459 {
460 return true;
461 }
462
463 static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
464 {
465 }
466
467 static inline void assert_list_leaf_cfs_rq(struct rq *rq)
468 {
469 }
470
471 #define for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) \
472 for (cfs_rq = &rq->cfs, pos = NULL; cfs_rq; cfs_rq = pos)
473
474 static inline struct sched_entity *parent_entity(struct sched_entity *se)
475 {
476 return NULL;
477 }
478
479 static inline int tg_is_idle(struct task_group *tg)
480 {
481 return 0;
482 }
483
484 static int cfs_rq_is_idle(struct cfs_rq *cfs_rq)
485 {
486 return 0;
487 }
488
489 static int se_is_idle(struct sched_entity *se)
490 {
491 return task_has_idle_policy(task_of(se));
492 }
493
494 #endif /* !CONFIG_FAIR_GROUP_SCHED */
495
496 static __always_inline
497 bool account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec);
498
499 /**************************************************************
500 * Scheduling class tree data structure manipulation methods:
501 */
502
503 extern void __BUILD_BUG_vruntime_cmp(void);
504
505 /* Use __builtin_strcmp() because of __HAVE_ARCH_STRCMP: */
506
507 #define vruntime_cmp(A, CMP_STR, B) ({ \
508 int __res = 0; \
509 \
510 if (!__builtin_strcmp(CMP_STR, "<")) { \
511 __res = ((s64)((A)-(B)) < 0); \
512 } else if (!__builtin_strcmp(CMP_STR, "<=")) { \
513 __res = ((s64)((A)-(B)) <= 0); \
514 } else if (!__builtin_strcmp(CMP_STR, ">")) { \
515 __res = ((s64)((A)-(B)) > 0); \
516 } else if (!__builtin_strcmp(CMP_STR, ">=")) { \
517 __res = ((s64)((A)-(B)) >= 0); \
518 } else { \
519 /* Unknown operator throws linker error: */ \
520 __BUILD_BUG_vruntime_cmp(); \
521 } \
522 \
523 __res; \
524 })
525
526 extern void __BUILD_BUG_vruntime_op(void);
527
528 #define vruntime_op(A, OP_STR, B) ({ \
529 s64 __res = 0; \
530 \
531 if (!__builtin_strcmp(OP_STR, "-")) { \
532 __res = (s64)((A)-(B)); \
533 } else { \
534 /* Unknown operator throws linker error: */ \
535 __BUILD_BUG_vruntime_op(); \
536 } \
537 \
538 __res; \
539 })
540
541
542 static inline __maybe_unused u64 max_vruntime(u64 max_vruntime, u64 vruntime)
543 {
544 if (vruntime_cmp(vruntime, ">", max_vruntime))
545 max_vruntime = vruntime;
546
547 return max_vruntime;
548 }
549
550 static inline __maybe_unused u64 min_vruntime(u64 min_vruntime, u64 vruntime)
551 {
552 if (vruntime_cmp(vruntime, "<", min_vruntime))
553 min_vruntime = vruntime;
554
555 return min_vruntime;
556 }
557
558 static inline bool entity_before(const struct sched_entity *a,
559 const struct sched_entity *b)
560 {
561 /*
562 * Tiebreak on vruntime seems unnecessary since it can
563 * hardly happen.
564 */
565 return vruntime_cmp(a->deadline, "<", b->deadline);
566 }
567
568 /*
569 * Per avg_vruntime() below, cfs_rq::zero_vruntime is only slightly stale
570 * and this value should be no more than two lag bounds. Which puts it in the
571 * general order of:
572 *
573 * (slice + TICK_NSEC) << NICE_0_LOAD_SHIFT
574 *
575 * which is around 44 bits in size (on 64bit); that is 20 for
576 * NICE_0_LOAD_SHIFT, another 20 for NSEC_PER_MSEC and then a handful for
577 * however many msec the actual slice+tick ends up begin.
578 *
579 * (disregarding the actual divide-by-weight part makes for the worst case
580 * weight of 2, which nicely cancels vs the fuzz in zero_vruntime not actually
581 * being the zero-lag point).
582 */
583 static inline s64 entity_key(struct cfs_rq *cfs_rq, struct sched_entity *se)
584 {
585 return vruntime_op(se->vruntime, "-", cfs_rq->zero_vruntime);
586 }
587
588 #define __node_2_se(node) \
589 rb_entry((node), struct sched_entity, run_node)
590
591 /*
592 * Compute virtual time from the per-task service numbers:
593 *
594 * Fair schedulers conserve lag:
595 *
596 * \Sum lag_i = 0
597 *
598 * Where lag_i is given by:
599 *
600 * lag_i = S - s_i = w_i * (V - v_i)
601 *
602 * Where S is the ideal service time and V is it's virtual time counterpart.
603 * Therefore:
604 *
605 * \Sum lag_i = 0
606 * \Sum w_i * (V - v_i) = 0
607 * \Sum (w_i * V - w_i * v_i) = 0
608 *
609 * From which we can solve an expression for V in v_i (which we have in
610 * se->vruntime):
611 *
612 * \Sum v_i * w_i \Sum v_i * w_i
613 * V = -------------- = --------------
614 * \Sum w_i W
615 *
616 * Specifically, this is the weighted average of all entity virtual runtimes.
617 *
618 * [[ NOTE: this is only equal to the ideal scheduler under the condition
619 * that join/leave operations happen at lag_i = 0, otherwise the
620 * virtual time has non-contiguous motion equivalent to:
621 *
622 * V +-= lag_i / W
623 *
624 * Also see the comment in place_entity() that deals with this. ]]
625 *
626 * However, since v_i is u64, and the multiplication could easily overflow
627 * transform it into a relative form that uses smaller quantities:
628 *
629 * Substitute: v_i == (v_i - v0) + v0
630 *
631 * \Sum ((v_i - v0) + v0) * w_i \Sum (v_i - v0) * w_i
632 * V = ---------------------------- = --------------------- + v0
633 * W W
634 *
635 * Which we track using:
636 *
637 * v0 := cfs_rq->zero_vruntime
638 * \Sum (v_i - v0) * w_i := cfs_rq->sum_w_vruntime
639 * \Sum w_i := cfs_rq->sum_weight
640 *
641 * Since zero_vruntime closely tracks the per-task service, these
642 * deltas: (v_i - v0), will be in the order of the maximal (virtual) lag
643 * induced in the system due to quantisation.
644 */
645 static inline unsigned long avg_vruntime_weight(struct cfs_rq *cfs_rq, unsigned long w)
646 {
647 #ifdef CONFIG_64BIT
648 if (cfs_rq->sum_shift)
649 w = max(2UL, w >> cfs_rq->sum_shift);
650 #endif
651 return w;
652 }
653
654 static inline void
655 __sum_w_vruntime_add(struct cfs_rq *cfs_rq, struct sched_entity *se)
656 {
657 unsigned long weight = avg_vruntime_weight(cfs_rq, se->h_load.weight);
658 s64 w_vruntime, key = entity_key(cfs_rq, se);
659
660 w_vruntime = key * weight;
661 WARN_ON_ONCE((w_vruntime >> 63) != (w_vruntime >> 62));
662
663 cfs_rq->sum_w_vruntime += w_vruntime;
664 cfs_rq->sum_weight += weight;
665 }
666
667 static void
668 sum_w_vruntime_add_paranoid(struct cfs_rq *cfs_rq, struct sched_entity *se)
669 {
670 unsigned long weight;
671 s64 key, tmp;
672
673 again:
674 weight = avg_vruntime_weight(cfs_rq, se->h_load.weight);
675 key = entity_key(cfs_rq, se);
676
677 if (check_mul_overflow(key, weight, &key))
678 goto overflow;
679
680 if (check_add_overflow(cfs_rq->sum_w_vruntime, key, &tmp))
681 goto overflow;
682
683 cfs_rq->sum_w_vruntime = tmp;
684 cfs_rq->sum_weight += weight;
685 return;
686
687 overflow:
688 /*
689 * There's gotta be a limit -- if we're still failing at this point
690 * there's really nothing much to be done about things.
691 */
692 BUG_ON(cfs_rq->sum_shift >= 10);
693 cfs_rq->sum_shift++;
694
695 /*
696 * Note: \Sum (k_i * (w_i >> 1)) != (\Sum (k_i * w_i)) >> 1
697 */
698 cfs_rq->sum_w_vruntime = 0;
699 cfs_rq->sum_weight = 0;
700
701 for (struct rb_node *node = cfs_rq->tasks_timeline.rb_leftmost;
702 node; node = rb_next(node))
703 __sum_w_vruntime_add(cfs_rq, __node_2_se(node));
704
705 goto again;
706 }
707
708 static void
709 sum_w_vruntime_add(struct cfs_rq *cfs_rq, struct sched_entity *se)
710 {
711 if (sched_feat(PARANOID_AVG))
712 return sum_w_vruntime_add_paranoid(cfs_rq, se);
713
714 __sum_w_vruntime_add(cfs_rq, se);
715 }
716
717 static void
718 sum_w_vruntime_sub(struct cfs_rq *cfs_rq, struct sched_entity *se)
719 {
720 unsigned long weight = avg_vruntime_weight(cfs_rq, se->h_load.weight);
721 s64 key = entity_key(cfs_rq, se);
722
723 cfs_rq->sum_w_vruntime -= key * weight;
724 cfs_rq->sum_weight -= weight;
725 }
726
727 static inline
728 void update_zero_vruntime(struct cfs_rq *cfs_rq, s64 delta)
729 {
730 /*
731 * v' = v + d ==> sum_w_vruntime' = sum_w_vruntime - d*sum_weight
732 */
733 cfs_rq->sum_w_vruntime -= cfs_rq->sum_weight * delta;
734 cfs_rq->zero_vruntime += delta;
735 }
736
737 /*
738 * Specifically: avg_vruntime() + 0 must result in entity_eligible() := true
739 * For this to be so, the result of this function must have a left bias.
740 *
741 * Called in:
742 * - place_entity() -- before enqueue
743 * - update_entity_lag() -- before dequeue
744 * - update_deadline() -- slice expiration
745 *
746 * This means it is one entry 'behind' but that puts it close enough to where
747 * the bound on entity_key() is at most two lag bounds.
748 */
749 u64 avg_vruntime(struct cfs_rq *cfs_rq)
750 {
751 struct sched_entity *curr = cfs_rq->curr;
752 long weight = cfs_rq->sum_weight;
753 s64 delta = 0;
754
755 if (curr && !curr->on_rq)
756 curr = NULL;
757
758 if (weight) {
759 s64 runtime = cfs_rq->sum_w_vruntime;
760
761 if (curr) {
762 unsigned long w = avg_vruntime_weight(cfs_rq, curr->h_load.weight);
763
764 runtime += entity_key(cfs_rq, curr) * w;
765 weight += w;
766 }
767
768 /* sign flips effective floor / ceiling */
769 if (runtime < 0)
770 runtime -= (weight - 1);
771
772 delta = div64_long(runtime, weight);
773 } else if (curr) {
774 /*
775 * When there is but one element, it is the average.
776 */
777 delta = curr->vruntime - cfs_rq->zero_vruntime;
778 }
779
780 update_zero_vruntime(cfs_rq, delta);
781
782 return cfs_rq->zero_vruntime;
783 }
784
785 /*
786 * \Sum (v_i - v0)*w_i
787 * V = ------------------- + v0
788 * \Sum w_i
789 *
790 * Let W = \Sum w_i, and move v_j such that 'v_j == V', thus:
791 *
792 * V = 1/W * {(v_j - v0)*w_j + \Sum_i!=j (v_i - v0)*w_i} + v0
793 *
794 * v_j = 1/W * {(v_j - v0)*w_j + \Sum_i!=j (v_i - v0)*w_i} + v0
795 *
796 * v_j = 1/W * (v_j - v0)*w_j + 1/W * \Sum_i!=j (v_i - v0)*w_i + v0
797 *
798 * v_j - 1/W * (v_j - v0)*w_j = 1/W * \Sum_i!=j (v_i - v0)*w_i + v0
799 *
800 * v_j*W - (v_j - v0)*w_j = \Sum_i!=j (v_i - v0)*w_i + v0*W
801 *
802 * v_j*(W - w_j) + v0*w_j = \Sum_i!=j (v_i - v0)*w_i + v0*W
803 *
804 * v_j*(W - w_j) = \Sum_i!=j (v_i - v0)*w_i + v0*(W - w_j)
805 *
806 * \Sum_i!=j (v_i - v0)*w_i
807 * v_j = ------------------------ + v0
808 * W - w_j
809 *
810 * When v_j happens to be curr, then '\Sum_i!=j (v_i - v0)*w_i'
811 * is cfs_rq->sum_w_runtime, and 'W - w_j' is cfs_rq->sum_weight, since curr
812 * is not included in the sum.
813 */
814 static u64 ineligible_vruntime(struct cfs_rq *cfs_rq)
815 {
816 struct sched_entity *curr = cfs_rq->curr;
817 long weight = cfs_rq->sum_weight;
818 s64 delta = 0;
819
820 if (curr && !curr->on_rq)
821 curr = NULL;
822
823 /*
824 * This is called from set_next_task_fair(.first=true) /
825 * set_protect_slice() so curr had better be set and on_rq.
826 */
827 WARN_ON_ONCE(!curr);
828
829 if (weight) {
830 s64 runtime = cfs_rq->sum_w_vruntime;
831
832 /*
833 * Do not add @curr to obtain the effective '- w_j' terms.
834 */
835
836 /* sign flips effective floor / ceiling */
837 if (runtime < 0)
838 runtime -= (weight - 1);
839
840 delta = div64_long(runtime, weight);
841 }
842
843 return cfs_rq->zero_vruntime + delta + 1;
844 }
845
846 static inline u64 cfs_rq_max_slice(struct cfs_rq *cfs_rq);
847
848 /*
849 * lag_i = S - s_i = w_i * (V - v_i)
850 *
851 * However, since V is approximated by the weighted average of all entities it
852 * is possible -- by addition/removal/reweight to the tree -- to move V around
853 * and end up with a larger lag than we started with.
854 *
855 * Limit this to either double the slice length with a minimum of TICK_NSEC
856 * since that is the timing granularity.
857 *
858 * EEVDF gives the following limit for a steady state system:
859 *
860 * -r_max < lag < max(r_max, q)
861 */
862 static s64 entity_lag(struct cfs_rq *cfs_rq, struct sched_entity *se, u64 avruntime)
863 {
864 u64 max_slice = cfs_rq_max_slice(cfs_rq) + TICK_NSEC;
865 s64 vlag, limit;
866
867 vlag = avruntime - se->vruntime;
868 limit = calc_delta_fair(max_slice, se);
869
870 return clamp(vlag, -limit, limit);
871 }
872
873 /*
874 * Delayed dequeue aims to reduce the negative lag of a dequeued task. While
875 * updating the lag of an entity, check that negative lag didn't increase
876 * during the delayed dequeue period which would be unfair.
877 * Similarly, check that the entity didn't gain positive lag when DELAY_ZERO
878 * is set.
879 *
880 * Return true if the vlag has been modified. Specifically:
881 *
882 * se->vlag != avg_vruntime() - se->vruntime
883 *
884 * This can be due to clamping in entity_lag() or clamping due to
885 * sched_delayed. Either way, when vlag is modified and the entity is
886 * retained, the tree needs to be adjusted.
887 */
888 static __always_inline
889 bool update_entity_lag(struct cfs_rq *cfs_rq, struct sched_entity *se)
890 {
891 u64 avruntime = avg_vruntime(cfs_rq);
892 s64 vlag = entity_lag(cfs_rq, se, avruntime);
893
894 if (se->sched_delayed) {
895 /* previous vlag < 0 otherwise se would not be delayed */
896 vlag = max(vlag, se->vlag);
897 if (sched_feat(DELAY_ZERO))
898 vlag = min(vlag, 0);
899 }
900 se->vlag = vlag;
901
902 return avruntime - vlag != se->vruntime;
903 }
904
905 /*
906 * Entity is eligible once it received less service than it ought to have,
907 * eg. lag >= 0.
908 *
909 * lag_i = S - s_i = w_i*(V - v_i)
910 *
911 * lag_i >= 0 -> V >= v_i
912 *
913 * \Sum (v_i - v0)*w_i
914 * V = ------------------- + v0
915 * \Sum w_i
916 *
917 * lag_i >= 0 -> \Sum (v_i - v0)*w_i >= (v_i - v0)*(\Sum w_i)
918 *
919 * Note: using 'avg_vruntime() > se->vruntime' is inaccurate due
920 * to the loss in precision caused by the division.
921 */
922 static int vruntime_eligible(struct cfs_rq *cfs_rq, u64 vruntime)
923 {
924 struct sched_entity *curr = cfs_rq->curr;
925 s64 key, avg = cfs_rq->sum_w_vruntime;
926 long load = cfs_rq->sum_weight;
927
928 if (curr && curr->on_rq) {
929 unsigned long weight = avg_vruntime_weight(cfs_rq, curr->h_load.weight);
930
931 avg += entity_key(cfs_rq, curr) * weight;
932 load += weight;
933 }
934
935 key = vruntime_op(vruntime, "-", cfs_rq->zero_vruntime);
936
937 /*
938 * The worst case term for @key includes 'NSEC_TICK * NICE_0_LOAD'
939 * and @load obviously includes NICE_0_LOAD. NSEC_TICK is around 24
940 * bits, while NICE_0_LOAD is 20 on 64bit and 10 otherwise.
941 *
942 * This gives that on 64bit the product will be at least 64bit which
943 * overflows s64, while on 32bit it will only be 44bits and should fit
944 * comfortably.
945 */
946 #ifdef CONFIG_64BIT
947 #ifdef CONFIG_ARCH_SUPPORTS_INT128
948 /* This often results in simpler code than __builtin_mul_overflow(). */
949 return avg >= (__int128)key * load;
950 #else
951 s64 rhs;
952 /*
953 * On overflow, the sign of key tells us the correct answer: a large
954 * positive key means vruntime >> V, so not eligible; a large negative
955 * key means vruntime << V, so eligible.
956 */
957 if (check_mul_overflow(key, load, &rhs))
958 return key <= 0;
959
960 return avg >= rhs;
961 #endif
962 #else /* 32bit */
963 return avg >= key * load;
964 #endif
965 }
966
967 int entity_eligible(struct cfs_rq *cfs_rq, struct sched_entity *se)
968 {
969 return vruntime_eligible(cfs_rq, se->vruntime);
970 }
971
972 static inline u64 cfs_rq_min_slice(struct cfs_rq *cfs_rq)
973 {
974 struct sched_entity *root = __pick_root_entity(cfs_rq);
975 struct sched_entity *curr = cfs_rq->curr;
976 u64 min_slice = ~0ULL;
977
978 if (curr && curr->on_rq)
979 min_slice = curr->slice;
980
981 if (root)
982 min_slice = min(min_slice, root->min_slice);
983
984 return min_slice;
985 }
986
987 static inline u64 cfs_rq_max_slice(struct cfs_rq *cfs_rq)
988 {
989 struct sched_entity *root = __pick_root_entity(cfs_rq);
990 struct sched_entity *curr = cfs_rq->curr;
991 u64 max_slice = 0ULL;
992
993 if (curr && curr->on_rq)
994 max_slice = curr->slice;
995
996 if (root)
997 max_slice = max(max_slice, root->max_slice);
998
999 return max_slice;
1000 }
1001
1002 static inline bool __entity_less(struct rb_node *a, const struct rb_node *b)
1003 {
1004 return entity_before(__node_2_se(a), __node_2_se(b));
1005 }
1006
1007 static inline void __min_vruntime_update(struct sched_entity *se, struct rb_node *node)
1008 {
1009 if (node) {
1010 struct sched_entity *rse = __node_2_se(node);
1011
1012 if (vruntime_cmp(se->min_vruntime, ">", rse->min_vruntime))
1013 se->min_vruntime = rse->min_vruntime;
1014 }
1015 }
1016
1017 static inline void __min_slice_update(struct sched_entity *se, struct rb_node *node)
1018 {
1019 if (node) {
1020 struct sched_entity *rse = __node_2_se(node);
1021 if (rse->min_slice < se->min_slice)
1022 se->min_slice = rse->min_slice;
1023 }
1024 }
1025
1026 static inline void __max_slice_update(struct sched_entity *se, struct rb_node *node)
1027 {
1028 if (node) {
1029 struct sched_entity *rse = __node_2_se(node);
1030 if (rse->max_slice > se->max_slice)
1031 se->max_slice = rse->max_slice;
1032 }
1033 }
1034
1035 static inline void min_vruntime_copy(struct sched_entity *new, struct sched_entity *old)
1036 {
1037 new->min_vruntime = old->min_vruntime;
1038 new->min_slice = old->min_slice;
1039 new->max_slice = old->max_slice;
1040 }
1041
1042 /*
1043 * se->min_vruntime = min(se->vruntime, {left,right}->min_vruntime)
1044 */
1045 static inline bool min_vruntime_update(struct sched_entity *se, bool exit)
1046 {
1047 u64 old_min_vruntime = se->min_vruntime;
1048 u64 old_min_slice = se->min_slice;
1049 u64 old_max_slice = se->max_slice;
1050 struct rb_node *node = &se->run_node;
1051
1052 se->min_vruntime = se->vruntime;
1053 __min_vruntime_update(se, node->rb_right);
1054 __min_vruntime_update(se, node->rb_left);
1055
1056 se->min_slice = se->slice;
1057 __min_slice_update(se, node->rb_right);
1058 __min_slice_update(se, node->rb_left);
1059
1060 se->max_slice = se->slice;
1061 __max_slice_update(se, node->rb_right);
1062 __max_slice_update(se, node->rb_left);
1063
1064 return se->min_vruntime == old_min_vruntime &&
1065 se->min_slice == old_min_slice &&
1066 se->max_slice == old_max_slice;
1067 }
1068
1069
1070 RB_DECLARE_CALLBACKS_MULTI(static, min_vruntime_cb, struct sched_entity,
1071 run_node, min_vruntime_copy, min_vruntime_update);
1072
1073 /*
1074 * Enqueue an entity into the rb-tree:
1075 */
1076 static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
1077 {
1078 WARN_ON_ONCE(&rq_of(cfs_rq)->cfs != cfs_rq);
1079 WARN_ON_ONCE(!entity_is_task(se));
1080
1081 sum_w_vruntime_add(cfs_rq, se);
1082 se->min_vruntime = se->vruntime;
1083 se->min_slice = se->slice;
1084 se->max_slice = se->slice;
1085
1086 rb_add_augmented_cached(&se->run_node, &cfs_rq->tasks_timeline,
1087 __entity_less, &min_vruntime_cb);
1088 }
1089
1090 static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
1091 {
1092 WARN_ON_ONCE(&rq_of(cfs_rq)->cfs != cfs_rq);
1093 WARN_ON_ONCE(!entity_is_task(se));
1094
1095 rb_erase_augmented_cached(&se->run_node, &cfs_rq->tasks_timeline,
1096 &min_vruntime_cb);
1097 sum_w_vruntime_sub(cfs_rq, se);
1098 }
1099
1100 struct sched_entity *__pick_root_entity(struct cfs_rq *cfs_rq)
1101 {
1102 struct rb_node *root = cfs_rq->tasks_timeline.rb_root.rb_node;
1103
1104 if (!root)
1105 return NULL;
1106
1107 return __node_2_se(root);
1108 }
1109
1110 struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq)
1111 {
1112 struct rb_node *left = rb_first_cached(&cfs_rq->tasks_timeline);
1113
1114 if (!left)
1115 return NULL;
1116
1117 return __node_2_se(left);
1118 }
1119
1120 /*
1121 * Set the vruntime up to which an entity can run before looking
1122 * for another entity to pick.
1123 * In case of run to parity, we use the shortest slice of the enqueued
1124 * entities to set the protected period.
1125 * When run to parity is disabled, we give a minimum quantum to the running
1126 * entity to ensure progress.
1127 */
1128 static inline void set_protect_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
1129 {
1130 u64 slice = normalized_sysctl_sched_base_slice;
1131 u64 vprot = se->deadline;
1132
1133 if (sched_feat(RUN_TO_PARITY))
1134 slice = cfs_rq_min_slice(cfs_rq);
1135
1136 slice = min(slice, se->slice);
1137
1138 /* If there are shorter slices than se's one */
1139 if (slice != se->slice) {
1140 if (sched_feat(PREEMPT_SHORT))
1141 vprot = min_vruntime(vprot, ineligible_vruntime(cfs_rq));
1142 else
1143 vprot = min_vruntime(vprot, se->vruntime + calc_delta_fair(slice, se));
1144 }
1145
1146 se->vprot = vprot;
1147 }
1148
1149 static inline void update_protect_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
1150 {
1151 u64 slice = cfs_rq_min_slice(cfs_rq);
1152 u64 vruntime = min_vruntime(se->vruntime, avg_vruntime(cfs_rq));
1153
1154 se->vprot = min_vruntime(se->vprot, vruntime + calc_delta_fair(slice, se));
1155 }
1156
1157 static inline bool protect_slice(struct sched_entity *se)
1158 {
1159 return vruntime_cmp(se->vruntime, "<", se->vprot);
1160 }
1161
1162 static inline void cancel_protect_slice(struct sched_entity *se)
1163 {
1164 if (protect_slice(se))
1165 se->vprot = se->vruntime;
1166 }
1167
1168 /*
1169 * Earliest Eligible Virtual Deadline First
1170 *
1171 * In order to provide latency guarantees for different request sizes
1172 * EEVDF selects the best runnable task from two criteria:
1173 *
1174 * 1) the task must be eligible (must be owed service)
1175 *
1176 * 2) from those tasks that meet 1), we select the one
1177 * with the earliest virtual deadline.
1178 *
1179 * We can do this in O(log n) time due to an augmented RB-tree. The
1180 * tree keeps the entries sorted on deadline, but also functions as a
1181 * heap based on the vruntime by keeping:
1182 *
1183 * se->min_vruntime = min(se->vruntime, se->{left,right}->min_vruntime)
1184 *
1185 * Which allows tree pruning through eligibility.
1186 */
1187 static struct sched_entity *pick_eevdf(struct cfs_rq *cfs_rq, bool protect)
1188 {
1189 struct rb_node *node = cfs_rq->tasks_timeline.rb_root.rb_node;
1190 struct sched_entity *se = __pick_first_entity(cfs_rq);
1191 struct sched_entity *curr = cfs_rq->curr;
1192 struct sched_entity *best = NULL;
1193
1194 /*
1195 * We can safely skip eligibility check if there is only one entity
1196 * in this cfs_rq, saving some cycles.
1197 */
1198 if (cfs_rq->h_nr_queued == 1)
1199 return curr && curr->on_rq ? curr : se;
1200
1201 /*
1202 * Picking the ->next buddy will affect latency but not fairness.
1203 */
1204 if (sched_feat(PICK_BUDDY) && protect &&
1205 cfs_rq->next && entity_eligible(cfs_rq, cfs_rq->next)) {
1206 /* ->next will never be delayed */
1207 WARN_ON_ONCE(cfs_rq->next->sched_delayed);
1208 return cfs_rq->next;
1209 }
1210
1211 if (curr && (!curr->on_rq || !entity_eligible(cfs_rq, curr)))
1212 curr = NULL;
1213
1214 if (curr && protect && protect_slice(curr))
1215 return curr;
1216
1217 /* Pick the leftmost entity if it's eligible */
1218 if (se && entity_eligible(cfs_rq, se)) {
1219 best = se;
1220 goto found;
1221 }
1222
1223 /* Heap search for the EEVD entity */
1224 while (node) {
1225 struct rb_node *left = node->rb_left;
1226
1227 /*
1228 * Eligible entities in left subtree are always better
1229 * choices, since they have earlier deadlines.
1230 */
1231 if (left && vruntime_eligible(cfs_rq,
1232 __node_2_se(left)->min_vruntime)) {
1233 node = left;
1234 continue;
1235 }
1236
1237 se = __node_2_se(node);
1238
1239 /*
1240 * The left subtree either is empty or has no eligible
1241 * entity, so check the current node since it is the one
1242 * with earliest deadline that might be eligible.
1243 */
1244 if (entity_eligible(cfs_rq, se)) {
1245 best = se;
1246 break;
1247 }
1248
1249 node = node->rb_right;
1250 }
1251 found:
1252 if (!best || (curr && entity_before(curr, best)))
1253 best = curr;
1254
1255 return best;
1256 }
1257
1258 struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
1259 {
1260 struct rb_node *last = rb_last(&cfs_rq->tasks_timeline.rb_root);
1261
1262 if (!last)
1263 return NULL;
1264
1265 return __node_2_se(last);
1266 }
1267
1268 /**************************************************************
1269 * Scheduling class statistics methods:
1270 */
1271 int sched_update_scaling(void)
1272 {
1273 unsigned int factor = get_update_sysctl_factor();
1274
1275 #define WRT_SYSCTL(name) \
1276 (normalized_sysctl_##name = sysctl_##name / (factor))
1277 WRT_SYSCTL(sched_base_slice);
1278 #undef WRT_SYSCTL
1279
1280 return 0;
1281 }
1282
1283 static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se);
1284
1285 /*
1286 * XXX: strictly: vd_i += N*r_i/w_i such that: vd_i > ve_i
1287 * this is probably good enough.
1288 */
1289 static bool update_deadline(struct cfs_rq *cfs_rq, struct sched_entity *se)
1290 {
1291 if (vruntime_cmp(se->vruntime, "<", se->deadline))
1292 return false;
1293
1294 /*
1295 * For EEVDF the virtual time slope is determined by w_i (iow.
1296 * nice) while the request time r_i is determined by
1297 * sysctl_sched_base_slice.
1298 */
1299 if (!se->custom_slice)
1300 se->slice = sysctl_sched_base_slice;
1301
1302 /*
1303 * EEVDF: vd_i = ve_i + r_i / w_i
1304 */
1305 se->deadline = se->vruntime + calc_delta_fair(se->slice, se);
1306 avg_vruntime(cfs_rq);
1307
1308 /*
1309 * The task has consumed its request, reschedule.
1310 */
1311 return true;
1312 }
1313
1314 #include "pelt.h"
1315
1316 static int select_idle_sibling(struct task_struct *p, int prev_cpu, int cpu);
1317 static unsigned long task_h_load(struct task_struct *p);
1318 static unsigned long capacity_of(int cpu);
1319
1320 /* Give new sched_entity start runnable values to heavy its load in infant time */
1321 void init_entity_runnable_average(struct sched_entity *se)
1322 {
1323 struct sched_avg *sa = &se->avg;
1324
1325 memset(sa, 0, sizeof(*sa));
1326
1327 /*
1328 * Tasks are initialized with full load to be seen as heavy tasks until
1329 * they get a chance to stabilize to their real load level.
1330 * Group entities are initialized with zero load to reflect the fact that
1331 * nothing has been attached to the task group yet.
1332 */
1333 if (entity_is_task(se))
1334 sa->load_avg = scale_load_down(se->load.weight);
1335
1336 /* when this task is enqueued, it will contribute to its cfs_rq's load_avg */
1337 }
1338
1339 /*
1340 * With new tasks being created, their initial util_avgs are extrapolated
1341 * based on the cfs_rq's current util_avg:
1342 *
1343 * util_avg = cfs_rq->avg.util_avg / (cfs_rq->avg.load_avg + 1)
1344 * * se_weight(se)
1345 *
1346 * However, in many cases, the above util_avg does not give a desired
1347 * value. Moreover, the sum of the util_avgs may be divergent, such
1348 * as when the series is a harmonic series.
1349 *
1350 * To solve this problem, we also cap the util_avg of successive tasks to
1351 * only 1/2 of the left utilization budget:
1352 *
1353 * util_avg_cap = (cpu_scale - cfs_rq->avg.util_avg) / 2^n
1354 *
1355 * where n denotes the nth task and cpu_scale the CPU capacity.
1356 *
1357 * For example, for a CPU with 1024 of capacity, a simplest series from
1358 * the beginning would be like:
1359 *
1360 * task util_avg: 512, 256, 128, 64, 32, 16, 8, ...
1361 * cfs_rq util_avg: 512, 768, 896, 960, 992, 1008, 1016, ...
1362 *
1363 * Finally, that extrapolated util_avg is clamped to the cap (util_avg_cap)
1364 * if util_avg > util_avg_cap.
1365 */
1366 void post_init_entity_util_avg(struct task_struct *p)
1367 {
1368 struct sched_entity *se = &p->se;
1369 struct cfs_rq *cfs_rq = cfs_rq_of(se);
1370 struct sched_avg *sa = &se->avg;
1371 long cpu_scale = arch_scale_cpu_capacity(cpu_of(rq_of(cfs_rq)));
1372 long cap = (long)(cpu_scale - cfs_rq->avg.util_avg) / 2;
1373
1374 if (p->sched_class != &fair_sched_class) {
1375 /*
1376 * For !fair tasks do:
1377 *
1378 update_cfs_rq_load_avg(now, cfs_rq);
1379 attach_entity_load_avg(cfs_rq, se);
1380 switched_from_fair(rq, p);
1381 *
1382 * such that the next switched_to_fair() has the
1383 * expected state.
1384 */
1385 se->avg.last_update_time = cfs_rq_clock_pelt(cfs_rq);
1386 return;
1387 }
1388
1389 if (cap > 0) {
1390 if (cfs_rq->avg.util_avg != 0) {
1391 sa->util_avg = cfs_rq->avg.util_avg * se_weight(se);
1392 sa->util_avg /= (cfs_rq->avg.load_avg + 1);
1393
1394 if (sa->util_avg > cap)
1395 sa->util_avg = cap;
1396 } else {
1397 sa->util_avg = cap;
1398 }
1399 }
1400
1401 sa->runnable_avg = sa->util_avg;
1402 }
1403
1404 static inline void account_mm_sched(struct rq *rq, struct task_struct *p, s64 delta_exec);
1405
1406 static s64 update_se(struct rq *rq, struct sched_entity *se)
1407 {
1408 u64 now = rq_clock_task(rq);
1409 s64 delta_exec;
1410
1411 delta_exec = now - se->exec_start;
1412 if (unlikely(delta_exec <= 0))
1413 return delta_exec;
1414
1415 se->exec_start = now;
1416 if (entity_is_task(se)) {
1417 struct task_struct *running = rq->curr;
1418 /*
1419 * If se is a task, we account the time against the running
1420 * task, as w/ proxy-exec they may not be the same.
1421 */
1422 running->se.exec_start = now;
1423 running->se.sum_exec_runtime += delta_exec;
1424
1425 trace_sched_stat_runtime(running, delta_exec);
1426 account_group_exec_runtime(running, delta_exec);
1427 account_mm_sched(rq, running, delta_exec);
1428
1429 cgroup_account_cputime(running, delta_exec);
1430 } else {
1431 /* If not task, account the time against donor se */
1432 se->sum_exec_runtime += delta_exec;
1433 }
1434
1435 if (schedstat_enabled()) {
1436 struct sched_statistics *stats;
1437
1438 stats = __schedstats_from_se(se);
1439 __schedstat_set(stats->exec_max,
1440 max(delta_exec, stats->exec_max));
1441 }
1442
1443 return delta_exec;
1444 }
1445
1446 #ifdef CONFIG_SCHED_CACHE
1447
1448 /*
1449 * XXX numbers come from a place the sun don't shine -- probably wants to be SD
1450 * tunable or so.
1451 */
1452 #define EPOCH_PERIOD (HZ / 100) /* 10 ms */
1453 #define EPOCH_LLC_AFFINITY_TIMEOUT 5 /* 50 ms */
1454 __read_mostly unsigned int llc_aggr_tolerance = 1;
1455 __read_mostly unsigned int llc_epoch_period = EPOCH_PERIOD;
1456 __read_mostly unsigned int llc_epoch_affinity_timeout = EPOCH_LLC_AFFINITY_TIMEOUT;
1457 __read_mostly unsigned int llc_imb_pct = 20;
1458 __read_mostly unsigned int llc_overaggr_pct = 50;
1459
1460 static int llc_id(int cpu)
1461 {
1462 if (cpu < 0)
1463 return -1;
1464
1465 return per_cpu(sd_llc_id, cpu);
1466 }
1467
1468 static inline int get_sched_cache_scale(int mul)
1469 {
1470 unsigned int tol = READ_ONCE(llc_aggr_tolerance);
1471
1472 if (!tol)
1473 return 0;
1474
1475 if (tol >= 100)
1476 return INT_MAX;
1477
1478 return (1 + (tol - 1) * mul);
1479 }
1480
1481 static bool exceed_llc_capacity(struct sched_cache_group *grp, int cpu)
1482 {
1483 #ifdef CONFIG_NUMA_BALANCING
1484 unsigned long llc, footprint;
1485 struct sched_domain *sd;
1486 int scale;
1487
1488 guard(rcu)();
1489
1490 sd = rcu_dereference_sched_domain(cpu_rq(cpu)->sd);
1491 if (!sd)
1492 return true;
1493
1494 if (static_branch_likely(&sched_numa_balancing)) {
1495 /*
1496 * TBD: RDT exclusive LLC ways reserved should be
1497 * excluded.
1498 */
1499 llc = sd->llc_bytes;
1500 footprint = READ_ONCE(grp->footprint);
1501
1502 /*
1503 * Scale the LLC size by 256*llc_aggr_tolerance
1504 * and compare it to the task's footprint.
1505 *
1506 * Suppose the L3 size is 32MB. If the
1507 * llc_aggr_tolerance is 1:
1508 * When the footprint is larger than 32MB, the
1509 * process is regarded as exceeding the LLC
1510 * capacity. If the llc_aggr_tolerance is 99:
1511 * When the footprint is larger than 784GB, the
1512 * process is regarded as exceeding the LLC
1513 * capacity:
1514 * 784GB = (1 + (99 - 1) * 256) * 32MB
1515 * If the llc_aggr_tolerance is 100:
1516 * ignore the footprint and do the aggregation
1517 * anyway.
1518 */
1519 scale = get_sched_cache_scale(256);
1520 if (scale == INT_MAX)
1521 return false;
1522
1523 return ((llc * (u64)scale) < (footprint * PAGE_SIZE));
1524 }
1525 #endif
1526 return false;
1527 }
1528
1529 static bool invalid_llc_nr(struct sched_cache_group *grp, struct task_struct *p,
1530 int cpu)
1531 {
1532 int scale;
1533
1534 if (get_nr_threads(p) <= 1)
1535 return true;
1536
1537 /*
1538 * Scale the number of 'cores' in a LLC by llc_aggr_tolerance
1539 * and compare it to the task's active threads.
1540 */
1541 scale = get_sched_cache_scale(1);
1542 if (scale == INT_MAX)
1543 return false;
1544
1545 return !fits_capacity((READ_ONCE(grp->nr_running_avg) * cpu_smt_num_threads),
1546 (scale * per_cpu(sd_llc_size, cpu)));
1547 }
1548
1549 /*
1550 * A task counts in nr_pref_llc_running while it is queued on its preferred
1551 * LLC (pref_llc_queued) and runnable (!sched_delayed), keeping the counter in
1552 * the runnable domain so alb_break_llc() can compare it with h_nr_runnable.
1553 */
1554 static bool task_pref_llc_runnable(struct task_struct *p)
1555 {
1556 return p->pref_llc_queued && !p->se.sched_delayed;
1557 }
1558
1559 static void pref_llc_running_inc(struct rq *rq, struct task_struct *p)
1560 {
1561 if (task_pref_llc_runnable(p))
1562 rq->nr_pref_llc_running++;
1563 }
1564
1565 static void pref_llc_running_dec(struct rq *rq, struct task_struct *p)
1566 {
1567 if (task_pref_llc_runnable(p))
1568 rq->nr_pref_llc_running--;
1569 }
1570
1571 static void account_llc_enqueue(struct rq *rq, struct task_struct *p)
1572 {
1573 int pref_llc, pref_llc_queued;
1574 struct sched_domain *sd;
1575
1576 pref_llc = p->preferred_llc;
1577 if (pref_llc < 0)
1578 return;
1579
1580 pref_llc_queued = (pref_llc == task_llc(p));
1581 rq->nr_llc_running++;
1582
1583 /*
1584 * Record whether p is enqueued on its preferred
1585 * LLC, in order to pair with account_llc_dequeue()
1586 * to maintain a consistent nr_pref_llc_running per
1587 * runqueue.
1588 * This is necessary because a race condition exists:
1589 * after a task is enqueued on a runqueue, task_llc(p)
1590 * may change due to CPU hotplug. Therefore, checking
1591 * task_llc(p) to determine whether the task is being
1592 * dequeued from its preferred LLC is unreliable and
1593 * can cause inconsistent values - checking the
1594 * p->pref_llc_queued in account_llc_dequeue() would
1595 * be reliable.
1596 */
1597 p->pref_llc_queued = pref_llc_queued;
1598
1599 /* Skipped while delayed; clear_delayed() adds it back on wake. */
1600 pref_llc_running_inc(rq, p);
1601
1602 sd = rcu_dereference_all(rq->sd);
1603 if (sd && (unsigned int)pref_llc < sd->llc_max)
1604 sd->llc_counts[pref_llc]++;
1605 }
1606
1607 static void account_llc_dequeue(struct rq *rq, struct task_struct *p)
1608 {
1609 struct sched_domain *sd;
1610 int pref_llc;
1611
1612 pref_llc = p->preferred_llc;
1613 if (pref_llc < 0)
1614 return;
1615
1616 rq->nr_llc_running--;
1617 if (p->pref_llc_queued) {
1618 /*
1619 * Skipped if still delayed (set_delayed() already removed it);
1620 * clearing pref_llc_queued below also stops clear_delayed()
1621 * from re-adding it.
1622 */
1623 pref_llc_running_dec(rq, p);
1624 /*
1625 * Update the status in case
1626 * other logic might query
1627 * this.
1628 */
1629 p->pref_llc_queued = 0;
1630 }
1631
1632 sd = rcu_dereference_all(rq->sd);
1633 if (sd && (unsigned int)pref_llc < sd->llc_max) {
1634 /*
1635 * There is a race condition between dequeue
1636 * and CPU hotplug. After a task has been enqueued
1637 * on CPUx, a CPU hotplug event occurs, and all online
1638 * CPUs (including CPUx) rebuild their sched_domains
1639 * and reset statistics to zero(including sd->llc_counts).
1640 * This can cause temporary undercount and we have to
1641 * check for such underflow in sd->llc_counts.
1642 *
1643 * This undercount is temporary and accurate accounting
1644 * will resume once the rq has a chance to be idle.
1645 */
1646 if (sd->llc_counts[pref_llc])
1647 sd->llc_counts[pref_llc]--;
1648 }
1649 }
1650
1651 int mm_init_sched(struct mm_struct *mm,
1652 struct sched_cache_time __percpu *_pcpu_sched)
1653 {
1654 struct sched_cache_group *grp;
1655 unsigned long epoch = 0;
1656 int i;
1657
1658 grp = kzalloc_obj(*grp);
1659 if (!grp) {
1660 free_percpu(_pcpu_sched);
1661 mm->sched_cache_grp = NULL;
1662 return -ENOMEM;
1663 }
1664
1665 for_each_possible_cpu(i) {
1666 struct sched_cache_time *pcpu_sched = per_cpu_ptr(_pcpu_sched, i);
1667 struct rq *rq = cpu_rq(i);
1668
1669 pcpu_sched->runtime = 0;
1670 /* a slightly stale cpu epoch is acceptible */
1671 pcpu_sched->epoch = rq->cpu_epoch;
1672 epoch = rq->cpu_epoch;
1673 }
1674
1675 raw_spin_lock_init(&grp->lock);
1676 grp->epoch = epoch;
1677 grp->cpu = -1;
1678 grp->next_scan = jiffies;
1679 grp->nr_running_avg = 0;
1680 grp->footprint = 0;
1681 refcount_set(&grp->refcnt, 1);
1682 /*
1683 * The update to grp->pcpu_sched should not be reordered
1684 * before initialization to grp's other fields, in case
1685 * the readers may get invalid mm_sched_epoch, etc.
1686 */
1687 smp_store_release(&grp->pcpu_sched, _pcpu_sched);
1688 /*
1689 * Publish the group last. Not every reader qualifies it by
1690 * grp->pcpu_sched - can_migrate_llc_task() only checks that the
1691 * pointer is non-NULL before reading grp->footprint and
1692 * grp->nr_running_avg - so a reachable group must already be
1693 * fully initialized.
1694 */
1695 smp_store_release(&mm->sched_cache_grp, grp);
1696 return 0;
1697 }
1698
1699 static void sched_cache_group_free_rcu(struct rcu_head *rcu)
1700 {
1701 struct sched_cache_group *grp =
1702 container_of(rcu, struct sched_cache_group, rcu);
1703
1704 free_percpu(grp->pcpu_sched);
1705 kfree(grp);
1706 }
1707
1708 static void sched_cache_group_put(struct sched_cache_group *grp)
1709 {
1710 if (!grp || !refcount_dec_and_test(&grp->refcnt))
1711 return;
1712
1713 call_rcu(&grp->rcu, sched_cache_group_free_rcu);
1714 }
1715
1716 DEFINE_FREE(sched_cache_group_put, struct sched_cache_group *,
1717 sched_cache_group_put(_T));
1718
1719 #define rcu_deref_sched_cache_grp(tsk) \
1720 rcu_dereference_check((tsk)->sched_cache_grp, (tsk) == current)
1721
1722 static struct sched_cache_group *sched_cache_replace_grp(struct task_struct *p,
1723 struct sched_cache_group *new)
1724 {
1725 struct sched_cache_group *old;
1726
1727 old = rcu_deref_sched_cache_grp(p);
1728 rcu_assign_pointer(p->sched_cache_grp, new);
1729
1730 return old;
1731 }
1732
1733 struct sched_cache_group *sched_cache_group_get(struct sched_cache_group *grp)
1734 {
1735 /*
1736 * refcount_inc_not_zero() is the acquire primitive for lockless
1737 * (RCU) lookups; plain refcount_inc() would scribble the count if
1738 * it already reached zero. Return NULL in that case.
1739 */
1740 if (grp && !refcount_inc_not_zero(&grp->refcnt))
1741 grp = NULL;
1742
1743 return grp;
1744 }
1745
1746 struct sched_cache_group *task_cache_group_get(struct task_struct *p)
1747 {
1748 guard(rcu)();
1749 return sched_cache_group_get(rcu_dereference(p->sched_cache_grp));
1750 }
1751
1752 void sched_cache_fork(struct task_struct *p)
1753 {
1754 /*
1755 * The child takes its own reference on the mm's cache group, separate
1756 * from the reference held by the mm. @p is not yet visible to readers,
1757 * so a plain initializing store is enough.
1758 */
1759 RCU_INIT_POINTER(p->sched_cache_grp,
1760 sched_cache_group_get(p->mm->sched_cache_grp));
1761 }
1762
1763 void sched_cache_fork_cleanup(struct task_struct *p)
1764 {
1765 /*
1766 * A fork that fails after sched_cache_fork() never reaches exit_mm(),
1767 * so drop the reference here. @p never became visible, so there are no
1768 * concurrent readers and the reference we hold keeps the group alive.
1769 */
1770 sched_cache_group_put(rcu_access_pointer(p->sched_cache_grp));
1771 RCU_INIT_POINTER(p->sched_cache_grp, NULL);
1772 }
1773
1774 void sched_cache_exec_mmap(struct task_struct *p, struct mm_struct *mm)
1775 {
1776 struct sched_cache_group *old;
1777
1778 /*
1779 * Acquire the new reference before publishing the pointer, then drop
1780 * the old one. @p is current and the only writer of its own pointer.
1781 */
1782 old = sched_cache_replace_grp(p, sched_cache_group_get(mm->sched_cache_grp));
1783 sched_cache_group_put(old);
1784 }
1785
1786 void sched_cache_exit_mm(struct task_struct *p)
1787 {
1788 struct sched_cache_group *grp = sched_cache_replace_grp(p, NULL);
1789
1790 #ifdef CONFIG_NUMA_BALANCING
1791 /*
1792 * Subtract this task's footprint from the group before dropping the
1793 * reference, so the group footprint converges as its threads exit.
1794 * Unlocked for performance; clamp to avoid underflow.
1795 */
1796 if (grp && p->total_numa_faults) {
1797 unsigned long fp = READ_ONCE(grp->footprint);
1798 unsigned long sub = min(fp, p->total_numa_faults);
1799
1800 WRITE_ONCE(grp->footprint, fp - sub);
1801 }
1802 #endif
1803 sched_cache_group_put(grp);
1804 }
1805
1806 void mm_destroy_sched(struct mm_struct *mm)
1807 {
1808 sched_cache_group_put(mm->sched_cache_grp);
1809 mm->sched_cache_grp = NULL;
1810 }
1811
1812 /* because why would C be fully specified */
1813 static __always_inline void __shr_u64(u64 *val, unsigned int n)
1814 {
1815 if (n >= 64) {
1816 *val = 0;
1817 return;
1818 }
1819 *val >>= n;
1820 }
1821
1822 static inline void __update_mm_sched(struct rq *rq,
1823 struct sched_cache_time *pcpu_sched)
1824 {
1825 lockdep_assert_held(&rq->cpu_epoch_lock);
1826
1827 unsigned int period = max(READ_ONCE(llc_epoch_period), 1U);
1828 unsigned long n, now = jiffies;
1829 long delta = now - rq->cpu_epoch_next;
1830
1831 if (delta > 0) {
1832 n = (delta + period - 1) / period;
1833 rq->cpu_epoch += n;
1834 rq->cpu_epoch_next += n * period;
1835 __shr_u64(&rq->cpu_runtime, n);
1836 }
1837
1838 n = rq->cpu_epoch - pcpu_sched->epoch;
1839 if (n) {
1840 pcpu_sched->epoch += n;
1841 __shr_u64(&pcpu_sched->runtime, n);
1842 }
1843 }
1844
1845 static unsigned long fraction_mm_sched(struct rq *rq,
1846 struct sched_cache_time *pcpu_sched)
1847 {
1848 guard(raw_spinlock_irqsave)(&rq->cpu_epoch_lock);
1849
1850 __update_mm_sched(rq, pcpu_sched);
1851
1852 /*
1853 * Runtime is a geometric series (r=0.5) and as such will sum to twice
1854 * the accumulation period, this means the multiplcation here should
1855 * not overflow.
1856 */
1857 return div64_u64(NICE_0_LOAD * pcpu_sched->runtime, rq->cpu_runtime + 1);
1858 }
1859
1860 static int get_pref_llc(struct task_struct *p, struct sched_cache_group *grp)
1861 {
1862 int mm_sched_llc = -1, mm_sched_cpu;
1863
1864 if (!grp)
1865 return -1;
1866
1867 mm_sched_cpu = READ_ONCE(grp->cpu);
1868 if (mm_sched_cpu != -1) {
1869 mm_sched_llc = llc_id(mm_sched_cpu);
1870
1871 #ifdef CONFIG_NUMA_BALANCING
1872 /*
1873 * Don't assign preferred LLC if it
1874 * conflicts with NUMA balancing.
1875 * This can happen when sched_setnuma() gets
1876 * called, however it is not much of an issue
1877 * because we expect account_mm_sched() to get
1878 * called fairly regularly -- at a higher rate
1879 * than sched_setnuma() at least -- and thus the
1880 * conflict only exists for a short period of time.
1881 */
1882 if (static_branch_likely(&sched_numa_balancing) &&
1883 p->numa_preferred_nid >= 0 &&
1884 cpu_to_node(mm_sched_cpu) != p->numa_preferred_nid)
1885 mm_sched_llc = -1;
1886 #endif
1887 }
1888
1889 return mm_sched_llc;
1890 }
1891
1892 static unsigned int task_running_on_cpu(int cpu, struct task_struct *p);
1893
1894 static inline
1895 void account_mm_sched(struct rq *rq, struct task_struct *p, s64 delta_exec)
1896 {
1897 struct sched_cache_group *grp = rcu_dereference_all(p->sched_cache_grp);
1898 struct sched_cache_time *pcpu_sched;
1899 int mm_sched_llc = -1;
1900 unsigned long epoch;
1901
1902 if (!sched_cache_enabled())
1903 return;
1904
1905 if (p->sched_class != &fair_sched_class)
1906 return;
1907 /*
1908 * init_task, kthreads and user thread created
1909 * by user_mode_thread() don't have a cache group.
1910 * In theory a kernel thread does not have any valid
1911 * cache group, because sched_cache_fork() is not
1912 * invoked for a kernel thread - !grp should gate the
1913 * kernel thread. Use the PF_KTHREAD check explicitly
1914 * here for safety reasons, to guard against future
1915 * modifications and to pair with task_tick_cache().
1916 */
1917 if (p->flags & PF_KTHREAD || !grp || !grp->pcpu_sched)
1918 return;
1919
1920 pcpu_sched = per_cpu_ptr(grp->pcpu_sched, cpu_of(rq));
1921
1922 scoped_guard (raw_spinlock, &rq->cpu_epoch_lock) {
1923 __update_mm_sched(rq, pcpu_sched);
1924 pcpu_sched->runtime += delta_exec;
1925 rq->cpu_runtime += delta_exec;
1926 epoch = rq->cpu_epoch;
1927 }
1928
1929 /*
1930 * If this process hasn't hit task_cache_work() for a while invalidate
1931 * its preferred state.
1932 */
1933 if ((long)(epoch - READ_ONCE(grp->epoch)) > llc_epoch_affinity_timeout ||
1934 invalid_llc_nr(grp, p, cpu_of(rq)) ||
1935 exceed_llc_capacity(grp, cpu_of(rq))) {
1936 if (READ_ONCE(grp->cpu) != -1)
1937 WRITE_ONCE(grp->cpu, -1);
1938 }
1939
1940 mm_sched_llc = get_pref_llc(p, grp);
1941
1942 /* task not on rq accounted later in account_entity_enqueue() */
1943 if (task_running_on_cpu(rq->cpu, p) &&
1944 READ_ONCE(p->preferred_llc) != mm_sched_llc) {
1945 account_llc_dequeue(rq, p);
1946 WRITE_ONCE(p->preferred_llc, mm_sched_llc);
1947 account_llc_enqueue(rq, p);
1948 }
1949 }
1950
1951 static void task_tick_cache(struct rq *rq, struct task_struct *p)
1952 {
1953 struct sched_cache_group *grp = rcu_dereference_all(p->sched_cache_grp);
1954 struct callback_head *work = &p->cache_work;
1955 unsigned long epoch;
1956
1957 if (!sched_cache_enabled())
1958 return;
1959
1960 if (!grp || p->flags & PF_KTHREAD ||
1961 !grp->pcpu_sched)
1962 return;
1963
1964 epoch = rq->cpu_epoch;
1965 /* avoid moving backwards */
1966 if (time_after_eq(grp->epoch, epoch))
1967 return;
1968
1969 guard(raw_spinlock)(&grp->lock);
1970
1971 if (work->next == work) {
1972 task_work_add(p, work, TWA_RESUME);
1973 WRITE_ONCE(grp->epoch, epoch);
1974 }
1975 }
1976
1977 static void get_scan_cpumasks(cpumask_var_t cpus, struct task_struct *p,
1978 struct sched_cache_group *grp)
1979 {
1980 #ifdef CONFIG_NUMA_BALANCING
1981 int cpu, curr_cpu, nid, pref_nid;
1982
1983 if (!static_branch_likely(&sched_numa_balancing))
1984 goto out;
1985
1986 cpu = READ_ONCE(grp->cpu);
1987 if (cpu != -1)
1988 nid = cpu_to_node(cpu);
1989 curr_cpu = task_cpu(p);
1990
1991 /*
1992 * Scanning in the preferred NUMA node is ideal. However, the NUMA
1993 * preferred node is per-task rather than per-process. It is possible
1994 * for different threads of the process to have distinct preferred
1995 * nodes; consequently, the process-wide preferred LLC may bounce
1996 * between different nodes. As a workaround, maintain the scan
1997 * CPU mask to also cover the process's current preferred LLC and the
1998 * current running node to mitigate the bouncing risk.
1999 * TBD: numa_group should be considered during task aggregation.
2000 */
2001 pref_nid = p->numa_preferred_nid;
2002 /* honor the task's preferred node */
2003 if (pref_nid == NUMA_NO_NODE)
2004 goto out;
2005
2006 cpumask_or(cpus, cpus, cpumask_of_node(pref_nid));
2007
2008 /* honor the task's preferred LLC CPU */
2009 if (cpu != -1 && !cpumask_test_cpu(cpu, cpus) && nid != NUMA_NO_NODE)
2010 cpumask_or(cpus, cpus, cpumask_of_node(nid));
2011
2012 /* make sure the task's current running node is included */
2013 if (!cpumask_test_cpu(curr_cpu, cpus))
2014 cpumask_or(cpus, cpus, cpumask_of_node(cpu_to_node(curr_cpu)));
2015
2016 return;
2017
2018 out:
2019 #endif
2020 cpumask_copy(cpus, cpu_online_mask);
2021 }
2022
2023 static inline void update_avg_scale(u64 *avg, u64 sample)
2024 {
2025 int factor = per_cpu(sd_llc_size, raw_smp_processor_id());
2026 s64 diff = sample - *avg;
2027 u32 divisor;
2028
2029 /*
2030 * Scale the divisor based on the number of CPUs contained
2031 * in the LLC. This scaling ensures smaller LLC domains use
2032 * a smaller divisor to achieve more precise sensitivity to
2033 * changes in nr_running, while larger LLC domains are capped
2034 * at a maximum divisor of 8 which is the default smoothing
2035 * factor of EWMA in update_avg().
2036 */
2037 divisor = clamp_t(u32, (factor >> 2), 2, 8);
2038 *avg += div64_s64(diff, divisor);
2039 }
2040
2041 static void task_cache_work(struct callback_head *work)
2042 {
2043 struct sched_cache_group *grp __free(sched_cache_group_put) = NULL;
2044 cpumask_var_t cpus __free(free_cpumask_var) = CPUMASK_VAR_NULL;
2045 int cpu, m_a_cpu = -1, nr_running = 0, curr_cpu;
2046 unsigned long next_scan, now = jiffies;
2047 struct task_struct *p = current, *cur;
2048 unsigned long curr_m_a_occ = 0;
2049 unsigned long m_a_occ = 0;
2050
2051 WARN_ON_ONCE(work != &p->cache_work);
2052
2053 work->next = work;
2054
2055 if (p->flags & PF_EXITING)
2056 return;
2057
2058 /*
2059 * A reference makes sure grp is not released by others. The rcu
2060 * lock can not be held till after zalloc_cpumask_var() below,
2061 * because the latter might sleep.
2062 */
2063 grp = task_cache_group_get(p);
2064 if (!grp)
2065 return;
2066
2067 next_scan = READ_ONCE(grp->next_scan);
2068 if (time_before(now, next_scan))
2069 return;
2070
2071 /* only 1 thread is allowed to scan */
2072 if (!try_cmpxchg(&grp->next_scan, &next_scan,
2073 now + max_t(unsigned long,
2074 READ_ONCE(llc_epoch_period), 1)))
2075 return;
2076
2077 curr_cpu = task_cpu(p);
2078 if (invalid_llc_nr(grp, p, curr_cpu) ||
2079 exceed_llc_capacity(grp, curr_cpu)) {
2080 if (READ_ONCE(grp->cpu) != -1)
2081 WRITE_ONCE(grp->cpu, -1);
2082
2083 return;
2084 }
2085
2086 if (!zalloc_cpumask_var(&cpus, GFP_KERNEL))
2087 return;
2088
2089 scoped_guard (cpus_read_lock) {
2090 guard(rcu)();
2091
2092 get_scan_cpumasks(cpus, p, grp);
2093
2094 for_each_cpu(cpu, cpus) {
2095 /* XXX sched_cluster_active */
2096 struct sched_domain *sd = rcu_dereference_all(per_cpu(sd_llc, cpu));
2097 unsigned long occ, m_occ = 0, a_occ = 0;
2098 int m_cpu = -1, i;
2099
2100 if (!sd)
2101 continue;
2102
2103 for_each_cpu(i, sched_domain_span(sd)) {
2104 occ = fraction_mm_sched(cpu_rq(i),
2105 per_cpu_ptr(grp->pcpu_sched, i));
2106 a_occ += occ;
2107 if (occ > m_occ) {
2108 m_occ = occ;
2109 m_cpu = i;
2110 }
2111
2112 /*
2113 * rcu_access_pointer() is used because the
2114 * pointer is only compared, never dereferenced.
2115 */
2116 cur = rcu_dereference_all(cpu_rq(i)->curr);
2117 if (cur && !(cur->flags & (PF_EXITING | PF_KTHREAD)) &&
2118 rcu_access_pointer(cur->sched_cache_grp) == grp)
2119 nr_running++;
2120 }
2121
2122 /*
2123 * Compare the accumulated occupancy of each LLC. The
2124 * reason for using accumulated occupancy rather than average
2125 * per CPU occupancy is that it works better in asymmetric LLC
2126 * scenarios.
2127 * For example, if there are 2 threads in a 4CPU LLC and 3
2128 * threads in an 8CPU LLC, it might be better to choose the one
2129 * with 3 threads. However, this would not be the case if the
2130 * occupancy is divided by the number of CPUs in an LLC (i.e.,
2131 * if average per CPU occupancy is used).
2132 * Besides, NUMA balancing fault statistics behave similarly:
2133 * the total number of faults per node is compared rather than
2134 * the average number of faults per CPU. This strategy is also
2135 * followed here.
2136 */
2137 if (a_occ > m_a_occ) {
2138 m_a_occ = a_occ;
2139 m_a_cpu = m_cpu;
2140 }
2141
2142 if (llc_id(cpu) == llc_id(READ_ONCE(grp->cpu)))
2143 curr_m_a_occ = a_occ;
2144
2145 cpumask_andnot(cpus, cpus, sched_domain_span(sd));
2146 }
2147 }
2148
2149 if (m_a_occ > (2 * curr_m_a_occ)) {
2150 /*
2151 * Avoid switching sched_cache_grp->cpu too fast.
2152 * The reason to choose 2X is because:
2153 * 1. It is better to keep the preferred LLC stable,
2154 * rather than changing it frequently and cause migrations
2155 * 2. 2X means the new preferred LLC has at least 1 more
2156 * busy CPU than the old one(200% vs 100%, eg)
2157 * 3. 2X is chosen based on test results, as it delivers
2158 * the optimal performance gain so far.
2159 */
2160 WRITE_ONCE(grp->cpu, m_a_cpu);
2161 }
2162
2163 update_avg_scale(&grp->nr_running_avg, nr_running);
2164 }
2165
2166 void init_sched_mm(struct task_struct *p)
2167 {
2168 struct callback_head *work = &p->cache_work;
2169
2170 init_task_work(work, task_cache_work);
2171 work->next = work;
2172 /*
2173 * dup_task_struct() copies the parent's task_struct, including its
2174 * sched_cache_grp, for which the child holds no reference. Clear it
2175 * here - before copy_mm() runs - so the child never carries a
2176 * borrowed pointer that the fork error path would put.
2177 */
2178 RCU_INIT_POINTER(p->sched_cache_grp, NULL);
2179 /*
2180 * Reset new task's preference to avoid
2181 * polluting account_llc_enqueue().
2182 */
2183 p->preferred_llc = -1;
2184 p->pref_llc_queued = 0;
2185 }
2186
2187 #else /* CONFIG_SCHED_CACHE */
2188
2189 static inline void account_mm_sched(struct rq *rq, struct task_struct *p,
2190 s64 delta_exec) { }
2191
2192 void init_sched_mm(struct task_struct *p) { }
2193
2194 static void task_tick_cache(struct rq *rq, struct task_struct *p) { }
2195
2196 static inline int get_pref_llc(struct task_struct *p,
2197 struct mm_struct *mm)
2198 {
2199 return -1;
2200 }
2201
2202 static void account_llc_enqueue(struct rq *rq, struct task_struct *p) {}
2203
2204 static void account_llc_dequeue(struct rq *rq, struct task_struct *p) {}
2205
2206 static void pref_llc_running_inc(struct rq *rq, struct task_struct *p) {}
2207
2208 static void pref_llc_running_dec(struct rq *rq, struct task_struct *p) {}
2209
2210 #endif /* CONFIG_SCHED_CACHE */
2211
2212 /*
2213 * Used by other classes to account runtime.
2214 */
2215 s64 update_curr_common(struct rq *rq)
2216 {
2217 return update_se(rq, &rq->donor->se);
2218 }
2219
2220 /*
2221 * Update the current task's runtime statistics.
2222 */
2223 static void update_curr(struct cfs_rq *cfs_rq)
2224 {
2225 /*
2226 * Note: cfs_rq->curr corresponds to the task picked to
2227 * run (ie: rq->donor.se) which due to proxy-exec may
2228 * not necessarily be the actual task running
2229 * (rq->curr.se). This is easy to confuse!
2230 */
2231 struct sched_entity *curr = cfs_rq->h_curr;
2232 struct rq *rq = rq_of(cfs_rq);
2233 s64 delta_exec;
2234 bool resched;
2235
2236 if (unlikely(!curr))
2237 return;
2238
2239 delta_exec = update_se(rq, curr);
2240 if (unlikely(delta_exec <= 0))
2241 return;
2242
2243 account_cfs_rq_runtime(cfs_rq, delta_exec);
2244
2245 if (!entity_is_task(curr))
2246 return;
2247
2248 cfs_rq = &rq->cfs;
2249
2250 curr->vruntime += calc_delta_fair(delta_exec, curr);
2251 resched = update_deadline(cfs_rq, curr);
2252
2253 /*
2254 * If the fair_server is active, we need to account for the
2255 * fair_server time whether or not the task is running on
2256 * behalf of fair_server or not:
2257 * - If the task is running on behalf of fair_server, we need
2258 * to limit its time based on the assigned runtime.
2259 * - Fair task that runs outside of fair_server should account
2260 * against fair_server such that it can account for this time
2261 * and possibly avoid running this period.
2262 */
2263 dl_server_update(&rq->fair_server, delta_exec);
2264
2265 if (cfs_rq->h_nr_queued == 1)
2266 return;
2267
2268 if (resched || !protect_slice(curr)) {
2269 resched_curr_lazy(rq);
2270 clear_buddies(cfs_rq, curr);
2271 }
2272 }
2273
2274 static void update_curr_fair(struct rq *rq)
2275 {
2276 struct sched_entity *se = &rq->donor->se;
2277
2278 for_each_sched_entity(se)
2279 update_curr(cfs_rq_of(se));
2280 }
2281
2282 static inline void
2283 update_stats_wait_start_fair(struct cfs_rq *cfs_rq, struct sched_entity *se)
2284 {
2285 struct sched_statistics *stats;
2286 struct task_struct *p = NULL;
2287
2288 if (!schedstat_enabled())
2289 return;
2290
2291 stats = __schedstats_from_se(se);
2292
2293 if (entity_is_task(se))
2294 p = task_of(se);
2295
2296 __update_stats_wait_start(rq_of(cfs_rq), p, stats);
2297 }
2298
2299 static inline void
2300 update_stats_wait_end_fair(struct cfs_rq *cfs_rq, struct sched_entity *se)
2301 {
2302 struct sched_statistics *stats;
2303 struct task_struct *p = NULL;
2304
2305 if (!schedstat_enabled())
2306 return;
2307
2308 stats = __schedstats_from_se(se);
2309
2310 /*
2311 * When the sched_schedstat changes from 0 to 1, some sched se
2312 * maybe already in the runqueue, the se->statistics.wait_start
2313 * will be 0.So it will let the delta wrong. We need to avoid this
2314 * scenario.
2315 */
2316 if (unlikely(!schedstat_val(stats->wait_start)))
2317 return;
2318
2319 if (entity_is_task(se))
2320 p = task_of(se);
2321
2322 __update_stats_wait_end(rq_of(cfs_rq), p, stats);
2323 }
2324
2325 static inline void
2326 update_stats_enqueue_sleeper_fair(struct cfs_rq *cfs_rq, struct sched_entity *se)
2327 {
2328 struct sched_statistics *stats;
2329 struct task_struct *tsk = NULL;
2330
2331 if (!schedstat_enabled())
2332 return;
2333
2334 stats = __schedstats_from_se(se);
2335
2336 if (entity_is_task(se))
2337 tsk = task_of(se);
2338
2339 __update_stats_enqueue_sleeper(rq_of(cfs_rq), tsk, stats);
2340 }
2341
2342 /*
2343 * Task is being enqueued - update stats:
2344 */
2345 static inline void
2346 update_stats_enqueue_fair(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
2347 {
2348 if (!schedstat_enabled())
2349 return;
2350
2351 /*
2352 * Are we enqueueing a waiting task? (for current tasks
2353 * a dequeue/enqueue event is a NOP)
2354 */
2355 if (se != cfs_rq->h_curr)
2356 update_stats_wait_start_fair(cfs_rq, se);
2357
2358 if (flags & ENQUEUE_WAKEUP)
2359 update_stats_enqueue_sleeper_fair(cfs_rq, se);
2360 }
2361
2362 static inline void
2363 update_stats_dequeue_fair(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
2364 {
2365
2366 if (!schedstat_enabled())
2367 return;
2368
2369 /*
2370 * Mark the end of the wait period if dequeueing a
2371 * waiting task:
2372 */
2373 if (se != cfs_rq->h_curr)
2374 update_stats_wait_end_fair(cfs_rq, se);
2375
2376 if ((flags & DEQUEUE_SLEEP) && entity_is_task(se)) {
2377 struct task_struct *tsk = task_of(se);
2378 unsigned int state;
2379
2380 /* XXX racy against TTWU */
2381 state = READ_ONCE(tsk->__state);
2382 if (state & TASK_INTERRUPTIBLE)
2383 __schedstat_set(tsk->stats.sleep_start,
2384 rq_clock(rq_of(cfs_rq)));
2385 if (state & TASK_UNINTERRUPTIBLE)
2386 __schedstat_set(tsk->stats.block_start,
2387 rq_clock(rq_of(cfs_rq)));
2388 }
2389 }
2390
2391 /*
2392 * We are picking a new current task - update its stats:
2393 */
2394 static inline void
2395 update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
2396 {
2397 /*
2398 * We are starting a new run period:
2399 */
2400 se->exec_start = rq_clock_task(rq_of(cfs_rq));
2401 }
2402
2403 /* Check sched_smt_active before calling this to avoid overheads in fastpaths */
2404 static inline bool is_core_idle(int cpu)
2405 {
2406 int sibling;
2407
2408 for_each_cpu(sibling, cpu_smt_mask(cpu)) {
2409 if (cpu == sibling)
2410 continue;
2411
2412 if (!idle_cpu(sibling))
2413 return false;
2414 }
2415
2416 return true;
2417 }
2418
2419 #ifdef CONFIG_NUMA
2420 #define NUMA_IMBALANCE_MIN 2
2421
2422 static inline long
2423 adjust_numa_imbalance(int imbalance, int dst_running, int imb_numa_nr)
2424 {
2425 /*
2426 * Allow a NUMA imbalance if busy CPUs is less than the maximum
2427 * threshold. Above this threshold, individual tasks may be contending
2428 * for both memory bandwidth and any shared HT resources. This is an
2429 * approximation as the number of running tasks may not be related to
2430 * the number of busy CPUs due to sched_setaffinity.
2431 */
2432 if (dst_running > imb_numa_nr)
2433 return imbalance;
2434
2435 /*
2436 * Allow a small imbalance based on a simple pair of communicating
2437 * tasks that remain local when the destination is lightly loaded.
2438 */
2439 if (imbalance <= NUMA_IMBALANCE_MIN)
2440 return 0;
2441
2442 return imbalance;
2443 }
2444 #endif /* CONFIG_NUMA */
2445
2446 #ifdef CONFIG_NUMA_BALANCING
2447 /*
2448 * Approximate time to scan a full NUMA task in ms. The task scan period is
2449 * calculated based on the tasks virtual memory size and
2450 * numa_balancing_scan_size.
2451 */
2452 unsigned int sysctl_numa_balancing_scan_period_min = 1000;
2453 unsigned int sysctl_numa_balancing_scan_period_max = 60000;
2454
2455 /* Portion of address space to scan in MB */
2456 unsigned int sysctl_numa_balancing_scan_size = 256;
2457
2458 /* Scan @scan_size MB every @scan_period after an initial @scan_delay in ms */
2459 unsigned int sysctl_numa_balancing_scan_delay = 1000;
2460
2461 /* The page with hint page fault latency < threshold in ms is considered hot */
2462 unsigned int sysctl_numa_balancing_hot_threshold = MSEC_PER_SEC;
2463
2464 struct numa_group {
2465 refcount_t refcount;
2466
2467 spinlock_t lock; /* nr_tasks, tasks */
2468 int nr_tasks;
2469 pid_t gid;
2470 int active_nodes;
2471
2472 struct rcu_head rcu;
2473 unsigned long total_faults;
2474 unsigned long max_faults_cpu;
2475 /*
2476 * faults[] array is split into two regions: faults_mem and faults_cpu.
2477 *
2478 * Faults_cpu is used to decide whether memory should move
2479 * towards the CPU. As a consequence, these stats are weighted
2480 * more by CPU use than by memory faults.
2481 */
2482 unsigned long faults[];
2483 };
2484
2485 /*
2486 * For functions that can be called in multiple contexts that permit reading
2487 * ->numa_group (see struct task_struct for locking rules).
2488 */
2489 static struct numa_group *deref_task_numa_group(struct task_struct *p)
2490 {
2491 return rcu_dereference_check(p->numa_group, p == current ||
2492 (lockdep_is_held(__rq_lockp(task_rq(p))) && !READ_ONCE(p->on_cpu)));
2493 }
2494
2495 static struct numa_group *deref_curr_numa_group(struct task_struct *p)
2496 {
2497 return rcu_dereference_protected(p->numa_group, p == current);
2498 }
2499
2500 static inline unsigned long group_faults_priv(struct numa_group *ng);
2501 static inline unsigned long group_faults_shared(struct numa_group *ng);
2502
2503 static unsigned int task_nr_scan_windows(struct task_struct *p)
2504 {
2505 unsigned long rss = 0;
2506 unsigned long nr_scan_pages;
2507
2508 /*
2509 * Calculations based on RSS as non-present and empty pages are skipped
2510 * by the PTE scanner and NUMA hinting faults should be trapped based
2511 * on resident pages
2512 */
2513 nr_scan_pages = MB_TO_PAGES(sysctl_numa_balancing_scan_size);
2514 rss = get_mm_rss(p->mm);
2515 if (!rss)
2516 rss = nr_scan_pages;
2517
2518 rss = round_up(rss, nr_scan_pages);
2519 return rss / nr_scan_pages;
2520 }
2521
2522 /* For sanity's sake, never scan more PTEs than MAX_SCAN_WINDOW MB/sec. */
2523 #define MAX_SCAN_WINDOW 2560
2524
2525 static unsigned int task_scan_min(struct task_struct *p)
2526 {
2527 unsigned int scan_size = READ_ONCE(sysctl_numa_balancing_scan_size);
2528 unsigned int scan, floor;
2529 unsigned int windows = 1;
2530
2531 if (scan_size < MAX_SCAN_WINDOW)
2532 windows = MAX_SCAN_WINDOW / scan_size;
2533 floor = 1000 / windows;
2534
2535 scan = sysctl_numa_balancing_scan_period_min / task_nr_scan_windows(p);
2536 return max_t(unsigned int, floor, scan);
2537 }
2538
2539 static unsigned int task_scan_start(struct task_struct *p)
2540 {
2541 unsigned long smin = task_scan_min(p);
2542 unsigned long period = smin;
2543 struct numa_group *ng;
2544
2545 /* Scale the maximum scan period with the amount of shared memory. */
2546 rcu_read_lock();
2547 ng = rcu_dereference_all(p->numa_group);
2548 if (ng) {
2549 unsigned long shared = group_faults_shared(ng);
2550 unsigned long private = group_faults_priv(ng);
2551
2552 period *= refcount_read(&ng->refcount);
2553 period *= shared + 1;
2554 period /= private + shared + 1;
2555 }
2556 rcu_read_unlock();
2557
2558 return max(smin, period);
2559 }
2560
2561 static unsigned int task_scan_max(struct task_struct *p)
2562 {
2563 unsigned long smin = task_scan_min(p);
2564 unsigned long smax;
2565 struct numa_group *ng;
2566
2567 /* Watch for min being lower than max due to floor calculations */
2568 smax = sysctl_numa_balancing_scan_period_max / task_nr_scan_windows(p);
2569
2570 /* Scale the maximum scan period with the amount of shared memory. */
2571 ng = deref_curr_numa_group(p);
2572 if (ng) {
2573 unsigned long shared = group_faults_shared(ng);
2574 unsigned long private = group_faults_priv(ng);
2575 unsigned long period = smax;
2576
2577 period *= refcount_read(&ng->refcount);
2578 period *= shared + 1;
2579 period /= private + shared + 1;
2580
2581 smax = max(smax, period);
2582 }
2583
2584 return max(smin, smax);
2585 }
2586
2587 static void account_numa_enqueue(struct rq *rq, struct task_struct *p)
2588 {
2589 rq->nr_numa_running += (p->numa_preferred_nid != NUMA_NO_NODE);
2590 rq->nr_preferred_running += (p->numa_preferred_nid == task_node(p));
2591 }
2592
2593 static void account_numa_dequeue(struct rq *rq, struct task_struct *p)
2594 {
2595 rq->nr_numa_running -= (p->numa_preferred_nid != NUMA_NO_NODE);
2596 rq->nr_preferred_running -= (p->numa_preferred_nid == task_node(p));
2597 }
2598
2599 /* Shared or private faults. */
2600 #define NR_NUMA_HINT_FAULT_TYPES 2
2601
2602 /* Memory and CPU locality */
2603 #define NR_NUMA_HINT_FAULT_STATS (NR_NUMA_HINT_FAULT_TYPES * 2)
2604
2605 /* Averaged statistics, and temporary buffers. */
2606 #define NR_NUMA_HINT_FAULT_BUCKETS (NR_NUMA_HINT_FAULT_STATS * 2)
2607
2608 pid_t task_numa_group_id(struct task_struct *p)
2609 {
2610 struct numa_group *ng;
2611 pid_t gid = 0;
2612
2613 rcu_read_lock();
2614 ng = rcu_dereference_all(p->numa_group);
2615 if (ng)
2616 gid = ng->gid;
2617 rcu_read_unlock();
2618
2619 return gid;
2620 }
2621
2622 /*
2623 * The averaged statistics, shared & private, memory & CPU,
2624 * occupy the first half of the array. The second half of the
2625 * array is for current counters, which are averaged into the
2626 * first set by task_numa_placement.
2627 */
2628 static inline int task_faults_idx(enum numa_faults_stats s, int nid, int priv)
2629 {
2630 return NR_NUMA_HINT_FAULT_TYPES * (s * nr_node_ids + nid) + priv;
2631 }
2632
2633 static inline unsigned long task_faults(struct task_struct *p, int nid)
2634 {
2635 if (!p->numa_faults)
2636 return 0;
2637
2638 return p->numa_faults[task_faults_idx(NUMA_MEM, nid, 0)] +
2639 p->numa_faults[task_faults_idx(NUMA_MEM, nid, 1)];
2640 }
2641
2642 static inline unsigned long group_faults(struct task_struct *p, int nid)
2643 {
2644 struct numa_group *ng = deref_task_numa_group(p);
2645
2646 if (!ng)
2647 return 0;
2648
2649 return ng->faults[task_faults_idx(NUMA_MEM, nid, 0)] +
2650 ng->faults[task_faults_idx(NUMA_MEM, nid, 1)];
2651 }
2652
2653 static inline unsigned long group_faults_cpu(struct numa_group *group, int nid)
2654 {
2655 return group->faults[task_faults_idx(NUMA_CPU, nid, 0)] +
2656 group->faults[task_faults_idx(NUMA_CPU, nid, 1)];
2657 }
2658
2659 static inline unsigned long group_faults_priv(struct numa_group *ng)
2660 {
2661 unsigned long faults = 0;
2662 int node;
2663
2664 for_each_online_node(node) {
2665 faults += ng->faults[task_faults_idx(NUMA_MEM, node, 1)];
2666 }
2667
2668 return faults;
2669 }
2670
2671 static inline unsigned long group_faults_shared(struct numa_group *ng)
2672 {
2673 unsigned long faults = 0;
2674 int node;
2675
2676 for_each_online_node(node) {
2677 faults += ng->faults[task_faults_idx(NUMA_MEM, node, 0)];
2678 }
2679
2680 return faults;
2681 }
2682
2683 /*
2684 * A node triggering more than 1/3 as many NUMA faults as the maximum is
2685 * considered part of a numa group's pseudo-interleaving set. Migrations
2686 * between these nodes are slowed down, to allow things to settle down.
2687 */
2688 #define ACTIVE_NODE_FRACTION 3
2689
2690 static bool numa_is_active_node(int nid, struct numa_group *ng)
2691 {
2692 return group_faults_cpu(ng, nid) * ACTIVE_NODE_FRACTION > ng->max_faults_cpu;
2693 }
2694
2695 /* Handle placement on systems where not all nodes are directly connected. */
2696 static unsigned long score_nearby_nodes(struct task_struct *p, int nid,
2697 int lim_dist, bool task)
2698 {
2699 unsigned long score = 0;
2700 int node, max_dist;
2701
2702 /*
2703 * All nodes are directly connected, and the same distance
2704 * from each other. No need for fancy placement algorithms.
2705 */
2706 if (sched_numa_topology_type == NUMA_DIRECT)
2707 return 0;
2708
2709 /* sched_max_numa_distance may be changed in parallel. */
2710 max_dist = READ_ONCE(sched_max_numa_distance);
2711 /*
2712 * This code is called for each node, introducing N^2 complexity,
2713 * which should be OK given the number of nodes rarely exceeds 8.
2714 */
2715 for_each_online_node(node) {
2716 unsigned long faults;
2717 int dist = node_distance(nid, node);
2718
2719 /*
2720 * The furthest away nodes in the system are not interesting
2721 * for placement; nid was already counted.
2722 */
2723 if (dist >= max_dist || node == nid)
2724 continue;
2725
2726 /*
2727 * On systems with a backplane NUMA topology, compare groups
2728 * of nodes, and move tasks towards the group with the most
2729 * memory accesses. When comparing two nodes at distance
2730 * "hoplimit", only nodes closer by than "hoplimit" are part
2731 * of each group. Skip other nodes.
2732 */
2733 if (sched_numa_topology_type == NUMA_BACKPLANE && dist >= lim_dist)
2734 continue;
2735
2736 /* Add up the faults from nearby nodes. */
2737 if (task)
2738 faults = task_faults(p, node);
2739 else
2740 faults = group_faults(p, node);
2741
2742 /*
2743 * On systems with a glueless mesh NUMA topology, there are
2744 * no fixed "groups of nodes". Instead, nodes that are not
2745 * directly connected bounce traffic through intermediate
2746 * nodes; a numa_group can occupy any set of nodes.
2747 * The further away a node is, the less the faults count.
2748 * This seems to result in good task placement.
2749 */
2750 if (sched_numa_topology_type == NUMA_GLUELESS_MESH) {
2751 faults *= (max_dist - dist);
2752 faults /= (max_dist - LOCAL_DISTANCE);
2753 }
2754
2755 score += faults;
2756 }
2757
2758 return score;
2759 }
2760
2761 /*
2762 * These return the fraction of accesses done by a particular task, or
2763 * task group, on a particular numa node. The group weight is given a
2764 * larger multiplier, in order to group tasks together that are almost
2765 * evenly spread out between numa nodes.
2766 */
2767 static inline unsigned long task_weight(struct task_struct *p, int nid,
2768 int dist)
2769 {
2770 unsigned long faults, total_faults;
2771
2772 if (!p->numa_faults)
2773 return 0;
2774
2775 total_faults = p->total_numa_faults;
2776
2777 if (!total_faults)
2778 return 0;
2779
2780 faults = task_faults(p, nid);
2781 faults += score_nearby_nodes(p, nid, dist, true);
2782
2783 return 1000 * faults / total_faults;
2784 }
2785
2786 static inline unsigned long group_weight(struct task_struct *p, int nid,
2787 int dist)
2788 {
2789 struct numa_group *ng = deref_task_numa_group(p);
2790 unsigned long faults, total_faults;
2791
2792 if (!ng)
2793 return 0;
2794
2795 total_faults = ng->total_faults;
2796
2797 if (!total_faults)
2798 return 0;
2799
2800 faults = group_faults(p, nid);
2801 faults += score_nearby_nodes(p, nid, dist, false);
2802
2803 return 1000 * faults / total_faults;
2804 }
2805
2806 /*
2807 * If memory tiering mode is enabled, cpupid of slow memory page is
2808 * used to record scan time instead of CPU and PID. When tiering mode
2809 * is disabled at run time, the scan time (in cpupid) will be
2810 * interpreted as CPU and PID. So CPU needs to be checked to avoid to
2811 * access out of array bound.
2812 */
2813 static inline bool cpupid_valid(int cpupid)
2814 {
2815 return cpupid_to_cpu(cpupid) < nr_cpu_ids;
2816 }
2817
2818 /*
2819 * For memory tiering mode, if there are enough free pages (more than
2820 * enough watermark defined here) in fast memory node, to take full
2821 * advantage of fast memory capacity, all recently accessed slow
2822 * memory pages will be migrated to fast memory node without
2823 * considering hot threshold.
2824 */
2825 static bool pgdat_free_space_enough(struct pglist_data *pgdat)
2826 {
2827 int z;
2828 unsigned long enough_wmark;
2829
2830 enough_wmark = max(1UL * 1024 * 1024 * 1024 >> PAGE_SHIFT,
2831 pgdat->node_present_pages >> 4);
2832 for (z = pgdat->nr_zones - 1; z >= 0; z--) {
2833 struct zone *zone = pgdat->node_zones + z;
2834
2835 if (!populated_zone(zone))
2836 continue;
2837
2838 if (zone_watermark_ok(zone, 0,
2839 promo_wmark_pages(zone) + enough_wmark,
2840 ZONE_MOVABLE, 0))
2841 return true;
2842 }
2843 return false;
2844 }
2845
2846 /*
2847 * For memory tiering mode, when page tables are scanned, the scan
2848 * time will be recorded in struct page in addition to make page
2849 * PROT_NONE for slow memory page. So when the page is accessed, in
2850 * hint page fault handler, the hint page fault latency is calculated
2851 * via,
2852 *
2853 * hint page fault latency = hint page fault time - scan time
2854 *
2855 * The smaller the hint page fault latency, the higher the possibility
2856 * for the page to be hot.
2857 */
2858 static int numa_hint_fault_latency(struct folio *folio)
2859 {
2860 int last_time, time;
2861
2862 time = jiffies_to_msecs(jiffies);
2863 last_time = folio_xchg_access_time(folio, time);
2864
2865 return (time - last_time) & PAGE_ACCESS_TIME_MASK;
2866 }
2867
2868 /*
2869 * For memory tiering mode, too high promotion/demotion throughput may
2870 * hurt application latency. So we provide a mechanism to rate limit
2871 * the number of pages that are tried to be promoted.
2872 */
2873 static bool numa_promotion_rate_limit(struct pglist_data *pgdat,
2874 unsigned long rate_limit, int nr)
2875 {
2876 unsigned long nr_cand;
2877 unsigned int now, start;
2878
2879 now = jiffies_to_msecs(jiffies);
2880 mod_node_page_state(pgdat, PGPROMOTE_CANDIDATE, nr);
2881 nr_cand = node_page_state(pgdat, PGPROMOTE_CANDIDATE);
2882 start = pgdat->nbp_rl_start;
2883 if (now - start > MSEC_PER_SEC &&
2884 cmpxchg(&pgdat->nbp_rl_start, start, now) == start)
2885 pgdat->nbp_rl_nr_cand = nr_cand;
2886 if (nr_cand - pgdat->nbp_rl_nr_cand >= rate_limit)
2887 return true;
2888 return false;
2889 }
2890
2891 #define NUMA_MIGRATION_ADJUST_STEPS 16
2892
2893 static void numa_promotion_adjust_threshold(struct pglist_data *pgdat,
2894 unsigned long rate_limit,
2895 unsigned int ref_th)
2896 {
2897 unsigned int now, start, th_period, unit_th, th;
2898 unsigned long nr_cand, ref_cand, diff_cand;
2899
2900 now = jiffies_to_msecs(jiffies);
2901 th_period = sysctl_numa_balancing_scan_period_max;
2902 start = pgdat->nbp_th_start;
2903 if (now - start > th_period &&
2904 cmpxchg(&pgdat->nbp_th_start, start, now) == start) {
2905 ref_cand = rate_limit *
2906 sysctl_numa_balancing_scan_period_max / MSEC_PER_SEC;
2907 nr_cand = node_page_state(pgdat, PGPROMOTE_CANDIDATE);
2908 diff_cand = nr_cand - pgdat->nbp_th_nr_cand;
2909 unit_th = ref_th * 2 / NUMA_MIGRATION_ADJUST_STEPS;
2910 th = pgdat->nbp_threshold ? : ref_th;
2911 if (diff_cand > ref_cand * 11 / 10)
2912 th = max(th - unit_th, unit_th);
2913 else if (diff_cand < ref_cand * 9 / 10)
2914 th = min(th + unit_th, ref_th * 2);
2915 pgdat->nbp_th_nr_cand = nr_cand;
2916 pgdat->nbp_threshold = th;
2917 }
2918 }
2919
2920 bool should_numa_migrate_memory(struct task_struct *p, struct folio *folio,
2921 int src_nid, int dst_cpu)
2922 {
2923 struct numa_group *ng = deref_curr_numa_group(p);
2924 int dst_nid = cpu_to_node(dst_cpu);
2925 int last_cpupid, this_cpupid;
2926
2927 /*
2928 * Cannot migrate to memoryless nodes.
2929 */
2930 if (!node_state(dst_nid, N_MEMORY))
2931 return false;
2932
2933 /*
2934 * The pages in slow memory node should be migrated according
2935 * to hot/cold instead of private/shared.
2936 */
2937 if (folio_use_access_time(folio)) {
2938 struct pglist_data *pgdat;
2939 unsigned long rate_limit;
2940 unsigned int latency, th, def_th;
2941 long nr = folio_nr_pages(folio);
2942
2943 pgdat = NODE_DATA(dst_nid);
2944 if (pgdat_free_space_enough(pgdat)) {
2945 /* workload changed, reset hot threshold */
2946 pgdat->nbp_threshold = 0;
2947 mod_node_page_state(pgdat, PGPROMOTE_CANDIDATE_NRL, nr);
2948 return true;
2949 }
2950
2951 def_th = sysctl_numa_balancing_hot_threshold;
2952 rate_limit = MB_TO_PAGES(sysctl_numa_balancing_promote_rate_limit);
2953 numa_promotion_adjust_threshold(pgdat, rate_limit, def_th);
2954
2955 th = pgdat->nbp_threshold ? : def_th;
2956 latency = numa_hint_fault_latency(folio);
2957 if (latency >= th)
2958 return false;
2959
2960 return !numa_promotion_rate_limit(pgdat, rate_limit, nr);
2961 }
2962
2963 this_cpupid = cpu_pid_to_cpupid(dst_cpu, current->pid);
2964 last_cpupid = folio_xchg_last_cpupid(folio, this_cpupid);
2965
2966 if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING) &&
2967 !node_is_toptier(src_nid) && !cpupid_valid(last_cpupid))
2968 return false;
2969
2970 /*
2971 * Allow first faults or private faults to migrate immediately early in
2972 * the lifetime of a task. The magic number 4 is based on waiting for
2973 * two full passes of the "multi-stage node selection" test that is
2974 * executed below.
2975 */
2976 if ((p->numa_preferred_nid == NUMA_NO_NODE || p->numa_scan_seq <= 4) &&
2977 (cpupid_pid_unset(last_cpupid) || cpupid_match_pid(p, last_cpupid)))
2978 return true;
2979
2980 /*
2981 * Multi-stage node selection is used in conjunction with a periodic
2982 * migration fault to build a temporal task<->page relation. By using
2983 * a two-stage filter we remove short/unlikely relations.
2984 *
2985 * Using P(p) ~ n_p / n_t as per frequentist probability, we can equate
2986 * a task's usage of a particular page (n_p) per total usage of this
2987 * page (n_t) (in a given time-span) to a probability.
2988 *
2989 * Our periodic faults will sample this probability and getting the
2990 * same result twice in a row, given these samples are fully
2991 * independent, is then given by P(n)^2, provided our sample period
2992 * is sufficiently short compared to the usage pattern.
2993 *
2994 * This quadric squishes small probabilities, making it less likely we
2995 * act on an unlikely task<->page relation.
2996 */
2997 if (!cpupid_pid_unset(last_cpupid) &&
2998 cpupid_to_nid(last_cpupid) != dst_nid)
2999 return false;
3000
3001 /* Always allow migrate on private faults */
3002 if (cpupid_match_pid(p, last_cpupid))
3003 return true;
3004
3005 /* A shared fault, but p->numa_group has not been set up yet. */
3006 if (!ng)
3007 return true;
3008
3009 /*
3010 * Destination node is much more heavily used than the source
3011 * node? Allow migration.
3012 */
3013 if (group_faults_cpu(ng, dst_nid) > group_faults_cpu(ng, src_nid) *
3014 ACTIVE_NODE_FRACTION)
3015 return true;
3016
3017 /*
3018 * Distribute memory according to CPU & memory use on each node,
3019 * with 3/4 hysteresis to avoid unnecessary memory migrations:
3020 *
3021 * faults_cpu(dst) 3 faults_cpu(src)
3022 * --------------- * - > ---------------
3023 * faults_mem(dst) 4 faults_mem(src)
3024 */
3025 return group_faults_cpu(ng, dst_nid) * group_faults(p, src_nid) * 3 >
3026 group_faults_cpu(ng, src_nid) * group_faults(p, dst_nid) * 4;
3027 }
3028
3029 /*
3030 * 'numa_type' describes the node at the moment of load balancing.
3031 */
3032 enum numa_type {
3033 /* The node has spare capacity that can be used to run more tasks. */
3034 node_has_spare = 0,
3035 /*
3036 * The node is fully used and the tasks don't compete for more CPU
3037 * cycles. Nevertheless, some tasks might wait before running.
3038 */
3039 node_fully_busy,
3040 /*
3041 * The node is overloaded and can't provide expected CPU cycles to all
3042 * tasks.
3043 */
3044 node_overloaded
3045 };
3046
3047 /* Cached statistics for all CPUs within a node */
3048 struct numa_stats {
3049 unsigned long load;
3050 unsigned long runnable;
3051 unsigned long util;
3052 /* Total compute capacity of CPUs on a node */
3053 unsigned long compute_capacity;
3054 unsigned int nr_running;
3055 unsigned int weight;
3056 enum numa_type node_type;
3057 int idle_cpu;
3058 };
3059
3060 struct task_numa_env {
3061 struct task_struct *p;
3062
3063 int src_cpu, src_nid;
3064 int dst_cpu, dst_nid;
3065 int imb_numa_nr;
3066
3067 struct numa_stats src_stats, dst_stats;
3068
3069 int imbalance_pct;
3070 int dist;
3071
3072 struct task_struct *best_task;
3073 long best_imp;
3074 int best_cpu;
3075 };
3076
3077 static unsigned long cpu_load(struct rq *rq);
3078 static unsigned long cpu_runnable(struct rq *rq);
3079
3080 static inline enum
3081 numa_type numa_classify(unsigned int imbalance_pct,
3082 struct numa_stats *ns)
3083 {
3084 if ((ns->nr_running > ns->weight) &&
3085 (((ns->compute_capacity * 100) < (ns->util * imbalance_pct)) ||
3086 ((ns->compute_capacity * imbalance_pct) < (ns->runnable * 100))))
3087 return node_overloaded;
3088
3089 if ((ns->nr_running < ns->weight) ||
3090 (((ns->compute_capacity * 100) > (ns->util * imbalance_pct)) &&
3091 ((ns->compute_capacity * imbalance_pct) > (ns->runnable * 100))))
3092 return node_has_spare;
3093
3094 return node_fully_busy;
3095 }
3096
3097 /* Forward declarations of select_idle_sibling helpers */
3098 static inline bool test_idle_cores(int cpu);
3099 static inline int numa_idle_core(int idle_core, int cpu)
3100 {
3101 if (!sched_smt_active() ||
3102 idle_core >= 0 || !test_idle_cores(cpu))
3103 return idle_core;
3104
3105 /*
3106 * Prefer cores instead of packing HT siblings
3107 * and triggering future load balancing.
3108 */
3109 if (is_core_idle(cpu))
3110 idle_core = cpu;
3111
3112 return idle_core;
3113 }
3114
3115 /*
3116 * Gather all necessary information to make NUMA balancing placement
3117 * decisions that are compatible with standard load balancer. This
3118 * borrows code and logic from update_sg_lb_stats but sharing a
3119 * common implementation is impractical.
3120 */
3121 static void update_numa_stats(struct task_numa_env *env,
3122 struct numa_stats *ns, int nid,
3123 bool find_idle)
3124 {
3125 int cpu, idle_core = -1;
3126
3127 memset(ns, 0, sizeof(*ns));
3128 ns->idle_cpu = -1;
3129
3130 rcu_read_lock();
3131 for_each_cpu(cpu, cpumask_of_node(nid)) {
3132 struct rq *rq = cpu_rq(cpu);
3133
3134 ns->load += cpu_load(rq);
3135 ns->runnable += cpu_runnable(rq);
3136 ns->util += cpu_util_cfs(cpu);
3137 ns->nr_running += rq->cfs.h_nr_runnable;
3138 ns->compute_capacity += capacity_of(cpu);
3139
3140 if (find_idle && idle_core < 0 && !rq->nr_running && idle_cpu(cpu)) {
3141 if (READ_ONCE(rq->numa_migrate_on) ||
3142 !cpumask_test_cpu(cpu, env->p->cpus_ptr))
3143 continue;
3144
3145 if (ns->idle_cpu == -1)
3146 ns->idle_cpu = cpu;
3147
3148 idle_core = numa_idle_core(idle_core, cpu);
3149 }
3150 }
3151 rcu_read_unlock();
3152
3153 ns->weight = cpumask_weight(cpumask_of_node(nid));
3154
3155 ns->node_type = numa_classify(env->imbalance_pct, ns);
3156
3157 if (idle_core >= 0)
3158 ns->idle_cpu = idle_core;
3159 }
3160
3161 static void task_numa_assign(struct task_numa_env *env,
3162 struct task_struct *p, long imp)
3163 {
3164 struct rq *rq = cpu_rq(env->dst_cpu);
3165
3166 /* Check if run-queue part of active NUMA balance. */
3167 if (env->best_cpu != env->dst_cpu && xchg(&rq->numa_migrate_on, 1)) {
3168 int cpu;
3169 int start = env->dst_cpu;
3170
3171 /* Find alternative idle CPU. */
3172 for_each_cpu_wrap(cpu, cpumask_of_node(env->dst_nid), start + 1) {
3173 if (cpu == env->best_cpu || !idle_cpu(cpu) ||
3174 !cpumask_test_cpu(cpu, env->p->cpus_ptr)) {
3175 continue;
3176 }
3177
3178 env->dst_cpu = cpu;
3179 rq = cpu_rq(env->dst_cpu);
3180 if (!xchg(&rq->numa_migrate_on, 1))
3181 goto assign;
3182 }
3183
3184 /* Failed to find an alternative idle CPU */
3185 return;
3186 }
3187
3188 assign:
3189 /*
3190 * Clear previous best_cpu/rq numa-migrate flag, since task now
3191 * found a better CPU to move/swap.
3192 */
3193 if (env->best_cpu != -1 && env->best_cpu != env->dst_cpu) {
3194 rq = cpu_rq(env->best_cpu);
3195 WRITE_ONCE(rq->numa_migrate_on, 0);
3196 }
3197
3198 if (env->best_task)
3199 put_task_struct(env->best_task);
3200 if (p)
3201 get_task_struct(p);
3202
3203 env->best_task = p;
3204 env->best_imp = imp;
3205 env->best_cpu = env->dst_cpu;
3206 }
3207
3208 static bool load_too_imbalanced(long src_load, long dst_load,
3209 struct task_numa_env *env)
3210 {
3211 long imb, old_imb;
3212 long orig_src_load, orig_dst_load;
3213 long src_capacity, dst_capacity;
3214
3215 /*
3216 * The load is corrected for the CPU capacity available on each node.
3217 *
3218 * src_load dst_load
3219 * ------------ vs ---------
3220 * src_capacity dst_capacity
3221 */
3222 src_capacity = env->src_stats.compute_capacity;
3223 dst_capacity = env->dst_stats.compute_capacity;
3224
3225 imb = abs(dst_load * src_capacity - src_load * dst_capacity);
3226
3227 orig_src_load = env->src_stats.load;
3228 orig_dst_load = env->dst_stats.load;
3229
3230 old_imb = abs(orig_dst_load * src_capacity - orig_src_load * dst_capacity);
3231
3232 /* Would this change make things worse? */
3233 return (imb > old_imb);
3234 }
3235
3236 /*
3237 * Maximum NUMA importance can be 1998 (2*999);
3238 * SMALLIMP @ 30 would be close to 1998/64.
3239 * Used to deter task migration.
3240 */
3241 #define SMALLIMP 30
3242
3243 /*
3244 * This checks if the overall compute and NUMA accesses of the system would
3245 * be improved if the source tasks was migrated to the target dst_cpu taking
3246 * into account that it might be best if task running on the dst_cpu should
3247 * be exchanged with the source task
3248 */
3249 static bool task_numa_compare(struct task_numa_env *env,
3250 long taskimp, long groupimp, bool maymove)
3251 {
3252 struct numa_group *cur_ng, *p_ng = deref_curr_numa_group(env->p);
3253 struct rq *dst_rq = cpu_rq(env->dst_cpu);
3254 long imp = p_ng ? groupimp : taskimp;
3255 struct task_struct *cur;
3256 long src_load, dst_load;
3257 int dist = env->dist;
3258 long moveimp = imp;
3259 long load;
3260 bool stopsearch = false;
3261
3262 if (READ_ONCE(dst_rq->numa_migrate_on))
3263 return false;
3264
3265 rcu_read_lock();
3266 cur = rcu_dereference_all(dst_rq->curr);
3267 if (cur && ((cur->flags & (PF_EXITING | PF_KTHREAD)) ||
3268 !cur->mm))
3269 cur = NULL;
3270
3271 /*
3272 * Because we have preemption enabled we can get migrated around and
3273 * end try selecting ourselves (current == env->p) as a swap candidate.
3274 */
3275 if (cur == env->p) {
3276 stopsearch = true;
3277 goto unlock;
3278 }
3279
3280 if (!cur) {
3281 if (maymove && moveimp >= env->best_imp)
3282 goto assign;
3283 else
3284 goto unlock;
3285 }
3286
3287 /* Skip this swap candidate if cannot move to the source cpu. */
3288 if (!cpumask_test_cpu(env->src_cpu, cur->cpus_ptr))
3289 goto unlock;
3290
3291 /*
3292 * Skip this swap candidate if it is not moving to its preferred
3293 * node and the best task is.
3294 */
3295 if (env->best_task &&
3296 env->best_task->numa_preferred_nid == env->src_nid &&
3297 cur->numa_preferred_nid != env->src_nid) {
3298 goto unlock;
3299 }
3300
3301 /*
3302 * "imp" is the fault differential for the source task between the
3303 * source and destination node. Calculate the total differential for
3304 * the source task and potential destination task. The more negative
3305 * the value is, the more remote accesses that would be expected to
3306 * be incurred if the tasks were swapped.
3307 *
3308 * If dst and source tasks are in the same NUMA group, or not
3309 * in any group then look only at task weights.
3310 */
3311 cur_ng = rcu_dereference_all(cur->numa_group);
3312 if (cur_ng == p_ng) {
3313 /*
3314 * Do not swap within a group or between tasks that have
3315 * no group if there is spare capacity. Swapping does
3316 * not address the load imbalance and helps one task at
3317 * the cost of punishing another.
3318 */
3319 if (env->dst_stats.node_type == node_has_spare)
3320 goto unlock;
3321
3322 imp = taskimp + task_weight(cur, env->src_nid, dist) -
3323 task_weight(cur, env->dst_nid, dist);
3324 /*
3325 * Add some hysteresis to prevent swapping the
3326 * tasks within a group over tiny differences.
3327 */
3328 if (cur_ng)
3329 imp -= imp / 16;
3330 } else {
3331 /*
3332 * Compare the group weights. If a task is all by itself
3333 * (not part of a group), use the task weight instead.
3334 */
3335 if (cur_ng && p_ng)
3336 imp += group_weight(cur, env->src_nid, dist) -
3337 group_weight(cur, env->dst_nid, dist);
3338 else
3339 imp += task_weight(cur, env->src_nid, dist) -
3340 task_weight(cur, env->dst_nid, dist);
3341 }
3342
3343 /* Discourage picking a task already on its preferred node */
3344 if (cur->numa_preferred_nid == env->dst_nid)
3345 imp -= imp / 16;
3346
3347 /*
3348 * Encourage picking a task that moves to its preferred node.
3349 * This potentially makes imp larger than it's maximum of
3350 * 1998 (see SMALLIMP and task_weight for why) but in this
3351 * case, it does not matter.
3352 */
3353 if (cur->numa_preferred_nid == env->src_nid)
3354 imp += imp / 8;
3355
3356 if (maymove && moveimp > imp && moveimp > env->best_imp) {
3357 imp = moveimp;
3358 cur = NULL;
3359 goto assign;
3360 }
3361
3362 /*
3363 * Prefer swapping with a task moving to its preferred node over a
3364 * task that is not.
3365 */
3366 if (env->best_task && cur->numa_preferred_nid == env->src_nid &&
3367 env->best_task->numa_preferred_nid != env->src_nid) {
3368 goto assign;
3369 }
3370
3371 /*
3372 * If the NUMA importance is less than SMALLIMP,
3373 * task migration might only result in ping pong
3374 * of tasks and also hurt performance due to cache
3375 * misses.
3376 */
3377 if (imp < SMALLIMP || imp <= env->best_imp + SMALLIMP / 2)
3378 goto unlock;
3379
3380 /*
3381 * In the overloaded case, try and keep the load balanced.
3382 */
3383 load = task_h_load(env->p) - task_h_load(cur);
3384 if (!load)
3385 goto assign;
3386
3387 dst_load = env->dst_stats.load + load;
3388 src_load = env->src_stats.load - load;
3389
3390 if (load_too_imbalanced(src_load, dst_load, env))
3391 goto unlock;
3392
3393 assign:
3394 /* Evaluate an idle CPU for a task numa move. */
3395 if (!cur) {
3396 int cpu = env->dst_stats.idle_cpu;
3397
3398 /* Nothing cached so current CPU went idle since the search. */
3399 if (cpu < 0)
3400 cpu = env->dst_cpu;
3401
3402 /*
3403 * If the CPU is no longer truly idle and the previous best CPU
3404 * is, keep using it.
3405 */
3406 if (!idle_cpu(cpu) && env->best_cpu >= 0 &&
3407 idle_cpu(env->best_cpu)) {
3408 cpu = env->best_cpu;
3409 }
3410
3411 env->dst_cpu = cpu;
3412 }
3413
3414 task_numa_assign(env, cur, imp);
3415
3416 /*
3417 * If a move to idle is allowed because there is capacity or load
3418 * balance improves then stop the search. While a better swap
3419 * candidate may exist, a search is not free.
3420 */
3421 if (maymove && !cur && env->best_cpu >= 0 && idle_cpu(env->best_cpu))
3422 stopsearch = true;
3423
3424 /*
3425 * If a swap candidate must be identified and the current best task
3426 * moves its preferred node then stop the search.
3427 */
3428 if (!maymove && env->best_task &&
3429 env->best_task->numa_preferred_nid == env->src_nid) {
3430 stopsearch = true;
3431 }
3432 unlock:
3433 rcu_read_unlock();
3434
3435 return stopsearch;
3436 }
3437
3438 static void task_numa_find_cpu(struct task_numa_env *env,
3439 long taskimp, long groupimp)
3440 {
3441 bool maymove = false;
3442 int cpu;
3443
3444 /*
3445 * If dst node has spare capacity, then check if there is an
3446 * imbalance that would be overruled by the load balancer.
3447 */
3448 if (env->dst_stats.node_type == node_has_spare) {
3449 unsigned int imbalance;
3450 int src_running, dst_running;
3451
3452 /*
3453 * Would movement cause an imbalance? Note that if src has
3454 * more running tasks that the imbalance is ignored as the
3455 * move improves the imbalance from the perspective of the
3456 * CPU load balancer.
3457 * */
3458 src_running = env->src_stats.nr_running - 1;
3459 dst_running = env->dst_stats.nr_running + 1;
3460 imbalance = max(0, dst_running - src_running);
3461 imbalance = adjust_numa_imbalance(imbalance, dst_running,
3462 env->imb_numa_nr);
3463
3464 /* Use idle CPU if there is no imbalance */
3465 if (!imbalance) {
3466 maymove = true;
3467 if (env->dst_stats.idle_cpu >= 0) {
3468 env->dst_cpu = env->dst_stats.idle_cpu;
3469 task_numa_assign(env, NULL, 0);
3470 return;
3471 }
3472 }
3473 } else {
3474 long src_load, dst_load, load;
3475 /*
3476 * If the improvement from just moving env->p direction is better
3477 * than swapping tasks around, check if a move is possible.
3478 */
3479 load = task_h_load(env->p);
3480 dst_load = env->dst_stats.load + load;
3481 src_load = env->src_stats.load - load;
3482 maymove = !load_too_imbalanced(src_load, dst_load, env);
3483 }
3484
3485 /* Skip CPUs if the source task cannot migrate */
3486 for_each_cpu_and(cpu, cpumask_of_node(env->dst_nid), env->p->cpus_ptr) {
3487 env->dst_cpu = cpu;
3488 if (task_numa_compare(env, taskimp, groupimp, maymove))
3489 break;
3490 }
3491 }
3492
3493 static int task_numa_migrate(struct task_struct *p)
3494 {
3495 struct task_numa_env env = {
3496 .p = p,
3497
3498 .src_cpu = task_cpu(p),
3499 .src_nid = task_node(p),
3500
3501 .imbalance_pct = 112,
3502
3503 .best_task = NULL,
3504 .best_imp = 0,
3505 .best_cpu = -1,
3506 };
3507 unsigned long taskweight, groupweight;
3508 struct sched_domain *sd;
3509 long taskimp, groupimp;
3510 struct numa_group *ng;
3511 struct rq *best_rq;
3512 int nid, ret, dist;
3513
3514 /*
3515 * Pick the lowest SD_NUMA domain, as that would have the smallest
3516 * imbalance and would be the first to start moving tasks about.
3517 *
3518 * And we want to avoid any moving of tasks about, as that would create
3519 * random movement of tasks -- counter the numa conditions we're trying
3520 * to satisfy here.
3521 */
3522 rcu_read_lock();
3523 sd = rcu_dereference_all(per_cpu(sd_numa, env.src_cpu));
3524 if (sd) {
3525 env.imbalance_pct = 100 + (sd->imbalance_pct - 100) / 2;
3526 env.imb_numa_nr = sd->imb_numa_nr;
3527 }
3528 rcu_read_unlock();
3529
3530 /*
3531 * Cpusets can break the scheduler domain tree into smaller
3532 * balance domains, some of which do not cross NUMA boundaries.
3533 * Tasks that are "trapped" in such domains cannot be migrated
3534 * elsewhere, so there is no point in (re)trying.
3535 */
3536 if (unlikely(!sd)) {
3537 sched_setnuma(p, task_node(p));
3538 return -EINVAL;
3539 }
3540
3541 env.dst_nid = p->numa_preferred_nid;
3542 dist = env.dist = node_distance(env.src_nid, env.dst_nid);
3543 taskweight = task_weight(p, env.src_nid, dist);
3544 groupweight = group_weight(p, env.src_nid, dist);
3545 update_numa_stats(&env, &env.src_stats, env.src_nid, false);
3546 taskimp = task_weight(p, env.dst_nid, dist) - taskweight;
3547 groupimp = group_weight(p, env.dst_nid, dist) - groupweight;
3548 update_numa_stats(&env, &env.dst_stats, env.dst_nid, true);
3549
3550 /* Try to find a spot on the preferred nid. */
3551 task_numa_find_cpu(&env, taskimp, groupimp);
3552
3553 /*
3554 * Look at other nodes in these cases:
3555 * - there is no space available on the preferred_nid
3556 * - the task is part of a numa_group that is interleaved across
3557 * multiple NUMA nodes; in order to better consolidate the group,
3558 * we need to check other locations.
3559 */
3560 ng = deref_curr_numa_group(p);
3561 if (env.best_cpu == -1 || (ng && ng->active_nodes > 1)) {
3562 for_each_node_state(nid, N_CPU) {
3563 if (nid == env.src_nid || nid == p->numa_preferred_nid)
3564 continue;
3565
3566 dist = node_distance(env.src_nid, env.dst_nid);
3567 if (sched_numa_topology_type == NUMA_BACKPLANE &&
3568 dist != env.dist) {
3569 taskweight = task_weight(p, env.src_nid, dist);
3570 groupweight = group_weight(p, env.src_nid, dist);
3571 }
3572
3573 /* Only consider nodes where both task and groups benefit */
3574 taskimp = task_weight(p, nid, dist) - taskweight;
3575 groupimp = group_weight(p, nid, dist) - groupweight;
3576 if (taskimp < 0 && groupimp < 0)
3577 continue;
3578
3579 env.dist = dist;
3580 env.dst_nid = nid;
3581 update_numa_stats(&env, &env.dst_stats, env.dst_nid, true);
3582 task_numa_find_cpu(&env, taskimp, groupimp);
3583 }
3584 }
3585
3586 /*
3587 * If the task is part of a workload that spans multiple NUMA nodes,
3588 * and is migrating into one of the workload's active nodes, remember
3589 * this node as the task's preferred numa node, so the workload can
3590 * settle down.
3591 * A task that migrated to a second choice node will be better off
3592 * trying for a better one later. Do not set the preferred node here.
3593 */
3594 if (ng) {
3595 if (env.best_cpu == -1)
3596 nid = env.src_nid;
3597 else
3598 nid = cpu_to_node(env.best_cpu);
3599
3600 if (nid != p->numa_preferred_nid)
3601 sched_setnuma(p, nid);
3602 }
3603
3604 /* No better CPU than the current one was found. */
3605 if (env.best_cpu == -1) {
3606 trace_sched_stick_numa(p, env.src_cpu, NULL, -1);
3607 return -EAGAIN;
3608 }
3609
3610 best_rq = cpu_rq(env.best_cpu);
3611 if (env.best_task == NULL) {
3612 ret = migrate_task_to(p, env.best_cpu);
3613 WRITE_ONCE(best_rq->numa_migrate_on, 0);
3614 if (ret != 0)
3615 trace_sched_stick_numa(p, env.src_cpu, NULL, env.best_cpu);
3616 return ret;
3617 }
3618
3619 ret = migrate_swap(p, env.best_task, env.best_cpu, env.src_cpu);
3620 WRITE_ONCE(best_rq->numa_migrate_on, 0);
3621
3622 if (ret != 0)
3623 trace_sched_stick_numa(p, env.src_cpu, env.best_task, env.best_cpu);
3624 put_task_struct(env.best_task);
3625 return ret;
3626 }
3627
3628 /* Attempt to migrate a task to a CPU on the preferred node. */
3629 static void numa_migrate_preferred(struct task_struct *p)
3630 {
3631 unsigned long interval = HZ;
3632
3633 /* This task has no NUMA fault statistics yet */
3634 if (unlikely(p->numa_preferred_nid == NUMA_NO_NODE || !p->numa_faults))
3635 return;
3636
3637 /* Periodically retry migrating the task to the preferred node */
3638 interval = min(interval, msecs_to_jiffies(p->numa_scan_period) / 16);
3639 p->numa_migrate_retry = jiffies + interval;
3640
3641 /* Success if task is already running on preferred CPU */
3642 if (task_node(p) == p->numa_preferred_nid)
3643 return;
3644
3645 /* Otherwise, try migrate to a CPU on the preferred node */
3646 task_numa_migrate(p);
3647 }
3648
3649 /*
3650 * Find out how many nodes the workload is actively running on. Do this by
3651 * tracking the nodes from which NUMA hinting faults are triggered. This can
3652 * be different from the set of nodes where the workload's memory is currently
3653 * located.
3654 */
3655 static void numa_group_count_active_nodes(struct numa_group *numa_group)
3656 {
3657 unsigned long faults, max_faults = 0;
3658 int nid, active_nodes = 0;
3659
3660 for_each_node_state(nid, N_CPU) {
3661 faults = group_faults_cpu(numa_group, nid);
3662 if (faults > max_faults)
3663 max_faults = faults;
3664 }
3665
3666 for_each_node_state(nid, N_CPU) {
3667 faults = group_faults_cpu(numa_group, nid);
3668 if (faults * ACTIVE_NODE_FRACTION > max_faults)
3669 active_nodes++;
3670 }
3671
3672 numa_group->max_faults_cpu = max_faults;
3673 numa_group->active_nodes = active_nodes;
3674 }
3675
3676 /*
3677 * When adapting the scan rate, the period is divided into NUMA_PERIOD_SLOTS
3678 * increments. The more local the fault statistics are, the higher the scan
3679 * period will be for the next scan window. If local/(local+remote) ratio is
3680 * below NUMA_PERIOD_THRESHOLD (where range of ratio is 1..NUMA_PERIOD_SLOTS)
3681 * the scan period will decrease. Aim for 70% local accesses.
3682 */
3683 #define NUMA_PERIOD_SLOTS 10
3684 #define NUMA_PERIOD_THRESHOLD 7
3685
3686 /*
3687 * Increase the scan period (slow down scanning) if the majority of
3688 * our memory is already on our local node, or if the majority of
3689 * the page accesses are shared with other processes.
3690 * Otherwise, decrease the scan period.
3691 */
3692 static void update_task_scan_period(struct task_struct *p,
3693 unsigned long shared, unsigned long private)
3694 {
3695 unsigned int period_slot;
3696 int lr_ratio, ps_ratio;
3697 int diff;
3698
3699 unsigned long remote = p->numa_faults_locality[0];
3700 unsigned long local = p->numa_faults_locality[1];
3701
3702 /*
3703 * If there were no record hinting faults then either the task is
3704 * completely idle or all activity is in areas that are not of interest
3705 * to automatic numa balancing. Related to that, if there were failed
3706 * migration then it implies we are migrating too quickly or the local
3707 * node is overloaded. In either case, scan slower
3708 */
3709 if (local + shared == 0 || p->numa_faults_locality[2]) {
3710 p->numa_scan_period = min(p->numa_scan_period_max,
3711 p->numa_scan_period << 1);
3712
3713 p->mm->numa_next_scan = jiffies +
3714 msecs_to_jiffies(p->numa_scan_period);
3715
3716 return;
3717 }
3718
3719 /*
3720 * Prepare to scale scan period relative to the current period.
3721 * == NUMA_PERIOD_THRESHOLD scan period stays the same
3722 * < NUMA_PERIOD_THRESHOLD scan period decreases (scan faster)
3723 * >= NUMA_PERIOD_THRESHOLD scan period increases (scan slower)
3724 */
3725 period_slot = DIV_ROUND_UP(p->numa_scan_period, NUMA_PERIOD_SLOTS);
3726 lr_ratio = (local * NUMA_PERIOD_SLOTS) / (local + remote);
3727 ps_ratio = (private * NUMA_PERIOD_SLOTS) / (private + shared);
3728
3729 if (ps_ratio >= NUMA_PERIOD_THRESHOLD) {
3730 /*
3731 * Most memory accesses are local. There is no need to
3732 * do fast NUMA scanning, since memory is already local.
3733 */
3734 int slot = ps_ratio - NUMA_PERIOD_THRESHOLD;
3735 if (!slot)
3736 slot = 1;
3737 diff = slot * period_slot;
3738 } else if (lr_ratio >= NUMA_PERIOD_THRESHOLD) {
3739 /*
3740 * Most memory accesses are shared with other tasks.
3741 * There is no point in continuing fast NUMA scanning,
3742 * since other tasks may just move the memory elsewhere.
3743 */
3744 int slot = lr_ratio - NUMA_PERIOD_THRESHOLD;
3745 if (!slot)
3746 slot = 1;
3747 diff = slot * period_slot;
3748 } else {
3749 /*
3750 * Private memory faults exceed (SLOTS-THRESHOLD)/SLOTS,
3751 * yet they are not on the local NUMA node. Speed up
3752 * NUMA scanning to get the memory moved over.
3753 */
3754 int ratio = max(lr_ratio, ps_ratio);
3755 diff = -(NUMA_PERIOD_THRESHOLD - ratio) * period_slot;
3756 }
3757
3758 p->numa_scan_period = clamp(p->numa_scan_period + diff,
3759 task_scan_min(p), task_scan_max(p));
3760 memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality));
3761 }
3762
3763 /*
3764 * Get the fraction of time the task has been running since the last
3765 * NUMA placement cycle. The scheduler keeps similar statistics, but
3766 * decays those on a 32ms period, which is orders of magnitude off
3767 * from the dozens-of-seconds NUMA balancing period. Use the scheduler
3768 * stats only if the task is so new there are no NUMA statistics yet.
3769 */
3770 static u64 numa_get_avg_runtime(struct task_struct *p, u64 *period)
3771 {
3772 u64 runtime, delta, now;
3773 /* Use the start of this time slice to avoid calculations. */
3774 now = p->se.exec_start;
3775 runtime = p->se.sum_exec_runtime;
3776
3777 if (p->last_task_numa_placement) {
3778 delta = runtime - p->last_sum_exec_runtime;
3779 *period = now - p->last_task_numa_placement;
3780
3781 /* Avoid time going backwards, prevent potential divide error: */
3782 if (unlikely((s64)*period < 0))
3783 *period = 0;
3784 } else {
3785 delta = p->se.avg.load_sum;
3786 *period = LOAD_AVG_MAX;
3787 }
3788
3789 p->last_sum_exec_runtime = runtime;
3790 p->last_task_numa_placement = now;
3791
3792 return delta;
3793 }
3794
3795 /*
3796 * Determine the preferred nid for a task in a numa_group. This needs to
3797 * be done in a way that produces consistent results with group_weight,
3798 * otherwise workloads might not converge.
3799 */
3800 static int preferred_group_nid(struct task_struct *p, int nid)
3801 {
3802 nodemask_t nodes;
3803 int dist;
3804
3805 /* Direct connections between all NUMA nodes. */
3806 if (sched_numa_topology_type == NUMA_DIRECT)
3807 return nid;
3808
3809 /*
3810 * On a system with glueless mesh NUMA topology, group_weight
3811 * scores nodes according to the number of NUMA hinting faults on
3812 * both the node itself, and on nearby nodes.
3813 */
3814 if (sched_numa_topology_type == NUMA_GLUELESS_MESH) {
3815 unsigned long score, max_score = 0;
3816 int node, max_node = nid;
3817
3818 dist = sched_max_numa_distance;
3819
3820 for_each_node_state(node, N_CPU) {
3821 score = group_weight(p, node, dist);
3822 if (score > max_score) {
3823 max_score = score;
3824 max_node = node;
3825 }
3826 }
3827 return max_node;
3828 }
3829
3830 /*
3831 * Finding the preferred nid in a system with NUMA backplane
3832 * interconnect topology is more involved. The goal is to locate
3833 * tasks from numa_groups near each other in the system, and
3834 * untangle workloads from different sides of the system. This requires
3835 * searching down the hierarchy of node groups, recursively searching
3836 * inside the highest scoring group of nodes. The nodemask tricks
3837 * keep the complexity of the search down.
3838 */
3839 nodes = node_states[N_CPU];
3840 for (dist = sched_max_numa_distance; dist > LOCAL_DISTANCE; dist--) {
3841 unsigned long max_faults = 0;
3842 nodemask_t max_group = NODE_MASK_NONE;
3843 int a, b;
3844
3845 /* Are there nodes at this distance from each other? */
3846 if (!find_numa_distance(dist))
3847 continue;
3848
3849 for_each_node_mask(a, nodes) {
3850 unsigned long faults = 0;
3851 nodemask_t this_group;
3852 nodes_clear(this_group);
3853
3854 /* Sum group's NUMA faults; includes a==b case. */
3855 for_each_node_mask(b, nodes) {
3856 if (node_distance(a, b) < dist) {
3857 faults += group_faults(p, b);
3858 node_set(b, this_group);
3859 node_clear(b, nodes);
3860 }
3861 }
3862
3863 /* Remember the top group. */
3864 if (faults > max_faults) {
3865 max_faults = faults;
3866 max_group = this_group;
3867 /*
3868 * subtle: at the smallest distance there is
3869 * just one node left in each "group", the
3870 * winner is the preferred nid.
3871 */
3872 nid = a;
3873 }
3874 }
3875 /* Next round, evaluate the nodes within max_group. */
3876 if (!max_faults)
3877 break;
3878 nodes = max_group;
3879 }
3880 return nid;
3881 }
3882
3883 static void task_numa_placement(struct task_struct *p)
3884 __context_unsafe(/* conditional locking */)
3885 {
3886 struct sched_cache_group __maybe_unused *grp;
3887 int seq, nid, max_nid = NUMA_NO_NODE;
3888 unsigned long max_faults = 0;
3889 unsigned long fault_types[2] = { 0, 0 };
3890 unsigned long total_faults;
3891 u64 runtime, period;
3892 spinlock_t *group_lock = NULL;
3893 long __maybe_unused new_fp;
3894 struct numa_group *ng;
3895
3896 /*
3897 * The p->mm->numa_scan_seq field gets updated without
3898 * exclusive access. Use READ_ONCE() here to ensure
3899 * that the field is read in a single access:
3900 */
3901 seq = READ_ONCE(p->mm->numa_scan_seq);
3902 if (p->numa_scan_seq == seq)
3903 return;
3904 p->numa_scan_seq = seq;
3905 p->numa_scan_period_max = task_scan_max(p);
3906
3907 total_faults = p->numa_faults_locality[0] +
3908 p->numa_faults_locality[1];
3909 runtime = numa_get_avg_runtime(p, &period);
3910
3911 /* If the task is part of a group prevent parallel updates to group stats */
3912 ng = deref_curr_numa_group(p);
3913 if (ng) {
3914 group_lock = &ng->lock;
3915 spin_lock_irq(group_lock);
3916 }
3917
3918 /* Find the node with the highest number of faults */
3919 for_each_online_node(nid) {
3920 /* Keep track of the offsets in numa_faults array */
3921 int mem_idx, membuf_idx, cpu_idx, cpubuf_idx;
3922 unsigned long faults = 0, group_faults = 0;
3923 int priv;
3924
3925 for (priv = 0; priv < NR_NUMA_HINT_FAULT_TYPES; priv++) {
3926 long diff, f_diff, f_weight;
3927
3928 mem_idx = task_faults_idx(NUMA_MEM, nid, priv);
3929 membuf_idx = task_faults_idx(NUMA_MEMBUF, nid, priv);
3930 cpu_idx = task_faults_idx(NUMA_CPU, nid, priv);
3931 cpubuf_idx = task_faults_idx(NUMA_CPUBUF, nid, priv);
3932
3933 /* Decay existing window, copy faults since last scan */
3934 diff = p->numa_faults[membuf_idx] - p->numa_faults[mem_idx] / 2;
3935 fault_types[priv] += p->numa_faults[membuf_idx];
3936 p->numa_faults[membuf_idx] = 0;
3937
3938 /*
3939 * Normalize the faults_from, so all tasks in a group
3940 * count according to CPU use, instead of by the raw
3941 * number of faults. Tasks with little runtime have
3942 * little over-all impact on throughput, and thus their
3943 * faults are less important.
3944 */
3945 f_weight = div64_u64(runtime << 16, period + 1);
3946 f_weight = (f_weight * p->numa_faults[cpubuf_idx]) /
3947 (total_faults + 1);
3948 f_diff = f_weight - p->numa_faults[cpu_idx] / 2;
3949 p->numa_faults[cpubuf_idx] = 0;
3950
3951 p->numa_faults[mem_idx] += diff;
3952 p->numa_faults[cpu_idx] += f_diff;
3953 faults += p->numa_faults[mem_idx];
3954 p->total_numa_faults += diff;
3955 if (ng) {
3956 /*
3957 * safe because we can only change our own group
3958 *
3959 * mem_idx represents the offset for a given
3960 * nid and priv in a specific region because it
3961 * is at the beginning of the numa_faults array.
3962 */
3963 ng->faults[mem_idx] += diff;
3964 ng->faults[cpu_idx] += f_diff;
3965 ng->total_faults += diff;
3966 group_faults += ng->faults[mem_idx];
3967 }
3968 #ifdef CONFIG_SCHED_CACHE
3969 /*
3970 * Per task p->numa_faults[mem_idx] converges,
3971 * so the accumulation of each task's faults
3972 * converges too - Given the number of threads,
3973 * it cannot overflow an unsigned long.
3974 * Racy with concurrent updates from other threads
3975 * sharing this mm. Acceptable since footprint is a
3976 * heuristic and occasional lost updates are tolerable.
3977 *
3978 * If a task exits, its corresponding footprint must
3979 * be subtracted from p->sched_cache_grp->footprint,
3980 * otherwise the footprint will not converge: the
3981 * exiting thread's footprint remains unchanged/undecayed.
3982 * See exit_mm().
3983 *
3984 * Lost updates and unsynchronized subtraction
3985 * in exit_mm() can cause footprint + diff to
3986 * go negative. Clamp to zero to prevent the
3987 * unsigned footprint from wrapping.
3988 */
3989 scoped_guard(rcu) {
3990 grp = rcu_dereference(p->sched_cache_grp);
3991
3992 if (grp) {
3993 new_fp = (long)READ_ONCE(grp->footprint) + diff;
3994 WRITE_ONCE(grp->footprint, max(new_fp, 0L));
3995 }
3996 }
3997 #endif
3998 }
3999
4000 if (!ng) {
4001 if (faults > max_faults) {
4002 max_faults = faults;
4003 max_nid = nid;
4004 }
4005 } else if (group_faults > max_faults) {
4006 max_faults = group_faults;
4007 max_nid = nid;
4008 }
4009 }
4010
4011 /* Cannot migrate task to CPU-less node */
4012 max_nid = numa_nearest_node(max_nid, N_CPU);
4013
4014 if (ng) {
4015 numa_group_count_active_nodes(ng);
4016 spin_unlock_irq(group_lock);
4017 max_nid = preferred_group_nid(p, max_nid);
4018 }
4019
4020 if (max_faults) {
4021 /* Set the new preferred node */
4022 if (max_nid != p->numa_preferred_nid)
4023 sched_setnuma(p, max_nid);
4024 }
4025
4026 update_task_scan_period(p, fault_types[0], fault_types[1]);
4027 }
4028
4029 static inline int get_numa_group(struct numa_group *grp)
4030 {
4031 return refcount_inc_not_zero(&grp->refcount);
4032 }
4033
4034 static inline void put_numa_group(struct numa_group *grp)
4035 {
4036 if (refcount_dec_and_test(&grp->refcount))
4037 kfree_rcu(grp, rcu);
4038 }
4039
4040 static void task_numa_group(struct task_struct *p, int cpupid, int flags,
4041 int *priv)
4042 {
4043 struct numa_group *grp, *my_grp;
4044 struct task_struct *tsk;
4045 bool join = false;
4046 int cpu = cpupid_to_cpu(cpupid);
4047 int i;
4048
4049 if (unlikely(!deref_curr_numa_group(p))) {
4050 unsigned int size = sizeof(struct numa_group) +
4051 NR_NUMA_HINT_FAULT_STATS *
4052 nr_node_ids * sizeof(unsigned long);
4053
4054 grp = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
4055 if (!grp)
4056 return;
4057
4058 refcount_set(&grp->refcount, 1);
4059 grp->active_nodes = 1;
4060 grp->max_faults_cpu = 0;
4061 spin_lock_init(&grp->lock);
4062 grp->gid = p->pid;
4063
4064 for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++)
4065 grp->faults[i] = p->numa_faults[i];
4066
4067 grp->total_faults = p->total_numa_faults;
4068
4069 grp->nr_tasks++;
4070 rcu_assign_pointer(p->numa_group, grp);
4071 }
4072
4073 rcu_read_lock();
4074 tsk = READ_ONCE(cpu_rq(cpu)->curr);
4075
4076 if (!cpupid_match_pid(tsk, cpupid))
4077 goto no_join;
4078
4079 grp = rcu_dereference_all(tsk->numa_group);
4080 if (!grp)
4081 goto no_join;
4082
4083 my_grp = deref_curr_numa_group(p);
4084 if (grp == my_grp)
4085 goto no_join;
4086
4087 /*
4088 * Only join the other group if its bigger; if we're the bigger group,
4089 * the other task will join us.
4090 */
4091 if (my_grp->nr_tasks > grp->nr_tasks)
4092 goto no_join;
4093
4094 /*
4095 * Tie-break on the grp address.
4096 */
4097 if (my_grp->nr_tasks == grp->nr_tasks && my_grp > grp)
4098 goto no_join;
4099
4100 /* Always join threads in the same process. */
4101 if (tsk->mm == current->mm)
4102 join = true;
4103
4104 /* Simple filter to avoid false positives due to PID collisions */
4105 if (flags & TNF_SHARED)
4106 join = true;
4107
4108 /* Update priv based on whether false sharing was detected */
4109 *priv = !join;
4110
4111 if (join && !get_numa_group(grp))
4112 goto no_join;
4113
4114 rcu_read_unlock();
4115
4116 if (!join)
4117 return;
4118
4119 WARN_ON_ONCE(irqs_disabled());
4120 double_lock_irq(&my_grp->lock, &grp->lock);
4121
4122 for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) {
4123 my_grp->faults[i] -= p->numa_faults[i];
4124 grp->faults[i] += p->numa_faults[i];
4125 }
4126 my_grp->total_faults -= p->total_numa_faults;
4127 grp->total_faults += p->total_numa_faults;
4128
4129 my_grp->nr_tasks--;
4130 grp->nr_tasks++;
4131
4132 spin_unlock(&my_grp->lock);
4133 spin_unlock_irq(&grp->lock);
4134
4135 rcu_assign_pointer(p->numa_group, grp);
4136
4137 put_numa_group(my_grp);
4138 return;
4139
4140 no_join:
4141 rcu_read_unlock();
4142 return;
4143 }
4144
4145 /*
4146 * Get rid of NUMA statistics associated with a task (either current or dead).
4147 * If @final is set, the task is dead and has reached refcount zero, so we can
4148 * safely free all relevant data structures. Otherwise, there might be
4149 * concurrent reads from places like load balancing and procfs, and we should
4150 * reset the data back to default state without freeing ->numa_faults.
4151 */
4152 void task_numa_free(struct task_struct *p, bool final)
4153 {
4154 /* safe: p either is current or is being freed by current */
4155 struct numa_group *grp = rcu_dereference_raw(p->numa_group);
4156 unsigned long *numa_faults = p->numa_faults;
4157 unsigned long flags;
4158 int i;
4159
4160 if (!numa_faults)
4161 return;
4162
4163 if (grp) {
4164 spin_lock_irqsave(&grp->lock, flags);
4165 for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++)
4166 grp->faults[i] -= p->numa_faults[i];
4167 grp->total_faults -= p->total_numa_faults;
4168
4169 grp->nr_tasks--;
4170 spin_unlock_irqrestore(&grp->lock, flags);
4171 RCU_INIT_POINTER(p->numa_group, NULL);
4172 put_numa_group(grp);
4173 }
4174
4175 if (final) {
4176 p->numa_faults = NULL;
4177 kfree(numa_faults);
4178 } else {
4179 p->total_numa_faults = 0;
4180 for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++)
4181 numa_faults[i] = 0;
4182 }
4183 }
4184
4185 /*
4186 * Got a PROT_NONE fault for a page on @node.
4187 */
4188 void task_numa_fault(int last_cpupid, int mem_node, int pages, int flags)
4189 {
4190 struct task_struct *p = current;
4191 bool migrated = flags & TNF_MIGRATED;
4192 int cpu_node = task_node(current);
4193 int local = !!(flags & TNF_FAULT_LOCAL);
4194 struct numa_group *ng;
4195 int priv;
4196
4197 if (!static_branch_likely(&sched_numa_balancing))
4198 return;
4199
4200 /* for example, ksmd faulting in a user's mm */
4201 if (!p->mm)
4202 return;
4203
4204 /*
4205 * NUMA faults statistics are unnecessary for the slow memory
4206 * node for memory tiering mode.
4207 */
4208 if (!node_is_toptier(mem_node) &&
4209 (sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING ||
4210 !cpupid_valid(last_cpupid)))
4211 return;
4212
4213 /* Allocate buffer to track faults on a per-node basis */
4214 if (unlikely(!p->numa_faults)) {
4215 int size = sizeof(*p->numa_faults) *
4216 NR_NUMA_HINT_FAULT_BUCKETS * nr_node_ids;
4217
4218 p->numa_faults = kzalloc(size, GFP_KERNEL|__GFP_NOWARN);
4219 if (!p->numa_faults)
4220 return;
4221
4222 p->total_numa_faults = 0;
4223 memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality));
4224 }
4225
4226 /*
4227 * First accesses are treated as private, otherwise consider accesses
4228 * to be private if the accessing pid has not changed
4229 */
4230 if (unlikely(last_cpupid == (-1 & LAST_CPUPID_MASK))) {
4231 priv = 1;
4232 } else {
4233 priv = cpupid_match_pid(p, last_cpupid);
4234 if (!priv && !(flags & TNF_NO_GROUP))
4235 task_numa_group(p, last_cpupid, flags, &priv);
4236 }
4237
4238 /*
4239 * If a workload spans multiple NUMA nodes, a shared fault that
4240 * occurs wholly within the set of nodes that the workload is
4241 * actively using should be counted as local. This allows the
4242 * scan rate to slow down when a workload has settled down.
4243 */
4244 ng = deref_curr_numa_group(p);
4245 if (!priv && !local && ng && ng->active_nodes > 1 &&
4246 numa_is_active_node(cpu_node, ng) &&
4247 numa_is_active_node(mem_node, ng))
4248 local = 1;
4249
4250 /*
4251 * Retry to migrate task to preferred node periodically, in case it
4252 * previously failed, or the scheduler moved us.
4253 */
4254 if (time_after(jiffies, p->numa_migrate_retry)) {
4255 task_numa_placement(p);
4256 numa_migrate_preferred(p);
4257 }
4258
4259 if (migrated)
4260 p->numa_pages_migrated += pages;
4261 if (flags & TNF_MIGRATE_FAIL)
4262 p->numa_faults_locality[2] += pages;
4263
4264 p->numa_faults[task_faults_idx(NUMA_MEMBUF, mem_node, priv)] += pages;
4265 p->numa_faults[task_faults_idx(NUMA_CPUBUF, cpu_node, priv)] += pages;
4266 p->numa_faults_locality[local] += pages;
4267 }
4268
4269 static void reset_ptenuma_scan(struct task_struct *p)
4270 {
4271 /*
4272 * We only did a read acquisition of the mmap sem, so
4273 * p->mm->numa_scan_seq is written to without exclusive access
4274 * and the update is not guaranteed to be atomic. That's not
4275 * much of an issue though, since this is just used for
4276 * statistical sampling. Use READ_ONCE/WRITE_ONCE, which are not
4277 * expensive, to avoid any form of compiler optimizations:
4278 */
4279 WRITE_ONCE(p->mm->numa_scan_seq, READ_ONCE(p->mm->numa_scan_seq) + 1);
4280 p->mm->numa_scan_offset = 0;
4281 }
4282
4283 static bool vma_is_accessed(struct mm_struct *mm, struct vm_area_struct *vma)
4284 {
4285 unsigned long pids;
4286 /*
4287 * Allow unconditional access first two times, so that all the (pages)
4288 * of VMAs get prot_none fault introduced irrespective of accesses.
4289 * This is also done to avoid any side effect of task scanning
4290 * amplifying the unfairness of disjoint set of VMAs' access.
4291 */
4292 if ((READ_ONCE(current->mm->numa_scan_seq) - vma->numab_state->start_scan_seq) < 2)
4293 return true;
4294
4295 pids = vma->numab_state->pids_active[0] | vma->numab_state->pids_active[1];
4296 if (test_bit(hash_32(current->pid, ilog2(BITS_PER_LONG)), &pids))
4297 return true;
4298
4299 /*
4300 * Complete a scan that has already started regardless of PID access, or
4301 * some VMAs may never be scanned in multi-threaded applications:
4302 */
4303 if (mm->numa_scan_offset > vma->vm_start) {
4304 trace_sched_skip_vma_numa(mm, vma, NUMAB_SKIP_IGNORE_PID);
4305 return true;
4306 }
4307
4308 /*
4309 * This vma has not been accessed for a while, and if the number
4310 * the threads in the same process is low, which means no other
4311 * threads can help scan this vma, force a vma scan.
4312 */
4313 if (READ_ONCE(mm->numa_scan_seq) >
4314 (vma->numab_state->prev_scan_seq + get_nr_threads(current)))
4315 return true;
4316
4317 return false;
4318 }
4319
4320 #define VMA_PID_RESET_PERIOD (4 * sysctl_numa_balancing_scan_delay)
4321
4322 /*
4323 * The expensive part of numa migration is done from task_work context.
4324 * Triggered from task_tick_numa().
4325 */
4326 static void task_numa_work(struct callback_head *work)
4327 {
4328 unsigned long migrate, next_scan, now = jiffies;
4329 struct task_struct *p = current;
4330 struct mm_struct *mm = p->mm;
4331 u64 runtime = p->se.sum_exec_runtime;
4332 struct vm_area_struct *vma;
4333 unsigned long start, end;
4334 unsigned long nr_pte_updates = 0;
4335 long pages, virtpages;
4336 struct vma_iterator vmi;
4337 bool vma_pids_skipped;
4338 bool vma_pids_forced = false;
4339
4340 WARN_ON_ONCE(p != container_of(work, struct task_struct, numa_work));
4341
4342 work->next = work;
4343 /*
4344 * Who cares about NUMA placement when they're dying.
4345 *
4346 * NOTE: make sure not to dereference p->mm before this check,
4347 * exit_task_work() happens _after_ exit_mm() so we could be called
4348 * without p->mm even though we still had it when we enqueued this
4349 * work.
4350 */
4351 if (p->flags & PF_EXITING)
4352 return;
4353
4354 /*
4355 * Memory is pinned to only one NUMA node via cpuset.mems, naturally
4356 * no page can be migrated.
4357 */
4358 if (cpusets_enabled() && nodes_weight(cpuset_current_mems_allowed) == 1) {
4359 trace_sched_skip_cpuset_numa(current, &cpuset_current_mems_allowed);
4360 return;
4361 }
4362
4363 if (!mm->numa_next_scan) {
4364 mm->numa_next_scan = now +
4365 msecs_to_jiffies(sysctl_numa_balancing_scan_delay);
4366 }
4367
4368 /*
4369 * Enforce maximal scan/migration frequency..
4370 */
4371 migrate = mm->numa_next_scan;
4372 if (time_before(now, migrate))
4373 return;
4374
4375 if (p->numa_scan_period == 0) {
4376 p->numa_scan_period_max = task_scan_max(p);
4377 p->numa_scan_period = task_scan_start(p);
4378 }
4379
4380 next_scan = now + msecs_to_jiffies(p->numa_scan_period);
4381 if (!try_cmpxchg(&mm->numa_next_scan, &migrate, next_scan))
4382 return;
4383
4384 /*
4385 * Delay this task enough that another task of this mm will likely win
4386 * the next time around.
4387 */
4388 p->node_stamp += 2 * TICK_NSEC;
4389
4390 pages = sysctl_numa_balancing_scan_size;
4391 pages <<= 20 - PAGE_SHIFT; /* MB in pages */
4392 virtpages = pages * 8; /* Scan up to this much virtual space */
4393 if (!pages)
4394 return;
4395
4396
4397 if (!mmap_read_trylock(mm))
4398 return;
4399
4400 /*
4401 * VMAs are skipped if the current PID has not trapped a fault within
4402 * the VMA recently. Allow scanning to be forced if there is no
4403 * suitable VMA remaining.
4404 */
4405 vma_pids_skipped = false;
4406
4407 retry_pids:
4408 start = mm->numa_scan_offset;
4409 vma_iter_init(&vmi, mm, start);
4410 vma = vma_next(&vmi);
4411 if (!vma) {
4412 reset_ptenuma_scan(p);
4413 start = 0;
4414 vma_iter_set(&vmi, start);
4415 vma = vma_next(&vmi);
4416 }
4417
4418 for (; vma; vma = vma_next(&vmi)) {
4419 if (!vma_migratable(vma) || !vma_policy_mof(vma) ||
4420 is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_MIXEDMAP)) {
4421 trace_sched_skip_vma_numa(mm, vma, NUMAB_SKIP_UNSUITABLE);
4422 continue;
4423 }
4424
4425 /*
4426 * Shared library pages mapped by multiple processes are not
4427 * migrated as it is expected they are cache replicated. Avoid
4428 * hinting faults in read-only file-backed mappings or the vDSO
4429 * as migrating the pages will be of marginal benefit.
4430 */
4431 if (!vma->vm_mm ||
4432 (vma->vm_file && (vma->vm_flags & (VM_READ|VM_WRITE)) == (VM_READ))) {
4433 trace_sched_skip_vma_numa(mm, vma, NUMAB_SKIP_SHARED_RO);
4434 continue;
4435 }
4436
4437 /*
4438 * Skip inaccessible VMAs to avoid any confusion between
4439 * PROT_NONE and NUMA hinting PTEs
4440 */
4441 if (!vma_is_accessible(vma)) {
4442 trace_sched_skip_vma_numa(mm, vma, NUMAB_SKIP_INACCESSIBLE);
4443 continue;
4444 }
4445
4446 /* Initialise new per-VMA NUMAB state. */
4447 if (!vma->numab_state) {
4448 struct vma_numab_state *ptr;
4449
4450 ptr = kzalloc_obj(*ptr);
4451 if (!ptr)
4452 continue;
4453
4454 if (cmpxchg(&vma->numab_state, NULL, ptr)) {
4455 kfree(ptr);
4456 continue;
4457 }
4458
4459 vma->numab_state->start_scan_seq = mm->numa_scan_seq;
4460
4461 vma->numab_state->next_scan = now +
4462 msecs_to_jiffies(sysctl_numa_balancing_scan_delay);
4463
4464 /* Reset happens after 4 times scan delay of scan start */
4465 vma->numab_state->pids_active_reset = vma->numab_state->next_scan +
4466 msecs_to_jiffies(VMA_PID_RESET_PERIOD);
4467
4468 /*
4469 * Ensure prev_scan_seq does not match numa_scan_seq,
4470 * to prevent VMAs being skipped prematurely on the
4471 * first scan:
4472 */
4473 vma->numab_state->prev_scan_seq = mm->numa_scan_seq - 1;
4474 }
4475
4476 /*
4477 * Scanning the VMAs of short lived tasks add more overhead. So
4478 * delay the scan for new VMAs.
4479 */
4480 if (mm->numa_scan_seq && time_before(jiffies,
4481 vma->numab_state->next_scan)) {
4482 trace_sched_skip_vma_numa(mm, vma, NUMAB_SKIP_SCAN_DELAY);
4483 continue;
4484 }
4485
4486 /* RESET access PIDs regularly for old VMAs. */
4487 if (mm->numa_scan_seq &&
4488 time_after(jiffies, vma->numab_state->pids_active_reset)) {
4489 vma->numab_state->pids_active_reset = vma->numab_state->pids_active_reset +
4490 msecs_to_jiffies(VMA_PID_RESET_PERIOD);
4491 vma->numab_state->pids_active[0] = READ_ONCE(vma->numab_state->pids_active[1]);
4492 vma->numab_state->pids_active[1] = 0;
4493 }
4494
4495 /* Do not rescan VMAs twice within the same sequence. */
4496 if (vma->numab_state->prev_scan_seq == mm->numa_scan_seq) {
4497 mm->numa_scan_offset = vma->vm_end;
4498 trace_sched_skip_vma_numa(mm, vma, NUMAB_SKIP_SEQ_COMPLETED);
4499 continue;
4500 }
4501
4502 /*
4503 * Do not scan the VMA if task has not accessed it, unless no other
4504 * VMA candidate exists.
4505 */
4506 if (!vma_pids_forced && !vma_is_accessed(mm, vma)) {
4507 vma_pids_skipped = true;
4508 trace_sched_skip_vma_numa(mm, vma, NUMAB_SKIP_PID_INACTIVE);
4509 continue;
4510 }
4511
4512 do {
4513 start = max(start, vma->vm_start);
4514 end = ALIGN(start + (pages << PAGE_SHIFT), HPAGE_SIZE);
4515 end = min(end, vma->vm_end);
4516 nr_pte_updates = change_prot_numa(vma, start, end);
4517
4518 /*
4519 * Try to scan sysctl_numa_balancing_size worth of
4520 * hpages that have at least one present PTE that
4521 * is not already PTE-numa. If the VMA contains
4522 * areas that are unused or already full of prot_numa
4523 * PTEs, scan up to virtpages, to skip through those
4524 * areas faster.
4525 */
4526 if (nr_pte_updates)
4527 pages -= (end - start) >> PAGE_SHIFT;
4528 virtpages -= (end - start) >> PAGE_SHIFT;
4529
4530 start = end;
4531 if (pages <= 0 || virtpages <= 0)
4532 goto out;
4533
4534 cond_resched();
4535 } while (end != vma->vm_end);
4536
4537 /* VMA scan is complete, do not scan until next sequence. */
4538 vma->numab_state->prev_scan_seq = mm->numa_scan_seq;
4539
4540 /*
4541 * Only force scan within one VMA at a time, to limit the
4542 * cost of scanning a potentially uninteresting VMA.
4543 */
4544 if (vma_pids_forced)
4545 break;
4546 }
4547
4548 /*
4549 * If no VMAs are remaining and VMAs were skipped due to the PID
4550 * not accessing the VMA previously, then force a scan to ensure
4551 * forward progress:
4552 */
4553 if (!vma && !vma_pids_forced && vma_pids_skipped) {
4554 vma_pids_forced = true;
4555 goto retry_pids;
4556 }
4557
4558 out:
4559 /*
4560 * It is possible to reach the end of the VMA list but the last few
4561 * VMAs are not guaranteed to the vma_migratable. If they are not, we
4562 * would find the !migratable VMA on the next scan but not reset the
4563 * scanner to the start so check it now.
4564 */
4565 if (vma)
4566 mm->numa_scan_offset = start;
4567 else
4568 reset_ptenuma_scan(p);
4569 mmap_read_unlock(mm);
4570
4571 /*
4572 * Make sure tasks use at least 32x as much time to run other code
4573 * than they used here, to limit NUMA PTE scanning overhead to 3% max.
4574 * Usually update_task_scan_period slows down scanning enough; on an
4575 * overloaded system we need to limit overhead on a per task basis.
4576 */
4577 if (unlikely(p->se.sum_exec_runtime != runtime)) {
4578 u64 diff = p->se.sum_exec_runtime - runtime;
4579 p->node_stamp += 32 * diff;
4580 }
4581 }
4582
4583 void init_numa_balancing(u64 clone_flags, struct task_struct *p)
4584 {
4585 int mm_users = 0;
4586 struct mm_struct *mm = p->mm;
4587
4588 if (mm) {
4589 mm_users = atomic_read(&mm->mm_users);
4590 if (mm_users == 1) {
4591 mm->numa_next_scan = jiffies + msecs_to_jiffies(sysctl_numa_balancing_scan_delay);
4592 mm->numa_scan_seq = 0;
4593 }
4594 }
4595 p->node_stamp = 0;
4596 p->numa_scan_seq = mm ? mm->numa_scan_seq : 0;
4597 p->numa_scan_period = sysctl_numa_balancing_scan_delay;
4598 p->numa_migrate_retry = 0;
4599 /* Protect against double add, see task_tick_numa and task_numa_work */
4600 p->numa_work.next = &p->numa_work;
4601 p->numa_faults = NULL;
4602 p->numa_pages_migrated = 0;
4603 p->total_numa_faults = 0;
4604 RCU_INIT_POINTER(p->numa_group, NULL);
4605 p->last_task_numa_placement = 0;
4606 p->last_sum_exec_runtime = 0;
4607
4608 init_task_work(&p->numa_work, task_numa_work);
4609
4610 /* New address space, reset the preferred nid */
4611 if (!(clone_flags & CLONE_VM)) {
4612 p->numa_preferred_nid = NUMA_NO_NODE;
4613 return;
4614 }
4615
4616 /*
4617 * New thread, keep existing numa_preferred_nid which should be copied
4618 * already by arch_dup_task_struct but stagger when scans start.
4619 */
4620 if (mm) {
4621 unsigned int delay;
4622
4623 delay = min_t(unsigned int, task_scan_max(current),
4624 current->numa_scan_period * mm_users * NSEC_PER_MSEC);
4625 delay += 2 * TICK_NSEC;
4626 p->node_stamp = delay;
4627 }
4628 }
4629
4630 /*
4631 * Drive the periodic memory faults..
4632 */
4633 static void task_tick_numa(struct rq *rq, struct task_struct *curr)
4634 {
4635 struct callback_head *work = &curr->numa_work;
4636 u64 period, now;
4637
4638 /*
4639 * We don't care about NUMA placement if we don't have memory.
4640 */
4641 if (!curr->mm || (curr->flags & (PF_EXITING | PF_KTHREAD)) || work->next != work)
4642 return;
4643
4644 /*
4645 * Using runtime rather than walltime has the dual advantage that
4646 * we (mostly) drive the selection from busy threads and that the
4647 * task needs to have done some actual work before we bother with
4648 * NUMA placement.
4649 */
4650 now = curr->se.sum_exec_runtime;
4651 period = (u64)curr->numa_scan_period * NSEC_PER_MSEC;
4652
4653 if (now > curr->node_stamp + period) {
4654 if (!curr->node_stamp)
4655 curr->numa_scan_period = task_scan_start(curr);
4656 curr->node_stamp += period;
4657
4658 if (!time_before(jiffies, curr->mm->numa_next_scan))
4659 task_work_add(curr, work, TWA_RESUME);
4660 }
4661 }
4662
4663 static void update_scan_period(struct task_struct *p, int new_cpu)
4664 {
4665 int src_nid = cpu_to_node(task_cpu(p));
4666 int dst_nid = cpu_to_node(new_cpu);
4667
4668 if (!static_branch_likely(&sched_numa_balancing))
4669 return;
4670
4671 if (!p->mm || !p->numa_faults || (p->flags & PF_EXITING))
4672 return;
4673
4674 if (src_nid == dst_nid)
4675 return;
4676
4677 /*
4678 * Allow resets if faults have been trapped before one scan
4679 * has completed. This is most likely due to a new task that
4680 * is pulled cross-node due to wakeups or load balancing.
4681 */
4682 if (p->numa_scan_seq) {
4683 /*
4684 * Avoid scan adjustments if moving to the preferred
4685 * node or if the task was not previously running on
4686 * the preferred node.
4687 */
4688 if (dst_nid == p->numa_preferred_nid ||
4689 (p->numa_preferred_nid != NUMA_NO_NODE &&
4690 src_nid != p->numa_preferred_nid))
4691 return;
4692 }
4693
4694 p->numa_scan_period = task_scan_start(p);
4695 }
4696
4697 #else /* !CONFIG_NUMA_BALANCING: */
4698
4699 static void task_tick_numa(struct rq *rq, struct task_struct *curr)
4700 {
4701 }
4702
4703 static inline void account_numa_enqueue(struct rq *rq, struct task_struct *p)
4704 {
4705 }
4706
4707 static inline void account_numa_dequeue(struct rq *rq, struct task_struct *p)
4708 {
4709 }
4710
4711 static inline void update_scan_period(struct task_struct *p, int new_cpu)
4712 {
4713 }
4714
4715 #endif /* !CONFIG_NUMA_BALANCING */
4716
4717 static void
4718 account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
4719 {
4720 WARN_ON_ONCE(cfs_rq != cfs_rq_of(se));
4721 update_load_add(&cfs_rq->load, se->load.weight);
4722 if (entity_is_task(se)) {
4723 struct task_struct *p = task_of(se);
4724 struct rq *rq = rq_of(cfs_rq);
4725
4726 account_numa_enqueue(rq, p);
4727 account_llc_enqueue(rq, p);
4728 list_add(&se->group_node, &rq->cfs_tasks);
4729 }
4730 cfs_rq->nr_queued++;
4731 }
4732
4733 static void
4734 account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
4735 {
4736 WARN_ON_ONCE(cfs_rq != cfs_rq_of(se));
4737 update_load_sub(&cfs_rq->load, se->load.weight);
4738 if (entity_is_task(se)) {
4739 struct task_struct *p = task_of(se);
4740 struct rq *rq = rq_of(cfs_rq);
4741
4742 account_numa_dequeue(rq, p);
4743 account_llc_dequeue(rq, p);
4744 list_del_init(&se->group_node);
4745 }
4746 cfs_rq->nr_queued--;
4747 }
4748
4749 /*
4750 * Signed add and clamp on underflow.
4751 *
4752 * Explicitly do a load-store to ensure the intermediate value never hits
4753 * memory. This allows lockless observations without ever seeing the negative
4754 * values.
4755 */
4756 #define add_positive(_ptr, _val) do { \
4757 typeof(_ptr) ptr = (_ptr); \
4758 __signed_scalar_typeof(*ptr) val = (_val); \
4759 typeof(*ptr) res, var = READ_ONCE(*ptr); \
4760 \
4761 res = var + val; \
4762 \
4763 if (val < 0 && res > var) \
4764 res = 0; \
4765 \
4766 WRITE_ONCE(*ptr, res); \
4767 } while (0)
4768
4769 /*
4770 * Remove and clamp on negative, from a local variable.
4771 *
4772 * A variant of sub_positive(), which does not use explicit load-store
4773 * and is thus optimized for local variable updates.
4774 */
4775 #define lsub_positive(_ptr, _val) do { \
4776 typeof(_ptr) ptr = (_ptr); \
4777 *ptr -= min_t(typeof(*ptr), *ptr, _val); \
4778 } while (0)
4779
4780
4781 /*
4782 * Because of rounding, se->util_sum might ends up being +1 more than
4783 * cfs->util_sum. Although this is not a problem by itself, detaching
4784 * a lot of tasks with the rounding problem between 2 updates of
4785 * util_avg (~1ms) can make cfs->util_sum becoming null whereas
4786 * cfs_util_avg is not.
4787 *
4788 * Check that util_sum is still above its lower bound for the new
4789 * util_avg. Given that period_contrib might have moved since the last
4790 * sync, we are only sure that util_sum must be above or equal to
4791 * util_avg * minimum possible divider
4792 */
4793 #define __update_sa(sa, name, delta_avg, delta_sum) do { \
4794 add_positive(&(sa)->name##_avg, delta_avg); \
4795 add_positive(&(sa)->name##_sum, delta_sum); \
4796 (sa)->name##_sum = max_t(typeof((sa)->name##_sum), \
4797 (sa)->name##_sum, \
4798 (sa)->name##_avg * PELT_MIN_DIVIDER); \
4799 } while (0)
4800
4801 static inline void
4802 enqueue_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se)
4803 {
4804 __update_sa(&cfs_rq->avg, load, se->avg.load_avg,
4805 se_weight(se) * se->avg.load_sum);
4806 }
4807
4808 static inline void
4809 dequeue_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se)
4810 {
4811 __update_sa(&cfs_rq->avg, load, -se->avg.load_avg,
4812 se_weight(se) * -se->avg.load_sum);
4813 }
4814
4815 static void
4816 rescale_entity(struct sched_entity *se, unsigned long weight, bool rel_vprot)
4817 {
4818 long old_weight = se->h_load.weight;
4819
4820 /*
4821 * VRUNTIME
4822 * --------
4823 *
4824 * COROLLARY #1: The virtual runtime of the entity needs to be
4825 * adjusted if re-weight at !0-lag point.
4826 *
4827 * Proof: For contradiction assume this is not true, so we can
4828 * re-weight without changing vruntime at !0-lag point.
4829 *
4830 * Weight VRuntime Avg-VRuntime
4831 * before w v V
4832 * after w' v' V'
4833 *
4834 * Since lag needs to be preserved through re-weight:
4835 *
4836 * lag = (V - v)*w = (V'- v')*w', where v = v'
4837 * ==> V' = (V - v)*w/w' + v (1)
4838 *
4839 * Let W be the total weight of the entities before reweight,
4840 * since V' is the new weighted average of entities:
4841 *
4842 * V' = (WV + w'v - wv) / (W + w' - w) (2)
4843 *
4844 * by using (1) & (2) we obtain:
4845 *
4846 * (WV + w'v - wv) / (W + w' - w) = (V - v)*w/w' + v
4847 * ==> (WV-Wv+Wv+w'v-wv)/(W+w'-w) = (V - v)*w/w' + v
4848 * ==> (WV - Wv)/(W + w' - w) + v = (V - v)*w/w' + v
4849 * ==> (V - v)*W/(W + w' - w) = (V - v)*w/w' (3)
4850 *
4851 * Since we are doing at !0-lag point which means V != v, we
4852 * can simplify (3):
4853 *
4854 * ==> W / (W + w' - w) = w / w'
4855 * ==> Ww' = Ww + ww' - ww
4856 * ==> W * (w' - w) = w * (w' - w)
4857 * ==> W = w (re-weight indicates w' != w)
4858 *
4859 * So the cfs_rq contains only one entity, hence vruntime of
4860 * the entity @v should always equal to the cfs_rq's weighted
4861 * average vruntime @V, which means we will always re-weight
4862 * at 0-lag point, thus breach assumption. Proof completed.
4863 *
4864 *
4865 * COROLLARY #2: Re-weight does NOT affect weighted average
4866 * vruntime of all the entities.
4867 *
4868 * Proof: According to corollary #1, Eq. (1) should be:
4869 *
4870 * (V - v)*w = (V' - v')*w'
4871 * ==> v' = V' - (V - v)*w/w' (4)
4872 *
4873 * According to the weighted average formula, we have:
4874 *
4875 * V' = (WV - wv + w'v') / (W - w + w')
4876 * = (WV - wv + w'(V' - (V - v)w/w')) / (W - w + w')
4877 * = (WV - wv + w'V' - Vw + wv) / (W - w + w')
4878 * = (WV + w'V' - Vw) / (W - w + w')
4879 *
4880 * ==> V'*(W - w + w') = WV + w'V' - Vw
4881 * ==> V' * (W - w) = (W - w) * V (5)
4882 *
4883 * If the entity is the only one in the cfs_rq, then reweight
4884 * always occurs at 0-lag point, so V won't change. Or else
4885 * there are other entities, hence W != w, then Eq. (5) turns
4886 * into V' = V. So V won't change in either case, proof done.
4887 *
4888 *
4889 * So according to corollary #1 & #2, the effect of re-weight
4890 * on vruntime should be:
4891 *
4892 * v' = V' - (V - v) * w / w' (4)
4893 * = V - (V - v) * w / w'
4894 * = V - vl * w / w'
4895 * = V - vl'
4896 */
4897 se->vlag = div64_long(se->vlag * old_weight, weight);
4898
4899 /*
4900 * DEADLINE
4901 * --------
4902 *
4903 * When the weight changes, the virtual time slope changes and
4904 * we should adjust the relative virtual deadline accordingly.
4905 *
4906 * d' = v' + (d - v)*w/w'
4907 * = V' - (V - v)*w/w' + (d - v)*w/w'
4908 * = V - (V - v)*w/w' + (d - v)*w/w'
4909 * = V + (d - V)*w/w'
4910 */
4911 if (se->rel_deadline)
4912 se->deadline = div64_long(se->deadline * old_weight, weight);
4913
4914 if (rel_vprot)
4915 se->vprot = div64_long(se->vprot * old_weight, weight);
4916 }
4917
4918 static void reweight_eevdf(struct cfs_rq *cfs_rq, struct sched_entity *se,
4919 unsigned long weight, bool on_rq)
4920 {
4921 bool curr = cfs_rq->curr == se;
4922 bool rel_vprot = false;
4923 u64 avruntime = 0;
4924
4925 if (se->h_load.weight == weight)
4926 return;
4927
4928 if (on_rq) {
4929 avruntime = avg_vruntime(cfs_rq);
4930 se->vlag = entity_lag(cfs_rq, se, avruntime);
4931 se->deadline -= avruntime;
4932 se->rel_deadline = 1;
4933 if (curr && protect_slice(se)) {
4934 se->vprot -= avruntime;
4935 rel_vprot = true;
4936 }
4937
4938 cfs_rq->h_nr_queued--;
4939 if (!curr)
4940 __dequeue_entity(cfs_rq, se);
4941 }
4942
4943 rescale_entity(se, weight, rel_vprot);
4944
4945 update_load_set(&se->h_load, weight);
4946
4947 if (on_rq) {
4948 if (rel_vprot)
4949 se->vprot += avruntime;
4950 se->deadline += avruntime;
4951 se->rel_deadline = 0;
4952 se->vruntime = avruntime - se->vlag;
4953
4954 if (!curr)
4955 __enqueue_entity(cfs_rq, se);
4956 cfs_rq->h_nr_queued++;
4957 }
4958 }
4959
4960 static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
4961 unsigned long weight)
4962 {
4963 if (se->load.weight == weight)
4964 return;
4965
4966 if (se->on_rq) {
4967 WARN_ON_ONCE(cfs_rq != cfs_rq_of(se));
4968 update_load_sub(&cfs_rq->load, se->load.weight);
4969 }
4970 dequeue_load_avg(cfs_rq, se);
4971
4972 update_load_set(&se->load, weight);
4973
4974 do {
4975 u32 divider = get_pelt_divider(&se->avg);
4976 se->avg.load_avg = div_u64(se_weight(se) * se->avg.load_sum, divider);
4977 } while (0);
4978
4979 enqueue_load_avg(cfs_rq, se);
4980
4981 if (se->on_rq)
4982 update_load_add(&cfs_rq->load, se->load.weight);
4983 }
4984
4985 /*
4986 * weight = NICE_0_LOAD;
4987 * for_each_entity_se(se)
4988 * weight = __calc_prop_weight(cfs_rq_of(se), se, weight);
4989 */
4990 static __always_inline
4991 unsigned long __calc_prop_weight(struct cfs_rq *cfs_rq, struct sched_entity *se,
4992 unsigned long weight)
4993 {
4994 weight *= se->load.weight;
4995 if (parent_entity(se))
4996 weight /= cfs_rq->load.weight;
4997 else
4998 weight /= NICE_0_LOAD;
4999
5000 return max(weight, MIN_SHARES);
5001 }
5002
5003 static void reweight_task_fair(struct rq *rq, struct task_struct *p,
5004 const struct load_weight *lw)
5005 {
5006 struct sched_entity *se = &p->se;
5007 unsigned long weight = NICE_0_LOAD;
5008
5009 if (se->on_rq)
5010 update_curr_fair(rq);
5011
5012 reweight_entity(cfs_rq_of(se), se, lw->weight);
5013 se->load.inv_weight = lw->inv_weight;
5014
5015 if (!se->on_rq)
5016 return;
5017
5018 for_each_sched_entity(se)
5019 weight = __calc_prop_weight(cfs_rq_of(se), se, weight);
5020
5021 reweight_eevdf(&rq->cfs, &p->se, weight, p->se.on_rq);
5022 }
5023
5024 static inline int throttled_hierarchy(struct cfs_rq *cfs_rq);
5025
5026 #ifdef CONFIG_FAIR_GROUP_SCHED
5027 /*
5028 * All this does is approximate the hierarchical proportion which includes that
5029 * global sum we all love to hate.
5030 *
5031 * That is, the weight of a group entity, is the proportional share of the
5032 * group weight based on the group runqueue weights. That is:
5033 *
5034 * tg->weight * grq->load.weight
5035 * ge->load.weight = ----------------------------- (1)
5036 * \Sum grq->load.weight
5037 *
5038 * Now, because computing that sum is prohibitively expensive to compute (been
5039 * there, done that) we approximate it with this average stuff. The average
5040 * moves slower and therefore the approximation is cheaper and more stable.
5041 *
5042 * So instead of the above, we substitute:
5043 *
5044 * grq->load.weight -> grq->avg.load_avg (2)
5045 *
5046 * which yields the following:
5047 *
5048 * tg->weight * grq->avg.load_avg
5049 * ge->load.weight = ------------------------------ (3)
5050 * tg->load_avg
5051 *
5052 * Where: tg->load_avg ~= \Sum grq->avg.load_avg
5053 *
5054 * That is shares_avg, and it is right (given the approximation (2)).
5055 *
5056 * The problem with it is that because the average is slow -- it was designed
5057 * to be exactly that of course -- this leads to transients in boundary
5058 * conditions. In specific, the case where the group was idle and we start the
5059 * one task. It takes time for our CPU's grq->avg.load_avg to build up,
5060 * yielding bad latency etc..
5061 *
5062 * Now, in that special case (1) reduces to:
5063 *
5064 * tg->weight * grq->load.weight
5065 * ge->load.weight = ----------------------------- = tg->weight (4)
5066 * grp->load.weight
5067 *
5068 * That is, the sum collapses because all other CPUs are idle; the UP scenario.
5069 *
5070 * So what we do is modify our approximation (3) to approach (4) in the (near)
5071 * UP case, like:
5072 *
5073 * ge->load.weight =
5074 *
5075 * tg->weight * grq->load.weight
5076 * --------------------------------------------------- (5)
5077 * tg->load_avg - grq->avg.load_avg + grq->load.weight
5078 *
5079 * But because grq->load.weight can drop to 0, resulting in a divide by zero,
5080 * we need to use grq->avg.load_avg as its lower bound, which then gives:
5081 *
5082 *
5083 * tg->weight * grq->load.weight
5084 * ge->load.weight = ----------------------------- (6)
5085 * tg_load_avg'
5086 *
5087 * Where:
5088 *
5089 * tg_load_avg' = tg->load_avg - grq->avg.load_avg +
5090 * max(grq->load.weight, grq->avg.load_avg)
5091 *
5092 * And that is shares_weight and is icky. In the (near) UP case it approaches
5093 * (4) while in the normal case it approaches (3). It consistently
5094 * overestimates the ge->load.weight and therefore:
5095 *
5096 * \Sum ge->load.weight >= tg->weight
5097 *
5098 * hence icky!
5099 */
5100 static long __calc_smp_shares(struct cfs_rq *cfs_rq, long tg_shares, long shares_max)
5101 {
5102 struct task_group *tg = cfs_rq->tg;
5103 long tg_weight, load, shares;
5104
5105 load = max(scale_load_down(cfs_rq->load.weight), cfs_rq->avg.load_avg);
5106
5107 tg_weight = atomic_long_read(&tg->load_avg);
5108
5109 /* Ensure tg_weight >= load */
5110 tg_weight -= cfs_rq->tg_load_avg_contrib;
5111 tg_weight += load;
5112
5113 shares = (tg_shares * load);
5114 if (tg_weight)
5115 shares /= tg_weight;
5116
5117 /*
5118 * MIN_SHARES has to be unscaled here to support per-CPU partitioning
5119 * of a group with small tg->shares value. It is a floor value which is
5120 * assigned as a minimum load.weight to the sched_entity representing
5121 * the group on a CPU.
5122 *
5123 * E.g. on 64-bit for a group with tg->shares of scale_load(15)=15*1024
5124 * on an 8-core system with 8 tasks each runnable on one CPU shares has
5125 * to be 15*1024*1/8=1920 instead of scale_load(MIN_SHARES)=2*1024. In
5126 * case no task is runnable on a CPU MIN_SHARES=2 should be returned
5127 * instead of 0.
5128 */
5129 return clamp_t(long, shares, MIN_SHARES, shares_max);
5130 }
5131
5132 static int tg_cpus(struct task_group *tg)
5133 {
5134 int nr = num_online_cpus();
5135
5136 if (cpusets_enabled()) {
5137 struct cgroup *cgrp = tg->css.cgroup;
5138 if (cgrp)
5139 nr = cpuset_num_cpus(cgrp);
5140 }
5141
5142 /*
5143 * An empty cpuset would propagate a 0 shares_max into
5144 * __calc_smp_shares(), where clamp() yields hi when hi < lo and so
5145 * defeats the MIN_SHARES floor. Match tg_tasks(), which floors at 1.
5146 */
5147 return max(nr, 1);
5148 }
5149
5150 static inline int tg_tasks(struct task_group *tg)
5151 {
5152 return max(1, atomic_long_read(&tg->runnable_avg) >> SCHED_CAPACITY_SHIFT);
5153 }
5154
5155 /*
5156 * Func: fraction(nr_tasks * tg->shares)
5157 *
5158 * Scale tg->shares by the number of tasks.
5159 */
5160 static long calc_tasks_shares(struct cfs_rq *cfs_rq)
5161 {
5162 struct task_group *tg = cfs_rq->tg;
5163 int nr = tg_tasks(tg);
5164 long tg_shares = READ_ONCE(tg->shares);
5165 return __calc_smp_shares(cfs_rq, nr * tg_shares, nr * tg_shares);
5166 }
5167
5168 /*
5169 * Func: min(fraction(nr_cpus * tg->shares), nice -20)
5170 *
5171 * Scale tg->shares by the maximal number of CPUs; but clip the max shares at
5172 * nice -20, otherwise a single spinner on a 512 CPU machine would result in
5173 * 512*NICE_0_LOAD, which is also crazy.
5174 */
5175 static long calc_max_shares(struct cfs_rq *cfs_rq)
5176 {
5177 struct task_group *tg = cfs_rq->tg;
5178 int nr = tg_cpus(tg);
5179 long tg_shares = READ_ONCE(tg->shares);
5180 long max_shares = scale_load(sched_prio_to_weight[0]);
5181 return __calc_smp_shares(cfs_rq, tg_shares * nr, max_shares);
5182 }
5183
5184 /*
5185 * Func: fraction(nr * tg->shares); nr = min(nr_tasks, nr_cpus)
5186 *
5187 * Scales between "smp" and "max" in a natural way. No longer needs clipping
5188 * since there are no unnatural inflations like with "max".
5189 */
5190 static long calc_concur_shares(struct cfs_rq *cfs_rq)
5191 {
5192 struct task_group *tg = cfs_rq->tg;
5193 int nr = min(tg_tasks(tg), tg_cpus(tg));
5194 long tg_shares = READ_ONCE(tg->shares);
5195 return __calc_smp_shares(cfs_rq, nr * tg_shares, nr * tg_shares);
5196 }
5197
5198 /*
5199 * Func: fraction(tg->shares)
5200 *
5201 * This infamously results in tiny shares when you have many CPUs.
5202 */
5203 static long calc_smp_shares(struct cfs_rq *cfs_rq)
5204 {
5205 struct task_group *tg = cfs_rq->tg;
5206 long tg_shares = READ_ONCE(tg->shares);
5207 return __calc_smp_shares(cfs_rq, tg_shares, tg_shares);
5208 }
5209
5210 /*
5211 * Ignore this pesky SMP stuff, use (4).
5212 */
5213 static long calc_up_shares(struct cfs_rq *cfs_rq)
5214 {
5215 struct task_group *tg = cfs_rq->tg;
5216 return READ_ONCE(tg->shares);
5217 }
5218
5219 DEFINE_STATIC_CALL(calc_group_shares, calc_concur_shares);
5220
5221 void __sched_cgroup_mode_update(int mode)
5222 {
5223 long (*func)(struct cfs_rq *);
5224 switch (mode) {
5225 case 0:
5226 func = &calc_up_shares;
5227 break;
5228 case 1:
5229 func = &calc_smp_shares;
5230 break;
5231 case 2:
5232 default:
5233 func = &calc_concur_shares;
5234 break;
5235 case 3:
5236 func = &calc_max_shares;
5237 break;
5238 case 4:
5239 func = &calc_tasks_shares;
5240 break;
5241 }
5242 static_call_update(calc_group_shares, func);
5243 }
5244
5245 /*
5246 * Recomputes the group entity based on the current state of its group
5247 * runqueue.
5248 */
5249 static void update_cfs_group(struct sched_entity *se)
5250 {
5251 struct cfs_rq *gcfs_rq = group_cfs_rq(se);
5252 long shares;
5253
5254 /*
5255 * When a group becomes empty, preserve its weight. This matters for
5256 * DELAY_DEQUEUE.
5257 */
5258 if (!gcfs_rq || !gcfs_rq->load.weight)
5259 return;
5260
5261 shares = static_call(calc_group_shares)(gcfs_rq);
5262 reweight_entity(cfs_rq_of(se), se, shares);
5263 }
5264
5265 #else /* !CONFIG_FAIR_GROUP_SCHED: */
5266 static inline void update_cfs_group(struct sched_entity *se)
5267 {
5268 }
5269 #endif /* !CONFIG_FAIR_GROUP_SCHED */
5270
5271 static inline void cfs_rq_util_change(struct cfs_rq *cfs_rq, int flags)
5272 {
5273 struct rq *rq = rq_of(cfs_rq);
5274
5275 if (&rq->cfs == cfs_rq) {
5276 /*
5277 * There are a few boundary cases this might miss but it should
5278 * get called often enough that that should (hopefully) not be
5279 * a real problem.
5280 *
5281 * It will not get called when we go idle, because the idle
5282 * thread is a different class (!fair), nor will the utilization
5283 * number include things like RT tasks.
5284 *
5285 * As is, the util number is not freq-invariant (we'd have to
5286 * implement arch_scale_freq_capacity() for that).
5287 *
5288 * See cpu_util_cfs().
5289 */
5290 cpufreq_update_util(rq, flags);
5291 }
5292 }
5293
5294 static inline bool load_avg_is_decayed(struct sched_avg *sa)
5295 {
5296 if (sa->load_sum)
5297 return false;
5298
5299 if (sa->util_sum)
5300 return false;
5301
5302 if (sa->runnable_sum)
5303 return false;
5304
5305 /*
5306 * _avg must be null when _sum are null because _avg = _sum / divider
5307 * Make sure that rounding and/or propagation of PELT values never
5308 * break this.
5309 */
5310 WARN_ON_ONCE(sa->load_avg ||
5311 sa->util_avg ||
5312 sa->runnable_avg);
5313
5314 return true;
5315 }
5316
5317 static inline u64 cfs_rq_last_update_time(struct cfs_rq *cfs_rq)
5318 {
5319 return u64_u32_load_copy(cfs_rq->avg.last_update_time,
5320 cfs_rq->last_update_time_copy);
5321 }
5322 #ifdef CONFIG_FAIR_GROUP_SCHED
5323 /*
5324 * Because list_add_leaf_cfs_rq always places a child cfs_rq on the list
5325 * immediately before a parent cfs_rq, and cfs_rqs are removed from the list
5326 * bottom-up, we only have to test whether the cfs_rq before us on the list
5327 * is our child.
5328 * If cfs_rq is not on the list, test whether a child needs its to be added to
5329 * connect a branch to the tree * (see list_add_leaf_cfs_rq() for details).
5330 */
5331 static inline bool child_cfs_rq_on_list(struct cfs_rq *cfs_rq)
5332 {
5333 struct cfs_rq *prev_cfs_rq;
5334 struct list_head *prev;
5335 struct rq *rq = rq_of(cfs_rq);
5336
5337 if (cfs_rq->on_list) {
5338 prev = cfs_rq->leaf_cfs_rq_list.prev;
5339 } else {
5340 prev = rq->tmp_alone_branch;
5341 }
5342
5343 if (prev == &rq->leaf_cfs_rq_list)
5344 return false;
5345
5346 prev_cfs_rq = container_of(prev, struct cfs_rq, leaf_cfs_rq_list);
5347
5348 return (prev_cfs_rq->tg->parent == cfs_rq->tg);
5349 }
5350
5351 static inline bool cfs_rq_is_decayed(struct cfs_rq *cfs_rq)
5352 {
5353 if (cfs_rq->load.weight)
5354 return false;
5355
5356 if (!load_avg_is_decayed(&cfs_rq->avg))
5357 return false;
5358
5359 if (child_cfs_rq_on_list(cfs_rq))
5360 return false;
5361
5362 if (cfs_rq->tg_load_avg_contrib)
5363 return false;
5364
5365 return true;
5366 }
5367
5368 /**
5369 * update_tg_load_avg - update the tg's load avg
5370 * @cfs_rq: the cfs_rq whose avg changed
5371 *
5372 * This function 'ensures': tg->load_avg := \Sum tg->cfs_rq[]->avg.load.
5373 * However, because tg->load_avg is a global value there are performance
5374 * considerations.
5375 *
5376 * In order to avoid having to look at the other cfs_rq's, we use a
5377 * differential update where we store the last value we propagated. This in
5378 * turn allows skipping updates if the differential is 'small'.
5379 *
5380 * Updating tg's load_avg is necessary before update_cfs_group().
5381 */
5382 static inline void update_tg_load_avg(struct cfs_rq *cfs_rq)
5383 {
5384 long dl, dr;
5385 u64 now;
5386
5387 /*
5388 * No need to update load_avg for root_task_group as it is not used.
5389 */
5390 if (cfs_rq->tg == &root_task_group)
5391 return;
5392
5393 /* rq has been offline and doesn't contribute to the share anymore: */
5394 if (!cpu_active(cpu_of(rq_of(cfs_rq))))
5395 return;
5396
5397 /*
5398 * For migration heavy workloads, access to tg->load_avg can be
5399 * unbound. Limit the update rate to at most once per ms.
5400 */
5401 now = rq_clock(rq_of(cfs_rq));
5402 if (now - cfs_rq->last_update_tg_load_avg < NSEC_PER_MSEC)
5403 return;
5404
5405 dl = cfs_rq->avg.load_avg - cfs_rq->tg_load_avg_contrib;
5406 dr = cfs_rq->avg.runnable_avg - cfs_rq->tg_runnable_avg_contrib;
5407 if (abs(dl) > cfs_rq->tg_load_avg_contrib / 64 ||
5408 abs(dr) > cfs_rq->tg_runnable_avg_contrib / 64) {
5409 atomic_long_add(dl, &cfs_rq->tg->load_avg);
5410 atomic_long_add(dr, &cfs_rq->tg->runnable_avg);
5411 cfs_rq->tg_load_avg_contrib = cfs_rq->avg.load_avg;
5412 cfs_rq->tg_runnable_avg_contrib = cfs_rq->avg.runnable_avg;
5413 cfs_rq->last_update_tg_load_avg = now;
5414 }
5415 }
5416
5417 static inline void clear_tg_load_avg(struct cfs_rq *cfs_rq)
5418 {
5419 long dl, dr;
5420 u64 now;
5421
5422 /*
5423 * No need to update load_avg for root_task_group, as it is not used.
5424 */
5425 if (cfs_rq->tg == &root_task_group)
5426 return;
5427
5428 now = rq_clock(rq_of(cfs_rq));
5429 dl = 0 - cfs_rq->tg_load_avg_contrib;
5430 dr = 0 - cfs_rq->tg_runnable_avg_contrib;
5431 atomic_long_add(dl, &cfs_rq->tg->load_avg);
5432 atomic_long_add(dr, &cfs_rq->tg->runnable_avg);
5433 cfs_rq->tg_load_avg_contrib = 0;
5434 cfs_rq->tg_runnable_avg_contrib = 0;
5435 cfs_rq->last_update_tg_load_avg = now;
5436 }
5437
5438 /* CPU offline callback: */
5439 static void __maybe_unused clear_tg_offline_cfs_rqs(struct rq *rq)
5440 {
5441 struct task_group *tg;
5442
5443 lockdep_assert_rq_held(rq);
5444
5445 /*
5446 * The rq clock has already been updated in
5447 * set_rq_offline(), so we should skip updating
5448 * the rq clock again in unthrottle_cfs_rq().
5449 */
5450 rq_clock_start_loop_update(rq);
5451
5452 guard(rcu)();
5453
5454 list_for_each_entry_rcu(tg, &task_groups, list) {
5455 struct cfs_rq *cfs_rq = tg_cfs_rq(tg, cpu_of(rq));
5456
5457 clear_tg_load_avg(cfs_rq);
5458 }
5459
5460 rq_clock_stop_loop_update(rq);
5461 }
5462
5463 /*
5464 * Called within set_task_rq() right before setting a task's CPU. The
5465 * caller only guarantees p->pi_lock is held; no other assumptions,
5466 * including the state of rq->lock, should be made.
5467 */
5468 void set_task_rq_fair(struct sched_entity *se,
5469 struct cfs_rq *prev, struct cfs_rq *next)
5470 {
5471 u64 p_last_update_time;
5472 u64 n_last_update_time;
5473
5474 if (!sched_feat(ATTACH_AGE_LOAD))
5475 return;
5476
5477 /*
5478 * We are supposed to update the task to "current" time, then its up to
5479 * date and ready to go to new CPU/cfs_rq. But we have difficulty in
5480 * getting what current time is, so simply throw away the out-of-date
5481 * time. This will result in the wakee task is less decayed, but giving
5482 * the wakee more load sounds not bad.
5483 */
5484 if (!(se->avg.last_update_time && prev))
5485 return;
5486
5487 p_last_update_time = cfs_rq_last_update_time(prev);
5488 n_last_update_time = cfs_rq_last_update_time(next);
5489
5490 __update_load_avg_blocked_se(p_last_update_time, se);
5491 se->avg.last_update_time = n_last_update_time;
5492 }
5493
5494 /*
5495 * When on migration a sched_entity joins/leaves the PELT hierarchy, we need to
5496 * propagate its contribution. The key to this propagation is the invariant
5497 * that for each group:
5498 *
5499 * ge->avg == grq->avg (1)
5500 *
5501 * _IFF_ we look at the pure running and runnable sums. Because they
5502 * represent the very same entity, just at different points in the hierarchy.
5503 *
5504 * Per the above update_tg_cfs_util() and update_tg_cfs_runnable() are trivial
5505 * and simply copies the running/runnable sum over (but still wrong, because
5506 * the group entity and group rq do not have their PELT windows aligned).
5507 *
5508 * However, update_tg_cfs_load() is more complex. So we have:
5509 *
5510 * ge->avg.load_avg = ge->load.weight * ge->avg.runnable_avg (2)
5511 *
5512 * And since, like util, the runnable part should be directly transferable,
5513 * the following would _appear_ to be the straight forward approach:
5514 *
5515 * grq->avg.load_avg = grq->load.weight * grq->avg.runnable_avg (3)
5516 *
5517 * And per (1) we have:
5518 *
5519 * ge->avg.runnable_avg == grq->avg.runnable_avg
5520 *
5521 * Which gives:
5522 *
5523 * ge->load.weight * grq->avg.load_avg
5524 * ge->avg.load_avg = ----------------------------------- (4)
5525 * grq->load.weight
5526 *
5527 * Except that is wrong!
5528 *
5529 * Because while for entities historical weight is not important and we
5530 * really only care about our future and therefore can consider a pure
5531 * runnable sum, runqueues can NOT do this.
5532 *
5533 * We specifically want runqueues to have a load_avg that includes
5534 * historical weights. Those represent the blocked load, the load we expect
5535 * to (shortly) return to us. This only works by keeping the weights as
5536 * integral part of the sum. We therefore cannot decompose as per (3).
5537 *
5538 * Another reason this doesn't work is that runnable isn't a 0-sum entity.
5539 * Imagine a rq with 2 tasks that each are runnable 2/3 of the time. Then the
5540 * rq itself is runnable anywhere between 2/3 and 1 depending on how the
5541 * runnable section of these tasks overlap (or not). If they were to perfectly
5542 * align the rq as a whole would be runnable 2/3 of the time. If however we
5543 * always have at least 1 runnable task, the rq as a whole is always runnable.
5544 *
5545 * So we'll have to approximate.. :/
5546 *
5547 * Given the constraint:
5548 *
5549 * ge->avg.running_sum <= ge->avg.runnable_sum <= LOAD_AVG_MAX
5550 *
5551 * We can construct a rule that adds runnable to a rq by assuming minimal
5552 * overlap.
5553 *
5554 * On removal, we'll assume each task is equally runnable; which yields:
5555 *
5556 * grq->avg.runnable_sum = grq->avg.load_sum / grq->load.weight
5557 *
5558 * XXX: only do this for the part of runnable > running ?
5559 *
5560 */
5561 static inline void
5562 update_tg_cfs_util(struct cfs_rq *cfs_rq, struct sched_entity *se, struct cfs_rq *gcfs_rq)
5563 {
5564 long delta_sum, delta_avg = gcfs_rq->avg.util_avg - se->avg.util_avg;
5565 u32 new_sum, divider;
5566
5567 /* Nothing to update */
5568 if (!delta_avg)
5569 return;
5570
5571 /*
5572 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se.
5573 * See ___update_load_avg() for details.
5574 */
5575 divider = get_pelt_divider(&cfs_rq->avg);
5576
5577 /* Set new sched_entity's utilization */
5578 se->avg.util_avg = gcfs_rq->avg.util_avg;
5579 new_sum = se->avg.util_avg * divider;
5580 delta_sum = (long)new_sum - (long)se->avg.util_sum;
5581 se->avg.util_sum = new_sum;
5582
5583 /* Update parent cfs_rq utilization */
5584 __update_sa(&cfs_rq->avg, util, delta_avg, delta_sum);
5585 }
5586
5587 static inline void
5588 update_tg_cfs_runnable(struct cfs_rq *cfs_rq, struct sched_entity *se, struct cfs_rq *gcfs_rq)
5589 {
5590 long delta_sum, delta_avg = gcfs_rq->avg.runnable_avg - se->avg.runnable_avg;
5591 u64 new_sum;
5592 u32 divider;
5593
5594 /* Nothing to update */
5595 if (!delta_avg)
5596 return;
5597
5598 /*
5599 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se.
5600 * See ___update_load_avg() for details.
5601 */
5602 divider = get_pelt_divider(&cfs_rq->avg);
5603
5604 /* Set new sched_entity's runnable */
5605 se->avg.runnable_avg = gcfs_rq->avg.runnable_avg;
5606 new_sum = (u64)se->avg.runnable_avg * divider;
5607 delta_sum = (long)new_sum - (long)se->avg.runnable_sum;
5608 se->avg.runnable_sum = new_sum;
5609
5610 /* Update parent cfs_rq runnable */
5611 __update_sa(&cfs_rq->avg, runnable, delta_avg, delta_sum);
5612 }
5613
5614 static inline void
5615 update_tg_cfs_load(struct cfs_rq *cfs_rq, struct sched_entity *se, struct cfs_rq *gcfs_rq)
5616 {
5617 long delta_avg, running_sum, runnable_sum = gcfs_rq->prop_runnable_sum;
5618 unsigned long load_avg;
5619 u64 load_sum = 0;
5620 s64 delta_sum;
5621 u32 divider;
5622
5623 if (!runnable_sum)
5624 return;
5625
5626 gcfs_rq->prop_runnable_sum = 0;
5627
5628 /*
5629 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se.
5630 * See ___update_load_avg() for details.
5631 */
5632 divider = get_pelt_divider(&cfs_rq->avg);
5633
5634 if (runnable_sum >= 0) {
5635 /*
5636 * Add runnable; clip at LOAD_AVG_MAX. Reflects that until
5637 * the CPU is saturated running == runnable.
5638 */
5639 runnable_sum += se->avg.load_sum;
5640 runnable_sum = min_t(long, runnable_sum, divider);
5641 } else {
5642 /*
5643 * Estimate the new unweighted runnable_sum of the gcfs_rq by
5644 * assuming all tasks are equally runnable.
5645 */
5646 if (scale_load_down(gcfs_rq->load.weight)) {
5647 load_sum = div_u64(gcfs_rq->avg.load_sum,
5648 scale_load_down(gcfs_rq->load.weight));
5649 }
5650
5651 /* But make sure to not inflate se's runnable */
5652 runnable_sum = min(se->avg.load_sum, load_sum);
5653 }
5654
5655 /*
5656 * runnable_sum can't be lower than running_sum
5657 * Rescale running sum to be in the same range as runnable sum
5658 * running_sum is in [0 : LOAD_AVG_MAX << SCHED_CAPACITY_SHIFT]
5659 * runnable_sum is in [0 : LOAD_AVG_MAX]
5660 */
5661 running_sum = se->avg.util_sum >> SCHED_CAPACITY_SHIFT;
5662 runnable_sum = max(runnable_sum, running_sum);
5663
5664 load_sum = se_weight(se) * runnable_sum;
5665 load_avg = div_u64(load_sum, divider);
5666
5667 delta_avg = load_avg - se->avg.load_avg;
5668 if (!delta_avg)
5669 return;
5670
5671 delta_sum = load_sum - (s64)se_weight(se) * se->avg.load_sum;
5672
5673 se->avg.load_sum = runnable_sum;
5674 se->avg.load_avg = load_avg;
5675 __update_sa(&cfs_rq->avg, load, delta_avg, delta_sum);
5676 }
5677
5678 static inline void add_tg_cfs_propagate(struct cfs_rq *cfs_rq, long runnable_sum)
5679 {
5680 cfs_rq->propagate = 1;
5681 cfs_rq->prop_runnable_sum += runnable_sum;
5682 }
5683
5684 /* Update task and its cfs_rq load average */
5685 static inline int propagate_entity_load_avg(struct sched_entity *se)
5686 {
5687 struct cfs_rq *cfs_rq, *gcfs_rq;
5688
5689 if (entity_is_task(se))
5690 return 0;
5691
5692 gcfs_rq = group_cfs_rq(se);
5693 if (!gcfs_rq->propagate)
5694 return 0;
5695
5696 gcfs_rq->propagate = 0;
5697
5698 cfs_rq = cfs_rq_of(se);
5699
5700 add_tg_cfs_propagate(cfs_rq, gcfs_rq->prop_runnable_sum);
5701
5702 update_tg_cfs_util(cfs_rq, se, gcfs_rq);
5703 update_tg_cfs_runnable(cfs_rq, se, gcfs_rq);
5704 update_tg_cfs_load(cfs_rq, se, gcfs_rq);
5705
5706 trace_pelt_cfs_tp(cfs_rq);
5707 trace_pelt_se_tp(se);
5708
5709 return 1;
5710 }
5711
5712 /*
5713 * Check if we need to update the load and the utilization of a blocked
5714 * group_entity:
5715 */
5716 static inline bool skip_blocked_update(struct sched_entity *se)
5717 {
5718 struct cfs_rq *gcfs_rq = group_cfs_rq(se);
5719
5720 /*
5721 * If sched_entity still have not zero load or utilization, we have to
5722 * decay it:
5723 */
5724 if (se->avg.load_avg || se->avg.util_avg)
5725 return false;
5726
5727 /*
5728 * If there is a pending propagation, we have to update the load and
5729 * the utilization of the sched_entity:
5730 */
5731 if (gcfs_rq->propagate)
5732 return false;
5733
5734 /*
5735 * Otherwise, the load and the utilization of the sched_entity is
5736 * already zero and there is no pending propagation, so it will be a
5737 * waste of time to try to decay it:
5738 */
5739 return true;
5740 }
5741
5742 #else /* !CONFIG_FAIR_GROUP_SCHED: */
5743
5744 static inline void update_tg_load_avg(struct cfs_rq *cfs_rq) {}
5745
5746 static inline void clear_tg_offline_cfs_rqs(struct rq *rq) {}
5747
5748 static inline int propagate_entity_load_avg(struct sched_entity *se)
5749 {
5750 return 0;
5751 }
5752
5753 static inline void add_tg_cfs_propagate(struct cfs_rq *cfs_rq, long runnable_sum) {}
5754
5755 #endif /* !CONFIG_FAIR_GROUP_SCHED */
5756
5757 #ifdef CONFIG_NO_HZ_COMMON
5758 static inline void migrate_se_pelt_lag(struct sched_entity *se)
5759 {
5760 u64 throttled = 0, now, lut;
5761 struct cfs_rq *cfs_rq;
5762 struct rq *rq;
5763 bool is_idle;
5764
5765 if (load_avg_is_decayed(&se->avg))
5766 return;
5767
5768 cfs_rq = cfs_rq_of(se);
5769 rq = rq_of(cfs_rq);
5770
5771 rcu_read_lock();
5772 is_idle = is_idle_task(rcu_dereference_all(rq->curr));
5773 rcu_read_unlock();
5774
5775 /*
5776 * The lag estimation comes with a cost we don't want to pay all the
5777 * time. Hence, limiting to the case where the source CPU is idle and
5778 * we know we are at the greatest risk to have an outdated clock.
5779 */
5780 if (!is_idle)
5781 return;
5782
5783 /*
5784 * Estimated "now" is: last_update_time + cfs_idle_lag + rq_idle_lag, where:
5785 *
5786 * last_update_time (the cfs_rq's last_update_time)
5787 * = cfs_rq_clock_pelt()@cfs_rq_idle
5788 * = rq_clock_pelt()@cfs_rq_idle
5789 * - cfs->throttled_clock_pelt_time@cfs_rq_idle
5790 *
5791 * cfs_idle_lag (delta between rq's update and cfs_rq's update)
5792 * = rq_clock_pelt()@rq_idle - rq_clock_pelt()@cfs_rq_idle
5793 *
5794 * rq_idle_lag (delta between now and rq's update)
5795 * = sched_clock_cpu() - rq_clock()@rq_idle
5796 *
5797 * We can then write:
5798 *
5799 * now = rq_clock_pelt()@rq_idle - cfs->throttled_clock_pelt_time +
5800 * sched_clock_cpu() - rq_clock()@rq_idle
5801 * Where:
5802 * rq_clock_pelt()@rq_idle is rq->clock_pelt_idle
5803 * rq_clock()@rq_idle is rq->clock_idle
5804 * cfs->throttled_clock_pelt_time@cfs_rq_idle
5805 * is cfs_rq->throttled_pelt_idle
5806 */
5807
5808 #ifdef CONFIG_CFS_BANDWIDTH
5809 throttled = u64_u32_load(cfs_rq->throttled_pelt_idle);
5810 /* The clock has been stopped for throttling */
5811 if (throttled == U64_MAX)
5812 return;
5813 #endif
5814 now = u64_u32_load(rq->clock_pelt_idle);
5815 /*
5816 * Paired with _update_idle_rq_clock_pelt(). It ensures at the worst case
5817 * is observed the old clock_pelt_idle value and the new clock_idle,
5818 * which lead to an underestimation. The opposite would lead to an
5819 * overestimation.
5820 */
5821 smp_rmb();
5822 lut = cfs_rq_last_update_time(cfs_rq);
5823
5824 now -= throttled;
5825 if (now < lut)
5826 /*
5827 * cfs_rq->avg.last_update_time is more recent than our
5828 * estimation, let's use it.
5829 */
5830 now = lut;
5831 else
5832 now += sched_clock_cpu(cpu_of(rq)) - u64_u32_load(rq->clock_idle);
5833
5834 __update_load_avg_blocked_se(now, se);
5835 }
5836 #else /* !CONFIG_NO_HZ_COMMON: */
5837 static void migrate_se_pelt_lag(struct sched_entity *se) {}
5838 #endif /* !CONFIG_NO_HZ_COMMON */
5839
5840 /**
5841 * update_cfs_rq_load_avg - update the cfs_rq's load/util averages
5842 * @now: current time, as per cfs_rq_clock_pelt()
5843 * @cfs_rq: cfs_rq to update
5844 *
5845 * The cfs_rq avg is the direct sum of all its entities (blocked and runnable)
5846 * avg. The immediate corollary is that all (fair) tasks must be attached.
5847 *
5848 * cfs_rq->avg is used for task_h_load() and update_cfs_group() for example.
5849 *
5850 * Return: true if the load decayed or we removed load.
5851 *
5852 * Since both these conditions indicate a changed cfs_rq->avg.load we should
5853 * call update_tg_load_avg() when this function returns true.
5854 */
5855 static inline int
5856 update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq)
5857 {
5858 unsigned long removed_load = 0, removed_util = 0, removed_runnable = 0;
5859 struct sched_avg *sa = &cfs_rq->avg;
5860 int decayed = 0;
5861
5862 if (cfs_rq->removed.nr) {
5863 unsigned long r;
5864 u32 divider = get_pelt_divider(&cfs_rq->avg);
5865
5866 raw_spin_lock(&cfs_rq->removed.lock);
5867 swap(cfs_rq->removed.util_avg, removed_util);
5868 swap(cfs_rq->removed.load_avg, removed_load);
5869 swap(cfs_rq->removed.runnable_avg, removed_runnable);
5870 cfs_rq->removed.nr = 0;
5871 raw_spin_unlock(&cfs_rq->removed.lock);
5872
5873 r = removed_load;
5874 __update_sa(sa, load, -r, -r*divider);
5875
5876 r = removed_util;
5877 __update_sa(sa, util, -r, -r*divider);
5878
5879 r = removed_runnable;
5880 __update_sa(sa, runnable, -r, -r*divider);
5881
5882 /*
5883 * removed_runnable is the unweighted version of removed_load so we
5884 * can use it to estimate removed_load_sum.
5885 */
5886 add_tg_cfs_propagate(cfs_rq,
5887 -(long)(removed_runnable * divider) >> SCHED_CAPACITY_SHIFT);
5888
5889 decayed = 1;
5890 }
5891
5892 decayed |= __update_load_avg_cfs_rq(now, cfs_rq);
5893 u64_u32_store_copy(sa->last_update_time,
5894 cfs_rq->last_update_time_copy,
5895 sa->last_update_time);
5896 return decayed;
5897 }
5898
5899 /**
5900 * attach_entity_load_avg - attach this entity to its cfs_rq load avg
5901 * @cfs_rq: cfs_rq to attach to
5902 * @se: sched_entity to attach
5903 *
5904 * Must call update_cfs_rq_load_avg() before this, since we rely on
5905 * cfs_rq->avg.last_update_time being current.
5906 */
5907 static void attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se)
5908 {
5909 /*
5910 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se.
5911 * See ___update_load_avg() for details.
5912 */
5913 u32 divider = get_pelt_divider(&cfs_rq->avg);
5914
5915 /*
5916 * When we attach the @se to the @cfs_rq, we must align the decay
5917 * window because without that, really weird and wonderful things can
5918 * happen.
5919 *
5920 * XXX illustrate
5921 */
5922 se->avg.last_update_time = cfs_rq->avg.last_update_time;
5923 se->avg.period_contrib = cfs_rq->avg.period_contrib;
5924
5925 /*
5926 * Hell(o) Nasty stuff.. we need to recompute _sum based on the new
5927 * period_contrib. This isn't strictly correct, but since we're
5928 * entirely outside of the PELT hierarchy, nobody cares if we truncate
5929 * _sum a little.
5930 */
5931 se->avg.util_sum = se->avg.util_avg * divider;
5932
5933 se->avg.runnable_sum = se->avg.runnable_avg * divider;
5934
5935 se->avg.load_sum = se->avg.load_avg * divider;
5936 if (se_weight(se) < se->avg.load_sum)
5937 se->avg.load_sum = div_u64(se->avg.load_sum, se_weight(se));
5938 else
5939 se->avg.load_sum = 1;
5940
5941 enqueue_load_avg(cfs_rq, se);
5942 cfs_rq->avg.util_avg += se->avg.util_avg;
5943 cfs_rq->avg.util_sum += se->avg.util_sum;
5944 cfs_rq->avg.runnable_avg += se->avg.runnable_avg;
5945 cfs_rq->avg.runnable_sum += se->avg.runnable_sum;
5946
5947 add_tg_cfs_propagate(cfs_rq, se->avg.load_sum);
5948
5949 cfs_rq_util_change(cfs_rq, 0);
5950
5951 trace_pelt_cfs_tp(cfs_rq);
5952 }
5953
5954 /**
5955 * detach_entity_load_avg - detach this entity from its cfs_rq load avg
5956 * @cfs_rq: cfs_rq to detach from
5957 * @se: sched_entity to detach
5958 *
5959 * Must call update_cfs_rq_load_avg() before this, since we rely on
5960 * cfs_rq->avg.last_update_time being current.
5961 */
5962 static void detach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se)
5963 {
5964 dequeue_load_avg(cfs_rq, se);
5965 __update_sa(&cfs_rq->avg, util, -se->avg.util_avg, -se->avg.util_sum);
5966 __update_sa(&cfs_rq->avg, runnable, -se->avg.runnable_avg, -se->avg.runnable_sum);
5967
5968 add_tg_cfs_propagate(cfs_rq, -se->avg.load_sum);
5969
5970 cfs_rq_util_change(cfs_rq, 0);
5971
5972 trace_pelt_cfs_tp(cfs_rq);
5973 }
5974
5975 #define UTIL_EST_MARGIN (SCHED_CAPACITY_SCALE / 100)
5976
5977 static inline void util_est_update(struct sched_entity *se)
5978 {
5979 unsigned int ewma, dequeued, last_ewma_diff;
5980
5981 if (!sched_feat(UTIL_EST))
5982 return;
5983
5984 /* Get current estimate of utilization */
5985 ewma = READ_ONCE(se->avg.util_est);
5986
5987 /*
5988 * If the PELT values haven't changed since enqueue time,
5989 * skip the util_est update.
5990 */
5991 if (ewma & UTIL_AVG_UNCHANGED)
5992 return;
5993
5994 /* Get utilization at dequeue */
5995 dequeued = READ_ONCE(se->avg.util_avg);
5996
5997 /*
5998 * Reset EWMA on utilization increases, the moving average is used only
5999 * to smooth utilization decreases.
6000 */
6001 if (ewma <= dequeued) {
6002 ewma = dequeued;
6003 goto done;
6004 }
6005
6006 /*
6007 * Skip update of task's estimated utilization when its members are
6008 * already ~1% close to its last activation value.
6009 */
6010 last_ewma_diff = ewma - dequeued;
6011 if (last_ewma_diff < UTIL_EST_MARGIN)
6012 goto done;
6013
6014 /*
6015 * To avoid underestimate of task utilization, skip updates of EWMA if
6016 * we cannot grant that thread got all CPU time it wanted.
6017 */
6018 if ((dequeued + UTIL_EST_MARGIN) < READ_ONCE(se->avg.runnable_avg))
6019 goto done;
6020
6021 /*
6022 * Update Task's estimated utilization
6023 *
6024 * When *p completes an activation we can consolidate another sample
6025 * of the task size. This is done by using this value to update the
6026 * Exponential Weighted Moving Average (EWMA):
6027 *
6028 * ewma(t) = w * task_util(p) + (1-w) * ewma(t-1)
6029 * = w * task_util(p) + ewma(t-1) - w * ewma(t-1)
6030 * = w * (task_util(p) - ewma(t-1)) + ewma(t-1)
6031 * = w * ( -last_ewma_diff ) + ewma(t-1)
6032 * = w * (-last_ewma_diff + ewma(t-1) / w)
6033 *
6034 * Where 'w' is the weight of new samples, which is configured to be
6035 * 0.25, thus making w=1/4 ( >>= UTIL_EST_WEIGHT_SHIFT)
6036 */
6037 ewma <<= UTIL_EST_WEIGHT_SHIFT;
6038 ewma -= last_ewma_diff;
6039 ewma >>= UTIL_EST_WEIGHT_SHIFT;
6040 done:
6041 ewma |= UTIL_AVG_UNCHANGED;
6042 WRITE_ONCE(se->avg.util_est, ewma);
6043
6044 trace_sched_util_est_se_tp(se);
6045 }
6046
6047 /*
6048 * Optional action to be done while updating the load average
6049 */
6050 #define UPDATE_TG 0x01
6051 #define SKIP_AGE_LOAD 0x02
6052 #define DO_ATTACH 0x04
6053 #define DO_DETACH 0x08
6054 #define UPDATE_UTIL_EST 0x10
6055
6056 /* Update task and its cfs_rq load average */
6057 static inline void update_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
6058 {
6059 u64 now = cfs_rq_clock_pelt(cfs_rq);
6060 int decayed;
6061
6062 /*
6063 * Track task load average for carrying it to new CPU after migrated, and
6064 * track group sched_entity load average for task_h_load calculation in migration
6065 */
6066 if (se->avg.last_update_time && !(flags & SKIP_AGE_LOAD))
6067 __update_load_avg_se(now, cfs_rq, se);
6068
6069 decayed = update_cfs_rq_load_avg(now, cfs_rq);
6070 decayed |= propagate_entity_load_avg(se);
6071
6072 if (!se->avg.last_update_time && (flags & DO_ATTACH)) {
6073
6074 /*
6075 * DO_ATTACH means we're here from enqueue_entity().
6076 * !last_update_time means we've passed through
6077 * migrate_task_rq_fair() indicating we migrated.
6078 *
6079 * IOW we're enqueueing a task on a new CPU.
6080 */
6081 attach_entity_load_avg(cfs_rq, se);
6082 update_tg_load_avg(cfs_rq);
6083
6084 } else if (flags & DO_DETACH) {
6085 /*
6086 * DO_DETACH means we're here from dequeue_entity()
6087 * and we are migrating task out of the CPU.
6088 */
6089 detach_entity_load_avg(cfs_rq, se);
6090 update_tg_load_avg(cfs_rq);
6091 } else if (decayed) {
6092 cfs_rq_util_change(cfs_rq, 0);
6093
6094 if (flags & UPDATE_TG)
6095 update_tg_load_avg(cfs_rq);
6096 }
6097
6098 if (flags & UPDATE_UTIL_EST)
6099 util_est_update(se);
6100 }
6101
6102 /*
6103 * Synchronize entity load avg of dequeued entity without locking
6104 * the previous rq.
6105 */
6106 static void sync_entity_load_avg(struct sched_entity *se)
6107 {
6108 struct cfs_rq *cfs_rq = cfs_rq_of(se);
6109 u64 last_update_time;
6110
6111 last_update_time = cfs_rq_last_update_time(cfs_rq);
6112 __update_load_avg_blocked_se(last_update_time, se);
6113 }
6114
6115 /*
6116 * Task first catches up with cfs_rq, and then subtract
6117 * itself from the cfs_rq (task must be off the queue now).
6118 */
6119 static void remove_entity_load_avg(struct sched_entity *se)
6120 {
6121 struct cfs_rq *cfs_rq = cfs_rq_of(se);
6122 unsigned long flags;
6123
6124 /*
6125 * tasks cannot exit without having gone through wake_up_new_task() ->
6126 * enqueue_task_fair() which will have added things to the cfs_rq,
6127 * so we can remove unconditionally.
6128 */
6129
6130 sync_entity_load_avg(se);
6131
6132 raw_spin_lock_irqsave(&cfs_rq->removed.lock, flags);
6133 ++cfs_rq->removed.nr;
6134 cfs_rq->removed.util_avg += se->avg.util_avg;
6135 cfs_rq->removed.load_avg += se->avg.load_avg;
6136 cfs_rq->removed.runnable_avg += se->avg.runnable_avg;
6137 raw_spin_unlock_irqrestore(&cfs_rq->removed.lock, flags);
6138 }
6139
6140 static inline unsigned long cfs_rq_runnable_avg(struct cfs_rq *cfs_rq)
6141 {
6142 return cfs_rq->avg.runnable_avg;
6143 }
6144
6145 static inline unsigned long cfs_rq_load_avg(struct cfs_rq *cfs_rq)
6146 {
6147 return cfs_rq->avg.load_avg;
6148 }
6149
6150 static int sched_balance_newidle(struct rq *this_rq, struct rq_flags *rf)
6151 __must_hold(__rq_lockp(this_rq));
6152
6153 static inline unsigned long task_util(struct task_struct *p)
6154 {
6155 return READ_ONCE(p->se.avg.util_avg);
6156 }
6157
6158 static inline unsigned long _task_util_est(struct task_struct *p)
6159 {
6160 return READ_ONCE(p->se.avg.util_est) & ~UTIL_AVG_UNCHANGED;
6161 }
6162
6163 static inline unsigned long task_util_est(struct task_struct *p)
6164 {
6165 return max(task_util(p), _task_util_est(p));
6166 }
6167
6168 static inline void util_est_enqueue(struct cfs_rq *cfs_rq,
6169 struct task_struct *p)
6170 {
6171 unsigned int enqueued;
6172
6173 if (!sched_feat(UTIL_EST))
6174 return;
6175
6176 /* Update root cfs_rq's estimated utilization */
6177 enqueued = cfs_rq->avg.util_est;
6178 enqueued += _task_util_est(p);
6179 WRITE_ONCE(cfs_rq->avg.util_est, enqueued);
6180
6181 trace_sched_util_est_cfs_tp(cfs_rq);
6182 }
6183
6184 static inline void util_est_dequeue(struct cfs_rq *cfs_rq,
6185 struct task_struct *p)
6186 {
6187 unsigned int enqueued;
6188
6189 if (!sched_feat(UTIL_EST))
6190 return;
6191
6192 /* Update root cfs_rq's estimated utilization */
6193 enqueued = cfs_rq->avg.util_est;
6194 enqueued -= min_t(unsigned int, enqueued, _task_util_est(p));
6195 WRITE_ONCE(cfs_rq->avg.util_est, enqueued);
6196
6197 trace_sched_util_est_cfs_tp(cfs_rq);
6198 }
6199
6200 static inline unsigned long get_actual_cpu_capacity(int cpu)
6201 {
6202 unsigned long capacity = arch_scale_cpu_capacity(cpu);
6203
6204 capacity -= max(hw_load_avg(cpu_rq(cpu)), cpufreq_get_pressure(cpu));
6205
6206 return capacity;
6207 }
6208
6209 static inline int util_fits_cpu(unsigned long util,
6210 unsigned long uclamp_min,
6211 unsigned long uclamp_max,
6212 int cpu)
6213 {
6214 unsigned long capacity = capacity_of(cpu);
6215 unsigned long capacity_orig;
6216 bool fits, uclamp_max_fits;
6217
6218 /*
6219 * Check if the real util fits without any uclamp boost/cap applied.
6220 */
6221 fits = fits_capacity(util, capacity);
6222
6223 if (!uclamp_is_used())
6224 return fits;
6225
6226 /*
6227 * We must use arch_scale_cpu_capacity() for comparing against uclamp_min and
6228 * uclamp_max. We only care about capacity pressure (by using
6229 * capacity_of()) for comparing against the real util.
6230 *
6231 * If a task is boosted to 1024 for example, we don't want a tiny
6232 * pressure to skew the check whether it fits a CPU or not.
6233 *
6234 * Similarly if a task is capped to arch_scale_cpu_capacity(little_cpu), it
6235 * should fit a little cpu even if there's some pressure.
6236 *
6237 * Only exception is for HW or cpufreq pressure since it has a direct impact
6238 * on available OPP of the system.
6239 *
6240 * We honour it for uclamp_min only as a drop in performance level
6241 * could result in not getting the requested minimum performance level.
6242 *
6243 * For uclamp_max, we can tolerate a drop in performance level as the
6244 * goal is to cap the task. So it's okay if it's getting less.
6245 */
6246 capacity_orig = arch_scale_cpu_capacity(cpu);
6247
6248 /*
6249 * We want to force a task to fit a cpu as implied by uclamp_max.
6250 * But we do have some corner cases to cater for..
6251 *
6252 *
6253 * C=z
6254 * | ___
6255 * | C=y | |
6256 * |_ _ _ _ _ _ _ _ _ ___ _ _ _ | _ | _ _ _ _ _ uclamp_max
6257 * | C=x | | | |
6258 * | ___ | | | |
6259 * | | | | | | | (util somewhere in this region)
6260 * | | | | | | |
6261 * | | | | | | |
6262 * +----------------------------------------
6263 * CPU0 CPU1 CPU2
6264 *
6265 * In the above example if a task is capped to a specific performance
6266 * point, y, then when:
6267 *
6268 * * util = 80% of x then it does not fit on CPU0 and should migrate
6269 * to CPU1
6270 * * util = 80% of y then it is forced to fit on CPU1 to honour
6271 * uclamp_max request.
6272 *
6273 * which is what we're enforcing here. A task always fits if
6274 * uclamp_max <= capacity_orig. But when uclamp_max > capacity_orig,
6275 * the normal upmigration rules should withhold still.
6276 *
6277 * Only exception is when we are on max capacity, then we need to be
6278 * careful not to block overutilized state. This is so because:
6279 *
6280 * 1. There's no concept of capping at max_capacity! We can't go
6281 * beyond this performance level anyway.
6282 * 2. The system is being saturated when we're operating near
6283 * max capacity, it doesn't make sense to block overutilized.
6284 */
6285 uclamp_max_fits = (capacity_orig == SCHED_CAPACITY_SCALE) && (uclamp_max == SCHED_CAPACITY_SCALE);
6286 uclamp_max_fits = !uclamp_max_fits && (uclamp_max <= capacity_orig);
6287 fits = fits || uclamp_max_fits;
6288
6289 /*
6290 *
6291 * C=z
6292 * | ___ (region a, capped, util >= uclamp_max)
6293 * | C=y | |
6294 * |_ _ _ _ _ _ _ _ _ ___ _ _ _ | _ | _ _ _ _ _ uclamp_max
6295 * | C=x | | | |
6296 * | ___ | | | | (region b, uclamp_min <= util <= uclamp_max)
6297 * |_ _ _|_ _|_ _ _ _| _ | _ _ _| _ | _ _ _ _ _ uclamp_min
6298 * | | | | | | |
6299 * | | | | | | | (region c, boosted, util < uclamp_min)
6300 * +----------------------------------------
6301 * CPU0 CPU1 CPU2
6302 *
6303 * a) If util > uclamp_max, then we're capped, we don't care about
6304 * actual fitness value here. We only care if uclamp_max fits
6305 * capacity without taking margin/pressure into account.
6306 * See comment above.
6307 *
6308 * b) If uclamp_min <= util <= uclamp_max, then the normal
6309 * fits_capacity() rules apply. Except we need to ensure that we
6310 * enforce we remain within uclamp_max, see comment above.
6311 *
6312 * c) If util < uclamp_min, then we are boosted. Same as (b) but we
6313 * need to take into account the boosted value fits the CPU without
6314 * taking margin/pressure into account.
6315 *
6316 * Cases (a) and (b) are handled in the 'fits' variable already. We
6317 * just need to consider an extra check for case (c) after ensuring we
6318 * handle the case uclamp_min > uclamp_max.
6319 */
6320 uclamp_min = min(uclamp_min, uclamp_max);
6321 if (fits && (util < uclamp_min) &&
6322 (uclamp_min > get_actual_cpu_capacity(cpu)))
6323 return -1;
6324
6325 return fits;
6326 }
6327
6328 static inline int task_fits_cpu(struct task_struct *p, int cpu)
6329 {
6330 unsigned long uclamp_min = uclamp_eff_value(p, UCLAMP_MIN);
6331 unsigned long uclamp_max = uclamp_eff_value(p, UCLAMP_MAX);
6332 unsigned long util = task_util_est(p);
6333 /*
6334 * Return true only if the cpu fully fits the task requirements, which
6335 * include the utilization but also the performance hints.
6336 */
6337 return (util_fits_cpu(util, uclamp_min, uclamp_max, cpu) > 0);
6338 }
6339
6340 static inline void update_misfit_status(struct task_struct *p, struct rq *rq)
6341 {
6342 int cpu = cpu_of(rq);
6343
6344 if (!sched_asym_cpucap_active())
6345 return;
6346
6347 /*
6348 * Affinity allows us to go somewhere higher? Or are we on biggest
6349 * available CPU already? Or do we fit into this CPU ?
6350 */
6351 if (!p || (p->nr_cpus_allowed == 1) ||
6352 (arch_scale_cpu_capacity(cpu) == p->max_allowed_capacity) ||
6353 task_fits_cpu(p, cpu)) {
6354
6355 rq->misfit_task_load = 0;
6356 return;
6357 }
6358
6359 /*
6360 * Make sure that misfit_task_load will not be null even if
6361 * task_h_load() returns 0.
6362 */
6363 rq->misfit_task_load = max_t(unsigned long, task_h_load(p), 1);
6364 }
6365
6366 void __setparam_fair(struct task_struct *p, const struct sched_attr *attr)
6367 {
6368 struct sched_entity *se = &p->se;
6369
6370 p->static_prio = NICE_TO_PRIO(attr->sched_nice);
6371 if (attr->sched_runtime) {
6372 se->custom_slice = 1;
6373 se->slice = clamp_t(u64, attr->sched_runtime,
6374 NSEC_PER_MSEC/10, /* HZ=1000 * 10 */
6375 NSEC_PER_MSEC*100); /* HZ=100 / 10 */
6376 } else {
6377 se->custom_slice = 0;
6378 se->slice = sysctl_sched_base_slice;
6379 }
6380 }
6381
6382 static void
6383 place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
6384 {
6385 u64 vslice, vruntime = avg_vruntime(cfs_rq);
6386 unsigned int nr_queued = cfs_rq->h_nr_queued;
6387 bool update_zero = false;
6388 s64 lag = 0;
6389
6390 if (!se->custom_slice)
6391 se->slice = sysctl_sched_base_slice;
6392 vslice = calc_delta_fair(se->slice, se);
6393
6394 if (flags & ENQUEUE_QUEUED)
6395 nr_queued -= 1;
6396
6397 /*
6398 * Due to how V is constructed as the weighted average of entities,
6399 * adding tasks with positive lag, or removing tasks with negative lag
6400 * will move 'time' backwards, this can screw around with the lag of
6401 * other tasks.
6402 *
6403 * EEVDF: placement strategy #1 / #2
6404 */
6405 if (sched_feat(PLACE_LAG) && nr_queued && se->vlag) {
6406 struct sched_entity *curr = cfs_rq->curr;
6407 long load, weight;
6408
6409 lag = se->vlag;
6410
6411 /*
6412 * If we want to place a task and preserve lag, we have to
6413 * consider the effect of the new entity on the weighted
6414 * average and compensate for this, otherwise lag can quickly
6415 * evaporate.
6416 *
6417 * Lag is defined as:
6418 *
6419 * lag_i = S - s_i = w_i * (V - v_i)
6420 *
6421 * To avoid the 'w_i' term all over the place, we only track
6422 * the virtual lag:
6423 *
6424 * vl_i = V - v_i <=> v_i = V - vl_i
6425 *
6426 * And we take V to be the weighted average of all v:
6427 *
6428 * V = (\Sum w_j*v_j) / W
6429 *
6430 * Where W is: \Sum w_j
6431 *
6432 * Then, the weighted average after adding an entity with lag
6433 * vl_i is given by:
6434 *
6435 * V' = (\Sum w_j*v_j + w_i*v_i) / (W + w_i)
6436 * = (W*V + w_i*(V - vl_i)) / (W + w_i)
6437 * = (W*V + w_i*V - w_i*vl_i) / (W + w_i)
6438 * = (V*(W + w_i) - w_i*vl_i) / (W + w_i)
6439 * = V - w_i*vl_i / (W + w_i)
6440 *
6441 * And the actual lag after adding an entity with vl_i is:
6442 *
6443 * vl'_i = V' - v_i
6444 * = V - w_i*vl_i / (W + w_i) - (V - vl_i)
6445 * = vl_i - w_i*vl_i / (W + w_i)
6446 *
6447 * Which is strictly less than vl_i. So in order to preserve lag
6448 * we should inflate the lag before placement such that the
6449 * effective lag after placement comes out right.
6450 *
6451 * As such, invert the above relation for vl'_i to get the vl_i
6452 * we need to use such that the lag after placement is the lag
6453 * we computed before dequeue.
6454 *
6455 * vl'_i = vl_i - w_i*vl_i / (W + w_i)
6456 * = ((W + w_i)*vl_i - w_i*vl_i) / (W + w_i)
6457 *
6458 * (W + w_i)*vl'_i = (W + w_i)*vl_i - w_i*vl_i
6459 * = W*vl_i
6460 *
6461 * vl_i = (W + w_i)*vl'_i / W
6462 */
6463 load = cfs_rq->sum_weight;
6464 if (curr && curr->on_rq)
6465 load += avg_vruntime_weight(cfs_rq, curr->h_load.weight);
6466 if (WARN_ON_ONCE(!load))
6467 load = 1;
6468
6469 weight = avg_vruntime_weight(cfs_rq, se->h_load.weight);
6470
6471 /*
6472 * Rewrite, because the product (W + w_i)*vl'_i might
6473 * overflow s64:
6474 *
6475 * vl_i = (W + w_i)*vl'_i / W
6476 * = (W*vl'_i + w_i*vl'_i) / W
6477 * = vl'_i + w_i*vl'_i/W
6478 */
6479 lag += div64_long(lag * weight, load);
6480
6481 /*
6482 * A heavy entity (relative to the tree) will pull the
6483 * avg_vruntime close to its vruntime position on enqueue. But
6484 * the zero_vruntime point is only updated at the next
6485 * update_deadline()/place_entity()/update_entity_lag().
6486 *
6487 * Specifically (see the comment near avg_vruntime_weight()):
6488 *
6489 * sum_w_vruntime = \Sum (v_i - v0) * w_i
6490 *
6491 * Note that if v0 is near a light entity, both terms will be
6492 * small for the light entity, while in that case both terms
6493 * are large for the heavy entity, leading to risk of
6494 * overflow.
6495 *
6496 * OTOH if v0 is near the heavy entity, then the difference is
6497 * larger for the light entity, but the factor is small, while
6498 * for the heavy entity the difference is small but the factor
6499 * is large. Avoiding the multiplication overflow.
6500 */
6501 if (weight > load)
6502 update_zero = true;
6503 }
6504
6505 se->vruntime = vruntime - lag;
6506
6507 if (update_zero)
6508 update_zero_vruntime(cfs_rq, -lag);
6509
6510 if (sched_feat(PLACE_REL_DEADLINE) && se->rel_deadline) {
6511 se->deadline += se->vruntime;
6512 se->rel_deadline = 0;
6513 return;
6514 }
6515
6516 /*
6517 * When joining the competition; the existing tasks will be,
6518 * on average, halfway through their slice, as such start tasks
6519 * off with half a slice to ease into the competition.
6520 */
6521 if (sched_feat(PLACE_DEADLINE_INITIAL) && (flags & ENQUEUE_INITIAL))
6522 vslice /= 2;
6523
6524 /*
6525 * EEVDF: vd_i = ve_i + r_i/w_i
6526 */
6527 se->deadline = se->vruntime + vslice;
6528 }
6529
6530 static void check_enqueue_throttle(struct cfs_rq *cfs_rq);
6531 static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq);
6532
6533 static void
6534 enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
6535 {
6536 /*
6537 * When enqueuing a sched_entity, we must:
6538 * - Update loads to have both entity and cfs_rq synced with now.
6539 * - For group_entity, update its runnable_weight to reflect the new
6540 * h_nr_runnable of its group cfs_rq.
6541 * - For group_entity, update its weight to reflect the new share of
6542 * its group cfs_rq
6543 * - Add its new weight to cfs_rq->load.weight
6544 */
6545 update_load_avg(cfs_rq, se, UPDATE_TG | DO_ATTACH);
6546 se_update_runnable(se);
6547 /*
6548 * XXX update_load_avg() above will have attached us to the pelt sum;
6549 * but update_cfs_group() here will re-adjust the weight and have to
6550 * undo/redo all that. Seems wasteful.
6551 */
6552 update_cfs_group(se);
6553
6554 account_entity_enqueue(cfs_rq, se);
6555
6556 /* Entity has migrated, no longer consider this task hot */
6557 if (flags & ENQUEUE_MIGRATED)
6558 se->exec_start = 0;
6559
6560 check_schedstat_required();
6561 update_stats_enqueue_fair(cfs_rq, se, flags);
6562 se->on_rq = 1;
6563
6564 if (cfs_rq->nr_queued == 1) {
6565 check_enqueue_throttle(cfs_rq);
6566 list_add_leaf_cfs_rq(cfs_rq);
6567 #ifdef CONFIG_CFS_BANDWIDTH
6568 if (cfs_rq->pelt_clock_throttled) {
6569 struct rq *rq = rq_of(cfs_rq);
6570
6571 cfs_rq->throttled_clock_pelt_time += rq_clock_pelt(rq) -
6572 cfs_rq->throttled_clock_pelt;
6573 cfs_rq->pelt_clock_throttled = 0;
6574 }
6575 #endif
6576 }
6577 }
6578
6579 static void set_next_buddy(struct cfs_rq *cfs_rq, struct sched_entity *se)
6580 {
6581 if (WARN_ON_ONCE(!se->on_rq || se->sched_delayed))
6582 return;
6583 if (se_is_idle(se))
6584 return;
6585 cfs_rq->next = se;
6586 }
6587
6588 static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
6589 {
6590 if (cfs_rq->next == se)
6591 cfs_rq->next = NULL;
6592 }
6593
6594 static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq);
6595
6596 static void set_delayed(struct sched_entity *se)
6597 {
6598 /*
6599 * Delayed se of cfs_rq have no tasks queued on them.
6600 * Do not adjust h_nr_runnable since __dequeue_task()
6601 * will account it for blocked tasks.
6602 *
6603 * This check can be removed because when flat pick
6604 * patches get merged as only task can get delayed,
6605 * same for clear_delayed().
6606 */
6607 if (!entity_is_task(se)) {
6608 se->sched_delayed = 1;
6609 return;
6610 }
6611
6612 /*
6613 * Drop a task leaving the runnable set.
6614 * Needs to be called before sched_delayed is set.
6615 * clear_delayed() mirrors this after clearing the flag.
6616 */
6617 pref_llc_running_dec(rq_of(cfs_rq_of(se)), task_of(se));
6618 se->sched_delayed = 1;
6619
6620 for_each_sched_entity(se) {
6621 struct cfs_rq *cfs_rq = cfs_rq_of(se);
6622
6623 cfs_rq->h_nr_runnable--;
6624 }
6625 }
6626
6627 static void clear_delayed(struct sched_entity *se)
6628 {
6629 se->sched_delayed = 0;
6630
6631 /*
6632 * Delayed se of cfs_rq have no tasks queued on them.
6633 * Do not adjust h_nr_runnable since a dequeue has
6634 * already accounted for it or an enqueue of a task
6635 * below it will account for it in enqueue_task_fair().
6636 */
6637 if (!entity_is_task(se))
6638 return;
6639
6640 /*
6641 * Re-add on wake, after sched_delayed is cleared. On a final delayed
6642 * dequeue account_llc_dequeue() already cleared pref_llc_queued, so
6643 * this does nothing.
6644 */
6645 pref_llc_running_inc(rq_of(cfs_rq_of(se)), task_of(se));
6646
6647 for_each_sched_entity(se) {
6648 struct cfs_rq *cfs_rq = cfs_rq_of(se);
6649
6650 cfs_rq->h_nr_runnable++;
6651 }
6652 }
6653
6654 static void
6655 dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
6656 {
6657 int action = UPDATE_TG;
6658
6659 if (entity_is_task(se)) {
6660 if (task_on_rq_migrating(task_of(se)))
6661 action |= DO_DETACH;
6662
6663 if ((flags & DEQUEUE_SLEEP) && !(flags & DEQUEUE_DELAYED))
6664 action |= UPDATE_UTIL_EST;
6665 }
6666
6667 /*
6668 * When dequeuing a sched_entity, we must:
6669 * - Update loads to have both entity and cfs_rq synced with now.
6670 * - For group_entity, update its runnable_weight to reflect the new
6671 * h_nr_runnable of its group cfs_rq.
6672 * - Subtract its previous weight from cfs_rq->load.weight.
6673 * - For group entity, update its weight to reflect the new share
6674 * of its group cfs_rq.
6675 */
6676 update_load_avg(cfs_rq, se, action);
6677 se_update_runnable(se);
6678
6679 update_stats_dequeue_fair(cfs_rq, se, flags);
6680
6681 se->on_rq = 0;
6682 account_entity_dequeue(cfs_rq, se);
6683
6684 /* return excess runtime on last dequeue */
6685 return_cfs_rq_runtime(cfs_rq);
6686
6687 update_cfs_group(se);
6688
6689 if (cfs_rq->nr_queued == 0) {
6690 update_idle_cfs_rq_clock_pelt(cfs_rq);
6691 #ifdef CONFIG_CFS_BANDWIDTH
6692 if (throttled_hierarchy(cfs_rq)) {
6693 struct rq *rq = rq_of(cfs_rq);
6694
6695 list_del_leaf_cfs_rq(cfs_rq);
6696 cfs_rq->throttled_clock_pelt = rq_clock_pelt(rq);
6697 cfs_rq->pelt_clock_throttled = 1;
6698 }
6699 #endif
6700 }
6701 }
6702
6703 static void
6704 set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
6705 {
6706 /* 'current' is not kept within the tree. */
6707 if (se->on_rq) {
6708 /*
6709 * Any task has to be enqueued before it get to execute on
6710 * a CPU. So account for the time it spent waiting on the
6711 * runqueue.
6712 */
6713 update_stats_wait_end_fair(cfs_rq, se);
6714 update_load_avg(cfs_rq, se, UPDATE_TG);
6715 }
6716
6717 update_stats_curr_start(cfs_rq, se);
6718 WARN_ON_ONCE(cfs_rq->h_curr);
6719 cfs_rq->h_curr = se;
6720
6721 /*
6722 * Track our maximum slice length, if the CPU's load is at
6723 * least twice that of our own weight (i.e. don't track it
6724 * when there are only lesser-weight tasks around):
6725 */
6726 if (schedstat_enabled() &&
6727 rq_of(cfs_rq)->cfs.load.weight >= 2*se->load.weight) {
6728 struct sched_statistics *stats;
6729
6730 stats = __schedstats_from_se(se);
6731 __schedstat_set(stats->slice_max,
6732 max((u64)stats->slice_max,
6733 se->sum_exec_runtime - se->prev_sum_exec_runtime));
6734 }
6735
6736 se->prev_sum_exec_runtime = se->sum_exec_runtime;
6737 }
6738
6739 static bool __dequeue_task(struct rq *rq, struct task_struct *p, int flags);
6740
6741 static struct sched_entity *
6742 pick_next_entity(struct rq *rq, bool protect)
6743 {
6744 struct cfs_rq *cfs_rq = &rq->cfs;
6745 struct sched_entity *se;
6746
6747 se = pick_eevdf(cfs_rq, protect);
6748 if (se->sched_delayed) {
6749 __dequeue_task(rq, task_of(se), DEQUEUE_SLEEP | DEQUEUE_DELAYED);
6750 /*
6751 * Must not reference @se again, see __block_task().
6752 */
6753 return NULL;
6754 }
6755 return se;
6756 }
6757
6758 static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
6759 {
6760 /*
6761 * If still on the runqueue then deactivate_task()
6762 * was not called and update_curr() has to be done:
6763 */
6764 if (prev->on_rq)
6765 update_curr(cfs_rq);
6766
6767 if (prev->on_rq) {
6768 update_stats_wait_start_fair(cfs_rq, prev);
6769 /* in !on_rq case, update occurred at dequeue */
6770 update_load_avg(cfs_rq, prev, 0);
6771 }
6772 WARN_ON_ONCE(cfs_rq->h_curr != prev);
6773 cfs_rq->h_curr = NULL;
6774 }
6775
6776 static void
6777 entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
6778 {
6779 /*
6780 * Update run-time statistics of the 'current'.
6781 */
6782 update_curr(cfs_rq);
6783
6784 /*
6785 * Ensure that runnable average is periodically updated.
6786 */
6787 update_load_avg(cfs_rq, curr, UPDATE_TG);
6788 update_cfs_group(curr);
6789
6790 #ifdef CONFIG_SCHED_HRTICK
6791 /*
6792 * queued ticks are scheduled to match the slice, so don't bother
6793 * validating it and just reschedule.
6794 */
6795 if (queued) {
6796 resched_curr(rq_of(cfs_rq));
6797 return;
6798 }
6799 #endif
6800 }
6801
6802
6803 /**************************************************
6804 * CFS bandwidth control machinery
6805 */
6806
6807 #ifdef CONFIG_CFS_BANDWIDTH
6808
6809 #ifdef CONFIG_JUMP_LABEL
6810 static struct static_key __cfs_bandwidth_used;
6811
6812 static inline bool cfs_bandwidth_used(void)
6813 {
6814 return static_key_false(&__cfs_bandwidth_used);
6815 }
6816
6817 void cfs_bandwidth_usage_inc(void)
6818 {
6819 static_key_slow_inc_cpuslocked(&__cfs_bandwidth_used);
6820 }
6821
6822 void cfs_bandwidth_usage_dec(void)
6823 {
6824 static_key_slow_dec_cpuslocked(&__cfs_bandwidth_used);
6825 }
6826 #else /* !CONFIG_JUMP_LABEL: */
6827 static bool cfs_bandwidth_used(void)
6828 {
6829 return true;
6830 }
6831
6832 void cfs_bandwidth_usage_inc(void) {}
6833 void cfs_bandwidth_usage_dec(void) {}
6834 #endif /* !CONFIG_JUMP_LABEL */
6835
6836 static inline u64 sched_cfs_bandwidth_slice(void)
6837 {
6838 return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC;
6839 }
6840
6841 /*
6842 * Replenish runtime according to assigned quota. We use sched_clock_cpu
6843 * directly instead of rq->clock to avoid adding additional synchronization
6844 * around rq->lock.
6845 *
6846 * requires cfs_b->lock
6847 */
6848 void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b)
6849 {
6850 s64 runtime;
6851
6852 if (unlikely(cfs_b->quota == RUNTIME_INF))
6853 return;
6854
6855 cfs_b->runtime += cfs_b->quota;
6856 runtime = cfs_b->runtime_snap - cfs_b->runtime;
6857 if (runtime > 0) {
6858 cfs_b->burst_time += runtime;
6859 cfs_b->nr_burst++;
6860 }
6861
6862 cfs_b->runtime = min(cfs_b->runtime, cfs_b->quota + cfs_b->burst);
6863 cfs_b->runtime_snap = cfs_b->runtime;
6864 }
6865
6866 static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
6867 {
6868 return &tg->cfs_bandwidth;
6869 }
6870
6871 /* returns 0 on failure to allocate runtime */
6872 static int __assign_cfs_rq_runtime(struct cfs_bandwidth *cfs_b,
6873 struct cfs_rq *cfs_rq, u64 target_runtime)
6874 {
6875 u64 min_amount, amount = 0;
6876
6877 lockdep_assert_held(&cfs_b->lock);
6878
6879 /* note: this is a positive sum as runtime_remaining <= 0 */
6880 min_amount = target_runtime - cfs_rq->runtime_remaining;
6881
6882 if (cfs_b->quota == RUNTIME_INF)
6883 amount = min_amount;
6884 else {
6885 start_cfs_bandwidth(cfs_b);
6886
6887 if (cfs_b->runtime > 0) {
6888 amount = min(cfs_b->runtime, min_amount);
6889 cfs_b->runtime -= amount;
6890 cfs_b->idle = 0;
6891 }
6892 }
6893
6894 cfs_rq->runtime_remaining += amount;
6895
6896 return cfs_rq->runtime_remaining > 0;
6897 }
6898
6899 static bool throttle_cfs_rq(struct cfs_rq *cfs_rq);
6900
6901 static bool __account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec)
6902 {
6903 /* dock delta_exec before expiring quota (as it could span periods) */
6904 cfs_rq->runtime_remaining -= delta_exec;
6905
6906 if (likely(cfs_rq->runtime_remaining > 0))
6907 return false;
6908
6909 if (cfs_rq->throttled)
6910 return true;
6911 /*
6912 * throttle_cfs_rq() will try to extend the runtime first
6913 * before throttling the hierarchy.
6914 */
6915 return throttle_cfs_rq(cfs_rq);
6916 }
6917
6918 static __always_inline
6919 bool account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec)
6920 {
6921 if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled)
6922 return false;
6923
6924 return __account_cfs_rq_runtime(cfs_rq, delta_exec);
6925 }
6926
6927 static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
6928 {
6929 return cfs_bandwidth_used() && cfs_rq->throttled;
6930 }
6931
6932 static inline bool cfs_rq_pelt_clock_throttled(struct cfs_rq *cfs_rq)
6933 {
6934 return cfs_bandwidth_used() && cfs_rq->pelt_clock_throttled;
6935 }
6936
6937 /* check whether cfs_rq, or any parent, is throttled */
6938 static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
6939 {
6940 return cfs_bandwidth_used() && cfs_rq->throttle_count;
6941 }
6942
6943 static inline int lb_throttled_hierarchy(struct task_struct *p, int dst_cpu)
6944 {
6945 return throttled_hierarchy(tg_cfs_rq(task_group(p), dst_cpu));
6946 }
6947
6948 static inline bool task_is_throttled(struct task_struct *p)
6949 {
6950 return cfs_bandwidth_used() && p->throttled;
6951 }
6952
6953 static bool dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags);
6954 static void throttle_cfs_rq_work(struct callback_head *work)
6955 {
6956 struct task_struct *p = container_of(work, struct task_struct, sched_throttle_work);
6957 struct sched_entity *se;
6958 struct cfs_rq *cfs_rq;
6959 struct rq *rq;
6960
6961 WARN_ON_ONCE(p != current);
6962 p->sched_throttle_work.next = &p->sched_throttle_work;
6963
6964 /*
6965 * If task is exiting, then there won't be a return to userspace, so we
6966 * don't have to bother with any of this.
6967 */
6968 if ((p->flags & PF_EXITING))
6969 return;
6970
6971 scoped_guard(task_rq_lock, p) {
6972 se = &p->se;
6973 cfs_rq = cfs_rq_of(se);
6974
6975 /* Raced, forget */
6976 if (p->sched_class != &fair_sched_class)
6977 return;
6978
6979 /*
6980 * If not in limbo, then either replenish has happened or this
6981 * task got migrated out of the throttled cfs_rq, move along.
6982 */
6983 if (!cfs_rq->throttle_count)
6984 return;
6985 rq = scope.rq;
6986 update_rq_clock(rq);
6987 WARN_ON_ONCE(p->throttled || !list_empty(&p->throttle_node));
6988 dequeue_task_fair(rq, p, DEQUEUE_SLEEP | DEQUEUE_THROTTLE);
6989 list_add(&p->throttle_node, &cfs_rq->throttled_limbo_list);
6990 /*
6991 * Must not set throttled before dequeue or dequeue will
6992 * mistakenly regard this task as an already throttled one.
6993 */
6994 p->throttled = true;
6995 resched_curr(rq);
6996 }
6997 }
6998
6999 void init_cfs_throttle_work(struct task_struct *p)
7000 {
7001 init_task_work(&p->sched_throttle_work, throttle_cfs_rq_work);
7002 /* Protect against double add, see throttle_cfs_rq() and throttle_cfs_rq_work() */
7003 p->sched_throttle_work.next = &p->sched_throttle_work;
7004 INIT_LIST_HEAD(&p->throttle_node);
7005 }
7006
7007 /*
7008 * Task is throttled and someone wants to dequeue it again:
7009 * it could be sched/core when core needs to do things like
7010 * task affinity change, task group change, task sched class
7011 * change etc. and in these cases, DEQUEUE_SLEEP is not set;
7012 * or the task is blocked after throttled due to freezer etc.
7013 * and in these cases, DEQUEUE_SLEEP is set.
7014 */
7015 static void detach_task_cfs_rq(struct task_struct *p);
7016 static void dequeue_throttled_task(struct task_struct *p, int flags)
7017 {
7018 WARN_ON_ONCE(p->se.on_rq);
7019 list_del_init(&p->throttle_node);
7020
7021 /* task blocked after throttled */
7022 if (flags & DEQUEUE_SLEEP) {
7023 p->throttled = false;
7024 return;
7025 }
7026
7027 /*
7028 * task is migrating off its old cfs_rq, detach
7029 * the task's load from its old cfs_rq.
7030 */
7031 if (task_on_rq_migrating(p))
7032 detach_task_cfs_rq(p);
7033 }
7034
7035 static bool enqueue_throttled_task(struct task_struct *p)
7036 {
7037 struct cfs_rq *cfs_rq = cfs_rq_of(&p->se);
7038
7039 /* @p should have gone through dequeue_throttled_task() first */
7040 WARN_ON_ONCE(!list_empty(&p->throttle_node));
7041
7042 /*
7043 * If the throttled task @p is enqueued to a throttled cfs_rq,
7044 * take the fast path by directly putting the task on the
7045 * target cfs_rq's limbo list.
7046 *
7047 * Do not do that when @p is current because the following race can
7048 * cause @p's group_node to be incorectly re-insterted in its rq's
7049 * cfs_tasks list, despite being throttled:
7050 *
7051 * cpuX cpuY
7052 * p ret2user
7053 * throttle_cfs_rq_work() sched_move_task(p)
7054 * LOCK task_rq_lock
7055 * dequeue_task_fair(p)
7056 * UNLOCK task_rq_lock
7057 * LOCK task_rq_lock
7058 * task_current_donor(p) == true
7059 * task_on_rq_queued(p) == true
7060 * dequeue_task(p)
7061 * put_prev_task(p)
7062 * sched_change_group()
7063 * enqueue_task(p) -> p's new cfs_rq
7064 * is throttled, go
7065 * fast path and skip
7066 * actual enqueue
7067 * set_next_task(p)
7068 * list_move(&se->group_node, &rq->cfs_tasks); // bug
7069 * schedule()
7070 *
7071 * In the above race case, @p current cfs_rq is in the same rq as
7072 * its previous cfs_rq because sched_move_task() only moves a task
7073 * to a different group from the same rq, so we can use its current
7074 * cfs_rq to derive rq and test if the task is current.
7075 */
7076 if (throttled_hierarchy(cfs_rq) &&
7077 !task_current_donor(rq_of(cfs_rq), p)) {
7078 list_add(&p->throttle_node, &cfs_rq->throttled_limbo_list);
7079 return true;
7080 }
7081
7082 /* we can't take the fast path, do an actual enqueue*/
7083 p->throttled = false;
7084 return false;
7085 }
7086
7087 static void enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags);
7088 static int tg_unthrottle_up(struct task_group *tg, void *data)
7089 {
7090 struct rq *rq = data;
7091 struct cfs_rq *cfs_rq = tg_cfs_rq(tg, cpu_of(rq));
7092 struct task_struct *p, *tmp;
7093 LIST_HEAD(throttled_tasks);
7094
7095 /*
7096 * If cfs_rq->curr is set, the cfs_rq might not have caught up
7097 * since the last clock update. Do it now before we begin
7098 * queueing task onto it to save the need for unnecessarily
7099 * unthrottle the hierarchy for this cfs_rq to be throttled
7100 * right back again.
7101 */
7102 update_curr(cfs_rq);
7103
7104 if (--cfs_rq->throttle_count)
7105 return 0;
7106
7107 if (cfs_rq->pelt_clock_throttled) {
7108 cfs_rq->throttled_clock_pelt_time += rq_clock_pelt(rq) -
7109 cfs_rq->throttled_clock_pelt;
7110 cfs_rq->pelt_clock_throttled = 0;
7111 }
7112
7113 if (cfs_rq->throttled_clock_self) {
7114 u64 delta = rq_clock(rq) - cfs_rq->throttled_clock_self;
7115
7116 cfs_rq->throttled_clock_self = 0;
7117
7118 if (WARN_ON_ONCE((s64)delta < 0))
7119 delta = 0;
7120
7121 cfs_rq->throttled_clock_self_time += delta;
7122 }
7123
7124 /*
7125 * Move the tasks to a local list since an update_curr() during
7126 * enqueue_task_fair() can throttle a higher cfs_rq, and it can
7127 * see the "throttled_limbo_list" being non-empty in
7128 * tg_throttle_down() if throttle_count turned 0 above.
7129 */
7130 list_splice_init(&cfs_rq->throttled_limbo_list, &throttled_tasks);
7131
7132 /* Re-enqueue the tasks that have been throttled at this level. */
7133 list_for_each_entry_safe(p, tmp, &throttled_tasks, throttle_node) {
7134 /*
7135 * Back to being throttled! Break out and put the remaining
7136 * tasks back onto the limbo_list to prevent running them
7137 * unnecessarily.
7138 */
7139 if (cfs_rq->throttle_count)
7140 break;
7141
7142 list_del_init(&p->throttle_node);
7143 p->throttled = false;
7144 enqueue_task_fair(rq, p, ENQUEUE_WAKEUP);
7145 }
7146
7147 list_splice(&throttled_tasks, &cfs_rq->throttled_limbo_list);
7148
7149 /* Add cfs_rq with load or one or more already running entities to the list */
7150 if (!cfs_rq_is_decayed(cfs_rq))
7151 list_add_leaf_cfs_rq(cfs_rq);
7152
7153 return 0;
7154 }
7155
7156 static inline bool task_has_throttle_work(struct task_struct *p)
7157 {
7158 return p->sched_throttle_work.next != &p->sched_throttle_work;
7159 }
7160
7161 static inline void task_throttle_setup_work(struct task_struct *p)
7162 {
7163 if (task_has_throttle_work(p))
7164 return;
7165
7166 /*
7167 * Kthreads and exiting tasks don't return to userspace, so adding the
7168 * work is pointless
7169 */
7170 if ((p->flags & (PF_EXITING | PF_KTHREAD)))
7171 return;
7172
7173 task_work_add(p, &p->sched_throttle_work, TWA_RESUME);
7174 }
7175
7176 static void record_throttle_clock(struct cfs_rq *cfs_rq)
7177 {
7178 struct rq *rq = rq_of(cfs_rq);
7179
7180 if (cfs_rq_throttled(cfs_rq) && !cfs_rq->throttled_clock)
7181 cfs_rq->throttled_clock = rq_clock(rq);
7182
7183 if (!cfs_rq->throttled_clock_self)
7184 cfs_rq->throttled_clock_self = rq_clock(rq);
7185 }
7186
7187 static int tg_throttle_down(struct task_group *tg, void *data)
7188 {
7189 struct rq *rq = data;
7190 struct cfs_rq *cfs_rq = tg_cfs_rq(tg, cpu_of(rq));
7191
7192 if (cfs_rq->throttle_count++)
7193 return 0;
7194
7195 /*
7196 * For cfs_rqs that still have entities enqueued, PELT clock
7197 * stop happens at dequeue time when all entities are dequeued.
7198 */
7199 if (!cfs_rq->nr_queued) {
7200 list_del_leaf_cfs_rq(cfs_rq);
7201 cfs_rq->throttled_clock_pelt = rq_clock_pelt(rq);
7202 cfs_rq->pelt_clock_throttled = 1;
7203 }
7204
7205 WARN_ON_ONCE(cfs_rq->throttled_clock_self);
7206 WARN_ON_ONCE(!list_empty(&cfs_rq->throttled_limbo_list));
7207 return 0;
7208 }
7209
7210 static bool throttle_cfs_rq(struct cfs_rq *cfs_rq)
7211 {
7212 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
7213 struct sched_entity *curr = cfs_rq->h_curr;
7214 struct rq *rq = rq_of(cfs_rq);
7215
7216 scoped_guard(raw_spinlock, &cfs_b->lock) {
7217 u64 target_runtime = 1;
7218
7219 /*
7220 * If cfs_rq->h_curr is still runnable, we are here from an
7221 * update_curr(). Request sysctl_sched_cfs_bandwidth_slice
7222 * worth of bandwidth to continue running.
7223 *
7224 * If the curr is not runnable, just request enough bandwidth
7225 * to be runnable next time the pick selects this cfs_rq.
7226 */
7227 if (curr && curr->on_rq)
7228 target_runtime = sched_cfs_bandwidth_slice();
7229
7230 /*
7231 * Check if We have raced with bandwidth becoming available. If
7232 * we actually throttled the timer might not unthrottle us for
7233 * an entire period. We additionally needed to make sure that
7234 * any subsequent check_cfs_rq_runtime calls agree not to
7235 * throttle us, as we may commit to do cfs put_prev+pick_next,
7236 * so we ask for 1ns of runtime rather than just check cfs_b.
7237 *
7238 * This will start the period timer if necessary.
7239 */
7240 if (__assign_cfs_rq_runtime(cfs_b, cfs_rq, target_runtime))
7241 return false;
7242
7243 /*
7244 * No bandwidth available; Add ourselves on the list to be
7245 * unthrottled later.
7246 */
7247 list_add_tail_rcu(&cfs_rq->throttled_list,
7248 &cfs_b->throttled_cfs_rq);
7249 }
7250
7251 /* freeze hierarchy runnable averages while throttled */
7252 scoped_guard(rcu)
7253 walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq);
7254
7255 /*
7256 * Note: distribution will already see us throttled via the
7257 * throttled-list. rq->lock protects completion.
7258 */
7259 cfs_rq->throttled = 1;
7260 WARN_ON_ONCE(cfs_rq->throttled_clock);
7261
7262 /*
7263 * If current hierarchy was throttled, add throttle work to the
7264 * current donor. In case of proxy-execution, the execution
7265 * context cannot exit to the userspace while holding a mutex
7266 * and the rule of throttle deferral to only throttle the
7267 * throttled context at exit to userspace is still preserved.
7268 */
7269 if (curr && curr->on_rq)
7270 task_throttle_setup_work(rq->donor);
7271
7272 return true;
7273 }
7274
7275 void unthrottle_cfs_rq(struct cfs_rq *cfs_rq)
7276 {
7277 struct rq *rq = rq_of(cfs_rq);
7278 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
7279 struct sched_entity *se = cfs_rq_se(cfs_rq);
7280
7281 /*
7282 * It's possible we are called with runtime_remaining < 0 due to things
7283 * like async unthrottled us with a positive runtime_remaining but other
7284 * still running entities consumed those runtime before we reached here.
7285 *
7286 * We can't unthrottle this cfs_rq without any runtime remaining because
7287 * any enqueue in tg_unthrottle_up() will immediately trigger a throttle,
7288 * which is not supposed to happen on unthrottle path.
7289 *
7290 * Catch up on the remaining runtime since last clock update before
7291 * checking runtime remaining.
7292 */
7293 update_curr(cfs_rq);
7294 if (cfs_rq->runtime_enabled && cfs_rq->runtime_remaining <= 0)
7295 return;
7296
7297 cfs_rq->throttled = 0;
7298
7299 scoped_guard(raw_spinlock, &cfs_b->lock) {
7300 list_del_rcu(&cfs_rq->throttled_list);
7301
7302 if (!cfs_rq->throttled_clock)
7303 break;
7304
7305 cfs_b->throttled_time += rq_clock(rq) - cfs_rq->throttled_clock;
7306 cfs_rq->throttled_clock = 0;
7307 }
7308
7309 /* update hierarchical throttle state */
7310 walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq);
7311
7312 if (!cfs_rq->load.weight) {
7313 if (!cfs_rq->on_list)
7314 return;
7315 /*
7316 * Nothing to run but something to decay (on_list)?
7317 * Complete the branch.
7318 */
7319 for_each_sched_entity(se) {
7320 if (list_add_leaf_cfs_rq(cfs_rq_of(se)))
7321 break;
7322 }
7323 }
7324
7325 assert_list_leaf_cfs_rq(rq);
7326
7327 /* Determine whether we need to wake up potentially idle CPU: */
7328 if (rq->curr == rq->idle && rq->cfs.h_nr_queued)
7329 resched_curr(rq);
7330 }
7331
7332 static void __cfsb_csd_unthrottle(void *arg)
7333 {
7334 struct cfs_rq *cursor, *tmp;
7335 struct rq *rq = arg;
7336
7337 guard(rq_lock)(rq);
7338
7339 /*
7340 * Iterating over the list can trigger several call to
7341 * update_rq_clock() in unthrottle_cfs_rq().
7342 * Do it once and skip the potential next ones.
7343 */
7344 update_rq_clock(rq);
7345 rq_clock_start_loop_update(rq);
7346
7347 /*
7348 * Since we hold rq lock we're safe from concurrent manipulation of
7349 * the CSD list. However, this RCU critical section annotates the
7350 * fact that we pair with sched_free_group_rcu(), so that we cannot
7351 * race with group being freed in the window between removing it
7352 * from the list and advancing to the next entry in the list.
7353 */
7354 guard(rcu)();
7355
7356 list_for_each_entry_safe(cursor, tmp, &rq->cfsb_csd_list,
7357 throttled_csd_list) {
7358 list_del_init(&cursor->throttled_csd_list);
7359
7360 if (cfs_rq_throttled(cursor))
7361 unthrottle_cfs_rq(cursor);
7362 }
7363
7364 rq_clock_stop_loop_update(rq);
7365 }
7366
7367 static inline void __unthrottle_cfs_rq_async(struct cfs_rq *cfs_rq)
7368 {
7369 struct rq *rq = rq_of(cfs_rq);
7370 bool first;
7371
7372 if (rq == this_rq()) {
7373 update_rq_clock(rq);
7374 unthrottle_cfs_rq(cfs_rq);
7375 return;
7376 }
7377
7378 /* Already enqueued */
7379 if (WARN_ON_ONCE(!list_empty(&cfs_rq->throttled_csd_list)))
7380 return;
7381
7382 first = list_empty(&rq->cfsb_csd_list);
7383 list_add_tail(&cfs_rq->throttled_csd_list, &rq->cfsb_csd_list);
7384 if (first)
7385 smp_call_function_single_async(cpu_of(rq), &rq->cfsb_csd);
7386 }
7387
7388 static void unthrottle_cfs_rq_async(struct cfs_rq *cfs_rq)
7389 {
7390 lockdep_assert_rq_held(rq_of(cfs_rq));
7391
7392 if (WARN_ON_ONCE(!cfs_rq_throttled(cfs_rq) ||
7393 cfs_rq->runtime_remaining <= 0))
7394 return;
7395
7396 __unthrottle_cfs_rq_async(cfs_rq);
7397 }
7398
7399 static bool distribute_cfs_runtime(struct cfs_bandwidth *cfs_b)
7400 {
7401 bool throttled = false, unthrottle_local = false;
7402 int this_cpu = smp_processor_id();
7403 u64 runtime, remaining = 1;
7404 struct cfs_rq *cfs_rq;
7405 struct rq *rq;
7406
7407 guard(rcu)();
7408
7409 list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq,
7410 throttled_list) {
7411 rq = rq_of(cfs_rq);
7412
7413 if (!remaining) {
7414 throttled = true;
7415 break;
7416 }
7417
7418 guard(rq_lock_irqsave)(rq);
7419
7420 if (!cfs_rq_throttled(cfs_rq))
7421 continue;
7422
7423 /* Already queued for async unthrottle */
7424 if (!list_empty(&cfs_rq->throttled_csd_list))
7425 continue;
7426
7427 if (cfs_rq->h_curr) {
7428 update_rq_clock(rq);
7429 update_curr(cfs_rq);
7430 }
7431
7432 /* By the above checks, this should never be true */
7433 WARN_ON_ONCE(cfs_rq->runtime_remaining > 0);
7434
7435 scoped_guard(raw_spinlock, &cfs_b->lock) {
7436 runtime = -cfs_rq->runtime_remaining + 1;
7437 if (runtime > cfs_b->runtime)
7438 runtime = cfs_b->runtime;
7439 cfs_b->runtime -= runtime;
7440 remaining = cfs_b->runtime;
7441 }
7442
7443 cfs_rq->runtime_remaining += runtime;
7444
7445 /*
7446 * Ran out of bandwidth during distribution!
7447 * Indicate throttled entities and break early.
7448 */
7449 if (cfs_rq->runtime_remaining <= 0) {
7450 throttled = true;
7451 break;
7452 }
7453
7454 /* we check whether we're throttled above */
7455 if (cpu_of(rq) != this_cpu) {
7456 unthrottle_cfs_rq_async(cfs_rq);
7457 continue;
7458 }
7459
7460 /*
7461 * Allow a parallel async unthrottle to unthrottle
7462 * this cfs_rq too via __cfsb_csd_unthrottle().
7463 * If we are first, do it ourselves at the end and
7464 * save on an IPI from remote CPUs.
7465 */
7466 unthrottle_local = list_empty(&rq->cfsb_csd_list);
7467 list_add_tail(&cfs_rq->throttled_csd_list, &rq->cfsb_csd_list);
7468 }
7469
7470 if (unthrottle_local) {
7471 /*
7472 * Protect against an IPI that is also trying to flush
7473 * the unthrottled cfs_rq(s) from this CPU's csd_list.
7474 */
7475 scoped_guard(irqsave)
7476 __cfsb_csd_unthrottle(cpu_rq(this_cpu));
7477 }
7478
7479 return throttled;
7480 }
7481
7482 /*
7483 * Responsible for refilling a task_group's bandwidth and unthrottling its
7484 * cfs_rqs as appropriate. If there has been no activity within the last
7485 * period the timer is deactivated until scheduling resumes; cfs_b->idle is
7486 * used to track this state.
7487 */
7488 static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun, unsigned long flags)
7489 __must_hold(&cfs_b->lock)
7490 {
7491 int throttled;
7492
7493 /* no need to continue the timer with no bandwidth constraint */
7494 if (cfs_b->quota == RUNTIME_INF)
7495 goto out_deactivate;
7496
7497 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
7498 cfs_b->nr_periods += overrun;
7499
7500 /* Refill extra burst quota even if cfs_b->idle */
7501 __refill_cfs_bandwidth_runtime(cfs_b);
7502
7503 /*
7504 * idle depends on !throttled (for the case of a large deficit), and if
7505 * we're going inactive then everything else can be deferred
7506 */
7507 if (cfs_b->idle && !throttled)
7508 goto out_deactivate;
7509
7510 if (!throttled) {
7511 /* mark as potentially idle for the upcoming period */
7512 cfs_b->idle = 1;
7513 return 0;
7514 }
7515
7516 /* account preceding periods in which throttling occurred */
7517 cfs_b->nr_throttled += overrun;
7518
7519 /*
7520 * This check is repeated as we release cfs_b->lock while we unthrottle.
7521 */
7522 while (throttled && cfs_b->runtime > 0) {
7523 raw_spin_unlock_irqrestore(&cfs_b->lock, flags);
7524 /* we can't nest cfs_b->lock while distributing bandwidth */
7525 throttled = distribute_cfs_runtime(cfs_b);
7526 raw_spin_lock_irqsave(&cfs_b->lock, flags);
7527 }
7528
7529 /*
7530 * While we are ensured activity in the period following an
7531 * unthrottle, this also covers the case in which the new bandwidth is
7532 * insufficient to cover the existing bandwidth deficit. (Forcing the
7533 * timer to remain active while there are any throttled entities.)
7534 */
7535 cfs_b->idle = 0;
7536
7537 return 0;
7538
7539 out_deactivate:
7540 return 1;
7541 }
7542
7543 /* a cfs_rq won't donate quota below this amount */
7544 static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC;
7545 /* minimum remaining period time to redistribute slack quota */
7546 static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC;
7547 /* how long we wait to gather additional slack before distributing */
7548 static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC;
7549
7550 /*
7551 * Are we near the end of the current quota period?
7552 *
7553 * Requires cfs_b->lock for hrtimer_expires_remaining to be safe against the
7554 * hrtimer base being cleared by hrtimer_start. In the case of
7555 * migrate_hrtimers, base is never cleared, so we are fine.
7556 */
7557 static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire)
7558 {
7559 struct hrtimer *refresh_timer = &cfs_b->period_timer;
7560 s64 remaining;
7561
7562 /* if the call-back is running a quota refresh is already occurring */
7563 if (hrtimer_callback_running(refresh_timer))
7564 return 1;
7565
7566 /* is a quota refresh about to occur? */
7567 remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer));
7568 if (remaining < (s64)min_expire)
7569 return 1;
7570
7571 return 0;
7572 }
7573
7574 static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b)
7575 {
7576 u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration;
7577
7578 /* if there's a quota refresh soon don't bother with slack */
7579 if (runtime_refresh_within(cfs_b, min_left))
7580 return;
7581
7582 /* don't push forwards an existing deferred unthrottle */
7583 if (cfs_b->slack_started)
7584 return;
7585 cfs_b->slack_started = true;
7586
7587 hrtimer_start(&cfs_b->slack_timer,
7588 ns_to_ktime(cfs_bandwidth_slack_period),
7589 HRTIMER_MODE_REL);
7590 }
7591
7592 /* we know any runtime found here is valid as update_curr() precedes return */
7593 static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
7594 {
7595 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
7596 s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime;
7597
7598 if (slack_runtime <= 0)
7599 return;
7600
7601 guard(raw_spinlock)(&cfs_b->lock);
7602
7603 if (cfs_b->quota != RUNTIME_INF) {
7604 cfs_b->runtime += slack_runtime;
7605
7606 /* we are under rq->lock, defer unthrottling using a timer */
7607 if (cfs_b->runtime > sched_cfs_bandwidth_slice() &&
7608 !list_empty(&cfs_b->throttled_cfs_rq))
7609 start_cfs_slack_bandwidth(cfs_b);
7610 }
7611
7612 /* even if it's not valid for return we don't want to try again */
7613 cfs_rq->runtime_remaining -= slack_runtime;
7614 }
7615
7616 static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
7617 {
7618 if (!cfs_bandwidth_used())
7619 return;
7620
7621 if (!cfs_rq->runtime_enabled || cfs_rq->nr_queued)
7622 return;
7623
7624 __return_cfs_rq_runtime(cfs_rq);
7625 }
7626
7627 /*
7628 * This is done with a timer (instead of inline with bandwidth return) since
7629 * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
7630 */
7631 static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b)
7632 {
7633 /* confirm we're still not at a refresh boundary */
7634 scoped_guard(raw_spinlock_irqsave, &cfs_b->lock) {
7635 u64 runtime = 0, slice = sched_cfs_bandwidth_slice();
7636
7637 cfs_b->slack_started = false;
7638
7639 if (runtime_refresh_within(cfs_b, min_bandwidth_expiration))
7640 return;
7641
7642 if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice)
7643 runtime = cfs_b->runtime;
7644
7645 if (!runtime)
7646 return;
7647 }
7648
7649 distribute_cfs_runtime(cfs_b);
7650 }
7651
7652 /*
7653 * When a group wakes up we want to make sure that its quota is not already
7654 * expired/exceeded, otherwise it may be allowed to steal additional ticks of
7655 * runtime as update_curr() throttling can not trigger until it's on-rq.
7656 */
7657 static void check_enqueue_throttle(struct cfs_rq *cfs_rq)
7658 {
7659 if (!cfs_bandwidth_used())
7660 return;
7661
7662 /* an active group must be handled by the update_curr() path */
7663 if (!cfs_rq->runtime_enabled || cfs_rq->h_curr)
7664 return;
7665
7666 /* ensure the group is not already throttled */
7667 if (cfs_rq_throttled(cfs_rq))
7668 return;
7669
7670 /* update runtime allocation */
7671 account_cfs_rq_runtime(cfs_rq, 0);
7672 }
7673
7674 static void sync_throttle(struct task_group *tg, int cpu)
7675 {
7676 struct cfs_rq *pcfs_rq, *cfs_rq;
7677
7678 if (!cfs_bandwidth_used())
7679 return;
7680
7681 if (!tg->parent)
7682 return;
7683
7684 cfs_rq = tg_cfs_rq(tg, cpu);
7685 pcfs_rq = tg_cfs_rq(tg->parent, cpu);
7686
7687 cfs_rq->throttle_count = pcfs_rq->throttle_count;
7688 cfs_rq->throttled_clock_pelt = rq_clock_pelt(cpu_rq(cpu));
7689
7690 /*
7691 * It is not enough to sync the "pelt_clock_throttled" indicator
7692 * with the parent cfs_rq when the hierarchy is not queued.
7693 * Always join a throttled hierarchy with PELT clock throttled
7694 * and leaf it to the first enqueue, or distribution to
7695 * unthrottle the PELT clock.
7696 */
7697 if (cfs_rq->throttle_count)
7698 cfs_rq->pelt_clock_throttled = 1;
7699 }
7700
7701 static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer)
7702 {
7703 struct cfs_bandwidth *cfs_b =
7704 container_of(timer, struct cfs_bandwidth, slack_timer);
7705
7706 do_sched_cfs_slack_timer(cfs_b);
7707
7708 return HRTIMER_NORESTART;
7709 }
7710
7711 static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer)
7712 {
7713 struct cfs_bandwidth *cfs_b =
7714 container_of(timer, struct cfs_bandwidth, period_timer);
7715 int overrun;
7716 int idle = 0;
7717 int count = 0;
7718
7719 CLASS(raw_spinlock_irqsave, cfsb_guard)(&cfs_b->lock);
7720
7721 for (;;) {
7722 overrun = hrtimer_forward_now(timer, cfs_b->period);
7723 if (!overrun)
7724 break;
7725
7726 idle = do_sched_cfs_period_timer(cfs_b, overrun, cfsb_guard.flags);
7727
7728 if (++count > 3) {
7729 u64 new, old = ktime_to_ns(cfs_b->period);
7730
7731 /*
7732 * Grow period by a factor of 2 to avoid losing precision.
7733 * Precision loss in the quota/period ratio can cause __cfs_schedulable
7734 * to fail.
7735 */
7736 new = old * 2;
7737 if (new < max_bw_quota_period_us * NSEC_PER_USEC) {
7738 cfs_b->period = ns_to_ktime(new);
7739 cfs_b->quota *= 2;
7740 cfs_b->burst *= 2;
7741
7742 pr_warn_ratelimited(
7743 "cfs_period_timer[cpu%d]: period too short, scaling up (new cfs_period_us = %lld, cfs_quota_us = %lld)\n",
7744 smp_processor_id(),
7745 div_u64(new, NSEC_PER_USEC),
7746 div_u64(cfs_b->quota, NSEC_PER_USEC));
7747 } else {
7748 pr_warn_ratelimited(
7749 "cfs_period_timer[cpu%d]: period too short, but cannot scale up without losing precision (cfs_period_us = %lld, cfs_quota_us = %lld)\n",
7750 smp_processor_id(),
7751 div_u64(old, NSEC_PER_USEC),
7752 div_u64(cfs_b->quota, NSEC_PER_USEC));
7753 }
7754
7755 /* reset count so we don't come right back in here */
7756 count = 0;
7757 }
7758 }
7759
7760 if (idle) {
7761 cfs_b->period_active = 0;
7762 return HRTIMER_NORESTART;
7763 }
7764
7765 return HRTIMER_RESTART;
7766 }
7767
7768 void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b, struct cfs_bandwidth *parent)
7769 {
7770 raw_spin_lock_init(&cfs_b->lock);
7771 cfs_b->runtime = 0;
7772 cfs_b->quota = RUNTIME_INF;
7773 cfs_b->period = us_to_ktime(default_bw_period_us());
7774 cfs_b->burst = 0;
7775 cfs_b->hierarchical_quota = parent ? parent->hierarchical_quota : RUNTIME_INF;
7776
7777 INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq);
7778 hrtimer_setup(&cfs_b->period_timer, sched_cfs_period_timer, CLOCK_MONOTONIC,
7779 HRTIMER_MODE_ABS_PINNED);
7780
7781 /* Add a random offset so that timers interleave */
7782 hrtimer_set_expires(&cfs_b->period_timer,
7783 get_random_u32_below(cfs_b->period));
7784 hrtimer_setup(&cfs_b->slack_timer, sched_cfs_slack_timer, CLOCK_MONOTONIC,
7785 HRTIMER_MODE_REL);
7786 cfs_b->slack_started = false;
7787 }
7788
7789 static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq)
7790 {
7791 cfs_rq->runtime_enabled = 0;
7792 INIT_LIST_HEAD(&cfs_rq->throttled_list);
7793 INIT_LIST_HEAD(&cfs_rq->throttled_csd_list);
7794 INIT_LIST_HEAD(&cfs_rq->throttled_limbo_list);
7795 }
7796
7797 void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
7798 {
7799 lockdep_assert_held(&cfs_b->lock);
7800
7801 if (cfs_b->period_active)
7802 return;
7803
7804 cfs_b->period_active = 1;
7805 hrtimer_forward_now(&cfs_b->period_timer, cfs_b->period);
7806 hrtimer_start_expires(&cfs_b->period_timer, HRTIMER_MODE_ABS_PINNED);
7807 }
7808
7809 static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
7810 {
7811 int __maybe_unused i;
7812
7813 /* init_cfs_bandwidth() was not called */
7814 if (!cfs_b->throttled_cfs_rq.next)
7815 return;
7816
7817 hrtimer_cancel(&cfs_b->period_timer);
7818 hrtimer_cancel(&cfs_b->slack_timer);
7819
7820 /*
7821 * It is possible that we still have some cfs_rq's pending on a CSD
7822 * list, though this race is very rare. In order for this to occur, we
7823 * must have raced with the last task leaving the group while there
7824 * exist throttled cfs_rq(s), and the period_timer must have queued the
7825 * CSD item but the remote cpu has not yet processed it. To handle this,
7826 * we can simply flush all pending CSD work inline here. We're
7827 * guaranteed at this point that no additional cfs_rq of this group can
7828 * join a CSD list.
7829 */
7830 for_each_possible_cpu(i) {
7831 struct rq *rq = cpu_rq(i);
7832
7833 if (list_empty(&rq->cfsb_csd_list))
7834 continue;
7835
7836 scoped_guard(irqsave)
7837 __cfsb_csd_unthrottle(rq);
7838 }
7839 }
7840
7841 /*
7842 * Both these CPU hotplug callbacks race against unregister_fair_sched_group()
7843 *
7844 * The race is harmless, since modifying bandwidth settings of unhooked group
7845 * bits doesn't do much.
7846 */
7847
7848 /* cpu online callback */
7849 static void __maybe_unused update_runtime_enabled(struct rq *rq)
7850 {
7851 struct task_group *tg;
7852
7853 lockdep_assert_rq_held(rq);
7854
7855 guard(rcu)();
7856
7857 list_for_each_entry_rcu(tg, &task_groups, list) {
7858 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
7859 struct cfs_rq *cfs_rq = tg_cfs_rq(tg, cpu_of(rq));
7860
7861 scoped_guard(raw_spinlock, &cfs_b->lock)
7862 cfs_rq->runtime_enabled = cfs_b->quota != RUNTIME_INF;
7863 }
7864 }
7865
7866 /* cpu offline callback */
7867 static void __maybe_unused unthrottle_offline_cfs_rqs(struct rq *rq)
7868 {
7869 struct task_group *tg;
7870
7871 lockdep_assert_rq_held(rq);
7872
7873 // Do not unthrottle for an active CPU
7874 if (cpumask_test_cpu(cpu_of(rq), cpu_active_mask))
7875 return;
7876
7877 /*
7878 * The rq clock has already been updated in the
7879 * set_rq_offline(), so we should skip updating
7880 * the rq clock again in unthrottle_cfs_rq().
7881 */
7882 rq_clock_start_loop_update(rq);
7883
7884 guard(rcu)();
7885
7886 list_for_each_entry_rcu(tg, &task_groups, list) {
7887 struct cfs_rq *cfs_rq = tg_cfs_rq(tg, cpu_of(rq));
7888
7889 if (!cfs_rq->runtime_enabled)
7890 continue;
7891
7892 /*
7893 * Offline rq is schedulable till CPU is completely disabled
7894 * in take_cpu_down(), so we prevent new cfs throttling here.
7895 */
7896 cfs_rq->runtime_enabled = 0;
7897
7898 if (!cfs_rq_throttled(cfs_rq))
7899 continue;
7900
7901 /*
7902 * clock_task is not advancing so we just need to make sure
7903 * there's some valid quota amount
7904 */
7905 cfs_rq->runtime_remaining = 1;
7906 unthrottle_cfs_rq(cfs_rq);
7907 }
7908
7909 rq_clock_stop_loop_update(rq);
7910 }
7911
7912 bool cfs_task_bw_constrained(struct task_struct *p)
7913 {
7914 struct cfs_rq *cfs_rq = task_cfs_rq(p);
7915
7916 if (!cfs_bandwidth_used())
7917 return false;
7918
7919 if (cfs_rq->runtime_enabled ||
7920 tg_cfs_bandwidth(cfs_rq->tg)->hierarchical_quota != RUNTIME_INF)
7921 return true;
7922
7923 return false;
7924 }
7925
7926 #ifdef CONFIG_NO_HZ_FULL
7927 /* called from pick_next_task_fair() */
7928 static void sched_fair_update_stop_tick(struct rq *rq, struct task_struct *p)
7929 {
7930 int cpu = cpu_of(rq);
7931
7932 if (!cfs_bandwidth_used())
7933 return;
7934
7935 if (!tick_nohz_full_cpu(cpu))
7936 return;
7937
7938 if (rq->nr_running != 1)
7939 return;
7940
7941 /*
7942 * We know there is only one task runnable and we've just picked it. The
7943 * normal enqueue path will have cleared TICK_DEP_BIT_SCHED if we will
7944 * be otherwise able to stop the tick. Just need to check if we are using
7945 * bandwidth control.
7946 */
7947 if (cfs_task_bw_constrained(p))
7948 tick_nohz_dep_set_cpu(cpu, TICK_DEP_BIT_SCHED);
7949 }
7950 #endif /* CONFIG_NO_HZ_FULL */
7951
7952 #else /* !CONFIG_CFS_BANDWIDTH: */
7953
7954 static bool account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec) { return false; }
7955 static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {}
7956 static inline void sync_throttle(struct task_group *tg, int cpu) {}
7957 static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
7958 static void task_throttle_setup_work(struct task_struct *p) {}
7959 static bool task_is_throttled(struct task_struct *p) { return false; }
7960 static void dequeue_throttled_task(struct task_struct *p, int flags) {}
7961 static bool enqueue_throttled_task(struct task_struct *p) { return false; }
7962 static void record_throttle_clock(struct cfs_rq *cfs_rq) {}
7963
7964 static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
7965 {
7966 return 0;
7967 }
7968
7969 static inline bool cfs_rq_pelt_clock_throttled(struct cfs_rq *cfs_rq)
7970 {
7971 return false;
7972 }
7973
7974 static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
7975 {
7976 return 0;
7977 }
7978
7979 static inline int lb_throttled_hierarchy(struct task_struct *p, int dst_cpu)
7980 {
7981 return 0;
7982 }
7983
7984 #ifdef CONFIG_FAIR_GROUP_SCHED
7985 void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b, struct cfs_bandwidth *parent) {}
7986 static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
7987 #endif
7988
7989 static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
7990 {
7991 return NULL;
7992 }
7993 static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
7994 static inline void update_runtime_enabled(struct rq *rq) {}
7995 static inline void unthrottle_offline_cfs_rqs(struct rq *rq) {}
7996 #ifdef CONFIG_CGROUP_SCHED
7997 bool cfs_task_bw_constrained(struct task_struct *p)
7998 {
7999 return false;
8000 }
8001 #endif
8002 #endif /* !CONFIG_CFS_BANDWIDTH */
8003
8004 #if !defined(CONFIG_CFS_BANDWIDTH) || !defined(CONFIG_NO_HZ_FULL)
8005 static inline void sched_fair_update_stop_tick(struct rq *rq, struct task_struct *p) {}
8006 #endif
8007
8008 /**************************************************
8009 * CFS operations on tasks:
8010 */
8011
8012 #ifdef CONFIG_SCHED_HRTICK
8013 static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
8014 {
8015 struct sched_entity *se = &p->se;
8016 unsigned long scale = 1024;
8017 unsigned long util = 0;
8018 u64 vdelta;
8019 u64 delta;
8020
8021 WARN_ON_ONCE(task_rq(p) != rq);
8022
8023 if (rq->cfs.h_nr_queued <= 1)
8024 return;
8025
8026 /*
8027 * Compute time until virtual deadline
8028 */
8029 vdelta = se->deadline - se->vruntime;
8030 if ((s64)vdelta < 0) {
8031 if (task_current_donor(rq, p))
8032 resched_curr(rq);
8033 return;
8034 }
8035 delta = (se->h_load.weight * vdelta) / NICE_0_LOAD;
8036
8037 /*
8038 * Correct for instantaneous load of other classes.
8039 */
8040 util += cpu_util_irq(rq);
8041 if (util && util < 1024) {
8042 scale *= 1024;
8043 scale /= (1024 - util);
8044 }
8045
8046 hrtick_start(rq, (scale * delta) / 1024);
8047 }
8048
8049 /*
8050 * Called on enqueue to start the hrtick when h_nr_queued becomes more than 1.
8051 */
8052 static void hrtick_update(struct rq *rq)
8053 {
8054 struct task_struct *donor = rq->donor;
8055
8056 if (!hrtick_enabled_fair(rq) || donor->sched_class != &fair_sched_class)
8057 return;
8058
8059 if (hrtick_active(rq))
8060 return;
8061
8062 hrtick_start_fair(rq, donor);
8063 }
8064 #else /* !CONFIG_SCHED_HRTICK: */
8065 static inline void
8066 hrtick_start_fair(struct rq *rq, struct task_struct *p)
8067 {
8068 }
8069
8070 static inline void hrtick_update(struct rq *rq)
8071 {
8072 }
8073 #endif /* !CONFIG_SCHED_HRTICK */
8074
8075 static inline bool cpu_overutilized(int cpu)
8076 {
8077 unsigned long rq_util_max;
8078
8079 if (!sched_energy_enabled())
8080 return false;
8081
8082 rq_util_max = uclamp_rq_get(cpu_rq(cpu), UCLAMP_MAX);
8083
8084 /* Return true only if the utilization doesn't fit CPU's capacity */
8085 return !util_fits_cpu(cpu_util_cfs(cpu), 0, rq_util_max, cpu);
8086 }
8087
8088 /*
8089 * overutilized value make sense only if EAS is enabled
8090 */
8091 static inline bool is_rd_overutilized(struct root_domain *rd)
8092 {
8093 return !sched_energy_enabled() || READ_ONCE(rd->overutilized);
8094 }
8095
8096 static inline void set_rd_overutilized(struct root_domain *rd, bool flag)
8097 {
8098 if (!sched_energy_enabled())
8099 return;
8100
8101 WRITE_ONCE(rd->overutilized, flag);
8102 trace_sched_overutilized_tp(rd, flag);
8103 }
8104
8105 static inline void check_update_overutilized_status(struct rq *rq)
8106 {
8107 /*
8108 * overutilized field is used for load balancing decisions only
8109 * if energy aware scheduler is being used
8110 */
8111
8112 if (!is_rd_overutilized(rq->rd) && cpu_overutilized(rq->cpu))
8113 set_rd_overutilized(rq->rd, 1);
8114 }
8115
8116 /* Runqueue only has SCHED_IDLE tasks enqueued */
8117 static int sched_idle_rq(struct rq *rq)
8118 {
8119 return unlikely(rq->nr_running == rq->cfs.h_nr_idle &&
8120 rq->nr_running);
8121 }
8122
8123 static int choose_sched_idle_rq(struct rq *rq, struct task_struct *p)
8124 {
8125 return sched_idle_rq(rq) && !task_has_idle_policy(p);
8126 }
8127
8128 static int choose_idle_cpu(int cpu, struct task_struct *p)
8129 {
8130 return available_idle_cpu(cpu) ||
8131 choose_sched_idle_rq(cpu_rq(cpu), p);
8132 }
8133
8134 static void
8135 requeue_delayed_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
8136 {
8137 /*
8138 * se->sched_delayed should imply: se->on_rq == 1.
8139 * Because a delayed entity is one that is still on
8140 * the runqueue competing until elegibility.
8141 */
8142 WARN_ON_ONCE(!se->sched_delayed);
8143 WARN_ON_ONCE(!se->on_rq);
8144
8145 if (update_entity_lag(cfs_rq, se)) {
8146 cfs_rq->h_nr_queued--;
8147 if (se != cfs_rq->curr)
8148 __dequeue_entity(cfs_rq, se);
8149 place_entity(cfs_rq, se, 0);
8150 if (se != cfs_rq->curr)
8151 __enqueue_entity(cfs_rq, se);
8152 cfs_rq->h_nr_queued++;
8153 }
8154
8155 update_load_avg(cfs_rq, se, 0);
8156 clear_delayed(se);
8157 }
8158
8159 static unsigned long enqueue_hierarchy(struct task_struct *p, int flags)
8160 {
8161 unsigned long weight = NICE_0_LOAD;
8162 int task_new = !(flags & ENQUEUE_WAKEUP);
8163 struct sched_entity *se = &p->se;
8164 int h_nr_idle = task_has_idle_policy(p);
8165 int h_nr_runnable = 1;
8166
8167 if (task_new && se->sched_delayed)
8168 h_nr_runnable = 0;
8169
8170 for_each_sched_entity(se) {
8171 struct cfs_rq *cfs_rq = cfs_rq_of(se);
8172
8173 update_curr(cfs_rq);
8174
8175 if (!se->on_rq) {
8176 enqueue_entity(cfs_rq, se, flags);
8177 } else {
8178 update_load_avg(cfs_rq, se, UPDATE_TG);
8179 se_update_runnable(se);
8180 update_cfs_group(se);
8181 }
8182
8183 cfs_rq->h_nr_runnable += h_nr_runnable;
8184 cfs_rq->h_nr_queued++;
8185 cfs_rq->h_nr_idle += h_nr_idle;
8186
8187 if (cfs_rq_is_idle(cfs_rq))
8188 h_nr_idle = 1;
8189
8190 weight = __calc_prop_weight(cfs_rq, se, weight);
8191
8192 flags = ENQUEUE_WAKEUP;
8193 }
8194
8195 return weight;
8196 }
8197
8198 /* Update curr's vruntime before placing entity or updating lag */
8199 static inline void update_curr_eevdf(struct cfs_rq *cfs_rq)
8200 {
8201 if (!cfs_rq->curr)
8202 return;
8203
8204 update_curr(cfs_rq_of(cfs_rq->curr));
8205 }
8206
8207 /*
8208 * The enqueue_task method is called before nr_running is
8209 * increased. Here we update the fair scheduling stats and
8210 * then put the task into the rbtree:
8211 */
8212 static void
8213 enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
8214 {
8215 int rq_h_nr_queued = rq->cfs.h_nr_queued;
8216 int task_new = !(flags & ENQUEUE_WAKEUP);
8217 struct sched_entity *se = &p->se;
8218 struct cfs_rq *cfs_rq = &rq->cfs;
8219 unsigned long weight;
8220 bool curr;
8221
8222 if (task_is_throttled(p) && enqueue_throttled_task(p))
8223 return;
8224
8225 /*
8226 * The code below (indirectly) updates schedutil which looks at
8227 * the cfs_rq utilization to select a frequency.
8228 * Let's add the task's estimated utilization to the cfs_rq's
8229 * estimated utilization, before we update schedutil.
8230 */
8231 if (!p->se.sched_delayed || (flags & ENQUEUE_DELAYED))
8232 util_est_enqueue(cfs_rq, p);
8233
8234 update_curr_eevdf(cfs_rq);
8235
8236 if (flags & ENQUEUE_DELAYED) {
8237 requeue_delayed_entity(cfs_rq, se);
8238 return;
8239 }
8240
8241 /*
8242 * If in_iowait is set, the code below may not trigger any cpufreq
8243 * utilization updates, so do it here explicitly with the IOWAIT flag
8244 * passed.
8245 */
8246 if (p->in_iowait)
8247 cpufreq_update_util(rq, SCHED_CPUFREQ_IOWAIT);
8248
8249 /*
8250 * XXX comment on the curr thing
8251 */
8252 curr = (cfs_rq->curr == se);
8253 if (curr)
8254 place_entity(cfs_rq, se, flags);
8255
8256 if (se->on_rq && se->sched_delayed)
8257 requeue_delayed_entity(cfs_rq, se);
8258
8259 weight = enqueue_hierarchy(p, flags);
8260
8261 if (!curr) {
8262 reweight_eevdf(cfs_rq, se, weight, false);
8263 place_entity(cfs_rq, se, flags | ENQUEUE_QUEUED);
8264 __enqueue_entity(cfs_rq, se);
8265 }
8266
8267 if (!rq_h_nr_queued && rq->cfs.h_nr_queued)
8268 dl_server_start(&rq->fair_server);
8269
8270 /* At this point se is NULL and we are at root level*/
8271 add_nr_running(rq, 1);
8272
8273 /*
8274 * Since new tasks are assigned an initial util_avg equal to
8275 * half of the spare capacity of their CPU, tiny tasks have the
8276 * ability to cross the overutilized threshold, which will
8277 * result in the load balancer ruining all the task placement
8278 * done by EAS. As a way to mitigate that effect, do not account
8279 * for the first enqueue operation of new tasks during the
8280 * overutilized flag detection.
8281 *
8282 * A better way of solving this problem would be to wait for
8283 * the PELT signals of tasks to converge before taking them
8284 * into account, but that is not straightforward to implement,
8285 * and the following generally works well enough in practice.
8286 */
8287 if (!task_new)
8288 check_update_overutilized_status(rq);
8289
8290 assert_list_leaf_cfs_rq(rq);
8291
8292 hrtick_update(rq);
8293 }
8294
8295 static void dequeue_hierarchy(struct task_struct *p, int flags)
8296 {
8297 struct sched_entity *se = &p->se;
8298 bool task_sleep = flags & DEQUEUE_SLEEP;
8299 bool task_delayed = flags & DEQUEUE_DELAYED;
8300 bool task_throttled = flags & DEQUEUE_THROTTLE;
8301 int h_nr_runnable = 0;
8302 int h_nr_idle = task_has_idle_policy(p);
8303 bool dequeue = true;
8304
8305 if (task_sleep || task_delayed || !se->sched_delayed)
8306 h_nr_runnable = 1;
8307
8308 for_each_sched_entity(se) {
8309 struct cfs_rq *cfs_rq = cfs_rq_of(se);
8310
8311 update_curr(cfs_rq);
8312
8313 if (dequeue) {
8314 dequeue_entity(cfs_rq, se, flags);
8315 /* Don't dequeue parent if it has other entities besides us */
8316 if (cfs_rq->load.weight)
8317 dequeue = false;
8318 } else {
8319 update_load_avg(cfs_rq, se, UPDATE_TG);
8320 se_update_runnable(se);
8321 update_cfs_group(se);
8322 }
8323
8324 cfs_rq->h_nr_runnable -= h_nr_runnable;
8325 cfs_rq->h_nr_queued--;
8326 cfs_rq->h_nr_idle -= h_nr_idle;
8327
8328 if (cfs_rq_is_idle(cfs_rq))
8329 h_nr_idle = 1;
8330
8331 if (throttled_hierarchy(cfs_rq) && task_throttled)
8332 record_throttle_clock(cfs_rq);
8333
8334 flags |= DEQUEUE_SLEEP;
8335 flags &= ~(DEQUEUE_DELAYED | DEQUEUE_SPECIAL);
8336 }
8337 }
8338
8339 /*
8340 * The part of dequeue_task_fair() that is needed to dequeue delayed tasks.
8341 *
8342 * Returns:
8343 * true - dequeued
8344 * false - delayed
8345 */
8346 static bool __dequeue_task(struct rq *rq, struct task_struct *p, int flags)
8347 {
8348 struct sched_entity *se = &p->se;
8349 struct cfs_rq *cfs_rq = &rq->cfs;
8350 bool was_sched_idle = sched_idle_rq(rq);
8351 bool task_sleep = flags & DEQUEUE_SLEEP;
8352 bool task_delayed = flags & DEQUEUE_DELAYED;
8353
8354 clear_buddies(cfs_rq, se);
8355
8356 update_curr_eevdf(cfs_rq);
8357 update_entity_lag(cfs_rq, se);
8358
8359 if (flags & DEQUEUE_DELAYED) {
8360 WARN_ON_ONCE(!se->sched_delayed);
8361 } else {
8362 bool delay = task_sleep;
8363 /*
8364 * DELAY_DEQUEUE relies on spurious wakeups, special task
8365 * states must not suffer spurious wakeups, excempt them.
8366 */
8367 if (flags & (DEQUEUE_SPECIAL | DEQUEUE_THROTTLE))
8368 delay = false;
8369
8370 WARN_ON_ONCE(delay && se->sched_delayed);
8371
8372 if (sched_feat(DELAY_DEQUEUE) && delay &&
8373 !entity_eligible(cfs_rq, se)) {
8374 update_load_avg(cfs_rq_of(se), se, UPDATE_UTIL_EST);
8375 set_delayed(se);
8376 return false;
8377 }
8378 }
8379
8380 dequeue_hierarchy(p, flags);
8381
8382 if (sched_feat(PLACE_REL_DEADLINE) && !task_sleep) {
8383 se->deadline -= se->vruntime;
8384 se->rel_deadline = 1;
8385 }
8386 if (se != cfs_rq->curr)
8387 __dequeue_entity(cfs_rq, se);
8388
8389 sub_nr_running(rq, 1);
8390
8391 /* balance early to pull high priority tasks */
8392 if (unlikely(!was_sched_idle && sched_idle_rq(rq)))
8393 rq->next_balance = jiffies;
8394
8395 if (task_delayed) {
8396 clear_delayed(se);
8397
8398 WARN_ON_ONCE(!task_sleep);
8399 WARN_ON_ONCE(p->on_rq != 1);
8400
8401 /*
8402 * Fix-up what block_task() skipped.
8403 *
8404 * Must be last, @p might not be valid after this.
8405 */
8406 __block_task(rq, p);
8407 }
8408
8409 return true;
8410 }
8411
8412 /*
8413 * The dequeue_task method is called before nr_running is
8414 * decreased. We remove the task from the rbtree and
8415 * update the fair scheduling stats:
8416 */
8417 static bool dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)
8418 {
8419 if (task_is_throttled(p)) {
8420 dequeue_throttled_task(p, flags);
8421 return true;
8422 }
8423
8424 if (!p->se.sched_delayed)
8425 util_est_dequeue(&rq->cfs, p);
8426
8427 if (!__dequeue_task(rq, p, flags))
8428 return false;
8429
8430 /*
8431 * Must not reference @p after __dequeue_task(DEQUEUE_DELAYED).
8432 */
8433 return true;
8434 }
8435
8436 static inline unsigned int cfs_h_nr_delayed(struct rq *rq)
8437 {
8438 return (rq->cfs.h_nr_queued - rq->cfs.h_nr_runnable);
8439 }
8440
8441 /* Working cpumask for: sched_balance_rq(), sched_balance_newidle(). */
8442 static DEFINE_PER_CPU(cpumask_var_t, load_balance_mask);
8443 static DEFINE_PER_CPU(cpumask_var_t, select_rq_mask);
8444 static DEFINE_PER_CPU(cpumask_var_t, should_we_balance_tmpmask);
8445
8446 #ifdef CONFIG_NO_HZ_COMMON
8447
8448 static struct {
8449 cpumask_var_t idle_cpus_mask;
8450 int has_blocked_load; /* Idle CPUS has blocked load */
8451 int needs_update; /* Newly idle CPUs need their next_balance collated */
8452 unsigned long next_balance; /* in jiffy units */
8453 unsigned long next_blocked; /* Next update of blocked load in jiffies */
8454 } nohz ____cacheline_aligned;
8455
8456 #endif /* CONFIG_NO_HZ_COMMON */
8457
8458 static unsigned long cpu_load(struct rq *rq)
8459 {
8460 return cfs_rq_load_avg(&rq->cfs);
8461 }
8462
8463 /*
8464 * cpu_load_without - compute CPU load without any contributions from *p
8465 * @cpu: the CPU which load is requested
8466 * @p: the task which load should be discounted
8467 *
8468 * The load of a CPU is defined by the load of tasks currently enqueued on that
8469 * CPU as well as tasks which are currently sleeping after an execution on that
8470 * CPU.
8471 *
8472 * This method returns the load of the specified CPU by discounting the load of
8473 * the specified task, whenever the task is currently contributing to the CPU
8474 * load.
8475 */
8476 static unsigned long cpu_load_without(struct rq *rq, struct task_struct *p)
8477 {
8478 struct cfs_rq *cfs_rq;
8479 unsigned int load;
8480
8481 /* Task has no contribution or is new */
8482 if (cpu_of(rq) != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time))
8483 return cpu_load(rq);
8484
8485 cfs_rq = &rq->cfs;
8486 load = READ_ONCE(cfs_rq->avg.load_avg);
8487
8488 /* Discount task's util from CPU's util */
8489 lsub_positive(&load, task_h_load(p));
8490
8491 return load;
8492 }
8493
8494 static unsigned long cpu_runnable(struct rq *rq)
8495 {
8496 return cfs_rq_runnable_avg(&rq->cfs);
8497 }
8498
8499 static unsigned long cpu_runnable_without(struct rq *rq, struct task_struct *p)
8500 {
8501 struct cfs_rq *cfs_rq;
8502 unsigned int runnable;
8503
8504 /* Task has no contribution or is new */
8505 if (cpu_of(rq) != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time))
8506 return cpu_runnable(rq);
8507
8508 cfs_rq = &rq->cfs;
8509 runnable = READ_ONCE(cfs_rq->avg.runnable_avg);
8510
8511 /* Discount task's runnable from CPU's runnable */
8512 lsub_positive(&runnable, p->se.avg.runnable_avg);
8513
8514 return runnable;
8515 }
8516
8517 static unsigned long capacity_of(int cpu)
8518 {
8519 return cpu_rq(cpu)->cpu_capacity;
8520 }
8521
8522 static void record_wakee(struct task_struct *p)
8523 {
8524 /*
8525 * Only decay a single time; tasks that have less then 1 wakeup per
8526 * jiffy will not have built up many flips.
8527 */
8528 if (time_after(jiffies, current->wakee_flip_decay_ts + HZ)) {
8529 current->wakee_flips >>= 1;
8530 current->wakee_flip_decay_ts = jiffies;
8531 }
8532
8533 if (current->last_wakee != p) {
8534 current->last_wakee = p;
8535 current->wakee_flips++;
8536 }
8537 }
8538
8539 /*
8540 * Detect M:N waker/wakee relationships via a switching-frequency heuristic.
8541 *
8542 * A waker of many should wake a different task than the one last awakened
8543 * at a frequency roughly N times higher than one of its wakees.
8544 *
8545 * In order to determine whether we should let the load spread vs consolidating
8546 * to shared cache, we look for a minimum 'flip' frequency of llc_size in one
8547 * partner, and a factor of lls_size higher frequency in the other.
8548 *
8549 * With both conditions met, we can be relatively sure that the relationship is
8550 * non-monogamous, with partner count exceeding socket size.
8551 *
8552 * Waker/wakee being client/server, worker/dispatcher, interrupt source or
8553 * whatever is irrelevant, spread criteria is apparent partner count exceeds
8554 * socket size.
8555 */
8556 static int wake_wide(struct task_struct *p)
8557 {
8558 unsigned int master = current->wakee_flips;
8559 unsigned int slave = p->wakee_flips;
8560 int factor = __this_cpu_read(sd_llc_size);
8561
8562 if (master < slave)
8563 swap(master, slave);
8564 if (slave < factor || master < slave * factor)
8565 return 0;
8566 return 1;
8567 }
8568
8569 /*
8570 * The purpose of wake_affine() is to quickly determine on which CPU we can run
8571 * soonest. For the purpose of speed we only consider the waking and previous
8572 * CPU.
8573 *
8574 * wake_affine_idle() - only considers 'now', it check if the waking CPU is
8575 * cache-affine and is (or will be) idle.
8576 *
8577 * wake_affine_weight() - considers the weight to reflect the average
8578 * scheduling latency of the CPUs. This seems to work
8579 * for the overloaded case.
8580 */
8581 static int
8582 wake_affine_idle(int this_cpu, int prev_cpu, int sync)
8583 {
8584 /*
8585 * If this_cpu is idle, it implies the wakeup is from interrupt
8586 * context. Only allow the move if cache is shared. Otherwise an
8587 * interrupt intensive workload could force all tasks onto one
8588 * node depending on the IO topology or IRQ affinity settings.
8589 *
8590 * If the prev_cpu is idle and cache affine then avoid a migration.
8591 * There is no guarantee that the cache hot data from an interrupt
8592 * is more important than cache hot data on the prev_cpu and from
8593 * a cpufreq perspective, it's better to have higher utilisation
8594 * on one CPU.
8595 */
8596 if (available_idle_cpu(this_cpu) && cpus_share_cache(this_cpu, prev_cpu))
8597 return available_idle_cpu(prev_cpu) ? prev_cpu : this_cpu;
8598
8599 if (sync) {
8600 struct rq *rq = cpu_rq(this_cpu);
8601
8602 if ((rq->nr_running - cfs_h_nr_delayed(rq)) == 1)
8603 return this_cpu;
8604 }
8605
8606 if (available_idle_cpu(prev_cpu))
8607 return prev_cpu;
8608
8609 return nr_cpumask_bits;
8610 }
8611
8612 static int
8613 wake_affine_weight(struct sched_domain *sd, struct task_struct *p,
8614 int this_cpu, int prev_cpu, int sync)
8615 {
8616 s64 this_eff_load, prev_eff_load;
8617 unsigned long task_load;
8618
8619 this_eff_load = cpu_load(cpu_rq(this_cpu));
8620
8621 if (sync) {
8622 unsigned long current_load = task_h_load(current);
8623
8624 if (current_load > this_eff_load)
8625 return this_cpu;
8626
8627 this_eff_load -= current_load;
8628 }
8629
8630 task_load = task_h_load(p);
8631
8632 this_eff_load += task_load;
8633 if (sched_feat(WA_BIAS))
8634 this_eff_load *= 100;
8635 this_eff_load *= capacity_of(prev_cpu);
8636
8637 prev_eff_load = cpu_load(cpu_rq(prev_cpu));
8638 prev_eff_load -= task_load;
8639 if (sched_feat(WA_BIAS))
8640 prev_eff_load *= 100 + (sd->imbalance_pct - 100) / 2;
8641 prev_eff_load *= capacity_of(this_cpu);
8642
8643 /*
8644 * If sync, adjust the weight of prev_eff_load such that if
8645 * prev_eff == this_eff that select_idle_sibling() will consider
8646 * stacking the wakee on top of the waker if no other CPU is
8647 * idle.
8648 */
8649 if (sync)
8650 prev_eff_load += 1;
8651
8652 return this_eff_load < prev_eff_load ? this_cpu : nr_cpumask_bits;
8653 }
8654
8655 static int wake_affine(struct sched_domain *sd, struct task_struct *p,
8656 int this_cpu, int prev_cpu, int sync)
8657 {
8658 int target = nr_cpumask_bits;
8659
8660 if (sched_feat(WA_IDLE))
8661 target = wake_affine_idle(this_cpu, prev_cpu, sync);
8662
8663 if (sched_feat(WA_WEIGHT) && target == nr_cpumask_bits)
8664 target = wake_affine_weight(sd, p, this_cpu, prev_cpu, sync);
8665
8666 schedstat_inc(p->stats.nr_wakeups_affine_attempts);
8667 if (target != this_cpu)
8668 return prev_cpu;
8669
8670 schedstat_inc(sd->ttwu_move_affine);
8671 schedstat_inc(p->stats.nr_wakeups_affine);
8672 return target;
8673 }
8674
8675 static struct sched_group *
8676 sched_balance_find_dst_group(struct sched_domain *sd, struct task_struct *p, int this_cpu);
8677
8678 /*
8679 * sched_balance_find_dst_group_cpu - find the idlest CPU among the CPUs in the group.
8680 */
8681 static int
8682 sched_balance_find_dst_group_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
8683 {
8684 unsigned long load, min_load = ULONG_MAX;
8685 unsigned int min_exit_latency = UINT_MAX;
8686 u64 latest_idle_timestamp = 0;
8687 int least_loaded_cpu = this_cpu;
8688 int shallowest_idle_cpu = -1;
8689 int i;
8690
8691 /* Check if we have any choice: */
8692 if (group->group_weight == 1)
8693 return cpumask_first(sched_group_span(group));
8694
8695 /* Traverse only the allowed CPUs */
8696 for_each_cpu_and(i, sched_group_span(group), p->cpus_ptr) {
8697 struct rq *rq = cpu_rq(i);
8698
8699 if (!sched_core_cookie_match(rq, p))
8700 continue;
8701
8702 if (choose_sched_idle_rq(rq, p))
8703 return i;
8704
8705 if (available_idle_cpu(i)) {
8706 struct cpuidle_state *idle = idle_get_state(rq);
8707 if (idle && idle->exit_latency < min_exit_latency) {
8708 /*
8709 * We give priority to a CPU whose idle state
8710 * has the smallest exit latency irrespective
8711 * of any idle timestamp.
8712 */
8713 min_exit_latency = idle->exit_latency;
8714 latest_idle_timestamp = rq->idle_stamp;
8715 shallowest_idle_cpu = i;
8716 } else if ((!idle || idle->exit_latency == min_exit_latency) &&
8717 rq->idle_stamp > latest_idle_timestamp) {
8718 /*
8719 * If equal or no active idle state, then
8720 * the most recently idled CPU might have
8721 * a warmer cache.
8722 */
8723 latest_idle_timestamp = rq->idle_stamp;
8724 shallowest_idle_cpu = i;
8725 }
8726 } else if (shallowest_idle_cpu == -1) {
8727 load = cpu_load(cpu_rq(i));
8728 if (load < min_load) {
8729 min_load = load;
8730 least_loaded_cpu = i;
8731 }
8732 }
8733 }
8734
8735 return shallowest_idle_cpu != -1 ? shallowest_idle_cpu : least_loaded_cpu;
8736 }
8737
8738 static inline int sched_balance_find_dst_cpu(struct sched_domain *sd, struct task_struct *p,
8739 int cpu, int prev_cpu, int sd_flag)
8740 {
8741 int new_cpu = cpu;
8742
8743 if (!cpumask_intersects(sched_domain_span(sd), p->cpus_ptr))
8744 return prev_cpu;
8745
8746 /*
8747 * We need task's util for cpu_util_without, sync it up to
8748 * prev_cpu's last_update_time.
8749 */
8750 if (!(sd_flag & SD_BALANCE_FORK))
8751 sync_entity_load_avg(&p->se);
8752
8753 while (sd) {
8754 struct sched_group *group;
8755 struct sched_domain *tmp;
8756 int weight;
8757
8758 if (!(sd->flags & sd_flag)) {
8759 sd = sd->child;
8760 continue;
8761 }
8762
8763 group = sched_balance_find_dst_group(sd, p, cpu);
8764 if (!group) {
8765 sd = sd->child;
8766 continue;
8767 }
8768
8769 new_cpu = sched_balance_find_dst_group_cpu(group, p, cpu);
8770 if (new_cpu == cpu) {
8771 /* Now try balancing at a lower domain level of 'cpu': */
8772 sd = sd->child;
8773 continue;
8774 }
8775
8776 /* Now try balancing at a lower domain level of 'new_cpu': */
8777 cpu = new_cpu;
8778 weight = sd->span_weight;
8779 sd = NULL;
8780 for_each_domain(cpu, tmp) {
8781 if (weight <= tmp->span_weight)
8782 break;
8783 if (tmp->flags & sd_flag)
8784 sd = tmp;
8785 }
8786 }
8787
8788 return new_cpu;
8789 }
8790
8791 static inline int __select_idle_cpu(int cpu, struct task_struct *p)
8792 {
8793 if (choose_idle_cpu(cpu, p) && sched_cpu_cookie_match(cpu_rq(cpu), p))
8794 return cpu;
8795
8796 return -1;
8797 }
8798
8799 DEFINE_STATIC_KEY_FALSE(sched_smt_present);
8800 EXPORT_SYMBOL_GPL(sched_smt_present);
8801
8802 static inline void set_idle_cores(int cpu, int val)
8803 {
8804 struct sched_domain_shared *sds;
8805
8806 sds = rcu_dereference_all(per_cpu(sd_balance_shared, cpu));
8807 if (sds)
8808 WRITE_ONCE(sds->has_idle_cores, val);
8809 }
8810
8811 static inline bool test_idle_cores(int cpu)
8812 {
8813 struct sched_domain_shared *sds;
8814
8815 sds = rcu_dereference_all(per_cpu(sd_balance_shared, cpu));
8816 if (sds)
8817 return READ_ONCE(sds->has_idle_cores);
8818
8819 return false;
8820 }
8821
8822 /*
8823 * Scans the local SMT mask to see if the entire core is idle, and records this
8824 * information in sd_balance_shared->has_idle_cores.
8825 *
8826 * Since SMT siblings share all cache levels, inspecting this limited remote
8827 * state should be fairly cheap.
8828 */
8829 void __update_idle_core(struct rq *rq)
8830 {
8831 int core = cpu_of(rq);
8832 int cpu;
8833
8834 rcu_read_lock();
8835 if (test_idle_cores(core))
8836 goto unlock;
8837
8838 for_each_cpu(cpu, cpu_smt_mask(core)) {
8839 if (cpu == core)
8840 continue;
8841
8842 if (!available_idle_cpu(cpu))
8843 goto unlock;
8844 }
8845
8846 set_idle_cores(core, 1);
8847 unlock:
8848 rcu_read_unlock();
8849 }
8850
8851 /*
8852 * Scan the entire LLC domain for idle cores; this dynamically switches off if
8853 * there are no idle cores left in the system; tracked through
8854 * sd_balance_shared->has_idle_cores and enabled through update_idle_core()
8855 * above.
8856 */
8857 static int select_idle_core(struct task_struct *p, int core, struct cpumask *cpus, int *idle_cpu)
8858 {
8859 bool idle = true;
8860 int cpu;
8861
8862 for_each_cpu(cpu, cpu_smt_mask(core)) {
8863 if (!available_idle_cpu(cpu)) {
8864 idle = false;
8865 if (*idle_cpu == -1) {
8866 if (choose_sched_idle_rq(cpu_rq(cpu), p) &&
8867 cpumask_test_cpu(cpu, cpus)) {
8868 *idle_cpu = cpu;
8869 break;
8870 }
8871 continue;
8872 }
8873 break;
8874 }
8875 if (*idle_cpu == -1 && cpumask_test_cpu(cpu, cpus))
8876 *idle_cpu = cpu;
8877 }
8878
8879 if (idle)
8880 return core;
8881
8882 cpumask_andnot(cpus, cpus, cpu_smt_mask(core));
8883 return -1;
8884 }
8885
8886 /*
8887 * Scan the local SMT mask for idle CPUs.
8888 */
8889 static int select_idle_smt(struct task_struct *p, struct sched_domain *sd, int target)
8890 {
8891 int cpu;
8892
8893 for_each_cpu_and(cpu, cpu_smt_mask(target), p->cpus_ptr) {
8894 if (cpu == target)
8895 continue;
8896 /*
8897 * Check if the CPU is in the LLC scheduling domain of @target.
8898 * Due to isolcpus, there is no guarantee that all the siblings are in the domain.
8899 */
8900 if (!cpumask_test_cpu(cpu, sched_domain_span(sd)))
8901 continue;
8902 if (choose_idle_cpu(cpu, p))
8903 return cpu;
8904 }
8905
8906 return -1;
8907 }
8908
8909 /*
8910 * Scan the LLC domain for idle CPUs; this is dynamically regulated by
8911 * comparing the average scan cost (tracked in sd->avg_scan_cost) against the
8912 * average idle time for this rq (as found in rq->avg_idle).
8913 */
8914 static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool has_idle_core, int target)
8915 {
8916 struct cpumask *cpus = this_cpu_cpumask_var_ptr(select_rq_mask);
8917 int i, cpu, idle_cpu = -1, nr = INT_MAX;
8918
8919 if (sched_feat(SIS_UTIL) && sd->shared) {
8920 /*
8921 * Increment because !--nr is the condition to stop scan.
8922 *
8923 * Since "sd" is "sd_llc" for target CPU dereferenced in the
8924 * caller, it is safe to directly dereference "sd->shared".
8925 * Topology bits always ensure it assigned for "sd_llc" abd it
8926 * cannot disappear as long as we have a RCU protected
8927 * reference to one the associated "sd" here.
8928 */
8929 nr = READ_ONCE(sd->shared->nr_idle_scan) + 1;
8930 /* overloaded LLC is unlikely to have idle cpu/core */
8931 if (nr == 1)
8932 return -1;
8933 }
8934
8935 if (!cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr))
8936 return -1;
8937
8938 if (static_branch_unlikely(&sched_cluster_active)) {
8939 struct sched_group *sg = sd->groups;
8940
8941 if (sg->flags & SD_CLUSTER) {
8942 for_each_cpu_wrap(cpu, sched_group_span(sg), target + 1) {
8943 if (!cpumask_test_cpu(cpu, cpus))
8944 continue;
8945
8946 if (has_idle_core) {
8947 i = select_idle_core(p, cpu, cpus, &idle_cpu);
8948 if ((unsigned int)i < nr_cpumask_bits)
8949 return i;
8950 } else {
8951 if (--nr <= 0)
8952 return -1;
8953 idle_cpu = __select_idle_cpu(cpu, p);
8954 if ((unsigned int)idle_cpu < nr_cpumask_bits)
8955 return idle_cpu;
8956 }
8957 }
8958 cpumask_andnot(cpus, cpus, sched_group_span(sg));
8959 }
8960 }
8961
8962 for_each_cpu_wrap(cpu, cpus, target + 1) {
8963 if (has_idle_core) {
8964 i = select_idle_core(p, cpu, cpus, &idle_cpu);
8965 if ((unsigned int)i < nr_cpumask_bits)
8966 return i;
8967
8968 } else {
8969 if (--nr <= 0)
8970 return -1;
8971 idle_cpu = __select_idle_cpu(cpu, p);
8972 if ((unsigned int)idle_cpu < nr_cpumask_bits)
8973 break;
8974 }
8975 }
8976
8977 if (has_idle_core)
8978 set_idle_cores(target, false);
8979
8980 return idle_cpu;
8981 }
8982
8983 /*
8984 * Idle-capacity scan converts util_fits_cpu() outcomes into preference ranks,
8985 * where lower values indicate a better fit - see select_idle_capacity().
8986 *
8987 * A CPU that both fits the task and sits on a fully-idle SMT core is returned
8988 * immediately and is never assigned one of these ranks. On !SMT every CPU is
8989 * its own "core", so the early return covers all fits-and-idle cases and the
8990 * core-tier ranks below become unreachable.
8991 *
8992 * Rank Val Tier Meaning
8993 * ------------------------------ --- ------ ---------------------------
8994 * ASYM_IDLE_UCLAMP_MISFIT -4 core Idle core; capacity fits
8995 * util but uclamp_min misses.
8996 * ASYM_IDLE_COMPLETE_MISFIT -3 core Idle core; capacity does
8997 * not fit. Still beats every
8998 * thread-tier rank: a busy
8999 * sibling cuts effective
9000 * capacity more than a
9001 * misfit hurts a quiet core.
9002 * ASYM_IDLE_THREAD_FITS -2 thread Busy SMT sibling; capacity
9003 * fits util + uclamp.
9004 * ASYM_IDLE_THREAD_UCLAMP_MISFIT -1 thread Busy SMT sibling; capacity
9005 * fits but uclamp_min misses
9006 * (native util_fits_cpu()
9007 * return value).
9008 * ASYM_IDLE_THREAD_MISFIT 0 thread Busy SMT sibling; capacity
9009 * does not fit.
9010 *
9011 * ASYM_IDLE_CORE_BIAS (-3) is an offset, not a state. On an idle core,
9012 * fits += ASYM_IDLE_CORE_BIAS rebases thread-tier ranks into the core tier:
9013 *
9014 * ASYM_IDLE_THREAD_UCLAMP_MISFIT (-1) + BIAS -> ASYM_IDLE_UCLAMP_MISFIT (-4)
9015 * ASYM_IDLE_THREAD_MISFIT (0) + BIAS -> ASYM_IDLE_COMPLETE_MISFIT (-3)
9016 *
9017 * ASYM_IDLE_THREAD_FITS (-2) is never rebased because a fully-fitting idle-core
9018 * candidate early-returns from select_idle_capacity().
9019 */
9020 enum asym_fits_state {
9021 ASYM_IDLE_UCLAMP_MISFIT = -4,
9022 ASYM_IDLE_COMPLETE_MISFIT,
9023 ASYM_IDLE_THREAD_FITS,
9024 ASYM_IDLE_THREAD_UCLAMP_MISFIT,
9025 ASYM_IDLE_THREAD_MISFIT,
9026
9027 /* util_fits_cpu() bias for idle core */
9028 ASYM_IDLE_CORE_BIAS = -3,
9029 };
9030
9031 /*
9032 * Scan the asym_capacity domain for idle CPUs; pick the first idle one on which
9033 * the task fits. If no CPU is big enough, but there are idle ones, try to
9034 * maximize capacity.
9035 */
9036 static int
9037 select_idle_capacity(struct task_struct *p, struct sched_domain *sd, int target)
9038 {
9039 /*
9040 * On !SMT systems, has_idle_core is always false and preferred_core
9041 * is always true (CPU == core), so the SMT preference logic below
9042 * collapses to the plain capacity scan.
9043 */
9044 bool has_idle_core = sched_smt_active() && test_idle_cores(target);
9045 unsigned long task_util, util_min, util_max, best_cap = 0;
9046 int fits, best_fits = ASYM_IDLE_THREAD_MISFIT;
9047 int cpu, best_cpu = -1;
9048 struct cpumask *cpus;
9049 int nr = INT_MAX;
9050
9051 cpus = this_cpu_cpumask_var_ptr(select_rq_mask);
9052 cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr);
9053
9054 task_util = task_util_est(p);
9055 util_min = uclamp_eff_value(p, UCLAMP_MIN);
9056 util_max = uclamp_eff_value(p, UCLAMP_MAX);
9057
9058 if (sched_feat(SIS_UTIL) && sd->shared) {
9059 /*
9060 * Same nr_idle_scan hint as select_idle_cpu(), nr only limits
9061 * the scan when not preferring an idle core.
9062 */
9063 nr = READ_ONCE(sd->shared->nr_idle_scan) + 1;
9064 /* overloaded domain is unlikely to have idle cpu/core */
9065 if (nr == 1)
9066 return -1;
9067 }
9068
9069 for_each_cpu_wrap(cpu, cpus, target) {
9070 bool preferred_core = !has_idle_core || is_core_idle(cpu);
9071 unsigned long cpu_cap = capacity_of(cpu);
9072
9073 /*
9074 * Stop when the nr_idle_scan is exhausted (mirrors
9075 * select_idle_cpu() logic).
9076 */
9077 if (!has_idle_core && --nr <= 0)
9078 return best_cpu;
9079
9080 if (!choose_idle_cpu(cpu, p))
9081 continue;
9082
9083 fits = util_fits_cpu(task_util, util_min, util_max, cpu);
9084
9085 /*
9086 * Perfect fit: capacity satisfies util + uclamp and the CPU
9087 * sits on a fully-idle SMT core, this is a !SMT system, or
9088 * there is no idle core to find.
9089 * Short-circuit the rank-based selection and return
9090 * immediately.
9091 */
9092 if (fits > 0 && preferred_core)
9093 return cpu;
9094 /*
9095 * Only the min performance hint (i.e. uclamp_min) doesn't fit.
9096 * Look for the CPU with best capacity.
9097 */
9098 else if (fits < 0)
9099 cpu_cap = get_actual_cpu_capacity(cpu);
9100 /*
9101 * fits > 0 implies we are not on a preferred core, but the util
9102 * fits CPU capacity. Set fits to ASYM_IDLE_THREAD_FITS
9103 * so the effective range becomes
9104 * [ASYM_IDLE_THREAD_FITS, ASYM_IDLE_THREAD_MISFIT], where:
9105 * ASYM_IDLE_THREAD_MISFIT - does not fit
9106 * ASYM_IDLE_THREAD_UCLAMP_MISFIT - fits with the exception of UCLAMP_MIN
9107 * ASYM_IDLE_THREAD_FITS - fits with the exception of preferred_core
9108 */
9109 else if (fits > 0)
9110 fits = ASYM_IDLE_THREAD_FITS;
9111
9112 /*
9113 * If we are on a preferred core, translate the range of fits
9114 * of [ASYM_IDLE_THREAD_UCLAMP_MISFIT, ASYM_IDLE_THREAD_MISFIT] to
9115 * [ASYM_IDLE_UCLAMP_MISFIT, ASYM_IDLE_COMPLETE_MISFIT].
9116 * This ensures that an idle core is always given priority over
9117 * (partially) busy core.
9118 *
9119 * A fully fitting idle core would have returned early and hence
9120 * fits > 0 for preferred_core need not be dealt with.
9121 */
9122 if (preferred_core)
9123 fits += ASYM_IDLE_CORE_BIAS;
9124
9125 /*
9126 * First, select CPU which fits better (lower is more preferred).
9127 * Then, select the one with best capacity at same level.
9128 */
9129 if ((fits < best_fits) ||
9130 ((fits == best_fits) && (cpu_cap > best_cap))) {
9131 best_cap = cpu_cap;
9132 best_cpu = cpu;
9133 best_fits = fits;
9134 }
9135 }
9136
9137 /*
9138 * A value in the [ASYM_IDLE_UCLAMP_MISFIT, ASYM_IDLE_COMPLETE_MISFIT]
9139 * range means the chosen CPU is in a fully idle SMT core. Values above
9140 * ASYM_IDLE_COMPLETE_MISFIT mean we never ranked such a CPU best.
9141 *
9142 * The asym-capacity wakeup path returns from select_idle_sibling()
9143 * after this function and never runs select_idle_cpu(), so the usual
9144 * select_idle_cpu() tail that clears idle cores must live here when the
9145 * idle-core preference did not win.
9146 */
9147 if (has_idle_core && best_fits > ASYM_IDLE_COMPLETE_MISFIT)
9148 set_idle_cores(target, false);
9149
9150 return best_cpu;
9151 }
9152
9153 static inline bool asym_fits_cpu(unsigned long util,
9154 unsigned long util_min,
9155 unsigned long util_max,
9156 int cpu)
9157 {
9158 if (sched_asym_cpucap_active()) {
9159 /*
9160 * Return true only if the cpu fully fits the task requirements
9161 * which include the utilization and the performance hints.
9162 *
9163 * When SMT is active, also require that the core has no busy
9164 * siblings.
9165 *
9166 * Note: gating on is_core_idle() also makes the early-bailout
9167 * candidates in select_idle_sibling() (target, prev,
9168 * recent_used_cpu) idle-core-aware on ASYM+SMT, which the
9169 * NO_ASYM path does not do.
9170 */
9171 return (!sched_smt_active() || is_core_idle(cpu)) &&
9172 (util_fits_cpu(util, util_min, util_max, cpu) > 0);
9173 }
9174
9175 return true;
9176 }
9177
9178 /*
9179 * Try and locate an idle core/thread in the LLC cache domain.
9180 */
9181 static int select_idle_sibling(struct task_struct *p, int prev, int target)
9182 {
9183 bool has_idle_core = false;
9184 struct sched_domain *sd;
9185 unsigned long task_util, util_min, util_max;
9186 int i, recent_used_cpu, prev_aff = -1;
9187
9188 /*
9189 * On asymmetric system, update task utilization because we will check
9190 * that the task fits with CPU's capacity.
9191 */
9192 if (sched_asym_cpucap_active()) {
9193 sync_entity_load_avg(&p->se);
9194 task_util = task_util_est(p);
9195 util_min = uclamp_eff_value(p, UCLAMP_MIN);
9196 util_max = uclamp_eff_value(p, UCLAMP_MAX);
9197 }
9198
9199 /*
9200 * per-cpu select_rq_mask usage
9201 */
9202 lockdep_assert_irqs_disabled();
9203
9204 if (choose_idle_cpu(target, p) &&
9205 asym_fits_cpu(task_util, util_min, util_max, target))
9206 return target;
9207
9208 /*
9209 * If the previous CPU is cache affine and idle, don't be stupid:
9210 */
9211 if (prev != target && cpus_share_cache(prev, target) &&
9212 choose_idle_cpu(prev, p) &&
9213 asym_fits_cpu(task_util, util_min, util_max, prev)) {
9214
9215 if (!static_branch_unlikely(&sched_cluster_active) ||
9216 cpus_share_resources(prev, target))
9217 return prev;
9218
9219 prev_aff = prev;
9220 }
9221
9222 /*
9223 * Allow a per-cpu kthread to stack with the wakee if the
9224 * kworker thread and the tasks previous CPUs are the same.
9225 * The assumption is that the wakee queued work for the
9226 * per-cpu kthread that is now complete and the wakeup is
9227 * essentially a sync wakeup. An obvious example of this
9228 * pattern is IO completions.
9229 */
9230 if (is_per_cpu_kthread(current) &&
9231 in_task() &&
9232 prev == smp_processor_id() &&
9233 this_rq()->nr_running <= 1 &&
9234 asym_fits_cpu(task_util, util_min, util_max, prev)) {
9235 return prev;
9236 }
9237
9238 /* Check a recently used CPU as a potential idle candidate: */
9239 recent_used_cpu = p->recent_used_cpu;
9240 p->recent_used_cpu = prev;
9241 if (recent_used_cpu != prev &&
9242 recent_used_cpu != target &&
9243 cpus_share_cache(recent_used_cpu, target) &&
9244 choose_idle_cpu(recent_used_cpu, p) &&
9245 cpumask_test_cpu(recent_used_cpu, p->cpus_ptr) &&
9246 asym_fits_cpu(task_util, util_min, util_max, recent_used_cpu)) {
9247
9248 if (!static_branch_unlikely(&sched_cluster_active) ||
9249 cpus_share_resources(recent_used_cpu, target))
9250 return recent_used_cpu;
9251
9252 } else {
9253 recent_used_cpu = -1;
9254 }
9255
9256 /*
9257 * For asymmetric CPU capacity systems, our domain of interest is
9258 * sd_asym_cpucapacity rather than sd_llc.
9259 */
9260 if (sched_asym_cpucap_active()) {
9261 sd = rcu_dereference_all(per_cpu(sd_asym_cpucapacity, target));
9262 /*
9263 * On an asymmetric CPU capacity system where an exclusive
9264 * cpuset defines a symmetric island (i.e. one unique
9265 * capacity_orig value through the cpuset), the key will be set
9266 * but the CPUs within that cpuset will not have a domain with
9267 * SD_ASYM_CPUCAPACITY. These should follow the usual symmetric
9268 * capacity path.
9269 */
9270 if (sd) {
9271 i = select_idle_capacity(p, sd, target);
9272 return ((unsigned)i < nr_cpumask_bits) ? i : target;
9273 }
9274 }
9275
9276 sd = rcu_dereference_all(per_cpu(sd_llc, target));
9277 if (!sd)
9278 return target;
9279
9280 if (sched_smt_active()) {
9281 has_idle_core = test_idle_cores(target);
9282
9283 if (!has_idle_core && cpus_share_cache(prev, target)) {
9284 i = select_idle_smt(p, sd, prev);
9285 if ((unsigned int)i < nr_cpumask_bits)
9286 return i;
9287 }
9288 }
9289
9290 i = select_idle_cpu(p, sd, has_idle_core, target);
9291 if ((unsigned)i < nr_cpumask_bits)
9292 return i;
9293
9294 /*
9295 * For cluster machines which have lower sharing cache like L2 or
9296 * LLC Tag, we tend to find an idle CPU in the target's cluster
9297 * first. But prev_cpu or recent_used_cpu may also be a good candidate,
9298 * use them if possible when no idle CPU found in select_idle_cpu().
9299 */
9300 if ((unsigned int)prev_aff < nr_cpumask_bits)
9301 return prev_aff;
9302 if ((unsigned int)recent_used_cpu < nr_cpumask_bits)
9303 return recent_used_cpu;
9304
9305 return target;
9306 }
9307
9308 /**
9309 * cpu_util() - Estimates the amount of CPU capacity used by CFS tasks.
9310 * @cpu: the CPU to get the utilization for
9311 * @p: task for which the CPU utilization should be predicted or NULL
9312 * @dst_cpu: CPU @p migrates to, -1 if @p moves from @cpu or @p == NULL
9313 * @boost: 1 to enable boosting, otherwise 0
9314 *
9315 * The unit of the return value must be the same as the one of CPU capacity
9316 * so that CPU utilization can be compared with CPU capacity.
9317 *
9318 * CPU utilization is the sum of running time of runnable tasks plus the
9319 * recent utilization of currently non-runnable tasks on that CPU.
9320 * It represents the amount of CPU capacity currently used by CFS tasks in
9321 * the range [0..max CPU capacity] with max CPU capacity being the CPU
9322 * capacity at f_max.
9323 *
9324 * The estimated CPU utilization is defined as the maximum between CPU
9325 * utilization and sum of the estimated utilization of the currently
9326 * runnable tasks on that CPU. It preserves a utilization "snapshot" of
9327 * previously-executed tasks, which helps better deduce how busy a CPU will
9328 * be when a long-sleeping task wakes up. The contribution to CPU utilization
9329 * of such a task would be significantly decayed at this point of time.
9330 *
9331 * Boosted CPU utilization is defined as max(CPU runnable, CPU utilization).
9332 * CPU contention for CFS tasks can be detected by CPU runnable > CPU
9333 * utilization. Boosting is implemented in cpu_util() so that internal
9334 * users (e.g. EAS) can use it next to external users (e.g. schedutil),
9335 * latter via cpu_util_cfs_boost().
9336 *
9337 * CPU utilization can be higher than the current CPU capacity
9338 * (f_curr/f_max * max CPU capacity) or even the max CPU capacity because
9339 * of rounding errors as well as task migrations or wakeups of new tasks.
9340 * CPU utilization has to be capped to fit into the [0..max CPU capacity]
9341 * range. Otherwise a group of CPUs (CPU0 util = 121% + CPU1 util = 80%)
9342 * could be seen as over-utilized even though CPU1 has 20% of spare CPU
9343 * capacity. CPU utilization is allowed to overshoot current CPU capacity
9344 * though since this is useful for predicting the CPU capacity required
9345 * after task migrations (scheduler-driven DVFS).
9346 *
9347 * Return: (Boosted) (estimated) utilization for the specified CPU.
9348 */
9349 static unsigned long
9350 cpu_util(int cpu, struct task_struct *p, int dst_cpu, int boost)
9351 {
9352 bool add_task = p && task_cpu(p) != cpu && dst_cpu == cpu;
9353 bool sub_task = p && task_cpu(p) == cpu && dst_cpu != cpu;
9354 struct cfs_rq *cfs_rq = &cpu_rq(cpu)->cfs;
9355 unsigned long util = READ_ONCE(cfs_rq->avg.util_avg);
9356 unsigned long runnable;
9357
9358 /*
9359 * If @dst_cpu is -1 or @p migrates from @cpu to @dst_cpu remove its
9360 * contribution. If @p migrates from another CPU to @cpu add its
9361 * contribution. In all the other cases @cpu is not impacted by the
9362 * migration so its util_avg is already correct.
9363 */
9364 if (add_task)
9365 util += task_util(p);
9366 else if (sub_task)
9367 lsub_positive(&util, task_util(p));
9368
9369 if (boost) {
9370 runnable = READ_ONCE(cfs_rq->avg.runnable_avg);
9371 if (add_task)
9372 runnable += READ_ONCE(p->se.avg.runnable_avg);
9373 else if (sub_task)
9374 lsub_positive(&runnable,
9375 READ_ONCE(p->se.avg.runnable_avg));
9376 util = max(util, runnable);
9377 }
9378
9379 if (sched_feat(UTIL_EST)) {
9380 unsigned long util_est;
9381
9382 util_est = READ_ONCE(cfs_rq->avg.util_est);
9383
9384 /*
9385 * During wake-up @p isn't enqueued yet and doesn't contribute
9386 * to any cpu_rq(cpu)->cfs.avg.util_est.
9387 * If @dst_cpu == @cpu add it to "simulate" cpu_util after @p
9388 * has been enqueued.
9389 *
9390 * During exec (@dst_cpu = -1) @p is enqueued and does
9391 * contribute to cpu_rq(cpu)->cfs.util_est.
9392 * Remove it to "simulate" cpu_util without @p's contribution.
9393 *
9394 * Despite the task_on_rq_queued(@p) check there is still a
9395 * small window for a possible race when an exec
9396 * select_task_rq_fair() races with LB's detach_task().
9397 *
9398 * detach_task()
9399 * deactivate_task()
9400 * p->on_rq = TASK_ON_RQ_MIGRATING;
9401 * -------------------------------- A
9402 * dequeue_task() \
9403 * dequeue_task_fair() + Race Time
9404 * util_est_dequeue() /
9405 * -------------------------------- B
9406 *
9407 * The additional check "current == p" is required to further
9408 * reduce the race window.
9409 */
9410 if (dst_cpu == cpu)
9411 util_est += _task_util_est(p);
9412 else if (p && unlikely(task_on_rq_queued(p) || current == p))
9413 lsub_positive(&util_est, _task_util_est(p));
9414
9415 util = max(util, util_est);
9416 }
9417
9418 return min(util, arch_scale_cpu_capacity(cpu));
9419 }
9420
9421 unsigned long cpu_util_cfs(int cpu)
9422 {
9423 return cpu_util(cpu, NULL, -1, 0);
9424 }
9425
9426 unsigned long cpu_util_cfs_boost(int cpu)
9427 {
9428 return cpu_util(cpu, NULL, -1, 1);
9429 }
9430
9431 /*
9432 * cpu_util_without: compute cpu utilization without any contributions from *p
9433 * @cpu: the CPU which utilization is requested
9434 * @p: the task which utilization should be discounted
9435 *
9436 * The utilization of a CPU is defined by the utilization of tasks currently
9437 * enqueued on that CPU as well as tasks which are currently sleeping after an
9438 * execution on that CPU.
9439 *
9440 * This method returns the utilization of the specified CPU by discounting the
9441 * utilization of the specified task, whenever the task is currently
9442 * contributing to the CPU utilization.
9443 */
9444 static unsigned long cpu_util_without(int cpu, struct task_struct *p)
9445 {
9446 /* Task has no contribution or is new */
9447 if (cpu != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time))
9448 p = NULL;
9449
9450 return cpu_util(cpu, p, -1, 0);
9451 }
9452
9453 /*
9454 * This function computes an effective utilization for the given CPU, to be
9455 * used for frequency selection given the linear relation: f = u * f_max.
9456 *
9457 * The scheduler tracks the following metrics:
9458 *
9459 * cpu_util_{cfs,rt,dl,irq}()
9460 * cpu_bw_dl()
9461 *
9462 * Where the cfs,rt and dl util numbers are tracked with the same metric and
9463 * synchronized windows and are thus directly comparable.
9464 *
9465 * The cfs,rt,dl utilization are the running times measured with rq->clock_task
9466 * which excludes things like IRQ and steal-time. These latter are then accrued
9467 * in the IRQ utilization.
9468 *
9469 * The DL bandwidth number OTOH is not a measured metric but a value computed
9470 * based on the task model parameters and gives the minimal utilization
9471 * required to meet deadlines.
9472 */
9473 unsigned long effective_cpu_util(int cpu, unsigned long util_cfs,
9474 unsigned long *min,
9475 unsigned long *max)
9476 {
9477 unsigned long util, irq, scale;
9478 struct rq *rq = cpu_rq(cpu);
9479
9480 scale = arch_scale_cpu_capacity(cpu);
9481
9482 /*
9483 * Early check to see if IRQ/steal time saturates the CPU, can be
9484 * because of inaccuracies in how we track these -- see
9485 * update_irq_load_avg().
9486 */
9487 irq = cpu_util_irq(rq);
9488 if (unlikely(irq >= scale)) {
9489 if (min)
9490 *min = scale;
9491 if (max)
9492 *max = scale;
9493 return scale;
9494 }
9495
9496 if (min) {
9497 /*
9498 * The minimum utilization returns the highest level between:
9499 * - the computed DL bandwidth needed with the IRQ pressure which
9500 * steals time to the deadline task.
9501 * - The minimum performance requirement for CFS and/or RT.
9502 */
9503 *min = max(irq + cpu_bw_dl(rq), uclamp_rq_get(rq, UCLAMP_MIN));
9504
9505 /*
9506 * When an RT task is runnable and uclamp is not used, we must
9507 * ensure that the task will run at maximum compute capacity.
9508 */
9509 if (!uclamp_is_used() && rt_rq_is_runnable(&rq->rt))
9510 *min = max(*min, scale);
9511 }
9512
9513 /*
9514 * Because the time spend on RT/DL tasks is visible as 'lost' time to
9515 * CFS tasks and we use the same metric to track the effective
9516 * utilization (PELT windows are synchronized) we can directly add them
9517 * to obtain the CPU's actual utilization.
9518 */
9519 util = util_cfs + cpu_util_rt(rq);
9520 util += cpu_util_dl(rq);
9521
9522 /*
9523 * The maximum hint is a soft bandwidth requirement, which can be lower
9524 * than the actual utilization because of uclamp_max requirements.
9525 */
9526 if (max)
9527 *max = min(scale, uclamp_rq_get(rq, UCLAMP_MAX));
9528
9529 if (util >= scale)
9530 return scale;
9531
9532 /*
9533 * There is still idle time; further improve the number by using the
9534 * IRQ metric. Because IRQ/steal time is hidden from the task clock we
9535 * need to scale the task numbers:
9536 *
9537 * max - irq
9538 * U' = irq + --------- * U
9539 * max
9540 */
9541 util = scale_irq_capacity(util, irq, scale);
9542 util += irq;
9543
9544 return min(scale, util);
9545 }
9546
9547 unsigned long sched_cpu_util(int cpu)
9548 {
9549 return effective_cpu_util(cpu, cpu_util_cfs(cpu), NULL, NULL);
9550 }
9551
9552 /*
9553 * energy_env - Utilization landscape for energy estimation.
9554 * @task_busy_time: Utilization contribution by the task for which we test the
9555 * placement. Given by eenv_task_busy_time().
9556 * @pd_busy_time: Utilization of the whole perf domain without the task
9557 * contribution. Given by eenv_pd_busy_time().
9558 * @cpu_cap: Maximum CPU capacity for the perf domain.
9559 * @pd_cap: Entire perf domain capacity. (pd->nr_cpus * cpu_cap).
9560 */
9561 struct energy_env {
9562 unsigned long task_busy_time;
9563 unsigned long pd_busy_time;
9564 unsigned long cpu_cap;
9565 unsigned long pd_cap;
9566 };
9567
9568 /*
9569 * Compute the task busy time for compute_energy(). This time cannot be
9570 * injected directly into effective_cpu_util() because of the IRQ scaling.
9571 * The latter only makes sense with the most recent CPUs where the task has
9572 * run.
9573 */
9574 static inline void eenv_task_busy_time(struct energy_env *eenv,
9575 struct task_struct *p, int prev_cpu)
9576 {
9577 unsigned long busy_time, max_cap = arch_scale_cpu_capacity(prev_cpu);
9578 unsigned long irq = cpu_util_irq(cpu_rq(prev_cpu));
9579
9580 if (unlikely(irq >= max_cap))
9581 busy_time = max_cap;
9582 else
9583 busy_time = scale_irq_capacity(task_util_est(p), irq, max_cap);
9584
9585 eenv->task_busy_time = busy_time;
9586 }
9587
9588 /*
9589 * Compute the perf_domain (PD) busy time for compute_energy(). Based on the
9590 * utilization for each @pd_cpus, it however doesn't take into account
9591 * clamping since the ratio (utilization / cpu_capacity) is already enough to
9592 * scale the EM reported power consumption at the (eventually clamped)
9593 * cpu_capacity.
9594 *
9595 * The contribution of the task @p for which we want to estimate the
9596 * energy cost is removed (by cpu_util()) and must be calculated
9597 * separately (see eenv_task_busy_time). This ensures:
9598 *
9599 * - A stable PD utilization, no matter which CPU of that PD we want to place
9600 * the task on.
9601 *
9602 * - A fair comparison between CPUs as the task contribution (task_util())
9603 * will always be the same no matter which CPU utilization we rely on
9604 * (util_avg or util_est).
9605 *
9606 * Set @eenv busy time for the PD that spans @pd_cpus. This busy time can't
9607 * exceed @eenv->pd_cap.
9608 */
9609 static inline void eenv_pd_busy_time(struct energy_env *eenv,
9610 struct cpumask *pd_cpus,
9611 struct task_struct *p)
9612 {
9613 unsigned long busy_time = 0;
9614 int cpu;
9615
9616 for_each_cpu(cpu, pd_cpus) {
9617 unsigned long util = cpu_util(cpu, p, -1, 0);
9618
9619 busy_time += effective_cpu_util(cpu, util, NULL, NULL);
9620 }
9621
9622 eenv->pd_busy_time = min(eenv->pd_cap, busy_time);
9623 }
9624
9625 /*
9626 * Compute the maximum utilization for compute_energy() when the task @p
9627 * is placed on the cpu @dst_cpu.
9628 *
9629 * Returns the maximum utilization among @eenv->cpus. This utilization can't
9630 * exceed @eenv->cpu_cap.
9631 */
9632 static inline unsigned long
9633 eenv_pd_max_util(struct energy_env *eenv, struct cpumask *pd_cpus,
9634 struct task_struct *p, int dst_cpu)
9635 {
9636 unsigned long max_util = 0;
9637 int cpu;
9638
9639 for_each_cpu(cpu, pd_cpus) {
9640 struct task_struct *tsk = (cpu == dst_cpu) ? p : NULL;
9641 unsigned long util = cpu_util(cpu, p, dst_cpu, 1);
9642 unsigned long eff_util, min, max;
9643
9644 /*
9645 * Performance domain frequency: utilization clamping
9646 * must be considered since it affects the selection
9647 * of the performance domain frequency.
9648 * NOTE: in case RT tasks are running, by default the min
9649 * utilization can be max OPP.
9650 */
9651 eff_util = effective_cpu_util(cpu, util, &min, &max);
9652
9653 /* Task's uclamp can modify min and max value */
9654 if (tsk && uclamp_is_used()) {
9655 min = max(min, uclamp_eff_value(p, UCLAMP_MIN));
9656
9657 /*
9658 * If there is no active max uclamp constraint,
9659 * directly use task's one, otherwise keep max.
9660 */
9661 if (uclamp_rq_is_idle(cpu_rq(cpu)))
9662 max = uclamp_eff_value(p, UCLAMP_MAX);
9663 else
9664 max = max(max, uclamp_eff_value(p, UCLAMP_MAX));
9665 }
9666
9667 eff_util = sugov_effective_cpu_perf(cpu, eff_util, min, max);
9668 max_util = max(max_util, eff_util);
9669 }
9670
9671 return min(max_util, eenv->cpu_cap);
9672 }
9673
9674 /*
9675 * compute_energy(): Use the Energy Model to estimate the energy that @pd would
9676 * consume for a given utilization landscape @eenv. When @dst_cpu < 0, the task
9677 * contribution is ignored.
9678 */
9679 static inline unsigned long
9680 compute_energy(struct energy_env *eenv, struct perf_domain *pd,
9681 struct cpumask *pd_cpus, struct task_struct *p, int dst_cpu)
9682 {
9683 unsigned long max_util = eenv_pd_max_util(eenv, pd_cpus, p, dst_cpu);
9684 unsigned long busy_time = eenv->pd_busy_time;
9685 unsigned long energy;
9686
9687 if (dst_cpu >= 0)
9688 busy_time = min(eenv->pd_cap, busy_time + eenv->task_busy_time);
9689
9690 energy = em_cpu_energy(pd->em_pd, max_util, busy_time, eenv->cpu_cap);
9691
9692 trace_sched_compute_energy_tp(p, dst_cpu, energy, max_util, busy_time);
9693
9694 return energy;
9695 }
9696
9697 /*
9698 * find_energy_efficient_cpu(): Find most energy-efficient target CPU for the
9699 * waking task. find_energy_efficient_cpu() looks for the CPU with maximum
9700 * spare capacity in each performance domain and uses it as a potential
9701 * candidate to execute the task. Then, it uses the Energy Model to figure
9702 * out which of the CPU candidates is the most energy-efficient.
9703 *
9704 * The rationale for this heuristic is as follows. In a performance domain,
9705 * all the most energy efficient CPU candidates (according to the Energy
9706 * Model) are those for which we'll request a low frequency. When there are
9707 * several CPUs for which the frequency request will be the same, we don't
9708 * have enough data to break the tie between them, because the Energy Model
9709 * only includes active power costs. With this model, if we assume that
9710 * frequency requests follow utilization (e.g. using schedutil), the CPU with
9711 * the maximum spare capacity in a performance domain is guaranteed to be among
9712 * the best candidates of the performance domain.
9713 *
9714 * In practice, it could be preferable from an energy standpoint to pack
9715 * small tasks on a CPU in order to let other CPUs go in deeper idle states,
9716 * but that could also hurt our chances to go cluster idle, and we have no
9717 * ways to tell with the current Energy Model if this is actually a good
9718 * idea or not. So, find_energy_efficient_cpu() basically favors
9719 * cluster-packing, and spreading inside a cluster. That should at least be
9720 * a good thing for latency, and this is consistent with the idea that most
9721 * of the energy savings of EAS come from the asymmetry of the system, and
9722 * not so much from breaking the tie between identical CPUs. That's also the
9723 * reason why EAS is enabled in the topology code only for systems where
9724 * SD_ASYM_CPUCAPACITY is set.
9725 *
9726 * NOTE: Forkees are not accepted in the energy-aware wake-up path because
9727 * they don't have any useful utilization data yet and it's not possible to
9728 * forecast their impact on energy consumption. Consequently, they will be
9729 * placed by sched_balance_find_dst_cpu() on the least loaded CPU, which might turn out
9730 * to be energy-inefficient in some use-cases. The alternative would be to
9731 * bias new tasks towards specific types of CPUs first, or to try to infer
9732 * their util_avg from the parent task, but those heuristics could hurt
9733 * other use-cases too. So, until someone finds a better way to solve this,
9734 * let's keep things simple by re-using the existing slow path.
9735 */
9736 static int find_energy_efficient_cpu(struct task_struct *p, int prev_cpu)
9737 {
9738 struct cpumask *cpus = this_cpu_cpumask_var_ptr(select_rq_mask);
9739 unsigned long prev_delta = ULONG_MAX, best_delta = ULONG_MAX;
9740 unsigned long p_util_min = uclamp_is_used() ? uclamp_eff_value(p, UCLAMP_MIN) : 0;
9741 unsigned long p_util_max = uclamp_is_used() ? uclamp_eff_value(p, UCLAMP_MAX) : 1024;
9742 struct root_domain *rd = this_rq()->rd;
9743 int cpu, best_energy_cpu, target = -1;
9744 int prev_fits = -1, best_fits = -1;
9745 unsigned long best_actual_cap = 0;
9746 unsigned long prev_actual_cap = 0;
9747 struct sched_domain *sd;
9748 struct perf_domain *pd;
9749 struct energy_env eenv;
9750
9751 pd = rcu_dereference_all(rd->pd);
9752 if (!pd)
9753 return target;
9754
9755 /*
9756 * Energy-aware wake-up happens on the lowest sched_domain starting
9757 * from sd_asym_cpucapacity spanning over this_cpu and prev_cpu.
9758 */
9759 sd = rcu_dereference_all(*this_cpu_ptr(&sd_asym_cpucapacity));
9760 while (sd && !cpumask_test_cpu(prev_cpu, sched_domain_span(sd)))
9761 sd = sd->parent;
9762 if (!sd)
9763 return target;
9764
9765 target = prev_cpu;
9766
9767 sync_entity_load_avg(&p->se);
9768 if (!task_util_est(p) && p_util_min == 0)
9769 return target;
9770
9771 eenv_task_busy_time(&eenv, p, prev_cpu);
9772
9773 for (; pd; pd = pd->next) {
9774 unsigned long util_min = p_util_min, util_max = p_util_max;
9775 unsigned long cpu_cap, cpu_actual_cap, util;
9776 long prev_spare_cap = -1, max_spare_cap = -1;
9777 unsigned long rq_util_min, rq_util_max;
9778 unsigned long cur_delta, base_energy;
9779 int max_spare_cap_cpu = -1;
9780 int fits, max_fits = -1;
9781
9782 if (!cpumask_and(cpus, perf_domain_span(pd), cpu_online_mask))
9783 continue;
9784
9785 /* Account external pressure for the energy estimation */
9786 cpu = cpumask_first(cpus);
9787 cpu_actual_cap = get_actual_cpu_capacity(cpu);
9788
9789 eenv.cpu_cap = cpu_actual_cap;
9790 eenv.pd_cap = 0;
9791
9792 for_each_cpu(cpu, cpus) {
9793 struct rq *rq = cpu_rq(cpu);
9794
9795 eenv.pd_cap += cpu_actual_cap;
9796
9797 if (!cpumask_test_cpu(cpu, sched_domain_span(sd)))
9798 continue;
9799
9800 if (!cpumask_test_cpu(cpu, p->cpus_ptr))
9801 continue;
9802
9803 util = cpu_util(cpu, p, cpu, 0);
9804 cpu_cap = capacity_of(cpu);
9805
9806 /*
9807 * Skip CPUs that cannot satisfy the capacity request.
9808 * IOW, placing the task there would make the CPU
9809 * overutilized. Take uclamp into account to see how
9810 * much capacity we can get out of the CPU; this is
9811 * aligned with sched_cpu_util().
9812 */
9813 if (uclamp_is_used() && !uclamp_rq_is_idle(rq)) {
9814 /*
9815 * Open code uclamp_rq_util_with() except for
9816 * the clamp() part. I.e.: apply max aggregation
9817 * only. util_fits_cpu() logic requires to
9818 * operate on non clamped util but must use the
9819 * max-aggregated uclamp_{min, max}.
9820 */
9821 rq_util_min = uclamp_rq_get(rq, UCLAMP_MIN);
9822 rq_util_max = uclamp_rq_get(rq, UCLAMP_MAX);
9823
9824 util_min = max(rq_util_min, p_util_min);
9825 util_max = max(rq_util_max, p_util_max);
9826 }
9827
9828 fits = util_fits_cpu(util, util_min, util_max, cpu);
9829 if (!fits)
9830 continue;
9831
9832 lsub_positive(&cpu_cap, util);
9833
9834 if (cpu == prev_cpu) {
9835 /* Always use prev_cpu as a candidate. */
9836 prev_spare_cap = cpu_cap;
9837 prev_fits = fits;
9838 } else if ((fits > max_fits) ||
9839 ((fits == max_fits) && ((long)cpu_cap > max_spare_cap))) {
9840 /*
9841 * Find the CPU with the maximum spare capacity
9842 * among the remaining CPUs in the performance
9843 * domain.
9844 */
9845 max_spare_cap = cpu_cap;
9846 max_spare_cap_cpu = cpu;
9847 max_fits = fits;
9848 }
9849 }
9850
9851 if (max_spare_cap_cpu < 0 && prev_spare_cap < 0)
9852 continue;
9853
9854 eenv_pd_busy_time(&eenv, cpus, p);
9855 /* Compute the 'base' energy of the pd, without @p */
9856 base_energy = compute_energy(&eenv, pd, cpus, p, -1);
9857
9858 /* Evaluate the energy impact of using prev_cpu. */
9859 if (prev_spare_cap > -1) {
9860 prev_delta = compute_energy(&eenv, pd, cpus, p,
9861 prev_cpu);
9862 /* CPU utilization has changed */
9863 if (prev_delta < base_energy)
9864 return target;
9865 prev_delta -= base_energy;
9866 prev_actual_cap = cpu_actual_cap;
9867 best_delta = min(best_delta, prev_delta);
9868 }
9869
9870 /* Evaluate the energy impact of using max_spare_cap_cpu. */
9871 if (max_spare_cap_cpu >= 0 && max_spare_cap > prev_spare_cap) {
9872 /* Current best energy cpu fits better */
9873 if (max_fits < best_fits)
9874 continue;
9875
9876 /*
9877 * Both don't fit performance hint (i.e. uclamp_min)
9878 * but best energy cpu has better capacity.
9879 */
9880 if ((max_fits < 0) &&
9881 (cpu_actual_cap <= best_actual_cap))
9882 continue;
9883
9884 cur_delta = compute_energy(&eenv, pd, cpus, p,
9885 max_spare_cap_cpu);
9886 /* CPU utilization has changed */
9887 if (cur_delta < base_energy)
9888 return target;
9889 cur_delta -= base_energy;
9890
9891 /*
9892 * Both fit for the task but best energy cpu has lower
9893 * energy impact.
9894 */
9895 if ((max_fits > 0) && (best_fits > 0) &&
9896 (cur_delta >= best_delta))
9897 continue;
9898
9899 best_delta = cur_delta;
9900 best_energy_cpu = max_spare_cap_cpu;
9901 best_fits = max_fits;
9902 best_actual_cap = cpu_actual_cap;
9903 }
9904 }
9905
9906 if ((best_fits > prev_fits) ||
9907 ((best_fits > 0) && (best_delta < prev_delta)) ||
9908 ((best_fits < 0) && (best_actual_cap > prev_actual_cap)))
9909 target = best_energy_cpu;
9910
9911 return target;
9912 }
9913
9914 /*
9915 * select_task_rq_fair: Select target runqueue for the waking task in domains
9916 * that have the relevant SD flag set. In practice, this is SD_BALANCE_WAKE,
9917 * SD_BALANCE_FORK, or SD_BALANCE_EXEC.
9918 *
9919 * Balances load by selecting the idlest CPU in the idlest group, or under
9920 * certain conditions an idle sibling CPU if the domain has SD_WAKE_AFFINE set.
9921 *
9922 * Returns the target CPU number.
9923 */
9924 static int
9925 select_task_rq_fair(struct task_struct *p, int prev_cpu, int wake_flags)
9926 {
9927 int sync = (wake_flags & WF_SYNC) && !(current->flags & PF_EXITING);
9928 struct sched_domain *tmp, *sd = NULL;
9929 int cpu = smp_processor_id();
9930 int new_cpu = prev_cpu;
9931 int want_affine = 0;
9932 /* SD_flags and WF_flags share the first nibble */
9933 int sd_flag = wake_flags & 0xF;
9934
9935 /*
9936 * required for stable ->cpus_allowed
9937 */
9938 lockdep_assert_held(&p->pi_lock);
9939 if (wake_flags & WF_TTWU) {
9940 record_wakee(p);
9941
9942 if ((wake_flags & WF_CURRENT_CPU) &&
9943 cpumask_test_cpu(cpu, p->cpus_ptr))
9944 return cpu;
9945
9946 if (!is_rd_overutilized(this_rq()->rd)) {
9947 new_cpu = find_energy_efficient_cpu(p, prev_cpu);
9948 if (new_cpu >= 0)
9949 return new_cpu;
9950 new_cpu = prev_cpu;
9951 }
9952
9953 want_affine = !wake_wide(p) && cpumask_test_cpu(cpu, p->cpus_ptr);
9954 }
9955
9956 for_each_domain(cpu, tmp) {
9957 /*
9958 * If both 'cpu' and 'prev_cpu' are part of this domain,
9959 * cpu is a valid SD_WAKE_AFFINE target.
9960 */
9961 if (want_affine && (tmp->flags & SD_WAKE_AFFINE) &&
9962 cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) {
9963 if (cpu != prev_cpu)
9964 new_cpu = wake_affine(tmp, p, cpu, prev_cpu, sync);
9965
9966 sd = NULL; /* Prefer wake_affine over balance flags */
9967 break;
9968 }
9969
9970 /*
9971 * Usually only true for WF_EXEC and WF_FORK, as sched_domains
9972 * usually do not have SD_BALANCE_WAKE set. That means wakeup
9973 * will usually go to the fast path.
9974 */
9975 if (tmp->flags & sd_flag)
9976 sd = tmp;
9977 else if (!want_affine)
9978 break;
9979 }
9980
9981 /* Slow path */
9982 if (unlikely(sd))
9983 return sched_balance_find_dst_cpu(sd, p, cpu, prev_cpu, sd_flag);
9984
9985 /* Fast path */
9986 if (wake_flags & WF_TTWU)
9987 return select_idle_sibling(p, prev_cpu, new_cpu);
9988
9989 return new_cpu;
9990 }
9991
9992 /*
9993 * Called immediately before a task is migrated to a new CPU; task_cpu(p) and
9994 * cfs_rq_of(p) references at time of call are still valid and identify the
9995 * previous CPU. The caller guarantees p->pi_lock or task_rq(p)->lock is held.
9996 */
9997 static void migrate_task_rq_fair(struct task_struct *p, int new_cpu)
9998 {
9999 struct sched_entity *se = &p->se;
10000
10001 if (!task_on_rq_migrating(p)) {
10002 remove_entity_load_avg(se);
10003
10004 /*
10005 * Here, the task's PELT values have been updated according to
10006 * the current rq's clock. But if that clock hasn't been
10007 * updated in a while, a substantial idle time will be missed,
10008 * leading to an inflation after wake-up on the new rq.
10009 *
10010 * Estimate the missing time from the cfs_rq last_update_time
10011 * and update sched_avg to improve the PELT continuity after
10012 * migration.
10013 */
10014 migrate_se_pelt_lag(se);
10015 }
10016
10017 /* Tell new CPU we are migrated */
10018 se->avg.last_update_time = 0;
10019
10020 update_scan_period(p, new_cpu);
10021 }
10022
10023 static void task_dead_fair(struct task_struct *p)
10024 {
10025 struct sched_entity *se = &p->se;
10026 remove_entity_load_avg(se);
10027 }
10028
10029 /*
10030 * Set the max capacity the task is allowed to run at for misfit detection.
10031 */
10032 static void set_task_max_allowed_capacity(struct task_struct *p)
10033 {
10034 struct asym_cap_data *entry;
10035
10036 if (!sched_asym_cpucap_active())
10037 return;
10038
10039 rcu_read_lock();
10040 list_for_each_entry_rcu(entry, &asym_cap_list, link) {
10041 cpumask_t *cpumask;
10042
10043 cpumask = cpu_capacity_span(entry);
10044 if (!cpumask_intersects(p->cpus_ptr, cpumask))
10045 continue;
10046
10047 p->max_allowed_capacity = entry->capacity;
10048 break;
10049 }
10050 rcu_read_unlock();
10051 }
10052
10053 static void set_cpus_allowed_fair(struct task_struct *p, struct affinity_context *ctx)
10054 {
10055 set_cpus_allowed_common(p, ctx);
10056 set_task_max_allowed_capacity(p);
10057 }
10058
10059 enum preempt_wakeup_action {
10060 PREEMPT_WAKEUP_NONE, /* No preemption. */
10061 PREEMPT_WAKEUP_SHORT, /* Ignore slice protection. */
10062 PREEMPT_WAKEUP_PICK, /* Let pick_eevdf() decide. */
10063 PREEMPT_WAKEUP_RESCHED, /* Force reschedule. */
10064 };
10065
10066 static inline bool set_preempt_buddy(struct cfs_rq *cfs_rq, struct sched_entity *pse)
10067 {
10068 /*
10069 * Keep existing buddy if the deadline is sooner than pse.
10070 * The older buddy may be cache cold and completely unrelated
10071 * to the current wakeup but that is unpredictable where as
10072 * obeying the deadline is more in line with EEVDF objectives.
10073 */
10074 if (cfs_rq->next && entity_before(cfs_rq->next, pse))
10075 return false;
10076
10077 set_next_buddy(cfs_rq, pse);
10078 return true;
10079 }
10080
10081 static inline bool set_short_buddy(struct cfs_rq *cfs_rq, struct sched_entity *pse)
10082 {
10083 if (cfs_rq->next && cfs_rq->next->slice < pse->slice)
10084 return false;
10085
10086 set_next_buddy(cfs_rq, pse);
10087 return true;
10088 }
10089
10090 /*
10091 * WF_SYNC|WF_TTWU indicates the waker expects to sleep but it is not
10092 * strictly enforced because the hint is either misunderstood or
10093 * multiple tasks must be woken up.
10094 */
10095 static inline enum preempt_wakeup_action
10096 preempt_sync(struct rq *rq, int wake_flags,
10097 struct sched_entity *pse, struct sched_entity *se)
10098 {
10099 u64 threshold, delta;
10100
10101 /*
10102 * WF_SYNC without WF_TTWU is not expected so warn if it happens even
10103 * though it is likely harmless.
10104 */
10105 WARN_ON_ONCE(!(wake_flags & WF_TTWU));
10106
10107 threshold = sysctl_sched_migration_cost;
10108 delta = rq_clock_task(rq) - se->exec_start;
10109 if ((s64)delta < 0)
10110 delta = 0;
10111
10112 /*
10113 * WF_RQ_SELECTED implies the tasks are stacking on a CPU when they
10114 * could run on other CPUs. Reduce the threshold before preemption is
10115 * allowed to an arbitrary lower value as it is more likely (but not
10116 * guaranteed) the waker requires the wakee to finish.
10117 */
10118 if (wake_flags & WF_RQ_SELECTED)
10119 threshold >>= 2;
10120
10121 /*
10122 * As WF_SYNC is not strictly obeyed, allow some runtime for batch
10123 * wakeups to be issued.
10124 */
10125 if (entity_before(pse, se) && delta >= threshold)
10126 return PREEMPT_WAKEUP_RESCHED;
10127
10128 return PREEMPT_WAKEUP_NONE;
10129 }
10130
10131 /*
10132 * Preempt the current task with a newly woken task if needed:
10133 */
10134 static void wakeup_preempt_fair(struct rq *rq, struct task_struct *p, int wake_flags)
10135 {
10136 enum preempt_wakeup_action preempt_action = PREEMPT_WAKEUP_PICK;
10137 struct task_struct *donor = rq->donor;
10138 struct sched_entity *nse, *se = &donor->se, *pse = &p->se;
10139 struct cfs_rq *cfs_rq = &rq->cfs;
10140 int cse_is_idle, pse_is_idle;
10141
10142 /*
10143 * XXX Getting preempted by higher class, try and find idle CPU?
10144 */
10145 if (p->sched_class != &fair_sched_class ||
10146 donor->sched_class != &fair_sched_class)
10147 return;
10148
10149 if (unlikely(se == pse))
10150 return;
10151
10152 /*
10153 * This is possible from callers such as attach_tasks(), in which we
10154 * unconditionally wakeup_preempt() after an enqueue (which may have
10155 * lead to a throttle). This both saves work and prevents false
10156 * next-buddy nomination below.
10157 */
10158 if (task_is_throttled(p))
10159 return;
10160
10161 /*
10162 * We can come here with TIF_NEED_RESCHED already set from new task
10163 * wake up path.
10164 *
10165 * Note: this also catches the edge-case of curr being in a throttled
10166 * group (e.g. via set_curr_task), since update_curr() (in the
10167 * enqueue of curr) will have resulted in resched being set. This
10168 * prevents us from potentially nominating it as a false LAST_BUDDY
10169 * below.
10170 */
10171 if (!sched_feat(PREEMPT_SHORT) && test_tsk_need_resched(rq->curr))
10172 return;
10173
10174 if (!sched_feat(WAKEUP_PREEMPTION))
10175 return;
10176
10177 WARN_ON_ONCE(!pse);
10178
10179 cse_is_idle = se_is_idle(se);
10180 pse_is_idle = se_is_idle(pse);
10181
10182 nse = se;
10183 /*
10184 * Preempt an idle entity in favor of a non-idle entity (and don't preempt
10185 * in the inverse case).
10186 */
10187 if (cse_is_idle && !pse_is_idle)
10188 goto preempt;
10189
10190 update_curr_fair(rq);
10191
10192 if (cse_is_idle != pse_is_idle)
10193 goto update;
10194
10195 /*
10196 * BATCH and IDLE tasks do not preempt others.
10197 */
10198 if (unlikely(!normal_policy(p->policy)))
10199 goto update;
10200
10201 /*
10202 * Do not preempt for tasks that are sched_delayed as it would violate
10203 * EEVDF to forcibly queue an ineligible task.
10204 */
10205 if (pse->sched_delayed)
10206 goto update;
10207
10208 /*
10209 * If @p has a shorter slice than current and @p is eligible, override
10210 * current's slice protection in order to allow preemption.
10211 */
10212 if (sched_feat(PREEMPT_SHORT) && (pse->slice < se->slice)) {
10213 preempt_action = PREEMPT_WAKEUP_SHORT;
10214 goto pick;
10215 }
10216
10217 /*
10218 * Ignore wakee preemption on WF_FORK as it is less likely that
10219 * there is shared data as exec often follow fork.
10220 */
10221 if (wake_flags & WF_FORK)
10222 goto update;
10223
10224 /* Prefer picking wakee soon if appropriate. */
10225 if (sched_feat(NEXT_BUDDY) && set_preempt_buddy(cfs_rq, pse)) {
10226 /*
10227 * Decide whether to obey WF_SYNC hint for a new buddy. Old
10228 * buddies are ignored as they may not be relevant to the
10229 * waker and less likely to be cache hot.
10230 */
10231 if (wake_flags & WF_SYNC)
10232 preempt_action = preempt_sync(rq, wake_flags, pse, se);
10233 }
10234
10235 switch (preempt_action) {
10236 case PREEMPT_WAKEUP_NONE:
10237 return;
10238 case PREEMPT_WAKEUP_RESCHED:
10239 goto preempt;
10240 case PREEMPT_WAKEUP_SHORT:
10241 fallthrough;
10242 case PREEMPT_WAKEUP_PICK:
10243 break;
10244 }
10245
10246 pick:
10247 if (cfs_rq->h_nr_queued) {
10248 nse = pick_next_entity(rq, preempt_action != PREEMPT_WAKEUP_SHORT);
10249 if (unlikely(!nse))
10250 goto pick;
10251
10252 /* If @p has become the most eligible task, force preemption */
10253 if (nse == pse)
10254 goto preempt;
10255 }
10256
10257 /*
10258 * If @p is eligible but not the next task to run then cancel protection
10259 * to prevent large scheduling latency
10260 */
10261 if (preempt_action == PREEMPT_WAKEUP_SHORT && entity_eligible(cfs_rq, pse))
10262 goto preempt;
10263 update:
10264 if (sched_feat(RUN_TO_PARITY))
10265 update_protect_slice(cfs_rq, se);
10266
10267 return;
10268
10269 preempt:
10270 cancel_protect_slice(se);
10271
10272 if (preempt_action == PREEMPT_WAKEUP_SHORT)
10273 set_short_buddy(cfs_rq, pse);
10274
10275 resched_curr_lazy(rq);
10276 }
10277
10278 struct task_struct *pick_task_fair(struct rq *rq, struct rq_flags *rf)
10279 __must_hold(__rq_lockp(rq))
10280 {
10281 struct cfs_rq *cfs_rq = &rq->cfs;
10282 struct sched_entity *se;
10283 struct task_struct *p;
10284 int new_tasks;
10285
10286 again:
10287 if (!cfs_rq->h_nr_queued)
10288 goto idle;
10289
10290 /* Might not have done put_prev_entity() */
10291 if (cfs_rq->curr && cfs_rq->curr->on_rq)
10292 update_curr_eevdf(cfs_rq);
10293
10294 se = pick_next_entity(rq, true);
10295 if (!se)
10296 goto again;
10297
10298 p = task_of(se);
10299 return p;
10300
10301 idle:
10302 if (sched_core_enabled(rq))
10303 return NULL;
10304
10305 new_tasks = sched_balance_newidle(rq, rf);
10306 if (new_tasks < 0)
10307 return RETRY_TASK;
10308 if (new_tasks > 0)
10309 goto again;
10310 return NULL;
10311 }
10312
10313 static struct task_struct *
10314 fair_server_pick_task(struct sched_dl_entity *dl_se, struct rq_flags *rf)
10315 __must_hold(__rq_lockp(dl_se->rq))
10316 {
10317 return pick_task_fair(dl_se->rq, rf);
10318 }
10319
10320 void fair_server_init(struct rq *rq)
10321 {
10322 struct sched_dl_entity *dl_se = &rq->fair_server;
10323
10324 init_dl_entity(dl_se);
10325
10326 dl_server_init(dl_se, rq, fair_server_pick_task);
10327 }
10328
10329 /*
10330 * Account for a descheduled task:
10331 */
10332 static void put_prev_task_fair(struct rq *rq, struct task_struct *prev, struct task_struct *next)
10333 {
10334 struct sched_entity *se = &prev->se;
10335 struct cfs_rq *cfs_rq = &rq->cfs;
10336 struct sched_entity *nse = NULL;
10337
10338 #ifdef CONFIG_FAIR_GROUP_SCHED
10339 if (next && next->sched_class == &fair_sched_class)
10340 nse = &next->se;
10341 #endif
10342
10343 while (se) {
10344 cfs_rq = cfs_rq_of(se);
10345 if (!nse || cfs_rq->h_curr)
10346 put_prev_entity(cfs_rq, se);
10347 #ifdef CONFIG_FAIR_GROUP_SCHED
10348 if (nse) {
10349 if (is_same_group(se, nse))
10350 break;
10351
10352 int d = nse->depth - se->depth;
10353 if (d >= 0) {
10354 /* nse has equal or greater depth, ascend */
10355 nse = parent_entity(nse);
10356 /* if nse is the deeper, do not ascend se */
10357 if (d > 0)
10358 continue;
10359 }
10360 }
10361 #endif
10362 se = parent_entity(se);
10363 }
10364
10365 /* Put 'current' back into the tree. */
10366 cfs_rq = &rq->cfs;
10367 se = &prev->se;
10368 WARN_ON_ONCE(cfs_rq->curr != se);
10369 cfs_rq->curr = NULL;
10370 if (se->on_rq)
10371 __enqueue_entity(cfs_rq, se);
10372 }
10373
10374 /*
10375 * sched_yield() is very simple
10376 */
10377 static void yield_task_fair(struct rq *rq)
10378 {
10379 struct task_struct *curr = rq->donor;
10380 struct sched_entity *se = &curr->se;
10381 struct cfs_rq *cfs_rq = &rq->cfs;
10382
10383 /*
10384 * Are we the only task in the tree?
10385 */
10386 if (unlikely(rq->nr_running == 1))
10387 return;
10388
10389 clear_buddies(cfs_rq, se);
10390
10391 update_rq_clock(rq);
10392 /*
10393 * Update run-time statistics of the 'current'.
10394 */
10395 update_curr_eevdf(cfs_rq);
10396 /*
10397 * Tell update_rq_clock() that we've just updated,
10398 * so we don't do microscopic update in schedule()
10399 * and double the fastpath cost.
10400 */
10401 rq_clock_skip_update(rq);
10402
10403 /*
10404 * Forfeit the remaining vruntime, only if the entity is eligible. This
10405 * condition is necessary because in core scheduling we prefer to run
10406 * ineligible tasks rather than force idling. If this happens we may
10407 * end up in a loop where the core scheduler picks the yielding task,
10408 * which yields immediately again; without the condition the vruntime
10409 * ends up quickly running away.
10410 */
10411 if (entity_eligible(cfs_rq, se)) {
10412 se->vruntime = se->deadline;
10413 update_deadline(cfs_rq, se);
10414 }
10415 }
10416
10417 static bool yield_to_task_fair(struct rq *rq, struct task_struct *p)
10418 {
10419 struct sched_entity *se = &p->se;
10420
10421 /* !se->on_rq also covers throttled task */
10422 if (!se->on_rq || se->sched_delayed)
10423 return false;
10424
10425 /* Tell the scheduler that we'd really like se to run next. */
10426 set_next_buddy(&task_rq(p)->cfs, se);
10427
10428 yield_task_fair(rq);
10429
10430 return true;
10431 }
10432
10433 /**************************************************
10434 * Fair scheduling class load-balancing methods.
10435 *
10436 * BASICS
10437 *
10438 * The purpose of load-balancing is to achieve the same basic fairness the
10439 * per-CPU scheduler provides, namely provide a proportional amount of compute
10440 * time to each task. This is expressed in the following equation:
10441 *
10442 * W_i,n/P_i == W_j,n/P_j for all i,j (1)
10443 *
10444 * Where W_i,n is the n-th weight average for CPU i. The instantaneous weight
10445 * W_i,0 is defined as:
10446 *
10447 * W_i,0 = \Sum_j w_i,j (2)
10448 *
10449 * Where w_i,j is the weight of the j-th runnable task on CPU i. This weight
10450 * is derived from the nice value as per sched_prio_to_weight[].
10451 *
10452 * The weight average is an exponential decay average of the instantaneous
10453 * weight:
10454 *
10455 * W'_i,n = (2^n - 1) / 2^n * W_i,n + 1 / 2^n * W_i,0 (3)
10456 *
10457 * C_i is the compute capacity of CPU i, typically it is the
10458 * fraction of 'recent' time available for SCHED_OTHER task execution. But it
10459 * can also include other factors [XXX].
10460 *
10461 * To achieve this balance we define a measure of imbalance which follows
10462 * directly from (1):
10463 *
10464 * imb_i,j = max{ avg(W/C), W_i/C_i } - min{ avg(W/C), W_j/C_j } (4)
10465 *
10466 * We them move tasks around to minimize the imbalance. In the continuous
10467 * function space it is obvious this converges, in the discrete case we get
10468 * a few fun cases generally called infeasible weight scenarios.
10469 *
10470 * [XXX expand on:
10471 * - infeasible weights;
10472 * - local vs global optima in the discrete case. ]
10473 *
10474 *
10475 * SCHED DOMAINS
10476 *
10477 * In order to solve the imbalance equation (4), and avoid the obvious O(n^2)
10478 * for all i,j solution, we create a tree of CPUs that follows the hardware
10479 * topology where each level pairs two lower groups (or better). This results
10480 * in O(log n) layers. Furthermore we reduce the number of CPUs going up the
10481 * tree to only the first of the previous level and we decrease the frequency
10482 * of load-balance at each level inversely proportional to the number of CPUs in
10483 * the groups.
10484 *
10485 * This yields:
10486 *
10487 * log_2 n 1 n
10488 * \Sum { --- * --- * 2^i } = O(n) (5)
10489 * i = 0 2^i 2^i
10490 * `- size of each group
10491 * | | `- number of CPUs doing load-balance
10492 * | `- freq
10493 * `- sum over all levels
10494 *
10495 * Coupled with a limit on how many tasks we can migrate every balance pass,
10496 * this makes (5) the runtime complexity of the balancer.
10497 *
10498 * An important property here is that each CPU is still (indirectly) connected
10499 * to every other CPU in at most O(log n) steps:
10500 *
10501 * The adjacency matrix of the resulting graph is given by:
10502 *
10503 * log_2 n
10504 * A_i,j = \Union (i % 2^k == 0) && i / 2^(k+1) == j / 2^(k+1) (6)
10505 * k = 0
10506 *
10507 * And you'll find that:
10508 *
10509 * A^(log_2 n)_i,j != 0 for all i,j (7)
10510 *
10511 * Showing there's indeed a path between every CPU in at most O(log n) steps.
10512 * The task movement gives a factor of O(m), giving a convergence complexity
10513 * of:
10514 *
10515 * O(nm log n), n := nr_cpus, m := nr_tasks (8)
10516 *
10517 *
10518 * WORK CONSERVING
10519 *
10520 * In order to avoid CPUs going idle while there's still work to do, new idle
10521 * balancing is more aggressive and has the newly idle CPU iterate up the domain
10522 * tree itself instead of relying on other CPUs to bring it work.
10523 *
10524 * This adds some complexity to both (5) and (8) but it reduces the total idle
10525 * time.
10526 *
10527 * [XXX more?]
10528 *
10529 *
10530 * CGROUPS
10531 *
10532 * Cgroups make a horror show out of (2), instead of a simple sum we get:
10533 *
10534 * s_k,i
10535 * W_i,0 = \Sum_j \Prod_k w_k * ----- (9)
10536 * S_k
10537 *
10538 * Where
10539 *
10540 * s_k,i = \Sum_j w_i,j,k and S_k = \Sum_i s_k,i (10)
10541 *
10542 * w_i,j,k is the weight of the j-th runnable task in the k-th cgroup on CPU i.
10543 *
10544 * The big problem is S_k, its a global sum needed to compute a local (W_i)
10545 * property.
10546 *
10547 * [XXX write more on how we solve this.. _after_ merging pjt's patches that
10548 * rewrite all of this once again.]
10549 */
10550
10551 static unsigned long __read_mostly max_load_balance_interval = HZ/10;
10552
10553 enum fbq_type { regular, remote, all };
10554
10555 /*
10556 * 'group_type' describes the group of CPUs at the moment of load balancing.
10557 *
10558 * The enum is ordered by pulling priority, with the group with lowest priority
10559 * first so the group_type can simply be compared when selecting the busiest
10560 * group. See update_sd_pick_busiest().
10561 */
10562 enum group_type {
10563 /* The group has spare capacity that can be used to run more tasks. */
10564 group_has_spare = 0,
10565 /*
10566 * The group is fully used and the tasks don't compete for more CPU
10567 * cycles. Nevertheless, some tasks might wait before running.
10568 */
10569 group_fully_busy,
10570 /*
10571 * One task doesn't fit with CPU's capacity and must be migrated to a
10572 * more powerful CPU.
10573 */
10574 group_misfit_task,
10575 /*
10576 * Balance SMT group that's fully busy. Can benefit from migration
10577 * a task on SMT with busy sibling to another CPU on idle core.
10578 */
10579 group_smt_balance,
10580 /*
10581 * SD_ASYM_PACKING only: One local CPU with higher capacity is available,
10582 * and the task should be migrated to it instead of running on the
10583 * current CPU.
10584 */
10585 group_asym_packing,
10586 /*
10587 * The tasks' affinity constraints previously prevented the scheduler
10588 * from balancing the load across the system.
10589 */
10590 group_imbalanced,
10591 /*
10592 * There are tasks running on non-preferred LLC, possible to move
10593 * them to their preferred LLC without creating too much imbalance.
10594 * The priority of group_llc_balance is lower than that of
10595 * group_overloaded and higher than that of all other group types.
10596 * This is because group_llc_balance may exacerbate load imbalance.
10597 * If the LLC balancing attempt fails, the nr_balance_failed
10598 * mechanism will trigger other group types to rebalance the load.
10599 */
10600 group_llc_balance,
10601 /*
10602 * The CPU is overloaded and can't provide expected CPU cycles to all
10603 * tasks.
10604 */
10605 group_overloaded
10606 };
10607
10608 enum migration_type {
10609 migrate_load = 0,
10610 migrate_util,
10611 migrate_task,
10612 migrate_misfit,
10613 migrate_llc_task
10614 };
10615
10616 #define LBF_ALL_PINNED 0x01
10617 #define LBF_NEED_BREAK 0x02
10618 #define LBF_DST_PINNED 0x04
10619 #define LBF_SOME_PINNED 0x08
10620 #define LBF_ACTIVE_LB 0x10
10621 #define LBF_LLC_PINNED 0x20
10622 #define LBF_ACTIVE_LB_LLC 0x40
10623
10624 struct lb_env {
10625 struct sched_domain *sd;
10626
10627 struct rq *src_rq;
10628 int src_cpu;
10629
10630 int dst_cpu;
10631 struct rq *dst_rq;
10632 bool dst_core_idle;
10633
10634 struct cpumask *dst_grpmask;
10635 int new_dst_cpu;
10636 enum cpu_idle_type idle;
10637 long imbalance;
10638 /* The set of CPUs under consideration for load-balancing */
10639 struct cpumask *cpus;
10640
10641 unsigned int flags;
10642
10643 unsigned int loop;
10644 unsigned int loop_break;
10645 unsigned int loop_max;
10646
10647 enum fbq_type fbq_type;
10648 enum migration_type migration_type;
10649 struct list_head tasks;
10650 };
10651
10652 /*
10653 * Is this task likely cache-hot:
10654 */
10655 static int task_hot(struct task_struct *p, struct lb_env *env)
10656 {
10657 s64 delta;
10658
10659 lockdep_assert_rq_held(env->src_rq);
10660
10661 if (p->sched_class != &fair_sched_class)
10662 return 0;
10663
10664 if (unlikely(task_has_idle_policy(p)))
10665 return 0;
10666
10667 /* SMT siblings share cache */
10668 if (env->sd->flags & SD_SHARE_CPUCAPACITY)
10669 return 0;
10670
10671 /*
10672 * Buddy candidates are cache hot:
10673 */
10674 if (sched_feat(CACHE_HOT_BUDDY) && env->dst_rq->nr_running &&
10675 (&p->se == cfs_rq_of(&p->se)->next))
10676 return 1;
10677
10678 if (sysctl_sched_migration_cost == -1)
10679 return 1;
10680
10681 /*
10682 * Don't migrate task if the task's cookie does not match
10683 * with the destination CPU's core cookie.
10684 */
10685 if (!sched_core_cookie_match(cpu_rq(env->dst_cpu), p))
10686 return 1;
10687
10688 if (sysctl_sched_migration_cost == 0)
10689 return 0;
10690
10691 delta = rq_clock_task(env->src_rq) - p->se.exec_start;
10692
10693 return delta < (s64)sysctl_sched_migration_cost;
10694 }
10695
10696 #ifdef CONFIG_NUMA_BALANCING
10697 /*
10698 * Returns a positive value, if task migration degrades locality.
10699 * Returns 0, if task migration is not affected by locality.
10700 * Returns a negative value, if task migration improves locality i.e migration preferred.
10701 */
10702 static long migrate_degrades_locality(struct task_struct *p, struct lb_env *env)
10703 {
10704 struct numa_group *numa_group = rcu_dereference_all(p->numa_group);
10705 unsigned long src_weight, dst_weight;
10706 int src_nid, dst_nid, dist;
10707
10708 if (!static_branch_likely(&sched_numa_balancing))
10709 return 0;
10710
10711 if (!p->numa_faults || !(env->sd->flags & SD_NUMA))
10712 return 0;
10713
10714 src_nid = cpu_to_node(env->src_cpu);
10715 dst_nid = cpu_to_node(env->dst_cpu);
10716
10717 if (src_nid == dst_nid)
10718 return 0;
10719
10720 /* Migrating away from the preferred node is always bad. */
10721 if (src_nid == p->numa_preferred_nid) {
10722 if (env->src_rq->nr_running > env->src_rq->nr_preferred_running)
10723 return 1;
10724 else
10725 return 0;
10726 }
10727
10728 /* Encourage migration to the preferred node. */
10729 if (dst_nid == p->numa_preferred_nid)
10730 return -1;
10731
10732 /* Leaving a core idle is often worse than degrading locality. */
10733 if (env->idle == CPU_IDLE)
10734 return 0;
10735
10736 dist = node_distance(src_nid, dst_nid);
10737 if (numa_group) {
10738 src_weight = group_weight(p, src_nid, dist);
10739 dst_weight = group_weight(p, dst_nid, dist);
10740 } else {
10741 src_weight = task_weight(p, src_nid, dist);
10742 dst_weight = task_weight(p, dst_nid, dist);
10743 }
10744
10745 return src_weight - dst_weight;
10746 }
10747
10748 #else /* !CONFIG_NUMA_BALANCING: */
10749 static inline long migrate_degrades_locality(struct task_struct *p,
10750 struct lb_env *env)
10751 {
10752 return 0;
10753 }
10754 #endif /* !CONFIG_NUMA_BALANCING */
10755
10756 /*
10757 * Check whether the task is ineligible on the destination cpu
10758 *
10759 * When the PLACE_LAG scheduling feature is enabled and
10760 * dst_cfs_rq->nr_queued is greater than 1, if the task
10761 * is ineligible, it will also be ineligible when
10762 * it is migrated to the destination cpu.
10763 */
10764 static inline int task_is_ineligible_on_dst_cpu(struct task_struct *p, int dest_cpu)
10765 {
10766 struct cfs_rq *dst_cfs_rq = &cpu_rq(dest_cpu)->cfs;
10767
10768 if (sched_feat(PLACE_LAG) && dst_cfs_rq->h_nr_queued &&
10769 !entity_eligible(&task_rq(p)->cfs, &p->se))
10770 return 1;
10771
10772 return 0;
10773 }
10774
10775 #ifdef CONFIG_SCHED_CACHE
10776 /*
10777 * The margin used when comparing LLC utilization with CPU capacity.
10778 * It determines the LLC load level where active LLC aggregation is
10779 * done.
10780 * Derived from fits_capacity().
10781 *
10782 * (default: ~50%, tunable via debugfs)
10783 */
10784 static bool fits_llc_capacity(unsigned long util, unsigned long max)
10785 {
10786 u32 aggr_pct = llc_overaggr_pct;
10787
10788 /*
10789 * For single core systems, raise the aggregation
10790 * threshold to accommodate more tasks.
10791 */
10792 if (cpu_smt_num_threads == 1)
10793 aggr_pct = (aggr_pct * 3 / 2);
10794
10795 return util * 100 < max * aggr_pct;
10796 }
10797
10798 /*
10799 * The margin used when comparing utilization.
10800 * is 'util1' noticeably greater than 'util2'
10801 * Derived from capacity_greater().
10802 * Bias is in perentage.
10803 */
10804 /* Allows dst util to be bigger than src util by up to bias percent */
10805 #define util_greater(util1, util2) \
10806 ((util1) * 100 > (util2) * (100 + llc_imb_pct))
10807
10808 static __maybe_unused bool get_llc_stats(int cpu, unsigned long *util,
10809 unsigned long *cap)
10810 {
10811 struct sched_domain_shared *sd_share;
10812
10813 sd_share = rcu_dereference_all(per_cpu(sd_llc_shared, cpu));
10814 if (!sd_share)
10815 return false;
10816
10817 *util = READ_ONCE(sd_share->util_avg);
10818 *cap = READ_ONCE(sd_share->capacity);
10819
10820 return true;
10821 }
10822
10823 /*
10824 * Decision matrix according to the LLC utilization. To
10825 * decide whether we can do task aggregation across LLC.
10826 *
10827 * By default, 50% is the threshold for treating the LLC
10828 * as busy. The reason for choosing 50% is to avoid saturation
10829 * of SMT-2, and it is also a safe cutoff for other SMT-n
10830 * platforms. SMT-1 has higher threshold because it is
10831 * supposed to accommodate more tasks, see fits_llc_capacity().
10832 *
10833 * 20% is the utilization imbalance percentage to decide
10834 * if the preferred LLC is busier than the non-preferred LLC.
10835 * 20 is a little higher than the LLC domain's imbalance_pct
10836 * 17. The hysteresis is used to avoid task bouncing between the
10837 * preferred LLC and the non-preferred LLC, and it will
10838 * be turned into tunable debugfs.
10839 *
10840 * 1. moving towards the preferred LLC, dst is the preferred
10841 * LLC, src is not.
10842 *
10843 * src \ dst 30% 40% 50% 60%
10844 * 30% Y Y Y N
10845 * 40% Y Y Y Y
10846 * 50% Y Y G G
10847 * 60% Y Y G G
10848 *
10849 * 2. moving out of the preferred LLC, src is the preferred
10850 * LLC, dst is not:
10851 *
10852 * src \ dst 30% 40% 50% 60%
10853 * 30% N N N N
10854 * 40% N N N N
10855 * 50% N N G G
10856 * 60% Y N G G
10857 *
10858 * src : src_util
10859 * dst : dst_util
10860 * Y : Yes, migrate
10861 * N : No, do not migrate
10862 * G : let the Generic load balance to even the load.
10863 *
10864 * The intention is that if both LLCs are quite busy, cache aware
10865 * load balance should not be performed, and generic load balance
10866 * should take effect. However, if one is busy and the other is not,
10867 * the preferred LLC capacity(50%) and imbalance criteria(20%) should
10868 * be considered to determine whether LLC aggregation should be
10869 * performed to bias the load towards the preferred LLC.
10870 */
10871
10872 /* migration decision, 3 states are orthogonal. */
10873 enum llc_mig {
10874 mig_forbid = 0, /* N: Don't migrate task, respect LLC preference */
10875 mig_llc, /* Y: Do LLC preference based migration */
10876 mig_unrestricted /* G: Don't restrict generic load balance migration */
10877 };
10878
10879 /*
10880 * Check if task can be moved from the source LLC to the
10881 * destination LLC without breaking cache aware preferrence.
10882 * src_cpu and dst_cpu are arbitrary CPUs within the source
10883 * and destination LLCs, respectively.
10884 */
10885 static enum llc_mig can_migrate_llc(int src_cpu, int dst_cpu,
10886 unsigned long tsk_util,
10887 bool to_pref)
10888 {
10889 unsigned long src_util, dst_util, src_cap, dst_cap;
10890
10891 if (!get_llc_stats(src_cpu, &src_util, &src_cap) ||
10892 !get_llc_stats(dst_cpu, &dst_util, &dst_cap))
10893 return mig_unrestricted;
10894
10895 src_util = src_util < tsk_util ? 0 : src_util - tsk_util;
10896 dst_util = dst_util + tsk_util;
10897
10898 if (!fits_llc_capacity(dst_util, dst_cap) &&
10899 !fits_llc_capacity(src_util, src_cap))
10900 return mig_unrestricted;
10901
10902 if (to_pref) {
10903 /*
10904 * Don't migrate if we will get preferred LLC too
10905 * heavily loaded and if the dest is much busier
10906 * than the src, in which case migration will
10907 * increase the imbalance too much.
10908 */
10909 if (!fits_llc_capacity(dst_util, dst_cap) &&
10910 util_greater(dst_util, src_util))
10911 return mig_forbid;
10912 } else {
10913 /*
10914 * Don't migrate if we will leave preferred LLC
10915 * too idle, or if this migration leads to the
10916 * non-preferred LLC falls within sysctl_aggr_imb percent
10917 * of preferred LLC, leading to migration again
10918 * back to preferred LLC.
10919 */
10920 if (fits_llc_capacity(src_util, src_cap) ||
10921 !util_greater(src_util, dst_util))
10922 return mig_forbid;
10923 }
10924 return mig_llc;
10925 }
10926
10927 static inline bool task_misfits_asym_cpu(struct lb_env *env, struct task_struct *p)
10928 {
10929 /*
10930 * On asymmetric CPU capacity domains, do not let cache-aware
10931 * balancing pull the task onto a destination CPU that cannot
10932 * accommodate it. Doing so would turn the task into a misfit on
10933 * the destination, trading a cache-locality gain for a capacity
10934 * loss. If the task already does not fit its source CPU, the move
10935 * cannot make things worse, so let the LLC preference decide.
10936 */
10937 if ((env->sd->flags & SD_ASYM_CPUCAPACITY) && p &&
10938 !task_fits_cpu(p, env->dst_cpu) &&
10939 task_fits_cpu(p, env->src_cpu))
10940 return true;
10941
10942 return false;
10943 }
10944
10945 /*
10946 * Check if task p can migrate from source LLC to
10947 * destination LLC in terms of cache aware load balance.
10948 */
10949 static enum llc_mig can_migrate_llc_task(struct lb_env *env,
10950 struct task_struct *p)
10951 {
10952 struct sched_cache_group *grp;
10953 bool to_pref;
10954 int cpu, src_cpu, dst_cpu;
10955
10956 if (task_misfits_asym_cpu(env, p))
10957 return mig_forbid;
10958
10959 src_cpu = env->src_cpu;
10960 dst_cpu = env->dst_cpu;
10961 grp = rcu_dereference_all(p->sched_cache_grp);
10962 if (!grp)
10963 return mig_unrestricted;
10964
10965 cpu = READ_ONCE(grp->cpu);
10966 if (cpu < 0 || cpus_share_cache(src_cpu, dst_cpu))
10967 return mig_unrestricted;
10968
10969 /* skip cache aware load balance for too many threads */
10970 if (invalid_llc_nr(grp, p, dst_cpu) ||
10971 exceed_llc_capacity(grp, dst_cpu)) {
10972 if (READ_ONCE(grp->cpu) != -1)
10973 WRITE_ONCE(grp->cpu, -1);
10974 return mig_unrestricted;
10975 }
10976
10977 if (cpus_share_cache(dst_cpu, cpu))
10978 to_pref = true;
10979 else if (cpus_share_cache(src_cpu, cpu))
10980 to_pref = false;
10981 else
10982 return mig_unrestricted;
10983
10984 return can_migrate_llc(src_cpu, dst_cpu,
10985 task_util(p), to_pref);
10986 }
10987
10988 /*
10989 * Check if active load balance breaks LLC locality in
10990 * terms of cache aware load balance. The load level and
10991 * imbalance do not warrant breaking LLC preference per
10992 * the can_migrate_llc() policy. Here, the benefit of
10993 * LLC locality outweighs the power efficiency gained from
10994 * migrating the only runnable task away.
10995 */
10996 static inline bool
10997 alb_break_llc(struct lb_env *env)
10998 {
10999 if (!sched_cache_enabled())
11000 return false;
11001
11002 if (cpus_share_cache(env->src_cpu, env->dst_cpu))
11003 return false;
11004 /*
11005 * All tasks prefer to stay on their current CPU.
11006 * Do not pull a task from its preferred CPU if:
11007 * 1. It is the only task running and does not exceed
11008 * imbalance allowance; OR
11009 * 2. Migrating it away from its preferred LLC would violate
11010 * the cache-aware scheduling policy.
11011 */
11012 if (env->src_rq->nr_pref_llc_running &&
11013 env->src_rq->nr_pref_llc_running == env->src_rq->cfs.h_nr_runnable) {
11014 unsigned long util = 0;
11015 struct task_struct *cur;
11016
11017 /*
11018 * Migrating misfit tasks from current CPU
11019 * to CPU with a better fit.
11020 * Prioritize that over LLC preference.
11021 */
11022 if (env->migration_type == migrate_misfit)
11023 return false;
11024
11025 if (env->src_rq->nr_running <= 1)
11026 return true;
11027
11028 cur = rcu_dereference_all(env->src_rq->curr);
11029 if (cur && cur->sched_class == &fair_sched_class)
11030 util = task_util(cur);
11031
11032 if (task_misfits_asym_cpu(env, cur) ||
11033 can_migrate_llc(env->src_cpu, env->dst_cpu,
11034 util, false) == mig_forbid)
11035 return true;
11036 }
11037
11038 return false;
11039 }
11040
11041 /*
11042 * Returns true if p's preferred LLC does not match the destination CPU
11043 * under migrate_llc_task semantics. Passive LB passes migrate_llc_task
11044 * in env->migration_type, while active LB carries LBF_ACTIVE_LB_LLC in
11045 * env->flags to avoid overwriting env->migration_type.
11046 */
11047 static inline bool
11048 migrate_llc_task_wrong_dst(struct task_struct *p, struct lb_env *env)
11049 {
11050 return sched_cache_enabled() &&
11051 (env->migration_type == migrate_llc_task ||
11052 env->flags & LBF_ACTIVE_LB_LLC) &&
11053 READ_ONCE(p->preferred_llc) != llc_id(env->dst_cpu);
11054 }
11055
11056 /*
11057 * Check if migrating task p from env->src_cpu to
11058 * env->dst_cpu breaks LLC localiy.
11059 */
11060 static bool migrate_degrades_llc(struct task_struct *p, struct lb_env *env)
11061 {
11062 if (!sched_cache_enabled())
11063 return false;
11064
11065 if (task_has_sched_core(p))
11066 return false;
11067 /*
11068 * Skip over tasks that would degrade LLC locality;
11069 * only when nr_balanced_failed is sufficiently high do we
11070 * ignore this constraint.
11071 *
11072 * Threshold of cache_nice_tries is set to 1 higher
11073 * than nr_balance_failed to avoid excessive task
11074 * migration at the same time.
11075 */
11076 if (env->sd->nr_balance_failed >= env->sd->cache_nice_tries + 1)
11077 return false;
11078
11079 /*
11080 * We know the env->src_cpu has some tasks prefer to
11081 * run on env->dst_cpu, skip the tasks do not prefer
11082 * env->dst_cpu, and find the one that prefers.
11083 */
11084 if (migrate_llc_task_wrong_dst(p, env))
11085 return true;
11086
11087 if (can_migrate_llc_task(env, p) != mig_forbid)
11088 return false;
11089
11090 return true;
11091 }
11092
11093 #else
11094 static inline bool get_llc_stats(int cpu, unsigned long *util,
11095 unsigned long *cap)
11096 {
11097 return false;
11098 }
11099
11100 static inline bool
11101 alb_break_llc(struct lb_env *env)
11102 {
11103 return false;
11104 }
11105
11106 static inline bool
11107 migrate_llc_task_wrong_dst(struct task_struct *p, struct lb_env *env)
11108 {
11109 return false;
11110 }
11111
11112 static inline bool
11113 migrate_degrades_llc(struct task_struct *p, struct lb_env *env)
11114 {
11115 return false;
11116 }
11117 #endif
11118 /*
11119 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
11120 */
11121 static
11122 int can_migrate_task(struct task_struct *p, struct lb_env *env)
11123 {
11124 long degrades, hot;
11125
11126 lockdep_assert_rq_held(env->src_rq);
11127 if (p->sched_task_hot)
11128 p->sched_task_hot = 0;
11129
11130 /*
11131 * We do not migrate tasks that are:
11132 * 1) delayed dequeued unless we migrate load, or
11133 * 2) target cfs_rq is in throttled hierarchy, or
11134 * 3) cannot be migrated to this CPU due to cpus_ptr, or
11135 * 4) running (obviously), or
11136 * 5) are cache-hot on their current CPU, or
11137 * 6) are blocked on mutexes (if SCHED_PROXY_EXEC is enabled)
11138 */
11139 if ((p->se.sched_delayed) && (env->migration_type != migrate_load))
11140 return 0;
11141
11142 if (lb_throttled_hierarchy(p, env->dst_cpu))
11143 return 0;
11144
11145 /*
11146 * We want to prioritize the migration of eligible tasks.
11147 * For ineligible tasks we soft-limit them and only allow
11148 * them to migrate when nr_balance_failed is non-zero to
11149 * avoid load-balancing trying very hard to balance the load.
11150 */
11151 if (!env->sd->nr_balance_failed &&
11152 task_is_ineligible_on_dst_cpu(p, env->dst_cpu))
11153 return 0;
11154
11155 /* Disregard percpu kthreads; they are where they need to be. */
11156 if (kthread_is_per_cpu(p))
11157 return 0;
11158
11159 if (task_is_blocked(p))
11160 return 0;
11161
11162 if (!cpumask_test_cpu(env->dst_cpu, p->cpus_ptr)) {
11163 int cpu;
11164
11165 schedstat_inc(p->stats.nr_failed_migrations_affine);
11166
11167 env->flags |= LBF_SOME_PINNED;
11168
11169 /*
11170 * Remember if this task can be migrated to any other CPU in
11171 * our sched_group. We may want to revisit it if we couldn't
11172 * meet load balance goals by pulling other tasks on src_cpu.
11173 *
11174 * Avoid computing new_dst_cpu
11175 * - for NEWLY_IDLE
11176 * - if we have already computed one in current iteration
11177 * - if it's an active balance
11178 */
11179 if (env->idle == CPU_NEWLY_IDLE ||
11180 env->flags & (LBF_DST_PINNED | LBF_ACTIVE_LB))
11181 return 0;
11182
11183 /* Prevent to re-select dst_cpu via env's CPUs: */
11184 cpu = cpumask_first_and_and(env->dst_grpmask, env->cpus, p->cpus_ptr);
11185
11186 if (cpu < nr_cpu_ids) {
11187 env->flags |= LBF_DST_PINNED;
11188 env->new_dst_cpu = cpu;
11189 }
11190
11191 return 0;
11192 }
11193
11194 /* Record that we found at least one task that could run on dst_cpu */
11195 env->flags &= ~LBF_ALL_PINNED;
11196
11197 if (task_on_cpu(env->src_rq, p) ||
11198 task_current_donor(env->src_rq, p)) {
11199 schedstat_inc(p->stats.nr_failed_migrations_running);
11200 return 0;
11201 }
11202
11203 /*
11204 * Aggressive migration if:
11205 * 1) active balance
11206 * 2) destination numa is preferred
11207 * 3) task is cache cold, or
11208 * 4) too many balance attempts have failed.
11209 */
11210 if (env->flags & LBF_ACTIVE_LB)
11211 return !migrate_llc_task_wrong_dst(p, env);
11212
11213 degrades = migrate_degrades_locality(p, env);
11214 if (!degrades) {
11215 /*
11216 * If the NUMA locality is not broken,
11217 * further check if migration would hurt
11218 * LLC locality.
11219 */
11220 if (migrate_degrades_llc(p, env)) {
11221 /*
11222 * If regular load balancing fails to pull a task
11223 * due to LLC locality, this is expected behavior
11224 * and we set LBF_LLC_PINNED so we don't increase
11225 * nr_balance_failed unecessarily.
11226 */
11227 if (env->migration_type != migrate_llc_task)
11228 env->flags |= LBF_LLC_PINNED;
11229
11230 return 0;
11231 }
11232
11233 hot = task_hot(p, env);
11234 } else {
11235 hot = degrades > 0;
11236 }
11237
11238 if (!hot || env->sd->nr_balance_failed > env->sd->cache_nice_tries) {
11239 if (hot)
11240 p->sched_task_hot = 1;
11241 return 1;
11242 }
11243
11244 schedstat_inc(p->stats.nr_failed_migrations_hot);
11245 return 0;
11246 }
11247
11248 /*
11249 * detach_task() -- detach the task for the migration specified in env
11250 */
11251 static void detach_task(struct task_struct *p, struct lb_env *env)
11252 {
11253 lockdep_assert_rq_held(env->src_rq);
11254
11255 if (p->sched_task_hot) {
11256 p->sched_task_hot = 0;
11257 schedstat_inc(env->sd->lb_hot_gained[env->idle]);
11258 schedstat_inc(p->stats.nr_forced_migrations);
11259 }
11260
11261 WARN_ON(task_current(env->src_rq, p));
11262 WARN_ON(task_current_donor(env->src_rq, p));
11263
11264 deactivate_task(env->src_rq, p, DEQUEUE_NOCLOCK);
11265 set_task_cpu(p, env->dst_cpu);
11266 }
11267
11268 /*
11269 * detach_one_task() -- tries to dequeue exactly one task from env->src_rq, as
11270 * part of active balancing operations within "domain".
11271 *
11272 * Returns a task if successful and NULL otherwise.
11273 */
11274 static struct task_struct *detach_one_task(struct lb_env *env)
11275 {
11276 struct task_struct *p;
11277
11278 lockdep_assert_rq_held(env->src_rq);
11279
11280 list_for_each_entry_reverse(p,
11281 &env->src_rq->cfs_tasks, se.group_node) {
11282 if (!can_migrate_task(p, env))
11283 continue;
11284
11285 detach_task(p, env);
11286
11287 /*
11288 * Right now, this is only the second place where
11289 * lb_gained[env->idle] is updated (other is detach_tasks)
11290 * so we can safely collect stats here rather than
11291 * inside detach_tasks().
11292 */
11293 schedstat_inc(env->sd->lb_gained[env->idle]);
11294 return p;
11295 }
11296 return NULL;
11297 }
11298
11299 /*
11300 * detach_tasks() -- tries to detach up to imbalance load/util/tasks from
11301 * busiest_rq, as part of a balancing operation within domain "sd".
11302 *
11303 * Returns number of detached tasks if successful and 0 otherwise.
11304 */
11305 static int detach_tasks(struct lb_env *env)
11306 {
11307 struct list_head *tasks = &env->src_rq->cfs_tasks;
11308 unsigned long util, load;
11309 struct task_struct *p;
11310 int detached = 0;
11311
11312 lockdep_assert_rq_held(env->src_rq);
11313
11314 /*
11315 * Source run queue has been emptied by another CPU, clear
11316 * LBF_ALL_PINNED flag as we will not test any task.
11317 */
11318 if (env->src_rq->nr_running <= 1) {
11319 env->flags &= ~LBF_ALL_PINNED;
11320 return 0;
11321 }
11322
11323 if (env->imbalance <= 0)
11324 return 0;
11325
11326 while (!list_empty(tasks)) {
11327 /*
11328 * We don't want to steal all, otherwise we may be treated likewise,
11329 * which could at worst lead to a livelock crash.
11330 */
11331 if (env->idle && env->src_rq->nr_running <= 1)
11332 break;
11333
11334 env->loop++;
11335 /* We've more or less seen every task there is, call it quits */
11336 if (env->loop > env->loop_max)
11337 break;
11338
11339 /* take a breather every nr_migrate tasks */
11340 if (env->loop > env->loop_break) {
11341 env->loop_break += SCHED_NR_MIGRATE_BREAK;
11342 env->flags |= LBF_NEED_BREAK;
11343 break;
11344 }
11345
11346 p = list_last_entry(tasks, struct task_struct, se.group_node);
11347
11348 if (!can_migrate_task(p, env))
11349 goto next;
11350
11351 switch (env->migration_type) {
11352 case migrate_load:
11353 /*
11354 * Depending of the number of CPUs and tasks and the
11355 * cgroup hierarchy, task_h_load() can return a null
11356 * value. Make sure that env->imbalance decreases
11357 * otherwise detach_tasks() will stop only after
11358 * detaching up to loop_max tasks.
11359 */
11360 load = max_t(unsigned long, task_h_load(p), 1);
11361
11362 if (sched_feat(LB_MIN) &&
11363 load < 16 && !env->sd->nr_balance_failed)
11364 goto next;
11365
11366 /*
11367 * Make sure that we don't migrate too much load.
11368 * Nevertheless, let relax the constraint if
11369 * scheduler fails to find a good waiting task to
11370 * migrate.
11371 */
11372 if (shr_bound(load, env->sd->nr_balance_failed) > env->imbalance)
11373 goto next;
11374
11375 env->imbalance -= load;
11376 break;
11377
11378 case migrate_util:
11379 util = task_util_est(p);
11380
11381 if (shr_bound(util, env->sd->nr_balance_failed) > env->imbalance)
11382 goto next;
11383
11384 env->imbalance -= util;
11385 break;
11386
11387 case migrate_task:
11388 env->imbalance--;
11389 break;
11390
11391 case migrate_misfit:
11392 /* This is not a misfit task */
11393 if (task_fits_cpu(p, env->src_cpu))
11394 goto next;
11395
11396 env->imbalance = 0;
11397 break;
11398
11399 case migrate_llc_task:
11400 env->imbalance--;
11401 break;
11402 }
11403
11404 detach_task(p, env);
11405 list_add(&p->se.group_node, &env->tasks);
11406
11407 detached++;
11408
11409 #ifdef CONFIG_PREEMPTION
11410 /*
11411 * NEWIDLE balancing is a source of latency, so preemptible
11412 * kernels will stop after the first task is detached to minimize
11413 * the critical section.
11414 */
11415 if (env->idle == CPU_NEWLY_IDLE)
11416 break;
11417 #endif
11418
11419 /*
11420 * We only want to steal up to the prescribed amount of
11421 * load/util/tasks.
11422 */
11423 if (env->imbalance <= 0)
11424 break;
11425
11426 continue;
11427 next:
11428 if (p->sched_task_hot)
11429 schedstat_inc(p->stats.nr_failed_migrations_hot);
11430
11431 list_move(&p->se.group_node, tasks);
11432 }
11433
11434 /*
11435 * Right now, this is one of only two places we collect this stat
11436 * so we can safely collect detach_one_task() stats here rather
11437 * than inside detach_one_task().
11438 */
11439 schedstat_add(env->sd->lb_gained[env->idle], detached);
11440
11441 return detached;
11442 }
11443
11444 /*
11445 * attach_tasks() -- attaches all tasks detached by detach_tasks() to their
11446 * new rq.
11447 */
11448 static void attach_tasks(struct lb_env *env)
11449 {
11450 struct list_head *tasks = &env->tasks;
11451 struct task_struct *p;
11452 struct rq_flags rf;
11453
11454 rq_lock(env->dst_rq, &rf);
11455 update_rq_clock(env->dst_rq);
11456
11457 while (!list_empty(tasks)) {
11458 p = list_first_entry(tasks, struct task_struct, se.group_node);
11459 list_del_init(&p->se.group_node);
11460
11461 attach_task(env->dst_rq, p);
11462 }
11463
11464 rq_unlock(env->dst_rq, &rf);
11465 }
11466
11467 #ifdef CONFIG_NO_HZ_COMMON
11468 static inline bool cfs_rq_has_blocked_load(struct cfs_rq *cfs_rq)
11469 {
11470 if (cfs_rq->avg.load_avg)
11471 return true;
11472
11473 if (cfs_rq->avg.util_avg)
11474 return true;
11475
11476 return false;
11477 }
11478
11479 static inline bool others_have_blocked(struct rq *rq)
11480 {
11481 if (cpu_util_rt(rq))
11482 return true;
11483
11484 if (cpu_util_dl(rq))
11485 return true;
11486
11487 if (hw_load_avg(rq))
11488 return true;
11489
11490 if (cpu_util_irq(rq))
11491 return true;
11492
11493 return false;
11494 }
11495
11496 static inline void update_blocked_load_tick(struct rq *rq)
11497 {
11498 WRITE_ONCE(rq->last_blocked_load_update_tick, jiffies);
11499 }
11500
11501 static inline void update_has_blocked_load_status(struct rq *rq, bool has_blocked_load)
11502 {
11503 if (!has_blocked_load)
11504 rq->has_blocked_load = 0;
11505 }
11506 #else /* !CONFIG_NO_HZ_COMMON: */
11507 static inline bool cfs_rq_has_blocked_load(struct cfs_rq *cfs_rq) { return false; }
11508 static inline bool others_have_blocked(struct rq *rq) { return false; }
11509 static inline void update_blocked_load_tick(struct rq *rq) {}
11510 static inline void update_has_blocked_load_status(struct rq *rq, bool has_blocked_load) {}
11511 #endif /* !CONFIG_NO_HZ_COMMON */
11512
11513 static bool __update_blocked_others(struct rq *rq, bool *done)
11514 {
11515 bool updated;
11516
11517 /*
11518 * update_load_avg() can call cpufreq_update_util(). Make sure that RT,
11519 * DL and IRQ signals have been updated before updating CFS.
11520 */
11521 updated = update_other_load_avgs(rq);
11522
11523 if (others_have_blocked(rq))
11524 *done = false;
11525
11526 return updated;
11527 }
11528
11529 #ifdef CONFIG_FAIR_GROUP_SCHED
11530
11531 static bool __update_blocked_fair(struct rq *rq, bool *done)
11532 {
11533 struct cfs_rq *cfs_rq, *pos;
11534 bool decayed = false;
11535
11536 /*
11537 * Iterates the task_group tree in a bottom up fashion, see
11538 * list_add_leaf_cfs_rq() for details.
11539 */
11540 for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) {
11541 struct sched_entity *se;
11542
11543 if (update_cfs_rq_load_avg(cfs_rq_clock_pelt(cfs_rq), cfs_rq)) {
11544 update_tg_load_avg(cfs_rq);
11545
11546 if (cfs_rq->nr_queued == 0)
11547 update_idle_cfs_rq_clock_pelt(cfs_rq);
11548
11549 if (cfs_rq == &rq->cfs)
11550 decayed = true;
11551 }
11552
11553 /* Propagate pending load changes to the parent, if any: */
11554 se = cfs_rq_se(cfs_rq);
11555 if (se && !skip_blocked_update(se))
11556 update_load_avg(cfs_rq_of(se), se, UPDATE_TG);
11557
11558 /*
11559 * There can be a lot of idle CPU cgroups. Don't let fully
11560 * decayed cfs_rqs linger on the list.
11561 */
11562 if (cfs_rq_is_decayed(cfs_rq))
11563 list_del_leaf_cfs_rq(cfs_rq);
11564
11565 /* Don't need periodic decay once load/util_avg are null */
11566 if (cfs_rq_has_blocked_load(cfs_rq))
11567 *done = false;
11568 }
11569
11570 return decayed;
11571 }
11572
11573 /*
11574 * Compute the hierarchical load factor for cfs_rq and all its ascendants.
11575 * This needs to be done in a top-down fashion because the load of a child
11576 * group is a fraction of its parents load.
11577 */
11578 static void update_cfs_rq_h_load(struct cfs_rq *cfs_rq)
11579 {
11580 struct sched_entity *se = cfs_rq_se(cfs_rq);
11581 unsigned long now = jiffies;
11582 unsigned long load;
11583
11584 if (cfs_rq->last_h_load_update == now)
11585 return;
11586
11587 WRITE_ONCE(cfs_rq->h_load_next, NULL);
11588 for_each_sched_entity(se) {
11589 cfs_rq = cfs_rq_of(se);
11590 WRITE_ONCE(cfs_rq->h_load_next, se);
11591 if (cfs_rq->last_h_load_update == now)
11592 break;
11593 }
11594
11595 if (!se) {
11596 cfs_rq->h_load = cfs_rq_load_avg(cfs_rq);
11597 cfs_rq->last_h_load_update = now;
11598 }
11599
11600 while ((se = READ_ONCE(cfs_rq->h_load_next)) != NULL) {
11601 load = cfs_rq->h_load;
11602 load = div64_ul(load * se->avg.load_avg,
11603 cfs_rq_load_avg(cfs_rq) + 1);
11604 cfs_rq = group_cfs_rq(se);
11605 cfs_rq->h_load = load;
11606 cfs_rq->last_h_load_update = now;
11607 }
11608 }
11609
11610 static unsigned long task_h_load(struct task_struct *p)
11611 {
11612 struct cfs_rq *cfs_rq = task_cfs_rq(p);
11613
11614 update_cfs_rq_h_load(cfs_rq);
11615 return div64_ul(p->se.avg.load_avg * cfs_rq->h_load,
11616 cfs_rq_load_avg(cfs_rq) + 1);
11617 }
11618 #else /* !CONFIG_FAIR_GROUP_SCHED: */
11619 static bool __update_blocked_fair(struct rq *rq, bool *done)
11620 {
11621 struct cfs_rq *cfs_rq = &rq->cfs;
11622 bool decayed;
11623
11624 decayed = update_cfs_rq_load_avg(cfs_rq_clock_pelt(cfs_rq), cfs_rq);
11625 if (cfs_rq_has_blocked_load(cfs_rq))
11626 *done = false;
11627
11628 return decayed;
11629 }
11630
11631 static unsigned long task_h_load(struct task_struct *p)
11632 {
11633 return p->se.avg.load_avg;
11634 }
11635 #endif /* !CONFIG_FAIR_GROUP_SCHED */
11636
11637 static void __sched_balance_update_blocked_averages(struct rq *rq)
11638 {
11639 bool decayed = false, done = true;
11640
11641 update_blocked_load_tick(rq);
11642
11643 decayed |= __update_blocked_others(rq, &done);
11644 decayed |= __update_blocked_fair(rq, &done);
11645
11646 update_has_blocked_load_status(rq, !done);
11647 if (decayed)
11648 cpufreq_update_util(rq, 0);
11649 }
11650
11651 static void sched_balance_update_blocked_averages(int cpu)
11652 {
11653 struct rq *rq = cpu_rq(cpu);
11654
11655 guard(rq_lock_irqsave)(rq);
11656 update_rq_clock(rq);
11657 __sched_balance_update_blocked_averages(rq);
11658 }
11659
11660 /********** Helpers for sched_balance_find_src_group ************************/
11661
11662 /*
11663 * sg_lb_stats - stats of a sched_group required for load-balancing:
11664 */
11665 struct sg_lb_stats {
11666 unsigned long avg_load; /* Avg load over the CPUs of the group */
11667 unsigned long group_load; /* Total load over the CPUs of the group */
11668 unsigned long group_capacity; /* Capacity over the CPUs of the group */
11669 unsigned long group_util; /* Total utilization over the CPUs of the group */
11670 unsigned long group_runnable; /* Total runnable time over the CPUs of the group */
11671 unsigned int sum_nr_running; /* Nr of all tasks running in the group */
11672 unsigned int sum_h_nr_running; /* Nr of CFS tasks running in the group */
11673 unsigned int idle_cpus; /* Nr of idle CPUs in the group */
11674 unsigned int group_weight;
11675 enum group_type group_type;
11676 unsigned int group_asym_packing; /* Tasks should be moved to preferred CPU */
11677 unsigned int group_smt_balance; /* Task on busy SMT be moved */
11678 unsigned int group_llc_balance; /* Tasks should be moved to preferred LLC */
11679 unsigned long group_misfit_task_load; /* A CPU has a task too big for its capacity */
11680 unsigned int group_overutilized; /* At least one CPU is overutilized in the group */
11681 #ifdef CONFIG_NUMA_BALANCING
11682 unsigned int nr_numa_running;
11683 unsigned int nr_preferred_running;
11684 #endif
11685 #ifdef CONFIG_SCHED_CACHE
11686 unsigned int nr_pref_dst_llc;
11687 #endif
11688 };
11689
11690 /*
11691 * sd_lb_stats - stats of a sched_domain required for load-balancing:
11692 */
11693 struct sd_lb_stats {
11694 struct sched_group *busiest; /* Busiest group in this sd */
11695 struct sched_group *local; /* Local group in this sd */
11696 unsigned long total_load; /* Total load of all groups in sd */
11697 unsigned long total_capacity; /* Total capacity of all groups in sd */
11698 unsigned long avg_load; /* Average load across all groups in sd */
11699 unsigned int prefer_sibling; /* Tasks should go to sibling first */
11700
11701 struct sg_lb_stats busiest_stat; /* Statistics of the busiest group */
11702 struct sg_lb_stats local_stat; /* Statistics of the local group */
11703 };
11704
11705 static inline void init_sd_lb_stats(struct sd_lb_stats *sds)
11706 {
11707 /*
11708 * Skimp on the clearing to avoid duplicate work. We can avoid clearing
11709 * local_stat because update_sg_lb_stats() does a full clear/assignment.
11710 * We must however set busiest_stat::group_type and
11711 * busiest_stat::idle_cpus to the worst busiest group because
11712 * update_sd_pick_busiest() reads these before assignment.
11713 */
11714 *sds = (struct sd_lb_stats){
11715 .busiest = NULL,
11716 .local = NULL,
11717 .total_load = 0UL,
11718 .total_capacity = 0UL,
11719 .busiest_stat = {
11720 .idle_cpus = UINT_MAX,
11721 .group_type = group_has_spare,
11722 },
11723 };
11724 }
11725
11726 static unsigned long scale_rt_capacity(int cpu)
11727 {
11728 unsigned long max = get_actual_cpu_capacity(cpu);
11729 struct rq *rq = cpu_rq(cpu);
11730 unsigned long used, free;
11731 unsigned long irq;
11732
11733 irq = cpu_util_irq(rq);
11734
11735 if (unlikely(irq >= max))
11736 return 1;
11737
11738 /*
11739 * avg_rt.util_avg and avg_dl.util_avg track binary signals
11740 * (running and not running) with weights 0 and 1024 respectively.
11741 */
11742 used = cpu_util_rt(rq);
11743 used += cpu_util_dl(rq);
11744
11745 if (unlikely(used >= max))
11746 return 1;
11747
11748 free = max - used;
11749
11750 return scale_irq_capacity(free, irq, max);
11751 }
11752
11753 static void update_cpu_capacity(struct sched_domain *sd, int cpu)
11754 {
11755 unsigned long capacity = scale_rt_capacity(cpu);
11756 struct sched_group *sdg = sd->groups;
11757
11758 if (!capacity)
11759 capacity = 1;
11760
11761 cpu_rq(cpu)->cpu_capacity = capacity;
11762 trace_sched_cpu_capacity_tp(cpu_rq(cpu));
11763
11764 sdg->sgc->capacity = capacity;
11765 sdg->sgc->min_capacity = capacity;
11766 sdg->sgc->max_capacity = capacity;
11767 }
11768
11769 void update_group_capacity(struct sched_domain *sd, int cpu)
11770 {
11771 struct sched_domain *child = sd->child;
11772 struct sched_group *group, *sdg = sd->groups;
11773 unsigned long capacity, min_capacity, max_capacity;
11774 unsigned long interval;
11775
11776 interval = msecs_to_jiffies(sd->balance_interval);
11777 interval = clamp(interval, 1UL, max_load_balance_interval);
11778 sdg->sgc->next_update = jiffies + interval;
11779
11780 if (!child) {
11781 update_cpu_capacity(sd, cpu);
11782 return;
11783 }
11784
11785 capacity = 0;
11786 min_capacity = ULONG_MAX;
11787 max_capacity = 0;
11788
11789 if (child->flags & SD_NUMA) {
11790 /*
11791 * SD_NUMA domains cannot assume that child groups
11792 * span the current group.
11793 */
11794
11795 for_each_cpu(cpu, sched_group_span(sdg)) {
11796 unsigned long cpu_cap = capacity_of(cpu);
11797
11798 capacity += cpu_cap;
11799 min_capacity = min(cpu_cap, min_capacity);
11800 max_capacity = max(cpu_cap, max_capacity);
11801 }
11802 } else {
11803 /*
11804 * !SD_NUMA domains can assume that child groups
11805 * span the current group.
11806 */
11807
11808 group = child->groups;
11809 do {
11810 struct sched_group_capacity *sgc = group->sgc;
11811
11812 capacity += sgc->capacity;
11813 min_capacity = min(sgc->min_capacity, min_capacity);
11814 max_capacity = max(sgc->max_capacity, max_capacity);
11815 group = group->next;
11816 } while (group != child->groups);
11817 }
11818
11819 sdg->sgc->capacity = capacity;
11820 sdg->sgc->min_capacity = min_capacity;
11821 sdg->sgc->max_capacity = max_capacity;
11822 }
11823
11824 /*
11825 * Check whether the capacity of the rq has been noticeably reduced by side
11826 * activity. The imbalance_pct is used for the threshold.
11827 * Return true is the capacity is reduced
11828 */
11829 static inline int
11830 check_cpu_capacity(struct rq *rq, struct sched_domain *sd)
11831 {
11832 return ((rq->cpu_capacity * sd->imbalance_pct) <
11833 (arch_scale_cpu_capacity(cpu_of(rq)) * 100));
11834 }
11835
11836 /* Check if the rq has a misfit task */
11837 static inline bool check_misfit_status(struct rq *rq)
11838 {
11839 return rq->misfit_task_load;
11840 }
11841
11842 /*
11843 * Group imbalance indicates (and tries to solve) the problem where balancing
11844 * groups is inadequate due to ->cpus_ptr constraints.
11845 *
11846 * Imagine a situation of two groups of 4 CPUs each and 4 tasks each with a
11847 * cpumask covering 1 CPU of the first group and 3 CPUs of the second group.
11848 * Something like:
11849 *
11850 * { 0 1 2 3 } { 4 5 6 7 }
11851 * * * * *
11852 *
11853 * If we were to balance group-wise we'd place two tasks in the first group and
11854 * two tasks in the second group. Clearly this is undesired as it will overload
11855 * cpu 3 and leave one of the CPUs in the second group unused.
11856 *
11857 * The current solution to this issue is detecting the skew in the first group
11858 * by noticing the lower domain failed to reach balance and had difficulty
11859 * moving tasks due to affinity constraints.
11860 *
11861 * When this is so detected; this group becomes a candidate for busiest; see
11862 * update_sd_pick_busiest(). And calculate_imbalance() and
11863 * sched_balance_find_src_group() avoid some of the usual balance conditions to allow it
11864 * to create an effective group imbalance.
11865 *
11866 * This is a somewhat tricky proposition since the next run might not find the
11867 * group imbalance and decide the groups need to be balanced again. A most
11868 * subtle and fragile situation.
11869 */
11870
11871 static inline int sg_imbalanced(struct sched_group *group)
11872 {
11873 return group->sgc->imbalance;
11874 }
11875
11876 /*
11877 * group_has_capacity returns true if the group has spare capacity that could
11878 * be used by some tasks.
11879 * We consider that a group has spare capacity if the number of task is
11880 * smaller than the number of CPUs or if the utilization is lower than the
11881 * available capacity for CFS tasks.
11882 * For the latter, we use a threshold to stabilize the state, to take into
11883 * account the variance of the tasks' load and to return true if the available
11884 * capacity in meaningful for the load balancer.
11885 * As an example, an available capacity of 1% can appear but it doesn't make
11886 * any benefit for the load balance.
11887 */
11888 static inline bool
11889 group_has_capacity(unsigned int imbalance_pct, struct sg_lb_stats *sgs)
11890 {
11891 if (sgs->sum_nr_running < sgs->group_weight)
11892 return true;
11893
11894 if ((sgs->group_capacity * imbalance_pct) <
11895 (sgs->group_runnable * 100))
11896 return false;
11897
11898 if ((sgs->group_capacity * 100) >
11899 (sgs->group_util * imbalance_pct))
11900 return true;
11901
11902 return false;
11903 }
11904
11905 /*
11906 * group_is_overloaded returns true if the group has more tasks than it can
11907 * handle.
11908 * group_is_overloaded is not equals to !group_has_capacity because a group
11909 * with the exact right number of tasks, has no more spare capacity but is not
11910 * overloaded so both group_has_capacity and group_is_overloaded return
11911 * false.
11912 */
11913 static inline bool
11914 group_is_overloaded(unsigned int imbalance_pct, struct sg_lb_stats *sgs)
11915 {
11916 /*
11917 * With EAS and uclamp, 1 CPU in the group must be overutilized to
11918 * consider the group overloaded.
11919 */
11920 if (sched_energy_enabled() && !sgs->group_overutilized)
11921 return false;
11922
11923 if (sgs->sum_nr_running <= sgs->group_weight)
11924 return false;
11925
11926 if ((sgs->group_capacity * 100) <
11927 (sgs->group_util * imbalance_pct))
11928 return true;
11929
11930 if ((sgs->group_capacity * imbalance_pct) <
11931 (sgs->group_runnable * 100))
11932 return true;
11933
11934 return false;
11935 }
11936
11937 static inline enum
11938 group_type group_classify(unsigned int imbalance_pct,
11939 struct sched_group *group,
11940 struct sg_lb_stats *sgs)
11941 {
11942 if (group_is_overloaded(imbalance_pct, sgs))
11943 return group_overloaded;
11944
11945 if (sgs->group_llc_balance)
11946 return group_llc_balance;
11947
11948 if (sg_imbalanced(group))
11949 return group_imbalanced;
11950
11951 if (sgs->group_asym_packing)
11952 return group_asym_packing;
11953
11954 if (sgs->group_smt_balance)
11955 return group_smt_balance;
11956
11957 if (sgs->group_misfit_task_load)
11958 return group_misfit_task;
11959
11960 if (!group_has_capacity(imbalance_pct, sgs))
11961 return group_fully_busy;
11962
11963 return group_has_spare;
11964 }
11965
11966 /**
11967 * sched_use_asym_prio - Check whether asym_packing priority must be used
11968 * @sd: The scheduling domain of the load balancing
11969 * @cpu: A CPU
11970 *
11971 * Always use CPU priority when balancing load between SMT siblings. When
11972 * balancing load between cores, it is not sufficient that @cpu is idle. Only
11973 * use CPU priority if the whole core is idle.
11974 *
11975 * Returns: True if the priority of @cpu must be followed. False otherwise.
11976 */
11977 static bool sched_use_asym_prio(struct sched_domain *sd, int cpu)
11978 {
11979 if (!(sd->flags & SD_ASYM_PACKING))
11980 return false;
11981
11982 if (!sched_smt_active())
11983 return true;
11984
11985 return sd->flags & SD_SHARE_CPUCAPACITY || is_core_idle(cpu);
11986 }
11987
11988 static inline bool sched_asym(struct sched_domain *sd, int dst_cpu, int src_cpu)
11989 {
11990 /*
11991 * First check if @dst_cpu can do asym_packing load balance. Only do it
11992 * if it has higher priority than @src_cpu.
11993 */
11994 return sched_use_asym_prio(sd, dst_cpu) &&
11995 sched_asym_prefer(dst_cpu, src_cpu);
11996 }
11997
11998 /**
11999 * sched_group_asym - Check if the destination CPU can do asym_packing balance
12000 * @env: The load balancing environment
12001 * @sgs: Load-balancing statistics of the candidate busiest group
12002 * @group: The candidate busiest group
12003 *
12004 * @env::dst_cpu can do asym_packing if it has higher priority than the
12005 * preferred CPU of @group.
12006 *
12007 * Return: true if @env::dst_cpu can do with asym_packing load balance. False
12008 * otherwise.
12009 */
12010 static inline bool
12011 sched_group_asym(struct lb_env *env, struct sg_lb_stats *sgs, struct sched_group *group)
12012 {
12013 /*
12014 * CPU priorities do not make sense for SMT cores with more than one
12015 * busy sibling.
12016 */
12017 if ((group->flags & SD_SHARE_CPUCAPACITY) &&
12018 (sgs->group_weight - sgs->idle_cpus != 1))
12019 return false;
12020
12021 return sched_asym(env->sd, env->dst_cpu, READ_ONCE(group->asym_prefer_cpu));
12022 }
12023
12024 /* One group has more than one SMT CPU while the other group does not */
12025 static inline bool smt_vs_nonsmt_groups(struct sched_group *sg1,
12026 struct sched_group *sg2)
12027 {
12028 if (!sg1 || !sg2)
12029 return false;
12030
12031 return (sg1->flags & SD_SHARE_CPUCAPACITY) !=
12032 (sg2->flags & SD_SHARE_CPUCAPACITY);
12033 }
12034
12035 static inline bool smt_balance(struct lb_env *env, struct sg_lb_stats *sgs,
12036 struct sched_group *group)
12037 {
12038 if (!env->idle)
12039 return false;
12040
12041 /*
12042 * For SMT source group, it is better to move a task
12043 * to a CPU that doesn't have multiple tasks sharing its CPU capacity.
12044 * Note that if a group has a single SMT, SD_SHARE_CPUCAPACITY
12045 * will not be on.
12046 */
12047 if (group->flags & SD_SHARE_CPUCAPACITY &&
12048 sgs->sum_h_nr_running > 1)
12049 return true;
12050
12051 return false;
12052 }
12053
12054 static inline long sibling_imbalance(struct lb_env *env,
12055 struct sd_lb_stats *sds,
12056 struct sg_lb_stats *busiest,
12057 struct sg_lb_stats *local)
12058 {
12059 int ncores_busiest, ncores_local;
12060 long imbalance;
12061
12062 if (!env->idle || !busiest->sum_nr_running)
12063 return 0;
12064
12065 ncores_busiest = sds->busiest->cores;
12066 ncores_local = sds->local->cores;
12067
12068 if (ncores_busiest == ncores_local) {
12069 imbalance = busiest->sum_nr_running;
12070 lsub_positive(&imbalance, local->sum_nr_running);
12071 return imbalance;
12072 }
12073
12074 /* Balance such that nr_running/ncores ratio are same on both groups */
12075 imbalance = ncores_local * busiest->sum_nr_running;
12076 lsub_positive(&imbalance, ncores_busiest * local->sum_nr_running);
12077 /* Normalize imbalance and do rounding on normalization */
12078 imbalance = 2 * imbalance + ncores_local + ncores_busiest;
12079 imbalance /= ncores_local + ncores_busiest;
12080
12081 /* Take advantage of resource in an empty sched group */
12082 if (imbalance <= 1 && local->sum_nr_running == 0 &&
12083 busiest->sum_nr_running > 1)
12084 imbalance = 2;
12085
12086 return imbalance;
12087 }
12088
12089 static inline bool
12090 sched_reduced_capacity(struct rq *rq, struct sched_domain *sd)
12091 {
12092 /*
12093 * When there is more than 1 task, the group_overloaded case already
12094 * takes care of cpu with reduced capacity
12095 */
12096 if (rq->cfs.h_nr_runnable != 1)
12097 return false;
12098
12099 return check_cpu_capacity(rq, sd);
12100 }
12101
12102 #ifdef CONFIG_SCHED_CACHE
12103 /*
12104 * Record the statistics for this scheduler group for later
12105 * use. These values guide load balancing on aggregating tasks
12106 * to a LLC.
12107 */
12108 static void record_sg_llc_stats(struct lb_env *env,
12109 struct sg_lb_stats *sgs,
12110 struct sched_group *group)
12111 {
12112 struct sched_domain_shared *sd_share;
12113 int cpu;
12114
12115 if (!sched_cache_enabled() || env->idle == CPU_NEWLY_IDLE)
12116 return;
12117
12118 /* Only care about sched domain spanning multiple LLCs */
12119 if (env->sd->child != rcu_dereference_all(per_cpu(sd_llc, env->dst_cpu)))
12120 return;
12121
12122 /*
12123 * At this point we know this group spans a LLC domain.
12124 * Record the statistic of this group in its corresponding
12125 * shared LLC domain.
12126 * Note: sd_share cannot be obtained via sd->child->shared,
12127 * because the latter refers to the domain that covers the
12128 * local group. Instead, sd_share should be located using
12129 * the first CPU of the LLC group.
12130 */
12131 cpu = cpumask_first(sched_group_span(group));
12132 sd_share = rcu_dereference_all(per_cpu(sd_llc_shared, cpu));
12133 if (!sd_share)
12134 return;
12135
12136 if (READ_ONCE(sd_share->util_avg) != sgs->group_util)
12137 WRITE_ONCE(sd_share->util_avg, sgs->group_util);
12138
12139 if (unlikely(READ_ONCE(sd_share->capacity) != sgs->group_capacity))
12140 WRITE_ONCE(sd_share->capacity, sgs->group_capacity);
12141 }
12142
12143 /*
12144 * Do LLC balance on sched group that contains LLC, and have tasks preferring
12145 * to run on LLC in idle dst_cpu.
12146 */
12147 static inline bool llc_balance(struct lb_env *env, struct sg_lb_stats *sgs,
12148 struct sched_group *group)
12149 {
12150 if (!sched_cache_enabled())
12151 return false;
12152
12153 if (env->sd->flags & SD_SHARE_LLC)
12154 return false;
12155
12156 /*
12157 * On asymmetric domains, group_misfit_task_load
12158 * should be prioritized to move tasks to CPU that fit them
12159 * over aggregating tasks to their preferred LLC.
12160 */
12161 if ((env->sd->flags & SD_ASYM_CPUCAPACITY) &&
12162 sgs->group_misfit_task_load)
12163 return false;
12164
12165 /*
12166 * Skip cache aware tagging if nr_balanced_failed is sufficiently high.
12167 * Threshold of cache_nice_tries is set to 1 higher than nr_balance_failed
12168 * to avoid excessive task migration at the same time.
12169 */
12170 if (env->sd->nr_balance_failed >= env->sd->cache_nice_tries + 1)
12171 return false;
12172
12173 if (sgs->nr_pref_dst_llc &&
12174 can_migrate_llc(cpumask_first(sched_group_span(group)),
12175 env->dst_cpu, 0, true) == mig_llc)
12176 return true;
12177
12178 return false;
12179 }
12180
12181 static bool update_llc_busiest(struct lb_env *env,
12182 struct sg_lb_stats *busiest,
12183 struct sg_lb_stats *sgs)
12184 {
12185 /*
12186 * There are more tasks that want to run on dst_cpu's LLC.
12187 */
12188 return sgs->nr_pref_dst_llc > busiest->nr_pref_dst_llc;
12189 }
12190 #else
12191 static inline void record_sg_llc_stats(struct lb_env *env, struct sg_lb_stats *sgs,
12192 struct sched_group *group)
12193 {
12194 }
12195
12196 static inline bool llc_balance(struct lb_env *env, struct sg_lb_stats *sgs,
12197 struct sched_group *group)
12198 {
12199 return false;
12200 }
12201
12202 static bool update_llc_busiest(struct lb_env *env,
12203 struct sg_lb_stats *busiest,
12204 struct sg_lb_stats *sgs)
12205 {
12206 return false;
12207 }
12208 #endif
12209
12210 /**
12211 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
12212 * @env: The load balancing environment.
12213 * @sds: Load-balancing data with statistics of the local group.
12214 * @group: sched_group whose statistics are to be updated.
12215 * @sgs: variable to hold the statistics for this group.
12216 * @sg_overloaded: sched_group is overloaded
12217 */
12218 static inline void update_sg_lb_stats(struct lb_env *env,
12219 struct sd_lb_stats *sds,
12220 struct sched_group *group,
12221 struct sg_lb_stats *sgs,
12222 bool *sg_overloaded)
12223 {
12224 int i, nr_running, local_group, sd_flags = env->sd->flags;
12225 bool balancing_at_rd = !env->sd->parent;
12226
12227 memset(sgs, 0, sizeof(*sgs));
12228
12229 local_group = group == sds->local;
12230
12231 for_each_cpu_and(i, sched_group_span(group), env->cpus) {
12232 struct rq *rq = cpu_rq(i);
12233 unsigned long load = cpu_load(rq);
12234
12235 sgs->group_load += load;
12236 sgs->group_util += cpu_util_cfs(i);
12237 sgs->group_runnable += cpu_runnable(rq);
12238 sgs->sum_h_nr_running += rq->cfs.h_nr_runnable;
12239
12240 nr_running = rq->nr_running;
12241 sgs->sum_nr_running += nr_running;
12242
12243 if (cpu_overutilized(i))
12244 sgs->group_overutilized = 1;
12245
12246 #ifdef CONFIG_SCHED_CACHE
12247 if (sched_cache_enabled()) {
12248 struct sched_domain *sd_tmp;
12249 int dst_llc;
12250
12251 dst_llc = llc_id(env->dst_cpu);
12252 if (llc_id(i) != dst_llc) {
12253 sd_tmp = rcu_dereference_all(rq->sd);
12254 if (sd_tmp && (unsigned int)dst_llc < sd_tmp->llc_max)
12255 sgs->nr_pref_dst_llc += sd_tmp->llc_counts[dst_llc];
12256 }
12257 }
12258 #endif
12259
12260 /*
12261 * No need to call idle_cpu() if nr_running is not 0
12262 */
12263 if (!nr_running && idle_cpu(i)) {
12264 sgs->idle_cpus++;
12265 /* Idle cpu can't have misfit task */
12266 continue;
12267 }
12268
12269 /* Overload indicator is only updated at root domain */
12270 if (balancing_at_rd && nr_running > 1)
12271 *sg_overloaded = 1;
12272
12273 #ifdef CONFIG_NUMA_BALANCING
12274 /* Only fbq_classify_group() uses this to classify NUMA groups */
12275 if (sd_flags & SD_NUMA) {
12276 sgs->nr_numa_running += rq->nr_numa_running;
12277 sgs->nr_preferred_running += rq->nr_preferred_running;
12278 }
12279 #endif
12280 if (local_group)
12281 continue;
12282
12283 if (sd_flags & SD_ASYM_CPUCAPACITY) {
12284 if (rq->misfit_task_load) {
12285 /*
12286 * Always mark the root domain overloaded so big
12287 * CPUs can pick up misfit tasks via newly idle
12288 * balance.
12289 */
12290 if (balancing_at_rd)
12291 *sg_overloaded = 1;
12292
12293 /*
12294 * Only account misfit load if @dst_cpu can
12295 * help; otherwise, the group may be classified
12296 * as misfit_task and update_sd_pick_busiest()
12297 * will skip it.
12298 */
12299 if (capacity_greater(capacity_of(env->dst_cpu),
12300 group->sgc->max_capacity) &&
12301 (sgs->group_misfit_task_load < rq->misfit_task_load))
12302 sgs->group_misfit_task_load = rq->misfit_task_load;
12303 }
12304 } else if (env->idle && sched_reduced_capacity(rq, env->sd)) {
12305 /* Check for a task running on a CPU with reduced capacity */
12306 if (sgs->group_misfit_task_load < load)
12307 sgs->group_misfit_task_load = load;
12308 }
12309 }
12310
12311 sgs->group_capacity = group->sgc->capacity;
12312
12313 sgs->group_weight = group->group_weight;
12314
12315 if (!local_group) {
12316 /* Check if dst CPU is idle and preferred to this group */
12317 if (env->idle && sgs->sum_h_nr_running &&
12318 sched_group_asym(env, sgs, group))
12319 sgs->group_asym_packing = 1;
12320
12321 /* Check for loaded SMT group to be balanced to dst CPU */
12322 if (smt_balance(env, sgs, group))
12323 sgs->group_smt_balance = 1;
12324
12325 /* Check for tasks in this group can be moved to their preferred LLC */
12326 if (llc_balance(env, sgs, group))
12327 sgs->group_llc_balance = 1;
12328 }
12329
12330 sgs->group_type = group_classify(env->sd->imbalance_pct, group, sgs);
12331
12332 record_sg_llc_stats(env, sgs, group);
12333 /* Computing avg_load makes sense only when group is overloaded */
12334 if (sgs->group_type == group_overloaded)
12335 sgs->avg_load = (sgs->group_load * SCHED_CAPACITY_SCALE) /
12336 sgs->group_capacity;
12337 }
12338
12339 /**
12340 * update_sd_pick_busiest - return 1 on busiest group
12341 * @env: The load balancing environment.
12342 * @sds: sched_domain statistics
12343 * @sg: sched_group candidate to be checked for being the busiest
12344 * @sgs: sched_group statistics
12345 *
12346 * Determine if @sg is a busier group than the previously selected
12347 * busiest group.
12348 *
12349 * Return: %true if @sg is a busier group than the previously selected
12350 * busiest group. %false otherwise.
12351 */
12352 static bool update_sd_pick_busiest(struct lb_env *env,
12353 struct sd_lb_stats *sds,
12354 struct sched_group *sg,
12355 struct sg_lb_stats *sgs)
12356 {
12357 struct sg_lb_stats *busiest = &sds->busiest_stat;
12358
12359 /* Make sure that there is at least one task to pull */
12360 if (!sgs->sum_h_nr_running)
12361 return false;
12362
12363 /*
12364 * Don't try to pull misfit tasks we can't help.
12365 * We can use max_capacity here as reduction in capacity on some
12366 * CPUs in the group should either be possible to resolve
12367 * internally or be covered by avg_load imbalance (eventually).
12368 *
12369 * When SMT is active, only pull a misfit to dst_cpu if it is on a
12370 * fully idle core; otherwise the effective capacity of the core is
12371 * reduced and we may not actually provide more capacity than the
12372 * source.
12373 */
12374 if ((env->sd->flags & SD_ASYM_CPUCAPACITY) &&
12375 (sgs->group_type == group_misfit_task) &&
12376 (!env->dst_core_idle ||
12377 !capacity_greater(capacity_of(env->dst_cpu), sg->sgc->max_capacity) ||
12378 sds->local_stat.group_type != group_has_spare))
12379 return false;
12380
12381 /*
12382 * Candidate sg has no more than one task per CPU and has higher
12383 * per-CPU capacity. Migrating tasks to less capable CPUs may harm
12384 * throughput. Maximize throughput, power/energy consequences are not
12385 * considered.
12386 */
12387 if ((env->sd->flags & SD_ASYM_CPUCAPACITY) &&
12388 (sgs->group_type <= group_fully_busy) &&
12389 (capacity_greater(sg->sgc->min_capacity, capacity_of(env->dst_cpu))))
12390 return false;
12391
12392 if (sgs->group_type > busiest->group_type)
12393 return true;
12394
12395 if (sgs->group_type < busiest->group_type)
12396 return false;
12397
12398 /*
12399 * The candidate and the current busiest group are the same type of
12400 * group. Let check which one is the busiest according to the type.
12401 */
12402
12403 switch (sgs->group_type) {
12404 case group_overloaded:
12405 /* Select the overloaded group with highest avg_load. */
12406 return sgs->avg_load > busiest->avg_load;
12407
12408 case group_llc_balance:
12409 /* Select the group with most tasks preferring dst LLC */
12410 return update_llc_busiest(env, busiest, sgs);
12411
12412 case group_imbalanced:
12413 /*
12414 * Select the 1st imbalanced group as we don't have any way to
12415 * choose one more than another.
12416 */
12417 return false;
12418
12419 case group_asym_packing:
12420 /* Prefer to move from lowest priority CPU's work */
12421 return sched_asym_prefer(READ_ONCE(sds->busiest->asym_prefer_cpu),
12422 READ_ONCE(sg->asym_prefer_cpu));
12423
12424 case group_misfit_task:
12425 /*
12426 * If we have more than one misfit sg go with the biggest
12427 * misfit.
12428 */
12429 return sgs->group_misfit_task_load > busiest->group_misfit_task_load;
12430
12431 case group_smt_balance:
12432 /*
12433 * Check if we have spare CPUs on either SMT group to
12434 * choose has spare or fully busy handling.
12435 */
12436 if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0)
12437 goto has_spare;
12438
12439 fallthrough;
12440
12441 case group_fully_busy:
12442 /*
12443 * Select the fully busy group with highest avg_load. In
12444 * theory, there is no need to pull task from such kind of
12445 * group because tasks have all compute capacity that they need
12446 * but we can still improve the overall throughput by reducing
12447 * contention when accessing shared HW resources.
12448 *
12449 * XXX for now avg_load is not computed and always 0 so we
12450 * select the 1st one, except if @sg is composed of SMT
12451 * siblings.
12452 */
12453
12454 if (sgs->avg_load < busiest->avg_load)
12455 return false;
12456
12457 if (sgs->avg_load == busiest->avg_load) {
12458 /*
12459 * SMT sched groups need more help than non-SMT groups.
12460 * If @sg happens to also be SMT, either choice is good.
12461 */
12462 if (sds->busiest->flags & SD_SHARE_CPUCAPACITY)
12463 return false;
12464 }
12465
12466 break;
12467
12468 case group_has_spare:
12469 /*
12470 * Do not pick sg with SMT CPUs over sg with pure CPUs,
12471 * as we do not want to pull task off SMT core with one task
12472 * and make the core idle.
12473 */
12474 if (smt_vs_nonsmt_groups(sds->busiest, sg)) {
12475 if (sg->flags & SD_SHARE_CPUCAPACITY && sgs->sum_h_nr_running <= 1)
12476 return false;
12477 else
12478 return true;
12479 }
12480 has_spare:
12481
12482 /*
12483 * Select not overloaded group with lowest number of idle CPUs
12484 * and highest number of running tasks. We could also compare
12485 * the spare capacity which is more stable but it can end up
12486 * that the group has less spare capacity but finally more idle
12487 * CPUs which means less opportunity to pull tasks.
12488 */
12489 if (sgs->idle_cpus > busiest->idle_cpus)
12490 return false;
12491 else if ((sgs->idle_cpus == busiest->idle_cpus) &&
12492 (sgs->sum_nr_running <= busiest->sum_nr_running))
12493 return false;
12494
12495 break;
12496 }
12497
12498 return true;
12499 }
12500
12501 #ifdef CONFIG_NUMA_BALANCING
12502 static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs)
12503 {
12504 if (sgs->sum_h_nr_running > sgs->nr_numa_running)
12505 return regular;
12506 if (sgs->sum_h_nr_running > sgs->nr_preferred_running)
12507 return remote;
12508 return all;
12509 }
12510
12511 static inline enum fbq_type fbq_classify_rq(struct rq *rq)
12512 {
12513 if (rq->nr_running > rq->nr_numa_running)
12514 return regular;
12515 if (rq->nr_running > rq->nr_preferred_running)
12516 return remote;
12517 return all;
12518 }
12519 #else /* !CONFIG_NUMA_BALANCING: */
12520 static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs)
12521 {
12522 return all;
12523 }
12524
12525 static inline enum fbq_type fbq_classify_rq(struct rq *rq)
12526 {
12527 return regular;
12528 }
12529 #endif /* !CONFIG_NUMA_BALANCING */
12530
12531
12532 struct sg_lb_stats;
12533
12534 /*
12535 * task_running_on_cpu - return 1 if @p is running on @cpu.
12536 */
12537
12538 static unsigned int task_running_on_cpu(int cpu, struct task_struct *p)
12539 {
12540 /* Task has no contribution or is new */
12541 if (cpu != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time))
12542 return 0;
12543
12544 if (task_on_rq_queued(p))
12545 return 1;
12546
12547 return 0;
12548 }
12549
12550 /**
12551 * idle_cpu_without - would a given CPU be idle without p ?
12552 * @cpu: the processor on which idleness is tested.
12553 * @p: task which should be ignored.
12554 *
12555 * Return: 1 if the CPU would be idle. 0 otherwise.
12556 */
12557 static int idle_cpu_without(int cpu, struct task_struct *p)
12558 {
12559 struct rq *rq = cpu_rq(cpu);
12560
12561 if (rq->curr != rq->idle && rq->curr != p)
12562 return 0;
12563
12564 /*
12565 * rq->nr_running can't be used but an updated version without the
12566 * impact of p on cpu must be used instead. The updated nr_running
12567 * be computed and tested before calling idle_cpu_without().
12568 */
12569
12570 if (rq->ttwu_pending)
12571 return 0;
12572
12573 return 1;
12574 }
12575
12576 /*
12577 * update_sg_wakeup_stats - Update sched_group's statistics for wakeup.
12578 * @sd: The sched_domain level to look for idlest group.
12579 * @group: sched_group whose statistics are to be updated.
12580 * @sgs: variable to hold the statistics for this group.
12581 * @p: The task for which we look for the idlest group/CPU.
12582 */
12583 static inline void update_sg_wakeup_stats(struct sched_domain *sd,
12584 struct sched_group *group,
12585 struct sg_lb_stats *sgs,
12586 struct task_struct *p)
12587 {
12588 int i, nr_running;
12589
12590 memset(sgs, 0, sizeof(*sgs));
12591
12592 /* Assume that task can't fit any CPU of the group */
12593 if (sd->flags & SD_ASYM_CPUCAPACITY)
12594 sgs->group_misfit_task_load = 1;
12595
12596 for_each_cpu_and(i, sched_group_span(group), p->cpus_ptr) {
12597 struct rq *rq = cpu_rq(i);
12598 unsigned int local;
12599
12600 sgs->group_load += cpu_load_without(rq, p);
12601 sgs->group_util += cpu_util_without(i, p);
12602 sgs->group_runnable += cpu_runnable_without(rq, p);
12603 local = task_running_on_cpu(i, p);
12604 sgs->sum_h_nr_running += rq->cfs.h_nr_runnable - local;
12605
12606 nr_running = rq->nr_running - local;
12607 sgs->sum_nr_running += nr_running;
12608
12609 /*
12610 * No need to call idle_cpu_without() if nr_running is not 0
12611 */
12612 if (!nr_running && idle_cpu_without(i, p))
12613 sgs->idle_cpus++;
12614
12615 /* Check if task fits in the CPU */
12616 if (sd->flags & SD_ASYM_CPUCAPACITY &&
12617 sgs->group_misfit_task_load &&
12618 task_fits_cpu(p, i))
12619 sgs->group_misfit_task_load = 0;
12620
12621 }
12622
12623 sgs->group_capacity = group->sgc->capacity;
12624
12625 sgs->group_weight = group->group_weight;
12626
12627 sgs->group_type = group_classify(sd->imbalance_pct, group, sgs);
12628
12629 /*
12630 * Computing avg_load makes sense only when group is fully busy or
12631 * overloaded
12632 */
12633 if (sgs->group_type == group_fully_busy ||
12634 sgs->group_type == group_overloaded)
12635 sgs->avg_load = (sgs->group_load * SCHED_CAPACITY_SCALE) /
12636 sgs->group_capacity;
12637 }
12638
12639 static bool update_pick_idlest(struct sched_group *idlest,
12640 struct sg_lb_stats *idlest_sgs,
12641 struct sched_group *group,
12642 struct sg_lb_stats *sgs)
12643 {
12644 if (sgs->group_type < idlest_sgs->group_type)
12645 return true;
12646
12647 if (sgs->group_type > idlest_sgs->group_type)
12648 return false;
12649
12650 /*
12651 * The candidate and the current idlest group are the same type of
12652 * group. Let check which one is the idlest according to the type.
12653 */
12654
12655 switch (sgs->group_type) {
12656 case group_overloaded:
12657 case group_fully_busy:
12658 /* Select the group with lowest avg_load. */
12659 if (idlest_sgs->avg_load <= sgs->avg_load)
12660 return false;
12661 break;
12662
12663 case group_llc_balance:
12664 case group_imbalanced:
12665 case group_asym_packing:
12666 case group_smt_balance:
12667 /* Those types are not used in the slow wakeup path */
12668 return false;
12669
12670 case group_misfit_task:
12671 /* Select group with the highest max capacity */
12672 if (idlest->sgc->max_capacity >= group->sgc->max_capacity)
12673 return false;
12674 break;
12675
12676 case group_has_spare:
12677 /* Select group with most idle CPUs */
12678 if (idlest_sgs->idle_cpus > sgs->idle_cpus)
12679 return false;
12680
12681 /* Select group with lowest group_util */
12682 if (idlest_sgs->idle_cpus == sgs->idle_cpus &&
12683 idlest_sgs->group_util <= sgs->group_util)
12684 return false;
12685
12686 break;
12687 }
12688
12689 return true;
12690 }
12691
12692 /*
12693 * sched_balance_find_dst_group() finds and returns the least busy CPU group within the
12694 * domain.
12695 *
12696 * Assumes p is allowed on at least one CPU in sd.
12697 */
12698 static struct sched_group *
12699 sched_balance_find_dst_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
12700 {
12701 struct sched_group *idlest = NULL, *local = NULL, *group = sd->groups;
12702 struct sg_lb_stats local_sgs, tmp_sgs;
12703 struct sg_lb_stats *sgs;
12704 unsigned long imbalance;
12705 struct sg_lb_stats idlest_sgs = {
12706 .avg_load = UINT_MAX,
12707 .group_type = group_overloaded,
12708 };
12709
12710 do {
12711 int local_group;
12712
12713 /* Skip over this group if it has no CPUs allowed */
12714 if (!cpumask_intersects(sched_group_span(group),
12715 p->cpus_ptr))
12716 continue;
12717
12718 /* Skip over this group if no cookie matched */
12719 if (!sched_group_cookie_match(cpu_rq(this_cpu), p, group))
12720 continue;
12721
12722 local_group = cpumask_test_cpu(this_cpu,
12723 sched_group_span(group));
12724
12725 if (local_group) {
12726 sgs = &local_sgs;
12727 local = group;
12728 } else {
12729 sgs = &tmp_sgs;
12730 }
12731
12732 update_sg_wakeup_stats(sd, group, sgs, p);
12733
12734 if (!local_group && update_pick_idlest(idlest, &idlest_sgs, group, sgs)) {
12735 idlest = group;
12736 idlest_sgs = *sgs;
12737 }
12738
12739 } while (group = group->next, group != sd->groups);
12740
12741
12742 /* There is no idlest group to push tasks to */
12743 if (!idlest)
12744 return NULL;
12745
12746 /* The local group has been skipped because of CPU affinity */
12747 if (!local)
12748 return idlest;
12749
12750 /*
12751 * If the local group is idler than the selected idlest group
12752 * don't try and push the task.
12753 */
12754 if (local_sgs.group_type < idlest_sgs.group_type)
12755 return NULL;
12756
12757 /*
12758 * If the local group is busier than the selected idlest group
12759 * try and push the task.
12760 */
12761 if (local_sgs.group_type > idlest_sgs.group_type)
12762 return idlest;
12763
12764 switch (local_sgs.group_type) {
12765 case group_overloaded:
12766 case group_fully_busy:
12767
12768 /* Calculate allowed imbalance based on load */
12769 imbalance = scale_load_down(NICE_0_LOAD) *
12770 (sd->imbalance_pct-100) / 100;
12771
12772 /*
12773 * When comparing groups across NUMA domains, it's possible for
12774 * the local domain to be very lightly loaded relative to the
12775 * remote domains but "imbalance" skews the comparison making
12776 * remote CPUs look much more favourable. When considering
12777 * cross-domain, add imbalance to the load on the remote node
12778 * and consider staying local.
12779 */
12780
12781 if ((sd->flags & SD_NUMA) &&
12782 ((idlest_sgs.avg_load + imbalance) >= local_sgs.avg_load))
12783 return NULL;
12784
12785 /*
12786 * If the local group is less loaded than the selected
12787 * idlest group don't try and push any tasks.
12788 */
12789 if (idlest_sgs.avg_load >= (local_sgs.avg_load + imbalance))
12790 return NULL;
12791
12792 if (100 * local_sgs.avg_load <= sd->imbalance_pct * idlest_sgs.avg_load)
12793 return NULL;
12794 break;
12795
12796 case group_llc_balance:
12797 case group_imbalanced:
12798 case group_asym_packing:
12799 case group_smt_balance:
12800 /* Those type are not used in the slow wakeup path */
12801 return NULL;
12802
12803 case group_misfit_task:
12804 /* Select group with the highest max capacity */
12805 if (local->sgc->max_capacity >= idlest->sgc->max_capacity)
12806 return NULL;
12807 break;
12808
12809 case group_has_spare:
12810 #ifdef CONFIG_NUMA
12811 if (sd->flags & SD_NUMA) {
12812 int imb_numa_nr = sd->imb_numa_nr;
12813 #ifdef CONFIG_NUMA_BALANCING
12814 int idlest_cpu;
12815 /*
12816 * If there is spare capacity at NUMA, try to select
12817 * the preferred node
12818 */
12819 if (cpu_to_node(this_cpu) == p->numa_preferred_nid)
12820 return NULL;
12821
12822 idlest_cpu = cpumask_first(sched_group_span(idlest));
12823 if (cpu_to_node(idlest_cpu) == p->numa_preferred_nid)
12824 return idlest;
12825 #endif /* CONFIG_NUMA_BALANCING */
12826 /*
12827 * Otherwise, keep the task close to the wakeup source
12828 * and improve locality if the number of running tasks
12829 * would remain below threshold where an imbalance is
12830 * allowed while accounting for the possibility the
12831 * task is pinned to a subset of CPUs. If there is a
12832 * real need of migration, periodic load balance will
12833 * take care of it.
12834 */
12835 if (p->nr_cpus_allowed != NR_CPUS) {
12836 unsigned int w = cpumask_weight_and(p->cpus_ptr,
12837 sched_group_span(local));
12838 imb_numa_nr = min(w, sd->imb_numa_nr);
12839 }
12840
12841 imbalance = abs(local_sgs.idle_cpus - idlest_sgs.idle_cpus);
12842 if (!adjust_numa_imbalance(imbalance,
12843 local_sgs.sum_nr_running + 1,
12844 imb_numa_nr)) {
12845 return NULL;
12846 }
12847 }
12848 #endif /* CONFIG_NUMA */
12849
12850 /*
12851 * Select group with highest number of idle CPUs. We could also
12852 * compare the utilization which is more stable but it can end
12853 * up that the group has less spare capacity but finally more
12854 * idle CPUs which means more opportunity to run task.
12855 */
12856 if (local_sgs.idle_cpus >= idlest_sgs.idle_cpus)
12857 return NULL;
12858 break;
12859 }
12860
12861 return idlest;
12862 }
12863
12864 static void update_idle_cpu_scan(struct lb_env *env,
12865 unsigned long sum_util)
12866 {
12867 struct sched_domain_shared *sd_share;
12868 struct sched_domain *sd = env->sd;
12869 int llc_weight, pct;
12870 u64 x, y, tmp;
12871 /*
12872 * Update the number of CPUs to scan in LLC domain, which could
12873 * be used as a hint in select_idle_cpu(). The update of sd_share
12874 * could be expensive because it is within a shared cache line.
12875 * So the write of this hint only occurs during periodic load
12876 * balancing, rather than CPU_NEWLY_IDLE, because the latter
12877 * can fire way more frequently than the former.
12878 */
12879 if (!sched_feat(SIS_UTIL) || env->idle == CPU_NEWLY_IDLE)
12880 return;
12881
12882 sd_share = sd->shared;
12883 if (!sd_share)
12884 return;
12885
12886 /*
12887 * The number of CPUs to search drops as sum_util increases, when
12888 * sum_util hits 85% or above, the scan stops.
12889 * The reason to choose 85% as the threshold is because this is the
12890 * imbalance_pct(117) when a LLC sched group is overloaded.
12891 *
12892 * let y = SCHED_CAPACITY_SCALE - p * x^2 [1]
12893 * and y'= y / SCHED_CAPACITY_SCALE
12894 *
12895 * x is the ratio of sum_util compared to the CPU capacity:
12896 * x = sum_util / (llc_weight * SCHED_CAPACITY_SCALE)
12897 * y' is the ratio of CPUs to be scanned in the LLC domain,
12898 * and the number of CPUs to scan is calculated by:
12899 *
12900 * nr_scan = llc_weight * y' [2]
12901 *
12902 * When x hits the threshold of overloaded, AKA, when
12903 * x = 100 / pct, y drops to 0. According to [1],
12904 * p should be SCHED_CAPACITY_SCALE * pct^2 / 10000
12905 *
12906 * Scale x by SCHED_CAPACITY_SCALE:
12907 * x' = sum_util / llc_weight; [3]
12908 *
12909 * and finally [1] becomes:
12910 * y = SCHED_CAPACITY_SCALE -
12911 * x'^2 * pct^2 / (10000 * SCHED_CAPACITY_SCALE) [4]
12912 *
12913 */
12914 /* equation [3] */
12915 x = sum_util;
12916 llc_weight = sd->span_weight;
12917 do_div(x, llc_weight);
12918
12919 /* equation [4] */
12920 pct = sd->imbalance_pct;
12921 tmp = x * x * pct * pct;
12922 do_div(tmp, 10000 * SCHED_CAPACITY_SCALE);
12923 tmp = min_t(long, tmp, SCHED_CAPACITY_SCALE);
12924 y = SCHED_CAPACITY_SCALE - tmp;
12925
12926 /* equation [2] */
12927 y *= llc_weight;
12928 do_div(y, SCHED_CAPACITY_SCALE);
12929 if ((int)y != sd_share->nr_idle_scan)
12930 WRITE_ONCE(sd_share->nr_idle_scan, (int)y);
12931 }
12932
12933 /**
12934 * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
12935 * @env: The load balancing environment.
12936 * @sds: variable to hold the statistics for this sched_domain.
12937 */
12938
12939 static inline void update_sd_lb_stats(struct lb_env *env, struct sd_lb_stats *sds)
12940 {
12941 struct sched_group *sg = env->sd->groups;
12942 struct sg_lb_stats *local = &sds->local_stat;
12943 struct sg_lb_stats tmp_sgs;
12944 unsigned long sum_util = 0;
12945 bool sg_overloaded = 0, sg_overutilized = 0;
12946
12947 env->dst_core_idle = !sched_smt_active() || is_core_idle(env->dst_cpu);
12948
12949 do {
12950 struct sg_lb_stats *sgs = &tmp_sgs;
12951 int local_group;
12952
12953 local_group = cpumask_test_cpu(env->dst_cpu, sched_group_span(sg));
12954 if (local_group) {
12955 sds->local = sg;
12956 sgs = local;
12957
12958 if (env->idle != CPU_NEWLY_IDLE ||
12959 time_after_eq(jiffies, sg->sgc->next_update))
12960 update_group_capacity(env->sd, env->dst_cpu);
12961 }
12962
12963 update_sg_lb_stats(env, sds, sg, sgs, &sg_overloaded);
12964
12965 if (!local_group && update_sd_pick_busiest(env, sds, sg, sgs)) {
12966 sds->busiest = sg;
12967 sds->busiest_stat = *sgs;
12968 }
12969
12970 sg_overutilized |= sgs->group_overutilized;
12971
12972 /* Now, start updating sd_lb_stats */
12973 sds->total_load += sgs->group_load;
12974 sds->total_capacity += sgs->group_capacity;
12975
12976 sum_util += sgs->group_util;
12977 sg = sg->next;
12978 } while (sg != env->sd->groups);
12979
12980 /*
12981 * Indicate that the child domain of the busiest group prefers tasks
12982 * go to a child's sibling domains first. NB the flags of a sched group
12983 * are those of the child domain.
12984 */
12985 if (sds->busiest)
12986 sds->prefer_sibling = !!(sds->busiest->flags & SD_PREFER_SIBLING);
12987
12988
12989 if (env->sd->flags & SD_NUMA)
12990 env->fbq_type = fbq_classify_group(&sds->busiest_stat);
12991
12992 if (!env->sd->parent) {
12993 /* update overload indicator if we are at root domain */
12994 set_rd_overloaded(env->dst_rq->rd, sg_overloaded);
12995
12996 /* Update over-utilization (tipping point, U >= 0) indicator */
12997 set_rd_overutilized(env->dst_rq->rd, sg_overutilized);
12998 } else if (sg_overutilized) {
12999 set_rd_overutilized(env->dst_rq->rd, sg_overutilized);
13000 }
13001
13002 update_idle_cpu_scan(env, sum_util);
13003 }
13004
13005 /**
13006 * calculate_imbalance - Calculate the amount of imbalance present within the
13007 * groups of a given sched_domain during load balance.
13008 * @env: load balance environment
13009 * @sds: statistics of the sched_domain whose imbalance is to be calculated.
13010 */
13011 static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
13012 {
13013 struct sg_lb_stats *local, *busiest;
13014
13015 local = &sds->local_stat;
13016 busiest = &sds->busiest_stat;
13017
13018 if (busiest->group_type == group_misfit_task) {
13019 if (env->sd->flags & SD_ASYM_CPUCAPACITY) {
13020 /* Set imbalance to allow misfit tasks to be balanced. */
13021 env->migration_type = migrate_misfit;
13022 env->imbalance = 1;
13023 } else {
13024 /*
13025 * Set load imbalance to allow moving task from cpu
13026 * with reduced capacity.
13027 */
13028 env->migration_type = migrate_load;
13029 env->imbalance = busiest->group_misfit_task_load;
13030 }
13031 return;
13032 }
13033
13034 if (busiest->group_type == group_asym_packing) {
13035 /*
13036 * In case of asym capacity, we will try to migrate all load to
13037 * the preferred CPU.
13038 */
13039 env->migration_type = migrate_task;
13040 env->imbalance = busiest->sum_h_nr_running;
13041 return;
13042 }
13043
13044 if (busiest->group_type == group_smt_balance) {
13045 /* Reduce number of tasks sharing CPU capacity */
13046 env->migration_type = migrate_task;
13047 env->imbalance = 1;
13048 return;
13049 }
13050
13051 #ifdef CONFIG_SCHED_CACHE
13052 if (busiest->group_type == group_llc_balance) {
13053 /* Move a task that prefer local LLC */
13054 env->migration_type = migrate_llc_task;
13055 env->imbalance = 1;
13056 return;
13057 }
13058 #endif
13059
13060 if (busiest->group_type == group_imbalanced) {
13061 /*
13062 * In the group_imb case we cannot rely on group-wide averages
13063 * to ensure CPU-load equilibrium, try to move any task to fix
13064 * the imbalance. The next load balance will take care of
13065 * balancing back the system.
13066 */
13067 env->migration_type = migrate_task;
13068 env->imbalance = 1;
13069 return;
13070 }
13071
13072 /*
13073 * Try to use spare capacity of local group without overloading it or
13074 * emptying busiest.
13075 */
13076 if (local->group_type == group_has_spare) {
13077 if ((busiest->group_type > group_fully_busy) &&
13078 !(env->sd->flags & SD_SHARE_LLC)) {
13079 /*
13080 * If busiest is overloaded, try to fill spare
13081 * capacity. This might end up creating spare capacity
13082 * in busiest or busiest still being overloaded but
13083 * there is no simple way to directly compute the
13084 * amount of load to migrate in order to balance the
13085 * system.
13086 */
13087 env->migration_type = migrate_util;
13088 env->imbalance = max(local->group_capacity, local->group_util) -
13089 local->group_util;
13090
13091 /*
13092 * In some cases, the group's utilization is max or even
13093 * higher than capacity because of migrations but the
13094 * local CPU is (newly) idle. There is at least one
13095 * waiting task in this overloaded busiest group. Let's
13096 * try to pull it.
13097 */
13098 if (env->idle && env->imbalance == 0) {
13099 env->migration_type = migrate_task;
13100 env->imbalance = 1;
13101 }
13102
13103 return;
13104 }
13105
13106 if (busiest->group_weight == 1 || sds->prefer_sibling) {
13107 /*
13108 * When prefer sibling, evenly spread running tasks on
13109 * groups.
13110 */
13111 env->migration_type = migrate_task;
13112 env->imbalance = sibling_imbalance(env, sds, busiest, local);
13113 } else {
13114
13115 /*
13116 * If there is no overload, we just want to even the number of
13117 * idle CPUs.
13118 */
13119 env->migration_type = migrate_task;
13120 env->imbalance = max_t(long, 0,
13121 (local->idle_cpus - busiest->idle_cpus));
13122 }
13123
13124 #ifdef CONFIG_NUMA
13125 /* Consider allowing a small imbalance between NUMA groups */
13126 if (env->sd->flags & SD_NUMA) {
13127 env->imbalance = adjust_numa_imbalance(env->imbalance,
13128 local->sum_nr_running + 1,
13129 env->sd->imb_numa_nr);
13130 }
13131 #endif
13132
13133 /* Number of tasks to move to restore balance */
13134 env->imbalance >>= 1;
13135
13136 return;
13137 }
13138
13139 /*
13140 * Local is fully busy but has to take more load to relieve the
13141 * busiest group
13142 */
13143 if (local->group_type < group_overloaded) {
13144 /*
13145 * Local will become overloaded so the avg_load metrics are
13146 * finally needed.
13147 */
13148
13149 local->avg_load = (local->group_load * SCHED_CAPACITY_SCALE) /
13150 local->group_capacity;
13151
13152 /*
13153 * If the local group is more loaded than the selected
13154 * busiest group don't try to pull any tasks.
13155 */
13156 if (local->avg_load >= busiest->avg_load) {
13157 env->imbalance = 0;
13158 return;
13159 }
13160
13161 sds->avg_load = (sds->total_load * SCHED_CAPACITY_SCALE) /
13162 sds->total_capacity;
13163
13164 /*
13165 * If the local group is more loaded than the average system
13166 * load, don't try to pull any tasks.
13167 */
13168 if (local->avg_load >= sds->avg_load) {
13169 env->imbalance = 0;
13170 return;
13171 }
13172
13173 }
13174
13175 /*
13176 * Both group are or will become overloaded and we're trying to get all
13177 * the CPUs to the average_load, so we don't want to push ourselves
13178 * above the average load, nor do we wish to reduce the max loaded CPU
13179 * below the average load. At the same time, we also don't want to
13180 * reduce the group load below the group capacity. Thus we look for
13181 * the minimum possible imbalance.
13182 */
13183 env->migration_type = migrate_load;
13184 env->imbalance = min(
13185 (busiest->avg_load - sds->avg_load) * busiest->group_capacity,
13186 (sds->avg_load - local->avg_load) * local->group_capacity
13187 ) / SCHED_CAPACITY_SCALE;
13188 }
13189
13190 /******* sched_balance_find_src_group() helpers end here *********************/
13191
13192 /*
13193 * Decision matrix according to the local and busiest group type:
13194 *
13195 * busiest \ local has_spare fully_busy misfit asym imbalanced overloaded
13196 * has_spare nr_idle balanced N/A N/A balanced balanced
13197 * fully_busy nr_idle nr_idle N/A N/A balanced balanced
13198 * misfit_task force N/A N/A N/A N/A N/A
13199 * asym_packing force force N/A N/A force force
13200 * imbalanced force force N/A N/A force force
13201 * overloaded force force N/A N/A force avg_load
13202 *
13203 * N/A : Not Applicable because already filtered while updating
13204 * statistics.
13205 * balanced : The system is balanced for these 2 groups.
13206 * force : Calculate the imbalance as load migration is probably needed.
13207 * avg_load : Only if imbalance is significant enough.
13208 * nr_idle : dst_cpu is not busy and the number of idle CPUs is quite
13209 * different in groups.
13210 */
13211
13212 /**
13213 * sched_balance_find_src_group - Returns the busiest group within the sched_domain
13214 * if there is an imbalance.
13215 * @env: The load balancing environment.
13216 *
13217 * Also calculates the amount of runnable load which should be moved
13218 * to restore balance.
13219 *
13220 * Return: - The busiest group if imbalance exists.
13221 */
13222 static struct sched_group *sched_balance_find_src_group(struct lb_env *env)
13223 {
13224 struct sg_lb_stats *local, *busiest;
13225 struct sd_lb_stats sds;
13226
13227 init_sd_lb_stats(&sds);
13228
13229 /*
13230 * Compute the various statistics relevant for load balancing at
13231 * this level.
13232 */
13233 update_sd_lb_stats(env, &sds);
13234
13235 /* There is no busy sibling group to pull tasks from */
13236 if (!sds.busiest)
13237 goto out_balanced;
13238
13239 busiest = &sds.busiest_stat;
13240
13241 /* Misfit tasks should be dealt with regardless of the avg load */
13242 if (busiest->group_type == group_misfit_task)
13243 goto force_balance;
13244
13245 if (!is_rd_overutilized(env->dst_rq->rd) &&
13246 rcu_dereference_all(env->dst_rq->rd->pd))
13247 goto out_balanced;
13248
13249 /* ASYM feature bypasses nice load balance check */
13250 if (busiest->group_type == group_asym_packing)
13251 goto force_balance;
13252
13253 /*
13254 * If the busiest group is imbalanced the below checks don't
13255 * work because they assume all things are equal, which typically
13256 * isn't true due to cpus_ptr constraints and the like.
13257 */
13258 if (busiest->group_type == group_imbalanced)
13259 goto force_balance;
13260
13261 local = &sds.local_stat;
13262 /*
13263 * If the local group is busier than the selected busiest group
13264 * don't try and pull any tasks.
13265 */
13266 if (local->group_type > busiest->group_type)
13267 goto out_balanced;
13268
13269 /*
13270 * When groups are overloaded, use the avg_load to ensure fairness
13271 * between tasks.
13272 */
13273 if (local->group_type == group_overloaded) {
13274 /*
13275 * If the local group is more loaded than the selected
13276 * busiest group don't try to pull any tasks.
13277 */
13278 if (local->avg_load >= busiest->avg_load)
13279 goto out_balanced;
13280
13281 /* XXX broken for overlapping NUMA groups */
13282 sds.avg_load = (sds.total_load * SCHED_CAPACITY_SCALE) /
13283 sds.total_capacity;
13284
13285 /*
13286 * Don't pull any tasks if this group is already above the
13287 * domain average load.
13288 */
13289 if (local->avg_load >= sds.avg_load)
13290 goto out_balanced;
13291
13292 /*
13293 * If the busiest group is more loaded, use imbalance_pct to be
13294 * conservative.
13295 */
13296 if (100 * busiest->avg_load <=
13297 env->sd->imbalance_pct * local->avg_load)
13298 goto out_balanced;
13299 }
13300
13301 /*
13302 * Try to move all excess tasks to a sibling domain of the busiest
13303 * group's child domain.
13304 */
13305 if (sds.prefer_sibling && local->group_type == group_has_spare &&
13306 (busiest->group_type == group_llc_balance ||
13307 sibling_imbalance(env, &sds, busiest, local) > 1))
13308 goto force_balance;
13309
13310 if (busiest->group_type != group_overloaded) {
13311 if (!env->idle) {
13312 /*
13313 * If the busiest group is not overloaded (and as a
13314 * result the local one too) but this CPU is already
13315 * busy, let another idle CPU try to pull task.
13316 */
13317 goto out_balanced;
13318 }
13319
13320 if (busiest->group_type == group_smt_balance &&
13321 smt_vs_nonsmt_groups(sds.local, sds.busiest)) {
13322 /* Let non SMT CPU pull from SMT CPU sharing with sibling */
13323 goto force_balance;
13324 }
13325
13326 if (busiest->group_weight > 1 &&
13327 local->idle_cpus <= (busiest->idle_cpus + 1)) {
13328 /*
13329 * If the busiest group is not overloaded
13330 * and there is no imbalance between this and busiest
13331 * group wrt idle CPUs, it is balanced. The imbalance
13332 * becomes significant if the diff is greater than 1
13333 * otherwise we might end up to just move the imbalance
13334 * on another group. Of course this applies only if
13335 * there is more than 1 CPU per group.
13336 */
13337 goto out_balanced;
13338 }
13339
13340 if (busiest->sum_h_nr_running == 1) {
13341 /*
13342 * busiest doesn't have any tasks waiting to run
13343 */
13344 goto out_balanced;
13345 }
13346 }
13347
13348 force_balance:
13349 /* Looks like there is an imbalance. Compute it */
13350 calculate_imbalance(env, &sds);
13351 return env->imbalance ? sds.busiest : NULL;
13352
13353 out_balanced:
13354 env->imbalance = 0;
13355 return NULL;
13356 }
13357
13358 /*
13359 * sched_balance_find_src_rq - find the busiest runqueue among the CPUs in the group.
13360 */
13361 static struct rq *sched_balance_find_src_rq(struct lb_env *env,
13362 struct sched_group *group)
13363 {
13364 struct rq *busiest = NULL, *rq;
13365 unsigned long busiest_util = 0, busiest_load = 0, busiest_capacity = 1;
13366 unsigned int __maybe_unused busiest_pref_llc = 0;
13367 struct sched_domain __maybe_unused *sd_tmp;
13368 unsigned int busiest_nr = 0;
13369 int __maybe_unused dst_llc;
13370 int i;
13371
13372 for_each_cpu_and(i, sched_group_span(group), env->cpus) {
13373 unsigned long capacity, load, util;
13374 unsigned int nr_running;
13375 enum fbq_type rt;
13376
13377 rq = cpu_rq(i);
13378 rt = fbq_classify_rq(rq);
13379
13380 /*
13381 * We classify groups/runqueues into three groups:
13382 * - regular: there are !numa tasks
13383 * - remote: there are numa tasks that run on the 'wrong' node
13384 * - all: there is no distinction
13385 *
13386 * In order to avoid migrating ideally placed numa tasks,
13387 * ignore those when there's better options.
13388 *
13389 * If we ignore the actual busiest queue to migrate another
13390 * task, the next balance pass can still reduce the busiest
13391 * queue by moving tasks around inside the node.
13392 *
13393 * If we cannot move enough load due to this classification
13394 * the next pass will adjust the group classification and
13395 * allow migration of more tasks.
13396 *
13397 * Both cases only affect the total convergence complexity.
13398 */
13399 if (rt > env->fbq_type)
13400 continue;
13401
13402 nr_running = rq->cfs.h_nr_runnable;
13403 if (!nr_running)
13404 continue;
13405
13406 capacity = capacity_of(i);
13407
13408 /*
13409 * For ASYM_CPUCAPACITY domains, don't pick a CPU that could
13410 * eventually lead to active_balancing high->low capacity.
13411 * Higher per-CPU capacity is considered better than balancing
13412 * average load.
13413 */
13414 if (env->sd->flags & SD_ASYM_CPUCAPACITY &&
13415 nr_running == 1) {
13416 bool cluster_equal_cap = static_branch_unlikely(&sched_cluster_active) &&
13417 (get_actual_cpu_capacity(env->dst_cpu) ==
13418 get_actual_cpu_capacity(i));
13419 bool smt_degraded_cap = sched_smt_active() && !is_core_idle(i);
13420
13421 /*
13422 * Busy SMT siblings reduce the capacity of CPU @i. Do
13423 * not skip it in this case.
13424 *
13425 * CONFIG_SCHED_CLUSTER requires balancing load across
13426 * clusters of identical capacity, accounting for
13427 * hardware and cpufreq pressure.
13428 */
13429 if (!smt_degraded_cap && !cluster_equal_cap &&
13430 !capacity_greater(capacity_of(env->dst_cpu), capacity))
13431 continue;
13432 }
13433
13434 /*
13435 * Make sure we only pull tasks from a CPU of lower priority
13436 * when balancing between SMT siblings.
13437 *
13438 * If balancing between cores, let lower priority CPUs help
13439 * SMT cores with more than one busy sibling.
13440 */
13441 if (sched_asym(env->sd, i, env->dst_cpu) && nr_running == 1)
13442 continue;
13443
13444 switch (env->migration_type) {
13445 case migrate_load:
13446 /*
13447 * When comparing with load imbalance, use cpu_load()
13448 * which is not scaled with the CPU capacity.
13449 */
13450 load = cpu_load(rq);
13451
13452 if (nr_running == 1 && load > env->imbalance &&
13453 !check_cpu_capacity(rq, env->sd))
13454 break;
13455
13456 /*
13457 * For the load comparisons with the other CPUs,
13458 * consider the cpu_load() scaled with the CPU
13459 * capacity, so that the load can be moved away
13460 * from the CPU that is potentially running at a
13461 * lower capacity.
13462 *
13463 * Thus we're looking for max(load_i / capacity_i),
13464 * crosswise multiplication to rid ourselves of the
13465 * division works out to:
13466 * load_i * capacity_j > load_j * capacity_i;
13467 * where j is our previous maximum.
13468 */
13469 if (load * busiest_capacity > busiest_load * capacity) {
13470 busiest_load = load;
13471 busiest_capacity = capacity;
13472 busiest = rq;
13473 }
13474 break;
13475
13476 case migrate_util:
13477 util = cpu_util_cfs_boost(i);
13478
13479 /*
13480 * Don't try to pull utilization from a CPU with one
13481 * running task. Whatever its utilization, we will fail
13482 * detach the task.
13483 */
13484 if (nr_running <= 1)
13485 continue;
13486
13487 if (busiest_util < util) {
13488 busiest_util = util;
13489 busiest = rq;
13490 }
13491 break;
13492
13493 case migrate_task:
13494 if (busiest_nr < nr_running) {
13495 busiest_nr = nr_running;
13496 busiest = rq;
13497 }
13498 break;
13499
13500 case migrate_misfit:
13501 /*
13502 * For ASYM_CPUCAPACITY domains with misfit tasks we
13503 * simply seek the "biggest" misfit task.
13504 */
13505 if (rq->misfit_task_load > busiest_load) {
13506 busiest_load = rq->misfit_task_load;
13507 busiest = rq;
13508 }
13509
13510 break;
13511
13512 case migrate_llc_task:
13513 #ifdef CONFIG_SCHED_CACHE
13514 sd_tmp = rcu_dereference_all(rq->sd);
13515 dst_llc = llc_id(env->dst_cpu);
13516
13517 if (sd_tmp && (unsigned)dst_llc < sd_tmp->llc_max) {
13518 unsigned int this_pref_llc =
13519 sd_tmp->llc_counts[dst_llc];
13520
13521 if (busiest_pref_llc < this_pref_llc) {
13522 busiest_pref_llc = this_pref_llc;
13523 busiest = rq;
13524 }
13525 }
13526 #endif
13527 break;
13528
13529 }
13530 }
13531
13532 return busiest;
13533 }
13534
13535 /*
13536 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
13537 * so long as it is large enough.
13538 */
13539 #define MAX_PINNED_INTERVAL 512
13540
13541 static inline bool
13542 asym_active_balance(struct lb_env *env)
13543 {
13544 /*
13545 * ASYM_PACKING needs to force migrate tasks from busy but lower
13546 * priority CPUs in order to pack all tasks in the highest priority
13547 * CPUs. When done between cores, do it only if the whole core if the
13548 * whole core is idle.
13549 *
13550 * If @env::src_cpu is an SMT core with busy siblings, let
13551 * the lower priority @env::dst_cpu help it. Do not follow
13552 * CPU priority.
13553 */
13554 return env->idle && sched_use_asym_prio(env->sd, env->dst_cpu) &&
13555 (sched_asym_prefer(env->dst_cpu, env->src_cpu) ||
13556 !sched_use_asym_prio(env->sd, env->src_cpu));
13557 }
13558
13559 static inline bool
13560 imbalanced_active_balance(struct lb_env *env)
13561 {
13562 struct sched_domain *sd = env->sd;
13563
13564 /*
13565 * The imbalanced case includes the case of pinned tasks preventing a fair
13566 * distribution of the load on the system but also the even distribution of the
13567 * threads on a system with spare capacity
13568 */
13569 if ((env->migration_type == migrate_task) &&
13570 (sd->nr_balance_failed > sd->cache_nice_tries+2))
13571 return 1;
13572
13573 return 0;
13574 }
13575
13576 static int need_active_balance(struct lb_env *env)
13577 {
13578 struct sched_domain *sd = env->sd;
13579
13580 if (alb_break_llc(env))
13581 return 0;
13582
13583 if (asym_active_balance(env))
13584 return 1;
13585
13586 if (imbalanced_active_balance(env))
13587 return 1;
13588
13589 /*
13590 * The dst_cpu is idle and the src_cpu CPU has only 1 CFS task.
13591 * It's worth migrating the task if the src_cpu's capacity is reduced
13592 * because of other sched_class or IRQs if more capacity stays
13593 * available on dst_cpu.
13594 */
13595 if (env->idle &&
13596 (env->src_rq->cfs.h_nr_runnable == 1)) {
13597 if ((check_cpu_capacity(env->src_rq, sd)) &&
13598 (capacity_of(env->src_cpu)*sd->imbalance_pct < capacity_of(env->dst_cpu)*100))
13599 return 1;
13600 }
13601
13602 if (env->migration_type == migrate_misfit ||
13603 env->migration_type == migrate_llc_task)
13604 return 1;
13605
13606 return 0;
13607 }
13608
13609 static int active_load_balance_cpu_stop(void *data);
13610 static int active_load_balance_llc_cpu_stop(void *data);
13611
13612 /*
13613 * migration_type is checked elsewhere to decide migration policy, so
13614 * it shouldn't be repurposed just to flag an LLC-directed active
13615 * balance across the stopper. Pick the callback here instead.
13616 */
13617 static inline cpu_stop_fn_t alb_stop_fn(struct lb_env *env)
13618 {
13619 if (env->migration_type == migrate_llc_task)
13620 return active_load_balance_llc_cpu_stop;
13621
13622 return active_load_balance_cpu_stop;
13623 }
13624
13625 static int should_we_balance(struct lb_env *env)
13626 {
13627 struct cpumask *swb_cpus = this_cpu_cpumask_var_ptr(should_we_balance_tmpmask);
13628 struct sched_group *sg = env->sd->groups;
13629 int cpu, idle_smt = -1;
13630
13631 /*
13632 * Ensure the balancing environment is consistent; can happen
13633 * when the softirq triggers 'during' hotplug.
13634 */
13635 if (!cpumask_test_cpu(env->dst_cpu, env->cpus))
13636 return 0;
13637
13638 /*
13639 * In the newly idle case, we will allow all the CPUs
13640 * to do the newly idle load balance.
13641 *
13642 * However, we bail out if we already have tasks or a wakeup pending,
13643 * to optimize wakeup latency.
13644 */
13645 if (env->idle == CPU_NEWLY_IDLE) {
13646 if (env->dst_rq->nr_running > 0 || env->dst_rq->ttwu_pending)
13647 return 0;
13648 return 1;
13649 }
13650
13651 cpumask_copy(swb_cpus, group_balance_mask(sg));
13652 /* Try to find first idle CPU */
13653 for_each_cpu_and(cpu, swb_cpus, env->cpus) {
13654 if (!idle_cpu(cpu))
13655 continue;
13656
13657 /*
13658 * Don't balance to idle SMT in busy core right away when
13659 * balancing cores, but remember the first idle SMT CPU for
13660 * later consideration. Find CPU on an idle core first.
13661 */
13662 if (sched_smt_active() &&
13663 !(env->sd->flags & SD_SHARE_CPUCAPACITY) &&
13664 !is_core_idle(cpu)) {
13665 if (idle_smt == -1)
13666 idle_smt = cpu;
13667 /*
13668 * If the core is not idle, and first SMT sibling which is
13669 * idle has been found, then its not needed to check other
13670 * SMT siblings for idleness:
13671 */
13672 cpumask_andnot(swb_cpus, swb_cpus, cpu_smt_mask(cpu));
13673 continue;
13674 }
13675
13676 /*
13677 * Are we the first idle core in a non-SMT domain or higher,
13678 * or the first idle CPU in a SMT domain?
13679 */
13680 return cpu == env->dst_cpu;
13681 }
13682
13683 /* Are we the first idle CPU with busy siblings? */
13684 if (idle_smt != -1)
13685 return idle_smt == env->dst_cpu;
13686
13687 /* Are we the first CPU of this group ? */
13688 return group_balance_cpu(sg) == env->dst_cpu;
13689 }
13690
13691 static void update_lb_imbalance_stat(struct lb_env *env, struct sched_domain *sd,
13692 enum cpu_idle_type idle)
13693 {
13694 if (!schedstat_enabled())
13695 return;
13696
13697 switch (env->migration_type) {
13698 case migrate_load:
13699 __schedstat_add(sd->lb_imbalance_load[idle], env->imbalance);
13700 break;
13701 case migrate_util:
13702 __schedstat_add(sd->lb_imbalance_util[idle], env->imbalance);
13703 break;
13704 case migrate_task:
13705 __schedstat_add(sd->lb_imbalance_task[idle], env->imbalance);
13706 break;
13707 case migrate_misfit:
13708 __schedstat_add(sd->lb_imbalance_misfit[idle], env->imbalance);
13709 break;
13710 case migrate_llc_task:
13711 break;
13712 }
13713 }
13714
13715 /*
13716 * This flag serializes load-balancing passes over large domains
13717 * (above the NODE topology level) - only one load-balancing instance
13718 * may run at a time, to reduce overhead on very large systems with
13719 * lots of CPUs and large NUMA distances.
13720 *
13721 * - Note that load-balancing passes triggered while another one
13722 * is executing are skipped and not re-tried.
13723 *
13724 * - Also note that this does not serialize rebalance_domains()
13725 * execution, as non-SD_SERIALIZE domains will still be
13726 * load-balanced in parallel.
13727 */
13728 static atomic_t sched_balance_running = ATOMIC_INIT(0);
13729
13730 /*
13731 * Check this_cpu to ensure it is balanced within domain. Attempt to move
13732 * tasks if there is an imbalance.
13733 */
13734 static int sched_balance_rq(int this_cpu, struct rq *this_rq,
13735 struct sched_domain *sd, enum cpu_idle_type idle,
13736 int *continue_balancing)
13737 {
13738 int ld_moved, cur_ld_moved, active_balance = 0;
13739 struct sched_domain *sd_parent = sd->parent;
13740 struct sched_group *group;
13741 struct rq *busiest;
13742 struct rq_flags rf;
13743 struct cpumask *cpus = this_cpu_cpumask_var_ptr(load_balance_mask);
13744 struct lb_env env = {
13745 .sd = sd,
13746 .dst_cpu = this_cpu,
13747 .dst_rq = this_rq,
13748 .dst_grpmask = group_balance_mask(sd->groups),
13749 .idle = idle,
13750 .loop_break = SCHED_NR_MIGRATE_BREAK,
13751 .cpus = cpus,
13752 .fbq_type = all,
13753 .tasks = LIST_HEAD_INIT(env.tasks),
13754 };
13755 bool need_unlock = false;
13756
13757 cpumask_and(cpus, sched_domain_span(sd), cpu_active_mask);
13758
13759 schedstat_inc(sd->lb_count[idle]);
13760
13761 redo:
13762 if (!should_we_balance(&env)) {
13763 *continue_balancing = 0;
13764 goto out_balanced;
13765 }
13766
13767 if (!need_unlock && (sd->flags & SD_SERIALIZE)) {
13768 int zero = 0;
13769 if (!atomic_try_cmpxchg_acquire(&sched_balance_running, &zero, 1))
13770 goto out_balanced;
13771
13772 need_unlock = true;
13773 }
13774
13775 group = sched_balance_find_src_group(&env);
13776 if (!group) {
13777 schedstat_inc(sd->lb_nobusyg[idle]);
13778 goto out_balanced;
13779 }
13780
13781 busiest = sched_balance_find_src_rq(&env, group);
13782 if (!busiest) {
13783 schedstat_inc(sd->lb_nobusyq[idle]);
13784 goto out_balanced;
13785 }
13786
13787 WARN_ON_ONCE(busiest == env.dst_rq);
13788
13789 update_lb_imbalance_stat(&env, sd, idle);
13790
13791 env.src_cpu = busiest->cpu;
13792 env.src_rq = busiest;
13793
13794 ld_moved = 0;
13795 /* Clear this flag as soon as we find a pullable task */
13796 env.flags |= LBF_ALL_PINNED;
13797 if (busiest->nr_running > 1) {
13798 /*
13799 * Attempt to move tasks. If sched_balance_find_src_group has found
13800 * an imbalance but busiest->nr_running <= 1, the group is
13801 * still unbalanced. ld_moved simply stays zero, so it is
13802 * correctly treated as an imbalance.
13803 */
13804 env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running);
13805
13806 more_balance:
13807 rq_lock_irqsave(busiest, &rf);
13808 update_rq_clock(busiest);
13809
13810 /*
13811 * cur_ld_moved - load moved in current iteration
13812 * ld_moved - cumulative load moved across iterations
13813 */
13814 cur_ld_moved = detach_tasks(&env);
13815
13816 /*
13817 * We've detached some tasks from busiest_rq. Every
13818 * task is masked "TASK_ON_RQ_MIGRATING", so we can safely
13819 * unlock busiest->lock, and we are able to be sure
13820 * that nobody can manipulate the tasks in parallel.
13821 * See task_rq_lock() family for the details.
13822 */
13823
13824 rq_unlock(busiest, &rf);
13825
13826 if (cur_ld_moved) {
13827 attach_tasks(&env);
13828 ld_moved += cur_ld_moved;
13829 }
13830
13831 local_irq_restore(rf.flags);
13832
13833 if (env.flags & LBF_NEED_BREAK) {
13834 env.flags &= ~LBF_NEED_BREAK;
13835 goto more_balance;
13836 }
13837
13838 /*
13839 * Revisit (affine) tasks on src_cpu that couldn't be moved to
13840 * us and move them to an alternate dst_cpu in our sched_group
13841 * where they can run. The upper limit on how many times we
13842 * iterate on same src_cpu is dependent on number of CPUs in our
13843 * sched_group.
13844 *
13845 * This changes load balance semantics a bit on who can move
13846 * load to a given_cpu. In addition to the given_cpu itself
13847 * (or a ilb_cpu acting on its behalf where given_cpu is
13848 * nohz-idle), we now have balance_cpu in a position to move
13849 * load to given_cpu. In rare situations, this may cause
13850 * conflicts (balance_cpu and given_cpu/ilb_cpu deciding
13851 * _independently_ and at _same_ time to move some load to
13852 * given_cpu) causing excess load to be moved to given_cpu.
13853 * This however should not happen so much in practice and
13854 * moreover subsequent load balance cycles should correct the
13855 * excess load moved.
13856 */
13857 if ((env.flags & LBF_DST_PINNED) && env.imbalance > 0) {
13858
13859 /* Prevent to re-select dst_cpu via env's CPUs */
13860 __cpumask_clear_cpu(env.dst_cpu, env.cpus);
13861
13862 env.dst_rq = cpu_rq(env.new_dst_cpu);
13863 env.dst_cpu = env.new_dst_cpu;
13864 env.flags &= ~LBF_DST_PINNED;
13865 env.loop = 0;
13866 env.loop_break = SCHED_NR_MIGRATE_BREAK;
13867
13868 /*
13869 * Go back to "more_balance" rather than "redo" since we
13870 * need to continue with same src_cpu.
13871 */
13872 goto more_balance;
13873 }
13874
13875 /*
13876 * We failed to reach balance because of affinity.
13877 */
13878 if (sd_parent) {
13879 int *group_imbalance = &sd_parent->groups->sgc->imbalance;
13880
13881 if ((env.flags & LBF_SOME_PINNED) && env.imbalance > 0)
13882 *group_imbalance = 1;
13883 }
13884
13885 /* All tasks on this runqueue were pinned by CPU affinity */
13886 if (unlikely(env.flags & LBF_ALL_PINNED)) {
13887 __cpumask_clear_cpu(cpu_of(busiest), cpus);
13888 /*
13889 * Attempting to continue load balancing at the current
13890 * sched_domain level only makes sense if there are
13891 * active CPUs remaining as possible busiest CPUs to
13892 * pull load from which are not contained within the
13893 * destination group that is receiving any migrated
13894 * load.
13895 */
13896 if (!cpumask_subset(cpus, env.dst_grpmask)) {
13897 env.loop = 0;
13898 env.loop_break = SCHED_NR_MIGRATE_BREAK;
13899 goto redo;
13900 }
13901 goto out_all_pinned;
13902 }
13903 }
13904
13905 if (ld_moved) {
13906 sd->nr_balance_failed = 0;
13907 goto out_unbalanced;
13908 }
13909
13910 schedstat_inc(sd->lb_failed[idle]);
13911 /*
13912 * Increment the failure counter only on periodic balance.
13913 * We do not want newidle balance, which can be very
13914 * frequent, pollute the failure counter causing
13915 * excessive cache_hot migrations and active balances.
13916 *
13917 * Similarly for migration_misfit which is not related to
13918 * load/util migration, don't pollute nr_balance_failed.
13919 *
13920 * The same for cache aware scheduling's allowance for
13921 * load imbalance. If regular load balance does not
13922 * migrate task due to LLC locality, it is a expected
13923 * behavior and don't pollute nr_balance_failed.
13924 * See can_migrate_task().
13925 */
13926 if (idle != CPU_NEWLY_IDLE &&
13927 env.migration_type != migrate_misfit &&
13928 !(env.flags & LBF_LLC_PINNED))
13929 sd->nr_balance_failed++;
13930
13931 if (!need_active_balance(&env))
13932 goto out_unbalanced;
13933
13934 scoped_guard (raw_spin_rq_lock_irqsave, busiest) {
13935 /*
13936 * Don't kick the active_load_balance_cpu_stop,
13937 * if the curr task on busiest CPU can't be
13938 * moved to this_cpu:
13939 */
13940 if (!cpumask_test_cpu(this_cpu, busiest->curr->cpus_ptr))
13941 goto out_one_pinned;
13942
13943 /* Record that we found at least one task that could run on this_cpu */
13944 env.flags &= ~LBF_ALL_PINNED;
13945
13946 /*
13947 * ->active_balance synchronizes accesses to
13948 * ->active_balance_work. Once set, it's cleared
13949 * only after active load balance is finished.
13950 */
13951 if (busiest->active_balance)
13952 goto out_unbalanced;
13953
13954 /*
13955 * @busiest dropped its rq_lock in the middle of
13956 * scheduling out its ->curr task (->on_rq := 0), no
13957 * need to forcefully punt it away with active balance.
13958 */
13959 if (!busiest->curr->on_rq)
13960 goto out_unbalanced;
13961
13962 busiest->active_balance = 1;
13963 busiest->push_cpu = this_cpu;
13964 active_balance = 1;
13965 preempt_disable();
13966 }
13967 if (active_balance) {
13968 stop_one_cpu_nowait(cpu_of(busiest),
13969 alb_stop_fn(&env), busiest,
13970 &busiest->active_balance_work);
13971 }
13972 preempt_enable();
13973
13974 out_unbalanced:
13975 /* We were unbalanced, so reset the balancing interval */
13976 sd->balance_interval = sd->min_interval;
13977 goto out;
13978
13979 out_balanced:
13980 /*
13981 * We reach balance although we may have faced some affinity
13982 * constraints. Clear the imbalance flag only if other tasks got
13983 * a chance to move and fix the imbalance.
13984 */
13985 if (sd_parent && !(env.flags & LBF_ALL_PINNED)) {
13986 int *group_imbalance = &sd_parent->groups->sgc->imbalance;
13987
13988 if (*group_imbalance)
13989 *group_imbalance = 0;
13990 }
13991
13992 out_all_pinned:
13993 /*
13994 * We reach balance because all tasks are pinned at this level so
13995 * we can't migrate them. Let the imbalance flag set so parent level
13996 * can try to migrate them.
13997 */
13998 schedstat_inc(sd->lb_balanced[idle]);
13999
14000 sd->nr_balance_failed = 0;
14001
14002 out_one_pinned:
14003 ld_moved = 0;
14004
14005 /*
14006 * sched_balance_newidle() disregards balance intervals, so we could
14007 * repeatedly reach this code, which would lead to balance_interval
14008 * skyrocketing in a short amount of time. Skip the balance_interval
14009 * increase logic to avoid that.
14010 *
14011 * Similarly misfit migration which is not necessarily an indication of
14012 * the system being busy and requires lb to backoff to let it settle
14013 * down.
14014 */
14015 if (env.idle == CPU_NEWLY_IDLE ||
14016 env.migration_type == migrate_misfit)
14017 goto out;
14018
14019 /* tune up the balancing interval */
14020 if ((env.flags & LBF_ALL_PINNED &&
14021 sd->balance_interval < MAX_PINNED_INTERVAL) ||
14022 sd->balance_interval < sd->max_interval)
14023 sd->balance_interval *= 2;
14024 out:
14025 if (need_unlock)
14026 atomic_set_release(&sched_balance_running, 0);
14027
14028 return ld_moved;
14029 }
14030
14031 static inline unsigned long
14032 get_sd_balance_interval(struct sched_domain *sd, int cpu_busy)
14033 {
14034 unsigned long interval = sd->balance_interval;
14035
14036 if (cpu_busy)
14037 interval *= sd->busy_factor;
14038
14039 /* scale ms to jiffies */
14040 interval = msecs_to_jiffies(interval);
14041
14042 /*
14043 * Reduce likelihood of busy balancing at higher domains racing with
14044 * balancing at lower domains by preventing their balancing periods
14045 * from being multiples of each other.
14046 */
14047 if (cpu_busy)
14048 interval -= 1;
14049
14050 interval = clamp(interval, 1UL, max_load_balance_interval);
14051
14052 return interval;
14053 }
14054
14055 static inline void
14056 update_next_balance(struct sched_domain *sd, unsigned long *next_balance)
14057 {
14058 unsigned long interval, next;
14059
14060 /* used by idle balance, so cpu_busy = 0 */
14061 interval = get_sd_balance_interval(sd, 0);
14062 next = sd->last_balance + interval;
14063
14064 if (time_after(*next_balance, next))
14065 *next_balance = next;
14066 }
14067
14068 /*
14069 * active_load_balance_cpu_stop is run by the CPU stopper. It pushes
14070 * running tasks off the busiest CPU onto idle CPUs. It requires at
14071 * least 1 task to be running on each physical CPU where possible, and
14072 * avoids physical / logical imbalances.
14073 */
14074 static int __active_load_balance_cpu_stop(void *data, unsigned int lb_flags)
14075 {
14076 struct rq *busiest_rq = data;
14077 int busiest_cpu = cpu_of(busiest_rq);
14078 int target_cpu = busiest_rq->push_cpu;
14079 struct rq *target_rq = cpu_rq(target_cpu);
14080 struct sched_domain *sd;
14081 struct task_struct *p = NULL;
14082 struct rq_flags rf;
14083
14084 rq_lock_irq(busiest_rq, &rf);
14085 /*
14086 * Between queueing the stop-work and running it is a hole in which
14087 * CPUs can become inactive. We should not move tasks from or to
14088 * inactive CPUs.
14089 */
14090 if (!cpu_active(busiest_cpu) || !cpu_active(target_cpu))
14091 goto out_unlock;
14092
14093 /* Make sure the requested CPU hasn't gone down in the meantime: */
14094 if (unlikely(busiest_cpu != smp_processor_id() ||
14095 !busiest_rq->active_balance))
14096 goto out_unlock;
14097
14098 /* Is there any task to move? */
14099 if (busiest_rq->nr_running <= 1)
14100 goto out_unlock;
14101
14102 /*
14103 * This condition is "impossible", if it occurs
14104 * we need to fix it. Originally reported by
14105 * Bjorn Helgaas on a 128-CPU setup.
14106 */
14107 WARN_ON_ONCE(busiest_rq == target_rq);
14108
14109 /* Search for an sd spanning us and the target CPU. */
14110 rcu_read_lock();
14111 for_each_domain(target_cpu, sd) {
14112 if (cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
14113 break;
14114 }
14115
14116 if (likely(sd)) {
14117 struct lb_env env = {
14118 .sd = sd,
14119 .dst_cpu = target_cpu,
14120 .dst_rq = target_rq,
14121 .src_cpu = busiest_rq->cpu,
14122 .src_rq = busiest_rq,
14123 .idle = CPU_IDLE,
14124 .flags = LBF_ACTIVE_LB | lb_flags,
14125 };
14126
14127 schedstat_inc(sd->alb_count);
14128 update_rq_clock(busiest_rq);
14129
14130 p = detach_one_task(&env);
14131 if (p) {
14132 schedstat_inc(sd->alb_pushed);
14133 /* Active balancing done, reset the failure counter. */
14134 sd->nr_balance_failed = 0;
14135 } else {
14136 schedstat_inc(sd->alb_failed);
14137 }
14138 }
14139 rcu_read_unlock();
14140 out_unlock:
14141 busiest_rq->active_balance = 0;
14142 rq_unlock(busiest_rq, &rf);
14143
14144 if (p)
14145 attach_one_task(target_rq, p);
14146
14147 local_irq_enable();
14148
14149 return 0;
14150 }
14151
14152 static int active_load_balance_cpu_stop(void *data)
14153 {
14154 return __active_load_balance_cpu_stop(data, 0);
14155 }
14156
14157 static int active_load_balance_llc_cpu_stop(void *data)
14158 {
14159 return __active_load_balance_cpu_stop(data, LBF_ACTIVE_LB_LLC);
14160 }
14161
14162 /*
14163 * Scale the max sched_balance_rq interval with the number of CPUs in the system.
14164 * This trades load-balance latency on larger machines for less cross talk.
14165 */
14166 void update_max_interval(void)
14167 {
14168 max_load_balance_interval = HZ*num_online_cpus()/10;
14169 }
14170
14171 static inline void update_newidle_stats(struct sched_domain *sd, unsigned int success)
14172 {
14173 sd->newidle_call++;
14174 sd->newidle_success += success;
14175
14176 if (sd->newidle_call >= 1024) {
14177 u64 now = sched_clock();
14178 s64 delta = now - sd->newidle_stamp;
14179 sd->newidle_stamp = now;
14180 int ratio = 0;
14181
14182 if (delta < 0)
14183 delta = 0;
14184
14185 if (sched_feat(NI_RATE)) {
14186 /*
14187 * ratio delta freq
14188 *
14189 * 1024 - 4 s - 128 Hz
14190 * 512 - 2 s - 256 Hz
14191 * 256 - 1 s - 512 Hz
14192 * 128 - .5 s - 1024 Hz
14193 * 64 - .25 s - 2048 Hz
14194 */
14195 ratio = delta >> 22;
14196 }
14197
14198 ratio += sd->newidle_success;
14199
14200 sd->newidle_ratio = min(1024, ratio);
14201 sd->newidle_call /= 2;
14202 sd->newidle_success /= 2;
14203 }
14204 }
14205
14206 static inline bool
14207 update_newidle_cost(struct sched_domain *sd, u64 cost, unsigned int success)
14208 {
14209 unsigned long next_decay = sd->last_decay_max_lb_cost + HZ;
14210 unsigned long now = jiffies;
14211
14212 if (cost)
14213 update_newidle_stats(sd, success);
14214
14215 if (cost > sd->max_newidle_lb_cost) {
14216 /*
14217 * Track max cost of a domain to make sure to not delay the
14218 * next wakeup on the CPU.
14219 */
14220 sd->max_newidle_lb_cost = cost;
14221 sd->last_decay_max_lb_cost = now;
14222
14223 } else if (time_after(now, next_decay)) {
14224 /*
14225 * Decay the newidle max times by ~1% per second to ensure that
14226 * it is not outdated and the current max cost is actually
14227 * shorter.
14228 */
14229 sd->max_newidle_lb_cost = (sd->max_newidle_lb_cost * 253) / 256;
14230 sd->last_decay_max_lb_cost = now;
14231 return true;
14232 }
14233
14234 return false;
14235 }
14236
14237 /*
14238 * It checks each scheduling domain to see if it is due to be balanced,
14239 * and initiates a balancing operation if so.
14240 *
14241 * Balancing parameters are set up in init_sched_domains.
14242 */
14243 static void sched_balance_domains(struct rq *rq, enum cpu_idle_type idle)
14244 {
14245 int continue_balancing = 1;
14246 int cpu = rq->cpu;
14247 int busy = idle != CPU_IDLE && !sched_idle_rq(rq);
14248 unsigned long interval;
14249 struct sched_domain *sd;
14250 /* Earliest time when we have to do rebalance again */
14251 unsigned long next_balance = jiffies + 60*HZ;
14252 int update_next_balance = 0;
14253 int need_decay = 0;
14254 u64 max_cost = 0;
14255
14256 rcu_read_lock();
14257 for_each_domain(cpu, sd) {
14258 /*
14259 * Decay the newidle max times here because this is a regular
14260 * visit to all the domains.
14261 */
14262 need_decay = update_newidle_cost(sd, 0, 0);
14263 max_cost += sd->max_newidle_lb_cost;
14264
14265 /*
14266 * Stop the load balance at this level. There is another
14267 * CPU in our sched group which is doing load balancing more
14268 * actively.
14269 */
14270 if (!continue_balancing) {
14271 if (need_decay)
14272 continue;
14273 break;
14274 }
14275
14276 interval = get_sd_balance_interval(sd, busy);
14277 if (time_after_eq(jiffies, sd->last_balance + interval)) {
14278 if (sched_balance_rq(cpu, rq, sd, idle, &continue_balancing)) {
14279 /*
14280 * The LBF_DST_PINNED logic could have changed
14281 * env->dst_cpu, so we can't know our idle
14282 * state even if we migrated tasks. Update it.
14283 */
14284 idle = idle_cpu(cpu);
14285 busy = !idle && !sched_idle_rq(rq);
14286 }
14287 sd->last_balance = jiffies;
14288 interval = get_sd_balance_interval(sd, busy);
14289 }
14290 if (time_after(next_balance, sd->last_balance + interval)) {
14291 next_balance = sd->last_balance + interval;
14292 update_next_balance = 1;
14293 }
14294 }
14295 if (need_decay) {
14296 /*
14297 * Ensure the rq-wide value also decays but keep it at a
14298 * reasonable floor to avoid funnies with rq->avg_idle.
14299 */
14300 rq->max_idle_balance_cost =
14301 max((u64)sysctl_sched_migration_cost, max_cost);
14302 }
14303 rcu_read_unlock();
14304
14305 /*
14306 * next_balance will be updated only when there is a need.
14307 * When the cpu is attached to null domain for ex, it will not be
14308 * updated.
14309 */
14310 if (likely(update_next_balance))
14311 rq->next_balance = next_balance;
14312
14313 }
14314
14315 static inline int on_null_domain(struct rq *rq)
14316 {
14317 return unlikely(!rcu_dereference_sched(rq->sd));
14318 }
14319
14320 #ifdef CONFIG_NO_HZ_COMMON
14321 /*
14322 * NOHZ idle load balancing (ILB) details:
14323 *
14324 * - When one of the busy CPUs notices that there may be an idle rebalancing
14325 * needed, they will kick the idle load balancer, which then does idle
14326 * load balancing for all the idle CPUs.
14327 */
14328 static inline int find_new_ilb(void)
14329 {
14330 struct cpumask *ilb_cpus;
14331 int ilb_cpu, fallback = -1;
14332
14333 lockdep_assert_irqs_disabled();
14334
14335 /*
14336 * Reuse the per-CPU select_rq_mask, which is protected from concurrent
14337 * use on this CPU by having interrupts disabled.
14338 */
14339 ilb_cpus = this_cpu_cpumask_var_ptr(select_rq_mask);
14340 cpumask_and(ilb_cpus, nohz.idle_cpus_mask,
14341 housekeeping_cpumask(HK_TYPE_KERNEL_NOISE));
14342
14343 for_each_cpu(ilb_cpu, ilb_cpus) {
14344 if (!idle_cpu(ilb_cpu)) {
14345 /*
14346 * Once an idle fallback exists, a busy CPU proves that
14347 * this core cannot be fully idle. Skip its siblings.
14348 */
14349 if (sched_smt_active() && fallback >= 0)
14350 cpumask_andnot(ilb_cpus, ilb_cpus, cpu_smt_mask(ilb_cpu));
14351 continue;
14352 }
14353
14354 /*
14355 * Running the idle load balancer on an idle sibling of a busy
14356 * SMT core can reduce the capacity available to its sibling. Prefer
14357 * a CPU whose entire core is idle, but retain the first idle CPU as
14358 * a fallback so idle balancing can still make progress when no fully
14359 * idle core exists.
14360 */
14361 if (sched_smt_active() && !is_core_idle(ilb_cpu)) {
14362 if (fallback < 0)
14363 fallback = ilb_cpu;
14364
14365 /*
14366 * The core is not idle, so there is no need to check
14367 * any of its other SMT siblings.
14368 */
14369 cpumask_andnot(ilb_cpus, ilb_cpus,
14370 cpu_smt_mask(ilb_cpu));
14371 continue;
14372 }
14373
14374 return ilb_cpu;
14375 }
14376
14377 return fallback;
14378 }
14379
14380 /*
14381 * Kick a CPU to do the NOHZ balancing, if it is time for it, via a cross-CPU
14382 * SMP function call (IPI).
14383 *
14384 * Prefer a CPU on a fully idle core in the HK_TYPE_KERNEL_NOISE housekeeping
14385 * set. Fall back to the first idle CPU when no fully idle core exists.
14386 */
14387 static void kick_ilb(unsigned int flags)
14388 {
14389 int ilb_cpu;
14390
14391 /*
14392 * Increase nohz.next_balance only when if full ilb is triggered but
14393 * not if we only update stats.
14394 */
14395 if (flags & NOHZ_BALANCE_KICK)
14396 nohz.next_balance = jiffies+1;
14397
14398 ilb_cpu = find_new_ilb();
14399 if (ilb_cpu < 0)
14400 return;
14401
14402 /*
14403 * Don't bother if no new NOHZ balance work items for ilb_cpu,
14404 * i.e. all bits in flags are already set in ilb_cpu.
14405 */
14406 if ((atomic_read(nohz_flags(ilb_cpu)) & flags) == flags)
14407 return;
14408
14409 /*
14410 * Access to rq::nohz_csd is serialized by NOHZ_KICK_MASK; he who sets
14411 * the first flag owns it; cleared by nohz_csd_func().
14412 */
14413 flags = atomic_fetch_or(flags, nohz_flags(ilb_cpu));
14414 if (flags & NOHZ_KICK_MASK)
14415 return;
14416
14417 /*
14418 * This way we generate an IPI on the target CPU which
14419 * is idle, and the softirq performing NOHZ idle load balancing
14420 * will be run before returning from the IPI.
14421 */
14422 smp_call_function_single_async(ilb_cpu, &cpu_rq(ilb_cpu)->nohz_csd);
14423 }
14424
14425 /*
14426 * Current decision point for kicking the idle load balancer in the presence
14427 * of idle CPUs in the system.
14428 */
14429 static void nohz_balancer_kick(struct rq *rq)
14430 {
14431 unsigned long now = jiffies;
14432 struct sched_domain_shared *sds;
14433 struct sched_domain *sd;
14434 int nr_busy, i, cpu = rq->cpu;
14435 unsigned int flags = 0;
14436
14437 if (unlikely(rq->idle_balance))
14438 return;
14439
14440 /*
14441 * We may be recently in ticked or tickless idle mode. At the first
14442 * busy tick after returning from idle, we will update the busy stats.
14443 */
14444 nohz_balance_exit_idle(rq);
14445
14446 if (READ_ONCE(nohz.has_blocked_load) &&
14447 time_after(now, READ_ONCE(nohz.next_blocked)))
14448 flags = NOHZ_STATS_KICK;
14449
14450 /*
14451 * Most of the time system is not 100% busy. i.e nohz.nr_cpus > 0
14452 * Skip the read if time is not due.
14453 *
14454 * If none are in tickless mode, there maybe a narrow window
14455 * (28 jiffies, HZ=1000) where flags maybe set and kick_ilb called.
14456 * But idle load balancing is not done as find_new_ilb fails.
14457 * That's very rare. So read nohz.nr_cpus only if time is due.
14458 */
14459 if (time_before(now, nohz.next_balance))
14460 goto out;
14461
14462 /*
14463 * None are in tickless mode and hence no need for NOHZ idle load
14464 * balancing
14465 */
14466 if (unlikely(cpumask_empty(nohz.idle_cpus_mask)))
14467 return;
14468
14469 if (rq->nr_running >= 2) {
14470 flags = NOHZ_STATS_KICK | NOHZ_BALANCE_KICK;
14471 goto out;
14472 }
14473
14474 sd = rcu_dereference_all(rq->sd);
14475 if (sd) {
14476 /*
14477 * If there's a runnable CFS task and the current CPU has reduced
14478 * capacity, kick the ILB to see if there's a better CPU to run on:
14479 */
14480 if (rq->cfs.h_nr_runnable >= 1 && check_cpu_capacity(rq, sd)) {
14481 flags |= NOHZ_STATS_KICK | NOHZ_BALANCE_KICK;
14482 goto out;
14483 }
14484 }
14485
14486 sd = rcu_dereference_all(per_cpu(sd_asym_packing, cpu));
14487 if (sd) {
14488 /*
14489 * When ASYM_PACKING; see if there's a more preferred CPU
14490 * currently idle; in which case, kick the ILB to move tasks
14491 * around.
14492 *
14493 * When balancing between cores, all the SMT siblings of the
14494 * preferred CPU must be idle.
14495 */
14496 for_each_cpu_and(i, sched_domain_span(sd), nohz.idle_cpus_mask) {
14497 if (sched_asym(sd, i, cpu)) {
14498 flags |= NOHZ_STATS_KICK | NOHZ_BALANCE_KICK;
14499 goto out;
14500 }
14501 }
14502 }
14503
14504 sd = rcu_dereference_all(per_cpu(sd_asym_cpucapacity, cpu));
14505 if (sd) {
14506 /*
14507 * When ASYM_CPUCAPACITY; see if there's a higher capacity CPU
14508 * to run the misfit task on.
14509 */
14510 if (check_misfit_status(rq))
14511 flags |= NOHZ_STATS_KICK | NOHZ_BALANCE_KICK;
14512
14513 /*
14514 * For asymmetric systems, we do not want to nicely balance
14515 * cache use, instead we want to embrace asymmetry and only
14516 * ensure tasks have enough CPU capacity.
14517 *
14518 * Skip the LLC logic because it's not relevant in that case.
14519 */
14520 goto out;
14521 }
14522
14523 sds = rcu_dereference_all(per_cpu(sd_balance_shared, cpu));
14524 if (sds) {
14525 /*
14526 * If there is an imbalance between LLC domains (IOW we could
14527 * increase the overall cache utilization), we need a less-loaded LLC
14528 * domain to pull some load from. Likewise, we may need to spread
14529 * load within the current LLC domain (e.g. packed SMT cores but
14530 * other CPUs are idle). We can't really know from here how busy
14531 * the others are - so just get a NOHZ balance going if it looks
14532 * like this LLC domain has tasks we could move.
14533 */
14534 nr_busy = atomic_read(&sds->nr_busy_cpus);
14535 if (nr_busy > 1)
14536 flags |= NOHZ_STATS_KICK | NOHZ_BALANCE_KICK;
14537 }
14538 out:
14539 if (READ_ONCE(nohz.needs_update))
14540 flags |= NOHZ_NEXT_KICK;
14541
14542 if (flags)
14543 kick_ilb(flags);
14544 }
14545
14546 static void set_cpu_sd_state_busy(int cpu)
14547 {
14548 struct sched_domain *sd;
14549 sd = rcu_dereference_all(per_cpu(sd_llc, cpu));
14550
14551 /*
14552 * sd->nohz_idle only pairs with nr_busy_cpus on sd->shared; if this
14553 * domain has no shared object there is nothing to clear or account.
14554 */
14555 if (!sd || !sd->shared || !sd->nohz_idle)
14556 return;
14557 sd->nohz_idle = 0;
14558
14559 atomic_inc(&sd->shared->nr_busy_cpus);
14560 }
14561
14562 void nohz_balance_exit_idle(struct rq *rq)
14563 {
14564 WARN_ON_ONCE(rq != this_rq());
14565
14566 if (likely(!rq->nohz_tick_stopped))
14567 return;
14568
14569 rq->nohz_tick_stopped = 0;
14570 cpumask_clear_cpu(rq->cpu, nohz.idle_cpus_mask);
14571
14572 set_cpu_sd_state_busy(rq->cpu);
14573 }
14574
14575 static void set_cpu_sd_state_idle(int cpu)
14576 {
14577 struct sched_domain *sd;
14578 sd = rcu_dereference_all(per_cpu(sd_llc, cpu));
14579
14580 /* See set_cpu_sd_state_busy(): nohz_idle is only used with sd->shared. */
14581 if (!sd || !sd->shared || sd->nohz_idle)
14582 return;
14583 sd->nohz_idle = 1;
14584
14585 atomic_dec(&sd->shared->nr_busy_cpus);
14586 }
14587
14588 /*
14589 * This routine will record that the CPU is going idle with tick stopped.
14590 * This info will be used in performing idle load balancing in the future.
14591 */
14592 void nohz_balance_enter_idle(int cpu)
14593 {
14594 struct rq *rq = cpu_rq(cpu);
14595
14596 WARN_ON_ONCE(cpu != smp_processor_id());
14597
14598 /* If this CPU is going down, then nothing needs to be done: */
14599 if (!cpu_active(cpu))
14600 return;
14601
14602 /*
14603 * Can be set safely without rq->lock held
14604 * If a clear happens, it will have evaluated last additions because
14605 * rq->lock is held during the check and the clear
14606 */
14607 rq->has_blocked_load = 1;
14608
14609 /*
14610 * The tick is still stopped but load could have been added in the
14611 * meantime. We set the nohz.has_blocked_load flag to trig a check of the
14612 * *_avg. The CPU is already part of nohz.idle_cpus_mask so the clear
14613 * of nohz.has_blocked_load can only happen after checking the new load
14614 */
14615 if (rq->nohz_tick_stopped)
14616 goto out;
14617
14618 /* If we're a completely isolated CPU, we don't play: */
14619 if (on_null_domain(rq))
14620 return;
14621
14622 rq->nohz_tick_stopped = 1;
14623
14624 cpumask_set_cpu(cpu, nohz.idle_cpus_mask);
14625
14626 /*
14627 * Ensures that if nohz_idle_balance() fails to observe our
14628 * @idle_cpus_mask store, it must observe the @has_blocked_load
14629 * and @needs_update stores.
14630 */
14631 smp_mb__after_atomic();
14632
14633 set_cpu_sd_state_idle(cpu);
14634
14635 WRITE_ONCE(nohz.needs_update, 1);
14636 out:
14637 /*
14638 * Each time a cpu enter idle, we assume that it has blocked load and
14639 * enable the periodic update of the load of idle CPUs
14640 */
14641 WRITE_ONCE(nohz.has_blocked_load, 1);
14642 }
14643
14644 static bool update_nohz_stats(struct rq *rq)
14645 {
14646 unsigned int cpu = rq->cpu;
14647
14648 if (!rq->has_blocked_load)
14649 return false;
14650
14651 if (!cpumask_test_cpu(cpu, nohz.idle_cpus_mask))
14652 return false;
14653
14654 if (!time_after(jiffies, READ_ONCE(rq->last_blocked_load_update_tick)))
14655 return true;
14656
14657 sched_balance_update_blocked_averages(cpu);
14658
14659 return rq->has_blocked_load;
14660 }
14661
14662 /*
14663 * Internal function that runs load balance for all idle CPUs. The load balance
14664 * can be a simple update of blocked load or a complete load balance with
14665 * tasks movement depending of flags.
14666 */
14667 static void _nohz_idle_balance(struct rq *this_rq, unsigned int flags)
14668 {
14669 /* Earliest time when we have to do rebalance again */
14670 unsigned long now = jiffies;
14671 unsigned long next_balance = now + 60*HZ;
14672 bool has_blocked_load = false;
14673 int update_next_balance = 0;
14674 int this_cpu = this_rq->cpu;
14675 int balance_cpu;
14676 struct rq *rq;
14677
14678 WARN_ON_ONCE((flags & NOHZ_KICK_MASK) == NOHZ_BALANCE_KICK);
14679
14680 /*
14681 * We assume there will be no idle load after this update and clear
14682 * the has_blocked_load flag. If a cpu enters idle in the mean time, it will
14683 * set the has_blocked_load flag and trigger another update of idle load.
14684 * Because a cpu that becomes idle, is added to idle_cpus_mask before
14685 * setting the flag, we are sure to not clear the state and not
14686 * check the load of an idle cpu.
14687 *
14688 * Same applies to idle_cpus_mask vs needs_update.
14689 */
14690 if (flags & NOHZ_STATS_KICK)
14691 WRITE_ONCE(nohz.has_blocked_load, 0);
14692 if (flags & NOHZ_NEXT_KICK)
14693 WRITE_ONCE(nohz.needs_update, 0);
14694
14695 /*
14696 * Ensures that if we miss the CPU, we must see the has_blocked_load
14697 * store from nohz_balance_enter_idle().
14698 */
14699 smp_mb();
14700
14701 /*
14702 * Start with the next CPU after this_cpu so we will end with this_cpu and let a
14703 * chance for other idle cpu to pull load.
14704 */
14705 for_each_cpu_wrap(balance_cpu, nohz.idle_cpus_mask, this_cpu+1) {
14706 if (!idle_cpu(balance_cpu))
14707 continue;
14708
14709 /*
14710 * If this CPU gets work to do, stop the load balancing
14711 * work being done for other CPUs. Next load
14712 * balancing owner will pick it up.
14713 */
14714 if (!idle_cpu(this_cpu) && need_resched()) {
14715 if (flags & NOHZ_STATS_KICK)
14716 has_blocked_load = true;
14717 if (flags & NOHZ_NEXT_KICK)
14718 WRITE_ONCE(nohz.needs_update, 1);
14719 goto abort;
14720 }
14721
14722 rq = cpu_rq(balance_cpu);
14723
14724 if (flags & NOHZ_STATS_KICK)
14725 has_blocked_load |= update_nohz_stats(rq);
14726
14727 /*
14728 * If time for next balance is due,
14729 * do the balance.
14730 */
14731 if (time_after_eq(jiffies, rq->next_balance)) {
14732 struct rq_flags rf;
14733
14734 rq_lock_irqsave(rq, &rf);
14735 update_rq_clock(rq);
14736 rq_unlock_irqrestore(rq, &rf);
14737
14738 if (flags & NOHZ_BALANCE_KICK)
14739 sched_balance_domains(rq, CPU_IDLE);
14740 }
14741
14742 if (time_after(next_balance, rq->next_balance)) {
14743 next_balance = rq->next_balance;
14744 update_next_balance = 1;
14745 }
14746 }
14747
14748 /*
14749 * next_balance will be updated only when there is a need.
14750 * When the CPU is attached to null domain for ex, it will not be
14751 * updated.
14752 */
14753 if (likely(update_next_balance))
14754 nohz.next_balance = next_balance;
14755
14756 if (flags & NOHZ_STATS_KICK)
14757 WRITE_ONCE(nohz.next_blocked,
14758 now + msecs_to_jiffies(LOAD_AVG_PERIOD));
14759
14760 abort:
14761 /* There is still blocked load, enable periodic update */
14762 if (has_blocked_load)
14763 WRITE_ONCE(nohz.has_blocked_load, 1);
14764 }
14765
14766 /*
14767 * In CONFIG_NO_HZ_COMMON case, the idle balance kickee will do the
14768 * rebalancing for all the CPUs for whom scheduler ticks are stopped.
14769 */
14770 static bool nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle)
14771 {
14772 unsigned int flags = this_rq->nohz_idle_balance;
14773
14774 if (!flags)
14775 return false;
14776
14777 this_rq->nohz_idle_balance = 0;
14778
14779 if (idle != CPU_IDLE)
14780 return false;
14781
14782 _nohz_idle_balance(this_rq, flags);
14783
14784 return true;
14785 }
14786
14787 /*
14788 * Check if we need to directly run the ILB for updating blocked load before
14789 * entering idle state. Here we run ILB directly without issuing IPIs.
14790 *
14791 * Note that when this function is called, the tick may not yet be stopped on
14792 * this CPU yet. nohz.idle_cpus_mask is updated only when tick is stopped and
14793 * cleared on the next busy tick. In other words, nohz.idle_cpus_mask updates
14794 * don't align with CPUs enter/exit idle to avoid bottlenecks due to high idle
14795 * entry/exit rate (usec). So it is possible that _nohz_idle_balance() is
14796 * called from this function on (this) CPU that's not yet in the mask. That's
14797 * OK because the goal of nohz_run_idle_balance() is to run ILB only for
14798 * updating the blocked load of already idle CPUs without waking up one of
14799 * those idle CPUs and outside the preempt disable / IRQ off phase of the local
14800 * cpu about to enter idle, because it can take a long time.
14801 */
14802 void nohz_run_idle_balance(int cpu)
14803 {
14804 unsigned int flags;
14805
14806 flags = atomic_fetch_andnot(NOHZ_NEWILB_KICK, nohz_flags(cpu));
14807
14808 /*
14809 * Update the blocked load only if no SCHED_SOFTIRQ is about to happen
14810 * (i.e. NOHZ_STATS_KICK set) and will do the same.
14811 */
14812 if ((flags == NOHZ_NEWILB_KICK) && !need_resched())
14813 _nohz_idle_balance(cpu_rq(cpu), NOHZ_STATS_KICK);
14814 }
14815
14816 static void nohz_newidle_balance(struct rq *this_rq)
14817 {
14818 int this_cpu = this_rq->cpu;
14819
14820 /* Will wake up very soon. No time for doing anything else*/
14821 if (this_rq->avg_idle < sysctl_sched_migration_cost)
14822 return;
14823
14824 /* Don't need to update blocked load of idle CPUs*/
14825 if (!READ_ONCE(nohz.has_blocked_load) ||
14826 time_before(jiffies, READ_ONCE(nohz.next_blocked)))
14827 return;
14828
14829 /*
14830 * Set the need to trigger ILB in order to update blocked load
14831 * before entering idle state.
14832 */
14833 atomic_or(NOHZ_NEWILB_KICK, nohz_flags(this_cpu));
14834 }
14835
14836 #else /* !CONFIG_NO_HZ_COMMON: */
14837 static inline void nohz_balancer_kick(struct rq *rq) { }
14838
14839 static inline bool nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle)
14840 {
14841 return false;
14842 }
14843
14844 static inline void nohz_newidle_balance(struct rq *this_rq) { }
14845 #endif /* !CONFIG_NO_HZ_COMMON */
14846
14847 /*
14848 * sched_balance_newidle is called by schedule() if this_cpu is about to become
14849 * idle. Attempts to pull tasks from other CPUs.
14850 *
14851 * Returns:
14852 * < 0 - we released the lock and there are !fair tasks present
14853 * 0 - failed, no new tasks
14854 * > 0 - success, new (fair) tasks present
14855 */
14856 static int sched_balance_newidle(struct rq *this_rq, struct rq_flags *rf)
14857 __must_hold(__rq_lockp(this_rq))
14858 {
14859 unsigned long next_balance = jiffies + HZ;
14860 int this_cpu = this_rq->cpu;
14861 int continue_balancing = 1;
14862 u64 t0, t1, curr_cost = 0;
14863 struct sched_domain *sd;
14864 int pulled_task = 0;
14865
14866 update_misfit_status(NULL, this_rq);
14867
14868 /*
14869 * There is a task waiting to run. No need to search for one.
14870 * Return 0; the task will be enqueued when switching to idle.
14871 */
14872 if (this_rq->ttwu_pending)
14873 return 0;
14874
14875 /*
14876 * We must set idle_stamp _before_ calling sched_balance_rq()
14877 * for CPU_NEWLY_IDLE, such that we measure the this duration
14878 * as idle time.
14879 */
14880 this_rq->idle_stamp = rq_clock(this_rq);
14881
14882 /*
14883 * Do not pull tasks towards !active CPUs...
14884 */
14885 if (!cpu_active(this_cpu))
14886 return 0;
14887
14888 /*
14889 * This is OK, because current is on_cpu, which avoids it being picked
14890 * for load-balance and preemption/IRQs are still disabled avoiding
14891 * further scheduler activity on it and we're being very careful to
14892 * re-start the picking loop.
14893 */
14894 rq_unpin_lock(this_rq, rf);
14895
14896 sd = rcu_dereference_sched_domain(this_rq->sd);
14897 if (!sd)
14898 goto out;
14899
14900 if (!get_rd_overloaded(this_rq->rd) ||
14901 this_rq->avg_idle < sd->max_newidle_lb_cost) {
14902
14903 update_next_balance(sd, &next_balance);
14904 goto out;
14905 }
14906
14907 /*
14908 * Include sched_balance_update_blocked_averages() in the cost
14909 * calculation because it can be quite costly -- this ensures we skip
14910 * it when avg_idle gets to be very low.
14911 */
14912 t0 = sched_clock_cpu(this_cpu);
14913 __sched_balance_update_blocked_averages(this_rq);
14914
14915 rq_modified_begin(this_rq, &fair_sched_class);
14916 raw_spin_rq_unlock(this_rq);
14917
14918 for_each_domain(this_cpu, sd) {
14919 u64 domain_cost;
14920
14921 update_next_balance(sd, &next_balance);
14922
14923 if (this_rq->avg_idle < curr_cost + sd->max_newidle_lb_cost)
14924 break;
14925
14926 if (sd->flags & SD_BALANCE_NEWIDLE) {
14927 unsigned int weight = 1;
14928
14929 if (sched_feat(NI_RANDOM) && sd->newidle_ratio < 1024) {
14930 /*
14931 * Throw a 1k sided dice; and only run
14932 * newidle_balance according to the success
14933 * rate.
14934 */
14935 u32 d1k = sched_rng() % 1024;
14936 weight = 1 + sd->newidle_ratio;
14937 if (d1k > weight) {
14938 update_newidle_stats(sd, 0);
14939 continue;
14940 }
14941 weight = (1024 + weight/2) / weight;
14942 }
14943
14944 pulled_task = sched_balance_rq(this_cpu, this_rq,
14945 sd, CPU_NEWLY_IDLE,
14946 &continue_balancing);
14947
14948 t1 = sched_clock_cpu(this_cpu);
14949 domain_cost = t1 - t0;
14950 curr_cost += domain_cost;
14951 t0 = t1;
14952
14953 /*
14954 * Track max cost of a domain to make sure to not delay the
14955 * next wakeup on the CPU.
14956 */
14957 update_newidle_cost(sd, domain_cost, weight * !!pulled_task);
14958 }
14959
14960 /*
14961 * Stop searching for tasks to pull if there are
14962 * now runnable tasks on this rq.
14963 */
14964 if (pulled_task || !continue_balancing)
14965 break;
14966 }
14967
14968 raw_spin_rq_lock(this_rq);
14969
14970 if (curr_cost > this_rq->max_idle_balance_cost)
14971 this_rq->max_idle_balance_cost = curr_cost;
14972
14973 /*
14974 * While browsing the domains, we released the rq lock, a task could
14975 * have been enqueued in the meantime. Since we're not going idle,
14976 * pretend we pulled a task.
14977 */
14978 if (this_rq->cfs.h_nr_queued && !pulled_task)
14979 pulled_task = 1;
14980
14981 /* If a higher prio class was modified, restart the pick */
14982 if (rq_modified_above(this_rq, &fair_sched_class))
14983 pulled_task = -1;
14984
14985 out:
14986 /* Move the next balance forward */
14987 if (time_after(this_rq->next_balance, next_balance))
14988 this_rq->next_balance = next_balance;
14989
14990 if (pulled_task)
14991 this_rq->idle_stamp = 0;
14992 else
14993 nohz_newidle_balance(this_rq);
14994
14995 rq_repin_lock(this_rq, rf);
14996
14997 return pulled_task;
14998 }
14999
15000 /*
15001 * This softirq handler is triggered via SCHED_SOFTIRQ from two places:
15002 *
15003 * - directly from the local sched_tick() for periodic load balancing
15004 *
15005 * - indirectly from a remote sched_tick() for NOHZ idle balancing
15006 * through the SMP cross-call nohz_csd_func()
15007 */
15008 static __latent_entropy void sched_balance_softirq(void)
15009 {
15010 struct rq *this_rq = this_rq();
15011 enum cpu_idle_type idle = this_rq->idle_balance;
15012 /*
15013 * If this CPU has a pending NOHZ_BALANCE_KICK, then do the
15014 * balancing on behalf of the other idle CPUs whose ticks are
15015 * stopped. Do nohz_idle_balance *before* sched_balance_domains to
15016 * give the idle CPUs a chance to load balance. Else we may
15017 * load balance only within the local sched_domain hierarchy
15018 * and abort nohz_idle_balance altogether if we pull some load.
15019 */
15020 if (nohz_idle_balance(this_rq, idle))
15021 return;
15022
15023 /* normal load balance */
15024 sched_balance_update_blocked_averages(this_rq->cpu);
15025 sched_balance_domains(this_rq, idle);
15026 }
15027
15028 /*
15029 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
15030 */
15031 void sched_balance_trigger(struct rq *rq)
15032 {
15033 /*
15034 * Don't need to rebalance while attached to NULL domain or
15035 * runqueue CPU is not active
15036 */
15037 if (unlikely(on_null_domain(rq) || !cpu_active(cpu_of(rq))))
15038 return;
15039
15040 if (time_after_eq(jiffies, rq->next_balance))
15041 raise_softirq(SCHED_SOFTIRQ);
15042
15043 nohz_balancer_kick(rq);
15044 }
15045
15046 static void rq_online_fair(struct rq *rq)
15047 {
15048 update_sysctl();
15049
15050 update_runtime_enabled(rq);
15051 }
15052
15053 static void rq_offline_fair(struct rq *rq)
15054 {
15055 update_sysctl();
15056
15057 /* Ensure any throttled groups are reachable by pick_next_task */
15058 unthrottle_offline_cfs_rqs(rq);
15059
15060 /* Ensure that we remove rq contribution to group share: */
15061 clear_tg_offline_cfs_rqs(rq);
15062 }
15063
15064 #ifdef CONFIG_SCHED_CORE
15065 static inline bool
15066 __entity_slice_used(struct sched_entity *se, int min_nr_tasks)
15067 {
15068 u64 rtime = se->sum_exec_runtime - se->prev_sum_exec_runtime;
15069 u64 slice = se->slice;
15070
15071 return (rtime * min_nr_tasks > slice);
15072 }
15073
15074 #define MIN_NR_TASKS_DURING_FORCEIDLE 2
15075 static inline void task_tick_core(struct rq *rq, struct task_struct *curr)
15076 {
15077 if (!sched_core_enabled(rq))
15078 return;
15079
15080 /*
15081 * If runqueue has only one task which used up its slice and
15082 * if the sibling is forced idle, then trigger schedule to
15083 * give forced idle task a chance.
15084 *
15085 * __entity_slice_used() considers only this active rq and it gets the
15086 * whole slice. But during force idle, we have siblings acting
15087 * like a single runqueue and hence we need to consider runnable
15088 * tasks on this CPU and the forced idle CPU. Ideally, we should
15089 * go through the forced idle rq, but that would be a perf hit.
15090 * We can assume that the forced idle CPU has at least
15091 * MIN_NR_TASKS_DURING_FORCEIDLE - 1 tasks and use that to check
15092 * if we need to give up the CPU.
15093 */
15094 if (rq->core->core_forceidle_count && rq->cfs.h_nr_queued == 1 &&
15095 __entity_slice_used(&curr->se, MIN_NR_TASKS_DURING_FORCEIDLE))
15096 resched_curr(rq);
15097 }
15098
15099 /*
15100 * Consider any infeasible weight scenario. Take for instance two tasks,
15101 * each bound to their respective sibling, one with weight 1 and one with
15102 * weight 2. Then the lower weight task will run ahead of the higher weight
15103 * task without bound.
15104 *
15105 * This utterly destroys the concept of a shared time base.
15106 *
15107 * Remember; all this is about a proportionally fair scheduling, where each
15108 * tasks receives:
15109 *
15110 * w_i
15111 * dt_i = ---------- dt (1)
15112 * \Sum_j w_j
15113 *
15114 * which we do by tracking a virtual time, s_i:
15115 *
15116 * 1
15117 * s_i = --- d[t]_i (2)
15118 * w_i
15119 *
15120 * Where d[t] is a delta of discrete time, while dt is an infinitesimal.
15121 * The immediate corollary is that the ideal schedule S, where (2) to use
15122 * an infinitesimal delta, is:
15123 *
15124 * 1
15125 * S = ---------- dt (3)
15126 * \Sum_i w_i
15127 *
15128 * From which we can define the lag, or deviation from the ideal, as:
15129 *
15130 * lag(i) = S - s_i (4)
15131 *
15132 * And since the one and only purpose is to approximate S, we get that:
15133 *
15134 * \Sum_i w_i lag(i) := 0 (5)
15135 *
15136 * If this were not so, we no longer converge to S, and we can no longer
15137 * claim our scheduler has any of the properties we derive from S. This is
15138 * exactly what you did above, you broke it!
15139 *
15140 *
15141 * Let's continue for a while though; to see if there is anything useful to
15142 * be learned. We can combine (1)-(3) or (4)-(5) and express S in s_i:
15143 *
15144 * \Sum_i w_i s_i
15145 * S = -------------- (6)
15146 * \Sum_i w_i
15147 *
15148 * Which gives us a way to compute S, given our s_i. Now, if you've read
15149 * our code, you know that we do not in fact do this, the reason for this
15150 * is two-fold. Firstly, computing S in that way requires a 64bit division
15151 * for every time we'd use it (see 12), and secondly, this only describes
15152 * the steady-state, it doesn't handle dynamics.
15153 *
15154 * Anyway, in (6): s_i -> x + (s_i - x), to get:
15155 *
15156 * \Sum_i w_i (s_i - x)
15157 * S - x = -------------------- (7)
15158 * \Sum_i w_i
15159 *
15160 * Which shows that S and s_i transform alike (which makes perfect sense
15161 * given that S is basically the (weighted) average of s_i).
15162 *
15163 * So the thing to remember is that the above is strictly UP. It is
15164 * possible to generalize to multiple runqueues -- however it gets really
15165 * yuck when you have to add affinity support, as illustrated by our very
15166 * first counter-example.
15167 *
15168 * Luckily I think we can avoid needing a full multi-queue variant for
15169 * core-scheduling (or load-balancing). The crucial observation is that we
15170 * only actually need this comparison in the presence of forced-idle; only
15171 * then do we need to tell if the stalled rq has higher priority over the
15172 * other.
15173 *
15174 * [XXX assumes SMT2; better consider the more general case, I suspect
15175 * it'll work out because our comparison is always between 2 rqs and the
15176 * answer is only interesting if one of them is forced-idle]
15177 *
15178 * And (under assumption of SMT2) when there is forced-idle, there is only
15179 * a single queue, so everything works like normal.
15180 *
15181 * Let, for our runqueue 'k':
15182 *
15183 * T_k = \Sum_i w_i s_i
15184 * W_k = \Sum_i w_i ; for all i of k (8)
15185 *
15186 * Then we can write (6) like:
15187 *
15188 * T_k
15189 * S_k = --- (9)
15190 * W_k
15191 *
15192 * From which immediately follows that:
15193 *
15194 * T_k + T_l
15195 * S_k+l = --------- (10)
15196 * W_k + W_l
15197 *
15198 * On which we can define a combined lag:
15199 *
15200 * lag_k+l(i) := S_k+l - s_i (11)
15201 *
15202 * And that gives us the tools to compare tasks across a combined runqueue.
15203 *
15204 *
15205 * Combined this gives the following:
15206 *
15207 * a) when a runqueue enters force-idle, sync it against it's sibling rq(s)
15208 * using (7); this only requires storing single 'time'-stamps.
15209 *
15210 * b) when comparing tasks between 2 runqueues of which one is forced-idle,
15211 * compare the combined lag, per (11).
15212 *
15213 * Now, of course cgroups (I so hate them) make this more interesting in
15214 * that a) seems to suggest we need to iterate all cgroup on a CPU at such
15215 * boundaries, but I think we can avoid that. The force-idle is for the
15216 * whole CPU, all it's rqs. So we can mark it in the root and lazily
15217 * propagate downward on demand.
15218 */
15219
15220 /*
15221 * So this sync is basically a relative reset of S to 0.
15222 *
15223 * So with 2 queues, when one goes idle, we drop them both to 0 and one
15224 * then increases due to not being idle, and the idle one builds up lag to
15225 * get re-elected. So far so simple, right?
15226 *
15227 * When there's 3, we can have the situation where 2 run and one is idle,
15228 * we sync to 0 and let the idle one build up lag to get re-election. Now
15229 * suppose another one also drops idle. At this point dropping all to 0
15230 * again would destroy the built-up lag from the queue that was already
15231 * idle, not good.
15232 *
15233 * So instead of syncing everything, we can:
15234 *
15235 * less := !((s64)(s_a - s_b) <= 0)
15236 *
15237 * (v_a - S_a) - (v_b - S_b) == v_a - v_b - S_a + S_b
15238 * == v_a - (v_b - S_a + S_b)
15239 *
15240 * IOW, we can recast the (lag) comparison to a one-sided difference.
15241 * So if then, instead of syncing the whole queue, sync the idle queue
15242 * against the active queue with S_a + S_b at the point where we sync.
15243 *
15244 * (XXX consider the implication of living in a cyclic group: N / 2^n N)
15245 *
15246 * This gives us means of syncing single queues against the active queue,
15247 * and for already idle queues to preserve their build-up lag.
15248 *
15249 * Of course, then we get the situation where there's 2 active and one
15250 * going idle, who do we pick to sync against? Theory would have us sync
15251 * against the combined S, but as we've already demonstrated, there is no
15252 * such thing in infeasible weight scenarios.
15253 *
15254 * One thing I've considered; and this is where that core_active rudiment
15255 * came from, is having active queues sync up between themselves after
15256 * every tick. This limits the observed divergence due to the work
15257 * conservancy.
15258 *
15259 * On top of that, we can improve upon things by employing (10) here.
15260 */
15261
15262 /*
15263 * se_fi_update - Update the cfs_rq->zero_vruntime_fi in a CFS hierarchy if needed.
15264 */
15265 static void se_fi_update(const struct sched_entity *se, unsigned int fi_seq,
15266 bool forceidle)
15267 {
15268 for_each_sched_entity(se) {
15269 struct cfs_rq *cfs_rq = cfs_rq_of(se);
15270
15271 if (forceidle) {
15272 if (cfs_rq->forceidle_seq == fi_seq)
15273 break;
15274 cfs_rq->forceidle_seq = fi_seq;
15275 }
15276
15277 cfs_rq->zero_vruntime_fi = cfs_rq->zero_vruntime;
15278 }
15279 }
15280
15281 void task_vruntime_update(struct rq *rq, struct task_struct *p, bool in_fi)
15282 {
15283 struct sched_entity *se = &p->se;
15284
15285 if (p->sched_class != &fair_sched_class)
15286 return;
15287
15288 se_fi_update(se, rq->core->core_forceidle_seq, in_fi);
15289 }
15290
15291 bool cfs_prio_less(const struct task_struct *a, const struct task_struct *b,
15292 bool in_fi)
15293 {
15294 struct rq *rq = task_rq(a);
15295 const struct sched_entity *sea = &a->se;
15296 const struct sched_entity *seb = &b->se;
15297 struct cfs_rq *cfs_rqa;
15298 struct cfs_rq *cfs_rqb;
15299 s64 delta;
15300
15301 WARN_ON_ONCE(task_rq(b)->core != rq->core);
15302
15303 cfs_rqa = &task_rq(a)->cfs;
15304 cfs_rqb = &task_rq(b)->cfs;
15305
15306 /*
15307 * Find delta after normalizing se's vruntime with its cfs_rq's
15308 * zero_vruntime_fi, which would have been updated in prior calls
15309 * to se_fi_update().
15310 */
15311 delta = vruntime_op(sea->vruntime, "-", seb->vruntime) +
15312 vruntime_op(cfs_rqb->zero_vruntime_fi, "-", cfs_rqa->zero_vruntime_fi);
15313
15314 return delta > 0;
15315 }
15316
15317 static int task_is_throttled_fair(struct task_struct *p, int cpu)
15318 {
15319 struct cfs_rq *cfs_rq;
15320
15321 #ifdef CONFIG_FAIR_GROUP_SCHED
15322 cfs_rq = tg_cfs_rq(task_group(p), cpu);
15323 #else
15324 cfs_rq = &cpu_rq(cpu)->cfs;
15325 #endif
15326 return throttled_hierarchy(cfs_rq);
15327 }
15328 #else /* !CONFIG_SCHED_CORE: */
15329 static inline void task_tick_core(struct rq *rq, struct task_struct *curr) {}
15330 #endif /* !CONFIG_SCHED_CORE */
15331
15332 /*
15333 * scheduler tick hitting a task of our scheduling class.
15334 *
15335 * NOTE: This function can be called remotely by the tick offload that
15336 * goes along full dynticks. Therefore no local assumption can be made
15337 * and everything must be accessed through the @rq and @curr passed in
15338 * parameters.
15339 */
15340 static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
15341 {
15342 struct sched_entity *se = &curr->se;
15343
15344 if (se->on_rq) {
15345 unsigned long weight = NICE_0_LOAD;
15346 struct cfs_rq *cfs_rq;
15347
15348 for_each_sched_entity(se) {
15349 cfs_rq = cfs_rq_of(se);
15350 entity_tick(cfs_rq, se, queued);
15351
15352 weight = __calc_prop_weight(cfs_rq, se, weight);
15353 }
15354
15355 se = &curr->se;
15356 reweight_eevdf(cfs_rq, se, weight, se->on_rq);
15357 }
15358
15359 if (queued)
15360 return;
15361
15362 if (static_branch_unlikely(&sched_numa_balancing))
15363 task_tick_numa(rq, curr);
15364
15365 task_tick_cache(rq, curr);
15366
15367 update_misfit_status(curr, rq);
15368 check_update_overutilized_status(task_rq(curr));
15369
15370 task_tick_core(rq, curr);
15371 }
15372
15373 /*
15374 * called on fork with the child task as argument from the parent's context
15375 * - child not yet on the tasklist
15376 * - preemption disabled
15377 */
15378 static void task_fork_fair(struct task_struct *p)
15379 {
15380 set_task_max_allowed_capacity(p);
15381 }
15382
15383 /*
15384 * Priority of the task has changed. Check to see if we preempt
15385 * the current task.
15386 */
15387 static void
15388 prio_changed_fair(struct rq *rq, struct task_struct *p, u64 oldprio)
15389 {
15390 if (!task_on_rq_queued(p))
15391 return;
15392
15393 if (p->prio == oldprio)
15394 return;
15395
15396 if (rq->cfs.h_nr_queued == 1)
15397 return;
15398
15399 /*
15400 * Reschedule if we are currently running on this runqueue and
15401 * our priority decreased, or if we are not currently running on
15402 * this runqueue and our priority is higher than the current's
15403 */
15404 if (task_current_donor(rq, p)) {
15405 if (p->prio > oldprio)
15406 resched_curr(rq);
15407 } else {
15408 wakeup_preempt(rq, p, 0);
15409 }
15410 }
15411
15412 #ifdef CONFIG_FAIR_GROUP_SCHED
15413 /*
15414 * Propagate the changes of the sched_entity across the tg tree to make it
15415 * visible to the root
15416 */
15417 static void propagate_entity_cfs_rq(struct sched_entity *se)
15418 {
15419 struct cfs_rq *cfs_rq = cfs_rq_of(se);
15420
15421 /*
15422 * If a task gets attached to this cfs_rq and before being queued,
15423 * it gets migrated to another CPU due to reasons like affinity
15424 * change, make sure this cfs_rq stays on leaf cfs_rq list to have
15425 * that removed load decayed or it can cause faireness problem.
15426 */
15427 if (!cfs_rq_pelt_clock_throttled(cfs_rq))
15428 list_add_leaf_cfs_rq(cfs_rq);
15429
15430 /* Start to propagate at parent */
15431 se = se->parent;
15432
15433 for_each_sched_entity(se) {
15434 cfs_rq = cfs_rq_of(se);
15435
15436 update_load_avg(cfs_rq, se, UPDATE_TG);
15437
15438 if (!cfs_rq_pelt_clock_throttled(cfs_rq))
15439 list_add_leaf_cfs_rq(cfs_rq);
15440 }
15441
15442 assert_list_leaf_cfs_rq(rq_of(cfs_rq));
15443 }
15444 #else /* !CONFIG_FAIR_GROUP_SCHED: */
15445 static void propagate_entity_cfs_rq(struct sched_entity *se) { }
15446 #endif /* !CONFIG_FAIR_GROUP_SCHED */
15447
15448 static void detach_entity_cfs_rq(struct sched_entity *se)
15449 {
15450 struct cfs_rq *cfs_rq = cfs_rq_of(se);
15451
15452 /*
15453 * In case the task sched_avg hasn't been attached:
15454 * - A forked task which hasn't been woken up by wake_up_new_task().
15455 * - A task which has been woken up by try_to_wake_up() but is
15456 * waiting for actually being woken up by sched_ttwu_pending().
15457 */
15458 if (!se->avg.last_update_time)
15459 return;
15460
15461 /* Catch up with the cfs_rq and remove our load when we leave */
15462 update_load_avg(cfs_rq, se, 0);
15463 detach_entity_load_avg(cfs_rq, se);
15464 update_tg_load_avg(cfs_rq);
15465 propagate_entity_cfs_rq(se);
15466 }
15467
15468 static void attach_entity_cfs_rq(struct sched_entity *se)
15469 {
15470 struct cfs_rq *cfs_rq = cfs_rq_of(se);
15471
15472 /* Synchronize entity with its cfs_rq */
15473 update_load_avg(cfs_rq, se, sched_feat(ATTACH_AGE_LOAD) ? 0 : SKIP_AGE_LOAD);
15474 attach_entity_load_avg(cfs_rq, se);
15475 update_tg_load_avg(cfs_rq);
15476 propagate_entity_cfs_rq(se);
15477 }
15478
15479 static void detach_task_cfs_rq(struct task_struct *p)
15480 {
15481 struct sched_entity *se = &p->se;
15482
15483 detach_entity_cfs_rq(se);
15484 }
15485
15486 static void attach_task_cfs_rq(struct task_struct *p)
15487 {
15488 struct sched_entity *se = &p->se;
15489
15490 attach_entity_cfs_rq(se);
15491 }
15492
15493 static void switching_from_fair(struct rq *rq, struct task_struct *p)
15494 {
15495 if (p->se.sched_delayed)
15496 dequeue_task(rq, p, DEQUEUE_SLEEP | DEQUEUE_DELAYED | DEQUEUE_NOCLOCK);
15497 }
15498
15499 static void switched_from_fair(struct rq *rq, struct task_struct *p)
15500 {
15501 detach_task_cfs_rq(p);
15502 }
15503
15504 static void switched_to_fair(struct rq *rq, struct task_struct *p)
15505 {
15506 WARN_ON_ONCE(p->se.sched_delayed);
15507
15508 attach_task_cfs_rq(p);
15509
15510 set_task_max_allowed_capacity(p);
15511
15512 if (task_on_rq_queued(p)) {
15513 /*
15514 * We were most likely switched from sched_rt, so
15515 * kick off the schedule if running, otherwise just see
15516 * if we can still preempt the current task.
15517 */
15518 if (task_current_donor(rq, p))
15519 resched_curr(rq);
15520 else
15521 wakeup_preempt(rq, p, 0);
15522 }
15523 }
15524
15525 static void set_next_task_fair(struct rq *rq, struct task_struct *p, bool first)
15526 {
15527 struct sched_entity *se = &p->se;
15528 bool throttled = false;
15529 struct cfs_rq *cfs_rq = &rq->cfs;
15530 unsigned long weight = NICE_0_LOAD;
15531 bool on_rq = se->on_rq;
15532
15533 clear_buddies(cfs_rq, se);
15534
15535 if (on_rq)
15536 __dequeue_entity(cfs_rq, se);
15537
15538 for_each_sched_entity(se) {
15539 cfs_rq = cfs_rq_of(se);
15540
15541 if (!IS_ENABLED(CONFIG_FAIR_GROUP_SCHED) ||
15542 !first || !cfs_rq->h_curr)
15543 set_next_entity(cfs_rq, se);
15544
15545 /* ensure bandwidth has been allocated on our new cfs_rq */
15546 throttled |= account_cfs_rq_runtime(cfs_rq, 0);
15547
15548 if (on_rq)
15549 weight = __calc_prop_weight(cfs_rq, se, weight);
15550 }
15551
15552 if (throttled)
15553 task_throttle_setup_work(p);
15554
15555 se = &p->se;
15556 cfs_rq->curr = se;
15557
15558 if (on_rq) {
15559 reweight_eevdf(cfs_rq, se, weight, se->on_rq);
15560 if (first)
15561 set_protect_slice(cfs_rq, se);
15562 }
15563
15564 if (task_on_rq_queued(p)) {
15565 /*
15566 * Move the next running task to the front of the list, so our
15567 * cfs_tasks list becomes MRU one.
15568 */
15569 list_move(&se->group_node, &rq->cfs_tasks);
15570 }
15571 if (!first)
15572 return;
15573
15574 WARN_ON_ONCE(se->sched_delayed);
15575
15576 if (hrtick_enabled_fair(rq))
15577 hrtick_start_fair(rq, p);
15578
15579 update_misfit_status(p, rq);
15580 sched_fair_update_stop_tick(rq, p);
15581 }
15582
15583 void init_cfs_rq(struct cfs_rq *cfs_rq)
15584 {
15585 cfs_rq->tasks_timeline = RB_ROOT_CACHED;
15586 cfs_rq->zero_vruntime = (u64)(-(1LL << 20));
15587 raw_spin_lock_init(&cfs_rq->removed.lock);
15588 }
15589
15590 #ifdef CONFIG_FAIR_GROUP_SCHED
15591 static void task_change_group_fair(struct task_struct *p)
15592 {
15593 /*
15594 * We couldn't detach or attach a forked task which
15595 * hasn't been woken up by wake_up_new_task().
15596 */
15597 if (READ_ONCE(p->__state) == TASK_NEW)
15598 return;
15599
15600 detach_task_cfs_rq(p);
15601
15602 /* Tell se's cfs_rq has been changed -- migrated */
15603 p->se.avg.last_update_time = 0;
15604 set_task_rq(p, task_cpu(p));
15605 attach_task_cfs_rq(p);
15606 }
15607
15608 void free_fair_sched_group(struct task_group *tg)
15609 {
15610 free_percpu(tg->cfs_rq);
15611 }
15612
15613 int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
15614 {
15615 struct cfs_tg_state __percpu *state;
15616 struct sched_entity *se;
15617 struct cfs_rq *cfs_rq;
15618 int i;
15619
15620 state = alloc_percpu_gfp(struct cfs_tg_state, GFP_KERNEL);
15621 if (!state)
15622 goto err;
15623
15624 tg->cfs_rq = &state->cfs_rq;
15625 tg->shares = NICE_0_LOAD;
15626
15627 init_cfs_bandwidth(tg_cfs_bandwidth(tg), tg_cfs_bandwidth(parent));
15628
15629 for_each_possible_cpu(i) {
15630 cfs_rq = tg_cfs_rq(tg, i);
15631 if (!cfs_rq)
15632 goto err;
15633
15634 se = tg_se(tg, i);
15635 init_cfs_rq(cfs_rq);
15636 init_tg_cfs_entry(tg, cfs_rq, se, i, tg_se(parent, i));
15637 init_entity_runnable_average(se);
15638 }
15639
15640 return 1;
15641
15642 err:
15643 return 0;
15644 }
15645
15646 void online_fair_sched_group(struct task_group *tg)
15647 {
15648 struct sched_entity *se;
15649 struct rq_flags rf;
15650 struct rq *rq;
15651 int i;
15652
15653 for_each_possible_cpu(i) {
15654 rq = cpu_rq(i);
15655 se = tg_se(tg, i);
15656 rq_lock_irq(rq, &rf);
15657 update_rq_clock(rq);
15658 attach_entity_cfs_rq(se);
15659 sync_throttle(tg, i);
15660 rq_unlock_irq(rq, &rf);
15661 }
15662 }
15663
15664 void unregister_fair_sched_group(struct task_group *tg)
15665 {
15666 int cpu;
15667
15668 destroy_cfs_bandwidth(tg_cfs_bandwidth(tg));
15669
15670 for_each_possible_cpu(cpu) {
15671 struct cfs_rq *cfs_rq = tg_cfs_rq(tg, cpu);
15672 struct sched_entity *se = tg_se(tg, cpu);
15673 struct rq *rq = cpu_rq(cpu);
15674
15675 if (se)
15676 remove_entity_load_avg(se);
15677
15678 /*
15679 * Only empty task groups can be destroyed; so we can speculatively
15680 * check on_list without danger of it being re-added.
15681 */
15682 if (cfs_rq->on_list) {
15683 guard(rq_lock_irqsave)(rq);
15684 list_del_leaf_cfs_rq(cfs_rq);
15685 }
15686 }
15687 }
15688
15689 void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
15690 struct sched_entity *se, int cpu,
15691 struct sched_entity *parent)
15692 {
15693 struct rq *rq = cpu_rq(cpu);
15694
15695 cfs_rq->tg = tg;
15696 cfs_rq->rq = rq;
15697 init_cfs_rq_runtime(cfs_rq);
15698
15699 /* se could be NULL for root_task_group */
15700 if (!se)
15701 return;
15702
15703 if (!parent) {
15704 se->cfs_rq = &rq->cfs;
15705 se->depth = 0;
15706 } else {
15707 se->cfs_rq = parent->my_q;
15708 se->depth = parent->depth + 1;
15709 }
15710
15711 se->my_q = cfs_rq;
15712 /* guarantee group entities always have weight */
15713 update_load_set(&se->load, NICE_0_LOAD);
15714 se->parent = parent;
15715 }
15716
15717 static DEFINE_MUTEX(shares_mutex);
15718
15719 static int __sched_group_set_shares(struct task_group *tg, unsigned long shares)
15720 {
15721 int i;
15722
15723 lockdep_assert_held(&shares_mutex);
15724
15725 /*
15726 * We can't change the weight of the root cgroup.
15727 */
15728 if (is_root_task_group(tg))
15729 return -EINVAL;
15730
15731 shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES));
15732
15733 if (tg->shares == shares)
15734 return 0;
15735
15736 tg->shares = shares;
15737 for_each_possible_cpu(i) {
15738 struct rq *rq = cpu_rq(i);
15739 struct sched_entity *se = tg_se(tg, i);
15740 struct rq_flags rf;
15741
15742 /* Propagate contribution to hierarchy */
15743 rq_lock_irqsave(rq, &rf);
15744 update_rq_clock(rq);
15745 for_each_sched_entity(se) {
15746 update_load_avg(cfs_rq_of(se), se, UPDATE_TG);
15747 update_cfs_group(se);
15748 }
15749 rq_unlock_irqrestore(rq, &rf);
15750 }
15751
15752 return 0;
15753 }
15754
15755 int sched_group_set_shares(struct task_group *tg, unsigned long shares)
15756 {
15757 int ret;
15758
15759 mutex_lock(&shares_mutex);
15760 if (tg_is_idle(tg))
15761 ret = -EINVAL;
15762 else
15763 ret = __sched_group_set_shares(tg, shares);
15764 mutex_unlock(&shares_mutex);
15765
15766 return ret;
15767 }
15768
15769 int sched_group_set_idle(struct task_group *tg, long idle)
15770 {
15771 int i;
15772
15773 if (tg == &root_task_group)
15774 return -EINVAL;
15775
15776 if (idle < 0 || idle > 1)
15777 return -EINVAL;
15778
15779 mutex_lock(&shares_mutex);
15780
15781 if (tg->idle == idle) {
15782 mutex_unlock(&shares_mutex);
15783 return 0;
15784 }
15785
15786 tg->idle = idle;
15787
15788 for_each_possible_cpu(i) {
15789 struct rq *rq = cpu_rq(i);
15790 struct sched_entity *se = tg_se(tg, i);
15791 struct cfs_rq *grp_cfs_rq = tg_cfs_rq(tg, i);
15792 bool was_idle = cfs_rq_is_idle(grp_cfs_rq);
15793 long idle_task_delta;
15794 struct rq_flags rf;
15795
15796 rq_lock_irqsave(rq, &rf);
15797
15798 grp_cfs_rq->idle = idle;
15799 if (WARN_ON_ONCE(was_idle == cfs_rq_is_idle(grp_cfs_rq)))
15800 goto next_cpu;
15801
15802 idle_task_delta = grp_cfs_rq->h_nr_queued -
15803 grp_cfs_rq->h_nr_idle;
15804 if (!cfs_rq_is_idle(grp_cfs_rq))
15805 idle_task_delta *= -1;
15806
15807 for_each_sched_entity(se) {
15808 struct cfs_rq *cfs_rq = cfs_rq_of(se);
15809
15810 if (!se->on_rq)
15811 break;
15812
15813 cfs_rq->h_nr_idle += idle_task_delta;
15814
15815 /* Already accounted at parent level and above. */
15816 if (cfs_rq_is_idle(cfs_rq))
15817 break;
15818 }
15819
15820 next_cpu:
15821 rq_unlock_irqrestore(rq, &rf);
15822 }
15823
15824 /* Idle groups have minimum weight. */
15825 if (tg_is_idle(tg))
15826 __sched_group_set_shares(tg, scale_load(WEIGHT_IDLEPRIO));
15827 else
15828 __sched_group_set_shares(tg, NICE_0_LOAD);
15829
15830 mutex_unlock(&shares_mutex);
15831 return 0;
15832 }
15833
15834 #endif /* CONFIG_FAIR_GROUP_SCHED */
15835
15836
15837 static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task)
15838 {
15839 struct sched_entity *se = &task->se;
15840 unsigned int rr_interval = 0;
15841
15842 /*
15843 * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise
15844 * idle runqueue:
15845 */
15846 if (rq->cfs.load.weight)
15847 rr_interval = NS_TO_JIFFIES(se->slice);
15848
15849 return rr_interval;
15850 }
15851
15852 /*
15853 * All the scheduling class methods:
15854 */
15855 DEFINE_SCHED_CLASS(fair) = {
15856 .enqueue_task = enqueue_task_fair,
15857 .dequeue_task = dequeue_task_fair,
15858 .yield_task = yield_task_fair,
15859 .yield_to_task = yield_to_task_fair,
15860
15861 .wakeup_preempt = wakeup_preempt_fair,
15862
15863 .pick_task = pick_task_fair,
15864 .put_prev_task = put_prev_task_fair,
15865 .set_next_task = set_next_task_fair,
15866
15867 .select_task_rq = select_task_rq_fair,
15868 .migrate_task_rq = migrate_task_rq_fair,
15869
15870 .rq_online = rq_online_fair,
15871 .rq_offline = rq_offline_fair,
15872
15873 .task_dead = task_dead_fair,
15874 .set_cpus_allowed = set_cpus_allowed_fair,
15875
15876 .task_tick = task_tick_fair,
15877 .task_fork = task_fork_fair,
15878
15879 .reweight_task = reweight_task_fair,
15880 .prio_changed = prio_changed_fair,
15881 .switching_from = switching_from_fair,
15882 .switched_from = switched_from_fair,
15883 .switched_to = switched_to_fair,
15884
15885 .get_rr_interval = get_rr_interval_fair,
15886
15887 .update_curr = update_curr_fair,
15888
15889 #ifdef CONFIG_FAIR_GROUP_SCHED
15890 .task_change_group = task_change_group_fair,
15891 #endif
15892
15893 #ifdef CONFIG_SCHED_CORE
15894 .task_is_throttled = task_is_throttled_fair,
15895 #endif
15896
15897 #ifdef CONFIG_UCLAMP_TASK
15898 .uclamp_enabled = 1,
15899 #endif
15900 };
15901
15902 void print_cfs_stats(struct seq_file *m, int cpu)
15903 {
15904 struct cfs_rq *cfs_rq, *pos;
15905
15906 rcu_read_lock();
15907 for_each_leaf_cfs_rq_safe(cpu_rq(cpu), cfs_rq, pos)
15908 print_cfs_rq(m, cpu, cfs_rq);
15909 rcu_read_unlock();
15910 }
15911
15912 #ifdef CONFIG_NUMA_BALANCING
15913 void show_numa_stats(struct task_struct *p, struct seq_file *m)
15914 {
15915 int node;
15916 unsigned long tsf = 0, tpf = 0, gsf = 0, gpf = 0;
15917 struct numa_group *ng;
15918
15919 rcu_read_lock();
15920 ng = rcu_dereference_all(p->numa_group);
15921 for_each_online_node(node) {
15922 if (p->numa_faults) {
15923 tsf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 0)];
15924 tpf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 1)];
15925 }
15926 if (ng) {
15927 gsf = ng->faults[task_faults_idx(NUMA_MEM, node, 0)];
15928 gpf = ng->faults[task_faults_idx(NUMA_MEM, node, 1)];
15929 }
15930 print_numa_stats(m, node, tsf, tpf, gsf, gpf);
15931 }
15932 rcu_read_unlock();
15933 }
15934 #endif /* CONFIG_NUMA_BALANCING */
15935
15936 __init void init_sched_fair_class(void)
15937 {
15938 int i;
15939
15940 for_each_possible_cpu(i) {
15941 zalloc_cpumask_var_node(&per_cpu(load_balance_mask, i), GFP_KERNEL, cpu_to_node(i));
15942 zalloc_cpumask_var_node(&per_cpu(select_rq_mask, i), GFP_KERNEL, cpu_to_node(i));
15943 zalloc_cpumask_var_node(&per_cpu(should_we_balance_tmpmask, i),
15944 GFP_KERNEL, cpu_to_node(i));
15945
15946 #ifdef CONFIG_CFS_BANDWIDTH
15947 INIT_CSD(&cpu_rq(i)->cfsb_csd, __cfsb_csd_unthrottle, cpu_rq(i));
15948 INIT_LIST_HEAD(&cpu_rq(i)->cfsb_csd_list);
15949 #endif
15950 }
15951
15952 open_softirq(SCHED_SOFTIRQ, sched_balance_softirq);
15953
15954 #ifdef CONFIG_NO_HZ_COMMON
15955 nohz.next_balance = jiffies;
15956 nohz.next_blocked = jiffies;
15957 zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT);
15958 #endif
15959 }