Lines Matching +full:current +full:- +full:regulated
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->min_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->min_vruntime
610 * \Sum (v_i - v0) * w_i := cfs_rq->avg_vruntime
611 * \Sum w_i := cfs_rq->avg_load
614 * the per-task service, these deltas: (v_i - v), will be in the order of the
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->min_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->min_vruntime) * load; in vruntime_eligible()
740 return vruntime_eligible(cfs_rq, se->vruntime); in entity_eligible()
745 u64 min_vruntime = cfs_rq->min_vruntime; in __update_min_vruntime()
749 s64 delta = (s64)(vruntime - min_vruntime); in __update_min_vruntime()
760 struct sched_entity *curr = cfs_rq->curr; in update_min_vruntime()
761 u64 vruntime = cfs_rq->min_vruntime; in update_min_vruntime()
764 if (curr->on_rq) in update_min_vruntime()
765 vruntime = curr->vruntime; in update_min_vruntime()
772 vruntime = se->min_vruntime; in update_min_vruntime()
774 vruntime = min_vruntime(vruntime, se->min_vruntime); in update_min_vruntime()
778 cfs_rq->min_vruntime = __update_min_vruntime(cfs_rq, vruntime); in update_min_vruntime()
784 struct sched_entity *curr = cfs_rq->curr; in cfs_rq_min_slice()
787 if (curr && curr->on_rq) in cfs_rq_min_slice()
788 min_slice = curr->slice; in cfs_rq_min_slice()
791 min_slice = min(min_slice, root->min_slice); in cfs_rq_min_slice()
801 #define vruntime_gt(field, lse, rse) ({ (s64)((lse)->field - (rse)->field) > 0; })
808 se->min_vruntime = rse->min_vruntime; in __min_vruntime_update()
816 if (rse->min_slice < se->min_slice) in __min_slice_update()
817 se->min_slice = rse->min_slice; in __min_slice_update()
822 * se->min_vruntime = min(se->vruntime, {left,right}->min_vruntime)
826 u64 old_min_vruntime = se->min_vruntime; in min_vruntime_update()
827 u64 old_min_slice = se->min_slice; in min_vruntime_update()
828 struct rb_node *node = &se->run_node; in min_vruntime_update()
830 se->min_vruntime = se->vruntime; in min_vruntime_update()
831 __min_vruntime_update(se, node->rb_right); in min_vruntime_update()
832 __min_vruntime_update(se, node->rb_left); in min_vruntime_update()
834 se->min_slice = se->slice; in min_vruntime_update()
835 __min_slice_update(se, node->rb_right); in min_vruntime_update()
836 __min_slice_update(se, node->rb_left); in min_vruntime_update()
838 return se->min_vruntime == old_min_vruntime && in min_vruntime_update()
839 se->min_slice == old_min_slice; in min_vruntime_update()
846 * Enqueue an entity into the rb-tree:
851 se->min_vruntime = se->vruntime; in __enqueue_entity()
852 se->min_slice = se->slice; in __enqueue_entity()
853 rb_add_augmented_cached(&se->run_node, &cfs_rq->tasks_timeline, in __enqueue_entity()
859 rb_erase_augmented_cached(&se->run_node, &cfs_rq->tasks_timeline, in __dequeue_entity()
866 struct rb_node *root = cfs_rq->tasks_timeline.rb_root.rb_node; in __pick_root_entity()
876 struct rb_node *left = rb_first_cached(&cfs_rq->tasks_timeline); in __pick_first_entity()
895 u64 vprot = se->deadline; in set_protect_slice()
900 slice = min(slice, se->slice); in set_protect_slice()
901 if (slice != se->slice) in set_protect_slice()
902 vprot = min_vruntime(vprot, se->vruntime + calc_delta_fair(slice, se)); in set_protect_slice()
904 se->vprot = vprot; in set_protect_slice()
911 se->vprot = min_vruntime(se->vprot, se->vruntime + calc_delta_fair(slice, se)); in update_protect_slice()
916 return ((s64)(se->vprot - se->vruntime) > 0); in protect_slice()
922 se->vprot = se->vruntime; in cancel_protect_slice()
936 * We can do this in O(log n) time due to an augmented RB-tree. The
940 * se->min_vruntime = min(se->vruntime, se->{left,right}->min_vruntime)
946 struct rb_node *node = cfs_rq->tasks_timeline.rb_root.rb_node; in __pick_eevdf()
948 struct sched_entity *curr = cfs_rq->curr; in __pick_eevdf()
955 if (cfs_rq->nr_queued == 1) in __pick_eevdf()
956 return curr && curr->on_rq ? curr : se; in __pick_eevdf()
958 if (curr && (!curr->on_rq || !entity_eligible(cfs_rq, curr))) in __pick_eevdf()
972 struct rb_node *left = node->rb_left; in __pick_eevdf()
979 __node_2_se(left)->min_vruntime)) { in __pick_eevdf()
988 * entity, so check the current node since it is the one in __pick_eevdf()
996 node = node->rb_right; in __pick_eevdf()
1012 struct rb_node *last = rb_last(&cfs_rq->tasks_timeline.rb_root); in __pick_last_entity()
1043 if ((s64)(se->vruntime - se->deadline) < 0) in update_deadline()
1051 if (!se->custom_slice) in update_deadline()
1052 se->slice = sysctl_sched_base_slice; in update_deadline()
1057 se->deadline = se->vruntime + calc_delta_fair(se->slice, se); in update_deadline()
1074 struct sched_avg *sa = &se->avg; in init_entity_runnable_average()
1085 sa->load_avg = scale_load_down(se->load.weight); in init_entity_runnable_average()
1092 * based on the cfs_rq's current util_avg:
1094 * util_avg = cfs_rq->avg.util_avg / (cfs_rq->avg.load_avg + 1)
1104 * util_avg_cap = (cpu_scale - cfs_rq->avg.util_avg) / 2^n
1119 struct sched_entity *se = &p->se; in post_init_entity_util_avg()
1121 struct sched_avg *sa = &se->avg; in post_init_entity_util_avg()
1123 long cap = (long)(cpu_scale - cfs_rq->avg.util_avg) / 2; in post_init_entity_util_avg()
1125 if (p->sched_class != &fair_sched_class) { in post_init_entity_util_avg()
1136 se->avg.last_update_time = cfs_rq_clock_pelt(cfs_rq); in post_init_entity_util_avg()
1141 if (cfs_rq->avg.util_avg != 0) { in post_init_entity_util_avg()
1142 sa->util_avg = cfs_rq->avg.util_avg * se_weight(se); in post_init_entity_util_avg()
1143 sa->util_avg /= (cfs_rq->avg.load_avg + 1); in post_init_entity_util_avg()
1145 if (sa->util_avg > cap) in post_init_entity_util_avg()
1146 sa->util_avg = cap; in post_init_entity_util_avg()
1148 sa->util_avg = cap; in post_init_entity_util_avg()
1152 sa->runnable_avg = sa->util_avg; in post_init_entity_util_avg()
1160 delta_exec = now - se->exec_start; in update_se()
1164 se->exec_start = now; in update_se()
1167 struct task_struct *running = rq->curr; in update_se()
1170 * task, as w/ proxy-exec they may not be the same. in update_se()
1172 running->se.exec_start = now; in update_se()
1173 running->se.sum_exec_runtime += delta_exec; in update_se()
1182 se->sum_exec_runtime += delta_exec; in update_se()
1189 __schedstat_set(stats->exec_max, in update_se()
1190 max(delta_exec, stats->exec_max)); in update_se()
1201 return update_se(rq, &rq->donor->se); in update_curr_common()
1205 * Update the current task's runtime statistics.
1210 * Note: cfs_rq->curr corresponds to the task picked to in update_curr()
1211 * run (ie: rq->donor.se) which due to proxy-exec may in update_curr()
1213 * (rq->curr.se). This is easy to confuse! in update_curr()
1215 struct sched_entity *curr = cfs_rq->curr; in update_curr()
1227 curr->vruntime += calc_delta_fair(delta_exec, curr); in update_curr()
1236 * - If the task is running on behalf of fair_server, we need in update_curr()
1238 * - Fair task that runs outside of fair_server should account in update_curr()
1242 if (dl_server_active(&rq->fair_server)) in update_curr()
1243 dl_server_update(&rq->fair_server, delta_exec); in update_curr()
1248 if (cfs_rq->nr_queued == 1) in update_curr()
1259 update_curr(cfs_rq_of(&rq->donor->se)); in update_curr_fair()
1292 * maybe already in the runqueue, the se->statistics.wait_start in update_stats_wait_end_fair()
1296 if (unlikely(!schedstat_val(stats->wait_start))) in update_stats_wait_end_fair()
1323 * Task is being enqueued - update stats:
1332 * Are we enqueueing a waiting task? (for current tasks in update_stats_enqueue_fair()
1335 if (se != cfs_rq->curr) in update_stats_enqueue_fair()
1353 if (se != cfs_rq->curr) in update_stats_dequeue_fair()
1361 state = READ_ONCE(tsk->__state); in update_stats_dequeue_fair()
1363 __schedstat_set(tsk->stats.sleep_start, in update_stats_dequeue_fair()
1366 __schedstat_set(tsk->stats.block_start, in update_stats_dequeue_fair()
1372 * We are picking a new current task - update its stats:
1380 se->exec_start = rq_clock_task(rq_of(cfs_rq)); in update_stats_curr_start()
1472 * ->numa_group (see struct task_struct for locking rules).
1476 return rcu_dereference_check(p->numa_group, p == current || in deref_task_numa_group()
1477 (lockdep_is_held(__rq_lockp(task_rq(p))) && !READ_ONCE(p->on_cpu))); in deref_task_numa_group()
1482 return rcu_dereference_protected(p->numa_group, p == current); in deref_curr_numa_group()
1494 * Calculations based on RSS as non-present and empty pages are skipped in task_nr_scan_windows()
1499 rss = get_mm_rss(p->mm); in task_nr_scan_windows()
1532 ng = rcu_dereference(p->numa_group); in task_scan_start()
1537 period *= refcount_read(&ng->refcount); in task_scan_start()
1562 period *= refcount_read(&ng->refcount); in task_scan_max()
1574 rq->nr_numa_running += (p->numa_preferred_nid != NUMA_NO_NODE); in account_numa_enqueue()
1575 rq->nr_preferred_running += (p->numa_preferred_nid == task_node(p)); in account_numa_enqueue()
1580 rq->nr_numa_running -= (p->numa_preferred_nid != NUMA_NO_NODE); in account_numa_dequeue()
1581 rq->nr_preferred_running -= (p->numa_preferred_nid == task_node(p)); in account_numa_dequeue()
1599 ng = rcu_dereference(p->numa_group); in task_numa_group_id()
1601 gid = ng->gid; in task_numa_group_id()
1610 * array is for current counters, which are averaged into the
1620 if (!p->numa_faults) in task_faults()
1623 return p->numa_faults[task_faults_idx(NUMA_MEM, nid, 0)] + in task_faults()
1624 p->numa_faults[task_faults_idx(NUMA_MEM, nid, 1)]; in task_faults()
1634 return ng->faults[task_faults_idx(NUMA_MEM, nid, 0)] + in group_faults()
1635 ng->faults[task_faults_idx(NUMA_MEM, nid, 1)]; in group_faults()
1640 return group->faults[task_faults_idx(NUMA_CPU, nid, 0)] + in group_faults_cpu()
1641 group->faults[task_faults_idx(NUMA_CPU, nid, 1)]; in group_faults_cpu()
1650 faults += ng->faults[task_faults_idx(NUMA_MEM, node, 1)]; in group_faults_priv()
1662 faults += ng->faults[task_faults_idx(NUMA_MEM, node, 0)]; in group_faults_shared()
1670 * considered part of a numa group's pseudo-interleaving set. Migrations
1677 return group_faults_cpu(ng, nid) * ACTIVE_NODE_FRACTION > ng->max_faults_cpu; in numa_is_active_node()
1736 faults *= (max_dist - dist); in score_nearby_nodes()
1737 faults /= (max_dist - LOCAL_DISTANCE); in score_nearby_nodes()
1757 if (!p->numa_faults) in task_weight()
1760 total_faults = p->total_numa_faults; in task_weight()
1780 total_faults = ng->total_faults; in group_weight()
1816 pgdat->node_present_pages >> 4); in pgdat_free_space_enough()
1817 for (z = pgdat->nr_zones - 1; z >= 0; z--) { in pgdat_free_space_enough()
1818 struct zone *zone = pgdat->node_zones + z; in pgdat_free_space_enough()
1838 * hint page fault latency = hint page fault time - scan time
1850 return (time - last_time) & PAGE_ACCESS_TIME_MASK; in numa_hint_fault_latency()
1867 start = pgdat->nbp_rl_start; in numa_promotion_rate_limit()
1868 if (now - start > MSEC_PER_SEC && in numa_promotion_rate_limit()
1869 cmpxchg(&pgdat->nbp_rl_start, start, now) == start) in numa_promotion_rate_limit()
1870 pgdat->nbp_rl_nr_cand = nr_cand; in numa_promotion_rate_limit()
1871 if (nr_cand - pgdat->nbp_rl_nr_cand >= rate_limit) in numa_promotion_rate_limit()
1887 start = pgdat->nbp_th_start; in numa_promotion_adjust_threshold()
1888 if (now - start > th_period && in numa_promotion_adjust_threshold()
1889 cmpxchg(&pgdat->nbp_th_start, start, now) == start) { in numa_promotion_adjust_threshold()
1893 diff_cand = nr_cand - pgdat->nbp_th_nr_cand; in numa_promotion_adjust_threshold()
1895 th = pgdat->nbp_threshold ? : ref_th; in numa_promotion_adjust_threshold()
1897 th = max(th - unit_th, unit_th); in numa_promotion_adjust_threshold()
1900 pgdat->nbp_th_nr_cand = nr_cand; in numa_promotion_adjust_threshold()
1901 pgdat->nbp_threshold = th; in numa_promotion_adjust_threshold()
1931 pgdat->nbp_threshold = 0; in should_numa_migrate_memory()
1940 th = pgdat->nbp_threshold ? : def_th; in should_numa_migrate_memory()
1948 this_cpupid = cpu_pid_to_cpupid(dst_cpu, current->pid); in should_numa_migrate_memory()
1958 * two full passes of the "multi-stage node selection" test that is in should_numa_migrate_memory()
1961 if ((p->numa_preferred_nid == NUMA_NO_NODE || p->numa_scan_seq <= 4) && in should_numa_migrate_memory()
1966 * Multi-stage node selection is used in conjunction with a periodic in should_numa_migrate_memory()
1967 * migration fault to build a temporal task<->page relation. By using in should_numa_migrate_memory()
1968 * a two-stage filter we remove short/unlikely relations. in should_numa_migrate_memory()
1972 * page (n_t) (in a given time-span) to a probability. in should_numa_migrate_memory()
1980 * act on an unlikely task<->page relation. in should_numa_migrate_memory()
1990 /* A shared fault, but p->numa_group has not been set up yet. */ in should_numa_migrate_memory()
2007 * --------------- * - > --------------- in should_numa_migrate_memory()
2069 if ((ns->nr_running > ns->weight) && in numa_classify()
2070 (((ns->compute_capacity * 100) < (ns->util * imbalance_pct)) || in numa_classify()
2071 ((ns->compute_capacity * imbalance_pct) < (ns->runnable * 100)))) in numa_classify()
2074 if ((ns->nr_running < ns->weight) || in numa_classify()
2075 (((ns->compute_capacity * 100) > (ns->util * imbalance_pct)) && in numa_classify()
2076 ((ns->compute_capacity * imbalance_pct) > (ns->runnable * 100)))) in numa_classify()
2117 int cpu, idle_core = -1; in update_numa_stats()
2120 ns->idle_cpu = -1; in update_numa_stats()
2126 ns->load += cpu_load(rq); in update_numa_stats()
2127 ns->runnable += cpu_runnable(rq); in update_numa_stats()
2128 ns->util += cpu_util_cfs(cpu); in update_numa_stats()
2129 ns->nr_running += rq->cfs.h_nr_runnable; in update_numa_stats()
2130 ns->compute_capacity += capacity_of(cpu); in update_numa_stats()
2132 if (find_idle && idle_core < 0 && !rq->nr_running && idle_cpu(cpu)) { in update_numa_stats()
2133 if (READ_ONCE(rq->numa_migrate_on) || in update_numa_stats()
2134 !cpumask_test_cpu(cpu, env->p->cpus_ptr)) in update_numa_stats()
2137 if (ns->idle_cpu == -1) in update_numa_stats()
2138 ns->idle_cpu = cpu; in update_numa_stats()
2145 ns->weight = cpumask_weight(cpumask_of_node(nid)); in update_numa_stats()
2147 ns->node_type = numa_classify(env->imbalance_pct, ns); in update_numa_stats()
2150 ns->idle_cpu = idle_core; in update_numa_stats()
2156 struct rq *rq = cpu_rq(env->dst_cpu); in task_numa_assign()
2158 /* Check if run-queue part of active NUMA balance. */ in task_numa_assign()
2159 if (env->best_cpu != env->dst_cpu && xchg(&rq->numa_migrate_on, 1)) { in task_numa_assign()
2161 int start = env->dst_cpu; in task_numa_assign()
2164 for_each_cpu_wrap(cpu, cpumask_of_node(env->dst_nid), start + 1) { in task_numa_assign()
2165 if (cpu == env->best_cpu || !idle_cpu(cpu) || in task_numa_assign()
2166 !cpumask_test_cpu(cpu, env->p->cpus_ptr)) { in task_numa_assign()
2170 env->dst_cpu = cpu; in task_numa_assign()
2171 rq = cpu_rq(env->dst_cpu); in task_numa_assign()
2172 if (!xchg(&rq->numa_migrate_on, 1)) in task_numa_assign()
2182 * Clear previous best_cpu/rq numa-migrate flag, since task now in task_numa_assign()
2185 if (env->best_cpu != -1 && env->best_cpu != env->dst_cpu) { in task_numa_assign()
2186 rq = cpu_rq(env->best_cpu); in task_numa_assign()
2187 WRITE_ONCE(rq->numa_migrate_on, 0); in task_numa_assign()
2190 if (env->best_task) in task_numa_assign()
2191 put_task_struct(env->best_task); in task_numa_assign()
2195 env->best_task = p; in task_numa_assign()
2196 env->best_imp = imp; in task_numa_assign()
2197 env->best_cpu = env->dst_cpu; in task_numa_assign()
2211 * ------------ vs --------- in load_too_imbalanced()
2214 src_capacity = env->src_stats.compute_capacity; in load_too_imbalanced()
2215 dst_capacity = env->dst_stats.compute_capacity; in load_too_imbalanced()
2217 imb = abs(dst_load * src_capacity - src_load * dst_capacity); in load_too_imbalanced()
2219 orig_src_load = env->src_stats.load; in load_too_imbalanced()
2220 orig_dst_load = env->dst_stats.load; in load_too_imbalanced()
2222 old_imb = abs(orig_dst_load * src_capacity - orig_src_load * dst_capacity); in load_too_imbalanced()
2244 struct numa_group *cur_ng, *p_ng = deref_curr_numa_group(env->p); in task_numa_compare()
2245 struct rq *dst_rq = cpu_rq(env->dst_cpu); in task_numa_compare()
2249 int dist = env->dist; in task_numa_compare()
2254 if (READ_ONCE(dst_rq->numa_migrate_on)) in task_numa_compare()
2258 cur = rcu_dereference(dst_rq->curr); in task_numa_compare()
2259 if (cur && ((cur->flags & (PF_EXITING | PF_KTHREAD)) || in task_numa_compare()
2260 !cur->mm)) in task_numa_compare()
2265 * end try selecting ourselves (current == env->p) as a swap candidate. in task_numa_compare()
2267 if (cur == env->p) { in task_numa_compare()
2273 if (maymove && moveimp >= env->best_imp) in task_numa_compare()
2280 if (!cpumask_test_cpu(env->src_cpu, cur->cpus_ptr)) in task_numa_compare()
2287 if (env->best_task && in task_numa_compare()
2288 env->best_task->numa_preferred_nid == env->src_nid && in task_numa_compare()
2289 cur->numa_preferred_nid != env->src_nid) { in task_numa_compare()
2303 cur_ng = rcu_dereference(cur->numa_group); in task_numa_compare()
2311 if (env->dst_stats.node_type == node_has_spare) in task_numa_compare()
2314 imp = taskimp + task_weight(cur, env->src_nid, dist) - in task_numa_compare()
2315 task_weight(cur, env->dst_nid, dist); in task_numa_compare()
2321 imp -= imp / 16; in task_numa_compare()
2328 imp += group_weight(cur, env->src_nid, dist) - in task_numa_compare()
2329 group_weight(cur, env->dst_nid, dist); in task_numa_compare()
2331 imp += task_weight(cur, env->src_nid, dist) - in task_numa_compare()
2332 task_weight(cur, env->dst_nid, dist); in task_numa_compare()
2336 if (cur->numa_preferred_nid == env->dst_nid) in task_numa_compare()
2337 imp -= imp / 16; in task_numa_compare()
2345 if (cur->numa_preferred_nid == env->src_nid) in task_numa_compare()
2348 if (maymove && moveimp > imp && moveimp > env->best_imp) { in task_numa_compare()
2358 if (env->best_task && cur->numa_preferred_nid == env->src_nid && in task_numa_compare()
2359 env->best_task->numa_preferred_nid != env->src_nid) { in task_numa_compare()
2369 if (imp < SMALLIMP || imp <= env->best_imp + SMALLIMP / 2) in task_numa_compare()
2375 load = task_h_load(env->p) - task_h_load(cur); in task_numa_compare()
2379 dst_load = env->dst_stats.load + load; in task_numa_compare()
2380 src_load = env->src_stats.load - load; in task_numa_compare()
2388 int cpu = env->dst_stats.idle_cpu; in task_numa_compare()
2390 /* Nothing cached so current CPU went idle since the search. */ in task_numa_compare()
2392 cpu = env->dst_cpu; in task_numa_compare()
2398 if (!idle_cpu(cpu) && env->best_cpu >= 0 && in task_numa_compare()
2399 idle_cpu(env->best_cpu)) { in task_numa_compare()
2400 cpu = env->best_cpu; in task_numa_compare()
2403 env->dst_cpu = cpu; in task_numa_compare()
2413 if (maymove && !cur && env->best_cpu >= 0 && idle_cpu(env->best_cpu)) in task_numa_compare()
2417 * If a swap candidate must be identified and the current best task in task_numa_compare()
2420 if (!maymove && env->best_task && in task_numa_compare()
2421 env->best_task->numa_preferred_nid == env->src_nid) { in task_numa_compare()
2440 if (env->dst_stats.node_type == node_has_spare) { in task_numa_find_cpu()
2450 src_running = env->src_stats.nr_running - 1; in task_numa_find_cpu()
2451 dst_running = env->dst_stats.nr_running + 1; in task_numa_find_cpu()
2452 imbalance = max(0, dst_running - src_running); in task_numa_find_cpu()
2454 env->imb_numa_nr); in task_numa_find_cpu()
2459 if (env->dst_stats.idle_cpu >= 0) { in task_numa_find_cpu()
2460 env->dst_cpu = env->dst_stats.idle_cpu; in task_numa_find_cpu()
2468 * If the improvement from just moving env->p direction is better in task_numa_find_cpu()
2471 load = task_h_load(env->p); in task_numa_find_cpu()
2472 dst_load = env->dst_stats.load + load; in task_numa_find_cpu()
2473 src_load = env->src_stats.load - load; in task_numa_find_cpu()
2477 for_each_cpu(cpu, cpumask_of_node(env->dst_nid)) { in task_numa_find_cpu()
2479 if (!cpumask_test_cpu(cpu, env->p->cpus_ptr)) in task_numa_find_cpu()
2482 env->dst_cpu = cpu; in task_numa_find_cpu()
2500 .best_cpu = -1, in task_numa_migrate()
2514 * random movement of tasks -- counter the numa conditions we're trying in task_numa_migrate()
2520 env.imbalance_pct = 100 + (sd->imbalance_pct - 100) / 2; in task_numa_migrate()
2521 env.imb_numa_nr = sd->imb_numa_nr; in task_numa_migrate()
2533 return -EINVAL; in task_numa_migrate()
2536 env.dst_nid = p->numa_preferred_nid; in task_numa_migrate()
2541 taskimp = task_weight(p, env.dst_nid, dist) - taskweight; in task_numa_migrate()
2542 groupimp = group_weight(p, env.dst_nid, dist) - groupweight; in task_numa_migrate()
2550 * - there is no space available on the preferred_nid in task_numa_migrate()
2551 * - the task is part of a numa_group that is interleaved across in task_numa_migrate()
2556 if (env.best_cpu == -1 || (ng && ng->active_nodes > 1)) { in task_numa_migrate()
2558 if (nid == env.src_nid || nid == p->numa_preferred_nid) in task_numa_migrate()
2569 taskimp = task_weight(p, nid, dist) - taskweight; in task_numa_migrate()
2570 groupimp = group_weight(p, nid, dist) - groupweight; in task_numa_migrate()
2590 if (env.best_cpu == -1) in task_numa_migrate()
2595 if (nid != p->numa_preferred_nid) in task_numa_migrate()
2599 /* No better CPU than the current one was found. */ in task_numa_migrate()
2600 if (env.best_cpu == -1) { in task_numa_migrate()
2601 trace_sched_stick_numa(p, env.src_cpu, NULL, -1); in task_numa_migrate()
2602 return -EAGAIN; in task_numa_migrate()
2608 WRITE_ONCE(best_rq->numa_migrate_on, 0); in task_numa_migrate()
2615 WRITE_ONCE(best_rq->numa_migrate_on, 0); in task_numa_migrate()
2629 if (unlikely(p->numa_preferred_nid == NUMA_NO_NODE || !p->numa_faults)) in numa_migrate_preferred()
2633 interval = min(interval, msecs_to_jiffies(p->numa_scan_period) / 16); in numa_migrate_preferred()
2634 p->numa_migrate_retry = jiffies + interval; in numa_migrate_preferred()
2637 if (task_node(p) == p->numa_preferred_nid) in numa_migrate_preferred()
2667 numa_group->max_faults_cpu = max_faults; in numa_group_count_active_nodes()
2668 numa_group->active_nodes = active_nodes; in numa_group_count_active_nodes()
2694 unsigned long remote = p->numa_faults_locality[0]; in update_task_scan_period()
2695 unsigned long local = p->numa_faults_locality[1]; in update_task_scan_period()
2704 if (local + shared == 0 || p->numa_faults_locality[2]) { in update_task_scan_period()
2705 p->numa_scan_period = min(p->numa_scan_period_max, in update_task_scan_period()
2706 p->numa_scan_period << 1); in update_task_scan_period()
2708 p->mm->numa_next_scan = jiffies + in update_task_scan_period()
2709 msecs_to_jiffies(p->numa_scan_period); in update_task_scan_period()
2715 * Prepare to scale scan period relative to the current period. in update_task_scan_period()
2720 period_slot = DIV_ROUND_UP(p->numa_scan_period, NUMA_PERIOD_SLOTS); in update_task_scan_period()
2729 int slot = ps_ratio - NUMA_PERIOD_THRESHOLD; in update_task_scan_period()
2739 int slot = lr_ratio - NUMA_PERIOD_THRESHOLD; in update_task_scan_period()
2745 * Private memory faults exceed (SLOTS-THRESHOLD)/SLOTS, in update_task_scan_period()
2750 diff = -(NUMA_PERIOD_THRESHOLD - ratio) * period_slot; in update_task_scan_period()
2753 p->numa_scan_period = clamp(p->numa_scan_period + diff, in update_task_scan_period()
2755 memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); in update_task_scan_period()
2762 * from the dozens-of-seconds NUMA balancing period. Use the scheduler
2769 now = p->se.exec_start; in numa_get_avg_runtime()
2770 runtime = p->se.sum_exec_runtime; in numa_get_avg_runtime()
2772 if (p->last_task_numa_placement) { in numa_get_avg_runtime()
2773 delta = runtime - p->last_sum_exec_runtime; in numa_get_avg_runtime()
2774 *period = now - p->last_task_numa_placement; in numa_get_avg_runtime()
2780 delta = p->se.avg.load_sum; in numa_get_avg_runtime()
2784 p->last_sum_exec_runtime = runtime; in numa_get_avg_runtime()
2785 p->last_task_numa_placement = now; in numa_get_avg_runtime()
2835 for (dist = sched_max_numa_distance; dist > LOCAL_DISTANCE; dist--) { in preferred_group_nid()
2889 * The p->mm->numa_scan_seq field gets updated without in task_numa_placement()
2893 seq = READ_ONCE(p->mm->numa_scan_seq); in task_numa_placement()
2894 if (p->numa_scan_seq == seq) in task_numa_placement()
2896 p->numa_scan_seq = seq; in task_numa_placement()
2897 p->numa_scan_period_max = task_scan_max(p); in task_numa_placement()
2899 total_faults = p->numa_faults_locality[0] + in task_numa_placement()
2900 p->numa_faults_locality[1]; in task_numa_placement()
2906 group_lock = &ng->lock; in task_numa_placement()
2926 diff = p->numa_faults[membuf_idx] - p->numa_faults[mem_idx] / 2; in task_numa_placement()
2927 fault_types[priv] += p->numa_faults[membuf_idx]; in task_numa_placement()
2928 p->numa_faults[membuf_idx] = 0; in task_numa_placement()
2934 * little over-all impact on throughput, and thus their in task_numa_placement()
2938 f_weight = (f_weight * p->numa_faults[cpubuf_idx]) / in task_numa_placement()
2940 f_diff = f_weight - p->numa_faults[cpu_idx] / 2; in task_numa_placement()
2941 p->numa_faults[cpubuf_idx] = 0; in task_numa_placement()
2943 p->numa_faults[mem_idx] += diff; in task_numa_placement()
2944 p->numa_faults[cpu_idx] += f_diff; in task_numa_placement()
2945 faults += p->numa_faults[mem_idx]; in task_numa_placement()
2946 p->total_numa_faults += diff; in task_numa_placement()
2955 ng->faults[mem_idx] += diff; in task_numa_placement()
2956 ng->faults[cpu_idx] += f_diff; in task_numa_placement()
2957 ng->total_faults += diff; in task_numa_placement()
2958 group_faults += ng->faults[mem_idx]; in task_numa_placement()
2973 /* Cannot migrate task to CPU-less node */ in task_numa_placement()
2984 if (max_nid != p->numa_preferred_nid) in task_numa_placement()
2993 return refcount_inc_not_zero(&grp->refcount); in get_numa_group()
2998 if (refcount_dec_and_test(&grp->refcount)) in put_numa_group()
3020 refcount_set(&grp->refcount, 1); in task_numa_group()
3021 grp->active_nodes = 1; in task_numa_group()
3022 grp->max_faults_cpu = 0; in task_numa_group()
3023 spin_lock_init(&grp->lock); in task_numa_group()
3024 grp->gid = p->pid; in task_numa_group()
3027 grp->faults[i] = p->numa_faults[i]; in task_numa_group()
3029 grp->total_faults = p->total_numa_faults; in task_numa_group()
3031 grp->nr_tasks++; in task_numa_group()
3032 rcu_assign_pointer(p->numa_group, grp); in task_numa_group()
3036 tsk = READ_ONCE(cpu_rq(cpu)->curr); in task_numa_group()
3041 grp = rcu_dereference(tsk->numa_group); in task_numa_group()
3053 if (my_grp->nr_tasks > grp->nr_tasks) in task_numa_group()
3057 * Tie-break on the grp address. in task_numa_group()
3059 if (my_grp->nr_tasks == grp->nr_tasks && my_grp > grp) in task_numa_group()
3063 if (tsk->mm == current->mm) in task_numa_group()
3082 double_lock_irq(&my_grp->lock, &grp->lock); in task_numa_group()
3085 my_grp->faults[i] -= p->numa_faults[i]; in task_numa_group()
3086 grp->faults[i] += p->numa_faults[i]; in task_numa_group()
3088 my_grp->total_faults -= p->total_numa_faults; in task_numa_group()
3089 grp->total_faults += p->total_numa_faults; in task_numa_group()
3091 my_grp->nr_tasks--; in task_numa_group()
3092 grp->nr_tasks++; in task_numa_group()
3094 spin_unlock(&my_grp->lock); in task_numa_group()
3095 spin_unlock_irq(&grp->lock); in task_numa_group()
3097 rcu_assign_pointer(p->numa_group, grp); in task_numa_group()
3108 * Get rid of NUMA statistics associated with a task (either current or dead).
3112 * reset the data back to default state without freeing ->numa_faults.
3116 /* safe: p either is current or is being freed by current */ in task_numa_free()
3117 struct numa_group *grp = rcu_dereference_raw(p->numa_group); in task_numa_free()
3118 unsigned long *numa_faults = p->numa_faults; in task_numa_free()
3126 spin_lock_irqsave(&grp->lock, flags); in task_numa_free()
3128 grp->faults[i] -= p->numa_faults[i]; in task_numa_free()
3129 grp->total_faults -= p->total_numa_faults; in task_numa_free()
3131 grp->nr_tasks--; in task_numa_free()
3132 spin_unlock_irqrestore(&grp->lock, flags); in task_numa_free()
3133 RCU_INIT_POINTER(p->numa_group, NULL); in task_numa_free()
3138 p->numa_faults = NULL; in task_numa_free()
3141 p->total_numa_faults = 0; in task_numa_free()
3152 struct task_struct *p = current; in task_numa_fault()
3154 int cpu_node = task_node(current); in task_numa_fault()
3163 if (!p->mm) in task_numa_fault()
3175 /* Allocate buffer to track faults on a per-node basis */ in task_numa_fault()
3176 if (unlikely(!p->numa_faults)) { in task_numa_fault()
3177 int size = sizeof(*p->numa_faults) * in task_numa_fault()
3180 p->numa_faults = kzalloc(size, GFP_KERNEL|__GFP_NOWARN); in task_numa_fault()
3181 if (!p->numa_faults) in task_numa_fault()
3184 p->total_numa_faults = 0; in task_numa_fault()
3185 memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); in task_numa_fault()
3192 if (unlikely(last_cpupid == (-1 & LAST_CPUPID_MASK))) { in task_numa_fault()
3207 if (!priv && !local && ng && ng->active_nodes > 1 && in task_numa_fault()
3216 if (time_after(jiffies, p->numa_migrate_retry)) { in task_numa_fault()
3222 p->numa_pages_migrated += pages; in task_numa_fault()
3224 p->numa_faults_locality[2] += pages; in task_numa_fault()
3226 p->numa_faults[task_faults_idx(NUMA_MEMBUF, mem_node, priv)] += pages; in task_numa_fault()
3227 p->numa_faults[task_faults_idx(NUMA_CPUBUF, cpu_node, priv)] += pages; in task_numa_fault()
3228 p->numa_faults_locality[local] += pages; in task_numa_fault()
3235 * p->mm->numa_scan_seq is written to without exclusive access in reset_ptenuma_scan()
3241 WRITE_ONCE(p->mm->numa_scan_seq, READ_ONCE(p->mm->numa_scan_seq) + 1); in reset_ptenuma_scan()
3242 p->mm->numa_scan_offset = 0; in reset_ptenuma_scan()
3254 if ((READ_ONCE(current->mm->numa_scan_seq) - vma->numab_state->start_scan_seq) < 2) in vma_is_accessed()
3257 pids = vma->numab_state->pids_active[0] | vma->numab_state->pids_active[1]; in vma_is_accessed()
3258 if (test_bit(hash_32(current->pid, ilog2(BITS_PER_LONG)), &pids)) in vma_is_accessed()
3263 * some VMAs may never be scanned in multi-threaded applications: in vma_is_accessed()
3265 if (mm->numa_scan_offset > vma->vm_start) { in vma_is_accessed()
3275 if (READ_ONCE(mm->numa_scan_seq) > in vma_is_accessed()
3276 (vma->numab_state->prev_scan_seq + get_nr_threads(current))) in vma_is_accessed()
3291 struct task_struct *p = current; in task_numa_work()
3292 struct mm_struct *mm = p->mm; in task_numa_work()
3293 u64 runtime = p->se.sum_exec_runtime; in task_numa_work()
3304 work->next = work; in task_numa_work()
3308 * NOTE: make sure not to dereference p->mm before this check, in task_numa_work()
3310 * without p->mm even though we still had it when we enqueued this in task_numa_work()
3313 if (p->flags & PF_EXITING) in task_numa_work()
3321 trace_sched_skip_cpuset_numa(current, &cpuset_current_mems_allowed); in task_numa_work()
3325 if (!mm->numa_next_scan) { in task_numa_work()
3326 mm->numa_next_scan = now + in task_numa_work()
3333 migrate = mm->numa_next_scan; in task_numa_work()
3337 if (p->numa_scan_period == 0) { in task_numa_work()
3338 p->numa_scan_period_max = task_scan_max(p); in task_numa_work()
3339 p->numa_scan_period = task_scan_start(p); in task_numa_work()
3342 next_scan = now + msecs_to_jiffies(p->numa_scan_period); in task_numa_work()
3343 if (!try_cmpxchg(&mm->numa_next_scan, &migrate, next_scan)) in task_numa_work()
3350 p->node_stamp += 2 * TICK_NSEC; in task_numa_work()
3353 pages <<= 20 - PAGE_SHIFT; /* MB in pages */ in task_numa_work()
3363 * VMAs are skipped if the current PID has not trapped a fault within in task_numa_work()
3370 start = mm->numa_scan_offset; in task_numa_work()
3382 is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_MIXEDMAP)) { in task_numa_work()
3390 * hinting faults in read-only file-backed mappings or the vDSO in task_numa_work()
3393 if (!vma->vm_mm || in task_numa_work()
3394 (vma->vm_file && (vma->vm_flags & (VM_READ|VM_WRITE)) == (VM_READ))) { in task_numa_work()
3408 /* Initialise new per-VMA NUMAB state. */ in task_numa_work()
3409 if (!vma->numab_state) { in task_numa_work()
3416 if (cmpxchg(&vma->numab_state, NULL, ptr)) { in task_numa_work()
3421 vma->numab_state->start_scan_seq = mm->numa_scan_seq; in task_numa_work()
3423 vma->numab_state->next_scan = now + in task_numa_work()
3427 vma->numab_state->pids_active_reset = vma->numab_state->next_scan + in task_numa_work()
3435 vma->numab_state->prev_scan_seq = mm->numa_scan_seq - 1; in task_numa_work()
3442 if (mm->numa_scan_seq && time_before(jiffies, in task_numa_work()
3443 vma->numab_state->next_scan)) { in task_numa_work()
3449 if (mm->numa_scan_seq && in task_numa_work()
3450 time_after(jiffies, vma->numab_state->pids_active_reset)) { in task_numa_work()
3451 vma->numab_state->pids_active_reset = vma->numab_state->pids_active_reset + in task_numa_work()
3453 vma->numab_state->pids_active[0] = READ_ONCE(vma->numab_state->pids_active[1]); in task_numa_work()
3454 vma->numab_state->pids_active[1] = 0; in task_numa_work()
3458 if (vma->numab_state->prev_scan_seq == mm->numa_scan_seq) { in task_numa_work()
3459 mm->numa_scan_offset = vma->vm_end; in task_numa_work()
3475 start = max(start, vma->vm_start); in task_numa_work()
3477 end = min(end, vma->vm_end); in task_numa_work()
3483 * is not already PTE-numa. If the VMA contains in task_numa_work()
3489 pages -= (end - start) >> PAGE_SHIFT; in task_numa_work()
3490 virtpages -= (end - start) >> PAGE_SHIFT; in task_numa_work()
3497 } while (end != vma->vm_end); in task_numa_work()
3500 vma->numab_state->prev_scan_seq = mm->numa_scan_seq; in task_numa_work()
3528 mm->numa_scan_offset = start; in task_numa_work()
3539 if (unlikely(p->se.sum_exec_runtime != runtime)) { in task_numa_work()
3540 u64 diff = p->se.sum_exec_runtime - runtime; in task_numa_work()
3541 p->node_stamp += 32 * diff; in task_numa_work()
3548 struct mm_struct *mm = p->mm; in init_numa_balancing()
3551 mm_users = atomic_read(&mm->mm_users); in init_numa_balancing()
3553 mm->numa_next_scan = jiffies + msecs_to_jiffies(sysctl_numa_balancing_scan_delay); in init_numa_balancing()
3554 mm->numa_scan_seq = 0; in init_numa_balancing()
3557 p->node_stamp = 0; in init_numa_balancing()
3558 p->numa_scan_seq = mm ? mm->numa_scan_seq : 0; in init_numa_balancing()
3559 p->numa_scan_period = sysctl_numa_balancing_scan_delay; in init_numa_balancing()
3560 p->numa_migrate_retry = 0; in init_numa_balancing()
3562 p->numa_work.next = &p->numa_work; in init_numa_balancing()
3563 p->numa_faults = NULL; in init_numa_balancing()
3564 p->numa_pages_migrated = 0; in init_numa_balancing()
3565 p->total_numa_faults = 0; in init_numa_balancing()
3566 RCU_INIT_POINTER(p->numa_group, NULL); in init_numa_balancing()
3567 p->last_task_numa_placement = 0; in init_numa_balancing()
3568 p->last_sum_exec_runtime = 0; in init_numa_balancing()
3570 init_task_work(&p->numa_work, task_numa_work); in init_numa_balancing()
3574 p->numa_preferred_nid = NUMA_NO_NODE; in init_numa_balancing()
3585 delay = min_t(unsigned int, task_scan_max(current), in init_numa_balancing()
3586 current->numa_scan_period * mm_users * NSEC_PER_MSEC); in init_numa_balancing()
3588 p->node_stamp = delay; in init_numa_balancing()
3597 struct callback_head *work = &curr->numa_work; in task_tick_numa()
3603 if (!curr->mm || (curr->flags & (PF_EXITING | PF_KTHREAD)) || work->next != work) in task_tick_numa()
3612 now = curr->se.sum_exec_runtime; in task_tick_numa()
3613 period = (u64)curr->numa_scan_period * NSEC_PER_MSEC; in task_tick_numa()
3615 if (now > curr->node_stamp + period) { in task_tick_numa()
3616 if (!curr->node_stamp) in task_tick_numa()
3617 curr->numa_scan_period = task_scan_start(curr); in task_tick_numa()
3618 curr->node_stamp += period; in task_tick_numa()
3620 if (!time_before(jiffies, curr->mm->numa_next_scan)) in task_tick_numa()
3633 if (!p->mm || !p->numa_faults || (p->flags & PF_EXITING)) in update_scan_period()
3642 * is pulled cross-node due to wakeups or load balancing. in update_scan_period()
3644 if (p->numa_scan_seq) { in update_scan_period()
3650 if (dst_nid == p->numa_preferred_nid || in update_scan_period()
3651 (p->numa_preferred_nid != NUMA_NO_NODE && in update_scan_period()
3652 src_nid != p->numa_preferred_nid)) in update_scan_period()
3656 p->numa_scan_period = task_scan_start(p); in update_scan_period()
3682 update_load_add(&cfs_rq->load, se->load.weight); in account_entity_enqueue()
3687 list_add(&se->group_node, &rq->cfs_tasks); in account_entity_enqueue()
3689 cfs_rq->nr_queued++; in account_entity_enqueue()
3695 update_load_sub(&cfs_rq->load, se->load.weight); in account_entity_dequeue()
3698 list_del_init(&se->group_node); in account_entity_dequeue()
3700 cfs_rq->nr_queued--; in account_entity_dequeue()
3706 * Explicitly do a load-store to ensure the intermediate value never hits
3726 * Explicitly do a load-store to ensure the intermediate value never hits
3734 res = var - val; \
3743 * A variant of sub_positive(), which does not use explicit load-store
3748 *ptr -= min_t(typeof(*ptr), *ptr, _val); \
3754 cfs_rq->avg.load_avg += se->avg.load_avg; in enqueue_load_avg()
3755 cfs_rq->avg.load_sum += se_weight(se) * se->avg.load_sum; in enqueue_load_avg()
3761 sub_positive(&cfs_rq->avg.load_avg, se->avg.load_avg); in dequeue_load_avg()
3762 sub_positive(&cfs_rq->avg.load_sum, se_weight(se) * se->avg.load_sum); in dequeue_load_avg()
3764 cfs_rq->avg.load_sum = max_t(u32, cfs_rq->avg.load_sum, in dequeue_load_avg()
3765 cfs_rq->avg.load_avg * PELT_MIN_DIVIDER); in dequeue_load_avg()
3773 bool curr = cfs_rq->curr == se; in reweight_entity()
3775 if (se->on_rq) { in reweight_entity()
3779 se->deadline -= se->vruntime; in reweight_entity()
3780 se->rel_deadline = 1; in reweight_entity()
3781 cfs_rq->nr_queued--; in reweight_entity()
3784 update_load_sub(&cfs_rq->load, se->load.weight); in reweight_entity()
3789 * Because we keep se->vlag = V - v_i, while: lag_i = w_i*(V - v_i), in reweight_entity()
3790 * we need to scale se->vlag when w_i changes. in reweight_entity()
3792 se->vlag = div_s64(se->vlag * se->load.weight, weight); in reweight_entity()
3793 if (se->rel_deadline) in reweight_entity()
3794 se->deadline = div_s64(se->deadline * se->load.weight, weight); in reweight_entity()
3796 update_load_set(&se->load, weight); in reweight_entity()
3799 u32 divider = get_pelt_divider(&se->avg); in reweight_entity()
3801 se->avg.load_avg = div_u64(se_weight(se) * se->avg.load_sum, divider); in reweight_entity()
3805 if (se->on_rq) { in reweight_entity()
3807 update_load_add(&cfs_rq->load, se->load.weight); in reweight_entity()
3810 cfs_rq->nr_queued++; in reweight_entity()
3814 * whether the rq-wide min_vruntime needs updated too. Since in reweight_entity()
3816 * than up-to-date one, we do the update at the end of the in reweight_entity()
3826 struct sched_entity *se = &p->se; in reweight_task_fair()
3828 struct load_weight *load = &se->load; in reweight_task_fair()
3830 reweight_entity(cfs_rq, se, lw->weight); in reweight_task_fair()
3831 load->inv_weight = lw->inv_weight; in reweight_task_fair()
3844 * tg->weight * grq->load.weight
3845 * ge->load.weight = ----------------------------- (1)
3846 * \Sum grq->load.weight
3854 * grq->load.weight -> grq->avg.load_avg (2)
3858 * tg->weight * grq->avg.load_avg
3859 * ge->load.weight = ------------------------------ (3)
3860 * tg->load_avg
3862 * Where: tg->load_avg ~= \Sum grq->avg.load_avg
3866 * The problem with it is that because the average is slow -- it was designed
3867 * to be exactly that of course -- this leads to transients in boundary
3869 * one task. It takes time for our CPU's grq->avg.load_avg to build up,
3874 * tg->weight * grq->load.weight
3875 * ge->load.weight = ----------------------------- = tg->weight (4)
3876 * grp->load.weight
3883 * ge->load.weight =
3885 * tg->weight * grq->load.weight
3886 * --------------------------------------------------- (5)
3887 * tg->load_avg - grq->avg.load_avg + grq->load.weight
3889 * But because grq->load.weight can drop to 0, resulting in a divide by zero,
3890 * we need to use grq->avg.load_avg as its lower bound, which then gives:
3893 * tg->weight * grq->load.weight
3894 * ge->load.weight = ----------------------------- (6)
3899 * tg_load_avg' = tg->load_avg - grq->avg.load_avg +
3900 * max(grq->load.weight, grq->avg.load_avg)
3904 * overestimates the ge->load.weight and therefore:
3906 * \Sum ge->load.weight >= tg->weight
3913 struct task_group *tg = cfs_rq->tg; in calc_group_shares()
3915 tg_shares = READ_ONCE(tg->shares); in calc_group_shares()
3917 load = max(scale_load_down(cfs_rq->load.weight), cfs_rq->avg.load_avg); in calc_group_shares()
3919 tg_weight = atomic_long_read(&tg->load_avg); in calc_group_shares()
3922 tg_weight -= cfs_rq->tg_load_avg_contrib; in calc_group_shares()
3930 * MIN_SHARES has to be unscaled here to support per-CPU partitioning in calc_group_shares()
3931 * of a group with small tg->shares value. It is a floor value which is in calc_group_shares()
3935 * E.g. on 64-bit for a group with tg->shares of scale_load(15)=15*1024 in calc_group_shares()
3936 * on an 8-core system with 8 tasks each runnable on one CPU shares has in calc_group_shares()
3945 * Recomputes the group entity based on the current state of its group
3957 if (!gcfs_rq || !gcfs_rq->load.weight) in update_cfs_group()
3961 if (unlikely(se->load.weight != shares)) in update_cfs_group()
3975 if (&rq->cfs == cfs_rq) { in cfs_rq_util_change()
3985 * As is, the util number is not freq-invariant (we'd have to in cfs_rq_util_change()
3996 if (sa->load_sum) in load_avg_is_decayed()
3999 if (sa->util_sum) in load_avg_is_decayed()
4002 if (sa->runnable_sum) in load_avg_is_decayed()
4010 WARN_ON_ONCE(sa->load_avg || in load_avg_is_decayed()
4011 sa->util_avg || in load_avg_is_decayed()
4012 sa->runnable_avg); in load_avg_is_decayed()
4019 return u64_u32_load_copy(cfs_rq->avg.last_update_time, in cfs_rq_last_update_time()
4020 cfs_rq->last_update_time_copy); in cfs_rq_last_update_time()
4026 * bottom-up, we only have to test whether the cfs_rq before us on the list
4037 if (cfs_rq->on_list) { in child_cfs_rq_on_list()
4038 prev = cfs_rq->leaf_cfs_rq_list.prev; in child_cfs_rq_on_list()
4040 prev = rq->tmp_alone_branch; in child_cfs_rq_on_list()
4043 if (prev == &rq->leaf_cfs_rq_list) in child_cfs_rq_on_list()
4048 return (prev_cfs_rq->tg->parent == cfs_rq->tg); in child_cfs_rq_on_list()
4053 if (cfs_rq->load.weight) in cfs_rq_is_decayed()
4056 if (!load_avg_is_decayed(&cfs_rq->avg)) in cfs_rq_is_decayed()
4066 * update_tg_load_avg - update the tg's load avg
4069 * This function 'ensures': tg->load_avg := \Sum tg->cfs_rq[]->avg.load.
4070 * However, because tg->load_avg is a global value there are performance
4087 if (cfs_rq->tg == &root_task_group) in update_tg_load_avg()
4095 * For migration heavy workloads, access to tg->load_avg can be in update_tg_load_avg()
4099 if (now - cfs_rq->last_update_tg_load_avg < NSEC_PER_MSEC) in update_tg_load_avg()
4102 delta = cfs_rq->avg.load_avg - cfs_rq->tg_load_avg_contrib; in update_tg_load_avg()
4103 if (abs(delta) > cfs_rq->tg_load_avg_contrib / 64) { in update_tg_load_avg()
4104 atomic_long_add(delta, &cfs_rq->tg->load_avg); in update_tg_load_avg()
4105 cfs_rq->tg_load_avg_contrib = cfs_rq->avg.load_avg; in update_tg_load_avg()
4106 cfs_rq->last_update_tg_load_avg = now; in update_tg_load_avg()
4118 if (cfs_rq->tg == &root_task_group) in clear_tg_load_avg()
4122 delta = 0 - cfs_rq->tg_load_avg_contrib; in clear_tg_load_avg()
4123 atomic_long_add(delta, &cfs_rq->tg->load_avg); in clear_tg_load_avg()
4124 cfs_rq->tg_load_avg_contrib = 0; in clear_tg_load_avg()
4125 cfs_rq->last_update_tg_load_avg = now; in clear_tg_load_avg()
4144 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in clear_tg_offline_cfs_rqs()
4155 * caller only guarantees p->pi_lock is held; no other assumptions,
4156 * including the state of rq->lock, should be made.
4168 * We are supposed to update the task to "current" time, then its up to in set_task_rq_fair()
4170 * getting what current time is, so simply throw away the out-of-date in set_task_rq_fair()
4174 if (!(se->avg.last_update_time && prev)) in set_task_rq_fair()
4181 se->avg.last_update_time = n_last_update_time; in set_task_rq_fair()
4189 * ge->avg == grq->avg (1)
4200 * ge->avg.load_avg = ge->load.weight * ge->avg.runnable_avg (2)
4205 * grq->avg.load_avg = grq->load.weight * grq->avg.runnable_avg (3)
4209 * ge->avg.runnable_avg == grq->avg.runnable_avg
4213 * ge->load.weight * grq->avg.load_avg
4214 * ge->avg.load_avg = ----------------------------------- (4)
4215 * grq->load.weight
4228 * Another reason this doesn't work is that runnable isn't a 0-sum entity.
4239 * ge->avg.running_sum <= ge->avg.runnable_sum <= LOAD_AVG_MAX
4246 * grq->avg.runnable_sum = grq->avg.load_sum / grq->load.weight
4254 long delta_sum, delta_avg = gcfs_rq->avg.util_avg - se->avg.util_avg; in update_tg_cfs_util()
4262 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in update_tg_cfs_util()
4265 divider = get_pelt_divider(&cfs_rq->avg); in update_tg_cfs_util()
4269 se->avg.util_avg = gcfs_rq->avg.util_avg; in update_tg_cfs_util()
4270 new_sum = se->avg.util_avg * divider; in update_tg_cfs_util()
4271 delta_sum = (long)new_sum - (long)se->avg.util_sum; in update_tg_cfs_util()
4272 se->avg.util_sum = new_sum; in update_tg_cfs_util()
4275 add_positive(&cfs_rq->avg.util_avg, delta_avg); in update_tg_cfs_util()
4276 add_positive(&cfs_rq->avg.util_sum, delta_sum); in update_tg_cfs_util()
4279 cfs_rq->avg.util_sum = max_t(u32, cfs_rq->avg.util_sum, in update_tg_cfs_util()
4280 cfs_rq->avg.util_avg * PELT_MIN_DIVIDER); in update_tg_cfs_util()
4286 long delta_sum, delta_avg = gcfs_rq->avg.runnable_avg - se->avg.runnable_avg; in update_tg_cfs_runnable()
4294 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in update_tg_cfs_runnable()
4297 divider = get_pelt_divider(&cfs_rq->avg); in update_tg_cfs_runnable()
4300 se->avg.runnable_avg = gcfs_rq->avg.runnable_avg; in update_tg_cfs_runnable()
4301 new_sum = se->avg.runnable_avg * divider; in update_tg_cfs_runnable()
4302 delta_sum = (long)new_sum - (long)se->avg.runnable_sum; in update_tg_cfs_runnable()
4303 se->avg.runnable_sum = new_sum; in update_tg_cfs_runnable()
4306 add_positive(&cfs_rq->avg.runnable_avg, delta_avg); in update_tg_cfs_runnable()
4307 add_positive(&cfs_rq->avg.runnable_sum, delta_sum); in update_tg_cfs_runnable()
4309 cfs_rq->avg.runnable_sum = max_t(u32, cfs_rq->avg.runnable_sum, in update_tg_cfs_runnable()
4310 cfs_rq->avg.runnable_avg * PELT_MIN_DIVIDER); in update_tg_cfs_runnable()
4316 long delta_avg, running_sum, runnable_sum = gcfs_rq->prop_runnable_sum; in update_tg_cfs_load()
4325 gcfs_rq->prop_runnable_sum = 0; in update_tg_cfs_load()
4328 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in update_tg_cfs_load()
4331 divider = get_pelt_divider(&cfs_rq->avg); in update_tg_cfs_load()
4338 runnable_sum += se->avg.load_sum; in update_tg_cfs_load()
4345 if (scale_load_down(gcfs_rq->load.weight)) { in update_tg_cfs_load()
4346 load_sum = div_u64(gcfs_rq->avg.load_sum, in update_tg_cfs_load()
4347 scale_load_down(gcfs_rq->load.weight)); in update_tg_cfs_load()
4351 runnable_sum = min(se->avg.load_sum, load_sum); in update_tg_cfs_load()
4360 running_sum = se->avg.util_sum >> SCHED_CAPACITY_SHIFT; in update_tg_cfs_load()
4366 delta_avg = load_avg - se->avg.load_avg; in update_tg_cfs_load()
4370 delta_sum = load_sum - (s64)se_weight(se) * se->avg.load_sum; in update_tg_cfs_load()
4372 se->avg.load_sum = runnable_sum; in update_tg_cfs_load()
4373 se->avg.load_avg = load_avg; in update_tg_cfs_load()
4374 add_positive(&cfs_rq->avg.load_avg, delta_avg); in update_tg_cfs_load()
4375 add_positive(&cfs_rq->avg.load_sum, delta_sum); in update_tg_cfs_load()
4377 cfs_rq->avg.load_sum = max_t(u32, cfs_rq->avg.load_sum, in update_tg_cfs_load()
4378 cfs_rq->avg.load_avg * PELT_MIN_DIVIDER); in update_tg_cfs_load()
4383 cfs_rq->propagate = 1; in add_tg_cfs_propagate()
4384 cfs_rq->prop_runnable_sum += runnable_sum; in add_tg_cfs_propagate()
4396 if (!gcfs_rq->propagate) in propagate_entity_load_avg()
4399 gcfs_rq->propagate = 0; in propagate_entity_load_avg()
4403 add_tg_cfs_propagate(cfs_rq, gcfs_rq->prop_runnable_sum); in propagate_entity_load_avg()
4427 if (se->avg.load_avg || se->avg.util_avg) in skip_blocked_update()
4434 if (gcfs_rq->propagate) in skip_blocked_update()
4468 if (load_avg_is_decayed(&se->avg)) in migrate_se_pelt_lag()
4475 is_idle = is_idle_task(rcu_dereference(rq->curr)); in migrate_se_pelt_lag()
4492 * - cfs->throttled_clock_pelt_time@cfs_rq_idle in migrate_se_pelt_lag()
4495 * = rq_clock_pelt()@rq_idle - rq_clock_pelt()@cfs_rq_idle in migrate_se_pelt_lag()
4498 * = sched_clock_cpu() - rq_clock()@rq_idle in migrate_se_pelt_lag()
4502 * now = rq_clock_pelt()@rq_idle - cfs->throttled_clock_pelt_time + in migrate_se_pelt_lag()
4503 * sched_clock_cpu() - rq_clock()@rq_idle in migrate_se_pelt_lag()
4505 * rq_clock_pelt()@rq_idle is rq->clock_pelt_idle in migrate_se_pelt_lag()
4506 * rq_clock()@rq_idle is rq->clock_idle in migrate_se_pelt_lag()
4507 * cfs->throttled_clock_pelt_time@cfs_rq_idle in migrate_se_pelt_lag()
4508 * is cfs_rq->throttled_pelt_idle in migrate_se_pelt_lag()
4512 throttled = u64_u32_load(cfs_rq->throttled_pelt_idle); in migrate_se_pelt_lag()
4517 now = u64_u32_load(rq->clock_pelt_idle); in migrate_se_pelt_lag()
4527 now -= throttled; in migrate_se_pelt_lag()
4530 * cfs_rq->avg.last_update_time is more recent than our in migrate_se_pelt_lag()
4535 now += sched_clock_cpu(cpu_of(rq)) - u64_u32_load(rq->clock_idle); in migrate_se_pelt_lag()
4544 * update_cfs_rq_load_avg - update the cfs_rq's load/util averages
4545 * @now: current time, as per cfs_rq_clock_pelt()
4551 * cfs_rq->avg is used for task_h_load() and update_cfs_share() for example.
4555 * Since both these conditions indicate a changed cfs_rq->avg.load we should
4562 struct sched_avg *sa = &cfs_rq->avg; in update_cfs_rq_load_avg()
4565 if (cfs_rq->removed.nr) { in update_cfs_rq_load_avg()
4567 u32 divider = get_pelt_divider(&cfs_rq->avg); in update_cfs_rq_load_avg()
4569 raw_spin_lock(&cfs_rq->removed.lock); in update_cfs_rq_load_avg()
4570 swap(cfs_rq->removed.util_avg, removed_util); in update_cfs_rq_load_avg()
4571 swap(cfs_rq->removed.load_avg, removed_load); in update_cfs_rq_load_avg()
4572 swap(cfs_rq->removed.runnable_avg, removed_runnable); in update_cfs_rq_load_avg()
4573 cfs_rq->removed.nr = 0; in update_cfs_rq_load_avg()
4574 raw_spin_unlock(&cfs_rq->removed.lock); in update_cfs_rq_load_avg()
4577 sub_positive(&sa->load_avg, r); in update_cfs_rq_load_avg()
4578 sub_positive(&sa->load_sum, r * divider); in update_cfs_rq_load_avg()
4579 /* See sa->util_sum below */ in update_cfs_rq_load_avg()
4580 sa->load_sum = max_t(u32, sa->load_sum, sa->load_avg * PELT_MIN_DIVIDER); in update_cfs_rq_load_avg()
4583 sub_positive(&sa->util_avg, r); in update_cfs_rq_load_avg()
4584 sub_positive(&sa->util_sum, r * divider); in update_cfs_rq_load_avg()
4586 * Because of rounding, se->util_sum might ends up being +1 more than in update_cfs_rq_load_avg()
4587 * cfs->util_sum. Although this is not a problem by itself, detaching in update_cfs_rq_load_avg()
4589 * util_avg (~1ms) can make cfs->util_sum becoming null whereas in update_cfs_rq_load_avg()
4596 sa->util_sum = max_t(u32, sa->util_sum, sa->util_avg * PELT_MIN_DIVIDER); in update_cfs_rq_load_avg()
4599 sub_positive(&sa->runnable_avg, r); in update_cfs_rq_load_avg()
4600 sub_positive(&sa->runnable_sum, r * divider); in update_cfs_rq_load_avg()
4601 /* See sa->util_sum above */ in update_cfs_rq_load_avg()
4602 sa->runnable_sum = max_t(u32, sa->runnable_sum, in update_cfs_rq_load_avg()
4603 sa->runnable_avg * PELT_MIN_DIVIDER); in update_cfs_rq_load_avg()
4610 -(long)(removed_runnable * divider) >> SCHED_CAPACITY_SHIFT); in update_cfs_rq_load_avg()
4616 u64_u32_store_copy(sa->last_update_time, in update_cfs_rq_load_avg()
4617 cfs_rq->last_update_time_copy, in update_cfs_rq_load_avg()
4618 sa->last_update_time); in update_cfs_rq_load_avg()
4623 * attach_entity_load_avg - attach this entity to its cfs_rq load avg
4628 * cfs_rq->avg.last_update_time being current.
4633 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in attach_entity_load_avg()
4636 u32 divider = get_pelt_divider(&cfs_rq->avg); in attach_entity_load_avg()
4645 se->avg.last_update_time = cfs_rq->avg.last_update_time; in attach_entity_load_avg()
4646 se->avg.period_contrib = cfs_rq->avg.period_contrib; in attach_entity_load_avg()
4654 se->avg.util_sum = se->avg.util_avg * divider; in attach_entity_load_avg()
4656 se->avg.runnable_sum = se->avg.runnable_avg * divider; in attach_entity_load_avg()
4658 se->avg.load_sum = se->avg.load_avg * divider; in attach_entity_load_avg()
4659 if (se_weight(se) < se->avg.load_sum) in attach_entity_load_avg()
4660 se->avg.load_sum = div_u64(se->avg.load_sum, se_weight(se)); in attach_entity_load_avg()
4662 se->avg.load_sum = 1; in attach_entity_load_avg()
4665 cfs_rq->avg.util_avg += se->avg.util_avg; in attach_entity_load_avg()
4666 cfs_rq->avg.util_sum += se->avg.util_sum; in attach_entity_load_avg()
4667 cfs_rq->avg.runnable_avg += se->avg.runnable_avg; in attach_entity_load_avg()
4668 cfs_rq->avg.runnable_sum += se->avg.runnable_sum; in attach_entity_load_avg()
4670 add_tg_cfs_propagate(cfs_rq, se->avg.load_sum); in attach_entity_load_avg()
4678 * detach_entity_load_avg - detach this entity from its cfs_rq load avg
4683 * cfs_rq->avg.last_update_time being current.
4688 sub_positive(&cfs_rq->avg.util_avg, se->avg.util_avg); in detach_entity_load_avg()
4689 sub_positive(&cfs_rq->avg.util_sum, se->avg.util_sum); in detach_entity_load_avg()
4691 cfs_rq->avg.util_sum = max_t(u32, cfs_rq->avg.util_sum, in detach_entity_load_avg()
4692 cfs_rq->avg.util_avg * PELT_MIN_DIVIDER); in detach_entity_load_avg()
4694 sub_positive(&cfs_rq->avg.runnable_avg, se->avg.runnable_avg); in detach_entity_load_avg()
4695 sub_positive(&cfs_rq->avg.runnable_sum, se->avg.runnable_sum); in detach_entity_load_avg()
4697 cfs_rq->avg.runnable_sum = max_t(u32, cfs_rq->avg.runnable_sum, in detach_entity_load_avg()
4698 cfs_rq->avg.runnable_avg * PELT_MIN_DIVIDER); in detach_entity_load_avg()
4700 add_tg_cfs_propagate(cfs_rq, -se->avg.load_sum); in detach_entity_load_avg()
4725 if (se->avg.last_update_time && !(flags & SKIP_AGE_LOAD)) in update_load_avg()
4731 if (!se->avg.last_update_time && (flags & DO_ATTACH)) { in update_load_avg()
4781 * tasks cannot exit without having gone through wake_up_new_task() -> in remove_entity_load_avg()
4788 raw_spin_lock_irqsave(&cfs_rq->removed.lock, flags); in remove_entity_load_avg()
4789 ++cfs_rq->removed.nr; in remove_entity_load_avg()
4790 cfs_rq->removed.util_avg += se->avg.util_avg; in remove_entity_load_avg()
4791 cfs_rq->removed.load_avg += se->avg.load_avg; in remove_entity_load_avg()
4792 cfs_rq->removed.runnable_avg += se->avg.runnable_avg; in remove_entity_load_avg()
4793 raw_spin_unlock_irqrestore(&cfs_rq->removed.lock, flags); in remove_entity_load_avg()
4798 return cfs_rq->avg.runnable_avg; in cfs_rq_runnable_avg()
4803 return cfs_rq->avg.load_avg; in cfs_rq_load_avg()
4810 return READ_ONCE(p->se.avg.util_avg); in task_util()
4815 return READ_ONCE(p->se.avg.runnable_avg); in task_runnable()
4820 return READ_ONCE(p->se.avg.util_est) & ~UTIL_AVG_UNCHANGED; in _task_util_est()
4837 enqueued = cfs_rq->avg.util_est; in util_est_enqueue()
4839 WRITE_ONCE(cfs_rq->avg.util_est, enqueued); in util_est_enqueue()
4853 enqueued = cfs_rq->avg.util_est; in util_est_dequeue()
4854 enqueued -= min_t(unsigned int, enqueued, _task_util_est(p)); in util_est_dequeue()
4855 WRITE_ONCE(cfs_rq->avg.util_est, enqueued); in util_est_dequeue()
4878 /* Get current estimate of utilization */ in util_est_update()
4879 ewma = READ_ONCE(p->se.avg.util_est); in util_est_update()
4904 last_ewma_diff = ewma - dequeued; in util_est_update()
4923 * ewma(t) = w * task_util(p) + (1-w) * ewma(t-1) in util_est_update()
4924 * = w * task_util(p) + ewma(t-1) - w * ewma(t-1) in util_est_update()
4925 * = w * (task_util(p) - ewma(t-1)) + ewma(t-1) in util_est_update()
4926 * = w * ( -last_ewma_diff ) + ewma(t-1) in util_est_update()
4927 * = w * (-last_ewma_diff + ewma(t-1) / w) in util_est_update()
4933 ewma -= last_ewma_diff; in util_est_update()
4937 WRITE_ONCE(p->se.avg.util_est, ewma); in util_est_update()
4939 trace_sched_util_est_se_tp(&p->se); in util_est_update()
4946 capacity -= max(hw_load_avg(cpu_rq(cpu)), cpufreq_get_pressure(cpu)); in get_actual_cpu_capacity()
5004 * +---------------------------------------- in util_fits_cpu()
5042 * +---------------------------------------- in util_fits_cpu()
5065 return -1; in util_fits_cpu()
5093 if (!p || (p->nr_cpus_allowed == 1) || in update_misfit_status()
5094 (arch_scale_cpu_capacity(cpu) == p->max_allowed_capacity) || in update_misfit_status()
5097 rq->misfit_task_load = 0; in update_misfit_status()
5105 rq->misfit_task_load = max_t(unsigned long, task_h_load(p), 1); in update_misfit_status()
5110 struct sched_entity *se = &p->se; in __setparam_fair()
5112 p->static_prio = NICE_TO_PRIO(attr->sched_nice); in __setparam_fair()
5113 if (attr->sched_runtime) { in __setparam_fair()
5114 se->custom_slice = 1; in __setparam_fair()
5115 se->slice = clamp_t(u64, attr->sched_runtime, in __setparam_fair()
5119 se->custom_slice = 0; in __setparam_fair()
5120 se->slice = sysctl_sched_base_slice; in __setparam_fair()
5130 if (!se->custom_slice) in place_entity()
5131 se->slice = sysctl_sched_base_slice; in place_entity()
5132 vslice = calc_delta_fair(se->slice, se); in place_entity()
5142 if (sched_feat(PLACE_LAG) && cfs_rq->nr_queued && se->vlag) { in place_entity()
5143 struct sched_entity *curr = cfs_rq->curr; in place_entity()
5146 lag = se->vlag; in place_entity()
5156 * lag_i = S - s_i = w_i * (V - v_i) in place_entity()
5161 * vl_i = V - v_i <=> v_i = V - vl_i in place_entity()
5173 * = (W*V + w_i*(V - vl_i)) / (W + w_i) in place_entity()
5174 * = (W*V + w_i*V - w_i*vl_i) / (W + w_i) in place_entity()
5175 * = (V*(W + w_i) - w_i*vl_i) / (W + w_i) in place_entity()
5176 * = V - w_i*vl_i / (W + w_i) in place_entity()
5180 * vl'_i = V' - v_i in place_entity()
5181 * = V - w_i*vl_i / (W + w_i) - (V - vl_i) in place_entity()
5182 * = vl_i - w_i*vl_i / (W + w_i) in place_entity()
5192 * vl'_i = vl_i - w_i*vl_i / (W + w_i) in place_entity()
5193 * = ((W + w_i)*vl_i - w_i*vl_i) / (W + w_i) in place_entity()
5195 * (W + w_i)*vl'_i = (W + w_i)*vl_i - w_i*vl_i in place_entity()
5200 load = cfs_rq->avg_load; in place_entity()
5201 if (curr && curr->on_rq) in place_entity()
5202 load += scale_load_down(curr->load.weight); in place_entity()
5204 lag *= load + scale_load_down(se->load.weight); in place_entity()
5210 se->vruntime = vruntime - lag; in place_entity()
5212 if (se->rel_deadline) { in place_entity()
5213 se->deadline += se->vruntime; in place_entity()
5214 se->rel_deadline = 0; in place_entity()
5229 se->deadline = se->vruntime + vslice; in place_entity()
5241 bool curr = cfs_rq->curr == se; in enqueue_entity()
5244 * If we're the current task, we must renormalise before calling in enqueue_entity()
5254 * - Update loads to have both entity and cfs_rq synced with now. in enqueue_entity()
5255 * - For group_entity, update its runnable_weight to reflect the new in enqueue_entity()
5257 * - For group_entity, update its weight to reflect the new share of in enqueue_entity()
5259 * - Add its new weight to cfs_rq->load.weight in enqueue_entity()
5265 * but update_cfs_group() here will re-adjust the weight and have to in enqueue_entity()
5271 * XXX now that the entity has been re-weighted, and it's lag adjusted, in enqueue_entity()
5281 se->exec_start = 0; in enqueue_entity()
5287 se->on_rq = 1; in enqueue_entity()
5289 if (cfs_rq->nr_queued == 1) { in enqueue_entity()
5293 if (cfs_rq->pelt_clock_throttled) { in enqueue_entity()
5296 cfs_rq->throttled_clock_pelt_time += rq_clock_pelt(rq) - in enqueue_entity()
5297 cfs_rq->throttled_clock_pelt; in enqueue_entity()
5298 cfs_rq->pelt_clock_throttled = 0; in enqueue_entity()
5308 if (cfs_rq->next != se) in __clear_buddies_next()
5311 cfs_rq->next = NULL; in __clear_buddies_next()
5317 if (cfs_rq->next == se) in clear_buddies()
5325 se->sched_delayed = 1; in set_delayed()
5338 cfs_rq->h_nr_runnable--; in set_delayed()
5344 se->sched_delayed = 0; in clear_delayed()
5358 cfs_rq->h_nr_runnable++; in clear_delayed()
5365 if (sched_feat(DELAY_ZERO) && se->vlag > 0) in finish_delayed_dequeue_entity()
5366 se->vlag = 0; in finish_delayed_dequeue_entity()
5379 WARN_ON_ONCE(!se->sched_delayed); in dequeue_entity()
5389 WARN_ON_ONCE(delay && se->sched_delayed); in dequeue_entity()
5404 * - Update loads to have both entity and cfs_rq synced with now. in dequeue_entity()
5405 * - For group_entity, update its runnable_weight to reflect the new in dequeue_entity()
5407 * - Subtract its previous weight from cfs_rq->load.weight. in dequeue_entity()
5408 * - For group entity, update its weight to reflect the new share in dequeue_entity()
5418 se->deadline -= se->vruntime; in dequeue_entity()
5419 se->rel_deadline = 1; in dequeue_entity()
5422 if (se != cfs_rq->curr) in dequeue_entity()
5424 se->on_rq = 0; in dequeue_entity()
5436 * further than we started -- i.e. we'll be penalized. in dequeue_entity()
5444 if (cfs_rq->nr_queued == 0) { in dequeue_entity()
5451 cfs_rq->throttled_clock_pelt = rq_clock_pelt(rq); in dequeue_entity()
5452 cfs_rq->pelt_clock_throttled = 1; in dequeue_entity()
5465 /* 'current' is not kept within the tree. */ in set_next_entity()
5466 if (se->on_rq) { in set_next_entity()
5480 WARN_ON_ONCE(cfs_rq->curr); in set_next_entity()
5481 cfs_rq->curr = se; in set_next_entity()
5486 * when there are only lesser-weight tasks around): in set_next_entity()
5489 rq_of(cfs_rq)->cfs.load.weight >= 2*se->load.weight) { in set_next_entity()
5493 __schedstat_set(stats->slice_max, in set_next_entity()
5494 max((u64)stats->slice_max, in set_next_entity()
5495 se->sum_exec_runtime - se->prev_sum_exec_runtime)); in set_next_entity()
5498 se->prev_sum_exec_runtime = se->sum_exec_runtime; in set_next_entity()
5516 * Picking the ->next buddy will affect latency but not fairness. in pick_next_entity()
5519 cfs_rq->next && entity_eligible(cfs_rq, cfs_rq->next)) { in pick_next_entity()
5520 /* ->next will never be delayed */ in pick_next_entity()
5521 WARN_ON_ONCE(cfs_rq->next->sched_delayed); in pick_next_entity()
5522 return cfs_rq->next; in pick_next_entity()
5526 if (se->sched_delayed) { in pick_next_entity()
5544 if (prev->on_rq) in put_prev_entity()
5550 if (prev->on_rq) { in put_prev_entity()
5552 /* Put 'current' back into the tree. */ in put_prev_entity()
5557 WARN_ON_ONCE(cfs_rq->curr != prev); in put_prev_entity()
5558 cfs_rq->curr = NULL; in put_prev_entity()
5565 * Update run-time statistics of the 'current'. in entity_tick()
5628 * directly instead of rq->clock to avoid adding additional synchronization
5629 * around rq->lock.
5631 * requires cfs_b->lock
5637 if (unlikely(cfs_b->quota == RUNTIME_INF)) in __refill_cfs_bandwidth_runtime()
5640 cfs_b->runtime += cfs_b->quota; in __refill_cfs_bandwidth_runtime()
5641 runtime = cfs_b->runtime_snap - cfs_b->runtime; in __refill_cfs_bandwidth_runtime()
5643 cfs_b->burst_time += runtime; in __refill_cfs_bandwidth_runtime()
5644 cfs_b->nr_burst++; in __refill_cfs_bandwidth_runtime()
5647 cfs_b->runtime = min(cfs_b->runtime, cfs_b->quota + cfs_b->burst); in __refill_cfs_bandwidth_runtime()
5648 cfs_b->runtime_snap = cfs_b->runtime; in __refill_cfs_bandwidth_runtime()
5653 return &tg->cfs_bandwidth; in tg_cfs_bandwidth()
5662 lockdep_assert_held(&cfs_b->lock); in __assign_cfs_rq_runtime()
5665 min_amount = target_runtime - cfs_rq->runtime_remaining; in __assign_cfs_rq_runtime()
5667 if (cfs_b->quota == RUNTIME_INF) in __assign_cfs_rq_runtime()
5672 if (cfs_b->runtime > 0) { in __assign_cfs_rq_runtime()
5673 amount = min(cfs_b->runtime, min_amount); in __assign_cfs_rq_runtime()
5674 cfs_b->runtime -= amount; in __assign_cfs_rq_runtime()
5675 cfs_b->idle = 0; in __assign_cfs_rq_runtime()
5679 cfs_rq->runtime_remaining += amount; in __assign_cfs_rq_runtime()
5681 return cfs_rq->runtime_remaining > 0; in __assign_cfs_rq_runtime()
5687 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in assign_cfs_rq_runtime()
5690 raw_spin_lock(&cfs_b->lock); in assign_cfs_rq_runtime()
5692 raw_spin_unlock(&cfs_b->lock); in assign_cfs_rq_runtime()
5700 cfs_rq->runtime_remaining -= delta_exec; in __account_cfs_rq_runtime()
5702 if (likely(cfs_rq->runtime_remaining > 0)) in __account_cfs_rq_runtime()
5705 if (cfs_rq->throttled) in __account_cfs_rq_runtime()
5711 if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr)) in __account_cfs_rq_runtime()
5718 if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled) in account_cfs_rq_runtime()
5726 return cfs_bandwidth_used() && cfs_rq->throttled; in cfs_rq_throttled()
5731 return cfs_bandwidth_used() && cfs_rq->pelt_clock_throttled; in cfs_rq_pelt_clock_throttled()
5737 return cfs_bandwidth_used() && cfs_rq->throttle_count; in throttled_hierarchy()
5742 return throttled_hierarchy(task_group(p)->cfs_rq[dst_cpu]); in lb_throttled_hierarchy()
5747 return cfs_bandwidth_used() && p->throttled; in task_is_throttled()
5758 WARN_ON_ONCE(p != current); in throttle_cfs_rq_work()
5759 p->sched_throttle_work.next = &p->sched_throttle_work; in throttle_cfs_rq_work()
5765 if ((p->flags & PF_EXITING)) in throttle_cfs_rq_work()
5769 se = &p->se; in throttle_cfs_rq_work()
5773 if (p->sched_class != &fair_sched_class) in throttle_cfs_rq_work()
5780 if (!cfs_rq->throttle_count) in throttle_cfs_rq_work()
5784 WARN_ON_ONCE(p->throttled || !list_empty(&p->throttle_node)); in throttle_cfs_rq_work()
5786 list_add(&p->throttle_node, &cfs_rq->throttled_limbo_list); in throttle_cfs_rq_work()
5791 p->throttled = true; in throttle_cfs_rq_work()
5798 init_task_work(&p->sched_throttle_work, throttle_cfs_rq_work); in init_cfs_throttle_work()
5800 p->sched_throttle_work.next = &p->sched_throttle_work; in init_cfs_throttle_work()
5801 INIT_LIST_HEAD(&p->throttle_node); in init_cfs_throttle_work()
5815 WARN_ON_ONCE(p->se.on_rq); in dequeue_throttled_task()
5816 list_del_init(&p->throttle_node); in dequeue_throttled_task()
5820 p->throttled = false; in dequeue_throttled_task()
5834 struct cfs_rq *cfs_rq = cfs_rq_of(&p->se); in enqueue_throttled_task()
5837 WARN_ON_ONCE(!list_empty(&p->throttle_node)); in enqueue_throttled_task()
5844 * Do not do that when @p is current because the following race can in enqueue_throttled_task()
5845 * cause @p's group_node to be incorectly re-insterted in its rq's in enqueue_throttled_task()
5860 * enqueue_task(p) -> p's new cfs_rq in enqueue_throttled_task()
5865 * list_move(&se->group_node, &rq->cfs_tasks); // bug in enqueue_throttled_task()
5868 * In the above race case, @p current cfs_rq is in the same rq as in enqueue_throttled_task()
5870 * to a different group from the same rq, so we can use its current in enqueue_throttled_task()
5871 * cfs_rq to derive rq and test if the task is current. in enqueue_throttled_task()
5875 list_add(&p->throttle_node, &cfs_rq->throttled_limbo_list); in enqueue_throttled_task()
5880 p->throttled = false; in enqueue_throttled_task()
5888 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in tg_unthrottle_up()
5891 if (--cfs_rq->throttle_count) in tg_unthrottle_up()
5894 if (cfs_rq->pelt_clock_throttled) { in tg_unthrottle_up()
5895 cfs_rq->throttled_clock_pelt_time += rq_clock_pelt(rq) - in tg_unthrottle_up()
5896 cfs_rq->throttled_clock_pelt; in tg_unthrottle_up()
5897 cfs_rq->pelt_clock_throttled = 0; in tg_unthrottle_up()
5900 if (cfs_rq->throttled_clock_self) { in tg_unthrottle_up()
5901 u64 delta = rq_clock(rq) - cfs_rq->throttled_clock_self; in tg_unthrottle_up()
5903 cfs_rq->throttled_clock_self = 0; in tg_unthrottle_up()
5908 cfs_rq->throttled_clock_self_time += delta; in tg_unthrottle_up()
5911 /* Re-enqueue the tasks that have been throttled at this level. */ in tg_unthrottle_up()
5912 list_for_each_entry_safe(p, tmp, &cfs_rq->throttled_limbo_list, throttle_node) { in tg_unthrottle_up()
5913 list_del_init(&p->throttle_node); in tg_unthrottle_up()
5914 p->throttled = false; in tg_unthrottle_up()
5927 return p->sched_throttle_work.next != &p->sched_throttle_work; in task_has_throttle_work()
5939 if ((p->flags & (PF_EXITING | PF_KTHREAD))) in task_throttle_setup_work()
5942 task_work_add(p, &p->sched_throttle_work, TWA_RESUME); in task_throttle_setup_work()
5949 if (cfs_rq_throttled(cfs_rq) && !cfs_rq->throttled_clock) in record_throttle_clock()
5950 cfs_rq->throttled_clock = rq_clock(rq); in record_throttle_clock()
5952 if (!cfs_rq->throttled_clock_self) in record_throttle_clock()
5953 cfs_rq->throttled_clock_self = rq_clock(rq); in record_throttle_clock()
5959 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in tg_throttle_down()
5961 if (cfs_rq->throttle_count++) in tg_throttle_down()
5968 if (!cfs_rq->nr_queued) { in tg_throttle_down()
5970 cfs_rq->throttled_clock_pelt = rq_clock_pelt(rq); in tg_throttle_down()
5971 cfs_rq->pelt_clock_throttled = 1; in tg_throttle_down()
5974 WARN_ON_ONCE(cfs_rq->throttled_clock_self); in tg_throttle_down()
5975 WARN_ON_ONCE(!list_empty(&cfs_rq->throttled_limbo_list)); in tg_throttle_down()
5982 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in throttle_cfs_rq()
5985 raw_spin_lock(&cfs_b->lock); in throttle_cfs_rq()
5998 list_add_tail_rcu(&cfs_rq->throttled_list, in throttle_cfs_rq()
5999 &cfs_b->throttled_cfs_rq); in throttle_cfs_rq()
6001 raw_spin_unlock(&cfs_b->lock); in throttle_cfs_rq()
6008 walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq); in throttle_cfs_rq()
6013 * throttled-list. rq->lock protects completion. in throttle_cfs_rq()
6015 cfs_rq->throttled = 1; in throttle_cfs_rq()
6016 WARN_ON_ONCE(cfs_rq->throttled_clock); in throttle_cfs_rq()
6023 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in unthrottle_cfs_rq()
6024 struct sched_entity *se = cfs_rq->tg->se[cpu_of(rq)]; in unthrottle_cfs_rq()
6035 if (cfs_rq->runtime_enabled && cfs_rq->runtime_remaining <= 0) in unthrottle_cfs_rq()
6038 cfs_rq->throttled = 0; in unthrottle_cfs_rq()
6042 raw_spin_lock(&cfs_b->lock); in unthrottle_cfs_rq()
6043 if (cfs_rq->throttled_clock) { in unthrottle_cfs_rq()
6044 cfs_b->throttled_time += rq_clock(rq) - cfs_rq->throttled_clock; in unthrottle_cfs_rq()
6045 cfs_rq->throttled_clock = 0; in unthrottle_cfs_rq()
6047 list_del_rcu(&cfs_rq->throttled_list); in unthrottle_cfs_rq()
6048 raw_spin_unlock(&cfs_b->lock); in unthrottle_cfs_rq()
6051 walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq); in unthrottle_cfs_rq()
6053 if (!cfs_rq->load.weight) { in unthrottle_cfs_rq()
6054 if (!cfs_rq->on_list) in unthrottle_cfs_rq()
6069 if (rq->curr == rq->idle && rq->cfs.nr_queued) in unthrottle_cfs_rq()
6098 list_for_each_entry_safe(cursor, tmp, &rq->cfsb_csd_list, in __cfsb_csd_unthrottle()
6100 list_del_init(&cursor->throttled_csd_list); in __cfsb_csd_unthrottle()
6123 if (WARN_ON_ONCE(!list_empty(&cfs_rq->throttled_csd_list))) in __unthrottle_cfs_rq_async()
6126 first = list_empty(&rq->cfsb_csd_list); in __unthrottle_cfs_rq_async()
6127 list_add_tail(&cfs_rq->throttled_csd_list, &rq->cfsb_csd_list); in __unthrottle_cfs_rq_async()
6129 smp_call_function_single_async(cpu_of(rq), &rq->cfsb_csd); in __unthrottle_cfs_rq_async()
6137 cfs_rq->runtime_remaining <= 0)) in unthrottle_cfs_rq_async()
6154 list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq, in distribute_cfs_runtime()
6168 if (!list_empty(&cfs_rq->throttled_csd_list)) in distribute_cfs_runtime()
6172 WARN_ON_ONCE(cfs_rq->runtime_remaining > 0); in distribute_cfs_runtime()
6174 raw_spin_lock(&cfs_b->lock); in distribute_cfs_runtime()
6175 runtime = -cfs_rq->runtime_remaining + 1; in distribute_cfs_runtime()
6176 if (runtime > cfs_b->runtime) in distribute_cfs_runtime()
6177 runtime = cfs_b->runtime; in distribute_cfs_runtime()
6178 cfs_b->runtime -= runtime; in distribute_cfs_runtime()
6179 remaining = cfs_b->runtime; in distribute_cfs_runtime()
6180 raw_spin_unlock(&cfs_b->lock); in distribute_cfs_runtime()
6182 cfs_rq->runtime_remaining += runtime; in distribute_cfs_runtime()
6185 if (cfs_rq->runtime_remaining > 0) { in distribute_cfs_runtime()
6194 list_add_tail(&cfs_rq->throttled_csd_list, in distribute_cfs_runtime()
6211 list_del_init(&cfs_rq->throttled_csd_list); in distribute_cfs_runtime()
6228 * period the timer is deactivated until scheduling resumes; cfs_b->idle is
6236 if (cfs_b->quota == RUNTIME_INF) in do_sched_cfs_period_timer()
6239 throttled = !list_empty(&cfs_b->throttled_cfs_rq); in do_sched_cfs_period_timer()
6240 cfs_b->nr_periods += overrun; in do_sched_cfs_period_timer()
6242 /* Refill extra burst quota even if cfs_b->idle */ in do_sched_cfs_period_timer()
6249 if (cfs_b->idle && !throttled) in do_sched_cfs_period_timer()
6254 cfs_b->idle = 1; in do_sched_cfs_period_timer()
6259 cfs_b->nr_throttled += overrun; in do_sched_cfs_period_timer()
6262 * This check is repeated as we release cfs_b->lock while we unthrottle. in do_sched_cfs_period_timer()
6264 while (throttled && cfs_b->runtime > 0) { in do_sched_cfs_period_timer()
6265 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in do_sched_cfs_period_timer()
6266 /* we can't nest cfs_b->lock while distributing bandwidth */ in do_sched_cfs_period_timer()
6268 raw_spin_lock_irqsave(&cfs_b->lock, flags); in do_sched_cfs_period_timer()
6277 cfs_b->idle = 0; in do_sched_cfs_period_timer()
6293 * Are we near the end of the current quota period?
6295 * Requires cfs_b->lock for hrtimer_expires_remaining to be safe against the
6301 struct hrtimer *refresh_timer = &cfs_b->period_timer; in runtime_refresh_within()
6304 /* if the call-back is running a quota refresh is already occurring */ in runtime_refresh_within()
6325 if (cfs_b->slack_started) in start_cfs_slack_bandwidth()
6327 cfs_b->slack_started = true; in start_cfs_slack_bandwidth()
6329 hrtimer_start(&cfs_b->slack_timer, in start_cfs_slack_bandwidth()
6337 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in __return_cfs_rq_runtime()
6338 s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime; in __return_cfs_rq_runtime()
6343 raw_spin_lock(&cfs_b->lock); in __return_cfs_rq_runtime()
6344 if (cfs_b->quota != RUNTIME_INF) { in __return_cfs_rq_runtime()
6345 cfs_b->runtime += slack_runtime; in __return_cfs_rq_runtime()
6347 /* we are under rq->lock, defer unthrottling using a timer */ in __return_cfs_rq_runtime()
6348 if (cfs_b->runtime > sched_cfs_bandwidth_slice() && in __return_cfs_rq_runtime()
6349 !list_empty(&cfs_b->throttled_cfs_rq)) in __return_cfs_rq_runtime()
6352 raw_spin_unlock(&cfs_b->lock); in __return_cfs_rq_runtime()
6355 cfs_rq->runtime_remaining -= slack_runtime; in __return_cfs_rq_runtime()
6363 if (!cfs_rq->runtime_enabled || cfs_rq->nr_queued) in return_cfs_rq_runtime()
6371 * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
6379 raw_spin_lock_irqsave(&cfs_b->lock, flags); in do_sched_cfs_slack_timer()
6380 cfs_b->slack_started = false; in do_sched_cfs_slack_timer()
6383 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in do_sched_cfs_slack_timer()
6387 if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) in do_sched_cfs_slack_timer()
6388 runtime = cfs_b->runtime; in do_sched_cfs_slack_timer()
6390 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in do_sched_cfs_slack_timer()
6401 * runtime as update_curr() throttling can not trigger until it's on-rq.
6408 /* an active group must be handled by the update_curr()->put() path */ in check_enqueue_throttle()
6409 if (!cfs_rq->runtime_enabled || cfs_rq->curr) in check_enqueue_throttle()
6418 if (cfs_rq->runtime_remaining <= 0) in check_enqueue_throttle()
6429 if (!tg->parent) in sync_throttle()
6432 cfs_rq = tg->cfs_rq[cpu]; in sync_throttle()
6433 pcfs_rq = tg->parent->cfs_rq[cpu]; in sync_throttle()
6435 cfs_rq->throttle_count = pcfs_rq->throttle_count; in sync_throttle()
6436 cfs_rq->throttled_clock_pelt = rq_clock_pelt(cpu_rq(cpu)); in sync_throttle()
6445 if (cfs_rq->throttle_count) in sync_throttle()
6446 cfs_rq->pelt_clock_throttled = 1; in sync_throttle()
6455 if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0)) in check_cfs_rq_runtime()
6487 raw_spin_lock_irqsave(&cfs_b->lock, flags); in sched_cfs_period_timer()
6489 overrun = hrtimer_forward_now(timer, cfs_b->period); in sched_cfs_period_timer()
6496 u64 new, old = ktime_to_ns(cfs_b->period); in sched_cfs_period_timer()
6505 cfs_b->period = ns_to_ktime(new); in sched_cfs_period_timer()
6506 cfs_b->quota *= 2; in sched_cfs_period_timer()
6507 cfs_b->burst *= 2; in sched_cfs_period_timer()
6513 div_u64(cfs_b->quota, NSEC_PER_USEC)); in sched_cfs_period_timer()
6519 div_u64(cfs_b->quota, NSEC_PER_USEC)); in sched_cfs_period_timer()
6527 cfs_b->period_active = 0; in sched_cfs_period_timer()
6528 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in sched_cfs_period_timer()
6535 raw_spin_lock_init(&cfs_b->lock); in init_cfs_bandwidth()
6536 cfs_b->runtime = 0; in init_cfs_bandwidth()
6537 cfs_b->quota = RUNTIME_INF; in init_cfs_bandwidth()
6538 cfs_b->period = us_to_ktime(default_bw_period_us()); in init_cfs_bandwidth()
6539 cfs_b->burst = 0; in init_cfs_bandwidth()
6540 cfs_b->hierarchical_quota = parent ? parent->hierarchical_quota : RUNTIME_INF; in init_cfs_bandwidth()
6542 INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq); in init_cfs_bandwidth()
6543 hrtimer_setup(&cfs_b->period_timer, sched_cfs_period_timer, CLOCK_MONOTONIC, in init_cfs_bandwidth()
6547 hrtimer_set_expires(&cfs_b->period_timer, in init_cfs_bandwidth()
6548 get_random_u32_below(cfs_b->period)); in init_cfs_bandwidth()
6549 hrtimer_setup(&cfs_b->slack_timer, sched_cfs_slack_timer, CLOCK_MONOTONIC, in init_cfs_bandwidth()
6551 cfs_b->slack_started = false; in init_cfs_bandwidth()
6556 cfs_rq->runtime_enabled = 0; in init_cfs_rq_runtime()
6557 INIT_LIST_HEAD(&cfs_rq->throttled_list); in init_cfs_rq_runtime()
6558 INIT_LIST_HEAD(&cfs_rq->throttled_csd_list); in init_cfs_rq_runtime()
6559 INIT_LIST_HEAD(&cfs_rq->throttled_limbo_list); in init_cfs_rq_runtime()
6564 lockdep_assert_held(&cfs_b->lock); in start_cfs_bandwidth()
6566 if (cfs_b->period_active) in start_cfs_bandwidth()
6569 cfs_b->period_active = 1; in start_cfs_bandwidth()
6570 hrtimer_forward_now(&cfs_b->period_timer, cfs_b->period); in start_cfs_bandwidth()
6571 hrtimer_start_expires(&cfs_b->period_timer, HRTIMER_MODE_ABS_PINNED); in start_cfs_bandwidth()
6579 if (!cfs_b->throttled_cfs_rq.next) in destroy_cfs_bandwidth()
6582 hrtimer_cancel(&cfs_b->period_timer); in destroy_cfs_bandwidth()
6583 hrtimer_cancel(&cfs_b->slack_timer); in destroy_cfs_bandwidth()
6599 if (list_empty(&rq->cfsb_csd_list)) in destroy_cfs_bandwidth()
6624 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth; in update_runtime_enabled()
6625 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in update_runtime_enabled()
6627 raw_spin_lock(&cfs_b->lock); in update_runtime_enabled()
6628 cfs_rq->runtime_enabled = cfs_b->quota != RUNTIME_INF; in update_runtime_enabled()
6629 raw_spin_unlock(&cfs_b->lock); in update_runtime_enabled()
6654 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in unthrottle_offline_cfs_rqs()
6656 if (!cfs_rq->runtime_enabled) in unthrottle_offline_cfs_rqs()
6663 cfs_rq->runtime_enabled = 0; in unthrottle_offline_cfs_rqs()
6672 cfs_rq->runtime_remaining = 1; in unthrottle_offline_cfs_rqs()
6687 if (cfs_rq->runtime_enabled || in cfs_task_bw_constrained()
6688 tg_cfs_bandwidth(cfs_rq->tg)->hierarchical_quota != RUNTIME_INF) in cfs_task_bw_constrained()
6706 if (rq->nr_running != 1) in sched_fair_update_stop_tick()
6784 struct sched_entity *se = &p->se; in hrtick_start_fair()
6788 if (rq->cfs.h_nr_queued > 1) { in hrtick_start_fair()
6789 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime; in hrtick_start_fair()
6790 u64 slice = se->slice; in hrtick_start_fair()
6791 s64 delta = slice - ran; in hrtick_start_fair()
6804 * current task is from our class and nr_running is low enough
6809 struct task_struct *donor = rq->donor; in hrtick_update()
6811 if (!hrtick_enabled_fair(rq) || donor->sched_class != &fair_sched_class) in hrtick_update()
6846 return !sched_energy_enabled() || READ_ONCE(rd->overutilized); in is_rd_overutilized()
6854 WRITE_ONCE(rd->overutilized, flag); in set_rd_overutilized()
6865 if (!is_rd_overutilized(rq->rd) && cpu_overutilized(rq->cpu)) in check_update_overutilized_status()
6866 set_rd_overutilized(rq->rd, 1); in check_update_overutilized_status()
6872 return unlikely(rq->nr_running == rq->cfs.h_nr_idle && in sched_idle_rq()
6873 rq->nr_running); in sched_idle_rq()
6887 * se->sched_delayed should imply: se->on_rq == 1. in requeue_delayed_entity()
6891 WARN_ON_ONCE(!se->sched_delayed); in requeue_delayed_entity()
6892 WARN_ON_ONCE(!se->on_rq); in requeue_delayed_entity()
6896 if (se->vlag > 0) { in requeue_delayed_entity()
6897 cfs_rq->nr_queued--; in requeue_delayed_entity()
6898 if (se != cfs_rq->curr) in requeue_delayed_entity()
6900 se->vlag = 0; in requeue_delayed_entity()
6902 if (se != cfs_rq->curr) in requeue_delayed_entity()
6904 cfs_rq->nr_queued++; in requeue_delayed_entity()
6921 struct sched_entity *se = &p->se; in enqueue_task_fair()
6925 int rq_h_nr_queued = rq->cfs.h_nr_queued; in enqueue_task_fair()
6937 if (!p->se.sched_delayed || (flags & ENQUEUE_DELAYED)) in enqueue_task_fair()
6938 util_est_enqueue(&rq->cfs, p); in enqueue_task_fair()
6950 if (p->in_iowait) in enqueue_task_fair()
6953 if (task_new && se->sched_delayed) in enqueue_task_fair()
6957 if (se->on_rq) { in enqueue_task_fair()
6958 if (se->sched_delayed) in enqueue_task_fair()
6967 * its entities in the desired time-frame. in enqueue_task_fair()
6970 se->slice = slice; in enqueue_task_fair()
6971 se->custom_slice = 1; in enqueue_task_fair()
6976 cfs_rq->h_nr_runnable += h_nr_runnable; in enqueue_task_fair()
6977 cfs_rq->h_nr_queued++; in enqueue_task_fair()
6978 cfs_rq->h_nr_idle += h_nr_idle; in enqueue_task_fair()
6993 se->slice = slice; in enqueue_task_fair()
6994 if (se != cfs_rq->curr) in enqueue_task_fair()
6995 min_vruntime_cb_propagate(&se->run_node, NULL); in enqueue_task_fair()
6998 cfs_rq->h_nr_runnable += h_nr_runnable; in enqueue_task_fair()
6999 cfs_rq->h_nr_queued++; in enqueue_task_fair()
7000 cfs_rq->h_nr_idle += h_nr_idle; in enqueue_task_fair()
7006 if (!rq_h_nr_queued && rq->cfs.h_nr_queued) { in enqueue_task_fair()
7008 if (!rq->nr_running) in enqueue_task_fair()
7009 dl_server_update_idle_time(rq, rq->curr); in enqueue_task_fair()
7010 dl_server_start(&rq->fair_server); in enqueue_task_fair()
7042 * failing half-way through and resume the dequeue later.
7045 * -1 - dequeue delayed
7046 * 0 - dequeue throttled
7047 * 1 - dequeue complete
7066 if (task_sleep || task_delayed || !se->sched_delayed) in dequeue_entities()
7074 if (p && &p->se == se) in dequeue_entities()
7075 return -1; in dequeue_entities()
7081 cfs_rq->h_nr_runnable -= h_nr_runnable; in dequeue_entities()
7082 cfs_rq->h_nr_queued -= h_nr_queued; in dequeue_entities()
7083 cfs_rq->h_nr_idle -= h_nr_idle; in dequeue_entities()
7092 if (cfs_rq->load.weight) { in dequeue_entities()
7095 /* Avoid re-evaluating load for this entity: */ in dequeue_entities()
7116 se->slice = slice; in dequeue_entities()
7117 if (se != cfs_rq->curr) in dequeue_entities()
7118 min_vruntime_cb_propagate(&se->run_node, NULL); in dequeue_entities()
7121 cfs_rq->h_nr_runnable -= h_nr_runnable; in dequeue_entities()
7122 cfs_rq->h_nr_queued -= h_nr_queued; in dequeue_entities()
7123 cfs_rq->h_nr_idle -= h_nr_idle; in dequeue_entities()
7136 rq->next_balance = jiffies; in dequeue_entities()
7140 WARN_ON_ONCE(p->on_rq != 1); in dequeue_entities()
7142 /* Fix-up what dequeue_task_fair() skipped */ in dequeue_entities()
7146 * Fix-up what block_task() skipped. in dequeue_entities()
7168 if (!p->se.sched_delayed) in dequeue_task_fair()
7169 util_est_dequeue(&rq->cfs, p); in dequeue_task_fair()
7171 util_est_update(&rq->cfs, p, flags & DEQUEUE_SLEEP); in dequeue_task_fair()
7172 if (dequeue_entities(rq, &p->se, flags) < 0) in dequeue_task_fair()
7185 return (rq->cfs.h_nr_queued - rq->cfs.h_nr_runnable); in cfs_h_nr_delayed()
7208 return cfs_rq_load_avg(&rq->cfs); in cpu_load()
7212 * cpu_load_without - compute CPU load without any contributions from *p
7230 if (cpu_of(rq) != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in cpu_load_without()
7233 cfs_rq = &rq->cfs; in cpu_load_without()
7234 load = READ_ONCE(cfs_rq->avg.load_avg); in cpu_load_without()
7244 return cfs_rq_runnable_avg(&rq->cfs); in cpu_runnable()
7253 if (cpu_of(rq) != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in cpu_runnable_without()
7256 cfs_rq = &rq->cfs; in cpu_runnable_without()
7257 runnable = READ_ONCE(cfs_rq->avg.runnable_avg); in cpu_runnable_without()
7260 lsub_positive(&runnable, p->se.avg.runnable_avg); in cpu_runnable_without()
7267 return cpu_rq(cpu)->cpu_capacity; in capacity_of()
7276 if (time_after(jiffies, current->wakee_flip_decay_ts + HZ)) { in record_wakee()
7277 current->wakee_flips >>= 1; in record_wakee()
7278 current->wakee_flip_decay_ts = jiffies; in record_wakee()
7281 if (current->last_wakee != p) { in record_wakee()
7282 current->last_wakee = p; in record_wakee()
7283 current->wakee_flips++; in record_wakee()
7288 * Detect M:N waker/wakee relationships via a switching-frequency heuristic.
7298 * non-monogamous, with partner count exceeding socket size.
7306 unsigned int master = current->wakee_flips; in wake_wide()
7307 unsigned int slave = p->wakee_flips; in wake_wide()
7322 * wake_affine_idle() - only considers 'now', it check if the waking CPU is
7323 * cache-affine and is (or will be) idle.
7325 * wake_affine_weight() - considers the weight to reflect the average
7350 if ((rq->nr_running - cfs_h_nr_delayed(rq)) == 1) in wake_affine_idle()
7370 unsigned long current_load = task_h_load(current); in wake_affine_weight()
7375 this_eff_load -= current_load; in wake_affine_weight()
7386 prev_eff_load -= task_load; in wake_affine_weight()
7388 prev_eff_load *= 100 + (sd->imbalance_pct - 100) / 2; in wake_affine_weight()
7414 schedstat_inc(p->stats.nr_wakeups_affine_attempts); in wake_affine()
7418 schedstat_inc(sd->ttwu_move_affine); in wake_affine()
7419 schedstat_inc(p->stats.nr_wakeups_affine); in wake_affine()
7427 * sched_balance_find_dst_group_cpu - find the idlest CPU among the CPUs in the group.
7436 int shallowest_idle_cpu = -1; in sched_balance_find_dst_group_cpu()
7440 if (group->group_weight == 1) in sched_balance_find_dst_group_cpu()
7444 for_each_cpu_and(i, sched_group_span(group), p->cpus_ptr) { in sched_balance_find_dst_group_cpu()
7455 if (idle && idle->exit_latency < min_exit_latency) { in sched_balance_find_dst_group_cpu()
7461 min_exit_latency = idle->exit_latency; in sched_balance_find_dst_group_cpu()
7462 latest_idle_timestamp = rq->idle_stamp; in sched_balance_find_dst_group_cpu()
7464 } else if ((!idle || idle->exit_latency == min_exit_latency) && in sched_balance_find_dst_group_cpu()
7465 rq->idle_stamp > latest_idle_timestamp) { in sched_balance_find_dst_group_cpu()
7471 latest_idle_timestamp = rq->idle_stamp; in sched_balance_find_dst_group_cpu()
7474 } else if (shallowest_idle_cpu == -1) { in sched_balance_find_dst_group_cpu()
7483 return shallowest_idle_cpu != -1 ? shallowest_idle_cpu : least_loaded_cpu; in sched_balance_find_dst_group_cpu()
7491 if (!cpumask_intersects(sched_domain_span(sd), p->cpus_ptr)) in sched_balance_find_dst_cpu()
7499 sync_entity_load_avg(&p->se); in sched_balance_find_dst_cpu()
7506 if (!(sd->flags & sd_flag)) { in sched_balance_find_dst_cpu()
7507 sd = sd->child; in sched_balance_find_dst_cpu()
7513 sd = sd->child; in sched_balance_find_dst_cpu()
7520 sd = sd->child; in sched_balance_find_dst_cpu()
7526 weight = sd->span_weight; in sched_balance_find_dst_cpu()
7529 if (weight <= tmp->span_weight) in sched_balance_find_dst_cpu()
7531 if (tmp->flags & sd_flag) in sched_balance_find_dst_cpu()
7545 return -1; in __select_idle_cpu()
7558 WRITE_ONCE(sds->has_idle_cores, val); in set_idle_cores()
7567 return READ_ONCE(sds->has_idle_cores); in test_idle_cores()
7574 * information in sd_llc_shared->has_idle_cores.
7604 * sd_llc->shared->has_idle_cores and enabled through update_idle_core() above.
7614 if (*idle_cpu == -1) { in select_idle_core()
7623 if (*idle_cpu == -1 && cpumask_test_cpu(cpu, cpus)) in select_idle_core()
7631 return -1; in select_idle_core()
7641 for_each_cpu_and(cpu, cpu_smt_mask(target), p->cpus_ptr) { in select_idle_smt()
7654 return -1; in select_idle_smt()
7675 return -1; in select_idle_smt()
7681 * Scan the LLC domain for idle CPUs; this is dynamically regulated by
7682 * comparing the average scan cost (tracked in sd->avg_scan_cost) against the
7683 * average idle time for this rq (as found in rq->avg_idle).
7688 int i, cpu, idle_cpu = -1, nr = INT_MAX; in select_idle_cpu()
7691 cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr); in select_idle_cpu()
7696 /* because !--nr is the condition to stop scan */ in select_idle_cpu()
7697 nr = READ_ONCE(sd_share->nr_idle_scan) + 1; in select_idle_cpu()
7700 return -1; in select_idle_cpu()
7705 struct sched_group *sg = sd->groups; in select_idle_cpu()
7707 if (sg->flags & SD_CLUSTER) { in select_idle_cpu()
7717 if (--nr <= 0) in select_idle_cpu()
7718 return -1; in select_idle_cpu()
7735 if (--nr <= 0) in select_idle_cpu()
7736 return -1; in select_idle_cpu()
7759 int cpu, best_cpu = -1; in select_idle_capacity()
7763 cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr); in select_idle_capacity()
7788 * First, select CPU which fits better (-1 being better than 0). in select_idle_capacity()
7825 int i, recent_used_cpu, prev_aff = -1; in select_idle_sibling()
7832 sync_entity_load_avg(&p->se); in select_idle_sibling()
7839 * per-cpu select_rq_mask usage in select_idle_sibling()
7862 * Allow a per-cpu kthread to stack with the wakee if the in select_idle_sibling()
7865 * per-cpu kthread that is now complete and the wakeup is in select_idle_sibling()
7869 if (is_per_cpu_kthread(current) && in select_idle_sibling()
7872 this_rq()->nr_running <= 1 && in select_idle_sibling()
7878 recent_used_cpu = p->recent_used_cpu; in select_idle_sibling()
7879 p->recent_used_cpu = prev; in select_idle_sibling()
7884 cpumask_test_cpu(recent_used_cpu, p->cpus_ptr) && in select_idle_sibling()
7892 recent_used_cpu = -1; in select_idle_sibling()
7948 * cpu_util() - Estimates the amount of CPU capacity used by CFS tasks.
7951 * @dst_cpu: CPU @p migrates to, -1 if @p moves from @cpu or @p == NULL
7958 * recent utilization of currently non-runnable tasks on that CPU.
7966 * previously-executed tasks, which helps better deduce how busy a CPU will
7967 * be when a long-sleeping task wakes up. The contribution to CPU utilization
7976 * CPU utilization can be higher than the current CPU capacity
7981 * could be seen as over-utilized even though CPU1 has 20% of spare CPU
7982 * capacity. CPU utilization is allowed to overshoot current CPU capacity
7984 * after task migrations (scheduler-driven DVFS).
7991 struct cfs_rq *cfs_rq = &cpu_rq(cpu)->cfs; in cpu_util()
7992 unsigned long util = READ_ONCE(cfs_rq->avg.util_avg); in cpu_util()
7996 runnable = READ_ONCE(cfs_rq->avg.runnable_avg); in cpu_util()
8001 * If @dst_cpu is -1 or @p migrates from @cpu to @dst_cpu remove its in cpu_util()
8014 util_est = READ_ONCE(cfs_rq->avg.util_est); in cpu_util()
8017 * During wake-up @p isn't enqueued yet and doesn't contribute in cpu_util()
8018 * to any cpu_rq(cpu)->cfs.avg.util_est. in cpu_util()
8022 * During exec (@dst_cpu = -1) @p is enqueued and does in cpu_util()
8023 * contribute to cpu_rq(cpu)->cfs.util_est. in cpu_util()
8032 * p->on_rq = TASK_ON_RQ_MIGRATING; in cpu_util()
8033 * -------------------------------- A in cpu_util()
8037 * -------------------------------- B in cpu_util()
8039 * The additional check "current == p" is required to further in cpu_util()
8044 else if (p && unlikely(task_on_rq_queued(p) || current == p)) in cpu_util()
8055 return cpu_util(cpu, NULL, -1, 0); in cpu_util_cfs()
8060 return cpu_util(cpu, NULL, -1, 1); in cpu_util_cfs_boost()
8079 if (cpu != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in cpu_util_without()
8082 return cpu_util(cpu, p, -1, 0); in cpu_util_without()
8097 * The cfs,rt,dl utilization are the running times measured with rq->clock_task
8098 * which excludes things like IRQ and steal-time. These latter are then accrued
8116 * because of inaccuracies in how we track these -- see in effective_cpu_util()
8131 * - the computed DL bandwidth needed with the IRQ pressure which in effective_cpu_util()
8133 * - The minimum performance requirement for CFS and/or RT. in effective_cpu_util()
8141 if (!uclamp_is_used() && rt_rq_is_runnable(&rq->rt)) in effective_cpu_util()
8169 * max - irq in effective_cpu_util()
8170 * U' = irq + --------- * U in effective_cpu_util()
8185 * energy_env - Utilization landscape for energy estimation.
8191 * @pd_cap: Entire perf domain capacity. (pd->nr_cpus * cpu_cap).
8217 eenv->task_busy_time = busy_time; in eenv_task_busy_time()
8231 * - A stable PD utilization, no matter which CPU of that PD we want to place
8234 * - A fair comparison between CPUs as the task contribution (task_util())
8239 * exceed @eenv->pd_cap.
8249 unsigned long util = cpu_util(cpu, p, -1, 0); in eenv_pd_busy_time()
8254 eenv->pd_busy_time = min(eenv->pd_cap, busy_time); in eenv_pd_busy_time()
8261 * Returns the maximum utilization among @eenv->cpus. This utilization can't
8262 * exceed @eenv->cpu_cap.
8303 return min(max_util, eenv->cpu_cap); in eenv_pd_max_util()
8316 unsigned long busy_time = eenv->pd_busy_time; in compute_energy()
8320 busy_time = min(eenv->pd_cap, busy_time + eenv->task_busy_time); in compute_energy()
8322 energy = em_cpu_energy(pd->em_pd, max_util, busy_time, eenv->cpu_cap); in compute_energy()
8330 * find_energy_efficient_cpu(): Find most energy-efficient target CPU for the
8334 * out which of the CPU candidates is the most energy-efficient.
8349 * ways to tell with the current Energy Model if this is actually a good
8351 * cluster-packing, and spreading inside a cluster. That should at least be
8358 * NOTE: Forkees are not accepted in the energy-aware wake-up path because
8362 * to be energy-inefficient in some use-cases. The alternative would be to
8365 * other use-cases too. So, until someone finds a better way to solve this,
8366 * let's keep things simple by re-using the existing slow path.
8374 struct root_domain *rd = this_rq()->rd; in find_energy_efficient_cpu()
8375 int cpu, best_energy_cpu, target = -1; in find_energy_efficient_cpu()
8376 int prev_fits = -1, best_fits = -1; in find_energy_efficient_cpu()
8384 pd = rcu_dereference(rd->pd); in find_energy_efficient_cpu()
8389 * Energy-aware wake-up happens on the lowest sched_domain starting in find_energy_efficient_cpu()
8394 sd = sd->parent; in find_energy_efficient_cpu()
8400 sync_entity_load_avg(&p->se); in find_energy_efficient_cpu()
8406 for (; pd; pd = pd->next) { in find_energy_efficient_cpu()
8409 long prev_spare_cap = -1, max_spare_cap = -1; in find_energy_efficient_cpu()
8412 int max_spare_cap_cpu = -1; in find_energy_efficient_cpu()
8413 int fits, max_fits = -1; in find_energy_efficient_cpu()
8435 if (!cpumask_test_cpu(cpu, p->cpus_ptr)) in find_energy_efficient_cpu()
8454 * max-aggregated uclamp_{min, max}. in find_energy_efficient_cpu()
8491 base_energy = compute_energy(&eenv, pd, cpus, p, -1); in find_energy_efficient_cpu()
8494 if (prev_spare_cap > -1) { in find_energy_efficient_cpu()
8500 prev_delta -= base_energy; in find_energy_efficient_cpu()
8507 /* Current best energy cpu fits better */ in find_energy_efficient_cpu()
8524 cur_delta -= base_energy; in find_energy_efficient_cpu()
8568 int sync = (wake_flags & WF_SYNC) && !(current->flags & PF_EXITING); in select_task_rq_fair()
8577 * required for stable ->cpus_allowed in select_task_rq_fair()
8579 lockdep_assert_held(&p->pi_lock); in select_task_rq_fair()
8584 cpumask_test_cpu(cpu, p->cpus_ptr)) in select_task_rq_fair()
8587 if (!is_rd_overutilized(this_rq()->rd)) { in select_task_rq_fair()
8594 want_affine = !wake_wide(p) && cpumask_test_cpu(cpu, p->cpus_ptr); in select_task_rq_fair()
8603 if (want_affine && (tmp->flags & SD_WAKE_AFFINE) && in select_task_rq_fair()
8617 if (tmp->flags & sd_flag) in select_task_rq_fair()
8638 * previous CPU. The caller guarantees p->pi_lock or task_rq(p)->lock is held.
8642 struct sched_entity *se = &p->se; in migrate_task_rq_fair()
8649 * the current rq's clock. But if that clock hasn't been in migrate_task_rq_fair()
8651 * leading to an inflation after wake-up on the new rq. in migrate_task_rq_fair()
8661 se->avg.last_update_time = 0; in migrate_task_rq_fair()
8668 struct sched_entity *se = &p->se; in task_dead_fair()
8670 if (se->sched_delayed) { in task_dead_fair()
8675 if (se->sched_delayed) { in task_dead_fair()
8700 if (!cpumask_intersects(p->cpus_ptr, cpumask)) in set_task_max_allowed_capacity()
8703 p->max_allowed_capacity = entry->capacity; in set_task_max_allowed_capacity()
8727 if (WARN_ON_ONCE(!se->on_rq)) in set_next_buddy()
8731 cfs_rq_of(se)->next = se; in set_next_buddy()
8736 * Preempt the current task with a newly woken task if needed:
8740 struct task_struct *donor = rq->donor; in check_preempt_wakeup_fair()
8741 struct sched_entity *se = &donor->se, *pse = &p->se; in check_preempt_wakeup_fair()
8753 * next-buddy nomination below. in check_preempt_wakeup_fair()
8758 if (sched_feat(NEXT_BUDDY) && !(wake_flags & WF_FORK) && !pse->sched_delayed) { in check_preempt_wakeup_fair()
8766 * Note: this also catches the edge-case of curr being in a throttled in check_preempt_wakeup_fair()
8772 if (test_tsk_need_resched(rq->curr)) in check_preempt_wakeup_fair()
8785 * Preempt an idle entity in favor of a non-idle entity (and don't preempt in check_preempt_wakeup_fair()
8790 * When non-idle entity preempt an idle entity, in check_preempt_wakeup_fair()
8803 if (unlikely(!normal_policy(p->policy))) in check_preempt_wakeup_fair()
8809 * If @p has a shorter slice than current and @p is eligible, override in check_preempt_wakeup_fair()
8810 * current's slice protection in order to allow preemption. in check_preempt_wakeup_fair()
8812 do_preempt_short = sched_feat(PREEMPT_SHORT) && (pse->slice < se->slice); in check_preempt_wakeup_fair()
8840 cfs_rq = &rq->cfs; in pick_task_fair()
8841 if (!cfs_rq->nr_queued) in pick_task_fair()
8848 if (cfs_rq->curr && cfs_rq->curr->on_rq) in pick_task_fair()
8879 se = &p->se; in pick_next_task_fair()
8882 if (prev->sched_class != &fair_sched_class) in pick_next_task_fair()
8889 * likely that a next task is from the same cgroup as the current. in pick_next_task_fair()
8899 struct sched_entity *pse = &prev->se; in pick_next_task_fair()
8903 int se_depth = se->depth; in pick_next_task_fair()
8904 int pse_depth = pse->depth; in pick_next_task_fair()
8934 * Because sched_balance_newidle() releases (and re-acquires) in pick_next_task_fair()
8935 * rq->lock, it is possible for any higher priority task to in pick_next_task_fair()
8936 * appear. In that case we must re-start the pick_next_entity() in pick_next_task_fair()
8962 return pick_task_fair(dl_se->rq); in fair_server_pick_task()
8967 struct sched_dl_entity *dl_se = &rq->fair_server; in fair_server_init()
8979 struct sched_entity *se = &prev->se; in put_prev_task_fair()
8993 struct task_struct *curr = rq->curr; in yield_task_fair()
8995 struct sched_entity *se = &curr->se; in yield_task_fair()
9000 if (unlikely(rq->nr_running == 1)) in yield_task_fair()
9007 * Update run-time statistics of the 'current'. in yield_task_fair()
9017 se->deadline += calc_delta_fair(se->slice, se); in yield_task_fair()
9022 struct sched_entity *se = &p->se; in yield_to_task_fair()
9024 /* !se->on_rq also covers throttled task */ in yield_to_task_fair()
9025 if (!se->on_rq) in yield_to_task_fair()
9037 * Fair scheduling class load-balancing methods.
9041 * The purpose of load-balancing is to achieve the same basic fairness the
9042 * per-CPU scheduler provides, namely provide a proportional amount of compute
9047 * Where W_i,n is the n-th weight average for CPU i. The instantaneous weight
9052 * Where w_i,j is the weight of the j-th runnable task on CPU i. This weight
9058 * W'_i,n = (2^n - 1) / 2^n * W_i,n + 1 / 2^n * W_i,0 (3)
9067 * imb_i,j = max{ avg(W/C), W_i/C_i } - min{ avg(W/C), W_j/C_j } (4)
9074 * - infeasible weights;
9075 * - local vs global optima in the discrete case. ]
9085 * of load-balance at each level inversely proportional to the number of CPUs in
9091 * \Sum { --- * --- * 2^i } = O(n) (5)
9093 * `- size of each group
9094 * | | `- number of CPUs doing load-balance
9095 * | `- freq
9096 * `- sum over all levels
9138 * W_i,0 = \Sum_j \Prod_k w_k * ----- (9)
9145 * w_i,j,k is the weight of the j-th runnable task in the k-th cgroup on CPU i.
9186 * current CPU.
9227 /* The set of CPUs under consideration for load-balancing */
9242 * Is this task likely cache-hot:
9248 lockdep_assert_rq_held(env->src_rq); in task_hot()
9250 if (p->sched_class != &fair_sched_class) in task_hot()
9257 if (env->sd->flags & SD_SHARE_CPUCAPACITY) in task_hot()
9263 if (sched_feat(CACHE_HOT_BUDDY) && env->dst_rq->nr_running && in task_hot()
9264 (&p->se == cfs_rq_of(&p->se)->next)) in task_hot()
9267 if (sysctl_sched_migration_cost == -1) in task_hot()
9274 if (!sched_core_cookie_match(cpu_rq(env->dst_cpu), p)) in task_hot()
9280 delta = rq_clock_task(env->src_rq) - p->se.exec_start; in task_hot()
9293 struct numa_group *numa_group = rcu_dereference(p->numa_group); in migrate_degrades_locality()
9300 if (!p->numa_faults || !(env->sd->flags & SD_NUMA)) in migrate_degrades_locality()
9303 src_nid = cpu_to_node(env->src_cpu); in migrate_degrades_locality()
9304 dst_nid = cpu_to_node(env->dst_cpu); in migrate_degrades_locality()
9310 if (src_nid == p->numa_preferred_nid) { in migrate_degrades_locality()
9311 if (env->src_rq->nr_running > env->src_rq->nr_preferred_running) in migrate_degrades_locality()
9318 if (dst_nid == p->numa_preferred_nid) in migrate_degrades_locality()
9319 return -1; in migrate_degrades_locality()
9322 if (env->idle == CPU_IDLE) in migrate_degrades_locality()
9334 return src_weight - dst_weight; in migrate_degrades_locality()
9349 * dst_cfs_rq->nr_queued is greater than 1, if the task
9358 dst_cfs_rq = task_group(p)->cfs_rq[dest_cpu]; in task_is_ineligible_on_dst_cpu()
9360 dst_cfs_rq = &cpu_rq(dest_cpu)->cfs; in task_is_ineligible_on_dst_cpu()
9362 if (sched_feat(PLACE_LAG) && dst_cfs_rq->nr_queued && in task_is_ineligible_on_dst_cpu()
9363 !entity_eligible(task_cfs_rq(p), &p->se)) in task_is_ineligible_on_dst_cpu()
9370 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
9377 lockdep_assert_rq_held(env->src_rq); in can_migrate_task()
9378 if (p->sched_task_hot) in can_migrate_task()
9379 p->sched_task_hot = 0; in can_migrate_task()
9387 * 5) are cache-hot on their current CPU, or in can_migrate_task()
9390 if ((p->se.sched_delayed) && (env->migration_type != migrate_load)) in can_migrate_task()
9393 if (lb_throttled_hierarchy(p, env->dst_cpu)) in can_migrate_task()
9398 * For ineligible tasks we soft-limit them and only allow in can_migrate_task()
9399 * them to migrate when nr_balance_failed is non-zero to in can_migrate_task()
9400 * avoid load-balancing trying very hard to balance the load. in can_migrate_task()
9402 if (!env->sd->nr_balance_failed && in can_migrate_task()
9403 task_is_ineligible_on_dst_cpu(p, env->dst_cpu)) in can_migrate_task()
9413 if (!cpumask_test_cpu(env->dst_cpu, p->cpus_ptr)) { in can_migrate_task()
9416 schedstat_inc(p->stats.nr_failed_migrations_affine); in can_migrate_task()
9418 env->flags |= LBF_SOME_PINNED; in can_migrate_task()
9426 * - for NEWLY_IDLE in can_migrate_task()
9427 * - if we have already computed one in current iteration in can_migrate_task()
9428 * - if it's an active balance in can_migrate_task()
9430 if (env->idle == CPU_NEWLY_IDLE || in can_migrate_task()
9431 env->flags & (LBF_DST_PINNED | LBF_ACTIVE_LB)) in can_migrate_task()
9434 /* Prevent to re-select dst_cpu via env's CPUs: */ in can_migrate_task()
9435 cpu = cpumask_first_and_and(env->dst_grpmask, env->cpus, p->cpus_ptr); in can_migrate_task()
9438 env->flags |= LBF_DST_PINNED; in can_migrate_task()
9439 env->new_dst_cpu = cpu; in can_migrate_task()
9446 env->flags &= ~LBF_ALL_PINNED; in can_migrate_task()
9448 if (task_on_cpu(env->src_rq, p) || in can_migrate_task()
9449 task_current_donor(env->src_rq, p)) { in can_migrate_task()
9450 schedstat_inc(p->stats.nr_failed_migrations_running); in can_migrate_task()
9461 if (env->flags & LBF_ACTIVE_LB) in can_migrate_task()
9470 if (!hot || env->sd->nr_balance_failed > env->sd->cache_nice_tries) { in can_migrate_task()
9472 p->sched_task_hot = 1; in can_migrate_task()
9476 schedstat_inc(p->stats.nr_failed_migrations_hot); in can_migrate_task()
9481 * detach_task() -- detach the task for the migration specified in env
9485 lockdep_assert_rq_held(env->src_rq); in detach_task()
9487 if (p->sched_task_hot) { in detach_task()
9488 p->sched_task_hot = 0; in detach_task()
9489 schedstat_inc(env->sd->lb_hot_gained[env->idle]); in detach_task()
9490 schedstat_inc(p->stats.nr_forced_migrations); in detach_task()
9493 WARN_ON(task_current(env->src_rq, p)); in detach_task()
9494 WARN_ON(task_current_donor(env->src_rq, p)); in detach_task()
9496 deactivate_task(env->src_rq, p, DEQUEUE_NOCLOCK); in detach_task()
9497 set_task_cpu(p, env->dst_cpu); in detach_task()
9501 * detach_one_task() -- tries to dequeue exactly one task from env->src_rq, as
9510 lockdep_assert_rq_held(env->src_rq); in detach_one_task()
9513 &env->src_rq->cfs_tasks, se.group_node) { in detach_one_task()
9521 * lb_gained[env->idle] is updated (other is detach_tasks) in detach_one_task()
9525 schedstat_inc(env->sd->lb_gained[env->idle]); in detach_one_task()
9532 * detach_tasks() -- tries to detach up to imbalance load/util/tasks from
9539 struct list_head *tasks = &env->src_rq->cfs_tasks; in detach_tasks()
9544 lockdep_assert_rq_held(env->src_rq); in detach_tasks()
9550 if (env->src_rq->nr_running <= 1) { in detach_tasks()
9551 env->flags &= ~LBF_ALL_PINNED; in detach_tasks()
9555 if (env->imbalance <= 0) in detach_tasks()
9563 if (env->idle && env->src_rq->nr_running <= 1) in detach_tasks()
9566 env->loop++; in detach_tasks()
9568 if (env->loop > env->loop_max) in detach_tasks()
9572 if (env->loop > env->loop_break) { in detach_tasks()
9573 env->loop_break += SCHED_NR_MIGRATE_BREAK; in detach_tasks()
9574 env->flags |= LBF_NEED_BREAK; in detach_tasks()
9583 switch (env->migration_type) { in detach_tasks()
9588 * value. Make sure that env->imbalance decreases in detach_tasks()
9595 load < 16 && !env->sd->nr_balance_failed) in detach_tasks()
9604 if (shr_bound(load, env->sd->nr_balance_failed) > env->imbalance) in detach_tasks()
9607 env->imbalance -= load; in detach_tasks()
9613 if (shr_bound(util, env->sd->nr_balance_failed) > env->imbalance) in detach_tasks()
9616 env->imbalance -= util; in detach_tasks()
9620 env->imbalance--; in detach_tasks()
9625 if (task_fits_cpu(p, env->src_cpu)) in detach_tasks()
9628 env->imbalance = 0; in detach_tasks()
9633 list_add(&p->se.group_node, &env->tasks); in detach_tasks()
9643 if (env->idle == CPU_NEWLY_IDLE) in detach_tasks()
9651 if (env->imbalance <= 0) in detach_tasks()
9656 if (p->sched_task_hot) in detach_tasks()
9657 schedstat_inc(p->stats.nr_failed_migrations_hot); in detach_tasks()
9659 list_move(&p->se.group_node, tasks); in detach_tasks()
9667 schedstat_add(env->sd->lb_gained[env->idle], detached); in detach_tasks()
9673 * attach_task() -- attach the task detached by detach_task() to its new rq.
9685 * attach_one_task() -- attaches the task returned from detach_one_task() to
9699 * attach_tasks() -- attaches all tasks detached by detach_tasks() to their
9704 struct list_head *tasks = &env->tasks; in attach_tasks()
9708 rq_lock(env->dst_rq, &rf); in attach_tasks()
9709 update_rq_clock(env->dst_rq); in attach_tasks()
9713 list_del_init(&p->se.group_node); in attach_tasks()
9715 attach_task(env->dst_rq, p); in attach_tasks()
9718 rq_unlock(env->dst_rq, &rf); in attach_tasks()
9724 if (cfs_rq->avg.load_avg) in cfs_rq_has_blocked()
9727 if (cfs_rq->avg.util_avg) in cfs_rq_has_blocked()
9752 WRITE_ONCE(rq->last_blocked_load_update_tick, jiffies); in update_blocked_load_tick()
9758 rq->has_blocked_load = 0; in update_blocked_load_status()
9801 if (cfs_rq->nr_queued == 0) in __update_blocked_fair()
9804 if (cfs_rq == &rq->cfs) in __update_blocked_fair()
9809 se = cfs_rq->tg->se[cpu]; in __update_blocked_fair()
9830 * This needs to be done in a top-down fashion because the load of a child
9836 struct sched_entity *se = cfs_rq->tg->se[cpu_of(rq)]; in update_cfs_rq_h_load()
9840 if (cfs_rq->last_h_load_update == now) in update_cfs_rq_h_load()
9843 WRITE_ONCE(cfs_rq->h_load_next, NULL); in update_cfs_rq_h_load()
9846 WRITE_ONCE(cfs_rq->h_load_next, se); in update_cfs_rq_h_load()
9847 if (cfs_rq->last_h_load_update == now) in update_cfs_rq_h_load()
9852 cfs_rq->h_load = cfs_rq_load_avg(cfs_rq); in update_cfs_rq_h_load()
9853 cfs_rq->last_h_load_update = now; in update_cfs_rq_h_load()
9856 while ((se = READ_ONCE(cfs_rq->h_load_next)) != NULL) { in update_cfs_rq_h_load()
9857 load = cfs_rq->h_load; in update_cfs_rq_h_load()
9858 load = div64_ul(load * se->avg.load_avg, in update_cfs_rq_h_load()
9861 cfs_rq->h_load = load; in update_cfs_rq_h_load()
9862 cfs_rq->last_h_load_update = now; in update_cfs_rq_h_load()
9871 return div64_ul(p->se.avg.load_avg * cfs_rq->h_load, in task_h_load()
9877 struct cfs_rq *cfs_rq = &rq->cfs; in __update_blocked_fair()
9889 return p->se.avg.load_avg; in task_h_load()
9915 * sg_lb_stats - stats of a sched_group required for load-balancing:
9938 * sd_lb_stats - stats of a sched_domain required for load-balancing:
9995 free = max - used; in scale_rt_capacity()
10003 struct sched_group *sdg = sd->groups; in update_cpu_capacity()
10008 cpu_rq(cpu)->cpu_capacity = capacity; in update_cpu_capacity()
10011 sdg->sgc->capacity = capacity; in update_cpu_capacity()
10012 sdg->sgc->min_capacity = capacity; in update_cpu_capacity()
10013 sdg->sgc->max_capacity = capacity; in update_cpu_capacity()
10018 struct sched_domain *child = sd->child; in update_group_capacity()
10019 struct sched_group *group, *sdg = sd->groups; in update_group_capacity()
10023 interval = msecs_to_jiffies(sd->balance_interval); in update_group_capacity()
10025 sdg->sgc->next_update = jiffies + interval; in update_group_capacity()
10036 if (child->flags & SD_NUMA) { in update_group_capacity()
10039 * span the current group. in update_group_capacity()
10052 * span the current group. in update_group_capacity()
10055 group = child->groups; in update_group_capacity()
10057 struct sched_group_capacity *sgc = group->sgc; in update_group_capacity()
10059 capacity += sgc->capacity; in update_group_capacity()
10060 min_capacity = min(sgc->min_capacity, min_capacity); in update_group_capacity()
10061 max_capacity = max(sgc->max_capacity, max_capacity); in update_group_capacity()
10062 group = group->next; in update_group_capacity()
10063 } while (group != child->groups); in update_group_capacity()
10066 sdg->sgc->capacity = capacity; in update_group_capacity()
10067 sdg->sgc->min_capacity = min_capacity; in update_group_capacity()
10068 sdg->sgc->max_capacity = max_capacity; in update_group_capacity()
10079 return ((rq->cpu_capacity * sd->imbalance_pct) < in check_cpu_capacity()
10086 return rq->misfit_task_load; in check_misfit_status()
10091 * groups is inadequate due to ->cpus_ptr constraints.
10100 * If we were to balance group-wise we'd place two tasks in the first group and
10104 * The current solution to this issue is detecting the skew in the first group
10120 return group->sgc->imbalance; in sg_imbalanced()
10138 if (sgs->sum_nr_running < sgs->group_weight) in group_has_capacity()
10141 if ((sgs->group_capacity * imbalance_pct) < in group_has_capacity()
10142 (sgs->group_runnable * 100)) in group_has_capacity()
10145 if ((sgs->group_capacity * 100) > in group_has_capacity()
10146 (sgs->group_util * imbalance_pct)) in group_has_capacity()
10163 if (sgs->sum_nr_running <= sgs->group_weight) in group_is_overloaded()
10166 if ((sgs->group_capacity * 100) < in group_is_overloaded()
10167 (sgs->group_util * imbalance_pct)) in group_is_overloaded()
10170 if ((sgs->group_capacity * imbalance_pct) < in group_is_overloaded()
10171 (sgs->group_runnable * 100)) in group_is_overloaded()
10188 if (sgs->group_asym_packing) in group_classify()
10191 if (sgs->group_smt_balance) in group_classify()
10194 if (sgs->group_misfit_task_load) in group_classify()
10204 * sched_use_asym_prio - Check whether asym_packing priority must be used
10216 if (!(sd->flags & SD_ASYM_PACKING)) in sched_use_asym_prio()
10222 return sd->flags & SD_SHARE_CPUCAPACITY || is_core_idle(cpu); in sched_use_asym_prio()
10236 * sched_group_asym - Check if the destination CPU can do asym_packing balance
10238 * @sgs: Load-balancing statistics of the candidate busiest group
10254 if ((group->flags & SD_SHARE_CPUCAPACITY) && in sched_group_asym()
10255 (sgs->group_weight - sgs->idle_cpus != 1)) in sched_group_asym()
10258 return sched_asym(env->sd, env->dst_cpu, READ_ONCE(group->asym_prefer_cpu)); in sched_group_asym()
10268 return (sg1->flags & SD_SHARE_CPUCAPACITY) != in smt_vs_nonsmt_groups()
10269 (sg2->flags & SD_SHARE_CPUCAPACITY); in smt_vs_nonsmt_groups()
10275 if (!env->idle) in smt_balance()
10284 if (group->flags & SD_SHARE_CPUCAPACITY && in smt_balance()
10285 sgs->sum_h_nr_running > 1) in smt_balance()
10299 if (!env->idle || !busiest->sum_nr_running) in sibling_imbalance()
10302 ncores_busiest = sds->busiest->cores; in sibling_imbalance()
10303 ncores_local = sds->local->cores; in sibling_imbalance()
10306 imbalance = busiest->sum_nr_running; in sibling_imbalance()
10307 lsub_positive(&imbalance, local->sum_nr_running); in sibling_imbalance()
10312 imbalance = ncores_local * busiest->sum_nr_running; in sibling_imbalance()
10313 lsub_positive(&imbalance, ncores_busiest * local->sum_nr_running); in sibling_imbalance()
10319 if (imbalance <= 1 && local->sum_nr_running == 0 && in sibling_imbalance()
10320 busiest->sum_nr_running > 1) in sibling_imbalance()
10333 if (rq->cfs.h_nr_runnable != 1) in sched_reduced_capacity()
10340 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
10342 * @sds: Load-balancing data with statistics of the local group.
10355 int i, nr_running, local_group, sd_flags = env->sd->flags; in update_sg_lb_stats()
10356 bool balancing_at_rd = !env->sd->parent; in update_sg_lb_stats()
10360 local_group = group == sds->local; in update_sg_lb_stats()
10362 for_each_cpu_and(i, sched_group_span(group), env->cpus) { in update_sg_lb_stats()
10366 sgs->group_load += load; in update_sg_lb_stats()
10367 sgs->group_util += cpu_util_cfs(i); in update_sg_lb_stats()
10368 sgs->group_runnable += cpu_runnable(rq); in update_sg_lb_stats()
10369 sgs->sum_h_nr_running += rq->cfs.h_nr_runnable; in update_sg_lb_stats()
10371 nr_running = rq->nr_running; in update_sg_lb_stats()
10372 sgs->sum_nr_running += nr_running; in update_sg_lb_stats()
10381 sgs->idle_cpus++; in update_sg_lb_stats()
10393 sgs->nr_numa_running += rq->nr_numa_running; in update_sg_lb_stats()
10394 sgs->nr_preferred_running += rq->nr_preferred_running; in update_sg_lb_stats()
10402 if (sgs->group_misfit_task_load < rq->misfit_task_load) { in update_sg_lb_stats()
10403 sgs->group_misfit_task_load = rq->misfit_task_load; in update_sg_lb_stats()
10406 } else if (env->idle && sched_reduced_capacity(rq, env->sd)) { in update_sg_lb_stats()
10408 if (sgs->group_misfit_task_load < load) in update_sg_lb_stats()
10409 sgs->group_misfit_task_load = load; in update_sg_lb_stats()
10413 sgs->group_capacity = group->sgc->capacity; in update_sg_lb_stats()
10415 sgs->group_weight = group->group_weight; in update_sg_lb_stats()
10418 if (!local_group && env->idle && sgs->sum_h_nr_running && in update_sg_lb_stats()
10420 sgs->group_asym_packing = 1; in update_sg_lb_stats()
10424 sgs->group_smt_balance = 1; in update_sg_lb_stats()
10426 sgs->group_type = group_classify(env->sd->imbalance_pct, group, sgs); in update_sg_lb_stats()
10429 if (sgs->group_type == group_overloaded) in update_sg_lb_stats()
10430 sgs->avg_load = (sgs->group_load * SCHED_CAPACITY_SCALE) / in update_sg_lb_stats()
10431 sgs->group_capacity; in update_sg_lb_stats()
10435 * update_sd_pick_busiest - return 1 on busiest group
10452 struct sg_lb_stats *busiest = &sds->busiest_stat; in update_sd_pick_busiest()
10455 if (!sgs->sum_h_nr_running) in update_sd_pick_busiest()
10464 if ((env->sd->flags & SD_ASYM_CPUCAPACITY) && in update_sd_pick_busiest()
10465 (sgs->group_type == group_misfit_task) && in update_sd_pick_busiest()
10466 (!capacity_greater(capacity_of(env->dst_cpu), sg->sgc->max_capacity) || in update_sd_pick_busiest()
10467 sds->local_stat.group_type != group_has_spare)) in update_sd_pick_busiest()
10470 if (sgs->group_type > busiest->group_type) in update_sd_pick_busiest()
10473 if (sgs->group_type < busiest->group_type) in update_sd_pick_busiest()
10477 * The candidate and the current busiest group are the same type of in update_sd_pick_busiest()
10481 switch (sgs->group_type) { in update_sd_pick_busiest()
10484 return sgs->avg_load > busiest->avg_load; in update_sd_pick_busiest()
10495 return sched_asym_prefer(READ_ONCE(sds->busiest->asym_prefer_cpu), in update_sd_pick_busiest()
10496 READ_ONCE(sg->asym_prefer_cpu)); in update_sd_pick_busiest()
10503 return sgs->group_misfit_task_load > busiest->group_misfit_task_load; in update_sd_pick_busiest()
10510 if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) in update_sd_pick_busiest()
10528 if (sgs->avg_load < busiest->avg_load) in update_sd_pick_busiest()
10531 if (sgs->avg_load == busiest->avg_load) { in update_sd_pick_busiest()
10533 * SMT sched groups need more help than non-SMT groups. in update_sd_pick_busiest()
10536 if (sds->busiest->flags & SD_SHARE_CPUCAPACITY) in update_sd_pick_busiest()
10548 if (smt_vs_nonsmt_groups(sds->busiest, sg)) { in update_sd_pick_busiest()
10549 if (sg->flags & SD_SHARE_CPUCAPACITY && sgs->sum_h_nr_running <= 1) in update_sd_pick_busiest()
10563 if (sgs->idle_cpus > busiest->idle_cpus) in update_sd_pick_busiest()
10565 else if ((sgs->idle_cpus == busiest->idle_cpus) && in update_sd_pick_busiest()
10566 (sgs->sum_nr_running <= busiest->sum_nr_running)) in update_sd_pick_busiest()
10574 * per-CPU capacity. Migrating tasks to less capable CPUs may harm in update_sd_pick_busiest()
10578 if ((env->sd->flags & SD_ASYM_CPUCAPACITY) && in update_sd_pick_busiest()
10579 (sgs->group_type <= group_fully_busy) && in update_sd_pick_busiest()
10580 (capacity_greater(sg->sgc->min_capacity, capacity_of(env->dst_cpu)))) in update_sd_pick_busiest()
10589 if (sgs->sum_h_nr_running > sgs->nr_numa_running) in fbq_classify_group()
10591 if (sgs->sum_h_nr_running > sgs->nr_preferred_running) in fbq_classify_group()
10598 if (rq->nr_running > rq->nr_numa_running) in fbq_classify_rq()
10600 if (rq->nr_running > rq->nr_preferred_running) in fbq_classify_rq()
10620 * task_running_on_cpu - return 1 if @p is running on @cpu.
10626 if (cpu != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in task_running_on_cpu()
10636 * idle_cpu_without - would a given CPU be idle without p ?
10646 if (rq->curr != rq->idle && rq->curr != p) in idle_cpu_without()
10650 * rq->nr_running can't be used but an updated version without the in idle_cpu_without()
10655 if (rq->ttwu_pending) in idle_cpu_without()
10662 * update_sg_wakeup_stats - Update sched_group's statistics for wakeup.
10678 if (sd->flags & SD_ASYM_CPUCAPACITY) in update_sg_wakeup_stats()
10679 sgs->group_misfit_task_load = 1; in update_sg_wakeup_stats()
10685 sgs->group_load += cpu_load_without(rq, p); in update_sg_wakeup_stats()
10686 sgs->group_util += cpu_util_without(i, p); in update_sg_wakeup_stats()
10687 sgs->group_runnable += cpu_runnable_without(rq, p); in update_sg_wakeup_stats()
10689 sgs->sum_h_nr_running += rq->cfs.h_nr_runnable - local; in update_sg_wakeup_stats()
10691 nr_running = rq->nr_running - local; in update_sg_wakeup_stats()
10692 sgs->sum_nr_running += nr_running; in update_sg_wakeup_stats()
10698 sgs->idle_cpus++; in update_sg_wakeup_stats()
10701 if (sd->flags & SD_ASYM_CPUCAPACITY && in update_sg_wakeup_stats()
10702 sgs->group_misfit_task_load && in update_sg_wakeup_stats()
10704 sgs->group_misfit_task_load = 0; in update_sg_wakeup_stats()
10708 sgs->group_capacity = group->sgc->capacity; in update_sg_wakeup_stats()
10710 sgs->group_weight = group->group_weight; in update_sg_wakeup_stats()
10712 sgs->group_type = group_classify(sd->imbalance_pct, group, sgs); in update_sg_wakeup_stats()
10718 if (sgs->group_type == group_fully_busy || in update_sg_wakeup_stats()
10719 sgs->group_type == group_overloaded) in update_sg_wakeup_stats()
10720 sgs->avg_load = (sgs->group_load * SCHED_CAPACITY_SCALE) / in update_sg_wakeup_stats()
10721 sgs->group_capacity; in update_sg_wakeup_stats()
10729 if (sgs->group_type < idlest_sgs->group_type) in update_pick_idlest()
10732 if (sgs->group_type > idlest_sgs->group_type) in update_pick_idlest()
10736 * The candidate and the current idlest group are the same type of in update_pick_idlest()
10740 switch (sgs->group_type) { in update_pick_idlest()
10744 if (idlest_sgs->avg_load <= sgs->avg_load) in update_pick_idlest()
10756 if (idlest->sgc->max_capacity >= group->sgc->max_capacity) in update_pick_idlest()
10762 if (idlest_sgs->idle_cpus > sgs->idle_cpus) in update_pick_idlest()
10766 if (idlest_sgs->idle_cpus == sgs->idle_cpus && in update_pick_idlest()
10767 idlest_sgs->group_util <= sgs->group_util) in update_pick_idlest()
10785 struct sched_group *idlest = NULL, *local = NULL, *group = sd->groups; in sched_balance_find_dst_group()
10799 p->cpus_ptr)) in sched_balance_find_dst_group()
10823 } while (group = group->next, group != sd->groups); in sched_balance_find_dst_group()
10854 (sd->imbalance_pct-100) / 100; in sched_balance_find_dst_group()
10861 * cross-domain, add imbalance to the load on the remote node in sched_balance_find_dst_group()
10865 if ((sd->flags & SD_NUMA) && in sched_balance_find_dst_group()
10876 if (100 * local_sgs.avg_load <= sd->imbalance_pct * idlest_sgs.avg_load) in sched_balance_find_dst_group()
10888 if (local->sgc->max_capacity >= idlest->sgc->max_capacity) in sched_balance_find_dst_group()
10894 if (sd->flags & SD_NUMA) { in sched_balance_find_dst_group()
10895 int imb_numa_nr = sd->imb_numa_nr; in sched_balance_find_dst_group()
10902 if (cpu_to_node(this_cpu) == p->numa_preferred_nid) in sched_balance_find_dst_group()
10906 if (cpu_to_node(idlest_cpu) == p->numa_preferred_nid) in sched_balance_find_dst_group()
10918 if (p->nr_cpus_allowed != NR_CPUS) { in sched_balance_find_dst_group()
10921 cpumask_and(cpus, sched_group_span(local), p->cpus_ptr); in sched_balance_find_dst_group()
10922 imb_numa_nr = min(cpumask_weight(cpus), sd->imb_numa_nr); in sched_balance_find_dst_group()
10925 imbalance = abs(local_sgs.idle_cpus - idlest_sgs.idle_cpus); in sched_balance_find_dst_group()
10962 if (!sched_feat(SIS_UTIL) || env->idle == CPU_NEWLY_IDLE) in update_idle_cpu_scan()
10965 llc_weight = per_cpu(sd_llc_size, env->dst_cpu); in update_idle_cpu_scan()
10966 if (env->sd->span_weight != llc_weight) in update_idle_cpu_scan()
10969 sd_share = rcu_dereference(per_cpu(sd_llc_shared, env->dst_cpu)); in update_idle_cpu_scan()
10979 * let y = SCHED_CAPACITY_SCALE - p * x^2 [1] in update_idle_cpu_scan()
10997 * y = SCHED_CAPACITY_SCALE - in update_idle_cpu_scan()
11006 pct = env->sd->imbalance_pct; in update_idle_cpu_scan()
11010 y = SCHED_CAPACITY_SCALE - tmp; in update_idle_cpu_scan()
11015 if ((int)y != sd_share->nr_idle_scan) in update_idle_cpu_scan()
11016 WRITE_ONCE(sd_share->nr_idle_scan, (int)y); in update_idle_cpu_scan()
11020 * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
11027 struct sched_group *sg = env->sd->groups; in update_sd_lb_stats()
11028 struct sg_lb_stats *local = &sds->local_stat; in update_sd_lb_stats()
11037 local_group = cpumask_test_cpu(env->dst_cpu, sched_group_span(sg)); in update_sd_lb_stats()
11039 sds->local = sg; in update_sd_lb_stats()
11042 if (env->idle != CPU_NEWLY_IDLE || in update_sd_lb_stats()
11043 time_after_eq(jiffies, sg->sgc->next_update)) in update_sd_lb_stats()
11044 update_group_capacity(env->sd, env->dst_cpu); in update_sd_lb_stats()
11050 sds->busiest = sg; in update_sd_lb_stats()
11051 sds->busiest_stat = *sgs; in update_sd_lb_stats()
11055 sds->total_load += sgs->group_load; in update_sd_lb_stats()
11056 sds->total_capacity += sgs->group_capacity; in update_sd_lb_stats()
11058 sum_util += sgs->group_util; in update_sd_lb_stats()
11059 sg = sg->next; in update_sd_lb_stats()
11060 } while (sg != env->sd->groups); in update_sd_lb_stats()
11067 if (sds->busiest) in update_sd_lb_stats()
11068 sds->prefer_sibling = !!(sds->busiest->flags & SD_PREFER_SIBLING); in update_sd_lb_stats()
11071 if (env->sd->flags & SD_NUMA) in update_sd_lb_stats()
11072 env->fbq_type = fbq_classify_group(&sds->busiest_stat); in update_sd_lb_stats()
11074 if (!env->sd->parent) { in update_sd_lb_stats()
11076 set_rd_overloaded(env->dst_rq->rd, sg_overloaded); in update_sd_lb_stats()
11078 /* Update over-utilization (tipping point, U >= 0) indicator */ in update_sd_lb_stats()
11079 set_rd_overutilized(env->dst_rq->rd, sg_overutilized); in update_sd_lb_stats()
11081 set_rd_overutilized(env->dst_rq->rd, sg_overutilized); in update_sd_lb_stats()
11088 * calculate_imbalance - Calculate the amount of imbalance present within the
11097 local = &sds->local_stat; in calculate_imbalance()
11098 busiest = &sds->busiest_stat; in calculate_imbalance()
11100 if (busiest->group_type == group_misfit_task) { in calculate_imbalance()
11101 if (env->sd->flags & SD_ASYM_CPUCAPACITY) { in calculate_imbalance()
11103 env->migration_type = migrate_misfit; in calculate_imbalance()
11104 env->imbalance = 1; in calculate_imbalance()
11110 env->migration_type = migrate_load; in calculate_imbalance()
11111 env->imbalance = busiest->group_misfit_task_load; in calculate_imbalance()
11116 if (busiest->group_type == group_asym_packing) { in calculate_imbalance()
11121 env->migration_type = migrate_task; in calculate_imbalance()
11122 env->imbalance = busiest->sum_h_nr_running; in calculate_imbalance()
11126 if (busiest->group_type == group_smt_balance) { in calculate_imbalance()
11128 env->migration_type = migrate_task; in calculate_imbalance()
11129 env->imbalance = 1; in calculate_imbalance()
11133 if (busiest->group_type == group_imbalanced) { in calculate_imbalance()
11135 * In the group_imb case we cannot rely on group-wide averages in calculate_imbalance()
11136 * to ensure CPU-load equilibrium, try to move any task to fix in calculate_imbalance()
11140 env->migration_type = migrate_task; in calculate_imbalance()
11141 env->imbalance = 1; in calculate_imbalance()
11149 if (local->group_type == group_has_spare) { in calculate_imbalance()
11150 if ((busiest->group_type > group_fully_busy) && in calculate_imbalance()
11151 !(env->sd->flags & SD_SHARE_LLC)) { in calculate_imbalance()
11160 env->migration_type = migrate_util; in calculate_imbalance()
11161 env->imbalance = max(local->group_capacity, local->group_util) - in calculate_imbalance()
11162 local->group_util; in calculate_imbalance()
11171 if (env->idle && env->imbalance == 0) { in calculate_imbalance()
11172 env->migration_type = migrate_task; in calculate_imbalance()
11173 env->imbalance = 1; in calculate_imbalance()
11179 if (busiest->group_weight == 1 || sds->prefer_sibling) { in calculate_imbalance()
11184 env->migration_type = migrate_task; in calculate_imbalance()
11185 env->imbalance = sibling_imbalance(env, sds, busiest, local); in calculate_imbalance()
11192 env->migration_type = migrate_task; in calculate_imbalance()
11193 env->imbalance = max_t(long, 0, in calculate_imbalance()
11194 (local->idle_cpus - busiest->idle_cpus)); in calculate_imbalance()
11199 if (env->sd->flags & SD_NUMA) { in calculate_imbalance()
11200 env->imbalance = adjust_numa_imbalance(env->imbalance, in calculate_imbalance()
11201 local->sum_nr_running + 1, in calculate_imbalance()
11202 env->sd->imb_numa_nr); in calculate_imbalance()
11207 env->imbalance >>= 1; in calculate_imbalance()
11216 if (local->group_type < group_overloaded) { in calculate_imbalance()
11222 local->avg_load = (local->group_load * SCHED_CAPACITY_SCALE) / in calculate_imbalance()
11223 local->group_capacity; in calculate_imbalance()
11229 if (local->avg_load >= busiest->avg_load) { in calculate_imbalance()
11230 env->imbalance = 0; in calculate_imbalance()
11234 sds->avg_load = (sds->total_load * SCHED_CAPACITY_SCALE) / in calculate_imbalance()
11235 sds->total_capacity; in calculate_imbalance()
11241 if (local->avg_load >= sds->avg_load) { in calculate_imbalance()
11242 env->imbalance = 0; in calculate_imbalance()
11256 env->migration_type = migrate_load; in calculate_imbalance()
11257 env->imbalance = min( in calculate_imbalance()
11258 (busiest->avg_load - sds->avg_load) * busiest->group_capacity, in calculate_imbalance()
11259 (sds->avg_load - local->avg_load) * local->group_capacity in calculate_imbalance()
11286 * sched_balance_find_src_group - Returns the busiest group within the sched_domain
11293 * Return: - The busiest group if imbalance exists.
11315 if (busiest->group_type == group_misfit_task) in sched_balance_find_src_group()
11318 if (!is_rd_overutilized(env->dst_rq->rd) && in sched_balance_find_src_group()
11319 rcu_dereference(env->dst_rq->rd->pd)) in sched_balance_find_src_group()
11323 if (busiest->group_type == group_asym_packing) in sched_balance_find_src_group()
11331 if (busiest->group_type == group_imbalanced) in sched_balance_find_src_group()
11339 if (local->group_type > busiest->group_type) in sched_balance_find_src_group()
11346 if (local->group_type == group_overloaded) { in sched_balance_find_src_group()
11351 if (local->avg_load >= busiest->avg_load) in sched_balance_find_src_group()
11362 if (local->avg_load >= sds.avg_load) in sched_balance_find_src_group()
11369 if (100 * busiest->avg_load <= in sched_balance_find_src_group()
11370 env->sd->imbalance_pct * local->avg_load) in sched_balance_find_src_group()
11378 if (sds.prefer_sibling && local->group_type == group_has_spare && in sched_balance_find_src_group()
11382 if (busiest->group_type != group_overloaded) { in sched_balance_find_src_group()
11383 if (!env->idle) { in sched_balance_find_src_group()
11392 if (busiest->group_type == group_smt_balance && in sched_balance_find_src_group()
11398 if (busiest->group_weight > 1 && in sched_balance_find_src_group()
11399 local->idle_cpus <= (busiest->idle_cpus + 1)) { in sched_balance_find_src_group()
11412 if (busiest->sum_h_nr_running == 1) { in sched_balance_find_src_group()
11423 return env->imbalance ? sds.busiest : NULL; in sched_balance_find_src_group()
11426 env->imbalance = 0; in sched_balance_find_src_group()
11431 * sched_balance_find_src_rq - find the busiest runqueue among the CPUs in the group.
11441 for_each_cpu_and(i, sched_group_span(group), env->cpus) { in sched_balance_find_src_rq()
11451 * - regular: there are !numa tasks in sched_balance_find_src_rq()
11452 * - remote: there are numa tasks that run on the 'wrong' node in sched_balance_find_src_rq()
11453 * - all: there is no distinction in sched_balance_find_src_rq()
11468 if (rt > env->fbq_type) in sched_balance_find_src_rq()
11471 nr_running = rq->cfs.h_nr_runnable; in sched_balance_find_src_rq()
11479 * eventually lead to active_balancing high->low capacity. in sched_balance_find_src_rq()
11480 * Higher per-CPU capacity is considered better than balancing in sched_balance_find_src_rq()
11483 if (env->sd->flags & SD_ASYM_CPUCAPACITY && in sched_balance_find_src_rq()
11484 !capacity_greater(capacity_of(env->dst_cpu), capacity) && in sched_balance_find_src_rq()
11495 if (sched_asym(env->sd, i, env->dst_cpu) && nr_running == 1) in sched_balance_find_src_rq()
11498 switch (env->migration_type) { in sched_balance_find_src_rq()
11506 if (nr_running == 1 && load > env->imbalance && in sched_balance_find_src_rq()
11507 !check_cpu_capacity(rq, env->sd)) in sched_balance_find_src_rq()
11559 if (rq->misfit_task_load > busiest_load) { in sched_balance_find_src_rq()
11560 busiest_load = rq->misfit_task_load; in sched_balance_find_src_rq()
11591 return env->idle && sched_use_asym_prio(env->sd, env->dst_cpu) && in asym_active_balance()
11592 (sched_asym_prefer(env->dst_cpu, env->src_cpu) || in asym_active_balance()
11593 !sched_use_asym_prio(env->sd, env->src_cpu)); in asym_active_balance()
11599 struct sched_domain *sd = env->sd; in imbalanced_active_balance()
11606 if ((env->migration_type == migrate_task) && in imbalanced_active_balance()
11607 (sd->nr_balance_failed > sd->cache_nice_tries+2)) in imbalanced_active_balance()
11615 struct sched_domain *sd = env->sd; in need_active_balance()
11629 if (env->idle && in need_active_balance()
11630 (env->src_rq->cfs.h_nr_runnable == 1)) { in need_active_balance()
11631 if ((check_cpu_capacity(env->src_rq, sd)) && in need_active_balance()
11632 (capacity_of(env->src_cpu)*sd->imbalance_pct < capacity_of(env->dst_cpu)*100)) in need_active_balance()
11636 if (env->migration_type == migrate_misfit) in need_active_balance()
11647 struct sched_group *sg = env->sd->groups; in should_we_balance()
11648 int cpu, idle_smt = -1; in should_we_balance()
11654 if (!cpumask_test_cpu(env->dst_cpu, env->cpus)) in should_we_balance()
11664 if (env->idle == CPU_NEWLY_IDLE) { in should_we_balance()
11665 if (env->dst_rq->nr_running > 0 || env->dst_rq->ttwu_pending) in should_we_balance()
11672 for_each_cpu_and(cpu, swb_cpus, env->cpus) { in should_we_balance()
11681 if (!(env->sd->flags & SD_SHARE_CPUCAPACITY) && !is_core_idle(cpu)) { in should_we_balance()
11682 if (idle_smt == -1) in should_we_balance()
11696 * Are we the first idle core in a non-SMT domain or higher, in should_we_balance()
11699 return cpu == env->dst_cpu; in should_we_balance()
11703 if (idle_smt != -1) in should_we_balance()
11704 return idle_smt == env->dst_cpu; in should_we_balance()
11707 return group_balance_cpu(sg) == env->dst_cpu; in should_we_balance()
11716 switch (env->migration_type) { in update_lb_imbalance_stat()
11718 __schedstat_add(sd->lb_imbalance_load[idle], env->imbalance); in update_lb_imbalance_stat()
11721 __schedstat_add(sd->lb_imbalance_util[idle], env->imbalance); in update_lb_imbalance_stat()
11724 __schedstat_add(sd->lb_imbalance_task[idle], env->imbalance); in update_lb_imbalance_stat()
11727 __schedstat_add(sd->lb_imbalance_misfit[idle], env->imbalance); in update_lb_imbalance_stat()
11741 struct sched_domain *sd_parent = sd->parent; in sched_balance_rq()
11750 .dst_grpmask = group_balance_mask(sd->groups), in sched_balance_rq()
11760 schedstat_inc(sd->lb_count[idle]); in sched_balance_rq()
11770 schedstat_inc(sd->lb_nobusyg[idle]); in sched_balance_rq()
11776 schedstat_inc(sd->lb_nobusyq[idle]); in sched_balance_rq()
11784 env.src_cpu = busiest->cpu; in sched_balance_rq()
11790 if (busiest->nr_running > 1) { in sched_balance_rq()
11793 * an imbalance but busiest->nr_running <= 1, the group is in sched_balance_rq()
11797 env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running); in sched_balance_rq()
11804 * cur_ld_moved - load moved in current iteration in sched_balance_rq()
11805 * ld_moved - cumulative load moved across iterations in sched_balance_rq()
11812 * unlock busiest->lock, and we are able to be sure in sched_balance_rq()
11841 * nohz-idle), we now have balance_cpu in a position to move in sched_balance_rq()
11852 /* Prevent to re-select dst_cpu via env's CPUs */ in sched_balance_rq()
11872 int *group_imbalance = &sd_parent->groups->sgc->imbalance; in sched_balance_rq()
11882 * Attempting to continue load balancing at the current in sched_balance_rq()
11899 schedstat_inc(sd->lb_failed[idle]); in sched_balance_rq()
11911 sd->nr_balance_failed++; in sched_balance_rq()
11923 if (!cpumask_test_cpu(this_cpu, busiest->curr->cpus_ptr)) { in sched_balance_rq()
11932 * ->active_balance synchronizes accesses to in sched_balance_rq()
11933 * ->active_balance_work. Once set, it's cleared in sched_balance_rq()
11936 if (!busiest->active_balance) { in sched_balance_rq()
11937 busiest->active_balance = 1; in sched_balance_rq()
11938 busiest->push_cpu = this_cpu; in sched_balance_rq()
11947 &busiest->active_balance_work); in sched_balance_rq()
11952 sd->nr_balance_failed = 0; in sched_balance_rq()
11957 sd->balance_interval = sd->min_interval; in sched_balance_rq()
11969 int *group_imbalance = &sd_parent->groups->sgc->imbalance; in sched_balance_rq()
11981 schedstat_inc(sd->lb_balanced[idle]); in sched_balance_rq()
11983 sd->nr_balance_failed = 0; in sched_balance_rq()
12004 sd->balance_interval < MAX_PINNED_INTERVAL) || in sched_balance_rq()
12005 sd->balance_interval < sd->max_interval) in sched_balance_rq()
12006 sd->balance_interval *= 2; in sched_balance_rq()
12014 unsigned long interval = sd->balance_interval; in get_sd_balance_interval()
12017 interval *= sd->busy_factor; in get_sd_balance_interval()
12028 interval -= 1; in get_sd_balance_interval()
12042 next = sd->last_balance + interval; in update_next_balance()
12058 int target_cpu = busiest_rq->push_cpu; in active_load_balance_cpu_stop()
12066 * Between queueing the stop-work and running it is a hole in which in active_load_balance_cpu_stop()
12075 !busiest_rq->active_balance)) in active_load_balance_cpu_stop()
12079 if (busiest_rq->nr_running <= 1) in active_load_balance_cpu_stop()
12085 * Bjorn Helgaas on a 128-CPU setup. in active_load_balance_cpu_stop()
12101 .src_cpu = busiest_rq->cpu, in active_load_balance_cpu_stop()
12107 schedstat_inc(sd->alb_count); in active_load_balance_cpu_stop()
12112 schedstat_inc(sd->alb_pushed); in active_load_balance_cpu_stop()
12114 sd->nr_balance_failed = 0; in active_load_balance_cpu_stop()
12116 schedstat_inc(sd->alb_failed); in active_load_balance_cpu_stop()
12121 busiest_rq->active_balance = 0; in active_load_balance_cpu_stop()
12133 * This flag serializes load-balancing passes over large domains
12134 * (above the NODE topology level) - only one load-balancing instance
12138 * - Note that load-balancing passes triggered while another one
12139 * is executing are skipped and not re-tried.
12141 * - Also note that this does not serialize rebalance_domains()
12142 * execution, as non-SD_SERIALIZE domains will still be
12143 * load-balanced in parallel.
12149 * This trades load-balance latency on larger machines for less cross talk.
12158 if (cost > sd->max_newidle_lb_cost) { in update_newidle_cost()
12168 sd->max_newidle_lb_cost = in update_newidle_cost()
12170 sd->last_decay_max_lb_cost = jiffies; in update_newidle_cost()
12171 } else if (time_after(jiffies, sd->last_decay_max_lb_cost + HZ)) { in update_newidle_cost()
12174 * it is not outdated and the current max cost is actually in update_newidle_cost()
12177 sd->max_newidle_lb_cost = (sd->max_newidle_lb_cost * 253) / 256; in update_newidle_cost()
12178 sd->last_decay_max_lb_cost = jiffies; in update_newidle_cost()
12195 int cpu = rq->cpu; in sched_balance_domains()
12212 max_cost += sd->max_newidle_lb_cost; in sched_balance_domains()
12227 need_serialize = sd->flags & SD_SERIALIZE; in sched_balance_domains()
12233 if (time_after_eq(jiffies, sd->last_balance + interval)) { in sched_balance_domains()
12237 * env->dst_cpu, so we can't know our idle in sched_balance_domains()
12243 sd->last_balance = jiffies; in sched_balance_domains()
12249 if (time_after(next_balance, sd->last_balance + interval)) { in sched_balance_domains()
12250 next_balance = sd->last_balance + interval; in sched_balance_domains()
12256 * Ensure the rq-wide value also decays but keep it at a in sched_balance_domains()
12257 * reasonable floor to avoid funnies with rq->avg_idle. in sched_balance_domains()
12259 rq->max_idle_balance_cost = in sched_balance_domains()
12270 rq->next_balance = next_balance; in sched_balance_domains()
12276 return unlikely(!rcu_dereference_sched(rq->sd)); in on_null_domain()
12283 * - When one of the busy CPUs notices that there may be an idle rebalancing
12303 return -1; in find_new_ilb()
12307 * Kick a CPU to do the NOHZ balancing, if it is time for it, via a cross-CPU
12348 smp_call_function_single_async(ilb_cpu, &cpu_rq(ilb_cpu)->nohz_csd); in kick_ilb()
12352 * Current decision point for kicking the idle load balancer in the presence
12360 int nr_busy, i, cpu = rq->cpu; in nohz_balancer_kick()
12363 if (unlikely(rq->idle_balance)) in nohz_balancer_kick()
12386 if (rq->nr_running >= 2) { in nohz_balancer_kick()
12393 sd = rcu_dereference(rq->sd); in nohz_balancer_kick()
12396 * If there's a runnable CFS task and the current CPU has reduced in nohz_balancer_kick()
12399 if (rq->cfs.h_nr_runnable >= 1 && check_cpu_capacity(rq, sd)) { in nohz_balancer_kick()
12448 * increase the overall cache utilization), we need a less-loaded LLC in nohz_balancer_kick()
12450 * load within the current LLC domain (e.g. packed SMT cores but in nohz_balancer_kick()
12452 * the others are - so just get a NOHZ balance going if it looks in nohz_balancer_kick()
12455 nr_busy = atomic_read(&sds->nr_busy_cpus); in nohz_balancer_kick()
12478 if (!sd || !sd->nohz_idle) in set_cpu_sd_state_busy()
12480 sd->nohz_idle = 0; in set_cpu_sd_state_busy()
12482 atomic_inc(&sd->shared->nr_busy_cpus); in set_cpu_sd_state_busy()
12491 if (likely(!rq->nohz_tick_stopped)) in nohz_balance_exit_idle()
12494 rq->nohz_tick_stopped = 0; in nohz_balance_exit_idle()
12495 cpumask_clear_cpu(rq->cpu, nohz.idle_cpus_mask); in nohz_balance_exit_idle()
12498 set_cpu_sd_state_busy(rq->cpu); in nohz_balance_exit_idle()
12508 if (!sd || sd->nohz_idle) in set_cpu_sd_state_idle()
12510 sd->nohz_idle = 1; in set_cpu_sd_state_idle()
12512 atomic_dec(&sd->shared->nr_busy_cpus); in set_cpu_sd_state_idle()
12532 * Can be set safely without rq->lock held in nohz_balance_enter_idle()
12534 * rq->lock is held during the check and the clear in nohz_balance_enter_idle()
12536 rq->has_blocked_load = 1; in nohz_balance_enter_idle()
12544 if (rq->nohz_tick_stopped) in nohz_balance_enter_idle()
12551 rq->nohz_tick_stopped = 1; in nohz_balance_enter_idle()
12576 unsigned int cpu = rq->cpu; in update_nohz_stats()
12578 if (!rq->has_blocked_load) in update_nohz_stats()
12584 if (!time_after(jiffies, READ_ONCE(rq->last_blocked_load_update_tick))) in update_nohz_stats()
12589 return rq->has_blocked_load; in update_nohz_stats()
12604 int this_cpu = this_rq->cpu; in _nohz_idle_balance()
12661 if (time_after_eq(jiffies, rq->next_balance)) { in _nohz_idle_balance()
12672 if (time_after(next_balance, rq->next_balance)) { in _nohz_idle_balance()
12673 next_balance = rq->next_balance; in _nohz_idle_balance()
12702 unsigned int flags = this_rq->nohz_idle_balance; in nohz_idle_balance()
12707 this_rq->nohz_idle_balance = 0; in nohz_idle_balance()
12748 int this_cpu = this_rq->cpu; in nohz_newidle_balance()
12751 if (this_rq->avg_idle < sysctl_sched_migration_cost) in nohz_newidle_balance()
12782 * < 0 - we released the lock and there are !fair tasks present
12783 * 0 - failed, no new tasks
12784 * > 0 - success, new (fair) tasks present
12789 int this_cpu = this_rq->cpu; in sched_balance_newidle()
12801 if (this_rq->ttwu_pending) in sched_balance_newidle()
12809 this_rq->idle_stamp = rq_clock(this_rq); in sched_balance_newidle()
12818 * This is OK, because current is on_cpu, which avoids it being picked in sched_balance_newidle()
12819 * for load-balance and preemption/IRQs are still disabled avoiding in sched_balance_newidle()
12821 * re-start the picking loop. in sched_balance_newidle()
12826 sd = rcu_dereference_check_sched_domain(this_rq->sd); in sched_balance_newidle()
12828 if (!get_rd_overloaded(this_rq->rd) || in sched_balance_newidle()
12829 (sd && this_rq->avg_idle < sd->max_newidle_lb_cost)) { in sched_balance_newidle()
12850 if (this_rq->avg_idle < curr_cost + sd->max_newidle_lb_cost) in sched_balance_newidle()
12853 if (sd->flags & SD_BALANCE_NEWIDLE) { in sched_balance_newidle()
12860 domain_cost = t1 - t0; in sched_balance_newidle()
12869 domain_cost = (3 * sd->max_newidle_lb_cost) / 2; in sched_balance_newidle()
12885 if (curr_cost > this_rq->max_idle_balance_cost) in sched_balance_newidle()
12886 this_rq->max_idle_balance_cost = curr_cost; in sched_balance_newidle()
12893 if (this_rq->cfs.h_nr_queued && !pulled_task) in sched_balance_newidle()
12897 if (this_rq->nr_running != this_rq->cfs.h_nr_queued) in sched_balance_newidle()
12898 pulled_task = -1; in sched_balance_newidle()
12902 if (time_after(this_rq->next_balance, next_balance)) in sched_balance_newidle()
12903 this_rq->next_balance = next_balance; in sched_balance_newidle()
12906 this_rq->idle_stamp = 0; in sched_balance_newidle()
12918 * - directly from the local sched_tick() for periodic load balancing
12920 * - indirectly from a remote sched_tick() for NOHZ idle balancing
12921 * through the SMP cross-call nohz_csd_func()
12926 enum cpu_idle_type idle = this_rq->idle_balance; in sched_balance_softirq()
12939 sched_balance_update_blocked_averages(this_rq->cpu); in sched_balance_softirq()
12955 if (time_after_eq(jiffies, rq->next_balance)) in sched_balance_trigger()
12983 u64 rtime = se->sum_exec_runtime - se->prev_sum_exec_runtime; in __entity_slice_used()
12984 u64 slice = se->slice; in __entity_slice_used()
13006 * MIN_NR_TASKS_DURING_FORCEIDLE - 1 tasks and use that to check in task_tick_core()
13009 if (rq->core->core_forceidle_count && rq->cfs.nr_queued == 1 && in task_tick_core()
13010 __entity_slice_used(&curr->se, MIN_NR_TASKS_DURING_FORCEIDLE)) in task_tick_core()
13015 * se_fi_update - Update the cfs_rq->min_vruntime_fi in a CFS hierarchy if needed.
13024 if (cfs_rq->forceidle_seq == fi_seq) in se_fi_update()
13026 cfs_rq->forceidle_seq = fi_seq; in se_fi_update()
13029 cfs_rq->min_vruntime_fi = cfs_rq->min_vruntime; in se_fi_update()
13035 struct sched_entity *se = &p->se; in task_vruntime_update()
13037 if (p->sched_class != &fair_sched_class) in task_vruntime_update()
13040 se_fi_update(se, rq->core->core_forceidle_seq, in_fi); in task_vruntime_update()
13047 const struct sched_entity *sea = &a->se; in cfs_prio_less()
13048 const struct sched_entity *seb = &b->se; in cfs_prio_less()
13053 WARN_ON_ONCE(task_rq(b)->core != rq->core); in cfs_prio_less()
13060 while (sea->cfs_rq->tg != seb->cfs_rq->tg) { in cfs_prio_less()
13061 int sea_depth = sea->depth; in cfs_prio_less()
13062 int seb_depth = seb->depth; in cfs_prio_less()
13070 se_fi_update(sea, rq->core->core_forceidle_seq, in_fi); in cfs_prio_less()
13071 se_fi_update(seb, rq->core->core_forceidle_seq, in_fi); in cfs_prio_less()
13073 cfs_rqa = sea->cfs_rq; in cfs_prio_less()
13074 cfs_rqb = seb->cfs_rq; in cfs_prio_less()
13076 cfs_rqa = &task_rq(a)->cfs; in cfs_prio_less()
13077 cfs_rqb = &task_rq(b)->cfs; in cfs_prio_less()
13085 delta = (s64)(sea->vruntime - seb->vruntime) + in cfs_prio_less()
13086 (s64)(cfs_rqb->min_vruntime_fi - cfs_rqa->min_vruntime_fi); in cfs_prio_less()
13096 cfs_rq = task_group(p)->cfs_rq[cpu]; in task_is_throttled_fair()
13098 cfs_rq = &cpu_rq(cpu)->cfs; in task_is_throttled_fair()
13117 struct sched_entity *se = &curr->se; in task_tick_fair()
13135 * - child not yet on the tasklist
13136 * - preemption disabled
13145 * the current task.
13153 if (rq->cfs.nr_queued == 1) in prio_changed_fair()
13159 * this runqueue and our priority is higher than the current's in prio_changed_fair()
13162 if (p->prio > oldprio) in prio_changed_fair()
13187 se = se->parent; in propagate_entity_cfs_rq()
13210 * - A forked task which hasn't been woken up by wake_up_new_task(). in detach_entity_cfs_rq()
13211 * - A task which has been woken up by try_to_wake_up() but is in detach_entity_cfs_rq()
13214 if (!se->avg.last_update_time) in detach_entity_cfs_rq()
13237 struct sched_entity *se = &p->se; in detach_task_cfs_rq()
13244 struct sched_entity *se = &p->se; in attach_task_cfs_rq()
13256 WARN_ON_ONCE(p->se.sched_delayed); in switched_to_fair()
13266 * if we can still preempt the current task. in switched_to_fair()
13277 struct sched_entity *se = &p->se; in __set_next_task_fair()
13284 list_move(&se->group_node, &rq->cfs_tasks); in __set_next_task_fair()
13289 WARN_ON_ONCE(se->sched_delayed); in __set_next_task_fair()
13301 * This routine is mostly called to set cfs_rq->curr field when a task
13306 struct sched_entity *se = &p->se; in set_next_task_fair()
13321 cfs_rq->tasks_timeline = RB_ROOT_CACHED; in init_cfs_rq()
13322 cfs_rq->min_vruntime = (u64)(-(1LL << 20)); in init_cfs_rq()
13323 raw_spin_lock_init(&cfs_rq->removed.lock); in init_cfs_rq()
13333 if (READ_ONCE(p->__state) == TASK_NEW) in task_change_group_fair()
13338 /* Tell se's cfs_rq has been changed -- migrated */ in task_change_group_fair()
13339 p->se.avg.last_update_time = 0; in task_change_group_fair()
13349 if (tg->cfs_rq) in free_fair_sched_group()
13350 kfree(tg->cfs_rq[i]); in free_fair_sched_group()
13351 if (tg->se) in free_fair_sched_group()
13352 kfree(tg->se[i]); in free_fair_sched_group()
13355 kfree(tg->cfs_rq); in free_fair_sched_group()
13356 kfree(tg->se); in free_fair_sched_group()
13365 tg->cfs_rq = kcalloc(nr_cpu_ids, sizeof(cfs_rq), GFP_KERNEL); in alloc_fair_sched_group()
13366 if (!tg->cfs_rq) in alloc_fair_sched_group()
13368 tg->se = kcalloc(nr_cpu_ids, sizeof(se), GFP_KERNEL); in alloc_fair_sched_group()
13369 if (!tg->se) in alloc_fair_sched_group()
13372 tg->shares = NICE_0_LOAD; in alloc_fair_sched_group()
13388 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]); in alloc_fair_sched_group()
13409 se = tg->se[i]; in online_fair_sched_group()
13425 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu]; in unregister_fair_sched_group()
13426 struct sched_entity *se = tg->se[cpu]; in unregister_fair_sched_group()
13430 if (se->sched_delayed) { in unregister_fair_sched_group()
13432 if (se->sched_delayed) { in unregister_fair_sched_group()
13443 * check on_list without danger of it being re-added. in unregister_fair_sched_group()
13445 if (cfs_rq->on_list) { in unregister_fair_sched_group()
13458 cfs_rq->tg = tg; in init_tg_cfs_entry()
13459 cfs_rq->rq = rq; in init_tg_cfs_entry()
13462 tg->cfs_rq[cpu] = cfs_rq; in init_tg_cfs_entry()
13463 tg->se[cpu] = se; in init_tg_cfs_entry()
13470 se->cfs_rq = &rq->cfs; in init_tg_cfs_entry()
13471 se->depth = 0; in init_tg_cfs_entry()
13473 se->cfs_rq = parent->my_q; in init_tg_cfs_entry()
13474 se->depth = parent->depth + 1; in init_tg_cfs_entry()
13477 se->my_q = cfs_rq; in init_tg_cfs_entry()
13479 update_load_set(&se->load, NICE_0_LOAD); in init_tg_cfs_entry()
13480 se->parent = parent; in init_tg_cfs_entry()
13494 if (!tg->se[0]) in __sched_group_set_shares()
13495 return -EINVAL; in __sched_group_set_shares()
13499 if (tg->shares == shares) in __sched_group_set_shares()
13502 tg->shares = shares; in __sched_group_set_shares()
13505 struct sched_entity *se = tg->se[i]; in __sched_group_set_shares()
13527 ret = -EINVAL; in sched_group_set_shares()
13540 return -EINVAL; in sched_group_set_idle()
13543 return -EINVAL; in sched_group_set_idle()
13547 if (tg->idle == idle) { in sched_group_set_idle()
13552 tg->idle = idle; in sched_group_set_idle()
13556 struct sched_entity *se = tg->se[i]; in sched_group_set_idle()
13557 struct cfs_rq *grp_cfs_rq = tg->cfs_rq[i]; in sched_group_set_idle()
13564 grp_cfs_rq->idle = idle; in sched_group_set_idle()
13568 idle_task_delta = grp_cfs_rq->h_nr_queued - in sched_group_set_idle()
13569 grp_cfs_rq->h_nr_idle; in sched_group_set_idle()
13571 idle_task_delta *= -1; in sched_group_set_idle()
13576 if (!se->on_rq) in sched_group_set_idle()
13579 cfs_rq->h_nr_idle += idle_task_delta; in sched_group_set_idle()
13605 struct sched_entity *se = &task->se; in get_rr_interval_fair()
13612 if (rq->cfs.load.weight) in get_rr_interval_fair()
13613 rr_interval = NS_TO_JIFFIES(se->slice); in get_rr_interval_fair()
13688 ng = rcu_dereference(p->numa_group); in show_numa_stats()
13690 if (p->numa_faults) { in show_numa_stats()
13691 tsf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 0)]; in show_numa_stats()
13692 tpf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 1)]; in show_numa_stats()
13695 gsf = ng->faults[task_faults_idx(NUMA_MEM, node, 0)], in show_numa_stats()
13696 gpf = ng->faults[task_faults_idx(NUMA_MEM, node, 1)]; in show_numa_stats()
13715 INIT_CSD(&cpu_rq(i)->cfsb_csd, __cfsb_csd_unthrottle, cpu_rq(i)); in init_sched_fair_class()
13716 INIT_LIST_HEAD(&cpu_rq(i)->cfsb_csd_list); in init_sched_fair_class()