Lines Matching +full:differential +full:- +full:pair
1 // SPDX-License-Identifier: GPL-2.0
43 #include <linux/memory-tiers.h>
59 * The initial- and re-scaling of tunables is configurable
63 * SCHED_TUNABLESCALING_NONE - unscaled, always *1
64 * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus)
65 * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus
72 * Minimal preemption granularity for CPU-bound tasks:
100 return -cpu; in arch_asym_cpu_priority()
121 * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool
173 lw->weight += inc; in update_load_add()
174 lw->inv_weight = 0; in update_load_add()
179 lw->weight -= dec; in update_load_sub()
180 lw->inv_weight = 0; in update_load_sub()
185 lw->weight = w; in update_load_set()
186 lw->inv_weight = 0; in update_load_set()
193 * so pick a second-best guess by going with the log2 of the
241 if (likely(lw->inv_weight)) in __update_inv_weight()
244 w = scale_load_down(lw->weight); in __update_inv_weight()
247 lw->inv_weight = 1; in __update_inv_weight()
249 lw->inv_weight = WMULT_CONST; in __update_inv_weight()
251 lw->inv_weight = WMULT_CONST / w; in __update_inv_weight()
257 * (delta_exec * (weight * lw->inv_weight)) >> WMULT_SHIFT
277 shift -= fs; in __calc_delta()
281 fact = mul_u32_u32(fact, lw->inv_weight); in __calc_delta()
286 shift -= fs; in __calc_delta()
298 if (unlikely(se->load.weight != NICE_0_LOAD)) in calc_delta_fair()
299 delta = __calc_delta(delta, NICE_0_LOAD, &se->load); in calc_delta_fair()
314 for (; se; se = se->parent)
321 if (cfs_rq->on_list) in list_add_leaf_cfs_rq()
322 return rq->tmp_alone_branch == &rq->leaf_cfs_rq_list; in list_add_leaf_cfs_rq()
324 cfs_rq->on_list = 1; in list_add_leaf_cfs_rq()
329 * enqueued. The fact that we always enqueue bottom-up in list_add_leaf_cfs_rq()
335 if (cfs_rq->tg->parent && in list_add_leaf_cfs_rq()
336 cfs_rq->tg->parent->cfs_rq[cpu]->on_list) { in list_add_leaf_cfs_rq()
343 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list, in list_add_leaf_cfs_rq()
344 &(cfs_rq->tg->parent->cfs_rq[cpu]->leaf_cfs_rq_list)); in list_add_leaf_cfs_rq()
350 rq->tmp_alone_branch = &rq->leaf_cfs_rq_list; in list_add_leaf_cfs_rq()
354 if (!cfs_rq->tg->parent) { in list_add_leaf_cfs_rq()
359 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list, in list_add_leaf_cfs_rq()
360 &rq->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()
375 list_add_rcu(&cfs_rq->leaf_cfs_rq_list, rq->tmp_alone_branch); in list_add_leaf_cfs_rq()
380 rq->tmp_alone_branch = &cfs_rq->leaf_cfs_rq_list; in list_add_leaf_cfs_rq()
386 if (cfs_rq->on_list) { in list_del_leaf_cfs_rq()
393 * to the prev element but it will point to rq->leaf_cfs_rq_list in list_del_leaf_cfs_rq()
396 if (rq->tmp_alone_branch == &cfs_rq->leaf_cfs_rq_list) in list_del_leaf_cfs_rq()
397 rq->tmp_alone_branch = cfs_rq->leaf_cfs_rq_list.prev; in list_del_leaf_cfs_rq()
399 list_del_rcu(&cfs_rq->leaf_cfs_rq_list); in list_del_leaf_cfs_rq()
400 cfs_rq->on_list = 0; in list_del_leaf_cfs_rq()
406 SCHED_WARN_ON(rq->tmp_alone_branch != &rq->leaf_cfs_rq_list); in assert_list_leaf_cfs_rq()
411 list_for_each_entry_safe(cfs_rq, pos, &rq->leaf_cfs_rq_list, \
418 if (se->cfs_rq == pse->cfs_rq) in is_same_group()
419 return se->cfs_rq; in is_same_group()
426 return se->parent; in parent_entity()
442 se_depth = (*se)->depth; in find_matching_se()
443 pse_depth = (*pse)->depth; in find_matching_se()
446 se_depth--; in find_matching_se()
451 pse_depth--; in find_matching_se()
463 return tg->idle > 0; in tg_is_idle()
468 return cfs_rq->idle > 0; in cfs_rq_is_idle()
497 for (cfs_rq = &rq->cfs, pos = NULL; cfs_rq; cfs_rq = pos)
535 s64 delta = (s64)(vruntime - max_vruntime); in max_vruntime()
544 s64 delta = (s64)(vruntime - min_vruntime); in min_vruntime()
558 return (s64)(a->deadline - b->deadline) < 0; in entity_before()
563 return (s64)(se->vruntime - cfs_rq->min_vruntime); in entity_key()
570 * Compute virtual time from the per-task service numbers:
578 * lag_i = S - s_i = w_i * (V - v_i)
584 * \Sum w_i * (V - v_i) = 0
585 * \Sum w_i * V - w_i * v_i = 0
588 * se->vruntime):
591 * V = -------------- = --------------
598 * virtual time has non-continguous motion equivalent to:
600 * V +-= lag_i / W
607 * Substitute: v_i == (v_i - v0) + v0
609 * \Sum ((v_i - v0) + v0) * w_i \Sum (v_i - v0) * w_i
610 * V = ---------------------------- = --------------------- + v0
615 * v0 := cfs_rq->min_vruntime
616 * \Sum (v_i - v0) * w_i := cfs_rq->avg_vruntime
617 * \Sum w_i := cfs_rq->avg_load
620 * the per-task service, these deltas: (v_i - v), will be in the order of the
630 unsigned long weight = scale_load_down(se->load.weight); in avg_vruntime_add()
633 cfs_rq->avg_vruntime += key * weight; in avg_vruntime_add()
634 cfs_rq->avg_load += weight; in avg_vruntime_add()
640 unsigned long weight = scale_load_down(se->load.weight); in avg_vruntime_sub()
643 cfs_rq->avg_vruntime -= key * weight; in avg_vruntime_sub()
644 cfs_rq->avg_load -= weight; in avg_vruntime_sub()
651 * v' = v + d ==> avg_vruntime' = avg_runtime - d*avg_load in avg_vruntime_update()
653 cfs_rq->avg_vruntime -= cfs_rq->avg_load * delta; in avg_vruntime_update()
662 struct sched_entity *curr = cfs_rq->curr; in avg_vruntime()
663 s64 avg = cfs_rq->avg_vruntime; in avg_vruntime()
664 long load = cfs_rq->avg_load; in avg_vruntime()
666 if (curr && curr->on_rq) { in avg_vruntime()
667 unsigned long weight = scale_load_down(curr->load.weight); in avg_vruntime()
676 avg -= (load - 1); in avg_vruntime()
680 return cfs_rq->min_vruntime + avg; in avg_vruntime()
684 * lag_i = S - s_i = w_i * (V - v_i)
687 * is possible -- by addition/removal/reweight to the tree -- to move V around
695 * -r_max < lag < max(r_max, q)
703 SCHED_WARN_ON(!se->on_rq); in update_entity_lag()
704 lag = avg_vruntime(cfs_rq) - se->vruntime; in update_entity_lag()
706 limit = calc_delta_fair(max_t(u64, 2*se->slice, TICK_NSEC), se); in update_entity_lag()
707 se->vlag = clamp(lag, -limit, limit); in update_entity_lag()
714 * lag_i = S - s_i = w_i*(V - v_i)
716 * lag_i >= 0 -> V >= v_i
718 * \Sum (v_i - v)*w_i
719 * V = ------------------ + v
722 * lag_i >= 0 -> \Sum (v_i - v)*w_i >= (v_i - v)*(\Sum w_i)
724 * Note: using 'avg_vruntime() > se->vruntime' is inacurate due
729 struct sched_entity *curr = cfs_rq->curr; in vruntime_eligible()
730 s64 avg = cfs_rq->avg_vruntime; in vruntime_eligible()
731 long load = cfs_rq->avg_load; in vruntime_eligible()
733 if (curr && curr->on_rq) { in vruntime_eligible()
734 unsigned long weight = scale_load_down(curr->load.weight); in vruntime_eligible()
740 return avg >= (s64)(vruntime - cfs_rq->min_vruntime) * load; in vruntime_eligible()
745 return vruntime_eligible(cfs_rq, se->vruntime); in entity_eligible()
750 u64 min_vruntime = cfs_rq->min_vruntime; in __update_min_vruntime()
754 s64 delta = (s64)(vruntime - min_vruntime); in __update_min_vruntime()
765 struct sched_entity *curr = cfs_rq->curr; in update_min_vruntime()
766 u64 vruntime = cfs_rq->min_vruntime; in update_min_vruntime()
769 if (curr->on_rq) in update_min_vruntime()
770 vruntime = curr->vruntime; in update_min_vruntime()
777 vruntime = se->min_vruntime; in update_min_vruntime()
779 vruntime = min_vruntime(vruntime, se->min_vruntime); in update_min_vruntime()
783 u64_u32_store(cfs_rq->min_vruntime, in update_min_vruntime()
792 #define vruntime_gt(field, lse, rse) ({ (s64)((lse)->field - (rse)->field) > 0; })
799 se->min_vruntime = rse->min_vruntime; in __min_vruntime_update()
804 * se->min_vruntime = min(se->vruntime, {left,right}->min_vruntime)
808 u64 old_min_vruntime = se->min_vruntime; in min_vruntime_update()
809 struct rb_node *node = &se->run_node; in min_vruntime_update()
811 se->min_vruntime = se->vruntime; in min_vruntime_update()
812 __min_vruntime_update(se, node->rb_right); in min_vruntime_update()
813 __min_vruntime_update(se, node->rb_left); in min_vruntime_update()
815 return se->min_vruntime == old_min_vruntime; in min_vruntime_update()
822 * Enqueue an entity into the rb-tree:
827 se->min_vruntime = se->vruntime; in __enqueue_entity()
828 rb_add_augmented_cached(&se->run_node, &cfs_rq->tasks_timeline, in __enqueue_entity()
834 rb_erase_augmented_cached(&se->run_node, &cfs_rq->tasks_timeline, in __dequeue_entity()
841 struct rb_node *root = cfs_rq->tasks_timeline.rb_root.rb_node; in __pick_root_entity()
851 struct rb_node *left = rb_first_cached(&cfs_rq->tasks_timeline); in __pick_first_entity()
870 * We can do this in O(log n) time due to an augmented RB-tree. The
874 * se->min_vruntime = min(se->vruntime, se->{left,right}->min_vruntime)
880 struct rb_node *node = cfs_rq->tasks_timeline.rb_root.rb_node; in pick_eevdf()
882 struct sched_entity *curr = cfs_rq->curr; in pick_eevdf()
889 if (cfs_rq->nr_running == 1) in pick_eevdf()
890 return curr && curr->on_rq ? curr : se; in pick_eevdf()
892 if (curr && (!curr->on_rq || !entity_eligible(cfs_rq, curr))) in pick_eevdf()
896 * Once selected, run a task until it either becomes non-eligible or in pick_eevdf()
899 if (sched_feat(RUN_TO_PARITY) && curr && curr->vlag == curr->deadline) in pick_eevdf()
910 struct rb_node *left = node->rb_left; in pick_eevdf()
917 __node_2_se(left)->min_vruntime)) { in pick_eevdf()
934 node = node->rb_right; in pick_eevdf()
946 struct rb_node *last = rb_last(&cfs_rq->tasks_timeline.rb_root); in __pick_last_entity()
980 if ((s64)(se->vruntime - se->deadline) < 0) in update_deadline()
988 se->slice = sysctl_sched_base_slice; in update_deadline()
993 se->deadline = se->vruntime + calc_delta_fair(se->slice, se); in update_deadline()
998 if (cfs_rq->nr_running > 1) { in update_deadline()
1014 struct sched_avg *sa = &se->avg; in init_entity_runnable_average()
1025 sa->load_avg = scale_load_down(se->load.weight); in init_entity_runnable_average()
1034 * util_avg = cfs_rq->util_avg / (cfs_rq->load_avg + 1) * se.load.weight
1043 * util_avg_cap = (cpu_scale - cfs_rq->avg.util_avg) / 2^n
1058 struct sched_entity *se = &p->se; in post_init_entity_util_avg()
1060 struct sched_avg *sa = &se->avg; in post_init_entity_util_avg()
1062 long cap = (long)(cpu_scale - cfs_rq->avg.util_avg) / 2; in post_init_entity_util_avg()
1064 if (p->sched_class != &fair_sched_class) { in post_init_entity_util_avg()
1075 se->avg.last_update_time = cfs_rq_clock_pelt(cfs_rq); in post_init_entity_util_avg()
1080 if (cfs_rq->avg.util_avg != 0) { in post_init_entity_util_avg()
1081 sa->util_avg = cfs_rq->avg.util_avg * se->load.weight; in post_init_entity_util_avg()
1082 sa->util_avg /= (cfs_rq->avg.load_avg + 1); in post_init_entity_util_avg()
1084 if (sa->util_avg > cap) in post_init_entity_util_avg()
1085 sa->util_avg = cap; in post_init_entity_util_avg()
1087 sa->util_avg = cap; in post_init_entity_util_avg()
1091 sa->runnable_avg = sa->util_avg; in post_init_entity_util_avg()
1111 delta_exec = now - curr->exec_start; in update_curr_se()
1115 curr->exec_start = now; in update_curr_se()
1116 curr->sum_exec_runtime += delta_exec; in update_curr_se()
1122 __schedstat_set(stats->exec_max, in update_curr_se()
1123 max(delta_exec, stats->exec_max)); in update_curr_se()
1134 if (p->dl_server) in update_curr_task()
1135 dl_server_update(p->dl_server, delta_exec); in update_curr_task()
1143 struct task_struct *curr = rq->curr; in update_curr_common()
1146 delta_exec = update_curr_se(rq, &curr->se); in update_curr_common()
1158 struct sched_entity *curr = cfs_rq->curr; in update_curr()
1168 curr->vruntime += calc_delta_fair(delta_exec, curr); in update_curr()
1180 update_curr(cfs_rq_of(&rq->curr->se)); in update_curr_fair()
1213 * maybe already in the runqueue, the se->statistics.wait_start in update_stats_wait_end_fair()
1217 if (unlikely(!schedstat_val(stats->wait_start))) in update_stats_wait_end_fair()
1244 * Task is being enqueued - update stats:
1256 if (se != cfs_rq->curr) in update_stats_enqueue_fair()
1274 if (se != cfs_rq->curr) in update_stats_dequeue_fair()
1282 state = READ_ONCE(tsk->__state); in update_stats_dequeue_fair()
1284 __schedstat_set(tsk->stats.sleep_start, in update_stats_dequeue_fair()
1287 __schedstat_set(tsk->stats.block_start, in update_stats_dequeue_fair()
1293 * We are picking a new current task - update its stats:
1301 se->exec_start = rq_clock_task(rq_of(cfs_rq)); in update_stats_curr_start()
1342 * Allow a small imbalance based on a simple pair of communicating in adjust_numa_imbalance()
1393 * ->numa_group (see struct task_struct for locking rules).
1397 return rcu_dereference_check(p->numa_group, p == current || in deref_task_numa_group()
1398 (lockdep_is_held(__rq_lockp(task_rq(p))) && !READ_ONCE(p->on_cpu))); in deref_task_numa_group()
1403 return rcu_dereference_protected(p->numa_group, p == current); in deref_curr_numa_group()
1415 * Calculations based on RSS as non-present and empty pages are skipped in task_nr_scan_windows()
1419 nr_scan_pages = sysctl_numa_balancing_scan_size << (20 - PAGE_SHIFT); in task_nr_scan_windows()
1420 rss = get_mm_rss(p->mm); in task_nr_scan_windows()
1453 ng = rcu_dereference(p->numa_group); in task_scan_start()
1458 period *= refcount_read(&ng->refcount); in task_scan_start()
1483 period *= refcount_read(&ng->refcount); in task_scan_max()
1495 rq->nr_numa_running += (p->numa_preferred_nid != NUMA_NO_NODE); in account_numa_enqueue()
1496 rq->nr_preferred_running += (p->numa_preferred_nid == task_node(p)); in account_numa_enqueue()
1501 rq->nr_numa_running -= (p->numa_preferred_nid != NUMA_NO_NODE); in account_numa_dequeue()
1502 rq->nr_preferred_running -= (p->numa_preferred_nid == task_node(p)); in account_numa_dequeue()
1520 ng = rcu_dereference(p->numa_group); in task_numa_group_id()
1522 gid = ng->gid; in task_numa_group_id()
1541 if (!p->numa_faults) in task_faults()
1544 return p->numa_faults[task_faults_idx(NUMA_MEM, nid, 0)] + in task_faults()
1545 p->numa_faults[task_faults_idx(NUMA_MEM, nid, 1)]; in task_faults()
1555 return ng->faults[task_faults_idx(NUMA_MEM, nid, 0)] + in group_faults()
1556 ng->faults[task_faults_idx(NUMA_MEM, nid, 1)]; in group_faults()
1561 return group->faults[task_faults_idx(NUMA_CPU, nid, 0)] + in group_faults_cpu()
1562 group->faults[task_faults_idx(NUMA_CPU, nid, 1)]; in group_faults_cpu()
1571 faults += ng->faults[task_faults_idx(NUMA_MEM, node, 1)]; in group_faults_priv()
1583 faults += ng->faults[task_faults_idx(NUMA_MEM, node, 0)]; in group_faults_shared()
1591 * considered part of a numa group's pseudo-interleaving set. Migrations
1598 return group_faults_cpu(ng, nid) * ACTIVE_NODE_FRACTION > ng->max_faults_cpu; in numa_is_active_node()
1657 faults *= (max_dist - dist); in score_nearby_nodes()
1658 faults /= (max_dist - LOCAL_DISTANCE); in score_nearby_nodes()
1678 if (!p->numa_faults) in task_weight()
1681 total_faults = p->total_numa_faults; in task_weight()
1701 total_faults = ng->total_faults; in group_weight()
1737 pgdat->node_present_pages >> 4); in pgdat_free_space_enough()
1738 for (z = pgdat->nr_zones - 1; z >= 0; z--) { in pgdat_free_space_enough()
1739 struct zone *zone = pgdat->node_zones + z; in pgdat_free_space_enough()
1759 * hint page fault latency = hint page fault time - scan time
1771 return (time - last_time) & PAGE_ACCESS_TIME_MASK; in numa_hint_fault_latency()
1788 start = pgdat->nbp_rl_start; in numa_promotion_rate_limit()
1789 if (now - start > MSEC_PER_SEC && in numa_promotion_rate_limit()
1790 cmpxchg(&pgdat->nbp_rl_start, start, now) == start) in numa_promotion_rate_limit()
1791 pgdat->nbp_rl_nr_cand = nr_cand; in numa_promotion_rate_limit()
1792 if (nr_cand - pgdat->nbp_rl_nr_cand >= rate_limit) in numa_promotion_rate_limit()
1808 start = pgdat->nbp_th_start; in numa_promotion_adjust_threshold()
1809 if (now - start > th_period && in numa_promotion_adjust_threshold()
1810 cmpxchg(&pgdat->nbp_th_start, start, now) == start) { in numa_promotion_adjust_threshold()
1814 diff_cand = nr_cand - pgdat->nbp_th_nr_cand; in numa_promotion_adjust_threshold()
1816 th = pgdat->nbp_threshold ? : ref_th; in numa_promotion_adjust_threshold()
1818 th = max(th - unit_th, unit_th); in numa_promotion_adjust_threshold()
1821 pgdat->nbp_th_nr_cand = nr_cand; in numa_promotion_adjust_threshold()
1822 pgdat->nbp_threshold = th; in numa_promotion_adjust_threshold()
1846 pgdat->nbp_threshold = 0; in should_numa_migrate_memory()
1852 (20 - PAGE_SHIFT); in should_numa_migrate_memory()
1855 th = pgdat->nbp_threshold ? : def_th; in should_numa_migrate_memory()
1864 this_cpupid = cpu_pid_to_cpupid(dst_cpu, current->pid); in should_numa_migrate_memory()
1874 * two full passes of the "multi-stage node selection" test that is in should_numa_migrate_memory()
1877 if ((p->numa_preferred_nid == NUMA_NO_NODE || p->numa_scan_seq <= 4) && in should_numa_migrate_memory()
1882 * Multi-stage node selection is used in conjunction with a periodic in should_numa_migrate_memory()
1883 * migration fault to build a temporal task<->page relation. By using in should_numa_migrate_memory()
1884 * a two-stage filter we remove short/unlikely relations. in should_numa_migrate_memory()
1888 * page (n_t) (in a given time-span) to a probability. in should_numa_migrate_memory()
1896 * act on an unlikely task<->page relation. in should_numa_migrate_memory()
1906 /* A shared fault, but p->numa_group has not been set up yet. */ in should_numa_migrate_memory()
1923 * --------------- * - > --------------- in should_numa_migrate_memory()
1985 if ((ns->nr_running > ns->weight) && in numa_classify()
1986 (((ns->compute_capacity * 100) < (ns->util * imbalance_pct)) || in numa_classify()
1987 ((ns->compute_capacity * imbalance_pct) < (ns->runnable * 100)))) in numa_classify()
1990 if ((ns->nr_running < ns->weight) || in numa_classify()
1991 (((ns->compute_capacity * 100) > (ns->util * imbalance_pct)) && in numa_classify()
1992 ((ns->compute_capacity * imbalance_pct) > (ns->runnable * 100)))) in numa_classify()
2033 int cpu, idle_core = -1; in update_numa_stats()
2036 ns->idle_cpu = -1; in update_numa_stats()
2042 ns->load += cpu_load(rq); in update_numa_stats()
2043 ns->runnable += cpu_runnable(rq); in update_numa_stats()
2044 ns->util += cpu_util_cfs(cpu); in update_numa_stats()
2045 ns->nr_running += rq->cfs.h_nr_running; in update_numa_stats()
2046 ns->compute_capacity += capacity_of(cpu); in update_numa_stats()
2048 if (find_idle && idle_core < 0 && !rq->nr_running && idle_cpu(cpu)) { in update_numa_stats()
2049 if (READ_ONCE(rq->numa_migrate_on) || in update_numa_stats()
2050 !cpumask_test_cpu(cpu, env->p->cpus_ptr)) in update_numa_stats()
2053 if (ns->idle_cpu == -1) in update_numa_stats()
2054 ns->idle_cpu = cpu; in update_numa_stats()
2061 ns->weight = cpumask_weight(cpumask_of_node(nid)); in update_numa_stats()
2063 ns->node_type = numa_classify(env->imbalance_pct, ns); in update_numa_stats()
2066 ns->idle_cpu = idle_core; in update_numa_stats()
2072 struct rq *rq = cpu_rq(env->dst_cpu); in task_numa_assign()
2074 /* Check if run-queue part of active NUMA balance. */ in task_numa_assign()
2075 if (env->best_cpu != env->dst_cpu && xchg(&rq->numa_migrate_on, 1)) { in task_numa_assign()
2077 int start = env->dst_cpu; in task_numa_assign()
2080 for_each_cpu_wrap(cpu, cpumask_of_node(env->dst_nid), start + 1) { in task_numa_assign()
2081 if (cpu == env->best_cpu || !idle_cpu(cpu) || in task_numa_assign()
2082 !cpumask_test_cpu(cpu, env->p->cpus_ptr)) { in task_numa_assign()
2086 env->dst_cpu = cpu; in task_numa_assign()
2087 rq = cpu_rq(env->dst_cpu); in task_numa_assign()
2088 if (!xchg(&rq->numa_migrate_on, 1)) in task_numa_assign()
2098 * Clear previous best_cpu/rq numa-migrate flag, since task now in task_numa_assign()
2101 if (env->best_cpu != -1 && env->best_cpu != env->dst_cpu) { in task_numa_assign()
2102 rq = cpu_rq(env->best_cpu); in task_numa_assign()
2103 WRITE_ONCE(rq->numa_migrate_on, 0); in task_numa_assign()
2106 if (env->best_task) in task_numa_assign()
2107 put_task_struct(env->best_task); in task_numa_assign()
2111 env->best_task = p; in task_numa_assign()
2112 env->best_imp = imp; in task_numa_assign()
2113 env->best_cpu = env->dst_cpu; in task_numa_assign()
2127 * ------------ vs --------- in load_too_imbalanced()
2130 src_capacity = env->src_stats.compute_capacity; in load_too_imbalanced()
2131 dst_capacity = env->dst_stats.compute_capacity; in load_too_imbalanced()
2133 imb = abs(dst_load * src_capacity - src_load * dst_capacity); in load_too_imbalanced()
2135 orig_src_load = env->src_stats.load; in load_too_imbalanced()
2136 orig_dst_load = env->dst_stats.load; in load_too_imbalanced()
2138 old_imb = abs(orig_dst_load * src_capacity - orig_src_load * dst_capacity); in load_too_imbalanced()
2160 struct numa_group *cur_ng, *p_ng = deref_curr_numa_group(env->p); in task_numa_compare()
2161 struct rq *dst_rq = cpu_rq(env->dst_cpu); in task_numa_compare()
2165 int dist = env->dist; in task_numa_compare()
2170 if (READ_ONCE(dst_rq->numa_migrate_on)) in task_numa_compare()
2174 cur = rcu_dereference(dst_rq->curr); in task_numa_compare()
2175 if (cur && ((cur->flags & PF_EXITING) || is_idle_task(cur))) in task_numa_compare()
2180 * end try selecting ourselves (current == env->p) as a swap candidate. in task_numa_compare()
2182 if (cur == env->p) { in task_numa_compare()
2188 if (maymove && moveimp >= env->best_imp) in task_numa_compare()
2195 if (!cpumask_test_cpu(env->src_cpu, cur->cpus_ptr)) in task_numa_compare()
2202 if (env->best_task && in task_numa_compare()
2203 env->best_task->numa_preferred_nid == env->src_nid && in task_numa_compare()
2204 cur->numa_preferred_nid != env->src_nid) { in task_numa_compare()
2209 * "imp" is the fault differential for the source task between the in task_numa_compare()
2210 * source and destination node. Calculate the total differential for in task_numa_compare()
2218 cur_ng = rcu_dereference(cur->numa_group); in task_numa_compare()
2226 if (env->dst_stats.node_type == node_has_spare) in task_numa_compare()
2229 imp = taskimp + task_weight(cur, env->src_nid, dist) - in task_numa_compare()
2230 task_weight(cur, env->dst_nid, dist); in task_numa_compare()
2236 imp -= imp / 16; in task_numa_compare()
2243 imp += group_weight(cur, env->src_nid, dist) - in task_numa_compare()
2244 group_weight(cur, env->dst_nid, dist); in task_numa_compare()
2246 imp += task_weight(cur, env->src_nid, dist) - in task_numa_compare()
2247 task_weight(cur, env->dst_nid, dist); in task_numa_compare()
2251 if (cur->numa_preferred_nid == env->dst_nid) in task_numa_compare()
2252 imp -= imp / 16; in task_numa_compare()
2260 if (cur->numa_preferred_nid == env->src_nid) in task_numa_compare()
2263 if (maymove && moveimp > imp && moveimp > env->best_imp) { in task_numa_compare()
2273 if (env->best_task && cur->numa_preferred_nid == env->src_nid && in task_numa_compare()
2274 env->best_task->numa_preferred_nid != env->src_nid) { in task_numa_compare()
2284 if (imp < SMALLIMP || imp <= env->best_imp + SMALLIMP / 2) in task_numa_compare()
2290 load = task_h_load(env->p) - task_h_load(cur); in task_numa_compare()
2294 dst_load = env->dst_stats.load + load; in task_numa_compare()
2295 src_load = env->src_stats.load - load; in task_numa_compare()
2303 int cpu = env->dst_stats.idle_cpu; in task_numa_compare()
2307 cpu = env->dst_cpu; in task_numa_compare()
2313 if (!idle_cpu(cpu) && env->best_cpu >= 0 && in task_numa_compare()
2314 idle_cpu(env->best_cpu)) { in task_numa_compare()
2315 cpu = env->best_cpu; in task_numa_compare()
2318 env->dst_cpu = cpu; in task_numa_compare()
2328 if (maymove && !cur && env->best_cpu >= 0 && idle_cpu(env->best_cpu)) in task_numa_compare()
2335 if (!maymove && env->best_task && in task_numa_compare()
2336 env->best_task->numa_preferred_nid == env->src_nid) { in task_numa_compare()
2355 if (env->dst_stats.node_type == node_has_spare) { in task_numa_find_cpu()
2365 src_running = env->src_stats.nr_running - 1; in task_numa_find_cpu()
2366 dst_running = env->dst_stats.nr_running + 1; in task_numa_find_cpu()
2367 imbalance = max(0, dst_running - src_running); in task_numa_find_cpu()
2369 env->imb_numa_nr); in task_numa_find_cpu()
2374 if (env->dst_stats.idle_cpu >= 0) { in task_numa_find_cpu()
2375 env->dst_cpu = env->dst_stats.idle_cpu; in task_numa_find_cpu()
2383 * If the improvement from just moving env->p direction is better in task_numa_find_cpu()
2386 load = task_h_load(env->p); in task_numa_find_cpu()
2387 dst_load = env->dst_stats.load + load; in task_numa_find_cpu()
2388 src_load = env->src_stats.load - load; in task_numa_find_cpu()
2392 for_each_cpu(cpu, cpumask_of_node(env->dst_nid)) { in task_numa_find_cpu()
2394 if (!cpumask_test_cpu(cpu, env->p->cpus_ptr)) in task_numa_find_cpu()
2397 env->dst_cpu = cpu; in task_numa_find_cpu()
2415 .best_cpu = -1, in task_numa_migrate()
2429 * random movement of tasks -- counter the numa conditions we're trying in task_numa_migrate()
2435 env.imbalance_pct = 100 + (sd->imbalance_pct - 100) / 2; in task_numa_migrate()
2436 env.imb_numa_nr = sd->imb_numa_nr; in task_numa_migrate()
2448 return -EINVAL; in task_numa_migrate()
2451 env.dst_nid = p->numa_preferred_nid; in task_numa_migrate()
2456 taskimp = task_weight(p, env.dst_nid, dist) - taskweight; in task_numa_migrate()
2457 groupimp = group_weight(p, env.dst_nid, dist) - groupweight; in task_numa_migrate()
2465 * - there is no space available on the preferred_nid in task_numa_migrate()
2466 * - the task is part of a numa_group that is interleaved across in task_numa_migrate()
2471 if (env.best_cpu == -1 || (ng && ng->active_nodes > 1)) { in task_numa_migrate()
2473 if (nid == env.src_nid || nid == p->numa_preferred_nid) in task_numa_migrate()
2484 taskimp = task_weight(p, nid, dist) - taskweight; in task_numa_migrate()
2485 groupimp = group_weight(p, nid, dist) - groupweight; in task_numa_migrate()
2505 if (env.best_cpu == -1) in task_numa_migrate()
2510 if (nid != p->numa_preferred_nid) in task_numa_migrate()
2515 if (env.best_cpu == -1) { in task_numa_migrate()
2516 trace_sched_stick_numa(p, env.src_cpu, NULL, -1); in task_numa_migrate()
2517 return -EAGAIN; in task_numa_migrate()
2523 WRITE_ONCE(best_rq->numa_migrate_on, 0); in task_numa_migrate()
2530 WRITE_ONCE(best_rq->numa_migrate_on, 0); in task_numa_migrate()
2544 if (unlikely(p->numa_preferred_nid == NUMA_NO_NODE || !p->numa_faults)) in numa_migrate_preferred()
2548 interval = min(interval, msecs_to_jiffies(p->numa_scan_period) / 16); in numa_migrate_preferred()
2549 p->numa_migrate_retry = jiffies + interval; in numa_migrate_preferred()
2552 if (task_node(p) == p->numa_preferred_nid) in numa_migrate_preferred()
2582 numa_group->max_faults_cpu = max_faults; in numa_group_count_active_nodes()
2583 numa_group->active_nodes = active_nodes; in numa_group_count_active_nodes()
2609 unsigned long remote = p->numa_faults_locality[0]; in update_task_scan_period()
2610 unsigned long local = p->numa_faults_locality[1]; in update_task_scan_period()
2619 if (local + shared == 0 || p->numa_faults_locality[2]) { in update_task_scan_period()
2620 p->numa_scan_period = min(p->numa_scan_period_max, in update_task_scan_period()
2621 p->numa_scan_period << 1); in update_task_scan_period()
2623 p->mm->numa_next_scan = jiffies + in update_task_scan_period()
2624 msecs_to_jiffies(p->numa_scan_period); in update_task_scan_period()
2635 period_slot = DIV_ROUND_UP(p->numa_scan_period, NUMA_PERIOD_SLOTS); in update_task_scan_period()
2644 int slot = ps_ratio - NUMA_PERIOD_THRESHOLD; in update_task_scan_period()
2654 int slot = lr_ratio - NUMA_PERIOD_THRESHOLD; in update_task_scan_period()
2660 * Private memory faults exceed (SLOTS-THRESHOLD)/SLOTS, in update_task_scan_period()
2665 diff = -(NUMA_PERIOD_THRESHOLD - ratio) * period_slot; in update_task_scan_period()
2668 p->numa_scan_period = clamp(p->numa_scan_period + diff, in update_task_scan_period()
2670 memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); in update_task_scan_period()
2677 * from the dozens-of-seconds NUMA balancing period. Use the scheduler
2684 now = p->se.exec_start; in numa_get_avg_runtime()
2685 runtime = p->se.sum_exec_runtime; in numa_get_avg_runtime()
2687 if (p->last_task_numa_placement) { in numa_get_avg_runtime()
2688 delta = runtime - p->last_sum_exec_runtime; in numa_get_avg_runtime()
2689 *period = now - p->last_task_numa_placement; in numa_get_avg_runtime()
2695 delta = p->se.avg.load_sum; in numa_get_avg_runtime()
2699 p->last_sum_exec_runtime = runtime; in numa_get_avg_runtime()
2700 p->last_task_numa_placement = now; in numa_get_avg_runtime()
2750 for (dist = sched_max_numa_distance; dist > LOCAL_DISTANCE; dist--) { in preferred_group_nid()
2804 * The p->mm->numa_scan_seq field gets updated without in task_numa_placement()
2808 seq = READ_ONCE(p->mm->numa_scan_seq); in task_numa_placement()
2809 if (p->numa_scan_seq == seq) in task_numa_placement()
2811 p->numa_scan_seq = seq; in task_numa_placement()
2812 p->numa_scan_period_max = task_scan_max(p); in task_numa_placement()
2814 total_faults = p->numa_faults_locality[0] + in task_numa_placement()
2815 p->numa_faults_locality[1]; in task_numa_placement()
2821 group_lock = &ng->lock; in task_numa_placement()
2841 diff = p->numa_faults[membuf_idx] - p->numa_faults[mem_idx] / 2; in task_numa_placement()
2842 fault_types[priv] += p->numa_faults[membuf_idx]; in task_numa_placement()
2843 p->numa_faults[membuf_idx] = 0; in task_numa_placement()
2849 * little over-all impact on throughput, and thus their in task_numa_placement()
2853 f_weight = (f_weight * p->numa_faults[cpubuf_idx]) / in task_numa_placement()
2855 f_diff = f_weight - p->numa_faults[cpu_idx] / 2; in task_numa_placement()
2856 p->numa_faults[cpubuf_idx] = 0; in task_numa_placement()
2858 p->numa_faults[mem_idx] += diff; in task_numa_placement()
2859 p->numa_faults[cpu_idx] += f_diff; in task_numa_placement()
2860 faults += p->numa_faults[mem_idx]; in task_numa_placement()
2861 p->total_numa_faults += diff; in task_numa_placement()
2870 ng->faults[mem_idx] += diff; in task_numa_placement()
2871 ng->faults[cpu_idx] += f_diff; in task_numa_placement()
2872 ng->total_faults += diff; in task_numa_placement()
2873 group_faults += ng->faults[mem_idx]; in task_numa_placement()
2888 /* Cannot migrate task to CPU-less node */ in task_numa_placement()
2899 if (max_nid != p->numa_preferred_nid) in task_numa_placement()
2908 return refcount_inc_not_zero(&grp->refcount); in get_numa_group()
2913 if (refcount_dec_and_test(&grp->refcount)) in put_numa_group()
2935 refcount_set(&grp->refcount, 1); in task_numa_group()
2936 grp->active_nodes = 1; in task_numa_group()
2937 grp->max_faults_cpu = 0; in task_numa_group()
2938 spin_lock_init(&grp->lock); in task_numa_group()
2939 grp->gid = p->pid; in task_numa_group()
2942 grp->faults[i] = p->numa_faults[i]; in task_numa_group()
2944 grp->total_faults = p->total_numa_faults; in task_numa_group()
2946 grp->nr_tasks++; in task_numa_group()
2947 rcu_assign_pointer(p->numa_group, grp); in task_numa_group()
2951 tsk = READ_ONCE(cpu_rq(cpu)->curr); in task_numa_group()
2956 grp = rcu_dereference(tsk->numa_group); in task_numa_group()
2968 if (my_grp->nr_tasks > grp->nr_tasks) in task_numa_group()
2972 * Tie-break on the grp address. in task_numa_group()
2974 if (my_grp->nr_tasks == grp->nr_tasks && my_grp > grp) in task_numa_group()
2978 if (tsk->mm == current->mm) in task_numa_group()
2997 double_lock_irq(&my_grp->lock, &grp->lock); in task_numa_group()
3000 my_grp->faults[i] -= p->numa_faults[i]; in task_numa_group()
3001 grp->faults[i] += p->numa_faults[i]; in task_numa_group()
3003 my_grp->total_faults -= p->total_numa_faults; in task_numa_group()
3004 grp->total_faults += p->total_numa_faults; in task_numa_group()
3006 my_grp->nr_tasks--; in task_numa_group()
3007 grp->nr_tasks++; in task_numa_group()
3009 spin_unlock(&my_grp->lock); in task_numa_group()
3010 spin_unlock_irq(&grp->lock); in task_numa_group()
3012 rcu_assign_pointer(p->numa_group, grp); in task_numa_group()
3027 * reset the data back to default state without freeing ->numa_faults.
3032 struct numa_group *grp = rcu_dereference_raw(p->numa_group); in task_numa_free()
3033 unsigned long *numa_faults = p->numa_faults; in task_numa_free()
3041 spin_lock_irqsave(&grp->lock, flags); in task_numa_free()
3043 grp->faults[i] -= p->numa_faults[i]; in task_numa_free()
3044 grp->total_faults -= p->total_numa_faults; in task_numa_free()
3046 grp->nr_tasks--; in task_numa_free()
3047 spin_unlock_irqrestore(&grp->lock, flags); in task_numa_free()
3048 RCU_INIT_POINTER(p->numa_group, NULL); in task_numa_free()
3053 p->numa_faults = NULL; in task_numa_free()
3056 p->total_numa_faults = 0; in task_numa_free()
3078 if (!p->mm) in task_numa_fault()
3090 /* Allocate buffer to track faults on a per-node basis */ in task_numa_fault()
3091 if (unlikely(!p->numa_faults)) { in task_numa_fault()
3092 int size = sizeof(*p->numa_faults) * in task_numa_fault()
3095 p->numa_faults = kzalloc(size, GFP_KERNEL|__GFP_NOWARN); in task_numa_fault()
3096 if (!p->numa_faults) in task_numa_fault()
3099 p->total_numa_faults = 0; in task_numa_fault()
3100 memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); in task_numa_fault()
3107 if (unlikely(last_cpupid == (-1 & LAST_CPUPID_MASK))) { in task_numa_fault()
3122 if (!priv && !local && ng && ng->active_nodes > 1 && in task_numa_fault()
3131 if (time_after(jiffies, p->numa_migrate_retry)) { in task_numa_fault()
3137 p->numa_pages_migrated += pages; in task_numa_fault()
3139 p->numa_faults_locality[2] += pages; in task_numa_fault()
3141 p->numa_faults[task_faults_idx(NUMA_MEMBUF, mem_node, priv)] += pages; in task_numa_fault()
3142 p->numa_faults[task_faults_idx(NUMA_CPUBUF, cpu_node, priv)] += pages; in task_numa_fault()
3143 p->numa_faults_locality[local] += pages; in task_numa_fault()
3150 * p->mm->numa_scan_seq is written to without exclusive access in reset_ptenuma_scan()
3156 WRITE_ONCE(p->mm->numa_scan_seq, READ_ONCE(p->mm->numa_scan_seq) + 1); in reset_ptenuma_scan()
3157 p->mm->numa_scan_offset = 0; in reset_ptenuma_scan()
3169 if ((READ_ONCE(current->mm->numa_scan_seq) - vma->numab_state->start_scan_seq) < 2) in vma_is_accessed()
3172 pids = vma->numab_state->pids_active[0] | vma->numab_state->pids_active[1]; in vma_is_accessed()
3173 if (test_bit(hash_32(current->pid, ilog2(BITS_PER_LONG)), &pids)) in vma_is_accessed()
3178 * some VMAs may never be scanned in multi-threaded applications: in vma_is_accessed()
3180 if (mm->numa_scan_offset > vma->vm_start) { in vma_is_accessed()
3198 struct mm_struct *mm = p->mm; in task_numa_work()
3199 u64 runtime = p->se.sum_exec_runtime; in task_numa_work()
3210 work->next = work; in task_numa_work()
3214 * NOTE: make sure not to dereference p->mm before this check, in task_numa_work()
3216 * without p->mm even though we still had it when we enqueued this in task_numa_work()
3219 if (p->flags & PF_EXITING) in task_numa_work()
3222 if (!mm->numa_next_scan) { in task_numa_work()
3223 mm->numa_next_scan = now + in task_numa_work()
3230 migrate = mm->numa_next_scan; in task_numa_work()
3234 if (p->numa_scan_period == 0) { in task_numa_work()
3235 p->numa_scan_period_max = task_scan_max(p); in task_numa_work()
3236 p->numa_scan_period = task_scan_start(p); in task_numa_work()
3239 next_scan = now + msecs_to_jiffies(p->numa_scan_period); in task_numa_work()
3240 if (!try_cmpxchg(&mm->numa_next_scan, &migrate, next_scan)) in task_numa_work()
3247 p->node_stamp += 2 * TICK_NSEC; in task_numa_work()
3250 pages <<= 20 - PAGE_SHIFT; /* MB in pages */ in task_numa_work()
3267 start = mm->numa_scan_offset; in task_numa_work()
3279 is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_MIXEDMAP)) { in task_numa_work()
3287 * hinting faults in read-only file-backed mappings or the vdso in task_numa_work()
3290 if (!vma->vm_mm || in task_numa_work()
3291 (vma->vm_file && (vma->vm_flags & (VM_READ|VM_WRITE)) == (VM_READ))) { in task_numa_work()
3305 /* Initialise new per-VMA NUMAB state. */ in task_numa_work()
3306 if (!vma->numab_state) { in task_numa_work()
3307 vma->numab_state = kzalloc(sizeof(struct vma_numab_state), in task_numa_work()
3309 if (!vma->numab_state) in task_numa_work()
3312 vma->numab_state->start_scan_seq = mm->numa_scan_seq; in task_numa_work()
3314 vma->numab_state->next_scan = now + in task_numa_work()
3318 vma->numab_state->pids_active_reset = vma->numab_state->next_scan + in task_numa_work()
3326 vma->numab_state->prev_scan_seq = mm->numa_scan_seq - 1; in task_numa_work()
3333 if (mm->numa_scan_seq && time_before(jiffies, in task_numa_work()
3334 vma->numab_state->next_scan)) { in task_numa_work()
3340 if (mm->numa_scan_seq && in task_numa_work()
3341 time_after(jiffies, vma->numab_state->pids_active_reset)) { in task_numa_work()
3342 vma->numab_state->pids_active_reset = vma->numab_state->pids_active_reset + in task_numa_work()
3344 vma->numab_state->pids_active[0] = READ_ONCE(vma->numab_state->pids_active[1]); in task_numa_work()
3345 vma->numab_state->pids_active[1] = 0; in task_numa_work()
3349 if (vma->numab_state->prev_scan_seq == mm->numa_scan_seq) { in task_numa_work()
3350 mm->numa_scan_offset = vma->vm_end; in task_numa_work()
3366 start = max(start, vma->vm_start); in task_numa_work()
3368 end = min(end, vma->vm_end); in task_numa_work()
3374 * is not already pte-numa. If the VMA contains in task_numa_work()
3380 pages -= (end - start) >> PAGE_SHIFT; in task_numa_work()
3381 virtpages -= (end - start) >> PAGE_SHIFT; in task_numa_work()
3388 } while (end != vma->vm_end); in task_numa_work()
3391 vma->numab_state->prev_scan_seq = mm->numa_scan_seq; in task_numa_work()
3419 mm->numa_scan_offset = start; in task_numa_work()
3430 if (unlikely(p->se.sum_exec_runtime != runtime)) { in task_numa_work()
3431 u64 diff = p->se.sum_exec_runtime - runtime; in task_numa_work()
3432 p->node_stamp += 32 * diff; in task_numa_work()
3439 struct mm_struct *mm = p->mm; in init_numa_balancing()
3442 mm_users = atomic_read(&mm->mm_users); in init_numa_balancing()
3444 mm->numa_next_scan = jiffies + msecs_to_jiffies(sysctl_numa_balancing_scan_delay); in init_numa_balancing()
3445 mm->numa_scan_seq = 0; in init_numa_balancing()
3448 p->node_stamp = 0; in init_numa_balancing()
3449 p->numa_scan_seq = mm ? mm->numa_scan_seq : 0; in init_numa_balancing()
3450 p->numa_scan_period = sysctl_numa_balancing_scan_delay; in init_numa_balancing()
3451 p->numa_migrate_retry = 0; in init_numa_balancing()
3453 p->numa_work.next = &p->numa_work; in init_numa_balancing()
3454 p->numa_faults = NULL; in init_numa_balancing()
3455 p->numa_pages_migrated = 0; in init_numa_balancing()
3456 p->total_numa_faults = 0; in init_numa_balancing()
3457 RCU_INIT_POINTER(p->numa_group, NULL); in init_numa_balancing()
3458 p->last_task_numa_placement = 0; in init_numa_balancing()
3459 p->last_sum_exec_runtime = 0; in init_numa_balancing()
3461 init_task_work(&p->numa_work, task_numa_work); in init_numa_balancing()
3465 p->numa_preferred_nid = NUMA_NO_NODE; in init_numa_balancing()
3477 current->numa_scan_period * mm_users * NSEC_PER_MSEC); in init_numa_balancing()
3479 p->node_stamp = delay; in init_numa_balancing()
3488 struct callback_head *work = &curr->numa_work; in task_tick_numa()
3494 if (!curr->mm || (curr->flags & (PF_EXITING | PF_KTHREAD)) || work->next != work) in task_tick_numa()
3503 now = curr->se.sum_exec_runtime; in task_tick_numa()
3504 period = (u64)curr->numa_scan_period * NSEC_PER_MSEC; in task_tick_numa()
3506 if (now > curr->node_stamp + period) { in task_tick_numa()
3507 if (!curr->node_stamp) in task_tick_numa()
3508 curr->numa_scan_period = task_scan_start(curr); in task_tick_numa()
3509 curr->node_stamp += period; in task_tick_numa()
3511 if (!time_before(jiffies, curr->mm->numa_next_scan)) in task_tick_numa()
3524 if (!p->mm || !p->numa_faults || (p->flags & PF_EXITING)) in update_scan_period()
3533 * is pulled cross-node due to wakeups or load balancing. in update_scan_period()
3535 if (p->numa_scan_seq) { in update_scan_period()
3541 if (dst_nid == p->numa_preferred_nid || in update_scan_period()
3542 (p->numa_preferred_nid != NUMA_NO_NODE && in update_scan_period()
3543 src_nid != p->numa_preferred_nid)) in update_scan_period()
3547 p->numa_scan_period = task_scan_start(p); in update_scan_period()
3572 update_load_add(&cfs_rq->load, se->load.weight); in account_entity_enqueue()
3578 list_add(&se->group_node, &rq->cfs_tasks); in account_entity_enqueue()
3581 cfs_rq->nr_running++; in account_entity_enqueue()
3583 cfs_rq->idle_nr_running++; in account_entity_enqueue()
3589 update_load_sub(&cfs_rq->load, se->load.weight); in account_entity_dequeue()
3593 list_del_init(&se->group_node); in account_entity_dequeue()
3596 cfs_rq->nr_running--; in account_entity_dequeue()
3598 cfs_rq->idle_nr_running--; in account_entity_dequeue()
3604 * Explicitly do a load-store to ensure the intermediate value never hits
3624 * Explicitly do a load-store to ensure the intermediate value never hits
3632 res = var - val; \
3641 * A variant of sub_positive(), which does not use explicit load-store
3646 *ptr -= min_t(typeof(*ptr), *ptr, _val); \
3653 cfs_rq->avg.load_avg += se->avg.load_avg; in enqueue_load_avg()
3654 cfs_rq->avg.load_sum += se_weight(se) * se->avg.load_sum; in enqueue_load_avg()
3660 sub_positive(&cfs_rq->avg.load_avg, se->avg.load_avg); in dequeue_load_avg()
3661 sub_positive(&cfs_rq->avg.load_sum, se_weight(se) * se->avg.load_sum); in dequeue_load_avg()
3663 cfs_rq->avg.load_sum = max_t(u32, cfs_rq->avg.load_sum, in dequeue_load_avg()
3664 cfs_rq->avg.load_avg * PELT_MIN_DIVIDER); in dequeue_load_avg()
3676 unsigned long old_weight = se->load.weight; in reweight_eevdf()
3685 * adjusted if re-weight at !0-lag point. in reweight_eevdf()
3688 * re-weight without changing vruntime at !0-lag point. in reweight_eevdf()
3690 * Weight VRuntime Avg-VRuntime in reweight_eevdf()
3694 * Since lag needs to be preserved through re-weight: in reweight_eevdf()
3696 * lag = (V - v)*w = (V'- v')*w', where v = v' in reweight_eevdf()
3697 * ==> V' = (V - v)*w/w' + v (1) in reweight_eevdf()
3702 * V' = (WV + w'v - wv) / (W + w' - w) (2) in reweight_eevdf()
3706 * (WV + w'v - wv) / (W + w' - w) = (V - v)*w/w' + v in reweight_eevdf()
3707 * ==> (WV-Wv+Wv+w'v-wv)/(W+w'-w) = (V - v)*w/w' + v in reweight_eevdf()
3708 * ==> (WV - Wv)/(W + w' - w) + v = (V - v)*w/w' + v in reweight_eevdf()
3709 * ==> (V - v)*W/(W + w' - w) = (V - v)*w/w' (3) in reweight_eevdf()
3711 * Since we are doing at !0-lag point which means V != v, we in reweight_eevdf()
3714 * ==> W / (W + w' - w) = w / w' in reweight_eevdf()
3715 * ==> Ww' = Ww + ww' - ww in reweight_eevdf()
3716 * ==> W * (w' - w) = w * (w' - w) in reweight_eevdf()
3717 * ==> W = w (re-weight indicates w' != w) in reweight_eevdf()
3721 * average vruntime @V, which means we will always re-weight in reweight_eevdf()
3722 * at 0-lag point, thus breach assumption. Proof completed. in reweight_eevdf()
3725 * COROLLARY #2: Re-weight does NOT affect weighted average in reweight_eevdf()
3730 * (V - v)*w = (V' - v')*w' in reweight_eevdf()
3731 * ==> v' = V' - (V - v)*w/w' (4) in reweight_eevdf()
3735 * V' = (WV - wv + w'v') / (W - w + w') in reweight_eevdf()
3736 * = (WV - wv + w'(V' - (V - v)w/w')) / (W - w + w') in reweight_eevdf()
3737 * = (WV - wv + w'V' - Vw + wv) / (W - w + w') in reweight_eevdf()
3738 * = (WV + w'V' - Vw) / (W - w + w') in reweight_eevdf()
3740 * ==> V'*(W - w + w') = WV + w'V' - Vw in reweight_eevdf()
3741 * ==> V' * (W - w) = (W - w) * V (5) in reweight_eevdf()
3744 * always occurs at 0-lag point, so V won't change. Or else in reweight_eevdf()
3749 * So according to corollary #1 & #2, the effect of re-weight in reweight_eevdf()
3752 * v' = V' - (V - v) * w / w' (4) in reweight_eevdf()
3753 * = V - (V - v) * w / w' in reweight_eevdf()
3754 * = V - vl * w / w' in reweight_eevdf()
3755 * = V - vl' in reweight_eevdf()
3757 if (avruntime != se->vruntime) { in reweight_eevdf()
3758 vlag = (s64)(avruntime - se->vruntime); in reweight_eevdf()
3760 se->vruntime = avruntime - vlag; in reweight_eevdf()
3770 * d' = v' + (d - v)*w/w' in reweight_eevdf()
3771 * = V' - (V - v)*w/w' + (d - v)*w/w' in reweight_eevdf()
3772 * = V - (V - v)*w/w' + (d - v)*w/w' in reweight_eevdf()
3773 * = V + (d - V)*w/w' in reweight_eevdf()
3775 vslice = (s64)(se->deadline - avruntime); in reweight_eevdf()
3777 se->deadline = avruntime + vslice; in reweight_eevdf()
3783 bool curr = cfs_rq->curr == se; in reweight_entity()
3785 if (se->on_rq) { in reweight_entity()
3791 update_load_sub(&cfs_rq->load, se->load.weight); in reweight_entity()
3795 if (!se->on_rq) { in reweight_entity()
3797 * Because we keep se->vlag = V - v_i, while: lag_i = w_i*(V - v_i), in reweight_entity()
3798 * we need to scale se->vlag when w_i changes. in reweight_entity()
3800 se->vlag = div_s64(se->vlag * se->load.weight, weight); in reweight_entity()
3805 update_load_set(&se->load, weight); in reweight_entity()
3809 u32 divider = get_pelt_divider(&se->avg); in reweight_entity()
3811 se->avg.load_avg = div_u64(se_weight(se) * se->avg.load_sum, divider); in reweight_entity()
3816 if (se->on_rq) { in reweight_entity()
3817 update_load_add(&cfs_rq->load, se->load.weight); in reweight_entity()
3823 * whether the rq-wide min_vruntime needs updated too. Since in reweight_entity()
3825 * than up-to-date one, we do the update at the end of the in reweight_entity()
3834 struct sched_entity *se = &p->se; in reweight_task()
3836 struct load_weight *load = &se->load; in reweight_task()
3840 load->inv_weight = sched_prio_to_wmult[prio]; in reweight_task()
3854 * tg->weight * grq->load.weight
3855 * ge->load.weight = ----------------------------- (1)
3856 * \Sum grq->load.weight
3864 * grq->load.weight -> grq->avg.load_avg (2)
3868 * tg->weight * grq->avg.load_avg
3869 * ge->load.weight = ------------------------------ (3)
3870 * tg->load_avg
3872 * Where: tg->load_avg ~= \Sum grq->avg.load_avg
3876 * The problem with it is that because the average is slow -- it was designed
3877 * to be exactly that of course -- this leads to transients in boundary
3879 * one task. It takes time for our CPU's grq->avg.load_avg to build up,
3884 * tg->weight * grq->load.weight
3885 * ge->load.weight = ----------------------------- = tg->weight (4)
3886 * grp->load.weight
3893 * ge->load.weight =
3895 * tg->weight * grq->load.weight
3896 * --------------------------------------------------- (5)
3897 * tg->load_avg - grq->avg.load_avg + grq->load.weight
3899 * But because grq->load.weight can drop to 0, resulting in a divide by zero,
3900 * we need to use grq->avg.load_avg as its lower bound, which then gives:
3903 * tg->weight * grq->load.weight
3904 * ge->load.weight = ----------------------------- (6)
3909 * tg_load_avg' = tg->load_avg - grq->avg.load_avg +
3910 * max(grq->load.weight, grq->avg.load_avg)
3914 * overestimates the ge->load.weight and therefore:
3916 * \Sum ge->load.weight >= tg->weight
3923 struct task_group *tg = cfs_rq->tg; in calc_group_shares()
3925 tg_shares = READ_ONCE(tg->shares); in calc_group_shares()
3927 load = max(scale_load_down(cfs_rq->load.weight), cfs_rq->avg.load_avg); in calc_group_shares()
3929 tg_weight = atomic_long_read(&tg->load_avg); in calc_group_shares()
3932 tg_weight -= cfs_rq->tg_load_avg_contrib; in calc_group_shares()
3940 * MIN_SHARES has to be unscaled here to support per-CPU partitioning in calc_group_shares()
3941 * of a group with small tg->shares value. It is a floor value which is in calc_group_shares()
3945 * E.g. on 64-bit for a group with tg->shares of scale_load(15)=15*1024 in calc_group_shares()
3946 * on an 8-core system with 8 tasks each runnable on one CPU shares has in calc_group_shares()
3971 shares = READ_ONCE(gcfs_rq->tg->shares); in update_cfs_group()
3975 if (unlikely(se->load.weight != shares)) in update_cfs_group()
3989 if (&rq->cfs == cfs_rq) { in cfs_rq_util_change()
3999 * As is, the util number is not freq-invariant (we'd have to in cfs_rq_util_change()
4011 if (sa->load_sum) in load_avg_is_decayed()
4014 if (sa->util_sum) in load_avg_is_decayed()
4017 if (sa->runnable_sum) in load_avg_is_decayed()
4025 SCHED_WARN_ON(sa->load_avg || in load_avg_is_decayed()
4026 sa->util_avg || in load_avg_is_decayed()
4027 sa->runnable_avg); in load_avg_is_decayed()
4034 return u64_u32_load_copy(cfs_rq->avg.last_update_time, in cfs_rq_last_update_time()
4035 cfs_rq->last_update_time_copy); in cfs_rq_last_update_time()
4041 * bottom-up, we only have to test whether the cfs_rq before us on the list
4051 if (cfs_rq->on_list) { in child_cfs_rq_on_list()
4052 prev = cfs_rq->leaf_cfs_rq_list.prev; in child_cfs_rq_on_list()
4056 prev = rq->tmp_alone_branch; in child_cfs_rq_on_list()
4061 return (prev_cfs_rq->tg->parent == cfs_rq->tg); in child_cfs_rq_on_list()
4066 if (cfs_rq->load.weight) in cfs_rq_is_decayed()
4069 if (!load_avg_is_decayed(&cfs_rq->avg)) in cfs_rq_is_decayed()
4079 * update_tg_load_avg - update the tg's load avg
4082 * This function 'ensures': tg->load_avg := \Sum tg->cfs_rq[]->avg.load.
4083 * However, because tg->load_avg is a global value there are performance
4087 * differential update where we store the last value we propagated. This in
4088 * turn allows skipping updates if the differential is 'small'.
4100 if (cfs_rq->tg == &root_task_group) in update_tg_load_avg()
4108 * For migration heavy workloads, access to tg->load_avg can be in update_tg_load_avg()
4112 if (now - cfs_rq->last_update_tg_load_avg < NSEC_PER_MSEC) in update_tg_load_avg()
4115 delta = cfs_rq->avg.load_avg - cfs_rq->tg_load_avg_contrib; in update_tg_load_avg()
4116 if (abs(delta) > cfs_rq->tg_load_avg_contrib / 64) { in update_tg_load_avg()
4117 atomic_long_add(delta, &cfs_rq->tg->load_avg); in update_tg_load_avg()
4118 cfs_rq->tg_load_avg_contrib = cfs_rq->avg.load_avg; in update_tg_load_avg()
4119 cfs_rq->last_update_tg_load_avg = now; in update_tg_load_avg()
4131 if (cfs_rq->tg == &root_task_group) in clear_tg_load_avg()
4135 delta = 0 - cfs_rq->tg_load_avg_contrib; in clear_tg_load_avg()
4136 atomic_long_add(delta, &cfs_rq->tg->load_avg); in clear_tg_load_avg()
4137 cfs_rq->tg_load_avg_contrib = 0; in clear_tg_load_avg()
4138 cfs_rq->last_update_tg_load_avg = now; in clear_tg_load_avg()
4157 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in clear_tg_offline_cfs_rqs()
4168 * caller only guarantees p->pi_lock is held; no other assumptions,
4169 * including the state of rq->lock, should be made.
4183 * getting what current time is, so simply throw away the out-of-date in set_task_rq_fair()
4187 if (!(se->avg.last_update_time && prev)) in set_task_rq_fair()
4194 se->avg.last_update_time = n_last_update_time; in set_task_rq_fair()
4202 * ge->avg == grq->avg (1)
4213 * ge->avg.load_avg = ge->load.weight * ge->avg.runnable_avg (2)
4218 * grq->avg.load_avg = grq->load.weight * grq->avg.runnable_avg (3)
4222 * ge->avg.runnable_avg == grq->avg.runnable_avg
4226 * ge->load.weight * grq->avg.load_avg
4227 * ge->avg.load_avg = ----------------------------------- (4)
4228 * grq->load.weight
4241 * Another reason this doesn't work is that runnable isn't a 0-sum entity.
4252 * ge->avg.running_sum <= ge->avg.runnable_sum <= LOAD_AVG_MAX
4259 * grq->avg.runnable_sum = grq->avg.load_sum / grq->load.weight
4267 long delta_sum, delta_avg = gcfs_rq->avg.util_avg - se->avg.util_avg; in update_tg_cfs_util()
4275 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in update_tg_cfs_util()
4278 divider = get_pelt_divider(&cfs_rq->avg); in update_tg_cfs_util()
4282 se->avg.util_avg = gcfs_rq->avg.util_avg; in update_tg_cfs_util()
4283 new_sum = se->avg.util_avg * divider; in update_tg_cfs_util()
4284 delta_sum = (long)new_sum - (long)se->avg.util_sum; in update_tg_cfs_util()
4285 se->avg.util_sum = new_sum; in update_tg_cfs_util()
4288 add_positive(&cfs_rq->avg.util_avg, delta_avg); in update_tg_cfs_util()
4289 add_positive(&cfs_rq->avg.util_sum, delta_sum); in update_tg_cfs_util()
4292 cfs_rq->avg.util_sum = max_t(u32, cfs_rq->avg.util_sum, in update_tg_cfs_util()
4293 cfs_rq->avg.util_avg * PELT_MIN_DIVIDER); in update_tg_cfs_util()
4299 long delta_sum, delta_avg = gcfs_rq->avg.runnable_avg - se->avg.runnable_avg; in update_tg_cfs_runnable()
4307 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in update_tg_cfs_runnable()
4310 divider = get_pelt_divider(&cfs_rq->avg); in update_tg_cfs_runnable()
4313 se->avg.runnable_avg = gcfs_rq->avg.runnable_avg; in update_tg_cfs_runnable()
4314 new_sum = se->avg.runnable_avg * divider; in update_tg_cfs_runnable()
4315 delta_sum = (long)new_sum - (long)se->avg.runnable_sum; in update_tg_cfs_runnable()
4316 se->avg.runnable_sum = new_sum; in update_tg_cfs_runnable()
4319 add_positive(&cfs_rq->avg.runnable_avg, delta_avg); in update_tg_cfs_runnable()
4320 add_positive(&cfs_rq->avg.runnable_sum, delta_sum); in update_tg_cfs_runnable()
4322 cfs_rq->avg.runnable_sum = max_t(u32, cfs_rq->avg.runnable_sum, in update_tg_cfs_runnable()
4323 cfs_rq->avg.runnable_avg * PELT_MIN_DIVIDER); in update_tg_cfs_runnable()
4329 long delta_avg, running_sum, runnable_sum = gcfs_rq->prop_runnable_sum; in update_tg_cfs_load()
4338 gcfs_rq->prop_runnable_sum = 0; in update_tg_cfs_load()
4341 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in update_tg_cfs_load()
4344 divider = get_pelt_divider(&cfs_rq->avg); in update_tg_cfs_load()
4351 runnable_sum += se->avg.load_sum; in update_tg_cfs_load()
4358 if (scale_load_down(gcfs_rq->load.weight)) { in update_tg_cfs_load()
4359 load_sum = div_u64(gcfs_rq->avg.load_sum, in update_tg_cfs_load()
4360 scale_load_down(gcfs_rq->load.weight)); in update_tg_cfs_load()
4364 runnable_sum = min(se->avg.load_sum, load_sum); in update_tg_cfs_load()
4373 running_sum = se->avg.util_sum >> SCHED_CAPACITY_SHIFT; in update_tg_cfs_load()
4379 delta_avg = load_avg - se->avg.load_avg; in update_tg_cfs_load()
4383 delta_sum = load_sum - (s64)se_weight(se) * se->avg.load_sum; in update_tg_cfs_load()
4385 se->avg.load_sum = runnable_sum; in update_tg_cfs_load()
4386 se->avg.load_avg = load_avg; in update_tg_cfs_load()
4387 add_positive(&cfs_rq->avg.load_avg, delta_avg); in update_tg_cfs_load()
4388 add_positive(&cfs_rq->avg.load_sum, delta_sum); in update_tg_cfs_load()
4390 cfs_rq->avg.load_sum = max_t(u32, cfs_rq->avg.load_sum, in update_tg_cfs_load()
4391 cfs_rq->avg.load_avg * PELT_MIN_DIVIDER); in update_tg_cfs_load()
4396 cfs_rq->propagate = 1; in add_tg_cfs_propagate()
4397 cfs_rq->prop_runnable_sum += runnable_sum; in add_tg_cfs_propagate()
4409 if (!gcfs_rq->propagate) in propagate_entity_load_avg()
4412 gcfs_rq->propagate = 0; in propagate_entity_load_avg()
4416 add_tg_cfs_propagate(cfs_rq, gcfs_rq->prop_runnable_sum); in propagate_entity_load_avg()
4440 if (se->avg.load_avg || se->avg.util_avg) in skip_blocked_update()
4447 if (gcfs_rq->propagate) in skip_blocked_update()
4481 if (load_avg_is_decayed(&se->avg)) in migrate_se_pelt_lag()
4488 is_idle = is_idle_task(rcu_dereference(rq->curr)); in migrate_se_pelt_lag()
4505 * - cfs->throttled_clock_pelt_time@cfs_rq_idle in migrate_se_pelt_lag()
4508 * = rq_clock_pelt()@rq_idle - rq_clock_pelt()@cfs_rq_idle in migrate_se_pelt_lag()
4511 * = sched_clock_cpu() - rq_clock()@rq_idle in migrate_se_pelt_lag()
4515 * now = rq_clock_pelt()@rq_idle - cfs->throttled_clock_pelt_time + in migrate_se_pelt_lag()
4516 * sched_clock_cpu() - rq_clock()@rq_idle in migrate_se_pelt_lag()
4518 * rq_clock_pelt()@rq_idle is rq->clock_pelt_idle in migrate_se_pelt_lag()
4519 * rq_clock()@rq_idle is rq->clock_idle in migrate_se_pelt_lag()
4520 * cfs->throttled_clock_pelt_time@cfs_rq_idle in migrate_se_pelt_lag()
4521 * is cfs_rq->throttled_pelt_idle in migrate_se_pelt_lag()
4525 throttled = u64_u32_load(cfs_rq->throttled_pelt_idle); in migrate_se_pelt_lag()
4530 now = u64_u32_load(rq->clock_pelt_idle); in migrate_se_pelt_lag()
4540 now -= throttled; in migrate_se_pelt_lag()
4543 * cfs_rq->avg.last_update_time is more recent than our in migrate_se_pelt_lag()
4548 now += sched_clock_cpu(cpu_of(rq)) - u64_u32_load(rq->clock_idle); in migrate_se_pelt_lag()
4557 * update_cfs_rq_load_avg - update the cfs_rq's load/util averages
4564 * cfs_rq->avg is used for task_h_load() and update_cfs_share() for example.
4568 * Since both these conditions indicate a changed cfs_rq->avg.load we should
4575 struct sched_avg *sa = &cfs_rq->avg; in update_cfs_rq_load_avg()
4578 if (cfs_rq->removed.nr) { in update_cfs_rq_load_avg()
4580 u32 divider = get_pelt_divider(&cfs_rq->avg); in update_cfs_rq_load_avg()
4582 raw_spin_lock(&cfs_rq->removed.lock); in update_cfs_rq_load_avg()
4583 swap(cfs_rq->removed.util_avg, removed_util); in update_cfs_rq_load_avg()
4584 swap(cfs_rq->removed.load_avg, removed_load); in update_cfs_rq_load_avg()
4585 swap(cfs_rq->removed.runnable_avg, removed_runnable); in update_cfs_rq_load_avg()
4586 cfs_rq->removed.nr = 0; in update_cfs_rq_load_avg()
4587 raw_spin_unlock(&cfs_rq->removed.lock); in update_cfs_rq_load_avg()
4590 sub_positive(&sa->load_avg, r); in update_cfs_rq_load_avg()
4591 sub_positive(&sa->load_sum, r * divider); in update_cfs_rq_load_avg()
4592 /* See sa->util_sum below */ in update_cfs_rq_load_avg()
4593 sa->load_sum = max_t(u32, sa->load_sum, sa->load_avg * PELT_MIN_DIVIDER); in update_cfs_rq_load_avg()
4596 sub_positive(&sa->util_avg, r); in update_cfs_rq_load_avg()
4597 sub_positive(&sa->util_sum, r * divider); in update_cfs_rq_load_avg()
4599 * Because of rounding, se->util_sum might ends up being +1 more than in update_cfs_rq_load_avg()
4600 * cfs->util_sum. Although this is not a problem by itself, detaching in update_cfs_rq_load_avg()
4602 * util_avg (~1ms) can make cfs->util_sum becoming null whereas in update_cfs_rq_load_avg()
4609 sa->util_sum = max_t(u32, sa->util_sum, sa->util_avg * PELT_MIN_DIVIDER); in update_cfs_rq_load_avg()
4612 sub_positive(&sa->runnable_avg, r); in update_cfs_rq_load_avg()
4613 sub_positive(&sa->runnable_sum, r * divider); in update_cfs_rq_load_avg()
4614 /* See sa->util_sum above */ in update_cfs_rq_load_avg()
4615 sa->runnable_sum = max_t(u32, sa->runnable_sum, in update_cfs_rq_load_avg()
4616 sa->runnable_avg * PELT_MIN_DIVIDER); in update_cfs_rq_load_avg()
4623 -(long)(removed_runnable * divider) >> SCHED_CAPACITY_SHIFT); in update_cfs_rq_load_avg()
4629 u64_u32_store_copy(sa->last_update_time, in update_cfs_rq_load_avg()
4630 cfs_rq->last_update_time_copy, in update_cfs_rq_load_avg()
4631 sa->last_update_time); in update_cfs_rq_load_avg()
4636 * attach_entity_load_avg - attach this entity to its cfs_rq load avg
4641 * cfs_rq->avg.last_update_time being current.
4646 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in attach_entity_load_avg()
4649 u32 divider = get_pelt_divider(&cfs_rq->avg); in attach_entity_load_avg()
4658 se->avg.last_update_time = cfs_rq->avg.last_update_time; in attach_entity_load_avg()
4659 se->avg.period_contrib = cfs_rq->avg.period_contrib; in attach_entity_load_avg()
4667 se->avg.util_sum = se->avg.util_avg * divider; in attach_entity_load_avg()
4669 se->avg.runnable_sum = se->avg.runnable_avg * divider; in attach_entity_load_avg()
4671 se->avg.load_sum = se->avg.load_avg * divider; in attach_entity_load_avg()
4672 if (se_weight(se) < se->avg.load_sum) in attach_entity_load_avg()
4673 se->avg.load_sum = div_u64(se->avg.load_sum, se_weight(se)); in attach_entity_load_avg()
4675 se->avg.load_sum = 1; in attach_entity_load_avg()
4678 cfs_rq->avg.util_avg += se->avg.util_avg; in attach_entity_load_avg()
4679 cfs_rq->avg.util_sum += se->avg.util_sum; in attach_entity_load_avg()
4680 cfs_rq->avg.runnable_avg += se->avg.runnable_avg; in attach_entity_load_avg()
4681 cfs_rq->avg.runnable_sum += se->avg.runnable_sum; in attach_entity_load_avg()
4683 add_tg_cfs_propagate(cfs_rq, se->avg.load_sum); in attach_entity_load_avg()
4691 * detach_entity_load_avg - detach this entity from its cfs_rq load avg
4696 * cfs_rq->avg.last_update_time being current.
4701 sub_positive(&cfs_rq->avg.util_avg, se->avg.util_avg); in detach_entity_load_avg()
4702 sub_positive(&cfs_rq->avg.util_sum, se->avg.util_sum); in detach_entity_load_avg()
4704 cfs_rq->avg.util_sum = max_t(u32, cfs_rq->avg.util_sum, in detach_entity_load_avg()
4705 cfs_rq->avg.util_avg * PELT_MIN_DIVIDER); in detach_entity_load_avg()
4707 sub_positive(&cfs_rq->avg.runnable_avg, se->avg.runnable_avg); in detach_entity_load_avg()
4708 sub_positive(&cfs_rq->avg.runnable_sum, se->avg.runnable_sum); in detach_entity_load_avg()
4710 cfs_rq->avg.runnable_sum = max_t(u32, cfs_rq->avg.runnable_sum, in detach_entity_load_avg()
4711 cfs_rq->avg.runnable_avg * PELT_MIN_DIVIDER); in detach_entity_load_avg()
4713 add_tg_cfs_propagate(cfs_rq, -se->avg.load_sum); in detach_entity_load_avg()
4738 if (se->avg.last_update_time && !(flags & SKIP_AGE_LOAD)) in update_load_avg()
4744 if (!se->avg.last_update_time && (flags & DO_ATTACH)) { in update_load_avg()
4794 * tasks cannot exit without having gone through wake_up_new_task() -> in remove_entity_load_avg()
4801 raw_spin_lock_irqsave(&cfs_rq->removed.lock, flags); in remove_entity_load_avg()
4802 ++cfs_rq->removed.nr; in remove_entity_load_avg()
4803 cfs_rq->removed.util_avg += se->avg.util_avg; in remove_entity_load_avg()
4804 cfs_rq->removed.load_avg += se->avg.load_avg; in remove_entity_load_avg()
4805 cfs_rq->removed.runnable_avg += se->avg.runnable_avg; in remove_entity_load_avg()
4806 raw_spin_unlock_irqrestore(&cfs_rq->removed.lock, flags); in remove_entity_load_avg()
4811 return cfs_rq->avg.runnable_avg; in cfs_rq_runnable_avg()
4816 return cfs_rq->avg.load_avg; in cfs_rq_load_avg()
4823 return READ_ONCE(p->se.avg.util_avg); in task_util()
4828 return READ_ONCE(p->se.avg.runnable_avg); in task_runnable()
4833 return READ_ONCE(p->se.avg.util_est) & ~UTIL_AVG_UNCHANGED; in _task_util_est()
4850 enqueued = cfs_rq->avg.util_est; in util_est_enqueue()
4852 WRITE_ONCE(cfs_rq->avg.util_est, enqueued); in util_est_enqueue()
4866 enqueued = cfs_rq->avg.util_est; in util_est_dequeue()
4867 enqueued -= min_t(unsigned int, enqueued, _task_util_est(p)); in util_est_dequeue()
4868 WRITE_ONCE(cfs_rq->avg.util_est, enqueued); in util_est_dequeue()
4892 ewma = READ_ONCE(p->se.avg.util_est); in util_est_update()
4917 last_ewma_diff = ewma - dequeued; in util_est_update()
4943 * ewma(t) = w * task_util(p) + (1-w) * ewma(t-1) in util_est_update()
4944 * = w * task_util(p) + ewma(t-1) - w * ewma(t-1) in util_est_update()
4945 * = w * (task_util(p) - ewma(t-1)) + ewma(t-1) in util_est_update()
4946 * = w * ( -last_ewma_diff ) + ewma(t-1) in util_est_update()
4947 * = w * (-last_ewma_diff + ewma(t-1) / w) in util_est_update()
4953 ewma -= last_ewma_diff; in util_est_update()
4957 WRITE_ONCE(p->se.avg.util_est, ewma); in util_est_update()
4959 trace_sched_util_est_se_tp(&p->se); in util_est_update()
5000 capacity_orig_thermal = capacity_orig - arch_scale_thermal_pressure(cpu); in util_fits_cpu()
5016 * +---------------------------------------- in util_fits_cpu()
5054 * +---------------------------------------- in util_fits_cpu()
5076 return -1; in util_fits_cpu()
5098 if (!p || p->nr_cpus_allowed == 1) { in update_misfit_status()
5099 rq->misfit_task_load = 0; in update_misfit_status()
5104 rq->misfit_task_load = 0; in update_misfit_status()
5112 rq->misfit_task_load = max_t(unsigned long, task_h_load(p), 1); in update_misfit_status()
5119 return !cfs_rq->nr_running; in cfs_rq_is_decayed()
5163 se->slice = sysctl_sched_base_slice; in place_entity()
5164 vslice = calc_delta_fair(se->slice, se); in place_entity()
5174 if (sched_feat(PLACE_LAG) && cfs_rq->nr_running) { in place_entity()
5175 struct sched_entity *curr = cfs_rq->curr; in place_entity()
5178 lag = se->vlag; in place_entity()
5188 * lag_i = S - s_i = w_i * (V - v_i) in place_entity()
5193 * vl_i = V - v_i <=> v_i = V - vl_i in place_entity()
5205 * = (W*V + w_i*(V - vl_i)) / (W + w_i) in place_entity()
5206 * = (W*V + w_i*V - w_i*vl_i) / (W + w_i) in place_entity()
5207 * = (V*(W + w_i) - w_i*l) / (W + w_i) in place_entity()
5208 * = V - w_i*vl_i / (W + w_i) in place_entity()
5212 * vl'_i = V' - v_i in place_entity()
5213 * = V - w_i*vl_i / (W + w_i) - (V - vl_i) in place_entity()
5214 * = vl_i - w_i*vl_i / (W + w_i) in place_entity()
5224 * vl'_i = vl_i - w_i*vl_i / (W + w_i) in place_entity()
5225 * = ((W + w_i)*vl_i - w_i*vl_i) / (W + w_i) in place_entity()
5227 * (W + w_i)*vl'_i = (W + w_i)*vl_i - w_i*vl_i in place_entity()
5232 load = cfs_rq->avg_load; in place_entity()
5233 if (curr && curr->on_rq) in place_entity()
5234 load += scale_load_down(curr->load.weight); in place_entity()
5236 lag *= load + scale_load_down(se->load.weight); in place_entity()
5242 se->vruntime = vruntime - lag; in place_entity()
5255 se->deadline = se->vruntime + vslice; in place_entity()
5266 bool curr = cfs_rq->curr == se; in enqueue_entity()
5279 * - Update loads to have both entity and cfs_rq synced with now. in enqueue_entity()
5280 * - For group_entity, update its runnable_weight to reflect the new in enqueue_entity()
5282 * - For group_entity, update its weight to reflect the new share of in enqueue_entity()
5284 * - Add its new weight to cfs_rq->load.weight in enqueue_entity()
5290 * but update_cfs_group() here will re-adjust the weight and have to in enqueue_entity()
5296 * XXX now that the entity has been re-weighted, and it's lag adjusted, in enqueue_entity()
5306 se->exec_start = 0; in enqueue_entity()
5312 se->on_rq = 1; in enqueue_entity()
5314 if (cfs_rq->nr_running == 1) { in enqueue_entity()
5322 if (cfs_rq_throttled(cfs_rq) && !cfs_rq->throttled_clock) in enqueue_entity()
5323 cfs_rq->throttled_clock = rq_clock(rq); in enqueue_entity()
5324 if (!cfs_rq->throttled_clock_self) in enqueue_entity()
5325 cfs_rq->throttled_clock_self = rq_clock(rq); in enqueue_entity()
5335 if (cfs_rq->next != se) in __clear_buddies_next()
5338 cfs_rq->next = NULL; in __clear_buddies_next()
5344 if (cfs_rq->next == se) in clear_buddies()
5359 * Update run-time statistics of the 'current'. in dequeue_entity()
5365 * - Update loads to have both entity and cfs_rq synced with now. in dequeue_entity()
5366 * - For group_entity, update its runnable_weight to reflect the new in dequeue_entity()
5368 * - Subtract its previous weight from cfs_rq->load.weight. in dequeue_entity()
5369 * - For group entity, update its weight to reflect the new share in dequeue_entity()
5380 if (se != cfs_rq->curr) in dequeue_entity()
5382 se->on_rq = 0; in dequeue_entity()
5394 * further than we started -- ie. we'll be penalized. in dequeue_entity()
5399 if (cfs_rq->nr_running == 0) in dequeue_entity()
5409 if (se->on_rq) { in set_next_entity()
5422 se->vlag = se->deadline; in set_next_entity()
5426 cfs_rq->curr = se; in set_next_entity()
5431 * when there are only lesser-weight tasks around): in set_next_entity()
5434 rq_of(cfs_rq)->cfs.load.weight >= 2*se->load.weight) { in set_next_entity()
5438 __schedstat_set(stats->slice_max, in set_next_entity()
5439 max((u64)stats->slice_max, in set_next_entity()
5440 se->sum_exec_runtime - se->prev_sum_exec_runtime)); in set_next_entity()
5443 se->prev_sum_exec_runtime = se->sum_exec_runtime; in set_next_entity()
5460 cfs_rq->next && entity_eligible(cfs_rq, cfs_rq->next)) in pick_next_entity()
5461 return cfs_rq->next; in pick_next_entity()
5474 if (prev->on_rq) in put_prev_entity()
5480 if (prev->on_rq) { in put_prev_entity()
5487 cfs_rq->curr = NULL; in put_prev_entity()
5494 * Update run-time statistics of the 'current'. in entity_tick()
5517 hrtimer_active(&rq_of(cfs_rq)->hrtick_timer)) in entity_tick()
5572 * directly instead of rq->clock to avoid adding additional synchronization
5573 * around rq->lock.
5575 * requires cfs_b->lock
5581 if (unlikely(cfs_b->quota == RUNTIME_INF)) in __refill_cfs_bandwidth_runtime()
5584 cfs_b->runtime += cfs_b->quota; in __refill_cfs_bandwidth_runtime()
5585 runtime = cfs_b->runtime_snap - cfs_b->runtime; in __refill_cfs_bandwidth_runtime()
5587 cfs_b->burst_time += runtime; in __refill_cfs_bandwidth_runtime()
5588 cfs_b->nr_burst++; in __refill_cfs_bandwidth_runtime()
5591 cfs_b->runtime = min(cfs_b->runtime, cfs_b->quota + cfs_b->burst); in __refill_cfs_bandwidth_runtime()
5592 cfs_b->runtime_snap = cfs_b->runtime; in __refill_cfs_bandwidth_runtime()
5597 return &tg->cfs_bandwidth; in tg_cfs_bandwidth()
5606 lockdep_assert_held(&cfs_b->lock); in __assign_cfs_rq_runtime()
5609 min_amount = target_runtime - cfs_rq->runtime_remaining; in __assign_cfs_rq_runtime()
5611 if (cfs_b->quota == RUNTIME_INF) in __assign_cfs_rq_runtime()
5616 if (cfs_b->runtime > 0) { in __assign_cfs_rq_runtime()
5617 amount = min(cfs_b->runtime, min_amount); in __assign_cfs_rq_runtime()
5618 cfs_b->runtime -= amount; in __assign_cfs_rq_runtime()
5619 cfs_b->idle = 0; in __assign_cfs_rq_runtime()
5623 cfs_rq->runtime_remaining += amount; in __assign_cfs_rq_runtime()
5625 return cfs_rq->runtime_remaining > 0; in __assign_cfs_rq_runtime()
5631 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in assign_cfs_rq_runtime()
5634 raw_spin_lock(&cfs_b->lock); in assign_cfs_rq_runtime()
5636 raw_spin_unlock(&cfs_b->lock); in assign_cfs_rq_runtime()
5644 cfs_rq->runtime_remaining -= delta_exec; in __account_cfs_rq_runtime()
5646 if (likely(cfs_rq->runtime_remaining > 0)) in __account_cfs_rq_runtime()
5649 if (cfs_rq->throttled) in __account_cfs_rq_runtime()
5655 if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr)) in __account_cfs_rq_runtime()
5662 if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled) in account_cfs_rq_runtime()
5670 return cfs_bandwidth_used() && cfs_rq->throttled; in cfs_rq_throttled()
5676 return cfs_bandwidth_used() && cfs_rq->throttle_count; in throttled_hierarchy()
5682 * load-balance operations.
5689 src_cfs_rq = tg->cfs_rq[src_cpu]; in throttled_lb_pair()
5690 dest_cfs_rq = tg->cfs_rq[dest_cpu]; in throttled_lb_pair()
5699 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in tg_unthrottle_up()
5701 cfs_rq->throttle_count--; in tg_unthrottle_up()
5702 if (!cfs_rq->throttle_count) { in tg_unthrottle_up()
5703 cfs_rq->throttled_clock_pelt_time += rq_clock_pelt(rq) - in tg_unthrottle_up()
5704 cfs_rq->throttled_clock_pelt; in tg_unthrottle_up()
5710 if (cfs_rq->throttled_clock_self) { in tg_unthrottle_up()
5711 u64 delta = rq_clock(rq) - cfs_rq->throttled_clock_self; in tg_unthrottle_up()
5713 cfs_rq->throttled_clock_self = 0; in tg_unthrottle_up()
5718 cfs_rq->throttled_clock_self_time += delta; in tg_unthrottle_up()
5728 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in tg_throttle_down()
5731 if (!cfs_rq->throttle_count) { in tg_throttle_down()
5732 cfs_rq->throttled_clock_pelt = rq_clock_pelt(rq); in tg_throttle_down()
5735 SCHED_WARN_ON(cfs_rq->throttled_clock_self); in tg_throttle_down()
5736 if (cfs_rq->nr_running) in tg_throttle_down()
5737 cfs_rq->throttled_clock_self = rq_clock(rq); in tg_throttle_down()
5739 cfs_rq->throttle_count++; in tg_throttle_down()
5747 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in throttle_cfs_rq()
5751 raw_spin_lock(&cfs_b->lock); in throttle_cfs_rq()
5764 list_add_tail_rcu(&cfs_rq->throttled_list, in throttle_cfs_rq()
5765 &cfs_b->throttled_cfs_rq); in throttle_cfs_rq()
5767 raw_spin_unlock(&cfs_b->lock); in throttle_cfs_rq()
5772 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))]; in throttle_cfs_rq()
5776 walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq); in throttle_cfs_rq()
5779 task_delta = cfs_rq->h_nr_running; in throttle_cfs_rq()
5780 idle_task_delta = cfs_rq->idle_h_nr_running; in throttle_cfs_rq()
5783 /* throttled entity or throttle-on-deactivate */ in throttle_cfs_rq()
5784 if (!se->on_rq) in throttle_cfs_rq()
5790 idle_task_delta = cfs_rq->h_nr_running; in throttle_cfs_rq()
5792 qcfs_rq->h_nr_running -= task_delta; in throttle_cfs_rq()
5793 qcfs_rq->idle_h_nr_running -= idle_task_delta; in throttle_cfs_rq()
5795 if (qcfs_rq->load.weight) { in throttle_cfs_rq()
5796 /* Avoid re-evaluating load for this entity: */ in throttle_cfs_rq()
5804 /* throttled entity or throttle-on-deactivate */ in throttle_cfs_rq()
5805 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()
5824 * throttled-list. rq->lock protects completion. in throttle_cfs_rq()
5826 cfs_rq->throttled = 1; in throttle_cfs_rq()
5827 SCHED_WARN_ON(cfs_rq->throttled_clock); in throttle_cfs_rq()
5828 if (cfs_rq->nr_running) in throttle_cfs_rq()
5829 cfs_rq->throttled_clock = rq_clock(rq); in throttle_cfs_rq()
5836 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in unthrottle_cfs_rq()
5840 se = cfs_rq->tg->se[cpu_of(rq)]; in unthrottle_cfs_rq()
5842 cfs_rq->throttled = 0; in unthrottle_cfs_rq()
5846 raw_spin_lock(&cfs_b->lock); in unthrottle_cfs_rq()
5847 if (cfs_rq->throttled_clock) { in unthrottle_cfs_rq()
5848 cfs_b->throttled_time += rq_clock(rq) - cfs_rq->throttled_clock; in unthrottle_cfs_rq()
5849 cfs_rq->throttled_clock = 0; in unthrottle_cfs_rq()
5851 list_del_rcu(&cfs_rq->throttled_list); in unthrottle_cfs_rq()
5852 raw_spin_unlock(&cfs_b->lock); in unthrottle_cfs_rq()
5855 walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq); in unthrottle_cfs_rq()
5857 if (!cfs_rq->load.weight) { in unthrottle_cfs_rq()
5858 if (!cfs_rq->on_list) in unthrottle_cfs_rq()
5871 task_delta = cfs_rq->h_nr_running; in unthrottle_cfs_rq()
5872 idle_task_delta = cfs_rq->idle_h_nr_running; in unthrottle_cfs_rq()
5876 if (se->on_rq) in unthrottle_cfs_rq()
5881 idle_task_delta = cfs_rq->h_nr_running; in unthrottle_cfs_rq()
5883 qcfs_rq->h_nr_running += task_delta; in unthrottle_cfs_rq()
5884 qcfs_rq->idle_h_nr_running += idle_task_delta; in unthrottle_cfs_rq()
5898 idle_task_delta = cfs_rq->h_nr_running; in unthrottle_cfs_rq()
5900 qcfs_rq->h_nr_running += task_delta; in unthrottle_cfs_rq()
5901 qcfs_rq->idle_h_nr_running += idle_task_delta; in unthrottle_cfs_rq()
5915 if (rq->curr == rq->idle && rq->cfs.nr_running) in unthrottle_cfs_rq()
5939 * fact that we pair with sched_free_group_rcu(), so that we cannot in __cfsb_csd_unthrottle()
5945 list_for_each_entry_safe(cursor, tmp, &rq->cfsb_csd_list, in __cfsb_csd_unthrottle()
5947 list_del_init(&cursor->throttled_csd_list); in __cfsb_csd_unthrottle()
5970 if (SCHED_WARN_ON(!list_empty(&cfs_rq->throttled_csd_list))) in __unthrottle_cfs_rq_async()
5973 first = list_empty(&rq->cfsb_csd_list); in __unthrottle_cfs_rq_async()
5974 list_add_tail(&cfs_rq->throttled_csd_list, &rq->cfsb_csd_list); in __unthrottle_cfs_rq_async()
5976 smp_call_function_single_async(cpu_of(rq), &rq->cfsb_csd); in __unthrottle_cfs_rq_async()
5990 cfs_rq->runtime_remaining <= 0)) in unthrottle_cfs_rq_async()
6007 list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq, in distribute_cfs_runtime()
6021 if (!list_empty(&cfs_rq->throttled_csd_list)) in distribute_cfs_runtime()
6025 SCHED_WARN_ON(cfs_rq->runtime_remaining > 0); in distribute_cfs_runtime()
6027 raw_spin_lock(&cfs_b->lock); in distribute_cfs_runtime()
6028 runtime = -cfs_rq->runtime_remaining + 1; in distribute_cfs_runtime()
6029 if (runtime > cfs_b->runtime) in distribute_cfs_runtime()
6030 runtime = cfs_b->runtime; in distribute_cfs_runtime()
6031 cfs_b->runtime -= runtime; in distribute_cfs_runtime()
6032 remaining = cfs_b->runtime; in distribute_cfs_runtime()
6033 raw_spin_unlock(&cfs_b->lock); in distribute_cfs_runtime()
6035 cfs_rq->runtime_remaining += runtime; in distribute_cfs_runtime()
6038 if (cfs_rq->runtime_remaining > 0) { in distribute_cfs_runtime()
6047 list_add_tail(&cfs_rq->throttled_csd_list, in distribute_cfs_runtime()
6064 list_del_init(&cfs_rq->throttled_csd_list); in distribute_cfs_runtime()
6081 * period the timer is deactivated until scheduling resumes; cfs_b->idle is
6089 if (cfs_b->quota == RUNTIME_INF) in do_sched_cfs_period_timer()
6092 throttled = !list_empty(&cfs_b->throttled_cfs_rq); in do_sched_cfs_period_timer()
6093 cfs_b->nr_periods += overrun; in do_sched_cfs_period_timer()
6095 /* Refill extra burst quota even if cfs_b->idle */ in do_sched_cfs_period_timer()
6102 if (cfs_b->idle && !throttled) in do_sched_cfs_period_timer()
6107 cfs_b->idle = 1; in do_sched_cfs_period_timer()
6112 cfs_b->nr_throttled += overrun; in do_sched_cfs_period_timer()
6115 * This check is repeated as we release cfs_b->lock while we unthrottle. in do_sched_cfs_period_timer()
6117 while (throttled && cfs_b->runtime > 0) { in do_sched_cfs_period_timer()
6118 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in do_sched_cfs_period_timer()
6119 /* we can't nest cfs_b->lock while distributing bandwidth */ in do_sched_cfs_period_timer()
6121 raw_spin_lock_irqsave(&cfs_b->lock, flags); in do_sched_cfs_period_timer()
6130 cfs_b->idle = 0; in do_sched_cfs_period_timer()
6148 * Requires cfs_b->lock for hrtimer_expires_remaining to be safe against the
6154 struct hrtimer *refresh_timer = &cfs_b->period_timer; in runtime_refresh_within()
6157 /* if the call-back is running a quota refresh is already occurring */ in runtime_refresh_within()
6178 if (cfs_b->slack_started) in start_cfs_slack_bandwidth()
6180 cfs_b->slack_started = true; in start_cfs_slack_bandwidth()
6182 hrtimer_start(&cfs_b->slack_timer, in start_cfs_slack_bandwidth()
6190 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in __return_cfs_rq_runtime()
6191 s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime; in __return_cfs_rq_runtime()
6196 raw_spin_lock(&cfs_b->lock); in __return_cfs_rq_runtime()
6197 if (cfs_b->quota != RUNTIME_INF) { in __return_cfs_rq_runtime()
6198 cfs_b->runtime += slack_runtime; in __return_cfs_rq_runtime()
6200 /* we are under rq->lock, defer unthrottling using a timer */ in __return_cfs_rq_runtime()
6201 if (cfs_b->runtime > sched_cfs_bandwidth_slice() && in __return_cfs_rq_runtime()
6202 !list_empty(&cfs_b->throttled_cfs_rq)) in __return_cfs_rq_runtime()
6205 raw_spin_unlock(&cfs_b->lock); in __return_cfs_rq_runtime()
6208 cfs_rq->runtime_remaining -= slack_runtime; in __return_cfs_rq_runtime()
6216 if (!cfs_rq->runtime_enabled || cfs_rq->nr_running) in return_cfs_rq_runtime()
6224 * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
6232 raw_spin_lock_irqsave(&cfs_b->lock, flags); in do_sched_cfs_slack_timer()
6233 cfs_b->slack_started = false; in do_sched_cfs_slack_timer()
6236 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in do_sched_cfs_slack_timer()
6240 if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) in do_sched_cfs_slack_timer()
6241 runtime = cfs_b->runtime; in do_sched_cfs_slack_timer()
6243 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in do_sched_cfs_slack_timer()
6254 * runtime as update_curr() throttling can not trigger until it's on-rq.
6261 /* an active group must be handled by the update_curr()->put() path */ in check_enqueue_throttle()
6262 if (!cfs_rq->runtime_enabled || cfs_rq->curr) in check_enqueue_throttle()
6271 if (cfs_rq->runtime_remaining <= 0) in check_enqueue_throttle()
6282 if (!tg->parent) in sync_throttle()
6285 cfs_rq = tg->cfs_rq[cpu]; in sync_throttle()
6286 pcfs_rq = tg->parent->cfs_rq[cpu]; in sync_throttle()
6288 cfs_rq->throttle_count = pcfs_rq->throttle_count; in sync_throttle()
6289 cfs_rq->throttled_clock_pelt = rq_clock_pelt(cpu_rq(cpu)); in sync_throttle()
6298 if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0)) in check_cfs_rq_runtime()
6332 raw_spin_lock_irqsave(&cfs_b->lock, flags); in sched_cfs_period_timer()
6334 overrun = hrtimer_forward_now(timer, cfs_b->period); in sched_cfs_period_timer()
6341 u64 new, old = ktime_to_ns(cfs_b->period); in sched_cfs_period_timer()
6350 cfs_b->period = ns_to_ktime(new); in sched_cfs_period_timer()
6351 cfs_b->quota *= 2; in sched_cfs_period_timer()
6352 cfs_b->burst *= 2; in sched_cfs_period_timer()
6358 div_u64(cfs_b->quota, NSEC_PER_USEC)); in sched_cfs_period_timer()
6364 div_u64(cfs_b->quota, NSEC_PER_USEC)); in sched_cfs_period_timer()
6372 cfs_b->period_active = 0; in sched_cfs_period_timer()
6373 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in sched_cfs_period_timer()
6380 raw_spin_lock_init(&cfs_b->lock); in init_cfs_bandwidth()
6381 cfs_b->runtime = 0; in init_cfs_bandwidth()
6382 cfs_b->quota = RUNTIME_INF; in init_cfs_bandwidth()
6383 cfs_b->period = ns_to_ktime(default_cfs_period()); in init_cfs_bandwidth()
6384 cfs_b->burst = 0; in init_cfs_bandwidth()
6385 cfs_b->hierarchical_quota = parent ? parent->hierarchical_quota : RUNTIME_INF; in init_cfs_bandwidth()
6387 INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq); in init_cfs_bandwidth()
6388 hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED); in init_cfs_bandwidth()
6389 cfs_b->period_timer.function = sched_cfs_period_timer; in init_cfs_bandwidth()
6392 hrtimer_set_expires(&cfs_b->period_timer, in init_cfs_bandwidth()
6393 get_random_u32_below(cfs_b->period)); in init_cfs_bandwidth()
6394 hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); in init_cfs_bandwidth()
6395 cfs_b->slack_timer.function = sched_cfs_slack_timer; in init_cfs_bandwidth()
6396 cfs_b->slack_started = false; in init_cfs_bandwidth()
6401 cfs_rq->runtime_enabled = 0; in init_cfs_rq_runtime()
6402 INIT_LIST_HEAD(&cfs_rq->throttled_list); in init_cfs_rq_runtime()
6403 INIT_LIST_HEAD(&cfs_rq->throttled_csd_list); in init_cfs_rq_runtime()
6408 lockdep_assert_held(&cfs_b->lock); in start_cfs_bandwidth()
6410 if (cfs_b->period_active) in start_cfs_bandwidth()
6413 cfs_b->period_active = 1; in start_cfs_bandwidth()
6414 hrtimer_forward_now(&cfs_b->period_timer, cfs_b->period); in start_cfs_bandwidth()
6415 hrtimer_start_expires(&cfs_b->period_timer, HRTIMER_MODE_ABS_PINNED); in start_cfs_bandwidth()
6423 if (!cfs_b->throttled_cfs_rq.next) in destroy_cfs_bandwidth()
6426 hrtimer_cancel(&cfs_b->period_timer); in destroy_cfs_bandwidth()
6427 hrtimer_cancel(&cfs_b->slack_timer); in destroy_cfs_bandwidth()
6444 if (list_empty(&rq->cfsb_csd_list)) in destroy_cfs_bandwidth()
6470 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth; in update_runtime_enabled()
6471 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in update_runtime_enabled()
6473 raw_spin_lock(&cfs_b->lock); in update_runtime_enabled()
6474 cfs_rq->runtime_enabled = cfs_b->quota != RUNTIME_INF; in update_runtime_enabled()
6475 raw_spin_unlock(&cfs_b->lock); in update_runtime_enabled()
6496 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in unthrottle_offline_cfs_rqs()
6498 if (!cfs_rq->runtime_enabled) in unthrottle_offline_cfs_rqs()
6505 cfs_rq->runtime_remaining = 1; in unthrottle_offline_cfs_rqs()
6510 cfs_rq->runtime_enabled = 0; in unthrottle_offline_cfs_rqs()
6527 if (cfs_rq->runtime_enabled || in cfs_task_bw_constrained()
6528 tg_cfs_bandwidth(cfs_rq->tg)->hierarchical_quota != RUNTIME_INF) in cfs_task_bw_constrained()
6546 if (rq->nr_running != 1) in sched_fair_update_stop_tick()
6620 struct sched_entity *se = &p->se; in hrtick_start_fair()
6624 if (rq->cfs.h_nr_running > 1) { in hrtick_start_fair()
6625 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime; in hrtick_start_fair()
6626 u64 slice = se->slice; in hrtick_start_fair()
6627 s64 delta = slice - ran; in hrtick_start_fair()
6645 struct task_struct *curr = rq->curr; in hrtick_update()
6647 if (!hrtick_enabled_fair(rq) || curr->sched_class != &fair_sched_class) in hrtick_update()
6675 if (!READ_ONCE(rq->rd->overutilized) && cpu_overutilized(rq->cpu)) { in update_overutilized_status()
6676 WRITE_ONCE(rq->rd->overutilized, SG_OVERUTILIZED); in update_overutilized_status()
6677 trace_sched_overutilized_tp(rq->rd, SG_OVERUTILIZED); in update_overutilized_status()
6687 return unlikely(rq->nr_running == rq->cfs.idle_h_nr_running && in sched_idle_rq()
6688 rq->nr_running); in sched_idle_rq()
6707 struct sched_entity *se = &p->se; in enqueue_task_fair()
6717 util_est_enqueue(&rq->cfs, p); in enqueue_task_fair()
6724 if (p->in_iowait) in enqueue_task_fair()
6728 if (se->on_rq) in enqueue_task_fair()
6733 cfs_rq->h_nr_running++; in enqueue_task_fair()
6734 cfs_rq->idle_h_nr_running += idle_h_nr_running; in enqueue_task_fair()
6753 cfs_rq->h_nr_running++; in enqueue_task_fair()
6754 cfs_rq->idle_h_nr_running += idle_h_nr_running; in enqueue_task_fair()
6800 struct sched_entity *se = &p->se; in dequeue_task_fair()
6805 util_est_dequeue(&rq->cfs, p); in dequeue_task_fair()
6811 cfs_rq->h_nr_running--; in dequeue_task_fair()
6812 cfs_rq->idle_h_nr_running -= idle_h_nr_running; in dequeue_task_fair()
6822 if (cfs_rq->load.weight) { in dequeue_task_fair()
6823 /* Avoid re-evaluating load for this entity: */ 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()
6860 rq->next_balance = jiffies; in dequeue_task_fair()
6863 util_est_update(&rq->cfs, p, task_sleep); in dequeue_task_fair()
6889 return cfs_rq_load_avg(&rq->cfs); in cpu_load()
6893 * cpu_load_without - compute CPU load without any contributions from *p
6911 if (cpu_of(rq) != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in cpu_load_without()
6914 cfs_rq = &rq->cfs; in cpu_load_without()
6915 load = READ_ONCE(cfs_rq->avg.load_avg); in cpu_load_without()
6925 return cfs_rq_runnable_avg(&rq->cfs); in cpu_runnable()
6934 if (cpu_of(rq) != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in cpu_runnable_without()
6937 cfs_rq = &rq->cfs; in cpu_runnable_without()
6938 runnable = READ_ONCE(cfs_rq->avg.runnable_avg); in cpu_runnable_without()
6941 lsub_positive(&runnable, p->se.avg.runnable_avg); in cpu_runnable_without()
6948 return cpu_rq(cpu)->cpu_capacity; in capacity_of()
6957 if (time_after(jiffies, current->wakee_flip_decay_ts + HZ)) { in record_wakee()
6958 current->wakee_flips >>= 1; in record_wakee()
6959 current->wakee_flip_decay_ts = jiffies; in record_wakee()
6962 if (current->last_wakee != p) { in record_wakee()
6963 current->last_wakee = p; in record_wakee()
6964 current->wakee_flips++; in record_wakee()
6969 * Detect M:N waker/wakee relationships via a switching-frequency heuristic.
6979 * non-monogamous, with partner count exceeding socket size.
6987 unsigned int master = current->wakee_flips; in wake_wide()
6988 unsigned int slave = p->wakee_flips; in wake_wide()
7003 * wake_affine_idle() - only considers 'now', it check if the waking CPU is
7004 * cache-affine and is (or will be) idle.
7006 * wake_affine_weight() - considers the weight to reflect the average
7028 if (sync && cpu_rq(this_cpu)->nr_running == 1) in wake_affine_idle()
7052 this_eff_load -= current_load; in wake_affine_weight()
7063 prev_eff_load -= task_load; in wake_affine_weight()
7065 prev_eff_load *= 100 + (sd->imbalance_pct - 100) / 2; in wake_affine_weight()
7091 schedstat_inc(p->stats.nr_wakeups_affine_attempts); in wake_affine()
7095 schedstat_inc(sd->ttwu_move_affine); in wake_affine()
7096 schedstat_inc(p->stats.nr_wakeups_affine); in wake_affine()
7104 * find_idlest_group_cpu - find the idlest CPU among the CPUs in the group.
7113 int shallowest_idle_cpu = -1; in find_idlest_group_cpu()
7117 if (group->group_weight == 1) in find_idlest_group_cpu()
7121 for_each_cpu_and(i, sched_group_span(group), p->cpus_ptr) { in find_idlest_group_cpu()
7132 if (idle && idle->exit_latency < min_exit_latency) { in find_idlest_group_cpu()
7138 min_exit_latency = idle->exit_latency; in find_idlest_group_cpu()
7139 latest_idle_timestamp = rq->idle_stamp; in find_idlest_group_cpu()
7141 } else if ((!idle || idle->exit_latency == min_exit_latency) && in find_idlest_group_cpu()
7142 rq->idle_stamp > latest_idle_timestamp) { in find_idlest_group_cpu()
7148 latest_idle_timestamp = rq->idle_stamp; in find_idlest_group_cpu()
7151 } else if (shallowest_idle_cpu == -1) { in find_idlest_group_cpu()
7160 return shallowest_idle_cpu != -1 ? shallowest_idle_cpu : least_loaded_cpu; in find_idlest_group_cpu()
7168 if (!cpumask_intersects(sched_domain_span(sd), p->cpus_ptr)) in find_idlest_cpu()
7176 sync_entity_load_avg(&p->se); in find_idlest_cpu()
7183 if (!(sd->flags & sd_flag)) { in find_idlest_cpu()
7184 sd = sd->child; in find_idlest_cpu()
7190 sd = sd->child; in find_idlest_cpu()
7197 sd = sd->child; in find_idlest_cpu()
7203 weight = sd->span_weight; in find_idlest_cpu()
7206 if (weight <= tmp->span_weight) in find_idlest_cpu()
7208 if (tmp->flags & sd_flag) in find_idlest_cpu()
7222 return -1; in __select_idle_cpu()
7235 WRITE_ONCE(sds->has_idle_cores, val); in set_idle_cores()
7244 return READ_ONCE(sds->has_idle_cores); in test_idle_cores()
7251 * information in sd_llc_shared->has_idle_cores.
7281 * sd_llc->shared->has_idle_cores and enabled through update_idle_core() above.
7291 if (*idle_cpu == -1) { in select_idle_core()
7292 if (sched_idle_cpu(cpu) && cpumask_test_cpu(cpu, p->cpus_ptr)) { in select_idle_core()
7300 if (*idle_cpu == -1 && cpumask_test_cpu(cpu, p->cpus_ptr)) in select_idle_core()
7308 return -1; in select_idle_core()
7318 for_each_cpu_and(cpu, cpu_smt_mask(target), p->cpus_ptr) { in select_idle_smt()
7325 return -1; in select_idle_smt()
7346 return -1; in select_idle_smt()
7353 * comparing the average scan cost (tracked in sd->avg_scan_cost) against the
7354 * average idle time for this rq (as found in rq->avg_idle).
7359 int i, cpu, idle_cpu = -1, nr = INT_MAX; in select_idle_cpu()
7362 cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr); in select_idle_cpu()
7367 /* because !--nr is the condition to stop scan */ in select_idle_cpu()
7368 nr = READ_ONCE(sd_share->nr_idle_scan) + 1; in select_idle_cpu()
7371 return -1; in select_idle_cpu()
7376 struct sched_group *sg = sd->groups; in select_idle_cpu()
7378 if (sg->flags & SD_CLUSTER) { in select_idle_cpu()
7388 if (--nr <= 0) in select_idle_cpu()
7389 return -1; in select_idle_cpu()
7406 if (--nr <= 0) in select_idle_cpu()
7407 return -1; in select_idle_cpu()
7430 int cpu, best_cpu = -1; in select_idle_capacity()
7434 cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr); in select_idle_capacity()
7456 cpu_cap = arch_scale_cpu_capacity(cpu) - thermal_load_avg(cpu_rq(cpu)); in select_idle_capacity()
7459 * First, select CPU which fits better (-1 being better than 0). in select_idle_capacity()
7496 int i, recent_used_cpu, prev_aff = -1; in select_idle_sibling()
7503 sync_entity_load_avg(&p->se); in select_idle_sibling()
7510 * per-cpu select_rq_mask usage in select_idle_sibling()
7533 * Allow a per-cpu kthread to stack with the wakee if the in select_idle_sibling()
7536 * per-cpu kthread that is now complete and the wakeup is in select_idle_sibling()
7543 this_rq()->nr_running <= 1 && in select_idle_sibling()
7549 recent_used_cpu = p->recent_used_cpu; in select_idle_sibling()
7550 p->recent_used_cpu = prev; in select_idle_sibling()
7555 cpumask_test_cpu(recent_used_cpu, p->cpus_ptr) && in select_idle_sibling()
7563 recent_used_cpu = -1; in select_idle_sibling()
7619 * cpu_util() - Estimates the amount of CPU capacity used by CFS tasks.
7622 * @dst_cpu: CPU @p migrates to, -1 if @p moves from @cpu or @p == NULL
7629 * recent utilization of currently non-runnable tasks on that CPU.
7637 * previously-executed tasks, which helps better deduce how busy a CPU will
7638 * be when a long-sleeping task wakes up. The contribution to CPU utilization
7652 * could be seen as over-utilized even though CPU1 has 20% of spare CPU
7655 * after task migrations (scheduler-driven DVFS).
7662 struct cfs_rq *cfs_rq = &cpu_rq(cpu)->cfs; in cpu_util()
7663 unsigned long util = READ_ONCE(cfs_rq->avg.util_avg); in cpu_util()
7667 runnable = READ_ONCE(cfs_rq->avg.runnable_avg); in cpu_util()
7672 * If @dst_cpu is -1 or @p migrates from @cpu to @dst_cpu remove its in cpu_util()
7685 util_est = READ_ONCE(cfs_rq->avg.util_est); in cpu_util()
7688 * During wake-up @p isn't enqueued yet and doesn't contribute in cpu_util()
7689 * to any cpu_rq(cpu)->cfs.avg.util_est. in cpu_util()
7693 * During exec (@dst_cpu = -1) @p is enqueued and does in cpu_util()
7694 * contribute to cpu_rq(cpu)->cfs.util_est. in cpu_util()
7703 * p->on_rq = TASK_ON_RQ_MIGRATING; in cpu_util()
7704 * -------------------------------- A in cpu_util()
7708 * -------------------------------- B in cpu_util()
7726 return cpu_util(cpu, NULL, -1, 0); in cpu_util_cfs()
7731 return cpu_util(cpu, NULL, -1, 1); in cpu_util_cfs_boost()
7750 if (cpu != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in cpu_util_without()
7753 return cpu_util(cpu, p, -1, 0); in cpu_util_without()
7757 * energy_env - Utilization landscape for energy estimation.
7763 * @pd_cap: Entire perf domain capacity. (pd->nr_cpus * cpu_cap).
7789 eenv->task_busy_time = busy_time; in eenv_task_busy_time()
7803 * - A stable PD utilization, no matter which CPU of that PD we want to place
7806 * - A fair comparison between CPUs as the task contribution (task_util())
7811 * exceed @eenv->pd_cap.
7821 unsigned long util = cpu_util(cpu, p, -1, 0); in eenv_pd_busy_time()
7826 eenv->pd_busy_time = min(eenv->pd_cap, busy_time); in eenv_pd_busy_time()
7833 * Returns the maximum utilization among @eenv->cpus. This utilization can't
7834 * exceed @eenv->cpu_cap.
7875 return min(max_util, eenv->cpu_cap); in eenv_pd_max_util()
7888 unsigned long busy_time = eenv->pd_busy_time; in compute_energy()
7892 busy_time = min(eenv->pd_cap, busy_time + eenv->task_busy_time); in compute_energy()
7894 energy = em_cpu_energy(pd->em_pd, max_util, busy_time, eenv->cpu_cap); in compute_energy()
7902 * find_energy_efficient_cpu(): Find most energy-efficient target CPU for the
7906 * out which of the CPU candidates is the most energy-efficient.
7923 * cluster-packing, and spreading inside a cluster. That should at least be
7930 * NOTE: Forkees are not accepted in the energy-aware wake-up path because
7934 * to be energy-inefficient in some use-cases. The alternative would be to
7937 * other use-cases too. So, until someone finds a better way to solve this,
7938 * let's keep things simple by re-using the existing slow path.
7946 struct root_domain *rd = this_rq()->rd; in find_energy_efficient_cpu()
7947 int cpu, best_energy_cpu, target = -1; in find_energy_efficient_cpu()
7948 int prev_fits = -1, best_fits = -1; in find_energy_efficient_cpu()
7956 pd = rcu_dereference(rd->pd); in find_energy_efficient_cpu()
7957 if (!pd || READ_ONCE(rd->overutilized)) in find_energy_efficient_cpu()
7961 * Energy-aware wake-up happens on the lowest sched_domain starting in find_energy_efficient_cpu()
7966 sd = sd->parent; in find_energy_efficient_cpu()
7972 sync_entity_load_avg(&p->se); in find_energy_efficient_cpu()
7978 for (; pd; pd = pd->next) { in find_energy_efficient_cpu()
7981 long prev_spare_cap = -1, max_spare_cap = -1; in find_energy_efficient_cpu()
7984 int max_spare_cap_cpu = -1; in find_energy_efficient_cpu()
7985 int fits, max_fits = -1; in find_energy_efficient_cpu()
7995 cpu_thermal_cap -= arch_scale_thermal_pressure(cpu); in find_energy_efficient_cpu()
8008 if (!cpumask_test_cpu(cpu, p->cpus_ptr)) in find_energy_efficient_cpu()
8027 * max-aggregated uclamp_{min, max}. in find_energy_efficient_cpu()
8064 base_energy = compute_energy(&eenv, pd, cpus, p, -1); in find_energy_efficient_cpu()
8067 if (prev_spare_cap > -1) { in find_energy_efficient_cpu()
8073 prev_delta -= base_energy; in find_energy_efficient_cpu()
8097 cur_delta -= base_energy; in find_energy_efficient_cpu()
8141 int sync = (wake_flags & WF_SYNC) && !(current->flags & PF_EXITING); in select_task_rq_fair()
8150 * required for stable ->cpus_allowed in select_task_rq_fair()
8152 lockdep_assert_held(&p->pi_lock); in select_task_rq_fair()
8157 cpumask_test_cpu(cpu, p->cpus_ptr)) in select_task_rq_fair()
8167 want_affine = !wake_wide(p) && cpumask_test_cpu(cpu, p->cpus_ptr); in select_task_rq_fair()
8176 if (want_affine && (tmp->flags & SD_WAKE_AFFINE) && in select_task_rq_fair()
8190 if (tmp->flags & sd_flag) in select_task_rq_fair()
8211 * previous CPU. The caller guarantees p->pi_lock or task_rq(p)->lock is held.
8215 struct sched_entity *se = &p->se; in migrate_task_rq_fair()
8224 * leading to an inflation after wake-up on the new rq. in migrate_task_rq_fair()
8234 se->avg.last_update_time = 0; in migrate_task_rq_fair()
8241 remove_entity_load_avg(&p->se); in task_dead_fair()
8247 if (rq->nr_running) in balance_fair()
8257 if (SCHED_WARN_ON(!se->on_rq)) in set_next_buddy()
8261 cfs_rq_of(se)->next = se; in set_next_buddy()
8270 struct task_struct *curr = rq->curr; in check_preempt_wakeup_fair()
8271 struct sched_entity *se = &curr->se, *pse = &p->se; in check_preempt_wakeup_fair()
8282 * next-buddy nomination below. in check_preempt_wakeup_fair()
8295 * Note: this also catches the edge-case of curr being in a throttled in check_preempt_wakeup_fair()
8304 /* Idle tasks are by definition preempted by non-idle tasks. */ in check_preempt_wakeup_fair()
8310 * Batch and idle tasks do not preempt non-idle tasks (their preemption in check_preempt_wakeup_fair()
8313 if (unlikely(p->policy != SCHED_NORMAL) || !sched_feat(WAKEUP_PREEMPTION)) in check_preempt_wakeup_fair()
8323 * Preempt an idle group in favor of a non-idle group (and don't preempt in check_preempt_wakeup_fair()
8353 cfs_rq = &rq->cfs; in pick_task_fair()
8354 if (!cfs_rq->nr_running) in pick_task_fair()
8358 struct sched_entity *curr = cfs_rq->curr; in pick_task_fair()
8362 if (curr->on_rq) in pick_task_fair()
8382 struct cfs_rq *cfs_rq = &rq->cfs; in pick_next_task_fair()
8392 if (!prev || prev->sched_class != &fair_sched_class) in pick_next_task_fair()
8404 struct sched_entity *curr = cfs_rq->curr; in pick_next_task_fair()
8408 * have to consider cfs_rq->curr. If it is still a runnable in pick_next_task_fair()
8413 if (curr->on_rq) in pick_next_task_fair()
8425 cfs_rq = &rq->cfs; in pick_next_task_fair()
8427 if (!cfs_rq->nr_running) in pick_next_task_fair()
8446 struct sched_entity *pse = &prev->se; in pick_next_task_fair()
8449 int se_depth = se->depth; in pick_next_task_fair()
8450 int pse_depth = pse->depth; in pick_next_task_fair()
8487 list_move(&p->se.group_node, &rq->cfs_tasks); in pick_next_task_fair()
8505 * Because newidle_balance() releases (and re-acquires) rq->lock, it is in pick_next_task_fair()
8507 * must re-start the pick_next_entity() loop. in pick_next_task_fair()
8534 struct sched_entity *se = &prev->se; in put_prev_task_fair()
8548 struct task_struct *curr = rq->curr; in yield_task_fair()
8550 struct sched_entity *se = &curr->se; in yield_task_fair()
8555 if (unlikely(rq->nr_running == 1)) in yield_task_fair()
8562 * Update run-time statistics of the 'current'. in yield_task_fair()
8572 se->deadline += calc_delta_fair(se->slice, se); in yield_task_fair()
8577 struct sched_entity *se = &p->se; in yield_to_task_fair()
8580 if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se))) in yield_to_task_fair()
8593 * Fair scheduling class load-balancing methods.
8597 * The purpose of load-balancing is to achieve the same basic fairness the
8598 * per-CPU scheduler provides, namely provide a proportional amount of compute
8603 * Where W_i,n is the n-th weight average for CPU i. The instantaneous weight
8608 * Where w_i,j is the weight of the j-th runnable task on CPU i. This weight
8614 * W'_i,n = (2^n - 1) / 2^n * W_i,n + 1 / 2^n * W_i,0 (3)
8623 * imb_i,j = max{ avg(W/C), W_i/C_i } - min{ avg(W/C), W_j/C_j } (4)
8630 * - infeasible weights;
8631 * - local vs global optima in the discrete case. ]
8641 * of load-balance at each level inv. proportional to the number of CPUs in
8647 * \Sum { --- * --- * 2^i } = O(n) (5)
8649 * `- size of each group
8650 * | | `- number of CPUs doing load-balance
8651 * | `- freq
8652 * `- sum over all levels
8694 * W_i,0 = \Sum_j \Prod_k w_k * ----- (9)
8701 * w_i,j,k is the weight of the j-th runnable task in the k-th cgroup on CPU i.
8783 /* The set of CPUs under consideration for load-balancing */
8798 * Is this task likely cache-hot:
8804 lockdep_assert_rq_held(env->src_rq); in task_hot()
8806 if (p->sched_class != &fair_sched_class) in task_hot()
8813 if (env->sd->flags & SD_SHARE_CPUCAPACITY) in task_hot()
8819 if (sched_feat(CACHE_HOT_BUDDY) && env->dst_rq->nr_running && in task_hot()
8820 (&p->se == cfs_rq_of(&p->se)->next)) in task_hot()
8823 if (sysctl_sched_migration_cost == -1) in task_hot()
8830 if (!sched_core_cookie_match(cpu_rq(env->dst_cpu), p)) in task_hot()
8836 delta = rq_clock_task(env->src_rq) - p->se.exec_start; in task_hot()
8845 * Returns -1, if task migration is not affected by locality.
8849 struct numa_group *numa_group = rcu_dereference(p->numa_group); in migrate_degrades_locality()
8854 return -1; in migrate_degrades_locality()
8856 if (!p->numa_faults || !(env->sd->flags & SD_NUMA)) in migrate_degrades_locality()
8857 return -1; in migrate_degrades_locality()
8859 src_nid = cpu_to_node(env->src_cpu); in migrate_degrades_locality()
8860 dst_nid = cpu_to_node(env->dst_cpu); in migrate_degrades_locality()
8863 return -1; in migrate_degrades_locality()
8866 if (src_nid == p->numa_preferred_nid) { in migrate_degrades_locality()
8867 if (env->src_rq->nr_running > env->src_rq->nr_preferred_running) in migrate_degrades_locality()
8870 return -1; in migrate_degrades_locality()
8874 if (dst_nid == p->numa_preferred_nid) in migrate_degrades_locality()
8878 if (env->idle == CPU_IDLE) in migrate_degrades_locality()
8879 return -1; in migrate_degrades_locality()
8897 return -1; in migrate_degrades_locality()
8902 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
8909 lockdep_assert_rq_held(env->src_rq); in can_migrate_task()
8916 * 4) are cache-hot on their current CPU. in can_migrate_task()
8918 if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu)) in can_migrate_task()
8925 if (!cpumask_test_cpu(env->dst_cpu, p->cpus_ptr)) { in can_migrate_task()
8928 schedstat_inc(p->stats.nr_failed_migrations_affine); in can_migrate_task()
8930 env->flags |= LBF_SOME_PINNED; in can_migrate_task()
8938 * - for NEWLY_IDLE in can_migrate_task()
8939 * - if we have already computed one in current iteration in can_migrate_task()
8940 * - if it's an active balance in can_migrate_task()
8942 if (env->idle == CPU_NEWLY_IDLE || in can_migrate_task()
8943 env->flags & (LBF_DST_PINNED | LBF_ACTIVE_LB)) in can_migrate_task()
8946 /* Prevent to re-select dst_cpu via env's CPUs: */ in can_migrate_task()
8947 for_each_cpu_and(cpu, env->dst_grpmask, env->cpus) { in can_migrate_task()
8948 if (cpumask_test_cpu(cpu, p->cpus_ptr)) { in can_migrate_task()
8949 env->flags |= LBF_DST_PINNED; in can_migrate_task()
8950 env->new_dst_cpu = cpu; in can_migrate_task()
8959 env->flags &= ~LBF_ALL_PINNED; in can_migrate_task()
8961 if (task_on_cpu(env->src_rq, p)) { in can_migrate_task()
8962 schedstat_inc(p->stats.nr_failed_migrations_running); in can_migrate_task()
8973 if (env->flags & LBF_ACTIVE_LB) in can_migrate_task()
8977 if (tsk_cache_hot == -1) in can_migrate_task()
8981 env->sd->nr_balance_failed > env->sd->cache_nice_tries) { in can_migrate_task()
8983 schedstat_inc(env->sd->lb_hot_gained[env->idle]); in can_migrate_task()
8984 schedstat_inc(p->stats.nr_forced_migrations); in can_migrate_task()
8989 schedstat_inc(p->stats.nr_failed_migrations_hot); in can_migrate_task()
8994 * detach_task() -- detach the task for the migration specified in env
8998 lockdep_assert_rq_held(env->src_rq); in detach_task()
9000 deactivate_task(env->src_rq, p, DEQUEUE_NOCLOCK); in detach_task()
9001 set_task_cpu(p, env->dst_cpu); in detach_task()
9005 * detach_one_task() -- tries to dequeue exactly one task from env->src_rq, as
9014 lockdep_assert_rq_held(env->src_rq); in detach_one_task()
9017 &env->src_rq->cfs_tasks, se.group_node) { in detach_one_task()
9025 * lb_gained[env->idle] is updated (other is detach_tasks) in detach_one_task()
9029 schedstat_inc(env->sd->lb_gained[env->idle]); in detach_one_task()
9036 * detach_tasks() -- tries to detach up to imbalance load/util/tasks from
9043 struct list_head *tasks = &env->src_rq->cfs_tasks; in detach_tasks()
9048 lockdep_assert_rq_held(env->src_rq); in detach_tasks()
9054 if (env->src_rq->nr_running <= 1) { in detach_tasks()
9055 env->flags &= ~LBF_ALL_PINNED; in detach_tasks()
9059 if (env->imbalance <= 0) in detach_tasks()
9067 if (env->idle != CPU_NOT_IDLE && env->src_rq->nr_running <= 1) in detach_tasks()
9070 env->loop++; in detach_tasks()
9075 if (env->loop > env->loop_max && in detach_tasks()
9076 !(env->flags & LBF_ALL_PINNED)) in detach_tasks()
9080 if (env->loop > env->loop_break) { in detach_tasks()
9081 env->loop_break += SCHED_NR_MIGRATE_BREAK; in detach_tasks()
9082 env->flags |= LBF_NEED_BREAK; in detach_tasks()
9091 switch (env->migration_type) { in detach_tasks()
9096 * value. Make sure that env->imbalance decreases in detach_tasks()
9103 load < 16 && !env->sd->nr_balance_failed) in detach_tasks()
9112 if (shr_bound(load, env->sd->nr_balance_failed) > env->imbalance) in detach_tasks()
9115 env->imbalance -= load; in detach_tasks()
9121 if (shr_bound(util, env->sd->nr_balance_failed) > env->imbalance) in detach_tasks()
9124 env->imbalance -= util; in detach_tasks()
9128 env->imbalance--; in detach_tasks()
9133 if (task_fits_cpu(p, env->src_cpu)) in detach_tasks()
9136 env->imbalance = 0; in detach_tasks()
9141 list_add(&p->se.group_node, &env->tasks); in detach_tasks()
9151 if (env->idle == CPU_NEWLY_IDLE) in detach_tasks()
9159 if (env->imbalance <= 0) in detach_tasks()
9164 list_move(&p->se.group_node, tasks); in detach_tasks()
9172 schedstat_add(env->sd->lb_gained[env->idle], detached); in detach_tasks()
9178 * attach_task() -- attach the task detached by detach_task() to its new rq.
9190 * attach_one_task() -- attaches the task returned from detach_one_task() to
9204 * attach_tasks() -- attaches all tasks detached by detach_tasks() to their
9209 struct list_head *tasks = &env->tasks; in attach_tasks()
9213 rq_lock(env->dst_rq, &rf); in attach_tasks()
9214 update_rq_clock(env->dst_rq); in attach_tasks()
9218 list_del_init(&p->se.group_node); in attach_tasks()
9220 attach_task(env->dst_rq, p); in attach_tasks()
9223 rq_unlock(env->dst_rq, &rf); in attach_tasks()
9229 if (cfs_rq->avg.load_avg) in cfs_rq_has_blocked()
9232 if (cfs_rq->avg.util_avg) in cfs_rq_has_blocked()
9240 if (READ_ONCE(rq->avg_rt.util_avg)) in others_have_blocked()
9243 if (READ_ONCE(rq->avg_dl.util_avg)) in others_have_blocked()
9250 if (READ_ONCE(rq->avg_irq.util_avg)) in others_have_blocked()
9259 WRITE_ONCE(rq->last_blocked_load_update_tick, jiffies); in update_blocked_load_tick()
9265 rq->has_blocked_load = 0; in update_blocked_load_status()
9285 curr_class = rq->curr->sched_class; in __update_blocked_others()
9318 if (cfs_rq->nr_running == 0) in __update_blocked_fair()
9321 if (cfs_rq == &rq->cfs) in __update_blocked_fair()
9326 se = cfs_rq->tg->se[cpu]; in __update_blocked_fair()
9347 * This needs to be done in a top-down fashion because the load of a child
9353 struct sched_entity *se = cfs_rq->tg->se[cpu_of(rq)]; in update_cfs_rq_h_load()
9357 if (cfs_rq->last_h_load_update == now) in update_cfs_rq_h_load()
9360 WRITE_ONCE(cfs_rq->h_load_next, NULL); in update_cfs_rq_h_load()
9363 WRITE_ONCE(cfs_rq->h_load_next, se); in update_cfs_rq_h_load()
9364 if (cfs_rq->last_h_load_update == now) in update_cfs_rq_h_load()
9369 cfs_rq->h_load = cfs_rq_load_avg(cfs_rq); in update_cfs_rq_h_load()
9370 cfs_rq->last_h_load_update = now; in update_cfs_rq_h_load()
9373 while ((se = READ_ONCE(cfs_rq->h_load_next)) != NULL) { in update_cfs_rq_h_load()
9374 load = cfs_rq->h_load; in update_cfs_rq_h_load()
9375 load = div64_ul(load * se->avg.load_avg, in update_cfs_rq_h_load()
9378 cfs_rq->h_load = load; in update_cfs_rq_h_load()
9379 cfs_rq->last_h_load_update = now; in update_cfs_rq_h_load()
9388 return div64_ul(p->se.avg.load_avg * cfs_rq->h_load, in task_h_load()
9394 struct cfs_rq *cfs_rq = &rq->cfs; in __update_blocked_fair()
9406 return p->se.avg.load_avg; in task_h_load()
9432 * sg_lb_stats - stats of a sched_group required for load_balancing
9455 * sd_lb_stats - Structure to store the statistics of a sched_domain
9509 used = READ_ONCE(rq->avg_rt.util_avg); in scale_rt_capacity()
9510 used += READ_ONCE(rq->avg_dl.util_avg); in scale_rt_capacity()
9516 free = max - used; in scale_rt_capacity()
9524 struct sched_group *sdg = sd->groups; in update_cpu_capacity()
9529 cpu_rq(cpu)->cpu_capacity = capacity; in update_cpu_capacity()
9532 sdg->sgc->capacity = capacity; in update_cpu_capacity()
9533 sdg->sgc->min_capacity = capacity; in update_cpu_capacity()
9534 sdg->sgc->max_capacity = capacity; in update_cpu_capacity()
9539 struct sched_domain *child = sd->child; in update_group_capacity()
9540 struct sched_group *group, *sdg = sd->groups; in update_group_capacity()
9544 interval = msecs_to_jiffies(sd->balance_interval); in update_group_capacity()
9546 sdg->sgc->next_update = jiffies + interval; in update_group_capacity()
9557 if (child->flags & SD_OVERLAP) { in update_group_capacity()
9576 group = child->groups; in update_group_capacity()
9578 struct sched_group_capacity *sgc = group->sgc; in update_group_capacity()
9580 capacity += sgc->capacity; in update_group_capacity()
9581 min_capacity = min(sgc->min_capacity, min_capacity); in update_group_capacity()
9582 max_capacity = max(sgc->max_capacity, max_capacity); in update_group_capacity()
9583 group = group->next; in update_group_capacity()
9584 } while (group != child->groups); in update_group_capacity()
9587 sdg->sgc->capacity = capacity; in update_group_capacity()
9588 sdg->sgc->min_capacity = min_capacity; in update_group_capacity()
9589 sdg->sgc->max_capacity = max_capacity; in update_group_capacity()
9600 return ((rq->cpu_capacity * sd->imbalance_pct) < in check_cpu_capacity()
9611 return rq->misfit_task_load && in check_misfit_status()
9612 (arch_scale_cpu_capacity(rq->cpu) < rq->rd->max_cpu_capacity || in check_misfit_status()
9618 * groups is inadequate due to ->cpus_ptr constraints.
9627 * If we were to balance group-wise we'd place two tasks in the first group and
9647 return group->sgc->imbalance; in sg_imbalanced()
9665 if (sgs->sum_nr_running < sgs->group_weight) in group_has_capacity()
9668 if ((sgs->group_capacity * imbalance_pct) < in group_has_capacity()
9669 (sgs->group_runnable * 100)) in group_has_capacity()
9672 if ((sgs->group_capacity * 100) > in group_has_capacity()
9673 (sgs->group_util * imbalance_pct)) in group_has_capacity()
9690 if (sgs->sum_nr_running <= sgs->group_weight) in group_is_overloaded()
9693 if ((sgs->group_capacity * 100) < in group_is_overloaded()
9694 (sgs->group_util * imbalance_pct)) in group_is_overloaded()
9697 if ((sgs->group_capacity * imbalance_pct) < in group_is_overloaded()
9698 (sgs->group_runnable * 100)) in group_is_overloaded()
9715 if (sgs->group_asym_packing) in group_classify()
9718 if (sgs->group_smt_balance) in group_classify()
9721 if (sgs->group_misfit_task_load) in group_classify()
9731 * sched_use_asym_prio - Check whether asym_packing priority must be used
9746 return sd->flags & SD_SHARE_CPUCAPACITY || is_core_idle(cpu); in sched_use_asym_prio()
9750 * sched_asym - Check if the destination CPU can do asym_packing load balance
9752 * @sds: Load-balancing data with statistics of the local group
9753 * @sgs: Load-balancing statistics of the candidate busiest group
9775 if (!sched_use_asym_prio(env->sd, env->dst_cpu)) in sched_asym()
9782 if (group->flags & SD_SHARE_CPUCAPACITY) { in sched_asym()
9783 if (sgs->group_weight - sgs->idle_cpus != 1) in sched_asym()
9787 return sched_asym_prefer(env->dst_cpu, group->asym_prefer_cpu); in sched_asym()
9797 return (sg1->flags & SD_SHARE_CPUCAPACITY) != in smt_vs_nonsmt_groups()
9798 (sg2->flags & SD_SHARE_CPUCAPACITY); in smt_vs_nonsmt_groups()
9804 if (env->idle == CPU_NOT_IDLE) in smt_balance()
9813 if (group->flags & SD_SHARE_CPUCAPACITY && in smt_balance()
9814 sgs->sum_h_nr_running > 1) in smt_balance()
9828 if (env->idle == CPU_NOT_IDLE || !busiest->sum_nr_running) in sibling_imbalance()
9831 ncores_busiest = sds->busiest->cores; in sibling_imbalance()
9832 ncores_local = sds->local->cores; in sibling_imbalance()
9835 imbalance = busiest->sum_nr_running; in sibling_imbalance()
9836 lsub_positive(&imbalance, local->sum_nr_running); in sibling_imbalance()
9841 imbalance = ncores_local * busiest->sum_nr_running; in sibling_imbalance()
9842 lsub_positive(&imbalance, ncores_busiest * local->sum_nr_running); in sibling_imbalance()
9848 if (imbalance <= 1 && local->sum_nr_running == 0 && in sibling_imbalance()
9849 busiest->sum_nr_running > 1) in sibling_imbalance()
9862 if (rq->cfs.h_nr_running != 1) in sched_reduced_capacity()
9869 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
9871 * @sds: Load-balancing data with statistics of the local group.
9886 local_group = group == sds->local; in update_sg_lb_stats()
9888 for_each_cpu_and(i, sched_group_span(group), env->cpus) { in update_sg_lb_stats()
9892 sgs->group_load += load; in update_sg_lb_stats()
9893 sgs->group_util += cpu_util_cfs(i); in update_sg_lb_stats()
9894 sgs->group_runnable += cpu_runnable(rq); in update_sg_lb_stats()
9895 sgs->sum_h_nr_running += rq->cfs.h_nr_running; in update_sg_lb_stats()
9897 nr_running = rq->nr_running; in update_sg_lb_stats()
9898 sgs->sum_nr_running += nr_running; in update_sg_lb_stats()
9907 sgs->nr_numa_running += rq->nr_numa_running; in update_sg_lb_stats()
9908 sgs->nr_preferred_running += rq->nr_preferred_running; in update_sg_lb_stats()
9914 sgs->idle_cpus++; in update_sg_lb_stats()
9922 if (env->sd->flags & SD_ASYM_CPUCAPACITY) { in update_sg_lb_stats()
9924 if (sgs->group_misfit_task_load < rq->misfit_task_load) { in update_sg_lb_stats()
9925 sgs->group_misfit_task_load = rq->misfit_task_load; in update_sg_lb_stats()
9928 } else if ((env->idle != CPU_NOT_IDLE) && in update_sg_lb_stats()
9929 sched_reduced_capacity(rq, env->sd)) { in update_sg_lb_stats()
9931 if (sgs->group_misfit_task_load < load) in update_sg_lb_stats()
9932 sgs->group_misfit_task_load = load; in update_sg_lb_stats()
9936 sgs->group_capacity = group->sgc->capacity; in update_sg_lb_stats()
9938 sgs->group_weight = group->group_weight; in update_sg_lb_stats()
9941 if (!local_group && env->sd->flags & SD_ASYM_PACKING && in update_sg_lb_stats()
9942 env->idle != CPU_NOT_IDLE && sgs->sum_h_nr_running && in update_sg_lb_stats()
9944 sgs->group_asym_packing = 1; in update_sg_lb_stats()
9949 sgs->group_smt_balance = 1; in update_sg_lb_stats()
9951 sgs->group_type = group_classify(env->sd->imbalance_pct, group, sgs); in update_sg_lb_stats()
9954 if (sgs->group_type == group_overloaded) in update_sg_lb_stats()
9955 sgs->avg_load = (sgs->group_load * SCHED_CAPACITY_SCALE) / in update_sg_lb_stats()
9956 sgs->group_capacity; in update_sg_lb_stats()
9960 * update_sd_pick_busiest - return 1 on busiest group
9977 struct sg_lb_stats *busiest = &sds->busiest_stat; in update_sd_pick_busiest()
9980 if (!sgs->sum_h_nr_running) in update_sd_pick_busiest()
9989 if ((env->sd->flags & SD_ASYM_CPUCAPACITY) && in update_sd_pick_busiest()
9990 (sgs->group_type == group_misfit_task) && in update_sd_pick_busiest()
9991 (!capacity_greater(capacity_of(env->dst_cpu), sg->sgc->max_capacity) || in update_sd_pick_busiest()
9992 sds->local_stat.group_type != group_has_spare)) in update_sd_pick_busiest()
9995 if (sgs->group_type > busiest->group_type) in update_sd_pick_busiest()
9998 if (sgs->group_type < busiest->group_type) in update_sd_pick_busiest()
10006 switch (sgs->group_type) { in update_sd_pick_busiest()
10009 if (sgs->avg_load <= busiest->avg_load) in update_sd_pick_busiest()
10022 if (sched_asym_prefer(sg->asym_prefer_cpu, sds->busiest->asym_prefer_cpu)) in update_sd_pick_busiest()
10031 if (sgs->group_misfit_task_load < busiest->group_misfit_task_load) in update_sd_pick_busiest()
10040 if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) in update_sd_pick_busiest()
10058 if (sgs->avg_load < busiest->avg_load) in update_sd_pick_busiest()
10061 if (sgs->avg_load == busiest->avg_load) { in update_sd_pick_busiest()
10063 * SMT sched groups need more help than non-SMT groups. in update_sd_pick_busiest()
10066 if (sds->busiest->flags & SD_SHARE_CPUCAPACITY) in update_sd_pick_busiest()
10078 if (smt_vs_nonsmt_groups(sds->busiest, sg)) { in update_sd_pick_busiest()
10079 if (sg->flags & SD_SHARE_CPUCAPACITY && sgs->sum_h_nr_running <= 1) in update_sd_pick_busiest()
10093 if (sgs->idle_cpus > busiest->idle_cpus) in update_sd_pick_busiest()
10095 else if ((sgs->idle_cpus == busiest->idle_cpus) && in update_sd_pick_busiest()
10096 (sgs->sum_nr_running <= busiest->sum_nr_running)) in update_sd_pick_busiest()
10104 * per-CPU capacity. Migrating tasks to less capable CPUs may harm in update_sd_pick_busiest()
10108 if ((env->sd->flags & SD_ASYM_CPUCAPACITY) && in update_sd_pick_busiest()
10109 (sgs->group_type <= group_fully_busy) && in update_sd_pick_busiest()
10110 (capacity_greater(sg->sgc->min_capacity, capacity_of(env->dst_cpu)))) in update_sd_pick_busiest()
10119 if (sgs->sum_h_nr_running > sgs->nr_numa_running) in fbq_classify_group()
10121 if (sgs->sum_h_nr_running > sgs->nr_preferred_running) in fbq_classify_group()
10128 if (rq->nr_running > rq->nr_numa_running) in fbq_classify_rq()
10130 if (rq->nr_running > rq->nr_preferred_running) in fbq_classify_rq()
10150 * task_running_on_cpu - return 1 if @p is running on @cpu.
10156 if (cpu != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in task_running_on_cpu()
10166 * idle_cpu_without - would a given CPU be idle without p ?
10176 if (rq->curr != rq->idle && rq->curr != p) in idle_cpu_without()
10180 * rq->nr_running can't be used but an updated version without the in idle_cpu_without()
10186 if (rq->ttwu_pending) in idle_cpu_without()
10194 * update_sg_wakeup_stats - Update sched_group's statistics for wakeup.
10210 if (sd->flags & SD_ASYM_CPUCAPACITY) in update_sg_wakeup_stats()
10211 sgs->group_misfit_task_load = 1; in update_sg_wakeup_stats()
10217 sgs->group_load += cpu_load_without(rq, p); in update_sg_wakeup_stats()
10218 sgs->group_util += cpu_util_without(i, p); in update_sg_wakeup_stats()
10219 sgs->group_runnable += cpu_runnable_without(rq, p); in update_sg_wakeup_stats()
10221 sgs->sum_h_nr_running += rq->cfs.h_nr_running - local; in update_sg_wakeup_stats()
10223 nr_running = rq->nr_running - local; in update_sg_wakeup_stats()
10224 sgs->sum_nr_running += nr_running; in update_sg_wakeup_stats()
10230 sgs->idle_cpus++; in update_sg_wakeup_stats()
10233 if (sd->flags & SD_ASYM_CPUCAPACITY && in update_sg_wakeup_stats()
10234 sgs->group_misfit_task_load && in update_sg_wakeup_stats()
10236 sgs->group_misfit_task_load = 0; in update_sg_wakeup_stats()
10240 sgs->group_capacity = group->sgc->capacity; in update_sg_wakeup_stats()
10242 sgs->group_weight = group->group_weight; in update_sg_wakeup_stats()
10244 sgs->group_type = group_classify(sd->imbalance_pct, group, sgs); in update_sg_wakeup_stats()
10250 if (sgs->group_type == group_fully_busy || in update_sg_wakeup_stats()
10251 sgs->group_type == group_overloaded) in update_sg_wakeup_stats()
10252 sgs->avg_load = (sgs->group_load * SCHED_CAPACITY_SCALE) / in update_sg_wakeup_stats()
10253 sgs->group_capacity; in update_sg_wakeup_stats()
10261 if (sgs->group_type < idlest_sgs->group_type) in update_pick_idlest()
10264 if (sgs->group_type > idlest_sgs->group_type) in update_pick_idlest()
10272 switch (sgs->group_type) { in update_pick_idlest()
10276 if (idlest_sgs->avg_load <= sgs->avg_load) in update_pick_idlest()
10288 if (idlest->sgc->max_capacity >= group->sgc->max_capacity) in update_pick_idlest()
10294 if (idlest_sgs->idle_cpus > sgs->idle_cpus) in update_pick_idlest()
10298 if (idlest_sgs->idle_cpus == sgs->idle_cpus && in update_pick_idlest()
10299 idlest_sgs->group_util <= sgs->group_util) in update_pick_idlest()
10317 struct sched_group *idlest = NULL, *local = NULL, *group = sd->groups; in find_idlest_group()
10331 p->cpus_ptr)) in find_idlest_group()
10355 } while (group = group->next, group != sd->groups); in find_idlest_group()
10386 (sd->imbalance_pct-100) / 100; in find_idlest_group()
10393 * cross-domain, add imbalance to the load on the remote node in find_idlest_group()
10397 if ((sd->flags & SD_NUMA) && in find_idlest_group()
10408 if (100 * local_sgs.avg_load <= sd->imbalance_pct * idlest_sgs.avg_load) in find_idlest_group()
10420 if (local->sgc->max_capacity >= idlest->sgc->max_capacity) in find_idlest_group()
10426 if (sd->flags & SD_NUMA) { in find_idlest_group()
10427 int imb_numa_nr = sd->imb_numa_nr; in find_idlest_group()
10434 if (cpu_to_node(this_cpu) == p->numa_preferred_nid) in find_idlest_group()
10438 if (cpu_to_node(idlest_cpu) == p->numa_preferred_nid) in find_idlest_group()
10450 if (p->nr_cpus_allowed != NR_CPUS) { in find_idlest_group()
10453 cpumask_and(cpus, sched_group_span(local), p->cpus_ptr); in find_idlest_group()
10454 imb_numa_nr = min(cpumask_weight(cpus), sd->imb_numa_nr); in find_idlest_group()
10457 imbalance = abs(local_sgs.idle_cpus - idlest_sgs.idle_cpus); in find_idlest_group()
10494 if (!sched_feat(SIS_UTIL) || env->idle == CPU_NEWLY_IDLE) in update_idle_cpu_scan()
10497 llc_weight = per_cpu(sd_llc_size, env->dst_cpu); in update_idle_cpu_scan()
10498 if (env->sd->span_weight != llc_weight) in update_idle_cpu_scan()
10501 sd_share = rcu_dereference(per_cpu(sd_llc_shared, env->dst_cpu)); in update_idle_cpu_scan()
10511 * let y = SCHED_CAPACITY_SCALE - p * x^2 [1] in update_idle_cpu_scan()
10529 * y = SCHED_CAPACITY_SCALE - in update_idle_cpu_scan()
10538 pct = env->sd->imbalance_pct; in update_idle_cpu_scan()
10542 y = SCHED_CAPACITY_SCALE - tmp; in update_idle_cpu_scan()
10547 if ((int)y != sd_share->nr_idle_scan) in update_idle_cpu_scan()
10548 WRITE_ONCE(sd_share->nr_idle_scan, (int)y); in update_idle_cpu_scan()
10552 * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
10559 struct sched_group *sg = env->sd->groups; in update_sd_lb_stats()
10560 struct sg_lb_stats *local = &sds->local_stat; in update_sd_lb_stats()
10569 local_group = cpumask_test_cpu(env->dst_cpu, sched_group_span(sg)); in update_sd_lb_stats()
10571 sds->local = sg; in update_sd_lb_stats()
10574 if (env->idle != CPU_NEWLY_IDLE || in update_sd_lb_stats()
10575 time_after_eq(jiffies, sg->sgc->next_update)) in update_sd_lb_stats()
10576 update_group_capacity(env->sd, env->dst_cpu); in update_sd_lb_stats()
10586 sds->busiest = sg; in update_sd_lb_stats()
10587 sds->busiest_stat = *sgs; in update_sd_lb_stats()
10592 sds->total_load += sgs->group_load; in update_sd_lb_stats()
10593 sds->total_capacity += sgs->group_capacity; in update_sd_lb_stats()
10595 sum_util += sgs->group_util; in update_sd_lb_stats()
10596 sg = sg->next; in update_sd_lb_stats()
10597 } while (sg != env->sd->groups); in update_sd_lb_stats()
10604 if (sds->busiest) in update_sd_lb_stats()
10605 sds->prefer_sibling = !!(sds->busiest->flags & SD_PREFER_SIBLING); in update_sd_lb_stats()
10608 if (env->sd->flags & SD_NUMA) in update_sd_lb_stats()
10609 env->fbq_type = fbq_classify_group(&sds->busiest_stat); in update_sd_lb_stats()
10611 if (!env->sd->parent) { in update_sd_lb_stats()
10612 struct root_domain *rd = env->dst_rq->rd; in update_sd_lb_stats()
10615 WRITE_ONCE(rd->overload, sg_status & SG_OVERLOAD); in update_sd_lb_stats()
10617 /* Update over-utilization (tipping point, U >= 0) indicator */ in update_sd_lb_stats()
10618 WRITE_ONCE(rd->overutilized, sg_status & SG_OVERUTILIZED); in update_sd_lb_stats()
10621 struct root_domain *rd = env->dst_rq->rd; in update_sd_lb_stats()
10623 WRITE_ONCE(rd->overutilized, SG_OVERUTILIZED); in update_sd_lb_stats()
10631 * calculate_imbalance - Calculate the amount of imbalance present within the
10640 local = &sds->local_stat; in calculate_imbalance()
10641 busiest = &sds->busiest_stat; in calculate_imbalance()
10643 if (busiest->group_type == group_misfit_task) { in calculate_imbalance()
10644 if (env->sd->flags & SD_ASYM_CPUCAPACITY) { in calculate_imbalance()
10646 env->migration_type = migrate_misfit; in calculate_imbalance()
10647 env->imbalance = 1; in calculate_imbalance()
10653 env->migration_type = migrate_load; in calculate_imbalance()
10654 env->imbalance = busiest->group_misfit_task_load; in calculate_imbalance()
10659 if (busiest->group_type == group_asym_packing) { in calculate_imbalance()
10664 env->migration_type = migrate_task; in calculate_imbalance()
10665 env->imbalance = busiest->sum_h_nr_running; in calculate_imbalance()
10669 if (busiest->group_type == group_smt_balance) { in calculate_imbalance()
10671 env->migration_type = migrate_task; in calculate_imbalance()
10672 env->imbalance = 1; in calculate_imbalance()
10676 if (busiest->group_type == group_imbalanced) { in calculate_imbalance()
10678 * In the group_imb case we cannot rely on group-wide averages in calculate_imbalance()
10679 * to ensure CPU-load equilibrium, try to move any task to fix in calculate_imbalance()
10683 env->migration_type = migrate_task; in calculate_imbalance()
10684 env->imbalance = 1; in calculate_imbalance()
10692 if (local->group_type == group_has_spare) { in calculate_imbalance()
10693 if ((busiest->group_type > group_fully_busy) && in calculate_imbalance()
10694 !(env->sd->flags & SD_SHARE_PKG_RESOURCES)) { in calculate_imbalance()
10703 env->migration_type = migrate_util; in calculate_imbalance()
10704 env->imbalance = max(local->group_capacity, local->group_util) - in calculate_imbalance()
10705 local->group_util; in calculate_imbalance()
10714 if (env->idle != CPU_NOT_IDLE && env->imbalance == 0) { in calculate_imbalance()
10715 env->migration_type = migrate_task; in calculate_imbalance()
10716 env->imbalance = 1; in calculate_imbalance()
10722 if (busiest->group_weight == 1 || sds->prefer_sibling) { in calculate_imbalance()
10727 env->migration_type = migrate_task; in calculate_imbalance()
10728 env->imbalance = sibling_imbalance(env, sds, busiest, local); in calculate_imbalance()
10735 env->migration_type = migrate_task; in calculate_imbalance()
10736 env->imbalance = max_t(long, 0, in calculate_imbalance()
10737 (local->idle_cpus - busiest->idle_cpus)); in calculate_imbalance()
10742 if (env->sd->flags & SD_NUMA) { in calculate_imbalance()
10743 env->imbalance = adjust_numa_imbalance(env->imbalance, in calculate_imbalance()
10744 local->sum_nr_running + 1, in calculate_imbalance()
10745 env->sd->imb_numa_nr); in calculate_imbalance()
10750 env->imbalance >>= 1; in calculate_imbalance()
10759 if (local->group_type < group_overloaded) { in calculate_imbalance()
10765 local->avg_load = (local->group_load * SCHED_CAPACITY_SCALE) / in calculate_imbalance()
10766 local->group_capacity; in calculate_imbalance()
10772 if (local->avg_load >= busiest->avg_load) { in calculate_imbalance()
10773 env->imbalance = 0; in calculate_imbalance()
10777 sds->avg_load = (sds->total_load * SCHED_CAPACITY_SCALE) / in calculate_imbalance()
10778 sds->total_capacity; in calculate_imbalance()
10784 if (local->avg_load >= sds->avg_load) { in calculate_imbalance()
10785 env->imbalance = 0; in calculate_imbalance()
10799 env->migration_type = migrate_load; in calculate_imbalance()
10800 env->imbalance = min( in calculate_imbalance()
10801 (busiest->avg_load - sds->avg_load) * busiest->group_capacity, in calculate_imbalance()
10802 (sds->avg_load - local->avg_load) * local->group_capacity in calculate_imbalance()
10829 * find_busiest_group - Returns the busiest group within the sched_domain
10836 * Return: - The busiest group if imbalance exists.
10858 if (busiest->group_type == group_misfit_task) in find_busiest_group()
10862 struct root_domain *rd = env->dst_rq->rd; in find_busiest_group()
10864 if (rcu_dereference(rd->pd) && !READ_ONCE(rd->overutilized)) in find_busiest_group()
10869 if (busiest->group_type == group_asym_packing) in find_busiest_group()
10877 if (busiest->group_type == group_imbalanced) in find_busiest_group()
10885 if (local->group_type > busiest->group_type) in find_busiest_group()
10892 if (local->group_type == group_overloaded) { in find_busiest_group()
10897 if (local->avg_load >= busiest->avg_load) in find_busiest_group()
10908 if (local->avg_load >= sds.avg_load) in find_busiest_group()
10915 if (100 * busiest->avg_load <= in find_busiest_group()
10916 env->sd->imbalance_pct * local->avg_load) in find_busiest_group()
10924 if (sds.prefer_sibling && local->group_type == group_has_spare && in find_busiest_group()
10928 if (busiest->group_type != group_overloaded) { in find_busiest_group()
10929 if (env->idle == CPU_NOT_IDLE) { in find_busiest_group()
10938 if (busiest->group_type == group_smt_balance && in find_busiest_group()
10944 if (busiest->group_weight > 1 && in find_busiest_group()
10945 local->idle_cpus <= (busiest->idle_cpus + 1)) { in find_busiest_group()
10958 if (busiest->sum_h_nr_running == 1) { in find_busiest_group()
10969 return env->imbalance ? sds.busiest : NULL; in find_busiest_group()
10972 env->imbalance = 0; in find_busiest_group()
10977 * find_busiest_queue - find the busiest runqueue among the CPUs in the group.
10987 for_each_cpu_and(i, sched_group_span(group), env->cpus) { in find_busiest_queue()
10997 * - regular: there are !numa tasks in find_busiest_queue()
10998 * - remote: there are numa tasks that run on the 'wrong' node in find_busiest_queue()
10999 * - all: there is no distinction in find_busiest_queue()
11014 if (rt > env->fbq_type) in find_busiest_queue()
11017 nr_running = rq->cfs.h_nr_running; in find_busiest_queue()
11025 * eventually lead to active_balancing high->low capacity. in find_busiest_queue()
11026 * Higher per-CPU capacity is considered better than balancing in find_busiest_queue()
11029 if (env->sd->flags & SD_ASYM_CPUCAPACITY && in find_busiest_queue()
11030 !capacity_greater(capacity_of(env->dst_cpu), capacity) && in find_busiest_queue()
11041 if ((env->sd->flags & SD_ASYM_PACKING) && in find_busiest_queue()
11042 sched_use_asym_prio(env->sd, i) && in find_busiest_queue()
11043 sched_asym_prefer(i, env->dst_cpu) && in find_busiest_queue()
11047 switch (env->migration_type) { in find_busiest_queue()
11055 if (nr_running == 1 && load > env->imbalance && in find_busiest_queue()
11056 !check_cpu_capacity(rq, env->sd)) in find_busiest_queue()
11108 if (rq->misfit_task_load > busiest_load) { in find_busiest_queue()
11109 busiest_load = rq->misfit_task_load; in find_busiest_queue()
11140 return env->idle != CPU_NOT_IDLE && (env->sd->flags & SD_ASYM_PACKING) && in asym_active_balance()
11141 sched_use_asym_prio(env->sd, env->dst_cpu) && in asym_active_balance()
11142 (sched_asym_prefer(env->dst_cpu, env->src_cpu) || in asym_active_balance()
11143 !sched_use_asym_prio(env->sd, env->src_cpu)); in asym_active_balance()
11149 struct sched_domain *sd = env->sd; in imbalanced_active_balance()
11156 if ((env->migration_type == migrate_task) && in imbalanced_active_balance()
11157 (sd->nr_balance_failed > sd->cache_nice_tries+2)) in imbalanced_active_balance()
11165 struct sched_domain *sd = env->sd; in need_active_balance()
11179 if ((env->idle != CPU_NOT_IDLE) && in need_active_balance()
11180 (env->src_rq->cfs.h_nr_running == 1)) { in need_active_balance()
11181 if ((check_cpu_capacity(env->src_rq, sd)) && in need_active_balance()
11182 (capacity_of(env->src_cpu)*sd->imbalance_pct < capacity_of(env->dst_cpu)*100)) in need_active_balance()
11186 if (env->migration_type == migrate_misfit) in need_active_balance()
11197 struct sched_group *sg = env->sd->groups; in should_we_balance()
11198 int cpu, idle_smt = -1; in should_we_balance()
11204 if (!cpumask_test_cpu(env->dst_cpu, env->cpus)) in should_we_balance()
11214 if (env->idle == CPU_NEWLY_IDLE) { in should_we_balance()
11215 if (env->dst_rq->nr_running > 0 || env->dst_rq->ttwu_pending) in should_we_balance()
11222 for_each_cpu_and(cpu, swb_cpus, env->cpus) { in should_we_balance()
11231 if (!(env->sd->flags & SD_SHARE_CPUCAPACITY) && !is_core_idle(cpu)) { in should_we_balance()
11232 if (idle_smt == -1) in should_we_balance()
11246 * Are we the first idle core in a non-SMT domain or higher, in should_we_balance()
11249 return cpu == env->dst_cpu; in should_we_balance()
11253 if (idle_smt != -1) in should_we_balance()
11254 return idle_smt == env->dst_cpu; in should_we_balance()
11257 return group_balance_cpu(sg) == env->dst_cpu; in should_we_balance()
11269 struct sched_domain *sd_parent = sd->parent; in load_balance()
11278 .dst_grpmask = group_balance_mask(sd->groups), in load_balance()
11288 schedstat_inc(sd->lb_count[idle]); in load_balance()
11298 schedstat_inc(sd->lb_nobusyg[idle]); in load_balance()
11304 schedstat_inc(sd->lb_nobusyq[idle]); in load_balance()
11310 schedstat_add(sd->lb_imbalance[idle], env.imbalance); in load_balance()
11312 env.src_cpu = busiest->cpu; in load_balance()
11318 if (busiest->nr_running > 1) { in load_balance()
11321 * an imbalance but busiest->nr_running <= 1, the group is in load_balance()
11325 env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running); in load_balance()
11332 * cur_ld_moved - load moved in current iteration in load_balance()
11333 * ld_moved - cumulative load moved across iterations in load_balance()
11340 * unlock busiest->lock, and we are able to be sure in load_balance()
11357 if (env.loop < busiest->nr_running) in load_balance()
11371 * nohz-idle), we now have balance_cpu in a position to move in load_balance()
11382 /* Prevent to re-select dst_cpu via env's CPUs */ in load_balance()
11402 int *group_imbalance = &sd_parent->groups->sgc->imbalance; in load_balance()
11429 schedstat_inc(sd->lb_failed[idle]); in load_balance()
11437 sd->nr_balance_failed++; in load_balance()
11449 if (!cpumask_test_cpu(this_cpu, busiest->curr->cpus_ptr)) { in load_balance()
11458 * ->active_balance synchronizes accesses to in load_balance()
11459 * ->active_balance_work. Once set, it's cleared in load_balance()
11462 if (!busiest->active_balance) { in load_balance()
11463 busiest->active_balance = 1; in load_balance()
11464 busiest->push_cpu = this_cpu; in load_balance()
11473 &busiest->active_balance_work); in load_balance()
11478 sd->nr_balance_failed = 0; in load_balance()
11483 sd->balance_interval = sd->min_interval; in load_balance()
11495 int *group_imbalance = &sd_parent->groups->sgc->imbalance; in load_balance()
11507 schedstat_inc(sd->lb_balanced[idle]); in load_balance()
11509 sd->nr_balance_failed = 0; in load_balance()
11525 sd->balance_interval < MAX_PINNED_INTERVAL) || in load_balance()
11526 sd->balance_interval < sd->max_interval) in load_balance()
11527 sd->balance_interval *= 2; in load_balance()
11535 unsigned long interval = sd->balance_interval; in get_sd_balance_interval()
11538 interval *= sd->busy_factor; in get_sd_balance_interval()
11549 interval -= 1; in get_sd_balance_interval()
11563 next = sd->last_balance + interval; in update_next_balance()
11579 int target_cpu = busiest_rq->push_cpu; in active_load_balance_cpu_stop()
11587 * Between queueing the stop-work and running it is a hole in which in active_load_balance_cpu_stop()
11596 !busiest_rq->active_balance)) in active_load_balance_cpu_stop()
11600 if (busiest_rq->nr_running <= 1) in active_load_balance_cpu_stop()
11606 * Bjorn Helgaas on a 128-CPU setup. in active_load_balance_cpu_stop()
11622 .src_cpu = busiest_rq->cpu, in active_load_balance_cpu_stop()
11628 schedstat_inc(sd->alb_count); in active_load_balance_cpu_stop()
11633 schedstat_inc(sd->alb_pushed); in active_load_balance_cpu_stop()
11635 sd->nr_balance_failed = 0; in active_load_balance_cpu_stop()
11637 schedstat_inc(sd->alb_failed); in active_load_balance_cpu_stop()
11642 busiest_rq->active_balance = 0; in active_load_balance_cpu_stop()
11657 * This trades load-balance latency on larger machines for less cross talk.
11666 if (cost > sd->max_newidle_lb_cost) { in update_newidle_cost()
11671 sd->max_newidle_lb_cost = cost; in update_newidle_cost()
11672 sd->last_decay_max_lb_cost = jiffies; in update_newidle_cost()
11673 } else if (time_after(jiffies, sd->last_decay_max_lb_cost + HZ)) { in update_newidle_cost()
11679 sd->max_newidle_lb_cost = (sd->max_newidle_lb_cost * 253) / 256; in update_newidle_cost()
11680 sd->last_decay_max_lb_cost = jiffies; in update_newidle_cost()
11697 int cpu = rq->cpu; in rebalance_domains()
11714 max_cost += sd->max_newidle_lb_cost; in rebalance_domains()
11729 need_serialize = sd->flags & SD_SERIALIZE; in rebalance_domains()
11735 if (time_after_eq(jiffies, sd->last_balance + interval)) { in rebalance_domains()
11739 * env->dst_cpu, so we can't know our idle in rebalance_domains()
11745 sd->last_balance = jiffies; in rebalance_domains()
11751 if (time_after(next_balance, sd->last_balance + interval)) { in rebalance_domains()
11752 next_balance = sd->last_balance + interval; in rebalance_domains()
11758 * Ensure the rq-wide value also decays but keep it at a in rebalance_domains()
11759 * reasonable floor to avoid funnies with rq->avg_idle. in rebalance_domains()
11761 rq->max_idle_balance_cost = in rebalance_domains()
11772 rq->next_balance = next_balance; in rebalance_domains()
11778 return unlikely(!rcu_dereference_sched(rq->sd)); in on_null_domain()
11785 * - When one of the busy CPUs notices that there may be an idle rebalancing
11789 * - HK_TYPE_MISC CPUs are used for this task, because HK_TYPE_SCHED is not set
11808 return -1; in find_new_ilb()
11812 * Kick a CPU to do the NOHZ balancing, if it is time for it, via a cross-CPU
11845 smp_call_function_single_async(ilb_cpu, &cpu_rq(ilb_cpu)->nohz_csd); in kick_ilb()
11857 int nr_busy, i, cpu = rq->cpu; in nohz_balancer_kick()
11860 if (unlikely(rq->idle_balance)) in nohz_balancer_kick()
11883 if (rq->nr_running >= 2) { in nohz_balancer_kick()
11890 sd = rcu_dereference(rq->sd); in nohz_balancer_kick()
11896 if (rq->cfs.h_nr_running >= 1 && check_cpu_capacity(rq, sd)) { in nohz_balancer_kick()
11946 * increase the overall cache utilization), we need a less-loaded LLC in nohz_balancer_kick()
11950 * the others are - so just get a NOHZ balance going if it looks in nohz_balancer_kick()
11953 nr_busy = atomic_read(&sds->nr_busy_cpus); in nohz_balancer_kick()
11976 if (!sd || !sd->nohz_idle) in set_cpu_sd_state_busy()
11978 sd->nohz_idle = 0; in set_cpu_sd_state_busy()
11980 atomic_inc(&sd->shared->nr_busy_cpus); in set_cpu_sd_state_busy()
11989 if (likely(!rq->nohz_tick_stopped)) in nohz_balance_exit_idle()
11992 rq->nohz_tick_stopped = 0; in nohz_balance_exit_idle()
11993 cpumask_clear_cpu(rq->cpu, nohz.idle_cpus_mask); in nohz_balance_exit_idle()
11996 set_cpu_sd_state_busy(rq->cpu); in nohz_balance_exit_idle()
12006 if (!sd || sd->nohz_idle) in set_cpu_sd_state_idle()
12008 sd->nohz_idle = 1; in set_cpu_sd_state_idle()
12010 atomic_dec(&sd->shared->nr_busy_cpus); in set_cpu_sd_state_idle()
12034 * Can be set safely without rq->lock held in nohz_balance_enter_idle()
12036 * rq->lock is held during the check and the clear in nohz_balance_enter_idle()
12038 rq->has_blocked_load = 1; in nohz_balance_enter_idle()
12046 if (rq->nohz_tick_stopped) in nohz_balance_enter_idle()
12053 rq->nohz_tick_stopped = 1; in nohz_balance_enter_idle()
12078 unsigned int cpu = rq->cpu; in update_nohz_stats()
12080 if (!rq->has_blocked_load) in update_nohz_stats()
12086 if (!time_after(jiffies, READ_ONCE(rq->last_blocked_load_update_tick))) in update_nohz_stats()
12091 return rq->has_blocked_load; in update_nohz_stats()
12106 int this_cpu = this_rq->cpu; in _nohz_idle_balance()
12163 if (time_after_eq(jiffies, rq->next_balance)) { in _nohz_idle_balance()
12174 if (time_after(next_balance, rq->next_balance)) { in _nohz_idle_balance()
12175 next_balance = rq->next_balance; in _nohz_idle_balance()
12204 unsigned int flags = this_rq->nohz_idle_balance; in nohz_idle_balance()
12209 this_rq->nohz_idle_balance = 0; in nohz_idle_balance()
12250 int this_cpu = this_rq->cpu; in nohz_newidle_balance()
12260 if (this_rq->avg_idle < sysctl_sched_migration_cost) in nohz_newidle_balance()
12291 * < 0 - we released the lock and there are !fair tasks present
12292 * 0 - failed, no new tasks
12293 * > 0 - success, new (fair) tasks present
12298 int this_cpu = this_rq->cpu; in newidle_balance()
12309 if (this_rq->ttwu_pending) in newidle_balance()
12316 this_rq->idle_stamp = rq_clock(this_rq); in newidle_balance()
12326 * for load-balance and preemption/IRQs are still disabled avoiding in newidle_balance()
12328 * re-start the picking loop. in newidle_balance()
12333 sd = rcu_dereference_check_sched_domain(this_rq->sd); in newidle_balance()
12335 if (!READ_ONCE(this_rq->rd->overload) || in newidle_balance()
12336 (sd && this_rq->avg_idle < sd->max_newidle_lb_cost)) { in newidle_balance()
12358 if (this_rq->avg_idle < curr_cost + sd->max_newidle_lb_cost) in newidle_balance()
12361 if (sd->flags & SD_BALANCE_NEWIDLE) { in newidle_balance()
12368 domain_cost = t1 - t0; in newidle_balance()
12379 if (pulled_task || this_rq->nr_running > 0 || in newidle_balance()
12380 this_rq->ttwu_pending) in newidle_balance()
12387 if (curr_cost > this_rq->max_idle_balance_cost) in newidle_balance()
12388 this_rq->max_idle_balance_cost = curr_cost; in newidle_balance()
12395 if (this_rq->cfs.h_nr_running && !pulled_task) in newidle_balance()
12399 if (this_rq->nr_running != this_rq->cfs.h_nr_running) in newidle_balance()
12400 pulled_task = -1; in newidle_balance()
12404 if (time_after(this_rq->next_balance, next_balance)) in newidle_balance()
12405 this_rq->next_balance = next_balance; in newidle_balance()
12408 this_rq->idle_stamp = 0; in newidle_balance()
12424 enum cpu_idle_type idle = this_rq->idle_balance ? in run_rebalance_domains()
12439 update_blocked_averages(this_rq->cpu); in run_rebalance_domains()
12455 if (time_after_eq(jiffies, rq->next_balance)) in trigger_load_balance()
12485 u64 rtime = se->sum_exec_runtime - se->prev_sum_exec_runtime; in __entity_slice_used()
12486 u64 slice = se->slice; in __entity_slice_used()
12508 * MIN_NR_TASKS_DURING_FORCEIDLE - 1 tasks and use that to check in task_tick_core()
12511 if (rq->core->core_forceidle_count && rq->cfs.nr_running == 1 && in task_tick_core()
12512 __entity_slice_used(&curr->se, MIN_NR_TASKS_DURING_FORCEIDLE)) in task_tick_core()
12517 * se_fi_update - Update the cfs_rq->min_vruntime_fi in a CFS hierarchy if needed.
12526 if (cfs_rq->forceidle_seq == fi_seq) in se_fi_update()
12528 cfs_rq->forceidle_seq = fi_seq; in se_fi_update()
12531 cfs_rq->min_vruntime_fi = cfs_rq->min_vruntime; in se_fi_update()
12537 struct sched_entity *se = &p->se; in task_vruntime_update()
12539 if (p->sched_class != &fair_sched_class) in task_vruntime_update()
12542 se_fi_update(se, rq->core->core_forceidle_seq, in_fi); in task_vruntime_update()
12549 const struct sched_entity *sea = &a->se; in cfs_prio_less()
12550 const struct sched_entity *seb = &b->se; in cfs_prio_less()
12555 SCHED_WARN_ON(task_rq(b)->core != rq->core); in cfs_prio_less()
12562 while (sea->cfs_rq->tg != seb->cfs_rq->tg) { in cfs_prio_less()
12563 int sea_depth = sea->depth; in cfs_prio_less()
12564 int seb_depth = seb->depth; in cfs_prio_less()
12572 se_fi_update(sea, rq->core->core_forceidle_seq, in_fi); in cfs_prio_less()
12573 se_fi_update(seb, rq->core->core_forceidle_seq, in_fi); in cfs_prio_less()
12575 cfs_rqa = sea->cfs_rq; in cfs_prio_less()
12576 cfs_rqb = seb->cfs_rq; in cfs_prio_less()
12578 cfs_rqa = &task_rq(a)->cfs; in cfs_prio_less()
12579 cfs_rqb = &task_rq(b)->cfs; in cfs_prio_less()
12587 delta = (s64)(sea->vruntime - seb->vruntime) + in cfs_prio_less()
12588 (s64)(cfs_rqb->min_vruntime_fi - cfs_rqa->min_vruntime_fi); in cfs_prio_less()
12598 cfs_rq = task_group(p)->cfs_rq[cpu]; in task_is_throttled_fair()
12600 cfs_rq = &cpu_rq(cpu)->cfs; in task_is_throttled_fair()
12619 struct sched_entity *se = &curr->se; in task_tick_fair()
12637 * - child not yet on the tasklist
12638 * - preemption disabled
12642 struct sched_entity *se = &p->se, *curr; in task_fork_fair()
12651 curr = cfs_rq->curr; in task_fork_fair()
12668 if (rq->cfs.nr_running == 1) in prio_changed_fair()
12677 if (p->prio > oldprio) in prio_changed_fair()
12699 se = se->parent; in propagate_entity_cfs_rq()
12724 * - A forked task which hasn't been woken up by wake_up_new_task(). in detach_entity_cfs_rq()
12725 * - A task which has been woken up by try_to_wake_up() but is in detach_entity_cfs_rq()
12728 if (!se->avg.last_update_time) in detach_entity_cfs_rq()
12752 struct sched_entity *se = &p->se; in detach_task_cfs_rq()
12759 struct sched_entity *se = &p->se; in attach_task_cfs_rq()
12788 * This routine is mostly called to set cfs_rq->curr field when a task
12793 struct sched_entity *se = &p->se; in set_next_task_fair()
12801 list_move(&se->group_node, &rq->cfs_tasks); in set_next_task_fair()
12816 cfs_rq->tasks_timeline = RB_ROOT_CACHED; in init_cfs_rq()
12817 u64_u32_store(cfs_rq->min_vruntime, (u64)(-(1LL << 20))); in init_cfs_rq()
12819 raw_spin_lock_init(&cfs_rq->removed.lock); in init_cfs_rq()
12830 if (READ_ONCE(p->__state) == TASK_NEW) in task_change_group_fair()
12836 /* Tell se's cfs_rq has been changed -- migrated */ in task_change_group_fair()
12837 p->se.avg.last_update_time = 0; in task_change_group_fair()
12848 if (tg->cfs_rq) in free_fair_sched_group()
12849 kfree(tg->cfs_rq[i]); in free_fair_sched_group()
12850 if (tg->se) in free_fair_sched_group()
12851 kfree(tg->se[i]); in free_fair_sched_group()
12854 kfree(tg->cfs_rq); in free_fair_sched_group()
12855 kfree(tg->se); in free_fair_sched_group()
12864 tg->cfs_rq = kcalloc(nr_cpu_ids, sizeof(cfs_rq), GFP_KERNEL); in alloc_fair_sched_group()
12865 if (!tg->cfs_rq) in alloc_fair_sched_group()
12867 tg->se = kcalloc(nr_cpu_ids, sizeof(se), GFP_KERNEL); in alloc_fair_sched_group()
12868 if (!tg->se) in alloc_fair_sched_group()
12871 tg->shares = NICE_0_LOAD; in alloc_fair_sched_group()
12887 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]); in alloc_fair_sched_group()
12908 se = tg->se[i]; in online_fair_sched_group()
12926 if (tg->se[cpu]) in unregister_fair_sched_group()
12927 remove_entity_load_avg(tg->se[cpu]); in unregister_fair_sched_group()
12931 * check on_list without danger of it being re-added. in unregister_fair_sched_group()
12933 if (!tg->cfs_rq[cpu]->on_list) in unregister_fair_sched_group()
12939 list_del_leaf_cfs_rq(tg->cfs_rq[cpu]); in unregister_fair_sched_group()
12950 cfs_rq->tg = tg; in init_tg_cfs_entry()
12951 cfs_rq->rq = rq; in init_tg_cfs_entry()
12954 tg->cfs_rq[cpu] = cfs_rq; in init_tg_cfs_entry()
12955 tg->se[cpu] = se; in init_tg_cfs_entry()
12962 se->cfs_rq = &rq->cfs; in init_tg_cfs_entry()
12963 se->depth = 0; in init_tg_cfs_entry()
12965 se->cfs_rq = parent->my_q; in init_tg_cfs_entry()
12966 se->depth = parent->depth + 1; in init_tg_cfs_entry()
12969 se->my_q = cfs_rq; in init_tg_cfs_entry()
12971 update_load_set(&se->load, NICE_0_LOAD); in init_tg_cfs_entry()
12972 se->parent = parent; in init_tg_cfs_entry()
12986 if (!tg->se[0]) in __sched_group_set_shares()
12987 return -EINVAL; in __sched_group_set_shares()
12991 if (tg->shares == shares) in __sched_group_set_shares()
12994 tg->shares = shares; in __sched_group_set_shares()
12997 struct sched_entity *se = tg->se[i]; in __sched_group_set_shares()
13019 ret = -EINVAL; in sched_group_set_shares()
13032 return -EINVAL; in sched_group_set_idle()
13035 return -EINVAL; in sched_group_set_idle()
13039 if (tg->idle == idle) { in sched_group_set_idle()
13044 tg->idle = idle; in sched_group_set_idle()
13048 struct sched_entity *se = tg->se[i]; in sched_group_set_idle()
13049 struct cfs_rq *parent_cfs_rq, *grp_cfs_rq = tg->cfs_rq[i]; in sched_group_set_idle()
13056 grp_cfs_rq->idle = idle; in sched_group_set_idle()
13060 if (se->on_rq) { in sched_group_set_idle()
13063 parent_cfs_rq->idle_nr_running++; in sched_group_set_idle()
13065 parent_cfs_rq->idle_nr_running--; in sched_group_set_idle()
13068 idle_task_delta = grp_cfs_rq->h_nr_running - in sched_group_set_idle()
13069 grp_cfs_rq->idle_h_nr_running; in sched_group_set_idle()
13071 idle_task_delta *= -1; in sched_group_set_idle()
13076 if (!se->on_rq) in sched_group_set_idle()
13079 cfs_rq->idle_h_nr_running += idle_task_delta; in sched_group_set_idle()
13105 struct sched_entity *se = &task->se; in get_rr_interval_fair()
13112 if (rq->cfs.load.weight) in get_rr_interval_fair()
13113 rr_interval = NS_TO_JIFFIES(se->slice); in get_rr_interval_fair()
13190 ng = rcu_dereference(p->numa_group); in show_numa_stats()
13192 if (p->numa_faults) { in show_numa_stats()
13193 tsf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 0)]; in show_numa_stats()
13194 tpf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 1)]; in show_numa_stats()
13197 gsf = ng->faults[task_faults_idx(NUMA_MEM, node, 0)], in show_numa_stats()
13198 gpf = ng->faults[task_faults_idx(NUMA_MEM, node, 1)]; in show_numa_stats()
13219 INIT_CSD(&cpu_rq(i)->cfsb_csd, __cfsb_csd_unthrottle, cpu_rq(i)); in init_sched_fair_class()
13220 INIT_LIST_HEAD(&cpu_rq(i)->cfsb_csd_list); in init_sched_fair_class()