| // SPDX-License-Identifier: GPL-2.0 |
| /* |
| * Completely Fair Scheduling (CFS) Class (SCHED_NORMAL/SCHED_BATCH) |
| * |
| * Copyright (C) 2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> |
| * |
| * Interactivity improvements by Mike Galbraith |
| * (C) 2007 Mike Galbraith <efault@gmx.de> |
| * |
| * Various enhancements by Dmitry Adamushko. |
| * (C) 2007 Dmitry Adamushko <dmitry.adamushko@gmail.com> |
| * |
| * Group scheduling enhancements by Srivatsa Vaddagiri |
| * Copyright IBM Corporation, 2007 |
| * Author: Srivatsa Vaddagiri <vatsa@linux.vnet.ibm.com> |
| * |
| * Scaled math optimizations by Thomas Gleixner |
| * Copyright (C) 2007, Thomas Gleixner <tglx@linutronix.de> |
| * |
| * Adaptive scheduling granularity, math enhancements by Peter Zijlstra |
| * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra |
| */ |
| #include "sched.h" |
| |
| #include <trace/events/sched.h> |
| |
| /* |
| * Targeted preemption latency for CPU-bound tasks: |
| * |
| * NOTE: this latency value is not the same as the concept of |
| * 'timeslice length' - timeslices in CFS are of variable length |
| * and have no persistent notion like in traditional, time-slice |
| * based scheduling concepts. |
| * |
| * (to see the precise effective timeslice length of your workload, |
| * run vmstat and monitor the context-switches (cs) field) |
| * |
| * (default: 6ms * (1 + ilog(ncpus)), units: nanoseconds) |
| */ |
| unsigned int sysctl_sched_latency = 6000000ULL; |
| static unsigned int normalized_sysctl_sched_latency = 6000000ULL; |
| |
| /* |
| * The initial- and re-scaling of tunables is configurable |
| * |
| * Options are: |
| * |
| * SCHED_TUNABLESCALING_NONE - unscaled, always *1 |
| * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus) |
| * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus |
| * |
| * (default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus)) |
| */ |
| enum sched_tunable_scaling sysctl_sched_tunable_scaling = SCHED_TUNABLESCALING_LOG; |
| |
| /* |
| * Minimal preemption granularity for CPU-bound tasks: |
| * |
| * (default: 0.75 msec * (1 + ilog(ncpus)), units: nanoseconds) |
| */ |
| unsigned int sysctl_sched_min_granularity = 750000ULL; |
| static unsigned int normalized_sysctl_sched_min_granularity = 750000ULL; |
| |
| /* |
| * This value is kept at sysctl_sched_latency/sysctl_sched_min_granularity |
| */ |
| static unsigned int sched_nr_latency = 8; |
| |
| /* |
| * After fork, child runs first. If set to 0 (default) then |
| * parent will (try to) run first. |
| */ |
| unsigned int sysctl_sched_child_runs_first __read_mostly; |
| |
| /* |
| * SCHED_OTHER wake-up granularity. |
| * |
| * This option delays the preemption effects of decoupled workloads |
| * and reduces their over-scheduling. Synchronous workloads will still |
| * have immediate wakeup/sleep latencies. |
| * |
| * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds) |
| */ |
| unsigned int sysctl_sched_wakeup_granularity = 1000000UL; |
| static unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL; |
| |
| const_debug unsigned int sysctl_sched_migration_cost = 500000UL; |
| |
| #ifdef CONFIG_SMP |
| /* |
| * For asym packing, by default the lower numbered CPU has higher priority. |
| */ |
| int __weak arch_asym_cpu_priority(int cpu) |
| { |
| return -cpu; |
| } |
| |
| /* |
| * The margin used when comparing utilization with CPU capacity: |
| * util * margin < capacity * 1024 |
| * |
| * (default: ~20%) |
| */ |
| static unsigned int capacity_margin = 1280; |
| #endif |
| |
| #ifdef CONFIG_CFS_BANDWIDTH |
| /* |
| * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool |
| * each time a cfs_rq requests quota. |
| * |
| * Note: in the case that the slice exceeds the runtime remaining (either due |
| * to consumption or the quota being specified to be smaller than the slice) |
| * we will always only issue the remaining available time. |
| * |
| * (default: 5 msec, units: microseconds) |
| */ |
| unsigned int sysctl_sched_cfs_bandwidth_slice = 5000UL; |
| #endif |
| |
| static inline void update_load_add(struct load_weight *lw, unsigned long inc) |
| { |
| lw->weight += inc; |
| lw->inv_weight = 0; |
| } |
| |
| static inline void update_load_sub(struct load_weight *lw, unsigned long dec) |
| { |
| lw->weight -= dec; |
| lw->inv_weight = 0; |
| } |
| |
| static inline void update_load_set(struct load_weight *lw, unsigned long w) |
| { |
| lw->weight = w; |
| lw->inv_weight = 0; |
| } |
| |
| /* |
| * Increase the granularity value when there are more CPUs, |
| * because with more CPUs the 'effective latency' as visible |
| * to users decreases. But the relationship is not linear, |
| * so pick a second-best guess by going with the log2 of the |
| * number of CPUs. |
| * |
| * This idea comes from the SD scheduler of Con Kolivas: |
| */ |
| static unsigned int get_update_sysctl_factor(void) |
| { |
| unsigned int cpus = min_t(unsigned int, num_online_cpus(), 8); |
| unsigned int factor; |
| |
| switch (sysctl_sched_tunable_scaling) { |
| case SCHED_TUNABLESCALING_NONE: |
| factor = 1; |
| break; |
| case SCHED_TUNABLESCALING_LINEAR: |
| factor = cpus; |
| break; |
| case SCHED_TUNABLESCALING_LOG: |
| default: |
| factor = 1 + ilog2(cpus); |
| break; |
| } |
| |
| return factor; |
| } |
| |
| static void update_sysctl(void) |
| { |
| unsigned int factor = get_update_sysctl_factor(); |
| |
| #define SET_SYSCTL(name) \ |
| (sysctl_##name = (factor) * normalized_sysctl_##name) |
| SET_SYSCTL(sched_min_granularity); |
| SET_SYSCTL(sched_latency); |
| SET_SYSCTL(sched_wakeup_granularity); |
| #undef SET_SYSCTL |
| } |
| |
| void sched_init_granularity(void) |
| { |
| update_sysctl(); |
| } |
| |
| #define WMULT_CONST (~0U) |
| #define WMULT_SHIFT 32 |
| |
| static void __update_inv_weight(struct load_weight *lw) |
| { |
| unsigned long w; |
| |
| if (likely(lw->inv_weight)) |
| return; |
| |
| w = scale_load_down(lw->weight); |
| |
| if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST)) |
| lw->inv_weight = 1; |
| else if (unlikely(!w)) |
| lw->inv_weight = WMULT_CONST; |
| else |
| lw->inv_weight = WMULT_CONST / w; |
| } |
| |
| /* |
| * delta_exec * weight / lw.weight |
| * OR |
| * (delta_exec * (weight * lw->inv_weight)) >> WMULT_SHIFT |
| * |
| * Either weight := NICE_0_LOAD and lw \e sched_prio_to_wmult[], in which case |
| * we're guaranteed shift stays positive because inv_weight is guaranteed to |
| * fit 32 bits, and NICE_0_LOAD gives another 10 bits; therefore shift >= 22. |
| * |
| * Or, weight =< lw.weight (because lw.weight is the runqueue weight), thus |
| * weight/lw.weight <= 1, and therefore our shift will also be positive. |
| */ |
| static u64 __calc_delta(u64 delta_exec, unsigned long weight, struct load_weight *lw) |
| { |
| u64 fact = scale_load_down(weight); |
| int shift = WMULT_SHIFT; |
| |
| __update_inv_weight(lw); |
| |
| if (unlikely(fact >> 32)) { |
| while (fact >> 32) { |
| fact >>= 1; |
| shift--; |
| } |
| } |
| |
| /* hint to use a 32x32->64 mul */ |
| fact = (u64)(u32)fact * lw->inv_weight; |
| |
| while (fact >> 32) { |
| fact >>= 1; |
| shift--; |
| } |
| |
| return mul_u64_u32_shr(delta_exec, fact, shift); |
| } |
| |
| |
| const struct sched_class fair_sched_class; |
| |
| /************************************************************** |
| * CFS operations on generic schedulable entities: |
| */ |
| |
| #ifdef CONFIG_FAIR_GROUP_SCHED |
| static inline struct task_struct *task_of(struct sched_entity *se) |
| { |
| SCHED_WARN_ON(!entity_is_task(se)); |
| return container_of(se, struct task_struct, se); |
| } |
| |
| /* Walk up scheduling entities hierarchy */ |
| #define for_each_sched_entity(se) \ |
| for (; se; se = se->parent) |
| |
| static inline struct cfs_rq *task_cfs_rq(struct task_struct *p) |
| { |
| return p->se.cfs_rq; |
| } |
| |
| /* runqueue on which this entity is (to be) queued */ |
| static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se) |
| { |
| return se->cfs_rq; |
| } |
| |
| /* runqueue "owned" by this group */ |
| static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp) |
| { |
| return grp->my_q; |
| } |
| |
| static inline bool list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq) |
| { |
| struct rq *rq = rq_of(cfs_rq); |
| int cpu = cpu_of(rq); |
| |
| if (cfs_rq->on_list) |
| return rq->tmp_alone_branch == &rq->leaf_cfs_rq_list; |
| |
| cfs_rq->on_list = 1; |
| |
| /* |
| * Ensure we either appear before our parent (if already |
| * enqueued) or force our parent to appear after us when it is |
| * enqueued. The fact that we always enqueue bottom-up |
| * reduces this to two cases and a special case for the root |
| * cfs_rq. Furthermore, it also means that we will always reset |
| * tmp_alone_branch either when the branch is connected |
| * to a tree or when we reach the top of the tree |
| */ |
| if (cfs_rq->tg->parent && |
| cfs_rq->tg->parent->cfs_rq[cpu]->on_list) { |
| /* |
| * If parent is already on the list, we add the child |
| * just before. Thanks to circular linked property of |
| * the list, this means to put the child at the tail |
| * of the list that starts by parent. |
| */ |
| list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list, |
| &(cfs_rq->tg->parent->cfs_rq[cpu]->leaf_cfs_rq_list)); |
| /* |
| * The branch is now connected to its tree so we can |
| * reset tmp_alone_branch to the beginning of the |
| * list. |
| */ |
| rq->tmp_alone_branch = &rq->leaf_cfs_rq_list; |
| return true; |
| } |
| |
| if (!cfs_rq->tg->parent) { |
| /* |
| * cfs rq without parent should be put |
| * at the tail of the list. |
| */ |
| list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list, |
| &rq->leaf_cfs_rq_list); |
| /* |
| * We have reach the top of a tree so we can reset |
| * tmp_alone_branch to the beginning of the list. |
| */ |
| rq->tmp_alone_branch = &rq->leaf_cfs_rq_list; |
| return true; |
| } |
| |
| /* |
| * The parent has not already been added so we want to |
| * make sure that it will be put after us. |
| * tmp_alone_branch points to the begin of the branch |
| * where we will add parent. |
| */ |
| list_add_rcu(&cfs_rq->leaf_cfs_rq_list, rq->tmp_alone_branch); |
| /* |
| * update tmp_alone_branch to points to the new begin |
| * of the branch |
| */ |
| rq->tmp_alone_branch = &cfs_rq->leaf_cfs_rq_list; |
| return false; |
| } |
| |
| static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq) |
| { |
| if (cfs_rq->on_list) { |
| struct rq *rq = rq_of(cfs_rq); |
| |
| /* |
| * With cfs_rq being unthrottled/throttled during an enqueue, |
| * it can happen the tmp_alone_branch points the a leaf that |
| * we finally want to del. In this case, tmp_alone_branch moves |
| * to the prev element but it will point to rq->leaf_cfs_rq_list |
| * at the end of the enqueue. |
| */ |
| if (rq->tmp_alone_branch == &cfs_rq->leaf_cfs_rq_list) |
| rq->tmp_alone_branch = cfs_rq->leaf_cfs_rq_list.prev; |
| |
| list_del_rcu(&cfs_rq->leaf_cfs_rq_list); |
| cfs_rq->on_list = 0; |
| } |
| } |
| |
| static inline void assert_list_leaf_cfs_rq(struct rq *rq) |
| { |
| SCHED_WARN_ON(rq->tmp_alone_branch != &rq->leaf_cfs_rq_list); |
| } |
| |
| /* Iterate thr' all leaf cfs_rq's on a runqueue */ |
| #define for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) \ |
| list_for_each_entry_safe(cfs_rq, pos, &rq->leaf_cfs_rq_list, \ |
| leaf_cfs_rq_list) |
| |
| /* Do the two (enqueued) entities belong to the same group ? */ |
| static inline struct cfs_rq * |
| is_same_group(struct sched_entity *se, struct sched_entity *pse) |
| { |
| if (se->cfs_rq == pse->cfs_rq) |
| return se->cfs_rq; |
| |
| return NULL; |
| } |
| |
| static inline struct sched_entity *parent_entity(struct sched_entity *se) |
| { |
| return se->parent; |
| } |
| |
| static void |
| find_matching_se(struct sched_entity **se, struct sched_entity **pse) |
| { |
| int se_depth, pse_depth; |
| |
| /* |
| * preemption test can be made between sibling entities who are in the |
| * same cfs_rq i.e who have a common parent. Walk up the hierarchy of |
| * both tasks until we find their ancestors who are siblings of common |
| * parent. |
| */ |
| |
| /* First walk up until both entities are at same depth */ |
| se_depth = (*se)->depth; |
| pse_depth = (*pse)->depth; |
| |
| while (se_depth > pse_depth) { |
| se_depth--; |
| *se = parent_entity(*se); |
| } |
| |
| while (pse_depth > se_depth) { |
| pse_depth--; |
| *pse = parent_entity(*pse); |
| } |
| |
| while (!is_same_group(*se, *pse)) { |
| *se = parent_entity(*se); |
| *pse = parent_entity(*pse); |
| } |
| } |
| |
| #else /* !CONFIG_FAIR_GROUP_SCHED */ |
| |
| static inline struct task_struct *task_of(struct sched_entity *se) |
| { |
| return container_of(se, struct task_struct, se); |
| } |
| |
| #define for_each_sched_entity(se) \ |
| for (; se; se = NULL) |
| |
| static inline struct cfs_rq *task_cfs_rq(struct task_struct *p) |
| { |
| return &task_rq(p)->cfs; |
| } |
| |
| static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se) |
| { |
| struct task_struct *p = task_of(se); |
| struct rq *rq = task_rq(p); |
| |
| return &rq->cfs; |
| } |
| |
| /* runqueue "owned" by this group */ |
| static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp) |
| { |
| return NULL; |
| } |
| |
| static inline bool list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq) |
| { |
| return true; |
| } |
| |
| static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq) |
| { |
| } |
| |
| static inline void assert_list_leaf_cfs_rq(struct rq *rq) |
| { |
| } |
| |
| #define for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) \ |
| for (cfs_rq = &rq->cfs, pos = NULL; cfs_rq; cfs_rq = pos) |
| |
| static inline struct sched_entity *parent_entity(struct sched_entity *se) |
| { |
| return NULL; |
| } |
| |
| static inline void |
| find_matching_se(struct sched_entity **se, struct sched_entity **pse) |
| { |
| } |
| |
| #endif /* CONFIG_FAIR_GROUP_SCHED */ |
| |
| static __always_inline |
| void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec); |
| |
| /************************************************************** |
| * Scheduling class tree data structure manipulation methods: |
| */ |
| |
| static inline u64 max_vruntime(u64 max_vruntime, u64 vruntime) |
| { |
| s64 delta = (s64)(vruntime - max_vruntime); |
| if (delta > 0) |
| max_vruntime = vruntime; |
| |
| return max_vruntime; |
| } |
| |
| static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime) |
| { |
| s64 delta = (s64)(vruntime - min_vruntime); |
| if (delta < 0) |
| min_vruntime = vruntime; |
| |
| return min_vruntime; |
| } |
| |
| static inline int entity_before(struct sched_entity *a, |
| struct sched_entity *b) |
| { |
| return (s64)(a->vruntime - b->vruntime) < 0; |
| } |
| |
| static void update_min_vruntime(struct cfs_rq *cfs_rq) |
| { |
| struct sched_entity *curr = cfs_rq->curr; |
| struct rb_node *leftmost = rb_first_cached(&cfs_rq->tasks_timeline); |
| |
| u64 vruntime = cfs_rq->min_vruntime; |
| |
| if (curr) { |
| if (curr->on_rq) |
| vruntime = curr->vruntime; |
| else |
| curr = NULL; |
| } |
| |
| if (leftmost) { /* non-empty tree */ |
| struct sched_entity *se; |
| se = rb_entry(leftmost, struct sched_entity, run_node); |
| |
| if (!curr) |
| vruntime = se->vruntime; |
| else |
| vruntime = min_vruntime(vruntime, se->vruntime); |
| } |
| |
| /* ensure we never gain time by being placed backwards. */ |
| cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime); |
| #ifndef CONFIG_64BIT |
| smp_wmb(); |
| cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime; |
| #endif |
| } |
| |
| /* |
| * Enqueue an entity into the rb-tree: |
| */ |
| static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| { |
| struct rb_node **link = &cfs_rq->tasks_timeline.rb_root.rb_node; |
| struct rb_node *parent = NULL; |
| struct sched_entity *entry; |
| bool leftmost = true; |
| |
| /* |
| * Find the right place in the rbtree: |
| */ |
| while (*link) { |
| parent = *link; |
| entry = rb_entry(parent, struct sched_entity, run_node); |
| /* |
| * We dont care about collisions. Nodes with |
| * the same key stay together. |
| */ |
| if (entity_before(se, entry)) { |
| link = &parent->rb_left; |
| } else { |
| link = &parent->rb_right; |
| leftmost = false; |
| } |
| } |
| |
| rb_link_node(&se->run_node, parent, link); |
| rb_insert_color_cached(&se->run_node, |
| &cfs_rq->tasks_timeline, leftmost); |
| } |
| |
| static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| { |
| rb_erase_cached(&se->run_node, &cfs_rq->tasks_timeline); |
| } |
| |
| struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq) |
| { |
| struct rb_node *left = rb_first_cached(&cfs_rq->tasks_timeline); |
| |
| if (!left) |
| return NULL; |
| |
| return rb_entry(left, struct sched_entity, run_node); |
| } |
| |
| static struct sched_entity *__pick_next_entity(struct sched_entity *se) |
| { |
| struct rb_node *next = rb_next(&se->run_node); |
| |
| if (!next) |
| return NULL; |
| |
| return rb_entry(next, struct sched_entity, run_node); |
| } |
| |
| #ifdef CONFIG_SCHED_DEBUG |
| struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq) |
| { |
| struct rb_node *last = rb_last(&cfs_rq->tasks_timeline.rb_root); |
| |
| if (!last) |
| return NULL; |
| |
| return rb_entry(last, struct sched_entity, run_node); |
| } |
| |
| /************************************************************** |
| * Scheduling class statistics methods: |
| */ |
| |
| int sched_proc_update_handler(struct ctl_table *table, int write, |
| void __user *buffer, size_t *lenp, |
| loff_t *ppos) |
| { |
| int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
| unsigned int factor = get_update_sysctl_factor(); |
| |
| if (ret || !write) |
| return ret; |
| |
| sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency, |
| sysctl_sched_min_granularity); |
| |
| #define WRT_SYSCTL(name) \ |
| (normalized_sysctl_##name = sysctl_##name / (factor)) |
| WRT_SYSCTL(sched_min_granularity); |
| WRT_SYSCTL(sched_latency); |
| WRT_SYSCTL(sched_wakeup_granularity); |
| #undef WRT_SYSCTL |
| |
| return 0; |
| } |
| #endif |
| |
| /* |
| * delta /= w |
| */ |
| static inline u64 calc_delta_fair(u64 delta, struct sched_entity *se) |
| { |
| if (unlikely(se->load.weight != NICE_0_LOAD)) |
| delta = __calc_delta(delta, NICE_0_LOAD, &se->load); |
| |
| return delta; |
| } |
| |
| /* |
| * The idea is to set a period in which each task runs once. |
| * |
| * When there are too many tasks (sched_nr_latency) we have to stretch |
| * this period because otherwise the slices get too small. |
| * |
| * p = (nr <= nl) ? l : l*nr/nl |
| */ |
| static u64 __sched_period(unsigned long nr_running) |
| { |
| if (unlikely(nr_running > sched_nr_latency)) |
| return nr_running * sysctl_sched_min_granularity; |
| else |
| return sysctl_sched_latency; |
| } |
| |
| /* |
| * We calculate the wall-time slice from the period by taking a part |
| * proportional to the weight. |
| * |
| * s = p*P[w/rw] |
| */ |
| static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| { |
| u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq); |
| |
| for_each_sched_entity(se) { |
| struct load_weight *load; |
| struct load_weight lw; |
| |
| cfs_rq = cfs_rq_of(se); |
| load = &cfs_rq->load; |
| |
| if (unlikely(!se->on_rq)) { |
| lw = cfs_rq->load; |
| |
| update_load_add(&lw, se->load.weight); |
| load = &lw; |
| } |
| slice = __calc_delta(slice, se->load.weight, load); |
| } |
| return slice; |
| } |
| |
| /* |
| * We calculate the vruntime slice of a to-be-inserted task. |
| * |
| * vs = s/w |
| */ |
| static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| { |
| return calc_delta_fair(sched_slice(cfs_rq, se), se); |
| } |
| |
| #include "pelt.h" |
| #ifdef CONFIG_SMP |
| |
| static int select_idle_sibling(struct task_struct *p, int prev_cpu, int cpu); |
| static unsigned long task_h_load(struct task_struct *p); |
| static unsigned long capacity_of(int cpu); |
| |
| /* Give new sched_entity start runnable values to heavy its load in infant time */ |
| void init_entity_runnable_average(struct sched_entity *se) |
| { |
| struct sched_avg *sa = &se->avg; |
| |
| memset(sa, 0, sizeof(*sa)); |
| |
| /* |
| * Tasks are initialized with full load to be seen as heavy tasks until |
| * they get a chance to stabilize to their real load level. |
| * Group entities are initialized with zero load to reflect the fact that |
| * nothing has been attached to the task group yet. |
| */ |
| if (entity_is_task(se)) |
| sa->runnable_load_avg = sa->load_avg = scale_load_down(se->load.weight); |
| |
| se->runnable_weight = se->load.weight; |
| |
| /* when this task enqueue'ed, it will contribute to its cfs_rq's load_avg */ |
| } |
| |
| static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq); |
| static void attach_entity_cfs_rq(struct sched_entity *se); |
| |
| /* |
| * With new tasks being created, their initial util_avgs are extrapolated |
| * based on the cfs_rq's current util_avg: |
| * |
| * util_avg = cfs_rq->util_avg / (cfs_rq->load_avg + 1) * se.load.weight |
| * |
| * However, in many cases, the above util_avg does not give a desired |
| * value. Moreover, the sum of the util_avgs may be divergent, such |
| * as when the series is a harmonic series. |
| * |
| * To solve this problem, we also cap the util_avg of successive tasks to |
| * only 1/2 of the left utilization budget: |
| * |
| * util_avg_cap = (cpu_scale - cfs_rq->avg.util_avg) / 2^n |
| * |
| * where n denotes the nth task and cpu_scale the CPU capacity. |
| * |
| * For example, for a CPU with 1024 of capacity, a simplest series from |
| * the beginning would be like: |
| * |
| * task util_avg: 512, 256, 128, 64, 32, 16, 8, ... |
| * cfs_rq util_avg: 512, 768, 896, 960, 992, 1008, 1016, ... |
| * |
| * Finally, that extrapolated util_avg is clamped to the cap (util_avg_cap) |
| * if util_avg > util_avg_cap. |
| */ |
| void post_init_entity_util_avg(struct task_struct *p) |
| { |
| struct sched_entity *se = &p->se; |
| struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| struct sched_avg *sa = &se->avg; |
| long cpu_scale = arch_scale_cpu_capacity(NULL, cpu_of(rq_of(cfs_rq))); |
| long cap = (long)(cpu_scale - cfs_rq->avg.util_avg) / 2; |
| |
| if (cap > 0) { |
| if (cfs_rq->avg.util_avg != 0) { |
| sa->util_avg = cfs_rq->avg.util_avg * se->load.weight; |
| sa->util_avg /= (cfs_rq->avg.load_avg + 1); |
| |
| if (sa->util_avg > cap) |
| sa->util_avg = cap; |
| } else { |
| sa->util_avg = cap; |
| } |
| } |
| |
| if (p->sched_class != &fair_sched_class) { |
| /* |
| * For !fair tasks do: |
| * |
| update_cfs_rq_load_avg(now, cfs_rq); |
| attach_entity_load_avg(cfs_rq, se, 0); |
| switched_from_fair(rq, p); |
| * |
| * such that the next switched_to_fair() has the |
| * expected state. |
| */ |
| se->avg.last_update_time = cfs_rq_clock_pelt(cfs_rq); |
| return; |
| } |
| |
| attach_entity_cfs_rq(se); |
| } |
| |
| #else /* !CONFIG_SMP */ |
| void init_entity_runnable_average(struct sched_entity *se) |
| { |
| } |
| void post_init_entity_util_avg(struct task_struct *p) |
| { |
| } |
| static void update_tg_load_avg(struct cfs_rq *cfs_rq, int force) |
| { |
| } |
| #endif /* CONFIG_SMP */ |
| |
| /* |
| * Update the current task's runtime statistics. |
| */ |
| static void update_curr(struct cfs_rq *cfs_rq) |
| { |
| struct sched_entity *curr = cfs_rq->curr; |
| u64 now = rq_clock_task(rq_of(cfs_rq)); |
| u64 delta_exec; |
| |
| if (unlikely(!curr)) |
| return; |
| |
| delta_exec = now - curr->exec_start; |
| if (unlikely((s64)delta_exec <= 0)) |
| return; |
| |
| curr->exec_start = now; |
| |
| schedstat_set(curr->statistics.exec_max, |
| max(delta_exec, curr->statistics.exec_max)); |
| |
| curr->sum_exec_runtime += delta_exec; |
| schedstat_add(cfs_rq->exec_clock, delta_exec); |
| |
| curr->vruntime += calc_delta_fair(delta_exec, curr); |
| update_min_vruntime(cfs_rq); |
| |
| if (entity_is_task(curr)) { |
| struct task_struct *curtask = task_of(curr); |
| |
| trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime); |
| cgroup_account_cputime(curtask, delta_exec); |
| account_group_exec_runtime(curtask, delta_exec); |
| } |
| |
| account_cfs_rq_runtime(cfs_rq, delta_exec); |
| } |
| |
| static void update_curr_fair(struct rq *rq) |
| { |
| update_curr(cfs_rq_of(&rq->curr->se)); |
| } |
| |
| static inline void |
| update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| { |
| u64 wait_start, prev_wait_start; |
| |
| if (!schedstat_enabled()) |
| return; |
| |
| wait_start = rq_clock(rq_of(cfs_rq)); |
| prev_wait_start = schedstat_val(se->statistics.wait_start); |
| |
| if (entity_is_task(se) && task_on_rq_migrating(task_of(se)) && |
| likely(wait_start > prev_wait_start)) |
| wait_start -= prev_wait_start; |
| |
| __schedstat_set(se->statistics.wait_start, wait_start); |
| } |
| |
| static inline void |
| update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| { |
| struct task_struct *p; |
| u64 delta; |
| |
| if (!schedstat_enabled()) |
| return; |
| |
| delta = rq_clock(rq_of(cfs_rq)) - schedstat_val(se->statistics.wait_start); |
| |
| if (entity_is_task(se)) { |
| p = task_of(se); |
| if (task_on_rq_migrating(p)) { |
| /* |
| * Preserve migrating task's wait time so wait_start |
| * time stamp can be adjusted to accumulate wait time |
| * prior to migration. |
| */ |
| __schedstat_set(se->statistics.wait_start, delta); |
| return; |
| } |
| trace_sched_stat_wait(p, delta); |
| } |
| |
| __schedstat_set(se->statistics.wait_max, |
| max(schedstat_val(se->statistics.wait_max), delta)); |
| __schedstat_inc(se->statistics.wait_count); |
| __schedstat_add(se->statistics.wait_sum, delta); |
| __schedstat_set(se->statistics.wait_start, 0); |
| } |
| |
| static inline void |
| update_stats_enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| { |
| struct task_struct *tsk = NULL; |
| u64 sleep_start, block_start; |
| |
| if (!schedstat_enabled()) |
| return; |
| |
| sleep_start = schedstat_val(se->statistics.sleep_start); |
| block_start = schedstat_val(se->statistics.block_start); |
| |
| if (entity_is_task(se)) |
| tsk = task_of(se); |
| |
| if (sleep_start) { |
| u64 delta = rq_clock(rq_of(cfs_rq)) - sleep_start; |
| |
| if ((s64)delta < 0) |
| delta = 0; |
| |
| if (unlikely(delta > schedstat_val(se->statistics.sleep_max))) |
| __schedstat_set(se->statistics.sleep_max, delta); |
| |
| __schedstat_set(se->statistics.sleep_start, 0); |
| __schedstat_add(se->statistics.sum_sleep_runtime, delta); |
| |
| if (tsk) { |
| account_scheduler_latency(tsk, delta >> 10, 1); |
| trace_sched_stat_sleep(tsk, delta); |
| } |
| } |
| if (block_start) { |
| u64 delta = rq_clock(rq_of(cfs_rq)) - block_start; |
| |
| if ((s64)delta < 0) |
| delta = 0; |
| |
| if (unlikely(delta > schedstat_val(se->statistics.block_max))) |
| __schedstat_set(se->statistics.block_max, delta); |
| |
| __schedstat_set(se->statistics.block_start, 0); |
| __schedstat_add(se->statistics.sum_sleep_runtime, delta); |
| |
| if (tsk) { |
| if (tsk->in_iowait) { |
| __schedstat_add(se->statistics.iowait_sum, delta); |
| __schedstat_inc(se->statistics.iowait_count); |
| trace_sched_stat_iowait(tsk, delta); |
| } |
| |
| trace_sched_stat_blocked(tsk, delta); |
| |
| /* |
| * Blocking time is in units of nanosecs, so shift by |
| * 20 to get a milliseconds-range estimation of the |
| * amount of time that the task spent sleeping: |
| */ |
| if (unlikely(prof_on == SLEEP_PROFILING)) { |
| profile_hits(SLEEP_PROFILING, |
| (void *)get_wchan(tsk), |
| delta >> 20); |
| } |
| account_scheduler_latency(tsk, delta >> 10, 0); |
| } |
| } |
| } |
| |
| /* |
| * Task is being enqueued - update stats: |
| */ |
| static inline void |
| update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
| { |
| if (!schedstat_enabled()) |
| return; |
| |
| /* |
| * Are we enqueueing a waiting task? (for current tasks |
| * a dequeue/enqueue event is a NOP) |
| */ |
| if (se != cfs_rq->curr) |
| update_stats_wait_start(cfs_rq, se); |
| |
| if (flags & ENQUEUE_WAKEUP) |
| update_stats_enqueue_sleeper(cfs_rq, se); |
| } |
| |
| static inline void |
| update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
| { |
| |
| if (!schedstat_enabled()) |
| return; |
| |
| /* |
| * Mark the end of the wait period if dequeueing a |
| * waiting task: |
| */ |
| if (se != cfs_rq->curr) |
| update_stats_wait_end(cfs_rq, se); |
| |
| if ((flags & DEQUEUE_SLEEP) && entity_is_task(se)) { |
| struct task_struct *tsk = task_of(se); |
| |
| if (tsk->state & TASK_INTERRUPTIBLE) |
| __schedstat_set(se->statistics.sleep_start, |
| rq_clock(rq_of(cfs_rq))); |
| if (tsk->state & TASK_UNINTERRUPTIBLE) |
| __schedstat_set(se->statistics.block_start, |
| rq_clock(rq_of(cfs_rq))); |
| } |
| } |
| |
| /* |
| * We are picking a new current task - update its stats: |
| */ |
| static inline void |
| update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| { |
| /* |
| * We are starting a new run period: |
| */ |
| se->exec_start = rq_clock_task(rq_of(cfs_rq)); |
| } |
| |
| /************************************************** |
| * Scheduling class queueing methods: |
| */ |
| |
| #ifdef CONFIG_NUMA_BALANCING |
| /* |
| * Approximate time to scan a full NUMA task in ms. The task scan period is |
| * calculated based on the tasks virtual memory size and |
| * numa_balancing_scan_size. |
| */ |
| unsigned int sysctl_numa_balancing_scan_period_min = 1000; |
| unsigned int sysctl_numa_balancing_scan_period_max = 60000; |
| |
| /* Portion of address space to scan in MB */ |
| unsigned int sysctl_numa_balancing_scan_size = 256; |
| |
| /* Scan @scan_size MB every @scan_period after an initial @scan_delay in ms */ |
| unsigned int sysctl_numa_balancing_scan_delay = 1000; |
| |
| struct numa_group { |
| refcount_t refcount; |
| |
| spinlock_t lock; /* nr_tasks, tasks */ |
| int nr_tasks; |
| pid_t gid; |
| int active_nodes; |
| |
| struct rcu_head rcu; |
| unsigned long total_faults; |
| unsigned long max_faults_cpu; |
| /* |
| * Faults_cpu is used to decide whether memory should move |
| * towards the CPU. As a consequence, these stats are weighted |
| * more by CPU use than by memory faults. |
| */ |
| unsigned long *faults_cpu; |
| unsigned long faults[0]; |
| }; |
| |
| static inline unsigned long group_faults_priv(struct numa_group *ng); |
| static inline unsigned long group_faults_shared(struct numa_group *ng); |
| |
| static unsigned int task_nr_scan_windows(struct task_struct *p) |
| { |
| unsigned long rss = 0; |
| unsigned long nr_scan_pages; |
| |
| /* |
| * Calculations based on RSS as non-present and empty pages are skipped |
| * by the PTE scanner and NUMA hinting faults should be trapped based |
| * on resident pages |
| */ |
| nr_scan_pages = sysctl_numa_balancing_scan_size << (20 - PAGE_SHIFT); |
| rss = get_mm_rss(p->mm); |
| if (!rss) |
| rss = nr_scan_pages; |
| |
| rss = round_up(rss, nr_scan_pages); |
| return rss / nr_scan_pages; |
| } |
| |
| /* For sanitys sake, never scan more PTEs than MAX_SCAN_WINDOW MB/sec. */ |
| #define MAX_SCAN_WINDOW 2560 |
| |
| static unsigned int task_scan_min(struct task_struct *p) |
| { |
| unsigned int scan_size = READ_ONCE(sysctl_numa_balancing_scan_size); |
| unsigned int scan, floor; |
| unsigned int windows = 1; |
| |
| if (scan_size < MAX_SCAN_WINDOW) |
| windows = MAX_SCAN_WINDOW / scan_size; |
| floor = 1000 / windows; |
| |
| scan = sysctl_numa_balancing_scan_period_min / task_nr_scan_windows(p); |
| return max_t(unsigned int, floor, scan); |
| } |
| |
| static unsigned int task_scan_start(struct task_struct *p) |
| { |
| unsigned long smin = task_scan_min(p); |
| unsigned long period = smin; |
| |
| /* Scale the maximum scan period with the amount of shared memory. */ |
| if (p->numa_group) { |
| struct numa_group *ng = p->numa_group; |
| unsigned long shared = group_faults_shared(ng); |
| unsigned long private = group_faults_priv(ng); |
| |
| period *= refcount_read(&ng->refcount); |
| period *= shared + 1; |
| period /= private + shared + 1; |
| } |
| |
| return max(smin, period); |
| } |
| |
| static unsigned int task_scan_max(struct task_struct *p) |
| { |
| unsigned long smin = task_scan_min(p); |
| unsigned long smax; |
| |
| /* Watch for min being lower than max due to floor calculations */ |
| smax = sysctl_numa_balancing_scan_period_max / task_nr_scan_windows(p); |
| |
| /* Scale the maximum scan period with the amount of shared memory. */ |
| if (p->numa_group) { |
| struct numa_group *ng = p->numa_group; |
| unsigned long shared = group_faults_shared(ng); |
| unsigned long private = group_faults_priv(ng); |
| unsigned long period = smax; |
| |
| period *= refcount_read(&ng->refcount); |
| period *= shared + 1; |
| period /= private + shared + 1; |
| |
| smax = max(smax, period); |
| } |
| |
| return max(smin, smax); |
| } |
| |
| void init_numa_balancing(unsigned long clone_flags, struct task_struct *p) |
| { |
| int mm_users = 0; |
| struct mm_struct *mm = p->mm; |
| |
| if (mm) { |
| mm_users = atomic_read(&mm->mm_users); |
| if (mm_users == 1) { |
| mm->numa_next_scan = jiffies + msecs_to_jiffies(sysctl_numa_balancing_scan_delay); |
| mm->numa_scan_seq = 0; |
| } |
| } |
| p->node_stamp = 0; |
| p->numa_scan_seq = mm ? mm->numa_scan_seq : 0; |
| p->numa_scan_period = sysctl_numa_balancing_scan_delay; |
| p->numa_work.next = &p->numa_work; |
| p->numa_faults = NULL; |
| p->numa_group = NULL; |
| p->last_task_numa_placement = 0; |
| p->last_sum_exec_runtime = 0; |
| |
| /* New address space, reset the preferred nid */ |
| if (!(clone_flags & CLONE_VM)) { |
| p->numa_preferred_nid = NUMA_NO_NODE; |
| return; |
| } |
| |
| /* |
| * New thread, keep existing numa_preferred_nid which should be copied |
| * already by arch_dup_task_struct but stagger when scans start. |
| */ |
| if (mm) { |
| unsigned int delay; |
| |
| delay = min_t(unsigned int, task_scan_max(current), |
| current->numa_scan_period * mm_users * NSEC_PER_MSEC); |
| delay += 2 * TICK_NSEC; |
| p->node_stamp = delay; |
| } |
| } |
| |
| static void account_numa_enqueue(struct rq *rq, struct task_struct *p) |
| { |
| rq->nr_numa_running += (p->numa_preferred_nid != NUMA_NO_NODE); |
| rq->nr_preferred_running += (p->numa_preferred_nid == task_node(p)); |
| } |
| |
| static void account_numa_dequeue(struct rq *rq, struct task_struct *p) |
| { |
| rq->nr_numa_running -= (p->numa_preferred_nid != NUMA_NO_NODE); |
| rq->nr_preferred_running -= (p->numa_preferred_nid == task_node(p)); |
| } |
| |
| /* Shared or private faults. */ |
| #define NR_NUMA_HINT_FAULT_TYPES 2 |
| |
| /* Memory and CPU locality */ |
| #define NR_NUMA_HINT_FAULT_STATS (NR_NUMA_HINT_FAULT_TYPES * 2) |
| |
| /* Averaged statistics, and temporary buffers. */ |
| #define NR_NUMA_HINT_FAULT_BUCKETS (NR_NUMA_HINT_FAULT_STATS * 2) |
| |
| pid_t task_numa_group_id(struct task_struct *p) |
| { |
| return p->numa_group ? p->numa_group->gid : 0; |
| } |
| |
| /* |
| * The averaged statistics, shared & private, memory & CPU, |
| * occupy the first half of the array. The second half of the |
| * array is for current counters, which are averaged into the |
| * first set by task_numa_placement. |
| */ |
| static inline int task_faults_idx(enum numa_faults_stats s, int nid, int priv) |
| { |
| return NR_NUMA_HINT_FAULT_TYPES * (s * nr_node_ids + nid) + priv; |
| } |
| |
| static inline unsigned long task_faults(struct task_struct *p, int nid) |
| { |
| if (!p->numa_faults) |
| return 0; |
| |
| return p->numa_faults[task_faults_idx(NUMA_MEM, nid, 0)] + |
| p->numa_faults[task_faults_idx(NUMA_MEM, nid, 1)]; |
| } |
| |
| static inline unsigned long group_faults(struct task_struct *p, int nid) |
| { |
| if (!p->numa_group) |
| return 0; |
| |
| return p->numa_group->faults[task_faults_idx(NUMA_MEM, nid, 0)] + |
| p->numa_group->faults[task_faults_idx(NUMA_MEM, nid, 1)]; |
| } |
| |
| static inline unsigned long group_faults_cpu(struct numa_group *group, int nid) |
| { |
| return group->faults_cpu[task_faults_idx(NUMA_MEM, nid, 0)] + |
| group->faults_cpu[task_faults_idx(NUMA_MEM, nid, 1)]; |
| } |
| |
| static inline unsigned long group_faults_priv(struct numa_group *ng) |
| { |
| unsigned long faults = 0; |
| int node; |
| |
| for_each_online_node(node) { |
| faults += ng->faults[task_faults_idx(NUMA_MEM, node, 1)]; |
| } |
| |
| return faults; |
| } |
| |
| static inline unsigned long group_faults_shared(struct numa_group *ng) |
| { |
| unsigned long faults = 0; |
| int node; |
| |
| for_each_online_node(node) { |
| faults += ng->faults[task_faults_idx(NUMA_MEM, node, 0)]; |
| } |
| |
| return faults; |
| } |
| |
| /* |
| * A node triggering more than 1/3 as many NUMA faults as the maximum is |
| * considered part of a numa group's pseudo-interleaving set. Migrations |
| * between these nodes are slowed down, to allow things to settle down. |
| */ |
| #define ACTIVE_NODE_FRACTION 3 |
| |
| static bool numa_is_active_node(int nid, struct numa_group *ng) |
| { |
| return group_faults_cpu(ng, nid) * ACTIVE_NODE_FRACTION > ng->max_faults_cpu; |
| } |
| |
| /* Handle placement on systems where not all nodes are directly connected. */ |
| static unsigned long score_nearby_nodes(struct task_struct *p, int nid, |
| int maxdist, bool task) |
| { |
| unsigned long score = 0; |
| int node; |
| |
| /* |
| * All nodes are directly connected, and the same distance |
| * from each other. No need for fancy placement algorithms. |
| */ |
| if (sched_numa_topology_type == NUMA_DIRECT) |
| return 0; |
| |
| /* |
| * This code is called for each node, introducing N^2 complexity, |
| * which should be ok given the number of nodes rarely exceeds 8. |
| */ |
| for_each_online_node(node) { |
| unsigned long faults; |
| int dist = node_distance(nid, node); |
| |
| /* |
| * The furthest away nodes in the system are not interesting |
| * for placement; nid was already counted. |
| */ |
| if (dist == sched_max_numa_distance || node == nid) |
| continue; |
| |
| /* |
| * On systems with a backplane NUMA topology, compare groups |
| * of nodes, and move tasks towards the group with the most |
| * memory accesses. When comparing two nodes at distance |
| * "hoplimit", only nodes closer by than "hoplimit" are part |
| * of each group. Skip other nodes. |
| */ |
| if (sched_numa_topology_type == NUMA_BACKPLANE && |
| dist >= maxdist) |
| continue; |
| |
| /* Add up the faults from nearby nodes. */ |
| if (task) |
| faults = task_faults(p, node); |
| else |
| faults = group_faults(p, node); |
| |
| /* |
| * On systems with a glueless mesh NUMA topology, there are |
| * no fixed "groups of nodes". Instead, nodes that are not |
| * directly connected bounce traffic through intermediate |
| * nodes; a numa_group can occupy any set of nodes. |
| * The further away a node is, the less the faults count. |
| * This seems to result in good task placement. |
| */ |
| if (sched_numa_topology_type == NUMA_GLUELESS_MESH) { |
| faults *= (sched_max_numa_distance - dist); |
| faults /= (sched_max_numa_distance - LOCAL_DISTANCE); |
| } |
| |
| score += faults; |
| } |
| |
| return score; |
| } |
| |
| /* |
| * These return the fraction of accesses done by a particular task, or |
| * task group, on a particular numa node. The group weight is given a |
| * larger multiplier, in order to group tasks together that are almost |
| * evenly spread out between numa nodes. |
| */ |
| static inline unsigned long task_weight(struct task_struct *p, int nid, |
| int dist) |
| { |
| unsigned long faults, total_faults; |
| |
| if (!p->numa_faults) |
| return 0; |
| |
| total_faults = p->total_numa_faults; |
| |
| if (!total_faults) |
| return 0; |
| |
| faults = task_faults(p, nid); |
| faults += score_nearby_nodes(p, nid, dist, true); |
| |
| return 1000 * faults / total_faults; |
| } |
| |
| static inline unsigned long group_weight(struct task_struct *p, int nid, |
| int dist) |
| { |
| unsigned long faults, total_faults; |
| |
| if (!p->numa_group) |
| return 0; |
| |
| total_faults = p->numa_group->total_faults; |
| |
| if (!total_faults) |
| return 0; |
| |
| faults = group_faults(p, nid); |
| faults += score_nearby_nodes(p, nid, dist, false); |
| |
| return 1000 * faults / total_faults; |
| } |
| |
| bool should_numa_migrate_memory(struct task_struct *p, struct page * page, |
| int src_nid, int dst_cpu) |
| { |
| struct numa_group *ng = p->numa_group; |
| int dst_nid = cpu_to_node(dst_cpu); |
| int last_cpupid, this_cpupid; |
| |
| this_cpupid = cpu_pid_to_cpupid(dst_cpu, current->pid); |
| last_cpupid = page_cpupid_xchg_last(page, this_cpupid); |
| |
| /* |
| * Allow first faults or private faults to migrate immediately early in |
| * the lifetime of a task. The magic number 4 is based on waiting for |
| * two full passes of the "multi-stage node selection" test that is |
| * executed below. |
| */ |
| if ((p->numa_preferred_nid == NUMA_NO_NODE || p->numa_scan_seq <= 4) && |
| (cpupid_pid_unset(last_cpupid) || cpupid_match_pid(p, last_cpupid))) |
| return true; |
| |
| /* |
| * Multi-stage node selection is used in conjunction with a periodic |
| * migration fault to build a temporal task<->page relation. By using |
| * a two-stage filter we remove short/unlikely relations. |
| * |
| * Using P(p) ~ n_p / n_t as per frequentist probability, we can equate |
| * a task's usage of a particular page (n_p) per total usage of this |
| * page (n_t) (in a given time-span) to a probability. |
| * |
| * Our periodic faults will sample this probability and getting the |
| * same result twice in a row, given these samples are fully |
| * independent, is then given by P(n)^2, provided our sample period |
| * is sufficiently short compared to the usage pattern. |
| * |
| * This quadric squishes small probabilities, making it less likely we |
| * act on an unlikely task<->page relation. |
| */ |
| if (!cpupid_pid_unset(last_cpupid) && |
| cpupid_to_nid(last_cpupid) != dst_nid) |
| return false; |
| |
| /* Always allow migrate on private faults */ |
| if (cpupid_match_pid(p, last_cpupid)) |
| return true; |
| |
| /* A shared fault, but p->numa_group has not been set up yet. */ |
| if (!ng) |
| return true; |
| |
| /* |
| * Destination node is much more heavily used than the source |
| * node? Allow migration. |
| */ |
| if (group_faults_cpu(ng, dst_nid) > group_faults_cpu(ng, src_nid) * |
| ACTIVE_NODE_FRACTION) |
| return true; |
| |
| /* |
| * Distribute memory according to CPU & memory use on each node, |
| * with 3/4 hysteresis to avoid unnecessary memory migrations: |
| * |
| * faults_cpu(dst) 3 faults_cpu(src) |
| * --------------- * - > --------------- |
| * faults_mem(dst) 4 faults_mem(src) |
| */ |
| return group_faults_cpu(ng, dst_nid) * group_faults(p, src_nid) * 3 > |
| group_faults_cpu(ng, src_nid) * group_faults(p, dst_nid) * 4; |
| } |
| |
| static unsigned long weighted_cpuload(struct rq *rq); |
| static unsigned long source_load(int cpu, int type); |
| static unsigned long target_load(int cpu, int type); |
| |
| /* Cached statistics for all CPUs within a node */ |
| struct numa_stats { |
| unsigned long load; |
| |
| /* Total compute capacity of CPUs on a node */ |
| unsigned long compute_capacity; |
| }; |
| |
| /* |
| * XXX borrowed from update_sg_lb_stats |
| */ |
| static void update_numa_stats(struct numa_stats *ns, int nid) |
| { |
| int cpu; |
| |
| memset(ns, 0, sizeof(*ns)); |
| for_each_cpu(cpu, cpumask_of_node(nid)) { |
| struct rq *rq = cpu_rq(cpu); |
| |
| ns->load += weighted_cpuload(rq); |
| ns->compute_capacity += capacity_of(cpu); |
| } |
| |
| } |
| |
| struct task_numa_env { |
| struct task_struct *p; |
| |
| int src_cpu, src_nid; |
| int dst_cpu, dst_nid; |
| |
| struct numa_stats src_stats, dst_stats; |
| |
| int imbalance_pct; |
| int dist; |
| |
| struct task_struct *best_task; |
| long best_imp; |
| int best_cpu; |
| }; |
| |
| static void task_numa_assign(struct task_numa_env *env, |
| struct task_struct *p, long imp) |
| { |
| struct rq *rq = cpu_rq(env->dst_cpu); |
| |
| /* Bail out if run-queue part of active NUMA balance. */ |
| if (xchg(&rq->numa_migrate_on, 1)) |
| return; |
| |
| /* |
| * Clear previous best_cpu/rq numa-migrate flag, since task now |
| * found a better CPU to move/swap. |
| */ |
| if (env->best_cpu != -1) { |
| rq = cpu_rq(env->best_cpu); |
| WRITE_ONCE(rq->numa_migrate_on, 0); |
| } |
| |
| if (env->best_task) |
| put_task_struct(env->best_task); |
| if (p) |
| get_task_struct(p); |
| |
| env->best_task = p; |
| env->best_imp = imp; |
| env->best_cpu = env->dst_cpu; |
| } |
| |
| static bool load_too_imbalanced(long src_load, long dst_load, |
| struct task_numa_env *env) |
| { |
| long imb, old_imb; |
| long orig_src_load, orig_dst_load; |
| long src_capacity, dst_capacity; |
| |
| /* |
| * The load is corrected for the CPU capacity available on each node. |
| * |
| * src_load dst_load |
| * ------------ vs --------- |
| * src_capacity dst_capacity |
| */ |
| src_capacity = env->src_stats.compute_capacity; |
| dst_capacity = env->dst_stats.compute_capacity; |
| |
| imb = abs(dst_load * src_capacity - src_load * dst_capacity); |
| |
| orig_src_load = env->src_stats.load; |
| orig_dst_load = env->dst_stats.load; |
| |
| old_imb = abs(orig_dst_load * src_capacity - orig_src_load * dst_capacity); |
| |
| /* Would this change make things worse? */ |
| return (imb > old_imb); |
| } |
| |
| /* |
| * Maximum NUMA importance can be 1998 (2*999); |
| * SMALLIMP @ 30 would be close to 1998/64. |
| * Used to deter task migration. |
| */ |
| #define SMALLIMP 30 |
| |
| /* |
| * This checks if the overall compute and NUMA accesses of the system would |
| * be improved if the source tasks was migrated to the target dst_cpu taking |
| * into account that it might be best if task running on the dst_cpu should |
| * be exchanged with the source task |
| */ |
| static void task_numa_compare(struct task_numa_env *env, |
| long taskimp, long groupimp, bool maymove) |
| { |
| struct rq *dst_rq = cpu_rq(env->dst_cpu); |
| struct task_struct *cur; |
| long src_load, dst_load; |
| long load; |
| long imp = env->p->numa_group ? groupimp : taskimp; |
| long moveimp = imp; |
| int dist = env->dist; |
| |
| if (READ_ONCE(dst_rq->numa_migrate_on)) |
| return; |
| |
| rcu_read_lock(); |
| cur = task_rcu_dereference(&dst_rq->curr); |
| if (cur && ((cur->flags & PF_EXITING) || is_idle_task(cur))) |
| cur = NULL; |
| |
| /* |
| * Because we have preemption enabled we can get migrated around and |
| * end try selecting ourselves (current == env->p) as a swap candidate. |
| */ |
| if (cur == env->p) |
| goto unlock; |
| |
| if (!cur) { |
| if (maymove && moveimp >= env->best_imp) |
| goto assign; |
| else |
| goto unlock; |
| } |
| |
| /* |
| * "imp" is the fault differential for the source task between the |
| * source and destination node. Calculate the total differential for |
| * the source task and potential destination task. The more negative |
| * the value is, the more remote accesses that would be expected to |
| * be incurred if the tasks were swapped. |
| */ |
| /* Skip this swap candidate if cannot move to the source cpu */ |
| if (!cpumask_test_cpu(env->src_cpu, &cur->cpus_allowed)) |
| goto unlock; |
| |
| /* |
| * If dst and source tasks are in the same NUMA group, or not |
| * in any group then look only at task weights. |
| */ |
| if (cur->numa_group == env->p->numa_group) { |
| imp = taskimp + task_weight(cur, env->src_nid, dist) - |
| task_weight(cur, env->dst_nid, dist); |
| /* |
| * Add some hysteresis to prevent swapping the |
| * tasks within a group over tiny differences. |
| */ |
| if (cur->numa_group) |
| imp -= imp / 16; |
| } else { |
| /* |
| * Compare the group weights. If a task is all by itself |
| * (not part of a group), use the task weight instead. |
| */ |
| if (cur->numa_group && env->p->numa_group) |
| imp += group_weight(cur, env->src_nid, dist) - |
| group_weight(cur, env->dst_nid, dist); |
| else |
| imp += task_weight(cur, env->src_nid, dist) - |
| task_weight(cur, env->dst_nid, dist); |
| } |
| |
| if (maymove && moveimp > imp && moveimp > env->best_imp) { |
| imp = moveimp; |
| cur = NULL; |
| goto assign; |
| } |
| |
| /* |
| * If the NUMA importance is less than SMALLIMP, |
| * task migration might only result in ping pong |
| * of tasks and also hurt performance due to cache |
| * misses. |
| */ |
| if (imp < SMALLIMP || imp <= env->best_imp + SMALLIMP / 2) |
| goto unlock; |
| |
| /* |
| * In the overloaded case, try and keep the load balanced. |
| */ |
| load = task_h_load(env->p) - task_h_load(cur); |
| if (!load) |
| goto assign; |
| |
| dst_load = env->dst_stats.load + load; |
| src_load = env->src_stats.load - load; |
| |
| if (load_too_imbalanced(src_load, dst_load, env)) |
| goto unlock; |
| |
| assign: |
| /* |
| * One idle CPU per node is evaluated for a task numa move. |
| * Call select_idle_sibling to maybe find a better one. |
| */ |
| if (!cur) { |
| /* |
| * select_idle_siblings() uses an per-CPU cpumask that |
| * can be used from IRQ context. |
| */ |
| local_irq_disable(); |
| env->dst_cpu = select_idle_sibling(env->p, env->src_cpu, |
| env->dst_cpu); |
| local_irq_enable(); |
| } |
| |
| task_numa_assign(env, cur, imp); |
| unlock: |
| rcu_read_unlock(); |
| } |
| |
| static void task_numa_find_cpu(struct task_numa_env *env, |
| long taskimp, long groupimp) |
| { |
| long src_load, dst_load, load; |
| bool maymove = false; |
| int cpu; |
| |
| load = task_h_load(env->p); |
| dst_load = env->dst_stats.load + load; |
| src_load = env->src_stats.load - load; |
| |
| /* |
| * If the improvement from just moving env->p direction is better |
| * than swapping tasks around, check if a move is possible. |
| */ |
| maymove = !load_too_imbalanced(src_load, dst_load, env); |
| |
| for_each_cpu(cpu, cpumask_of_node(env->dst_nid)) { |
| /* Skip this CPU if the source task cannot migrate */ |
| if (!cpumask_test_cpu(cpu, &env->p->cpus_allowed)) |
| continue; |
| |
| env->dst_cpu = cpu; |
| task_numa_compare(env, taskimp, groupimp, maymove); |
| } |
| } |
| |
| static int task_numa_migrate(struct task_struct *p) |
| { |
| struct task_numa_env env = { |
| .p = p, |
| |
| .src_cpu = task_cpu(p), |
| .src_nid = task_node(p), |
| |
| .imbalance_pct = 112, |
| |
| .best_task = NULL, |
| .best_imp = 0, |
| .best_cpu = -1, |
| }; |
| struct sched_domain *sd; |
| struct rq *best_rq; |
| unsigned long taskweight, groupweight; |
| int nid, ret, dist; |
| long taskimp, groupimp; |
| |
| /* |
| * Pick the lowest SD_NUMA domain, as that would have the smallest |
| * imbalance and would be the first to start moving tasks about. |
| * |
| * And we want to avoid any moving of tasks about, as that would create |
| * random movement of tasks -- counter the numa conditions we're trying |
| * to satisfy here. |
| */ |
| rcu_read_lock(); |
| sd = rcu_dereference(per_cpu(sd_numa, env.src_cpu)); |
| if (sd) |
| env.imbalance_pct = 100 + (sd->imbalance_pct - 100) / 2; |
| rcu_read_unlock(); |
| |
| /* |
| * Cpusets can break the scheduler domain tree into smaller |
| * balance domains, some of which do not cross NUMA boundaries. |
| * Tasks that are "trapped" in such domains cannot be migrated |
| * elsewhere, so there is no point in (re)trying. |
| */ |
| if (unlikely(!sd)) { |
| sched_setnuma(p, task_node(p)); |
| return -EINVAL; |
| } |
| |
| env.dst_nid = p->numa_preferred_nid; |
| dist = env.dist = node_distance(env.src_nid, env.dst_nid); |
| taskweight = task_weight(p, env.src_nid, dist); |
| groupweight = group_weight(p, env.src_nid, dist); |
| update_numa_stats(&env.src_stats, env.src_nid); |
| taskimp = task_weight(p, env.dst_nid, dist) - taskweight; |
| groupimp = group_weight(p, env.dst_nid, dist) - groupweight; |
| update_numa_stats(&env.dst_stats, env.dst_nid); |
| |
| /* Try to find a spot on the preferred nid. */ |
| task_numa_find_cpu(&env, taskimp, groupimp); |
| |
| /* |
| * Look at other nodes in these cases: |
| * - there is no space available on the preferred_nid |
| * - the task is part of a numa_group that is interleaved across |
| * multiple NUMA nodes; in order to better consolidate the group, |
| * we need to check other locations. |
| */ |
| if (env.best_cpu == -1 || (p->numa_group && p->numa_group->active_nodes > 1)) { |
| for_each_online_node(nid) { |
| if (nid == env.src_nid || nid == p->numa_preferred_nid) |
| continue; |
| |
| dist = node_distance(env.src_nid, env.dst_nid); |
| if (sched_numa_topology_type == NUMA_BACKPLANE && |
| dist != env.dist) { |
| taskweight = task_weight(p, env.src_nid, dist); |
| groupweight = group_weight(p, env.src_nid, dist); |
| } |
| |
| /* Only consider nodes where both task and groups benefit */ |
| taskimp = task_weight(p, nid, dist) - taskweight; |
| groupimp = group_weight(p, nid, dist) - groupweight; |
| if (taskimp < 0 && groupimp < 0) |
| continue; |
| |
| env.dist = dist; |
| env.dst_nid = nid; |
| update_numa_stats(&env.dst_stats, env.dst_nid); |
| task_numa_find_cpu(&env, taskimp, groupimp); |
| } |
| } |
| |
| /* |
| * If the task is part of a workload that spans multiple NUMA nodes, |
| * and is migrating into one of the workload's active nodes, remember |
| * this node as the task's preferred numa node, so the workload can |
| * settle down. |
| * A task that migrated to a second choice node will be better off |
| * trying for a better one later. Do not set the preferred node here. |
| */ |
| if (p->numa_group) { |
| if (env.best_cpu == -1) |
| nid = env.src_nid; |
| else |
| nid = cpu_to_node(env.best_cpu); |
| |
| if (nid != p->numa_preferred_nid) |
| sched_setnuma(p, nid); |
| } |
| |
| /* No better CPU than the current one was found. */ |
| if (env.best_cpu == -1) |
| return -EAGAIN; |
| |
| best_rq = cpu_rq(env.best_cpu); |
| if (env.best_task == NULL) { |
| ret = migrate_task_to(p, env.best_cpu); |
| WRITE_ONCE(best_rq->numa_migrate_on, 0); |
| if (ret != 0) |
| trace_sched_stick_numa(p, env.src_cpu, env.best_cpu); |
| return ret; |
| } |
| |
| ret = migrate_swap(p, env.best_task, env.best_cpu, env.src_cpu); |
| WRITE_ONCE(best_rq->numa_migrate_on, 0); |
| |
| if (ret != 0) |
| trace_sched_stick_numa(p, env.src_cpu, task_cpu(env.best_task)); |
| put_task_struct(env.best_task); |
| return ret; |
| } |
| |
| /* Attempt to migrate a task to a CPU on the preferred node. */ |
| static void numa_migrate_preferred(struct task_struct *p) |
| { |
| unsigned long interval = HZ; |
| |
| /* This task has no NUMA fault statistics yet */ |
| if (unlikely(p->numa_preferred_nid == NUMA_NO_NODE || !p->numa_faults)) |
| return; |
| |
| /* Periodically retry migrating the task to the preferred node */ |
| interval = min(interval, msecs_to_jiffies(p->numa_scan_period) / 16); |
| p->numa_migrate_retry = jiffies + interval; |
| |
| /* Success if task is already running on preferred CPU */ |
| if (task_node(p) == p->numa_preferred_nid) |
| return; |
| |
| /* Otherwise, try migrate to a CPU on the preferred node */ |
| task_numa_migrate(p); |
| } |
| |
| /* |
| * Find out how many nodes on the workload is actively running on. Do this by |
| * tracking the nodes from which NUMA hinting faults are triggered. This can |
| * be different from the set of nodes where the workload's memory is currently |
| * located. |
| */ |
| static void numa_group_count_active_nodes(struct numa_group *numa_group) |
| { |
| unsigned long faults, max_faults = 0; |
| int nid, active_nodes = 0; |
| |
| for_each_online_node(nid) { |
| faults = group_faults_cpu(numa_group, nid); |
| if (faults > max_faults) |
| max_faults = faults; |
| } |
| |
| for_each_online_node(nid) { |
| faults = group_faults_cpu(numa_group, nid); |
| if (faults * ACTIVE_NODE_FRACTION > max_faults) |
| active_nodes++; |
| } |
| |
| numa_group->max_faults_cpu = max_faults; |
| numa_group->active_nodes = active_nodes; |
| } |
| |
| /* |
| * When adapting the scan rate, the period is divided into NUMA_PERIOD_SLOTS |
| * increments. The more local the fault statistics are, the higher the scan |
| * period will be for the next scan window. If local/(local+remote) ratio is |
| * below NUMA_PERIOD_THRESHOLD (where range of ratio is 1..NUMA_PERIOD_SLOTS) |
| * the scan period will decrease. Aim for 70% local accesses. |
| */ |
| #define NUMA_PERIOD_SLOTS 10 |
| #define NUMA_PERIOD_THRESHOLD 7 |
| |
| /* |
| * Increase the scan period (slow down scanning) if the majority of |
| * our memory is already on our local node, or if the majority of |
| * the page accesses are shared with other processes. |
| * Otherwise, decrease the scan period. |
| */ |
| static void update_task_scan_period(struct task_struct *p, |
| unsigned long shared, unsigned long private) |
| { |
| unsigned int period_slot; |
| int lr_ratio, ps_ratio; |
| int diff; |
| |
| unsigned long remote = p->numa_faults_locality[0]; |
| unsigned long local = p->numa_faults_locality[1]; |
| |
| /* |
| * If there were no record hinting faults then either the task is |
| * completely idle or all activity is areas that are not of interest |
| * to automatic numa balancing. Related to that, if there were failed |
| * migration then it implies we are migrating too quickly or the local |
| * node is overloaded. In either case, scan slower |
| */ |
| if (local + shared == 0 || p->numa_faults_locality[2]) { |
| p->numa_scan_period = min(p->numa_scan_period_max, |
| p->numa_scan_period << 1); |
| |
| p->mm->numa_next_scan = jiffies + |
| msecs_to_jiffies(p->numa_scan_period); |
| |
| return; |
| } |
| |
| /* |
| * Prepare to scale scan period relative to the current period. |
| * == NUMA_PERIOD_THRESHOLD scan period stays the same |
| * < NUMA_PERIOD_THRESHOLD scan period decreases (scan faster) |
| * >= NUMA_PERIOD_THRESHOLD scan period increases (scan slower) |
| */ |
| period_slot = DIV_ROUND_UP(p->numa_scan_period, NUMA_PERIOD_SLOTS); |
| lr_ratio = (local * NUMA_PERIOD_SLOTS) / (local + remote); |
| ps_ratio = (private * NUMA_PERIOD_SLOTS) / (private + shared); |
| |
| if (ps_ratio >= NUMA_PERIOD_THRESHOLD) { |
| /* |
| * Most memory accesses are local. There is no need to |
| * do fast NUMA scanning, since memory is already local. |
| */ |
| int slot = ps_ratio - NUMA_PERIOD_THRESHOLD; |
| if (!slot) |
| slot = 1; |
| diff = slot * period_slot; |
| } else if (lr_ratio >= NUMA_PERIOD_THRESHOLD) { |
| /* |
| * Most memory accesses are shared with other tasks. |
| * There is no point in continuing fast NUMA scanning, |
| * since other tasks may just move the memory elsewhere. |
| */ |
| int slot = lr_ratio - NUMA_PERIOD_THRESHOLD; |
| if (!slot) |
| slot = 1; |
| diff = slot * period_slot; |
| } else { |
| /* |
| * Private memory faults exceed (SLOTS-THRESHOLD)/SLOTS, |
| * yet they are not on the local NUMA node. Speed up |
| * NUMA scanning to get the memory moved over. |
| */ |
| int ratio = max(lr_ratio, ps_ratio); |
| diff = -(NUMA_PERIOD_THRESHOLD - ratio) * period_slot; |
| } |
| |
| p->numa_scan_period = clamp(p->numa_scan_period + diff, |
| task_scan_min(p), task_scan_max(p)); |
| memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); |
| } |
| |
| /* |
| * Get the fraction of time the task has been running since the last |
| * NUMA placement cycle. The scheduler keeps similar statistics, but |
| * decays those on a 32ms period, which is orders of magnitude off |
| * from the dozens-of-seconds NUMA balancing period. Use the scheduler |
| * stats only if the task is so new there are no NUMA statistics yet. |
| */ |
| static u64 numa_get_avg_runtime(struct task_struct *p, u64 *period) |
| { |
| u64 runtime, delta, now; |
| /* Use the start of this time slice to avoid calculations. */ |
| now = p->se.exec_start; |
| runtime = p->se.sum_exec_runtime; |
| |
| if (p->last_task_numa_placement) { |
| delta = runtime - p->last_sum_exec_runtime; |
| *period = now - p->last_task_numa_placement; |
| |
| /* Avoid time going backwards, prevent potential divide error: */ |
| if (unlikely((s64)*period < 0)) |
| *period = 0; |
| } else { |
| delta = p->se.avg.load_sum; |
| *period = LOAD_AVG_MAX; |
| } |
| |
| p->last_sum_exec_runtime = runtime; |
| p->last_task_numa_placement = now; |
| |
| return delta; |
| } |
| |
| /* |
| * Determine the preferred nid for a task in a numa_group. This needs to |
| * be done in a way that produces consistent results with group_weight, |
| * otherwise workloads might not converge. |
| */ |
| static int preferred_group_nid(struct task_struct *p, int nid) |
| { |
| nodemask_t nodes; |
| int dist; |
| |
| /* Direct connections between all NUMA nodes. */ |
| if (sched_numa_topology_type == NUMA_DIRECT) |
| return nid; |
| |
| /* |
| * On a system with glueless mesh NUMA topology, group_weight |
| * scores nodes according to the number of NUMA hinting faults on |
| * both the node itself, and on nearby nodes. |
| */ |
| if (sched_numa_topology_type == NUMA_GLUELESS_MESH) { |
| unsigned long score, max_score = 0; |
| int node, max_node = nid; |
| |
| dist = sched_max_numa_distance; |
| |
| for_each_online_node(node) { |
| score = group_weight(p, node, dist); |
| if (score > max_score) { |
| max_score = score; |
| max_node = node; |
| } |
| } |
| return max_node; |
| } |
| |
| /* |
| * Finding the preferred nid in a system with NUMA backplane |
| * interconnect topology is more involved. The goal is to locate |
| * tasks from numa_groups near each other in the system, and |
| * untangle workloads from different sides of the system. This requires |
| * searching down the hierarchy of node groups, recursively searching |
| * inside the highest scoring group of nodes. The nodemask tricks |
| * keep the complexity of the search down. |
| */ |
| nodes = node_online_map; |
| for (dist = sched_max_numa_distance; dist > LOCAL_DISTANCE; dist--) { |
| unsigned long max_faults = 0; |
| nodemask_t max_group = NODE_MASK_NONE; |
| int a, b; |
| |
| /* Are there nodes at this distance from each other? */ |
| if (!find_numa_distance(dist)) |
| continue; |
| |
| for_each_node_mask(a, nodes) { |
| unsigned long faults = 0; |
| nodemask_t this_group; |
| nodes_clear(this_group); |
| |
| /* Sum group's NUMA faults; includes a==b case. */ |
| for_each_node_mask(b, nodes) { |
| if (node_distance(a, b) < dist) { |
| faults += group_faults(p, b); |
| node_set(b, this_group); |
| node_clear(b, nodes); |
| } |
| } |
| |
| /* Remember the top group. */ |
| if (faults > max_faults) { |
| max_faults = faults; |
| max_group = this_group; |
| /* |
| * subtle: at the smallest distance there is |
| * just one node left in each "group", the |
| * winner is the preferred nid. |
| */ |
| nid = a; |
| } |
| } |
| /* Next round, evaluate the nodes within max_group. */ |
| if (!max_faults) |
| break; |
| nodes = max_group; |
| } |
| return nid; |
| } |
| |
| static void task_numa_placement(struct task_struct *p) |
| { |
| int seq, nid, max_nid = NUMA_NO_NODE; |
| unsigned long max_faults = 0; |
| unsigned long fault_types[2] = { 0, 0 }; |
| unsigned long total_faults; |
| u64 runtime, period; |
| spinlock_t *group_lock = NULL; |
| |
| /* |
| * The p->mm->numa_scan_seq field gets updated without |
| * exclusive access. Use READ_ONCE() here to ensure |
| * that the field is read in a single access: |
| */ |
| seq = READ_ONCE(p->mm->numa_scan_seq); |
| if (p->numa_scan_seq == seq) |
| return; |
| p->numa_scan_seq = seq; |
| p->numa_scan_period_max = task_scan_max(p); |
| |
| total_faults = p->numa_faults_locality[0] + |
| p->numa_faults_locality[1]; |
| runtime = numa_get_avg_runtime(p, &period); |
| |
| /* If the task is part of a group prevent parallel updates to group stats */ |
| if (p->numa_group) { |
| group_lock = &p->numa_group->lock; |
| spin_lock_irq(group_lock); |
| } |
| |
| /* Find the node with the highest number of faults */ |
| for_each_online_node(nid) { |
| /* Keep track of the offsets in numa_faults array */ |
| int mem_idx, membuf_idx, cpu_idx, cpubuf_idx; |
| unsigned long faults = 0, group_faults = 0; |
| int priv; |
| |
| for (priv = 0; priv < NR_NUMA_HINT_FAULT_TYPES; priv++) { |
| long diff, f_diff, f_weight; |
| |
| mem_idx = task_faults_idx(NUMA_MEM, nid, priv); |
| membuf_idx = task_faults_idx(NUMA_MEMBUF, nid, priv); |
| cpu_idx = task_faults_idx(NUMA_CPU, nid, priv); |
| cpubuf_idx = task_faults_idx(NUMA_CPUBUF, nid, priv); |
| |
| /* Decay existing window, copy faults since last scan */ |
| diff = p->numa_faults[membuf_idx] - p->numa_faults[mem_idx] / 2; |
| fault_types[priv] += p->numa_faults[membuf_idx]; |
| p->numa_faults[membuf_idx] = 0; |
| |
| /* |
| * Normalize the faults_from, so all tasks in a group |
| * count according to CPU use, instead of by the raw |
| * number of faults. Tasks with little runtime have |
| * little over-all impact on throughput, and thus their |
| * faults are less important. |
| */ |
| f_weight = div64_u64(runtime << 16, period + 1); |
| f_weight = (f_weight * p->numa_faults[cpubuf_idx]) / |
| (total_faults + 1); |
| f_diff = f_weight - p->numa_faults[cpu_idx] / 2; |
| p->numa_faults[cpubuf_idx] = 0; |
| |
| p->numa_faults[mem_idx] += diff; |
| p->numa_faults[cpu_idx] += f_diff; |
| faults += p->numa_faults[mem_idx]; |
| p->total_numa_faults += diff; |
| if (p->numa_group) { |
| /* |
| * safe because we can only change our own group |
| * |
| * mem_idx represents the offset for a given |
| * nid and priv in a specific region because it |
| * is at the beginning of the numa_faults array. |
| */ |
| p->numa_group->faults[mem_idx] += diff; |
| p->numa_group->faults_cpu[mem_idx] += f_diff; |
| p->numa_group->total_faults += diff; |
| group_faults += p->numa_group->faults[mem_idx]; |
| } |
| } |
| |
| if (!p->numa_group) { |
| if (faults > max_faults) { |
| max_faults = faults; |
| max_nid = nid; |
| } |
| } else if (group_faults > max_faults) { |
| max_faults = group_faults; |
| max_nid = nid; |
| } |
| } |
| |
| if (p->numa_group) { |
| numa_group_count_active_nodes(p->numa_group); |
| spin_unlock_irq(group_lock); |
| max_nid = preferred_group_nid(p, max_nid); |
| } |
| |
| if (max_faults) { |
| /* Set the new preferred node */ |
| if (max_nid != p->numa_preferred_nid) |
| sched_setnuma(p, max_nid); |
| } |
| |
| update_task_scan_period(p, fault_types[0], fault_types[1]); |
| } |
| |
| static inline int get_numa_group(struct numa_group *grp) |
| { |
| return refcount_inc_not_zero(&grp->refcount); |
| } |
| |
| static inline void put_numa_group(struct numa_group *grp) |
| { |
| if (refcount_dec_and_test(&grp->refcount)) |
| kfree_rcu(grp, rcu); |
| } |
| |
| static void task_numa_group(struct task_struct *p, int cpupid, int flags, |
| int *priv) |
| { |
| struct numa_group *grp, *my_grp; |
| struct task_struct *tsk; |
| bool join = false; |
| int cpu = cpupid_to_cpu(cpupid); |
| int i; |
| |
| if (unlikely(!p->numa_group)) { |
| unsigned int size = sizeof(struct numa_group) + |
| 4*nr_node_ids*sizeof(unsigned long); |
| |
| grp = kzalloc(size, GFP_KERNEL | __GFP_NOWARN); |
| if (!grp) |
| return; |
| |
| refcount_set(&grp->refcount, 1); |
| grp->active_nodes = 1; |
| grp->max_faults_cpu = 0; |
| spin_lock_init(&grp->lock); |
| grp->gid = p->pid; |
| /* Second half of the array tracks nids where faults happen */ |
| grp->faults_cpu = grp->faults + NR_NUMA_HINT_FAULT_TYPES * |
| nr_node_ids; |
| |
| for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) |
| grp->faults[i] = p->numa_faults[i]; |
| |
| grp->total_faults = p->total_numa_faults; |
| |
| grp->nr_tasks++; |
| rcu_assign_pointer(p->numa_group, grp); |
| } |
| |
| rcu_read_lock(); |
| tsk = READ_ONCE(cpu_rq(cpu)->curr); |
| |
| if (!cpupid_match_pid(tsk, cpupid)) |
| goto no_join; |
| |
| grp = rcu_dereference(tsk->numa_group); |
| if (!grp) |
| goto no_join; |
| |
| my_grp = p->numa_group; |
| if (grp == my_grp) |
| goto no_join; |
| |
| /* |
| * Only join the other group if its bigger; if we're the bigger group, |
| * the other task will join us. |
| */ |
| if (my_grp->nr_tasks > grp->nr_tasks) |
| goto no_join; |
| |
| /* |
| * Tie-break on the grp address. |
| */ |
| if (my_grp->nr_tasks == grp->nr_tasks && my_grp > grp) |
| goto no_join; |
| |
| /* Always join threads in the same process. */ |
| if (tsk->mm == current->mm) |
| join = true; |
| |
| /* Simple filter to avoid false positives due to PID collisions */ |
| if (flags & TNF_SHARED) |
| join = true; |
| |
| /* Update priv based on whether false sharing was detected */ |
| *priv = !join; |
| |
| if (join && !get_numa_group(grp)) |
| goto no_join; |
| |
| rcu_read_unlock(); |
| |
| if (!join) |
| return; |
| |
| BUG_ON(irqs_disabled()); |
| double_lock_irq(&my_grp->lock, &grp->lock); |
| |
| for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) { |
| my_grp->faults[i] -= p->numa_faults[i]; |
| grp->faults[i] += p->numa_faults[i]; |
| } |
| my_grp->total_faults -= p->total_numa_faults; |
| grp->total_faults += p->total_numa_faults; |
| |
| my_grp->nr_tasks--; |
| grp->nr_tasks++; |
| |
| spin_unlock(&my_grp->lock); |
| spin_unlock_irq(&grp->lock); |
| |
| rcu_assign_pointer(p->numa_group, grp); |
| |
| put_numa_group(my_grp); |
| return; |
| |
| no_join: |
| rcu_read_unlock(); |
| return; |
| } |
| |
| void task_numa_free(struct task_struct *p) |
| { |
| struct numa_group *grp = p->numa_group; |
| void *numa_faults = p->numa_faults; |
| unsigned long flags; |
| int i; |
| |
| if (grp) { |
| spin_lock_irqsave(&grp->lock, flags); |
| for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) |
| grp->faults[i] -= p->numa_faults[i]; |
| grp->total_faults -= p->total_numa_faults; |
| |
| grp->nr_tasks--; |
| spin_unlock_irqrestore(&grp->lock, flags); |
| RCU_INIT_POINTER(p->numa_group, NULL); |
| put_numa_group(grp); |
| } |
| |
| p->numa_faults = NULL; |
| kfree(numa_faults); |
| } |
| |
| /* |
| * Got a PROT_NONE fault for a page on @node. |
| */ |
| void task_numa_fault(int last_cpupid, int mem_node, int pages, int flags) |
| { |
| struct task_struct *p = current; |
| bool migrated = flags & TNF_MIGRATED; |
| int cpu_node = task_node(current); |
| int local = !!(flags & TNF_FAULT_LOCAL); |
| struct numa_group *ng; |
| int priv; |
| |
| if (!static_branch_likely(&sched_numa_balancing)) |
| return; |
| |
| /* for example, ksmd faulting in a user's mm */ |
| if (!p->mm) |
| return; |
| |
| /* Allocate buffer to track faults on a per-node basis */ |
| if (unlikely(!p->numa_faults)) { |
| int size = sizeof(*p->numa_faults) * |
| NR_NUMA_HINT_FAULT_BUCKETS * nr_node_ids; |
| |
| p->numa_faults = kzalloc(size, GFP_KERNEL|__GFP_NOWARN); |
| if (!p->numa_faults) |
| return; |
| |
| p->total_numa_faults = 0; |
| memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); |
| } |
| |
| /* |
| * First accesses are treated as private, otherwise consider accesses |
| * to be private if the accessing pid has not changed |
| */ |
| if (unlikely(last_cpupid == (-1 & LAST_CPUPID_MASK))) { |
| priv = 1; |
| } else { |
| priv = cpupid_match_pid(p, last_cpupid); |
| if (!priv && !(flags & TNF_NO_GROUP)) |
| task_numa_group(p, last_cpupid, flags, &priv); |
| } |
| |
| /* |
| * If a workload spans multiple NUMA nodes, a shared fault that |
| * occurs wholly within the set of nodes that the workload is |
| * actively using should be counted as local. This allows the |
| * scan rate to slow down when a workload has settled down. |
| */ |
| ng = p->numa_group; |
| if (!priv && !local && ng && ng->active_nodes > 1 && |
| numa_is_active_node(cpu_node, ng) && |
| numa_is_active_node(mem_node, ng)) |
| local = 1; |
| |
| /* |
| * Retry to migrate task to preferred node periodically, in case it |
| * previously failed, or the scheduler moved us. |
| */ |
| if (time_after(jiffies, p->numa_migrate_retry)) { |
| task_numa_placement(p); |
| numa_migrate_preferred(p); |
| } |
| |
| if (migrated) |
| p->numa_pages_migrated += pages; |
| if (flags & TNF_MIGRATE_FAIL) |
| p->numa_faults_locality[2] += pages; |
| |
| p->numa_faults[task_faults_idx(NUMA_MEMBUF, mem_node, priv)] += pages; |
| p->numa_faults[task_faults_idx(NUMA_CPUBUF, cpu_node, priv)] += pages; |
| p->numa_faults_locality[local] += pages; |
| } |
| |
| static void reset_ptenuma_scan(struct task_struct *p) |
| { |
| /* |
| * We only did a read acquisition of the mmap sem, so |
| * p->mm->numa_scan_seq is written to without exclusive access |
| * and the update is not guaranteed to be atomic. That's not |
| * much of an issue though, since this is just used for |
| * statistical sampling. Use READ_ONCE/WRITE_ONCE, which are not |
| * expensive, to avoid any form of compiler optimizations: |
| */ |
| WRITE_ONCE(p->mm->numa_scan_seq, READ_ONCE(p->mm->numa_scan_seq) + 1); |
| p->mm->numa_scan_offset = 0; |
| } |
| |
| /* |
| * The expensive part of numa migration is done from task_work context. |
| * Triggered from task_tick_numa(). |
| */ |
| void task_numa_work(struct callback_head *work) |
| { |
| unsigned long migrate, next_scan, now = jiffies; |
| struct task_struct *p = current; |
| struct mm_struct *mm = p->mm; |
| u64 runtime = p->se.sum_exec_runtime; |
| struct vm_area_struct *vma; |
| unsigned long start, end; |
| unsigned long nr_pte_updates = 0; |
| long pages, virtpages; |
| |
| SCHED_WARN_ON(p != container_of(work, struct task_struct, numa_work)); |
| |
| work->next = work; /* protect against double add */ |
| /* |
| * Who cares about NUMA placement when they're dying. |
| * |
| * NOTE: make sure not to dereference p->mm before this check, |
| * exit_task_work() happens _after_ exit_mm() so we could be called |
| * without p->mm even though we still had it when we enqueued this |
| * work. |
| */ |
| if (p->flags & PF_EXITING) |
| return; |
| |
| if (!mm->numa_next_scan) { |
| mm->numa_next_scan = now + |
| msecs_to_jiffies(sysctl_numa_balancing_scan_delay); |
| } |
| |
| /* |
| * Enforce maximal scan/migration frequency.. |
| */ |
| migrate = mm->numa_next_scan; |
| if (time_before(now, migrate)) |
| return; |
| |
| if (p->numa_scan_period == 0) { |
| p->numa_scan_period_max = task_scan_max(p); |
| p->numa_scan_period = task_scan_start(p); |
| } |
| |
| next_scan = now + msecs_to_jiffies(p->numa_scan_period); |
| if (cmpxchg(&mm->numa_next_scan, migrate, next_scan) != migrate) |
| return; |
| |
| /* |
| * Delay this task enough that another task of this mm will likely win |
| * the next time around. |
| */ |
| p->node_stamp += 2 * TICK_NSEC; |
| |
| start = mm->numa_scan_offset; |
| pages = sysctl_numa_balancing_scan_size; |
| pages <<= 20 - PAGE_SHIFT; /* MB in pages */ |
| virtpages = pages * 8; /* Scan up to this much virtual space */ |
| if (!pages) |
| return; |
| |
| |
| if (!down_read_trylock(&mm->mmap_sem)) |
| return; |
| vma = find_vma(mm, start); |
| if (!vma) { |
| reset_ptenuma_scan(p); |
| start = 0; |
| vma = mm->mmap; |
| } |
| for (; vma; vma = vma->vm_next) { |
| if (!vma_migratable(vma) || !vma_policy_mof(vma) || |
| is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_MIXEDMAP)) { |
| continue; |
| } |
| |
| /* |
| * Shared library pages mapped by multiple processes are not |
| * migrated as it is expected they are cache replicated. Avoid |
| * hinting faults in read-only file-backed mappings or the vdso |
| * as migrating the pages will be of marginal benefit. |
| */ |
| if (!vma->vm_mm || |
| (vma->vm_file && (vma->vm_flags & (VM_READ|VM_WRITE)) == (VM_READ))) |
| continue; |
| |
| /* |
| * Skip inaccessible VMAs to avoid any confusion between |
| * PROT_NONE and NUMA hinting ptes |
| */ |
| if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))) |
| continue; |
| |
| do { |
| start = max(start, vma->vm_start); |
| end = ALIGN(start + (pages << PAGE_SHIFT), HPAGE_SIZE); |
| end = min(end, vma->vm_end); |
| nr_pte_updates = change_prot_numa(vma, start, end); |
| |
| /* |
| * Try to scan sysctl_numa_balancing_size worth of |
| * hpages that have at least one present PTE that |
| * is not already pte-numa. If the VMA contains |
| * areas that are unused or already full of prot_numa |
| * PTEs, scan up to virtpages, to skip through those |
| * areas faster. |
| */ |
| if (nr_pte_updates) |
| pages -= (end - start) >> PAGE_SHIFT; |
| virtpages -= (end - start) >> PAGE_SHIFT; |
| |
| start = end; |
| if (pages <= 0 || virtpages <= 0) |
| goto out; |
| |
| cond_resched(); |
| } while (end != vma->vm_end); |
| } |
| |
| out: |
| /* |
| * It is possible to reach the end of the VMA list but the last few |
| * VMAs are not guaranteed to the vma_migratable. If they are not, we |
| * would find the !migratable VMA on the next scan but not reset the |
| * scanner to the start so check it now. |
| */ |
| if (vma) |
| mm->numa_scan_offset = start; |
| else |
| reset_ptenuma_scan(p); |
| up_read(&mm->mmap_sem); |
| |
| /* |
| * Make sure tasks use at least 32x as much time to run other code |
| * than they used here, to limit NUMA PTE scanning overhead to 3% max. |
| * Usually update_task_scan_period slows down scanning enough; on an |
| * overloaded system we need to limit overhead on a per task basis. |
| */ |
| if (unlikely(p->se.sum_exec_runtime != runtime)) { |
| u64 diff = p->se.sum_exec_runtime - runtime; |
| p->node_stamp += 32 * diff; |
| } |
| } |
| |
| /* |
| * Drive the periodic memory faults.. |
| */ |
| void task_tick_numa(struct rq *rq, struct task_struct *curr) |
| { |
| struct callback_head *work = &curr->numa_work; |
| u64 period, now; |
| |
| /* |
| * We don't care about NUMA placement if we don't have memory. |
| */ |
| if (!curr->mm || (curr->flags & PF_EXITING) || work->next != work) |
| return; |
| |
| /* |
| * Using runtime rather than walltime has the dual advantage that |
| * we (mostly) drive the selection from busy threads and that the |
| * task needs to have done some actual work before we bother with |
| * NUMA placement. |
| */ |
| now = curr->se.sum_exec_runtime; |
| period = (u64)curr->numa_scan_period * NSEC_PER_MSEC; |
| |
| if (now > curr->node_stamp + period) { |
| if (!curr->node_stamp) |
| curr->numa_scan_period = task_scan_start(curr); |
| curr->node_stamp += period; |
| |
| if (!time_before(jiffies, curr->mm->numa_next_scan)) { |
| init_task_work(work, task_numa_work); /* TODO: move this into sched_fork() */ |
| task_work_add(curr, work, true); |
| } |
| } |
| } |
| |
| static void update_scan_period(struct task_struct *p, int new_cpu) |
| { |
| int src_nid = cpu_to_node(task_cpu(p)); |
| int dst_nid = cpu_to_node(new_cpu); |
| |
| if (!static_branch_likely(&sched_numa_balancing)) |
| return; |
| |
| if (!p->mm || !p->numa_faults || (p->flags & PF_EXITING)) |
| return; |
| |
| if (src_nid == dst_nid) |
| return; |
| |
| /* |
| * Allow resets if faults have been trapped before one scan |
| * has completed. This is most likely due to a new task that |
| * is pulled cross-node due to wakeups or load balancing. |
| */ |
| if (p->numa_scan_seq) { |
| /* |
| * Avoid scan adjustments if moving to the preferred |
| * node or if the task was not previously running on |
| * the preferred node. |
| */ |
| if (dst_nid == p->numa_preferred_nid || |
| (p->numa_preferred_nid != NUMA_NO_NODE && |
| src_nid != p->numa_preferred_nid)) |
| return; |
| } |
| |
| p->numa_scan_period = task_scan_start(p); |
| } |
| |
| #else |
| static void task_tick_numa(struct rq *rq, struct task_struct *curr) |
| { |
| } |
| |
| static inline void account_numa_enqueue(struct rq *rq, struct task_struct *p) |
| { |
| } |
| |
| static inline void account_numa_dequeue(struct rq *rq, struct task_struct *p) |
| { |
| } |
| |
| static inline void update_scan_period(struct task_struct *p, int new_cpu) |
| { |
| } |
| |
| #endif /* CONFIG_NUMA_BALANCING */ |
| |
| static void |
| account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| { |
| update_load_add(&cfs_rq->load, se->load.weight); |
| if (!parent_entity(se)) |
| update_load_add(&rq_of(cfs_rq)->load, se->load.weight); |
| #ifdef CONFIG_SMP |
| if (entity_is_task(se)) { |
| struct rq *rq = rq_of(cfs_rq); |
| |
| account_numa_enqueue(rq, task_of(se)); |
| list_add(&se->group_node, &rq->cfs_tasks); |
| } |
| #endif |
| cfs_rq->nr_running++; |
| } |
| |
| static void |
| account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| { |
| update_load_sub(&cfs_rq->load, se->load.weight); |
| if (!parent_entity(se)) |
| update_load_sub(&rq_of(cfs_rq)->load, se->load.weight); |
| #ifdef CONFIG_SMP |
| if (entity_is_task(se)) { |
| account_numa_dequeue(rq_of(cfs_rq), task_of(se)); |
| list_del_init(&se->group_node); |
| } |
| #endif |
| cfs_rq->nr_running--; |
| } |
| |
| /* |
| * Signed add and clamp on underflow. |
| * |
| * Explicitly do a load-store to ensure the intermediate value never hits |
| * memory. This allows lockless observations without ever seeing the negative |
| * values. |
| */ |
| #define add_positive(_ptr, _val) do { \ |
| typeof(_ptr) ptr = (_ptr); \ |
| typeof(_val) val = (_val); \ |
| typeof(*ptr) res, var = READ_ONCE(*ptr); \ |
| \ |
| res = var + val; \ |
| \ |
| if (val < 0 && res > var) \ |
| res = 0; \ |
| \ |
| WRITE_ONCE(*ptr, res); \ |
| } while (0) |
| |
| /* |
| * Unsigned subtract and clamp on underflow. |
| * |
| * Explicitly do a load-store to ensure the intermediate value never hits |
| * memory. This allows lockless observations without ever seeing the negative |
| * values. |
| */ |
| #define sub_positive(_ptr, _val) do { \ |
| typeof(_ptr) ptr = (_ptr); \ |
| typeof(*ptr) val = (_val); \ |
| typeof(*ptr) res, var = READ_ONCE(*ptr); \ |
| res = var - val; \ |
| if (res > var) \ |
| res = 0; \ |
| WRITE_ONCE(*ptr, res); \ |
| } while (0) |
| |
| /* |
| * Remove and clamp on negative, from a local variable. |
| * |
| * A variant of sub_positive(), which does not use explicit load-store |
| * and is thus optimized for local variable updates. |
| */ |
| #define lsub_positive(_ptr, _val) do { \ |
| typeof(_ptr) ptr = (_ptr); \ |
| *ptr -= min_t(typeof(*ptr), *ptr, _val); \ |
| } while (0) |
| |
| #ifdef CONFIG_SMP |
| static inline void |
| enqueue_runnable_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| { |
| cfs_rq->runnable_weight += se->runnable_weight; |
| |
| cfs_rq->avg.runnable_load_avg += se->avg.runnable_load_avg; |
| cfs_rq->avg.runnable_load_sum += se_runnable(se) * se->avg.runnable_load_sum; |
| } |
| |
| static inline void |
| dequeue_runnable_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| { |
| cfs_rq->runnable_weight -= se->runnable_weight; |
| |
| sub_positive(&cfs_rq->avg.runnable_load_avg, se->avg.runnable_load_avg); |
| sub_positive(&cfs_rq->avg.runnable_load_sum, |
| se_runnable(se) * se->avg.runnable_load_sum); |
| } |
| |
| static inline void |
| enqueue_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| { |
| cfs_rq->avg.load_avg += se->avg.load_avg; |
| cfs_rq->avg.load_sum += se_weight(se) * se->avg.load_sum; |
| } |
| |
| static inline void |
| dequeue_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| { |
| sub_positive(&cfs_rq->avg.load_avg, se->avg.load_avg); |
| sub_positive(&cfs_rq->avg.load_sum, se_weight(se) * se->avg.load_sum); |
| } |
| #else |
| static inline void |
| enqueue_runnable_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) { } |
| static inline void |
| dequeue_runnable_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) { } |
| static inline void |
| enqueue_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) { } |
| static inline void |
| dequeue_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) { } |
| #endif |
| |
| static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, |
| unsigned long weight, unsigned long runnable) |
| { |
| if (se->on_rq) { |
| /* commit outstanding execution time */ |
| if (cfs_rq->curr == se) |
| update_curr(cfs_rq); |
| account_entity_dequeue(cfs_rq, se); |
| dequeue_runnable_load_avg(cfs_rq, se); |
| } |
| dequeue_load_avg(cfs_rq, se); |
| |
| se->runnable_weight = runnable; |
| update_load_set(&se->load, weight); |
| |
| #ifdef CONFIG_SMP |
| do { |
| u32 divider = LOAD_AVG_MAX - 1024 + se->avg.period_contrib; |
| |
| se->avg.load_avg = div_u64(se_weight(se) * se->avg.load_sum, divider); |
| se->avg.runnable_load_avg = |
| div_u64(se_runnable(se) * se->avg.runnable_load_sum, divider); |
| } while (0); |
| #endif |
| |
| enqueue_load_avg(cfs_rq, se); |
| if (se->on_rq) { |
| account_entity_enqueue(cfs_rq, se); |
| enqueue_runnable_load_avg(cfs_rq, se); |
| } |
| } |
| |
| void reweight_task(struct task_struct *p, int prio) |
| { |
| struct sched_entity *se = &p->se; |
| struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| struct load_weight *load = &se->load; |
| unsigned long weight = scale_load(sched_prio_to_weight[prio]); |
| |
| reweight_entity(cfs_rq, se, weight, weight); |
| load->inv_weight = sched_prio_to_wmult[prio]; |
| } |
| |
| #ifdef CONFIG_FAIR_GROUP_SCHED |
| #ifdef CONFIG_SMP |
| /* |
| * All this does is approximate the hierarchical proportion which includes that |
| * global sum we all love to hate. |
| * |
| * That is, the weight of a group entity, is the proportional share of the |
| * group weight based on the group runqueue weights. That is: |
| * |
| * tg->weight * grq->load.weight |
| * ge->load.weight = ----------------------------- (1) |
| * \Sum grq->load.weight |
| * |
| * Now, because computing that sum is prohibitively expensive to compute (been |
| * there, done that) we approximate it with this average stuff. The average |
| * moves slower and therefore the approximation is cheaper and more stable. |
| * |
| * So instead of the above, we substitute: |
| * |
| * grq->load.weight -> grq->avg.load_avg (2) |
| * |
| * which yields the following: |
| * |
| * tg->weight * grq->avg.load_avg |
| * ge->load.weight = ------------------------------ (3) |
| * tg->load_avg |
| * |
| * Where: tg->load_avg ~= \Sum grq->avg.load_avg |
| * |
| * That is shares_avg, and it is right (given the approximation (2)). |
| * |
| * The problem with it is that because the average is slow -- it was designed |
| * to be exactly that of course -- this leads to transients in boundary |
| * conditions. In specific, the case where the group was idle and we start the |
| * one task. It takes time for our CPU's grq->avg.load_avg to build up, |
| * yielding bad latency etc.. |
| * |
| * Now, in that special case (1) reduces to: |
| * |
| * tg->weight * grq->load.weight |
| * ge->load.weight = ----------------------------- = tg->weight (4) |
| * grp->load.weight |
| * |
| * That is, the sum collapses because all other CPUs are idle; the UP scenario. |
| * |
| * So what we do is modify our approximation (3) to approach (4) in the (near) |
| * UP case, like: |
| * |
| * ge->load.weight = |
| * |
| * tg->weight * grq->load.weight |
| * --------------------------------------------------- (5) |
| * tg->load_avg - grq->avg.load_avg + grq->load.weight |
| * |
| * But because grq->load.weight can drop to 0, resulting in a divide by zero, |
| * we need to use grq->avg.load_avg as its lower bound, which then gives: |
| * |
| * |
| * tg->weight * grq->load.weight |
| * ge->load.weight = ----------------------------- (6) |
| * tg_load_avg' |
| * |
| * Where: |
| * |
| * tg_load_avg' = tg->load_avg - grq->avg.load_avg + |
| * max(grq->load.weight, grq->avg.load_avg) |
| * |
| * And that is shares_weight and is icky. In the (near) UP case it approaches |
| * (4) while in the normal case it approaches (3). It consistently |
| * overestimates the ge->load.weight and therefore: |
| * |
| * \Sum ge->load.weight >= tg->weight |
| * |
| * hence icky! |
| */ |
| static long calc_group_shares(struct cfs_rq *cfs_rq) |
| { |
| long tg_weight, tg_shares, load, shares; |
| struct task_group *tg = cfs_rq->tg; |
| |
| tg_shares = READ_ONCE(tg->shares); |
| |
| load = max(scale_load_down(cfs_rq->load.weight), cfs_rq->avg.load_avg); |
| |
| tg_weight = atomic_long_read(&tg->load_avg); |
| |
| /* Ensure tg_weight >= load */ |
| tg_weight -= cfs_rq->tg_load_avg_contrib; |
| tg_weight += load; |
| |
| shares = (tg_shares * load); |
| if (tg_weight) |
| shares /= tg_weight; |
| |
| /* |
| * MIN_SHARES has to be unscaled here to support per-CPU partitioning |
| * of a group with small tg->shares value. It is a floor value which is |
| * assigned as a minimum load.weight to the sched_entity representing |
| * the group on a CPU. |
| * |
| * E.g. on 64-bit for a group with tg->shares of scale_load(15)=15*1024 |
| * on an 8-core system with 8 tasks each runnable on one CPU shares has |
| * to be 15*1024*1/8=1920 instead of scale_load(MIN_SHARES)=2*1024. In |
| * case no task is runnable on a CPU MIN_SHARES=2 should be returned |
| * instead of 0. |
| */ |
| return clamp_t(long, shares, MIN_SHARES, tg_shares); |
| } |
| |
| /* |
| * This calculates the effective runnable weight for a group entity based on |
| * the group entity weight calculated above. |
| * |
| * Because of the above approximation (2), our group entity weight is |
| * an load_avg based ratio (3). This means that it includes blocked load and |
| * does not represent the runnable weight. |
| * |
| * Approximate the group entity's runnable weight per ratio from the group |
| * runqueue: |
| * |
| * grq->avg.runnable_load_avg |
| * ge->runnable_weight = ge->load.weight * -------------------------- (7) |
| * grq->avg.load_avg |
| * |
| * However, analogous to above, since the avg numbers are slow, this leads to |
| * transients in the from-idle case. Instead we use: |
| * |
| * ge->runnable_weight = ge->load.weight * |
| * |
| * max(grq->avg.runnable_load_avg, grq->runnable_weight) |
| * ----------------------------------------------------- (8) |
| * max(grq->avg.load_avg, grq->load.weight) |
| * |
| * Where these max() serve both to use the 'instant' values to fix the slow |
| * from-idle and avoid the /0 on to-idle, similar to (6). |
| */ |
| static long calc_group_runnable(struct cfs_rq *cfs_rq, long shares) |
| { |
| long runnable, load_avg; |
| |
| load_avg = max(cfs_rq->avg.load_avg, |
| scale_load_down(cfs_rq->load.weight)); |
| |
| runnable = max(cfs_rq->avg.runnable_load_avg, |
| scale_load_down(cfs_rq->runnable_weight)); |
| |
| runnable *= shares; |
| if (load_avg) |
| runnable /= load_avg; |
| |
| return clamp_t(long, runnable, MIN_SHARES, shares); |
| } |
| #endif /* CONFIG_SMP */ |
| |
| static inline int throttled_hierarchy(struct cfs_rq *cfs_rq); |
| |
| /* |
| * Recomputes the group entity based on the current state of its group |
| * runqueue. |
| */ |
| static void update_cfs_group(struct sched_entity *se) |
| { |
| struct cfs_rq *gcfs_rq = group_cfs_rq(se); |
| long shares, runnable; |
| |
| if (!gcfs_rq) |
| return; |
| |
| if (throttled_hierarchy(gcfs_rq)) |
| return; |
| |
| #ifndef CONFIG_SMP |
| runnable = shares = READ_ONCE(gcfs_rq->tg->shares); |
| |
| if (likely(se->load.weight == shares)) |
| return; |
| #else |
| shares = calc_group_shares(gcfs_rq); |
| runnable = calc_group_runnable(gcfs_rq, shares); |
| #endif |
| |
| reweight_entity(cfs_rq_of(se), se, shares, runnable); |
| } |
| |
| #else /* CONFIG_FAIR_GROUP_SCHED */ |
| static inline void update_cfs_group(struct sched_entity *se) |
| { |
| } |
| #endif /* CONFIG_FAIR_GROUP_SCHED */ |
| |
| static inline void cfs_rq_util_change(struct cfs_rq *cfs_rq, int flags) |
| { |
| struct rq *rq = rq_of(cfs_rq); |
| |
| if (&rq->cfs == cfs_rq || (flags & SCHED_CPUFREQ_MIGRATION)) { |
| /* |
| * There are a few boundary cases this might miss but it should |
| * get called often enough that that should (hopefully) not be |
| * a real problem. |
| * |
| * It will not get called when we go idle, because the idle |
| * thread is a different class (!fair), nor will the utilization |
| * number include things like RT tasks. |
| * |
| * As is, the util number is not freq-invariant (we'd have to |
| * implement arch_scale_freq_capacity() for that). |
| * |
| * See cpu_util(). |
| */ |
| cpufreq_update_util(rq, flags); |
| } |
| } |
| |
| #ifdef CONFIG_SMP |
| #ifdef CONFIG_FAIR_GROUP_SCHED |
| /** |
| * update_tg_load_avg - update the tg's load avg |
| * @cfs_rq: the cfs_rq whose avg changed |
| * @force: update regardless of how small the difference |
| * |
| * This function 'ensures': tg->load_avg := \Sum tg->cfs_rq[]->avg.load. |
| * However, because tg->load_avg is a global value there are performance |
| * considerations. |
| * |
| * In order to avoid having to look at the other cfs_rq's, we use a |
| * differential update where we store the last value we propagated. This in |
| * turn allows skipping updates if the differential is 'small'. |
| * |
| * Updating tg's load_avg is necessary before update_cfs_share(). |
| */ |
| static inline void update_tg_load_avg(struct cfs_rq *cfs_rq, int force) |
| { |
| long delta = cfs_rq->avg.load_avg - cfs_rq->tg_load_avg_contrib; |
| |
| /* |
| * No need to update load_avg for root_task_group as it is not used. |
| */ |
| if (cfs_rq->tg == &root_task_group) |
| return; |
| |
| if (force || abs(delta) > cfs_rq->tg_load_avg_contrib / 64) { |
| atomic_long_add(delta, &cfs_rq->tg->load_avg); |
| cfs_rq->tg_load_avg_contrib = cfs_rq->avg.load_avg; |
| } |
| } |
| |
| /* |
| * Called within set_task_rq() right before setting a task's CPU. The |
| * caller only guarantees p->pi_lock is held; no other assumptions, |
| * including the state of rq->lock, should be made. |
| */ |
| void set_task_rq_fair(struct sched_entity *se, |
| struct cfs_rq *prev, struct cfs_rq *next) |
| { |
| u64 p_last_update_time; |
| u64 n_last_update_time; |
| |
| if (!sched_feat(ATTACH_AGE_LOAD)) |
| return; |
| |
| /* |
| * We are supposed to update the task to "current" time, then its up to |
| * date and ready to go to new CPU/cfs_rq. But we have difficulty in |
| * getting what current time is, so simply throw away the out-of-date |
| * time. This will result in the wakee task is less decayed, but giving |
| * the wakee more load sounds not bad. |
| */ |
| if (!(se->avg.last_update_time && prev)) |
| return; |
| |
| #ifndef CONFIG_64BIT |
| { |
| u64 p_last_update_time_copy; |
| u64 n_last_update_time_copy; |
| |
| do { |
| p_last_update_time_copy = prev->load_last_update_time_copy; |
| n_last_update_time_copy = next->load_last_update_time_copy; |
| |
| smp_rmb(); |
| |
| p_last_update_time = prev->avg.last_update_time; |
| n_last_update_time = next->avg.last_update_time; |
| |
| } while (p_last_update_time != p_last_update_time_copy || |
| n_last_update_time != n_last_update_time_copy); |
| } |
| #else |
| p_last_update_time = prev->avg.last_update_time; |
| n_last_update_time = next->avg.last_update_time; |
| #endif |
| __update_load_avg_blocked_se(p_last_update_time, se); |
| se->avg.last_update_time = n_last_update_time; |
| } |
| |
| |
| /* |
| * When on migration a sched_entity joins/leaves the PELT hierarchy, we need to |
| * propagate its contribution. The key to this propagation is the invariant |
| * that for each group: |
| * |
| * ge->avg == grq->avg (1) |
| * |
| * _IFF_ we look at the pure running and runnable sums. Because they |
| * represent the very same entity, just at different points in the hierarchy. |
| * |
| * Per the above update_tg_cfs_util() is trivial and simply copies the running |
| * sum over (but still wrong, because the group entity and group rq do not have |
| * their PELT windows aligned). |
| * |
| * However, update_tg_cfs_runnable() is more complex. So we have: |
| * |
| * ge->avg.load_avg = ge->load.weight * ge->avg.runnable_avg (2) |
| * |
| * And since, like util, the runnable part should be directly transferable, |
| * the following would _appear_ to be the straight forward approach: |
| * |
| * grq->avg.load_avg = grq->load.weight * grq->avg.runnable_avg (3) |
| * |
| * And per (1) we have: |
| * |
| * ge->avg.runnable_avg == grq->avg.runnable_avg |
| * |
| * Which gives: |
| * |
| * ge->load.weight * grq->avg.load_avg |
| * ge->avg.load_avg = ----------------------------------- (4) |
| * grq->load.weight |
| * |
| * Except that is wrong! |
| * |
| * Because while for entities historical weight is not important and we |
| * really only care about our future and therefore can consider a pure |
| * runnable sum, runqueues can NOT do this. |
| * |
| * We specifically want runqueues to have a load_avg that includes |
| * historical weights. Those represent the blocked load, the load we expect |
| * to (shortly) return to us. This only works by keeping the weights as |
| * integral part of the sum. We therefore cannot decompose as per (3). |
| * |
| * Another reason this doesn't work is that runnable isn't a 0-sum entity. |
| * Imagine a rq with 2 tasks that each are runnable 2/3 of the time. Then the |
| * rq itself is runnable anywhere between 2/3 and 1 depending on how the |
| * runnable section of these tasks overlap (or not). If they were to perfectly |
| * align the rq as a whole would be runnable 2/3 of the time. If however we |
| * always have at least 1 runnable task, the rq as a whole is always runnable. |
| * |
| * So we'll have to approximate.. :/ |
| * |
| * Given the constraint: |
| * |
| * ge->avg.running_sum <= ge->avg.runnable_sum <= LOAD_AVG_MAX |
| * |
| * We can construct a rule that adds runnable to a rq by assuming minimal |
| * overlap. |
| * |
| * On removal, we'll assume each task is equally runnable; which yields: |
| * |
| * grq->avg.runnable_sum = grq->avg.load_sum / grq->load.weight |
| * |
| * XXX: only do this for the part of runnable > running ? |
| * |
| */ |
| |
| static inline void |
| update_tg_cfs_util(struct cfs_rq *cfs_rq, struct sched_entity *se, struct cfs_rq *gcfs_rq) |
| { |
| long delta = gcfs_rq->avg.util_avg - se->avg.util_avg; |
| |
| /* Nothing to update */ |
| if (!delta) |
| return; |
| |
| /* |
| * The relation between sum and avg is: |
| * |
| * LOAD_AVG_MAX - 1024 + sa->period_contrib |
| * |
| * however, the PELT windows are not aligned between grq and gse. |
| */ |
| |
| /* Set new sched_entity's utilization */ |
| se->avg.util_avg = gcfs_rq->avg.util_avg; |
| se->avg.util_sum = se->avg.util_avg * LOAD_AVG_MAX; |
| |
| /* Update parent cfs_rq utilization */ |
| add_positive(&cfs_rq->avg.util_avg, delta); |
| cfs_rq->avg.util_sum = cfs_rq->avg.util_avg * LOAD_AVG_MAX; |
| } |
| |
| static inline void |
| update_tg_cfs_runnable(struct cfs_rq *cfs_rq, struct sched_entity *se, struct cfs_rq *gcfs_rq) |
| { |
| long delta_avg, running_sum, runnable_sum = gcfs_rq->prop_runnable_sum; |
| unsigned long runnable_load_avg, load_avg; |
| u64 runnable_load_sum, load_sum = 0; |
| s64 delta_sum; |
| |
| if (!runnable_sum) |
| return; |
| |
| gcfs_rq->prop_runnable_sum = 0; |
| |
| if (runnable_sum >= |