Lines Matching +full:lock +full:- +full:detect +full:- +full:precision +full:- +full:6 +full:ns +full:- +full:enable

1 // SPDX-License-Identifier: GPL-2.0
44 #include <linux/memory-tiers.h>
62 * The initial- and re-scaling of tunables is configurable
66 * SCHED_TUNABLESCALING_NONE - unscaled, always *1
67 * SCHED_TUNABLESCALING_LOG - scaled logarithmically, *1+ilog(ncpus)
68 * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus
75 * Minimal preemption granularity for CPU-bound tasks:
97 return -cpu; in arch_asym_cpu_priority()
118 * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool
169 lw->weight += inc; in update_load_add()
170 lw->inv_weight = 0; in update_load_add()
175 lw->weight -= dec; in update_load_sub()
176 lw->inv_weight = 0; in update_load_sub()
181 lw->weight = w; in update_load_set()
182 lw->inv_weight = 0; in update_load_set()
189 * so pick a second-best guess by going with the log2 of the
237 if (likely(lw->inv_weight)) in __update_inv_weight()
240 w = scale_load_down(lw->weight); in __update_inv_weight()
243 lw->inv_weight = 1; in __update_inv_weight()
245 lw->inv_weight = WMULT_CONST; in __update_inv_weight()
247 lw->inv_weight = WMULT_CONST / w; in __update_inv_weight()
253 * (delta_exec * (weight * lw->inv_weight)) >> WMULT_SHIFT
273 shift -= fs; in __calc_delta()
277 fact = mul_u32_u32(fact, lw->inv_weight); in __calc_delta()
282 shift -= fs; in __calc_delta()
294 if (unlikely(se->load.weight != NICE_0_LOAD)) in calc_delta_fair()
295 delta = __calc_delta(delta, NICE_0_LOAD, &se->load); in calc_delta_fair()
310 for (; se; se = se->parent)
317 if (cfs_rq->on_list) in list_add_leaf_cfs_rq()
318 return rq->tmp_alone_branch == &rq->leaf_cfs_rq_list; in list_add_leaf_cfs_rq()
320 cfs_rq->on_list = 1; in list_add_leaf_cfs_rq()
325 * enqueued. The fact that we always enqueue bottom-up in list_add_leaf_cfs_rq()
331 if (cfs_rq->tg->parent && in list_add_leaf_cfs_rq()
332 cfs_rq->tg->parent->cfs_rq[cpu]->on_list) { in list_add_leaf_cfs_rq()
339 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list, in list_add_leaf_cfs_rq()
340 &(cfs_rq->tg->parent->cfs_rq[cpu]->leaf_cfs_rq_list)); in list_add_leaf_cfs_rq()
346 rq->tmp_alone_branch = &rq->leaf_cfs_rq_list; in list_add_leaf_cfs_rq()
350 if (!cfs_rq->tg->parent) { in list_add_leaf_cfs_rq()
355 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list, in list_add_leaf_cfs_rq()
356 &rq->leaf_cfs_rq_list); in list_add_leaf_cfs_rq()
361 rq->tmp_alone_branch = &rq->leaf_cfs_rq_list; in list_add_leaf_cfs_rq()
371 list_add_rcu(&cfs_rq->leaf_cfs_rq_list, rq->tmp_alone_branch); in list_add_leaf_cfs_rq()
376 rq->tmp_alone_branch = &cfs_rq->leaf_cfs_rq_list; in list_add_leaf_cfs_rq()
382 if (cfs_rq->on_list) { in list_del_leaf_cfs_rq()
389 * to the prev element but it will point to rq->leaf_cfs_rq_list in list_del_leaf_cfs_rq()
392 if (rq->tmp_alone_branch == &cfs_rq->leaf_cfs_rq_list) in list_del_leaf_cfs_rq()
393 rq->tmp_alone_branch = cfs_rq->leaf_cfs_rq_list.prev; in list_del_leaf_cfs_rq()
395 list_del_rcu(&cfs_rq->leaf_cfs_rq_list); in list_del_leaf_cfs_rq()
396 cfs_rq->on_list = 0; in list_del_leaf_cfs_rq()
402 WARN_ON_ONCE(rq->tmp_alone_branch != &rq->leaf_cfs_rq_list); in assert_list_leaf_cfs_rq()
407 list_for_each_entry_safe(cfs_rq, pos, &rq->leaf_cfs_rq_list, \
414 if (se->cfs_rq == pse->cfs_rq) in is_same_group()
415 return se->cfs_rq; in is_same_group()
422 return se->parent; in parent_entity()
438 se_depth = (*se)->depth; in find_matching_se()
439 pse_depth = (*pse)->depth; in find_matching_se()
442 se_depth--; in find_matching_se()
447 pse_depth--; in find_matching_se()
459 return tg->idle > 0; in tg_is_idle()
464 return cfs_rq->idle > 0; in cfs_rq_is_idle()
493 for (cfs_rq = &rq->cfs, pos = NULL; cfs_rq; cfs_rq = pos)
531 s64 delta = (s64)(vruntime - max_vruntime); in max_vruntime()
540 s64 delta = (s64)(vruntime - min_vruntime); in min_vruntime()
554 return (s64)(a->deadline - b->deadline) < 0; in entity_before()
559 return (s64)(se->vruntime - cfs_rq->min_vruntime); in entity_key()
566 * Compute virtual time from the per-task service numbers:
574 * lag_i = S - s_i = w_i * (V - v_i)
580 * \Sum w_i * (V - v_i) = 0
581 * \Sum w_i * V - w_i * v_i = 0
584 * se->vruntime):
587 * V = -------------- = --------------
594 * virtual time has non-contiguous motion equivalent to:
596 * V +-= lag_i / W
603 * Substitute: v_i == (v_i - v0) + v0
605 * \Sum ((v_i - v0) + v0) * w_i \Sum (v_i - v0) * w_i
606 * V = ---------------------------- = --------------------- + v0
611 * v0 := cfs_rq->min_vruntime
612 * \Sum (v_i - v0) * w_i := cfs_rq->avg_vruntime
613 * \Sum w_i := cfs_rq->avg_load
616 * the per-task service, these deltas: (v_i - v), will be in the order of the
626 unsigned long weight = scale_load_down(se->load.weight); in avg_vruntime_add()
629 cfs_rq->avg_vruntime += key * weight; in avg_vruntime_add()
630 cfs_rq->avg_load += weight; in avg_vruntime_add()
636 unsigned long weight = scale_load_down(se->load.weight); in avg_vruntime_sub()
639 cfs_rq->avg_vruntime -= key * weight; in avg_vruntime_sub()
640 cfs_rq->avg_load -= weight; in avg_vruntime_sub()
647 * v' = v + d ==> avg_vruntime' = avg_runtime - d*avg_load in avg_vruntime_update()
649 cfs_rq->avg_vruntime -= cfs_rq->avg_load * delta; in avg_vruntime_update()
658 struct sched_entity *curr = cfs_rq->curr; in avg_vruntime()
659 s64 avg = cfs_rq->avg_vruntime; in avg_vruntime()
660 long load = cfs_rq->avg_load; in avg_vruntime()
662 if (curr && curr->on_rq) { in avg_vruntime()
663 unsigned long weight = scale_load_down(curr->load.weight); in avg_vruntime()
672 avg -= (load - 1); in avg_vruntime()
676 return cfs_rq->min_vruntime + avg; in avg_vruntime()
680 * lag_i = S - s_i = w_i * (V - v_i)
683 * is possible -- by addition/removal/reweight to the tree -- to move V around
691 * -r_max < lag < max(r_max, q)
699 WARN_ON_ONCE(!se->on_rq); in update_entity_lag()
701 vlag = avg_vruntime(cfs_rq) - se->vruntime; in update_entity_lag()
702 limit = calc_delta_fair(max_t(u64, 2*se->slice, TICK_NSEC), se); in update_entity_lag()
704 se->vlag = clamp(vlag, -limit, limit); in update_entity_lag()
711 * lag_i = S - s_i = w_i*(V - v_i)
713 * lag_i >= 0 -> V >= v_i
715 * \Sum (v_i - v)*w_i
716 * V = ------------------ + v
719 * lag_i >= 0 -> \Sum (v_i - v)*w_i >= (v_i - v)*(\Sum w_i)
721 * Note: using 'avg_vruntime() > se->vruntime' is inaccurate due
722 * to the loss in precision caused by the division.
726 struct sched_entity *curr = cfs_rq->curr; in vruntime_eligible()
727 s64 avg = cfs_rq->avg_vruntime; in vruntime_eligible()
728 long load = cfs_rq->avg_load; in vruntime_eligible()
730 if (curr && curr->on_rq) { in vruntime_eligible()
731 unsigned long weight = scale_load_down(curr->load.weight); in vruntime_eligible()
737 return avg >= (s64)(vruntime - cfs_rq->min_vruntime) * load; in vruntime_eligible()
742 return vruntime_eligible(cfs_rq, se->vruntime); in entity_eligible()
747 u64 min_vruntime = cfs_rq->min_vruntime; in __update_min_vruntime()
751 s64 delta = (s64)(vruntime - min_vruntime); in __update_min_vruntime()
762 struct sched_entity *curr = cfs_rq->curr; in update_min_vruntime()
763 u64 vruntime = cfs_rq->min_vruntime; in update_min_vruntime()
766 if (curr->on_rq) in update_min_vruntime()
767 vruntime = curr->vruntime; in update_min_vruntime()
774 vruntime = se->min_vruntime; in update_min_vruntime()
776 vruntime = min_vruntime(vruntime, se->min_vruntime); in update_min_vruntime()
780 cfs_rq->min_vruntime = __update_min_vruntime(cfs_rq, vruntime); in update_min_vruntime()
786 struct sched_entity *curr = cfs_rq->curr; in cfs_rq_min_slice()
789 if (curr && curr->on_rq) in cfs_rq_min_slice()
790 min_slice = curr->slice; in cfs_rq_min_slice()
793 min_slice = min(min_slice, root->min_slice); in cfs_rq_min_slice()
803 #define vruntime_gt(field, lse, rse) ({ (s64)((lse)->field - (rse)->field) > 0; })
810 se->min_vruntime = rse->min_vruntime; in __min_vruntime_update()
818 if (rse->min_slice < se->min_slice) in __min_slice_update()
819 se->min_slice = rse->min_slice; in __min_slice_update()
824 * se->min_vruntime = min(se->vruntime, {left,right}->min_vruntime)
828 u64 old_min_vruntime = se->min_vruntime; in min_vruntime_update()
829 u64 old_min_slice = se->min_slice; in min_vruntime_update()
830 struct rb_node *node = &se->run_node; in min_vruntime_update()
832 se->min_vruntime = se->vruntime; in min_vruntime_update()
833 __min_vruntime_update(se, node->rb_right); in min_vruntime_update()
834 __min_vruntime_update(se, node->rb_left); in min_vruntime_update()
836 se->min_slice = se->slice; in min_vruntime_update()
837 __min_slice_update(se, node->rb_right); in min_vruntime_update()
838 __min_slice_update(se, node->rb_left); in min_vruntime_update()
840 return se->min_vruntime == old_min_vruntime && in min_vruntime_update()
841 se->min_slice == old_min_slice; in min_vruntime_update()
848 * Enqueue an entity into the rb-tree:
853 se->min_vruntime = se->vruntime; in __enqueue_entity()
854 se->min_slice = se->slice; in __enqueue_entity()
855 rb_add_augmented_cached(&se->run_node, &cfs_rq->tasks_timeline, in __enqueue_entity()
861 rb_erase_augmented_cached(&se->run_node, &cfs_rq->tasks_timeline, in __dequeue_entity()
868 struct rb_node *root = cfs_rq->tasks_timeline.rb_root.rb_node; in __pick_root_entity()
878 struct rb_node *left = rb_first_cached(&cfs_rq->tasks_timeline); in __pick_first_entity()
892 se->vlag = se->deadline; in set_protect_slice()
897 return se->vlag == se->deadline; in protect_slice()
903 se->vlag = se->deadline + 1; in cancel_protect_slice()
917 * We can do this in O(log n) time due to an augmented RB-tree. The
921 * se->min_vruntime = min(se->vruntime, se->{left,right}->min_vruntime)
927 struct rb_node *node = cfs_rq->tasks_timeline.rb_root.rb_node; in pick_eevdf()
929 struct sched_entity *curr = cfs_rq->curr; in pick_eevdf()
936 if (cfs_rq->nr_queued == 1) in pick_eevdf()
937 return curr && curr->on_rq ? curr : se; in pick_eevdf()
939 if (curr && (!curr->on_rq || !entity_eligible(cfs_rq, curr))) in pick_eevdf()
953 struct rb_node *left = node->rb_left; in pick_eevdf()
960 __node_2_se(left)->min_vruntime)) { in pick_eevdf()
977 node = node->rb_right; in pick_eevdf()
988 struct rb_node *last = rb_last(&cfs_rq->tasks_timeline.rb_root); in __pick_last_entity()
1021 if ((s64)(se->vruntime - se->deadline) < 0) in update_deadline()
1029 if (!se->custom_slice) in update_deadline()
1030 se->slice = sysctl_sched_base_slice; in update_deadline()
1035 se->deadline = se->vruntime + calc_delta_fair(se->slice, se); in update_deadline()
1053 struct sched_avg *sa = &se->avg; in init_entity_runnable_average()
1064 sa->load_avg = scale_load_down(se->load.weight); in init_entity_runnable_average()
1073 * util_avg = cfs_rq->avg.util_avg / (cfs_rq->avg.load_avg + 1)
1083 * util_avg_cap = (cpu_scale - cfs_rq->avg.util_avg) / 2^n
1098 struct sched_entity *se = &p->se; in post_init_entity_util_avg()
1100 struct sched_avg *sa = &se->avg; in post_init_entity_util_avg()
1102 long cap = (long)(cpu_scale - cfs_rq->avg.util_avg) / 2; in post_init_entity_util_avg()
1104 if (p->sched_class != &fair_sched_class) { in post_init_entity_util_avg()
1115 se->avg.last_update_time = cfs_rq_clock_pelt(cfs_rq); in post_init_entity_util_avg()
1120 if (cfs_rq->avg.util_avg != 0) { in post_init_entity_util_avg()
1121 sa->util_avg = cfs_rq->avg.util_avg * se_weight(se); in post_init_entity_util_avg()
1122 sa->util_avg /= (cfs_rq->avg.load_avg + 1); in post_init_entity_util_avg()
1124 if (sa->util_avg > cap) in post_init_entity_util_avg()
1125 sa->util_avg = cap; in post_init_entity_util_avg()
1127 sa->util_avg = cap; in post_init_entity_util_avg()
1131 sa->runnable_avg = sa->util_avg; in post_init_entity_util_avg()
1151 delta_exec = now - curr->exec_start; in update_curr_se()
1155 curr->exec_start = now; in update_curr_se()
1156 curr->sum_exec_runtime += delta_exec; in update_curr_se()
1162 __schedstat_set(stats->exec_max, in update_curr_se()
1163 max(delta_exec, stats->exec_max)); in update_curr_se()
1181 if (curr->vlag == curr->deadline) in did_preempt_short()
1193 if (pse->slice >= se->slice) in do_preempt_short()
1213 struct task_struct *donor = rq->donor; in update_curr_common()
1216 delta_exec = update_curr_se(rq, &donor->se); in update_curr_common()
1228 struct sched_entity *curr = cfs_rq->curr; in update_curr()
1240 curr->vruntime += calc_delta_fair(delta_exec, curr); in update_curr()
1253 * - If the task is running on behalf of fair_server, we need in update_curr()
1255 * - Fair task that runs outside of fair_server should account in update_curr()
1259 if (dl_server_active(&rq->fair_server)) in update_curr()
1260 dl_server_update(&rq->fair_server, delta_exec); in update_curr()
1265 if (cfs_rq->nr_queued == 1) in update_curr()
1276 update_curr(cfs_rq_of(&rq->donor->se)); in update_curr_fair()
1309 * maybe already in the runqueue, the se->statistics.wait_start in update_stats_wait_end_fair()
1313 if (unlikely(!schedstat_val(stats->wait_start))) in update_stats_wait_end_fair()
1340 * Task is being enqueued - update stats:
1352 if (se != cfs_rq->curr) in update_stats_enqueue_fair()
1370 if (se != cfs_rq->curr) in update_stats_dequeue_fair()
1378 state = READ_ONCE(tsk->__state); in update_stats_dequeue_fair()
1380 __schedstat_set(tsk->stats.sleep_start, in update_stats_dequeue_fair()
1383 __schedstat_set(tsk->stats.block_start, in update_stats_dequeue_fair()
1389 * We are picking a new current task - update its stats:
1397 se->exec_start = rq_clock_task(rq_of(cfs_rq)); in update_stats_curr_start()
1469 spinlock_t lock; /* nr_tasks, tasks */ member
1489 * ->numa_group (see struct task_struct for locking rules).
1493 return rcu_dereference_check(p->numa_group, p == current || in deref_task_numa_group()
1494 (lockdep_is_held(__rq_lockp(task_rq(p))) && !READ_ONCE(p->on_cpu))); in deref_task_numa_group()
1499 return rcu_dereference_protected(p->numa_group, p == current); in deref_curr_numa_group()
1511 * Calculations based on RSS as non-present and empty pages are skipped in task_nr_scan_windows()
1515 nr_scan_pages = sysctl_numa_balancing_scan_size << (20 - PAGE_SHIFT); in task_nr_scan_windows()
1516 rss = get_mm_rss(p->mm); in task_nr_scan_windows()
1549 ng = rcu_dereference(p->numa_group); in task_scan_start()
1554 period *= refcount_read(&ng->refcount); in task_scan_start()
1579 period *= refcount_read(&ng->refcount); in task_scan_max()
1591 rq->nr_numa_running += (p->numa_preferred_nid != NUMA_NO_NODE); in account_numa_enqueue()
1592 rq->nr_preferred_running += (p->numa_preferred_nid == task_node(p)); in account_numa_enqueue()
1597 rq->nr_numa_running -= (p->numa_preferred_nid != NUMA_NO_NODE); in account_numa_dequeue()
1598 rq->nr_preferred_running -= (p->numa_preferred_nid == task_node(p)); in account_numa_dequeue()
1616 ng = rcu_dereference(p->numa_group); in task_numa_group_id()
1618 gid = ng->gid; in task_numa_group_id()
1637 if (!p->numa_faults) in task_faults()
1640 return p->numa_faults[task_faults_idx(NUMA_MEM, nid, 0)] + in task_faults()
1641 p->numa_faults[task_faults_idx(NUMA_MEM, nid, 1)]; in task_faults()
1651 return ng->faults[task_faults_idx(NUMA_MEM, nid, 0)] + in group_faults()
1652 ng->faults[task_faults_idx(NUMA_MEM, nid, 1)]; in group_faults()
1657 return group->faults[task_faults_idx(NUMA_CPU, nid, 0)] + in group_faults_cpu()
1658 group->faults[task_faults_idx(NUMA_CPU, nid, 1)]; in group_faults_cpu()
1667 faults += ng->faults[task_faults_idx(NUMA_MEM, node, 1)]; in group_faults_priv()
1679 faults += ng->faults[task_faults_idx(NUMA_MEM, node, 0)]; in group_faults_shared()
1687 * considered part of a numa group's pseudo-interleaving set. Migrations
1694 return group_faults_cpu(ng, nid) * ACTIVE_NODE_FRACTION > ng->max_faults_cpu; in numa_is_active_node()
1753 faults *= (max_dist - dist); in score_nearby_nodes()
1754 faults /= (max_dist - LOCAL_DISTANCE); in score_nearby_nodes()
1774 if (!p->numa_faults) in task_weight()
1777 total_faults = p->total_numa_faults; in task_weight()
1797 total_faults = ng->total_faults; in group_weight()
1833 pgdat->node_present_pages >> 4); in pgdat_free_space_enough()
1834 for (z = pgdat->nr_zones - 1; z >= 0; z--) { in pgdat_free_space_enough()
1835 struct zone *zone = pgdat->node_zones + z; in pgdat_free_space_enough()
1855 * hint page fault latency = hint page fault time - scan time
1867 return (time - last_time) & PAGE_ACCESS_TIME_MASK; in numa_hint_fault_latency()
1884 start = pgdat->nbp_rl_start; in numa_promotion_rate_limit()
1885 if (now - start > MSEC_PER_SEC && in numa_promotion_rate_limit()
1886 cmpxchg(&pgdat->nbp_rl_start, start, now) == start) in numa_promotion_rate_limit()
1887 pgdat->nbp_rl_nr_cand = nr_cand; in numa_promotion_rate_limit()
1888 if (nr_cand - pgdat->nbp_rl_nr_cand >= rate_limit) in numa_promotion_rate_limit()
1904 start = pgdat->nbp_th_start; in numa_promotion_adjust_threshold()
1905 if (now - start > th_period && in numa_promotion_adjust_threshold()
1906 cmpxchg(&pgdat->nbp_th_start, start, now) == start) { in numa_promotion_adjust_threshold()
1910 diff_cand = nr_cand - pgdat->nbp_th_nr_cand; in numa_promotion_adjust_threshold()
1912 th = pgdat->nbp_threshold ? : ref_th; in numa_promotion_adjust_threshold()
1914 th = max(th - unit_th, unit_th); in numa_promotion_adjust_threshold()
1917 pgdat->nbp_th_nr_cand = nr_cand; in numa_promotion_adjust_threshold()
1918 pgdat->nbp_threshold = th; in numa_promotion_adjust_threshold()
1947 pgdat->nbp_threshold = 0; in should_numa_migrate_memory()
1953 (20 - PAGE_SHIFT); in should_numa_migrate_memory()
1956 th = pgdat->nbp_threshold ? : def_th; in should_numa_migrate_memory()
1965 this_cpupid = cpu_pid_to_cpupid(dst_cpu, current->pid); in should_numa_migrate_memory()
1975 * two full passes of the "multi-stage node selection" test that is in should_numa_migrate_memory()
1978 if ((p->numa_preferred_nid == NUMA_NO_NODE || p->numa_scan_seq <= 4) && in should_numa_migrate_memory()
1983 * Multi-stage node selection is used in conjunction with a periodic in should_numa_migrate_memory()
1984 * migration fault to build a temporal task<->page relation. By using in should_numa_migrate_memory()
1985 * a two-stage filter we remove short/unlikely relations. in should_numa_migrate_memory()
1989 * page (n_t) (in a given time-span) to a probability. in should_numa_migrate_memory()
1997 * act on an unlikely task<->page relation. in should_numa_migrate_memory()
2007 /* A shared fault, but p->numa_group has not been set up yet. */ in should_numa_migrate_memory()
2024 * --------------- * - > --------------- in should_numa_migrate_memory()
2084 struct numa_stats *ns) in numa_classify() argument
2086 if ((ns->nr_running > ns->weight) && in numa_classify()
2087 (((ns->compute_capacity * 100) < (ns->util * imbalance_pct)) || in numa_classify()
2088 ((ns->compute_capacity * imbalance_pct) < (ns->runnable * 100)))) in numa_classify()
2091 if ((ns->nr_running < ns->weight) || in numa_classify()
2092 (((ns->compute_capacity * 100) > (ns->util * imbalance_pct)) && in numa_classify()
2093 ((ns->compute_capacity * imbalance_pct) > (ns->runnable * 100)))) in numa_classify()
2131 struct numa_stats *ns, int nid, in update_numa_stats() argument
2134 int cpu, idle_core = -1; in update_numa_stats()
2136 memset(ns, 0, sizeof(*ns)); in update_numa_stats()
2137 ns->idle_cpu = -1; in update_numa_stats()
2143 ns->load += cpu_load(rq); in update_numa_stats()
2144 ns->runnable += cpu_runnable(rq); in update_numa_stats()
2145 ns->util += cpu_util_cfs(cpu); in update_numa_stats()
2146 ns->nr_running += rq->cfs.h_nr_runnable; in update_numa_stats()
2147 ns->compute_capacity += capacity_of(cpu); in update_numa_stats()
2149 if (find_idle && idle_core < 0 && !rq->nr_running && idle_cpu(cpu)) { in update_numa_stats()
2150 if (READ_ONCE(rq->numa_migrate_on) || in update_numa_stats()
2151 !cpumask_test_cpu(cpu, env->p->cpus_ptr)) in update_numa_stats()
2154 if (ns->idle_cpu == -1) in update_numa_stats()
2155 ns->idle_cpu = cpu; in update_numa_stats()
2162 ns->weight = cpumask_weight(cpumask_of_node(nid)); in update_numa_stats()
2164 ns->node_type = numa_classify(env->imbalance_pct, ns); in update_numa_stats()
2167 ns->idle_cpu = idle_core; in update_numa_stats()
2173 struct rq *rq = cpu_rq(env->dst_cpu); in task_numa_assign()
2175 /* Check if run-queue part of active NUMA balance. */ in task_numa_assign()
2176 if (env->best_cpu != env->dst_cpu && xchg(&rq->numa_migrate_on, 1)) { in task_numa_assign()
2178 int start = env->dst_cpu; in task_numa_assign()
2181 for_each_cpu_wrap(cpu, cpumask_of_node(env->dst_nid), start + 1) { in task_numa_assign()
2182 if (cpu == env->best_cpu || !idle_cpu(cpu) || in task_numa_assign()
2183 !cpumask_test_cpu(cpu, env->p->cpus_ptr)) { in task_numa_assign()
2187 env->dst_cpu = cpu; in task_numa_assign()
2188 rq = cpu_rq(env->dst_cpu); in task_numa_assign()
2189 if (!xchg(&rq->numa_migrate_on, 1)) in task_numa_assign()
2199 * Clear previous best_cpu/rq numa-migrate flag, since task now in task_numa_assign()
2202 if (env->best_cpu != -1 && env->best_cpu != env->dst_cpu) { in task_numa_assign()
2203 rq = cpu_rq(env->best_cpu); in task_numa_assign()
2204 WRITE_ONCE(rq->numa_migrate_on, 0); in task_numa_assign()
2207 if (env->best_task) in task_numa_assign()
2208 put_task_struct(env->best_task); in task_numa_assign()
2212 env->best_task = p; in task_numa_assign()
2213 env->best_imp = imp; in task_numa_assign()
2214 env->best_cpu = env->dst_cpu; in task_numa_assign()
2228 * ------------ vs --------- in load_too_imbalanced()
2231 src_capacity = env->src_stats.compute_capacity; in load_too_imbalanced()
2232 dst_capacity = env->dst_stats.compute_capacity; in load_too_imbalanced()
2234 imb = abs(dst_load * src_capacity - src_load * dst_capacity); in load_too_imbalanced()
2236 orig_src_load = env->src_stats.load; in load_too_imbalanced()
2237 orig_dst_load = env->dst_stats.load; in load_too_imbalanced()
2239 old_imb = abs(orig_dst_load * src_capacity - orig_src_load * dst_capacity); in load_too_imbalanced()
2261 struct numa_group *cur_ng, *p_ng = deref_curr_numa_group(env->p); in task_numa_compare()
2262 struct rq *dst_rq = cpu_rq(env->dst_cpu); in task_numa_compare()
2266 int dist = env->dist; in task_numa_compare()
2271 if (READ_ONCE(dst_rq->numa_migrate_on)) in task_numa_compare()
2275 cur = rcu_dereference(dst_rq->curr); in task_numa_compare()
2276 if (cur && ((cur->flags & PF_EXITING) || is_idle_task(cur))) in task_numa_compare()
2281 * end try selecting ourselves (current == env->p) as a swap candidate. in task_numa_compare()
2283 if (cur == env->p) { in task_numa_compare()
2289 if (maymove && moveimp >= env->best_imp) in task_numa_compare()
2296 if (!cpumask_test_cpu(env->src_cpu, cur->cpus_ptr)) in task_numa_compare()
2303 if (env->best_task && in task_numa_compare()
2304 env->best_task->numa_preferred_nid == env->src_nid && in task_numa_compare()
2305 cur->numa_preferred_nid != env->src_nid) { in task_numa_compare()
2319 cur_ng = rcu_dereference(cur->numa_group); in task_numa_compare()
2327 if (env->dst_stats.node_type == node_has_spare) in task_numa_compare()
2330 imp = taskimp + task_weight(cur, env->src_nid, dist) - in task_numa_compare()
2331 task_weight(cur, env->dst_nid, dist); in task_numa_compare()
2337 imp -= imp / 16; in task_numa_compare()
2344 imp += group_weight(cur, env->src_nid, dist) - in task_numa_compare()
2345 group_weight(cur, env->dst_nid, dist); in task_numa_compare()
2347 imp += task_weight(cur, env->src_nid, dist) - in task_numa_compare()
2348 task_weight(cur, env->dst_nid, dist); in task_numa_compare()
2352 if (cur->numa_preferred_nid == env->dst_nid) in task_numa_compare()
2353 imp -= imp / 16; in task_numa_compare()
2361 if (cur->numa_preferred_nid == env->src_nid) in task_numa_compare()
2364 if (maymove && moveimp > imp && moveimp > env->best_imp) { in task_numa_compare()
2374 if (env->best_task && cur->numa_preferred_nid == env->src_nid && in task_numa_compare()
2375 env->best_task->numa_preferred_nid != env->src_nid) { in task_numa_compare()
2385 if (imp < SMALLIMP || imp <= env->best_imp + SMALLIMP / 2) in task_numa_compare()
2391 load = task_h_load(env->p) - task_h_load(cur); in task_numa_compare()
2395 dst_load = env->dst_stats.load + load; in task_numa_compare()
2396 src_load = env->src_stats.load - load; in task_numa_compare()
2404 int cpu = env->dst_stats.idle_cpu; in task_numa_compare()
2408 cpu = env->dst_cpu; in task_numa_compare()
2414 if (!idle_cpu(cpu) && env->best_cpu >= 0 && in task_numa_compare()
2415 idle_cpu(env->best_cpu)) { in task_numa_compare()
2416 cpu = env->best_cpu; in task_numa_compare()
2419 env->dst_cpu = cpu; in task_numa_compare()
2429 if (maymove && !cur && env->best_cpu >= 0 && idle_cpu(env->best_cpu)) in task_numa_compare()
2436 if (!maymove && env->best_task && in task_numa_compare()
2437 env->best_task->numa_preferred_nid == env->src_nid) { in task_numa_compare()
2456 if (env->dst_stats.node_type == node_has_spare) { in task_numa_find_cpu()
2466 src_running = env->src_stats.nr_running - 1; in task_numa_find_cpu()
2467 dst_running = env->dst_stats.nr_running + 1; in task_numa_find_cpu()
2468 imbalance = max(0, dst_running - src_running); in task_numa_find_cpu()
2470 env->imb_numa_nr); in task_numa_find_cpu()
2475 if (env->dst_stats.idle_cpu >= 0) { in task_numa_find_cpu()
2476 env->dst_cpu = env->dst_stats.idle_cpu; in task_numa_find_cpu()
2484 * If the improvement from just moving env->p direction is better in task_numa_find_cpu()
2487 load = task_h_load(env->p); in task_numa_find_cpu()
2488 dst_load = env->dst_stats.load + load; in task_numa_find_cpu()
2489 src_load = env->src_stats.load - load; in task_numa_find_cpu()
2493 for_each_cpu(cpu, cpumask_of_node(env->dst_nid)) { in task_numa_find_cpu()
2495 if (!cpumask_test_cpu(cpu, env->p->cpus_ptr)) in task_numa_find_cpu()
2498 env->dst_cpu = cpu; in task_numa_find_cpu()
2516 .best_cpu = -1, in task_numa_migrate()
2530 * random movement of tasks -- counter the numa conditions we're trying in task_numa_migrate()
2536 env.imbalance_pct = 100 + (sd->imbalance_pct - 100) / 2; in task_numa_migrate()
2537 env.imb_numa_nr = sd->imb_numa_nr; in task_numa_migrate()
2549 return -EINVAL; in task_numa_migrate()
2552 env.dst_nid = p->numa_preferred_nid; in task_numa_migrate()
2557 taskimp = task_weight(p, env.dst_nid, dist) - taskweight; in task_numa_migrate()
2558 groupimp = group_weight(p, env.dst_nid, dist) - groupweight; in task_numa_migrate()
2566 * - there is no space available on the preferred_nid in task_numa_migrate()
2567 * - the task is part of a numa_group that is interleaved across in task_numa_migrate()
2572 if (env.best_cpu == -1 || (ng && ng->active_nodes > 1)) { in task_numa_migrate()
2574 if (nid == env.src_nid || nid == p->numa_preferred_nid) in task_numa_migrate()
2585 taskimp = task_weight(p, nid, dist) - taskweight; in task_numa_migrate()
2586 groupimp = group_weight(p, nid, dist) - groupweight; in task_numa_migrate()
2606 if (env.best_cpu == -1) in task_numa_migrate()
2611 if (nid != p->numa_preferred_nid) in task_numa_migrate()
2616 if (env.best_cpu == -1) { in task_numa_migrate()
2617 trace_sched_stick_numa(p, env.src_cpu, NULL, -1); in task_numa_migrate()
2618 return -EAGAIN; in task_numa_migrate()
2624 WRITE_ONCE(best_rq->numa_migrate_on, 0); in task_numa_migrate()
2631 WRITE_ONCE(best_rq->numa_migrate_on, 0); in task_numa_migrate()
2645 if (unlikely(p->numa_preferred_nid == NUMA_NO_NODE || !p->numa_faults)) in numa_migrate_preferred()
2649 interval = min(interval, msecs_to_jiffies(p->numa_scan_period) / 16); in numa_migrate_preferred()
2650 p->numa_migrate_retry = jiffies + interval; in numa_migrate_preferred()
2653 if (task_node(p) == p->numa_preferred_nid) in numa_migrate_preferred()
2683 numa_group->max_faults_cpu = max_faults; in numa_group_count_active_nodes()
2684 numa_group->active_nodes = active_nodes; in numa_group_count_active_nodes()
2710 unsigned long remote = p->numa_faults_locality[0]; in update_task_scan_period()
2711 unsigned long local = p->numa_faults_locality[1]; in update_task_scan_period()
2720 if (local + shared == 0 || p->numa_faults_locality[2]) { in update_task_scan_period()
2721 p->numa_scan_period = min(p->numa_scan_period_max, in update_task_scan_period()
2722 p->numa_scan_period << 1); in update_task_scan_period()
2724 p->mm->numa_next_scan = jiffies + in update_task_scan_period()
2725 msecs_to_jiffies(p->numa_scan_period); in update_task_scan_period()
2736 period_slot = DIV_ROUND_UP(p->numa_scan_period, NUMA_PERIOD_SLOTS); in update_task_scan_period()
2745 int slot = ps_ratio - NUMA_PERIOD_THRESHOLD; in update_task_scan_period()
2755 int slot = lr_ratio - NUMA_PERIOD_THRESHOLD; in update_task_scan_period()
2761 * Private memory faults exceed (SLOTS-THRESHOLD)/SLOTS, in update_task_scan_period()
2766 diff = -(NUMA_PERIOD_THRESHOLD - ratio) * period_slot; in update_task_scan_period()
2769 p->numa_scan_period = clamp(p->numa_scan_period + diff, in update_task_scan_period()
2771 memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); in update_task_scan_period()
2778 * from the dozens-of-seconds NUMA balancing period. Use the scheduler
2785 now = p->se.exec_start; in numa_get_avg_runtime()
2786 runtime = p->se.sum_exec_runtime; in numa_get_avg_runtime()
2788 if (p->last_task_numa_placement) { in numa_get_avg_runtime()
2789 delta = runtime - p->last_sum_exec_runtime; in numa_get_avg_runtime()
2790 *period = now - p->last_task_numa_placement; in numa_get_avg_runtime()
2796 delta = p->se.avg.load_sum; in numa_get_avg_runtime()
2800 p->last_sum_exec_runtime = runtime; in numa_get_avg_runtime()
2801 p->last_task_numa_placement = now; in numa_get_avg_runtime()
2851 for (dist = sched_max_numa_distance; dist > LOCAL_DISTANCE; dist--) { in preferred_group_nid()
2905 * The p->mm->numa_scan_seq field gets updated without in task_numa_placement()
2909 seq = READ_ONCE(p->mm->numa_scan_seq); in task_numa_placement()
2910 if (p->numa_scan_seq == seq) in task_numa_placement()
2912 p->numa_scan_seq = seq; in task_numa_placement()
2913 p->numa_scan_period_max = task_scan_max(p); in task_numa_placement()
2915 total_faults = p->numa_faults_locality[0] + in task_numa_placement()
2916 p->numa_faults_locality[1]; in task_numa_placement()
2922 group_lock = &ng->lock; in task_numa_placement()
2942 diff = p->numa_faults[membuf_idx] - p->numa_faults[mem_idx] / 2; in task_numa_placement()
2943 fault_types[priv] += p->numa_faults[membuf_idx]; in task_numa_placement()
2944 p->numa_faults[membuf_idx] = 0; in task_numa_placement()
2950 * little over-all impact on throughput, and thus their in task_numa_placement()
2954 f_weight = (f_weight * p->numa_faults[cpubuf_idx]) / in task_numa_placement()
2956 f_diff = f_weight - p->numa_faults[cpu_idx] / 2; in task_numa_placement()
2957 p->numa_faults[cpubuf_idx] = 0; in task_numa_placement()
2959 p->numa_faults[mem_idx] += diff; in task_numa_placement()
2960 p->numa_faults[cpu_idx] += f_diff; in task_numa_placement()
2961 faults += p->numa_faults[mem_idx]; in task_numa_placement()
2962 p->total_numa_faults += diff; in task_numa_placement()
2971 ng->faults[mem_idx] += diff; in task_numa_placement()
2972 ng->faults[cpu_idx] += f_diff; in task_numa_placement()
2973 ng->total_faults += diff; in task_numa_placement()
2974 group_faults += ng->faults[mem_idx]; in task_numa_placement()
2989 /* Cannot migrate task to CPU-less node */ in task_numa_placement()
3000 if (max_nid != p->numa_preferred_nid) in task_numa_placement()
3009 return refcount_inc_not_zero(&grp->refcount); in get_numa_group()
3014 if (refcount_dec_and_test(&grp->refcount)) in put_numa_group()
3036 refcount_set(&grp->refcount, 1); in task_numa_group()
3037 grp->active_nodes = 1; in task_numa_group()
3038 grp->max_faults_cpu = 0; in task_numa_group()
3039 spin_lock_init(&grp->lock); in task_numa_group()
3040 grp->gid = p->pid; in task_numa_group()
3043 grp->faults[i] = p->numa_faults[i]; in task_numa_group()
3045 grp->total_faults = p->total_numa_faults; in task_numa_group()
3047 grp->nr_tasks++; in task_numa_group()
3048 rcu_assign_pointer(p->numa_group, grp); in task_numa_group()
3052 tsk = READ_ONCE(cpu_rq(cpu)->curr); in task_numa_group()
3057 grp = rcu_dereference(tsk->numa_group); in task_numa_group()
3069 if (my_grp->nr_tasks > grp->nr_tasks) in task_numa_group()
3073 * Tie-break on the grp address. in task_numa_group()
3075 if (my_grp->nr_tasks == grp->nr_tasks && my_grp > grp) in task_numa_group()
3079 if (tsk->mm == current->mm) in task_numa_group()
3098 double_lock_irq(&my_grp->lock, &grp->lock); in task_numa_group()
3101 my_grp->faults[i] -= p->numa_faults[i]; in task_numa_group()
3102 grp->faults[i] += p->numa_faults[i]; in task_numa_group()
3104 my_grp->total_faults -= p->total_numa_faults; in task_numa_group()
3105 grp->total_faults += p->total_numa_faults; in task_numa_group()
3107 my_grp->nr_tasks--; in task_numa_group()
3108 grp->nr_tasks++; in task_numa_group()
3110 spin_unlock(&my_grp->lock); in task_numa_group()
3111 spin_unlock_irq(&grp->lock); in task_numa_group()
3113 rcu_assign_pointer(p->numa_group, grp); in task_numa_group()
3128 * reset the data back to default state without freeing ->numa_faults.
3133 struct numa_group *grp = rcu_dereference_raw(p->numa_group); in task_numa_free()
3134 unsigned long *numa_faults = p->numa_faults; in task_numa_free()
3142 spin_lock_irqsave(&grp->lock, flags); in task_numa_free()
3144 grp->faults[i] -= p->numa_faults[i]; in task_numa_free()
3145 grp->total_faults -= p->total_numa_faults; in task_numa_free()
3147 grp->nr_tasks--; in task_numa_free()
3148 spin_unlock_irqrestore(&grp->lock, flags); in task_numa_free()
3149 RCU_INIT_POINTER(p->numa_group, NULL); in task_numa_free()
3154 p->numa_faults = NULL; in task_numa_free()
3157 p->total_numa_faults = 0; in task_numa_free()
3179 if (!p->mm) in task_numa_fault()
3191 /* Allocate buffer to track faults on a per-node basis */ in task_numa_fault()
3192 if (unlikely(!p->numa_faults)) { in task_numa_fault()
3193 int size = sizeof(*p->numa_faults) * in task_numa_fault()
3196 p->numa_faults = kzalloc(size, GFP_KERNEL|__GFP_NOWARN); in task_numa_fault()
3197 if (!p->numa_faults) in task_numa_fault()
3200 p->total_numa_faults = 0; in task_numa_fault()
3201 memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); in task_numa_fault()
3208 if (unlikely(last_cpupid == (-1 & LAST_CPUPID_MASK))) { in task_numa_fault()
3223 if (!priv && !local && ng && ng->active_nodes > 1 && in task_numa_fault()
3232 if (time_after(jiffies, p->numa_migrate_retry)) { in task_numa_fault()
3238 p->numa_pages_migrated += pages; in task_numa_fault()
3240 p->numa_faults_locality[2] += pages; in task_numa_fault()
3242 p->numa_faults[task_faults_idx(NUMA_MEMBUF, mem_node, priv)] += pages; in task_numa_fault()
3243 p->numa_faults[task_faults_idx(NUMA_CPUBUF, cpu_node, priv)] += pages; in task_numa_fault()
3244 p->numa_faults_locality[local] += pages; in task_numa_fault()
3251 * p->mm->numa_scan_seq is written to without exclusive access in reset_ptenuma_scan()
3257 WRITE_ONCE(p->mm->numa_scan_seq, READ_ONCE(p->mm->numa_scan_seq) + 1); in reset_ptenuma_scan()
3258 p->mm->numa_scan_offset = 0; in reset_ptenuma_scan()
3270 if ((READ_ONCE(current->mm->numa_scan_seq) - vma->numab_state->start_scan_seq) < 2) in vma_is_accessed()
3273 pids = vma->numab_state->pids_active[0] | vma->numab_state->pids_active[1]; in vma_is_accessed()
3274 if (test_bit(hash_32(current->pid, ilog2(BITS_PER_LONG)), &pids)) in vma_is_accessed()
3279 * some VMAs may never be scanned in multi-threaded applications: in vma_is_accessed()
3281 if (mm->numa_scan_offset > vma->vm_start) { in vma_is_accessed()
3291 if (READ_ONCE(mm->numa_scan_seq) > in vma_is_accessed()
3292 (vma->numab_state->prev_scan_seq + get_nr_threads(current))) in vma_is_accessed()
3308 struct mm_struct *mm = p->mm; in task_numa_work()
3309 u64 runtime = p->se.sum_exec_runtime; in task_numa_work()
3320 work->next = work; in task_numa_work()
3324 * NOTE: make sure not to dereference p->mm before this check, in task_numa_work()
3326 * without p->mm even though we still had it when we enqueued this in task_numa_work()
3329 if (p->flags & PF_EXITING) in task_numa_work()
3332 if (!mm->numa_next_scan) { in task_numa_work()
3333 mm->numa_next_scan = now + in task_numa_work()
3340 migrate = mm->numa_next_scan; in task_numa_work()
3344 if (p->numa_scan_period == 0) { in task_numa_work()
3345 p->numa_scan_period_max = task_scan_max(p); in task_numa_work()
3346 p->numa_scan_period = task_scan_start(p); in task_numa_work()
3349 next_scan = now + msecs_to_jiffies(p->numa_scan_period); in task_numa_work()
3350 if (!try_cmpxchg(&mm->numa_next_scan, &migrate, next_scan)) in task_numa_work()
3357 p->node_stamp += 2 * TICK_NSEC; in task_numa_work()
3360 pages <<= 20 - PAGE_SHIFT; /* MB in pages */ in task_numa_work()
3377 start = mm->numa_scan_offset; in task_numa_work()
3389 is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_MIXEDMAP)) { in task_numa_work()
3397 * hinting faults in read-only file-backed mappings or the vDSO in task_numa_work()
3400 if (!vma->vm_mm || in task_numa_work()
3401 (vma->vm_file && (vma->vm_flags & (VM_READ|VM_WRITE)) == (VM_READ))) { in task_numa_work()
3415 /* Initialise new per-VMA NUMAB state. */ in task_numa_work()
3416 if (!vma->numab_state) { in task_numa_work()
3423 if (cmpxchg(&vma->numab_state, NULL, ptr)) { in task_numa_work()
3428 vma->numab_state->start_scan_seq = mm->numa_scan_seq; in task_numa_work()
3430 vma->numab_state->next_scan = now + in task_numa_work()
3434 vma->numab_state->pids_active_reset = vma->numab_state->next_scan + in task_numa_work()
3442 vma->numab_state->prev_scan_seq = mm->numa_scan_seq - 1; in task_numa_work()
3449 if (mm->numa_scan_seq && time_before(jiffies, in task_numa_work()
3450 vma->numab_state->next_scan)) { in task_numa_work()
3456 if (mm->numa_scan_seq && in task_numa_work()
3457 time_after(jiffies, vma->numab_state->pids_active_reset)) { in task_numa_work()
3458 vma->numab_state->pids_active_reset = vma->numab_state->pids_active_reset + in task_numa_work()
3460 vma->numab_state->pids_active[0] = READ_ONCE(vma->numab_state->pids_active[1]); in task_numa_work()
3461 vma->numab_state->pids_active[1] = 0; in task_numa_work()
3465 if (vma->numab_state->prev_scan_seq == mm->numa_scan_seq) { in task_numa_work()
3466 mm->numa_scan_offset = vma->vm_end; in task_numa_work()
3482 start = max(start, vma->vm_start); in task_numa_work()
3484 end = min(end, vma->vm_end); in task_numa_work()
3490 * is not already PTE-numa. If the VMA contains in task_numa_work()
3496 pages -= (end - start) >> PAGE_SHIFT; in task_numa_work()
3497 virtpages -= (end - start) >> PAGE_SHIFT; in task_numa_work()
3504 } while (end != vma->vm_end); in task_numa_work()
3507 vma->numab_state->prev_scan_seq = mm->numa_scan_seq; in task_numa_work()
3535 mm->numa_scan_offset = start; in task_numa_work()
3546 if (unlikely(p->se.sum_exec_runtime != runtime)) { in task_numa_work()
3547 u64 diff = p->se.sum_exec_runtime - runtime; in task_numa_work()
3548 p->node_stamp += 32 * diff; in task_numa_work()
3555 struct mm_struct *mm = p->mm; in init_numa_balancing()
3558 mm_users = atomic_read(&mm->mm_users); in init_numa_balancing()
3560 mm->numa_next_scan = jiffies + msecs_to_jiffies(sysctl_numa_balancing_scan_delay); in init_numa_balancing()
3561 mm->numa_scan_seq = 0; in init_numa_balancing()
3564 p->node_stamp = 0; in init_numa_balancing()
3565 p->numa_scan_seq = mm ? mm->numa_scan_seq : 0; in init_numa_balancing()
3566 p->numa_scan_period = sysctl_numa_balancing_scan_delay; in init_numa_balancing()
3567 p->numa_migrate_retry = 0; in init_numa_balancing()
3569 p->numa_work.next = &p->numa_work; in init_numa_balancing()
3570 p->numa_faults = NULL; in init_numa_balancing()
3571 p->numa_pages_migrated = 0; in init_numa_balancing()
3572 p->total_numa_faults = 0; in init_numa_balancing()
3573 RCU_INIT_POINTER(p->numa_group, NULL); in init_numa_balancing()
3574 p->last_task_numa_placement = 0; in init_numa_balancing()
3575 p->last_sum_exec_runtime = 0; in init_numa_balancing()
3577 init_task_work(&p->numa_work, task_numa_work); in init_numa_balancing()
3581 p->numa_preferred_nid = NUMA_NO_NODE; in init_numa_balancing()
3593 current->numa_scan_period * mm_users * NSEC_PER_MSEC); in init_numa_balancing()
3595 p->node_stamp = delay; in init_numa_balancing()
3604 struct callback_head *work = &curr->numa_work; in task_tick_numa()
3610 if (!curr->mm || (curr->flags & (PF_EXITING | PF_KTHREAD)) || work->next != work) in task_tick_numa()
3619 now = curr->se.sum_exec_runtime; in task_tick_numa()
3620 period = (u64)curr->numa_scan_period * NSEC_PER_MSEC; in task_tick_numa()
3622 if (now > curr->node_stamp + period) { in task_tick_numa()
3623 if (!curr->node_stamp) in task_tick_numa()
3624 curr->numa_scan_period = task_scan_start(curr); in task_tick_numa()
3625 curr->node_stamp += period; in task_tick_numa()
3627 if (!time_before(jiffies, curr->mm->numa_next_scan)) in task_tick_numa()
3640 if (!p->mm || !p->numa_faults || (p->flags & PF_EXITING)) in update_scan_period()
3649 * is pulled cross-node due to wakeups or load balancing. in update_scan_period()
3651 if (p->numa_scan_seq) { in update_scan_period()
3657 if (dst_nid == p->numa_preferred_nid || in update_scan_period()
3658 (p->numa_preferred_nid != NUMA_NO_NODE && in update_scan_period()
3659 src_nid != p->numa_preferred_nid)) in update_scan_period()
3663 p->numa_scan_period = task_scan_start(p); in update_scan_period()
3688 update_load_add(&cfs_rq->load, se->load.weight); in account_entity_enqueue()
3694 list_add(&se->group_node, &rq->cfs_tasks); in account_entity_enqueue()
3697 cfs_rq->nr_queued++; in account_entity_enqueue()
3703 update_load_sub(&cfs_rq->load, se->load.weight); in account_entity_dequeue()
3707 list_del_init(&se->group_node); in account_entity_dequeue()
3710 cfs_rq->nr_queued--; in account_entity_dequeue()
3716 * Explicitly do a load-store to ensure the intermediate value never hits
3736 * Explicitly do a load-store to ensure the intermediate value never hits
3744 res = var - val; \
3753 * A variant of sub_positive(), which does not use explicit load-store
3758 *ptr -= min_t(typeof(*ptr), *ptr, _val); \
3765 cfs_rq->avg.load_avg += se->avg.load_avg; in enqueue_load_avg()
3766 cfs_rq->avg.load_sum += se_weight(se) * se->avg.load_sum; in enqueue_load_avg()
3772 sub_positive(&cfs_rq->avg.load_avg, se->avg.load_avg); in dequeue_load_avg()
3773 sub_positive(&cfs_rq->avg.load_sum, se_weight(se) * se->avg.load_sum); in dequeue_load_avg()
3775 cfs_rq->avg.load_sum = max_t(u32, cfs_rq->avg.load_sum, in dequeue_load_avg()
3776 cfs_rq->avg.load_avg * PELT_MIN_DIVIDER); in dequeue_load_avg()
3790 bool curr = cfs_rq->curr == se; in reweight_entity()
3792 if (se->on_rq) { in reweight_entity()
3796 se->deadline -= se->vruntime; in reweight_entity()
3797 se->rel_deadline = 1; in reweight_entity()
3800 update_load_sub(&cfs_rq->load, se->load.weight); in reweight_entity()
3805 * Because we keep se->vlag = V - v_i, while: lag_i = w_i*(V - v_i), in reweight_entity()
3806 * we need to scale se->vlag when w_i changes. in reweight_entity()
3808 se->vlag = div_s64(se->vlag * se->load.weight, weight); in reweight_entity()
3809 if (se->rel_deadline) in reweight_entity()
3810 se->deadline = div_s64(se->deadline * se->load.weight, weight); in reweight_entity()
3812 update_load_set(&se->load, weight); in reweight_entity()
3816 u32 divider = get_pelt_divider(&se->avg); in reweight_entity()
3818 se->avg.load_avg = div_u64(se_weight(se) * se->avg.load_sum, divider); in reweight_entity()
3823 if (se->on_rq) { in reweight_entity()
3824 update_load_add(&cfs_rq->load, se->load.weight); in reweight_entity()
3831 * whether the rq-wide min_vruntime needs updated too. Since in reweight_entity()
3833 * than up-to-date one, we do the update at the end of the in reweight_entity()
3843 struct sched_entity *se = &p->se; in reweight_task_fair()
3845 struct load_weight *load = &se->load; in reweight_task_fair()
3847 reweight_entity(cfs_rq, se, lw->weight); in reweight_task_fair()
3848 load->inv_weight = lw->inv_weight; in reweight_task_fair()
3862 * tg->weight * grq->load.weight
3863 * ge->load.weight = ----------------------------- (1)
3864 * \Sum grq->load.weight
3872 * grq->load.weight -> grq->avg.load_avg (2)
3876 * tg->weight * grq->avg.load_avg
3877 * ge->load.weight = ------------------------------ (3)
3878 * tg->load_avg
3880 * Where: tg->load_avg ~= \Sum grq->avg.load_avg
3884 * The problem with it is that because the average is slow -- it was designed
3885 * to be exactly that of course -- this leads to transients in boundary
3887 * one task. It takes time for our CPU's grq->avg.load_avg to build up,
3892 * tg->weight * grq->load.weight
3893 * ge->load.weight = ----------------------------- = tg->weight (4)
3894 * grp->load.weight
3901 * ge->load.weight =
3903 * tg->weight * grq->load.weight
3904 * --------------------------------------------------- (5)
3905 * tg->load_avg - grq->avg.load_avg + grq->load.weight
3907 * But because grq->load.weight can drop to 0, resulting in a divide by zero,
3908 * we need to use grq->avg.load_avg as its lower bound, which then gives:
3911 * tg->weight * grq->load.weight
3912 * ge->load.weight = ----------------------------- (6)
3917 * tg_load_avg' = tg->load_avg - grq->avg.load_avg +
3918 * max(grq->load.weight, grq->avg.load_avg)
3922 * overestimates the ge->load.weight and therefore:
3924 * \Sum ge->load.weight >= tg->weight
3931 struct task_group *tg = cfs_rq->tg; in calc_group_shares()
3933 tg_shares = READ_ONCE(tg->shares); in calc_group_shares()
3935 load = max(scale_load_down(cfs_rq->load.weight), cfs_rq->avg.load_avg); in calc_group_shares()
3937 tg_weight = atomic_long_read(&tg->load_avg); in calc_group_shares()
3940 tg_weight -= cfs_rq->tg_load_avg_contrib; in calc_group_shares()
3948 * MIN_SHARES has to be unscaled here to support per-CPU partitioning in calc_group_shares()
3949 * of a group with small tg->shares value. It is a floor value which is in calc_group_shares()
3953 * E.g. on 64-bit for a group with tg->shares of scale_load(15)=15*1024 in calc_group_shares()
3954 * on an 8-core system with 8 tasks each runnable on one CPU shares has in calc_group_shares()
3976 if (!gcfs_rq || !gcfs_rq->load.weight) in update_cfs_group()
3983 shares = READ_ONCE(gcfs_rq->tg->shares); in update_cfs_group()
3987 if (unlikely(se->load.weight != shares)) in update_cfs_group()
4001 if (&rq->cfs == cfs_rq) { in cfs_rq_util_change()
4011 * As is, the util number is not freq-invariant (we'd have to in cfs_rq_util_change()
4023 if (sa->load_sum) in load_avg_is_decayed()
4026 if (sa->util_sum) in load_avg_is_decayed()
4029 if (sa->runnable_sum) in load_avg_is_decayed()
4037 WARN_ON_ONCE(sa->load_avg || in load_avg_is_decayed()
4038 sa->util_avg || in load_avg_is_decayed()
4039 sa->runnable_avg); in load_avg_is_decayed()
4046 return u64_u32_load_copy(cfs_rq->avg.last_update_time, in cfs_rq_last_update_time()
4047 cfs_rq->last_update_time_copy); in cfs_rq_last_update_time()
4053 * bottom-up, we only have to test whether the cfs_rq before us on the list
4064 if (cfs_rq->on_list) { in child_cfs_rq_on_list()
4065 prev = cfs_rq->leaf_cfs_rq_list.prev; in child_cfs_rq_on_list()
4067 prev = rq->tmp_alone_branch; in child_cfs_rq_on_list()
4070 if (prev == &rq->leaf_cfs_rq_list) in child_cfs_rq_on_list()
4075 return (prev_cfs_rq->tg->parent == cfs_rq->tg); in child_cfs_rq_on_list()
4080 if (cfs_rq->load.weight) in cfs_rq_is_decayed()
4083 if (!load_avg_is_decayed(&cfs_rq->avg)) in cfs_rq_is_decayed()
4093 * update_tg_load_avg - update the tg's load avg
4096 * This function 'ensures': tg->load_avg := \Sum tg->cfs_rq[]->avg.load.
4097 * However, because tg->load_avg is a global value there are performance
4114 if (cfs_rq->tg == &root_task_group) in update_tg_load_avg()
4122 * For migration heavy workloads, access to tg->load_avg can be in update_tg_load_avg()
4126 if (now - cfs_rq->last_update_tg_load_avg < NSEC_PER_MSEC) in update_tg_load_avg()
4129 delta = cfs_rq->avg.load_avg - cfs_rq->tg_load_avg_contrib; in update_tg_load_avg()
4130 if (abs(delta) > cfs_rq->tg_load_avg_contrib / 64) { in update_tg_load_avg()
4131 atomic_long_add(delta, &cfs_rq->tg->load_avg); in update_tg_load_avg()
4132 cfs_rq->tg_load_avg_contrib = cfs_rq->avg.load_avg; in update_tg_load_avg()
4133 cfs_rq->last_update_tg_load_avg = now; in update_tg_load_avg()
4145 if (cfs_rq->tg == &root_task_group) in clear_tg_load_avg()
4149 delta = 0 - cfs_rq->tg_load_avg_contrib; in clear_tg_load_avg()
4150 atomic_long_add(delta, &cfs_rq->tg->load_avg); in clear_tg_load_avg()
4151 cfs_rq->tg_load_avg_contrib = 0; in clear_tg_load_avg()
4152 cfs_rq->last_update_tg_load_avg = now; in clear_tg_load_avg()
4171 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in clear_tg_offline_cfs_rqs()
4182 * caller only guarantees p->pi_lock is held; no other assumptions,
4183 * including the state of rq->lock, should be made.
4197 * getting what current time is, so simply throw away the out-of-date in set_task_rq_fair()
4201 if (!(se->avg.last_update_time && prev)) in set_task_rq_fair()
4208 se->avg.last_update_time = n_last_update_time; in set_task_rq_fair()
4216 * ge->avg == grq->avg (1)
4227 * ge->avg.load_avg = ge->load.weight * ge->avg.runnable_avg (2)
4232 * grq->avg.load_avg = grq->load.weight * grq->avg.runnable_avg (3)
4236 * ge->avg.runnable_avg == grq->avg.runnable_avg
4240 * ge->load.weight * grq->avg.load_avg
4241 * ge->avg.load_avg = ----------------------------------- (4)
4242 * grq->load.weight
4255 * Another reason this doesn't work is that runnable isn't a 0-sum entity.
4266 * ge->avg.running_sum <= ge->avg.runnable_sum <= LOAD_AVG_MAX
4273 * grq->avg.runnable_sum = grq->avg.load_sum / grq->load.weight
4281 long delta_sum, delta_avg = gcfs_rq->avg.util_avg - se->avg.util_avg; in update_tg_cfs_util()
4289 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in update_tg_cfs_util()
4292 divider = get_pelt_divider(&cfs_rq->avg); in update_tg_cfs_util()
4296 se->avg.util_avg = gcfs_rq->avg.util_avg; in update_tg_cfs_util()
4297 new_sum = se->avg.util_avg * divider; in update_tg_cfs_util()
4298 delta_sum = (long)new_sum - (long)se->avg.util_sum; in update_tg_cfs_util()
4299 se->avg.util_sum = new_sum; in update_tg_cfs_util()
4302 add_positive(&cfs_rq->avg.util_avg, delta_avg); in update_tg_cfs_util()
4303 add_positive(&cfs_rq->avg.util_sum, delta_sum); in update_tg_cfs_util()
4306 cfs_rq->avg.util_sum = max_t(u32, cfs_rq->avg.util_sum, in update_tg_cfs_util()
4307 cfs_rq->avg.util_avg * PELT_MIN_DIVIDER); in update_tg_cfs_util()
4313 long delta_sum, delta_avg = gcfs_rq->avg.runnable_avg - se->avg.runnable_avg; in update_tg_cfs_runnable()
4321 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in update_tg_cfs_runnable()
4324 divider = get_pelt_divider(&cfs_rq->avg); in update_tg_cfs_runnable()
4327 se->avg.runnable_avg = gcfs_rq->avg.runnable_avg; in update_tg_cfs_runnable()
4328 new_sum = se->avg.runnable_avg * divider; in update_tg_cfs_runnable()
4329 delta_sum = (long)new_sum - (long)se->avg.runnable_sum; in update_tg_cfs_runnable()
4330 se->avg.runnable_sum = new_sum; in update_tg_cfs_runnable()
4333 add_positive(&cfs_rq->avg.runnable_avg, delta_avg); in update_tg_cfs_runnable()
4334 add_positive(&cfs_rq->avg.runnable_sum, delta_sum); in update_tg_cfs_runnable()
4336 cfs_rq->avg.runnable_sum = max_t(u32, cfs_rq->avg.runnable_sum, in update_tg_cfs_runnable()
4337 cfs_rq->avg.runnable_avg * PELT_MIN_DIVIDER); in update_tg_cfs_runnable()
4343 long delta_avg, running_sum, runnable_sum = gcfs_rq->prop_runnable_sum; in update_tg_cfs_load()
4352 gcfs_rq->prop_runnable_sum = 0; in update_tg_cfs_load()
4355 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in update_tg_cfs_load()
4358 divider = get_pelt_divider(&cfs_rq->avg); in update_tg_cfs_load()
4365 runnable_sum += se->avg.load_sum; in update_tg_cfs_load()
4372 if (scale_load_down(gcfs_rq->load.weight)) { in update_tg_cfs_load()
4373 load_sum = div_u64(gcfs_rq->avg.load_sum, in update_tg_cfs_load()
4374 scale_load_down(gcfs_rq->load.weight)); in update_tg_cfs_load()
4378 runnable_sum = min(se->avg.load_sum, load_sum); in update_tg_cfs_load()
4387 running_sum = se->avg.util_sum >> SCHED_CAPACITY_SHIFT; in update_tg_cfs_load()
4393 delta_avg = load_avg - se->avg.load_avg; in update_tg_cfs_load()
4397 delta_sum = load_sum - (s64)se_weight(se) * se->avg.load_sum; in update_tg_cfs_load()
4399 se->avg.load_sum = runnable_sum; in update_tg_cfs_load()
4400 se->avg.load_avg = load_avg; in update_tg_cfs_load()
4401 add_positive(&cfs_rq->avg.load_avg, delta_avg); in update_tg_cfs_load()
4402 add_positive(&cfs_rq->avg.load_sum, delta_sum); in update_tg_cfs_load()
4404 cfs_rq->avg.load_sum = max_t(u32, cfs_rq->avg.load_sum, in update_tg_cfs_load()
4405 cfs_rq->avg.load_avg * PELT_MIN_DIVIDER); in update_tg_cfs_load()
4410 cfs_rq->propagate = 1; in add_tg_cfs_propagate()
4411 cfs_rq->prop_runnable_sum += runnable_sum; in add_tg_cfs_propagate()
4423 if (!gcfs_rq->propagate) in propagate_entity_load_avg()
4426 gcfs_rq->propagate = 0; in propagate_entity_load_avg()
4430 add_tg_cfs_propagate(cfs_rq, gcfs_rq->prop_runnable_sum); in propagate_entity_load_avg()
4454 if (se->avg.load_avg || se->avg.util_avg) in skip_blocked_update()
4461 if (gcfs_rq->propagate) in skip_blocked_update()
4495 if (load_avg_is_decayed(&se->avg)) in migrate_se_pelt_lag()
4502 is_idle = is_idle_task(rcu_dereference(rq->curr)); in migrate_se_pelt_lag()
4519 * - cfs->throttled_clock_pelt_time@cfs_rq_idle in migrate_se_pelt_lag()
4522 * = rq_clock_pelt()@rq_idle - rq_clock_pelt()@cfs_rq_idle in migrate_se_pelt_lag()
4525 * = sched_clock_cpu() - rq_clock()@rq_idle in migrate_se_pelt_lag()
4529 * now = rq_clock_pelt()@rq_idle - cfs->throttled_clock_pelt_time + in migrate_se_pelt_lag()
4530 * sched_clock_cpu() - rq_clock()@rq_idle in migrate_se_pelt_lag()
4532 * rq_clock_pelt()@rq_idle is rq->clock_pelt_idle in migrate_se_pelt_lag()
4533 * rq_clock()@rq_idle is rq->clock_idle in migrate_se_pelt_lag()
4534 * cfs->throttled_clock_pelt_time@cfs_rq_idle in migrate_se_pelt_lag()
4535 * is cfs_rq->throttled_pelt_idle in migrate_se_pelt_lag()
4539 throttled = u64_u32_load(cfs_rq->throttled_pelt_idle); in migrate_se_pelt_lag()
4544 now = u64_u32_load(rq->clock_pelt_idle); in migrate_se_pelt_lag()
4554 now -= throttled; in migrate_se_pelt_lag()
4557 * cfs_rq->avg.last_update_time is more recent than our in migrate_se_pelt_lag()
4562 now += sched_clock_cpu(cpu_of(rq)) - u64_u32_load(rq->clock_idle); in migrate_se_pelt_lag()
4571 * update_cfs_rq_load_avg - update the cfs_rq's load/util averages
4578 * cfs_rq->avg is used for task_h_load() and update_cfs_share() for example.
4582 * Since both these conditions indicate a changed cfs_rq->avg.load we should
4589 struct sched_avg *sa = &cfs_rq->avg; in update_cfs_rq_load_avg()
4592 if (cfs_rq->removed.nr) { in update_cfs_rq_load_avg()
4594 u32 divider = get_pelt_divider(&cfs_rq->avg); in update_cfs_rq_load_avg()
4596 raw_spin_lock(&cfs_rq->removed.lock); in update_cfs_rq_load_avg()
4597 swap(cfs_rq->removed.util_avg, removed_util); in update_cfs_rq_load_avg()
4598 swap(cfs_rq->removed.load_avg, removed_load); in update_cfs_rq_load_avg()
4599 swap(cfs_rq->removed.runnable_avg, removed_runnable); in update_cfs_rq_load_avg()
4600 cfs_rq->removed.nr = 0; in update_cfs_rq_load_avg()
4601 raw_spin_unlock(&cfs_rq->removed.lock); in update_cfs_rq_load_avg()
4604 sub_positive(&sa->load_avg, r); in update_cfs_rq_load_avg()
4605 sub_positive(&sa->load_sum, r * divider); in update_cfs_rq_load_avg()
4606 /* See sa->util_sum below */ in update_cfs_rq_load_avg()
4607 sa->load_sum = max_t(u32, sa->load_sum, sa->load_avg * PELT_MIN_DIVIDER); in update_cfs_rq_load_avg()
4610 sub_positive(&sa->util_avg, r); in update_cfs_rq_load_avg()
4611 sub_positive(&sa->util_sum, r * divider); in update_cfs_rq_load_avg()
4613 * Because of rounding, se->util_sum might ends up being +1 more than in update_cfs_rq_load_avg()
4614 * cfs->util_sum. Although this is not a problem by itself, detaching in update_cfs_rq_load_avg()
4616 * util_avg (~1ms) can make cfs->util_sum becoming null whereas in update_cfs_rq_load_avg()
4623 sa->util_sum = max_t(u32, sa->util_sum, sa->util_avg * PELT_MIN_DIVIDER); in update_cfs_rq_load_avg()
4626 sub_positive(&sa->runnable_avg, r); in update_cfs_rq_load_avg()
4627 sub_positive(&sa->runnable_sum, r * divider); in update_cfs_rq_load_avg()
4628 /* See sa->util_sum above */ in update_cfs_rq_load_avg()
4629 sa->runnable_sum = max_t(u32, sa->runnable_sum, in update_cfs_rq_load_avg()
4630 sa->runnable_avg * PELT_MIN_DIVIDER); in update_cfs_rq_load_avg()
4637 -(long)(removed_runnable * divider) >> SCHED_CAPACITY_SHIFT); in update_cfs_rq_load_avg()
4643 u64_u32_store_copy(sa->last_update_time, in update_cfs_rq_load_avg()
4644 cfs_rq->last_update_time_copy, in update_cfs_rq_load_avg()
4645 sa->last_update_time); in update_cfs_rq_load_avg()
4650 * attach_entity_load_avg - attach this entity to its cfs_rq load avg
4655 * cfs_rq->avg.last_update_time being current.
4660 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in attach_entity_load_avg()
4663 u32 divider = get_pelt_divider(&cfs_rq->avg); in attach_entity_load_avg()
4672 se->avg.last_update_time = cfs_rq->avg.last_update_time; in attach_entity_load_avg()
4673 se->avg.period_contrib = cfs_rq->avg.period_contrib; in attach_entity_load_avg()
4681 se->avg.util_sum = se->avg.util_avg * divider; in attach_entity_load_avg()
4683 se->avg.runnable_sum = se->avg.runnable_avg * divider; in attach_entity_load_avg()
4685 se->avg.load_sum = se->avg.load_avg * divider; in attach_entity_load_avg()
4686 if (se_weight(se) < se->avg.load_sum) in attach_entity_load_avg()
4687 se->avg.load_sum = div_u64(se->avg.load_sum, se_weight(se)); in attach_entity_load_avg()
4689 se->avg.load_sum = 1; in attach_entity_load_avg()
4692 cfs_rq->avg.util_avg += se->avg.util_avg; in attach_entity_load_avg()
4693 cfs_rq->avg.util_sum += se->avg.util_sum; in attach_entity_load_avg()
4694 cfs_rq->avg.runnable_avg += se->avg.runnable_avg; in attach_entity_load_avg()
4695 cfs_rq->avg.runnable_sum += se->avg.runnable_sum; in attach_entity_load_avg()
4697 add_tg_cfs_propagate(cfs_rq, se->avg.load_sum); in attach_entity_load_avg()
4705 * detach_entity_load_avg - detach this entity from its cfs_rq load avg
4710 * cfs_rq->avg.last_update_time being current.
4715 sub_positive(&cfs_rq->avg.util_avg, se->avg.util_avg); in detach_entity_load_avg()
4716 sub_positive(&cfs_rq->avg.util_sum, se->avg.util_sum); in detach_entity_load_avg()
4718 cfs_rq->avg.util_sum = max_t(u32, cfs_rq->avg.util_sum, in detach_entity_load_avg()
4719 cfs_rq->avg.util_avg * PELT_MIN_DIVIDER); in detach_entity_load_avg()
4721 sub_positive(&cfs_rq->avg.runnable_avg, se->avg.runnable_avg); in detach_entity_load_avg()
4722 sub_positive(&cfs_rq->avg.runnable_sum, se->avg.runnable_sum); in detach_entity_load_avg()
4724 cfs_rq->avg.runnable_sum = max_t(u32, cfs_rq->avg.runnable_sum, in detach_entity_load_avg()
4725 cfs_rq->avg.runnable_avg * PELT_MIN_DIVIDER); in detach_entity_load_avg()
4727 add_tg_cfs_propagate(cfs_rq, -se->avg.load_sum); in detach_entity_load_avg()
4752 if (se->avg.last_update_time && !(flags & SKIP_AGE_LOAD)) in update_load_avg()
4758 if (!se->avg.last_update_time && (flags & DO_ATTACH)) { in update_load_avg()
4808 * tasks cannot exit without having gone through wake_up_new_task() -> in remove_entity_load_avg()
4815 raw_spin_lock_irqsave(&cfs_rq->removed.lock, flags); in remove_entity_load_avg()
4816 ++cfs_rq->removed.nr; in remove_entity_load_avg()
4817 cfs_rq->removed.util_avg += se->avg.util_avg; in remove_entity_load_avg()
4818 cfs_rq->removed.load_avg += se->avg.load_avg; in remove_entity_load_avg()
4819 cfs_rq->removed.runnable_avg += se->avg.runnable_avg; in remove_entity_load_avg()
4820 raw_spin_unlock_irqrestore(&cfs_rq->removed.lock, flags); in remove_entity_load_avg()
4825 return cfs_rq->avg.runnable_avg; in cfs_rq_runnable_avg()
4830 return cfs_rq->avg.load_avg; in cfs_rq_load_avg()
4837 return READ_ONCE(p->se.avg.util_avg); in task_util()
4842 return READ_ONCE(p->se.avg.runnable_avg); in task_runnable()
4847 return READ_ONCE(p->se.avg.util_est) & ~UTIL_AVG_UNCHANGED; in _task_util_est()
4864 enqueued = cfs_rq->avg.util_est; in util_est_enqueue()
4866 WRITE_ONCE(cfs_rq->avg.util_est, enqueued); in util_est_enqueue()
4880 enqueued = cfs_rq->avg.util_est; in util_est_dequeue()
4881 enqueued -= min_t(unsigned int, enqueued, _task_util_est(p)); in util_est_dequeue()
4882 WRITE_ONCE(cfs_rq->avg.util_est, enqueued); in util_est_dequeue()
4906 ewma = READ_ONCE(p->se.avg.util_est); in util_est_update()
4931 last_ewma_diff = ewma - dequeued; in util_est_update()
4957 * ewma(t) = w * task_util(p) + (1-w) * ewma(t-1) in util_est_update()
4958 * = w * task_util(p) + ewma(t-1) - w * ewma(t-1) in util_est_update()
4959 * = w * (task_util(p) - ewma(t-1)) + ewma(t-1) in util_est_update()
4960 * = w * ( -last_ewma_diff ) + ewma(t-1) in util_est_update()
4961 * = w * (-last_ewma_diff + ewma(t-1) / w) in util_est_update()
4967 ewma -= last_ewma_diff; in util_est_update()
4971 WRITE_ONCE(p->se.avg.util_est, ewma); in util_est_update()
4973 trace_sched_util_est_se_tp(&p->se); in util_est_update()
4980 capacity -= max(hw_load_avg(cpu_rq(cpu)), cpufreq_get_pressure(cpu)); in get_actual_cpu_capacity()
5038 * +---------------------------------------- in util_fits_cpu()
5076 * +---------------------------------------- in util_fits_cpu()
5099 return -1; in util_fits_cpu()
5127 if (!p || (p->nr_cpus_allowed == 1) || in update_misfit_status()
5128 (arch_scale_cpu_capacity(cpu) == p->max_allowed_capacity) || in update_misfit_status()
5131 rq->misfit_task_load = 0; in update_misfit_status()
5139 rq->misfit_task_load = max_t(unsigned long, task_h_load(p), 1); in update_misfit_status()
5146 return !cfs_rq->nr_queued; in cfs_rq_is_decayed()
5186 struct sched_entity *se = &p->se; in __setparam_fair()
5188 p->static_prio = NICE_TO_PRIO(attr->sched_nice); in __setparam_fair()
5189 if (attr->sched_runtime) { in __setparam_fair()
5190 se->custom_slice = 1; in __setparam_fair()
5191 se->slice = clamp_t(u64, attr->sched_runtime, in __setparam_fair()
5195 se->custom_slice = 0; in __setparam_fair()
5196 se->slice = sysctl_sched_base_slice; in __setparam_fair()
5206 if (!se->custom_slice) in place_entity()
5207 se->slice = sysctl_sched_base_slice; in place_entity()
5208 vslice = calc_delta_fair(se->slice, se); in place_entity()
5218 if (sched_feat(PLACE_LAG) && cfs_rq->nr_queued && se->vlag) { in place_entity()
5219 struct sched_entity *curr = cfs_rq->curr; in place_entity()
5222 lag = se->vlag; in place_entity()
5232 * lag_i = S - s_i = w_i * (V - v_i) in place_entity()
5237 * vl_i = V - v_i <=> v_i = V - vl_i in place_entity()
5249 * = (W*V + w_i*(V - vl_i)) / (W + w_i) in place_entity()
5250 * = (W*V + w_i*V - w_i*vl_i) / (W + w_i) in place_entity()
5251 * = (V*(W + w_i) - w_i*l) / (W + w_i) in place_entity()
5252 * = V - w_i*vl_i / (W + w_i) in place_entity()
5256 * vl'_i = V' - v_i in place_entity()
5257 * = V - w_i*vl_i / (W + w_i) - (V - vl_i) in place_entity()
5258 * = vl_i - w_i*vl_i / (W + w_i) in place_entity()
5268 * vl'_i = vl_i - w_i*vl_i / (W + w_i) in place_entity()
5269 * = ((W + w_i)*vl_i - w_i*vl_i) / (W + w_i) in place_entity()
5271 * (W + w_i)*vl'_i = (W + w_i)*vl_i - w_i*vl_i in place_entity()
5276 load = cfs_rq->avg_load; in place_entity()
5277 if (curr && curr->on_rq) in place_entity()
5278 load += scale_load_down(curr->load.weight); in place_entity()
5280 lag *= load + scale_load_down(se->load.weight); in place_entity()
5286 se->vruntime = vruntime - lag; in place_entity()
5288 if (se->rel_deadline) { in place_entity()
5289 se->deadline += se->vruntime; in place_entity()
5290 se->rel_deadline = 0; in place_entity()
5305 se->deadline = se->vruntime + vslice; in place_entity()
5317 bool curr = cfs_rq->curr == se; in enqueue_entity()
5330 * - Update loads to have both entity and cfs_rq synced with now. in enqueue_entity()
5331 * - For group_entity, update its runnable_weight to reflect the new in enqueue_entity()
5333 * - For group_entity, update its weight to reflect the new share of in enqueue_entity()
5335 * - Add its new weight to cfs_rq->load.weight in enqueue_entity()
5341 * but update_cfs_group() here will re-adjust the weight and have to in enqueue_entity()
5347 * XXX now that the entity has been re-weighted, and it's lag adjusted, in enqueue_entity()
5357 se->exec_start = 0; in enqueue_entity()
5363 se->on_rq = 1; in enqueue_entity()
5365 if (cfs_rq->nr_queued == 1) { in enqueue_entity()
5373 if (cfs_rq_throttled(cfs_rq) && !cfs_rq->throttled_clock) in enqueue_entity()
5374 cfs_rq->throttled_clock = rq_clock(rq); in enqueue_entity()
5375 if (!cfs_rq->throttled_clock_self) in enqueue_entity()
5376 cfs_rq->throttled_clock_self = rq_clock(rq); in enqueue_entity()
5386 if (cfs_rq->next != se) in __clear_buddies_next()
5389 cfs_rq->next = NULL; in __clear_buddies_next()
5395 if (cfs_rq->next == se) in clear_buddies()
5403 se->sched_delayed = 1; in set_delayed()
5416 cfs_rq->h_nr_runnable--; in set_delayed()
5424 se->sched_delayed = 0; in clear_delayed()
5438 cfs_rq->h_nr_runnable++; in clear_delayed()
5447 if (sched_feat(DELAY_ZERO) && se->vlag > 0) in finish_delayed_dequeue_entity()
5448 se->vlag = 0; in finish_delayed_dequeue_entity()
5461 WARN_ON_ONCE(!se->sched_delayed); in dequeue_entity()
5471 WARN_ON_ONCE(delay && se->sched_delayed); in dequeue_entity()
5486 * - Update loads to have both entity and cfs_rq synced with now. in dequeue_entity()
5487 * - For group_entity, update its runnable_weight to reflect the new in dequeue_entity()
5489 * - Subtract its previous weight from cfs_rq->load.weight. in dequeue_entity()
5490 * - For group entity, update its weight to reflect the new share in dequeue_entity()
5500 se->deadline -= se->vruntime; in dequeue_entity()
5501 se->rel_deadline = 1; in dequeue_entity()
5504 if (se != cfs_rq->curr) in dequeue_entity()
5506 se->on_rq = 0; in dequeue_entity()
5518 * further than we started -- i.e. we'll be penalized. in dequeue_entity()
5526 if (cfs_rq->nr_queued == 0) in dequeue_entity()
5538 if (se->on_rq) { in set_next_entity()
5552 WARN_ON_ONCE(cfs_rq->curr); in set_next_entity()
5553 cfs_rq->curr = se; in set_next_entity()
5558 * when there are only lesser-weight tasks around): in set_next_entity()
5561 rq_of(cfs_rq)->cfs.load.weight >= 2*se->load.weight) { in set_next_entity()
5565 __schedstat_set(stats->slice_max, in set_next_entity()
5566 max((u64)stats->slice_max, in set_next_entity()
5567 se->sum_exec_runtime - se->prev_sum_exec_runtime)); in set_next_entity()
5570 se->prev_sum_exec_runtime = se->sum_exec_runtime; in set_next_entity()
5588 * Picking the ->next buddy will affect latency but not fairness. in pick_next_entity()
5591 cfs_rq->next && entity_eligible(cfs_rq, cfs_rq->next)) { in pick_next_entity()
5592 /* ->next will never be delayed */ in pick_next_entity()
5593 WARN_ON_ONCE(cfs_rq->next->sched_delayed); in pick_next_entity()
5594 return cfs_rq->next; in pick_next_entity()
5598 if (se->sched_delayed) { in pick_next_entity()
5616 if (prev->on_rq) in put_prev_entity()
5622 if (prev->on_rq) { in put_prev_entity()
5629 WARN_ON_ONCE(cfs_rq->curr != prev); in put_prev_entity()
5630 cfs_rq->curr = NULL; in put_prev_entity()
5637 * Update run-time statistics of the 'current'. in entity_tick()
5709 * directly instead of rq->clock to avoid adding additional synchronization
5710 * around rq->lock.
5712 * requires cfs_b->lock
5718 if (unlikely(cfs_b->quota == RUNTIME_INF)) in __refill_cfs_bandwidth_runtime()
5721 cfs_b->runtime += cfs_b->quota; in __refill_cfs_bandwidth_runtime()
5722 runtime = cfs_b->runtime_snap - cfs_b->runtime; in __refill_cfs_bandwidth_runtime()
5724 cfs_b->burst_time += runtime; in __refill_cfs_bandwidth_runtime()
5725 cfs_b->nr_burst++; in __refill_cfs_bandwidth_runtime()
5728 cfs_b->runtime = min(cfs_b->runtime, cfs_b->quota + cfs_b->burst); in __refill_cfs_bandwidth_runtime()
5729 cfs_b->runtime_snap = cfs_b->runtime; in __refill_cfs_bandwidth_runtime()
5734 return &tg->cfs_bandwidth; in tg_cfs_bandwidth()
5743 lockdep_assert_held(&cfs_b->lock); in __assign_cfs_rq_runtime()
5746 min_amount = target_runtime - cfs_rq->runtime_remaining; in __assign_cfs_rq_runtime()
5748 if (cfs_b->quota == RUNTIME_INF) in __assign_cfs_rq_runtime()
5753 if (cfs_b->runtime > 0) { in __assign_cfs_rq_runtime()
5754 amount = min(cfs_b->runtime, min_amount); in __assign_cfs_rq_runtime()
5755 cfs_b->runtime -= amount; in __assign_cfs_rq_runtime()
5756 cfs_b->idle = 0; in __assign_cfs_rq_runtime()
5760 cfs_rq->runtime_remaining += amount; in __assign_cfs_rq_runtime()
5762 return cfs_rq->runtime_remaining > 0; in __assign_cfs_rq_runtime()
5768 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in assign_cfs_rq_runtime()
5771 raw_spin_lock(&cfs_b->lock); in assign_cfs_rq_runtime()
5773 raw_spin_unlock(&cfs_b->lock); in assign_cfs_rq_runtime()
5781 cfs_rq->runtime_remaining -= delta_exec; in __account_cfs_rq_runtime()
5783 if (likely(cfs_rq->runtime_remaining > 0)) in __account_cfs_rq_runtime()
5786 if (cfs_rq->throttled) in __account_cfs_rq_runtime()
5792 if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr)) in __account_cfs_rq_runtime()
5799 if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled) in account_cfs_rq_runtime()
5807 return cfs_bandwidth_used() && cfs_rq->throttled; in cfs_rq_throttled()
5813 return cfs_bandwidth_used() && cfs_rq->throttle_count; in throttled_hierarchy()
5819 * load-balance operations.
5826 src_cfs_rq = tg->cfs_rq[src_cpu]; in throttled_lb_pair()
5827 dest_cfs_rq = tg->cfs_rq[dest_cpu]; in throttled_lb_pair()
5836 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in tg_unthrottle_up()
5838 cfs_rq->throttle_count--; in tg_unthrottle_up()
5839 if (!cfs_rq->throttle_count) { in tg_unthrottle_up()
5840 cfs_rq->throttled_clock_pelt_time += rq_clock_pelt(rq) - in tg_unthrottle_up()
5841 cfs_rq->throttled_clock_pelt; in tg_unthrottle_up()
5847 if (cfs_rq->throttled_clock_self) { in tg_unthrottle_up()
5848 u64 delta = rq_clock(rq) - cfs_rq->throttled_clock_self; in tg_unthrottle_up()
5850 cfs_rq->throttled_clock_self = 0; in tg_unthrottle_up()
5855 cfs_rq->throttled_clock_self_time += delta; in tg_unthrottle_up()
5865 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in tg_throttle_down()
5868 if (!cfs_rq->throttle_count) { in tg_throttle_down()
5869 cfs_rq->throttled_clock_pelt = rq_clock_pelt(rq); in tg_throttle_down()
5872 WARN_ON_ONCE(cfs_rq->throttled_clock_self); in tg_throttle_down()
5873 if (cfs_rq->nr_queued) in tg_throttle_down()
5874 cfs_rq->throttled_clock_self = rq_clock(rq); in tg_throttle_down()
5876 cfs_rq->throttle_count++; in tg_throttle_down()
5884 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in throttle_cfs_rq()
5887 long rq_h_nr_queued = rq->cfs.h_nr_queued; in throttle_cfs_rq()
5889 raw_spin_lock(&cfs_b->lock); in throttle_cfs_rq()
5898 * for 1ns of runtime rather than just check cfs_b. in throttle_cfs_rq()
5902 list_add_tail_rcu(&cfs_rq->throttled_list, in throttle_cfs_rq()
5903 &cfs_b->throttled_cfs_rq); in throttle_cfs_rq()
5905 raw_spin_unlock(&cfs_b->lock); in throttle_cfs_rq()
5910 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))]; in throttle_cfs_rq()
5914 walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq); in throttle_cfs_rq()
5917 queued_delta = cfs_rq->h_nr_queued; in throttle_cfs_rq()
5918 runnable_delta = cfs_rq->h_nr_runnable; in throttle_cfs_rq()
5919 idle_delta = cfs_rq->h_nr_idle; in throttle_cfs_rq()
5924 /* throttled entity or throttle-on-deactivate */ in throttle_cfs_rq()
5925 if (!se->on_rq) in throttle_cfs_rq()
5934 if (se->sched_delayed) in throttle_cfs_rq()
5939 idle_delta = cfs_rq->h_nr_queued; in throttle_cfs_rq()
5941 qcfs_rq->h_nr_queued -= queued_delta; in throttle_cfs_rq()
5942 qcfs_rq->h_nr_runnable -= runnable_delta; in throttle_cfs_rq()
5943 qcfs_rq->h_nr_idle -= idle_delta; in throttle_cfs_rq()
5945 if (qcfs_rq->load.weight) { in throttle_cfs_rq()
5946 /* Avoid re-evaluating load for this entity: */ in throttle_cfs_rq()
5954 /* throttled entity or throttle-on-deactivate */ in throttle_cfs_rq()
5955 if (!se->on_rq) in throttle_cfs_rq()
5962 idle_delta = cfs_rq->h_nr_queued; in throttle_cfs_rq()
5964 qcfs_rq->h_nr_queued -= queued_delta; in throttle_cfs_rq()
5965 qcfs_rq->h_nr_runnable -= runnable_delta; in throttle_cfs_rq()
5966 qcfs_rq->h_nr_idle -= idle_delta; in throttle_cfs_rq()
5973 if (rq_h_nr_queued && !rq->cfs.h_nr_queued) in throttle_cfs_rq()
5974 dl_server_stop(&rq->fair_server); in throttle_cfs_rq()
5978 * throttled-list. rq->lock protects completion. in throttle_cfs_rq()
5980 cfs_rq->throttled = 1; in throttle_cfs_rq()
5981 WARN_ON_ONCE(cfs_rq->throttled_clock); in throttle_cfs_rq()
5982 if (cfs_rq->nr_queued) in throttle_cfs_rq()
5983 cfs_rq->throttled_clock = rq_clock(rq); in throttle_cfs_rq()
5990 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in unthrottle_cfs_rq()
5993 long rq_h_nr_queued = rq->cfs.h_nr_queued; in unthrottle_cfs_rq()
5995 se = cfs_rq->tg->se[cpu_of(rq)]; in unthrottle_cfs_rq()
5997 cfs_rq->throttled = 0; in unthrottle_cfs_rq()
6001 raw_spin_lock(&cfs_b->lock); in unthrottle_cfs_rq()
6002 if (cfs_rq->throttled_clock) { in unthrottle_cfs_rq()
6003 cfs_b->throttled_time += rq_clock(rq) - cfs_rq->throttled_clock; in unthrottle_cfs_rq()
6004 cfs_rq->throttled_clock = 0; in unthrottle_cfs_rq()
6006 list_del_rcu(&cfs_rq->throttled_list); in unthrottle_cfs_rq()
6007 raw_spin_unlock(&cfs_b->lock); in unthrottle_cfs_rq()
6010 walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq); in unthrottle_cfs_rq()
6012 if (!cfs_rq->load.weight) { in unthrottle_cfs_rq()
6013 if (!cfs_rq->on_list) in unthrottle_cfs_rq()
6026 queued_delta = cfs_rq->h_nr_queued; in unthrottle_cfs_rq()
6027 runnable_delta = cfs_rq->h_nr_runnable; in unthrottle_cfs_rq()
6028 idle_delta = cfs_rq->h_nr_idle; in unthrottle_cfs_rq()
6033 if (se->sched_delayed) { in unthrottle_cfs_rq()
6037 } else if (se->on_rq) in unthrottle_cfs_rq()
6042 idle_delta = cfs_rq->h_nr_queued; in unthrottle_cfs_rq()
6044 qcfs_rq->h_nr_queued += queued_delta; in unthrottle_cfs_rq()
6045 qcfs_rq->h_nr_runnable += runnable_delta; in unthrottle_cfs_rq()
6046 qcfs_rq->h_nr_idle += idle_delta; in unthrottle_cfs_rq()
6060 idle_delta = cfs_rq->h_nr_queued; in unthrottle_cfs_rq()
6062 qcfs_rq->h_nr_queued += queued_delta; in unthrottle_cfs_rq()
6063 qcfs_rq->h_nr_runnable += runnable_delta; in unthrottle_cfs_rq()
6064 qcfs_rq->h_nr_idle += idle_delta; in unthrottle_cfs_rq()
6071 /* Start the fair server if un-throttling resulted in new runnable tasks */ in unthrottle_cfs_rq()
6072 if (!rq_h_nr_queued && rq->cfs.h_nr_queued) in unthrottle_cfs_rq()
6073 dl_server_start(&rq->fair_server); in unthrottle_cfs_rq()
6082 if (rq->curr == rq->idle && rq->cfs.nr_queued) in unthrottle_cfs_rq()
6104 * Since we hold rq lock we're safe from concurrent manipulation of in __cfsb_csd_unthrottle()
6112 list_for_each_entry_safe(cursor, tmp, &rq->cfsb_csd_list, in __cfsb_csd_unthrottle()
6114 list_del_init(&cursor->throttled_csd_list); in __cfsb_csd_unthrottle()
6137 if (WARN_ON_ONCE(!list_empty(&cfs_rq->throttled_csd_list))) in __unthrottle_cfs_rq_async()
6140 first = list_empty(&rq->cfsb_csd_list); in __unthrottle_cfs_rq_async()
6141 list_add_tail(&cfs_rq->throttled_csd_list, &rq->cfsb_csd_list); in __unthrottle_cfs_rq_async()
6143 smp_call_function_single_async(cpu_of(rq), &rq->cfsb_csd); in __unthrottle_cfs_rq_async()
6157 cfs_rq->runtime_remaining <= 0)) in unthrottle_cfs_rq_async()
6174 list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq, in distribute_cfs_runtime()
6188 if (!list_empty(&cfs_rq->throttled_csd_list)) in distribute_cfs_runtime()
6192 WARN_ON_ONCE(cfs_rq->runtime_remaining > 0); in distribute_cfs_runtime()
6194 raw_spin_lock(&cfs_b->lock); in distribute_cfs_runtime()
6195 runtime = -cfs_rq->runtime_remaining + 1; in distribute_cfs_runtime()
6196 if (runtime > cfs_b->runtime) in distribute_cfs_runtime()
6197 runtime = cfs_b->runtime; in distribute_cfs_runtime()
6198 cfs_b->runtime -= runtime; in distribute_cfs_runtime()
6199 remaining = cfs_b->runtime; in distribute_cfs_runtime()
6200 raw_spin_unlock(&cfs_b->lock); in distribute_cfs_runtime()
6202 cfs_rq->runtime_remaining += runtime; in distribute_cfs_runtime()
6205 if (cfs_rq->runtime_remaining > 0) { in distribute_cfs_runtime()
6214 list_add_tail(&cfs_rq->throttled_csd_list, in distribute_cfs_runtime()
6231 list_del_init(&cfs_rq->throttled_csd_list); in distribute_cfs_runtime()
6248 * period the timer is deactivated until scheduling resumes; cfs_b->idle is
6256 if (cfs_b->quota == RUNTIME_INF) in do_sched_cfs_period_timer()
6259 throttled = !list_empty(&cfs_b->throttled_cfs_rq); in do_sched_cfs_period_timer()
6260 cfs_b->nr_periods += overrun; in do_sched_cfs_period_timer()
6262 /* Refill extra burst quota even if cfs_b->idle */ in do_sched_cfs_period_timer()
6269 if (cfs_b->idle && !throttled) in do_sched_cfs_period_timer()
6274 cfs_b->idle = 1; in do_sched_cfs_period_timer()
6279 cfs_b->nr_throttled += overrun; in do_sched_cfs_period_timer()
6282 * This check is repeated as we release cfs_b->lock while we unthrottle. in do_sched_cfs_period_timer()
6284 while (throttled && cfs_b->runtime > 0) { in do_sched_cfs_period_timer()
6285 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in do_sched_cfs_period_timer()
6286 /* we can't nest cfs_b->lock while distributing bandwidth */ in do_sched_cfs_period_timer()
6288 raw_spin_lock_irqsave(&cfs_b->lock, flags); in do_sched_cfs_period_timer()
6297 cfs_b->idle = 0; in do_sched_cfs_period_timer()
6315 * Requires cfs_b->lock for hrtimer_expires_remaining to be safe against the
6321 struct hrtimer *refresh_timer = &cfs_b->period_timer; in runtime_refresh_within()
6324 /* if the call-back is running a quota refresh is already occurring */ in runtime_refresh_within()
6345 if (cfs_b->slack_started) in start_cfs_slack_bandwidth()
6347 cfs_b->slack_started = true; in start_cfs_slack_bandwidth()
6349 hrtimer_start(&cfs_b->slack_timer, in start_cfs_slack_bandwidth()
6357 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in __return_cfs_rq_runtime()
6358 s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime; in __return_cfs_rq_runtime()
6363 raw_spin_lock(&cfs_b->lock); in __return_cfs_rq_runtime()
6364 if (cfs_b->quota != RUNTIME_INF) { in __return_cfs_rq_runtime()
6365 cfs_b->runtime += slack_runtime; in __return_cfs_rq_runtime()
6367 /* we are under rq->lock, defer unthrottling using a timer */ in __return_cfs_rq_runtime()
6368 if (cfs_b->runtime > sched_cfs_bandwidth_slice() && in __return_cfs_rq_runtime()
6369 !list_empty(&cfs_b->throttled_cfs_rq)) in __return_cfs_rq_runtime()
6372 raw_spin_unlock(&cfs_b->lock); in __return_cfs_rq_runtime()
6375 cfs_rq->runtime_remaining -= slack_runtime; in __return_cfs_rq_runtime()
6383 if (!cfs_rq->runtime_enabled || cfs_rq->nr_queued) in return_cfs_rq_runtime()
6391 * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
6399 raw_spin_lock_irqsave(&cfs_b->lock, flags); in do_sched_cfs_slack_timer()
6400 cfs_b->slack_started = false; in do_sched_cfs_slack_timer()
6403 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in do_sched_cfs_slack_timer()
6407 if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) in do_sched_cfs_slack_timer()
6408 runtime = cfs_b->runtime; in do_sched_cfs_slack_timer()
6410 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in do_sched_cfs_slack_timer()
6421 * runtime as update_curr() throttling can not trigger until it's on-rq.
6428 /* an active group must be handled by the update_curr()->put() path */ in check_enqueue_throttle()
6429 if (!cfs_rq->runtime_enabled || cfs_rq->curr) in check_enqueue_throttle()
6438 if (cfs_rq->runtime_remaining <= 0) in check_enqueue_throttle()
6449 if (!tg->parent) in sync_throttle()
6452 cfs_rq = tg->cfs_rq[cpu]; in sync_throttle()
6453 pcfs_rq = tg->parent->cfs_rq[cpu]; in sync_throttle()
6455 cfs_rq->throttle_count = pcfs_rq->throttle_count; in sync_throttle()
6456 cfs_rq->throttled_clock_pelt = rq_clock_pelt(cpu_rq(cpu)); in sync_throttle()
6465 if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0)) in check_cfs_rq_runtime()
6499 raw_spin_lock_irqsave(&cfs_b->lock, flags); in sched_cfs_period_timer()
6501 overrun = hrtimer_forward_now(timer, cfs_b->period); in sched_cfs_period_timer()
6508 u64 new, old = ktime_to_ns(cfs_b->period); in sched_cfs_period_timer()
6511 * Grow period by a factor of 2 to avoid losing precision. in sched_cfs_period_timer()
6512 * Precision loss in the quota/period ratio can cause __cfs_schedulable in sched_cfs_period_timer()
6517 cfs_b->period = ns_to_ktime(new); in sched_cfs_period_timer()
6518 cfs_b->quota *= 2; in sched_cfs_period_timer()
6519 cfs_b->burst *= 2; in sched_cfs_period_timer()
6525 div_u64(cfs_b->quota, NSEC_PER_USEC)); in sched_cfs_period_timer()
6528 …"cfs_period_timer[cpu%d]: period too short, but cannot scale up without losing precision (cfs_peri… in sched_cfs_period_timer()
6531 div_u64(cfs_b->quota, NSEC_PER_USEC)); in sched_cfs_period_timer()
6539 cfs_b->period_active = 0; in sched_cfs_period_timer()
6540 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in sched_cfs_period_timer()
6547 raw_spin_lock_init(&cfs_b->lock); in init_cfs_bandwidth()
6548 cfs_b->runtime = 0; in init_cfs_bandwidth()
6549 cfs_b->quota = RUNTIME_INF; in init_cfs_bandwidth()
6550 cfs_b->period = ns_to_ktime(default_cfs_period()); in init_cfs_bandwidth()
6551 cfs_b->burst = 0; in init_cfs_bandwidth()
6552 cfs_b->hierarchical_quota = parent ? parent->hierarchical_quota : RUNTIME_INF; in init_cfs_bandwidth()
6554 INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq); in init_cfs_bandwidth()
6555 hrtimer_setup(&cfs_b->period_timer, sched_cfs_period_timer, CLOCK_MONOTONIC, in init_cfs_bandwidth()
6559 hrtimer_set_expires(&cfs_b->period_timer, in init_cfs_bandwidth()
6560 get_random_u32_below(cfs_b->period)); in init_cfs_bandwidth()
6561 hrtimer_setup(&cfs_b->slack_timer, sched_cfs_slack_timer, CLOCK_MONOTONIC, in init_cfs_bandwidth()
6563 cfs_b->slack_started = false; in init_cfs_bandwidth()
6568 cfs_rq->runtime_enabled = 0; in init_cfs_rq_runtime()
6569 INIT_LIST_HEAD(&cfs_rq->throttled_list); in init_cfs_rq_runtime()
6570 INIT_LIST_HEAD(&cfs_rq->throttled_csd_list); in init_cfs_rq_runtime()
6575 lockdep_assert_held(&cfs_b->lock); in start_cfs_bandwidth()
6577 if (cfs_b->period_active) in start_cfs_bandwidth()
6580 cfs_b->period_active = 1; in start_cfs_bandwidth()
6581 hrtimer_forward_now(&cfs_b->period_timer, cfs_b->period); in start_cfs_bandwidth()
6582 hrtimer_start_expires(&cfs_b->period_timer, HRTIMER_MODE_ABS_PINNED); in start_cfs_bandwidth()
6590 if (!cfs_b->throttled_cfs_rq.next) in destroy_cfs_bandwidth()
6593 hrtimer_cancel(&cfs_b->period_timer); in destroy_cfs_bandwidth()
6594 hrtimer_cancel(&cfs_b->slack_timer); in destroy_cfs_bandwidth()
6611 if (list_empty(&rq->cfsb_csd_list)) in destroy_cfs_bandwidth()
6637 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth; in update_runtime_enabled()
6638 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in update_runtime_enabled()
6640 raw_spin_lock(&cfs_b->lock); in update_runtime_enabled()
6641 cfs_rq->runtime_enabled = cfs_b->quota != RUNTIME_INF; in update_runtime_enabled()
6642 raw_spin_unlock(&cfs_b->lock); in update_runtime_enabled()
6667 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in unthrottle_offline_cfs_rqs()
6669 if (!cfs_rq->runtime_enabled) in unthrottle_offline_cfs_rqs()
6676 cfs_rq->runtime_enabled = 0; in unthrottle_offline_cfs_rqs()
6685 cfs_rq->runtime_remaining = 1; in unthrottle_offline_cfs_rqs()
6700 if (cfs_rq->runtime_enabled || in cfs_task_bw_constrained()
6701 tg_cfs_bandwidth(cfs_rq->tg)->hierarchical_quota != RUNTIME_INF) in cfs_task_bw_constrained()
6719 if (rq->nr_running != 1) in sched_fair_update_stop_tick()
6788 struct sched_entity *se = &p->se; in hrtick_start_fair()
6792 if (rq->cfs.h_nr_queued > 1) { in hrtick_start_fair()
6793 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime; in hrtick_start_fair()
6794 u64 slice = se->slice; in hrtick_start_fair()
6795 s64 delta = slice - ran; in hrtick_start_fair()
6813 struct task_struct *donor = rq->donor; in hrtick_update()
6815 if (!hrtick_enabled_fair(rq) || donor->sched_class != &fair_sched_class) in hrtick_update()
6851 return !sched_energy_enabled() || READ_ONCE(rd->overutilized); in is_rd_overutilized()
6859 WRITE_ONCE(rd->overutilized, flag); in set_rd_overutilized()
6870 if (!is_rd_overutilized(rq->rd) && cpu_overutilized(rq->cpu)) in check_update_overutilized_status()
6871 set_rd_overutilized(rq->rd, 1); in check_update_overutilized_status()
6880 return unlikely(rq->nr_running == rq->cfs.h_nr_idle && in sched_idle_rq()
6881 rq->nr_running); in sched_idle_rq()
6897 * se->sched_delayed should imply: se->on_rq == 1. in requeue_delayed_entity()
6901 WARN_ON_ONCE(!se->sched_delayed); in requeue_delayed_entity()
6902 WARN_ON_ONCE(!se->on_rq); in requeue_delayed_entity()
6906 if (se->vlag > 0) { in requeue_delayed_entity()
6907 cfs_rq->nr_queued--; in requeue_delayed_entity()
6908 if (se != cfs_rq->curr) in requeue_delayed_entity()
6910 se->vlag = 0; in requeue_delayed_entity()
6912 if (se != cfs_rq->curr) in requeue_delayed_entity()
6914 cfs_rq->nr_queued++; in requeue_delayed_entity()
6931 struct sched_entity *se = &p->se; in enqueue_task_fair()
6935 int rq_h_nr_queued = rq->cfs.h_nr_queued; in enqueue_task_fair()
6944 if (!(p->se.sched_delayed && (task_on_rq_migrating(p) || (flags & ENQUEUE_RESTORE)))) in enqueue_task_fair()
6945 util_est_enqueue(&rq->cfs, p); in enqueue_task_fair()
6957 if (p->in_iowait) in enqueue_task_fair()
6960 if (task_new && se->sched_delayed) in enqueue_task_fair()
6964 if (se->on_rq) { in enqueue_task_fair()
6965 if (se->sched_delayed) in enqueue_task_fair()
6974 * its entities in the desired time-frame. in enqueue_task_fair()
6977 se->slice = slice; in enqueue_task_fair()
6978 se->custom_slice = 1; in enqueue_task_fair()
6983 cfs_rq->h_nr_runnable += h_nr_runnable; in enqueue_task_fair()
6984 cfs_rq->h_nr_queued++; in enqueue_task_fair()
6985 cfs_rq->h_nr_idle += h_nr_idle; in enqueue_task_fair()
7004 se->slice = slice; in enqueue_task_fair()
7005 if (se != cfs_rq->curr) in enqueue_task_fair()
7006 min_vruntime_cb_propagate(&se->run_node, NULL); in enqueue_task_fair()
7009 cfs_rq->h_nr_runnable += h_nr_runnable; in enqueue_task_fair()
7010 cfs_rq->h_nr_queued++; in enqueue_task_fair()
7011 cfs_rq->h_nr_idle += h_nr_idle; in enqueue_task_fair()
7021 if (!rq_h_nr_queued && rq->cfs.h_nr_queued) { in enqueue_task_fair()
7023 if (!rq->nr_running) in enqueue_task_fair()
7024 dl_server_update_idle_time(rq, rq->curr); in enqueue_task_fair()
7025 dl_server_start(&rq->fair_server); in enqueue_task_fair()
7058 * failing half-way through and resume the dequeue later.
7061 * -1 - dequeue delayed
7062 * 0 - dequeue throttled
7063 * 1 - dequeue complete
7068 int rq_h_nr_queued = rq->cfs.h_nr_queued; in dequeue_entities()
7082 if (task_sleep || task_delayed || !se->sched_delayed) in dequeue_entities()
7090 if (p && &p->se == se) in dequeue_entities()
7091 return -1; in dequeue_entities()
7097 cfs_rq->h_nr_runnable -= h_nr_runnable; in dequeue_entities()
7098 cfs_rq->h_nr_queued -= h_nr_queued; in dequeue_entities()
7099 cfs_rq->h_nr_idle -= h_nr_idle; in dequeue_entities()
7109 if (cfs_rq->load.weight) { in dequeue_entities()
7112 /* Avoid re-evaluating load for this entity: */ in dequeue_entities()
7133 se->slice = slice; in dequeue_entities()
7134 if (se != cfs_rq->curr) in dequeue_entities()
7135 min_vruntime_cb_propagate(&se->run_node, NULL); in dequeue_entities()
7138 cfs_rq->h_nr_runnable -= h_nr_runnable; in dequeue_entities()
7139 cfs_rq->h_nr_queued -= h_nr_queued; in dequeue_entities()
7140 cfs_rq->h_nr_idle -= h_nr_idle; in dequeue_entities()
7152 if (rq_h_nr_queued && !rq->cfs.h_nr_queued) in dequeue_entities()
7153 dl_server_stop(&rq->fair_server); in dequeue_entities()
7157 rq->next_balance = jiffies; in dequeue_entities()
7161 WARN_ON_ONCE(p->on_rq != 1); in dequeue_entities()
7163 /* Fix-up what dequeue_task_fair() skipped */ in dequeue_entities()
7167 * Fix-up what block_task() skipped. in dequeue_entities()
7184 if (!(p->se.sched_delayed && (task_on_rq_migrating(p) || (flags & DEQUEUE_SAVE)))) in dequeue_task_fair()
7185 util_est_dequeue(&rq->cfs, p); in dequeue_task_fair()
7187 util_est_update(&rq->cfs, p, flags & DEQUEUE_SLEEP); in dequeue_task_fair()
7188 if (dequeue_entities(rq, &p->se, flags) < 0) in dequeue_task_fair()
7221 return cfs_rq_load_avg(&rq->cfs); in cpu_load()
7225 * cpu_load_without - compute CPU load without any contributions from *p
7243 if (cpu_of(rq) != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in cpu_load_without()
7246 cfs_rq = &rq->cfs; in cpu_load_without()
7247 load = READ_ONCE(cfs_rq->avg.load_avg); in cpu_load_without()
7257 return cfs_rq_runnable_avg(&rq->cfs); in cpu_runnable()
7266 if (cpu_of(rq) != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in cpu_runnable_without()
7269 cfs_rq = &rq->cfs; in cpu_runnable_without()
7270 runnable = READ_ONCE(cfs_rq->avg.runnable_avg); in cpu_runnable_without()
7273 lsub_positive(&runnable, p->se.avg.runnable_avg); in cpu_runnable_without()
7280 return cpu_rq(cpu)->cpu_capacity; in capacity_of()
7289 if (time_after(jiffies, current->wakee_flip_decay_ts + HZ)) { in record_wakee()
7290 current->wakee_flips >>= 1; in record_wakee()
7291 current->wakee_flip_decay_ts = jiffies; in record_wakee()
7294 if (current->last_wakee != p) { in record_wakee()
7295 current->last_wakee = p; in record_wakee()
7296 current->wakee_flips++; in record_wakee()
7301 * Detect M:N waker/wakee relationships via a switching-frequency heuristic.
7311 * non-monogamous, with partner count exceeding socket size.
7319 unsigned int master = current->wakee_flips; in wake_wide()
7320 unsigned int slave = p->wakee_flips; in wake_wide()
7335 * wake_affine_idle() - only considers 'now', it check if the waking CPU is
7336 * cache-affine and is (or will be) idle.
7338 * wake_affine_weight() - considers the weight to reflect the average
7360 if (sync && cpu_rq(this_cpu)->nr_running == 1) in wake_affine_idle()
7384 this_eff_load -= current_load; in wake_affine_weight()
7395 prev_eff_load -= task_load; in wake_affine_weight()
7397 prev_eff_load *= 100 + (sd->imbalance_pct - 100) / 2; in wake_affine_weight()
7423 schedstat_inc(p->stats.nr_wakeups_affine_attempts); in wake_affine()
7427 schedstat_inc(sd->ttwu_move_affine); in wake_affine()
7428 schedstat_inc(p->stats.nr_wakeups_affine); in wake_affine()
7436 * sched_balance_find_dst_group_cpu - find the idlest CPU among the CPUs in the group.
7445 int shallowest_idle_cpu = -1; in sched_balance_find_dst_group_cpu()
7449 if (group->group_weight == 1) in sched_balance_find_dst_group_cpu()
7453 for_each_cpu_and(i, sched_group_span(group), p->cpus_ptr) { in sched_balance_find_dst_group_cpu()
7464 if (idle && idle->exit_latency < min_exit_latency) { in sched_balance_find_dst_group_cpu()
7470 min_exit_latency = idle->exit_latency; in sched_balance_find_dst_group_cpu()
7471 latest_idle_timestamp = rq->idle_stamp; in sched_balance_find_dst_group_cpu()
7473 } else if ((!idle || idle->exit_latency == min_exit_latency) && in sched_balance_find_dst_group_cpu()
7474 rq->idle_stamp > latest_idle_timestamp) { in sched_balance_find_dst_group_cpu()
7480 latest_idle_timestamp = rq->idle_stamp; in sched_balance_find_dst_group_cpu()
7483 } else if (shallowest_idle_cpu == -1) { in sched_balance_find_dst_group_cpu()
7492 return shallowest_idle_cpu != -1 ? shallowest_idle_cpu : least_loaded_cpu; in sched_balance_find_dst_group_cpu()
7500 if (!cpumask_intersects(sched_domain_span(sd), p->cpus_ptr)) in sched_balance_find_dst_cpu()
7508 sync_entity_load_avg(&p->se); in sched_balance_find_dst_cpu()
7515 if (!(sd->flags & sd_flag)) { in sched_balance_find_dst_cpu()
7516 sd = sd->child; in sched_balance_find_dst_cpu()
7522 sd = sd->child; in sched_balance_find_dst_cpu()
7529 sd = sd->child; in sched_balance_find_dst_cpu()
7535 weight = sd->span_weight; in sched_balance_find_dst_cpu()
7538 if (weight <= tmp->span_weight) in sched_balance_find_dst_cpu()
7540 if (tmp->flags & sd_flag) in sched_balance_find_dst_cpu()
7554 return -1; in __select_idle_cpu()
7567 WRITE_ONCE(sds->has_idle_cores, val); in set_idle_cores()
7576 return READ_ONCE(sds->has_idle_cores); in test_idle_cores()
7583 * information in sd_llc_shared->has_idle_cores.
7613 * sd_llc->shared->has_idle_cores and enabled through update_idle_core() above.
7623 if (*idle_cpu == -1) { in select_idle_core()
7632 if (*idle_cpu == -1 && cpumask_test_cpu(cpu, cpus)) in select_idle_core()
7640 return -1; in select_idle_core()
7650 for_each_cpu_and(cpu, cpu_smt_mask(target), p->cpus_ptr) { in select_idle_smt()
7663 return -1; in select_idle_smt()
7684 return -1; in select_idle_smt()
7691 * comparing the average scan cost (tracked in sd->avg_scan_cost) against the
7692 * average idle time for this rq (as found in rq->avg_idle).
7697 int i, cpu, idle_cpu = -1, nr = INT_MAX; in select_idle_cpu()
7700 cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr); in select_idle_cpu()
7705 /* because !--nr is the condition to stop scan */ in select_idle_cpu()
7706 nr = READ_ONCE(sd_share->nr_idle_scan) + 1; in select_idle_cpu()
7709 return -1; in select_idle_cpu()
7714 struct sched_group *sg = sd->groups; in select_idle_cpu()
7716 if (sg->flags & SD_CLUSTER) { in select_idle_cpu()
7726 if (--nr <= 0) in select_idle_cpu()
7727 return -1; in select_idle_cpu()
7744 if (--nr <= 0) in select_idle_cpu()
7745 return -1; in select_idle_cpu()
7768 int cpu, best_cpu = -1; in select_idle_capacity()
7772 cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr); in select_idle_capacity()
7797 * First, select CPU which fits better (-1 being better than 0). in select_idle_capacity()
7834 int i, recent_used_cpu, prev_aff = -1; in select_idle_sibling()
7841 sync_entity_load_avg(&p->se); in select_idle_sibling()
7848 * per-cpu select_rq_mask usage in select_idle_sibling()
7871 * Allow a per-cpu kthread to stack with the wakee if the in select_idle_sibling()
7874 * per-cpu kthread that is now complete and the wakeup is in select_idle_sibling()
7881 this_rq()->nr_running <= 1 && in select_idle_sibling()
7887 recent_used_cpu = p->recent_used_cpu; in select_idle_sibling()
7888 p->recent_used_cpu = prev; in select_idle_sibling()
7893 cpumask_test_cpu(recent_used_cpu, p->cpus_ptr) && in select_idle_sibling()
7901 recent_used_cpu = -1; in select_idle_sibling()
7957 * cpu_util() - Estimates the amount of CPU capacity used by CFS tasks.
7960 * @dst_cpu: CPU @p migrates to, -1 if @p moves from @cpu or @p == NULL
7961 * @boost: 1 to enable boosting, otherwise 0
7967 * recent utilization of currently non-runnable tasks on that CPU.
7975 * previously-executed tasks, which helps better deduce how busy a CPU will
7976 * be when a long-sleeping task wakes up. The contribution to CPU utilization
7990 * could be seen as over-utilized even though CPU1 has 20% of spare CPU
7993 * after task migrations (scheduler-driven DVFS).
8000 struct cfs_rq *cfs_rq = &cpu_rq(cpu)->cfs; in cpu_util()
8001 unsigned long util = READ_ONCE(cfs_rq->avg.util_avg); in cpu_util()
8005 runnable = READ_ONCE(cfs_rq->avg.runnable_avg); in cpu_util()
8010 * If @dst_cpu is -1 or @p migrates from @cpu to @dst_cpu remove its in cpu_util()
8023 util_est = READ_ONCE(cfs_rq->avg.util_est); in cpu_util()
8026 * During wake-up @p isn't enqueued yet and doesn't contribute in cpu_util()
8027 * to any cpu_rq(cpu)->cfs.avg.util_est. in cpu_util()
8031 * During exec (@dst_cpu = -1) @p is enqueued and does in cpu_util()
8032 * contribute to cpu_rq(cpu)->cfs.util_est. in cpu_util()
8041 * p->on_rq = TASK_ON_RQ_MIGRATING; in cpu_util()
8042 * -------------------------------- A in cpu_util()
8046 * -------------------------------- B in cpu_util()
8064 return cpu_util(cpu, NULL, -1, 0); in cpu_util_cfs()
8069 return cpu_util(cpu, NULL, -1, 1); in cpu_util_cfs_boost()
8088 if (cpu != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in cpu_util_without()
8091 return cpu_util(cpu, p, -1, 0); in cpu_util_without()
8106 * The cfs,rt,dl utilization are the running times measured with rq->clock_task
8107 * which excludes things like IRQ and steal-time. These latter are then accrued
8125 * because of inaccuracies in how we track these -- see in effective_cpu_util()
8140 * - the computed DL bandwidth needed with the IRQ pressure which in effective_cpu_util()
8142 * - The minimum performance requirement for CFS and/or RT. in effective_cpu_util()
8150 if (!uclamp_is_used() && rt_rq_is_runnable(&rq->rt)) in effective_cpu_util()
8178 * max - irq in effective_cpu_util()
8179 * U' = irq + --------- * U in effective_cpu_util()
8194 * energy_env - Utilization landscape for energy estimation.
8200 * @pd_cap: Entire perf domain capacity. (pd->nr_cpus * cpu_cap).
8226 eenv->task_busy_time = busy_time; in eenv_task_busy_time()
8240 * - A stable PD utilization, no matter which CPU of that PD we want to place
8243 * - A fair comparison between CPUs as the task contribution (task_util())
8248 * exceed @eenv->pd_cap.
8258 unsigned long util = cpu_util(cpu, p, -1, 0); in eenv_pd_busy_time()
8263 eenv->pd_busy_time = min(eenv->pd_cap, busy_time); in eenv_pd_busy_time()
8270 * Returns the maximum utilization among @eenv->cpus. This utilization can't
8271 * exceed @eenv->cpu_cap.
8312 return min(max_util, eenv->cpu_cap); in eenv_pd_max_util()
8325 unsigned long busy_time = eenv->pd_busy_time; in compute_energy()
8329 busy_time = min(eenv->pd_cap, busy_time + eenv->task_busy_time); in compute_energy()
8331 energy = em_cpu_energy(pd->em_pd, max_util, busy_time, eenv->cpu_cap); in compute_energy()
8339 * find_energy_efficient_cpu(): Find most energy-efficient target CPU for the
8343 * out which of the CPU candidates is the most energy-efficient.
8360 * cluster-packing, and spreading inside a cluster. That should at least be
8367 * NOTE: Forkees are not accepted in the energy-aware wake-up path because
8371 * to be energy-inefficient in some use-cases. The alternative would be to
8374 * other use-cases too. So, until someone finds a better way to solve this,
8375 * let's keep things simple by re-using the existing slow path.
8383 struct root_domain *rd = this_rq()->rd; in find_energy_efficient_cpu()
8384 int cpu, best_energy_cpu, target = -1; in find_energy_efficient_cpu()
8385 int prev_fits = -1, best_fits = -1; in find_energy_efficient_cpu()
8393 pd = rcu_dereference(rd->pd); in find_energy_efficient_cpu()
8398 * Energy-aware wake-up happens on the lowest sched_domain starting in find_energy_efficient_cpu()
8403 sd = sd->parent; in find_energy_efficient_cpu()
8409 sync_entity_load_avg(&p->se); in find_energy_efficient_cpu()
8415 for (; pd; pd = pd->next) { in find_energy_efficient_cpu()
8418 long prev_spare_cap = -1, max_spare_cap = -1; in find_energy_efficient_cpu()
8421 int max_spare_cap_cpu = -1; in find_energy_efficient_cpu()
8422 int fits, max_fits = -1; in find_energy_efficient_cpu()
8444 if (!cpumask_test_cpu(cpu, p->cpus_ptr)) in find_energy_efficient_cpu()
8463 * max-aggregated uclamp_{min, max}. in find_energy_efficient_cpu()
8500 base_energy = compute_energy(&eenv, pd, cpus, p, -1); in find_energy_efficient_cpu()
8503 if (prev_spare_cap > -1) { in find_energy_efficient_cpu()
8509 prev_delta -= base_energy; in find_energy_efficient_cpu()
8533 cur_delta -= base_energy; in find_energy_efficient_cpu()
8577 int sync = (wake_flags & WF_SYNC) && !(current->flags & PF_EXITING); in select_task_rq_fair()
8586 * required for stable ->cpus_allowed in select_task_rq_fair()
8588 lockdep_assert_held(&p->pi_lock); in select_task_rq_fair()
8593 cpumask_test_cpu(cpu, p->cpus_ptr)) in select_task_rq_fair()
8596 if (!is_rd_overutilized(this_rq()->rd)) { in select_task_rq_fair()
8603 want_affine = !wake_wide(p) && cpumask_test_cpu(cpu, p->cpus_ptr); in select_task_rq_fair()
8612 if (want_affine && (tmp->flags & SD_WAKE_AFFINE) && in select_task_rq_fair()
8626 if (tmp->flags & sd_flag) in select_task_rq_fair()
8647 * previous CPU. The caller guarantees p->pi_lock or task_rq(p)->lock is held.
8651 struct sched_entity *se = &p->se; in migrate_task_rq_fair()
8660 * leading to an inflation after wake-up on the new rq. in migrate_task_rq_fair()
8670 se->avg.last_update_time = 0; in migrate_task_rq_fair()
8677 struct sched_entity *se = &p->se; in task_dead_fair()
8679 if (se->sched_delayed) { in task_dead_fair()
8684 if (se->sched_delayed) { in task_dead_fair()
8709 if (!cpumask_intersects(p->cpus_ptr, cpumask)) in set_task_max_allowed_capacity()
8712 p->max_allowed_capacity = entry->capacity; in set_task_max_allowed_capacity()
8739 if (WARN_ON_ONCE(!se->on_rq)) in set_next_buddy()
8743 cfs_rq_of(se)->next = se; in set_next_buddy()
8752 struct task_struct *donor = rq->donor; in check_preempt_wakeup_fair()
8753 struct sched_entity *se = &donor->se, *pse = &p->se; in check_preempt_wakeup_fair()
8764 * next-buddy nomination below. in check_preempt_wakeup_fair()
8769 if (sched_feat(NEXT_BUDDY) && !(wake_flags & WF_FORK) && !pse->sched_delayed) { in check_preempt_wakeup_fair()
8777 * Note: this also catches the edge-case of curr being in a throttled in check_preempt_wakeup_fair()
8783 if (test_tsk_need_resched(rq->curr)) in check_preempt_wakeup_fair()
8796 * Preempt an idle entity in favor of a non-idle entity (and don't preempt in check_preempt_wakeup_fair()
8801 * When non-idle entity preempt an idle entity, in check_preempt_wakeup_fair()
8814 if (unlikely(!normal_policy(p->policy))) in check_preempt_wakeup_fair()
8847 cfs_rq = &rq->cfs; in pick_task_fair()
8848 if (!cfs_rq->nr_queued) in pick_task_fair()
8853 if (cfs_rq->curr && cfs_rq->curr->on_rq) in pick_task_fair()
8882 se = &p->se; in pick_next_task_fair()
8885 if (prev->sched_class != &fair_sched_class) in pick_next_task_fair()
8902 struct sched_entity *pse = &prev->se; in pick_next_task_fair()
8906 int se_depth = se->depth; in pick_next_task_fair()
8907 int pse_depth = pse->depth; in pick_next_task_fair()
8939 * Because sched_balance_newidle() releases (and re-acquires) rq->lock, it is in pick_next_task_fair()
8941 * must re-start the pick_next_entity() loop. in pick_next_task_fair()
8965 return !!dl_se->rq->cfs.nr_queued; in fair_server_has_tasks()
8970 return pick_task_fair(dl_se->rq); in fair_server_pick_task()
8975 struct sched_dl_entity *dl_se = &rq->fair_server; in fair_server_init()
8987 struct sched_entity *se = &prev->se; in put_prev_task_fair()
9001 struct task_struct *curr = rq->curr; in yield_task_fair()
9003 struct sched_entity *se = &curr->se; in yield_task_fair()
9008 if (unlikely(rq->nr_running == 1)) in yield_task_fair()
9015 * Update run-time statistics of the 'current'. in yield_task_fair()
9025 se->deadline += calc_delta_fair(se->slice, se); in yield_task_fair()
9030 struct sched_entity *se = &p->se; in yield_to_task_fair()
9033 if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se))) in yield_to_task_fair()
9046 * Fair scheduling class load-balancing methods.
9050 * The purpose of load-balancing is to achieve the same basic fairness the
9051 * per-CPU scheduler provides, namely provide a proportional amount of compute
9056 * Where W_i,n is the n-th weight average for CPU i. The instantaneous weight
9061 * Where w_i,j is the weight of the j-th runnable task on CPU i. This weight
9067 * W'_i,n = (2^n - 1) / 2^n * W_i,n + 1 / 2^n * W_i,0 (3)
9076 * imb_i,j = max{ avg(W/C), W_i/C_i } - min{ avg(W/C), W_j/C_j } (4)
9083 * - infeasible weights;
9084 * - local vs global optima in the discrete case. ]
9094 * of load-balance at each level inversely proportional to the number of CPUs in
9100 * \Sum { --- * --- * 2^i } = O(n) (5)
9102 * `- size of each group
9103 * | | `- number of CPUs doing load-balance
9104 * | `- freq
9105 * `- sum over all levels
9116 * A_i,j = \Union (i % 2^k == 0) && i / 2^(k+1) == j / 2^(k+1) (6)
9147 * W_i,0 = \Sum_j \Prod_k w_k * ----- (9)
9154 * w_i,j,k is the weight of the j-th runnable task in the k-th cgroup on CPU i.
9236 /* The set of CPUs under consideration for load-balancing */
9251 * Is this task likely cache-hot:
9257 lockdep_assert_rq_held(env->src_rq); in task_hot()
9259 if (p->sched_class != &fair_sched_class) in task_hot()
9266 if (env->sd->flags & SD_SHARE_CPUCAPACITY) in task_hot()
9272 if (sched_feat(CACHE_HOT_BUDDY) && env->dst_rq->nr_running && in task_hot()
9273 (&p->se == cfs_rq_of(&p->se)->next)) in task_hot()
9276 if (sysctl_sched_migration_cost == -1) in task_hot()
9283 if (!sched_core_cookie_match(cpu_rq(env->dst_cpu), p)) in task_hot()
9289 delta = rq_clock_task(env->src_rq) - p->se.exec_start; in task_hot()
9302 struct numa_group *numa_group = rcu_dereference(p->numa_group); in migrate_degrades_locality()
9309 if (!p->numa_faults || !(env->sd->flags & SD_NUMA)) in migrate_degrades_locality()
9312 src_nid = cpu_to_node(env->src_cpu); in migrate_degrades_locality()
9313 dst_nid = cpu_to_node(env->dst_cpu); in migrate_degrades_locality()
9319 if (src_nid == p->numa_preferred_nid) { in migrate_degrades_locality()
9320 if (env->src_rq->nr_running > env->src_rq->nr_preferred_running) in migrate_degrades_locality()
9327 if (dst_nid == p->numa_preferred_nid) in migrate_degrades_locality()
9328 return -1; in migrate_degrades_locality()
9331 if (env->idle == CPU_IDLE) in migrate_degrades_locality()
9343 return src_weight - dst_weight; in migrate_degrades_locality()
9358 * dst_cfs_rq->nr_queued is greater than 1, if the task
9367 dst_cfs_rq = task_group(p)->cfs_rq[dest_cpu]; in task_is_ineligible_on_dst_cpu()
9369 dst_cfs_rq = &cpu_rq(dest_cpu)->cfs; in task_is_ineligible_on_dst_cpu()
9371 if (sched_feat(PLACE_LAG) && dst_cfs_rq->nr_queued && in task_is_ineligible_on_dst_cpu()
9372 !entity_eligible(task_cfs_rq(p), &p->se)) in task_is_ineligible_on_dst_cpu()
9379 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
9386 lockdep_assert_rq_held(env->src_rq); in can_migrate_task()
9387 if (p->sched_task_hot) in can_migrate_task()
9388 p->sched_task_hot = 0; in can_migrate_task()
9396 * 5) are cache-hot on their current CPU. in can_migrate_task()
9398 if ((p->se.sched_delayed) && (env->migration_type != migrate_load)) in can_migrate_task()
9401 if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu)) in can_migrate_task()
9406 * For ineligible tasks we soft-limit them and only allow in can_migrate_task()
9407 * them to migrate when nr_balance_failed is non-zero to in can_migrate_task()
9408 * avoid load-balancing trying very hard to balance the load. in can_migrate_task()
9410 if (!env->sd->nr_balance_failed && in can_migrate_task()
9411 task_is_ineligible_on_dst_cpu(p, env->dst_cpu)) in can_migrate_task()
9418 if (!cpumask_test_cpu(env->dst_cpu, p->cpus_ptr)) { in can_migrate_task()
9421 schedstat_inc(p->stats.nr_failed_migrations_affine); in can_migrate_task()
9423 env->flags |= LBF_SOME_PINNED; in can_migrate_task()
9431 * - for NEWLY_IDLE in can_migrate_task()
9432 * - if we have already computed one in current iteration in can_migrate_task()
9433 * - if it's an active balance in can_migrate_task()
9435 if (env->idle == CPU_NEWLY_IDLE || in can_migrate_task()
9436 env->flags & (LBF_DST_PINNED | LBF_ACTIVE_LB)) in can_migrate_task()
9439 /* Prevent to re-select dst_cpu via env's CPUs: */ in can_migrate_task()
9440 cpu = cpumask_first_and_and(env->dst_grpmask, env->cpus, p->cpus_ptr); in can_migrate_task()
9443 env->flags |= LBF_DST_PINNED; in can_migrate_task()
9444 env->new_dst_cpu = cpu; in can_migrate_task()
9451 env->flags &= ~LBF_ALL_PINNED; in can_migrate_task()
9453 if (task_on_cpu(env->src_rq, p)) { in can_migrate_task()
9454 schedstat_inc(p->stats.nr_failed_migrations_running); in can_migrate_task()
9465 if (env->flags & LBF_ACTIVE_LB) in can_migrate_task()
9474 if (!hot || env->sd->nr_balance_failed > env->sd->cache_nice_tries) { in can_migrate_task()
9476 p->sched_task_hot = 1; in can_migrate_task()
9480 schedstat_inc(p->stats.nr_failed_migrations_hot); in can_migrate_task()
9485 * detach_task() -- detach the task for the migration specified in env
9489 lockdep_assert_rq_held(env->src_rq); in detach_task()
9491 if (p->sched_task_hot) { in detach_task()
9492 p->sched_task_hot = 0; in detach_task()
9493 schedstat_inc(env->sd->lb_hot_gained[env->idle]); in detach_task()
9494 schedstat_inc(p->stats.nr_forced_migrations); in detach_task()
9497 deactivate_task(env->src_rq, p, DEQUEUE_NOCLOCK); in detach_task()
9498 set_task_cpu(p, env->dst_cpu); in detach_task()
9502 * detach_one_task() -- tries to dequeue exactly one task from env->src_rq, as
9511 lockdep_assert_rq_held(env->src_rq); in detach_one_task()
9514 &env->src_rq->cfs_tasks, se.group_node) { in detach_one_task()
9522 * lb_gained[env->idle] is updated (other is detach_tasks) in detach_one_task()
9526 schedstat_inc(env->sd->lb_gained[env->idle]); in detach_one_task()
9533 * detach_tasks() -- tries to detach up to imbalance load/util/tasks from
9540 struct list_head *tasks = &env->src_rq->cfs_tasks; in detach_tasks()
9545 lockdep_assert_rq_held(env->src_rq); in detach_tasks()
9551 if (env->src_rq->nr_running <= 1) { in detach_tasks()
9552 env->flags &= ~LBF_ALL_PINNED; in detach_tasks()
9556 if (env->imbalance <= 0) in detach_tasks()
9564 if (env->idle && env->src_rq->nr_running <= 1) in detach_tasks()
9567 env->loop++; in detach_tasks()
9569 if (env->loop > env->loop_max) in detach_tasks()
9573 if (env->loop > env->loop_break) { in detach_tasks()
9574 env->loop_break += SCHED_NR_MIGRATE_BREAK; in detach_tasks()
9575 env->flags |= LBF_NEED_BREAK; in detach_tasks()
9584 switch (env->migration_type) { in detach_tasks()
9589 * value. Make sure that env->imbalance decreases in detach_tasks()
9596 load < 16 && !env->sd->nr_balance_failed) in detach_tasks()
9605 if (shr_bound(load, env->sd->nr_balance_failed) > env->imbalance) in detach_tasks()
9608 env->imbalance -= load; in detach_tasks()
9614 if (shr_bound(util, env->sd->nr_balance_failed) > env->imbalance) in detach_tasks()
9617 env->imbalance -= util; in detach_tasks()
9621 env->imbalance--; in detach_tasks()
9626 if (task_fits_cpu(p, env->src_cpu)) in detach_tasks()
9629 env->imbalance = 0; in detach_tasks()
9634 list_add(&p->se.group_node, &env->tasks); in detach_tasks()
9644 if (env->idle == CPU_NEWLY_IDLE) in detach_tasks()
9652 if (env->imbalance <= 0) in detach_tasks()
9657 if (p->sched_task_hot) in detach_tasks()
9658 schedstat_inc(p->stats.nr_failed_migrations_hot); in detach_tasks()
9660 list_move(&p->se.group_node, tasks); in detach_tasks()
9668 schedstat_add(env->sd->lb_gained[env->idle], detached); in detach_tasks()
9674 * attach_task() -- attach the task detached by detach_task() to its new rq.
9686 * attach_one_task() -- attaches the task returned from detach_one_task() to
9700 * attach_tasks() -- attaches all tasks detached by detach_tasks() to their
9705 struct list_head *tasks = &env->tasks; in attach_tasks()
9709 rq_lock(env->dst_rq, &rf); in attach_tasks()
9710 update_rq_clock(env->dst_rq); in attach_tasks()
9714 list_del_init(&p->se.group_node); in attach_tasks()
9716 attach_task(env->dst_rq, p); in attach_tasks()
9719 rq_unlock(env->dst_rq, &rf); in attach_tasks()
9725 if (cfs_rq->avg.load_avg) in cfs_rq_has_blocked()
9728 if (cfs_rq->avg.util_avg) in cfs_rq_has_blocked()
9753 WRITE_ONCE(rq->last_blocked_load_update_tick, jiffies); in update_blocked_load_tick()
9759 rq->has_blocked_load = 0; in update_blocked_load_status()
9802 if (cfs_rq->nr_queued == 0) in __update_blocked_fair()
9805 if (cfs_rq == &rq->cfs) in __update_blocked_fair()
9810 se = cfs_rq->tg->se[cpu]; in __update_blocked_fair()
9831 * This needs to be done in a top-down fashion because the load of a child
9837 struct sched_entity *se = cfs_rq->tg->se[cpu_of(rq)]; in update_cfs_rq_h_load()
9841 if (cfs_rq->last_h_load_update == now) in update_cfs_rq_h_load()
9844 WRITE_ONCE(cfs_rq->h_load_next, NULL); in update_cfs_rq_h_load()
9847 WRITE_ONCE(cfs_rq->h_load_next, se); in update_cfs_rq_h_load()
9848 if (cfs_rq->last_h_load_update == now) in update_cfs_rq_h_load()
9853 cfs_rq->h_load = cfs_rq_load_avg(cfs_rq); in update_cfs_rq_h_load()
9854 cfs_rq->last_h_load_update = now; in update_cfs_rq_h_load()
9857 while ((se = READ_ONCE(cfs_rq->h_load_next)) != NULL) { in update_cfs_rq_h_load()
9858 load = cfs_rq->h_load; in update_cfs_rq_h_load()
9859 load = div64_ul(load * se->avg.load_avg, in update_cfs_rq_h_load()
9862 cfs_rq->h_load = load; in update_cfs_rq_h_load()
9863 cfs_rq->last_h_load_update = now; in update_cfs_rq_h_load()
9872 return div64_ul(p->se.avg.load_avg * cfs_rq->h_load, in task_h_load()
9878 struct cfs_rq *cfs_rq = &rq->cfs; in __update_blocked_fair()
9890 return p->se.avg.load_avg; in task_h_load()
9916 * sg_lb_stats - stats of a sched_group required for load-balancing:
9939 * sd_lb_stats - stats of a sched_domain required for load-balancing:
9996 free = max - used; in scale_rt_capacity()
10004 struct sched_group *sdg = sd->groups; in update_cpu_capacity()
10009 cpu_rq(cpu)->cpu_capacity = capacity; in update_cpu_capacity()
10012 sdg->sgc->capacity = capacity; in update_cpu_capacity()
10013 sdg->sgc->min_capacity = capacity; in update_cpu_capacity()
10014 sdg->sgc->max_capacity = capacity; in update_cpu_capacity()
10019 struct sched_domain *child = sd->child; in update_group_capacity()
10020 struct sched_group *group, *sdg = sd->groups; in update_group_capacity()
10024 interval = msecs_to_jiffies(sd->balance_interval); in update_group_capacity()
10026 sdg->sgc->next_update = jiffies + interval; in update_group_capacity()
10037 if (child->flags & SD_OVERLAP) { in update_group_capacity()
10056 group = child->groups; in update_group_capacity()
10058 struct sched_group_capacity *sgc = group->sgc; in update_group_capacity()
10060 capacity += sgc->capacity; in update_group_capacity()
10061 min_capacity = min(sgc->min_capacity, min_capacity); in update_group_capacity()
10062 max_capacity = max(sgc->max_capacity, max_capacity); in update_group_capacity()
10063 group = group->next; in update_group_capacity()
10064 } while (group != child->groups); in update_group_capacity()
10067 sdg->sgc->capacity = capacity; in update_group_capacity()
10068 sdg->sgc->min_capacity = min_capacity; in update_group_capacity()
10069 sdg->sgc->max_capacity = max_capacity; in update_group_capacity()
10080 return ((rq->cpu_capacity * sd->imbalance_pct) < in check_cpu_capacity()
10087 return rq->misfit_task_load; in check_misfit_status()
10092 * groups is inadequate due to ->cpus_ptr constraints.
10098 * { 0 1 2 3 } { 4 5 6 7 }
10101 * If we were to balance group-wise we'd place two tasks in the first group and
10121 return group->sgc->imbalance; in sg_imbalanced()
10139 if (sgs->sum_nr_running < sgs->group_weight) in group_has_capacity()
10142 if ((sgs->group_capacity * imbalance_pct) < in group_has_capacity()
10143 (sgs->group_runnable * 100)) in group_has_capacity()
10146 if ((sgs->group_capacity * 100) > in group_has_capacity()
10147 (sgs->group_util * imbalance_pct)) in group_has_capacity()
10164 if (sgs->sum_nr_running <= sgs->group_weight) in group_is_overloaded()
10167 if ((sgs->group_capacity * 100) < in group_is_overloaded()
10168 (sgs->group_util * imbalance_pct)) in group_is_overloaded()
10171 if ((sgs->group_capacity * imbalance_pct) < in group_is_overloaded()
10172 (sgs->group_runnable * 100)) in group_is_overloaded()
10189 if (sgs->group_asym_packing) in group_classify()
10192 if (sgs->group_smt_balance) in group_classify()
10195 if (sgs->group_misfit_task_load) in group_classify()
10205 * sched_use_asym_prio - Check whether asym_packing priority must be used
10217 if (!(sd->flags & SD_ASYM_PACKING)) in sched_use_asym_prio()
10223 return sd->flags & SD_SHARE_CPUCAPACITY || is_core_idle(cpu); in sched_use_asym_prio()
10237 * sched_group_asym - Check if the destination CPU can do asym_packing balance
10239 * @sgs: Load-balancing statistics of the candidate busiest group
10255 if ((group->flags & SD_SHARE_CPUCAPACITY) && in sched_group_asym()
10256 (sgs->group_weight - sgs->idle_cpus != 1)) in sched_group_asym()
10259 return sched_asym(env->sd, env->dst_cpu, group->asym_prefer_cpu); in sched_group_asym()
10269 return (sg1->flags & SD_SHARE_CPUCAPACITY) != in smt_vs_nonsmt_groups()
10270 (sg2->flags & SD_SHARE_CPUCAPACITY); in smt_vs_nonsmt_groups()
10276 if (!env->idle) in smt_balance()
10285 if (group->flags & SD_SHARE_CPUCAPACITY && in smt_balance()
10286 sgs->sum_h_nr_running > 1) in smt_balance()
10300 if (!env->idle || !busiest->sum_nr_running) in sibling_imbalance()
10303 ncores_busiest = sds->busiest->cores; in sibling_imbalance()
10304 ncores_local = sds->local->cores; in sibling_imbalance()
10307 imbalance = busiest->sum_nr_running; in sibling_imbalance()
10308 lsub_positive(&imbalance, local->sum_nr_running); in sibling_imbalance()
10313 imbalance = ncores_local * busiest->sum_nr_running; in sibling_imbalance()
10314 lsub_positive(&imbalance, ncores_busiest * local->sum_nr_running); in sibling_imbalance()
10320 if (imbalance <= 1 && local->sum_nr_running == 0 && in sibling_imbalance()
10321 busiest->sum_nr_running > 1) in sibling_imbalance()
10334 if (rq->cfs.h_nr_runnable != 1) in sched_reduced_capacity()
10341 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
10343 * @sds: Load-balancing data with statistics of the local group.
10356 int i, nr_running, local_group, sd_flags = env->sd->flags; in update_sg_lb_stats()
10357 bool balancing_at_rd = !env->sd->parent; in update_sg_lb_stats()
10361 local_group = group == sds->local; in update_sg_lb_stats()
10363 for_each_cpu_and(i, sched_group_span(group), env->cpus) { in update_sg_lb_stats()
10367 sgs->group_load += load; in update_sg_lb_stats()
10368 sgs->group_util += cpu_util_cfs(i); in update_sg_lb_stats()
10369 sgs->group_runnable += cpu_runnable(rq); in update_sg_lb_stats()
10370 sgs->sum_h_nr_running += rq->cfs.h_nr_runnable; in update_sg_lb_stats()
10372 nr_running = rq->nr_running; in update_sg_lb_stats()
10373 sgs->sum_nr_running += nr_running; in update_sg_lb_stats()
10382 sgs->idle_cpus++; in update_sg_lb_stats()
10394 sgs->nr_numa_running += rq->nr_numa_running; in update_sg_lb_stats()
10395 sgs->nr_preferred_running += rq->nr_preferred_running; in update_sg_lb_stats()
10403 if (sgs->group_misfit_task_load < rq->misfit_task_load) { in update_sg_lb_stats()
10404 sgs->group_misfit_task_load = rq->misfit_task_load; in update_sg_lb_stats()
10407 } else if (env->idle && sched_reduced_capacity(rq, env->sd)) { in update_sg_lb_stats()
10409 if (sgs->group_misfit_task_load < load) in update_sg_lb_stats()
10410 sgs->group_misfit_task_load = load; in update_sg_lb_stats()
10414 sgs->group_capacity = group->sgc->capacity; in update_sg_lb_stats()
10416 sgs->group_weight = group->group_weight; in update_sg_lb_stats()
10419 if (!local_group && env->idle && sgs->sum_h_nr_running && in update_sg_lb_stats()
10421 sgs->group_asym_packing = 1; in update_sg_lb_stats()
10425 sgs->group_smt_balance = 1; in update_sg_lb_stats()
10427 sgs->group_type = group_classify(env->sd->imbalance_pct, group, sgs); in update_sg_lb_stats()
10430 if (sgs->group_type == group_overloaded) in update_sg_lb_stats()
10431 sgs->avg_load = (sgs->group_load * SCHED_CAPACITY_SCALE) / in update_sg_lb_stats()
10432 sgs->group_capacity; in update_sg_lb_stats()
10436 * update_sd_pick_busiest - return 1 on busiest group
10453 struct sg_lb_stats *busiest = &sds->busiest_stat; in update_sd_pick_busiest()
10456 if (!sgs->sum_h_nr_running) in update_sd_pick_busiest()
10465 if ((env->sd->flags & SD_ASYM_CPUCAPACITY) && in update_sd_pick_busiest()
10466 (sgs->group_type == group_misfit_task) && in update_sd_pick_busiest()
10467 (!capacity_greater(capacity_of(env->dst_cpu), sg->sgc->max_capacity) || in update_sd_pick_busiest()
10468 sds->local_stat.group_type != group_has_spare)) in update_sd_pick_busiest()
10471 if (sgs->group_type > busiest->group_type) in update_sd_pick_busiest()
10474 if (sgs->group_type < busiest->group_type) in update_sd_pick_busiest()
10482 switch (sgs->group_type) { in update_sd_pick_busiest()
10485 return sgs->avg_load > busiest->avg_load; in update_sd_pick_busiest()
10496 return sched_asym_prefer(sds->busiest->asym_prefer_cpu, sg->asym_prefer_cpu); in update_sd_pick_busiest()
10503 return sgs->group_misfit_task_load > busiest->group_misfit_task_load; in update_sd_pick_busiest()
10510 if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) in update_sd_pick_busiest()
10528 if (sgs->avg_load < busiest->avg_load) in update_sd_pick_busiest()
10531 if (sgs->avg_load == busiest->avg_load) { in update_sd_pick_busiest()
10533 * SMT sched groups need more help than non-SMT groups. in update_sd_pick_busiest()
10536 if (sds->busiest->flags & SD_SHARE_CPUCAPACITY) in update_sd_pick_busiest()
10548 if (smt_vs_nonsmt_groups(sds->busiest, sg)) { in update_sd_pick_busiest()
10549 if (sg->flags & SD_SHARE_CPUCAPACITY && sgs->sum_h_nr_running <= 1) in update_sd_pick_busiest()
10563 if (sgs->idle_cpus > busiest->idle_cpus) in update_sd_pick_busiest()
10565 else if ((sgs->idle_cpus == busiest->idle_cpus) && in update_sd_pick_busiest()
10566 (sgs->sum_nr_running <= busiest->sum_nr_running)) in update_sd_pick_busiest()
10574 * per-CPU capacity. Migrating tasks to less capable CPUs may harm in update_sd_pick_busiest()
10578 if ((env->sd->flags & SD_ASYM_CPUCAPACITY) && in update_sd_pick_busiest()
10579 (sgs->group_type <= group_fully_busy) && in update_sd_pick_busiest()
10580 (capacity_greater(sg->sgc->min_capacity, capacity_of(env->dst_cpu)))) in update_sd_pick_busiest()
10589 if (sgs->sum_h_nr_running > sgs->nr_numa_running) in fbq_classify_group()
10591 if (sgs->sum_h_nr_running > sgs->nr_preferred_running) in fbq_classify_group()
10598 if (rq->nr_running > rq->nr_numa_running) in fbq_classify_rq()
10600 if (rq->nr_running > rq->nr_preferred_running) in fbq_classify_rq()
10620 * task_running_on_cpu - return 1 if @p is running on @cpu.
10626 if (cpu != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in task_running_on_cpu()
10636 * idle_cpu_without - would a given CPU be idle without p ?
10646 if (rq->curr != rq->idle && rq->curr != p) in idle_cpu_without()
10650 * rq->nr_running can't be used but an updated version without the in idle_cpu_without()
10655 if (rq->ttwu_pending) in idle_cpu_without()
10662 * update_sg_wakeup_stats - Update sched_group's statistics for wakeup.
10678 if (sd->flags & SD_ASYM_CPUCAPACITY) in update_sg_wakeup_stats()
10679 sgs->group_misfit_task_load = 1; in update_sg_wakeup_stats()
10685 sgs->group_load += cpu_load_without(rq, p); in update_sg_wakeup_stats()
10686 sgs->group_util += cpu_util_without(i, p); in update_sg_wakeup_stats()
10687 sgs->group_runnable += cpu_runnable_without(rq, p); in update_sg_wakeup_stats()
10689 sgs->sum_h_nr_running += rq->cfs.h_nr_runnable - local; in update_sg_wakeup_stats()
10691 nr_running = rq->nr_running - local; in update_sg_wakeup_stats()
10692 sgs->sum_nr_running += nr_running; in update_sg_wakeup_stats()
10698 sgs->idle_cpus++; in update_sg_wakeup_stats()
10701 if (sd->flags & SD_ASYM_CPUCAPACITY && in update_sg_wakeup_stats()
10702 sgs->group_misfit_task_load && in update_sg_wakeup_stats()
10704 sgs->group_misfit_task_load = 0; in update_sg_wakeup_stats()
10708 sgs->group_capacity = group->sgc->capacity; in update_sg_wakeup_stats()
10710 sgs->group_weight = group->group_weight; in update_sg_wakeup_stats()
10712 sgs->group_type = group_classify(sd->imbalance_pct, group, sgs); in update_sg_wakeup_stats()
10718 if (sgs->group_type == group_fully_busy || in update_sg_wakeup_stats()
10719 sgs->group_type == group_overloaded) in update_sg_wakeup_stats()
10720 sgs->avg_load = (sgs->group_load * SCHED_CAPACITY_SCALE) / in update_sg_wakeup_stats()
10721 sgs->group_capacity; in update_sg_wakeup_stats()
10729 if (sgs->group_type < idlest_sgs->group_type) in update_pick_idlest()
10732 if (sgs->group_type > idlest_sgs->group_type) in update_pick_idlest()
10740 switch (sgs->group_type) { in update_pick_idlest()
10744 if (idlest_sgs->avg_load <= sgs->avg_load) in update_pick_idlest()
10756 if (idlest->sgc->max_capacity >= group->sgc->max_capacity) in update_pick_idlest()
10762 if (idlest_sgs->idle_cpus > sgs->idle_cpus) in update_pick_idlest()
10766 if (idlest_sgs->idle_cpus == sgs->idle_cpus && in update_pick_idlest()
10767 idlest_sgs->group_util <= sgs->group_util) in update_pick_idlest()
10785 struct sched_group *idlest = NULL, *local = NULL, *group = sd->groups; in sched_balance_find_dst_group()
10799 p->cpus_ptr)) in sched_balance_find_dst_group()
10823 } while (group = group->next, group != sd->groups); in sched_balance_find_dst_group()
10854 (sd->imbalance_pct-100) / 100; in sched_balance_find_dst_group()
10861 * cross-domain, add imbalance to the load on the remote node in sched_balance_find_dst_group()
10865 if ((sd->flags & SD_NUMA) && in sched_balance_find_dst_group()
10876 if (100 * local_sgs.avg_load <= sd->imbalance_pct * idlest_sgs.avg_load) in sched_balance_find_dst_group()
10888 if (local->sgc->max_capacity >= idlest->sgc->max_capacity) in sched_balance_find_dst_group()
10894 if (sd->flags & SD_NUMA) { in sched_balance_find_dst_group()
10895 int imb_numa_nr = sd->imb_numa_nr; in sched_balance_find_dst_group()
10902 if (cpu_to_node(this_cpu) == p->numa_preferred_nid) in sched_balance_find_dst_group()
10906 if (cpu_to_node(idlest_cpu) == p->numa_preferred_nid) in sched_balance_find_dst_group()
10918 if (p->nr_cpus_allowed != NR_CPUS) { in sched_balance_find_dst_group()
10921 cpumask_and(cpus, sched_group_span(local), p->cpus_ptr); in sched_balance_find_dst_group()
10922 imb_numa_nr = min(cpumask_weight(cpus), sd->imb_numa_nr); in sched_balance_find_dst_group()
10925 imbalance = abs(local_sgs.idle_cpus - idlest_sgs.idle_cpus); in sched_balance_find_dst_group()
10962 if (!sched_feat(SIS_UTIL) || env->idle == CPU_NEWLY_IDLE) in update_idle_cpu_scan()
10965 llc_weight = per_cpu(sd_llc_size, env->dst_cpu); in update_idle_cpu_scan()
10966 if (env->sd->span_weight != llc_weight) in update_idle_cpu_scan()
10969 sd_share = rcu_dereference(per_cpu(sd_llc_shared, env->dst_cpu)); in update_idle_cpu_scan()
10979 * let y = SCHED_CAPACITY_SCALE - p * x^2 [1] in update_idle_cpu_scan()
10997 * y = SCHED_CAPACITY_SCALE - in update_idle_cpu_scan()
11006 pct = env->sd->imbalance_pct; in update_idle_cpu_scan()
11010 y = SCHED_CAPACITY_SCALE - tmp; in update_idle_cpu_scan()
11015 if ((int)y != sd_share->nr_idle_scan) in update_idle_cpu_scan()
11016 WRITE_ONCE(sd_share->nr_idle_scan, (int)y); in update_idle_cpu_scan()
11020 * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
11027 struct sched_group *sg = env->sd->groups; in update_sd_lb_stats()
11028 struct sg_lb_stats *local = &sds->local_stat; in update_sd_lb_stats()
11037 local_group = cpumask_test_cpu(env->dst_cpu, sched_group_span(sg)); in update_sd_lb_stats()
11039 sds->local = sg; in update_sd_lb_stats()
11042 if (env->idle != CPU_NEWLY_IDLE || in update_sd_lb_stats()
11043 time_after_eq(jiffies, sg->sgc->next_update)) in update_sd_lb_stats()
11044 update_group_capacity(env->sd, env->dst_cpu); in update_sd_lb_stats()
11050 sds->busiest = sg; in update_sd_lb_stats()
11051 sds->busiest_stat = *sgs; in update_sd_lb_stats()
11055 sds->total_load += sgs->group_load; in update_sd_lb_stats()
11056 sds->total_capacity += sgs->group_capacity; in update_sd_lb_stats()
11058 sum_util += sgs->group_util; in update_sd_lb_stats()
11059 sg = sg->next; in update_sd_lb_stats()
11060 } while (sg != env->sd->groups); in update_sd_lb_stats()
11067 if (sds->busiest) in update_sd_lb_stats()
11068 sds->prefer_sibling = !!(sds->busiest->flags & SD_PREFER_SIBLING); in update_sd_lb_stats()
11071 if (env->sd->flags & SD_NUMA) in update_sd_lb_stats()
11072 env->fbq_type = fbq_classify_group(&sds->busiest_stat); in update_sd_lb_stats()
11074 if (!env->sd->parent) { in update_sd_lb_stats()
11076 set_rd_overloaded(env->dst_rq->rd, sg_overloaded); in update_sd_lb_stats()
11078 /* Update over-utilization (tipping point, U >= 0) indicator */ in update_sd_lb_stats()
11079 set_rd_overutilized(env->dst_rq->rd, sg_overutilized); in update_sd_lb_stats()
11081 set_rd_overutilized(env->dst_rq->rd, sg_overutilized); in update_sd_lb_stats()
11088 * calculate_imbalance - Calculate the amount of imbalance present within the
11097 local = &sds->local_stat; in calculate_imbalance()
11098 busiest = &sds->busiest_stat; in calculate_imbalance()
11100 if (busiest->group_type == group_misfit_task) { in calculate_imbalance()
11101 if (env->sd->flags & SD_ASYM_CPUCAPACITY) { in calculate_imbalance()
11103 env->migration_type = migrate_misfit; in calculate_imbalance()
11104 env->imbalance = 1; in calculate_imbalance()
11110 env->migration_type = migrate_load; in calculate_imbalance()
11111 env->imbalance = busiest->group_misfit_task_load; in calculate_imbalance()
11116 if (busiest->group_type == group_asym_packing) { in calculate_imbalance()
11121 env->migration_type = migrate_task; in calculate_imbalance()
11122 env->imbalance = busiest->sum_h_nr_running; in calculate_imbalance()
11126 if (busiest->group_type == group_smt_balance) { in calculate_imbalance()
11128 env->migration_type = migrate_task; in calculate_imbalance()
11129 env->imbalance = 1; in calculate_imbalance()
11133 if (busiest->group_type == group_imbalanced) { in calculate_imbalance()
11135 * In the group_imb case we cannot rely on group-wide averages in calculate_imbalance()
11136 * to ensure CPU-load equilibrium, try to move any task to fix in calculate_imbalance()
11140 env->migration_type = migrate_task; in calculate_imbalance()
11141 env->imbalance = 1; in calculate_imbalance()
11149 if (local->group_type == group_has_spare) { in calculate_imbalance()
11150 if ((busiest->group_type > group_fully_busy) && in calculate_imbalance()
11151 !(env->sd->flags & SD_SHARE_LLC)) { in calculate_imbalance()
11160 env->migration_type = migrate_util; in calculate_imbalance()
11161 env->imbalance = max(local->group_capacity, local->group_util) - in calculate_imbalance()
11162 local->group_util; in calculate_imbalance()
11171 if (env->idle && env->imbalance == 0) { in calculate_imbalance()
11172 env->migration_type = migrate_task; in calculate_imbalance()
11173 env->imbalance = 1; in calculate_imbalance()
11179 if (busiest->group_weight == 1 || sds->prefer_sibling) { in calculate_imbalance()
11184 env->migration_type = migrate_task; in calculate_imbalance()
11185 env->imbalance = sibling_imbalance(env, sds, busiest, local); in calculate_imbalance()
11192 env->migration_type = migrate_task; in calculate_imbalance()
11193 env->imbalance = max_t(long, 0, in calculate_imbalance()
11194 (local->idle_cpus - busiest->idle_cpus)); in calculate_imbalance()
11199 if (env->sd->flags & SD_NUMA) { in calculate_imbalance()
11200 env->imbalance = adjust_numa_imbalance(env->imbalance, in calculate_imbalance()
11201 local->sum_nr_running + 1, in calculate_imbalance()
11202 env->sd->imb_numa_nr); in calculate_imbalance()
11207 env->imbalance >>= 1; in calculate_imbalance()
11216 if (local->group_type < group_overloaded) { in calculate_imbalance()
11222 local->avg_load = (local->group_load * SCHED_CAPACITY_SCALE) / in calculate_imbalance()
11223 local->group_capacity; in calculate_imbalance()
11229 if (local->avg_load >= busiest->avg_load) { in calculate_imbalance()
11230 env->imbalance = 0; in calculate_imbalance()
11234 sds->avg_load = (sds->total_load * SCHED_CAPACITY_SCALE) / in calculate_imbalance()
11235 sds->total_capacity; in calculate_imbalance()
11241 if (local->avg_load >= sds->avg_load) { in calculate_imbalance()
11242 env->imbalance = 0; in calculate_imbalance()
11256 env->migration_type = migrate_load; in calculate_imbalance()
11257 env->imbalance = min( in calculate_imbalance()
11258 (busiest->avg_load - sds->avg_load) * busiest->group_capacity, in calculate_imbalance()
11259 (sds->avg_load - local->avg_load) * local->group_capacity in calculate_imbalance()
11286 * sched_balance_find_src_group - Returns the busiest group within the sched_domain
11293 * Return: - The busiest group if imbalance exists.
11315 if (busiest->group_type == group_misfit_task) in sched_balance_find_src_group()
11318 if (!is_rd_overutilized(env->dst_rq->rd) && in sched_balance_find_src_group()
11319 rcu_dereference(env->dst_rq->rd->pd)) in sched_balance_find_src_group()
11323 if (busiest->group_type == group_asym_packing) in sched_balance_find_src_group()
11331 if (busiest->group_type == group_imbalanced) in sched_balance_find_src_group()
11339 if (local->group_type > busiest->group_type) in sched_balance_find_src_group()
11346 if (local->group_type == group_overloaded) { in sched_balance_find_src_group()
11351 if (local->avg_load >= busiest->avg_load) in sched_balance_find_src_group()
11362 if (local->avg_load >= sds.avg_load) in sched_balance_find_src_group()
11369 if (100 * busiest->avg_load <= in sched_balance_find_src_group()
11370 env->sd->imbalance_pct * local->avg_load) in sched_balance_find_src_group()
11378 if (sds.prefer_sibling && local->group_type == group_has_spare && in sched_balance_find_src_group()
11382 if (busiest->group_type != group_overloaded) { in sched_balance_find_src_group()
11383 if (!env->idle) { in sched_balance_find_src_group()
11392 if (busiest->group_type == group_smt_balance && in sched_balance_find_src_group()
11398 if (busiest->group_weight > 1 && in sched_balance_find_src_group()
11399 local->idle_cpus <= (busiest->idle_cpus + 1)) { in sched_balance_find_src_group()
11412 if (busiest->sum_h_nr_running == 1) { in sched_balance_find_src_group()
11423 return env->imbalance ? sds.busiest : NULL; in sched_balance_find_src_group()
11426 env->imbalance = 0; in sched_balance_find_src_group()
11431 * sched_balance_find_src_rq - find the busiest runqueue among the CPUs in the group.
11441 for_each_cpu_and(i, sched_group_span(group), env->cpus) { in sched_balance_find_src_rq()
11451 * - regular: there are !numa tasks in sched_balance_find_src_rq()
11452 * - remote: there are numa tasks that run on the 'wrong' node in sched_balance_find_src_rq()
11453 * - all: there is no distinction in sched_balance_find_src_rq()
11468 if (rt > env->fbq_type) in sched_balance_find_src_rq()
11471 nr_running = rq->cfs.h_nr_runnable; in sched_balance_find_src_rq()
11479 * eventually lead to active_balancing high->low capacity. in sched_balance_find_src_rq()
11480 * Higher per-CPU capacity is considered better than balancing in sched_balance_find_src_rq()
11483 if (env->sd->flags & SD_ASYM_CPUCAPACITY && in sched_balance_find_src_rq()
11484 !capacity_greater(capacity_of(env->dst_cpu), capacity) && in sched_balance_find_src_rq()
11495 if (sched_asym(env->sd, i, env->dst_cpu) && nr_running == 1) in sched_balance_find_src_rq()
11498 switch (env->migration_type) { in sched_balance_find_src_rq()
11506 if (nr_running == 1 && load > env->imbalance && in sched_balance_find_src_rq()
11507 !check_cpu_capacity(rq, env->sd)) in sched_balance_find_src_rq()
11559 if (rq->misfit_task_load > busiest_load) { in sched_balance_find_src_rq()
11560 busiest_load = rq->misfit_task_load; in sched_balance_find_src_rq()
11591 return env->idle && sched_use_asym_prio(env->sd, env->dst_cpu) && in asym_active_balance()
11592 (sched_asym_prefer(env->dst_cpu, env->src_cpu) || in asym_active_balance()
11593 !sched_use_asym_prio(env->sd, env->src_cpu)); in asym_active_balance()
11599 struct sched_domain *sd = env->sd; in imbalanced_active_balance()
11606 if ((env->migration_type == migrate_task) && in imbalanced_active_balance()
11607 (sd->nr_balance_failed > sd->cache_nice_tries+2)) in imbalanced_active_balance()
11615 struct sched_domain *sd = env->sd; in need_active_balance()
11629 if (env->idle && in need_active_balance()
11630 (env->src_rq->cfs.h_nr_runnable == 1)) { in need_active_balance()
11631 if ((check_cpu_capacity(env->src_rq, sd)) && in need_active_balance()
11632 (capacity_of(env->src_cpu)*sd->imbalance_pct < capacity_of(env->dst_cpu)*100)) in need_active_balance()
11636 if (env->migration_type == migrate_misfit) in need_active_balance()
11647 struct sched_group *sg = env->sd->groups; in should_we_balance()
11648 int cpu, idle_smt = -1; in should_we_balance()
11654 if (!cpumask_test_cpu(env->dst_cpu, env->cpus)) in should_we_balance()
11664 if (env->idle == CPU_NEWLY_IDLE) { in should_we_balance()
11665 if (env->dst_rq->nr_running > 0 || env->dst_rq->ttwu_pending) in should_we_balance()
11672 for_each_cpu_and(cpu, swb_cpus, env->cpus) { in should_we_balance()
11681 if (!(env->sd->flags & SD_SHARE_CPUCAPACITY) && !is_core_idle(cpu)) { in should_we_balance()
11682 if (idle_smt == -1) in should_we_balance()
11696 * Are we the first idle core in a non-SMT domain or higher, in should_we_balance()
11699 return cpu == env->dst_cpu; in should_we_balance()
11703 if (idle_smt != -1) in should_we_balance()
11704 return idle_smt == env->dst_cpu; in should_we_balance()
11707 return group_balance_cpu(sg) == env->dst_cpu; in should_we_balance()
11716 switch (env->migration_type) { in update_lb_imbalance_stat()
11718 __schedstat_add(sd->lb_imbalance_load[idle], env->imbalance); in update_lb_imbalance_stat()
11721 __schedstat_add(sd->lb_imbalance_util[idle], env->imbalance); in update_lb_imbalance_stat()
11724 __schedstat_add(sd->lb_imbalance_task[idle], env->imbalance); in update_lb_imbalance_stat()
11727 __schedstat_add(sd->lb_imbalance_misfit[idle], env->imbalance); in update_lb_imbalance_stat()
11741 struct sched_domain *sd_parent = sd->parent; in sched_balance_rq()
11750 .dst_grpmask = group_balance_mask(sd->groups), in sched_balance_rq()
11760 schedstat_inc(sd->lb_count[idle]); in sched_balance_rq()
11770 schedstat_inc(sd->lb_nobusyg[idle]); in sched_balance_rq()
11776 schedstat_inc(sd->lb_nobusyq[idle]); in sched_balance_rq()
11784 env.src_cpu = busiest->cpu; in sched_balance_rq()
11790 if (busiest->nr_running > 1) { in sched_balance_rq()
11793 * an imbalance but busiest->nr_running <= 1, the group is in sched_balance_rq()
11797 env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running); in sched_balance_rq()
11804 * cur_ld_moved - load moved in current iteration in sched_balance_rq()
11805 * ld_moved - cumulative load moved across iterations in sched_balance_rq()
11812 * unlock busiest->lock, and we are able to be sure in sched_balance_rq()
11841 * nohz-idle), we now have balance_cpu in a position to move in sched_balance_rq()
11852 /* Prevent to re-select dst_cpu via env's CPUs */ in sched_balance_rq()
11872 int *group_imbalance = &sd_parent->groups->sgc->imbalance; in sched_balance_rq()
11899 schedstat_inc(sd->lb_failed[idle]); in sched_balance_rq()
11911 sd->nr_balance_failed++; in sched_balance_rq()
11923 if (!cpumask_test_cpu(this_cpu, busiest->curr->cpus_ptr)) { in sched_balance_rq()
11932 * ->active_balance synchronizes accesses to in sched_balance_rq()
11933 * ->active_balance_work. Once set, it's cleared in sched_balance_rq()
11936 if (!busiest->active_balance) { in sched_balance_rq()
11937 busiest->active_balance = 1; in sched_balance_rq()
11938 busiest->push_cpu = this_cpu; in sched_balance_rq()
11947 &busiest->active_balance_work); in sched_balance_rq()
11952 sd->nr_balance_failed = 0; in sched_balance_rq()
11957 sd->balance_interval = sd->min_interval; in sched_balance_rq()
11969 int *group_imbalance = &sd_parent->groups->sgc->imbalance; in sched_balance_rq()
11981 schedstat_inc(sd->lb_balanced[idle]); in sched_balance_rq()
11983 sd->nr_balance_failed = 0; in sched_balance_rq()
12004 sd->balance_interval < MAX_PINNED_INTERVAL) || in sched_balance_rq()
12005 sd->balance_interval < sd->max_interval) in sched_balance_rq()
12006 sd->balance_interval *= 2; in sched_balance_rq()
12014 unsigned long interval = sd->balance_interval; in get_sd_balance_interval()
12017 interval *= sd->busy_factor; in get_sd_balance_interval()
12028 interval -= 1; in get_sd_balance_interval()
12042 next = sd->last_balance + interval; in update_next_balance()
12058 int target_cpu = busiest_rq->push_cpu; in active_load_balance_cpu_stop()
12066 * Between queueing the stop-work and running it is a hole in which in active_load_balance_cpu_stop()
12075 !busiest_rq->active_balance)) in active_load_balance_cpu_stop()
12079 if (busiest_rq->nr_running <= 1) in active_load_balance_cpu_stop()
12085 * Bjorn Helgaas on a 128-CPU setup. in active_load_balance_cpu_stop()
12101 .src_cpu = busiest_rq->cpu, in active_load_balance_cpu_stop()
12107 schedstat_inc(sd->alb_count); in active_load_balance_cpu_stop()
12112 schedstat_inc(sd->alb_pushed); in active_load_balance_cpu_stop()
12114 sd->nr_balance_failed = 0; in active_load_balance_cpu_stop()
12116 schedstat_inc(sd->alb_failed); in active_load_balance_cpu_stop()
12121 busiest_rq->active_balance = 0; in active_load_balance_cpu_stop()
12133 * This flag serializes load-balancing passes over large domains
12134 * (above the NODE topology level) - only one load-balancing instance
12138 * - Note that load-balancing passes triggered while another one
12139 * is executing are skipped and not re-tried.
12141 * - Also note that this does not serialize rebalance_domains()
12142 * execution, as non-SD_SERIALIZE domains will still be
12143 * load-balanced in parallel.
12149 * This trades load-balance latency on larger machines for less cross talk.
12158 if (cost > sd->max_newidle_lb_cost) { in update_newidle_cost()
12163 sd->max_newidle_lb_cost = cost; in update_newidle_cost()
12164 sd->last_decay_max_lb_cost = jiffies; in update_newidle_cost()
12165 } else if (time_after(jiffies, sd->last_decay_max_lb_cost + HZ)) { in update_newidle_cost()
12171 sd->max_newidle_lb_cost = (sd->max_newidle_lb_cost * 253) / 256; in update_newidle_cost()
12172 sd->last_decay_max_lb_cost = jiffies; in update_newidle_cost()
12189 int cpu = rq->cpu; in sched_balance_domains()
12206 max_cost += sd->max_newidle_lb_cost; in sched_balance_domains()
12221 need_serialize = sd->flags & SD_SERIALIZE; in sched_balance_domains()
12227 if (time_after_eq(jiffies, sd->last_balance + interval)) { in sched_balance_domains()
12231 * env->dst_cpu, so we can't know our idle in sched_balance_domains()
12237 sd->last_balance = jiffies; in sched_balance_domains()
12243 if (time_after(next_balance, sd->last_balance + interval)) { in sched_balance_domains()
12244 next_balance = sd->last_balance + interval; in sched_balance_domains()
12250 * Ensure the rq-wide value also decays but keep it at a in sched_balance_domains()
12251 * reasonable floor to avoid funnies with rq->avg_idle. in sched_balance_domains()
12253 rq->max_idle_balance_cost = in sched_balance_domains()
12264 rq->next_balance = next_balance; in sched_balance_domains()
12270 return unlikely(!rcu_dereference_sched(rq->sd)); in on_null_domain()
12277 * - When one of the busy CPUs notices that there may be an idle rebalancing
12297 return -1; in find_new_ilb()
12301 * Kick a CPU to do the NOHZ balancing, if it is time for it, via a cross-CPU
12342 smp_call_function_single_async(ilb_cpu, &cpu_rq(ilb_cpu)->nohz_csd); in kick_ilb()
12354 int nr_busy, i, cpu = rq->cpu; in nohz_balancer_kick()
12357 if (unlikely(rq->idle_balance)) in nohz_balancer_kick()
12380 if (rq->nr_running >= 2) { in nohz_balancer_kick()
12387 sd = rcu_dereference(rq->sd); in nohz_balancer_kick()
12393 if (rq->cfs.h_nr_runnable >= 1 && check_cpu_capacity(rq, sd)) { in nohz_balancer_kick()
12442 * increase the overall cache utilization), we need a less-loaded LLC in nohz_balancer_kick()
12446 * the others are - so just get a NOHZ balance going if it looks in nohz_balancer_kick()
12449 nr_busy = atomic_read(&sds->nr_busy_cpus); in nohz_balancer_kick()
12472 if (!sd || !sd->nohz_idle) in set_cpu_sd_state_busy()
12474 sd->nohz_idle = 0; in set_cpu_sd_state_busy()
12476 atomic_inc(&sd->shared->nr_busy_cpus); in set_cpu_sd_state_busy()
12485 if (likely(!rq->nohz_tick_stopped)) in nohz_balance_exit_idle()
12488 rq->nohz_tick_stopped = 0; in nohz_balance_exit_idle()
12489 cpumask_clear_cpu(rq->cpu, nohz.idle_cpus_mask); in nohz_balance_exit_idle()
12492 set_cpu_sd_state_busy(rq->cpu); in nohz_balance_exit_idle()
12502 if (!sd || sd->nohz_idle) in set_cpu_sd_state_idle()
12504 sd->nohz_idle = 1; in set_cpu_sd_state_idle()
12506 atomic_dec(&sd->shared->nr_busy_cpus); in set_cpu_sd_state_idle()
12526 * Can be set safely without rq->lock held in nohz_balance_enter_idle()
12528 * rq->lock is held during the check and the clear in nohz_balance_enter_idle()
12530 rq->has_blocked_load = 1; in nohz_balance_enter_idle()
12538 if (rq->nohz_tick_stopped) in nohz_balance_enter_idle()
12545 rq->nohz_tick_stopped = 1; in nohz_balance_enter_idle()
12563 * enable the periodic update of the load of idle CPUs in nohz_balance_enter_idle()
12570 unsigned int cpu = rq->cpu; in update_nohz_stats()
12572 if (!rq->has_blocked_load) in update_nohz_stats()
12578 if (!time_after(jiffies, READ_ONCE(rq->last_blocked_load_update_tick))) in update_nohz_stats()
12583 return rq->has_blocked_load; in update_nohz_stats()
12598 int this_cpu = this_rq->cpu; in _nohz_idle_balance()
12655 if (time_after_eq(jiffies, rq->next_balance)) { in _nohz_idle_balance()
12666 if (time_after(next_balance, rq->next_balance)) { in _nohz_idle_balance()
12667 next_balance = rq->next_balance; in _nohz_idle_balance()
12685 /* There is still blocked load, enable periodic update */ in _nohz_idle_balance()
12696 unsigned int flags = this_rq->nohz_idle_balance; in nohz_idle_balance()
12701 this_rq->nohz_idle_balance = 0; in nohz_idle_balance()
12742 int this_cpu = this_rq->cpu; in nohz_newidle_balance()
12745 if (this_rq->avg_idle < sysctl_sched_migration_cost) in nohz_newidle_balance()
12776 * < 0 - we released the lock and there are !fair tasks present
12777 * 0 - failed, no new tasks
12778 * > 0 - success, new (fair) tasks present
12783 int this_cpu = this_rq->cpu; in sched_balance_newidle()
12795 if (this_rq->ttwu_pending) in sched_balance_newidle()
12803 this_rq->idle_stamp = rq_clock(this_rq); in sched_balance_newidle()
12813 * for load-balance and preemption/IRQs are still disabled avoiding in sched_balance_newidle()
12815 * re-start the picking loop. in sched_balance_newidle()
12820 sd = rcu_dereference_check_sched_domain(this_rq->sd); in sched_balance_newidle()
12822 if (!get_rd_overloaded(this_rq->rd) || in sched_balance_newidle()
12823 (sd && this_rq->avg_idle < sd->max_newidle_lb_cost)) { in sched_balance_newidle()
12844 if (this_rq->avg_idle < curr_cost + sd->max_newidle_lb_cost) in sched_balance_newidle()
12847 if (sd->flags & SD_BALANCE_NEWIDLE) { in sched_balance_newidle()
12854 domain_cost = t1 - t0; in sched_balance_newidle()
12872 if (curr_cost > this_rq->max_idle_balance_cost) in sched_balance_newidle()
12873 this_rq->max_idle_balance_cost = curr_cost; in sched_balance_newidle()
12876 * While browsing the domains, we released the rq lock, a task could in sched_balance_newidle()
12880 if (this_rq->cfs.h_nr_queued && !pulled_task) in sched_balance_newidle()
12884 if (this_rq->nr_running != this_rq->cfs.h_nr_queued) in sched_balance_newidle()
12885 pulled_task = -1; in sched_balance_newidle()
12889 if (time_after(this_rq->next_balance, next_balance)) in sched_balance_newidle()
12890 this_rq->next_balance = next_balance; in sched_balance_newidle()
12893 this_rq->idle_stamp = 0; in sched_balance_newidle()
12905 * - directly from the local sched_tick() for periodic load balancing
12907 * - indirectly from a remote sched_tick() for NOHZ idle balancing
12908 * through the SMP cross-call nohz_csd_func()
12913 enum cpu_idle_type idle = this_rq->idle_balance; in sched_balance_softirq()
12926 sched_balance_update_blocked_averages(this_rq->cpu); in sched_balance_softirq()
12942 if (time_after_eq(jiffies, rq->next_balance)) in sched_balance_trigger()
12972 u64 rtime = se->sum_exec_runtime - se->prev_sum_exec_runtime; in __entity_slice_used()
12973 u64 slice = se->slice; in __entity_slice_used()
12995 * MIN_NR_TASKS_DURING_FORCEIDLE - 1 tasks and use that to check in task_tick_core()
12998 if (rq->core->core_forceidle_count && rq->cfs.nr_queued == 1 && in task_tick_core()
12999 __entity_slice_used(&curr->se, MIN_NR_TASKS_DURING_FORCEIDLE)) in task_tick_core()
13004 * se_fi_update - Update the cfs_rq->min_vruntime_fi in a CFS hierarchy if needed.
13013 if (cfs_rq->forceidle_seq == fi_seq) in se_fi_update()
13015 cfs_rq->forceidle_seq = fi_seq; in se_fi_update()
13018 cfs_rq->min_vruntime_fi = cfs_rq->min_vruntime; in se_fi_update()
13024 struct sched_entity *se = &p->se; in task_vruntime_update()
13026 if (p->sched_class != &fair_sched_class) in task_vruntime_update()
13029 se_fi_update(se, rq->core->core_forceidle_seq, in_fi); in task_vruntime_update()
13036 const struct sched_entity *sea = &a->se; in cfs_prio_less()
13037 const struct sched_entity *seb = &b->se; in cfs_prio_less()
13042 WARN_ON_ONCE(task_rq(b)->core != rq->core); in cfs_prio_less()
13049 while (sea->cfs_rq->tg != seb->cfs_rq->tg) { in cfs_prio_less()
13050 int sea_depth = sea->depth; in cfs_prio_less()
13051 int seb_depth = seb->depth; in cfs_prio_less()
13059 se_fi_update(sea, rq->core->core_forceidle_seq, in_fi); in cfs_prio_less()
13060 se_fi_update(seb, rq->core->core_forceidle_seq, in_fi); in cfs_prio_less()
13062 cfs_rqa = sea->cfs_rq; in cfs_prio_less()
13063 cfs_rqb = seb->cfs_rq; in cfs_prio_less()
13065 cfs_rqa = &task_rq(a)->cfs; in cfs_prio_less()
13066 cfs_rqb = &task_rq(b)->cfs; in cfs_prio_less()
13074 delta = (s64)(sea->vruntime - seb->vruntime) + in cfs_prio_less()
13075 (s64)(cfs_rqb->min_vruntime_fi - cfs_rqa->min_vruntime_fi); in cfs_prio_less()
13085 cfs_rq = task_group(p)->cfs_rq[cpu]; in task_is_throttled_fair()
13087 cfs_rq = &cpu_rq(cpu)->cfs; in task_is_throttled_fair()
13106 struct sched_entity *se = &curr->se; in task_tick_fair()
13124 * - child not yet on the tasklist
13125 * - preemption disabled
13142 if (rq->cfs.nr_queued == 1) in prio_changed_fair()
13151 if (p->prio > oldprio) in prio_changed_fair()
13173 se = se->parent; in propagate_entity_cfs_rq()
13198 * - A forked task which hasn't been woken up by wake_up_new_task(). in detach_entity_cfs_rq()
13199 * - A task which has been woken up by try_to_wake_up() but is in detach_entity_cfs_rq()
13202 if (!se->avg.last_update_time) in detach_entity_cfs_rq()
13226 struct sched_entity *se = &p->se; in detach_task_cfs_rq()
13233 struct sched_entity *se = &p->se; in attach_task_cfs_rq()
13245 WARN_ON_ONCE(p->se.sched_delayed); in switched_to_fair()
13266 struct sched_entity *se = &p->se; in __set_next_task_fair()
13274 list_move(&se->group_node, &rq->cfs_tasks); in __set_next_task_fair()
13280 WARN_ON_ONCE(se->sched_delayed); in __set_next_task_fair()
13292 * This routine is mostly called to set cfs_rq->curr field when a task
13297 struct sched_entity *se = &p->se; in set_next_task_fair()
13312 cfs_rq->tasks_timeline = RB_ROOT_CACHED; in init_cfs_rq()
13313 cfs_rq->min_vruntime = (u64)(-(1LL << 20)); in init_cfs_rq()
13315 raw_spin_lock_init(&cfs_rq->removed.lock); in init_cfs_rq()
13326 if (READ_ONCE(p->__state) == TASK_NEW) in task_change_group_fair()
13332 /* Tell se's cfs_rq has been changed -- migrated */ in task_change_group_fair()
13333 p->se.avg.last_update_time = 0; in task_change_group_fair()
13344 if (tg->cfs_rq) in free_fair_sched_group()
13345 kfree(tg->cfs_rq[i]); in free_fair_sched_group()
13346 if (tg->se) in free_fair_sched_group()
13347 kfree(tg->se[i]); in free_fair_sched_group()
13350 kfree(tg->cfs_rq); in free_fair_sched_group()
13351 kfree(tg->se); in free_fair_sched_group()
13360 tg->cfs_rq = kcalloc(nr_cpu_ids, sizeof(cfs_rq), GFP_KERNEL); in alloc_fair_sched_group()
13361 if (!tg->cfs_rq) in alloc_fair_sched_group()
13363 tg->se = kcalloc(nr_cpu_ids, sizeof(se), GFP_KERNEL); in alloc_fair_sched_group()
13364 if (!tg->se) in alloc_fair_sched_group()
13367 tg->shares = NICE_0_LOAD; in alloc_fair_sched_group()
13383 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]); in alloc_fair_sched_group()
13404 se = tg->se[i]; in online_fair_sched_group()
13420 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu]; in unregister_fair_sched_group()
13421 struct sched_entity *se = tg->se[cpu]; in unregister_fair_sched_group()
13425 if (se->sched_delayed) { in unregister_fair_sched_group()
13427 if (se->sched_delayed) { in unregister_fair_sched_group()
13438 * check on_list without danger of it being re-added. in unregister_fair_sched_group()
13440 if (cfs_rq->on_list) { in unregister_fair_sched_group()
13453 cfs_rq->tg = tg; in init_tg_cfs_entry()
13454 cfs_rq->rq = rq; in init_tg_cfs_entry()
13457 tg->cfs_rq[cpu] = cfs_rq; in init_tg_cfs_entry()
13458 tg->se[cpu] = se; in init_tg_cfs_entry()
13465 se->cfs_rq = &rq->cfs; in init_tg_cfs_entry()
13466 se->depth = 0; in init_tg_cfs_entry()
13468 se->cfs_rq = parent->my_q; in init_tg_cfs_entry()
13469 se->depth = parent->depth + 1; in init_tg_cfs_entry()
13472 se->my_q = cfs_rq; in init_tg_cfs_entry()
13474 update_load_set(&se->load, NICE_0_LOAD); in init_tg_cfs_entry()
13475 se->parent = parent; in init_tg_cfs_entry()
13489 if (!tg->se[0]) in __sched_group_set_shares()
13490 return -EINVAL; in __sched_group_set_shares()
13494 if (tg->shares == shares) in __sched_group_set_shares()
13497 tg->shares = shares; in __sched_group_set_shares()
13500 struct sched_entity *se = tg->se[i]; in __sched_group_set_shares()
13522 ret = -EINVAL; in sched_group_set_shares()
13535 return -EINVAL; in sched_group_set_idle()
13538 return -EINVAL; in sched_group_set_idle()
13542 if (tg->idle == idle) { in sched_group_set_idle()
13547 tg->idle = idle; in sched_group_set_idle()
13551 struct sched_entity *se = tg->se[i]; in sched_group_set_idle()
13552 struct cfs_rq *grp_cfs_rq = tg->cfs_rq[i]; in sched_group_set_idle()
13559 grp_cfs_rq->idle = idle; in sched_group_set_idle()
13563 idle_task_delta = grp_cfs_rq->h_nr_queued - in sched_group_set_idle()
13564 grp_cfs_rq->h_nr_idle; in sched_group_set_idle()
13566 idle_task_delta *= -1; in sched_group_set_idle()
13571 if (!se->on_rq) in sched_group_set_idle()
13574 cfs_rq->h_nr_idle += idle_task_delta; in sched_group_set_idle()
13600 struct sched_entity *se = &task->se; in get_rr_interval_fair()
13607 if (rq->cfs.load.weight) in get_rr_interval_fair()
13608 rr_interval = NS_TO_JIFFIES(se->slice); in get_rr_interval_fair()
13685 ng = rcu_dereference(p->numa_group); in show_numa_stats()
13687 if (p->numa_faults) { in show_numa_stats()
13688 tsf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 0)]; in show_numa_stats()
13689 tpf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 1)]; in show_numa_stats()
13692 gsf = ng->faults[task_faults_idx(NUMA_MEM, node, 0)], in show_numa_stats()
13693 gpf = ng->faults[task_faults_idx(NUMA_MEM, node, 1)]; in show_numa_stats()
13713 INIT_CSD(&cpu_rq(i)->cfsb_csd, __cfsb_csd_unthrottle, cpu_rq(i)); in init_sched_fair_class()
13714 INIT_LIST_HEAD(&cpu_rq(i)->cfsb_csd_list); in init_sched_fair_class()