Lines Matching +full:overrun +full:- +full:throttle +full:- +full:ms
1 // SPDX-License-Identifier: GPL-2.0
43 #include <linux/memory-tiers.h>
59 * The initial- and re-scaling of tunables is configurable
63 * SCHED_TUNABLESCALING_NONE - unscaled, always *1
64 * SCHED_TUNABLESCALING_LOG - scaled logarithmically, *1+ilog(ncpus)
65 * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus
72 * Minimal preemption granularity for CPU-bound tasks:
94 return -cpu; in arch_asym_cpu_priority()
115 * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool
166 lw->weight += inc; in update_load_add()
167 lw->inv_weight = 0; in update_load_add()
172 lw->weight -= dec; in update_load_sub()
173 lw->inv_weight = 0; in update_load_sub()
178 lw->weight = w; in update_load_set()
179 lw->inv_weight = 0; in update_load_set()
186 * so pick a second-best guess by going with the log2 of the
234 if (likely(lw->inv_weight)) in __update_inv_weight()
237 w = scale_load_down(lw->weight); in __update_inv_weight()
240 lw->inv_weight = 1; in __update_inv_weight()
242 lw->inv_weight = WMULT_CONST; in __update_inv_weight()
244 lw->inv_weight = WMULT_CONST / w; in __update_inv_weight()
250 * (delta_exec * (weight * lw->inv_weight)) >> WMULT_SHIFT
270 shift -= fs; in __calc_delta()
274 fact = mul_u32_u32(fact, lw->inv_weight); in __calc_delta()
279 shift -= fs; in __calc_delta()
291 if (unlikely(se->load.weight != NICE_0_LOAD)) in calc_delta_fair()
292 delta = __calc_delta(delta, NICE_0_LOAD, &se->load); in calc_delta_fair()
307 for (; se; se = se->parent)
314 if (cfs_rq->on_list) in list_add_leaf_cfs_rq()
315 return rq->tmp_alone_branch == &rq->leaf_cfs_rq_list; in list_add_leaf_cfs_rq()
317 cfs_rq->on_list = 1; in list_add_leaf_cfs_rq()
322 * enqueued. The fact that we always enqueue bottom-up in list_add_leaf_cfs_rq()
328 if (cfs_rq->tg->parent && in list_add_leaf_cfs_rq()
329 cfs_rq->tg->parent->cfs_rq[cpu]->on_list) { in list_add_leaf_cfs_rq()
336 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list, in list_add_leaf_cfs_rq()
337 &(cfs_rq->tg->parent->cfs_rq[cpu]->leaf_cfs_rq_list)); in list_add_leaf_cfs_rq()
343 rq->tmp_alone_branch = &rq->leaf_cfs_rq_list; in list_add_leaf_cfs_rq()
347 if (!cfs_rq->tg->parent) { in list_add_leaf_cfs_rq()
352 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list, in list_add_leaf_cfs_rq()
353 &rq->leaf_cfs_rq_list); in list_add_leaf_cfs_rq()
358 rq->tmp_alone_branch = &rq->leaf_cfs_rq_list; in list_add_leaf_cfs_rq()
368 list_add_rcu(&cfs_rq->leaf_cfs_rq_list, rq->tmp_alone_branch); in list_add_leaf_cfs_rq()
373 rq->tmp_alone_branch = &cfs_rq->leaf_cfs_rq_list; in list_add_leaf_cfs_rq()
379 if (cfs_rq->on_list) { in list_del_leaf_cfs_rq()
386 * to the prev element but it will point to rq->leaf_cfs_rq_list in list_del_leaf_cfs_rq()
389 if (rq->tmp_alone_branch == &cfs_rq->leaf_cfs_rq_list) in list_del_leaf_cfs_rq()
390 rq->tmp_alone_branch = cfs_rq->leaf_cfs_rq_list.prev; in list_del_leaf_cfs_rq()
392 list_del_rcu(&cfs_rq->leaf_cfs_rq_list); in list_del_leaf_cfs_rq()
393 cfs_rq->on_list = 0; in list_del_leaf_cfs_rq()
399 SCHED_WARN_ON(rq->tmp_alone_branch != &rq->leaf_cfs_rq_list); in assert_list_leaf_cfs_rq()
404 list_for_each_entry_safe(cfs_rq, pos, &rq->leaf_cfs_rq_list, \
411 if (se->cfs_rq == pse->cfs_rq) in is_same_group()
412 return se->cfs_rq; in is_same_group()
419 return se->parent; in parent_entity()
435 se_depth = (*se)->depth; in find_matching_se()
436 pse_depth = (*pse)->depth; in find_matching_se()
439 se_depth--; in find_matching_se()
444 pse_depth--; in find_matching_se()
456 return tg->idle > 0; in tg_is_idle()
461 return cfs_rq->idle > 0; in cfs_rq_is_idle()
490 for (cfs_rq = &rq->cfs, pos = NULL; cfs_rq; cfs_rq = pos)
528 s64 delta = (s64)(vruntime - max_vruntime); in max_vruntime()
537 s64 delta = (s64)(vruntime - min_vruntime); in min_vruntime()
551 return (s64)(a->deadline - b->deadline) < 0; in entity_before()
556 return (s64)(se->vruntime - cfs_rq->min_vruntime); in entity_key()
563 * Compute virtual time from the per-task service numbers:
571 * lag_i = S - s_i = w_i * (V - v_i)
577 * \Sum w_i * (V - v_i) = 0
578 * \Sum w_i * V - w_i * v_i = 0
581 * se->vruntime):
584 * V = -------------- = --------------
591 * virtual time has non-contiguous motion equivalent to:
593 * V +-= lag_i / W
600 * Substitute: v_i == (v_i - v0) + v0
602 * \Sum ((v_i - v0) + v0) * w_i \Sum (v_i - v0) * w_i
603 * V = ---------------------------- = --------------------- + v0
608 * v0 := cfs_rq->min_vruntime
609 * \Sum (v_i - v0) * w_i := cfs_rq->avg_vruntime
610 * \Sum w_i := cfs_rq->avg_load
613 * the per-task service, these deltas: (v_i - v), will be in the order of the
623 unsigned long weight = scale_load_down(se->load.weight); in avg_vruntime_add()
626 cfs_rq->avg_vruntime += key * weight; in avg_vruntime_add()
627 cfs_rq->avg_load += weight; in avg_vruntime_add()
633 unsigned long weight = scale_load_down(se->load.weight); in avg_vruntime_sub()
636 cfs_rq->avg_vruntime -= key * weight; in avg_vruntime_sub()
637 cfs_rq->avg_load -= weight; in avg_vruntime_sub()
644 * v' = v + d ==> avg_vruntime' = avg_runtime - d*avg_load in avg_vruntime_update()
646 cfs_rq->avg_vruntime -= cfs_rq->avg_load * delta; in avg_vruntime_update()
655 struct sched_entity *curr = cfs_rq->curr; in avg_vruntime()
656 s64 avg = cfs_rq->avg_vruntime; in avg_vruntime()
657 long load = cfs_rq->avg_load; in avg_vruntime()
659 if (curr && curr->on_rq) { in avg_vruntime()
660 unsigned long weight = scale_load_down(curr->load.weight); in avg_vruntime()
669 avg -= (load - 1); in avg_vruntime()
673 return cfs_rq->min_vruntime + avg; in avg_vruntime()
677 * lag_i = S - s_i = w_i * (V - v_i)
680 * is possible -- by addition/removal/reweight to the tree -- to move V around
688 * -r_max < lag < max(r_max, q)
696 vlag = avruntime - se->vruntime; in entity_lag()
697 limit = calc_delta_fair(max_t(u64, 2*se->slice, TICK_NSEC), se); in entity_lag()
699 return clamp(vlag, -limit, limit); in entity_lag()
704 SCHED_WARN_ON(!se->on_rq); in update_entity_lag()
706 se->vlag = entity_lag(avg_vruntime(cfs_rq), se); in update_entity_lag()
713 * lag_i = S - s_i = w_i*(V - v_i)
715 * lag_i >= 0 -> V >= v_i
717 * \Sum (v_i - v)*w_i
718 * V = ------------------ + v
721 * lag_i >= 0 -> \Sum (v_i - v)*w_i >= (v_i - v)*(\Sum w_i)
723 * Note: using 'avg_vruntime() > se->vruntime' is inaccurate due
728 struct sched_entity *curr = cfs_rq->curr; in vruntime_eligible()
729 s64 avg = cfs_rq->avg_vruntime; in vruntime_eligible()
730 long load = cfs_rq->avg_load; in vruntime_eligible()
732 if (curr && curr->on_rq) { in vruntime_eligible()
733 unsigned long weight = scale_load_down(curr->load.weight); in vruntime_eligible()
739 return avg >= (s64)(vruntime - cfs_rq->min_vruntime) * load; in vruntime_eligible()
744 return vruntime_eligible(cfs_rq, se->vruntime); in entity_eligible()
749 u64 min_vruntime = cfs_rq->min_vruntime; in __update_min_vruntime()
753 s64 delta = (s64)(vruntime - min_vruntime); in __update_min_vruntime()
764 struct sched_entity *curr = cfs_rq->curr; in update_min_vruntime()
765 u64 vruntime = cfs_rq->min_vruntime; in update_min_vruntime()
768 if (curr->on_rq) in update_min_vruntime()
769 vruntime = curr->vruntime; in update_min_vruntime()
776 vruntime = se->min_vruntime; in update_min_vruntime()
778 vruntime = min_vruntime(vruntime, se->min_vruntime); in update_min_vruntime()
782 cfs_rq->min_vruntime = __update_min_vruntime(cfs_rq, vruntime); in update_min_vruntime()
788 struct sched_entity *curr = cfs_rq->curr; in cfs_rq_min_slice()
791 if (curr && curr->on_rq) in cfs_rq_min_slice()
792 min_slice = curr->slice; in cfs_rq_min_slice()
795 min_slice = min(min_slice, root->min_slice); in cfs_rq_min_slice()
805 #define vruntime_gt(field, lse, rse) ({ (s64)((lse)->field - (rse)->field) > 0; })
812 se->min_vruntime = rse->min_vruntime; in __min_vruntime_update()
820 if (rse->min_slice < se->min_slice) in __min_slice_update()
821 se->min_slice = rse->min_slice; in __min_slice_update()
826 * se->min_vruntime = min(se->vruntime, {left,right}->min_vruntime)
830 u64 old_min_vruntime = se->min_vruntime; in min_vruntime_update()
831 u64 old_min_slice = se->min_slice; in min_vruntime_update()
832 struct rb_node *node = &se->run_node; in min_vruntime_update()
834 se->min_vruntime = se->vruntime; in min_vruntime_update()
835 __min_vruntime_update(se, node->rb_right); in min_vruntime_update()
836 __min_vruntime_update(se, node->rb_left); in min_vruntime_update()
838 se->min_slice = se->slice; in min_vruntime_update()
839 __min_slice_update(se, node->rb_right); in min_vruntime_update()
840 __min_slice_update(se, node->rb_left); in min_vruntime_update()
842 return se->min_vruntime == old_min_vruntime && in min_vruntime_update()
843 se->min_slice == old_min_slice; in min_vruntime_update()
850 * Enqueue an entity into the rb-tree:
855 se->min_vruntime = se->vruntime; in __enqueue_entity()
856 se->min_slice = se->slice; in __enqueue_entity()
857 rb_add_augmented_cached(&se->run_node, &cfs_rq->tasks_timeline, in __enqueue_entity()
863 rb_erase_augmented_cached(&se->run_node, &cfs_rq->tasks_timeline, in __dequeue_entity()
870 struct rb_node *root = cfs_rq->tasks_timeline.rb_root.rb_node; in __pick_root_entity()
880 struct rb_node *left = rb_first_cached(&cfs_rq->tasks_timeline); in __pick_first_entity()
899 * We can do this in O(log n) time due to an augmented RB-tree. The
903 * se->min_vruntime = min(se->vruntime, se->{left,right}->min_vruntime)
909 struct rb_node *node = cfs_rq->tasks_timeline.rb_root.rb_node; in pick_eevdf()
911 struct sched_entity *curr = cfs_rq->curr; in pick_eevdf()
918 if (cfs_rq->nr_running == 1) in pick_eevdf()
919 return curr && curr->on_rq ? curr : se; in pick_eevdf()
921 if (curr && (!curr->on_rq || !entity_eligible(cfs_rq, curr))) in pick_eevdf()
925 * Once selected, run a task until it either becomes non-eligible or in pick_eevdf()
928 if (sched_feat(RUN_TO_PARITY) && curr && curr->vlag == curr->deadline) in pick_eevdf()
939 struct rb_node *left = node->rb_left; in pick_eevdf()
946 __node_2_se(left)->min_vruntime)) { in pick_eevdf()
963 node = node->rb_right; in pick_eevdf()
975 struct rb_node *last = rb_last(&cfs_rq->tasks_timeline.rb_root); in __pick_last_entity()
1009 if ((s64)(se->vruntime - se->deadline) < 0) in update_deadline()
1017 if (!se->custom_slice) in update_deadline()
1018 se->slice = sysctl_sched_base_slice; in update_deadline()
1023 se->deadline = se->vruntime + calc_delta_fair(se->slice, se); in update_deadline()
1041 struct sched_avg *sa = &se->avg; in init_entity_runnable_average()
1052 sa->load_avg = scale_load_down(se->load.weight); in init_entity_runnable_average()
1061 * util_avg = cfs_rq->avg.util_avg / (cfs_rq->avg.load_avg + 1)
1071 * util_avg_cap = (cpu_scale - cfs_rq->avg.util_avg) / 2^n
1086 struct sched_entity *se = &p->se; in post_init_entity_util_avg()
1088 struct sched_avg *sa = &se->avg; in post_init_entity_util_avg()
1090 long cap = (long)(cpu_scale - cfs_rq->avg.util_avg) / 2; in post_init_entity_util_avg()
1092 if (p->sched_class != &fair_sched_class) { in post_init_entity_util_avg()
1103 se->avg.last_update_time = cfs_rq_clock_pelt(cfs_rq); in post_init_entity_util_avg()
1108 if (cfs_rq->avg.util_avg != 0) { in post_init_entity_util_avg()
1109 sa->util_avg = cfs_rq->avg.util_avg * se_weight(se); in post_init_entity_util_avg()
1110 sa->util_avg /= (cfs_rq->avg.load_avg + 1); in post_init_entity_util_avg()
1112 if (sa->util_avg > cap) in post_init_entity_util_avg()
1113 sa->util_avg = cap; in post_init_entity_util_avg()
1115 sa->util_avg = cap; in post_init_entity_util_avg()
1119 sa->runnable_avg = sa->util_avg; in post_init_entity_util_avg()
1139 delta_exec = now - curr->exec_start; in update_curr_se()
1143 curr->exec_start = now; in update_curr_se()
1144 curr->sum_exec_runtime += delta_exec; in update_curr_se()
1150 __schedstat_set(stats->exec_max, in update_curr_se()
1151 max(delta_exec, stats->exec_max)); in update_curr_se()
1162 if (p->dl_server) in update_curr_task()
1163 dl_server_update(p->dl_server, delta_exec); in update_curr_task()
1171 if (curr->vlag == curr->deadline) in did_preempt_short()
1183 if (pse->slice >= se->slice) in do_preempt_short()
1203 struct task_struct *curr = rq->curr; in update_curr_common()
1206 delta_exec = update_curr_se(rq, &curr->se); in update_curr_common()
1218 struct sched_entity *curr = cfs_rq->curr; in update_curr()
1230 curr->vruntime += calc_delta_fair(delta_exec, curr); in update_curr()
1244 if (p->dl_server != &rq->fair_server) in update_curr()
1245 dl_server_update(&rq->fair_server, delta_exec); in update_curr()
1250 if (cfs_rq->nr_running == 1) in update_curr()
1261 update_curr(cfs_rq_of(&rq->curr->se)); in update_curr_fair()
1294 * maybe already in the runqueue, the se->statistics.wait_start in update_stats_wait_end_fair()
1298 if (unlikely(!schedstat_val(stats->wait_start))) in update_stats_wait_end_fair()
1325 * Task is being enqueued - update stats:
1337 if (se != cfs_rq->curr) in update_stats_enqueue_fair()
1355 if (se != cfs_rq->curr) in update_stats_dequeue_fair()
1363 state = READ_ONCE(tsk->__state); in update_stats_dequeue_fair()
1365 __schedstat_set(tsk->stats.sleep_start, in update_stats_dequeue_fair()
1368 __schedstat_set(tsk->stats.block_start, in update_stats_dequeue_fair()
1374 * We are picking a new current task - update its stats:
1382 se->exec_start = rq_clock_task(rq_of(cfs_rq)); in update_stats_curr_start()
1435 * Approximate time to scan a full NUMA task in ms. The task scan period is
1445 /* Scan @scan_size MB every @scan_period after an initial @scan_delay in ms */
1448 /* The page with hint page fault latency < threshold in ms is considered hot */
1474 * ->numa_group (see struct task_struct for locking rules).
1478 return rcu_dereference_check(p->numa_group, p == current || in deref_task_numa_group()
1479 (lockdep_is_held(__rq_lockp(task_rq(p))) && !READ_ONCE(p->on_cpu))); in deref_task_numa_group()
1484 return rcu_dereference_protected(p->numa_group, p == current); in deref_curr_numa_group()
1496 * Calculations based on RSS as non-present and empty pages are skipped in task_nr_scan_windows()
1500 nr_scan_pages = sysctl_numa_balancing_scan_size << (20 - PAGE_SHIFT); in task_nr_scan_windows()
1501 rss = get_mm_rss(p->mm); in task_nr_scan_windows()
1534 ng = rcu_dereference(p->numa_group); in task_scan_start()
1539 period *= refcount_read(&ng->refcount); in task_scan_start()
1564 period *= refcount_read(&ng->refcount); in task_scan_max()
1576 rq->nr_numa_running += (p->numa_preferred_nid != NUMA_NO_NODE); in account_numa_enqueue()
1577 rq->nr_preferred_running += (p->numa_preferred_nid == task_node(p)); in account_numa_enqueue()
1582 rq->nr_numa_running -= (p->numa_preferred_nid != NUMA_NO_NODE); in account_numa_dequeue()
1583 rq->nr_preferred_running -= (p->numa_preferred_nid == task_node(p)); in account_numa_dequeue()
1601 ng = rcu_dereference(p->numa_group); in task_numa_group_id()
1603 gid = ng->gid; in task_numa_group_id()
1622 if (!p->numa_faults) in task_faults()
1625 return p->numa_faults[task_faults_idx(NUMA_MEM, nid, 0)] + in task_faults()
1626 p->numa_faults[task_faults_idx(NUMA_MEM, nid, 1)]; in task_faults()
1636 return ng->faults[task_faults_idx(NUMA_MEM, nid, 0)] + in group_faults()
1637 ng->faults[task_faults_idx(NUMA_MEM, nid, 1)]; in group_faults()
1642 return group->faults[task_faults_idx(NUMA_CPU, nid, 0)] + in group_faults_cpu()
1643 group->faults[task_faults_idx(NUMA_CPU, nid, 1)]; in group_faults_cpu()
1652 faults += ng->faults[task_faults_idx(NUMA_MEM, node, 1)]; in group_faults_priv()
1664 faults += ng->faults[task_faults_idx(NUMA_MEM, node, 0)]; in group_faults_shared()
1672 * considered part of a numa group's pseudo-interleaving set. Migrations
1679 return group_faults_cpu(ng, nid) * ACTIVE_NODE_FRACTION > ng->max_faults_cpu; in numa_is_active_node()
1738 faults *= (max_dist - dist); in score_nearby_nodes()
1739 faults /= (max_dist - LOCAL_DISTANCE); in score_nearby_nodes()
1759 if (!p->numa_faults) in task_weight()
1762 total_faults = p->total_numa_faults; in task_weight()
1782 total_faults = ng->total_faults; in group_weight()
1818 pgdat->node_present_pages >> 4); in pgdat_free_space_enough()
1819 for (z = pgdat->nr_zones - 1; z >= 0; z--) { in pgdat_free_space_enough()
1820 struct zone *zone = pgdat->node_zones + z; in pgdat_free_space_enough()
1840 * hint page fault latency = hint page fault time - scan time
1852 return (time - last_time) & PAGE_ACCESS_TIME_MASK; in numa_hint_fault_latency()
1869 start = pgdat->nbp_rl_start; in numa_promotion_rate_limit()
1870 if (now - start > MSEC_PER_SEC && in numa_promotion_rate_limit()
1871 cmpxchg(&pgdat->nbp_rl_start, start, now) == start) in numa_promotion_rate_limit()
1872 pgdat->nbp_rl_nr_cand = nr_cand; in numa_promotion_rate_limit()
1873 if (nr_cand - pgdat->nbp_rl_nr_cand >= rate_limit) in numa_promotion_rate_limit()
1889 start = pgdat->nbp_th_start; in numa_promotion_adjust_threshold()
1890 if (now - start > th_period && in numa_promotion_adjust_threshold()
1891 cmpxchg(&pgdat->nbp_th_start, start, now) == start) { in numa_promotion_adjust_threshold()
1895 diff_cand = nr_cand - pgdat->nbp_th_nr_cand; in numa_promotion_adjust_threshold()
1897 th = pgdat->nbp_threshold ? : ref_th; in numa_promotion_adjust_threshold()
1899 th = max(th - unit_th, unit_th); in numa_promotion_adjust_threshold()
1902 pgdat->nbp_th_nr_cand = nr_cand; in numa_promotion_adjust_threshold()
1903 pgdat->nbp_threshold = th; in numa_promotion_adjust_threshold()
1932 pgdat->nbp_threshold = 0; in should_numa_migrate_memory()
1938 (20 - PAGE_SHIFT); in should_numa_migrate_memory()
1941 th = pgdat->nbp_threshold ? : def_th; in should_numa_migrate_memory()
1950 this_cpupid = cpu_pid_to_cpupid(dst_cpu, current->pid); in should_numa_migrate_memory()
1960 * two full passes of the "multi-stage node selection" test that is in should_numa_migrate_memory()
1963 if ((p->numa_preferred_nid == NUMA_NO_NODE || p->numa_scan_seq <= 4) && in should_numa_migrate_memory()
1968 * Multi-stage node selection is used in conjunction with a periodic in should_numa_migrate_memory()
1969 * migration fault to build a temporal task<->page relation. By using in should_numa_migrate_memory()
1970 * a two-stage filter we remove short/unlikely relations. in should_numa_migrate_memory()
1974 * page (n_t) (in a given time-span) to a probability. in should_numa_migrate_memory()
1982 * act on an unlikely task<->page relation. in should_numa_migrate_memory()
1992 /* A shared fault, but p->numa_group has not been set up yet. */ in should_numa_migrate_memory()
2009 * --------------- * - > --------------- in should_numa_migrate_memory()
2071 if ((ns->nr_running > ns->weight) && in numa_classify()
2072 (((ns->compute_capacity * 100) < (ns->util * imbalance_pct)) || in numa_classify()
2073 ((ns->compute_capacity * imbalance_pct) < (ns->runnable * 100)))) in numa_classify()
2076 if ((ns->nr_running < ns->weight) || in numa_classify()
2077 (((ns->compute_capacity * 100) > (ns->util * imbalance_pct)) && in numa_classify()
2078 ((ns->compute_capacity * imbalance_pct) > (ns->runnable * 100)))) in numa_classify()
2119 int cpu, idle_core = -1; in update_numa_stats()
2122 ns->idle_cpu = -1; in update_numa_stats()
2128 ns->load += cpu_load(rq); in update_numa_stats()
2129 ns->runnable += cpu_runnable(rq); in update_numa_stats()
2130 ns->util += cpu_util_cfs(cpu); in update_numa_stats()
2131 ns->nr_running += rq->cfs.h_nr_running; in update_numa_stats()
2132 ns->compute_capacity += capacity_of(cpu); in update_numa_stats()
2134 if (find_idle && idle_core < 0 && !rq->nr_running && idle_cpu(cpu)) { in update_numa_stats()
2135 if (READ_ONCE(rq->numa_migrate_on) || in update_numa_stats()
2136 !cpumask_test_cpu(cpu, env->p->cpus_ptr)) in update_numa_stats()
2139 if (ns->idle_cpu == -1) in update_numa_stats()
2140 ns->idle_cpu = cpu; in update_numa_stats()
2147 ns->weight = cpumask_weight(cpumask_of_node(nid)); in update_numa_stats()
2149 ns->node_type = numa_classify(env->imbalance_pct, ns); in update_numa_stats()
2152 ns->idle_cpu = idle_core; in update_numa_stats()
2158 struct rq *rq = cpu_rq(env->dst_cpu); in task_numa_assign()
2160 /* Check if run-queue part of active NUMA balance. */ in task_numa_assign()
2161 if (env->best_cpu != env->dst_cpu && xchg(&rq->numa_migrate_on, 1)) { in task_numa_assign()
2163 int start = env->dst_cpu; in task_numa_assign()
2166 for_each_cpu_wrap(cpu, cpumask_of_node(env->dst_nid), start + 1) { in task_numa_assign()
2167 if (cpu == env->best_cpu || !idle_cpu(cpu) || in task_numa_assign()
2168 !cpumask_test_cpu(cpu, env->p->cpus_ptr)) { in task_numa_assign()
2172 env->dst_cpu = cpu; in task_numa_assign()
2173 rq = cpu_rq(env->dst_cpu); in task_numa_assign()
2174 if (!xchg(&rq->numa_migrate_on, 1)) in task_numa_assign()
2184 * Clear previous best_cpu/rq numa-migrate flag, since task now in task_numa_assign()
2187 if (env->best_cpu != -1 && env->best_cpu != env->dst_cpu) { in task_numa_assign()
2188 rq = cpu_rq(env->best_cpu); in task_numa_assign()
2189 WRITE_ONCE(rq->numa_migrate_on, 0); in task_numa_assign()
2192 if (env->best_task) in task_numa_assign()
2193 put_task_struct(env->best_task); in task_numa_assign()
2197 env->best_task = p; in task_numa_assign()
2198 env->best_imp = imp; in task_numa_assign()
2199 env->best_cpu = env->dst_cpu; in task_numa_assign()
2213 * ------------ vs --------- in load_too_imbalanced()
2216 src_capacity = env->src_stats.compute_capacity; in load_too_imbalanced()
2217 dst_capacity = env->dst_stats.compute_capacity; in load_too_imbalanced()
2219 imb = abs(dst_load * src_capacity - src_load * dst_capacity); in load_too_imbalanced()
2221 orig_src_load = env->src_stats.load; in load_too_imbalanced()
2222 orig_dst_load = env->dst_stats.load; in load_too_imbalanced()
2224 old_imb = abs(orig_dst_load * src_capacity - orig_src_load * dst_capacity); in load_too_imbalanced()
2246 struct numa_group *cur_ng, *p_ng = deref_curr_numa_group(env->p); in task_numa_compare()
2247 struct rq *dst_rq = cpu_rq(env->dst_cpu); in task_numa_compare()
2251 int dist = env->dist; in task_numa_compare()
2256 if (READ_ONCE(dst_rq->numa_migrate_on)) in task_numa_compare()
2260 cur = rcu_dereference(dst_rq->curr); in task_numa_compare()
2261 if (cur && ((cur->flags & PF_EXITING) || is_idle_task(cur))) in task_numa_compare()
2266 * end try selecting ourselves (current == env->p) as a swap candidate. in task_numa_compare()
2268 if (cur == env->p) { in task_numa_compare()
2274 if (maymove && moveimp >= env->best_imp) in task_numa_compare()
2281 if (!cpumask_test_cpu(env->src_cpu, cur->cpus_ptr)) in task_numa_compare()
2288 if (env->best_task && in task_numa_compare()
2289 env->best_task->numa_preferred_nid == env->src_nid && in task_numa_compare()
2290 cur->numa_preferred_nid != env->src_nid) { in task_numa_compare()
2304 cur_ng = rcu_dereference(cur->numa_group); in task_numa_compare()
2312 if (env->dst_stats.node_type == node_has_spare) in task_numa_compare()
2315 imp = taskimp + task_weight(cur, env->src_nid, dist) - in task_numa_compare()
2316 task_weight(cur, env->dst_nid, dist); in task_numa_compare()
2322 imp -= imp / 16; in task_numa_compare()
2329 imp += group_weight(cur, env->src_nid, dist) - in task_numa_compare()
2330 group_weight(cur, env->dst_nid, dist); in task_numa_compare()
2332 imp += task_weight(cur, env->src_nid, dist) - in task_numa_compare()
2333 task_weight(cur, env->dst_nid, dist); in task_numa_compare()
2337 if (cur->numa_preferred_nid == env->dst_nid) in task_numa_compare()
2338 imp -= imp / 16; in task_numa_compare()
2346 if (cur->numa_preferred_nid == env->src_nid) in task_numa_compare()
2349 if (maymove && moveimp > imp && moveimp > env->best_imp) { in task_numa_compare()
2359 if (env->best_task && cur->numa_preferred_nid == env->src_nid && in task_numa_compare()
2360 env->best_task->numa_preferred_nid != env->src_nid) { in task_numa_compare()
2370 if (imp < SMALLIMP || imp <= env->best_imp + SMALLIMP / 2) in task_numa_compare()
2376 load = task_h_load(env->p) - task_h_load(cur); in task_numa_compare()
2380 dst_load = env->dst_stats.load + load; in task_numa_compare()
2381 src_load = env->src_stats.load - load; in task_numa_compare()
2389 int cpu = env->dst_stats.idle_cpu; in task_numa_compare()
2393 cpu = env->dst_cpu; in task_numa_compare()
2399 if (!idle_cpu(cpu) && env->best_cpu >= 0 && in task_numa_compare()
2400 idle_cpu(env->best_cpu)) { in task_numa_compare()
2401 cpu = env->best_cpu; in task_numa_compare()
2404 env->dst_cpu = cpu; in task_numa_compare()
2414 if (maymove && !cur && env->best_cpu >= 0 && idle_cpu(env->best_cpu)) in task_numa_compare()
2421 if (!maymove && env->best_task && in task_numa_compare()
2422 env->best_task->numa_preferred_nid == env->src_nid) { in task_numa_compare()
2441 if (env->dst_stats.node_type == node_has_spare) { in task_numa_find_cpu()
2451 src_running = env->src_stats.nr_running - 1; in task_numa_find_cpu()
2452 dst_running = env->dst_stats.nr_running + 1; in task_numa_find_cpu()
2453 imbalance = max(0, dst_running - src_running); in task_numa_find_cpu()
2455 env->imb_numa_nr); in task_numa_find_cpu()
2460 if (env->dst_stats.idle_cpu >= 0) { in task_numa_find_cpu()
2461 env->dst_cpu = env->dst_stats.idle_cpu; in task_numa_find_cpu()
2469 * If the improvement from just moving env->p direction is better in task_numa_find_cpu()
2472 load = task_h_load(env->p); in task_numa_find_cpu()
2473 dst_load = env->dst_stats.load + load; in task_numa_find_cpu()
2474 src_load = env->src_stats.load - load; in task_numa_find_cpu()
2478 for_each_cpu(cpu, cpumask_of_node(env->dst_nid)) { in task_numa_find_cpu()
2480 if (!cpumask_test_cpu(cpu, env->p->cpus_ptr)) in task_numa_find_cpu()
2483 env->dst_cpu = cpu; in task_numa_find_cpu()
2501 .best_cpu = -1, in task_numa_migrate()
2515 * random movement of tasks -- counter the numa conditions we're trying in task_numa_migrate()
2521 env.imbalance_pct = 100 + (sd->imbalance_pct - 100) / 2; in task_numa_migrate()
2522 env.imb_numa_nr = sd->imb_numa_nr; in task_numa_migrate()
2534 return -EINVAL; in task_numa_migrate()
2537 env.dst_nid = p->numa_preferred_nid; in task_numa_migrate()
2542 taskimp = task_weight(p, env.dst_nid, dist) - taskweight; in task_numa_migrate()
2543 groupimp = group_weight(p, env.dst_nid, dist) - groupweight; in task_numa_migrate()
2551 * - there is no space available on the preferred_nid in task_numa_migrate()
2552 * - the task is part of a numa_group that is interleaved across in task_numa_migrate()
2557 if (env.best_cpu == -1 || (ng && ng->active_nodes > 1)) { in task_numa_migrate()
2559 if (nid == env.src_nid || nid == p->numa_preferred_nid) in task_numa_migrate()
2570 taskimp = task_weight(p, nid, dist) - taskweight; in task_numa_migrate()
2571 groupimp = group_weight(p, nid, dist) - groupweight; in task_numa_migrate()
2591 if (env.best_cpu == -1) in task_numa_migrate()
2596 if (nid != p->numa_preferred_nid) in task_numa_migrate()
2601 if (env.best_cpu == -1) { in task_numa_migrate()
2602 trace_sched_stick_numa(p, env.src_cpu, NULL, -1); in task_numa_migrate()
2603 return -EAGAIN; in task_numa_migrate()
2609 WRITE_ONCE(best_rq->numa_migrate_on, 0); in task_numa_migrate()
2616 WRITE_ONCE(best_rq->numa_migrate_on, 0); in task_numa_migrate()
2630 if (unlikely(p->numa_preferred_nid == NUMA_NO_NODE || !p->numa_faults)) in numa_migrate_preferred()
2634 interval = min(interval, msecs_to_jiffies(p->numa_scan_period) / 16); in numa_migrate_preferred()
2635 p->numa_migrate_retry = jiffies + interval; in numa_migrate_preferred()
2638 if (task_node(p) == p->numa_preferred_nid) in numa_migrate_preferred()
2668 numa_group->max_faults_cpu = max_faults; in numa_group_count_active_nodes()
2669 numa_group->active_nodes = active_nodes; in numa_group_count_active_nodes()
2695 unsigned long remote = p->numa_faults_locality[0]; in update_task_scan_period()
2696 unsigned long local = p->numa_faults_locality[1]; in update_task_scan_period()
2705 if (local + shared == 0 || p->numa_faults_locality[2]) { in update_task_scan_period()
2706 p->numa_scan_period = min(p->numa_scan_period_max, in update_task_scan_period()
2707 p->numa_scan_period << 1); in update_task_scan_period()
2709 p->mm->numa_next_scan = jiffies + in update_task_scan_period()
2710 msecs_to_jiffies(p->numa_scan_period); in update_task_scan_period()
2721 period_slot = DIV_ROUND_UP(p->numa_scan_period, NUMA_PERIOD_SLOTS); in update_task_scan_period()
2730 int slot = ps_ratio - NUMA_PERIOD_THRESHOLD; in update_task_scan_period()
2740 int slot = lr_ratio - NUMA_PERIOD_THRESHOLD; in update_task_scan_period()
2746 * Private memory faults exceed (SLOTS-THRESHOLD)/SLOTS, in update_task_scan_period()
2751 diff = -(NUMA_PERIOD_THRESHOLD - ratio) * period_slot; in update_task_scan_period()
2754 p->numa_scan_period = clamp(p->numa_scan_period + diff, in update_task_scan_period()
2756 memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); in update_task_scan_period()
2762 * decays those on a 32ms period, which is orders of magnitude off
2763 * from the dozens-of-seconds NUMA balancing period. Use the scheduler
2770 now = p->se.exec_start; in numa_get_avg_runtime()
2771 runtime = p->se.sum_exec_runtime; in numa_get_avg_runtime()
2773 if (p->last_task_numa_placement) { in numa_get_avg_runtime()
2774 delta = runtime - p->last_sum_exec_runtime; in numa_get_avg_runtime()
2775 *period = now - p->last_task_numa_placement; in numa_get_avg_runtime()
2781 delta = p->se.avg.load_sum; in numa_get_avg_runtime()
2785 p->last_sum_exec_runtime = runtime; in numa_get_avg_runtime()
2786 p->last_task_numa_placement = now; in numa_get_avg_runtime()
2836 for (dist = sched_max_numa_distance; dist > LOCAL_DISTANCE; dist--) { in preferred_group_nid()
2890 * The p->mm->numa_scan_seq field gets updated without in task_numa_placement()
2894 seq = READ_ONCE(p->mm->numa_scan_seq); in task_numa_placement()
2895 if (p->numa_scan_seq == seq) in task_numa_placement()
2897 p->numa_scan_seq = seq; in task_numa_placement()
2898 p->numa_scan_period_max = task_scan_max(p); in task_numa_placement()
2900 total_faults = p->numa_faults_locality[0] + in task_numa_placement()
2901 p->numa_faults_locality[1]; in task_numa_placement()
2907 group_lock = &ng->lock; in task_numa_placement()
2927 diff = p->numa_faults[membuf_idx] - p->numa_faults[mem_idx] / 2; in task_numa_placement()
2928 fault_types[priv] += p->numa_faults[membuf_idx]; in task_numa_placement()
2929 p->numa_faults[membuf_idx] = 0; in task_numa_placement()
2935 * little over-all impact on throughput, and thus their in task_numa_placement()
2939 f_weight = (f_weight * p->numa_faults[cpubuf_idx]) / in task_numa_placement()
2941 f_diff = f_weight - p->numa_faults[cpu_idx] / 2; in task_numa_placement()
2942 p->numa_faults[cpubuf_idx] = 0; in task_numa_placement()
2944 p->numa_faults[mem_idx] += diff; in task_numa_placement()
2945 p->numa_faults[cpu_idx] += f_diff; in task_numa_placement()
2946 faults += p->numa_faults[mem_idx]; in task_numa_placement()
2947 p->total_numa_faults += diff; in task_numa_placement()
2956 ng->faults[mem_idx] += diff; in task_numa_placement()
2957 ng->faults[cpu_idx] += f_diff; in task_numa_placement()
2958 ng->total_faults += diff; in task_numa_placement()
2959 group_faults += ng->faults[mem_idx]; in task_numa_placement()
2974 /* Cannot migrate task to CPU-less node */ in task_numa_placement()
2985 if (max_nid != p->numa_preferred_nid) in task_numa_placement()
2994 return refcount_inc_not_zero(&grp->refcount); in get_numa_group()
2999 if (refcount_dec_and_test(&grp->refcount)) in put_numa_group()
3021 refcount_set(&grp->refcount, 1); in task_numa_group()
3022 grp->active_nodes = 1; in task_numa_group()
3023 grp->max_faults_cpu = 0; in task_numa_group()
3024 spin_lock_init(&grp->lock); in task_numa_group()
3025 grp->gid = p->pid; in task_numa_group()
3028 grp->faults[i] = p->numa_faults[i]; in task_numa_group()
3030 grp->total_faults = p->total_numa_faults; in task_numa_group()
3032 grp->nr_tasks++; in task_numa_group()
3033 rcu_assign_pointer(p->numa_group, grp); in task_numa_group()
3037 tsk = READ_ONCE(cpu_rq(cpu)->curr); in task_numa_group()
3042 grp = rcu_dereference(tsk->numa_group); in task_numa_group()
3054 if (my_grp->nr_tasks > grp->nr_tasks) in task_numa_group()
3058 * Tie-break on the grp address. in task_numa_group()
3060 if (my_grp->nr_tasks == grp->nr_tasks && my_grp > grp) in task_numa_group()
3064 if (tsk->mm == current->mm) in task_numa_group()
3083 double_lock_irq(&my_grp->lock, &grp->lock); in task_numa_group()
3086 my_grp->faults[i] -= p->numa_faults[i]; in task_numa_group()
3087 grp->faults[i] += p->numa_faults[i]; in task_numa_group()
3089 my_grp->total_faults -= p->total_numa_faults; in task_numa_group()
3090 grp->total_faults += p->total_numa_faults; in task_numa_group()
3092 my_grp->nr_tasks--; in task_numa_group()
3093 grp->nr_tasks++; in task_numa_group()
3095 spin_unlock(&my_grp->lock); in task_numa_group()
3096 spin_unlock_irq(&grp->lock); in task_numa_group()
3098 rcu_assign_pointer(p->numa_group, grp); in task_numa_group()
3113 * reset the data back to default state without freeing ->numa_faults.
3118 struct numa_group *grp = rcu_dereference_raw(p->numa_group); in task_numa_free()
3119 unsigned long *numa_faults = p->numa_faults; in task_numa_free()
3127 spin_lock_irqsave(&grp->lock, flags); in task_numa_free()
3129 grp->faults[i] -= p->numa_faults[i]; in task_numa_free()
3130 grp->total_faults -= p->total_numa_faults; in task_numa_free()
3132 grp->nr_tasks--; in task_numa_free()
3133 spin_unlock_irqrestore(&grp->lock, flags); in task_numa_free()
3134 RCU_INIT_POINTER(p->numa_group, NULL); in task_numa_free()
3139 p->numa_faults = NULL; in task_numa_free()
3142 p->total_numa_faults = 0; in task_numa_free()
3164 if (!p->mm) in task_numa_fault()
3176 /* Allocate buffer to track faults on a per-node basis */ in task_numa_fault()
3177 if (unlikely(!p->numa_faults)) { in task_numa_fault()
3178 int size = sizeof(*p->numa_faults) * in task_numa_fault()
3181 p->numa_faults = kzalloc(size, GFP_KERNEL|__GFP_NOWARN); in task_numa_fault()
3182 if (!p->numa_faults) in task_numa_fault()
3185 p->total_numa_faults = 0; in task_numa_fault()
3186 memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); in task_numa_fault()
3193 if (unlikely(last_cpupid == (-1 & LAST_CPUPID_MASK))) { in task_numa_fault()
3208 if (!priv && !local && ng && ng->active_nodes > 1 && in task_numa_fault()
3217 if (time_after(jiffies, p->numa_migrate_retry)) { in task_numa_fault()
3223 p->numa_pages_migrated += pages; in task_numa_fault()
3225 p->numa_faults_locality[2] += pages; in task_numa_fault()
3227 p->numa_faults[task_faults_idx(NUMA_MEMBUF, mem_node, priv)] += pages; in task_numa_fault()
3228 p->numa_faults[task_faults_idx(NUMA_CPUBUF, cpu_node, priv)] += pages; in task_numa_fault()
3229 p->numa_faults_locality[local] += pages; in task_numa_fault()
3236 * p->mm->numa_scan_seq is written to without exclusive access in reset_ptenuma_scan()
3242 WRITE_ONCE(p->mm->numa_scan_seq, READ_ONCE(p->mm->numa_scan_seq) + 1); in reset_ptenuma_scan()
3243 p->mm->numa_scan_offset = 0; in reset_ptenuma_scan()
3255 if ((READ_ONCE(current->mm->numa_scan_seq) - vma->numab_state->start_scan_seq) < 2) in vma_is_accessed()
3258 pids = vma->numab_state->pids_active[0] | vma->numab_state->pids_active[1]; in vma_is_accessed()
3259 if (test_bit(hash_32(current->pid, ilog2(BITS_PER_LONG)), &pids)) in vma_is_accessed()
3264 * some VMAs may never be scanned in multi-threaded applications: in vma_is_accessed()
3266 if (mm->numa_scan_offset > vma->vm_start) { in vma_is_accessed()
3276 if (READ_ONCE(mm->numa_scan_seq) > in vma_is_accessed()
3277 (vma->numab_state->prev_scan_seq + get_nr_threads(current))) in vma_is_accessed()
3293 struct mm_struct *mm = p->mm; in task_numa_work()
3294 u64 runtime = p->se.sum_exec_runtime; in task_numa_work()
3305 work->next = work; in task_numa_work()
3309 * NOTE: make sure not to dereference p->mm before this check, in task_numa_work()
3311 * without p->mm even though we still had it when we enqueued this in task_numa_work()
3314 if (p->flags & PF_EXITING) in task_numa_work()
3317 if (!mm->numa_next_scan) { in task_numa_work()
3318 mm->numa_next_scan = now + in task_numa_work()
3325 migrate = mm->numa_next_scan; in task_numa_work()
3329 if (p->numa_scan_period == 0) { in task_numa_work()
3330 p->numa_scan_period_max = task_scan_max(p); in task_numa_work()
3331 p->numa_scan_period = task_scan_start(p); in task_numa_work()
3334 next_scan = now + msecs_to_jiffies(p->numa_scan_period); in task_numa_work()
3335 if (!try_cmpxchg(&mm->numa_next_scan, &migrate, next_scan)) in task_numa_work()
3342 p->node_stamp += 2 * TICK_NSEC; in task_numa_work()
3345 pages <<= 20 - PAGE_SHIFT; /* MB in pages */ in task_numa_work()
3362 start = mm->numa_scan_offset; in task_numa_work()
3374 is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_MIXEDMAP)) { in task_numa_work()
3382 * hinting faults in read-only file-backed mappings or the vDSO in task_numa_work()
3385 if (!vma->vm_mm || in task_numa_work()
3386 (vma->vm_file && (vma->vm_flags & (VM_READ|VM_WRITE)) == (VM_READ))) { in task_numa_work()
3400 /* Initialise new per-VMA NUMAB state. */ in task_numa_work()
3401 if (!vma->numab_state) { in task_numa_work()
3402 vma->numab_state = kzalloc(sizeof(struct vma_numab_state), in task_numa_work()
3404 if (!vma->numab_state) in task_numa_work()
3407 vma->numab_state->start_scan_seq = mm->numa_scan_seq; in task_numa_work()
3409 vma->numab_state->next_scan = now + in task_numa_work()
3413 vma->numab_state->pids_active_reset = vma->numab_state->next_scan + in task_numa_work()
3421 vma->numab_state->prev_scan_seq = mm->numa_scan_seq - 1; in task_numa_work()
3428 if (mm->numa_scan_seq && time_before(jiffies, in task_numa_work()
3429 vma->numab_state->next_scan)) { in task_numa_work()
3435 if (mm->numa_scan_seq && in task_numa_work()
3436 time_after(jiffies, vma->numab_state->pids_active_reset)) { in task_numa_work()
3437 vma->numab_state->pids_active_reset = vma->numab_state->pids_active_reset + in task_numa_work()
3439 vma->numab_state->pids_active[0] = READ_ONCE(vma->numab_state->pids_active[1]); in task_numa_work()
3440 vma->numab_state->pids_active[1] = 0; in task_numa_work()
3444 if (vma->numab_state->prev_scan_seq == mm->numa_scan_seq) { in task_numa_work()
3445 mm->numa_scan_offset = vma->vm_end; in task_numa_work()
3461 start = max(start, vma->vm_start); in task_numa_work()
3463 end = min(end, vma->vm_end); in task_numa_work()
3469 * is not already PTE-numa. If the VMA contains in task_numa_work()
3475 pages -= (end - start) >> PAGE_SHIFT; in task_numa_work()
3476 virtpages -= (end - start) >> PAGE_SHIFT; in task_numa_work()
3483 } while (end != vma->vm_end); in task_numa_work()
3486 vma->numab_state->prev_scan_seq = mm->numa_scan_seq; in task_numa_work()
3514 mm->numa_scan_offset = start; in task_numa_work()
3525 if (unlikely(p->se.sum_exec_runtime != runtime)) { in task_numa_work()
3526 u64 diff = p->se.sum_exec_runtime - runtime; in task_numa_work()
3527 p->node_stamp += 32 * diff; in task_numa_work()
3534 struct mm_struct *mm = p->mm; in init_numa_balancing()
3537 mm_users = atomic_read(&mm->mm_users); in init_numa_balancing()
3539 mm->numa_next_scan = jiffies + msecs_to_jiffies(sysctl_numa_balancing_scan_delay); in init_numa_balancing()
3540 mm->numa_scan_seq = 0; in init_numa_balancing()
3543 p->node_stamp = 0; in init_numa_balancing()
3544 p->numa_scan_seq = mm ? mm->numa_scan_seq : 0; in init_numa_balancing()
3545 p->numa_scan_period = sysctl_numa_balancing_scan_delay; in init_numa_balancing()
3546 p->numa_migrate_retry = 0; in init_numa_balancing()
3548 p->numa_work.next = &p->numa_work; in init_numa_balancing()
3549 p->numa_faults = NULL; in init_numa_balancing()
3550 p->numa_pages_migrated = 0; in init_numa_balancing()
3551 p->total_numa_faults = 0; in init_numa_balancing()
3552 RCU_INIT_POINTER(p->numa_group, NULL); in init_numa_balancing()
3553 p->last_task_numa_placement = 0; in init_numa_balancing()
3554 p->last_sum_exec_runtime = 0; in init_numa_balancing()
3556 init_task_work(&p->numa_work, task_numa_work); in init_numa_balancing()
3560 p->numa_preferred_nid = NUMA_NO_NODE; in init_numa_balancing()
3572 current->numa_scan_period * mm_users * NSEC_PER_MSEC); in init_numa_balancing()
3574 p->node_stamp = delay; in init_numa_balancing()
3583 struct callback_head *work = &curr->numa_work; in task_tick_numa()
3589 if (!curr->mm || (curr->flags & (PF_EXITING | PF_KTHREAD)) || work->next != work) in task_tick_numa()
3598 now = curr->se.sum_exec_runtime; in task_tick_numa()
3599 period = (u64)curr->numa_scan_period * NSEC_PER_MSEC; in task_tick_numa()
3601 if (now > curr->node_stamp + period) { in task_tick_numa()
3602 if (!curr->node_stamp) in task_tick_numa()
3603 curr->numa_scan_period = task_scan_start(curr); in task_tick_numa()
3604 curr->node_stamp += period; in task_tick_numa()
3606 if (!time_before(jiffies, curr->mm->numa_next_scan)) in task_tick_numa()
3619 if (!p->mm || !p->numa_faults || (p->flags & PF_EXITING)) in update_scan_period()
3628 * is pulled cross-node due to wakeups or load balancing. in update_scan_period()
3630 if (p->numa_scan_seq) { in update_scan_period()
3636 if (dst_nid == p->numa_preferred_nid || in update_scan_period()
3637 (p->numa_preferred_nid != NUMA_NO_NODE && in update_scan_period()
3638 src_nid != p->numa_preferred_nid)) in update_scan_period()
3642 p->numa_scan_period = task_scan_start(p); in update_scan_period()
3667 update_load_add(&cfs_rq->load, se->load.weight); in account_entity_enqueue()
3673 list_add(&se->group_node, &rq->cfs_tasks); in account_entity_enqueue()
3676 cfs_rq->nr_running++; in account_entity_enqueue()
3678 cfs_rq->idle_nr_running++; in account_entity_enqueue()
3684 update_load_sub(&cfs_rq->load, se->load.weight); in account_entity_dequeue()
3688 list_del_init(&se->group_node); in account_entity_dequeue()
3691 cfs_rq->nr_running--; in account_entity_dequeue()
3693 cfs_rq->idle_nr_running--; in account_entity_dequeue()
3699 * Explicitly do a load-store to ensure the intermediate value never hits
3719 * Explicitly do a load-store to ensure the intermediate value never hits
3727 res = var - val; \
3736 * A variant of sub_positive(), which does not use explicit load-store
3741 *ptr -= min_t(typeof(*ptr), *ptr, _val); \
3748 cfs_rq->avg.load_avg += se->avg.load_avg; in enqueue_load_avg()
3749 cfs_rq->avg.load_sum += se_weight(se) * se->avg.load_sum; in enqueue_load_avg()
3755 sub_positive(&cfs_rq->avg.load_avg, se->avg.load_avg); in dequeue_load_avg()
3756 sub_positive(&cfs_rq->avg.load_sum, se_weight(se) * se->avg.load_sum); in dequeue_load_avg()
3758 cfs_rq->avg.load_sum = max_t(u32, cfs_rq->avg.load_sum, in dequeue_load_avg()
3759 cfs_rq->avg.load_avg * PELT_MIN_DIVIDER); in dequeue_load_avg()
3771 unsigned long old_weight = se->load.weight; in reweight_eevdf()
3776 * -------- in reweight_eevdf()
3779 * adjusted if re-weight at !0-lag point. in reweight_eevdf()
3782 * re-weight without changing vruntime at !0-lag point. in reweight_eevdf()
3784 * Weight VRuntime Avg-VRuntime in reweight_eevdf()
3788 * Since lag needs to be preserved through re-weight: in reweight_eevdf()
3790 * lag = (V - v)*w = (V'- v')*w', where v = v' in reweight_eevdf()
3791 * ==> V' = (V - v)*w/w' + v (1) in reweight_eevdf()
3796 * V' = (WV + w'v - wv) / (W + w' - w) (2) in reweight_eevdf()
3800 * (WV + w'v - wv) / (W + w' - w) = (V - v)*w/w' + v in reweight_eevdf()
3801 * ==> (WV-Wv+Wv+w'v-wv)/(W+w'-w) = (V - v)*w/w' + v in reweight_eevdf()
3802 * ==> (WV - Wv)/(W + w' - w) + v = (V - v)*w/w' + v in reweight_eevdf()
3803 * ==> (V - v)*W/(W + w' - w) = (V - v)*w/w' (3) in reweight_eevdf()
3805 * Since we are doing at !0-lag point which means V != v, we in reweight_eevdf()
3808 * ==> W / (W + w' - w) = w / w' in reweight_eevdf()
3809 * ==> Ww' = Ww + ww' - ww in reweight_eevdf()
3810 * ==> W * (w' - w) = w * (w' - w) in reweight_eevdf()
3811 * ==> W = w (re-weight indicates w' != w) in reweight_eevdf()
3815 * average vruntime @V, which means we will always re-weight in reweight_eevdf()
3816 * at 0-lag point, thus breach assumption. Proof completed. in reweight_eevdf()
3819 * COROLLARY #2: Re-weight does NOT affect weighted average in reweight_eevdf()
3824 * (V - v)*w = (V' - v')*w' in reweight_eevdf()
3825 * ==> v' = V' - (V - v)*w/w' (4) in reweight_eevdf()
3829 * V' = (WV - wv + w'v') / (W - w + w') in reweight_eevdf()
3830 * = (WV - wv + w'(V' - (V - v)w/w')) / (W - w + w') in reweight_eevdf()
3831 * = (WV - wv + w'V' - Vw + wv) / (W - w + w') in reweight_eevdf()
3832 * = (WV + w'V' - Vw) / (W - w + w') in reweight_eevdf()
3834 * ==> V'*(W - w + w') = WV + w'V' - Vw in reweight_eevdf()
3835 * ==> V' * (W - w) = (W - w) * V (5) in reweight_eevdf()
3838 * always occurs at 0-lag point, so V won't change. Or else in reweight_eevdf()
3843 * So according to corollary #1 & #2, the effect of re-weight in reweight_eevdf()
3846 * v' = V' - (V - v) * w / w' (4) in reweight_eevdf()
3847 * = V - (V - v) * w / w' in reweight_eevdf()
3848 * = V - vl * w / w' in reweight_eevdf()
3849 * = V - vl' in reweight_eevdf()
3851 if (avruntime != se->vruntime) { in reweight_eevdf()
3854 se->vruntime = avruntime - vlag; in reweight_eevdf()
3859 * -------- in reweight_eevdf()
3864 * d' = v' + (d - v)*w/w' in reweight_eevdf()
3865 * = V' - (V - v)*w/w' + (d - v)*w/w' in reweight_eevdf()
3866 * = V - (V - v)*w/w' + (d - v)*w/w' in reweight_eevdf()
3867 * = V + (d - V)*w/w' in reweight_eevdf()
3869 vslice = (s64)(se->deadline - avruntime); in reweight_eevdf()
3871 se->deadline = avruntime + vslice; in reweight_eevdf()
3877 bool curr = cfs_rq->curr == se; in reweight_entity()
3880 if (se->on_rq) { in reweight_entity()
3886 update_load_sub(&cfs_rq->load, se->load.weight); in reweight_entity()
3890 if (se->on_rq) { in reweight_entity()
3894 * Because we keep se->vlag = V - v_i, while: lag_i = w_i*(V - v_i), in reweight_entity()
3895 * we need to scale se->vlag when w_i changes. in reweight_entity()
3897 se->vlag = div_s64(se->vlag * se->load.weight, weight); in reweight_entity()
3900 update_load_set(&se->load, weight); in reweight_entity()
3904 u32 divider = get_pelt_divider(&se->avg); in reweight_entity()
3906 se->avg.load_avg = div_u64(se_weight(se) * se->avg.load_sum, divider); in reweight_entity()
3911 if (se->on_rq) { in reweight_entity()
3912 update_load_add(&cfs_rq->load, se->load.weight); in reweight_entity()
3918 * whether the rq-wide min_vruntime needs updated too. Since in reweight_entity()
3920 * than up-to-date one, we do the update at the end of the in reweight_entity()
3930 struct sched_entity *se = &p->se; in reweight_task_fair()
3932 struct load_weight *load = &se->load; in reweight_task_fair()
3934 reweight_entity(cfs_rq, se, lw->weight); in reweight_task_fair()
3935 load->inv_weight = lw->inv_weight; in reweight_task_fair()
3949 * tg->weight * grq->load.weight
3950 * ge->load.weight = ----------------------------- (1)
3951 * \Sum grq->load.weight
3959 * grq->load.weight -> grq->avg.load_avg (2)
3963 * tg->weight * grq->avg.load_avg
3964 * ge->load.weight = ------------------------------ (3)
3965 * tg->load_avg
3967 * Where: tg->load_avg ~= \Sum grq->avg.load_avg
3971 * The problem with it is that because the average is slow -- it was designed
3972 * to be exactly that of course -- this leads to transients in boundary
3974 * one task. It takes time for our CPU's grq->avg.load_avg to build up,
3979 * tg->weight * grq->load.weight
3980 * ge->load.weight = ----------------------------- = tg->weight (4)
3981 * grp->load.weight
3988 * ge->load.weight =
3990 * tg->weight * grq->load.weight
3991 * --------------------------------------------------- (5)
3992 * tg->load_avg - grq->avg.load_avg + grq->load.weight
3994 * But because grq->load.weight can drop to 0, resulting in a divide by zero,
3995 * we need to use grq->avg.load_avg as its lower bound, which then gives:
3998 * tg->weight * grq->load.weight
3999 * ge->load.weight = ----------------------------- (6)
4004 * tg_load_avg' = tg->load_avg - grq->avg.load_avg +
4005 * max(grq->load.weight, grq->avg.load_avg)
4009 * overestimates the ge->load.weight and therefore:
4011 * \Sum ge->load.weight >= tg->weight
4018 struct task_group *tg = cfs_rq->tg; in calc_group_shares()
4020 tg_shares = READ_ONCE(tg->shares); in calc_group_shares()
4022 load = max(scale_load_down(cfs_rq->load.weight), cfs_rq->avg.load_avg); in calc_group_shares()
4024 tg_weight = atomic_long_read(&tg->load_avg); in calc_group_shares()
4027 tg_weight -= cfs_rq->tg_load_avg_contrib; in calc_group_shares()
4035 * MIN_SHARES has to be unscaled here to support per-CPU partitioning in calc_group_shares()
4036 * of a group with small tg->shares value. It is a floor value which is in calc_group_shares()
4040 * E.g. on 64-bit for a group with tg->shares of scale_load(15)=15*1024 in calc_group_shares()
4041 * on an 8-core system with 8 tasks each runnable on one CPU shares has in calc_group_shares()
4066 shares = READ_ONCE(gcfs_rq->tg->shares); in update_cfs_group()
4070 if (unlikely(se->load.weight != shares)) in update_cfs_group()
4084 if (&rq->cfs == cfs_rq) { in cfs_rq_util_change()
4094 * As is, the util number is not freq-invariant (we'd have to in cfs_rq_util_change()
4106 if (sa->load_sum) in load_avg_is_decayed()
4109 if (sa->util_sum) in load_avg_is_decayed()
4112 if (sa->runnable_sum) in load_avg_is_decayed()
4120 SCHED_WARN_ON(sa->load_avg || in load_avg_is_decayed()
4121 sa->util_avg || in load_avg_is_decayed()
4122 sa->runnable_avg); in load_avg_is_decayed()
4129 return u64_u32_load_copy(cfs_rq->avg.last_update_time, in cfs_rq_last_update_time()
4130 cfs_rq->last_update_time_copy); in cfs_rq_last_update_time()
4136 * bottom-up, we only have to test whether the cfs_rq before us on the list
4146 if (cfs_rq->on_list) { in child_cfs_rq_on_list()
4147 prev = cfs_rq->leaf_cfs_rq_list.prev; in child_cfs_rq_on_list()
4151 prev = rq->tmp_alone_branch; in child_cfs_rq_on_list()
4156 return (prev_cfs_rq->tg->parent == cfs_rq->tg); in child_cfs_rq_on_list()
4161 if (cfs_rq->load.weight) in cfs_rq_is_decayed()
4164 if (!load_avg_is_decayed(&cfs_rq->avg)) in cfs_rq_is_decayed()
4174 * update_tg_load_avg - update the tg's load avg
4177 * This function 'ensures': tg->load_avg := \Sum tg->cfs_rq[]->avg.load.
4178 * However, because tg->load_avg is a global value there are performance
4195 if (cfs_rq->tg == &root_task_group) in update_tg_load_avg()
4203 * For migration heavy workloads, access to tg->load_avg can be in update_tg_load_avg()
4204 * unbound. Limit the update rate to at most once per ms. in update_tg_load_avg()
4207 if (now - cfs_rq->last_update_tg_load_avg < NSEC_PER_MSEC) in update_tg_load_avg()
4210 delta = cfs_rq->avg.load_avg - cfs_rq->tg_load_avg_contrib; in update_tg_load_avg()
4211 if (abs(delta) > cfs_rq->tg_load_avg_contrib / 64) { in update_tg_load_avg()
4212 atomic_long_add(delta, &cfs_rq->tg->load_avg); in update_tg_load_avg()
4213 cfs_rq->tg_load_avg_contrib = cfs_rq->avg.load_avg; in update_tg_load_avg()
4214 cfs_rq->last_update_tg_load_avg = now; in update_tg_load_avg()
4226 if (cfs_rq->tg == &root_task_group) in clear_tg_load_avg()
4230 delta = 0 - cfs_rq->tg_load_avg_contrib; in clear_tg_load_avg()
4231 atomic_long_add(delta, &cfs_rq->tg->load_avg); in clear_tg_load_avg()
4232 cfs_rq->tg_load_avg_contrib = 0; in clear_tg_load_avg()
4233 cfs_rq->last_update_tg_load_avg = now; in clear_tg_load_avg()
4252 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in clear_tg_offline_cfs_rqs()
4263 * caller only guarantees p->pi_lock is held; no other assumptions,
4264 * including the state of rq->lock, should be made.
4278 * getting what current time is, so simply throw away the out-of-date in set_task_rq_fair()
4282 if (!(se->avg.last_update_time && prev)) in set_task_rq_fair()
4289 se->avg.last_update_time = n_last_update_time; in set_task_rq_fair()
4297 * ge->avg == grq->avg (1)
4308 * ge->avg.load_avg = ge->load.weight * ge->avg.runnable_avg (2)
4313 * grq->avg.load_avg = grq->load.weight * grq->avg.runnable_avg (3)
4317 * ge->avg.runnable_avg == grq->avg.runnable_avg
4321 * ge->load.weight * grq->avg.load_avg
4322 * ge->avg.load_avg = ----------------------------------- (4)
4323 * grq->load.weight
4336 * Another reason this doesn't work is that runnable isn't a 0-sum entity.
4347 * ge->avg.running_sum <= ge->avg.runnable_sum <= LOAD_AVG_MAX
4354 * grq->avg.runnable_sum = grq->avg.load_sum / grq->load.weight
4362 long delta_sum, delta_avg = gcfs_rq->avg.util_avg - se->avg.util_avg; in update_tg_cfs_util()
4370 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in update_tg_cfs_util()
4373 divider = get_pelt_divider(&cfs_rq->avg); in update_tg_cfs_util()
4377 se->avg.util_avg = gcfs_rq->avg.util_avg; in update_tg_cfs_util()
4378 new_sum = se->avg.util_avg * divider; in update_tg_cfs_util()
4379 delta_sum = (long)new_sum - (long)se->avg.util_sum; in update_tg_cfs_util()
4380 se->avg.util_sum = new_sum; in update_tg_cfs_util()
4383 add_positive(&cfs_rq->avg.util_avg, delta_avg); in update_tg_cfs_util()
4384 add_positive(&cfs_rq->avg.util_sum, delta_sum); in update_tg_cfs_util()
4387 cfs_rq->avg.util_sum = max_t(u32, cfs_rq->avg.util_sum, in update_tg_cfs_util()
4388 cfs_rq->avg.util_avg * PELT_MIN_DIVIDER); in update_tg_cfs_util()
4394 long delta_sum, delta_avg = gcfs_rq->avg.runnable_avg - se->avg.runnable_avg; in update_tg_cfs_runnable()
4402 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in update_tg_cfs_runnable()
4405 divider = get_pelt_divider(&cfs_rq->avg); in update_tg_cfs_runnable()
4408 se->avg.runnable_avg = gcfs_rq->avg.runnable_avg; in update_tg_cfs_runnable()
4409 new_sum = se->avg.runnable_avg * divider; in update_tg_cfs_runnable()
4410 delta_sum = (long)new_sum - (long)se->avg.runnable_sum; in update_tg_cfs_runnable()
4411 se->avg.runnable_sum = new_sum; in update_tg_cfs_runnable()
4414 add_positive(&cfs_rq->avg.runnable_avg, delta_avg); in update_tg_cfs_runnable()
4415 add_positive(&cfs_rq->avg.runnable_sum, delta_sum); in update_tg_cfs_runnable()
4417 cfs_rq->avg.runnable_sum = max_t(u32, cfs_rq->avg.runnable_sum, in update_tg_cfs_runnable()
4418 cfs_rq->avg.runnable_avg * PELT_MIN_DIVIDER); in update_tg_cfs_runnable()
4424 long delta_avg, running_sum, runnable_sum = gcfs_rq->prop_runnable_sum; in update_tg_cfs_load()
4433 gcfs_rq->prop_runnable_sum = 0; in update_tg_cfs_load()
4436 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in update_tg_cfs_load()
4439 divider = get_pelt_divider(&cfs_rq->avg); in update_tg_cfs_load()
4446 runnable_sum += se->avg.load_sum; in update_tg_cfs_load()
4453 if (scale_load_down(gcfs_rq->load.weight)) { in update_tg_cfs_load()
4454 load_sum = div_u64(gcfs_rq->avg.load_sum, in update_tg_cfs_load()
4455 scale_load_down(gcfs_rq->load.weight)); in update_tg_cfs_load()
4459 runnable_sum = min(se->avg.load_sum, load_sum); in update_tg_cfs_load()
4468 running_sum = se->avg.util_sum >> SCHED_CAPACITY_SHIFT; in update_tg_cfs_load()
4474 delta_avg = load_avg - se->avg.load_avg; in update_tg_cfs_load()
4478 delta_sum = load_sum - (s64)se_weight(se) * se->avg.load_sum; in update_tg_cfs_load()
4480 se->avg.load_sum = runnable_sum; in update_tg_cfs_load()
4481 se->avg.load_avg = load_avg; in update_tg_cfs_load()
4482 add_positive(&cfs_rq->avg.load_avg, delta_avg); in update_tg_cfs_load()
4483 add_positive(&cfs_rq->avg.load_sum, delta_sum); in update_tg_cfs_load()
4485 cfs_rq->avg.load_sum = max_t(u32, cfs_rq->avg.load_sum, in update_tg_cfs_load()
4486 cfs_rq->avg.load_avg * PELT_MIN_DIVIDER); in update_tg_cfs_load()
4491 cfs_rq->propagate = 1; in add_tg_cfs_propagate()
4492 cfs_rq->prop_runnable_sum += runnable_sum; in add_tg_cfs_propagate()
4504 if (!gcfs_rq->propagate) in propagate_entity_load_avg()
4507 gcfs_rq->propagate = 0; in propagate_entity_load_avg()
4511 add_tg_cfs_propagate(cfs_rq, gcfs_rq->prop_runnable_sum); in propagate_entity_load_avg()
4535 if (se->avg.load_avg || se->avg.util_avg) in skip_blocked_update()
4542 if (gcfs_rq->propagate) in skip_blocked_update()
4576 if (load_avg_is_decayed(&se->avg)) in migrate_se_pelt_lag()
4583 is_idle = is_idle_task(rcu_dereference(rq->curr)); in migrate_se_pelt_lag()
4600 * - cfs->throttled_clock_pelt_time@cfs_rq_idle in migrate_se_pelt_lag()
4603 * = rq_clock_pelt()@rq_idle - rq_clock_pelt()@cfs_rq_idle in migrate_se_pelt_lag()
4606 * = sched_clock_cpu() - rq_clock()@rq_idle in migrate_se_pelt_lag()
4610 * now = rq_clock_pelt()@rq_idle - cfs->throttled_clock_pelt_time + in migrate_se_pelt_lag()
4611 * sched_clock_cpu() - rq_clock()@rq_idle in migrate_se_pelt_lag()
4613 * rq_clock_pelt()@rq_idle is rq->clock_pelt_idle in migrate_se_pelt_lag()
4614 * rq_clock()@rq_idle is rq->clock_idle in migrate_se_pelt_lag()
4615 * cfs->throttled_clock_pelt_time@cfs_rq_idle in migrate_se_pelt_lag()
4616 * is cfs_rq->throttled_pelt_idle in migrate_se_pelt_lag()
4620 throttled = u64_u32_load(cfs_rq->throttled_pelt_idle); in migrate_se_pelt_lag()
4625 now = u64_u32_load(rq->clock_pelt_idle); in migrate_se_pelt_lag()
4635 now -= throttled; in migrate_se_pelt_lag()
4638 * cfs_rq->avg.last_update_time is more recent than our in migrate_se_pelt_lag()
4643 now += sched_clock_cpu(cpu_of(rq)) - u64_u32_load(rq->clock_idle); in migrate_se_pelt_lag()
4652 * update_cfs_rq_load_avg - update the cfs_rq's load/util averages
4659 * cfs_rq->avg is used for task_h_load() and update_cfs_share() for example.
4663 * Since both these conditions indicate a changed cfs_rq->avg.load we should
4670 struct sched_avg *sa = &cfs_rq->avg; in update_cfs_rq_load_avg()
4673 if (cfs_rq->removed.nr) { in update_cfs_rq_load_avg()
4675 u32 divider = get_pelt_divider(&cfs_rq->avg); in update_cfs_rq_load_avg()
4677 raw_spin_lock(&cfs_rq->removed.lock); in update_cfs_rq_load_avg()
4678 swap(cfs_rq->removed.util_avg, removed_util); in update_cfs_rq_load_avg()
4679 swap(cfs_rq->removed.load_avg, removed_load); in update_cfs_rq_load_avg()
4680 swap(cfs_rq->removed.runnable_avg, removed_runnable); in update_cfs_rq_load_avg()
4681 cfs_rq->removed.nr = 0; in update_cfs_rq_load_avg()
4682 raw_spin_unlock(&cfs_rq->removed.lock); in update_cfs_rq_load_avg()
4685 sub_positive(&sa->load_avg, r); in update_cfs_rq_load_avg()
4686 sub_positive(&sa->load_sum, r * divider); in update_cfs_rq_load_avg()
4687 /* See sa->util_sum below */ in update_cfs_rq_load_avg()
4688 sa->load_sum = max_t(u32, sa->load_sum, sa->load_avg * PELT_MIN_DIVIDER); in update_cfs_rq_load_avg()
4691 sub_positive(&sa->util_avg, r); in update_cfs_rq_load_avg()
4692 sub_positive(&sa->util_sum, r * divider); in update_cfs_rq_load_avg()
4694 * Because of rounding, se->util_sum might ends up being +1 more than in update_cfs_rq_load_avg()
4695 * cfs->util_sum. Although this is not a problem by itself, detaching in update_cfs_rq_load_avg()
4697 * util_avg (~1ms) can make cfs->util_sum becoming null whereas in update_cfs_rq_load_avg()
4704 sa->util_sum = max_t(u32, sa->util_sum, sa->util_avg * PELT_MIN_DIVIDER); in update_cfs_rq_load_avg()
4707 sub_positive(&sa->runnable_avg, r); in update_cfs_rq_load_avg()
4708 sub_positive(&sa->runnable_sum, r * divider); in update_cfs_rq_load_avg()
4709 /* See sa->util_sum above */ in update_cfs_rq_load_avg()
4710 sa->runnable_sum = max_t(u32, sa->runnable_sum, in update_cfs_rq_load_avg()
4711 sa->runnable_avg * PELT_MIN_DIVIDER); in update_cfs_rq_load_avg()
4718 -(long)(removed_runnable * divider) >> SCHED_CAPACITY_SHIFT); in update_cfs_rq_load_avg()
4724 u64_u32_store_copy(sa->last_update_time, in update_cfs_rq_load_avg()
4725 cfs_rq->last_update_time_copy, in update_cfs_rq_load_avg()
4726 sa->last_update_time); in update_cfs_rq_load_avg()
4731 * attach_entity_load_avg - attach this entity to its cfs_rq load avg
4736 * cfs_rq->avg.last_update_time being current.
4741 * cfs_rq->avg.period_contrib can be used for both cfs_rq and se. in attach_entity_load_avg()
4744 u32 divider = get_pelt_divider(&cfs_rq->avg); in attach_entity_load_avg()
4753 se->avg.last_update_time = cfs_rq->avg.last_update_time; in attach_entity_load_avg()
4754 se->avg.period_contrib = cfs_rq->avg.period_contrib; in attach_entity_load_avg()
4762 se->avg.util_sum = se->avg.util_avg * divider; in attach_entity_load_avg()
4764 se->avg.runnable_sum = se->avg.runnable_avg * divider; in attach_entity_load_avg()
4766 se->avg.load_sum = se->avg.load_avg * divider; in attach_entity_load_avg()
4767 if (se_weight(se) < se->avg.load_sum) in attach_entity_load_avg()
4768 se->avg.load_sum = div_u64(se->avg.load_sum, se_weight(se)); in attach_entity_load_avg()
4770 se->avg.load_sum = 1; in attach_entity_load_avg()
4773 cfs_rq->avg.util_avg += se->avg.util_avg; in attach_entity_load_avg()
4774 cfs_rq->avg.util_sum += se->avg.util_sum; in attach_entity_load_avg()
4775 cfs_rq->avg.runnable_avg += se->avg.runnable_avg; in attach_entity_load_avg()
4776 cfs_rq->avg.runnable_sum += se->avg.runnable_sum; in attach_entity_load_avg()
4778 add_tg_cfs_propagate(cfs_rq, se->avg.load_sum); in attach_entity_load_avg()
4786 * detach_entity_load_avg - detach this entity from its cfs_rq load avg
4791 * cfs_rq->avg.last_update_time being current.
4796 sub_positive(&cfs_rq->avg.util_avg, se->avg.util_avg); in detach_entity_load_avg()
4797 sub_positive(&cfs_rq->avg.util_sum, se->avg.util_sum); in detach_entity_load_avg()
4799 cfs_rq->avg.util_sum = max_t(u32, cfs_rq->avg.util_sum, in detach_entity_load_avg()
4800 cfs_rq->avg.util_avg * PELT_MIN_DIVIDER); in detach_entity_load_avg()
4802 sub_positive(&cfs_rq->avg.runnable_avg, se->avg.runnable_avg); in detach_entity_load_avg()
4803 sub_positive(&cfs_rq->avg.runnable_sum, se->avg.runnable_sum); in detach_entity_load_avg()
4805 cfs_rq->avg.runnable_sum = max_t(u32, cfs_rq->avg.runnable_sum, in detach_entity_load_avg()
4806 cfs_rq->avg.runnable_avg * PELT_MIN_DIVIDER); in detach_entity_load_avg()
4808 add_tg_cfs_propagate(cfs_rq, -se->avg.load_sum); in detach_entity_load_avg()
4833 if (se->avg.last_update_time && !(flags & SKIP_AGE_LOAD)) in update_load_avg()
4839 if (!se->avg.last_update_time && (flags & DO_ATTACH)) { in update_load_avg()
4889 * tasks cannot exit without having gone through wake_up_new_task() -> in remove_entity_load_avg()
4896 raw_spin_lock_irqsave(&cfs_rq->removed.lock, flags); in remove_entity_load_avg()
4897 ++cfs_rq->removed.nr; in remove_entity_load_avg()
4898 cfs_rq->removed.util_avg += se->avg.util_avg; in remove_entity_load_avg()
4899 cfs_rq->removed.load_avg += se->avg.load_avg; in remove_entity_load_avg()
4900 cfs_rq->removed.runnable_avg += se->avg.runnable_avg; in remove_entity_load_avg()
4901 raw_spin_unlock_irqrestore(&cfs_rq->removed.lock, flags); in remove_entity_load_avg()
4906 return cfs_rq->avg.runnable_avg; in cfs_rq_runnable_avg()
4911 return cfs_rq->avg.load_avg; in cfs_rq_load_avg()
4918 return READ_ONCE(p->se.avg.util_avg); in task_util()
4923 return READ_ONCE(p->se.avg.runnable_avg); in task_runnable()
4928 return READ_ONCE(p->se.avg.util_est) & ~UTIL_AVG_UNCHANGED; in _task_util_est()
4945 enqueued = cfs_rq->avg.util_est; in util_est_enqueue()
4947 WRITE_ONCE(cfs_rq->avg.util_est, enqueued); in util_est_enqueue()
4961 enqueued = cfs_rq->avg.util_est; in util_est_dequeue()
4962 enqueued -= min_t(unsigned int, enqueued, _task_util_est(p)); in util_est_dequeue()
4963 WRITE_ONCE(cfs_rq->avg.util_est, enqueued); in util_est_dequeue()
4987 ewma = READ_ONCE(p->se.avg.util_est); in util_est_update()
5012 last_ewma_diff = ewma - dequeued; in util_est_update()
5038 * ewma(t) = w * task_util(p) + (1-w) * ewma(t-1) in util_est_update()
5039 * = w * task_util(p) + ewma(t-1) - w * ewma(t-1) in util_est_update()
5040 * = w * (task_util(p) - ewma(t-1)) + ewma(t-1) in util_est_update()
5041 * = w * ( -last_ewma_diff ) + ewma(t-1) in util_est_update()
5042 * = w * (-last_ewma_diff + ewma(t-1) / w) in util_est_update()
5048 ewma -= last_ewma_diff; in util_est_update()
5052 WRITE_ONCE(p->se.avg.util_est, ewma); in util_est_update()
5054 trace_sched_util_est_se_tp(&p->se); in util_est_update()
5061 capacity -= max(hw_load_avg(cpu_rq(cpu)), cpufreq_get_pressure(cpu)); in get_actual_cpu_capacity()
5119 * +---------------------------------------- in util_fits_cpu()
5157 * +---------------------------------------- in util_fits_cpu()
5180 return -1; in util_fits_cpu()
5208 if (!p || (p->nr_cpus_allowed == 1) || in update_misfit_status()
5209 (arch_scale_cpu_capacity(cpu) == p->max_allowed_capacity) || in update_misfit_status()
5212 rq->misfit_task_load = 0; in update_misfit_status()
5220 rq->misfit_task_load = max_t(unsigned long, task_h_load(p), 1); in update_misfit_status()
5227 return !cfs_rq->nr_running; in cfs_rq_is_decayed()
5271 if (!se->custom_slice) in place_entity()
5272 se->slice = sysctl_sched_base_slice; in place_entity()
5273 vslice = calc_delta_fair(se->slice, se); in place_entity()
5283 if (sched_feat(PLACE_LAG) && cfs_rq->nr_running) { in place_entity()
5284 struct sched_entity *curr = cfs_rq->curr; in place_entity()
5287 lag = se->vlag; in place_entity()
5297 * lag_i = S - s_i = w_i * (V - v_i) in place_entity()
5302 * vl_i = V - v_i <=> v_i = V - vl_i in place_entity()
5314 * = (W*V + w_i*(V - vl_i)) / (W + w_i) in place_entity()
5315 * = (W*V + w_i*V - w_i*vl_i) / (W + w_i) in place_entity()
5316 * = (V*(W + w_i) - w_i*l) / (W + w_i) in place_entity()
5317 * = V - w_i*vl_i / (W + w_i) in place_entity()
5321 * vl'_i = V' - v_i in place_entity()
5322 * = V - w_i*vl_i / (W + w_i) - (V - vl_i) in place_entity()
5323 * = vl_i - w_i*vl_i / (W + w_i) in place_entity()
5333 * vl'_i = vl_i - w_i*vl_i / (W + w_i) in place_entity()
5334 * = ((W + w_i)*vl_i - w_i*vl_i) / (W + w_i) in place_entity()
5336 * (W + w_i)*vl'_i = (W + w_i)*vl_i - w_i*vl_i in place_entity()
5341 load = cfs_rq->avg_load; in place_entity()
5342 if (curr && curr->on_rq) in place_entity()
5343 load += scale_load_down(curr->load.weight); in place_entity()
5345 lag *= load + scale_load_down(se->load.weight); in place_entity()
5351 se->vruntime = vruntime - lag; in place_entity()
5353 if (sched_feat(PLACE_REL_DEADLINE) && se->rel_deadline) { in place_entity()
5354 se->deadline += se->vruntime; in place_entity()
5355 se->rel_deadline = 0; in place_entity()
5370 se->deadline = se->vruntime + vslice; in place_entity()
5384 bool curr = cfs_rq->curr == se; in enqueue_entity()
5397 * - Update loads to have both entity and cfs_rq synced with now. in enqueue_entity()
5398 * - For group_entity, update its runnable_weight to reflect the new in enqueue_entity()
5400 * - For group_entity, update its weight to reflect the new share of in enqueue_entity()
5402 * - Add its new weight to cfs_rq->load.weight in enqueue_entity()
5408 * but update_cfs_group() here will re-adjust the weight and have to in enqueue_entity()
5414 * XXX now that the entity has been re-weighted, and it's lag adjusted, in enqueue_entity()
5424 se->exec_start = 0; in enqueue_entity()
5430 se->on_rq = 1; in enqueue_entity()
5432 if (cfs_rq->nr_running == 1) { in enqueue_entity()
5440 if (cfs_rq_throttled(cfs_rq) && !cfs_rq->throttled_clock) in enqueue_entity()
5441 cfs_rq->throttled_clock = rq_clock(rq); in enqueue_entity()
5442 if (!cfs_rq->throttled_clock_self) in enqueue_entity()
5443 cfs_rq->throttled_clock_self = rq_clock(rq); in enqueue_entity()
5453 if (cfs_rq->next != se) in __clear_buddies_next()
5456 cfs_rq->next = NULL; in __clear_buddies_next()
5462 if (cfs_rq->next == se) in clear_buddies()
5470 se->sched_delayed = 0; in finish_delayed_dequeue_entity()
5471 if (sched_feat(DELAY_ZERO) && se->vlag > 0) in finish_delayed_dequeue_entity()
5472 se->vlag = 0; in finish_delayed_dequeue_entity()
5483 SCHED_WARN_ON(!se->sched_delayed); in dequeue_entity()
5493 SCHED_WARN_ON(delay && se->sched_delayed); in dequeue_entity()
5497 if (cfs_rq->next == se) in dequeue_entity()
5498 cfs_rq->next = NULL; in dequeue_entity()
5500 se->sched_delayed = 1; in dequeue_entity()
5511 * - Update loads to have both entity and cfs_rq synced with now. in dequeue_entity()
5512 * - For group_entity, update its runnable_weight to reflect the new in dequeue_entity()
5514 * - Subtract its previous weight from cfs_rq->load.weight. in dequeue_entity()
5515 * - For group entity, update its weight to reflect the new share in dequeue_entity()
5527 se->deadline -= se->vruntime; in dequeue_entity()
5528 se->rel_deadline = 1; in dequeue_entity()
5531 if (se != cfs_rq->curr) in dequeue_entity()
5533 se->on_rq = 0; in dequeue_entity()
5545 * further than we started -- i.e. we'll be penalized. in dequeue_entity()
5553 if (cfs_rq->nr_running == 0) in dequeue_entity()
5565 if (se->on_rq) { in set_next_entity()
5578 se->vlag = se->deadline; in set_next_entity()
5582 SCHED_WARN_ON(cfs_rq->curr); in set_next_entity()
5583 cfs_rq->curr = se; in set_next_entity()
5588 * when there are only lesser-weight tasks around): in set_next_entity()
5591 rq_of(cfs_rq)->cfs.load.weight >= 2*se->load.weight) { in set_next_entity()
5595 __schedstat_set(stats->slice_max, in set_next_entity()
5596 max((u64)stats->slice_max, in set_next_entity()
5597 se->sum_exec_runtime - se->prev_sum_exec_runtime)); in set_next_entity()
5600 se->prev_sum_exec_runtime = se->sum_exec_runtime; in set_next_entity()
5619 cfs_rq->next && entity_eligible(cfs_rq, cfs_rq->next)) { in pick_next_entity()
5620 /* ->next will never be delayed */ in pick_next_entity()
5621 SCHED_WARN_ON(cfs_rq->next->sched_delayed); in pick_next_entity()
5622 return cfs_rq->next; in pick_next_entity()
5626 if (se->sched_delayed) { in pick_next_entity()
5644 if (prev->on_rq) in put_prev_entity()
5647 /* throttle cfs_rqs exceeding runtime */ in put_prev_entity()
5650 if (prev->on_rq) { in put_prev_entity()
5657 SCHED_WARN_ON(cfs_rq->curr != prev); in put_prev_entity()
5658 cfs_rq->curr = NULL; in put_prev_entity()
5665 * Update run-time statistics of the 'current'. in entity_tick()
5688 hrtimer_active(&rq_of(cfs_rq)->hrtick_timer)) in entity_tick()
5743 * directly instead of rq->clock to avoid adding additional synchronization
5744 * around rq->lock.
5746 * requires cfs_b->lock
5752 if (unlikely(cfs_b->quota == RUNTIME_INF)) in __refill_cfs_bandwidth_runtime()
5755 cfs_b->runtime += cfs_b->quota; in __refill_cfs_bandwidth_runtime()
5756 runtime = cfs_b->runtime_snap - cfs_b->runtime; in __refill_cfs_bandwidth_runtime()
5758 cfs_b->burst_time += runtime; in __refill_cfs_bandwidth_runtime()
5759 cfs_b->nr_burst++; in __refill_cfs_bandwidth_runtime()
5762 cfs_b->runtime = min(cfs_b->runtime, cfs_b->quota + cfs_b->burst); in __refill_cfs_bandwidth_runtime()
5763 cfs_b->runtime_snap = cfs_b->runtime; in __refill_cfs_bandwidth_runtime()
5768 return &tg->cfs_bandwidth; in tg_cfs_bandwidth()
5777 lockdep_assert_held(&cfs_b->lock); in __assign_cfs_rq_runtime()
5780 min_amount = target_runtime - cfs_rq->runtime_remaining; in __assign_cfs_rq_runtime()
5782 if (cfs_b->quota == RUNTIME_INF) in __assign_cfs_rq_runtime()
5787 if (cfs_b->runtime > 0) { in __assign_cfs_rq_runtime()
5788 amount = min(cfs_b->runtime, min_amount); in __assign_cfs_rq_runtime()
5789 cfs_b->runtime -= amount; in __assign_cfs_rq_runtime()
5790 cfs_b->idle = 0; in __assign_cfs_rq_runtime()
5794 cfs_rq->runtime_remaining += amount; in __assign_cfs_rq_runtime()
5796 return cfs_rq->runtime_remaining > 0; in __assign_cfs_rq_runtime()
5802 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in assign_cfs_rq_runtime()
5805 raw_spin_lock(&cfs_b->lock); in assign_cfs_rq_runtime()
5807 raw_spin_unlock(&cfs_b->lock); in assign_cfs_rq_runtime()
5815 cfs_rq->runtime_remaining -= delta_exec; in __account_cfs_rq_runtime()
5817 if (likely(cfs_rq->runtime_remaining > 0)) in __account_cfs_rq_runtime()
5820 if (cfs_rq->throttled) in __account_cfs_rq_runtime()
5826 if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr)) in __account_cfs_rq_runtime()
5833 if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled) in account_cfs_rq_runtime()
5841 return cfs_bandwidth_used() && cfs_rq->throttled; in cfs_rq_throttled()
5847 return cfs_bandwidth_used() && cfs_rq->throttle_count; in throttled_hierarchy()
5853 * load-balance operations.
5860 src_cfs_rq = tg->cfs_rq[src_cpu]; in throttled_lb_pair()
5861 dest_cfs_rq = tg->cfs_rq[dest_cpu]; in throttled_lb_pair()
5870 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in tg_unthrottle_up()
5872 cfs_rq->throttle_count--; in tg_unthrottle_up()
5873 if (!cfs_rq->throttle_count) { in tg_unthrottle_up()
5874 cfs_rq->throttled_clock_pelt_time += rq_clock_pelt(rq) - in tg_unthrottle_up()
5875 cfs_rq->throttled_clock_pelt; in tg_unthrottle_up()
5881 if (cfs_rq->throttled_clock_self) { in tg_unthrottle_up()
5882 u64 delta = rq_clock(rq) - cfs_rq->throttled_clock_self; in tg_unthrottle_up()
5884 cfs_rq->throttled_clock_self = 0; in tg_unthrottle_up()
5889 cfs_rq->throttled_clock_self_time += delta; in tg_unthrottle_up()
5899 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in tg_throttle_down()
5902 if (!cfs_rq->throttle_count) { in tg_throttle_down()
5903 cfs_rq->throttled_clock_pelt = rq_clock_pelt(rq); in tg_throttle_down()
5906 SCHED_WARN_ON(cfs_rq->throttled_clock_self); in tg_throttle_down()
5907 if (cfs_rq->nr_running) in tg_throttle_down()
5908 cfs_rq->throttled_clock_self = rq_clock(rq); in tg_throttle_down()
5910 cfs_rq->throttle_count++; in tg_throttle_down()
5918 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in throttle_cfs_rq()
5921 long rq_h_nr_running = rq->cfs.h_nr_running; in throttle_cfs_rq()
5923 raw_spin_lock(&cfs_b->lock); in throttle_cfs_rq()
5930 * subsequent check_cfs_rq_runtime calls agree not to throttle in throttle_cfs_rq()
5936 list_add_tail_rcu(&cfs_rq->throttled_list, in throttle_cfs_rq()
5937 &cfs_b->throttled_cfs_rq); in throttle_cfs_rq()
5939 raw_spin_unlock(&cfs_b->lock); in throttle_cfs_rq()
5942 return false; /* Throttle no longer required. */ in throttle_cfs_rq()
5944 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))]; in throttle_cfs_rq()
5948 walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq); in throttle_cfs_rq()
5951 task_delta = cfs_rq->h_nr_running; in throttle_cfs_rq()
5952 idle_task_delta = cfs_rq->idle_h_nr_running; in throttle_cfs_rq()
5957 /* throttled entity or throttle-on-deactivate */ in throttle_cfs_rq()
5958 if (!se->on_rq) in throttle_cfs_rq()
5967 if (se->sched_delayed) in throttle_cfs_rq()
5972 idle_task_delta = cfs_rq->h_nr_running; in throttle_cfs_rq()
5974 qcfs_rq->h_nr_running -= task_delta; in throttle_cfs_rq()
5975 qcfs_rq->idle_h_nr_running -= idle_task_delta; in throttle_cfs_rq()
5977 if (qcfs_rq->load.weight) { in throttle_cfs_rq()
5978 /* Avoid re-evaluating load for this entity: */ in throttle_cfs_rq()
5986 /* throttled entity or throttle-on-deactivate */ in throttle_cfs_rq()
5987 if (!se->on_rq) in throttle_cfs_rq()
5994 idle_task_delta = cfs_rq->h_nr_running; in throttle_cfs_rq()
5996 qcfs_rq->h_nr_running -= task_delta; in throttle_cfs_rq()
5997 qcfs_rq->idle_h_nr_running -= idle_task_delta; in throttle_cfs_rq()
6004 if (rq_h_nr_running && !rq->cfs.h_nr_running) in throttle_cfs_rq()
6005 dl_server_stop(&rq->fair_server); in throttle_cfs_rq()
6009 * throttled-list. rq->lock protects completion. in throttle_cfs_rq()
6011 cfs_rq->throttled = 1; in throttle_cfs_rq()
6012 SCHED_WARN_ON(cfs_rq->throttled_clock); in throttle_cfs_rq()
6013 if (cfs_rq->nr_running) in throttle_cfs_rq()
6014 cfs_rq->throttled_clock = rq_clock(rq); in throttle_cfs_rq()
6021 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in unthrottle_cfs_rq()
6024 long rq_h_nr_running = rq->cfs.h_nr_running; in unthrottle_cfs_rq()
6026 se = cfs_rq->tg->se[cpu_of(rq)]; in unthrottle_cfs_rq()
6028 cfs_rq->throttled = 0; in unthrottle_cfs_rq()
6032 raw_spin_lock(&cfs_b->lock); in unthrottle_cfs_rq()
6033 if (cfs_rq->throttled_clock) { in unthrottle_cfs_rq()
6034 cfs_b->throttled_time += rq_clock(rq) - cfs_rq->throttled_clock; in unthrottle_cfs_rq()
6035 cfs_rq->throttled_clock = 0; in unthrottle_cfs_rq()
6037 list_del_rcu(&cfs_rq->throttled_list); in unthrottle_cfs_rq()
6038 raw_spin_unlock(&cfs_b->lock); in unthrottle_cfs_rq()
6040 /* update hierarchical throttle state */ in unthrottle_cfs_rq()
6041 walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq); in unthrottle_cfs_rq()
6043 if (!cfs_rq->load.weight) { in unthrottle_cfs_rq()
6044 if (!cfs_rq->on_list) in unthrottle_cfs_rq()
6057 task_delta = cfs_rq->h_nr_running; in unthrottle_cfs_rq()
6058 idle_task_delta = cfs_rq->idle_h_nr_running; in unthrottle_cfs_rq()
6063 if (se->sched_delayed) { in unthrottle_cfs_rq()
6067 } else if (se->on_rq) in unthrottle_cfs_rq()
6072 idle_task_delta = cfs_rq->h_nr_running; in unthrottle_cfs_rq()
6074 qcfs_rq->h_nr_running += task_delta; in unthrottle_cfs_rq()
6075 qcfs_rq->idle_h_nr_running += idle_task_delta; in unthrottle_cfs_rq()
6089 idle_task_delta = cfs_rq->h_nr_running; in unthrottle_cfs_rq()
6091 qcfs_rq->h_nr_running += task_delta; in unthrottle_cfs_rq()
6092 qcfs_rq->idle_h_nr_running += idle_task_delta; in unthrottle_cfs_rq()
6099 /* Start the fair server if un-throttling resulted in new runnable tasks */ in unthrottle_cfs_rq()
6100 if (!rq_h_nr_running && rq->cfs.h_nr_running) in unthrottle_cfs_rq()
6101 dl_server_start(&rq->fair_server); in unthrottle_cfs_rq()
6110 if (rq->curr == rq->idle && rq->cfs.nr_running) in unthrottle_cfs_rq()
6140 list_for_each_entry_safe(cursor, tmp, &rq->cfsb_csd_list, in __cfsb_csd_unthrottle()
6142 list_del_init(&cursor->throttled_csd_list); in __cfsb_csd_unthrottle()
6165 if (SCHED_WARN_ON(!list_empty(&cfs_rq->throttled_csd_list))) in __unthrottle_cfs_rq_async()
6168 first = list_empty(&rq->cfsb_csd_list); in __unthrottle_cfs_rq_async()
6169 list_add_tail(&cfs_rq->throttled_csd_list, &rq->cfsb_csd_list); in __unthrottle_cfs_rq_async()
6171 smp_call_function_single_async(cpu_of(rq), &rq->cfsb_csd); in __unthrottle_cfs_rq_async()
6185 cfs_rq->runtime_remaining <= 0)) in unthrottle_cfs_rq_async()
6202 list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq, in distribute_cfs_runtime()
6216 if (!list_empty(&cfs_rq->throttled_csd_list)) in distribute_cfs_runtime()
6220 SCHED_WARN_ON(cfs_rq->runtime_remaining > 0); in distribute_cfs_runtime()
6222 raw_spin_lock(&cfs_b->lock); in distribute_cfs_runtime()
6223 runtime = -cfs_rq->runtime_remaining + 1; in distribute_cfs_runtime()
6224 if (runtime > cfs_b->runtime) in distribute_cfs_runtime()
6225 runtime = cfs_b->runtime; in distribute_cfs_runtime()
6226 cfs_b->runtime -= runtime; in distribute_cfs_runtime()
6227 remaining = cfs_b->runtime; in distribute_cfs_runtime()
6228 raw_spin_unlock(&cfs_b->lock); in distribute_cfs_runtime()
6230 cfs_rq->runtime_remaining += runtime; in distribute_cfs_runtime()
6233 if (cfs_rq->runtime_remaining > 0) { in distribute_cfs_runtime()
6242 list_add_tail(&cfs_rq->throttled_csd_list, in distribute_cfs_runtime()
6259 list_del_init(&cfs_rq->throttled_csd_list); in distribute_cfs_runtime()
6276 * period the timer is deactivated until scheduling resumes; cfs_b->idle is
6279 static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun, unsigned long flags) in do_sched_cfs_period_timer() argument
6284 if (cfs_b->quota == RUNTIME_INF) in do_sched_cfs_period_timer()
6287 throttled = !list_empty(&cfs_b->throttled_cfs_rq); in do_sched_cfs_period_timer()
6288 cfs_b->nr_periods += overrun; in do_sched_cfs_period_timer()
6290 /* Refill extra burst quota even if cfs_b->idle */ in do_sched_cfs_period_timer()
6297 if (cfs_b->idle && !throttled) in do_sched_cfs_period_timer()
6302 cfs_b->idle = 1; in do_sched_cfs_period_timer()
6307 cfs_b->nr_throttled += overrun; in do_sched_cfs_period_timer()
6310 * This check is repeated as we release cfs_b->lock while we unthrottle. in do_sched_cfs_period_timer()
6312 while (throttled && cfs_b->runtime > 0) { in do_sched_cfs_period_timer()
6313 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in do_sched_cfs_period_timer()
6314 /* we can't nest cfs_b->lock while distributing bandwidth */ in do_sched_cfs_period_timer()
6316 raw_spin_lock_irqsave(&cfs_b->lock, flags); in do_sched_cfs_period_timer()
6325 cfs_b->idle = 0; in do_sched_cfs_period_timer()
6343 * Requires cfs_b->lock for hrtimer_expires_remaining to be safe against the
6349 struct hrtimer *refresh_timer = &cfs_b->period_timer; in runtime_refresh_within()
6352 /* if the call-back is running a quota refresh is already occurring */ in runtime_refresh_within()
6373 if (cfs_b->slack_started) in start_cfs_slack_bandwidth()
6375 cfs_b->slack_started = true; in start_cfs_slack_bandwidth()
6377 hrtimer_start(&cfs_b->slack_timer, in start_cfs_slack_bandwidth()
6385 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); in __return_cfs_rq_runtime()
6386 s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime; in __return_cfs_rq_runtime()
6391 raw_spin_lock(&cfs_b->lock); in __return_cfs_rq_runtime()
6392 if (cfs_b->quota != RUNTIME_INF) { in __return_cfs_rq_runtime()
6393 cfs_b->runtime += slack_runtime; in __return_cfs_rq_runtime()
6395 /* we are under rq->lock, defer unthrottling using a timer */ in __return_cfs_rq_runtime()
6396 if (cfs_b->runtime > sched_cfs_bandwidth_slice() && in __return_cfs_rq_runtime()
6397 !list_empty(&cfs_b->throttled_cfs_rq)) in __return_cfs_rq_runtime()
6400 raw_spin_unlock(&cfs_b->lock); in __return_cfs_rq_runtime()
6403 cfs_rq->runtime_remaining -= slack_runtime; in __return_cfs_rq_runtime()
6411 if (!cfs_rq->runtime_enabled || cfs_rq->nr_running) in return_cfs_rq_runtime()
6419 * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
6427 raw_spin_lock_irqsave(&cfs_b->lock, flags); in do_sched_cfs_slack_timer()
6428 cfs_b->slack_started = false; in do_sched_cfs_slack_timer()
6431 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in do_sched_cfs_slack_timer()
6435 if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) in do_sched_cfs_slack_timer()
6436 runtime = cfs_b->runtime; in do_sched_cfs_slack_timer()
6438 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in do_sched_cfs_slack_timer()
6449 * runtime as update_curr() throttling can not trigger until it's on-rq.
6456 /* an active group must be handled by the update_curr()->put() path */ in check_enqueue_throttle()
6457 if (!cfs_rq->runtime_enabled || cfs_rq->curr) in check_enqueue_throttle()
6466 if (cfs_rq->runtime_remaining <= 0) in check_enqueue_throttle()
6477 if (!tg->parent) in sync_throttle()
6480 cfs_rq = tg->cfs_rq[cpu]; in sync_throttle()
6481 pcfs_rq = tg->parent->cfs_rq[cpu]; in sync_throttle()
6483 cfs_rq->throttle_count = pcfs_rq->throttle_count; in sync_throttle()
6484 cfs_rq->throttled_clock_pelt = rq_clock_pelt(cpu_rq(cpu)); in sync_throttle()
6487 /* conditionally throttle active cfs_rq's from put_prev_entity() */
6493 if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0)) in check_cfs_rq_runtime()
6523 int overrun; in sched_cfs_period_timer() local
6527 raw_spin_lock_irqsave(&cfs_b->lock, flags); in sched_cfs_period_timer()
6529 overrun = hrtimer_forward_now(timer, cfs_b->period); in sched_cfs_period_timer()
6530 if (!overrun) in sched_cfs_period_timer()
6533 idle = do_sched_cfs_period_timer(cfs_b, overrun, flags); in sched_cfs_period_timer()
6536 u64 new, old = ktime_to_ns(cfs_b->period); in sched_cfs_period_timer()
6545 cfs_b->period = ns_to_ktime(new); in sched_cfs_period_timer()
6546 cfs_b->quota *= 2; in sched_cfs_period_timer()
6547 cfs_b->burst *= 2; in sched_cfs_period_timer()
6553 div_u64(cfs_b->quota, NSEC_PER_USEC)); in sched_cfs_period_timer()
6559 div_u64(cfs_b->quota, NSEC_PER_USEC)); in sched_cfs_period_timer()
6567 cfs_b->period_active = 0; in sched_cfs_period_timer()
6568 raw_spin_unlock_irqrestore(&cfs_b->lock, flags); in sched_cfs_period_timer()
6575 raw_spin_lock_init(&cfs_b->lock); in init_cfs_bandwidth()
6576 cfs_b->runtime = 0; in init_cfs_bandwidth()
6577 cfs_b->quota = RUNTIME_INF; in init_cfs_bandwidth()
6578 cfs_b->period = ns_to_ktime(default_cfs_period()); in init_cfs_bandwidth()
6579 cfs_b->burst = 0; in init_cfs_bandwidth()
6580 cfs_b->hierarchical_quota = parent ? parent->hierarchical_quota : RUNTIME_INF; in init_cfs_bandwidth()
6582 INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq); in init_cfs_bandwidth()
6583 hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED); in init_cfs_bandwidth()
6584 cfs_b->period_timer.function = sched_cfs_period_timer; in init_cfs_bandwidth()
6587 hrtimer_set_expires(&cfs_b->period_timer, in init_cfs_bandwidth()
6588 get_random_u32_below(cfs_b->period)); in init_cfs_bandwidth()
6589 hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); in init_cfs_bandwidth()
6590 cfs_b->slack_timer.function = sched_cfs_slack_timer; in init_cfs_bandwidth()
6591 cfs_b->slack_started = false; in init_cfs_bandwidth()
6596 cfs_rq->runtime_enabled = 0; in init_cfs_rq_runtime()
6597 INIT_LIST_HEAD(&cfs_rq->throttled_list); in init_cfs_rq_runtime()
6598 INIT_LIST_HEAD(&cfs_rq->throttled_csd_list); in init_cfs_rq_runtime()
6603 lockdep_assert_held(&cfs_b->lock); in start_cfs_bandwidth()
6605 if (cfs_b->period_active) in start_cfs_bandwidth()
6608 cfs_b->period_active = 1; in start_cfs_bandwidth()
6609 hrtimer_forward_now(&cfs_b->period_timer, cfs_b->period); in start_cfs_bandwidth()
6610 hrtimer_start_expires(&cfs_b->period_timer, HRTIMER_MODE_ABS_PINNED); in start_cfs_bandwidth()
6618 if (!cfs_b->throttled_cfs_rq.next) in destroy_cfs_bandwidth()
6621 hrtimer_cancel(&cfs_b->period_timer); in destroy_cfs_bandwidth()
6622 hrtimer_cancel(&cfs_b->slack_timer); in destroy_cfs_bandwidth()
6639 if (list_empty(&rq->cfsb_csd_list)) in destroy_cfs_bandwidth()
6665 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth; in update_runtime_enabled()
6666 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in update_runtime_enabled()
6668 raw_spin_lock(&cfs_b->lock); in update_runtime_enabled()
6669 cfs_rq->runtime_enabled = cfs_b->quota != RUNTIME_INF; in update_runtime_enabled()
6670 raw_spin_unlock(&cfs_b->lock); in update_runtime_enabled()
6691 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; in unthrottle_offline_cfs_rqs()
6693 if (!cfs_rq->runtime_enabled) in unthrottle_offline_cfs_rqs()
6700 cfs_rq->runtime_remaining = 1; in unthrottle_offline_cfs_rqs()
6705 cfs_rq->runtime_enabled = 0; in unthrottle_offline_cfs_rqs()
6722 if (cfs_rq->runtime_enabled || in cfs_task_bw_constrained()
6723 tg_cfs_bandwidth(cfs_rq->tg)->hierarchical_quota != RUNTIME_INF) in cfs_task_bw_constrained()
6741 if (rq->nr_running != 1) in sched_fair_update_stop_tick()
6815 struct sched_entity *se = &p->se; in hrtick_start_fair()
6819 if (rq->cfs.h_nr_running > 1) { in hrtick_start_fair()
6820 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime; in hrtick_start_fair()
6821 u64 slice = se->slice; in hrtick_start_fair()
6822 s64 delta = slice - ran; in hrtick_start_fair()
6840 struct task_struct *curr = rq->curr; in hrtick_update()
6842 if (!hrtick_enabled_fair(rq) || curr->sched_class != &fair_sched_class) in hrtick_update()
6878 return !sched_energy_enabled() || READ_ONCE(rd->overutilized); in is_rd_overutilized()
6886 WRITE_ONCE(rd->overutilized, flag); in set_rd_overutilized()
6897 if (!is_rd_overutilized(rq->rd) && cpu_overutilized(rq->cpu)) in check_update_overutilized_status()
6898 set_rd_overutilized(rq->rd, 1); in check_update_overutilized_status()
6907 return unlikely(rq->nr_running == rq->cfs.idle_h_nr_running && in sched_idle_rq()
6908 rq->nr_running); in sched_idle_rq()
6924 * se->sched_delayed should imply: se->on_rq == 1. in requeue_delayed_entity()
6928 SCHED_WARN_ON(!se->sched_delayed); in requeue_delayed_entity()
6929 SCHED_WARN_ON(!se->on_rq); in requeue_delayed_entity()
6933 if (se->vlag > 0) { in requeue_delayed_entity()
6934 cfs_rq->nr_running--; in requeue_delayed_entity()
6935 if (se != cfs_rq->curr) in requeue_delayed_entity()
6937 se->vlag = 0; in requeue_delayed_entity()
6939 if (se != cfs_rq->curr) in requeue_delayed_entity()
6941 cfs_rq->nr_running++; in requeue_delayed_entity()
6946 se->sched_delayed = 0; in requeue_delayed_entity()
6958 struct sched_entity *se = &p->se; in enqueue_task_fair()
6961 int rq_h_nr_running = rq->cfs.h_nr_running; in enqueue_task_fair()
6970 if (!(p->se.sched_delayed && (task_on_rq_migrating(p) || (flags & ENQUEUE_RESTORE)))) in enqueue_task_fair()
6971 util_est_enqueue(&rq->cfs, p); in enqueue_task_fair()
6983 if (p->in_iowait) in enqueue_task_fair()
6987 if (se->on_rq) { in enqueue_task_fair()
6988 if (se->sched_delayed) in enqueue_task_fair()
6997 * its entities in the desired time-frame. in enqueue_task_fair()
7000 se->slice = slice; in enqueue_task_fair()
7001 se->custom_slice = 1; in enqueue_task_fair()
7006 cfs_rq->h_nr_running++; in enqueue_task_fair()
7007 cfs_rq->idle_h_nr_running += idle_h_nr_running; in enqueue_task_fair()
7026 se->slice = slice; in enqueue_task_fair()
7029 cfs_rq->h_nr_running++; in enqueue_task_fair()
7030 cfs_rq->idle_h_nr_running += idle_h_nr_running; in enqueue_task_fair()
7040 if (!rq_h_nr_running && rq->cfs.h_nr_running) { in enqueue_task_fair()
7042 if (!rq->nr_running) in enqueue_task_fair()
7043 dl_server_update_idle_time(rq, rq->curr); in enqueue_task_fair()
7044 dl_server_start(&rq->fair_server); in enqueue_task_fair()
7077 * failing half-way through and resume the dequeue later.
7080 * -1 - dequeue delayed
7081 * 0 - dequeue throttled
7082 * 1 - dequeue complete
7087 int rq_h_nr_running = rq->cfs.h_nr_running; in dequeue_entities()
7109 if (p && &p->se == se) in dequeue_entities()
7110 return -1; in dequeue_entities()
7115 cfs_rq->h_nr_running -= h_nr_running; in dequeue_entities()
7116 cfs_rq->idle_h_nr_running -= idle_h_nr_running; in dequeue_entities()
7126 if (cfs_rq->load.weight) { in dequeue_entities()
7129 /* Avoid re-evaluating load for this entity: */ in dequeue_entities()
7150 se->slice = slice; in dequeue_entities()
7153 cfs_rq->h_nr_running -= h_nr_running; in dequeue_entities()
7154 cfs_rq->idle_h_nr_running -= idle_h_nr_running; in dequeue_entities()
7166 if (rq_h_nr_running && !rq->cfs.h_nr_running) in dequeue_entities()
7167 dl_server_stop(&rq->fair_server); in dequeue_entities()
7171 rq->next_balance = jiffies; in dequeue_entities()
7175 SCHED_WARN_ON(p->on_rq != 1); in dequeue_entities()
7177 /* Fix-up what dequeue_task_fair() skipped */ in dequeue_entities()
7181 * Fix-up what block_task() skipped. in dequeue_entities()
7198 if (!(p->se.sched_delayed && (task_on_rq_migrating(p) || (flags & DEQUEUE_SAVE)))) in dequeue_task_fair()
7199 util_est_dequeue(&rq->cfs, p); in dequeue_task_fair()
7201 util_est_update(&rq->cfs, p, flags & DEQUEUE_SLEEP); in dequeue_task_fair()
7202 if (dequeue_entities(rq, &p->se, flags) < 0) in dequeue_task_fair()
7235 return cfs_rq_load_avg(&rq->cfs); in cpu_load()
7239 * cpu_load_without - compute CPU load without any contributions from *p
7257 if (cpu_of(rq) != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in cpu_load_without()
7260 cfs_rq = &rq->cfs; in cpu_load_without()
7261 load = READ_ONCE(cfs_rq->avg.load_avg); in cpu_load_without()
7271 return cfs_rq_runnable_avg(&rq->cfs); in cpu_runnable()
7280 if (cpu_of(rq) != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in cpu_runnable_without()
7283 cfs_rq = &rq->cfs; in cpu_runnable_without()
7284 runnable = READ_ONCE(cfs_rq->avg.runnable_avg); in cpu_runnable_without()
7287 lsub_positive(&runnable, p->se.avg.runnable_avg); in cpu_runnable_without()
7294 return cpu_rq(cpu)->cpu_capacity; in capacity_of()
7303 if (time_after(jiffies, current->wakee_flip_decay_ts + HZ)) { in record_wakee()
7304 current->wakee_flips >>= 1; in record_wakee()
7305 current->wakee_flip_decay_ts = jiffies; in record_wakee()
7308 if (current->last_wakee != p) { in record_wakee()
7309 current->last_wakee = p; in record_wakee()
7310 current->wakee_flips++; in record_wakee()
7315 * Detect M:N waker/wakee relationships via a switching-frequency heuristic.
7325 * non-monogamous, with partner count exceeding socket size.
7333 unsigned int master = current->wakee_flips; in wake_wide()
7334 unsigned int slave = p->wakee_flips; in wake_wide()
7349 * wake_affine_idle() - only considers 'now', it check if the waking CPU is
7350 * cache-affine and is (or will be) idle.
7352 * wake_affine_weight() - considers the weight to reflect the average
7374 if (sync && cpu_rq(this_cpu)->nr_running == 1) in wake_affine_idle()
7398 this_eff_load -= current_load; in wake_affine_weight()
7409 prev_eff_load -= task_load; in wake_affine_weight()
7411 prev_eff_load *= 100 + (sd->imbalance_pct - 100) / 2; in wake_affine_weight()
7437 schedstat_inc(p->stats.nr_wakeups_affine_attempts); in wake_affine()
7441 schedstat_inc(sd->ttwu_move_affine); in wake_affine()
7442 schedstat_inc(p->stats.nr_wakeups_affine); in wake_affine()
7450 * sched_balance_find_dst_group_cpu - find the idlest CPU among the CPUs in the group.
7459 int shallowest_idle_cpu = -1; in sched_balance_find_dst_group_cpu()
7463 if (group->group_weight == 1) in sched_balance_find_dst_group_cpu()
7467 for_each_cpu_and(i, sched_group_span(group), p->cpus_ptr) { in sched_balance_find_dst_group_cpu()
7478 if (idle && idle->exit_latency < min_exit_latency) { in sched_balance_find_dst_group_cpu()
7484 min_exit_latency = idle->exit_latency; in sched_balance_find_dst_group_cpu()
7485 latest_idle_timestamp = rq->idle_stamp; in sched_balance_find_dst_group_cpu()
7487 } else if ((!idle || idle->exit_latency == min_exit_latency) && in sched_balance_find_dst_group_cpu()
7488 rq->idle_stamp > latest_idle_timestamp) { in sched_balance_find_dst_group_cpu()
7494 latest_idle_timestamp = rq->idle_stamp; in sched_balance_find_dst_group_cpu()
7497 } else if (shallowest_idle_cpu == -1) { in sched_balance_find_dst_group_cpu()
7506 return shallowest_idle_cpu != -1 ? shallowest_idle_cpu : least_loaded_cpu; in sched_balance_find_dst_group_cpu()
7514 if (!cpumask_intersects(sched_domain_span(sd), p->cpus_ptr)) in sched_balance_find_dst_cpu()
7522 sync_entity_load_avg(&p->se); in sched_balance_find_dst_cpu()
7529 if (!(sd->flags & sd_flag)) { in sched_balance_find_dst_cpu()
7530 sd = sd->child; in sched_balance_find_dst_cpu()
7536 sd = sd->child; in sched_balance_find_dst_cpu()
7543 sd = sd->child; in sched_balance_find_dst_cpu()
7549 weight = sd->span_weight; in sched_balance_find_dst_cpu()
7552 if (weight <= tmp->span_weight) in sched_balance_find_dst_cpu()
7554 if (tmp->flags & sd_flag) in sched_balance_find_dst_cpu()
7568 return -1; in __select_idle_cpu()
7581 WRITE_ONCE(sds->has_idle_cores, val); in set_idle_cores()
7590 return READ_ONCE(sds->has_idle_cores); in test_idle_cores()
7597 * information in sd_llc_shared->has_idle_cores.
7627 * sd_llc->shared->has_idle_cores and enabled through update_idle_core() above.
7637 if (*idle_cpu == -1) { in select_idle_core()
7646 if (*idle_cpu == -1 && cpumask_test_cpu(cpu, cpus)) in select_idle_core()
7654 return -1; in select_idle_core()
7664 for_each_cpu_and(cpu, cpu_smt_mask(target), p->cpus_ptr) { in select_idle_smt()
7677 return -1; in select_idle_smt()
7698 return -1; in select_idle_smt()
7705 * comparing the average scan cost (tracked in sd->avg_scan_cost) against the
7706 * average idle time for this rq (as found in rq->avg_idle).
7711 int i, cpu, idle_cpu = -1, nr = INT_MAX; in select_idle_cpu()
7714 cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr); in select_idle_cpu()
7719 /* because !--nr is the condition to stop scan */ in select_idle_cpu()
7720 nr = READ_ONCE(sd_share->nr_idle_scan) + 1; in select_idle_cpu()
7723 return -1; in select_idle_cpu()
7728 struct sched_group *sg = sd->groups; in select_idle_cpu()
7730 if (sg->flags & SD_CLUSTER) { in select_idle_cpu()
7740 if (--nr <= 0) in select_idle_cpu()
7741 return -1; in select_idle_cpu()
7758 if (--nr <= 0) in select_idle_cpu()
7759 return -1; in select_idle_cpu()
7782 int cpu, best_cpu = -1; in select_idle_capacity()
7786 cpumask_and(cpus, sched_domain_span(sd), p->cpus_ptr); in select_idle_capacity()
7811 * First, select CPU which fits better (-1 being better than 0). in select_idle_capacity()
7848 int i, recent_used_cpu, prev_aff = -1; in select_idle_sibling()
7855 sync_entity_load_avg(&p->se); in select_idle_sibling()
7862 * per-cpu select_rq_mask usage in select_idle_sibling()
7885 * Allow a per-cpu kthread to stack with the wakee if the in select_idle_sibling()
7888 * per-cpu kthread that is now complete and the wakeup is in select_idle_sibling()
7895 this_rq()->nr_running <= 1 && in select_idle_sibling()
7901 recent_used_cpu = p->recent_used_cpu; in select_idle_sibling()
7902 p->recent_used_cpu = prev; in select_idle_sibling()
7907 cpumask_test_cpu(recent_used_cpu, p->cpus_ptr) && in select_idle_sibling()
7915 recent_used_cpu = -1; in select_idle_sibling()
7971 * cpu_util() - Estimates the amount of CPU capacity used by CFS tasks.
7974 * @dst_cpu: CPU @p migrates to, -1 if @p moves from @cpu or @p == NULL
7981 * recent utilization of currently non-runnable tasks on that CPU.
7989 * previously-executed tasks, which helps better deduce how busy a CPU will
7990 * be when a long-sleeping task wakes up. The contribution to CPU utilization
8004 * could be seen as over-utilized even though CPU1 has 20% of spare CPU
8007 * after task migrations (scheduler-driven DVFS).
8014 struct cfs_rq *cfs_rq = &cpu_rq(cpu)->cfs; in cpu_util()
8015 unsigned long util = READ_ONCE(cfs_rq->avg.util_avg); in cpu_util()
8019 runnable = READ_ONCE(cfs_rq->avg.runnable_avg); in cpu_util()
8024 * If @dst_cpu is -1 or @p migrates from @cpu to @dst_cpu remove its in cpu_util()
8037 util_est = READ_ONCE(cfs_rq->avg.util_est); in cpu_util()
8040 * During wake-up @p isn't enqueued yet and doesn't contribute in cpu_util()
8041 * to any cpu_rq(cpu)->cfs.avg.util_est. in cpu_util()
8045 * During exec (@dst_cpu = -1) @p is enqueued and does in cpu_util()
8046 * contribute to cpu_rq(cpu)->cfs.util_est. in cpu_util()
8055 * p->on_rq = TASK_ON_RQ_MIGRATING; in cpu_util()
8056 * -------------------------------- A in cpu_util()
8060 * -------------------------------- B in cpu_util()
8078 return cpu_util(cpu, NULL, -1, 0); in cpu_util_cfs()
8083 return cpu_util(cpu, NULL, -1, 1); in cpu_util_cfs_boost()
8102 if (cpu != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in cpu_util_without()
8105 return cpu_util(cpu, p, -1, 0); in cpu_util_without()
8120 * The cfs,rt,dl utilization are the running times measured with rq->clock_task
8121 * which excludes things like IRQ and steal-time. These latter are then accrued
8139 * because of inaccuracies in how we track these -- see in effective_cpu_util()
8154 * - the computed DL bandwidth needed with the IRQ pressure which in effective_cpu_util()
8156 * - The minimum performance requirement for CFS and/or RT. in effective_cpu_util()
8164 if (!uclamp_is_used() && rt_rq_is_runnable(&rq->rt)) in effective_cpu_util()
8192 * max - irq in effective_cpu_util()
8193 * U' = irq + --------- * U in effective_cpu_util()
8208 * energy_env - Utilization landscape for energy estimation.
8214 * @pd_cap: Entire perf domain capacity. (pd->nr_cpus * cpu_cap).
8240 eenv->task_busy_time = busy_time; in eenv_task_busy_time()
8254 * - A stable PD utilization, no matter which CPU of that PD we want to place
8257 * - A fair comparison between CPUs as the task contribution (task_util())
8262 * exceed @eenv->pd_cap.
8272 unsigned long util = cpu_util(cpu, p, -1, 0); in eenv_pd_busy_time()
8277 eenv->pd_busy_time = min(eenv->pd_cap, busy_time); in eenv_pd_busy_time()
8284 * Returns the maximum utilization among @eenv->cpus. This utilization can't
8285 * exceed @eenv->cpu_cap.
8326 return min(max_util, eenv->cpu_cap); in eenv_pd_max_util()
8339 unsigned long busy_time = eenv->pd_busy_time; in compute_energy()
8343 busy_time = min(eenv->pd_cap, busy_time + eenv->task_busy_time); in compute_energy()
8345 energy = em_cpu_energy(pd->em_pd, max_util, busy_time, eenv->cpu_cap); in compute_energy()
8353 * find_energy_efficient_cpu(): Find most energy-efficient target CPU for the
8357 * out which of the CPU candidates is the most energy-efficient.
8374 * cluster-packing, and spreading inside a cluster. That should at least be
8381 * NOTE: Forkees are not accepted in the energy-aware wake-up path because
8385 * to be energy-inefficient in some use-cases. The alternative would be to
8388 * other use-cases too. So, until someone finds a better way to solve this,
8389 * let's keep things simple by re-using the existing slow path.
8397 struct root_domain *rd = this_rq()->rd; in find_energy_efficient_cpu()
8398 int cpu, best_energy_cpu, target = -1; in find_energy_efficient_cpu()
8399 int prev_fits = -1, best_fits = -1; in find_energy_efficient_cpu()
8407 pd = rcu_dereference(rd->pd); in find_energy_efficient_cpu()
8412 * Energy-aware wake-up happens on the lowest sched_domain starting in find_energy_efficient_cpu()
8417 sd = sd->parent; in find_energy_efficient_cpu()
8423 sync_entity_load_avg(&p->se); in find_energy_efficient_cpu()
8429 for (; pd; pd = pd->next) { in find_energy_efficient_cpu()
8432 long prev_spare_cap = -1, max_spare_cap = -1; in find_energy_efficient_cpu()
8435 int max_spare_cap_cpu = -1; in find_energy_efficient_cpu()
8436 int fits, max_fits = -1; in find_energy_efficient_cpu()
8458 if (!cpumask_test_cpu(cpu, p->cpus_ptr)) in find_energy_efficient_cpu()
8477 * max-aggregated uclamp_{min, max}. in find_energy_efficient_cpu()
8514 base_energy = compute_energy(&eenv, pd, cpus, p, -1); in find_energy_efficient_cpu()
8517 if (prev_spare_cap > -1) { in find_energy_efficient_cpu()
8523 prev_delta -= base_energy; in find_energy_efficient_cpu()
8547 cur_delta -= base_energy; in find_energy_efficient_cpu()
8591 int sync = (wake_flags & WF_SYNC) && !(current->flags & PF_EXITING); in select_task_rq_fair()
8600 * required for stable ->cpus_allowed in select_task_rq_fair()
8602 lockdep_assert_held(&p->pi_lock); in select_task_rq_fair()
8607 cpumask_test_cpu(cpu, p->cpus_ptr)) in select_task_rq_fair()
8610 if (!is_rd_overutilized(this_rq()->rd)) { in select_task_rq_fair()
8617 want_affine = !wake_wide(p) && cpumask_test_cpu(cpu, p->cpus_ptr); in select_task_rq_fair()
8626 if (want_affine && (tmp->flags & SD_WAKE_AFFINE) && in select_task_rq_fair()
8640 if (tmp->flags & sd_flag) in select_task_rq_fair()
8661 * previous CPU. The caller guarantees p->pi_lock or task_rq(p)->lock is held.
8665 struct sched_entity *se = &p->se; in migrate_task_rq_fair()
8674 * leading to an inflation after wake-up on the new rq. in migrate_task_rq_fair()
8684 se->avg.last_update_time = 0; in migrate_task_rq_fair()
8691 struct sched_entity *se = &p->se; in task_dead_fair()
8693 if (se->sched_delayed) { in task_dead_fair()
8698 if (se->sched_delayed) { in task_dead_fair()
8723 if (!cpumask_intersects(p->cpus_ptr, cpumask)) in set_task_max_allowed_capacity()
8726 p->max_allowed_capacity = entry->capacity; in set_task_max_allowed_capacity()
8753 if (SCHED_WARN_ON(!se->on_rq)) in set_next_buddy()
8757 cfs_rq_of(se)->next = se; in set_next_buddy()
8766 struct task_struct *curr = rq->curr; in check_preempt_wakeup_fair()
8767 struct sched_entity *se = &curr->se, *pse = &p->se; in check_preempt_wakeup_fair()
8777 * lead to a throttle). This both saves work and prevents false in check_preempt_wakeup_fair()
8778 * next-buddy nomination below. in check_preempt_wakeup_fair()
8791 * Note: this also catches the edge-case of curr being in a throttled in check_preempt_wakeup_fair()
8810 * Preempt an idle entity in favor of a non-idle entity (and don't preempt in check_preempt_wakeup_fair()
8821 if (unlikely(!normal_policy(p->policy))) in check_preempt_wakeup_fair()
8833 if (do_preempt_short(cfs_rq, pse, se) && se->vlag == se->deadline) in check_preempt_wakeup_fair()
8834 se->vlag = se->deadline + 1; in check_preempt_wakeup_fair()
8854 cfs_rq = &rq->cfs; in pick_task_fair()
8855 if (!cfs_rq->nr_running) in pick_task_fair()
8860 if (cfs_rq->curr && cfs_rq->curr->on_rq) in pick_task_fair()
8889 se = &p->se; in pick_next_task_fair()
8892 if (prev->sched_class != &fair_sched_class) in pick_next_task_fair()
8909 struct sched_entity *pse = &prev->se; in pick_next_task_fair()
8913 int se_depth = se->depth; in pick_next_task_fair()
8914 int pse_depth = pse->depth; in pick_next_task_fair()
8946 * Because sched_balance_newidle() releases (and re-acquires) rq->lock, it is in pick_next_task_fair()
8948 * must re-start the pick_next_entity() loop. in pick_next_task_fair()
8972 return !!dl_se->rq->cfs.nr_running; in fair_server_has_tasks()
8977 return pick_task_fair(dl_se->rq); in fair_server_pick_task()
8982 struct sched_dl_entity *dl_se = &rq->fair_server; in fair_server_init()
8994 struct sched_entity *se = &prev->se; in put_prev_task_fair()
9008 struct task_struct *curr = rq->curr; in yield_task_fair()
9010 struct sched_entity *se = &curr->se; in yield_task_fair()
9015 if (unlikely(rq->nr_running == 1)) in yield_task_fair()
9022 * Update run-time statistics of the 'current'. in yield_task_fair()
9032 se->deadline += calc_delta_fair(se->slice, se); in yield_task_fair()
9037 struct sched_entity *se = &p->se; in yield_to_task_fair()
9040 if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se))) in yield_to_task_fair()
9053 * Fair scheduling class load-balancing methods.
9057 * The purpose of load-balancing is to achieve the same basic fairness the
9058 * per-CPU scheduler provides, namely provide a proportional amount of compute
9063 * Where W_i,n is the n-th weight average for CPU i. The instantaneous weight
9068 * Where w_i,j is the weight of the j-th runnable task on CPU i. This weight
9074 * W'_i,n = (2^n - 1) / 2^n * W_i,n + 1 / 2^n * W_i,0 (3)
9083 * imb_i,j = max{ avg(W/C), W_i/C_i } - min{ avg(W/C), W_j/C_j } (4)
9090 * - infeasible weights;
9091 * - local vs global optima in the discrete case. ]
9101 * of load-balance at each level inversely proportional to the number of CPUs in
9107 * \Sum { --- * --- * 2^i } = O(n) (5)
9109 * `- size of each group
9110 * | | `- number of CPUs doing load-balance
9111 * | `- freq
9112 * `- sum over all levels
9154 * W_i,0 = \Sum_j \Prod_k w_k * ----- (9)
9161 * w_i,j,k is the weight of the j-th runnable task in the k-th cgroup on CPU i.
9243 /* The set of CPUs under consideration for load-balancing */
9258 * Is this task likely cache-hot:
9264 lockdep_assert_rq_held(env->src_rq); in task_hot()
9266 if (p->sched_class != &fair_sched_class) in task_hot()
9273 if (env->sd->flags & SD_SHARE_CPUCAPACITY) in task_hot()
9279 if (sched_feat(CACHE_HOT_BUDDY) && env->dst_rq->nr_running && in task_hot()
9280 (&p->se == cfs_rq_of(&p->se)->next)) in task_hot()
9283 if (sysctl_sched_migration_cost == -1) in task_hot()
9290 if (!sched_core_cookie_match(cpu_rq(env->dst_cpu), p)) in task_hot()
9296 delta = rq_clock_task(env->src_rq) - p->se.exec_start; in task_hot()
9305 * Returns -1, if task migration is not affected by locality.
9309 struct numa_group *numa_group = rcu_dereference(p->numa_group); in migrate_degrades_locality()
9314 return -1; in migrate_degrades_locality()
9316 if (!p->numa_faults || !(env->sd->flags & SD_NUMA)) in migrate_degrades_locality()
9317 return -1; in migrate_degrades_locality()
9319 src_nid = cpu_to_node(env->src_cpu); in migrate_degrades_locality()
9320 dst_nid = cpu_to_node(env->dst_cpu); in migrate_degrades_locality()
9323 return -1; in migrate_degrades_locality()
9326 if (src_nid == p->numa_preferred_nid) { in migrate_degrades_locality()
9327 if (env->src_rq->nr_running > env->src_rq->nr_preferred_running) in migrate_degrades_locality()
9330 return -1; in migrate_degrades_locality()
9334 if (dst_nid == p->numa_preferred_nid) in migrate_degrades_locality()
9338 if (env->idle == CPU_IDLE) in migrate_degrades_locality()
9339 return -1; in migrate_degrades_locality()
9357 return -1; in migrate_degrades_locality()
9362 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
9369 lockdep_assert_rq_held(env->src_rq); in can_migrate_task()
9376 * 4) are cache-hot on their current CPU. in can_migrate_task()
9378 if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu)) in can_migrate_task()
9385 if (!cpumask_test_cpu(env->dst_cpu, p->cpus_ptr)) { in can_migrate_task()
9388 schedstat_inc(p->stats.nr_failed_migrations_affine); in can_migrate_task()
9390 env->flags |= LBF_SOME_PINNED; in can_migrate_task()
9398 * - for NEWLY_IDLE in can_migrate_task()
9399 * - if we have already computed one in current iteration in can_migrate_task()
9400 * - if it's an active balance in can_migrate_task()
9402 if (env->idle == CPU_NEWLY_IDLE || in can_migrate_task()
9403 env->flags & (LBF_DST_PINNED | LBF_ACTIVE_LB)) in can_migrate_task()
9406 /* Prevent to re-select dst_cpu via env's CPUs: */ in can_migrate_task()
9407 for_each_cpu_and(cpu, env->dst_grpmask, env->cpus) { in can_migrate_task()
9408 if (cpumask_test_cpu(cpu, p->cpus_ptr)) { in can_migrate_task()
9409 env->flags |= LBF_DST_PINNED; in can_migrate_task()
9410 env->new_dst_cpu = cpu; in can_migrate_task()
9419 env->flags &= ~LBF_ALL_PINNED; in can_migrate_task()
9421 if (task_on_cpu(env->src_rq, p)) { in can_migrate_task()
9422 schedstat_inc(p->stats.nr_failed_migrations_running); in can_migrate_task()
9433 if (env->flags & LBF_ACTIVE_LB) in can_migrate_task()
9437 if (tsk_cache_hot == -1) in can_migrate_task()
9441 env->sd->nr_balance_failed > env->sd->cache_nice_tries) { in can_migrate_task()
9443 schedstat_inc(env->sd->lb_hot_gained[env->idle]); in can_migrate_task()
9444 schedstat_inc(p->stats.nr_forced_migrations); in can_migrate_task()
9449 schedstat_inc(p->stats.nr_failed_migrations_hot); in can_migrate_task()
9454 * detach_task() -- detach the task for the migration specified in env
9458 lockdep_assert_rq_held(env->src_rq); in detach_task()
9460 deactivate_task(env->src_rq, p, DEQUEUE_NOCLOCK); in detach_task()
9461 set_task_cpu(p, env->dst_cpu); in detach_task()
9465 * detach_one_task() -- tries to dequeue exactly one task from env->src_rq, as
9474 lockdep_assert_rq_held(env->src_rq); in detach_one_task()
9477 &env->src_rq->cfs_tasks, se.group_node) { in detach_one_task()
9485 * lb_gained[env->idle] is updated (other is detach_tasks) in detach_one_task()
9489 schedstat_inc(env->sd->lb_gained[env->idle]); in detach_one_task()
9496 * detach_tasks() -- tries to detach up to imbalance load/util/tasks from
9503 struct list_head *tasks = &env->src_rq->cfs_tasks; in detach_tasks()
9508 lockdep_assert_rq_held(env->src_rq); in detach_tasks()
9514 if (env->src_rq->nr_running <= 1) { in detach_tasks()
9515 env->flags &= ~LBF_ALL_PINNED; in detach_tasks()
9519 if (env->imbalance <= 0) in detach_tasks()
9527 if (env->idle && env->src_rq->nr_running <= 1) in detach_tasks()
9530 env->loop++; in detach_tasks()
9532 if (env->loop > env->loop_max) in detach_tasks()
9536 if (env->loop > env->loop_break) { in detach_tasks()
9537 env->loop_break += SCHED_NR_MIGRATE_BREAK; in detach_tasks()
9538 env->flags |= LBF_NEED_BREAK; in detach_tasks()
9547 switch (env->migration_type) { in detach_tasks()
9552 * value. Make sure that env->imbalance decreases in detach_tasks()
9559 load < 16 && !env->sd->nr_balance_failed) in detach_tasks()
9568 if (shr_bound(load, env->sd->nr_balance_failed) > env->imbalance) in detach_tasks()
9571 env->imbalance -= load; in detach_tasks()
9577 if (shr_bound(util, env->sd->nr_balance_failed) > env->imbalance) in detach_tasks()
9580 env->imbalance -= util; in detach_tasks()
9584 env->imbalance--; in detach_tasks()
9589 if (task_fits_cpu(p, env->src_cpu)) in detach_tasks()
9592 env->imbalance = 0; in detach_tasks()
9597 list_add(&p->se.group_node, &env->tasks); in detach_tasks()
9607 if (env->idle == CPU_NEWLY_IDLE) in detach_tasks()
9615 if (env->imbalance <= 0) in detach_tasks()
9620 list_move(&p->se.group_node, tasks); in detach_tasks()
9628 schedstat_add(env->sd->lb_gained[env->idle], detached); in detach_tasks()
9634 * attach_task() -- attach the task detached by detach_task() to its new rq.
9646 * attach_one_task() -- attaches the task returned from detach_one_task() to
9660 * attach_tasks() -- attaches all tasks detached by detach_tasks() to their
9665 struct list_head *tasks = &env->tasks; in attach_tasks()
9669 rq_lock(env->dst_rq, &rf); in attach_tasks()
9670 update_rq_clock(env->dst_rq); in attach_tasks()
9674 list_del_init(&p->se.group_node); in attach_tasks()
9676 attach_task(env->dst_rq, p); in attach_tasks()
9679 rq_unlock(env->dst_rq, &rf); in attach_tasks()
9685 if (cfs_rq->avg.load_avg) in cfs_rq_has_blocked()
9688 if (cfs_rq->avg.util_avg) in cfs_rq_has_blocked()
9713 WRITE_ONCE(rq->last_blocked_load_update_tick, jiffies); in update_blocked_load_tick()
9719 rq->has_blocked_load = 0; in update_blocked_load_status()
9762 if (cfs_rq->nr_running == 0) in __update_blocked_fair()
9765 if (cfs_rq == &rq->cfs) in __update_blocked_fair()
9770 se = cfs_rq->tg->se[cpu]; in __update_blocked_fair()
9791 * This needs to be done in a top-down fashion because the load of a child
9797 struct sched_entity *se = cfs_rq->tg->se[cpu_of(rq)]; in update_cfs_rq_h_load()
9801 if (cfs_rq->last_h_load_update == now) in update_cfs_rq_h_load()
9804 WRITE_ONCE(cfs_rq->h_load_next, NULL); in update_cfs_rq_h_load()
9807 WRITE_ONCE(cfs_rq->h_load_next, se); in update_cfs_rq_h_load()
9808 if (cfs_rq->last_h_load_update == now) in update_cfs_rq_h_load()
9813 cfs_rq->h_load = cfs_rq_load_avg(cfs_rq); in update_cfs_rq_h_load()
9814 cfs_rq->last_h_load_update = now; in update_cfs_rq_h_load()
9817 while ((se = READ_ONCE(cfs_rq->h_load_next)) != NULL) { in update_cfs_rq_h_load()
9818 load = cfs_rq->h_load; in update_cfs_rq_h_load()
9819 load = div64_ul(load * se->avg.load_avg, in update_cfs_rq_h_load()
9822 cfs_rq->h_load = load; in update_cfs_rq_h_load()
9823 cfs_rq->last_h_load_update = now; in update_cfs_rq_h_load()
9832 return div64_ul(p->se.avg.load_avg * cfs_rq->h_load, in task_h_load()
9838 struct cfs_rq *cfs_rq = &rq->cfs; in __update_blocked_fair()
9850 return p->se.avg.load_avg; in task_h_load()
9876 * sg_lb_stats - stats of a sched_group required for load-balancing:
9899 * sd_lb_stats - stats of a sched_domain required for load-balancing:
9956 free = max - used; in scale_rt_capacity()
9964 struct sched_group *sdg = sd->groups; in update_cpu_capacity()
9969 cpu_rq(cpu)->cpu_capacity = capacity; in update_cpu_capacity()
9972 sdg->sgc->capacity = capacity; in update_cpu_capacity()
9973 sdg->sgc->min_capacity = capacity; in update_cpu_capacity()
9974 sdg->sgc->max_capacity = capacity; in update_cpu_capacity()
9979 struct sched_domain *child = sd->child; in update_group_capacity()
9980 struct sched_group *group, *sdg = sd->groups; in update_group_capacity()
9984 interval = msecs_to_jiffies(sd->balance_interval); in update_group_capacity()
9986 sdg->sgc->next_update = jiffies + interval; in update_group_capacity()
9997 if (child->flags & SD_OVERLAP) { in update_group_capacity()
10016 group = child->groups; in update_group_capacity()
10018 struct sched_group_capacity *sgc = group->sgc; in update_group_capacity()
10020 capacity += sgc->capacity; in update_group_capacity()
10021 min_capacity = min(sgc->min_capacity, min_capacity); in update_group_capacity()
10022 max_capacity = max(sgc->max_capacity, max_capacity); in update_group_capacity()
10023 group = group->next; in update_group_capacity()
10024 } while (group != child->groups); in update_group_capacity()
10027 sdg->sgc->capacity = capacity; in update_group_capacity()
10028 sdg->sgc->min_capacity = min_capacity; in update_group_capacity()
10029 sdg->sgc->max_capacity = max_capacity; in update_group_capacity()
10040 return ((rq->cpu_capacity * sd->imbalance_pct) < in check_cpu_capacity()
10047 return rq->misfit_task_load; in check_misfit_status()
10052 * groups is inadequate due to ->cpus_ptr constraints.
10061 * If we were to balance group-wise we'd place two tasks in the first group and
10081 return group->sgc->imbalance; in sg_imbalanced()
10099 if (sgs->sum_nr_running < sgs->group_weight) in group_has_capacity()
10102 if ((sgs->group_capacity * imbalance_pct) < in group_has_capacity()
10103 (sgs->group_runnable * 100)) in group_has_capacity()
10106 if ((sgs->group_capacity * 100) > in group_has_capacity()
10107 (sgs->group_util * imbalance_pct)) in group_has_capacity()
10124 if (sgs->sum_nr_running <= sgs->group_weight) in group_is_overloaded()
10127 if ((sgs->group_capacity * 100) < in group_is_overloaded()
10128 (sgs->group_util * imbalance_pct)) in group_is_overloaded()
10131 if ((sgs->group_capacity * imbalance_pct) < in group_is_overloaded()
10132 (sgs->group_runnable * 100)) in group_is_overloaded()
10149 if (sgs->group_asym_packing) in group_classify()
10152 if (sgs->group_smt_balance) in group_classify()
10155 if (sgs->group_misfit_task_load) in group_classify()
10165 * sched_use_asym_prio - Check whether asym_packing priority must be used
10177 if (!(sd->flags & SD_ASYM_PACKING)) in sched_use_asym_prio()
10183 return sd->flags & SD_SHARE_CPUCAPACITY || is_core_idle(cpu); in sched_use_asym_prio()
10197 * sched_group_asym - Check if the destination CPU can do asym_packing balance
10199 * @sgs: Load-balancing statistics of the candidate busiest group
10215 if ((group->flags & SD_SHARE_CPUCAPACITY) && in sched_group_asym()
10216 (sgs->group_weight - sgs->idle_cpus != 1)) in sched_group_asym()
10219 return sched_asym(env->sd, env->dst_cpu, group->asym_prefer_cpu); in sched_group_asym()
10229 return (sg1->flags & SD_SHARE_CPUCAPACITY) != in smt_vs_nonsmt_groups()
10230 (sg2->flags & SD_SHARE_CPUCAPACITY); in smt_vs_nonsmt_groups()
10236 if (!env->idle) in smt_balance()
10245 if (group->flags & SD_SHARE_CPUCAPACITY && in smt_balance()
10246 sgs->sum_h_nr_running > 1) in smt_balance()
10260 if (!env->idle || !busiest->sum_nr_running) in sibling_imbalance()
10263 ncores_busiest = sds->busiest->cores; in sibling_imbalance()
10264 ncores_local = sds->local->cores; in sibling_imbalance()
10267 imbalance = busiest->sum_nr_running; in sibling_imbalance()
10268 lsub_positive(&imbalance, local->sum_nr_running); in sibling_imbalance()
10273 imbalance = ncores_local * busiest->sum_nr_running; in sibling_imbalance()
10274 lsub_positive(&imbalance, ncores_busiest * local->sum_nr_running); in sibling_imbalance()
10280 if (imbalance <= 1 && local->sum_nr_running == 0 && in sibling_imbalance()
10281 busiest->sum_nr_running > 1) in sibling_imbalance()
10294 if (rq->cfs.h_nr_running != 1) in sched_reduced_capacity()
10301 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
10303 * @sds: Load-balancing data with statistics of the local group.
10320 local_group = group == sds->local; in update_sg_lb_stats()
10322 for_each_cpu_and(i, sched_group_span(group), env->cpus) { in update_sg_lb_stats()
10326 sgs->group_load += load; in update_sg_lb_stats()
10327 sgs->group_util += cpu_util_cfs(i); in update_sg_lb_stats()
10328 sgs->group_runnable += cpu_runnable(rq); in update_sg_lb_stats()
10329 sgs->sum_h_nr_running += rq->cfs.h_nr_running; in update_sg_lb_stats()
10331 nr_running = rq->nr_running; in update_sg_lb_stats()
10332 sgs->sum_nr_running += nr_running; in update_sg_lb_stats()
10341 sgs->nr_numa_running += rq->nr_numa_running; in update_sg_lb_stats()
10342 sgs->nr_preferred_running += rq->nr_preferred_running; in update_sg_lb_stats()
10348 sgs->idle_cpus++; in update_sg_lb_stats()
10356 if (env->sd->flags & SD_ASYM_CPUCAPACITY) { in update_sg_lb_stats()
10358 if (sgs->group_misfit_task_load < rq->misfit_task_load) { in update_sg_lb_stats()
10359 sgs->group_misfit_task_load = rq->misfit_task_load; in update_sg_lb_stats()
10362 } else if (env->idle && sched_reduced_capacity(rq, env->sd)) { in update_sg_lb_stats()
10364 if (sgs->group_misfit_task_load < load) in update_sg_lb_stats()
10365 sgs->group_misfit_task_load = load; in update_sg_lb_stats()
10369 sgs->group_capacity = group->sgc->capacity; in update_sg_lb_stats()
10371 sgs->group_weight = group->group_weight; in update_sg_lb_stats()
10374 if (!local_group && env->idle && sgs->sum_h_nr_running && in update_sg_lb_stats()
10376 sgs->group_asym_packing = 1; in update_sg_lb_stats()
10380 sgs->group_smt_balance = 1; in update_sg_lb_stats()
10382 sgs->group_type = group_classify(env->sd->imbalance_pct, group, sgs); in update_sg_lb_stats()
10385 if (sgs->group_type == group_overloaded) in update_sg_lb_stats()
10386 sgs->avg_load = (sgs->group_load * SCHED_CAPACITY_SCALE) / in update_sg_lb_stats()
10387 sgs->group_capacity; in update_sg_lb_stats()
10391 * update_sd_pick_busiest - return 1 on busiest group
10408 struct sg_lb_stats *busiest = &sds->busiest_stat; in update_sd_pick_busiest()
10411 if (!sgs->sum_h_nr_running) in update_sd_pick_busiest()
10420 if ((env->sd->flags & SD_ASYM_CPUCAPACITY) && in update_sd_pick_busiest()
10421 (sgs->group_type == group_misfit_task) && in update_sd_pick_busiest()
10422 (!capacity_greater(capacity_of(env->dst_cpu), sg->sgc->max_capacity) || in update_sd_pick_busiest()
10423 sds->local_stat.group_type != group_has_spare)) in update_sd_pick_busiest()
10426 if (sgs->group_type > busiest->group_type) in update_sd_pick_busiest()
10429 if (sgs->group_type < busiest->group_type) in update_sd_pick_busiest()
10437 switch (sgs->group_type) { in update_sd_pick_busiest()
10440 return sgs->avg_load > busiest->avg_load; in update_sd_pick_busiest()
10451 return sched_asym_prefer(sds->busiest->asym_prefer_cpu, sg->asym_prefer_cpu); in update_sd_pick_busiest()
10458 return sgs->group_misfit_task_load > busiest->group_misfit_task_load; in update_sd_pick_busiest()
10465 if (sgs->idle_cpus != 0 || busiest->idle_cpus != 0) in update_sd_pick_busiest()
10483 if (sgs->avg_load < busiest->avg_load) in update_sd_pick_busiest()
10486 if (sgs->avg_load == busiest->avg_load) { in update_sd_pick_busiest()
10488 * SMT sched groups need more help than non-SMT groups. in update_sd_pick_busiest()
10491 if (sds->busiest->flags & SD_SHARE_CPUCAPACITY) in update_sd_pick_busiest()
10503 if (smt_vs_nonsmt_groups(sds->busiest, sg)) { in update_sd_pick_busiest()
10504 if (sg->flags & SD_SHARE_CPUCAPACITY && sgs->sum_h_nr_running <= 1) in update_sd_pick_busiest()
10518 if (sgs->idle_cpus > busiest->idle_cpus) in update_sd_pick_busiest()
10520 else if ((sgs->idle_cpus == busiest->idle_cpus) && in update_sd_pick_busiest()
10521 (sgs->sum_nr_running <= busiest->sum_nr_running)) in update_sd_pick_busiest()
10529 * per-CPU capacity. Migrating tasks to less capable CPUs may harm in update_sd_pick_busiest()
10533 if ((env->sd->flags & SD_ASYM_CPUCAPACITY) && in update_sd_pick_busiest()
10534 (sgs->group_type <= group_fully_busy) && in update_sd_pick_busiest()
10535 (capacity_greater(sg->sgc->min_capacity, capacity_of(env->dst_cpu)))) in update_sd_pick_busiest()
10544 if (sgs->sum_h_nr_running > sgs->nr_numa_running) in fbq_classify_group()
10546 if (sgs->sum_h_nr_running > sgs->nr_preferred_running) in fbq_classify_group()
10553 if (rq->nr_running > rq->nr_numa_running) in fbq_classify_rq()
10555 if (rq->nr_running > rq->nr_preferred_running) in fbq_classify_rq()
10575 * task_running_on_cpu - return 1 if @p is running on @cpu.
10581 if (cpu != task_cpu(p) || !READ_ONCE(p->se.avg.last_update_time)) in task_running_on_cpu()
10591 * idle_cpu_without - would a given CPU be idle without p ?
10601 if (rq->curr != rq->idle && rq->curr != p) in idle_cpu_without()
10605 * rq->nr_running can't be used but an updated version without the in idle_cpu_without()
10610 if (rq->ttwu_pending) in idle_cpu_without()
10617 * update_sg_wakeup_stats - Update sched_group's statistics for wakeup.
10633 if (sd->flags & SD_ASYM_CPUCAPACITY) in update_sg_wakeup_stats()
10634 sgs->group_misfit_task_load = 1; in update_sg_wakeup_stats()
10640 sgs->group_load += cpu_load_without(rq, p); in update_sg_wakeup_stats()
10641 sgs->group_util += cpu_util_without(i, p); in update_sg_wakeup_stats()
10642 sgs->group_runnable += cpu_runnable_without(rq, p); in update_sg_wakeup_stats()
10644 sgs->sum_h_nr_running += rq->cfs.h_nr_running - local; in update_sg_wakeup_stats()
10646 nr_running = rq->nr_running - local; in update_sg_wakeup_stats()
10647 sgs->sum_nr_running += nr_running; in update_sg_wakeup_stats()
10653 sgs->idle_cpus++; in update_sg_wakeup_stats()
10656 if (sd->flags & SD_ASYM_CPUCAPACITY && in update_sg_wakeup_stats()
10657 sgs->group_misfit_task_load && in update_sg_wakeup_stats()
10659 sgs->group_misfit_task_load = 0; in update_sg_wakeup_stats()
10663 sgs->group_capacity = group->sgc->capacity; in update_sg_wakeup_stats()
10665 sgs->group_weight = group->group_weight; in update_sg_wakeup_stats()
10667 sgs->group_type = group_classify(sd->imbalance_pct, group, sgs); in update_sg_wakeup_stats()
10673 if (sgs->group_type == group_fully_busy || in update_sg_wakeup_stats()
10674 sgs->group_type == group_overloaded) in update_sg_wakeup_stats()
10675 sgs->avg_load = (sgs->group_load * SCHED_CAPACITY_SCALE) / in update_sg_wakeup_stats()
10676 sgs->group_capacity; in update_sg_wakeup_stats()
10684 if (sgs->group_type < idlest_sgs->group_type) in update_pick_idlest()
10687 if (sgs->group_type > idlest_sgs->group_type) in update_pick_idlest()
10695 switch (sgs->group_type) { in update_pick_idlest()
10699 if (idlest_sgs->avg_load <= sgs->avg_load) in update_pick_idlest()
10711 if (idlest->sgc->max_capacity >= group->sgc->max_capacity) in update_pick_idlest()
10717 if (idlest_sgs->idle_cpus > sgs->idle_cpus) in update_pick_idlest()
10721 if (idlest_sgs->idle_cpus == sgs->idle_cpus && in update_pick_idlest()
10722 idlest_sgs->group_util <= sgs->group_util) in update_pick_idlest()
10740 struct sched_group *idlest = NULL, *local = NULL, *group = sd->groups; in sched_balance_find_dst_group()
10754 p->cpus_ptr)) in sched_balance_find_dst_group()
10778 } while (group = group->next, group != sd->groups); in sched_balance_find_dst_group()
10809 (sd->imbalance_pct-100) / 100; in sched_balance_find_dst_group()
10816 * cross-domain, add imbalance to the load on the remote node in sched_balance_find_dst_group()
10820 if ((sd->flags & SD_NUMA) && in sched_balance_find_dst_group()
10831 if (100 * local_sgs.avg_load <= sd->imbalance_pct * idlest_sgs.avg_load) in sched_balance_find_dst_group()
10843 if (local->sgc->max_capacity >= idlest->sgc->max_capacity) in sched_balance_find_dst_group()
10849 if (sd->flags & SD_NUMA) { in sched_balance_find_dst_group()
10850 int imb_numa_nr = sd->imb_numa_nr; in sched_balance_find_dst_group()
10857 if (cpu_to_node(this_cpu) == p->numa_preferred_nid) in sched_balance_find_dst_group()
10861 if (cpu_to_node(idlest_cpu) == p->numa_preferred_nid) in sched_balance_find_dst_group()
10873 if (p->nr_cpus_allowed != NR_CPUS) { in sched_balance_find_dst_group()
10876 cpumask_and(cpus, sched_group_span(local), p->cpus_ptr); in sched_balance_find_dst_group()
10877 imb_numa_nr = min(cpumask_weight(cpus), sd->imb_numa_nr); in sched_balance_find_dst_group()
10880 imbalance = abs(local_sgs.idle_cpus - idlest_sgs.idle_cpus); in sched_balance_find_dst_group()
10917 if (!sched_feat(SIS_UTIL) || env->idle == CPU_NEWLY_IDLE) in update_idle_cpu_scan()
10920 llc_weight = per_cpu(sd_llc_size, env->dst_cpu); in update_idle_cpu_scan()
10921 if (env->sd->span_weight != llc_weight) in update_idle_cpu_scan()
10924 sd_share = rcu_dereference(per_cpu(sd_llc_shared, env->dst_cpu)); in update_idle_cpu_scan()
10934 * let y = SCHED_CAPACITY_SCALE - p * x^2 [1] in update_idle_cpu_scan()
10952 * y = SCHED_CAPACITY_SCALE - in update_idle_cpu_scan()
10961 pct = env->sd->imbalance_pct; in update_idle_cpu_scan()
10965 y = SCHED_CAPACITY_SCALE - tmp; in update_idle_cpu_scan()
10970 if ((int)y != sd_share->nr_idle_scan) in update_idle_cpu_scan()
10971 WRITE_ONCE(sd_share->nr_idle_scan, (int)y); in update_idle_cpu_scan()
10975 * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
10982 struct sched_group *sg = env->sd->groups; in update_sd_lb_stats()
10983 struct sg_lb_stats *local = &sds->local_stat; in update_sd_lb_stats()
10992 local_group = cpumask_test_cpu(env->dst_cpu, sched_group_span(sg)); in update_sd_lb_stats()
10994 sds->local = sg; in update_sd_lb_stats()
10997 if (env->idle != CPU_NEWLY_IDLE || in update_sd_lb_stats()
10998 time_after_eq(jiffies, sg->sgc->next_update)) in update_sd_lb_stats()
10999 update_group_capacity(env->sd, env->dst_cpu); in update_sd_lb_stats()
11005 sds->busiest = sg; in update_sd_lb_stats()
11006 sds->busiest_stat = *sgs; in update_sd_lb_stats()
11010 sds->total_load += sgs->group_load; in update_sd_lb_stats()
11011 sds->total_capacity += sgs->group_capacity; in update_sd_lb_stats()
11013 sum_util += sgs->group_util; in update_sd_lb_stats()
11014 sg = sg->next; in update_sd_lb_stats()
11015 } while (sg != env->sd->groups); in update_sd_lb_stats()
11022 if (sds->busiest) in update_sd_lb_stats()
11023 sds->prefer_sibling = !!(sds->busiest->flags & SD_PREFER_SIBLING); in update_sd_lb_stats()
11026 if (env->sd->flags & SD_NUMA) in update_sd_lb_stats()
11027 env->fbq_type = fbq_classify_group(&sds->busiest_stat); in update_sd_lb_stats()
11029 if (!env->sd->parent) { in update_sd_lb_stats()
11031 set_rd_overloaded(env->dst_rq->rd, sg_overloaded); in update_sd_lb_stats()
11033 /* Update over-utilization (tipping point, U >= 0) indicator */ in update_sd_lb_stats()
11034 set_rd_overutilized(env->dst_rq->rd, sg_overutilized); in update_sd_lb_stats()
11036 set_rd_overutilized(env->dst_rq->rd, sg_overutilized); in update_sd_lb_stats()
11043 * calculate_imbalance - Calculate the amount of imbalance present within the
11052 local = &sds->local_stat; in calculate_imbalance()
11053 busiest = &sds->busiest_stat; in calculate_imbalance()
11055 if (busiest->group_type == group_misfit_task) { in calculate_imbalance()
11056 if (env->sd->flags & SD_ASYM_CPUCAPACITY) { in calculate_imbalance()
11058 env->migration_type = migrate_misfit; in calculate_imbalance()
11059 env->imbalance = 1; in calculate_imbalance()
11065 env->migration_type = migrate_load; in calculate_imbalance()
11066 env->imbalance = busiest->group_misfit_task_load; in calculate_imbalance()
11071 if (busiest->group_type == group_asym_packing) { in calculate_imbalance()
11076 env->migration_type = migrate_task; in calculate_imbalance()
11077 env->imbalance = busiest->sum_h_nr_running; in calculate_imbalance()
11081 if (busiest->group_type == group_smt_balance) { in calculate_imbalance()
11083 env->migration_type = migrate_task; in calculate_imbalance()
11084 env->imbalance = 1; in calculate_imbalance()
11088 if (busiest->group_type == group_imbalanced) { in calculate_imbalance()
11090 * In the group_imb case we cannot rely on group-wide averages in calculate_imbalance()
11091 * to ensure CPU-load equilibrium, try to move any task to fix in calculate_imbalance()
11095 env->migration_type = migrate_task; in calculate_imbalance()
11096 env->imbalance = 1; in calculate_imbalance()
11104 if (local->group_type == group_has_spare) { in calculate_imbalance()
11105 if ((busiest->group_type > group_fully_busy) && in calculate_imbalance()
11106 !(env->sd->flags & SD_SHARE_LLC)) { in calculate_imbalance()
11115 env->migration_type = migrate_util; in calculate_imbalance()
11116 env->imbalance = max(local->group_capacity, local->group_util) - in calculate_imbalance()
11117 local->group_util; in calculate_imbalance()
11126 if (env->idle && env->imbalance == 0) { in calculate_imbalance()
11127 env->migration_type = migrate_task; in calculate_imbalance()
11128 env->imbalance = 1; in calculate_imbalance()
11134 if (busiest->group_weight == 1 || sds->prefer_sibling) { in calculate_imbalance()
11139 env->migration_type = migrate_task; in calculate_imbalance()
11140 env->imbalance = sibling_imbalance(env, sds, busiest, local); in calculate_imbalance()
11147 env->migration_type = migrate_task; in calculate_imbalance()
11148 env->imbalance = max_t(long, 0, in calculate_imbalance()
11149 (local->idle_cpus - busiest->idle_cpus)); in calculate_imbalance()
11154 if (env->sd->flags & SD_NUMA) { in calculate_imbalance()
11155 env->imbalance = adjust_numa_imbalance(env->imbalance, in calculate_imbalance()
11156 local->sum_nr_running + 1, in calculate_imbalance()
11157 env->sd->imb_numa_nr); in calculate_imbalance()
11162 env->imbalance >>= 1; in calculate_imbalance()
11171 if (local->group_type < group_overloaded) { in calculate_imbalance()
11177 local->avg_load = (local->group_load * SCHED_CAPACITY_SCALE) / in calculate_imbalance()
11178 local->group_capacity; in calculate_imbalance()
11184 if (local->avg_load >= busiest->avg_load) { in calculate_imbalance()
11185 env->imbalance = 0; in calculate_imbalance()
11189 sds->avg_load = (sds->total_load * SCHED_CAPACITY_SCALE) / in calculate_imbalance()
11190 sds->total_capacity; in calculate_imbalance()
11196 if (local->avg_load >= sds->avg_load) { in calculate_imbalance()
11197 env->imbalance = 0; in calculate_imbalance()
11211 env->migration_type = migrate_load; in calculate_imbalance()
11212 env->imbalance = min( in calculate_imbalance()
11213 (busiest->avg_load - sds->avg_load) * busiest->group_capacity, in calculate_imbalance()
11214 (sds->avg_load - local->avg_load) * local->group_capacity in calculate_imbalance()
11241 * sched_balance_find_src_group - Returns the busiest group within the sched_domain
11248 * Return: - The busiest group if imbalance exists.
11270 if (busiest->group_type == group_misfit_task) in sched_balance_find_src_group()
11273 if (!is_rd_overutilized(env->dst_rq->rd) && in sched_balance_find_src_group()
11274 rcu_dereference(env->dst_rq->rd->pd)) in sched_balance_find_src_group()
11278 if (busiest->group_type == group_asym_packing) in sched_balance_find_src_group()
11286 if (busiest->group_type == group_imbalanced) in sched_balance_find_src_group()
11294 if (local->group_type > busiest->group_type) in sched_balance_find_src_group()
11301 if (local->group_type == group_overloaded) { in sched_balance_find_src_group()
11306 if (local->avg_load >= busiest->avg_load) in sched_balance_find_src_group()
11317 if (local->avg_load >= sds.avg_load) in sched_balance_find_src_group()
11324 if (100 * busiest->avg_load <= in sched_balance_find_src_group()
11325 env->sd->imbalance_pct * local->avg_load) in sched_balance_find_src_group()
11333 if (sds.prefer_sibling && local->group_type == group_has_spare && in sched_balance_find_src_group()
11337 if (busiest->group_type != group_overloaded) { in sched_balance_find_src_group()
11338 if (!env->idle) { in sched_balance_find_src_group()
11347 if (busiest->group_type == group_smt_balance && in sched_balance_find_src_group()
11353 if (busiest->group_weight > 1 && in sched_balance_find_src_group()
11354 local->idle_cpus <= (busiest->idle_cpus + 1)) { in sched_balance_find_src_group()
11367 if (busiest->sum_h_nr_running == 1) { in sched_balance_find_src_group()
11378 return env->imbalance ? sds.busiest : NULL; in sched_balance_find_src_group()
11381 env->imbalance = 0; in sched_balance_find_src_group()
11386 * sched_balance_find_src_rq - find the busiest runqueue among the CPUs in the group.
11396 for_each_cpu_and(i, sched_group_span(group), env->cpus) { in sched_balance_find_src_rq()
11406 * - regular: there are !numa tasks in sched_balance_find_src_rq()
11407 * - remote: there are numa tasks that run on the 'wrong' node in sched_balance_find_src_rq()
11408 * - all: there is no distinction in sched_balance_find_src_rq()
11423 if (rt > env->fbq_type) in sched_balance_find_src_rq()
11426 nr_running = rq->cfs.h_nr_running; in sched_balance_find_src_rq()
11434 * eventually lead to active_balancing high->low capacity. in sched_balance_find_src_rq()
11435 * Higher per-CPU capacity is considered better than balancing in sched_balance_find_src_rq()
11438 if (env->sd->flags & SD_ASYM_CPUCAPACITY && in sched_balance_find_src_rq()
11439 !capacity_greater(capacity_of(env->dst_cpu), capacity) && in sched_balance_find_src_rq()
11450 if (sched_asym(env->sd, i, env->dst_cpu) && nr_running == 1) in sched_balance_find_src_rq()
11453 switch (env->migration_type) { in sched_balance_find_src_rq()
11461 if (nr_running == 1 && load > env->imbalance && in sched_balance_find_src_rq()
11462 !check_cpu_capacity(rq, env->sd)) in sched_balance_find_src_rq()
11514 if (rq->misfit_task_load > busiest_load) { in sched_balance_find_src_rq()
11515 busiest_load = rq->misfit_task_load; in sched_balance_find_src_rq()
11546 return env->idle && sched_use_asym_prio(env->sd, env->dst_cpu) && in asym_active_balance()
11547 (sched_asym_prefer(env->dst_cpu, env->src_cpu) || in asym_active_balance()
11548 !sched_use_asym_prio(env->sd, env->src_cpu)); in asym_active_balance()
11554 struct sched_domain *sd = env->sd; in imbalanced_active_balance()
11561 if ((env->migration_type == migrate_task) && in imbalanced_active_balance()
11562 (sd->nr_balance_failed > sd->cache_nice_tries+2)) in imbalanced_active_balance()
11570 struct sched_domain *sd = env->sd; in need_active_balance()
11584 if (env->idle && in need_active_balance()
11585 (env->src_rq->cfs.h_nr_running == 1)) { in need_active_balance()
11586 if ((check_cpu_capacity(env->src_rq, sd)) && in need_active_balance()
11587 (capacity_of(env->src_cpu)*sd->imbalance_pct < capacity_of(env->dst_cpu)*100)) in need_active_balance()
11591 if (env->migration_type == migrate_misfit) in need_active_balance()
11602 struct sched_group *sg = env->sd->groups; in should_we_balance()
11603 int cpu, idle_smt = -1; in should_we_balance()
11609 if (!cpumask_test_cpu(env->dst_cpu, env->cpus)) in should_we_balance()
11619 if (env->idle == CPU_NEWLY_IDLE) { in should_we_balance()
11620 if (env->dst_rq->nr_running > 0 || env->dst_rq->ttwu_pending) in should_we_balance()
11627 for_each_cpu_and(cpu, swb_cpus, env->cpus) { in should_we_balance()
11636 if (!(env->sd->flags & SD_SHARE_CPUCAPACITY) && !is_core_idle(cpu)) { in should_we_balance()
11637 if (idle_smt == -1) in should_we_balance()
11651 * Are we the first idle core in a non-SMT domain or higher, in should_we_balance()
11654 return cpu == env->dst_cpu; in should_we_balance()
11658 if (idle_smt != -1) in should_we_balance()
11659 return idle_smt == env->dst_cpu; in should_we_balance()
11662 return group_balance_cpu(sg) == env->dst_cpu; in should_we_balance()
11674 struct sched_domain *sd_parent = sd->parent; in sched_balance_rq()
11683 .dst_grpmask = group_balance_mask(sd->groups), in sched_balance_rq()
11693 schedstat_inc(sd->lb_count[idle]); in sched_balance_rq()
11703 schedstat_inc(sd->lb_nobusyg[idle]); in sched_balance_rq()
11709 schedstat_inc(sd->lb_nobusyq[idle]); in sched_balance_rq()
11715 schedstat_add(sd->lb_imbalance[idle], env.imbalance); in sched_balance_rq()
11717 env.src_cpu = busiest->cpu; in sched_balance_rq()
11723 if (busiest->nr_running > 1) { in sched_balance_rq()
11726 * an imbalance but busiest->nr_running <= 1, the group is in sched_balance_rq()
11730 env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running); in sched_balance_rq()
11737 * cur_ld_moved - load moved in current iteration in sched_balance_rq()
11738 * ld_moved - cumulative load moved across iterations in sched_balance_rq()
11745 * unlock busiest->lock, and we are able to be sure in sched_balance_rq()
11774 * nohz-idle), we now have balance_cpu in a position to move in sched_balance_rq()
11785 /* Prevent to re-select dst_cpu via env's CPUs */ in sched_balance_rq()
11805 int *group_imbalance = &sd_parent->groups->sgc->imbalance; in sched_balance_rq()
11832 schedstat_inc(sd->lb_failed[idle]); in sched_balance_rq()
11844 sd->nr_balance_failed++; in sched_balance_rq()
11856 if (!cpumask_test_cpu(this_cpu, busiest->curr->cpus_ptr)) { in sched_balance_rq()
11865 * ->active_balance synchronizes accesses to in sched_balance_rq()
11866 * ->active_balance_work. Once set, it's cleared in sched_balance_rq()
11869 if (!busiest->active_balance) { in sched_balance_rq()
11870 busiest->active_balance = 1; in sched_balance_rq()
11871 busiest->push_cpu = this_cpu; in sched_balance_rq()
11880 &busiest->active_balance_work); in sched_balance_rq()
11885 sd->nr_balance_failed = 0; in sched_balance_rq()
11890 sd->balance_interval = sd->min_interval; in sched_balance_rq()
11902 int *group_imbalance = &sd_parent->groups->sgc->imbalance; in sched_balance_rq()
11914 schedstat_inc(sd->lb_balanced[idle]); in sched_balance_rq()
11916 sd->nr_balance_failed = 0; in sched_balance_rq()
11937 sd->balance_interval < MAX_PINNED_INTERVAL) || in sched_balance_rq()
11938 sd->balance_interval < sd->max_interval) in sched_balance_rq()
11939 sd->balance_interval *= 2; in sched_balance_rq()
11947 unsigned long interval = sd->balance_interval; in get_sd_balance_interval()
11950 interval *= sd->busy_factor; in get_sd_balance_interval()
11952 /* scale ms to jiffies */ in get_sd_balance_interval()
11961 interval -= 1; in get_sd_balance_interval()
11975 next = sd->last_balance + interval; in update_next_balance()
11991 int target_cpu = busiest_rq->push_cpu; in active_load_balance_cpu_stop()
11999 * Between queueing the stop-work and running it is a hole in which in active_load_balance_cpu_stop()
12008 !busiest_rq->active_balance)) in active_load_balance_cpu_stop()
12012 if (busiest_rq->nr_running <= 1) in active_load_balance_cpu_stop()
12018 * Bjorn Helgaas on a 128-CPU setup. in active_load_balance_cpu_stop()
12034 .src_cpu = busiest_rq->cpu, in active_load_balance_cpu_stop()
12040 schedstat_inc(sd->alb_count); in active_load_balance_cpu_stop()
12045 schedstat_inc(sd->alb_pushed); in active_load_balance_cpu_stop()
12047 sd->nr_balance_failed = 0; in active_load_balance_cpu_stop()
12049 schedstat_inc(sd->alb_failed); in active_load_balance_cpu_stop()
12054 busiest_rq->active_balance = 0; in active_load_balance_cpu_stop()
12066 * This flag serializes load-balancing passes over large domains
12067 * (above the NODE topology level) - only one load-balancing instance
12071 * - Note that load-balancing passes triggered while another one
12072 * is executing are skipped and not re-tried.
12074 * - Also note that this does not serialize rebalance_domains()
12075 * execution, as non-SD_SERIALIZE domains will still be
12076 * load-balanced in parallel.
12082 * This trades load-balance latency on larger machines for less cross talk.
12091 if (cost > sd->max_newidle_lb_cost) { in update_newidle_cost()
12096 sd->max_newidle_lb_cost = cost; in update_newidle_cost()
12097 sd->last_decay_max_lb_cost = jiffies; in update_newidle_cost()
12098 } else if (time_after(jiffies, sd->last_decay_max_lb_cost + HZ)) { in update_newidle_cost()
12104 sd->max_newidle_lb_cost = (sd->max_newidle_lb_cost * 253) / 256; in update_newidle_cost()
12105 sd->last_decay_max_lb_cost = jiffies; in update_newidle_cost()
12122 int cpu = rq->cpu; in sched_balance_domains()
12139 max_cost += sd->max_newidle_lb_cost; in sched_balance_domains()
12154 need_serialize = sd->flags & SD_SERIALIZE; in sched_balance_domains()
12160 if (time_after_eq(jiffies, sd->last_balance + interval)) { in sched_balance_domains()
12164 * env->dst_cpu, so we can't know our idle in sched_balance_domains()
12170 sd->last_balance = jiffies; in sched_balance_domains()
12176 if (time_after(next_balance, sd->last_balance + interval)) { in sched_balance_domains()
12177 next_balance = sd->last_balance + interval; in sched_balance_domains()
12183 * Ensure the rq-wide value also decays but keep it at a in sched_balance_domains()
12184 * reasonable floor to avoid funnies with rq->avg_idle. in sched_balance_domains()
12186 rq->max_idle_balance_cost = in sched_balance_domains()
12197 rq->next_balance = next_balance; in sched_balance_domains()
12203 return unlikely(!rcu_dereference_sched(rq->sd)); in on_null_domain()
12210 * - When one of the busy CPUs notices that there may be an idle rebalancing
12214 * - HK_TYPE_MISC CPUs are used for this task, because HK_TYPE_SCHED is not set
12233 return -1; in find_new_ilb()
12237 * Kick a CPU to do the NOHZ balancing, if it is time for it, via a cross-CPU
12277 smp_call_function_single_async(ilb_cpu, &cpu_rq(ilb_cpu)->nohz_csd); in kick_ilb()
12289 int nr_busy, i, cpu = rq->cpu; in nohz_balancer_kick()
12292 if (unlikely(rq->idle_balance)) in nohz_balancer_kick()
12315 if (rq->nr_running >= 2) { in nohz_balancer_kick()
12322 sd = rcu_dereference(rq->sd); in nohz_balancer_kick()
12328 if (rq->cfs.h_nr_running >= 1 && check_cpu_capacity(rq, sd)) { in nohz_balancer_kick()
12377 * increase the overall cache utilization), we need a less-loaded LLC in nohz_balancer_kick()
12381 * the others are - so just get a NOHZ balance going if it looks in nohz_balancer_kick()
12384 nr_busy = atomic_read(&sds->nr_busy_cpus); in nohz_balancer_kick()
12407 if (!sd || !sd->nohz_idle) in set_cpu_sd_state_busy()
12409 sd->nohz_idle = 0; in set_cpu_sd_state_busy()
12411 atomic_inc(&sd->shared->nr_busy_cpus); in set_cpu_sd_state_busy()
12420 if (likely(!rq->nohz_tick_stopped)) in nohz_balance_exit_idle()
12423 rq->nohz_tick_stopped = 0; in nohz_balance_exit_idle()
12424 cpumask_clear_cpu(rq->cpu, nohz.idle_cpus_mask); in nohz_balance_exit_idle()
12427 set_cpu_sd_state_busy(rq->cpu); in nohz_balance_exit_idle()
12437 if (!sd || sd->nohz_idle) in set_cpu_sd_state_idle()
12439 sd->nohz_idle = 1; in set_cpu_sd_state_idle()
12441 atomic_dec(&sd->shared->nr_busy_cpus); in set_cpu_sd_state_idle()
12465 * Can be set safely without rq->lock held in nohz_balance_enter_idle()
12467 * rq->lock is held during the check and the clear in nohz_balance_enter_idle()
12469 rq->has_blocked_load = 1; in nohz_balance_enter_idle()
12477 if (rq->nohz_tick_stopped) in nohz_balance_enter_idle()
12484 rq->nohz_tick_stopped = 1; in nohz_balance_enter_idle()
12509 unsigned int cpu = rq->cpu; in update_nohz_stats()
12511 if (!rq->has_blocked_load) in update_nohz_stats()
12517 if (!time_after(jiffies, READ_ONCE(rq->last_blocked_load_update_tick))) in update_nohz_stats()
12522 return rq->has_blocked_load; in update_nohz_stats()
12537 int this_cpu = this_rq->cpu; in _nohz_idle_balance()
12594 if (time_after_eq(jiffies, rq->next_balance)) { in _nohz_idle_balance()
12605 if (time_after(next_balance, rq->next_balance)) { in _nohz_idle_balance()
12606 next_balance = rq->next_balance; in _nohz_idle_balance()
12635 unsigned int flags = this_rq->nohz_idle_balance; in nohz_idle_balance()
12640 this_rq->nohz_idle_balance = 0; in nohz_idle_balance()
12681 int this_cpu = this_rq->cpu; in nohz_newidle_balance()
12691 if (this_rq->avg_idle < sysctl_sched_migration_cost) in nohz_newidle_balance()
12722 * < 0 - we released the lock and there are !fair tasks present
12723 * 0 - failed, no new tasks
12724 * > 0 - success, new (fair) tasks present
12729 int this_cpu = this_rq->cpu; in sched_balance_newidle()
12741 if (this_rq->ttwu_pending) in sched_balance_newidle()
12749 this_rq->idle_stamp = rq_clock(this_rq); in sched_balance_newidle()
12759 * for load-balance and preemption/IRQs are still disabled avoiding in sched_balance_newidle()
12761 * re-start the picking loop. in sched_balance_newidle()
12766 sd = rcu_dereference_check_sched_domain(this_rq->sd); in sched_balance_newidle()
12768 if (!get_rd_overloaded(this_rq->rd) || in sched_balance_newidle()
12769 (sd && this_rq->avg_idle < sd->max_newidle_lb_cost)) { in sched_balance_newidle()
12790 if (this_rq->avg_idle < curr_cost + sd->max_newidle_lb_cost) in sched_balance_newidle()
12793 if (sd->flags & SD_BALANCE_NEWIDLE) { in sched_balance_newidle()
12800 domain_cost = t1 - t0; in sched_balance_newidle()
12818 if (curr_cost > this_rq->max_idle_balance_cost) in sched_balance_newidle()
12819 this_rq->max_idle_balance_cost = curr_cost; in sched_balance_newidle()
12826 if (this_rq->cfs.h_nr_running && !pulled_task) in sched_balance_newidle()
12830 if (this_rq->nr_running != this_rq->cfs.h_nr_running) in sched_balance_newidle()
12831 pulled_task = -1; in sched_balance_newidle()
12835 if (time_after(this_rq->next_balance, next_balance)) in sched_balance_newidle()
12836 this_rq->next_balance = next_balance; in sched_balance_newidle()
12839 this_rq->idle_stamp = 0; in sched_balance_newidle()
12851 * - directly from the local scheduler_tick() for periodic load balancing
12853 * - indirectly from a remote scheduler_tick() for NOHZ idle balancing
12854 * through the SMP cross-call nohz_csd_func()
12859 enum cpu_idle_type idle = this_rq->idle_balance; in sched_balance_softirq()
12872 sched_balance_update_blocked_averages(this_rq->cpu); in sched_balance_softirq()
12888 if (time_after_eq(jiffies, rq->next_balance)) in sched_balance_trigger()
12918 u64 rtime = se->sum_exec_runtime - se->prev_sum_exec_runtime; in __entity_slice_used()
12919 u64 slice = se->slice; in __entity_slice_used()
12941 * MIN_NR_TASKS_DURING_FORCEIDLE - 1 tasks and use that to check in task_tick_core()
12944 if (rq->core->core_forceidle_count && rq->cfs.nr_running == 1 && in task_tick_core()
12945 __entity_slice_used(&curr->se, MIN_NR_TASKS_DURING_FORCEIDLE)) in task_tick_core()
12950 * se_fi_update - Update the cfs_rq->min_vruntime_fi in a CFS hierarchy if needed.
12959 if (cfs_rq->forceidle_seq == fi_seq) in se_fi_update()
12961 cfs_rq->forceidle_seq = fi_seq; in se_fi_update()
12964 cfs_rq->min_vruntime_fi = cfs_rq->min_vruntime; in se_fi_update()
12970 struct sched_entity *se = &p->se; in task_vruntime_update()
12972 if (p->sched_class != &fair_sched_class) in task_vruntime_update()
12975 se_fi_update(se, rq->core->core_forceidle_seq, in_fi); in task_vruntime_update()
12982 const struct sched_entity *sea = &a->se; in cfs_prio_less()
12983 const struct sched_entity *seb = &b->se; in cfs_prio_less()
12988 SCHED_WARN_ON(task_rq(b)->core != rq->core); in cfs_prio_less()
12995 while (sea->cfs_rq->tg != seb->cfs_rq->tg) { in cfs_prio_less()
12996 int sea_depth = sea->depth; in cfs_prio_less()
12997 int seb_depth = seb->depth; in cfs_prio_less()
13005 se_fi_update(sea, rq->core->core_forceidle_seq, in_fi); in cfs_prio_less()
13006 se_fi_update(seb, rq->core->core_forceidle_seq, in_fi); in cfs_prio_less()
13008 cfs_rqa = sea->cfs_rq; in cfs_prio_less()
13009 cfs_rqb = seb->cfs_rq; in cfs_prio_less()
13011 cfs_rqa = &task_rq(a)->cfs; in cfs_prio_less()
13012 cfs_rqb = &task_rq(b)->cfs; in cfs_prio_less()
13020 delta = (s64)(sea->vruntime - seb->vruntime) + in cfs_prio_less()
13021 (s64)(cfs_rqb->min_vruntime_fi - cfs_rqa->min_vruntime_fi); in cfs_prio_less()
13031 cfs_rq = task_group(p)->cfs_rq[cpu]; in task_is_throttled_fair()
13033 cfs_rq = &cpu_rq(cpu)->cfs; in task_is_throttled_fair()
13052 struct sched_entity *se = &curr->se; in task_tick_fair()
13070 * - child not yet on the tasklist
13071 * - preemption disabled
13088 if (rq->cfs.nr_running == 1) in prio_changed_fair()
13097 if (p->prio > oldprio) in prio_changed_fair()
13119 se = se->parent; in propagate_entity_cfs_rq()
13144 * - A forked task which hasn't been woken up by wake_up_new_task(). in detach_entity_cfs_rq()
13145 * - A task which has been woken up by try_to_wake_up() but is in detach_entity_cfs_rq()
13148 if (!se->avg.last_update_time) in detach_entity_cfs_rq()
13172 struct sched_entity *se = &p->se; in detach_task_cfs_rq()
13179 struct sched_entity *se = &p->se; in attach_task_cfs_rq()
13191 SCHED_WARN_ON(p->se.sched_delayed); in switched_to_fair()
13212 struct sched_entity *se = &p->se; in __set_next_task_fair()
13220 list_move(&se->group_node, &rq->cfs_tasks); in __set_next_task_fair()
13226 SCHED_WARN_ON(se->sched_delayed); in __set_next_task_fair()
13238 * This routine is mostly called to set cfs_rq->curr field when a task
13243 struct sched_entity *se = &p->se; in set_next_task_fair()
13258 cfs_rq->tasks_timeline = RB_ROOT_CACHED; in init_cfs_rq()
13259 cfs_rq->min_vruntime = (u64)(-(1LL << 20)); in init_cfs_rq()
13261 raw_spin_lock_init(&cfs_rq->removed.lock); in init_cfs_rq()
13272 if (READ_ONCE(p->__state) == TASK_NEW) in task_change_group_fair()
13278 /* Tell se's cfs_rq has been changed -- migrated */ in task_change_group_fair()
13279 p->se.avg.last_update_time = 0; in task_change_group_fair()
13290 if (tg->cfs_rq) in free_fair_sched_group()
13291 kfree(tg->cfs_rq[i]); in free_fair_sched_group()
13292 if (tg->se) in free_fair_sched_group()
13293 kfree(tg->se[i]); in free_fair_sched_group()
13296 kfree(tg->cfs_rq); in free_fair_sched_group()
13297 kfree(tg->se); in free_fair_sched_group()
13306 tg->cfs_rq = kcalloc(nr_cpu_ids, sizeof(cfs_rq), GFP_KERNEL); in alloc_fair_sched_group()
13307 if (!tg->cfs_rq) in alloc_fair_sched_group()
13309 tg->se = kcalloc(nr_cpu_ids, sizeof(se), GFP_KERNEL); in alloc_fair_sched_group()
13310 if (!tg->se) in alloc_fair_sched_group()
13313 tg->shares = NICE_0_LOAD; in alloc_fair_sched_group()
13329 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]); in alloc_fair_sched_group()
13350 se = tg->se[i]; in online_fair_sched_group()
13366 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu]; in unregister_fair_sched_group()
13367 struct sched_entity *se = tg->se[cpu]; in unregister_fair_sched_group()
13371 if (se->sched_delayed) { in unregister_fair_sched_group()
13373 if (se->sched_delayed) { in unregister_fair_sched_group()
13384 * check on_list without danger of it being re-added. in unregister_fair_sched_group()
13386 if (cfs_rq->on_list) { in unregister_fair_sched_group()
13399 cfs_rq->tg = tg; in init_tg_cfs_entry()
13400 cfs_rq->rq = rq; in init_tg_cfs_entry()
13403 tg->cfs_rq[cpu] = cfs_rq; in init_tg_cfs_entry()
13404 tg->se[cpu] = se; in init_tg_cfs_entry()
13411 se->cfs_rq = &rq->cfs; in init_tg_cfs_entry()
13412 se->depth = 0; in init_tg_cfs_entry()
13414 se->cfs_rq = parent->my_q; in init_tg_cfs_entry()
13415 se->depth = parent->depth + 1; in init_tg_cfs_entry()
13418 se->my_q = cfs_rq; in init_tg_cfs_entry()
13420 update_load_set(&se->load, NICE_0_LOAD); in init_tg_cfs_entry()
13421 se->parent = parent; in init_tg_cfs_entry()
13435 if (!tg->se[0]) in __sched_group_set_shares()
13436 return -EINVAL; in __sched_group_set_shares()
13440 if (tg->shares == shares) in __sched_group_set_shares()
13443 tg->shares = shares; in __sched_group_set_shares()
13446 struct sched_entity *se = tg->se[i]; in __sched_group_set_shares()
13468 ret = -EINVAL; in sched_group_set_shares()
13481 return -EINVAL; in sched_group_set_idle()
13484 return -EINVAL; in sched_group_set_idle()
13488 if (tg->idle == idle) { in sched_group_set_idle()
13493 tg->idle = idle; in sched_group_set_idle()
13497 struct sched_entity *se = tg->se[i]; in sched_group_set_idle()
13498 struct cfs_rq *parent_cfs_rq, *grp_cfs_rq = tg->cfs_rq[i]; in sched_group_set_idle()
13505 grp_cfs_rq->idle = idle; in sched_group_set_idle()
13509 if (se->on_rq) { in sched_group_set_idle()
13512 parent_cfs_rq->idle_nr_running++; in sched_group_set_idle()
13514 parent_cfs_rq->idle_nr_running--; in sched_group_set_idle()
13517 idle_task_delta = grp_cfs_rq->h_nr_running - in sched_group_set_idle()
13518 grp_cfs_rq->idle_h_nr_running; in sched_group_set_idle()
13520 idle_task_delta *= -1; in sched_group_set_idle()
13525 if (!se->on_rq) in sched_group_set_idle()
13528 cfs_rq->idle_h_nr_running += idle_task_delta; in sched_group_set_idle()
13554 struct sched_entity *se = &task->se; in get_rr_interval_fair()
13561 if (rq->cfs.load.weight) in get_rr_interval_fair()
13562 rr_interval = NS_TO_JIFFIES(se->slice); in get_rr_interval_fair()
13640 ng = rcu_dereference(p->numa_group); in show_numa_stats()
13642 if (p->numa_faults) { in show_numa_stats()
13643 tsf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 0)]; in show_numa_stats()
13644 tpf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 1)]; in show_numa_stats()
13647 gsf = ng->faults[task_faults_idx(NUMA_MEM, node, 0)], in show_numa_stats()
13648 gpf = ng->faults[task_faults_idx(NUMA_MEM, node, 1)]; in show_numa_stats()
13669 INIT_CSD(&cpu_rq(i)->cfsb_csd, __cfsb_csd_unthrottle, cpu_rq(i)); in init_sched_fair_class()
13670 INIT_LIST_HEAD(&cpu_rq(i)->cfsb_csd_list); in init_sched_fair_class()