Lines Matching +full:hw +full:- +full:settle +full:- +full:time +full:- +full:us
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
117 * each time a cfs_rq requests quota.
121 * we will always only issue the remaining available time.
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()
322 * enqueued) or force our parent to appear after us when it is 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()
365 * make sure that it will be put after us. 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)
574 * Where S is the ideal service time and V is it's virtual time counterpart.
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()
777 /* ensure we never gain time by being placed backwards. */ 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()
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()
1047 * For EEVDF the virtual time slope is determined by w_i (iow. in update_deadline()
1048 * nice) while the request time r_i is determined by 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()
1071 /* Give new sched_entity start runnable values to heavy its load in infant time */
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()
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()
1169 * If se is a task, we account the time against the running 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()
1178 /* cgroup time is always accounted against the donor */ in update_se()
1181 /* If not task, account the time against donor se */ 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()
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()
1234 * fair_server time whether or not the task is running on in update_curr()
1236 * - If the task is running on behalf of fair_server, we need in update_curr()
1237 * to limit its time based on the assigned runtime. in update_curr()
1238 * - Fair task that runs outside of fair_server should account in update_curr()
1239 * against fair_server such that it can account for this time 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:
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()
1433 * Approximate time to scan a full NUMA task in ms. The task scan period is
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()
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
1671 * between these nodes are slowed down, to allow things to settle down.
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()
1793 * used to record scan time instead of CPU and PID. When tiering mode
1794 * is disabled at run time, the scan time (in cpupid) will be
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()
1833 * time will be recorded in struct page in addition to make page
1838 * hint page fault latency = hint page fault time - scan time
1845 int last_time, time; in numa_hint_fault_latency() local
1847 time = jiffies_to_msecs(jiffies); in numa_hint_fault_latency()
1848 last_time = folio_xchg_access_time(folio, time); in numa_hint_fault_latency()
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()
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()
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()
2585 * settle down. 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()
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()
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()
2759 * Get the fraction of time the task has been running since the last
2762 * from the dozens-of-seconds NUMA balancing period. Use the scheduler
2768 /* Use the start of this time slice to avoid calculations. */ in numa_get_avg_runtime()
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()
2776 /* Avoid time going backwards, prevent potential divide error: */ 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()
3051 * the other task will join us. 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()
3112 * reset the data back to default state without freeing ->numa_faults.
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()
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()
3214 * previously failed, or the scheduler moved us. 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()
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()
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()
3348 * the next time around. 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()
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()
3503 * Only force scan within one VMA at a time, to limit the in task_numa_work()
3528 mm->numa_scan_offset = start; in task_numa_work()
3534 * Make sure tasks use at least 32x as much time to run other code 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()
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()
3776 /* commit outstanding execution time */ 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()
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()
4171 * time. This will result in the wakee task is less decayed, but giving 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
4225 * to (shortly) return to us. This only works by keeping the weights as
4228 * Another reason this doesn't work is that runnable isn't a 0-sum entity.
4229 * Imagine a rq with 2 tasks that each are runnable 2/3 of the time. Then the
4232 * align the rq as a whole would be runnable 2/3 of the time. If however we
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()
4440 * waste of time to try to decay it: 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()
4480 * time. Hence, limiting to the case where the source CPU is idle and 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()
4879 ewma = READ_ONCE(p->se.avg.util_est); in util_est_update()
4882 * If the PELT values haven't changed since enqueue time, in util_est_update()
4904 last_ewma_diff = ewma - dequeued; in util_est_update()
4910 * we cannot grant that thread got all CPU time it wanted. 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()
4979 * Only exception is for HW or cpufreq pressure since it has a direct impact in util_fits_cpu()
5004 * +---------------------------------------- in util_fits_cpu()
5042 * +---------------------------------------- in util_fits_cpu()
5065 return -1; in util_fits_cpu()
5090 * Affinity allows us to go somewhere higher? Or are we on biggest in update_misfit_status()
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()
5137 * will move 'time' backwards, this can screw around with the lag of 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()
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()
5264 * XXX update_load_avg() above will have attached us to the pelt sum; 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()
5466 if (se->on_rq) { in set_next_entity()
5469 * a CPU. So account for the time it spent waiting on the 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()
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()
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()
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()
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()
5966 * stop happens at dequeue time when all entities are dequeued. 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()
5990 * actually throttled the timer might not unthrottle us for an in throttle_cfs_rq()
5993 * us, as we may commit to do cfs put_prev+pick_next, so we ask 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()
6012 * Note: distribution will already see us throttled via the 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()
6029 * unthrottled us with a positive runtime_remaining but other still in unthrottle_cfs_rq()
6036 if (cfs_rq->runtime_enabled && cfs_rq->runtime_remaining <= 0) in unthrottle_cfs_rq()
6039 se = cfs_rq->tg->se[cpu_of(rq)]; in unthrottle_cfs_rq()
6041 cfs_rq->throttled = 0; in unthrottle_cfs_rq()
6045 raw_spin_lock(&cfs_b->lock); in unthrottle_cfs_rq()
6046 if (cfs_rq->throttled_clock) { in unthrottle_cfs_rq()
6047 cfs_b->throttled_time += rq_clock(rq) - cfs_rq->throttled_clock; in unthrottle_cfs_rq()
6048 cfs_rq->throttled_clock = 0; in unthrottle_cfs_rq()
6050 list_del_rcu(&cfs_rq->throttled_list); in unthrottle_cfs_rq()
6051 raw_spin_unlock(&cfs_b->lock); in unthrottle_cfs_rq()
6054 walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq); in unthrottle_cfs_rq()
6056 if (!cfs_rq->load.weight) { in unthrottle_cfs_rq()
6057 if (!cfs_rq->on_list) in unthrottle_cfs_rq()
6072 if (rq->curr == rq->idle && rq->cfs.nr_queued) in unthrottle_cfs_rq()
6101 list_for_each_entry_safe(cursor, tmp, &rq->cfsb_csd_list, in __cfsb_csd_unthrottle()
6103 list_del_init(&cursor->throttled_csd_list); in __cfsb_csd_unthrottle()
6126 if (WARN_ON_ONCE(!list_empty(&cfs_rq->throttled_csd_list))) in __unthrottle_cfs_rq_async()
6129 first = list_empty(&rq->cfsb_csd_list); in __unthrottle_cfs_rq_async()
6130 list_add_tail(&cfs_rq->throttled_csd_list, &rq->cfsb_csd_list); in __unthrottle_cfs_rq_async()
6132 smp_call_function_single_async(cpu_of(rq), &rq->cfsb_csd); in __unthrottle_cfs_rq_async()
6140 cfs_rq->runtime_remaining <= 0)) in unthrottle_cfs_rq_async()
6157 list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq, in distribute_cfs_runtime()
6171 if (!list_empty(&cfs_rq->throttled_csd_list)) in distribute_cfs_runtime()
6175 WARN_ON_ONCE(cfs_rq->runtime_remaining > 0); in distribute_cfs_runtime()
6177 raw_spin_lock(&cfs_b->lock); in distribute_cfs_runtime()
6178 runtime = -cfs_rq->runtime_remaining + 1; in distribute_cfs_runtime()
6179 if (runtime > cfs_b->runtime) in distribute_cfs_runtime()
6180 runtime = cfs_b->runtime; in distribute_cfs_runtime()
6181 cfs_b->runtime -= runtime; in distribute_cfs_runtime()
6182 remaining = cfs_b->runtime; in distribute_cfs_runtime()
6183 raw_spin_unlock(&cfs_b->lock); in distribute_cfs_runtime()
6185 cfs_rq->runtime_remaining += runtime; in distribute_cfs_runtime()
6188 if (cfs_rq->runtime_remaining > 0) { in distribute_cfs_runtime()
6197 list_add_tail(&cfs_rq->throttled_csd_list, in distribute_cfs_runtime()
6214 list_del_init(&cfs_rq->throttled_csd_list); in distribute_cfs_runtime()
6231 * period the timer is deactivated until scheduling resumes; cfs_b->idle is
6239 if (cfs_b->quota == RUNTIME_INF) in do_sched_cfs_period_timer()
6242 throttled = !list_empty(&cfs_b->throttled_cfs_rq); in do_sched_cfs_period_timer()
6243 cfs_b->nr_periods += overrun; in do_sched_cfs_period_timer()
6245 /* Refill extra burst quota even if cfs_b->idle */ in do_sched_cfs_period_timer()
6252 if (cfs_b->idle && !throttled) in do_sched_cfs_period_timer()
6257 cfs_b->idle = 1; in do_sched_cfs_period_timer()
6262 cfs_b->nr_throttled += overrun; in do_sched_cfs_period_timer()
6265 * This check is repeated as we release cfs_b->lock while we unthrottle. in do_sched_cfs_period_timer()
6267 while (throttled && cfs_b->runtime > 0) { in do_sched_cfs_period_timer()
6268 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in do_sched_cfs_period_timer()
6269 /* we can't nest cfs_b->lock while distributing bandwidth */ in do_sched_cfs_period_timer()
6271 raw_spin_lock_irqsave(&cfs_b->lock, flags); in do_sched_cfs_period_timer()
6280 cfs_b->idle = 0; in do_sched_cfs_period_timer()
6290 /* minimum remaining period time to redistribute slack quota */
6298 * Requires cfs_b->lock for hrtimer_expires_remaining to be safe against the
6304 struct hrtimer *refresh_timer = &cfs_b->period_timer; in runtime_refresh_within()
6307 /* if the call-back is running a quota refresh is already occurring */ in runtime_refresh_within()
6328 if (cfs_b->slack_started) in start_cfs_slack_bandwidth()
6330 cfs_b->slack_started = true; in start_cfs_slack_bandwidth()
6332 hrtimer_start(&cfs_b->slack_timer, in start_cfs_slack_bandwidth()
6340 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in __return_cfs_rq_runtime()
6341 s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime; in __return_cfs_rq_runtime()
6346 raw_spin_lock(&cfs_b->lock); in __return_cfs_rq_runtime()
6347 if (cfs_b->quota != RUNTIME_INF) { in __return_cfs_rq_runtime()
6348 cfs_b->runtime += slack_runtime; in __return_cfs_rq_runtime()
6350 /* we are under rq->lock, defer unthrottling using a timer */ in __return_cfs_rq_runtime()
6351 if (cfs_b->runtime > sched_cfs_bandwidth_slice() && in __return_cfs_rq_runtime()
6352 !list_empty(&cfs_b->throttled_cfs_rq)) in __return_cfs_rq_runtime()
6355 raw_spin_unlock(&cfs_b->lock); in __return_cfs_rq_runtime()
6358 cfs_rq->runtime_remaining -= slack_runtime; in __return_cfs_rq_runtime()
6366 if (!cfs_rq->runtime_enabled || cfs_rq->nr_queued) in return_cfs_rq_runtime()
6374 * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
6382 raw_spin_lock_irqsave(&cfs_b->lock, flags); in do_sched_cfs_slack_timer()
6383 cfs_b->slack_started = false; in do_sched_cfs_slack_timer()
6386 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in do_sched_cfs_slack_timer()
6390 if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) in do_sched_cfs_slack_timer()
6391 runtime = cfs_b->runtime; in do_sched_cfs_slack_timer()
6393 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in do_sched_cfs_slack_timer()
6404 * runtime as update_curr() throttling can not trigger until it's on-rq.
6411 /* an active group must be handled by the update_curr()->put() path */ in check_enqueue_throttle()
6412 if (!cfs_rq->runtime_enabled || cfs_rq->curr) in check_enqueue_throttle()
6421 if (cfs_rq->runtime_remaining <= 0) in check_enqueue_throttle()
6432 if (!tg->parent) in sync_throttle()
6435 cfs_rq = tg->cfs_rq[cpu]; in sync_throttle()
6436 pcfs_rq = tg->parent->cfs_rq[cpu]; in sync_throttle()
6438 cfs_rq->throttle_count = pcfs_rq->throttle_count; in sync_throttle()
6439 cfs_rq->throttled_clock_pelt = rq_clock_pelt(cpu_rq(cpu)); in sync_throttle()
6448 if (cfs_rq->throttle_count) in sync_throttle()
6449 cfs_rq->pelt_clock_throttled = 1; in sync_throttle()
6458 if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0)) in check_cfs_rq_runtime()
6490 raw_spin_lock_irqsave(&cfs_b->lock, flags); in sched_cfs_period_timer()
6492 overrun = hrtimer_forward_now(timer, cfs_b->period); in sched_cfs_period_timer()
6499 u64 new, old = ktime_to_ns(cfs_b->period); in sched_cfs_period_timer()
6508 cfs_b->period = ns_to_ktime(new); in sched_cfs_period_timer()
6509 cfs_b->quota *= 2; in sched_cfs_period_timer()
6510 cfs_b->burst *= 2; in sched_cfs_period_timer()
6516 div_u64(cfs_b->quota, NSEC_PER_USEC)); in sched_cfs_period_timer()
6522 div_u64(cfs_b->quota, NSEC_PER_USEC)); in sched_cfs_period_timer()
6530 cfs_b->period_active = 0; in sched_cfs_period_timer()
6531 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in sched_cfs_period_timer()
6538 raw_spin_lock_init(&cfs_b->lock); in init_cfs_bandwidth()
6539 cfs_b->runtime = 0; in init_cfs_bandwidth()
6540 cfs_b->quota = RUNTIME_INF; in init_cfs_bandwidth()
6541 cfs_b->period = us_to_ktime(default_bw_period_us()); in init_cfs_bandwidth()
6542 cfs_b->burst = 0; in init_cfs_bandwidth()
6543 cfs_b->hierarchical_quota = parent ? parent->hierarchical_quota : RUNTIME_INF; in init_cfs_bandwidth()
6545 INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq); in init_cfs_bandwidth()
6546 hrtimer_setup(&cfs_b->period_timer, sched_cfs_period_timer, CLOCK_MONOTONIC, in init_cfs_bandwidth()
6550 hrtimer_set_expires(&cfs_b->period_timer, in init_cfs_bandwidth()
6551 get_random_u32_below(cfs_b->period)); in init_cfs_bandwidth()
6552 hrtimer_setup(&cfs_b->slack_timer, sched_cfs_slack_timer, CLOCK_MONOTONIC, in init_cfs_bandwidth()
6554 cfs_b->slack_started = false; in init_cfs_bandwidth()
6559 cfs_rq->runtime_enabled = 0; in init_cfs_rq_runtime()
6560 INIT_LIST_HEAD(&cfs_rq->throttled_list); in init_cfs_rq_runtime()
6561 INIT_LIST_HEAD(&cfs_rq->throttled_csd_list); in init_cfs_rq_runtime()
6562 INIT_LIST_HEAD(&cfs_rq->throttled_limbo_list); in init_cfs_rq_runtime()
6567 lockdep_assert_held(&cfs_b->lock); in start_cfs_bandwidth()
6569 if (cfs_b->period_active) in start_cfs_bandwidth()
6572 cfs_b->period_active = 1; in start_cfs_bandwidth()
6573 hrtimer_forward_now(&cfs_b->period_timer, cfs_b->period); in start_cfs_bandwidth()
6574 hrtimer_start_expires(&cfs_b->period_timer, HRTIMER_MODE_ABS_PINNED); in start_cfs_bandwidth()
6582 if (!cfs_b->throttled_cfs_rq.next) in destroy_cfs_bandwidth()
6585 hrtimer_cancel(&cfs_b->period_timer); in destroy_cfs_bandwidth()
6586 hrtimer_cancel(&cfs_b->slack_timer); in destroy_cfs_bandwidth()
6602 if (list_empty(&rq->cfsb_csd_list)) in destroy_cfs_bandwidth()
6627 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth; in update_runtime_enabled()
6628 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in update_runtime_enabled()
6630 raw_spin_lock(&cfs_b->lock); in update_runtime_enabled()
6631 cfs_rq->runtime_enabled = cfs_b->quota != RUNTIME_INF; in update_runtime_enabled()
6632 raw_spin_unlock(&cfs_b->lock); in update_runtime_enabled()
6657 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in unthrottle_offline_cfs_rqs()
6659 if (!cfs_rq->runtime_enabled) in unthrottle_offline_cfs_rqs()
6666 cfs_rq->runtime_enabled = 0; in unthrottle_offline_cfs_rqs()
6675 cfs_rq->runtime_remaining = 1; in unthrottle_offline_cfs_rqs()
6690 if (cfs_rq->runtime_enabled || in cfs_task_bw_constrained()
6691 tg_cfs_bandwidth(cfs_rq->tg)->hierarchical_quota != RUNTIME_INF) in cfs_task_bw_constrained()
6709 if (rq->nr_running != 1) in sched_fair_update_stop_tick()
6787 struct sched_entity *se = &p->se; in hrtick_start_fair()
6791 if (rq->cfs.h_nr_queued > 1) { in hrtick_start_fair()
6792 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime; in hrtick_start_fair()
6793 u64 slice = se->slice; in hrtick_start_fair()
6794 s64 delta = slice - ran; in hrtick_start_fair()
6812 struct task_struct *donor = rq->donor; in hrtick_update()
6814 if (!hrtick_enabled_fair(rq) || donor->sched_class != &fair_sched_class) in hrtick_update()
6849 return !sched_energy_enabled() || READ_ONCE(rd->overutilized); in is_rd_overutilized()
6857 WRITE_ONCE(rd->overutilized, flag); in set_rd_overutilized()
6868 if (!is_rd_overutilized(rq->rd) && cpu_overutilized(rq->cpu)) in check_update_overutilized_status()
6869 set_rd_overutilized(rq->rd, 1); in check_update_overutilized_status()
6875 return unlikely(rq->nr_running == rq->cfs.h_nr_idle && in sched_idle_rq()
6876 rq->nr_running); in sched_idle_rq()
6890 * se->sched_delayed should imply: se->on_rq == 1. in requeue_delayed_entity()
6894 WARN_ON_ONCE(!se->sched_delayed); in requeue_delayed_entity()
6895 WARN_ON_ONCE(!se->on_rq); in requeue_delayed_entity()
6899 if (se->vlag > 0) { in requeue_delayed_entity()
6900 cfs_rq->nr_queued--; in requeue_delayed_entity()
6901 if (se != cfs_rq->curr) in requeue_delayed_entity()
6903 se->vlag = 0; in requeue_delayed_entity()
6905 if (se != cfs_rq->curr) in requeue_delayed_entity()
6907 cfs_rq->nr_queued++; in requeue_delayed_entity()
6924 struct sched_entity *se = &p->se; in enqueue_task_fair()
6928 int rq_h_nr_queued = rq->cfs.h_nr_queued; in enqueue_task_fair()
6940 if (!p->se.sched_delayed || (flags & ENQUEUE_DELAYED)) in enqueue_task_fair()
6941 util_est_enqueue(&rq->cfs, p); in enqueue_task_fair()
6953 if (p->in_iowait) in enqueue_task_fair()
6956 if (task_new && se->sched_delayed) in enqueue_task_fair()
6960 if (se->on_rq) { in enqueue_task_fair()
6961 if (se->sched_delayed) in enqueue_task_fair()
6970 * its entities in the desired time-frame. in enqueue_task_fair()
6973 se->slice = slice; in enqueue_task_fair()
6974 se->custom_slice = 1; in enqueue_task_fair()
6979 cfs_rq->h_nr_runnable += h_nr_runnable; in enqueue_task_fair()
6980 cfs_rq->h_nr_queued++; in enqueue_task_fair()
6981 cfs_rq->h_nr_idle += h_nr_idle; in enqueue_task_fair()
6996 se->slice = slice; in enqueue_task_fair()
6997 if (se != cfs_rq->curr) in enqueue_task_fair()
6998 min_vruntime_cb_propagate(&se->run_node, NULL); in enqueue_task_fair()
7001 cfs_rq->h_nr_runnable += h_nr_runnable; in enqueue_task_fair()
7002 cfs_rq->h_nr_queued++; in enqueue_task_fair()
7003 cfs_rq->h_nr_idle += h_nr_idle; in enqueue_task_fair()
7009 if (!rq_h_nr_queued && rq->cfs.h_nr_queued) { in enqueue_task_fair()
7011 if (!rq->nr_running) in enqueue_task_fair()
7012 dl_server_update_idle_time(rq, rq->curr); in enqueue_task_fair()
7013 dl_server_start(&rq->fair_server); in enqueue_task_fair()
7045 * failing half-way through and resume the dequeue later.
7048 * -1 - dequeue delayed
7049 * 0 - dequeue throttled
7050 * 1 - dequeue complete
7069 if (task_sleep || task_delayed || !se->sched_delayed) in dequeue_entities()
7077 if (p && &p->se == se) in dequeue_entities()
7078 return -1; in dequeue_entities()
7084 cfs_rq->h_nr_runnable -= h_nr_runnable; in dequeue_entities()
7085 cfs_rq->h_nr_queued -= h_nr_queued; in dequeue_entities()
7086 cfs_rq->h_nr_idle -= h_nr_idle; in dequeue_entities()
7094 /* Don't dequeue parent if it has other entities besides us */ in dequeue_entities()
7095 if (cfs_rq->load.weight) { in dequeue_entities()
7098 /* Avoid re-evaluating load for this entity: */ in dequeue_entities()
7119 se->slice = slice; in dequeue_entities()
7120 if (se != cfs_rq->curr) in dequeue_entities()
7121 min_vruntime_cb_propagate(&se->run_node, NULL); in dequeue_entities()
7124 cfs_rq->h_nr_runnable -= h_nr_runnable; in dequeue_entities()
7125 cfs_rq->h_nr_queued -= h_nr_queued; in dequeue_entities()
7126 cfs_rq->h_nr_idle -= h_nr_idle; in dequeue_entities()
7139 rq->next_balance = jiffies; in dequeue_entities()
7143 WARN_ON_ONCE(p->on_rq != 1); in dequeue_entities()
7145 /* Fix-up what dequeue_task_fair() skipped */ in dequeue_entities()
7149 * Fix-up what block_task() skipped. in dequeue_entities()
7171 if (!p->se.sched_delayed) in dequeue_task_fair()
7172 util_est_dequeue(&rq->cfs, p); in dequeue_task_fair()
7174 util_est_update(&rq->cfs, p, flags & DEQUEUE_SLEEP); in dequeue_task_fair()
7175 if (dequeue_entities(rq, &p->se, flags) < 0) in dequeue_task_fair()
7188 return (rq->cfs.h_nr_queued - rq->cfs.h_nr_runnable); in cfs_h_nr_delayed()
7211 return cfs_rq_load_avg(&rq->cfs); in cpu_load()
7215 * cpu_load_without - compute CPU load without any contributions from *p
7233 if (cpu_of(rq) != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in cpu_load_without()
7236 cfs_rq = &rq->cfs; in cpu_load_without()
7237 load = READ_ONCE(cfs_rq->avg.load_avg); in cpu_load_without()
7247 return cfs_rq_runnable_avg(&rq->cfs); in cpu_runnable()
7256 if (cpu_of(rq) != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in cpu_runnable_without()
7259 cfs_rq = &rq->cfs; in cpu_runnable_without()
7260 runnable = READ_ONCE(cfs_rq->avg.runnable_avg); in cpu_runnable_without()
7263 lsub_positive(&runnable, p->se.avg.runnable_avg); in cpu_runnable_without()
7270 return cpu_rq(cpu)->cpu_capacity; in capacity_of()
7276 * Only decay a single time; tasks that have less then 1 wakeup per in record_wakee()
7279 if (time_after(jiffies, current->wakee_flip_decay_ts + HZ)) { in record_wakee()
7280 current->wakee_flips >>= 1; in record_wakee()
7281 current->wakee_flip_decay_ts = jiffies; in record_wakee()
7284 if (current->last_wakee != p) { in record_wakee()
7285 current->last_wakee = p; in record_wakee()
7286 current->wakee_flips++; in record_wakee()
7291 * Detect M:N waker/wakee relationships via a switching-frequency heuristic.
7301 * non-monogamous, with partner count exceeding socket size.
7309 unsigned int master = current->wakee_flips; in wake_wide()
7310 unsigned int slave = p->wakee_flips; in wake_wide()
7325 * wake_affine_idle() - only considers 'now', it check if the waking CPU is
7326 * cache-affine and is (or will be) idle.
7328 * wake_affine_weight() - considers the weight to reflect the average
7353 if ((rq->nr_running - cfs_h_nr_delayed(rq)) == 1) in wake_affine_idle()
7378 this_eff_load -= current_load; in wake_affine_weight()
7389 prev_eff_load -= task_load; in wake_affine_weight()
7391 prev_eff_load *= 100 + (sd->imbalance_pct - 100) / 2; in wake_affine_weight()
7417 schedstat_inc(p->stats.nr_wakeups_affine_attempts); in wake_affine()
7421 schedstat_inc(sd->ttwu_move_affine); in wake_affine()
7422 schedstat_inc(p->stats.nr_wakeups_affine); in wake_affine()
7430 * sched_balance_find_dst_group_cpu - find the idlest CPU among the CPUs in the group.
7439 int shallowest_idle_cpu = -1; in sched_balance_find_dst_group_cpu()
7443 if (group->group_weight == 1) in sched_balance_find_dst_group_cpu()
7447 for_each_cpu_and(i, sched_group_span(group), p->cpus_ptr) { in sched_balance_find_dst_group_cpu()
7458 if (idle && idle->exit_latency < min_exit_latency) { in sched_balance_find_dst_group_cpu()
7464 min_exit_latency = idle->exit_latency; in sched_balance_find_dst_group_cpu()
7465 latest_idle_timestamp = rq->idle_stamp; in sched_balance_find_dst_group_cpu()
7467 } else if ((!idle || idle->exit_latency == min_exit_latency) && in sched_balance_find_dst_group_cpu()
7468 rq->idle_stamp > latest_idle_timestamp) { in sched_balance_find_dst_group_cpu()
7474 latest_idle_timestamp = rq->idle_stamp; in sched_balance_find_dst_group_cpu()
7477 } else if (shallowest_idle_cpu == -1) { in sched_balance_find_dst_group_cpu()
7486 return shallowest_idle_cpu != -1 ? shallowest_idle_cpu : least_loaded_cpu; in sched_balance_find_dst_group_cpu()
7494 if (!cpumask_intersects(sched_domain_span(sd), p->cpus_ptr)) in sched_balance_find_dst_cpu()
7502 sync_entity_load_avg(&p->se); in sched_balance_find_dst_cpu()
7509 if (!(sd->flags & sd_flag)) { in sched_balance_find_dst_cpu()
7510 sd = sd->child; in sched_balance_find_dst_cpu()
7516 sd = sd->child; in sched_balance_find_dst_cpu()
7523 sd = sd->child; in sched_balance_find_dst_cpu()
7529 weight = sd->span_weight; in sched_balance_find_dst_cpu()
7532 if (weight <= tmp->span_weight) in sched_balance_find_dst_cpu()
7534 if (tmp->flags & sd_flag) in sched_balance_find_dst_cpu()
7548 return -1; in __select_idle_cpu()
7561 WRITE_ONCE(sds->has_idle_cores, val); in set_idle_cores()
7570 return READ_ONCE(sds->has_idle_cores); in test_idle_cores()
7577 * information in sd_llc_shared->has_idle_cores.
7607 * sd_llc->shared->has_idle_cores and enabled through update_idle_core() above.
7617 if (*idle_cpu == -1) { in select_idle_core()
7626 if (*idle_cpu == -1 && cpumask_test_cpu(cpu, cpus)) in select_idle_core()
7634 return -1; in select_idle_core()
7644 for_each_cpu_and(cpu, cpu_smt_mask(target), p->cpus_ptr) { in select_idle_smt()
7657 return -1; in select_idle_smt()
7678 return -1; in select_idle_smt()
7685 * comparing the average scan cost (tracked in sd->avg_scan_cost) against the
7686 * average idle time for this rq (as found in rq->avg_idle).
7691 int i, cpu, idle_cpu = -1, nr = INT_MAX; in select_idle_cpu()
7694 cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr); in select_idle_cpu()
7699 /* because !--nr is the condition to stop scan */ in select_idle_cpu()
7700 nr = READ_ONCE(sd_share->nr_idle_scan) + 1; in select_idle_cpu()
7703 return -1; in select_idle_cpu()
7708 struct sched_group *sg = sd->groups; in select_idle_cpu()
7710 if (sg->flags & SD_CLUSTER) { in select_idle_cpu()
7720 if (--nr <= 0) in select_idle_cpu()
7721 return -1; in select_idle_cpu()
7738 if (--nr <= 0) in select_idle_cpu()
7739 return -1; in select_idle_cpu()
7762 int cpu, best_cpu = -1; in select_idle_capacity()
7766 cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr); in select_idle_capacity()
7791 * First, select CPU which fits better (-1 being better than 0). in select_idle_capacity()
7828 int i, recent_used_cpu, prev_aff = -1; in select_idle_sibling()
7835 sync_entity_load_avg(&p->se); in select_idle_sibling()
7842 * per-cpu select_rq_mask usage in select_idle_sibling()
7865 * Allow a per-cpu kthread to stack with the wakee if the in select_idle_sibling()
7868 * per-cpu kthread that is now complete and the wakeup is in select_idle_sibling()
7875 this_rq()->nr_running <= 1 && in select_idle_sibling()
7881 recent_used_cpu = p->recent_used_cpu; in select_idle_sibling()
7882 p->recent_used_cpu = prev; in select_idle_sibling()
7887 cpumask_test_cpu(recent_used_cpu, p->cpus_ptr) && in select_idle_sibling()
7895 recent_used_cpu = -1; in select_idle_sibling()
7951 * cpu_util() - Estimates the amount of CPU capacity used by CFS tasks.
7954 * @dst_cpu: CPU @p migrates to, -1 if @p moves from @cpu or @p == NULL
7960 * CPU utilization is the sum of running time of runnable tasks plus the
7961 * recent utilization of currently non-runnable tasks on that CPU.
7969 * previously-executed tasks, which helps better deduce how busy a CPU will
7970 * be when a long-sleeping task wakes up. The contribution to CPU utilization
7971 * of such a task would be significantly decayed at this point of time.
7984 * could be seen as over-utilized even though CPU1 has 20% of spare CPU
7987 * after task migrations (scheduler-driven DVFS).
7994 struct cfs_rq *cfs_rq = &cpu_rq(cpu)->cfs; in cpu_util()
7995 unsigned long util = READ_ONCE(cfs_rq->avg.util_avg); in cpu_util()
7999 runnable = READ_ONCE(cfs_rq->avg.runnable_avg); in cpu_util()
8004 * If @dst_cpu is -1 or @p migrates from @cpu to @dst_cpu remove its in cpu_util()
8017 util_est = READ_ONCE(cfs_rq->avg.util_est); in cpu_util()
8020 * During wake-up @p isn't enqueued yet and doesn't contribute in cpu_util()
8021 * to any cpu_rq(cpu)->cfs.avg.util_est. in cpu_util()
8025 * During exec (@dst_cpu = -1) @p is enqueued and does in cpu_util()
8026 * contribute to cpu_rq(cpu)->cfs.util_est. in cpu_util()
8035 * p->on_rq = TASK_ON_RQ_MIGRATING; in cpu_util()
8036 * -------------------------------- A in cpu_util()
8038 * dequeue_task_fair() + Race Time in cpu_util()
8040 * -------------------------------- B in cpu_util()
8058 return cpu_util(cpu, NULL, -1, 0); in cpu_util_cfs()
8063 return cpu_util(cpu, NULL, -1, 1); in cpu_util_cfs_boost()
8082 if (cpu != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in cpu_util_without()
8085 return cpu_util(cpu, p, -1, 0); in cpu_util_without()
8100 * The cfs,rt,dl utilization are the running times measured with rq->clock_task
8101 * which excludes things like IRQ and steal-time. These latter are then accrued
8118 * Early check to see if IRQ/steal time saturates the CPU, can be in effective_cpu_util()
8119 * because of inaccuracies in how we track these -- see in effective_cpu_util()
8134 * - the computed DL bandwidth needed with the IRQ pressure which in effective_cpu_util()
8135 * steals time to the deadline task. in effective_cpu_util()
8136 * - The minimum performance requirement for CFS and/or RT. in effective_cpu_util()
8144 if (!uclamp_is_used() && rt_rq_is_runnable(&rq->rt)) in effective_cpu_util()
8149 * Because the time spend on RT/DL tasks is visible as 'lost' time to in effective_cpu_util()
8168 * There is still idle time; further improve the number by using the in effective_cpu_util()
8169 * IRQ metric. Because IRQ/steal time is hidden from the task clock we in effective_cpu_util()
8172 * max - irq in effective_cpu_util()
8173 * U' = irq + --------- * U in effective_cpu_util()
8188 * energy_env - Utilization landscape for energy estimation.
8194 * @pd_cap: Entire perf domain capacity. (pd->nr_cpus * cpu_cap).
8204 * Compute the task busy time for compute_energy(). This time cannot be
8220 eenv->task_busy_time = busy_time; in eenv_task_busy_time()
8224 * Compute the perf_domain (PD) busy time for compute_energy(). Based on the
8234 * - A stable PD utilization, no matter which CPU of that PD we want to place
8237 * - A fair comparison between CPUs as the task contribution (task_util())
8241 * Set @eenv busy time for the PD that spans @pd_cpus. This busy time can't
8242 * exceed @eenv->pd_cap.
8252 unsigned long util = cpu_util(cpu, p, -1, 0); in eenv_pd_busy_time()
8257 eenv->pd_busy_time = min(eenv->pd_cap, busy_time); in eenv_pd_busy_time()
8264 * Returns the maximum utilization among @eenv->cpus. This utilization can't
8265 * exceed @eenv->cpu_cap.
8306 return min(max_util, eenv->cpu_cap); in eenv_pd_max_util()
8319 unsigned long busy_time = eenv->pd_busy_time; in compute_energy()
8323 busy_time = min(eenv->pd_cap, busy_time + eenv->task_busy_time); in compute_energy()
8325 energy = em_cpu_energy(pd->em_pd, max_util, busy_time, eenv->cpu_cap); in compute_energy()
8333 * find_energy_efficient_cpu(): Find most energy-efficient target CPU for the
8337 * out which of the CPU candidates is the most energy-efficient.
8354 * cluster-packing, and spreading inside a cluster. That should at least be
8361 * NOTE: Forkees are not accepted in the energy-aware wake-up path because
8365 * to be energy-inefficient in some use-cases. The alternative would be to
8368 * other use-cases too. So, until someone finds a better way to solve this,
8369 * let's keep things simple by re-using the existing slow path.
8377 struct root_domain *rd = this_rq()->rd; in find_energy_efficient_cpu()
8378 int cpu, best_energy_cpu, target = -1; in find_energy_efficient_cpu()
8379 int prev_fits = -1, best_fits = -1; in find_energy_efficient_cpu()
8387 pd = rcu_dereference(rd->pd); in find_energy_efficient_cpu()
8392 * Energy-aware wake-up happens on the lowest sched_domain starting in find_energy_efficient_cpu()
8397 sd = sd->parent; in find_energy_efficient_cpu()
8403 sync_entity_load_avg(&p->se); in find_energy_efficient_cpu()
8409 for (; pd; pd = pd->next) { in find_energy_efficient_cpu()
8412 long prev_spare_cap = -1, max_spare_cap = -1; in find_energy_efficient_cpu()
8415 int max_spare_cap_cpu = -1; in find_energy_efficient_cpu()
8416 int fits, max_fits = -1; in find_energy_efficient_cpu()
8438 if (!cpumask_test_cpu(cpu, p->cpus_ptr)) in find_energy_efficient_cpu()
8457 * max-aggregated uclamp_{min, max}. in find_energy_efficient_cpu()
8494 base_energy = compute_energy(&eenv, pd, cpus, p, -1); in find_energy_efficient_cpu()
8497 if (prev_spare_cap > -1) { in find_energy_efficient_cpu()
8503 prev_delta -= base_energy; in find_energy_efficient_cpu()
8527 cur_delta -= base_energy; in find_energy_efficient_cpu()
8571 int sync = (wake_flags & WF_SYNC) && !(current->flags & PF_EXITING); in select_task_rq_fair()
8580 * required for stable ->cpus_allowed in select_task_rq_fair()
8582 lockdep_assert_held(&p->pi_lock); in select_task_rq_fair()
8587 cpumask_test_cpu(cpu, p->cpus_ptr)) in select_task_rq_fair()
8590 if (!is_rd_overutilized(this_rq()->rd)) { in select_task_rq_fair()
8597 want_affine = !wake_wide(p) && cpumask_test_cpu(cpu, p->cpus_ptr); in select_task_rq_fair()
8606 if (want_affine && (tmp->flags & SD_WAKE_AFFINE) && in select_task_rq_fair()
8620 if (tmp->flags & sd_flag) in select_task_rq_fair()
8640 * cfs_rq_of(p) references at time of call are still valid and identify the
8641 * previous CPU. The caller guarantees p->pi_lock or task_rq(p)->lock is held.
8645 struct sched_entity *se = &p->se; in migrate_task_rq_fair()
8653 * updated in a while, a substantial idle time will be missed, in migrate_task_rq_fair()
8654 * leading to an inflation after wake-up on the new rq. in migrate_task_rq_fair()
8656 * Estimate the missing time from the cfs_rq last_update_time in migrate_task_rq_fair()
8664 se->avg.last_update_time = 0; in migrate_task_rq_fair()
8671 struct sched_entity *se = &p->se; in task_dead_fair()
8673 if (se->sched_delayed) { in task_dead_fair()
8678 if (se->sched_delayed) { in task_dead_fair()
8703 if (!cpumask_intersects(p->cpus_ptr, cpumask)) in set_task_max_allowed_capacity()
8706 p->max_allowed_capacity = entry->capacity; in set_task_max_allowed_capacity()
8730 if (WARN_ON_ONCE(!se->on_rq)) in set_next_buddy()
8734 cfs_rq_of(se)->next = se; in set_next_buddy()
8743 struct task_struct *donor = rq->donor; in check_preempt_wakeup_fair()
8744 struct sched_entity *se = &donor->se, *pse = &p->se; in check_preempt_wakeup_fair()
8756 * next-buddy nomination below. in check_preempt_wakeup_fair()
8761 if (sched_feat(NEXT_BUDDY) && !(wake_flags & WF_FORK) && !pse->sched_delayed) { in check_preempt_wakeup_fair()
8769 * Note: this also catches the edge-case of curr being in a throttled in check_preempt_wakeup_fair()
8772 * prevents us from potentially nominating it as a false LAST_BUDDY in check_preempt_wakeup_fair()
8775 if (test_tsk_need_resched(rq->curr)) in check_preempt_wakeup_fair()
8788 * Preempt an idle entity in favor of a non-idle entity (and don't preempt in check_preempt_wakeup_fair()
8793 * When non-idle entity preempt an idle entity, in check_preempt_wakeup_fair()
8806 if (unlikely(!normal_policy(p->policy))) in check_preempt_wakeup_fair()
8815 do_preempt_short = sched_feat(PREEMPT_SHORT) && (pse->slice < se->slice); in check_preempt_wakeup_fair()
8843 cfs_rq = &rq->cfs; in pick_task_fair()
8844 if (!cfs_rq->nr_queued) in pick_task_fair()
8851 if (cfs_rq->curr && cfs_rq->curr->on_rq) in pick_task_fair()
8882 se = &p->se; in pick_next_task_fair()
8885 if (prev->sched_class != &fair_sched_class) in pick_next_task_fair()
8902 struct sched_entity *pse = &prev->se; in pick_next_task_fair()
8906 int se_depth = se->depth; in pick_next_task_fair()
8907 int pse_depth = pse->depth; in pick_next_task_fair()
8937 * Because sched_balance_newidle() releases (and re-acquires) in pick_next_task_fair()
8938 * rq->lock, it is possible for any higher priority task to in pick_next_task_fair()
8939 * appear. In that case we must re-start the pick_next_entity() in pick_next_task_fair()
8965 return pick_task_fair(dl_se->rq); in fair_server_pick_task()
8970 struct sched_dl_entity *dl_se = &rq->fair_server; in fair_server_init()
8982 struct sched_entity *se = &prev->se; in put_prev_task_fair()
8996 struct task_struct *curr = rq->curr; in yield_task_fair()
8998 struct sched_entity *se = &curr->se; in yield_task_fair()
9003 if (unlikely(rq->nr_running == 1)) in yield_task_fair()
9010 * Update run-time statistics of the 'current'. in yield_task_fair()
9020 se->deadline += calc_delta_fair(se->slice, se); in yield_task_fair()
9025 struct sched_entity *se = &p->se; in yield_to_task_fair()
9027 /* !se->on_rq also covers throttled task */ in yield_to_task_fair()
9028 if (!se->on_rq) in yield_to_task_fair()
9040 * Fair scheduling class load-balancing methods.
9044 * The purpose of load-balancing is to achieve the same basic fairness the
9045 * per-CPU scheduler provides, namely provide a proportional amount of compute
9046 * time to each task. This is expressed in the following equation:
9050 * Where W_i,n is the n-th weight average for CPU i. The instantaneous weight
9055 * Where w_i,j is the weight of the j-th runnable task on CPU i. This weight
9061 * W'_i,n = (2^n - 1) / 2^n * W_i,n + 1 / 2^n * W_i,0 (3)
9064 * fraction of 'recent' time available for SCHED_OTHER task execution. But it
9070 * imb_i,j = max{ avg(W/C), W_i/C_i } - min{ avg(W/C), W_j/C_j } (4)
9077 * - infeasible weights;
9078 * - local vs global optima in the discrete case. ]
9088 * of load-balance at each level inversely proportional to the number of CPUs in
9094 * \Sum { --- * --- * 2^i } = O(n) (5)
9096 * `- size of each group
9097 * | | `- number of CPUs doing load-balance
9098 * | `- freq
9099 * `- sum over all levels
9131 * time.
9141 * W_i,0 = \Sum_j \Prod_k w_k * ----- (9)
9148 * w_i,j,k is the weight of the j-th runnable task in the k-th cgroup on CPU i.
9230 /* The set of CPUs under consideration for load-balancing */
9245 * Is this task likely cache-hot:
9251 lockdep_assert_rq_held(env->src_rq); in task_hot()
9253 if (p->sched_class != &fair_sched_class) in task_hot()
9260 if (env->sd->flags & SD_SHARE_CPUCAPACITY) in task_hot()
9266 if (sched_feat(CACHE_HOT_BUDDY) && env->dst_rq->nr_running && in task_hot()
9267 (&p->se == cfs_rq_of(&p->se)->next)) in task_hot()
9270 if (sysctl_sched_migration_cost == -1) in task_hot()
9277 if (!sched_core_cookie_match(cpu_rq(env->dst_cpu), p)) in task_hot()
9283 delta = rq_clock_task(env->src_rq) - p->se.exec_start; in task_hot()
9296 struct numa_group *numa_group = rcu_dereference(p->numa_group); in migrate_degrades_locality()
9303 if (!p->numa_faults || !(env->sd->flags & SD_NUMA)) in migrate_degrades_locality()
9306 src_nid = cpu_to_node(env->src_cpu); in migrate_degrades_locality()
9307 dst_nid = cpu_to_node(env->dst_cpu); in migrate_degrades_locality()
9313 if (src_nid == p->numa_preferred_nid) { in migrate_degrades_locality()
9314 if (env->src_rq->nr_running > env->src_rq->nr_preferred_running) in migrate_degrades_locality()
9321 if (dst_nid == p->numa_preferred_nid) in migrate_degrades_locality()
9322 return -1; in migrate_degrades_locality()
9325 if (env->idle == CPU_IDLE) in migrate_degrades_locality()
9337 return src_weight - dst_weight; in migrate_degrades_locality()
9352 * dst_cfs_rq->nr_queued is greater than 1, if the task
9361 dst_cfs_rq = task_group(p)->cfs_rq[dest_cpu]; in task_is_ineligible_on_dst_cpu()
9363 dst_cfs_rq = &cpu_rq(dest_cpu)->cfs; in task_is_ineligible_on_dst_cpu()
9365 if (sched_feat(PLACE_LAG) && dst_cfs_rq->nr_queued && in task_is_ineligible_on_dst_cpu()
9366 !entity_eligible(task_cfs_rq(p), &p->se)) in task_is_ineligible_on_dst_cpu()
9373 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
9380 lockdep_assert_rq_held(env->src_rq); in can_migrate_task()
9381 if (p->sched_task_hot) in can_migrate_task()
9382 p->sched_task_hot = 0; in can_migrate_task()
9390 * 5) are cache-hot on their current CPU, or in can_migrate_task()
9393 if ((p->se.sched_delayed) && (env->migration_type != migrate_load)) in can_migrate_task()
9396 if (lb_throttled_hierarchy(p, env->dst_cpu)) in can_migrate_task()
9401 * For ineligible tasks we soft-limit them and only allow in can_migrate_task()
9402 * them to migrate when nr_balance_failed is non-zero to in can_migrate_task()
9403 * avoid load-balancing trying very hard to balance the load. in can_migrate_task()
9405 if (!env->sd->nr_balance_failed && in can_migrate_task()
9406 task_is_ineligible_on_dst_cpu(p, env->dst_cpu)) in can_migrate_task()
9416 if (!cpumask_test_cpu(env->dst_cpu, p->cpus_ptr)) { in can_migrate_task()
9419 schedstat_inc(p->stats.nr_failed_migrations_affine); in can_migrate_task()
9421 env->flags |= LBF_SOME_PINNED; in can_migrate_task()
9429 * - for NEWLY_IDLE in can_migrate_task()
9430 * - if we have already computed one in current iteration in can_migrate_task()
9431 * - if it's an active balance in can_migrate_task()
9433 if (env->idle == CPU_NEWLY_IDLE || in can_migrate_task()
9434 env->flags & (LBF_DST_PINNED | LBF_ACTIVE_LB)) in can_migrate_task()
9437 /* Prevent to re-select dst_cpu via env's CPUs: */ in can_migrate_task()
9438 cpu = cpumask_first_and_and(env->dst_grpmask, env->cpus, p->cpus_ptr); in can_migrate_task()
9441 env->flags |= LBF_DST_PINNED; in can_migrate_task()
9442 env->new_dst_cpu = cpu; in can_migrate_task()
9449 env->flags &= ~LBF_ALL_PINNED; in can_migrate_task()
9451 if (task_on_cpu(env->src_rq, p) || in can_migrate_task()
9452 task_current_donor(env->src_rq, p)) { in can_migrate_task()
9453 schedstat_inc(p->stats.nr_failed_migrations_running); in can_migrate_task()
9464 if (env->flags & LBF_ACTIVE_LB) in can_migrate_task()
9473 if (!hot || env->sd->nr_balance_failed > env->sd->cache_nice_tries) { in can_migrate_task()
9475 p->sched_task_hot = 1; in can_migrate_task()
9479 schedstat_inc(p->stats.nr_failed_migrations_hot); in can_migrate_task()
9484 * detach_task() -- detach the task for the migration specified in env
9488 lockdep_assert_rq_held(env->src_rq); in detach_task()
9490 if (p->sched_task_hot) { in detach_task()
9491 p->sched_task_hot = 0; in detach_task()
9492 schedstat_inc(env->sd->lb_hot_gained[env->idle]); in detach_task()
9493 schedstat_inc(p->stats.nr_forced_migrations); in detach_task()
9496 WARN_ON(task_current(env->src_rq, p)); in detach_task()
9497 WARN_ON(task_current_donor(env->src_rq, p)); in detach_task()
9499 deactivate_task(env->src_rq, p, DEQUEUE_NOCLOCK); in detach_task()
9500 set_task_cpu(p, env->dst_cpu); in detach_task()
9504 * detach_one_task() -- tries to dequeue exactly one task from env->src_rq, as
9513 lockdep_assert_rq_held(env->src_rq); in detach_one_task()
9516 &env->src_rq->cfs_tasks, se.group_node) { in detach_one_task()
9524 * lb_gained[env->idle] is updated (other is detach_tasks) in detach_one_task()
9528 schedstat_inc(env->sd->lb_gained[env->idle]); in detach_one_task()
9535 * detach_tasks() -- tries to detach up to imbalance load/util/tasks from
9542 struct list_head *tasks = &env->src_rq->cfs_tasks; in detach_tasks()
9547 lockdep_assert_rq_held(env->src_rq); in detach_tasks()
9553 if (env->src_rq->nr_running <= 1) { in detach_tasks()
9554 env->flags &= ~LBF_ALL_PINNED; in detach_tasks()
9558 if (env->imbalance <= 0) in detach_tasks()
9566 if (env->idle && env->src_rq->nr_running <= 1) in detach_tasks()
9569 env->loop++; in detach_tasks()
9571 if (env->loop > env->loop_max) in detach_tasks()
9575 if (env->loop > env->loop_break) { in detach_tasks()
9576 env->loop_break += SCHED_NR_MIGRATE_BREAK; in detach_tasks()
9577 env->flags |= LBF_NEED_BREAK; in detach_tasks()
9586 switch (env->migration_type) { in detach_tasks()
9591 * value. Make sure that env->imbalance decreases in detach_tasks()
9598 load < 16 && !env->sd->nr_balance_failed) in detach_tasks()
9607 if (shr_bound(load, env->sd->nr_balance_failed) > env->imbalance) in detach_tasks()
9610 env->imbalance -= load; in detach_tasks()
9616 if (shr_bound(util, env->sd->nr_balance_failed) > env->imbalance) in detach_tasks()
9619 env->imbalance -= util; in detach_tasks()
9623 env->imbalance--; in detach_tasks()
9628 if (task_fits_cpu(p, env->src_cpu)) in detach_tasks()
9631 env->imbalance = 0; in detach_tasks()
9636 list_add(&p->se.group_node, &env->tasks); in detach_tasks()
9646 if (env->idle == CPU_NEWLY_IDLE) in detach_tasks()
9654 if (env->imbalance <= 0) in detach_tasks()
9659 if (p->sched_task_hot) in detach_tasks()
9660 schedstat_inc(p->stats.nr_failed_migrations_hot); in detach_tasks()
9662 list_move(&p->se.group_node, tasks); in detach_tasks()
9670 schedstat_add(env->sd->lb_gained[env->idle], detached); in detach_tasks()
9676 * attach_task() -- attach the task detached by detach_task() to its new rq.
9688 * attach_one_task() -- attaches the task returned from detach_one_task() to
9702 * attach_tasks() -- attaches all tasks detached by detach_tasks() to their
9707 struct list_head *tasks = &env->tasks; in attach_tasks()
9711 rq_lock(env->dst_rq, &rf); in attach_tasks()
9712 update_rq_clock(env->dst_rq); in attach_tasks()
9716 list_del_init(&p->se.group_node); in attach_tasks()
9718 attach_task(env->dst_rq, p); in attach_tasks()
9721 rq_unlock(env->dst_rq, &rf); in attach_tasks()
9727 if (cfs_rq->avg.load_avg) in cfs_rq_has_blocked()
9730 if (cfs_rq->avg.util_avg) in cfs_rq_has_blocked()
9755 WRITE_ONCE(rq->last_blocked_load_update_tick, jiffies); in update_blocked_load_tick()
9761 rq->has_blocked_load = 0; in update_blocked_load_status()
9804 if (cfs_rq->nr_queued == 0) in __update_blocked_fair()
9807 if (cfs_rq == &rq->cfs) in __update_blocked_fair()
9812 se = cfs_rq->tg->se[cpu]; in __update_blocked_fair()
9833 * This needs to be done in a top-down fashion because the load of a child
9839 struct sched_entity *se = cfs_rq->tg->se[cpu_of(rq)]; in update_cfs_rq_h_load()
9843 if (cfs_rq->last_h_load_update == now) in update_cfs_rq_h_load()
9846 WRITE_ONCE(cfs_rq->h_load_next, NULL); in update_cfs_rq_h_load()
9849 WRITE_ONCE(cfs_rq->h_load_next, se); in update_cfs_rq_h_load()
9850 if (cfs_rq->last_h_load_update == now) in update_cfs_rq_h_load()
9855 cfs_rq->h_load = cfs_rq_load_avg(cfs_rq); in update_cfs_rq_h_load()
9856 cfs_rq->last_h_load_update = now; in update_cfs_rq_h_load()
9859 while ((se = READ_ONCE(cfs_rq->h_load_next)) != NULL) { in update_cfs_rq_h_load()
9860 load = cfs_rq->h_load; in update_cfs_rq_h_load()
9861 load = div64_ul(load * se->avg.load_avg, in update_cfs_rq_h_load()
9864 cfs_rq->h_load = load; in update_cfs_rq_h_load()
9865 cfs_rq->last_h_load_update = now; in update_cfs_rq_h_load()
9874 return div64_ul(p->se.avg.load_avg * cfs_rq->h_load, in task_h_load()
9880 struct cfs_rq *cfs_rq = &rq->cfs; in __update_blocked_fair()
9892 return p->se.avg.load_avg; in task_h_load()
9918 * sg_lb_stats - stats of a sched_group required for load-balancing:
9925 unsigned long group_runnable; /* Total runnable time over the CPUs of the group */
9941 * sd_lb_stats - stats of a sched_domain required for load-balancing:
9998 free = max - used; in scale_rt_capacity()
10006 struct sched_group *sdg = sd->groups; in update_cpu_capacity()
10011 cpu_rq(cpu)->cpu_capacity = capacity; in update_cpu_capacity()
10014 sdg->sgc->capacity = capacity; in update_cpu_capacity()
10015 sdg->sgc->min_capacity = capacity; in update_cpu_capacity()
10016 sdg->sgc->max_capacity = capacity; in update_cpu_capacity()
10021 struct sched_domain *child = sd->child; in update_group_capacity()
10022 struct sched_group *group, *sdg = sd->groups; in update_group_capacity()
10026 interval = msecs_to_jiffies(sd->balance_interval); in update_group_capacity()
10028 sdg->sgc->next_update = jiffies + interval; in update_group_capacity()
10039 if (child->flags & SD_NUMA) { in update_group_capacity()
10058 group = child->groups; in update_group_capacity()
10060 struct sched_group_capacity *sgc = group->sgc; in update_group_capacity()
10062 capacity += sgc->capacity; in update_group_capacity()
10063 min_capacity = min(sgc->min_capacity, min_capacity); in update_group_capacity()
10064 max_capacity = max(sgc->max_capacity, max_capacity); in update_group_capacity()
10065 group = group->next; in update_group_capacity()
10066 } while (group != child->groups); in update_group_capacity()
10069 sdg->sgc->capacity = capacity; in update_group_capacity()
10070 sdg->sgc->min_capacity = min_capacity; in update_group_capacity()
10071 sdg->sgc->max_capacity = max_capacity; in update_group_capacity()
10082 return ((rq->cpu_capacity * sd->imbalance_pct) < in check_cpu_capacity()
10089 return rq->misfit_task_load; in check_misfit_status()
10094 * groups is inadequate due to ->cpus_ptr constraints.
10103 * If we were to balance group-wise we'd place two tasks in the first group and
10123 return group->sgc->imbalance; in sg_imbalanced()
10141 if (sgs->sum_nr_running < sgs->group_weight) in group_has_capacity()
10144 if ((sgs->group_capacity * imbalance_pct) < in group_has_capacity()
10145 (sgs->group_runnable * 100)) in group_has_capacity()
10148 if ((sgs->group_capacity * 100) > in group_has_capacity()
10149 (sgs->group_util * imbalance_pct)) in group_has_capacity()
10166 if (sgs->sum_nr_running <= sgs->group_weight) in group_is_overloaded()
10169 if ((sgs->group_capacity * 100) < in group_is_overloaded()
10170 (sgs->group_util * imbalance_pct)) in group_is_overloaded()
10173 if ((sgs->group_capacity * imbalance_pct) < in group_is_overloaded()
10174 (sgs->group_runnable * 100)) in group_is_overloaded()
10191 if (sgs->group_asym_packing) in group_classify()
10194 if (sgs->group_smt_balance) in group_classify()
10197 if (sgs->group_misfit_task_load) in group_classify()
10207 * sched_use_asym_prio - Check whether asym_packing priority must be used
10219 if (!(sd->flags & SD_ASYM_PACKING)) in sched_use_asym_prio()
10225 return sd->flags & SD_SHARE_CPUCAPACITY || is_core_idle(cpu); in sched_use_asym_prio()
10239 * sched_group_asym - Check if the destination CPU can do asym_packing balance
10241 * @sgs: Load-balancing statistics of the candidate busiest group
10257 if ((group->flags & SD_SHARE_CPUCAPACITY) && in sched_group_asym()
10258 (sgs->group_weight - sgs->idle_cpus != 1)) in sched_group_asym()
10261 return sched_asym(env->sd, env->dst_cpu, READ_ONCE(group->asym_prefer_cpu)); in sched_group_asym()
10271 return (sg1->flags & SD_SHARE_CPUCAPACITY) != in smt_vs_nonsmt_groups()
10272 (sg2->flags & SD_SHARE_CPUCAPACITY); in smt_vs_nonsmt_groups()
10278 if (!env->idle) in smt_balance()
10287 if (group->flags & SD_SHARE_CPUCAPACITY && in smt_balance()
10288 sgs->sum_h_nr_running > 1) in smt_balance()
10302 if (!env->idle || !busiest->sum_nr_running) in sibling_imbalance()
10305 ncores_busiest = sds->busiest->cores; in sibling_imbalance()
10306 ncores_local = sds->local->cores; in sibling_imbalance()
10309 imbalance = busiest->sum_nr_running; in sibling_imbalance()
10310 lsub_positive(&imbalance, local->sum_nr_running); in sibling_imbalance()
10315 imbalance = ncores_local * busiest->sum_nr_running; in sibling_imbalance()
10316 lsub_positive(&imbalance, ncores_busiest * local->sum_nr_running); in sibling_imbalance()
10322 if (imbalance <= 1 && local->sum_nr_running == 0 && in sibling_imbalance()
10323 busiest->sum_nr_running > 1) in sibling_imbalance()
10336 if (rq->cfs.h_nr_runnable != 1) in sched_reduced_capacity()
10343 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
10345 * @sds: Load-balancing data with statistics of the local group.
10358 int i, nr_running, local_group, sd_flags = env->sd->flags; in update_sg_lb_stats()
10359 bool balancing_at_rd = !env->sd->parent; in update_sg_lb_stats()
10363 local_group = group == sds->local; in update_sg_lb_stats()
10365 for_each_cpu_and(i, sched_group_span(group), env->cpus) { in update_sg_lb_stats()
10369 sgs->group_load += load; in update_sg_lb_stats()
10370 sgs->group_util += cpu_util_cfs(i); in update_sg_lb_stats()
10371 sgs->group_runnable += cpu_runnable(rq); in update_sg_lb_stats()
10372 sgs->sum_h_nr_running += rq->cfs.h_nr_runnable; in update_sg_lb_stats()
10374 nr_running = rq->nr_running; in update_sg_lb_stats()
10375 sgs->sum_nr_running += nr_running; in update_sg_lb_stats()
10384 sgs->idle_cpus++; in update_sg_lb_stats()
10396 sgs->nr_numa_running += rq->nr_numa_running; in update_sg_lb_stats()
10397 sgs->nr_preferred_running += rq->nr_preferred_running; in update_sg_lb_stats()
10405 if (sgs->group_misfit_task_load < rq->misfit_task_load) { in update_sg_lb_stats()
10406 sgs->group_misfit_task_load = rq->misfit_task_load; in update_sg_lb_stats()
10409 } else if (env->idle && sched_reduced_capacity(rq, env->sd)) { in update_sg_lb_stats()
10411 if (sgs->group_misfit_task_load < load) in update_sg_lb_stats()
10412 sgs->group_misfit_task_load = load; in update_sg_lb_stats()
10416 sgs->group_capacity = group->sgc->capacity; in update_sg_lb_stats()
10418 sgs->group_weight = group->group_weight; in update_sg_lb_stats()
10421 if (!local_group && env->idle && sgs->sum_h_nr_running && in update_sg_lb_stats()
10423 sgs->group_asym_packing = 1; in update_sg_lb_stats()
10427 sgs->group_smt_balance = 1; in update_sg_lb_stats()
10429 sgs->group_type = group_classify(env->sd->imbalance_pct, group, sgs); in update_sg_lb_stats()
10432 if (sgs->group_type == group_overloaded) in update_sg_lb_stats()
10433 sgs->avg_load = (sgs->group_load * SCHED_CAPACITY_SCALE) / in update_sg_lb_stats()
10434 sgs->group_capacity; in update_sg_lb_stats()
10438 * update_sd_pick_busiest - return 1 on busiest group
10455 struct sg_lb_stats *busiest = &sds->busiest_stat; in update_sd_pick_busiest()
10458 if (!sgs->sum_h_nr_running) in update_sd_pick_busiest()
10467 if ((env->sd->flags & SD_ASYM_CPUCAPACITY) && in update_sd_pick_busiest()
10468 (sgs->group_type == group_misfit_task) && in update_sd_pick_busiest()
10469 (!capacity_greater(capacity_of(env->dst_cpu), sg->sgc->max_capacity) || in update_sd_pick_busiest()
10470 sds->local_stat.group_type != group_has_spare)) in update_sd_pick_busiest()
10473 if (sgs->group_type > busiest->group_type) in update_sd_pick_busiest()
10476 if (sgs->group_type < busiest->group_type) in update_sd_pick_busiest()
10484 switch (sgs->group_type) { in update_sd_pick_busiest()
10487 return sgs->avg_load > busiest->avg_load; in update_sd_pick_busiest()
10498 return sched_asym_prefer(READ_ONCE(sds->busiest->asym_prefer_cpu), in update_sd_pick_busiest()
10499 READ_ONCE(sg->asym_prefer_cpu)); in update_sd_pick_busiest()
10506 return sgs->group_misfit_task_load > busiest->group_misfit_task_load; in update_sd_pick_busiest()
10513 if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) in update_sd_pick_busiest()
10524 * contention when accessing shared HW resources. in update_sd_pick_busiest()
10531 if (sgs->avg_load < busiest->avg_load) in update_sd_pick_busiest()
10534 if (sgs->avg_load == busiest->avg_load) { in update_sd_pick_busiest()
10536 * SMT sched groups need more help than non-SMT groups. in update_sd_pick_busiest()
10539 if (sds->busiest->flags & SD_SHARE_CPUCAPACITY) in update_sd_pick_busiest()
10551 if (smt_vs_nonsmt_groups(sds->busiest, sg)) { in update_sd_pick_busiest()
10552 if (sg->flags & SD_SHARE_CPUCAPACITY && sgs->sum_h_nr_running <= 1) in update_sd_pick_busiest()
10566 if (sgs->idle_cpus > busiest->idle_cpus) in update_sd_pick_busiest()
10568 else if ((sgs->idle_cpus == busiest->idle_cpus) && in update_sd_pick_busiest()
10569 (sgs->sum_nr_running <= busiest->sum_nr_running)) in update_sd_pick_busiest()
10577 * per-CPU capacity. Migrating tasks to less capable CPUs may harm in update_sd_pick_busiest()
10581 if ((env->sd->flags & SD_ASYM_CPUCAPACITY) && in update_sd_pick_busiest()
10582 (sgs->group_type <= group_fully_busy) && in update_sd_pick_busiest()
10583 (capacity_greater(sg->sgc->min_capacity, capacity_of(env->dst_cpu)))) in update_sd_pick_busiest()
10592 if (sgs->sum_h_nr_running > sgs->nr_numa_running) in fbq_classify_group()
10594 if (sgs->sum_h_nr_running > sgs->nr_preferred_running) in fbq_classify_group()
10601 if (rq->nr_running > rq->nr_numa_running) in fbq_classify_rq()
10603 if (rq->nr_running > rq->nr_preferred_running) in fbq_classify_rq()
10623 * task_running_on_cpu - return 1 if @p is running on @cpu.
10629 if (cpu != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in task_running_on_cpu()
10639 * idle_cpu_without - would a given CPU be idle without p ?
10649 if (rq->curr != rq->idle && rq->curr != p) in idle_cpu_without()
10653 * rq->nr_running can't be used but an updated version without the in idle_cpu_without()
10658 if (rq->ttwu_pending) in idle_cpu_without()
10665 * update_sg_wakeup_stats - Update sched_group's statistics for wakeup.
10681 if (sd->flags & SD_ASYM_CPUCAPACITY) in update_sg_wakeup_stats()
10682 sgs->group_misfit_task_load = 1; in update_sg_wakeup_stats()
10688 sgs->group_load += cpu_load_without(rq, p); in update_sg_wakeup_stats()
10689 sgs->group_util += cpu_util_without(i, p); in update_sg_wakeup_stats()
10690 sgs->group_runnable += cpu_runnable_without(rq, p); in update_sg_wakeup_stats()
10692 sgs->sum_h_nr_running += rq->cfs.h_nr_runnable - local; in update_sg_wakeup_stats()
10694 nr_running = rq->nr_running - local; in update_sg_wakeup_stats()
10695 sgs->sum_nr_running += nr_running; in update_sg_wakeup_stats()
10701 sgs->idle_cpus++; in update_sg_wakeup_stats()
10704 if (sd->flags & SD_ASYM_CPUCAPACITY && in update_sg_wakeup_stats()
10705 sgs->group_misfit_task_load && in update_sg_wakeup_stats()
10707 sgs->group_misfit_task_load = 0; in update_sg_wakeup_stats()
10711 sgs->group_capacity = group->sgc->capacity; in update_sg_wakeup_stats()
10713 sgs->group_weight = group->group_weight; in update_sg_wakeup_stats()
10715 sgs->group_type = group_classify(sd->imbalance_pct, group, sgs); in update_sg_wakeup_stats()
10721 if (sgs->group_type == group_fully_busy || in update_sg_wakeup_stats()
10722 sgs->group_type == group_overloaded) in update_sg_wakeup_stats()
10723 sgs->avg_load = (sgs->group_load * SCHED_CAPACITY_SCALE) / in update_sg_wakeup_stats()
10724 sgs->group_capacity; in update_sg_wakeup_stats()
10732 if (sgs->group_type < idlest_sgs->group_type) in update_pick_idlest()
10735 if (sgs->group_type > idlest_sgs->group_type) in update_pick_idlest()
10743 switch (sgs->group_type) { in update_pick_idlest()
10747 if (idlest_sgs->avg_load <= sgs->avg_load) in update_pick_idlest()
10759 if (idlest->sgc->max_capacity >= group->sgc->max_capacity) in update_pick_idlest()
10765 if (idlest_sgs->idle_cpus > sgs->idle_cpus) in update_pick_idlest()
10769 if (idlest_sgs->idle_cpus == sgs->idle_cpus && in update_pick_idlest()
10770 idlest_sgs->group_util <= sgs->group_util) in update_pick_idlest()
10788 struct sched_group *idlest = NULL, *local = NULL, *group = sd->groups; in sched_balance_find_dst_group()
10802 p->cpus_ptr)) in sched_balance_find_dst_group()
10826 } while (group = group->next, group != sd->groups); in sched_balance_find_dst_group()
10857 (sd->imbalance_pct-100) / 100; in sched_balance_find_dst_group()
10864 * cross-domain, add imbalance to the load on the remote node in sched_balance_find_dst_group()
10868 if ((sd->flags & SD_NUMA) && in sched_balance_find_dst_group()
10879 if (100 * local_sgs.avg_load <= sd->imbalance_pct * idlest_sgs.avg_load) in sched_balance_find_dst_group()
10891 if (local->sgc->max_capacity >= idlest->sgc->max_capacity) in sched_balance_find_dst_group()
10897 if (sd->flags & SD_NUMA) { in sched_balance_find_dst_group()
10898 int imb_numa_nr = sd->imb_numa_nr; in sched_balance_find_dst_group()
10905 if (cpu_to_node(this_cpu) == p->numa_preferred_nid) in sched_balance_find_dst_group()
10909 if (cpu_to_node(idlest_cpu) == p->numa_preferred_nid) in sched_balance_find_dst_group()
10921 if (p->nr_cpus_allowed != NR_CPUS) { in sched_balance_find_dst_group()
10924 cpumask_and(cpus, sched_group_span(local), p->cpus_ptr); in sched_balance_find_dst_group()
10925 imb_numa_nr = min(cpumask_weight(cpus), sd->imb_numa_nr); in sched_balance_find_dst_group()
10928 imbalance = abs(local_sgs.idle_cpus - idlest_sgs.idle_cpus); in sched_balance_find_dst_group()
10965 if (!sched_feat(SIS_UTIL) || env->idle == CPU_NEWLY_IDLE) in update_idle_cpu_scan()
10968 llc_weight = per_cpu(sd_llc_size, env->dst_cpu); in update_idle_cpu_scan()
10969 if (env->sd->span_weight != llc_weight) in update_idle_cpu_scan()
10972 sd_share = rcu_dereference(per_cpu(sd_llc_shared, env->dst_cpu)); in update_idle_cpu_scan()
10982 * let y = SCHED_CAPACITY_SCALE - p * x^2 [1] in update_idle_cpu_scan()
11000 * y = SCHED_CAPACITY_SCALE - in update_idle_cpu_scan()
11009 pct = env->sd->imbalance_pct; in update_idle_cpu_scan()
11013 y = SCHED_CAPACITY_SCALE - tmp; in update_idle_cpu_scan()
11018 if ((int)y != sd_share->nr_idle_scan) in update_idle_cpu_scan()
11019 WRITE_ONCE(sd_share->nr_idle_scan, (int)y); in update_idle_cpu_scan()
11023 * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
11030 struct sched_group *sg = env->sd->groups; in update_sd_lb_stats()
11031 struct sg_lb_stats *local = &sds->local_stat; in update_sd_lb_stats()
11040 local_group = cpumask_test_cpu(env->dst_cpu, sched_group_span(sg)); in update_sd_lb_stats()
11042 sds->local = sg; in update_sd_lb_stats()
11045 if (env->idle != CPU_NEWLY_IDLE || in update_sd_lb_stats()
11046 time_after_eq(jiffies, sg->sgc->next_update)) in update_sd_lb_stats()
11047 update_group_capacity(env->sd, env->dst_cpu); in update_sd_lb_stats()
11053 sds->busiest = sg; in update_sd_lb_stats()
11054 sds->busiest_stat = *sgs; in update_sd_lb_stats()
11058 sds->total_load += sgs->group_load; in update_sd_lb_stats()
11059 sds->total_capacity += sgs->group_capacity; in update_sd_lb_stats()
11061 sum_util += sgs->group_util; in update_sd_lb_stats()
11062 sg = sg->next; in update_sd_lb_stats()
11063 } while (sg != env->sd->groups); in update_sd_lb_stats()
11070 if (sds->busiest) in update_sd_lb_stats()
11071 sds->prefer_sibling = !!(sds->busiest->flags & SD_PREFER_SIBLING); in update_sd_lb_stats()
11074 if (env->sd->flags & SD_NUMA) in update_sd_lb_stats()
11075 env->fbq_type = fbq_classify_group(&sds->busiest_stat); in update_sd_lb_stats()
11077 if (!env->sd->parent) { in update_sd_lb_stats()
11079 set_rd_overloaded(env->dst_rq->rd, sg_overloaded); in update_sd_lb_stats()
11081 /* Update over-utilization (tipping point, U >= 0) indicator */ in update_sd_lb_stats()
11082 set_rd_overutilized(env->dst_rq->rd, sg_overutilized); in update_sd_lb_stats()
11084 set_rd_overutilized(env->dst_rq->rd, sg_overutilized); in update_sd_lb_stats()
11091 * calculate_imbalance - Calculate the amount of imbalance present within the
11100 local = &sds->local_stat; in calculate_imbalance()
11101 busiest = &sds->busiest_stat; in calculate_imbalance()
11103 if (busiest->group_type == group_misfit_task) { in calculate_imbalance()
11104 if (env->sd->flags & SD_ASYM_CPUCAPACITY) { in calculate_imbalance()
11106 env->migration_type = migrate_misfit; in calculate_imbalance()
11107 env->imbalance = 1; in calculate_imbalance()
11113 env->migration_type = migrate_load; in calculate_imbalance()
11114 env->imbalance = busiest->group_misfit_task_load; in calculate_imbalance()
11119 if (busiest->group_type == group_asym_packing) { in calculate_imbalance()
11124 env->migration_type = migrate_task; in calculate_imbalance()
11125 env->imbalance = busiest->sum_h_nr_running; in calculate_imbalance()
11129 if (busiest->group_type == group_smt_balance) { in calculate_imbalance()
11131 env->migration_type = migrate_task; in calculate_imbalance()
11132 env->imbalance = 1; in calculate_imbalance()
11136 if (busiest->group_type == group_imbalanced) { in calculate_imbalance()
11138 * In the group_imb case we cannot rely on group-wide averages in calculate_imbalance()
11139 * to ensure CPU-load equilibrium, try to move any task to fix in calculate_imbalance()
11143 env->migration_type = migrate_task; in calculate_imbalance()
11144 env->imbalance = 1; in calculate_imbalance()
11152 if (local->group_type == group_has_spare) { in calculate_imbalance()
11153 if ((busiest->group_type > group_fully_busy) && in calculate_imbalance()
11154 !(env->sd->flags & SD_SHARE_LLC)) { in calculate_imbalance()
11163 env->migration_type = migrate_util; in calculate_imbalance()
11164 env->imbalance = max(local->group_capacity, local->group_util) - in calculate_imbalance()
11165 local->group_util; in calculate_imbalance()
11174 if (env->idle && env->imbalance == 0) { in calculate_imbalance()
11175 env->migration_type = migrate_task; in calculate_imbalance()
11176 env->imbalance = 1; in calculate_imbalance()
11182 if (busiest->group_weight == 1 || sds->prefer_sibling) { in calculate_imbalance()
11187 env->migration_type = migrate_task; in calculate_imbalance()
11188 env->imbalance = sibling_imbalance(env, sds, busiest, local); in calculate_imbalance()
11195 env->migration_type = migrate_task; in calculate_imbalance()
11196 env->imbalance = max_t(long, 0, in calculate_imbalance()
11197 (local->idle_cpus - busiest->idle_cpus)); in calculate_imbalance()
11202 if (env->sd->flags & SD_NUMA) { in calculate_imbalance()
11203 env->imbalance = adjust_numa_imbalance(env->imbalance, in calculate_imbalance()
11204 local->sum_nr_running + 1, in calculate_imbalance()
11205 env->sd->imb_numa_nr); in calculate_imbalance()
11210 env->imbalance >>= 1; in calculate_imbalance()
11219 if (local->group_type < group_overloaded) { in calculate_imbalance()
11225 local->avg_load = (local->group_load * SCHED_CAPACITY_SCALE) / in calculate_imbalance()
11226 local->group_capacity; in calculate_imbalance()
11232 if (local->avg_load >= busiest->avg_load) { in calculate_imbalance()
11233 env->imbalance = 0; in calculate_imbalance()
11237 sds->avg_load = (sds->total_load * SCHED_CAPACITY_SCALE) / in calculate_imbalance()
11238 sds->total_capacity; in calculate_imbalance()
11244 if (local->avg_load >= sds->avg_load) { in calculate_imbalance()
11245 env->imbalance = 0; in calculate_imbalance()
11255 * below the average load. At the same time, we also don't want to in calculate_imbalance()
11259 env->migration_type = migrate_load; in calculate_imbalance()
11260 env->imbalance = min( in calculate_imbalance()
11261 (busiest->avg_load - sds->avg_load) * busiest->group_capacity, in calculate_imbalance()
11262 (sds->avg_load - local->avg_load) * local->group_capacity in calculate_imbalance()
11289 * sched_balance_find_src_group - Returns the busiest group within the sched_domain
11296 * Return: - The busiest group if imbalance exists.
11318 if (busiest->group_type == group_misfit_task) in sched_balance_find_src_group()
11321 if (!is_rd_overutilized(env->dst_rq->rd) && in sched_balance_find_src_group()
11322 rcu_dereference(env->dst_rq->rd->pd)) in sched_balance_find_src_group()
11326 if (busiest->group_type == group_asym_packing) in sched_balance_find_src_group()
11334 if (busiest->group_type == group_imbalanced) in sched_balance_find_src_group()
11342 if (local->group_type > busiest->group_type) in sched_balance_find_src_group()
11349 if (local->group_type == group_overloaded) { in sched_balance_find_src_group()
11354 if (local->avg_load >= busiest->avg_load) in sched_balance_find_src_group()
11365 if (local->avg_load >= sds.avg_load) in sched_balance_find_src_group()
11372 if (100 * busiest->avg_load <= in sched_balance_find_src_group()
11373 env->sd->imbalance_pct * local->avg_load) in sched_balance_find_src_group()
11381 if (sds.prefer_sibling && local->group_type == group_has_spare && in sched_balance_find_src_group()
11385 if (busiest->group_type != group_overloaded) { in sched_balance_find_src_group()
11386 if (!env->idle) { in sched_balance_find_src_group()
11395 if (busiest->group_type == group_smt_balance && in sched_balance_find_src_group()
11401 if (busiest->group_weight > 1 && in sched_balance_find_src_group()
11402 local->idle_cpus <= (busiest->idle_cpus + 1)) { in sched_balance_find_src_group()
11415 if (busiest->sum_h_nr_running == 1) { in sched_balance_find_src_group()
11426 return env->imbalance ? sds.busiest : NULL; in sched_balance_find_src_group()
11429 env->imbalance = 0; in sched_balance_find_src_group()
11434 * sched_balance_find_src_rq - find the busiest runqueue among the CPUs in the group.
11444 for_each_cpu_and(i, sched_group_span(group), env->cpus) { in sched_balance_find_src_rq()
11454 * - regular: there are !numa tasks in sched_balance_find_src_rq()
11455 * - remote: there are numa tasks that run on the 'wrong' node in sched_balance_find_src_rq()
11456 * - all: there is no distinction in sched_balance_find_src_rq()
11471 if (rt > env->fbq_type) in sched_balance_find_src_rq()
11474 nr_running = rq->cfs.h_nr_runnable; in sched_balance_find_src_rq()
11482 * eventually lead to active_balancing high->low capacity. in sched_balance_find_src_rq()
11483 * Higher per-CPU capacity is considered better than balancing in sched_balance_find_src_rq()
11486 if (env->sd->flags & SD_ASYM_CPUCAPACITY && in sched_balance_find_src_rq()
11487 !capacity_greater(capacity_of(env->dst_cpu), capacity) && in sched_balance_find_src_rq()
11498 if (sched_asym(env->sd, i, env->dst_cpu) && nr_running == 1) in sched_balance_find_src_rq()
11501 switch (env->migration_type) { in sched_balance_find_src_rq()
11509 if (nr_running == 1 && load > env->imbalance && in sched_balance_find_src_rq()
11510 !check_cpu_capacity(rq, env->sd)) in sched_balance_find_src_rq()
11562 if (rq->misfit_task_load > busiest_load) { in sched_balance_find_src_rq()
11563 busiest_load = rq->misfit_task_load; in sched_balance_find_src_rq()
11594 return env->idle && sched_use_asym_prio(env->sd, env->dst_cpu) && in asym_active_balance()
11595 (sched_asym_prefer(env->dst_cpu, env->src_cpu) || in asym_active_balance()
11596 !sched_use_asym_prio(env->sd, env->src_cpu)); in asym_active_balance()
11602 struct sched_domain *sd = env->sd; in imbalanced_active_balance()
11609 if ((env->migration_type == migrate_task) && in imbalanced_active_balance()
11610 (sd->nr_balance_failed > sd->cache_nice_tries+2)) in imbalanced_active_balance()
11618 struct sched_domain *sd = env->sd; in need_active_balance()
11632 if (env->idle && in need_active_balance()
11633 (env->src_rq->cfs.h_nr_runnable == 1)) { in need_active_balance()
11634 if ((check_cpu_capacity(env->src_rq, sd)) && in need_active_balance()
11635 (capacity_of(env->src_cpu)*sd->imbalance_pct < capacity_of(env->dst_cpu)*100)) in need_active_balance()
11639 if (env->migration_type == migrate_misfit) in need_active_balance()
11650 struct sched_group *sg = env->sd->groups; in should_we_balance()
11651 int cpu, idle_smt = -1; in should_we_balance()
11657 if (!cpumask_test_cpu(env->dst_cpu, env->cpus)) in should_we_balance()
11667 if (env->idle == CPU_NEWLY_IDLE) { in should_we_balance()
11668 if (env->dst_rq->nr_running > 0 || env->dst_rq->ttwu_pending) in should_we_balance()
11675 for_each_cpu_and(cpu, swb_cpus, env->cpus) { in should_we_balance()
11684 if (!(env->sd->flags & SD_SHARE_CPUCAPACITY) && !is_core_idle(cpu)) { in should_we_balance()
11685 if (idle_smt == -1) in should_we_balance()
11699 * Are we the first idle core in a non-SMT domain or higher, in should_we_balance()
11702 return cpu == env->dst_cpu; in should_we_balance()
11706 if (idle_smt != -1) in should_we_balance()
11707 return idle_smt == env->dst_cpu; in should_we_balance()
11710 return group_balance_cpu(sg) == env->dst_cpu; in should_we_balance()
11719 switch (env->migration_type) { in update_lb_imbalance_stat()
11721 __schedstat_add(sd->lb_imbalance_load[idle], env->imbalance); in update_lb_imbalance_stat()
11724 __schedstat_add(sd->lb_imbalance_util[idle], env->imbalance); in update_lb_imbalance_stat()
11727 __schedstat_add(sd->lb_imbalance_task[idle], env->imbalance); in update_lb_imbalance_stat()
11730 __schedstat_add(sd->lb_imbalance_misfit[idle], env->imbalance); in update_lb_imbalance_stat()
11744 struct sched_domain *sd_parent = sd->parent; in sched_balance_rq()
11753 .dst_grpmask = group_balance_mask(sd->groups), in sched_balance_rq()
11763 schedstat_inc(sd->lb_count[idle]); in sched_balance_rq()
11773 schedstat_inc(sd->lb_nobusyg[idle]); in sched_balance_rq()
11779 schedstat_inc(sd->lb_nobusyq[idle]); in sched_balance_rq()
11787 env.src_cpu = busiest->cpu; in sched_balance_rq()
11793 if (busiest->nr_running > 1) { in sched_balance_rq()
11796 * an imbalance but busiest->nr_running <= 1, the group is in sched_balance_rq()
11800 env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running); in sched_balance_rq()
11807 * cur_ld_moved - load moved in current iteration in sched_balance_rq()
11808 * ld_moved - cumulative load moved across iterations in sched_balance_rq()
11815 * unlock busiest->lock, and we are able to be sure in sched_balance_rq()
11836 * us and move them to an alternate dst_cpu in our sched_group in sched_balance_rq()
11844 * nohz-idle), we now have balance_cpu in a position to move in sched_balance_rq()
11847 * _independently_ and at _same_ time to move some load to in sched_balance_rq()
11855 /* Prevent to re-select dst_cpu via env's CPUs */ in sched_balance_rq()
11875 int *group_imbalance = &sd_parent->groups->sgc->imbalance; in sched_balance_rq()
11902 schedstat_inc(sd->lb_failed[idle]); in sched_balance_rq()
11914 sd->nr_balance_failed++; in sched_balance_rq()
11926 if (!cpumask_test_cpu(this_cpu, busiest->curr->cpus_ptr)) { in sched_balance_rq()
11935 * ->active_balance synchronizes accesses to in sched_balance_rq()
11936 * ->active_balance_work. Once set, it's cleared in sched_balance_rq()
11939 if (!busiest->active_balance) { in sched_balance_rq()
11940 busiest->active_balance = 1; in sched_balance_rq()
11941 busiest->push_cpu = this_cpu; in sched_balance_rq()
11950 &busiest->active_balance_work); in sched_balance_rq()
11955 sd->nr_balance_failed = 0; in sched_balance_rq()
11960 sd->balance_interval = sd->min_interval; in sched_balance_rq()
11972 int *group_imbalance = &sd_parent->groups->sgc->imbalance; in sched_balance_rq()
11984 schedstat_inc(sd->lb_balanced[idle]); in sched_balance_rq()
11986 sd->nr_balance_failed = 0; in sched_balance_rq()
11994 * skyrocketing in a short amount of time. Skip the balance_interval in sched_balance_rq()
11998 * the system being busy and requires lb to backoff to let it settle in sched_balance_rq()
12007 sd->balance_interval < MAX_PINNED_INTERVAL) || in sched_balance_rq()
12008 sd->balance_interval < sd->max_interval) in sched_balance_rq()
12009 sd->balance_interval *= 2; in sched_balance_rq()
12017 unsigned long interval = sd->balance_interval; in get_sd_balance_interval()
12020 interval *= sd->busy_factor; in get_sd_balance_interval()
12031 interval -= 1; in get_sd_balance_interval()
12045 next = sd->last_balance + interval; in update_next_balance()
12061 int target_cpu = busiest_rq->push_cpu; in active_load_balance_cpu_stop()
12069 * Between queueing the stop-work and running it is a hole in which in active_load_balance_cpu_stop()
12078 !busiest_rq->active_balance)) in active_load_balance_cpu_stop()
12082 if (busiest_rq->nr_running <= 1) in active_load_balance_cpu_stop()
12088 * Bjorn Helgaas on a 128-CPU setup. in active_load_balance_cpu_stop()
12092 /* Search for an sd spanning us and the target CPU. */ in active_load_balance_cpu_stop()
12104 .src_cpu = busiest_rq->cpu, in active_load_balance_cpu_stop()
12110 schedstat_inc(sd->alb_count); in active_load_balance_cpu_stop()
12115 schedstat_inc(sd->alb_pushed); in active_load_balance_cpu_stop()
12117 sd->nr_balance_failed = 0; in active_load_balance_cpu_stop()
12119 schedstat_inc(sd->alb_failed); in active_load_balance_cpu_stop()
12124 busiest_rq->active_balance = 0; in active_load_balance_cpu_stop()
12136 * This flag serializes load-balancing passes over large domains
12137 * (above the NODE topology level) - only one load-balancing instance
12138 * may run at a time, to reduce overhead on very large systems with
12141 * - Note that load-balancing passes triggered while another one
12142 * is executing are skipped and not re-tried.
12144 * - Also note that this does not serialize rebalance_domains()
12145 * execution, as non-SD_SERIALIZE domains will still be
12146 * load-balanced in parallel.
12152 * This trades load-balance latency on larger machines for less cross talk.
12161 if (cost > sd->max_newidle_lb_cost) { in update_newidle_cost()
12171 sd->max_newidle_lb_cost = in update_newidle_cost()
12173 sd->last_decay_max_lb_cost = jiffies; in update_newidle_cost()
12174 } else if (time_after(jiffies, sd->last_decay_max_lb_cost + HZ)) { in update_newidle_cost()
12180 sd->max_newidle_lb_cost = (sd->max_newidle_lb_cost * 253) / 256; in update_newidle_cost()
12181 sd->last_decay_max_lb_cost = jiffies; in update_newidle_cost()
12198 int cpu = rq->cpu; in sched_balance_domains()
12202 /* Earliest time when we have to do rebalance again */ in sched_balance_domains()
12215 max_cost += sd->max_newidle_lb_cost; in sched_balance_domains()
12230 need_serialize = sd->flags & SD_SERIALIZE; in sched_balance_domains()
12236 if (time_after_eq(jiffies, sd->last_balance + interval)) { in sched_balance_domains()
12240 * env->dst_cpu, so we can't know our idle in sched_balance_domains()
12246 sd->last_balance = jiffies; in sched_balance_domains()
12252 if (time_after(next_balance, sd->last_balance + interval)) { in sched_balance_domains()
12253 next_balance = sd->last_balance + interval; in sched_balance_domains()
12259 * Ensure the rq-wide value also decays but keep it at a in sched_balance_domains()
12260 * reasonable floor to avoid funnies with rq->avg_idle. in sched_balance_domains()
12262 rq->max_idle_balance_cost = in sched_balance_domains()
12273 rq->next_balance = next_balance; in sched_balance_domains()
12279 return unlikely(!rcu_dereference_sched(rq->sd)); in on_null_domain()
12286 * - When one of the busy CPUs notices that there may be an idle rebalancing
12306 return -1; in find_new_ilb()
12310 * Kick a CPU to do the NOHZ balancing, if it is time for it, via a cross-CPU
12351 smp_call_function_single_async(ilb_cpu, &cpu_rq(ilb_cpu)->nohz_csd); in kick_ilb()
12363 int nr_busy, i, cpu = rq->cpu; in nohz_balancer_kick()
12366 if (unlikely(rq->idle_balance)) in nohz_balancer_kick()
12389 if (rq->nr_running >= 2) { in nohz_balancer_kick()
12396 sd = rcu_dereference(rq->sd); in nohz_balancer_kick()
12402 if (rq->cfs.h_nr_runnable >= 1 && check_cpu_capacity(rq, sd)) { in nohz_balancer_kick()
12451 * increase the overall cache utilization), we need a less-loaded LLC in nohz_balancer_kick()
12455 * the others are - so just get a NOHZ balance going if it looks in nohz_balancer_kick()
12458 nr_busy = atomic_read(&sds->nr_busy_cpus); in nohz_balancer_kick()
12481 if (!sd || !sd->nohz_idle) in set_cpu_sd_state_busy()
12483 sd->nohz_idle = 0; in set_cpu_sd_state_busy()
12485 atomic_inc(&sd->shared->nr_busy_cpus); in set_cpu_sd_state_busy()
12494 if (likely(!rq->nohz_tick_stopped)) in nohz_balance_exit_idle()
12497 rq->nohz_tick_stopped = 0; in nohz_balance_exit_idle()
12498 cpumask_clear_cpu(rq->cpu, nohz.idle_cpus_mask); in nohz_balance_exit_idle()
12501 set_cpu_sd_state_busy(rq->cpu); in nohz_balance_exit_idle()
12511 if (!sd || sd->nohz_idle) in set_cpu_sd_state_idle()
12513 sd->nohz_idle = 1; in set_cpu_sd_state_idle()
12515 atomic_dec(&sd->shared->nr_busy_cpus); in set_cpu_sd_state_idle()
12535 * Can be set safely without rq->lock held in nohz_balance_enter_idle()
12537 * rq->lock is held during the check and the clear in nohz_balance_enter_idle()
12539 rq->has_blocked_load = 1; in nohz_balance_enter_idle()
12547 if (rq->nohz_tick_stopped) in nohz_balance_enter_idle()
12554 rq->nohz_tick_stopped = 1; in nohz_balance_enter_idle()
12571 * Each time a cpu enter idle, we assume that it has blocked load and in nohz_balance_enter_idle()
12579 unsigned int cpu = rq->cpu; in update_nohz_stats()
12581 if (!rq->has_blocked_load) in update_nohz_stats()
12587 if (!time_after(jiffies, READ_ONCE(rq->last_blocked_load_update_tick))) in update_nohz_stats()
12592 return rq->has_blocked_load; in update_nohz_stats()
12602 /* Earliest time when we have to do rebalance again */ in _nohz_idle_balance()
12607 int this_cpu = this_rq->cpu; in _nohz_idle_balance()
12615 * the has_blocked flag. If a cpu enters idle in the mean time, it will in _nohz_idle_balance()
12661 * If time for next balance is due, in _nohz_idle_balance()
12664 if (time_after_eq(jiffies, rq->next_balance)) { in _nohz_idle_balance()
12675 if (time_after(next_balance, rq->next_balance)) { in _nohz_idle_balance()
12676 next_balance = rq->next_balance; in _nohz_idle_balance()
12705 unsigned int flags = this_rq->nohz_idle_balance; in nohz_idle_balance()
12710 this_rq->nohz_idle_balance = 0; in nohz_idle_balance()
12733 * cpu about to enter idle, because it can take a long time.
12751 int this_cpu = this_rq->cpu; in nohz_newidle_balance()
12753 /* Will wake up very soon. No time for doing anything else*/ in nohz_newidle_balance()
12754 if (this_rq->avg_idle < sysctl_sched_migration_cost) in nohz_newidle_balance()
12785 * < 0 - we released the lock and there are !fair tasks present
12786 * 0 - failed, no new tasks
12787 * > 0 - success, new (fair) tasks present
12792 int this_cpu = this_rq->cpu; in sched_balance_newidle()
12804 if (this_rq->ttwu_pending) in sched_balance_newidle()
12810 * as idle time. in sched_balance_newidle()
12812 this_rq->idle_stamp = rq_clock(this_rq); in sched_balance_newidle()
12822 * for load-balance and preemption/IRQs are still disabled avoiding in sched_balance_newidle()
12824 * re-start the picking loop. in sched_balance_newidle()
12829 sd = rcu_dereference_check_sched_domain(this_rq->sd); in sched_balance_newidle()
12831 if (!get_rd_overloaded(this_rq->rd) || in sched_balance_newidle()
12832 (sd && this_rq->avg_idle < sd->max_newidle_lb_cost)) { in sched_balance_newidle()
12853 if (this_rq->avg_idle < curr_cost + sd->max_newidle_lb_cost) in sched_balance_newidle()
12856 if (sd->flags & SD_BALANCE_NEWIDLE) { in sched_balance_newidle()
12863 domain_cost = t1 - t0; in sched_balance_newidle()
12872 domain_cost = (3 * sd->max_newidle_lb_cost) / 2; in sched_balance_newidle()
12888 if (curr_cost > this_rq->max_idle_balance_cost) in sched_balance_newidle()
12889 this_rq->max_idle_balance_cost = curr_cost; in sched_balance_newidle()
12896 if (this_rq->cfs.h_nr_queued && !pulled_task) in sched_balance_newidle()
12900 if (this_rq->nr_running != this_rq->cfs.h_nr_queued) in sched_balance_newidle()
12901 pulled_task = -1; in sched_balance_newidle()
12905 if (time_after(this_rq->next_balance, next_balance)) in sched_balance_newidle()
12906 this_rq->next_balance = next_balance; in sched_balance_newidle()
12909 this_rq->idle_stamp = 0; in sched_balance_newidle()
12921 * - directly from the local sched_tick() for periodic load balancing
12923 * - indirectly from a remote sched_tick() for NOHZ idle balancing
12924 * through the SMP cross-call nohz_csd_func()
12929 enum cpu_idle_type idle = this_rq->idle_balance; in sched_balance_softirq()
12942 sched_balance_update_blocked_averages(this_rq->cpu); in sched_balance_softirq()
12947 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
12958 if (time_after_eq(jiffies, rq->next_balance)) in sched_balance_trigger()
12986 u64 rtime = se->sum_exec_runtime - se->prev_sum_exec_runtime; in __entity_slice_used()
12987 u64 slice = se->slice; in __entity_slice_used()
13009 * MIN_NR_TASKS_DURING_FORCEIDLE - 1 tasks and use that to check in task_tick_core()
13012 if (rq->core->core_forceidle_count && rq->cfs.nr_queued == 1 && in task_tick_core()
13013 __entity_slice_used(&curr->se, MIN_NR_TASKS_DURING_FORCEIDLE)) in task_tick_core()
13018 * se_fi_update - Update the cfs_rq->min_vruntime_fi in a CFS hierarchy if needed.
13027 if (cfs_rq->forceidle_seq == fi_seq) in se_fi_update()
13029 cfs_rq->forceidle_seq = fi_seq; in se_fi_update()
13032 cfs_rq->min_vruntime_fi = cfs_rq->min_vruntime; in se_fi_update()
13038 struct sched_entity *se = &p->se; in task_vruntime_update()
13040 if (p->sched_class != &fair_sched_class) in task_vruntime_update()
13043 se_fi_update(se, rq->core->core_forceidle_seq, in_fi); in task_vruntime_update()
13050 const struct sched_entity *sea = &a->se; in cfs_prio_less()
13051 const struct sched_entity *seb = &b->se; in cfs_prio_less()
13056 WARN_ON_ONCE(task_rq(b)->core != rq->core); in cfs_prio_less()
13063 while (sea->cfs_rq->tg != seb->cfs_rq->tg) { in cfs_prio_less()
13064 int sea_depth = sea->depth; in cfs_prio_less()
13065 int seb_depth = seb->depth; in cfs_prio_less()
13073 se_fi_update(sea, rq->core->core_forceidle_seq, in_fi); in cfs_prio_less()
13074 se_fi_update(seb, rq->core->core_forceidle_seq, in_fi); in cfs_prio_less()
13076 cfs_rqa = sea->cfs_rq; in cfs_prio_less()
13077 cfs_rqb = seb->cfs_rq; in cfs_prio_less()
13079 cfs_rqa = &task_rq(a)->cfs; in cfs_prio_less()
13080 cfs_rqb = &task_rq(b)->cfs; in cfs_prio_less()
13088 delta = (s64)(sea->vruntime - seb->vruntime) + in cfs_prio_less()
13089 (s64)(cfs_rqb->min_vruntime_fi - cfs_rqa->min_vruntime_fi); in cfs_prio_less()
13099 cfs_rq = task_group(p)->cfs_rq[cpu]; in task_is_throttled_fair()
13101 cfs_rq = &cpu_rq(cpu)->cfs; in task_is_throttled_fair()
13120 struct sched_entity *se = &curr->se; in task_tick_fair()
13138 * - child not yet on the tasklist
13139 * - preemption disabled
13156 if (rq->cfs.nr_queued == 1) in prio_changed_fair()
13165 if (p->prio > oldprio) in prio_changed_fair()
13190 se = se->parent; in propagate_entity_cfs_rq()
13213 * - A forked task which hasn't been woken up by wake_up_new_task(). in detach_entity_cfs_rq()
13214 * - A task which has been woken up by try_to_wake_up() but is in detach_entity_cfs_rq()
13217 if (!se->avg.last_update_time) in detach_entity_cfs_rq()
13240 struct sched_entity *se = &p->se; in detach_task_cfs_rq()
13247 struct sched_entity *se = &p->se; in attach_task_cfs_rq()
13259 WARN_ON_ONCE(p->se.sched_delayed); in switched_to_fair()
13280 struct sched_entity *se = &p->se; in __set_next_task_fair()
13287 list_move(&se->group_node, &rq->cfs_tasks); in __set_next_task_fair()
13292 WARN_ON_ONCE(se->sched_delayed); in __set_next_task_fair()
13304 * This routine is mostly called to set cfs_rq->curr field when a task
13309 struct sched_entity *se = &p->se; in set_next_task_fair()
13324 cfs_rq->tasks_timeline = RB_ROOT_CACHED; in init_cfs_rq()
13325 cfs_rq->min_vruntime = (u64)(-(1LL << 20)); in init_cfs_rq()
13326 raw_spin_lock_init(&cfs_rq->removed.lock); in init_cfs_rq()
13336 if (READ_ONCE(p->__state) == TASK_NEW) in task_change_group_fair()
13341 /* Tell se's cfs_rq has been changed -- migrated */ in task_change_group_fair()
13342 p->se.avg.last_update_time = 0; in task_change_group_fair()
13352 if (tg->cfs_rq) in free_fair_sched_group()
13353 kfree(tg->cfs_rq[i]); in free_fair_sched_group()
13354 if (tg->se) in free_fair_sched_group()
13355 kfree(tg->se[i]); in free_fair_sched_group()
13358 kfree(tg->cfs_rq); in free_fair_sched_group()
13359 kfree(tg->se); in free_fair_sched_group()
13368 tg->cfs_rq = kcalloc(nr_cpu_ids, sizeof(cfs_rq), GFP_KERNEL); in alloc_fair_sched_group()
13369 if (!tg->cfs_rq) in alloc_fair_sched_group()
13371 tg->se = kcalloc(nr_cpu_ids, sizeof(se), GFP_KERNEL); in alloc_fair_sched_group()
13372 if (!tg->se) in alloc_fair_sched_group()
13375 tg->shares = NICE_0_LOAD; in alloc_fair_sched_group()
13391 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]); in alloc_fair_sched_group()
13412 se = tg->se[i]; in online_fair_sched_group()
13428 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu]; in unregister_fair_sched_group()
13429 struct sched_entity *se = tg->se[cpu]; in unregister_fair_sched_group()
13433 if (se->sched_delayed) { in unregister_fair_sched_group()
13435 if (se->sched_delayed) { in unregister_fair_sched_group()
13446 * check on_list without danger of it being re-added. in unregister_fair_sched_group()
13448 if (cfs_rq->on_list) { in unregister_fair_sched_group()
13461 cfs_rq->tg = tg; in init_tg_cfs_entry()
13462 cfs_rq->rq = rq; in init_tg_cfs_entry()
13465 tg->cfs_rq[cpu] = cfs_rq; in init_tg_cfs_entry()
13466 tg->se[cpu] = se; in init_tg_cfs_entry()
13473 se->cfs_rq = &rq->cfs; in init_tg_cfs_entry()
13474 se->depth = 0; in init_tg_cfs_entry()
13476 se->cfs_rq = parent->my_q; in init_tg_cfs_entry()
13477 se->depth = parent->depth + 1; in init_tg_cfs_entry()
13480 se->my_q = cfs_rq; in init_tg_cfs_entry()
13482 update_load_set(&se->load, NICE_0_LOAD); in init_tg_cfs_entry()
13483 se->parent = parent; in init_tg_cfs_entry()
13497 if (!tg->se[0]) in __sched_group_set_shares()
13498 return -EINVAL; in __sched_group_set_shares()
13502 if (tg->shares == shares) in __sched_group_set_shares()
13505 tg->shares = shares; in __sched_group_set_shares()
13508 struct sched_entity *se = tg->se[i]; in __sched_group_set_shares()
13530 ret = -EINVAL; in sched_group_set_shares()
13543 return -EINVAL; in sched_group_set_idle()
13546 return -EINVAL; in sched_group_set_idle()
13550 if (tg->idle == idle) { in sched_group_set_idle()
13555 tg->idle = idle; in sched_group_set_idle()
13559 struct sched_entity *se = tg->se[i]; in sched_group_set_idle()
13560 struct cfs_rq *grp_cfs_rq = tg->cfs_rq[i]; in sched_group_set_idle()
13567 grp_cfs_rq->idle = idle; in sched_group_set_idle()
13571 idle_task_delta = grp_cfs_rq->h_nr_queued - in sched_group_set_idle()
13572 grp_cfs_rq->h_nr_idle; in sched_group_set_idle()
13574 idle_task_delta *= -1; in sched_group_set_idle()
13579 if (!se->on_rq) in sched_group_set_idle()
13582 cfs_rq->h_nr_idle += idle_task_delta; in sched_group_set_idle()
13608 struct sched_entity *se = &task->se; in get_rr_interval_fair()
13612 * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise in get_rr_interval_fair()
13615 if (rq->cfs.load.weight) in get_rr_interval_fair()
13616 rr_interval = NS_TO_JIFFIES(se->slice); in get_rr_interval_fair()
13691 ng = rcu_dereference(p->numa_group); in show_numa_stats()
13693 if (p->numa_faults) { in show_numa_stats()
13694 tsf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 0)]; in show_numa_stats()
13695 tpf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 1)]; in show_numa_stats()
13698 gsf = ng->faults[task_faults_idx(NUMA_MEM, node, 0)], in show_numa_stats()
13699 gpf = ng->faults[task_faults_idx(NUMA_MEM, node, 1)]; in show_numa_stats()
13718 INIT_CSD(&cpu_rq(i)->cfsb_csd, __cfsb_csd_unthrottle, cpu_rq(i)); in init_sched_fair_class()
13719 INIT_LIST_HEAD(&cpu_rq(i)->cfsb_csd_list); in init_sched_fair_class()