Lines Matching +full:lock +full:- +full:latency +full:- +full:ns
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()
206 * because with more CPUs the 'effective latency' as visible
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()
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()
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()
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()
865 * In order to provide latency guarantees for different request sizes
873 * We can do this in O(log n) time due to an augmented RB-tree. The
877 * se->min_deadline = min(se->deadline, se->{left,right}->min_deadline)
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()
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()
1412 /* The page with hint page fault latency < threshold in ms is considered hot */
1418 spinlock_t lock; /* nr_tasks, tasks */ member
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()
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()
1702 faults *= (max_dist - dist); in score_nearby_nodes()
1703 faults /= (max_dist - LOCAL_DISTANCE); in score_nearby_nodes()
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()
1801 * hint page fault handler, the hint page fault latency is calculated
1804 * hint page fault latency = hint page fault time - scan time
1806 * The smaller the hint page fault latency, the higher the possibility
1816 return (time - last_time) & PAGE_ACCESS_TIME_MASK; in numa_hint_fault_latency()
1821 * hurt application latency. So we provide a mechanism to rate limit
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()
1886 unsigned int latency, th, def_th; in should_numa_migrate_memory() local
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()
1901 latency = numa_hint_fault_latency(page); in should_numa_migrate_memory()
1902 if (latency >= th) in should_numa_migrate_memory()
1909 this_cpupid = cpu_pid_to_cpupid(dst_cpu, current->pid); 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()
1951 /* A shared fault, but p->numa_group has not been set up yet. */ in should_numa_migrate_memory()
1968 * --------------- * - > --------------- in should_numa_migrate_memory()
2028 struct numa_stats *ns) in numa_classify() argument
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()
2075 struct numa_stats *ns, int nid, in update_numa_stats() argument
2078 int cpu, idle_core = -1; in update_numa_stats()
2080 memset(ns, 0, sizeof(*ns)); 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()
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()
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()
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()
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()
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()
2597 if (task_node(p) == p->numa_preferred_nid) in numa_migrate_preferred()
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()
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()
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()
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()
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()
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()
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()
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()
3491 mm->numa_scan_offset = start; 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()
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()
3676 * Explicitly do a load-store to ensure the intermediate value never hits
3696 * Explicitly do a load-store to ensure the intermediate value never hits
3704 res = var - val; \
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()
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()
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,
3951 * yielding bad latency etc..
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()
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()
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()
4112 * bottom-up, we only have to test whether the cfs_rq before us on the list
4123 if (cfs_rq->on_list) { in child_cfs_rq_on_list()
4124 prev = cfs_rq->leaf_cfs_rq_list.prev; in child_cfs_rq_on_list()
4126 prev = rq->tmp_alone_branch; in child_cfs_rq_on_list()
4129 if (prev == &rq->leaf_cfs_rq_list) in child_cfs_rq_on_list()
4134 return (prev_cfs_rq->tg->parent == cfs_rq->tg); in child_cfs_rq_on_list()
4139 if (cfs_rq->load.weight) in cfs_rq_is_decayed()
4142 if (!load_avg_is_decayed(&cfs_rq->avg)) in cfs_rq_is_decayed()
4152 * update_tg_load_avg - update the tg's load avg
4155 * This function 'ensures': tg->load_avg := \Sum tg->cfs_rq[]->avg.load.
4156 * However, because tg->load_avg is a global value there are performance
4167 long delta = cfs_rq->avg.load_avg - cfs_rq->tg_load_avg_contrib; in update_tg_load_avg()
4172 if (cfs_rq->tg == &root_task_group) in update_tg_load_avg()
4175 if (abs(delta) > cfs_rq->tg_load_avg_contrib / 64) { in update_tg_load_avg()
4176 atomic_long_add(delta, &cfs_rq->tg->load_avg); in update_tg_load_avg()
4177 cfs_rq->tg_load_avg_contrib = cfs_rq->avg.load_avg; in update_tg_load_avg()
4183 * caller only guarantees p->pi_lock is held; no other assumptions,
4184 * including the state of rq->lock, should be made.
4198 * getting what current time is, so simply throw away the out-of-date in set_task_rq_fair()
4202 if (!(se->avg.last_update_time && prev)) in set_task_rq_fair()
4209 se->avg.last_update_time = n_last_update_time; in set_task_rq_fair()
4217 * ge->avg == grq->avg (1)
4228 * ge->avg.load_avg = ge->load.weight * ge->avg.runnable_avg (2)
4233 * grq->avg.load_avg = grq->load.weight * grq->avg.runnable_avg (3)
4237 * ge->avg.runnable_avg == grq->avg.runnable_avg
4241 * ge->load.weight * grq->avg.load_avg
4242 * ge->avg.load_avg = ----------------------------------- (4)
4243 * grq->load.weight
4256 * Another reason this doesn't work is that runnable isn't a 0-sum entity.
4267 * ge->avg.running_sum <= ge->avg.runnable_sum <= LOAD_AVG_MAX
4274 * grq->avg.runnable_sum = grq->avg.load_sum / grq->load.weight
4282 long delta_sum, delta_avg = gcfs_rq->avg.util_avg - se->avg.util_avg; in update_tg_cfs_util()
4290 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in update_tg_cfs_util()
4293 divider = get_pelt_divider(&cfs_rq->avg); in update_tg_cfs_util()
4297 se->avg.util_avg = gcfs_rq->avg.util_avg; in update_tg_cfs_util()
4298 new_sum = se->avg.util_avg * divider; in update_tg_cfs_util()
4299 delta_sum = (long)new_sum - (long)se->avg.util_sum; in update_tg_cfs_util()
4300 se->avg.util_sum = new_sum; in update_tg_cfs_util()
4303 add_positive(&cfs_rq->avg.util_avg, delta_avg); in update_tg_cfs_util()
4304 add_positive(&cfs_rq->avg.util_sum, delta_sum); in update_tg_cfs_util()
4307 cfs_rq->avg.util_sum = max_t(u32, cfs_rq->avg.util_sum, in update_tg_cfs_util()
4308 cfs_rq->avg.util_avg * PELT_MIN_DIVIDER); in update_tg_cfs_util()
4314 long delta_sum, delta_avg = gcfs_rq->avg.runnable_avg - se->avg.runnable_avg; in update_tg_cfs_runnable()
4322 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in update_tg_cfs_runnable()
4325 divider = get_pelt_divider(&cfs_rq->avg); in update_tg_cfs_runnable()
4328 se->avg.runnable_avg = gcfs_rq->avg.runnable_avg; in update_tg_cfs_runnable()
4329 new_sum = se->avg.runnable_avg * divider; in update_tg_cfs_runnable()
4330 delta_sum = (long)new_sum - (long)se->avg.runnable_sum; in update_tg_cfs_runnable()
4331 se->avg.runnable_sum = new_sum; in update_tg_cfs_runnable()
4334 add_positive(&cfs_rq->avg.runnable_avg, delta_avg); in update_tg_cfs_runnable()
4335 add_positive(&cfs_rq->avg.runnable_sum, delta_sum); in update_tg_cfs_runnable()
4337 cfs_rq->avg.runnable_sum = max_t(u32, cfs_rq->avg.runnable_sum, in update_tg_cfs_runnable()
4338 cfs_rq->avg.runnable_avg * PELT_MIN_DIVIDER); in update_tg_cfs_runnable()
4344 long delta_avg, running_sum, runnable_sum = gcfs_rq->prop_runnable_sum; in update_tg_cfs_load()
4353 gcfs_rq->prop_runnable_sum = 0; in update_tg_cfs_load()
4356 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in update_tg_cfs_load()
4359 divider = get_pelt_divider(&cfs_rq->avg); in update_tg_cfs_load()
4366 runnable_sum += se->avg.load_sum; in update_tg_cfs_load()
4373 if (scale_load_down(gcfs_rq->load.weight)) { in update_tg_cfs_load()
4374 load_sum = div_u64(gcfs_rq->avg.load_sum, in update_tg_cfs_load()
4375 scale_load_down(gcfs_rq->load.weight)); in update_tg_cfs_load()
4379 runnable_sum = min(se->avg.load_sum, load_sum); in update_tg_cfs_load()
4388 running_sum = se->avg.util_sum >> SCHED_CAPACITY_SHIFT; in update_tg_cfs_load()
4394 delta_avg = load_avg - se->avg.load_avg; in update_tg_cfs_load()
4398 delta_sum = load_sum - (s64)se_weight(se) * se->avg.load_sum; in update_tg_cfs_load()
4400 se->avg.load_sum = runnable_sum; in update_tg_cfs_load()
4401 se->avg.load_avg = load_avg; in update_tg_cfs_load()
4402 add_positive(&cfs_rq->avg.load_avg, delta_avg); in update_tg_cfs_load()
4403 add_positive(&cfs_rq->avg.load_sum, delta_sum); in update_tg_cfs_load()
4405 cfs_rq->avg.load_sum = max_t(u32, cfs_rq->avg.load_sum, in update_tg_cfs_load()
4406 cfs_rq->avg.load_avg * PELT_MIN_DIVIDER); in update_tg_cfs_load()
4411 cfs_rq->propagate = 1; in add_tg_cfs_propagate()
4412 cfs_rq->prop_runnable_sum += runnable_sum; in add_tg_cfs_propagate()
4424 if (!gcfs_rq->propagate) in propagate_entity_load_avg()
4427 gcfs_rq->propagate = 0; in propagate_entity_load_avg()
4431 add_tg_cfs_propagate(cfs_rq, gcfs_rq->prop_runnable_sum); in propagate_entity_load_avg()
4455 if (se->avg.load_avg || se->avg.util_avg) in skip_blocked_update()
4462 if (gcfs_rq->propagate) in skip_blocked_update()
4494 if (load_avg_is_decayed(&se->avg)) in migrate_se_pelt_lag()
4501 is_idle = is_idle_task(rcu_dereference(rq->curr)); in migrate_se_pelt_lag()
4518 * - cfs->throttled_clock_pelt_time@cfs_rq_idle in migrate_se_pelt_lag()
4521 * = rq_clock_pelt()@rq_idle - rq_clock_pelt()@cfs_rq_idle in migrate_se_pelt_lag()
4524 * = sched_clock_cpu() - rq_clock()@rq_idle in migrate_se_pelt_lag()
4528 * now = rq_clock_pelt()@rq_idle - cfs->throttled_clock_pelt_time + in migrate_se_pelt_lag()
4529 * sched_clock_cpu() - rq_clock()@rq_idle in migrate_se_pelt_lag()
4531 * rq_clock_pelt()@rq_idle is rq->clock_pelt_idle in migrate_se_pelt_lag()
4532 * rq_clock()@rq_idle is rq->clock_idle in migrate_se_pelt_lag()
4533 * cfs->throttled_clock_pelt_time@cfs_rq_idle in migrate_se_pelt_lag()
4534 * is cfs_rq->throttled_pelt_idle in migrate_se_pelt_lag()
4538 throttled = u64_u32_load(cfs_rq->throttled_pelt_idle); in migrate_se_pelt_lag()
4543 now = u64_u32_load(rq->clock_pelt_idle); in migrate_se_pelt_lag()
4553 now -= throttled; in migrate_se_pelt_lag()
4556 * cfs_rq->avg.last_update_time is more recent than our in migrate_se_pelt_lag()
4561 now += sched_clock_cpu(cpu_of(rq)) - u64_u32_load(rq->clock_idle); in migrate_se_pelt_lag()
4570 * update_cfs_rq_load_avg - update the cfs_rq's load/util averages
4577 * cfs_rq->avg is used for task_h_load() and update_cfs_share() for example.
4581 * Since both these conditions indicate a changed cfs_rq->avg.load we should
4588 struct sched_avg *sa = &cfs_rq->avg; in update_cfs_rq_load_avg()
4591 if (cfs_rq->removed.nr) { in update_cfs_rq_load_avg()
4593 u32 divider = get_pelt_divider(&cfs_rq->avg); in update_cfs_rq_load_avg()
4595 raw_spin_lock(&cfs_rq->removed.lock); in update_cfs_rq_load_avg()
4596 swap(cfs_rq->removed.util_avg, removed_util); in update_cfs_rq_load_avg()
4597 swap(cfs_rq->removed.load_avg, removed_load); in update_cfs_rq_load_avg()
4598 swap(cfs_rq->removed.runnable_avg, removed_runnable); in update_cfs_rq_load_avg()
4599 cfs_rq->removed.nr = 0; in update_cfs_rq_load_avg()
4600 raw_spin_unlock(&cfs_rq->removed.lock); in update_cfs_rq_load_avg()
4603 sub_positive(&sa->load_avg, r); in update_cfs_rq_load_avg()
4604 sub_positive(&sa->load_sum, r * divider); in update_cfs_rq_load_avg()
4605 /* See sa->util_sum below */ in update_cfs_rq_load_avg()
4606 sa->load_sum = max_t(u32, sa->load_sum, sa->load_avg * PELT_MIN_DIVIDER); in update_cfs_rq_load_avg()
4609 sub_positive(&sa->util_avg, r); in update_cfs_rq_load_avg()
4610 sub_positive(&sa->util_sum, r * divider); in update_cfs_rq_load_avg()
4612 * Because of rounding, se->util_sum might ends up being +1 more than in update_cfs_rq_load_avg()
4613 * cfs->util_sum. Although this is not a problem by itself, detaching in update_cfs_rq_load_avg()
4615 * util_avg (~1ms) can make cfs->util_sum becoming null whereas in update_cfs_rq_load_avg()
4622 sa->util_sum = max_t(u32, sa->util_sum, sa->util_avg * PELT_MIN_DIVIDER); in update_cfs_rq_load_avg()
4625 sub_positive(&sa->runnable_avg, r); in update_cfs_rq_load_avg()
4626 sub_positive(&sa->runnable_sum, r * divider); in update_cfs_rq_load_avg()
4627 /* See sa->util_sum above */ in update_cfs_rq_load_avg()
4628 sa->runnable_sum = max_t(u32, sa->runnable_sum, in update_cfs_rq_load_avg()
4629 sa->runnable_avg * PELT_MIN_DIVIDER); in update_cfs_rq_load_avg()
4636 -(long)(removed_runnable * divider) >> SCHED_CAPACITY_SHIFT); in update_cfs_rq_load_avg()
4642 u64_u32_store_copy(sa->last_update_time, in update_cfs_rq_load_avg()
4643 cfs_rq->last_update_time_copy, in update_cfs_rq_load_avg()
4644 sa->last_update_time); in update_cfs_rq_load_avg()
4649 * attach_entity_load_avg - attach this entity to its cfs_rq load avg
4654 * cfs_rq->avg.last_update_time being current.
4659 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in attach_entity_load_avg()
4662 u32 divider = get_pelt_divider(&cfs_rq->avg); in attach_entity_load_avg()
4671 se->avg.last_update_time = cfs_rq->avg.last_update_time; in attach_entity_load_avg()
4672 se->avg.period_contrib = cfs_rq->avg.period_contrib; in attach_entity_load_avg()
4680 se->avg.util_sum = se->avg.util_avg * divider; in attach_entity_load_avg()
4682 se->avg.runnable_sum = se->avg.runnable_avg * divider; in attach_entity_load_avg()
4684 se->avg.load_sum = se->avg.load_avg * divider; in attach_entity_load_avg()
4685 if (se_weight(se) < se->avg.load_sum) in attach_entity_load_avg()
4686 se->avg.load_sum = div_u64(se->avg.load_sum, se_weight(se)); in attach_entity_load_avg()
4688 se->avg.load_sum = 1; in attach_entity_load_avg()
4691 cfs_rq->avg.util_avg += se->avg.util_avg; in attach_entity_load_avg()
4692 cfs_rq->avg.util_sum += se->avg.util_sum; in attach_entity_load_avg()
4693 cfs_rq->avg.runnable_avg += se->avg.runnable_avg; in attach_entity_load_avg()
4694 cfs_rq->avg.runnable_sum += se->avg.runnable_sum; in attach_entity_load_avg()
4696 add_tg_cfs_propagate(cfs_rq, se->avg.load_sum); in attach_entity_load_avg()
4704 * detach_entity_load_avg - detach this entity from its cfs_rq load avg
4709 * cfs_rq->avg.last_update_time being current.
4714 sub_positive(&cfs_rq->avg.util_avg, se->avg.util_avg); in detach_entity_load_avg()
4715 sub_positive(&cfs_rq->avg.util_sum, se->avg.util_sum); in detach_entity_load_avg()
4717 cfs_rq->avg.util_sum = max_t(u32, cfs_rq->avg.util_sum, in detach_entity_load_avg()
4718 cfs_rq->avg.util_avg * PELT_MIN_DIVIDER); in detach_entity_load_avg()
4720 sub_positive(&cfs_rq->avg.runnable_avg, se->avg.runnable_avg); in detach_entity_load_avg()
4721 sub_positive(&cfs_rq->avg.runnable_sum, se->avg.runnable_sum); in detach_entity_load_avg()
4723 cfs_rq->avg.runnable_sum = max_t(u32, cfs_rq->avg.runnable_sum, in detach_entity_load_avg()
4724 cfs_rq->avg.runnable_avg * PELT_MIN_DIVIDER); in detach_entity_load_avg()
4726 add_tg_cfs_propagate(cfs_rq, -se->avg.load_sum); in detach_entity_load_avg()
4751 if (se->avg.last_update_time && !(flags & SKIP_AGE_LOAD)) in update_load_avg()
4757 if (!se->avg.last_update_time && (flags & DO_ATTACH)) { in update_load_avg()
4807 * tasks cannot exit without having gone through wake_up_new_task() -> in remove_entity_load_avg()
4814 raw_spin_lock_irqsave(&cfs_rq->removed.lock, flags); in remove_entity_load_avg()
4815 ++cfs_rq->removed.nr; in remove_entity_load_avg()
4816 cfs_rq->removed.util_avg += se->avg.util_avg; in remove_entity_load_avg()
4817 cfs_rq->removed.load_avg += se->avg.load_avg; in remove_entity_load_avg()
4818 cfs_rq->removed.runnable_avg += se->avg.runnable_avg; in remove_entity_load_avg()
4819 raw_spin_unlock_irqrestore(&cfs_rq->removed.lock, flags); in remove_entity_load_avg()
4824 return cfs_rq->avg.runnable_avg; in cfs_rq_runnable_avg()
4829 return cfs_rq->avg.load_avg; in cfs_rq_load_avg()
4836 return READ_ONCE(p->se.avg.util_avg); in task_util()
4841 struct util_est ue = READ_ONCE(p->se.avg.util_est); in _task_util_est()
4860 enqueued = cfs_rq->avg.util_est.enqueued; in util_est_enqueue()
4862 WRITE_ONCE(cfs_rq->avg.util_est.enqueued, enqueued); in util_est_enqueue()
4876 enqueued = cfs_rq->avg.util_est.enqueued; in util_est_dequeue()
4877 enqueued -= min_t(unsigned int, enqueued, _task_util_est(p)); in util_est_dequeue()
4878 WRITE_ONCE(cfs_rq->avg.util_est.enqueued, enqueued); in util_est_dequeue()
4889 * abs(x) < y := (unsigned)(x + y - 1) < (2 * y - 1)
4895 return ((unsigned int)(value + margin - 1) < (2 * margin - 1)); in within_margin()
4919 ue = p->se.avg.util_est; in util_est_update()
4941 last_ewma_diff = ue.enqueued - ue.ewma; in util_est_update()
4942 last_enqueued_diff -= ue.enqueued; in util_est_update()
4965 * ewma(t) = w * task_util(p) + (1-w) * ewma(t-1) in util_est_update()
4966 * = w * task_util(p) + ewma(t-1) - w * ewma(t-1) in util_est_update()
4967 * = w * (task_util(p) - ewma(t-1)) + ewma(t-1) in util_est_update()
4968 * = w * ( last_ewma_diff ) + ewma(t-1) in util_est_update()
4969 * = w * (last_ewma_diff + ewma(t-1) / w) in util_est_update()
4979 WRITE_ONCE(p->se.avg.util_est, ue); in util_est_update()
4981 trace_sched_util_est_se_tp(&p->se); in util_est_update()
5022 capacity_orig_thermal = capacity_orig - arch_scale_thermal_pressure(cpu); in util_fits_cpu()
5038 * +---------------------------------------- in util_fits_cpu()
5076 * +---------------------------------------- in util_fits_cpu()
5098 return -1; in util_fits_cpu()
5120 if (!p || p->nr_cpus_allowed == 1) { in update_misfit_status()
5121 rq->misfit_task_load = 0; in update_misfit_status()
5126 rq->misfit_task_load = 0; in update_misfit_status()
5134 rq->misfit_task_load = max_t(unsigned long, task_h_load(p), 1); in update_misfit_status()
5141 return !cfs_rq->nr_running; in cfs_rq_is_decayed()
5185 se->slice = sysctl_sched_base_slice; in place_entity()
5186 vslice = calc_delta_fair(se->slice, se); in place_entity()
5196 if (sched_feat(PLACE_LAG) && cfs_rq->nr_running) { in place_entity()
5197 struct sched_entity *curr = cfs_rq->curr; in place_entity()
5200 lag = se->vlag; in place_entity()
5210 * lag_i = S - s_i = w_i * (V - v_i) in place_entity()
5215 * vl_i = V - v_i <=> v_i = V - vl_i in place_entity()
5227 * = (W*V + w_i*(V - vl_i)) / (W + w_i) in place_entity()
5228 * = (W*V + w_i*V - w_i*vl_i) / (W + w_i) in place_entity()
5229 * = (V*(W + w_i) - w_i*l) / (W + w_i) in place_entity()
5230 * = V - w_i*vl_i / (W + w_i) in place_entity()
5234 * vl'_i = V' - v_i in place_entity()
5235 * = V - w_i*vl_i / (W + w_i) - (V - vl_i) in place_entity()
5236 * = vl_i - w_i*vl_i / (W + w_i) in place_entity()
5246 * vl'_i = vl_i - w_i*vl_i / (W + w_i) in place_entity()
5247 * = ((W + w_i)*vl_i - w_i*vl_i) / (W + w_i) in place_entity()
5249 * (W + w_i)*vl'_i = (W + w_i)*vl_i - w_i*vl_i in place_entity()
5254 load = cfs_rq->avg_load; in place_entity()
5255 if (curr && curr->on_rq) in place_entity()
5256 load += scale_load_down(curr->load.weight); in place_entity()
5258 lag *= load + scale_load_down(se->load.weight); in place_entity()
5264 se->vruntime = vruntime - lag; in place_entity()
5277 se->deadline = se->vruntime + vslice; in place_entity()
5288 bool curr = cfs_rq->curr == se; in enqueue_entity()
5301 * - Update loads to have both entity and cfs_rq synced with now. in enqueue_entity()
5302 * - For group_entity, update its runnable_weight to reflect the new in enqueue_entity()
5304 * - For group_entity, update its weight to reflect the new share of in enqueue_entity()
5306 * - Add its new weight to cfs_rq->load.weight in enqueue_entity()
5312 * but update_cfs_group() here will re-adjust the weight and have to in enqueue_entity()
5318 * XXX now that the entity has been re-weighted, and it's lag adjusted, in enqueue_entity()
5328 se->exec_start = 0; in enqueue_entity()
5334 se->on_rq = 1; in enqueue_entity()
5336 if (cfs_rq->nr_running == 1) { in enqueue_entity()
5344 if (cfs_rq_throttled(cfs_rq) && !cfs_rq->throttled_clock) in enqueue_entity()
5345 cfs_rq->throttled_clock = rq_clock(rq); in enqueue_entity()
5346 if (!cfs_rq->throttled_clock_self) in enqueue_entity()
5347 cfs_rq->throttled_clock_self = rq_clock(rq); in enqueue_entity()
5357 if (cfs_rq->next != se) in __clear_buddies_next()
5360 cfs_rq->next = NULL; in __clear_buddies_next()
5366 if (cfs_rq->next == se) in clear_buddies()
5381 * Update run-time statistics of the 'current'. in dequeue_entity()
5387 * - Update loads to have both entity and cfs_rq synced with now. in dequeue_entity()
5388 * - For group_entity, update its runnable_weight to reflect the new in dequeue_entity()
5390 * - Subtract its previous weight from cfs_rq->load.weight. in dequeue_entity()
5391 * - For group entity, update its weight to reflect the new share in dequeue_entity()
5402 if (se != cfs_rq->curr) in dequeue_entity()
5404 se->on_rq = 0; in dequeue_entity()
5416 * further than we started -- ie. we'll be penalized. in dequeue_entity()
5421 if (cfs_rq->nr_running == 0) in dequeue_entity()
5431 if (se->on_rq) { in set_next_entity()
5444 se->vlag = se->deadline; in set_next_entity()
5448 cfs_rq->curr = se; in set_next_entity()
5453 * when there are only lesser-weight tasks around): in set_next_entity()
5456 rq_of(cfs_rq)->cfs.load.weight >= 2*se->load.weight) { in set_next_entity()
5460 __schedstat_set(stats->slice_max, in set_next_entity()
5461 max((u64)stats->slice_max, in set_next_entity()
5462 se->sum_exec_runtime - se->prev_sum_exec_runtime)); in set_next_entity()
5465 se->prev_sum_exec_runtime = se->sum_exec_runtime; in set_next_entity()
5479 * Enabling NEXT_BUDDY will affect latency but not fairness. in pick_next_entity()
5482 cfs_rq->next && entity_eligible(cfs_rq, cfs_rq->next)) in pick_next_entity()
5483 return cfs_rq->next; in pick_next_entity()
5496 if (prev->on_rq) in put_prev_entity()
5502 if (prev->on_rq) { in put_prev_entity()
5509 cfs_rq->curr = NULL; in put_prev_entity()
5516 * Update run-time statistics of the 'current'. in entity_tick()
5539 hrtimer_active(&rq_of(cfs_rq)->hrtick_timer)) in entity_tick()
5594 * directly instead of rq->clock to avoid adding additional synchronization
5595 * around rq->lock.
5597 * requires cfs_b->lock
5603 if (unlikely(cfs_b->quota == RUNTIME_INF)) in __refill_cfs_bandwidth_runtime()
5606 cfs_b->runtime += cfs_b->quota; in __refill_cfs_bandwidth_runtime()
5607 runtime = cfs_b->runtime_snap - cfs_b->runtime; in __refill_cfs_bandwidth_runtime()
5609 cfs_b->burst_time += runtime; in __refill_cfs_bandwidth_runtime()
5610 cfs_b->nr_burst++; in __refill_cfs_bandwidth_runtime()
5613 cfs_b->runtime = min(cfs_b->runtime, cfs_b->quota + cfs_b->burst); in __refill_cfs_bandwidth_runtime()
5614 cfs_b->runtime_snap = cfs_b->runtime; in __refill_cfs_bandwidth_runtime()
5619 return &tg->cfs_bandwidth; in tg_cfs_bandwidth()
5628 lockdep_assert_held(&cfs_b->lock); in __assign_cfs_rq_runtime()
5631 min_amount = target_runtime - cfs_rq->runtime_remaining; in __assign_cfs_rq_runtime()
5633 if (cfs_b->quota == RUNTIME_INF) in __assign_cfs_rq_runtime()
5638 if (cfs_b->runtime > 0) { in __assign_cfs_rq_runtime()
5639 amount = min(cfs_b->runtime, min_amount); in __assign_cfs_rq_runtime()
5640 cfs_b->runtime -= amount; in __assign_cfs_rq_runtime()
5641 cfs_b->idle = 0; in __assign_cfs_rq_runtime()
5645 cfs_rq->runtime_remaining += amount; in __assign_cfs_rq_runtime()
5647 return cfs_rq->runtime_remaining > 0; in __assign_cfs_rq_runtime()
5653 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in assign_cfs_rq_runtime()
5656 raw_spin_lock(&cfs_b->lock); in assign_cfs_rq_runtime()
5658 raw_spin_unlock(&cfs_b->lock); in assign_cfs_rq_runtime()
5666 cfs_rq->runtime_remaining -= delta_exec; in __account_cfs_rq_runtime()
5668 if (likely(cfs_rq->runtime_remaining > 0)) in __account_cfs_rq_runtime()
5671 if (cfs_rq->throttled) in __account_cfs_rq_runtime()
5677 if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr)) in __account_cfs_rq_runtime()
5684 if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled) in account_cfs_rq_runtime()
5692 return cfs_bandwidth_used() && cfs_rq->throttled; in cfs_rq_throttled()
5698 return cfs_bandwidth_used() && cfs_rq->throttle_count; in throttled_hierarchy()
5704 * load-balance operations.
5711 src_cfs_rq = tg->cfs_rq[src_cpu]; in throttled_lb_pair()
5712 dest_cfs_rq = tg->cfs_rq[dest_cpu]; in throttled_lb_pair()
5721 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in tg_unthrottle_up()
5723 cfs_rq->throttle_count--; in tg_unthrottle_up()
5724 if (!cfs_rq->throttle_count) { in tg_unthrottle_up()
5725 cfs_rq->throttled_clock_pelt_time += rq_clock_pelt(rq) - in tg_unthrottle_up()
5726 cfs_rq->throttled_clock_pelt; in tg_unthrottle_up()
5732 if (cfs_rq->throttled_clock_self) { in tg_unthrottle_up()
5733 u64 delta = rq_clock(rq) - cfs_rq->throttled_clock_self; in tg_unthrottle_up()
5735 cfs_rq->throttled_clock_self = 0; in tg_unthrottle_up()
5740 cfs_rq->throttled_clock_self_time += delta; in tg_unthrottle_up()
5750 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in tg_throttle_down()
5753 if (!cfs_rq->throttle_count) { in tg_throttle_down()
5754 cfs_rq->throttled_clock_pelt = rq_clock_pelt(rq); in tg_throttle_down()
5757 SCHED_WARN_ON(cfs_rq->throttled_clock_self); in tg_throttle_down()
5758 if (cfs_rq->nr_running) in tg_throttle_down()
5759 cfs_rq->throttled_clock_self = rq_clock(rq); in tg_throttle_down()
5761 cfs_rq->throttle_count++; in tg_throttle_down()
5769 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in throttle_cfs_rq()
5773 raw_spin_lock(&cfs_b->lock); in throttle_cfs_rq()
5782 * for 1ns of runtime rather than just check cfs_b. in throttle_cfs_rq()
5786 list_add_tail_rcu(&cfs_rq->throttled_list, in throttle_cfs_rq()
5787 &cfs_b->throttled_cfs_rq); in throttle_cfs_rq()
5789 raw_spin_unlock(&cfs_b->lock); in throttle_cfs_rq()
5794 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))]; in throttle_cfs_rq()
5798 walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq); in throttle_cfs_rq()
5801 task_delta = cfs_rq->h_nr_running; in throttle_cfs_rq()
5802 idle_task_delta = cfs_rq->idle_h_nr_running; in throttle_cfs_rq()
5805 /* throttled entity or throttle-on-deactivate */ in throttle_cfs_rq()
5806 if (!se->on_rq) in throttle_cfs_rq()
5812 idle_task_delta = cfs_rq->h_nr_running; in throttle_cfs_rq()
5814 qcfs_rq->h_nr_running -= task_delta; in throttle_cfs_rq()
5815 qcfs_rq->idle_h_nr_running -= idle_task_delta; in throttle_cfs_rq()
5817 if (qcfs_rq->load.weight) { in throttle_cfs_rq()
5818 /* Avoid re-evaluating load for this entity: */ in throttle_cfs_rq()
5826 /* throttled entity or throttle-on-deactivate */ in throttle_cfs_rq()
5827 if (!se->on_rq) in throttle_cfs_rq()
5834 idle_task_delta = cfs_rq->h_nr_running; in throttle_cfs_rq()
5836 qcfs_rq->h_nr_running -= task_delta; in throttle_cfs_rq()
5837 qcfs_rq->idle_h_nr_running -= idle_task_delta; in throttle_cfs_rq()
5846 * throttled-list. rq->lock protects completion. in throttle_cfs_rq()
5848 cfs_rq->throttled = 1; in throttle_cfs_rq()
5849 SCHED_WARN_ON(cfs_rq->throttled_clock); in throttle_cfs_rq()
5850 if (cfs_rq->nr_running) in throttle_cfs_rq()
5851 cfs_rq->throttled_clock = rq_clock(rq); in throttle_cfs_rq()
5858 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in unthrottle_cfs_rq()
5862 se = cfs_rq->tg->se[cpu_of(rq)]; in unthrottle_cfs_rq()
5864 cfs_rq->throttled = 0; in unthrottle_cfs_rq()
5868 raw_spin_lock(&cfs_b->lock); in unthrottle_cfs_rq()
5869 if (cfs_rq->throttled_clock) { in unthrottle_cfs_rq()
5870 cfs_b->throttled_time += rq_clock(rq) - cfs_rq->throttled_clock; in unthrottle_cfs_rq()
5871 cfs_rq->throttled_clock = 0; in unthrottle_cfs_rq()
5873 list_del_rcu(&cfs_rq->throttled_list); in unthrottle_cfs_rq()
5874 raw_spin_unlock(&cfs_b->lock); in unthrottle_cfs_rq()
5877 walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq); in unthrottle_cfs_rq()
5879 if (!cfs_rq->load.weight) { in unthrottle_cfs_rq()
5880 if (!cfs_rq->on_list) in unthrottle_cfs_rq()
5893 task_delta = cfs_rq->h_nr_running; in unthrottle_cfs_rq()
5894 idle_task_delta = cfs_rq->idle_h_nr_running; in unthrottle_cfs_rq()
5898 if (se->on_rq) in unthrottle_cfs_rq()
5903 idle_task_delta = cfs_rq->h_nr_running; in unthrottle_cfs_rq()
5905 qcfs_rq->h_nr_running += task_delta; in unthrottle_cfs_rq()
5906 qcfs_rq->idle_h_nr_running += idle_task_delta; in unthrottle_cfs_rq()
5920 idle_task_delta = cfs_rq->h_nr_running; in unthrottle_cfs_rq()
5922 qcfs_rq->h_nr_running += task_delta; in unthrottle_cfs_rq()
5923 qcfs_rq->idle_h_nr_running += idle_task_delta; in unthrottle_cfs_rq()
5937 if (rq->curr == rq->idle && rq->cfs.nr_running) in unthrottle_cfs_rq()
5959 * Since we hold rq lock we're safe from concurrent manipulation of in __cfsb_csd_unthrottle()
5967 list_for_each_entry_safe(cursor, tmp, &rq->cfsb_csd_list, in __cfsb_csd_unthrottle()
5969 list_del_init(&cursor->throttled_csd_list); in __cfsb_csd_unthrottle()
5992 if (SCHED_WARN_ON(!list_empty(&cfs_rq->throttled_csd_list))) in __unthrottle_cfs_rq_async()
5995 first = list_empty(&rq->cfsb_csd_list); in __unthrottle_cfs_rq_async()
5996 list_add_tail(&cfs_rq->throttled_csd_list, &rq->cfsb_csd_list); in __unthrottle_cfs_rq_async()
5998 smp_call_function_single_async(cpu_of(rq), &rq->cfsb_csd); in __unthrottle_cfs_rq_async()
6012 cfs_rq->runtime_remaining <= 0)) in unthrottle_cfs_rq_async()
6029 list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq, in distribute_cfs_runtime()
6044 if (!list_empty(&cfs_rq->throttled_csd_list)) in distribute_cfs_runtime()
6049 SCHED_WARN_ON(cfs_rq->runtime_remaining > 0); in distribute_cfs_runtime()
6051 raw_spin_lock(&cfs_b->lock); in distribute_cfs_runtime()
6052 runtime = -cfs_rq->runtime_remaining + 1; in distribute_cfs_runtime()
6053 if (runtime > cfs_b->runtime) in distribute_cfs_runtime()
6054 runtime = cfs_b->runtime; in distribute_cfs_runtime()
6055 cfs_b->runtime -= runtime; in distribute_cfs_runtime()
6056 remaining = cfs_b->runtime; in distribute_cfs_runtime()
6057 raw_spin_unlock(&cfs_b->lock); in distribute_cfs_runtime()
6059 cfs_rq->runtime_remaining += runtime; in distribute_cfs_runtime()
6062 if (cfs_rq->runtime_remaining > 0) { in distribute_cfs_runtime()
6091 * period the timer is deactivated until scheduling resumes; cfs_b->idle is
6099 if (cfs_b->quota == RUNTIME_INF) in do_sched_cfs_period_timer()
6102 throttled = !list_empty(&cfs_b->throttled_cfs_rq); in do_sched_cfs_period_timer()
6103 cfs_b->nr_periods += overrun; in do_sched_cfs_period_timer()
6105 /* Refill extra burst quota even if cfs_b->idle */ in do_sched_cfs_period_timer()
6112 if (cfs_b->idle && !throttled) in do_sched_cfs_period_timer()
6117 cfs_b->idle = 1; in do_sched_cfs_period_timer()
6122 cfs_b->nr_throttled += overrun; in do_sched_cfs_period_timer()
6125 * This check is repeated as we release cfs_b->lock while we unthrottle. in do_sched_cfs_period_timer()
6127 while (throttled && cfs_b->runtime > 0) { in do_sched_cfs_period_timer()
6128 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in do_sched_cfs_period_timer()
6129 /* we can't nest cfs_b->lock while distributing bandwidth */ in do_sched_cfs_period_timer()
6131 raw_spin_lock_irqsave(&cfs_b->lock, flags); in do_sched_cfs_period_timer()
6140 cfs_b->idle = 0; in do_sched_cfs_period_timer()
6158 * Requires cfs_b->lock for hrtimer_expires_remaining to be safe against the
6164 struct hrtimer *refresh_timer = &cfs_b->period_timer; in runtime_refresh_within()
6167 /* if the call-back is running a quota refresh is already occurring */ in runtime_refresh_within()
6188 if (cfs_b->slack_started) in start_cfs_slack_bandwidth()
6190 cfs_b->slack_started = true; in start_cfs_slack_bandwidth()
6192 hrtimer_start(&cfs_b->slack_timer, in start_cfs_slack_bandwidth()
6200 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in __return_cfs_rq_runtime()
6201 s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime; in __return_cfs_rq_runtime()
6206 raw_spin_lock(&cfs_b->lock); in __return_cfs_rq_runtime()
6207 if (cfs_b->quota != RUNTIME_INF) { in __return_cfs_rq_runtime()
6208 cfs_b->runtime += slack_runtime; in __return_cfs_rq_runtime()
6210 /* we are under rq->lock, defer unthrottling using a timer */ in __return_cfs_rq_runtime()
6211 if (cfs_b->runtime > sched_cfs_bandwidth_slice() && in __return_cfs_rq_runtime()
6212 !list_empty(&cfs_b->throttled_cfs_rq)) in __return_cfs_rq_runtime()
6215 raw_spin_unlock(&cfs_b->lock); in __return_cfs_rq_runtime()
6218 cfs_rq->runtime_remaining -= slack_runtime; in __return_cfs_rq_runtime()
6226 if (!cfs_rq->runtime_enabled || cfs_rq->nr_running) in return_cfs_rq_runtime()
6234 * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
6242 raw_spin_lock_irqsave(&cfs_b->lock, flags); in do_sched_cfs_slack_timer()
6243 cfs_b->slack_started = false; in do_sched_cfs_slack_timer()
6246 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in do_sched_cfs_slack_timer()
6250 if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) in do_sched_cfs_slack_timer()
6251 runtime = cfs_b->runtime; in do_sched_cfs_slack_timer()
6253 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in do_sched_cfs_slack_timer()
6264 * runtime as update_curr() throttling can not trigger until it's on-rq.
6271 /* an active group must be handled by the update_curr()->put() path */ in check_enqueue_throttle()
6272 if (!cfs_rq->runtime_enabled || cfs_rq->curr) in check_enqueue_throttle()
6281 if (cfs_rq->runtime_remaining <= 0) in check_enqueue_throttle()
6292 if (!tg->parent) in sync_throttle()
6295 cfs_rq = tg->cfs_rq[cpu]; in sync_throttle()
6296 pcfs_rq = tg->parent->cfs_rq[cpu]; in sync_throttle()
6298 cfs_rq->throttle_count = pcfs_rq->throttle_count; in sync_throttle()
6299 cfs_rq->throttled_clock_pelt = rq_clock_pelt(cpu_rq(cpu)); in sync_throttle()
6308 if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0)) in check_cfs_rq_runtime()
6342 raw_spin_lock_irqsave(&cfs_b->lock, flags); in sched_cfs_period_timer()
6344 overrun = hrtimer_forward_now(timer, cfs_b->period); in sched_cfs_period_timer()
6351 u64 new, old = ktime_to_ns(cfs_b->period); in sched_cfs_period_timer()
6360 cfs_b->period = ns_to_ktime(new); in sched_cfs_period_timer()
6361 cfs_b->quota *= 2; in sched_cfs_period_timer()
6362 cfs_b->burst *= 2; in sched_cfs_period_timer()
6368 div_u64(cfs_b->quota, NSEC_PER_USEC)); in sched_cfs_period_timer()
6374 div_u64(cfs_b->quota, NSEC_PER_USEC)); in sched_cfs_period_timer()
6382 cfs_b->period_active = 0; in sched_cfs_period_timer()
6383 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in sched_cfs_period_timer()
6390 raw_spin_lock_init(&cfs_b->lock); in init_cfs_bandwidth()
6391 cfs_b->runtime = 0; in init_cfs_bandwidth()
6392 cfs_b->quota = RUNTIME_INF; in init_cfs_bandwidth()
6393 cfs_b->period = ns_to_ktime(default_cfs_period()); in init_cfs_bandwidth()
6394 cfs_b->burst = 0; in init_cfs_bandwidth()
6395 cfs_b->hierarchical_quota = parent ? parent->hierarchical_quota : RUNTIME_INF; in init_cfs_bandwidth()
6397 INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq); in init_cfs_bandwidth()
6398 hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED); in init_cfs_bandwidth()
6399 cfs_b->period_timer.function = sched_cfs_period_timer; in init_cfs_bandwidth()
6402 hrtimer_set_expires(&cfs_b->period_timer, in init_cfs_bandwidth()
6403 get_random_u32_below(cfs_b->period)); in init_cfs_bandwidth()
6404 hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); in init_cfs_bandwidth()
6405 cfs_b->slack_timer.function = sched_cfs_slack_timer; in init_cfs_bandwidth()
6406 cfs_b->slack_started = false; in init_cfs_bandwidth()
6411 cfs_rq->runtime_enabled = 0; in init_cfs_rq_runtime()
6412 INIT_LIST_HEAD(&cfs_rq->throttled_list); in init_cfs_rq_runtime()
6414 INIT_LIST_HEAD(&cfs_rq->throttled_csd_list); in init_cfs_rq_runtime()
6420 lockdep_assert_held(&cfs_b->lock); in start_cfs_bandwidth()
6422 if (cfs_b->period_active) in start_cfs_bandwidth()
6425 cfs_b->period_active = 1; in start_cfs_bandwidth()
6426 hrtimer_forward_now(&cfs_b->period_timer, cfs_b->period); in start_cfs_bandwidth()
6427 hrtimer_start_expires(&cfs_b->period_timer, HRTIMER_MODE_ABS_PINNED); in start_cfs_bandwidth()
6435 if (!cfs_b->throttled_cfs_rq.next) in destroy_cfs_bandwidth()
6438 hrtimer_cancel(&cfs_b->period_timer); in destroy_cfs_bandwidth()
6439 hrtimer_cancel(&cfs_b->slack_timer); in destroy_cfs_bandwidth()
6456 if (list_empty(&rq->cfsb_csd_list)) in destroy_cfs_bandwidth()
6482 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth; in update_runtime_enabled()
6483 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in update_runtime_enabled()
6485 raw_spin_lock(&cfs_b->lock); in update_runtime_enabled()
6486 cfs_rq->runtime_enabled = cfs_b->quota != RUNTIME_INF; in update_runtime_enabled()
6487 raw_spin_unlock(&cfs_b->lock); in update_runtime_enabled()
6508 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in unthrottle_offline_cfs_rqs()
6510 if (!cfs_rq->runtime_enabled) in unthrottle_offline_cfs_rqs()
6517 cfs_rq->runtime_remaining = 1; in unthrottle_offline_cfs_rqs()
6522 cfs_rq->runtime_enabled = 0; in unthrottle_offline_cfs_rqs()
6539 if (cfs_rq->runtime_enabled || in cfs_task_bw_constrained()
6540 tg_cfs_bandwidth(cfs_rq->tg)->hierarchical_quota != RUNTIME_INF) in cfs_task_bw_constrained()
6558 if (rq->nr_running != 1) in sched_fair_update_stop_tick()
6632 struct sched_entity *se = &p->se; in hrtick_start_fair()
6636 if (rq->cfs.h_nr_running > 1) { in hrtick_start_fair()
6637 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime; in hrtick_start_fair()
6638 u64 slice = se->slice; in hrtick_start_fair()
6639 s64 delta = slice - ran; in hrtick_start_fair()
6657 struct task_struct *curr = rq->curr; in hrtick_update()
6659 if (!hrtick_enabled_fair(rq) || curr->sched_class != &fair_sched_class) in hrtick_update()
6696 WRITE_ONCE(rd->overutilized, status); in set_rd_overutilized_status()
6709 if (!READ_ONCE(rq->rd->overutilized) && cpu_overutilized(rq->cpu)) in check_update_overutilized_status()
6710 set_rd_overutilized_status(rq->rd, SG_OVERUTILIZED); in check_update_overutilized_status()
6719 return unlikely(rq->nr_running == rq->cfs.idle_h_nr_running && in sched_idle_rq()
6720 rq->nr_running); in sched_idle_rq()
6739 struct sched_entity *se = &p->se; in enqueue_task_fair()
6749 util_est_enqueue(&rq->cfs, p); in enqueue_task_fair()
6756 if (p->in_iowait) in enqueue_task_fair()
6760 if (se->on_rq) in enqueue_task_fair()
6765 cfs_rq->h_nr_running++; in enqueue_task_fair()
6766 cfs_rq->idle_h_nr_running += idle_h_nr_running; in enqueue_task_fair()
6785 cfs_rq->h_nr_running++; in enqueue_task_fair()
6786 cfs_rq->idle_h_nr_running += idle_h_nr_running; in enqueue_task_fair()
6832 struct sched_entity *se = &p->se; in dequeue_task_fair()
6837 util_est_dequeue(&rq->cfs, p); in dequeue_task_fair()
6843 cfs_rq->h_nr_running--; in dequeue_task_fair()
6844 cfs_rq->idle_h_nr_running -= idle_h_nr_running; in dequeue_task_fair()
6854 if (cfs_rq->load.weight) { in dequeue_task_fair()
6855 /* Avoid re-evaluating load for this entity: */ in dequeue_task_fair()
6875 cfs_rq->h_nr_running--; in dequeue_task_fair()
6876 cfs_rq->idle_h_nr_running -= idle_h_nr_running; in dequeue_task_fair()
6892 rq->next_balance = jiffies; in dequeue_task_fair()
6895 util_est_update(&rq->cfs, p, task_sleep); in dequeue_task_fair()
6921 return cfs_rq_load_avg(&rq->cfs); in cpu_load()
6925 * cpu_load_without - compute CPU load without any contributions from *p
6943 if (cpu_of(rq) != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in cpu_load_without()
6946 cfs_rq = &rq->cfs; in cpu_load_without()
6947 load = READ_ONCE(cfs_rq->avg.load_avg); in cpu_load_without()
6957 return cfs_rq_runnable_avg(&rq->cfs); in cpu_runnable()
6966 if (cpu_of(rq) != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in cpu_runnable_without()
6969 cfs_rq = &rq->cfs; in cpu_runnable_without()
6970 runnable = READ_ONCE(cfs_rq->avg.runnable_avg); in cpu_runnable_without()
6973 lsub_positive(&runnable, p->se.avg.runnable_avg); in cpu_runnable_without()
6980 return cpu_rq(cpu)->cpu_capacity; in capacity_of()
6989 if (time_after(jiffies, current->wakee_flip_decay_ts + HZ)) { in record_wakee()
6990 current->wakee_flips >>= 1; in record_wakee()
6991 current->wakee_flip_decay_ts = jiffies; in record_wakee()
6994 if (current->last_wakee != p) { in record_wakee()
6995 current->last_wakee = p; in record_wakee()
6996 current->wakee_flips++; in record_wakee()
7001 * Detect M:N waker/wakee relationships via a switching-frequency heuristic.
7011 * non-monogamous, with partner count exceeding socket size.
7019 unsigned int master = current->wakee_flips; in wake_wide()
7020 unsigned int slave = p->wakee_flips; in wake_wide()
7035 * wake_affine_idle() - only considers 'now', it check if the waking CPU is
7036 * cache-affine and is (or will be) idle.
7038 * wake_affine_weight() - considers the weight to reflect the average
7039 * scheduling latency of the CPUs. This seems to work
7060 if (sync && cpu_rq(this_cpu)->nr_running == 1) in wake_affine_idle()
7084 this_eff_load -= current_load; in wake_affine_weight()
7095 prev_eff_load -= task_load; in wake_affine_weight()
7097 prev_eff_load *= 100 + (sd->imbalance_pct - 100) / 2; in wake_affine_weight()
7123 schedstat_inc(p->stats.nr_wakeups_affine_attempts); in wake_affine()
7127 schedstat_inc(sd->ttwu_move_affine); in wake_affine()
7128 schedstat_inc(p->stats.nr_wakeups_affine); in wake_affine()
7136 * find_idlest_group_cpu - find the idlest CPU among the CPUs in the group.
7145 int shallowest_idle_cpu = -1; in find_idlest_group_cpu()
7149 if (group->group_weight == 1) in find_idlest_group_cpu()
7153 for_each_cpu_and(i, sched_group_span(group), p->cpus_ptr) { in find_idlest_group_cpu()
7164 if (idle && idle->exit_latency < min_exit_latency) { in find_idlest_group_cpu()
7167 * has the smallest exit latency irrespective in find_idlest_group_cpu()
7170 min_exit_latency = idle->exit_latency; in find_idlest_group_cpu()
7171 latest_idle_timestamp = rq->idle_stamp; in find_idlest_group_cpu()
7173 } else if ((!idle || idle->exit_latency == min_exit_latency) && in find_idlest_group_cpu()
7174 rq->idle_stamp > latest_idle_timestamp) { in find_idlest_group_cpu()
7180 latest_idle_timestamp = rq->idle_stamp; in find_idlest_group_cpu()
7183 } else if (shallowest_idle_cpu == -1) { in find_idlest_group_cpu()
7192 return shallowest_idle_cpu != -1 ? shallowest_idle_cpu : least_loaded_cpu; in find_idlest_group_cpu()
7200 if (!cpumask_intersects(sched_domain_span(sd), p->cpus_ptr)) in find_idlest_cpu()
7208 sync_entity_load_avg(&p->se); in find_idlest_cpu()
7215 if (!(sd->flags & sd_flag)) { in find_idlest_cpu()
7216 sd = sd->child; in find_idlest_cpu()
7222 sd = sd->child; in find_idlest_cpu()
7229 sd = sd->child; in find_idlest_cpu()
7235 weight = sd->span_weight; in find_idlest_cpu()
7238 if (weight <= tmp->span_weight) in find_idlest_cpu()
7240 if (tmp->flags & sd_flag) in find_idlest_cpu()
7254 return -1; in __select_idle_cpu()
7267 WRITE_ONCE(sds->has_idle_cores, val); in set_idle_cores()
7276 return READ_ONCE(sds->has_idle_cores); in test_idle_cores()
7283 * information in sd_llc_shared->has_idle_cores.
7313 * sd_llc->shared->has_idle_cores and enabled through update_idle_core() above.
7323 if (*idle_cpu == -1) { in select_idle_core()
7332 if (*idle_cpu == -1 && cpumask_test_cpu(cpu, cpus)) in select_idle_core()
7340 return -1; in select_idle_core()
7350 for_each_cpu_and(cpu, cpu_smt_mask(target), p->cpus_ptr) { in select_idle_smt()
7363 return -1; in select_idle_smt()
7384 return -1; in select_idle_smt()
7391 * comparing the average scan cost (tracked in sd->avg_scan_cost) against the
7392 * average idle time for this rq (as found in rq->avg_idle).
7397 int i, cpu, idle_cpu = -1, nr = INT_MAX; in select_idle_cpu()
7404 cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr); in select_idle_cpu()
7412 return -1; in select_idle_cpu()
7419 if (unlikely(this_rq->wake_stamp < now)) { in select_idle_cpu()
7420 while (this_rq->wake_stamp < now && this_rq->wake_avg_idle) { in select_idle_cpu()
7421 this_rq->wake_stamp++; in select_idle_cpu()
7422 this_rq->wake_avg_idle >>= 1; in select_idle_cpu()
7426 avg_idle = this_rq->wake_avg_idle; in select_idle_cpu()
7427 avg_cost = this_sd->avg_scan_cost + 1; in select_idle_cpu()
7429 span_avg = sd->span_weight * avg_idle; in select_idle_cpu()
7441 /* because !--nr is the condition to stop scan */ in select_idle_cpu()
7442 nr = READ_ONCE(sd_share->nr_idle_scan) + 1; in select_idle_cpu()
7445 return -1; in select_idle_cpu()
7456 if (!--nr) in select_idle_cpu()
7457 return -1; in select_idle_cpu()
7468 time = cpu_clock(this) - time; in select_idle_cpu()
7474 this_rq->wake_avg_idle -= min(this_rq->wake_avg_idle, time); in select_idle_cpu()
7476 update_avg(&this_sd->avg_scan_cost, time); in select_idle_cpu()
7492 int cpu, best_cpu = -1; in select_idle_capacity()
7496 cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr); in select_idle_capacity()
7518 cpu_cap = arch_scale_cpu_capacity(cpu) - thermal_load_avg(cpu_rq(cpu)); in select_idle_capacity()
7521 * First, select CPU which fits better (-1 being better than 0). in select_idle_capacity()
7565 sync_entity_load_avg(&p->se); in select_idle_sibling()
7572 * per-cpu select_rq_mask usage in select_idle_sibling()
7589 * Allow a per-cpu kthread to stack with the wakee if the in select_idle_sibling()
7592 * per-cpu kthread that is now complete and the wakeup is in select_idle_sibling()
7599 this_rq()->nr_running <= 1 && in select_idle_sibling()
7605 recent_used_cpu = p->recent_used_cpu; in select_idle_sibling()
7606 p->recent_used_cpu = prev; in select_idle_sibling()
7611 cpumask_test_cpu(recent_used_cpu, p->cpus_ptr) && in select_idle_sibling()
7658 * cpu_util() - Estimates the amount of CPU capacity used by CFS tasks.
7661 * @dst_cpu: CPU @p migrates to, -1 if @p moves from @cpu or @p == NULL
7668 * recent utilization of currently non-runnable tasks on that CPU.
7676 * previously-executed tasks, which helps better deduce how busy a CPU will
7677 * be when a long-sleeping task wakes up. The contribution to CPU utilization
7691 * could be seen as over-utilized even though CPU1 has 20% of spare CPU
7694 * after task migrations (scheduler-driven DVFS).
7701 struct cfs_rq *cfs_rq = &cpu_rq(cpu)->cfs; in cpu_util()
7702 unsigned long util = READ_ONCE(cfs_rq->avg.util_avg); in cpu_util()
7706 runnable = READ_ONCE(cfs_rq->avg.runnable_avg); in cpu_util()
7711 * If @dst_cpu is -1 or @p migrates from @cpu to @dst_cpu remove its in cpu_util()
7724 util_est = READ_ONCE(cfs_rq->avg.util_est.enqueued); in cpu_util()
7727 * During wake-up @p isn't enqueued yet and doesn't contribute in cpu_util()
7728 * to any cpu_rq(cpu)->cfs.avg.util_est.enqueued. in cpu_util()
7732 * During exec (@dst_cpu = -1) @p is enqueued and does in cpu_util()
7733 * contribute to cpu_rq(cpu)->cfs.util_est.enqueued. in cpu_util()
7742 * p->on_rq = TASK_ON_RQ_MIGRATING; in cpu_util()
7743 * -------------------------------- A in cpu_util()
7747 * -------------------------------- B in cpu_util()
7765 return cpu_util(cpu, NULL, -1, 0); in cpu_util_cfs()
7770 return cpu_util(cpu, NULL, -1, 1); in cpu_util_cfs_boost()
7789 if (cpu != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in cpu_util_without()
7792 return cpu_util(cpu, p, -1, 0); in cpu_util_without()
7796 * energy_env - Utilization landscape for energy estimation.
7802 * @pd_cap: Entire perf domain capacity. (pd->nr_cpus * cpu_cap).
7828 eenv->task_busy_time = busy_time; in eenv_task_busy_time()
7842 * - A stable PD utilization, no matter which CPU of that PD we want to place
7845 * - A fair comparison between CPUs as the task contribution (task_util())
7850 * exceed @eenv->pd_cap.
7860 unsigned long util = cpu_util(cpu, p, -1, 0); in eenv_pd_busy_time()
7865 eenv->pd_busy_time = min(eenv->pd_cap, busy_time); in eenv_pd_busy_time()
7872 * Returns the maximum utilization among @eenv->cpus. This utilization can't
7873 * exceed @eenv->cpu_cap.
7914 return min(max_util, eenv->cpu_cap); in eenv_pd_max_util()
7927 unsigned long busy_time = eenv->pd_busy_time; in compute_energy()
7930 busy_time = min(eenv->pd_cap, busy_time + eenv->task_busy_time); in compute_energy()
7932 return em_cpu_energy(pd->em_pd, max_util, busy_time, eenv->cpu_cap); in compute_energy()
7936 * find_energy_efficient_cpu(): Find most energy-efficient target CPU for the
7940 * out which of the CPU candidates is the most energy-efficient.
7957 * cluster-packing, and spreading inside a cluster. That should at least be
7958 * a good thing for latency, and this is consistent with the idea that most
7964 * NOTE: Forkees are not accepted in the energy-aware wake-up path because
7968 * to be energy-inefficient in some use-cases. The alternative would be to
7971 * other use-cases too. So, until someone finds a better way to solve this,
7972 * let's keep things simple by re-using the existing slow path.
7980 struct root_domain *rd = this_rq()->rd; in find_energy_efficient_cpu()
7981 int cpu, best_energy_cpu, target = -1; in find_energy_efficient_cpu()
7982 int prev_fits = -1, best_fits = -1; in find_energy_efficient_cpu()
7990 pd = rcu_dereference(rd->pd); in find_energy_efficient_cpu()
7991 if (!pd || READ_ONCE(rd->overutilized)) in find_energy_efficient_cpu()
7995 * Energy-aware wake-up happens on the lowest sched_domain starting in find_energy_efficient_cpu()
8000 sd = sd->parent; in find_energy_efficient_cpu()
8006 sync_entity_load_avg(&p->se); in find_energy_efficient_cpu()
8012 for (; pd; pd = pd->next) { in find_energy_efficient_cpu()
8015 long prev_spare_cap = -1, max_spare_cap = -1; in find_energy_efficient_cpu()
8018 int max_spare_cap_cpu = -1; in find_energy_efficient_cpu()
8019 int fits, max_fits = -1; in find_energy_efficient_cpu()
8029 cpu_thermal_cap -= arch_scale_thermal_pressure(cpu); in find_energy_efficient_cpu()
8042 if (!cpumask_test_cpu(cpu, p->cpus_ptr)) in find_energy_efficient_cpu()
8061 * max-aggregated uclamp_{min, max}. in find_energy_efficient_cpu()
8098 base_energy = compute_energy(&eenv, pd, cpus, p, -1); in find_energy_efficient_cpu()
8101 if (prev_spare_cap > -1) { in find_energy_efficient_cpu()
8107 prev_delta -= base_energy; in find_energy_efficient_cpu()
8131 cur_delta -= base_energy; in find_energy_efficient_cpu()
8175 int sync = (wake_flags & WF_SYNC) && !(current->flags & PF_EXITING); in select_task_rq_fair()
8184 * required for stable ->cpus_allowed in select_task_rq_fair()
8186 lockdep_assert_held(&p->pi_lock); in select_task_rq_fair()
8191 cpumask_test_cpu(cpu, p->cpus_ptr)) in select_task_rq_fair()
8201 want_affine = !wake_wide(p) && cpumask_test_cpu(cpu, p->cpus_ptr); in select_task_rq_fair()
8210 if (want_affine && (tmp->flags & SD_WAKE_AFFINE) && in select_task_rq_fair()
8224 if (tmp->flags & sd_flag) in select_task_rq_fair()
8245 * previous CPU. The caller guarantees p->pi_lock or task_rq(p)->lock is held.
8249 struct sched_entity *se = &p->se; in migrate_task_rq_fair()
8258 * leading to an inflation after wake-up on the new rq. in migrate_task_rq_fair()
8268 se->avg.last_update_time = 0; in migrate_task_rq_fair()
8275 remove_entity_load_avg(&p->se); in task_dead_fair()
8281 if (rq->nr_running) in balance_fair()
8291 if (SCHED_WARN_ON(!se->on_rq)) in set_next_buddy()
8295 cfs_rq_of(se)->next = se; in set_next_buddy()
8304 struct task_struct *curr = rq->curr; in check_preempt_wakeup_fair()
8305 struct sched_entity *se = &curr->se, *pse = &p->se; in check_preempt_wakeup_fair()
8317 * next-buddy nomination below. in check_preempt_wakeup_fair()
8331 * Note: this also catches the edge-case of curr being in a throttled in check_preempt_wakeup_fair()
8350 * Preempt an idle entity in favor of a non-idle entity (and don't preempt in check_preempt_wakeup_fair()
8361 if (unlikely(p->policy != SCHED_NORMAL)) in check_preempt_wakeup_fair()
8385 cfs_rq = &rq->cfs; in pick_task_fair()
8386 if (!cfs_rq->nr_running) in pick_task_fair()
8390 struct sched_entity *curr = cfs_rq->curr; in pick_task_fair()
8394 if (curr->on_rq) in pick_task_fair()
8414 struct cfs_rq *cfs_rq = &rq->cfs; in pick_next_task_fair()
8424 if (!prev || prev->sched_class != &fair_sched_class) in pick_next_task_fair()
8436 struct sched_entity *curr = cfs_rq->curr; in pick_next_task_fair()
8440 * have to consider cfs_rq->curr. If it is still a runnable in pick_next_task_fair()
8445 if (curr->on_rq) in pick_next_task_fair()
8457 cfs_rq = &rq->cfs; in pick_next_task_fair()
8459 if (!cfs_rq->nr_running) in pick_next_task_fair()
8478 struct sched_entity *pse = &prev->se; in pick_next_task_fair()
8481 int se_depth = se->depth; in pick_next_task_fair()
8482 int pse_depth = pse->depth; in pick_next_task_fair()
8519 list_move(&p->se.group_node, &rq->cfs_tasks); in pick_next_task_fair()
8537 * Because newidle_balance() releases (and re-acquires) rq->lock, it is in pick_next_task_fair()
8539 * must re-start the pick_next_entity() loop. in pick_next_task_fair()
8566 struct sched_entity *se = &prev->se; in put_prev_task_fair()
8580 struct task_struct *curr = rq->curr; in yield_task_fair()
8582 struct sched_entity *se = &curr->se; in yield_task_fair()
8587 if (unlikely(rq->nr_running == 1)) in yield_task_fair()
8594 * Update run-time statistics of the 'current'. in yield_task_fair()
8604 se->deadline += calc_delta_fair(se->slice, se); in yield_task_fair()
8609 struct sched_entity *se = &p->se; in yield_to_task_fair()
8612 if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se))) in yield_to_task_fair()
8625 * Fair scheduling class load-balancing methods.
8629 * The purpose of load-balancing is to achieve the same basic fairness the
8630 * per-CPU scheduler provides, namely provide a proportional amount of compute
8635 * Where W_i,n is the n-th weight average for CPU i. The instantaneous weight
8640 * Where w_i,j is the weight of the j-th runnable task on CPU i. This weight
8646 * W'_i,n = (2^n - 1) / 2^n * W_i,n + 1 / 2^n * W_i,0 (3)
8655 * imb_i,j = max{ avg(W/C), W_i/C_i } - min{ avg(W/C), W_j/C_j } (4)
8662 * - infeasible weights;
8663 * - local vs global optima in the discrete case. ]
8673 * of load-balance at each level inv. proportional to the number of CPUs in
8679 * \Sum { --- * --- * 2^i } = O(n) (5)
8681 * `- size of each group
8682 * | | `- number of CPUs doing load-balance
8683 * | `- freq
8684 * `- sum over all levels
8726 * W_i,0 = \Sum_j \Prod_k w_k * ----- (9)
8733 * w_i,j,k is the weight of the j-th runnable task in the k-th cgroup on CPU i.
8815 /* The set of CPUs under consideration for load-balancing */
8830 * Is this task likely cache-hot:
8836 lockdep_assert_rq_held(env->src_rq); in task_hot()
8838 if (p->sched_class != &fair_sched_class) in task_hot()
8845 if (env->sd->flags & SD_SHARE_CPUCAPACITY) in task_hot()
8851 if (sched_feat(CACHE_HOT_BUDDY) && env->dst_rq->nr_running && in task_hot()
8852 (&p->se == cfs_rq_of(&p->se)->next)) in task_hot()
8855 if (sysctl_sched_migration_cost == -1) in task_hot()
8862 if (!sched_core_cookie_match(cpu_rq(env->dst_cpu), p)) in task_hot()
8868 delta = rq_clock_task(env->src_rq) - p->se.exec_start; in task_hot()
8877 * Returns -1, if task migration is not affected by locality.
8881 struct numa_group *numa_group = rcu_dereference(p->numa_group); in migrate_degrades_locality()
8886 return -1; in migrate_degrades_locality()
8888 if (!p->numa_faults || !(env->sd->flags & SD_NUMA)) in migrate_degrades_locality()
8889 return -1; in migrate_degrades_locality()
8891 src_nid = cpu_to_node(env->src_cpu); in migrate_degrades_locality()
8892 dst_nid = cpu_to_node(env->dst_cpu); in migrate_degrades_locality()
8895 return -1; in migrate_degrades_locality()
8898 if (src_nid == p->numa_preferred_nid) { in migrate_degrades_locality()
8899 if (env->src_rq->nr_running > env->src_rq->nr_preferred_running) in migrate_degrades_locality()
8902 return -1; in migrate_degrades_locality()
8906 if (dst_nid == p->numa_preferred_nid) in migrate_degrades_locality()
8910 if (env->idle == CPU_IDLE) in migrate_degrades_locality()
8911 return -1; in migrate_degrades_locality()
8929 return -1; in migrate_degrades_locality()
8934 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
8941 lockdep_assert_rq_held(env->src_rq); in can_migrate_task()
8942 if (p->sched_task_hot) in can_migrate_task()
8943 p->sched_task_hot = 0; in can_migrate_task()
8950 * 4) are cache-hot on their current CPU. in can_migrate_task()
8952 if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu)) in can_migrate_task()
8959 if (!cpumask_test_cpu(env->dst_cpu, p->cpus_ptr)) { in can_migrate_task()
8962 schedstat_inc(p->stats.nr_failed_migrations_affine); in can_migrate_task()
8964 env->flags |= LBF_SOME_PINNED; in can_migrate_task()
8972 * - for NEWLY_IDLE in can_migrate_task()
8973 * - if we have already computed one in current iteration in can_migrate_task()
8974 * - if it's an active balance in can_migrate_task()
8976 if (env->idle == CPU_NEWLY_IDLE || in can_migrate_task()
8977 env->flags & (LBF_DST_PINNED | LBF_ACTIVE_LB)) in can_migrate_task()
8980 /* Prevent to re-select dst_cpu via env's CPUs: */ in can_migrate_task()
8981 for_each_cpu_and(cpu, env->dst_grpmask, env->cpus) { in can_migrate_task()
8982 if (cpumask_test_cpu(cpu, p->cpus_ptr)) { in can_migrate_task()
8983 env->flags |= LBF_DST_PINNED; in can_migrate_task()
8984 env->new_dst_cpu = cpu; in can_migrate_task()
8993 env->flags &= ~LBF_ALL_PINNED; in can_migrate_task()
8995 if (task_on_cpu(env->src_rq, p)) { in can_migrate_task()
8996 schedstat_inc(p->stats.nr_failed_migrations_running); in can_migrate_task()
9007 if (env->flags & LBF_ACTIVE_LB) in can_migrate_task()
9011 if (tsk_cache_hot == -1) in can_migrate_task()
9015 env->sd->nr_balance_failed > env->sd->cache_nice_tries) { in can_migrate_task()
9017 p->sched_task_hot = 1; in can_migrate_task()
9021 schedstat_inc(p->stats.nr_failed_migrations_hot); in can_migrate_task()
9026 * detach_task() -- detach the task for the migration specified in env
9030 lockdep_assert_rq_held(env->src_rq); in detach_task()
9032 if (p->sched_task_hot) { in detach_task()
9033 p->sched_task_hot = 0; in detach_task()
9034 schedstat_inc(env->sd->lb_hot_gained[env->idle]); in detach_task()
9035 schedstat_inc(p->stats.nr_forced_migrations); in detach_task()
9038 deactivate_task(env->src_rq, p, DEQUEUE_NOCLOCK); in detach_task()
9039 set_task_cpu(p, env->dst_cpu); in detach_task()
9043 * detach_one_task() -- tries to dequeue exactly one task from env->src_rq, as
9052 lockdep_assert_rq_held(env->src_rq); in detach_one_task()
9055 &env->src_rq->cfs_tasks, se.group_node) { in detach_one_task()
9063 * lb_gained[env->idle] is updated (other is detach_tasks) in detach_one_task()
9067 schedstat_inc(env->sd->lb_gained[env->idle]); in detach_one_task()
9074 * detach_tasks() -- tries to detach up to imbalance load/util/tasks from
9081 struct list_head *tasks = &env->src_rq->cfs_tasks; in detach_tasks()
9086 lockdep_assert_rq_held(env->src_rq); in detach_tasks()
9092 if (env->src_rq->nr_running <= 1) { in detach_tasks()
9093 env->flags &= ~LBF_ALL_PINNED; in detach_tasks()
9097 if (env->imbalance <= 0) in detach_tasks()
9105 if (env->idle != CPU_NOT_IDLE && env->src_rq->nr_running <= 1) in detach_tasks()
9108 env->loop++; in detach_tasks()
9110 if (env->loop > env->loop_max) in detach_tasks()
9114 if (env->loop > env->loop_break) { in detach_tasks()
9115 env->loop_break += SCHED_NR_MIGRATE_BREAK; in detach_tasks()
9116 env->flags |= LBF_NEED_BREAK; in detach_tasks()
9125 switch (env->migration_type) { in detach_tasks()
9130 * value. Make sure that env->imbalance decreases in detach_tasks()
9137 load < 16 && !env->sd->nr_balance_failed) in detach_tasks()
9146 if (shr_bound(load, env->sd->nr_balance_failed) > env->imbalance) in detach_tasks()
9149 env->imbalance -= load; in detach_tasks()
9155 if (util > env->imbalance) in detach_tasks()
9158 env->imbalance -= util; in detach_tasks()
9162 env->imbalance--; in detach_tasks()
9167 if (task_fits_cpu(p, env->src_cpu)) in detach_tasks()
9170 env->imbalance = 0; in detach_tasks()
9175 list_add(&p->se.group_node, &env->tasks); in detach_tasks()
9181 * NEWIDLE balancing is a source of latency, so preemptible in detach_tasks()
9185 if (env->idle == CPU_NEWLY_IDLE) in detach_tasks()
9193 if (env->imbalance <= 0) in detach_tasks()
9198 if (p->sched_task_hot) in detach_tasks()
9199 schedstat_inc(p->stats.nr_failed_migrations_hot); in detach_tasks()
9201 list_move(&p->se.group_node, tasks); in detach_tasks()
9209 schedstat_add(env->sd->lb_gained[env->idle], detached); in detach_tasks()
9215 * attach_task() -- attach the task detached by detach_task() to its new rq.
9227 * attach_one_task() -- attaches the task returned from detach_one_task() to
9241 * attach_tasks() -- attaches all tasks detached by detach_tasks() to their
9246 struct list_head *tasks = &env->tasks; in attach_tasks()
9250 rq_lock(env->dst_rq, &rf); in attach_tasks()
9251 update_rq_clock(env->dst_rq); in attach_tasks()
9255 list_del_init(&p->se.group_node); in attach_tasks()
9257 attach_task(env->dst_rq, p); in attach_tasks()
9260 rq_unlock(env->dst_rq, &rf); in attach_tasks()
9266 if (cfs_rq->avg.load_avg) in cfs_rq_has_blocked()
9269 if (cfs_rq->avg.util_avg) in cfs_rq_has_blocked()
9277 if (READ_ONCE(rq->avg_rt.util_avg)) in others_have_blocked()
9280 if (READ_ONCE(rq->avg_dl.util_avg)) in others_have_blocked()
9287 if (READ_ONCE(rq->avg_irq.util_avg)) in others_have_blocked()
9296 WRITE_ONCE(rq->last_blocked_load_update_tick, jiffies); in update_blocked_load_tick()
9302 rq->has_blocked_load = 0; in update_blocked_load_status()
9322 curr_class = rq->curr->sched_class; in __update_blocked_others()
9355 if (cfs_rq->nr_running == 0) in __update_blocked_fair()
9358 if (cfs_rq == &rq->cfs) in __update_blocked_fair()
9363 se = cfs_rq->tg->se[cpu]; in __update_blocked_fair()
9384 * This needs to be done in a top-down fashion because the load of a child
9390 struct sched_entity *se = cfs_rq->tg->se[cpu_of(rq)]; in update_cfs_rq_h_load()
9394 if (cfs_rq->last_h_load_update == now) in update_cfs_rq_h_load()
9397 WRITE_ONCE(cfs_rq->h_load_next, NULL); in update_cfs_rq_h_load()
9400 WRITE_ONCE(cfs_rq->h_load_next, se); in update_cfs_rq_h_load()
9401 if (cfs_rq->last_h_load_update == now) in update_cfs_rq_h_load()
9406 cfs_rq->h_load = cfs_rq_load_avg(cfs_rq); in update_cfs_rq_h_load()
9407 cfs_rq->last_h_load_update = now; in update_cfs_rq_h_load()
9410 while ((se = READ_ONCE(cfs_rq->h_load_next)) != NULL) { in update_cfs_rq_h_load()
9411 load = cfs_rq->h_load; in update_cfs_rq_h_load()
9412 load = div64_ul(load * se->avg.load_avg, in update_cfs_rq_h_load()
9415 cfs_rq->h_load = load; in update_cfs_rq_h_load()
9416 cfs_rq->last_h_load_update = now; in update_cfs_rq_h_load()
9425 return div64_ul(p->se.avg.load_avg * cfs_rq->h_load, in task_h_load()
9431 struct cfs_rq *cfs_rq = &rq->cfs; in __update_blocked_fair()
9443 return p->se.avg.load_avg; in task_h_load()
9469 * sg_lb_stats - stats of a sched_group required for load_balancing
9492 * sd_lb_stats - Structure to store the statistics of a sched_domain
9546 used = READ_ONCE(rq->avg_rt.util_avg); in scale_rt_capacity()
9547 used += READ_ONCE(rq->avg_dl.util_avg); in scale_rt_capacity()
9553 free = max - used; in scale_rt_capacity()
9561 struct sched_group *sdg = sd->groups; in update_cpu_capacity()
9566 cpu_rq(cpu)->cpu_capacity = capacity; in update_cpu_capacity()
9569 sdg->sgc->capacity = capacity; in update_cpu_capacity()
9570 sdg->sgc->min_capacity = capacity; in update_cpu_capacity()
9571 sdg->sgc->max_capacity = capacity; in update_cpu_capacity()
9576 struct sched_domain *child = sd->child; in update_group_capacity()
9577 struct sched_group *group, *sdg = sd->groups; in update_group_capacity()
9581 interval = msecs_to_jiffies(sd->balance_interval); in update_group_capacity()
9583 sdg->sgc->next_update = jiffies + interval; in update_group_capacity()
9594 if (child->flags & SD_OVERLAP) { in update_group_capacity()
9613 group = child->groups; in update_group_capacity()
9615 struct sched_group_capacity *sgc = group->sgc; in update_group_capacity()
9617 capacity += sgc->capacity; in update_group_capacity()
9618 min_capacity = min(sgc->min_capacity, min_capacity); in update_group_capacity()
9619 max_capacity = max(sgc->max_capacity, max_capacity); in update_group_capacity()
9620 group = group->next; in update_group_capacity()
9621 } while (group != child->groups); in update_group_capacity()
9624 sdg->sgc->capacity = capacity; in update_group_capacity()
9625 sdg->sgc->min_capacity = min_capacity; in update_group_capacity()
9626 sdg->sgc->max_capacity = max_capacity; in update_group_capacity()
9637 return ((rq->cpu_capacity * sd->imbalance_pct) < in check_cpu_capacity()
9648 return rq->misfit_task_load && in check_misfit_status()
9649 (arch_scale_cpu_capacity(rq->cpu) < rq->rd->max_cpu_capacity || in check_misfit_status()
9655 * groups is inadequate due to ->cpus_ptr constraints.
9664 * If we were to balance group-wise we'd place two tasks in the first group and
9684 return group->sgc->imbalance; in sg_imbalanced()
9702 if (sgs->sum_nr_running < sgs->group_weight) in group_has_capacity()
9705 if ((sgs->group_capacity * imbalance_pct) < in group_has_capacity()
9706 (sgs->group_runnable * 100)) in group_has_capacity()
9709 if ((sgs->group_capacity * 100) > in group_has_capacity()
9710 (sgs->group_util * imbalance_pct)) in group_has_capacity()
9727 if (sgs->sum_nr_running <= sgs->group_weight) in group_is_overloaded()
9730 if ((sgs->group_capacity * 100) < in group_is_overloaded()
9731 (sgs->group_util * imbalance_pct)) in group_is_overloaded()
9734 if ((sgs->group_capacity * imbalance_pct) < in group_is_overloaded()
9735 (sgs->group_runnable * 100)) in group_is_overloaded()
9752 if (sgs->group_asym_packing) in group_classify()
9755 if (sgs->group_smt_balance) in group_classify()
9758 if (sgs->group_misfit_task_load) in group_classify()
9768 * sched_use_asym_prio - Check whether asym_packing priority must be used
9783 return sd->flags & SD_SHARE_CPUCAPACITY || is_core_idle(cpu); in sched_use_asym_prio()
9787 * sched_asym - Check if the destination CPU can do asym_packing load balance
9789 * @sds: Load-balancing data with statistics of the local group
9790 * @sgs: Load-balancing statistics of the candidate busiest group
9812 if (!sched_use_asym_prio(env->sd, env->dst_cpu)) in sched_asym()
9819 if (group->flags & SD_SHARE_CPUCAPACITY) { in sched_asym()
9820 if (sgs->group_weight - sgs->idle_cpus != 1) in sched_asym()
9824 return sched_asym_prefer(env->dst_cpu, group->asym_prefer_cpu); in sched_asym()
9834 return (sg1->flags & SD_SHARE_CPUCAPACITY) != in smt_vs_nonsmt_groups()
9835 (sg2->flags & SD_SHARE_CPUCAPACITY); in smt_vs_nonsmt_groups()
9841 if (env->idle == CPU_NOT_IDLE) in smt_balance()
9850 if (group->flags & SD_SHARE_CPUCAPACITY && in smt_balance()
9851 sgs->sum_h_nr_running > 1) in smt_balance()
9865 if (env->idle == CPU_NOT_IDLE || !busiest->sum_nr_running) in sibling_imbalance()
9868 ncores_busiest = sds->busiest->cores; in sibling_imbalance()
9869 ncores_local = sds->local->cores; in sibling_imbalance()
9872 imbalance = busiest->sum_nr_running; in sibling_imbalance()
9873 lsub_positive(&imbalance, local->sum_nr_running); in sibling_imbalance()
9878 imbalance = ncores_local * busiest->sum_nr_running; in sibling_imbalance()
9879 lsub_positive(&imbalance, ncores_busiest * local->sum_nr_running); in sibling_imbalance()
9885 if (imbalance <= 1 && local->sum_nr_running == 0 && in sibling_imbalance()
9886 busiest->sum_nr_running > 1) in sibling_imbalance()
9899 if (rq->cfs.h_nr_running != 1) in sched_reduced_capacity()
9906 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
9908 * @sds: Load-balancing data with statistics of the local group.
9923 local_group = group == sds->local; in update_sg_lb_stats()
9925 for_each_cpu_and(i, sched_group_span(group), env->cpus) { in update_sg_lb_stats()
9929 sgs->group_load += load; in update_sg_lb_stats()
9930 sgs->group_util += cpu_util_cfs(i); in update_sg_lb_stats()
9931 sgs->group_runnable += cpu_runnable(rq); in update_sg_lb_stats()
9932 sgs->sum_h_nr_running += rq->cfs.h_nr_running; in update_sg_lb_stats()
9934 nr_running = rq->nr_running; in update_sg_lb_stats()
9935 sgs->sum_nr_running += nr_running; in update_sg_lb_stats()
9944 sgs->nr_numa_running += rq->nr_numa_running; in update_sg_lb_stats()
9945 sgs->nr_preferred_running += rq->nr_preferred_running; in update_sg_lb_stats()
9951 sgs->idle_cpus++; in update_sg_lb_stats()
9959 if (env->sd->flags & SD_ASYM_CPUCAPACITY) { in update_sg_lb_stats()
9961 if (sgs->group_misfit_task_load < rq->misfit_task_load) { in update_sg_lb_stats()
9962 sgs->group_misfit_task_load = rq->misfit_task_load; in update_sg_lb_stats()
9965 } else if ((env->idle != CPU_NOT_IDLE) && in update_sg_lb_stats()
9966 sched_reduced_capacity(rq, env->sd)) { in update_sg_lb_stats()
9968 if (sgs->group_misfit_task_load < load) in update_sg_lb_stats()
9969 sgs->group_misfit_task_load = load; in update_sg_lb_stats()
9973 sgs->group_capacity = group->sgc->capacity; in update_sg_lb_stats()
9975 sgs->group_weight = group->group_weight; in update_sg_lb_stats()
9978 if (!local_group && env->sd->flags & SD_ASYM_PACKING && in update_sg_lb_stats()
9979 env->idle != CPU_NOT_IDLE && sgs->sum_h_nr_running && in update_sg_lb_stats()
9981 sgs->group_asym_packing = 1; in update_sg_lb_stats()
9986 sgs->group_smt_balance = 1; in update_sg_lb_stats()
9988 sgs->group_type = group_classify(env->sd->imbalance_pct, group, sgs); in update_sg_lb_stats()
9991 if (sgs->group_type == group_overloaded) in update_sg_lb_stats()
9992 sgs->avg_load = (sgs->group_load * SCHED_CAPACITY_SCALE) / in update_sg_lb_stats()
9993 sgs->group_capacity; in update_sg_lb_stats()
9997 * update_sd_pick_busiest - return 1 on busiest group
10014 struct sg_lb_stats *busiest = &sds->busiest_stat; in update_sd_pick_busiest()
10017 if (!sgs->sum_h_nr_running) in update_sd_pick_busiest()
10026 if ((env->sd->flags & SD_ASYM_CPUCAPACITY) && in update_sd_pick_busiest()
10027 (sgs->group_type == group_misfit_task) && in update_sd_pick_busiest()
10028 (!capacity_greater(capacity_of(env->dst_cpu), sg->sgc->max_capacity) || in update_sd_pick_busiest()
10029 sds->local_stat.group_type != group_has_spare)) in update_sd_pick_busiest()
10032 if (sgs->group_type > busiest->group_type) in update_sd_pick_busiest()
10035 if (sgs->group_type < busiest->group_type) in update_sd_pick_busiest()
10043 switch (sgs->group_type) { in update_sd_pick_busiest()
10046 if (sgs->avg_load <= busiest->avg_load) in update_sd_pick_busiest()
10059 if (sched_asym_prefer(sg->asym_prefer_cpu, sds->busiest->asym_prefer_cpu)) in update_sd_pick_busiest()
10068 if (sgs->group_misfit_task_load < busiest->group_misfit_task_load) in update_sd_pick_busiest()
10077 if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) in update_sd_pick_busiest()
10095 if (sgs->avg_load < busiest->avg_load) in update_sd_pick_busiest()
10098 if (sgs->avg_load == busiest->avg_load) { in update_sd_pick_busiest()
10100 * SMT sched groups need more help than non-SMT groups. in update_sd_pick_busiest()
10103 if (sds->busiest->flags & SD_SHARE_CPUCAPACITY) in update_sd_pick_busiest()
10115 if (smt_vs_nonsmt_groups(sds->busiest, sg)) { in update_sd_pick_busiest()
10116 if (sg->flags & SD_SHARE_CPUCAPACITY && sgs->sum_h_nr_running <= 1) in update_sd_pick_busiest()
10130 if (sgs->idle_cpus > busiest->idle_cpus) in update_sd_pick_busiest()
10132 else if ((sgs->idle_cpus == busiest->idle_cpus) && in update_sd_pick_busiest()
10133 (sgs->sum_nr_running <= busiest->sum_nr_running)) in update_sd_pick_busiest()
10141 * per-CPU capacity. Migrating tasks to less capable CPUs may harm in update_sd_pick_busiest()
10145 if ((env->sd->flags & SD_ASYM_CPUCAPACITY) && in update_sd_pick_busiest()
10146 (sgs->group_type <= group_fully_busy) && in update_sd_pick_busiest()
10147 (capacity_greater(sg->sgc->min_capacity, capacity_of(env->dst_cpu)))) in update_sd_pick_busiest()
10156 if (sgs->sum_h_nr_running > sgs->nr_numa_running) in fbq_classify_group()
10158 if (sgs->sum_h_nr_running > sgs->nr_preferred_running) in fbq_classify_group()
10165 if (rq->nr_running > rq->nr_numa_running) in fbq_classify_rq()
10167 if (rq->nr_running > rq->nr_preferred_running) in fbq_classify_rq()
10187 * task_running_on_cpu - return 1 if @p is running on @cpu.
10193 if (cpu != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in task_running_on_cpu()
10203 * idle_cpu_without - would a given CPU be idle without p ?
10213 if (rq->curr != rq->idle && rq->curr != p) in idle_cpu_without()
10217 * rq->nr_running can't be used but an updated version without the in idle_cpu_without()
10223 if (rq->ttwu_pending) in idle_cpu_without()
10231 * update_sg_wakeup_stats - Update sched_group's statistics for wakeup.
10247 if (sd->flags & SD_ASYM_CPUCAPACITY) in update_sg_wakeup_stats()
10248 sgs->group_misfit_task_load = 1; in update_sg_wakeup_stats()
10254 sgs->group_load += cpu_load_without(rq, p); in update_sg_wakeup_stats()
10255 sgs->group_util += cpu_util_without(i, p); in update_sg_wakeup_stats()
10256 sgs->group_runnable += cpu_runnable_without(rq, p); in update_sg_wakeup_stats()
10258 sgs->sum_h_nr_running += rq->cfs.h_nr_running - local; in update_sg_wakeup_stats()
10260 nr_running = rq->nr_running - local; in update_sg_wakeup_stats()
10261 sgs->sum_nr_running += nr_running; in update_sg_wakeup_stats()
10267 sgs->idle_cpus++; in update_sg_wakeup_stats()
10270 if (sd->flags & SD_ASYM_CPUCAPACITY && in update_sg_wakeup_stats()
10271 sgs->group_misfit_task_load && in update_sg_wakeup_stats()
10273 sgs->group_misfit_task_load = 0; in update_sg_wakeup_stats()
10277 sgs->group_capacity = group->sgc->capacity; in update_sg_wakeup_stats()
10279 sgs->group_weight = group->group_weight; in update_sg_wakeup_stats()
10281 sgs->group_type = group_classify(sd->imbalance_pct, group, sgs); in update_sg_wakeup_stats()
10287 if (sgs->group_type == group_fully_busy || in update_sg_wakeup_stats()
10288 sgs->group_type == group_overloaded) in update_sg_wakeup_stats()
10289 sgs->avg_load = (sgs->group_load * SCHED_CAPACITY_SCALE) / in update_sg_wakeup_stats()
10290 sgs->group_capacity; in update_sg_wakeup_stats()
10298 if (sgs->group_type < idlest_sgs->group_type) in update_pick_idlest()
10301 if (sgs->group_type > idlest_sgs->group_type) in update_pick_idlest()
10309 switch (sgs->group_type) { in update_pick_idlest()
10313 if (idlest_sgs->avg_load <= sgs->avg_load) in update_pick_idlest()
10325 if (idlest->sgc->max_capacity >= group->sgc->max_capacity) in update_pick_idlest()
10331 if (idlest_sgs->idle_cpus > sgs->idle_cpus) in update_pick_idlest()
10335 if (idlest_sgs->idle_cpus == sgs->idle_cpus && in update_pick_idlest()
10336 idlest_sgs->group_util <= sgs->group_util) in update_pick_idlest()
10354 struct sched_group *idlest = NULL, *local = NULL, *group = sd->groups; in find_idlest_group()
10368 p->cpus_ptr)) in find_idlest_group()
10392 } while (group = group->next, group != sd->groups); in find_idlest_group()
10423 (sd->imbalance_pct-100) / 100; in find_idlest_group()
10430 * cross-domain, add imbalance to the load on the remote node in find_idlest_group()
10434 if ((sd->flags & SD_NUMA) && in find_idlest_group()
10445 if (100 * local_sgs.avg_load <= sd->imbalance_pct * idlest_sgs.avg_load) in find_idlest_group()
10457 if (local->sgc->max_capacity >= idlest->sgc->max_capacity) in find_idlest_group()
10463 if (sd->flags & SD_NUMA) { in find_idlest_group()
10464 int imb_numa_nr = sd->imb_numa_nr; in find_idlest_group()
10471 if (cpu_to_node(this_cpu) == p->numa_preferred_nid) in find_idlest_group()
10475 if (cpu_to_node(idlest_cpu) == p->numa_preferred_nid) in find_idlest_group()
10487 if (p->nr_cpus_allowed != NR_CPUS) { in find_idlest_group()
10490 cpumask_and(cpus, sched_group_span(local), p->cpus_ptr); in find_idlest_group()
10491 imb_numa_nr = min(cpumask_weight(cpus), sd->imb_numa_nr); in find_idlest_group()
10494 imbalance = abs(local_sgs.idle_cpus - idlest_sgs.idle_cpus); in find_idlest_group()
10531 if (!sched_feat(SIS_UTIL) || env->idle == CPU_NEWLY_IDLE) in update_idle_cpu_scan()
10534 llc_weight = per_cpu(sd_llc_size, env->dst_cpu); in update_idle_cpu_scan()
10535 if (env->sd->span_weight != llc_weight) in update_idle_cpu_scan()
10538 sd_share = rcu_dereference(per_cpu(sd_llc_shared, env->dst_cpu)); in update_idle_cpu_scan()
10548 * let y = SCHED_CAPACITY_SCALE - p * x^2 [1] in update_idle_cpu_scan()
10566 * y = SCHED_CAPACITY_SCALE - in update_idle_cpu_scan()
10575 pct = env->sd->imbalance_pct; in update_idle_cpu_scan()
10579 y = SCHED_CAPACITY_SCALE - tmp; in update_idle_cpu_scan()
10584 if ((int)y != sd_share->nr_idle_scan) in update_idle_cpu_scan()
10585 WRITE_ONCE(sd_share->nr_idle_scan, (int)y); in update_idle_cpu_scan()
10589 * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
10596 struct sched_group *sg = env->sd->groups; in update_sd_lb_stats()
10597 struct sg_lb_stats *local = &sds->local_stat; in update_sd_lb_stats()
10606 local_group = cpumask_test_cpu(env->dst_cpu, sched_group_span(sg)); in update_sd_lb_stats()
10608 sds->local = sg; in update_sd_lb_stats()
10611 if (env->idle != CPU_NEWLY_IDLE || in update_sd_lb_stats()
10612 time_after_eq(jiffies, sg->sgc->next_update)) in update_sd_lb_stats()
10613 update_group_capacity(env->sd, env->dst_cpu); in update_sd_lb_stats()
10623 sds->busiest = sg; in update_sd_lb_stats()
10624 sds->busiest_stat = *sgs; in update_sd_lb_stats()
10629 sds->total_load += sgs->group_load; in update_sd_lb_stats()
10630 sds->total_capacity += sgs->group_capacity; in update_sd_lb_stats()
10632 sum_util += sgs->group_util; in update_sd_lb_stats()
10633 sg = sg->next; in update_sd_lb_stats()
10634 } while (sg != env->sd->groups); in update_sd_lb_stats()
10641 if (sds->busiest) in update_sd_lb_stats()
10642 sds->prefer_sibling = !!(sds->busiest->flags & SD_PREFER_SIBLING); in update_sd_lb_stats()
10645 if (env->sd->flags & SD_NUMA) in update_sd_lb_stats()
10646 env->fbq_type = fbq_classify_group(&sds->busiest_stat); in update_sd_lb_stats()
10648 if (!env->sd->parent) { in update_sd_lb_stats()
10650 WRITE_ONCE(env->dst_rq->rd->overload, sg_status & SG_OVERLOAD); in update_sd_lb_stats()
10652 /* Update over-utilization (tipping point, U >= 0) indicator */ in update_sd_lb_stats()
10653 set_rd_overutilized_status(env->dst_rq->rd, in update_sd_lb_stats()
10656 set_rd_overutilized_status(env->dst_rq->rd, SG_OVERUTILIZED); in update_sd_lb_stats()
10663 * calculate_imbalance - Calculate the amount of imbalance present within the
10672 local = &sds->local_stat; in calculate_imbalance()
10673 busiest = &sds->busiest_stat; in calculate_imbalance()
10675 if (busiest->group_type == group_misfit_task) { in calculate_imbalance()
10676 if (env->sd->flags & SD_ASYM_CPUCAPACITY) { in calculate_imbalance()
10678 env->migration_type = migrate_misfit; in calculate_imbalance()
10679 env->imbalance = 1; in calculate_imbalance()
10685 env->migration_type = migrate_load; in calculate_imbalance()
10686 env->imbalance = busiest->group_misfit_task_load; in calculate_imbalance()
10691 if (busiest->group_type == group_asym_packing) { in calculate_imbalance()
10696 env->migration_type = migrate_task; in calculate_imbalance()
10697 env->imbalance = busiest->sum_h_nr_running; in calculate_imbalance()
10701 if (busiest->group_type == group_smt_balance) { in calculate_imbalance()
10703 env->migration_type = migrate_task; in calculate_imbalance()
10704 env->imbalance = 1; in calculate_imbalance()
10708 if (busiest->group_type == group_imbalanced) { in calculate_imbalance()
10710 * In the group_imb case we cannot rely on group-wide averages in calculate_imbalance()
10711 * to ensure CPU-load equilibrium, try to move any task to fix in calculate_imbalance()
10715 env->migration_type = migrate_task; in calculate_imbalance()
10716 env->imbalance = 1; in calculate_imbalance()
10724 if (local->group_type == group_has_spare) { in calculate_imbalance()
10725 if ((busiest->group_type > group_fully_busy) && in calculate_imbalance()
10726 !(env->sd->flags & SD_SHARE_PKG_RESOURCES)) { in calculate_imbalance()
10735 env->migration_type = migrate_util; in calculate_imbalance()
10736 env->imbalance = max(local->group_capacity, local->group_util) - in calculate_imbalance()
10737 local->group_util; in calculate_imbalance()
10746 if (env->idle != CPU_NOT_IDLE && env->imbalance == 0) { in calculate_imbalance()
10747 env->migration_type = migrate_task; in calculate_imbalance()
10748 env->imbalance = 1; in calculate_imbalance()
10754 if (busiest->group_weight == 1 || sds->prefer_sibling) { in calculate_imbalance()
10759 env->migration_type = migrate_task; in calculate_imbalance()
10760 env->imbalance = sibling_imbalance(env, sds, busiest, local); in calculate_imbalance()
10767 env->migration_type = migrate_task; in calculate_imbalance()
10768 env->imbalance = max_t(long, 0, in calculate_imbalance()
10769 (local->idle_cpus - busiest->idle_cpus)); in calculate_imbalance()
10774 if (env->sd->flags & SD_NUMA) { in calculate_imbalance()
10775 env->imbalance = adjust_numa_imbalance(env->imbalance, in calculate_imbalance()
10776 local->sum_nr_running + 1, in calculate_imbalance()
10777 env->sd->imb_numa_nr); in calculate_imbalance()
10782 env->imbalance >>= 1; in calculate_imbalance()
10791 if (local->group_type < group_overloaded) { in calculate_imbalance()
10797 local->avg_load = (local->group_load * SCHED_CAPACITY_SCALE) / in calculate_imbalance()
10798 local->group_capacity; in calculate_imbalance()
10804 if (local->avg_load >= busiest->avg_load) { in calculate_imbalance()
10805 env->imbalance = 0; in calculate_imbalance()
10809 sds->avg_load = (sds->total_load * SCHED_CAPACITY_SCALE) / in calculate_imbalance()
10810 sds->total_capacity; in calculate_imbalance()
10816 if (local->avg_load >= sds->avg_load) { in calculate_imbalance()
10817 env->imbalance = 0; in calculate_imbalance()
10831 env->migration_type = migrate_load; in calculate_imbalance()
10832 env->imbalance = min( in calculate_imbalance()
10833 (busiest->avg_load - sds->avg_load) * busiest->group_capacity, in calculate_imbalance()
10834 (sds->avg_load - local->avg_load) * local->group_capacity in calculate_imbalance()
10861 * find_busiest_group - Returns the busiest group within the sched_domain
10868 * Return: - The busiest group if imbalance exists.
10890 if (busiest->group_type == group_misfit_task) in find_busiest_group()
10894 struct root_domain *rd = env->dst_rq->rd; in find_busiest_group()
10896 if (rcu_dereference(rd->pd) && !READ_ONCE(rd->overutilized)) in find_busiest_group()
10901 if (busiest->group_type == group_asym_packing) in find_busiest_group()
10909 if (busiest->group_type == group_imbalanced) in find_busiest_group()
10917 if (local->group_type > busiest->group_type) in find_busiest_group()
10924 if (local->group_type == group_overloaded) { in find_busiest_group()
10929 if (local->avg_load >= busiest->avg_load) in find_busiest_group()
10940 if (local->avg_load >= sds.avg_load) in find_busiest_group()
10947 if (100 * busiest->avg_load <= in find_busiest_group()
10948 env->sd->imbalance_pct * local->avg_load) in find_busiest_group()
10956 if (sds.prefer_sibling && local->group_type == group_has_spare && in find_busiest_group()
10960 if (busiest->group_type != group_overloaded) { in find_busiest_group()
10961 if (env->idle == CPU_NOT_IDLE) { in find_busiest_group()
10970 if (busiest->group_type == group_smt_balance && in find_busiest_group()
10976 if (busiest->group_weight > 1 && in find_busiest_group()
10977 local->idle_cpus <= (busiest->idle_cpus + 1)) { in find_busiest_group()
10990 if (busiest->sum_h_nr_running == 1) { in find_busiest_group()
11001 return env->imbalance ? sds.busiest : NULL; in find_busiest_group()
11004 env->imbalance = 0; in find_busiest_group()
11009 * find_busiest_queue - find the busiest runqueue among the CPUs in the group.
11019 for_each_cpu_and(i, sched_group_span(group), env->cpus) { in find_busiest_queue()
11029 * - regular: there are !numa tasks in find_busiest_queue()
11030 * - remote: there are numa tasks that run on the 'wrong' node in find_busiest_queue()
11031 * - all: there is no distinction in find_busiest_queue()
11046 if (rt > env->fbq_type) in find_busiest_queue()
11049 nr_running = rq->cfs.h_nr_running; in find_busiest_queue()
11057 * eventually lead to active_balancing high->low capacity. in find_busiest_queue()
11058 * Higher per-CPU capacity is considered better than balancing in find_busiest_queue()
11061 if (env->sd->flags & SD_ASYM_CPUCAPACITY && in find_busiest_queue()
11062 !capacity_greater(capacity_of(env->dst_cpu), capacity) && in find_busiest_queue()
11073 if ((env->sd->flags & SD_ASYM_PACKING) && in find_busiest_queue()
11074 sched_use_asym_prio(env->sd, i) && in find_busiest_queue()
11075 sched_asym_prefer(i, env->dst_cpu) && in find_busiest_queue()
11079 switch (env->migration_type) { in find_busiest_queue()
11087 if (nr_running == 1 && load > env->imbalance && in find_busiest_queue()
11088 !check_cpu_capacity(rq, env->sd)) in find_busiest_queue()
11140 if (rq->misfit_task_load > busiest_load) { in find_busiest_queue()
11141 busiest_load = rq->misfit_task_load; in find_busiest_queue()
11172 return env->idle != CPU_NOT_IDLE && (env->sd->flags & SD_ASYM_PACKING) && in asym_active_balance()
11173 sched_use_asym_prio(env->sd, env->dst_cpu) && in asym_active_balance()
11174 (sched_asym_prefer(env->dst_cpu, env->src_cpu) || in asym_active_balance()
11175 !sched_use_asym_prio(env->sd, env->src_cpu)); in asym_active_balance()
11181 struct sched_domain *sd = env->sd; in imbalanced_active_balance()
11188 if ((env->migration_type == migrate_task) && in imbalanced_active_balance()
11189 (sd->nr_balance_failed > sd->cache_nice_tries+2)) in imbalanced_active_balance()
11197 struct sched_domain *sd = env->sd; in need_active_balance()
11211 if ((env->idle != CPU_NOT_IDLE) && in need_active_balance()
11212 (env->src_rq->cfs.h_nr_running == 1)) { in need_active_balance()
11213 if ((check_cpu_capacity(env->src_rq, sd)) && in need_active_balance()
11214 (capacity_of(env->src_cpu)*sd->imbalance_pct < capacity_of(env->dst_cpu)*100)) in need_active_balance()
11218 if (env->migration_type == migrate_misfit) in need_active_balance()
11229 struct sched_group *sg = env->sd->groups; in should_we_balance()
11230 int cpu, idle_smt = -1; in should_we_balance()
11236 if (!cpumask_test_cpu(env->dst_cpu, env->cpus)) in should_we_balance()
11244 * to optimize wakeup latency. in should_we_balance()
11246 if (env->idle == CPU_NEWLY_IDLE) { in should_we_balance()
11247 if (env->dst_rq->nr_running > 0 || env->dst_rq->ttwu_pending) in should_we_balance()
11254 for_each_cpu_and(cpu, swb_cpus, env->cpus) { in should_we_balance()
11263 if (!(env->sd->flags & SD_SHARE_CPUCAPACITY) && !is_core_idle(cpu)) { in should_we_balance()
11264 if (idle_smt == -1) in should_we_balance()
11278 * Are we the first idle core in a non-SMT domain or higher, in should_we_balance()
11281 return cpu == env->dst_cpu; in should_we_balance()
11285 if (idle_smt != -1) in should_we_balance()
11286 return idle_smt == env->dst_cpu; in should_we_balance()
11289 return group_balance_cpu(sg) == env->dst_cpu; in should_we_balance()
11301 struct sched_domain *sd_parent = sd->parent; in load_balance()
11310 .dst_grpmask = group_balance_mask(sd->groups), in load_balance()
11320 schedstat_inc(sd->lb_count[idle]); in load_balance()
11330 schedstat_inc(sd->lb_nobusyg[idle]); in load_balance()
11336 schedstat_inc(sd->lb_nobusyq[idle]); in load_balance()
11342 schedstat_add(sd->lb_imbalance[idle], env.imbalance); in load_balance()
11344 env.src_cpu = busiest->cpu; in load_balance()
11350 if (busiest->nr_running > 1) { in load_balance()
11353 * an imbalance but busiest->nr_running <= 1, the group is in load_balance()
11357 env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running); in load_balance()
11364 * cur_ld_moved - load moved in current iteration in load_balance()
11365 * ld_moved - cumulative load moved across iterations in load_balance()
11372 * unlock busiest->lock, and we are able to be sure in load_balance()
11401 * nohz-idle), we now have balance_cpu in a position to move in load_balance()
11412 /* Prevent to re-select dst_cpu via env's CPUs */ in load_balance()
11432 int *group_imbalance = &sd_parent->groups->sgc->imbalance; in load_balance()
11459 schedstat_inc(sd->lb_failed[idle]); in load_balance()
11467 sd->nr_balance_failed++; in load_balance()
11479 if (!cpumask_test_cpu(this_cpu, busiest->curr->cpus_ptr)) { in load_balance()
11488 * ->active_balance synchronizes accesses to in load_balance()
11489 * ->active_balance_work. Once set, it's cleared in load_balance()
11492 if (!busiest->active_balance) { in load_balance()
11493 busiest->active_balance = 1; in load_balance()
11494 busiest->push_cpu = this_cpu; in load_balance()
11503 &busiest->active_balance_work); in load_balance()
11508 sd->nr_balance_failed = 0; in load_balance()
11513 sd->balance_interval = sd->min_interval; in load_balance()
11525 int *group_imbalance = &sd_parent->groups->sgc->imbalance; in load_balance()
11537 schedstat_inc(sd->lb_balanced[idle]); in load_balance()
11539 sd->nr_balance_failed = 0; in load_balance()
11555 sd->balance_interval < MAX_PINNED_INTERVAL) || in load_balance()
11556 sd->balance_interval < sd->max_interval) in load_balance()
11557 sd->balance_interval *= 2; in load_balance()
11565 unsigned long interval = sd->balance_interval; in get_sd_balance_interval()
11568 interval *= sd->busy_factor; in get_sd_balance_interval()
11579 interval -= 1; in get_sd_balance_interval()
11593 next = sd->last_balance + interval; in update_next_balance()
11609 int target_cpu = busiest_rq->push_cpu; in active_load_balance_cpu_stop()
11617 * Between queueing the stop-work and running it is a hole in which in active_load_balance_cpu_stop()
11626 !busiest_rq->active_balance)) in active_load_balance_cpu_stop()
11630 if (busiest_rq->nr_running <= 1) in active_load_balance_cpu_stop()
11636 * Bjorn Helgaas on a 128-CPU setup. in active_load_balance_cpu_stop()
11652 .src_cpu = busiest_rq->cpu, in active_load_balance_cpu_stop()
11658 schedstat_inc(sd->alb_count); in active_load_balance_cpu_stop()
11663 schedstat_inc(sd->alb_pushed); in active_load_balance_cpu_stop()
11665 sd->nr_balance_failed = 0; in active_load_balance_cpu_stop()
11667 schedstat_inc(sd->alb_failed); in active_load_balance_cpu_stop()
11672 busiest_rq->active_balance = 0; in active_load_balance_cpu_stop()
11687 * This trades load-balance latency on larger machines for less cross talk.
11696 if (cost > sd->max_newidle_lb_cost) { in update_newidle_cost()
11701 sd->max_newidle_lb_cost = cost; in update_newidle_cost()
11702 sd->last_decay_max_lb_cost = jiffies; in update_newidle_cost()
11703 } else if (time_after(jiffies, sd->last_decay_max_lb_cost + HZ)) { in update_newidle_cost()
11709 sd->max_newidle_lb_cost = (sd->max_newidle_lb_cost * 253) / 256; in update_newidle_cost()
11710 sd->last_decay_max_lb_cost = jiffies; in update_newidle_cost()
11727 int cpu = rq->cpu; in rebalance_domains()
11744 max_cost += sd->max_newidle_lb_cost; in rebalance_domains()
11759 need_serialize = sd->flags & SD_SERIALIZE; in rebalance_domains()
11765 if (time_after_eq(jiffies, sd->last_balance + interval)) { in rebalance_domains()
11769 * env->dst_cpu, so we can't know our idle in rebalance_domains()
11775 sd->last_balance = jiffies; in rebalance_domains()
11781 if (time_after(next_balance, sd->last_balance + interval)) { in rebalance_domains()
11782 next_balance = sd->last_balance + interval; in rebalance_domains()
11788 * Ensure the rq-wide value also decays but keep it at a in rebalance_domains()
11789 * reasonable floor to avoid funnies with rq->avg_idle. in rebalance_domains()
11791 rq->max_idle_balance_cost = in rebalance_domains()
11802 rq->next_balance = next_balance; in rebalance_domains()
11808 return unlikely(!rcu_dereference_sched(rq->sd)); in on_null_domain()
11814 * - When one of the busy CPUs notice that there may be an idle rebalancing
11817 * - HK_TYPE_MISC CPUs are used for this task, because HK_TYPE_SCHED not set
11873 smp_call_function_single_async(ilb_cpu, &cpu_rq(ilb_cpu)->nohz_csd); in kick_ilb()
11885 int nr_busy, i, cpu = rq->cpu; in nohz_balancer_kick()
11888 if (unlikely(rq->idle_balance)) in nohz_balancer_kick()
11911 if (rq->nr_running >= 2) { in nohz_balancer_kick()
11918 sd = rcu_dereference(rq->sd); in nohz_balancer_kick()
11925 if (rq->cfs.h_nr_running >= 1 && check_cpu_capacity(rq, sd)) { in nohz_balancer_kick()
11975 * increase the overall cache use), we need some less-loaded LLC in nohz_balancer_kick()
11979 * the others are - so just get a nohz balance going if it looks in nohz_balancer_kick()
11982 nr_busy = atomic_read(&sds->nr_busy_cpus); in nohz_balancer_kick()
12005 if (!sd || !sd->nohz_idle) in set_cpu_sd_state_busy()
12007 sd->nohz_idle = 0; in set_cpu_sd_state_busy()
12009 atomic_inc(&sd->shared->nr_busy_cpus); in set_cpu_sd_state_busy()
12018 if (likely(!rq->nohz_tick_stopped)) in nohz_balance_exit_idle()
12021 rq->nohz_tick_stopped = 0; in nohz_balance_exit_idle()
12022 cpumask_clear_cpu(rq->cpu, nohz.idle_cpus_mask); in nohz_balance_exit_idle()
12025 set_cpu_sd_state_busy(rq->cpu); in nohz_balance_exit_idle()
12035 if (!sd || sd->nohz_idle) in set_cpu_sd_state_idle()
12037 sd->nohz_idle = 1; in set_cpu_sd_state_idle()
12039 atomic_dec(&sd->shared->nr_busy_cpus); in set_cpu_sd_state_idle()
12063 * Can be set safely without rq->lock held in nohz_balance_enter_idle()
12065 * rq->lock is held during the check and the clear in nohz_balance_enter_idle()
12067 rq->has_blocked_load = 1; in nohz_balance_enter_idle()
12075 if (rq->nohz_tick_stopped) in nohz_balance_enter_idle()
12082 rq->nohz_tick_stopped = 1; in nohz_balance_enter_idle()
12107 unsigned int cpu = rq->cpu; in update_nohz_stats()
12109 if (!rq->has_blocked_load) in update_nohz_stats()
12115 if (!time_after(jiffies, READ_ONCE(rq->last_blocked_load_update_tick))) in update_nohz_stats()
12120 return rq->has_blocked_load; in update_nohz_stats()
12135 int this_cpu = this_rq->cpu; in _nohz_idle_balance()
12192 if (time_after_eq(jiffies, rq->next_balance)) { in _nohz_idle_balance()
12203 if (time_after(next_balance, rq->next_balance)) { in _nohz_idle_balance()
12204 next_balance = rq->next_balance; in _nohz_idle_balance()
12233 unsigned int flags = this_rq->nohz_idle_balance; in nohz_idle_balance()
12238 this_rq->nohz_idle_balance = 0; in nohz_idle_balance()
12268 int this_cpu = this_rq->cpu; in nohz_newidle_balance()
12278 if (this_rq->avg_idle < sysctl_sched_migration_cost) in nohz_newidle_balance()
12309 * < 0 - we released the lock and there are !fair tasks present
12310 * 0 - failed, no new tasks
12311 * > 0 - success, new (fair) tasks present
12316 int this_cpu = this_rq->cpu; in newidle_balance()
12327 if (this_rq->ttwu_pending) in newidle_balance()
12334 this_rq->idle_stamp = rq_clock(this_rq); in newidle_balance()
12344 * for load-balance and preemption/IRQs are still disabled avoiding in newidle_balance()
12346 * re-start the picking loop. in newidle_balance()
12351 sd = rcu_dereference_check_sched_domain(this_rq->sd); in newidle_balance()
12353 if (!READ_ONCE(this_rq->rd->overload) || in newidle_balance()
12354 (sd && this_rq->avg_idle < sd->max_newidle_lb_cost)) { in newidle_balance()
12376 if (this_rq->avg_idle < curr_cost + sd->max_newidle_lb_cost) in newidle_balance()
12379 if (sd->flags & SD_BALANCE_NEWIDLE) { in newidle_balance()
12386 domain_cost = t1 - t0; in newidle_balance()
12397 if (pulled_task || this_rq->nr_running > 0 || in newidle_balance()
12398 this_rq->ttwu_pending) in newidle_balance()
12405 if (curr_cost > this_rq->max_idle_balance_cost) in newidle_balance()
12406 this_rq->max_idle_balance_cost = curr_cost; in newidle_balance()
12409 * While browsing the domains, we released the rq lock, a task could in newidle_balance()
12413 if (this_rq->cfs.h_nr_running && !pulled_task) in newidle_balance()
12417 if (this_rq->nr_running != this_rq->cfs.h_nr_running) in newidle_balance()
12418 pulled_task = -1; in newidle_balance()
12422 if (time_after(this_rq->next_balance, next_balance)) in newidle_balance()
12423 this_rq->next_balance = next_balance; in newidle_balance()
12426 this_rq->idle_stamp = 0; in newidle_balance()
12442 enum cpu_idle_type idle = this_rq->idle_balance ? in run_rebalance_domains()
12457 update_blocked_averages(this_rq->cpu); in run_rebalance_domains()
12473 if (time_after_eq(jiffies, rq->next_balance)) in trigger_load_balance()
12500 u64 rtime = se->sum_exec_runtime - se->prev_sum_exec_runtime; in __entity_slice_used()
12501 u64 slice = se->slice; in __entity_slice_used()
12523 * MIN_NR_TASKS_DURING_FORCEIDLE - 1 tasks and use that to check in task_tick_core()
12526 if (rq->core->core_forceidle_count && rq->cfs.nr_running == 1 && in task_tick_core()
12527 __entity_slice_used(&curr->se, MIN_NR_TASKS_DURING_FORCEIDLE)) in task_tick_core()
12532 * se_fi_update - Update the cfs_rq->min_vruntime_fi in a CFS hierarchy if needed.
12541 if (cfs_rq->forceidle_seq == fi_seq) in se_fi_update()
12543 cfs_rq->forceidle_seq = fi_seq; in se_fi_update()
12546 cfs_rq->min_vruntime_fi = cfs_rq->min_vruntime; in se_fi_update()
12552 struct sched_entity *se = &p->se; in task_vruntime_update()
12554 if (p->sched_class != &fair_sched_class) in task_vruntime_update()
12557 se_fi_update(se, rq->core->core_forceidle_seq, in_fi); in task_vruntime_update()
12564 const struct sched_entity *sea = &a->se; in cfs_prio_less()
12565 const struct sched_entity *seb = &b->se; in cfs_prio_less()
12570 SCHED_WARN_ON(task_rq(b)->core != rq->core); in cfs_prio_less()
12577 while (sea->cfs_rq->tg != seb->cfs_rq->tg) { in cfs_prio_less()
12578 int sea_depth = sea->depth; in cfs_prio_less()
12579 int seb_depth = seb->depth; in cfs_prio_less()
12587 se_fi_update(sea, rq->core->core_forceidle_seq, in_fi); in cfs_prio_less()
12588 se_fi_update(seb, rq->core->core_forceidle_seq, in_fi); in cfs_prio_less()
12590 cfs_rqa = sea->cfs_rq; in cfs_prio_less()
12591 cfs_rqb = seb->cfs_rq; in cfs_prio_less()
12593 cfs_rqa = &task_rq(a)->cfs; in cfs_prio_less()
12594 cfs_rqb = &task_rq(b)->cfs; in cfs_prio_less()
12602 delta = (s64)(sea->vruntime - seb->vruntime) + in cfs_prio_less()
12603 (s64)(cfs_rqb->min_vruntime_fi - cfs_rqa->min_vruntime_fi); in cfs_prio_less()
12613 cfs_rq = task_group(p)->cfs_rq[cpu]; in task_is_throttled_fair()
12615 cfs_rq = &cpu_rq(cpu)->cfs; in task_is_throttled_fair()
12634 struct sched_entity *se = &curr->se; in task_tick_fair()
12652 * - child not yet on the tasklist
12653 * - preemption disabled
12657 struct sched_entity *se = &p->se, *curr; in task_fork_fair()
12666 curr = cfs_rq->curr; in task_fork_fair()
12683 if (rq->cfs.nr_running == 1) in prio_changed_fair()
12692 if (p->prio > oldprio) in prio_changed_fair()
12714 se = se->parent; in propagate_entity_cfs_rq()
12739 * - A forked task which hasn't been woken up by wake_up_new_task(). in detach_entity_cfs_rq()
12740 * - A task which has been woken up by try_to_wake_up() but is in detach_entity_cfs_rq()
12743 if (!se->avg.last_update_time) in detach_entity_cfs_rq()
12767 struct sched_entity *se = &p->se; in detach_task_cfs_rq()
12774 struct sched_entity *se = &p->se; in attach_task_cfs_rq()
12803 * This routine is mostly called to set cfs_rq->curr field when a task
12808 struct sched_entity *se = &p->se; in set_next_task_fair()
12816 list_move(&se->group_node, &rq->cfs_tasks); in set_next_task_fair()
12831 cfs_rq->tasks_timeline = RB_ROOT_CACHED; in init_cfs_rq()
12832 u64_u32_store(cfs_rq->min_vruntime, (u64)(-(1LL << 20))); in init_cfs_rq()
12834 raw_spin_lock_init(&cfs_rq->removed.lock); in init_cfs_rq()
12845 if (READ_ONCE(p->__state) == TASK_NEW) in task_change_group_fair()
12851 /* Tell se's cfs_rq has been changed -- migrated */ in task_change_group_fair()
12852 p->se.avg.last_update_time = 0; in task_change_group_fair()
12863 if (tg->cfs_rq) in free_fair_sched_group()
12864 kfree(tg->cfs_rq[i]); in free_fair_sched_group()
12865 if (tg->se) in free_fair_sched_group()
12866 kfree(tg->se[i]); in free_fair_sched_group()
12869 kfree(tg->cfs_rq); in free_fair_sched_group()
12870 kfree(tg->se); in free_fair_sched_group()
12879 tg->cfs_rq = kcalloc(nr_cpu_ids, sizeof(cfs_rq), GFP_KERNEL); in alloc_fair_sched_group()
12880 if (!tg->cfs_rq) in alloc_fair_sched_group()
12882 tg->se = kcalloc(nr_cpu_ids, sizeof(se), GFP_KERNEL); in alloc_fair_sched_group()
12883 if (!tg->se) in alloc_fair_sched_group()
12886 tg->shares = NICE_0_LOAD; in alloc_fair_sched_group()
12902 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]); in alloc_fair_sched_group()
12923 se = tg->se[i]; in online_fair_sched_group()
12941 if (tg->se[cpu]) in unregister_fair_sched_group()
12942 remove_entity_load_avg(tg->se[cpu]); in unregister_fair_sched_group()
12946 * check on_list without danger of it being re-added. in unregister_fair_sched_group()
12948 if (!tg->cfs_rq[cpu]->on_list) in unregister_fair_sched_group()
12954 list_del_leaf_cfs_rq(tg->cfs_rq[cpu]); in unregister_fair_sched_group()
12965 cfs_rq->tg = tg; in init_tg_cfs_entry()
12966 cfs_rq->rq = rq; in init_tg_cfs_entry()
12969 tg->cfs_rq[cpu] = cfs_rq; in init_tg_cfs_entry()
12970 tg->se[cpu] = se; in init_tg_cfs_entry()
12977 se->cfs_rq = &rq->cfs; in init_tg_cfs_entry()
12978 se->depth = 0; in init_tg_cfs_entry()
12980 se->cfs_rq = parent->my_q; in init_tg_cfs_entry()
12981 se->depth = parent->depth + 1; in init_tg_cfs_entry()
12984 se->my_q = cfs_rq; in init_tg_cfs_entry()
12986 update_load_set(&se->load, NICE_0_LOAD); in init_tg_cfs_entry()
12987 se->parent = parent; in init_tg_cfs_entry()
13001 if (!tg->se[0]) in __sched_group_set_shares()
13002 return -EINVAL; in __sched_group_set_shares()
13006 if (tg->shares == shares) in __sched_group_set_shares()
13009 tg->shares = shares; in __sched_group_set_shares()
13012 struct sched_entity *se = tg->se[i]; in __sched_group_set_shares()
13034 ret = -EINVAL; in sched_group_set_shares()
13047 return -EINVAL; in sched_group_set_idle()
13050 return -EINVAL; in sched_group_set_idle()
13054 if (tg->idle == idle) { in sched_group_set_idle()
13059 tg->idle = idle; in sched_group_set_idle()
13063 struct sched_entity *se = tg->se[i]; in sched_group_set_idle()
13064 struct cfs_rq *parent_cfs_rq, *grp_cfs_rq = tg->cfs_rq[i]; in sched_group_set_idle()
13071 grp_cfs_rq->idle = idle; in sched_group_set_idle()
13075 if (se->on_rq) { in sched_group_set_idle()
13078 parent_cfs_rq->idle_nr_running++; in sched_group_set_idle()
13080 parent_cfs_rq->idle_nr_running--; in sched_group_set_idle()
13083 idle_task_delta = grp_cfs_rq->h_nr_running - in sched_group_set_idle()
13084 grp_cfs_rq->idle_h_nr_running; in sched_group_set_idle()
13086 idle_task_delta *= -1; in sched_group_set_idle()
13091 if (!se->on_rq) in sched_group_set_idle()
13094 cfs_rq->idle_h_nr_running += idle_task_delta; in sched_group_set_idle()
13133 struct sched_entity *se = &task->se; in get_rr_interval_fair()
13140 if (rq->cfs.load.weight) in get_rr_interval_fair()
13141 rr_interval = NS_TO_JIFFIES(se->slice); in get_rr_interval_fair()
13218 ng = rcu_dereference(p->numa_group); in show_numa_stats()
13220 if (p->numa_faults) { in show_numa_stats()
13221 tsf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 0)]; in show_numa_stats()
13222 tpf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 1)]; in show_numa_stats()
13225 gsf = ng->faults[task_faults_idx(NUMA_MEM, node, 0)], in show_numa_stats()
13226 gpf = ng->faults[task_faults_idx(NUMA_MEM, node, 1)]; in show_numa_stats()
13247 INIT_CSD(&cpu_rq(i)->cfsb_csd, __cfsb_csd_unthrottle, cpu_rq(i)); in init_sched_fair_class()
13248 INIT_LIST_HEAD(&cpu_rq(i)->cfsb_csd_list); in init_sched_fair_class()