Lines Matching +full:wake +full:- +full:on +full:- +full:motion

1 // SPDX-License-Identifier: GPL-2.0
43 #include <linux/memory-tiers.h>
61 * The initial- and re-scaling of tunables is configurable
65 * SCHED_TUNABLESCALING_NONE - unscaled, always *1
66 * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus)
67 * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus
74 * Minimal preemption granularity for CPU-bound tasks:
108 return -cpu; in arch_asym_cpu_priority()
129 * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool
188 lw->weight += inc; in update_load_add()
189 lw->inv_weight = 0; in update_load_add()
194 lw->weight -= dec; in update_load_sub()
195 lw->inv_weight = 0; in update_load_sub()
200 lw->weight = w; in update_load_set()
201 lw->inv_weight = 0; in update_load_set()
208 * so pick a second-best guess by going with the log2 of the
256 if (likely(lw->inv_weight)) in __update_inv_weight()
259 w = scale_load_down(lw->weight); in __update_inv_weight()
262 lw->inv_weight = 1; in __update_inv_weight()
264 lw->inv_weight = WMULT_CONST; in __update_inv_weight()
266 lw->inv_weight = WMULT_CONST / w; in __update_inv_weight()
272 * (delta_exec * (weight * lw->inv_weight)) >> WMULT_SHIFT
292 shift -= fs; in __calc_delta()
296 fact = mul_u32_u32(fact, lw->inv_weight); in __calc_delta()
301 shift -= fs; in __calc_delta()
313 if (unlikely(se->load.weight != NICE_0_LOAD)) in calc_delta_fair()
314 delta = __calc_delta(delta, NICE_0_LOAD, &se->load); in calc_delta_fair()
322 * CFS operations on generic schedulable entities:
329 for (; se; se = se->parent)
336 if (cfs_rq->on_list) in list_add_leaf_cfs_rq()
337 return rq->tmp_alone_branch == &rq->leaf_cfs_rq_list; in list_add_leaf_cfs_rq()
339 cfs_rq->on_list = 1; in list_add_leaf_cfs_rq()
344 * enqueued. The fact that we always enqueue bottom-up in list_add_leaf_cfs_rq()
350 if (cfs_rq->tg->parent && in list_add_leaf_cfs_rq()
351 cfs_rq->tg->parent->cfs_rq[cpu]->on_list) { in list_add_leaf_cfs_rq()
353 * If parent is already on the list, we add the child in list_add_leaf_cfs_rq()
358 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list, in list_add_leaf_cfs_rq()
359 &(cfs_rq->tg->parent->cfs_rq[cpu]->leaf_cfs_rq_list)); in list_add_leaf_cfs_rq()
365 rq->tmp_alone_branch = &rq->leaf_cfs_rq_list; in list_add_leaf_cfs_rq()
369 if (!cfs_rq->tg->parent) { in list_add_leaf_cfs_rq()
374 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list, in list_add_leaf_cfs_rq()
375 &rq->leaf_cfs_rq_list); in list_add_leaf_cfs_rq()
380 rq->tmp_alone_branch = &rq->leaf_cfs_rq_list; in list_add_leaf_cfs_rq()
390 list_add_rcu(&cfs_rq->leaf_cfs_rq_list, rq->tmp_alone_branch); in list_add_leaf_cfs_rq()
395 rq->tmp_alone_branch = &cfs_rq->leaf_cfs_rq_list; in list_add_leaf_cfs_rq()
401 if (cfs_rq->on_list) { in list_del_leaf_cfs_rq()
408 * to the prev element but it will point to rq->leaf_cfs_rq_list in list_del_leaf_cfs_rq()
411 if (rq->tmp_alone_branch == &cfs_rq->leaf_cfs_rq_list) in list_del_leaf_cfs_rq()
412 rq->tmp_alone_branch = cfs_rq->leaf_cfs_rq_list.prev; in list_del_leaf_cfs_rq()
414 list_del_rcu(&cfs_rq->leaf_cfs_rq_list); in list_del_leaf_cfs_rq()
415 cfs_rq->on_list = 0; in list_del_leaf_cfs_rq()
421 SCHED_WARN_ON(rq->tmp_alone_branch != &rq->leaf_cfs_rq_list); in assert_list_leaf_cfs_rq()
424 /* Iterate thr' all leaf cfs_rq's on a runqueue */
426 list_for_each_entry_safe(cfs_rq, pos, &rq->leaf_cfs_rq_list, \
433 if (se->cfs_rq == pse->cfs_rq) in is_same_group()
434 return se->cfs_rq; in is_same_group()
441 return se->parent; in parent_entity()
457 se_depth = (*se)->depth; in find_matching_se()
458 pse_depth = (*pse)->depth; in find_matching_se()
461 se_depth--; in find_matching_se()
466 pse_depth--; in find_matching_se()
478 return tg->idle > 0; in tg_is_idle()
483 return cfs_rq->idle > 0; in cfs_rq_is_idle()
512 for (cfs_rq = &rq->cfs, pos = NULL; cfs_rq; cfs_rq = pos)
550 s64 delta = (s64)(vruntime - max_vruntime); in max_vruntime()
559 s64 delta = (s64)(vruntime - min_vruntime); in min_vruntime()
569 return (s64)(a->vruntime - b->vruntime) < 0; in entity_before()
574 return (s64)(se->vruntime - cfs_rq->min_vruntime); in entity_key()
581 * Compute virtual time from the per-task service numbers:
589 * lag_i = S - s_i = w_i * (V - v_i)
595 * \Sum w_i * (V - v_i) = 0
596 * \Sum w_i * V - w_i * v_i = 0
599 * se->vruntime):
602 * V = -------------- = --------------
609 * virtual time has non-continguous motion equivalent to:
611 * V +-= lag_i / W
618 * Substitute: v_i == (v_i - v0) + v0
620 * \Sum ((v_i - v0) + v0) * w_i \Sum (v_i - v0) * w_i
621 * V = ---------------------------- = --------------------- + v0
626 * v0 := cfs_rq->min_vruntime
627 * \Sum (v_i - v0) * w_i := cfs_rq->avg_vruntime
628 * \Sum w_i := cfs_rq->avg_load
631 * the per-task service, these deltas: (v_i - v), will be in the order of the
641 unsigned long weight = scale_load_down(se->load.weight); in avg_vruntime_add()
644 cfs_rq->avg_vruntime += key * weight; in avg_vruntime_add()
645 cfs_rq->avg_load += weight; in avg_vruntime_add()
651 unsigned long weight = scale_load_down(se->load.weight); in avg_vruntime_sub()
654 cfs_rq->avg_vruntime -= key * weight; in avg_vruntime_sub()
655 cfs_rq->avg_load -= weight; in avg_vruntime_sub()
662 * v' = v + d ==> avg_vruntime' = avg_runtime - d*avg_load in avg_vruntime_update()
664 cfs_rq->avg_vruntime -= cfs_rq->avg_load * delta; in avg_vruntime_update()
673 struct sched_entity *curr = cfs_rq->curr; in avg_vruntime()
674 s64 avg = cfs_rq->avg_vruntime; in avg_vruntime()
675 long load = cfs_rq->avg_load; in avg_vruntime()
677 if (curr && curr->on_rq) { in avg_vruntime()
678 unsigned long weight = scale_load_down(curr->load.weight); in avg_vruntime()
687 avg -= (load - 1); in avg_vruntime()
691 return cfs_rq->min_vruntime + avg; in avg_vruntime()
695 * lag_i = S - s_i = w_i * (V - v_i)
698 * is possible -- by addition/removal/reweight to the tree -- to move V around
706 * -r_max < lag < max(r_max, q)
714 vlag = avruntime - se->vruntime; in entity_lag()
715 limit = calc_delta_fair(max_t(u64, 2*se->slice, TICK_NSEC), se); in entity_lag()
717 return clamp(vlag, -limit, limit); in entity_lag()
722 SCHED_WARN_ON(!se->on_rq); in update_entity_lag()
724 se->vlag = entity_lag(avg_vruntime(cfs_rq), se); in update_entity_lag()
731 * lag_i = S - s_i = w_i*(V - v_i)
733 * lag_i >= 0 -> V >= v_i
735 * \Sum (v_i - v)*w_i
736 * V = ------------------ + v
739 * lag_i >= 0 -> \Sum (v_i - v)*w_i >= (v_i - v)*(\Sum w_i)
741 * Note: using 'avg_vruntime() > se->vruntime' is inacurate due
746 struct sched_entity *curr = cfs_rq->curr; in entity_eligible()
747 s64 avg = cfs_rq->avg_vruntime; in entity_eligible()
748 long load = cfs_rq->avg_load; in entity_eligible()
750 if (curr && curr->on_rq) { in entity_eligible()
751 unsigned long weight = scale_load_down(curr->load.weight); in entity_eligible()
762 u64 min_vruntime = cfs_rq->min_vruntime; in __update_min_vruntime()
766 s64 delta = (s64)(vruntime - min_vruntime); in __update_min_vruntime()
777 struct sched_entity *curr = cfs_rq->curr; in update_min_vruntime()
779 u64 vruntime = cfs_rq->min_vruntime; in update_min_vruntime()
782 if (curr->on_rq) in update_min_vruntime()
783 vruntime = curr->vruntime; in update_min_vruntime()
790 vruntime = se->vruntime; in update_min_vruntime()
792 vruntime = min_vruntime(vruntime, se->vruntime); in update_min_vruntime()
796 u64_u32_store(cfs_rq->min_vruntime, in update_min_vruntime()
805 #define deadline_gt(field, lse, rse) ({ (s64)((lse)->field - (rse)->field) > 0; })
812 se->min_deadline = rse->min_deadline; in __update_min_deadline()
817 * se->min_deadline = min(se->deadline, left->min_deadline, right->min_deadline)
821 u64 old_min_deadline = se->min_deadline; in min_deadline_update()
822 struct rb_node *node = &se->run_node; in min_deadline_update()
824 se->min_deadline = se->deadline; in min_deadline_update()
825 __update_min_deadline(se, node->rb_right); in min_deadline_update()
826 __update_min_deadline(se, node->rb_left); in min_deadline_update()
828 return se->min_deadline == old_min_deadline; in min_deadline_update()
835 * Enqueue an entity into the rb-tree:
840 se->min_deadline = se->deadline; in __enqueue_entity()
841 rb_add_augmented_cached(&se->run_node, &cfs_rq->tasks_timeline, in __enqueue_entity()
847 rb_erase_augmented_cached(&se->run_node, &cfs_rq->tasks_timeline, in __dequeue_entity()
854 struct rb_node *left = rb_first_cached(&cfs_rq->tasks_timeline); in __pick_first_entity()
873 * We can do this in O(log n) time due to an augmented RB-tree. The
874 * tree keeps the entries sorted on service, but also functions as a
875 * heap based on the deadline by keeping:
877 * se->min_deadline = min(se->deadline, se->{left,right}->min_deadline)
879 * Which allows an EDF like search on (sub)trees.
883 struct rb_node *node = cfs_rq->tasks_timeline.rb_root.rb_node; in __pick_eevdf()
884 struct sched_entity *curr = cfs_rq->curr; in __pick_eevdf()
888 if (curr && (!curr->on_rq || !entity_eligible(cfs_rq, curr))) in __pick_eevdf()
893 * Once selected, run a task until it either becomes non-eligible or in __pick_eevdf()
896 if (sched_feat(RUN_TO_PARITY) && curr && curr->vlag == curr->deadline) in __pick_eevdf()
906 node = node->rb_left; in __pick_eevdf()
920 if (node->rb_left) { in __pick_eevdf()
921 struct sched_entity *left = __node_2_se(node->rb_left); in __pick_eevdf()
931 if (left->min_deadline == se->min_deadline) in __pick_eevdf()
936 if (se->deadline == se->min_deadline) in __pick_eevdf()
940 node = node->rb_right; in __pick_eevdf()
947 if (!best_left || (s64)(best_left->min_deadline - best->deadline) > 0) in __pick_eevdf()
954 node = &best_left->run_node; in __pick_eevdf()
959 if (se->deadline == se->min_deadline) in __pick_eevdf()
963 if (node->rb_left && in __pick_eevdf()
964 __node_2_se(node->rb_left)->min_deadline == se->min_deadline) { in __pick_eevdf()
965 node = node->rb_left; in __pick_eevdf()
970 node = node->rb_right; in __pick_eevdf()
993 struct rb_node *last = rb_last(&cfs_rq->tasks_timeline.rb_root); in __pick_last_entity()
1027 if ((s64)(se->vruntime - se->deadline) < 0) in update_deadline()
1035 se->slice = sysctl_sched_base_slice; in update_deadline()
1040 se->deadline = se->vruntime + calc_delta_fair(se->slice, se); in update_deadline()
1045 if (cfs_rq->nr_running > 1) { in update_deadline()
1061 struct sched_avg *sa = &se->avg; in init_entity_runnable_average()
1072 sa->load_avg = scale_load_down(se->load.weight); in init_entity_runnable_average()
1079 * based on the cfs_rq's current util_avg:
1081 * util_avg = cfs_rq->util_avg / (cfs_rq->load_avg + 1) * se.load.weight
1090 * util_avg_cap = (cpu_scale - cfs_rq->avg.util_avg) / 2^n
1105 struct sched_entity *se = &p->se; in post_init_entity_util_avg()
1107 struct sched_avg *sa = &se->avg; in post_init_entity_util_avg()
1109 long cap = (long)(cpu_scale - cfs_rq->avg.util_avg) / 2; in post_init_entity_util_avg()
1111 if (p->sched_class != &fair_sched_class) { in post_init_entity_util_avg()
1122 se->avg.last_update_time = cfs_rq_clock_pelt(cfs_rq); in post_init_entity_util_avg()
1127 if (cfs_rq->avg.util_avg != 0) { in post_init_entity_util_avg()
1128 sa->util_avg = cfs_rq->avg.util_avg * se->load.weight; in post_init_entity_util_avg()
1129 sa->util_avg /= (cfs_rq->avg.load_avg + 1); in post_init_entity_util_avg()
1131 if (sa->util_avg > cap) in post_init_entity_util_avg()
1132 sa->util_avg = cap; in post_init_entity_util_avg()
1134 sa->util_avg = cap; in post_init_entity_util_avg()
1138 sa->runnable_avg = sa->util_avg; in post_init_entity_util_avg()
1158 delta_exec = now - curr->exec_start; in update_curr_se()
1162 curr->exec_start = now; in update_curr_se()
1163 curr->sum_exec_runtime += delta_exec; in update_curr_se()
1169 __schedstat_set(stats->exec_max, in update_curr_se()
1170 max(delta_exec, stats->exec_max)); in update_curr_se()
1188 struct task_struct *curr = rq->curr; in update_curr_common()
1191 delta_exec = update_curr_se(rq, &curr->se); in update_curr_common()
1203 struct sched_entity *curr = cfs_rq->curr; in update_curr()
1213 curr->vruntime += calc_delta_fair(delta_exec, curr); in update_curr()
1225 update_curr(cfs_rq_of(&rq->curr->se)); in update_curr_fair()
1258 * maybe already in the runqueue, the se->statistics.wait_start in update_stats_wait_end_fair()
1262 if (unlikely(!schedstat_val(stats->wait_start))) in update_stats_wait_end_fair()
1289 * Task is being enqueued - update stats:
1301 if (se != cfs_rq->curr) in update_stats_enqueue_fair()
1319 if (se != cfs_rq->curr) in update_stats_dequeue_fair()
1327 state = READ_ONCE(tsk->__state); in update_stats_dequeue_fair()
1329 __schedstat_set(tsk->stats.sleep_start, in update_stats_dequeue_fair()
1332 __schedstat_set(tsk->stats.block_start, in update_stats_dequeue_fair()
1338 * We are picking a new current task - update its stats:
1346 se->exec_start = rq_clock_task(rq_of(cfs_rq)); in update_stats_curr_start()
1387 * Allow a small imbalance based on a simple pair of communicating in adjust_numa_imbalance()
1400 * calculated based on the tasks virtual memory size and
1438 * ->numa_group (see struct task_struct for locking rules).
1442 return rcu_dereference_check(p->numa_group, p == current || in deref_task_numa_group()
1443 (lockdep_is_held(__rq_lockp(task_rq(p))) && !READ_ONCE(p->on_cpu))); in deref_task_numa_group()
1448 return rcu_dereference_protected(p->numa_group, p == current); in deref_curr_numa_group()
1460 * Calculations based on RSS as non-present and empty pages are skipped in task_nr_scan_windows()
1462 * on resident pages in task_nr_scan_windows()
1464 nr_scan_pages = sysctl_numa_balancing_scan_size << (20 - PAGE_SHIFT); in task_nr_scan_windows()
1465 rss = get_mm_rss(p->mm); in task_nr_scan_windows()
1498 ng = rcu_dereference(p->numa_group); in task_scan_start()
1503 period *= refcount_read(&ng->refcount); in task_scan_start()
1528 period *= refcount_read(&ng->refcount); in task_scan_max()
1540 rq->nr_numa_running += (p->numa_preferred_nid != NUMA_NO_NODE); in account_numa_enqueue()
1541 rq->nr_preferred_running += (p->numa_preferred_nid == task_node(p)); in account_numa_enqueue()
1546 rq->nr_numa_running -= (p->numa_preferred_nid != NUMA_NO_NODE); in account_numa_dequeue()
1547 rq->nr_preferred_running -= (p->numa_preferred_nid == task_node(p)); in account_numa_dequeue()
1565 ng = rcu_dereference(p->numa_group); in task_numa_group_id()
1567 gid = ng->gid; in task_numa_group_id()
1586 if (!p->numa_faults) in task_faults()
1589 return p->numa_faults[task_faults_idx(NUMA_MEM, nid, 0)] + in task_faults()
1590 p->numa_faults[task_faults_idx(NUMA_MEM, nid, 1)]; in task_faults()
1600 return ng->faults[task_faults_idx(NUMA_MEM, nid, 0)] + in group_faults()
1601 ng->faults[task_faults_idx(NUMA_MEM, nid, 1)]; in group_faults()
1606 return group->faults[task_faults_idx(NUMA_CPU, nid, 0)] + in group_faults_cpu()
1607 group->faults[task_faults_idx(NUMA_CPU, nid, 1)]; in group_faults_cpu()
1616 faults += ng->faults[task_faults_idx(NUMA_MEM, node, 1)]; in group_faults_priv()
1628 faults += ng->faults[task_faults_idx(NUMA_MEM, node, 0)]; in group_faults_shared()
1636 * considered part of a numa group's pseudo-interleaving set. Migrations
1643 return group_faults_cpu(ng, nid) * ACTIVE_NODE_FRACTION > ng->max_faults_cpu; in numa_is_active_node()
1646 /* Handle placement on systems where not all nodes are directly connected. */
1678 * On systems with a backplane NUMA topology, compare groups in score_nearby_nodes()
1694 * On systems with a glueless mesh NUMA topology, there are in score_nearby_nodes()
1702 faults *= (max_dist - dist); in score_nearby_nodes()
1703 faults /= (max_dist - LOCAL_DISTANCE); in score_nearby_nodes()
1714 * task group, on a particular numa node. The group weight is given a
1723 if (!p->numa_faults) in task_weight()
1726 total_faults = p->total_numa_faults; in task_weight()
1746 total_faults = ng->total_faults; in group_weight()
1782 pgdat->node_present_pages >> 4); in pgdat_free_space_enough()
1783 for (z = pgdat->nr_zones - 1; z >= 0; z--) { in pgdat_free_space_enough()
1784 struct zone *zone = pgdat->node_zones + z; in pgdat_free_space_enough()
1804 * hint page fault latency = hint page fault time - scan time
1816 return (time - last_time) & PAGE_ACCESS_TIME_MASK; in numa_hint_fault_latency()
1833 start = pgdat->nbp_rl_start; in numa_promotion_rate_limit()
1834 if (now - start > MSEC_PER_SEC && in numa_promotion_rate_limit()
1835 cmpxchg(&pgdat->nbp_rl_start, start, now) == start) in numa_promotion_rate_limit()
1836 pgdat->nbp_rl_nr_cand = nr_cand; in numa_promotion_rate_limit()
1837 if (nr_cand - pgdat->nbp_rl_nr_cand >= rate_limit) in numa_promotion_rate_limit()
1853 start = pgdat->nbp_th_start; in numa_promotion_adjust_threshold()
1854 if (now - start > th_period && in numa_promotion_adjust_threshold()
1855 cmpxchg(&pgdat->nbp_th_start, start, now) == start) { in numa_promotion_adjust_threshold()
1859 diff_cand = nr_cand - pgdat->nbp_th_nr_cand; in numa_promotion_adjust_threshold()
1861 th = pgdat->nbp_threshold ? : ref_th; in numa_promotion_adjust_threshold()
1863 th = max(th - unit_th, unit_th); in numa_promotion_adjust_threshold()
1866 pgdat->nbp_th_nr_cand = nr_cand; in numa_promotion_adjust_threshold()
1867 pgdat->nbp_threshold = th; in numa_promotion_adjust_threshold()
1891 pgdat->nbp_threshold = 0; in should_numa_migrate_memory()
1897 (20 - PAGE_SHIFT); in should_numa_migrate_memory()
1900 th = pgdat->nbp_threshold ? : def_th; in should_numa_migrate_memory()
1909 this_cpupid = cpu_pid_to_cpupid(dst_cpu, current->pid); in should_numa_migrate_memory()
1918 * the lifetime of a task. The magic number 4 is based on waiting for in should_numa_migrate_memory()
1919 * two full passes of the "multi-stage node selection" test that is in should_numa_migrate_memory()
1922 if ((p->numa_preferred_nid == NUMA_NO_NODE || p->numa_scan_seq <= 4) && in should_numa_migrate_memory()
1927 * Multi-stage node selection is used in conjunction with a periodic in should_numa_migrate_memory()
1928 * migration fault to build a temporal task<->page relation. By using in should_numa_migrate_memory()
1929 * a two-stage filter we remove short/unlikely relations. in should_numa_migrate_memory()
1933 * page (n_t) (in a given time-span) to a probability. in should_numa_migrate_memory()
1941 * act on an unlikely task<->page relation. in should_numa_migrate_memory()
1947 /* Always allow migrate on private faults */ in should_numa_migrate_memory()
1951 /* A shared fault, but p->numa_group has not been set up yet. */ in should_numa_migrate_memory()
1964 * Distribute memory according to CPU & memory use on each node, in should_numa_migrate_memory()
1968 * --------------- * - > --------------- in should_numa_migrate_memory()
1998 /* Total compute capacity of CPUs on a node */
2030 if ((ns->nr_running > ns->weight) && in numa_classify()
2031 (((ns->compute_capacity * 100) < (ns->util * imbalance_pct)) || in numa_classify()
2032 ((ns->compute_capacity * imbalance_pct) < (ns->runnable * 100)))) in numa_classify()
2035 if ((ns->nr_running < ns->weight) || in numa_classify()
2036 (((ns->compute_capacity * 100) > (ns->util * imbalance_pct)) && in numa_classify()
2037 ((ns->compute_capacity * imbalance_pct) > (ns->runnable * 100)))) in numa_classify()
2078 int cpu, idle_core = -1; in update_numa_stats()
2081 ns->idle_cpu = -1; in update_numa_stats()
2087 ns->load += cpu_load(rq); in update_numa_stats()
2088 ns->runnable += cpu_runnable(rq); in update_numa_stats()
2089 ns->util += cpu_util_cfs(cpu); in update_numa_stats()
2090 ns->nr_running += rq->cfs.h_nr_running; in update_numa_stats()
2091 ns->compute_capacity += capacity_of(cpu); in update_numa_stats()
2093 if (find_idle && idle_core < 0 && !rq->nr_running && idle_cpu(cpu)) { in update_numa_stats()
2094 if (READ_ONCE(rq->numa_migrate_on) || in update_numa_stats()
2095 !cpumask_test_cpu(cpu, env->p->cpus_ptr)) in update_numa_stats()
2098 if (ns->idle_cpu == -1) in update_numa_stats()
2099 ns->idle_cpu = cpu; in update_numa_stats()
2106 ns->weight = cpumask_weight(cpumask_of_node(nid)); in update_numa_stats()
2108 ns->node_type = numa_classify(env->imbalance_pct, ns); in update_numa_stats()
2111 ns->idle_cpu = idle_core; in update_numa_stats()
2117 struct rq *rq = cpu_rq(env->dst_cpu); in task_numa_assign()
2119 /* Check if run-queue part of active NUMA balance. */ in task_numa_assign()
2120 if (env->best_cpu != env->dst_cpu && xchg(&rq->numa_migrate_on, 1)) { in task_numa_assign()
2122 int start = env->dst_cpu; in task_numa_assign()
2125 for_each_cpu_wrap(cpu, cpumask_of_node(env->dst_nid), start + 1) { in task_numa_assign()
2126 if (cpu == env->best_cpu || !idle_cpu(cpu) || in task_numa_assign()
2127 !cpumask_test_cpu(cpu, env->p->cpus_ptr)) { in task_numa_assign()
2131 env->dst_cpu = cpu; in task_numa_assign()
2132 rq = cpu_rq(env->dst_cpu); in task_numa_assign()
2133 if (!xchg(&rq->numa_migrate_on, 1)) in task_numa_assign()
2143 * Clear previous best_cpu/rq numa-migrate flag, since task now in task_numa_assign()
2146 if (env->best_cpu != -1 && env->best_cpu != env->dst_cpu) { in task_numa_assign()
2147 rq = cpu_rq(env->best_cpu); in task_numa_assign()
2148 WRITE_ONCE(rq->numa_migrate_on, 0); in task_numa_assign()
2151 if (env->best_task) in task_numa_assign()
2152 put_task_struct(env->best_task); in task_numa_assign()
2156 env->best_task = p; in task_numa_assign()
2157 env->best_imp = imp; in task_numa_assign()
2158 env->best_cpu = env->dst_cpu; in task_numa_assign()
2169 * The load is corrected for the CPU capacity available on each node. in load_too_imbalanced()
2172 * ------------ vs --------- in load_too_imbalanced()
2175 src_capacity = env->src_stats.compute_capacity; in load_too_imbalanced()
2176 dst_capacity = env->dst_stats.compute_capacity; in load_too_imbalanced()
2178 imb = abs(dst_load * src_capacity - src_load * dst_capacity); in load_too_imbalanced()
2180 orig_src_load = env->src_stats.load; in load_too_imbalanced()
2181 orig_dst_load = env->dst_stats.load; in load_too_imbalanced()
2183 old_imb = abs(orig_dst_load * src_capacity - orig_src_load * dst_capacity); in load_too_imbalanced()
2199 * into account that it might be best if task running on the dst_cpu should
2205 struct numa_group *cur_ng, *p_ng = deref_curr_numa_group(env->p); in task_numa_compare()
2206 struct rq *dst_rq = cpu_rq(env->dst_cpu); in task_numa_compare()
2210 int dist = env->dist; in task_numa_compare()
2215 if (READ_ONCE(dst_rq->numa_migrate_on)) in task_numa_compare()
2219 cur = rcu_dereference(dst_rq->curr); in task_numa_compare()
2220 if (cur && ((cur->flags & PF_EXITING) || is_idle_task(cur))) in task_numa_compare()
2225 * end try selecting ourselves (current == env->p) as a swap candidate. in task_numa_compare()
2227 if (cur == env->p) { in task_numa_compare()
2233 if (maymove && moveimp >= env->best_imp) in task_numa_compare()
2240 if (!cpumask_test_cpu(env->src_cpu, cur->cpus_ptr)) in task_numa_compare()
2247 if (env->best_task && in task_numa_compare()
2248 env->best_task->numa_preferred_nid == env->src_nid && in task_numa_compare()
2249 cur->numa_preferred_nid != env->src_nid) { in task_numa_compare()
2263 cur_ng = rcu_dereference(cur->numa_group); in task_numa_compare()
2271 if (env->dst_stats.node_type == node_has_spare) in task_numa_compare()
2274 imp = taskimp + task_weight(cur, env->src_nid, dist) - in task_numa_compare()
2275 task_weight(cur, env->dst_nid, dist); in task_numa_compare()
2281 imp -= imp / 16; in task_numa_compare()
2288 imp += group_weight(cur, env->src_nid, dist) - in task_numa_compare()
2289 group_weight(cur, env->dst_nid, dist); in task_numa_compare()
2291 imp += task_weight(cur, env->src_nid, dist) - in task_numa_compare()
2292 task_weight(cur, env->dst_nid, dist); in task_numa_compare()
2295 /* Discourage picking a task already on its preferred node */ in task_numa_compare()
2296 if (cur->numa_preferred_nid == env->dst_nid) in task_numa_compare()
2297 imp -= imp / 16; in task_numa_compare()
2305 if (cur->numa_preferred_nid == env->src_nid) in task_numa_compare()
2308 if (maymove && moveimp > imp && moveimp > env->best_imp) { in task_numa_compare()
2318 if (env->best_task && cur->numa_preferred_nid == env->src_nid && in task_numa_compare()
2319 env->best_task->numa_preferred_nid != env->src_nid) { in task_numa_compare()
2329 if (imp < SMALLIMP || imp <= env->best_imp + SMALLIMP / 2) in task_numa_compare()
2335 load = task_h_load(env->p) - task_h_load(cur); in task_numa_compare()
2339 dst_load = env->dst_stats.load + load; in task_numa_compare()
2340 src_load = env->src_stats.load - load; in task_numa_compare()
2348 int cpu = env->dst_stats.idle_cpu; in task_numa_compare()
2352 cpu = env->dst_cpu; in task_numa_compare()
2358 if (!idle_cpu(cpu) && env->best_cpu >= 0 && in task_numa_compare()
2359 idle_cpu(env->best_cpu)) { in task_numa_compare()
2360 cpu = env->best_cpu; in task_numa_compare()
2363 env->dst_cpu = cpu; in task_numa_compare()
2373 if (maymove && !cur && env->best_cpu >= 0 && idle_cpu(env->best_cpu)) in task_numa_compare()
2380 if (!maymove && env->best_task && in task_numa_compare()
2381 env->best_task->numa_preferred_nid == env->src_nid) { in task_numa_compare()
2400 if (env->dst_stats.node_type == node_has_spare) { in task_numa_find_cpu()
2410 src_running = env->src_stats.nr_running - 1; in task_numa_find_cpu()
2411 dst_running = env->dst_stats.nr_running + 1; in task_numa_find_cpu()
2412 imbalance = max(0, dst_running - src_running); in task_numa_find_cpu()
2414 env->imb_numa_nr); in task_numa_find_cpu()
2419 if (env->dst_stats.idle_cpu >= 0) { in task_numa_find_cpu()
2420 env->dst_cpu = env->dst_stats.idle_cpu; in task_numa_find_cpu()
2428 * If the improvement from just moving env->p direction is better in task_numa_find_cpu()
2431 load = task_h_load(env->p); in task_numa_find_cpu()
2432 dst_load = env->dst_stats.load + load; in task_numa_find_cpu()
2433 src_load = env->src_stats.load - load; in task_numa_find_cpu()
2437 for_each_cpu(cpu, cpumask_of_node(env->dst_nid)) { in task_numa_find_cpu()
2439 if (!cpumask_test_cpu(cpu, env->p->cpus_ptr)) in task_numa_find_cpu()
2442 env->dst_cpu = cpu; in task_numa_find_cpu()
2460 .best_cpu = -1, in task_numa_migrate()
2474 * random movement of tasks -- counter the numa conditions we're trying in task_numa_migrate()
2480 env.imbalance_pct = 100 + (sd->imbalance_pct - 100) / 2; in task_numa_migrate()
2481 env.imb_numa_nr = sd->imb_numa_nr; in task_numa_migrate()
2493 return -EINVAL; in task_numa_migrate()
2496 env.dst_nid = p->numa_preferred_nid; in task_numa_migrate()
2501 taskimp = task_weight(p, env.dst_nid, dist) - taskweight; in task_numa_migrate()
2502 groupimp = group_weight(p, env.dst_nid, dist) - groupweight; in task_numa_migrate()
2505 /* Try to find a spot on the preferred nid. */ in task_numa_migrate()
2510 * - there is no space available on the preferred_nid in task_numa_migrate()
2511 * - the task is part of a numa_group that is interleaved across in task_numa_migrate()
2516 if (env.best_cpu == -1 || (ng && ng->active_nodes > 1)) { in task_numa_migrate()
2518 if (nid == env.src_nid || nid == p->numa_preferred_nid) in task_numa_migrate()
2529 taskimp = task_weight(p, nid, dist) - taskweight; in task_numa_migrate()
2530 groupimp = group_weight(p, nid, dist) - groupweight; in task_numa_migrate()
2550 if (env.best_cpu == -1) in task_numa_migrate()
2555 if (nid != p->numa_preferred_nid) in task_numa_migrate()
2560 if (env.best_cpu == -1) { in task_numa_migrate()
2561 trace_sched_stick_numa(p, env.src_cpu, NULL, -1); in task_numa_migrate()
2562 return -EAGAIN; in task_numa_migrate()
2568 WRITE_ONCE(best_rq->numa_migrate_on, 0); in task_numa_migrate()
2575 WRITE_ONCE(best_rq->numa_migrate_on, 0); in task_numa_migrate()
2583 /* Attempt to migrate a task to a CPU on the preferred node. */
2589 if (unlikely(p->numa_preferred_nid == NUMA_NO_NODE || !p->numa_faults)) in numa_migrate_preferred()
2593 interval = min(interval, msecs_to_jiffies(p->numa_scan_period) / 16); in numa_migrate_preferred()
2594 p->numa_migrate_retry = jiffies + interval; in numa_migrate_preferred()
2596 /* Success if task is already running on preferred CPU */ in numa_migrate_preferred()
2597 if (task_node(p) == p->numa_preferred_nid) in numa_migrate_preferred()
2600 /* Otherwise, try migrate to a CPU on the preferred node */ in numa_migrate_preferred()
2605 * Find out how many nodes the workload is actively running on. Do this by
2627 numa_group->max_faults_cpu = max_faults; in numa_group_count_active_nodes()
2628 numa_group->active_nodes = active_nodes; in numa_group_count_active_nodes()
2643 * our memory is already on our local node, or if the majority of
2654 unsigned long remote = p->numa_faults_locality[0]; in update_task_scan_period()
2655 unsigned long local = p->numa_faults_locality[1]; in update_task_scan_period()
2664 if (local + shared == 0 || p->numa_faults_locality[2]) { in update_task_scan_period()
2665 p->numa_scan_period = min(p->numa_scan_period_max, in update_task_scan_period()
2666 p->numa_scan_period << 1); in update_task_scan_period()
2668 p->mm->numa_next_scan = jiffies + in update_task_scan_period()
2669 msecs_to_jiffies(p->numa_scan_period); in update_task_scan_period()
2680 period_slot = DIV_ROUND_UP(p->numa_scan_period, NUMA_PERIOD_SLOTS); in update_task_scan_period()
2689 int slot = ps_ratio - NUMA_PERIOD_THRESHOLD; in update_task_scan_period()
2699 int slot = lr_ratio - NUMA_PERIOD_THRESHOLD; in update_task_scan_period()
2705 * Private memory faults exceed (SLOTS-THRESHOLD)/SLOTS, in update_task_scan_period()
2706 * yet they are not on the local NUMA node. Speed up in update_task_scan_period()
2710 diff = -(NUMA_PERIOD_THRESHOLD - ratio) * period_slot; in update_task_scan_period()
2713 p->numa_scan_period = clamp(p->numa_scan_period + diff, in update_task_scan_period()
2715 memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); in update_task_scan_period()
2721 * decays those on a 32ms period, which is orders of magnitude off
2722 * from the dozens-of-seconds NUMA balancing period. Use the scheduler
2729 now = p->se.exec_start; in numa_get_avg_runtime()
2730 runtime = p->se.sum_exec_runtime; in numa_get_avg_runtime()
2732 if (p->last_task_numa_placement) { in numa_get_avg_runtime()
2733 delta = runtime - p->last_sum_exec_runtime; in numa_get_avg_runtime()
2734 *period = now - p->last_task_numa_placement; in numa_get_avg_runtime()
2740 delta = p->se.avg.load_sum; in numa_get_avg_runtime()
2744 p->last_sum_exec_runtime = runtime; in numa_get_avg_runtime()
2745 p->last_task_numa_placement = now; in numa_get_avg_runtime()
2765 * On a system with glueless mesh NUMA topology, group_weight in preferred_group_nid()
2766 * scores nodes according to the number of NUMA hinting faults on in preferred_group_nid()
2767 * both the node itself, and on nearby nodes. in preferred_group_nid()
2795 for (dist = sched_max_numa_distance; dist > LOCAL_DISTANCE; dist--) { in preferred_group_nid()
2849 * The p->mm->numa_scan_seq field gets updated without in task_numa_placement()
2853 seq = READ_ONCE(p->mm->numa_scan_seq); in task_numa_placement()
2854 if (p->numa_scan_seq == seq) in task_numa_placement()
2856 p->numa_scan_seq = seq; in task_numa_placement()
2857 p->numa_scan_period_max = task_scan_max(p); in task_numa_placement()
2859 total_faults = p->numa_faults_locality[0] + in task_numa_placement()
2860 p->numa_faults_locality[1]; in task_numa_placement()
2866 group_lock = &ng->lock; in task_numa_placement()
2886 diff = p->numa_faults[membuf_idx] - p->numa_faults[mem_idx] / 2; in task_numa_placement()
2887 fault_types[priv] += p->numa_faults[membuf_idx]; in task_numa_placement()
2888 p->numa_faults[membuf_idx] = 0; in task_numa_placement()
2894 * little over-all impact on throughput, and thus their in task_numa_placement()
2898 f_weight = (f_weight * p->numa_faults[cpubuf_idx]) / in task_numa_placement()
2900 f_diff = f_weight - p->numa_faults[cpu_idx] / 2; in task_numa_placement()
2901 p->numa_faults[cpubuf_idx] = 0; in task_numa_placement()
2903 p->numa_faults[mem_idx] += diff; in task_numa_placement()
2904 p->numa_faults[cpu_idx] += f_diff; in task_numa_placement()
2905 faults += p->numa_faults[mem_idx]; in task_numa_placement()
2906 p->total_numa_faults += diff; in task_numa_placement()
2915 ng->faults[mem_idx] += diff; in task_numa_placement()
2916 ng->faults[cpu_idx] += f_diff; in task_numa_placement()
2917 ng->total_faults += diff; in task_numa_placement()
2918 group_faults += ng->faults[mem_idx]; in task_numa_placement()
2933 /* Cannot migrate task to CPU-less node */ in task_numa_placement()
2956 if (max_nid != p->numa_preferred_nid) in task_numa_placement()
2965 return refcount_inc_not_zero(&grp->refcount); in get_numa_group()
2970 if (refcount_dec_and_test(&grp->refcount)) in put_numa_group()
2992 refcount_set(&grp->refcount, 1); in task_numa_group()
2993 grp->active_nodes = 1; in task_numa_group()
2994 grp->max_faults_cpu = 0; in task_numa_group()
2995 spin_lock_init(&grp->lock); in task_numa_group()
2996 grp->gid = p->pid; in task_numa_group()
2999 grp->faults[i] = p->numa_faults[i]; in task_numa_group()
3001 grp->total_faults = p->total_numa_faults; in task_numa_group()
3003 grp->nr_tasks++; in task_numa_group()
3004 rcu_assign_pointer(p->numa_group, grp); in task_numa_group()
3008 tsk = READ_ONCE(cpu_rq(cpu)->curr); in task_numa_group()
3013 grp = rcu_dereference(tsk->numa_group); in task_numa_group()
3025 if (my_grp->nr_tasks > grp->nr_tasks) in task_numa_group()
3029 * Tie-break on the grp address. in task_numa_group()
3031 if (my_grp->nr_tasks == grp->nr_tasks && my_grp > grp) in task_numa_group()
3035 if (tsk->mm == current->mm) in task_numa_group()
3042 /* Update priv based on whether false sharing was detected */ in task_numa_group()
3054 double_lock_irq(&my_grp->lock, &grp->lock); in task_numa_group()
3057 my_grp->faults[i] -= p->numa_faults[i]; in task_numa_group()
3058 grp->faults[i] += p->numa_faults[i]; in task_numa_group()
3060 my_grp->total_faults -= p->total_numa_faults; in task_numa_group()
3061 grp->total_faults += p->total_numa_faults; in task_numa_group()
3063 my_grp->nr_tasks--; in task_numa_group()
3064 grp->nr_tasks++; in task_numa_group()
3066 spin_unlock(&my_grp->lock); in task_numa_group()
3067 spin_unlock_irq(&grp->lock); in task_numa_group()
3069 rcu_assign_pointer(p->numa_group, grp); in task_numa_group()
3084 * reset the data back to default state without freeing ->numa_faults.
3089 struct numa_group *grp = rcu_dereference_raw(p->numa_group); in task_numa_free()
3090 unsigned long *numa_faults = p->numa_faults; in task_numa_free()
3098 spin_lock_irqsave(&grp->lock, flags); in task_numa_free()
3100 grp->faults[i] -= p->numa_faults[i]; in task_numa_free()
3101 grp->total_faults -= p->total_numa_faults; in task_numa_free()
3103 grp->nr_tasks--; in task_numa_free()
3104 spin_unlock_irqrestore(&grp->lock, flags); in task_numa_free()
3105 RCU_INIT_POINTER(p->numa_group, NULL); in task_numa_free()
3110 p->numa_faults = NULL; in task_numa_free()
3113 p->total_numa_faults = 0; in task_numa_free()
3120 * Got a PROT_NONE fault for a page on @node.
3135 if (!p->mm) in task_numa_fault()
3147 /* Allocate buffer to track faults on a per-node basis */ in task_numa_fault()
3148 if (unlikely(!p->numa_faults)) { in task_numa_fault()
3149 int size = sizeof(*p->numa_faults) * in task_numa_fault()
3152 p->numa_faults = kzalloc(size, GFP_KERNEL|__GFP_NOWARN); in task_numa_fault()
3153 if (!p->numa_faults) in task_numa_fault()
3156 p->total_numa_faults = 0; in task_numa_fault()
3157 memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); in task_numa_fault()
3164 if (unlikely(last_cpupid == (-1 & LAST_CPUPID_MASK))) { in task_numa_fault()
3179 if (!priv && !local && ng && ng->active_nodes > 1 && in task_numa_fault()
3188 if (time_after(jiffies, p->numa_migrate_retry)) { in task_numa_fault()
3194 p->numa_pages_migrated += pages; in task_numa_fault()
3196 p->numa_faults_locality[2] += pages; in task_numa_fault()
3198 p->numa_faults[task_faults_idx(NUMA_MEMBUF, mem_node, priv)] += pages; in task_numa_fault()
3199 p->numa_faults[task_faults_idx(NUMA_CPUBUF, cpu_node, priv)] += pages; in task_numa_fault()
3200 p->numa_faults_locality[local] += pages; in task_numa_fault()
3207 * p->mm->numa_scan_seq is written to without exclusive access in reset_ptenuma_scan()
3213 WRITE_ONCE(p->mm->numa_scan_seq, READ_ONCE(p->mm->numa_scan_seq) + 1); in reset_ptenuma_scan()
3214 p->mm->numa_scan_offset = 0; in reset_ptenuma_scan()
3226 if ((READ_ONCE(current->mm->numa_scan_seq) - vma->numab_state->start_scan_seq) < 2) in vma_is_accessed()
3229 pids = vma->numab_state->pids_active[0] | vma->numab_state->pids_active[1]; in vma_is_accessed()
3230 if (test_bit(hash_32(current->pid, ilog2(BITS_PER_LONG)), &pids)) in vma_is_accessed()
3235 * some VMAs may never be scanned in multi-threaded applications: in vma_is_accessed()
3237 if (mm->numa_scan_offset > vma->vm_start) { in vma_is_accessed()
3247 if (READ_ONCE(mm->numa_scan_seq) > in vma_is_accessed()
3248 (vma->numab_state->prev_scan_seq + get_nr_threads(current))) in vma_is_accessed()
3264 struct mm_struct *mm = p->mm; in task_numa_work()
3265 u64 runtime = p->se.sum_exec_runtime; in task_numa_work()
3276 work->next = work; in task_numa_work()
3280 * NOTE: make sure not to dereference p->mm before this check, in task_numa_work()
3282 * without p->mm even though we still had it when we enqueued this in task_numa_work()
3285 if (p->flags & PF_EXITING) in task_numa_work()
3288 if (!mm->numa_next_scan) { in task_numa_work()
3289 mm->numa_next_scan = now + in task_numa_work()
3296 migrate = mm->numa_next_scan; in task_numa_work()
3300 if (p->numa_scan_period == 0) { in task_numa_work()
3301 p->numa_scan_period_max = task_scan_max(p); in task_numa_work()
3302 p->numa_scan_period = task_scan_start(p); in task_numa_work()
3305 next_scan = now + msecs_to_jiffies(p->numa_scan_period); in task_numa_work()
3306 if (!try_cmpxchg(&mm->numa_next_scan, &migrate, next_scan)) in task_numa_work()
3313 p->node_stamp += 2 * TICK_NSEC; in task_numa_work()
3316 pages <<= 20 - PAGE_SHIFT; /* MB in pages */ in task_numa_work()
3333 start = mm->numa_scan_offset; in task_numa_work()
3345 is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_MIXEDMAP)) { in task_numa_work()
3353 * hinting faults in read-only file-backed mappings or the vdso in task_numa_work()
3356 if (!vma->vm_mm || in task_numa_work()
3357 (vma->vm_file && (vma->vm_flags & (VM_READ|VM_WRITE)) == (VM_READ))) { in task_numa_work()
3371 /* Initialise new per-VMA NUMAB state. */ in task_numa_work()
3372 if (!vma->numab_state) { in task_numa_work()
3379 if (cmpxchg(&vma->numab_state, NULL, ptr)) { in task_numa_work()
3384 vma->numab_state->start_scan_seq = mm->numa_scan_seq; in task_numa_work()
3386 vma->numab_state->next_scan = now + in task_numa_work()
3390 vma->numab_state->pids_active_reset = vma->numab_state->next_scan + in task_numa_work()
3395 * to prevent VMAs being skipped prematurely on the in task_numa_work()
3398 vma->numab_state->prev_scan_seq = mm->numa_scan_seq - 1; in task_numa_work()
3405 if (mm->numa_scan_seq && time_before(jiffies, in task_numa_work()
3406 vma->numab_state->next_scan)) { in task_numa_work()
3412 if (mm->numa_scan_seq && in task_numa_work()
3413 time_after(jiffies, vma->numab_state->pids_active_reset)) { in task_numa_work()
3414 vma->numab_state->pids_active_reset = vma->numab_state->pids_active_reset + in task_numa_work()
3416 vma->numab_state->pids_active[0] = READ_ONCE(vma->numab_state->pids_active[1]); in task_numa_work()
3417 vma->numab_state->pids_active[1] = 0; in task_numa_work()
3421 if (vma->numab_state->prev_scan_seq == mm->numa_scan_seq) { in task_numa_work()
3422 mm->numa_scan_offset = vma->vm_end; in task_numa_work()
3438 start = max(start, vma->vm_start); in task_numa_work()
3440 end = min(end, vma->vm_end); in task_numa_work()
3446 * is not already pte-numa. If the VMA contains in task_numa_work()
3452 pages -= (end - start) >> PAGE_SHIFT; in task_numa_work()
3453 virtpages -= (end - start) >> PAGE_SHIFT; in task_numa_work()
3460 } while (end != vma->vm_end); in task_numa_work()
3463 vma->numab_state->prev_scan_seq = mm->numa_scan_seq; in task_numa_work()
3487 * would find the !migratable VMA on the next scan but not reset the in task_numa_work()
3491 mm->numa_scan_offset = start; in task_numa_work()
3499 * Usually update_task_scan_period slows down scanning enough; on an in task_numa_work()
3500 * overloaded system we need to limit overhead on a per task basis. in task_numa_work()
3502 if (unlikely(p->se.sum_exec_runtime != runtime)) { in task_numa_work()
3503 u64 diff = p->se.sum_exec_runtime - runtime; in task_numa_work()
3504 p->node_stamp += 32 * diff; in task_numa_work()
3511 struct mm_struct *mm = p->mm; in init_numa_balancing()
3514 mm_users = atomic_read(&mm->mm_users); in init_numa_balancing()
3516 mm->numa_next_scan = jiffies + msecs_to_jiffies(sysctl_numa_balancing_scan_delay); in init_numa_balancing()
3517 mm->numa_scan_seq = 0; in init_numa_balancing()
3520 p->node_stamp = 0; in init_numa_balancing()
3521 p->numa_scan_seq = mm ? mm->numa_scan_seq : 0; in init_numa_balancing()
3522 p->numa_scan_period = sysctl_numa_balancing_scan_delay; in init_numa_balancing()
3523 p->numa_migrate_retry = 0; in init_numa_balancing()
3525 p->numa_work.next = &p->numa_work; in init_numa_balancing()
3526 p->numa_faults = NULL; in init_numa_balancing()
3527 p->numa_pages_migrated = 0; in init_numa_balancing()
3528 p->total_numa_faults = 0; in init_numa_balancing()
3529 RCU_INIT_POINTER(p->numa_group, NULL); in init_numa_balancing()
3530 p->last_task_numa_placement = 0; in init_numa_balancing()
3531 p->last_sum_exec_runtime = 0; in init_numa_balancing()
3533 init_task_work(&p->numa_work, task_numa_work); in init_numa_balancing()
3537 p->numa_preferred_nid = NUMA_NO_NODE; in init_numa_balancing()
3549 current->numa_scan_period * mm_users * NSEC_PER_MSEC); in init_numa_balancing()
3551 p->node_stamp = delay; in init_numa_balancing()
3560 struct callback_head *work = &curr->numa_work; in task_tick_numa()
3566 if (!curr->mm || (curr->flags & (PF_EXITING | PF_KTHREAD)) || work->next != work) in task_tick_numa()
3575 now = curr->se.sum_exec_runtime; in task_tick_numa()
3576 period = (u64)curr->numa_scan_period * NSEC_PER_MSEC; in task_tick_numa()
3578 if (now > curr->node_stamp + period) { in task_tick_numa()
3579 if (!curr->node_stamp) in task_tick_numa()
3580 curr->numa_scan_period = task_scan_start(curr); in task_tick_numa()
3581 curr->node_stamp += period; in task_tick_numa()
3583 if (!time_before(jiffies, curr->mm->numa_next_scan)) in task_tick_numa()
3596 if (!p->mm || !p->numa_faults || (p->flags & PF_EXITING)) in update_scan_period()
3605 * is pulled cross-node due to wakeups or load balancing. in update_scan_period()
3607 if (p->numa_scan_seq) { in update_scan_period()
3610 * node or if the task was not previously running on in update_scan_period()
3613 if (dst_nid == p->numa_preferred_nid || in update_scan_period()
3614 (p->numa_preferred_nid != NUMA_NO_NODE && in update_scan_period()
3615 src_nid != p->numa_preferred_nid)) in update_scan_period()
3619 p->numa_scan_period = task_scan_start(p); in update_scan_period()
3644 update_load_add(&cfs_rq->load, se->load.weight); in account_entity_enqueue()
3650 list_add(&se->group_node, &rq->cfs_tasks); in account_entity_enqueue()
3653 cfs_rq->nr_running++; in account_entity_enqueue()
3655 cfs_rq->idle_nr_running++; in account_entity_enqueue()
3661 update_load_sub(&cfs_rq->load, se->load.weight); in account_entity_dequeue()
3665 list_del_init(&se->group_node); in account_entity_dequeue()
3668 cfs_rq->nr_running--; in account_entity_dequeue()
3670 cfs_rq->idle_nr_running--; in account_entity_dequeue()
3674 * Signed add and clamp on underflow.
3676 * Explicitly do a load-store to ensure the intermediate value never hits
3694 * Unsigned subtract and clamp on underflow.
3696 * Explicitly do a load-store to ensure the intermediate value never hits
3704 res = var - val; \
3711 * Remove and clamp on negative, from a local variable.
3713 * A variant of sub_positive(), which does not use explicit load-store
3718 *ptr -= min_t(typeof(*ptr), *ptr, _val); \
3725 cfs_rq->avg.load_avg += se->avg.load_avg; in enqueue_load_avg()
3726 cfs_rq->avg.load_sum += se_weight(se) * se->avg.load_sum; in enqueue_load_avg()
3732 sub_positive(&cfs_rq->avg.load_avg, se->avg.load_avg); in dequeue_load_avg()
3733 sub_positive(&cfs_rq->avg.load_sum, se_weight(se) * se->avg.load_sum); in dequeue_load_avg()
3735 cfs_rq->avg.load_sum = max_t(u32, cfs_rq->avg.load_sum, in dequeue_load_avg()
3736 cfs_rq->avg.load_avg * PELT_MIN_DIVIDER); in dequeue_load_avg()
3748 unsigned long old_weight = se->load.weight; in reweight_eevdf()
3756 * adjusted if re-weight at !0-lag point. in reweight_eevdf()
3759 * re-weight without changing vruntime at !0-lag point. in reweight_eevdf()
3761 * Weight VRuntime Avg-VRuntime in reweight_eevdf()
3765 * Since lag needs to be preserved through re-weight: in reweight_eevdf()
3767 * lag = (V - v)*w = (V'- v')*w', where v = v' in reweight_eevdf()
3768 * ==> V' = (V - v)*w/w' + v (1) in reweight_eevdf()
3773 * V' = (WV + w'v - wv) / (W + w' - w) (2) in reweight_eevdf()
3777 * (WV + w'v - wv) / (W + w' - w) = (V - v)*w/w' + v in reweight_eevdf()
3778 * ==> (WV-Wv+Wv+w'v-wv)/(W+w'-w) = (V - v)*w/w' + v in reweight_eevdf()
3779 * ==> (WV - Wv)/(W + w' - w) + v = (V - v)*w/w' + v in reweight_eevdf()
3780 * ==> (V - v)*W/(W + w' - w) = (V - v)*w/w' (3) in reweight_eevdf()
3782 * Since we are doing at !0-lag point which means V != v, we in reweight_eevdf()
3785 * ==> W / (W + w' - w) = w / w' in reweight_eevdf()
3786 * ==> Ww' = Ww + ww' - ww in reweight_eevdf()
3787 * ==> W * (w' - w) = w * (w' - w) in reweight_eevdf()
3788 * ==> W = w (re-weight indicates w' != w) in reweight_eevdf()
3792 * average vruntime @V, which means we will always re-weight in reweight_eevdf()
3793 * at 0-lag point, thus breach assumption. Proof completed. in reweight_eevdf()
3796 * COROLLARY #2: Re-weight does NOT affect weighted average in reweight_eevdf()
3801 * (V - v)*w = (V' - v')*w' in reweight_eevdf()
3802 * ==> v' = V' - (V - v)*w/w' (4) in reweight_eevdf()
3806 * V' = (WV - wv + w'v') / (W - w + w') in reweight_eevdf()
3807 * = (WV - wv + w'(V' - (V - v)w/w')) / (W - w + w') in reweight_eevdf()
3808 * = (WV - wv + w'V' - Vw + wv) / (W - w + w') in reweight_eevdf()
3809 * = (WV + w'V' - Vw) / (W - w + w') in reweight_eevdf()
3811 * ==> V'*(W - w + w') = WV + w'V' - Vw in reweight_eevdf()
3812 * ==> V' * (W - w) = (W - w) * V (5) in reweight_eevdf()
3815 * always occurs at 0-lag point, so V won't change. Or else in reweight_eevdf()
3820 * So according to corollary #1 & #2, the effect of re-weight in reweight_eevdf()
3821 * on vruntime should be: in reweight_eevdf()
3823 * v' = V' - (V - v) * w / w' (4) in reweight_eevdf()
3824 * = V - (V - v) * w / w' in reweight_eevdf()
3825 * = V - vl * w / w' in reweight_eevdf()
3826 * = V - vl' in reweight_eevdf()
3828 if (avruntime != se->vruntime) { in reweight_eevdf()
3831 se->vruntime = avruntime - vlag; in reweight_eevdf()
3841 * d' = v' + (d - v)*w/w' in reweight_eevdf()
3842 * = V' - (V - v)*w/w' + (d - v)*w/w' in reweight_eevdf()
3843 * = V - (V - v)*w/w' + (d - v)*w/w' in reweight_eevdf()
3844 * = V + (d - V)*w/w' in reweight_eevdf()
3846 vslice = (s64)(se->deadline - avruntime); in reweight_eevdf()
3848 se->deadline = avruntime + vslice; in reweight_eevdf()
3854 bool curr = cfs_rq->curr == se; in reweight_entity()
3857 if (se->on_rq) { in reweight_entity()
3863 update_load_sub(&cfs_rq->load, se->load.weight); in reweight_entity()
3867 if (se->on_rq) { in reweight_entity()
3871 * Because we keep se->vlag = V - v_i, while: lag_i = w_i*(V - v_i), in reweight_entity()
3872 * we need to scale se->vlag when w_i changes. in reweight_entity()
3874 se->vlag = div_s64(se->vlag * se->load.weight, weight); in reweight_entity()
3877 update_load_set(&se->load, weight); in reweight_entity()
3881 u32 divider = get_pelt_divider(&se->avg); in reweight_entity()
3883 se->avg.load_avg = div_u64(se_weight(se) * se->avg.load_sum, divider); in reweight_entity()
3888 if (se->on_rq) { in reweight_entity()
3889 update_load_add(&cfs_rq->load, se->load.weight); in reweight_entity()
3895 * whether the rq-wide min_vruntime needs updated too. Since in reweight_entity()
3897 * than up-to-date one, we do the update at the end of the in reweight_entity()
3906 struct sched_entity *se = &p->se; in reweight_task()
3908 struct load_weight *load = &se->load; in reweight_task()
3910 reweight_entity(cfs_rq, se, lw->weight); in reweight_task()
3911 load->inv_weight = lw->inv_weight; in reweight_task()
3923 * group weight based on the group runqueue weights. That is:
3925 * tg->weight * grq->load.weight
3926 * ge->load.weight = ----------------------------- (1)
3927 * \Sum grq->load.weight
3935 * grq->load.weight -> grq->avg.load_avg (2)
3939 * tg->weight * grq->avg.load_avg
3940 * ge->load.weight = ------------------------------ (3)
3941 * tg->load_avg
3943 * Where: tg->load_avg ~= \Sum grq->avg.load_avg
3947 * The problem with it is that because the average is slow -- it was designed
3948 * to be exactly that of course -- this leads to transients in boundary
3950 * one task. It takes time for our CPU's grq->avg.load_avg to build up,
3955 * tg->weight * grq->load.weight
3956 * ge->load.weight = ----------------------------- = tg->weight (4)
3957 * grp->load.weight
3964 * ge->load.weight =
3966 * tg->weight * grq->load.weight
3967 * --------------------------------------------------- (5)
3968 * tg->load_avg - grq->avg.load_avg + grq->load.weight
3970 * But because grq->load.weight can drop to 0, resulting in a divide by zero,
3971 * we need to use grq->avg.load_avg as its lower bound, which then gives:
3974 * tg->weight * grq->load.weight
3975 * ge->load.weight = ----------------------------- (6)
3980 * tg_load_avg' = tg->load_avg - grq->avg.load_avg +
3981 * max(grq->load.weight, grq->avg.load_avg)
3985 * overestimates the ge->load.weight and therefore:
3987 * \Sum ge->load.weight >= tg->weight
3994 struct task_group *tg = cfs_rq->tg; in calc_group_shares()
3996 tg_shares = READ_ONCE(tg->shares); in calc_group_shares()
3998 load = max(scale_load_down(cfs_rq->load.weight), cfs_rq->avg.load_avg); in calc_group_shares()
4000 tg_weight = atomic_long_read(&tg->load_avg); in calc_group_shares()
4003 tg_weight -= cfs_rq->tg_load_avg_contrib; in calc_group_shares()
4011 * MIN_SHARES has to be unscaled here to support per-CPU partitioning in calc_group_shares()
4012 * of a group with small tg->shares value. It is a floor value which is in calc_group_shares()
4014 * the group on a CPU. in calc_group_shares()
4016 * E.g. on 64-bit for a group with tg->shares of scale_load(15)=15*1024 in calc_group_shares()
4017 * on an 8-core system with 8 tasks each runnable on one CPU shares has in calc_group_shares()
4019 * case no task is runnable on a CPU MIN_SHARES=2 should be returned in calc_group_shares()
4027 * Recomputes the group entity based on the current state of its group
4042 shares = READ_ONCE(gcfs_rq->tg->shares); in update_cfs_group()
4046 if (unlikely(se->load.weight != shares)) in update_cfs_group()
4060 if (&rq->cfs == cfs_rq) { in cfs_rq_util_change()
4070 * As is, the util number is not freq-invariant (we'd have to in cfs_rq_util_change()
4082 if (sa->load_sum) in load_avg_is_decayed()
4085 if (sa->util_sum) in load_avg_is_decayed()
4088 if (sa->runnable_sum) in load_avg_is_decayed()
4096 SCHED_WARN_ON(sa->load_avg || in load_avg_is_decayed()
4097 sa->util_avg || in load_avg_is_decayed()
4098 sa->runnable_avg); in load_avg_is_decayed()
4105 return u64_u32_load_copy(cfs_rq->avg.last_update_time, in cfs_rq_last_update_time()
4106 cfs_rq->last_update_time_copy); in cfs_rq_last_update_time()
4110 * Because list_add_leaf_cfs_rq always places a child cfs_rq on the list
4112 * bottom-up, we only have to test whether the cfs_rq before us on the list
4114 * If cfs_rq is not on the list, test whether a child needs its to be added to
4122 if (cfs_rq->on_list) { in child_cfs_rq_on_list()
4123 prev = cfs_rq->leaf_cfs_rq_list.prev; in child_cfs_rq_on_list()
4127 prev = rq->tmp_alone_branch; in child_cfs_rq_on_list()
4132 return (prev_cfs_rq->tg->parent == cfs_rq->tg); in child_cfs_rq_on_list()
4137 if (cfs_rq->load.weight) in cfs_rq_is_decayed()
4140 if (!load_avg_is_decayed(&cfs_rq->avg)) in cfs_rq_is_decayed()
4150 * update_tg_load_avg - update the tg's load avg
4153 * This function 'ensures': tg->load_avg := \Sum tg->cfs_rq[]->avg.load.
4154 * However, because tg->load_avg is a global value there are performance
4165 long delta = cfs_rq->avg.load_avg - cfs_rq->tg_load_avg_contrib; in update_tg_load_avg()
4170 if (cfs_rq->tg == &root_task_group) in update_tg_load_avg()
4173 if (abs(delta) > cfs_rq->tg_load_avg_contrib / 64) { in update_tg_load_avg()
4174 atomic_long_add(delta, &cfs_rq->tg->load_avg); in update_tg_load_avg()
4175 cfs_rq->tg_load_avg_contrib = cfs_rq->avg.load_avg; in update_tg_load_avg()
4181 * caller only guarantees p->pi_lock is held; no other assumptions,
4182 * including the state of rq->lock, should be made.
4196 * getting what current time is, so simply throw away the out-of-date in set_task_rq_fair()
4200 if (!(se->avg.last_update_time && prev)) in set_task_rq_fair()
4207 se->avg.last_update_time = n_last_update_time; in set_task_rq_fair()
4211 * When on migration a sched_entity joins/leaves the PELT hierarchy, we need to
4215 * ge->avg == grq->avg (1)
4226 * ge->avg.load_avg = ge->load.weight * ge->avg.runnable_avg (2)
4231 * grq->avg.load_avg = grq->load.weight * grq->avg.runnable_avg (3)
4235 * ge->avg.runnable_avg == grq->avg.runnable_avg
4239 * ge->load.weight * grq->avg.load_avg
4240 * ge->avg.load_avg = ----------------------------------- (4)
4241 * grq->load.weight
4254 * Another reason this doesn't work is that runnable isn't a 0-sum entity.
4256 * rq itself is runnable anywhere between 2/3 and 1 depending on how the
4265 * ge->avg.running_sum <= ge->avg.runnable_sum <= LOAD_AVG_MAX
4270 * On removal, we'll assume each task is equally runnable; which yields:
4272 * grq->avg.runnable_sum = grq->avg.load_sum / grq->load.weight
4280 long delta_sum, delta_avg = gcfs_rq->avg.util_avg - se->avg.util_avg; in update_tg_cfs_util()
4288 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in update_tg_cfs_util()
4291 divider = get_pelt_divider(&cfs_rq->avg); in update_tg_cfs_util()
4295 se->avg.util_avg = gcfs_rq->avg.util_avg; in update_tg_cfs_util()
4296 new_sum = se->avg.util_avg * divider; in update_tg_cfs_util()
4297 delta_sum = (long)new_sum - (long)se->avg.util_sum; in update_tg_cfs_util()
4298 se->avg.util_sum = new_sum; in update_tg_cfs_util()
4301 add_positive(&cfs_rq->avg.util_avg, delta_avg); in update_tg_cfs_util()
4302 add_positive(&cfs_rq->avg.util_sum, delta_sum); in update_tg_cfs_util()
4305 cfs_rq->avg.util_sum = max_t(u32, cfs_rq->avg.util_sum, in update_tg_cfs_util()
4306 cfs_rq->avg.util_avg * PELT_MIN_DIVIDER); in update_tg_cfs_util()
4312 long delta_sum, delta_avg = gcfs_rq->avg.runnable_avg - se->avg.runnable_avg; in update_tg_cfs_runnable()
4320 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in update_tg_cfs_runnable()
4323 divider = get_pelt_divider(&cfs_rq->avg); in update_tg_cfs_runnable()
4326 se->avg.runnable_avg = gcfs_rq->avg.runnable_avg; in update_tg_cfs_runnable()
4327 new_sum = se->avg.runnable_avg * divider; in update_tg_cfs_runnable()
4328 delta_sum = (long)new_sum - (long)se->avg.runnable_sum; in update_tg_cfs_runnable()
4329 se->avg.runnable_sum = new_sum; in update_tg_cfs_runnable()
4332 add_positive(&cfs_rq->avg.runnable_avg, delta_avg); in update_tg_cfs_runnable()
4333 add_positive(&cfs_rq->avg.runnable_sum, delta_sum); in update_tg_cfs_runnable()
4335 cfs_rq->avg.runnable_sum = max_t(u32, cfs_rq->avg.runnable_sum, in update_tg_cfs_runnable()
4336 cfs_rq->avg.runnable_avg * PELT_MIN_DIVIDER); in update_tg_cfs_runnable()
4342 long delta_avg, running_sum, runnable_sum = gcfs_rq->prop_runnable_sum; in update_tg_cfs_load()
4351 gcfs_rq->prop_runnable_sum = 0; in update_tg_cfs_load()
4354 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in update_tg_cfs_load()
4357 divider = get_pelt_divider(&cfs_rq->avg); in update_tg_cfs_load()
4364 runnable_sum += se->avg.load_sum; in update_tg_cfs_load()
4371 if (scale_load_down(gcfs_rq->load.weight)) { in update_tg_cfs_load()
4372 load_sum = div_u64(gcfs_rq->avg.load_sum, in update_tg_cfs_load()
4373 scale_load_down(gcfs_rq->load.weight)); in update_tg_cfs_load()
4377 runnable_sum = min(se->avg.load_sum, load_sum); in update_tg_cfs_load()
4386 running_sum = se->avg.util_sum >> SCHED_CAPACITY_SHIFT; in update_tg_cfs_load()
4392 delta_avg = load_avg - se->avg.load_avg; in update_tg_cfs_load()
4396 delta_sum = load_sum - (s64)se_weight(se) * se->avg.load_sum; in update_tg_cfs_load()
4398 se->avg.load_sum = runnable_sum; in update_tg_cfs_load()
4399 se->avg.load_avg = load_avg; in update_tg_cfs_load()
4400 add_positive(&cfs_rq->avg.load_avg, delta_avg); in update_tg_cfs_load()
4401 add_positive(&cfs_rq->avg.load_sum, delta_sum); in update_tg_cfs_load()
4403 cfs_rq->avg.load_sum = max_t(u32, cfs_rq->avg.load_sum, in update_tg_cfs_load()
4404 cfs_rq->avg.load_avg * PELT_MIN_DIVIDER); in update_tg_cfs_load()
4409 cfs_rq->propagate = 1; in add_tg_cfs_propagate()
4410 cfs_rq->prop_runnable_sum += runnable_sum; in add_tg_cfs_propagate()
4422 if (!gcfs_rq->propagate) in propagate_entity_load_avg()
4425 gcfs_rq->propagate = 0; in propagate_entity_load_avg()
4429 add_tg_cfs_propagate(cfs_rq, gcfs_rq->prop_runnable_sum); in propagate_entity_load_avg()
4453 if (se->avg.load_avg || se->avg.util_avg) in skip_blocked_update()
4460 if (gcfs_rq->propagate) in skip_blocked_update()
4492 if (load_avg_is_decayed(&se->avg)) in migrate_se_pelt_lag()
4499 is_idle = is_idle_task(rcu_dereference(rq->curr)); in migrate_se_pelt_lag()
4516 * - cfs->throttled_clock_pelt_time@cfs_rq_idle in migrate_se_pelt_lag()
4519 * = rq_clock_pelt()@rq_idle - rq_clock_pelt()@cfs_rq_idle in migrate_se_pelt_lag()
4522 * = sched_clock_cpu() - rq_clock()@rq_idle in migrate_se_pelt_lag()
4526 * now = rq_clock_pelt()@rq_idle - cfs->throttled_clock_pelt_time + in migrate_se_pelt_lag()
4527 * sched_clock_cpu() - rq_clock()@rq_idle in migrate_se_pelt_lag()
4529 * rq_clock_pelt()@rq_idle is rq->clock_pelt_idle in migrate_se_pelt_lag()
4530 * rq_clock()@rq_idle is rq->clock_idle in migrate_se_pelt_lag()
4531 * cfs->throttled_clock_pelt_time@cfs_rq_idle in migrate_se_pelt_lag()
4532 * is cfs_rq->throttled_pelt_idle in migrate_se_pelt_lag()
4536 throttled = u64_u32_load(cfs_rq->throttled_pelt_idle); in migrate_se_pelt_lag()
4541 now = u64_u32_load(rq->clock_pelt_idle); in migrate_se_pelt_lag()
4551 now -= throttled; in migrate_se_pelt_lag()
4554 * cfs_rq->avg.last_update_time is more recent than our in migrate_se_pelt_lag()
4559 now += sched_clock_cpu(cpu_of(rq)) - u64_u32_load(rq->clock_idle); in migrate_se_pelt_lag()
4568 * update_cfs_rq_load_avg - update the cfs_rq's load/util averages
4575 * cfs_rq->avg is used for task_h_load() and update_cfs_share() for example.
4579 * Since both these conditions indicate a changed cfs_rq->avg.load we should
4586 struct sched_avg *sa = &cfs_rq->avg; in update_cfs_rq_load_avg()
4589 if (cfs_rq->removed.nr) { in update_cfs_rq_load_avg()
4591 u32 divider = get_pelt_divider(&cfs_rq->avg); in update_cfs_rq_load_avg()
4593 raw_spin_lock(&cfs_rq->removed.lock); in update_cfs_rq_load_avg()
4594 swap(cfs_rq->removed.util_avg, removed_util); in update_cfs_rq_load_avg()
4595 swap(cfs_rq->removed.load_avg, removed_load); in update_cfs_rq_load_avg()
4596 swap(cfs_rq->removed.runnable_avg, removed_runnable); in update_cfs_rq_load_avg()
4597 cfs_rq->removed.nr = 0; in update_cfs_rq_load_avg()
4598 raw_spin_unlock(&cfs_rq->removed.lock); in update_cfs_rq_load_avg()
4601 sub_positive(&sa->load_avg, r); in update_cfs_rq_load_avg()
4602 sub_positive(&sa->load_sum, r * divider); in update_cfs_rq_load_avg()
4603 /* See sa->util_sum below */ in update_cfs_rq_load_avg()
4604 sa->load_sum = max_t(u32, sa->load_sum, sa->load_avg * PELT_MIN_DIVIDER); in update_cfs_rq_load_avg()
4607 sub_positive(&sa->util_avg, r); in update_cfs_rq_load_avg()
4608 sub_positive(&sa->util_sum, r * divider); in update_cfs_rq_load_avg()
4610 * Because of rounding, se->util_sum might ends up being +1 more than in update_cfs_rq_load_avg()
4611 * cfs->util_sum. Although this is not a problem by itself, detaching in update_cfs_rq_load_avg()
4613 * util_avg (~1ms) can make cfs->util_sum becoming null whereas in update_cfs_rq_load_avg()
4620 sa->util_sum = max_t(u32, sa->util_sum, sa->util_avg * PELT_MIN_DIVIDER); in update_cfs_rq_load_avg()
4623 sub_positive(&sa->runnable_avg, r); in update_cfs_rq_load_avg()
4624 sub_positive(&sa->runnable_sum, r * divider); in update_cfs_rq_load_avg()
4625 /* See sa->util_sum above */ in update_cfs_rq_load_avg()
4626 sa->runnable_sum = max_t(u32, sa->runnable_sum, in update_cfs_rq_load_avg()
4627 sa->runnable_avg * PELT_MIN_DIVIDER); in update_cfs_rq_load_avg()
4634 -(long)(removed_runnable * divider) >> SCHED_CAPACITY_SHIFT); in update_cfs_rq_load_avg()
4640 u64_u32_store_copy(sa->last_update_time, in update_cfs_rq_load_avg()
4641 cfs_rq->last_update_time_copy, in update_cfs_rq_load_avg()
4642 sa->last_update_time); in update_cfs_rq_load_avg()
4647 * attach_entity_load_avg - attach this entity to its cfs_rq load avg
4651 * Must call update_cfs_rq_load_avg() before this, since we rely on
4652 * cfs_rq->avg.last_update_time being current.
4657 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in attach_entity_load_avg()
4660 u32 divider = get_pelt_divider(&cfs_rq->avg); in attach_entity_load_avg()
4669 se->avg.last_update_time = cfs_rq->avg.last_update_time; in attach_entity_load_avg()
4670 se->avg.period_contrib = cfs_rq->avg.period_contrib; in attach_entity_load_avg()
4673 * Hell(o) Nasty stuff.. we need to recompute _sum based on the new in attach_entity_load_avg()
4678 se->avg.util_sum = se->avg.util_avg * divider; in attach_entity_load_avg()
4680 se->avg.runnable_sum = se->avg.runnable_avg * divider; in attach_entity_load_avg()
4682 se->avg.load_sum = se->avg.load_avg * divider; in attach_entity_load_avg()
4683 if (se_weight(se) < se->avg.load_sum) in attach_entity_load_avg()
4684 se->avg.load_sum = div_u64(se->avg.load_sum, se_weight(se)); in attach_entity_load_avg()
4686 se->avg.load_sum = 1; in attach_entity_load_avg()
4689 cfs_rq->avg.util_avg += se->avg.util_avg; in attach_entity_load_avg()
4690 cfs_rq->avg.util_sum += se->avg.util_sum; in attach_entity_load_avg()
4691 cfs_rq->avg.runnable_avg += se->avg.runnable_avg; in attach_entity_load_avg()
4692 cfs_rq->avg.runnable_sum += se->avg.runnable_sum; in attach_entity_load_avg()
4694 add_tg_cfs_propagate(cfs_rq, se->avg.load_sum); in attach_entity_load_avg()
4702 * detach_entity_load_avg - detach this entity from its cfs_rq load avg
4706 * Must call update_cfs_rq_load_avg() before this, since we rely on
4707 * cfs_rq->avg.last_update_time being current.
4712 sub_positive(&cfs_rq->avg.util_avg, se->avg.util_avg); in detach_entity_load_avg()
4713 sub_positive(&cfs_rq->avg.util_sum, se->avg.util_sum); in detach_entity_load_avg()
4715 cfs_rq->avg.util_sum = max_t(u32, cfs_rq->avg.util_sum, in detach_entity_load_avg()
4716 cfs_rq->avg.util_avg * PELT_MIN_DIVIDER); in detach_entity_load_avg()
4718 sub_positive(&cfs_rq->avg.runnable_avg, se->avg.runnable_avg); in detach_entity_load_avg()
4719 sub_positive(&cfs_rq->avg.runnable_sum, se->avg.runnable_sum); in detach_entity_load_avg()
4721 cfs_rq->avg.runnable_sum = max_t(u32, cfs_rq->avg.runnable_sum, in detach_entity_load_avg()
4722 cfs_rq->avg.runnable_avg * PELT_MIN_DIVIDER); in detach_entity_load_avg()
4724 add_tg_cfs_propagate(cfs_rq, -se->avg.load_sum); in detach_entity_load_avg()
4749 if (se->avg.last_update_time && !(flags & SKIP_AGE_LOAD)) in update_load_avg()
4755 if (!se->avg.last_update_time && (flags & DO_ATTACH)) { in update_load_avg()
4762 * IOW we're enqueueing a task on a new CPU. in update_load_avg()
4805 * tasks cannot exit without having gone through wake_up_new_task() -> in remove_entity_load_avg()
4812 raw_spin_lock_irqsave(&cfs_rq->removed.lock, flags); in remove_entity_load_avg()
4813 ++cfs_rq->removed.nr; in remove_entity_load_avg()
4814 cfs_rq->removed.util_avg += se->avg.util_avg; in remove_entity_load_avg()
4815 cfs_rq->removed.load_avg += se->avg.load_avg; in remove_entity_load_avg()
4816 cfs_rq->removed.runnable_avg += se->avg.runnable_avg; in remove_entity_load_avg()
4817 raw_spin_unlock_irqrestore(&cfs_rq->removed.lock, flags); in remove_entity_load_avg()
4822 return cfs_rq->avg.runnable_avg; in cfs_rq_runnable_avg()
4827 return cfs_rq->avg.load_avg; in cfs_rq_load_avg()
4834 return READ_ONCE(p->se.avg.util_avg); in task_util()
4839 struct util_est ue = READ_ONCE(p->se.avg.util_est); in _task_util_est()
4858 enqueued = cfs_rq->avg.util_est.enqueued; in util_est_enqueue()
4860 WRITE_ONCE(cfs_rq->avg.util_est.enqueued, enqueued); in util_est_enqueue()
4874 enqueued = cfs_rq->avg.util_est.enqueued; in util_est_dequeue()
4875 enqueued -= min_t(unsigned int, enqueued, _task_util_est(p)); in util_est_dequeue()
4876 WRITE_ONCE(cfs_rq->avg.util_est.enqueued, enqueued); in util_est_dequeue()
4885 * based on the observation that:
4887 * abs(x) < y := (unsigned)(x + y - 1) < (2 * y - 1)
4893 return ((unsigned int)(value + margin - 1) < (2 * margin - 1)); in within_margin()
4917 ue = p->se.avg.util_est; in util_est_update()
4924 * Reset EWMA on utilization increases, the moving average is used only in util_est_update()
4939 last_ewma_diff = ue.enqueued - ue.ewma; in util_est_update()
4940 last_enqueued_diff -= ue.enqueued; in util_est_update()
4963 * ewma(t) = w * task_util(p) + (1-w) * ewma(t-1) in util_est_update()
4964 * = w * task_util(p) + ewma(t-1) - w * ewma(t-1) in util_est_update()
4965 * = w * (task_util(p) - ewma(t-1)) + ewma(t-1) in util_est_update()
4966 * = w * ( last_ewma_diff ) + ewma(t-1) in util_est_update()
4967 * = w * (last_ewma_diff + ewma(t-1) / w) in util_est_update()
4977 WRITE_ONCE(p->se.avg.util_est, ue); in util_est_update()
4979 trace_sched_util_est_se_tp(&p->se); in util_est_update()
5011 * on available OPP of the system. in util_fits_cpu()
5020 capacity_orig_thermal = capacity_orig - arch_scale_thermal_pressure(cpu); in util_fits_cpu()
5036 * +---------------------------------------- in util_fits_cpu()
5042 * * util = 80% of x then it does not fit on cpu0 and should migrate in util_fits_cpu()
5044 * * util = 80% of y then it is forced to fit on cpu1 to honour in util_fits_cpu()
5051 * Only exception is when we are on max capacity, then we need to be in util_fits_cpu()
5074 * +---------------------------------------- in util_fits_cpu()
5096 return -1; in util_fits_cpu()
5118 if (!p || p->nr_cpus_allowed == 1) { in update_misfit_status()
5119 rq->misfit_task_load = 0; in update_misfit_status()
5124 rq->misfit_task_load = 0; in update_misfit_status()
5132 rq->misfit_task_load = max_t(unsigned long, task_h_load(p), 1); in update_misfit_status()
5139 return !cfs_rq->nr_running; in cfs_rq_is_decayed()
5183 se->slice = sysctl_sched_base_slice; in place_entity()
5184 vslice = calc_delta_fair(se->slice, se); in place_entity()
5194 if (sched_feat(PLACE_LAG) && cfs_rq->nr_running) { in place_entity()
5195 struct sched_entity *curr = cfs_rq->curr; in place_entity()
5198 lag = se->vlag; in place_entity()
5202 * consider the effect of the new entity on the weighted in place_entity()
5208 * lag_i = S - s_i = w_i * (V - v_i) in place_entity()
5213 * vl_i = V - v_i <=> v_i = V - vl_i in place_entity()
5225 * = (W*V + w_i*(V - vl_i)) / (W + w_i) in place_entity()
5226 * = (W*V + w_i*V - w_i*vl_i) / (W + w_i) in place_entity()
5227 * = (V*(W + w_i) - w_i*l) / (W + w_i) in place_entity()
5228 * = V - w_i*vl_i / (W + w_i) in place_entity()
5232 * vl'_i = V' - v_i in place_entity()
5233 * = V - w_i*vl_i / (W + w_i) - (V - vl_i) in place_entity()
5234 * = vl_i - w_i*vl_i / (W + w_i) in place_entity()
5244 * vl'_i = vl_i - w_i*vl_i / (W + w_i) in place_entity()
5245 * = ((W + w_i)*vl_i - w_i*vl_i) / (W + w_i) in place_entity()
5247 * (W + w_i)*vl'_i = (W + w_i)*vl_i - w_i*vl_i in place_entity()
5252 load = cfs_rq->avg_load; in place_entity()
5253 if (curr && curr->on_rq) in place_entity()
5254 load += scale_load_down(curr->load.weight); in place_entity()
5256 lag *= load + scale_load_down(se->load.weight); in place_entity()
5262 se->vruntime = vruntime - lag; in place_entity()
5266 * on average, halfway through their slice, as such start tasks in place_entity()
5275 se->deadline = se->vruntime + vslice; in place_entity()
5286 bool curr = cfs_rq->curr == se; in enqueue_entity()
5299 * - Update loads to have both entity and cfs_rq synced with now. in enqueue_entity()
5300 * - For group_entity, update its runnable_weight to reflect the new in enqueue_entity()
5302 * - For group_entity, update its weight to reflect the new share of in enqueue_entity()
5304 * - Add its new weight to cfs_rq->load.weight in enqueue_entity()
5310 * but update_cfs_group() here will re-adjust the weight and have to in enqueue_entity()
5316 * XXX now that the entity has been re-weighted, and it's lag adjusted, in enqueue_entity()
5326 se->exec_start = 0; in enqueue_entity()
5332 se->on_rq = 1; in enqueue_entity()
5334 if (cfs_rq->nr_running == 1) { in enqueue_entity()
5342 if (cfs_rq_throttled(cfs_rq) && !cfs_rq->throttled_clock) in enqueue_entity()
5343 cfs_rq->throttled_clock = rq_clock(rq); in enqueue_entity()
5344 if (!cfs_rq->throttled_clock_self) in enqueue_entity()
5345 cfs_rq->throttled_clock_self = rq_clock(rq); in enqueue_entity()
5355 if (cfs_rq->next != se) in __clear_buddies_next()
5358 cfs_rq->next = NULL; in __clear_buddies_next()
5364 if (cfs_rq->next == se) in clear_buddies()
5379 * Update run-time statistics of the 'current'. in dequeue_entity()
5385 * - Update loads to have both entity and cfs_rq synced with now. in dequeue_entity()
5386 * - For group_entity, update its runnable_weight to reflect the new in dequeue_entity()
5388 * - Subtract its previous weight from cfs_rq->load.weight. in dequeue_entity()
5389 * - For group entity, update its weight to reflect the new share in dequeue_entity()
5400 if (se != cfs_rq->curr) in dequeue_entity()
5402 se->on_rq = 0; in dequeue_entity()
5405 /* return excess runtime on last dequeue */ in dequeue_entity()
5413 * put back on, and if we advance min_vruntime, we'll be placed back in dequeue_entity()
5414 * further than we started -- ie. we'll be penalized. in dequeue_entity()
5419 if (cfs_rq->nr_running == 0) in dequeue_entity()
5429 if (se->on_rq) { in set_next_entity()
5431 * Any task has to be enqueued before it get to execute on in set_next_entity()
5432 * a CPU. So account for the time it spent waiting on the in set_next_entity()
5442 se->vlag = se->deadline; in set_next_entity()
5446 cfs_rq->curr = se; in set_next_entity()
5451 * when there are only lesser-weight tasks around): in set_next_entity()
5454 rq_of(cfs_rq)->cfs.load.weight >= 2*se->load.weight) { in set_next_entity()
5458 __schedstat_set(stats->slice_max, in set_next_entity()
5459 max((u64)stats->slice_max, in set_next_entity()
5460 se->sum_exec_runtime - se->prev_sum_exec_runtime)); in set_next_entity()
5463 se->prev_sum_exec_runtime = se->sum_exec_runtime; in set_next_entity()
5480 cfs_rq->next && entity_eligible(cfs_rq, cfs_rq->next)) in pick_next_entity()
5481 return cfs_rq->next; in pick_next_entity()
5491 * If still on the runqueue then deactivate_task() in put_prev_entity()
5494 if (prev->on_rq) in put_prev_entity()
5500 if (prev->on_rq) { in put_prev_entity()
5507 cfs_rq->curr = NULL; in put_prev_entity()
5514 * Update run-time statistics of the 'current'. in entity_tick()
5537 hrtimer_active(&rq_of(cfs_rq)->hrtick_timer)) in entity_tick()
5592 * directly instead of rq->clock to avoid adding additional synchronization
5593 * around rq->lock.
5595 * requires cfs_b->lock
5601 if (unlikely(cfs_b->quota == RUNTIME_INF)) in __refill_cfs_bandwidth_runtime()
5604 cfs_b->runtime += cfs_b->quota; in __refill_cfs_bandwidth_runtime()
5605 runtime = cfs_b->runtime_snap - cfs_b->runtime; in __refill_cfs_bandwidth_runtime()
5607 cfs_b->burst_time += runtime; in __refill_cfs_bandwidth_runtime()
5608 cfs_b->nr_burst++; in __refill_cfs_bandwidth_runtime()
5611 cfs_b->runtime = min(cfs_b->runtime, cfs_b->quota + cfs_b->burst); in __refill_cfs_bandwidth_runtime()
5612 cfs_b->runtime_snap = cfs_b->runtime; in __refill_cfs_bandwidth_runtime()
5617 return &tg->cfs_bandwidth; in tg_cfs_bandwidth()
5620 /* returns 0 on failure to allocate runtime */
5626 lockdep_assert_held(&cfs_b->lock); in __assign_cfs_rq_runtime()
5629 min_amount = target_runtime - cfs_rq->runtime_remaining; in __assign_cfs_rq_runtime()
5631 if (cfs_b->quota == RUNTIME_INF) in __assign_cfs_rq_runtime()
5636 if (cfs_b->runtime > 0) { in __assign_cfs_rq_runtime()
5637 amount = min(cfs_b->runtime, min_amount); in __assign_cfs_rq_runtime()
5638 cfs_b->runtime -= amount; in __assign_cfs_rq_runtime()
5639 cfs_b->idle = 0; in __assign_cfs_rq_runtime()
5643 cfs_rq->runtime_remaining += amount; in __assign_cfs_rq_runtime()
5645 return cfs_rq->runtime_remaining > 0; in __assign_cfs_rq_runtime()
5648 /* returns 0 on failure to allocate runtime */
5651 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in assign_cfs_rq_runtime()
5654 raw_spin_lock(&cfs_b->lock); in assign_cfs_rq_runtime()
5656 raw_spin_unlock(&cfs_b->lock); in assign_cfs_rq_runtime()
5664 cfs_rq->runtime_remaining -= delta_exec; in __account_cfs_rq_runtime()
5666 if (likely(cfs_rq->runtime_remaining > 0)) in __account_cfs_rq_runtime()
5669 if (cfs_rq->throttled) in __account_cfs_rq_runtime()
5675 if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr)) in __account_cfs_rq_runtime()
5682 if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled) in account_cfs_rq_runtime()
5690 return cfs_bandwidth_used() && cfs_rq->throttled; in cfs_rq_throttled()
5696 return cfs_bandwidth_used() && cfs_rq->throttle_count; in throttled_hierarchy()
5702 * load-balance operations.
5709 src_cfs_rq = tg->cfs_rq[src_cpu]; in throttled_lb_pair()
5710 dest_cfs_rq = tg->cfs_rq[dest_cpu]; in throttled_lb_pair()
5719 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in tg_unthrottle_up()
5721 cfs_rq->throttle_count--; in tg_unthrottle_up()
5722 if (!cfs_rq->throttle_count) { in tg_unthrottle_up()
5723 cfs_rq->throttled_clock_pelt_time += rq_clock_pelt(rq) - in tg_unthrottle_up()
5724 cfs_rq->throttled_clock_pelt; in tg_unthrottle_up()
5730 if (cfs_rq->throttled_clock_self) { in tg_unthrottle_up()
5731 u64 delta = rq_clock(rq) - cfs_rq->throttled_clock_self; in tg_unthrottle_up()
5733 cfs_rq->throttled_clock_self = 0; in tg_unthrottle_up()
5738 cfs_rq->throttled_clock_self_time += delta; in tg_unthrottle_up()
5748 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in tg_throttle_down()
5751 if (!cfs_rq->throttle_count) { in tg_throttle_down()
5752 cfs_rq->throttled_clock_pelt = rq_clock_pelt(rq); in tg_throttle_down()
5755 SCHED_WARN_ON(cfs_rq->throttled_clock_self); in tg_throttle_down()
5756 if (cfs_rq->nr_running) in tg_throttle_down()
5757 cfs_rq->throttled_clock_self = rq_clock(rq); in tg_throttle_down()
5759 cfs_rq->throttle_count++; in tg_throttle_down()
5767 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in throttle_cfs_rq()
5771 raw_spin_lock(&cfs_b->lock); in throttle_cfs_rq()
5784 list_add_tail_rcu(&cfs_rq->throttled_list, in throttle_cfs_rq()
5785 &cfs_b->throttled_cfs_rq); in throttle_cfs_rq()
5787 raw_spin_unlock(&cfs_b->lock); in throttle_cfs_rq()
5792 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))]; in throttle_cfs_rq()
5796 walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq); in throttle_cfs_rq()
5799 task_delta = cfs_rq->h_nr_running; in throttle_cfs_rq()
5800 idle_task_delta = cfs_rq->idle_h_nr_running; in throttle_cfs_rq()
5803 /* throttled entity or throttle-on-deactivate */ in throttle_cfs_rq()
5804 if (!se->on_rq) in throttle_cfs_rq()
5810 idle_task_delta = cfs_rq->h_nr_running; in throttle_cfs_rq()
5812 qcfs_rq->h_nr_running -= task_delta; in throttle_cfs_rq()
5813 qcfs_rq->idle_h_nr_running -= idle_task_delta; in throttle_cfs_rq()
5815 if (qcfs_rq->load.weight) { in throttle_cfs_rq()
5816 /* Avoid re-evaluating load for this entity: */ in throttle_cfs_rq()
5824 /* throttled entity or throttle-on-deactivate */ in throttle_cfs_rq()
5825 if (!se->on_rq) in throttle_cfs_rq()
5832 idle_task_delta = cfs_rq->h_nr_running; in throttle_cfs_rq()
5834 qcfs_rq->h_nr_running -= task_delta; in throttle_cfs_rq()
5835 qcfs_rq->idle_h_nr_running -= idle_task_delta; in throttle_cfs_rq()
5844 * throttled-list. rq->lock protects completion. in throttle_cfs_rq()
5846 cfs_rq->throttled = 1; in throttle_cfs_rq()
5847 SCHED_WARN_ON(cfs_rq->throttled_clock); in throttle_cfs_rq()
5848 if (cfs_rq->nr_running) in throttle_cfs_rq()
5849 cfs_rq->throttled_clock = rq_clock(rq); in throttle_cfs_rq()
5856 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in unthrottle_cfs_rq()
5860 se = cfs_rq->tg->se[cpu_of(rq)]; in unthrottle_cfs_rq()
5862 cfs_rq->throttled = 0; in unthrottle_cfs_rq()
5866 raw_spin_lock(&cfs_b->lock); in unthrottle_cfs_rq()
5867 if (cfs_rq->throttled_clock) { in unthrottle_cfs_rq()
5868 cfs_b->throttled_time += rq_clock(rq) - cfs_rq->throttled_clock; in unthrottle_cfs_rq()
5869 cfs_rq->throttled_clock = 0; in unthrottle_cfs_rq()
5871 list_del_rcu(&cfs_rq->throttled_list); in unthrottle_cfs_rq()
5872 raw_spin_unlock(&cfs_b->lock); in unthrottle_cfs_rq()
5875 walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq); in unthrottle_cfs_rq()
5877 if (!cfs_rq->load.weight) { in unthrottle_cfs_rq()
5878 if (!cfs_rq->on_list) in unthrottle_cfs_rq()
5891 task_delta = cfs_rq->h_nr_running; in unthrottle_cfs_rq()
5892 idle_task_delta = cfs_rq->idle_h_nr_running; in unthrottle_cfs_rq()
5896 if (se->on_rq) in unthrottle_cfs_rq()
5901 idle_task_delta = cfs_rq->h_nr_running; in unthrottle_cfs_rq()
5903 qcfs_rq->h_nr_running += task_delta; in unthrottle_cfs_rq()
5904 qcfs_rq->idle_h_nr_running += idle_task_delta; in unthrottle_cfs_rq()
5906 /* end evaluation on encountering a throttled cfs_rq */ in unthrottle_cfs_rq()
5918 idle_task_delta = cfs_rq->h_nr_running; in unthrottle_cfs_rq()
5920 qcfs_rq->h_nr_running += task_delta; in unthrottle_cfs_rq()
5921 qcfs_rq->idle_h_nr_running += idle_task_delta; in unthrottle_cfs_rq()
5923 /* end evaluation on encountering a throttled cfs_rq */ in unthrottle_cfs_rq()
5934 /* Determine whether we need to wake up potentially idle CPU: */ in unthrottle_cfs_rq()
5935 if (rq->curr == rq->idle && rq->cfs.nr_running) in unthrottle_cfs_rq()
5965 list_for_each_entry_safe(cursor, tmp, &rq->cfsb_csd_list, in __cfsb_csd_unthrottle()
5967 list_del_init(&cursor->throttled_csd_list); in __cfsb_csd_unthrottle()
5990 if (SCHED_WARN_ON(!list_empty(&cfs_rq->throttled_csd_list))) in __unthrottle_cfs_rq_async()
5993 first = list_empty(&rq->cfsb_csd_list); in __unthrottle_cfs_rq_async()
5994 list_add_tail(&cfs_rq->throttled_csd_list, &rq->cfsb_csd_list); in __unthrottle_cfs_rq_async()
5996 smp_call_function_single_async(cpu_of(rq), &rq->cfsb_csd); in __unthrottle_cfs_rq_async()
6010 cfs_rq->runtime_remaining <= 0)) in unthrottle_cfs_rq_async()
6027 list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq, in distribute_cfs_runtime()
6042 if (!list_empty(&cfs_rq->throttled_csd_list)) in distribute_cfs_runtime()
6047 SCHED_WARN_ON(cfs_rq->runtime_remaining > 0); in distribute_cfs_runtime()
6049 raw_spin_lock(&cfs_b->lock); in distribute_cfs_runtime()
6050 runtime = -cfs_rq->runtime_remaining + 1; in distribute_cfs_runtime()
6051 if (runtime > cfs_b->runtime) in distribute_cfs_runtime()
6052 runtime = cfs_b->runtime; in distribute_cfs_runtime()
6053 cfs_b->runtime -= runtime; in distribute_cfs_runtime()
6054 remaining = cfs_b->runtime; in distribute_cfs_runtime()
6055 raw_spin_unlock(&cfs_b->lock); in distribute_cfs_runtime()
6057 cfs_rq->runtime_remaining += runtime; in distribute_cfs_runtime()
6060 if (cfs_rq->runtime_remaining > 0) { in distribute_cfs_runtime()
6089 * period the timer is deactivated until scheduling resumes; cfs_b->idle is
6097 if (cfs_b->quota == RUNTIME_INF) in do_sched_cfs_period_timer()
6100 throttled = !list_empty(&cfs_b->throttled_cfs_rq); in do_sched_cfs_period_timer()
6101 cfs_b->nr_periods += overrun; in do_sched_cfs_period_timer()
6103 /* Refill extra burst quota even if cfs_b->idle */ in do_sched_cfs_period_timer()
6107 * idle depends on !throttled (for the case of a large deficit), and if in do_sched_cfs_period_timer()
6110 if (cfs_b->idle && !throttled) in do_sched_cfs_period_timer()
6115 cfs_b->idle = 1; in do_sched_cfs_period_timer()
6120 cfs_b->nr_throttled += overrun; in do_sched_cfs_period_timer()
6123 * This check is repeated as we release cfs_b->lock while we unthrottle. in do_sched_cfs_period_timer()
6125 while (throttled && cfs_b->runtime > 0) { in do_sched_cfs_period_timer()
6126 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in do_sched_cfs_period_timer()
6127 /* we can't nest cfs_b->lock while distributing bandwidth */ in do_sched_cfs_period_timer()
6129 raw_spin_lock_irqsave(&cfs_b->lock, flags); in do_sched_cfs_period_timer()
6138 cfs_b->idle = 0; in do_sched_cfs_period_timer()
6156 * Requires cfs_b->lock for hrtimer_expires_remaining to be safe against the
6162 struct hrtimer *refresh_timer = &cfs_b->period_timer; in runtime_refresh_within()
6165 /* if the call-back is running a quota refresh is already occurring */ in runtime_refresh_within()
6186 if (cfs_b->slack_started) in start_cfs_slack_bandwidth()
6188 cfs_b->slack_started = true; in start_cfs_slack_bandwidth()
6190 hrtimer_start(&cfs_b->slack_timer, in start_cfs_slack_bandwidth()
6198 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in __return_cfs_rq_runtime()
6199 s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime; in __return_cfs_rq_runtime()
6204 raw_spin_lock(&cfs_b->lock); in __return_cfs_rq_runtime()
6205 if (cfs_b->quota != RUNTIME_INF) { in __return_cfs_rq_runtime()
6206 cfs_b->runtime += slack_runtime; in __return_cfs_rq_runtime()
6208 /* we are under rq->lock, defer unthrottling using a timer */ in __return_cfs_rq_runtime()
6209 if (cfs_b->runtime > sched_cfs_bandwidth_slice() && in __return_cfs_rq_runtime()
6210 !list_empty(&cfs_b->throttled_cfs_rq)) in __return_cfs_rq_runtime()
6213 raw_spin_unlock(&cfs_b->lock); in __return_cfs_rq_runtime()
6216 cfs_rq->runtime_remaining -= slack_runtime; in __return_cfs_rq_runtime()
6224 if (!cfs_rq->runtime_enabled || cfs_rq->nr_running) in return_cfs_rq_runtime()
6232 * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
6240 raw_spin_lock_irqsave(&cfs_b->lock, flags); in do_sched_cfs_slack_timer()
6241 cfs_b->slack_started = false; in do_sched_cfs_slack_timer()
6244 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in do_sched_cfs_slack_timer()
6248 if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) in do_sched_cfs_slack_timer()
6249 runtime = cfs_b->runtime; in do_sched_cfs_slack_timer()
6251 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in do_sched_cfs_slack_timer()
6262 * runtime as update_curr() throttling can not trigger until it's on-rq.
6269 /* an active group must be handled by the update_curr()->put() path */ in check_enqueue_throttle()
6270 if (!cfs_rq->runtime_enabled || cfs_rq->curr) in check_enqueue_throttle()
6279 if (cfs_rq->runtime_remaining <= 0) in check_enqueue_throttle()
6290 if (!tg->parent) in sync_throttle()
6293 cfs_rq = tg->cfs_rq[cpu]; in sync_throttle()
6294 pcfs_rq = tg->parent->cfs_rq[cpu]; in sync_throttle()
6296 cfs_rq->throttle_count = pcfs_rq->throttle_count; in sync_throttle()
6297 cfs_rq->throttled_clock_pelt = rq_clock_pelt(cpu_rq(cpu)); in sync_throttle()
6306 if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0)) in check_cfs_rq_runtime()
6340 raw_spin_lock_irqsave(&cfs_b->lock, flags); in sched_cfs_period_timer()
6342 overrun = hrtimer_forward_now(timer, cfs_b->period); in sched_cfs_period_timer()
6349 u64 new, old = ktime_to_ns(cfs_b->period); in sched_cfs_period_timer()
6358 cfs_b->period = ns_to_ktime(new); in sched_cfs_period_timer()
6359 cfs_b->quota *= 2; in sched_cfs_period_timer()
6360 cfs_b->burst *= 2; in sched_cfs_period_timer()
6366 div_u64(cfs_b->quota, NSEC_PER_USEC)); in sched_cfs_period_timer()
6372 div_u64(cfs_b->quota, NSEC_PER_USEC)); in sched_cfs_period_timer()
6380 cfs_b->period_active = 0; in sched_cfs_period_timer()
6381 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in sched_cfs_period_timer()
6388 raw_spin_lock_init(&cfs_b->lock); in init_cfs_bandwidth()
6389 cfs_b->runtime = 0; in init_cfs_bandwidth()
6390 cfs_b->quota = RUNTIME_INF; in init_cfs_bandwidth()
6391 cfs_b->period = ns_to_ktime(default_cfs_period()); in init_cfs_bandwidth()
6392 cfs_b->burst = 0; in init_cfs_bandwidth()
6393 cfs_b->hierarchical_quota = parent ? parent->hierarchical_quota : RUNTIME_INF; in init_cfs_bandwidth()
6395 INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq); in init_cfs_bandwidth()
6396 hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED); in init_cfs_bandwidth()
6397 cfs_b->period_timer.function = sched_cfs_period_timer; in init_cfs_bandwidth()
6400 hrtimer_set_expires(&cfs_b->period_timer, in init_cfs_bandwidth()
6401 get_random_u32_below(cfs_b->period)); in init_cfs_bandwidth()
6402 hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); in init_cfs_bandwidth()
6403 cfs_b->slack_timer.function = sched_cfs_slack_timer; in init_cfs_bandwidth()
6404 cfs_b->slack_started = false; in init_cfs_bandwidth()
6409 cfs_rq->runtime_enabled = 0; in init_cfs_rq_runtime()
6410 INIT_LIST_HEAD(&cfs_rq->throttled_list); in init_cfs_rq_runtime()
6412 INIT_LIST_HEAD(&cfs_rq->throttled_csd_list); in init_cfs_rq_runtime()
6418 lockdep_assert_held(&cfs_b->lock); in start_cfs_bandwidth()
6420 if (cfs_b->period_active) in start_cfs_bandwidth()
6423 cfs_b->period_active = 1; in start_cfs_bandwidth()
6424 hrtimer_forward_now(&cfs_b->period_timer, cfs_b->period); in start_cfs_bandwidth()
6425 hrtimer_start_expires(&cfs_b->period_timer, HRTIMER_MODE_ABS_PINNED); in start_cfs_bandwidth()
6433 if (!cfs_b->throttled_cfs_rq.next) in destroy_cfs_bandwidth()
6436 hrtimer_cancel(&cfs_b->period_timer); in destroy_cfs_bandwidth()
6437 hrtimer_cancel(&cfs_b->slack_timer); in destroy_cfs_bandwidth()
6440 * It is possible that we still have some cfs_rq's pending on a CSD in destroy_cfs_bandwidth()
6454 if (list_empty(&rq->cfsb_csd_list)) in destroy_cfs_bandwidth()
6480 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth; in update_runtime_enabled()
6481 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in update_runtime_enabled()
6483 raw_spin_lock(&cfs_b->lock); in update_runtime_enabled()
6484 cfs_rq->runtime_enabled = cfs_b->quota != RUNTIME_INF; in update_runtime_enabled()
6485 raw_spin_unlock(&cfs_b->lock); in update_runtime_enabled()
6506 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in unthrottle_offline_cfs_rqs()
6508 if (!cfs_rq->runtime_enabled) in unthrottle_offline_cfs_rqs()
6515 cfs_rq->runtime_remaining = 1; in unthrottle_offline_cfs_rqs()
6520 cfs_rq->runtime_enabled = 0; in unthrottle_offline_cfs_rqs()
6537 if (cfs_rq->runtime_enabled || in cfs_task_bw_constrained()
6538 tg_cfs_bandwidth(cfs_rq->tg)->hierarchical_quota != RUNTIME_INF) in cfs_task_bw_constrained()
6556 if (rq->nr_running != 1) in sched_fair_update_stop_tick()
6624 * CFS operations on tasks:
6630 struct sched_entity *se = &p->se; in hrtick_start_fair()
6634 if (rq->cfs.h_nr_running > 1) { in hrtick_start_fair()
6635 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime; in hrtick_start_fair()
6636 u64 slice = se->slice; in hrtick_start_fair()
6637 s64 delta = slice - ran; in hrtick_start_fair()
6655 struct task_struct *curr = rq->curr; in hrtick_update()
6657 if (!hrtick_enabled_fair(rq) || curr->sched_class != &fair_sched_class) in hrtick_update()
6694 WRITE_ONCE(rd->overutilized, status); in set_rd_overutilized_status()
6707 if (!READ_ONCE(rq->rd->overutilized) && cpu_overutilized(rq->cpu)) in check_update_overutilized_status()
6708 set_rd_overutilized_status(rq->rd, SG_OVERUTILIZED); in check_update_overutilized_status()
6717 return unlikely(rq->nr_running == rq->cfs.idle_h_nr_running && in sched_idle_rq()
6718 rq->nr_running); in sched_idle_rq()
6737 struct sched_entity *se = &p->se; in enqueue_task_fair()
6747 util_est_enqueue(&rq->cfs, p); in enqueue_task_fair()
6754 if (p->in_iowait) in enqueue_task_fair()
6758 if (se->on_rq) in enqueue_task_fair()
6763 cfs_rq->h_nr_running++; in enqueue_task_fair()
6764 cfs_rq->idle_h_nr_running += idle_h_nr_running; in enqueue_task_fair()
6769 /* end evaluation on encountering a throttled cfs_rq */ in enqueue_task_fair()
6783 cfs_rq->h_nr_running++; in enqueue_task_fair()
6784 cfs_rq->idle_h_nr_running += idle_h_nr_running; in enqueue_task_fair()
6789 /* end evaluation on encountering a throttled cfs_rq */ in enqueue_task_fair()
6830 struct sched_entity *se = &p->se; in dequeue_task_fair()
6835 util_est_dequeue(&rq->cfs, p); in dequeue_task_fair()
6841 cfs_rq->h_nr_running--; in dequeue_task_fair()
6842 cfs_rq->idle_h_nr_running -= idle_h_nr_running; in dequeue_task_fair()
6847 /* end evaluation on encountering a throttled cfs_rq */ in dequeue_task_fair()
6852 if (cfs_rq->load.weight) { in dequeue_task_fair()
6853 /* Avoid re-evaluating load for this entity: */ in dequeue_task_fair()
6873 cfs_rq->h_nr_running--; in dequeue_task_fair()
6874 cfs_rq->idle_h_nr_running -= idle_h_nr_running; in dequeue_task_fair()
6879 /* end evaluation on encountering a throttled cfs_rq */ in dequeue_task_fair()
6890 rq->next_balance = jiffies; in dequeue_task_fair()
6893 util_est_update(&rq->cfs, p, task_sleep); in dequeue_task_fair()
6919 return cfs_rq_load_avg(&rq->cfs); in cpu_load()
6923 * cpu_load_without - compute CPU load without any contributions from *p
6927 * The load of a CPU is defined by the load of tasks currently enqueued on that
6928 * CPU as well as tasks which are currently sleeping after an execution on that
6941 if (cpu_of(rq) != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in cpu_load_without()
6944 cfs_rq = &rq->cfs; in cpu_load_without()
6945 load = READ_ONCE(cfs_rq->avg.load_avg); in cpu_load_without()
6955 return cfs_rq_runnable_avg(&rq->cfs); in cpu_runnable()
6964 if (cpu_of(rq) != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in cpu_runnable_without()
6967 cfs_rq = &rq->cfs; in cpu_runnable_without()
6968 runnable = READ_ONCE(cfs_rq->avg.runnable_avg); in cpu_runnable_without()
6971 lsub_positive(&runnable, p->se.avg.runnable_avg); in cpu_runnable_without()
6978 return cpu_rq(cpu)->cpu_capacity; in capacity_of()
6987 if (time_after(jiffies, current->wakee_flip_decay_ts + HZ)) { in record_wakee()
6988 current->wakee_flips >>= 1; in record_wakee()
6989 current->wakee_flip_decay_ts = jiffies; in record_wakee()
6992 if (current->last_wakee != p) { in record_wakee()
6993 current->last_wakee = p; in record_wakee()
6994 current->wakee_flips++; in record_wakee()
6999 * Detect M:N waker/wakee relationships via a switching-frequency heuristic.
7001 * A waker of many should wake a different task than the one last awakened
7009 * non-monogamous, with partner count exceeding socket size.
7017 unsigned int master = current->wakee_flips; in wake_wide()
7018 unsigned int slave = p->wakee_flips; in wake_wide()
7029 * The purpose of wake_affine() is to quickly determine on which CPU we can run
7033 * wake_affine_idle() - only considers 'now', it check if the waking CPU is
7034 * cache-affine and is (or will be) idle.
7036 * wake_affine_weight() - considers the weight to reflect the average
7047 * node depending on the IO topology or IRQ affinity settings. in wake_affine_idle()
7051 * is more important than cache hot data on the prev_cpu and from in wake_affine_idle()
7053 * on one CPU. in wake_affine_idle()
7058 if (sync && cpu_rq(this_cpu)->nr_running == 1) in wake_affine_idle()
7082 this_eff_load -= current_load; in wake_affine_weight()
7093 prev_eff_load -= task_load; in wake_affine_weight()
7095 prev_eff_load *= 100 + (sd->imbalance_pct - 100) / 2; in wake_affine_weight()
7101 * stacking the wakee on top of the waker if no other CPU is in wake_affine_weight()
7121 schedstat_inc(p->stats.nr_wakeups_affine_attempts); in wake_affine()
7125 schedstat_inc(sd->ttwu_move_affine); in wake_affine()
7126 schedstat_inc(p->stats.nr_wakeups_affine); in wake_affine()
7134 * find_idlest_group_cpu - find the idlest CPU among the CPUs in the group.
7143 int shallowest_idle_cpu = -1; in find_idlest_group_cpu()
7147 if (group->group_weight == 1) in find_idlest_group_cpu()
7151 for_each_cpu_and(i, sched_group_span(group), p->cpus_ptr) { in find_idlest_group_cpu()
7162 if (idle && idle->exit_latency < min_exit_latency) { in find_idlest_group_cpu()
7168 min_exit_latency = idle->exit_latency; in find_idlest_group_cpu()
7169 latest_idle_timestamp = rq->idle_stamp; in find_idlest_group_cpu()
7171 } else if ((!idle || idle->exit_latency == min_exit_latency) && in find_idlest_group_cpu()
7172 rq->idle_stamp > latest_idle_timestamp) { in find_idlest_group_cpu()
7178 latest_idle_timestamp = rq->idle_stamp; in find_idlest_group_cpu()
7181 } else if (shallowest_idle_cpu == -1) { in find_idlest_group_cpu()
7190 return shallowest_idle_cpu != -1 ? shallowest_idle_cpu : least_loaded_cpu; in find_idlest_group_cpu()
7198 if (!cpumask_intersects(sched_domain_span(sd), p->cpus_ptr)) in find_idlest_cpu()
7206 sync_entity_load_avg(&p->se); in find_idlest_cpu()
7213 if (!(sd->flags & sd_flag)) { in find_idlest_cpu()
7214 sd = sd->child; in find_idlest_cpu()
7220 sd = sd->child; in find_idlest_cpu()
7227 sd = sd->child; in find_idlest_cpu()
7233 weight = sd->span_weight; in find_idlest_cpu()
7236 if (weight <= tmp->span_weight) in find_idlest_cpu()
7238 if (tmp->flags & sd_flag) in find_idlest_cpu()
7252 return -1; in __select_idle_cpu()
7265 WRITE_ONCE(sds->has_idle_cores, val); in set_idle_cores()
7274 return READ_ONCE(sds->has_idle_cores); in test_idle_cores()
7281 * information in sd_llc_shared->has_idle_cores.
7311 * sd_llc->shared->has_idle_cores and enabled through update_idle_core() above.
7321 if (*idle_cpu == -1) { in select_idle_core()
7330 if (*idle_cpu == -1 && cpumask_test_cpu(cpu, cpus)) in select_idle_core()
7338 return -1; in select_idle_core()
7348 for_each_cpu_and(cpu, cpu_smt_mask(target), p->cpus_ptr) { in select_idle_smt()
7361 return -1; in select_idle_smt()
7382 return -1; in select_idle_smt()
7389 * comparing the average scan cost (tracked in sd->avg_scan_cost) against the
7390 * average idle time for this rq (as found in rq->avg_idle).
7395 int i, cpu, idle_cpu = -1, nr = INT_MAX; in select_idle_cpu()
7402 cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr); in select_idle_cpu()
7410 return -1; in select_idle_cpu()
7417 if (unlikely(this_rq->wake_stamp < now)) { in select_idle_cpu()
7418 while (this_rq->wake_stamp < now && this_rq->wake_avg_idle) { in select_idle_cpu()
7419 this_rq->wake_stamp++; in select_idle_cpu()
7420 this_rq->wake_avg_idle >>= 1; in select_idle_cpu()
7424 avg_idle = this_rq->wake_avg_idle; in select_idle_cpu()
7425 avg_cost = this_sd->avg_scan_cost + 1; in select_idle_cpu()
7427 span_avg = sd->span_weight * avg_idle; in select_idle_cpu()
7439 /* because !--nr is the condition to stop scan */ in select_idle_cpu()
7440 nr = READ_ONCE(sd_share->nr_idle_scan) + 1; in select_idle_cpu()
7443 return -1; in select_idle_cpu()
7454 if (!--nr) in select_idle_cpu()
7455 return -1; in select_idle_cpu()
7466 time = cpu_clock(this) - time; in select_idle_cpu()
7472 this_rq->wake_avg_idle -= min(this_rq->wake_avg_idle, time); in select_idle_cpu()
7474 update_avg(&this_sd->avg_scan_cost, time); in select_idle_cpu()
7481 * Scan the asym_capacity domain for idle CPUs; pick the first idle one on which
7490 int cpu, best_cpu = -1; in select_idle_capacity()
7494 cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr); in select_idle_capacity()
7516 cpu_cap = capacity_orig_of(cpu) - thermal_load_avg(cpu_rq(cpu)); in select_idle_capacity()
7519 * First, select CPU which fits better (-1 being better than 0). in select_idle_capacity()
7559 * On asymmetric system, update task utilization because we will check in select_idle_sibling()
7563 sync_entity_load_avg(&p->se); in select_idle_sibling()
7570 * per-cpu select_rq_mask usage in select_idle_sibling()
7587 * Allow a per-cpu kthread to stack with the wakee if the in select_idle_sibling()
7590 * per-cpu kthread that is now complete and the wakeup is in select_idle_sibling()
7597 this_rq()->nr_running <= 1 && in select_idle_sibling()
7603 recent_used_cpu = p->recent_used_cpu; in select_idle_sibling()
7604 p->recent_used_cpu = prev; in select_idle_sibling()
7609 cpumask_test_cpu(recent_used_cpu, p->cpus_ptr) && in select_idle_sibling()
7621 * On an asymmetric CPU capacity system where an exclusive in select_idle_sibling()
7656 * cpu_util() - Estimates the amount of CPU capacity used by CFS tasks.
7659 * @dst_cpu: CPU @p migrates to, -1 if @p moves from @cpu or @p == NULL
7666 * recent utilization of currently non-runnable tasks on that CPU.
7673 * runnable tasks on that CPU. It preserves a utilization "snapshot" of
7674 * previously-executed tasks, which helps better deduce how busy a CPU will
7675 * be when a long-sleeping task wakes up. The contribution to CPU utilization
7689 * could be seen as over-utilized even though CPU1 has 20% of spare CPU
7692 * after task migrations (scheduler-driven DVFS).
7699 struct cfs_rq *cfs_rq = &cpu_rq(cpu)->cfs; in cpu_util()
7700 unsigned long util = READ_ONCE(cfs_rq->avg.util_avg); in cpu_util()
7704 runnable = READ_ONCE(cfs_rq->avg.runnable_avg); in cpu_util()
7709 * If @dst_cpu is -1 or @p migrates from @cpu to @dst_cpu remove its in cpu_util()
7722 util_est = READ_ONCE(cfs_rq->avg.util_est.enqueued); in cpu_util()
7725 * During wake-up @p isn't enqueued yet and doesn't contribute in cpu_util()
7726 * to any cpu_rq(cpu)->cfs.avg.util_est.enqueued. in cpu_util()
7730 * During exec (@dst_cpu = -1) @p is enqueued and does in cpu_util()
7731 * contribute to cpu_rq(cpu)->cfs.util_est.enqueued. in cpu_util()
7740 * p->on_rq = TASK_ON_RQ_MIGRATING; in cpu_util()
7741 * -------------------------------- A in cpu_util()
7745 * -------------------------------- B in cpu_util()
7763 return cpu_util(cpu, NULL, -1, 0); in cpu_util_cfs()
7768 return cpu_util(cpu, NULL, -1, 1); in cpu_util_cfs_boost()
7777 * enqueued on that CPU as well as tasks which are currently sleeping after an
7778 * execution on that CPU.
7787 if (cpu != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in cpu_util_without()
7790 return cpu_util(cpu, p, -1, 0); in cpu_util_without()
7794 * energy_env - Utilization landscape for energy estimation.
7800 * @pd_cap: Entire perf domain capacity. (pd->nr_cpus * cpu_cap).
7826 eenv->task_busy_time = busy_time; in eenv_task_busy_time()
7830 * Compute the perf_domain (PD) busy time for compute_energy(). Based on the
7840 * - A stable PD utilization, no matter which CPU of that PD we want to place
7841 * the task on.
7843 * - A fair comparison between CPUs as the task contribution (task_util())
7844 * will always be the same no matter which CPU utilization we rely on
7848 * exceed @eenv->pd_cap.
7858 unsigned long util = cpu_util(cpu, p, -1, 0); in eenv_pd_busy_time()
7863 eenv->pd_busy_time = min(eenv->pd_cap, busy_time); in eenv_pd_busy_time()
7868 * is placed on the cpu @dst_cpu.
7870 * Returns the maximum utilization among @eenv->cpus. This utilization can't
7871 * exceed @eenv->cpu_cap.
7896 return min(max_util, eenv->cpu_cap); in eenv_pd_max_util()
7909 unsigned long busy_time = eenv->pd_busy_time; in compute_energy()
7912 busy_time = min(eenv->pd_cap, busy_time + eenv->task_busy_time); in compute_energy()
7914 return em_cpu_energy(pd->em_pd, max_util, busy_time, eenv->cpu_cap); in compute_energy()
7918 * find_energy_efficient_cpu(): Find most energy-efficient target CPU for the
7922 * out which of the CPU candidates is the most energy-efficient.
7935 * small tasks on a CPU in order to let other CPUs go in deeper idle states,
7939 * cluster-packing, and spreading inside a cluster. That should at least be
7946 * NOTE: Forkees are not accepted in the energy-aware wake-up path because
7948 * forecast their impact on energy consumption. Consequently, they will be
7949 * placed by find_idlest_cpu() on the least loaded CPU, which might turn out
7950 * to be energy-inefficient in some use-cases. The alternative would be to
7953 * other use-cases too. So, until someone finds a better way to solve this,
7954 * let's keep things simple by re-using the existing slow path.
7962 struct root_domain *rd = this_rq()->rd; in find_energy_efficient_cpu()
7963 int cpu, best_energy_cpu, target = -1; in find_energy_efficient_cpu()
7964 int prev_fits = -1, best_fits = -1; in find_energy_efficient_cpu()
7972 pd = rcu_dereference(rd->pd); in find_energy_efficient_cpu()
7973 if (!pd || READ_ONCE(rd->overutilized)) in find_energy_efficient_cpu()
7977 * Energy-aware wake-up happens on the lowest sched_domain starting in find_energy_efficient_cpu()
7982 sd = sd->parent; in find_energy_efficient_cpu()
7988 sync_entity_load_avg(&p->se); in find_energy_efficient_cpu()
7994 for (; pd; pd = pd->next) { in find_energy_efficient_cpu()
7997 long prev_spare_cap = -1, max_spare_cap = -1; in find_energy_efficient_cpu()
8000 int max_spare_cap_cpu = -1; in find_energy_efficient_cpu()
8001 int fits, max_fits = -1; in find_energy_efficient_cpu()
8011 cpu_thermal_cap -= arch_scale_thermal_pressure(cpu); in find_energy_efficient_cpu()
8024 if (!cpumask_test_cpu(cpu, p->cpus_ptr)) in find_energy_efficient_cpu()
8042 * operate on non clamped util but must use the in find_energy_efficient_cpu()
8043 * max-aggregated uclamp_{min, max}. in find_energy_efficient_cpu()
8080 base_energy = compute_energy(&eenv, pd, cpus, p, -1); in find_energy_efficient_cpu()
8083 if (prev_spare_cap > -1) { in find_energy_efficient_cpu()
8089 prev_delta -= base_energy; in find_energy_efficient_cpu()
8113 cur_delta -= base_energy; in find_energy_efficient_cpu()
8157 int sync = (wake_flags & WF_SYNC) && !(current->flags & PF_EXITING); in select_task_rq_fair()
8166 * required for stable ->cpus_allowed in select_task_rq_fair()
8168 lockdep_assert_held(&p->pi_lock); in select_task_rq_fair()
8173 cpumask_test_cpu(cpu, p->cpus_ptr)) in select_task_rq_fair()
8183 want_affine = !wake_wide(p) && cpumask_test_cpu(cpu, p->cpus_ptr); in select_task_rq_fair()
8192 if (want_affine && (tmp->flags & SD_WAKE_AFFINE) && in select_task_rq_fair()
8206 if (tmp->flags & sd_flag) in select_task_rq_fair()
8227 * previous CPU. The caller guarantees p->pi_lock or task_rq(p)->lock is held.
8231 struct sched_entity *se = &p->se; in migrate_task_rq_fair()
8240 * leading to an inflation after wake-up on the new rq. in migrate_task_rq_fair()
8250 se->avg.last_update_time = 0; in migrate_task_rq_fair()
8257 remove_entity_load_avg(&p->se); in task_dead_fair()
8263 if (rq->nr_running) in balance_fair()
8273 if (SCHED_WARN_ON(!se->on_rq)) in set_next_buddy()
8277 cfs_rq_of(se)->next = se; in set_next_buddy()
8286 struct task_struct *curr = rq->curr; in check_preempt_wakeup_fair()
8287 struct sched_entity *se = &curr->se, *pse = &p->se; in check_preempt_wakeup_fair()
8299 * next-buddy nomination below. in check_preempt_wakeup_fair()
8311 * wake up path. in check_preempt_wakeup_fair()
8313 * Note: this also catches the edge-case of curr being in a throttled in check_preempt_wakeup_fair()
8332 * Preempt an idle entity in favor of a non-idle entity (and don't preempt in check_preempt_wakeup_fair()
8343 if (unlikely(p->policy != SCHED_NORMAL)) in check_preempt_wakeup_fair()
8367 cfs_rq = &rq->cfs; in pick_task_fair()
8368 if (!cfs_rq->nr_running) in pick_task_fair()
8372 struct sched_entity *curr = cfs_rq->curr; in pick_task_fair()
8376 if (curr->on_rq) in pick_task_fair()
8396 struct cfs_rq *cfs_rq = &rq->cfs; in pick_next_task_fair()
8406 if (!prev || prev->sched_class != &fair_sched_class) in pick_next_task_fair()
8418 struct sched_entity *curr = cfs_rq->curr; in pick_next_task_fair()
8422 * have to consider cfs_rq->curr. If it is still a runnable in pick_next_task_fair()
8427 if (curr->on_rq) in pick_next_task_fair()
8439 cfs_rq = &rq->cfs; in pick_next_task_fair()
8441 if (!cfs_rq->nr_running) in pick_next_task_fair()
8460 struct sched_entity *pse = &prev->se; in pick_next_task_fair()
8463 int se_depth = se->depth; in pick_next_task_fair()
8464 int pse_depth = pse->depth; in pick_next_task_fair()
8501 list_move(&p->se.group_node, &rq->cfs_tasks); in pick_next_task_fair()
8519 * Because newidle_balance() releases (and re-acquires) rq->lock, it is in pick_next_task_fair()
8521 * must re-start the pick_next_entity() loop. in pick_next_task_fair()
8548 struct sched_entity *se = &prev->se; in put_prev_task_fair()
8562 struct task_struct *curr = rq->curr; in yield_task_fair()
8564 struct sched_entity *se = &curr->se; in yield_task_fair()
8569 if (unlikely(rq->nr_running == 1)) in yield_task_fair()
8576 * Update run-time statistics of the 'current'. in yield_task_fair()
8586 se->deadline += calc_delta_fair(se->slice, se); in yield_task_fair()
8591 struct sched_entity *se = &p->se; in yield_to_task_fair()
8594 if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se))) in yield_to_task_fair()
8607 * Fair scheduling class load-balancing methods.
8611 * The purpose of load-balancing is to achieve the same basic fairness the
8612 * per-CPU scheduler provides, namely provide a proportional amount of compute
8617 * Where W_i,n is the n-th weight average for CPU i. The instantaneous weight
8622 * Where w_i,j is the weight of the j-th runnable task on CPU i. This weight
8628 * W'_i,n = (2^n - 1) / 2^n * W_i,n + 1 / 2^n * W_i,0 (3)
8637 * imb_i,j = max{ avg(W/C), W_i/C_i } - min{ avg(W/C), W_j/C_j } (4)
8643 * [XXX expand on:
8644 * - infeasible weights;
8645 * - local vs global optima in the discrete case. ]
8655 * of load-balance at each level inv. proportional to the number of CPUs in
8661 * \Sum { --- * --- * 2^i } = O(n) (5)
8663 * `- size of each group
8664 * | | `- number of CPUs doing load-balance
8665 * | `- freq
8666 * `- sum over all levels
8668 * Coupled with a limit on how many tasks we can migrate every balance pass,
8695 * tree itself instead of relying on other CPUs to bring it work.
8708 * W_i,0 = \Sum_j \Prod_k w_k * ----- (9)
8715 * w_i,j,k is the weight of the j-th runnable task in the k-th cgroup on CPU i.
8720 * [XXX write more on how we solve this.. _after_ merging pjt's patches that
8750 * a task on SMT with busy sibling to another CPU on idle core.
8755 * and the task should be migrated to it instead of running on the
8797 /* The set of CPUs under consideration for load-balancing */
8812 * Is this task likely cache-hot:
8818 lockdep_assert_rq_held(env->src_rq); in task_hot()
8820 if (p->sched_class != &fair_sched_class) in task_hot()
8827 if (env->sd->flags & SD_SHARE_CPUCAPACITY) in task_hot()
8833 if (sched_feat(CACHE_HOT_BUDDY) && env->dst_rq->nr_running && in task_hot()
8834 (&p->se == cfs_rq_of(&p->se)->next)) in task_hot()
8837 if (sysctl_sched_migration_cost == -1) in task_hot()
8844 if (!sched_core_cookie_match(cpu_rq(env->dst_cpu), p)) in task_hot()
8850 delta = rq_clock_task(env->src_rq) - p->se.exec_start; in task_hot()
8859 * Returns -1, if task migration is not affected by locality.
8863 struct numa_group *numa_group = rcu_dereference(p->numa_group); in migrate_degrades_locality()
8868 return -1; in migrate_degrades_locality()
8870 if (!p->numa_faults || !(env->sd->flags & SD_NUMA)) in migrate_degrades_locality()
8871 return -1; in migrate_degrades_locality()
8873 src_nid = cpu_to_node(env->src_cpu); in migrate_degrades_locality()
8874 dst_nid = cpu_to_node(env->dst_cpu); in migrate_degrades_locality()
8877 return -1; in migrate_degrades_locality()
8880 if (src_nid == p->numa_preferred_nid) { in migrate_degrades_locality()
8881 if (env->src_rq->nr_running > env->src_rq->nr_preferred_running) in migrate_degrades_locality()
8884 return -1; in migrate_degrades_locality()
8888 if (dst_nid == p->numa_preferred_nid) in migrate_degrades_locality()
8892 if (env->idle == CPU_IDLE) in migrate_degrades_locality()
8893 return -1; in migrate_degrades_locality()
8911 return -1; in migrate_degrades_locality()
8916 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
8923 lockdep_assert_rq_held(env->src_rq); in can_migrate_task()
8930 * 4) are cache-hot on their current CPU. in can_migrate_task()
8932 if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu)) in can_migrate_task()
8939 if (!cpumask_test_cpu(env->dst_cpu, p->cpus_ptr)) { in can_migrate_task()
8942 schedstat_inc(p->stats.nr_failed_migrations_affine); in can_migrate_task()
8944 env->flags |= LBF_SOME_PINNED; in can_migrate_task()
8949 * meet load balance goals by pulling other tasks on src_cpu. in can_migrate_task()
8952 * - for NEWLY_IDLE in can_migrate_task()
8953 * - if we have already computed one in current iteration in can_migrate_task()
8954 * - if it's an active balance in can_migrate_task()
8956 if (env->idle == CPU_NEWLY_IDLE || in can_migrate_task()
8957 env->flags & (LBF_DST_PINNED | LBF_ACTIVE_LB)) in can_migrate_task()
8960 /* Prevent to re-select dst_cpu via env's CPUs: */ in can_migrate_task()
8961 for_each_cpu_and(cpu, env->dst_grpmask, env->cpus) { in can_migrate_task()
8962 if (cpumask_test_cpu(cpu, p->cpus_ptr)) { in can_migrate_task()
8963 env->flags |= LBF_DST_PINNED; in can_migrate_task()
8964 env->new_dst_cpu = cpu; in can_migrate_task()
8972 /* Record that we found at least one task that could run on dst_cpu */ in can_migrate_task()
8973 env->flags &= ~LBF_ALL_PINNED; in can_migrate_task()
8975 if (task_on_cpu(env->src_rq, p)) { in can_migrate_task()
8976 schedstat_inc(p->stats.nr_failed_migrations_running); in can_migrate_task()
8987 if (env->flags & LBF_ACTIVE_LB) in can_migrate_task()
8991 if (tsk_cache_hot == -1) in can_migrate_task()
8995 env->sd->nr_balance_failed > env->sd->cache_nice_tries) { in can_migrate_task()
8997 schedstat_inc(env->sd->lb_hot_gained[env->idle]); in can_migrate_task()
8998 schedstat_inc(p->stats.nr_forced_migrations); in can_migrate_task()
9003 schedstat_inc(p->stats.nr_failed_migrations_hot); in can_migrate_task()
9008 * detach_task() -- detach the task for the migration specified in env
9012 lockdep_assert_rq_held(env->src_rq); in detach_task()
9014 deactivate_task(env->src_rq, p, DEQUEUE_NOCLOCK); in detach_task()
9015 set_task_cpu(p, env->dst_cpu); in detach_task()
9019 * detach_one_task() -- tries to dequeue exactly one task from env->src_rq, as
9028 lockdep_assert_rq_held(env->src_rq); in detach_one_task()
9031 &env->src_rq->cfs_tasks, se.group_node) { in detach_one_task()
9039 * lb_gained[env->idle] is updated (other is detach_tasks) in detach_one_task()
9043 schedstat_inc(env->sd->lb_gained[env->idle]); in detach_one_task()
9050 * detach_tasks() -- tries to detach up to imbalance load/util/tasks from
9057 struct list_head *tasks = &env->src_rq->cfs_tasks; in detach_tasks()
9062 lockdep_assert_rq_held(env->src_rq); in detach_tasks()
9068 if (env->src_rq->nr_running <= 1) { in detach_tasks()
9069 env->flags &= ~LBF_ALL_PINNED; in detach_tasks()
9073 if (env->imbalance <= 0) in detach_tasks()
9081 if (env->idle != CPU_NOT_IDLE && env->src_rq->nr_running <= 1) in detach_tasks()
9084 env->loop++; in detach_tasks()
9086 if (env->loop > env->loop_max) in detach_tasks()
9090 if (env->loop > env->loop_break) { in detach_tasks()
9091 env->loop_break += SCHED_NR_MIGRATE_BREAK; in detach_tasks()
9092 env->flags |= LBF_NEED_BREAK; in detach_tasks()
9101 switch (env->migration_type) { in detach_tasks()
9106 * value. Make sure that env->imbalance decreases in detach_tasks()
9113 load < 16 && !env->sd->nr_balance_failed) in detach_tasks()
9122 if (shr_bound(load, env->sd->nr_balance_failed) > env->imbalance) in detach_tasks()
9125 env->imbalance -= load; in detach_tasks()
9131 if (util > env->imbalance) in detach_tasks()
9134 env->imbalance -= util; in detach_tasks()
9138 env->imbalance--; in detach_tasks()
9143 if (task_fits_cpu(p, env->src_cpu)) in detach_tasks()
9146 env->imbalance = 0; in detach_tasks()
9151 list_add(&p->se.group_node, &env->tasks); in detach_tasks()
9161 if (env->idle == CPU_NEWLY_IDLE) in detach_tasks()
9169 if (env->imbalance <= 0) in detach_tasks()
9174 list_move(&p->se.group_node, tasks); in detach_tasks()
9182 schedstat_add(env->sd->lb_gained[env->idle], detached); in detach_tasks()
9188 * attach_task() -- attach the task detached by detach_task() to its new rq.
9200 * attach_one_task() -- attaches the task returned from detach_one_task() to
9214 * attach_tasks() -- attaches all tasks detached by detach_tasks() to their
9219 struct list_head *tasks = &env->tasks; in attach_tasks()
9223 rq_lock(env->dst_rq, &rf); in attach_tasks()
9224 update_rq_clock(env->dst_rq); in attach_tasks()
9228 list_del_init(&p->se.group_node); in attach_tasks()
9230 attach_task(env->dst_rq, p); in attach_tasks()
9233 rq_unlock(env->dst_rq, &rf); in attach_tasks()
9239 if (cfs_rq->avg.load_avg) in cfs_rq_has_blocked()
9242 if (cfs_rq->avg.util_avg) in cfs_rq_has_blocked()
9250 if (READ_ONCE(rq->avg_rt.util_avg)) in others_have_blocked()
9253 if (READ_ONCE(rq->avg_dl.util_avg)) in others_have_blocked()
9260 if (READ_ONCE(rq->avg_irq.util_avg)) in others_have_blocked()
9269 WRITE_ONCE(rq->last_blocked_load_update_tick, jiffies); in update_blocked_load_tick()
9275 rq->has_blocked_load = 0; in update_blocked_load_status()
9295 curr_class = rq->curr->sched_class; in __update_blocked_others()
9328 if (cfs_rq->nr_running == 0) in __update_blocked_fair()
9331 if (cfs_rq == &rq->cfs) in __update_blocked_fair()
9336 se = cfs_rq->tg->se[cpu]; in __update_blocked_fair()
9342 * decayed cfs_rqs linger on the list. in __update_blocked_fair()
9357 * This needs to be done in a top-down fashion because the load of a child
9363 struct sched_entity *se = cfs_rq->tg->se[cpu_of(rq)]; in update_cfs_rq_h_load()
9367 if (cfs_rq->last_h_load_update == now) in update_cfs_rq_h_load()
9370 WRITE_ONCE(cfs_rq->h_load_next, NULL); in update_cfs_rq_h_load()
9373 WRITE_ONCE(cfs_rq->h_load_next, se); in update_cfs_rq_h_load()
9374 if (cfs_rq->last_h_load_update == now) in update_cfs_rq_h_load()
9379 cfs_rq->h_load = cfs_rq_load_avg(cfs_rq); in update_cfs_rq_h_load()
9380 cfs_rq->last_h_load_update = now; in update_cfs_rq_h_load()
9383 while ((se = READ_ONCE(cfs_rq->h_load_next)) != NULL) { in update_cfs_rq_h_load()
9384 load = cfs_rq->h_load; in update_cfs_rq_h_load()
9385 load = div64_ul(load * se->avg.load_avg, in update_cfs_rq_h_load()
9388 cfs_rq->h_load = load; in update_cfs_rq_h_load()
9389 cfs_rq->last_h_load_update = now; in update_cfs_rq_h_load()
9398 return div64_ul(p->se.avg.load_avg * cfs_rq->h_load, in task_h_load()
9404 struct cfs_rq *cfs_rq = &rq->cfs; in __update_blocked_fair()
9416 return p->se.avg.load_avg; in task_h_load()
9442 * sg_lb_stats - stats of a sched_group required for load_balancing
9456 unsigned int group_smt_balance; /* Task on busy SMT be moved */
9465 * sd_lb_stats - Structure to store the statistics of a sched_domain
9483 * Skimp on the clearing to avoid duplicate work. We can avoid clearing in init_sd_lb_stats()
9519 used = READ_ONCE(rq->avg_rt.util_avg); in scale_rt_capacity()
9520 used += READ_ONCE(rq->avg_dl.util_avg); in scale_rt_capacity()
9526 free = max - used; in scale_rt_capacity()
9534 struct sched_group *sdg = sd->groups; in update_cpu_capacity()
9536 cpu_rq(cpu)->cpu_capacity_orig = arch_scale_cpu_capacity(cpu); in update_cpu_capacity()
9541 cpu_rq(cpu)->cpu_capacity = capacity; in update_cpu_capacity()
9544 sdg->sgc->capacity = capacity; in update_cpu_capacity()
9545 sdg->sgc->min_capacity = capacity; in update_cpu_capacity()
9546 sdg->sgc->max_capacity = capacity; in update_cpu_capacity()
9551 struct sched_domain *child = sd->child; in update_group_capacity()
9552 struct sched_group *group, *sdg = sd->groups; in update_group_capacity()
9556 interval = msecs_to_jiffies(sd->balance_interval); in update_group_capacity()
9558 sdg->sgc->next_update = jiffies + interval; in update_group_capacity()
9569 if (child->flags & SD_OVERLAP) { in update_group_capacity()
9588 group = child->groups; in update_group_capacity()
9590 struct sched_group_capacity *sgc = group->sgc; in update_group_capacity()
9592 capacity += sgc->capacity; in update_group_capacity()
9593 min_capacity = min(sgc->min_capacity, min_capacity); in update_group_capacity()
9594 max_capacity = max(sgc->max_capacity, max_capacity); in update_group_capacity()
9595 group = group->next; in update_group_capacity()
9596 } while (group != child->groups); in update_group_capacity()
9599 sdg->sgc->capacity = capacity; in update_group_capacity()
9600 sdg->sgc->min_capacity = min_capacity; in update_group_capacity()
9601 sdg->sgc->max_capacity = max_capacity; in update_group_capacity()
9612 return ((rq->cpu_capacity * sd->imbalance_pct) < in check_cpu_capacity()
9613 (rq->cpu_capacity_orig * 100)); in check_cpu_capacity()
9623 return rq->misfit_task_load && in check_misfit_status()
9624 (rq->cpu_capacity_orig < rq->rd->max_cpu_capacity || in check_misfit_status()
9630 * groups is inadequate due to ->cpus_ptr constraints.
9639 * If we were to balance group-wise we'd place two tasks in the first group and
9659 return group->sgc->imbalance; in sg_imbalanced()
9677 if (sgs->sum_nr_running < sgs->group_weight) in group_has_capacity()
9680 if ((sgs->group_capacity * imbalance_pct) < in group_has_capacity()
9681 (sgs->group_runnable * 100)) in group_has_capacity()
9684 if ((sgs->group_capacity * 100) > in group_has_capacity()
9685 (sgs->group_util * imbalance_pct)) in group_has_capacity()
9702 if (sgs->sum_nr_running <= sgs->group_weight) in group_is_overloaded()
9705 if ((sgs->group_capacity * 100) < in group_is_overloaded()
9706 (sgs->group_util * imbalance_pct)) in group_is_overloaded()
9709 if ((sgs->group_capacity * imbalance_pct) < in group_is_overloaded()
9710 (sgs->group_runnable * 100)) in group_is_overloaded()
9727 if (sgs->group_asym_packing) in group_classify()
9730 if (sgs->group_smt_balance) in group_classify()
9733 if (sgs->group_misfit_task_load) in group_classify()
9743 * sched_use_asym_prio - Check whether asym_packing priority must be used
9758 return sd->flags & SD_SHARE_CPUCAPACITY || is_core_idle(cpu); in sched_use_asym_prio()
9762 * sched_asym - Check if the destination CPU can do asym_packing load balance
9764 * @sds: Load-balancing data with statistics of the local group
9765 * @sgs: Load-balancing statistics of the candidate busiest group
9773 * can only do it if @group is an SMT group and has exactly on busy CPU. Larger
9787 if (!sched_use_asym_prio(env->sd, env->dst_cpu)) in sched_asym()
9794 if (group->flags & SD_SHARE_CPUCAPACITY) { in sched_asym()
9795 if (sgs->group_weight - sgs->idle_cpus != 1) in sched_asym()
9799 return sched_asym_prefer(env->dst_cpu, group->asym_prefer_cpu); in sched_asym()
9809 return (sg1->flags & SD_SHARE_CPUCAPACITY) != in smt_vs_nonsmt_groups()
9810 (sg2->flags & SD_SHARE_CPUCAPACITY); in smt_vs_nonsmt_groups()
9816 if (env->idle == CPU_NOT_IDLE) in smt_balance()
9823 * will not be on. in smt_balance()
9825 if (group->flags & SD_SHARE_CPUCAPACITY && in smt_balance()
9826 sgs->sum_h_nr_running > 1) in smt_balance()
9840 if (env->idle == CPU_NOT_IDLE || !busiest->sum_nr_running) in sibling_imbalance()
9843 ncores_busiest = sds->busiest->cores; in sibling_imbalance()
9844 ncores_local = sds->local->cores; in sibling_imbalance()
9847 imbalance = busiest->sum_nr_running; in sibling_imbalance()
9848 lsub_positive(&imbalance, local->sum_nr_running); in sibling_imbalance()
9852 /* Balance such that nr_running/ncores ratio are same on both groups */ in sibling_imbalance()
9853 imbalance = ncores_local * busiest->sum_nr_running; in sibling_imbalance()
9854 lsub_positive(&imbalance, ncores_busiest * local->sum_nr_running); in sibling_imbalance()
9855 /* Normalize imbalance and do rounding on normalization */ in sibling_imbalance()
9860 if (imbalance <= 1 && local->sum_nr_running == 0 && in sibling_imbalance()
9861 busiest->sum_nr_running > 1) in sibling_imbalance()
9874 if (rq->cfs.h_nr_running != 1) in sched_reduced_capacity()
9881 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
9883 * @sds: Load-balancing data with statistics of the local group.
9898 local_group = group == sds->local; in update_sg_lb_stats()
9900 for_each_cpu_and(i, sched_group_span(group), env->cpus) { in update_sg_lb_stats()
9904 sgs->group_load += load; in update_sg_lb_stats()
9905 sgs->group_util += cpu_util_cfs(i); in update_sg_lb_stats()
9906 sgs->group_runnable += cpu_runnable(rq); in update_sg_lb_stats()
9907 sgs->sum_h_nr_running += rq->cfs.h_nr_running; in update_sg_lb_stats()
9909 nr_running = rq->nr_running; in update_sg_lb_stats()
9910 sgs->sum_nr_running += nr_running; in update_sg_lb_stats()
9919 sgs->nr_numa_running += rq->nr_numa_running; in update_sg_lb_stats()
9920 sgs->nr_preferred_running += rq->nr_preferred_running; in update_sg_lb_stats()
9926 sgs->idle_cpus++; in update_sg_lb_stats()
9934 if (env->sd->flags & SD_ASYM_CPUCAPACITY) { in update_sg_lb_stats()
9935 /* Check for a misfit task on the cpu */ in update_sg_lb_stats()
9936 if (sgs->group_misfit_task_load < rq->misfit_task_load) { in update_sg_lb_stats()
9937 sgs->group_misfit_task_load = rq->misfit_task_load; in update_sg_lb_stats()
9940 } else if ((env->idle != CPU_NOT_IDLE) && in update_sg_lb_stats()
9941 sched_reduced_capacity(rq, env->sd)) { in update_sg_lb_stats()
9942 /* Check for a task running on a CPU with reduced capacity */ in update_sg_lb_stats()
9943 if (sgs->group_misfit_task_load < load) in update_sg_lb_stats()
9944 sgs->group_misfit_task_load = load; in update_sg_lb_stats()
9948 sgs->group_capacity = group->sgc->capacity; in update_sg_lb_stats()
9950 sgs->group_weight = group->group_weight; in update_sg_lb_stats()
9953 if (!local_group && env->sd->flags & SD_ASYM_PACKING && in update_sg_lb_stats()
9954 env->idle != CPU_NOT_IDLE && sgs->sum_h_nr_running && in update_sg_lb_stats()
9956 sgs->group_asym_packing = 1; in update_sg_lb_stats()
9961 sgs->group_smt_balance = 1; in update_sg_lb_stats()
9963 sgs->group_type = group_classify(env->sd->imbalance_pct, group, sgs); in update_sg_lb_stats()
9966 if (sgs->group_type == group_overloaded) in update_sg_lb_stats()
9967 sgs->avg_load = (sgs->group_load * SCHED_CAPACITY_SCALE) / in update_sg_lb_stats()
9968 sgs->group_capacity; in update_sg_lb_stats()
9972 * update_sd_pick_busiest - return 1 on busiest group
9989 struct sg_lb_stats *busiest = &sds->busiest_stat; in update_sd_pick_busiest()
9992 if (!sgs->sum_h_nr_running) in update_sd_pick_busiest()
9997 * We can use max_capacity here as reduction in capacity on some in update_sd_pick_busiest()
10001 if ((env->sd->flags & SD_ASYM_CPUCAPACITY) && in update_sd_pick_busiest()
10002 (sgs->group_type == group_misfit_task) && in update_sd_pick_busiest()
10003 (!capacity_greater(capacity_of(env->dst_cpu), sg->sgc->max_capacity) || in update_sd_pick_busiest()
10004 sds->local_stat.group_type != group_has_spare)) in update_sd_pick_busiest()
10007 if (sgs->group_type > busiest->group_type) in update_sd_pick_busiest()
10010 if (sgs->group_type < busiest->group_type) in update_sd_pick_busiest()
10018 switch (sgs->group_type) { in update_sd_pick_busiest()
10021 if (sgs->avg_load <= busiest->avg_load) in update_sd_pick_busiest()
10034 if (sched_asym_prefer(sg->asym_prefer_cpu, sds->busiest->asym_prefer_cpu)) in update_sd_pick_busiest()
10043 if (sgs->group_misfit_task_load < busiest->group_misfit_task_load) in update_sd_pick_busiest()
10049 * Check if we have spare CPUs on either SMT group to in update_sd_pick_busiest()
10052 if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) in update_sd_pick_busiest()
10070 if (sgs->avg_load < busiest->avg_load) in update_sd_pick_busiest()
10073 if (sgs->avg_load == busiest->avg_load) { in update_sd_pick_busiest()
10075 * SMT sched groups need more help than non-SMT groups. in update_sd_pick_busiest()
10078 if (sds->busiest->flags & SD_SHARE_CPUCAPACITY) in update_sd_pick_busiest()
10090 if (smt_vs_nonsmt_groups(sds->busiest, sg)) { in update_sd_pick_busiest()
10091 if (sg->flags & SD_SHARE_CPUCAPACITY && sgs->sum_h_nr_running <= 1) in update_sd_pick_busiest()
10105 if (sgs->idle_cpus > busiest->idle_cpus) in update_sd_pick_busiest()
10107 else if ((sgs->idle_cpus == busiest->idle_cpus) && in update_sd_pick_busiest()
10108 (sgs->sum_nr_running <= busiest->sum_nr_running)) in update_sd_pick_busiest()
10116 * per-CPU capacity. Migrating tasks to less capable CPUs may harm in update_sd_pick_busiest()
10120 if ((env->sd->flags & SD_ASYM_CPUCAPACITY) && in update_sd_pick_busiest()
10121 (sgs->group_type <= group_fully_busy) && in update_sd_pick_busiest()
10122 (capacity_greater(sg->sgc->min_capacity, capacity_of(env->dst_cpu)))) in update_sd_pick_busiest()
10131 if (sgs->sum_h_nr_running > sgs->nr_numa_running) in fbq_classify_group()
10133 if (sgs->sum_h_nr_running > sgs->nr_preferred_running) in fbq_classify_group()
10140 if (rq->nr_running > rq->nr_numa_running) in fbq_classify_rq()
10142 if (rq->nr_running > rq->nr_preferred_running) in fbq_classify_rq()
10162 * task_running_on_cpu - return 1 if @p is running on @cpu.
10168 if (cpu != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in task_running_on_cpu()
10178 * idle_cpu_without - would a given CPU be idle without p ?
10179 * @cpu: the processor on which idleness is tested.
10188 if (rq->curr != rq->idle && rq->curr != p) in idle_cpu_without()
10192 * rq->nr_running can't be used but an updated version without the in idle_cpu_without()
10193 * impact of p on cpu must be used instead. The updated nr_running in idle_cpu_without()
10198 if (rq->ttwu_pending) in idle_cpu_without()
10206 * update_sg_wakeup_stats - Update sched_group's statistics for wakeup.
10222 if (sd->flags & SD_ASYM_CPUCAPACITY) in update_sg_wakeup_stats()
10223 sgs->group_misfit_task_load = 1; in update_sg_wakeup_stats()
10229 sgs->group_load += cpu_load_without(rq, p); in update_sg_wakeup_stats()
10230 sgs->group_util += cpu_util_without(i, p); in update_sg_wakeup_stats()
10231 sgs->group_runnable += cpu_runnable_without(rq, p); in update_sg_wakeup_stats()
10233 sgs->sum_h_nr_running += rq->cfs.h_nr_running - local; in update_sg_wakeup_stats()
10235 nr_running = rq->nr_running - local; in update_sg_wakeup_stats()
10236 sgs->sum_nr_running += nr_running; in update_sg_wakeup_stats()
10242 sgs->idle_cpus++; in update_sg_wakeup_stats()
10245 if (sd->flags & SD_ASYM_CPUCAPACITY && in update_sg_wakeup_stats()
10246 sgs->group_misfit_task_load && in update_sg_wakeup_stats()
10248 sgs->group_misfit_task_load = 0; in update_sg_wakeup_stats()
10252 sgs->group_capacity = group->sgc->capacity; in update_sg_wakeup_stats()
10254 sgs->group_weight = group->group_weight; in update_sg_wakeup_stats()
10256 sgs->group_type = group_classify(sd->imbalance_pct, group, sgs); in update_sg_wakeup_stats()
10262 if (sgs->group_type == group_fully_busy || in update_sg_wakeup_stats()
10263 sgs->group_type == group_overloaded) in update_sg_wakeup_stats()
10264 sgs->avg_load = (sgs->group_load * SCHED_CAPACITY_SCALE) / in update_sg_wakeup_stats()
10265 sgs->group_capacity; in update_sg_wakeup_stats()
10273 if (sgs->group_type < idlest_sgs->group_type) in update_pick_idlest()
10276 if (sgs->group_type > idlest_sgs->group_type) in update_pick_idlest()
10284 switch (sgs->group_type) { in update_pick_idlest()
10288 if (idlest_sgs->avg_load <= sgs->avg_load) in update_pick_idlest()
10300 if (idlest->sgc->max_capacity >= group->sgc->max_capacity) in update_pick_idlest()
10306 if (idlest_sgs->idle_cpus > sgs->idle_cpus) in update_pick_idlest()
10310 if (idlest_sgs->idle_cpus == sgs->idle_cpus && in update_pick_idlest()
10311 idlest_sgs->group_util <= sgs->group_util) in update_pick_idlest()
10324 * Assumes p is allowed on at least one CPU in sd.
10329 struct sched_group *idlest = NULL, *local = NULL, *group = sd->groups; in find_idlest_group()
10343 p->cpus_ptr)) in find_idlest_group()
10367 } while (group = group->next, group != sd->groups); in find_idlest_group()
10396 /* Calculate allowed imbalance based on load */ in find_idlest_group()
10398 (sd->imbalance_pct-100) / 100; in find_idlest_group()
10405 * cross-domain, add imbalance to the load on the remote node in find_idlest_group()
10409 if ((sd->flags & SD_NUMA) && in find_idlest_group()
10420 if (100 * local_sgs.avg_load <= sd->imbalance_pct * idlest_sgs.avg_load) in find_idlest_group()
10432 if (local->sgc->max_capacity >= idlest->sgc->max_capacity) in find_idlest_group()
10438 if (sd->flags & SD_NUMA) { in find_idlest_group()
10439 int imb_numa_nr = sd->imb_numa_nr; in find_idlest_group()
10446 if (cpu_to_node(this_cpu) == p->numa_preferred_nid) in find_idlest_group()
10450 if (cpu_to_node(idlest_cpu) == p->numa_preferred_nid) in find_idlest_group()
10462 if (p->nr_cpus_allowed != NR_CPUS) { in find_idlest_group()
10465 cpumask_and(cpus, sched_group_span(local), p->cpus_ptr); in find_idlest_group()
10466 imb_numa_nr = min(cpumask_weight(cpus), sd->imb_numa_nr); in find_idlest_group()
10469 imbalance = abs(local_sgs.idle_cpus - idlest_sgs.idle_cpus); in find_idlest_group()
10506 if (!sched_feat(SIS_UTIL) || env->idle == CPU_NEWLY_IDLE) in update_idle_cpu_scan()
10509 llc_weight = per_cpu(sd_llc_size, env->dst_cpu); in update_idle_cpu_scan()
10510 if (env->sd->span_weight != llc_weight) in update_idle_cpu_scan()
10513 sd_share = rcu_dereference(per_cpu(sd_llc_shared, env->dst_cpu)); in update_idle_cpu_scan()
10523 * let y = SCHED_CAPACITY_SCALE - p * x^2 [1] in update_idle_cpu_scan()
10541 * y = SCHED_CAPACITY_SCALE - in update_idle_cpu_scan()
10550 pct = env->sd->imbalance_pct; in update_idle_cpu_scan()
10554 y = SCHED_CAPACITY_SCALE - tmp; in update_idle_cpu_scan()
10559 if ((int)y != sd_share->nr_idle_scan) in update_idle_cpu_scan()
10560 WRITE_ONCE(sd_share->nr_idle_scan, (int)y); in update_idle_cpu_scan()
10564 * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
10571 struct sched_group *sg = env->sd->groups; in update_sd_lb_stats()
10572 struct sg_lb_stats *local = &sds->local_stat; in update_sd_lb_stats()
10581 local_group = cpumask_test_cpu(env->dst_cpu, sched_group_span(sg)); in update_sd_lb_stats()
10583 sds->local = sg; in update_sd_lb_stats()
10586 if (env->idle != CPU_NEWLY_IDLE || in update_sd_lb_stats()
10587 time_after_eq(jiffies, sg->sgc->next_update)) in update_sd_lb_stats()
10588 update_group_capacity(env->sd, env->dst_cpu); in update_sd_lb_stats()
10598 sds->busiest = sg; in update_sd_lb_stats()
10599 sds->busiest_stat = *sgs; in update_sd_lb_stats()
10604 sds->total_load += sgs->group_load; in update_sd_lb_stats()
10605 sds->total_capacity += sgs->group_capacity; in update_sd_lb_stats()
10607 sum_util += sgs->group_util; in update_sd_lb_stats()
10608 sg = sg->next; in update_sd_lb_stats()
10609 } while (sg != env->sd->groups); in update_sd_lb_stats()
10616 if (sds->busiest) in update_sd_lb_stats()
10617 sds->prefer_sibling = !!(sds->busiest->flags & SD_PREFER_SIBLING); in update_sd_lb_stats()
10620 if (env->sd->flags & SD_NUMA) in update_sd_lb_stats()
10621 env->fbq_type = fbq_classify_group(&sds->busiest_stat); in update_sd_lb_stats()
10623 if (!env->sd->parent) { in update_sd_lb_stats()
10625 WRITE_ONCE(env->dst_rq->rd->overload, sg_status & SG_OVERLOAD); in update_sd_lb_stats()
10627 /* Update over-utilization (tipping point, U >= 0) indicator */ in update_sd_lb_stats()
10628 set_rd_overutilized_status(env->dst_rq->rd, in update_sd_lb_stats()
10631 set_rd_overutilized_status(env->dst_rq->rd, SG_OVERUTILIZED); in update_sd_lb_stats()
10638 * calculate_imbalance - Calculate the amount of imbalance present within the
10647 local = &sds->local_stat; in calculate_imbalance()
10648 busiest = &sds->busiest_stat; in calculate_imbalance()
10650 if (busiest->group_type == group_misfit_task) { in calculate_imbalance()
10651 if (env->sd->flags & SD_ASYM_CPUCAPACITY) { in calculate_imbalance()
10653 env->migration_type = migrate_misfit; in calculate_imbalance()
10654 env->imbalance = 1; in calculate_imbalance()
10660 env->migration_type = migrate_load; in calculate_imbalance()
10661 env->imbalance = busiest->group_misfit_task_load; in calculate_imbalance()
10666 if (busiest->group_type == group_asym_packing) { in calculate_imbalance()
10671 env->migration_type = migrate_task; in calculate_imbalance()
10672 env->imbalance = busiest->sum_h_nr_running; in calculate_imbalance()
10676 if (busiest->group_type == group_smt_balance) { in calculate_imbalance()
10678 env->migration_type = migrate_task; in calculate_imbalance()
10679 env->imbalance = 1; in calculate_imbalance()
10683 if (busiest->group_type == group_imbalanced) { in calculate_imbalance()
10685 * In the group_imb case we cannot rely on group-wide averages in calculate_imbalance()
10686 * to ensure CPU-load equilibrium, try to move any task to fix in calculate_imbalance()
10690 env->migration_type = migrate_task; in calculate_imbalance()
10691 env->imbalance = 1; in calculate_imbalance()
10699 if (local->group_type == group_has_spare) { in calculate_imbalance()
10700 if ((busiest->group_type > group_fully_busy) && in calculate_imbalance()
10701 !(env->sd->flags & SD_SHARE_PKG_RESOURCES)) { in calculate_imbalance()
10710 env->migration_type = migrate_util; in calculate_imbalance()
10711 env->imbalance = max(local->group_capacity, local->group_util) - in calculate_imbalance()
10712 local->group_util; in calculate_imbalance()
10721 if (env->idle != CPU_NOT_IDLE && env->imbalance == 0) { in calculate_imbalance()
10722 env->migration_type = migrate_task; in calculate_imbalance()
10723 env->imbalance = 1; in calculate_imbalance()
10729 if (busiest->group_weight == 1 || sds->prefer_sibling) { in calculate_imbalance()
10731 * When prefer sibling, evenly spread running tasks on in calculate_imbalance()
10734 env->migration_type = migrate_task; in calculate_imbalance()
10735 env->imbalance = sibling_imbalance(env, sds, busiest, local); in calculate_imbalance()
10742 env->migration_type = migrate_task; in calculate_imbalance()
10743 env->imbalance = max_t(long, 0, in calculate_imbalance()
10744 (local->idle_cpus - busiest->idle_cpus)); in calculate_imbalance()
10749 if (env->sd->flags & SD_NUMA) { in calculate_imbalance()
10750 env->imbalance = adjust_numa_imbalance(env->imbalance, in calculate_imbalance()
10751 local->sum_nr_running + 1, in calculate_imbalance()
10752 env->sd->imb_numa_nr); in calculate_imbalance()
10757 env->imbalance >>= 1; in calculate_imbalance()
10766 if (local->group_type < group_overloaded) { in calculate_imbalance()
10772 local->avg_load = (local->group_load * SCHED_CAPACITY_SCALE) / in calculate_imbalance()
10773 local->group_capacity; in calculate_imbalance()
10779 if (local->avg_load >= busiest->avg_load) { in calculate_imbalance()
10780 env->imbalance = 0; in calculate_imbalance()
10784 sds->avg_load = (sds->total_load * SCHED_CAPACITY_SCALE) / in calculate_imbalance()
10785 sds->total_capacity; in calculate_imbalance()
10791 if (local->avg_load >= sds->avg_load) { in calculate_imbalance()
10792 env->imbalance = 0; in calculate_imbalance()
10806 env->migration_type = migrate_load; in calculate_imbalance()
10807 env->imbalance = min( in calculate_imbalance()
10808 (busiest->avg_load - sds->avg_load) * busiest->group_capacity, in calculate_imbalance()
10809 (sds->avg_load - local->avg_load) * local->group_capacity in calculate_imbalance()
10836 * find_busiest_group - Returns the busiest group within the sched_domain
10843 * Return: - The busiest group if imbalance exists.
10865 if (busiest->group_type == group_misfit_task) in find_busiest_group()
10869 struct root_domain *rd = env->dst_rq->rd; in find_busiest_group()
10871 if (rcu_dereference(rd->pd) && !READ_ONCE(rd->overutilized)) in find_busiest_group()
10876 if (busiest->group_type == group_asym_packing) in find_busiest_group()
10884 if (busiest->group_type == group_imbalanced) in find_busiest_group()
10892 if (local->group_type > busiest->group_type) in find_busiest_group()
10899 if (local->group_type == group_overloaded) { in find_busiest_group()
10904 if (local->avg_load >= busiest->avg_load) in find_busiest_group()
10915 if (local->avg_load >= sds.avg_load) in find_busiest_group()
10922 if (100 * busiest->avg_load <= in find_busiest_group()
10923 env->sd->imbalance_pct * local->avg_load) in find_busiest_group()
10931 if (sds.prefer_sibling && local->group_type == group_has_spare && in find_busiest_group()
10935 if (busiest->group_type != group_overloaded) { in find_busiest_group()
10936 if (env->idle == CPU_NOT_IDLE) { in find_busiest_group()
10945 if (busiest->group_type == group_smt_balance && in find_busiest_group()
10951 if (busiest->group_weight > 1 && in find_busiest_group()
10952 local->idle_cpus <= (busiest->idle_cpus + 1)) { in find_busiest_group()
10959 * on another group. Of course this applies only if in find_busiest_group()
10965 if (busiest->sum_h_nr_running == 1) { in find_busiest_group()
10976 return env->imbalance ? sds.busiest : NULL; in find_busiest_group()
10979 env->imbalance = 0; in find_busiest_group()
10984 * find_busiest_queue - find the busiest runqueue among the CPUs in the group.
10994 for_each_cpu_and(i, sched_group_span(group), env->cpus) { in find_busiest_queue()
11004 * - regular: there are !numa tasks in find_busiest_queue()
11005 * - remote: there are numa tasks that run on the 'wrong' node in find_busiest_queue()
11006 * - all: there is no distinction in find_busiest_queue()
11021 if (rt > env->fbq_type) in find_busiest_queue()
11024 nr_running = rq->cfs.h_nr_running; in find_busiest_queue()
11032 * eventually lead to active_balancing high->low capacity. in find_busiest_queue()
11033 * Higher per-CPU capacity is considered better than balancing in find_busiest_queue()
11036 if (env->sd->flags & SD_ASYM_CPUCAPACITY && in find_busiest_queue()
11037 !capacity_greater(capacity_of(env->dst_cpu), capacity) && in find_busiest_queue()
11048 if ((env->sd->flags & SD_ASYM_PACKING) && in find_busiest_queue()
11049 sched_use_asym_prio(env->sd, i) && in find_busiest_queue()
11050 sched_asym_prefer(i, env->dst_cpu) && in find_busiest_queue()
11054 switch (env->migration_type) { in find_busiest_queue()
11062 if (nr_running == 1 && load > env->imbalance && in find_busiest_queue()
11063 !check_cpu_capacity(rq, env->sd)) in find_busiest_queue()
11115 if (rq->misfit_task_load > busiest_load) { in find_busiest_queue()
11116 busiest_load = rq->misfit_task_load; in find_busiest_queue()
11147 return env->idle != CPU_NOT_IDLE && (env->sd->flags & SD_ASYM_PACKING) && in asym_active_balance()
11148 sched_use_asym_prio(env->sd, env->dst_cpu) && in asym_active_balance()
11149 (sched_asym_prefer(env->dst_cpu, env->src_cpu) || in asym_active_balance()
11150 !sched_use_asym_prio(env->sd, env->src_cpu)); in asym_active_balance()
11156 struct sched_domain *sd = env->sd; in imbalanced_active_balance()
11160 * distribution of the load on the system but also the even distribution of the in imbalanced_active_balance()
11161 * threads on a system with spare capacity in imbalanced_active_balance()
11163 if ((env->migration_type == migrate_task) && in imbalanced_active_balance()
11164 (sd->nr_balance_failed > sd->cache_nice_tries+2)) in imbalanced_active_balance()
11172 struct sched_domain *sd = env->sd; in need_active_balance()
11184 * available on dst_cpu. in need_active_balance()
11186 if ((env->idle != CPU_NOT_IDLE) && in need_active_balance()
11187 (env->src_rq->cfs.h_nr_running == 1)) { in need_active_balance()
11188 if ((check_cpu_capacity(env->src_rq, sd)) && in need_active_balance()
11189 (capacity_of(env->src_cpu)*sd->imbalance_pct < capacity_of(env->dst_cpu)*100)) in need_active_balance()
11193 if (env->migration_type == migrate_misfit) in need_active_balance()
11204 struct sched_group *sg = env->sd->groups; in should_we_balance()
11205 int cpu, idle_smt = -1; in should_we_balance()
11211 if (!cpumask_test_cpu(env->dst_cpu, env->cpus)) in should_we_balance()
11221 if (env->idle == CPU_NEWLY_IDLE) { in should_we_balance()
11222 if (env->dst_rq->nr_running > 0 || env->dst_rq->ttwu_pending) in should_we_balance()
11229 for_each_cpu_and(cpu, swb_cpus, env->cpus) { in should_we_balance()
11236 * later consideration. Find CPU on an idle core first. in should_we_balance()
11238 if (!(env->sd->flags & SD_SHARE_CPUCAPACITY) && !is_core_idle(cpu)) { in should_we_balance()
11239 if (idle_smt == -1) in should_we_balance()
11253 * Are we the first idle core in a non-SMT domain or higher, in should_we_balance()
11256 return cpu == env->dst_cpu; in should_we_balance()
11260 if (idle_smt != -1) in should_we_balance()
11261 return idle_smt == env->dst_cpu; in should_we_balance()
11264 return group_balance_cpu(sg) == env->dst_cpu; in should_we_balance()
11276 struct sched_domain *sd_parent = sd->parent; in load_balance()
11285 .dst_grpmask = group_balance_mask(sd->groups), in load_balance()
11295 schedstat_inc(sd->lb_count[idle]); in load_balance()
11305 schedstat_inc(sd->lb_nobusyg[idle]); in load_balance()
11311 schedstat_inc(sd->lb_nobusyq[idle]); in load_balance()
11317 schedstat_add(sd->lb_imbalance[idle], env.imbalance); in load_balance()
11319 env.src_cpu = busiest->cpu; in load_balance()
11325 if (busiest->nr_running > 1) { in load_balance()
11328 * an imbalance but busiest->nr_running <= 1, the group is in load_balance()
11332 env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running); in load_balance()
11339 * cur_ld_moved - load moved in current iteration in load_balance()
11340 * ld_moved - cumulative load moved across iterations in load_balance()
11347 * unlock busiest->lock, and we are able to be sure in load_balance()
11367 * Revisit (affine) tasks on src_cpu that couldn't be moved to in load_balance()
11369 * where they can run. The upper limit on how many times we in load_balance()
11370 * iterate on same src_cpu is dependent on number of CPUs in our in load_balance()
11373 * This changes load balance semantics a bit on who can move in load_balance()
11375 * (or a ilb_cpu acting on its behalf where given_cpu is in load_balance()
11376 * nohz-idle), we now have balance_cpu in a position to move in load_balance()
11387 /* Prevent to re-select dst_cpu via env's CPUs */ in load_balance()
11407 int *group_imbalance = &sd_parent->groups->sgc->imbalance; in load_balance()
11413 /* All tasks on this runqueue were pinned by CPU affinity */ in load_balance()
11434 schedstat_inc(sd->lb_failed[idle]); in load_balance()
11436 * Increment the failure counter only on periodic balance. in load_balance()
11442 sd->nr_balance_failed++; in load_balance()
11451 * if the curr task on busiest CPU can't be in load_balance()
11454 if (!cpumask_test_cpu(this_cpu, busiest->curr->cpus_ptr)) { in load_balance()
11459 /* Record that we found at least one task that could run on this_cpu */ in load_balance()
11463 * ->active_balance synchronizes accesses to in load_balance()
11464 * ->active_balance_work. Once set, it's cleared in load_balance()
11467 if (!busiest->active_balance) { in load_balance()
11468 busiest->active_balance = 1; in load_balance()
11469 busiest->push_cpu = this_cpu; in load_balance()
11478 &busiest->active_balance_work); in load_balance()
11483 sd->nr_balance_failed = 0; in load_balance()
11488 sd->balance_interval = sd->min_interval; in load_balance()
11500 int *group_imbalance = &sd_parent->groups->sgc->imbalance; in load_balance()
11512 schedstat_inc(sd->lb_balanced[idle]); in load_balance()
11514 sd->nr_balance_failed = 0; in load_balance()
11530 sd->balance_interval < MAX_PINNED_INTERVAL) || in load_balance()
11531 sd->balance_interval < sd->max_interval) in load_balance()
11532 sd->balance_interval *= 2; in load_balance()
11540 unsigned long interval = sd->balance_interval; in get_sd_balance_interval()
11543 interval *= sd->busy_factor; in get_sd_balance_interval()
11554 interval -= 1; in get_sd_balance_interval()
11568 next = sd->last_balance + interval; in update_next_balance()
11577 * least 1 task to be running on each physical CPU where possible, and
11584 int target_cpu = busiest_rq->push_cpu; in active_load_balance_cpu_stop()
11592 * Between queueing the stop-work and running it is a hole in which in active_load_balance_cpu_stop()
11601 !busiest_rq->active_balance)) in active_load_balance_cpu_stop()
11605 if (busiest_rq->nr_running <= 1) in active_load_balance_cpu_stop()
11611 * Bjorn Helgaas on a 128-CPU setup. in active_load_balance_cpu_stop()
11627 .src_cpu = busiest_rq->cpu, in active_load_balance_cpu_stop()
11633 schedstat_inc(sd->alb_count); in active_load_balance_cpu_stop()
11638 schedstat_inc(sd->alb_pushed); in active_load_balance_cpu_stop()
11640 sd->nr_balance_failed = 0; in active_load_balance_cpu_stop()
11642 schedstat_inc(sd->alb_failed); in active_load_balance_cpu_stop()
11647 busiest_rq->active_balance = 0; in active_load_balance_cpu_stop()
11662 * This trades load-balance latency on larger machines for less cross talk.
11671 if (cost > sd->max_newidle_lb_cost) { in update_newidle_cost()
11674 * next wakeup on the CPU. in update_newidle_cost()
11676 sd->max_newidle_lb_cost = cost; in update_newidle_cost()
11677 sd->last_decay_max_lb_cost = jiffies; in update_newidle_cost()
11678 } else if (time_after(jiffies, sd->last_decay_max_lb_cost + HZ)) { in update_newidle_cost()
11684 sd->max_newidle_lb_cost = (sd->max_newidle_lb_cost * 253) / 256; in update_newidle_cost()
11685 sd->last_decay_max_lb_cost = jiffies; in update_newidle_cost()
11702 int cpu = rq->cpu; in rebalance_domains()
11719 max_cost += sd->max_newidle_lb_cost; in rebalance_domains()
11734 need_serialize = sd->flags & SD_SERIALIZE; in rebalance_domains()
11740 if (time_after_eq(jiffies, sd->last_balance + interval)) { in rebalance_domains()
11744 * env->dst_cpu, so we can't know our idle in rebalance_domains()
11750 sd->last_balance = jiffies; in rebalance_domains()
11756 if (time_after(next_balance, sd->last_balance + interval)) { in rebalance_domains()
11757 next_balance = sd->last_balance + interval; in rebalance_domains()
11763 * Ensure the rq-wide value also decays but keep it at a in rebalance_domains()
11764 * reasonable floor to avoid funnies with rq->avg_idle. in rebalance_domains()
11766 rq->max_idle_balance_cost = in rebalance_domains()
11777 rq->next_balance = next_balance; in rebalance_domains()
11783 return unlikely(!rcu_dereference_sched(rq->sd)); in on_null_domain()
11789 * - When one of the busy CPUs notice that there may be an idle rebalancing
11792 * - HK_TYPE_MISC CPUs are used for this task, because HK_TYPE_SCHED not set
11844 * This way we generate an IPI on the target CPU which in kick_ilb()
11848 smp_call_function_single_async(ilb_cpu, &cpu_rq(ilb_cpu)->nohz_csd); in kick_ilb()
11860 int nr_busy, i, cpu = rq->cpu; in nohz_balancer_kick()
11863 if (unlikely(rq->idle_balance)) in nohz_balancer_kick()
11886 if (rq->nr_running >= 2) { in nohz_balancer_kick()
11893 sd = rcu_dereference(rq->sd); in nohz_balancer_kick()
11898 * on. in nohz_balancer_kick()
11900 if (rq->cfs.h_nr_running >= 1 && check_cpu_capacity(rq, sd)) { in nohz_balancer_kick()
11929 * to run the misfit task on. in nohz_balancer_kick()
11950 * increase the overall cache use), we need some less-loaded LLC in nohz_balancer_kick()
11954 * the others are - so just get a nohz balance going if it looks in nohz_balancer_kick()
11957 nr_busy = atomic_read(&sds->nr_busy_cpus); in nohz_balancer_kick()
11980 if (!sd || !sd->nohz_idle) in set_cpu_sd_state_busy()
11982 sd->nohz_idle = 0; in set_cpu_sd_state_busy()
11984 atomic_inc(&sd->shared->nr_busy_cpus); in set_cpu_sd_state_busy()
11993 if (likely(!rq->nohz_tick_stopped)) in nohz_balance_exit_idle()
11996 rq->nohz_tick_stopped = 0; in nohz_balance_exit_idle()
11997 cpumask_clear_cpu(rq->cpu, nohz.idle_cpus_mask); in nohz_balance_exit_idle()
12000 set_cpu_sd_state_busy(rq->cpu); in nohz_balance_exit_idle()
12010 if (!sd || sd->nohz_idle) in set_cpu_sd_state_idle()
12012 sd->nohz_idle = 1; in set_cpu_sd_state_idle()
12014 atomic_dec(&sd->shared->nr_busy_cpus); in set_cpu_sd_state_idle()
12033 /* Spare idle load balancing on CPUs that don't want to be disturbed: */ in nohz_balance_enter_idle()
12038 * Can be set safely without rq->lock held in nohz_balance_enter_idle()
12040 * rq->lock is held during the check and the clear in nohz_balance_enter_idle()
12042 rq->has_blocked_load = 1; in nohz_balance_enter_idle()
12050 if (rq->nohz_tick_stopped) in nohz_balance_enter_idle()
12057 rq->nohz_tick_stopped = 1; in nohz_balance_enter_idle()
12082 unsigned int cpu = rq->cpu; in update_nohz_stats()
12084 if (!rq->has_blocked_load) in update_nohz_stats()
12090 if (!time_after(jiffies, READ_ONCE(rq->last_blocked_load_update_tick))) in update_nohz_stats()
12095 return rq->has_blocked_load; in update_nohz_stats()
12110 int this_cpu = this_rq->cpu; in _nohz_idle_balance()
12167 if (time_after_eq(jiffies, rq->next_balance)) { in _nohz_idle_balance()
12178 if (time_after(next_balance, rq->next_balance)) { in _nohz_idle_balance()
12179 next_balance = rq->next_balance; in _nohz_idle_balance()
12208 unsigned int flags = this_rq->nohz_idle_balance; in nohz_idle_balance()
12213 this_rq->nohz_idle_balance = 0; in nohz_idle_balance()
12243 int this_cpu = this_rq->cpu; in nohz_newidle_balance()
12252 /* Will wake up very soon. No time for doing anything else*/ in nohz_newidle_balance()
12253 if (this_rq->avg_idle < sysctl_sched_migration_cost) in nohz_newidle_balance()
12284 * < 0 - we released the lock and there are !fair tasks present
12285 * 0 - failed, no new tasks
12286 * > 0 - success, new (fair) tasks present
12291 int this_cpu = this_rq->cpu; in newidle_balance()
12302 if (this_rq->ttwu_pending) in newidle_balance()
12309 this_rq->idle_stamp = rq_clock(this_rq); in newidle_balance()
12319 * for load-balance and preemption/IRQs are still disabled avoiding in newidle_balance()
12320 * further scheduler activity on it and we're being very careful to in newidle_balance()
12321 * re-start the picking loop. in newidle_balance()
12326 sd = rcu_dereference_check_sched_domain(this_rq->sd); in newidle_balance()
12328 if (!READ_ONCE(this_rq->rd->overload) || in newidle_balance()
12329 (sd && this_rq->avg_idle < sd->max_newidle_lb_cost)) { in newidle_balance()
12351 if (this_rq->avg_idle < curr_cost + sd->max_newidle_lb_cost) in newidle_balance()
12354 if (sd->flags & SD_BALANCE_NEWIDLE) { in newidle_balance()
12361 domain_cost = t1 - t0; in newidle_balance()
12370 * now runnable tasks on this rq. in newidle_balance()
12372 if (pulled_task || this_rq->nr_running > 0 || in newidle_balance()
12373 this_rq->ttwu_pending) in newidle_balance()
12380 if (curr_cost > this_rq->max_idle_balance_cost) in newidle_balance()
12381 this_rq->max_idle_balance_cost = curr_cost; in newidle_balance()
12388 if (this_rq->cfs.h_nr_running && !pulled_task) in newidle_balance()
12392 if (this_rq->nr_running != this_rq->cfs.h_nr_running) in newidle_balance()
12393 pulled_task = -1; in newidle_balance()
12397 if (time_after(this_rq->next_balance, next_balance)) in newidle_balance()
12398 this_rq->next_balance = next_balance; in newidle_balance()
12401 this_rq->idle_stamp = 0; in newidle_balance()
12417 enum cpu_idle_type idle = this_rq->idle_balance ? in run_rebalance_domains()
12422 * balancing on behalf of the other idle CPUs whose ticks are in run_rebalance_domains()
12432 update_blocked_averages(this_rq->cpu); in run_rebalance_domains()
12448 if (time_after_eq(jiffies, rq->next_balance)) in trigger_load_balance()
12475 u64 rtime = se->sum_exec_runtime - se->prev_sum_exec_runtime; in __entity_slice_used()
12476 u64 slice = se->slice; in __entity_slice_used()
12495 * tasks on this CPU and the forced idle CPU. Ideally, we should in task_tick_core()
12498 * MIN_NR_TASKS_DURING_FORCEIDLE - 1 tasks and use that to check in task_tick_core()
12501 if (rq->core->core_forceidle_count && rq->cfs.nr_running == 1 && in task_tick_core()
12502 __entity_slice_used(&curr->se, MIN_NR_TASKS_DURING_FORCEIDLE)) in task_tick_core()
12507 * se_fi_update - Update the cfs_rq->min_vruntime_fi in a CFS hierarchy if needed.
12516 if (cfs_rq->forceidle_seq == fi_seq) in se_fi_update()
12518 cfs_rq->forceidle_seq = fi_seq; in se_fi_update()
12521 cfs_rq->min_vruntime_fi = cfs_rq->min_vruntime; in se_fi_update()
12527 struct sched_entity *se = &p->se; in task_vruntime_update()
12529 if (p->sched_class != &fair_sched_class) in task_vruntime_update()
12532 se_fi_update(se, rq->core->core_forceidle_seq, in_fi); in task_vruntime_update()
12539 const struct sched_entity *sea = &a->se; in cfs_prio_less()
12540 const struct sched_entity *seb = &b->se; in cfs_prio_less()
12545 SCHED_WARN_ON(task_rq(b)->core != rq->core); in cfs_prio_less()
12552 while (sea->cfs_rq->tg != seb->cfs_rq->tg) { in cfs_prio_less()
12553 int sea_depth = sea->depth; in cfs_prio_less()
12554 int seb_depth = seb->depth; in cfs_prio_less()
12562 se_fi_update(sea, rq->core->core_forceidle_seq, in_fi); in cfs_prio_less()
12563 se_fi_update(seb, rq->core->core_forceidle_seq, in_fi); in cfs_prio_less()
12565 cfs_rqa = sea->cfs_rq; in cfs_prio_less()
12566 cfs_rqb = seb->cfs_rq; in cfs_prio_less()
12568 cfs_rqa = &task_rq(a)->cfs; in cfs_prio_less()
12569 cfs_rqb = &task_rq(b)->cfs; in cfs_prio_less()
12577 delta = (s64)(sea->vruntime - seb->vruntime) + in cfs_prio_less()
12578 (s64)(cfs_rqb->min_vruntime_fi - cfs_rqa->min_vruntime_fi); in cfs_prio_less()
12588 cfs_rq = task_group(p)->cfs_rq[cpu]; in task_is_throttled_fair()
12590 cfs_rq = &cpu_rq(cpu)->cfs; in task_is_throttled_fair()
12609 struct sched_entity *se = &curr->se; in task_tick_fair()
12626 * called on fork with the child task as argument from the parent's context
12627 * - child not yet on the tasklist
12628 * - preemption disabled
12632 struct sched_entity *se = &p->se, *curr; in task_fork_fair()
12641 curr = cfs_rq->curr; in task_fork_fair()
12658 if (rq->cfs.nr_running == 1) in prio_changed_fair()
12662 * Reschedule if we are currently running on this runqueue and in prio_changed_fair()
12663 * our priority decreased, or if we are not currently running on in prio_changed_fair()
12667 if (p->prio > oldprio) in prio_changed_fair()
12689 se = se->parent; in propagate_entity_cfs_rq()
12714 * - A forked task which hasn't been woken up by wake_up_new_task(). in detach_entity_cfs_rq()
12715 * - A task which has been woken up by try_to_wake_up() but is in detach_entity_cfs_rq()
12718 if (!se->avg.last_update_time) in detach_entity_cfs_rq()
12742 struct sched_entity *se = &p->se; in detach_task_cfs_rq()
12749 struct sched_entity *se = &p->se; in attach_task_cfs_rq()
12778 * This routine is mostly called to set cfs_rq->curr field when a task
12783 struct sched_entity *se = &p->se; in set_next_task_fair()
12791 list_move(&se->group_node, &rq->cfs_tasks); in set_next_task_fair()
12799 /* ensure bandwidth has been allocated on our new cfs_rq */ in set_next_task_fair()
12806 cfs_rq->tasks_timeline = RB_ROOT_CACHED; in init_cfs_rq()
12807 u64_u32_store(cfs_rq->min_vruntime, (u64)(-(1LL << 20))); in init_cfs_rq()
12809 raw_spin_lock_init(&cfs_rq->removed.lock); in init_cfs_rq()
12820 if (READ_ONCE(p->__state) == TASK_NEW) in task_change_group_fair()
12826 /* Tell se's cfs_rq has been changed -- migrated */ in task_change_group_fair()
12827 p->se.avg.last_update_time = 0; in task_change_group_fair()
12838 if (tg->cfs_rq) in free_fair_sched_group()
12839 kfree(tg->cfs_rq[i]); in free_fair_sched_group()
12840 if (tg->se) in free_fair_sched_group()
12841 kfree(tg->se[i]); in free_fair_sched_group()
12844 kfree(tg->cfs_rq); in free_fair_sched_group()
12845 kfree(tg->se); in free_fair_sched_group()
12854 tg->cfs_rq = kcalloc(nr_cpu_ids, sizeof(cfs_rq), GFP_KERNEL); in alloc_fair_sched_group()
12855 if (!tg->cfs_rq) in alloc_fair_sched_group()
12857 tg->se = kcalloc(nr_cpu_ids, sizeof(se), GFP_KERNEL); in alloc_fair_sched_group()
12858 if (!tg->se) in alloc_fair_sched_group()
12861 tg->shares = NICE_0_LOAD; in alloc_fair_sched_group()
12877 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]); in alloc_fair_sched_group()
12898 se = tg->se[i]; in online_fair_sched_group()
12916 if (tg->se[cpu]) in unregister_fair_sched_group()
12917 remove_entity_load_avg(tg->se[cpu]); in unregister_fair_sched_group()
12921 * check on_list without danger of it being re-added. in unregister_fair_sched_group()
12923 if (!tg->cfs_rq[cpu]->on_list) in unregister_fair_sched_group()
12929 list_del_leaf_cfs_rq(tg->cfs_rq[cpu]); in unregister_fair_sched_group()
12940 cfs_rq->tg = tg; in init_tg_cfs_entry()
12941 cfs_rq->rq = rq; in init_tg_cfs_entry()
12944 tg->cfs_rq[cpu] = cfs_rq; in init_tg_cfs_entry()
12945 tg->se[cpu] = se; in init_tg_cfs_entry()
12952 se->cfs_rq = &rq->cfs; in init_tg_cfs_entry()
12953 se->depth = 0; in init_tg_cfs_entry()
12955 se->cfs_rq = parent->my_q; in init_tg_cfs_entry()
12956 se->depth = parent->depth + 1; in init_tg_cfs_entry()
12959 se->my_q = cfs_rq; in init_tg_cfs_entry()
12961 update_load_set(&se->load, NICE_0_LOAD); in init_tg_cfs_entry()
12962 se->parent = parent; in init_tg_cfs_entry()
12976 if (!tg->se[0]) in __sched_group_set_shares()
12977 return -EINVAL; in __sched_group_set_shares()
12981 if (tg->shares == shares) in __sched_group_set_shares()
12984 tg->shares = shares; in __sched_group_set_shares()
12987 struct sched_entity *se = tg->se[i]; in __sched_group_set_shares()
13009 ret = -EINVAL; in sched_group_set_shares()
13022 return -EINVAL; in sched_group_set_idle()
13025 return -EINVAL; in sched_group_set_idle()
13029 if (tg->idle == idle) { in sched_group_set_idle()
13034 tg->idle = idle; in sched_group_set_idle()
13038 struct sched_entity *se = tg->se[i]; in sched_group_set_idle()
13039 struct cfs_rq *parent_cfs_rq, *grp_cfs_rq = tg->cfs_rq[i]; in sched_group_set_idle()
13046 grp_cfs_rq->idle = idle; in sched_group_set_idle()
13050 if (se->on_rq) { in sched_group_set_idle()
13053 parent_cfs_rq->idle_nr_running++; in sched_group_set_idle()
13055 parent_cfs_rq->idle_nr_running--; in sched_group_set_idle()
13058 idle_task_delta = grp_cfs_rq->h_nr_running - in sched_group_set_idle()
13059 grp_cfs_rq->idle_h_nr_running; in sched_group_set_idle()
13061 idle_task_delta *= -1; in sched_group_set_idle()
13066 if (!se->on_rq) in sched_group_set_idle()
13069 cfs_rq->idle_h_nr_running += idle_task_delta; in sched_group_set_idle()
13108 struct sched_entity *se = &task->se; in get_rr_interval_fair()
13112 * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise in get_rr_interval_fair()
13115 if (rq->cfs.load.weight) in get_rr_interval_fair()
13116 rr_interval = NS_TO_JIFFIES(se->slice); in get_rr_interval_fair()
13193 ng = rcu_dereference(p->numa_group); in show_numa_stats()
13195 if (p->numa_faults) { in show_numa_stats()
13196 tsf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 0)]; in show_numa_stats()
13197 tpf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 1)]; in show_numa_stats()
13200 gsf = ng->faults[task_faults_idx(NUMA_MEM, node, 0)], in show_numa_stats()
13201 gpf = ng->faults[task_faults_idx(NUMA_MEM, node, 1)]; in show_numa_stats()
13222 INIT_CSD(&cpu_rq(i)->cfsb_csd, __cfsb_csd_unthrottle, cpu_rq(i)); in init_sched_fair_class()
13223 INIT_LIST_HEAD(&cpu_rq(i)->cfsb_csd_list); in init_sched_fair_class()