• Home
  • Raw
  • Download

Lines Matching +full:current +full:- +full:regulated

1 // SPDX-License-Identifier: GPL-2.0
55 * Targeted preemption latency for CPU-bound tasks:
58 * 'timeslice length' - timeslices in CFS are of variable length
59 * and have no persistent notion like in traditional, time-slice
63 * run vmstat and monitor the context-switches (cs) field)
71 * The initial- and re-scaling of tunables is configurable
75 * SCHED_TUNABLESCALING_NONE - unscaled, always *1
76 * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus)
77 * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus
84 * Minimal preemption granularity for CPU-bound tasks:
103 * SCHED_OTHER wake-up granularity.
106 * and reduces their over-scheduling. Synchronous workloads will still
135 return -cpu; in arch_asym_cpu_priority()
149 * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool
163 lw->weight += inc; in update_load_add()
164 lw->inv_weight = 0; in update_load_add()
169 lw->weight -= dec; in update_load_sub()
170 lw->inv_weight = 0; in update_load_sub()
175 lw->weight = w; in update_load_set()
176 lw->inv_weight = 0; in update_load_set()
183 * so pick a second-best guess by going with the log2 of the
233 if (likely(lw->inv_weight)) in __update_inv_weight()
236 w = scale_load_down(lw->weight); in __update_inv_weight()
239 lw->inv_weight = 1; in __update_inv_weight()
241 lw->inv_weight = WMULT_CONST; in __update_inv_weight()
243 lw->inv_weight = WMULT_CONST / w; in __update_inv_weight()
249 * (delta_exec * (weight * lw->inv_weight)) >> WMULT_SHIFT
268 shift--; in __calc_delta()
272 fact = mul_u32_u32(fact, lw->inv_weight); in __calc_delta()
276 shift--; in __calc_delta()
298 for (; se; se = se->parent)
302 return p->se.cfs_rq; in task_cfs_rq()
308 return se->cfs_rq; in cfs_rq_of()
314 return grp->my_q; in group_cfs_rq()
322 if (cfs_rq && task_group_is_autogroup(cfs_rq->tg)) in cfs_rq_tg_path()
323 autogroup_path(cfs_rq->tg, path, len); in cfs_rq_tg_path()
324 else if (cfs_rq && cfs_rq->tg->css.cgroup) in cfs_rq_tg_path()
325 cgroup_path(cfs_rq->tg->css.cgroup, path, len); in cfs_rq_tg_path()
335 if (cfs_rq->on_list) in list_add_leaf_cfs_rq()
336 return rq->tmp_alone_branch == &rq->leaf_cfs_rq_list; in list_add_leaf_cfs_rq()
338 cfs_rq->on_list = 1; in list_add_leaf_cfs_rq()
343 * enqueued. The fact that we always enqueue bottom-up in list_add_leaf_cfs_rq()
349 if (cfs_rq->tg->parent && in list_add_leaf_cfs_rq()
350 cfs_rq->tg->parent->cfs_rq[cpu]->on_list) { in list_add_leaf_cfs_rq()
357 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list, in list_add_leaf_cfs_rq()
358 &(cfs_rq->tg->parent->cfs_rq[cpu]->leaf_cfs_rq_list)); in list_add_leaf_cfs_rq()
364 rq->tmp_alone_branch = &rq->leaf_cfs_rq_list; in list_add_leaf_cfs_rq()
368 if (!cfs_rq->tg->parent) { in list_add_leaf_cfs_rq()
373 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list, in list_add_leaf_cfs_rq()
374 &rq->leaf_cfs_rq_list); in list_add_leaf_cfs_rq()
379 rq->tmp_alone_branch = &rq->leaf_cfs_rq_list; in list_add_leaf_cfs_rq()
389 list_add_rcu(&cfs_rq->leaf_cfs_rq_list, rq->tmp_alone_branch); in list_add_leaf_cfs_rq()
394 rq->tmp_alone_branch = &cfs_rq->leaf_cfs_rq_list; in list_add_leaf_cfs_rq()
400 if (cfs_rq->on_list) { in list_del_leaf_cfs_rq()
407 * to the prev element but it will point to rq->leaf_cfs_rq_list in list_del_leaf_cfs_rq()
410 if (rq->tmp_alone_branch == &cfs_rq->leaf_cfs_rq_list) in list_del_leaf_cfs_rq()
411 rq->tmp_alone_branch = cfs_rq->leaf_cfs_rq_list.prev; in list_del_leaf_cfs_rq()
413 list_del_rcu(&cfs_rq->leaf_cfs_rq_list); in list_del_leaf_cfs_rq()
414 cfs_rq->on_list = 0; in list_del_leaf_cfs_rq()
420 SCHED_WARN_ON(rq->tmp_alone_branch != &rq->leaf_cfs_rq_list); in assert_list_leaf_cfs_rq()
425 list_for_each_entry_safe(cfs_rq, pos, &rq->leaf_cfs_rq_list, \
432 if (se->cfs_rq == pse->cfs_rq) in is_same_group()
433 return se->cfs_rq; in is_same_group()
440 return se->parent; in parent_entity()
456 se_depth = (*se)->depth; in find_matching_se()
457 pse_depth = (*pse)->depth; in find_matching_se()
460 se_depth--; in find_matching_se()
465 pse_depth--; in find_matching_se()
487 return &task_rq(p)->cfs; in task_cfs_rq()
495 return &rq->cfs; in cfs_rq_of()
524 for (cfs_rq = &rq->cfs, pos = NULL; cfs_rq; cfs_rq = pos)
547 s64 delta = (s64)(vruntime - max_vruntime); in max_vruntime()
556 s64 delta = (s64)(vruntime - min_vruntime); in min_vruntime()
566 return (s64)(a->vruntime - b->vruntime) < 0; in entity_before()
571 struct sched_entity *curr = cfs_rq->curr; in update_min_vruntime()
572 struct rb_node *leftmost = rb_first_cached(&cfs_rq->tasks_timeline); in update_min_vruntime()
574 u64 vruntime = cfs_rq->min_vruntime; in update_min_vruntime()
577 if (curr->on_rq) in update_min_vruntime()
578 vruntime = curr->vruntime; in update_min_vruntime()
583 if (leftmost) { /* non-empty tree */ in update_min_vruntime()
588 vruntime = se->vruntime; in update_min_vruntime()
590 vruntime = min_vruntime(vruntime, se->vruntime); in update_min_vruntime()
594 cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime); in update_min_vruntime()
597 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime; in update_min_vruntime()
602 * Enqueue an entity into the rb-tree:
606 struct rb_node **link = &cfs_rq->tasks_timeline.rb_root.rb_node; in __enqueue_entity()
622 link = &parent->rb_left; in __enqueue_entity()
624 link = &parent->rb_right; in __enqueue_entity()
629 rb_link_node(&se->run_node, parent, link); in __enqueue_entity()
630 rb_insert_color_cached(&se->run_node, in __enqueue_entity()
631 &cfs_rq->tasks_timeline, leftmost); in __enqueue_entity()
636 rb_erase_cached(&se->run_node, &cfs_rq->tasks_timeline); in __dequeue_entity()
641 struct rb_node *left = rb_first_cached(&cfs_rq->tasks_timeline); in __pick_first_entity()
651 struct rb_node *next = rb_next(&se->run_node); in __pick_next_entity()
662 struct rb_node *last = rb_last(&cfs_rq->tasks_timeline.rb_root); in __pick_last_entity()
702 if (unlikely(se->load.weight != NICE_0_LOAD)) in calc_delta_fair()
703 delta = __calc_delta(delta, NICE_0_LOAD, &se->load); in calc_delta_fair()
725 * We calculate the wall-time slice from the period by taking a part
732 unsigned int nr_running = cfs_rq->nr_running; in sched_slice()
736 nr_running = rq_of(cfs_rq)->cfs.h_nr_running; in sched_slice()
738 slice = __sched_period(nr_running + !se->on_rq); in sched_slice()
745 load = &cfs_rq->load; in sched_slice()
747 if (unlikely(!se->on_rq)) { in sched_slice()
748 lw = cfs_rq->load; in sched_slice()
750 update_load_add(&lw, se->load.weight); in sched_slice()
753 slice = __calc_delta(slice, se->load.weight, load); in sched_slice()
763 * We calculate the vruntime slice of a to-be-inserted task.
781 struct sched_avg *sa = &se->avg; in init_entity_runnable_average()
792 sa->load_avg = scale_load_down(se->load.weight); in init_entity_runnable_average()
801 * based on the cfs_rq's current util_avg:
803 * util_avg = cfs_rq->util_avg / (cfs_rq->load_avg + 1) * se.load.weight
812 * util_avg_cap = (cpu_scale - cfs_rq->avg.util_avg) / 2^n
827 struct sched_entity *se = &p->se; in post_init_entity_util_avg()
829 struct sched_avg *sa = &se->avg; in post_init_entity_util_avg()
831 long cap = (long)(cpu_scale - cfs_rq->avg.util_avg) / 2; in post_init_entity_util_avg()
834 if (cfs_rq->avg.util_avg != 0) { in post_init_entity_util_avg()
835 sa->util_avg = cfs_rq->avg.util_avg * se->load.weight; in post_init_entity_util_avg()
836 sa->util_avg /= (cfs_rq->avg.load_avg + 1); in post_init_entity_util_avg()
838 if (sa->util_avg > cap) in post_init_entity_util_avg()
839 sa->util_avg = cap; in post_init_entity_util_avg()
841 sa->util_avg = cap; in post_init_entity_util_avg()
845 sa->runnable_avg = sa->util_avg; in post_init_entity_util_avg()
847 if (p->sched_class != &fair_sched_class) { in post_init_entity_util_avg()
858 se->avg.last_update_time = cfs_rq_clock_pelt(cfs_rq); in post_init_entity_util_avg()
878 * Update the current task's runtime statistics.
882 struct sched_entity *curr = cfs_rq->curr; in update_curr()
889 delta_exec = now - curr->exec_start; in update_curr()
893 curr->exec_start = now; in update_curr()
895 schedstat_set(curr->statistics.exec_max, in update_curr()
896 max(delta_exec, curr->statistics.exec_max)); in update_curr()
898 curr->sum_exec_runtime += delta_exec; in update_curr()
899 schedstat_add(cfs_rq->exec_clock, delta_exec); in update_curr()
901 curr->vruntime += calc_delta_fair(delta_exec, curr); in update_curr()
907 trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime); in update_curr()
917 update_curr(cfs_rq_of(&rq->curr->se)); in update_curr_fair()
929 prev_wait_start = schedstat_val(se->statistics.wait_start); in update_stats_wait_start()
933 wait_start -= prev_wait_start; in update_stats_wait_start()
935 __schedstat_set(se->statistics.wait_start, wait_start); in update_stats_wait_start()
947 delta = rq_clock(rq_of(cfs_rq)) - schedstat_val(se->statistics.wait_start); in update_stats_wait_end()
957 __schedstat_set(se->statistics.wait_start, delta); in update_stats_wait_end()
963 __schedstat_set(se->statistics.wait_max, in update_stats_wait_end()
964 max(schedstat_val(se->statistics.wait_max), delta)); in update_stats_wait_end()
965 __schedstat_inc(se->statistics.wait_count); in update_stats_wait_end()
966 __schedstat_add(se->statistics.wait_sum, delta); in update_stats_wait_end()
967 __schedstat_set(se->statistics.wait_start, 0); in update_stats_wait_end()
979 sleep_start = schedstat_val(se->statistics.sleep_start); in update_stats_enqueue_sleeper()
980 block_start = schedstat_val(se->statistics.block_start); in update_stats_enqueue_sleeper()
986 u64 delta = rq_clock(rq_of(cfs_rq)) - sleep_start; in update_stats_enqueue_sleeper()
991 if (unlikely(delta > schedstat_val(se->statistics.sleep_max))) in update_stats_enqueue_sleeper()
992 __schedstat_set(se->statistics.sleep_max, delta); in update_stats_enqueue_sleeper()
994 __schedstat_set(se->statistics.sleep_start, 0); in update_stats_enqueue_sleeper()
995 __schedstat_add(se->statistics.sum_sleep_runtime, delta); in update_stats_enqueue_sleeper()
1003 u64 delta = rq_clock(rq_of(cfs_rq)) - block_start; in update_stats_enqueue_sleeper()
1008 if (unlikely(delta > schedstat_val(se->statistics.block_max))) in update_stats_enqueue_sleeper()
1009 __schedstat_set(se->statistics.block_max, delta); in update_stats_enqueue_sleeper()
1011 __schedstat_set(se->statistics.block_start, 0); in update_stats_enqueue_sleeper()
1012 __schedstat_add(se->statistics.sum_sleep_runtime, delta); in update_stats_enqueue_sleeper()
1015 if (tsk->in_iowait) { in update_stats_enqueue_sleeper()
1016 __schedstat_add(se->statistics.iowait_sum, delta); in update_stats_enqueue_sleeper()
1017 __schedstat_inc(se->statistics.iowait_count); in update_stats_enqueue_sleeper()
1025 * 20 to get a milliseconds-range estimation of the in update_stats_enqueue_sleeper()
1039 * Task is being enqueued - update stats:
1048 * Are we enqueueing a waiting task? (for current tasks in update_stats_enqueue()
1051 if (se != cfs_rq->curr) in update_stats_enqueue()
1069 if (se != cfs_rq->curr) in update_stats_dequeue()
1075 if (tsk->state & TASK_INTERRUPTIBLE) in update_stats_dequeue()
1076 __schedstat_set(se->statistics.sleep_start, in update_stats_dequeue()
1078 if (tsk->state & TASK_UNINTERRUPTIBLE) in update_stats_dequeue()
1079 __schedstat_set(se->statistics.block_start, in update_stats_dequeue()
1085 * We are picking a new current task - update its stats:
1093 se->exec_start = rq_clock_task(rq_of(cfs_rq)); in update_stats_curr_start()
1137 * ->numa_group (see struct task_struct for locking rules).
1141 return rcu_dereference_check(p->numa_group, p == current || in deref_task_numa_group()
1142 (lockdep_is_held(&task_rq(p)->lock) && !READ_ONCE(p->on_cpu))); in deref_task_numa_group()
1147 return rcu_dereference_protected(p->numa_group, p == current); in deref_curr_numa_group()
1159 * Calculations based on RSS as non-present and empty pages are skipped in task_nr_scan_windows()
1163 nr_scan_pages = sysctl_numa_balancing_scan_size << (20 - PAGE_SHIFT); in task_nr_scan_windows()
1164 rss = get_mm_rss(p->mm); in task_nr_scan_windows()
1197 ng = rcu_dereference(p->numa_group); in task_scan_start()
1202 period *= refcount_read(&ng->refcount); in task_scan_start()
1227 period *= refcount_read(&ng->refcount); in task_scan_max()
1239 rq->nr_numa_running += (p->numa_preferred_nid != NUMA_NO_NODE); in account_numa_enqueue()
1240 rq->nr_preferred_running += (p->numa_preferred_nid == task_node(p)); in account_numa_enqueue()
1245 rq->nr_numa_running -= (p->numa_preferred_nid != NUMA_NO_NODE); in account_numa_dequeue()
1246 rq->nr_preferred_running -= (p->numa_preferred_nid == task_node(p)); in account_numa_dequeue()
1264 ng = rcu_dereference(p->numa_group); in task_numa_group_id()
1266 gid = ng->gid; in task_numa_group_id()
1275 * array is for current counters, which are averaged into the
1285 if (!p->numa_faults) in task_faults()
1288 return p->numa_faults[task_faults_idx(NUMA_MEM, nid, 0)] + in task_faults()
1289 p->numa_faults[task_faults_idx(NUMA_MEM, nid, 1)]; in task_faults()
1299 return ng->faults[task_faults_idx(NUMA_MEM, nid, 0)] + in group_faults()
1300 ng->faults[task_faults_idx(NUMA_MEM, nid, 1)]; in group_faults()
1305 return group->faults_cpu[task_faults_idx(NUMA_MEM, nid, 0)] + in group_faults_cpu()
1306 group->faults_cpu[task_faults_idx(NUMA_MEM, nid, 1)]; in group_faults_cpu()
1315 faults += ng->faults[task_faults_idx(NUMA_MEM, node, 1)]; in group_faults_priv()
1327 faults += ng->faults[task_faults_idx(NUMA_MEM, node, 0)]; in group_faults_shared()
1335 * considered part of a numa group's pseudo-interleaving set. Migrations
1342 return group_faults_cpu(ng, nid) * ACTIVE_NODE_FRACTION > ng->max_faults_cpu; in numa_is_active_node()
1400 faults *= (sched_max_numa_distance - dist); in score_nearby_nodes()
1401 faults /= (sched_max_numa_distance - LOCAL_DISTANCE); in score_nearby_nodes()
1421 if (!p->numa_faults) in task_weight()
1424 total_faults = p->total_numa_faults; in task_weight()
1444 total_faults = ng->total_faults; in group_weight()
1462 this_cpupid = cpu_pid_to_cpupid(dst_cpu, current->pid); in should_numa_migrate_memory()
1468 * two full passes of the "multi-stage node selection" test that is in should_numa_migrate_memory()
1471 if ((p->numa_preferred_nid == NUMA_NO_NODE || p->numa_scan_seq <= 4) && in should_numa_migrate_memory()
1476 * Multi-stage node selection is used in conjunction with a periodic in should_numa_migrate_memory()
1477 * migration fault to build a temporal task<->page relation. By using in should_numa_migrate_memory()
1478 * a two-stage filter we remove short/unlikely relations. in should_numa_migrate_memory()
1482 * page (n_t) (in a given time-span) to a probability. in should_numa_migrate_memory()
1490 * act on an unlikely task<->page relation. in should_numa_migrate_memory()
1500 /* A shared fault, but p->numa_group has not been set up yet. */ in should_numa_migrate_memory()
1517 * --------------- * - > --------------- in should_numa_migrate_memory()
1596 if ((ns->nr_running > ns->weight) && in numa_classify()
1597 (((ns->compute_capacity * 100) < (ns->util * imbalance_pct)) || in numa_classify()
1598 ((ns->compute_capacity * imbalance_pct) < (ns->runnable * 100)))) in numa_classify()
1601 if ((ns->nr_running < ns->weight) || in numa_classify()
1602 (((ns->compute_capacity * 100) > (ns->util * imbalance_pct)) && in numa_classify()
1603 ((ns->compute_capacity * imbalance_pct) > (ns->runnable * 100)))) in numa_classify()
1644 int cpu, idle_core = -1; in update_numa_stats()
1647 ns->idle_cpu = -1; in update_numa_stats()
1653 ns->load += cpu_load(rq); in update_numa_stats()
1654 ns->runnable += cpu_runnable(rq); in update_numa_stats()
1655 ns->util += cpu_util(cpu); in update_numa_stats()
1656 ns->nr_running += rq->cfs.h_nr_running; in update_numa_stats()
1657 ns->compute_capacity += capacity_of(cpu); in update_numa_stats()
1659 if (find_idle && !rq->nr_running && idle_cpu(cpu)) { in update_numa_stats()
1660 if (READ_ONCE(rq->numa_migrate_on) || in update_numa_stats()
1661 !cpumask_test_cpu(cpu, env->p->cpus_ptr)) in update_numa_stats()
1664 if (ns->idle_cpu == -1) in update_numa_stats()
1665 ns->idle_cpu = cpu; in update_numa_stats()
1672 ns->weight = cpumask_weight(cpumask_of_node(nid)); in update_numa_stats()
1674 ns->node_type = numa_classify(env->imbalance_pct, ns); in update_numa_stats()
1677 ns->idle_cpu = idle_core; in update_numa_stats()
1683 struct rq *rq = cpu_rq(env->dst_cpu); in task_numa_assign()
1685 /* Check if run-queue part of active NUMA balance. */ in task_numa_assign()
1686 if (env->best_cpu != env->dst_cpu && xchg(&rq->numa_migrate_on, 1)) { in task_numa_assign()
1688 int start = env->dst_cpu; in task_numa_assign()
1691 for_each_cpu_wrap(cpu, cpumask_of_node(env->dst_nid), start) { in task_numa_assign()
1692 if (cpu == env->best_cpu || !idle_cpu(cpu) || in task_numa_assign()
1693 !cpumask_test_cpu(cpu, env->p->cpus_ptr)) { in task_numa_assign()
1697 env->dst_cpu = cpu; in task_numa_assign()
1698 rq = cpu_rq(env->dst_cpu); in task_numa_assign()
1699 if (!xchg(&rq->numa_migrate_on, 1)) in task_numa_assign()
1709 * Clear previous best_cpu/rq numa-migrate flag, since task now in task_numa_assign()
1712 if (env->best_cpu != -1 && env->best_cpu != env->dst_cpu) { in task_numa_assign()
1713 rq = cpu_rq(env->best_cpu); in task_numa_assign()
1714 WRITE_ONCE(rq->numa_migrate_on, 0); in task_numa_assign()
1717 if (env->best_task) in task_numa_assign()
1718 put_task_struct(env->best_task); in task_numa_assign()
1722 env->best_task = p; in task_numa_assign()
1723 env->best_imp = imp; in task_numa_assign()
1724 env->best_cpu = env->dst_cpu; in task_numa_assign()
1738 * ------------ vs --------- in load_too_imbalanced()
1741 src_capacity = env->src_stats.compute_capacity; in load_too_imbalanced()
1742 dst_capacity = env->dst_stats.compute_capacity; in load_too_imbalanced()
1744 imb = abs(dst_load * src_capacity - src_load * dst_capacity); in load_too_imbalanced()
1746 orig_src_load = env->src_stats.load; in load_too_imbalanced()
1747 orig_dst_load = env->dst_stats.load; in load_too_imbalanced()
1749 old_imb = abs(orig_dst_load * src_capacity - orig_src_load * dst_capacity); in load_too_imbalanced()
1771 struct numa_group *cur_ng, *p_ng = deref_curr_numa_group(env->p); in task_numa_compare()
1772 struct rq *dst_rq = cpu_rq(env->dst_cpu); in task_numa_compare()
1776 int dist = env->dist; in task_numa_compare()
1781 if (READ_ONCE(dst_rq->numa_migrate_on)) in task_numa_compare()
1785 cur = rcu_dereference(dst_rq->curr); in task_numa_compare()
1786 if (cur && ((cur->flags & PF_EXITING) || is_idle_task(cur))) in task_numa_compare()
1791 * end try selecting ourselves (current == env->p) as a swap candidate. in task_numa_compare()
1793 if (cur == env->p) { in task_numa_compare()
1799 if (maymove && moveimp >= env->best_imp) in task_numa_compare()
1806 if (!cpumask_test_cpu(env->src_cpu, cur->cpus_ptr)) in task_numa_compare()
1813 if (env->best_task && in task_numa_compare()
1814 env->best_task->numa_preferred_nid == env->src_nid && in task_numa_compare()
1815 cur->numa_preferred_nid != env->src_nid) { in task_numa_compare()
1829 cur_ng = rcu_dereference(cur->numa_group); in task_numa_compare()
1831 imp = taskimp + task_weight(cur, env->src_nid, dist) - in task_numa_compare()
1832 task_weight(cur, env->dst_nid, dist); in task_numa_compare()
1838 imp -= imp / 16; in task_numa_compare()
1845 imp += group_weight(cur, env->src_nid, dist) - in task_numa_compare()
1846 group_weight(cur, env->dst_nid, dist); in task_numa_compare()
1848 imp += task_weight(cur, env->src_nid, dist) - in task_numa_compare()
1849 task_weight(cur, env->dst_nid, dist); in task_numa_compare()
1853 if (cur->numa_preferred_nid == env->dst_nid) in task_numa_compare()
1854 imp -= imp / 16; in task_numa_compare()
1862 if (cur->numa_preferred_nid == env->src_nid) in task_numa_compare()
1865 if (maymove && moveimp > imp && moveimp > env->best_imp) { in task_numa_compare()
1875 if (env->best_task && cur->numa_preferred_nid == env->src_nid && in task_numa_compare()
1876 env->best_task->numa_preferred_nid != env->src_nid) { in task_numa_compare()
1886 if (imp < SMALLIMP || imp <= env->best_imp + SMALLIMP / 2) in task_numa_compare()
1892 load = task_h_load(env->p) - task_h_load(cur); in task_numa_compare()
1896 dst_load = env->dst_stats.load + load; in task_numa_compare()
1897 src_load = env->src_stats.load - load; in task_numa_compare()
1905 int cpu = env->dst_stats.idle_cpu; in task_numa_compare()
1907 /* Nothing cached so current CPU went idle since the search. */ in task_numa_compare()
1909 cpu = env->dst_cpu; in task_numa_compare()
1915 if (!idle_cpu(cpu) && env->best_cpu >= 0 && in task_numa_compare()
1916 idle_cpu(env->best_cpu)) { in task_numa_compare()
1917 cpu = env->best_cpu; in task_numa_compare()
1920 env->dst_cpu = cpu; in task_numa_compare()
1930 if (maymove && !cur && env->best_cpu >= 0 && idle_cpu(env->best_cpu)) in task_numa_compare()
1934 * If a swap candidate must be identified and the current best task in task_numa_compare()
1937 if (!maymove && env->best_task && in task_numa_compare()
1938 env->best_task->numa_preferred_nid == env->src_nid) { in task_numa_compare()
1957 if (env->dst_stats.node_type == node_has_spare) { in task_numa_find_cpu()
1967 src_running = env->src_stats.nr_running - 1; in task_numa_find_cpu()
1968 dst_running = env->dst_stats.nr_running + 1; in task_numa_find_cpu()
1969 imbalance = max(0, dst_running - src_running); in task_numa_find_cpu()
1975 if (env->dst_stats.idle_cpu >= 0) { in task_numa_find_cpu()
1976 env->dst_cpu = env->dst_stats.idle_cpu; in task_numa_find_cpu()
1984 * If the improvement from just moving env->p direction is better in task_numa_find_cpu()
1987 load = task_h_load(env->p); in task_numa_find_cpu()
1988 dst_load = env->dst_stats.load + load; in task_numa_find_cpu()
1989 src_load = env->src_stats.load - load; in task_numa_find_cpu()
1993 for_each_cpu(cpu, cpumask_of_node(env->dst_nid)) { in task_numa_find_cpu()
1995 if (!cpumask_test_cpu(cpu, env->p->cpus_ptr)) in task_numa_find_cpu()
1998 env->dst_cpu = cpu; in task_numa_find_cpu()
2016 .best_cpu = -1, in task_numa_migrate()
2030 * random movement of tasks -- counter the numa conditions we're trying in task_numa_migrate()
2036 env.imbalance_pct = 100 + (sd->imbalance_pct - 100) / 2; in task_numa_migrate()
2047 return -EINVAL; in task_numa_migrate()
2050 env.dst_nid = p->numa_preferred_nid; in task_numa_migrate()
2055 taskimp = task_weight(p, env.dst_nid, dist) - taskweight; in task_numa_migrate()
2056 groupimp = group_weight(p, env.dst_nid, dist) - groupweight; in task_numa_migrate()
2064 * - there is no space available on the preferred_nid in task_numa_migrate()
2065 * - the task is part of a numa_group that is interleaved across in task_numa_migrate()
2070 if (env.best_cpu == -1 || (ng && ng->active_nodes > 1)) { in task_numa_migrate()
2072 if (nid == env.src_nid || nid == p->numa_preferred_nid) in task_numa_migrate()
2083 taskimp = task_weight(p, nid, dist) - taskweight; in task_numa_migrate()
2084 groupimp = group_weight(p, nid, dist) - groupweight; in task_numa_migrate()
2104 if (env.best_cpu == -1) in task_numa_migrate()
2109 if (nid != p->numa_preferred_nid) in task_numa_migrate()
2113 /* No better CPU than the current one was found. */ in task_numa_migrate()
2114 if (env.best_cpu == -1) { in task_numa_migrate()
2115 trace_sched_stick_numa(p, env.src_cpu, NULL, -1); in task_numa_migrate()
2116 return -EAGAIN; in task_numa_migrate()
2122 WRITE_ONCE(best_rq->numa_migrate_on, 0); in task_numa_migrate()
2129 WRITE_ONCE(best_rq->numa_migrate_on, 0); in task_numa_migrate()
2143 if (unlikely(p->numa_preferred_nid == NUMA_NO_NODE || !p->numa_faults)) in numa_migrate_preferred()
2147 interval = min(interval, msecs_to_jiffies(p->numa_scan_period) / 16); in numa_migrate_preferred()
2148 p->numa_migrate_retry = jiffies + interval; in numa_migrate_preferred()
2151 if (task_node(p) == p->numa_preferred_nid) in numa_migrate_preferred()
2181 numa_group->max_faults_cpu = max_faults; in numa_group_count_active_nodes()
2182 numa_group->active_nodes = active_nodes; in numa_group_count_active_nodes()
2208 unsigned long remote = p->numa_faults_locality[0]; in update_task_scan_period()
2209 unsigned long local = p->numa_faults_locality[1]; in update_task_scan_period()
2218 if (local + shared == 0 || p->numa_faults_locality[2]) { in update_task_scan_period()
2219 p->numa_scan_period = min(p->numa_scan_period_max, in update_task_scan_period()
2220 p->numa_scan_period << 1); in update_task_scan_period()
2222 p->mm->numa_next_scan = jiffies + in update_task_scan_period()
2223 msecs_to_jiffies(p->numa_scan_period); in update_task_scan_period()
2229 * Prepare to scale scan period relative to the current period. in update_task_scan_period()
2234 period_slot = DIV_ROUND_UP(p->numa_scan_period, NUMA_PERIOD_SLOTS); in update_task_scan_period()
2243 int slot = ps_ratio - NUMA_PERIOD_THRESHOLD; in update_task_scan_period()
2253 int slot = lr_ratio - NUMA_PERIOD_THRESHOLD; in update_task_scan_period()
2259 * Private memory faults exceed (SLOTS-THRESHOLD)/SLOTS, in update_task_scan_period()
2264 diff = -(NUMA_PERIOD_THRESHOLD - ratio) * period_slot; in update_task_scan_period()
2267 p->numa_scan_period = clamp(p->numa_scan_period + diff, in update_task_scan_period()
2269 memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); in update_task_scan_period()
2276 * from the dozens-of-seconds NUMA balancing period. Use the scheduler
2283 now = p->se.exec_start; in numa_get_avg_runtime()
2284 runtime = p->se.sum_exec_runtime; in numa_get_avg_runtime()
2286 if (p->last_task_numa_placement) { in numa_get_avg_runtime()
2287 delta = runtime - p->last_sum_exec_runtime; in numa_get_avg_runtime()
2288 *period = now - p->last_task_numa_placement; in numa_get_avg_runtime()
2294 delta = p->se.avg.load_sum; in numa_get_avg_runtime()
2298 p->last_sum_exec_runtime = runtime; in numa_get_avg_runtime()
2299 p->last_task_numa_placement = now; in numa_get_avg_runtime()
2349 for (dist = sched_max_numa_distance; dist > LOCAL_DISTANCE; dist--) { in preferred_group_nid()
2403 * The p->mm->numa_scan_seq field gets updated without in task_numa_placement()
2407 seq = READ_ONCE(p->mm->numa_scan_seq); in task_numa_placement()
2408 if (p->numa_scan_seq == seq) in task_numa_placement()
2410 p->numa_scan_seq = seq; in task_numa_placement()
2411 p->numa_scan_period_max = task_scan_max(p); in task_numa_placement()
2413 total_faults = p->numa_faults_locality[0] + in task_numa_placement()
2414 p->numa_faults_locality[1]; in task_numa_placement()
2420 group_lock = &ng->lock; in task_numa_placement()
2440 diff = p->numa_faults[membuf_idx] - p->numa_faults[mem_idx] / 2; in task_numa_placement()
2441 fault_types[priv] += p->numa_faults[membuf_idx]; in task_numa_placement()
2442 p->numa_faults[membuf_idx] = 0; in task_numa_placement()
2448 * little over-all impact on throughput, and thus their in task_numa_placement()
2452 f_weight = (f_weight * p->numa_faults[cpubuf_idx]) / in task_numa_placement()
2454 f_diff = f_weight - p->numa_faults[cpu_idx] / 2; in task_numa_placement()
2455 p->numa_faults[cpubuf_idx] = 0; in task_numa_placement()
2457 p->numa_faults[mem_idx] += diff; in task_numa_placement()
2458 p->numa_faults[cpu_idx] += f_diff; in task_numa_placement()
2459 faults += p->numa_faults[mem_idx]; in task_numa_placement()
2460 p->total_numa_faults += diff; in task_numa_placement()
2469 ng->faults[mem_idx] += diff; in task_numa_placement()
2470 ng->faults_cpu[mem_idx] += f_diff; in task_numa_placement()
2471 ng->total_faults += diff; in task_numa_placement()
2472 group_faults += ng->faults[mem_idx]; in task_numa_placement()
2495 if (max_nid != p->numa_preferred_nid) in task_numa_placement()
2504 return refcount_inc_not_zero(&grp->refcount); in get_numa_group()
2509 if (refcount_dec_and_test(&grp->refcount)) in put_numa_group()
2530 refcount_set(&grp->refcount, 1); in task_numa_group()
2531 grp->active_nodes = 1; in task_numa_group()
2532 grp->max_faults_cpu = 0; in task_numa_group()
2533 spin_lock_init(&grp->lock); in task_numa_group()
2534 grp->gid = p->pid; in task_numa_group()
2536 grp->faults_cpu = grp->faults + NR_NUMA_HINT_FAULT_TYPES * in task_numa_group()
2540 grp->faults[i] = p->numa_faults[i]; in task_numa_group()
2542 grp->total_faults = p->total_numa_faults; in task_numa_group()
2544 grp->nr_tasks++; in task_numa_group()
2545 rcu_assign_pointer(p->numa_group, grp); in task_numa_group()
2549 tsk = READ_ONCE(cpu_rq(cpu)->curr); in task_numa_group()
2554 grp = rcu_dereference(tsk->numa_group); in task_numa_group()
2566 if (my_grp->nr_tasks > grp->nr_tasks) in task_numa_group()
2570 * Tie-break on the grp address. in task_numa_group()
2572 if (my_grp->nr_tasks == grp->nr_tasks && my_grp > grp) in task_numa_group()
2576 if (tsk->mm == current->mm) in task_numa_group()
2595 double_lock_irq(&my_grp->lock, &grp->lock); in task_numa_group()
2598 my_grp->faults[i] -= p->numa_faults[i]; in task_numa_group()
2599 grp->faults[i] += p->numa_faults[i]; in task_numa_group()
2601 my_grp->total_faults -= p->total_numa_faults; in task_numa_group()
2602 grp->total_faults += p->total_numa_faults; in task_numa_group()
2604 my_grp->nr_tasks--; in task_numa_group()
2605 grp->nr_tasks++; in task_numa_group()
2607 spin_unlock(&my_grp->lock); in task_numa_group()
2608 spin_unlock_irq(&grp->lock); in task_numa_group()
2610 rcu_assign_pointer(p->numa_group, grp); in task_numa_group()
2621 * Get rid of NUMA staticstics associated with a task (either current or dead).
2625 * reset the data back to default state without freeing ->numa_faults.
2629 /* safe: p either is current or is being freed by current */ in task_numa_free()
2630 struct numa_group *grp = rcu_dereference_raw(p->numa_group); in task_numa_free()
2631 unsigned long *numa_faults = p->numa_faults; in task_numa_free()
2639 spin_lock_irqsave(&grp->lock, flags); in task_numa_free()
2641 grp->faults[i] -= p->numa_faults[i]; in task_numa_free()
2642 grp->total_faults -= p->total_numa_faults; in task_numa_free()
2644 grp->nr_tasks--; in task_numa_free()
2645 spin_unlock_irqrestore(&grp->lock, flags); in task_numa_free()
2646 RCU_INIT_POINTER(p->numa_group, NULL); in task_numa_free()
2651 p->numa_faults = NULL; in task_numa_free()
2654 p->total_numa_faults = 0; in task_numa_free()
2665 struct task_struct *p = current; in task_numa_fault()
2667 int cpu_node = task_node(current); in task_numa_fault()
2676 if (!p->mm) in task_numa_fault()
2679 /* Allocate buffer to track faults on a per-node basis */ in task_numa_fault()
2680 if (unlikely(!p->numa_faults)) { in task_numa_fault()
2681 int size = sizeof(*p->numa_faults) * in task_numa_fault()
2684 p->numa_faults = kzalloc(size, GFP_KERNEL|__GFP_NOWARN); in task_numa_fault()
2685 if (!p->numa_faults) in task_numa_fault()
2688 p->total_numa_faults = 0; in task_numa_fault()
2689 memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); in task_numa_fault()
2696 if (unlikely(last_cpupid == (-1 & LAST_CPUPID_MASK))) { in task_numa_fault()
2711 if (!priv && !local && ng && ng->active_nodes > 1 && in task_numa_fault()
2720 if (time_after(jiffies, p->numa_migrate_retry)) { in task_numa_fault()
2726 p->numa_pages_migrated += pages; in task_numa_fault()
2728 p->numa_faults_locality[2] += pages; in task_numa_fault()
2730 p->numa_faults[task_faults_idx(NUMA_MEMBUF, mem_node, priv)] += pages; in task_numa_fault()
2731 p->numa_faults[task_faults_idx(NUMA_CPUBUF, cpu_node, priv)] += pages; in task_numa_fault()
2732 p->numa_faults_locality[local] += pages; in task_numa_fault()
2739 * p->mm->numa_scan_seq is written to without exclusive access in reset_ptenuma_scan()
2745 WRITE_ONCE(p->mm->numa_scan_seq, READ_ONCE(p->mm->numa_scan_seq) + 1); in reset_ptenuma_scan()
2746 p->mm->numa_scan_offset = 0; in reset_ptenuma_scan()
2756 struct task_struct *p = current; in task_numa_work()
2757 struct mm_struct *mm = p->mm; in task_numa_work()
2758 u64 runtime = p->se.sum_exec_runtime; in task_numa_work()
2766 work->next = work; in task_numa_work()
2770 * NOTE: make sure not to dereference p->mm before this check, in task_numa_work()
2772 * without p->mm even though we still had it when we enqueued this in task_numa_work()
2775 if (p->flags & PF_EXITING) in task_numa_work()
2778 if (!mm->numa_next_scan) { in task_numa_work()
2779 mm->numa_next_scan = now + in task_numa_work()
2786 migrate = mm->numa_next_scan; in task_numa_work()
2790 if (p->numa_scan_period == 0) { in task_numa_work()
2791 p->numa_scan_period_max = task_scan_max(p); in task_numa_work()
2792 p->numa_scan_period = task_scan_start(p); in task_numa_work()
2795 next_scan = now + msecs_to_jiffies(p->numa_scan_period); in task_numa_work()
2796 if (cmpxchg(&mm->numa_next_scan, migrate, next_scan) != migrate) in task_numa_work()
2803 p->node_stamp += 2 * TICK_NSEC; in task_numa_work()
2805 start = mm->numa_scan_offset; in task_numa_work()
2807 pages <<= 20 - PAGE_SHIFT; /* MB in pages */ in task_numa_work()
2819 vma = mm->mmap; in task_numa_work()
2821 for (; vma; vma = vma->vm_next) { in task_numa_work()
2823 is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_MIXEDMAP)) { in task_numa_work()
2830 * hinting faults in read-only file-backed mappings or the vdso in task_numa_work()
2833 if (!vma->vm_mm || in task_numa_work()
2834 (vma->vm_file && (vma->vm_flags & (VM_READ|VM_WRITE)) == (VM_READ))) in task_numa_work()
2845 start = max(start, vma->vm_start); in task_numa_work()
2847 end = min(end, vma->vm_end); in task_numa_work()
2853 * is not already pte-numa. If the VMA contains in task_numa_work()
2859 pages -= (end - start) >> PAGE_SHIFT; in task_numa_work()
2860 virtpages -= (end - start) >> PAGE_SHIFT; in task_numa_work()
2867 } while (end != vma->vm_end); in task_numa_work()
2878 mm->numa_scan_offset = start; in task_numa_work()
2889 if (unlikely(p->se.sum_exec_runtime != runtime)) { in task_numa_work()
2890 u64 diff = p->se.sum_exec_runtime - runtime; in task_numa_work()
2891 p->node_stamp += 32 * diff; in task_numa_work()
2898 struct mm_struct *mm = p->mm; in init_numa_balancing()
2901 mm_users = atomic_read(&mm->mm_users); in init_numa_balancing()
2903 mm->numa_next_scan = jiffies + msecs_to_jiffies(sysctl_numa_balancing_scan_delay); in init_numa_balancing()
2904 mm->numa_scan_seq = 0; in init_numa_balancing()
2907 p->node_stamp = 0; in init_numa_balancing()
2908 p->numa_scan_seq = mm ? mm->numa_scan_seq : 0; in init_numa_balancing()
2909 p->numa_scan_period = sysctl_numa_balancing_scan_delay; in init_numa_balancing()
2911 p->numa_work.next = &p->numa_work; in init_numa_balancing()
2912 p->numa_faults = NULL; in init_numa_balancing()
2913 RCU_INIT_POINTER(p->numa_group, NULL); in init_numa_balancing()
2914 p->last_task_numa_placement = 0; in init_numa_balancing()
2915 p->last_sum_exec_runtime = 0; in init_numa_balancing()
2917 init_task_work(&p->numa_work, task_numa_work); in init_numa_balancing()
2921 p->numa_preferred_nid = NUMA_NO_NODE; in init_numa_balancing()
2932 delay = min_t(unsigned int, task_scan_max(current), in init_numa_balancing()
2933 current->numa_scan_period * mm_users * NSEC_PER_MSEC); in init_numa_balancing()
2935 p->node_stamp = delay; in init_numa_balancing()
2944 struct callback_head *work = &curr->numa_work; in task_tick_numa()
2950 if ((curr->flags & (PF_EXITING | PF_KTHREAD)) || work->next != work) in task_tick_numa()
2959 now = curr->se.sum_exec_runtime; in task_tick_numa()
2960 period = (u64)curr->numa_scan_period * NSEC_PER_MSEC; in task_tick_numa()
2962 if (now > curr->node_stamp + period) { in task_tick_numa()
2963 if (!curr->node_stamp) in task_tick_numa()
2964 curr->numa_scan_period = task_scan_start(curr); in task_tick_numa()
2965 curr->node_stamp += period; in task_tick_numa()
2967 if (!time_before(jiffies, curr->mm->numa_next_scan)) in task_tick_numa()
2980 if (!p->mm || !p->numa_faults || (p->flags & PF_EXITING)) in update_scan_period()
2989 * is pulled cross-node due to wakeups or load balancing. in update_scan_period()
2991 if (p->numa_scan_seq) { in update_scan_period()
2997 if (dst_nid == p->numa_preferred_nid || in update_scan_period()
2998 (p->numa_preferred_nid != NUMA_NO_NODE && in update_scan_period()
2999 src_nid != p->numa_preferred_nid)) in update_scan_period()
3003 p->numa_scan_period = task_scan_start(p); in update_scan_period()
3028 update_load_add(&cfs_rq->load, se->load.weight); in account_entity_enqueue()
3034 list_add(&se->group_node, &rq->cfs_tasks); in account_entity_enqueue()
3037 cfs_rq->nr_running++; in account_entity_enqueue()
3043 update_load_sub(&cfs_rq->load, se->load.weight); in account_entity_dequeue()
3047 list_del_init(&se->group_node); in account_entity_dequeue()
3050 cfs_rq->nr_running--; in account_entity_dequeue()
3056 * Explicitly do a load-store to ensure the intermediate value never hits
3076 * Explicitly do a load-store to ensure the intermediate value never hits
3084 res = var - val; \
3093 * A variant of sub_positive(), which does not use explicit load-store
3098 *ptr -= min_t(typeof(*ptr), *ptr, _val); \
3105 cfs_rq->avg.load_avg += se->avg.load_avg; in enqueue_load_avg()
3106 cfs_rq->avg.load_sum += se_weight(se) * se->avg.load_sum; in enqueue_load_avg()
3112 sub_positive(&cfs_rq->avg.load_avg, se->avg.load_avg); in dequeue_load_avg()
3113 sub_positive(&cfs_rq->avg.load_sum, se_weight(se) * se->avg.load_sum); in dequeue_load_avg()
3125 if (se->on_rq) { in reweight_entity()
3127 if (cfs_rq->curr == se) in reweight_entity()
3129 update_load_sub(&cfs_rq->load, se->load.weight); in reweight_entity()
3133 update_load_set(&se->load, weight); in reweight_entity()
3137 u32 divider = get_pelt_divider(&se->avg); in reweight_entity()
3139 se->avg.load_avg = div_u64(se_weight(se) * se->avg.load_sum, divider); in reweight_entity()
3144 if (se->on_rq) in reweight_entity()
3145 update_load_add(&cfs_rq->load, se->load.weight); in reweight_entity()
3151 struct sched_entity *se = &p->se; in reweight_task()
3153 struct load_weight *load = &se->load; in reweight_task()
3157 load->inv_weight = sched_prio_to_wmult[prio]; in reweight_task()
3169 * tg->weight * grq->load.weight
3170 * ge->load.weight = ----------------------------- (1)
3171 * \Sum grq->load.weight
3179 * grq->load.weight -> grq->avg.load_avg (2)
3183 * tg->weight * grq->avg.load_avg
3184 * ge->load.weight = ------------------------------ (3)
3185 * tg->load_avg
3187 * Where: tg->load_avg ~= \Sum grq->avg.load_avg
3191 * The problem with it is that because the average is slow -- it was designed
3192 * to be exactly that of course -- this leads to transients in boundary
3194 * one task. It takes time for our CPU's grq->avg.load_avg to build up,
3199 * tg->weight * grq->load.weight
3200 * ge->load.weight = ----------------------------- = tg->weight (4)
3201 * grp->load.weight
3208 * ge->load.weight =
3210 * tg->weight * grq->load.weight
3211 * --------------------------------------------------- (5)
3212 * tg->load_avg - grq->avg.load_avg + grq->load.weight
3214 * But because grq->load.weight can drop to 0, resulting in a divide by zero,
3215 * we need to use grq->avg.load_avg as its lower bound, which then gives:
3218 * tg->weight * grq->load.weight
3219 * ge->load.weight = ----------------------------- (6)
3224 * tg_load_avg' = tg->load_avg - grq->avg.load_avg +
3225 * max(grq->load.weight, grq->avg.load_avg)
3229 * overestimates the ge->load.weight and therefore:
3231 * \Sum ge->load.weight >= tg->weight
3238 struct task_group *tg = cfs_rq->tg; in calc_group_shares()
3240 tg_shares = READ_ONCE(tg->shares); in calc_group_shares()
3242 load = max(scale_load_down(cfs_rq->load.weight), cfs_rq->avg.load_avg); in calc_group_shares()
3244 tg_weight = atomic_long_read(&tg->load_avg); in calc_group_shares()
3247 tg_weight -= cfs_rq->tg_load_avg_contrib; in calc_group_shares()
3255 * MIN_SHARES has to be unscaled here to support per-CPU partitioning in calc_group_shares()
3256 * of a group with small tg->shares value. It is a floor value which is in calc_group_shares()
3260 * E.g. on 64-bit for a group with tg->shares of scale_load(15)=15*1024 in calc_group_shares()
3261 * on an 8-core system with 8 tasks each runnable on one CPU shares has in calc_group_shares()
3273 * Recomputes the group entity based on the current state of its group
3288 shares = READ_ONCE(gcfs_rq->tg->shares); in update_cfs_group()
3290 if (likely(se->load.weight == shares)) in update_cfs_group()
3309 if (&rq->cfs == cfs_rq) { in cfs_rq_util_change()
3319 * As is, the util number is not freq-invariant (we'd have to in cfs_rq_util_change()
3331 * update_tg_load_avg - update the tg's load avg
3334 * This function 'ensures': tg->load_avg := \Sum tg->cfs_rq[]->avg.load.
3335 * However, because tg->load_avg is a global value there are performance
3346 long delta = cfs_rq->avg.load_avg - cfs_rq->tg_load_avg_contrib; in update_tg_load_avg()
3351 if (cfs_rq->tg == &root_task_group) in update_tg_load_avg()
3354 if (abs(delta) > cfs_rq->tg_load_avg_contrib / 64) { in update_tg_load_avg()
3355 atomic_long_add(delta, &cfs_rq->tg->load_avg); in update_tg_load_avg()
3356 cfs_rq->tg_load_avg_contrib = cfs_rq->avg.load_avg; in update_tg_load_avg()
3362 * caller only guarantees p->pi_lock is held; no other assumptions,
3363 * including the state of rq->lock, should be made.
3375 * We are supposed to update the task to "current" time, then its up to in set_task_rq_fair()
3377 * getting what current time is, so simply throw away the out-of-date in set_task_rq_fair()
3381 if (!(se->avg.last_update_time && prev)) in set_task_rq_fair()
3390 p_last_update_time_copy = prev->load_last_update_time_copy; in set_task_rq_fair()
3391 n_last_update_time_copy = next->load_last_update_time_copy; in set_task_rq_fair()
3395 p_last_update_time = prev->avg.last_update_time; in set_task_rq_fair()
3396 n_last_update_time = next->avg.last_update_time; in set_task_rq_fair()
3402 p_last_update_time = prev->avg.last_update_time; in set_task_rq_fair()
3403 n_last_update_time = next->avg.last_update_time; in set_task_rq_fair()
3406 se->avg.last_update_time = n_last_update_time; in set_task_rq_fair()
3414 * ge->avg == grq->avg (1)
3425 * ge->avg.load_avg = ge->load.weight * ge->avg.runnable_avg (2)
3430 * grq->avg.load_avg = grq->load.weight * grq->avg.runnable_avg (3)
3434 * ge->avg.runnable_avg == grq->avg.runnable_avg
3438 * ge->load.weight * grq->avg.load_avg
3439 * ge->avg.load_avg = ----------------------------------- (4)
3440 * grq->load.weight
3453 * Another reason this doesn't work is that runnable isn't a 0-sum entity.
3464 * ge->avg.running_sum <= ge->avg.runnable_sum <= LOAD_AVG_MAX
3471 * grq->avg.runnable_sum = grq->avg.load_sum / grq->load.weight
3479 long delta = gcfs_rq->avg.util_avg - se->avg.util_avg; in update_tg_cfs_util()
3487 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in update_tg_cfs_util()
3490 divider = get_pelt_divider(&cfs_rq->avg); in update_tg_cfs_util()
3493 se->avg.util_avg = gcfs_rq->avg.util_avg; in update_tg_cfs_util()
3494 se->avg.util_sum = se->avg.util_avg * divider; in update_tg_cfs_util()
3497 add_positive(&cfs_rq->avg.util_avg, delta); in update_tg_cfs_util()
3498 cfs_rq->avg.util_sum = cfs_rq->avg.util_avg * divider; in update_tg_cfs_util()
3504 long delta = gcfs_rq->avg.runnable_avg - se->avg.runnable_avg; in update_tg_cfs_runnable()
3512 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in update_tg_cfs_runnable()
3515 divider = get_pelt_divider(&cfs_rq->avg); in update_tg_cfs_runnable()
3518 se->avg.runnable_avg = gcfs_rq->avg.runnable_avg; in update_tg_cfs_runnable()
3519 se->avg.runnable_sum = se->avg.runnable_avg * divider; in update_tg_cfs_runnable()
3522 add_positive(&cfs_rq->avg.runnable_avg, delta); in update_tg_cfs_runnable()
3523 cfs_rq->avg.runnable_sum = cfs_rq->avg.runnable_avg * divider; in update_tg_cfs_runnable()
3529 long delta, running_sum, runnable_sum = gcfs_rq->prop_runnable_sum; in update_tg_cfs_load()
3537 gcfs_rq->prop_runnable_sum = 0; in update_tg_cfs_load()
3540 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in update_tg_cfs_load()
3543 divider = get_pelt_divider(&cfs_rq->avg); in update_tg_cfs_load()
3550 runnable_sum += se->avg.load_sum; in update_tg_cfs_load()
3557 if (scale_load_down(gcfs_rq->load.weight)) { in update_tg_cfs_load()
3558 load_sum = div_s64(gcfs_rq->avg.load_sum, in update_tg_cfs_load()
3559 scale_load_down(gcfs_rq->load.weight)); in update_tg_cfs_load()
3563 runnable_sum = min(se->avg.load_sum, load_sum); in update_tg_cfs_load()
3572 running_sum = se->avg.util_sum >> SCHED_CAPACITY_SHIFT; in update_tg_cfs_load()
3578 delta = load_avg - se->avg.load_avg; in update_tg_cfs_load()
3580 se->avg.load_sum = runnable_sum; in update_tg_cfs_load()
3581 se->avg.load_avg = load_avg; in update_tg_cfs_load()
3583 add_positive(&cfs_rq->avg.load_avg, delta); in update_tg_cfs_load()
3584 cfs_rq->avg.load_sum = cfs_rq->avg.load_avg * divider; in update_tg_cfs_load()
3589 cfs_rq->propagate = 1; in add_tg_cfs_propagate()
3590 cfs_rq->prop_runnable_sum += runnable_sum; in add_tg_cfs_propagate()
3602 if (!gcfs_rq->propagate) in propagate_entity_load_avg()
3605 gcfs_rq->propagate = 0; in propagate_entity_load_avg()
3609 add_tg_cfs_propagate(cfs_rq, gcfs_rq->prop_runnable_sum); in propagate_entity_load_avg()
3633 if (se->avg.load_avg || se->avg.util_avg) in skip_blocked_update()
3640 if (gcfs_rq->propagate) in skip_blocked_update()
3665 * update_cfs_rq_load_avg - update the cfs_rq's load/util averages
3666 * @now: current time, as per cfs_rq_clock_pelt()
3673 * cfs_rq->avg is used for task_h_load() and update_cfs_share() for example.
3677 * Since both these conditions indicate a changed cfs_rq->avg.load we should
3684 struct sched_avg *sa = &cfs_rq->avg; in update_cfs_rq_load_avg()
3687 if (cfs_rq->removed.nr) { in update_cfs_rq_load_avg()
3689 u32 divider = get_pelt_divider(&cfs_rq->avg); in update_cfs_rq_load_avg()
3691 raw_spin_lock(&cfs_rq->removed.lock); in update_cfs_rq_load_avg()
3692 swap(cfs_rq->removed.util_avg, removed_util); in update_cfs_rq_load_avg()
3693 swap(cfs_rq->removed.load_avg, removed_load); in update_cfs_rq_load_avg()
3694 swap(cfs_rq->removed.runnable_avg, removed_runnable); in update_cfs_rq_load_avg()
3695 cfs_rq->removed.nr = 0; in update_cfs_rq_load_avg()
3696 raw_spin_unlock(&cfs_rq->removed.lock); in update_cfs_rq_load_avg()
3699 sub_positive(&sa->load_avg, r); in update_cfs_rq_load_avg()
3700 sa->load_sum = sa->load_avg * divider; in update_cfs_rq_load_avg()
3703 sub_positive(&sa->util_avg, r); in update_cfs_rq_load_avg()
3704 sub_positive(&sa->util_sum, r * divider); in update_cfs_rq_load_avg()
3706 * Because of rounding, se->util_sum might ends up being +1 more than in update_cfs_rq_load_avg()
3707 * cfs->util_sum. Although this is not a problem by itself, detaching in update_cfs_rq_load_avg()
3709 * util_avg (~1ms) can make cfs->util_sum becoming null whereas in update_cfs_rq_load_avg()
3716 sa->util_sum = max_t(u32, sa->util_sum, sa->util_avg * PELT_MIN_DIVIDER); in update_cfs_rq_load_avg()
3719 sub_positive(&sa->runnable_avg, r); in update_cfs_rq_load_avg()
3720 sa->runnable_sum = sa->runnable_avg * divider; in update_cfs_rq_load_avg()
3727 -(long)(removed_runnable * divider) >> SCHED_CAPACITY_SHIFT); in update_cfs_rq_load_avg()
3736 cfs_rq->load_last_update_time_copy = sa->last_update_time; in update_cfs_rq_load_avg()
3743 * attach_entity_load_avg - attach this entity to its cfs_rq load avg
3748 * cfs_rq->avg.last_update_time being current.
3753 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in attach_entity_load_avg()
3756 u32 divider = get_pelt_divider(&cfs_rq->avg); in attach_entity_load_avg()
3765 se->avg.last_update_time = cfs_rq->avg.last_update_time; in attach_entity_load_avg()
3766 se->avg.period_contrib = cfs_rq->avg.period_contrib; in attach_entity_load_avg()
3774 se->avg.util_sum = se->avg.util_avg * divider; in attach_entity_load_avg()
3776 se->avg.runnable_sum = se->avg.runnable_avg * divider; in attach_entity_load_avg()
3778 se->avg.load_sum = se->avg.load_avg * divider; in attach_entity_load_avg()
3779 if (se_weight(se) < se->avg.load_sum) in attach_entity_load_avg()
3780 se->avg.load_sum = div_u64(se->avg.load_sum, se_weight(se)); in attach_entity_load_avg()
3782 se->avg.load_sum = 1; in attach_entity_load_avg()
3785 cfs_rq->avg.util_avg += se->avg.util_avg; in attach_entity_load_avg()
3786 cfs_rq->avg.util_sum += se->avg.util_sum; in attach_entity_load_avg()
3787 cfs_rq->avg.runnable_avg += se->avg.runnable_avg; in attach_entity_load_avg()
3788 cfs_rq->avg.runnable_sum += se->avg.runnable_sum; in attach_entity_load_avg()
3790 add_tg_cfs_propagate(cfs_rq, se->avg.load_sum); in attach_entity_load_avg()
3798 * detach_entity_load_avg - detach this entity from its cfs_rq load avg
3803 * cfs_rq->avg.last_update_time being current.
3808 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in detach_entity_load_avg()
3811 u32 divider = get_pelt_divider(&cfs_rq->avg); in detach_entity_load_avg()
3814 sub_positive(&cfs_rq->avg.util_avg, se->avg.util_avg); in detach_entity_load_avg()
3815 cfs_rq->avg.util_sum = cfs_rq->avg.util_avg * divider; in detach_entity_load_avg()
3816 sub_positive(&cfs_rq->avg.runnable_avg, se->avg.runnable_avg); in detach_entity_load_avg()
3817 cfs_rq->avg.runnable_sum = cfs_rq->avg.runnable_avg * divider; in detach_entity_load_avg()
3819 add_tg_cfs_propagate(cfs_rq, -se->avg.load_sum); in detach_entity_load_avg()
3843 if (se->avg.last_update_time && !(flags & SKIP_AGE_LOAD)) in update_load_avg()
3849 if (!se->avg.last_update_time && (flags & DO_ATTACH)) { in update_load_avg()
3876 last_update_time_copy = cfs_rq->load_last_update_time_copy; in cfs_rq_last_update_time()
3878 last_update_time = cfs_rq->avg.last_update_time; in cfs_rq_last_update_time()
3886 return cfs_rq->avg.last_update_time; in cfs_rq_last_update_time()
3913 * tasks cannot exit without having gone through wake_up_new_task() -> in remove_entity_load_avg()
3920 raw_spin_lock_irqsave(&cfs_rq->removed.lock, flags); in remove_entity_load_avg()
3921 ++cfs_rq->removed.nr; in remove_entity_load_avg()
3922 cfs_rq->removed.util_avg += se->avg.util_avg; in remove_entity_load_avg()
3923 cfs_rq->removed.load_avg += se->avg.load_avg; in remove_entity_load_avg()
3924 cfs_rq->removed.runnable_avg += se->avg.runnable_avg; in remove_entity_load_avg()
3925 raw_spin_unlock_irqrestore(&cfs_rq->removed.lock, flags); in remove_entity_load_avg()
3930 return cfs_rq->avg.runnable_avg; in cfs_rq_runnable_avg()
3935 return cfs_rq->avg.load_avg; in cfs_rq_load_avg()
3944 return p->ravg.demand_scaled; in task_util()
3946 return READ_ONCE(p->se.avg.util_avg); in task_util()
3951 struct util_est ue = READ_ONCE(p->se.avg.util_est); in _task_util_est()
3960 return p->ravg.demand_scaled; in task_util_est()
4002 enqueued = cfs_rq->avg.util_est.enqueued; in util_est_enqueue()
4004 WRITE_ONCE(cfs_rq->avg.util_est.enqueued, enqueued); in util_est_enqueue()
4018 enqueued = cfs_rq->avg.util_est.enqueued; in util_est_dequeue()
4019 enqueued -= min_t(unsigned int, enqueued, _task_util_est(p)); in util_est_dequeue()
4020 WRITE_ONCE(cfs_rq->avg.util_est.enqueued, enqueued); in util_est_dequeue()
4031 * abs(x) < y := (unsigned)(x + y - 1) < (2 * y - 1)
4037 return ((unsigned int)(value + margin - 1) < (2 * margin - 1)); in within_margin()
4061 ue = p->se.avg.util_est; in util_est_update()
4083 last_ewma_diff = ue.enqueued - ue.ewma; in util_est_update()
4084 last_enqueued_diff -= ue.enqueued; in util_est_update()
4103 * of the task size. This is done by storing the current PELT value in util_est_update()
4107 * ewma(t) = w * task_util(p) + (1-w) * ewma(t-1) in util_est_update()
4108 * = w * task_util(p) + ewma(t-1) - w * ewma(t-1) in util_est_update()
4109 * = w * (task_util(p) - ewma(t-1)) + ewma(t-1) in util_est_update()
4110 * = w * ( last_ewma_diff ) + ewma(t-1) in util_est_update()
4111 * = w * (last_ewma_diff + ewma(t-1) / w) in util_est_update()
4121 WRITE_ONCE(p->se.avg.util_est, ue); in util_est_update()
4123 trace_sched_util_est_se_tp(&p->se); in util_est_update()
4171 capacity_orig_thermal = capacity_orig - arch_scale_thermal_pressure(cpu); in util_fits_cpu()
4188 * +---------------------------------------- in util_fits_cpu()
4226 * +---------------------------------------- in util_fits_cpu()
4265 unsigned long max_capacity = cpu_rq(cpu)->rd->max_cpu_capacity; in task_fits_max()
4285 if (!p || p->nr_cpus_allowed == 1) { in update_misfit_status()
4286 rq->misfit_task_load = 0; in update_misfit_status()
4301 rq->misfit_task_load = 0; in update_misfit_status()
4309 rq->misfit_task_load = max_t(unsigned long, task_h_load(p), 1); in update_misfit_status()
4351 s64 d = se->vruntime - cfs_rq->min_vruntime; in check_spread()
4354 d = -d; in check_spread()
4357 schedstat_inc(cfs_rq->nr_spread_over); in check_spread()
4366 if (se->exec_start == 0) in entity_is_long_sleeper()
4374 if (sleep_time <= se->exec_start) in entity_is_long_sleeper()
4377 sleep_time -= se->exec_start; in entity_is_long_sleeper()
4387 u64 vruntime = cfs_rq->min_vruntime; in place_entity()
4390 * The 'current' period is already promised to the current tasks, in place_entity()
4409 vruntime -= thresh; in place_entity()
4432 se->vruntime = vruntime; in place_entity()
4434 se->vruntime = max_vruntime(se->vruntime, vruntime); in place_entity()
4467 * vruntime -= min_vruntime
4475 * min_vruntime are up-to-date.
4479 * ->migrate_task_rq_fair() (p->state == TASK_WAKING)
4480 * vruntime -= min_vruntime
4487 * this way we don't have the most up-to-date min_vruntime on the originating
4488 * CPU and an up-to-date min_vruntime on the destination CPU.
4495 bool curr = cfs_rq->curr == se; in enqueue_entity()
4498 * If we're the current task, we must renormalise before calling in enqueue_entity()
4502 se->vruntime += cfs_rq->min_vruntime; in enqueue_entity()
4507 * Otherwise, renormalise after, such that we're placed at the current in enqueue_entity()
4513 se->vruntime += cfs_rq->min_vruntime; in enqueue_entity()
4517 * - Update loads to have both entity and cfs_rq synced with now. in enqueue_entity()
4518 * - Add its load to cfs_rq->runnable_avg in enqueue_entity()
4519 * - For group_entity, update its weight to reflect the new share of in enqueue_entity()
4521 * - Add its new weight to cfs_rq->load.weight in enqueue_entity()
4532 se->exec_start = 0; in enqueue_entity()
4539 se->on_rq = 1; in enqueue_entity()
4543 * because of a parent been throttled but cfs->nr_running > 1. Try to in enqueue_entity()
4546 if (cfs_rq->nr_running == 1 || cfs_bandwidth_used()) in enqueue_entity()
4549 if (cfs_rq->nr_running == 1) in enqueue_entity()
4557 if (cfs_rq->last != se) in __clear_buddies_last()
4560 cfs_rq->last = NULL; in __clear_buddies_last()
4568 if (cfs_rq->next != se) in __clear_buddies_next()
4571 cfs_rq->next = NULL; in __clear_buddies_next()
4579 if (cfs_rq->skip != se) in __clear_buddies_skip()
4582 cfs_rq->skip = NULL; in __clear_buddies_skip()
4588 if (cfs_rq->last == se) in clear_buddies()
4591 if (cfs_rq->next == se) in clear_buddies()
4594 if (cfs_rq->skip == se) in clear_buddies()
4604 * Update run-time statistics of the 'current'. in dequeue_entity()
4610 * - Update loads to have both entity and cfs_rq synced with now. in dequeue_entity()
4611 * - Subtract its load from the cfs_rq->runnable_avg. in dequeue_entity()
4612 * - Subtract its previous weight from cfs_rq->load.weight. in dequeue_entity()
4613 * - For group entity, update its weight to reflect the new share in dequeue_entity()
4623 if (se != cfs_rq->curr) in dequeue_entity()
4625 se->on_rq = 0; in dequeue_entity()
4635 se->vruntime -= cfs_rq->min_vruntime; in dequeue_entity()
4646 * further than we started -- ie. we'll be penalized. in dequeue_entity()
4653 * Preempt the current task with a newly woken task if needed:
4663 delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime; in check_preempt_tick()
4667 * The current task ran long enough, ensure it doesn't get in check_preempt_tick()
4668 * re-elected due to buddy favours. in check_preempt_tick()
4683 delta = curr->vruntime - se->vruntime; in check_preempt_tick()
4695 /* 'current' is not kept within the tree. */ in set_next_entity()
4696 if (se->on_rq) { in set_next_entity()
4708 cfs_rq->curr = se; in set_next_entity()
4713 * when there are only lesser-weight tasks around): in set_next_entity()
4716 rq_of(cfs_rq)->cfs.load.weight >= 2*se->load.weight) { in set_next_entity()
4717 schedstat_set(se->statistics.slice_max, in set_next_entity()
4718 max((u64)schedstat_val(se->statistics.slice_max), in set_next_entity()
4719 se->sum_exec_runtime - se->prev_sum_exec_runtime)); in set_next_entity()
4722 se->prev_sum_exec_runtime = se->sum_exec_runtime; in set_next_entity()
4754 if (cfs_rq->skip == se) { in pick_next_entity()
4769 if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1) { in pick_next_entity()
4773 se = cfs_rq->next; in pick_next_entity()
4774 } else if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1) { in pick_next_entity()
4778 se = cfs_rq->last; in pick_next_entity()
4794 if (prev->on_rq) in put_prev_entity()
4802 if (prev->on_rq) { in put_prev_entity()
4804 /* Put 'current' back into the tree. */ in put_prev_entity()
4809 cfs_rq->curr = NULL; in put_prev_entity()
4816 * Update run-time statistics of the 'current'. in entity_tick()
4839 hrtimer_active(&rq_of(cfs_rq)->hrtick_timer)) in entity_tick()
4843 if (cfs_rq->nr_running > 1) in entity_tick()
4897 * directly instead of rq->clock to avoid adding additional synchronization
4898 * around rq->lock.
4900 * requires cfs_b->lock
4904 if (cfs_b->quota != RUNTIME_INF) in __refill_cfs_bandwidth_runtime()
4905 cfs_b->runtime = cfs_b->quota; in __refill_cfs_bandwidth_runtime()
4910 return &tg->cfs_bandwidth; in tg_cfs_bandwidth()
4919 lockdep_assert_held(&cfs_b->lock); in __assign_cfs_rq_runtime()
4922 min_amount = target_runtime - cfs_rq->runtime_remaining; in __assign_cfs_rq_runtime()
4924 if (cfs_b->quota == RUNTIME_INF) in __assign_cfs_rq_runtime()
4929 if (cfs_b->runtime > 0) { in __assign_cfs_rq_runtime()
4930 amount = min(cfs_b->runtime, min_amount); in __assign_cfs_rq_runtime()
4931 cfs_b->runtime -= amount; in __assign_cfs_rq_runtime()
4932 cfs_b->idle = 0; in __assign_cfs_rq_runtime()
4936 cfs_rq->runtime_remaining += amount; in __assign_cfs_rq_runtime()
4938 return cfs_rq->runtime_remaining > 0; in __assign_cfs_rq_runtime()
4944 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in assign_cfs_rq_runtime()
4947 raw_spin_lock(&cfs_b->lock); in assign_cfs_rq_runtime()
4949 raw_spin_unlock(&cfs_b->lock); in assign_cfs_rq_runtime()
4957 cfs_rq->runtime_remaining -= delta_exec; in __account_cfs_rq_runtime()
4959 if (likely(cfs_rq->runtime_remaining > 0)) in __account_cfs_rq_runtime()
4962 if (cfs_rq->throttled) in __account_cfs_rq_runtime()
4968 if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr)) in __account_cfs_rq_runtime()
4975 if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled) in account_cfs_rq_runtime()
4983 return cfs_bandwidth_used() && cfs_rq->throttled; in cfs_rq_throttled()
4989 return cfs_bandwidth_used() && cfs_rq->throttle_count; in throttled_hierarchy()
4995 * load-balance operations.
5002 src_cfs_rq = tg->cfs_rq[src_cpu]; in throttled_lb_pair()
5003 dest_cfs_rq = tg->cfs_rq[dest_cpu]; in throttled_lb_pair()
5012 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in tg_unthrottle_up()
5014 cfs_rq->throttle_count--; in tg_unthrottle_up()
5015 if (!cfs_rq->throttle_count) { in tg_unthrottle_up()
5016 cfs_rq->throttled_clock_pelt_time += rq_clock_pelt(rq) - in tg_unthrottle_up()
5017 cfs_rq->throttled_clock_pelt; in tg_unthrottle_up()
5020 if (cfs_rq->nr_running >= 1) in tg_unthrottle_up()
5030 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in tg_throttle_down()
5033 if (!cfs_rq->throttle_count) { in tg_throttle_down()
5034 cfs_rq->throttled_clock_pelt = rq_clock_pelt(rq); in tg_throttle_down()
5037 cfs_rq->throttle_count++; in tg_throttle_down()
5045 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in throttle_cfs_rq()
5049 raw_spin_lock(&cfs_b->lock); in throttle_cfs_rq()
5062 list_add_tail_rcu(&cfs_rq->throttled_list, in throttle_cfs_rq()
5063 &cfs_b->throttled_cfs_rq); in throttle_cfs_rq()
5065 raw_spin_unlock(&cfs_b->lock); in throttle_cfs_rq()
5070 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))]; in throttle_cfs_rq()
5074 walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq); in throttle_cfs_rq()
5077 task_delta = cfs_rq->h_nr_running; in throttle_cfs_rq()
5078 idle_task_delta = cfs_rq->idle_h_nr_running; in throttle_cfs_rq()
5081 /* throttled entity or throttle-on-deactivate */ in throttle_cfs_rq()
5082 if (!se->on_rq) in throttle_cfs_rq()
5092 qcfs_rq->h_nr_running -= task_delta; in throttle_cfs_rq()
5093 qcfs_rq->idle_h_nr_running -= idle_task_delta; in throttle_cfs_rq()
5094 walt_dec_throttled_cfs_rq_stats(&qcfs_rq->walt_stats, cfs_rq); in throttle_cfs_rq()
5096 if (qcfs_rq->load.weight) in throttle_cfs_rq()
5102 walt_dec_throttled_cfs_rq_stats(&rq->walt_stats, cfs_rq); in throttle_cfs_rq()
5107 * throttled-list. rq->lock protects completion. in throttle_cfs_rq()
5109 cfs_rq->throttled = 1; in throttle_cfs_rq()
5110 cfs_rq->throttled_clock = rq_clock(rq); in throttle_cfs_rq()
5117 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in unthrottle_cfs_rq()
5122 se = cfs_rq->tg->se[cpu_of(rq)]; in unthrottle_cfs_rq()
5124 cfs_rq->throttled = 0; in unthrottle_cfs_rq()
5128 raw_spin_lock(&cfs_b->lock); in unthrottle_cfs_rq()
5129 cfs_b->throttled_time += rq_clock(rq) - cfs_rq->throttled_clock; in unthrottle_cfs_rq()
5130 list_del_rcu(&cfs_rq->throttled_list); in unthrottle_cfs_rq()
5131 raw_spin_unlock(&cfs_b->lock); in unthrottle_cfs_rq()
5134 walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq); in unthrottle_cfs_rq()
5136 if (!cfs_rq->load.weight) in unthrottle_cfs_rq()
5139 task_delta = cfs_rq->h_nr_running; in unthrottle_cfs_rq()
5140 idle_task_delta = cfs_rq->idle_h_nr_running; in unthrottle_cfs_rq()
5142 if (se->on_rq) in unthrottle_cfs_rq()
5147 cfs_rq->h_nr_running += task_delta; in unthrottle_cfs_rq()
5148 cfs_rq->idle_h_nr_running += idle_task_delta; in unthrottle_cfs_rq()
5149 walt_inc_throttled_cfs_rq_stats(&cfs_rq->walt_stats, tcfs_rq); in unthrottle_cfs_rq()
5162 cfs_rq->h_nr_running += task_delta; in unthrottle_cfs_rq()
5163 cfs_rq->idle_h_nr_running += idle_task_delta; in unthrottle_cfs_rq()
5164 walt_inc_throttled_cfs_rq_stats(&cfs_rq->walt_stats, tcfs_rq); in unthrottle_cfs_rq()
5180 walt_inc_throttled_cfs_rq_stats(&rq->walt_stats, tcfs_rq); in unthrottle_cfs_rq()
5198 if (rq->curr == rq->idle && rq->cfs.nr_running) in unthrottle_cfs_rq()
5208 list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq, in distribute_cfs_runtime()
5218 SCHED_WARN_ON(cfs_rq->runtime_remaining > 0); in distribute_cfs_runtime()
5220 raw_spin_lock(&cfs_b->lock); in distribute_cfs_runtime()
5221 runtime = -cfs_rq->runtime_remaining + 1; in distribute_cfs_runtime()
5222 if (runtime > cfs_b->runtime) in distribute_cfs_runtime()
5223 runtime = cfs_b->runtime; in distribute_cfs_runtime()
5224 cfs_b->runtime -= runtime; in distribute_cfs_runtime()
5225 remaining = cfs_b->runtime; in distribute_cfs_runtime()
5226 raw_spin_unlock(&cfs_b->lock); in distribute_cfs_runtime()
5228 cfs_rq->runtime_remaining += runtime; in distribute_cfs_runtime()
5231 if (cfs_rq->runtime_remaining > 0) in distribute_cfs_runtime()
5246 * period the timer is deactivated until scheduling resumes; cfs_b->idle is
5254 if (cfs_b->quota == RUNTIME_INF) in do_sched_cfs_period_timer()
5257 throttled = !list_empty(&cfs_b->throttled_cfs_rq); in do_sched_cfs_period_timer()
5258 cfs_b->nr_periods += overrun; in do_sched_cfs_period_timer()
5264 if (cfs_b->idle && !throttled) in do_sched_cfs_period_timer()
5271 cfs_b->idle = 1; in do_sched_cfs_period_timer()
5276 cfs_b->nr_throttled += overrun; in do_sched_cfs_period_timer()
5279 * This check is repeated as we release cfs_b->lock while we unthrottle. in do_sched_cfs_period_timer()
5281 while (throttled && cfs_b->runtime > 0) { in do_sched_cfs_period_timer()
5282 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in do_sched_cfs_period_timer()
5283 /* we can't nest cfs_b->lock while distributing bandwidth */ in do_sched_cfs_period_timer()
5285 raw_spin_lock_irqsave(&cfs_b->lock, flags); in do_sched_cfs_period_timer()
5287 throttled = !list_empty(&cfs_b->throttled_cfs_rq); in do_sched_cfs_period_timer()
5296 cfs_b->idle = 0; in do_sched_cfs_period_timer()
5312 * Are we near the end of the current quota period?
5314 * Requires cfs_b->lock for hrtimer_expires_remaining to be safe against the
5320 struct hrtimer *refresh_timer = &cfs_b->period_timer; in runtime_refresh_within()
5323 /* if the call-back is running a quota refresh is already occurring */ in runtime_refresh_within()
5344 if (cfs_b->slack_started) in start_cfs_slack_bandwidth()
5346 cfs_b->slack_started = true; in start_cfs_slack_bandwidth()
5348 hrtimer_start(&cfs_b->slack_timer, in start_cfs_slack_bandwidth()
5356 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in __return_cfs_rq_runtime()
5357 s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime; in __return_cfs_rq_runtime()
5362 raw_spin_lock(&cfs_b->lock); in __return_cfs_rq_runtime()
5363 if (cfs_b->quota != RUNTIME_INF) { in __return_cfs_rq_runtime()
5364 cfs_b->runtime += slack_runtime; in __return_cfs_rq_runtime()
5366 /* we are under rq->lock, defer unthrottling using a timer */ in __return_cfs_rq_runtime()
5367 if (cfs_b->runtime > sched_cfs_bandwidth_slice() && in __return_cfs_rq_runtime()
5368 !list_empty(&cfs_b->throttled_cfs_rq)) in __return_cfs_rq_runtime()
5371 raw_spin_unlock(&cfs_b->lock); in __return_cfs_rq_runtime()
5374 cfs_rq->runtime_remaining -= slack_runtime; in __return_cfs_rq_runtime()
5382 if (!cfs_rq->runtime_enabled || cfs_rq->nr_running) in return_cfs_rq_runtime()
5390 * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
5398 raw_spin_lock_irqsave(&cfs_b->lock, flags); in do_sched_cfs_slack_timer()
5399 cfs_b->slack_started = false; in do_sched_cfs_slack_timer()
5402 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in do_sched_cfs_slack_timer()
5406 if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) in do_sched_cfs_slack_timer()
5407 runtime = cfs_b->runtime; in do_sched_cfs_slack_timer()
5409 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in do_sched_cfs_slack_timer()
5416 raw_spin_lock_irqsave(&cfs_b->lock, flags); in do_sched_cfs_slack_timer()
5417 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in do_sched_cfs_slack_timer()
5423 * runtime as update_curr() throttling can not trigger until it's on-rq.
5430 /* an active group must be handled by the update_curr()->put() path */ in check_enqueue_throttle()
5431 if (!cfs_rq->runtime_enabled || cfs_rq->curr) in check_enqueue_throttle()
5440 if (cfs_rq->runtime_remaining <= 0) in check_enqueue_throttle()
5451 if (!tg->parent) in sync_throttle()
5454 cfs_rq = tg->cfs_rq[cpu]; in sync_throttle()
5455 pcfs_rq = tg->parent->cfs_rq[cpu]; in sync_throttle()
5457 cfs_rq->throttle_count = pcfs_rq->throttle_count; in sync_throttle()
5458 cfs_rq->throttled_clock_pelt = rq_clock_pelt(cpu_rq(cpu)); in sync_throttle()
5467 if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0)) in check_cfs_rq_runtime()
5501 raw_spin_lock_irqsave(&cfs_b->lock, flags); in sched_cfs_period_timer()
5503 overrun = hrtimer_forward_now(timer, cfs_b->period); in sched_cfs_period_timer()
5510 u64 new, old = ktime_to_ns(cfs_b->period); in sched_cfs_period_timer()
5519 cfs_b->period = ns_to_ktime(new); in sched_cfs_period_timer()
5520 cfs_b->quota *= 2; in sched_cfs_period_timer()
5526 div_u64(cfs_b->quota, NSEC_PER_USEC)); in sched_cfs_period_timer()
5532 div_u64(cfs_b->quota, NSEC_PER_USEC)); in sched_cfs_period_timer()
5540 cfs_b->period_active = 0; in sched_cfs_period_timer()
5541 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in sched_cfs_period_timer()
5548 raw_spin_lock_init(&cfs_b->lock); in init_cfs_bandwidth()
5549 cfs_b->runtime = 0; in init_cfs_bandwidth()
5550 cfs_b->quota = RUNTIME_INF; in init_cfs_bandwidth()
5551 cfs_b->period = ns_to_ktime(default_cfs_period()); in init_cfs_bandwidth()
5553 INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq); in init_cfs_bandwidth()
5554 hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED); in init_cfs_bandwidth()
5555 cfs_b->period_timer.function = sched_cfs_period_timer; in init_cfs_bandwidth()
5556 hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); in init_cfs_bandwidth()
5557 cfs_b->slack_timer.function = sched_cfs_slack_timer; in init_cfs_bandwidth()
5558 cfs_b->slack_started = false; in init_cfs_bandwidth()
5563 cfs_rq->runtime_enabled = 0; in init_cfs_rq_runtime()
5564 INIT_LIST_HEAD(&cfs_rq->throttled_list); in init_cfs_rq_runtime()
5570 lockdep_assert_held(&cfs_b->lock); in start_cfs_bandwidth()
5572 if (cfs_b->period_active) in start_cfs_bandwidth()
5575 cfs_b->period_active = 1; in start_cfs_bandwidth()
5576 hrtimer_forward_now(&cfs_b->period_timer, cfs_b->period); in start_cfs_bandwidth()
5577 hrtimer_start_expires(&cfs_b->period_timer, HRTIMER_MODE_ABS_PINNED); in start_cfs_bandwidth()
5583 if (!cfs_b->throttled_cfs_rq.next) in destroy_cfs_bandwidth()
5586 hrtimer_cancel(&cfs_b->period_timer); in destroy_cfs_bandwidth()
5587 hrtimer_cancel(&cfs_b->slack_timer); in destroy_cfs_bandwidth()
5602 lockdep_assert_held(&rq->lock); in update_runtime_enabled()
5606 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth; in update_runtime_enabled()
5607 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in update_runtime_enabled()
5609 raw_spin_lock(&cfs_b->lock); in update_runtime_enabled()
5610 cfs_rq->runtime_enabled = cfs_b->quota != RUNTIME_INF; in update_runtime_enabled()
5611 raw_spin_unlock(&cfs_b->lock); in update_runtime_enabled()
5621 lockdep_assert_held(&rq->lock); in unthrottle_offline_cfs_rqs()
5625 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in unthrottle_offline_cfs_rqs()
5627 if (!cfs_rq->runtime_enabled) in unthrottle_offline_cfs_rqs()
5634 cfs_rq->runtime_remaining = 1; in unthrottle_offline_cfs_rqs()
5639 cfs_rq->runtime_enabled = 0; in unthrottle_offline_cfs_rqs()
5699 struct sched_entity *se = &p->se; in hrtick_start_fair()
5704 if (rq->cfs.h_nr_running > 1) { in hrtick_start_fair()
5706 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime; in hrtick_start_fair()
5707 s64 delta = slice - ran; in hrtick_start_fair()
5710 if (rq->curr == p) in hrtick_start_fair()
5720 * current task is from our class and nr_running is low enough
5725 struct task_struct *curr = rq->curr; in hrtick_update()
5727 if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class) in hrtick_update()
5730 if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency) in hrtick_update()
5755 if (!READ_ONCE(rq->rd->overutilized) && cpu_overutilized(rq->cpu)) { in update_overutilized_status()
5756 WRITE_ONCE(rq->rd->overutilized, SG_OVERUTILIZED); in update_overutilized_status()
5757 trace_sched_overutilized_tp(rq->rd, SG_OVERUTILIZED); in update_overutilized_status()
5767 return unlikely(rq->nr_running == rq->cfs.idle_h_nr_running && in sched_idle_rq()
5768 rq->nr_running); in sched_idle_rq()
5785 if (se->latency_weight <= 0) in check_preempt_from_idle()
5788 if (cfs->nr_running <= 1) in check_preempt_from_idle()
5799 if (cfs->next) in check_preempt_from_idle()
5800 next = cfs->next; in check_preempt_from_idle()
5818 struct sched_entity *se = &p->se; in enqueue_task_fair()
5828 util_est_enqueue(&rq->cfs, p); in enqueue_task_fair()
5835 if (p->in_iowait) in enqueue_task_fair()
5839 if (se->on_rq) in enqueue_task_fair()
5844 cfs_rq->h_nr_running++; in enqueue_task_fair()
5845 cfs_rq->idle_h_nr_running += idle_h_nr_running; in enqueue_task_fair()
5862 cfs_rq->h_nr_running++; in enqueue_task_fair()
5863 cfs_rq->idle_h_nr_running += idle_h_nr_running; in enqueue_task_fair()
5899 if (rq->curr == rq->idle) in enqueue_task_fair()
5900 check_preempt_from_idle(cfs_rq_of(&p->se), &p->se); in enqueue_task_fair()
5932 struct sched_entity *se = &p->se; in dequeue_task_fair()
5937 util_est_dequeue(&rq->cfs, p); in dequeue_task_fair()
5943 cfs_rq->h_nr_running--; in dequeue_task_fair()
5944 cfs_rq->idle_h_nr_running -= idle_h_nr_running; in dequeue_task_fair()
5952 if (cfs_rq->load.weight) { in dequeue_task_fair()
5953 /* Avoid re-evaluating load for this entity: */ in dequeue_task_fair()
5973 cfs_rq->h_nr_running--; in dequeue_task_fair()
5974 cfs_rq->idle_h_nr_running -= idle_h_nr_running; in dequeue_task_fair()
5989 rq->next_balance = jiffies; in dequeue_task_fair()
5992 util_est_update(&rq->cfs, p, task_sleep); in dequeue_task_fair()
6016 return cfs_rq_load_avg(&rq->cfs); in cpu_load()
6020 * cpu_load_without - compute CPU load without any contributions from *p
6038 if (cpu_of(rq) != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in cpu_load_without()
6041 cfs_rq = &rq->cfs; in cpu_load_without()
6042 load = READ_ONCE(cfs_rq->avg.load_avg); in cpu_load_without()
6052 return cfs_rq_runnable_avg(&rq->cfs); in cpu_runnable()
6061 if (cpu_of(rq) != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in cpu_runnable_without()
6064 cfs_rq = &rq->cfs; in cpu_runnable_without()
6065 runnable = READ_ONCE(cfs_rq->avg.runnable_avg); in cpu_runnable_without()
6068 lsub_positive(&runnable, p->se.avg.runnable_avg); in cpu_runnable_without()
6079 if (time_after(jiffies, current->wakee_flip_decay_ts + HZ)) { in record_wakee()
6080 current->wakee_flips >>= 1; in record_wakee()
6081 current->wakee_flip_decay_ts = jiffies; in record_wakee()
6084 if (current->last_wakee != p) { in record_wakee()
6085 current->last_wakee = p; in record_wakee()
6086 current->wakee_flips++; in record_wakee()
6091 * Detect M:N waker/wakee relationships via a switching-frequency heuristic.
6101 * non-monogamous, with partner count exceeding socket size.
6109 unsigned int master = current->wakee_flips; in wake_wide()
6110 unsigned int slave = p->wakee_flips; in wake_wide()
6125 * wake_affine_idle() - only considers 'now', it check if the waking CPU is
6126 * cache-affine and is (or will be) idle.
6128 * wake_affine_weight() - considers the weight to reflect the average
6150 if (sync && cpu_rq(this_cpu)->nr_running == 1) in wake_affine_idle()
6166 unsigned long current_load = task_h_load(current); in wake_affine_weight()
6171 this_eff_load -= current_load; in wake_affine_weight()
6182 prev_eff_load -= task_load; in wake_affine_weight()
6184 prev_eff_load *= 100 + (sd->imbalance_pct - 100) / 2; in wake_affine_weight()
6210 schedstat_inc(p->se.statistics.nr_wakeups_affine_attempts); in wake_affine()
6214 schedstat_inc(sd->ttwu_move_affine); in wake_affine()
6215 schedstat_inc(p->se.statistics.nr_wakeups_affine); in wake_affine()
6223 * find_idlest_group_cpu - find the idlest CPU among the CPUs in the group.
6232 int shallowest_idle_cpu = -1; in find_idlest_group_cpu()
6236 if (group->group_weight == 1) in find_idlest_group_cpu()
6240 for_each_cpu_and(i, sched_group_span(group), p->cpus_ptr) { in find_idlest_group_cpu()
6250 if (idle && idle->exit_latency < min_exit_latency) { in find_idlest_group_cpu()
6256 min_exit_latency = idle->exit_latency; in find_idlest_group_cpu()
6257 latest_idle_timestamp = rq->idle_stamp; in find_idlest_group_cpu()
6259 } else if ((!idle || idle->exit_latency == min_exit_latency) && in find_idlest_group_cpu()
6260 rq->idle_stamp > latest_idle_timestamp) { in find_idlest_group_cpu()
6266 latest_idle_timestamp = rq->idle_stamp; in find_idlest_group_cpu()
6269 } else if (shallowest_idle_cpu == -1) { in find_idlest_group_cpu()
6278 return shallowest_idle_cpu != -1 ? shallowest_idle_cpu : least_loaded_cpu; in find_idlest_group_cpu()
6286 if (!cpumask_intersects(sched_domain_span(sd), p->cpus_ptr)) in find_idlest_cpu()
6294 sync_entity_load_avg(&p->se); in find_idlest_cpu()
6301 if (!(sd->flags & sd_flag)) { in find_idlest_cpu()
6302 sd = sd->child; in find_idlest_cpu()
6308 sd = sd->child; in find_idlest_cpu()
6315 sd = sd->child; in find_idlest_cpu()
6321 weight = sd->span_weight; in find_idlest_cpu()
6324 if (weight <= tmp->span_weight) in find_idlest_cpu()
6326 if (tmp->flags & sd_flag) in find_idlest_cpu()
6344 WRITE_ONCE(sds->has_idle_cores, val); in set_idle_cores()
6353 return READ_ONCE(sds->has_idle_cores); in test_idle_cores()
6360 * information in sd_llc_shared->has_idle_cores.
6390 * sd_llc->shared->has_idle_cores and enabled through update_idle_core() above.
6398 return -1; in select_idle_core()
6401 return -1; in select_idle_core()
6403 cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr); in select_idle_core()
6428 return -1; in select_idle_core()
6439 return -1; in select_idle_smt()
6442 if (!cpumask_test_cpu(cpu, p->cpus_ptr) || in select_idle_smt()
6451 return -1; in select_idle_smt()
6458 return -1; in select_idle_core()
6463 return -1; in select_idle_smt()
6469 * Scan the LLC domain for idle CPUs; this is dynamically regulated by
6470 * comparing the average scan cost (tracked in sd->avg_scan_cost) against the
6471 * average idle time for this rq (as found in rq->avg_idle).
6484 return -1; in select_idle_cpu()
6490 avg_idle = this_rq()->avg_idle / 512; in select_idle_cpu()
6491 avg_cost = this_sd->avg_scan_cost + 1; in select_idle_cpu()
6494 return -1; in select_idle_cpu()
6497 u64 span_avg = sd->span_weight * avg_idle; in select_idle_cpu()
6506 cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr); in select_idle_cpu()
6509 if (!--nr) in select_idle_cpu()
6510 return -1; in select_idle_cpu()
6517 time = cpu_clock(this) - time; in select_idle_cpu()
6518 update_avg(&this_sd->avg_scan_cost, time); in select_idle_cpu()
6532 int cpu, best_cpu = -1; in select_idle_capacity()
6536 cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr); in select_idle_capacity()
6587 sync_entity_load_avg(&p->se); in select_idle_sibling()
6606 * Allow a per-cpu kthread to stack with the wakee if the in select_idle_sibling()
6609 * per-cpu kthread that is now complete and the wakeup is in select_idle_sibling()
6613 if (is_per_cpu_kthread(current) && in select_idle_sibling()
6616 this_rq()->nr_running <= 1 && in select_idle_sibling()
6622 recent_used_cpu = p->recent_used_cpu; in select_idle_sibling()
6627 cpumask_test_cpu(p->recent_used_cpu, p->cpus_ptr) && in select_idle_sibling()
6633 p->recent_used_cpu = prev; in select_idle_sibling()
6685 * recent utilization of currently non-runnable tasks on a CPU. It represents
6690 * current capacity (capacity_curr <= capacity_orig) of the CPU because it is
6710 * migrations (scheduler-driven DVFS).
6722 cpu_rq(cpu)->walt_stats.cumulative_runnable_avg_scaled; in cpu_util()
6729 cfs_rq = &cpu_rq(cpu)->cfs; in cpu_util()
6730 util = READ_ONCE(cfs_rq->avg.util_avg); in cpu_util()
6733 util = max(util, READ_ONCE(cfs_rq->avg.util_est.enqueued)); in cpu_util()
6764 p->state == TASK_WAKING) in cpu_util_without()
6769 if (cpu != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in cpu_util_without()
6774 util = max_t(long, cpu_util(cpu) - task_util(p), 0); in cpu_util_without()
6779 cfs_rq = &cpu_rq(cpu)->cfs; in cpu_util_without()
6780 util = READ_ONCE(cfs_rq->avg.util_avg); in cpu_util_without()
6791 * cpu_util_without = (cpu_util - task_util) = 0 in cpu_util_without()
6798 * cpu_util_without = (cpu_util - task_util) >= 0 in cpu_util_without()
6813 READ_ONCE(cfs_rq->avg.util_est.enqueued); in cpu_util_without()
6821 * p->on_rq = TASK_ON_RQ_MIGRATING; in cpu_util_without()
6822 * ---------------------------------- A in cpu_util_without()
6826 * ---------------------------------- B in cpu_util_without()
6828 * The additional check on "current == p" it's required to in cpu_util_without()
6832 if (unlikely(task_on_rq_queued(p) || current == p)) in cpu_util_without()
6849 return max_t(long, capacity_of(cpu) - cpu_util_without(cpu, p), 0); in capacity_spare_without()
6858 struct cfs_rq *cfs_rq = &cpu_rq(cpu)->cfs; in cpu_util_next()
6859 unsigned long util_est, util = READ_ONCE(cfs_rq->avg.util_avg); in cpu_util_next()
6873 util_est = READ_ONCE(cfs_rq->avg.util_est.enqueued); in cpu_util_next()
6876 * During wake-up, the task isn't enqueued yet and doesn't in cpu_util_next()
6877 * appear in the cfs_rq->avg.util_est.enqueued of any rq, in cpu_util_next()
6891 * Returns the current capacity of cpu after applying both
6896 unsigned long max_cap = cpu_rq(cpu)->cpu_capacity_orig; in capacity_curr_of()
6918 * The capacity state of CPUs of the current rd can be driven by CPUs in compute_energy()
6923 * If an entire pd is outside of the current rd, it will not appear in in compute_energy()
6951 return em_cpu_energy(pd->em_pd, max_util, sum_util); in compute_energy()
6955 * find_energy_efficient_cpu(): Find most energy-efficient target CPU for the
6959 * out which of the CPU candidates is the most energy-efficient.
6974 * ways to tell with the current Energy Model if this is actually a good
6976 * cluster-packing, and spreading inside a cluster. That should at least be
6983 * NOTE: Forkees are not accepted in the energy-aware wake-up path because
6987 * to be energy-inefficient in some use-cases. The alternative would be to
6990 * other use-cases too. So, until someone finds a better way to solve this,
6991 * let's keep things simple by re-using the existing slow path.
6998 struct root_domain *rd = cpu_rq(smp_processor_id())->rd; in find_energy_efficient_cpu()
7005 pd = rcu_dereference(rd->pd); in find_energy_efficient_cpu()
7006 if (!pd || READ_ONCE(rd->overutilized)) in find_energy_efficient_cpu()
7010 * Energy-aware wake-up happens on the lowest sched_domain starting in find_energy_efficient_cpu()
7015 sd = sd->parent; in find_energy_efficient_cpu()
7019 sync_entity_load_avg(&p->se); in find_energy_efficient_cpu()
7023 for (; pd; pd = pd->next) { in find_energy_efficient_cpu()
7028 int max_spare_cap_cpu = -1; in find_energy_efficient_cpu()
7031 base_energy_pd = compute_energy(p, -1, pd); in find_energy_efficient_cpu()
7037 if (!cpumask_test_cpu(cpu, p->cpus_ptr)) in find_energy_efficient_cpu()
7058 * max-aggregated uclamp_{min, max}. in find_energy_efficient_cpu()
7072 prev_delta -= base_energy_pd; in find_energy_efficient_cpu()
7089 cur_delta -= base_energy_pd; in find_energy_efficient_cpu()
7106 if ((prev_delta - best_delta) > ((prev_delta + base_energy) >> 4)) in find_energy_efficient_cpu()
7114 return -1; in find_energy_efficient_cpu()
7136 int sync = (wake_flags & WF_SYNC) && !(current->flags & PF_EXITING); in select_task_rq_fair()
7138 int target_cpu = -1; in select_task_rq_fair()
7154 want_affine = !wake_wide(p) && cpumask_test_cpu(cpu, p->cpus_ptr); in select_task_rq_fair()
7163 if (want_affine && (tmp->flags & SD_WAKE_AFFINE) && in select_task_rq_fair()
7172 if (tmp->flags & sd_flag) in select_task_rq_fair()
7187 current->recent_used_cpu = cpu; in select_task_rq_fair()
7199 * previous CPU. The caller guarantees p->pi_lock or task_rq(p)->lock is held.
7206 * min_vruntime -- the latter is done by enqueue_entity() when placing in migrate_task_rq_fair()
7209 if (p->state == TASK_WAKING) { in migrate_task_rq_fair()
7210 struct sched_entity *se = &p->se; in migrate_task_rq_fair()
7218 min_vruntime_copy = cfs_rq->min_vruntime_copy; in migrate_task_rq_fair()
7220 min_vruntime = cfs_rq->min_vruntime; in migrate_task_rq_fair()
7223 min_vruntime = cfs_rq->min_vruntime; in migrate_task_rq_fair()
7226 se->vruntime -= min_vruntime; in migrate_task_rq_fair()
7229 if (p->on_rq == TASK_ON_RQ_MIGRATING) { in migrate_task_rq_fair()
7232 * rq->lock and can modify state directly. in migrate_task_rq_fair()
7234 lockdep_assert_held(&task_rq(p)->lock); in migrate_task_rq_fair()
7235 detach_entity_cfs_rq(&p->se); in migrate_task_rq_fair()
7239 * We are supposed to update the task to "current" time, then in migrate_task_rq_fair()
7241 * have difficulty in getting what current time is, so simply in migrate_task_rq_fair()
7242 * throw away the out-of-date time. This will result in the in migrate_task_rq_fair()
7246 remove_entity_load_avg(&p->se); in migrate_task_rq_fair()
7250 p->se.avg.last_update_time = 0; in migrate_task_rq_fair()
7257 remove_entity_load_avg(&p->se); in task_dead_fair()
7263 if (rq->nr_running) in balance_fair()
7273 int latency_weight = se->latency_weight; in wakeup_latency_gran()
7282 if ((se->latency_weight > 0) || (curr->latency_weight > 0)) in wakeup_latency_gran()
7283 latency_weight -= curr->latency_weight; in wakeup_latency_gran()
7293 * [-sysctl_sched_latency:sysctl_sched_latency] in wakeup_latency_gran()
7296 -1 * NICE_LATENCY_WEIGHT_MAX, in wakeup_latency_gran()
7308 * Since its curr running now, convert the gran from real-time in wakeup_gran()
7309 * to virtual-time in his units. in wakeup_gran()
7330 * |<--->|c
7332 * w(c, s1) = -1
7340 s64 gran, vdiff = curr->vruntime - se->vruntime; in wakeup_preempt_entity()
7348 return -1; in wakeup_preempt_entity()
7363 if (SCHED_WARN_ON(!se->on_rq)) in set_last_buddy()
7365 cfs_rq_of(se)->last = se; in set_last_buddy()
7375 if (SCHED_WARN_ON(!se->on_rq)) in set_next_buddy()
7377 cfs_rq_of(se)->next = se; in set_next_buddy()
7384 cfs_rq_of(se)->skip = se; in set_skip_buddy()
7388 * Preempt the current task with a newly woken task if needed:
7392 struct task_struct *curr = rq->curr; in check_preempt_wakeup()
7393 struct sched_entity *se = &curr->se, *pse = &p->se; in check_preempt_wakeup()
7395 int scale = cfs_rq->nr_running >= sched_nr_latency; in check_preempt_wakeup()
7405 * next-buddy nomination below. in check_preempt_wakeup()
7419 * Note: this also catches the edge-case of curr being in a throttled in check_preempt_wakeup()
7428 /* Idle tasks are by definition preempted by non-idle tasks. */ in check_preempt_wakeup()
7434 * Batch and idle tasks do not preempt non-idle tasks (their preemption in check_preempt_wakeup()
7437 if (unlikely(p->policy != SCHED_NORMAL) || !sched_feat(WAKEUP_PREEMPTION)) in check_preempt_wakeup()
7458 * Only set the backward buddy when the current task is still in check_preempt_wakeup()
7460 * with schedule on the ->pre_schedule() or idle_balance() in check_preempt_wakeup()
7466 if (unlikely(!se->on_rq || curr == rq->idle)) in check_preempt_wakeup()
7476 struct cfs_rq *cfs_rq = &rq->cfs; in pick_next_task_fair()
7486 if (!prev || prev->sched_class != &fair_sched_class) in pick_next_task_fair()
7491 * likely that a next task is from the same cgroup as the current. in pick_next_task_fair()
7498 struct sched_entity *curr = cfs_rq->curr; in pick_next_task_fair()
7502 * have to consider cfs_rq->curr. If it is still a runnable in pick_next_task_fair()
7507 if (curr->on_rq) in pick_next_task_fair()
7519 cfs_rq = &rq->cfs; in pick_next_task_fair()
7521 if (!cfs_rq->nr_running) in pick_next_task_fair()
7540 struct sched_entity *pse = &prev->se; in pick_next_task_fair()
7543 int se_depth = se->depth; in pick_next_task_fair()
7544 int pse_depth = pse->depth; in pick_next_task_fair()
7581 list_move(&p->se.group_node, &rq->cfs_tasks); in pick_next_task_fair()
7598 * Because newidle_balance() releases (and re-acquires) rq->lock, it is in pick_next_task_fair()
7600 * must re-start the pick_next_entity() loop. in pick_next_task_fair()
7627 struct sched_entity *se = &prev->se; in put_prev_task_fair()
7639 * The magic of dealing with the ->skip buddy is in pick_next_entity.
7643 struct task_struct *curr = rq->curr; in yield_task_fair()
7645 struct sched_entity *se = &curr->se; in yield_task_fair()
7650 if (unlikely(rq->nr_running == 1)) in yield_task_fair()
7655 if (curr->policy != SCHED_BATCH) { in yield_task_fair()
7658 * Update run-time statistics of the 'current'. in yield_task_fair()
7674 struct sched_entity *se = &p->se; in yield_to_task_fair()
7677 if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se))) in yield_to_task_fair()
7690 * Fair scheduling class load-balancing methods.
7694 * The purpose of load-balancing is to achieve the same basic fairness the
7695 * per-CPU scheduler provides, namely provide a proportional amount of compute
7700 * Where W_i,n is the n-th weight average for CPU i. The instantaneous weight
7705 * Where w_i,j is the weight of the j-th runnable task on CPU i. This weight
7711 * W'_i,n = (2^n - 1) / 2^n * W_i,n + 1 / 2^n * W_i,0 (3)
7720 * imb_i,j = max{ avg(W/C), W_i/C_i } - min{ avg(W/C), W_j/C_j } (4)
7727 * - infeasible weights;
7728 * - local vs global optima in the discrete case. ]
7738 * of load-balance at each level inv. proportional to the number of CPUs in
7744 * \Sum { --- * --- * 2^i } = O(n) (5)
7746 * `- size of each group
7747 * | | `- number of CPUs doing load-balance
7748 * | `- freq
7749 * `- sum over all levels
7791 * W_i,0 = \Sum_j \Prod_k w_k * ----- (9)
7798 * w_i,j,k is the weight of the j-th runnable task in the k-th cgroup on CPU i.
7834 * current CPU.
7877 /* The set of CPUs under consideration for load-balancing */
7892 * Is this task likely cache-hot:
7898 lockdep_assert_held(&env->src_rq->lock); in task_hot()
7900 if (p->sched_class != &fair_sched_class) in task_hot()
7907 if (env->sd->flags & SD_SHARE_CPUCAPACITY) in task_hot()
7913 if (sched_feat(CACHE_HOT_BUDDY) && env->dst_rq->nr_running && in task_hot()
7914 (&p->se == cfs_rq_of(&p->se)->next || in task_hot()
7915 &p->se == cfs_rq_of(&p->se)->last)) in task_hot()
7918 if (sysctl_sched_migration_cost == -1) in task_hot()
7923 delta = rq_clock_task(env->src_rq) - p->se.exec_start; in task_hot()
7932 * Returns -1, if task migration is not affected by locality.
7936 struct numa_group *numa_group = rcu_dereference(p->numa_group); in migrate_degrades_locality()
7941 return -1; in migrate_degrades_locality()
7943 if (!p->numa_faults || !(env->sd->flags & SD_NUMA)) in migrate_degrades_locality()
7944 return -1; in migrate_degrades_locality()
7946 src_nid = cpu_to_node(env->src_cpu); in migrate_degrades_locality()
7947 dst_nid = cpu_to_node(env->dst_cpu); in migrate_degrades_locality()
7950 return -1; in migrate_degrades_locality()
7953 if (src_nid == p->numa_preferred_nid) { in migrate_degrades_locality()
7954 if (env->src_rq->nr_running > env->src_rq->nr_preferred_running) in migrate_degrades_locality()
7957 return -1; in migrate_degrades_locality()
7961 if (dst_nid == p->numa_preferred_nid) in migrate_degrades_locality()
7965 if (env->idle == CPU_IDLE) in migrate_degrades_locality()
7966 return -1; in migrate_degrades_locality()
7984 return -1; in migrate_degrades_locality()
7989 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
7996 lockdep_assert_held(&env->src_rq->lock); in can_migrate_task()
8003 * 4) are cache-hot on their current CPU. in can_migrate_task()
8005 if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu)) in can_migrate_task()
8012 if (!cpumask_test_cpu(env->dst_cpu, p->cpus_ptr)) { in can_migrate_task()
8015 schedstat_inc(p->se.statistics.nr_failed_migrations_affine); in can_migrate_task()
8017 env->flags |= LBF_SOME_PINNED; in can_migrate_task()
8025 * already computed one in current iteration. in can_migrate_task()
8027 if (env->idle == CPU_NEWLY_IDLE || (env->flags & LBF_DST_PINNED)) in can_migrate_task()
8030 /* Prevent to re-select dst_cpu via env's CPUs: */ in can_migrate_task()
8031 for_each_cpu_and(cpu, env->dst_grpmask, env->cpus) { in can_migrate_task()
8032 if (cpumask_test_cpu(cpu, p->cpus_ptr)) { in can_migrate_task()
8033 env->flags |= LBF_DST_PINNED; in can_migrate_task()
8034 env->new_dst_cpu = cpu; in can_migrate_task()
8043 env->flags &= ~LBF_ALL_PINNED; in can_migrate_task()
8047 if (env->flags & LBF_IGNORE_PREFERRED_CLUSTER_TASKS && in can_migrate_task()
8048 !preferred_cluster(cpu_rq(env->dst_cpu)->cluster, p)) in can_migrate_task()
8052 if (task_running(env->src_rq, p)) { in can_migrate_task()
8053 schedstat_inc(p->se.statistics.nr_failed_migrations_running); in can_migrate_task()
8064 if (tsk_cache_hot == -1) in can_migrate_task()
8068 env->sd->nr_balance_failed > env->sd->cache_nice_tries) { in can_migrate_task()
8070 schedstat_inc(env->sd->lb_hot_gained[env->idle]); in can_migrate_task()
8071 schedstat_inc(p->se.statistics.nr_forced_migrations); in can_migrate_task()
8076 schedstat_inc(p->se.statistics.nr_failed_migrations_hot); in can_migrate_task()
8081 * detach_task() -- detach the task for the migration specified in env
8085 lockdep_assert_held(&env->src_rq->lock); in detach_task()
8087 deactivate_task(env->src_rq, p, DEQUEUE_NOCLOCK); in detach_task()
8089 double_lock_balance(env->src_rq, env->dst_rq); in detach_task()
8090 if (!(env->src_rq->clock_update_flags & RQCF_UPDATED)) in detach_task()
8091 update_rq_clock(env->src_rq); in detach_task()
8093 set_task_cpu(p, env->dst_cpu); in detach_task()
8095 double_unlock_balance(env->src_rq, env->dst_rq); in detach_task()
8100 * detach_one_task() -- tries to dequeue exactly one task from env->src_rq, as
8109 lockdep_assert_held(&env->src_rq->lock); in detach_one_task()
8112 &env->src_rq->cfs_tasks, se.group_node) { in detach_one_task()
8120 * lb_gained[env->idle] is updated (other is detach_tasks) in detach_one_task()
8124 schedstat_inc(env->sd->lb_gained[env->idle]); in detach_one_task()
8133 * detach_tasks() -- tries to detach up to imbalance load/util/tasks from
8140 struct list_head *tasks = &env->src_rq->cfs_tasks; in detach_tasks()
8145 int orig_loop = env->loop; in detach_tasks()
8148 lockdep_assert_held(&env->src_rq->lock); in detach_tasks()
8150 if (env->imbalance <= 0) in detach_tasks()
8154 if (!same_cluster(env->dst_cpu, env->src_cpu)) in detach_tasks()
8155 env->flags |= LBF_IGNORE_PREFERRED_CLUSTER_TASKS; in detach_tasks()
8164 if (env->idle != CPU_NOT_IDLE && env->src_rq->nr_running <= 1) in detach_tasks()
8169 env->loop++; in detach_tasks()
8171 if (env->loop > env->loop_max) in detach_tasks()
8175 if (env->loop > env->loop_break) { in detach_tasks()
8176 env->loop_break += sched_nr_migrate_break; in detach_tasks()
8177 env->flags |= LBF_NEED_BREAK; in detach_tasks()
8184 switch (env->migration_type) { in detach_tasks()
8189 * value. Make sure that env->imbalance decreases in detach_tasks()
8196 load < 16 && !env->sd->nr_balance_failed) in detach_tasks()
8205 if (shr_bound(load, env->sd->nr_balance_failed) > env->imbalance) in detach_tasks()
8208 env->imbalance -= load; in detach_tasks()
8214 if (util > env->imbalance) in detach_tasks()
8217 env->imbalance -= util; in detach_tasks()
8221 env->imbalance--; in detach_tasks()
8226 if (task_fits_cpu(p, env->src_cpu)) in detach_tasks()
8229 env->imbalance = 0; in detach_tasks()
8234 list_add(&p->se.group_node, &env->tasks); in detach_tasks()
8244 if (env->idle == CPU_NEWLY_IDLE) in detach_tasks()
8252 if (env->imbalance <= 0) in detach_tasks()
8257 list_move(&p->se.group_node, tasks); in detach_tasks()
8261 if (env->flags & LBF_IGNORE_PREFERRED_CLUSTER_TASKS && !detached) { in detach_tasks()
8262 tasks = &env->src_rq->cfs_tasks; in detach_tasks()
8263 env->flags &= ~LBF_IGNORE_PREFERRED_CLUSTER_TASKS; in detach_tasks()
8264 env->loop = orig_loop; in detach_tasks()
8274 schedstat_add(env->sd->lb_gained[env->idle], detached); in detach_tasks()
8280 * attach_task() -- attach the task detached by detach_task() to its new rq.
8284 lockdep_assert_held(&rq->lock); in attach_task()
8292 * attach_one_task() -- attaches the task returned from detach_one_task() to
8306 * attach_tasks() -- attaches all tasks detached by detach_tasks() to their
8311 struct list_head *tasks = &env->tasks; in attach_tasks()
8315 rq_lock(env->dst_rq, &rf); in attach_tasks()
8316 update_rq_clock(env->dst_rq); in attach_tasks()
8320 list_del_init(&p->se.group_node); in attach_tasks()
8322 attach_task(env->dst_rq, p); in attach_tasks()
8325 rq_unlock(env->dst_rq, &rf); in attach_tasks()
8331 if (cfs_rq->avg.load_avg) in cfs_rq_has_blocked()
8334 if (cfs_rq->avg.util_avg) in cfs_rq_has_blocked()
8342 if (READ_ONCE(rq->avg_rt.util_avg)) in others_have_blocked()
8345 if (READ_ONCE(rq->avg_dl.util_avg)) in others_have_blocked()
8352 if (READ_ONCE(rq->avg_irq.util_avg)) in others_have_blocked()
8361 rq->last_blocked_load_update_tick = jiffies; in update_blocked_load_status()
8364 rq->has_blocked_load = 0; in update_blocked_load_status()
8383 curr_class = rq->curr->sched_class; in __update_blocked_others()
8402 if (cfs_rq->load.weight) in cfs_rq_is_decayed()
8405 if (cfs_rq->avg.load_sum) in cfs_rq_is_decayed()
8408 if (cfs_rq->avg.util_sum) in cfs_rq_is_decayed()
8411 if (cfs_rq->avg.runnable_sum) in cfs_rq_is_decayed()
8433 if (cfs_rq == &rq->cfs) in __update_blocked_fair()
8438 se = cfs_rq->tg->se[cpu]; in __update_blocked_fair()
8459 * This needs to be done in a top-down fashion because the load of a child
8465 struct sched_entity *se = cfs_rq->tg->se[cpu_of(rq)]; in update_cfs_rq_h_load()
8469 if (cfs_rq->last_h_load_update == now) in update_cfs_rq_h_load()
8472 WRITE_ONCE(cfs_rq->h_load_next, NULL); in update_cfs_rq_h_load()
8475 WRITE_ONCE(cfs_rq->h_load_next, se); in update_cfs_rq_h_load()
8476 if (cfs_rq->last_h_load_update == now) in update_cfs_rq_h_load()
8481 cfs_rq->h_load = cfs_rq_load_avg(cfs_rq); in update_cfs_rq_h_load()
8482 cfs_rq->last_h_load_update = now; in update_cfs_rq_h_load()
8485 while ((se = READ_ONCE(cfs_rq->h_load_next)) != NULL) { in update_cfs_rq_h_load()
8486 load = cfs_rq->h_load; in update_cfs_rq_h_load()
8487 load = div64_ul(load * se->avg.load_avg, in update_cfs_rq_h_load()
8490 cfs_rq->h_load = load; in update_cfs_rq_h_load()
8491 cfs_rq->last_h_load_update = now; in update_cfs_rq_h_load()
8500 return div64_ul(p->se.avg.load_avg * cfs_rq->h_load, in task_h_load()
8506 struct cfs_rq *cfs_rq = &rq->cfs; in __update_blocked_fair()
8518 return p->se.avg.load_avg; in task_h_load()
8543 * sg_lb_stats - stats of a sched_group required for load_balancing
8565 * sd_lb_stats - Structure to store the statistics of a sched_domain
8619 used = READ_ONCE(rq->avg_rt.util_avg); in scale_rt_capacity()
8620 used += READ_ONCE(rq->avg_dl.util_avg); in scale_rt_capacity()
8626 free = max - used; in scale_rt_capacity()
8635 struct sched_group *sdg = sd->groups; in update_cpu_capacity()
8638 rq->cpu_capacity_orig = capacity_orig; in update_cpu_capacity()
8643 rq->cpu_capacity = capacity; in update_cpu_capacity()
8664 unsigned long inv_cap = capacity_orig - thermal_load_avg(rq); in update_cpu_capacity()
8669 pd = rcu_dereference(rq->rd->pd); in update_cpu_capacity()
8670 rq->cpu_capacity_inverted = 0; in update_cpu_capacity()
8672 for (; pd; pd = pd->next) { in update_cpu_capacity()
8693 pd_cap = pd_cap_orig - thermal_load_avg(cpu_rq(cpu)); in update_cpu_capacity()
8696 rq->cpu_capacity_inverted = inv_cap; in update_cpu_capacity()
8700 rq->cpu_capacity_inverted = inv_cap; in update_cpu_capacity()
8710 sdg->sgc->capacity = capacity; in update_cpu_capacity()
8711 sdg->sgc->min_capacity = capacity; in update_cpu_capacity()
8712 sdg->sgc->max_capacity = capacity; in update_cpu_capacity()
8717 struct sched_domain *child = sd->child; in update_group_capacity()
8718 struct sched_group *group, *sdg = sd->groups; in update_group_capacity()
8722 interval = msecs_to_jiffies(sd->balance_interval); in update_group_capacity()
8724 sdg->sgc->next_update = jiffies + interval; in update_group_capacity()
8735 if (child->flags & SD_OVERLAP) { in update_group_capacity()
8738 * span the current group. in update_group_capacity()
8754 * span the current group. in update_group_capacity()
8757 group = child->groups; in update_group_capacity()
8759 struct sched_group_capacity *sgc = group->sgc; in update_group_capacity()
8764 capacity += sgc->capacity; in update_group_capacity()
8765 min_capacity = min(sgc->min_capacity, in update_group_capacity()
8767 max_capacity = max(sgc->max_capacity, in update_group_capacity()
8770 group = group->next; in update_group_capacity()
8771 } while (group != child->groups); in update_group_capacity()
8774 sdg->sgc->capacity = capacity; in update_group_capacity()
8775 sdg->sgc->min_capacity = min_capacity; in update_group_capacity()
8776 sdg->sgc->max_capacity = max_capacity; in update_group_capacity()
8787 return ((rq->cpu_capacity * sd->imbalance_pct) < in check_cpu_capacity()
8788 (rq->cpu_capacity_orig * 100)); in check_cpu_capacity()
8794 * the task's current CPU is heavily pressured.
8798 return rq->misfit_task_load && in check_misfit_status()
8799 (rq->cpu_capacity_orig < rq->rd->max_cpu_capacity || in check_misfit_status()
8805 * groups is inadequate due to ->cpus_ptr constraints.
8814 * If we were to balance group-wise we'd place two tasks in the first group and
8818 * The current solution to this issue is detecting the skew in the first group
8834 return group->sgc->imbalance; in sg_imbalanced()
8852 if (sgs->sum_nr_running < sgs->group_weight) in group_has_capacity()
8855 if ((sgs->group_capacity * imbalance_pct) < in group_has_capacity()
8856 (sgs->group_runnable * 100)) in group_has_capacity()
8859 if ((sgs->group_capacity * 100) > in group_has_capacity()
8860 (sgs->group_util * imbalance_pct)) in group_has_capacity()
8877 if (sgs->sum_nr_running <= sgs->group_weight) in group_is_overloaded()
8880 if ((sgs->group_capacity * 100) < in group_is_overloaded()
8881 (sgs->group_util * imbalance_pct)) in group_is_overloaded()
8884 if ((sgs->group_capacity * imbalance_pct) < in group_is_overloaded()
8885 (sgs->group_runnable * 100)) in group_is_overloaded()
8893 * per-CPU capacity than sched_group ref.
8898 return fits_capacity(sg->sgc->min_capacity, ref->sgc->min_capacity); in group_smaller_min_cpu_capacity()
8903 * per-CPU capacity_orig than sched_group ref.
8908 return fits_capacity(sg->sgc->max_capacity, ref->sgc->max_capacity); in group_smaller_max_cpu_capacity()
8922 if (sgs->group_asym_packing) in group_classify()
8925 if (sgs->group_misfit_task_load) in group_classify()
8937 unsigned int cpu = rq->cpu; in update_nohz_stats()
8939 if (!rq->has_blocked_load) in update_nohz_stats()
8945 if (!force && !time_after(jiffies, rq->last_blocked_load_update_tick)) in update_nohz_stats()
8950 return rq->has_blocked_load; in update_nohz_stats()
8957 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
8972 local_group = cpumask_test_cpu(env->dst_cpu, sched_group_span(group)); in update_sg_lb_stats()
8974 for_each_cpu_and(i, sched_group_span(group), env->cpus) { in update_sg_lb_stats()
8980 if ((env->flags & LBF_NOHZ_STATS) && update_nohz_stats(rq, false)) in update_sg_lb_stats()
8981 env->flags |= LBF_NOHZ_AGAIN; in update_sg_lb_stats()
8983 sgs->group_load += cpu_load(rq); in update_sg_lb_stats()
8984 sgs->group_util += cpu_util(i); in update_sg_lb_stats()
8985 sgs->group_runnable += cpu_runnable(rq); in update_sg_lb_stats()
8986 sgs->sum_h_nr_running += rq->cfs.h_nr_running; in update_sg_lb_stats()
8988 nr_running = rq->nr_running; in update_sg_lb_stats()
8989 sgs->sum_nr_running += nr_running; in update_sg_lb_stats()
8998 sgs->nr_numa_running += rq->nr_numa_running; in update_sg_lb_stats()
8999 sgs->nr_preferred_running += rq->nr_preferred_running; in update_sg_lb_stats()
9005 sgs->idle_cpus++; in update_sg_lb_stats()
9014 if (env->sd->flags & SD_ASYM_CPUCAPACITY && in update_sg_lb_stats()
9015 sgs->group_misfit_task_load < rq->misfit_task_load) { in update_sg_lb_stats()
9016 sgs->group_misfit_task_load = rq->misfit_task_load; in update_sg_lb_stats()
9022 if (!group->group_weight) { in update_sg_lb_stats()
9023 sgs->group_capacity = 0; in update_sg_lb_stats()
9024 sgs->avg_load = 0; in update_sg_lb_stats()
9025 sgs->group_type = group_has_spare; in update_sg_lb_stats()
9026 sgs->group_weight = group->group_weight; in update_sg_lb_stats()
9031 if (env->sd->flags & SD_ASYM_PACKING && in update_sg_lb_stats()
9032 env->idle != CPU_NOT_IDLE && in update_sg_lb_stats()
9033 sgs->sum_h_nr_running && in update_sg_lb_stats()
9034 sched_asym_prefer(env->dst_cpu, group->asym_prefer_cpu)) { in update_sg_lb_stats()
9035 sgs->group_asym_packing = 1; in update_sg_lb_stats()
9038 sgs->group_capacity = group->sgc->capacity; in update_sg_lb_stats()
9040 sgs->group_weight = group->group_weight; in update_sg_lb_stats()
9042 sgs->group_type = group_classify(env->sd->imbalance_pct, group, sgs); in update_sg_lb_stats()
9045 if (sgs->group_type == group_overloaded) in update_sg_lb_stats()
9046 sgs->avg_load = (sgs->group_load * SCHED_CAPACITY_SCALE) / in update_sg_lb_stats()
9047 sgs->group_capacity; in update_sg_lb_stats()
9051 * update_sd_pick_busiest - return 1 on busiest group
9068 struct sg_lb_stats *busiest = &sds->busiest_stat; in update_sd_pick_busiest()
9071 if (!sgs->sum_h_nr_running) in update_sd_pick_busiest()
9080 if (sgs->group_type == group_misfit_task && in update_sd_pick_busiest()
9081 (!group_smaller_max_cpu_capacity(sg, sds->local) || in update_sd_pick_busiest()
9082 sds->local_stat.group_type != group_has_spare)) in update_sd_pick_busiest()
9085 if (sgs->group_type > busiest->group_type) in update_sd_pick_busiest()
9088 if (sgs->group_type < busiest->group_type) in update_sd_pick_busiest()
9092 * The candidate and the current busiest group are the same type of in update_sd_pick_busiest()
9096 switch (sgs->group_type) { in update_sd_pick_busiest()
9099 if (sgs->avg_load <= busiest->avg_load) in update_sd_pick_busiest()
9112 if (sched_asym_prefer(sg->asym_prefer_cpu, sds->busiest->asym_prefer_cpu)) in update_sd_pick_busiest()
9121 if (sgs->group_misfit_task_load < busiest->group_misfit_task_load) in update_sd_pick_busiest()
9136 if (sgs->avg_load <= busiest->avg_load) in update_sd_pick_busiest()
9148 if (sgs->idle_cpus > busiest->idle_cpus) in update_sd_pick_busiest()
9150 else if ((sgs->idle_cpus == busiest->idle_cpus) && in update_sd_pick_busiest()
9151 (sgs->sum_nr_running <= busiest->sum_nr_running)) in update_sd_pick_busiest()
9159 * per-CPU capacity. Migrating tasks to less capable CPUs may harm in update_sd_pick_busiest()
9163 if ((env->sd->flags & SD_ASYM_CPUCAPACITY) && in update_sd_pick_busiest()
9164 (sgs->group_type <= group_fully_busy) && in update_sd_pick_busiest()
9165 (group_smaller_min_cpu_capacity(sds->local, sg))) in update_sd_pick_busiest()
9174 if (sgs->sum_h_nr_running > sgs->nr_numa_running) in fbq_classify_group()
9176 if (sgs->sum_h_nr_running > sgs->nr_preferred_running) in fbq_classify_group()
9183 if (rq->nr_running > rq->nr_numa_running) in fbq_classify_rq()
9185 if (rq->nr_running > rq->nr_preferred_running) in fbq_classify_rq()
9205 * task_running_on_cpu - return 1 if @p is running on @cpu.
9211 if (cpu != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in task_running_on_cpu()
9221 * idle_cpu_without - would a given CPU be idle without p ?
9231 if (rq->curr != rq->idle && rq->curr != p) in idle_cpu_without()
9235 * rq->nr_running can't be used but an updated version without the in idle_cpu_without()
9241 if (rq->ttwu_pending) in idle_cpu_without()
9249 * update_sg_wakeup_stats - Update sched_group's statistics for wakeup.
9265 if (sd->flags & SD_ASYM_CPUCAPACITY) in update_sg_wakeup_stats()
9266 sgs->group_misfit_task_load = 1; in update_sg_wakeup_stats()
9272 sgs->group_load += cpu_load_without(rq, p); in update_sg_wakeup_stats()
9273 sgs->group_util += cpu_util_without(i, p); in update_sg_wakeup_stats()
9274 sgs->group_runnable += cpu_runnable_without(rq, p); in update_sg_wakeup_stats()
9276 sgs->sum_h_nr_running += rq->cfs.h_nr_running - local; in update_sg_wakeup_stats()
9278 nr_running = rq->nr_running - local; in update_sg_wakeup_stats()
9279 sgs->sum_nr_running += nr_running; in update_sg_wakeup_stats()
9285 sgs->idle_cpus++; in update_sg_wakeup_stats()
9288 if (sd->flags & SD_ASYM_CPUCAPACITY && in update_sg_wakeup_stats()
9289 sgs->group_misfit_task_load && in update_sg_wakeup_stats()
9291 sgs->group_misfit_task_load = 0; in update_sg_wakeup_stats()
9295 sgs->group_capacity = group->sgc->capacity; in update_sg_wakeup_stats()
9297 sgs->group_weight = group->group_weight; in update_sg_wakeup_stats()
9299 sgs->group_type = group_classify(sd->imbalance_pct, group, sgs); in update_sg_wakeup_stats()
9305 if (sgs->group_type == group_fully_busy || in update_sg_wakeup_stats()
9306 sgs->group_type == group_overloaded) in update_sg_wakeup_stats()
9307 sgs->avg_load = (sgs->group_load * SCHED_CAPACITY_SCALE) / in update_sg_wakeup_stats()
9308 sgs->group_capacity; in update_sg_wakeup_stats()
9316 if (sgs->group_type < idlest_sgs->group_type) in update_pick_idlest()
9319 if (sgs->group_type > idlest_sgs->group_type) in update_pick_idlest()
9323 * The candidate and the current idlest group are the same type of in update_pick_idlest()
9327 switch (sgs->group_type) { in update_pick_idlest()
9331 if (idlest_sgs->avg_load <= sgs->avg_load) in update_pick_idlest()
9342 if (idlest->sgc->max_capacity >= group->sgc->max_capacity) in update_pick_idlest()
9348 if (idlest_sgs->idle_cpus > sgs->idle_cpus) in update_pick_idlest()
9352 if (idlest_sgs->idle_cpus == sgs->idle_cpus && in update_pick_idlest()
9353 idlest_sgs->group_util <= sgs->group_util) in update_pick_idlest()
9371 struct sched_group *idlest = NULL, *local = NULL, *group = sd->groups; in find_idlest_group()
9382 cpumask_andnot(&allowed_cpus, p->cpus_ptr, cpu_isolated_mask); in find_idlest_group()
9386 (sd->imbalance_pct-100) / 100; in find_idlest_group()
9397 p->cpus_ptr)) in find_idlest_group()
9418 } while (group = group->next, group != sd->groups); in find_idlest_group()
9451 * cross-domain, add imbalance to the load on the remote node in find_idlest_group()
9455 if ((sd->flags & SD_NUMA) && in find_idlest_group()
9466 if (100 * local_sgs.avg_load <= sd->imbalance_pct * idlest_sgs.avg_load) in find_idlest_group()
9477 if (local->sgc->max_capacity >= idlest->sgc->max_capacity) in find_idlest_group()
9482 if (sd->flags & SD_NUMA) { in find_idlest_group()
9489 if (cpu_to_node(this_cpu) == p->numa_preferred_nid) in find_idlest_group()
9493 if (cpu_to_node(idlest_cpu) == p->numa_preferred_nid) in find_idlest_group()
9521 * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
9528 struct sched_domain *child = env->sd->child; in update_sd_lb_stats()
9529 struct sched_group *sg = env->sd->groups; in update_sd_lb_stats()
9530 struct sg_lb_stats *local = &sds->local_stat; in update_sd_lb_stats()
9535 if (env->idle == CPU_NEWLY_IDLE && READ_ONCE(nohz.has_blocked)) in update_sd_lb_stats()
9536 env->flags |= LBF_NOHZ_STATS; in update_sd_lb_stats()
9543 local_group = cpumask_test_cpu(env->dst_cpu, sched_group_span(sg)); in update_sd_lb_stats()
9545 sds->local = sg; in update_sd_lb_stats()
9548 if (env->idle != CPU_NEWLY_IDLE || in update_sd_lb_stats()
9549 time_after_eq(jiffies, sg->sgc->next_update)) in update_sd_lb_stats()
9550 update_group_capacity(env->sd, env->dst_cpu); in update_sd_lb_stats()
9560 sds->busiest = sg; in update_sd_lb_stats()
9561 sds->busiest_stat = *sgs; in update_sd_lb_stats()
9566 sds->total_load += sgs->group_load; in update_sd_lb_stats()
9567 sds->total_capacity += sgs->group_capacity; in update_sd_lb_stats()
9569 sg = sg->next; in update_sd_lb_stats()
9570 } while (sg != env->sd->groups); in update_sd_lb_stats()
9573 sds->prefer_sibling = child && child->flags & SD_PREFER_SIBLING; in update_sd_lb_stats()
9576 if ((env->flags & LBF_NOHZ_AGAIN) && in update_sd_lb_stats()
9577 cpumask_subset(nohz.idle_cpus_mask, sched_domain_span(env->sd))) { in update_sd_lb_stats()
9584 if (env->sd->flags & SD_NUMA) in update_sd_lb_stats()
9585 env->fbq_type = fbq_classify_group(&sds->busiest_stat); in update_sd_lb_stats()
9587 if (!env->sd->parent) { in update_sd_lb_stats()
9588 struct root_domain *rd = env->dst_rq->rd; in update_sd_lb_stats()
9591 WRITE_ONCE(rd->overload, sg_status & SG_OVERLOAD); in update_sd_lb_stats()
9593 /* Update over-utilization (tipping point, U >= 0) indicator */ in update_sd_lb_stats()
9594 WRITE_ONCE(rd->overutilized, sg_status & SG_OVERUTILIZED); in update_sd_lb_stats()
9597 struct root_domain *rd = env->dst_rq->rd; in update_sd_lb_stats()
9599 WRITE_ONCE(rd->overutilized, SG_OVERUTILIZED); in update_sd_lb_stats()
9620 * calculate_imbalance - Calculate the amount of imbalance present within the
9629 local = &sds->local_stat; in calculate_imbalance()
9630 busiest = &sds->busiest_stat; in calculate_imbalance()
9632 if (busiest->group_type == group_misfit_task) { in calculate_imbalance()
9634 env->migration_type = migrate_misfit; in calculate_imbalance()
9635 env->imbalance = 1; in calculate_imbalance()
9639 if (busiest->group_type == group_asym_packing) { in calculate_imbalance()
9644 env->migration_type = migrate_task; in calculate_imbalance()
9645 env->imbalance = busiest->sum_h_nr_running; in calculate_imbalance()
9649 if (busiest->group_type == group_imbalanced) { in calculate_imbalance()
9651 * In the group_imb case we cannot rely on group-wide averages in calculate_imbalance()
9652 * to ensure CPU-load equilibrium, try to move any task to fix in calculate_imbalance()
9656 env->migration_type = migrate_task; in calculate_imbalance()
9657 env->imbalance = 1; in calculate_imbalance()
9665 if (local->group_type == group_has_spare) { in calculate_imbalance()
9666 if ((busiest->group_type > group_fully_busy) && in calculate_imbalance()
9667 !(env->sd->flags & SD_SHARE_PKG_RESOURCES)) { in calculate_imbalance()
9676 env->migration_type = migrate_util; in calculate_imbalance()
9677 env->imbalance = max(local->group_capacity, local->group_util) - in calculate_imbalance()
9678 local->group_util; in calculate_imbalance()
9687 if (env->idle != CPU_NOT_IDLE && env->imbalance == 0) { in calculate_imbalance()
9688 env->migration_type = migrate_task; in calculate_imbalance()
9689 env->imbalance = 1; in calculate_imbalance()
9695 if (busiest->group_weight == 1 || sds->prefer_sibling) { in calculate_imbalance()
9696 unsigned int nr_diff = busiest->sum_nr_running; in calculate_imbalance()
9701 env->migration_type = migrate_task; in calculate_imbalance()
9702 lsub_positive(&nr_diff, local->sum_nr_running); in calculate_imbalance()
9703 env->imbalance = nr_diff >> 1; in calculate_imbalance()
9710 env->migration_type = migrate_task; in calculate_imbalance()
9711 env->imbalance = max_t(long, 0, (local->idle_cpus - in calculate_imbalance()
9712 busiest->idle_cpus) >> 1); in calculate_imbalance()
9716 if (env->sd->flags & SD_NUMA) in calculate_imbalance()
9717 env->imbalance = adjust_numa_imbalance(env->imbalance, in calculate_imbalance()
9718 busiest->sum_nr_running); in calculate_imbalance()
9727 if (local->group_type < group_overloaded) { in calculate_imbalance()
9733 local->avg_load = (local->group_load * SCHED_CAPACITY_SCALE) / in calculate_imbalance()
9734 local->group_capacity; in calculate_imbalance()
9740 if (local->avg_load >= busiest->avg_load) { in calculate_imbalance()
9741 env->imbalance = 0; in calculate_imbalance()
9745 sds->avg_load = (sds->total_load * SCHED_CAPACITY_SCALE) / in calculate_imbalance()
9746 sds->total_capacity; in calculate_imbalance()
9752 if (local->avg_load >= sds->avg_load) { in calculate_imbalance()
9753 env->imbalance = 0; in calculate_imbalance()
9767 env->migration_type = migrate_load; in calculate_imbalance()
9768 env->imbalance = min( in calculate_imbalance()
9769 (busiest->avg_load - sds->avg_load) * busiest->group_capacity, in calculate_imbalance()
9770 (sds->avg_load - local->avg_load) * local->group_capacity in calculate_imbalance()
9797 * find_busiest_group - Returns the busiest group within the sched_domain
9805 * Return: - The busiest group if imbalance exists.
9821 struct root_domain *rd = env->dst_rq->rd; in find_busiest_group()
9823 if (rcu_dereference(rd->pd) && !READ_ONCE(rd->overutilized)) in find_busiest_group()
9835 if (busiest->group_type == group_misfit_task) in find_busiest_group()
9839 if (busiest->group_type == group_asym_packing) in find_busiest_group()
9847 if (busiest->group_type == group_imbalanced) in find_busiest_group()
9854 if (local->group_type > busiest->group_type) in find_busiest_group()
9861 if (local->group_type == group_overloaded) { in find_busiest_group()
9866 if (local->avg_load >= busiest->avg_load) in find_busiest_group()
9877 if (local->avg_load >= sds.avg_load) in find_busiest_group()
9884 if (100 * busiest->avg_load <= in find_busiest_group()
9885 env->sd->imbalance_pct * local->avg_load) in find_busiest_group()
9890 if (sds.prefer_sibling && local->group_type == group_has_spare && in find_busiest_group()
9891 busiest->sum_nr_running > local->sum_nr_running + 1) in find_busiest_group()
9894 if (busiest->group_type != group_overloaded) { in find_busiest_group()
9895 if (env->idle == CPU_NOT_IDLE) in find_busiest_group()
9903 if (busiest->group_weight > 1 && in find_busiest_group()
9904 local->idle_cpus <= (busiest->idle_cpus + 1)) in find_busiest_group()
9916 if (busiest->sum_h_nr_running == 1) in find_busiest_group()
9926 return env->imbalance ? sds.busiest : NULL; in find_busiest_group()
9929 env->imbalance = 0; in find_busiest_group()
9934 * find_busiest_queue - find the busiest runqueue among the CPUs in the group.
9944 for_each_cpu_and(i, sched_group_span(group), env->cpus) { in find_busiest_queue()
9954 * - regular: there are !numa tasks in find_busiest_queue()
9955 * - remote: there are numa tasks that run on the 'wrong' node in find_busiest_queue()
9956 * - all: there is no distinction in find_busiest_queue()
9971 if (rt > env->fbq_type) in find_busiest_queue()
9978 nr_running = rq->cfs.h_nr_running; in find_busiest_queue()
9982 * eventually lead to active_balancing high->low capacity. in find_busiest_queue()
9983 * Higher per-CPU capacity is considered better than balancing in find_busiest_queue()
9986 if (env->sd->flags & SD_ASYM_CPUCAPACITY && in find_busiest_queue()
9987 capacity_of(env->dst_cpu) < capacity && in find_busiest_queue()
9991 switch (env->migration_type) { in find_busiest_queue()
9999 if (nr_running == 1 && load > env->imbalance && in find_busiest_queue()
10000 !check_cpu_capacity(rq, env->sd)) in find_busiest_queue()
10052 if (rq->misfit_task_load > busiest_load) { in find_busiest_queue()
10053 busiest_load = rq->misfit_task_load; in find_busiest_queue()
10079 return env->idle != CPU_NOT_IDLE && (env->sd->flags & SD_ASYM_PACKING) && in asym_active_balance()
10080 sched_asym_prefer(env->dst_cpu, env->src_cpu); in asym_active_balance()
10086 struct sched_domain *sd = env->sd; in voluntary_active_balance()
10097 if ((env->idle != CPU_NOT_IDLE) && in voluntary_active_balance()
10098 (env->src_rq->cfs.h_nr_running == 1)) { in voluntary_active_balance()
10099 if ((check_cpu_capacity(env->src_rq, sd)) && in voluntary_active_balance()
10100 (capacity_of(env->src_cpu)*sd->imbalance_pct < capacity_of(env->dst_cpu)*100)) in voluntary_active_balance()
10104 if (env->migration_type == migrate_misfit) in voluntary_active_balance()
10112 struct sched_domain *sd = env->sd; in need_active_balance()
10117 return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2); in need_active_balance()
10135 struct sched_group *sg = env->sd->groups; in should_we_balance()
10142 if (!cpumask_test_cpu(env->dst_cpu, env->cpus)) in should_we_balance()
10149 if (env->idle == CPU_NEWLY_IDLE) in should_we_balance()
10153 for_each_cpu_and(cpu, group_balance_mask(sg), env->cpus) { in should_we_balance()
10158 return cpu == env->dst_cpu; in should_we_balance()
10162 return group_balance_cpu_not_isolated(sg) == env->dst_cpu; in should_we_balance()
10174 struct sched_domain *sd_parent = sd->parent; in load_balance()
10184 .dst_grpmask = group_balance_mask(sd->groups), in load_balance()
10194 schedstat_inc(sd->lb_count[idle]); in load_balance()
10204 schedstat_inc(sd->lb_nobusyg[idle]); in load_balance()
10210 schedstat_inc(sd->lb_nobusyq[idle]); in load_balance()
10216 schedstat_add(sd->lb_imbalance[idle], env.imbalance); in load_balance()
10218 env.src_cpu = busiest->cpu; in load_balance()
10222 if (busiest->nr_running > 1) { in load_balance()
10225 * an imbalance but busiest->nr_running <= 1, the group is in load_balance()
10230 env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running); in load_balance()
10237 * cur_ld_moved - load moved in current iteration in load_balance()
10238 * ld_moved - cumulative load moved across iterations in load_balance()
10245 * unlock busiest->lock, and we are able to be sure in load_balance()
10274 * nohz-idle), we now have balance_cpu in a position to move in load_balance()
10285 /* Prevent to re-select dst_cpu via env's CPUs */ in load_balance()
10305 int *group_imbalance = &sd_parent->groups->sgc->imbalance; in load_balance()
10315 * Attempting to continue load balancing at the current in load_balance()
10332 schedstat_inc(sd->lb_failed[idle]); in load_balance()
10340 sd->nr_balance_failed++; in load_balance()
10345 raw_spin_lock_irqsave(&busiest->lock, flags); in load_balance()
10352 if (!cpumask_test_cpu(this_cpu, busiest->curr->cpus_ptr)) { in load_balance()
10353 raw_spin_unlock_irqrestore(&busiest->lock, in load_balance()
10360 * ->active_balance synchronizes accesses to in load_balance()
10361 * ->active_balance_work. Once set, it's cleared in load_balance()
10364 if (!busiest->active_balance && in load_balance()
10366 busiest->active_balance = 1; in load_balance()
10367 busiest->push_cpu = this_cpu; in load_balance()
10370 raw_spin_unlock_irqrestore(&busiest->lock, flags); in load_balance()
10375 &busiest->active_balance_work); in load_balance()
10379 sd->nr_balance_failed = sd->cache_nice_tries+1; in load_balance()
10382 sd->nr_balance_failed = 0; in load_balance()
10386 sd->balance_interval = sd->min_interval; in load_balance()
10394 if (sd->balance_interval < sd->max_interval) in load_balance()
10395 sd->balance_interval *= 2; in load_balance()
10407 int *group_imbalance = &sd_parent->groups->sgc->imbalance; in load_balance()
10419 schedstat_inc(sd->lb_balanced[idle]); in load_balance()
10421 sd->nr_balance_failed = 0; in load_balance()
10437 sd->balance_interval < MAX_PINNED_INTERVAL) || in load_balance()
10438 sd->balance_interval < sd->max_interval) in load_balance()
10439 sd->balance_interval *= 2; in load_balance()
10447 unsigned long interval = sd->balance_interval; in get_sd_balance_interval()
10450 interval *= sd->busy_factor; in get_sd_balance_interval()
10461 interval -= 1; in get_sd_balance_interval()
10475 next = sd->last_balance + interval; in update_next_balance()
10491 int target_cpu = busiest_rq->push_cpu; in active_load_balance_cpu_stop()
10503 * Between queueing the stop-work and running it is a hole in which in active_load_balance_cpu_stop()
10512 !busiest_rq->active_balance)) in active_load_balance_cpu_stop()
10516 if (busiest_rq->nr_running <= 1) in active_load_balance_cpu_stop()
10522 * Bjorn Helgaas on a 128-CPU setup. in active_load_balance_cpu_stop()
10527 push_task = busiest_rq->push_task; in active_load_balance_cpu_stop()
10528 target_cpu = busiest_rq->push_cpu; in active_load_balance_cpu_stop()
10534 .src_cpu = busiest_rq->cpu, in active_load_balance_cpu_stop()
10541 push_task->state == TASK_RUNNING && in active_load_balance_cpu_stop()
10564 .src_cpu = busiest_rq->cpu, in active_load_balance_cpu_stop()
10576 schedstat_inc(sd->alb_count); in active_load_balance_cpu_stop()
10581 schedstat_inc(sd->alb_pushed); in active_load_balance_cpu_stop()
10583 sd->nr_balance_failed = 0; in active_load_balance_cpu_stop()
10585 schedstat_inc(sd->alb_failed); in active_load_balance_cpu_stop()
10590 busiest_rq->active_balance = 0; in active_load_balance_cpu_stop()
10593 push_task = busiest_rq->push_task; in active_load_balance_cpu_stop()
10595 busiest_rq->push_task = NULL; in active_load_balance_cpu_stop()
10620 * This trades load-balance latency on larger machines for less cross talk.
10646 int cpu = rq->cpu; in rebalance_domains()
10662 if (time_after(jiffies, sd->next_decay_max_lb_cost)) { in rebalance_domains()
10663 sd->max_newidle_lb_cost = in rebalance_domains()
10664 (sd->max_newidle_lb_cost * 253) / 256; in rebalance_domains()
10665 sd->next_decay_max_lb_cost = jiffies + HZ; in rebalance_domains()
10668 max_cost += sd->max_newidle_lb_cost; in rebalance_domains()
10683 need_serialize = sd->flags & SD_SERIALIZE; in rebalance_domains()
10689 if (time_after_eq(jiffies, sd->last_balance + interval)) { in rebalance_domains()
10693 * env->dst_cpu, so we can't know our idle in rebalance_domains()
10699 sd->last_balance = jiffies; in rebalance_domains()
10705 if (time_after(next_balance, sd->last_balance + interval)) { in rebalance_domains()
10706 next_balance = sd->last_balance + interval; in rebalance_domains()
10712 * Ensure the rq-wide value also decays but keep it at a in rebalance_domains()
10713 * reasonable floor to avoid funnies with rq->avg_idle. in rebalance_domains()
10715 rq->max_idle_balance_cost = in rebalance_domains()
10726 rq->next_balance = next_balance; in rebalance_domains()
10737 if ((idle == CPU_IDLE) && time_after(nohz.next_balance, rq->next_balance)) in rebalance_domains()
10738 nohz.next_balance = rq->next_balance; in rebalance_domains()
10745 return unlikely(!rcu_dereference_sched(rq->sd)); in on_null_domain()
10751 * - When one of the busy CPUs notice that there may be an idle rebalancing
10754 * - HK_FLAG_MISC CPUs are used for this task, because HK_FLAG_SCHED not set
10807 smp_call_function_single_async(ilb_cpu, &cpu_rq(ilb_cpu)->nohz_csd); in kick_ilb()
10811 * Current decision point for kicking the idle load balancer in the presence
10819 int nr_busy, i, cpu = rq->cpu; in nohz_balancer_kick()
10823 if (unlikely(rq->idle_balance)) in nohz_balancer_kick()
10853 if (rq->nr_running >= 2) { in nohz_balancer_kick()
10860 sd = rcu_dereference(rq->sd); in nohz_balancer_kick()
10863 * If there's a CFS task and the current CPU has reduced in nohz_balancer_kick()
10867 if (rq->cfs.h_nr_running >= 1 && check_cpu_capacity(rq, sd)) { in nohz_balancer_kick()
10913 * increase the overall cache use), we need some less-loaded LLC in nohz_balancer_kick()
10915 * load within the current LLC domain (e.g. packed SMT cores but in nohz_balancer_kick()
10917 * the others are - so just get a nohz balance going if it looks in nohz_balancer_kick()
10920 nr_busy = atomic_read(&sds->nr_busy_cpus); in nohz_balancer_kick()
10940 if (!sd || !sd->nohz_idle) in set_cpu_sd_state_busy()
10942 sd->nohz_idle = 0; in set_cpu_sd_state_busy()
10944 atomic_inc(&sd->shared->nr_busy_cpus); in set_cpu_sd_state_busy()
10953 if (likely(!rq->nohz_tick_stopped)) in nohz_balance_exit_idle()
10956 rq->nohz_tick_stopped = 0; in nohz_balance_exit_idle()
10957 cpumask_clear_cpu(rq->cpu, nohz.idle_cpus_mask); in nohz_balance_exit_idle()
10960 set_cpu_sd_state_busy(rq->cpu); in nohz_balance_exit_idle()
10970 if (!sd || sd->nohz_idle) in set_cpu_sd_state_idle()
10972 sd->nohz_idle = 1; in set_cpu_sd_state_idle()
10974 atomic_dec(&sd->shared->nr_busy_cpus); in set_cpu_sd_state_idle()
10998 * Can be set safely without rq->lock held in nohz_balance_enter_idle()
11000 * rq->lock is held during the check and the clear in nohz_balance_enter_idle()
11002 rq->has_blocked_load = 1; in nohz_balance_enter_idle()
11010 if (rq->nohz_tick_stopped) in nohz_balance_enter_idle()
11017 rq->nohz_tick_stopped = 1; in nohz_balance_enter_idle()
11054 int this_cpu = this_rq->cpu; in _nohz_idle_balance()
11106 if (time_after_eq(jiffies, rq->next_balance)) { in _nohz_idle_balance()
11117 if (time_after(next_balance, rq->next_balance)) { in _nohz_idle_balance()
11118 next_balance = rq->next_balance; in _nohz_idle_balance()
11134 has_blocked_load |= this_rq->has_blocked_load; in _nohz_idle_balance()
11160 unsigned int flags = this_rq->nohz_idle_balance; in nohz_idle_balance()
11165 this_rq->nohz_idle_balance = 0; in nohz_idle_balance()
11177 int this_cpu = this_rq->cpu; in nohz_newidle_balance()
11187 if (this_rq->avg_idle < sysctl_sched_migration_cost) in nohz_newidle_balance()
11195 raw_spin_unlock(&this_rq->lock); in nohz_newidle_balance()
11204 raw_spin_lock(&this_rq->lock); in nohz_newidle_balance()
11223 * < 0 - we released the lock and there are !fair tasks present
11224 * 0 - failed, no new tasks
11225 * > 0 - success, new (fair) tasks present
11230 int this_cpu = this_rq->cpu; in newidle_balance()
11243 this_rq->idle_stamp = rq_clock(this_rq); in newidle_balance()
11252 * This is OK, because current is on_cpu, which avoids it being picked in newidle_balance()
11253 * for load-balance and preemption/IRQs are still disabled avoiding in newidle_balance()
11255 * re-start the picking loop. in newidle_balance()
11259 if (this_rq->avg_idle < sysctl_sched_migration_cost || in newidle_balance()
11260 !READ_ONCE(this_rq->rd->overload)) { in newidle_balance()
11263 sd = rcu_dereference_check_sched_domain(this_rq->sd); in newidle_balance()
11273 raw_spin_unlock(&this_rq->lock); in newidle_balance()
11281 if (this_rq->avg_idle < curr_cost + sd->max_newidle_lb_cost) { in newidle_balance()
11286 if (sd->flags & SD_BALANCE_NEWIDLE) { in newidle_balance()
11293 domain_cost = sched_clock_cpu(this_cpu) - t0; in newidle_balance()
11294 if (domain_cost > sd->max_newidle_lb_cost) in newidle_balance()
11295 sd->max_newidle_lb_cost = domain_cost; in newidle_balance()
11306 if (pulled_task || this_rq->nr_running > 0) in newidle_balance()
11311 raw_spin_lock(&this_rq->lock); in newidle_balance()
11313 if (curr_cost > this_rq->max_idle_balance_cost) in newidle_balance()
11314 this_rq->max_idle_balance_cost = curr_cost; in newidle_balance()
11322 if (this_rq->cfs.h_nr_running && !pulled_task) in newidle_balance()
11326 if (time_after(this_rq->next_balance, next_balance)) in newidle_balance()
11327 this_rq->next_balance = next_balance; in newidle_balance()
11330 if (this_rq->nr_running != this_rq->cfs.h_nr_running) in newidle_balance()
11331 pulled_task = -1; in newidle_balance()
11334 this_rq->idle_stamp = 0; in newidle_balance()
11348 enum cpu_idle_type idle = this_rq->idle_balance ? in run_rebalance_domains()
11356 if (cpu_isolated(this_rq->cpu)) in run_rebalance_domains()
11371 update_blocked_averages(this_rq->cpu); in run_rebalance_domains()
11386 if (time_after_eq(jiffies, rq->next_balance)) in trigger_load_balance()
11418 raw_spin_lock_irqsave(&rq->lock, flags); in kick_active_balance()
11419 if (!rq->active_balance) { in kick_active_balance()
11420 rq->active_balance = 1; in kick_active_balance()
11421 rq->push_cpu = new_cpu; in kick_active_balance()
11423 rq->push_task = p; in kick_active_balance()
11426 raw_spin_unlock_irqrestore(&rq->lock, flags); in kick_active_balance()
11434 int new_cpu = -1; in check_for_migration_fair()
11448 if (rq->misfit_task_load) { in check_for_migration_fair()
11449 if (rq->curr->state != TASK_RUNNING || in check_for_migration_fair()
11450 rq->curr->nr_cpus_allowed == 1) in check_for_migration_fair()
11458 if (new_cpu != -1 && need_down_migrate && in check_for_migration_fair()
11463 if (new_cpu != -1 && in check_for_migration_fair()
11474 if (new_cpu == -1 || in check_for_migration_fair()
11486 &rq->active_balance_work); in check_for_migration_fair()
11511 struct sched_entity *se = &curr->se; in task_tick_fair()
11527 * - child not yet on the tasklist
11528 * - preemption disabled
11533 struct sched_entity *se = &p->se, *curr; in task_fork_fair()
11540 cfs_rq = task_cfs_rq(current); in task_fork_fair()
11541 curr = cfs_rq->curr; in task_fork_fair()
11544 se->vruntime = curr->vruntime; in task_fork_fair()
11551 * 'current' within the tree based on its new key value. in task_fork_fair()
11553 swap(curr->vruntime, se->vruntime); in task_fork_fair()
11557 se->vruntime -= cfs_rq->min_vruntime; in task_fork_fair()
11563 * the current task.
11571 if (rq->cfs.nr_running == 1) in prio_changed_fair()
11577 * this runqueue and our priority is higher than the current's in prio_changed_fair()
11579 if (rq->curr == p) { in prio_changed_fair()
11580 if (p->prio > oldprio) in prio_changed_fair()
11588 struct sched_entity *se = &p->se; in vruntime_normalized()
11595 if (p->on_rq) in vruntime_normalized()
11602 * - A forked child which is waiting for being woken up by in vruntime_normalized()
11604 * - A task which has been woken up by try_to_wake_up() and in vruntime_normalized()
11607 if (!se->sum_exec_runtime || in vruntime_normalized()
11608 (p->state == TASK_WAKING && p->sched_remote_wakeup)) in vruntime_normalized()
11626 se = se->parent; in propagate_entity_cfs_rq()
11662 * Since the real-depth could have been changed (only FAIR in attach_entity_cfs_rq()
11665 se->depth = se->parent ? se->parent->depth + 1 : 0; in attach_entity_cfs_rq()
11677 struct sched_entity *se = &p->se; in detach_task_cfs_rq()
11682 * Fix up our vruntime so that the current sleep doesn't in detach_task_cfs_rq()
11686 se->vruntime -= cfs_rq->min_vruntime; in detach_task_cfs_rq()
11694 struct sched_entity *se = &p->se; in attach_task_cfs_rq()
11700 se->vruntime += cfs_rq->min_vruntime; in attach_task_cfs_rq()
11716 * if we can still preempt the current task. in switched_to_fair()
11718 if (rq->curr == p) in switched_to_fair()
11727 * This routine is mostly called to set cfs_rq->curr field when a task
11732 struct sched_entity *se = &p->se; in set_next_task_fair()
11740 list_move(&se->group_node, &rq->cfs_tasks); in set_next_task_fair()
11755 cfs_rq->tasks_timeline = RB_ROOT_CACHED; in init_cfs_rq()
11756 cfs_rq->min_vruntime = (u64)(-(1LL << 20)); in init_cfs_rq()
11758 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime; in init_cfs_rq()
11761 raw_spin_lock_init(&cfs_rq->removed.lock); in init_cfs_rq()
11768 struct sched_entity *se = &p->se; in task_set_group_fair()
11771 se->depth = se->parent ? se->parent->depth + 1 : 0; in task_set_group_fair()
11780 /* Tell se's cfs_rq has been changed -- migrated */ in task_move_group_fair()
11781 p->se.avg.last_update_time = 0; in task_move_group_fair()
11806 if (tg->cfs_rq) in free_fair_sched_group()
11807 kfree(tg->cfs_rq[i]); in free_fair_sched_group()
11808 if (tg->se) in free_fair_sched_group()
11809 kfree(tg->se[i]); in free_fair_sched_group()
11812 kfree(tg->cfs_rq); in free_fair_sched_group()
11813 kfree(tg->se); in free_fair_sched_group()
11822 tg->cfs_rq = kcalloc(nr_cpu_ids, sizeof(cfs_rq), GFP_KERNEL); in alloc_fair_sched_group()
11823 if (!tg->cfs_rq) in alloc_fair_sched_group()
11825 tg->se = kcalloc(nr_cpu_ids, sizeof(se), GFP_KERNEL); in alloc_fair_sched_group()
11826 if (!tg->se) in alloc_fair_sched_group()
11829 tg->shares = NICE_0_LOAD; in alloc_fair_sched_group()
11845 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]); in alloc_fair_sched_group()
11866 se = tg->se[i]; in online_fair_sched_group()
11882 if (tg->se[cpu]) in unregister_fair_sched_group()
11883 remove_entity_load_avg(tg->se[cpu]); in unregister_fair_sched_group()
11887 * check on_list without danger of it being re-added. in unregister_fair_sched_group()
11889 if (!tg->cfs_rq[cpu]->on_list) in unregister_fair_sched_group()
11894 raw_spin_lock_irqsave(&rq->lock, flags); in unregister_fair_sched_group()
11895 list_del_leaf_cfs_rq(tg->cfs_rq[cpu]); in unregister_fair_sched_group()
11896 raw_spin_unlock_irqrestore(&rq->lock, flags); in unregister_fair_sched_group()
11906 cfs_rq->tg = tg; in init_tg_cfs_entry()
11907 cfs_rq->rq = rq; in init_tg_cfs_entry()
11910 tg->cfs_rq[cpu] = cfs_rq; in init_tg_cfs_entry()
11911 tg->se[cpu] = se; in init_tg_cfs_entry()
11918 se->cfs_rq = &rq->cfs; in init_tg_cfs_entry()
11919 se->depth = 0; in init_tg_cfs_entry()
11921 se->cfs_rq = parent->my_q; in init_tg_cfs_entry()
11922 se->depth = parent->depth + 1; in init_tg_cfs_entry()
11925 se->my_q = cfs_rq; in init_tg_cfs_entry()
11927 update_load_set(&se->load, NICE_0_LOAD); in init_tg_cfs_entry()
11928 se->parent = parent; in init_tg_cfs_entry()
11940 if (!tg->se[0]) in sched_group_set_shares()
11941 return -EINVAL; in sched_group_set_shares()
11946 if (tg->shares == shares) in sched_group_set_shares()
11949 tg->shares = shares; in sched_group_set_shares()
11952 struct sched_entity *se = tg->se[i]; in sched_group_set_shares()
11987 struct sched_entity *se = &task->se; in get_rr_interval_fair()
11994 if (rq->cfs.load.weight) in get_rr_interval_fair()
12073 ng = rcu_dereference(p->numa_group); in show_numa_stats()
12075 if (p->numa_faults) { in show_numa_stats()
12076 tsf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 0)]; in show_numa_stats()
12077 tpf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 1)]; in show_numa_stats()
12080 gsf = ng->faults[task_faults_idx(NUMA_MEM, node, 0)], in show_numa_stats()
12081 gpf = ng->faults[task_faults_idx(NUMA_MEM, node, 1)]; in show_numa_stats()
12111 cfs_rq->walt_stats.cumulative_runnable_avg_scaled = 0; in walt_init_cfs_rq_stats()
12116 fixup_cumulative_runnable_avg(&cfs_rq->walt_stats, in walt_inc_cfs_rq_stats()
12117 p->ravg.demand_scaled); in walt_inc_cfs_rq_stats()
12122 fixup_cumulative_runnable_avg(&cfs_rq->walt_stats, in walt_dec_cfs_rq_stats()
12123 -(s64)p->ravg.demand_scaled); in walt_dec_cfs_rq_stats()
12132 tcfs_rq->walt_stats.cumulative_runnable_avg_scaled); in walt_inc_throttled_cfs_rq_stats()
12134 if (stats == &rq->walt_stats) in walt_inc_throttled_cfs_rq_stats()
12136 tcfs_rq->walt_stats.cumulative_runnable_avg_scaled); in walt_inc_throttled_cfs_rq_stats()
12146 -tcfs_rq->walt_stats.cumulative_runnable_avg_scaled); in walt_dec_throttled_cfs_rq_stats()
12153 if (stats == &rq->walt_stats) in walt_dec_throttled_cfs_rq_stats()
12155 -tcfs_rq->walt_stats.cumulative_runnable_avg_scaled); in walt_dec_throttled_cfs_rq_stats()
12162 struct sched_entity *se = &p->se; in walt_fixup_sched_stats_fair()
12163 s64 task_load_delta = (s64)updated_demand_scaled - in walt_fixup_sched_stats_fair()
12164 p->ravg.demand_scaled; in walt_fixup_sched_stats_fair()
12169 fixup_cumulative_runnable_avg(&cfs_rq->walt_stats, in walt_fixup_sched_stats_fair()
12175 /* Fix up rq->walt_stats only if we didn't find any throttled cfs_rq */ in walt_fixup_sched_stats_fair()
12177 fixup_cumulative_runnable_avg(&rq->walt_stats, in walt_fixup_sched_stats_fair()
12199 return cfs_rq ? &cfs_rq->avg : NULL; in sched_trace_cfs_rq_avg()
12222 return cfs_rq ? cpu_of(rq_of(cfs_rq)) : -1; in sched_trace_cfs_rq_cpu()
12229 return rq ? &rq->avg_rt : NULL; in sched_trace_rq_avg_rt()
12239 return rq ? &rq->avg_dl : NULL; in sched_trace_rq_avg_dl()
12249 return rq ? &rq->avg_irq : NULL; in sched_trace_rq_avg_irq()
12258 return rq ? cpu_of(rq) : -1; in sched_trace_rq_cpu()
12266 rq->cpu_capacity in sched_trace_rq_cpu_capacity()
12270 : -1; in sched_trace_rq_cpu_capacity()
12277 return rd ? rd->span : NULL; in sched_trace_rd_span()
12286 return rq ? rq->nr_running : -1; in sched_trace_rq_nr_running()