Lines Matching +full:open +full:- +full:dice
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:
96 return -cpu; in arch_asym_cpu_priority()
116 * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool
167 lw->weight += inc; in update_load_add()
168 lw->inv_weight = 0; in update_load_add()
173 lw->weight -= dec; in update_load_sub()
174 lw->inv_weight = 0; in update_load_sub()
179 lw->weight = w; in update_load_set()
180 lw->inv_weight = 0; in update_load_set()
187 * so pick a second-best guess by going with the log2 of the
235 if (likely(lw->inv_weight)) in __update_inv_weight()
238 w = scale_load_down(lw->weight); in __update_inv_weight()
241 lw->inv_weight = 1; in __update_inv_weight()
243 lw->inv_weight = WMULT_CONST; in __update_inv_weight()
245 lw->inv_weight = WMULT_CONST / w; in __update_inv_weight()
251 * (delta_exec * (weight * lw->inv_weight)) >> WMULT_SHIFT
271 shift -= fs; in __calc_delta()
275 fact = mul_u32_u32(fact, lw->inv_weight); in __calc_delta()
280 shift -= fs; in __calc_delta()
292 if (unlikely(se->load.weight != NICE_0_LOAD)) in calc_delta_fair()
293 delta = __calc_delta(delta, NICE_0_LOAD, &se->load); in calc_delta_fair()
308 for (; se; se = se->parent)
315 if (cfs_rq->on_list) in list_add_leaf_cfs_rq()
316 return rq->tmp_alone_branch == &rq->leaf_cfs_rq_list; in list_add_leaf_cfs_rq()
318 cfs_rq->on_list = 1; in list_add_leaf_cfs_rq()
323 * enqueued. The fact that we always enqueue bottom-up in list_add_leaf_cfs_rq()
329 if (cfs_rq->tg->parent && in list_add_leaf_cfs_rq()
330 cfs_rq->tg->parent->cfs_rq[cpu]->on_list) { in list_add_leaf_cfs_rq()
337 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list, in list_add_leaf_cfs_rq()
338 &(cfs_rq->tg->parent->cfs_rq[cpu]->leaf_cfs_rq_list)); in list_add_leaf_cfs_rq()
344 rq->tmp_alone_branch = &rq->leaf_cfs_rq_list; in list_add_leaf_cfs_rq()
348 if (!cfs_rq->tg->parent) { in list_add_leaf_cfs_rq()
353 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list, in list_add_leaf_cfs_rq()
354 &rq->leaf_cfs_rq_list); in list_add_leaf_cfs_rq()
359 rq->tmp_alone_branch = &rq->leaf_cfs_rq_list; in list_add_leaf_cfs_rq()
369 list_add_rcu(&cfs_rq->leaf_cfs_rq_list, rq->tmp_alone_branch); in list_add_leaf_cfs_rq()
374 rq->tmp_alone_branch = &cfs_rq->leaf_cfs_rq_list; in list_add_leaf_cfs_rq()
380 if (cfs_rq->on_list) { in list_del_leaf_cfs_rq()
387 * to the prev element but it will point to rq->leaf_cfs_rq_list in list_del_leaf_cfs_rq()
390 if (rq->tmp_alone_branch == &cfs_rq->leaf_cfs_rq_list) in list_del_leaf_cfs_rq()
391 rq->tmp_alone_branch = cfs_rq->leaf_cfs_rq_list.prev; in list_del_leaf_cfs_rq()
393 list_del_rcu(&cfs_rq->leaf_cfs_rq_list); in list_del_leaf_cfs_rq()
394 cfs_rq->on_list = 0; in list_del_leaf_cfs_rq()
400 WARN_ON_ONCE(rq->tmp_alone_branch != &rq->leaf_cfs_rq_list); in assert_list_leaf_cfs_rq()
405 list_for_each_entry_safe(cfs_rq, pos, &rq->leaf_cfs_rq_list, \
412 if (se->cfs_rq == pse->cfs_rq) in is_same_group()
413 return se->cfs_rq; in is_same_group()
420 return se->parent; in parent_entity()
436 se_depth = (*se)->depth; in find_matching_se()
437 pse_depth = (*pse)->depth; in find_matching_se()
440 se_depth--; in find_matching_se()
445 pse_depth--; in find_matching_se()
457 return tg->idle > 0; in tg_is_idle()
462 return cfs_rq->idle > 0; in cfs_rq_is_idle()
491 for (cfs_rq = &rq->cfs, pos = NULL; cfs_rq; cfs_rq = pos)
529 s64 delta = (s64)(vruntime - max_vruntime); in max_vruntime()
538 s64 delta = (s64)(vruntime - min_vruntime); in min_vruntime()
552 return (s64)(a->deadline - b->deadline) < 0; in entity_before()
557 return (s64)(se->vruntime - cfs_rq->zero_vruntime); in entity_key()
564 * Compute virtual time from the per-task service numbers:
572 * lag_i = S - s_i = w_i * (V - v_i)
578 * \Sum w_i * (V - v_i) = 0
579 * \Sum w_i * V - w_i * v_i = 0
582 * se->vruntime):
585 * V = -------------- = --------------
592 * virtual time has non-contiguous motion equivalent to:
594 * V +-= lag_i / W
601 * Substitute: v_i == (v_i - v0) + v0
603 * \Sum ((v_i - v0) + v0) * w_i \Sum (v_i - v0) * w_i
604 * V = ---------------------------- = --------------------- + v0
609 * v0 := cfs_rq->zero_vruntime
610 * \Sum (v_i - v0) * w_i := cfs_rq->avg_vruntime
611 * \Sum w_i := cfs_rq->avg_load
613 * Since zero_vruntime closely tracks the per-task service, these
614 * deltas: (v_i - v), will be in the order of the maximal (virtual) lag
624 unsigned long weight = scale_load_down(se->load.weight); in avg_vruntime_add()
627 cfs_rq->avg_vruntime += key * weight; in avg_vruntime_add()
628 cfs_rq->avg_load += weight; in avg_vruntime_add()
634 unsigned long weight = scale_load_down(se->load.weight); in avg_vruntime_sub()
637 cfs_rq->avg_vruntime -= key * weight; in avg_vruntime_sub()
638 cfs_rq->avg_load -= weight; in avg_vruntime_sub()
645 * v' = v + d ==> avg_vruntime' = avg_runtime - d*avg_load in avg_vruntime_update()
647 cfs_rq->avg_vruntime -= cfs_rq->avg_load * delta; in avg_vruntime_update()
656 struct sched_entity *curr = cfs_rq->curr; in avg_vruntime()
657 s64 avg = cfs_rq->avg_vruntime; in avg_vruntime()
658 long load = cfs_rq->avg_load; in avg_vruntime()
660 if (curr && curr->on_rq) { in avg_vruntime()
661 unsigned long weight = scale_load_down(curr->load.weight); in avg_vruntime()
670 avg -= (load - 1); in avg_vruntime()
674 return cfs_rq->zero_vruntime + avg; in avg_vruntime()
678 * lag_i = S - s_i = w_i * (V - v_i)
681 * is possible -- by addition/removal/reweight to the tree -- to move V around
689 * -r_max < lag < max(r_max, q)
697 WARN_ON_ONCE(!se->on_rq); in update_entity_lag()
699 vlag = avg_vruntime(cfs_rq) - se->vruntime; in update_entity_lag()
700 limit = calc_delta_fair(max_t(u64, 2*se->slice, TICK_NSEC), se); in update_entity_lag()
702 se->vlag = clamp(vlag, -limit, limit); in update_entity_lag()
709 * lag_i = S - s_i = w_i*(V - v_i)
711 * lag_i >= 0 -> V >= v_i
713 * \Sum (v_i - v)*w_i
714 * V = ------------------ + v
717 * lag_i >= 0 -> \Sum (v_i - v)*w_i >= (v_i - v)*(\Sum w_i)
719 * Note: using 'avg_vruntime() > se->vruntime' is inaccurate due
724 struct sched_entity *curr = cfs_rq->curr; in vruntime_eligible()
725 s64 avg = cfs_rq->avg_vruntime; in vruntime_eligible()
726 long load = cfs_rq->avg_load; in vruntime_eligible()
728 if (curr && curr->on_rq) { in vruntime_eligible()
729 unsigned long weight = scale_load_down(curr->load.weight); in vruntime_eligible()
735 return avg >= (s64)(vruntime - cfs_rq->zero_vruntime) * load; in vruntime_eligible()
740 return vruntime_eligible(cfs_rq, se->vruntime); in entity_eligible()
746 s64 delta = (s64)(vruntime - cfs_rq->zero_vruntime); in update_zero_vruntime()
750 cfs_rq->zero_vruntime = vruntime; in update_zero_vruntime()
756 struct sched_entity *curr = cfs_rq->curr; in cfs_rq_min_slice()
759 if (curr && curr->on_rq) in cfs_rq_min_slice()
760 min_slice = curr->slice; in cfs_rq_min_slice()
763 min_slice = min(min_slice, root->min_slice); in cfs_rq_min_slice()
773 #define vruntime_gt(field, lse, rse) ({ (s64)((lse)->field - (rse)->field) > 0; })
780 se->min_vruntime = rse->min_vruntime; in __min_vruntime_update()
788 if (rse->min_slice < se->min_slice) in __min_slice_update()
789 se->min_slice = rse->min_slice; in __min_slice_update()
794 * se->min_vruntime = min(se->vruntime, {left,right}->min_vruntime)
798 u64 old_min_vruntime = se->min_vruntime; in min_vruntime_update()
799 u64 old_min_slice = se->min_slice; in min_vruntime_update()
800 struct rb_node *node = &se->run_node; in min_vruntime_update()
802 se->min_vruntime = se->vruntime; in min_vruntime_update()
803 __min_vruntime_update(se, node->rb_right); in min_vruntime_update()
804 __min_vruntime_update(se, node->rb_left); in min_vruntime_update()
806 se->min_slice = se->slice; in min_vruntime_update()
807 __min_slice_update(se, node->rb_right); in min_vruntime_update()
808 __min_slice_update(se, node->rb_left); in min_vruntime_update()
810 return se->min_vruntime == old_min_vruntime && in min_vruntime_update()
811 se->min_slice == old_min_slice; in min_vruntime_update()
818 * Enqueue an entity into the rb-tree:
824 se->min_vruntime = se->vruntime; in __enqueue_entity()
825 se->min_slice = se->slice; in __enqueue_entity()
826 rb_add_augmented_cached(&se->run_node, &cfs_rq->tasks_timeline, in __enqueue_entity()
832 rb_erase_augmented_cached(&se->run_node, &cfs_rq->tasks_timeline, in __dequeue_entity()
840 struct rb_node *root = cfs_rq->tasks_timeline.rb_root.rb_node; in __pick_root_entity()
850 struct rb_node *left = rb_first_cached(&cfs_rq->tasks_timeline); in __pick_first_entity()
869 u64 vprot = se->deadline; in set_protect_slice()
874 slice = min(slice, se->slice); in set_protect_slice()
875 if (slice != se->slice) in set_protect_slice()
876 vprot = min_vruntime(vprot, se->vruntime + calc_delta_fair(slice, se)); in set_protect_slice()
878 se->vprot = vprot; in set_protect_slice()
885 se->vprot = min_vruntime(se->vprot, se->vruntime + calc_delta_fair(slice, se)); in update_protect_slice()
890 return ((s64)(se->vprot - se->vruntime) > 0); in protect_slice()
896 se->vprot = se->vruntime; in cancel_protect_slice()
910 * We can do this in O(log n) time due to an augmented RB-tree. The
914 * se->min_vruntime = min(se->vruntime, se->{left,right}->min_vruntime)
920 struct rb_node *node = cfs_rq->tasks_timeline.rb_root.rb_node; in __pick_eevdf()
922 struct sched_entity *curr = cfs_rq->curr; in __pick_eevdf()
929 if (cfs_rq->nr_queued == 1) in __pick_eevdf()
930 return curr && curr->on_rq ? curr : se; in __pick_eevdf()
933 * Picking the ->next buddy will affect latency but not fairness. in __pick_eevdf()
936 cfs_rq->next && entity_eligible(cfs_rq, cfs_rq->next)) { in __pick_eevdf()
937 /* ->next will never be delayed */ in __pick_eevdf()
938 WARN_ON_ONCE(cfs_rq->next->sched_delayed); in __pick_eevdf()
939 return cfs_rq->next; in __pick_eevdf()
942 if (curr && (!curr->on_rq || !entity_eligible(cfs_rq, curr))) in __pick_eevdf()
956 struct rb_node *left = node->rb_left; in __pick_eevdf()
963 __node_2_se(left)->min_vruntime)) { in __pick_eevdf()
980 node = node->rb_right; in __pick_eevdf()
996 struct rb_node *last = rb_last(&cfs_rq->tasks_timeline.rb_root); in __pick_last_entity()
1027 if ((s64)(se->vruntime - se->deadline) < 0) in update_deadline()
1035 if (!se->custom_slice) in update_deadline()
1036 se->slice = sysctl_sched_base_slice; in update_deadline()
1041 se->deadline = se->vruntime + calc_delta_fair(se->slice, se); in update_deadline()
1058 struct sched_avg *sa = &se->avg; in init_entity_runnable_average()
1069 sa->load_avg = scale_load_down(se->load.weight); in init_entity_runnable_average()
1078 * util_avg = cfs_rq->avg.util_avg / (cfs_rq->avg.load_avg + 1)
1088 * util_avg_cap = (cpu_scale - cfs_rq->avg.util_avg) / 2^n
1103 struct sched_entity *se = &p->se; in post_init_entity_util_avg()
1105 struct sched_avg *sa = &se->avg; in post_init_entity_util_avg()
1107 long cap = (long)(cpu_scale - cfs_rq->avg.util_avg) / 2; in post_init_entity_util_avg()
1109 if (p->sched_class != &fair_sched_class) { in post_init_entity_util_avg()
1120 se->avg.last_update_time = cfs_rq_clock_pelt(cfs_rq); in post_init_entity_util_avg()
1125 if (cfs_rq->avg.util_avg != 0) { in post_init_entity_util_avg()
1126 sa->util_avg = cfs_rq->avg.util_avg * se_weight(se); in post_init_entity_util_avg()
1127 sa->util_avg /= (cfs_rq->avg.load_avg + 1); in post_init_entity_util_avg()
1129 if (sa->util_avg > cap) in post_init_entity_util_avg()
1130 sa->util_avg = cap; in post_init_entity_util_avg()
1132 sa->util_avg = cap; in post_init_entity_util_avg()
1136 sa->runnable_avg = sa->util_avg; in post_init_entity_util_avg()
1144 delta_exec = now - se->exec_start; in update_se()
1148 se->exec_start = now; in update_se()
1151 struct task_struct *running = rq->curr; in update_se()
1154 * task, as w/ proxy-exec they may not be the same. in update_se()
1156 running->se.exec_start = now; in update_se()
1157 running->se.sum_exec_runtime += delta_exec; in update_se()
1166 se->sum_exec_runtime += delta_exec; in update_se()
1173 __schedstat_set(stats->exec_max, in update_se()
1174 max(delta_exec, stats->exec_max)); in update_se()
1187 return update_se(rq, &rq->donor->se); in update_curr_common()
1196 * Note: cfs_rq->curr corresponds to the task picked to in update_curr()
1197 * run (ie: rq->donor.se) which due to proxy-exec may in update_curr()
1199 * (rq->curr.se). This is easy to confuse! in update_curr()
1201 struct sched_entity *curr = cfs_rq->curr; in update_curr()
1213 curr->vruntime += calc_delta_fair(delta_exec, curr); in update_curr()
1221 * - If the task is running on behalf of fair_server, we need in update_curr()
1223 * - Fair task that runs outside of fair_server should account in update_curr()
1227 dl_server_update(&rq->fair_server, delta_exec); in update_curr()
1232 if (cfs_rq->nr_queued == 1) in update_curr()
1243 update_curr(cfs_rq_of(&rq->donor->se)); in update_curr_fair()
1276 * maybe already in the runqueue, the se->statistics.wait_start in update_stats_wait_end_fair()
1280 if (unlikely(!schedstat_val(stats->wait_start))) in update_stats_wait_end_fair()
1307 * Task is being enqueued - update stats:
1319 if (se != cfs_rq->curr) in update_stats_enqueue_fair()
1337 if (se != cfs_rq->curr) in update_stats_dequeue_fair()
1345 state = READ_ONCE(tsk->__state); in update_stats_dequeue_fair()
1347 __schedstat_set(tsk->stats.sleep_start, in update_stats_dequeue_fair()
1350 __schedstat_set(tsk->stats.block_start, in update_stats_dequeue_fair()
1356 * We are picking a new current task - update its stats:
1364 se->exec_start = rq_clock_task(rq_of(cfs_rq)); in update_stats_curr_start()
1456 * ->numa_group (see struct task_struct for locking rules).
1460 return rcu_dereference_check(p->numa_group, p == current || in deref_task_numa_group()
1461 (lockdep_is_held(__rq_lockp(task_rq(p))) && !READ_ONCE(p->on_cpu))); in deref_task_numa_group()
1466 return rcu_dereference_protected(p->numa_group, p == current); in deref_curr_numa_group()
1478 * Calculations based on RSS as non-present and empty pages are skipped in task_nr_scan_windows()
1483 rss = get_mm_rss(p->mm); in task_nr_scan_windows()
1516 ng = rcu_dereference(p->numa_group); in task_scan_start()
1521 period *= refcount_read(&ng->refcount); in task_scan_start()
1546 period *= refcount_read(&ng->refcount); in task_scan_max()
1558 rq->nr_numa_running += (p->numa_preferred_nid != NUMA_NO_NODE); in account_numa_enqueue()
1559 rq->nr_preferred_running += (p->numa_preferred_nid == task_node(p)); in account_numa_enqueue()
1564 rq->nr_numa_running -= (p->numa_preferred_nid != NUMA_NO_NODE); in account_numa_dequeue()
1565 rq->nr_preferred_running -= (p->numa_preferred_nid == task_node(p)); in account_numa_dequeue()
1583 ng = rcu_dereference(p->numa_group); in task_numa_group_id()
1585 gid = ng->gid; in task_numa_group_id()
1604 if (!p->numa_faults) in task_faults()
1607 return p->numa_faults[task_faults_idx(NUMA_MEM, nid, 0)] + in task_faults()
1608 p->numa_faults[task_faults_idx(NUMA_MEM, nid, 1)]; in task_faults()
1618 return ng->faults[task_faults_idx(NUMA_MEM, nid, 0)] + in group_faults()
1619 ng->faults[task_faults_idx(NUMA_MEM, nid, 1)]; in group_faults()
1624 return group->faults[task_faults_idx(NUMA_CPU, nid, 0)] + in group_faults_cpu()
1625 group->faults[task_faults_idx(NUMA_CPU, nid, 1)]; in group_faults_cpu()
1634 faults += ng->faults[task_faults_idx(NUMA_MEM, node, 1)]; in group_faults_priv()
1646 faults += ng->faults[task_faults_idx(NUMA_MEM, node, 0)]; in group_faults_shared()
1654 * considered part of a numa group's pseudo-interleaving set. Migrations
1661 return group_faults_cpu(ng, nid) * ACTIVE_NODE_FRACTION > ng->max_faults_cpu; in numa_is_active_node()
1720 faults *= (max_dist - dist); in score_nearby_nodes()
1721 faults /= (max_dist - LOCAL_DISTANCE); in score_nearby_nodes()
1741 if (!p->numa_faults) in task_weight()
1744 total_faults = p->total_numa_faults; in task_weight()
1764 total_faults = ng->total_faults; in group_weight()
1800 pgdat->node_present_pages >> 4); in pgdat_free_space_enough()
1801 for (z = pgdat->nr_zones - 1; z >= 0; z--) { in pgdat_free_space_enough()
1802 struct zone *zone = pgdat->node_zones + z; in pgdat_free_space_enough()
1822 * hint page fault latency = hint page fault time - scan time
1834 return (time - last_time) & PAGE_ACCESS_TIME_MASK; in numa_hint_fault_latency()
1851 start = pgdat->nbp_rl_start; in numa_promotion_rate_limit()
1852 if (now - start > MSEC_PER_SEC && in numa_promotion_rate_limit()
1853 cmpxchg(&pgdat->nbp_rl_start, start, now) == start) in numa_promotion_rate_limit()
1854 pgdat->nbp_rl_nr_cand = nr_cand; in numa_promotion_rate_limit()
1855 if (nr_cand - pgdat->nbp_rl_nr_cand >= rate_limit) in numa_promotion_rate_limit()
1871 start = pgdat->nbp_th_start; in numa_promotion_adjust_threshold()
1872 if (now - start > th_period && in numa_promotion_adjust_threshold()
1873 cmpxchg(&pgdat->nbp_th_start, start, now) == start) { in numa_promotion_adjust_threshold()
1877 diff_cand = nr_cand - pgdat->nbp_th_nr_cand; in numa_promotion_adjust_threshold()
1879 th = pgdat->nbp_threshold ? : ref_th; in numa_promotion_adjust_threshold()
1881 th = max(th - unit_th, unit_th); in numa_promotion_adjust_threshold()
1884 pgdat->nbp_th_nr_cand = nr_cand; in numa_promotion_adjust_threshold()
1885 pgdat->nbp_threshold = th; in numa_promotion_adjust_threshold()
1915 pgdat->nbp_threshold = 0; in should_numa_migrate_memory()
1924 th = pgdat->nbp_threshold ? : def_th; in should_numa_migrate_memory()
1932 this_cpupid = cpu_pid_to_cpupid(dst_cpu, current->pid); in should_numa_migrate_memory()
1942 * two full passes of the "multi-stage node selection" test that is in should_numa_migrate_memory()
1945 if ((p->numa_preferred_nid == NUMA_NO_NODE || p->numa_scan_seq <= 4) && in should_numa_migrate_memory()
1950 * Multi-stage node selection is used in conjunction with a periodic in should_numa_migrate_memory()
1951 * migration fault to build a temporal task<->page relation. By using in should_numa_migrate_memory()
1952 * a two-stage filter we remove short/unlikely relations. in should_numa_migrate_memory()
1956 * page (n_t) (in a given time-span) to a probability. in should_numa_migrate_memory()
1964 * act on an unlikely task<->page relation. in should_numa_migrate_memory()
1974 /* A shared fault, but p->numa_group has not been set up yet. */ in should_numa_migrate_memory()
1991 * --------------- * - > --------------- in should_numa_migrate_memory()
2053 if ((ns->nr_running > ns->weight) && in numa_classify()
2054 (((ns->compute_capacity * 100) < (ns->util * imbalance_pct)) || in numa_classify()
2055 ((ns->compute_capacity * imbalance_pct) < (ns->runnable * 100)))) in numa_classify()
2058 if ((ns->nr_running < ns->weight) || in numa_classify()
2059 (((ns->compute_capacity * 100) > (ns->util * imbalance_pct)) && in numa_classify()
2060 ((ns->compute_capacity * imbalance_pct) > (ns->runnable * 100)))) in numa_classify()
2101 int cpu, idle_core = -1; in update_numa_stats()
2104 ns->idle_cpu = -1; in update_numa_stats()
2110 ns->load += cpu_load(rq); in update_numa_stats()
2111 ns->runnable += cpu_runnable(rq); in update_numa_stats()
2112 ns->util += cpu_util_cfs(cpu); in update_numa_stats()
2113 ns->nr_running += rq->cfs.h_nr_runnable; in update_numa_stats()
2114 ns->compute_capacity += capacity_of(cpu); in update_numa_stats()
2116 if (find_idle && idle_core < 0 && !rq->nr_running && idle_cpu(cpu)) { in update_numa_stats()
2117 if (READ_ONCE(rq->numa_migrate_on) || in update_numa_stats()
2118 !cpumask_test_cpu(cpu, env->p->cpus_ptr)) in update_numa_stats()
2121 if (ns->idle_cpu == -1) in update_numa_stats()
2122 ns->idle_cpu = cpu; in update_numa_stats()
2129 ns->weight = cpumask_weight(cpumask_of_node(nid)); in update_numa_stats()
2131 ns->node_type = numa_classify(env->imbalance_pct, ns); in update_numa_stats()
2134 ns->idle_cpu = idle_core; in update_numa_stats()
2140 struct rq *rq = cpu_rq(env->dst_cpu); in task_numa_assign()
2142 /* Check if run-queue part of active NUMA balance. */ in task_numa_assign()
2143 if (env->best_cpu != env->dst_cpu && xchg(&rq->numa_migrate_on, 1)) { in task_numa_assign()
2145 int start = env->dst_cpu; in task_numa_assign()
2148 for_each_cpu_wrap(cpu, cpumask_of_node(env->dst_nid), start + 1) { in task_numa_assign()
2149 if (cpu == env->best_cpu || !idle_cpu(cpu) || in task_numa_assign()
2150 !cpumask_test_cpu(cpu, env->p->cpus_ptr)) { in task_numa_assign()
2154 env->dst_cpu = cpu; in task_numa_assign()
2155 rq = cpu_rq(env->dst_cpu); in task_numa_assign()
2156 if (!xchg(&rq->numa_migrate_on, 1)) in task_numa_assign()
2166 * Clear previous best_cpu/rq numa-migrate flag, since task now in task_numa_assign()
2169 if (env->best_cpu != -1 && env->best_cpu != env->dst_cpu) { in task_numa_assign()
2170 rq = cpu_rq(env->best_cpu); in task_numa_assign()
2171 WRITE_ONCE(rq->numa_migrate_on, 0); in task_numa_assign()
2174 if (env->best_task) in task_numa_assign()
2175 put_task_struct(env->best_task); in task_numa_assign()
2179 env->best_task = p; in task_numa_assign()
2180 env->best_imp = imp; in task_numa_assign()
2181 env->best_cpu = env->dst_cpu; in task_numa_assign()
2195 * ------------ vs --------- in load_too_imbalanced()
2198 src_capacity = env->src_stats.compute_capacity; in load_too_imbalanced()
2199 dst_capacity = env->dst_stats.compute_capacity; in load_too_imbalanced()
2201 imb = abs(dst_load * src_capacity - src_load * dst_capacity); in load_too_imbalanced()
2203 orig_src_load = env->src_stats.load; in load_too_imbalanced()
2204 orig_dst_load = env->dst_stats.load; in load_too_imbalanced()
2206 old_imb = abs(orig_dst_load * src_capacity - orig_src_load * dst_capacity); in load_too_imbalanced()
2228 struct numa_group *cur_ng, *p_ng = deref_curr_numa_group(env->p); in task_numa_compare()
2229 struct rq *dst_rq = cpu_rq(env->dst_cpu); in task_numa_compare()
2233 int dist = env->dist; in task_numa_compare()
2238 if (READ_ONCE(dst_rq->numa_migrate_on)) in task_numa_compare()
2242 cur = rcu_dereference(dst_rq->curr); in task_numa_compare()
2243 if (cur && ((cur->flags & (PF_EXITING | PF_KTHREAD)) || in task_numa_compare()
2244 !cur->mm)) in task_numa_compare()
2249 * end try selecting ourselves (current == env->p) as a swap candidate. in task_numa_compare()
2251 if (cur == env->p) { in task_numa_compare()
2257 if (maymove && moveimp >= env->best_imp) in task_numa_compare()
2264 if (!cpumask_test_cpu(env->src_cpu, cur->cpus_ptr)) in task_numa_compare()
2271 if (env->best_task && in task_numa_compare()
2272 env->best_task->numa_preferred_nid == env->src_nid && in task_numa_compare()
2273 cur->numa_preferred_nid != env->src_nid) { in task_numa_compare()
2287 cur_ng = rcu_dereference(cur->numa_group); in task_numa_compare()
2295 if (env->dst_stats.node_type == node_has_spare) in task_numa_compare()
2298 imp = taskimp + task_weight(cur, env->src_nid, dist) - in task_numa_compare()
2299 task_weight(cur, env->dst_nid, dist); in task_numa_compare()
2305 imp -= imp / 16; in task_numa_compare()
2312 imp += group_weight(cur, env->src_nid, dist) - in task_numa_compare()
2313 group_weight(cur, env->dst_nid, dist); in task_numa_compare()
2315 imp += task_weight(cur, env->src_nid, dist) - in task_numa_compare()
2316 task_weight(cur, env->dst_nid, dist); in task_numa_compare()
2320 if (cur->numa_preferred_nid == env->dst_nid) in task_numa_compare()
2321 imp -= imp / 16; in task_numa_compare()
2329 if (cur->numa_preferred_nid == env->src_nid) in task_numa_compare()
2332 if (maymove && moveimp > imp && moveimp > env->best_imp) { in task_numa_compare()
2342 if (env->best_task && cur->numa_preferred_nid == env->src_nid && in task_numa_compare()
2343 env->best_task->numa_preferred_nid != env->src_nid) { in task_numa_compare()
2353 if (imp < SMALLIMP || imp <= env->best_imp + SMALLIMP / 2) in task_numa_compare()
2359 load = task_h_load(env->p) - task_h_load(cur); in task_numa_compare()
2363 dst_load = env->dst_stats.load + load; in task_numa_compare()
2364 src_load = env->src_stats.load - load; in task_numa_compare()
2372 int cpu = env->dst_stats.idle_cpu; in task_numa_compare()
2376 cpu = env->dst_cpu; in task_numa_compare()
2382 if (!idle_cpu(cpu) && env->best_cpu >= 0 && in task_numa_compare()
2383 idle_cpu(env->best_cpu)) { in task_numa_compare()
2384 cpu = env->best_cpu; in task_numa_compare()
2387 env->dst_cpu = cpu; in task_numa_compare()
2397 if (maymove && !cur && env->best_cpu >= 0 && idle_cpu(env->best_cpu)) in task_numa_compare()
2404 if (!maymove && env->best_task && in task_numa_compare()
2405 env->best_task->numa_preferred_nid == env->src_nid) { in task_numa_compare()
2424 if (env->dst_stats.node_type == node_has_spare) { in task_numa_find_cpu()
2434 src_running = env->src_stats.nr_running - 1; in task_numa_find_cpu()
2435 dst_running = env->dst_stats.nr_running + 1; in task_numa_find_cpu()
2436 imbalance = max(0, dst_running - src_running); in task_numa_find_cpu()
2438 env->imb_numa_nr); in task_numa_find_cpu()
2443 if (env->dst_stats.idle_cpu >= 0) { in task_numa_find_cpu()
2444 env->dst_cpu = env->dst_stats.idle_cpu; in task_numa_find_cpu()
2452 * If the improvement from just moving env->p direction is better in task_numa_find_cpu()
2455 load = task_h_load(env->p); in task_numa_find_cpu()
2456 dst_load = env->dst_stats.load + load; in task_numa_find_cpu()
2457 src_load = env->src_stats.load - load; in task_numa_find_cpu()
2461 for_each_cpu(cpu, cpumask_of_node(env->dst_nid)) { in task_numa_find_cpu()
2463 if (!cpumask_test_cpu(cpu, env->p->cpus_ptr)) in task_numa_find_cpu()
2466 env->dst_cpu = cpu; in task_numa_find_cpu()
2484 .best_cpu = -1, in task_numa_migrate()
2498 * random movement of tasks -- counter the numa conditions we're trying in task_numa_migrate()
2504 env.imbalance_pct = 100 + (sd->imbalance_pct - 100) / 2; in task_numa_migrate()
2505 env.imb_numa_nr = sd->imb_numa_nr; in task_numa_migrate()
2517 return -EINVAL; in task_numa_migrate()
2520 env.dst_nid = p->numa_preferred_nid; in task_numa_migrate()
2525 taskimp = task_weight(p, env.dst_nid, dist) - taskweight; in task_numa_migrate()
2526 groupimp = group_weight(p, env.dst_nid, dist) - groupweight; in task_numa_migrate()
2534 * - there is no space available on the preferred_nid in task_numa_migrate()
2535 * - the task is part of a numa_group that is interleaved across in task_numa_migrate()
2540 if (env.best_cpu == -1 || (ng && ng->active_nodes > 1)) { in task_numa_migrate()
2542 if (nid == env.src_nid || nid == p->numa_preferred_nid) in task_numa_migrate()
2553 taskimp = task_weight(p, nid, dist) - taskweight; in task_numa_migrate()
2554 groupimp = group_weight(p, nid, dist) - groupweight; in task_numa_migrate()
2574 if (env.best_cpu == -1) in task_numa_migrate()
2579 if (nid != p->numa_preferred_nid) in task_numa_migrate()
2584 if (env.best_cpu == -1) { in task_numa_migrate()
2585 trace_sched_stick_numa(p, env.src_cpu, NULL, -1); in task_numa_migrate()
2586 return -EAGAIN; in task_numa_migrate()
2592 WRITE_ONCE(best_rq->numa_migrate_on, 0); in task_numa_migrate()
2599 WRITE_ONCE(best_rq->numa_migrate_on, 0); in task_numa_migrate()
2613 if (unlikely(p->numa_preferred_nid == NUMA_NO_NODE || !p->numa_faults)) in numa_migrate_preferred()
2617 interval = min(interval, msecs_to_jiffies(p->numa_scan_period) / 16); in numa_migrate_preferred()
2618 p->numa_migrate_retry = jiffies + interval; in numa_migrate_preferred()
2621 if (task_node(p) == p->numa_preferred_nid) in numa_migrate_preferred()
2651 numa_group->max_faults_cpu = max_faults; in numa_group_count_active_nodes()
2652 numa_group->active_nodes = active_nodes; in numa_group_count_active_nodes()
2678 unsigned long remote = p->numa_faults_locality[0]; in update_task_scan_period()
2679 unsigned long local = p->numa_faults_locality[1]; in update_task_scan_period()
2688 if (local + shared == 0 || p->numa_faults_locality[2]) { in update_task_scan_period()
2689 p->numa_scan_period = min(p->numa_scan_period_max, in update_task_scan_period()
2690 p->numa_scan_period << 1); in update_task_scan_period()
2692 p->mm->numa_next_scan = jiffies + in update_task_scan_period()
2693 msecs_to_jiffies(p->numa_scan_period); in update_task_scan_period()
2704 period_slot = DIV_ROUND_UP(p->numa_scan_period, NUMA_PERIOD_SLOTS); in update_task_scan_period()
2713 int slot = ps_ratio - NUMA_PERIOD_THRESHOLD; in update_task_scan_period()
2723 int slot = lr_ratio - NUMA_PERIOD_THRESHOLD; in update_task_scan_period()
2729 * Private memory faults exceed (SLOTS-THRESHOLD)/SLOTS, in update_task_scan_period()
2734 diff = -(NUMA_PERIOD_THRESHOLD - ratio) * period_slot; in update_task_scan_period()
2737 p->numa_scan_period = clamp(p->numa_scan_period + diff, in update_task_scan_period()
2739 memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); in update_task_scan_period()
2746 * from the dozens-of-seconds NUMA balancing period. Use the scheduler
2753 now = p->se.exec_start; in numa_get_avg_runtime()
2754 runtime = p->se.sum_exec_runtime; in numa_get_avg_runtime()
2756 if (p->last_task_numa_placement) { in numa_get_avg_runtime()
2757 delta = runtime - p->last_sum_exec_runtime; in numa_get_avg_runtime()
2758 *period = now - p->last_task_numa_placement; in numa_get_avg_runtime()
2764 delta = p->se.avg.load_sum; in numa_get_avg_runtime()
2768 p->last_sum_exec_runtime = runtime; in numa_get_avg_runtime()
2769 p->last_task_numa_placement = now; in numa_get_avg_runtime()
2819 for (dist = sched_max_numa_distance; dist > LOCAL_DISTANCE; dist--) { in preferred_group_nid()
2873 * The p->mm->numa_scan_seq field gets updated without in task_numa_placement()
2877 seq = READ_ONCE(p->mm->numa_scan_seq); in task_numa_placement()
2878 if (p->numa_scan_seq == seq) in task_numa_placement()
2880 p->numa_scan_seq = seq; in task_numa_placement()
2881 p->numa_scan_period_max = task_scan_max(p); in task_numa_placement()
2883 total_faults = p->numa_faults_locality[0] + in task_numa_placement()
2884 p->numa_faults_locality[1]; in task_numa_placement()
2890 group_lock = &ng->lock; in task_numa_placement()
2910 diff = p->numa_faults[membuf_idx] - p->numa_faults[mem_idx] / 2; in task_numa_placement()
2911 fault_types[priv] += p->numa_faults[membuf_idx]; in task_numa_placement()
2912 p->numa_faults[membuf_idx] = 0; in task_numa_placement()
2918 * little over-all impact on throughput, and thus their in task_numa_placement()
2922 f_weight = (f_weight * p->numa_faults[cpubuf_idx]) / in task_numa_placement()
2924 f_diff = f_weight - p->numa_faults[cpu_idx] / 2; in task_numa_placement()
2925 p->numa_faults[cpubuf_idx] = 0; in task_numa_placement()
2927 p->numa_faults[mem_idx] += diff; in task_numa_placement()
2928 p->numa_faults[cpu_idx] += f_diff; in task_numa_placement()
2929 faults += p->numa_faults[mem_idx]; in task_numa_placement()
2930 p->total_numa_faults += diff; in task_numa_placement()
2939 ng->faults[mem_idx] += diff; in task_numa_placement()
2940 ng->faults[cpu_idx] += f_diff; in task_numa_placement()
2941 ng->total_faults += diff; in task_numa_placement()
2942 group_faults += ng->faults[mem_idx]; in task_numa_placement()
2957 /* Cannot migrate task to CPU-less node */ in task_numa_placement()
2968 if (max_nid != p->numa_preferred_nid) in task_numa_placement()
2977 return refcount_inc_not_zero(&grp->refcount); in get_numa_group()
2982 if (refcount_dec_and_test(&grp->refcount)) in put_numa_group()
3004 refcount_set(&grp->refcount, 1); in task_numa_group()
3005 grp->active_nodes = 1; in task_numa_group()
3006 grp->max_faults_cpu = 0; in task_numa_group()
3007 spin_lock_init(&grp->lock); in task_numa_group()
3008 grp->gid = p->pid; in task_numa_group()
3011 grp->faults[i] = p->numa_faults[i]; in task_numa_group()
3013 grp->total_faults = p->total_numa_faults; in task_numa_group()
3015 grp->nr_tasks++; in task_numa_group()
3016 rcu_assign_pointer(p->numa_group, grp); in task_numa_group()
3020 tsk = READ_ONCE(cpu_rq(cpu)->curr); in task_numa_group()
3025 grp = rcu_dereference(tsk->numa_group); in task_numa_group()
3037 if (my_grp->nr_tasks > grp->nr_tasks) in task_numa_group()
3041 * Tie-break on the grp address. in task_numa_group()
3043 if (my_grp->nr_tasks == grp->nr_tasks && my_grp > grp) in task_numa_group()
3047 if (tsk->mm == current->mm) in task_numa_group()
3066 double_lock_irq(&my_grp->lock, &grp->lock); in task_numa_group()
3069 my_grp->faults[i] -= p->numa_faults[i]; in task_numa_group()
3070 grp->faults[i] += p->numa_faults[i]; in task_numa_group()
3072 my_grp->total_faults -= p->total_numa_faults; in task_numa_group()
3073 grp->total_faults += p->total_numa_faults; in task_numa_group()
3075 my_grp->nr_tasks--; in task_numa_group()
3076 grp->nr_tasks++; in task_numa_group()
3078 spin_unlock(&my_grp->lock); in task_numa_group()
3079 spin_unlock_irq(&grp->lock); in task_numa_group()
3081 rcu_assign_pointer(p->numa_group, grp); in task_numa_group()
3096 * reset the data back to default state without freeing ->numa_faults.
3101 struct numa_group *grp = rcu_dereference_raw(p->numa_group); in task_numa_free()
3102 unsigned long *numa_faults = p->numa_faults; in task_numa_free()
3110 spin_lock_irqsave(&grp->lock, flags); in task_numa_free()
3112 grp->faults[i] -= p->numa_faults[i]; in task_numa_free()
3113 grp->total_faults -= p->total_numa_faults; in task_numa_free()
3115 grp->nr_tasks--; in task_numa_free()
3116 spin_unlock_irqrestore(&grp->lock, flags); in task_numa_free()
3117 RCU_INIT_POINTER(p->numa_group, NULL); in task_numa_free()
3122 p->numa_faults = NULL; in task_numa_free()
3125 p->total_numa_faults = 0; in task_numa_free()
3147 if (!p->mm) in task_numa_fault()
3159 /* Allocate buffer to track faults on a per-node basis */ in task_numa_fault()
3160 if (unlikely(!p->numa_faults)) { in task_numa_fault()
3161 int size = sizeof(*p->numa_faults) * in task_numa_fault()
3164 p->numa_faults = kzalloc(size, GFP_KERNEL|__GFP_NOWARN); in task_numa_fault()
3165 if (!p->numa_faults) in task_numa_fault()
3168 p->total_numa_faults = 0; in task_numa_fault()
3169 memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); in task_numa_fault()
3176 if (unlikely(last_cpupid == (-1 & LAST_CPUPID_MASK))) { in task_numa_fault()
3191 if (!priv && !local && ng && ng->active_nodes > 1 && in task_numa_fault()
3200 if (time_after(jiffies, p->numa_migrate_retry)) { in task_numa_fault()
3206 p->numa_pages_migrated += pages; in task_numa_fault()
3208 p->numa_faults_locality[2] += pages; in task_numa_fault()
3210 p->numa_faults[task_faults_idx(NUMA_MEMBUF, mem_node, priv)] += pages; in task_numa_fault()
3211 p->numa_faults[task_faults_idx(NUMA_CPUBUF, cpu_node, priv)] += pages; in task_numa_fault()
3212 p->numa_faults_locality[local] += pages; in task_numa_fault()
3219 * p->mm->numa_scan_seq is written to without exclusive access in reset_ptenuma_scan()
3225 WRITE_ONCE(p->mm->numa_scan_seq, READ_ONCE(p->mm->numa_scan_seq) + 1); in reset_ptenuma_scan()
3226 p->mm->numa_scan_offset = 0; in reset_ptenuma_scan()
3238 if ((READ_ONCE(current->mm->numa_scan_seq) - vma->numab_state->start_scan_seq) < 2) in vma_is_accessed()
3241 pids = vma->numab_state->pids_active[0] | vma->numab_state->pids_active[1]; in vma_is_accessed()
3242 if (test_bit(hash_32(current->pid, ilog2(BITS_PER_LONG)), &pids)) in vma_is_accessed()
3247 * some VMAs may never be scanned in multi-threaded applications: in vma_is_accessed()
3249 if (mm->numa_scan_offset > vma->vm_start) { in vma_is_accessed()
3259 if (READ_ONCE(mm->numa_scan_seq) > in vma_is_accessed()
3260 (vma->numab_state->prev_scan_seq + get_nr_threads(current))) in vma_is_accessed()
3276 struct mm_struct *mm = p->mm; in task_numa_work()
3277 u64 runtime = p->se.sum_exec_runtime; in task_numa_work()
3288 work->next = work; in task_numa_work()
3292 * NOTE: make sure not to dereference p->mm before this check, in task_numa_work()
3294 * without p->mm even though we still had it when we enqueued this in task_numa_work()
3297 if (p->flags & PF_EXITING) in task_numa_work()
3309 if (!mm->numa_next_scan) { in task_numa_work()
3310 mm->numa_next_scan = now + in task_numa_work()
3317 migrate = mm->numa_next_scan; in task_numa_work()
3321 if (p->numa_scan_period == 0) { in task_numa_work()
3322 p->numa_scan_period_max = task_scan_max(p); in task_numa_work()
3323 p->numa_scan_period = task_scan_start(p); in task_numa_work()
3326 next_scan = now + msecs_to_jiffies(p->numa_scan_period); in task_numa_work()
3327 if (!try_cmpxchg(&mm->numa_next_scan, &migrate, next_scan)) in task_numa_work()
3334 p->node_stamp += 2 * TICK_NSEC; in task_numa_work()
3337 pages <<= 20 - PAGE_SHIFT; /* MB in pages */ in task_numa_work()
3354 start = mm->numa_scan_offset; in task_numa_work()
3366 is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_MIXEDMAP)) { in task_numa_work()
3374 * hinting faults in read-only file-backed mappings or the vDSO in task_numa_work()
3377 if (!vma->vm_mm || in task_numa_work()
3378 (vma->vm_file && (vma->vm_flags & (VM_READ|VM_WRITE)) == (VM_READ))) { in task_numa_work()
3392 /* Initialise new per-VMA NUMAB state. */ in task_numa_work()
3393 if (!vma->numab_state) { in task_numa_work()
3400 if (cmpxchg(&vma->numab_state, NULL, ptr)) { in task_numa_work()
3405 vma->numab_state->start_scan_seq = mm->numa_scan_seq; in task_numa_work()
3407 vma->numab_state->next_scan = now + in task_numa_work()
3411 vma->numab_state->pids_active_reset = vma->numab_state->next_scan + in task_numa_work()
3419 vma->numab_state->prev_scan_seq = mm->numa_scan_seq - 1; in task_numa_work()
3426 if (mm->numa_scan_seq && time_before(jiffies, in task_numa_work()
3427 vma->numab_state->next_scan)) { in task_numa_work()
3433 if (mm->numa_scan_seq && in task_numa_work()
3434 time_after(jiffies, vma->numab_state->pids_active_reset)) { in task_numa_work()
3435 vma->numab_state->pids_active_reset = vma->numab_state->pids_active_reset + in task_numa_work()
3437 vma->numab_state->pids_active[0] = READ_ONCE(vma->numab_state->pids_active[1]); in task_numa_work()
3438 vma->numab_state->pids_active[1] = 0; in task_numa_work()
3442 if (vma->numab_state->prev_scan_seq == mm->numa_scan_seq) { in task_numa_work()
3443 mm->numa_scan_offset = vma->vm_end; in task_numa_work()
3459 start = max(start, vma->vm_start); in task_numa_work()
3461 end = min(end, vma->vm_end); in task_numa_work()
3467 * is not already PTE-numa. If the VMA contains in task_numa_work()
3473 pages -= (end - start) >> PAGE_SHIFT; in task_numa_work()
3474 virtpages -= (end - start) >> PAGE_SHIFT; in task_numa_work()
3481 } while (end != vma->vm_end); in task_numa_work()
3484 vma->numab_state->prev_scan_seq = mm->numa_scan_seq; in task_numa_work()
3512 mm->numa_scan_offset = start; in task_numa_work()
3523 if (unlikely(p->se.sum_exec_runtime != runtime)) { in task_numa_work()
3524 u64 diff = p->se.sum_exec_runtime - runtime; in task_numa_work()
3525 p->node_stamp += 32 * diff; in task_numa_work()
3532 struct mm_struct *mm = p->mm; in init_numa_balancing()
3535 mm_users = atomic_read(&mm->mm_users); in init_numa_balancing()
3537 mm->numa_next_scan = jiffies + msecs_to_jiffies(sysctl_numa_balancing_scan_delay); in init_numa_balancing()
3538 mm->numa_scan_seq = 0; in init_numa_balancing()
3541 p->node_stamp = 0; in init_numa_balancing()
3542 p->numa_scan_seq = mm ? mm->numa_scan_seq : 0; in init_numa_balancing()
3543 p->numa_scan_period = sysctl_numa_balancing_scan_delay; in init_numa_balancing()
3544 p->numa_migrate_retry = 0; in init_numa_balancing()
3546 p->numa_work.next = &p->numa_work; in init_numa_balancing()
3547 p->numa_faults = NULL; in init_numa_balancing()
3548 p->numa_pages_migrated = 0; in init_numa_balancing()
3549 p->total_numa_faults = 0; in init_numa_balancing()
3550 RCU_INIT_POINTER(p->numa_group, NULL); in init_numa_balancing()
3551 p->last_task_numa_placement = 0; in init_numa_balancing()
3552 p->last_sum_exec_runtime = 0; in init_numa_balancing()
3554 init_task_work(&p->numa_work, task_numa_work); in init_numa_balancing()
3558 p->numa_preferred_nid = NUMA_NO_NODE; in init_numa_balancing()
3570 current->numa_scan_period * mm_users * NSEC_PER_MSEC); in init_numa_balancing()
3572 p->node_stamp = delay; in init_numa_balancing()
3581 struct callback_head *work = &curr->numa_work; in task_tick_numa()
3587 if (!curr->mm || (curr->flags & (PF_EXITING | PF_KTHREAD)) || work->next != work) in task_tick_numa()
3596 now = curr->se.sum_exec_runtime; in task_tick_numa()
3597 period = (u64)curr->numa_scan_period * NSEC_PER_MSEC; in task_tick_numa()
3599 if (now > curr->node_stamp + period) { in task_tick_numa()
3600 if (!curr->node_stamp) in task_tick_numa()
3601 curr->numa_scan_period = task_scan_start(curr); in task_tick_numa()
3602 curr->node_stamp += period; in task_tick_numa()
3604 if (!time_before(jiffies, curr->mm->numa_next_scan)) in task_tick_numa()
3617 if (!p->mm || !p->numa_faults || (p->flags & PF_EXITING)) in update_scan_period()
3626 * is pulled cross-node due to wakeups or load balancing. in update_scan_period()
3628 if (p->numa_scan_seq) { in update_scan_period()
3634 if (dst_nid == p->numa_preferred_nid || in update_scan_period()
3635 (p->numa_preferred_nid != NUMA_NO_NODE && in update_scan_period()
3636 src_nid != p->numa_preferred_nid)) in update_scan_period()
3640 p->numa_scan_period = task_scan_start(p); in update_scan_period()
3666 update_load_add(&cfs_rq->load, se->load.weight); in account_entity_enqueue()
3671 list_add(&se->group_node, &rq->cfs_tasks); in account_entity_enqueue()
3673 cfs_rq->nr_queued++; in account_entity_enqueue()
3679 update_load_sub(&cfs_rq->load, se->load.weight); in account_entity_dequeue()
3682 list_del_init(&se->group_node); in account_entity_dequeue()
3684 cfs_rq->nr_queued--; in account_entity_dequeue()
3690 * Explicitly do a load-store to ensure the intermediate value never hits
3710 * Explicitly do a load-store to ensure the intermediate value never hits
3718 res = var - val; \
3727 * A variant of sub_positive(), which does not use explicit load-store
3732 *ptr -= min_t(typeof(*ptr), *ptr, _val); \
3738 cfs_rq->avg.load_avg += se->avg.load_avg; in enqueue_load_avg()
3739 cfs_rq->avg.load_sum += se_weight(se) * se->avg.load_sum; in enqueue_load_avg()
3745 sub_positive(&cfs_rq->avg.load_avg, se->avg.load_avg); in dequeue_load_avg()
3746 sub_positive(&cfs_rq->avg.load_sum, se_weight(se) * se->avg.load_sum); in dequeue_load_avg()
3748 cfs_rq->avg.load_sum = max_t(u32, cfs_rq->avg.load_sum, in dequeue_load_avg()
3749 cfs_rq->avg.load_avg * PELT_MIN_DIVIDER); in dequeue_load_avg()
3757 bool curr = cfs_rq->curr == se; in reweight_entity()
3759 if (se->on_rq) { in reweight_entity()
3763 se->deadline -= se->vruntime; in reweight_entity()
3764 se->rel_deadline = 1; in reweight_entity()
3765 cfs_rq->nr_queued--; in reweight_entity()
3768 update_load_sub(&cfs_rq->load, se->load.weight); in reweight_entity()
3773 * Because we keep se->vlag = V - v_i, while: lag_i = w_i*(V - v_i), in reweight_entity()
3774 * we need to scale se->vlag when w_i changes. in reweight_entity()
3776 se->vlag = div_s64(se->vlag * se->load.weight, weight); in reweight_entity()
3777 if (se->rel_deadline) in reweight_entity()
3778 se->deadline = div_s64(se->deadline * se->load.weight, weight); in reweight_entity()
3780 update_load_set(&se->load, weight); in reweight_entity()
3783 u32 divider = get_pelt_divider(&se->avg); in reweight_entity()
3785 se->avg.load_avg = div_u64(se_weight(se) * se->avg.load_sum, divider); in reweight_entity()
3789 if (se->on_rq) { in reweight_entity()
3791 update_load_add(&cfs_rq->load, se->load.weight); in reweight_entity()
3794 cfs_rq->nr_queued++; in reweight_entity()
3801 struct sched_entity *se = &p->se; in reweight_task_fair()
3803 struct load_weight *load = &se->load; in reweight_task_fair()
3805 reweight_entity(cfs_rq, se, lw->weight); in reweight_task_fair()
3806 load->inv_weight = lw->inv_weight; in reweight_task_fair()
3819 * tg->weight * grq->load.weight
3820 * ge->load.weight = ----------------------------- (1)
3821 * \Sum grq->load.weight
3829 * grq->load.weight -> grq->avg.load_avg (2)
3833 * tg->weight * grq->avg.load_avg
3834 * ge->load.weight = ------------------------------ (3)
3835 * tg->load_avg
3837 * Where: tg->load_avg ~= \Sum grq->avg.load_avg
3841 * The problem with it is that because the average is slow -- it was designed
3842 * to be exactly that of course -- this leads to transients in boundary
3844 * one task. It takes time for our CPU's grq->avg.load_avg to build up,
3849 * tg->weight * grq->load.weight
3850 * ge->load.weight = ----------------------------- = tg->weight (4)
3851 * grp->load.weight
3858 * ge->load.weight =
3860 * tg->weight * grq->load.weight
3861 * --------------------------------------------------- (5)
3862 * tg->load_avg - grq->avg.load_avg + grq->load.weight
3864 * But because grq->load.weight can drop to 0, resulting in a divide by zero,
3865 * we need to use grq->avg.load_avg as its lower bound, which then gives:
3868 * tg->weight * grq->load.weight
3869 * ge->load.weight = ----------------------------- (6)
3874 * tg_load_avg' = tg->load_avg - grq->avg.load_avg +
3875 * max(grq->load.weight, grq->avg.load_avg)
3879 * overestimates the ge->load.weight and therefore:
3881 * \Sum ge->load.weight >= tg->weight
3888 struct task_group *tg = cfs_rq->tg; in calc_group_shares()
3890 tg_shares = READ_ONCE(tg->shares); in calc_group_shares()
3892 load = max(scale_load_down(cfs_rq->load.weight), cfs_rq->avg.load_avg); in calc_group_shares()
3894 tg_weight = atomic_long_read(&tg->load_avg); in calc_group_shares()
3897 tg_weight -= cfs_rq->tg_load_avg_contrib; in calc_group_shares()
3905 * MIN_SHARES has to be unscaled here to support per-CPU partitioning in calc_group_shares()
3906 * of a group with small tg->shares value. It is a floor value which is in calc_group_shares()
3910 * E.g. on 64-bit for a group with tg->shares of scale_load(15)=15*1024 in calc_group_shares()
3911 * on an 8-core system with 8 tasks each runnable on one CPU shares has in calc_group_shares()
3932 if (!gcfs_rq || !gcfs_rq->load.weight) in update_cfs_group()
3936 if (unlikely(se->load.weight != shares)) in update_cfs_group()
3950 if (&rq->cfs == cfs_rq) { in cfs_rq_util_change()
3960 * As is, the util number is not freq-invariant (we'd have to in cfs_rq_util_change()
3971 if (sa->load_sum) in load_avg_is_decayed()
3974 if (sa->util_sum) in load_avg_is_decayed()
3977 if (sa->runnable_sum) in load_avg_is_decayed()
3985 WARN_ON_ONCE(sa->load_avg || in load_avg_is_decayed()
3986 sa->util_avg || in load_avg_is_decayed()
3987 sa->runnable_avg); in load_avg_is_decayed()
3994 return u64_u32_load_copy(cfs_rq->avg.last_update_time, in cfs_rq_last_update_time()
3995 cfs_rq->last_update_time_copy); in cfs_rq_last_update_time()
4001 * bottom-up, we only have to test whether the cfs_rq before us on the list
4012 if (cfs_rq->on_list) { in child_cfs_rq_on_list()
4013 prev = cfs_rq->leaf_cfs_rq_list.prev; in child_cfs_rq_on_list()
4015 prev = rq->tmp_alone_branch; in child_cfs_rq_on_list()
4018 if (prev == &rq->leaf_cfs_rq_list) in child_cfs_rq_on_list()
4023 return (prev_cfs_rq->tg->parent == cfs_rq->tg); in child_cfs_rq_on_list()
4028 if (cfs_rq->load.weight) in cfs_rq_is_decayed()
4031 if (!load_avg_is_decayed(&cfs_rq->avg)) in cfs_rq_is_decayed()
4037 if (cfs_rq->tg_load_avg_contrib) in cfs_rq_is_decayed()
4044 * update_tg_load_avg - update the tg's load avg
4047 * This function 'ensures': tg->load_avg := \Sum tg->cfs_rq[]->avg.load.
4048 * However, because tg->load_avg is a global value there are performance
4065 if (cfs_rq->tg == &root_task_group) in update_tg_load_avg()
4073 * For migration heavy workloads, access to tg->load_avg can be in update_tg_load_avg()
4077 if (now - cfs_rq->last_update_tg_load_avg < NSEC_PER_MSEC) in update_tg_load_avg()
4080 delta = cfs_rq->avg.load_avg - cfs_rq->tg_load_avg_contrib; in update_tg_load_avg()
4081 if (abs(delta) > cfs_rq->tg_load_avg_contrib / 64) { in update_tg_load_avg()
4082 atomic_long_add(delta, &cfs_rq->tg->load_avg); in update_tg_load_avg()
4083 cfs_rq->tg_load_avg_contrib = cfs_rq->avg.load_avg; in update_tg_load_avg()
4084 cfs_rq->last_update_tg_load_avg = now; in update_tg_load_avg()
4096 if (cfs_rq->tg == &root_task_group) in clear_tg_load_avg()
4100 delta = 0 - cfs_rq->tg_load_avg_contrib; in clear_tg_load_avg()
4101 atomic_long_add(delta, &cfs_rq->tg->load_avg); in clear_tg_load_avg()
4102 cfs_rq->tg_load_avg_contrib = 0; in clear_tg_load_avg()
4103 cfs_rq->last_update_tg_load_avg = now; in clear_tg_load_avg()
4122 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in clear_tg_offline_cfs_rqs()
4133 * caller only guarantees p->pi_lock is held; no other assumptions,
4134 * including the state of rq->lock, should be made.
4148 * getting what current time is, so simply throw away the out-of-date in set_task_rq_fair()
4152 if (!(se->avg.last_update_time && prev)) in set_task_rq_fair()
4159 se->avg.last_update_time = n_last_update_time; in set_task_rq_fair()
4167 * ge->avg == grq->avg (1)
4178 * ge->avg.load_avg = ge->load.weight * ge->avg.runnable_avg (2)
4183 * grq->avg.load_avg = grq->load.weight * grq->avg.runnable_avg (3)
4187 * ge->avg.runnable_avg == grq->avg.runnable_avg
4191 * ge->load.weight * grq->avg.load_avg
4192 * ge->avg.load_avg = ----------------------------------- (4)
4193 * grq->load.weight
4206 * Another reason this doesn't work is that runnable isn't a 0-sum entity.
4217 * ge->avg.running_sum <= ge->avg.runnable_sum <= LOAD_AVG_MAX
4224 * grq->avg.runnable_sum = grq->avg.load_sum / grq->load.weight
4232 long delta_sum, delta_avg = gcfs_rq->avg.util_avg - se->avg.util_avg; in update_tg_cfs_util()
4240 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in update_tg_cfs_util()
4243 divider = get_pelt_divider(&cfs_rq->avg); in update_tg_cfs_util()
4247 se->avg.util_avg = gcfs_rq->avg.util_avg; in update_tg_cfs_util()
4248 new_sum = se->avg.util_avg * divider; in update_tg_cfs_util()
4249 delta_sum = (long)new_sum - (long)se->avg.util_sum; in update_tg_cfs_util()
4250 se->avg.util_sum = new_sum; in update_tg_cfs_util()
4253 add_positive(&cfs_rq->avg.util_avg, delta_avg); in update_tg_cfs_util()
4254 add_positive(&cfs_rq->avg.util_sum, delta_sum); in update_tg_cfs_util()
4257 cfs_rq->avg.util_sum = max_t(u32, cfs_rq->avg.util_sum, in update_tg_cfs_util()
4258 cfs_rq->avg.util_avg * PELT_MIN_DIVIDER); in update_tg_cfs_util()
4264 long delta_sum, delta_avg = gcfs_rq->avg.runnable_avg - se->avg.runnable_avg; in update_tg_cfs_runnable()
4272 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in update_tg_cfs_runnable()
4275 divider = get_pelt_divider(&cfs_rq->avg); in update_tg_cfs_runnable()
4278 se->avg.runnable_avg = gcfs_rq->avg.runnable_avg; in update_tg_cfs_runnable()
4279 new_sum = se->avg.runnable_avg * divider; in update_tg_cfs_runnable()
4280 delta_sum = (long)new_sum - (long)se->avg.runnable_sum; in update_tg_cfs_runnable()
4281 se->avg.runnable_sum = new_sum; in update_tg_cfs_runnable()
4284 add_positive(&cfs_rq->avg.runnable_avg, delta_avg); in update_tg_cfs_runnable()
4285 add_positive(&cfs_rq->avg.runnable_sum, delta_sum); in update_tg_cfs_runnable()
4287 cfs_rq->avg.runnable_sum = max_t(u32, cfs_rq->avg.runnable_sum, in update_tg_cfs_runnable()
4288 cfs_rq->avg.runnable_avg * PELT_MIN_DIVIDER); in update_tg_cfs_runnable()
4294 long delta_avg, running_sum, runnable_sum = gcfs_rq->prop_runnable_sum; in update_tg_cfs_load()
4303 gcfs_rq->prop_runnable_sum = 0; in update_tg_cfs_load()
4306 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in update_tg_cfs_load()
4309 divider = get_pelt_divider(&cfs_rq->avg); in update_tg_cfs_load()
4316 runnable_sum += se->avg.load_sum; in update_tg_cfs_load()
4323 if (scale_load_down(gcfs_rq->load.weight)) { in update_tg_cfs_load()
4324 load_sum = div_u64(gcfs_rq->avg.load_sum, in update_tg_cfs_load()
4325 scale_load_down(gcfs_rq->load.weight)); in update_tg_cfs_load()
4329 runnable_sum = min(se->avg.load_sum, load_sum); in update_tg_cfs_load()
4338 running_sum = se->avg.util_sum >> SCHED_CAPACITY_SHIFT; in update_tg_cfs_load()
4344 delta_avg = load_avg - se->avg.load_avg; in update_tg_cfs_load()
4348 delta_sum = load_sum - (s64)se_weight(se) * se->avg.load_sum; in update_tg_cfs_load()
4350 se->avg.load_sum = runnable_sum; in update_tg_cfs_load()
4351 se->avg.load_avg = load_avg; in update_tg_cfs_load()
4352 add_positive(&cfs_rq->avg.load_avg, delta_avg); in update_tg_cfs_load()
4353 add_positive(&cfs_rq->avg.load_sum, delta_sum); in update_tg_cfs_load()
4355 cfs_rq->avg.load_sum = max_t(u32, cfs_rq->avg.load_sum, in update_tg_cfs_load()
4356 cfs_rq->avg.load_avg * PELT_MIN_DIVIDER); in update_tg_cfs_load()
4361 cfs_rq->propagate = 1; in add_tg_cfs_propagate()
4362 cfs_rq->prop_runnable_sum += runnable_sum; in add_tg_cfs_propagate()
4374 if (!gcfs_rq->propagate) in propagate_entity_load_avg()
4377 gcfs_rq->propagate = 0; in propagate_entity_load_avg()
4381 add_tg_cfs_propagate(cfs_rq, gcfs_rq->prop_runnable_sum); in propagate_entity_load_avg()
4405 if (se->avg.load_avg || se->avg.util_avg) in skip_blocked_update()
4412 if (gcfs_rq->propagate) in skip_blocked_update()
4446 if (load_avg_is_decayed(&se->avg)) in migrate_se_pelt_lag()
4453 is_idle = is_idle_task(rcu_dereference(rq->curr)); in migrate_se_pelt_lag()
4470 * - cfs->throttled_clock_pelt_time@cfs_rq_idle in migrate_se_pelt_lag()
4473 * = rq_clock_pelt()@rq_idle - rq_clock_pelt()@cfs_rq_idle in migrate_se_pelt_lag()
4476 * = sched_clock_cpu() - rq_clock()@rq_idle in migrate_se_pelt_lag()
4480 * now = rq_clock_pelt()@rq_idle - cfs->throttled_clock_pelt_time + in migrate_se_pelt_lag()
4481 * sched_clock_cpu() - rq_clock()@rq_idle in migrate_se_pelt_lag()
4483 * rq_clock_pelt()@rq_idle is rq->clock_pelt_idle in migrate_se_pelt_lag()
4484 * rq_clock()@rq_idle is rq->clock_idle in migrate_se_pelt_lag()
4485 * cfs->throttled_clock_pelt_time@cfs_rq_idle in migrate_se_pelt_lag()
4486 * is cfs_rq->throttled_pelt_idle in migrate_se_pelt_lag()
4490 throttled = u64_u32_load(cfs_rq->throttled_pelt_idle); in migrate_se_pelt_lag()
4495 now = u64_u32_load(rq->clock_pelt_idle); in migrate_se_pelt_lag()
4505 now -= throttled; in migrate_se_pelt_lag()
4508 * cfs_rq->avg.last_update_time is more recent than our in migrate_se_pelt_lag()
4513 now += sched_clock_cpu(cpu_of(rq)) - u64_u32_load(rq->clock_idle); in migrate_se_pelt_lag()
4522 * update_cfs_rq_load_avg - update the cfs_rq's load/util averages
4529 * cfs_rq->avg is used for task_h_load() and update_cfs_share() for example.
4533 * Since both these conditions indicate a changed cfs_rq->avg.load we should
4540 struct sched_avg *sa = &cfs_rq->avg; in update_cfs_rq_load_avg()
4543 if (cfs_rq->removed.nr) { in update_cfs_rq_load_avg()
4545 u32 divider = get_pelt_divider(&cfs_rq->avg); in update_cfs_rq_load_avg()
4547 raw_spin_lock(&cfs_rq->removed.lock); in update_cfs_rq_load_avg()
4548 swap(cfs_rq->removed.util_avg, removed_util); in update_cfs_rq_load_avg()
4549 swap(cfs_rq->removed.load_avg, removed_load); in update_cfs_rq_load_avg()
4550 swap(cfs_rq->removed.runnable_avg, removed_runnable); in update_cfs_rq_load_avg()
4551 cfs_rq->removed.nr = 0; in update_cfs_rq_load_avg()
4552 raw_spin_unlock(&cfs_rq->removed.lock); in update_cfs_rq_load_avg()
4555 sub_positive(&sa->load_avg, r); in update_cfs_rq_load_avg()
4556 sub_positive(&sa->load_sum, r * divider); in update_cfs_rq_load_avg()
4557 /* See sa->util_sum below */ in update_cfs_rq_load_avg()
4558 sa->load_sum = max_t(u32, sa->load_sum, sa->load_avg * PELT_MIN_DIVIDER); in update_cfs_rq_load_avg()
4561 sub_positive(&sa->util_avg, r); in update_cfs_rq_load_avg()
4562 sub_positive(&sa->util_sum, r * divider); in update_cfs_rq_load_avg()
4564 * Because of rounding, se->util_sum might ends up being +1 more than in update_cfs_rq_load_avg()
4565 * cfs->util_sum. Although this is not a problem by itself, detaching in update_cfs_rq_load_avg()
4567 * util_avg (~1ms) can make cfs->util_sum becoming null whereas in update_cfs_rq_load_avg()
4574 sa->util_sum = max_t(u32, sa->util_sum, sa->util_avg * PELT_MIN_DIVIDER); in update_cfs_rq_load_avg()
4577 sub_positive(&sa->runnable_avg, r); in update_cfs_rq_load_avg()
4578 sub_positive(&sa->runnable_sum, r * divider); in update_cfs_rq_load_avg()
4579 /* See sa->util_sum above */ in update_cfs_rq_load_avg()
4580 sa->runnable_sum = max_t(u32, sa->runnable_sum, in update_cfs_rq_load_avg()
4581 sa->runnable_avg * PELT_MIN_DIVIDER); in update_cfs_rq_load_avg()
4588 -(long)(removed_runnable * divider) >> SCHED_CAPACITY_SHIFT); in update_cfs_rq_load_avg()
4594 u64_u32_store_copy(sa->last_update_time, in update_cfs_rq_load_avg()
4595 cfs_rq->last_update_time_copy, in update_cfs_rq_load_avg()
4596 sa->last_update_time); in update_cfs_rq_load_avg()
4601 * attach_entity_load_avg - attach this entity to its cfs_rq load avg
4606 * cfs_rq->avg.last_update_time being current.
4611 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in attach_entity_load_avg()
4614 u32 divider = get_pelt_divider(&cfs_rq->avg); in attach_entity_load_avg()
4623 se->avg.last_update_time = cfs_rq->avg.last_update_time; in attach_entity_load_avg()
4624 se->avg.period_contrib = cfs_rq->avg.period_contrib; in attach_entity_load_avg()
4632 se->avg.util_sum = se->avg.util_avg * divider; in attach_entity_load_avg()
4634 se->avg.runnable_sum = se->avg.runnable_avg * divider; in attach_entity_load_avg()
4636 se->avg.load_sum = se->avg.load_avg * divider; in attach_entity_load_avg()
4637 if (se_weight(se) < se->avg.load_sum) in attach_entity_load_avg()
4638 se->avg.load_sum = div_u64(se->avg.load_sum, se_weight(se)); in attach_entity_load_avg()
4640 se->avg.load_sum = 1; in attach_entity_load_avg()
4643 cfs_rq->avg.util_avg += se->avg.util_avg; in attach_entity_load_avg()
4644 cfs_rq->avg.util_sum += se->avg.util_sum; in attach_entity_load_avg()
4645 cfs_rq->avg.runnable_avg += se->avg.runnable_avg; in attach_entity_load_avg()
4646 cfs_rq->avg.runnable_sum += se->avg.runnable_sum; in attach_entity_load_avg()
4648 add_tg_cfs_propagate(cfs_rq, se->avg.load_sum); in attach_entity_load_avg()
4656 * detach_entity_load_avg - detach this entity from its cfs_rq load avg
4661 * cfs_rq->avg.last_update_time being current.
4666 sub_positive(&cfs_rq->avg.util_avg, se->avg.util_avg); in detach_entity_load_avg()
4667 sub_positive(&cfs_rq->avg.util_sum, se->avg.util_sum); in detach_entity_load_avg()
4669 cfs_rq->avg.util_sum = max_t(u32, cfs_rq->avg.util_sum, in detach_entity_load_avg()
4670 cfs_rq->avg.util_avg * PELT_MIN_DIVIDER); in detach_entity_load_avg()
4672 sub_positive(&cfs_rq->avg.runnable_avg, se->avg.runnable_avg); in detach_entity_load_avg()
4673 sub_positive(&cfs_rq->avg.runnable_sum, se->avg.runnable_sum); in detach_entity_load_avg()
4675 cfs_rq->avg.runnable_sum = max_t(u32, cfs_rq->avg.runnable_sum, in detach_entity_load_avg()
4676 cfs_rq->avg.runnable_avg * PELT_MIN_DIVIDER); in detach_entity_load_avg()
4678 add_tg_cfs_propagate(cfs_rq, -se->avg.load_sum); in detach_entity_load_avg()
4703 if (se->avg.last_update_time && !(flags & SKIP_AGE_LOAD)) in update_load_avg()
4709 if (!se->avg.last_update_time && (flags & DO_ATTACH)) { in update_load_avg()
4759 * tasks cannot exit without having gone through wake_up_new_task() -> in remove_entity_load_avg()
4766 raw_spin_lock_irqsave(&cfs_rq->removed.lock, flags); in remove_entity_load_avg()
4767 ++cfs_rq->removed.nr; in remove_entity_load_avg()
4768 cfs_rq->removed.util_avg += se->avg.util_avg; in remove_entity_load_avg()
4769 cfs_rq->removed.load_avg += se->avg.load_avg; in remove_entity_load_avg()
4770 cfs_rq->removed.runnable_avg += se->avg.runnable_avg; in remove_entity_load_avg()
4771 raw_spin_unlock_irqrestore(&cfs_rq->removed.lock, flags); in remove_entity_load_avg()
4776 return cfs_rq->avg.runnable_avg; in cfs_rq_runnable_avg()
4781 return cfs_rq->avg.load_avg; in cfs_rq_load_avg()
4788 return READ_ONCE(p->se.avg.util_avg); in task_util()
4793 return READ_ONCE(p->se.avg.runnable_avg); in task_runnable()
4798 return READ_ONCE(p->se.avg.util_est) & ~UTIL_AVG_UNCHANGED; in _task_util_est()
4815 enqueued = cfs_rq->avg.util_est; in util_est_enqueue()
4817 WRITE_ONCE(cfs_rq->avg.util_est, enqueued); in util_est_enqueue()
4831 enqueued = cfs_rq->avg.util_est; in util_est_dequeue()
4832 enqueued -= min_t(unsigned int, enqueued, _task_util_est(p)); in util_est_dequeue()
4833 WRITE_ONCE(cfs_rq->avg.util_est, enqueued); in util_est_dequeue()
4857 ewma = READ_ONCE(p->se.avg.util_est); in util_est_update()
4882 last_ewma_diff = ewma - dequeued; in util_est_update()
4901 * ewma(t) = w * task_util(p) + (1-w) * ewma(t-1) in util_est_update()
4902 * = w * task_util(p) + ewma(t-1) - w * ewma(t-1) in util_est_update()
4903 * = w * (task_util(p) - ewma(t-1)) + ewma(t-1) in util_est_update()
4904 * = w * ( -last_ewma_diff ) + ewma(t-1) in util_est_update()
4905 * = w * (-last_ewma_diff + ewma(t-1) / w) in util_est_update()
4911 ewma -= last_ewma_diff; in util_est_update()
4915 WRITE_ONCE(p->se.avg.util_est, ewma); in util_est_update()
4917 trace_sched_util_est_se_tp(&p->se); in util_est_update()
4924 capacity -= max(hw_load_avg(cpu_rq(cpu)), cpufreq_get_pressure(cpu)); in get_actual_cpu_capacity()
4982 * +---------------------------------------- in util_fits_cpu()
5020 * +---------------------------------------- in util_fits_cpu()
5043 return -1; in util_fits_cpu()
5071 if (!p || (p->nr_cpus_allowed == 1) || in update_misfit_status()
5072 (arch_scale_cpu_capacity(cpu) == p->max_allowed_capacity) || in update_misfit_status()
5075 rq->misfit_task_load = 0; in update_misfit_status()
5083 rq->misfit_task_load = max_t(unsigned long, task_h_load(p), 1); in update_misfit_status()
5088 struct sched_entity *se = &p->se; in __setparam_fair()
5090 p->static_prio = NICE_TO_PRIO(attr->sched_nice); in __setparam_fair()
5091 if (attr->sched_runtime) { in __setparam_fair()
5092 se->custom_slice = 1; in __setparam_fair()
5093 se->slice = clamp_t(u64, attr->sched_runtime, in __setparam_fair()
5097 se->custom_slice = 0; in __setparam_fair()
5098 se->slice = sysctl_sched_base_slice; in __setparam_fair()
5108 if (!se->custom_slice) in place_entity()
5109 se->slice = sysctl_sched_base_slice; in place_entity()
5110 vslice = calc_delta_fair(se->slice, se); in place_entity()
5120 if (sched_feat(PLACE_LAG) && cfs_rq->nr_queued && se->vlag) { in place_entity()
5121 struct sched_entity *curr = cfs_rq->curr; in place_entity()
5124 lag = se->vlag; in place_entity()
5134 * lag_i = S - s_i = w_i * (V - v_i) in place_entity()
5139 * vl_i = V - v_i <=> v_i = V - vl_i in place_entity()
5151 * = (W*V + w_i*(V - vl_i)) / (W + w_i) in place_entity()
5152 * = (W*V + w_i*V - w_i*vl_i) / (W + w_i) in place_entity()
5153 * = (V*(W + w_i) - w_i*vl_i) / (W + w_i) in place_entity()
5154 * = V - w_i*vl_i / (W + w_i) in place_entity()
5158 * vl'_i = V' - v_i in place_entity()
5159 * = V - w_i*vl_i / (W + w_i) - (V - vl_i) in place_entity()
5160 * = vl_i - w_i*vl_i / (W + w_i) in place_entity()
5170 * vl'_i = vl_i - w_i*vl_i / (W + w_i) in place_entity()
5171 * = ((W + w_i)*vl_i - w_i*vl_i) / (W + w_i) in place_entity()
5173 * (W + w_i)*vl'_i = (W + w_i)*vl_i - w_i*vl_i in place_entity()
5178 load = cfs_rq->avg_load; in place_entity()
5179 if (curr && curr->on_rq) in place_entity()
5180 load += scale_load_down(curr->load.weight); in place_entity()
5182 lag *= load + scale_load_down(se->load.weight); in place_entity()
5188 se->vruntime = vruntime - lag; in place_entity()
5190 if (se->rel_deadline) { in place_entity()
5191 se->deadline += se->vruntime; in place_entity()
5192 se->rel_deadline = 0; in place_entity()
5207 se->deadline = se->vruntime + vslice; in place_entity()
5219 bool curr = cfs_rq->curr == se; in enqueue_entity()
5232 * - Update loads to have both entity and cfs_rq synced with now. in enqueue_entity()
5233 * - For group_entity, update its runnable_weight to reflect the new in enqueue_entity()
5235 * - For group_entity, update its weight to reflect the new share of in enqueue_entity()
5237 * - Add its new weight to cfs_rq->load.weight in enqueue_entity()
5243 * but update_cfs_group() here will re-adjust the weight and have to in enqueue_entity()
5249 * XXX now that the entity has been re-weighted, and it's lag adjusted, in enqueue_entity()
5259 se->exec_start = 0; in enqueue_entity()
5265 se->on_rq = 1; in enqueue_entity()
5267 if (cfs_rq->nr_queued == 1) { in enqueue_entity()
5271 if (cfs_rq->pelt_clock_throttled) { in enqueue_entity()
5274 cfs_rq->throttled_clock_pelt_time += rq_clock_pelt(rq) - in enqueue_entity()
5275 cfs_rq->throttled_clock_pelt; in enqueue_entity()
5276 cfs_rq->pelt_clock_throttled = 0; in enqueue_entity()
5286 if (cfs_rq->next != se) in __clear_buddies_next()
5289 cfs_rq->next = NULL; in __clear_buddies_next()
5295 if (cfs_rq->next == se) in clear_buddies()
5303 se->sched_delayed = 1; in set_delayed()
5316 cfs_rq->h_nr_runnable--; in set_delayed()
5322 se->sched_delayed = 0; in clear_delayed()
5336 cfs_rq->h_nr_runnable++; in clear_delayed()
5343 if (sched_feat(DELAY_ZERO) && se->vlag > 0) in finish_delayed_dequeue_entity()
5344 se->vlag = 0; in finish_delayed_dequeue_entity()
5357 WARN_ON_ONCE(!se->sched_delayed); in dequeue_entity()
5367 WARN_ON_ONCE(delay && se->sched_delayed); in dequeue_entity()
5382 * - Update loads to have both entity and cfs_rq synced with now. in dequeue_entity()
5383 * - For group_entity, update its runnable_weight to reflect the new in dequeue_entity()
5385 * - Subtract its previous weight from cfs_rq->load.weight. in dequeue_entity()
5386 * - For group entity, update its weight to reflect the new share in dequeue_entity()
5396 se->deadline -= se->vruntime; in dequeue_entity()
5397 se->rel_deadline = 1; in dequeue_entity()
5400 if (se != cfs_rq->curr) in dequeue_entity()
5402 se->on_rq = 0; in dequeue_entity()
5413 if (cfs_rq->nr_queued == 0) { in dequeue_entity()
5420 cfs_rq->throttled_clock_pelt = rq_clock_pelt(rq); in dequeue_entity()
5421 cfs_rq->pelt_clock_throttled = 1; in dequeue_entity()
5435 if (se->on_rq) { in set_next_entity()
5449 WARN_ON_ONCE(cfs_rq->curr); in set_next_entity()
5450 cfs_rq->curr = se; in set_next_entity()
5455 * when there are only lesser-weight tasks around): in set_next_entity()
5458 rq_of(cfs_rq)->cfs.load.weight >= 2*se->load.weight) { in set_next_entity()
5462 __schedstat_set(stats->slice_max, in set_next_entity()
5463 max((u64)stats->slice_max, in set_next_entity()
5464 se->sum_exec_runtime - se->prev_sum_exec_runtime)); in set_next_entity()
5467 se->prev_sum_exec_runtime = se->sum_exec_runtime; in set_next_entity()
5485 if (se->sched_delayed) { in pick_next_entity()
5503 if (prev->on_rq) in put_prev_entity()
5509 if (prev->on_rq) { in put_prev_entity()
5516 WARN_ON_ONCE(cfs_rq->curr != prev); in put_prev_entity()
5517 cfs_rq->curr = NULL; in put_prev_entity()
5524 * Update run-time statistics of the 'current'. in entity_tick()
5587 * directly instead of rq->clock to avoid adding additional synchronization
5588 * around rq->lock.
5590 * requires cfs_b->lock
5596 if (unlikely(cfs_b->quota == RUNTIME_INF)) in __refill_cfs_bandwidth_runtime()
5599 cfs_b->runtime += cfs_b->quota; in __refill_cfs_bandwidth_runtime()
5600 runtime = cfs_b->runtime_snap - cfs_b->runtime; in __refill_cfs_bandwidth_runtime()
5602 cfs_b->burst_time += runtime; in __refill_cfs_bandwidth_runtime()
5603 cfs_b->nr_burst++; in __refill_cfs_bandwidth_runtime()
5606 cfs_b->runtime = min(cfs_b->runtime, cfs_b->quota + cfs_b->burst); in __refill_cfs_bandwidth_runtime()
5607 cfs_b->runtime_snap = cfs_b->runtime; in __refill_cfs_bandwidth_runtime()
5612 return &tg->cfs_bandwidth; in tg_cfs_bandwidth()
5621 lockdep_assert_held(&cfs_b->lock); in __assign_cfs_rq_runtime()
5624 min_amount = target_runtime - cfs_rq->runtime_remaining; in __assign_cfs_rq_runtime()
5626 if (cfs_b->quota == RUNTIME_INF) in __assign_cfs_rq_runtime()
5631 if (cfs_b->runtime > 0) { in __assign_cfs_rq_runtime()
5632 amount = min(cfs_b->runtime, min_amount); in __assign_cfs_rq_runtime()
5633 cfs_b->runtime -= amount; in __assign_cfs_rq_runtime()
5634 cfs_b->idle = 0; in __assign_cfs_rq_runtime()
5638 cfs_rq->runtime_remaining += amount; in __assign_cfs_rq_runtime()
5640 return cfs_rq->runtime_remaining > 0; in __assign_cfs_rq_runtime()
5646 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in assign_cfs_rq_runtime()
5649 raw_spin_lock(&cfs_b->lock); in assign_cfs_rq_runtime()
5651 raw_spin_unlock(&cfs_b->lock); in assign_cfs_rq_runtime()
5659 cfs_rq->runtime_remaining -= delta_exec; in __account_cfs_rq_runtime()
5661 if (likely(cfs_rq->runtime_remaining > 0)) in __account_cfs_rq_runtime()
5664 if (cfs_rq->throttled) in __account_cfs_rq_runtime()
5670 if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr)) in __account_cfs_rq_runtime()
5677 if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled) in account_cfs_rq_runtime()
5685 return cfs_bandwidth_used() && cfs_rq->throttled; in cfs_rq_throttled()
5690 return cfs_bandwidth_used() && cfs_rq->pelt_clock_throttled; in cfs_rq_pelt_clock_throttled()
5696 return cfs_bandwidth_used() && cfs_rq->throttle_count; in throttled_hierarchy()
5701 return throttled_hierarchy(task_group(p)->cfs_rq[dst_cpu]); in lb_throttled_hierarchy()
5706 return cfs_bandwidth_used() && p->throttled; in task_is_throttled()
5718 p->sched_throttle_work.next = &p->sched_throttle_work; in throttle_cfs_rq_work()
5724 if ((p->flags & PF_EXITING)) in throttle_cfs_rq_work()
5728 se = &p->se; in throttle_cfs_rq_work()
5732 if (p->sched_class != &fair_sched_class) in throttle_cfs_rq_work()
5739 if (!cfs_rq->throttle_count) in throttle_cfs_rq_work()
5743 WARN_ON_ONCE(p->throttled || !list_empty(&p->throttle_node)); in throttle_cfs_rq_work()
5745 list_add(&p->throttle_node, &cfs_rq->throttled_limbo_list); in throttle_cfs_rq_work()
5750 p->throttled = true; in throttle_cfs_rq_work()
5757 init_task_work(&p->sched_throttle_work, throttle_cfs_rq_work); in init_cfs_throttle_work()
5759 p->sched_throttle_work.next = &p->sched_throttle_work; in init_cfs_throttle_work()
5760 INIT_LIST_HEAD(&p->throttle_node); in init_cfs_throttle_work()
5774 WARN_ON_ONCE(p->se.on_rq); in dequeue_throttled_task()
5775 list_del_init(&p->throttle_node); in dequeue_throttled_task()
5779 p->throttled = false; in dequeue_throttled_task()
5793 struct cfs_rq *cfs_rq = cfs_rq_of(&p->se); in enqueue_throttled_task()
5796 WARN_ON_ONCE(!list_empty(&p->throttle_node)); in enqueue_throttled_task()
5804 * cause @p's group_node to be incorectly re-insterted in its rq's in enqueue_throttled_task()
5819 * enqueue_task(p) -> p's new cfs_rq in enqueue_throttled_task()
5824 * list_move(&se->group_node, &rq->cfs_tasks); // bug in enqueue_throttled_task()
5834 list_add(&p->throttle_node, &cfs_rq->throttled_limbo_list); in enqueue_throttled_task()
5839 p->throttled = false; in enqueue_throttled_task()
5847 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in tg_unthrottle_up()
5850 if (--cfs_rq->throttle_count) in tg_unthrottle_up()
5853 if (cfs_rq->pelt_clock_throttled) { in tg_unthrottle_up()
5854 cfs_rq->throttled_clock_pelt_time += rq_clock_pelt(rq) - in tg_unthrottle_up()
5855 cfs_rq->throttled_clock_pelt; in tg_unthrottle_up()
5856 cfs_rq->pelt_clock_throttled = 0; in tg_unthrottle_up()
5859 if (cfs_rq->throttled_clock_self) { in tg_unthrottle_up()
5860 u64 delta = rq_clock(rq) - cfs_rq->throttled_clock_self; in tg_unthrottle_up()
5862 cfs_rq->throttled_clock_self = 0; in tg_unthrottle_up()
5867 cfs_rq->throttled_clock_self_time += delta; in tg_unthrottle_up()
5870 /* Re-enqueue the tasks that have been throttled at this level. */ in tg_unthrottle_up()
5871 list_for_each_entry_safe(p, tmp, &cfs_rq->throttled_limbo_list, throttle_node) { in tg_unthrottle_up()
5872 list_del_init(&p->throttle_node); in tg_unthrottle_up()
5873 p->throttled = false; in tg_unthrottle_up()
5886 return p->sched_throttle_work.next != &p->sched_throttle_work; in task_has_throttle_work()
5898 if ((p->flags & (PF_EXITING | PF_KTHREAD))) in task_throttle_setup_work()
5901 task_work_add(p, &p->sched_throttle_work, TWA_RESUME); in task_throttle_setup_work()
5908 if (cfs_rq_throttled(cfs_rq) && !cfs_rq->throttled_clock) in record_throttle_clock()
5909 cfs_rq->throttled_clock = rq_clock(rq); in record_throttle_clock()
5911 if (!cfs_rq->throttled_clock_self) in record_throttle_clock()
5912 cfs_rq->throttled_clock_self = rq_clock(rq); in record_throttle_clock()
5918 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in tg_throttle_down()
5920 if (cfs_rq->throttle_count++) in tg_throttle_down()
5927 if (!cfs_rq->nr_queued) { in tg_throttle_down()
5929 cfs_rq->throttled_clock_pelt = rq_clock_pelt(rq); in tg_throttle_down()
5930 cfs_rq->pelt_clock_throttled = 1; in tg_throttle_down()
5933 WARN_ON_ONCE(cfs_rq->throttled_clock_self); in tg_throttle_down()
5934 WARN_ON_ONCE(!list_empty(&cfs_rq->throttled_limbo_list)); in tg_throttle_down()
5941 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in throttle_cfs_rq()
5944 raw_spin_lock(&cfs_b->lock); in throttle_cfs_rq()
5957 list_add_tail_rcu(&cfs_rq->throttled_list, in throttle_cfs_rq()
5958 &cfs_b->throttled_cfs_rq); in throttle_cfs_rq()
5960 raw_spin_unlock(&cfs_b->lock); in throttle_cfs_rq()
5967 walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq); in throttle_cfs_rq()
5972 * throttled-list. rq->lock protects completion. in throttle_cfs_rq()
5974 cfs_rq->throttled = 1; in throttle_cfs_rq()
5975 WARN_ON_ONCE(cfs_rq->throttled_clock); in throttle_cfs_rq()
5982 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in unthrottle_cfs_rq()
5983 struct sched_entity *se = cfs_rq->tg->se[cpu_of(rq)]; in unthrottle_cfs_rq()
5994 if (cfs_rq->runtime_enabled && cfs_rq->runtime_remaining <= 0) 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()
6028 if (rq->curr == rq->idle && rq->cfs.nr_queued) in unthrottle_cfs_rq()
6057 list_for_each_entry_safe(cursor, tmp, &rq->cfsb_csd_list, in __cfsb_csd_unthrottle()
6059 list_del_init(&cursor->throttled_csd_list); in __cfsb_csd_unthrottle()
6082 if (WARN_ON_ONCE(!list_empty(&cfs_rq->throttled_csd_list))) in __unthrottle_cfs_rq_async()
6085 first = list_empty(&rq->cfsb_csd_list); in __unthrottle_cfs_rq_async()
6086 list_add_tail(&cfs_rq->throttled_csd_list, &rq->cfsb_csd_list); in __unthrottle_cfs_rq_async()
6088 smp_call_function_single_async(cpu_of(rq), &rq->cfsb_csd); in __unthrottle_cfs_rq_async()
6096 cfs_rq->runtime_remaining <= 0)) in unthrottle_cfs_rq_async()
6113 list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq, in distribute_cfs_runtime()
6127 if (!list_empty(&cfs_rq->throttled_csd_list)) in distribute_cfs_runtime()
6131 WARN_ON_ONCE(cfs_rq->runtime_remaining > 0); in distribute_cfs_runtime()
6133 raw_spin_lock(&cfs_b->lock); in distribute_cfs_runtime()
6134 runtime = -cfs_rq->runtime_remaining + 1; in distribute_cfs_runtime()
6135 if (runtime > cfs_b->runtime) in distribute_cfs_runtime()
6136 runtime = cfs_b->runtime; in distribute_cfs_runtime()
6137 cfs_b->runtime -= runtime; in distribute_cfs_runtime()
6138 remaining = cfs_b->runtime; in distribute_cfs_runtime()
6139 raw_spin_unlock(&cfs_b->lock); in distribute_cfs_runtime()
6141 cfs_rq->runtime_remaining += runtime; in distribute_cfs_runtime()
6144 if (cfs_rq->runtime_remaining > 0) { in distribute_cfs_runtime()
6153 list_add_tail(&cfs_rq->throttled_csd_list, in distribute_cfs_runtime()
6170 list_del_init(&cfs_rq->throttled_csd_list); in distribute_cfs_runtime()
6187 * period the timer is deactivated until scheduling resumes; cfs_b->idle is
6195 if (cfs_b->quota == RUNTIME_INF) in do_sched_cfs_period_timer()
6198 throttled = !list_empty(&cfs_b->throttled_cfs_rq); in do_sched_cfs_period_timer()
6199 cfs_b->nr_periods += overrun; in do_sched_cfs_period_timer()
6201 /* Refill extra burst quota even if cfs_b->idle */ in do_sched_cfs_period_timer()
6208 if (cfs_b->idle && !throttled) in do_sched_cfs_period_timer()
6213 cfs_b->idle = 1; in do_sched_cfs_period_timer()
6218 cfs_b->nr_throttled += overrun; in do_sched_cfs_period_timer()
6221 * This check is repeated as we release cfs_b->lock while we unthrottle. in do_sched_cfs_period_timer()
6223 while (throttled && cfs_b->runtime > 0) { in do_sched_cfs_period_timer()
6224 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in do_sched_cfs_period_timer()
6225 /* we can't nest cfs_b->lock while distributing bandwidth */ in do_sched_cfs_period_timer()
6227 raw_spin_lock_irqsave(&cfs_b->lock, flags); in do_sched_cfs_period_timer()
6236 cfs_b->idle = 0; in do_sched_cfs_period_timer()
6254 * Requires cfs_b->lock for hrtimer_expires_remaining to be safe against the
6260 struct hrtimer *refresh_timer = &cfs_b->period_timer; in runtime_refresh_within()
6263 /* if the call-back is running a quota refresh is already occurring */ in runtime_refresh_within()
6284 if (cfs_b->slack_started) in start_cfs_slack_bandwidth()
6286 cfs_b->slack_started = true; in start_cfs_slack_bandwidth()
6288 hrtimer_start(&cfs_b->slack_timer, in start_cfs_slack_bandwidth()
6296 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in __return_cfs_rq_runtime()
6297 s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime; in __return_cfs_rq_runtime()
6302 raw_spin_lock(&cfs_b->lock); in __return_cfs_rq_runtime()
6303 if (cfs_b->quota != RUNTIME_INF) { in __return_cfs_rq_runtime()
6304 cfs_b->runtime += slack_runtime; in __return_cfs_rq_runtime()
6306 /* we are under rq->lock, defer unthrottling using a timer */ in __return_cfs_rq_runtime()
6307 if (cfs_b->runtime > sched_cfs_bandwidth_slice() && in __return_cfs_rq_runtime()
6308 !list_empty(&cfs_b->throttled_cfs_rq)) in __return_cfs_rq_runtime()
6311 raw_spin_unlock(&cfs_b->lock); in __return_cfs_rq_runtime()
6314 cfs_rq->runtime_remaining -= slack_runtime; in __return_cfs_rq_runtime()
6322 if (!cfs_rq->runtime_enabled || cfs_rq->nr_queued) in return_cfs_rq_runtime()
6330 * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
6338 raw_spin_lock_irqsave(&cfs_b->lock, flags); in do_sched_cfs_slack_timer()
6339 cfs_b->slack_started = false; in do_sched_cfs_slack_timer()
6342 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in do_sched_cfs_slack_timer()
6346 if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) in do_sched_cfs_slack_timer()
6347 runtime = cfs_b->runtime; in do_sched_cfs_slack_timer()
6349 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in do_sched_cfs_slack_timer()
6360 * runtime as update_curr() throttling can not trigger until it's on-rq.
6367 /* an active group must be handled by the update_curr()->put() path */ in check_enqueue_throttle()
6368 if (!cfs_rq->runtime_enabled || cfs_rq->curr) in check_enqueue_throttle()
6377 if (cfs_rq->runtime_remaining <= 0) in check_enqueue_throttle()
6388 if (!tg->parent) in sync_throttle()
6391 cfs_rq = tg->cfs_rq[cpu]; in sync_throttle()
6392 pcfs_rq = tg->parent->cfs_rq[cpu]; in sync_throttle()
6394 cfs_rq->throttle_count = pcfs_rq->throttle_count; in sync_throttle()
6395 cfs_rq->throttled_clock_pelt = rq_clock_pelt(cpu_rq(cpu)); in sync_throttle()
6404 if (cfs_rq->throttle_count) in sync_throttle()
6405 cfs_rq->pelt_clock_throttled = 1; in sync_throttle()
6414 if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0)) in check_cfs_rq_runtime()
6446 raw_spin_lock_irqsave(&cfs_b->lock, flags); in sched_cfs_period_timer()
6448 overrun = hrtimer_forward_now(timer, cfs_b->period); in sched_cfs_period_timer()
6455 u64 new, old = ktime_to_ns(cfs_b->period); in sched_cfs_period_timer()
6464 cfs_b->period = ns_to_ktime(new); in sched_cfs_period_timer()
6465 cfs_b->quota *= 2; in sched_cfs_period_timer()
6466 cfs_b->burst *= 2; in sched_cfs_period_timer()
6472 div_u64(cfs_b->quota, NSEC_PER_USEC)); in sched_cfs_period_timer()
6478 div_u64(cfs_b->quota, NSEC_PER_USEC)); in sched_cfs_period_timer()
6486 cfs_b->period_active = 0; in sched_cfs_period_timer()
6487 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in sched_cfs_period_timer()
6494 raw_spin_lock_init(&cfs_b->lock); in init_cfs_bandwidth()
6495 cfs_b->runtime = 0; in init_cfs_bandwidth()
6496 cfs_b->quota = RUNTIME_INF; in init_cfs_bandwidth()
6497 cfs_b->period = us_to_ktime(default_bw_period_us()); in init_cfs_bandwidth()
6498 cfs_b->burst = 0; in init_cfs_bandwidth()
6499 cfs_b->hierarchical_quota = parent ? parent->hierarchical_quota : RUNTIME_INF; in init_cfs_bandwidth()
6501 INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq); in init_cfs_bandwidth()
6502 hrtimer_setup(&cfs_b->period_timer, sched_cfs_period_timer, CLOCK_MONOTONIC, in init_cfs_bandwidth()
6506 hrtimer_set_expires(&cfs_b->period_timer, in init_cfs_bandwidth()
6507 get_random_u32_below(cfs_b->period)); in init_cfs_bandwidth()
6508 hrtimer_setup(&cfs_b->slack_timer, sched_cfs_slack_timer, CLOCK_MONOTONIC, in init_cfs_bandwidth()
6510 cfs_b->slack_started = false; in init_cfs_bandwidth()
6515 cfs_rq->runtime_enabled = 0; in init_cfs_rq_runtime()
6516 INIT_LIST_HEAD(&cfs_rq->throttled_list); in init_cfs_rq_runtime()
6517 INIT_LIST_HEAD(&cfs_rq->throttled_csd_list); in init_cfs_rq_runtime()
6518 INIT_LIST_HEAD(&cfs_rq->throttled_limbo_list); in init_cfs_rq_runtime()
6523 lockdep_assert_held(&cfs_b->lock); in start_cfs_bandwidth()
6525 if (cfs_b->period_active) in start_cfs_bandwidth()
6528 cfs_b->period_active = 1; in start_cfs_bandwidth()
6529 hrtimer_forward_now(&cfs_b->period_timer, cfs_b->period); in start_cfs_bandwidth()
6530 hrtimer_start_expires(&cfs_b->period_timer, HRTIMER_MODE_ABS_PINNED); in start_cfs_bandwidth()
6538 if (!cfs_b->throttled_cfs_rq.next) in destroy_cfs_bandwidth()
6541 hrtimer_cancel(&cfs_b->period_timer); in destroy_cfs_bandwidth()
6542 hrtimer_cancel(&cfs_b->slack_timer); in destroy_cfs_bandwidth()
6558 if (list_empty(&rq->cfsb_csd_list)) in destroy_cfs_bandwidth()
6583 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth; in update_runtime_enabled()
6584 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in update_runtime_enabled()
6586 raw_spin_lock(&cfs_b->lock); in update_runtime_enabled()
6587 cfs_rq->runtime_enabled = cfs_b->quota != RUNTIME_INF; in update_runtime_enabled()
6588 raw_spin_unlock(&cfs_b->lock); in update_runtime_enabled()
6613 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in unthrottle_offline_cfs_rqs()
6615 if (!cfs_rq->runtime_enabled) in unthrottle_offline_cfs_rqs()
6622 cfs_rq->runtime_enabled = 0; in unthrottle_offline_cfs_rqs()
6631 cfs_rq->runtime_remaining = 1; in unthrottle_offline_cfs_rqs()
6646 if (cfs_rq->runtime_enabled || in cfs_task_bw_constrained()
6647 tg_cfs_bandwidth(cfs_rq->tg)->hierarchical_quota != RUNTIME_INF) in cfs_task_bw_constrained()
6665 if (rq->nr_running != 1) in sched_fair_update_stop_tick()
6743 struct sched_entity *se = &p->se; in hrtick_start_fair()
6747 if (rq->cfs.h_nr_queued > 1) { in hrtick_start_fair()
6748 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime; in hrtick_start_fair()
6749 u64 slice = se->slice; in hrtick_start_fair()
6750 s64 delta = slice - ran; in hrtick_start_fair()
6768 struct task_struct *donor = rq->donor; in hrtick_update()
6770 if (!hrtick_enabled_fair(rq) || donor->sched_class != &fair_sched_class) in hrtick_update()
6805 return !sched_energy_enabled() || READ_ONCE(rd->overutilized); in is_rd_overutilized()
6813 WRITE_ONCE(rd->overutilized, flag); in set_rd_overutilized()
6824 if (!is_rd_overutilized(rq->rd) && cpu_overutilized(rq->cpu)) in check_update_overutilized_status()
6825 set_rd_overutilized(rq->rd, 1); in check_update_overutilized_status()
6831 return unlikely(rq->nr_running == rq->cfs.h_nr_idle && in sched_idle_rq()
6832 rq->nr_running); in sched_idle_rq()
6846 * se->sched_delayed should imply: se->on_rq == 1. in requeue_delayed_entity()
6850 WARN_ON_ONCE(!se->sched_delayed); in requeue_delayed_entity()
6851 WARN_ON_ONCE(!se->on_rq); in requeue_delayed_entity()
6855 if (se->vlag > 0) { in requeue_delayed_entity()
6856 cfs_rq->nr_queued--; in requeue_delayed_entity()
6857 if (se != cfs_rq->curr) in requeue_delayed_entity()
6859 se->vlag = 0; in requeue_delayed_entity()
6861 if (se != cfs_rq->curr) in requeue_delayed_entity()
6863 cfs_rq->nr_queued++; in requeue_delayed_entity()
6880 struct sched_entity *se = &p->se; in enqueue_task_fair()
6884 int rq_h_nr_queued = rq->cfs.h_nr_queued; in enqueue_task_fair()
6896 if (!p->se.sched_delayed || (flags & ENQUEUE_DELAYED)) in enqueue_task_fair()
6897 util_est_enqueue(&rq->cfs, p); in enqueue_task_fair()
6909 if (p->in_iowait) in enqueue_task_fair()
6912 if (task_new && se->sched_delayed) in enqueue_task_fair()
6916 if (se->on_rq) { in enqueue_task_fair()
6917 if (se->sched_delayed) in enqueue_task_fair()
6926 * its entities in the desired time-frame. in enqueue_task_fair()
6929 se->slice = slice; in enqueue_task_fair()
6930 se->custom_slice = 1; in enqueue_task_fair()
6935 cfs_rq->h_nr_runnable += h_nr_runnable; in enqueue_task_fair()
6936 cfs_rq->h_nr_queued++; in enqueue_task_fair()
6937 cfs_rq->h_nr_idle += h_nr_idle; in enqueue_task_fair()
6952 se->slice = slice; in enqueue_task_fair()
6953 if (se != cfs_rq->curr) in enqueue_task_fair()
6954 min_vruntime_cb_propagate(&se->run_node, NULL); in enqueue_task_fair()
6957 cfs_rq->h_nr_runnable += h_nr_runnable; in enqueue_task_fair()
6958 cfs_rq->h_nr_queued++; in enqueue_task_fair()
6959 cfs_rq->h_nr_idle += h_nr_idle; in enqueue_task_fair()
6965 if (!rq_h_nr_queued && rq->cfs.h_nr_queued) in enqueue_task_fair()
6966 dl_server_start(&rq->fair_server); in enqueue_task_fair()
6995 * failing half-way through and resume the dequeue later.
6998 * -1 - dequeue delayed
6999 * 0 - dequeue throttled
7000 * 1 - dequeue complete
7019 if (task_sleep || task_delayed || !se->sched_delayed) in dequeue_entities()
7027 if (p && &p->se == se) in dequeue_entities()
7028 return -1; in dequeue_entities()
7034 cfs_rq->h_nr_runnable -= h_nr_runnable; in dequeue_entities()
7035 cfs_rq->h_nr_queued -= h_nr_queued; in dequeue_entities()
7036 cfs_rq->h_nr_idle -= h_nr_idle; in dequeue_entities()
7045 if (cfs_rq->load.weight) { in dequeue_entities()
7048 /* Avoid re-evaluating load for this entity: */ in dequeue_entities()
7069 se->slice = slice; in dequeue_entities()
7070 if (se != cfs_rq->curr) in dequeue_entities()
7071 min_vruntime_cb_propagate(&se->run_node, NULL); in dequeue_entities()
7074 cfs_rq->h_nr_runnable -= h_nr_runnable; in dequeue_entities()
7075 cfs_rq->h_nr_queued -= h_nr_queued; in dequeue_entities()
7076 cfs_rq->h_nr_idle -= h_nr_idle; in dequeue_entities()
7089 rq->next_balance = jiffies; in dequeue_entities()
7093 WARN_ON_ONCE(p->on_rq != 1); in dequeue_entities()
7095 /* Fix-up what dequeue_task_fair() skipped */ in dequeue_entities()
7099 * Fix-up what block_task() skipped. in dequeue_entities()
7121 if (!p->se.sched_delayed) in dequeue_task_fair()
7122 util_est_dequeue(&rq->cfs, p); in dequeue_task_fair()
7124 util_est_update(&rq->cfs, p, flags & DEQUEUE_SLEEP); in dequeue_task_fair()
7125 if (dequeue_entities(rq, &p->se, flags) < 0) in dequeue_task_fair()
7138 return (rq->cfs.h_nr_queued - rq->cfs.h_nr_runnable); in cfs_h_nr_delayed()
7161 return cfs_rq_load_avg(&rq->cfs); in cpu_load()
7165 * cpu_load_without - compute CPU load without any contributions from *p
7183 if (cpu_of(rq) != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in cpu_load_without()
7186 cfs_rq = &rq->cfs; in cpu_load_without()
7187 load = READ_ONCE(cfs_rq->avg.load_avg); in cpu_load_without()
7197 return cfs_rq_runnable_avg(&rq->cfs); in cpu_runnable()
7206 if (cpu_of(rq) != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in cpu_runnable_without()
7209 cfs_rq = &rq->cfs; in cpu_runnable_without()
7210 runnable = READ_ONCE(cfs_rq->avg.runnable_avg); in cpu_runnable_without()
7213 lsub_positive(&runnable, p->se.avg.runnable_avg); in cpu_runnable_without()
7220 return cpu_rq(cpu)->cpu_capacity; in capacity_of()
7229 if (time_after(jiffies, current->wakee_flip_decay_ts + HZ)) { in record_wakee()
7230 current->wakee_flips >>= 1; in record_wakee()
7231 current->wakee_flip_decay_ts = jiffies; in record_wakee()
7234 if (current->last_wakee != p) { in record_wakee()
7235 current->last_wakee = p; in record_wakee()
7236 current->wakee_flips++; in record_wakee()
7241 * Detect M:N waker/wakee relationships via a switching-frequency heuristic.
7251 * non-monogamous, with partner count exceeding socket size.
7259 unsigned int master = current->wakee_flips; in wake_wide()
7260 unsigned int slave = p->wakee_flips; in wake_wide()
7275 * wake_affine_idle() - only considers 'now', it check if the waking CPU is
7276 * cache-affine and is (or will be) idle.
7278 * wake_affine_weight() - considers the weight to reflect the average
7303 if ((rq->nr_running - cfs_h_nr_delayed(rq)) == 1) in wake_affine_idle()
7328 this_eff_load -= current_load; in wake_affine_weight()
7339 prev_eff_load -= task_load; in wake_affine_weight()
7341 prev_eff_load *= 100 + (sd->imbalance_pct - 100) / 2; in wake_affine_weight()
7367 schedstat_inc(p->stats.nr_wakeups_affine_attempts); in wake_affine()
7371 schedstat_inc(sd->ttwu_move_affine); in wake_affine()
7372 schedstat_inc(p->stats.nr_wakeups_affine); in wake_affine()
7380 * sched_balance_find_dst_group_cpu - find the idlest CPU among the CPUs in the group.
7389 int shallowest_idle_cpu = -1; in sched_balance_find_dst_group_cpu()
7393 if (group->group_weight == 1) in sched_balance_find_dst_group_cpu()
7397 for_each_cpu_and(i, sched_group_span(group), p->cpus_ptr) { in sched_balance_find_dst_group_cpu()
7408 if (idle && idle->exit_latency < min_exit_latency) { in sched_balance_find_dst_group_cpu()
7414 min_exit_latency = idle->exit_latency; in sched_balance_find_dst_group_cpu()
7415 latest_idle_timestamp = rq->idle_stamp; in sched_balance_find_dst_group_cpu()
7417 } else if ((!idle || idle->exit_latency == min_exit_latency) && in sched_balance_find_dst_group_cpu()
7418 rq->idle_stamp > latest_idle_timestamp) { in sched_balance_find_dst_group_cpu()
7424 latest_idle_timestamp = rq->idle_stamp; in sched_balance_find_dst_group_cpu()
7427 } else if (shallowest_idle_cpu == -1) { in sched_balance_find_dst_group_cpu()
7436 return shallowest_idle_cpu != -1 ? shallowest_idle_cpu : least_loaded_cpu; in sched_balance_find_dst_group_cpu()
7444 if (!cpumask_intersects(sched_domain_span(sd), p->cpus_ptr)) in sched_balance_find_dst_cpu()
7452 sync_entity_load_avg(&p->se); in sched_balance_find_dst_cpu()
7459 if (!(sd->flags & sd_flag)) { in sched_balance_find_dst_cpu()
7460 sd = sd->child; in sched_balance_find_dst_cpu()
7466 sd = sd->child; in sched_balance_find_dst_cpu()
7473 sd = sd->child; in sched_balance_find_dst_cpu()
7479 weight = sd->span_weight; in sched_balance_find_dst_cpu()
7482 if (weight <= tmp->span_weight) in sched_balance_find_dst_cpu()
7484 if (tmp->flags & sd_flag) in sched_balance_find_dst_cpu()
7498 return -1; in __select_idle_cpu()
7511 WRITE_ONCE(sds->has_idle_cores, val); in set_idle_cores()
7520 return READ_ONCE(sds->has_idle_cores); in test_idle_cores()
7527 * information in sd_llc_shared->has_idle_cores.
7557 * sd_llc->shared->has_idle_cores and enabled through update_idle_core() above.
7567 if (*idle_cpu == -1) { in select_idle_core()
7576 if (*idle_cpu == -1 && cpumask_test_cpu(cpu, cpus)) in select_idle_core()
7584 return -1; in select_idle_core()
7594 for_each_cpu_and(cpu, cpu_smt_mask(target), p->cpus_ptr) { in select_idle_smt()
7607 return -1; in select_idle_smt()
7628 return -1; in select_idle_smt()
7635 * comparing the average scan cost (tracked in sd->avg_scan_cost) against the
7636 * average idle time for this rq (as found in rq->avg_idle).
7641 int i, cpu, idle_cpu = -1, nr = INT_MAX; in select_idle_cpu()
7644 cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr); in select_idle_cpu()
7649 /* because !--nr is the condition to stop scan */ in select_idle_cpu()
7650 nr = READ_ONCE(sd_share->nr_idle_scan) + 1; in select_idle_cpu()
7653 return -1; in select_idle_cpu()
7658 struct sched_group *sg = sd->groups; in select_idle_cpu()
7660 if (sg->flags & SD_CLUSTER) { in select_idle_cpu()
7670 if (--nr <= 0) in select_idle_cpu()
7671 return -1; in select_idle_cpu()
7688 if (--nr <= 0) in select_idle_cpu()
7689 return -1; in select_idle_cpu()
7712 int cpu, best_cpu = -1; in select_idle_capacity()
7716 cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr); in select_idle_capacity()
7741 * First, select CPU which fits better (-1 being better than 0). in select_idle_capacity()
7778 int i, recent_used_cpu, prev_aff = -1; in select_idle_sibling()
7785 sync_entity_load_avg(&p->se); in select_idle_sibling()
7792 * per-cpu select_rq_mask usage in select_idle_sibling()
7815 * Allow a per-cpu kthread to stack with the wakee if the in select_idle_sibling()
7818 * per-cpu kthread that is now complete and the wakeup is in select_idle_sibling()
7825 this_rq()->nr_running <= 1 && in select_idle_sibling()
7831 recent_used_cpu = p->recent_used_cpu; in select_idle_sibling()
7832 p->recent_used_cpu = prev; in select_idle_sibling()
7837 cpumask_test_cpu(recent_used_cpu, p->cpus_ptr) && in select_idle_sibling()
7845 recent_used_cpu = -1; in select_idle_sibling()
7901 * cpu_util() - Estimates the amount of CPU capacity used by CFS tasks.
7904 * @dst_cpu: CPU @p migrates to, -1 if @p moves from @cpu or @p == NULL
7911 * recent utilization of currently non-runnable tasks on that CPU.
7919 * previously-executed tasks, which helps better deduce how busy a CPU will
7920 * be when a long-sleeping task wakes up. The contribution to CPU utilization
7934 * could be seen as over-utilized even though CPU1 has 20% of spare CPU
7937 * after task migrations (scheduler-driven DVFS).
7944 struct cfs_rq *cfs_rq = &cpu_rq(cpu)->cfs; in cpu_util()
7945 unsigned long util = READ_ONCE(cfs_rq->avg.util_avg); in cpu_util()
7949 runnable = READ_ONCE(cfs_rq->avg.runnable_avg); in cpu_util()
7954 * If @dst_cpu is -1 or @p migrates from @cpu to @dst_cpu remove its in cpu_util()
7967 util_est = READ_ONCE(cfs_rq->avg.util_est); in cpu_util()
7970 * During wake-up @p isn't enqueued yet and doesn't contribute in cpu_util()
7971 * to any cpu_rq(cpu)->cfs.avg.util_est. in cpu_util()
7975 * During exec (@dst_cpu = -1) @p is enqueued and does in cpu_util()
7976 * contribute to cpu_rq(cpu)->cfs.util_est. in cpu_util()
7985 * p->on_rq = TASK_ON_RQ_MIGRATING; in cpu_util()
7986 * -------------------------------- A in cpu_util()
7990 * -------------------------------- B in cpu_util()
8008 return cpu_util(cpu, NULL, -1, 0); in cpu_util_cfs()
8013 return cpu_util(cpu, NULL, -1, 1); in cpu_util_cfs_boost()
8032 if (cpu != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in cpu_util_without()
8035 return cpu_util(cpu, p, -1, 0); in cpu_util_without()
8050 * The cfs,rt,dl utilization are the running times measured with rq->clock_task
8051 * which excludes things like IRQ and steal-time. These latter are then accrued
8069 * because of inaccuracies in how we track these -- see in effective_cpu_util()
8084 * - the computed DL bandwidth needed with the IRQ pressure which in effective_cpu_util()
8086 * - The minimum performance requirement for CFS and/or RT. in effective_cpu_util()
8094 if (!uclamp_is_used() && rt_rq_is_runnable(&rq->rt)) in effective_cpu_util()
8122 * max - irq in effective_cpu_util()
8123 * U' = irq + --------- * U in effective_cpu_util()
8138 * energy_env - Utilization landscape for energy estimation.
8144 * @pd_cap: Entire perf domain capacity. (pd->nr_cpus * cpu_cap).
8170 eenv->task_busy_time = busy_time; in eenv_task_busy_time()
8184 * - A stable PD utilization, no matter which CPU of that PD we want to place
8187 * - A fair comparison between CPUs as the task contribution (task_util())
8192 * exceed @eenv->pd_cap.
8202 unsigned long util = cpu_util(cpu, p, -1, 0); in eenv_pd_busy_time()
8207 eenv->pd_busy_time = min(eenv->pd_cap, busy_time); in eenv_pd_busy_time()
8214 * Returns the maximum utilization among @eenv->cpus. This utilization can't
8215 * exceed @eenv->cpu_cap.
8256 return min(max_util, eenv->cpu_cap); in eenv_pd_max_util()
8269 unsigned long busy_time = eenv->pd_busy_time; in compute_energy()
8273 busy_time = min(eenv->pd_cap, busy_time + eenv->task_busy_time); in compute_energy()
8275 energy = em_cpu_energy(pd->em_pd, max_util, busy_time, eenv->cpu_cap); in compute_energy()
8283 * find_energy_efficient_cpu(): Find most energy-efficient target CPU for the
8287 * out which of the CPU candidates is the most energy-efficient.
8304 * cluster-packing, and spreading inside a cluster. That should at least be
8311 * NOTE: Forkees are not accepted in the energy-aware wake-up path because
8315 * to be energy-inefficient in some use-cases. The alternative would be to
8318 * other use-cases too. So, until someone finds a better way to solve this,
8319 * let's keep things simple by re-using the existing slow path.
8327 struct root_domain *rd = this_rq()->rd; in find_energy_efficient_cpu()
8328 int cpu, best_energy_cpu, target = -1; in find_energy_efficient_cpu()
8329 int prev_fits = -1, best_fits = -1; in find_energy_efficient_cpu()
8337 pd = rcu_dereference(rd->pd); in find_energy_efficient_cpu()
8342 * Energy-aware wake-up happens on the lowest sched_domain starting in find_energy_efficient_cpu()
8347 sd = sd->parent; in find_energy_efficient_cpu()
8353 sync_entity_load_avg(&p->se); in find_energy_efficient_cpu()
8359 for (; pd; pd = pd->next) { in find_energy_efficient_cpu()
8362 long prev_spare_cap = -1, max_spare_cap = -1; in find_energy_efficient_cpu()
8365 int max_spare_cap_cpu = -1; in find_energy_efficient_cpu()
8366 int fits, max_fits = -1; in find_energy_efficient_cpu()
8388 if (!cpumask_test_cpu(cpu, p->cpus_ptr)) in find_energy_efficient_cpu()
8403 * Open code uclamp_rq_util_with() except for in find_energy_efficient_cpu()
8407 * max-aggregated uclamp_{min, max}. in find_energy_efficient_cpu()
8444 base_energy = compute_energy(&eenv, pd, cpus, p, -1); in find_energy_efficient_cpu()
8447 if (prev_spare_cap > -1) { in find_energy_efficient_cpu()
8453 prev_delta -= base_energy; in find_energy_efficient_cpu()
8477 cur_delta -= base_energy; in find_energy_efficient_cpu()
8521 int sync = (wake_flags & WF_SYNC) && !(current->flags & PF_EXITING); in select_task_rq_fair()
8530 * required for stable ->cpus_allowed in select_task_rq_fair()
8532 lockdep_assert_held(&p->pi_lock); in select_task_rq_fair()
8537 cpumask_test_cpu(cpu, p->cpus_ptr)) in select_task_rq_fair()
8540 if (!is_rd_overutilized(this_rq()->rd)) { in select_task_rq_fair()
8547 want_affine = !wake_wide(p) && cpumask_test_cpu(cpu, p->cpus_ptr); in select_task_rq_fair()
8556 if (want_affine && (tmp->flags & SD_WAKE_AFFINE) && in select_task_rq_fair()
8570 if (tmp->flags & sd_flag) in select_task_rq_fair()
8591 * previous CPU. The caller guarantees p->pi_lock or task_rq(p)->lock is held.
8595 struct sched_entity *se = &p->se; in migrate_task_rq_fair()
8604 * leading to an inflation after wake-up on the new rq. in migrate_task_rq_fair()
8614 se->avg.last_update_time = 0; in migrate_task_rq_fair()
8621 struct sched_entity *se = &p->se; in task_dead_fair()
8623 if (se->sched_delayed) { in task_dead_fair()
8628 if (se->sched_delayed) { in task_dead_fair()
8653 if (!cpumask_intersects(p->cpus_ptr, cpumask)) in set_task_max_allowed_capacity()
8656 p->max_allowed_capacity = entry->capacity; in set_task_max_allowed_capacity()
8671 if (WARN_ON_ONCE(!se->on_rq)) in set_next_buddy()
8675 cfs_rq_of(se)->next = se; in set_next_buddy()
8696 if (cfs_rq->next && entity_before(cfs_rq->next, pse)) in set_preempt_buddy()
8721 delta = rq_clock_task(rq) - se->exec_start; in preempt_sync()
8750 struct task_struct *donor = rq->donor; in check_preempt_wakeup_fair()
8751 struct sched_entity *se = &donor->se, *pse = &p->se; in check_preempt_wakeup_fair()
8762 * next-buddy nomination below. in check_preempt_wakeup_fair()
8771 * Note: this also catches the edge-case of curr being in a throttled in check_preempt_wakeup_fair()
8777 if (test_tsk_need_resched(rq->curr)) in check_preempt_wakeup_fair()
8790 * Preempt an idle entity in favor of a non-idle entity (and don't preempt in check_preempt_wakeup_fair()
8795 * When non-idle entity preempt an idle entity, in check_preempt_wakeup_fair()
8808 if (unlikely(!normal_policy(p->policy))) in check_preempt_wakeup_fair()
8817 if (sched_feat(PREEMPT_SHORT) && (pse->slice < se->slice)) { in check_preempt_wakeup_fair()
8828 if ((wake_flags & WF_FORK) || pse->sched_delayed) in check_preempt_wakeup_fair()
8892 cfs_rq = &rq->cfs; in pick_task_fair()
8893 if (!cfs_rq->nr_queued) in pick_task_fair()
8900 if (cfs_rq->curr && cfs_rq->curr->on_rq) in pick_task_fair()
8931 se = &p->se; in pick_next_task_fair()
8934 if (prev->sched_class != &fair_sched_class) in pick_next_task_fair()
8951 struct sched_entity *pse = &prev->se; in pick_next_task_fair()
8955 int se_depth = se->depth; in pick_next_task_fair()
8956 int pse_depth = pse->depth; in pick_next_task_fair()
8986 * Because sched_balance_newidle() releases (and re-acquires) in pick_next_task_fair()
8987 * rq->lock, it is possible for any higher priority task to in pick_next_task_fair()
8988 * appear. In that case we must re-start the pick_next_entity() in pick_next_task_fair()
9010 return pick_task_fair(dl_se->rq, rf); in fair_server_pick_task()
9015 struct sched_dl_entity *dl_se = &rq->fair_server; in fair_server_init()
9027 struct sched_entity *se = &prev->se; in put_prev_task_fair()
9041 struct task_struct *curr = rq->donor; in yield_task_fair()
9043 struct sched_entity *se = &curr->se; in yield_task_fair()
9048 if (unlikely(rq->nr_running == 1)) in yield_task_fair()
9055 * Update run-time statistics of the 'current'. in yield_task_fair()
9074 se->vruntime = se->deadline; in yield_task_fair()
9075 se->deadline += calc_delta_fair(se->slice, se); in yield_task_fair()
9081 struct sched_entity *se = &p->se; in yield_to_task_fair()
9083 /* !se->on_rq also covers throttled task */ in yield_to_task_fair()
9084 if (!se->on_rq) in yield_to_task_fair()
9096 * Fair scheduling class load-balancing methods.
9100 * The purpose of load-balancing is to achieve the same basic fairness the
9101 * per-CPU scheduler provides, namely provide a proportional amount of compute
9106 * Where W_i,n is the n-th weight average for CPU i. The instantaneous weight
9111 * Where w_i,j is the weight of the j-th runnable task on CPU i. This weight
9117 * W'_i,n = (2^n - 1) / 2^n * W_i,n + 1 / 2^n * W_i,0 (3)
9126 * imb_i,j = max{ avg(W/C), W_i/C_i } - min{ avg(W/C), W_j/C_j } (4)
9133 * - infeasible weights;
9134 * - local vs global optima in the discrete case. ]
9144 * of load-balance at each level inversely proportional to the number of CPUs in
9150 * \Sum { --- * --- * 2^i } = O(n) (5)
9152 * `- size of each group
9153 * | | `- number of CPUs doing load-balance
9154 * | `- freq
9155 * `- sum over all levels
9197 * W_i,0 = \Sum_j \Prod_k w_k * ----- (9)
9204 * w_i,j,k is the weight of the j-th runnable task in the k-th cgroup on CPU i.
9286 /* The set of CPUs under consideration for load-balancing */
9301 * Is this task likely cache-hot:
9307 lockdep_assert_rq_held(env->src_rq); in task_hot()
9309 if (p->sched_class != &fair_sched_class) in task_hot()
9316 if (env->sd->flags & SD_SHARE_CPUCAPACITY) in task_hot()
9322 if (sched_feat(CACHE_HOT_BUDDY) && env->dst_rq->nr_running && in task_hot()
9323 (&p->se == cfs_rq_of(&p->se)->next)) in task_hot()
9326 if (sysctl_sched_migration_cost == -1) in task_hot()
9333 if (!sched_core_cookie_match(cpu_rq(env->dst_cpu), p)) in task_hot()
9339 delta = rq_clock_task(env->src_rq) - p->se.exec_start; in task_hot()
9352 struct numa_group *numa_group = rcu_dereference(p->numa_group); in migrate_degrades_locality()
9359 if (!p->numa_faults || !(env->sd->flags & SD_NUMA)) in migrate_degrades_locality()
9362 src_nid = cpu_to_node(env->src_cpu); in migrate_degrades_locality()
9363 dst_nid = cpu_to_node(env->dst_cpu); in migrate_degrades_locality()
9369 if (src_nid == p->numa_preferred_nid) { in migrate_degrades_locality()
9370 if (env->src_rq->nr_running > env->src_rq->nr_preferred_running) in migrate_degrades_locality()
9377 if (dst_nid == p->numa_preferred_nid) in migrate_degrades_locality()
9378 return -1; in migrate_degrades_locality()
9381 if (env->idle == CPU_IDLE) in migrate_degrades_locality()
9393 return src_weight - dst_weight; in migrate_degrades_locality()
9408 * dst_cfs_rq->nr_queued is greater than 1, if the task
9417 dst_cfs_rq = task_group(p)->cfs_rq[dest_cpu]; in task_is_ineligible_on_dst_cpu()
9419 dst_cfs_rq = &cpu_rq(dest_cpu)->cfs; in task_is_ineligible_on_dst_cpu()
9421 if (sched_feat(PLACE_LAG) && dst_cfs_rq->nr_queued && in task_is_ineligible_on_dst_cpu()
9422 !entity_eligible(task_cfs_rq(p), &p->se)) in task_is_ineligible_on_dst_cpu()
9429 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
9436 lockdep_assert_rq_held(env->src_rq); in can_migrate_task()
9437 if (p->sched_task_hot) in can_migrate_task()
9438 p->sched_task_hot = 0; in can_migrate_task()
9446 * 5) are cache-hot on their current CPU, or in can_migrate_task()
9449 if ((p->se.sched_delayed) && (env->migration_type != migrate_load)) in can_migrate_task()
9452 if (lb_throttled_hierarchy(p, env->dst_cpu)) in can_migrate_task()
9457 * For ineligible tasks we soft-limit them and only allow in can_migrate_task()
9458 * them to migrate when nr_balance_failed is non-zero to in can_migrate_task()
9459 * avoid load-balancing trying very hard to balance the load. in can_migrate_task()
9461 if (!env->sd->nr_balance_failed && in can_migrate_task()
9462 task_is_ineligible_on_dst_cpu(p, env->dst_cpu)) in can_migrate_task()
9472 if (!cpumask_test_cpu(env->dst_cpu, p->cpus_ptr)) { in can_migrate_task()
9475 schedstat_inc(p->stats.nr_failed_migrations_affine); in can_migrate_task()
9477 env->flags |= LBF_SOME_PINNED; in can_migrate_task()
9485 * - for NEWLY_IDLE in can_migrate_task()
9486 * - if we have already computed one in current iteration in can_migrate_task()
9487 * - if it's an active balance in can_migrate_task()
9489 if (env->idle == CPU_NEWLY_IDLE || in can_migrate_task()
9490 env->flags & (LBF_DST_PINNED | LBF_ACTIVE_LB)) in can_migrate_task()
9493 /* Prevent to re-select dst_cpu via env's CPUs: */ in can_migrate_task()
9494 cpu = cpumask_first_and_and(env->dst_grpmask, env->cpus, p->cpus_ptr); in can_migrate_task()
9497 env->flags |= LBF_DST_PINNED; in can_migrate_task()
9498 env->new_dst_cpu = cpu; in can_migrate_task()
9505 env->flags &= ~LBF_ALL_PINNED; in can_migrate_task()
9507 if (task_on_cpu(env->src_rq, p) || in can_migrate_task()
9508 task_current_donor(env->src_rq, p)) { in can_migrate_task()
9509 schedstat_inc(p->stats.nr_failed_migrations_running); in can_migrate_task()
9520 if (env->flags & LBF_ACTIVE_LB) in can_migrate_task()
9529 if (!hot || env->sd->nr_balance_failed > env->sd->cache_nice_tries) { in can_migrate_task()
9531 p->sched_task_hot = 1; in can_migrate_task()
9535 schedstat_inc(p->stats.nr_failed_migrations_hot); in can_migrate_task()
9540 * detach_task() -- detach the task for the migration specified in env
9544 lockdep_assert_rq_held(env->src_rq); in detach_task()
9546 if (p->sched_task_hot) { in detach_task()
9547 p->sched_task_hot = 0; in detach_task()
9548 schedstat_inc(env->sd->lb_hot_gained[env->idle]); in detach_task()
9549 schedstat_inc(p->stats.nr_forced_migrations); in detach_task()
9552 WARN_ON(task_current(env->src_rq, p)); in detach_task()
9553 WARN_ON(task_current_donor(env->src_rq, p)); in detach_task()
9555 deactivate_task(env->src_rq, p, DEQUEUE_NOCLOCK); in detach_task()
9556 set_task_cpu(p, env->dst_cpu); in detach_task()
9560 * detach_one_task() -- tries to dequeue exactly one task from env->src_rq, as
9569 lockdep_assert_rq_held(env->src_rq); in detach_one_task()
9572 &env->src_rq->cfs_tasks, se.group_node) { in detach_one_task()
9580 * lb_gained[env->idle] is updated (other is detach_tasks) in detach_one_task()
9584 schedstat_inc(env->sd->lb_gained[env->idle]); in detach_one_task()
9591 * detach_tasks() -- tries to detach up to imbalance load/util/tasks from
9598 struct list_head *tasks = &env->src_rq->cfs_tasks; in detach_tasks()
9603 lockdep_assert_rq_held(env->src_rq); in detach_tasks()
9609 if (env->src_rq->nr_running <= 1) { in detach_tasks()
9610 env->flags &= ~LBF_ALL_PINNED; in detach_tasks()
9614 if (env->imbalance <= 0) in detach_tasks()
9622 if (env->idle && env->src_rq->nr_running <= 1) in detach_tasks()
9625 env->loop++; in detach_tasks()
9627 if (env->loop > env->loop_max) in detach_tasks()
9631 if (env->loop > env->loop_break) { in detach_tasks()
9632 env->loop_break += SCHED_NR_MIGRATE_BREAK; in detach_tasks()
9633 env->flags |= LBF_NEED_BREAK; in detach_tasks()
9642 switch (env->migration_type) { in detach_tasks()
9647 * value. Make sure that env->imbalance decreases in detach_tasks()
9654 load < 16 && !env->sd->nr_balance_failed) in detach_tasks()
9663 if (shr_bound(load, env->sd->nr_balance_failed) > env->imbalance) in detach_tasks()
9666 env->imbalance -= load; in detach_tasks()
9672 if (shr_bound(util, env->sd->nr_balance_failed) > env->imbalance) in detach_tasks()
9675 env->imbalance -= util; in detach_tasks()
9679 env->imbalance--; in detach_tasks()
9684 if (task_fits_cpu(p, env->src_cpu)) in detach_tasks()
9687 env->imbalance = 0; in detach_tasks()
9692 list_add(&p->se.group_node, &env->tasks); in detach_tasks()
9702 if (env->idle == CPU_NEWLY_IDLE) in detach_tasks()
9710 if (env->imbalance <= 0) in detach_tasks()
9715 if (p->sched_task_hot) in detach_tasks()
9716 schedstat_inc(p->stats.nr_failed_migrations_hot); in detach_tasks()
9718 list_move(&p->se.group_node, tasks); in detach_tasks()
9726 schedstat_add(env->sd->lb_gained[env->idle], detached); in detach_tasks()
9732 * attach_task() -- attach the task detached by detach_task() to its new rq.
9744 * attach_one_task() -- attaches the task returned from detach_one_task() to
9758 * attach_tasks() -- attaches all tasks detached by detach_tasks() to their
9763 struct list_head *tasks = &env->tasks; in attach_tasks()
9767 rq_lock(env->dst_rq, &rf); in attach_tasks()
9768 update_rq_clock(env->dst_rq); in attach_tasks()
9772 list_del_init(&p->se.group_node); in attach_tasks()
9774 attach_task(env->dst_rq, p); in attach_tasks()
9777 rq_unlock(env->dst_rq, &rf); in attach_tasks()
9783 if (cfs_rq->avg.load_avg) in cfs_rq_has_blocked()
9786 if (cfs_rq->avg.util_avg) in cfs_rq_has_blocked()
9811 WRITE_ONCE(rq->last_blocked_load_update_tick, jiffies); in update_blocked_load_tick()
9817 rq->has_blocked_load = 0; in update_blocked_load_status()
9860 if (cfs_rq->nr_queued == 0) in __update_blocked_fair()
9863 if (cfs_rq == &rq->cfs) in __update_blocked_fair()
9868 se = cfs_rq->tg->se[cpu]; in __update_blocked_fair()
9889 * This needs to be done in a top-down fashion because the load of a child
9895 struct sched_entity *se = cfs_rq->tg->se[cpu_of(rq)]; in update_cfs_rq_h_load()
9899 if (cfs_rq->last_h_load_update == now) in update_cfs_rq_h_load()
9902 WRITE_ONCE(cfs_rq->h_load_next, NULL); in update_cfs_rq_h_load()
9905 WRITE_ONCE(cfs_rq->h_load_next, se); in update_cfs_rq_h_load()
9906 if (cfs_rq->last_h_load_update == now) in update_cfs_rq_h_load()
9911 cfs_rq->h_load = cfs_rq_load_avg(cfs_rq); in update_cfs_rq_h_load()
9912 cfs_rq->last_h_load_update = now; in update_cfs_rq_h_load()
9915 while ((se = READ_ONCE(cfs_rq->h_load_next)) != NULL) { in update_cfs_rq_h_load()
9916 load = cfs_rq->h_load; in update_cfs_rq_h_load()
9917 load = div64_ul(load * se->avg.load_avg, in update_cfs_rq_h_load()
9920 cfs_rq->h_load = load; in update_cfs_rq_h_load()
9921 cfs_rq->last_h_load_update = now; in update_cfs_rq_h_load()
9930 return div64_ul(p->se.avg.load_avg * cfs_rq->h_load, in task_h_load()
9936 struct cfs_rq *cfs_rq = &rq->cfs; in __update_blocked_fair()
9948 return p->se.avg.load_avg; in task_h_load()
9974 * sg_lb_stats - stats of a sched_group required for load-balancing:
9997 * sd_lb_stats - stats of a sched_domain required for load-balancing:
10054 free = max - used; in scale_rt_capacity()
10062 struct sched_group *sdg = sd->groups; in update_cpu_capacity()
10067 cpu_rq(cpu)->cpu_capacity = capacity; in update_cpu_capacity()
10070 sdg->sgc->capacity = capacity; in update_cpu_capacity()
10071 sdg->sgc->min_capacity = capacity; in update_cpu_capacity()
10072 sdg->sgc->max_capacity = capacity; in update_cpu_capacity()
10077 struct sched_domain *child = sd->child; in update_group_capacity()
10078 struct sched_group *group, *sdg = sd->groups; in update_group_capacity()
10082 interval = msecs_to_jiffies(sd->balance_interval); in update_group_capacity()
10084 sdg->sgc->next_update = jiffies + interval; in update_group_capacity()
10095 if (child->flags & SD_NUMA) { in update_group_capacity()
10114 group = child->groups; in update_group_capacity()
10116 struct sched_group_capacity *sgc = group->sgc; in update_group_capacity()
10118 capacity += sgc->capacity; in update_group_capacity()
10119 min_capacity = min(sgc->min_capacity, min_capacity); in update_group_capacity()
10120 max_capacity = max(sgc->max_capacity, max_capacity); in update_group_capacity()
10121 group = group->next; in update_group_capacity()
10122 } while (group != child->groups); in update_group_capacity()
10125 sdg->sgc->capacity = capacity; in update_group_capacity()
10126 sdg->sgc->min_capacity = min_capacity; in update_group_capacity()
10127 sdg->sgc->max_capacity = max_capacity; in update_group_capacity()
10138 return ((rq->cpu_capacity * sd->imbalance_pct) < in check_cpu_capacity()
10145 return rq->misfit_task_load; in check_misfit_status()
10150 * groups is inadequate due to ->cpus_ptr constraints.
10159 * If we were to balance group-wise we'd place two tasks in the first group and
10179 return group->sgc->imbalance; in sg_imbalanced()
10197 if (sgs->sum_nr_running < sgs->group_weight) in group_has_capacity()
10200 if ((sgs->group_capacity * imbalance_pct) < in group_has_capacity()
10201 (sgs->group_runnable * 100)) in group_has_capacity()
10204 if ((sgs->group_capacity * 100) > in group_has_capacity()
10205 (sgs->group_util * imbalance_pct)) in group_has_capacity()
10222 if (sgs->sum_nr_running <= sgs->group_weight) in group_is_overloaded()
10225 if ((sgs->group_capacity * 100) < in group_is_overloaded()
10226 (sgs->group_util * imbalance_pct)) in group_is_overloaded()
10229 if ((sgs->group_capacity * imbalance_pct) < in group_is_overloaded()
10230 (sgs->group_runnable * 100)) in group_is_overloaded()
10247 if (sgs->group_asym_packing) in group_classify()
10250 if (sgs->group_smt_balance) in group_classify()
10253 if (sgs->group_misfit_task_load) in group_classify()
10263 * sched_use_asym_prio - Check whether asym_packing priority must be used
10275 if (!(sd->flags & SD_ASYM_PACKING)) in sched_use_asym_prio()
10281 return sd->flags & SD_SHARE_CPUCAPACITY || is_core_idle(cpu); in sched_use_asym_prio()
10295 * sched_group_asym - Check if the destination CPU can do asym_packing balance
10297 * @sgs: Load-balancing statistics of the candidate busiest group
10313 if ((group->flags & SD_SHARE_CPUCAPACITY) && in sched_group_asym()
10314 (sgs->group_weight - sgs->idle_cpus != 1)) in sched_group_asym()
10317 return sched_asym(env->sd, env->dst_cpu, READ_ONCE(group->asym_prefer_cpu)); in sched_group_asym()
10327 return (sg1->flags & SD_SHARE_CPUCAPACITY) != in smt_vs_nonsmt_groups()
10328 (sg2->flags & SD_SHARE_CPUCAPACITY); in smt_vs_nonsmt_groups()
10334 if (!env->idle) in smt_balance()
10343 if (group->flags & SD_SHARE_CPUCAPACITY && in smt_balance()
10344 sgs->sum_h_nr_running > 1) in smt_balance()
10358 if (!env->idle || !busiest->sum_nr_running) in sibling_imbalance()
10361 ncores_busiest = sds->busiest->cores; in sibling_imbalance()
10362 ncores_local = sds->local->cores; in sibling_imbalance()
10365 imbalance = busiest->sum_nr_running; in sibling_imbalance()
10366 lsub_positive(&imbalance, local->sum_nr_running); in sibling_imbalance()
10371 imbalance = ncores_local * busiest->sum_nr_running; in sibling_imbalance()
10372 lsub_positive(&imbalance, ncores_busiest * local->sum_nr_running); in sibling_imbalance()
10378 if (imbalance <= 1 && local->sum_nr_running == 0 && in sibling_imbalance()
10379 busiest->sum_nr_running > 1) in sibling_imbalance()
10392 if (rq->cfs.h_nr_runnable != 1) in sched_reduced_capacity()
10399 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
10401 * @sds: Load-balancing data with statistics of the local group.
10414 int i, nr_running, local_group, sd_flags = env->sd->flags; in update_sg_lb_stats()
10415 bool balancing_at_rd = !env->sd->parent; in update_sg_lb_stats()
10419 local_group = group == sds->local; in update_sg_lb_stats()
10421 for_each_cpu_and(i, sched_group_span(group), env->cpus) { in update_sg_lb_stats()
10425 sgs->group_load += load; in update_sg_lb_stats()
10426 sgs->group_util += cpu_util_cfs(i); in update_sg_lb_stats()
10427 sgs->group_runnable += cpu_runnable(rq); in update_sg_lb_stats()
10428 sgs->sum_h_nr_running += rq->cfs.h_nr_runnable; in update_sg_lb_stats()
10430 nr_running = rq->nr_running; in update_sg_lb_stats()
10431 sgs->sum_nr_running += nr_running; in update_sg_lb_stats()
10440 sgs->idle_cpus++; in update_sg_lb_stats()
10452 sgs->nr_numa_running += rq->nr_numa_running; in update_sg_lb_stats()
10453 sgs->nr_preferred_running += rq->nr_preferred_running; in update_sg_lb_stats()
10461 if (sgs->group_misfit_task_load < rq->misfit_task_load) { in update_sg_lb_stats()
10462 sgs->group_misfit_task_load = rq->misfit_task_load; in update_sg_lb_stats()
10465 } else if (env->idle && sched_reduced_capacity(rq, env->sd)) { in update_sg_lb_stats()
10467 if (sgs->group_misfit_task_load < load) in update_sg_lb_stats()
10468 sgs->group_misfit_task_load = load; in update_sg_lb_stats()
10472 sgs->group_capacity = group->sgc->capacity; in update_sg_lb_stats()
10474 sgs->group_weight = group->group_weight; in update_sg_lb_stats()
10477 if (!local_group && env->idle && sgs->sum_h_nr_running && in update_sg_lb_stats()
10479 sgs->group_asym_packing = 1; in update_sg_lb_stats()
10483 sgs->group_smt_balance = 1; in update_sg_lb_stats()
10485 sgs->group_type = group_classify(env->sd->imbalance_pct, group, sgs); in update_sg_lb_stats()
10488 if (sgs->group_type == group_overloaded) in update_sg_lb_stats()
10489 sgs->avg_load = (sgs->group_load * SCHED_CAPACITY_SCALE) / in update_sg_lb_stats()
10490 sgs->group_capacity; in update_sg_lb_stats()
10494 * update_sd_pick_busiest - return 1 on busiest group
10511 struct sg_lb_stats *busiest = &sds->busiest_stat; in update_sd_pick_busiest()
10514 if (!sgs->sum_h_nr_running) in update_sd_pick_busiest()
10523 if ((env->sd->flags & SD_ASYM_CPUCAPACITY) && in update_sd_pick_busiest()
10524 (sgs->group_type == group_misfit_task) && in update_sd_pick_busiest()
10525 (!capacity_greater(capacity_of(env->dst_cpu), sg->sgc->max_capacity) || in update_sd_pick_busiest()
10526 sds->local_stat.group_type != group_has_spare)) in update_sd_pick_busiest()
10529 if (sgs->group_type > busiest->group_type) in update_sd_pick_busiest()
10532 if (sgs->group_type < busiest->group_type) in update_sd_pick_busiest()
10540 switch (sgs->group_type) { in update_sd_pick_busiest()
10543 return sgs->avg_load > busiest->avg_load; in update_sd_pick_busiest()
10554 return sched_asym_prefer(READ_ONCE(sds->busiest->asym_prefer_cpu), in update_sd_pick_busiest()
10555 READ_ONCE(sg->asym_prefer_cpu)); in update_sd_pick_busiest()
10562 return sgs->group_misfit_task_load > busiest->group_misfit_task_load; in update_sd_pick_busiest()
10569 if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) in update_sd_pick_busiest()
10587 if (sgs->avg_load < busiest->avg_load) in update_sd_pick_busiest()
10590 if (sgs->avg_load == busiest->avg_load) { in update_sd_pick_busiest()
10592 * SMT sched groups need more help than non-SMT groups. in update_sd_pick_busiest()
10595 if (sds->busiest->flags & SD_SHARE_CPUCAPACITY) in update_sd_pick_busiest()
10607 if (smt_vs_nonsmt_groups(sds->busiest, sg)) { in update_sd_pick_busiest()
10608 if (sg->flags & SD_SHARE_CPUCAPACITY && sgs->sum_h_nr_running <= 1) in update_sd_pick_busiest()
10622 if (sgs->idle_cpus > busiest->idle_cpus) in update_sd_pick_busiest()
10624 else if ((sgs->idle_cpus == busiest->idle_cpus) && in update_sd_pick_busiest()
10625 (sgs->sum_nr_running <= busiest->sum_nr_running)) in update_sd_pick_busiest()
10633 * per-CPU capacity. Migrating tasks to less capable CPUs may harm in update_sd_pick_busiest()
10637 if ((env->sd->flags & SD_ASYM_CPUCAPACITY) && in update_sd_pick_busiest()
10638 (sgs->group_type <= group_fully_busy) && in update_sd_pick_busiest()
10639 (capacity_greater(sg->sgc->min_capacity, capacity_of(env->dst_cpu)))) in update_sd_pick_busiest()
10648 if (sgs->sum_h_nr_running > sgs->nr_numa_running) in fbq_classify_group()
10650 if (sgs->sum_h_nr_running > sgs->nr_preferred_running) in fbq_classify_group()
10657 if (rq->nr_running > rq->nr_numa_running) in fbq_classify_rq()
10659 if (rq->nr_running > rq->nr_preferred_running) in fbq_classify_rq()
10679 * task_running_on_cpu - return 1 if @p is running on @cpu.
10685 if (cpu != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in task_running_on_cpu()
10695 * idle_cpu_without - would a given CPU be idle without p ?
10705 if (rq->curr != rq->idle && rq->curr != p) in idle_cpu_without()
10709 * rq->nr_running can't be used but an updated version without the in idle_cpu_without()
10714 if (rq->ttwu_pending) in idle_cpu_without()
10721 * update_sg_wakeup_stats - Update sched_group's statistics for wakeup.
10737 if (sd->flags & SD_ASYM_CPUCAPACITY) in update_sg_wakeup_stats()
10738 sgs->group_misfit_task_load = 1; in update_sg_wakeup_stats()
10740 for_each_cpu_and(i, sched_group_span(group), p->cpus_ptr) { in update_sg_wakeup_stats()
10744 sgs->group_load += cpu_load_without(rq, p); in update_sg_wakeup_stats()
10745 sgs->group_util += cpu_util_without(i, p); in update_sg_wakeup_stats()
10746 sgs->group_runnable += cpu_runnable_without(rq, p); in update_sg_wakeup_stats()
10748 sgs->sum_h_nr_running += rq->cfs.h_nr_runnable - local; in update_sg_wakeup_stats()
10750 nr_running = rq->nr_running - local; in update_sg_wakeup_stats()
10751 sgs->sum_nr_running += nr_running; in update_sg_wakeup_stats()
10757 sgs->idle_cpus++; in update_sg_wakeup_stats()
10760 if (sd->flags & SD_ASYM_CPUCAPACITY && in update_sg_wakeup_stats()
10761 sgs->group_misfit_task_load && in update_sg_wakeup_stats()
10763 sgs->group_misfit_task_load = 0; in update_sg_wakeup_stats()
10767 sgs->group_capacity = group->sgc->capacity; in update_sg_wakeup_stats()
10769 sgs->group_weight = group->group_weight; in update_sg_wakeup_stats()
10771 sgs->group_type = group_classify(sd->imbalance_pct, group, sgs); in update_sg_wakeup_stats()
10777 if (sgs->group_type == group_fully_busy || in update_sg_wakeup_stats()
10778 sgs->group_type == group_overloaded) in update_sg_wakeup_stats()
10779 sgs->avg_load = (sgs->group_load * SCHED_CAPACITY_SCALE) / in update_sg_wakeup_stats()
10780 sgs->group_capacity; in update_sg_wakeup_stats()
10788 if (sgs->group_type < idlest_sgs->group_type) in update_pick_idlest()
10791 if (sgs->group_type > idlest_sgs->group_type) in update_pick_idlest()
10799 switch (sgs->group_type) { in update_pick_idlest()
10803 if (idlest_sgs->avg_load <= sgs->avg_load) in update_pick_idlest()
10815 if (idlest->sgc->max_capacity >= group->sgc->max_capacity) in update_pick_idlest()
10821 if (idlest_sgs->idle_cpus > sgs->idle_cpus) in update_pick_idlest()
10825 if (idlest_sgs->idle_cpus == sgs->idle_cpus && in update_pick_idlest()
10826 idlest_sgs->group_util <= sgs->group_util) in update_pick_idlest()
10844 struct sched_group *idlest = NULL, *local = NULL, *group = sd->groups; in sched_balance_find_dst_group()
10858 p->cpus_ptr)) in sched_balance_find_dst_group()
10882 } while (group = group->next, group != sd->groups); in sched_balance_find_dst_group()
10913 (sd->imbalance_pct-100) / 100; in sched_balance_find_dst_group()
10920 * cross-domain, add imbalance to the load on the remote node in sched_balance_find_dst_group()
10924 if ((sd->flags & SD_NUMA) && in sched_balance_find_dst_group()
10935 if (100 * local_sgs.avg_load <= sd->imbalance_pct * idlest_sgs.avg_load) in sched_balance_find_dst_group()
10947 if (local->sgc->max_capacity >= idlest->sgc->max_capacity) in sched_balance_find_dst_group()
10953 if (sd->flags & SD_NUMA) { in sched_balance_find_dst_group()
10954 int imb_numa_nr = sd->imb_numa_nr; in sched_balance_find_dst_group()
10961 if (cpu_to_node(this_cpu) == p->numa_preferred_nid) in sched_balance_find_dst_group()
10965 if (cpu_to_node(idlest_cpu) == p->numa_preferred_nid) in sched_balance_find_dst_group()
10977 if (p->nr_cpus_allowed != NR_CPUS) { in sched_balance_find_dst_group()
10980 cpumask_and(cpus, sched_group_span(local), p->cpus_ptr); in sched_balance_find_dst_group()
10981 imb_numa_nr = min(cpumask_weight(cpus), sd->imb_numa_nr); in sched_balance_find_dst_group()
10984 imbalance = abs(local_sgs.idle_cpus - idlest_sgs.idle_cpus); in sched_balance_find_dst_group()
11021 if (!sched_feat(SIS_UTIL) || env->idle == CPU_NEWLY_IDLE) in update_idle_cpu_scan()
11024 llc_weight = per_cpu(sd_llc_size, env->dst_cpu); in update_idle_cpu_scan()
11025 if (env->sd->span_weight != llc_weight) in update_idle_cpu_scan()
11028 sd_share = rcu_dereference(per_cpu(sd_llc_shared, env->dst_cpu)); in update_idle_cpu_scan()
11038 * let y = SCHED_CAPACITY_SCALE - p * x^2 [1] in update_idle_cpu_scan()
11056 * y = SCHED_CAPACITY_SCALE - in update_idle_cpu_scan()
11065 pct = env->sd->imbalance_pct; in update_idle_cpu_scan()
11069 y = SCHED_CAPACITY_SCALE - tmp; in update_idle_cpu_scan()
11074 if ((int)y != sd_share->nr_idle_scan) in update_idle_cpu_scan()
11075 WRITE_ONCE(sd_share->nr_idle_scan, (int)y); in update_idle_cpu_scan()
11079 * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
11086 struct sched_group *sg = env->sd->groups; in update_sd_lb_stats()
11087 struct sg_lb_stats *local = &sds->local_stat; in update_sd_lb_stats()
11096 local_group = cpumask_test_cpu(env->dst_cpu, sched_group_span(sg)); in update_sd_lb_stats()
11098 sds->local = sg; in update_sd_lb_stats()
11101 if (env->idle != CPU_NEWLY_IDLE || in update_sd_lb_stats()
11102 time_after_eq(jiffies, sg->sgc->next_update)) in update_sd_lb_stats()
11103 update_group_capacity(env->sd, env->dst_cpu); in update_sd_lb_stats()
11109 sds->busiest = sg; in update_sd_lb_stats()
11110 sds->busiest_stat = *sgs; in update_sd_lb_stats()
11114 sds->total_load += sgs->group_load; in update_sd_lb_stats()
11115 sds->total_capacity += sgs->group_capacity; in update_sd_lb_stats()
11117 sum_util += sgs->group_util; in update_sd_lb_stats()
11118 sg = sg->next; in update_sd_lb_stats()
11119 } while (sg != env->sd->groups); in update_sd_lb_stats()
11126 if (sds->busiest) in update_sd_lb_stats()
11127 sds->prefer_sibling = !!(sds->busiest->flags & SD_PREFER_SIBLING); in update_sd_lb_stats()
11130 if (env->sd->flags & SD_NUMA) in update_sd_lb_stats()
11131 env->fbq_type = fbq_classify_group(&sds->busiest_stat); in update_sd_lb_stats()
11133 if (!env->sd->parent) { in update_sd_lb_stats()
11135 set_rd_overloaded(env->dst_rq->rd, sg_overloaded); in update_sd_lb_stats()
11137 /* Update over-utilization (tipping point, U >= 0) indicator */ in update_sd_lb_stats()
11138 set_rd_overutilized(env->dst_rq->rd, sg_overutilized); in update_sd_lb_stats()
11140 set_rd_overutilized(env->dst_rq->rd, sg_overutilized); in update_sd_lb_stats()
11147 * calculate_imbalance - Calculate the amount of imbalance present within the
11156 local = &sds->local_stat; in calculate_imbalance()
11157 busiest = &sds->busiest_stat; in calculate_imbalance()
11159 if (busiest->group_type == group_misfit_task) { in calculate_imbalance()
11160 if (env->sd->flags & SD_ASYM_CPUCAPACITY) { in calculate_imbalance()
11162 env->migration_type = migrate_misfit; in calculate_imbalance()
11163 env->imbalance = 1; in calculate_imbalance()
11169 env->migration_type = migrate_load; in calculate_imbalance()
11170 env->imbalance = busiest->group_misfit_task_load; in calculate_imbalance()
11175 if (busiest->group_type == group_asym_packing) { in calculate_imbalance()
11180 env->migration_type = migrate_task; in calculate_imbalance()
11181 env->imbalance = busiest->sum_h_nr_running; in calculate_imbalance()
11185 if (busiest->group_type == group_smt_balance) { in calculate_imbalance()
11187 env->migration_type = migrate_task; in calculate_imbalance()
11188 env->imbalance = 1; in calculate_imbalance()
11192 if (busiest->group_type == group_imbalanced) { in calculate_imbalance()
11194 * In the group_imb case we cannot rely on group-wide averages in calculate_imbalance()
11195 * to ensure CPU-load equilibrium, try to move any task to fix in calculate_imbalance()
11199 env->migration_type = migrate_task; in calculate_imbalance()
11200 env->imbalance = 1; in calculate_imbalance()
11208 if (local->group_type == group_has_spare) { in calculate_imbalance()
11209 if ((busiest->group_type > group_fully_busy) && in calculate_imbalance()
11210 !(env->sd->flags & SD_SHARE_LLC)) { in calculate_imbalance()
11219 env->migration_type = migrate_util; in calculate_imbalance()
11220 env->imbalance = max(local->group_capacity, local->group_util) - in calculate_imbalance()
11221 local->group_util; in calculate_imbalance()
11230 if (env->idle && env->imbalance == 0) { in calculate_imbalance()
11231 env->migration_type = migrate_task; in calculate_imbalance()
11232 env->imbalance = 1; in calculate_imbalance()
11238 if (busiest->group_weight == 1 || sds->prefer_sibling) { in calculate_imbalance()
11243 env->migration_type = migrate_task; in calculate_imbalance()
11244 env->imbalance = sibling_imbalance(env, sds, busiest, local); in calculate_imbalance()
11251 env->migration_type = migrate_task; in calculate_imbalance()
11252 env->imbalance = max_t(long, 0, in calculate_imbalance()
11253 (local->idle_cpus - busiest->idle_cpus)); in calculate_imbalance()
11258 if (env->sd->flags & SD_NUMA) { in calculate_imbalance()
11259 env->imbalance = adjust_numa_imbalance(env->imbalance, in calculate_imbalance()
11260 local->sum_nr_running + 1, in calculate_imbalance()
11261 env->sd->imb_numa_nr); in calculate_imbalance()
11266 env->imbalance >>= 1; in calculate_imbalance()
11275 if (local->group_type < group_overloaded) { in calculate_imbalance()
11281 local->avg_load = (local->group_load * SCHED_CAPACITY_SCALE) / in calculate_imbalance()
11282 local->group_capacity; in calculate_imbalance()
11288 if (local->avg_load >= busiest->avg_load) { in calculate_imbalance()
11289 env->imbalance = 0; in calculate_imbalance()
11293 sds->avg_load = (sds->total_load * SCHED_CAPACITY_SCALE) / in calculate_imbalance()
11294 sds->total_capacity; in calculate_imbalance()
11300 if (local->avg_load >= sds->avg_load) { in calculate_imbalance()
11301 env->imbalance = 0; in calculate_imbalance()
11315 env->migration_type = migrate_load; in calculate_imbalance()
11316 env->imbalance = min( in calculate_imbalance()
11317 (busiest->avg_load - sds->avg_load) * busiest->group_capacity, in calculate_imbalance()
11318 (sds->avg_load - local->avg_load) * local->group_capacity in calculate_imbalance()
11345 * sched_balance_find_src_group - Returns the busiest group within the sched_domain
11352 * Return: - The busiest group if imbalance exists.
11374 if (busiest->group_type == group_misfit_task) in sched_balance_find_src_group()
11377 if (!is_rd_overutilized(env->dst_rq->rd) && in sched_balance_find_src_group()
11378 rcu_dereference(env->dst_rq->rd->pd)) in sched_balance_find_src_group()
11382 if (busiest->group_type == group_asym_packing) in sched_balance_find_src_group()
11390 if (busiest->group_type == group_imbalanced) in sched_balance_find_src_group()
11398 if (local->group_type > busiest->group_type) in sched_balance_find_src_group()
11405 if (local->group_type == group_overloaded) { in sched_balance_find_src_group()
11410 if (local->avg_load >= busiest->avg_load) in sched_balance_find_src_group()
11421 if (local->avg_load >= sds.avg_load) in sched_balance_find_src_group()
11428 if (100 * busiest->avg_load <= in sched_balance_find_src_group()
11429 env->sd->imbalance_pct * local->avg_load) in sched_balance_find_src_group()
11437 if (sds.prefer_sibling && local->group_type == group_has_spare && in sched_balance_find_src_group()
11441 if (busiest->group_type != group_overloaded) { in sched_balance_find_src_group()
11442 if (!env->idle) { in sched_balance_find_src_group()
11451 if (busiest->group_type == group_smt_balance && in sched_balance_find_src_group()
11457 if (busiest->group_weight > 1 && in sched_balance_find_src_group()
11458 local->idle_cpus <= (busiest->idle_cpus + 1)) { in sched_balance_find_src_group()
11471 if (busiest->sum_h_nr_running == 1) { in sched_balance_find_src_group()
11482 return env->imbalance ? sds.busiest : NULL; in sched_balance_find_src_group()
11485 env->imbalance = 0; in sched_balance_find_src_group()
11490 * sched_balance_find_src_rq - find the busiest runqueue among the CPUs in the group.
11500 for_each_cpu_and(i, sched_group_span(group), env->cpus) { in sched_balance_find_src_rq()
11510 * - regular: there are !numa tasks in sched_balance_find_src_rq()
11511 * - remote: there are numa tasks that run on the 'wrong' node in sched_balance_find_src_rq()
11512 * - all: there is no distinction in sched_balance_find_src_rq()
11527 if (rt > env->fbq_type) in sched_balance_find_src_rq()
11530 nr_running = rq->cfs.h_nr_runnable; in sched_balance_find_src_rq()
11538 * eventually lead to active_balancing high->low capacity. in sched_balance_find_src_rq()
11539 * Higher per-CPU capacity is considered better than balancing in sched_balance_find_src_rq()
11542 if (env->sd->flags & SD_ASYM_CPUCAPACITY && in sched_balance_find_src_rq()
11543 !capacity_greater(capacity_of(env->dst_cpu), capacity) && in sched_balance_find_src_rq()
11554 if (sched_asym(env->sd, i, env->dst_cpu) && nr_running == 1) in sched_balance_find_src_rq()
11557 switch (env->migration_type) { in sched_balance_find_src_rq()
11565 if (nr_running == 1 && load > env->imbalance && in sched_balance_find_src_rq()
11566 !check_cpu_capacity(rq, env->sd)) in sched_balance_find_src_rq()
11618 if (rq->misfit_task_load > busiest_load) { in sched_balance_find_src_rq()
11619 busiest_load = rq->misfit_task_load; in sched_balance_find_src_rq()
11650 return env->idle && sched_use_asym_prio(env->sd, env->dst_cpu) && in asym_active_balance()
11651 (sched_asym_prefer(env->dst_cpu, env->src_cpu) || in asym_active_balance()
11652 !sched_use_asym_prio(env->sd, env->src_cpu)); in asym_active_balance()
11658 struct sched_domain *sd = env->sd; in imbalanced_active_balance()
11665 if ((env->migration_type == migrate_task) && in imbalanced_active_balance()
11666 (sd->nr_balance_failed > sd->cache_nice_tries+2)) in imbalanced_active_balance()
11674 struct sched_domain *sd = env->sd; in need_active_balance()
11688 if (env->idle && in need_active_balance()
11689 (env->src_rq->cfs.h_nr_runnable == 1)) { in need_active_balance()
11690 if ((check_cpu_capacity(env->src_rq, sd)) && in need_active_balance()
11691 (capacity_of(env->src_cpu)*sd->imbalance_pct < capacity_of(env->dst_cpu)*100)) in need_active_balance()
11695 if (env->migration_type == migrate_misfit) in need_active_balance()
11706 struct sched_group *sg = env->sd->groups; in should_we_balance()
11707 int cpu, idle_smt = -1; in should_we_balance()
11713 if (!cpumask_test_cpu(env->dst_cpu, env->cpus)) in should_we_balance()
11723 if (env->idle == CPU_NEWLY_IDLE) { in should_we_balance()
11724 if (env->dst_rq->nr_running > 0 || env->dst_rq->ttwu_pending) in should_we_balance()
11731 for_each_cpu_and(cpu, swb_cpus, env->cpus) { in should_we_balance()
11740 if (!(env->sd->flags & SD_SHARE_CPUCAPACITY) && !is_core_idle(cpu)) { in should_we_balance()
11741 if (idle_smt == -1) in should_we_balance()
11755 * Are we the first idle core in a non-SMT domain or higher, in should_we_balance()
11758 return cpu == env->dst_cpu; in should_we_balance()
11762 if (idle_smt != -1) in should_we_balance()
11763 return idle_smt == env->dst_cpu; in should_we_balance()
11766 return group_balance_cpu(sg) == env->dst_cpu; in should_we_balance()
11775 switch (env->migration_type) { in update_lb_imbalance_stat()
11777 __schedstat_add(sd->lb_imbalance_load[idle], env->imbalance); in update_lb_imbalance_stat()
11780 __schedstat_add(sd->lb_imbalance_util[idle], env->imbalance); in update_lb_imbalance_stat()
11783 __schedstat_add(sd->lb_imbalance_task[idle], env->imbalance); in update_lb_imbalance_stat()
11786 __schedstat_add(sd->lb_imbalance_misfit[idle], env->imbalance); in update_lb_imbalance_stat()
11792 * This flag serializes load-balancing passes over large domains
11793 * (above the NODE topology level) - only one load-balancing instance
11797 * - Note that load-balancing passes triggered while another one
11798 * is executing are skipped and not re-tried.
11800 * - Also note that this does not serialize rebalance_domains()
11801 * execution, as non-SD_SERIALIZE domains will still be
11802 * load-balanced in parallel.
11815 struct sched_domain *sd_parent = sd->parent; in sched_balance_rq()
11824 .dst_grpmask = group_balance_mask(sd->groups), in sched_balance_rq()
11835 schedstat_inc(sd->lb_count[idle]); in sched_balance_rq()
11843 if (!need_unlock && (sd->flags & SD_SERIALIZE)) { in sched_balance_rq()
11853 schedstat_inc(sd->lb_nobusyg[idle]); in sched_balance_rq()
11859 schedstat_inc(sd->lb_nobusyq[idle]); in sched_balance_rq()
11867 env.src_cpu = busiest->cpu; in sched_balance_rq()
11873 if (busiest->nr_running > 1) { in sched_balance_rq()
11876 * an imbalance but busiest->nr_running <= 1, the group is in sched_balance_rq()
11880 env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running); in sched_balance_rq()
11887 * cur_ld_moved - load moved in current iteration in sched_balance_rq()
11888 * ld_moved - cumulative load moved across iterations in sched_balance_rq()
11895 * unlock busiest->lock, and we are able to be sure in sched_balance_rq()
11924 * nohz-idle), we now have balance_cpu in a position to move in sched_balance_rq()
11935 /* Prevent to re-select dst_cpu via env's CPUs */ in sched_balance_rq()
11955 int *group_imbalance = &sd_parent->groups->sgc->imbalance; in sched_balance_rq()
11982 schedstat_inc(sd->lb_failed[idle]); in sched_balance_rq()
11994 sd->nr_balance_failed++; in sched_balance_rq()
12006 if (!cpumask_test_cpu(this_cpu, busiest->curr->cpus_ptr)) { in sched_balance_rq()
12015 * ->active_balance synchronizes accesses to in sched_balance_rq()
12016 * ->active_balance_work. Once set, it's cleared in sched_balance_rq()
12019 if (!busiest->active_balance) { in sched_balance_rq()
12020 busiest->active_balance = 1; in sched_balance_rq()
12021 busiest->push_cpu = this_cpu; in sched_balance_rq()
12030 &busiest->active_balance_work); in sched_balance_rq()
12035 sd->nr_balance_failed = 0; in sched_balance_rq()
12040 sd->balance_interval = sd->min_interval; in sched_balance_rq()
12052 int *group_imbalance = &sd_parent->groups->sgc->imbalance; in sched_balance_rq()
12064 schedstat_inc(sd->lb_balanced[idle]); in sched_balance_rq()
12066 sd->nr_balance_failed = 0; in sched_balance_rq()
12087 sd->balance_interval < MAX_PINNED_INTERVAL) || in sched_balance_rq()
12088 sd->balance_interval < sd->max_interval) in sched_balance_rq()
12089 sd->balance_interval *= 2; in sched_balance_rq()
12100 unsigned long interval = sd->balance_interval; in get_sd_balance_interval()
12103 interval *= sd->busy_factor; in get_sd_balance_interval()
12114 interval -= 1; in get_sd_balance_interval()
12128 next = sd->last_balance + interval; in update_next_balance()
12144 int target_cpu = busiest_rq->push_cpu; in active_load_balance_cpu_stop()
12152 * Between queueing the stop-work and running it is a hole in which in active_load_balance_cpu_stop()
12161 !busiest_rq->active_balance)) in active_load_balance_cpu_stop()
12165 if (busiest_rq->nr_running <= 1) in active_load_balance_cpu_stop()
12171 * Bjorn Helgaas on a 128-CPU setup. in active_load_balance_cpu_stop()
12187 .src_cpu = busiest_rq->cpu, in active_load_balance_cpu_stop()
12193 schedstat_inc(sd->alb_count); in active_load_balance_cpu_stop()
12198 schedstat_inc(sd->alb_pushed); in active_load_balance_cpu_stop()
12200 sd->nr_balance_failed = 0; in active_load_balance_cpu_stop()
12202 schedstat_inc(sd->alb_failed); in active_load_balance_cpu_stop()
12207 busiest_rq->active_balance = 0; in active_load_balance_cpu_stop()
12220 * This trades load-balance latency on larger machines for less cross talk.
12229 sd->newidle_call++; in update_newidle_stats()
12230 sd->newidle_success += success; in update_newidle_stats()
12232 if (sd->newidle_call >= 1024) { in update_newidle_stats()
12233 sd->newidle_ratio = sd->newidle_success; in update_newidle_stats()
12234 sd->newidle_call /= 2; in update_newidle_stats()
12235 sd->newidle_success /= 2; in update_newidle_stats()
12242 unsigned long next_decay = sd->last_decay_max_lb_cost + HZ; in update_newidle_cost()
12248 if (cost > sd->max_newidle_lb_cost) { in update_newidle_cost()
12253 sd->max_newidle_lb_cost = cost; in update_newidle_cost()
12254 sd->last_decay_max_lb_cost = now; in update_newidle_cost()
12262 sd->max_newidle_lb_cost = (sd->max_newidle_lb_cost * 253) / 256; in update_newidle_cost()
12263 sd->last_decay_max_lb_cost = now; in update_newidle_cost()
12279 int cpu = rq->cpu; in sched_balance_domains()
12296 max_cost += sd->max_newidle_lb_cost; in sched_balance_domains()
12310 if (time_after_eq(jiffies, sd->last_balance + interval)) { in sched_balance_domains()
12314 * env->dst_cpu, so we can't know our idle in sched_balance_domains()
12320 sd->last_balance = jiffies; in sched_balance_domains()
12323 if (time_after(next_balance, sd->last_balance + interval)) { in sched_balance_domains()
12324 next_balance = sd->last_balance + interval; in sched_balance_domains()
12330 * Ensure the rq-wide value also decays but keep it at a in sched_balance_domains()
12331 * reasonable floor to avoid funnies with rq->avg_idle. in sched_balance_domains()
12333 rq->max_idle_balance_cost = in sched_balance_domains()
12344 rq->next_balance = next_balance; in sched_balance_domains()
12350 return unlikely(!rcu_dereference_sched(rq->sd)); in on_null_domain()
12357 * - When one of the busy CPUs notices that there may be an idle rebalancing
12377 return -1; in find_new_ilb()
12381 * Kick a CPU to do the NOHZ balancing, if it is time for it, via a cross-CPU
12422 smp_call_function_single_async(ilb_cpu, &cpu_rq(ilb_cpu)->nohz_csd); in kick_ilb()
12434 int nr_busy, i, cpu = rq->cpu; in nohz_balancer_kick()
12437 if (unlikely(rq->idle_balance)) in nohz_balancer_kick()
12460 if (rq->nr_running >= 2) { in nohz_balancer_kick()
12467 sd = rcu_dereference(rq->sd); in nohz_balancer_kick()
12473 if (rq->cfs.h_nr_runnable >= 1 && check_cpu_capacity(rq, sd)) { in nohz_balancer_kick()
12522 * increase the overall cache utilization), we need a less-loaded LLC in nohz_balancer_kick()
12526 * the others are - so just get a NOHZ balance going if it looks in nohz_balancer_kick()
12529 nr_busy = atomic_read(&sds->nr_busy_cpus); in nohz_balancer_kick()
12552 if (!sd || !sd->nohz_idle) in set_cpu_sd_state_busy()
12554 sd->nohz_idle = 0; in set_cpu_sd_state_busy()
12556 atomic_inc(&sd->shared->nr_busy_cpus); in set_cpu_sd_state_busy()
12565 if (likely(!rq->nohz_tick_stopped)) in nohz_balance_exit_idle()
12568 rq->nohz_tick_stopped = 0; in nohz_balance_exit_idle()
12569 cpumask_clear_cpu(rq->cpu, nohz.idle_cpus_mask); in nohz_balance_exit_idle()
12572 set_cpu_sd_state_busy(rq->cpu); in nohz_balance_exit_idle()
12582 if (!sd || sd->nohz_idle) in set_cpu_sd_state_idle()
12584 sd->nohz_idle = 1; in set_cpu_sd_state_idle()
12586 atomic_dec(&sd->shared->nr_busy_cpus); in set_cpu_sd_state_idle()
12606 * Can be set safely without rq->lock held in nohz_balance_enter_idle()
12608 * rq->lock is held during the check and the clear in nohz_balance_enter_idle()
12610 rq->has_blocked_load = 1; in nohz_balance_enter_idle()
12618 if (rq->nohz_tick_stopped) in nohz_balance_enter_idle()
12625 rq->nohz_tick_stopped = 1; in nohz_balance_enter_idle()
12650 unsigned int cpu = rq->cpu; in update_nohz_stats()
12652 if (!rq->has_blocked_load) in update_nohz_stats()
12658 if (!time_after(jiffies, READ_ONCE(rq->last_blocked_load_update_tick))) in update_nohz_stats()
12663 return rq->has_blocked_load; in update_nohz_stats()
12678 int this_cpu = this_rq->cpu; in _nohz_idle_balance()
12735 if (time_after_eq(jiffies, rq->next_balance)) { in _nohz_idle_balance()
12746 if (time_after(next_balance, rq->next_balance)) { in _nohz_idle_balance()
12747 next_balance = rq->next_balance; in _nohz_idle_balance()
12776 unsigned int flags = this_rq->nohz_idle_balance; in nohz_idle_balance()
12781 this_rq->nohz_idle_balance = 0; in nohz_idle_balance()
12822 int this_cpu = this_rq->cpu; in nohz_newidle_balance()
12825 if (this_rq->avg_idle < sysctl_sched_migration_cost) in nohz_newidle_balance()
12856 * < 0 - we released the lock and there are !fair tasks present
12857 * 0 - failed, no new tasks
12858 * > 0 - success, new (fair) tasks present
12863 int this_cpu = this_rq->cpu; in sched_balance_newidle()
12875 if (this_rq->ttwu_pending) in sched_balance_newidle()
12883 this_rq->idle_stamp = rq_clock(this_rq); in sched_balance_newidle()
12893 * for load-balance and preemption/IRQs are still disabled avoiding in sched_balance_newidle()
12895 * re-start the picking loop. in sched_balance_newidle()
12900 sd = rcu_dereference_check_sched_domain(this_rq->sd); in sched_balance_newidle()
12906 if (!get_rd_overloaded(this_rq->rd) || in sched_balance_newidle()
12907 this_rq->avg_idle < sd->max_newidle_lb_cost) { in sched_balance_newidle()
12927 if (this_rq->avg_idle < curr_cost + sd->max_newidle_lb_cost) in sched_balance_newidle()
12930 if (sd->flags & SD_BALANCE_NEWIDLE) { in sched_balance_newidle()
12935 * Throw a 1k sided dice; and only run in sched_balance_newidle()
12940 weight = 1 + sd->newidle_ratio; in sched_balance_newidle()
12953 domain_cost = t1 - t0; in sched_balance_newidle()
12975 if (curr_cost > this_rq->max_idle_balance_cost) in sched_balance_newidle()
12976 this_rq->max_idle_balance_cost = curr_cost; in sched_balance_newidle()
12983 if (this_rq->cfs.h_nr_queued && !pulled_task) in sched_balance_newidle()
12988 pulled_task = -1; in sched_balance_newidle()
12992 if (time_after(this_rq->next_balance, next_balance)) in sched_balance_newidle()
12993 this_rq->next_balance = next_balance; in sched_balance_newidle()
12996 this_rq->idle_stamp = 0; in sched_balance_newidle()
13008 * - directly from the local sched_tick() for periodic load balancing
13010 * - indirectly from a remote sched_tick() for NOHZ idle balancing
13011 * through the SMP cross-call nohz_csd_func()
13016 enum cpu_idle_type idle = this_rq->idle_balance; in sched_balance_softirq()
13029 sched_balance_update_blocked_averages(this_rq->cpu); in sched_balance_softirq()
13045 if (time_after_eq(jiffies, rq->next_balance)) in sched_balance_trigger()
13073 u64 rtime = se->sum_exec_runtime - se->prev_sum_exec_runtime; in __entity_slice_used()
13074 u64 slice = se->slice; in __entity_slice_used()
13096 * MIN_NR_TASKS_DURING_FORCEIDLE - 1 tasks and use that to check in task_tick_core()
13099 if (rq->core->core_forceidle_count && rq->cfs.nr_queued == 1 && in task_tick_core()
13100 __entity_slice_used(&curr->se, MIN_NR_TASKS_DURING_FORCEIDLE)) in task_tick_core()
13116 * dt_i = ---------- dt (1)
13122 * s_i = --- d[t]_i (2)
13130 * S = ---------- dt (3)
13135 * lag(i) = S - s_i (4)
13147 * be learned. We can combine (1)-(3) or (4)-(5) and express S in s_i:
13150 * S = -------------- (6)
13155 * is two-fold. Firstly, computing S in that way requires a 64bit division
13157 * the steady-state, it doesn't handle dynamics.
13159 * Anyway, in (6): s_i -> x + (s_i - x), to get:
13161 * \Sum_i w_i (s_i - x)
13162 * S - x = -------------------- (7)
13169 * possible to generalize to multiple runqueues -- however it gets really
13171 * first counter-example.
13173 * Luckily I think we can avoid needing a full multi-queue variant for
13174 * core-scheduling (or load-balancing). The crucial observation is that we
13175 * only actually need this comparison in the presence of forced-idle; only
13181 * answer is only interesting if one of them is forced-idle]
13183 * And (under assumption of SMT2) when there is forced-idle, there is only
13194 * S_k = --- (9)
13200 * S_k+l = --------- (10)
13205 * lag_k+l(i) := S_k+l - s_i (11)
13212 * a) when a runqueue enters force-idle, sync it against it's sibling rq(s)
13213 * using (7); this only requires storing single 'time'-stamps.
13215 * b) when comparing tasks between 2 runqueues of which one is forced-idle,
13220 * boundaries, but I think we can avoid that. The force-idle is for the
13230 * get re-elected. So far so simple, right?
13233 * we sync to 0 and let the idle one build up lag to get re-election. Now
13235 * again would destroy the built-up lag from the queue that was already
13240 * less := !((s64)(s_a - s_b) <= 0)
13242 * (v_a - S_a) - (v_b - S_b) == v_a - v_b - S_a + S_b
13243 * == v_a - (v_b - S_a + S_b)
13245 * IOW, we can recast the (lag) comparison to a one-sided difference.
13252 * and for already idle queues to preserve their build-up lag.
13268 * se_fi_update - Update the cfs_rq->zero_vruntime_fi in a CFS hierarchy if needed.
13277 if (cfs_rq->forceidle_seq == fi_seq) in se_fi_update()
13279 cfs_rq->forceidle_seq = fi_seq; in se_fi_update()
13282 cfs_rq->zero_vruntime_fi = cfs_rq->zero_vruntime; in se_fi_update()
13288 struct sched_entity *se = &p->se; in task_vruntime_update()
13290 if (p->sched_class != &fair_sched_class) in task_vruntime_update()
13293 se_fi_update(se, rq->core->core_forceidle_seq, in_fi); in task_vruntime_update()
13300 const struct sched_entity *sea = &a->se; in cfs_prio_less()
13301 const struct sched_entity *seb = &b->se; in cfs_prio_less()
13306 WARN_ON_ONCE(task_rq(b)->core != rq->core); in cfs_prio_less()
13313 while (sea->cfs_rq->tg != seb->cfs_rq->tg) { in cfs_prio_less()
13314 int sea_depth = sea->depth; in cfs_prio_less()
13315 int seb_depth = seb->depth; in cfs_prio_less()
13323 se_fi_update(sea, rq->core->core_forceidle_seq, in_fi); in cfs_prio_less()
13324 se_fi_update(seb, rq->core->core_forceidle_seq, in_fi); in cfs_prio_less()
13326 cfs_rqa = sea->cfs_rq; in cfs_prio_less()
13327 cfs_rqb = seb->cfs_rq; in cfs_prio_less()
13329 cfs_rqa = &task_rq(a)->cfs; in cfs_prio_less()
13330 cfs_rqb = &task_rq(b)->cfs; in cfs_prio_less()
13338 delta = (s64)(sea->vruntime - seb->vruntime) + in cfs_prio_less()
13339 (s64)(cfs_rqb->zero_vruntime_fi - cfs_rqa->zero_vruntime_fi); in cfs_prio_less()
13349 cfs_rq = task_group(p)->cfs_rq[cpu]; in task_is_throttled_fair()
13351 cfs_rq = &cpu_rq(cpu)->cfs; in task_is_throttled_fair()
13370 struct sched_entity *se = &curr->se; in task_tick_fair()
13388 * - child not yet on the tasklist
13389 * - preemption disabled
13406 if (p->prio == oldprio) in prio_changed_fair()
13409 if (rq->cfs.nr_queued == 1) in prio_changed_fair()
13418 if (p->prio > oldprio) in prio_changed_fair()
13444 se = se->parent; in propagate_entity_cfs_rq()
13467 * - A forked task which hasn't been woken up by wake_up_new_task(). in detach_entity_cfs_rq()
13468 * - A task which has been woken up by try_to_wake_up() but is in detach_entity_cfs_rq()
13471 if (!se->avg.last_update_time) in detach_entity_cfs_rq()
13494 struct sched_entity *se = &p->se; in detach_task_cfs_rq()
13501 struct sched_entity *se = &p->se; in attach_task_cfs_rq()
13508 if (p->se.sched_delayed) in switching_from_fair()
13519 WARN_ON_ONCE(p->se.sched_delayed); in switched_to_fair()
13540 struct sched_entity *se = &p->se; in __set_next_task_fair()
13547 list_move(&se->group_node, &rq->cfs_tasks); in __set_next_task_fair()
13552 WARN_ON_ONCE(se->sched_delayed); in __set_next_task_fair()
13564 * This routine is mostly called to set cfs_rq->curr field when a task
13569 struct sched_entity *se = &p->se; in set_next_task_fair()
13584 cfs_rq->tasks_timeline = RB_ROOT_CACHED; in init_cfs_rq()
13585 cfs_rq->zero_vruntime = (u64)(-(1LL << 20)); in init_cfs_rq()
13586 raw_spin_lock_init(&cfs_rq->removed.lock); in init_cfs_rq()
13596 if (READ_ONCE(p->__state) == TASK_NEW) in task_change_group_fair()
13601 /* Tell se's cfs_rq has been changed -- migrated */ in task_change_group_fair()
13602 p->se.avg.last_update_time = 0; in task_change_group_fair()
13612 if (tg->cfs_rq) in free_fair_sched_group()
13613 kfree(tg->cfs_rq[i]); in free_fair_sched_group()
13614 if (tg->se) in free_fair_sched_group()
13615 kfree(tg->se[i]); in free_fair_sched_group()
13618 kfree(tg->cfs_rq); in free_fair_sched_group()
13619 kfree(tg->se); in free_fair_sched_group()
13628 tg->cfs_rq = kcalloc(nr_cpu_ids, sizeof(cfs_rq), GFP_KERNEL); in alloc_fair_sched_group()
13629 if (!tg->cfs_rq) in alloc_fair_sched_group()
13631 tg->se = kcalloc(nr_cpu_ids, sizeof(se), GFP_KERNEL); in alloc_fair_sched_group()
13632 if (!tg->se) in alloc_fair_sched_group()
13635 tg->shares = NICE_0_LOAD; in alloc_fair_sched_group()
13651 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]); in alloc_fair_sched_group()
13672 se = tg->se[i]; in online_fair_sched_group()
13688 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu]; in unregister_fair_sched_group()
13689 struct sched_entity *se = tg->se[cpu]; in unregister_fair_sched_group()
13693 if (se->sched_delayed) { in unregister_fair_sched_group()
13695 if (se->sched_delayed) { in unregister_fair_sched_group()
13706 * check on_list without danger of it being re-added. in unregister_fair_sched_group()
13708 if (cfs_rq->on_list) { in unregister_fair_sched_group()
13721 cfs_rq->tg = tg; in init_tg_cfs_entry()
13722 cfs_rq->rq = rq; in init_tg_cfs_entry()
13725 tg->cfs_rq[cpu] = cfs_rq; in init_tg_cfs_entry()
13726 tg->se[cpu] = se; in init_tg_cfs_entry()
13733 se->cfs_rq = &rq->cfs; in init_tg_cfs_entry()
13734 se->depth = 0; in init_tg_cfs_entry()
13736 se->cfs_rq = parent->my_q; in init_tg_cfs_entry()
13737 se->depth = parent->depth + 1; in init_tg_cfs_entry()
13740 se->my_q = cfs_rq; in init_tg_cfs_entry()
13742 update_load_set(&se->load, NICE_0_LOAD); in init_tg_cfs_entry()
13743 se->parent = parent; in init_tg_cfs_entry()
13757 if (!tg->se[0]) in __sched_group_set_shares()
13758 return -EINVAL; in __sched_group_set_shares()
13762 if (tg->shares == shares) in __sched_group_set_shares()
13765 tg->shares = shares; in __sched_group_set_shares()
13768 struct sched_entity *se = tg->se[i]; in __sched_group_set_shares()
13790 ret = -EINVAL; in sched_group_set_shares()
13803 return -EINVAL; in sched_group_set_idle()
13806 return -EINVAL; in sched_group_set_idle()
13810 if (tg->idle == idle) { in sched_group_set_idle()
13815 tg->idle = idle; in sched_group_set_idle()
13819 struct sched_entity *se = tg->se[i]; in sched_group_set_idle()
13820 struct cfs_rq *grp_cfs_rq = tg->cfs_rq[i]; in sched_group_set_idle()
13827 grp_cfs_rq->idle = idle; in sched_group_set_idle()
13831 idle_task_delta = grp_cfs_rq->h_nr_queued - in sched_group_set_idle()
13832 grp_cfs_rq->h_nr_idle; in sched_group_set_idle()
13834 idle_task_delta *= -1; in sched_group_set_idle()
13839 if (!se->on_rq) in sched_group_set_idle()
13842 cfs_rq->h_nr_idle += idle_task_delta; in sched_group_set_idle()
13868 struct sched_entity *se = &task->se; in get_rr_interval_fair()
13875 if (rq->cfs.load.weight) in get_rr_interval_fair()
13876 rr_interval = NS_TO_JIFFIES(se->slice); in get_rr_interval_fair()
13953 ng = rcu_dereference(p->numa_group); in show_numa_stats()
13955 if (p->numa_faults) { in show_numa_stats()
13956 tsf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 0)]; in show_numa_stats()
13957 tpf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 1)]; in show_numa_stats()
13960 gsf = ng->faults[task_faults_idx(NUMA_MEM, node, 0)], in show_numa_stats()
13961 gpf = ng->faults[task_faults_idx(NUMA_MEM, node, 1)]; in show_numa_stats()
13980 INIT_CSD(&cpu_rq(i)->cfsb_csd, __cfsb_csd_unthrottle, cpu_rq(i)); in init_sched_fair_class()
13981 INIT_LIST_HEAD(&cpu_rq(i)->cfsb_csd_list); in init_sched_fair_class()