Lines Matching +full:cpu +full:- +full:capacity

1 // SPDX-License-Identifier: GPL-2.0
43 #include <linux/memory-tiers.h>
61 * The initial- and re-scaling of tunables is configurable
65 * SCHED_TUNABLESCALING_NONE - unscaled, always *1
66 * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus)
67 * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus
74 * Minimal preemption granularity for CPU-bound tasks:
104 * For asym packing, by default the lower numbered CPU has higher priority.
106 int __weak arch_asym_cpu_priority(int cpu) in arch_asym_cpu_priority() argument
108 return -cpu; in arch_asym_cpu_priority()
112 * The margin used when comparing utilization with CPU capacity.
119 * The margin used when comparing CPU capacities.
129 * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool
188 lw->weight += inc; in update_load_add()
189 lw->inv_weight = 0; in update_load_add()
194 lw->weight -= dec; in update_load_sub()
195 lw->inv_weight = 0; in update_load_sub()
200 lw->weight = w; in update_load_set()
201 lw->inv_weight = 0; in update_load_set()
208 * so pick a second-best guess by going with the log2 of the
256 if (likely(lw->inv_weight)) in __update_inv_weight()
259 w = scale_load_down(lw->weight); in __update_inv_weight()
262 lw->inv_weight = 1; in __update_inv_weight()
264 lw->inv_weight = WMULT_CONST; in __update_inv_weight()
266 lw->inv_weight = WMULT_CONST / w; in __update_inv_weight()
272 * (delta_exec * (weight * lw->inv_weight)) >> WMULT_SHIFT
292 shift -= fs; in __calc_delta()
296 fact = mul_u32_u32(fact, lw->inv_weight); in __calc_delta()
301 shift -= fs; in __calc_delta()
313 if (unlikely(se->load.weight != NICE_0_LOAD)) in calc_delta_fair()
314 delta = __calc_delta(delta, NICE_0_LOAD, &se->load); in calc_delta_fair()
329 for (; se; se = se->parent)
334 int cpu = cpu_of(rq); in list_add_leaf_cfs_rq() local
336 if (cfs_rq->on_list) in list_add_leaf_cfs_rq()
337 return rq->tmp_alone_branch == &rq->leaf_cfs_rq_list; in list_add_leaf_cfs_rq()
339 cfs_rq->on_list = 1; in list_add_leaf_cfs_rq()
344 * enqueued. The fact that we always enqueue bottom-up in list_add_leaf_cfs_rq()
350 if (cfs_rq->tg->parent && in list_add_leaf_cfs_rq()
351 cfs_rq->tg->parent->cfs_rq[cpu]->on_list) { in list_add_leaf_cfs_rq()
358 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list, in list_add_leaf_cfs_rq()
359 &(cfs_rq->tg->parent->cfs_rq[cpu]->leaf_cfs_rq_list)); in list_add_leaf_cfs_rq()
365 rq->tmp_alone_branch = &rq->leaf_cfs_rq_list; in list_add_leaf_cfs_rq()
369 if (!cfs_rq->tg->parent) { in list_add_leaf_cfs_rq()
374 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list, in list_add_leaf_cfs_rq()
375 &rq->leaf_cfs_rq_list); in list_add_leaf_cfs_rq()
380 rq->tmp_alone_branch = &rq->leaf_cfs_rq_list; in list_add_leaf_cfs_rq()
390 list_add_rcu(&cfs_rq->leaf_cfs_rq_list, rq->tmp_alone_branch); in list_add_leaf_cfs_rq()
395 rq->tmp_alone_branch = &cfs_rq->leaf_cfs_rq_list; in list_add_leaf_cfs_rq()
401 if (cfs_rq->on_list) { in list_del_leaf_cfs_rq()
408 * to the prev element but it will point to rq->leaf_cfs_rq_list in list_del_leaf_cfs_rq()
411 if (rq->tmp_alone_branch == &cfs_rq->leaf_cfs_rq_list) in list_del_leaf_cfs_rq()
412 rq->tmp_alone_branch = cfs_rq->leaf_cfs_rq_list.prev; in list_del_leaf_cfs_rq()
414 list_del_rcu(&cfs_rq->leaf_cfs_rq_list); in list_del_leaf_cfs_rq()
415 cfs_rq->on_list = 0; in list_del_leaf_cfs_rq()
421 SCHED_WARN_ON(rq->tmp_alone_branch != &rq->leaf_cfs_rq_list); in assert_list_leaf_cfs_rq()
426 list_for_each_entry_safe(cfs_rq, pos, &rq->leaf_cfs_rq_list, \
433 if (se->cfs_rq == pse->cfs_rq) in is_same_group()
434 return se->cfs_rq; in is_same_group()
441 return se->parent; in parent_entity()
457 se_depth = (*se)->depth; in find_matching_se()
458 pse_depth = (*pse)->depth; in find_matching_se()
461 se_depth--; in find_matching_se()
466 pse_depth--; in find_matching_se()
478 return tg->idle > 0; in tg_is_idle()
483 return cfs_rq->idle > 0; in cfs_rq_is_idle()
512 for (cfs_rq = &rq->cfs, pos = NULL; cfs_rq; cfs_rq = pos)
550 s64 delta = (s64)(vruntime - max_vruntime); in max_vruntime()
559 s64 delta = (s64)(vruntime - min_vruntime); in min_vruntime()
569 return (s64)(a->vruntime - b->vruntime) < 0; in entity_before()
574 return (s64)(se->vruntime - cfs_rq->min_vruntime); in entity_key()
581 * Compute virtual time from the per-task service numbers:
589 * lag_i = S - s_i = w_i * (V - v_i)
595 * \Sum w_i * (V - v_i) = 0
596 * \Sum w_i * V - w_i * v_i = 0
599 * se->vruntime):
602 * V = -------------- = --------------
609 * virtual time has non-continguous motion equivalent to:
611 * V +-= lag_i / W
618 * Substitute: v_i == (v_i - v0) + v0
620 * \Sum ((v_i - v0) + v0) * w_i \Sum (v_i - v0) * w_i
621 * V = ---------------------------- = --------------------- + v0
626 * v0 := cfs_rq->min_vruntime
627 * \Sum (v_i - v0) * w_i := cfs_rq->avg_vruntime
628 * \Sum w_i := cfs_rq->avg_load
631 * the per-task service, these deltas: (v_i - v), will be in the order of the
641 unsigned long weight = scale_load_down(se->load.weight); in avg_vruntime_add()
644 cfs_rq->avg_vruntime += key * weight; in avg_vruntime_add()
645 cfs_rq->avg_load += weight; in avg_vruntime_add()
651 unsigned long weight = scale_load_down(se->load.weight); in avg_vruntime_sub()
654 cfs_rq->avg_vruntime -= key * weight; in avg_vruntime_sub()
655 cfs_rq->avg_load -= weight; in avg_vruntime_sub()
662 * v' = v + d ==> avg_vruntime' = avg_runtime - d*avg_load in avg_vruntime_update()
664 cfs_rq->avg_vruntime -= cfs_rq->avg_load * delta; in avg_vruntime_update()
673 struct sched_entity *curr = cfs_rq->curr; in avg_vruntime()
674 s64 avg = cfs_rq->avg_vruntime; in avg_vruntime()
675 long load = cfs_rq->avg_load; in avg_vruntime()
677 if (curr && curr->on_rq) { in avg_vruntime()
678 unsigned long weight = scale_load_down(curr->load.weight); in avg_vruntime()
687 avg -= (load - 1); in avg_vruntime()
691 return cfs_rq->min_vruntime + avg; in avg_vruntime()
695 * lag_i = S - s_i = w_i * (V - v_i)
698 * is possible -- by addition/removal/reweight to the tree -- to move V around
706 * -r_max < lag < max(r_max, q)
714 vlag = avruntime - se->vruntime; in entity_lag()
715 limit = calc_delta_fair(max_t(u64, 2*se->slice, TICK_NSEC), se); in entity_lag()
717 return clamp(vlag, -limit, limit); in entity_lag()
722 SCHED_WARN_ON(!se->on_rq); in update_entity_lag()
724 se->vlag = entity_lag(avg_vruntime(cfs_rq), se); in update_entity_lag()
731 * lag_i = S - s_i = w_i*(V - v_i)
733 * lag_i >= 0 -> V >= v_i
735 * \Sum (v_i - v)*w_i
736 * V = ------------------ + v
739 * lag_i >= 0 -> \Sum (v_i - v)*w_i >= (v_i - v)*(\Sum w_i)
741 * Note: using 'avg_vruntime() > se->vruntime' is inacurate due
746 struct sched_entity *curr = cfs_rq->curr; in entity_eligible()
747 s64 avg = cfs_rq->avg_vruntime; in entity_eligible()
748 long load = cfs_rq->avg_load; in entity_eligible()
750 if (curr && curr->on_rq) { in entity_eligible()
751 unsigned long weight = scale_load_down(curr->load.weight); in entity_eligible()
762 u64 min_vruntime = cfs_rq->min_vruntime; in __update_min_vruntime()
766 s64 delta = (s64)(vruntime - min_vruntime); in __update_min_vruntime()
777 struct sched_entity *curr = cfs_rq->curr; in update_min_vruntime()
779 u64 vruntime = cfs_rq->min_vruntime; in update_min_vruntime()
782 if (curr->on_rq) in update_min_vruntime()
783 vruntime = curr->vruntime; in update_min_vruntime()
790 vruntime = se->vruntime; in update_min_vruntime()
792 vruntime = min_vruntime(vruntime, se->vruntime); in update_min_vruntime()
796 u64_u32_store(cfs_rq->min_vruntime, in update_min_vruntime()
805 #define deadline_gt(field, lse, rse) ({ (s64)((lse)->field - (rse)->field) > 0; })
812 se->min_deadline = rse->min_deadline; in __update_min_deadline()
817 * se->min_deadline = min(se->deadline, left->min_deadline, right->min_deadline)
821 u64 old_min_deadline = se->min_deadline; in min_deadline_update()
822 struct rb_node *node = &se->run_node; in min_deadline_update()
824 se->min_deadline = se->deadline; in min_deadline_update()
825 __update_min_deadline(se, node->rb_right); in min_deadline_update()
826 __update_min_deadline(se, node->rb_left); in min_deadline_update()
828 return se->min_deadline == old_min_deadline; in min_deadline_update()
835 * Enqueue an entity into the rb-tree:
840 se->min_deadline = se->deadline; in __enqueue_entity()
841 rb_add_augmented_cached(&se->run_node, &cfs_rq->tasks_timeline, in __enqueue_entity()
847 rb_erase_augmented_cached(&se->run_node, &cfs_rq->tasks_timeline, in __dequeue_entity()
854 struct rb_node *left = rb_first_cached(&cfs_rq->tasks_timeline); in __pick_first_entity()
873 * We can do this in O(log n) time due to an augmented RB-tree. The
877 * se->min_deadline = min(se->deadline, se->{left,right}->min_deadline)
883 struct rb_node *node = cfs_rq->tasks_timeline.rb_root.rb_node; in __pick_eevdf()
884 struct sched_entity *curr = cfs_rq->curr; in __pick_eevdf()
888 if (curr && (!curr->on_rq || !entity_eligible(cfs_rq, curr))) in __pick_eevdf()
893 * Once selected, run a task until it either becomes non-eligible or in __pick_eevdf()
896 if (sched_feat(RUN_TO_PARITY) && curr && curr->vlag == curr->deadline) in __pick_eevdf()
906 node = node->rb_left; in __pick_eevdf()
920 if (node->rb_left) { in __pick_eevdf()
921 struct sched_entity *left = __node_2_se(node->rb_left); in __pick_eevdf()
931 if (left->min_deadline == se->min_deadline) in __pick_eevdf()
936 if (se->deadline == se->min_deadline) in __pick_eevdf()
940 node = node->rb_right; in __pick_eevdf()
947 if (!best_left || (s64)(best_left->min_deadline - best->deadline) > 0) in __pick_eevdf()
954 node = &best_left->run_node; in __pick_eevdf()
959 if (se->deadline == se->min_deadline) in __pick_eevdf()
963 if (node->rb_left && in __pick_eevdf()
964 __node_2_se(node->rb_left)->min_deadline == se->min_deadline) { in __pick_eevdf()
965 node = node->rb_left; in __pick_eevdf()
970 node = node->rb_right; in __pick_eevdf()
993 struct rb_node *last = rb_last(&cfs_rq->tasks_timeline.rb_root); in __pick_last_entity()
1027 if ((s64)(se->vruntime - se->deadline) < 0) in update_deadline()
1035 se->slice = sysctl_sched_base_slice; in update_deadline()
1040 se->deadline = se->vruntime + calc_delta_fair(se->slice, se); in update_deadline()
1045 if (cfs_rq->nr_running > 1) { in update_deadline()
1054 static int select_idle_sibling(struct task_struct *p, int prev_cpu, int cpu);
1056 static unsigned long capacity_of(int cpu);
1061 struct sched_avg *sa = &se->avg; in init_entity_runnable_average()
1072 sa->load_avg = scale_load_down(se->load.weight); in init_entity_runnable_average()
1081 * util_avg = cfs_rq->util_avg / (cfs_rq->load_avg + 1) * se.load.weight
1090 * util_avg_cap = (cpu_scale - cfs_rq->avg.util_avg) / 2^n
1092 * where n denotes the nth task and cpu_scale the CPU capacity.
1094 * For example, for a CPU with 1024 of capacity, a simplest series from
1105 struct sched_entity *se = &p->se; in post_init_entity_util_avg()
1107 struct sched_avg *sa = &se->avg; in post_init_entity_util_avg()
1109 long cap = (long)(cpu_scale - cfs_rq->avg.util_avg) / 2; in post_init_entity_util_avg()
1111 if (p->sched_class != &fair_sched_class) { in post_init_entity_util_avg()
1122 se->avg.last_update_time = cfs_rq_clock_pelt(cfs_rq); in post_init_entity_util_avg()
1127 if (cfs_rq->avg.util_avg != 0) { in post_init_entity_util_avg()
1128 sa->util_avg = cfs_rq->avg.util_avg * se->load.weight; in post_init_entity_util_avg()
1129 sa->util_avg /= (cfs_rq->avg.load_avg + 1); in post_init_entity_util_avg()
1131 if (sa->util_avg > cap) in post_init_entity_util_avg()
1132 sa->util_avg = cap; in post_init_entity_util_avg()
1134 sa->util_avg = cap; in post_init_entity_util_avg()
1138 sa->runnable_avg = sa->util_avg; in post_init_entity_util_avg()
1158 delta_exec = now - curr->exec_start; in update_curr_se()
1162 curr->exec_start = now; in update_curr_se()
1163 curr->sum_exec_runtime += delta_exec; in update_curr_se()
1169 __schedstat_set(stats->exec_max, in update_curr_se()
1170 max(delta_exec, stats->exec_max)); in update_curr_se()
1188 struct task_struct *curr = rq->curr; in update_curr_common()
1191 delta_exec = update_curr_se(rq, &curr->se); in update_curr_common()
1203 struct sched_entity *curr = cfs_rq->curr; in update_curr()
1213 curr->vruntime += calc_delta_fair(delta_exec, curr); in update_curr()
1225 update_curr(cfs_rq_of(&rq->curr->se)); in update_curr_fair()
1258 * maybe already in the runqueue, the se->statistics.wait_start in update_stats_wait_end_fair()
1262 if (unlikely(!schedstat_val(stats->wait_start))) in update_stats_wait_end_fair()
1289 * Task is being enqueued - update stats:
1301 if (se != cfs_rq->curr) in update_stats_enqueue_fair()
1319 if (se != cfs_rq->curr) in update_stats_dequeue_fair()
1327 state = READ_ONCE(tsk->__state); in update_stats_dequeue_fair()
1329 __schedstat_set(tsk->stats.sleep_start, in update_stats_dequeue_fair()
1332 __schedstat_set(tsk->stats.block_start, in update_stats_dequeue_fair()
1338 * We are picking a new current task - update its stats:
1346 se->exec_start = rq_clock_task(rq_of(cfs_rq)); in update_stats_curr_start()
1353 static inline bool is_core_idle(int cpu) in is_core_idle() argument
1358 for_each_cpu(sibling, cpu_smt_mask(cpu)) { in is_core_idle()
1359 if (cpu == sibling) in is_core_idle()
1430 * towards the CPU. As a consequence, these stats are weighted
1431 * more by CPU use than by memory faults.
1438 * ->numa_group (see struct task_struct for locking rules).
1442 return rcu_dereference_check(p->numa_group, p == current || in deref_task_numa_group()
1443 (lockdep_is_held(__rq_lockp(task_rq(p))) && !READ_ONCE(p->on_cpu))); in deref_task_numa_group()
1448 return rcu_dereference_protected(p->numa_group, p == current); in deref_curr_numa_group()
1460 * Calculations based on RSS as non-present and empty pages are skipped in task_nr_scan_windows()
1464 nr_scan_pages = sysctl_numa_balancing_scan_size << (20 - PAGE_SHIFT); in task_nr_scan_windows()
1465 rss = get_mm_rss(p->mm); in task_nr_scan_windows()
1498 ng = rcu_dereference(p->numa_group); in task_scan_start()
1503 period *= refcount_read(&ng->refcount); in task_scan_start()
1528 period *= refcount_read(&ng->refcount); in task_scan_max()
1540 rq->nr_numa_running += (p->numa_preferred_nid != NUMA_NO_NODE); in account_numa_enqueue()
1541 rq->nr_preferred_running += (p->numa_preferred_nid == task_node(p)); in account_numa_enqueue()
1546 rq->nr_numa_running -= (p->numa_preferred_nid != NUMA_NO_NODE); in account_numa_dequeue()
1547 rq->nr_preferred_running -= (p->numa_preferred_nid == task_node(p)); in account_numa_dequeue()
1553 /* Memory and CPU locality */
1565 ng = rcu_dereference(p->numa_group); in task_numa_group_id()
1567 gid = ng->gid; in task_numa_group_id()
1574 * The averaged statistics, shared & private, memory & CPU,
1586 if (!p->numa_faults) in task_faults()
1589 return p->numa_faults[task_faults_idx(NUMA_MEM, nid, 0)] + in task_faults()
1590 p->numa_faults[task_faults_idx(NUMA_MEM, nid, 1)]; in task_faults()
1600 return ng->faults[task_faults_idx(NUMA_MEM, nid, 0)] + in group_faults()
1601 ng->faults[task_faults_idx(NUMA_MEM, nid, 1)]; in group_faults()
1606 return group->faults[task_faults_idx(NUMA_CPU, nid, 0)] + in group_faults_cpu()
1607 group->faults[task_faults_idx(NUMA_CPU, nid, 1)]; in group_faults_cpu()
1616 faults += ng->faults[task_faults_idx(NUMA_MEM, node, 1)]; in group_faults_priv()
1628 faults += ng->faults[task_faults_idx(NUMA_MEM, node, 0)]; in group_faults_shared()
1636 * considered part of a numa group's pseudo-interleaving set. Migrations
1643 return group_faults_cpu(ng, nid) * ACTIVE_NODE_FRACTION > ng->max_faults_cpu; in numa_is_active_node()
1702 faults *= (max_dist - dist); in score_nearby_nodes()
1703 faults /= (max_dist - LOCAL_DISTANCE); in score_nearby_nodes()
1723 if (!p->numa_faults) in task_weight()
1726 total_faults = p->total_numa_faults; in task_weight()
1746 total_faults = ng->total_faults; in group_weight()
1759 * used to record scan time instead of CPU and PID. When tiering mode
1761 * interpreted as CPU and PID. So CPU needs to be checked to avoid to
1772 * advantage of fast memory capacity, all recently accessed slow
1782 pgdat->node_present_pages >> 4); in pgdat_free_space_enough()
1783 for (z = pgdat->nr_zones - 1; z >= 0; z--) { in pgdat_free_space_enough()
1784 struct zone *zone = pgdat->node_zones + z; in pgdat_free_space_enough()
1804 * hint page fault latency = hint page fault time - scan time
1816 return (time - last_time) & PAGE_ACCESS_TIME_MASK; in numa_hint_fault_latency()
1833 start = pgdat->nbp_rl_start; in numa_promotion_rate_limit()
1834 if (now - start > MSEC_PER_SEC && in numa_promotion_rate_limit()
1835 cmpxchg(&pgdat->nbp_rl_start, start, now) == start) in numa_promotion_rate_limit()
1836 pgdat->nbp_rl_nr_cand = nr_cand; in numa_promotion_rate_limit()
1837 if (nr_cand - pgdat->nbp_rl_nr_cand >= rate_limit) in numa_promotion_rate_limit()
1853 start = pgdat->nbp_th_start; in numa_promotion_adjust_threshold()
1854 if (now - start > th_period && in numa_promotion_adjust_threshold()
1855 cmpxchg(&pgdat->nbp_th_start, start, now) == start) { in numa_promotion_adjust_threshold()
1859 diff_cand = nr_cand - pgdat->nbp_th_nr_cand; in numa_promotion_adjust_threshold()
1861 th = pgdat->nbp_threshold ? : ref_th; in numa_promotion_adjust_threshold()
1863 th = max(th - unit_th, unit_th); in numa_promotion_adjust_threshold()
1866 pgdat->nbp_th_nr_cand = nr_cand; in numa_promotion_adjust_threshold()
1867 pgdat->nbp_threshold = th; in numa_promotion_adjust_threshold()
1891 pgdat->nbp_threshold = 0; in should_numa_migrate_memory()
1897 (20 - PAGE_SHIFT); in should_numa_migrate_memory()
1900 th = pgdat->nbp_threshold ? : def_th; in should_numa_migrate_memory()
1909 this_cpupid = cpu_pid_to_cpupid(dst_cpu, current->pid); in should_numa_migrate_memory()
1919 * two full passes of the "multi-stage node selection" test that is in should_numa_migrate_memory()
1922 if ((p->numa_preferred_nid == NUMA_NO_NODE || p->numa_scan_seq <= 4) && in should_numa_migrate_memory()
1927 * Multi-stage node selection is used in conjunction with a periodic in should_numa_migrate_memory()
1928 * migration fault to build a temporal task<->page relation. By using in should_numa_migrate_memory()
1929 * a two-stage filter we remove short/unlikely relations. in should_numa_migrate_memory()
1933 * page (n_t) (in a given time-span) to a probability. in should_numa_migrate_memory()
1941 * act on an unlikely task<->page relation. in should_numa_migrate_memory()
1951 /* A shared fault, but p->numa_group has not been set up yet. */ in should_numa_migrate_memory()
1964 * Distribute memory according to CPU & memory use on each node, in should_numa_migrate_memory()
1968 * --------------- * - > --------------- in should_numa_migrate_memory()
1979 /* The node has spare capacity that can be used to run more tasks. */
1982 * The node is fully used and the tasks don't compete for more CPU
1987 * The node is overloaded and can't provide expected CPU cycles to all
1998 /* Total compute capacity of CPUs on a node */
2030 if ((ns->nr_running > ns->weight) && in numa_classify()
2031 (((ns->compute_capacity * 100) < (ns->util * imbalance_pct)) || in numa_classify()
2032 ((ns->compute_capacity * imbalance_pct) < (ns->runnable * 100)))) in numa_classify()
2035 if ((ns->nr_running < ns->weight) || in numa_classify()
2036 (((ns->compute_capacity * 100) > (ns->util * imbalance_pct)) && in numa_classify()
2037 ((ns->compute_capacity * imbalance_pct) > (ns->runnable * 100)))) in numa_classify()
2045 static inline bool test_idle_cores(int cpu);
2046 static inline int numa_idle_core(int idle_core, int cpu) in numa_idle_core() argument
2049 idle_core >= 0 || !test_idle_cores(cpu)) in numa_idle_core()
2056 if (is_core_idle(cpu)) in numa_idle_core()
2057 idle_core = cpu; in numa_idle_core()
2062 static inline int numa_idle_core(int idle_core, int cpu) in numa_idle_core() argument
2078 int cpu, idle_core = -1; in update_numa_stats() local
2081 ns->idle_cpu = -1; in update_numa_stats()
2084 for_each_cpu(cpu, cpumask_of_node(nid)) { in update_numa_stats()
2085 struct rq *rq = cpu_rq(cpu); in update_numa_stats()
2087 ns->load += cpu_load(rq); in update_numa_stats()
2088 ns->runnable += cpu_runnable(rq); in update_numa_stats()
2089 ns->util += cpu_util_cfs(cpu); in update_numa_stats()
2090 ns->nr_running += rq->cfs.h_nr_running; in update_numa_stats()
2091 ns->compute_capacity += capacity_of(cpu); in update_numa_stats()
2093 if (find_idle && idle_core < 0 && !rq->nr_running && idle_cpu(cpu)) { in update_numa_stats()
2094 if (READ_ONCE(rq->numa_migrate_on) || in update_numa_stats()
2095 !cpumask_test_cpu(cpu, env->p->cpus_ptr)) in update_numa_stats()
2098 if (ns->idle_cpu == -1) in update_numa_stats()
2099 ns->idle_cpu = cpu; in update_numa_stats()
2101 idle_core = numa_idle_core(idle_core, cpu); in update_numa_stats()
2106 ns->weight = cpumask_weight(cpumask_of_node(nid)); in update_numa_stats()
2108 ns->node_type = numa_classify(env->imbalance_pct, ns); in update_numa_stats()
2111 ns->idle_cpu = idle_core; in update_numa_stats()
2117 struct rq *rq = cpu_rq(env->dst_cpu); in task_numa_assign()
2119 /* Check if run-queue part of active NUMA balance. */ in task_numa_assign()
2120 if (env->best_cpu != env->dst_cpu && xchg(&rq->numa_migrate_on, 1)) { in task_numa_assign()
2121 int cpu; in task_numa_assign() local
2122 int start = env->dst_cpu; in task_numa_assign()
2124 /* Find alternative idle CPU. */ in task_numa_assign()
2125 for_each_cpu_wrap(cpu, cpumask_of_node(env->dst_nid), start + 1) { in task_numa_assign()
2126 if (cpu == env->best_cpu || !idle_cpu(cpu) || in task_numa_assign()
2127 !cpumask_test_cpu(cpu, env->p->cpus_ptr)) { in task_numa_assign()
2131 env->dst_cpu = cpu; in task_numa_assign()
2132 rq = cpu_rq(env->dst_cpu); in task_numa_assign()
2133 if (!xchg(&rq->numa_migrate_on, 1)) in task_numa_assign()
2137 /* Failed to find an alternative idle CPU */ in task_numa_assign()
2143 * Clear previous best_cpu/rq numa-migrate flag, since task now in task_numa_assign()
2144 * found a better CPU to move/swap. in task_numa_assign()
2146 if (env->best_cpu != -1 && env->best_cpu != env->dst_cpu) { in task_numa_assign()
2147 rq = cpu_rq(env->best_cpu); in task_numa_assign()
2148 WRITE_ONCE(rq->numa_migrate_on, 0); in task_numa_assign()
2151 if (env->best_task) in task_numa_assign()
2152 put_task_struct(env->best_task); in task_numa_assign()
2156 env->best_task = p; in task_numa_assign()
2157 env->best_imp = imp; in task_numa_assign()
2158 env->best_cpu = env->dst_cpu; in task_numa_assign()
2169 * The load is corrected for the CPU capacity available on each node. in load_too_imbalanced()
2172 * ------------ vs --------- in load_too_imbalanced()
2175 src_capacity = env->src_stats.compute_capacity; in load_too_imbalanced()
2176 dst_capacity = env->dst_stats.compute_capacity; in load_too_imbalanced()
2178 imb = abs(dst_load * src_capacity - src_load * dst_capacity); in load_too_imbalanced()
2180 orig_src_load = env->src_stats.load; in load_too_imbalanced()
2181 orig_dst_load = env->dst_stats.load; in load_too_imbalanced()
2183 old_imb = abs(orig_dst_load * src_capacity - orig_src_load * dst_capacity); in load_too_imbalanced()
2205 struct numa_group *cur_ng, *p_ng = deref_curr_numa_group(env->p); in task_numa_compare()
2206 struct rq *dst_rq = cpu_rq(env->dst_cpu); in task_numa_compare()
2210 int dist = env->dist; in task_numa_compare()
2215 if (READ_ONCE(dst_rq->numa_migrate_on)) in task_numa_compare()
2219 cur = rcu_dereference(dst_rq->curr); in task_numa_compare()
2220 if (cur && ((cur->flags & PF_EXITING) || is_idle_task(cur))) in task_numa_compare()
2225 * end try selecting ourselves (current == env->p) as a swap candidate. in task_numa_compare()
2227 if (cur == env->p) { in task_numa_compare()
2233 if (maymove && moveimp >= env->best_imp) in task_numa_compare()
2239 /* Skip this swap candidate if cannot move to the source cpu. */ in task_numa_compare()
2240 if (!cpumask_test_cpu(env->src_cpu, cur->cpus_ptr)) in task_numa_compare()
2247 if (env->best_task && in task_numa_compare()
2248 env->best_task->numa_preferred_nid == env->src_nid && in task_numa_compare()
2249 cur->numa_preferred_nid != env->src_nid) { in task_numa_compare()
2263 cur_ng = rcu_dereference(cur->numa_group); in task_numa_compare()
2267 * no group if there is spare capacity. Swapping does in task_numa_compare()
2271 if (env->dst_stats.node_type == node_has_spare) in task_numa_compare()
2274 imp = taskimp + task_weight(cur, env->src_nid, dist) - in task_numa_compare()
2275 task_weight(cur, env->dst_nid, dist); in task_numa_compare()
2281 imp -= imp / 16; in task_numa_compare()
2288 imp += group_weight(cur, env->src_nid, dist) - in task_numa_compare()
2289 group_weight(cur, env->dst_nid, dist); in task_numa_compare()
2291 imp += task_weight(cur, env->src_nid, dist) - in task_numa_compare()
2292 task_weight(cur, env->dst_nid, dist); in task_numa_compare()
2296 if (cur->numa_preferred_nid == env->dst_nid) in task_numa_compare()
2297 imp -= imp / 16; in task_numa_compare()
2305 if (cur->numa_preferred_nid == env->src_nid) in task_numa_compare()
2308 if (maymove && moveimp > imp && moveimp > env->best_imp) { in task_numa_compare()
2318 if (env->best_task && cur->numa_preferred_nid == env->src_nid && in task_numa_compare()
2319 env->best_task->numa_preferred_nid != env->src_nid) { in task_numa_compare()
2329 if (imp < SMALLIMP || imp <= env->best_imp + SMALLIMP / 2) in task_numa_compare()
2335 load = task_h_load(env->p) - task_h_load(cur); in task_numa_compare()
2339 dst_load = env->dst_stats.load + load; in task_numa_compare()
2340 src_load = env->src_stats.load - load; in task_numa_compare()
2346 /* Evaluate an idle CPU for a task numa move. */ in task_numa_compare()
2348 int cpu = env->dst_stats.idle_cpu; in task_numa_compare() local
2350 /* Nothing cached so current CPU went idle since the search. */ in task_numa_compare()
2351 if (cpu < 0) in task_numa_compare()
2352 cpu = env->dst_cpu; in task_numa_compare()
2355 * If the CPU is no longer truly idle and the previous best CPU in task_numa_compare()
2358 if (!idle_cpu(cpu) && env->best_cpu >= 0 && in task_numa_compare()
2359 idle_cpu(env->best_cpu)) { in task_numa_compare()
2360 cpu = env->best_cpu; in task_numa_compare()
2363 env->dst_cpu = cpu; in task_numa_compare()
2369 * If a move to idle is allowed because there is capacity or load in task_numa_compare()
2373 if (maymove && !cur && env->best_cpu >= 0 && idle_cpu(env->best_cpu)) in task_numa_compare()
2380 if (!maymove && env->best_task && in task_numa_compare()
2381 env->best_task->numa_preferred_nid == env->src_nid) { in task_numa_compare()
2394 int cpu; in task_numa_find_cpu() local
2397 * If dst node has spare capacity, then check if there is an in task_numa_find_cpu()
2400 if (env->dst_stats.node_type == node_has_spare) { in task_numa_find_cpu()
2408 * CPU load balancer. in task_numa_find_cpu()
2410 src_running = env->src_stats.nr_running - 1; in task_numa_find_cpu()
2411 dst_running = env->dst_stats.nr_running + 1; in task_numa_find_cpu()
2412 imbalance = max(0, dst_running - src_running); in task_numa_find_cpu()
2414 env->imb_numa_nr); in task_numa_find_cpu()
2416 /* Use idle CPU if there is no imbalance */ in task_numa_find_cpu()
2419 if (env->dst_stats.idle_cpu >= 0) { in task_numa_find_cpu()
2420 env->dst_cpu = env->dst_stats.idle_cpu; in task_numa_find_cpu()
2428 * If the improvement from just moving env->p direction is better in task_numa_find_cpu()
2431 load = task_h_load(env->p); in task_numa_find_cpu()
2432 dst_load = env->dst_stats.load + load; in task_numa_find_cpu()
2433 src_load = env->src_stats.load - load; in task_numa_find_cpu()
2437 for_each_cpu(cpu, cpumask_of_node(env->dst_nid)) { in task_numa_find_cpu()
2438 /* Skip this CPU if the source task cannot migrate */ in task_numa_find_cpu()
2439 if (!cpumask_test_cpu(cpu, env->p->cpus_ptr)) in task_numa_find_cpu()
2442 env->dst_cpu = cpu; in task_numa_find_cpu()
2460 .best_cpu = -1, in task_numa_migrate()
2474 * random movement of tasks -- counter the numa conditions we're trying in task_numa_migrate()
2480 env.imbalance_pct = 100 + (sd->imbalance_pct - 100) / 2; in task_numa_migrate()
2481 env.imb_numa_nr = sd->imb_numa_nr; in task_numa_migrate()
2493 return -EINVAL; in task_numa_migrate()
2496 env.dst_nid = p->numa_preferred_nid; in task_numa_migrate()
2501 taskimp = task_weight(p, env.dst_nid, dist) - taskweight; in task_numa_migrate()
2502 groupimp = group_weight(p, env.dst_nid, dist) - groupweight; in task_numa_migrate()
2510 * - there is no space available on the preferred_nid in task_numa_migrate()
2511 * - the task is part of a numa_group that is interleaved across in task_numa_migrate()
2516 if (env.best_cpu == -1 || (ng && ng->active_nodes > 1)) { in task_numa_migrate()
2518 if (nid == env.src_nid || nid == p->numa_preferred_nid) in task_numa_migrate()
2529 taskimp = task_weight(p, nid, dist) - taskweight; in task_numa_migrate()
2530 groupimp = group_weight(p, nid, dist) - groupweight; in task_numa_migrate()
2550 if (env.best_cpu == -1) in task_numa_migrate()
2555 if (nid != p->numa_preferred_nid) in task_numa_migrate()
2559 /* No better CPU than the current one was found. */ in task_numa_migrate()
2560 if (env.best_cpu == -1) { in task_numa_migrate()
2561 trace_sched_stick_numa(p, env.src_cpu, NULL, -1); in task_numa_migrate()
2562 return -EAGAIN; in task_numa_migrate()
2568 WRITE_ONCE(best_rq->numa_migrate_on, 0); in task_numa_migrate()
2575 WRITE_ONCE(best_rq->numa_migrate_on, 0); in task_numa_migrate()
2583 /* Attempt to migrate a task to a CPU on the preferred node. */
2589 if (unlikely(p->numa_preferred_nid == NUMA_NO_NODE || !p->numa_faults)) in numa_migrate_preferred()
2593 interval = min(interval, msecs_to_jiffies(p->numa_scan_period) / 16); in numa_migrate_preferred()
2594 p->numa_migrate_retry = jiffies + interval; in numa_migrate_preferred()
2596 /* Success if task is already running on preferred CPU */ in numa_migrate_preferred()
2597 if (task_node(p) == p->numa_preferred_nid) in numa_migrate_preferred()
2600 /* Otherwise, try migrate to a CPU on the preferred node */ in numa_migrate_preferred()
2627 numa_group->max_faults_cpu = max_faults; in numa_group_count_active_nodes()
2628 numa_group->active_nodes = active_nodes; in numa_group_count_active_nodes()
2654 unsigned long remote = p->numa_faults_locality[0]; in update_task_scan_period()
2655 unsigned long local = p->numa_faults_locality[1]; in update_task_scan_period()
2664 if (local + shared == 0 || p->numa_faults_locality[2]) { in update_task_scan_period()
2665 p->numa_scan_period = min(p->numa_scan_period_max, in update_task_scan_period()
2666 p->numa_scan_period << 1); in update_task_scan_period()
2668 p->mm->numa_next_scan = jiffies + in update_task_scan_period()
2669 msecs_to_jiffies(p->numa_scan_period); in update_task_scan_period()
2680 period_slot = DIV_ROUND_UP(p->numa_scan_period, NUMA_PERIOD_SLOTS); in update_task_scan_period()
2689 int slot = ps_ratio - NUMA_PERIOD_THRESHOLD; in update_task_scan_period()
2699 int slot = lr_ratio - NUMA_PERIOD_THRESHOLD; in update_task_scan_period()
2705 * Private memory faults exceed (SLOTS-THRESHOLD)/SLOTS, in update_task_scan_period()
2710 diff = -(NUMA_PERIOD_THRESHOLD - ratio) * period_slot; in update_task_scan_period()
2713 p->numa_scan_period = clamp(p->numa_scan_period + diff, in update_task_scan_period()
2715 memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); in update_task_scan_period()
2722 * from the dozens-of-seconds NUMA balancing period. Use the scheduler
2729 now = p->se.exec_start; in numa_get_avg_runtime()
2730 runtime = p->se.sum_exec_runtime; in numa_get_avg_runtime()
2732 if (p->last_task_numa_placement) { in numa_get_avg_runtime()
2733 delta = runtime - p->last_sum_exec_runtime; in numa_get_avg_runtime()
2734 *period = now - p->last_task_numa_placement; in numa_get_avg_runtime()
2740 delta = p->se.avg.load_sum; in numa_get_avg_runtime()
2744 p->last_sum_exec_runtime = runtime; in numa_get_avg_runtime()
2745 p->last_task_numa_placement = now; in numa_get_avg_runtime()
2795 for (dist = sched_max_numa_distance; dist > LOCAL_DISTANCE; dist--) { in preferred_group_nid()
2849 * The p->mm->numa_scan_seq field gets updated without in task_numa_placement()
2853 seq = READ_ONCE(p->mm->numa_scan_seq); in task_numa_placement()
2854 if (p->numa_scan_seq == seq) in task_numa_placement()
2856 p->numa_scan_seq = seq; in task_numa_placement()
2857 p->numa_scan_period_max = task_scan_max(p); in task_numa_placement()
2859 total_faults = p->numa_faults_locality[0] + in task_numa_placement()
2860 p->numa_faults_locality[1]; in task_numa_placement()
2866 group_lock = &ng->lock; in task_numa_placement()
2886 diff = p->numa_faults[membuf_idx] - p->numa_faults[mem_idx] / 2; in task_numa_placement()
2887 fault_types[priv] += p->numa_faults[membuf_idx]; in task_numa_placement()
2888 p->numa_faults[membuf_idx] = 0; in task_numa_placement()
2892 * count according to CPU use, instead of by the raw in task_numa_placement()
2894 * little over-all impact on throughput, and thus their in task_numa_placement()
2898 f_weight = (f_weight * p->numa_faults[cpubuf_idx]) / in task_numa_placement()
2900 f_diff = f_weight - p->numa_faults[cpu_idx] / 2; in task_numa_placement()
2901 p->numa_faults[cpubuf_idx] = 0; in task_numa_placement()
2903 p->numa_faults[mem_idx] += diff; in task_numa_placement()
2904 p->numa_faults[cpu_idx] += f_diff; in task_numa_placement()
2905 faults += p->numa_faults[mem_idx]; in task_numa_placement()
2906 p->total_numa_faults += diff; in task_numa_placement()
2915 ng->faults[mem_idx] += diff; in task_numa_placement()
2916 ng->faults[cpu_idx] += f_diff; in task_numa_placement()
2917 ng->total_faults += diff; in task_numa_placement()
2918 group_faults += ng->faults[mem_idx]; in task_numa_placement()
2933 /* Cannot migrate task to CPU-less node */ in task_numa_placement()
2956 if (max_nid != p->numa_preferred_nid) in task_numa_placement()
2965 return refcount_inc_not_zero(&grp->refcount); in get_numa_group()
2970 if (refcount_dec_and_test(&grp->refcount)) in put_numa_group()
2980 int cpu = cpupid_to_cpu(cpupid); in task_numa_group() local
2992 refcount_set(&grp->refcount, 1); in task_numa_group()
2993 grp->active_nodes = 1; in task_numa_group()
2994 grp->max_faults_cpu = 0; in task_numa_group()
2995 spin_lock_init(&grp->lock); in task_numa_group()
2996 grp->gid = p->pid; in task_numa_group()
2999 grp->faults[i] = p->numa_faults[i]; in task_numa_group()
3001 grp->total_faults = p->total_numa_faults; in task_numa_group()
3003 grp->nr_tasks++; in task_numa_group()
3004 rcu_assign_pointer(p->numa_group, grp); in task_numa_group()
3008 tsk = READ_ONCE(cpu_rq(cpu)->curr); in task_numa_group()
3013 grp = rcu_dereference(tsk->numa_group); in task_numa_group()
3025 if (my_grp->nr_tasks > grp->nr_tasks) in task_numa_group()
3029 * Tie-break on the grp address. in task_numa_group()
3031 if (my_grp->nr_tasks == grp->nr_tasks && my_grp > grp) in task_numa_group()
3035 if (tsk->mm == current->mm) in task_numa_group()
3054 double_lock_irq(&my_grp->lock, &grp->lock); in task_numa_group()
3057 my_grp->faults[i] -= p->numa_faults[i]; in task_numa_group()
3058 grp->faults[i] += p->numa_faults[i]; in task_numa_group()
3060 my_grp->total_faults -= p->total_numa_faults; in task_numa_group()
3061 grp->total_faults += p->total_numa_faults; in task_numa_group()
3063 my_grp->nr_tasks--; in task_numa_group()
3064 grp->nr_tasks++; in task_numa_group()
3066 spin_unlock(&my_grp->lock); in task_numa_group()
3067 spin_unlock_irq(&grp->lock); in task_numa_group()
3069 rcu_assign_pointer(p->numa_group, grp); in task_numa_group()
3084 * reset the data back to default state without freeing ->numa_faults.
3089 struct numa_group *grp = rcu_dereference_raw(p->numa_group); in task_numa_free()
3090 unsigned long *numa_faults = p->numa_faults; in task_numa_free()
3098 spin_lock_irqsave(&grp->lock, flags); in task_numa_free()
3100 grp->faults[i] -= p->numa_faults[i]; in task_numa_free()
3101 grp->total_faults -= p->total_numa_faults; in task_numa_free()
3103 grp->nr_tasks--; in task_numa_free()
3104 spin_unlock_irqrestore(&grp->lock, flags); in task_numa_free()
3105 RCU_INIT_POINTER(p->numa_group, NULL); in task_numa_free()
3110 p->numa_faults = NULL; in task_numa_free()
3113 p->total_numa_faults = 0; in task_numa_free()
3135 if (!p->mm) in task_numa_fault()
3147 /* Allocate buffer to track faults on a per-node basis */ in task_numa_fault()
3148 if (unlikely(!p->numa_faults)) { in task_numa_fault()
3149 int size = sizeof(*p->numa_faults) * in task_numa_fault()
3152 p->numa_faults = kzalloc(size, GFP_KERNEL|__GFP_NOWARN); in task_numa_fault()
3153 if (!p->numa_faults) in task_numa_fault()
3156 p->total_numa_faults = 0; in task_numa_fault()
3157 memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); in task_numa_fault()
3164 if (unlikely(last_cpupid == (-1 & LAST_CPUPID_MASK))) { in task_numa_fault()
3179 if (!priv && !local && ng && ng->active_nodes > 1 && in task_numa_fault()
3188 if (time_after(jiffies, p->numa_migrate_retry)) { in task_numa_fault()
3194 p->numa_pages_migrated += pages; in task_numa_fault()
3196 p->numa_faults_locality[2] += pages; in task_numa_fault()
3198 p->numa_faults[task_faults_idx(NUMA_MEMBUF, mem_node, priv)] += pages; in task_numa_fault()
3199 p->numa_faults[task_faults_idx(NUMA_CPUBUF, cpu_node, priv)] += pages; in task_numa_fault()
3200 p->numa_faults_locality[local] += pages; in task_numa_fault()
3207 * p->mm->numa_scan_seq is written to without exclusive access in reset_ptenuma_scan()
3213 WRITE_ONCE(p->mm->numa_scan_seq, READ_ONCE(p->mm->numa_scan_seq) + 1); in reset_ptenuma_scan()
3214 p->mm->numa_scan_offset = 0; in reset_ptenuma_scan()
3226 if ((READ_ONCE(current->mm->numa_scan_seq) - vma->numab_state->start_scan_seq) < 2) in vma_is_accessed()
3229 pids = vma->numab_state->pids_active[0] | vma->numab_state->pids_active[1]; in vma_is_accessed()
3230 if (test_bit(hash_32(current->pid, ilog2(BITS_PER_LONG)), &pids)) in vma_is_accessed()
3235 * some VMAs may never be scanned in multi-threaded applications: in vma_is_accessed()
3237 if (mm->numa_scan_offset > vma->vm_start) { in vma_is_accessed()
3247 if (READ_ONCE(mm->numa_scan_seq) > in vma_is_accessed()
3248 (vma->numab_state->prev_scan_seq + get_nr_threads(current))) in vma_is_accessed()
3264 struct mm_struct *mm = p->mm; in task_numa_work()
3265 u64 runtime = p->se.sum_exec_runtime; in task_numa_work()
3276 work->next = work; in task_numa_work()
3280 * NOTE: make sure not to dereference p->mm before this check, in task_numa_work()
3282 * without p->mm even though we still had it when we enqueued this in task_numa_work()
3285 if (p->flags & PF_EXITING) in task_numa_work()
3288 if (!mm->numa_next_scan) { in task_numa_work()
3289 mm->numa_next_scan = now + in task_numa_work()
3296 migrate = mm->numa_next_scan; in task_numa_work()
3300 if (p->numa_scan_period == 0) { in task_numa_work()
3301 p->numa_scan_period_max = task_scan_max(p); in task_numa_work()
3302 p->numa_scan_period = task_scan_start(p); in task_numa_work()
3305 next_scan = now + msecs_to_jiffies(p->numa_scan_period); in task_numa_work()
3306 if (!try_cmpxchg(&mm->numa_next_scan, &migrate, next_scan)) in task_numa_work()
3313 p->node_stamp += 2 * TICK_NSEC; in task_numa_work()
3316 pages <<= 20 - PAGE_SHIFT; /* MB in pages */ in task_numa_work()
3333 start = mm->numa_scan_offset; in task_numa_work()
3345 is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_MIXEDMAP)) { in task_numa_work()
3353 * hinting faults in read-only file-backed mappings or the vdso in task_numa_work()
3356 if (!vma->vm_mm || in task_numa_work()
3357 (vma->vm_file && (vma->vm_flags & (VM_READ|VM_WRITE)) == (VM_READ))) { in task_numa_work()
3371 /* Initialise new per-VMA NUMAB state. */ in task_numa_work()
3372 if (!vma->numab_state) { in task_numa_work()
3379 if (cmpxchg(&vma->numab_state, NULL, ptr)) { in task_numa_work()
3384 vma->numab_state->start_scan_seq = mm->numa_scan_seq; in task_numa_work()
3386 vma->numab_state->next_scan = now + in task_numa_work()
3390 vma->numab_state->pids_active_reset = vma->numab_state->next_scan + in task_numa_work()
3398 vma->numab_state->prev_scan_seq = mm->numa_scan_seq - 1; in task_numa_work()
3405 if (mm->numa_scan_seq && time_before(jiffies, in task_numa_work()
3406 vma->numab_state->next_scan)) { in task_numa_work()
3412 if (mm->numa_scan_seq && in task_numa_work()
3413 time_after(jiffies, vma->numab_state->pids_active_reset)) { in task_numa_work()
3414 vma->numab_state->pids_active_reset = vma->numab_state->pids_active_reset + in task_numa_work()
3416 vma->numab_state->pids_active[0] = READ_ONCE(vma->numab_state->pids_active[1]); in task_numa_work()
3417 vma->numab_state->pids_active[1] = 0; in task_numa_work()
3421 if (vma->numab_state->prev_scan_seq == mm->numa_scan_seq) { in task_numa_work()
3422 mm->numa_scan_offset = vma->vm_end; in task_numa_work()
3438 start = max(start, vma->vm_start); in task_numa_work()
3440 end = min(end, vma->vm_end); in task_numa_work()
3446 * is not already pte-numa. If the VMA contains in task_numa_work()
3452 pages -= (end - start) >> PAGE_SHIFT; in task_numa_work()
3453 virtpages -= (end - start) >> PAGE_SHIFT; in task_numa_work()
3460 } while (end != vma->vm_end); in task_numa_work()
3463 vma->numab_state->prev_scan_seq = mm->numa_scan_seq; in task_numa_work()
3491 mm->numa_scan_offset = start; in task_numa_work()
3502 if (unlikely(p->se.sum_exec_runtime != runtime)) { in task_numa_work()
3503 u64 diff = p->se.sum_exec_runtime - runtime; in task_numa_work()
3504 p->node_stamp += 32 * diff; in task_numa_work()
3511 struct mm_struct *mm = p->mm; in init_numa_balancing()
3514 mm_users = atomic_read(&mm->mm_users); in init_numa_balancing()
3516 mm->numa_next_scan = jiffies + msecs_to_jiffies(sysctl_numa_balancing_scan_delay); in init_numa_balancing()
3517 mm->numa_scan_seq = 0; in init_numa_balancing()
3520 p->node_stamp = 0; in init_numa_balancing()
3521 p->numa_scan_seq = mm ? mm->numa_scan_seq : 0; in init_numa_balancing()
3522 p->numa_scan_period = sysctl_numa_balancing_scan_delay; in init_numa_balancing()
3523 p->numa_migrate_retry = 0; in init_numa_balancing()
3525 p->numa_work.next = &p->numa_work; in init_numa_balancing()
3526 p->numa_faults = NULL; in init_numa_balancing()
3527 p->numa_pages_migrated = 0; in init_numa_balancing()
3528 p->total_numa_faults = 0; in init_numa_balancing()
3529 RCU_INIT_POINTER(p->numa_group, NULL); in init_numa_balancing()
3530 p->last_task_numa_placement = 0; in init_numa_balancing()
3531 p->last_sum_exec_runtime = 0; in init_numa_balancing()
3533 init_task_work(&p->numa_work, task_numa_work); in init_numa_balancing()
3537 p->numa_preferred_nid = NUMA_NO_NODE; in init_numa_balancing()
3549 current->numa_scan_period * mm_users * NSEC_PER_MSEC); in init_numa_balancing()
3551 p->node_stamp = delay; in init_numa_balancing()
3560 struct callback_head *work = &curr->numa_work; in task_tick_numa()
3566 if (!curr->mm || (curr->flags & (PF_EXITING | PF_KTHREAD)) || work->next != work) in task_tick_numa()
3575 now = curr->se.sum_exec_runtime; in task_tick_numa()
3576 period = (u64)curr->numa_scan_period * NSEC_PER_MSEC; in task_tick_numa()
3578 if (now > curr->node_stamp + period) { in task_tick_numa()
3579 if (!curr->node_stamp) in task_tick_numa()
3580 curr->numa_scan_period = task_scan_start(curr); in task_tick_numa()
3581 curr->node_stamp += period; in task_tick_numa()
3583 if (!time_before(jiffies, curr->mm->numa_next_scan)) in task_tick_numa()
3596 if (!p->mm || !p->numa_faults || (p->flags & PF_EXITING)) in update_scan_period()
3605 * is pulled cross-node due to wakeups or load balancing. in update_scan_period()
3607 if (p->numa_scan_seq) { in update_scan_period()
3613 if (dst_nid == p->numa_preferred_nid || in update_scan_period()
3614 (p->numa_preferred_nid != NUMA_NO_NODE && in update_scan_period()
3615 src_nid != p->numa_preferred_nid)) in update_scan_period()
3619 p->numa_scan_period = task_scan_start(p); in update_scan_period()
3644 update_load_add(&cfs_rq->load, se->load.weight); in account_entity_enqueue()
3650 list_add(&se->group_node, &rq->cfs_tasks); in account_entity_enqueue()
3653 cfs_rq->nr_running++; in account_entity_enqueue()
3655 cfs_rq->idle_nr_running++; in account_entity_enqueue()
3661 update_load_sub(&cfs_rq->load, se->load.weight); in account_entity_dequeue()
3665 list_del_init(&se->group_node); in account_entity_dequeue()
3668 cfs_rq->nr_running--; in account_entity_dequeue()
3670 cfs_rq->idle_nr_running--; in account_entity_dequeue()
3676 * Explicitly do a load-store to ensure the intermediate value never hits
3696 * Explicitly do a load-store to ensure the intermediate value never hits
3704 res = var - val; \
3713 * A variant of sub_positive(), which does not use explicit load-store
3718 *ptr -= min_t(typeof(*ptr), *ptr, _val); \
3725 cfs_rq->avg.load_avg += se->avg.load_avg; in enqueue_load_avg()
3726 cfs_rq->avg.load_sum += se_weight(se) * se->avg.load_sum; in enqueue_load_avg()
3732 sub_positive(&cfs_rq->avg.load_avg, se->avg.load_avg); in dequeue_load_avg()
3733 sub_positive(&cfs_rq->avg.load_sum, se_weight(se) * se->avg.load_sum); in dequeue_load_avg()
3735 cfs_rq->avg.load_sum = max_t(u32, cfs_rq->avg.load_sum, in dequeue_load_avg()
3736 cfs_rq->avg.load_avg * PELT_MIN_DIVIDER); in dequeue_load_avg()
3748 unsigned long old_weight = se->load.weight; in reweight_eevdf()
3756 * adjusted if re-weight at !0-lag point. in reweight_eevdf()
3759 * re-weight without changing vruntime at !0-lag point. in reweight_eevdf()
3761 * Weight VRuntime Avg-VRuntime in reweight_eevdf()
3765 * Since lag needs to be preserved through re-weight: in reweight_eevdf()
3767 * lag = (V - v)*w = (V'- v')*w', where v = v' in reweight_eevdf()
3768 * ==> V' = (V - v)*w/w' + v (1) in reweight_eevdf()
3773 * V' = (WV + w'v - wv) / (W + w' - w) (2) in reweight_eevdf()
3777 * (WV + w'v - wv) / (W + w' - w) = (V - v)*w/w' + v in reweight_eevdf()
3778 * ==> (WV-Wv+Wv+w'v-wv)/(W+w'-w) = (V - v)*w/w' + v in reweight_eevdf()
3779 * ==> (WV - Wv)/(W + w' - w) + v = (V - v)*w/w' + v in reweight_eevdf()
3780 * ==> (V - v)*W/(W + w' - w) = (V - v)*w/w' (3) in reweight_eevdf()
3782 * Since we are doing at !0-lag point which means V != v, we in reweight_eevdf()
3785 * ==> W / (W + w' - w) = w / w' in reweight_eevdf()
3786 * ==> Ww' = Ww + ww' - ww in reweight_eevdf()
3787 * ==> W * (w' - w) = w * (w' - w) in reweight_eevdf()
3788 * ==> W = w (re-weight indicates w' != w) in reweight_eevdf()
3792 * average vruntime @V, which means we will always re-weight in reweight_eevdf()
3793 * at 0-lag point, thus breach assumption. Proof completed. in reweight_eevdf()
3796 * COROLLARY #2: Re-weight does NOT affect weighted average in reweight_eevdf()
3801 * (V - v)*w = (V' - v')*w' in reweight_eevdf()
3802 * ==> v' = V' - (V - v)*w/w' (4) in reweight_eevdf()
3806 * V' = (WV - wv + w'v') / (W - w + w') in reweight_eevdf()
3807 * = (WV - wv + w'(V' - (V - v)w/w')) / (W - w + w') in reweight_eevdf()
3808 * = (WV - wv + w'V' - Vw + wv) / (W - w + w') in reweight_eevdf()
3809 * = (WV + w'V' - Vw) / (W - w + w') in reweight_eevdf()
3811 * ==> V'*(W - w + w') = WV + w'V' - Vw in reweight_eevdf()
3812 * ==> V' * (W - w) = (W - w) * V (5) in reweight_eevdf()
3815 * always occurs at 0-lag point, so V won't change. Or else in reweight_eevdf()
3820 * So according to corollary #1 & #2, the effect of re-weight in reweight_eevdf()
3823 * v' = V' - (V - v) * w / w' (4) in reweight_eevdf()
3824 * = V - (V - v) * w / w' in reweight_eevdf()
3825 * = V - vl * w / w' in reweight_eevdf()
3826 * = V - vl' in reweight_eevdf()
3828 if (avruntime != se->vruntime) { in reweight_eevdf()
3831 se->vruntime = avruntime - vlag; in reweight_eevdf()
3841 * d' = v' + (d - v)*w/w' in reweight_eevdf()
3842 * = V' - (V - v)*w/w' + (d - v)*w/w' in reweight_eevdf()
3843 * = V - (V - v)*w/w' + (d - v)*w/w' in reweight_eevdf()
3844 * = V + (d - V)*w/w' in reweight_eevdf()
3846 vslice = (s64)(se->deadline - avruntime); in reweight_eevdf()
3848 se->deadline = avruntime + vslice; in reweight_eevdf()
3854 bool curr = cfs_rq->curr == se; in reweight_entity()
3857 if (se->on_rq) { in reweight_entity()
3863 update_load_sub(&cfs_rq->load, se->load.weight); in reweight_entity()
3867 if (se->on_rq) { in reweight_entity()
3871 * Because we keep se->vlag = V - v_i, while: lag_i = w_i*(V - v_i), in reweight_entity()
3872 * we need to scale se->vlag when w_i changes. in reweight_entity()
3874 se->vlag = div_s64(se->vlag * se->load.weight, weight); in reweight_entity()
3877 update_load_set(&se->load, weight); in reweight_entity()
3881 u32 divider = get_pelt_divider(&se->avg); in reweight_entity()
3883 se->avg.load_avg = div_u64(se_weight(se) * se->avg.load_sum, divider); in reweight_entity()
3888 if (se->on_rq) { in reweight_entity()
3889 update_load_add(&cfs_rq->load, se->load.weight); in reweight_entity()
3895 * whether the rq-wide min_vruntime needs updated too. Since in reweight_entity()
3897 * than up-to-date one, we do the update at the end of the in reweight_entity()
3906 struct sched_entity *se = &p->se; in reweight_task()
3908 struct load_weight *load = &se->load; in reweight_task()
3910 reweight_entity(cfs_rq, se, lw->weight); in reweight_task()
3911 load->inv_weight = lw->inv_weight; in reweight_task()
3925 * tg->weight * grq->load.weight
3926 * ge->load.weight = ----------------------------- (1)
3927 * \Sum grq->load.weight
3935 * grq->load.weight -> grq->avg.load_avg (2)
3939 * tg->weight * grq->avg.load_avg
3940 * ge->load.weight = ------------------------------ (3)
3941 * tg->load_avg
3943 * Where: tg->load_avg ~= \Sum grq->avg.load_avg
3947 * The problem with it is that because the average is slow -- it was designed
3948 * to be exactly that of course -- this leads to transients in boundary
3950 * one task. It takes time for our CPU's grq->avg.load_avg to build up,
3955 * tg->weight * grq->load.weight
3956 * ge->load.weight = ----------------------------- = tg->weight (4)
3957 * grp->load.weight
3964 * ge->load.weight =
3966 * tg->weight * grq->load.weight
3967 * --------------------------------------------------- (5)
3968 * tg->load_avg - grq->avg.load_avg + grq->load.weight
3970 * But because grq->load.weight can drop to 0, resulting in a divide by zero,
3971 * we need to use grq->avg.load_avg as its lower bound, which then gives:
3974 * tg->weight * grq->load.weight
3975 * ge->load.weight = ----------------------------- (6)
3980 * tg_load_avg' = tg->load_avg - grq->avg.load_avg +
3981 * max(grq->load.weight, grq->avg.load_avg)
3985 * overestimates the ge->load.weight and therefore:
3987 * \Sum ge->load.weight >= tg->weight
3994 struct task_group *tg = cfs_rq->tg; in calc_group_shares()
3996 tg_shares = READ_ONCE(tg->shares); in calc_group_shares()
3998 load = max(scale_load_down(cfs_rq->load.weight), cfs_rq->avg.load_avg); in calc_group_shares()
4000 tg_weight = atomic_long_read(&tg->load_avg); in calc_group_shares()
4003 tg_weight -= cfs_rq->tg_load_avg_contrib; in calc_group_shares()
4011 * MIN_SHARES has to be unscaled here to support per-CPU partitioning in calc_group_shares()
4012 * of a group with small tg->shares value. It is a floor value which is in calc_group_shares()
4014 * the group on a CPU. in calc_group_shares()
4016 * E.g. on 64-bit for a group with tg->shares of scale_load(15)=15*1024 in calc_group_shares()
4017 * on an 8-core system with 8 tasks each runnable on one CPU shares has in calc_group_shares()
4019 * case no task is runnable on a CPU MIN_SHARES=2 should be returned in calc_group_shares()
4042 shares = READ_ONCE(gcfs_rq->tg->shares); in update_cfs_group()
4046 if (unlikely(se->load.weight != shares)) in update_cfs_group()
4060 if (&rq->cfs == cfs_rq) { in cfs_rq_util_change()
4070 * As is, the util number is not freq-invariant (we'd have to in cfs_rq_util_change()
4082 if (sa->load_sum) in load_avg_is_decayed()
4085 if (sa->util_sum) in load_avg_is_decayed()
4088 if (sa->runnable_sum) in load_avg_is_decayed()
4096 SCHED_WARN_ON(sa->load_avg || in load_avg_is_decayed()
4097 sa->util_avg || in load_avg_is_decayed()
4098 sa->runnable_avg); in load_avg_is_decayed()
4105 return u64_u32_load_copy(cfs_rq->avg.last_update_time, in cfs_rq_last_update_time()
4106 cfs_rq->last_update_time_copy); in cfs_rq_last_update_time()
4112 * bottom-up, we only have to test whether the cfs_rq before us on the list
4123 if (cfs_rq->on_list) { in child_cfs_rq_on_list()
4124 prev = cfs_rq->leaf_cfs_rq_list.prev; in child_cfs_rq_on_list()
4126 prev = rq->tmp_alone_branch; in child_cfs_rq_on_list()
4129 if (prev == &rq->leaf_cfs_rq_list) in child_cfs_rq_on_list()
4134 return (prev_cfs_rq->tg->parent == cfs_rq->tg); in child_cfs_rq_on_list()
4139 if (cfs_rq->load.weight) in cfs_rq_is_decayed()
4142 if (!load_avg_is_decayed(&cfs_rq->avg)) in cfs_rq_is_decayed()
4152 * update_tg_load_avg - update the tg's load avg
4155 * This function 'ensures': tg->load_avg := \Sum tg->cfs_rq[]->avg.load.
4156 * However, because tg->load_avg is a global value there are performance
4167 long delta = cfs_rq->avg.load_avg - cfs_rq->tg_load_avg_contrib; in update_tg_load_avg()
4172 if (cfs_rq->tg == &root_task_group) in update_tg_load_avg()
4175 if (abs(delta) > cfs_rq->tg_load_avg_contrib / 64) { in update_tg_load_avg()
4176 atomic_long_add(delta, &cfs_rq->tg->load_avg); in update_tg_load_avg()
4177 cfs_rq->tg_load_avg_contrib = cfs_rq->avg.load_avg; in update_tg_load_avg()
4182 * Called within set_task_rq() right before setting a task's CPU. The
4183 * caller only guarantees p->pi_lock is held; no other assumptions,
4184 * including the state of rq->lock, should be made.
4197 * date and ready to go to new CPU/cfs_rq. But we have difficulty in in set_task_rq_fair()
4198 * getting what current time is, so simply throw away the out-of-date in set_task_rq_fair()
4202 if (!(se->avg.last_update_time && prev)) in set_task_rq_fair()
4209 se->avg.last_update_time = n_last_update_time; in set_task_rq_fair()
4217 * ge->avg == grq->avg (1)
4228 * ge->avg.load_avg = ge->load.weight * ge->avg.runnable_avg (2)
4233 * grq->avg.load_avg = grq->load.weight * grq->avg.runnable_avg (3)
4237 * ge->avg.runnable_avg == grq->avg.runnable_avg
4241 * ge->load.weight * grq->avg.load_avg
4242 * ge->avg.load_avg = ----------------------------------- (4)
4243 * grq->load.weight
4256 * Another reason this doesn't work is that runnable isn't a 0-sum entity.
4267 * ge->avg.running_sum <= ge->avg.runnable_sum <= LOAD_AVG_MAX
4274 * grq->avg.runnable_sum = grq->avg.load_sum / grq->load.weight
4282 long delta_sum, delta_avg = gcfs_rq->avg.util_avg - se->avg.util_avg; in update_tg_cfs_util()
4290 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in update_tg_cfs_util()
4293 divider = get_pelt_divider(&cfs_rq->avg); in update_tg_cfs_util()
4297 se->avg.util_avg = gcfs_rq->avg.util_avg; in update_tg_cfs_util()
4298 new_sum = se->avg.util_avg * divider; in update_tg_cfs_util()
4299 delta_sum = (long)new_sum - (long)se->avg.util_sum; in update_tg_cfs_util()
4300 se->avg.util_sum = new_sum; in update_tg_cfs_util()
4303 add_positive(&cfs_rq->avg.util_avg, delta_avg); in update_tg_cfs_util()
4304 add_positive(&cfs_rq->avg.util_sum, delta_sum); in update_tg_cfs_util()
4307 cfs_rq->avg.util_sum = max_t(u32, cfs_rq->avg.util_sum, in update_tg_cfs_util()
4308 cfs_rq->avg.util_avg * PELT_MIN_DIVIDER); in update_tg_cfs_util()
4314 long delta_sum, delta_avg = gcfs_rq->avg.runnable_avg - se->avg.runnable_avg; in update_tg_cfs_runnable()
4322 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in update_tg_cfs_runnable()
4325 divider = get_pelt_divider(&cfs_rq->avg); in update_tg_cfs_runnable()
4328 se->avg.runnable_avg = gcfs_rq->avg.runnable_avg; in update_tg_cfs_runnable()
4329 new_sum = se->avg.runnable_avg * divider; in update_tg_cfs_runnable()
4330 delta_sum = (long)new_sum - (long)se->avg.runnable_sum; in update_tg_cfs_runnable()
4331 se->avg.runnable_sum = new_sum; in update_tg_cfs_runnable()
4334 add_positive(&cfs_rq->avg.runnable_avg, delta_avg); in update_tg_cfs_runnable()
4335 add_positive(&cfs_rq->avg.runnable_sum, delta_sum); in update_tg_cfs_runnable()
4337 cfs_rq->avg.runnable_sum = max_t(u32, cfs_rq->avg.runnable_sum, in update_tg_cfs_runnable()
4338 cfs_rq->avg.runnable_avg * PELT_MIN_DIVIDER); in update_tg_cfs_runnable()
4344 long delta_avg, running_sum, runnable_sum = gcfs_rq->prop_runnable_sum; in update_tg_cfs_load()
4353 gcfs_rq->prop_runnable_sum = 0; in update_tg_cfs_load()
4356 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in update_tg_cfs_load()
4359 divider = get_pelt_divider(&cfs_rq->avg); in update_tg_cfs_load()
4364 * the CPU is saturated running == runnable. in update_tg_cfs_load()
4366 runnable_sum += se->avg.load_sum; in update_tg_cfs_load()
4373 if (scale_load_down(gcfs_rq->load.weight)) { in update_tg_cfs_load()
4374 load_sum = div_u64(gcfs_rq->avg.load_sum, in update_tg_cfs_load()
4375 scale_load_down(gcfs_rq->load.weight)); in update_tg_cfs_load()
4379 runnable_sum = min(se->avg.load_sum, load_sum); in update_tg_cfs_load()
4388 running_sum = se->avg.util_sum >> SCHED_CAPACITY_SHIFT; in update_tg_cfs_load()
4394 delta_avg = load_avg - se->avg.load_avg; in update_tg_cfs_load()
4398 delta_sum = load_sum - (s64)se_weight(se) * se->avg.load_sum; in update_tg_cfs_load()
4400 se->avg.load_sum = runnable_sum; in update_tg_cfs_load()
4401 se->avg.load_avg = load_avg; in update_tg_cfs_load()
4402 add_positive(&cfs_rq->avg.load_avg, delta_avg); in update_tg_cfs_load()
4403 add_positive(&cfs_rq->avg.load_sum, delta_sum); in update_tg_cfs_load()
4405 cfs_rq->avg.load_sum = max_t(u32, cfs_rq->avg.load_sum, in update_tg_cfs_load()
4406 cfs_rq->avg.load_avg * PELT_MIN_DIVIDER); in update_tg_cfs_load()
4411 cfs_rq->propagate = 1; in add_tg_cfs_propagate()
4412 cfs_rq->prop_runnable_sum += runnable_sum; in add_tg_cfs_propagate()
4424 if (!gcfs_rq->propagate) in propagate_entity_load_avg()
4427 gcfs_rq->propagate = 0; in propagate_entity_load_avg()
4431 add_tg_cfs_propagate(cfs_rq, gcfs_rq->prop_runnable_sum); in propagate_entity_load_avg()
4455 if (se->avg.load_avg || se->avg.util_avg) in skip_blocked_update()
4462 if (gcfs_rq->propagate) in skip_blocked_update()
4494 if (load_avg_is_decayed(&se->avg)) in migrate_se_pelt_lag()
4501 is_idle = is_idle_task(rcu_dereference(rq->curr)); in migrate_se_pelt_lag()
4506 * time. Hence, limiting to the case where the source CPU is idle and in migrate_se_pelt_lag()
4518 * - cfs->throttled_clock_pelt_time@cfs_rq_idle in migrate_se_pelt_lag()
4521 * = rq_clock_pelt()@rq_idle - rq_clock_pelt()@cfs_rq_idle in migrate_se_pelt_lag()
4524 * = sched_clock_cpu() - rq_clock()@rq_idle in migrate_se_pelt_lag()
4528 * now = rq_clock_pelt()@rq_idle - cfs->throttled_clock_pelt_time + in migrate_se_pelt_lag()
4529 * sched_clock_cpu() - rq_clock()@rq_idle in migrate_se_pelt_lag()
4531 * rq_clock_pelt()@rq_idle is rq->clock_pelt_idle in migrate_se_pelt_lag()
4532 * rq_clock()@rq_idle is rq->clock_idle in migrate_se_pelt_lag()
4533 * cfs->throttled_clock_pelt_time@cfs_rq_idle in migrate_se_pelt_lag()
4534 * is cfs_rq->throttled_pelt_idle in migrate_se_pelt_lag()
4538 throttled = u64_u32_load(cfs_rq->throttled_pelt_idle); in migrate_se_pelt_lag()
4543 now = u64_u32_load(rq->clock_pelt_idle); in migrate_se_pelt_lag()
4553 now -= throttled; in migrate_se_pelt_lag()
4556 * cfs_rq->avg.last_update_time is more recent than our in migrate_se_pelt_lag()
4561 now += sched_clock_cpu(cpu_of(rq)) - u64_u32_load(rq->clock_idle); in migrate_se_pelt_lag()
4570 * update_cfs_rq_load_avg - update the cfs_rq's load/util averages
4577 * cfs_rq->avg is used for task_h_load() and update_cfs_share() for example.
4581 * Since both these conditions indicate a changed cfs_rq->avg.load we should
4588 struct sched_avg *sa = &cfs_rq->avg; in update_cfs_rq_load_avg()
4591 if (cfs_rq->removed.nr) { in update_cfs_rq_load_avg()
4593 u32 divider = get_pelt_divider(&cfs_rq->avg); in update_cfs_rq_load_avg()
4595 raw_spin_lock(&cfs_rq->removed.lock); in update_cfs_rq_load_avg()
4596 swap(cfs_rq->removed.util_avg, removed_util); in update_cfs_rq_load_avg()
4597 swap(cfs_rq->removed.load_avg, removed_load); in update_cfs_rq_load_avg()
4598 swap(cfs_rq->removed.runnable_avg, removed_runnable); in update_cfs_rq_load_avg()
4599 cfs_rq->removed.nr = 0; in update_cfs_rq_load_avg()
4600 raw_spin_unlock(&cfs_rq->removed.lock); in update_cfs_rq_load_avg()
4603 sub_positive(&sa->load_avg, r); in update_cfs_rq_load_avg()
4604 sub_positive(&sa->load_sum, r * divider); in update_cfs_rq_load_avg()
4605 /* See sa->util_sum below */ in update_cfs_rq_load_avg()
4606 sa->load_sum = max_t(u32, sa->load_sum, sa->load_avg * PELT_MIN_DIVIDER); in update_cfs_rq_load_avg()
4609 sub_positive(&sa->util_avg, r); in update_cfs_rq_load_avg()
4610 sub_positive(&sa->util_sum, r * divider); in update_cfs_rq_load_avg()
4612 * Because of rounding, se->util_sum might ends up being +1 more than in update_cfs_rq_load_avg()
4613 * cfs->util_sum. Although this is not a problem by itself, detaching in update_cfs_rq_load_avg()
4615 * util_avg (~1ms) can make cfs->util_sum becoming null whereas in update_cfs_rq_load_avg()
4622 sa->util_sum = max_t(u32, sa->util_sum, sa->util_avg * PELT_MIN_DIVIDER); in update_cfs_rq_load_avg()
4625 sub_positive(&sa->runnable_avg, r); in update_cfs_rq_load_avg()
4626 sub_positive(&sa->runnable_sum, r * divider); in update_cfs_rq_load_avg()
4627 /* See sa->util_sum above */ in update_cfs_rq_load_avg()
4628 sa->runnable_sum = max_t(u32, sa->runnable_sum, in update_cfs_rq_load_avg()
4629 sa->runnable_avg * PELT_MIN_DIVIDER); in update_cfs_rq_load_avg()
4636 -(long)(removed_runnable * divider) >> SCHED_CAPACITY_SHIFT); in update_cfs_rq_load_avg()
4642 u64_u32_store_copy(sa->last_update_time, in update_cfs_rq_load_avg()
4643 cfs_rq->last_update_time_copy, in update_cfs_rq_load_avg()
4644 sa->last_update_time); in update_cfs_rq_load_avg()
4649 * attach_entity_load_avg - attach this entity to its cfs_rq load avg
4654 * cfs_rq->avg.last_update_time being current.
4659 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in attach_entity_load_avg()
4662 u32 divider = get_pelt_divider(&cfs_rq->avg); in attach_entity_load_avg()
4671 se->avg.last_update_time = cfs_rq->avg.last_update_time; in attach_entity_load_avg()
4672 se->avg.period_contrib = cfs_rq->avg.period_contrib; in attach_entity_load_avg()
4680 se->avg.util_sum = se->avg.util_avg * divider; in attach_entity_load_avg()
4682 se->avg.runnable_sum = se->avg.runnable_avg * divider; in attach_entity_load_avg()
4684 se->avg.load_sum = se->avg.load_avg * divider; in attach_entity_load_avg()
4685 if (se_weight(se) < se->avg.load_sum) in attach_entity_load_avg()
4686 se->avg.load_sum = div_u64(se->avg.load_sum, se_weight(se)); in attach_entity_load_avg()
4688 se->avg.load_sum = 1; in attach_entity_load_avg()
4691 cfs_rq->avg.util_avg += se->avg.util_avg; in attach_entity_load_avg()
4692 cfs_rq->avg.util_sum += se->avg.util_sum; in attach_entity_load_avg()
4693 cfs_rq->avg.runnable_avg += se->avg.runnable_avg; in attach_entity_load_avg()
4694 cfs_rq->avg.runnable_sum += se->avg.runnable_sum; in attach_entity_load_avg()
4696 add_tg_cfs_propagate(cfs_rq, se->avg.load_sum); in attach_entity_load_avg()
4704 * detach_entity_load_avg - detach this entity from its cfs_rq load avg
4709 * cfs_rq->avg.last_update_time being current.
4714 sub_positive(&cfs_rq->avg.util_avg, se->avg.util_avg); in detach_entity_load_avg()
4715 sub_positive(&cfs_rq->avg.util_sum, se->avg.util_sum); in detach_entity_load_avg()
4717 cfs_rq->avg.util_sum = max_t(u32, cfs_rq->avg.util_sum, in detach_entity_load_avg()
4718 cfs_rq->avg.util_avg * PELT_MIN_DIVIDER); in detach_entity_load_avg()
4720 sub_positive(&cfs_rq->avg.runnable_avg, se->avg.runnable_avg); in detach_entity_load_avg()
4721 sub_positive(&cfs_rq->avg.runnable_sum, se->avg.runnable_sum); in detach_entity_load_avg()
4723 cfs_rq->avg.runnable_sum = max_t(u32, cfs_rq->avg.runnable_sum, in detach_entity_load_avg()
4724 cfs_rq->avg.runnable_avg * PELT_MIN_DIVIDER); in detach_entity_load_avg()
4726 add_tg_cfs_propagate(cfs_rq, -se->avg.load_sum); in detach_entity_load_avg()
4748 * Track task load average for carrying it to new CPU after migrated, and in update_load_avg()
4751 if (se->avg.last_update_time && !(flags & SKIP_AGE_LOAD)) in update_load_avg()
4757 if (!se->avg.last_update_time && (flags & DO_ATTACH)) { in update_load_avg()
4764 * IOW we're enqueueing a task on a new CPU. in update_load_avg()
4772 * and we are migrating task out of the CPU. in update_load_avg()
4807 * tasks cannot exit without having gone through wake_up_new_task() -> in remove_entity_load_avg()
4814 raw_spin_lock_irqsave(&cfs_rq->removed.lock, flags); in remove_entity_load_avg()
4815 ++cfs_rq->removed.nr; in remove_entity_load_avg()
4816 cfs_rq->removed.util_avg += se->avg.util_avg; in remove_entity_load_avg()
4817 cfs_rq->removed.load_avg += se->avg.load_avg; in remove_entity_load_avg()
4818 cfs_rq->removed.runnable_avg += se->avg.runnable_avg; in remove_entity_load_avg()
4819 raw_spin_unlock_irqrestore(&cfs_rq->removed.lock, flags); in remove_entity_load_avg()
4824 return cfs_rq->avg.runnable_avg; in cfs_rq_runnable_avg()
4829 return cfs_rq->avg.load_avg; in cfs_rq_load_avg()
4836 return READ_ONCE(p->se.avg.util_avg); in task_util()
4841 struct util_est ue = READ_ONCE(p->se.avg.util_est); in _task_util_est()
4860 enqueued = cfs_rq->avg.util_est.enqueued; in util_est_enqueue()
4862 WRITE_ONCE(cfs_rq->avg.util_est.enqueued, enqueued); in util_est_enqueue()
4876 enqueued = cfs_rq->avg.util_est.enqueued; in util_est_dequeue()
4877 enqueued -= min_t(unsigned int, enqueued, _task_util_est(p)); in util_est_dequeue()
4878 WRITE_ONCE(cfs_rq->avg.util_est.enqueued, enqueued); in util_est_dequeue()
4889 * abs(x) < y := (unsigned)(x + y - 1) < (2 * y - 1)
4895 return ((unsigned int)(value + margin - 1) < (2 * margin - 1)); in within_margin()
4919 ue = p->se.avg.util_est; in util_est_update()
4941 last_ewma_diff = ue.enqueued - ue.ewma; in util_est_update()
4942 last_enqueued_diff -= ue.enqueued; in util_est_update()
4952 * we cannot grant there is idle time in this CPU. in util_est_update()
4965 * ewma(t) = w * task_util(p) + (1-w) * ewma(t-1) in util_est_update()
4966 * = w * task_util(p) + ewma(t-1) - w * ewma(t-1) in util_est_update()
4967 * = w * (task_util(p) - ewma(t-1)) + ewma(t-1) in util_est_update()
4968 * = w * ( last_ewma_diff ) + ewma(t-1) in util_est_update()
4969 * = w * (last_ewma_diff + ewma(t-1) / w) in util_est_update()
4979 WRITE_ONCE(p->se.avg.util_est, ue); in util_est_update()
4981 trace_sched_util_est_se_tp(&p->se); in util_est_update()
4987 int cpu) in util_fits_cpu() argument
4990 unsigned long capacity = capacity_of(cpu); in util_fits_cpu() local
4996 fits = fits_capacity(util, capacity); in util_fits_cpu()
5003 * uclamp_max. We only care about capacity pressure (by using in util_fits_cpu()
5007 * pressure to skew the check whether it fits a CPU or not. in util_fits_cpu()
5010 * should fit a little cpu even if there's some pressure. in util_fits_cpu()
5021 capacity_orig = capacity_orig_of(cpu); in util_fits_cpu()
5022 capacity_orig_thermal = capacity_orig - arch_scale_thermal_pressure(cpu); in util_fits_cpu()
5025 * We want to force a task to fit a cpu as implied by uclamp_max. in util_fits_cpu()
5038 * +---------------------------------------- in util_fits_cpu()
5053 * Only exception is when we are on max capacity, then we need to be in util_fits_cpu()
5059 * max capacity, it doesn't make sense to block overutilized. in util_fits_cpu()
5076 * +---------------------------------------- in util_fits_cpu()
5081 * capacity without taking margin/pressure into account. in util_fits_cpu()
5089 * need to take into account the boosted value fits the CPU without in util_fits_cpu()
5098 return -1; in util_fits_cpu()
5103 static inline int task_fits_cpu(struct task_struct *p, int cpu) in task_fits_cpu() argument
5109 * Return true only if the cpu fully fits the task requirements, which in task_fits_cpu()
5112 return (util_fits_cpu(util, uclamp_min, uclamp_max, cpu) > 0); in task_fits_cpu()
5120 if (!p || p->nr_cpus_allowed == 1) { in update_misfit_status()
5121 rq->misfit_task_load = 0; in update_misfit_status()
5126 rq->misfit_task_load = 0; in update_misfit_status()
5134 rq->misfit_task_load = max_t(unsigned long, task_h_load(p), 1); in update_misfit_status()
5141 return !cfs_rq->nr_running; in cfs_rq_is_decayed()
5185 se->slice = sysctl_sched_base_slice; in place_entity()
5186 vslice = calc_delta_fair(se->slice, se); in place_entity()
5196 if (sched_feat(PLACE_LAG) && cfs_rq->nr_running) { in place_entity()
5197 struct sched_entity *curr = cfs_rq->curr; in place_entity()
5200 lag = se->vlag; in place_entity()
5210 * lag_i = S - s_i = w_i * (V - v_i) in place_entity()
5215 * vl_i = V - v_i <=> v_i = V - vl_i in place_entity()
5227 * = (W*V + w_i*(V - vl_i)) / (W + w_i) in place_entity()
5228 * = (W*V + w_i*V - w_i*vl_i) / (W + w_i) in place_entity()
5229 * = (V*(W + w_i) - w_i*l) / (W + w_i) in place_entity()
5230 * = V - w_i*vl_i / (W + w_i) in place_entity()
5234 * vl'_i = V' - v_i in place_entity()
5235 * = V - w_i*vl_i / (W + w_i) - (V - vl_i) in place_entity()
5236 * = vl_i - w_i*vl_i / (W + w_i) in place_entity()
5246 * vl'_i = vl_i - w_i*vl_i / (W + w_i) in place_entity()
5247 * = ((W + w_i)*vl_i - w_i*vl_i) / (W + w_i) in place_entity()
5249 * (W + w_i)*vl'_i = (W + w_i)*vl_i - w_i*vl_i in place_entity()
5254 load = cfs_rq->avg_load; in place_entity()
5255 if (curr && curr->on_rq) in place_entity()
5256 load += scale_load_down(curr->load.weight); in place_entity()
5258 lag *= load + scale_load_down(se->load.weight); in place_entity()
5264 se->vruntime = vruntime - lag; in place_entity()
5277 se->deadline = se->vruntime + vslice; in place_entity()
5288 bool curr = cfs_rq->curr == se; in enqueue_entity()
5301 * - Update loads to have both entity and cfs_rq synced with now. in enqueue_entity()
5302 * - For group_entity, update its runnable_weight to reflect the new in enqueue_entity()
5304 * - For group_entity, update its weight to reflect the new share of in enqueue_entity()
5306 * - Add its new weight to cfs_rq->load.weight in enqueue_entity()
5312 * but update_cfs_group() here will re-adjust the weight and have to in enqueue_entity()
5318 * XXX now that the entity has been re-weighted, and it's lag adjusted, in enqueue_entity()
5328 se->exec_start = 0; in enqueue_entity()
5334 se->on_rq = 1; in enqueue_entity()
5336 if (cfs_rq->nr_running == 1) { in enqueue_entity()
5344 if (cfs_rq_throttled(cfs_rq) && !cfs_rq->throttled_clock) in enqueue_entity()
5345 cfs_rq->throttled_clock = rq_clock(rq); in enqueue_entity()
5346 if (!cfs_rq->throttled_clock_self) in enqueue_entity()
5347 cfs_rq->throttled_clock_self = rq_clock(rq); in enqueue_entity()
5357 if (cfs_rq->next != se) in __clear_buddies_next()
5360 cfs_rq->next = NULL; in __clear_buddies_next()
5366 if (cfs_rq->next == se) in clear_buddies()
5381 * Update run-time statistics of the 'current'. in dequeue_entity()
5387 * - Update loads to have both entity and cfs_rq synced with now. in dequeue_entity()
5388 * - For group_entity, update its runnable_weight to reflect the new in dequeue_entity()
5390 * - Subtract its previous weight from cfs_rq->load.weight. in dequeue_entity()
5391 * - For group entity, update its weight to reflect the new share in dequeue_entity()
5402 if (se != cfs_rq->curr) in dequeue_entity()
5404 se->on_rq = 0; in dequeue_entity()
5416 * further than we started -- ie. we'll be penalized. in dequeue_entity()
5421 if (cfs_rq->nr_running == 0) in dequeue_entity()
5431 if (se->on_rq) { in set_next_entity()
5434 * a CPU. So account for the time it spent waiting on the in set_next_entity()
5444 se->vlag = se->deadline; in set_next_entity()
5448 cfs_rq->curr = se; in set_next_entity()
5451 * Track our maximum slice length, if the CPU's load is at in set_next_entity()
5453 * when there are only lesser-weight tasks around): in set_next_entity()
5456 rq_of(cfs_rq)->cfs.load.weight >= 2*se->load.weight) { in set_next_entity()
5460 __schedstat_set(stats->slice_max, in set_next_entity()
5461 max((u64)stats->slice_max, in set_next_entity()
5462 se->sum_exec_runtime - se->prev_sum_exec_runtime)); in set_next_entity()
5465 se->prev_sum_exec_runtime = se->sum_exec_runtime; in set_next_entity()
5482 cfs_rq->next && entity_eligible(cfs_rq, cfs_rq->next)) in pick_next_entity()
5483 return cfs_rq->next; in pick_next_entity()
5496 if (prev->on_rq) in put_prev_entity()
5502 if (prev->on_rq) { in put_prev_entity()
5509 cfs_rq->curr = NULL; in put_prev_entity()
5516 * Update run-time statistics of the 'current'. in entity_tick()
5539 hrtimer_active(&rq_of(cfs_rq)->hrtick_timer)) in entity_tick()
5594 * directly instead of rq->clock to avoid adding additional synchronization
5595 * around rq->lock.
5597 * requires cfs_b->lock
5603 if (unlikely(cfs_b->quota == RUNTIME_INF)) in __refill_cfs_bandwidth_runtime()
5606 cfs_b->runtime += cfs_b->quota; in __refill_cfs_bandwidth_runtime()
5607 runtime = cfs_b->runtime_snap - cfs_b->runtime; in __refill_cfs_bandwidth_runtime()
5609 cfs_b->burst_time += runtime; in __refill_cfs_bandwidth_runtime()
5610 cfs_b->nr_burst++; in __refill_cfs_bandwidth_runtime()
5613 cfs_b->runtime = min(cfs_b->runtime, cfs_b->quota + cfs_b->burst); in __refill_cfs_bandwidth_runtime()
5614 cfs_b->runtime_snap = cfs_b->runtime; in __refill_cfs_bandwidth_runtime()
5619 return &tg->cfs_bandwidth; in tg_cfs_bandwidth()
5628 lockdep_assert_held(&cfs_b->lock); in __assign_cfs_rq_runtime()
5631 min_amount = target_runtime - cfs_rq->runtime_remaining; in __assign_cfs_rq_runtime()
5633 if (cfs_b->quota == RUNTIME_INF) in __assign_cfs_rq_runtime()
5638 if (cfs_b->runtime > 0) { in __assign_cfs_rq_runtime()
5639 amount = min(cfs_b->runtime, min_amount); in __assign_cfs_rq_runtime()
5640 cfs_b->runtime -= amount; in __assign_cfs_rq_runtime()
5641 cfs_b->idle = 0; in __assign_cfs_rq_runtime()
5645 cfs_rq->runtime_remaining += amount; in __assign_cfs_rq_runtime()
5647 return cfs_rq->runtime_remaining > 0; in __assign_cfs_rq_runtime()
5653 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in assign_cfs_rq_runtime()
5656 raw_spin_lock(&cfs_b->lock); in assign_cfs_rq_runtime()
5658 raw_spin_unlock(&cfs_b->lock); in assign_cfs_rq_runtime()
5666 cfs_rq->runtime_remaining -= delta_exec; in __account_cfs_rq_runtime()
5668 if (likely(cfs_rq->runtime_remaining > 0)) in __account_cfs_rq_runtime()
5671 if (cfs_rq->throttled) in __account_cfs_rq_runtime()
5677 if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr)) in __account_cfs_rq_runtime()
5684 if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled) in account_cfs_rq_runtime()
5692 return cfs_bandwidth_used() && cfs_rq->throttled; in cfs_rq_throttled()
5698 return cfs_bandwidth_used() && cfs_rq->throttle_count; in throttled_hierarchy()
5704 * load-balance operations.
5711 src_cfs_rq = tg->cfs_rq[src_cpu]; in throttled_lb_pair()
5712 dest_cfs_rq = tg->cfs_rq[dest_cpu]; in throttled_lb_pair()
5721 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in tg_unthrottle_up()
5723 cfs_rq->throttle_count--; in tg_unthrottle_up()
5724 if (!cfs_rq->throttle_count) { in tg_unthrottle_up()
5725 cfs_rq->throttled_clock_pelt_time += rq_clock_pelt(rq) - in tg_unthrottle_up()
5726 cfs_rq->throttled_clock_pelt; in tg_unthrottle_up()
5732 if (cfs_rq->throttled_clock_self) { in tg_unthrottle_up()
5733 u64 delta = rq_clock(rq) - cfs_rq->throttled_clock_self; in tg_unthrottle_up()
5735 cfs_rq->throttled_clock_self = 0; in tg_unthrottle_up()
5740 cfs_rq->throttled_clock_self_time += delta; in tg_unthrottle_up()
5750 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in tg_throttle_down()
5753 if (!cfs_rq->throttle_count) { in tg_throttle_down()
5754 cfs_rq->throttled_clock_pelt = rq_clock_pelt(rq); in tg_throttle_down()
5757 SCHED_WARN_ON(cfs_rq->throttled_clock_self); in tg_throttle_down()
5758 if (cfs_rq->nr_running) in tg_throttle_down()
5759 cfs_rq->throttled_clock_self = rq_clock(rq); in tg_throttle_down()
5761 cfs_rq->throttle_count++; in tg_throttle_down()
5769 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in throttle_cfs_rq()
5773 raw_spin_lock(&cfs_b->lock); in throttle_cfs_rq()
5786 list_add_tail_rcu(&cfs_rq->throttled_list, in throttle_cfs_rq()
5787 &cfs_b->throttled_cfs_rq); in throttle_cfs_rq()
5789 raw_spin_unlock(&cfs_b->lock); in throttle_cfs_rq()
5794 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))]; in throttle_cfs_rq()
5798 walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq); in throttle_cfs_rq()
5801 task_delta = cfs_rq->h_nr_running; in throttle_cfs_rq()
5802 idle_task_delta = cfs_rq->idle_h_nr_running; in throttle_cfs_rq()
5805 /* throttled entity or throttle-on-deactivate */ in throttle_cfs_rq()
5806 if (!se->on_rq) in throttle_cfs_rq()
5812 idle_task_delta = cfs_rq->h_nr_running; in throttle_cfs_rq()
5814 qcfs_rq->h_nr_running -= task_delta; in throttle_cfs_rq()
5815 qcfs_rq->idle_h_nr_running -= idle_task_delta; in throttle_cfs_rq()
5817 if (qcfs_rq->load.weight) { in throttle_cfs_rq()
5818 /* Avoid re-evaluating load for this entity: */ in throttle_cfs_rq()
5826 /* throttled entity or throttle-on-deactivate */ in throttle_cfs_rq()
5827 if (!se->on_rq) in throttle_cfs_rq()
5834 idle_task_delta = cfs_rq->h_nr_running; in throttle_cfs_rq()
5836 qcfs_rq->h_nr_running -= task_delta; in throttle_cfs_rq()
5837 qcfs_rq->idle_h_nr_running -= idle_task_delta; in throttle_cfs_rq()
5846 * throttled-list. rq->lock protects completion. in throttle_cfs_rq()
5848 cfs_rq->throttled = 1; in throttle_cfs_rq()
5849 SCHED_WARN_ON(cfs_rq->throttled_clock); in throttle_cfs_rq()
5850 if (cfs_rq->nr_running) in throttle_cfs_rq()
5851 cfs_rq->throttled_clock = rq_clock(rq); in throttle_cfs_rq()
5858 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in unthrottle_cfs_rq()
5862 se = cfs_rq->tg->se[cpu_of(rq)]; in unthrottle_cfs_rq()
5864 cfs_rq->throttled = 0; in unthrottle_cfs_rq()
5868 raw_spin_lock(&cfs_b->lock); in unthrottle_cfs_rq()
5869 if (cfs_rq->throttled_clock) { in unthrottle_cfs_rq()
5870 cfs_b->throttled_time += rq_clock(rq) - cfs_rq->throttled_clock; in unthrottle_cfs_rq()
5871 cfs_rq->throttled_clock = 0; in unthrottle_cfs_rq()
5873 list_del_rcu(&cfs_rq->throttled_list); in unthrottle_cfs_rq()
5874 raw_spin_unlock(&cfs_b->lock); in unthrottle_cfs_rq()
5877 walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq); in unthrottle_cfs_rq()
5879 if (!cfs_rq->load.weight) { in unthrottle_cfs_rq()
5880 if (!cfs_rq->on_list) in unthrottle_cfs_rq()
5893 task_delta = cfs_rq->h_nr_running; in unthrottle_cfs_rq()
5894 idle_task_delta = cfs_rq->idle_h_nr_running; in unthrottle_cfs_rq()
5898 if (se->on_rq) in unthrottle_cfs_rq()
5903 idle_task_delta = cfs_rq->h_nr_running; in unthrottle_cfs_rq()
5905 qcfs_rq->h_nr_running += task_delta; in unthrottle_cfs_rq()
5906 qcfs_rq->idle_h_nr_running += idle_task_delta; in unthrottle_cfs_rq()
5920 idle_task_delta = cfs_rq->h_nr_running; in unthrottle_cfs_rq()
5922 qcfs_rq->h_nr_running += task_delta; in unthrottle_cfs_rq()
5923 qcfs_rq->idle_h_nr_running += idle_task_delta; in unthrottle_cfs_rq()
5936 /* Determine whether we need to wake up potentially idle CPU: */ in unthrottle_cfs_rq()
5937 if (rq->curr == rq->idle && rq->cfs.nr_running) in unthrottle_cfs_rq()
5967 list_for_each_entry_safe(cursor, tmp, &rq->cfsb_csd_list, in __cfsb_csd_unthrottle()
5969 list_del_init(&cursor->throttled_csd_list); in __cfsb_csd_unthrottle()
5992 if (SCHED_WARN_ON(!list_empty(&cfs_rq->throttled_csd_list))) in __unthrottle_cfs_rq_async()
5995 first = list_empty(&rq->cfsb_csd_list); in __unthrottle_cfs_rq_async()
5996 list_add_tail(&cfs_rq->throttled_csd_list, &rq->cfsb_csd_list); in __unthrottle_cfs_rq_async()
5998 smp_call_function_single_async(cpu_of(rq), &rq->cfsb_csd); in __unthrottle_cfs_rq_async()
6012 cfs_rq->runtime_remaining <= 0)) in unthrottle_cfs_rq_async()
6029 list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq, in distribute_cfs_runtime()
6044 if (!list_empty(&cfs_rq->throttled_csd_list)) in distribute_cfs_runtime()
6049 SCHED_WARN_ON(cfs_rq->runtime_remaining > 0); in distribute_cfs_runtime()
6051 raw_spin_lock(&cfs_b->lock); in distribute_cfs_runtime()
6052 runtime = -cfs_rq->runtime_remaining + 1; in distribute_cfs_runtime()
6053 if (runtime > cfs_b->runtime) in distribute_cfs_runtime()
6054 runtime = cfs_b->runtime; in distribute_cfs_runtime()
6055 cfs_b->runtime -= runtime; in distribute_cfs_runtime()
6056 remaining = cfs_b->runtime; in distribute_cfs_runtime()
6057 raw_spin_unlock(&cfs_b->lock); in distribute_cfs_runtime()
6059 cfs_rq->runtime_remaining += runtime; in distribute_cfs_runtime()
6062 if (cfs_rq->runtime_remaining > 0) { in distribute_cfs_runtime()
6091 * period the timer is deactivated until scheduling resumes; cfs_b->idle is
6099 if (cfs_b->quota == RUNTIME_INF) in do_sched_cfs_period_timer()
6102 throttled = !list_empty(&cfs_b->throttled_cfs_rq); in do_sched_cfs_period_timer()
6103 cfs_b->nr_periods += overrun; in do_sched_cfs_period_timer()
6105 /* Refill extra burst quota even if cfs_b->idle */ in do_sched_cfs_period_timer()
6112 if (cfs_b->idle && !throttled) in do_sched_cfs_period_timer()
6117 cfs_b->idle = 1; in do_sched_cfs_period_timer()
6122 cfs_b->nr_throttled += overrun; in do_sched_cfs_period_timer()
6125 * This check is repeated as we release cfs_b->lock while we unthrottle. in do_sched_cfs_period_timer()
6127 while (throttled && cfs_b->runtime > 0) { in do_sched_cfs_period_timer()
6128 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in do_sched_cfs_period_timer()
6129 /* we can't nest cfs_b->lock while distributing bandwidth */ in do_sched_cfs_period_timer()
6131 raw_spin_lock_irqsave(&cfs_b->lock, flags); in do_sched_cfs_period_timer()
6140 cfs_b->idle = 0; in do_sched_cfs_period_timer()
6158 * Requires cfs_b->lock for hrtimer_expires_remaining to be safe against the
6164 struct hrtimer *refresh_timer = &cfs_b->period_timer; in runtime_refresh_within()
6167 /* if the call-back is running a quota refresh is already occurring */ in runtime_refresh_within()
6188 if (cfs_b->slack_started) in start_cfs_slack_bandwidth()
6190 cfs_b->slack_started = true; in start_cfs_slack_bandwidth()
6192 hrtimer_start(&cfs_b->slack_timer, in start_cfs_slack_bandwidth()
6200 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in __return_cfs_rq_runtime()
6201 s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime; in __return_cfs_rq_runtime()
6206 raw_spin_lock(&cfs_b->lock); in __return_cfs_rq_runtime()
6207 if (cfs_b->quota != RUNTIME_INF) { in __return_cfs_rq_runtime()
6208 cfs_b->runtime += slack_runtime; in __return_cfs_rq_runtime()
6210 /* we are under rq->lock, defer unthrottling using a timer */ in __return_cfs_rq_runtime()
6211 if (cfs_b->runtime > sched_cfs_bandwidth_slice() && in __return_cfs_rq_runtime()
6212 !list_empty(&cfs_b->throttled_cfs_rq)) in __return_cfs_rq_runtime()
6215 raw_spin_unlock(&cfs_b->lock); in __return_cfs_rq_runtime()
6218 cfs_rq->runtime_remaining -= slack_runtime; in __return_cfs_rq_runtime()
6226 if (!cfs_rq->runtime_enabled || cfs_rq->nr_running) in return_cfs_rq_runtime()
6234 * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
6242 raw_spin_lock_irqsave(&cfs_b->lock, flags); in do_sched_cfs_slack_timer()
6243 cfs_b->slack_started = false; in do_sched_cfs_slack_timer()
6246 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in do_sched_cfs_slack_timer()
6250 if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) in do_sched_cfs_slack_timer()
6251 runtime = cfs_b->runtime; in do_sched_cfs_slack_timer()
6253 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in do_sched_cfs_slack_timer()
6264 * runtime as update_curr() throttling can not trigger until it's on-rq.
6271 /* an active group must be handled by the update_curr()->put() path */ in check_enqueue_throttle()
6272 if (!cfs_rq->runtime_enabled || cfs_rq->curr) in check_enqueue_throttle()
6281 if (cfs_rq->runtime_remaining <= 0) in check_enqueue_throttle()
6285 static void sync_throttle(struct task_group *tg, int cpu) in sync_throttle() argument
6292 if (!tg->parent) in sync_throttle()
6295 cfs_rq = tg->cfs_rq[cpu]; in sync_throttle()
6296 pcfs_rq = tg->parent->cfs_rq[cpu]; in sync_throttle()
6298 cfs_rq->throttle_count = pcfs_rq->throttle_count; in sync_throttle()
6299 cfs_rq->throttled_clock_pelt = rq_clock_pelt(cpu_rq(cpu)); in sync_throttle()
6308 if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0)) in check_cfs_rq_runtime()
6342 raw_spin_lock_irqsave(&cfs_b->lock, flags); in sched_cfs_period_timer()
6344 overrun = hrtimer_forward_now(timer, cfs_b->period); in sched_cfs_period_timer()
6351 u64 new, old = ktime_to_ns(cfs_b->period); in sched_cfs_period_timer()
6360 cfs_b->period = ns_to_ktime(new); in sched_cfs_period_timer()
6361 cfs_b->quota *= 2; in sched_cfs_period_timer()
6362 cfs_b->burst *= 2; in sched_cfs_period_timer()
6365 …"cfs_period_timer[cpu%d]: period too short, scaling up (new cfs_period_us = %lld, cfs_quota_us = %… in sched_cfs_period_timer()
6368 div_u64(cfs_b->quota, NSEC_PER_USEC)); in sched_cfs_period_timer()
6371 …"cfs_period_timer[cpu%d]: period too short, but cannot scale up without losing precision (cfs_peri… in sched_cfs_period_timer()
6374 div_u64(cfs_b->quota, NSEC_PER_USEC)); in sched_cfs_period_timer()
6382 cfs_b->period_active = 0; in sched_cfs_period_timer()
6383 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in sched_cfs_period_timer()
6390 raw_spin_lock_init(&cfs_b->lock); in init_cfs_bandwidth()
6391 cfs_b->runtime = 0; in init_cfs_bandwidth()
6392 cfs_b->quota = RUNTIME_INF; in init_cfs_bandwidth()
6393 cfs_b->period = ns_to_ktime(default_cfs_period()); in init_cfs_bandwidth()
6394 cfs_b->burst = 0; in init_cfs_bandwidth()
6395 cfs_b->hierarchical_quota = parent ? parent->hierarchical_quota : RUNTIME_INF; in init_cfs_bandwidth()
6397 INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq); in init_cfs_bandwidth()
6398 hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED); in init_cfs_bandwidth()
6399 cfs_b->period_timer.function = sched_cfs_period_timer; in init_cfs_bandwidth()
6402 hrtimer_set_expires(&cfs_b->period_timer, in init_cfs_bandwidth()
6403 get_random_u32_below(cfs_b->period)); in init_cfs_bandwidth()
6404 hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); in init_cfs_bandwidth()
6405 cfs_b->slack_timer.function = sched_cfs_slack_timer; in init_cfs_bandwidth()
6406 cfs_b->slack_started = false; in init_cfs_bandwidth()
6411 cfs_rq->runtime_enabled = 0; in init_cfs_rq_runtime()
6412 INIT_LIST_HEAD(&cfs_rq->throttled_list); in init_cfs_rq_runtime()
6414 INIT_LIST_HEAD(&cfs_rq->throttled_csd_list); in init_cfs_rq_runtime()
6420 lockdep_assert_held(&cfs_b->lock); in start_cfs_bandwidth()
6422 if (cfs_b->period_active) in start_cfs_bandwidth()
6425 cfs_b->period_active = 1; in start_cfs_bandwidth()
6426 hrtimer_forward_now(&cfs_b->period_timer, cfs_b->period); in start_cfs_bandwidth()
6427 hrtimer_start_expires(&cfs_b->period_timer, HRTIMER_MODE_ABS_PINNED); in start_cfs_bandwidth()
6435 if (!cfs_b->throttled_cfs_rq.next) in destroy_cfs_bandwidth()
6438 hrtimer_cancel(&cfs_b->period_timer); in destroy_cfs_bandwidth()
6439 hrtimer_cancel(&cfs_b->slack_timer); in destroy_cfs_bandwidth()
6446 * CSD item but the remote cpu has not yet processed it. To handle this, in destroy_cfs_bandwidth()
6456 if (list_empty(&rq->cfsb_csd_list)) in destroy_cfs_bandwidth()
6467 * Both these CPU hotplug callbacks race against unregister_fair_sched_group()
6473 /* cpu online callback */
6482 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth; in update_runtime_enabled()
6483 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in update_runtime_enabled()
6485 raw_spin_lock(&cfs_b->lock); in update_runtime_enabled()
6486 cfs_rq->runtime_enabled = cfs_b->quota != RUNTIME_INF; in update_runtime_enabled()
6487 raw_spin_unlock(&cfs_b->lock); in update_runtime_enabled()
6492 /* cpu offline callback */
6508 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in unthrottle_offline_cfs_rqs()
6510 if (!cfs_rq->runtime_enabled) in unthrottle_offline_cfs_rqs()
6517 cfs_rq->runtime_remaining = 1; in unthrottle_offline_cfs_rqs()
6519 * Offline rq is schedulable till CPU is completely disabled in unthrottle_offline_cfs_rqs()
6522 cfs_rq->runtime_enabled = 0; in unthrottle_offline_cfs_rqs()
6539 if (cfs_rq->runtime_enabled || in cfs_task_bw_constrained()
6540 tg_cfs_bandwidth(cfs_rq->tg)->hierarchical_quota != RUNTIME_INF) in cfs_task_bw_constrained()
6550 int cpu = cpu_of(rq); in sched_fair_update_stop_tick() local
6555 if (!tick_nohz_full_cpu(cpu)) in sched_fair_update_stop_tick()
6558 if (rq->nr_running != 1) in sched_fair_update_stop_tick()
6568 tick_nohz_dep_set_cpu(cpu, TICK_DEP_BIT_SCHED); in sched_fair_update_stop_tick()
6582 static inline void sync_throttle(struct task_group *tg, int cpu) {} in sync_throttle() argument
6632 struct sched_entity *se = &p->se; in hrtick_start_fair()
6636 if (rq->cfs.h_nr_running > 1) { in hrtick_start_fair()
6637 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime; in hrtick_start_fair()
6638 u64 slice = se->slice; in hrtick_start_fair()
6639 s64 delta = slice - ran; in hrtick_start_fair()
6657 struct task_struct *curr = rq->curr; in hrtick_update()
6659 if (!hrtick_enabled_fair(rq) || curr->sched_class != &fair_sched_class) in hrtick_update()
6676 static inline bool cpu_overutilized(int cpu) in cpu_overutilized() argument
6683 rq_util_min = uclamp_rq_get(cpu_rq(cpu), UCLAMP_MIN); in cpu_overutilized()
6684 rq_util_max = uclamp_rq_get(cpu_rq(cpu), UCLAMP_MAX); in cpu_overutilized()
6686 /* Return true only if the utilization doesn't fit CPU's capacity */ in cpu_overutilized()
6687 return !util_fits_cpu(cpu_util_cfs(cpu), rq_util_min, rq_util_max, cpu); in cpu_overutilized()
6696 WRITE_ONCE(rd->overutilized, status); in set_rd_overutilized_status()
6709 if (!READ_ONCE(rq->rd->overutilized) && cpu_overutilized(rq->cpu)) in check_update_overutilized_status()
6710 set_rd_overutilized_status(rq->rd, SG_OVERUTILIZED); in check_update_overutilized_status()
6719 return unlikely(rq->nr_running == rq->cfs.idle_h_nr_running && in sched_idle_rq()
6720 rq->nr_running); in sched_idle_rq()
6724 static int sched_idle_cpu(int cpu) in sched_idle_cpu() argument
6726 return sched_idle_rq(cpu_rq(cpu)); in sched_idle_cpu()
6739 struct sched_entity *se = &p->se; in enqueue_task_fair()
6749 util_est_enqueue(&rq->cfs, p); in enqueue_task_fair()
6756 if (p->in_iowait) in enqueue_task_fair()
6760 if (se->on_rq) in enqueue_task_fair()
6765 cfs_rq->h_nr_running++; in enqueue_task_fair()
6766 cfs_rq->idle_h_nr_running += idle_h_nr_running; in enqueue_task_fair()
6785 cfs_rq->h_nr_running++; in enqueue_task_fair()
6786 cfs_rq->idle_h_nr_running += idle_h_nr_running; in enqueue_task_fair()
6801 * half of the spare capacity of their CPU, tiny tasks have the in enqueue_task_fair()
6832 struct sched_entity *se = &p->se; in dequeue_task_fair()
6837 util_est_dequeue(&rq->cfs, p); in dequeue_task_fair()
6843 cfs_rq->h_nr_running--; in dequeue_task_fair()
6844 cfs_rq->idle_h_nr_running -= idle_h_nr_running; in dequeue_task_fair()
6854 if (cfs_rq->load.weight) { in dequeue_task_fair()
6855 /* Avoid re-evaluating load for this entity: */ in dequeue_task_fair()
6875 cfs_rq->h_nr_running--; in dequeue_task_fair()
6876 cfs_rq->idle_h_nr_running -= idle_h_nr_running; in dequeue_task_fair()
6892 rq->next_balance = jiffies; in dequeue_task_fair()
6895 util_est_update(&rq->cfs, p, task_sleep); in dequeue_task_fair()
6921 return cfs_rq_load_avg(&rq->cfs); in cpu_load()
6925 * cpu_load_without - compute CPU load without any contributions from *p
6926 * @cpu: the CPU which load is requested
6929 * The load of a CPU is defined by the load of tasks currently enqueued on that
6930 * CPU as well as tasks which are currently sleeping after an execution on that
6931 * CPU.
6933 * This method returns the load of the specified CPU by discounting the load of
6934 * the specified task, whenever the task is currently contributing to the CPU
6943 if (cpu_of(rq) != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in cpu_load_without()
6946 cfs_rq = &rq->cfs; in cpu_load_without()
6947 load = READ_ONCE(cfs_rq->avg.load_avg); in cpu_load_without()
6949 /* Discount task's util from CPU's util */ in cpu_load_without()
6957 return cfs_rq_runnable_avg(&rq->cfs); in cpu_runnable()
6966 if (cpu_of(rq) != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in cpu_runnable_without()
6969 cfs_rq = &rq->cfs; in cpu_runnable_without()
6970 runnable = READ_ONCE(cfs_rq->avg.runnable_avg); in cpu_runnable_without()
6972 /* Discount task's runnable from CPU's runnable */ in cpu_runnable_without()
6973 lsub_positive(&runnable, p->se.avg.runnable_avg); in cpu_runnable_without()
6978 static unsigned long capacity_of(int cpu) in capacity_of() argument
6980 return cpu_rq(cpu)->cpu_capacity; in capacity_of()
6989 if (time_after(jiffies, current->wakee_flip_decay_ts + HZ)) { in record_wakee()
6990 current->wakee_flips >>= 1; in record_wakee()
6991 current->wakee_flip_decay_ts = jiffies; in record_wakee()
6994 if (current->last_wakee != p) { in record_wakee()
6995 current->last_wakee = p; in record_wakee()
6996 current->wakee_flips++; in record_wakee()
7001 * Detect M:N waker/wakee relationships via a switching-frequency heuristic.
7011 * non-monogamous, with partner count exceeding socket size.
7019 unsigned int master = current->wakee_flips; in wake_wide()
7020 unsigned int slave = p->wakee_flips; in wake_wide()
7031 * The purpose of wake_affine() is to quickly determine on which CPU we can run
7033 * CPU.
7035 * wake_affine_idle() - only considers 'now', it check if the waking CPU is
7036 * cache-affine and is (or will be) idle.
7038 * wake_affine_weight() - considers the weight to reflect the average
7055 * on one CPU. in wake_affine_idle()
7060 if (sync && cpu_rq(this_cpu)->nr_running == 1) in wake_affine_idle()
7084 this_eff_load -= current_load; in wake_affine_weight()
7095 prev_eff_load -= task_load; in wake_affine_weight()
7097 prev_eff_load *= 100 + (sd->imbalance_pct - 100) / 2; in wake_affine_weight()
7103 * stacking the wakee on top of the waker if no other CPU is in wake_affine_weight()
7123 schedstat_inc(p->stats.nr_wakeups_affine_attempts); in wake_affine()
7127 schedstat_inc(sd->ttwu_move_affine); in wake_affine()
7128 schedstat_inc(p->stats.nr_wakeups_affine); in wake_affine()
7136 * find_idlest_group_cpu - find the idlest CPU among the CPUs in the group.
7145 int shallowest_idle_cpu = -1; in find_idlest_group_cpu()
7149 if (group->group_weight == 1) in find_idlest_group_cpu()
7153 for_each_cpu_and(i, sched_group_span(group), p->cpus_ptr) { in find_idlest_group_cpu()
7164 if (idle && idle->exit_latency < min_exit_latency) { in find_idlest_group_cpu()
7166 * We give priority to a CPU whose idle state in find_idlest_group_cpu()
7170 min_exit_latency = idle->exit_latency; in find_idlest_group_cpu()
7171 latest_idle_timestamp = rq->idle_stamp; in find_idlest_group_cpu()
7173 } else if ((!idle || idle->exit_latency == min_exit_latency) && in find_idlest_group_cpu()
7174 rq->idle_stamp > latest_idle_timestamp) { in find_idlest_group_cpu()
7177 * the most recently idled CPU might have in find_idlest_group_cpu()
7180 latest_idle_timestamp = rq->idle_stamp; in find_idlest_group_cpu()
7183 } else if (shallowest_idle_cpu == -1) { in find_idlest_group_cpu()
7192 return shallowest_idle_cpu != -1 ? shallowest_idle_cpu : least_loaded_cpu; in find_idlest_group_cpu()
7196 int cpu, int prev_cpu, int sd_flag) in find_idlest_cpu() argument
7198 int new_cpu = cpu; in find_idlest_cpu()
7200 if (!cpumask_intersects(sched_domain_span(sd), p->cpus_ptr)) in find_idlest_cpu()
7208 sync_entity_load_avg(&p->se); in find_idlest_cpu()
7215 if (!(sd->flags & sd_flag)) { in find_idlest_cpu()
7216 sd = sd->child; in find_idlest_cpu()
7220 group = find_idlest_group(sd, p, cpu); in find_idlest_cpu()
7222 sd = sd->child; in find_idlest_cpu()
7226 new_cpu = find_idlest_group_cpu(group, p, cpu); in find_idlest_cpu()
7227 if (new_cpu == cpu) { in find_idlest_cpu()
7228 /* Now try balancing at a lower domain level of 'cpu': */ in find_idlest_cpu()
7229 sd = sd->child; in find_idlest_cpu()
7234 cpu = new_cpu; in find_idlest_cpu()
7235 weight = sd->span_weight; in find_idlest_cpu()
7237 for_each_domain(cpu, tmp) { in find_idlest_cpu()
7238 if (weight <= tmp->span_weight) in find_idlest_cpu()
7240 if (tmp->flags & sd_flag) in find_idlest_cpu()
7248 static inline int __select_idle_cpu(int cpu, struct task_struct *p) in __select_idle_cpu() argument
7250 if ((available_idle_cpu(cpu) || sched_idle_cpu(cpu)) && in __select_idle_cpu()
7251 sched_cpu_cookie_match(cpu_rq(cpu), p)) in __select_idle_cpu()
7252 return cpu; in __select_idle_cpu()
7254 return -1; in __select_idle_cpu()
7261 static inline void set_idle_cores(int cpu, int val) in set_idle_cores() argument
7265 sds = rcu_dereference(per_cpu(sd_llc_shared, cpu)); in set_idle_cores()
7267 WRITE_ONCE(sds->has_idle_cores, val); in set_idle_cores()
7270 static inline bool test_idle_cores(int cpu) in test_idle_cores() argument
7274 sds = rcu_dereference(per_cpu(sd_llc_shared, cpu)); in test_idle_cores()
7276 return READ_ONCE(sds->has_idle_cores); in test_idle_cores()
7283 * information in sd_llc_shared->has_idle_cores.
7291 int cpu; in __update_idle_core() local
7297 for_each_cpu(cpu, cpu_smt_mask(core)) { in __update_idle_core()
7298 if (cpu == core) in __update_idle_core()
7301 if (!available_idle_cpu(cpu)) in __update_idle_core()
7313 * sd_llc->shared->has_idle_cores and enabled through update_idle_core() above.
7318 int cpu; in select_idle_core() local
7320 for_each_cpu(cpu, cpu_smt_mask(core)) { in select_idle_core()
7321 if (!available_idle_cpu(cpu)) { in select_idle_core()
7323 if (*idle_cpu == -1) { in select_idle_core()
7324 if (sched_idle_cpu(cpu) && cpumask_test_cpu(cpu, cpus)) { in select_idle_core()
7325 *idle_cpu = cpu; in select_idle_core()
7332 if (*idle_cpu == -1 && cpumask_test_cpu(cpu, cpus)) in select_idle_core()
7333 *idle_cpu = cpu; in select_idle_core()
7340 return -1; in select_idle_core()
7348 int cpu; in select_idle_smt() local
7350 for_each_cpu_and(cpu, cpu_smt_mask(target), p->cpus_ptr) { in select_idle_smt()
7351 if (cpu == target) in select_idle_smt()
7354 * Check if the CPU is in the LLC scheduling domain of @target. in select_idle_smt()
7357 if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) in select_idle_smt()
7359 if (available_idle_cpu(cpu) || sched_idle_cpu(cpu)) in select_idle_smt()
7360 return cpu; in select_idle_smt()
7363 return -1; in select_idle_smt()
7368 static inline void set_idle_cores(int cpu, int val) in set_idle_cores() argument
7372 static inline bool test_idle_cores(int cpu) in test_idle_cores() argument
7384 return -1; in select_idle_smt()
7391 * comparing the average scan cost (tracked in sd->avg_scan_cost) against the
7392 * average idle time for this rq (as found in rq->avg_idle).
7397 int i, cpu, idle_cpu = -1, nr = INT_MAX; in select_idle_cpu() local
7404 cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr); in select_idle_cpu()
7412 return -1; in select_idle_cpu()
7419 if (unlikely(this_rq->wake_stamp < now)) { in select_idle_cpu()
7420 while (this_rq->wake_stamp < now && this_rq->wake_avg_idle) { in select_idle_cpu()
7421 this_rq->wake_stamp++; in select_idle_cpu()
7422 this_rq->wake_avg_idle >>= 1; in select_idle_cpu()
7426 avg_idle = this_rq->wake_avg_idle; in select_idle_cpu()
7427 avg_cost = this_sd->avg_scan_cost + 1; in select_idle_cpu()
7429 span_avg = sd->span_weight * avg_idle; in select_idle_cpu()
7441 /* because !--nr is the condition to stop scan */ in select_idle_cpu()
7442 nr = READ_ONCE(sd_share->nr_idle_scan) + 1; in select_idle_cpu()
7443 /* overloaded LLC is unlikely to have idle cpu/core */ in select_idle_cpu()
7445 return -1; in select_idle_cpu()
7449 for_each_cpu_wrap(cpu, cpus, target + 1) { in select_idle_cpu()
7451 i = select_idle_core(p, cpu, cpus, &idle_cpu); in select_idle_cpu()
7456 if (!--nr) in select_idle_cpu()
7457 return -1; in select_idle_cpu()
7458 idle_cpu = __select_idle_cpu(cpu, p); in select_idle_cpu()
7468 time = cpu_clock(this) - time; in select_idle_cpu()
7474 this_rq->wake_avg_idle -= min(this_rq->wake_avg_idle, time); in select_idle_cpu()
7476 update_avg(&this_sd->avg_scan_cost, time); in select_idle_cpu()
7484 * the task fits. If no CPU is big enough, but there are idle ones, try to
7485 * maximize capacity.
7492 int cpu, best_cpu = -1; in select_idle_capacity() local
7496 cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr); in select_idle_capacity()
7502 for_each_cpu_wrap(cpu, cpus, target) { in select_idle_capacity()
7503 unsigned long cpu_cap = capacity_of(cpu); in select_idle_capacity()
7505 if (!available_idle_cpu(cpu) && !sched_idle_cpu(cpu)) in select_idle_capacity()
7508 fits = util_fits_cpu(task_util, util_min, util_max, cpu); in select_idle_capacity()
7510 /* This CPU fits with all requirements */ in select_idle_capacity()
7512 return cpu; in select_idle_capacity()
7515 * Look for the CPU with best capacity. in select_idle_capacity()
7518 cpu_cap = capacity_orig_of(cpu) - thermal_load_avg(cpu_rq(cpu)); in select_idle_capacity()
7521 * First, select CPU which fits better (-1 being better than 0). in select_idle_capacity()
7522 * Then, select the one with best capacity at same level. in select_idle_capacity()
7527 best_cpu = cpu; in select_idle_capacity()
7538 int cpu) in asym_fits_cpu() argument
7542 * Return true only if the cpu fully fits the task requirements in asym_fits_cpu()
7545 return (util_fits_cpu(util, util_min, util_max, cpu) > 0); in asym_fits_cpu()
7562 * that the task fits with cpu's capacity. in select_idle_sibling()
7565 sync_entity_load_avg(&p->se); in select_idle_sibling()
7572 * per-cpu select_rq_mask usage in select_idle_sibling()
7581 * If the previous CPU is cache affine and idle, don't be stupid: in select_idle_sibling()
7589 * Allow a per-cpu kthread to stack with the wakee if the in select_idle_sibling()
7592 * per-cpu kthread that is now complete and the wakeup is in select_idle_sibling()
7599 this_rq()->nr_running <= 1 && in select_idle_sibling()
7604 /* Check a recently used CPU as a potential idle candidate: */ in select_idle_sibling()
7605 recent_used_cpu = p->recent_used_cpu; in select_idle_sibling()
7606 p->recent_used_cpu = prev; in select_idle_sibling()
7611 cpumask_test_cpu(recent_used_cpu, p->cpus_ptr) && in select_idle_sibling()
7617 * For asymmetric CPU capacity systems, our domain of interest is in select_idle_sibling()
7623 * On an asymmetric CPU capacity system where an exclusive in select_idle_sibling()
7628 * capacity path. in select_idle_sibling()
7658 * cpu_util() - Estimates the amount of CPU capacity used by CFS tasks.
7659 * @cpu: the CPU to get the utilization for
7660 * @p: task for which the CPU utilization should be predicted or NULL
7661 * @dst_cpu: CPU @p migrates to, -1 if @p moves from @cpu or @p == NULL
7664 * The unit of the return value must be the same as the one of CPU capacity
7665 * so that CPU utilization can be compared with CPU capacity.
7667 * CPU utilization is the sum of running time of runnable tasks plus the
7668 * recent utilization of currently non-runnable tasks on that CPU.
7669 * It represents the amount of CPU capacity currently used by CFS tasks in
7670 * the range [0..max CPU capacity] with max CPU capacity being the CPU
7671 * capacity at f_max.
7673 * The estimated CPU utilization is defined as the maximum between CPU
7675 * runnable tasks on that CPU. It preserves a utilization "snapshot" of
7676 * previously-executed tasks, which helps better deduce how busy a CPU will
7677 * be when a long-sleeping task wakes up. The contribution to CPU utilization
7680 * Boosted CPU utilization is defined as max(CPU runnable, CPU utilization).
7681 * CPU contention for CFS tasks can be detected by CPU runnable > CPU
7686 * CPU utilization can be higher than the current CPU capacity
7687 * (f_curr/f_max * max CPU capacity) or even the max CPU capacity because
7689 * CPU utilization has to be capped to fit into the [0..max CPU capacity]
7691 * could be seen as over-utilized even though CPU1 has 20% of spare CPU
7692 * capacity. CPU utilization is allowed to overshoot current CPU capacity
7693 * though since this is useful for predicting the CPU capacity required
7694 * after task migrations (scheduler-driven DVFS).
7696 * Return: (Boosted) (estimated) utilization for the specified CPU.
7699 cpu_util(int cpu, struct task_struct *p, int dst_cpu, int boost) in cpu_util() argument
7701 struct cfs_rq *cfs_rq = &cpu_rq(cpu)->cfs; in cpu_util()
7702 unsigned long util = READ_ONCE(cfs_rq->avg.util_avg); in cpu_util()
7706 runnable = READ_ONCE(cfs_rq->avg.runnable_avg); in cpu_util()
7711 * If @dst_cpu is -1 or @p migrates from @cpu to @dst_cpu remove its in cpu_util()
7712 * contribution. If @p migrates from another CPU to @cpu add its in cpu_util()
7713 * contribution. In all the other cases @cpu is not impacted by the in cpu_util()
7716 if (p && task_cpu(p) == cpu && dst_cpu != cpu) in cpu_util()
7718 else if (p && task_cpu(p) != cpu && dst_cpu == cpu) in cpu_util()
7724 util_est = READ_ONCE(cfs_rq->avg.util_est.enqueued); in cpu_util()
7727 * During wake-up @p isn't enqueued yet and doesn't contribute in cpu_util()
7728 * to any cpu_rq(cpu)->cfs.avg.util_est.enqueued. in cpu_util()
7729 * If @dst_cpu == @cpu add it to "simulate" cpu_util after @p in cpu_util()
7732 * During exec (@dst_cpu = -1) @p is enqueued and does in cpu_util()
7733 * contribute to cpu_rq(cpu)->cfs.util_est.enqueued. in cpu_util()
7742 * p->on_rq = TASK_ON_RQ_MIGRATING; in cpu_util()
7743 * -------------------------------- A in cpu_util()
7747 * -------------------------------- B in cpu_util()
7752 if (dst_cpu == cpu) in cpu_util()
7760 return min(util, capacity_orig_of(cpu)); in cpu_util()
7763 unsigned long cpu_util_cfs(int cpu) in cpu_util_cfs() argument
7765 return cpu_util(cpu, NULL, -1, 0); in cpu_util_cfs()
7768 unsigned long cpu_util_cfs_boost(int cpu) in cpu_util_cfs_boost() argument
7770 return cpu_util(cpu, NULL, -1, 1); in cpu_util_cfs_boost()
7774 * cpu_util_without: compute cpu utilization without any contributions from *p
7775 * @cpu: the CPU which utilization is requested
7778 * The utilization of a CPU is defined by the utilization of tasks currently
7779 * enqueued on that CPU as well as tasks which are currently sleeping after an
7780 * execution on that CPU.
7782 * This method returns the utilization of the specified CPU by discounting the
7784 * contributing to the CPU utilization.
7786 static unsigned long cpu_util_without(int cpu, struct task_struct *p) in cpu_util_without() argument
7789 if (cpu != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in cpu_util_without()
7792 return cpu_util(cpu, p, -1, 0); in cpu_util_without()
7796 * energy_env - Utilization landscape for energy estimation.
7801 * @cpu_cap: Maximum CPU capacity for the perf domain.
7802 * @pd_cap: Entire perf domain capacity. (pd->nr_cpus * cpu_cap).
7828 eenv->task_busy_time = busy_time; in eenv_task_busy_time()
7842 * - A stable PD utilization, no matter which CPU of that PD we want to place
7845 * - A fair comparison between CPUs as the task contribution (task_util())
7846 * will always be the same no matter which CPU utilization we rely on
7850 * exceed @eenv->pd_cap.
7857 int cpu; in eenv_pd_busy_time() local
7859 for_each_cpu(cpu, pd_cpus) { in eenv_pd_busy_time()
7860 unsigned long util = cpu_util(cpu, p, -1, 0); in eenv_pd_busy_time()
7862 busy_time += effective_cpu_util(cpu, util, ENERGY_UTIL, NULL); in eenv_pd_busy_time()
7865 eenv->pd_busy_time = min(eenv->pd_cap, busy_time); in eenv_pd_busy_time()
7870 * is placed on the cpu @dst_cpu.
7872 * Returns the maximum utilization among @eenv->cpus. This utilization can't
7873 * exceed @eenv->cpu_cap.
7880 int cpu; in eenv_pd_max_util() local
7882 for_each_cpu(cpu, pd_cpus) { in eenv_pd_max_util()
7883 struct task_struct *tsk = (cpu == dst_cpu) ? p : NULL; in eenv_pd_max_util()
7884 unsigned long util = cpu_util(cpu, p, dst_cpu, 1); in eenv_pd_max_util()
7894 eff_util = effective_cpu_util(cpu, util, FREQUENCY_UTIL, tsk); in eenv_pd_max_util()
7898 return min(max_util, eenv->cpu_cap); in eenv_pd_max_util()
7911 unsigned long busy_time = eenv->pd_busy_time; in compute_energy()
7914 busy_time = min(eenv->pd_cap, busy_time + eenv->task_busy_time); in compute_energy()
7916 return em_cpu_energy(pd->em_pd, max_util, busy_time, eenv->cpu_cap); in compute_energy()
7920 * find_energy_efficient_cpu(): Find most energy-efficient target CPU for the
7921 * waking task. find_energy_efficient_cpu() looks for the CPU with maximum
7922 * spare capacity in each performance domain and uses it as a potential
7924 * out which of the CPU candidates is the most energy-efficient.
7927 * all the most energy efficient CPU candidates (according to the Energy
7932 * frequency requests follow utilization (e.g. using schedutil), the CPU with
7933 * the maximum spare capacity in a performance domain is guaranteed to be among
7937 * small tasks on a CPU in order to let other CPUs go in deeper idle states,
7941 * cluster-packing, and spreading inside a cluster. That should at least be
7948 * NOTE: Forkees are not accepted in the energy-aware wake-up path because
7951 * placed by find_idlest_cpu() on the least loaded CPU, which might turn out
7952 * to be energy-inefficient in some use-cases. The alternative would be to
7955 * other use-cases too. So, until someone finds a better way to solve this,
7956 * let's keep things simple by re-using the existing slow path.
7964 struct root_domain *rd = this_rq()->rd; in find_energy_efficient_cpu()
7965 int cpu, best_energy_cpu, target = -1; in find_energy_efficient_cpu() local
7966 int prev_fits = -1, best_fits = -1; in find_energy_efficient_cpu()
7974 pd = rcu_dereference(rd->pd); in find_energy_efficient_cpu()
7975 if (!pd || READ_ONCE(rd->overutilized)) in find_energy_efficient_cpu()
7979 * Energy-aware wake-up happens on the lowest sched_domain starting in find_energy_efficient_cpu()
7984 sd = sd->parent; in find_energy_efficient_cpu()
7990 sync_entity_load_avg(&p->se); in find_energy_efficient_cpu()
7996 for (; pd; pd = pd->next) { in find_energy_efficient_cpu()
7999 long prev_spare_cap = -1, max_spare_cap = -1; in find_energy_efficient_cpu()
8002 int max_spare_cap_cpu = -1; in find_energy_efficient_cpu()
8003 int fits, max_fits = -1; in find_energy_efficient_cpu()
8011 cpu = cpumask_first(cpus); in find_energy_efficient_cpu()
8012 cpu_thermal_cap = arch_scale_cpu_capacity(cpu); in find_energy_efficient_cpu()
8013 cpu_thermal_cap -= arch_scale_thermal_pressure(cpu); in find_energy_efficient_cpu()
8018 for_each_cpu(cpu, cpus) { in find_energy_efficient_cpu()
8019 struct rq *rq = cpu_rq(cpu); in find_energy_efficient_cpu()
8023 if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) in find_energy_efficient_cpu()
8026 if (!cpumask_test_cpu(cpu, p->cpus_ptr)) in find_energy_efficient_cpu()
8029 util = cpu_util(cpu, p, cpu, 0); in find_energy_efficient_cpu()
8030 cpu_cap = capacity_of(cpu); in find_energy_efficient_cpu()
8033 * Skip CPUs that cannot satisfy the capacity request. in find_energy_efficient_cpu()
8034 * IOW, placing the task there would make the CPU in find_energy_efficient_cpu()
8036 * much capacity we can get out of the CPU; this is in find_energy_efficient_cpu()
8045 * max-aggregated uclamp_{min, max}. in find_energy_efficient_cpu()
8054 fits = util_fits_cpu(util, util_min, util_max, cpu); in find_energy_efficient_cpu()
8060 if (cpu == prev_cpu) { in find_energy_efficient_cpu()
8067 * Find the CPU with the maximum spare capacity in find_energy_efficient_cpu()
8072 max_spare_cap_cpu = cpu; in find_energy_efficient_cpu()
8082 base_energy = compute_energy(&eenv, pd, cpus, p, -1); in find_energy_efficient_cpu()
8085 if (prev_spare_cap > -1) { in find_energy_efficient_cpu()
8088 /* CPU utilization has changed */ in find_energy_efficient_cpu()
8091 prev_delta -= base_energy; in find_energy_efficient_cpu()
8098 /* Current best energy cpu fits better */ in find_energy_efficient_cpu()
8104 * but best energy cpu has better capacity. in find_energy_efficient_cpu()
8112 /* CPU utilization has changed */ in find_energy_efficient_cpu()
8115 cur_delta -= base_energy; in find_energy_efficient_cpu()
8118 * Both fit for the task but best energy cpu has lower in find_energy_efficient_cpu()
8151 * Balances load by selecting the idlest CPU in the idlest group, or under
8152 * certain conditions an idle sibling CPU if the domain has SD_WAKE_AFFINE set.
8154 * Returns the target CPU number.
8159 int sync = (wake_flags & WF_SYNC) && !(current->flags & PF_EXITING); in select_task_rq_fair()
8161 int cpu = smp_processor_id(); in select_task_rq_fair() local
8168 * required for stable ->cpus_allowed in select_task_rq_fair()
8170 lockdep_assert_held(&p->pi_lock); in select_task_rq_fair()
8175 cpumask_test_cpu(cpu, p->cpus_ptr)) in select_task_rq_fair()
8176 return cpu; in select_task_rq_fair()
8185 want_affine = !wake_wide(p) && cpumask_test_cpu(cpu, p->cpus_ptr); in select_task_rq_fair()
8189 for_each_domain(cpu, tmp) { in select_task_rq_fair()
8191 * If both 'cpu' and 'prev_cpu' are part of this domain, in select_task_rq_fair()
8192 * cpu is a valid SD_WAKE_AFFINE target. in select_task_rq_fair()
8194 if (want_affine && (tmp->flags & SD_WAKE_AFFINE) && in select_task_rq_fair()
8196 if (cpu != prev_cpu) in select_task_rq_fair()
8197 new_cpu = wake_affine(tmp, p, cpu, prev_cpu, sync); in select_task_rq_fair()
8208 if (tmp->flags & sd_flag) in select_task_rq_fair()
8216 new_cpu = find_idlest_cpu(sd, p, cpu, prev_cpu, sd_flag); in select_task_rq_fair()
8227 * Called immediately before a task is migrated to a new CPU; task_cpu(p) and
8229 * previous CPU. The caller guarantees p->pi_lock or task_rq(p)->lock is held.
8233 struct sched_entity *se = &p->se; in migrate_task_rq_fair()
8242 * leading to an inflation after wake-up on the new rq. in migrate_task_rq_fair()
8251 /* Tell new CPU we are migrated */ in migrate_task_rq_fair()
8252 se->avg.last_update_time = 0; in migrate_task_rq_fair()
8259 remove_entity_load_avg(&p->se); in task_dead_fair()
8265 if (rq->nr_running) in balance_fair()
8275 if (SCHED_WARN_ON(!se->on_rq)) in set_next_buddy()
8279 cfs_rq_of(se)->next = se; in set_next_buddy()
8288 struct task_struct *curr = rq->curr; in check_preempt_wakeup_fair()
8289 struct sched_entity *se = &curr->se, *pse = &p->se; in check_preempt_wakeup_fair()
8301 * next-buddy nomination below. in check_preempt_wakeup_fair()
8315 * Note: this also catches the edge-case of curr being in a throttled in check_preempt_wakeup_fair()
8334 * Preempt an idle entity in favor of a non-idle entity (and don't preempt in check_preempt_wakeup_fair()
8345 if (unlikely(p->policy != SCHED_NORMAL)) in check_preempt_wakeup_fair()
8369 cfs_rq = &rq->cfs; in pick_task_fair()
8370 if (!cfs_rq->nr_running) in pick_task_fair()
8374 struct sched_entity *curr = cfs_rq->curr; in pick_task_fair()
8378 if (curr->on_rq) in pick_task_fair()
8398 struct cfs_rq *cfs_rq = &rq->cfs; in pick_next_task_fair()
8408 if (!prev || prev->sched_class != &fair_sched_class) in pick_next_task_fair()
8420 struct sched_entity *curr = cfs_rq->curr; in pick_next_task_fair()
8424 * have to consider cfs_rq->curr. If it is still a runnable in pick_next_task_fair()
8429 if (curr->on_rq) in pick_next_task_fair()
8441 cfs_rq = &rq->cfs; in pick_next_task_fair()
8443 if (!cfs_rq->nr_running) in pick_next_task_fair()
8462 struct sched_entity *pse = &prev->se; in pick_next_task_fair()
8465 int se_depth = se->depth; in pick_next_task_fair()
8466 int pse_depth = pse->depth; in pick_next_task_fair()
8503 list_move(&p->se.group_node, &rq->cfs_tasks); in pick_next_task_fair()
8521 * Because newidle_balance() releases (and re-acquires) rq->lock, it is in pick_next_task_fair()
8523 * must re-start the pick_next_entity() loop. in pick_next_task_fair()
8550 struct sched_entity *se = &prev->se; in put_prev_task_fair()
8564 struct task_struct *curr = rq->curr; in yield_task_fair()
8566 struct sched_entity *se = &curr->se; in yield_task_fair()
8571 if (unlikely(rq->nr_running == 1)) in yield_task_fair()
8578 * Update run-time statistics of the 'current'. in yield_task_fair()
8588 se->deadline += calc_delta_fair(se->slice, se); in yield_task_fair()
8593 struct sched_entity *se = &p->se; in yield_to_task_fair()
8596 if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se))) in yield_to_task_fair()
8609 * Fair scheduling class load-balancing methods.
8613 * The purpose of load-balancing is to achieve the same basic fairness the
8614 * per-CPU scheduler provides, namely provide a proportional amount of compute
8619 * Where W_i,n is the n-th weight average for CPU i. The instantaneous weight
8624 * Where w_i,j is the weight of the j-th runnable task on CPU i. This weight
8630 * W'_i,n = (2^n - 1) / 2^n * W_i,n + 1 / 2^n * W_i,0 (3)
8632 * C_i is the compute capacity of CPU i, typically it is the
8639 * imb_i,j = max{ avg(W/C), W_i/C_i } - min{ avg(W/C), W_j/C_j } (4)
8646 * - infeasible weights;
8647 * - local vs global optima in the discrete case. ]
8657 * of load-balance at each level inv. proportional to the number of CPUs in
8663 * \Sum { --- * --- * 2^i } = O(n) (5)
8665 * `- size of each group
8666 * | | `- number of CPUs doing load-balance
8667 * | `- freq
8668 * `- sum over all levels
8673 * An important property here is that each CPU is still (indirectly) connected
8674 * to every other CPU in at most O(log n) steps:
8686 * Showing there's indeed a path between every CPU in at most O(log n) steps.
8696 * balancing is more aggressive and has the newly idle CPU iterate up the domain
8710 * W_i,0 = \Sum_j \Prod_k w_k * ----- (9)
8717 * w_i,j,k is the weight of the j-th runnable task in the k-th cgroup on CPU i.
8738 /* The group has spare capacity that can be used to run more tasks. */
8741 * The group is fully used and the tasks don't compete for more CPU
8746 * One task doesn't fit with CPU's capacity and must be migrated to a
8747 * more powerful CPU.
8752 * a task on SMT with busy sibling to another CPU on idle core.
8756 * SD_ASYM_PACKING only: One local CPU with higher capacity is available,
8758 * current CPU.
8767 * The CPU is overloaded and can't provide expected CPU cycles to all
8799 /* The set of CPUs under consideration for load-balancing */
8814 * Is this task likely cache-hot:
8820 lockdep_assert_rq_held(env->src_rq); in task_hot()
8822 if (p->sched_class != &fair_sched_class) in task_hot()
8829 if (env->sd->flags & SD_SHARE_CPUCAPACITY) in task_hot()
8835 if (sched_feat(CACHE_HOT_BUDDY) && env->dst_rq->nr_running && in task_hot()
8836 (&p->se == cfs_rq_of(&p->se)->next)) in task_hot()
8839 if (sysctl_sched_migration_cost == -1) in task_hot()
8844 * with the destination CPU's core cookie. in task_hot()
8846 if (!sched_core_cookie_match(cpu_rq(env->dst_cpu), p)) in task_hot()
8852 delta = rq_clock_task(env->src_rq) - p->se.exec_start; in task_hot()
8861 * Returns -1, if task migration is not affected by locality.
8865 struct numa_group *numa_group = rcu_dereference(p->numa_group); in migrate_degrades_locality()
8870 return -1; in migrate_degrades_locality()
8872 if (!p->numa_faults || !(env->sd->flags & SD_NUMA)) in migrate_degrades_locality()
8873 return -1; in migrate_degrades_locality()
8875 src_nid = cpu_to_node(env->src_cpu); in migrate_degrades_locality()
8876 dst_nid = cpu_to_node(env->dst_cpu); in migrate_degrades_locality()
8879 return -1; in migrate_degrades_locality()
8882 if (src_nid == p->numa_preferred_nid) { in migrate_degrades_locality()
8883 if (env->src_rq->nr_running > env->src_rq->nr_preferred_running) in migrate_degrades_locality()
8886 return -1; in migrate_degrades_locality()
8890 if (dst_nid == p->numa_preferred_nid) in migrate_degrades_locality()
8894 if (env->idle == CPU_IDLE) in migrate_degrades_locality()
8895 return -1; in migrate_degrades_locality()
8913 return -1; in migrate_degrades_locality()
8918 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
8925 lockdep_assert_rq_held(env->src_rq); in can_migrate_task()
8926 if (p->sched_task_hot) in can_migrate_task()
8927 p->sched_task_hot = 0; in can_migrate_task()
8932 * 2) cannot be migrated to this CPU due to cpus_ptr, or in can_migrate_task()
8934 * 4) are cache-hot on their current CPU. in can_migrate_task()
8936 if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu)) in can_migrate_task()
8943 if (!cpumask_test_cpu(env->dst_cpu, p->cpus_ptr)) { in can_migrate_task()
8944 int cpu; in can_migrate_task() local
8946 schedstat_inc(p->stats.nr_failed_migrations_affine); in can_migrate_task()
8948 env->flags |= LBF_SOME_PINNED; in can_migrate_task()
8951 * Remember if this task can be migrated to any other CPU in in can_migrate_task()
8956 * - for NEWLY_IDLE in can_migrate_task()
8957 * - if we have already computed one in current iteration in can_migrate_task()
8958 * - if it's an active balance in can_migrate_task()
8960 if (env->idle == CPU_NEWLY_IDLE || in can_migrate_task()
8961 env->flags & (LBF_DST_PINNED | LBF_ACTIVE_LB)) in can_migrate_task()
8964 /* Prevent to re-select dst_cpu via env's CPUs: */ in can_migrate_task()
8965 for_each_cpu_and(cpu, env->dst_grpmask, env->cpus) { in can_migrate_task()
8966 if (cpumask_test_cpu(cpu, p->cpus_ptr)) { in can_migrate_task()
8967 env->flags |= LBF_DST_PINNED; in can_migrate_task()
8968 env->new_dst_cpu = cpu; in can_migrate_task()
8977 env->flags &= ~LBF_ALL_PINNED; in can_migrate_task()
8979 if (task_on_cpu(env->src_rq, p)) { in can_migrate_task()
8980 schedstat_inc(p->stats.nr_failed_migrations_running); in can_migrate_task()
8991 if (env->flags & LBF_ACTIVE_LB) in can_migrate_task()
8995 if (tsk_cache_hot == -1) in can_migrate_task()
8999 env->sd->nr_balance_failed > env->sd->cache_nice_tries) { in can_migrate_task()
9001 p->sched_task_hot = 1; in can_migrate_task()
9005 schedstat_inc(p->stats.nr_failed_migrations_hot); in can_migrate_task()
9010 * detach_task() -- detach the task for the migration specified in env
9014 lockdep_assert_rq_held(env->src_rq); in detach_task()
9016 if (p->sched_task_hot) { in detach_task()
9017 p->sched_task_hot = 0; in detach_task()
9018 schedstat_inc(env->sd->lb_hot_gained[env->idle]); in detach_task()
9019 schedstat_inc(p->stats.nr_forced_migrations); in detach_task()
9022 deactivate_task(env->src_rq, p, DEQUEUE_NOCLOCK); in detach_task()
9023 set_task_cpu(p, env->dst_cpu); in detach_task()
9027 * detach_one_task() -- tries to dequeue exactly one task from env->src_rq, as
9036 lockdep_assert_rq_held(env->src_rq); in detach_one_task()
9039 &env->src_rq->cfs_tasks, se.group_node) { in detach_one_task()
9047 * lb_gained[env->idle] is updated (other is detach_tasks) in detach_one_task()
9051 schedstat_inc(env->sd->lb_gained[env->idle]); in detach_one_task()
9058 * detach_tasks() -- tries to detach up to imbalance load/util/tasks from
9065 struct list_head *tasks = &env->src_rq->cfs_tasks; in detach_tasks()
9070 lockdep_assert_rq_held(env->src_rq); in detach_tasks()
9073 * Source run queue has been emptied by another CPU, clear in detach_tasks()
9076 if (env->src_rq->nr_running <= 1) { in detach_tasks()
9077 env->flags &= ~LBF_ALL_PINNED; in detach_tasks()
9081 if (env->imbalance <= 0) in detach_tasks()
9089 if (env->idle != CPU_NOT_IDLE && env->src_rq->nr_running <= 1) in detach_tasks()
9092 env->loop++; in detach_tasks()
9094 if (env->loop > env->loop_max) in detach_tasks()
9098 if (env->loop > env->loop_break) { in detach_tasks()
9099 env->loop_break += SCHED_NR_MIGRATE_BREAK; in detach_tasks()
9100 env->flags |= LBF_NEED_BREAK; in detach_tasks()
9109 switch (env->migration_type) { in detach_tasks()
9114 * value. Make sure that env->imbalance decreases in detach_tasks()
9121 load < 16 && !env->sd->nr_balance_failed) in detach_tasks()
9130 if (shr_bound(load, env->sd->nr_balance_failed) > env->imbalance) in detach_tasks()
9133 env->imbalance -= load; in detach_tasks()
9139 if (util > env->imbalance) in detach_tasks()
9142 env->imbalance -= util; in detach_tasks()
9146 env->imbalance--; in detach_tasks()
9151 if (task_fits_cpu(p, env->src_cpu)) in detach_tasks()
9154 env->imbalance = 0; in detach_tasks()
9159 list_add(&p->se.group_node, &env->tasks); in detach_tasks()
9169 if (env->idle == CPU_NEWLY_IDLE) in detach_tasks()
9177 if (env->imbalance <= 0) in detach_tasks()
9182 if (p->sched_task_hot) in detach_tasks()
9183 schedstat_inc(p->stats.nr_failed_migrations_hot); in detach_tasks()
9185 list_move(&p->se.group_node, tasks); in detach_tasks()
9193 schedstat_add(env->sd->lb_gained[env->idle], detached); in detach_tasks()
9199 * attach_task() -- attach the task detached by detach_task() to its new rq.
9211 * attach_one_task() -- attaches the task returned from detach_one_task() to
9225 * attach_tasks() -- attaches all tasks detached by detach_tasks() to their
9230 struct list_head *tasks = &env->tasks; in attach_tasks()
9234 rq_lock(env->dst_rq, &rf); in attach_tasks()
9235 update_rq_clock(env->dst_rq); in attach_tasks()
9239 list_del_init(&p->se.group_node); in attach_tasks()
9241 attach_task(env->dst_rq, p); in attach_tasks()
9244 rq_unlock(env->dst_rq, &rf); in attach_tasks()
9250 if (cfs_rq->avg.load_avg) in cfs_rq_has_blocked()
9253 if (cfs_rq->avg.util_avg) in cfs_rq_has_blocked()
9261 if (READ_ONCE(rq->avg_rt.util_avg)) in others_have_blocked()
9264 if (READ_ONCE(rq->avg_dl.util_avg)) in others_have_blocked()
9271 if (READ_ONCE(rq->avg_irq.util_avg)) in others_have_blocked()
9280 WRITE_ONCE(rq->last_blocked_load_update_tick, jiffies); in update_blocked_load_tick()
9286 rq->has_blocked_load = 0; in update_blocked_load_status()
9306 curr_class = rq->curr->sched_class; in __update_blocked_others()
9327 int cpu = cpu_of(rq); in __update_blocked_fair() local
9339 if (cfs_rq->nr_running == 0) in __update_blocked_fair()
9342 if (cfs_rq == &rq->cfs) in __update_blocked_fair()
9347 se = cfs_rq->tg->se[cpu]; in __update_blocked_fair()
9352 * There can be a lot of idle CPU cgroups. Don't let fully in __update_blocked_fair()
9368 * This needs to be done in a top-down fashion because the load of a child
9374 struct sched_entity *se = cfs_rq->tg->se[cpu_of(rq)]; in update_cfs_rq_h_load()
9378 if (cfs_rq->last_h_load_update == now) in update_cfs_rq_h_load()
9381 WRITE_ONCE(cfs_rq->h_load_next, NULL); in update_cfs_rq_h_load()
9384 WRITE_ONCE(cfs_rq->h_load_next, se); in update_cfs_rq_h_load()
9385 if (cfs_rq->last_h_load_update == now) in update_cfs_rq_h_load()
9390 cfs_rq->h_load = cfs_rq_load_avg(cfs_rq); in update_cfs_rq_h_load()
9391 cfs_rq->last_h_load_update = now; in update_cfs_rq_h_load()
9394 while ((se = READ_ONCE(cfs_rq->h_load_next)) != NULL) { in update_cfs_rq_h_load()
9395 load = cfs_rq->h_load; in update_cfs_rq_h_load()
9396 load = div64_ul(load * se->avg.load_avg, in update_cfs_rq_h_load()
9399 cfs_rq->h_load = load; in update_cfs_rq_h_load()
9400 cfs_rq->last_h_load_update = now; in update_cfs_rq_h_load()
9409 return div64_ul(p->se.avg.load_avg * cfs_rq->h_load, in task_h_load()
9415 struct cfs_rq *cfs_rq = &rq->cfs; in __update_blocked_fair()
9427 return p->se.avg.load_avg; in task_h_load()
9431 static void update_blocked_averages(int cpu) in update_blocked_averages() argument
9434 struct rq *rq = cpu_rq(cpu); in update_blocked_averages()
9453 * sg_lb_stats - stats of a sched_group required for load_balancing
9466 unsigned int group_asym_packing; /* Tasks should be moved to preferred CPU */
9468 unsigned long group_misfit_task_load; /* A CPU has a task too big for its capacity */
9476 * sd_lb_stats - Structure to store the statistics of a sched_domain
9483 unsigned long total_capacity; /* Total capacity of all groups in sd */
9512 static unsigned long scale_rt_capacity(int cpu) in scale_rt_capacity() argument
9514 struct rq *rq = cpu_rq(cpu); in scale_rt_capacity()
9515 unsigned long max = arch_scale_cpu_capacity(cpu); in scale_rt_capacity()
9528 * average uses the actual delta max capacity(load). in scale_rt_capacity()
9530 used = READ_ONCE(rq->avg_rt.util_avg); in scale_rt_capacity()
9531 used += READ_ONCE(rq->avg_dl.util_avg); in scale_rt_capacity()
9537 free = max - used; in scale_rt_capacity()
9542 static void update_cpu_capacity(struct sched_domain *sd, int cpu) in update_cpu_capacity() argument
9544 unsigned long capacity = scale_rt_capacity(cpu); in update_cpu_capacity() local
9545 struct sched_group *sdg = sd->groups; in update_cpu_capacity()
9547 cpu_rq(cpu)->cpu_capacity_orig = arch_scale_cpu_capacity(cpu); in update_cpu_capacity()
9549 if (!capacity) in update_cpu_capacity()
9550 capacity = 1; in update_cpu_capacity()
9552 cpu_rq(cpu)->cpu_capacity = capacity; in update_cpu_capacity()
9553 trace_sched_cpu_capacity_tp(cpu_rq(cpu)); in update_cpu_capacity()
9555 sdg->sgc->capacity = capacity; in update_cpu_capacity()
9556 sdg->sgc->min_capacity = capacity; in update_cpu_capacity()
9557 sdg->sgc->max_capacity = capacity; in update_cpu_capacity()
9560 void update_group_capacity(struct sched_domain *sd, int cpu) in update_group_capacity() argument
9562 struct sched_domain *child = sd->child; in update_group_capacity()
9563 struct sched_group *group, *sdg = sd->groups; in update_group_capacity()
9564 unsigned long capacity, min_capacity, max_capacity; in update_group_capacity() local
9567 interval = msecs_to_jiffies(sd->balance_interval); in update_group_capacity()
9569 sdg->sgc->next_update = jiffies + interval; in update_group_capacity()
9572 update_cpu_capacity(sd, cpu); in update_group_capacity()
9576 capacity = 0; in update_group_capacity()
9580 if (child->flags & SD_OVERLAP) { in update_group_capacity()
9586 for_each_cpu(cpu, sched_group_span(sdg)) { in update_group_capacity()
9587 unsigned long cpu_cap = capacity_of(cpu); in update_group_capacity()
9589 capacity += cpu_cap; in update_group_capacity()
9599 group = child->groups; in update_group_capacity()
9601 struct sched_group_capacity *sgc = group->sgc; in update_group_capacity()
9603 capacity += sgc->capacity; in update_group_capacity()
9604 min_capacity = min(sgc->min_capacity, min_capacity); in update_group_capacity()
9605 max_capacity = max(sgc->max_capacity, max_capacity); in update_group_capacity()
9606 group = group->next; in update_group_capacity()
9607 } while (group != child->groups); in update_group_capacity()
9610 sdg->sgc->capacity = capacity; in update_group_capacity()
9611 sdg->sgc->min_capacity = min_capacity; in update_group_capacity()
9612 sdg->sgc->max_capacity = max_capacity; in update_group_capacity()
9616 * Check whether the capacity of the rq has been noticeably reduced by side
9618 * Return true is the capacity is reduced
9623 return ((rq->cpu_capacity * sd->imbalance_pct) < in check_cpu_capacity()
9624 (rq->cpu_capacity_orig * 100)); in check_cpu_capacity()
9629 * help that task: we can migrate the task to a CPU of higher capacity, or
9630 * the task's current CPU is heavily pressured.
9634 return rq->misfit_task_load && in check_misfit_status()
9635 (rq->cpu_capacity_orig < rq->rd->max_cpu_capacity || in check_misfit_status()
9641 * groups is inadequate due to ->cpus_ptr constraints.
9644 * cpumask covering 1 CPU of the first group and 3 CPUs of the second group.
9650 * If we were to balance group-wise we'd place two tasks in the first group and
9652 * cpu 3 and leave one of the CPUs in the second group unused.
9670 return group->sgc->imbalance; in sg_imbalanced()
9674 * group_has_capacity returns true if the group has spare capacity that could
9676 * We consider that a group has spare capacity if the number of task is
9678 * available capacity for CFS tasks.
9681 * capacity in meaningful for the load balancer.
9682 * As an example, an available capacity of 1% can appear but it doesn't make
9688 if (sgs->sum_nr_running < sgs->group_weight) in group_has_capacity()
9691 if ((sgs->group_capacity * imbalance_pct) < in group_has_capacity()
9692 (sgs->group_runnable * 100)) in group_has_capacity()
9695 if ((sgs->group_capacity * 100) > in group_has_capacity()
9696 (sgs->group_util * imbalance_pct)) in group_has_capacity()
9706 * with the exact right number of tasks, has no more spare capacity but is not
9713 if (sgs->sum_nr_running <= sgs->group_weight) in group_is_overloaded()
9716 if ((sgs->group_capacity * 100) < in group_is_overloaded()
9717 (sgs->group_util * imbalance_pct)) in group_is_overloaded()
9720 if ((sgs->group_capacity * imbalance_pct) < in group_is_overloaded()
9721 (sgs->group_runnable * 100)) in group_is_overloaded()
9738 if (sgs->group_asym_packing) in group_classify()
9741 if (sgs->group_smt_balance) in group_classify()
9744 if (sgs->group_misfit_task_load) in group_classify()
9754 * sched_use_asym_prio - Check whether asym_packing priority must be used
9756 * @cpu: A CPU
9758 * Always use CPU priority when balancing load between SMT siblings. When
9759 * balancing load between cores, it is not sufficient that @cpu is idle. Only
9760 * use CPU priority if the whole core is idle.
9762 * Returns: True if the priority of @cpu must be followed. False otherwise.
9764 static bool sched_use_asym_prio(struct sched_domain *sd, int cpu) in sched_use_asym_prio() argument
9769 return sd->flags & SD_SHARE_CPUCAPACITY || is_core_idle(cpu); in sched_use_asym_prio()
9773 * sched_asym - Check if the destination CPU can do asym_packing load balance
9775 * @sds: Load-balancing data with statistics of the local group
9776 * @sgs: Load-balancing statistics of the candidate busiest group
9780 * preferred CPU of @group.
9784 * can only do it if @group is an SMT group and has exactly on busy CPU. Larger
9798 if (!sched_use_asym_prio(env->sd, env->dst_cpu)) in sched_asym()
9802 * CPU priorities does not make sense for SMT cores with more than one in sched_asym()
9805 if (group->flags & SD_SHARE_CPUCAPACITY) { in sched_asym()
9806 if (sgs->group_weight - sgs->idle_cpus != 1) in sched_asym()
9810 return sched_asym_prefer(env->dst_cpu, group->asym_prefer_cpu); in sched_asym()
9813 /* One group has more than one SMT CPU while the other group does not */
9820 return (sg1->flags & SD_SHARE_CPUCAPACITY) != in smt_vs_nonsmt_groups()
9821 (sg2->flags & SD_SHARE_CPUCAPACITY); in smt_vs_nonsmt_groups()
9827 if (env->idle == CPU_NOT_IDLE) in smt_balance()
9832 * to a CPU that doesn't have multiple tasks sharing its CPU capacity. in smt_balance()
9836 if (group->flags & SD_SHARE_CPUCAPACITY && in smt_balance()
9837 sgs->sum_h_nr_running > 1) in smt_balance()
9851 if (env->idle == CPU_NOT_IDLE || !busiest->sum_nr_running) in sibling_imbalance()
9854 ncores_busiest = sds->busiest->cores; in sibling_imbalance()
9855 ncores_local = sds->local->cores; in sibling_imbalance()
9858 imbalance = busiest->sum_nr_running; in sibling_imbalance()
9859 lsub_positive(&imbalance, local->sum_nr_running); in sibling_imbalance()
9864 imbalance = ncores_local * busiest->sum_nr_running; in sibling_imbalance()
9865 lsub_positive(&imbalance, ncores_busiest * local->sum_nr_running); in sibling_imbalance()
9871 if (imbalance <= 1 && local->sum_nr_running == 0 && in sibling_imbalance()
9872 busiest->sum_nr_running > 1) in sibling_imbalance()
9883 * takes care of cpu with reduced capacity in sched_reduced_capacity()
9885 if (rq->cfs.h_nr_running != 1) in sched_reduced_capacity()
9892 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
9894 * @sds: Load-balancing data with statistics of the local group.
9909 local_group = group == sds->local; in update_sg_lb_stats()
9911 for_each_cpu_and(i, sched_group_span(group), env->cpus) { in update_sg_lb_stats()
9915 sgs->group_load += load; in update_sg_lb_stats()
9916 sgs->group_util += cpu_util_cfs(i); in update_sg_lb_stats()
9917 sgs->group_runnable += cpu_runnable(rq); in update_sg_lb_stats()
9918 sgs->sum_h_nr_running += rq->cfs.h_nr_running; in update_sg_lb_stats()
9920 nr_running = rq->nr_running; in update_sg_lb_stats()
9921 sgs->sum_nr_running += nr_running; in update_sg_lb_stats()
9930 sgs->nr_numa_running += rq->nr_numa_running; in update_sg_lb_stats()
9931 sgs->nr_preferred_running += rq->nr_preferred_running; in update_sg_lb_stats()
9937 sgs->idle_cpus++; in update_sg_lb_stats()
9938 /* Idle cpu can't have misfit task */ in update_sg_lb_stats()
9945 if (env->sd->flags & SD_ASYM_CPUCAPACITY) { in update_sg_lb_stats()
9946 /* Check for a misfit task on the cpu */ in update_sg_lb_stats()
9947 if (sgs->group_misfit_task_load < rq->misfit_task_load) { in update_sg_lb_stats()
9948 sgs->group_misfit_task_load = rq->misfit_task_load; in update_sg_lb_stats()
9951 } else if ((env->idle != CPU_NOT_IDLE) && in update_sg_lb_stats()
9952 sched_reduced_capacity(rq, env->sd)) { in update_sg_lb_stats()
9953 /* Check for a task running on a CPU with reduced capacity */ in update_sg_lb_stats()
9954 if (sgs->group_misfit_task_load < load) in update_sg_lb_stats()
9955 sgs->group_misfit_task_load = load; in update_sg_lb_stats()
9959 sgs->group_capacity = group->sgc->capacity; in update_sg_lb_stats()
9961 sgs->group_weight = group->group_weight; in update_sg_lb_stats()
9963 /* Check if dst CPU is idle and preferred to this group */ in update_sg_lb_stats()
9964 if (!local_group && env->sd->flags & SD_ASYM_PACKING && in update_sg_lb_stats()
9965 env->idle != CPU_NOT_IDLE && sgs->sum_h_nr_running && in update_sg_lb_stats()
9967 sgs->group_asym_packing = 1; in update_sg_lb_stats()
9970 /* Check for loaded SMT group to be balanced to dst CPU */ in update_sg_lb_stats()
9972 sgs->group_smt_balance = 1; in update_sg_lb_stats()
9974 sgs->group_type = group_classify(env->sd->imbalance_pct, group, sgs); in update_sg_lb_stats()
9977 if (sgs->group_type == group_overloaded) in update_sg_lb_stats()
9978 sgs->avg_load = (sgs->group_load * SCHED_CAPACITY_SCALE) / in update_sg_lb_stats()
9979 sgs->group_capacity; in update_sg_lb_stats()
9983 * update_sd_pick_busiest - return 1 on busiest group
10000 struct sg_lb_stats *busiest = &sds->busiest_stat; in update_sd_pick_busiest()
10003 if (!sgs->sum_h_nr_running) in update_sd_pick_busiest()
10008 * We can use max_capacity here as reduction in capacity on some in update_sd_pick_busiest()
10012 if ((env->sd->flags & SD_ASYM_CPUCAPACITY) && in update_sd_pick_busiest()
10013 (sgs->group_type == group_misfit_task) && in update_sd_pick_busiest()
10014 (!capacity_greater(capacity_of(env->dst_cpu), sg->sgc->max_capacity) || in update_sd_pick_busiest()
10015 sds->local_stat.group_type != group_has_spare)) in update_sd_pick_busiest()
10018 if (sgs->group_type > busiest->group_type) in update_sd_pick_busiest()
10021 if (sgs->group_type < busiest->group_type) in update_sd_pick_busiest()
10029 switch (sgs->group_type) { in update_sd_pick_busiest()
10032 if (sgs->avg_load <= busiest->avg_load) in update_sd_pick_busiest()
10044 /* Prefer to move from lowest priority CPU's work */ in update_sd_pick_busiest()
10045 if (sched_asym_prefer(sg->asym_prefer_cpu, sds->busiest->asym_prefer_cpu)) in update_sd_pick_busiest()
10054 if (sgs->group_misfit_task_load < busiest->group_misfit_task_load) in update_sd_pick_busiest()
10063 if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) in update_sd_pick_busiest()
10072 * group because tasks have all compute capacity that they need in update_sd_pick_busiest()
10081 if (sgs->avg_load < busiest->avg_load) in update_sd_pick_busiest()
10084 if (sgs->avg_load == busiest->avg_load) { in update_sd_pick_busiest()
10086 * SMT sched groups need more help than non-SMT groups. in update_sd_pick_busiest()
10089 if (sds->busiest->flags & SD_SHARE_CPUCAPACITY) in update_sd_pick_busiest()
10101 if (smt_vs_nonsmt_groups(sds->busiest, sg)) { in update_sd_pick_busiest()
10102 if (sg->flags & SD_SHARE_CPUCAPACITY && sgs->sum_h_nr_running <= 1) in update_sd_pick_busiest()
10112 * the spare capacity which is more stable but it can end up in update_sd_pick_busiest()
10113 * that the group has less spare capacity but finally more idle in update_sd_pick_busiest()
10116 if (sgs->idle_cpus > busiest->idle_cpus) in update_sd_pick_busiest()
10118 else if ((sgs->idle_cpus == busiest->idle_cpus) && in update_sd_pick_busiest()
10119 (sgs->sum_nr_running <= busiest->sum_nr_running)) in update_sd_pick_busiest()
10126 * Candidate sg has no more than one task per CPU and has higher in update_sd_pick_busiest()
10127 * per-CPU capacity. Migrating tasks to less capable CPUs may harm in update_sd_pick_busiest()
10131 if ((env->sd->flags & SD_ASYM_CPUCAPACITY) && in update_sd_pick_busiest()
10132 (sgs->group_type <= group_fully_busy) && in update_sd_pick_busiest()
10133 (capacity_greater(sg->sgc->min_capacity, capacity_of(env->dst_cpu)))) in update_sd_pick_busiest()
10142 if (sgs->sum_h_nr_running > sgs->nr_numa_running) in fbq_classify_group()
10144 if (sgs->sum_h_nr_running > sgs->nr_preferred_running) in fbq_classify_group()
10151 if (rq->nr_running > rq->nr_numa_running) in fbq_classify_rq()
10153 if (rq->nr_running > rq->nr_preferred_running) in fbq_classify_rq()
10173 * task_running_on_cpu - return 1 if @p is running on @cpu.
10176 static unsigned int task_running_on_cpu(int cpu, struct task_struct *p) in task_running_on_cpu() argument
10179 if (cpu != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in task_running_on_cpu()
10189 * idle_cpu_without - would a given CPU be idle without p ?
10190 * @cpu: the processor on which idleness is tested.
10193 * Return: 1 if the CPU would be idle. 0 otherwise.
10195 static int idle_cpu_without(int cpu, struct task_struct *p) in idle_cpu_without() argument
10197 struct rq *rq = cpu_rq(cpu); in idle_cpu_without()
10199 if (rq->curr != rq->idle && rq->curr != p) in idle_cpu_without()
10203 * rq->nr_running can't be used but an updated version without the in idle_cpu_without()
10204 * impact of p on cpu must be used instead. The updated nr_running in idle_cpu_without()
10209 if (rq->ttwu_pending) in idle_cpu_without()
10217 * update_sg_wakeup_stats - Update sched_group's statistics for wakeup.
10221 * @p: The task for which we look for the idlest group/CPU.
10232 /* Assume that task can't fit any CPU of the group */ in update_sg_wakeup_stats()
10233 if (sd->flags & SD_ASYM_CPUCAPACITY) in update_sg_wakeup_stats()
10234 sgs->group_misfit_task_load = 1; in update_sg_wakeup_stats()
10240 sgs->group_load += cpu_load_without(rq, p); in update_sg_wakeup_stats()
10241 sgs->group_util += cpu_util_without(i, p); in update_sg_wakeup_stats()
10242 sgs->group_runnable += cpu_runnable_without(rq, p); in update_sg_wakeup_stats()
10244 sgs->sum_h_nr_running += rq->cfs.h_nr_running - local; in update_sg_wakeup_stats()
10246 nr_running = rq->nr_running - local; in update_sg_wakeup_stats()
10247 sgs->sum_nr_running += nr_running; in update_sg_wakeup_stats()
10253 sgs->idle_cpus++; in update_sg_wakeup_stats()
10255 /* Check if task fits in the CPU */ in update_sg_wakeup_stats()
10256 if (sd->flags & SD_ASYM_CPUCAPACITY && in update_sg_wakeup_stats()
10257 sgs->group_misfit_task_load && in update_sg_wakeup_stats()
10259 sgs->group_misfit_task_load = 0; in update_sg_wakeup_stats()
10263 sgs->group_capacity = group->sgc->capacity; in update_sg_wakeup_stats()
10265 sgs->group_weight = group->group_weight; in update_sg_wakeup_stats()
10267 sgs->group_type = group_classify(sd->imbalance_pct, group, sgs); in update_sg_wakeup_stats()
10273 if (sgs->group_type == group_fully_busy || in update_sg_wakeup_stats()
10274 sgs->group_type == group_overloaded) in update_sg_wakeup_stats()
10275 sgs->avg_load = (sgs->group_load * SCHED_CAPACITY_SCALE) / in update_sg_wakeup_stats()
10276 sgs->group_capacity; in update_sg_wakeup_stats()
10284 if (sgs->group_type < idlest_sgs->group_type) in update_pick_idlest()
10287 if (sgs->group_type > idlest_sgs->group_type) in update_pick_idlest()
10295 switch (sgs->group_type) { in update_pick_idlest()
10299 if (idlest_sgs->avg_load <= sgs->avg_load) in update_pick_idlest()
10310 /* Select group with the highest max capacity */ in update_pick_idlest()
10311 if (idlest->sgc->max_capacity >= group->sgc->max_capacity) in update_pick_idlest()
10317 if (idlest_sgs->idle_cpus > sgs->idle_cpus) in update_pick_idlest()
10321 if (idlest_sgs->idle_cpus == sgs->idle_cpus && in update_pick_idlest()
10322 idlest_sgs->group_util <= sgs->group_util) in update_pick_idlest()
10332 * find_idlest_group() finds and returns the least busy CPU group within the
10335 * Assumes p is allowed on at least one CPU in sd.
10340 struct sched_group *idlest = NULL, *local = NULL, *group = sd->groups; in find_idlest_group()
10354 p->cpus_ptr)) in find_idlest_group()
10378 } while (group = group->next, group != sd->groups); in find_idlest_group()
10385 /* The local group has been skipped because of CPU affinity */ in find_idlest_group()
10409 (sd->imbalance_pct-100) / 100; in find_idlest_group()
10416 * cross-domain, add imbalance to the load on the remote node in find_idlest_group()
10420 if ((sd->flags & SD_NUMA) && in find_idlest_group()
10431 if (100 * local_sgs.avg_load <= sd->imbalance_pct * idlest_sgs.avg_load) in find_idlest_group()
10442 /* Select group with the highest max capacity */ in find_idlest_group()
10443 if (local->sgc->max_capacity >= idlest->sgc->max_capacity) in find_idlest_group()
10449 if (sd->flags & SD_NUMA) { in find_idlest_group()
10450 int imb_numa_nr = sd->imb_numa_nr; in find_idlest_group()
10454 * If there is spare capacity at NUMA, try to select in find_idlest_group()
10457 if (cpu_to_node(this_cpu) == p->numa_preferred_nid) in find_idlest_group()
10461 if (cpu_to_node(idlest_cpu) == p->numa_preferred_nid) in find_idlest_group()
10473 if (p->nr_cpus_allowed != NR_CPUS) { in find_idlest_group()
10476 cpumask_and(cpus, sched_group_span(local), p->cpus_ptr); in find_idlest_group()
10477 imb_numa_nr = min(cpumask_weight(cpus), sd->imb_numa_nr); in find_idlest_group()
10480 imbalance = abs(local_sgs.idle_cpus - idlest_sgs.idle_cpus); in find_idlest_group()
10492 * up that the group has less spare capacity but finally more in find_idlest_group()
10517 if (!sched_feat(SIS_UTIL) || env->idle == CPU_NEWLY_IDLE) in update_idle_cpu_scan()
10520 llc_weight = per_cpu(sd_llc_size, env->dst_cpu); in update_idle_cpu_scan()
10521 if (env->sd->span_weight != llc_weight) in update_idle_cpu_scan()
10524 sd_share = rcu_dereference(per_cpu(sd_llc_shared, env->dst_cpu)); in update_idle_cpu_scan()
10534 * let y = SCHED_CAPACITY_SCALE - p * x^2 [1] in update_idle_cpu_scan()
10537 * x is the ratio of sum_util compared to the CPU capacity: in update_idle_cpu_scan()
10552 * y = SCHED_CAPACITY_SCALE - in update_idle_cpu_scan()
10561 pct = env->sd->imbalance_pct; in update_idle_cpu_scan()
10565 y = SCHED_CAPACITY_SCALE - tmp; in update_idle_cpu_scan()
10570 if ((int)y != sd_share->nr_idle_scan) in update_idle_cpu_scan()
10571 WRITE_ONCE(sd_share->nr_idle_scan, (int)y); in update_idle_cpu_scan()
10575 * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
10582 struct sched_group *sg = env->sd->groups; in update_sd_lb_stats()
10583 struct sg_lb_stats *local = &sds->local_stat; in update_sd_lb_stats()
10592 local_group = cpumask_test_cpu(env->dst_cpu, sched_group_span(sg)); in update_sd_lb_stats()
10594 sds->local = sg; in update_sd_lb_stats()
10597 if (env->idle != CPU_NEWLY_IDLE || in update_sd_lb_stats()
10598 time_after_eq(jiffies, sg->sgc->next_update)) in update_sd_lb_stats()
10599 update_group_capacity(env->sd, env->dst_cpu); in update_sd_lb_stats()
10609 sds->busiest = sg; in update_sd_lb_stats()
10610 sds->busiest_stat = *sgs; in update_sd_lb_stats()
10615 sds->total_load += sgs->group_load; in update_sd_lb_stats()
10616 sds->total_capacity += sgs->group_capacity; in update_sd_lb_stats()
10618 sum_util += sgs->group_util; in update_sd_lb_stats()
10619 sg = sg->next; in update_sd_lb_stats()
10620 } while (sg != env->sd->groups); in update_sd_lb_stats()
10627 if (sds->busiest) in update_sd_lb_stats()
10628 sds->prefer_sibling = !!(sds->busiest->flags & SD_PREFER_SIBLING); in update_sd_lb_stats()
10631 if (env->sd->flags & SD_NUMA) in update_sd_lb_stats()
10632 env->fbq_type = fbq_classify_group(&sds->busiest_stat); in update_sd_lb_stats()
10634 if (!env->sd->parent) { in update_sd_lb_stats()
10636 WRITE_ONCE(env->dst_rq->rd->overload, sg_status & SG_OVERLOAD); in update_sd_lb_stats()
10638 /* Update over-utilization (tipping point, U >= 0) indicator */ in update_sd_lb_stats()
10639 set_rd_overutilized_status(env->dst_rq->rd, in update_sd_lb_stats()
10642 set_rd_overutilized_status(env->dst_rq->rd, SG_OVERUTILIZED); in update_sd_lb_stats()
10649 * calculate_imbalance - Calculate the amount of imbalance present within the
10658 local = &sds->local_stat; in calculate_imbalance()
10659 busiest = &sds->busiest_stat; in calculate_imbalance()
10661 if (busiest->group_type == group_misfit_task) { in calculate_imbalance()
10662 if (env->sd->flags & SD_ASYM_CPUCAPACITY) { in calculate_imbalance()
10664 env->migration_type = migrate_misfit; in calculate_imbalance()
10665 env->imbalance = 1; in calculate_imbalance()
10668 * Set load imbalance to allow moving task from cpu in calculate_imbalance()
10669 * with reduced capacity. in calculate_imbalance()
10671 env->migration_type = migrate_load; in calculate_imbalance()
10672 env->imbalance = busiest->group_misfit_task_load; in calculate_imbalance()
10677 if (busiest->group_type == group_asym_packing) { in calculate_imbalance()
10679 * In case of asym capacity, we will try to migrate all load to in calculate_imbalance()
10680 * the preferred CPU. in calculate_imbalance()
10682 env->migration_type = migrate_task; in calculate_imbalance()
10683 env->imbalance = busiest->sum_h_nr_running; in calculate_imbalance()
10687 if (busiest->group_type == group_smt_balance) { in calculate_imbalance()
10688 /* Reduce number of tasks sharing CPU capacity */ in calculate_imbalance()
10689 env->migration_type = migrate_task; in calculate_imbalance()
10690 env->imbalance = 1; in calculate_imbalance()
10694 if (busiest->group_type == group_imbalanced) { in calculate_imbalance()
10696 * In the group_imb case we cannot rely on group-wide averages in calculate_imbalance()
10697 * to ensure CPU-load equilibrium, try to move any task to fix in calculate_imbalance()
10701 env->migration_type = migrate_task; in calculate_imbalance()
10702 env->imbalance = 1; in calculate_imbalance()
10707 * Try to use spare capacity of local group without overloading it or in calculate_imbalance()
10710 if (local->group_type == group_has_spare) { in calculate_imbalance()
10711 if ((busiest->group_type > group_fully_busy) && in calculate_imbalance()
10712 !(env->sd->flags & SD_SHARE_PKG_RESOURCES)) { in calculate_imbalance()
10715 * capacity. This might end up creating spare capacity in calculate_imbalance()
10721 env->migration_type = migrate_util; in calculate_imbalance()
10722 env->imbalance = max(local->group_capacity, local->group_util) - in calculate_imbalance()
10723 local->group_util; in calculate_imbalance()
10727 * higher than capacity because of migrations but the in calculate_imbalance()
10728 * local CPU is (newly) idle. There is at least one in calculate_imbalance()
10732 if (env->idle != CPU_NOT_IDLE && env->imbalance == 0) { in calculate_imbalance()
10733 env->migration_type = migrate_task; in calculate_imbalance()
10734 env->imbalance = 1; in calculate_imbalance()
10740 if (busiest->group_weight == 1 || sds->prefer_sibling) { in calculate_imbalance()
10745 env->migration_type = migrate_task; in calculate_imbalance()
10746 env->imbalance = sibling_imbalance(env, sds, busiest, local); in calculate_imbalance()
10753 env->migration_type = migrate_task; in calculate_imbalance()
10754 env->imbalance = max_t(long, 0, in calculate_imbalance()
10755 (local->idle_cpus - busiest->idle_cpus)); in calculate_imbalance()
10760 if (env->sd->flags & SD_NUMA) { in calculate_imbalance()
10761 env->imbalance = adjust_numa_imbalance(env->imbalance, in calculate_imbalance()
10762 local->sum_nr_running + 1, in calculate_imbalance()
10763 env->sd->imb_numa_nr); in calculate_imbalance()
10768 env->imbalance >>= 1; in calculate_imbalance()
10777 if (local->group_type < group_overloaded) { in calculate_imbalance()
10783 local->avg_load = (local->group_load * SCHED_CAPACITY_SCALE) / in calculate_imbalance()
10784 local->group_capacity; in calculate_imbalance()
10790 if (local->avg_load >= busiest->avg_load) { in calculate_imbalance()
10791 env->imbalance = 0; in calculate_imbalance()
10795 sds->avg_load = (sds->total_load * SCHED_CAPACITY_SCALE) / in calculate_imbalance()
10796 sds->total_capacity; in calculate_imbalance()
10802 if (local->avg_load >= sds->avg_load) { in calculate_imbalance()
10803 env->imbalance = 0; in calculate_imbalance()
10812 * above the average load, nor do we wish to reduce the max loaded CPU in calculate_imbalance()
10814 * reduce the group load below the group capacity. Thus we look for in calculate_imbalance()
10817 env->migration_type = migrate_load; in calculate_imbalance()
10818 env->imbalance = min( in calculate_imbalance()
10819 (busiest->avg_load - sds->avg_load) * busiest->group_capacity, in calculate_imbalance()
10820 (sds->avg_load - local->avg_load) * local->group_capacity in calculate_imbalance()
10847 * find_busiest_group - Returns the busiest group within the sched_domain
10854 * Return: - The busiest group if imbalance exists.
10876 if (busiest->group_type == group_misfit_task) in find_busiest_group()
10880 struct root_domain *rd = env->dst_rq->rd; in find_busiest_group()
10882 if (rcu_dereference(rd->pd) && !READ_ONCE(rd->overutilized)) in find_busiest_group()
10887 if (busiest->group_type == group_asym_packing) in find_busiest_group()
10895 if (busiest->group_type == group_imbalanced) in find_busiest_group()
10903 if (local->group_type > busiest->group_type) in find_busiest_group()
10910 if (local->group_type == group_overloaded) { in find_busiest_group()
10915 if (local->avg_load >= busiest->avg_load) in find_busiest_group()
10926 if (local->avg_load >= sds.avg_load) in find_busiest_group()
10933 if (100 * busiest->avg_load <= in find_busiest_group()
10934 env->sd->imbalance_pct * local->avg_load) in find_busiest_group()
10942 if (sds.prefer_sibling && local->group_type == group_has_spare && in find_busiest_group()
10946 if (busiest->group_type != group_overloaded) { in find_busiest_group()
10947 if (env->idle == CPU_NOT_IDLE) { in find_busiest_group()
10950 * result the local one too) but this CPU is already in find_busiest_group()
10951 * busy, let another idle CPU try to pull task. in find_busiest_group()
10956 if (busiest->group_type == group_smt_balance && in find_busiest_group()
10958 /* Let non SMT CPU pull from SMT CPU sharing with sibling */ in find_busiest_group()
10962 if (busiest->group_weight > 1 && in find_busiest_group()
10963 local->idle_cpus <= (busiest->idle_cpus + 1)) { in find_busiest_group()
10971 * there is more than 1 CPU per group. in find_busiest_group()
10976 if (busiest->sum_h_nr_running == 1) { in find_busiest_group()
10987 return env->imbalance ? sds.busiest : NULL; in find_busiest_group()
10990 env->imbalance = 0; in find_busiest_group()
10995 * find_busiest_queue - find the busiest runqueue among the CPUs in the group.
11005 for_each_cpu_and(i, sched_group_span(group), env->cpus) { in find_busiest_queue()
11006 unsigned long capacity, load, util; in find_busiest_queue() local
11015 * - regular: there are !numa tasks in find_busiest_queue()
11016 * - remote: there are numa tasks that run on the 'wrong' node in find_busiest_queue()
11017 * - all: there is no distinction in find_busiest_queue()
11032 if (rt > env->fbq_type) in find_busiest_queue()
11035 nr_running = rq->cfs.h_nr_running; in find_busiest_queue()
11039 capacity = capacity_of(i); in find_busiest_queue()
11042 * For ASYM_CPUCAPACITY domains, don't pick a CPU that could in find_busiest_queue()
11043 * eventually lead to active_balancing high->low capacity. in find_busiest_queue()
11044 * Higher per-CPU capacity is considered better than balancing in find_busiest_queue()
11047 if (env->sd->flags & SD_ASYM_CPUCAPACITY && in find_busiest_queue()
11048 !capacity_greater(capacity_of(env->dst_cpu), capacity) && in find_busiest_queue()
11053 * Make sure we only pull tasks from a CPU of lower priority in find_busiest_queue()
11059 if ((env->sd->flags & SD_ASYM_PACKING) && in find_busiest_queue()
11060 sched_use_asym_prio(env->sd, i) && in find_busiest_queue()
11061 sched_asym_prefer(i, env->dst_cpu) && in find_busiest_queue()
11065 switch (env->migration_type) { in find_busiest_queue()
11069 * which is not scaled with the CPU capacity. in find_busiest_queue()
11073 if (nr_running == 1 && load > env->imbalance && in find_busiest_queue()
11074 !check_cpu_capacity(rq, env->sd)) in find_busiest_queue()
11079 * consider the cpu_load() scaled with the CPU in find_busiest_queue()
11080 * capacity, so that the load can be moved away in find_busiest_queue()
11081 * from the CPU that is potentially running at a in find_busiest_queue()
11082 * lower capacity. in find_busiest_queue()
11090 if (load * busiest_capacity > busiest_load * capacity) { in find_busiest_queue()
11092 busiest_capacity = capacity; in find_busiest_queue()
11101 * Don't try to pull utilization from a CPU with one in find_busiest_queue()
11126 if (rq->misfit_task_load > busiest_load) { in find_busiest_queue()
11127 busiest_load = rq->misfit_task_load; in find_busiest_queue()
11156 * CPU priority. in asym_active_balance()
11158 return env->idle != CPU_NOT_IDLE && (env->sd->flags & SD_ASYM_PACKING) && in asym_active_balance()
11159 sched_use_asym_prio(env->sd, env->dst_cpu) && in asym_active_balance()
11160 (sched_asym_prefer(env->dst_cpu, env->src_cpu) || in asym_active_balance()
11161 !sched_use_asym_prio(env->sd, env->src_cpu)); in asym_active_balance()
11167 struct sched_domain *sd = env->sd; in imbalanced_active_balance()
11172 * threads on a system with spare capacity in imbalanced_active_balance()
11174 if ((env->migration_type == migrate_task) && in imbalanced_active_balance()
11175 (sd->nr_balance_failed > sd->cache_nice_tries+2)) in imbalanced_active_balance()
11183 struct sched_domain *sd = env->sd; in need_active_balance()
11192 * The dst_cpu is idle and the src_cpu CPU has only 1 CFS task. in need_active_balance()
11193 * It's worth migrating the task if the src_cpu's capacity is reduced in need_active_balance()
11194 * because of other sched_class or IRQs if more capacity stays in need_active_balance()
11197 if ((env->idle != CPU_NOT_IDLE) && in need_active_balance()
11198 (env->src_rq->cfs.h_nr_running == 1)) { in need_active_balance()
11199 if ((check_cpu_capacity(env->src_rq, sd)) && in need_active_balance()
11200 (capacity_of(env->src_cpu)*sd->imbalance_pct < capacity_of(env->dst_cpu)*100)) in need_active_balance()
11204 if (env->migration_type == migrate_misfit) in need_active_balance()
11215 struct sched_group *sg = env->sd->groups; in should_we_balance()
11216 int cpu, idle_smt = -1; in should_we_balance() local
11222 if (!cpumask_test_cpu(env->dst_cpu, env->cpus)) in should_we_balance()
11232 if (env->idle == CPU_NEWLY_IDLE) { in should_we_balance()
11233 if (env->dst_rq->nr_running > 0 || env->dst_rq->ttwu_pending) in should_we_balance()
11239 /* Try to find first idle CPU */ in should_we_balance()
11240 for_each_cpu_and(cpu, swb_cpus, env->cpus) { in should_we_balance()
11241 if (!idle_cpu(cpu)) in should_we_balance()
11246 * balancing cores, but remember the first idle SMT CPU for in should_we_balance()
11247 * later consideration. Find CPU on an idle core first. in should_we_balance()
11249 if (!(env->sd->flags & SD_SHARE_CPUCAPACITY) && !is_core_idle(cpu)) { in should_we_balance()
11250 if (idle_smt == -1) in should_we_balance()
11251 idle_smt = cpu; in should_we_balance()
11258 cpumask_andnot(swb_cpus, swb_cpus, cpu_smt_mask(cpu)); in should_we_balance()
11264 * Are we the first idle core in a non-SMT domain or higher, in should_we_balance()
11265 * or the first idle CPU in a SMT domain? in should_we_balance()
11267 return cpu == env->dst_cpu; in should_we_balance()
11270 /* Are we the first idle CPU with busy siblings? */ in should_we_balance()
11271 if (idle_smt != -1) in should_we_balance()
11272 return idle_smt == env->dst_cpu; in should_we_balance()
11274 /* Are we the first CPU of this group ? */ in should_we_balance()
11275 return group_balance_cpu(sg) == env->dst_cpu; in should_we_balance()
11287 struct sched_domain *sd_parent = sd->parent; in load_balance()
11296 .dst_grpmask = group_balance_mask(sd->groups), in load_balance()
11306 schedstat_inc(sd->lb_count[idle]); in load_balance()
11316 schedstat_inc(sd->lb_nobusyg[idle]); in load_balance()
11322 schedstat_inc(sd->lb_nobusyq[idle]); in load_balance()
11328 schedstat_add(sd->lb_imbalance[idle], env.imbalance); in load_balance()
11330 env.src_cpu = busiest->cpu; in load_balance()
11336 if (busiest->nr_running > 1) { in load_balance()
11339 * an imbalance but busiest->nr_running <= 1, the group is in load_balance()
11343 env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running); in load_balance()
11350 * cur_ld_moved - load moved in current iteration in load_balance()
11351 * ld_moved - cumulative load moved across iterations in load_balance()
11358 * unlock busiest->lock, and we are able to be sure in load_balance()
11387 * nohz-idle), we now have balance_cpu in a position to move in load_balance()
11398 /* Prevent to re-select dst_cpu via env's CPUs */ in load_balance()
11418 int *group_imbalance = &sd_parent->groups->sgc->imbalance; in load_balance()
11424 /* All tasks on this runqueue were pinned by CPU affinity */ in load_balance()
11445 schedstat_inc(sd->lb_failed[idle]); in load_balance()
11453 sd->nr_balance_failed++; in load_balance()
11462 * if the curr task on busiest CPU can't be in load_balance()
11465 if (!cpumask_test_cpu(this_cpu, busiest->curr->cpus_ptr)) { in load_balance()
11474 * ->active_balance synchronizes accesses to in load_balance()
11475 * ->active_balance_work. Once set, it's cleared in load_balance()
11478 if (!busiest->active_balance) { in load_balance()
11479 busiest->active_balance = 1; in load_balance()
11480 busiest->push_cpu = this_cpu; in load_balance()
11489 &busiest->active_balance_work); in load_balance()
11494 sd->nr_balance_failed = 0; in load_balance()
11499 sd->balance_interval = sd->min_interval; in load_balance()
11511 int *group_imbalance = &sd_parent->groups->sgc->imbalance; in load_balance()
11523 schedstat_inc(sd->lb_balanced[idle]); in load_balance()
11525 sd->nr_balance_failed = 0; in load_balance()
11541 sd->balance_interval < MAX_PINNED_INTERVAL) || in load_balance()
11542 sd->balance_interval < sd->max_interval) in load_balance()
11543 sd->balance_interval *= 2; in load_balance()
11551 unsigned long interval = sd->balance_interval; in get_sd_balance_interval()
11554 interval *= sd->busy_factor; in get_sd_balance_interval()
11565 interval -= 1; in get_sd_balance_interval()
11579 next = sd->last_balance + interval; in update_next_balance()
11586 * active_load_balance_cpu_stop is run by the CPU stopper. It pushes
11587 * running tasks off the busiest CPU onto idle CPUs. It requires at
11588 * least 1 task to be running on each physical CPU where possible, and
11595 int target_cpu = busiest_rq->push_cpu; in active_load_balance_cpu_stop()
11603 * Between queueing the stop-work and running it is a hole in which in active_load_balance_cpu_stop()
11610 /* Make sure the requested CPU hasn't gone down in the meantime: */ in active_load_balance_cpu_stop()
11612 !busiest_rq->active_balance)) in active_load_balance_cpu_stop()
11616 if (busiest_rq->nr_running <= 1) in active_load_balance_cpu_stop()
11622 * Bjorn Helgaas on a 128-CPU setup. in active_load_balance_cpu_stop()
11626 /* Search for an sd spanning us and the target CPU. */ in active_load_balance_cpu_stop()
11638 .src_cpu = busiest_rq->cpu, in active_load_balance_cpu_stop()
11644 schedstat_inc(sd->alb_count); in active_load_balance_cpu_stop()
11649 schedstat_inc(sd->alb_pushed); in active_load_balance_cpu_stop()
11651 sd->nr_balance_failed = 0; in active_load_balance_cpu_stop()
11653 schedstat_inc(sd->alb_failed); in active_load_balance_cpu_stop()
11658 busiest_rq->active_balance = 0; in active_load_balance_cpu_stop()
11673 * This trades load-balance latency on larger machines for less cross talk.
11682 if (cost > sd->max_newidle_lb_cost) { in update_newidle_cost()
11685 * next wakeup on the CPU. in update_newidle_cost()
11687 sd->max_newidle_lb_cost = cost; in update_newidle_cost()
11688 sd->last_decay_max_lb_cost = jiffies; in update_newidle_cost()
11689 } else if (time_after(jiffies, sd->last_decay_max_lb_cost + HZ)) { in update_newidle_cost()
11695 sd->max_newidle_lb_cost = (sd->max_newidle_lb_cost * 253) / 256; in update_newidle_cost()
11696 sd->last_decay_max_lb_cost = jiffies; in update_newidle_cost()
11713 int cpu = rq->cpu; in rebalance_domains() local
11714 int busy = idle != CPU_IDLE && !sched_idle_cpu(cpu); in rebalance_domains()
11724 for_each_domain(cpu, sd) { in rebalance_domains()
11730 max_cost += sd->max_newidle_lb_cost; in rebalance_domains()
11734 * CPU in our sched group which is doing load balancing more in rebalance_domains()
11745 need_serialize = sd->flags & SD_SERIALIZE; in rebalance_domains()
11751 if (time_after_eq(jiffies, sd->last_balance + interval)) { in rebalance_domains()
11752 if (load_balance(cpu, rq, sd, idle, &continue_balancing)) { in rebalance_domains()
11755 * env->dst_cpu, so we can't know our idle in rebalance_domains()
11758 idle = idle_cpu(cpu) ? CPU_IDLE : CPU_NOT_IDLE; in rebalance_domains()
11759 busy = idle != CPU_IDLE && !sched_idle_cpu(cpu); in rebalance_domains()
11761 sd->last_balance = jiffies; in rebalance_domains()
11767 if (time_after(next_balance, sd->last_balance + interval)) { in rebalance_domains()
11768 next_balance = sd->last_balance + interval; in rebalance_domains()
11774 * Ensure the rq-wide value also decays but keep it at a in rebalance_domains()
11775 * reasonable floor to avoid funnies with rq->avg_idle. in rebalance_domains()
11777 rq->max_idle_balance_cost = in rebalance_domains()
11784 * When the cpu is attached to null domain for ex, it will not be in rebalance_domains()
11788 rq->next_balance = next_balance; in rebalance_domains()
11794 return unlikely(!rcu_dereference_sched(rq->sd)); in on_null_domain()
11800 * - When one of the busy CPUs notice that there may be an idle rebalancing
11803 * - HK_TYPE_MISC CPUs are used for this task, because HK_TYPE_SCHED not set
11827 * Kick a CPU to do the nohz balancing, if it is time for it. We pick any
11828 * idle CPU in the HK_TYPE_MISC housekeeping set (if there is one).
11855 * This way we generate an IPI on the target CPU which in kick_ilb()
11859 smp_call_function_single_async(ilb_cpu, &cpu_rq(ilb_cpu)->nohz_csd); in kick_ilb()
11871 int nr_busy, i, cpu = rq->cpu; in nohz_balancer_kick() local
11874 if (unlikely(rq->idle_balance)) in nohz_balancer_kick()
11897 if (rq->nr_running >= 2) { in nohz_balancer_kick()
11904 sd = rcu_dereference(rq->sd); in nohz_balancer_kick()
11907 * If there's a CFS task and the current CPU has reduced in nohz_balancer_kick()
11908 * capacity; kick the ILB to see if there's a better CPU to run in nohz_balancer_kick()
11911 if (rq->cfs.h_nr_running >= 1 && check_cpu_capacity(rq, sd)) { in nohz_balancer_kick()
11917 sd = rcu_dereference(per_cpu(sd_asym_packing, cpu)); in nohz_balancer_kick()
11920 * When ASYM_PACKING; see if there's a more preferred CPU in nohz_balancer_kick()
11925 * preferred CPU must be idle. in nohz_balancer_kick()
11929 sched_asym_prefer(i, cpu)) { in nohz_balancer_kick()
11936 sd = rcu_dereference(per_cpu(sd_asym_cpucapacity, cpu)); in nohz_balancer_kick()
11939 * When ASYM_CPUCAPACITY; see if there's a higher capacity CPU in nohz_balancer_kick()
11950 * ensure tasks have enough CPU capacity. in nohz_balancer_kick()
11957 sds = rcu_dereference(per_cpu(sd_llc_shared, cpu)); in nohz_balancer_kick()
11961 * increase the overall cache use), we need some less-loaded LLC in nohz_balancer_kick()
11965 * the others are - so just get a nohz balance going if it looks in nohz_balancer_kick()
11968 nr_busy = atomic_read(&sds->nr_busy_cpus); in nohz_balancer_kick()
11984 static void set_cpu_sd_state_busy(int cpu) in set_cpu_sd_state_busy() argument
11989 sd = rcu_dereference(per_cpu(sd_llc, cpu)); in set_cpu_sd_state_busy()
11991 if (!sd || !sd->nohz_idle) in set_cpu_sd_state_busy()
11993 sd->nohz_idle = 0; in set_cpu_sd_state_busy()
11995 atomic_inc(&sd->shared->nr_busy_cpus); in set_cpu_sd_state_busy()
12004 if (likely(!rq->nohz_tick_stopped)) in nohz_balance_exit_idle()
12007 rq->nohz_tick_stopped = 0; in nohz_balance_exit_idle()
12008 cpumask_clear_cpu(rq->cpu, nohz.idle_cpus_mask); in nohz_balance_exit_idle()
12011 set_cpu_sd_state_busy(rq->cpu); in nohz_balance_exit_idle()
12014 static void set_cpu_sd_state_idle(int cpu) in set_cpu_sd_state_idle() argument
12019 sd = rcu_dereference(per_cpu(sd_llc, cpu)); in set_cpu_sd_state_idle()
12021 if (!sd || sd->nohz_idle) in set_cpu_sd_state_idle()
12023 sd->nohz_idle = 1; in set_cpu_sd_state_idle()
12025 atomic_dec(&sd->shared->nr_busy_cpus); in set_cpu_sd_state_idle()
12031 * This routine will record that the CPU is going idle with tick stopped.
12034 void nohz_balance_enter_idle(int cpu) in nohz_balance_enter_idle() argument
12036 struct rq *rq = cpu_rq(cpu); in nohz_balance_enter_idle()
12038 SCHED_WARN_ON(cpu != smp_processor_id()); in nohz_balance_enter_idle()
12040 /* If this CPU is going down, then nothing needs to be done: */ in nohz_balance_enter_idle()
12041 if (!cpu_active(cpu)) in nohz_balance_enter_idle()
12045 if (!housekeeping_cpu(cpu, HK_TYPE_SCHED)) in nohz_balance_enter_idle()
12049 * Can be set safely without rq->lock held in nohz_balance_enter_idle()
12051 * rq->lock is held during the check and the clear in nohz_balance_enter_idle()
12053 rq->has_blocked_load = 1; in nohz_balance_enter_idle()
12058 * *_avg. The CPU is already part of nohz.idle_cpus_mask so the clear in nohz_balance_enter_idle()
12061 if (rq->nohz_tick_stopped) in nohz_balance_enter_idle()
12064 /* If we're a completely isolated CPU, we don't play: */ in nohz_balance_enter_idle()
12068 rq->nohz_tick_stopped = 1; in nohz_balance_enter_idle()
12070 cpumask_set_cpu(cpu, nohz.idle_cpus_mask); in nohz_balance_enter_idle()
12080 set_cpu_sd_state_idle(cpu); in nohz_balance_enter_idle()
12085 * Each time a cpu enter idle, we assume that it has blocked load and in nohz_balance_enter_idle()
12093 unsigned int cpu = rq->cpu; in update_nohz_stats() local
12095 if (!rq->has_blocked_load) in update_nohz_stats()
12098 if (!cpumask_test_cpu(cpu, nohz.idle_cpus_mask)) in update_nohz_stats()
12101 if (!time_after(jiffies, READ_ONCE(rq->last_blocked_load_update_tick))) in update_nohz_stats()
12104 update_blocked_averages(cpu); in update_nohz_stats()
12106 return rq->has_blocked_load; in update_nohz_stats()
12121 int this_cpu = this_rq->cpu; in _nohz_idle_balance()
12129 * the has_blocked flag. If a cpu enters idle in the mean time, it will in _nohz_idle_balance()
12131 * Because a cpu that becomes idle, is added to idle_cpus_mask before in _nohz_idle_balance()
12133 * check the load of an idle cpu. in _nohz_idle_balance()
12143 * Ensures that if we miss the CPU, we must see the has_blocked in _nohz_idle_balance()
12149 * Start with the next CPU after this_cpu so we will end with this_cpu and let a in _nohz_idle_balance()
12150 * chance for other idle cpu to pull load. in _nohz_idle_balance()
12157 * If this CPU gets work to do, stop the load balancing in _nohz_idle_balance()
12178 if (time_after_eq(jiffies, rq->next_balance)) { in _nohz_idle_balance()
12189 if (time_after(next_balance, rq->next_balance)) { in _nohz_idle_balance()
12190 next_balance = rq->next_balance; in _nohz_idle_balance()
12197 * When the CPU is attached to null domain for ex, it will not be in _nohz_idle_balance()
12219 unsigned int flags = this_rq->nohz_idle_balance; in nohz_idle_balance()
12224 this_rq->nohz_idle_balance = 0; in nohz_idle_balance()
12238 void nohz_run_idle_balance(int cpu) in nohz_run_idle_balance() argument
12242 flags = atomic_fetch_andnot(NOHZ_NEWILB_KICK, nohz_flags(cpu)); in nohz_run_idle_balance()
12249 _nohz_idle_balance(cpu_rq(cpu), NOHZ_STATS_KICK); in nohz_run_idle_balance()
12254 int this_cpu = this_rq->cpu; in nohz_newidle_balance()
12257 * This CPU doesn't want to be disturbed by scheduler in nohz_newidle_balance()
12264 if (this_rq->avg_idle < sysctl_sched_migration_cost) in nohz_newidle_balance()
12295 * < 0 - we released the lock and there are !fair tasks present
12296 * 0 - failed, no new tasks
12297 * > 0 - success, new (fair) tasks present
12302 int this_cpu = this_rq->cpu; in newidle_balance()
12313 if (this_rq->ttwu_pending) in newidle_balance()
12320 this_rq->idle_stamp = rq_clock(this_rq); in newidle_balance()
12330 * for load-balance and preemption/IRQs are still disabled avoiding in newidle_balance()
12332 * re-start the picking loop. in newidle_balance()
12337 sd = rcu_dereference_check_sched_domain(this_rq->sd); in newidle_balance()
12339 if (!READ_ONCE(this_rq->rd->overload) || in newidle_balance()
12340 (sd && this_rq->avg_idle < sd->max_newidle_lb_cost)) { in newidle_balance()
12362 if (this_rq->avg_idle < curr_cost + sd->max_newidle_lb_cost) in newidle_balance()
12365 if (sd->flags & SD_BALANCE_NEWIDLE) { in newidle_balance()
12372 domain_cost = t1 - t0; in newidle_balance()
12383 if (pulled_task || this_rq->nr_running > 0 || in newidle_balance()
12384 this_rq->ttwu_pending) in newidle_balance()
12391 if (curr_cost > this_rq->max_idle_balance_cost) in newidle_balance()
12392 this_rq->max_idle_balance_cost = curr_cost; in newidle_balance()
12399 if (this_rq->cfs.h_nr_running && !pulled_task) in newidle_balance()
12403 if (this_rq->nr_running != this_rq->cfs.h_nr_running) in newidle_balance()
12404 pulled_task = -1; in newidle_balance()
12408 if (time_after(this_rq->next_balance, next_balance)) in newidle_balance()
12409 this_rq->next_balance = next_balance; in newidle_balance()
12412 this_rq->idle_stamp = 0; in newidle_balance()
12428 enum cpu_idle_type idle = this_rq->idle_balance ? in run_rebalance_domains()
12432 * If this CPU has a pending nohz_balance_kick, then do the in run_rebalance_domains()
12443 update_blocked_averages(this_rq->cpu); in run_rebalance_domains()
12454 * runqueue CPU is not active in trigger_load_balance()
12459 if (time_after_eq(jiffies, rq->next_balance)) in trigger_load_balance()
12486 u64 rtime = se->sum_exec_runtime - se->prev_sum_exec_runtime; in __entity_slice_used()
12487 u64 slice = se->slice; in __entity_slice_used()
12506 * tasks on this CPU and the forced idle CPU. Ideally, we should in task_tick_core()
12508 * We can assume that the forced idle CPU has at least in task_tick_core()
12509 * MIN_NR_TASKS_DURING_FORCEIDLE - 1 tasks and use that to check in task_tick_core()
12510 * if we need to give up the CPU. in task_tick_core()
12512 if (rq->core->core_forceidle_count && rq->cfs.nr_running == 1 && in task_tick_core()
12513 __entity_slice_used(&curr->se, MIN_NR_TASKS_DURING_FORCEIDLE)) in task_tick_core()
12518 * se_fi_update - Update the cfs_rq->min_vruntime_fi in a CFS hierarchy if needed.
12527 if (cfs_rq->forceidle_seq == fi_seq) in se_fi_update()
12529 cfs_rq->forceidle_seq = fi_seq; in se_fi_update()
12532 cfs_rq->min_vruntime_fi = cfs_rq->min_vruntime; in se_fi_update()
12538 struct sched_entity *se = &p->se; in task_vruntime_update()
12540 if (p->sched_class != &fair_sched_class) in task_vruntime_update()
12543 se_fi_update(se, rq->core->core_forceidle_seq, in_fi); in task_vruntime_update()
12550 const struct sched_entity *sea = &a->se; in cfs_prio_less()
12551 const struct sched_entity *seb = &b->se; in cfs_prio_less()
12556 SCHED_WARN_ON(task_rq(b)->core != rq->core); in cfs_prio_less()
12563 while (sea->cfs_rq->tg != seb->cfs_rq->tg) { in cfs_prio_less()
12564 int sea_depth = sea->depth; in cfs_prio_less()
12565 int seb_depth = seb->depth; in cfs_prio_less()
12573 se_fi_update(sea, rq->core->core_forceidle_seq, in_fi); in cfs_prio_less()
12574 se_fi_update(seb, rq->core->core_forceidle_seq, in_fi); in cfs_prio_less()
12576 cfs_rqa = sea->cfs_rq; in cfs_prio_less()
12577 cfs_rqb = seb->cfs_rq; in cfs_prio_less()
12579 cfs_rqa = &task_rq(a)->cfs; in cfs_prio_less()
12580 cfs_rqb = &task_rq(b)->cfs; in cfs_prio_less()
12588 delta = (s64)(sea->vruntime - seb->vruntime) + in cfs_prio_less()
12589 (s64)(cfs_rqb->min_vruntime_fi - cfs_rqa->min_vruntime_fi); in cfs_prio_less()
12594 static int task_is_throttled_fair(struct task_struct *p, int cpu) in task_is_throttled_fair() argument
12599 cfs_rq = task_group(p)->cfs_rq[cpu]; in task_is_throttled_fair()
12601 cfs_rq = &cpu_rq(cpu)->cfs; in task_is_throttled_fair()
12620 struct sched_entity *se = &curr->se; in task_tick_fair()
12638 * - child not yet on the tasklist
12639 * - preemption disabled
12643 struct sched_entity *se = &p->se, *curr; in task_fork_fair()
12652 curr = cfs_rq->curr; in task_fork_fair()
12669 if (rq->cfs.nr_running == 1) in prio_changed_fair()
12678 if (p->prio > oldprio) in prio_changed_fair()
12700 se = se->parent; in propagate_entity_cfs_rq()
12725 * - A forked task which hasn't been woken up by wake_up_new_task(). in detach_entity_cfs_rq()
12726 * - A task which has been woken up by try_to_wake_up() but is in detach_entity_cfs_rq()
12729 if (!se->avg.last_update_time) in detach_entity_cfs_rq()
12753 struct sched_entity *se = &p->se; in detach_task_cfs_rq()
12760 struct sched_entity *se = &p->se; in attach_task_cfs_rq()
12789 * This routine is mostly called to set cfs_rq->curr field when a task
12794 struct sched_entity *se = &p->se; in set_next_task_fair()
12802 list_move(&se->group_node, &rq->cfs_tasks); in set_next_task_fair()
12817 cfs_rq->tasks_timeline = RB_ROOT_CACHED; in init_cfs_rq()
12818 u64_u32_store(cfs_rq->min_vruntime, (u64)(-(1LL << 20))); in init_cfs_rq()
12820 raw_spin_lock_init(&cfs_rq->removed.lock); in init_cfs_rq()
12831 if (READ_ONCE(p->__state) == TASK_NEW) in task_change_group_fair()
12837 /* Tell se's cfs_rq has been changed -- migrated */ in task_change_group_fair()
12838 p->se.avg.last_update_time = 0; in task_change_group_fair()
12849 if (tg->cfs_rq) in free_fair_sched_group()
12850 kfree(tg->cfs_rq[i]); in free_fair_sched_group()
12851 if (tg->se) in free_fair_sched_group()
12852 kfree(tg->se[i]); in free_fair_sched_group()
12855 kfree(tg->cfs_rq); in free_fair_sched_group()
12856 kfree(tg->se); in free_fair_sched_group()
12865 tg->cfs_rq = kcalloc(nr_cpu_ids, sizeof(cfs_rq), GFP_KERNEL); in alloc_fair_sched_group()
12866 if (!tg->cfs_rq) in alloc_fair_sched_group()
12868 tg->se = kcalloc(nr_cpu_ids, sizeof(se), GFP_KERNEL); in alloc_fair_sched_group()
12869 if (!tg->se) in alloc_fair_sched_group()
12872 tg->shares = NICE_0_LOAD; in alloc_fair_sched_group()
12888 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]); in alloc_fair_sched_group()
12909 se = tg->se[i]; in online_fair_sched_group()
12922 int cpu; in unregister_fair_sched_group() local
12926 for_each_possible_cpu(cpu) { in unregister_fair_sched_group()
12927 if (tg->se[cpu]) in unregister_fair_sched_group()
12928 remove_entity_load_avg(tg->se[cpu]); in unregister_fair_sched_group()
12932 * check on_list without danger of it being re-added. in unregister_fair_sched_group()
12934 if (!tg->cfs_rq[cpu]->on_list) in unregister_fair_sched_group()
12937 rq = cpu_rq(cpu); in unregister_fair_sched_group()
12940 list_del_leaf_cfs_rq(tg->cfs_rq[cpu]); in unregister_fair_sched_group()
12946 struct sched_entity *se, int cpu, in init_tg_cfs_entry() argument
12949 struct rq *rq = cpu_rq(cpu); in init_tg_cfs_entry()
12951 cfs_rq->tg = tg; in init_tg_cfs_entry()
12952 cfs_rq->rq = rq; in init_tg_cfs_entry()
12955 tg->cfs_rq[cpu] = cfs_rq; in init_tg_cfs_entry()
12956 tg->se[cpu] = se; in init_tg_cfs_entry()
12963 se->cfs_rq = &rq->cfs; in init_tg_cfs_entry()
12964 se->depth = 0; in init_tg_cfs_entry()
12966 se->cfs_rq = parent->my_q; in init_tg_cfs_entry()
12967 se->depth = parent->depth + 1; in init_tg_cfs_entry()
12970 se->my_q = cfs_rq; in init_tg_cfs_entry()
12972 update_load_set(&se->load, NICE_0_LOAD); in init_tg_cfs_entry()
12973 se->parent = parent; in init_tg_cfs_entry()
12987 if (!tg->se[0]) in __sched_group_set_shares()
12988 return -EINVAL; in __sched_group_set_shares()
12992 if (tg->shares == shares) in __sched_group_set_shares()
12995 tg->shares = shares; in __sched_group_set_shares()
12998 struct sched_entity *se = tg->se[i]; in __sched_group_set_shares()
13020 ret = -EINVAL; in sched_group_set_shares()
13033 return -EINVAL; in sched_group_set_idle()
13036 return -EINVAL; in sched_group_set_idle()
13040 if (tg->idle == idle) { in sched_group_set_idle()
13045 tg->idle = idle; in sched_group_set_idle()
13049 struct sched_entity *se = tg->se[i]; in sched_group_set_idle()
13050 struct cfs_rq *parent_cfs_rq, *grp_cfs_rq = tg->cfs_rq[i]; in sched_group_set_idle()
13057 grp_cfs_rq->idle = idle; in sched_group_set_idle()
13061 if (se->on_rq) { in sched_group_set_idle()
13064 parent_cfs_rq->idle_nr_running++; in sched_group_set_idle()
13066 parent_cfs_rq->idle_nr_running--; in sched_group_set_idle()
13069 idle_task_delta = grp_cfs_rq->h_nr_running - in sched_group_set_idle()
13070 grp_cfs_rq->idle_h_nr_running; in sched_group_set_idle()
13072 idle_task_delta *= -1; in sched_group_set_idle()
13077 if (!se->on_rq) in sched_group_set_idle()
13080 cfs_rq->idle_h_nr_running += idle_task_delta; in sched_group_set_idle()
13119 struct sched_entity *se = &task->se; in get_rr_interval_fair()
13126 if (rq->cfs.load.weight) in get_rr_interval_fair()
13127 rr_interval = NS_TO_JIFFIES(se->slice); in get_rr_interval_fair()
13186 void print_cfs_stats(struct seq_file *m, int cpu) in print_cfs_stats() argument
13191 for_each_leaf_cfs_rq_safe(cpu_rq(cpu), cfs_rq, pos) in print_cfs_stats()
13192 print_cfs_rq(m, cpu, cfs_rq); in print_cfs_stats()
13204 ng = rcu_dereference(p->numa_group); in show_numa_stats()
13206 if (p->numa_faults) { in show_numa_stats()
13207 tsf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 0)]; in show_numa_stats()
13208 tpf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 1)]; in show_numa_stats()
13211 gsf = ng->faults[task_faults_idx(NUMA_MEM, node, 0)], in show_numa_stats()
13212 gpf = ng->faults[task_faults_idx(NUMA_MEM, node, 1)]; in show_numa_stats()
13233 INIT_CSD(&cpu_rq(i)->cfsb_csd, __cfsb_csd_unthrottle, cpu_rq(i)); in init_sched_fair_class()
13234 INIT_LIST_HEAD(&cpu_rq(i)->cfsb_csd_list); in init_sched_fair_class()