Lines Matching +full:lock +full:- +full:latency +full:- +full:ns
1 // SPDX-License-Identifier: GPL-2.0
43 #include <linux/memory-tiers.h>
60 * Targeted preemption latency for CPU-bound tasks:
62 * NOTE: this latency value is not the same as the concept of
63 * 'timeslice length' - timeslices in CFS are of variable length
64 * and have no persistent notion like in traditional, time-slice
68 * run vmstat and monitor the context-switches (cs) field)
76 * The initial- and re-scaling of tunables is configurable
80 * SCHED_TUNABLESCALING_NONE - unscaled, always *1
81 * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus)
82 * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus
89 * Minimal preemption granularity for CPU-bound tasks:
97 * Minimal preemption granularity for CPU-bound SCHED_IDLE tasks.
116 * SCHED_OTHER wake-up granularity.
119 * and reduces their over-scheduling. Synchronous workloads will still
148 return -cpu; in arch_asym_cpu_priority()
169 * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool
213 lw->weight += inc; in update_load_add()
214 lw->inv_weight = 0; in update_load_add()
219 lw->weight -= dec; in update_load_sub()
220 lw->inv_weight = 0; in update_load_sub()
225 lw->weight = w; in update_load_set()
226 lw->inv_weight = 0; in update_load_set()
231 * because with more CPUs the 'effective latency' as visible
233 * so pick a second-best guess by going with the log2 of the
283 if (likely(lw->inv_weight)) in __update_inv_weight()
286 w = scale_load_down(lw->weight); in __update_inv_weight()
289 lw->inv_weight = 1; in __update_inv_weight()
291 lw->inv_weight = WMULT_CONST; in __update_inv_weight()
293 lw->inv_weight = WMULT_CONST / w; in __update_inv_weight()
299 * (delta_exec * (weight * lw->inv_weight)) >> WMULT_SHIFT
319 shift -= fs; in __calc_delta()
323 fact = mul_u32_u32(fact, lw->inv_weight); in __calc_delta()
328 shift -= fs; in __calc_delta()
346 for (; se; se = se->parent)
353 if (cfs_rq->on_list) in list_add_leaf_cfs_rq()
354 return rq->tmp_alone_branch == &rq->leaf_cfs_rq_list; in list_add_leaf_cfs_rq()
356 cfs_rq->on_list = 1; in list_add_leaf_cfs_rq()
361 * enqueued. The fact that we always enqueue bottom-up in list_add_leaf_cfs_rq()
367 if (cfs_rq->tg->parent && in list_add_leaf_cfs_rq()
368 cfs_rq->tg->parent->cfs_rq[cpu]->on_list) { in list_add_leaf_cfs_rq()
375 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list, in list_add_leaf_cfs_rq()
376 &(cfs_rq->tg->parent->cfs_rq[cpu]->leaf_cfs_rq_list)); in list_add_leaf_cfs_rq()
382 rq->tmp_alone_branch = &rq->leaf_cfs_rq_list; in list_add_leaf_cfs_rq()
386 if (!cfs_rq->tg->parent) { in list_add_leaf_cfs_rq()
391 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list, in list_add_leaf_cfs_rq()
392 &rq->leaf_cfs_rq_list); in list_add_leaf_cfs_rq()
397 rq->tmp_alone_branch = &rq->leaf_cfs_rq_list; in list_add_leaf_cfs_rq()
407 list_add_rcu(&cfs_rq->leaf_cfs_rq_list, rq->tmp_alone_branch); in list_add_leaf_cfs_rq()
412 rq->tmp_alone_branch = &cfs_rq->leaf_cfs_rq_list; in list_add_leaf_cfs_rq()
418 if (cfs_rq->on_list) { in list_del_leaf_cfs_rq()
425 * to the prev element but it will point to rq->leaf_cfs_rq_list in list_del_leaf_cfs_rq()
428 if (rq->tmp_alone_branch == &cfs_rq->leaf_cfs_rq_list) in list_del_leaf_cfs_rq()
429 rq->tmp_alone_branch = cfs_rq->leaf_cfs_rq_list.prev; in list_del_leaf_cfs_rq()
431 list_del_rcu(&cfs_rq->leaf_cfs_rq_list); in list_del_leaf_cfs_rq()
432 cfs_rq->on_list = 0; in list_del_leaf_cfs_rq()
438 SCHED_WARN_ON(rq->tmp_alone_branch != &rq->leaf_cfs_rq_list); in assert_list_leaf_cfs_rq()
443 list_for_each_entry_safe(cfs_rq, pos, &rq->leaf_cfs_rq_list, \
450 if (se->cfs_rq == pse->cfs_rq) in is_same_group()
451 return se->cfs_rq; in is_same_group()
458 return se->parent; in parent_entity()
474 se_depth = (*se)->depth; in find_matching_se()
475 pse_depth = (*pse)->depth; in find_matching_se()
478 se_depth--; in find_matching_se()
483 pse_depth--; in find_matching_se()
495 return tg->idle > 0; in tg_is_idle()
500 return cfs_rq->idle > 0; in cfs_rq_is_idle()
529 for (cfs_rq = &rq->cfs, pos = NULL; cfs_rq; cfs_rq = pos)
567 s64 delta = (s64)(vruntime - max_vruntime); in max_vruntime()
576 s64 delta = (s64)(vruntime - min_vruntime); in min_vruntime()
586 return (s64)(a->vruntime - b->vruntime) < 0; in entity_before()
594 struct sched_entity *curr = cfs_rq->curr; in update_min_vruntime()
595 struct rb_node *leftmost = rb_first_cached(&cfs_rq->tasks_timeline); in update_min_vruntime()
597 u64 vruntime = cfs_rq->min_vruntime; in update_min_vruntime()
600 if (curr->on_rq) in update_min_vruntime()
601 vruntime = curr->vruntime; in update_min_vruntime()
606 if (leftmost) { /* non-empty tree */ in update_min_vruntime()
610 vruntime = se->vruntime; in update_min_vruntime()
612 vruntime = min_vruntime(vruntime, se->vruntime); in update_min_vruntime()
616 u64_u32_store(cfs_rq->min_vruntime, in update_min_vruntime()
617 max_vruntime(cfs_rq->min_vruntime, vruntime)); in update_min_vruntime()
626 * Enqueue an entity into the rb-tree:
630 rb_add_cached(&se->run_node, &cfs_rq->tasks_timeline, __entity_less); in __enqueue_entity()
635 rb_erase_cached(&se->run_node, &cfs_rq->tasks_timeline); in __dequeue_entity()
640 struct rb_node *left = rb_first_cached(&cfs_rq->tasks_timeline); in __pick_first_entity()
650 struct rb_node *next = rb_next(&se->run_node); in __pick_next_entity()
661 struct rb_node *last = rb_last(&cfs_rq->tasks_timeline.rb_root); in __pick_last_entity()
696 if (unlikely(se->load.weight != NICE_0_LOAD)) in calc_delta_fair()
697 delta = __calc_delta(delta, NICE_0_LOAD, &se->load); in calc_delta_fair()
721 * We calculate the wall-time slice from the period by taking a part
728 unsigned int nr_running = cfs_rq->nr_running; in sched_slice()
734 nr_running = rq_of(cfs_rq)->cfs.h_nr_running; in sched_slice()
736 slice = __sched_period(nr_running + !se->on_rq); in sched_slice()
744 load = &qcfs_rq->load; in sched_slice()
746 if (unlikely(!se->on_rq)) { in sched_slice()
747 lw = qcfs_rq->load; in sched_slice()
749 update_load_add(&lw, se->load.weight); in sched_slice()
752 slice = __calc_delta(slice, se->load.weight, load); in sched_slice()
768 * We calculate the vruntime slice of a to-be-inserted task.
787 struct sched_avg *sa = &se->avg; in init_entity_runnable_average()
798 sa->load_avg = scale_load_down(se->load.weight); in init_entity_runnable_average()
807 * util_avg = cfs_rq->util_avg / (cfs_rq->load_avg + 1) * se.load.weight
816 * util_avg_cap = (cpu_scale - cfs_rq->avg.util_avg) / 2^n
831 struct sched_entity *se = &p->se; in post_init_entity_util_avg()
833 struct sched_avg *sa = &se->avg; in post_init_entity_util_avg()
835 long cap = (long)(cpu_scale - cfs_rq->avg.util_avg) / 2; in post_init_entity_util_avg()
837 if (p->sched_class != &fair_sched_class) { in post_init_entity_util_avg()
848 se->avg.last_update_time = cfs_rq_clock_pelt(cfs_rq); in post_init_entity_util_avg()
853 if (cfs_rq->avg.util_avg != 0) { in post_init_entity_util_avg()
854 sa->util_avg = cfs_rq->avg.util_avg * se->load.weight; in post_init_entity_util_avg()
855 sa->util_avg /= (cfs_rq->avg.load_avg + 1); in post_init_entity_util_avg()
857 if (sa->util_avg > cap) in post_init_entity_util_avg()
858 sa->util_avg = cap; in post_init_entity_util_avg()
860 sa->util_avg = cap; in post_init_entity_util_avg()
864 sa->runnable_avg = sa->util_avg; in post_init_entity_util_avg()
884 struct sched_entity *curr = cfs_rq->curr; in update_curr()
891 delta_exec = now - curr->exec_start; in update_curr()
895 curr->exec_start = now; in update_curr()
901 __schedstat_set(stats->exec_max, in update_curr()
902 max(delta_exec, stats->exec_max)); in update_curr()
905 curr->sum_exec_runtime += delta_exec; in update_curr()
906 schedstat_add(cfs_rq->exec_clock, delta_exec); in update_curr()
908 curr->vruntime += calc_delta_fair(delta_exec, curr); in update_curr()
914 trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime); in update_curr()
924 update_curr(cfs_rq_of(&rq->curr->se)); in update_curr_fair()
957 * maybe already in the runqueue, the se->statistics.wait_start in update_stats_wait_end_fair()
961 if (unlikely(!schedstat_val(stats->wait_start))) in update_stats_wait_end_fair()
988 * Task is being enqueued - update stats:
1000 if (se != cfs_rq->curr) in update_stats_enqueue_fair()
1018 if (se != cfs_rq->curr) in update_stats_dequeue_fair()
1026 state = READ_ONCE(tsk->__state); in update_stats_dequeue_fair()
1028 __schedstat_set(tsk->stats.sleep_start, in update_stats_dequeue_fair()
1031 __schedstat_set(tsk->stats.block_start, in update_stats_dequeue_fair()
1037 * We are picking a new current task - update its stats:
1045 se->exec_start = rq_clock_task(rq_of(cfs_rq)); in update_stats_curr_start()
1094 /* The page with hint page fault latency < threshold in ms is considered hot */
1103 spinlock_t lock; /* nr_tasks, tasks */ member
1123 * ->numa_group (see struct task_struct for locking rules).
1127 return rcu_dereference_check(p->numa_group, p == current || in deref_task_numa_group()
1128 (lockdep_is_held(__rq_lockp(task_rq(p))) && !READ_ONCE(p->on_cpu))); in deref_task_numa_group()
1133 return rcu_dereference_protected(p->numa_group, p == current); in deref_curr_numa_group()
1145 * Calculations based on RSS as non-present and empty pages are skipped in task_nr_scan_windows()
1149 nr_scan_pages = sysctl_numa_balancing_scan_size << (20 - PAGE_SHIFT); in task_nr_scan_windows()
1150 rss = get_mm_rss(p->mm); in task_nr_scan_windows()
1183 ng = rcu_dereference(p->numa_group); in task_scan_start()
1188 period *= refcount_read(&ng->refcount); in task_scan_start()
1213 period *= refcount_read(&ng->refcount); in task_scan_max()
1225 rq->nr_numa_running += (p->numa_preferred_nid != NUMA_NO_NODE); in account_numa_enqueue()
1226 rq->nr_preferred_running += (p->numa_preferred_nid == task_node(p)); in account_numa_enqueue()
1231 rq->nr_numa_running -= (p->numa_preferred_nid != NUMA_NO_NODE); in account_numa_dequeue()
1232 rq->nr_preferred_running -= (p->numa_preferred_nid == task_node(p)); in account_numa_dequeue()
1250 ng = rcu_dereference(p->numa_group); in task_numa_group_id()
1252 gid = ng->gid; in task_numa_group_id()
1271 if (!p->numa_faults) in task_faults()
1274 return p->numa_faults[task_faults_idx(NUMA_MEM, nid, 0)] + in task_faults()
1275 p->numa_faults[task_faults_idx(NUMA_MEM, nid, 1)]; in task_faults()
1285 return ng->faults[task_faults_idx(NUMA_MEM, nid, 0)] + in group_faults()
1286 ng->faults[task_faults_idx(NUMA_MEM, nid, 1)]; in group_faults()
1291 return group->faults[task_faults_idx(NUMA_CPU, nid, 0)] + in group_faults_cpu()
1292 group->faults[task_faults_idx(NUMA_CPU, nid, 1)]; in group_faults_cpu()
1301 faults += ng->faults[task_faults_idx(NUMA_MEM, node, 1)]; in group_faults_priv()
1313 faults += ng->faults[task_faults_idx(NUMA_MEM, node, 0)]; in group_faults_shared()
1321 * considered part of a numa group's pseudo-interleaving set. Migrations
1328 return group_faults_cpu(ng, nid) * ACTIVE_NODE_FRACTION > ng->max_faults_cpu; in numa_is_active_node()
1387 faults *= (max_dist - dist); in score_nearby_nodes()
1388 faults /= (max_dist - LOCAL_DISTANCE); in score_nearby_nodes()
1408 if (!p->numa_faults) in task_weight()
1411 total_faults = p->total_numa_faults; in task_weight()
1431 total_faults = ng->total_faults; in group_weight()
1467 pgdat->node_present_pages >> 4); in pgdat_free_space_enough()
1468 for (z = pgdat->nr_zones - 1; z >= 0; z--) { in pgdat_free_space_enough()
1469 struct zone *zone = pgdat->node_zones + z; in pgdat_free_space_enough()
1486 * hint page fault handler, the hint page fault latency is calculated
1489 * hint page fault latency = hint page fault time - scan time
1491 * The smaller the hint page fault latency, the higher the possibility
1501 return (time - last_time) & PAGE_ACCESS_TIME_MASK; in numa_hint_fault_latency()
1506 * hurt application latency. So we provide a mechanism to rate limit
1518 start = pgdat->nbp_rl_start; in numa_promotion_rate_limit()
1519 if (now - start > MSEC_PER_SEC && in numa_promotion_rate_limit()
1520 cmpxchg(&pgdat->nbp_rl_start, start, now) == start) in numa_promotion_rate_limit()
1521 pgdat->nbp_rl_nr_cand = nr_cand; in numa_promotion_rate_limit()
1522 if (nr_cand - pgdat->nbp_rl_nr_cand >= rate_limit) in numa_promotion_rate_limit()
1538 start = pgdat->nbp_th_start; in numa_promotion_adjust_threshold()
1539 if (now - start > th_period && in numa_promotion_adjust_threshold()
1540 cmpxchg(&pgdat->nbp_th_start, start, now) == start) { in numa_promotion_adjust_threshold()
1544 diff_cand = nr_cand - pgdat->nbp_th_nr_cand; in numa_promotion_adjust_threshold()
1546 th = pgdat->nbp_threshold ? : ref_th; in numa_promotion_adjust_threshold()
1548 th = max(th - unit_th, unit_th); in numa_promotion_adjust_threshold()
1551 pgdat->nbp_th_nr_cand = nr_cand; in numa_promotion_adjust_threshold()
1552 pgdat->nbp_threshold = th; in numa_promotion_adjust_threshold()
1571 unsigned int latency, th, def_th; in should_numa_migrate_memory() local
1576 pgdat->nbp_threshold = 0; in should_numa_migrate_memory()
1582 (20 - PAGE_SHIFT); in should_numa_migrate_memory()
1585 th = pgdat->nbp_threshold ? : def_th; in should_numa_migrate_memory()
1586 latency = numa_hint_fault_latency(page); in should_numa_migrate_memory()
1587 if (latency >= th) in should_numa_migrate_memory()
1594 this_cpupid = cpu_pid_to_cpupid(dst_cpu, current->pid); in should_numa_migrate_memory()
1604 * two full passes of the "multi-stage node selection" test that is in should_numa_migrate_memory()
1607 if ((p->numa_preferred_nid == NUMA_NO_NODE || p->numa_scan_seq <= 4) && in should_numa_migrate_memory()
1612 * Multi-stage node selection is used in conjunction with a periodic in should_numa_migrate_memory()
1613 * migration fault to build a temporal task<->page relation. By using in should_numa_migrate_memory()
1614 * a two-stage filter we remove short/unlikely relations. in should_numa_migrate_memory()
1618 * page (n_t) (in a given time-span) to a probability. in should_numa_migrate_memory()
1626 * act on an unlikely task<->page relation. in should_numa_migrate_memory()
1636 /* A shared fault, but p->numa_group has not been set up yet. */ in should_numa_migrate_memory()
1653 * --------------- * - > --------------- in should_numa_migrate_memory()
1730 struct numa_stats *ns) in numa_classify() argument
1732 if ((ns->nr_running > ns->weight) && in numa_classify()
1733 (((ns->compute_capacity * 100) < (ns->util * imbalance_pct)) || in numa_classify()
1734 ((ns->compute_capacity * imbalance_pct) < (ns->runnable * 100)))) in numa_classify()
1737 if ((ns->nr_running < ns->weight) || in numa_classify()
1738 (((ns->compute_capacity * 100) > (ns->util * imbalance_pct)) && in numa_classify()
1739 ((ns->compute_capacity * imbalance_pct) > (ns->runnable * 100)))) in numa_classify()
1777 struct numa_stats *ns, int nid, in update_numa_stats() argument
1780 int cpu, idle_core = -1; in update_numa_stats()
1782 memset(ns, 0, sizeof(*ns)); in update_numa_stats()
1783 ns->idle_cpu = -1; in update_numa_stats()
1789 ns->load += cpu_load(rq); in update_numa_stats()
1790 ns->runnable += cpu_runnable(rq); in update_numa_stats()
1791 ns->util += cpu_util_cfs(cpu); in update_numa_stats()
1792 ns->nr_running += rq->cfs.h_nr_running; in update_numa_stats()
1793 ns->compute_capacity += capacity_of(cpu); in update_numa_stats()
1795 if (find_idle && !rq->nr_running && idle_cpu(cpu)) { in update_numa_stats()
1796 if (READ_ONCE(rq->numa_migrate_on) || in update_numa_stats()
1797 !cpumask_test_cpu(cpu, env->p->cpus_ptr)) in update_numa_stats()
1800 if (ns->idle_cpu == -1) in update_numa_stats()
1801 ns->idle_cpu = cpu; in update_numa_stats()
1808 ns->weight = cpumask_weight(cpumask_of_node(nid)); in update_numa_stats()
1810 ns->node_type = numa_classify(env->imbalance_pct, ns); in update_numa_stats()
1813 ns->idle_cpu = idle_core; in update_numa_stats()
1819 struct rq *rq = cpu_rq(env->dst_cpu); in task_numa_assign()
1821 /* Check if run-queue part of active NUMA balance. */ in task_numa_assign()
1822 if (env->best_cpu != env->dst_cpu && xchg(&rq->numa_migrate_on, 1)) { in task_numa_assign()
1824 int start = env->dst_cpu; in task_numa_assign()
1827 for_each_cpu_wrap(cpu, cpumask_of_node(env->dst_nid), start) { in task_numa_assign()
1828 if (cpu == env->best_cpu || !idle_cpu(cpu) || in task_numa_assign()
1829 !cpumask_test_cpu(cpu, env->p->cpus_ptr)) { in task_numa_assign()
1833 env->dst_cpu = cpu; in task_numa_assign()
1834 rq = cpu_rq(env->dst_cpu); in task_numa_assign()
1835 if (!xchg(&rq->numa_migrate_on, 1)) in task_numa_assign()
1845 * Clear previous best_cpu/rq numa-migrate flag, since task now in task_numa_assign()
1848 if (env->best_cpu != -1 && env->best_cpu != env->dst_cpu) { in task_numa_assign()
1849 rq = cpu_rq(env->best_cpu); in task_numa_assign()
1850 WRITE_ONCE(rq->numa_migrate_on, 0); in task_numa_assign()
1853 if (env->best_task) in task_numa_assign()
1854 put_task_struct(env->best_task); in task_numa_assign()
1858 env->best_task = p; in task_numa_assign()
1859 env->best_imp = imp; in task_numa_assign()
1860 env->best_cpu = env->dst_cpu; in task_numa_assign()
1874 * ------------ vs --------- in load_too_imbalanced()
1877 src_capacity = env->src_stats.compute_capacity; in load_too_imbalanced()
1878 dst_capacity = env->dst_stats.compute_capacity; in load_too_imbalanced()
1880 imb = abs(dst_load * src_capacity - src_load * dst_capacity); in load_too_imbalanced()
1882 orig_src_load = env->src_stats.load; in load_too_imbalanced()
1883 orig_dst_load = env->dst_stats.load; in load_too_imbalanced()
1885 old_imb = abs(orig_dst_load * src_capacity - orig_src_load * dst_capacity); in load_too_imbalanced()
1907 struct numa_group *cur_ng, *p_ng = deref_curr_numa_group(env->p); in task_numa_compare()
1908 struct rq *dst_rq = cpu_rq(env->dst_cpu); in task_numa_compare()
1912 int dist = env->dist; in task_numa_compare()
1917 if (READ_ONCE(dst_rq->numa_migrate_on)) in task_numa_compare()
1921 cur = rcu_dereference(dst_rq->curr); in task_numa_compare()
1922 if (cur && ((cur->flags & PF_EXITING) || is_idle_task(cur))) in task_numa_compare()
1927 * end try selecting ourselves (current == env->p) as a swap candidate. in task_numa_compare()
1929 if (cur == env->p) { in task_numa_compare()
1935 if (maymove && moveimp >= env->best_imp) in task_numa_compare()
1942 if (!cpumask_test_cpu(env->src_cpu, cur->cpus_ptr)) in task_numa_compare()
1949 if (env->best_task && in task_numa_compare()
1950 env->best_task->numa_preferred_nid == env->src_nid && in task_numa_compare()
1951 cur->numa_preferred_nid != env->src_nid) { in task_numa_compare()
1965 cur_ng = rcu_dereference(cur->numa_group); in task_numa_compare()
1973 if (env->dst_stats.node_type == node_has_spare) in task_numa_compare()
1976 imp = taskimp + task_weight(cur, env->src_nid, dist) - in task_numa_compare()
1977 task_weight(cur, env->dst_nid, dist); in task_numa_compare()
1983 imp -= imp / 16; in task_numa_compare()
1990 imp += group_weight(cur, env->src_nid, dist) - in task_numa_compare()
1991 group_weight(cur, env->dst_nid, dist); in task_numa_compare()
1993 imp += task_weight(cur, env->src_nid, dist) - in task_numa_compare()
1994 task_weight(cur, env->dst_nid, dist); in task_numa_compare()
1998 if (cur->numa_preferred_nid == env->dst_nid) in task_numa_compare()
1999 imp -= imp / 16; in task_numa_compare()
2007 if (cur->numa_preferred_nid == env->src_nid) in task_numa_compare()
2010 if (maymove && moveimp > imp && moveimp > env->best_imp) { in task_numa_compare()
2020 if (env->best_task && cur->numa_preferred_nid == env->src_nid && in task_numa_compare()
2021 env->best_task->numa_preferred_nid != env->src_nid) { in task_numa_compare()
2031 if (imp < SMALLIMP || imp <= env->best_imp + SMALLIMP / 2) in task_numa_compare()
2037 load = task_h_load(env->p) - task_h_load(cur); in task_numa_compare()
2041 dst_load = env->dst_stats.load + load; in task_numa_compare()
2042 src_load = env->src_stats.load - load; in task_numa_compare()
2050 int cpu = env->dst_stats.idle_cpu; in task_numa_compare()
2054 cpu = env->dst_cpu; in task_numa_compare()
2060 if (!idle_cpu(cpu) && env->best_cpu >= 0 && in task_numa_compare()
2061 idle_cpu(env->best_cpu)) { in task_numa_compare()
2062 cpu = env->best_cpu; in task_numa_compare()
2065 env->dst_cpu = cpu; in task_numa_compare()
2075 if (maymove && !cur && env->best_cpu >= 0 && idle_cpu(env->best_cpu)) in task_numa_compare()
2082 if (!maymove && env->best_task && in task_numa_compare()
2083 env->best_task->numa_preferred_nid == env->src_nid) { in task_numa_compare()
2102 if (env->dst_stats.node_type == node_has_spare) { in task_numa_find_cpu()
2112 src_running = env->src_stats.nr_running - 1; in task_numa_find_cpu()
2113 dst_running = env->dst_stats.nr_running + 1; in task_numa_find_cpu()
2114 imbalance = max(0, dst_running - src_running); in task_numa_find_cpu()
2116 env->imb_numa_nr); in task_numa_find_cpu()
2121 if (env->dst_stats.idle_cpu >= 0) { in task_numa_find_cpu()
2122 env->dst_cpu = env->dst_stats.idle_cpu; in task_numa_find_cpu()
2130 * If the improvement from just moving env->p direction is better in task_numa_find_cpu()
2133 load = task_h_load(env->p); in task_numa_find_cpu()
2134 dst_load = env->dst_stats.load + load; in task_numa_find_cpu()
2135 src_load = env->src_stats.load - load; in task_numa_find_cpu()
2139 for_each_cpu(cpu, cpumask_of_node(env->dst_nid)) { in task_numa_find_cpu()
2141 if (!cpumask_test_cpu(cpu, env->p->cpus_ptr)) in task_numa_find_cpu()
2144 env->dst_cpu = cpu; in task_numa_find_cpu()
2162 .best_cpu = -1, in task_numa_migrate()
2176 * random movement of tasks -- counter the numa conditions we're trying in task_numa_migrate()
2182 env.imbalance_pct = 100 + (sd->imbalance_pct - 100) / 2; in task_numa_migrate()
2183 env.imb_numa_nr = sd->imb_numa_nr; in task_numa_migrate()
2195 return -EINVAL; in task_numa_migrate()
2198 env.dst_nid = p->numa_preferred_nid; in task_numa_migrate()
2203 taskimp = task_weight(p, env.dst_nid, dist) - taskweight; in task_numa_migrate()
2204 groupimp = group_weight(p, env.dst_nid, dist) - groupweight; in task_numa_migrate()
2212 * - there is no space available on the preferred_nid in task_numa_migrate()
2213 * - the task is part of a numa_group that is interleaved across in task_numa_migrate()
2218 if (env.best_cpu == -1 || (ng && ng->active_nodes > 1)) { in task_numa_migrate()
2220 if (nid == env.src_nid || nid == p->numa_preferred_nid) in task_numa_migrate()
2231 taskimp = task_weight(p, nid, dist) - taskweight; in task_numa_migrate()
2232 groupimp = group_weight(p, nid, dist) - groupweight; in task_numa_migrate()
2252 if (env.best_cpu == -1) in task_numa_migrate()
2257 if (nid != p->numa_preferred_nid) in task_numa_migrate()
2262 if (env.best_cpu == -1) { in task_numa_migrate()
2263 trace_sched_stick_numa(p, env.src_cpu, NULL, -1); in task_numa_migrate()
2264 return -EAGAIN; in task_numa_migrate()
2270 WRITE_ONCE(best_rq->numa_migrate_on, 0); in task_numa_migrate()
2277 WRITE_ONCE(best_rq->numa_migrate_on, 0); in task_numa_migrate()
2291 if (unlikely(p->numa_preferred_nid == NUMA_NO_NODE || !p->numa_faults)) in numa_migrate_preferred()
2295 interval = min(interval, msecs_to_jiffies(p->numa_scan_period) / 16); in numa_migrate_preferred()
2296 p->numa_migrate_retry = jiffies + interval; in numa_migrate_preferred()
2299 if (task_node(p) == p->numa_preferred_nid) in numa_migrate_preferred()
2329 numa_group->max_faults_cpu = max_faults; in numa_group_count_active_nodes()
2330 numa_group->active_nodes = active_nodes; in numa_group_count_active_nodes()
2356 unsigned long remote = p->numa_faults_locality[0]; in update_task_scan_period()
2357 unsigned long local = p->numa_faults_locality[1]; in update_task_scan_period()
2366 if (local + shared == 0 || p->numa_faults_locality[2]) { in update_task_scan_period()
2367 p->numa_scan_period = min(p->numa_scan_period_max, in update_task_scan_period()
2368 p->numa_scan_period << 1); in update_task_scan_period()
2370 p->mm->numa_next_scan = jiffies + in update_task_scan_period()
2371 msecs_to_jiffies(p->numa_scan_period); in update_task_scan_period()
2382 period_slot = DIV_ROUND_UP(p->numa_scan_period, NUMA_PERIOD_SLOTS); in update_task_scan_period()
2391 int slot = ps_ratio - NUMA_PERIOD_THRESHOLD; in update_task_scan_period()
2401 int slot = lr_ratio - NUMA_PERIOD_THRESHOLD; in update_task_scan_period()
2407 * Private memory faults exceed (SLOTS-THRESHOLD)/SLOTS, in update_task_scan_period()
2412 diff = -(NUMA_PERIOD_THRESHOLD - ratio) * period_slot; in update_task_scan_period()
2415 p->numa_scan_period = clamp(p->numa_scan_period + diff, in update_task_scan_period()
2417 memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); in update_task_scan_period()
2424 * from the dozens-of-seconds NUMA balancing period. Use the scheduler
2431 now = p->se.exec_start; in numa_get_avg_runtime()
2432 runtime = p->se.sum_exec_runtime; in numa_get_avg_runtime()
2434 if (p->last_task_numa_placement) { in numa_get_avg_runtime()
2435 delta = runtime - p->last_sum_exec_runtime; in numa_get_avg_runtime()
2436 *period = now - p->last_task_numa_placement; in numa_get_avg_runtime()
2442 delta = p->se.avg.load_sum; in numa_get_avg_runtime()
2446 p->last_sum_exec_runtime = runtime; in numa_get_avg_runtime()
2447 p->last_task_numa_placement = now; in numa_get_avg_runtime()
2497 for (dist = sched_max_numa_distance; dist > LOCAL_DISTANCE; dist--) { in preferred_group_nid()
2551 * The p->mm->numa_scan_seq field gets updated without in task_numa_placement()
2555 seq = READ_ONCE(p->mm->numa_scan_seq); in task_numa_placement()
2556 if (p->numa_scan_seq == seq) in task_numa_placement()
2558 p->numa_scan_seq = seq; in task_numa_placement()
2559 p->numa_scan_period_max = task_scan_max(p); in task_numa_placement()
2561 total_faults = p->numa_faults_locality[0] + in task_numa_placement()
2562 p->numa_faults_locality[1]; in task_numa_placement()
2568 group_lock = &ng->lock; in task_numa_placement()
2588 diff = p->numa_faults[membuf_idx] - p->numa_faults[mem_idx] / 2; in task_numa_placement()
2589 fault_types[priv] += p->numa_faults[membuf_idx]; in task_numa_placement()
2590 p->numa_faults[membuf_idx] = 0; in task_numa_placement()
2596 * little over-all impact on throughput, and thus their in task_numa_placement()
2600 f_weight = (f_weight * p->numa_faults[cpubuf_idx]) / in task_numa_placement()
2602 f_diff = f_weight - p->numa_faults[cpu_idx] / 2; in task_numa_placement()
2603 p->numa_faults[cpubuf_idx] = 0; in task_numa_placement()
2605 p->numa_faults[mem_idx] += diff; in task_numa_placement()
2606 p->numa_faults[cpu_idx] += f_diff; in task_numa_placement()
2607 faults += p->numa_faults[mem_idx]; in task_numa_placement()
2608 p->total_numa_faults += diff; in task_numa_placement()
2617 ng->faults[mem_idx] += diff; in task_numa_placement()
2618 ng->faults[cpu_idx] += f_diff; in task_numa_placement()
2619 ng->total_faults += diff; in task_numa_placement()
2620 group_faults += ng->faults[mem_idx]; in task_numa_placement()
2635 /* Cannot migrate task to CPU-less node */ in task_numa_placement()
2658 if (max_nid != p->numa_preferred_nid) in task_numa_placement()
2667 return refcount_inc_not_zero(&grp->refcount); in get_numa_group()
2672 if (refcount_dec_and_test(&grp->refcount)) in put_numa_group()
2694 refcount_set(&grp->refcount, 1); in task_numa_group()
2695 grp->active_nodes = 1; in task_numa_group()
2696 grp->max_faults_cpu = 0; in task_numa_group()
2697 spin_lock_init(&grp->lock); in task_numa_group()
2698 grp->gid = p->pid; in task_numa_group()
2701 grp->faults[i] = p->numa_faults[i]; in task_numa_group()
2703 grp->total_faults = p->total_numa_faults; in task_numa_group()
2705 grp->nr_tasks++; in task_numa_group()
2706 rcu_assign_pointer(p->numa_group, grp); in task_numa_group()
2710 tsk = READ_ONCE(cpu_rq(cpu)->curr); in task_numa_group()
2715 grp = rcu_dereference(tsk->numa_group); in task_numa_group()
2727 if (my_grp->nr_tasks > grp->nr_tasks) in task_numa_group()
2731 * Tie-break on the grp address. in task_numa_group()
2733 if (my_grp->nr_tasks == grp->nr_tasks && my_grp > grp) in task_numa_group()
2737 if (tsk->mm == current->mm) in task_numa_group()
2756 double_lock_irq(&my_grp->lock, &grp->lock); in task_numa_group()
2759 my_grp->faults[i] -= p->numa_faults[i]; in task_numa_group()
2760 grp->faults[i] += p->numa_faults[i]; in task_numa_group()
2762 my_grp->total_faults -= p->total_numa_faults; in task_numa_group()
2763 grp->total_faults += p->total_numa_faults; in task_numa_group()
2765 my_grp->nr_tasks--; in task_numa_group()
2766 grp->nr_tasks++; in task_numa_group()
2768 spin_unlock(&my_grp->lock); in task_numa_group()
2769 spin_unlock_irq(&grp->lock); in task_numa_group()
2771 rcu_assign_pointer(p->numa_group, grp); in task_numa_group()
2786 * reset the data back to default state without freeing ->numa_faults.
2791 struct numa_group *grp = rcu_dereference_raw(p->numa_group); in task_numa_free()
2792 unsigned long *numa_faults = p->numa_faults; in task_numa_free()
2800 spin_lock_irqsave(&grp->lock, flags); in task_numa_free()
2802 grp->faults[i] -= p->numa_faults[i]; in task_numa_free()
2803 grp->total_faults -= p->total_numa_faults; in task_numa_free()
2805 grp->nr_tasks--; in task_numa_free()
2806 spin_unlock_irqrestore(&grp->lock, flags); in task_numa_free()
2807 RCU_INIT_POINTER(p->numa_group, NULL); in task_numa_free()
2812 p->numa_faults = NULL; in task_numa_free()
2815 p->total_numa_faults = 0; in task_numa_free()
2837 if (!p->mm) in task_numa_fault()
2849 /* Allocate buffer to track faults on a per-node basis */ in task_numa_fault()
2850 if (unlikely(!p->numa_faults)) { in task_numa_fault()
2851 int size = sizeof(*p->numa_faults) * in task_numa_fault()
2854 p->numa_faults = kzalloc(size, GFP_KERNEL|__GFP_NOWARN); in task_numa_fault()
2855 if (!p->numa_faults) in task_numa_fault()
2858 p->total_numa_faults = 0; in task_numa_fault()
2859 memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); in task_numa_fault()
2866 if (unlikely(last_cpupid == (-1 & LAST_CPUPID_MASK))) { in task_numa_fault()
2881 if (!priv && !local && ng && ng->active_nodes > 1 && in task_numa_fault()
2890 if (time_after(jiffies, p->numa_migrate_retry)) { in task_numa_fault()
2896 p->numa_pages_migrated += pages; in task_numa_fault()
2898 p->numa_faults_locality[2] += pages; in task_numa_fault()
2900 p->numa_faults[task_faults_idx(NUMA_MEMBUF, mem_node, priv)] += pages; in task_numa_fault()
2901 p->numa_faults[task_faults_idx(NUMA_CPUBUF, cpu_node, priv)] += pages; in task_numa_fault()
2902 p->numa_faults_locality[local] += pages; in task_numa_fault()
2909 * p->mm->numa_scan_seq is written to without exclusive access in reset_ptenuma_scan()
2915 WRITE_ONCE(p->mm->numa_scan_seq, READ_ONCE(p->mm->numa_scan_seq) + 1); in reset_ptenuma_scan()
2916 p->mm->numa_scan_offset = 0; in reset_ptenuma_scan()
2927 struct mm_struct *mm = p->mm; in task_numa_work()
2928 u64 runtime = p->se.sum_exec_runtime; in task_numa_work()
2929 MA_STATE(mas, &mm->mm_mt, 0, 0); in task_numa_work()
2937 work->next = work; in task_numa_work()
2941 * NOTE: make sure not to dereference p->mm before this check, in task_numa_work()
2943 * without p->mm even though we still had it when we enqueued this in task_numa_work()
2946 if (p->flags & PF_EXITING) in task_numa_work()
2949 if (!mm->numa_next_scan) { in task_numa_work()
2950 mm->numa_next_scan = now + in task_numa_work()
2957 migrate = mm->numa_next_scan; in task_numa_work()
2961 if (p->numa_scan_period == 0) { in task_numa_work()
2962 p->numa_scan_period_max = task_scan_max(p); in task_numa_work()
2963 p->numa_scan_period = task_scan_start(p); in task_numa_work()
2966 next_scan = now + msecs_to_jiffies(p->numa_scan_period); in task_numa_work()
2967 if (cmpxchg(&mm->numa_next_scan, migrate, next_scan) != migrate) in task_numa_work()
2974 p->node_stamp += 2 * TICK_NSEC; in task_numa_work()
2976 start = mm->numa_scan_offset; in task_numa_work()
2978 pages <<= 20 - PAGE_SHIFT; /* MB in pages */ in task_numa_work()
2997 is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_MIXEDMAP)) { in task_numa_work()
3004 * hinting faults in read-only file-backed mappings or the vdso in task_numa_work()
3007 if (!vma->vm_mm || in task_numa_work()
3008 (vma->vm_file && (vma->vm_flags & (VM_READ|VM_WRITE)) == (VM_READ))) in task_numa_work()
3019 start = max(start, vma->vm_start); in task_numa_work()
3021 end = min(end, vma->vm_end); in task_numa_work()
3027 * is not already pte-numa. If the VMA contains in task_numa_work()
3033 pages -= (end - start) >> PAGE_SHIFT; in task_numa_work()
3034 virtpages -= (end - start) >> PAGE_SHIFT; in task_numa_work()
3041 } while (end != vma->vm_end); in task_numa_work()
3052 mm->numa_scan_offset = start; in task_numa_work()
3063 if (unlikely(p->se.sum_exec_runtime != runtime)) { in task_numa_work()
3064 u64 diff = p->se.sum_exec_runtime - runtime; in task_numa_work()
3065 p->node_stamp += 32 * diff; in task_numa_work()
3072 struct mm_struct *mm = p->mm; in init_numa_balancing()
3075 mm_users = atomic_read(&mm->mm_users); in init_numa_balancing()
3077 mm->numa_next_scan = jiffies + msecs_to_jiffies(sysctl_numa_balancing_scan_delay); in init_numa_balancing()
3078 mm->numa_scan_seq = 0; in init_numa_balancing()
3081 p->node_stamp = 0; in init_numa_balancing()
3082 p->numa_scan_seq = mm ? mm->numa_scan_seq : 0; in init_numa_balancing()
3083 p->numa_scan_period = sysctl_numa_balancing_scan_delay; in init_numa_balancing()
3084 p->numa_migrate_retry = 0; in init_numa_balancing()
3086 p->numa_work.next = &p->numa_work; in init_numa_balancing()
3087 p->numa_faults = NULL; in init_numa_balancing()
3088 p->numa_pages_migrated = 0; in init_numa_balancing()
3089 p->total_numa_faults = 0; in init_numa_balancing()
3090 RCU_INIT_POINTER(p->numa_group, NULL); in init_numa_balancing()
3091 p->last_task_numa_placement = 0; in init_numa_balancing()
3092 p->last_sum_exec_runtime = 0; in init_numa_balancing()
3094 init_task_work(&p->numa_work, task_numa_work); in init_numa_balancing()
3098 p->numa_preferred_nid = NUMA_NO_NODE; in init_numa_balancing()
3110 current->numa_scan_period * mm_users * NSEC_PER_MSEC); in init_numa_balancing()
3112 p->node_stamp = delay; in init_numa_balancing()
3121 struct callback_head *work = &curr->numa_work; in task_tick_numa()
3127 if (!curr->mm || (curr->flags & (PF_EXITING | PF_KTHREAD)) || work->next != work) in task_tick_numa()
3136 now = curr->se.sum_exec_runtime; in task_tick_numa()
3137 period = (u64)curr->numa_scan_period * NSEC_PER_MSEC; in task_tick_numa()
3139 if (now > curr->node_stamp + period) { in task_tick_numa()
3140 if (!curr->node_stamp) in task_tick_numa()
3141 curr->numa_scan_period = task_scan_start(curr); in task_tick_numa()
3142 curr->node_stamp += period; in task_tick_numa()
3144 if (!time_before(jiffies, curr->mm->numa_next_scan)) in task_tick_numa()
3157 if (!p->mm || !p->numa_faults || (p->flags & PF_EXITING)) in update_scan_period()
3166 * is pulled cross-node due to wakeups or load balancing. in update_scan_period()
3168 if (p->numa_scan_seq) { in update_scan_period()
3174 if (dst_nid == p->numa_preferred_nid || in update_scan_period()
3175 (p->numa_preferred_nid != NUMA_NO_NODE && in update_scan_period()
3176 src_nid != p->numa_preferred_nid)) in update_scan_period()
3180 p->numa_scan_period = task_scan_start(p); in update_scan_period()
3205 update_load_add(&cfs_rq->load, se->load.weight); in account_entity_enqueue()
3211 list_add(&se->group_node, &rq->cfs_tasks); in account_entity_enqueue()
3214 cfs_rq->nr_running++; in account_entity_enqueue()
3216 cfs_rq->idle_nr_running++; in account_entity_enqueue()
3222 update_load_sub(&cfs_rq->load, se->load.weight); in account_entity_dequeue()
3226 list_del_init(&se->group_node); in account_entity_dequeue()
3229 cfs_rq->nr_running--; in account_entity_dequeue()
3231 cfs_rq->idle_nr_running--; in account_entity_dequeue()
3237 * Explicitly do a load-store to ensure the intermediate value never hits
3257 * Explicitly do a load-store to ensure the intermediate value never hits
3265 res = var - val; \
3274 * A variant of sub_positive(), which does not use explicit load-store
3279 *ptr -= min_t(typeof(*ptr), *ptr, _val); \
3286 cfs_rq->avg.load_avg += se->avg.load_avg; in enqueue_load_avg()
3287 cfs_rq->avg.load_sum += se_weight(se) * se->avg.load_sum; in enqueue_load_avg()
3293 sub_positive(&cfs_rq->avg.load_avg, se->avg.load_avg); in dequeue_load_avg()
3294 sub_positive(&cfs_rq->avg.load_sum, se_weight(se) * se->avg.load_sum); in dequeue_load_avg()
3296 cfs_rq->avg.load_sum = max_t(u32, cfs_rq->avg.load_sum, in dequeue_load_avg()
3297 cfs_rq->avg.load_avg * PELT_MIN_DIVIDER); in dequeue_load_avg()
3309 if (se->on_rq) { in reweight_entity()
3311 if (cfs_rq->curr == se) in reweight_entity()
3313 update_load_sub(&cfs_rq->load, se->load.weight); in reweight_entity()
3317 update_load_set(&se->load, weight); in reweight_entity()
3321 u32 divider = get_pelt_divider(&se->avg); in reweight_entity()
3323 se->avg.load_avg = div_u64(se_weight(se) * se->avg.load_sum, divider); in reweight_entity()
3328 if (se->on_rq) in reweight_entity()
3329 update_load_add(&cfs_rq->load, se->load.weight); in reweight_entity()
3335 struct sched_entity *se = &p->se; in reweight_task()
3337 struct load_weight *load = &se->load; in reweight_task()
3341 load->inv_weight = sched_prio_to_wmult[prio]; in reweight_task()
3355 * tg->weight * grq->load.weight
3356 * ge->load.weight = ----------------------------- (1)
3357 * \Sum grq->load.weight
3365 * grq->load.weight -> grq->avg.load_avg (2)
3369 * tg->weight * grq->avg.load_avg
3370 * ge->load.weight = ------------------------------ (3)
3371 * tg->load_avg
3373 * Where: tg->load_avg ~= \Sum grq->avg.load_avg
3377 * The problem with it is that because the average is slow -- it was designed
3378 * to be exactly that of course -- this leads to transients in boundary
3380 * one task. It takes time for our CPU's grq->avg.load_avg to build up,
3381 * yielding bad latency etc..
3385 * tg->weight * grq->load.weight
3386 * ge->load.weight = ----------------------------- = tg->weight (4)
3387 * grp->load.weight
3394 * ge->load.weight =
3396 * tg->weight * grq->load.weight
3397 * --------------------------------------------------- (5)
3398 * tg->load_avg - grq->avg.load_avg + grq->load.weight
3400 * But because grq->load.weight can drop to 0, resulting in a divide by zero,
3401 * we need to use grq->avg.load_avg as its lower bound, which then gives:
3404 * tg->weight * grq->load.weight
3405 * ge->load.weight = ----------------------------- (6)
3410 * tg_load_avg' = tg->load_avg - grq->avg.load_avg +
3411 * max(grq->load.weight, grq->avg.load_avg)
3415 * overestimates the ge->load.weight and therefore:
3417 * \Sum ge->load.weight >= tg->weight
3424 struct task_group *tg = cfs_rq->tg; in calc_group_shares()
3426 tg_shares = READ_ONCE(tg->shares); in calc_group_shares()
3428 load = max(scale_load_down(cfs_rq->load.weight), cfs_rq->avg.load_avg); in calc_group_shares()
3430 tg_weight = atomic_long_read(&tg->load_avg); in calc_group_shares()
3433 tg_weight -= cfs_rq->tg_load_avg_contrib; in calc_group_shares()
3441 * MIN_SHARES has to be unscaled here to support per-CPU partitioning in calc_group_shares()
3442 * of a group with small tg->shares value. It is a floor value which is in calc_group_shares()
3446 * E.g. on 64-bit for a group with tg->shares of scale_load(15)=15*1024 in calc_group_shares()
3447 * on an 8-core system with 8 tasks each runnable on one CPU shares has in calc_group_shares()
3472 shares = READ_ONCE(gcfs_rq->tg->shares); in update_cfs_group()
3474 if (likely(se->load.weight == shares)) in update_cfs_group()
3493 if (&rq->cfs == cfs_rq) { in cfs_rq_util_change()
3503 * As is, the util number is not freq-invariant (we'd have to in cfs_rq_util_change()
3515 if (sa->load_sum) in load_avg_is_decayed()
3518 if (sa->util_sum) in load_avg_is_decayed()
3521 if (sa->runnable_sum) in load_avg_is_decayed()
3529 SCHED_WARN_ON(sa->load_avg || in load_avg_is_decayed()
3530 sa->util_avg || in load_avg_is_decayed()
3531 sa->runnable_avg); in load_avg_is_decayed()
3538 return u64_u32_load_copy(cfs_rq->avg.last_update_time, in cfs_rq_last_update_time()
3539 cfs_rq->last_update_time_copy); in cfs_rq_last_update_time()
3545 * bottom-up, we only have to test whether the cfs_rq before us on the list
3555 if (cfs_rq->on_list) { in child_cfs_rq_on_list()
3556 prev = cfs_rq->leaf_cfs_rq_list.prev; in child_cfs_rq_on_list()
3560 prev = rq->tmp_alone_branch; in child_cfs_rq_on_list()
3565 return (prev_cfs_rq->tg->parent == cfs_rq->tg); in child_cfs_rq_on_list()
3570 if (cfs_rq->load.weight) in cfs_rq_is_decayed()
3573 if (!load_avg_is_decayed(&cfs_rq->avg)) in cfs_rq_is_decayed()
3583 * update_tg_load_avg - update the tg's load avg
3586 * This function 'ensures': tg->load_avg := \Sum tg->cfs_rq[]->avg.load.
3587 * However, because tg->load_avg is a global value there are performance
3598 long delta = cfs_rq->avg.load_avg - cfs_rq->tg_load_avg_contrib; in update_tg_load_avg()
3603 if (cfs_rq->tg == &root_task_group) in update_tg_load_avg()
3606 if (abs(delta) > cfs_rq->tg_load_avg_contrib / 64) { in update_tg_load_avg()
3607 atomic_long_add(delta, &cfs_rq->tg->load_avg); in update_tg_load_avg()
3608 cfs_rq->tg_load_avg_contrib = cfs_rq->avg.load_avg; in update_tg_load_avg()
3614 * caller only guarantees p->pi_lock is held; no other assumptions,
3615 * including the state of rq->lock, should be made.
3629 * getting what current time is, so simply throw away the out-of-date in set_task_rq_fair()
3633 if (!(se->avg.last_update_time && prev)) in set_task_rq_fair()
3640 se->avg.last_update_time = n_last_update_time; in set_task_rq_fair()
3648 * ge->avg == grq->avg (1)
3659 * ge->avg.load_avg = ge->load.weight * ge->avg.runnable_avg (2)
3664 * grq->avg.load_avg = grq->load.weight * grq->avg.runnable_avg (3)
3668 * ge->avg.runnable_avg == grq->avg.runnable_avg
3672 * ge->load.weight * grq->avg.load_avg
3673 * ge->avg.load_avg = ----------------------------------- (4)
3674 * grq->load.weight
3687 * Another reason this doesn't work is that runnable isn't a 0-sum entity.
3698 * ge->avg.running_sum <= ge->avg.runnable_sum <= LOAD_AVG_MAX
3705 * grq->avg.runnable_sum = grq->avg.load_sum / grq->load.weight
3713 long delta_sum, delta_avg = gcfs_rq->avg.util_avg - se->avg.util_avg; in update_tg_cfs_util()
3721 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in update_tg_cfs_util()
3724 divider = get_pelt_divider(&cfs_rq->avg); in update_tg_cfs_util()
3728 se->avg.util_avg = gcfs_rq->avg.util_avg; in update_tg_cfs_util()
3729 new_sum = se->avg.util_avg * divider; in update_tg_cfs_util()
3730 delta_sum = (long)new_sum - (long)se->avg.util_sum; in update_tg_cfs_util()
3731 se->avg.util_sum = new_sum; in update_tg_cfs_util()
3734 add_positive(&cfs_rq->avg.util_avg, delta_avg); in update_tg_cfs_util()
3735 add_positive(&cfs_rq->avg.util_sum, delta_sum); in update_tg_cfs_util()
3738 cfs_rq->avg.util_sum = max_t(u32, cfs_rq->avg.util_sum, in update_tg_cfs_util()
3739 cfs_rq->avg.util_avg * PELT_MIN_DIVIDER); in update_tg_cfs_util()
3745 long delta_sum, delta_avg = gcfs_rq->avg.runnable_avg - se->avg.runnable_avg; in update_tg_cfs_runnable()
3753 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in update_tg_cfs_runnable()
3756 divider = get_pelt_divider(&cfs_rq->avg); in update_tg_cfs_runnable()
3759 se->avg.runnable_avg = gcfs_rq->avg.runnable_avg; in update_tg_cfs_runnable()
3760 new_sum = se->avg.runnable_avg * divider; in update_tg_cfs_runnable()
3761 delta_sum = (long)new_sum - (long)se->avg.runnable_sum; in update_tg_cfs_runnable()
3762 se->avg.runnable_sum = new_sum; in update_tg_cfs_runnable()
3765 add_positive(&cfs_rq->avg.runnable_avg, delta_avg); in update_tg_cfs_runnable()
3766 add_positive(&cfs_rq->avg.runnable_sum, delta_sum); in update_tg_cfs_runnable()
3768 cfs_rq->avg.runnable_sum = max_t(u32, cfs_rq->avg.runnable_sum, in update_tg_cfs_runnable()
3769 cfs_rq->avg.runnable_avg * PELT_MIN_DIVIDER); in update_tg_cfs_runnable()
3775 long delta_avg, running_sum, runnable_sum = gcfs_rq->prop_runnable_sum; in update_tg_cfs_load()
3784 gcfs_rq->prop_runnable_sum = 0; in update_tg_cfs_load()
3787 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in update_tg_cfs_load()
3790 divider = get_pelt_divider(&cfs_rq->avg); in update_tg_cfs_load()
3797 runnable_sum += se->avg.load_sum; in update_tg_cfs_load()
3804 if (scale_load_down(gcfs_rq->load.weight)) { in update_tg_cfs_load()
3805 load_sum = div_u64(gcfs_rq->avg.load_sum, in update_tg_cfs_load()
3806 scale_load_down(gcfs_rq->load.weight)); in update_tg_cfs_load()
3810 runnable_sum = min(se->avg.load_sum, load_sum); in update_tg_cfs_load()
3819 running_sum = se->avg.util_sum >> SCHED_CAPACITY_SHIFT; in update_tg_cfs_load()
3825 delta_avg = load_avg - se->avg.load_avg; in update_tg_cfs_load()
3829 delta_sum = load_sum - (s64)se_weight(se) * se->avg.load_sum; in update_tg_cfs_load()
3831 se->avg.load_sum = runnable_sum; in update_tg_cfs_load()
3832 se->avg.load_avg = load_avg; in update_tg_cfs_load()
3833 add_positive(&cfs_rq->avg.load_avg, delta_avg); in update_tg_cfs_load()
3834 add_positive(&cfs_rq->avg.load_sum, delta_sum); in update_tg_cfs_load()
3836 cfs_rq->avg.load_sum = max_t(u32, cfs_rq->avg.load_sum, in update_tg_cfs_load()
3837 cfs_rq->avg.load_avg * PELT_MIN_DIVIDER); in update_tg_cfs_load()
3842 cfs_rq->propagate = 1; in add_tg_cfs_propagate()
3843 cfs_rq->prop_runnable_sum += runnable_sum; in add_tg_cfs_propagate()
3855 if (!gcfs_rq->propagate) in propagate_entity_load_avg()
3858 gcfs_rq->propagate = 0; in propagate_entity_load_avg()
3862 add_tg_cfs_propagate(cfs_rq, gcfs_rq->prop_runnable_sum); in propagate_entity_load_avg()
3886 if (se->avg.load_avg || se->avg.util_avg) in skip_blocked_update()
3893 if (gcfs_rq->propagate) in skip_blocked_update()
3925 if (load_avg_is_decayed(&se->avg)) in migrate_se_pelt_lag()
3932 is_idle = is_idle_task(rcu_dereference(rq->curr)); in migrate_se_pelt_lag()
3949 * - cfs->throttled_clock_pelt_time@cfs_rq_idle in migrate_se_pelt_lag()
3952 * = rq_clock_pelt()@rq_idle - rq_clock_pelt()@cfs_rq_idle in migrate_se_pelt_lag()
3955 * = sched_clock_cpu() - rq_clock()@rq_idle in migrate_se_pelt_lag()
3959 * now = rq_clock_pelt()@rq_idle - cfs->throttled_clock_pelt_time + in migrate_se_pelt_lag()
3960 * sched_clock_cpu() - rq_clock()@rq_idle in migrate_se_pelt_lag()
3962 * rq_clock_pelt()@rq_idle is rq->clock_pelt_idle in migrate_se_pelt_lag()
3963 * rq_clock()@rq_idle is rq->clock_idle in migrate_se_pelt_lag()
3964 * cfs->throttled_clock_pelt_time@cfs_rq_idle in migrate_se_pelt_lag()
3965 * is cfs_rq->throttled_pelt_idle in migrate_se_pelt_lag()
3969 throttled = u64_u32_load(cfs_rq->throttled_pelt_idle); in migrate_se_pelt_lag()
3974 now = u64_u32_load(rq->clock_pelt_idle); in migrate_se_pelt_lag()
3984 now -= throttled; in migrate_se_pelt_lag()
3987 * cfs_rq->avg.last_update_time is more recent than our in migrate_se_pelt_lag()
3992 now += sched_clock_cpu(cpu_of(rq)) - u64_u32_load(rq->clock_idle); in migrate_se_pelt_lag()
4001 * update_cfs_rq_load_avg - update the cfs_rq's load/util averages
4008 * cfs_rq->avg is used for task_h_load() and update_cfs_share() for example.
4012 * Since both these conditions indicate a changed cfs_rq->avg.load we should
4019 struct sched_avg *sa = &cfs_rq->avg; in update_cfs_rq_load_avg()
4022 if (cfs_rq->removed.nr) { in update_cfs_rq_load_avg()
4024 u32 divider = get_pelt_divider(&cfs_rq->avg); in update_cfs_rq_load_avg()
4026 raw_spin_lock(&cfs_rq->removed.lock); in update_cfs_rq_load_avg()
4027 swap(cfs_rq->removed.util_avg, removed_util); in update_cfs_rq_load_avg()
4028 swap(cfs_rq->removed.load_avg, removed_load); in update_cfs_rq_load_avg()
4029 swap(cfs_rq->removed.runnable_avg, removed_runnable); in update_cfs_rq_load_avg()
4030 cfs_rq->removed.nr = 0; in update_cfs_rq_load_avg()
4031 raw_spin_unlock(&cfs_rq->removed.lock); in update_cfs_rq_load_avg()
4034 sub_positive(&sa->load_avg, r); in update_cfs_rq_load_avg()
4035 sub_positive(&sa->load_sum, r * divider); in update_cfs_rq_load_avg()
4036 /* See sa->util_sum below */ in update_cfs_rq_load_avg()
4037 sa->load_sum = max_t(u32, sa->load_sum, sa->load_avg * PELT_MIN_DIVIDER); in update_cfs_rq_load_avg()
4040 sub_positive(&sa->util_avg, r); in update_cfs_rq_load_avg()
4041 sub_positive(&sa->util_sum, r * divider); in update_cfs_rq_load_avg()
4043 * Because of rounding, se->util_sum might ends up being +1 more than in update_cfs_rq_load_avg()
4044 * cfs->util_sum. Although this is not a problem by itself, detaching in update_cfs_rq_load_avg()
4046 * util_avg (~1ms) can make cfs->util_sum becoming null whereas in update_cfs_rq_load_avg()
4053 sa->util_sum = max_t(u32, sa->util_sum, sa->util_avg * PELT_MIN_DIVIDER); in update_cfs_rq_load_avg()
4056 sub_positive(&sa->runnable_avg, r); in update_cfs_rq_load_avg()
4057 sub_positive(&sa->runnable_sum, r * divider); in update_cfs_rq_load_avg()
4058 /* See sa->util_sum above */ in update_cfs_rq_load_avg()
4059 sa->runnable_sum = max_t(u32, sa->runnable_sum, in update_cfs_rq_load_avg()
4060 sa->runnable_avg * PELT_MIN_DIVIDER); in update_cfs_rq_load_avg()
4067 -(long)(removed_runnable * divider) >> SCHED_CAPACITY_SHIFT); in update_cfs_rq_load_avg()
4073 u64_u32_store_copy(sa->last_update_time, in update_cfs_rq_load_avg()
4074 cfs_rq->last_update_time_copy, in update_cfs_rq_load_avg()
4075 sa->last_update_time); in update_cfs_rq_load_avg()
4080 * attach_entity_load_avg - attach this entity to its cfs_rq load avg
4085 * cfs_rq->avg.last_update_time being current.
4090 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in attach_entity_load_avg()
4093 u32 divider = get_pelt_divider(&cfs_rq->avg); in attach_entity_load_avg()
4102 se->avg.last_update_time = cfs_rq->avg.last_update_time; in attach_entity_load_avg()
4103 se->avg.period_contrib = cfs_rq->avg.period_contrib; in attach_entity_load_avg()
4111 se->avg.util_sum = se->avg.util_avg * divider; in attach_entity_load_avg()
4113 se->avg.runnable_sum = se->avg.runnable_avg * divider; in attach_entity_load_avg()
4115 se->avg.load_sum = se->avg.load_avg * divider; in attach_entity_load_avg()
4116 if (se_weight(se) < se->avg.load_sum) in attach_entity_load_avg()
4117 se->avg.load_sum = div_u64(se->avg.load_sum, se_weight(se)); in attach_entity_load_avg()
4119 se->avg.load_sum = 1; in attach_entity_load_avg()
4122 cfs_rq->avg.util_avg += se->avg.util_avg; in attach_entity_load_avg()
4123 cfs_rq->avg.util_sum += se->avg.util_sum; in attach_entity_load_avg()
4124 cfs_rq->avg.runnable_avg += se->avg.runnable_avg; in attach_entity_load_avg()
4125 cfs_rq->avg.runnable_sum += se->avg.runnable_sum; in attach_entity_load_avg()
4127 add_tg_cfs_propagate(cfs_rq, se->avg.load_sum); in attach_entity_load_avg()
4135 * detach_entity_load_avg - detach this entity from its cfs_rq load avg
4140 * cfs_rq->avg.last_update_time being current.
4145 sub_positive(&cfs_rq->avg.util_avg, se->avg.util_avg); in detach_entity_load_avg()
4146 sub_positive(&cfs_rq->avg.util_sum, se->avg.util_sum); in detach_entity_load_avg()
4148 cfs_rq->avg.util_sum = max_t(u32, cfs_rq->avg.util_sum, in detach_entity_load_avg()
4149 cfs_rq->avg.util_avg * PELT_MIN_DIVIDER); in detach_entity_load_avg()
4151 sub_positive(&cfs_rq->avg.runnable_avg, se->avg.runnable_avg); in detach_entity_load_avg()
4152 sub_positive(&cfs_rq->avg.runnable_sum, se->avg.runnable_sum); in detach_entity_load_avg()
4154 cfs_rq->avg.runnable_sum = max_t(u32, cfs_rq->avg.runnable_sum, in detach_entity_load_avg()
4155 cfs_rq->avg.runnable_avg * PELT_MIN_DIVIDER); in detach_entity_load_avg()
4157 add_tg_cfs_propagate(cfs_rq, -se->avg.load_sum); in detach_entity_load_avg()
4182 if (se->avg.last_update_time && !(flags & SKIP_AGE_LOAD)) in update_load_avg()
4188 if (!se->avg.last_update_time && (flags & DO_ATTACH)) { in update_load_avg()
4238 * tasks cannot exit without having gone through wake_up_new_task() -> in remove_entity_load_avg()
4245 raw_spin_lock_irqsave(&cfs_rq->removed.lock, flags); in remove_entity_load_avg()
4246 ++cfs_rq->removed.nr; in remove_entity_load_avg()
4247 cfs_rq->removed.util_avg += se->avg.util_avg; in remove_entity_load_avg()
4248 cfs_rq->removed.load_avg += se->avg.load_avg; in remove_entity_load_avg()
4249 cfs_rq->removed.runnable_avg += se->avg.runnable_avg; in remove_entity_load_avg()
4250 raw_spin_unlock_irqrestore(&cfs_rq->removed.lock, flags); in remove_entity_load_avg()
4255 return cfs_rq->avg.runnable_avg; in cfs_rq_runnable_avg()
4260 return cfs_rq->avg.load_avg; in cfs_rq_load_avg()
4267 return READ_ONCE(p->se.avg.util_avg); in task_util()
4272 struct util_est ue = READ_ONCE(p->se.avg.util_est); in _task_util_est()
4305 enqueued = cfs_rq->avg.util_est.enqueued; in util_est_enqueue()
4307 WRITE_ONCE(cfs_rq->avg.util_est.enqueued, enqueued); in util_est_enqueue()
4321 enqueued = cfs_rq->avg.util_est.enqueued; in util_est_dequeue()
4322 enqueued -= min_t(unsigned int, enqueued, _task_util_est(p)); in util_est_dequeue()
4323 WRITE_ONCE(cfs_rq->avg.util_est.enqueued, enqueued); in util_est_dequeue()
4334 * abs(x) < y := (unsigned)(x + y - 1) < (2 * y - 1)
4340 return ((unsigned int)(value + margin - 1) < (2 * margin - 1)); in within_margin()
4364 ue = p->se.avg.util_est; in util_est_update()
4386 last_ewma_diff = ue.enqueued - ue.ewma; in util_est_update()
4387 last_enqueued_diff -= ue.enqueued; in util_est_update()
4410 * ewma(t) = w * task_util(p) + (1-w) * ewma(t-1) in util_est_update()
4411 * = w * task_util(p) + ewma(t-1) - w * ewma(t-1) in util_est_update()
4412 * = w * (task_util(p) - ewma(t-1)) + ewma(t-1) in util_est_update()
4413 * = w * ( last_ewma_diff ) + ewma(t-1) in util_est_update()
4414 * = w * (last_ewma_diff + ewma(t-1) / w) in util_est_update()
4424 WRITE_ONCE(p->se.avg.util_est, ue); in util_est_update()
4426 trace_sched_util_est_se_tp(&p->se); in util_est_update()
4440 if (!p || p->nr_cpus_allowed == 1) { in update_misfit_status()
4441 rq->misfit_task_load = 0; in update_misfit_status()
4446 rq->misfit_task_load = 0; in update_misfit_status()
4454 rq->misfit_task_load = max_t(unsigned long, task_h_load(p), 1); in update_misfit_status()
4502 s64 d = se->vruntime - cfs_rq->min_vruntime; in check_spread()
4505 d = -d; in check_spread()
4508 schedstat_inc(cfs_rq->nr_spread_over); in check_spread()
4515 u64 vruntime = cfs_rq->min_vruntime; in place_entity()
4526 /* sleeps up to a single latency don't count. */ in place_entity()
4542 vruntime -= thresh; in place_entity()
4546 se->vruntime = max_vruntime(se->vruntime, vruntime); in place_entity()
4559 * vruntime -= min_vruntime
4567 * min_vruntime are up-to-date.
4571 * ->migrate_task_rq_fair() (p->state == TASK_WAKING)
4572 * vruntime -= min_vruntime
4579 * this way we don't have the most up-to-date min_vruntime on the originating
4580 * CPU and an up-to-date min_vruntime on the destination CPU.
4587 bool curr = cfs_rq->curr == se; in enqueue_entity()
4594 se->vruntime += cfs_rq->min_vruntime; in enqueue_entity()
4605 se->vruntime += cfs_rq->min_vruntime; in enqueue_entity()
4609 * - Update loads to have both entity and cfs_rq synced with now. in enqueue_entity()
4610 * - For group_entity, update its runnable_weight to reflect the new in enqueue_entity()
4612 * - For group_entity, update its weight to reflect the new share of in enqueue_entity()
4614 * - Add its new weight to cfs_rq->load.weight in enqueue_entity()
4629 se->on_rq = 1; in enqueue_entity()
4631 if (cfs_rq->nr_running == 1) { in enqueue_entity()
4642 if (cfs_rq->last != se) in __clear_buddies_last()
4645 cfs_rq->last = NULL; in __clear_buddies_last()
4653 if (cfs_rq->next != se) in __clear_buddies_next()
4656 cfs_rq->next = NULL; in __clear_buddies_next()
4664 if (cfs_rq->skip != se) in __clear_buddies_skip()
4667 cfs_rq->skip = NULL; in __clear_buddies_skip()
4673 if (cfs_rq->last == se) in clear_buddies()
4676 if (cfs_rq->next == se) in clear_buddies()
4679 if (cfs_rq->skip == se) in clear_buddies()
4694 * Update run-time statistics of the 'current'. in dequeue_entity()
4700 * - Update loads to have both entity and cfs_rq synced with now. in dequeue_entity()
4701 * - For group_entity, update its runnable_weight to reflect the new in dequeue_entity()
4703 * - Subtract its previous weight from cfs_rq->load.weight. in dequeue_entity()
4704 * - For group entity, update its weight to reflect the new share in dequeue_entity()
4714 if (se != cfs_rq->curr) in dequeue_entity()
4716 se->on_rq = 0; in dequeue_entity()
4726 se->vruntime -= cfs_rq->min_vruntime; in dequeue_entity()
4737 * further than we started -- ie. we'll be penalized. in dequeue_entity()
4742 if (cfs_rq->nr_running == 0) in dequeue_entity()
4757 delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime; in check_preempt_tick()
4762 * re-elected due to buddy favours. in check_preempt_tick()
4777 delta = curr->vruntime - se->vruntime; in check_preempt_tick()
4792 if (se->on_rq) { in set_next_entity()
4804 cfs_rq->curr = se; in set_next_entity()
4809 * when there are only lesser-weight tasks around): in set_next_entity()
4812 rq_of(cfs_rq)->cfs.load.weight >= 2*se->load.weight) { in set_next_entity()
4816 __schedstat_set(stats->slice_max, in set_next_entity()
4817 max((u64)stats->slice_max, in set_next_entity()
4818 se->sum_exec_runtime - se->prev_sum_exec_runtime)); in set_next_entity()
4821 se->prev_sum_exec_runtime = se->sum_exec_runtime; in set_next_entity()
4853 if (cfs_rq->skip && cfs_rq->skip == se) { in pick_next_entity()
4868 if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1) { in pick_next_entity()
4872 se = cfs_rq->next; in pick_next_entity()
4873 } else if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1) { in pick_next_entity()
4877 se = cfs_rq->last; in pick_next_entity()
4891 if (prev->on_rq) in put_prev_entity()
4899 if (prev->on_rq) { in put_prev_entity()
4906 cfs_rq->curr = NULL; in put_prev_entity()
4913 * Update run-time statistics of the 'current'. in entity_tick()
4936 hrtimer_active(&rq_of(cfs_rq)->hrtick_timer)) in entity_tick()
4940 if (cfs_rq->nr_running > 1) in entity_tick()
4994 * directly instead of rq->clock to avoid adding additional synchronization
4995 * around rq->lock.
4997 * requires cfs_b->lock
5003 if (unlikely(cfs_b->quota == RUNTIME_INF)) in __refill_cfs_bandwidth_runtime()
5006 cfs_b->runtime += cfs_b->quota; in __refill_cfs_bandwidth_runtime()
5007 runtime = cfs_b->runtime_snap - cfs_b->runtime; in __refill_cfs_bandwidth_runtime()
5009 cfs_b->burst_time += runtime; in __refill_cfs_bandwidth_runtime()
5010 cfs_b->nr_burst++; in __refill_cfs_bandwidth_runtime()
5013 cfs_b->runtime = min(cfs_b->runtime, cfs_b->quota + cfs_b->burst); in __refill_cfs_bandwidth_runtime()
5014 cfs_b->runtime_snap = cfs_b->runtime; in __refill_cfs_bandwidth_runtime()
5019 return &tg->cfs_bandwidth; in tg_cfs_bandwidth()
5028 lockdep_assert_held(&cfs_b->lock); in __assign_cfs_rq_runtime()
5031 min_amount = target_runtime - cfs_rq->runtime_remaining; in __assign_cfs_rq_runtime()
5033 if (cfs_b->quota == RUNTIME_INF) in __assign_cfs_rq_runtime()
5038 if (cfs_b->runtime > 0) { in __assign_cfs_rq_runtime()
5039 amount = min(cfs_b->runtime, min_amount); in __assign_cfs_rq_runtime()
5040 cfs_b->runtime -= amount; in __assign_cfs_rq_runtime()
5041 cfs_b->idle = 0; in __assign_cfs_rq_runtime()
5045 cfs_rq->runtime_remaining += amount; in __assign_cfs_rq_runtime()
5047 return cfs_rq->runtime_remaining > 0; in __assign_cfs_rq_runtime()
5053 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in assign_cfs_rq_runtime()
5056 raw_spin_lock(&cfs_b->lock); in assign_cfs_rq_runtime()
5058 raw_spin_unlock(&cfs_b->lock); in assign_cfs_rq_runtime()
5066 cfs_rq->runtime_remaining -= delta_exec; in __account_cfs_rq_runtime()
5068 if (likely(cfs_rq->runtime_remaining > 0)) in __account_cfs_rq_runtime()
5071 if (cfs_rq->throttled) in __account_cfs_rq_runtime()
5077 if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr)) in __account_cfs_rq_runtime()
5084 if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled) in account_cfs_rq_runtime()
5092 return cfs_bandwidth_used() && cfs_rq->throttled; in cfs_rq_throttled()
5098 return cfs_bandwidth_used() && cfs_rq->throttle_count; in throttled_hierarchy()
5104 * load-balance operations.
5111 src_cfs_rq = tg->cfs_rq[src_cpu]; in throttled_lb_pair()
5112 dest_cfs_rq = tg->cfs_rq[dest_cpu]; in throttled_lb_pair()
5121 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in tg_unthrottle_up()
5123 cfs_rq->throttle_count--; in tg_unthrottle_up()
5124 if (!cfs_rq->throttle_count) { in tg_unthrottle_up()
5125 cfs_rq->throttled_clock_pelt_time += rq_clock_pelt(rq) - in tg_unthrottle_up()
5126 cfs_rq->throttled_clock_pelt; in tg_unthrottle_up()
5139 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in tg_throttle_down()
5142 if (!cfs_rq->throttle_count) { in tg_throttle_down()
5143 cfs_rq->throttled_clock_pelt = rq_clock_pelt(rq); in tg_throttle_down()
5146 cfs_rq->throttle_count++; in tg_throttle_down()
5154 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in throttle_cfs_rq()
5158 raw_spin_lock(&cfs_b->lock); in throttle_cfs_rq()
5167 * for 1ns of runtime rather than just check cfs_b. in throttle_cfs_rq()
5171 list_add_tail_rcu(&cfs_rq->throttled_list, in throttle_cfs_rq()
5172 &cfs_b->throttled_cfs_rq); in throttle_cfs_rq()
5174 raw_spin_unlock(&cfs_b->lock); in throttle_cfs_rq()
5179 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))]; in throttle_cfs_rq()
5183 walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq); in throttle_cfs_rq()
5186 task_delta = cfs_rq->h_nr_running; in throttle_cfs_rq()
5187 idle_task_delta = cfs_rq->idle_h_nr_running; in throttle_cfs_rq()
5190 /* throttled entity or throttle-on-deactivate */ in throttle_cfs_rq()
5191 if (!se->on_rq) in throttle_cfs_rq()
5197 idle_task_delta = cfs_rq->h_nr_running; in throttle_cfs_rq()
5199 qcfs_rq->h_nr_running -= task_delta; in throttle_cfs_rq()
5200 qcfs_rq->idle_h_nr_running -= idle_task_delta; in throttle_cfs_rq()
5202 if (qcfs_rq->load.weight) { in throttle_cfs_rq()
5203 /* Avoid re-evaluating load for this entity: */ in throttle_cfs_rq()
5211 /* throttled entity or throttle-on-deactivate */ in throttle_cfs_rq()
5212 if (!se->on_rq) in throttle_cfs_rq()
5219 idle_task_delta = cfs_rq->h_nr_running; in throttle_cfs_rq()
5221 qcfs_rq->h_nr_running -= task_delta; in throttle_cfs_rq()
5222 qcfs_rq->idle_h_nr_running -= idle_task_delta; in throttle_cfs_rq()
5231 * throttled-list. rq->lock protects completion. in throttle_cfs_rq()
5233 cfs_rq->throttled = 1; in throttle_cfs_rq()
5234 cfs_rq->throttled_clock = rq_clock(rq); in throttle_cfs_rq()
5241 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in unthrottle_cfs_rq()
5245 se = cfs_rq->tg->se[cpu_of(rq)]; in unthrottle_cfs_rq()
5247 cfs_rq->throttled = 0; in unthrottle_cfs_rq()
5251 raw_spin_lock(&cfs_b->lock); in unthrottle_cfs_rq()
5252 cfs_b->throttled_time += rq_clock(rq) - cfs_rq->throttled_clock; in unthrottle_cfs_rq()
5253 list_del_rcu(&cfs_rq->throttled_list); in unthrottle_cfs_rq()
5254 raw_spin_unlock(&cfs_b->lock); in unthrottle_cfs_rq()
5257 walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq); in unthrottle_cfs_rq()
5259 if (!cfs_rq->load.weight) { in unthrottle_cfs_rq()
5260 if (!cfs_rq->on_list) in unthrottle_cfs_rq()
5273 task_delta = cfs_rq->h_nr_running; in unthrottle_cfs_rq()
5274 idle_task_delta = cfs_rq->idle_h_nr_running; in unthrottle_cfs_rq()
5278 if (se->on_rq) in unthrottle_cfs_rq()
5283 idle_task_delta = cfs_rq->h_nr_running; in unthrottle_cfs_rq()
5285 qcfs_rq->h_nr_running += task_delta; in unthrottle_cfs_rq()
5286 qcfs_rq->idle_h_nr_running += idle_task_delta; in unthrottle_cfs_rq()
5300 idle_task_delta = cfs_rq->h_nr_running; in unthrottle_cfs_rq()
5302 qcfs_rq->h_nr_running += task_delta; in unthrottle_cfs_rq()
5303 qcfs_rq->idle_h_nr_running += idle_task_delta; in unthrottle_cfs_rq()
5317 if (rq->curr == rq->idle && rq->cfs.nr_running) in unthrottle_cfs_rq()
5327 list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq, in distribute_cfs_runtime()
5337 SCHED_WARN_ON(cfs_rq->runtime_remaining > 0); in distribute_cfs_runtime()
5339 raw_spin_lock(&cfs_b->lock); in distribute_cfs_runtime()
5340 runtime = -cfs_rq->runtime_remaining + 1; in distribute_cfs_runtime()
5341 if (runtime > cfs_b->runtime) in distribute_cfs_runtime()
5342 runtime = cfs_b->runtime; in distribute_cfs_runtime()
5343 cfs_b->runtime -= runtime; in distribute_cfs_runtime()
5344 remaining = cfs_b->runtime; in distribute_cfs_runtime()
5345 raw_spin_unlock(&cfs_b->lock); in distribute_cfs_runtime()
5347 cfs_rq->runtime_remaining += runtime; in distribute_cfs_runtime()
5350 if (cfs_rq->runtime_remaining > 0) in distribute_cfs_runtime()
5365 * period the timer is deactivated until scheduling resumes; cfs_b->idle is
5373 if (cfs_b->quota == RUNTIME_INF) in do_sched_cfs_period_timer()
5376 throttled = !list_empty(&cfs_b->throttled_cfs_rq); in do_sched_cfs_period_timer()
5377 cfs_b->nr_periods += overrun; in do_sched_cfs_period_timer()
5379 /* Refill extra burst quota even if cfs_b->idle */ in do_sched_cfs_period_timer()
5386 if (cfs_b->idle && !throttled) in do_sched_cfs_period_timer()
5391 cfs_b->idle = 1; in do_sched_cfs_period_timer()
5396 cfs_b->nr_throttled += overrun; in do_sched_cfs_period_timer()
5399 * This check is repeated as we release cfs_b->lock while we unthrottle. in do_sched_cfs_period_timer()
5401 while (throttled && cfs_b->runtime > 0) { in do_sched_cfs_period_timer()
5402 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in do_sched_cfs_period_timer()
5403 /* we can't nest cfs_b->lock while distributing bandwidth */ in do_sched_cfs_period_timer()
5405 raw_spin_lock_irqsave(&cfs_b->lock, flags); in do_sched_cfs_period_timer()
5407 throttled = !list_empty(&cfs_b->throttled_cfs_rq); in do_sched_cfs_period_timer()
5416 cfs_b->idle = 0; in do_sched_cfs_period_timer()
5434 * Requires cfs_b->lock for hrtimer_expires_remaining to be safe against the
5440 struct hrtimer *refresh_timer = &cfs_b->period_timer; in runtime_refresh_within()
5443 /* if the call-back is running a quota refresh is already occurring */ in runtime_refresh_within()
5464 if (cfs_b->slack_started) in start_cfs_slack_bandwidth()
5466 cfs_b->slack_started = true; in start_cfs_slack_bandwidth()
5468 hrtimer_start(&cfs_b->slack_timer, in start_cfs_slack_bandwidth()
5476 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in __return_cfs_rq_runtime()
5477 s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime; in __return_cfs_rq_runtime()
5482 raw_spin_lock(&cfs_b->lock); in __return_cfs_rq_runtime()
5483 if (cfs_b->quota != RUNTIME_INF) { in __return_cfs_rq_runtime()
5484 cfs_b->runtime += slack_runtime; in __return_cfs_rq_runtime()
5486 /* we are under rq->lock, defer unthrottling using a timer */ in __return_cfs_rq_runtime()
5487 if (cfs_b->runtime > sched_cfs_bandwidth_slice() && in __return_cfs_rq_runtime()
5488 !list_empty(&cfs_b->throttled_cfs_rq)) in __return_cfs_rq_runtime()
5491 raw_spin_unlock(&cfs_b->lock); in __return_cfs_rq_runtime()
5494 cfs_rq->runtime_remaining -= slack_runtime; in __return_cfs_rq_runtime()
5502 if (!cfs_rq->runtime_enabled || cfs_rq->nr_running) in return_cfs_rq_runtime()
5510 * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
5518 raw_spin_lock_irqsave(&cfs_b->lock, flags); in do_sched_cfs_slack_timer()
5519 cfs_b->slack_started = false; in do_sched_cfs_slack_timer()
5522 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in do_sched_cfs_slack_timer()
5526 if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) in do_sched_cfs_slack_timer()
5527 runtime = cfs_b->runtime; in do_sched_cfs_slack_timer()
5529 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in do_sched_cfs_slack_timer()
5540 * runtime as update_curr() throttling can not trigger until it's on-rq.
5547 /* an active group must be handled by the update_curr()->put() path */ in check_enqueue_throttle()
5548 if (!cfs_rq->runtime_enabled || cfs_rq->curr) in check_enqueue_throttle()
5557 if (cfs_rq->runtime_remaining <= 0) in check_enqueue_throttle()
5568 if (!tg->parent) in sync_throttle()
5571 cfs_rq = tg->cfs_rq[cpu]; in sync_throttle()
5572 pcfs_rq = tg->parent->cfs_rq[cpu]; in sync_throttle()
5574 cfs_rq->throttle_count = pcfs_rq->throttle_count; in sync_throttle()
5575 cfs_rq->throttled_clock_pelt = rq_clock_pelt(cpu_rq(cpu)); in sync_throttle()
5584 if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0)) in check_cfs_rq_runtime()
5618 raw_spin_lock_irqsave(&cfs_b->lock, flags); in sched_cfs_period_timer()
5620 overrun = hrtimer_forward_now(timer, cfs_b->period); in sched_cfs_period_timer()
5627 u64 new, old = ktime_to_ns(cfs_b->period); in sched_cfs_period_timer()
5636 cfs_b->period = ns_to_ktime(new); in sched_cfs_period_timer()
5637 cfs_b->quota *= 2; in sched_cfs_period_timer()
5638 cfs_b->burst *= 2; in sched_cfs_period_timer()
5644 div_u64(cfs_b->quota, NSEC_PER_USEC)); in sched_cfs_period_timer()
5650 div_u64(cfs_b->quota, NSEC_PER_USEC)); in sched_cfs_period_timer()
5658 cfs_b->period_active = 0; in sched_cfs_period_timer()
5659 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in sched_cfs_period_timer()
5666 raw_spin_lock_init(&cfs_b->lock); in init_cfs_bandwidth()
5667 cfs_b->runtime = 0; in init_cfs_bandwidth()
5668 cfs_b->quota = RUNTIME_INF; in init_cfs_bandwidth()
5669 cfs_b->period = ns_to_ktime(default_cfs_period()); in init_cfs_bandwidth()
5670 cfs_b->burst = 0; in init_cfs_bandwidth()
5672 INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq); in init_cfs_bandwidth()
5673 hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED); in init_cfs_bandwidth()
5674 cfs_b->period_timer.function = sched_cfs_period_timer; in init_cfs_bandwidth()
5675 hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); in init_cfs_bandwidth()
5676 cfs_b->slack_timer.function = sched_cfs_slack_timer; in init_cfs_bandwidth()
5677 cfs_b->slack_started = false; in init_cfs_bandwidth()
5682 cfs_rq->runtime_enabled = 0; in init_cfs_rq_runtime()
5683 INIT_LIST_HEAD(&cfs_rq->throttled_list); in init_cfs_rq_runtime()
5688 lockdep_assert_held(&cfs_b->lock); in start_cfs_bandwidth()
5690 if (cfs_b->period_active) in start_cfs_bandwidth()
5693 cfs_b->period_active = 1; in start_cfs_bandwidth()
5694 hrtimer_forward_now(&cfs_b->period_timer, cfs_b->period); in start_cfs_bandwidth()
5695 hrtimer_start_expires(&cfs_b->period_timer, HRTIMER_MODE_ABS_PINNED); in start_cfs_bandwidth()
5701 if (!cfs_b->throttled_cfs_rq.next) in destroy_cfs_bandwidth()
5704 hrtimer_cancel(&cfs_b->period_timer); in destroy_cfs_bandwidth()
5705 hrtimer_cancel(&cfs_b->slack_timer); in destroy_cfs_bandwidth()
5724 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth; in update_runtime_enabled()
5725 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in update_runtime_enabled()
5727 raw_spin_lock(&cfs_b->lock); in update_runtime_enabled()
5728 cfs_rq->runtime_enabled = cfs_b->quota != RUNTIME_INF; in update_runtime_enabled()
5729 raw_spin_unlock(&cfs_b->lock); in update_runtime_enabled()
5743 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in unthrottle_offline_cfs_rqs()
5745 if (!cfs_rq->runtime_enabled) in unthrottle_offline_cfs_rqs()
5752 cfs_rq->runtime_remaining = 1; in unthrottle_offline_cfs_rqs()
5757 cfs_rq->runtime_enabled = 0; in unthrottle_offline_cfs_rqs()
5817 struct sched_entity *se = &p->se; in hrtick_start_fair()
5822 if (rq->cfs.h_nr_running > 1) { in hrtick_start_fair()
5824 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime; in hrtick_start_fair()
5825 s64 delta = slice - ran; in hrtick_start_fair()
5843 struct task_struct *curr = rq->curr; in hrtick_update()
5845 if (!hrtick_enabled_fair(rq) || curr->sched_class != &fair_sched_class) in hrtick_update()
5848 if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency) in hrtick_update()
5870 if (!READ_ONCE(rq->rd->overutilized) && cpu_overutilized(rq->cpu)) { in update_overutilized_status()
5871 WRITE_ONCE(rq->rd->overutilized, SG_OVERUTILIZED); in update_overutilized_status()
5872 trace_sched_overutilized_tp(rq->rd, SG_OVERUTILIZED); in update_overutilized_status()
5882 return unlikely(rq->nr_running == rq->cfs.idle_h_nr_running && in sched_idle_rq()
5883 rq->nr_running); in sched_idle_rq()
5893 return cfs_rq->nr_running && in sched_idle_cfs_rq()
5894 cfs_rq->nr_running == cfs_rq->idle_nr_running; in sched_idle_cfs_rq()
5913 struct sched_entity *se = &p->se; in enqueue_task_fair()
5923 util_est_enqueue(&rq->cfs, p); in enqueue_task_fair()
5930 if (p->in_iowait) in enqueue_task_fair()
5934 if (se->on_rq) in enqueue_task_fair()
5939 cfs_rq->h_nr_running++; in enqueue_task_fair()
5940 cfs_rq->idle_h_nr_running += idle_h_nr_running; in enqueue_task_fair()
5959 cfs_rq->h_nr_running++; in enqueue_task_fair()
5960 cfs_rq->idle_h_nr_running += idle_h_nr_running; in enqueue_task_fair()
6006 struct sched_entity *se = &p->se; in dequeue_task_fair()
6011 util_est_dequeue(&rq->cfs, p); in dequeue_task_fair()
6017 cfs_rq->h_nr_running--; in dequeue_task_fair()
6018 cfs_rq->idle_h_nr_running -= idle_h_nr_running; in dequeue_task_fair()
6028 if (cfs_rq->load.weight) { in dequeue_task_fair()
6029 /* Avoid re-evaluating load for this entity: */ in dequeue_task_fair()
6049 cfs_rq->h_nr_running--; in dequeue_task_fair()
6050 cfs_rq->idle_h_nr_running -= idle_h_nr_running; in dequeue_task_fair()
6066 rq->next_balance = jiffies; in dequeue_task_fair()
6069 util_est_update(&rq->cfs, p, task_sleep); in dequeue_task_fair()
6094 return cfs_rq_load_avg(&rq->cfs); in cpu_load()
6098 * cpu_load_without - compute CPU load without any contributions from *p
6116 if (cpu_of(rq) != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in cpu_load_without()
6119 cfs_rq = &rq->cfs; in cpu_load_without()
6120 load = READ_ONCE(cfs_rq->avg.load_avg); in cpu_load_without()
6130 return cfs_rq_runnable_avg(&rq->cfs); in cpu_runnable()
6139 if (cpu_of(rq) != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in cpu_runnable_without()
6142 cfs_rq = &rq->cfs; in cpu_runnable_without()
6143 runnable = READ_ONCE(cfs_rq->avg.runnable_avg); in cpu_runnable_without()
6146 lsub_positive(&runnable, p->se.avg.runnable_avg); in cpu_runnable_without()
6153 return cpu_rq(cpu)->cpu_capacity; in capacity_of()
6162 if (time_after(jiffies, current->wakee_flip_decay_ts + HZ)) { in record_wakee()
6163 current->wakee_flips >>= 1; in record_wakee()
6164 current->wakee_flip_decay_ts = jiffies; in record_wakee()
6167 if (current->last_wakee != p) { in record_wakee()
6168 current->last_wakee = p; in record_wakee()
6169 current->wakee_flips++; in record_wakee()
6174 * Detect M:N waker/wakee relationships via a switching-frequency heuristic.
6184 * non-monogamous, with partner count exceeding socket size.
6192 unsigned int master = current->wakee_flips; in wake_wide()
6193 unsigned int slave = p->wakee_flips; in wake_wide()
6208 * wake_affine_idle() - only considers 'now', it check if the waking CPU is
6209 * cache-affine and is (or will be) idle.
6211 * wake_affine_weight() - considers the weight to reflect the average
6212 * scheduling latency of the CPUs. This seems to work
6233 if (sync && cpu_rq(this_cpu)->nr_running == 1) in wake_affine_idle()
6257 this_eff_load -= current_load; in wake_affine_weight()
6268 prev_eff_load -= task_load; in wake_affine_weight()
6270 prev_eff_load *= 100 + (sd->imbalance_pct - 100) / 2; in wake_affine_weight()
6296 schedstat_inc(p->stats.nr_wakeups_affine_attempts); in wake_affine()
6300 schedstat_inc(sd->ttwu_move_affine); in wake_affine()
6301 schedstat_inc(p->stats.nr_wakeups_affine); in wake_affine()
6309 * find_idlest_group_cpu - find the idlest CPU among the CPUs in the group.
6318 int shallowest_idle_cpu = -1; in find_idlest_group_cpu()
6322 if (group->group_weight == 1) in find_idlest_group_cpu()
6326 for_each_cpu_and(i, sched_group_span(group), p->cpus_ptr) { in find_idlest_group_cpu()
6337 if (idle && idle->exit_latency < min_exit_latency) { in find_idlest_group_cpu()
6340 * has the smallest exit latency irrespective in find_idlest_group_cpu()
6343 min_exit_latency = idle->exit_latency; in find_idlest_group_cpu()
6344 latest_idle_timestamp = rq->idle_stamp; in find_idlest_group_cpu()
6346 } else if ((!idle || idle->exit_latency == min_exit_latency) && in find_idlest_group_cpu()
6347 rq->idle_stamp > latest_idle_timestamp) { in find_idlest_group_cpu()
6353 latest_idle_timestamp = rq->idle_stamp; in find_idlest_group_cpu()
6356 } else if (shallowest_idle_cpu == -1) { in find_idlest_group_cpu()
6365 return shallowest_idle_cpu != -1 ? shallowest_idle_cpu : least_loaded_cpu; in find_idlest_group_cpu()
6373 if (!cpumask_intersects(sched_domain_span(sd), p->cpus_ptr)) in find_idlest_cpu()
6381 sync_entity_load_avg(&p->se); in find_idlest_cpu()
6388 if (!(sd->flags & sd_flag)) { in find_idlest_cpu()
6389 sd = sd->child; in find_idlest_cpu()
6395 sd = sd->child; in find_idlest_cpu()
6402 sd = sd->child; in find_idlest_cpu()
6408 weight = sd->span_weight; in find_idlest_cpu()
6411 if (weight <= tmp->span_weight) in find_idlest_cpu()
6413 if (tmp->flags & sd_flag) in find_idlest_cpu()
6427 return -1; in __select_idle_cpu()
6440 WRITE_ONCE(sds->has_idle_cores, val); in set_idle_cores()
6449 return READ_ONCE(sds->has_idle_cores); in test_idle_cores()
6456 * information in sd_llc_shared->has_idle_cores.
6486 * sd_llc->shared->has_idle_cores and enabled through update_idle_core() above.
6496 if (*idle_cpu == -1) { in select_idle_core()
6497 if (sched_idle_cpu(cpu) && cpumask_test_cpu(cpu, p->cpus_ptr)) { in select_idle_core()
6505 if (*idle_cpu == -1 && cpumask_test_cpu(cpu, p->cpus_ptr)) in select_idle_core()
6513 return -1; in select_idle_core()
6523 for_each_cpu_and(cpu, cpu_smt_mask(target), p->cpus_ptr) { in select_idle_smt()
6530 return -1; in select_idle_smt()
6551 return -1; in select_idle_smt()
6558 * comparing the average scan cost (tracked in sd->avg_scan_cost) against the
6559 * average idle time for this rq (as found in rq->avg_idle).
6564 int i, cpu, idle_cpu = -1, nr = INT_MAX; in select_idle_cpu()
6571 cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr); in select_idle_cpu()
6579 return -1; in select_idle_cpu()
6586 if (unlikely(this_rq->wake_stamp < now)) { in select_idle_cpu()
6587 while (this_rq->wake_stamp < now && this_rq->wake_avg_idle) { in select_idle_cpu()
6588 this_rq->wake_stamp++; in select_idle_cpu()
6589 this_rq->wake_avg_idle >>= 1; in select_idle_cpu()
6593 avg_idle = this_rq->wake_avg_idle; in select_idle_cpu()
6594 avg_cost = this_sd->avg_scan_cost + 1; in select_idle_cpu()
6596 span_avg = sd->span_weight * avg_idle; in select_idle_cpu()
6608 /* because !--nr is the condition to stop scan */ in select_idle_cpu()
6609 nr = READ_ONCE(sd_share->nr_idle_scan) + 1; in select_idle_cpu()
6612 return -1; in select_idle_cpu()
6623 if (!--nr) in select_idle_cpu()
6624 return -1; in select_idle_cpu()
6635 time = cpu_clock(this) - time; in select_idle_cpu()
6641 this_rq->wake_avg_idle -= min(this_rq->wake_avg_idle, time); in select_idle_cpu()
6643 update_avg(&this_sd->avg_scan_cost, time); in select_idle_cpu()
6658 int cpu, best_cpu = -1; in select_idle_capacity()
6662 cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr); in select_idle_capacity()
6706 sync_entity_load_avg(&p->se); in select_idle_sibling()
6711 * per-cpu select_rq_mask usage in select_idle_sibling()
6728 * Allow a per-cpu kthread to stack with the wakee if the in select_idle_sibling()
6731 * per-cpu kthread that is now complete and the wakeup is in select_idle_sibling()
6738 this_rq()->nr_running <= 1 && in select_idle_sibling()
6744 recent_used_cpu = p->recent_used_cpu; in select_idle_sibling()
6745 p->recent_used_cpu = prev; in select_idle_sibling()
6750 cpumask_test_cpu(p->recent_used_cpu, p->cpus_ptr) && in select_idle_sibling()
6798 * (@dst_cpu = -1) or migrated to @dst_cpu.
6802 struct cfs_rq *cfs_rq = &cpu_rq(cpu)->cfs; in cpu_util_next()
6803 unsigned long util = READ_ONCE(cfs_rq->avg.util_avg); in cpu_util_next()
6806 * If @dst_cpu is -1 or @p migrates from @cpu to @dst_cpu remove its in cpu_util_next()
6819 util_est = READ_ONCE(cfs_rq->avg.util_est.enqueued); in cpu_util_next()
6822 * During wake-up @p isn't enqueued yet and doesn't contribute in cpu_util_next()
6823 * to any cpu_rq(cpu)->cfs.avg.util_est.enqueued. in cpu_util_next()
6827 * During exec (@dst_cpu = -1) @p is enqueued and does in cpu_util_next()
6828 * contribute to cpu_rq(cpu)->cfs.util_est.enqueued. in cpu_util_next()
6837 * p->on_rq = TASK_ON_RQ_MIGRATING; in cpu_util_next()
6838 * -------------------------------- A in cpu_util_next()
6842 * -------------------------------- B in cpu_util_next()
6874 if (cpu != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in cpu_util_without()
6877 return cpu_util_next(cpu, p, -1); in cpu_util_without()
6881 * energy_env - Utilization landscape for energy estimation.
6887 * @pd_cap: Entire perf domain capacity. (pd->nr_cpus * cpu_cap).
6913 eenv->task_busy_time = busy_time; in eenv_task_busy_time()
6927 * - A stable PD utilization, no matter which CPU of that PD we want to place
6930 * - A fair comparison between CPUs as the task contribution (task_util())
6935 * exceed @eenv->pd_cap.
6945 unsigned long util = cpu_util_next(cpu, p, -1); in eenv_pd_busy_time()
6950 eenv->pd_busy_time = min(eenv->pd_cap, busy_time); in eenv_pd_busy_time()
6957 * Returns the maximum utilization among @eenv->cpus. This utilization can't
6958 * exceed @eenv->cpu_cap.
6983 return min(max_util, eenv->cpu_cap); in eenv_pd_max_util()
6996 unsigned long busy_time = eenv->pd_busy_time; in compute_energy()
6999 busy_time = min(eenv->pd_cap, busy_time + eenv->task_busy_time); in compute_energy()
7001 return em_cpu_energy(pd->em_pd, max_util, busy_time, eenv->cpu_cap); in compute_energy()
7005 * find_energy_efficient_cpu(): Find most energy-efficient target CPU for the
7009 * out which of the CPU candidates is the most energy-efficient.
7026 * cluster-packing, and spreading inside a cluster. That should at least be
7027 * a good thing for latency, and this is consistent with the idea that most
7033 * NOTE: Forkees are not accepted in the energy-aware wake-up path because
7037 * to be energy-inefficient in some use-cases. The alternative would be to
7040 * other use-cases too. So, until someone finds a better way to solve this,
7041 * let's keep things simple by re-using the existing slow path.
7047 struct root_domain *rd = this_rq()->rd; in find_energy_efficient_cpu()
7048 int cpu, best_energy_cpu, target = -1; in find_energy_efficient_cpu()
7054 pd = rcu_dereference(rd->pd); in find_energy_efficient_cpu()
7055 if (!pd || READ_ONCE(rd->overutilized)) in find_energy_efficient_cpu()
7059 * Energy-aware wake-up happens on the lowest sched_domain starting in find_energy_efficient_cpu()
7064 sd = sd->parent; in find_energy_efficient_cpu()
7070 sync_entity_load_avg(&p->se); in find_energy_efficient_cpu()
7076 for (; pd; pd = pd->next) { in find_energy_efficient_cpu()
7080 int max_spare_cap_cpu = -1; in find_energy_efficient_cpu()
7091 cpu_thermal_cap -= arch_scale_thermal_pressure(cpu); in find_energy_efficient_cpu()
7102 if (!cpumask_test_cpu(cpu, p->cpus_ptr)) in find_energy_efficient_cpu()
7139 base_energy = compute_energy(&eenv, pd, cpus, p, -1); in find_energy_efficient_cpu()
7148 prev_delta -= base_energy; in find_energy_efficient_cpu()
7159 cur_delta -= base_energy; in find_energy_efficient_cpu()
7192 int sync = (wake_flags & WF_SYNC) && !(current->flags & PF_EXITING); in select_task_rq_fair()
7201 * required for stable ->cpus_allowed in select_task_rq_fair()
7203 lockdep_assert_held(&p->pi_lock); in select_task_rq_fair()
7214 want_affine = !wake_wide(p) && cpumask_test_cpu(cpu, p->cpus_ptr); in select_task_rq_fair()
7223 if (want_affine && (tmp->flags & SD_WAKE_AFFINE) && in select_task_rq_fair()
7237 if (tmp->flags & sd_flag) in select_task_rq_fair()
7258 * previous CPU. The caller guarantees p->pi_lock or task_rq(p)->lock is held.
7262 struct sched_entity *se = &p->se; in migrate_task_rq_fair()
7267 * min_vruntime -- the latter is done by enqueue_entity() when placing in migrate_task_rq_fair()
7270 if (READ_ONCE(p->__state) == TASK_WAKING) { in migrate_task_rq_fair()
7273 se->vruntime -= u64_u32_load(cfs_rq->min_vruntime); in migrate_task_rq_fair()
7283 * leading to an inflation after wake-up on the new rq. in migrate_task_rq_fair()
7293 se->avg.last_update_time = 0; in migrate_task_rq_fair()
7296 se->exec_start = 0; in migrate_task_rq_fair()
7303 remove_entity_load_avg(&p->se); in task_dead_fair()
7309 if (rq->nr_running) in balance_fair()
7321 * Since its curr running now, convert the gran from real-time in wakeup_gran()
7322 * to virtual-time in his units. in wakeup_gran()
7343 * |<--->|c
7345 * w(c, s1) = -1
7353 s64 gran, vdiff = curr->vruntime - se->vruntime; in wakeup_preempt_entity()
7356 return -1; in wakeup_preempt_entity()
7368 if (SCHED_WARN_ON(!se->on_rq)) in set_last_buddy()
7372 cfs_rq_of(se)->last = se; in set_last_buddy()
7379 if (SCHED_WARN_ON(!se->on_rq)) in set_next_buddy()
7383 cfs_rq_of(se)->next = se; in set_next_buddy()
7390 cfs_rq_of(se)->skip = se; in set_skip_buddy()
7398 struct task_struct *curr = rq->curr; in check_preempt_wakeup()
7399 struct sched_entity *se = &curr->se, *pse = &p->se; in check_preempt_wakeup()
7401 int scale = cfs_rq->nr_running >= sched_nr_latency; in check_preempt_wakeup()
7412 * next-buddy nomination below. in check_preempt_wakeup()
7426 * Note: this also catches the edge-case of curr being in a throttled in check_preempt_wakeup()
7435 /* Idle tasks are by definition preempted by non-idle tasks. */ in check_preempt_wakeup()
7441 * Batch and idle tasks do not preempt non-idle tasks (their preemption in check_preempt_wakeup()
7444 if (unlikely(p->policy != SCHED_NORMAL) || !sched_feat(WAKEUP_PREEMPTION)) in check_preempt_wakeup()
7454 * Preempt an idle group in favor of a non-idle group (and don't preempt in check_preempt_wakeup()
7480 * with schedule on the ->pre_schedule() or idle_balance() in check_preempt_wakeup()
7481 * point, either of which can * drop the rq lock. in check_preempt_wakeup()
7486 if (unlikely(!se->on_rq || curr == rq->idle)) in check_preempt_wakeup()
7500 cfs_rq = &rq->cfs; in pick_task_fair()
7501 if (!cfs_rq->nr_running) in pick_task_fair()
7505 struct sched_entity *curr = cfs_rq->curr; in pick_task_fair()
7509 if (curr->on_rq) in pick_task_fair()
7529 struct cfs_rq *cfs_rq = &rq->cfs; in pick_next_task_fair()
7539 if (!prev || prev->sched_class != &fair_sched_class) in pick_next_task_fair()
7551 struct sched_entity *curr = cfs_rq->curr; in pick_next_task_fair()
7555 * have to consider cfs_rq->curr. If it is still a runnable in pick_next_task_fair()
7560 if (curr->on_rq) in pick_next_task_fair()
7572 cfs_rq = &rq->cfs; in pick_next_task_fair()
7574 if (!cfs_rq->nr_running) in pick_next_task_fair()
7593 struct sched_entity *pse = &prev->se; in pick_next_task_fair()
7596 int se_depth = se->depth; in pick_next_task_fair()
7597 int pse_depth = pse->depth; in pick_next_task_fair()
7634 list_move(&p->se.group_node, &rq->cfs_tasks); in pick_next_task_fair()
7651 * Because newidle_balance() releases (and re-acquires) rq->lock, it is in pick_next_task_fair()
7653 * must re-start the pick_next_entity() loop. in pick_next_task_fair()
7680 struct sched_entity *se = &prev->se; in put_prev_task_fair()
7692 * The magic of dealing with the ->skip buddy is in pick_next_entity.
7696 struct task_struct *curr = rq->curr; in yield_task_fair()
7698 struct sched_entity *se = &curr->se; in yield_task_fair()
7703 if (unlikely(rq->nr_running == 1)) in yield_task_fair()
7708 if (curr->policy != SCHED_BATCH) { in yield_task_fair()
7711 * Update run-time statistics of the 'current'. in yield_task_fair()
7727 struct sched_entity *se = &p->se; in yield_to_task_fair()
7730 if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se))) in yield_to_task_fair()
7743 * Fair scheduling class load-balancing methods.
7747 * The purpose of load-balancing is to achieve the same basic fairness the
7748 * per-CPU scheduler provides, namely provide a proportional amount of compute
7753 * Where W_i,n is the n-th weight average for CPU i. The instantaneous weight
7758 * Where w_i,j is the weight of the j-th runnable task on CPU i. This weight
7764 * W'_i,n = (2^n - 1) / 2^n * W_i,n + 1 / 2^n * W_i,0 (3)
7773 * imb_i,j = max{ avg(W/C), W_i/C_i } - min{ avg(W/C), W_j/C_j } (4)
7780 * - infeasible weights;
7781 * - local vs global optima in the discrete case. ]
7791 * of load-balance at each level inv. proportional to the number of CPUs in
7797 * \Sum { --- * --- * 2^i } = O(n) (5)
7799 * `- size of each group
7800 * | | `- number of CPUs doing load-balance
7801 * | `- freq
7802 * `- sum over all levels
7844 * W_i,0 = \Sum_j \Prod_k w_k * ----- (9)
7851 * w_i,j,k is the weight of the j-th runnable task in the k-th cgroup on CPU i.
7928 /* The set of CPUs under consideration for load-balancing */
7943 * Is this task likely cache-hot:
7949 lockdep_assert_rq_held(env->src_rq); in task_hot()
7951 if (p->sched_class != &fair_sched_class) in task_hot()
7958 if (env->sd->flags & SD_SHARE_CPUCAPACITY) in task_hot()
7964 if (sched_feat(CACHE_HOT_BUDDY) && env->dst_rq->nr_running && in task_hot()
7965 (&p->se == cfs_rq_of(&p->se)->next || in task_hot()
7966 &p->se == cfs_rq_of(&p->se)->last)) in task_hot()
7969 if (sysctl_sched_migration_cost == -1) in task_hot()
7976 if (!sched_core_cookie_match(cpu_rq(env->dst_cpu), p)) in task_hot()
7982 delta = rq_clock_task(env->src_rq) - p->se.exec_start; in task_hot()
7991 * Returns -1, if task migration is not affected by locality.
7995 struct numa_group *numa_group = rcu_dereference(p->numa_group); in migrate_degrades_locality()
8000 return -1; in migrate_degrades_locality()
8002 if (!p->numa_faults || !(env->sd->flags & SD_NUMA)) in migrate_degrades_locality()
8003 return -1; in migrate_degrades_locality()
8005 src_nid = cpu_to_node(env->src_cpu); in migrate_degrades_locality()
8006 dst_nid = cpu_to_node(env->dst_cpu); in migrate_degrades_locality()
8009 return -1; in migrate_degrades_locality()
8012 if (src_nid == p->numa_preferred_nid) { in migrate_degrades_locality()
8013 if (env->src_rq->nr_running > env->src_rq->nr_preferred_running) in migrate_degrades_locality()
8016 return -1; in migrate_degrades_locality()
8020 if (dst_nid == p->numa_preferred_nid) in migrate_degrades_locality()
8024 if (env->idle == CPU_IDLE) in migrate_degrades_locality()
8025 return -1; in migrate_degrades_locality()
8043 return -1; in migrate_degrades_locality()
8048 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
8055 lockdep_assert_rq_held(env->src_rq); in can_migrate_task()
8062 * 4) are cache-hot on their current CPU. in can_migrate_task()
8064 if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu)) in can_migrate_task()
8071 if (!cpumask_test_cpu(env->dst_cpu, p->cpus_ptr)) { in can_migrate_task()
8074 schedstat_inc(p->stats.nr_failed_migrations_affine); in can_migrate_task()
8076 env->flags |= LBF_SOME_PINNED; in can_migrate_task()
8084 * - for NEWLY_IDLE in can_migrate_task()
8085 * - if we have already computed one in current iteration in can_migrate_task()
8086 * - if it's an active balance in can_migrate_task()
8088 if (env->idle == CPU_NEWLY_IDLE || in can_migrate_task()
8089 env->flags & (LBF_DST_PINNED | LBF_ACTIVE_LB)) in can_migrate_task()
8092 /* Prevent to re-select dst_cpu via env's CPUs: */ in can_migrate_task()
8093 for_each_cpu_and(cpu, env->dst_grpmask, env->cpus) { in can_migrate_task()
8094 if (cpumask_test_cpu(cpu, p->cpus_ptr)) { in can_migrate_task()
8095 env->flags |= LBF_DST_PINNED; in can_migrate_task()
8096 env->new_dst_cpu = cpu; in can_migrate_task()
8105 env->flags &= ~LBF_ALL_PINNED; in can_migrate_task()
8107 if (task_on_cpu(env->src_rq, p)) { in can_migrate_task()
8108 schedstat_inc(p->stats.nr_failed_migrations_running); in can_migrate_task()
8119 if (env->flags & LBF_ACTIVE_LB) in can_migrate_task()
8123 if (tsk_cache_hot == -1) in can_migrate_task()
8127 env->sd->nr_balance_failed > env->sd->cache_nice_tries) { in can_migrate_task()
8129 schedstat_inc(env->sd->lb_hot_gained[env->idle]); in can_migrate_task()
8130 schedstat_inc(p->stats.nr_forced_migrations); in can_migrate_task()
8135 schedstat_inc(p->stats.nr_failed_migrations_hot); in can_migrate_task()
8140 * detach_task() -- detach the task for the migration specified in env
8144 lockdep_assert_rq_held(env->src_rq); in detach_task()
8146 deactivate_task(env->src_rq, p, DEQUEUE_NOCLOCK); in detach_task()
8147 set_task_cpu(p, env->dst_cpu); in detach_task()
8151 * detach_one_task() -- tries to dequeue exactly one task from env->src_rq, as
8160 lockdep_assert_rq_held(env->src_rq); in detach_one_task()
8163 &env->src_rq->cfs_tasks, se.group_node) { in detach_one_task()
8171 * lb_gained[env->idle] is updated (other is detach_tasks) in detach_one_task()
8175 schedstat_inc(env->sd->lb_gained[env->idle]); in detach_one_task()
8182 * detach_tasks() -- tries to detach up to imbalance load/util/tasks from
8189 struct list_head *tasks = &env->src_rq->cfs_tasks; in detach_tasks()
8194 lockdep_assert_rq_held(env->src_rq); in detach_tasks()
8200 if (env->src_rq->nr_running <= 1) { in detach_tasks()
8201 env->flags &= ~LBF_ALL_PINNED; in detach_tasks()
8205 if (env->imbalance <= 0) in detach_tasks()
8213 if (env->idle != CPU_NOT_IDLE && env->src_rq->nr_running <= 1) in detach_tasks()
8216 env->loop++; in detach_tasks()
8221 if (env->loop > env->loop_max && in detach_tasks()
8222 !(env->flags & LBF_ALL_PINNED)) in detach_tasks()
8226 if (env->loop > env->loop_break) { in detach_tasks()
8227 env->loop_break += SCHED_NR_MIGRATE_BREAK; in detach_tasks()
8228 env->flags |= LBF_NEED_BREAK; in detach_tasks()
8237 switch (env->migration_type) { in detach_tasks()
8242 * value. Make sure that env->imbalance decreases in detach_tasks()
8249 load < 16 && !env->sd->nr_balance_failed) in detach_tasks()
8258 if (shr_bound(load, env->sd->nr_balance_failed) > env->imbalance) in detach_tasks()
8261 env->imbalance -= load; in detach_tasks()
8267 if (util > env->imbalance) in detach_tasks()
8270 env->imbalance -= util; in detach_tasks()
8274 env->imbalance--; in detach_tasks()
8279 if (task_fits_capacity(p, capacity_of(env->src_cpu))) in detach_tasks()
8282 env->imbalance = 0; in detach_tasks()
8287 list_add(&p->se.group_node, &env->tasks); in detach_tasks()
8293 * NEWIDLE balancing is a source of latency, so preemptible in detach_tasks()
8297 if (env->idle == CPU_NEWLY_IDLE) in detach_tasks()
8305 if (env->imbalance <= 0) in detach_tasks()
8310 list_move(&p->se.group_node, tasks); in detach_tasks()
8318 schedstat_add(env->sd->lb_gained[env->idle], detached); in detach_tasks()
8324 * attach_task() -- attach the task detached by detach_task() to its new rq.
8336 * attach_one_task() -- attaches the task returned from detach_one_task() to
8350 * attach_tasks() -- attaches all tasks detached by detach_tasks() to their
8355 struct list_head *tasks = &env->tasks; in attach_tasks()
8359 rq_lock(env->dst_rq, &rf); in attach_tasks()
8360 update_rq_clock(env->dst_rq); in attach_tasks()
8364 list_del_init(&p->se.group_node); in attach_tasks()
8366 attach_task(env->dst_rq, p); in attach_tasks()
8369 rq_unlock(env->dst_rq, &rf); in attach_tasks()
8375 if (cfs_rq->avg.load_avg) in cfs_rq_has_blocked()
8378 if (cfs_rq->avg.util_avg) in cfs_rq_has_blocked()
8386 if (READ_ONCE(rq->avg_rt.util_avg)) in others_have_blocked()
8389 if (READ_ONCE(rq->avg_dl.util_avg)) in others_have_blocked()
8396 if (READ_ONCE(rq->avg_irq.util_avg)) in others_have_blocked()
8405 WRITE_ONCE(rq->last_blocked_load_update_tick, jiffies); in update_blocked_load_tick()
8411 rq->has_blocked_load = 0; in update_blocked_load_status()
8431 curr_class = rq->curr->sched_class; in __update_blocked_others()
8464 if (cfs_rq->nr_running == 0) in __update_blocked_fair()
8467 if (cfs_rq == &rq->cfs) in __update_blocked_fair()
8472 se = cfs_rq->tg->se[cpu]; in __update_blocked_fair()
8493 * This needs to be done in a top-down fashion because the load of a child
8499 struct sched_entity *se = cfs_rq->tg->se[cpu_of(rq)]; in update_cfs_rq_h_load()
8503 if (cfs_rq->last_h_load_update == now) in update_cfs_rq_h_load()
8506 WRITE_ONCE(cfs_rq->h_load_next, NULL); in update_cfs_rq_h_load()
8509 WRITE_ONCE(cfs_rq->h_load_next, se); in update_cfs_rq_h_load()
8510 if (cfs_rq->last_h_load_update == now) in update_cfs_rq_h_load()
8515 cfs_rq->h_load = cfs_rq_load_avg(cfs_rq); in update_cfs_rq_h_load()
8516 cfs_rq->last_h_load_update = now; in update_cfs_rq_h_load()
8519 while ((se = READ_ONCE(cfs_rq->h_load_next)) != NULL) { in update_cfs_rq_h_load()
8520 load = cfs_rq->h_load; in update_cfs_rq_h_load()
8521 load = div64_ul(load * se->avg.load_avg, in update_cfs_rq_h_load()
8524 cfs_rq->h_load = load; in update_cfs_rq_h_load()
8525 cfs_rq->last_h_load_update = now; in update_cfs_rq_h_load()
8534 return div64_ul(p->se.avg.load_avg * cfs_rq->h_load, in task_h_load()
8540 struct cfs_rq *cfs_rq = &rq->cfs; in __update_blocked_fair()
8552 return p->se.avg.load_avg; in task_h_load()
8578 * sg_lb_stats - stats of a sched_group required for load_balancing
8600 * sd_lb_stats - Structure to store the statistics of a sched_domain
8654 used = READ_ONCE(rq->avg_rt.util_avg); in scale_rt_capacity()
8655 used += READ_ONCE(rq->avg_dl.util_avg); in scale_rt_capacity()
8661 free = max - used; in scale_rt_capacity()
8669 struct sched_group *sdg = sd->groups; in update_cpu_capacity()
8671 cpu_rq(cpu)->cpu_capacity_orig = arch_scale_cpu_capacity(cpu); in update_cpu_capacity()
8676 cpu_rq(cpu)->cpu_capacity = capacity; in update_cpu_capacity()
8679 sdg->sgc->capacity = capacity; in update_cpu_capacity()
8680 sdg->sgc->min_capacity = capacity; in update_cpu_capacity()
8681 sdg->sgc->max_capacity = capacity; in update_cpu_capacity()
8686 struct sched_domain *child = sd->child; in update_group_capacity()
8687 struct sched_group *group, *sdg = sd->groups; in update_group_capacity()
8691 interval = msecs_to_jiffies(sd->balance_interval); in update_group_capacity()
8693 sdg->sgc->next_update = jiffies + interval; in update_group_capacity()
8704 if (child->flags & SD_OVERLAP) { in update_group_capacity()
8723 group = child->groups; in update_group_capacity()
8725 struct sched_group_capacity *sgc = group->sgc; in update_group_capacity()
8727 capacity += sgc->capacity; in update_group_capacity()
8728 min_capacity = min(sgc->min_capacity, min_capacity); in update_group_capacity()
8729 max_capacity = max(sgc->max_capacity, max_capacity); in update_group_capacity()
8730 group = group->next; in update_group_capacity()
8731 } while (group != child->groups); in update_group_capacity()
8734 sdg->sgc->capacity = capacity; in update_group_capacity()
8735 sdg->sgc->min_capacity = min_capacity; in update_group_capacity()
8736 sdg->sgc->max_capacity = max_capacity; in update_group_capacity()
8747 return ((rq->cpu_capacity * sd->imbalance_pct) < in check_cpu_capacity()
8748 (rq->cpu_capacity_orig * 100)); in check_cpu_capacity()
8758 return rq->misfit_task_load && in check_misfit_status()
8759 (rq->cpu_capacity_orig < rq->rd->max_cpu_capacity || in check_misfit_status()
8765 * groups is inadequate due to ->cpus_ptr constraints.
8774 * If we were to balance group-wise we'd place two tasks in the first group and
8794 return group->sgc->imbalance; in sg_imbalanced()
8812 if (sgs->sum_nr_running < sgs->group_weight) in group_has_capacity()
8815 if ((sgs->group_capacity * imbalance_pct) < in group_has_capacity()
8816 (sgs->group_runnable * 100)) in group_has_capacity()
8819 if ((sgs->group_capacity * 100) > in group_has_capacity()
8820 (sgs->group_util * imbalance_pct)) in group_has_capacity()
8837 if (sgs->sum_nr_running <= sgs->group_weight) in group_is_overloaded()
8840 if ((sgs->group_capacity * 100) < in group_is_overloaded()
8841 (sgs->group_util * imbalance_pct)) in group_is_overloaded()
8844 if ((sgs->group_capacity * imbalance_pct) < in group_is_overloaded()
8845 (sgs->group_runnable * 100)) in group_is_overloaded()
8862 if (sgs->group_asym_packing) in group_classify()
8865 if (sgs->group_misfit_task_load) in group_classify()
8875 * asym_smt_can_pull_tasks - Check whether the load balancing CPU can pull tasks
8877 * @sds: Load-balancing data with statistics of the local group
8878 * @sgs: Load-balancing statistics of the candidate busiest group
8906 local_is_smt = sds->local->flags & SD_SHARE_CPUCAPACITY; in asym_smt_can_pull_tasks()
8907 sg_is_smt = sg->flags & SD_SHARE_CPUCAPACITY; in asym_smt_can_pull_tasks()
8909 sg_busy_cpus = sgs->group_weight - sgs->idle_cpus; in asym_smt_can_pull_tasks()
8926 return sched_asym_prefer(dst_cpu, sg->asym_prefer_cpu); in asym_smt_can_pull_tasks()
8932 int local_busy_cpus = sds->local->group_weight - in asym_smt_can_pull_tasks()
8933 sds->local_stat.idle_cpus; in asym_smt_can_pull_tasks()
8934 int busy_cpus_delta = sg_busy_cpus - local_busy_cpus; in asym_smt_can_pull_tasks()
8937 return sched_asym_prefer(dst_cpu, sg->asym_prefer_cpu); in asym_smt_can_pull_tasks()
8947 if (!sds->local_stat.sum_nr_running) in asym_smt_can_pull_tasks()
8948 return sched_asym_prefer(dst_cpu, sg->asym_prefer_cpu); in asym_smt_can_pull_tasks()
8952 /* Always return false so that callers deal with non-SMT cases. */ in asym_smt_can_pull_tasks()
8962 if ((sds->local->flags & SD_SHARE_CPUCAPACITY) || in sched_asym()
8963 (group->flags & SD_SHARE_CPUCAPACITY)) in sched_asym()
8964 return asym_smt_can_pull_tasks(env->dst_cpu, sds, sgs, group); in sched_asym()
8966 return sched_asym_prefer(env->dst_cpu, group->asym_prefer_cpu); in sched_asym()
8976 if (rq->cfs.h_nr_running != 1) in sched_reduced_capacity()
8983 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
8985 * @sds: Load-balancing data with statistics of the local group.
9000 local_group = group == sds->local; in update_sg_lb_stats()
9002 for_each_cpu_and(i, sched_group_span(group), env->cpus) { in update_sg_lb_stats()
9006 sgs->group_load += load; in update_sg_lb_stats()
9007 sgs->group_util += cpu_util_cfs(i); in update_sg_lb_stats()
9008 sgs->group_runnable += cpu_runnable(rq); in update_sg_lb_stats()
9009 sgs->sum_h_nr_running += rq->cfs.h_nr_running; in update_sg_lb_stats()
9011 nr_running = rq->nr_running; in update_sg_lb_stats()
9012 sgs->sum_nr_running += nr_running; in update_sg_lb_stats()
9021 sgs->nr_numa_running += rq->nr_numa_running; in update_sg_lb_stats()
9022 sgs->nr_preferred_running += rq->nr_preferred_running; in update_sg_lb_stats()
9028 sgs->idle_cpus++; in update_sg_lb_stats()
9036 if (env->sd->flags & SD_ASYM_CPUCAPACITY) { in update_sg_lb_stats()
9038 if (sgs->group_misfit_task_load < rq->misfit_task_load) { in update_sg_lb_stats()
9039 sgs->group_misfit_task_load = rq->misfit_task_load; in update_sg_lb_stats()
9042 } else if ((env->idle != CPU_NOT_IDLE) && in update_sg_lb_stats()
9043 sched_reduced_capacity(rq, env->sd)) { in update_sg_lb_stats()
9045 if (sgs->group_misfit_task_load < load) in update_sg_lb_stats()
9046 sgs->group_misfit_task_load = load; in update_sg_lb_stats()
9050 sgs->group_capacity = group->sgc->capacity; in update_sg_lb_stats()
9052 sgs->group_weight = group->group_weight; in update_sg_lb_stats()
9055 if (!local_group && env->sd->flags & SD_ASYM_PACKING && in update_sg_lb_stats()
9056 env->idle != CPU_NOT_IDLE && sgs->sum_h_nr_running && in update_sg_lb_stats()
9058 sgs->group_asym_packing = 1; in update_sg_lb_stats()
9061 sgs->group_type = group_classify(env->sd->imbalance_pct, group, sgs); in update_sg_lb_stats()
9064 if (sgs->group_type == group_overloaded) in update_sg_lb_stats()
9065 sgs->avg_load = (sgs->group_load * SCHED_CAPACITY_SCALE) / in update_sg_lb_stats()
9066 sgs->group_capacity; in update_sg_lb_stats()
9070 * update_sd_pick_busiest - return 1 on busiest group
9087 struct sg_lb_stats *busiest = &sds->busiest_stat; in update_sd_pick_busiest()
9090 if (!sgs->sum_h_nr_running) in update_sd_pick_busiest()
9099 if ((env->sd->flags & SD_ASYM_CPUCAPACITY) && in update_sd_pick_busiest()
9100 (sgs->group_type == group_misfit_task) && in update_sd_pick_busiest()
9101 (!capacity_greater(capacity_of(env->dst_cpu), sg->sgc->max_capacity) || in update_sd_pick_busiest()
9102 sds->local_stat.group_type != group_has_spare)) in update_sd_pick_busiest()
9105 if (sgs->group_type > busiest->group_type) in update_sd_pick_busiest()
9108 if (sgs->group_type < busiest->group_type) in update_sd_pick_busiest()
9116 switch (sgs->group_type) { in update_sd_pick_busiest()
9119 if (sgs->avg_load <= busiest->avg_load) in update_sd_pick_busiest()
9132 if (sched_asym_prefer(sg->asym_prefer_cpu, sds->busiest->asym_prefer_cpu)) in update_sd_pick_busiest()
9141 if (sgs->group_misfit_task_load < busiest->group_misfit_task_load) in update_sd_pick_busiest()
9156 if (sgs->avg_load <= busiest->avg_load) in update_sd_pick_busiest()
9168 if (sgs->idle_cpus > busiest->idle_cpus) in update_sd_pick_busiest()
9170 else if ((sgs->idle_cpus == busiest->idle_cpus) && in update_sd_pick_busiest()
9171 (sgs->sum_nr_running <= busiest->sum_nr_running)) in update_sd_pick_busiest()
9179 * per-CPU capacity. Migrating tasks to less capable CPUs may harm in update_sd_pick_busiest()
9183 if ((env->sd->flags & SD_ASYM_CPUCAPACITY) && in update_sd_pick_busiest()
9184 (sgs->group_type <= group_fully_busy) && in update_sd_pick_busiest()
9185 (capacity_greater(sg->sgc->min_capacity, capacity_of(env->dst_cpu)))) in update_sd_pick_busiest()
9194 if (sgs->sum_h_nr_running > sgs->nr_numa_running) in fbq_classify_group()
9196 if (sgs->sum_h_nr_running > sgs->nr_preferred_running) in fbq_classify_group()
9203 if (rq->nr_running > rq->nr_numa_running) in fbq_classify_rq()
9205 if (rq->nr_running > rq->nr_preferred_running) in fbq_classify_rq()
9225 * task_running_on_cpu - return 1 if @p is running on @cpu.
9231 if (cpu != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in task_running_on_cpu()
9241 * idle_cpu_without - would a given CPU be idle without p ?
9251 if (rq->curr != rq->idle && rq->curr != p) in idle_cpu_without()
9255 * rq->nr_running can't be used but an updated version without the in idle_cpu_without()
9261 if (rq->ttwu_pending) in idle_cpu_without()
9269 * update_sg_wakeup_stats - Update sched_group's statistics for wakeup.
9288 sgs->group_load += cpu_load_without(rq, p); in update_sg_wakeup_stats()
9289 sgs->group_util += cpu_util_without(i, p); in update_sg_wakeup_stats()
9290 sgs->group_runnable += cpu_runnable_without(rq, p); in update_sg_wakeup_stats()
9292 sgs->sum_h_nr_running += rq->cfs.h_nr_running - local; in update_sg_wakeup_stats()
9294 nr_running = rq->nr_running - local; in update_sg_wakeup_stats()
9295 sgs->sum_nr_running += nr_running; in update_sg_wakeup_stats()
9301 sgs->idle_cpus++; in update_sg_wakeup_stats()
9306 if (sd->flags & SD_ASYM_CPUCAPACITY && in update_sg_wakeup_stats()
9307 !task_fits_capacity(p, group->sgc->max_capacity)) { in update_sg_wakeup_stats()
9308 sgs->group_misfit_task_load = 1; in update_sg_wakeup_stats()
9311 sgs->group_capacity = group->sgc->capacity; in update_sg_wakeup_stats()
9313 sgs->group_weight = group->group_weight; in update_sg_wakeup_stats()
9315 sgs->group_type = group_classify(sd->imbalance_pct, group, sgs); in update_sg_wakeup_stats()
9321 if (sgs->group_type == group_fully_busy || in update_sg_wakeup_stats()
9322 sgs->group_type == group_overloaded) in update_sg_wakeup_stats()
9323 sgs->avg_load = (sgs->group_load * SCHED_CAPACITY_SCALE) / in update_sg_wakeup_stats()
9324 sgs->group_capacity; in update_sg_wakeup_stats()
9332 if (sgs->group_type < idlest_sgs->group_type) in update_pick_idlest()
9335 if (sgs->group_type > idlest_sgs->group_type) in update_pick_idlest()
9343 switch (sgs->group_type) { in update_pick_idlest()
9347 if (idlest_sgs->avg_load <= sgs->avg_load) in update_pick_idlest()
9358 if (idlest->sgc->max_capacity >= group->sgc->max_capacity) in update_pick_idlest()
9364 if (idlest_sgs->idle_cpus > sgs->idle_cpus) in update_pick_idlest()
9368 if (idlest_sgs->idle_cpus == sgs->idle_cpus && in update_pick_idlest()
9369 idlest_sgs->group_util <= sgs->group_util) in update_pick_idlest()
9387 struct sched_group *idlest = NULL, *local = NULL, *group = sd->groups; in find_idlest_group()
9401 p->cpus_ptr)) in find_idlest_group()
9425 } while (group = group->next, group != sd->groups); in find_idlest_group()
9456 (sd->imbalance_pct-100) / 100; in find_idlest_group()
9463 * cross-domain, add imbalance to the load on the remote node in find_idlest_group()
9467 if ((sd->flags & SD_NUMA) && in find_idlest_group()
9478 if (100 * local_sgs.avg_load <= sd->imbalance_pct * idlest_sgs.avg_load) in find_idlest_group()
9489 if (local->sgc->max_capacity >= idlest->sgc->max_capacity) in find_idlest_group()
9495 if (sd->flags & SD_NUMA) { in find_idlest_group()
9496 int imb_numa_nr = sd->imb_numa_nr; in find_idlest_group()
9503 if (cpu_to_node(this_cpu) == p->numa_preferred_nid) in find_idlest_group()
9507 if (cpu_to_node(idlest_cpu) == p->numa_preferred_nid) in find_idlest_group()
9519 if (p->nr_cpus_allowed != NR_CPUS) { in find_idlest_group()
9522 cpumask_and(cpus, sched_group_span(local), p->cpus_ptr); in find_idlest_group()
9523 imb_numa_nr = min(cpumask_weight(cpus), sd->imb_numa_nr); in find_idlest_group()
9526 imbalance = abs(local_sgs.idle_cpus - idlest_sgs.idle_cpus); in find_idlest_group()
9563 if (!sched_feat(SIS_UTIL) || env->idle == CPU_NEWLY_IDLE) in update_idle_cpu_scan()
9566 llc_weight = per_cpu(sd_llc_size, env->dst_cpu); in update_idle_cpu_scan()
9567 if (env->sd->span_weight != llc_weight) in update_idle_cpu_scan()
9570 sd_share = rcu_dereference(per_cpu(sd_llc_shared, env->dst_cpu)); in update_idle_cpu_scan()
9580 * let y = SCHED_CAPACITY_SCALE - p * x^2 [1] in update_idle_cpu_scan()
9598 * y = SCHED_CAPACITY_SCALE - in update_idle_cpu_scan()
9607 pct = env->sd->imbalance_pct; in update_idle_cpu_scan()
9611 y = SCHED_CAPACITY_SCALE - tmp; in update_idle_cpu_scan()
9616 if ((int)y != sd_share->nr_idle_scan) in update_idle_cpu_scan()
9617 WRITE_ONCE(sd_share->nr_idle_scan, (int)y); in update_idle_cpu_scan()
9621 * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
9628 struct sched_domain *child = env->sd->child; in update_sd_lb_stats()
9629 struct sched_group *sg = env->sd->groups; in update_sd_lb_stats()
9630 struct sg_lb_stats *local = &sds->local_stat; in update_sd_lb_stats()
9639 local_group = cpumask_test_cpu(env->dst_cpu, sched_group_span(sg)); in update_sd_lb_stats()
9641 sds->local = sg; in update_sd_lb_stats()
9644 if (env->idle != CPU_NEWLY_IDLE || in update_sd_lb_stats()
9645 time_after_eq(jiffies, sg->sgc->next_update)) in update_sd_lb_stats()
9646 update_group_capacity(env->sd, env->dst_cpu); in update_sd_lb_stats()
9656 sds->busiest = sg; in update_sd_lb_stats()
9657 sds->busiest_stat = *sgs; in update_sd_lb_stats()
9662 sds->total_load += sgs->group_load; in update_sd_lb_stats()
9663 sds->total_capacity += sgs->group_capacity; in update_sd_lb_stats()
9665 sum_util += sgs->group_util; in update_sd_lb_stats()
9666 sg = sg->next; in update_sd_lb_stats()
9667 } while (sg != env->sd->groups); in update_sd_lb_stats()
9670 sds->prefer_sibling = child && child->flags & SD_PREFER_SIBLING; in update_sd_lb_stats()
9673 if (env->sd->flags & SD_NUMA) in update_sd_lb_stats()
9674 env->fbq_type = fbq_classify_group(&sds->busiest_stat); in update_sd_lb_stats()
9676 if (!env->sd->parent) { in update_sd_lb_stats()
9677 struct root_domain *rd = env->dst_rq->rd; in update_sd_lb_stats()
9680 WRITE_ONCE(rd->overload, sg_status & SG_OVERLOAD); in update_sd_lb_stats()
9682 /* Update over-utilization (tipping point, U >= 0) indicator */ in update_sd_lb_stats()
9683 WRITE_ONCE(rd->overutilized, sg_status & SG_OVERUTILIZED); in update_sd_lb_stats()
9686 struct root_domain *rd = env->dst_rq->rd; in update_sd_lb_stats()
9688 WRITE_ONCE(rd->overutilized, SG_OVERUTILIZED); in update_sd_lb_stats()
9696 * calculate_imbalance - Calculate the amount of imbalance present within the
9705 local = &sds->local_stat; in calculate_imbalance()
9706 busiest = &sds->busiest_stat; in calculate_imbalance()
9708 if (busiest->group_type == group_misfit_task) { in calculate_imbalance()
9709 if (env->sd->flags & SD_ASYM_CPUCAPACITY) { in calculate_imbalance()
9711 env->migration_type = migrate_misfit; in calculate_imbalance()
9712 env->imbalance = 1; in calculate_imbalance()
9718 env->migration_type = migrate_load; in calculate_imbalance()
9719 env->imbalance = busiest->group_misfit_task_load; in calculate_imbalance()
9724 if (busiest->group_type == group_asym_packing) { in calculate_imbalance()
9729 env->migration_type = migrate_task; in calculate_imbalance()
9730 env->imbalance = busiest->sum_h_nr_running; in calculate_imbalance()
9734 if (busiest->group_type == group_imbalanced) { in calculate_imbalance()
9736 * In the group_imb case we cannot rely on group-wide averages in calculate_imbalance()
9737 * to ensure CPU-load equilibrium, try to move any task to fix in calculate_imbalance()
9741 env->migration_type = migrate_task; in calculate_imbalance()
9742 env->imbalance = 1; in calculate_imbalance()
9750 if (local->group_type == group_has_spare) { in calculate_imbalance()
9751 if ((busiest->group_type > group_fully_busy) && in calculate_imbalance()
9752 !(env->sd->flags & SD_SHARE_PKG_RESOURCES)) { in calculate_imbalance()
9761 env->migration_type = migrate_util; in calculate_imbalance()
9762 env->imbalance = max(local->group_capacity, local->group_util) - in calculate_imbalance()
9763 local->group_util; in calculate_imbalance()
9772 if (env->idle != CPU_NOT_IDLE && env->imbalance == 0) { in calculate_imbalance()
9773 env->migration_type = migrate_task; in calculate_imbalance()
9774 env->imbalance = 1; in calculate_imbalance()
9780 if (busiest->group_weight == 1 || sds->prefer_sibling) { in calculate_imbalance()
9781 unsigned int nr_diff = busiest->sum_nr_running; in calculate_imbalance()
9786 env->migration_type = migrate_task; in calculate_imbalance()
9787 lsub_positive(&nr_diff, local->sum_nr_running); in calculate_imbalance()
9788 env->imbalance = nr_diff; in calculate_imbalance()
9795 env->migration_type = migrate_task; in calculate_imbalance()
9796 env->imbalance = max_t(long, 0, in calculate_imbalance()
9797 (local->idle_cpus - busiest->idle_cpus)); in calculate_imbalance()
9802 if (env->sd->flags & SD_NUMA) { in calculate_imbalance()
9803 env->imbalance = adjust_numa_imbalance(env->imbalance, in calculate_imbalance()
9804 local->sum_nr_running + 1, in calculate_imbalance()
9805 env->sd->imb_numa_nr); in calculate_imbalance()
9810 env->imbalance >>= 1; in calculate_imbalance()
9819 if (local->group_type < group_overloaded) { in calculate_imbalance()
9825 local->avg_load = (local->group_load * SCHED_CAPACITY_SCALE) / in calculate_imbalance()
9826 local->group_capacity; in calculate_imbalance()
9832 if (local->avg_load >= busiest->avg_load) { in calculate_imbalance()
9833 env->imbalance = 0; in calculate_imbalance()
9837 sds->avg_load = (sds->total_load * SCHED_CAPACITY_SCALE) / in calculate_imbalance()
9838 sds->total_capacity; in calculate_imbalance()
9849 env->migration_type = migrate_load; in calculate_imbalance()
9850 env->imbalance = min( in calculate_imbalance()
9851 (busiest->avg_load - sds->avg_load) * busiest->group_capacity, in calculate_imbalance()
9852 (sds->avg_load - local->avg_load) * local->group_capacity in calculate_imbalance()
9879 * find_busiest_group - Returns the busiest group within the sched_domain
9886 * Return: - The busiest group if imbalance exists.
9902 struct root_domain *rd = env->dst_rq->rd; in find_busiest_group()
9904 if (rcu_dereference(rd->pd) && !READ_ONCE(rd->overutilized)) in find_busiest_group()
9916 if (busiest->group_type == group_misfit_task) in find_busiest_group()
9920 if (busiest->group_type == group_asym_packing) in find_busiest_group()
9928 if (busiest->group_type == group_imbalanced) in find_busiest_group()
9935 if (local->group_type > busiest->group_type) in find_busiest_group()
9942 if (local->group_type == group_overloaded) { in find_busiest_group()
9947 if (local->avg_load >= busiest->avg_load) in find_busiest_group()
9958 if (local->avg_load >= sds.avg_load) in find_busiest_group()
9965 if (100 * busiest->avg_load <= in find_busiest_group()
9966 env->sd->imbalance_pct * local->avg_load) in find_busiest_group()
9971 if (sds.prefer_sibling && local->group_type == group_has_spare && in find_busiest_group()
9972 busiest->sum_nr_running > local->sum_nr_running + 1) in find_busiest_group()
9975 if (busiest->group_type != group_overloaded) { in find_busiest_group()
9976 if (env->idle == CPU_NOT_IDLE) in find_busiest_group()
9984 if (busiest->group_weight > 1 && in find_busiest_group()
9985 local->idle_cpus <= (busiest->idle_cpus + 1)) in find_busiest_group()
9997 if (busiest->sum_h_nr_running == 1) in find_busiest_group()
10007 return env->imbalance ? sds.busiest : NULL; in find_busiest_group()
10010 env->imbalance = 0; in find_busiest_group()
10015 * find_busiest_queue - find the busiest runqueue among the CPUs in the group.
10025 for_each_cpu_and(i, sched_group_span(group), env->cpus) { in find_busiest_queue()
10035 * - regular: there are !numa tasks in find_busiest_queue()
10036 * - remote: there are numa tasks that run on the 'wrong' node in find_busiest_queue()
10037 * - all: there is no distinction in find_busiest_queue()
10052 if (rt > env->fbq_type) in find_busiest_queue()
10055 nr_running = rq->cfs.h_nr_running; in find_busiest_queue()
10063 * eventually lead to active_balancing high->low capacity. in find_busiest_queue()
10064 * Higher per-CPU capacity is considered better than balancing in find_busiest_queue()
10067 if (env->sd->flags & SD_ASYM_CPUCAPACITY && in find_busiest_queue()
10068 !capacity_greater(capacity_of(env->dst_cpu), capacity) && in find_busiest_queue()
10073 if ((env->sd->flags & SD_ASYM_PACKING) && in find_busiest_queue()
10074 sched_asym_prefer(i, env->dst_cpu) && in find_busiest_queue()
10078 switch (env->migration_type) { in find_busiest_queue()
10086 if (nr_running == 1 && load > env->imbalance && in find_busiest_queue()
10087 !check_cpu_capacity(rq, env->sd)) in find_busiest_queue()
10139 if (rq->misfit_task_load > busiest_load) { in find_busiest_queue()
10140 busiest_load = rq->misfit_task_load; in find_busiest_queue()
10166 return env->idle != CPU_NOT_IDLE && (env->sd->flags & SD_ASYM_PACKING) && in asym_active_balance()
10167 sched_asym_prefer(env->dst_cpu, env->src_cpu); in asym_active_balance()
10173 struct sched_domain *sd = env->sd; in imbalanced_active_balance()
10180 if ((env->migration_type == migrate_task) && in imbalanced_active_balance()
10181 (sd->nr_balance_failed > sd->cache_nice_tries+2)) in imbalanced_active_balance()
10189 struct sched_domain *sd = env->sd; in need_active_balance()
10203 if ((env->idle != CPU_NOT_IDLE) && in need_active_balance()
10204 (env->src_rq->cfs.h_nr_running == 1)) { in need_active_balance()
10205 if ((check_cpu_capacity(env->src_rq, sd)) && in need_active_balance()
10206 (capacity_of(env->src_cpu)*sd->imbalance_pct < capacity_of(env->dst_cpu)*100)) in need_active_balance()
10210 if (env->migration_type == migrate_misfit) in need_active_balance()
10220 struct sched_group *sg = env->sd->groups; in should_we_balance()
10227 if (!cpumask_test_cpu(env->dst_cpu, env->cpus)) in should_we_balance()
10235 * to optimize wakeup latency. in should_we_balance()
10237 if (env->idle == CPU_NEWLY_IDLE) { in should_we_balance()
10238 if (env->dst_rq->nr_running > 0 || env->dst_rq->ttwu_pending) in should_we_balance()
10244 for_each_cpu_and(cpu, group_balance_mask(sg), env->cpus) { in should_we_balance()
10249 return cpu == env->dst_cpu; in should_we_balance()
10253 return group_balance_cpu(sg) == env->dst_cpu; in should_we_balance()
10265 struct sched_domain *sd_parent = sd->parent; in load_balance()
10274 .dst_grpmask = sched_group_span(sd->groups), in load_balance()
10284 schedstat_inc(sd->lb_count[idle]); in load_balance()
10294 schedstat_inc(sd->lb_nobusyg[idle]); in load_balance()
10300 schedstat_inc(sd->lb_nobusyq[idle]); in load_balance()
10306 schedstat_add(sd->lb_imbalance[idle], env.imbalance); in load_balance()
10308 env.src_cpu = busiest->cpu; in load_balance()
10314 if (busiest->nr_running > 1) { in load_balance()
10317 * an imbalance but busiest->nr_running <= 1, the group is in load_balance()
10321 env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running); in load_balance()
10328 * cur_ld_moved - load moved in current iteration in load_balance()
10329 * ld_moved - cumulative load moved across iterations in load_balance()
10336 * unlock busiest->lock, and we are able to be sure in load_balance()
10353 if (env.loop < busiest->nr_running) in load_balance()
10367 * nohz-idle), we now have balance_cpu in a position to move in load_balance()
10378 /* Prevent to re-select dst_cpu via env's CPUs */ in load_balance()
10398 int *group_imbalance = &sd_parent->groups->sgc->imbalance; in load_balance()
10425 schedstat_inc(sd->lb_failed[idle]); in load_balance()
10433 sd->nr_balance_failed++; in load_balance()
10445 if (!cpumask_test_cpu(this_cpu, busiest->curr->cpus_ptr)) { in load_balance()
10454 * ->active_balance synchronizes accesses to in load_balance()
10455 * ->active_balance_work. Once set, it's cleared in load_balance()
10458 if (!busiest->active_balance) { in load_balance()
10459 busiest->active_balance = 1; in load_balance()
10460 busiest->push_cpu = this_cpu; in load_balance()
10468 &busiest->active_balance_work); in load_balance()
10472 sd->nr_balance_failed = 0; in load_balance()
10477 sd->balance_interval = sd->min_interval; in load_balance()
10489 int *group_imbalance = &sd_parent->groups->sgc->imbalance; in load_balance()
10501 schedstat_inc(sd->lb_balanced[idle]); in load_balance()
10503 sd->nr_balance_failed = 0; in load_balance()
10519 sd->balance_interval < MAX_PINNED_INTERVAL) || in load_balance()
10520 sd->balance_interval < sd->max_interval) in load_balance()
10521 sd->balance_interval *= 2; in load_balance()
10529 unsigned long interval = sd->balance_interval; in get_sd_balance_interval()
10532 interval *= sd->busy_factor; in get_sd_balance_interval()
10543 interval -= 1; in get_sd_balance_interval()
10557 next = sd->last_balance + interval; in update_next_balance()
10573 int target_cpu = busiest_rq->push_cpu; in active_load_balance_cpu_stop()
10581 * Between queueing the stop-work and running it is a hole in which in active_load_balance_cpu_stop()
10590 !busiest_rq->active_balance)) in active_load_balance_cpu_stop()
10594 if (busiest_rq->nr_running <= 1) in active_load_balance_cpu_stop()
10600 * Bjorn Helgaas on a 128-CPU setup. in active_load_balance_cpu_stop()
10616 .src_cpu = busiest_rq->cpu, in active_load_balance_cpu_stop()
10622 schedstat_inc(sd->alb_count); in active_load_balance_cpu_stop()
10627 schedstat_inc(sd->alb_pushed); in active_load_balance_cpu_stop()
10629 sd->nr_balance_failed = 0; in active_load_balance_cpu_stop()
10631 schedstat_inc(sd->alb_failed); in active_load_balance_cpu_stop()
10636 busiest_rq->active_balance = 0; in active_load_balance_cpu_stop()
10651 * This trades load-balance latency on larger machines for less cross talk.
10660 if (cost > sd->max_newidle_lb_cost) { in update_newidle_cost()
10665 sd->max_newidle_lb_cost = cost; in update_newidle_cost()
10666 sd->last_decay_max_lb_cost = jiffies; in update_newidle_cost()
10667 } else if (time_after(jiffies, sd->last_decay_max_lb_cost + HZ)) { in update_newidle_cost()
10673 sd->max_newidle_lb_cost = (sd->max_newidle_lb_cost * 253) / 256; in update_newidle_cost()
10674 sd->last_decay_max_lb_cost = jiffies; in update_newidle_cost()
10691 int cpu = rq->cpu; in rebalance_domains()
10708 max_cost += sd->max_newidle_lb_cost; in rebalance_domains()
10723 need_serialize = sd->flags & SD_SERIALIZE; in rebalance_domains()
10729 if (time_after_eq(jiffies, sd->last_balance + interval)) { in rebalance_domains()
10733 * env->dst_cpu, so we can't know our idle in rebalance_domains()
10739 sd->last_balance = jiffies; in rebalance_domains()
10745 if (time_after(next_balance, sd->last_balance + interval)) { in rebalance_domains()
10746 next_balance = sd->last_balance + interval; in rebalance_domains()
10752 * Ensure the rq-wide value also decays but keep it at a in rebalance_domains()
10753 * reasonable floor to avoid funnies with rq->avg_idle. in rebalance_domains()
10755 rq->max_idle_balance_cost = in rebalance_domains()
10766 rq->next_balance = next_balance; in rebalance_domains()
10772 return unlikely(!rcu_dereference_sched(rq->sd)); in on_null_domain()
10778 * - When one of the busy CPUs notice that there may be an idle rebalancing
10781 * - HK_TYPE_MISC CPUs are used for this task, because HK_TYPE_SCHED not set
10837 smp_call_function_single_async(ilb_cpu, &cpu_rq(ilb_cpu)->nohz_csd); in kick_ilb()
10849 int nr_busy, i, cpu = rq->cpu; in nohz_balancer_kick()
10852 if (unlikely(rq->idle_balance)) in nohz_balancer_kick()
10875 if (rq->nr_running >= 2) { in nohz_balancer_kick()
10882 sd = rcu_dereference(rq->sd); in nohz_balancer_kick()
10889 if (rq->cfs.h_nr_running >= 1 && check_cpu_capacity(rq, sd)) { in nohz_balancer_kick()
10935 * increase the overall cache use), we need some less-loaded LLC in nohz_balancer_kick()
10939 * the others are - so just get a nohz balance going if it looks in nohz_balancer_kick()
10942 nr_busy = atomic_read(&sds->nr_busy_cpus); in nohz_balancer_kick()
10965 if (!sd || !sd->nohz_idle) in set_cpu_sd_state_busy()
10967 sd->nohz_idle = 0; in set_cpu_sd_state_busy()
10969 atomic_inc(&sd->shared->nr_busy_cpus); in set_cpu_sd_state_busy()
10978 if (likely(!rq->nohz_tick_stopped)) in nohz_balance_exit_idle()
10981 rq->nohz_tick_stopped = 0; in nohz_balance_exit_idle()
10982 cpumask_clear_cpu(rq->cpu, nohz.idle_cpus_mask); in nohz_balance_exit_idle()
10985 set_cpu_sd_state_busy(rq->cpu); in nohz_balance_exit_idle()
10995 if (!sd || sd->nohz_idle) in set_cpu_sd_state_idle()
10997 sd->nohz_idle = 1; in set_cpu_sd_state_idle()
10999 atomic_dec(&sd->shared->nr_busy_cpus); in set_cpu_sd_state_idle()
11023 * Can be set safely without rq->lock held in nohz_balance_enter_idle()
11025 * rq->lock is held during the check and the clear in nohz_balance_enter_idle()
11027 rq->has_blocked_load = 1; in nohz_balance_enter_idle()
11035 if (rq->nohz_tick_stopped) in nohz_balance_enter_idle()
11042 rq->nohz_tick_stopped = 1; in nohz_balance_enter_idle()
11067 unsigned int cpu = rq->cpu; in update_nohz_stats()
11069 if (!rq->has_blocked_load) in update_nohz_stats()
11075 if (!time_after(jiffies, READ_ONCE(rq->last_blocked_load_update_tick))) in update_nohz_stats()
11080 return rq->has_blocked_load; in update_nohz_stats()
11095 int this_cpu = this_rq->cpu; in _nohz_idle_balance()
11152 if (time_after_eq(jiffies, rq->next_balance)) { in _nohz_idle_balance()
11163 if (time_after(next_balance, rq->next_balance)) { in _nohz_idle_balance()
11164 next_balance = rq->next_balance; in _nohz_idle_balance()
11193 unsigned int flags = this_rq->nohz_idle_balance; in nohz_idle_balance()
11198 this_rq->nohz_idle_balance = 0; in nohz_idle_balance()
11228 int this_cpu = this_rq->cpu; in nohz_newidle_balance()
11238 if (this_rq->avg_idle < sysctl_sched_migration_cost) in nohz_newidle_balance()
11269 * < 0 - we released the lock and there are !fair tasks present
11270 * 0 - failed, no new tasks
11271 * > 0 - success, new (fair) tasks present
11276 int this_cpu = this_rq->cpu; in newidle_balance()
11287 if (this_rq->ttwu_pending) in newidle_balance()
11294 this_rq->idle_stamp = rq_clock(this_rq); in newidle_balance()
11304 * for load-balance and preemption/IRQs are still disabled avoiding in newidle_balance()
11306 * re-start the picking loop. in newidle_balance()
11311 sd = rcu_dereference_check_sched_domain(this_rq->sd); in newidle_balance()
11313 if (!READ_ONCE(this_rq->rd->overload) || in newidle_balance()
11314 (sd && this_rq->avg_idle < sd->max_newidle_lb_cost)) { in newidle_balance()
11336 if (this_rq->avg_idle < curr_cost + sd->max_newidle_lb_cost) in newidle_balance()
11339 if (sd->flags & SD_BALANCE_NEWIDLE) { in newidle_balance()
11346 domain_cost = t1 - t0; in newidle_balance()
11357 if (pulled_task || this_rq->nr_running > 0 || in newidle_balance()
11358 this_rq->ttwu_pending) in newidle_balance()
11365 if (curr_cost > this_rq->max_idle_balance_cost) in newidle_balance()
11366 this_rq->max_idle_balance_cost = curr_cost; in newidle_balance()
11369 * While browsing the domains, we released the rq lock, a task could in newidle_balance()
11373 if (this_rq->cfs.h_nr_running && !pulled_task) in newidle_balance()
11377 if (this_rq->nr_running != this_rq->cfs.h_nr_running) in newidle_balance()
11378 pulled_task = -1; in newidle_balance()
11382 if (time_after(this_rq->next_balance, next_balance)) in newidle_balance()
11383 this_rq->next_balance = next_balance; in newidle_balance()
11386 this_rq->idle_stamp = 0; in newidle_balance()
11402 enum cpu_idle_type idle = this_rq->idle_balance ? in run_rebalance_domains()
11417 update_blocked_averages(this_rq->cpu); in run_rebalance_domains()
11433 if (time_after_eq(jiffies, rq->next_balance)) in trigger_load_balance()
11461 u64 rtime = se->sum_exec_runtime - se->prev_sum_exec_runtime; in __entity_slice_used()
11483 * MIN_NR_TASKS_DURING_FORCEIDLE - 1 tasks and use that to check in task_tick_core()
11486 if (rq->core->core_forceidle_count && rq->cfs.nr_running == 1 && in task_tick_core()
11487 __entity_slice_used(&curr->se, MIN_NR_TASKS_DURING_FORCEIDLE)) in task_tick_core()
11492 * se_fi_update - Update the cfs_rq->min_vruntime_fi in a CFS hierarchy if needed.
11500 if (cfs_rq->forceidle_seq == fi_seq) in se_fi_update()
11502 cfs_rq->forceidle_seq = fi_seq; in se_fi_update()
11505 cfs_rq->min_vruntime_fi = cfs_rq->min_vruntime; in se_fi_update()
11511 struct sched_entity *se = &p->se; in task_vruntime_update()
11513 if (p->sched_class != &fair_sched_class) in task_vruntime_update()
11516 se_fi_update(se, rq->core->core_forceidle_seq, in_fi); in task_vruntime_update()
11522 struct sched_entity *sea = &a->se; in cfs_prio_less()
11523 struct sched_entity *seb = &b->se; in cfs_prio_less()
11528 SCHED_WARN_ON(task_rq(b)->core != rq->core); in cfs_prio_less()
11535 while (sea->cfs_rq->tg != seb->cfs_rq->tg) { in cfs_prio_less()
11536 int sea_depth = sea->depth; in cfs_prio_less()
11537 int seb_depth = seb->depth; in cfs_prio_less()
11545 se_fi_update(sea, rq->core->core_forceidle_seq, in_fi); in cfs_prio_less()
11546 se_fi_update(seb, rq->core->core_forceidle_seq, in_fi); in cfs_prio_less()
11548 cfs_rqa = sea->cfs_rq; in cfs_prio_less()
11549 cfs_rqb = seb->cfs_rq; in cfs_prio_less()
11551 cfs_rqa = &task_rq(a)->cfs; in cfs_prio_less()
11552 cfs_rqb = &task_rq(b)->cfs; in cfs_prio_less()
11560 delta = (s64)(sea->vruntime - seb->vruntime) + in cfs_prio_less()
11561 (s64)(cfs_rqb->min_vruntime_fi - cfs_rqa->min_vruntime_fi); in cfs_prio_less()
11580 struct sched_entity *se = &curr->se; in task_tick_fair()
11598 * - child not yet on the tasklist
11599 * - preemption disabled
11604 struct sched_entity *se = &p->se, *curr; in task_fork_fair()
11612 curr = cfs_rq->curr; in task_fork_fair()
11615 se->vruntime = curr->vruntime; in task_fork_fair()
11624 swap(curr->vruntime, se->vruntime); in task_fork_fair()
11628 se->vruntime -= cfs_rq->min_vruntime; in task_fork_fair()
11642 if (rq->cfs.nr_running == 1) in prio_changed_fair()
11651 if (p->prio > oldprio) in prio_changed_fair()
11659 struct sched_entity *se = &p->se; in vruntime_normalized()
11666 if (p->on_rq) in vruntime_normalized()
11673 * - A forked child which is waiting for being woken up by in vruntime_normalized()
11675 * - A task which has been woken up by try_to_wake_up() and in vruntime_normalized()
11678 if (!se->sum_exec_runtime || in vruntime_normalized()
11679 (READ_ONCE(p->__state) == TASK_WAKING && p->sched_remote_wakeup)) in vruntime_normalized()
11701 se = se->parent; in propagate_entity_cfs_rq()
11726 * - A forked task which hasn't been woken up by wake_up_new_task(). in detach_entity_cfs_rq()
11727 * - A task which has been woken up by try_to_wake_up() but is in detach_entity_cfs_rq()
11730 if (!se->avg.last_update_time) in detach_entity_cfs_rq()
11754 struct sched_entity *se = &p->se; in detach_task_cfs_rq()
11763 se->vruntime -= cfs_rq->min_vruntime; in detach_task_cfs_rq()
11771 struct sched_entity *se = &p->se; in attach_task_cfs_rq()
11777 se->vruntime += cfs_rq->min_vruntime; in attach_task_cfs_rq()
11804 * This routine is mostly called to set cfs_rq->curr field when a task
11809 struct sched_entity *se = &p->se; in set_next_task_fair()
11817 list_move(&se->group_node, &rq->cfs_tasks); in set_next_task_fair()
11832 cfs_rq->tasks_timeline = RB_ROOT_CACHED; in init_cfs_rq()
11833 u64_u32_store(cfs_rq->min_vruntime, (u64)(-(1LL << 20))); in init_cfs_rq()
11835 raw_spin_lock_init(&cfs_rq->removed.lock); in init_cfs_rq()
11846 if (READ_ONCE(p->__state) == TASK_NEW) in task_change_group_fair()
11852 /* Tell se's cfs_rq has been changed -- migrated */ in task_change_group_fair()
11853 p->se.avg.last_update_time = 0; in task_change_group_fair()
11864 if (tg->cfs_rq) in free_fair_sched_group()
11865 kfree(tg->cfs_rq[i]); in free_fair_sched_group()
11866 if (tg->se) in free_fair_sched_group()
11867 kfree(tg->se[i]); in free_fair_sched_group()
11870 kfree(tg->cfs_rq); in free_fair_sched_group()
11871 kfree(tg->se); in free_fair_sched_group()
11880 tg->cfs_rq = kcalloc(nr_cpu_ids, sizeof(cfs_rq), GFP_KERNEL); in alloc_fair_sched_group()
11881 if (!tg->cfs_rq) in alloc_fair_sched_group()
11883 tg->se = kcalloc(nr_cpu_ids, sizeof(se), GFP_KERNEL); in alloc_fair_sched_group()
11884 if (!tg->se) in alloc_fair_sched_group()
11887 tg->shares = NICE_0_LOAD; in alloc_fair_sched_group()
11903 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]); in alloc_fair_sched_group()
11924 se = tg->se[i]; in online_fair_sched_group()
11942 if (tg->se[cpu]) in unregister_fair_sched_group()
11943 remove_entity_load_avg(tg->se[cpu]); in unregister_fair_sched_group()
11947 * check on_list without danger of it being re-added. in unregister_fair_sched_group()
11949 if (!tg->cfs_rq[cpu]->on_list) in unregister_fair_sched_group()
11955 list_del_leaf_cfs_rq(tg->cfs_rq[cpu]); in unregister_fair_sched_group()
11966 cfs_rq->tg = tg; in init_tg_cfs_entry()
11967 cfs_rq->rq = rq; in init_tg_cfs_entry()
11970 tg->cfs_rq[cpu] = cfs_rq; in init_tg_cfs_entry()
11971 tg->se[cpu] = se; in init_tg_cfs_entry()
11978 se->cfs_rq = &rq->cfs; in init_tg_cfs_entry()
11979 se->depth = 0; in init_tg_cfs_entry()
11981 se->cfs_rq = parent->my_q; in init_tg_cfs_entry()
11982 se->depth = parent->depth + 1; in init_tg_cfs_entry()
11985 se->my_q = cfs_rq; in init_tg_cfs_entry()
11987 update_load_set(&se->load, NICE_0_LOAD); in init_tg_cfs_entry()
11988 se->parent = parent; in init_tg_cfs_entry()
12002 if (!tg->se[0]) in __sched_group_set_shares()
12003 return -EINVAL; in __sched_group_set_shares()
12007 if (tg->shares == shares) in __sched_group_set_shares()
12010 tg->shares = shares; in __sched_group_set_shares()
12013 struct sched_entity *se = tg->se[i]; in __sched_group_set_shares()
12035 ret = -EINVAL; in sched_group_set_shares()
12048 return -EINVAL; in sched_group_set_idle()
12051 return -EINVAL; in sched_group_set_idle()
12055 if (tg->idle == idle) { in sched_group_set_idle()
12060 tg->idle = idle; in sched_group_set_idle()
12064 struct sched_entity *se = tg->se[i]; in sched_group_set_idle()
12065 struct cfs_rq *parent_cfs_rq, *grp_cfs_rq = tg->cfs_rq[i]; in sched_group_set_idle()
12072 grp_cfs_rq->idle = idle; in sched_group_set_idle()
12076 if (se->on_rq) { in sched_group_set_idle()
12079 parent_cfs_rq->idle_nr_running++; in sched_group_set_idle()
12081 parent_cfs_rq->idle_nr_running--; in sched_group_set_idle()
12084 idle_task_delta = grp_cfs_rq->h_nr_running - in sched_group_set_idle()
12085 grp_cfs_rq->idle_h_nr_running; in sched_group_set_idle()
12087 idle_task_delta *= -1; in sched_group_set_idle()
12092 if (!se->on_rq) in sched_group_set_idle()
12095 cfs_rq->idle_h_nr_running += idle_task_delta; in sched_group_set_idle()
12134 struct sched_entity *se = &task->se; in get_rr_interval_fair()
12141 if (rq->cfs.load.weight) in get_rr_interval_fair()
12215 ng = rcu_dereference(p->numa_group); in show_numa_stats()
12217 if (p->numa_faults) { in show_numa_stats()
12218 tsf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 0)]; in show_numa_stats()
12219 tpf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 1)]; in show_numa_stats()
12222 gsf = ng->faults[task_faults_idx(NUMA_MEM, node, 0)], in show_numa_stats()
12223 gpf = ng->faults[task_faults_idx(NUMA_MEM, node, 1)]; in show_numa_stats()