Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Completely Fair Scheduling (CFS) Class (SCHED_NORMAL/SCHED_BATCH) |
| 3 | * |
| 4 | * Copyright (C) 2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> |
| 5 | * |
| 6 | * Interactivity improvements by Mike Galbraith |
| 7 | * (C) 2007 Mike Galbraith <efault@gmx.de> |
| 8 | * |
| 9 | * Various enhancements by Dmitry Adamushko. |
| 10 | * (C) 2007 Dmitry Adamushko <dmitry.adamushko@gmail.com> |
| 11 | * |
| 12 | * Group scheduling enhancements by Srivatsa Vaddagiri |
| 13 | * Copyright IBM Corporation, 2007 |
| 14 | * Author: Srivatsa Vaddagiri <vatsa@linux.vnet.ibm.com> |
| 15 | * |
| 16 | * Scaled math optimizations by Thomas Gleixner |
| 17 | * Copyright (C) 2007, Thomas Gleixner <tglx@linutronix.de> |
Peter Zijlstra | 2180508 | 2007-08-25 18:41:53 +0200 | [diff] [blame] | 18 | * |
| 19 | * Adaptive scheduling granularity, math enhancements by Peter Zijlstra |
Peter Zijlstra | 90eec10 | 2015-11-16 11:08:45 +0100 | [diff] [blame] | 20 | * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 21 | */ |
| 22 | |
Christian Ehrhardt | 1983a92 | 2009-11-30 12:16:47 +0100 | [diff] [blame] | 23 | #include <linux/sched.h> |
Mel Gorman | cb25176 | 2016-02-05 09:08:36 +0000 | [diff] [blame] | 24 | #include <linux/latencytop.h> |
Sisir Koppaka | 3436ae1 | 2011-03-26 18:22:55 +0530 | [diff] [blame] | 25 | #include <linux/cpumask.h> |
Nicolas Pitre | 83a0a96 | 2014-09-04 11:32:10 -0400 | [diff] [blame] | 26 | #include <linux/cpuidle.h> |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 27 | #include <linux/slab.h> |
| 28 | #include <linux/profile.h> |
| 29 | #include <linux/interrupt.h> |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 30 | #include <linux/mempolicy.h> |
Mel Gorman | e14808b | 2012-11-19 10:59:15 +0000 | [diff] [blame] | 31 | #include <linux/migrate.h> |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 32 | #include <linux/task_work.h> |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 33 | |
| 34 | #include <trace/events/sched.h> |
| 35 | |
| 36 | #include "sched.h" |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 37 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 38 | /* |
Peter Zijlstra | 2180508 | 2007-08-25 18:41:53 +0200 | [diff] [blame] | 39 | * Targeted preemption latency for CPU-bound tasks: |
Takuya Yoshikawa | 864616e | 2010-10-14 16:09:13 +0900 | [diff] [blame] | 40 | * (default: 6ms * (1 + ilog(ncpus)), units: nanoseconds) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 41 | * |
Peter Zijlstra | 2180508 | 2007-08-25 18:41:53 +0200 | [diff] [blame] | 42 | * NOTE: this latency value is not the same as the concept of |
Ingo Molnar | d274a4c | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 43 | * 'timeslice length' - timeslices in CFS are of variable length |
| 44 | * and have no persistent notion like in traditional, time-slice |
| 45 | * based scheduling concepts. |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 46 | * |
Ingo Molnar | d274a4c | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 47 | * (to see the precise effective timeslice length of your workload, |
| 48 | * run vmstat and monitor the context-switches (cs) field) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 49 | */ |
Mike Galbraith | 2140692 | 2010-03-11 17:17:15 +0100 | [diff] [blame] | 50 | unsigned int sysctl_sched_latency = 6000000ULL; |
| 51 | unsigned int normalized_sysctl_sched_latency = 6000000ULL; |
Ingo Molnar | 2bd8e6d | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 52 | |
| 53 | /* |
Christian Ehrhardt | 1983a92 | 2009-11-30 12:16:47 +0100 | [diff] [blame] | 54 | * The initial- and re-scaling of tunables is configurable |
| 55 | * (default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus)) |
| 56 | * |
| 57 | * Options are: |
| 58 | * SCHED_TUNABLESCALING_NONE - unscaled, always *1 |
| 59 | * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus) |
| 60 | * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus |
| 61 | */ |
| 62 | enum sched_tunable_scaling sysctl_sched_tunable_scaling |
| 63 | = SCHED_TUNABLESCALING_LOG; |
| 64 | |
| 65 | /* |
Peter Zijlstra | b2be5e9 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 66 | * Minimal preemption granularity for CPU-bound tasks: |
Takuya Yoshikawa | 864616e | 2010-10-14 16:09:13 +0900 | [diff] [blame] | 67 | * (default: 0.75 msec * (1 + ilog(ncpus)), units: nanoseconds) |
Peter Zijlstra | b2be5e9 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 68 | */ |
Ingo Molnar | 0bf377b | 2010-09-12 08:14:52 +0200 | [diff] [blame] | 69 | unsigned int sysctl_sched_min_granularity = 750000ULL; |
| 70 | unsigned int normalized_sysctl_sched_min_granularity = 750000ULL; |
Peter Zijlstra | b2be5e9 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 71 | |
| 72 | /* |
| 73 | * is kept at sysctl_sched_latency / sysctl_sched_min_granularity |
| 74 | */ |
Ingo Molnar | 0bf377b | 2010-09-12 08:14:52 +0200 | [diff] [blame] | 75 | static unsigned int sched_nr_latency = 8; |
Peter Zijlstra | b2be5e9 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 76 | |
| 77 | /* |
Mike Galbraith | 2bba22c | 2009-09-09 15:41:37 +0200 | [diff] [blame] | 78 | * After fork, child runs first. If set to 0 (default) then |
Ingo Molnar | 2bd8e6d | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 79 | * parent will (try to) run first. |
| 80 | */ |
Mike Galbraith | 2bba22c | 2009-09-09 15:41:37 +0200 | [diff] [blame] | 81 | unsigned int sysctl_sched_child_runs_first __read_mostly; |
Peter Zijlstra | 2180508 | 2007-08-25 18:41:53 +0200 | [diff] [blame] | 82 | |
| 83 | /* |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 84 | * SCHED_OTHER wake-up granularity. |
Mike Galbraith | 172e082 | 2009-09-09 15:41:37 +0200 | [diff] [blame] | 85 | * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 86 | * |
| 87 | * This option delays the preemption effects of decoupled workloads |
| 88 | * and reduces their over-scheduling. Synchronous workloads will still |
| 89 | * have immediate wakeup/sleep latencies. |
| 90 | */ |
Mike Galbraith | 172e082 | 2009-09-09 15:41:37 +0200 | [diff] [blame] | 91 | unsigned int sysctl_sched_wakeup_granularity = 1000000UL; |
Christian Ehrhardt | 0bcdcf2 | 2009-11-30 12:16:46 +0100 | [diff] [blame] | 92 | unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 93 | |
Ingo Molnar | da84d96 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 94 | const_debug unsigned int sysctl_sched_migration_cost = 500000UL; |
| 95 | |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 96 | #ifdef CONFIG_CFS_BANDWIDTH |
| 97 | /* |
| 98 | * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool |
| 99 | * each time a cfs_rq requests quota. |
| 100 | * |
| 101 | * Note: in the case that the slice exceeds the runtime remaining (either due |
| 102 | * to consumption or the quota being specified to be smaller than the slice) |
| 103 | * we will always only issue the remaining available time. |
| 104 | * |
| 105 | * default: 5 msec, units: microseconds |
| 106 | */ |
| 107 | unsigned int sysctl_sched_cfs_bandwidth_slice = 5000UL; |
| 108 | #endif |
| 109 | |
Morten Rasmussen | 3273163 | 2016-07-25 14:34:26 +0100 | [diff] [blame] | 110 | /* |
| 111 | * The margin used when comparing utilization with CPU capacity: |
Morten Rasmussen | 893c5d2 | 2016-10-14 14:41:12 +0100 | [diff] [blame] | 112 | * util * margin < capacity * 1024 |
Morten Rasmussen | 3273163 | 2016-07-25 14:34:26 +0100 | [diff] [blame] | 113 | */ |
| 114 | unsigned int capacity_margin = 1280; /* ~20% */ |
| 115 | |
Paul Gortmaker | 8527632 | 2013-04-19 15:10:50 -0400 | [diff] [blame] | 116 | static inline void update_load_add(struct load_weight *lw, unsigned long inc) |
| 117 | { |
| 118 | lw->weight += inc; |
| 119 | lw->inv_weight = 0; |
| 120 | } |
| 121 | |
| 122 | static inline void update_load_sub(struct load_weight *lw, unsigned long dec) |
| 123 | { |
| 124 | lw->weight -= dec; |
| 125 | lw->inv_weight = 0; |
| 126 | } |
| 127 | |
| 128 | static inline void update_load_set(struct load_weight *lw, unsigned long w) |
| 129 | { |
| 130 | lw->weight = w; |
| 131 | lw->inv_weight = 0; |
| 132 | } |
| 133 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 134 | /* |
| 135 | * Increase the granularity value when there are more CPUs, |
| 136 | * because with more CPUs the 'effective latency' as visible |
| 137 | * to users decreases. But the relationship is not linear, |
| 138 | * so pick a second-best guess by going with the log2 of the |
| 139 | * number of CPUs. |
| 140 | * |
| 141 | * This idea comes from the SD scheduler of Con Kolivas: |
| 142 | */ |
Nicholas Mc Guire | 58ac93e | 2015-05-15 21:05:42 +0200 | [diff] [blame] | 143 | static unsigned int get_update_sysctl_factor(void) |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 144 | { |
Nicholas Mc Guire | 58ac93e | 2015-05-15 21:05:42 +0200 | [diff] [blame] | 145 | unsigned int cpus = min_t(unsigned int, num_online_cpus(), 8); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 146 | unsigned int factor; |
| 147 | |
| 148 | switch (sysctl_sched_tunable_scaling) { |
| 149 | case SCHED_TUNABLESCALING_NONE: |
| 150 | factor = 1; |
| 151 | break; |
| 152 | case SCHED_TUNABLESCALING_LINEAR: |
| 153 | factor = cpus; |
| 154 | break; |
| 155 | case SCHED_TUNABLESCALING_LOG: |
| 156 | default: |
| 157 | factor = 1 + ilog2(cpus); |
| 158 | break; |
| 159 | } |
| 160 | |
| 161 | return factor; |
| 162 | } |
| 163 | |
| 164 | static void update_sysctl(void) |
| 165 | { |
| 166 | unsigned int factor = get_update_sysctl_factor(); |
| 167 | |
| 168 | #define SET_SYSCTL(name) \ |
| 169 | (sysctl_##name = (factor) * normalized_sysctl_##name) |
| 170 | SET_SYSCTL(sched_min_granularity); |
| 171 | SET_SYSCTL(sched_latency); |
| 172 | SET_SYSCTL(sched_wakeup_granularity); |
| 173 | #undef SET_SYSCTL |
| 174 | } |
| 175 | |
| 176 | void sched_init_granularity(void) |
| 177 | { |
| 178 | update_sysctl(); |
| 179 | } |
| 180 | |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 181 | #define WMULT_CONST (~0U) |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 182 | #define WMULT_SHIFT 32 |
| 183 | |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 184 | static void __update_inv_weight(struct load_weight *lw) |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 185 | { |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 186 | unsigned long w; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 187 | |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 188 | if (likely(lw->inv_weight)) |
| 189 | return; |
| 190 | |
| 191 | w = scale_load_down(lw->weight); |
| 192 | |
| 193 | if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST)) |
| 194 | lw->inv_weight = 1; |
| 195 | else if (unlikely(!w)) |
| 196 | lw->inv_weight = WMULT_CONST; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 197 | else |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 198 | lw->inv_weight = WMULT_CONST / w; |
| 199 | } |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 200 | |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 201 | /* |
| 202 | * delta_exec * weight / lw.weight |
| 203 | * OR |
| 204 | * (delta_exec * (weight * lw->inv_weight)) >> WMULT_SHIFT |
| 205 | * |
Yuyang Du | 1c3de5e | 2016-03-30 07:07:51 +0800 | [diff] [blame] | 206 | * Either weight := NICE_0_LOAD and lw \e sched_prio_to_wmult[], in which case |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 207 | * we're guaranteed shift stays positive because inv_weight is guaranteed to |
| 208 | * fit 32 bits, and NICE_0_LOAD gives another 10 bits; therefore shift >= 22. |
| 209 | * |
| 210 | * Or, weight =< lw.weight (because lw.weight is the runqueue weight), thus |
| 211 | * weight/lw.weight <= 1, and therefore our shift will also be positive. |
| 212 | */ |
| 213 | static u64 __calc_delta(u64 delta_exec, unsigned long weight, struct load_weight *lw) |
| 214 | { |
| 215 | u64 fact = scale_load_down(weight); |
| 216 | int shift = WMULT_SHIFT; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 217 | |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 218 | __update_inv_weight(lw); |
| 219 | |
| 220 | if (unlikely(fact >> 32)) { |
| 221 | while (fact >> 32) { |
| 222 | fact >>= 1; |
| 223 | shift--; |
| 224 | } |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 225 | } |
| 226 | |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 227 | /* hint to use a 32x32->64 mul */ |
| 228 | fact = (u64)(u32)fact * lw->inv_weight; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 229 | |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 230 | while (fact >> 32) { |
| 231 | fact >>= 1; |
| 232 | shift--; |
| 233 | } |
| 234 | |
| 235 | return mul_u64_u32_shr(delta_exec, fact, shift); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | |
| 239 | const struct sched_class fair_sched_class; |
Peter Zijlstra | a4c2f00 | 2008-10-17 19:27:03 +0200 | [diff] [blame] | 240 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 241 | /************************************************************** |
| 242 | * CFS operations on generic schedulable entities: |
| 243 | */ |
| 244 | |
| 245 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 246 | |
| 247 | /* cpu runqueue to which this cfs_rq is attached */ |
| 248 | static inline struct rq *rq_of(struct cfs_rq *cfs_rq) |
| 249 | { |
| 250 | return cfs_rq->rq; |
| 251 | } |
| 252 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 253 | /* An entity is a task if it doesn't "own" a runqueue */ |
| 254 | #define entity_is_task(se) (!se->my_q) |
| 255 | |
Peter Zijlstra | 8f48894 | 2009-07-24 12:25:30 +0200 | [diff] [blame] | 256 | static inline struct task_struct *task_of(struct sched_entity *se) |
| 257 | { |
Peter Zijlstra | 9148a3a | 2016-09-20 22:34:51 +0200 | [diff] [blame] | 258 | SCHED_WARN_ON(!entity_is_task(se)); |
Peter Zijlstra | 8f48894 | 2009-07-24 12:25:30 +0200 | [diff] [blame] | 259 | return container_of(se, struct task_struct, se); |
| 260 | } |
| 261 | |
Peter Zijlstra | b758149 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 262 | /* Walk up scheduling entities hierarchy */ |
| 263 | #define for_each_sched_entity(se) \ |
| 264 | for (; se; se = se->parent) |
| 265 | |
| 266 | static inline struct cfs_rq *task_cfs_rq(struct task_struct *p) |
| 267 | { |
| 268 | return p->se.cfs_rq; |
| 269 | } |
| 270 | |
| 271 | /* runqueue on which this entity is (to be) queued */ |
| 272 | static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se) |
| 273 | { |
| 274 | return se->cfs_rq; |
| 275 | } |
| 276 | |
| 277 | /* runqueue "owned" by this group */ |
| 278 | static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp) |
| 279 | { |
| 280 | return grp->my_q; |
| 281 | } |
| 282 | |
Peter Zijlstra | 3d4b47b | 2010-11-15 15:47:01 -0800 | [diff] [blame] | 283 | static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq) |
| 284 | { |
| 285 | if (!cfs_rq->on_list) { |
Vincent Guittot | 9c2791f | 2016-11-08 10:53:43 +0100 | [diff] [blame] | 286 | struct rq *rq = rq_of(cfs_rq); |
| 287 | int cpu = cpu_of(rq); |
Paul Turner | 67e8625 | 2010-11-15 15:47:05 -0800 | [diff] [blame] | 288 | /* |
| 289 | * Ensure we either appear before our parent (if already |
| 290 | * enqueued) or force our parent to appear after us when it is |
Vincent Guittot | 9c2791f | 2016-11-08 10:53:43 +0100 | [diff] [blame] | 291 | * enqueued. The fact that we always enqueue bottom-up |
| 292 | * reduces this to two cases and a special case for the root |
| 293 | * cfs_rq. Furthermore, it also means that we will always reset |
| 294 | * tmp_alone_branch either when the branch is connected |
| 295 | * to a tree or when we reach the beg of the tree |
Paul Turner | 67e8625 | 2010-11-15 15:47:05 -0800 | [diff] [blame] | 296 | */ |
| 297 | if (cfs_rq->tg->parent && |
Vincent Guittot | 9c2791f | 2016-11-08 10:53:43 +0100 | [diff] [blame] | 298 | cfs_rq->tg->parent->cfs_rq[cpu]->on_list) { |
| 299 | /* |
| 300 | * If parent is already on the list, we add the child |
| 301 | * just before. Thanks to circular linked property of |
| 302 | * the list, this means to put the child at the tail |
| 303 | * of the list that starts by parent. |
| 304 | */ |
Paul Turner | 67e8625 | 2010-11-15 15:47:05 -0800 | [diff] [blame] | 305 | list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list, |
Vincent Guittot | 9c2791f | 2016-11-08 10:53:43 +0100 | [diff] [blame] | 306 | &(cfs_rq->tg->parent->cfs_rq[cpu]->leaf_cfs_rq_list)); |
| 307 | /* |
| 308 | * The branch is now connected to its tree so we can |
| 309 | * reset tmp_alone_branch to the beginning of the |
| 310 | * list. |
| 311 | */ |
| 312 | rq->tmp_alone_branch = &rq->leaf_cfs_rq_list; |
| 313 | } else if (!cfs_rq->tg->parent) { |
| 314 | /* |
| 315 | * cfs rq without parent should be put |
| 316 | * at the tail of the list. |
| 317 | */ |
| 318 | list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list, |
| 319 | &rq->leaf_cfs_rq_list); |
| 320 | /* |
| 321 | * We have reach the beg of a tree so we can reset |
| 322 | * tmp_alone_branch to the beginning of the list. |
| 323 | */ |
| 324 | rq->tmp_alone_branch = &rq->leaf_cfs_rq_list; |
| 325 | } else { |
| 326 | /* |
| 327 | * The parent has not already been added so we want to |
| 328 | * make sure that it will be put after us. |
| 329 | * tmp_alone_branch points to the beg of the branch |
| 330 | * where we will add parent. |
| 331 | */ |
| 332 | list_add_rcu(&cfs_rq->leaf_cfs_rq_list, |
| 333 | rq->tmp_alone_branch); |
| 334 | /* |
| 335 | * update tmp_alone_branch to points to the new beg |
| 336 | * of the branch |
| 337 | */ |
| 338 | rq->tmp_alone_branch = &cfs_rq->leaf_cfs_rq_list; |
Paul Turner | 67e8625 | 2010-11-15 15:47:05 -0800 | [diff] [blame] | 339 | } |
Peter Zijlstra | 3d4b47b | 2010-11-15 15:47:01 -0800 | [diff] [blame] | 340 | |
| 341 | cfs_rq->on_list = 1; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq) |
| 346 | { |
| 347 | if (cfs_rq->on_list) { |
| 348 | list_del_rcu(&cfs_rq->leaf_cfs_rq_list); |
| 349 | cfs_rq->on_list = 0; |
| 350 | } |
| 351 | } |
| 352 | |
Peter Zijlstra | b758149 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 353 | /* Iterate thr' all leaf cfs_rq's on a runqueue */ |
| 354 | #define for_each_leaf_cfs_rq(rq, cfs_rq) \ |
| 355 | list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list) |
| 356 | |
| 357 | /* Do the two (enqueued) entities belong to the same group ? */ |
Peter Zijlstra | fed14d4 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 358 | static inline struct cfs_rq * |
Peter Zijlstra | b758149 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 359 | is_same_group(struct sched_entity *se, struct sched_entity *pse) |
| 360 | { |
| 361 | if (se->cfs_rq == pse->cfs_rq) |
Peter Zijlstra | fed14d4 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 362 | return se->cfs_rq; |
Peter Zijlstra | b758149 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 363 | |
Peter Zijlstra | fed14d4 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 364 | return NULL; |
Peter Zijlstra | b758149 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | static inline struct sched_entity *parent_entity(struct sched_entity *se) |
| 368 | { |
| 369 | return se->parent; |
| 370 | } |
| 371 | |
Peter Zijlstra | 464b752 | 2008-10-24 11:06:15 +0200 | [diff] [blame] | 372 | static void |
| 373 | find_matching_se(struct sched_entity **se, struct sched_entity **pse) |
| 374 | { |
| 375 | int se_depth, pse_depth; |
| 376 | |
| 377 | /* |
| 378 | * preemption test can be made between sibling entities who are in the |
| 379 | * same cfs_rq i.e who have a common parent. Walk up the hierarchy of |
| 380 | * both tasks until we find their ancestors who are siblings of common |
| 381 | * parent. |
| 382 | */ |
| 383 | |
| 384 | /* First walk up until both entities are at same depth */ |
Peter Zijlstra | fed14d4 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 385 | se_depth = (*se)->depth; |
| 386 | pse_depth = (*pse)->depth; |
Peter Zijlstra | 464b752 | 2008-10-24 11:06:15 +0200 | [diff] [blame] | 387 | |
| 388 | while (se_depth > pse_depth) { |
| 389 | se_depth--; |
| 390 | *se = parent_entity(*se); |
| 391 | } |
| 392 | |
| 393 | while (pse_depth > se_depth) { |
| 394 | pse_depth--; |
| 395 | *pse = parent_entity(*pse); |
| 396 | } |
| 397 | |
| 398 | while (!is_same_group(*se, *pse)) { |
| 399 | *se = parent_entity(*se); |
| 400 | *pse = parent_entity(*pse); |
| 401 | } |
| 402 | } |
| 403 | |
Peter Zijlstra | 8f48894 | 2009-07-24 12:25:30 +0200 | [diff] [blame] | 404 | #else /* !CONFIG_FAIR_GROUP_SCHED */ |
| 405 | |
| 406 | static inline struct task_struct *task_of(struct sched_entity *se) |
| 407 | { |
| 408 | return container_of(se, struct task_struct, se); |
| 409 | } |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 410 | |
| 411 | static inline struct rq *rq_of(struct cfs_rq *cfs_rq) |
| 412 | { |
| 413 | return container_of(cfs_rq, struct rq, cfs); |
| 414 | } |
| 415 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 416 | #define entity_is_task(se) 1 |
| 417 | |
Peter Zijlstra | b758149 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 418 | #define for_each_sched_entity(se) \ |
| 419 | for (; se; se = NULL) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 420 | |
Peter Zijlstra | b758149 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 421 | static inline struct cfs_rq *task_cfs_rq(struct task_struct *p) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 422 | { |
Peter Zijlstra | b758149 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 423 | return &task_rq(p)->cfs; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 424 | } |
| 425 | |
Peter Zijlstra | b758149 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 426 | static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se) |
| 427 | { |
| 428 | struct task_struct *p = task_of(se); |
| 429 | struct rq *rq = task_rq(p); |
| 430 | |
| 431 | return &rq->cfs; |
| 432 | } |
| 433 | |
| 434 | /* runqueue "owned" by this group */ |
| 435 | static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp) |
| 436 | { |
| 437 | return NULL; |
| 438 | } |
| 439 | |
Peter Zijlstra | 3d4b47b | 2010-11-15 15:47:01 -0800 | [diff] [blame] | 440 | static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq) |
| 441 | { |
| 442 | } |
| 443 | |
| 444 | static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq) |
| 445 | { |
| 446 | } |
| 447 | |
Peter Zijlstra | b758149 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 448 | #define for_each_leaf_cfs_rq(rq, cfs_rq) \ |
| 449 | for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL) |
| 450 | |
Peter Zijlstra | b758149 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 451 | static inline struct sched_entity *parent_entity(struct sched_entity *se) |
| 452 | { |
| 453 | return NULL; |
| 454 | } |
| 455 | |
Peter Zijlstra | 464b752 | 2008-10-24 11:06:15 +0200 | [diff] [blame] | 456 | static inline void |
| 457 | find_matching_se(struct sched_entity **se, struct sched_entity **pse) |
| 458 | { |
| 459 | } |
| 460 | |
Peter Zijlstra | b758149 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 461 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
| 462 | |
Peter Zijlstra | 6c16a6d | 2012-03-21 13:07:16 -0700 | [diff] [blame] | 463 | static __always_inline |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 464 | void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 465 | |
| 466 | /************************************************************** |
| 467 | * Scheduling class tree data structure manipulation methods: |
| 468 | */ |
| 469 | |
Andrei Epure | 1bf0823 | 2013-03-12 21:12:24 +0200 | [diff] [blame] | 470 | static inline u64 max_vruntime(u64 max_vruntime, u64 vruntime) |
Peter Zijlstra | 02e0431 | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 471 | { |
Andrei Epure | 1bf0823 | 2013-03-12 21:12:24 +0200 | [diff] [blame] | 472 | s64 delta = (s64)(vruntime - max_vruntime); |
Peter Zijlstra | 368059a | 2007-10-15 17:00:11 +0200 | [diff] [blame] | 473 | if (delta > 0) |
Andrei Epure | 1bf0823 | 2013-03-12 21:12:24 +0200 | [diff] [blame] | 474 | max_vruntime = vruntime; |
Peter Zijlstra | 02e0431 | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 475 | |
Andrei Epure | 1bf0823 | 2013-03-12 21:12:24 +0200 | [diff] [blame] | 476 | return max_vruntime; |
Peter Zijlstra | 02e0431 | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 477 | } |
| 478 | |
Ingo Molnar | 0702e3e | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 479 | static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime) |
Peter Zijlstra | b0ffd24 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 480 | { |
| 481 | s64 delta = (s64)(vruntime - min_vruntime); |
| 482 | if (delta < 0) |
| 483 | min_vruntime = vruntime; |
| 484 | |
| 485 | return min_vruntime; |
| 486 | } |
| 487 | |
Fabio Checconi | 54fdc58 | 2009-07-16 12:32:27 +0200 | [diff] [blame] | 488 | static inline int entity_before(struct sched_entity *a, |
| 489 | struct sched_entity *b) |
| 490 | { |
| 491 | return (s64)(a->vruntime - b->vruntime) < 0; |
| 492 | } |
| 493 | |
Peter Zijlstra | 1af5f73 | 2008-10-24 11:06:13 +0200 | [diff] [blame] | 494 | static void update_min_vruntime(struct cfs_rq *cfs_rq) |
| 495 | { |
Peter Zijlstra | b60205c | 2016-09-20 21:58:12 +0200 | [diff] [blame] | 496 | struct sched_entity *curr = cfs_rq->curr; |
| 497 | |
Peter Zijlstra | 1af5f73 | 2008-10-24 11:06:13 +0200 | [diff] [blame] | 498 | u64 vruntime = cfs_rq->min_vruntime; |
| 499 | |
Peter Zijlstra | b60205c | 2016-09-20 21:58:12 +0200 | [diff] [blame] | 500 | if (curr) { |
| 501 | if (curr->on_rq) |
| 502 | vruntime = curr->vruntime; |
| 503 | else |
| 504 | curr = NULL; |
| 505 | } |
Peter Zijlstra | 1af5f73 | 2008-10-24 11:06:13 +0200 | [diff] [blame] | 506 | |
| 507 | if (cfs_rq->rb_leftmost) { |
| 508 | struct sched_entity *se = rb_entry(cfs_rq->rb_leftmost, |
| 509 | struct sched_entity, |
| 510 | run_node); |
| 511 | |
Peter Zijlstra | b60205c | 2016-09-20 21:58:12 +0200 | [diff] [blame] | 512 | if (!curr) |
Peter Zijlstra | 1af5f73 | 2008-10-24 11:06:13 +0200 | [diff] [blame] | 513 | vruntime = se->vruntime; |
| 514 | else |
| 515 | vruntime = min_vruntime(vruntime, se->vruntime); |
| 516 | } |
| 517 | |
Andrei Epure | 1bf0823 | 2013-03-12 21:12:24 +0200 | [diff] [blame] | 518 | /* ensure we never gain time by being placed backwards. */ |
Peter Zijlstra | 1af5f73 | 2008-10-24 11:06:13 +0200 | [diff] [blame] | 519 | cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime); |
Peter Zijlstra | 3fe1698 | 2011-04-05 17:23:48 +0200 | [diff] [blame] | 520 | #ifndef CONFIG_64BIT |
| 521 | smp_wmb(); |
| 522 | cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime; |
| 523 | #endif |
Peter Zijlstra | 1af5f73 | 2008-10-24 11:06:13 +0200 | [diff] [blame] | 524 | } |
| 525 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 526 | /* |
| 527 | * Enqueue an entity into the rb-tree: |
| 528 | */ |
Ingo Molnar | 0702e3e | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 529 | static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 530 | { |
| 531 | struct rb_node **link = &cfs_rq->tasks_timeline.rb_node; |
| 532 | struct rb_node *parent = NULL; |
| 533 | struct sched_entity *entry; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 534 | int leftmost = 1; |
| 535 | |
| 536 | /* |
| 537 | * Find the right place in the rbtree: |
| 538 | */ |
| 539 | while (*link) { |
| 540 | parent = *link; |
| 541 | entry = rb_entry(parent, struct sched_entity, run_node); |
| 542 | /* |
| 543 | * We dont care about collisions. Nodes with |
| 544 | * the same key stay together. |
| 545 | */ |
Stephan Baerwolf | 2bd2d6f | 2011-07-20 14:46:59 +0200 | [diff] [blame] | 546 | if (entity_before(se, entry)) { |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 547 | link = &parent->rb_left; |
| 548 | } else { |
| 549 | link = &parent->rb_right; |
| 550 | leftmost = 0; |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | /* |
| 555 | * Maintain a cache of leftmost tree entries (it is frequently |
| 556 | * used): |
| 557 | */ |
Peter Zijlstra | 1af5f73 | 2008-10-24 11:06:13 +0200 | [diff] [blame] | 558 | if (leftmost) |
Ingo Molnar | 57cb499 | 2007-10-15 17:00:11 +0200 | [diff] [blame] | 559 | cfs_rq->rb_leftmost = &se->run_node; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 560 | |
| 561 | rb_link_node(&se->run_node, parent, link); |
| 562 | rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 563 | } |
| 564 | |
Ingo Molnar | 0702e3e | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 565 | static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 566 | { |
Peter Zijlstra | 3fe6974 | 2008-03-14 20:55:51 +0100 | [diff] [blame] | 567 | if (cfs_rq->rb_leftmost == &se->run_node) { |
| 568 | struct rb_node *next_node; |
Peter Zijlstra | 3fe6974 | 2008-03-14 20:55:51 +0100 | [diff] [blame] | 569 | |
| 570 | next_node = rb_next(&se->run_node); |
| 571 | cfs_rq->rb_leftmost = next_node; |
Peter Zijlstra | 3fe6974 | 2008-03-14 20:55:51 +0100 | [diff] [blame] | 572 | } |
Ingo Molnar | e9acbff | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 573 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 574 | rb_erase(&se->run_node, &cfs_rq->tasks_timeline); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 575 | } |
| 576 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 577 | struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 578 | { |
Peter Zijlstra | f4b6755 | 2008-11-04 21:25:07 +0100 | [diff] [blame] | 579 | struct rb_node *left = cfs_rq->rb_leftmost; |
| 580 | |
| 581 | if (!left) |
| 582 | return NULL; |
| 583 | |
| 584 | return rb_entry(left, struct sched_entity, run_node); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 585 | } |
| 586 | |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 587 | static struct sched_entity *__pick_next_entity(struct sched_entity *se) |
| 588 | { |
| 589 | struct rb_node *next = rb_next(&se->run_node); |
| 590 | |
| 591 | if (!next) |
| 592 | return NULL; |
| 593 | |
| 594 | return rb_entry(next, struct sched_entity, run_node); |
| 595 | } |
| 596 | |
| 597 | #ifdef CONFIG_SCHED_DEBUG |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 598 | struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq) |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 599 | { |
Ingo Molnar | 7eee3e6 | 2008-02-22 10:32:21 +0100 | [diff] [blame] | 600 | struct rb_node *last = rb_last(&cfs_rq->tasks_timeline); |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 601 | |
Balbir Singh | 70eee74 | 2008-02-22 13:25:53 +0530 | [diff] [blame] | 602 | if (!last) |
| 603 | return NULL; |
Ingo Molnar | 7eee3e6 | 2008-02-22 10:32:21 +0100 | [diff] [blame] | 604 | |
| 605 | return rb_entry(last, struct sched_entity, run_node); |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 606 | } |
| 607 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 608 | /************************************************************** |
| 609 | * Scheduling class statistics methods: |
| 610 | */ |
| 611 | |
Christian Ehrhardt | acb4a84 | 2009-11-30 12:16:48 +0100 | [diff] [blame] | 612 | int sched_proc_update_handler(struct ctl_table *table, int write, |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 613 | void __user *buffer, size_t *lenp, |
Peter Zijlstra | b2be5e9 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 614 | loff_t *ppos) |
| 615 | { |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 616 | int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
Nicholas Mc Guire | 58ac93e | 2015-05-15 21:05:42 +0200 | [diff] [blame] | 617 | unsigned int factor = get_update_sysctl_factor(); |
Peter Zijlstra | b2be5e9 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 618 | |
| 619 | if (ret || !write) |
| 620 | return ret; |
| 621 | |
| 622 | sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency, |
| 623 | sysctl_sched_min_granularity); |
| 624 | |
Christian Ehrhardt | acb4a84 | 2009-11-30 12:16:48 +0100 | [diff] [blame] | 625 | #define WRT_SYSCTL(name) \ |
| 626 | (normalized_sysctl_##name = sysctl_##name / (factor)) |
| 627 | WRT_SYSCTL(sched_min_granularity); |
| 628 | WRT_SYSCTL(sched_latency); |
| 629 | WRT_SYSCTL(sched_wakeup_granularity); |
Christian Ehrhardt | acb4a84 | 2009-11-30 12:16:48 +0100 | [diff] [blame] | 630 | #undef WRT_SYSCTL |
| 631 | |
Peter Zijlstra | b2be5e9 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 632 | return 0; |
| 633 | } |
| 634 | #endif |
Ingo Molnar | 647e7ca | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 635 | |
| 636 | /* |
Peter Zijlstra | f9c0b09 | 2008-10-17 19:27:04 +0200 | [diff] [blame] | 637 | * delta /= w |
Peter Zijlstra | a7be37a | 2008-06-27 13:41:11 +0200 | [diff] [blame] | 638 | */ |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 639 | static inline u64 calc_delta_fair(u64 delta, struct sched_entity *se) |
Peter Zijlstra | a7be37a | 2008-06-27 13:41:11 +0200 | [diff] [blame] | 640 | { |
Peter Zijlstra | f9c0b09 | 2008-10-17 19:27:04 +0200 | [diff] [blame] | 641 | if (unlikely(se->load.weight != NICE_0_LOAD)) |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 642 | delta = __calc_delta(delta, NICE_0_LOAD, &se->load); |
Peter Zijlstra | a7be37a | 2008-06-27 13:41:11 +0200 | [diff] [blame] | 643 | |
| 644 | return delta; |
| 645 | } |
| 646 | |
| 647 | /* |
Ingo Molnar | 647e7ca | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 648 | * The idea is to set a period in which each task runs once. |
| 649 | * |
Borislav Petkov | 532b185 | 2012-08-08 16:16:04 +0200 | [diff] [blame] | 650 | * When there are too many tasks (sched_nr_latency) we have to stretch |
Ingo Molnar | 647e7ca | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 651 | * this period because otherwise the slices get too small. |
| 652 | * |
| 653 | * p = (nr <= nl) ? l : l*nr/nl |
| 654 | */ |
Peter Zijlstra | 4d78e7b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 655 | static u64 __sched_period(unsigned long nr_running) |
| 656 | { |
Boqun Feng | 8e2b0bf | 2015-07-02 22:25:52 +0800 | [diff] [blame] | 657 | if (unlikely(nr_running > sched_nr_latency)) |
| 658 | return nr_running * sysctl_sched_min_granularity; |
| 659 | else |
| 660 | return sysctl_sched_latency; |
Peter Zijlstra | 4d78e7b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 661 | } |
| 662 | |
Ingo Molnar | 647e7ca | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 663 | /* |
| 664 | * We calculate the wall-time slice from the period by taking a part |
| 665 | * proportional to the weight. |
| 666 | * |
Peter Zijlstra | f9c0b09 | 2008-10-17 19:27:04 +0200 | [diff] [blame] | 667 | * s = p*P[w/rw] |
Ingo Molnar | 647e7ca | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 668 | */ |
Peter Zijlstra | 6d0f0eb | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 669 | static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se) |
Peter Zijlstra | 2180508 | 2007-08-25 18:41:53 +0200 | [diff] [blame] | 670 | { |
Mike Galbraith | 0a58244 | 2009-01-02 12:16:42 +0100 | [diff] [blame] | 671 | u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq); |
Peter Zijlstra | f9c0b09 | 2008-10-17 19:27:04 +0200 | [diff] [blame] | 672 | |
Mike Galbraith | 0a58244 | 2009-01-02 12:16:42 +0100 | [diff] [blame] | 673 | for_each_sched_entity(se) { |
Lin Ming | 6272d68 | 2009-01-15 17:17:15 +0100 | [diff] [blame] | 674 | struct load_weight *load; |
Christian Engelmayer | 3104bf0 | 2009-06-16 10:35:12 +0200 | [diff] [blame] | 675 | struct load_weight lw; |
Lin Ming | 6272d68 | 2009-01-15 17:17:15 +0100 | [diff] [blame] | 676 | |
| 677 | cfs_rq = cfs_rq_of(se); |
| 678 | load = &cfs_rq->load; |
Peter Zijlstra | f9c0b09 | 2008-10-17 19:27:04 +0200 | [diff] [blame] | 679 | |
Mike Galbraith | 0a58244 | 2009-01-02 12:16:42 +0100 | [diff] [blame] | 680 | if (unlikely(!se->on_rq)) { |
Christian Engelmayer | 3104bf0 | 2009-06-16 10:35:12 +0200 | [diff] [blame] | 681 | lw = cfs_rq->load; |
Mike Galbraith | 0a58244 | 2009-01-02 12:16:42 +0100 | [diff] [blame] | 682 | |
| 683 | update_load_add(&lw, se->load.weight); |
| 684 | load = &lw; |
| 685 | } |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 686 | slice = __calc_delta(slice, se->load.weight, load); |
Mike Galbraith | 0a58244 | 2009-01-02 12:16:42 +0100 | [diff] [blame] | 687 | } |
| 688 | return slice; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 689 | } |
| 690 | |
Ingo Molnar | 647e7ca | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 691 | /* |
Andrei Epure | 660cc00 | 2013-03-11 12:03:20 +0200 | [diff] [blame] | 692 | * We calculate the vruntime slice of a to-be-inserted task. |
Ingo Molnar | 647e7ca | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 693 | * |
Peter Zijlstra | f9c0b09 | 2008-10-17 19:27:04 +0200 | [diff] [blame] | 694 | * vs = s/w |
Ingo Molnar | 647e7ca | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 695 | */ |
Peter Zijlstra | f9c0b09 | 2008-10-17 19:27:04 +0200 | [diff] [blame] | 696 | static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se) |
Ingo Molnar | 647e7ca | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 697 | { |
Peter Zijlstra | f9c0b09 | 2008-10-17 19:27:04 +0200 | [diff] [blame] | 698 | return calc_delta_fair(sched_slice(cfs_rq, se), se); |
Peter Zijlstra | a7be37a | 2008-06-27 13:41:11 +0200 | [diff] [blame] | 699 | } |
| 700 | |
Alex Shi | a75cdaa | 2013-06-20 10:18:47 +0800 | [diff] [blame] | 701 | #ifdef CONFIG_SMP |
Morten Rasmussen | 772bd008c | 2016-06-22 18:03:13 +0100 | [diff] [blame] | 702 | static int select_idle_sibling(struct task_struct *p, int prev_cpu, int cpu); |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 703 | static unsigned long task_h_load(struct task_struct *p); |
| 704 | |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 705 | /* |
| 706 | * We choose a half-life close to 1 scheduling period. |
Leo Yan | 84fb5a1 | 2015-09-15 18:57:37 +0800 | [diff] [blame] | 707 | * Note: The tables runnable_avg_yN_inv and runnable_avg_yN_sum are |
| 708 | * dependent on this value. |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 709 | */ |
| 710 | #define LOAD_AVG_PERIOD 32 |
| 711 | #define LOAD_AVG_MAX 47742 /* maximum possible load avg */ |
Leo Yan | 84fb5a1 | 2015-09-15 18:57:37 +0800 | [diff] [blame] | 712 | #define LOAD_AVG_MAX_N 345 /* number of full periods to produce LOAD_AVG_MAX */ |
Alex Shi | a75cdaa | 2013-06-20 10:18:47 +0800 | [diff] [blame] | 713 | |
Yuyang Du | 540247f | 2015-07-15 08:04:39 +0800 | [diff] [blame] | 714 | /* Give new sched_entity start runnable values to heavy its load in infant time */ |
| 715 | void init_entity_runnable_average(struct sched_entity *se) |
Alex Shi | a75cdaa | 2013-06-20 10:18:47 +0800 | [diff] [blame] | 716 | { |
Yuyang Du | 540247f | 2015-07-15 08:04:39 +0800 | [diff] [blame] | 717 | struct sched_avg *sa = &se->avg; |
Alex Shi | a75cdaa | 2013-06-20 10:18:47 +0800 | [diff] [blame] | 718 | |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 719 | sa->last_update_time = 0; |
| 720 | /* |
| 721 | * sched_avg's period_contrib should be strictly less then 1024, so |
| 722 | * we give it 1023 to make sure it is almost a period (1024us), and |
| 723 | * will definitely be update (after enqueue). |
| 724 | */ |
| 725 | sa->period_contrib = 1023; |
Vincent Guittot | b5a9b34 | 2016-10-19 14:45:23 +0200 | [diff] [blame] | 726 | /* |
| 727 | * Tasks are intialized with full load to be seen as heavy tasks until |
| 728 | * they get a chance to stabilize to their real load level. |
| 729 | * Group entities are intialized with zero load to reflect the fact that |
| 730 | * nothing has been attached to the task group yet. |
| 731 | */ |
| 732 | if (entity_is_task(se)) |
| 733 | sa->load_avg = scale_load_down(se->load.weight); |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 734 | sa->load_sum = sa->load_avg * LOAD_AVG_MAX; |
Yuyang Du | 2b8c41d | 2016-03-30 04:30:56 +0800 | [diff] [blame] | 735 | /* |
| 736 | * At this point, util_avg won't be used in select_task_rq_fair anyway |
| 737 | */ |
| 738 | sa->util_avg = 0; |
| 739 | sa->util_sum = 0; |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 740 | /* when this task enqueue'ed, it will contribute to its cfs_rq's load_avg */ |
Alex Shi | a75cdaa | 2013-06-20 10:18:47 +0800 | [diff] [blame] | 741 | } |
Yuyang Du | 7ea241a | 2015-07-15 08:04:42 +0800 | [diff] [blame] | 742 | |
Peter Zijlstra | 7dc603c | 2016-06-16 13:29:28 +0200 | [diff] [blame] | 743 | static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq); |
Vincent Guittot | df21791 | 2016-11-08 10:53:42 +0100 | [diff] [blame] | 744 | static void attach_entity_cfs_rq(struct sched_entity *se); |
Peter Zijlstra | 7dc603c | 2016-06-16 13:29:28 +0200 | [diff] [blame] | 745 | |
Yuyang Du | 2b8c41d | 2016-03-30 04:30:56 +0800 | [diff] [blame] | 746 | /* |
| 747 | * With new tasks being created, their initial util_avgs are extrapolated |
| 748 | * based on the cfs_rq's current util_avg: |
| 749 | * |
| 750 | * util_avg = cfs_rq->util_avg / (cfs_rq->load_avg + 1) * se.load.weight |
| 751 | * |
| 752 | * However, in many cases, the above util_avg does not give a desired |
| 753 | * value. Moreover, the sum of the util_avgs may be divergent, such |
| 754 | * as when the series is a harmonic series. |
| 755 | * |
| 756 | * To solve this problem, we also cap the util_avg of successive tasks to |
| 757 | * only 1/2 of the left utilization budget: |
| 758 | * |
| 759 | * util_avg_cap = (1024 - cfs_rq->avg.util_avg) / 2^n |
| 760 | * |
| 761 | * where n denotes the nth task. |
| 762 | * |
| 763 | * For example, a simplest series from the beginning would be like: |
| 764 | * |
| 765 | * task util_avg: 512, 256, 128, 64, 32, 16, 8, ... |
| 766 | * cfs_rq util_avg: 512, 768, 896, 960, 992, 1008, 1016, ... |
| 767 | * |
| 768 | * Finally, that extrapolated util_avg is clamped to the cap (util_avg_cap) |
| 769 | * if util_avg > util_avg_cap. |
| 770 | */ |
| 771 | void post_init_entity_util_avg(struct sched_entity *se) |
| 772 | { |
| 773 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 774 | struct sched_avg *sa = &se->avg; |
Yuyang Du | 172895e | 2016-04-05 12:12:27 +0800 | [diff] [blame] | 775 | long cap = (long)(SCHED_CAPACITY_SCALE - cfs_rq->avg.util_avg) / 2; |
Yuyang Du | 2b8c41d | 2016-03-30 04:30:56 +0800 | [diff] [blame] | 776 | |
| 777 | if (cap > 0) { |
| 778 | if (cfs_rq->avg.util_avg != 0) { |
| 779 | sa->util_avg = cfs_rq->avg.util_avg * se->load.weight; |
| 780 | sa->util_avg /= (cfs_rq->avg.load_avg + 1); |
| 781 | |
| 782 | if (sa->util_avg > cap) |
| 783 | sa->util_avg = cap; |
| 784 | } else { |
| 785 | sa->util_avg = cap; |
| 786 | } |
| 787 | sa->util_sum = sa->util_avg * LOAD_AVG_MAX; |
| 788 | } |
Peter Zijlstra | 7dc603c | 2016-06-16 13:29:28 +0200 | [diff] [blame] | 789 | |
| 790 | if (entity_is_task(se)) { |
| 791 | struct task_struct *p = task_of(se); |
| 792 | if (p->sched_class != &fair_sched_class) { |
| 793 | /* |
| 794 | * For !fair tasks do: |
| 795 | * |
| 796 | update_cfs_rq_load_avg(now, cfs_rq, false); |
| 797 | attach_entity_load_avg(cfs_rq, se); |
| 798 | switched_from_fair(rq, p); |
| 799 | * |
| 800 | * such that the next switched_to_fair() has the |
| 801 | * expected state. |
| 802 | */ |
Vincent Guittot | df21791 | 2016-11-08 10:53:42 +0100 | [diff] [blame] | 803 | se->avg.last_update_time = cfs_rq_clock_task(cfs_rq); |
Peter Zijlstra | 7dc603c | 2016-06-16 13:29:28 +0200 | [diff] [blame] | 804 | return; |
| 805 | } |
| 806 | } |
| 807 | |
Vincent Guittot | df21791 | 2016-11-08 10:53:42 +0100 | [diff] [blame] | 808 | attach_entity_cfs_rq(se); |
Yuyang Du | 2b8c41d | 2016-03-30 04:30:56 +0800 | [diff] [blame] | 809 | } |
| 810 | |
Peter Zijlstra | 7dc603c | 2016-06-16 13:29:28 +0200 | [diff] [blame] | 811 | #else /* !CONFIG_SMP */ |
Yuyang Du | 540247f | 2015-07-15 08:04:39 +0800 | [diff] [blame] | 812 | void init_entity_runnable_average(struct sched_entity *se) |
Alex Shi | a75cdaa | 2013-06-20 10:18:47 +0800 | [diff] [blame] | 813 | { |
| 814 | } |
Yuyang Du | 2b8c41d | 2016-03-30 04:30:56 +0800 | [diff] [blame] | 815 | void post_init_entity_util_avg(struct sched_entity *se) |
| 816 | { |
| 817 | } |
Peter Zijlstra | 3d30544f | 2016-06-21 14:27:50 +0200 | [diff] [blame] | 818 | static void update_tg_load_avg(struct cfs_rq *cfs_rq, int force) |
| 819 | { |
| 820 | } |
Peter Zijlstra | 7dc603c | 2016-06-16 13:29:28 +0200 | [diff] [blame] | 821 | #endif /* CONFIG_SMP */ |
Alex Shi | a75cdaa | 2013-06-20 10:18:47 +0800 | [diff] [blame] | 822 | |
Peter Zijlstra | a7be37a | 2008-06-27 13:41:11 +0200 | [diff] [blame] | 823 | /* |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 824 | * Update the current task's runtime statistics. |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 825 | */ |
Ingo Molnar | b7cc089 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 826 | static void update_curr(struct cfs_rq *cfs_rq) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 827 | { |
Ingo Molnar | 429d43b | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 828 | struct sched_entity *curr = cfs_rq->curr; |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 829 | u64 now = rq_clock_task(rq_of(cfs_rq)); |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 830 | u64 delta_exec; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 831 | |
| 832 | if (unlikely(!curr)) |
| 833 | return; |
| 834 | |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 835 | delta_exec = now - curr->exec_start; |
| 836 | if (unlikely((s64)delta_exec <= 0)) |
Peter Zijlstra | 34f28ec | 2008-12-16 08:45:31 +0100 | [diff] [blame] | 837 | return; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 838 | |
Ingo Molnar | 8ebc91d | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 839 | curr->exec_start = now; |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 840 | |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 841 | schedstat_set(curr->statistics.exec_max, |
| 842 | max(delta_exec, curr->statistics.exec_max)); |
| 843 | |
| 844 | curr->sum_exec_runtime += delta_exec; |
Josh Poimboeuf | ae92882 | 2016-06-17 12:43:24 -0500 | [diff] [blame] | 845 | schedstat_add(cfs_rq->exec_clock, delta_exec); |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 846 | |
| 847 | curr->vruntime += calc_delta_fair(delta_exec, curr); |
| 848 | update_min_vruntime(cfs_rq); |
| 849 | |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 850 | if (entity_is_task(curr)) { |
| 851 | struct task_struct *curtask = task_of(curr); |
| 852 | |
Ingo Molnar | f977bb4 | 2009-09-13 18:15:54 +0200 | [diff] [blame] | 853 | trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime); |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 854 | cpuacct_charge(curtask, delta_exec); |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 855 | account_group_exec_runtime(curtask, delta_exec); |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 856 | } |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 857 | |
| 858 | account_cfs_rq_runtime(cfs_rq, delta_exec); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 859 | } |
| 860 | |
Stanislaw Gruszka | 6e99891 | 2014-11-12 16:58:44 +0100 | [diff] [blame] | 861 | static void update_curr_fair(struct rq *rq) |
| 862 | { |
| 863 | update_curr(cfs_rq_of(&rq->curr->se)); |
| 864 | } |
| 865 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 866 | static inline void |
Ingo Molnar | 5870db5 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 867 | update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 868 | { |
Josh Poimboeuf | 4fa8d29 | 2016-06-17 12:43:26 -0500 | [diff] [blame] | 869 | u64 wait_start, prev_wait_start; |
| 870 | |
| 871 | if (!schedstat_enabled()) |
| 872 | return; |
| 873 | |
| 874 | wait_start = rq_clock(rq_of(cfs_rq)); |
| 875 | prev_wait_start = schedstat_val(se->statistics.wait_start); |
Joonwoo Park | 3ea94de | 2015-11-12 19:38:54 -0800 | [diff] [blame] | 876 | |
| 877 | if (entity_is_task(se) && task_on_rq_migrating(task_of(se)) && |
Josh Poimboeuf | 4fa8d29 | 2016-06-17 12:43:26 -0500 | [diff] [blame] | 878 | likely(wait_start > prev_wait_start)) |
| 879 | wait_start -= prev_wait_start; |
Joonwoo Park | 3ea94de | 2015-11-12 19:38:54 -0800 | [diff] [blame] | 880 | |
Josh Poimboeuf | 4fa8d29 | 2016-06-17 12:43:26 -0500 | [diff] [blame] | 881 | schedstat_set(se->statistics.wait_start, wait_start); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 882 | } |
| 883 | |
Josh Poimboeuf | 4fa8d29 | 2016-06-17 12:43:26 -0500 | [diff] [blame] | 884 | static inline void |
Joonwoo Park | 3ea94de | 2015-11-12 19:38:54 -0800 | [diff] [blame] | 885 | update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| 886 | { |
| 887 | struct task_struct *p; |
Mel Gorman | cb25176 | 2016-02-05 09:08:36 +0000 | [diff] [blame] | 888 | u64 delta; |
| 889 | |
Josh Poimboeuf | 4fa8d29 | 2016-06-17 12:43:26 -0500 | [diff] [blame] | 890 | if (!schedstat_enabled()) |
| 891 | return; |
| 892 | |
| 893 | delta = rq_clock(rq_of(cfs_rq)) - schedstat_val(se->statistics.wait_start); |
Joonwoo Park | 3ea94de | 2015-11-12 19:38:54 -0800 | [diff] [blame] | 894 | |
| 895 | if (entity_is_task(se)) { |
| 896 | p = task_of(se); |
| 897 | if (task_on_rq_migrating(p)) { |
| 898 | /* |
| 899 | * Preserve migrating task's wait time so wait_start |
| 900 | * time stamp can be adjusted to accumulate wait time |
| 901 | * prior to migration. |
| 902 | */ |
Josh Poimboeuf | 4fa8d29 | 2016-06-17 12:43:26 -0500 | [diff] [blame] | 903 | schedstat_set(se->statistics.wait_start, delta); |
Joonwoo Park | 3ea94de | 2015-11-12 19:38:54 -0800 | [diff] [blame] | 904 | return; |
| 905 | } |
| 906 | trace_sched_stat_wait(p, delta); |
| 907 | } |
| 908 | |
Josh Poimboeuf | 4fa8d29 | 2016-06-17 12:43:26 -0500 | [diff] [blame] | 909 | schedstat_set(se->statistics.wait_max, |
| 910 | max(schedstat_val(se->statistics.wait_max), delta)); |
| 911 | schedstat_inc(se->statistics.wait_count); |
| 912 | schedstat_add(se->statistics.wait_sum, delta); |
| 913 | schedstat_set(se->statistics.wait_start, 0); |
Joonwoo Park | 3ea94de | 2015-11-12 19:38:54 -0800 | [diff] [blame] | 914 | } |
Joonwoo Park | 3ea94de | 2015-11-12 19:38:54 -0800 | [diff] [blame] | 915 | |
Josh Poimboeuf | 4fa8d29 | 2016-06-17 12:43:26 -0500 | [diff] [blame] | 916 | static inline void |
Josh Poimboeuf | 1a3d027 | 2016-06-17 12:43:23 -0500 | [diff] [blame] | 917 | update_stats_enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| 918 | { |
| 919 | struct task_struct *tsk = NULL; |
Josh Poimboeuf | 4fa8d29 | 2016-06-17 12:43:26 -0500 | [diff] [blame] | 920 | u64 sleep_start, block_start; |
| 921 | |
| 922 | if (!schedstat_enabled()) |
| 923 | return; |
| 924 | |
| 925 | sleep_start = schedstat_val(se->statistics.sleep_start); |
| 926 | block_start = schedstat_val(se->statistics.block_start); |
Josh Poimboeuf | 1a3d027 | 2016-06-17 12:43:23 -0500 | [diff] [blame] | 927 | |
| 928 | if (entity_is_task(se)) |
| 929 | tsk = task_of(se); |
| 930 | |
Josh Poimboeuf | 4fa8d29 | 2016-06-17 12:43:26 -0500 | [diff] [blame] | 931 | if (sleep_start) { |
| 932 | u64 delta = rq_clock(rq_of(cfs_rq)) - sleep_start; |
Josh Poimboeuf | 1a3d027 | 2016-06-17 12:43:23 -0500 | [diff] [blame] | 933 | |
| 934 | if ((s64)delta < 0) |
| 935 | delta = 0; |
| 936 | |
Josh Poimboeuf | 4fa8d29 | 2016-06-17 12:43:26 -0500 | [diff] [blame] | 937 | if (unlikely(delta > schedstat_val(se->statistics.sleep_max))) |
| 938 | schedstat_set(se->statistics.sleep_max, delta); |
Josh Poimboeuf | 1a3d027 | 2016-06-17 12:43:23 -0500 | [diff] [blame] | 939 | |
Josh Poimboeuf | 4fa8d29 | 2016-06-17 12:43:26 -0500 | [diff] [blame] | 940 | schedstat_set(se->statistics.sleep_start, 0); |
| 941 | schedstat_add(se->statistics.sum_sleep_runtime, delta); |
Josh Poimboeuf | 1a3d027 | 2016-06-17 12:43:23 -0500 | [diff] [blame] | 942 | |
| 943 | if (tsk) { |
| 944 | account_scheduler_latency(tsk, delta >> 10, 1); |
| 945 | trace_sched_stat_sleep(tsk, delta); |
| 946 | } |
| 947 | } |
Josh Poimboeuf | 4fa8d29 | 2016-06-17 12:43:26 -0500 | [diff] [blame] | 948 | if (block_start) { |
| 949 | u64 delta = rq_clock(rq_of(cfs_rq)) - block_start; |
Josh Poimboeuf | 1a3d027 | 2016-06-17 12:43:23 -0500 | [diff] [blame] | 950 | |
| 951 | if ((s64)delta < 0) |
| 952 | delta = 0; |
| 953 | |
Josh Poimboeuf | 4fa8d29 | 2016-06-17 12:43:26 -0500 | [diff] [blame] | 954 | if (unlikely(delta > schedstat_val(se->statistics.block_max))) |
| 955 | schedstat_set(se->statistics.block_max, delta); |
Josh Poimboeuf | 1a3d027 | 2016-06-17 12:43:23 -0500 | [diff] [blame] | 956 | |
Josh Poimboeuf | 4fa8d29 | 2016-06-17 12:43:26 -0500 | [diff] [blame] | 957 | schedstat_set(se->statistics.block_start, 0); |
| 958 | schedstat_add(se->statistics.sum_sleep_runtime, delta); |
Josh Poimboeuf | 1a3d027 | 2016-06-17 12:43:23 -0500 | [diff] [blame] | 959 | |
| 960 | if (tsk) { |
| 961 | if (tsk->in_iowait) { |
Josh Poimboeuf | 4fa8d29 | 2016-06-17 12:43:26 -0500 | [diff] [blame] | 962 | schedstat_add(se->statistics.iowait_sum, delta); |
| 963 | schedstat_inc(se->statistics.iowait_count); |
Josh Poimboeuf | 1a3d027 | 2016-06-17 12:43:23 -0500 | [diff] [blame] | 964 | trace_sched_stat_iowait(tsk, delta); |
| 965 | } |
| 966 | |
| 967 | trace_sched_stat_blocked(tsk, delta); |
| 968 | |
| 969 | /* |
| 970 | * Blocking time is in units of nanosecs, so shift by |
| 971 | * 20 to get a milliseconds-range estimation of the |
| 972 | * amount of time that the task spent sleeping: |
| 973 | */ |
| 974 | if (unlikely(prof_on == SLEEP_PROFILING)) { |
| 975 | profile_hits(SLEEP_PROFILING, |
| 976 | (void *)get_wchan(tsk), |
| 977 | delta >> 20); |
| 978 | } |
| 979 | account_scheduler_latency(tsk, delta >> 10, 0); |
| 980 | } |
| 981 | } |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 982 | } |
| 983 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 984 | /* |
| 985 | * Task is being enqueued - update stats: |
| 986 | */ |
Mel Gorman | cb25176 | 2016-02-05 09:08:36 +0000 | [diff] [blame] | 987 | static inline void |
Josh Poimboeuf | 1a3d027 | 2016-06-17 12:43:23 -0500 | [diff] [blame] | 988 | update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 989 | { |
Josh Poimboeuf | 4fa8d29 | 2016-06-17 12:43:26 -0500 | [diff] [blame] | 990 | if (!schedstat_enabled()) |
| 991 | return; |
| 992 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 993 | /* |
| 994 | * Are we enqueueing a waiting task? (for current tasks |
| 995 | * a dequeue/enqueue event is a NOP) |
| 996 | */ |
Ingo Molnar | 429d43b | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 997 | if (se != cfs_rq->curr) |
Ingo Molnar | 5870db5 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 998 | update_stats_wait_start(cfs_rq, se); |
Josh Poimboeuf | 1a3d027 | 2016-06-17 12:43:23 -0500 | [diff] [blame] | 999 | |
| 1000 | if (flags & ENQUEUE_WAKEUP) |
| 1001 | update_stats_enqueue_sleeper(cfs_rq, se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1002 | } |
| 1003 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1004 | static inline void |
Mel Gorman | cb25176 | 2016-02-05 09:08:36 +0000 | [diff] [blame] | 1005 | update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1006 | { |
Josh Poimboeuf | 4fa8d29 | 2016-06-17 12:43:26 -0500 | [diff] [blame] | 1007 | |
| 1008 | if (!schedstat_enabled()) |
| 1009 | return; |
| 1010 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1011 | /* |
| 1012 | * Mark the end of the wait period if dequeueing a |
| 1013 | * waiting task: |
| 1014 | */ |
Ingo Molnar | 429d43b | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 1015 | if (se != cfs_rq->curr) |
Ingo Molnar | 9ef0a96 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 1016 | update_stats_wait_end(cfs_rq, se); |
Mel Gorman | cb25176 | 2016-02-05 09:08:36 +0000 | [diff] [blame] | 1017 | |
Josh Poimboeuf | 4fa8d29 | 2016-06-17 12:43:26 -0500 | [diff] [blame] | 1018 | if ((flags & DEQUEUE_SLEEP) && entity_is_task(se)) { |
| 1019 | struct task_struct *tsk = task_of(se); |
Mel Gorman | cb25176 | 2016-02-05 09:08:36 +0000 | [diff] [blame] | 1020 | |
Josh Poimboeuf | 4fa8d29 | 2016-06-17 12:43:26 -0500 | [diff] [blame] | 1021 | if (tsk->state & TASK_INTERRUPTIBLE) |
| 1022 | schedstat_set(se->statistics.sleep_start, |
| 1023 | rq_clock(rq_of(cfs_rq))); |
| 1024 | if (tsk->state & TASK_UNINTERRUPTIBLE) |
| 1025 | schedstat_set(se->statistics.block_start, |
| 1026 | rq_clock(rq_of(cfs_rq))); |
Mel Gorman | cb25176 | 2016-02-05 09:08:36 +0000 | [diff] [blame] | 1027 | } |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | /* |
| 1031 | * We are picking a new current task - update its stats: |
| 1032 | */ |
| 1033 | static inline void |
Ingo Molnar | 79303e9 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 1034 | update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1035 | { |
| 1036 | /* |
| 1037 | * We are starting a new run period: |
| 1038 | */ |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 1039 | se->exec_start = rq_clock_task(rq_of(cfs_rq)); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1040 | } |
| 1041 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1042 | /************************************************** |
| 1043 | * Scheduling class queueing methods: |
| 1044 | */ |
| 1045 | |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 1046 | #ifdef CONFIG_NUMA_BALANCING |
| 1047 | /* |
Mel Gorman | 598f0ec | 2013-10-07 11:28:55 +0100 | [diff] [blame] | 1048 | * Approximate time to scan a full NUMA task in ms. The task scan period is |
| 1049 | * calculated based on the tasks virtual memory size and |
| 1050 | * numa_balancing_scan_size. |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 1051 | */ |
Mel Gorman | 598f0ec | 2013-10-07 11:28:55 +0100 | [diff] [blame] | 1052 | unsigned int sysctl_numa_balancing_scan_period_min = 1000; |
| 1053 | unsigned int sysctl_numa_balancing_scan_period_max = 60000; |
Peter Zijlstra | 6e5fb22 | 2012-10-25 14:16:45 +0200 | [diff] [blame] | 1054 | |
| 1055 | /* Portion of address space to scan in MB */ |
| 1056 | unsigned int sysctl_numa_balancing_scan_size = 256; |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 1057 | |
Peter Zijlstra | 4b96a29 | 2012-10-25 14:16:47 +0200 | [diff] [blame] | 1058 | /* Scan @scan_size MB every @scan_period after an initial @scan_delay in ms */ |
| 1059 | unsigned int sysctl_numa_balancing_scan_delay = 1000; |
| 1060 | |
Mel Gorman | 598f0ec | 2013-10-07 11:28:55 +0100 | [diff] [blame] | 1061 | static unsigned int task_nr_scan_windows(struct task_struct *p) |
| 1062 | { |
| 1063 | unsigned long rss = 0; |
| 1064 | unsigned long nr_scan_pages; |
| 1065 | |
| 1066 | /* |
| 1067 | * Calculations based on RSS as non-present and empty pages are skipped |
| 1068 | * by the PTE scanner and NUMA hinting faults should be trapped based |
| 1069 | * on resident pages |
| 1070 | */ |
| 1071 | nr_scan_pages = sysctl_numa_balancing_scan_size << (20 - PAGE_SHIFT); |
| 1072 | rss = get_mm_rss(p->mm); |
| 1073 | if (!rss) |
| 1074 | rss = nr_scan_pages; |
| 1075 | |
| 1076 | rss = round_up(rss, nr_scan_pages); |
| 1077 | return rss / nr_scan_pages; |
| 1078 | } |
| 1079 | |
| 1080 | /* For sanitys sake, never scan more PTEs than MAX_SCAN_WINDOW MB/sec. */ |
| 1081 | #define MAX_SCAN_WINDOW 2560 |
| 1082 | |
| 1083 | static unsigned int task_scan_min(struct task_struct *p) |
| 1084 | { |
Jason Low | 316c1608d | 2015-04-28 13:00:20 -0700 | [diff] [blame] | 1085 | unsigned int scan_size = READ_ONCE(sysctl_numa_balancing_scan_size); |
Mel Gorman | 598f0ec | 2013-10-07 11:28:55 +0100 | [diff] [blame] | 1086 | unsigned int scan, floor; |
| 1087 | unsigned int windows = 1; |
| 1088 | |
Kirill Tkhai | 6419265 | 2014-10-16 14:39:37 +0400 | [diff] [blame] | 1089 | if (scan_size < MAX_SCAN_WINDOW) |
| 1090 | windows = MAX_SCAN_WINDOW / scan_size; |
Mel Gorman | 598f0ec | 2013-10-07 11:28:55 +0100 | [diff] [blame] | 1091 | floor = 1000 / windows; |
| 1092 | |
| 1093 | scan = sysctl_numa_balancing_scan_period_min / task_nr_scan_windows(p); |
| 1094 | return max_t(unsigned int, floor, scan); |
| 1095 | } |
| 1096 | |
| 1097 | static unsigned int task_scan_max(struct task_struct *p) |
| 1098 | { |
| 1099 | unsigned int smin = task_scan_min(p); |
| 1100 | unsigned int smax; |
| 1101 | |
| 1102 | /* Watch for min being lower than max due to floor calculations */ |
| 1103 | smax = sysctl_numa_balancing_scan_period_max / task_nr_scan_windows(p); |
| 1104 | return max(smin, smax); |
| 1105 | } |
| 1106 | |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 1107 | static void account_numa_enqueue(struct rq *rq, struct task_struct *p) |
| 1108 | { |
| 1109 | rq->nr_numa_running += (p->numa_preferred_nid != -1); |
| 1110 | rq->nr_preferred_running += (p->numa_preferred_nid == task_node(p)); |
| 1111 | } |
| 1112 | |
| 1113 | static void account_numa_dequeue(struct rq *rq, struct task_struct *p) |
| 1114 | { |
| 1115 | rq->nr_numa_running -= (p->numa_preferred_nid != -1); |
| 1116 | rq->nr_preferred_running -= (p->numa_preferred_nid == task_node(p)); |
| 1117 | } |
| 1118 | |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 1119 | struct numa_group { |
| 1120 | atomic_t refcount; |
| 1121 | |
| 1122 | spinlock_t lock; /* nr_tasks, tasks */ |
| 1123 | int nr_tasks; |
Mel Gorman | e29cf08 | 2013-10-07 11:29:22 +0100 | [diff] [blame] | 1124 | pid_t gid; |
Rik van Riel | 4142c3e | 2016-01-25 17:07:39 -0500 | [diff] [blame] | 1125 | int active_nodes; |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 1126 | |
| 1127 | struct rcu_head rcu; |
Mel Gorman | 989348b | 2013-10-07 11:29:40 +0100 | [diff] [blame] | 1128 | unsigned long total_faults; |
Rik van Riel | 4142c3e | 2016-01-25 17:07:39 -0500 | [diff] [blame] | 1129 | unsigned long max_faults_cpu; |
Rik van Riel | 7e2703e | 2014-01-27 17:03:45 -0500 | [diff] [blame] | 1130 | /* |
| 1131 | * Faults_cpu is used to decide whether memory should move |
| 1132 | * towards the CPU. As a consequence, these stats are weighted |
| 1133 | * more by CPU use than by memory faults. |
| 1134 | */ |
Rik van Riel | 50ec8a4 | 2014-01-27 17:03:42 -0500 | [diff] [blame] | 1135 | unsigned long *faults_cpu; |
Mel Gorman | 989348b | 2013-10-07 11:29:40 +0100 | [diff] [blame] | 1136 | unsigned long faults[0]; |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 1137 | }; |
| 1138 | |
Rik van Riel | be1e4e7 | 2014-01-27 17:03:48 -0500 | [diff] [blame] | 1139 | /* Shared or private faults. */ |
| 1140 | #define NR_NUMA_HINT_FAULT_TYPES 2 |
| 1141 | |
| 1142 | /* Memory and CPU locality */ |
| 1143 | #define NR_NUMA_HINT_FAULT_STATS (NR_NUMA_HINT_FAULT_TYPES * 2) |
| 1144 | |
| 1145 | /* Averaged statistics, and temporary buffers. */ |
| 1146 | #define NR_NUMA_HINT_FAULT_BUCKETS (NR_NUMA_HINT_FAULT_STATS * 2) |
| 1147 | |
Mel Gorman | e29cf08 | 2013-10-07 11:29:22 +0100 | [diff] [blame] | 1148 | pid_t task_numa_group_id(struct task_struct *p) |
| 1149 | { |
| 1150 | return p->numa_group ? p->numa_group->gid : 0; |
| 1151 | } |
| 1152 | |
Iulia Manda | 44dba3d | 2014-10-31 02:13:31 +0200 | [diff] [blame] | 1153 | /* |
| 1154 | * The averaged statistics, shared & private, memory & cpu, |
| 1155 | * occupy the first half of the array. The second half of the |
| 1156 | * array is for current counters, which are averaged into the |
| 1157 | * first set by task_numa_placement. |
| 1158 | */ |
| 1159 | static inline int task_faults_idx(enum numa_faults_stats s, int nid, int priv) |
Mel Gorman | ac8e895 | 2013-10-07 11:29:03 +0100 | [diff] [blame] | 1160 | { |
Iulia Manda | 44dba3d | 2014-10-31 02:13:31 +0200 | [diff] [blame] | 1161 | return NR_NUMA_HINT_FAULT_TYPES * (s * nr_node_ids + nid) + priv; |
Mel Gorman | ac8e895 | 2013-10-07 11:29:03 +0100 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | static inline unsigned long task_faults(struct task_struct *p, int nid) |
| 1165 | { |
Iulia Manda | 44dba3d | 2014-10-31 02:13:31 +0200 | [diff] [blame] | 1166 | if (!p->numa_faults) |
Mel Gorman | ac8e895 | 2013-10-07 11:29:03 +0100 | [diff] [blame] | 1167 | return 0; |
| 1168 | |
Iulia Manda | 44dba3d | 2014-10-31 02:13:31 +0200 | [diff] [blame] | 1169 | return p->numa_faults[task_faults_idx(NUMA_MEM, nid, 0)] + |
| 1170 | p->numa_faults[task_faults_idx(NUMA_MEM, nid, 1)]; |
Mel Gorman | ac8e895 | 2013-10-07 11:29:03 +0100 | [diff] [blame] | 1171 | } |
| 1172 | |
Mel Gorman | 83e1d2c | 2013-10-07 11:29:27 +0100 | [diff] [blame] | 1173 | static inline unsigned long group_faults(struct task_struct *p, int nid) |
| 1174 | { |
| 1175 | if (!p->numa_group) |
| 1176 | return 0; |
| 1177 | |
Iulia Manda | 44dba3d | 2014-10-31 02:13:31 +0200 | [diff] [blame] | 1178 | return p->numa_group->faults[task_faults_idx(NUMA_MEM, nid, 0)] + |
| 1179 | p->numa_group->faults[task_faults_idx(NUMA_MEM, nid, 1)]; |
Mel Gorman | 83e1d2c | 2013-10-07 11:29:27 +0100 | [diff] [blame] | 1180 | } |
| 1181 | |
Rik van Riel | 20e07de | 2014-01-27 17:03:43 -0500 | [diff] [blame] | 1182 | static inline unsigned long group_faults_cpu(struct numa_group *group, int nid) |
| 1183 | { |
Iulia Manda | 44dba3d | 2014-10-31 02:13:31 +0200 | [diff] [blame] | 1184 | return group->faults_cpu[task_faults_idx(NUMA_MEM, nid, 0)] + |
| 1185 | group->faults_cpu[task_faults_idx(NUMA_MEM, nid, 1)]; |
Rik van Riel | 20e07de | 2014-01-27 17:03:43 -0500 | [diff] [blame] | 1186 | } |
| 1187 | |
Rik van Riel | 4142c3e | 2016-01-25 17:07:39 -0500 | [diff] [blame] | 1188 | /* |
| 1189 | * A node triggering more than 1/3 as many NUMA faults as the maximum is |
| 1190 | * considered part of a numa group's pseudo-interleaving set. Migrations |
| 1191 | * between these nodes are slowed down, to allow things to settle down. |
| 1192 | */ |
| 1193 | #define ACTIVE_NODE_FRACTION 3 |
| 1194 | |
| 1195 | static bool numa_is_active_node(int nid, struct numa_group *ng) |
| 1196 | { |
| 1197 | return group_faults_cpu(ng, nid) * ACTIVE_NODE_FRACTION > ng->max_faults_cpu; |
| 1198 | } |
| 1199 | |
Rik van Riel | 6c6b119 | 2014-10-17 03:29:52 -0400 | [diff] [blame] | 1200 | /* Handle placement on systems where not all nodes are directly connected. */ |
| 1201 | static unsigned long score_nearby_nodes(struct task_struct *p, int nid, |
| 1202 | int maxdist, bool task) |
| 1203 | { |
| 1204 | unsigned long score = 0; |
| 1205 | int node; |
| 1206 | |
| 1207 | /* |
| 1208 | * All nodes are directly connected, and the same distance |
| 1209 | * from each other. No need for fancy placement algorithms. |
| 1210 | */ |
| 1211 | if (sched_numa_topology_type == NUMA_DIRECT) |
| 1212 | return 0; |
| 1213 | |
| 1214 | /* |
| 1215 | * This code is called for each node, introducing N^2 complexity, |
| 1216 | * which should be ok given the number of nodes rarely exceeds 8. |
| 1217 | */ |
| 1218 | for_each_online_node(node) { |
| 1219 | unsigned long faults; |
| 1220 | int dist = node_distance(nid, node); |
| 1221 | |
| 1222 | /* |
| 1223 | * The furthest away nodes in the system are not interesting |
| 1224 | * for placement; nid was already counted. |
| 1225 | */ |
| 1226 | if (dist == sched_max_numa_distance || node == nid) |
| 1227 | continue; |
| 1228 | |
| 1229 | /* |
| 1230 | * On systems with a backplane NUMA topology, compare groups |
| 1231 | * of nodes, and move tasks towards the group with the most |
| 1232 | * memory accesses. When comparing two nodes at distance |
| 1233 | * "hoplimit", only nodes closer by than "hoplimit" are part |
| 1234 | * of each group. Skip other nodes. |
| 1235 | */ |
| 1236 | if (sched_numa_topology_type == NUMA_BACKPLANE && |
| 1237 | dist > maxdist) |
| 1238 | continue; |
| 1239 | |
| 1240 | /* Add up the faults from nearby nodes. */ |
| 1241 | if (task) |
| 1242 | faults = task_faults(p, node); |
| 1243 | else |
| 1244 | faults = group_faults(p, node); |
| 1245 | |
| 1246 | /* |
| 1247 | * On systems with a glueless mesh NUMA topology, there are |
| 1248 | * no fixed "groups of nodes". Instead, nodes that are not |
| 1249 | * directly connected bounce traffic through intermediate |
| 1250 | * nodes; a numa_group can occupy any set of nodes. |
| 1251 | * The further away a node is, the less the faults count. |
| 1252 | * This seems to result in good task placement. |
| 1253 | */ |
| 1254 | if (sched_numa_topology_type == NUMA_GLUELESS_MESH) { |
| 1255 | faults *= (sched_max_numa_distance - dist); |
| 1256 | faults /= (sched_max_numa_distance - LOCAL_DISTANCE); |
| 1257 | } |
| 1258 | |
| 1259 | score += faults; |
| 1260 | } |
| 1261 | |
| 1262 | return score; |
| 1263 | } |
| 1264 | |
Mel Gorman | 83e1d2c | 2013-10-07 11:29:27 +0100 | [diff] [blame] | 1265 | /* |
| 1266 | * These return the fraction of accesses done by a particular task, or |
| 1267 | * task group, on a particular numa node. The group weight is given a |
| 1268 | * larger multiplier, in order to group tasks together that are almost |
| 1269 | * evenly spread out between numa nodes. |
| 1270 | */ |
Rik van Riel | 7bd9532 | 2014-10-17 03:29:51 -0400 | [diff] [blame] | 1271 | static inline unsigned long task_weight(struct task_struct *p, int nid, |
| 1272 | int dist) |
Mel Gorman | 83e1d2c | 2013-10-07 11:29:27 +0100 | [diff] [blame] | 1273 | { |
Rik van Riel | 7bd9532 | 2014-10-17 03:29:51 -0400 | [diff] [blame] | 1274 | unsigned long faults, total_faults; |
Mel Gorman | 83e1d2c | 2013-10-07 11:29:27 +0100 | [diff] [blame] | 1275 | |
Iulia Manda | 44dba3d | 2014-10-31 02:13:31 +0200 | [diff] [blame] | 1276 | if (!p->numa_faults) |
Mel Gorman | 83e1d2c | 2013-10-07 11:29:27 +0100 | [diff] [blame] | 1277 | return 0; |
| 1278 | |
| 1279 | total_faults = p->total_numa_faults; |
| 1280 | |
| 1281 | if (!total_faults) |
| 1282 | return 0; |
| 1283 | |
Rik van Riel | 7bd9532 | 2014-10-17 03:29:51 -0400 | [diff] [blame] | 1284 | faults = task_faults(p, nid); |
Rik van Riel | 6c6b119 | 2014-10-17 03:29:52 -0400 | [diff] [blame] | 1285 | faults += score_nearby_nodes(p, nid, dist, true); |
| 1286 | |
Rik van Riel | 7bd9532 | 2014-10-17 03:29:51 -0400 | [diff] [blame] | 1287 | return 1000 * faults / total_faults; |
Mel Gorman | 83e1d2c | 2013-10-07 11:29:27 +0100 | [diff] [blame] | 1288 | } |
| 1289 | |
Rik van Riel | 7bd9532 | 2014-10-17 03:29:51 -0400 | [diff] [blame] | 1290 | static inline unsigned long group_weight(struct task_struct *p, int nid, |
| 1291 | int dist) |
Mel Gorman | 83e1d2c | 2013-10-07 11:29:27 +0100 | [diff] [blame] | 1292 | { |
Rik van Riel | 7bd9532 | 2014-10-17 03:29:51 -0400 | [diff] [blame] | 1293 | unsigned long faults, total_faults; |
| 1294 | |
| 1295 | if (!p->numa_group) |
Mel Gorman | 83e1d2c | 2013-10-07 11:29:27 +0100 | [diff] [blame] | 1296 | return 0; |
| 1297 | |
Rik van Riel | 7bd9532 | 2014-10-17 03:29:51 -0400 | [diff] [blame] | 1298 | total_faults = p->numa_group->total_faults; |
| 1299 | |
| 1300 | if (!total_faults) |
| 1301 | return 0; |
| 1302 | |
| 1303 | faults = group_faults(p, nid); |
Rik van Riel | 6c6b119 | 2014-10-17 03:29:52 -0400 | [diff] [blame] | 1304 | faults += score_nearby_nodes(p, nid, dist, false); |
| 1305 | |
Rik van Riel | 7bd9532 | 2014-10-17 03:29:51 -0400 | [diff] [blame] | 1306 | return 1000 * faults / total_faults; |
Mel Gorman | 83e1d2c | 2013-10-07 11:29:27 +0100 | [diff] [blame] | 1307 | } |
| 1308 | |
Rik van Riel | 10f3904 | 2014-01-27 17:03:44 -0500 | [diff] [blame] | 1309 | bool should_numa_migrate_memory(struct task_struct *p, struct page * page, |
| 1310 | int src_nid, int dst_cpu) |
| 1311 | { |
| 1312 | struct numa_group *ng = p->numa_group; |
| 1313 | int dst_nid = cpu_to_node(dst_cpu); |
| 1314 | int last_cpupid, this_cpupid; |
| 1315 | |
| 1316 | this_cpupid = cpu_pid_to_cpupid(dst_cpu, current->pid); |
| 1317 | |
| 1318 | /* |
| 1319 | * Multi-stage node selection is used in conjunction with a periodic |
| 1320 | * migration fault to build a temporal task<->page relation. By using |
| 1321 | * a two-stage filter we remove short/unlikely relations. |
| 1322 | * |
| 1323 | * Using P(p) ~ n_p / n_t as per frequentist probability, we can equate |
| 1324 | * a task's usage of a particular page (n_p) per total usage of this |
| 1325 | * page (n_t) (in a given time-span) to a probability. |
| 1326 | * |
| 1327 | * Our periodic faults will sample this probability and getting the |
| 1328 | * same result twice in a row, given these samples are fully |
| 1329 | * independent, is then given by P(n)^2, provided our sample period |
| 1330 | * is sufficiently short compared to the usage pattern. |
| 1331 | * |
| 1332 | * This quadric squishes small probabilities, making it less likely we |
| 1333 | * act on an unlikely task<->page relation. |
| 1334 | */ |
| 1335 | last_cpupid = page_cpupid_xchg_last(page, this_cpupid); |
| 1336 | if (!cpupid_pid_unset(last_cpupid) && |
| 1337 | cpupid_to_nid(last_cpupid) != dst_nid) |
| 1338 | return false; |
| 1339 | |
| 1340 | /* Always allow migrate on private faults */ |
| 1341 | if (cpupid_match_pid(p, last_cpupid)) |
| 1342 | return true; |
| 1343 | |
| 1344 | /* A shared fault, but p->numa_group has not been set up yet. */ |
| 1345 | if (!ng) |
| 1346 | return true; |
| 1347 | |
| 1348 | /* |
Rik van Riel | 4142c3e | 2016-01-25 17:07:39 -0500 | [diff] [blame] | 1349 | * Destination node is much more heavily used than the source |
| 1350 | * node? Allow migration. |
Rik van Riel | 10f3904 | 2014-01-27 17:03:44 -0500 | [diff] [blame] | 1351 | */ |
Rik van Riel | 4142c3e | 2016-01-25 17:07:39 -0500 | [diff] [blame] | 1352 | if (group_faults_cpu(ng, dst_nid) > group_faults_cpu(ng, src_nid) * |
| 1353 | ACTIVE_NODE_FRACTION) |
Rik van Riel | 10f3904 | 2014-01-27 17:03:44 -0500 | [diff] [blame] | 1354 | return true; |
| 1355 | |
| 1356 | /* |
Rik van Riel | 4142c3e | 2016-01-25 17:07:39 -0500 | [diff] [blame] | 1357 | * Distribute memory according to CPU & memory use on each node, |
| 1358 | * with 3/4 hysteresis to avoid unnecessary memory migrations: |
| 1359 | * |
| 1360 | * faults_cpu(dst) 3 faults_cpu(src) |
| 1361 | * --------------- * - > --------------- |
| 1362 | * faults_mem(dst) 4 faults_mem(src) |
Rik van Riel | 10f3904 | 2014-01-27 17:03:44 -0500 | [diff] [blame] | 1363 | */ |
Rik van Riel | 4142c3e | 2016-01-25 17:07:39 -0500 | [diff] [blame] | 1364 | return group_faults_cpu(ng, dst_nid) * group_faults(p, src_nid) * 3 > |
| 1365 | group_faults_cpu(ng, src_nid) * group_faults(p, dst_nid) * 4; |
Rik van Riel | 10f3904 | 2014-01-27 17:03:44 -0500 | [diff] [blame] | 1366 | } |
| 1367 | |
Mel Gorman | e6628d5 | 2013-10-07 11:29:02 +0100 | [diff] [blame] | 1368 | static unsigned long weighted_cpuload(const int cpu); |
Mel Gorman | 58d081b | 2013-10-07 11:29:10 +0100 | [diff] [blame] | 1369 | static unsigned long source_load(int cpu, int type); |
| 1370 | static unsigned long target_load(int cpu, int type); |
Nicolas Pitre | ced549f | 2014-05-26 18:19:38 -0400 | [diff] [blame] | 1371 | static unsigned long capacity_of(int cpu); |
Mel Gorman | 58d081b | 2013-10-07 11:29:10 +0100 | [diff] [blame] | 1372 | static long effective_load(struct task_group *tg, int cpu, long wl, long wg); |
Mel Gorman | e6628d5 | 2013-10-07 11:29:02 +0100 | [diff] [blame] | 1373 | |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1374 | /* Cached statistics for all CPUs within a node */ |
Mel Gorman | 58d081b | 2013-10-07 11:29:10 +0100 | [diff] [blame] | 1375 | struct numa_stats { |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1376 | unsigned long nr_running; |
Mel Gorman | 58d081b | 2013-10-07 11:29:10 +0100 | [diff] [blame] | 1377 | unsigned long load; |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1378 | |
| 1379 | /* Total compute capacity of CPUs on a node */ |
Nicolas Pitre | 5ef20ca | 2014-05-26 18:19:34 -0400 | [diff] [blame] | 1380 | unsigned long compute_capacity; |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1381 | |
| 1382 | /* Approximate capacity in terms of runnable tasks on a node */ |
Nicolas Pitre | 5ef20ca | 2014-05-26 18:19:34 -0400 | [diff] [blame] | 1383 | unsigned long task_capacity; |
Nicolas Pitre | 1b6a749 | 2014-05-26 18:19:35 -0400 | [diff] [blame] | 1384 | int has_free_capacity; |
Mel Gorman | 58d081b | 2013-10-07 11:29:10 +0100 | [diff] [blame] | 1385 | }; |
Mel Gorman | e6628d5 | 2013-10-07 11:29:02 +0100 | [diff] [blame] | 1386 | |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1387 | /* |
| 1388 | * XXX borrowed from update_sg_lb_stats |
| 1389 | */ |
| 1390 | static void update_numa_stats(struct numa_stats *ns, int nid) |
| 1391 | { |
Rik van Riel | 83d7f24 | 2014-08-04 13:23:28 -0400 | [diff] [blame] | 1392 | int smt, cpu, cpus = 0; |
| 1393 | unsigned long capacity; |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1394 | |
| 1395 | memset(ns, 0, sizeof(*ns)); |
| 1396 | for_each_cpu(cpu, cpumask_of_node(nid)) { |
| 1397 | struct rq *rq = cpu_rq(cpu); |
| 1398 | |
| 1399 | ns->nr_running += rq->nr_running; |
| 1400 | ns->load += weighted_cpuload(cpu); |
Nicolas Pitre | ced549f | 2014-05-26 18:19:38 -0400 | [diff] [blame] | 1401 | ns->compute_capacity += capacity_of(cpu); |
Peter Zijlstra | 5eca82a | 2013-11-06 18:47:57 +0100 | [diff] [blame] | 1402 | |
| 1403 | cpus++; |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1404 | } |
| 1405 | |
Peter Zijlstra | 5eca82a | 2013-11-06 18:47:57 +0100 | [diff] [blame] | 1406 | /* |
| 1407 | * If we raced with hotplug and there are no CPUs left in our mask |
| 1408 | * the @ns structure is NULL'ed and task_numa_compare() will |
| 1409 | * not find this node attractive. |
| 1410 | * |
Nicolas Pitre | 1b6a749 | 2014-05-26 18:19:35 -0400 | [diff] [blame] | 1411 | * We'll either bail at !has_free_capacity, or we'll detect a huge |
| 1412 | * imbalance and bail there. |
Peter Zijlstra | 5eca82a | 2013-11-06 18:47:57 +0100 | [diff] [blame] | 1413 | */ |
| 1414 | if (!cpus) |
| 1415 | return; |
| 1416 | |
Rik van Riel | 83d7f24 | 2014-08-04 13:23:28 -0400 | [diff] [blame] | 1417 | /* smt := ceil(cpus / capacity), assumes: 1 < smt_power < 2 */ |
| 1418 | smt = DIV_ROUND_UP(SCHED_CAPACITY_SCALE * cpus, ns->compute_capacity); |
| 1419 | capacity = cpus / smt; /* cores */ |
| 1420 | |
| 1421 | ns->task_capacity = min_t(unsigned, capacity, |
| 1422 | DIV_ROUND_CLOSEST(ns->compute_capacity, SCHED_CAPACITY_SCALE)); |
Nicolas Pitre | 1b6a749 | 2014-05-26 18:19:35 -0400 | [diff] [blame] | 1423 | ns->has_free_capacity = (ns->nr_running < ns->task_capacity); |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1424 | } |
| 1425 | |
Mel Gorman | 58d081b | 2013-10-07 11:29:10 +0100 | [diff] [blame] | 1426 | struct task_numa_env { |
| 1427 | struct task_struct *p; |
| 1428 | |
| 1429 | int src_cpu, src_nid; |
| 1430 | int dst_cpu, dst_nid; |
| 1431 | |
| 1432 | struct numa_stats src_stats, dst_stats; |
| 1433 | |
Wanpeng Li | 40ea2b4 | 2013-12-05 19:10:17 +0800 | [diff] [blame] | 1434 | int imbalance_pct; |
Rik van Riel | 7bd9532 | 2014-10-17 03:29:51 -0400 | [diff] [blame] | 1435 | int dist; |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1436 | |
| 1437 | struct task_struct *best_task; |
| 1438 | long best_imp; |
Mel Gorman | 58d081b | 2013-10-07 11:29:10 +0100 | [diff] [blame] | 1439 | int best_cpu; |
| 1440 | }; |
| 1441 | |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1442 | static void task_numa_assign(struct task_numa_env *env, |
| 1443 | struct task_struct *p, long imp) |
| 1444 | { |
| 1445 | if (env->best_task) |
| 1446 | put_task_struct(env->best_task); |
Oleg Nesterov | bac7857 | 2016-05-18 21:57:33 +0200 | [diff] [blame] | 1447 | if (p) |
| 1448 | get_task_struct(p); |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1449 | |
| 1450 | env->best_task = p; |
| 1451 | env->best_imp = imp; |
| 1452 | env->best_cpu = env->dst_cpu; |
| 1453 | } |
| 1454 | |
Rik van Riel | 28a2174 | 2014-06-23 11:46:13 -0400 | [diff] [blame] | 1455 | static bool load_too_imbalanced(long src_load, long dst_load, |
Rik van Riel | e63da03 | 2014-05-14 13:22:21 -0400 | [diff] [blame] | 1456 | struct task_numa_env *env) |
| 1457 | { |
Rik van Riel | e4991b2 | 2015-05-27 15:04:27 -0400 | [diff] [blame] | 1458 | long imb, old_imb; |
| 1459 | long orig_src_load, orig_dst_load; |
Rik van Riel | 28a2174 | 2014-06-23 11:46:13 -0400 | [diff] [blame] | 1460 | long src_capacity, dst_capacity; |
| 1461 | |
| 1462 | /* |
| 1463 | * The load is corrected for the CPU capacity available on each node. |
| 1464 | * |
| 1465 | * src_load dst_load |
| 1466 | * ------------ vs --------- |
| 1467 | * src_capacity dst_capacity |
| 1468 | */ |
| 1469 | src_capacity = env->src_stats.compute_capacity; |
| 1470 | dst_capacity = env->dst_stats.compute_capacity; |
Rik van Riel | e63da03 | 2014-05-14 13:22:21 -0400 | [diff] [blame] | 1471 | |
| 1472 | /* We care about the slope of the imbalance, not the direction. */ |
Rik van Riel | e4991b2 | 2015-05-27 15:04:27 -0400 | [diff] [blame] | 1473 | if (dst_load < src_load) |
| 1474 | swap(dst_load, src_load); |
Rik van Riel | e63da03 | 2014-05-14 13:22:21 -0400 | [diff] [blame] | 1475 | |
| 1476 | /* Is the difference below the threshold? */ |
Rik van Riel | e4991b2 | 2015-05-27 15:04:27 -0400 | [diff] [blame] | 1477 | imb = dst_load * src_capacity * 100 - |
| 1478 | src_load * dst_capacity * env->imbalance_pct; |
Rik van Riel | e63da03 | 2014-05-14 13:22:21 -0400 | [diff] [blame] | 1479 | if (imb <= 0) |
| 1480 | return false; |
| 1481 | |
| 1482 | /* |
| 1483 | * The imbalance is above the allowed threshold. |
Rik van Riel | e4991b2 | 2015-05-27 15:04:27 -0400 | [diff] [blame] | 1484 | * Compare it with the old imbalance. |
Rik van Riel | e63da03 | 2014-05-14 13:22:21 -0400 | [diff] [blame] | 1485 | */ |
Rik van Riel | 28a2174 | 2014-06-23 11:46:13 -0400 | [diff] [blame] | 1486 | orig_src_load = env->src_stats.load; |
Rik van Riel | e4991b2 | 2015-05-27 15:04:27 -0400 | [diff] [blame] | 1487 | orig_dst_load = env->dst_stats.load; |
Rik van Riel | 28a2174 | 2014-06-23 11:46:13 -0400 | [diff] [blame] | 1488 | |
Rik van Riel | e4991b2 | 2015-05-27 15:04:27 -0400 | [diff] [blame] | 1489 | if (orig_dst_load < orig_src_load) |
| 1490 | swap(orig_dst_load, orig_src_load); |
Rik van Riel | e63da03 | 2014-05-14 13:22:21 -0400 | [diff] [blame] | 1491 | |
Rik van Riel | e4991b2 | 2015-05-27 15:04:27 -0400 | [diff] [blame] | 1492 | old_imb = orig_dst_load * src_capacity * 100 - |
| 1493 | orig_src_load * dst_capacity * env->imbalance_pct; |
| 1494 | |
| 1495 | /* Would this change make things worse? */ |
| 1496 | return (imb > old_imb); |
Rik van Riel | e63da03 | 2014-05-14 13:22:21 -0400 | [diff] [blame] | 1497 | } |
| 1498 | |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1499 | /* |
| 1500 | * This checks if the overall compute and NUMA accesses of the system would |
| 1501 | * be improved if the source tasks was migrated to the target dst_cpu taking |
| 1502 | * into account that it might be best if task running on the dst_cpu should |
| 1503 | * be exchanged with the source task |
| 1504 | */ |
Rik van Riel | 887c290 | 2013-10-07 11:29:31 +0100 | [diff] [blame] | 1505 | static void task_numa_compare(struct task_numa_env *env, |
| 1506 | long taskimp, long groupimp) |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1507 | { |
| 1508 | struct rq *src_rq = cpu_rq(env->src_cpu); |
| 1509 | struct rq *dst_rq = cpu_rq(env->dst_cpu); |
| 1510 | struct task_struct *cur; |
Rik van Riel | 28a2174 | 2014-06-23 11:46:13 -0400 | [diff] [blame] | 1511 | long src_load, dst_load; |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1512 | long load; |
Rik van Riel | 1c5d3eb | 2014-06-23 11:46:15 -0400 | [diff] [blame] | 1513 | long imp = env->p->numa_group ? groupimp : taskimp; |
Rik van Riel | 0132c3e | 2014-06-23 11:46:16 -0400 | [diff] [blame] | 1514 | long moveimp = imp; |
Rik van Riel | 7bd9532 | 2014-10-17 03:29:51 -0400 | [diff] [blame] | 1515 | int dist = env->dist; |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1516 | |
| 1517 | rcu_read_lock(); |
Oleg Nesterov | bac7857 | 2016-05-18 21:57:33 +0200 | [diff] [blame] | 1518 | cur = task_rcu_dereference(&dst_rq->curr); |
| 1519 | if (cur && ((cur->flags & PF_EXITING) || is_idle_task(cur))) |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1520 | cur = NULL; |
| 1521 | |
| 1522 | /* |
Peter Zijlstra | 7af6833 | 2014-11-10 10:54:35 +0100 | [diff] [blame] | 1523 | * Because we have preemption enabled we can get migrated around and |
| 1524 | * end try selecting ourselves (current == env->p) as a swap candidate. |
| 1525 | */ |
| 1526 | if (cur == env->p) |
| 1527 | goto unlock; |
| 1528 | |
| 1529 | /* |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1530 | * "imp" is the fault differential for the source task between the |
| 1531 | * source and destination node. Calculate the total differential for |
| 1532 | * the source task and potential destination task. The more negative |
| 1533 | * the value is, the more rmeote accesses that would be expected to |
| 1534 | * be incurred if the tasks were swapped. |
| 1535 | */ |
| 1536 | if (cur) { |
| 1537 | /* Skip this swap candidate if cannot move to the source cpu */ |
| 1538 | if (!cpumask_test_cpu(env->src_cpu, tsk_cpus_allowed(cur))) |
| 1539 | goto unlock; |
| 1540 | |
Rik van Riel | 887c290 | 2013-10-07 11:29:31 +0100 | [diff] [blame] | 1541 | /* |
| 1542 | * If dst and source tasks are in the same NUMA group, or not |
Rik van Riel | ca28aa53 | 2013-10-07 11:29:32 +0100 | [diff] [blame] | 1543 | * in any group then look only at task weights. |
Rik van Riel | 887c290 | 2013-10-07 11:29:31 +0100 | [diff] [blame] | 1544 | */ |
Rik van Riel | ca28aa53 | 2013-10-07 11:29:32 +0100 | [diff] [blame] | 1545 | if (cur->numa_group == env->p->numa_group) { |
Rik van Riel | 7bd9532 | 2014-10-17 03:29:51 -0400 | [diff] [blame] | 1546 | imp = taskimp + task_weight(cur, env->src_nid, dist) - |
| 1547 | task_weight(cur, env->dst_nid, dist); |
Rik van Riel | ca28aa53 | 2013-10-07 11:29:32 +0100 | [diff] [blame] | 1548 | /* |
| 1549 | * Add some hysteresis to prevent swapping the |
| 1550 | * tasks within a group over tiny differences. |
| 1551 | */ |
| 1552 | if (cur->numa_group) |
| 1553 | imp -= imp/16; |
Rik van Riel | 887c290 | 2013-10-07 11:29:31 +0100 | [diff] [blame] | 1554 | } else { |
Rik van Riel | ca28aa53 | 2013-10-07 11:29:32 +0100 | [diff] [blame] | 1555 | /* |
| 1556 | * Compare the group weights. If a task is all by |
| 1557 | * itself (not part of a group), use the task weight |
| 1558 | * instead. |
| 1559 | */ |
Rik van Riel | ca28aa53 | 2013-10-07 11:29:32 +0100 | [diff] [blame] | 1560 | if (cur->numa_group) |
Rik van Riel | 7bd9532 | 2014-10-17 03:29:51 -0400 | [diff] [blame] | 1561 | imp += group_weight(cur, env->src_nid, dist) - |
| 1562 | group_weight(cur, env->dst_nid, dist); |
Rik van Riel | ca28aa53 | 2013-10-07 11:29:32 +0100 | [diff] [blame] | 1563 | else |
Rik van Riel | 7bd9532 | 2014-10-17 03:29:51 -0400 | [diff] [blame] | 1564 | imp += task_weight(cur, env->src_nid, dist) - |
| 1565 | task_weight(cur, env->dst_nid, dist); |
Rik van Riel | 887c290 | 2013-10-07 11:29:31 +0100 | [diff] [blame] | 1566 | } |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1567 | } |
| 1568 | |
Rik van Riel | 0132c3e | 2014-06-23 11:46:16 -0400 | [diff] [blame] | 1569 | if (imp <= env->best_imp && moveimp <= env->best_imp) |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1570 | goto unlock; |
| 1571 | |
| 1572 | if (!cur) { |
| 1573 | /* Is there capacity at our destination? */ |
Rik van Riel | b932c03 | 2014-08-04 13:23:27 -0400 | [diff] [blame] | 1574 | if (env->src_stats.nr_running <= env->src_stats.task_capacity && |
Nicolas Pitre | 1b6a749 | 2014-05-26 18:19:35 -0400 | [diff] [blame] | 1575 | !env->dst_stats.has_free_capacity) |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1576 | goto unlock; |
| 1577 | |
| 1578 | goto balance; |
| 1579 | } |
| 1580 | |
| 1581 | /* Balance doesn't matter much if we're running a task per cpu */ |
Rik van Riel | 0132c3e | 2014-06-23 11:46:16 -0400 | [diff] [blame] | 1582 | if (imp > env->best_imp && src_rq->nr_running == 1 && |
| 1583 | dst_rq->nr_running == 1) |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1584 | goto assign; |
| 1585 | |
| 1586 | /* |
| 1587 | * In the overloaded case, try and keep the load balanced. |
| 1588 | */ |
| 1589 | balance: |
Peter Zijlstra | e720fff | 2014-07-11 16:01:53 +0200 | [diff] [blame] | 1590 | load = task_h_load(env->p); |
| 1591 | dst_load = env->dst_stats.load + load; |
| 1592 | src_load = env->src_stats.load - load; |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1593 | |
Rik van Riel | 0132c3e | 2014-06-23 11:46:16 -0400 | [diff] [blame] | 1594 | if (moveimp > imp && moveimp > env->best_imp) { |
| 1595 | /* |
| 1596 | * If the improvement from just moving env->p direction is |
| 1597 | * better than swapping tasks around, check if a move is |
| 1598 | * possible. Store a slightly smaller score than moveimp, |
| 1599 | * so an actually idle CPU will win. |
| 1600 | */ |
| 1601 | if (!load_too_imbalanced(src_load, dst_load, env)) { |
| 1602 | imp = moveimp - 1; |
| 1603 | cur = NULL; |
| 1604 | goto assign; |
| 1605 | } |
| 1606 | } |
| 1607 | |
| 1608 | if (imp <= env->best_imp) |
| 1609 | goto unlock; |
| 1610 | |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1611 | if (cur) { |
Peter Zijlstra | e720fff | 2014-07-11 16:01:53 +0200 | [diff] [blame] | 1612 | load = task_h_load(cur); |
| 1613 | dst_load -= load; |
| 1614 | src_load += load; |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1615 | } |
| 1616 | |
Rik van Riel | 28a2174 | 2014-06-23 11:46:13 -0400 | [diff] [blame] | 1617 | if (load_too_imbalanced(src_load, dst_load, env)) |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1618 | goto unlock; |
| 1619 | |
Rik van Riel | ba7e5a2 | 2014-09-04 16:35:30 -0400 | [diff] [blame] | 1620 | /* |
| 1621 | * One idle CPU per node is evaluated for a task numa move. |
| 1622 | * Call select_idle_sibling to maybe find a better one. |
| 1623 | */ |
Peter Zijlstra | 10e2f1a | 2016-05-09 10:38:05 +0200 | [diff] [blame] | 1624 | if (!cur) { |
| 1625 | /* |
| 1626 | * select_idle_siblings() uses an per-cpu cpumask that |
| 1627 | * can be used from IRQ context. |
| 1628 | */ |
| 1629 | local_irq_disable(); |
Morten Rasmussen | 772bd008c | 2016-06-22 18:03:13 +0100 | [diff] [blame] | 1630 | env->dst_cpu = select_idle_sibling(env->p, env->src_cpu, |
| 1631 | env->dst_cpu); |
Peter Zijlstra | 10e2f1a | 2016-05-09 10:38:05 +0200 | [diff] [blame] | 1632 | local_irq_enable(); |
| 1633 | } |
Rik van Riel | ba7e5a2 | 2014-09-04 16:35:30 -0400 | [diff] [blame] | 1634 | |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1635 | assign: |
| 1636 | task_numa_assign(env, cur, imp); |
| 1637 | unlock: |
| 1638 | rcu_read_unlock(); |
| 1639 | } |
| 1640 | |
Rik van Riel | 887c290 | 2013-10-07 11:29:31 +0100 | [diff] [blame] | 1641 | static void task_numa_find_cpu(struct task_numa_env *env, |
| 1642 | long taskimp, long groupimp) |
Mel Gorman | 2c8a50a | 2013-10-07 11:29:18 +0100 | [diff] [blame] | 1643 | { |
| 1644 | int cpu; |
| 1645 | |
| 1646 | for_each_cpu(cpu, cpumask_of_node(env->dst_nid)) { |
| 1647 | /* Skip this CPU if the source task cannot migrate */ |
| 1648 | if (!cpumask_test_cpu(cpu, tsk_cpus_allowed(env->p))) |
| 1649 | continue; |
| 1650 | |
| 1651 | env->dst_cpu = cpu; |
Rik van Riel | 887c290 | 2013-10-07 11:29:31 +0100 | [diff] [blame] | 1652 | task_numa_compare(env, taskimp, groupimp); |
Mel Gorman | 2c8a50a | 2013-10-07 11:29:18 +0100 | [diff] [blame] | 1653 | } |
| 1654 | } |
| 1655 | |
Rik van Riel | 6f9aad0 | 2015-05-28 09:52:49 -0400 | [diff] [blame] | 1656 | /* Only move tasks to a NUMA node less busy than the current node. */ |
| 1657 | static bool numa_has_capacity(struct task_numa_env *env) |
| 1658 | { |
| 1659 | struct numa_stats *src = &env->src_stats; |
| 1660 | struct numa_stats *dst = &env->dst_stats; |
| 1661 | |
| 1662 | if (src->has_free_capacity && !dst->has_free_capacity) |
| 1663 | return false; |
| 1664 | |
| 1665 | /* |
| 1666 | * Only consider a task move if the source has a higher load |
| 1667 | * than the destination, corrected for CPU capacity on each node. |
| 1668 | * |
| 1669 | * src->load dst->load |
| 1670 | * --------------------- vs --------------------- |
| 1671 | * src->compute_capacity dst->compute_capacity |
| 1672 | */ |
Srikar Dronamraju | 44dcb04 | 2015-06-16 17:26:00 +0530 | [diff] [blame] | 1673 | if (src->load * dst->compute_capacity * env->imbalance_pct > |
| 1674 | |
| 1675 | dst->load * src->compute_capacity * 100) |
Rik van Riel | 6f9aad0 | 2015-05-28 09:52:49 -0400 | [diff] [blame] | 1676 | return true; |
| 1677 | |
| 1678 | return false; |
| 1679 | } |
| 1680 | |
Mel Gorman | 58d081b | 2013-10-07 11:29:10 +0100 | [diff] [blame] | 1681 | static int task_numa_migrate(struct task_struct *p) |
Mel Gorman | e6628d5 | 2013-10-07 11:29:02 +0100 | [diff] [blame] | 1682 | { |
Mel Gorman | 58d081b | 2013-10-07 11:29:10 +0100 | [diff] [blame] | 1683 | struct task_numa_env env = { |
| 1684 | .p = p, |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1685 | |
Mel Gorman | 58d081b | 2013-10-07 11:29:10 +0100 | [diff] [blame] | 1686 | .src_cpu = task_cpu(p), |
Ingo Molnar | b32e86b | 2013-10-07 11:29:30 +0100 | [diff] [blame] | 1687 | .src_nid = task_node(p), |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1688 | |
| 1689 | .imbalance_pct = 112, |
| 1690 | |
| 1691 | .best_task = NULL, |
| 1692 | .best_imp = 0, |
Rik van Riel | 4142c3e | 2016-01-25 17:07:39 -0500 | [diff] [blame] | 1693 | .best_cpu = -1, |
Mel Gorman | 58d081b | 2013-10-07 11:29:10 +0100 | [diff] [blame] | 1694 | }; |
| 1695 | struct sched_domain *sd; |
Rik van Riel | 887c290 | 2013-10-07 11:29:31 +0100 | [diff] [blame] | 1696 | unsigned long taskweight, groupweight; |
Rik van Riel | 7bd9532 | 2014-10-17 03:29:51 -0400 | [diff] [blame] | 1697 | int nid, ret, dist; |
Rik van Riel | 887c290 | 2013-10-07 11:29:31 +0100 | [diff] [blame] | 1698 | long taskimp, groupimp; |
Mel Gorman | e6628d5 | 2013-10-07 11:29:02 +0100 | [diff] [blame] | 1699 | |
Mel Gorman | 58d081b | 2013-10-07 11:29:10 +0100 | [diff] [blame] | 1700 | /* |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1701 | * Pick the lowest SD_NUMA domain, as that would have the smallest |
| 1702 | * imbalance and would be the first to start moving tasks about. |
| 1703 | * |
| 1704 | * And we want to avoid any moving of tasks about, as that would create |
| 1705 | * random movement of tasks -- counter the numa conditions we're trying |
| 1706 | * to satisfy here. |
Mel Gorman | 58d081b | 2013-10-07 11:29:10 +0100 | [diff] [blame] | 1707 | */ |
Mel Gorman | e6628d5 | 2013-10-07 11:29:02 +0100 | [diff] [blame] | 1708 | rcu_read_lock(); |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1709 | sd = rcu_dereference(per_cpu(sd_numa, env.src_cpu)); |
Rik van Riel | 46a73e8 | 2013-11-11 19:29:25 -0500 | [diff] [blame] | 1710 | if (sd) |
| 1711 | env.imbalance_pct = 100 + (sd->imbalance_pct - 100) / 2; |
Mel Gorman | e6628d5 | 2013-10-07 11:29:02 +0100 | [diff] [blame] | 1712 | rcu_read_unlock(); |
| 1713 | |
Rik van Riel | 46a73e8 | 2013-11-11 19:29:25 -0500 | [diff] [blame] | 1714 | /* |
| 1715 | * Cpusets can break the scheduler domain tree into smaller |
| 1716 | * balance domains, some of which do not cross NUMA boundaries. |
| 1717 | * Tasks that are "trapped" in such domains cannot be migrated |
| 1718 | * elsewhere, so there is no point in (re)trying. |
| 1719 | */ |
| 1720 | if (unlikely(!sd)) { |
Wanpeng Li | de1b301 | 2013-12-12 15:23:24 +0800 | [diff] [blame] | 1721 | p->numa_preferred_nid = task_node(p); |
Rik van Riel | 46a73e8 | 2013-11-11 19:29:25 -0500 | [diff] [blame] | 1722 | return -EINVAL; |
| 1723 | } |
| 1724 | |
Mel Gorman | 2c8a50a | 2013-10-07 11:29:18 +0100 | [diff] [blame] | 1725 | env.dst_nid = p->numa_preferred_nid; |
Rik van Riel | 7bd9532 | 2014-10-17 03:29:51 -0400 | [diff] [blame] | 1726 | dist = env.dist = node_distance(env.src_nid, env.dst_nid); |
| 1727 | taskweight = task_weight(p, env.src_nid, dist); |
| 1728 | groupweight = group_weight(p, env.src_nid, dist); |
| 1729 | update_numa_stats(&env.src_stats, env.src_nid); |
| 1730 | taskimp = task_weight(p, env.dst_nid, dist) - taskweight; |
| 1731 | groupimp = group_weight(p, env.dst_nid, dist) - groupweight; |
Mel Gorman | 2c8a50a | 2013-10-07 11:29:18 +0100 | [diff] [blame] | 1732 | update_numa_stats(&env.dst_stats, env.dst_nid); |
Mel Gorman | 58d081b | 2013-10-07 11:29:10 +0100 | [diff] [blame] | 1733 | |
Rik van Riel | a43455a | 2014-06-04 16:09:42 -0400 | [diff] [blame] | 1734 | /* Try to find a spot on the preferred nid. */ |
Rik van Riel | 6f9aad0 | 2015-05-28 09:52:49 -0400 | [diff] [blame] | 1735 | if (numa_has_capacity(&env)) |
| 1736 | task_numa_find_cpu(&env, taskimp, groupimp); |
Rik van Riel | e1dda8a | 2013-10-07 11:29:19 +0100 | [diff] [blame] | 1737 | |
Rik van Riel | 9de05d4 | 2014-10-09 17:27:47 -0400 | [diff] [blame] | 1738 | /* |
| 1739 | * Look at other nodes in these cases: |
| 1740 | * - there is no space available on the preferred_nid |
| 1741 | * - the task is part of a numa_group that is interleaved across |
| 1742 | * multiple NUMA nodes; in order to better consolidate the group, |
| 1743 | * we need to check other locations. |
| 1744 | */ |
Rik van Riel | 4142c3e | 2016-01-25 17:07:39 -0500 | [diff] [blame] | 1745 | if (env.best_cpu == -1 || (p->numa_group && p->numa_group->active_nodes > 1)) { |
Mel Gorman | 2c8a50a | 2013-10-07 11:29:18 +0100 | [diff] [blame] | 1746 | for_each_online_node(nid) { |
| 1747 | if (nid == env.src_nid || nid == p->numa_preferred_nid) |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1748 | continue; |
| 1749 | |
Rik van Riel | 7bd9532 | 2014-10-17 03:29:51 -0400 | [diff] [blame] | 1750 | dist = node_distance(env.src_nid, env.dst_nid); |
Rik van Riel | 6c6b119 | 2014-10-17 03:29:52 -0400 | [diff] [blame] | 1751 | if (sched_numa_topology_type == NUMA_BACKPLANE && |
| 1752 | dist != env.dist) { |
| 1753 | taskweight = task_weight(p, env.src_nid, dist); |
| 1754 | groupweight = group_weight(p, env.src_nid, dist); |
| 1755 | } |
Rik van Riel | 7bd9532 | 2014-10-17 03:29:51 -0400 | [diff] [blame] | 1756 | |
Mel Gorman | 83e1d2c | 2013-10-07 11:29:27 +0100 | [diff] [blame] | 1757 | /* Only consider nodes where both task and groups benefit */ |
Rik van Riel | 7bd9532 | 2014-10-17 03:29:51 -0400 | [diff] [blame] | 1758 | taskimp = task_weight(p, nid, dist) - taskweight; |
| 1759 | groupimp = group_weight(p, nid, dist) - groupweight; |
Rik van Riel | 887c290 | 2013-10-07 11:29:31 +0100 | [diff] [blame] | 1760 | if (taskimp < 0 && groupimp < 0) |
Mel Gorman | 2c8a50a | 2013-10-07 11:29:18 +0100 | [diff] [blame] | 1761 | continue; |
| 1762 | |
Rik van Riel | 7bd9532 | 2014-10-17 03:29:51 -0400 | [diff] [blame] | 1763 | env.dist = dist; |
Mel Gorman | 2c8a50a | 2013-10-07 11:29:18 +0100 | [diff] [blame] | 1764 | env.dst_nid = nid; |
| 1765 | update_numa_stats(&env.dst_stats, env.dst_nid); |
Rik van Riel | 6f9aad0 | 2015-05-28 09:52:49 -0400 | [diff] [blame] | 1766 | if (numa_has_capacity(&env)) |
| 1767 | task_numa_find_cpu(&env, taskimp, groupimp); |
Mel Gorman | 58d081b | 2013-10-07 11:29:10 +0100 | [diff] [blame] | 1768 | } |
| 1769 | } |
| 1770 | |
Rik van Riel | 68d1b02 | 2014-04-11 13:00:29 -0400 | [diff] [blame] | 1771 | /* |
| 1772 | * If the task is part of a workload that spans multiple NUMA nodes, |
| 1773 | * and is migrating into one of the workload's active nodes, remember |
| 1774 | * this node as the task's preferred numa node, so the workload can |
| 1775 | * settle down. |
| 1776 | * A task that migrated to a second choice node will be better off |
| 1777 | * trying for a better one later. Do not set the preferred node here. |
| 1778 | */ |
Rik van Riel | db015da | 2014-06-23 11:41:34 -0400 | [diff] [blame] | 1779 | if (p->numa_group) { |
Rik van Riel | 4142c3e | 2016-01-25 17:07:39 -0500 | [diff] [blame] | 1780 | struct numa_group *ng = p->numa_group; |
| 1781 | |
Rik van Riel | db015da | 2014-06-23 11:41:34 -0400 | [diff] [blame] | 1782 | if (env.best_cpu == -1) |
| 1783 | nid = env.src_nid; |
| 1784 | else |
| 1785 | nid = env.dst_nid; |
| 1786 | |
Rik van Riel | 4142c3e | 2016-01-25 17:07:39 -0500 | [diff] [blame] | 1787 | if (ng->active_nodes > 1 && numa_is_active_node(env.dst_nid, ng)) |
Rik van Riel | db015da | 2014-06-23 11:41:34 -0400 | [diff] [blame] | 1788 | sched_setnuma(p, env.dst_nid); |
| 1789 | } |
| 1790 | |
| 1791 | /* No better CPU than the current one was found. */ |
| 1792 | if (env.best_cpu == -1) |
| 1793 | return -EAGAIN; |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 1794 | |
Rik van Riel | 04bb2f9 | 2013-10-07 11:29:36 +0100 | [diff] [blame] | 1795 | /* |
| 1796 | * Reset the scan period if the task is being rescheduled on an |
| 1797 | * alternative node to recheck if the tasks is now properly placed. |
| 1798 | */ |
| 1799 | p->numa_scan_period = task_scan_min(p); |
| 1800 | |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1801 | if (env.best_task == NULL) { |
Mel Gorman | 286549d | 2014-01-21 15:51:03 -0800 | [diff] [blame] | 1802 | ret = migrate_task_to(p, env.best_cpu); |
| 1803 | if (ret != 0) |
| 1804 | trace_sched_stick_numa(p, env.src_cpu, env.best_cpu); |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1805 | return ret; |
| 1806 | } |
| 1807 | |
| 1808 | ret = migrate_swap(p, env.best_task); |
Mel Gorman | 286549d | 2014-01-21 15:51:03 -0800 | [diff] [blame] | 1809 | if (ret != 0) |
| 1810 | trace_sched_stick_numa(p, env.src_cpu, task_cpu(env.best_task)); |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1811 | put_task_struct(env.best_task); |
| 1812 | return ret; |
Mel Gorman | e6628d5 | 2013-10-07 11:29:02 +0100 | [diff] [blame] | 1813 | } |
| 1814 | |
Mel Gorman | 6b9a746 | 2013-10-07 11:29:11 +0100 | [diff] [blame] | 1815 | /* Attempt to migrate a task to a CPU on the preferred node. */ |
| 1816 | static void numa_migrate_preferred(struct task_struct *p) |
| 1817 | { |
Rik van Riel | 5085e2a | 2014-04-11 13:00:28 -0400 | [diff] [blame] | 1818 | unsigned long interval = HZ; |
| 1819 | |
Rik van Riel | 2739d3e | 2013-10-07 11:29:41 +0100 | [diff] [blame] | 1820 | /* This task has no NUMA fault statistics yet */ |
Iulia Manda | 44dba3d | 2014-10-31 02:13:31 +0200 | [diff] [blame] | 1821 | if (unlikely(p->numa_preferred_nid == -1 || !p->numa_faults)) |
Rik van Riel | 2739d3e | 2013-10-07 11:29:41 +0100 | [diff] [blame] | 1822 | return; |
| 1823 | |
| 1824 | /* Periodically retry migrating the task to the preferred node */ |
Rik van Riel | 5085e2a | 2014-04-11 13:00:28 -0400 | [diff] [blame] | 1825 | interval = min(interval, msecs_to_jiffies(p->numa_scan_period) / 16); |
| 1826 | p->numa_migrate_retry = jiffies + interval; |
Rik van Riel | 2739d3e | 2013-10-07 11:29:41 +0100 | [diff] [blame] | 1827 | |
Mel Gorman | 6b9a746 | 2013-10-07 11:29:11 +0100 | [diff] [blame] | 1828 | /* Success if task is already running on preferred CPU */ |
Wanpeng Li | de1b301 | 2013-12-12 15:23:24 +0800 | [diff] [blame] | 1829 | if (task_node(p) == p->numa_preferred_nid) |
Mel Gorman | 6b9a746 | 2013-10-07 11:29:11 +0100 | [diff] [blame] | 1830 | return; |
| 1831 | |
Mel Gorman | 6b9a746 | 2013-10-07 11:29:11 +0100 | [diff] [blame] | 1832 | /* Otherwise, try migrate to a CPU on the preferred node */ |
Rik van Riel | 2739d3e | 2013-10-07 11:29:41 +0100 | [diff] [blame] | 1833 | task_numa_migrate(p); |
Mel Gorman | 6b9a746 | 2013-10-07 11:29:11 +0100 | [diff] [blame] | 1834 | } |
| 1835 | |
Rik van Riel | 04bb2f9 | 2013-10-07 11:29:36 +0100 | [diff] [blame] | 1836 | /* |
Rik van Riel | 4142c3e | 2016-01-25 17:07:39 -0500 | [diff] [blame] | 1837 | * Find out how many nodes on the workload is actively running on. Do this by |
Rik van Riel | 20e07de | 2014-01-27 17:03:43 -0500 | [diff] [blame] | 1838 | * tracking the nodes from which NUMA hinting faults are triggered. This can |
| 1839 | * be different from the set of nodes where the workload's memory is currently |
| 1840 | * located. |
Rik van Riel | 20e07de | 2014-01-27 17:03:43 -0500 | [diff] [blame] | 1841 | */ |
Rik van Riel | 4142c3e | 2016-01-25 17:07:39 -0500 | [diff] [blame] | 1842 | static void numa_group_count_active_nodes(struct numa_group *numa_group) |
Rik van Riel | 20e07de | 2014-01-27 17:03:43 -0500 | [diff] [blame] | 1843 | { |
| 1844 | unsigned long faults, max_faults = 0; |
Rik van Riel | 4142c3e | 2016-01-25 17:07:39 -0500 | [diff] [blame] | 1845 | int nid, active_nodes = 0; |
Rik van Riel | 20e07de | 2014-01-27 17:03:43 -0500 | [diff] [blame] | 1846 | |
| 1847 | for_each_online_node(nid) { |
| 1848 | faults = group_faults_cpu(numa_group, nid); |
| 1849 | if (faults > max_faults) |
| 1850 | max_faults = faults; |
| 1851 | } |
| 1852 | |
| 1853 | for_each_online_node(nid) { |
| 1854 | faults = group_faults_cpu(numa_group, nid); |
Rik van Riel | 4142c3e | 2016-01-25 17:07:39 -0500 | [diff] [blame] | 1855 | if (faults * ACTIVE_NODE_FRACTION > max_faults) |
| 1856 | active_nodes++; |
Rik van Riel | 20e07de | 2014-01-27 17:03:43 -0500 | [diff] [blame] | 1857 | } |
Rik van Riel | 4142c3e | 2016-01-25 17:07:39 -0500 | [diff] [blame] | 1858 | |
| 1859 | numa_group->max_faults_cpu = max_faults; |
| 1860 | numa_group->active_nodes = active_nodes; |
Rik van Riel | 20e07de | 2014-01-27 17:03:43 -0500 | [diff] [blame] | 1861 | } |
| 1862 | |
| 1863 | /* |
Rik van Riel | 04bb2f9 | 2013-10-07 11:29:36 +0100 | [diff] [blame] | 1864 | * When adapting the scan rate, the period is divided into NUMA_PERIOD_SLOTS |
| 1865 | * increments. The more local the fault statistics are, the higher the scan |
Rik van Riel | a22b4b0 | 2014-06-23 11:41:35 -0400 | [diff] [blame] | 1866 | * period will be for the next scan window. If local/(local+remote) ratio is |
| 1867 | * below NUMA_PERIOD_THRESHOLD (where range of ratio is 1..NUMA_PERIOD_SLOTS) |
| 1868 | * the scan period will decrease. Aim for 70% local accesses. |
Rik van Riel | 04bb2f9 | 2013-10-07 11:29:36 +0100 | [diff] [blame] | 1869 | */ |
| 1870 | #define NUMA_PERIOD_SLOTS 10 |
Rik van Riel | a22b4b0 | 2014-06-23 11:41:35 -0400 | [diff] [blame] | 1871 | #define NUMA_PERIOD_THRESHOLD 7 |
Rik van Riel | 04bb2f9 | 2013-10-07 11:29:36 +0100 | [diff] [blame] | 1872 | |
| 1873 | /* |
| 1874 | * Increase the scan period (slow down scanning) if the majority of |
| 1875 | * our memory is already on our local node, or if the majority of |
| 1876 | * the page accesses are shared with other processes. |
| 1877 | * Otherwise, decrease the scan period. |
| 1878 | */ |
| 1879 | static void update_task_scan_period(struct task_struct *p, |
| 1880 | unsigned long shared, unsigned long private) |
| 1881 | { |
| 1882 | unsigned int period_slot; |
| 1883 | int ratio; |
| 1884 | int diff; |
| 1885 | |
| 1886 | unsigned long remote = p->numa_faults_locality[0]; |
| 1887 | unsigned long local = p->numa_faults_locality[1]; |
| 1888 | |
| 1889 | /* |
| 1890 | * If there were no record hinting faults then either the task is |
| 1891 | * completely idle or all activity is areas that are not of interest |
Mel Gorman | 074c238 | 2015-03-25 15:55:42 -0700 | [diff] [blame] | 1892 | * to automatic numa balancing. Related to that, if there were failed |
| 1893 | * migration then it implies we are migrating too quickly or the local |
| 1894 | * node is overloaded. In either case, scan slower |
Rik van Riel | 04bb2f9 | 2013-10-07 11:29:36 +0100 | [diff] [blame] | 1895 | */ |
Mel Gorman | 074c238 | 2015-03-25 15:55:42 -0700 | [diff] [blame] | 1896 | if (local + shared == 0 || p->numa_faults_locality[2]) { |
Rik van Riel | 04bb2f9 | 2013-10-07 11:29:36 +0100 | [diff] [blame] | 1897 | p->numa_scan_period = min(p->numa_scan_period_max, |
| 1898 | p->numa_scan_period << 1); |
| 1899 | |
| 1900 | p->mm->numa_next_scan = jiffies + |
| 1901 | msecs_to_jiffies(p->numa_scan_period); |
| 1902 | |
| 1903 | return; |
| 1904 | } |
| 1905 | |
| 1906 | /* |
| 1907 | * Prepare to scale scan period relative to the current period. |
| 1908 | * == NUMA_PERIOD_THRESHOLD scan period stays the same |
| 1909 | * < NUMA_PERIOD_THRESHOLD scan period decreases (scan faster) |
| 1910 | * >= NUMA_PERIOD_THRESHOLD scan period increases (scan slower) |
| 1911 | */ |
| 1912 | period_slot = DIV_ROUND_UP(p->numa_scan_period, NUMA_PERIOD_SLOTS); |
| 1913 | ratio = (local * NUMA_PERIOD_SLOTS) / (local + remote); |
| 1914 | if (ratio >= NUMA_PERIOD_THRESHOLD) { |
| 1915 | int slot = ratio - NUMA_PERIOD_THRESHOLD; |
| 1916 | if (!slot) |
| 1917 | slot = 1; |
| 1918 | diff = slot * period_slot; |
| 1919 | } else { |
| 1920 | diff = -(NUMA_PERIOD_THRESHOLD - ratio) * period_slot; |
| 1921 | |
| 1922 | /* |
| 1923 | * Scale scan rate increases based on sharing. There is an |
| 1924 | * inverse relationship between the degree of sharing and |
| 1925 | * the adjustment made to the scanning period. Broadly |
| 1926 | * speaking the intent is that there is little point |
| 1927 | * scanning faster if shared accesses dominate as it may |
| 1928 | * simply bounce migrations uselessly |
| 1929 | */ |
Yasuaki Ishimatsu | 2847c90 | 2014-10-22 16:04:35 +0900 | [diff] [blame] | 1930 | ratio = DIV_ROUND_UP(private * NUMA_PERIOD_SLOTS, (private + shared + 1)); |
Rik van Riel | 04bb2f9 | 2013-10-07 11:29:36 +0100 | [diff] [blame] | 1931 | diff = (diff * ratio) / NUMA_PERIOD_SLOTS; |
| 1932 | } |
| 1933 | |
| 1934 | p->numa_scan_period = clamp(p->numa_scan_period + diff, |
| 1935 | task_scan_min(p), task_scan_max(p)); |
| 1936 | memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); |
| 1937 | } |
| 1938 | |
Rik van Riel | 7e2703e | 2014-01-27 17:03:45 -0500 | [diff] [blame] | 1939 | /* |
| 1940 | * Get the fraction of time the task has been running since the last |
| 1941 | * NUMA placement cycle. The scheduler keeps similar statistics, but |
| 1942 | * decays those on a 32ms period, which is orders of magnitude off |
| 1943 | * from the dozens-of-seconds NUMA balancing period. Use the scheduler |
| 1944 | * stats only if the task is so new there are no NUMA statistics yet. |
| 1945 | */ |
| 1946 | static u64 numa_get_avg_runtime(struct task_struct *p, u64 *period) |
| 1947 | { |
| 1948 | u64 runtime, delta, now; |
| 1949 | /* Use the start of this time slice to avoid calculations. */ |
| 1950 | now = p->se.exec_start; |
| 1951 | runtime = p->se.sum_exec_runtime; |
| 1952 | |
| 1953 | if (p->last_task_numa_placement) { |
| 1954 | delta = runtime - p->last_sum_exec_runtime; |
| 1955 | *period = now - p->last_task_numa_placement; |
| 1956 | } else { |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 1957 | delta = p->se.avg.load_sum / p->se.load.weight; |
| 1958 | *period = LOAD_AVG_MAX; |
Rik van Riel | 7e2703e | 2014-01-27 17:03:45 -0500 | [diff] [blame] | 1959 | } |
| 1960 | |
| 1961 | p->last_sum_exec_runtime = runtime; |
| 1962 | p->last_task_numa_placement = now; |
| 1963 | |
| 1964 | return delta; |
| 1965 | } |
| 1966 | |
Rik van Riel | 5400941 | 2014-10-17 03:29:53 -0400 | [diff] [blame] | 1967 | /* |
| 1968 | * Determine the preferred nid for a task in a numa_group. This needs to |
| 1969 | * be done in a way that produces consistent results with group_weight, |
| 1970 | * otherwise workloads might not converge. |
| 1971 | */ |
| 1972 | static int preferred_group_nid(struct task_struct *p, int nid) |
| 1973 | { |
| 1974 | nodemask_t nodes; |
| 1975 | int dist; |
| 1976 | |
| 1977 | /* Direct connections between all NUMA nodes. */ |
| 1978 | if (sched_numa_topology_type == NUMA_DIRECT) |
| 1979 | return nid; |
| 1980 | |
| 1981 | /* |
| 1982 | * On a system with glueless mesh NUMA topology, group_weight |
| 1983 | * scores nodes according to the number of NUMA hinting faults on |
| 1984 | * both the node itself, and on nearby nodes. |
| 1985 | */ |
| 1986 | if (sched_numa_topology_type == NUMA_GLUELESS_MESH) { |
| 1987 | unsigned long score, max_score = 0; |
| 1988 | int node, max_node = nid; |
| 1989 | |
| 1990 | dist = sched_max_numa_distance; |
| 1991 | |
| 1992 | for_each_online_node(node) { |
| 1993 | score = group_weight(p, node, dist); |
| 1994 | if (score > max_score) { |
| 1995 | max_score = score; |
| 1996 | max_node = node; |
| 1997 | } |
| 1998 | } |
| 1999 | return max_node; |
| 2000 | } |
| 2001 | |
| 2002 | /* |
| 2003 | * Finding the preferred nid in a system with NUMA backplane |
| 2004 | * interconnect topology is more involved. The goal is to locate |
| 2005 | * tasks from numa_groups near each other in the system, and |
| 2006 | * untangle workloads from different sides of the system. This requires |
| 2007 | * searching down the hierarchy of node groups, recursively searching |
| 2008 | * inside the highest scoring group of nodes. The nodemask tricks |
| 2009 | * keep the complexity of the search down. |
| 2010 | */ |
| 2011 | nodes = node_online_map; |
| 2012 | for (dist = sched_max_numa_distance; dist > LOCAL_DISTANCE; dist--) { |
| 2013 | unsigned long max_faults = 0; |
Jan Beulich | 8190747 | 2015-01-23 08:25:38 +0000 | [diff] [blame] | 2014 | nodemask_t max_group = NODE_MASK_NONE; |
Rik van Riel | 5400941 | 2014-10-17 03:29:53 -0400 | [diff] [blame] | 2015 | int a, b; |
| 2016 | |
| 2017 | /* Are there nodes at this distance from each other? */ |
| 2018 | if (!find_numa_distance(dist)) |
| 2019 | continue; |
| 2020 | |
| 2021 | for_each_node_mask(a, nodes) { |
| 2022 | unsigned long faults = 0; |
| 2023 | nodemask_t this_group; |
| 2024 | nodes_clear(this_group); |
| 2025 | |
| 2026 | /* Sum group's NUMA faults; includes a==b case. */ |
| 2027 | for_each_node_mask(b, nodes) { |
| 2028 | if (node_distance(a, b) < dist) { |
| 2029 | faults += group_faults(p, b); |
| 2030 | node_set(b, this_group); |
| 2031 | node_clear(b, nodes); |
| 2032 | } |
| 2033 | } |
| 2034 | |
| 2035 | /* Remember the top group. */ |
| 2036 | if (faults > max_faults) { |
| 2037 | max_faults = faults; |
| 2038 | max_group = this_group; |
| 2039 | /* |
| 2040 | * subtle: at the smallest distance there is |
| 2041 | * just one node left in each "group", the |
| 2042 | * winner is the preferred nid. |
| 2043 | */ |
| 2044 | nid = a; |
| 2045 | } |
| 2046 | } |
| 2047 | /* Next round, evaluate the nodes within max_group. */ |
Jan Beulich | 890a540 | 2015-02-09 12:30:00 +0100 | [diff] [blame] | 2048 | if (!max_faults) |
| 2049 | break; |
Rik van Riel | 5400941 | 2014-10-17 03:29:53 -0400 | [diff] [blame] | 2050 | nodes = max_group; |
| 2051 | } |
| 2052 | return nid; |
| 2053 | } |
| 2054 | |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 2055 | static void task_numa_placement(struct task_struct *p) |
| 2056 | { |
Mel Gorman | 83e1d2c | 2013-10-07 11:29:27 +0100 | [diff] [blame] | 2057 | int seq, nid, max_nid = -1, max_group_nid = -1; |
| 2058 | unsigned long max_faults = 0, max_group_faults = 0; |
Rik van Riel | 04bb2f9 | 2013-10-07 11:29:36 +0100 | [diff] [blame] | 2059 | unsigned long fault_types[2] = { 0, 0 }; |
Rik van Riel | 7e2703e | 2014-01-27 17:03:45 -0500 | [diff] [blame] | 2060 | unsigned long total_faults; |
| 2061 | u64 runtime, period; |
Mel Gorman | 7dbd13e | 2013-10-07 11:29:29 +0100 | [diff] [blame] | 2062 | spinlock_t *group_lock = NULL; |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 2063 | |
Jason Low | 7e5a2c1 | 2015-04-30 17:28:14 -0700 | [diff] [blame] | 2064 | /* |
| 2065 | * The p->mm->numa_scan_seq field gets updated without |
| 2066 | * exclusive access. Use READ_ONCE() here to ensure |
| 2067 | * that the field is read in a single access: |
| 2068 | */ |
Jason Low | 316c1608d | 2015-04-28 13:00:20 -0700 | [diff] [blame] | 2069 | seq = READ_ONCE(p->mm->numa_scan_seq); |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 2070 | if (p->numa_scan_seq == seq) |
| 2071 | return; |
| 2072 | p->numa_scan_seq = seq; |
Mel Gorman | 598f0ec | 2013-10-07 11:28:55 +0100 | [diff] [blame] | 2073 | p->numa_scan_period_max = task_scan_max(p); |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 2074 | |
Rik van Riel | 7e2703e | 2014-01-27 17:03:45 -0500 | [diff] [blame] | 2075 | total_faults = p->numa_faults_locality[0] + |
| 2076 | p->numa_faults_locality[1]; |
| 2077 | runtime = numa_get_avg_runtime(p, &period); |
| 2078 | |
Mel Gorman | 7dbd13e | 2013-10-07 11:29:29 +0100 | [diff] [blame] | 2079 | /* If the task is part of a group prevent parallel updates to group stats */ |
| 2080 | if (p->numa_group) { |
| 2081 | group_lock = &p->numa_group->lock; |
Mike Galbraith | 60e69ee | 2014-04-07 10:55:15 +0200 | [diff] [blame] | 2082 | spin_lock_irq(group_lock); |
Mel Gorman | 7dbd13e | 2013-10-07 11:29:29 +0100 | [diff] [blame] | 2083 | } |
| 2084 | |
Mel Gorman | 688b758 | 2013-10-07 11:28:58 +0100 | [diff] [blame] | 2085 | /* Find the node with the highest number of faults */ |
| 2086 | for_each_online_node(nid) { |
Iulia Manda | 44dba3d | 2014-10-31 02:13:31 +0200 | [diff] [blame] | 2087 | /* Keep track of the offsets in numa_faults array */ |
| 2088 | int mem_idx, membuf_idx, cpu_idx, cpubuf_idx; |
Mel Gorman | 83e1d2c | 2013-10-07 11:29:27 +0100 | [diff] [blame] | 2089 | unsigned long faults = 0, group_faults = 0; |
Iulia Manda | 44dba3d | 2014-10-31 02:13:31 +0200 | [diff] [blame] | 2090 | int priv; |
Mel Gorman | 745d614 | 2013-10-07 11:28:59 +0100 | [diff] [blame] | 2091 | |
Rik van Riel | be1e4e7 | 2014-01-27 17:03:48 -0500 | [diff] [blame] | 2092 | for (priv = 0; priv < NR_NUMA_HINT_FAULT_TYPES; priv++) { |
Rik van Riel | 7e2703e | 2014-01-27 17:03:45 -0500 | [diff] [blame] | 2093 | long diff, f_diff, f_weight; |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 2094 | |
Iulia Manda | 44dba3d | 2014-10-31 02:13:31 +0200 | [diff] [blame] | 2095 | mem_idx = task_faults_idx(NUMA_MEM, nid, priv); |
| 2096 | membuf_idx = task_faults_idx(NUMA_MEMBUF, nid, priv); |
| 2097 | cpu_idx = task_faults_idx(NUMA_CPU, nid, priv); |
| 2098 | cpubuf_idx = task_faults_idx(NUMA_CPUBUF, nid, priv); |
Mel Gorman | 745d614 | 2013-10-07 11:28:59 +0100 | [diff] [blame] | 2099 | |
Mel Gorman | ac8e895 | 2013-10-07 11:29:03 +0100 | [diff] [blame] | 2100 | /* Decay existing window, copy faults since last scan */ |
Iulia Manda | 44dba3d | 2014-10-31 02:13:31 +0200 | [diff] [blame] | 2101 | diff = p->numa_faults[membuf_idx] - p->numa_faults[mem_idx] / 2; |
| 2102 | fault_types[priv] += p->numa_faults[membuf_idx]; |
| 2103 | p->numa_faults[membuf_idx] = 0; |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 2104 | |
Rik van Riel | 7e2703e | 2014-01-27 17:03:45 -0500 | [diff] [blame] | 2105 | /* |
| 2106 | * Normalize the faults_from, so all tasks in a group |
| 2107 | * count according to CPU use, instead of by the raw |
| 2108 | * number of faults. Tasks with little runtime have |
| 2109 | * little over-all impact on throughput, and thus their |
| 2110 | * faults are less important. |
| 2111 | */ |
| 2112 | f_weight = div64_u64(runtime << 16, period + 1); |
Iulia Manda | 44dba3d | 2014-10-31 02:13:31 +0200 | [diff] [blame] | 2113 | f_weight = (f_weight * p->numa_faults[cpubuf_idx]) / |
Rik van Riel | 7e2703e | 2014-01-27 17:03:45 -0500 | [diff] [blame] | 2114 | (total_faults + 1); |
Iulia Manda | 44dba3d | 2014-10-31 02:13:31 +0200 | [diff] [blame] | 2115 | f_diff = f_weight - p->numa_faults[cpu_idx] / 2; |
| 2116 | p->numa_faults[cpubuf_idx] = 0; |
Rik van Riel | 50ec8a4 | 2014-01-27 17:03:42 -0500 | [diff] [blame] | 2117 | |
Iulia Manda | 44dba3d | 2014-10-31 02:13:31 +0200 | [diff] [blame] | 2118 | p->numa_faults[mem_idx] += diff; |
| 2119 | p->numa_faults[cpu_idx] += f_diff; |
| 2120 | faults += p->numa_faults[mem_idx]; |
Mel Gorman | 83e1d2c | 2013-10-07 11:29:27 +0100 | [diff] [blame] | 2121 | p->total_numa_faults += diff; |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 2122 | if (p->numa_group) { |
Iulia Manda | 44dba3d | 2014-10-31 02:13:31 +0200 | [diff] [blame] | 2123 | /* |
| 2124 | * safe because we can only change our own group |
| 2125 | * |
| 2126 | * mem_idx represents the offset for a given |
| 2127 | * nid and priv in a specific region because it |
| 2128 | * is at the beginning of the numa_faults array. |
| 2129 | */ |
| 2130 | p->numa_group->faults[mem_idx] += diff; |
| 2131 | p->numa_group->faults_cpu[mem_idx] += f_diff; |
Mel Gorman | 989348b | 2013-10-07 11:29:40 +0100 | [diff] [blame] | 2132 | p->numa_group->total_faults += diff; |
Iulia Manda | 44dba3d | 2014-10-31 02:13:31 +0200 | [diff] [blame] | 2133 | group_faults += p->numa_group->faults[mem_idx]; |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 2134 | } |
Mel Gorman | ac8e895 | 2013-10-07 11:29:03 +0100 | [diff] [blame] | 2135 | } |
| 2136 | |
Mel Gorman | 688b758 | 2013-10-07 11:28:58 +0100 | [diff] [blame] | 2137 | if (faults > max_faults) { |
| 2138 | max_faults = faults; |
| 2139 | max_nid = nid; |
| 2140 | } |
Mel Gorman | 83e1d2c | 2013-10-07 11:29:27 +0100 | [diff] [blame] | 2141 | |
| 2142 | if (group_faults > max_group_faults) { |
| 2143 | max_group_faults = group_faults; |
| 2144 | max_group_nid = nid; |
| 2145 | } |
| 2146 | } |
| 2147 | |
Rik van Riel | 04bb2f9 | 2013-10-07 11:29:36 +0100 | [diff] [blame] | 2148 | update_task_scan_period(p, fault_types[0], fault_types[1]); |
| 2149 | |
Mel Gorman | 7dbd13e | 2013-10-07 11:29:29 +0100 | [diff] [blame] | 2150 | if (p->numa_group) { |
Rik van Riel | 4142c3e | 2016-01-25 17:07:39 -0500 | [diff] [blame] | 2151 | numa_group_count_active_nodes(p->numa_group); |
Mike Galbraith | 60e69ee | 2014-04-07 10:55:15 +0200 | [diff] [blame] | 2152 | spin_unlock_irq(group_lock); |
Rik van Riel | 5400941 | 2014-10-17 03:29:53 -0400 | [diff] [blame] | 2153 | max_nid = preferred_group_nid(p, max_group_nid); |
Mel Gorman | 688b758 | 2013-10-07 11:28:58 +0100 | [diff] [blame] | 2154 | } |
| 2155 | |
Rik van Riel | bb97fc3 | 2014-06-04 16:33:15 -0400 | [diff] [blame] | 2156 | if (max_faults) { |
| 2157 | /* Set the new preferred node */ |
| 2158 | if (max_nid != p->numa_preferred_nid) |
| 2159 | sched_setnuma(p, max_nid); |
| 2160 | |
| 2161 | if (task_node(p) != p->numa_preferred_nid) |
| 2162 | numa_migrate_preferred(p); |
Mel Gorman | 3a7053b | 2013-10-07 11:29:00 +0100 | [diff] [blame] | 2163 | } |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 2164 | } |
| 2165 | |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 2166 | static inline int get_numa_group(struct numa_group *grp) |
| 2167 | { |
| 2168 | return atomic_inc_not_zero(&grp->refcount); |
| 2169 | } |
| 2170 | |
| 2171 | static inline void put_numa_group(struct numa_group *grp) |
| 2172 | { |
| 2173 | if (atomic_dec_and_test(&grp->refcount)) |
| 2174 | kfree_rcu(grp, rcu); |
| 2175 | } |
| 2176 | |
Mel Gorman | 3e6a941 | 2013-10-07 11:29:35 +0100 | [diff] [blame] | 2177 | static void task_numa_group(struct task_struct *p, int cpupid, int flags, |
| 2178 | int *priv) |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 2179 | { |
| 2180 | struct numa_group *grp, *my_grp; |
| 2181 | struct task_struct *tsk; |
| 2182 | bool join = false; |
| 2183 | int cpu = cpupid_to_cpu(cpupid); |
| 2184 | int i; |
| 2185 | |
| 2186 | if (unlikely(!p->numa_group)) { |
| 2187 | unsigned int size = sizeof(struct numa_group) + |
Rik van Riel | 50ec8a4 | 2014-01-27 17:03:42 -0500 | [diff] [blame] | 2188 | 4*nr_node_ids*sizeof(unsigned long); |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 2189 | |
| 2190 | grp = kzalloc(size, GFP_KERNEL | __GFP_NOWARN); |
| 2191 | if (!grp) |
| 2192 | return; |
| 2193 | |
| 2194 | atomic_set(&grp->refcount, 1); |
Rik van Riel | 4142c3e | 2016-01-25 17:07:39 -0500 | [diff] [blame] | 2195 | grp->active_nodes = 1; |
| 2196 | grp->max_faults_cpu = 0; |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 2197 | spin_lock_init(&grp->lock); |
Mel Gorman | e29cf08 | 2013-10-07 11:29:22 +0100 | [diff] [blame] | 2198 | grp->gid = p->pid; |
Rik van Riel | 50ec8a4 | 2014-01-27 17:03:42 -0500 | [diff] [blame] | 2199 | /* Second half of the array tracks nids where faults happen */ |
Rik van Riel | be1e4e7 | 2014-01-27 17:03:48 -0500 | [diff] [blame] | 2200 | grp->faults_cpu = grp->faults + NR_NUMA_HINT_FAULT_TYPES * |
| 2201 | nr_node_ids; |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 2202 | |
Rik van Riel | be1e4e7 | 2014-01-27 17:03:48 -0500 | [diff] [blame] | 2203 | for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) |
Iulia Manda | 44dba3d | 2014-10-31 02:13:31 +0200 | [diff] [blame] | 2204 | grp->faults[i] = p->numa_faults[i]; |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 2205 | |
Mel Gorman | 989348b | 2013-10-07 11:29:40 +0100 | [diff] [blame] | 2206 | grp->total_faults = p->total_numa_faults; |
Mel Gorman | 83e1d2c | 2013-10-07 11:29:27 +0100 | [diff] [blame] | 2207 | |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 2208 | grp->nr_tasks++; |
| 2209 | rcu_assign_pointer(p->numa_group, grp); |
| 2210 | } |
| 2211 | |
| 2212 | rcu_read_lock(); |
Jason Low | 316c1608d | 2015-04-28 13:00:20 -0700 | [diff] [blame] | 2213 | tsk = READ_ONCE(cpu_rq(cpu)->curr); |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 2214 | |
| 2215 | if (!cpupid_match_pid(tsk, cpupid)) |
Peter Zijlstra | 3354781 | 2013-10-09 10:24:48 +0200 | [diff] [blame] | 2216 | goto no_join; |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 2217 | |
| 2218 | grp = rcu_dereference(tsk->numa_group); |
| 2219 | if (!grp) |
Peter Zijlstra | 3354781 | 2013-10-09 10:24:48 +0200 | [diff] [blame] | 2220 | goto no_join; |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 2221 | |
| 2222 | my_grp = p->numa_group; |
| 2223 | if (grp == my_grp) |
Peter Zijlstra | 3354781 | 2013-10-09 10:24:48 +0200 | [diff] [blame] | 2224 | goto no_join; |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 2225 | |
| 2226 | /* |
| 2227 | * Only join the other group if its bigger; if we're the bigger group, |
| 2228 | * the other task will join us. |
| 2229 | */ |
| 2230 | if (my_grp->nr_tasks > grp->nr_tasks) |
Peter Zijlstra | 3354781 | 2013-10-09 10:24:48 +0200 | [diff] [blame] | 2231 | goto no_join; |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 2232 | |
| 2233 | /* |
| 2234 | * Tie-break on the grp address. |
| 2235 | */ |
| 2236 | if (my_grp->nr_tasks == grp->nr_tasks && my_grp > grp) |
Peter Zijlstra | 3354781 | 2013-10-09 10:24:48 +0200 | [diff] [blame] | 2237 | goto no_join; |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 2238 | |
Rik van Riel | dabe1d9 | 2013-10-07 11:29:34 +0100 | [diff] [blame] | 2239 | /* Always join threads in the same process. */ |
| 2240 | if (tsk->mm == current->mm) |
| 2241 | join = true; |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 2242 | |
Rik van Riel | dabe1d9 | 2013-10-07 11:29:34 +0100 | [diff] [blame] | 2243 | /* Simple filter to avoid false positives due to PID collisions */ |
| 2244 | if (flags & TNF_SHARED) |
| 2245 | join = true; |
| 2246 | |
Mel Gorman | 3e6a941 | 2013-10-07 11:29:35 +0100 | [diff] [blame] | 2247 | /* Update priv based on whether false sharing was detected */ |
| 2248 | *priv = !join; |
| 2249 | |
Rik van Riel | dabe1d9 | 2013-10-07 11:29:34 +0100 | [diff] [blame] | 2250 | if (join && !get_numa_group(grp)) |
Peter Zijlstra | 3354781 | 2013-10-09 10:24:48 +0200 | [diff] [blame] | 2251 | goto no_join; |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 2252 | |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 2253 | rcu_read_unlock(); |
| 2254 | |
| 2255 | if (!join) |
| 2256 | return; |
| 2257 | |
Mike Galbraith | 60e69ee | 2014-04-07 10:55:15 +0200 | [diff] [blame] | 2258 | BUG_ON(irqs_disabled()); |
| 2259 | double_lock_irq(&my_grp->lock, &grp->lock); |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 2260 | |
Rik van Riel | be1e4e7 | 2014-01-27 17:03:48 -0500 | [diff] [blame] | 2261 | for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) { |
Iulia Manda | 44dba3d | 2014-10-31 02:13:31 +0200 | [diff] [blame] | 2262 | my_grp->faults[i] -= p->numa_faults[i]; |
| 2263 | grp->faults[i] += p->numa_faults[i]; |
Mel Gorman | 989348b | 2013-10-07 11:29:40 +0100 | [diff] [blame] | 2264 | } |
| 2265 | my_grp->total_faults -= p->total_numa_faults; |
| 2266 | grp->total_faults += p->total_numa_faults; |
| 2267 | |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 2268 | my_grp->nr_tasks--; |
| 2269 | grp->nr_tasks++; |
| 2270 | |
| 2271 | spin_unlock(&my_grp->lock); |
Mike Galbraith | 60e69ee | 2014-04-07 10:55:15 +0200 | [diff] [blame] | 2272 | spin_unlock_irq(&grp->lock); |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 2273 | |
| 2274 | rcu_assign_pointer(p->numa_group, grp); |
| 2275 | |
| 2276 | put_numa_group(my_grp); |
Peter Zijlstra | 3354781 | 2013-10-09 10:24:48 +0200 | [diff] [blame] | 2277 | return; |
| 2278 | |
| 2279 | no_join: |
| 2280 | rcu_read_unlock(); |
| 2281 | return; |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 2282 | } |
| 2283 | |
| 2284 | void task_numa_free(struct task_struct *p) |
| 2285 | { |
| 2286 | struct numa_group *grp = p->numa_group; |
Iulia Manda | 44dba3d | 2014-10-31 02:13:31 +0200 | [diff] [blame] | 2287 | void *numa_faults = p->numa_faults; |
Steven Rostedt | e9dd685 | 2014-05-27 17:02:04 -0400 | [diff] [blame] | 2288 | unsigned long flags; |
| 2289 | int i; |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 2290 | |
| 2291 | if (grp) { |
Steven Rostedt | e9dd685 | 2014-05-27 17:02:04 -0400 | [diff] [blame] | 2292 | spin_lock_irqsave(&grp->lock, flags); |
Rik van Riel | be1e4e7 | 2014-01-27 17:03:48 -0500 | [diff] [blame] | 2293 | for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) |
Iulia Manda | 44dba3d | 2014-10-31 02:13:31 +0200 | [diff] [blame] | 2294 | grp->faults[i] -= p->numa_faults[i]; |
Mel Gorman | 989348b | 2013-10-07 11:29:40 +0100 | [diff] [blame] | 2295 | grp->total_faults -= p->total_numa_faults; |
| 2296 | |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 2297 | grp->nr_tasks--; |
Steven Rostedt | e9dd685 | 2014-05-27 17:02:04 -0400 | [diff] [blame] | 2298 | spin_unlock_irqrestore(&grp->lock, flags); |
Andreea-Cristina Bernat | 35b123e | 2014-08-22 17:50:43 +0300 | [diff] [blame] | 2299 | RCU_INIT_POINTER(p->numa_group, NULL); |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 2300 | put_numa_group(grp); |
| 2301 | } |
| 2302 | |
Iulia Manda | 44dba3d | 2014-10-31 02:13:31 +0200 | [diff] [blame] | 2303 | p->numa_faults = NULL; |
Rik van Riel | 8272701 | 2013-10-07 11:29:28 +0100 | [diff] [blame] | 2304 | kfree(numa_faults); |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 2305 | } |
| 2306 | |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 2307 | /* |
| 2308 | * Got a PROT_NONE fault for a page on @node. |
| 2309 | */ |
Rik van Riel | 58b46da | 2014-01-27 17:03:47 -0500 | [diff] [blame] | 2310 | void task_numa_fault(int last_cpupid, int mem_node, int pages, int flags) |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 2311 | { |
| 2312 | struct task_struct *p = current; |
Peter Zijlstra | 6688cc0 | 2013-10-07 11:29:24 +0100 | [diff] [blame] | 2313 | bool migrated = flags & TNF_MIGRATED; |
Rik van Riel | 58b46da | 2014-01-27 17:03:47 -0500 | [diff] [blame] | 2314 | int cpu_node = task_node(current); |
Rik van Riel | 792568e | 2014-04-11 13:00:27 -0400 | [diff] [blame] | 2315 | int local = !!(flags & TNF_FAULT_LOCAL); |
Rik van Riel | 4142c3e | 2016-01-25 17:07:39 -0500 | [diff] [blame] | 2316 | struct numa_group *ng; |
Mel Gorman | ac8e895 | 2013-10-07 11:29:03 +0100 | [diff] [blame] | 2317 | int priv; |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 2318 | |
Srikar Dronamraju | 2a59572 | 2015-08-11 21:54:21 +0530 | [diff] [blame] | 2319 | if (!static_branch_likely(&sched_numa_balancing)) |
Mel Gorman | 1a687c2 | 2012-11-22 11:16:36 +0000 | [diff] [blame] | 2320 | return; |
| 2321 | |
Mel Gorman | 9ff1d9f | 2013-10-07 11:29:04 +0100 | [diff] [blame] | 2322 | /* for example, ksmd faulting in a user's mm */ |
| 2323 | if (!p->mm) |
| 2324 | return; |
| 2325 | |
Mel Gorman | f809ca9 | 2013-10-07 11:28:57 +0100 | [diff] [blame] | 2326 | /* Allocate buffer to track faults on a per-node basis */ |
Iulia Manda | 44dba3d | 2014-10-31 02:13:31 +0200 | [diff] [blame] | 2327 | if (unlikely(!p->numa_faults)) { |
| 2328 | int size = sizeof(*p->numa_faults) * |
Rik van Riel | be1e4e7 | 2014-01-27 17:03:48 -0500 | [diff] [blame] | 2329 | NR_NUMA_HINT_FAULT_BUCKETS * nr_node_ids; |
Mel Gorman | f809ca9 | 2013-10-07 11:28:57 +0100 | [diff] [blame] | 2330 | |
Iulia Manda | 44dba3d | 2014-10-31 02:13:31 +0200 | [diff] [blame] | 2331 | p->numa_faults = kzalloc(size, GFP_KERNEL|__GFP_NOWARN); |
| 2332 | if (!p->numa_faults) |
Mel Gorman | f809ca9 | 2013-10-07 11:28:57 +0100 | [diff] [blame] | 2333 | return; |
Mel Gorman | 745d614 | 2013-10-07 11:28:59 +0100 | [diff] [blame] | 2334 | |
Mel Gorman | 83e1d2c | 2013-10-07 11:29:27 +0100 | [diff] [blame] | 2335 | p->total_numa_faults = 0; |
Rik van Riel | 04bb2f9 | 2013-10-07 11:29:36 +0100 | [diff] [blame] | 2336 | memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality)); |
Mel Gorman | f809ca9 | 2013-10-07 11:28:57 +0100 | [diff] [blame] | 2337 | } |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 2338 | |
Mel Gorman | fb003b8 | 2012-11-15 09:01:14 +0000 | [diff] [blame] | 2339 | /* |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 2340 | * First accesses are treated as private, otherwise consider accesses |
| 2341 | * to be private if the accessing pid has not changed |
| 2342 | */ |
| 2343 | if (unlikely(last_cpupid == (-1 & LAST_CPUPID_MASK))) { |
| 2344 | priv = 1; |
| 2345 | } else { |
| 2346 | priv = cpupid_match_pid(p, last_cpupid); |
Peter Zijlstra | 6688cc0 | 2013-10-07 11:29:24 +0100 | [diff] [blame] | 2347 | if (!priv && !(flags & TNF_NO_GROUP)) |
Mel Gorman | 3e6a941 | 2013-10-07 11:29:35 +0100 | [diff] [blame] | 2348 | task_numa_group(p, last_cpupid, flags, &priv); |
Peter Zijlstra | 8c8a743 | 2013-10-07 11:29:21 +0100 | [diff] [blame] | 2349 | } |
| 2350 | |
Rik van Riel | 792568e | 2014-04-11 13:00:27 -0400 | [diff] [blame] | 2351 | /* |
| 2352 | * If a workload spans multiple NUMA nodes, a shared fault that |
| 2353 | * occurs wholly within the set of nodes that the workload is |
| 2354 | * actively using should be counted as local. This allows the |
| 2355 | * scan rate to slow down when a workload has settled down. |
| 2356 | */ |
Rik van Riel | 4142c3e | 2016-01-25 17:07:39 -0500 | [diff] [blame] | 2357 | ng = p->numa_group; |
| 2358 | if (!priv && !local && ng && ng->active_nodes > 1 && |
| 2359 | numa_is_active_node(cpu_node, ng) && |
| 2360 | numa_is_active_node(mem_node, ng)) |
Rik van Riel | 792568e | 2014-04-11 13:00:27 -0400 | [diff] [blame] | 2361 | local = 1; |
| 2362 | |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 2363 | task_numa_placement(p); |
Mel Gorman | f809ca9 | 2013-10-07 11:28:57 +0100 | [diff] [blame] | 2364 | |
Rik van Riel | 2739d3e | 2013-10-07 11:29:41 +0100 | [diff] [blame] | 2365 | /* |
| 2366 | * Retry task to preferred node migration periodically, in case it |
| 2367 | * case it previously failed, or the scheduler moved us. |
| 2368 | */ |
| 2369 | if (time_after(jiffies, p->numa_migrate_retry)) |
Mel Gorman | 6b9a746 | 2013-10-07 11:29:11 +0100 | [diff] [blame] | 2370 | numa_migrate_preferred(p); |
| 2371 | |
Ingo Molnar | b32e86b | 2013-10-07 11:29:30 +0100 | [diff] [blame] | 2372 | if (migrated) |
| 2373 | p->numa_pages_migrated += pages; |
Mel Gorman | 074c238 | 2015-03-25 15:55:42 -0700 | [diff] [blame] | 2374 | if (flags & TNF_MIGRATE_FAIL) |
| 2375 | p->numa_faults_locality[2] += pages; |
Ingo Molnar | b32e86b | 2013-10-07 11:29:30 +0100 | [diff] [blame] | 2376 | |
Iulia Manda | 44dba3d | 2014-10-31 02:13:31 +0200 | [diff] [blame] | 2377 | p->numa_faults[task_faults_idx(NUMA_MEMBUF, mem_node, priv)] += pages; |
| 2378 | p->numa_faults[task_faults_idx(NUMA_CPUBUF, cpu_node, priv)] += pages; |
Rik van Riel | 792568e | 2014-04-11 13:00:27 -0400 | [diff] [blame] | 2379 | p->numa_faults_locality[local] += pages; |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 2380 | } |
| 2381 | |
Peter Zijlstra | 6e5fb22 | 2012-10-25 14:16:45 +0200 | [diff] [blame] | 2382 | static void reset_ptenuma_scan(struct task_struct *p) |
| 2383 | { |
Jason Low | 7e5a2c1 | 2015-04-30 17:28:14 -0700 | [diff] [blame] | 2384 | /* |
| 2385 | * We only did a read acquisition of the mmap sem, so |
| 2386 | * p->mm->numa_scan_seq is written to without exclusive access |
| 2387 | * and the update is not guaranteed to be atomic. That's not |
| 2388 | * much of an issue though, since this is just used for |
| 2389 | * statistical sampling. Use READ_ONCE/WRITE_ONCE, which are not |
| 2390 | * expensive, to avoid any form of compiler optimizations: |
| 2391 | */ |
Jason Low | 316c1608d | 2015-04-28 13:00:20 -0700 | [diff] [blame] | 2392 | WRITE_ONCE(p->mm->numa_scan_seq, READ_ONCE(p->mm->numa_scan_seq) + 1); |
Peter Zijlstra | 6e5fb22 | 2012-10-25 14:16:45 +0200 | [diff] [blame] | 2393 | p->mm->numa_scan_offset = 0; |
| 2394 | } |
| 2395 | |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 2396 | /* |
| 2397 | * The expensive part of numa migration is done from task_work context. |
| 2398 | * Triggered from task_tick_numa(). |
| 2399 | */ |
| 2400 | void task_numa_work(struct callback_head *work) |
| 2401 | { |
| 2402 | unsigned long migrate, next_scan, now = jiffies; |
| 2403 | struct task_struct *p = current; |
| 2404 | struct mm_struct *mm = p->mm; |
Rik van Riel | 5117084 | 2015-11-05 15:56:23 -0500 | [diff] [blame] | 2405 | u64 runtime = p->se.sum_exec_runtime; |
Peter Zijlstra | 6e5fb22 | 2012-10-25 14:16:45 +0200 | [diff] [blame] | 2406 | struct vm_area_struct *vma; |
Mel Gorman | 9f40604 | 2012-11-14 18:34:32 +0000 | [diff] [blame] | 2407 | unsigned long start, end; |
Mel Gorman | 598f0ec | 2013-10-07 11:28:55 +0100 | [diff] [blame] | 2408 | unsigned long nr_pte_updates = 0; |
Rik van Riel | 4620f8c | 2015-09-11 09:00:27 -0400 | [diff] [blame] | 2409 | long pages, virtpages; |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 2410 | |
Peter Zijlstra | 9148a3a | 2016-09-20 22:34:51 +0200 | [diff] [blame] | 2411 | SCHED_WARN_ON(p != container_of(work, struct task_struct, numa_work)); |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 2412 | |
| 2413 | work->next = work; /* protect against double add */ |
| 2414 | /* |
| 2415 | * Who cares about NUMA placement when they're dying. |
| 2416 | * |
| 2417 | * NOTE: make sure not to dereference p->mm before this check, |
| 2418 | * exit_task_work() happens _after_ exit_mm() so we could be called |
| 2419 | * without p->mm even though we still had it when we enqueued this |
| 2420 | * work. |
| 2421 | */ |
| 2422 | if (p->flags & PF_EXITING) |
| 2423 | return; |
| 2424 | |
Mel Gorman | 930aa17 | 2013-10-07 11:29:37 +0100 | [diff] [blame] | 2425 | if (!mm->numa_next_scan) { |
Mel Gorman | 7e8d16b | 2013-10-07 11:28:54 +0100 | [diff] [blame] | 2426 | mm->numa_next_scan = now + |
| 2427 | msecs_to_jiffies(sysctl_numa_balancing_scan_delay); |
Mel Gorman | b8593bf | 2012-11-21 01:18:23 +0000 | [diff] [blame] | 2428 | } |
| 2429 | |
| 2430 | /* |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 2431 | * Enforce maximal scan/migration frequency.. |
| 2432 | */ |
| 2433 | migrate = mm->numa_next_scan; |
| 2434 | if (time_before(now, migrate)) |
| 2435 | return; |
| 2436 | |
Mel Gorman | 598f0ec | 2013-10-07 11:28:55 +0100 | [diff] [blame] | 2437 | if (p->numa_scan_period == 0) { |
| 2438 | p->numa_scan_period_max = task_scan_max(p); |
| 2439 | p->numa_scan_period = task_scan_min(p); |
| 2440 | } |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 2441 | |
Mel Gorman | fb003b8 | 2012-11-15 09:01:14 +0000 | [diff] [blame] | 2442 | next_scan = now + msecs_to_jiffies(p->numa_scan_period); |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 2443 | if (cmpxchg(&mm->numa_next_scan, migrate, next_scan) != migrate) |
| 2444 | return; |
| 2445 | |
Mel Gorman | e14808b | 2012-11-19 10:59:15 +0000 | [diff] [blame] | 2446 | /* |
Peter Zijlstra | 19a78d1 | 2013-10-07 11:28:51 +0100 | [diff] [blame] | 2447 | * Delay this task enough that another task of this mm will likely win |
| 2448 | * the next time around. |
| 2449 | */ |
| 2450 | p->node_stamp += 2 * TICK_NSEC; |
| 2451 | |
Mel Gorman | 9f40604 | 2012-11-14 18:34:32 +0000 | [diff] [blame] | 2452 | start = mm->numa_scan_offset; |
| 2453 | pages = sysctl_numa_balancing_scan_size; |
| 2454 | pages <<= 20 - PAGE_SHIFT; /* MB in pages */ |
Rik van Riel | 4620f8c | 2015-09-11 09:00:27 -0400 | [diff] [blame] | 2455 | virtpages = pages * 8; /* Scan up to this much virtual space */ |
Mel Gorman | 9f40604 | 2012-11-14 18:34:32 +0000 | [diff] [blame] | 2456 | if (!pages) |
| 2457 | return; |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 2458 | |
Rik van Riel | 4620f8c | 2015-09-11 09:00:27 -0400 | [diff] [blame] | 2459 | |
Peter Zijlstra | 6e5fb22 | 2012-10-25 14:16:45 +0200 | [diff] [blame] | 2460 | down_read(&mm->mmap_sem); |
Mel Gorman | 9f40604 | 2012-11-14 18:34:32 +0000 | [diff] [blame] | 2461 | vma = find_vma(mm, start); |
Peter Zijlstra | 6e5fb22 | 2012-10-25 14:16:45 +0200 | [diff] [blame] | 2462 | if (!vma) { |
| 2463 | reset_ptenuma_scan(p); |
Mel Gorman | 9f40604 | 2012-11-14 18:34:32 +0000 | [diff] [blame] | 2464 | start = 0; |
Peter Zijlstra | 6e5fb22 | 2012-10-25 14:16:45 +0200 | [diff] [blame] | 2465 | vma = mm->mmap; |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 2466 | } |
Mel Gorman | 9f40604 | 2012-11-14 18:34:32 +0000 | [diff] [blame] | 2467 | for (; vma; vma = vma->vm_next) { |
Naoya Horiguchi | 6b79c57 | 2015-04-07 14:26:47 -0700 | [diff] [blame] | 2468 | if (!vma_migratable(vma) || !vma_policy_mof(vma) || |
Mel Gorman | 8e76d4e | 2015-06-10 11:15:00 -0700 | [diff] [blame] | 2469 | is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_MIXEDMAP)) { |
Peter Zijlstra | 6e5fb22 | 2012-10-25 14:16:45 +0200 | [diff] [blame] | 2470 | continue; |
Naoya Horiguchi | 6b79c57 | 2015-04-07 14:26:47 -0700 | [diff] [blame] | 2471 | } |
Peter Zijlstra | 6e5fb22 | 2012-10-25 14:16:45 +0200 | [diff] [blame] | 2472 | |
Mel Gorman | 4591ce4f | 2013-10-07 11:29:13 +0100 | [diff] [blame] | 2473 | /* |
| 2474 | * Shared library pages mapped by multiple processes are not |
| 2475 | * migrated as it is expected they are cache replicated. Avoid |
| 2476 | * hinting faults in read-only file-backed mappings or the vdso |
| 2477 | * as migrating the pages will be of marginal benefit. |
| 2478 | */ |
| 2479 | if (!vma->vm_mm || |
| 2480 | (vma->vm_file && (vma->vm_flags & (VM_READ|VM_WRITE)) == (VM_READ))) |
| 2481 | continue; |
| 2482 | |
Mel Gorman | 3c67f47 | 2013-12-18 17:08:40 -0800 | [diff] [blame] | 2483 | /* |
| 2484 | * Skip inaccessible VMAs to avoid any confusion between |
| 2485 | * PROT_NONE and NUMA hinting ptes |
| 2486 | */ |
| 2487 | if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))) |
| 2488 | continue; |
| 2489 | |
Mel Gorman | 9f40604 | 2012-11-14 18:34:32 +0000 | [diff] [blame] | 2490 | do { |
| 2491 | start = max(start, vma->vm_start); |
| 2492 | end = ALIGN(start + (pages << PAGE_SHIFT), HPAGE_SIZE); |
| 2493 | end = min(end, vma->vm_end); |
Rik van Riel | 4620f8c | 2015-09-11 09:00:27 -0400 | [diff] [blame] | 2494 | nr_pte_updates = change_prot_numa(vma, start, end); |
Mel Gorman | 598f0ec | 2013-10-07 11:28:55 +0100 | [diff] [blame] | 2495 | |
| 2496 | /* |
Rik van Riel | 4620f8c | 2015-09-11 09:00:27 -0400 | [diff] [blame] | 2497 | * Try to scan sysctl_numa_balancing_size worth of |
| 2498 | * hpages that have at least one present PTE that |
| 2499 | * is not already pte-numa. If the VMA contains |
| 2500 | * areas that are unused or already full of prot_numa |
| 2501 | * PTEs, scan up to virtpages, to skip through those |
| 2502 | * areas faster. |
Mel Gorman | 598f0ec | 2013-10-07 11:28:55 +0100 | [diff] [blame] | 2503 | */ |
| 2504 | if (nr_pte_updates) |
| 2505 | pages -= (end - start) >> PAGE_SHIFT; |
Rik van Riel | 4620f8c | 2015-09-11 09:00:27 -0400 | [diff] [blame] | 2506 | virtpages -= (end - start) >> PAGE_SHIFT; |
Peter Zijlstra | 6e5fb22 | 2012-10-25 14:16:45 +0200 | [diff] [blame] | 2507 | |
Mel Gorman | 9f40604 | 2012-11-14 18:34:32 +0000 | [diff] [blame] | 2508 | start = end; |
Rik van Riel | 4620f8c | 2015-09-11 09:00:27 -0400 | [diff] [blame] | 2509 | if (pages <= 0 || virtpages <= 0) |
Mel Gorman | 9f40604 | 2012-11-14 18:34:32 +0000 | [diff] [blame] | 2510 | goto out; |
Rik van Riel | 3cf1962 | 2014-02-18 17:12:44 -0500 | [diff] [blame] | 2511 | |
| 2512 | cond_resched(); |
Mel Gorman | 9f40604 | 2012-11-14 18:34:32 +0000 | [diff] [blame] | 2513 | } while (end != vma->vm_end); |
Peter Zijlstra | 6e5fb22 | 2012-10-25 14:16:45 +0200 | [diff] [blame] | 2514 | } |
| 2515 | |
Mel Gorman | 9f40604 | 2012-11-14 18:34:32 +0000 | [diff] [blame] | 2516 | out: |
Peter Zijlstra | 6e5fb22 | 2012-10-25 14:16:45 +0200 | [diff] [blame] | 2517 | /* |
Peter Zijlstra | c69307d | 2013-10-07 11:28:41 +0100 | [diff] [blame] | 2518 | * It is possible to reach the end of the VMA list but the last few |
| 2519 | * VMAs are not guaranteed to the vma_migratable. If they are not, we |
| 2520 | * would find the !migratable VMA on the next scan but not reset the |
| 2521 | * scanner to the start so check it now. |
Peter Zijlstra | 6e5fb22 | 2012-10-25 14:16:45 +0200 | [diff] [blame] | 2522 | */ |
| 2523 | if (vma) |
Mel Gorman | 9f40604 | 2012-11-14 18:34:32 +0000 | [diff] [blame] | 2524 | mm->numa_scan_offset = start; |
Peter Zijlstra | 6e5fb22 | 2012-10-25 14:16:45 +0200 | [diff] [blame] | 2525 | else |
| 2526 | reset_ptenuma_scan(p); |
| 2527 | up_read(&mm->mmap_sem); |
Rik van Riel | 5117084 | 2015-11-05 15:56:23 -0500 | [diff] [blame] | 2528 | |
| 2529 | /* |
| 2530 | * Make sure tasks use at least 32x as much time to run other code |
| 2531 | * than they used here, to limit NUMA PTE scanning overhead to 3% max. |
| 2532 | * Usually update_task_scan_period slows down scanning enough; on an |
| 2533 | * overloaded system we need to limit overhead on a per task basis. |
| 2534 | */ |
| 2535 | if (unlikely(p->se.sum_exec_runtime != runtime)) { |
| 2536 | u64 diff = p->se.sum_exec_runtime - runtime; |
| 2537 | p->node_stamp += 32 * diff; |
| 2538 | } |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 2539 | } |
| 2540 | |
| 2541 | /* |
| 2542 | * Drive the periodic memory faults.. |
| 2543 | */ |
| 2544 | void task_tick_numa(struct rq *rq, struct task_struct *curr) |
| 2545 | { |
| 2546 | struct callback_head *work = &curr->numa_work; |
| 2547 | u64 period, now; |
| 2548 | |
| 2549 | /* |
| 2550 | * We don't care about NUMA placement if we don't have memory. |
| 2551 | */ |
| 2552 | if (!curr->mm || (curr->flags & PF_EXITING) || work->next != work) |
| 2553 | return; |
| 2554 | |
| 2555 | /* |
| 2556 | * Using runtime rather than walltime has the dual advantage that |
| 2557 | * we (mostly) drive the selection from busy threads and that the |
| 2558 | * task needs to have done some actual work before we bother with |
| 2559 | * NUMA placement. |
| 2560 | */ |
| 2561 | now = curr->se.sum_exec_runtime; |
| 2562 | period = (u64)curr->numa_scan_period * NSEC_PER_MSEC; |
| 2563 | |
Rik van Riel | 25b3e5a | 2015-11-05 15:56:22 -0500 | [diff] [blame] | 2564 | if (now > curr->node_stamp + period) { |
Peter Zijlstra | 4b96a29 | 2012-10-25 14:16:47 +0200 | [diff] [blame] | 2565 | if (!curr->node_stamp) |
Mel Gorman | 598f0ec | 2013-10-07 11:28:55 +0100 | [diff] [blame] | 2566 | curr->numa_scan_period = task_scan_min(curr); |
Peter Zijlstra | 19a78d1 | 2013-10-07 11:28:51 +0100 | [diff] [blame] | 2567 | curr->node_stamp += period; |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 2568 | |
| 2569 | if (!time_before(jiffies, curr->mm->numa_next_scan)) { |
| 2570 | init_task_work(work, task_numa_work); /* TODO: move this into sched_fork() */ |
| 2571 | task_work_add(curr, work, true); |
| 2572 | } |
| 2573 | } |
| 2574 | } |
| 2575 | #else |
| 2576 | static void task_tick_numa(struct rq *rq, struct task_struct *curr) |
| 2577 | { |
| 2578 | } |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 2579 | |
| 2580 | static inline void account_numa_enqueue(struct rq *rq, struct task_struct *p) |
| 2581 | { |
| 2582 | } |
| 2583 | |
| 2584 | static inline void account_numa_dequeue(struct rq *rq, struct task_struct *p) |
| 2585 | { |
| 2586 | } |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 2587 | #endif /* CONFIG_NUMA_BALANCING */ |
| 2588 | |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 2589 | static void |
| 2590 | account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| 2591 | { |
| 2592 | update_load_add(&cfs_rq->load, se->load.weight); |
Peter Zijlstra | c09595f | 2008-06-27 13:41:14 +0200 | [diff] [blame] | 2593 | if (!parent_entity(se)) |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 2594 | update_load_add(&rq_of(cfs_rq)->load, se->load.weight); |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 2595 | #ifdef CONFIG_SMP |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 2596 | if (entity_is_task(se)) { |
| 2597 | struct rq *rq = rq_of(cfs_rq); |
| 2598 | |
| 2599 | account_numa_enqueue(rq, task_of(se)); |
| 2600 | list_add(&se->group_node, &rq->cfs_tasks); |
| 2601 | } |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 2602 | #endif |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 2603 | cfs_rq->nr_running++; |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 2604 | } |
| 2605 | |
| 2606 | static void |
| 2607 | account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| 2608 | { |
| 2609 | update_load_sub(&cfs_rq->load, se->load.weight); |
Peter Zijlstra | c09595f | 2008-06-27 13:41:14 +0200 | [diff] [blame] | 2610 | if (!parent_entity(se)) |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 2611 | update_load_sub(&rq_of(cfs_rq)->load, se->load.weight); |
Tim Chen | bfdb198 | 2016-02-01 14:47:59 -0800 | [diff] [blame] | 2612 | #ifdef CONFIG_SMP |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 2613 | if (entity_is_task(se)) { |
| 2614 | account_numa_dequeue(rq_of(cfs_rq), task_of(se)); |
Bharata B Rao | b87f172 | 2008-09-25 09:53:54 +0530 | [diff] [blame] | 2615 | list_del_init(&se->group_node); |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 2616 | } |
Tim Chen | bfdb198 | 2016-02-01 14:47:59 -0800 | [diff] [blame] | 2617 | #endif |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 2618 | cfs_rq->nr_running--; |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 2619 | } |
| 2620 | |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 2621 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 2622 | # ifdef CONFIG_SMP |
Paul Turner | 6d5ab29 | 2011-01-21 20:45:01 -0800 | [diff] [blame] | 2623 | static long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg) |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 2624 | { |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 2625 | long tg_weight, load, shares; |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 2626 | |
Peter Zijlstra | ea1dc6f | 2016-06-24 16:11:02 +0200 | [diff] [blame] | 2627 | /* |
| 2628 | * This really should be: cfs_rq->avg.load_avg, but instead we use |
| 2629 | * cfs_rq->load.weight, which is its upper bound. This helps ramp up |
| 2630 | * the shares for small weight interactive tasks. |
| 2631 | */ |
| 2632 | load = scale_load_down(cfs_rq->load.weight); |
| 2633 | |
| 2634 | tg_weight = atomic_long_read(&tg->load_avg); |
| 2635 | |
| 2636 | /* Ensure tg_weight >= load */ |
| 2637 | tg_weight -= cfs_rq->tg_load_avg_contrib; |
| 2638 | tg_weight += load; |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 2639 | |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 2640 | shares = (tg->shares * load); |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 2641 | if (tg_weight) |
| 2642 | shares /= tg_weight; |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 2643 | |
| 2644 | if (shares < MIN_SHARES) |
| 2645 | shares = MIN_SHARES; |
| 2646 | if (shares > tg->shares) |
| 2647 | shares = tg->shares; |
| 2648 | |
| 2649 | return shares; |
| 2650 | } |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 2651 | # else /* CONFIG_SMP */ |
Paul Turner | 6d5ab29 | 2011-01-21 20:45:01 -0800 | [diff] [blame] | 2652 | static inline long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg) |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 2653 | { |
| 2654 | return tg->shares; |
| 2655 | } |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 2656 | # endif /* CONFIG_SMP */ |
Peter Zijlstra | ea1dc6f | 2016-06-24 16:11:02 +0200 | [diff] [blame] | 2657 | |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 2658 | static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, |
| 2659 | unsigned long weight) |
| 2660 | { |
Paul Turner | 19e5eeb | 2010-12-15 19:10:18 -0800 | [diff] [blame] | 2661 | if (se->on_rq) { |
| 2662 | /* commit outstanding execution time */ |
| 2663 | if (cfs_rq->curr == se) |
| 2664 | update_curr(cfs_rq); |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 2665 | account_entity_dequeue(cfs_rq, se); |
Paul Turner | 19e5eeb | 2010-12-15 19:10:18 -0800 | [diff] [blame] | 2666 | } |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 2667 | |
| 2668 | update_load_set(&se->load, weight); |
| 2669 | |
| 2670 | if (se->on_rq) |
| 2671 | account_entity_enqueue(cfs_rq, se); |
| 2672 | } |
| 2673 | |
Paul Turner | 8295836 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 2674 | static inline int throttled_hierarchy(struct cfs_rq *cfs_rq); |
| 2675 | |
Paul Turner | 6d5ab29 | 2011-01-21 20:45:01 -0800 | [diff] [blame] | 2676 | static void update_cfs_shares(struct cfs_rq *cfs_rq) |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 2677 | { |
| 2678 | struct task_group *tg; |
| 2679 | struct sched_entity *se; |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 2680 | long shares; |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 2681 | |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 2682 | tg = cfs_rq->tg; |
| 2683 | se = tg->se[cpu_of(rq_of(cfs_rq))]; |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 2684 | if (!se || throttled_hierarchy(cfs_rq)) |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 2685 | return; |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 2686 | #ifndef CONFIG_SMP |
| 2687 | if (likely(se->load.weight == tg->shares)) |
| 2688 | return; |
| 2689 | #endif |
Paul Turner | 6d5ab29 | 2011-01-21 20:45:01 -0800 | [diff] [blame] | 2690 | shares = calc_cfs_shares(cfs_rq, tg); |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 2691 | |
| 2692 | reweight_entity(cfs_rq_of(se), se, shares); |
| 2693 | } |
| 2694 | #else /* CONFIG_FAIR_GROUP_SCHED */ |
Paul Turner | 6d5ab29 | 2011-01-21 20:45:01 -0800 | [diff] [blame] | 2695 | static inline void update_cfs_shares(struct cfs_rq *cfs_rq) |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 2696 | { |
| 2697 | } |
| 2698 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
| 2699 | |
Alex Shi | 141965c | 2013-06-26 13:05:39 +0800 | [diff] [blame] | 2700 | #ifdef CONFIG_SMP |
Paul Turner | 5b51f2f | 2012-10-04 13:18:32 +0200 | [diff] [blame] | 2701 | /* Precomputed fixed inverse multiplies for multiplication by y^n */ |
| 2702 | static const u32 runnable_avg_yN_inv[] = { |
| 2703 | 0xffffffff, 0xfa83b2da, 0xf5257d14, 0xefe4b99a, 0xeac0c6e6, 0xe5b906e6, |
| 2704 | 0xe0ccdeeb, 0xdbfbb796, 0xd744fcc9, 0xd2a81d91, 0xce248c14, 0xc9b9bd85, |
| 2705 | 0xc5672a10, 0xc12c4cc9, 0xbd08a39e, 0xb8fbaf46, 0xb504f333, 0xb123f581, |
| 2706 | 0xad583ee9, 0xa9a15ab4, 0xa5fed6a9, 0xa2704302, 0x9ef5325f, 0x9b8d39b9, |
| 2707 | 0x9837f050, 0x94f4efa8, 0x91c3d373, 0x8ea4398a, 0x8b95c1e3, 0x88980e80, |
| 2708 | 0x85aac367, 0x82cd8698, |
| 2709 | }; |
| 2710 | |
| 2711 | /* |
| 2712 | * Precomputed \Sum y^k { 1<=k<=n }. These are floor(true_value) to prevent |
| 2713 | * over-estimates when re-combining. |
| 2714 | */ |
| 2715 | static const u32 runnable_avg_yN_sum[] = { |
| 2716 | 0, 1002, 1982, 2941, 3880, 4798, 5697, 6576, 7437, 8279, 9103, |
| 2717 | 9909,10698,11470,12226,12966,13690,14398,15091,15769,16433,17082, |
| 2718 | 17718,18340,18949,19545,20128,20698,21256,21802,22336,22859,23371, |
| 2719 | }; |
| 2720 | |
| 2721 | /* |
Yuyang Du | 7b20b91 | 2016-05-03 05:54:27 +0800 | [diff] [blame] | 2722 | * Precomputed \Sum y^k { 1<=k<=n, where n%32=0). Values are rolled down to |
| 2723 | * lower integers. See Documentation/scheduler/sched-avg.txt how these |
| 2724 | * were generated: |
| 2725 | */ |
| 2726 | static const u32 __accumulated_sum_N32[] = { |
| 2727 | 0, 23371, 35056, 40899, 43820, 45281, |
| 2728 | 46011, 46376, 46559, 46650, 46696, 46719, |
| 2729 | }; |
| 2730 | |
| 2731 | /* |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 2732 | * Approximate: |
| 2733 | * val * y^n, where y^32 ~= 0.5 (~1 scheduling period) |
| 2734 | */ |
| 2735 | static __always_inline u64 decay_load(u64 val, u64 n) |
| 2736 | { |
Paul Turner | 5b51f2f | 2012-10-04 13:18:32 +0200 | [diff] [blame] | 2737 | unsigned int local_n; |
| 2738 | |
| 2739 | if (!n) |
| 2740 | return val; |
| 2741 | else if (unlikely(n > LOAD_AVG_PERIOD * 63)) |
| 2742 | return 0; |
| 2743 | |
| 2744 | /* after bounds checking we can collapse to 32-bit */ |
| 2745 | local_n = n; |
| 2746 | |
| 2747 | /* |
| 2748 | * As y^PERIOD = 1/2, we can combine |
Zhihui Zhang | 9c58c79 | 2014-09-20 21:24:36 -0400 | [diff] [blame] | 2749 | * y^n = 1/2^(n/PERIOD) * y^(n%PERIOD) |
| 2750 | * With a look-up table which covers y^n (n<PERIOD) |
Paul Turner | 5b51f2f | 2012-10-04 13:18:32 +0200 | [diff] [blame] | 2751 | * |
| 2752 | * To achieve constant time decay_load. |
| 2753 | */ |
| 2754 | if (unlikely(local_n >= LOAD_AVG_PERIOD)) { |
| 2755 | val >>= local_n / LOAD_AVG_PERIOD; |
| 2756 | local_n %= LOAD_AVG_PERIOD; |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 2757 | } |
| 2758 | |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 2759 | val = mul_u64_u32_shr(val, runnable_avg_yN_inv[local_n], 32); |
| 2760 | return val; |
Paul Turner | 5b51f2f | 2012-10-04 13:18:32 +0200 | [diff] [blame] | 2761 | } |
| 2762 | |
| 2763 | /* |
| 2764 | * For updates fully spanning n periods, the contribution to runnable |
| 2765 | * average will be: \Sum 1024*y^n |
| 2766 | * |
| 2767 | * We can compute this reasonably efficiently by combining: |
| 2768 | * y^PERIOD = 1/2 with precomputed \Sum 1024*y^n {for n <PERIOD} |
| 2769 | */ |
| 2770 | static u32 __compute_runnable_contrib(u64 n) |
| 2771 | { |
| 2772 | u32 contrib = 0; |
| 2773 | |
| 2774 | if (likely(n <= LOAD_AVG_PERIOD)) |
| 2775 | return runnable_avg_yN_sum[n]; |
| 2776 | else if (unlikely(n >= LOAD_AVG_MAX_N)) |
| 2777 | return LOAD_AVG_MAX; |
| 2778 | |
Yuyang Du | 7b20b91 | 2016-05-03 05:54:27 +0800 | [diff] [blame] | 2779 | /* Since n < LOAD_AVG_MAX_N, n/LOAD_AVG_PERIOD < 11 */ |
| 2780 | contrib = __accumulated_sum_N32[n/LOAD_AVG_PERIOD]; |
| 2781 | n %= LOAD_AVG_PERIOD; |
Paul Turner | 5b51f2f | 2012-10-04 13:18:32 +0200 | [diff] [blame] | 2782 | contrib = decay_load(contrib, n); |
| 2783 | return contrib + runnable_avg_yN_sum[n]; |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 2784 | } |
| 2785 | |
Peter Zijlstra | 54a2138 | 2015-09-07 15:05:42 +0200 | [diff] [blame] | 2786 | #define cap_scale(v, s) ((v)*(s) >> SCHED_CAPACITY_SHIFT) |
Dietmar Eggemann | e0f5f3a | 2015-08-14 17:23:09 +0100 | [diff] [blame] | 2787 | |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 2788 | /* |
| 2789 | * We can represent the historical contribution to runnable average as the |
| 2790 | * coefficients of a geometric series. To do this we sub-divide our runnable |
| 2791 | * history into segments of approximately 1ms (1024us); label the segment that |
| 2792 | * occurred N-ms ago p_N, with p_0 corresponding to the current period, e.g. |
| 2793 | * |
| 2794 | * [<- 1024us ->|<- 1024us ->|<- 1024us ->| ... |
| 2795 | * p0 p1 p2 |
| 2796 | * (now) (~1ms ago) (~2ms ago) |
| 2797 | * |
| 2798 | * Let u_i denote the fraction of p_i that the entity was runnable. |
| 2799 | * |
| 2800 | * We then designate the fractions u_i as our co-efficients, yielding the |
| 2801 | * following representation of historical load: |
| 2802 | * u_0 + u_1*y + u_2*y^2 + u_3*y^3 + ... |
| 2803 | * |
| 2804 | * We choose y based on the with of a reasonably scheduling period, fixing: |
| 2805 | * y^32 = 0.5 |
| 2806 | * |
| 2807 | * This means that the contribution to load ~32ms ago (u_32) will be weighted |
| 2808 | * approximately half as much as the contribution to load within the last ms |
| 2809 | * (u_0). |
| 2810 | * |
| 2811 | * When a period "rolls over" and we have new u_0`, multiplying the previous |
| 2812 | * sum again by y is sufficient to update: |
| 2813 | * load_avg = u_0` + y*(u_0 + u_1*y + u_2*y^2 + ... ) |
| 2814 | * = u_0 + u_1*y + u_2*y^2 + ... [re-labeling u_i --> u_{i+1}] |
| 2815 | */ |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 2816 | static __always_inline int |
| 2817 | __update_load_avg(u64 now, int cpu, struct sched_avg *sa, |
Yuyang Du | 1396223 | 2015-07-15 08:04:41 +0800 | [diff] [blame] | 2818 | unsigned long weight, int running, struct cfs_rq *cfs_rq) |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 2819 | { |
Dietmar Eggemann | e0f5f3a | 2015-08-14 17:23:09 +0100 | [diff] [blame] | 2820 | u64 delta, scaled_delta, periods; |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 2821 | u32 contrib; |
Peter Zijlstra | 6115c79 | 2015-09-07 15:09:15 +0200 | [diff] [blame] | 2822 | unsigned int delta_w, scaled_delta_w, decayed = 0; |
Dietmar Eggemann | 6f2b045 | 2015-09-07 14:57:22 +0100 | [diff] [blame] | 2823 | unsigned long scale_freq, scale_cpu; |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 2824 | |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 2825 | delta = now - sa->last_update_time; |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 2826 | /* |
| 2827 | * This should only happen when time goes backwards, which it |
| 2828 | * unfortunately does during sched clock init when we swap over to TSC. |
| 2829 | */ |
| 2830 | if ((s64)delta < 0) { |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 2831 | sa->last_update_time = now; |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 2832 | return 0; |
| 2833 | } |
| 2834 | |
| 2835 | /* |
| 2836 | * Use 1024ns as the unit of measurement since it's a reasonable |
| 2837 | * approximation of 1us and fast to compute. |
| 2838 | */ |
| 2839 | delta >>= 10; |
| 2840 | if (!delta) |
| 2841 | return 0; |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 2842 | sa->last_update_time = now; |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 2843 | |
Dietmar Eggemann | 6f2b045 | 2015-09-07 14:57:22 +0100 | [diff] [blame] | 2844 | scale_freq = arch_scale_freq_capacity(NULL, cpu); |
| 2845 | scale_cpu = arch_scale_cpu_capacity(NULL, cpu); |
| 2846 | |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 2847 | /* delta_w is the amount already accumulated against our next period */ |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 2848 | delta_w = sa->period_contrib; |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 2849 | if (delta + delta_w >= 1024) { |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 2850 | decayed = 1; |
| 2851 | |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 2852 | /* how much left for next period will start over, we don't know yet */ |
| 2853 | sa->period_contrib = 0; |
| 2854 | |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 2855 | /* |
| 2856 | * Now that we know we're crossing a period boundary, figure |
| 2857 | * out how much from delta we need to complete the current |
| 2858 | * period and accrue it. |
| 2859 | */ |
| 2860 | delta_w = 1024 - delta_w; |
Peter Zijlstra | 54a2138 | 2015-09-07 15:05:42 +0200 | [diff] [blame] | 2861 | scaled_delta_w = cap_scale(delta_w, scale_freq); |
Yuyang Du | 1396223 | 2015-07-15 08:04:41 +0800 | [diff] [blame] | 2862 | if (weight) { |
Dietmar Eggemann | e0f5f3a | 2015-08-14 17:23:09 +0100 | [diff] [blame] | 2863 | sa->load_sum += weight * scaled_delta_w; |
| 2864 | if (cfs_rq) { |
| 2865 | cfs_rq->runnable_load_sum += |
| 2866 | weight * scaled_delta_w; |
| 2867 | } |
Yuyang Du | 1396223 | 2015-07-15 08:04:41 +0800 | [diff] [blame] | 2868 | } |
Vincent Guittot | 36ee28e | 2015-02-27 16:54:04 +0100 | [diff] [blame] | 2869 | if (running) |
Peter Zijlstra | 006cdf0 | 2015-09-09 09:06:17 +0200 | [diff] [blame] | 2870 | sa->util_sum += scaled_delta_w * scale_cpu; |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 2871 | |
Paul Turner | 5b51f2f | 2012-10-04 13:18:32 +0200 | [diff] [blame] | 2872 | delta -= delta_w; |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 2873 | |
Paul Turner | 5b51f2f | 2012-10-04 13:18:32 +0200 | [diff] [blame] | 2874 | /* Figure out how many additional periods this update spans */ |
| 2875 | periods = delta / 1024; |
| 2876 | delta %= 1024; |
| 2877 | |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 2878 | sa->load_sum = decay_load(sa->load_sum, periods + 1); |
Yuyang Du | 1396223 | 2015-07-15 08:04:41 +0800 | [diff] [blame] | 2879 | if (cfs_rq) { |
| 2880 | cfs_rq->runnable_load_sum = |
| 2881 | decay_load(cfs_rq->runnable_load_sum, periods + 1); |
| 2882 | } |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 2883 | sa->util_sum = decay_load((u64)(sa->util_sum), periods + 1); |
Paul Turner | 5b51f2f | 2012-10-04 13:18:32 +0200 | [diff] [blame] | 2884 | |
| 2885 | /* Efficiently calculate \sum (1..n_period) 1024*y^i */ |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 2886 | contrib = __compute_runnable_contrib(periods); |
Peter Zijlstra | 54a2138 | 2015-09-07 15:05:42 +0200 | [diff] [blame] | 2887 | contrib = cap_scale(contrib, scale_freq); |
Yuyang Du | 1396223 | 2015-07-15 08:04:41 +0800 | [diff] [blame] | 2888 | if (weight) { |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 2889 | sa->load_sum += weight * contrib; |
Yuyang Du | 1396223 | 2015-07-15 08:04:41 +0800 | [diff] [blame] | 2890 | if (cfs_rq) |
| 2891 | cfs_rq->runnable_load_sum += weight * contrib; |
| 2892 | } |
Vincent Guittot | 36ee28e | 2015-02-27 16:54:04 +0100 | [diff] [blame] | 2893 | if (running) |
Peter Zijlstra | 006cdf0 | 2015-09-09 09:06:17 +0200 | [diff] [blame] | 2894 | sa->util_sum += contrib * scale_cpu; |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 2895 | } |
| 2896 | |
| 2897 | /* Remainder of delta accrued against u_0` */ |
Peter Zijlstra | 54a2138 | 2015-09-07 15:05:42 +0200 | [diff] [blame] | 2898 | scaled_delta = cap_scale(delta, scale_freq); |
Yuyang Du | 1396223 | 2015-07-15 08:04:41 +0800 | [diff] [blame] | 2899 | if (weight) { |
Dietmar Eggemann | e0f5f3a | 2015-08-14 17:23:09 +0100 | [diff] [blame] | 2900 | sa->load_sum += weight * scaled_delta; |
Yuyang Du | 1396223 | 2015-07-15 08:04:41 +0800 | [diff] [blame] | 2901 | if (cfs_rq) |
Dietmar Eggemann | e0f5f3a | 2015-08-14 17:23:09 +0100 | [diff] [blame] | 2902 | cfs_rq->runnable_load_sum += weight * scaled_delta; |
Yuyang Du | 1396223 | 2015-07-15 08:04:41 +0800 | [diff] [blame] | 2903 | } |
Vincent Guittot | 36ee28e | 2015-02-27 16:54:04 +0100 | [diff] [blame] | 2904 | if (running) |
Peter Zijlstra | 006cdf0 | 2015-09-09 09:06:17 +0200 | [diff] [blame] | 2905 | sa->util_sum += scaled_delta * scale_cpu; |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 2906 | |
| 2907 | sa->period_contrib += delta; |
| 2908 | |
| 2909 | if (decayed) { |
| 2910 | sa->load_avg = div_u64(sa->load_sum, LOAD_AVG_MAX); |
Yuyang Du | 1396223 | 2015-07-15 08:04:41 +0800 | [diff] [blame] | 2911 | if (cfs_rq) { |
| 2912 | cfs_rq->runnable_load_avg = |
| 2913 | div_u64(cfs_rq->runnable_load_sum, LOAD_AVG_MAX); |
| 2914 | } |
Peter Zijlstra | 006cdf0 | 2015-09-09 09:06:17 +0200 | [diff] [blame] | 2915 | sa->util_avg = sa->util_sum / LOAD_AVG_MAX; |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 2916 | } |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 2917 | |
| 2918 | return decayed; |
| 2919 | } |
| 2920 | |
Vincent Guittot | 09a43ac | 2016-11-08 10:53:45 +0100 | [diff] [blame^] | 2921 | /* |
| 2922 | * Signed add and clamp on underflow. |
| 2923 | * |
| 2924 | * Explicitly do a load-store to ensure the intermediate value never hits |
| 2925 | * memory. This allows lockless observations without ever seeing the negative |
| 2926 | * values. |
| 2927 | */ |
| 2928 | #define add_positive(_ptr, _val) do { \ |
| 2929 | typeof(_ptr) ptr = (_ptr); \ |
| 2930 | typeof(_val) val = (_val); \ |
| 2931 | typeof(*ptr) res, var = READ_ONCE(*ptr); \ |
| 2932 | \ |
| 2933 | res = var + val; \ |
| 2934 | \ |
| 2935 | if (val < 0 && res > var) \ |
| 2936 | res = 0; \ |
| 2937 | \ |
| 2938 | WRITE_ONCE(*ptr, res); \ |
| 2939 | } while (0) |
| 2940 | |
Paul Turner | c566e8e | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2941 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Peter Zijlstra | 7c3edd2 | 2016-07-13 10:56:25 +0200 | [diff] [blame] | 2942 | /** |
| 2943 | * update_tg_load_avg - update the tg's load avg |
| 2944 | * @cfs_rq: the cfs_rq whose avg changed |
| 2945 | * @force: update regardless of how small the difference |
| 2946 | * |
| 2947 | * This function 'ensures': tg->load_avg := \Sum tg->cfs_rq[]->avg.load. |
| 2948 | * However, because tg->load_avg is a global value there are performance |
| 2949 | * considerations. |
| 2950 | * |
| 2951 | * In order to avoid having to look at the other cfs_rq's, we use a |
| 2952 | * differential update where we store the last value we propagated. This in |
| 2953 | * turn allows skipping updates if the differential is 'small'. |
| 2954 | * |
| 2955 | * Updating tg's load_avg is necessary before update_cfs_share() (which is |
| 2956 | * done) and effective_load() (which is not done because it is too costly). |
Paul Turner | bb17f65 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 2957 | */ |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 2958 | static inline void update_tg_load_avg(struct cfs_rq *cfs_rq, int force) |
Paul Turner | bb17f65 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 2959 | { |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 2960 | long delta = cfs_rq->avg.load_avg - cfs_rq->tg_load_avg_contrib; |
Paul Turner | bb17f65 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 2961 | |
Waiman Long | aa0b7ae | 2015-12-02 13:41:50 -0500 | [diff] [blame] | 2962 | /* |
| 2963 | * No need to update load_avg for root_task_group as it is not used. |
| 2964 | */ |
| 2965 | if (cfs_rq->tg == &root_task_group) |
| 2966 | return; |
| 2967 | |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 2968 | if (force || abs(delta) > cfs_rq->tg_load_avg_contrib / 64) { |
| 2969 | atomic_long_add(delta, &cfs_rq->tg->load_avg); |
| 2970 | cfs_rq->tg_load_avg_contrib = cfs_rq->avg.load_avg; |
Paul Turner | bb17f65 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 2971 | } |
Paul Turner | 8165e14 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 2972 | } |
Dietmar Eggemann | f5f9739 | 2014-02-26 11:19:33 +0000 | [diff] [blame] | 2973 | |
Byungchul Park | ad936d8 | 2015-10-24 01:16:19 +0900 | [diff] [blame] | 2974 | /* |
| 2975 | * Called within set_task_rq() right before setting a task's cpu. The |
| 2976 | * caller only guarantees p->pi_lock is held; no other assumptions, |
| 2977 | * including the state of rq->lock, should be made. |
| 2978 | */ |
| 2979 | void set_task_rq_fair(struct sched_entity *se, |
| 2980 | struct cfs_rq *prev, struct cfs_rq *next) |
| 2981 | { |
| 2982 | if (!sched_feat(ATTACH_AGE_LOAD)) |
| 2983 | return; |
| 2984 | |
| 2985 | /* |
| 2986 | * We are supposed to update the task to "current" time, then its up to |
| 2987 | * date and ready to go to new CPU/cfs_rq. But we have difficulty in |
| 2988 | * getting what current time is, so simply throw away the out-of-date |
| 2989 | * time. This will result in the wakee task is less decayed, but giving |
| 2990 | * the wakee more load sounds not bad. |
| 2991 | */ |
| 2992 | if (se->avg.last_update_time && prev) { |
| 2993 | u64 p_last_update_time; |
| 2994 | u64 n_last_update_time; |
| 2995 | |
| 2996 | #ifndef CONFIG_64BIT |
| 2997 | u64 p_last_update_time_copy; |
| 2998 | u64 n_last_update_time_copy; |
| 2999 | |
| 3000 | do { |
| 3001 | p_last_update_time_copy = prev->load_last_update_time_copy; |
| 3002 | n_last_update_time_copy = next->load_last_update_time_copy; |
| 3003 | |
| 3004 | smp_rmb(); |
| 3005 | |
| 3006 | p_last_update_time = prev->avg.last_update_time; |
| 3007 | n_last_update_time = next->avg.last_update_time; |
| 3008 | |
| 3009 | } while (p_last_update_time != p_last_update_time_copy || |
| 3010 | n_last_update_time != n_last_update_time_copy); |
| 3011 | #else |
| 3012 | p_last_update_time = prev->avg.last_update_time; |
| 3013 | n_last_update_time = next->avg.last_update_time; |
| 3014 | #endif |
| 3015 | __update_load_avg(p_last_update_time, cpu_of(rq_of(prev)), |
| 3016 | &se->avg, 0, 0, NULL); |
| 3017 | se->avg.last_update_time = n_last_update_time; |
| 3018 | } |
| 3019 | } |
Vincent Guittot | 09a43ac | 2016-11-08 10:53:45 +0100 | [diff] [blame^] | 3020 | |
| 3021 | /* Take into account change of utilization of a child task group */ |
| 3022 | static inline void |
| 3023 | update_tg_cfs_util(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| 3024 | { |
| 3025 | struct cfs_rq *gcfs_rq = group_cfs_rq(se); |
| 3026 | long delta = gcfs_rq->avg.util_avg - se->avg.util_avg; |
| 3027 | |
| 3028 | /* Nothing to update */ |
| 3029 | if (!delta) |
| 3030 | return; |
| 3031 | |
| 3032 | /* Set new sched_entity's utilization */ |
| 3033 | se->avg.util_avg = gcfs_rq->avg.util_avg; |
| 3034 | se->avg.util_sum = se->avg.util_avg * LOAD_AVG_MAX; |
| 3035 | |
| 3036 | /* Update parent cfs_rq utilization */ |
| 3037 | add_positive(&cfs_rq->avg.util_avg, delta); |
| 3038 | cfs_rq->avg.util_sum = cfs_rq->avg.util_avg * LOAD_AVG_MAX; |
| 3039 | } |
| 3040 | |
| 3041 | /* Take into account change of load of a child task group */ |
| 3042 | static inline void |
| 3043 | update_tg_cfs_load(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| 3044 | { |
| 3045 | struct cfs_rq *gcfs_rq = group_cfs_rq(se); |
| 3046 | long delta, load = gcfs_rq->avg.load_avg; |
| 3047 | |
| 3048 | /* |
| 3049 | * If the load of group cfs_rq is null, the load of the |
| 3050 | * sched_entity will also be null so we can skip the formula |
| 3051 | */ |
| 3052 | if (load) { |
| 3053 | long tg_load; |
| 3054 | |
| 3055 | /* Get tg's load and ensure tg_load > 0 */ |
| 3056 | tg_load = atomic_long_read(&gcfs_rq->tg->load_avg) + 1; |
| 3057 | |
| 3058 | /* Ensure tg_load >= load and updated with current load*/ |
| 3059 | tg_load -= gcfs_rq->tg_load_avg_contrib; |
| 3060 | tg_load += load; |
| 3061 | |
| 3062 | /* |
| 3063 | * We need to compute a correction term in the case that the |
| 3064 | * task group is consuming more CPU than a task of equal |
| 3065 | * weight. A task with a weight equals to tg->shares will have |
| 3066 | * a load less or equal to scale_load_down(tg->shares). |
| 3067 | * Similarly, the sched_entities that represent the task group |
| 3068 | * at parent level, can't have a load higher than |
| 3069 | * scale_load_down(tg->shares). And the Sum of sched_entities' |
| 3070 | * load must be <= scale_load_down(tg->shares). |
| 3071 | */ |
| 3072 | if (tg_load > scale_load_down(gcfs_rq->tg->shares)) { |
| 3073 | /* scale gcfs_rq's load into tg's shares*/ |
| 3074 | load *= scale_load_down(gcfs_rq->tg->shares); |
| 3075 | load /= tg_load; |
| 3076 | } |
| 3077 | } |
| 3078 | |
| 3079 | delta = load - se->avg.load_avg; |
| 3080 | |
| 3081 | /* Nothing to update */ |
| 3082 | if (!delta) |
| 3083 | return; |
| 3084 | |
| 3085 | /* Set new sched_entity's load */ |
| 3086 | se->avg.load_avg = load; |
| 3087 | se->avg.load_sum = se->avg.load_avg * LOAD_AVG_MAX; |
| 3088 | |
| 3089 | /* Update parent cfs_rq load */ |
| 3090 | add_positive(&cfs_rq->avg.load_avg, delta); |
| 3091 | cfs_rq->avg.load_sum = cfs_rq->avg.load_avg * LOAD_AVG_MAX; |
| 3092 | |
| 3093 | /* |
| 3094 | * If the sched_entity is already enqueued, we also have to update the |
| 3095 | * runnable load avg. |
| 3096 | */ |
| 3097 | if (se->on_rq) { |
| 3098 | /* Update parent cfs_rq runnable_load_avg */ |
| 3099 | add_positive(&cfs_rq->runnable_load_avg, delta); |
| 3100 | cfs_rq->runnable_load_sum = cfs_rq->runnable_load_avg * LOAD_AVG_MAX; |
| 3101 | } |
| 3102 | } |
| 3103 | |
| 3104 | static inline void set_tg_cfs_propagate(struct cfs_rq *cfs_rq) |
| 3105 | { |
| 3106 | cfs_rq->propagate_avg = 1; |
| 3107 | } |
| 3108 | |
| 3109 | static inline int test_and_clear_tg_cfs_propagate(struct sched_entity *se) |
| 3110 | { |
| 3111 | struct cfs_rq *cfs_rq = group_cfs_rq(se); |
| 3112 | |
| 3113 | if (!cfs_rq->propagate_avg) |
| 3114 | return 0; |
| 3115 | |
| 3116 | cfs_rq->propagate_avg = 0; |
| 3117 | return 1; |
| 3118 | } |
| 3119 | |
| 3120 | /* Update task and its cfs_rq load average */ |
| 3121 | static inline int propagate_entity_load_avg(struct sched_entity *se) |
| 3122 | { |
| 3123 | struct cfs_rq *cfs_rq; |
| 3124 | |
| 3125 | if (entity_is_task(se)) |
| 3126 | return 0; |
| 3127 | |
| 3128 | if (!test_and_clear_tg_cfs_propagate(se)) |
| 3129 | return 0; |
| 3130 | |
| 3131 | cfs_rq = cfs_rq_of(se); |
| 3132 | |
| 3133 | set_tg_cfs_propagate(cfs_rq); |
| 3134 | |
| 3135 | update_tg_cfs_util(cfs_rq, se); |
| 3136 | update_tg_cfs_load(cfs_rq, se); |
| 3137 | |
| 3138 | return 1; |
| 3139 | } |
| 3140 | |
Peter Zijlstra | 6e83125 | 2014-02-11 16:11:48 +0100 | [diff] [blame] | 3141 | #else /* CONFIG_FAIR_GROUP_SCHED */ |
Vincent Guittot | 09a43ac | 2016-11-08 10:53:45 +0100 | [diff] [blame^] | 3142 | |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 3143 | static inline void update_tg_load_avg(struct cfs_rq *cfs_rq, int force) {} |
Vincent Guittot | 09a43ac | 2016-11-08 10:53:45 +0100 | [diff] [blame^] | 3144 | |
| 3145 | static inline int propagate_entity_load_avg(struct sched_entity *se) |
| 3146 | { |
| 3147 | return 0; |
| 3148 | } |
| 3149 | |
| 3150 | static inline void set_tg_cfs_propagate(struct cfs_rq *cfs_rq) {} |
| 3151 | |
Peter Zijlstra | 6e83125 | 2014-02-11 16:11:48 +0100 | [diff] [blame] | 3152 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
Paul Turner | c566e8e | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 3153 | |
Steve Muckle | a2c6c91 | 2016-03-24 15:26:07 -0700 | [diff] [blame] | 3154 | static inline void cfs_rq_util_change(struct cfs_rq *cfs_rq) |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 3155 | { |
Rafael J. Wysocki | 58919e8 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 3156 | if (&this_rq()->cfs == cfs_rq) { |
Steve Muckle | 21e96f8 | 2016-03-21 17:21:07 -0700 | [diff] [blame] | 3157 | /* |
| 3158 | * There are a few boundary cases this might miss but it should |
| 3159 | * get called often enough that that should (hopefully) not be |
| 3160 | * a real problem -- added to that it only calls on the local |
| 3161 | * CPU, so if we enqueue remotely we'll miss an update, but |
| 3162 | * the next tick/schedule should update. |
| 3163 | * |
| 3164 | * It will not get called when we go idle, because the idle |
| 3165 | * thread is a different class (!fair), nor will the utilization |
| 3166 | * number include things like RT tasks. |
| 3167 | * |
| 3168 | * As is, the util number is not freq-invariant (we'd have to |
| 3169 | * implement arch_scale_freq_capacity() for that). |
| 3170 | * |
| 3171 | * See cpu_util(). |
| 3172 | */ |
Rafael J. Wysocki | 12bde33 | 2016-08-10 03:11:17 +0200 | [diff] [blame] | 3173 | cpufreq_update_util(rq_of(cfs_rq), 0); |
Steve Muckle | 21e96f8 | 2016-03-21 17:21:07 -0700 | [diff] [blame] | 3174 | } |
Steve Muckle | a2c6c91 | 2016-03-24 15:26:07 -0700 | [diff] [blame] | 3175 | } |
| 3176 | |
Peter Zijlstra | 8974189 | 2016-06-16 10:50:40 +0200 | [diff] [blame] | 3177 | /* |
| 3178 | * Unsigned subtract and clamp on underflow. |
| 3179 | * |
| 3180 | * Explicitly do a load-store to ensure the intermediate value never hits |
| 3181 | * memory. This allows lockless observations without ever seeing the negative |
| 3182 | * values. |
| 3183 | */ |
| 3184 | #define sub_positive(_ptr, _val) do { \ |
| 3185 | typeof(_ptr) ptr = (_ptr); \ |
| 3186 | typeof(*ptr) val = (_val); \ |
| 3187 | typeof(*ptr) res, var = READ_ONCE(*ptr); \ |
| 3188 | res = var - val; \ |
| 3189 | if (res > var) \ |
| 3190 | res = 0; \ |
| 3191 | WRITE_ONCE(*ptr, res); \ |
| 3192 | } while (0) |
| 3193 | |
Peter Zijlstra | 3d30544f | 2016-06-21 14:27:50 +0200 | [diff] [blame] | 3194 | /** |
| 3195 | * update_cfs_rq_load_avg - update the cfs_rq's load/util averages |
| 3196 | * @now: current time, as per cfs_rq_clock_task() |
| 3197 | * @cfs_rq: cfs_rq to update |
| 3198 | * @update_freq: should we call cfs_rq_util_change() or will the call do so |
| 3199 | * |
| 3200 | * The cfs_rq avg is the direct sum of all its entities (blocked and runnable) |
| 3201 | * avg. The immediate corollary is that all (fair) tasks must be attached, see |
| 3202 | * post_init_entity_util_avg(). |
| 3203 | * |
| 3204 | * cfs_rq->avg is used for task_h_load() and update_cfs_share() for example. |
| 3205 | * |
Peter Zijlstra | 7c3edd2 | 2016-07-13 10:56:25 +0200 | [diff] [blame] | 3206 | * Returns true if the load decayed or we removed load. |
| 3207 | * |
| 3208 | * Since both these conditions indicate a changed cfs_rq->avg.load we should |
| 3209 | * call update_tg_load_avg() when this function returns true. |
Peter Zijlstra | 3d30544f | 2016-06-21 14:27:50 +0200 | [diff] [blame] | 3210 | */ |
Steve Muckle | a2c6c91 | 2016-03-24 15:26:07 -0700 | [diff] [blame] | 3211 | static inline int |
| 3212 | update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq, bool update_freq) |
| 3213 | { |
| 3214 | struct sched_avg *sa = &cfs_rq->avg; |
| 3215 | int decayed, removed_load = 0, removed_util = 0; |
| 3216 | |
| 3217 | if (atomic_long_read(&cfs_rq->removed_load_avg)) { |
| 3218 | s64 r = atomic_long_xchg(&cfs_rq->removed_load_avg, 0); |
Peter Zijlstra | 8974189 | 2016-06-16 10:50:40 +0200 | [diff] [blame] | 3219 | sub_positive(&sa->load_avg, r); |
| 3220 | sub_positive(&sa->load_sum, r * LOAD_AVG_MAX); |
Steve Muckle | a2c6c91 | 2016-03-24 15:26:07 -0700 | [diff] [blame] | 3221 | removed_load = 1; |
| 3222 | } |
| 3223 | |
| 3224 | if (atomic_long_read(&cfs_rq->removed_util_avg)) { |
| 3225 | long r = atomic_long_xchg(&cfs_rq->removed_util_avg, 0); |
Peter Zijlstra | 8974189 | 2016-06-16 10:50:40 +0200 | [diff] [blame] | 3226 | sub_positive(&sa->util_avg, r); |
| 3227 | sub_positive(&sa->util_sum, r * LOAD_AVG_MAX); |
Steve Muckle | a2c6c91 | 2016-03-24 15:26:07 -0700 | [diff] [blame] | 3228 | removed_util = 1; |
| 3229 | } |
| 3230 | |
| 3231 | decayed = __update_load_avg(now, cpu_of(rq_of(cfs_rq)), sa, |
| 3232 | scale_load_down(cfs_rq->load.weight), cfs_rq->curr != NULL, cfs_rq); |
| 3233 | |
| 3234 | #ifndef CONFIG_64BIT |
| 3235 | smp_wmb(); |
| 3236 | cfs_rq->load_last_update_time_copy = sa->last_update_time; |
| 3237 | #endif |
| 3238 | |
| 3239 | if (update_freq && (decayed || removed_util)) |
| 3240 | cfs_rq_util_change(cfs_rq); |
Steve Muckle | 21e96f8 | 2016-03-21 17:21:07 -0700 | [diff] [blame] | 3241 | |
Steve Muckle | 41e0d37 | 2016-03-21 17:21:08 -0700 | [diff] [blame] | 3242 | return decayed || removed_load; |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 3243 | } |
| 3244 | |
Vincent Guittot | d31b1a6 | 2016-11-08 10:53:44 +0100 | [diff] [blame] | 3245 | /* |
| 3246 | * Optional action to be done while updating the load average |
| 3247 | */ |
| 3248 | #define UPDATE_TG 0x1 |
| 3249 | #define SKIP_AGE_LOAD 0x2 |
| 3250 | |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 3251 | /* Update task and its cfs_rq load average */ |
Vincent Guittot | d31b1a6 | 2016-11-08 10:53:44 +0100 | [diff] [blame] | 3252 | static inline void update_load_avg(struct sched_entity *se, int flags) |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 3253 | { |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 3254 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 3255 | u64 now = cfs_rq_clock_task(cfs_rq); |
Rafael J. Wysocki | 34e2c55 | 2016-02-15 20:20:42 +0100 | [diff] [blame] | 3256 | struct rq *rq = rq_of(cfs_rq); |
| 3257 | int cpu = cpu_of(rq); |
Vincent Guittot | 09a43ac | 2016-11-08 10:53:45 +0100 | [diff] [blame^] | 3258 | int decayed; |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 3259 | |
Paul Turner | f1b1728 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 3260 | /* |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 3261 | * Track task load average for carrying it to new CPU after migrated, and |
| 3262 | * track group sched_entity load average for task_h_load calc in migration |
Paul Turner | f1b1728 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 3263 | */ |
Vincent Guittot | d31b1a6 | 2016-11-08 10:53:44 +0100 | [diff] [blame] | 3264 | if (se->avg.last_update_time && !(flags & SKIP_AGE_LOAD)) { |
| 3265 | __update_load_avg(now, cpu, &se->avg, |
Byungchul Park | a05e8c5 | 2015-08-20 20:21:56 +0900 | [diff] [blame] | 3266 | se->on_rq * scale_load_down(se->load.weight), |
| 3267 | cfs_rq->curr == se, NULL); |
Vincent Guittot | d31b1a6 | 2016-11-08 10:53:44 +0100 | [diff] [blame] | 3268 | } |
Paul Turner | f1b1728 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 3269 | |
Vincent Guittot | 09a43ac | 2016-11-08 10:53:45 +0100 | [diff] [blame^] | 3270 | decayed = update_cfs_rq_load_avg(now, cfs_rq, true); |
| 3271 | decayed |= propagate_entity_load_avg(se); |
| 3272 | |
| 3273 | if (decayed && (flags & UPDATE_TG)) |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 3274 | update_tg_load_avg(cfs_rq, 0); |
| 3275 | } |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 3276 | |
Peter Zijlstra | 3d30544f | 2016-06-21 14:27:50 +0200 | [diff] [blame] | 3277 | /** |
| 3278 | * attach_entity_load_avg - attach this entity to its cfs_rq load avg |
| 3279 | * @cfs_rq: cfs_rq to attach to |
| 3280 | * @se: sched_entity to attach |
| 3281 | * |
| 3282 | * Must call update_cfs_rq_load_avg() before this, since we rely on |
| 3283 | * cfs_rq->avg.last_update_time being current. |
| 3284 | */ |
Byungchul Park | a05e8c5 | 2015-08-20 20:21:56 +0900 | [diff] [blame] | 3285 | static void attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| 3286 | { |
| 3287 | se->avg.last_update_time = cfs_rq->avg.last_update_time; |
| 3288 | cfs_rq->avg.load_avg += se->avg.load_avg; |
| 3289 | cfs_rq->avg.load_sum += se->avg.load_sum; |
| 3290 | cfs_rq->avg.util_avg += se->avg.util_avg; |
| 3291 | cfs_rq->avg.util_sum += se->avg.util_sum; |
Vincent Guittot | 09a43ac | 2016-11-08 10:53:45 +0100 | [diff] [blame^] | 3292 | set_tg_cfs_propagate(cfs_rq); |
Steve Muckle | a2c6c91 | 2016-03-24 15:26:07 -0700 | [diff] [blame] | 3293 | |
| 3294 | cfs_rq_util_change(cfs_rq); |
Byungchul Park | a05e8c5 | 2015-08-20 20:21:56 +0900 | [diff] [blame] | 3295 | } |
| 3296 | |
Peter Zijlstra | 3d30544f | 2016-06-21 14:27:50 +0200 | [diff] [blame] | 3297 | /** |
| 3298 | * detach_entity_load_avg - detach this entity from its cfs_rq load avg |
| 3299 | * @cfs_rq: cfs_rq to detach from |
| 3300 | * @se: sched_entity to detach |
| 3301 | * |
| 3302 | * Must call update_cfs_rq_load_avg() before this, since we rely on |
| 3303 | * cfs_rq->avg.last_update_time being current. |
| 3304 | */ |
Byungchul Park | a05e8c5 | 2015-08-20 20:21:56 +0900 | [diff] [blame] | 3305 | static void detach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| 3306 | { |
Byungchul Park | a05e8c5 | 2015-08-20 20:21:56 +0900 | [diff] [blame] | 3307 | |
Peter Zijlstra | 8974189 | 2016-06-16 10:50:40 +0200 | [diff] [blame] | 3308 | sub_positive(&cfs_rq->avg.load_avg, se->avg.load_avg); |
| 3309 | sub_positive(&cfs_rq->avg.load_sum, se->avg.load_sum); |
| 3310 | sub_positive(&cfs_rq->avg.util_avg, se->avg.util_avg); |
| 3311 | sub_positive(&cfs_rq->avg.util_sum, se->avg.util_sum); |
Vincent Guittot | 09a43ac | 2016-11-08 10:53:45 +0100 | [diff] [blame^] | 3312 | set_tg_cfs_propagate(cfs_rq); |
Steve Muckle | a2c6c91 | 2016-03-24 15:26:07 -0700 | [diff] [blame] | 3313 | |
| 3314 | cfs_rq_util_change(cfs_rq); |
Byungchul Park | a05e8c5 | 2015-08-20 20:21:56 +0900 | [diff] [blame] | 3315 | } |
| 3316 | |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 3317 | /* Add the load generated by se into cfs_rq's load average */ |
| 3318 | static inline void |
| 3319 | enqueue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| 3320 | { |
| 3321 | struct sched_avg *sa = &se->avg; |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 3322 | |
Yuyang Du | 1396223 | 2015-07-15 08:04:41 +0800 | [diff] [blame] | 3323 | cfs_rq->runnable_load_avg += sa->load_avg; |
| 3324 | cfs_rq->runnable_load_sum += sa->load_sum; |
| 3325 | |
Vincent Guittot | d31b1a6 | 2016-11-08 10:53:44 +0100 | [diff] [blame] | 3326 | if (!sa->last_update_time) { |
Byungchul Park | a05e8c5 | 2015-08-20 20:21:56 +0900 | [diff] [blame] | 3327 | attach_entity_load_avg(cfs_rq, se); |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 3328 | update_tg_load_avg(cfs_rq, 0); |
Vincent Guittot | d31b1a6 | 2016-11-08 10:53:44 +0100 | [diff] [blame] | 3329 | } |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 3330 | } |
| 3331 | |
Yuyang Du | 1396223 | 2015-07-15 08:04:41 +0800 | [diff] [blame] | 3332 | /* Remove the runnable load generated by se from cfs_rq's runnable load average */ |
| 3333 | static inline void |
| 3334 | dequeue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| 3335 | { |
Yuyang Du | 1396223 | 2015-07-15 08:04:41 +0800 | [diff] [blame] | 3336 | cfs_rq->runnable_load_avg = |
| 3337 | max_t(long, cfs_rq->runnable_load_avg - se->avg.load_avg, 0); |
| 3338 | cfs_rq->runnable_load_sum = |
Byungchul Park | a05e8c5 | 2015-08-20 20:21:56 +0900 | [diff] [blame] | 3339 | max_t(s64, cfs_rq->runnable_load_sum - se->avg.load_sum, 0); |
Yuyang Du | 1396223 | 2015-07-15 08:04:41 +0800 | [diff] [blame] | 3340 | } |
| 3341 | |
Yuyang Du | 0905f04 | 2015-12-17 07:34:27 +0800 | [diff] [blame] | 3342 | #ifndef CONFIG_64BIT |
| 3343 | static inline u64 cfs_rq_last_update_time(struct cfs_rq *cfs_rq) |
| 3344 | { |
| 3345 | u64 last_update_time_copy; |
| 3346 | u64 last_update_time; |
| 3347 | |
| 3348 | do { |
| 3349 | last_update_time_copy = cfs_rq->load_last_update_time_copy; |
| 3350 | smp_rmb(); |
| 3351 | last_update_time = cfs_rq->avg.last_update_time; |
| 3352 | } while (last_update_time != last_update_time_copy); |
| 3353 | |
| 3354 | return last_update_time; |
| 3355 | } |
| 3356 | #else |
| 3357 | static inline u64 cfs_rq_last_update_time(struct cfs_rq *cfs_rq) |
| 3358 | { |
| 3359 | return cfs_rq->avg.last_update_time; |
| 3360 | } |
| 3361 | #endif |
| 3362 | |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 3363 | /* |
Morten Rasmussen | 104cb16 | 2016-10-14 14:41:07 +0100 | [diff] [blame] | 3364 | * Synchronize entity load avg of dequeued entity without locking |
| 3365 | * the previous rq. |
| 3366 | */ |
| 3367 | void sync_entity_load_avg(struct sched_entity *se) |
| 3368 | { |
| 3369 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 3370 | u64 last_update_time; |
| 3371 | |
| 3372 | last_update_time = cfs_rq_last_update_time(cfs_rq); |
| 3373 | __update_load_avg(last_update_time, cpu_of(rq_of(cfs_rq)), &se->avg, 0, 0, NULL); |
| 3374 | } |
| 3375 | |
| 3376 | /* |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 3377 | * Task first catches up with cfs_rq, and then subtract |
| 3378 | * itself from the cfs_rq (task must be off the queue now). |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 3379 | */ |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 3380 | void remove_entity_load_avg(struct sched_entity *se) |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 3381 | { |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 3382 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 3383 | |
Yuyang Du | 0905f04 | 2015-12-17 07:34:27 +0800 | [diff] [blame] | 3384 | /* |
Peter Zijlstra | 7dc603c | 2016-06-16 13:29:28 +0200 | [diff] [blame] | 3385 | * tasks cannot exit without having gone through wake_up_new_task() -> |
| 3386 | * post_init_entity_util_avg() which will have added things to the |
| 3387 | * cfs_rq, so we can remove unconditionally. |
| 3388 | * |
| 3389 | * Similarly for groups, they will have passed through |
| 3390 | * post_init_entity_util_avg() before unregister_sched_fair_group() |
| 3391 | * calls this. |
Yuyang Du | 0905f04 | 2015-12-17 07:34:27 +0800 | [diff] [blame] | 3392 | */ |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 3393 | |
Morten Rasmussen | 104cb16 | 2016-10-14 14:41:07 +0100 | [diff] [blame] | 3394 | sync_entity_load_avg(se); |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 3395 | atomic_long_add(se->avg.load_avg, &cfs_rq->removed_load_avg); |
| 3396 | atomic_long_add(se->avg.util_avg, &cfs_rq->removed_util_avg); |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 3397 | } |
Vincent Guittot | 642dbc3 | 2013-04-18 18:34:26 +0200 | [diff] [blame] | 3398 | |
Yuyang Du | 7ea241a | 2015-07-15 08:04:42 +0800 | [diff] [blame] | 3399 | static inline unsigned long cfs_rq_runnable_load_avg(struct cfs_rq *cfs_rq) |
| 3400 | { |
| 3401 | return cfs_rq->runnable_load_avg; |
| 3402 | } |
| 3403 | |
| 3404 | static inline unsigned long cfs_rq_load_avg(struct cfs_rq *cfs_rq) |
| 3405 | { |
| 3406 | return cfs_rq->avg.load_avg; |
| 3407 | } |
| 3408 | |
Peter Zijlstra | 6e83125 | 2014-02-11 16:11:48 +0100 | [diff] [blame] | 3409 | static int idle_balance(struct rq *this_rq); |
| 3410 | |
Peter Zijlstra | 38033c3 | 2014-01-23 20:32:21 +0100 | [diff] [blame] | 3411 | #else /* CONFIG_SMP */ |
| 3412 | |
Peter Zijlstra | 0101147 | 2016-06-17 11:20:46 +0200 | [diff] [blame] | 3413 | static inline int |
| 3414 | update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq, bool update_freq) |
| 3415 | { |
| 3416 | return 0; |
| 3417 | } |
| 3418 | |
Vincent Guittot | d31b1a6 | 2016-11-08 10:53:44 +0100 | [diff] [blame] | 3419 | #define UPDATE_TG 0x0 |
| 3420 | #define SKIP_AGE_LOAD 0x0 |
| 3421 | |
| 3422 | static inline void update_load_avg(struct sched_entity *se, int not_used1) |
Rafael J. Wysocki | 536bd00 | 2016-05-06 14:58:43 +0200 | [diff] [blame] | 3423 | { |
Rafael J. Wysocki | 12bde33 | 2016-08-10 03:11:17 +0200 | [diff] [blame] | 3424 | cpufreq_update_util(rq_of(cfs_rq_of(se)), 0); |
Rafael J. Wysocki | 536bd00 | 2016-05-06 14:58:43 +0200 | [diff] [blame] | 3425 | } |
| 3426 | |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 3427 | static inline void |
| 3428 | enqueue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {} |
Yuyang Du | 1396223 | 2015-07-15 08:04:41 +0800 | [diff] [blame] | 3429 | static inline void |
| 3430 | dequeue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {} |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 3431 | static inline void remove_entity_load_avg(struct sched_entity *se) {} |
Peter Zijlstra | 6e83125 | 2014-02-11 16:11:48 +0100 | [diff] [blame] | 3432 | |
Byungchul Park | a05e8c5 | 2015-08-20 20:21:56 +0900 | [diff] [blame] | 3433 | static inline void |
| 3434 | attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {} |
| 3435 | static inline void |
| 3436 | detach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {} |
| 3437 | |
Peter Zijlstra | 6e83125 | 2014-02-11 16:11:48 +0100 | [diff] [blame] | 3438 | static inline int idle_balance(struct rq *rq) |
| 3439 | { |
| 3440 | return 0; |
| 3441 | } |
| 3442 | |
Peter Zijlstra | 38033c3 | 2014-01-23 20:32:21 +0100 | [diff] [blame] | 3443 | #endif /* CONFIG_SMP */ |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 3444 | |
Peter Zijlstra | ddc9729 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 3445 | static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| 3446 | { |
| 3447 | #ifdef CONFIG_SCHED_DEBUG |
| 3448 | s64 d = se->vruntime - cfs_rq->min_vruntime; |
| 3449 | |
| 3450 | if (d < 0) |
| 3451 | d = -d; |
| 3452 | |
| 3453 | if (d > 3*sysctl_sched_latency) |
Josh Poimboeuf | ae92882 | 2016-06-17 12:43:24 -0500 | [diff] [blame] | 3454 | schedstat_inc(cfs_rq->nr_spread_over); |
Peter Zijlstra | ddc9729 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 3455 | #endif |
| 3456 | } |
| 3457 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3458 | static void |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 3459 | place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial) |
| 3460 | { |
Peter Zijlstra | 1af5f73 | 2008-10-24 11:06:13 +0200 | [diff] [blame] | 3461 | u64 vruntime = cfs_rq->min_vruntime; |
Peter Zijlstra | 94dfb5e | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 3462 | |
Peter Zijlstra | 2cb8600 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 3463 | /* |
| 3464 | * The 'current' period is already promised to the current tasks, |
| 3465 | * however the extra weight of the new task will slow them down a |
| 3466 | * little, place the new task so that it fits in the slot that |
| 3467 | * stays open at the end. |
| 3468 | */ |
Peter Zijlstra | 94dfb5e | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 3469 | if (initial && sched_feat(START_DEBIT)) |
Peter Zijlstra | f9c0b09 | 2008-10-17 19:27:04 +0200 | [diff] [blame] | 3470 | vruntime += sched_vslice(cfs_rq, se); |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 3471 | |
Mike Galbraith | a2e7a7e | 2009-09-18 09:19:25 +0200 | [diff] [blame] | 3472 | /* sleeps up to a single latency don't count. */ |
Mike Galbraith | 5ca9880 | 2010-03-11 17:17:17 +0100 | [diff] [blame] | 3473 | if (!initial) { |
Mike Galbraith | a2e7a7e | 2009-09-18 09:19:25 +0200 | [diff] [blame] | 3474 | unsigned long thresh = sysctl_sched_latency; |
Peter Zijlstra | a7be37a | 2008-06-27 13:41:11 +0200 | [diff] [blame] | 3475 | |
Mike Galbraith | a2e7a7e | 2009-09-18 09:19:25 +0200 | [diff] [blame] | 3476 | /* |
Mike Galbraith | a2e7a7e | 2009-09-18 09:19:25 +0200 | [diff] [blame] | 3477 | * Halve their sleep time's effect, to allow |
| 3478 | * for a gentler effect of sleepers: |
| 3479 | */ |
| 3480 | if (sched_feat(GENTLE_FAIR_SLEEPERS)) |
| 3481 | thresh >>= 1; |
Ingo Molnar | 51e0304 | 2009-09-16 08:54:45 +0200 | [diff] [blame] | 3482 | |
Mike Galbraith | a2e7a7e | 2009-09-18 09:19:25 +0200 | [diff] [blame] | 3483 | vruntime -= thresh; |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 3484 | } |
| 3485 | |
Mike Galbraith | b5d9d73 | 2009-09-08 11:12:28 +0200 | [diff] [blame] | 3486 | /* ensure we never gain time by being placed backwards. */ |
Viresh Kumar | 16c8f1c | 2012-11-08 13:33:46 +0530 | [diff] [blame] | 3487 | se->vruntime = max_vruntime(se->vruntime, vruntime); |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 3488 | } |
| 3489 | |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 3490 | static void check_enqueue_throttle(struct cfs_rq *cfs_rq); |
| 3491 | |
Mel Gorman | cb25176 | 2016-02-05 09:08:36 +0000 | [diff] [blame] | 3492 | static inline void check_schedstat_required(void) |
| 3493 | { |
| 3494 | #ifdef CONFIG_SCHEDSTATS |
| 3495 | if (schedstat_enabled()) |
| 3496 | return; |
| 3497 | |
| 3498 | /* Force schedstat enabled if a dependent tracepoint is active */ |
| 3499 | if (trace_sched_stat_wait_enabled() || |
| 3500 | trace_sched_stat_sleep_enabled() || |
| 3501 | trace_sched_stat_iowait_enabled() || |
| 3502 | trace_sched_stat_blocked_enabled() || |
| 3503 | trace_sched_stat_runtime_enabled()) { |
Josh Poimboeuf | eda8dca | 2016-06-13 02:32:09 -0500 | [diff] [blame] | 3504 | printk_deferred_once("Scheduler tracepoints stat_sleep, stat_iowait, " |
Mel Gorman | cb25176 | 2016-02-05 09:08:36 +0000 | [diff] [blame] | 3505 | "stat_blocked and stat_runtime require the " |
| 3506 | "kernel parameter schedstats=enabled or " |
| 3507 | "kernel.sched_schedstats=1\n"); |
| 3508 | } |
| 3509 | #endif |
| 3510 | } |
| 3511 | |
Peter Zijlstra | b5179ac | 2016-05-11 16:10:34 +0200 | [diff] [blame] | 3512 | |
| 3513 | /* |
| 3514 | * MIGRATION |
| 3515 | * |
| 3516 | * dequeue |
| 3517 | * update_curr() |
| 3518 | * update_min_vruntime() |
| 3519 | * vruntime -= min_vruntime |
| 3520 | * |
| 3521 | * enqueue |
| 3522 | * update_curr() |
| 3523 | * update_min_vruntime() |
| 3524 | * vruntime += min_vruntime |
| 3525 | * |
| 3526 | * this way the vruntime transition between RQs is done when both |
| 3527 | * min_vruntime are up-to-date. |
| 3528 | * |
| 3529 | * WAKEUP (remote) |
| 3530 | * |
Peter Zijlstra | 59efa0b | 2016-05-10 18:24:37 +0200 | [diff] [blame] | 3531 | * ->migrate_task_rq_fair() (p->state == TASK_WAKING) |
Peter Zijlstra | b5179ac | 2016-05-11 16:10:34 +0200 | [diff] [blame] | 3532 | * vruntime -= min_vruntime |
| 3533 | * |
| 3534 | * enqueue |
| 3535 | * update_curr() |
| 3536 | * update_min_vruntime() |
| 3537 | * vruntime += min_vruntime |
| 3538 | * |
| 3539 | * this way we don't have the most up-to-date min_vruntime on the originating |
| 3540 | * CPU and an up-to-date min_vruntime on the destination CPU. |
| 3541 | */ |
| 3542 | |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 3543 | static void |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 3544 | enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3545 | { |
Peter Zijlstra | 2f95035 | 2016-05-11 19:27:56 +0200 | [diff] [blame] | 3546 | bool renorm = !(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_MIGRATED); |
| 3547 | bool curr = cfs_rq->curr == se; |
Peter Zijlstra | 3a47d51 | 2016-03-09 13:04:03 +0100 | [diff] [blame] | 3548 | |
Ingo Molnar | 53d3bc7 | 2016-05-11 08:25:53 +0200 | [diff] [blame] | 3549 | /* |
Peter Zijlstra | 2f95035 | 2016-05-11 19:27:56 +0200 | [diff] [blame] | 3550 | * If we're the current task, we must renormalise before calling |
| 3551 | * update_curr(). |
Ingo Molnar | 53d3bc7 | 2016-05-11 08:25:53 +0200 | [diff] [blame] | 3552 | */ |
Peter Zijlstra | 2f95035 | 2016-05-11 19:27:56 +0200 | [diff] [blame] | 3553 | if (renorm && curr) |
| 3554 | se->vruntime += cfs_rq->min_vruntime; |
| 3555 | |
Ingo Molnar | b7cc089 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 3556 | update_curr(cfs_rq); |
Peter Zijlstra | 2f95035 | 2016-05-11 19:27:56 +0200 | [diff] [blame] | 3557 | |
| 3558 | /* |
| 3559 | * Otherwise, renormalise after, such that we're placed at the current |
| 3560 | * moment in time, instead of some random moment in the past. Being |
| 3561 | * placed in the past could significantly boost this task to the |
| 3562 | * fairness detriment of existing tasks. |
| 3563 | */ |
| 3564 | if (renorm && !curr) |
| 3565 | se->vruntime += cfs_rq->min_vruntime; |
| 3566 | |
Vincent Guittot | d31b1a6 | 2016-11-08 10:53:44 +0100 | [diff] [blame] | 3567 | update_load_avg(se, UPDATE_TG); |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 3568 | enqueue_entity_load_avg(cfs_rq, se); |
Linus Torvalds | 17bc14b | 2012-12-14 07:20:43 -0800 | [diff] [blame] | 3569 | account_entity_enqueue(cfs_rq, se); |
| 3570 | update_cfs_shares(cfs_rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3571 | |
Josh Poimboeuf | 1a3d027 | 2016-06-17 12:43:23 -0500 | [diff] [blame] | 3572 | if (flags & ENQUEUE_WAKEUP) |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 3573 | place_entity(cfs_rq, se, 0); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3574 | |
Mel Gorman | cb25176 | 2016-02-05 09:08:36 +0000 | [diff] [blame] | 3575 | check_schedstat_required(); |
Josh Poimboeuf | 4fa8d29 | 2016-06-17 12:43:26 -0500 | [diff] [blame] | 3576 | update_stats_enqueue(cfs_rq, se, flags); |
| 3577 | check_spread(cfs_rq, se); |
Peter Zijlstra | 2f95035 | 2016-05-11 19:27:56 +0200 | [diff] [blame] | 3578 | if (!curr) |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 3579 | __enqueue_entity(cfs_rq, se); |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 3580 | se->on_rq = 1; |
Peter Zijlstra | 3d4b47b | 2010-11-15 15:47:01 -0800 | [diff] [blame] | 3581 | |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 3582 | if (cfs_rq->nr_running == 1) { |
Peter Zijlstra | 3d4b47b | 2010-11-15 15:47:01 -0800 | [diff] [blame] | 3583 | list_add_leaf_cfs_rq(cfs_rq); |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 3584 | check_enqueue_throttle(cfs_rq); |
| 3585 | } |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3586 | } |
| 3587 | |
Rik van Riel | 2c13c919 | 2011-02-01 09:48:37 -0500 | [diff] [blame] | 3588 | static void __clear_buddies_last(struct sched_entity *se) |
Peter Zijlstra | 2002c69 | 2008-11-11 11:52:33 +0100 | [diff] [blame] | 3589 | { |
Rik van Riel | 2c13c919 | 2011-02-01 09:48:37 -0500 | [diff] [blame] | 3590 | for_each_sched_entity(se) { |
| 3591 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
Peter Zijlstra | f104479 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 3592 | if (cfs_rq->last != se) |
Rik van Riel | 2c13c919 | 2011-02-01 09:48:37 -0500 | [diff] [blame] | 3593 | break; |
Peter Zijlstra | f104479 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 3594 | |
| 3595 | cfs_rq->last = NULL; |
Rik van Riel | 2c13c919 | 2011-02-01 09:48:37 -0500 | [diff] [blame] | 3596 | } |
| 3597 | } |
Peter Zijlstra | 2002c69 | 2008-11-11 11:52:33 +0100 | [diff] [blame] | 3598 | |
Rik van Riel | 2c13c919 | 2011-02-01 09:48:37 -0500 | [diff] [blame] | 3599 | static void __clear_buddies_next(struct sched_entity *se) |
| 3600 | { |
| 3601 | for_each_sched_entity(se) { |
| 3602 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
Peter Zijlstra | f104479 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 3603 | if (cfs_rq->next != se) |
Rik van Riel | 2c13c919 | 2011-02-01 09:48:37 -0500 | [diff] [blame] | 3604 | break; |
Peter Zijlstra | f104479 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 3605 | |
| 3606 | cfs_rq->next = NULL; |
Rik van Riel | 2c13c919 | 2011-02-01 09:48:37 -0500 | [diff] [blame] | 3607 | } |
Peter Zijlstra | 2002c69 | 2008-11-11 11:52:33 +0100 | [diff] [blame] | 3608 | } |
| 3609 | |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 3610 | static void __clear_buddies_skip(struct sched_entity *se) |
| 3611 | { |
| 3612 | for_each_sched_entity(se) { |
| 3613 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
Peter Zijlstra | f104479 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 3614 | if (cfs_rq->skip != se) |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 3615 | break; |
Peter Zijlstra | f104479 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 3616 | |
| 3617 | cfs_rq->skip = NULL; |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 3618 | } |
| 3619 | } |
| 3620 | |
Peter Zijlstra | a571bbe | 2009-01-28 14:51:40 +0100 | [diff] [blame] | 3621 | static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| 3622 | { |
Rik van Riel | 2c13c919 | 2011-02-01 09:48:37 -0500 | [diff] [blame] | 3623 | if (cfs_rq->last == se) |
| 3624 | __clear_buddies_last(se); |
| 3625 | |
| 3626 | if (cfs_rq->next == se) |
| 3627 | __clear_buddies_next(se); |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 3628 | |
| 3629 | if (cfs_rq->skip == se) |
| 3630 | __clear_buddies_skip(se); |
Peter Zijlstra | a571bbe | 2009-01-28 14:51:40 +0100 | [diff] [blame] | 3631 | } |
| 3632 | |
Peter Zijlstra | 6c16a6d | 2012-03-21 13:07:16 -0700 | [diff] [blame] | 3633 | static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq); |
Paul Turner | d8b4986 | 2011-07-21 09:43:41 -0700 | [diff] [blame] | 3634 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3635 | static void |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 3636 | dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3637 | { |
Dmitry Adamushko | a2a2d68 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 3638 | /* |
| 3639 | * Update run-time statistics of the 'current'. |
| 3640 | */ |
| 3641 | update_curr(cfs_rq); |
Vincent Guittot | d31b1a6 | 2016-11-08 10:53:44 +0100 | [diff] [blame] | 3642 | update_load_avg(se, UPDATE_TG); |
Yuyang Du | 1396223 | 2015-07-15 08:04:41 +0800 | [diff] [blame] | 3643 | dequeue_entity_load_avg(cfs_rq, se); |
Dmitry Adamushko | a2a2d68 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 3644 | |
Josh Poimboeuf | 4fa8d29 | 2016-06-17 12:43:26 -0500 | [diff] [blame] | 3645 | update_stats_dequeue(cfs_rq, se, flags); |
Peter Zijlstra | 67e9fb2 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 3646 | |
Peter Zijlstra | 2002c69 | 2008-11-11 11:52:33 +0100 | [diff] [blame] | 3647 | clear_buddies(cfs_rq, se); |
Peter Zijlstra | 4793241 | 2008-11-04 21:25:09 +0100 | [diff] [blame] | 3648 | |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 3649 | if (se != cfs_rq->curr) |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 3650 | __dequeue_entity(cfs_rq, se); |
Linus Torvalds | 17bc14b | 2012-12-14 07:20:43 -0800 | [diff] [blame] | 3651 | se->on_rq = 0; |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 3652 | account_entity_dequeue(cfs_rq, se); |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 3653 | |
| 3654 | /* |
Peter Zijlstra | b60205c | 2016-09-20 21:58:12 +0200 | [diff] [blame] | 3655 | * Normalize after update_curr(); which will also have moved |
| 3656 | * min_vruntime if @se is the one holding it back. But before doing |
| 3657 | * update_min_vruntime() again, which will discount @se's position and |
| 3658 | * can move min_vruntime forward still more. |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 3659 | */ |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 3660 | if (!(flags & DEQUEUE_SLEEP)) |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 3661 | se->vruntime -= cfs_rq->min_vruntime; |
Peter Zijlstra | 1e87623 | 2011-05-17 16:21:10 -0700 | [diff] [blame] | 3662 | |
Paul Turner | d8b4986 | 2011-07-21 09:43:41 -0700 | [diff] [blame] | 3663 | /* return excess runtime on last dequeue */ |
| 3664 | return_cfs_rq_runtime(cfs_rq); |
| 3665 | |
Linus Torvalds | 17bc14b | 2012-12-14 07:20:43 -0800 | [diff] [blame] | 3666 | update_cfs_shares(cfs_rq); |
Peter Zijlstra | b60205c | 2016-09-20 21:58:12 +0200 | [diff] [blame] | 3667 | |
| 3668 | /* |
| 3669 | * Now advance min_vruntime if @se was the entity holding it back, |
| 3670 | * except when: DEQUEUE_SAVE && !DEQUEUE_MOVE, in this case we'll be |
| 3671 | * put back on, and if we advance min_vruntime, we'll be placed back |
| 3672 | * further than we started -- ie. we'll be penalized. |
| 3673 | */ |
| 3674 | if ((flags & (DEQUEUE_SAVE | DEQUEUE_MOVE)) == DEQUEUE_SAVE) |
| 3675 | update_min_vruntime(cfs_rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3676 | } |
| 3677 | |
| 3678 | /* |
| 3679 | * Preempt the current task with a newly woken task if needed: |
| 3680 | */ |
Peter Zijlstra | 7c92e54 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 3681 | static void |
Ingo Molnar | 2e09bf5 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 3682 | check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3683 | { |
Peter Zijlstra | 1169783 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 3684 | unsigned long ideal_runtime, delta_exec; |
Wang Xingchao | f4cfb33 | 2011-09-16 13:35:52 -0400 | [diff] [blame] | 3685 | struct sched_entity *se; |
| 3686 | s64 delta; |
Peter Zijlstra | 1169783 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 3687 | |
Peter Zijlstra | 6d0f0eb | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 3688 | ideal_runtime = sched_slice(cfs_rq, curr); |
Peter Zijlstra | 1169783 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 3689 | delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime; |
Mike Galbraith | a9f3e2b | 2009-01-28 14:51:39 +0100 | [diff] [blame] | 3690 | if (delta_exec > ideal_runtime) { |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 3691 | resched_curr(rq_of(cfs_rq)); |
Mike Galbraith | a9f3e2b | 2009-01-28 14:51:39 +0100 | [diff] [blame] | 3692 | /* |
| 3693 | * The current task ran long enough, ensure it doesn't get |
| 3694 | * re-elected due to buddy favours. |
| 3695 | */ |
| 3696 | clear_buddies(cfs_rq, curr); |
Mike Galbraith | f685cea | 2009-10-23 23:09:22 +0200 | [diff] [blame] | 3697 | return; |
| 3698 | } |
| 3699 | |
| 3700 | /* |
| 3701 | * Ensure that a task that missed wakeup preemption by a |
| 3702 | * narrow margin doesn't have to wait for a full slice. |
| 3703 | * This also mitigates buddy induced latencies under load. |
| 3704 | */ |
Mike Galbraith | f685cea | 2009-10-23 23:09:22 +0200 | [diff] [blame] | 3705 | if (delta_exec < sysctl_sched_min_granularity) |
| 3706 | return; |
| 3707 | |
Wang Xingchao | f4cfb33 | 2011-09-16 13:35:52 -0400 | [diff] [blame] | 3708 | se = __pick_first_entity(cfs_rq); |
| 3709 | delta = curr->vruntime - se->vruntime; |
Mike Galbraith | f685cea | 2009-10-23 23:09:22 +0200 | [diff] [blame] | 3710 | |
Wang Xingchao | f4cfb33 | 2011-09-16 13:35:52 -0400 | [diff] [blame] | 3711 | if (delta < 0) |
| 3712 | return; |
Mike Galbraith | d7d8294 | 2011-01-05 05:41:17 +0100 | [diff] [blame] | 3713 | |
Wang Xingchao | f4cfb33 | 2011-09-16 13:35:52 -0400 | [diff] [blame] | 3714 | if (delta > ideal_runtime) |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 3715 | resched_curr(rq_of(cfs_rq)); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3716 | } |
| 3717 | |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 3718 | static void |
Ingo Molnar | 8494f41 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 3719 | set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3720 | { |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 3721 | /* 'current' is not kept within the tree. */ |
| 3722 | if (se->on_rq) { |
| 3723 | /* |
| 3724 | * Any task has to be enqueued before it get to execute on |
| 3725 | * a CPU. So account for the time it spent waiting on the |
| 3726 | * runqueue. |
| 3727 | */ |
Josh Poimboeuf | 4fa8d29 | 2016-06-17 12:43:26 -0500 | [diff] [blame] | 3728 | update_stats_wait_end(cfs_rq, se); |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 3729 | __dequeue_entity(cfs_rq, se); |
Vincent Guittot | d31b1a6 | 2016-11-08 10:53:44 +0100 | [diff] [blame] | 3730 | update_load_avg(se, UPDATE_TG); |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 3731 | } |
| 3732 | |
Ingo Molnar | 79303e9 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 3733 | update_stats_curr_start(cfs_rq, se); |
Ingo Molnar | 429d43b | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 3734 | cfs_rq->curr = se; |
Josh Poimboeuf | 4fa8d29 | 2016-06-17 12:43:26 -0500 | [diff] [blame] | 3735 | |
Ingo Molnar | eba1ed4 | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 3736 | /* |
| 3737 | * Track our maximum slice length, if the CPU's load is at |
| 3738 | * least twice that of our own weight (i.e. dont track it |
| 3739 | * when there are only lesser-weight tasks around): |
| 3740 | */ |
Mel Gorman | cb25176 | 2016-02-05 09:08:36 +0000 | [diff] [blame] | 3741 | if (schedstat_enabled() && rq_of(cfs_rq)->load.weight >= 2*se->load.weight) { |
Josh Poimboeuf | 4fa8d29 | 2016-06-17 12:43:26 -0500 | [diff] [blame] | 3742 | schedstat_set(se->statistics.slice_max, |
| 3743 | max((u64)schedstat_val(se->statistics.slice_max), |
| 3744 | se->sum_exec_runtime - se->prev_sum_exec_runtime)); |
Ingo Molnar | eba1ed4 | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 3745 | } |
Josh Poimboeuf | 4fa8d29 | 2016-06-17 12:43:26 -0500 | [diff] [blame] | 3746 | |
Peter Zijlstra | 4a55b45 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 3747 | se->prev_sum_exec_runtime = se->sum_exec_runtime; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3748 | } |
| 3749 | |
Peter Zijlstra | 3f3a490 | 2008-10-24 11:06:16 +0200 | [diff] [blame] | 3750 | static int |
| 3751 | wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se); |
| 3752 | |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 3753 | /* |
| 3754 | * Pick the next process, keeping these things in mind, in this order: |
| 3755 | * 1) keep things fair between processes/task groups |
| 3756 | * 2) pick the "next" process, since someone really wants that to run |
| 3757 | * 3) pick the "last" process, for cache locality |
| 3758 | * 4) do not run the "skip" process, if something else is available |
| 3759 | */ |
Peter Zijlstra | 678d571 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 3760 | static struct sched_entity * |
| 3761 | pick_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *curr) |
Peter Zijlstra | aa2ac25 | 2008-03-14 21:12:12 +0100 | [diff] [blame] | 3762 | { |
Peter Zijlstra | 678d571 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 3763 | struct sched_entity *left = __pick_first_entity(cfs_rq); |
| 3764 | struct sched_entity *se; |
| 3765 | |
| 3766 | /* |
| 3767 | * If curr is set we have to see if its left of the leftmost entity |
| 3768 | * still in the tree, provided there was anything in the tree at all. |
| 3769 | */ |
| 3770 | if (!left || (curr && entity_before(curr, left))) |
| 3771 | left = curr; |
| 3772 | |
| 3773 | se = left; /* ideally we run the leftmost entity */ |
Peter Zijlstra | f4b6755 | 2008-11-04 21:25:07 +0100 | [diff] [blame] | 3774 | |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 3775 | /* |
| 3776 | * Avoid running the skip buddy, if running something else can |
| 3777 | * be done without getting too unfair. |
| 3778 | */ |
| 3779 | if (cfs_rq->skip == se) { |
Peter Zijlstra | 678d571 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 3780 | struct sched_entity *second; |
| 3781 | |
| 3782 | if (se == curr) { |
| 3783 | second = __pick_first_entity(cfs_rq); |
| 3784 | } else { |
| 3785 | second = __pick_next_entity(se); |
| 3786 | if (!second || (curr && entity_before(curr, second))) |
| 3787 | second = curr; |
| 3788 | } |
| 3789 | |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 3790 | if (second && wakeup_preempt_entity(second, left) < 1) |
| 3791 | se = second; |
| 3792 | } |
Peter Zijlstra | aa2ac25 | 2008-03-14 21:12:12 +0100 | [diff] [blame] | 3793 | |
Mike Galbraith | f685cea | 2009-10-23 23:09:22 +0200 | [diff] [blame] | 3794 | /* |
| 3795 | * Prefer last buddy, try to return the CPU to a preempted task. |
| 3796 | */ |
| 3797 | if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1) |
| 3798 | se = cfs_rq->last; |
| 3799 | |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 3800 | /* |
| 3801 | * Someone really wants this to run. If it's not unfair, run it. |
| 3802 | */ |
| 3803 | if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1) |
| 3804 | se = cfs_rq->next; |
| 3805 | |
Mike Galbraith | f685cea | 2009-10-23 23:09:22 +0200 | [diff] [blame] | 3806 | clear_buddies(cfs_rq, se); |
Peter Zijlstra | 4793241 | 2008-11-04 21:25:09 +0100 | [diff] [blame] | 3807 | |
| 3808 | return se; |
Peter Zijlstra | aa2ac25 | 2008-03-14 21:12:12 +0100 | [diff] [blame] | 3809 | } |
| 3810 | |
Peter Zijlstra | 678d571 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 3811 | static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq); |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 3812 | |
Ingo Molnar | ab6cde2 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 3813 | static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3814 | { |
| 3815 | /* |
| 3816 | * If still on the runqueue then deactivate_task() |
| 3817 | * was not called and update_curr() has to be done: |
| 3818 | */ |
| 3819 | if (prev->on_rq) |
Ingo Molnar | b7cc089 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 3820 | update_curr(cfs_rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3821 | |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 3822 | /* throttle cfs_rqs exceeding runtime */ |
| 3823 | check_cfs_rq_runtime(cfs_rq); |
| 3824 | |
Josh Poimboeuf | 4fa8d29 | 2016-06-17 12:43:26 -0500 | [diff] [blame] | 3825 | check_spread(cfs_rq, prev); |
Mel Gorman | cb25176 | 2016-02-05 09:08:36 +0000 | [diff] [blame] | 3826 | |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 3827 | if (prev->on_rq) { |
Josh Poimboeuf | 4fa8d29 | 2016-06-17 12:43:26 -0500 | [diff] [blame] | 3828 | update_stats_wait_start(cfs_rq, prev); |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 3829 | /* Put 'current' back into the tree. */ |
| 3830 | __enqueue_entity(cfs_rq, prev); |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 3831 | /* in !on_rq case, update occurred at dequeue */ |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 3832 | update_load_avg(prev, 0); |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 3833 | } |
Ingo Molnar | 429d43b | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 3834 | cfs_rq->curr = NULL; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3835 | } |
| 3836 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 3837 | static void |
| 3838 | entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3839 | { |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3840 | /* |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 3841 | * Update run-time statistics of the 'current'. |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3842 | */ |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 3843 | update_curr(cfs_rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3844 | |
Paul Turner | 43365bd | 2010-12-15 19:10:17 -0800 | [diff] [blame] | 3845 | /* |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 3846 | * Ensure that runnable average is periodically updated. |
| 3847 | */ |
Vincent Guittot | d31b1a6 | 2016-11-08 10:53:44 +0100 | [diff] [blame] | 3848 | update_load_avg(curr, UPDATE_TG); |
Peter Zijlstra | bf0bd94 | 2013-07-26 23:48:42 +0200 | [diff] [blame] | 3849 | update_cfs_shares(cfs_rq); |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 3850 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 3851 | #ifdef CONFIG_SCHED_HRTICK |
| 3852 | /* |
| 3853 | * queued ticks are scheduled to match the slice, so don't bother |
| 3854 | * validating it and just reschedule. |
| 3855 | */ |
Harvey Harrison | 983ed7a | 2008-04-24 18:17:55 -0700 | [diff] [blame] | 3856 | if (queued) { |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 3857 | resched_curr(rq_of(cfs_rq)); |
Harvey Harrison | 983ed7a | 2008-04-24 18:17:55 -0700 | [diff] [blame] | 3858 | return; |
| 3859 | } |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 3860 | /* |
| 3861 | * don't let the period tick interfere with the hrtick preemption |
| 3862 | */ |
| 3863 | if (!sched_feat(DOUBLE_TICK) && |
| 3864 | hrtimer_active(&rq_of(cfs_rq)->hrtick_timer)) |
| 3865 | return; |
| 3866 | #endif |
| 3867 | |
Yong Zhang | 2c2efae | 2011-07-29 16:20:33 +0800 | [diff] [blame] | 3868 | if (cfs_rq->nr_running > 1) |
Ingo Molnar | 2e09bf5 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 3869 | check_preempt_tick(cfs_rq, curr); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3870 | } |
| 3871 | |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 3872 | |
| 3873 | /************************************************** |
| 3874 | * CFS bandwidth control machinery |
| 3875 | */ |
| 3876 | |
| 3877 | #ifdef CONFIG_CFS_BANDWIDTH |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 3878 | |
| 3879 | #ifdef HAVE_JUMP_LABEL |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 3880 | static struct static_key __cfs_bandwidth_used; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 3881 | |
| 3882 | static inline bool cfs_bandwidth_used(void) |
| 3883 | { |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 3884 | return static_key_false(&__cfs_bandwidth_used); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 3885 | } |
| 3886 | |
Ben Segall | 1ee14e6 | 2013-10-16 11:16:12 -0700 | [diff] [blame] | 3887 | void cfs_bandwidth_usage_inc(void) |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 3888 | { |
Ben Segall | 1ee14e6 | 2013-10-16 11:16:12 -0700 | [diff] [blame] | 3889 | static_key_slow_inc(&__cfs_bandwidth_used); |
| 3890 | } |
| 3891 | |
| 3892 | void cfs_bandwidth_usage_dec(void) |
| 3893 | { |
| 3894 | static_key_slow_dec(&__cfs_bandwidth_used); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 3895 | } |
| 3896 | #else /* HAVE_JUMP_LABEL */ |
| 3897 | static bool cfs_bandwidth_used(void) |
| 3898 | { |
| 3899 | return true; |
| 3900 | } |
| 3901 | |
Ben Segall | 1ee14e6 | 2013-10-16 11:16:12 -0700 | [diff] [blame] | 3902 | void cfs_bandwidth_usage_inc(void) {} |
| 3903 | void cfs_bandwidth_usage_dec(void) {} |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 3904 | #endif /* HAVE_JUMP_LABEL */ |
| 3905 | |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 3906 | /* |
| 3907 | * default period for cfs group bandwidth. |
| 3908 | * default: 0.1s, units: nanoseconds |
| 3909 | */ |
| 3910 | static inline u64 default_cfs_period(void) |
| 3911 | { |
| 3912 | return 100000000ULL; |
| 3913 | } |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 3914 | |
| 3915 | static inline u64 sched_cfs_bandwidth_slice(void) |
| 3916 | { |
| 3917 | return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC; |
| 3918 | } |
| 3919 | |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 3920 | /* |
| 3921 | * Replenish runtime according to assigned quota and update expiration time. |
| 3922 | * We use sched_clock_cpu directly instead of rq->clock to avoid adding |
| 3923 | * additional synchronization around rq->lock. |
| 3924 | * |
| 3925 | * requires cfs_b->lock |
| 3926 | */ |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 3927 | void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b) |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 3928 | { |
| 3929 | u64 now; |
| 3930 | |
| 3931 | if (cfs_b->quota == RUNTIME_INF) |
| 3932 | return; |
| 3933 | |
| 3934 | now = sched_clock_cpu(smp_processor_id()); |
| 3935 | cfs_b->runtime = cfs_b->quota; |
| 3936 | cfs_b->runtime_expires = now + ktime_to_ns(cfs_b->period); |
| 3937 | } |
| 3938 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 3939 | static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg) |
| 3940 | { |
| 3941 | return &tg->cfs_bandwidth; |
| 3942 | } |
| 3943 | |
Paul Turner | f1b1728 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 3944 | /* rq->task_clock normalized against any time this cfs_rq has spent throttled */ |
| 3945 | static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq) |
| 3946 | { |
| 3947 | if (unlikely(cfs_rq->throttle_count)) |
Xunlei Pang | 1a99ae3 | 2016-05-10 21:03:18 +0800 | [diff] [blame] | 3948 | return cfs_rq->throttled_clock_task - cfs_rq->throttled_clock_task_time; |
Paul Turner | f1b1728 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 3949 | |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 3950 | return rq_clock_task(rq_of(cfs_rq)) - cfs_rq->throttled_clock_task_time; |
Paul Turner | f1b1728 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 3951 | } |
| 3952 | |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 3953 | /* returns 0 on failure to allocate runtime */ |
| 3954 | static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq) |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 3955 | { |
| 3956 | struct task_group *tg = cfs_rq->tg; |
| 3957 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg); |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 3958 | u64 amount = 0, min_amount, expires; |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 3959 | |
| 3960 | /* note: this is a positive sum as runtime_remaining <= 0 */ |
| 3961 | min_amount = sched_cfs_bandwidth_slice() - cfs_rq->runtime_remaining; |
| 3962 | |
| 3963 | raw_spin_lock(&cfs_b->lock); |
| 3964 | if (cfs_b->quota == RUNTIME_INF) |
| 3965 | amount = min_amount; |
Paul Turner | 58088ad | 2011-07-21 09:43:31 -0700 | [diff] [blame] | 3966 | else { |
Peter Zijlstra | 77a4d1a | 2015-04-15 11:41:57 +0200 | [diff] [blame] | 3967 | start_cfs_bandwidth(cfs_b); |
Paul Turner | 58088ad | 2011-07-21 09:43:31 -0700 | [diff] [blame] | 3968 | |
| 3969 | if (cfs_b->runtime > 0) { |
| 3970 | amount = min(cfs_b->runtime, min_amount); |
| 3971 | cfs_b->runtime -= amount; |
| 3972 | cfs_b->idle = 0; |
| 3973 | } |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 3974 | } |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 3975 | expires = cfs_b->runtime_expires; |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 3976 | raw_spin_unlock(&cfs_b->lock); |
| 3977 | |
| 3978 | cfs_rq->runtime_remaining += amount; |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 3979 | /* |
| 3980 | * we may have advanced our local expiration to account for allowed |
| 3981 | * spread between our sched_clock and the one on which runtime was |
| 3982 | * issued. |
| 3983 | */ |
| 3984 | if ((s64)(expires - cfs_rq->runtime_expires) > 0) |
| 3985 | cfs_rq->runtime_expires = expires; |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 3986 | |
| 3987 | return cfs_rq->runtime_remaining > 0; |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 3988 | } |
| 3989 | |
| 3990 | /* |
| 3991 | * Note: This depends on the synchronization provided by sched_clock and the |
| 3992 | * fact that rq->clock snapshots this value. |
| 3993 | */ |
| 3994 | static void expire_cfs_rq_runtime(struct cfs_rq *cfs_rq) |
| 3995 | { |
| 3996 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 3997 | |
| 3998 | /* if the deadline is ahead of our clock, nothing to do */ |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 3999 | if (likely((s64)(rq_clock(rq_of(cfs_rq)) - cfs_rq->runtime_expires) < 0)) |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 4000 | return; |
| 4001 | |
| 4002 | if (cfs_rq->runtime_remaining < 0) |
| 4003 | return; |
| 4004 | |
| 4005 | /* |
| 4006 | * If the local deadline has passed we have to consider the |
| 4007 | * possibility that our sched_clock is 'fast' and the global deadline |
| 4008 | * has not truly expired. |
| 4009 | * |
| 4010 | * Fortunately we can check determine whether this the case by checking |
Ben Segall | 51f2176 | 2014-05-19 15:49:45 -0700 | [diff] [blame] | 4011 | * whether the global deadline has advanced. It is valid to compare |
| 4012 | * cfs_b->runtime_expires without any locks since we only care about |
| 4013 | * exact equality, so a partial write will still work. |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 4014 | */ |
| 4015 | |
Ben Segall | 51f2176 | 2014-05-19 15:49:45 -0700 | [diff] [blame] | 4016 | if (cfs_rq->runtime_expires != cfs_b->runtime_expires) { |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 4017 | /* extend local deadline, drift is bounded above by 2 ticks */ |
| 4018 | cfs_rq->runtime_expires += TICK_NSEC; |
| 4019 | } else { |
| 4020 | /* global deadline is ahead, expiration has passed */ |
| 4021 | cfs_rq->runtime_remaining = 0; |
| 4022 | } |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 4023 | } |
| 4024 | |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 4025 | static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec) |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 4026 | { |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 4027 | /* dock delta_exec before expiring quota (as it could span periods) */ |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 4028 | cfs_rq->runtime_remaining -= delta_exec; |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 4029 | expire_cfs_rq_runtime(cfs_rq); |
| 4030 | |
| 4031 | if (likely(cfs_rq->runtime_remaining > 0)) |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 4032 | return; |
| 4033 | |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 4034 | /* |
| 4035 | * if we're unable to extend our runtime we resched so that the active |
| 4036 | * hierarchy can be throttled |
| 4037 | */ |
| 4038 | if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr)) |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 4039 | resched_curr(rq_of(cfs_rq)); |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 4040 | } |
| 4041 | |
Peter Zijlstra | 6c16a6d | 2012-03-21 13:07:16 -0700 | [diff] [blame] | 4042 | static __always_inline |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 4043 | void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec) |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 4044 | { |
Paul Turner | 56f570e | 2011-11-07 20:26:33 -0800 | [diff] [blame] | 4045 | if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled) |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 4046 | return; |
| 4047 | |
| 4048 | __account_cfs_rq_runtime(cfs_rq, delta_exec); |
| 4049 | } |
| 4050 | |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 4051 | static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq) |
| 4052 | { |
Paul Turner | 56f570e | 2011-11-07 20:26:33 -0800 | [diff] [blame] | 4053 | return cfs_bandwidth_used() && cfs_rq->throttled; |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 4054 | } |
| 4055 | |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 4056 | /* check whether cfs_rq, or any parent, is throttled */ |
| 4057 | static inline int throttled_hierarchy(struct cfs_rq *cfs_rq) |
| 4058 | { |
Paul Turner | 56f570e | 2011-11-07 20:26:33 -0800 | [diff] [blame] | 4059 | return cfs_bandwidth_used() && cfs_rq->throttle_count; |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 4060 | } |
| 4061 | |
| 4062 | /* |
| 4063 | * Ensure that neither of the group entities corresponding to src_cpu or |
| 4064 | * dest_cpu are members of a throttled hierarchy when performing group |
| 4065 | * load-balance operations. |
| 4066 | */ |
| 4067 | static inline int throttled_lb_pair(struct task_group *tg, |
| 4068 | int src_cpu, int dest_cpu) |
| 4069 | { |
| 4070 | struct cfs_rq *src_cfs_rq, *dest_cfs_rq; |
| 4071 | |
| 4072 | src_cfs_rq = tg->cfs_rq[src_cpu]; |
| 4073 | dest_cfs_rq = tg->cfs_rq[dest_cpu]; |
| 4074 | |
| 4075 | return throttled_hierarchy(src_cfs_rq) || |
| 4076 | throttled_hierarchy(dest_cfs_rq); |
| 4077 | } |
| 4078 | |
| 4079 | /* updated child weight may affect parent so we have to do this bottom up */ |
| 4080 | static int tg_unthrottle_up(struct task_group *tg, void *data) |
| 4081 | { |
| 4082 | struct rq *rq = data; |
| 4083 | struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; |
| 4084 | |
| 4085 | cfs_rq->throttle_count--; |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 4086 | if (!cfs_rq->throttle_count) { |
Paul Turner | f1b1728 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 4087 | /* adjust cfs_rq_clock_task() */ |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 4088 | cfs_rq->throttled_clock_task_time += rq_clock_task(rq) - |
Paul Turner | f1b1728 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 4089 | cfs_rq->throttled_clock_task; |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 4090 | } |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 4091 | |
| 4092 | return 0; |
| 4093 | } |
| 4094 | |
| 4095 | static int tg_throttle_down(struct task_group *tg, void *data) |
| 4096 | { |
| 4097 | struct rq *rq = data; |
| 4098 | struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; |
| 4099 | |
Paul Turner | 8295836 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 4100 | /* group is entering throttled state, stop time */ |
| 4101 | if (!cfs_rq->throttle_count) |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 4102 | cfs_rq->throttled_clock_task = rq_clock_task(rq); |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 4103 | cfs_rq->throttle_count++; |
| 4104 | |
| 4105 | return 0; |
| 4106 | } |
| 4107 | |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 4108 | static void throttle_cfs_rq(struct cfs_rq *cfs_rq) |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 4109 | { |
| 4110 | struct rq *rq = rq_of(cfs_rq); |
| 4111 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); |
| 4112 | struct sched_entity *se; |
| 4113 | long task_delta, dequeue = 1; |
Peter Zijlstra | 77a4d1a | 2015-04-15 11:41:57 +0200 | [diff] [blame] | 4114 | bool empty; |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 4115 | |
| 4116 | se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))]; |
| 4117 | |
Paul Turner | f1b1728 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 4118 | /* freeze hierarchy runnable averages while throttled */ |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 4119 | rcu_read_lock(); |
| 4120 | walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq); |
| 4121 | rcu_read_unlock(); |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 4122 | |
| 4123 | task_delta = cfs_rq->h_nr_running; |
| 4124 | for_each_sched_entity(se) { |
| 4125 | struct cfs_rq *qcfs_rq = cfs_rq_of(se); |
| 4126 | /* throttled entity or throttle-on-deactivate */ |
| 4127 | if (!se->on_rq) |
| 4128 | break; |
| 4129 | |
| 4130 | if (dequeue) |
| 4131 | dequeue_entity(qcfs_rq, se, DEQUEUE_SLEEP); |
| 4132 | qcfs_rq->h_nr_running -= task_delta; |
| 4133 | |
| 4134 | if (qcfs_rq->load.weight) |
| 4135 | dequeue = 0; |
| 4136 | } |
| 4137 | |
| 4138 | if (!se) |
Kirill Tkhai | 7246544 | 2014-05-09 03:00:14 +0400 | [diff] [blame] | 4139 | sub_nr_running(rq, task_delta); |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 4140 | |
| 4141 | cfs_rq->throttled = 1; |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 4142 | cfs_rq->throttled_clock = rq_clock(rq); |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 4143 | raw_spin_lock(&cfs_b->lock); |
Cong Wang | d49db34 | 2015-06-24 12:41:47 -0700 | [diff] [blame] | 4144 | empty = list_empty(&cfs_b->throttled_cfs_rq); |
Peter Zijlstra | 77a4d1a | 2015-04-15 11:41:57 +0200 | [diff] [blame] | 4145 | |
Ben Segall | c06f04c | 2014-06-20 15:21:20 -0700 | [diff] [blame] | 4146 | /* |
| 4147 | * Add to the _head_ of the list, so that an already-started |
| 4148 | * distribute_cfs_runtime will not see us |
| 4149 | */ |
| 4150 | list_add_rcu(&cfs_rq->throttled_list, &cfs_b->throttled_cfs_rq); |
Peter Zijlstra | 77a4d1a | 2015-04-15 11:41:57 +0200 | [diff] [blame] | 4151 | |
| 4152 | /* |
| 4153 | * If we're the first throttled task, make sure the bandwidth |
| 4154 | * timer is running. |
| 4155 | */ |
| 4156 | if (empty) |
| 4157 | start_cfs_bandwidth(cfs_b); |
| 4158 | |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 4159 | raw_spin_unlock(&cfs_b->lock); |
| 4160 | } |
| 4161 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 4162 | void unthrottle_cfs_rq(struct cfs_rq *cfs_rq) |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 4163 | { |
| 4164 | struct rq *rq = rq_of(cfs_rq); |
| 4165 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); |
| 4166 | struct sched_entity *se; |
| 4167 | int enqueue = 1; |
| 4168 | long task_delta; |
| 4169 | |
Michael Wang | 22b958d | 2013-06-04 14:23:39 +0800 | [diff] [blame] | 4170 | se = cfs_rq->tg->se[cpu_of(rq)]; |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 4171 | |
| 4172 | cfs_rq->throttled = 0; |
Frederic Weisbecker | 1a55af2 | 2013-04-12 01:51:01 +0200 | [diff] [blame] | 4173 | |
| 4174 | update_rq_clock(rq); |
| 4175 | |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 4176 | raw_spin_lock(&cfs_b->lock); |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 4177 | cfs_b->throttled_time += rq_clock(rq) - cfs_rq->throttled_clock; |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 4178 | list_del_rcu(&cfs_rq->throttled_list); |
| 4179 | raw_spin_unlock(&cfs_b->lock); |
| 4180 | |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 4181 | /* update hierarchical throttle state */ |
| 4182 | walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq); |
| 4183 | |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 4184 | if (!cfs_rq->load.weight) |
| 4185 | return; |
| 4186 | |
| 4187 | task_delta = cfs_rq->h_nr_running; |
| 4188 | for_each_sched_entity(se) { |
| 4189 | if (se->on_rq) |
| 4190 | enqueue = 0; |
| 4191 | |
| 4192 | cfs_rq = cfs_rq_of(se); |
| 4193 | if (enqueue) |
| 4194 | enqueue_entity(cfs_rq, se, ENQUEUE_WAKEUP); |
| 4195 | cfs_rq->h_nr_running += task_delta; |
| 4196 | |
| 4197 | if (cfs_rq_throttled(cfs_rq)) |
| 4198 | break; |
| 4199 | } |
| 4200 | |
| 4201 | if (!se) |
Kirill Tkhai | 7246544 | 2014-05-09 03:00:14 +0400 | [diff] [blame] | 4202 | add_nr_running(rq, task_delta); |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 4203 | |
| 4204 | /* determine whether we need to wake up potentially idle cpu */ |
| 4205 | if (rq->curr == rq->idle && rq->cfs.nr_running) |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 4206 | resched_curr(rq); |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 4207 | } |
| 4208 | |
| 4209 | static u64 distribute_cfs_runtime(struct cfs_bandwidth *cfs_b, |
| 4210 | u64 remaining, u64 expires) |
| 4211 | { |
| 4212 | struct cfs_rq *cfs_rq; |
Ben Segall | c06f04c | 2014-06-20 15:21:20 -0700 | [diff] [blame] | 4213 | u64 runtime; |
| 4214 | u64 starting_runtime = remaining; |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 4215 | |
| 4216 | rcu_read_lock(); |
| 4217 | list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq, |
| 4218 | throttled_list) { |
| 4219 | struct rq *rq = rq_of(cfs_rq); |
| 4220 | |
| 4221 | raw_spin_lock(&rq->lock); |
| 4222 | if (!cfs_rq_throttled(cfs_rq)) |
| 4223 | goto next; |
| 4224 | |
| 4225 | runtime = -cfs_rq->runtime_remaining + 1; |
| 4226 | if (runtime > remaining) |
| 4227 | runtime = remaining; |
| 4228 | remaining -= runtime; |
| 4229 | |
| 4230 | cfs_rq->runtime_remaining += runtime; |
| 4231 | cfs_rq->runtime_expires = expires; |
| 4232 | |
| 4233 | /* we check whether we're throttled above */ |
| 4234 | if (cfs_rq->runtime_remaining > 0) |
| 4235 | unthrottle_cfs_rq(cfs_rq); |
| 4236 | |
| 4237 | next: |
| 4238 | raw_spin_unlock(&rq->lock); |
| 4239 | |
| 4240 | if (!remaining) |
| 4241 | break; |
| 4242 | } |
| 4243 | rcu_read_unlock(); |
| 4244 | |
Ben Segall | c06f04c | 2014-06-20 15:21:20 -0700 | [diff] [blame] | 4245 | return starting_runtime - remaining; |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 4246 | } |
| 4247 | |
Paul Turner | 58088ad | 2011-07-21 09:43:31 -0700 | [diff] [blame] | 4248 | /* |
| 4249 | * Responsible for refilling a task_group's bandwidth and unthrottling its |
| 4250 | * cfs_rqs as appropriate. If there has been no activity within the last |
| 4251 | * period the timer is deactivated until scheduling resumes; cfs_b->idle is |
| 4252 | * used to track this state. |
| 4253 | */ |
| 4254 | static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun) |
| 4255 | { |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 4256 | u64 runtime, runtime_expires; |
Ben Segall | 51f2176 | 2014-05-19 15:49:45 -0700 | [diff] [blame] | 4257 | int throttled; |
Paul Turner | 58088ad | 2011-07-21 09:43:31 -0700 | [diff] [blame] | 4258 | |
Paul Turner | 58088ad | 2011-07-21 09:43:31 -0700 | [diff] [blame] | 4259 | /* no need to continue the timer with no bandwidth constraint */ |
| 4260 | if (cfs_b->quota == RUNTIME_INF) |
Ben Segall | 51f2176 | 2014-05-19 15:49:45 -0700 | [diff] [blame] | 4261 | goto out_deactivate; |
Paul Turner | 58088ad | 2011-07-21 09:43:31 -0700 | [diff] [blame] | 4262 | |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 4263 | throttled = !list_empty(&cfs_b->throttled_cfs_rq); |
Nikhil Rao | e8da1b1 | 2011-07-21 09:43:40 -0700 | [diff] [blame] | 4264 | cfs_b->nr_periods += overrun; |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 4265 | |
Ben Segall | 51f2176 | 2014-05-19 15:49:45 -0700 | [diff] [blame] | 4266 | /* |
| 4267 | * idle depends on !throttled (for the case of a large deficit), and if |
| 4268 | * we're going inactive then everything else can be deferred |
| 4269 | */ |
| 4270 | if (cfs_b->idle && !throttled) |
| 4271 | goto out_deactivate; |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 4272 | |
| 4273 | __refill_cfs_bandwidth_runtime(cfs_b); |
| 4274 | |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 4275 | if (!throttled) { |
| 4276 | /* mark as potentially idle for the upcoming period */ |
| 4277 | cfs_b->idle = 1; |
Ben Segall | 51f2176 | 2014-05-19 15:49:45 -0700 | [diff] [blame] | 4278 | return 0; |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 4279 | } |
Paul Turner | 58088ad | 2011-07-21 09:43:31 -0700 | [diff] [blame] | 4280 | |
Nikhil Rao | e8da1b1 | 2011-07-21 09:43:40 -0700 | [diff] [blame] | 4281 | /* account preceding periods in which throttling occurred */ |
| 4282 | cfs_b->nr_throttled += overrun; |
| 4283 | |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 4284 | runtime_expires = cfs_b->runtime_expires; |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 4285 | |
| 4286 | /* |
Ben Segall | c06f04c | 2014-06-20 15:21:20 -0700 | [diff] [blame] | 4287 | * This check is repeated as we are holding onto the new bandwidth while |
| 4288 | * we unthrottle. This can potentially race with an unthrottled group |
| 4289 | * trying to acquire new bandwidth from the global pool. This can result |
| 4290 | * in us over-using our runtime if it is all used during this loop, but |
| 4291 | * only by limited amounts in that extreme case. |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 4292 | */ |
Ben Segall | c06f04c | 2014-06-20 15:21:20 -0700 | [diff] [blame] | 4293 | while (throttled && cfs_b->runtime > 0) { |
| 4294 | runtime = cfs_b->runtime; |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 4295 | raw_spin_unlock(&cfs_b->lock); |
| 4296 | /* we can't nest cfs_b->lock while distributing bandwidth */ |
| 4297 | runtime = distribute_cfs_runtime(cfs_b, runtime, |
| 4298 | runtime_expires); |
| 4299 | raw_spin_lock(&cfs_b->lock); |
| 4300 | |
| 4301 | throttled = !list_empty(&cfs_b->throttled_cfs_rq); |
Ben Segall | c06f04c | 2014-06-20 15:21:20 -0700 | [diff] [blame] | 4302 | |
| 4303 | cfs_b->runtime -= min(runtime, cfs_b->runtime); |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 4304 | } |
| 4305 | |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 4306 | /* |
| 4307 | * While we are ensured activity in the period following an |
| 4308 | * unthrottle, this also covers the case in which the new bandwidth is |
| 4309 | * insufficient to cover the existing bandwidth deficit. (Forcing the |
| 4310 | * timer to remain active while there are any throttled entities.) |
| 4311 | */ |
| 4312 | cfs_b->idle = 0; |
Paul Turner | 58088ad | 2011-07-21 09:43:31 -0700 | [diff] [blame] | 4313 | |
Ben Segall | 51f2176 | 2014-05-19 15:49:45 -0700 | [diff] [blame] | 4314 | return 0; |
| 4315 | |
| 4316 | out_deactivate: |
Ben Segall | 51f2176 | 2014-05-19 15:49:45 -0700 | [diff] [blame] | 4317 | return 1; |
Paul Turner | 58088ad | 2011-07-21 09:43:31 -0700 | [diff] [blame] | 4318 | } |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 4319 | |
Paul Turner | d8b4986 | 2011-07-21 09:43:41 -0700 | [diff] [blame] | 4320 | /* a cfs_rq won't donate quota below this amount */ |
| 4321 | static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC; |
| 4322 | /* minimum remaining period time to redistribute slack quota */ |
| 4323 | static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC; |
| 4324 | /* how long we wait to gather additional slack before distributing */ |
| 4325 | static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC; |
| 4326 | |
Ben Segall | db06e78 | 2013-10-16 11:16:17 -0700 | [diff] [blame] | 4327 | /* |
| 4328 | * Are we near the end of the current quota period? |
| 4329 | * |
| 4330 | * Requires cfs_b->lock for hrtimer_expires_remaining to be safe against the |
Thomas Gleixner | 4961b6e | 2015-04-14 21:09:05 +0000 | [diff] [blame] | 4331 | * hrtimer base being cleared by hrtimer_start. In the case of |
Ben Segall | db06e78 | 2013-10-16 11:16:17 -0700 | [diff] [blame] | 4332 | * migrate_hrtimers, base is never cleared, so we are fine. |
| 4333 | */ |
Paul Turner | d8b4986 | 2011-07-21 09:43:41 -0700 | [diff] [blame] | 4334 | static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire) |
| 4335 | { |
| 4336 | struct hrtimer *refresh_timer = &cfs_b->period_timer; |
| 4337 | u64 remaining; |
| 4338 | |
| 4339 | /* if the call-back is running a quota refresh is already occurring */ |
| 4340 | if (hrtimer_callback_running(refresh_timer)) |
| 4341 | return 1; |
| 4342 | |
| 4343 | /* is a quota refresh about to occur? */ |
| 4344 | remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer)); |
| 4345 | if (remaining < min_expire) |
| 4346 | return 1; |
| 4347 | |
| 4348 | return 0; |
| 4349 | } |
| 4350 | |
| 4351 | static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b) |
| 4352 | { |
| 4353 | u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration; |
| 4354 | |
| 4355 | /* if there's a quota refresh soon don't bother with slack */ |
| 4356 | if (runtime_refresh_within(cfs_b, min_left)) |
| 4357 | return; |
| 4358 | |
Peter Zijlstra | 4cfafd3 | 2015-05-14 12:23:11 +0200 | [diff] [blame] | 4359 | hrtimer_start(&cfs_b->slack_timer, |
| 4360 | ns_to_ktime(cfs_bandwidth_slack_period), |
| 4361 | HRTIMER_MODE_REL); |
Paul Turner | d8b4986 | 2011-07-21 09:43:41 -0700 | [diff] [blame] | 4362 | } |
| 4363 | |
| 4364 | /* we know any runtime found here is valid as update_curr() precedes return */ |
| 4365 | static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq) |
| 4366 | { |
| 4367 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); |
| 4368 | s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime; |
| 4369 | |
| 4370 | if (slack_runtime <= 0) |
| 4371 | return; |
| 4372 | |
| 4373 | raw_spin_lock(&cfs_b->lock); |
| 4374 | if (cfs_b->quota != RUNTIME_INF && |
| 4375 | cfs_rq->runtime_expires == cfs_b->runtime_expires) { |
| 4376 | cfs_b->runtime += slack_runtime; |
| 4377 | |
| 4378 | /* we are under rq->lock, defer unthrottling using a timer */ |
| 4379 | if (cfs_b->runtime > sched_cfs_bandwidth_slice() && |
| 4380 | !list_empty(&cfs_b->throttled_cfs_rq)) |
| 4381 | start_cfs_slack_bandwidth(cfs_b); |
| 4382 | } |
| 4383 | raw_spin_unlock(&cfs_b->lock); |
| 4384 | |
| 4385 | /* even if it's not valid for return we don't want to try again */ |
| 4386 | cfs_rq->runtime_remaining -= slack_runtime; |
| 4387 | } |
| 4388 | |
| 4389 | static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) |
| 4390 | { |
Paul Turner | 56f570e | 2011-11-07 20:26:33 -0800 | [diff] [blame] | 4391 | if (!cfs_bandwidth_used()) |
| 4392 | return; |
| 4393 | |
Paul Turner | fccfdc6 | 2011-11-07 20:26:34 -0800 | [diff] [blame] | 4394 | if (!cfs_rq->runtime_enabled || cfs_rq->nr_running) |
Paul Turner | d8b4986 | 2011-07-21 09:43:41 -0700 | [diff] [blame] | 4395 | return; |
| 4396 | |
| 4397 | __return_cfs_rq_runtime(cfs_rq); |
| 4398 | } |
| 4399 | |
| 4400 | /* |
| 4401 | * This is done with a timer (instead of inline with bandwidth return) since |
| 4402 | * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs. |
| 4403 | */ |
| 4404 | static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b) |
| 4405 | { |
| 4406 | u64 runtime = 0, slice = sched_cfs_bandwidth_slice(); |
| 4407 | u64 expires; |
| 4408 | |
| 4409 | /* confirm we're still not at a refresh boundary */ |
Paul Turner | d8b4986 | 2011-07-21 09:43:41 -0700 | [diff] [blame] | 4410 | raw_spin_lock(&cfs_b->lock); |
Ben Segall | db06e78 | 2013-10-16 11:16:17 -0700 | [diff] [blame] | 4411 | if (runtime_refresh_within(cfs_b, min_bandwidth_expiration)) { |
| 4412 | raw_spin_unlock(&cfs_b->lock); |
| 4413 | return; |
| 4414 | } |
| 4415 | |
Ben Segall | c06f04c | 2014-06-20 15:21:20 -0700 | [diff] [blame] | 4416 | if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) |
Paul Turner | d8b4986 | 2011-07-21 09:43:41 -0700 | [diff] [blame] | 4417 | runtime = cfs_b->runtime; |
Ben Segall | c06f04c | 2014-06-20 15:21:20 -0700 | [diff] [blame] | 4418 | |
Paul Turner | d8b4986 | 2011-07-21 09:43:41 -0700 | [diff] [blame] | 4419 | expires = cfs_b->runtime_expires; |
| 4420 | raw_spin_unlock(&cfs_b->lock); |
| 4421 | |
| 4422 | if (!runtime) |
| 4423 | return; |
| 4424 | |
| 4425 | runtime = distribute_cfs_runtime(cfs_b, runtime, expires); |
| 4426 | |
| 4427 | raw_spin_lock(&cfs_b->lock); |
| 4428 | if (expires == cfs_b->runtime_expires) |
Ben Segall | c06f04c | 2014-06-20 15:21:20 -0700 | [diff] [blame] | 4429 | cfs_b->runtime -= min(runtime, cfs_b->runtime); |
Paul Turner | d8b4986 | 2011-07-21 09:43:41 -0700 | [diff] [blame] | 4430 | raw_spin_unlock(&cfs_b->lock); |
| 4431 | } |
| 4432 | |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 4433 | /* |
| 4434 | * When a group wakes up we want to make sure that its quota is not already |
| 4435 | * expired/exceeded, otherwise it may be allowed to steal additional ticks of |
| 4436 | * runtime as update_curr() throttling can not not trigger until it's on-rq. |
| 4437 | */ |
| 4438 | static void check_enqueue_throttle(struct cfs_rq *cfs_rq) |
| 4439 | { |
Paul Turner | 56f570e | 2011-11-07 20:26:33 -0800 | [diff] [blame] | 4440 | if (!cfs_bandwidth_used()) |
| 4441 | return; |
| 4442 | |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 4443 | /* an active group must be handled by the update_curr()->put() path */ |
| 4444 | if (!cfs_rq->runtime_enabled || cfs_rq->curr) |
| 4445 | return; |
| 4446 | |
| 4447 | /* ensure the group is not already throttled */ |
| 4448 | if (cfs_rq_throttled(cfs_rq)) |
| 4449 | return; |
| 4450 | |
| 4451 | /* update runtime allocation */ |
| 4452 | account_cfs_rq_runtime(cfs_rq, 0); |
| 4453 | if (cfs_rq->runtime_remaining <= 0) |
| 4454 | throttle_cfs_rq(cfs_rq); |
| 4455 | } |
| 4456 | |
Peter Zijlstra | 55e16d3 | 2016-06-22 15:14:26 +0200 | [diff] [blame] | 4457 | static void sync_throttle(struct task_group *tg, int cpu) |
| 4458 | { |
| 4459 | struct cfs_rq *pcfs_rq, *cfs_rq; |
| 4460 | |
| 4461 | if (!cfs_bandwidth_used()) |
| 4462 | return; |
| 4463 | |
| 4464 | if (!tg->parent) |
| 4465 | return; |
| 4466 | |
| 4467 | cfs_rq = tg->cfs_rq[cpu]; |
| 4468 | pcfs_rq = tg->parent->cfs_rq[cpu]; |
| 4469 | |
| 4470 | cfs_rq->throttle_count = pcfs_rq->throttle_count; |
Xunlei Pang | b892212 | 2016-07-09 15:54:22 +0800 | [diff] [blame] | 4471 | cfs_rq->throttled_clock_task = rq_clock_task(cpu_rq(cpu)); |
Peter Zijlstra | 55e16d3 | 2016-06-22 15:14:26 +0200 | [diff] [blame] | 4472 | } |
| 4473 | |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 4474 | /* conditionally throttle active cfs_rq's from put_prev_entity() */ |
Peter Zijlstra | 678d571 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 4475 | static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq) |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 4476 | { |
Paul Turner | 56f570e | 2011-11-07 20:26:33 -0800 | [diff] [blame] | 4477 | if (!cfs_bandwidth_used()) |
Peter Zijlstra | 678d571 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 4478 | return false; |
Paul Turner | 56f570e | 2011-11-07 20:26:33 -0800 | [diff] [blame] | 4479 | |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 4480 | if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0)) |
Peter Zijlstra | 678d571 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 4481 | return false; |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 4482 | |
| 4483 | /* |
| 4484 | * it's possible for a throttled entity to be forced into a running |
| 4485 | * state (e.g. set_curr_task), in this case we're finished. |
| 4486 | */ |
| 4487 | if (cfs_rq_throttled(cfs_rq)) |
Peter Zijlstra | 678d571 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 4488 | return true; |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 4489 | |
| 4490 | throttle_cfs_rq(cfs_rq); |
Peter Zijlstra | 678d571 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 4491 | return true; |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 4492 | } |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 4493 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 4494 | static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer) |
| 4495 | { |
| 4496 | struct cfs_bandwidth *cfs_b = |
| 4497 | container_of(timer, struct cfs_bandwidth, slack_timer); |
Peter Zijlstra | 77a4d1a | 2015-04-15 11:41:57 +0200 | [diff] [blame] | 4498 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 4499 | do_sched_cfs_slack_timer(cfs_b); |
| 4500 | |
| 4501 | return HRTIMER_NORESTART; |
| 4502 | } |
| 4503 | |
| 4504 | static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer) |
| 4505 | { |
| 4506 | struct cfs_bandwidth *cfs_b = |
| 4507 | container_of(timer, struct cfs_bandwidth, period_timer); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 4508 | int overrun; |
| 4509 | int idle = 0; |
| 4510 | |
Ben Segall | 51f2176 | 2014-05-19 15:49:45 -0700 | [diff] [blame] | 4511 | raw_spin_lock(&cfs_b->lock); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 4512 | for (;;) { |
Peter Zijlstra | 77a4d1a | 2015-04-15 11:41:57 +0200 | [diff] [blame] | 4513 | overrun = hrtimer_forward_now(timer, cfs_b->period); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 4514 | if (!overrun) |
| 4515 | break; |
| 4516 | |
| 4517 | idle = do_sched_cfs_period_timer(cfs_b, overrun); |
| 4518 | } |
Peter Zijlstra | 4cfafd3 | 2015-05-14 12:23:11 +0200 | [diff] [blame] | 4519 | if (idle) |
| 4520 | cfs_b->period_active = 0; |
Ben Segall | 51f2176 | 2014-05-19 15:49:45 -0700 | [diff] [blame] | 4521 | raw_spin_unlock(&cfs_b->lock); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 4522 | |
| 4523 | return idle ? HRTIMER_NORESTART : HRTIMER_RESTART; |
| 4524 | } |
| 4525 | |
| 4526 | void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) |
| 4527 | { |
| 4528 | raw_spin_lock_init(&cfs_b->lock); |
| 4529 | cfs_b->runtime = 0; |
| 4530 | cfs_b->quota = RUNTIME_INF; |
| 4531 | cfs_b->period = ns_to_ktime(default_cfs_period()); |
| 4532 | |
| 4533 | INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq); |
Peter Zijlstra | 4cfafd3 | 2015-05-14 12:23:11 +0200 | [diff] [blame] | 4534 | hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 4535 | cfs_b->period_timer.function = sched_cfs_period_timer; |
| 4536 | hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 4537 | cfs_b->slack_timer.function = sched_cfs_slack_timer; |
| 4538 | } |
| 4539 | |
| 4540 | static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) |
| 4541 | { |
| 4542 | cfs_rq->runtime_enabled = 0; |
| 4543 | INIT_LIST_HEAD(&cfs_rq->throttled_list); |
| 4544 | } |
| 4545 | |
Peter Zijlstra | 77a4d1a | 2015-04-15 11:41:57 +0200 | [diff] [blame] | 4546 | void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b) |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 4547 | { |
Peter Zijlstra | 4cfafd3 | 2015-05-14 12:23:11 +0200 | [diff] [blame] | 4548 | lockdep_assert_held(&cfs_b->lock); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 4549 | |
Peter Zijlstra | 4cfafd3 | 2015-05-14 12:23:11 +0200 | [diff] [blame] | 4550 | if (!cfs_b->period_active) { |
| 4551 | cfs_b->period_active = 1; |
| 4552 | hrtimer_forward_now(&cfs_b->period_timer, cfs_b->period); |
| 4553 | hrtimer_start_expires(&cfs_b->period_timer, HRTIMER_MODE_ABS_PINNED); |
| 4554 | } |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 4555 | } |
| 4556 | |
| 4557 | static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) |
| 4558 | { |
Tetsuo Handa | 7f1a169 | 2014-12-25 15:51:21 +0900 | [diff] [blame] | 4559 | /* init_cfs_bandwidth() was not called */ |
| 4560 | if (!cfs_b->throttled_cfs_rq.next) |
| 4561 | return; |
| 4562 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 4563 | hrtimer_cancel(&cfs_b->period_timer); |
| 4564 | hrtimer_cancel(&cfs_b->slack_timer); |
| 4565 | } |
| 4566 | |
Kirill Tkhai | 0e59bda | 2014-06-25 12:19:42 +0400 | [diff] [blame] | 4567 | static void __maybe_unused update_runtime_enabled(struct rq *rq) |
| 4568 | { |
| 4569 | struct cfs_rq *cfs_rq; |
| 4570 | |
| 4571 | for_each_leaf_cfs_rq(rq, cfs_rq) { |
| 4572 | struct cfs_bandwidth *cfs_b = &cfs_rq->tg->cfs_bandwidth; |
| 4573 | |
| 4574 | raw_spin_lock(&cfs_b->lock); |
| 4575 | cfs_rq->runtime_enabled = cfs_b->quota != RUNTIME_INF; |
| 4576 | raw_spin_unlock(&cfs_b->lock); |
| 4577 | } |
| 4578 | } |
| 4579 | |
Arnd Bergmann | 38dc334 | 2013-01-25 14:14:22 +0000 | [diff] [blame] | 4580 | static void __maybe_unused unthrottle_offline_cfs_rqs(struct rq *rq) |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 4581 | { |
| 4582 | struct cfs_rq *cfs_rq; |
| 4583 | |
| 4584 | for_each_leaf_cfs_rq(rq, cfs_rq) { |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 4585 | if (!cfs_rq->runtime_enabled) |
| 4586 | continue; |
| 4587 | |
| 4588 | /* |
| 4589 | * clock_task is not advancing so we just need to make sure |
| 4590 | * there's some valid quota amount |
| 4591 | */ |
Ben Segall | 51f2176 | 2014-05-19 15:49:45 -0700 | [diff] [blame] | 4592 | cfs_rq->runtime_remaining = 1; |
Kirill Tkhai | 0e59bda | 2014-06-25 12:19:42 +0400 | [diff] [blame] | 4593 | /* |
| 4594 | * Offline rq is schedulable till cpu is completely disabled |
| 4595 | * in take_cpu_down(), so we prevent new cfs throttling here. |
| 4596 | */ |
| 4597 | cfs_rq->runtime_enabled = 0; |
| 4598 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 4599 | if (cfs_rq_throttled(cfs_rq)) |
| 4600 | unthrottle_cfs_rq(cfs_rq); |
| 4601 | } |
| 4602 | } |
| 4603 | |
| 4604 | #else /* CONFIG_CFS_BANDWIDTH */ |
Paul Turner | f1b1728 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 4605 | static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq) |
| 4606 | { |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 4607 | return rq_clock_task(rq_of(cfs_rq)); |
Paul Turner | f1b1728 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 4608 | } |
| 4609 | |
Peter Zijlstra | 9dbdb15 | 2013-11-18 18:27:06 +0100 | [diff] [blame] | 4610 | static void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec) {} |
Peter Zijlstra | 678d571 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 4611 | static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq) { return false; } |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 4612 | static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {} |
Peter Zijlstra | 55e16d3 | 2016-06-22 15:14:26 +0200 | [diff] [blame] | 4613 | static inline void sync_throttle(struct task_group *tg, int cpu) {} |
Peter Zijlstra | 6c16a6d | 2012-03-21 13:07:16 -0700 | [diff] [blame] | 4614 | static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {} |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 4615 | |
| 4616 | static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq) |
| 4617 | { |
| 4618 | return 0; |
| 4619 | } |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 4620 | |
| 4621 | static inline int throttled_hierarchy(struct cfs_rq *cfs_rq) |
| 4622 | { |
| 4623 | return 0; |
| 4624 | } |
| 4625 | |
| 4626 | static inline int throttled_lb_pair(struct task_group *tg, |
| 4627 | int src_cpu, int dest_cpu) |
| 4628 | { |
| 4629 | return 0; |
| 4630 | } |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 4631 | |
| 4632 | void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {} |
| 4633 | |
| 4634 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 4635 | static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {} |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 4636 | #endif |
| 4637 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 4638 | static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg) |
| 4639 | { |
| 4640 | return NULL; |
| 4641 | } |
| 4642 | static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {} |
Kirill Tkhai | 0e59bda | 2014-06-25 12:19:42 +0400 | [diff] [blame] | 4643 | static inline void update_runtime_enabled(struct rq *rq) {} |
Peter Boonstoppel | a4c96ae | 2012-08-09 15:34:47 -0700 | [diff] [blame] | 4644 | static inline void unthrottle_offline_cfs_rqs(struct rq *rq) {} |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 4645 | |
| 4646 | #endif /* CONFIG_CFS_BANDWIDTH */ |
| 4647 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4648 | /************************************************** |
| 4649 | * CFS operations on tasks: |
| 4650 | */ |
| 4651 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 4652 | #ifdef CONFIG_SCHED_HRTICK |
| 4653 | static void hrtick_start_fair(struct rq *rq, struct task_struct *p) |
| 4654 | { |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 4655 | struct sched_entity *se = &p->se; |
| 4656 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 4657 | |
Peter Zijlstra | 9148a3a | 2016-09-20 22:34:51 +0200 | [diff] [blame] | 4658 | SCHED_WARN_ON(task_rq(p) != rq); |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 4659 | |
Srivatsa Vaddagiri | 8bf46a3 | 2016-09-16 18:28:51 -0700 | [diff] [blame] | 4660 | if (rq->cfs.h_nr_running > 1) { |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 4661 | u64 slice = sched_slice(cfs_rq, se); |
| 4662 | u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime; |
| 4663 | s64 delta = slice - ran; |
| 4664 | |
| 4665 | if (delta < 0) { |
| 4666 | if (rq->curr == p) |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 4667 | resched_curr(rq); |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 4668 | return; |
| 4669 | } |
Peter Zijlstra | 3165651 | 2008-07-18 18:01:23 +0200 | [diff] [blame] | 4670 | hrtick_start(rq, delta); |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 4671 | } |
| 4672 | } |
Peter Zijlstra | a4c2f00 | 2008-10-17 19:27:03 +0200 | [diff] [blame] | 4673 | |
| 4674 | /* |
| 4675 | * called from enqueue/dequeue and updates the hrtick when the |
| 4676 | * current task is from our class and nr_running is low enough |
| 4677 | * to matter. |
| 4678 | */ |
| 4679 | static void hrtick_update(struct rq *rq) |
| 4680 | { |
| 4681 | struct task_struct *curr = rq->curr; |
| 4682 | |
Mike Galbraith | b39e66e | 2011-11-22 15:20:07 +0100 | [diff] [blame] | 4683 | if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class) |
Peter Zijlstra | a4c2f00 | 2008-10-17 19:27:03 +0200 | [diff] [blame] | 4684 | return; |
| 4685 | |
| 4686 | if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency) |
| 4687 | hrtick_start_fair(rq, curr); |
| 4688 | } |
Dhaval Giani | 55e12e5 | 2008-06-24 23:39:43 +0530 | [diff] [blame] | 4689 | #else /* !CONFIG_SCHED_HRTICK */ |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 4690 | static inline void |
| 4691 | hrtick_start_fair(struct rq *rq, struct task_struct *p) |
| 4692 | { |
| 4693 | } |
Peter Zijlstra | a4c2f00 | 2008-10-17 19:27:03 +0200 | [diff] [blame] | 4694 | |
| 4695 | static inline void hrtick_update(struct rq *rq) |
| 4696 | { |
| 4697 | } |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 4698 | #endif |
| 4699 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4700 | /* |
| 4701 | * The enqueue_task method is called before nr_running is |
| 4702 | * increased. Here we update the fair scheduling stats and |
| 4703 | * then put the task into the rbtree: |
| 4704 | */ |
Thomas Gleixner | ea87bb7 | 2010-01-20 20:58:57 +0000 | [diff] [blame] | 4705 | static void |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 4706 | enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4707 | { |
| 4708 | struct cfs_rq *cfs_rq; |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 4709 | struct sched_entity *se = &p->se; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4710 | |
Rafael J. Wysocki | 8c34ab1 | 2016-09-09 23:59:33 +0200 | [diff] [blame] | 4711 | /* |
| 4712 | * If in_iowait is set, the code below may not trigger any cpufreq |
| 4713 | * utilization updates, so do it here explicitly with the IOWAIT flag |
| 4714 | * passed. |
| 4715 | */ |
| 4716 | if (p->in_iowait) |
| 4717 | cpufreq_update_this_cpu(rq, SCHED_CPUFREQ_IOWAIT); |
| 4718 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4719 | for_each_sched_entity(se) { |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 4720 | if (se->on_rq) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4721 | break; |
| 4722 | cfs_rq = cfs_rq_of(se); |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 4723 | enqueue_entity(cfs_rq, se, flags); |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 4724 | |
| 4725 | /* |
| 4726 | * end evaluation on encountering a throttled cfs_rq |
| 4727 | * |
| 4728 | * note: in the case of encountering a throttled cfs_rq we will |
| 4729 | * post the final h_nr_running increment below. |
Peter Zijlstra | e210bff | 2016-06-16 18:51:48 +0200 | [diff] [blame] | 4730 | */ |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 4731 | if (cfs_rq_throttled(cfs_rq)) |
| 4732 | break; |
Paul Turner | 953bfcd | 2011-07-21 09:43:27 -0700 | [diff] [blame] | 4733 | cfs_rq->h_nr_running++; |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 4734 | |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 4735 | flags = ENQUEUE_WAKEUP; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4736 | } |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 4737 | |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 4738 | for_each_sched_entity(se) { |
Lin Ming | 0f31714 | 2011-07-22 09:14:31 +0800 | [diff] [blame] | 4739 | cfs_rq = cfs_rq_of(se); |
Paul Turner | 953bfcd | 2011-07-21 09:43:27 -0700 | [diff] [blame] | 4740 | cfs_rq->h_nr_running++; |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 4741 | |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 4742 | if (cfs_rq_throttled(cfs_rq)) |
| 4743 | break; |
| 4744 | |
Vincent Guittot | d31b1a6 | 2016-11-08 10:53:44 +0100 | [diff] [blame] | 4745 | update_load_avg(se, UPDATE_TG); |
Linus Torvalds | 17bc14b | 2012-12-14 07:20:43 -0800 | [diff] [blame] | 4746 | update_cfs_shares(cfs_rq); |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 4747 | } |
| 4748 | |
Yuyang Du | cd126af | 2015-07-15 08:04:36 +0800 | [diff] [blame] | 4749 | if (!se) |
Kirill Tkhai | 7246544 | 2014-05-09 03:00:14 +0400 | [diff] [blame] | 4750 | add_nr_running(rq, 1); |
Yuyang Du | cd126af | 2015-07-15 08:04:36 +0800 | [diff] [blame] | 4751 | |
Peter Zijlstra | a4c2f00 | 2008-10-17 19:27:03 +0200 | [diff] [blame] | 4752 | hrtick_update(rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4753 | } |
| 4754 | |
Venkatesh Pallipadi | 2f36825 | 2011-04-14 10:30:53 -0700 | [diff] [blame] | 4755 | static void set_next_buddy(struct sched_entity *se); |
| 4756 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4757 | /* |
| 4758 | * The dequeue_task method is called before nr_running is |
| 4759 | * decreased. We remove the task from the rbtree and |
| 4760 | * update the fair scheduling stats: |
| 4761 | */ |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 4762 | static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4763 | { |
| 4764 | struct cfs_rq *cfs_rq; |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 4765 | struct sched_entity *se = &p->se; |
Venkatesh Pallipadi | 2f36825 | 2011-04-14 10:30:53 -0700 | [diff] [blame] | 4766 | int task_sleep = flags & DEQUEUE_SLEEP; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4767 | |
| 4768 | for_each_sched_entity(se) { |
| 4769 | cfs_rq = cfs_rq_of(se); |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 4770 | dequeue_entity(cfs_rq, se, flags); |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 4771 | |
| 4772 | /* |
| 4773 | * end evaluation on encountering a throttled cfs_rq |
| 4774 | * |
| 4775 | * note: in the case of encountering a throttled cfs_rq we will |
| 4776 | * post the final h_nr_running decrement below. |
| 4777 | */ |
| 4778 | if (cfs_rq_throttled(cfs_rq)) |
| 4779 | break; |
Paul Turner | 953bfcd | 2011-07-21 09:43:27 -0700 | [diff] [blame] | 4780 | cfs_rq->h_nr_running--; |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 4781 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4782 | /* Don't dequeue parent if it has other entities besides us */ |
Venkatesh Pallipadi | 2f36825 | 2011-04-14 10:30:53 -0700 | [diff] [blame] | 4783 | if (cfs_rq->load.weight) { |
Konstantin Khlebnikov | 754bd59 | 2016-06-16 15:57:15 +0300 | [diff] [blame] | 4784 | /* Avoid re-evaluating load for this entity: */ |
| 4785 | se = parent_entity(se); |
Venkatesh Pallipadi | 2f36825 | 2011-04-14 10:30:53 -0700 | [diff] [blame] | 4786 | /* |
| 4787 | * Bias pick_next to pick a task from this cfs_rq, as |
| 4788 | * p is sleeping when it is within its sched_slice. |
| 4789 | */ |
Konstantin Khlebnikov | 754bd59 | 2016-06-16 15:57:15 +0300 | [diff] [blame] | 4790 | if (task_sleep && se && !throttled_hierarchy(cfs_rq)) |
| 4791 | set_next_buddy(se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4792 | break; |
Venkatesh Pallipadi | 2f36825 | 2011-04-14 10:30:53 -0700 | [diff] [blame] | 4793 | } |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 4794 | flags |= DEQUEUE_SLEEP; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4795 | } |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 4796 | |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 4797 | for_each_sched_entity(se) { |
Lin Ming | 0f31714 | 2011-07-22 09:14:31 +0800 | [diff] [blame] | 4798 | cfs_rq = cfs_rq_of(se); |
Paul Turner | 953bfcd | 2011-07-21 09:43:27 -0700 | [diff] [blame] | 4799 | cfs_rq->h_nr_running--; |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 4800 | |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 4801 | if (cfs_rq_throttled(cfs_rq)) |
| 4802 | break; |
| 4803 | |
Vincent Guittot | d31b1a6 | 2016-11-08 10:53:44 +0100 | [diff] [blame] | 4804 | update_load_avg(se, UPDATE_TG); |
Linus Torvalds | 17bc14b | 2012-12-14 07:20:43 -0800 | [diff] [blame] | 4805 | update_cfs_shares(cfs_rq); |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 4806 | } |
| 4807 | |
Yuyang Du | cd126af | 2015-07-15 08:04:36 +0800 | [diff] [blame] | 4808 | if (!se) |
Kirill Tkhai | 7246544 | 2014-05-09 03:00:14 +0400 | [diff] [blame] | 4809 | sub_nr_running(rq, 1); |
Yuyang Du | cd126af | 2015-07-15 08:04:36 +0800 | [diff] [blame] | 4810 | |
Peter Zijlstra | a4c2f00 | 2008-10-17 19:27:03 +0200 | [diff] [blame] | 4811 | hrtick_update(rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4812 | } |
| 4813 | |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 4814 | #ifdef CONFIG_SMP |
Peter Zijlstra | 10e2f1a | 2016-05-09 10:38:05 +0200 | [diff] [blame] | 4815 | |
| 4816 | /* Working cpumask for: load_balance, load_balance_newidle. */ |
| 4817 | DEFINE_PER_CPU(cpumask_var_t, load_balance_mask); |
| 4818 | DEFINE_PER_CPU(cpumask_var_t, select_idle_mask); |
| 4819 | |
Frederic Weisbecker | 9fd81dd | 2016-04-19 17:36:51 +0200 | [diff] [blame] | 4820 | #ifdef CONFIG_NO_HZ_COMMON |
Peter Zijlstra | 3289bdb | 2015-04-14 13:19:42 +0200 | [diff] [blame] | 4821 | /* |
| 4822 | * per rq 'load' arrray crap; XXX kill this. |
| 4823 | */ |
| 4824 | |
| 4825 | /* |
Peter Zijlstra | d937cdc | 2015-10-19 13:49:30 +0200 | [diff] [blame] | 4826 | * The exact cpuload calculated at every tick would be: |
Peter Zijlstra | 3289bdb | 2015-04-14 13:19:42 +0200 | [diff] [blame] | 4827 | * |
Peter Zijlstra | d937cdc | 2015-10-19 13:49:30 +0200 | [diff] [blame] | 4828 | * load' = (1 - 1/2^i) * load + (1/2^i) * cur_load |
| 4829 | * |
| 4830 | * If a cpu misses updates for n ticks (as it was idle) and update gets |
| 4831 | * called on the n+1-th tick when cpu may be busy, then we have: |
| 4832 | * |
| 4833 | * load_n = (1 - 1/2^i)^n * load_0 |
| 4834 | * load_n+1 = (1 - 1/2^i) * load_n + (1/2^i) * cur_load |
Peter Zijlstra | 3289bdb | 2015-04-14 13:19:42 +0200 | [diff] [blame] | 4835 | * |
| 4836 | * decay_load_missed() below does efficient calculation of |
Peter Zijlstra | d937cdc | 2015-10-19 13:49:30 +0200 | [diff] [blame] | 4837 | * |
| 4838 | * load' = (1 - 1/2^i)^n * load |
| 4839 | * |
| 4840 | * Because x^(n+m) := x^n * x^m we can decompose any x^n in power-of-2 factors. |
| 4841 | * This allows us to precompute the above in said factors, thereby allowing the |
| 4842 | * reduction of an arbitrary n in O(log_2 n) steps. (See also |
| 4843 | * fixed_power_int()) |
Peter Zijlstra | 3289bdb | 2015-04-14 13:19:42 +0200 | [diff] [blame] | 4844 | * |
| 4845 | * The calculation is approximated on a 128 point scale. |
Peter Zijlstra | 3289bdb | 2015-04-14 13:19:42 +0200 | [diff] [blame] | 4846 | */ |
| 4847 | #define DEGRADE_SHIFT 7 |
Peter Zijlstra | d937cdc | 2015-10-19 13:49:30 +0200 | [diff] [blame] | 4848 | |
| 4849 | static const u8 degrade_zero_ticks[CPU_LOAD_IDX_MAX] = {0, 8, 32, 64, 128}; |
| 4850 | static const u8 degrade_factor[CPU_LOAD_IDX_MAX][DEGRADE_SHIFT + 1] = { |
| 4851 | { 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 4852 | { 64, 32, 8, 0, 0, 0, 0, 0 }, |
| 4853 | { 96, 72, 40, 12, 1, 0, 0, 0 }, |
| 4854 | { 112, 98, 75, 43, 15, 1, 0, 0 }, |
| 4855 | { 120, 112, 98, 76, 45, 16, 2, 0 } |
| 4856 | }; |
Peter Zijlstra | 3289bdb | 2015-04-14 13:19:42 +0200 | [diff] [blame] | 4857 | |
| 4858 | /* |
| 4859 | * Update cpu_load for any missed ticks, due to tickless idle. The backlog |
| 4860 | * would be when CPU is idle and so we just decay the old load without |
| 4861 | * adding any new load. |
| 4862 | */ |
| 4863 | static unsigned long |
| 4864 | decay_load_missed(unsigned long load, unsigned long missed_updates, int idx) |
| 4865 | { |
| 4866 | int j = 0; |
| 4867 | |
| 4868 | if (!missed_updates) |
| 4869 | return load; |
| 4870 | |
| 4871 | if (missed_updates >= degrade_zero_ticks[idx]) |
| 4872 | return 0; |
| 4873 | |
| 4874 | if (idx == 1) |
| 4875 | return load >> missed_updates; |
| 4876 | |
| 4877 | while (missed_updates) { |
| 4878 | if (missed_updates % 2) |
| 4879 | load = (load * degrade_factor[idx][j]) >> DEGRADE_SHIFT; |
| 4880 | |
| 4881 | missed_updates >>= 1; |
| 4882 | j++; |
| 4883 | } |
| 4884 | return load; |
| 4885 | } |
Frederic Weisbecker | 9fd81dd | 2016-04-19 17:36:51 +0200 | [diff] [blame] | 4886 | #endif /* CONFIG_NO_HZ_COMMON */ |
Peter Zijlstra | 3289bdb | 2015-04-14 13:19:42 +0200 | [diff] [blame] | 4887 | |
Byungchul Park | 5954327 | 2015-10-14 18:47:35 +0900 | [diff] [blame] | 4888 | /** |
Frederic Weisbecker | cee1afc | 2016-04-13 15:56:50 +0200 | [diff] [blame] | 4889 | * __cpu_load_update - update the rq->cpu_load[] statistics |
Byungchul Park | 5954327 | 2015-10-14 18:47:35 +0900 | [diff] [blame] | 4890 | * @this_rq: The rq to update statistics for |
| 4891 | * @this_load: The current load |
| 4892 | * @pending_updates: The number of missed updates |
Byungchul Park | 5954327 | 2015-10-14 18:47:35 +0900 | [diff] [blame] | 4893 | * |
Peter Zijlstra | 3289bdb | 2015-04-14 13:19:42 +0200 | [diff] [blame] | 4894 | * Update rq->cpu_load[] statistics. This function is usually called every |
Byungchul Park | 5954327 | 2015-10-14 18:47:35 +0900 | [diff] [blame] | 4895 | * scheduler tick (TICK_NSEC). |
| 4896 | * |
| 4897 | * This function computes a decaying average: |
| 4898 | * |
| 4899 | * load[i]' = (1 - 1/2^i) * load[i] + (1/2^i) * load |
| 4900 | * |
| 4901 | * Because of NOHZ it might not get called on every tick which gives need for |
| 4902 | * the @pending_updates argument. |
| 4903 | * |
| 4904 | * load[i]_n = (1 - 1/2^i) * load[i]_n-1 + (1/2^i) * load_n-1 |
| 4905 | * = A * load[i]_n-1 + B ; A := (1 - 1/2^i), B := (1/2^i) * load |
| 4906 | * = A * (A * load[i]_n-2 + B) + B |
| 4907 | * = A * (A * (A * load[i]_n-3 + B) + B) + B |
| 4908 | * = A^3 * load[i]_n-3 + (A^2 + A + 1) * B |
| 4909 | * = A^n * load[i]_0 + (A^(n-1) + A^(n-2) + ... + 1) * B |
| 4910 | * = A^n * load[i]_0 + ((1 - A^n) / (1 - A)) * B |
| 4911 | * = (1 - 1/2^i)^n * (load[i]_0 - load) + load |
| 4912 | * |
| 4913 | * In the above we've assumed load_n := load, which is true for NOHZ_FULL as |
| 4914 | * any change in load would have resulted in the tick being turned back on. |
| 4915 | * |
| 4916 | * For regular NOHZ, this reduces to: |
| 4917 | * |
| 4918 | * load[i]_n = (1 - 1/2^i)^n * load[i]_0 |
| 4919 | * |
| 4920 | * see decay_load_misses(). For NOHZ_FULL we get to subtract and add the extra |
Frederic Weisbecker | 1f41906 | 2016-04-13 15:56:51 +0200 | [diff] [blame] | 4921 | * term. |
Peter Zijlstra | 3289bdb | 2015-04-14 13:19:42 +0200 | [diff] [blame] | 4922 | */ |
Frederic Weisbecker | 1f41906 | 2016-04-13 15:56:51 +0200 | [diff] [blame] | 4923 | static void cpu_load_update(struct rq *this_rq, unsigned long this_load, |
| 4924 | unsigned long pending_updates) |
Peter Zijlstra | 3289bdb | 2015-04-14 13:19:42 +0200 | [diff] [blame] | 4925 | { |
Frederic Weisbecker | 9fd81dd | 2016-04-19 17:36:51 +0200 | [diff] [blame] | 4926 | unsigned long __maybe_unused tickless_load = this_rq->cpu_load[0]; |
Peter Zijlstra | 3289bdb | 2015-04-14 13:19:42 +0200 | [diff] [blame] | 4927 | int i, scale; |
| 4928 | |
| 4929 | this_rq->nr_load_updates++; |
| 4930 | |
| 4931 | /* Update our load: */ |
| 4932 | this_rq->cpu_load[0] = this_load; /* Fasttrack for idx 0 */ |
| 4933 | for (i = 1, scale = 2; i < CPU_LOAD_IDX_MAX; i++, scale += scale) { |
| 4934 | unsigned long old_load, new_load; |
| 4935 | |
| 4936 | /* scale is effectively 1 << i now, and >> i divides by scale */ |
| 4937 | |
Byungchul Park | 7400d3b | 2016-01-15 16:07:49 +0900 | [diff] [blame] | 4938 | old_load = this_rq->cpu_load[i]; |
Frederic Weisbecker | 9fd81dd | 2016-04-19 17:36:51 +0200 | [diff] [blame] | 4939 | #ifdef CONFIG_NO_HZ_COMMON |
Peter Zijlstra | 3289bdb | 2015-04-14 13:19:42 +0200 | [diff] [blame] | 4940 | old_load = decay_load_missed(old_load, pending_updates - 1, i); |
Byungchul Park | 7400d3b | 2016-01-15 16:07:49 +0900 | [diff] [blame] | 4941 | if (tickless_load) { |
| 4942 | old_load -= decay_load_missed(tickless_load, pending_updates - 1, i); |
| 4943 | /* |
| 4944 | * old_load can never be a negative value because a |
| 4945 | * decayed tickless_load cannot be greater than the |
| 4946 | * original tickless_load. |
| 4947 | */ |
| 4948 | old_load += tickless_load; |
| 4949 | } |
Frederic Weisbecker | 9fd81dd | 2016-04-19 17:36:51 +0200 | [diff] [blame] | 4950 | #endif |
Peter Zijlstra | 3289bdb | 2015-04-14 13:19:42 +0200 | [diff] [blame] | 4951 | new_load = this_load; |
| 4952 | /* |
| 4953 | * Round up the averaging division if load is increasing. This |
| 4954 | * prevents us from getting stuck on 9 if the load is 10, for |
| 4955 | * example. |
| 4956 | */ |
| 4957 | if (new_load > old_load) |
| 4958 | new_load += scale - 1; |
| 4959 | |
| 4960 | this_rq->cpu_load[i] = (old_load * (scale - 1) + new_load) >> i; |
| 4961 | } |
| 4962 | |
| 4963 | sched_avg_update(this_rq); |
| 4964 | } |
| 4965 | |
Yuyang Du | 7ea241a | 2015-07-15 08:04:42 +0800 | [diff] [blame] | 4966 | /* Used instead of source_load when we know the type == 0 */ |
| 4967 | static unsigned long weighted_cpuload(const int cpu) |
| 4968 | { |
| 4969 | return cfs_rq_runnable_load_avg(&cpu_rq(cpu)->cfs); |
| 4970 | } |
| 4971 | |
Peter Zijlstra | 3289bdb | 2015-04-14 13:19:42 +0200 | [diff] [blame] | 4972 | #ifdef CONFIG_NO_HZ_COMMON |
Frederic Weisbecker | 1f41906 | 2016-04-13 15:56:51 +0200 | [diff] [blame] | 4973 | /* |
| 4974 | * There is no sane way to deal with nohz on smp when using jiffies because the |
| 4975 | * cpu doing the jiffies update might drift wrt the cpu doing the jiffy reading |
| 4976 | * causing off-by-one errors in observed deltas; {0,2} instead of {1,1}. |
| 4977 | * |
| 4978 | * Therefore we need to avoid the delta approach from the regular tick when |
| 4979 | * possible since that would seriously skew the load calculation. This is why we |
| 4980 | * use cpu_load_update_periodic() for CPUs out of nohz. However we'll rely on |
| 4981 | * jiffies deltas for updates happening while in nohz mode (idle ticks, idle |
| 4982 | * loop exit, nohz_idle_balance, nohz full exit...) |
| 4983 | * |
| 4984 | * This means we might still be one tick off for nohz periods. |
| 4985 | */ |
| 4986 | |
| 4987 | static void cpu_load_update_nohz(struct rq *this_rq, |
| 4988 | unsigned long curr_jiffies, |
| 4989 | unsigned long load) |
Frederic Weisbecker | be68a68 | 2016-01-13 17:01:29 +0100 | [diff] [blame] | 4990 | { |
| 4991 | unsigned long pending_updates; |
| 4992 | |
| 4993 | pending_updates = curr_jiffies - this_rq->last_load_update_tick; |
| 4994 | if (pending_updates) { |
| 4995 | this_rq->last_load_update_tick = curr_jiffies; |
| 4996 | /* |
| 4997 | * In the regular NOHZ case, we were idle, this means load 0. |
| 4998 | * In the NOHZ_FULL case, we were non-idle, we should consider |
| 4999 | * its weighted load. |
| 5000 | */ |
Frederic Weisbecker | 1f41906 | 2016-04-13 15:56:51 +0200 | [diff] [blame] | 5001 | cpu_load_update(this_rq, load, pending_updates); |
Frederic Weisbecker | be68a68 | 2016-01-13 17:01:29 +0100 | [diff] [blame] | 5002 | } |
| 5003 | } |
| 5004 | |
Peter Zijlstra | 3289bdb | 2015-04-14 13:19:42 +0200 | [diff] [blame] | 5005 | /* |
Peter Zijlstra | 3289bdb | 2015-04-14 13:19:42 +0200 | [diff] [blame] | 5006 | * Called from nohz_idle_balance() to update the load ratings before doing the |
| 5007 | * idle balance. |
| 5008 | */ |
Frederic Weisbecker | cee1afc | 2016-04-13 15:56:50 +0200 | [diff] [blame] | 5009 | static void cpu_load_update_idle(struct rq *this_rq) |
Peter Zijlstra | 3289bdb | 2015-04-14 13:19:42 +0200 | [diff] [blame] | 5010 | { |
Peter Zijlstra | 3289bdb | 2015-04-14 13:19:42 +0200 | [diff] [blame] | 5011 | /* |
| 5012 | * bail if there's load or we're actually up-to-date. |
| 5013 | */ |
Frederic Weisbecker | be68a68 | 2016-01-13 17:01:29 +0100 | [diff] [blame] | 5014 | if (weighted_cpuload(cpu_of(this_rq))) |
Peter Zijlstra | 3289bdb | 2015-04-14 13:19:42 +0200 | [diff] [blame] | 5015 | return; |
| 5016 | |
Frederic Weisbecker | 1f41906 | 2016-04-13 15:56:51 +0200 | [diff] [blame] | 5017 | cpu_load_update_nohz(this_rq, READ_ONCE(jiffies), 0); |
Peter Zijlstra | 3289bdb | 2015-04-14 13:19:42 +0200 | [diff] [blame] | 5018 | } |
| 5019 | |
| 5020 | /* |
Frederic Weisbecker | 1f41906 | 2016-04-13 15:56:51 +0200 | [diff] [blame] | 5021 | * Record CPU load on nohz entry so we know the tickless load to account |
| 5022 | * on nohz exit. cpu_load[0] happens then to be updated more frequently |
| 5023 | * than other cpu_load[idx] but it should be fine as cpu_load readers |
| 5024 | * shouldn't rely into synchronized cpu_load[*] updates. |
Peter Zijlstra | 3289bdb | 2015-04-14 13:19:42 +0200 | [diff] [blame] | 5025 | */ |
Frederic Weisbecker | 1f41906 | 2016-04-13 15:56:51 +0200 | [diff] [blame] | 5026 | void cpu_load_update_nohz_start(void) |
Peter Zijlstra | 3289bdb | 2015-04-14 13:19:42 +0200 | [diff] [blame] | 5027 | { |
| 5028 | struct rq *this_rq = this_rq(); |
Frederic Weisbecker | 1f41906 | 2016-04-13 15:56:51 +0200 | [diff] [blame] | 5029 | |
| 5030 | /* |
| 5031 | * This is all lockless but should be fine. If weighted_cpuload changes |
| 5032 | * concurrently we'll exit nohz. And cpu_load write can race with |
| 5033 | * cpu_load_update_idle() but both updater would be writing the same. |
| 5034 | */ |
| 5035 | this_rq->cpu_load[0] = weighted_cpuload(cpu_of(this_rq)); |
| 5036 | } |
| 5037 | |
| 5038 | /* |
| 5039 | * Account the tickless load in the end of a nohz frame. |
| 5040 | */ |
| 5041 | void cpu_load_update_nohz_stop(void) |
| 5042 | { |
Jason Low | 316c1608d | 2015-04-28 13:00:20 -0700 | [diff] [blame] | 5043 | unsigned long curr_jiffies = READ_ONCE(jiffies); |
Frederic Weisbecker | 1f41906 | 2016-04-13 15:56:51 +0200 | [diff] [blame] | 5044 | struct rq *this_rq = this_rq(); |
| 5045 | unsigned long load; |
Peter Zijlstra | 3289bdb | 2015-04-14 13:19:42 +0200 | [diff] [blame] | 5046 | |
| 5047 | if (curr_jiffies == this_rq->last_load_update_tick) |
| 5048 | return; |
| 5049 | |
Frederic Weisbecker | 1f41906 | 2016-04-13 15:56:51 +0200 | [diff] [blame] | 5050 | load = weighted_cpuload(cpu_of(this_rq)); |
Peter Zijlstra | 3289bdb | 2015-04-14 13:19:42 +0200 | [diff] [blame] | 5051 | raw_spin_lock(&this_rq->lock); |
Matt Fleming | b52fad2 | 2016-05-03 20:46:54 +0100 | [diff] [blame] | 5052 | update_rq_clock(this_rq); |
Frederic Weisbecker | 1f41906 | 2016-04-13 15:56:51 +0200 | [diff] [blame] | 5053 | cpu_load_update_nohz(this_rq, curr_jiffies, load); |
Peter Zijlstra | 3289bdb | 2015-04-14 13:19:42 +0200 | [diff] [blame] | 5054 | raw_spin_unlock(&this_rq->lock); |
| 5055 | } |
Frederic Weisbecker | 1f41906 | 2016-04-13 15:56:51 +0200 | [diff] [blame] | 5056 | #else /* !CONFIG_NO_HZ_COMMON */ |
| 5057 | static inline void cpu_load_update_nohz(struct rq *this_rq, |
| 5058 | unsigned long curr_jiffies, |
| 5059 | unsigned long load) { } |
| 5060 | #endif /* CONFIG_NO_HZ_COMMON */ |
| 5061 | |
| 5062 | static void cpu_load_update_periodic(struct rq *this_rq, unsigned long load) |
| 5063 | { |
Frederic Weisbecker | 9fd81dd | 2016-04-19 17:36:51 +0200 | [diff] [blame] | 5064 | #ifdef CONFIG_NO_HZ_COMMON |
Frederic Weisbecker | 1f41906 | 2016-04-13 15:56:51 +0200 | [diff] [blame] | 5065 | /* See the mess around cpu_load_update_nohz(). */ |
| 5066 | this_rq->last_load_update_tick = READ_ONCE(jiffies); |
Frederic Weisbecker | 9fd81dd | 2016-04-19 17:36:51 +0200 | [diff] [blame] | 5067 | #endif |
Frederic Weisbecker | 1f41906 | 2016-04-13 15:56:51 +0200 | [diff] [blame] | 5068 | cpu_load_update(this_rq, load, 1); |
| 5069 | } |
Peter Zijlstra | 3289bdb | 2015-04-14 13:19:42 +0200 | [diff] [blame] | 5070 | |
| 5071 | /* |
| 5072 | * Called from scheduler_tick() |
| 5073 | */ |
Frederic Weisbecker | cee1afc | 2016-04-13 15:56:50 +0200 | [diff] [blame] | 5074 | void cpu_load_update_active(struct rq *this_rq) |
Peter Zijlstra | 3289bdb | 2015-04-14 13:19:42 +0200 | [diff] [blame] | 5075 | { |
Yuyang Du | 7ea241a | 2015-07-15 08:04:42 +0800 | [diff] [blame] | 5076 | unsigned long load = weighted_cpuload(cpu_of(this_rq)); |
Frederic Weisbecker | 1f41906 | 2016-04-13 15:56:51 +0200 | [diff] [blame] | 5077 | |
| 5078 | if (tick_nohz_tick_stopped()) |
| 5079 | cpu_load_update_nohz(this_rq, READ_ONCE(jiffies), load); |
| 5080 | else |
| 5081 | cpu_load_update_periodic(this_rq, load); |
Peter Zijlstra | 3289bdb | 2015-04-14 13:19:42 +0200 | [diff] [blame] | 5082 | } |
| 5083 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 5084 | /* |
| 5085 | * Return a low guess at the load of a migration-source cpu weighted |
| 5086 | * according to the scheduling class and "nice" value. |
| 5087 | * |
| 5088 | * We want to under-estimate the load of migration sources, to |
| 5089 | * balance conservatively. |
| 5090 | */ |
| 5091 | static unsigned long source_load(int cpu, int type) |
| 5092 | { |
| 5093 | struct rq *rq = cpu_rq(cpu); |
| 5094 | unsigned long total = weighted_cpuload(cpu); |
| 5095 | |
| 5096 | if (type == 0 || !sched_feat(LB_BIAS)) |
| 5097 | return total; |
| 5098 | |
| 5099 | return min(rq->cpu_load[type-1], total); |
| 5100 | } |
| 5101 | |
| 5102 | /* |
| 5103 | * Return a high guess at the load of a migration-target cpu weighted |
| 5104 | * according to the scheduling class and "nice" value. |
| 5105 | */ |
| 5106 | static unsigned long target_load(int cpu, int type) |
| 5107 | { |
| 5108 | struct rq *rq = cpu_rq(cpu); |
| 5109 | unsigned long total = weighted_cpuload(cpu); |
| 5110 | |
| 5111 | if (type == 0 || !sched_feat(LB_BIAS)) |
| 5112 | return total; |
| 5113 | |
| 5114 | return max(rq->cpu_load[type-1], total); |
| 5115 | } |
| 5116 | |
Nicolas Pitre | ced549f | 2014-05-26 18:19:38 -0400 | [diff] [blame] | 5117 | static unsigned long capacity_of(int cpu) |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 5118 | { |
Nicolas Pitre | ced549f | 2014-05-26 18:19:38 -0400 | [diff] [blame] | 5119 | return cpu_rq(cpu)->cpu_capacity; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 5120 | } |
| 5121 | |
Vincent Guittot | ca6d75e | 2015-02-27 16:54:09 +0100 | [diff] [blame] | 5122 | static unsigned long capacity_orig_of(int cpu) |
| 5123 | { |
| 5124 | return cpu_rq(cpu)->cpu_capacity_orig; |
| 5125 | } |
| 5126 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 5127 | static unsigned long cpu_avg_load_per_task(int cpu) |
| 5128 | { |
| 5129 | struct rq *rq = cpu_rq(cpu); |
Jason Low | 316c1608d | 2015-04-28 13:00:20 -0700 | [diff] [blame] | 5130 | unsigned long nr_running = READ_ONCE(rq->cfs.h_nr_running); |
Yuyang Du | 7ea241a | 2015-07-15 08:04:42 +0800 | [diff] [blame] | 5131 | unsigned long load_avg = weighted_cpuload(cpu); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 5132 | |
| 5133 | if (nr_running) |
Alex Shi | b92486c | 2013-06-20 10:18:50 +0800 | [diff] [blame] | 5134 | return load_avg / nr_running; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 5135 | |
| 5136 | return 0; |
| 5137 | } |
| 5138 | |
Peter Zijlstra | bb3469a | 2008-06-27 13:41:27 +0200 | [diff] [blame] | 5139 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Peter Zijlstra | f5bfb7d | 2008-06-27 13:41:39 +0200 | [diff] [blame] | 5140 | /* |
| 5141 | * effective_load() calculates the load change as seen from the root_task_group |
| 5142 | * |
| 5143 | * Adding load to a group doesn't make a group heavier, but can cause movement |
| 5144 | * of group shares between cpus. Assuming the shares were perfectly aligned one |
| 5145 | * can calculate the shift in shares. |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 5146 | * |
| 5147 | * Calculate the effective load difference if @wl is added (subtracted) to @tg |
| 5148 | * on this @cpu and results in a total addition (subtraction) of @wg to the |
| 5149 | * total group weight. |
| 5150 | * |
| 5151 | * Given a runqueue weight distribution (rw_i) we can compute a shares |
| 5152 | * distribution (s_i) using: |
| 5153 | * |
| 5154 | * s_i = rw_i / \Sum rw_j (1) |
| 5155 | * |
| 5156 | * Suppose we have 4 CPUs and our @tg is a direct child of the root group and |
| 5157 | * has 7 equal weight tasks, distributed as below (rw_i), with the resulting |
| 5158 | * shares distribution (s_i): |
| 5159 | * |
| 5160 | * rw_i = { 2, 4, 1, 0 } |
| 5161 | * s_i = { 2/7, 4/7, 1/7, 0 } |
| 5162 | * |
| 5163 | * As per wake_affine() we're interested in the load of two CPUs (the CPU the |
| 5164 | * task used to run on and the CPU the waker is running on), we need to |
| 5165 | * compute the effect of waking a task on either CPU and, in case of a sync |
| 5166 | * wakeup, compute the effect of the current task going to sleep. |
| 5167 | * |
| 5168 | * So for a change of @wl to the local @cpu with an overall group weight change |
| 5169 | * of @wl we can compute the new shares distribution (s'_i) using: |
| 5170 | * |
| 5171 | * s'_i = (rw_i + @wl) / (@wg + \Sum rw_j) (2) |
| 5172 | * |
| 5173 | * Suppose we're interested in CPUs 0 and 1, and want to compute the load |
| 5174 | * differences in waking a task to CPU 0. The additional task changes the |
| 5175 | * weight and shares distributions like: |
| 5176 | * |
| 5177 | * rw'_i = { 3, 4, 1, 0 } |
| 5178 | * s'_i = { 3/8, 4/8, 1/8, 0 } |
| 5179 | * |
| 5180 | * We can then compute the difference in effective weight by using: |
| 5181 | * |
| 5182 | * dw_i = S * (s'_i - s_i) (3) |
| 5183 | * |
| 5184 | * Where 'S' is the group weight as seen by its parent. |
| 5185 | * |
| 5186 | * Therefore the effective change in loads on CPU 0 would be 5/56 (3/8 - 2/7) |
| 5187 | * times the weight of the group. The effect on CPU 1 would be -4/56 (4/8 - |
| 5188 | * 4/7) times the weight of the group. |
Peter Zijlstra | f5bfb7d | 2008-06-27 13:41:39 +0200 | [diff] [blame] | 5189 | */ |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 5190 | static long effective_load(struct task_group *tg, int cpu, long wl, long wg) |
Peter Zijlstra | bb3469a | 2008-06-27 13:41:27 +0200 | [diff] [blame] | 5191 | { |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 5192 | struct sched_entity *se = tg->se[cpu]; |
Peter Zijlstra | f1d239f | 2008-06-27 13:41:38 +0200 | [diff] [blame] | 5193 | |
Rik van Riel | 9722c2d | 2014-01-06 11:39:12 +0000 | [diff] [blame] | 5194 | if (!tg->parent) /* the trivial, non-cgroup case */ |
Peter Zijlstra | f1d239f | 2008-06-27 13:41:38 +0200 | [diff] [blame] | 5195 | return wl; |
| 5196 | |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 5197 | for_each_sched_entity(se) { |
Peter Zijlstra | 7dd4912 | 2016-06-24 15:53:54 +0200 | [diff] [blame] | 5198 | struct cfs_rq *cfs_rq = se->my_q; |
| 5199 | long W, w = cfs_rq_load_avg(cfs_rq); |
Peter Zijlstra | bb3469a | 2008-06-27 13:41:27 +0200 | [diff] [blame] | 5200 | |
Peter Zijlstra | 7dd4912 | 2016-06-24 15:53:54 +0200 | [diff] [blame] | 5201 | tg = cfs_rq->tg; |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 5202 | |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 5203 | /* |
| 5204 | * W = @wg + \Sum rw_j |
| 5205 | */ |
Peter Zijlstra | 7dd4912 | 2016-06-24 15:53:54 +0200 | [diff] [blame] | 5206 | W = wg + atomic_long_read(&tg->load_avg); |
| 5207 | |
| 5208 | /* Ensure \Sum rw_j >= rw_i */ |
| 5209 | W -= cfs_rq->tg_load_avg_contrib; |
| 5210 | W += w; |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 5211 | |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 5212 | /* |
| 5213 | * w = rw_i + @wl |
| 5214 | */ |
Peter Zijlstra | 7dd4912 | 2016-06-24 15:53:54 +0200 | [diff] [blame] | 5215 | w += wl; |
Peter Zijlstra | 940959e | 2008-09-23 15:33:42 +0200 | [diff] [blame] | 5216 | |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 5217 | /* |
| 5218 | * wl = S * s'_i; see (2) |
| 5219 | */ |
| 5220 | if (W > 0 && w < W) |
Dietmar Eggemann | ab522e3 | 2016-08-22 15:00:41 +0100 | [diff] [blame] | 5221 | wl = (w * (long)scale_load_down(tg->shares)) / W; |
Paul Turner | 977dda7 | 2011-01-14 17:57:50 -0800 | [diff] [blame] | 5222 | else |
Dietmar Eggemann | ab522e3 | 2016-08-22 15:00:41 +0100 | [diff] [blame] | 5223 | wl = scale_load_down(tg->shares); |
Peter Zijlstra | 940959e | 2008-09-23 15:33:42 +0200 | [diff] [blame] | 5224 | |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 5225 | /* |
| 5226 | * Per the above, wl is the new se->load.weight value; since |
| 5227 | * those are clipped to [MIN_SHARES, ...) do so now. See |
| 5228 | * calc_cfs_shares(). |
| 5229 | */ |
Paul Turner | 977dda7 | 2011-01-14 17:57:50 -0800 | [diff] [blame] | 5230 | if (wl < MIN_SHARES) |
| 5231 | wl = MIN_SHARES; |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 5232 | |
| 5233 | /* |
| 5234 | * wl = dw_i = S * (s'_i - s_i); see (3) |
| 5235 | */ |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 5236 | wl -= se->avg.load_avg; |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 5237 | |
| 5238 | /* |
| 5239 | * Recursively apply this logic to all parent groups to compute |
| 5240 | * the final effective load change on the root group. Since |
| 5241 | * only the @tg group gets extra weight, all parent groups can |
| 5242 | * only redistribute existing shares. @wl is the shift in shares |
| 5243 | * resulting from this level per the above. |
| 5244 | */ |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 5245 | wg = 0; |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 5246 | } |
| 5247 | |
| 5248 | return wl; |
Peter Zijlstra | bb3469a | 2008-06-27 13:41:27 +0200 | [diff] [blame] | 5249 | } |
| 5250 | #else |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 5251 | |
Mel Gorman | 58d081b | 2013-10-07 11:29:10 +0100 | [diff] [blame] | 5252 | static long effective_load(struct task_group *tg, int cpu, long wl, long wg) |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 5253 | { |
Peter Zijlstra | 8337826 | 2008-06-27 13:41:37 +0200 | [diff] [blame] | 5254 | return wl; |
Peter Zijlstra | bb3469a | 2008-06-27 13:41:27 +0200 | [diff] [blame] | 5255 | } |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 5256 | |
Peter Zijlstra | bb3469a | 2008-06-27 13:41:27 +0200 | [diff] [blame] | 5257 | #endif |
| 5258 | |
Peter Zijlstra | c58d25f | 2016-05-12 09:19:59 +0200 | [diff] [blame] | 5259 | static void record_wakee(struct task_struct *p) |
| 5260 | { |
| 5261 | /* |
| 5262 | * Only decay a single time; tasks that have less then 1 wakeup per |
| 5263 | * jiffy will not have built up many flips. |
| 5264 | */ |
| 5265 | if (time_after(jiffies, current->wakee_flip_decay_ts + HZ)) { |
| 5266 | current->wakee_flips >>= 1; |
| 5267 | current->wakee_flip_decay_ts = jiffies; |
| 5268 | } |
| 5269 | |
| 5270 | if (current->last_wakee != p) { |
| 5271 | current->last_wakee = p; |
| 5272 | current->wakee_flips++; |
| 5273 | } |
| 5274 | } |
| 5275 | |
Mike Galbraith | 63b0e9e | 2015-07-14 17:39:50 +0200 | [diff] [blame] | 5276 | /* |
| 5277 | * Detect M:N waker/wakee relationships via a switching-frequency heuristic. |
Peter Zijlstra | c58d25f | 2016-05-12 09:19:59 +0200 | [diff] [blame] | 5278 | * |
Mike Galbraith | 63b0e9e | 2015-07-14 17:39:50 +0200 | [diff] [blame] | 5279 | * A waker of many should wake a different task than the one last awakened |
Peter Zijlstra | c58d25f | 2016-05-12 09:19:59 +0200 | [diff] [blame] | 5280 | * at a frequency roughly N times higher than one of its wakees. |
| 5281 | * |
| 5282 | * In order to determine whether we should let the load spread vs consolidating |
| 5283 | * to shared cache, we look for a minimum 'flip' frequency of llc_size in one |
| 5284 | * partner, and a factor of lls_size higher frequency in the other. |
| 5285 | * |
| 5286 | * With both conditions met, we can be relatively sure that the relationship is |
| 5287 | * non-monogamous, with partner count exceeding socket size. |
| 5288 | * |
| 5289 | * Waker/wakee being client/server, worker/dispatcher, interrupt source or |
| 5290 | * whatever is irrelevant, spread criteria is apparent partner count exceeds |
| 5291 | * socket size. |
Mike Galbraith | 63b0e9e | 2015-07-14 17:39:50 +0200 | [diff] [blame] | 5292 | */ |
Michael Wang | 6247041 | 2013-07-04 12:55:51 +0800 | [diff] [blame] | 5293 | static int wake_wide(struct task_struct *p) |
| 5294 | { |
Mike Galbraith | 63b0e9e | 2015-07-14 17:39:50 +0200 | [diff] [blame] | 5295 | unsigned int master = current->wakee_flips; |
| 5296 | unsigned int slave = p->wakee_flips; |
Peter Zijlstra | 7d9ffa8 | 2013-07-04 12:56:46 +0800 | [diff] [blame] | 5297 | int factor = this_cpu_read(sd_llc_size); |
Michael Wang | 6247041 | 2013-07-04 12:55:51 +0800 | [diff] [blame] | 5298 | |
Mike Galbraith | 63b0e9e | 2015-07-14 17:39:50 +0200 | [diff] [blame] | 5299 | if (master < slave) |
| 5300 | swap(master, slave); |
| 5301 | if (slave < factor || master < slave * factor) |
| 5302 | return 0; |
| 5303 | return 1; |
Michael Wang | 6247041 | 2013-07-04 12:55:51 +0800 | [diff] [blame] | 5304 | } |
| 5305 | |
Morten Rasmussen | 772bd008c | 2016-06-22 18:03:13 +0100 | [diff] [blame] | 5306 | static int wake_affine(struct sched_domain *sd, struct task_struct *p, |
| 5307 | int prev_cpu, int sync) |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 5308 | { |
Paul Turner | e37b6a7 | 2011-01-21 20:44:59 -0800 | [diff] [blame] | 5309 | s64 this_load, load; |
Vincent Guittot | bd61c98 | 2014-08-26 13:06:50 +0200 | [diff] [blame] | 5310 | s64 this_eff_load, prev_eff_load; |
Morten Rasmussen | 772bd008c | 2016-06-22 18:03:13 +0100 | [diff] [blame] | 5311 | int idx, this_cpu; |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 5312 | struct task_group *tg; |
Peter Zijlstra | 8337826 | 2008-06-27 13:41:37 +0200 | [diff] [blame] | 5313 | unsigned long weight; |
Mike Galbraith | b3137bc | 2008-05-29 11:11:41 +0200 | [diff] [blame] | 5314 | int balanced; |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 5315 | |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 5316 | idx = sd->wake_idx; |
| 5317 | this_cpu = smp_processor_id(); |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 5318 | load = source_load(prev_cpu, idx); |
| 5319 | this_load = target_load(this_cpu, idx); |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 5320 | |
| 5321 | /* |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 5322 | * If sync wakeup then subtract the (maximum possible) |
| 5323 | * effect of the currently running task from the load |
| 5324 | * of the current CPU: |
| 5325 | */ |
Peter Zijlstra | 8337826 | 2008-06-27 13:41:37 +0200 | [diff] [blame] | 5326 | if (sync) { |
| 5327 | tg = task_group(current); |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 5328 | weight = current->se.avg.load_avg; |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 5329 | |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 5330 | this_load += effective_load(tg, this_cpu, -weight, -weight); |
Peter Zijlstra | 8337826 | 2008-06-27 13:41:37 +0200 | [diff] [blame] | 5331 | load += effective_load(tg, prev_cpu, 0, -weight); |
| 5332 | } |
| 5333 | |
| 5334 | tg = task_group(p); |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 5335 | weight = p->se.avg.load_avg; |
Peter Zijlstra | 8337826 | 2008-06-27 13:41:37 +0200 | [diff] [blame] | 5336 | |
Peter Zijlstra | 71a29aa | 2009-09-07 18:28:05 +0200 | [diff] [blame] | 5337 | /* |
| 5338 | * In low-load situations, where prev_cpu is idle and this_cpu is idle |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 5339 | * due to the sync cause above having dropped this_load to 0, we'll |
| 5340 | * always have an imbalance, but there's really nothing you can do |
| 5341 | * about that, so that's good too. |
Peter Zijlstra | 71a29aa | 2009-09-07 18:28:05 +0200 | [diff] [blame] | 5342 | * |
| 5343 | * Otherwise check if either cpus are near enough in load to allow this |
| 5344 | * task to be woken on this_cpu. |
| 5345 | */ |
Vincent Guittot | bd61c98 | 2014-08-26 13:06:50 +0200 | [diff] [blame] | 5346 | this_eff_load = 100; |
| 5347 | this_eff_load *= capacity_of(prev_cpu); |
Peter Zijlstra | e51fd5e | 2010-05-31 12:37:30 +0200 | [diff] [blame] | 5348 | |
Vincent Guittot | bd61c98 | 2014-08-26 13:06:50 +0200 | [diff] [blame] | 5349 | prev_eff_load = 100 + (sd->imbalance_pct - 100) / 2; |
| 5350 | prev_eff_load *= capacity_of(this_cpu); |
| 5351 | |
| 5352 | if (this_load > 0) { |
Peter Zijlstra | e51fd5e | 2010-05-31 12:37:30 +0200 | [diff] [blame] | 5353 | this_eff_load *= this_load + |
| 5354 | effective_load(tg, this_cpu, weight, weight); |
| 5355 | |
Peter Zijlstra | e51fd5e | 2010-05-31 12:37:30 +0200 | [diff] [blame] | 5356 | prev_eff_load *= load + effective_load(tg, prev_cpu, 0, weight); |
Vincent Guittot | bd61c98 | 2014-08-26 13:06:50 +0200 | [diff] [blame] | 5357 | } |
Peter Zijlstra | e51fd5e | 2010-05-31 12:37:30 +0200 | [diff] [blame] | 5358 | |
Vincent Guittot | bd61c98 | 2014-08-26 13:06:50 +0200 | [diff] [blame] | 5359 | balanced = this_eff_load <= prev_eff_load; |
Mike Galbraith | b3137bc | 2008-05-29 11:11:41 +0200 | [diff] [blame] | 5360 | |
Josh Poimboeuf | ae92882 | 2016-06-17 12:43:24 -0500 | [diff] [blame] | 5361 | schedstat_inc(p->se.statistics.nr_wakeups_affine_attempts); |
Mike Galbraith | b3137bc | 2008-05-29 11:11:41 +0200 | [diff] [blame] | 5362 | |
Vincent Guittot | 05bfb65 | 2014-08-26 13:06:45 +0200 | [diff] [blame] | 5363 | if (!balanced) |
| 5364 | return 0; |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 5365 | |
Josh Poimboeuf | ae92882 | 2016-06-17 12:43:24 -0500 | [diff] [blame] | 5366 | schedstat_inc(sd->ttwu_move_affine); |
| 5367 | schedstat_inc(p->se.statistics.nr_wakeups_affine); |
Vincent Guittot | 05bfb65 | 2014-08-26 13:06:45 +0200 | [diff] [blame] | 5368 | |
| 5369 | return 1; |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 5370 | } |
| 5371 | |
Morten Rasmussen | 6a0b19c | 2016-10-14 14:41:08 +0100 | [diff] [blame] | 5372 | static inline int task_util(struct task_struct *p); |
| 5373 | static int cpu_util_wake(int cpu, struct task_struct *p); |
| 5374 | |
| 5375 | static unsigned long capacity_spare_wake(int cpu, struct task_struct *p) |
| 5376 | { |
| 5377 | return capacity_orig_of(cpu) - cpu_util_wake(cpu, p); |
| 5378 | } |
| 5379 | |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 5380 | /* |
| 5381 | * find_idlest_group finds and returns the least busy CPU group within the |
| 5382 | * domain. |
| 5383 | */ |
| 5384 | static struct sched_group * |
Peter Zijlstra | 78e7ed5 | 2009-09-03 13:16:51 +0200 | [diff] [blame] | 5385 | find_idlest_group(struct sched_domain *sd, struct task_struct *p, |
Vincent Guittot | c44f2a0 | 2013-10-18 13:52:21 +0200 | [diff] [blame] | 5386 | int this_cpu, int sd_flag) |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 5387 | { |
Andi Kleen | b3bd3de | 2010-08-10 14:17:51 -0700 | [diff] [blame] | 5388 | struct sched_group *idlest = NULL, *group = sd->groups; |
Morten Rasmussen | 6a0b19c | 2016-10-14 14:41:08 +0100 | [diff] [blame] | 5389 | struct sched_group *most_spare_sg = NULL; |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 5390 | unsigned long min_load = ULONG_MAX, this_load = 0; |
Morten Rasmussen | 6a0b19c | 2016-10-14 14:41:08 +0100 | [diff] [blame] | 5391 | unsigned long most_spare = 0, this_spare = 0; |
Vincent Guittot | c44f2a0 | 2013-10-18 13:52:21 +0200 | [diff] [blame] | 5392 | int load_idx = sd->forkexec_idx; |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 5393 | int imbalance = 100 + (sd->imbalance_pct-100)/2; |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 5394 | |
Vincent Guittot | c44f2a0 | 2013-10-18 13:52:21 +0200 | [diff] [blame] | 5395 | if (sd_flag & SD_BALANCE_WAKE) |
| 5396 | load_idx = sd->wake_idx; |
| 5397 | |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 5398 | do { |
Morten Rasmussen | 6a0b19c | 2016-10-14 14:41:08 +0100 | [diff] [blame] | 5399 | unsigned long load, avg_load, spare_cap, max_spare_cap; |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 5400 | int local_group; |
| 5401 | int i; |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 5402 | |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 5403 | /* Skip over this group if it has no CPUs allowed */ |
| 5404 | if (!cpumask_intersects(sched_group_cpus(group), |
Peter Zijlstra | fa17b50 | 2011-06-16 12:23:22 +0200 | [diff] [blame] | 5405 | tsk_cpus_allowed(p))) |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 5406 | continue; |
| 5407 | |
| 5408 | local_group = cpumask_test_cpu(this_cpu, |
| 5409 | sched_group_cpus(group)); |
| 5410 | |
Morten Rasmussen | 6a0b19c | 2016-10-14 14:41:08 +0100 | [diff] [blame] | 5411 | /* |
| 5412 | * Tally up the load of all CPUs in the group and find |
| 5413 | * the group containing the CPU with most spare capacity. |
| 5414 | */ |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 5415 | avg_load = 0; |
Morten Rasmussen | 6a0b19c | 2016-10-14 14:41:08 +0100 | [diff] [blame] | 5416 | max_spare_cap = 0; |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 5417 | |
| 5418 | for_each_cpu(i, sched_group_cpus(group)) { |
| 5419 | /* Bias balancing toward cpus of our domain */ |
| 5420 | if (local_group) |
| 5421 | load = source_load(i, load_idx); |
| 5422 | else |
| 5423 | load = target_load(i, load_idx); |
| 5424 | |
| 5425 | avg_load += load; |
Morten Rasmussen | 6a0b19c | 2016-10-14 14:41:08 +0100 | [diff] [blame] | 5426 | |
| 5427 | spare_cap = capacity_spare_wake(i, p); |
| 5428 | |
| 5429 | if (spare_cap > max_spare_cap) |
| 5430 | max_spare_cap = spare_cap; |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 5431 | } |
| 5432 | |
Nicolas Pitre | 63b2ca3 | 2014-05-26 18:19:37 -0400 | [diff] [blame] | 5433 | /* Adjust by relative CPU capacity of the group */ |
Nicolas Pitre | ca8ce3d | 2014-05-26 18:19:39 -0400 | [diff] [blame] | 5434 | avg_load = (avg_load * SCHED_CAPACITY_SCALE) / group->sgc->capacity; |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 5435 | |
| 5436 | if (local_group) { |
| 5437 | this_load = avg_load; |
Morten Rasmussen | 6a0b19c | 2016-10-14 14:41:08 +0100 | [diff] [blame] | 5438 | this_spare = max_spare_cap; |
| 5439 | } else { |
| 5440 | if (avg_load < min_load) { |
| 5441 | min_load = avg_load; |
| 5442 | idlest = group; |
| 5443 | } |
| 5444 | |
| 5445 | if (most_spare < max_spare_cap) { |
| 5446 | most_spare = max_spare_cap; |
| 5447 | most_spare_sg = group; |
| 5448 | } |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 5449 | } |
| 5450 | } while (group = group->next, group != sd->groups); |
| 5451 | |
Morten Rasmussen | 6a0b19c | 2016-10-14 14:41:08 +0100 | [diff] [blame] | 5452 | /* |
| 5453 | * The cross-over point between using spare capacity or least load |
| 5454 | * is too conservative for high utilization tasks on partially |
| 5455 | * utilized systems if we require spare_capacity > task_util(p), |
| 5456 | * so we allow for some task stuffing by using |
| 5457 | * spare_capacity > task_util(p)/2. |
| 5458 | */ |
| 5459 | if (this_spare > task_util(p) / 2 && |
| 5460 | imbalance*this_spare > 100*most_spare) |
| 5461 | return NULL; |
| 5462 | else if (most_spare > task_util(p) / 2) |
| 5463 | return most_spare_sg; |
| 5464 | |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 5465 | if (!idlest || 100*this_load < imbalance*min_load) |
| 5466 | return NULL; |
| 5467 | return idlest; |
| 5468 | } |
| 5469 | |
| 5470 | /* |
| 5471 | * find_idlest_cpu - find the idlest cpu among the cpus in group. |
| 5472 | */ |
| 5473 | static int |
| 5474 | find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu) |
| 5475 | { |
| 5476 | unsigned long load, min_load = ULONG_MAX; |
Nicolas Pitre | 83a0a96 | 2014-09-04 11:32:10 -0400 | [diff] [blame] | 5477 | unsigned int min_exit_latency = UINT_MAX; |
| 5478 | u64 latest_idle_timestamp = 0; |
| 5479 | int least_loaded_cpu = this_cpu; |
| 5480 | int shallowest_idle_cpu = -1; |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 5481 | int i; |
| 5482 | |
Morten Rasmussen | eaecf41 | 2016-06-22 18:03:14 +0100 | [diff] [blame] | 5483 | /* Check if we have any choice: */ |
| 5484 | if (group->group_weight == 1) |
| 5485 | return cpumask_first(sched_group_cpus(group)); |
| 5486 | |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 5487 | /* Traverse only the allowed CPUs */ |
Peter Zijlstra | fa17b50 | 2011-06-16 12:23:22 +0200 | [diff] [blame] | 5488 | for_each_cpu_and(i, sched_group_cpus(group), tsk_cpus_allowed(p)) { |
Nicolas Pitre | 83a0a96 | 2014-09-04 11:32:10 -0400 | [diff] [blame] | 5489 | if (idle_cpu(i)) { |
| 5490 | struct rq *rq = cpu_rq(i); |
| 5491 | struct cpuidle_state *idle = idle_get_state(rq); |
| 5492 | if (idle && idle->exit_latency < min_exit_latency) { |
| 5493 | /* |
| 5494 | * We give priority to a CPU whose idle state |
| 5495 | * has the smallest exit latency irrespective |
| 5496 | * of any idle timestamp. |
| 5497 | */ |
| 5498 | min_exit_latency = idle->exit_latency; |
| 5499 | latest_idle_timestamp = rq->idle_stamp; |
| 5500 | shallowest_idle_cpu = i; |
| 5501 | } else if ((!idle || idle->exit_latency == min_exit_latency) && |
| 5502 | rq->idle_stamp > latest_idle_timestamp) { |
| 5503 | /* |
| 5504 | * If equal or no active idle state, then |
| 5505 | * the most recently idled CPU might have |
| 5506 | * a warmer cache. |
| 5507 | */ |
| 5508 | latest_idle_timestamp = rq->idle_stamp; |
| 5509 | shallowest_idle_cpu = i; |
| 5510 | } |
Yao Dongdong | 9f96742 | 2014-10-28 04:08:06 +0000 | [diff] [blame] | 5511 | } else if (shallowest_idle_cpu == -1) { |
Nicolas Pitre | 83a0a96 | 2014-09-04 11:32:10 -0400 | [diff] [blame] | 5512 | load = weighted_cpuload(i); |
| 5513 | if (load < min_load || (load == min_load && i == this_cpu)) { |
| 5514 | min_load = load; |
| 5515 | least_loaded_cpu = i; |
| 5516 | } |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 5517 | } |
| 5518 | } |
| 5519 | |
Nicolas Pitre | 83a0a96 | 2014-09-04 11:32:10 -0400 | [diff] [blame] | 5520 | return shallowest_idle_cpu != -1 ? shallowest_idle_cpu : least_loaded_cpu; |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 5521 | } |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 5522 | |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 5523 | /* |
Peter Zijlstra | 10e2f1a | 2016-05-09 10:38:05 +0200 | [diff] [blame] | 5524 | * Implement a for_each_cpu() variant that starts the scan at a given cpu |
| 5525 | * (@start), and wraps around. |
| 5526 | * |
| 5527 | * This is used to scan for idle CPUs; such that not all CPUs looking for an |
| 5528 | * idle CPU find the same CPU. The down-side is that tasks tend to cycle |
| 5529 | * through the LLC domain. |
| 5530 | * |
| 5531 | * Especially tbench is found sensitive to this. |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 5532 | */ |
Peter Zijlstra | 10e2f1a | 2016-05-09 10:38:05 +0200 | [diff] [blame] | 5533 | |
| 5534 | static int cpumask_next_wrap(int n, const struct cpumask *mask, int start, int *wrapped) |
| 5535 | { |
| 5536 | int next; |
| 5537 | |
| 5538 | again: |
| 5539 | next = find_next_bit(cpumask_bits(mask), nr_cpumask_bits, n+1); |
| 5540 | |
| 5541 | if (*wrapped) { |
| 5542 | if (next >= start) |
| 5543 | return nr_cpumask_bits; |
| 5544 | } else { |
| 5545 | if (next >= nr_cpumask_bits) { |
| 5546 | *wrapped = 1; |
| 5547 | n = -1; |
| 5548 | goto again; |
| 5549 | } |
| 5550 | } |
| 5551 | |
| 5552 | return next; |
| 5553 | } |
| 5554 | |
| 5555 | #define for_each_cpu_wrap(cpu, mask, start, wrap) \ |
| 5556 | for ((wrap) = 0, (cpu) = (start)-1; \ |
| 5557 | (cpu) = cpumask_next_wrap((cpu), (mask), (start), &(wrap)), \ |
| 5558 | (cpu) < nr_cpumask_bits; ) |
| 5559 | |
| 5560 | #ifdef CONFIG_SCHED_SMT |
| 5561 | |
| 5562 | static inline void set_idle_cores(int cpu, int val) |
| 5563 | { |
| 5564 | struct sched_domain_shared *sds; |
| 5565 | |
| 5566 | sds = rcu_dereference(per_cpu(sd_llc_shared, cpu)); |
| 5567 | if (sds) |
| 5568 | WRITE_ONCE(sds->has_idle_cores, val); |
| 5569 | } |
| 5570 | |
| 5571 | static inline bool test_idle_cores(int cpu, bool def) |
| 5572 | { |
| 5573 | struct sched_domain_shared *sds; |
| 5574 | |
| 5575 | sds = rcu_dereference(per_cpu(sd_llc_shared, cpu)); |
| 5576 | if (sds) |
| 5577 | return READ_ONCE(sds->has_idle_cores); |
| 5578 | |
| 5579 | return def; |
| 5580 | } |
| 5581 | |
| 5582 | /* |
| 5583 | * Scans the local SMT mask to see if the entire core is idle, and records this |
| 5584 | * information in sd_llc_shared->has_idle_cores. |
| 5585 | * |
| 5586 | * Since SMT siblings share all cache levels, inspecting this limited remote |
| 5587 | * state should be fairly cheap. |
| 5588 | */ |
Peter Zijlstra | 1b568f0 | 2016-05-09 10:38:41 +0200 | [diff] [blame] | 5589 | void __update_idle_core(struct rq *rq) |
Peter Zijlstra | 10e2f1a | 2016-05-09 10:38:05 +0200 | [diff] [blame] | 5590 | { |
| 5591 | int core = cpu_of(rq); |
| 5592 | int cpu; |
| 5593 | |
| 5594 | rcu_read_lock(); |
| 5595 | if (test_idle_cores(core, true)) |
| 5596 | goto unlock; |
| 5597 | |
| 5598 | for_each_cpu(cpu, cpu_smt_mask(core)) { |
| 5599 | if (cpu == core) |
| 5600 | continue; |
| 5601 | |
| 5602 | if (!idle_cpu(cpu)) |
| 5603 | goto unlock; |
| 5604 | } |
| 5605 | |
| 5606 | set_idle_cores(core, 1); |
| 5607 | unlock: |
| 5608 | rcu_read_unlock(); |
| 5609 | } |
| 5610 | |
| 5611 | /* |
| 5612 | * Scan the entire LLC domain for idle cores; this dynamically switches off if |
| 5613 | * there are no idle cores left in the system; tracked through |
| 5614 | * sd_llc->shared->has_idle_cores and enabled through update_idle_core() above. |
| 5615 | */ |
| 5616 | static int select_idle_core(struct task_struct *p, struct sched_domain *sd, int target) |
| 5617 | { |
| 5618 | struct cpumask *cpus = this_cpu_cpumask_var_ptr(select_idle_mask); |
| 5619 | int core, cpu, wrap; |
| 5620 | |
Peter Zijlstra | 1b568f0 | 2016-05-09 10:38:41 +0200 | [diff] [blame] | 5621 | if (!static_branch_likely(&sched_smt_present)) |
| 5622 | return -1; |
| 5623 | |
Peter Zijlstra | 10e2f1a | 2016-05-09 10:38:05 +0200 | [diff] [blame] | 5624 | if (!test_idle_cores(target, false)) |
| 5625 | return -1; |
| 5626 | |
| 5627 | cpumask_and(cpus, sched_domain_span(sd), tsk_cpus_allowed(p)); |
| 5628 | |
| 5629 | for_each_cpu_wrap(core, cpus, target, wrap) { |
| 5630 | bool idle = true; |
| 5631 | |
| 5632 | for_each_cpu(cpu, cpu_smt_mask(core)) { |
| 5633 | cpumask_clear_cpu(cpu, cpus); |
| 5634 | if (!idle_cpu(cpu)) |
| 5635 | idle = false; |
| 5636 | } |
| 5637 | |
| 5638 | if (idle) |
| 5639 | return core; |
| 5640 | } |
| 5641 | |
| 5642 | /* |
| 5643 | * Failed to find an idle core; stop looking for one. |
| 5644 | */ |
| 5645 | set_idle_cores(target, 0); |
| 5646 | |
| 5647 | return -1; |
| 5648 | } |
| 5649 | |
| 5650 | /* |
| 5651 | * Scan the local SMT mask for idle CPUs. |
| 5652 | */ |
| 5653 | static int select_idle_smt(struct task_struct *p, struct sched_domain *sd, int target) |
| 5654 | { |
| 5655 | int cpu; |
| 5656 | |
Peter Zijlstra | 1b568f0 | 2016-05-09 10:38:41 +0200 | [diff] [blame] | 5657 | if (!static_branch_likely(&sched_smt_present)) |
| 5658 | return -1; |
| 5659 | |
Peter Zijlstra | 10e2f1a | 2016-05-09 10:38:05 +0200 | [diff] [blame] | 5660 | for_each_cpu(cpu, cpu_smt_mask(target)) { |
| 5661 | if (!cpumask_test_cpu(cpu, tsk_cpus_allowed(p))) |
| 5662 | continue; |
| 5663 | if (idle_cpu(cpu)) |
| 5664 | return cpu; |
| 5665 | } |
| 5666 | |
| 5667 | return -1; |
| 5668 | } |
| 5669 | |
| 5670 | #else /* CONFIG_SCHED_SMT */ |
| 5671 | |
| 5672 | static inline int select_idle_core(struct task_struct *p, struct sched_domain *sd, int target) |
| 5673 | { |
| 5674 | return -1; |
| 5675 | } |
| 5676 | |
| 5677 | static inline int select_idle_smt(struct task_struct *p, struct sched_domain *sd, int target) |
| 5678 | { |
| 5679 | return -1; |
| 5680 | } |
| 5681 | |
| 5682 | #endif /* CONFIG_SCHED_SMT */ |
| 5683 | |
| 5684 | /* |
| 5685 | * Scan the LLC domain for idle CPUs; this is dynamically regulated by |
| 5686 | * comparing the average scan cost (tracked in sd->avg_scan_cost) against the |
| 5687 | * average idle time for this rq (as found in rq->avg_idle). |
| 5688 | */ |
| 5689 | static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, int target) |
| 5690 | { |
Wanpeng Li | 9cfb38a | 2016-10-09 08:04:03 +0800 | [diff] [blame] | 5691 | struct sched_domain *this_sd; |
| 5692 | u64 avg_cost, avg_idle = this_rq()->avg_idle; |
Peter Zijlstra | 10e2f1a | 2016-05-09 10:38:05 +0200 | [diff] [blame] | 5693 | u64 time, cost; |
| 5694 | s64 delta; |
| 5695 | int cpu, wrap; |
| 5696 | |
Wanpeng Li | 9cfb38a | 2016-10-09 08:04:03 +0800 | [diff] [blame] | 5697 | this_sd = rcu_dereference(*this_cpu_ptr(&sd_llc)); |
| 5698 | if (!this_sd) |
| 5699 | return -1; |
| 5700 | |
| 5701 | avg_cost = this_sd->avg_scan_cost; |
| 5702 | |
Peter Zijlstra | 10e2f1a | 2016-05-09 10:38:05 +0200 | [diff] [blame] | 5703 | /* |
| 5704 | * Due to large variance we need a large fuzz factor; hackbench in |
| 5705 | * particularly is sensitive here. |
| 5706 | */ |
| 5707 | if ((avg_idle / 512) < avg_cost) |
| 5708 | return -1; |
| 5709 | |
| 5710 | time = local_clock(); |
| 5711 | |
| 5712 | for_each_cpu_wrap(cpu, sched_domain_span(sd), target, wrap) { |
| 5713 | if (!cpumask_test_cpu(cpu, tsk_cpus_allowed(p))) |
| 5714 | continue; |
| 5715 | if (idle_cpu(cpu)) |
| 5716 | break; |
| 5717 | } |
| 5718 | |
| 5719 | time = local_clock() - time; |
| 5720 | cost = this_sd->avg_scan_cost; |
| 5721 | delta = (s64)(time - cost) / 8; |
| 5722 | this_sd->avg_scan_cost += delta; |
| 5723 | |
| 5724 | return cpu; |
| 5725 | } |
| 5726 | |
| 5727 | /* |
| 5728 | * Try and locate an idle core/thread in the LLC cache domain. |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 5729 | */ |
Morten Rasmussen | 772bd008c | 2016-06-22 18:03:13 +0100 | [diff] [blame] | 5730 | static int select_idle_sibling(struct task_struct *p, int prev, int target) |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 5731 | { |
Suresh Siddha | 99bd5e2 | 2010-03-31 16:47:45 -0700 | [diff] [blame] | 5732 | struct sched_domain *sd; |
Peter Zijlstra | 10e2f1a | 2016-05-09 10:38:05 +0200 | [diff] [blame] | 5733 | int i; |
Mike Galbraith | e0a79f5 | 2013-01-28 12:19:25 +0100 | [diff] [blame] | 5734 | |
| 5735 | if (idle_cpu(target)) |
| 5736 | return target; |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 5737 | |
| 5738 | /* |
Peter Zijlstra | 10e2f1a | 2016-05-09 10:38:05 +0200 | [diff] [blame] | 5739 | * If the previous cpu is cache affine and idle, don't be stupid. |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 5740 | */ |
Morten Rasmussen | 772bd008c | 2016-06-22 18:03:13 +0100 | [diff] [blame] | 5741 | if (prev != target && cpus_share_cache(prev, target) && idle_cpu(prev)) |
| 5742 | return prev; |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 5743 | |
Peter Zijlstra | 518cd62 | 2011-12-07 15:07:31 +0100 | [diff] [blame] | 5744 | sd = rcu_dereference(per_cpu(sd_llc, target)); |
Peter Zijlstra | 10e2f1a | 2016-05-09 10:38:05 +0200 | [diff] [blame] | 5745 | if (!sd) |
| 5746 | return target; |
Morten Rasmussen | 772bd008c | 2016-06-22 18:03:13 +0100 | [diff] [blame] | 5747 | |
Peter Zijlstra | 10e2f1a | 2016-05-09 10:38:05 +0200 | [diff] [blame] | 5748 | i = select_idle_core(p, sd, target); |
| 5749 | if ((unsigned)i < nr_cpumask_bits) |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 5750 | return i; |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 5751 | |
Peter Zijlstra | 10e2f1a | 2016-05-09 10:38:05 +0200 | [diff] [blame] | 5752 | i = select_idle_cpu(p, sd, target); |
| 5753 | if ((unsigned)i < nr_cpumask_bits) |
| 5754 | return i; |
Mike Galbraith | 970e178 | 2012-06-12 05:18:32 +0200 | [diff] [blame] | 5755 | |
Peter Zijlstra | 10e2f1a | 2016-05-09 10:38:05 +0200 | [diff] [blame] | 5756 | i = select_idle_smt(p, sd, target); |
| 5757 | if ((unsigned)i < nr_cpumask_bits) |
| 5758 | return i; |
Linus Torvalds | 37407ea | 2012-09-16 12:29:43 -0700 | [diff] [blame] | 5759 | |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 5760 | return target; |
| 5761 | } |
Dietmar Eggemann | 231678b | 2015-08-14 17:23:13 +0100 | [diff] [blame] | 5762 | |
Vincent Guittot | 8bb5b00 | 2015-03-04 08:48:47 +0100 | [diff] [blame] | 5763 | /* |
Dietmar Eggemann | 9e91d61 | 2015-08-14 17:23:12 +0100 | [diff] [blame] | 5764 | * cpu_util returns the amount of capacity of a CPU that is used by CFS |
Vincent Guittot | 8bb5b00 | 2015-03-04 08:48:47 +0100 | [diff] [blame] | 5765 | * tasks. The unit of the return value must be the one of capacity so we can |
Dietmar Eggemann | 9e91d61 | 2015-08-14 17:23:12 +0100 | [diff] [blame] | 5766 | * compare the utilization with the capacity of the CPU that is available for |
| 5767 | * CFS task (ie cpu_capacity). |
Dietmar Eggemann | 231678b | 2015-08-14 17:23:13 +0100 | [diff] [blame] | 5768 | * |
| 5769 | * cfs_rq.avg.util_avg is the sum of running time of runnable tasks plus the |
| 5770 | * recent utilization of currently non-runnable tasks on a CPU. It represents |
| 5771 | * the amount of utilization of a CPU in the range [0..capacity_orig] where |
| 5772 | * capacity_orig is the cpu_capacity available at the highest frequency |
| 5773 | * (arch_scale_freq_capacity()). |
| 5774 | * The utilization of a CPU converges towards a sum equal to or less than the |
| 5775 | * current capacity (capacity_curr <= capacity_orig) of the CPU because it is |
| 5776 | * the running time on this CPU scaled by capacity_curr. |
| 5777 | * |
| 5778 | * Nevertheless, cfs_rq.avg.util_avg can be higher than capacity_curr or even |
| 5779 | * higher than capacity_orig because of unfortunate rounding in |
| 5780 | * cfs.avg.util_avg or just after migrating tasks and new task wakeups until |
| 5781 | * the average stabilizes with the new running time. We need to check that the |
| 5782 | * utilization stays within the range of [0..capacity_orig] and cap it if |
| 5783 | * necessary. Without utilization capping, a group could be seen as overloaded |
| 5784 | * (CPU0 utilization at 121% + CPU1 utilization at 80%) whereas CPU1 has 20% of |
| 5785 | * available capacity. We allow utilization to overshoot capacity_curr (but not |
| 5786 | * capacity_orig) as it useful for predicting the capacity required after task |
| 5787 | * migrations (scheduler-driven DVFS). |
Vincent Guittot | 8bb5b00 | 2015-03-04 08:48:47 +0100 | [diff] [blame] | 5788 | */ |
Dietmar Eggemann | 9e91d61 | 2015-08-14 17:23:12 +0100 | [diff] [blame] | 5789 | static int cpu_util(int cpu) |
Vincent Guittot | 8bb5b00 | 2015-03-04 08:48:47 +0100 | [diff] [blame] | 5790 | { |
Dietmar Eggemann | 9e91d61 | 2015-08-14 17:23:12 +0100 | [diff] [blame] | 5791 | unsigned long util = cpu_rq(cpu)->cfs.avg.util_avg; |
Vincent Guittot | 8bb5b00 | 2015-03-04 08:48:47 +0100 | [diff] [blame] | 5792 | unsigned long capacity = capacity_orig_of(cpu); |
| 5793 | |
Dietmar Eggemann | 231678b | 2015-08-14 17:23:13 +0100 | [diff] [blame] | 5794 | return (util >= capacity) ? capacity : util; |
Vincent Guittot | 8bb5b00 | 2015-03-04 08:48:47 +0100 | [diff] [blame] | 5795 | } |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 5796 | |
Morten Rasmussen | 3273163 | 2016-07-25 14:34:26 +0100 | [diff] [blame] | 5797 | static inline int task_util(struct task_struct *p) |
| 5798 | { |
| 5799 | return p->se.avg.util_avg; |
| 5800 | } |
| 5801 | |
| 5802 | /* |
Morten Rasmussen | 104cb16 | 2016-10-14 14:41:07 +0100 | [diff] [blame] | 5803 | * cpu_util_wake: Compute cpu utilization with any contributions from |
| 5804 | * the waking task p removed. |
| 5805 | */ |
| 5806 | static int cpu_util_wake(int cpu, struct task_struct *p) |
| 5807 | { |
| 5808 | unsigned long util, capacity; |
| 5809 | |
| 5810 | /* Task has no contribution or is new */ |
| 5811 | if (cpu != task_cpu(p) || !p->se.avg.last_update_time) |
| 5812 | return cpu_util(cpu); |
| 5813 | |
| 5814 | capacity = capacity_orig_of(cpu); |
| 5815 | util = max_t(long, cpu_rq(cpu)->cfs.avg.util_avg - task_util(p), 0); |
| 5816 | |
| 5817 | return (util >= capacity) ? capacity : util; |
| 5818 | } |
| 5819 | |
| 5820 | /* |
Morten Rasmussen | 3273163 | 2016-07-25 14:34:26 +0100 | [diff] [blame] | 5821 | * Disable WAKE_AFFINE in the case where task @p doesn't fit in the |
| 5822 | * capacity of either the waking CPU @cpu or the previous CPU @prev_cpu. |
| 5823 | * |
| 5824 | * In that case WAKE_AFFINE doesn't make sense and we'll let |
| 5825 | * BALANCE_WAKE sort things out. |
| 5826 | */ |
| 5827 | static int wake_cap(struct task_struct *p, int cpu, int prev_cpu) |
| 5828 | { |
| 5829 | long min_cap, max_cap; |
| 5830 | |
| 5831 | min_cap = min(capacity_orig_of(prev_cpu), capacity_orig_of(cpu)); |
| 5832 | max_cap = cpu_rq(cpu)->rd->max_cpu_capacity; |
| 5833 | |
| 5834 | /* Minimum capacity is close to max, no need to abort wake_affine */ |
| 5835 | if (max_cap - min_cap < max_cap >> 3) |
| 5836 | return 0; |
| 5837 | |
Morten Rasmussen | 104cb16 | 2016-10-14 14:41:07 +0100 | [diff] [blame] | 5838 | /* Bring task utilization in sync with prev_cpu */ |
| 5839 | sync_entity_load_avg(&p->se); |
| 5840 | |
Morten Rasmussen | 3273163 | 2016-07-25 14:34:26 +0100 | [diff] [blame] | 5841 | return min_cap * 1024 < task_util(p) * capacity_margin; |
| 5842 | } |
| 5843 | |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 5844 | /* |
Morten Rasmussen | de91b9c | 2014-02-18 14:14:24 +0000 | [diff] [blame] | 5845 | * select_task_rq_fair: Select target runqueue for the waking task in domains |
| 5846 | * that have the 'sd_flag' flag set. In practice, this is SD_BALANCE_WAKE, |
| 5847 | * SD_BALANCE_FORK, or SD_BALANCE_EXEC. |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 5848 | * |
Morten Rasmussen | de91b9c | 2014-02-18 14:14:24 +0000 | [diff] [blame] | 5849 | * Balances load by selecting the idlest cpu in the idlest group, or under |
| 5850 | * certain conditions an idle sibling cpu if the domain has SD_WAKE_AFFINE set. |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 5851 | * |
Morten Rasmussen | de91b9c | 2014-02-18 14:14:24 +0000 | [diff] [blame] | 5852 | * Returns the target cpu number. |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 5853 | * |
| 5854 | * preempt must be disabled. |
| 5855 | */ |
Peter Zijlstra | 0017d73 | 2010-03-24 18:34:10 +0100 | [diff] [blame] | 5856 | static int |
Peter Zijlstra | ac66f54 | 2013-10-07 11:29:16 +0100 | [diff] [blame] | 5857 | select_task_rq_fair(struct task_struct *p, int prev_cpu, int sd_flag, int wake_flags) |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 5858 | { |
Peter Zijlstra | 29cd8ba | 2009-09-17 09:01:14 +0200 | [diff] [blame] | 5859 | struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL; |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 5860 | int cpu = smp_processor_id(); |
Mike Galbraith | 63b0e9e | 2015-07-14 17:39:50 +0200 | [diff] [blame] | 5861 | int new_cpu = prev_cpu; |
Suresh Siddha | 99bd5e2 | 2010-03-31 16:47:45 -0700 | [diff] [blame] | 5862 | int want_affine = 0; |
Peter Zijlstra | 5158f4e | 2009-09-16 13:46:59 +0200 | [diff] [blame] | 5863 | int sync = wake_flags & WF_SYNC; |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 5864 | |
Peter Zijlstra | c58d25f | 2016-05-12 09:19:59 +0200 | [diff] [blame] | 5865 | if (sd_flag & SD_BALANCE_WAKE) { |
| 5866 | record_wakee(p); |
Morten Rasmussen | 3273163 | 2016-07-25 14:34:26 +0100 | [diff] [blame] | 5867 | want_affine = !wake_wide(p) && !wake_cap(p, cpu, prev_cpu) |
| 5868 | && cpumask_test_cpu(cpu, tsk_cpus_allowed(p)); |
Peter Zijlstra | c58d25f | 2016-05-12 09:19:59 +0200 | [diff] [blame] | 5869 | } |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 5870 | |
Peter Zijlstra | dce840a | 2011-04-07 14:09:50 +0200 | [diff] [blame] | 5871 | rcu_read_lock(); |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 5872 | for_each_domain(cpu, tmp) { |
Peter Zijlstra | e4f4288 | 2009-12-16 18:04:34 +0100 | [diff] [blame] | 5873 | if (!(tmp->flags & SD_LOAD_BALANCE)) |
Mike Galbraith | 63b0e9e | 2015-07-14 17:39:50 +0200 | [diff] [blame] | 5874 | break; |
Peter Zijlstra | e4f4288 | 2009-12-16 18:04:34 +0100 | [diff] [blame] | 5875 | |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 5876 | /* |
Suresh Siddha | 99bd5e2 | 2010-03-31 16:47:45 -0700 | [diff] [blame] | 5877 | * If both cpu and prev_cpu are part of this domain, |
| 5878 | * cpu is a valid SD_WAKE_AFFINE target. |
Peter Zijlstra | fe3bcfe | 2009-11-12 15:55:29 +0100 | [diff] [blame] | 5879 | */ |
Suresh Siddha | 99bd5e2 | 2010-03-31 16:47:45 -0700 | [diff] [blame] | 5880 | if (want_affine && (tmp->flags & SD_WAKE_AFFINE) && |
| 5881 | cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) { |
| 5882 | affine_sd = tmp; |
Alex Shi | f03542a | 2012-07-26 08:55:34 +0800 | [diff] [blame] | 5883 | break; |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 5884 | } |
| 5885 | |
Alex Shi | f03542a | 2012-07-26 08:55:34 +0800 | [diff] [blame] | 5886 | if (tmp->flags & sd_flag) |
Peter Zijlstra | 29cd8ba | 2009-09-17 09:01:14 +0200 | [diff] [blame] | 5887 | sd = tmp; |
Mike Galbraith | 63b0e9e | 2015-07-14 17:39:50 +0200 | [diff] [blame] | 5888 | else if (!want_affine) |
| 5889 | break; |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 5890 | } |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 5891 | |
Mike Galbraith | 63b0e9e | 2015-07-14 17:39:50 +0200 | [diff] [blame] | 5892 | if (affine_sd) { |
| 5893 | sd = NULL; /* Prefer wake_affine over balance flags */ |
Morten Rasmussen | 772bd008c | 2016-06-22 18:03:13 +0100 | [diff] [blame] | 5894 | if (cpu != prev_cpu && wake_affine(affine_sd, p, prev_cpu, sync)) |
Mike Galbraith | 63b0e9e | 2015-07-14 17:39:50 +0200 | [diff] [blame] | 5895 | new_cpu = cpu; |
Mike Galbraith | 8b911ac | 2010-03-11 17:17:16 +0100 | [diff] [blame] | 5896 | } |
Peter Zijlstra | 3b64089 | 2009-09-16 13:44:33 +0200 | [diff] [blame] | 5897 | |
Mike Galbraith | 63b0e9e | 2015-07-14 17:39:50 +0200 | [diff] [blame] | 5898 | if (!sd) { |
| 5899 | if (sd_flag & SD_BALANCE_WAKE) /* XXX always ? */ |
Morten Rasmussen | 772bd008c | 2016-06-22 18:03:13 +0100 | [diff] [blame] | 5900 | new_cpu = select_idle_sibling(p, prev_cpu, new_cpu); |
Mike Galbraith | 63b0e9e | 2015-07-14 17:39:50 +0200 | [diff] [blame] | 5901 | |
| 5902 | } else while (sd) { |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 5903 | struct sched_group *group; |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 5904 | int weight; |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 5905 | |
Peter Zijlstra | 0763a66 | 2009-09-14 19:37:39 +0200 | [diff] [blame] | 5906 | if (!(sd->flags & sd_flag)) { |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 5907 | sd = sd->child; |
| 5908 | continue; |
| 5909 | } |
| 5910 | |
Vincent Guittot | c44f2a0 | 2013-10-18 13:52:21 +0200 | [diff] [blame] | 5911 | group = find_idlest_group(sd, p, cpu, sd_flag); |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 5912 | if (!group) { |
| 5913 | sd = sd->child; |
| 5914 | continue; |
| 5915 | } |
| 5916 | |
Peter Zijlstra | d7c33c4 | 2009-09-11 12:45:38 +0200 | [diff] [blame] | 5917 | new_cpu = find_idlest_cpu(group, p, cpu); |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 5918 | if (new_cpu == -1 || new_cpu == cpu) { |
| 5919 | /* Now try balancing at a lower domain level of cpu */ |
| 5920 | sd = sd->child; |
| 5921 | continue; |
| 5922 | } |
| 5923 | |
| 5924 | /* Now try balancing at a lower domain level of new_cpu */ |
| 5925 | cpu = new_cpu; |
Peter Zijlstra | 669c55e | 2010-04-16 14:59:29 +0200 | [diff] [blame] | 5926 | weight = sd->span_weight; |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 5927 | sd = NULL; |
| 5928 | for_each_domain(cpu, tmp) { |
Peter Zijlstra | 669c55e | 2010-04-16 14:59:29 +0200 | [diff] [blame] | 5929 | if (weight <= tmp->span_weight) |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 5930 | break; |
Peter Zijlstra | 0763a66 | 2009-09-14 19:37:39 +0200 | [diff] [blame] | 5931 | if (tmp->flags & sd_flag) |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 5932 | sd = tmp; |
| 5933 | } |
| 5934 | /* while loop will break here if sd == NULL */ |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 5935 | } |
Peter Zijlstra | dce840a | 2011-04-07 14:09:50 +0200 | [diff] [blame] | 5936 | rcu_read_unlock(); |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 5937 | |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 5938 | return new_cpu; |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 5939 | } |
Paul Turner | 0a74bef | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 5940 | |
| 5941 | /* |
| 5942 | * Called immediately before a task is migrated to a new cpu; task_cpu(p) and |
| 5943 | * cfs_rq_of(p) references at time of call are still valid and identify the |
Byungchul Park | 525628c | 2015-11-18 09:34:59 +0900 | [diff] [blame] | 5944 | * previous cpu. The caller guarantees p->pi_lock or task_rq(p)->lock is held. |
Paul Turner | 0a74bef | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 5945 | */ |
xiaofeng.yan | 5a4fd03 | 2015-09-23 14:55:59 +0800 | [diff] [blame] | 5946 | static void migrate_task_rq_fair(struct task_struct *p) |
Paul Turner | 0a74bef | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 5947 | { |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 5948 | /* |
Peter Zijlstra | 59efa0b | 2016-05-10 18:24:37 +0200 | [diff] [blame] | 5949 | * As blocked tasks retain absolute vruntime the migration needs to |
| 5950 | * deal with this by subtracting the old and adding the new |
| 5951 | * min_vruntime -- the latter is done by enqueue_entity() when placing |
| 5952 | * the task on the new runqueue. |
| 5953 | */ |
| 5954 | if (p->state == TASK_WAKING) { |
| 5955 | struct sched_entity *se = &p->se; |
| 5956 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 5957 | u64 min_vruntime; |
| 5958 | |
| 5959 | #ifndef CONFIG_64BIT |
| 5960 | u64 min_vruntime_copy; |
| 5961 | |
| 5962 | do { |
| 5963 | min_vruntime_copy = cfs_rq->min_vruntime_copy; |
| 5964 | smp_rmb(); |
| 5965 | min_vruntime = cfs_rq->min_vruntime; |
| 5966 | } while (min_vruntime != min_vruntime_copy); |
| 5967 | #else |
| 5968 | min_vruntime = cfs_rq->min_vruntime; |
| 5969 | #endif |
| 5970 | |
| 5971 | se->vruntime -= min_vruntime; |
| 5972 | } |
| 5973 | |
| 5974 | /* |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 5975 | * We are supposed to update the task to "current" time, then its up to date |
| 5976 | * and ready to go to new CPU/cfs_rq. But we have difficulty in getting |
| 5977 | * what current time is, so simply throw away the out-of-date time. This |
| 5978 | * will result in the wakee task is less decayed, but giving the wakee more |
| 5979 | * load sounds not bad. |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 5980 | */ |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 5981 | remove_entity_load_avg(&p->se); |
| 5982 | |
| 5983 | /* Tell new CPU we are migrated */ |
| 5984 | p->se.avg.last_update_time = 0; |
Ben Segall | 3944a92 | 2014-05-15 15:59:20 -0700 | [diff] [blame] | 5985 | |
| 5986 | /* We have migrated, no longer consider this task hot */ |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 5987 | p->se.exec_start = 0; |
Paul Turner | 0a74bef | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 5988 | } |
Yuyang Du | 1269557 | 2015-07-15 08:04:40 +0800 | [diff] [blame] | 5989 | |
| 5990 | static void task_dead_fair(struct task_struct *p) |
| 5991 | { |
| 5992 | remove_entity_load_avg(&p->se); |
| 5993 | } |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 5994 | #endif /* CONFIG_SMP */ |
| 5995 | |
Peter Zijlstra | e52fb7c | 2009-01-14 12:39:19 +0100 | [diff] [blame] | 5996 | static unsigned long |
| 5997 | wakeup_gran(struct sched_entity *curr, struct sched_entity *se) |
Peter Zijlstra | 0bbd333 | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 5998 | { |
| 5999 | unsigned long gran = sysctl_sched_wakeup_granularity; |
| 6000 | |
| 6001 | /* |
Peter Zijlstra | e52fb7c | 2009-01-14 12:39:19 +0100 | [diff] [blame] | 6002 | * Since its curr running now, convert the gran from real-time |
| 6003 | * to virtual-time in his units. |
Mike Galbraith | 13814d4 | 2010-03-11 17:17:04 +0100 | [diff] [blame] | 6004 | * |
| 6005 | * By using 'se' instead of 'curr' we penalize light tasks, so |
| 6006 | * they get preempted easier. That is, if 'se' < 'curr' then |
| 6007 | * the resulting gran will be larger, therefore penalizing the |
| 6008 | * lighter, if otoh 'se' > 'curr' then the resulting gran will |
| 6009 | * be smaller, again penalizing the lighter task. |
| 6010 | * |
| 6011 | * This is especially important for buddies when the leftmost |
| 6012 | * task is higher priority than the buddy. |
Peter Zijlstra | 0bbd333 | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 6013 | */ |
Shaohua Li | f4ad9bd | 2011-04-08 12:53:09 +0800 | [diff] [blame] | 6014 | return calc_delta_fair(gran, se); |
Peter Zijlstra | 0bbd333 | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 6015 | } |
| 6016 | |
| 6017 | /* |
Peter Zijlstra | 464b752 | 2008-10-24 11:06:15 +0200 | [diff] [blame] | 6018 | * Should 'se' preempt 'curr'. |
| 6019 | * |
| 6020 | * |s1 |
| 6021 | * |s2 |
| 6022 | * |s3 |
| 6023 | * g |
| 6024 | * |<--->|c |
| 6025 | * |
| 6026 | * w(c, s1) = -1 |
| 6027 | * w(c, s2) = 0 |
| 6028 | * w(c, s3) = 1 |
| 6029 | * |
| 6030 | */ |
| 6031 | static int |
| 6032 | wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se) |
| 6033 | { |
| 6034 | s64 gran, vdiff = curr->vruntime - se->vruntime; |
| 6035 | |
| 6036 | if (vdiff <= 0) |
| 6037 | return -1; |
| 6038 | |
Peter Zijlstra | e52fb7c | 2009-01-14 12:39:19 +0100 | [diff] [blame] | 6039 | gran = wakeup_gran(curr, se); |
Peter Zijlstra | 464b752 | 2008-10-24 11:06:15 +0200 | [diff] [blame] | 6040 | if (vdiff > gran) |
| 6041 | return 1; |
| 6042 | |
| 6043 | return 0; |
| 6044 | } |
| 6045 | |
Peter Zijlstra | 0247909 | 2008-11-04 21:25:10 +0100 | [diff] [blame] | 6046 | static void set_last_buddy(struct sched_entity *se) |
| 6047 | { |
Venkatesh Pallipadi | 69c80f3 | 2011-04-13 18:21:09 -0700 | [diff] [blame] | 6048 | if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE)) |
| 6049 | return; |
| 6050 | |
| 6051 | for_each_sched_entity(se) |
| 6052 | cfs_rq_of(se)->last = se; |
Peter Zijlstra | 0247909 | 2008-11-04 21:25:10 +0100 | [diff] [blame] | 6053 | } |
| 6054 | |
| 6055 | static void set_next_buddy(struct sched_entity *se) |
| 6056 | { |
Venkatesh Pallipadi | 69c80f3 | 2011-04-13 18:21:09 -0700 | [diff] [blame] | 6057 | if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE)) |
| 6058 | return; |
| 6059 | |
| 6060 | for_each_sched_entity(se) |
| 6061 | cfs_rq_of(se)->next = se; |
Peter Zijlstra | 0247909 | 2008-11-04 21:25:10 +0100 | [diff] [blame] | 6062 | } |
| 6063 | |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 6064 | static void set_skip_buddy(struct sched_entity *se) |
| 6065 | { |
Venkatesh Pallipadi | 69c80f3 | 2011-04-13 18:21:09 -0700 | [diff] [blame] | 6066 | for_each_sched_entity(se) |
| 6067 | cfs_rq_of(se)->skip = se; |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 6068 | } |
| 6069 | |
Peter Zijlstra | 464b752 | 2008-10-24 11:06:15 +0200 | [diff] [blame] | 6070 | /* |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 6071 | * Preempt the current task with a newly woken task if needed: |
| 6072 | */ |
Peter Zijlstra | 5a9b86f | 2009-09-16 13:47:58 +0200 | [diff] [blame] | 6073 | static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 6074 | { |
| 6075 | struct task_struct *curr = rq->curr; |
Srivatsa Vaddagiri | 8651a86 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 6076 | struct sched_entity *se = &curr->se, *pse = &p->se; |
Mike Galbraith | 03e89e4 | 2008-12-16 08:45:30 +0100 | [diff] [blame] | 6077 | struct cfs_rq *cfs_rq = task_cfs_rq(curr); |
Mike Galbraith | f685cea | 2009-10-23 23:09:22 +0200 | [diff] [blame] | 6078 | int scale = cfs_rq->nr_running >= sched_nr_latency; |
Venkatesh Pallipadi | 2f36825 | 2011-04-14 10:30:53 -0700 | [diff] [blame] | 6079 | int next_buddy_marked = 0; |
Mike Galbraith | 03e89e4 | 2008-12-16 08:45:30 +0100 | [diff] [blame] | 6080 | |
Ingo Molnar | 4ae7d5c | 2008-03-19 01:42:00 +0100 | [diff] [blame] | 6081 | if (unlikely(se == pse)) |
| 6082 | return; |
| 6083 | |
Paul Turner | 5238cdd | 2011-07-21 09:43:37 -0700 | [diff] [blame] | 6084 | /* |
Kirill Tkhai | 163122b | 2014-08-20 13:48:29 +0400 | [diff] [blame] | 6085 | * This is possible from callers such as attach_tasks(), in which we |
Paul Turner | 5238cdd | 2011-07-21 09:43:37 -0700 | [diff] [blame] | 6086 | * unconditionally check_prempt_curr() after an enqueue (which may have |
| 6087 | * lead to a throttle). This both saves work and prevents false |
| 6088 | * next-buddy nomination below. |
| 6089 | */ |
| 6090 | if (unlikely(throttled_hierarchy(cfs_rq_of(pse)))) |
| 6091 | return; |
| 6092 | |
Venkatesh Pallipadi | 2f36825 | 2011-04-14 10:30:53 -0700 | [diff] [blame] | 6093 | if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) { |
Mike Galbraith | 3cb63d5 | 2009-09-11 12:01:17 +0200 | [diff] [blame] | 6094 | set_next_buddy(pse); |
Venkatesh Pallipadi | 2f36825 | 2011-04-14 10:30:53 -0700 | [diff] [blame] | 6095 | next_buddy_marked = 1; |
| 6096 | } |
Peter Zijlstra | 57fdc26 | 2008-09-23 15:33:45 +0200 | [diff] [blame] | 6097 | |
Bharata B Rao | aec0a51 | 2008-08-28 14:42:49 +0530 | [diff] [blame] | 6098 | /* |
| 6099 | * We can come here with TIF_NEED_RESCHED already set from new task |
| 6100 | * wake up path. |
Paul Turner | 5238cdd | 2011-07-21 09:43:37 -0700 | [diff] [blame] | 6101 | * |
| 6102 | * Note: this also catches the edge-case of curr being in a throttled |
| 6103 | * group (e.g. via set_curr_task), since update_curr() (in the |
| 6104 | * enqueue of curr) will have resulted in resched being set. This |
| 6105 | * prevents us from potentially nominating it as a false LAST_BUDDY |
| 6106 | * below. |
Bharata B Rao | aec0a51 | 2008-08-28 14:42:49 +0530 | [diff] [blame] | 6107 | */ |
| 6108 | if (test_tsk_need_resched(curr)) |
| 6109 | return; |
| 6110 | |
Darren Hart | a2f5c9a | 2011-02-22 13:04:33 -0800 | [diff] [blame] | 6111 | /* Idle tasks are by definition preempted by non-idle tasks. */ |
| 6112 | if (unlikely(curr->policy == SCHED_IDLE) && |
| 6113 | likely(p->policy != SCHED_IDLE)) |
| 6114 | goto preempt; |
| 6115 | |
Ingo Molnar | 91c234b | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 6116 | /* |
Darren Hart | a2f5c9a | 2011-02-22 13:04:33 -0800 | [diff] [blame] | 6117 | * Batch and idle tasks do not preempt non-idle tasks (their preemption |
| 6118 | * is driven by the tick): |
Ingo Molnar | 91c234b | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 6119 | */ |
Ingo Molnar | 8ed92e5 | 2012-10-14 14:28:50 +0200 | [diff] [blame] | 6120 | if (unlikely(p->policy != SCHED_NORMAL) || !sched_feat(WAKEUP_PREEMPTION)) |
Ingo Molnar | 91c234b | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 6121 | return; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 6122 | |
Peter Zijlstra | 3a7e73a | 2009-11-28 18:51:02 +0100 | [diff] [blame] | 6123 | find_matching_se(&se, &pse); |
Paul Turner | 9bbd737 | 2011-07-05 19:07:21 -0700 | [diff] [blame] | 6124 | update_curr(cfs_rq_of(se)); |
Peter Zijlstra | 3a7e73a | 2009-11-28 18:51:02 +0100 | [diff] [blame] | 6125 | BUG_ON(!pse); |
Venkatesh Pallipadi | 2f36825 | 2011-04-14 10:30:53 -0700 | [diff] [blame] | 6126 | if (wakeup_preempt_entity(se, pse) == 1) { |
| 6127 | /* |
| 6128 | * Bias pick_next to pick the sched entity that is |
| 6129 | * triggering this preemption. |
| 6130 | */ |
| 6131 | if (!next_buddy_marked) |
| 6132 | set_next_buddy(pse); |
Peter Zijlstra | 3a7e73a | 2009-11-28 18:51:02 +0100 | [diff] [blame] | 6133 | goto preempt; |
Venkatesh Pallipadi | 2f36825 | 2011-04-14 10:30:53 -0700 | [diff] [blame] | 6134 | } |
Jupyung Lee | a65ac74 | 2009-11-17 18:51:40 +0900 | [diff] [blame] | 6135 | |
Peter Zijlstra | 3a7e73a | 2009-11-28 18:51:02 +0100 | [diff] [blame] | 6136 | return; |
| 6137 | |
| 6138 | preempt: |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 6139 | resched_curr(rq); |
Peter Zijlstra | 3a7e73a | 2009-11-28 18:51:02 +0100 | [diff] [blame] | 6140 | /* |
| 6141 | * Only set the backward buddy when the current task is still |
| 6142 | * on the rq. This can happen when a wakeup gets interleaved |
| 6143 | * with schedule on the ->pre_schedule() or idle_balance() |
| 6144 | * point, either of which can * drop the rq lock. |
| 6145 | * |
| 6146 | * Also, during early boot the idle thread is in the fair class, |
| 6147 | * for obvious reasons its a bad idea to schedule back to it. |
| 6148 | */ |
| 6149 | if (unlikely(!se->on_rq || curr == rq->idle)) |
| 6150 | return; |
| 6151 | |
| 6152 | if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se)) |
| 6153 | set_last_buddy(se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 6154 | } |
| 6155 | |
Peter Zijlstra | 606dba2 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 6156 | static struct task_struct * |
Peter Zijlstra | e7904a2 | 2015-08-01 19:25:08 +0200 | [diff] [blame] | 6157 | pick_next_task_fair(struct rq *rq, struct task_struct *prev, struct pin_cookie cookie) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 6158 | { |
| 6159 | struct cfs_rq *cfs_rq = &rq->cfs; |
| 6160 | struct sched_entity *se; |
Peter Zijlstra | 678d571 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 6161 | struct task_struct *p; |
Peter Zijlstra | 37e117c | 2014-02-14 12:25:08 +0100 | [diff] [blame] | 6162 | int new_tasks; |
Peter Zijlstra | 678d571 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 6163 | |
Peter Zijlstra | 6e83125 | 2014-02-11 16:11:48 +0100 | [diff] [blame] | 6164 | again: |
Peter Zijlstra | 678d571 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 6165 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 6166 | if (!cfs_rq->nr_running) |
Peter Zijlstra | 38033c3 | 2014-01-23 20:32:21 +0100 | [diff] [blame] | 6167 | goto idle; |
Peter Zijlstra | 678d571 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 6168 | |
Peter Zijlstra | 3f1d2a3 | 2014-02-12 10:49:30 +0100 | [diff] [blame] | 6169 | if (prev->sched_class != &fair_sched_class) |
Peter Zijlstra | 678d571 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 6170 | goto simple; |
| 6171 | |
| 6172 | /* |
| 6173 | * Because of the set_next_buddy() in dequeue_task_fair() it is rather |
| 6174 | * likely that a next task is from the same cgroup as the current. |
| 6175 | * |
| 6176 | * Therefore attempt to avoid putting and setting the entire cgroup |
| 6177 | * hierarchy, only change the part that actually changes. |
| 6178 | */ |
| 6179 | |
| 6180 | do { |
| 6181 | struct sched_entity *curr = cfs_rq->curr; |
| 6182 | |
| 6183 | /* |
| 6184 | * Since we got here without doing put_prev_entity() we also |
| 6185 | * have to consider cfs_rq->curr. If it is still a runnable |
| 6186 | * entity, update_curr() will update its vruntime, otherwise |
| 6187 | * forget we've ever seen it. |
| 6188 | */ |
Ben Segall | 54d2736 | 2015-04-06 15:28:10 -0700 | [diff] [blame] | 6189 | if (curr) { |
| 6190 | if (curr->on_rq) |
| 6191 | update_curr(cfs_rq); |
| 6192 | else |
| 6193 | curr = NULL; |
Peter Zijlstra | 678d571 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 6194 | |
Ben Segall | 54d2736 | 2015-04-06 15:28:10 -0700 | [diff] [blame] | 6195 | /* |
| 6196 | * This call to check_cfs_rq_runtime() will do the |
| 6197 | * throttle and dequeue its entity in the parent(s). |
| 6198 | * Therefore the 'simple' nr_running test will indeed |
| 6199 | * be correct. |
| 6200 | */ |
| 6201 | if (unlikely(check_cfs_rq_runtime(cfs_rq))) |
| 6202 | goto simple; |
| 6203 | } |
Peter Zijlstra | 678d571 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 6204 | |
| 6205 | se = pick_next_entity(cfs_rq, curr); |
| 6206 | cfs_rq = group_cfs_rq(se); |
| 6207 | } while (cfs_rq); |
| 6208 | |
| 6209 | p = task_of(se); |
| 6210 | |
| 6211 | /* |
| 6212 | * Since we haven't yet done put_prev_entity and if the selected task |
| 6213 | * is a different task than we started out with, try and touch the |
| 6214 | * least amount of cfs_rqs. |
| 6215 | */ |
| 6216 | if (prev != p) { |
| 6217 | struct sched_entity *pse = &prev->se; |
| 6218 | |
| 6219 | while (!(cfs_rq = is_same_group(se, pse))) { |
| 6220 | int se_depth = se->depth; |
| 6221 | int pse_depth = pse->depth; |
| 6222 | |
| 6223 | if (se_depth <= pse_depth) { |
| 6224 | put_prev_entity(cfs_rq_of(pse), pse); |
| 6225 | pse = parent_entity(pse); |
| 6226 | } |
| 6227 | if (se_depth >= pse_depth) { |
| 6228 | set_next_entity(cfs_rq_of(se), se); |
| 6229 | se = parent_entity(se); |
| 6230 | } |
| 6231 | } |
| 6232 | |
| 6233 | put_prev_entity(cfs_rq, pse); |
| 6234 | set_next_entity(cfs_rq, se); |
| 6235 | } |
| 6236 | |
| 6237 | if (hrtick_enabled(rq)) |
| 6238 | hrtick_start_fair(rq, p); |
| 6239 | |
| 6240 | return p; |
| 6241 | simple: |
| 6242 | cfs_rq = &rq->cfs; |
| 6243 | #endif |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 6244 | |
Tim Blechmann | 36ace27 | 2009-11-24 11:55:45 +0100 | [diff] [blame] | 6245 | if (!cfs_rq->nr_running) |
Peter Zijlstra | 38033c3 | 2014-01-23 20:32:21 +0100 | [diff] [blame] | 6246 | goto idle; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 6247 | |
Peter Zijlstra | 3f1d2a3 | 2014-02-12 10:49:30 +0100 | [diff] [blame] | 6248 | put_prev_task(rq, prev); |
Peter Zijlstra | 606dba2 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 6249 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 6250 | do { |
Peter Zijlstra | 678d571 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 6251 | se = pick_next_entity(cfs_rq, NULL); |
Peter Zijlstra | f4b6755 | 2008-11-04 21:25:07 +0100 | [diff] [blame] | 6252 | set_next_entity(cfs_rq, se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 6253 | cfs_rq = group_cfs_rq(se); |
| 6254 | } while (cfs_rq); |
| 6255 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 6256 | p = task_of(se); |
Peter Zijlstra | 678d571 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 6257 | |
Mike Galbraith | b39e66e | 2011-11-22 15:20:07 +0100 | [diff] [blame] | 6258 | if (hrtick_enabled(rq)) |
| 6259 | hrtick_start_fair(rq, p); |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 6260 | |
| 6261 | return p; |
Peter Zijlstra | 38033c3 | 2014-01-23 20:32:21 +0100 | [diff] [blame] | 6262 | |
| 6263 | idle: |
Peter Zijlstra | cbce1a6 | 2015-06-11 14:46:54 +0200 | [diff] [blame] | 6264 | /* |
| 6265 | * This is OK, because current is on_cpu, which avoids it being picked |
| 6266 | * for load-balance and preemption/IRQs are still disabled avoiding |
| 6267 | * further scheduler activity on it and we're being very careful to |
| 6268 | * re-start the picking loop. |
| 6269 | */ |
Peter Zijlstra | e7904a2 | 2015-08-01 19:25:08 +0200 | [diff] [blame] | 6270 | lockdep_unpin_lock(&rq->lock, cookie); |
Kirill Tkhai | e4aa358 | 2014-03-06 13:31:55 +0400 | [diff] [blame] | 6271 | new_tasks = idle_balance(rq); |
Peter Zijlstra | e7904a2 | 2015-08-01 19:25:08 +0200 | [diff] [blame] | 6272 | lockdep_repin_lock(&rq->lock, cookie); |
Peter Zijlstra | 37e117c | 2014-02-14 12:25:08 +0100 | [diff] [blame] | 6273 | /* |
| 6274 | * Because idle_balance() releases (and re-acquires) rq->lock, it is |
| 6275 | * possible for any higher priority task to appear. In that case we |
| 6276 | * must re-start the pick_next_entity() loop. |
| 6277 | */ |
Kirill Tkhai | e4aa358 | 2014-03-06 13:31:55 +0400 | [diff] [blame] | 6278 | if (new_tasks < 0) |
Peter Zijlstra | 37e117c | 2014-02-14 12:25:08 +0100 | [diff] [blame] | 6279 | return RETRY_TASK; |
| 6280 | |
Kirill Tkhai | e4aa358 | 2014-03-06 13:31:55 +0400 | [diff] [blame] | 6281 | if (new_tasks > 0) |
Peter Zijlstra | 38033c3 | 2014-01-23 20:32:21 +0100 | [diff] [blame] | 6282 | goto again; |
Peter Zijlstra | 38033c3 | 2014-01-23 20:32:21 +0100 | [diff] [blame] | 6283 | |
| 6284 | return NULL; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 6285 | } |
| 6286 | |
| 6287 | /* |
| 6288 | * Account for a descheduled task: |
| 6289 | */ |
Ingo Molnar | 31ee529 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 6290 | static void put_prev_task_fair(struct rq *rq, struct task_struct *prev) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 6291 | { |
| 6292 | struct sched_entity *se = &prev->se; |
| 6293 | struct cfs_rq *cfs_rq; |
| 6294 | |
| 6295 | for_each_sched_entity(se) { |
| 6296 | cfs_rq = cfs_rq_of(se); |
Ingo Molnar | ab6cde2 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 6297 | put_prev_entity(cfs_rq, se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 6298 | } |
| 6299 | } |
| 6300 | |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 6301 | /* |
| 6302 | * sched_yield() is very simple |
| 6303 | * |
| 6304 | * The magic of dealing with the ->skip buddy is in pick_next_entity. |
| 6305 | */ |
| 6306 | static void yield_task_fair(struct rq *rq) |
| 6307 | { |
| 6308 | struct task_struct *curr = rq->curr; |
| 6309 | struct cfs_rq *cfs_rq = task_cfs_rq(curr); |
| 6310 | struct sched_entity *se = &curr->se; |
| 6311 | |
| 6312 | /* |
| 6313 | * Are we the only task in the tree? |
| 6314 | */ |
| 6315 | if (unlikely(rq->nr_running == 1)) |
| 6316 | return; |
| 6317 | |
| 6318 | clear_buddies(cfs_rq, se); |
| 6319 | |
| 6320 | if (curr->policy != SCHED_BATCH) { |
| 6321 | update_rq_clock(rq); |
| 6322 | /* |
| 6323 | * Update run-time statistics of the 'current'. |
| 6324 | */ |
| 6325 | update_curr(cfs_rq); |
Mike Galbraith | 916671c | 2011-11-22 15:21:26 +0100 | [diff] [blame] | 6326 | /* |
| 6327 | * Tell update_rq_clock() that we've just updated, |
| 6328 | * so we don't do microscopic update in schedule() |
| 6329 | * and double the fastpath cost. |
| 6330 | */ |
Peter Zijlstra | 9edfbfe | 2015-01-05 11:18:11 +0100 | [diff] [blame] | 6331 | rq_clock_skip_update(rq, true); |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 6332 | } |
| 6333 | |
| 6334 | set_skip_buddy(se); |
| 6335 | } |
| 6336 | |
Mike Galbraith | d95f412 | 2011-02-01 09:50:51 -0500 | [diff] [blame] | 6337 | static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt) |
| 6338 | { |
| 6339 | struct sched_entity *se = &p->se; |
| 6340 | |
Paul Turner | 5238cdd | 2011-07-21 09:43:37 -0700 | [diff] [blame] | 6341 | /* throttled hierarchies are not runnable */ |
| 6342 | if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se))) |
Mike Galbraith | d95f412 | 2011-02-01 09:50:51 -0500 | [diff] [blame] | 6343 | return false; |
| 6344 | |
| 6345 | /* Tell the scheduler that we'd really like pse to run next. */ |
| 6346 | set_next_buddy(se); |
| 6347 | |
Mike Galbraith | d95f412 | 2011-02-01 09:50:51 -0500 | [diff] [blame] | 6348 | yield_task_fair(rq); |
| 6349 | |
| 6350 | return true; |
| 6351 | } |
| 6352 | |
Peter Williams | 681f3e6 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 6353 | #ifdef CONFIG_SMP |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 6354 | /************************************************** |
Peter Zijlstra | e9c84cb | 2012-07-03 13:53:26 +0200 | [diff] [blame] | 6355 | * Fair scheduling class load-balancing methods. |
| 6356 | * |
| 6357 | * BASICS |
| 6358 | * |
| 6359 | * The purpose of load-balancing is to achieve the same basic fairness the |
| 6360 | * per-cpu scheduler provides, namely provide a proportional amount of compute |
| 6361 | * time to each task. This is expressed in the following equation: |
| 6362 | * |
| 6363 | * W_i,n/P_i == W_j,n/P_j for all i,j (1) |
| 6364 | * |
| 6365 | * Where W_i,n is the n-th weight average for cpu i. The instantaneous weight |
| 6366 | * W_i,0 is defined as: |
| 6367 | * |
| 6368 | * W_i,0 = \Sum_j w_i,j (2) |
| 6369 | * |
| 6370 | * Where w_i,j is the weight of the j-th runnable task on cpu i. This weight |
Yuyang Du | 1c3de5e | 2016-03-30 07:07:51 +0800 | [diff] [blame] | 6371 | * is derived from the nice value as per sched_prio_to_weight[]. |
Peter Zijlstra | e9c84cb | 2012-07-03 13:53:26 +0200 | [diff] [blame] | 6372 | * |
| 6373 | * The weight average is an exponential decay average of the instantaneous |
| 6374 | * weight: |
| 6375 | * |
| 6376 | * W'_i,n = (2^n - 1) / 2^n * W_i,n + 1 / 2^n * W_i,0 (3) |
| 6377 | * |
Nicolas Pitre | ced549f | 2014-05-26 18:19:38 -0400 | [diff] [blame] | 6378 | * C_i is the compute capacity of cpu i, typically it is the |
Peter Zijlstra | e9c84cb | 2012-07-03 13:53:26 +0200 | [diff] [blame] | 6379 | * fraction of 'recent' time available for SCHED_OTHER task execution. But it |
| 6380 | * can also include other factors [XXX]. |
| 6381 | * |
| 6382 | * To achieve this balance we define a measure of imbalance which follows |
| 6383 | * directly from (1): |
| 6384 | * |
Nicolas Pitre | ced549f | 2014-05-26 18:19:38 -0400 | [diff] [blame] | 6385 | * imb_i,j = max{ avg(W/C), W_i/C_i } - min{ avg(W/C), W_j/C_j } (4) |
Peter Zijlstra | e9c84cb | 2012-07-03 13:53:26 +0200 | [diff] [blame] | 6386 | * |
| 6387 | * We them move tasks around to minimize the imbalance. In the continuous |
| 6388 | * function space it is obvious this converges, in the discrete case we get |
| 6389 | * a few fun cases generally called infeasible weight scenarios. |
| 6390 | * |
| 6391 | * [XXX expand on: |
| 6392 | * - infeasible weights; |
| 6393 | * - local vs global optima in the discrete case. ] |
| 6394 | * |
| 6395 | * |
| 6396 | * SCHED DOMAINS |
| 6397 | * |
| 6398 | * In order to solve the imbalance equation (4), and avoid the obvious O(n^2) |
| 6399 | * for all i,j solution, we create a tree of cpus that follows the hardware |
| 6400 | * topology where each level pairs two lower groups (or better). This results |
| 6401 | * in O(log n) layers. Furthermore we reduce the number of cpus going up the |
| 6402 | * tree to only the first of the previous level and we decrease the frequency |
| 6403 | * of load-balance at each level inv. proportional to the number of cpus in |
| 6404 | * the groups. |
| 6405 | * |
| 6406 | * This yields: |
| 6407 | * |
| 6408 | * log_2 n 1 n |
| 6409 | * \Sum { --- * --- * 2^i } = O(n) (5) |
| 6410 | * i = 0 2^i 2^i |
| 6411 | * `- size of each group |
| 6412 | * | | `- number of cpus doing load-balance |
| 6413 | * | `- freq |
| 6414 | * `- sum over all levels |
| 6415 | * |
| 6416 | * Coupled with a limit on how many tasks we can migrate every balance pass, |
| 6417 | * this makes (5) the runtime complexity of the balancer. |
| 6418 | * |
| 6419 | * An important property here is that each CPU is still (indirectly) connected |
| 6420 | * to every other cpu in at most O(log n) steps: |
| 6421 | * |
| 6422 | * The adjacency matrix of the resulting graph is given by: |
| 6423 | * |
Byungchul Park | 97a7142 | 2015-07-05 18:33:48 +0900 | [diff] [blame] | 6424 | * log_2 n |
Peter Zijlstra | e9c84cb | 2012-07-03 13:53:26 +0200 | [diff] [blame] | 6425 | * A_i,j = \Union (i % 2^k == 0) && i / 2^(k+1) == j / 2^(k+1) (6) |
| 6426 | * k = 0 |
| 6427 | * |
| 6428 | * And you'll find that: |
| 6429 | * |
| 6430 | * A^(log_2 n)_i,j != 0 for all i,j (7) |
| 6431 | * |
| 6432 | * Showing there's indeed a path between every cpu in at most O(log n) steps. |
| 6433 | * The task movement gives a factor of O(m), giving a convergence complexity |
| 6434 | * of: |
| 6435 | * |
| 6436 | * O(nm log n), n := nr_cpus, m := nr_tasks (8) |
| 6437 | * |
| 6438 | * |
| 6439 | * WORK CONSERVING |
| 6440 | * |
| 6441 | * In order to avoid CPUs going idle while there's still work to do, new idle |
| 6442 | * balancing is more aggressive and has the newly idle cpu iterate up the domain |
| 6443 | * tree itself instead of relying on other CPUs to bring it work. |
| 6444 | * |
| 6445 | * This adds some complexity to both (5) and (8) but it reduces the total idle |
| 6446 | * time. |
| 6447 | * |
| 6448 | * [XXX more?] |
| 6449 | * |
| 6450 | * |
| 6451 | * CGROUPS |
| 6452 | * |
| 6453 | * Cgroups make a horror show out of (2), instead of a simple sum we get: |
| 6454 | * |
| 6455 | * s_k,i |
| 6456 | * W_i,0 = \Sum_j \Prod_k w_k * ----- (9) |
| 6457 | * S_k |
| 6458 | * |
| 6459 | * Where |
| 6460 | * |
| 6461 | * s_k,i = \Sum_j w_i,j,k and S_k = \Sum_i s_k,i (10) |
| 6462 | * |
| 6463 | * w_i,j,k is the weight of the j-th runnable task in the k-th cgroup on cpu i. |
| 6464 | * |
| 6465 | * The big problem is S_k, its a global sum needed to compute a local (W_i) |
| 6466 | * property. |
| 6467 | * |
| 6468 | * [XXX write more on how we solve this.. _after_ merging pjt's patches that |
| 6469 | * rewrite all of this once again.] |
Byungchul Park | 97a7142 | 2015-07-05 18:33:48 +0900 | [diff] [blame] | 6470 | */ |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 6471 | |
Hiroshi Shimamoto | ed387b7 | 2012-01-31 11:40:32 +0900 | [diff] [blame] | 6472 | static unsigned long __read_mostly max_load_balance_interval = HZ/10; |
| 6473 | |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 6474 | enum fbq_type { regular, remote, all }; |
| 6475 | |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 6476 | #define LBF_ALL_PINNED 0x01 |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 6477 | #define LBF_NEED_BREAK 0x02 |
Peter Zijlstra | 6263322 | 2013-08-19 12:41:09 +0200 | [diff] [blame] | 6478 | #define LBF_DST_PINNED 0x04 |
| 6479 | #define LBF_SOME_PINNED 0x08 |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 6480 | |
| 6481 | struct lb_env { |
| 6482 | struct sched_domain *sd; |
| 6483 | |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 6484 | struct rq *src_rq; |
Prashanth Nageshappa | 85c1e7d | 2012-06-19 17:47:34 +0530 | [diff] [blame] | 6485 | int src_cpu; |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 6486 | |
| 6487 | int dst_cpu; |
| 6488 | struct rq *dst_rq; |
| 6489 | |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 6490 | struct cpumask *dst_grpmask; |
| 6491 | int new_dst_cpu; |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 6492 | enum cpu_idle_type idle; |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 6493 | long imbalance; |
Michael Wang | b9403130 | 2012-07-12 16:10:13 +0800 | [diff] [blame] | 6494 | /* The set of CPUs under consideration for load-balancing */ |
| 6495 | struct cpumask *cpus; |
| 6496 | |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 6497 | unsigned int flags; |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 6498 | |
| 6499 | unsigned int loop; |
| 6500 | unsigned int loop_break; |
| 6501 | unsigned int loop_max; |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 6502 | |
| 6503 | enum fbq_type fbq_type; |
Kirill Tkhai | 163122b | 2014-08-20 13:48:29 +0400 | [diff] [blame] | 6504 | struct list_head tasks; |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 6505 | }; |
| 6506 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6507 | /* |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 6508 | * Is this task likely cache-hot: |
| 6509 | */ |
Hillf Danton | 5d5e2b1 | 2014-06-10 10:58:43 +0200 | [diff] [blame] | 6510 | static int task_hot(struct task_struct *p, struct lb_env *env) |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 6511 | { |
| 6512 | s64 delta; |
| 6513 | |
Kirill Tkhai | e5673f2 | 2014-08-20 13:48:01 +0400 | [diff] [blame] | 6514 | lockdep_assert_held(&env->src_rq->lock); |
| 6515 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 6516 | if (p->sched_class != &fair_sched_class) |
| 6517 | return 0; |
| 6518 | |
| 6519 | if (unlikely(p->policy == SCHED_IDLE)) |
| 6520 | return 0; |
| 6521 | |
| 6522 | /* |
| 6523 | * Buddy candidates are cache hot: |
| 6524 | */ |
Hillf Danton | 5d5e2b1 | 2014-06-10 10:58:43 +0200 | [diff] [blame] | 6525 | if (sched_feat(CACHE_HOT_BUDDY) && env->dst_rq->nr_running && |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 6526 | (&p->se == cfs_rq_of(&p->se)->next || |
| 6527 | &p->se == cfs_rq_of(&p->se)->last)) |
| 6528 | return 1; |
| 6529 | |
| 6530 | if (sysctl_sched_migration_cost == -1) |
| 6531 | return 1; |
| 6532 | if (sysctl_sched_migration_cost == 0) |
| 6533 | return 0; |
| 6534 | |
Hillf Danton | 5d5e2b1 | 2014-06-10 10:58:43 +0200 | [diff] [blame] | 6535 | delta = rq_clock_task(env->src_rq) - p->se.exec_start; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 6536 | |
| 6537 | return delta < (s64)sysctl_sched_migration_cost; |
| 6538 | } |
| 6539 | |
Mel Gorman | 3a7053b | 2013-10-07 11:29:00 +0100 | [diff] [blame] | 6540 | #ifdef CONFIG_NUMA_BALANCING |
Rik van Riel | c1ceac6 | 2015-05-14 22:59:36 -0400 | [diff] [blame] | 6541 | /* |
Srikar Dronamraju | 2a1ed24 | 2015-06-16 17:25:59 +0530 | [diff] [blame] | 6542 | * Returns 1, if task migration degrades locality |
| 6543 | * Returns 0, if task migration improves locality i.e migration preferred. |
| 6544 | * Returns -1, if task migration is not affected by locality. |
Rik van Riel | c1ceac6 | 2015-05-14 22:59:36 -0400 | [diff] [blame] | 6545 | */ |
Srikar Dronamraju | 2a1ed24 | 2015-06-16 17:25:59 +0530 | [diff] [blame] | 6546 | static int migrate_degrades_locality(struct task_struct *p, struct lb_env *env) |
Mel Gorman | 3a7053b | 2013-10-07 11:29:00 +0100 | [diff] [blame] | 6547 | { |
Rik van Riel | b1ad065 | 2014-05-15 13:03:06 -0400 | [diff] [blame] | 6548 | struct numa_group *numa_group = rcu_dereference(p->numa_group); |
Rik van Riel | c1ceac6 | 2015-05-14 22:59:36 -0400 | [diff] [blame] | 6549 | unsigned long src_faults, dst_faults; |
Mel Gorman | 3a7053b | 2013-10-07 11:29:00 +0100 | [diff] [blame] | 6550 | int src_nid, dst_nid; |
| 6551 | |
Srikar Dronamraju | 2a59572 | 2015-08-11 21:54:21 +0530 | [diff] [blame] | 6552 | if (!static_branch_likely(&sched_numa_balancing)) |
Srikar Dronamraju | 2a1ed24 | 2015-06-16 17:25:59 +0530 | [diff] [blame] | 6553 | return -1; |
| 6554 | |
Srikar Dronamraju | c3b9bc5 | 2015-08-11 16:30:12 +0530 | [diff] [blame] | 6555 | if (!p->numa_faults || !(env->sd->flags & SD_NUMA)) |
Srikar Dronamraju | 2a1ed24 | 2015-06-16 17:25:59 +0530 | [diff] [blame] | 6556 | return -1; |
Mel Gorman | 7a0f308 | 2013-10-07 11:29:01 +0100 | [diff] [blame] | 6557 | |
| 6558 | src_nid = cpu_to_node(env->src_cpu); |
| 6559 | dst_nid = cpu_to_node(env->dst_cpu); |
| 6560 | |
Mel Gorman | 83e1d2c | 2013-10-07 11:29:27 +0100 | [diff] [blame] | 6561 | if (src_nid == dst_nid) |
Srikar Dronamraju | 2a1ed24 | 2015-06-16 17:25:59 +0530 | [diff] [blame] | 6562 | return -1; |
Mel Gorman | 7a0f308 | 2013-10-07 11:29:01 +0100 | [diff] [blame] | 6563 | |
Srikar Dronamraju | 2a1ed24 | 2015-06-16 17:25:59 +0530 | [diff] [blame] | 6564 | /* Migrating away from the preferred node is always bad. */ |
| 6565 | if (src_nid == p->numa_preferred_nid) { |
| 6566 | if (env->src_rq->nr_running > env->src_rq->nr_preferred_running) |
| 6567 | return 1; |
| 6568 | else |
| 6569 | return -1; |
| 6570 | } |
Mel Gorman | 83e1d2c | 2013-10-07 11:29:27 +0100 | [diff] [blame] | 6571 | |
Rik van Riel | c1ceac6 | 2015-05-14 22:59:36 -0400 | [diff] [blame] | 6572 | /* Encourage migration to the preferred node. */ |
| 6573 | if (dst_nid == p->numa_preferred_nid) |
Srikar Dronamraju | 2a1ed24 | 2015-06-16 17:25:59 +0530 | [diff] [blame] | 6574 | return 0; |
Rik van Riel | c1ceac6 | 2015-05-14 22:59:36 -0400 | [diff] [blame] | 6575 | |
| 6576 | if (numa_group) { |
| 6577 | src_faults = group_faults(p, src_nid); |
| 6578 | dst_faults = group_faults(p, dst_nid); |
| 6579 | } else { |
| 6580 | src_faults = task_faults(p, src_nid); |
| 6581 | dst_faults = task_faults(p, dst_nid); |
| 6582 | } |
| 6583 | |
| 6584 | return dst_faults < src_faults; |
Mel Gorman | 7a0f308 | 2013-10-07 11:29:01 +0100 | [diff] [blame] | 6585 | } |
| 6586 | |
Mel Gorman | 3a7053b | 2013-10-07 11:29:00 +0100 | [diff] [blame] | 6587 | #else |
Srikar Dronamraju | 2a1ed24 | 2015-06-16 17:25:59 +0530 | [diff] [blame] | 6588 | static inline int migrate_degrades_locality(struct task_struct *p, |
Mel Gorman | 3a7053b | 2013-10-07 11:29:00 +0100 | [diff] [blame] | 6589 | struct lb_env *env) |
| 6590 | { |
Srikar Dronamraju | 2a1ed24 | 2015-06-16 17:25:59 +0530 | [diff] [blame] | 6591 | return -1; |
Mel Gorman | 7a0f308 | 2013-10-07 11:29:01 +0100 | [diff] [blame] | 6592 | } |
Mel Gorman | 3a7053b | 2013-10-07 11:29:00 +0100 | [diff] [blame] | 6593 | #endif |
| 6594 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 6595 | /* |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6596 | * can_migrate_task - may task p from runqueue rq be migrated to this_cpu? |
| 6597 | */ |
| 6598 | static |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 6599 | int can_migrate_task(struct task_struct *p, struct lb_env *env) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6600 | { |
Srikar Dronamraju | 2a1ed24 | 2015-06-16 17:25:59 +0530 | [diff] [blame] | 6601 | int tsk_cache_hot; |
Kirill Tkhai | e5673f2 | 2014-08-20 13:48:01 +0400 | [diff] [blame] | 6602 | |
| 6603 | lockdep_assert_held(&env->src_rq->lock); |
| 6604 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6605 | /* |
| 6606 | * We do not migrate tasks that are: |
Joonsoo Kim | d319808 | 2013-04-23 17:27:40 +0900 | [diff] [blame] | 6607 | * 1) throttled_lb_pair, or |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6608 | * 2) cannot be migrated to this CPU due to cpus_allowed, or |
Joonsoo Kim | d319808 | 2013-04-23 17:27:40 +0900 | [diff] [blame] | 6609 | * 3) running (obviously), or |
| 6610 | * 4) are cache-hot on their current CPU. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6611 | */ |
Joonsoo Kim | d319808 | 2013-04-23 17:27:40 +0900 | [diff] [blame] | 6612 | if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu)) |
| 6613 | return 0; |
| 6614 | |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 6615 | if (!cpumask_test_cpu(env->dst_cpu, tsk_cpus_allowed(p))) { |
Joonsoo Kim | e02e60c | 2013-04-23 17:27:42 +0900 | [diff] [blame] | 6616 | int cpu; |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 6617 | |
Josh Poimboeuf | ae92882 | 2016-06-17 12:43:24 -0500 | [diff] [blame] | 6618 | schedstat_inc(p->se.statistics.nr_failed_migrations_affine); |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 6619 | |
Peter Zijlstra | 6263322 | 2013-08-19 12:41:09 +0200 | [diff] [blame] | 6620 | env->flags |= LBF_SOME_PINNED; |
| 6621 | |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 6622 | /* |
| 6623 | * Remember if this task can be migrated to any other cpu in |
| 6624 | * our sched_group. We may want to revisit it if we couldn't |
| 6625 | * meet load balance goals by pulling other tasks on src_cpu. |
| 6626 | * |
| 6627 | * Also avoid computing new_dst_cpu if we have already computed |
| 6628 | * one in current iteration. |
| 6629 | */ |
Peter Zijlstra | 6263322 | 2013-08-19 12:41:09 +0200 | [diff] [blame] | 6630 | if (!env->dst_grpmask || (env->flags & LBF_DST_PINNED)) |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 6631 | return 0; |
| 6632 | |
Joonsoo Kim | e02e60c | 2013-04-23 17:27:42 +0900 | [diff] [blame] | 6633 | /* Prevent to re-select dst_cpu via env's cpus */ |
| 6634 | for_each_cpu_and(cpu, env->dst_grpmask, env->cpus) { |
| 6635 | if (cpumask_test_cpu(cpu, tsk_cpus_allowed(p))) { |
Peter Zijlstra | 6263322 | 2013-08-19 12:41:09 +0200 | [diff] [blame] | 6636 | env->flags |= LBF_DST_PINNED; |
Joonsoo Kim | e02e60c | 2013-04-23 17:27:42 +0900 | [diff] [blame] | 6637 | env->new_dst_cpu = cpu; |
| 6638 | break; |
| 6639 | } |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 6640 | } |
Joonsoo Kim | e02e60c | 2013-04-23 17:27:42 +0900 | [diff] [blame] | 6641 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6642 | return 0; |
| 6643 | } |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 6644 | |
| 6645 | /* Record that we found atleast one task that could run on dst_cpu */ |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 6646 | env->flags &= ~LBF_ALL_PINNED; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6647 | |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 6648 | if (task_running(env->src_rq, p)) { |
Josh Poimboeuf | ae92882 | 2016-06-17 12:43:24 -0500 | [diff] [blame] | 6649 | schedstat_inc(p->se.statistics.nr_failed_migrations_running); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6650 | return 0; |
| 6651 | } |
| 6652 | |
| 6653 | /* |
| 6654 | * Aggressive migration if: |
Mel Gorman | 3a7053b | 2013-10-07 11:29:00 +0100 | [diff] [blame] | 6655 | * 1) destination numa is preferred |
| 6656 | * 2) task is cache cold, or |
| 6657 | * 3) too many balance attempts have failed. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6658 | */ |
Srikar Dronamraju | 2a1ed24 | 2015-06-16 17:25:59 +0530 | [diff] [blame] | 6659 | tsk_cache_hot = migrate_degrades_locality(p, env); |
| 6660 | if (tsk_cache_hot == -1) |
| 6661 | tsk_cache_hot = task_hot(p, env); |
Mel Gorman | 3a7053b | 2013-10-07 11:29:00 +0100 | [diff] [blame] | 6662 | |
Srikar Dronamraju | 2a1ed24 | 2015-06-16 17:25:59 +0530 | [diff] [blame] | 6663 | if (tsk_cache_hot <= 0 || |
Kirill Tkhai | 7a96c23 | 2014-09-22 22:36:12 +0400 | [diff] [blame] | 6664 | env->sd->nr_balance_failed > env->sd->cache_nice_tries) { |
Srikar Dronamraju | 2a1ed24 | 2015-06-16 17:25:59 +0530 | [diff] [blame] | 6665 | if (tsk_cache_hot == 1) { |
Josh Poimboeuf | ae92882 | 2016-06-17 12:43:24 -0500 | [diff] [blame] | 6666 | schedstat_inc(env->sd->lb_hot_gained[env->idle]); |
| 6667 | schedstat_inc(p->se.statistics.nr_forced_migrations); |
Mel Gorman | 3a7053b | 2013-10-07 11:29:00 +0100 | [diff] [blame] | 6668 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6669 | return 1; |
| 6670 | } |
| 6671 | |
Josh Poimboeuf | ae92882 | 2016-06-17 12:43:24 -0500 | [diff] [blame] | 6672 | schedstat_inc(p->se.statistics.nr_failed_migrations_hot); |
Zhang Hang | 4e2dcb7 | 2013-04-10 14:04:55 +0800 | [diff] [blame] | 6673 | return 0; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6674 | } |
| 6675 | |
Peter Zijlstra | 897c395 | 2009-12-17 17:45:42 +0100 | [diff] [blame] | 6676 | /* |
Kirill Tkhai | 163122b | 2014-08-20 13:48:29 +0400 | [diff] [blame] | 6677 | * detach_task() -- detach the task for the migration specified in env |
Peter Zijlstra | 897c395 | 2009-12-17 17:45:42 +0100 | [diff] [blame] | 6678 | */ |
Kirill Tkhai | 163122b | 2014-08-20 13:48:29 +0400 | [diff] [blame] | 6679 | static void detach_task(struct task_struct *p, struct lb_env *env) |
| 6680 | { |
| 6681 | lockdep_assert_held(&env->src_rq->lock); |
| 6682 | |
Kirill Tkhai | 163122b | 2014-08-20 13:48:29 +0400 | [diff] [blame] | 6683 | p->on_rq = TASK_ON_RQ_MIGRATING; |
Joonwoo Park | 3ea94de | 2015-11-12 19:38:54 -0800 | [diff] [blame] | 6684 | deactivate_task(env->src_rq, p, 0); |
Kirill Tkhai | 163122b | 2014-08-20 13:48:29 +0400 | [diff] [blame] | 6685 | set_task_cpu(p, env->dst_cpu); |
| 6686 | } |
| 6687 | |
| 6688 | /* |
Kirill Tkhai | e5673f2 | 2014-08-20 13:48:01 +0400 | [diff] [blame] | 6689 | * detach_one_task() -- tries to dequeue exactly one task from env->src_rq, as |
Peter Zijlstra | 897c395 | 2009-12-17 17:45:42 +0100 | [diff] [blame] | 6690 | * part of active balancing operations within "domain". |
Peter Zijlstra | 897c395 | 2009-12-17 17:45:42 +0100 | [diff] [blame] | 6691 | * |
Kirill Tkhai | e5673f2 | 2014-08-20 13:48:01 +0400 | [diff] [blame] | 6692 | * Returns a task if successful and NULL otherwise. |
Peter Zijlstra | 897c395 | 2009-12-17 17:45:42 +0100 | [diff] [blame] | 6693 | */ |
Kirill Tkhai | e5673f2 | 2014-08-20 13:48:01 +0400 | [diff] [blame] | 6694 | static struct task_struct *detach_one_task(struct lb_env *env) |
Peter Zijlstra | 897c395 | 2009-12-17 17:45:42 +0100 | [diff] [blame] | 6695 | { |
| 6696 | struct task_struct *p, *n; |
Peter Zijlstra | 897c395 | 2009-12-17 17:45:42 +0100 | [diff] [blame] | 6697 | |
Kirill Tkhai | e5673f2 | 2014-08-20 13:48:01 +0400 | [diff] [blame] | 6698 | lockdep_assert_held(&env->src_rq->lock); |
| 6699 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 6700 | list_for_each_entry_safe(p, n, &env->src_rq->cfs_tasks, se.group_node) { |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 6701 | if (!can_migrate_task(p, env)) |
| 6702 | continue; |
Peter Zijlstra | 897c395 | 2009-12-17 17:45:42 +0100 | [diff] [blame] | 6703 | |
Kirill Tkhai | 163122b | 2014-08-20 13:48:29 +0400 | [diff] [blame] | 6704 | detach_task(p, env); |
Kirill Tkhai | e5673f2 | 2014-08-20 13:48:01 +0400 | [diff] [blame] | 6705 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 6706 | /* |
Kirill Tkhai | e5673f2 | 2014-08-20 13:48:01 +0400 | [diff] [blame] | 6707 | * Right now, this is only the second place where |
Kirill Tkhai | 163122b | 2014-08-20 13:48:29 +0400 | [diff] [blame] | 6708 | * lb_gained[env->idle] is updated (other is detach_tasks) |
Kirill Tkhai | e5673f2 | 2014-08-20 13:48:01 +0400 | [diff] [blame] | 6709 | * so we can safely collect stats here rather than |
Kirill Tkhai | 163122b | 2014-08-20 13:48:29 +0400 | [diff] [blame] | 6710 | * inside detach_tasks(). |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 6711 | */ |
Josh Poimboeuf | ae92882 | 2016-06-17 12:43:24 -0500 | [diff] [blame] | 6712 | schedstat_inc(env->sd->lb_gained[env->idle]); |
Kirill Tkhai | e5673f2 | 2014-08-20 13:48:01 +0400 | [diff] [blame] | 6713 | return p; |
Peter Zijlstra | 897c395 | 2009-12-17 17:45:42 +0100 | [diff] [blame] | 6714 | } |
Kirill Tkhai | e5673f2 | 2014-08-20 13:48:01 +0400 | [diff] [blame] | 6715 | return NULL; |
Peter Zijlstra | 897c395 | 2009-12-17 17:45:42 +0100 | [diff] [blame] | 6716 | } |
| 6717 | |
Peter Zijlstra | eb95308 | 2012-04-17 13:38:40 +0200 | [diff] [blame] | 6718 | static const unsigned int sched_nr_migrate_break = 32; |
| 6719 | |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 6720 | /* |
Kirill Tkhai | 163122b | 2014-08-20 13:48:29 +0400 | [diff] [blame] | 6721 | * detach_tasks() -- tries to detach up to imbalance weighted load from |
| 6722 | * busiest_rq, as part of a balancing operation within domain "sd". |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 6723 | * |
Kirill Tkhai | 163122b | 2014-08-20 13:48:29 +0400 | [diff] [blame] | 6724 | * Returns number of detached tasks if successful and 0 otherwise. |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 6725 | */ |
Kirill Tkhai | 163122b | 2014-08-20 13:48:29 +0400 | [diff] [blame] | 6726 | static int detach_tasks(struct lb_env *env) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6727 | { |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 6728 | struct list_head *tasks = &env->src_rq->cfs_tasks; |
| 6729 | struct task_struct *p; |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 6730 | unsigned long load; |
Kirill Tkhai | 163122b | 2014-08-20 13:48:29 +0400 | [diff] [blame] | 6731 | int detached = 0; |
| 6732 | |
| 6733 | lockdep_assert_held(&env->src_rq->lock); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6734 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 6735 | if (env->imbalance <= 0) |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 6736 | return 0; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6737 | |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 6738 | while (!list_empty(tasks)) { |
Yuyang Du | 985d3a4 | 2015-07-06 06:11:51 +0800 | [diff] [blame] | 6739 | /* |
| 6740 | * We don't want to steal all, otherwise we may be treated likewise, |
| 6741 | * which could at worst lead to a livelock crash. |
| 6742 | */ |
| 6743 | if (env->idle != CPU_NOT_IDLE && env->src_rq->nr_running <= 1) |
| 6744 | break; |
| 6745 | |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 6746 | p = list_first_entry(tasks, struct task_struct, se.group_node); |
| 6747 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 6748 | env->loop++; |
| 6749 | /* We've more or less seen every task there is, call it quits */ |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 6750 | if (env->loop > env->loop_max) |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 6751 | break; |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 6752 | |
| 6753 | /* take a breather every nr_migrate tasks */ |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 6754 | if (env->loop > env->loop_break) { |
Peter Zijlstra | eb95308 | 2012-04-17 13:38:40 +0200 | [diff] [blame] | 6755 | env->loop_break += sched_nr_migrate_break; |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 6756 | env->flags |= LBF_NEED_BREAK; |
Peter Zijlstra | ee00e66 | 2009-12-17 17:25:20 +0100 | [diff] [blame] | 6757 | break; |
Peter Zijlstra | a195f00 | 2011-09-22 15:30:18 +0200 | [diff] [blame] | 6758 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6759 | |
Joonsoo Kim | d319808 | 2013-04-23 17:27:40 +0900 | [diff] [blame] | 6760 | if (!can_migrate_task(p, env)) |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 6761 | goto next; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6762 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 6763 | load = task_h_load(p); |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 6764 | |
Peter Zijlstra | eb95308 | 2012-04-17 13:38:40 +0200 | [diff] [blame] | 6765 | if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed) |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 6766 | goto next; |
| 6767 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 6768 | if ((load / 2) > env->imbalance) |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 6769 | goto next; |
| 6770 | |
Kirill Tkhai | 163122b | 2014-08-20 13:48:29 +0400 | [diff] [blame] | 6771 | detach_task(p, env); |
| 6772 | list_add(&p->se.group_node, &env->tasks); |
| 6773 | |
| 6774 | detached++; |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 6775 | env->imbalance -= load; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6776 | |
| 6777 | #ifdef CONFIG_PREEMPT |
Peter Zijlstra | ee00e66 | 2009-12-17 17:25:20 +0100 | [diff] [blame] | 6778 | /* |
| 6779 | * NEWIDLE balancing is a source of latency, so preemptible |
Kirill Tkhai | 163122b | 2014-08-20 13:48:29 +0400 | [diff] [blame] | 6780 | * kernels will stop after the first task is detached to minimize |
Peter Zijlstra | ee00e66 | 2009-12-17 17:25:20 +0100 | [diff] [blame] | 6781 | * the critical section. |
| 6782 | */ |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 6783 | if (env->idle == CPU_NEWLY_IDLE) |
Peter Zijlstra | ee00e66 | 2009-12-17 17:25:20 +0100 | [diff] [blame] | 6784 | break; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6785 | #endif |
| 6786 | |
Peter Zijlstra | ee00e66 | 2009-12-17 17:25:20 +0100 | [diff] [blame] | 6787 | /* |
| 6788 | * We only want to steal up to the prescribed amount of |
| 6789 | * weighted load. |
| 6790 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 6791 | if (env->imbalance <= 0) |
Peter Zijlstra | ee00e66 | 2009-12-17 17:25:20 +0100 | [diff] [blame] | 6792 | break; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6793 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 6794 | continue; |
| 6795 | next: |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 6796 | list_move_tail(&p->se.group_node, tasks); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6797 | } |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 6798 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6799 | /* |
Kirill Tkhai | 163122b | 2014-08-20 13:48:29 +0400 | [diff] [blame] | 6800 | * Right now, this is one of only two places we collect this stat |
| 6801 | * so we can safely collect detach_one_task() stats here rather |
| 6802 | * than inside detach_one_task(). |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6803 | */ |
Josh Poimboeuf | ae92882 | 2016-06-17 12:43:24 -0500 | [diff] [blame] | 6804 | schedstat_add(env->sd->lb_gained[env->idle], detached); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6805 | |
Kirill Tkhai | 163122b | 2014-08-20 13:48:29 +0400 | [diff] [blame] | 6806 | return detached; |
| 6807 | } |
| 6808 | |
| 6809 | /* |
| 6810 | * attach_task() -- attach the task detached by detach_task() to its new rq. |
| 6811 | */ |
| 6812 | static void attach_task(struct rq *rq, struct task_struct *p) |
| 6813 | { |
| 6814 | lockdep_assert_held(&rq->lock); |
| 6815 | |
| 6816 | BUG_ON(task_rq(p) != rq); |
Kirill Tkhai | 163122b | 2014-08-20 13:48:29 +0400 | [diff] [blame] | 6817 | activate_task(rq, p, 0); |
Joonwoo Park | 3ea94de | 2015-11-12 19:38:54 -0800 | [diff] [blame] | 6818 | p->on_rq = TASK_ON_RQ_QUEUED; |
Kirill Tkhai | 163122b | 2014-08-20 13:48:29 +0400 | [diff] [blame] | 6819 | check_preempt_curr(rq, p, 0); |
| 6820 | } |
| 6821 | |
| 6822 | /* |
| 6823 | * attach_one_task() -- attaches the task returned from detach_one_task() to |
| 6824 | * its new rq. |
| 6825 | */ |
| 6826 | static void attach_one_task(struct rq *rq, struct task_struct *p) |
| 6827 | { |
| 6828 | raw_spin_lock(&rq->lock); |
| 6829 | attach_task(rq, p); |
| 6830 | raw_spin_unlock(&rq->lock); |
| 6831 | } |
| 6832 | |
| 6833 | /* |
| 6834 | * attach_tasks() -- attaches all tasks detached by detach_tasks() to their |
| 6835 | * new rq. |
| 6836 | */ |
| 6837 | static void attach_tasks(struct lb_env *env) |
| 6838 | { |
| 6839 | struct list_head *tasks = &env->tasks; |
| 6840 | struct task_struct *p; |
| 6841 | |
| 6842 | raw_spin_lock(&env->dst_rq->lock); |
| 6843 | |
| 6844 | while (!list_empty(tasks)) { |
| 6845 | p = list_first_entry(tasks, struct task_struct, se.group_node); |
| 6846 | list_del_init(&p->se.group_node); |
| 6847 | |
| 6848 | attach_task(env->dst_rq, p); |
| 6849 | } |
| 6850 | |
| 6851 | raw_spin_unlock(&env->dst_rq->lock); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6852 | } |
| 6853 | |
Peter Zijlstra | 230059de | 2009-12-17 17:47:12 +0100 | [diff] [blame] | 6854 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Paul Turner | 48a1675 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 6855 | static void update_blocked_averages(int cpu) |
Peter Zijlstra | 9e3081c | 2010-11-15 15:47:02 -0800 | [diff] [blame] | 6856 | { |
Peter Zijlstra | 9e3081c | 2010-11-15 15:47:02 -0800 | [diff] [blame] | 6857 | struct rq *rq = cpu_rq(cpu); |
Paul Turner | 48a1675 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 6858 | struct cfs_rq *cfs_rq; |
| 6859 | unsigned long flags; |
Peter Zijlstra | 9e3081c | 2010-11-15 15:47:02 -0800 | [diff] [blame] | 6860 | |
Paul Turner | 48a1675 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 6861 | raw_spin_lock_irqsave(&rq->lock, flags); |
| 6862 | update_rq_clock(rq); |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 6863 | |
Peter Zijlstra | 9763b67 | 2011-07-13 13:09:25 +0200 | [diff] [blame] | 6864 | /* |
| 6865 | * Iterates the task_group tree in a bottom up fashion, see |
| 6866 | * list_add_leaf_cfs_rq() for details. |
| 6867 | */ |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 6868 | for_each_leaf_cfs_rq(rq, cfs_rq) { |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 6869 | /* throttled entities do not contribute to load */ |
| 6870 | if (throttled_hierarchy(cfs_rq)) |
| 6871 | continue; |
Paul Turner | 48a1675 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 6872 | |
Steve Muckle | a2c6c91 | 2016-03-24 15:26:07 -0700 | [diff] [blame] | 6873 | if (update_cfs_rq_load_avg(cfs_rq_clock_task(cfs_rq), cfs_rq, true)) |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 6874 | update_tg_load_avg(cfs_rq, 0); |
| 6875 | } |
Paul Turner | 48a1675 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 6876 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
Peter Zijlstra | 9e3081c | 2010-11-15 15:47:02 -0800 | [diff] [blame] | 6877 | } |
| 6878 | |
Peter Zijlstra | 9763b67 | 2011-07-13 13:09:25 +0200 | [diff] [blame] | 6879 | /* |
Vladimir Davydov | 6852079 | 2013-07-15 17:49:19 +0400 | [diff] [blame] | 6880 | * Compute the hierarchical load factor for cfs_rq and all its ascendants. |
Peter Zijlstra | 9763b67 | 2011-07-13 13:09:25 +0200 | [diff] [blame] | 6881 | * This needs to be done in a top-down fashion because the load of a child |
| 6882 | * group is a fraction of its parents load. |
| 6883 | */ |
Vladimir Davydov | 6852079 | 2013-07-15 17:49:19 +0400 | [diff] [blame] | 6884 | static void update_cfs_rq_h_load(struct cfs_rq *cfs_rq) |
Peter Zijlstra | 9763b67 | 2011-07-13 13:09:25 +0200 | [diff] [blame] | 6885 | { |
Vladimir Davydov | 6852079 | 2013-07-15 17:49:19 +0400 | [diff] [blame] | 6886 | struct rq *rq = rq_of(cfs_rq); |
| 6887 | struct sched_entity *se = cfs_rq->tg->se[cpu_of(rq)]; |
Peter Zijlstra | a35b646 | 2012-08-08 21:46:40 +0200 | [diff] [blame] | 6888 | unsigned long now = jiffies; |
Vladimir Davydov | 6852079 | 2013-07-15 17:49:19 +0400 | [diff] [blame] | 6889 | unsigned long load; |
Peter Zijlstra | a35b646 | 2012-08-08 21:46:40 +0200 | [diff] [blame] | 6890 | |
Vladimir Davydov | 6852079 | 2013-07-15 17:49:19 +0400 | [diff] [blame] | 6891 | if (cfs_rq->last_h_load_update == now) |
Peter Zijlstra | a35b646 | 2012-08-08 21:46:40 +0200 | [diff] [blame] | 6892 | return; |
| 6893 | |
Vladimir Davydov | 6852079 | 2013-07-15 17:49:19 +0400 | [diff] [blame] | 6894 | cfs_rq->h_load_next = NULL; |
| 6895 | for_each_sched_entity(se) { |
| 6896 | cfs_rq = cfs_rq_of(se); |
| 6897 | cfs_rq->h_load_next = se; |
| 6898 | if (cfs_rq->last_h_load_update == now) |
| 6899 | break; |
| 6900 | } |
Peter Zijlstra | a35b646 | 2012-08-08 21:46:40 +0200 | [diff] [blame] | 6901 | |
Vladimir Davydov | 6852079 | 2013-07-15 17:49:19 +0400 | [diff] [blame] | 6902 | if (!se) { |
Yuyang Du | 7ea241a | 2015-07-15 08:04:42 +0800 | [diff] [blame] | 6903 | cfs_rq->h_load = cfs_rq_load_avg(cfs_rq); |
Vladimir Davydov | 6852079 | 2013-07-15 17:49:19 +0400 | [diff] [blame] | 6904 | cfs_rq->last_h_load_update = now; |
| 6905 | } |
| 6906 | |
| 6907 | while ((se = cfs_rq->h_load_next) != NULL) { |
| 6908 | load = cfs_rq->h_load; |
Yuyang Du | 7ea241a | 2015-07-15 08:04:42 +0800 | [diff] [blame] | 6909 | load = div64_ul(load * se->avg.load_avg, |
| 6910 | cfs_rq_load_avg(cfs_rq) + 1); |
Vladimir Davydov | 6852079 | 2013-07-15 17:49:19 +0400 | [diff] [blame] | 6911 | cfs_rq = group_cfs_rq(se); |
| 6912 | cfs_rq->h_load = load; |
| 6913 | cfs_rq->last_h_load_update = now; |
| 6914 | } |
Peter Zijlstra | 9763b67 | 2011-07-13 13:09:25 +0200 | [diff] [blame] | 6915 | } |
| 6916 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 6917 | static unsigned long task_h_load(struct task_struct *p) |
Peter Zijlstra | 230059de | 2009-12-17 17:47:12 +0100 | [diff] [blame] | 6918 | { |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 6919 | struct cfs_rq *cfs_rq = task_cfs_rq(p); |
Peter Zijlstra | 230059de | 2009-12-17 17:47:12 +0100 | [diff] [blame] | 6920 | |
Vladimir Davydov | 6852079 | 2013-07-15 17:49:19 +0400 | [diff] [blame] | 6921 | update_cfs_rq_h_load(cfs_rq); |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 6922 | return div64_ul(p->se.avg.load_avg * cfs_rq->h_load, |
Yuyang Du | 7ea241a | 2015-07-15 08:04:42 +0800 | [diff] [blame] | 6923 | cfs_rq_load_avg(cfs_rq) + 1); |
Peter Zijlstra | 230059de | 2009-12-17 17:47:12 +0100 | [diff] [blame] | 6924 | } |
| 6925 | #else |
Paul Turner | 48a1675 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 6926 | static inline void update_blocked_averages(int cpu) |
Peter Zijlstra | 9e3081c | 2010-11-15 15:47:02 -0800 | [diff] [blame] | 6927 | { |
Vincent Guittot | 6c1d47c | 2015-07-15 08:04:38 +0800 | [diff] [blame] | 6928 | struct rq *rq = cpu_rq(cpu); |
| 6929 | struct cfs_rq *cfs_rq = &rq->cfs; |
| 6930 | unsigned long flags; |
| 6931 | |
| 6932 | raw_spin_lock_irqsave(&rq->lock, flags); |
| 6933 | update_rq_clock(rq); |
Steve Muckle | a2c6c91 | 2016-03-24 15:26:07 -0700 | [diff] [blame] | 6934 | update_cfs_rq_load_avg(cfs_rq_clock_task(cfs_rq), cfs_rq, true); |
Vincent Guittot | 6c1d47c | 2015-07-15 08:04:38 +0800 | [diff] [blame] | 6935 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
Peter Zijlstra | 9e3081c | 2010-11-15 15:47:02 -0800 | [diff] [blame] | 6936 | } |
| 6937 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 6938 | static unsigned long task_h_load(struct task_struct *p) |
| 6939 | { |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 6940 | return p->se.avg.load_avg; |
Peter Zijlstra | 230059de | 2009-12-17 17:47:12 +0100 | [diff] [blame] | 6941 | } |
| 6942 | #endif |
| 6943 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6944 | /********** Helpers for find_busiest_group ************************/ |
Rik van Riel | caeb178 | 2014-07-28 14:16:28 -0400 | [diff] [blame] | 6945 | |
| 6946 | enum group_type { |
| 6947 | group_other = 0, |
| 6948 | group_imbalanced, |
| 6949 | group_overloaded, |
| 6950 | }; |
| 6951 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6952 | /* |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6953 | * sg_lb_stats - stats of a sched_group required for load_balancing |
| 6954 | */ |
| 6955 | struct sg_lb_stats { |
| 6956 | unsigned long avg_load; /*Avg load across the CPUs of the group */ |
| 6957 | unsigned long group_load; /* Total load over the CPUs of the group */ |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6958 | unsigned long sum_weighted_load; /* Weighted load of group's tasks */ |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 6959 | unsigned long load_per_task; |
Nicolas Pitre | 63b2ca3 | 2014-05-26 18:19:37 -0400 | [diff] [blame] | 6960 | unsigned long group_capacity; |
Dietmar Eggemann | 9e91d61 | 2015-08-14 17:23:12 +0100 | [diff] [blame] | 6961 | unsigned long group_util; /* Total utilization of the group */ |
Peter Zijlstra | 147c5fc | 2013-08-19 15:22:57 +0200 | [diff] [blame] | 6962 | unsigned int sum_nr_running; /* Nr tasks running in the group */ |
Peter Zijlstra | 147c5fc | 2013-08-19 15:22:57 +0200 | [diff] [blame] | 6963 | unsigned int idle_cpus; |
| 6964 | unsigned int group_weight; |
Rik van Riel | caeb178 | 2014-07-28 14:16:28 -0400 | [diff] [blame] | 6965 | enum group_type group_type; |
Vincent Guittot | ea67821 | 2015-02-27 16:54:11 +0100 | [diff] [blame] | 6966 | int group_no_capacity; |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 6967 | #ifdef CONFIG_NUMA_BALANCING |
| 6968 | unsigned int nr_numa_running; |
| 6969 | unsigned int nr_preferred_running; |
| 6970 | #endif |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 6971 | }; |
| 6972 | |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 6973 | /* |
| 6974 | * sd_lb_stats - Structure to store the statistics of a sched_domain |
| 6975 | * during load balancing. |
| 6976 | */ |
| 6977 | struct sd_lb_stats { |
| 6978 | struct sched_group *busiest; /* Busiest group in this sd */ |
| 6979 | struct sched_group *local; /* Local group in this sd */ |
| 6980 | unsigned long total_load; /* Total load of all groups in sd */ |
Nicolas Pitre | 63b2ca3 | 2014-05-26 18:19:37 -0400 | [diff] [blame] | 6981 | unsigned long total_capacity; /* Total capacity of all groups in sd */ |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 6982 | unsigned long avg_load; /* Average load across all groups in sd */ |
| 6983 | |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 6984 | struct sg_lb_stats busiest_stat;/* Statistics of the busiest group */ |
Peter Zijlstra | 147c5fc | 2013-08-19 15:22:57 +0200 | [diff] [blame] | 6985 | struct sg_lb_stats local_stat; /* Statistics of the local group */ |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 6986 | }; |
| 6987 | |
Peter Zijlstra | 147c5fc | 2013-08-19 15:22:57 +0200 | [diff] [blame] | 6988 | static inline void init_sd_lb_stats(struct sd_lb_stats *sds) |
| 6989 | { |
| 6990 | /* |
| 6991 | * Skimp on the clearing to avoid duplicate work. We can avoid clearing |
| 6992 | * local_stat because update_sg_lb_stats() does a full clear/assignment. |
| 6993 | * We must however clear busiest_stat::avg_load because |
| 6994 | * update_sd_pick_busiest() reads this before assignment. |
| 6995 | */ |
| 6996 | *sds = (struct sd_lb_stats){ |
| 6997 | .busiest = NULL, |
| 6998 | .local = NULL, |
| 6999 | .total_load = 0UL, |
Nicolas Pitre | 63b2ca3 | 2014-05-26 18:19:37 -0400 | [diff] [blame] | 7000 | .total_capacity = 0UL, |
Peter Zijlstra | 147c5fc | 2013-08-19 15:22:57 +0200 | [diff] [blame] | 7001 | .busiest_stat = { |
| 7002 | .avg_load = 0UL, |
Rik van Riel | caeb178 | 2014-07-28 14:16:28 -0400 | [diff] [blame] | 7003 | .sum_nr_running = 0, |
| 7004 | .group_type = group_other, |
Peter Zijlstra | 147c5fc | 2013-08-19 15:22:57 +0200 | [diff] [blame] | 7005 | }, |
| 7006 | }; |
| 7007 | } |
| 7008 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7009 | /** |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7010 | * get_sd_load_idx - Obtain the load index for a given sched domain. |
| 7011 | * @sd: The sched_domain whose load_idx is to be obtained. |
Kamalesh Babulal | ed1b773 | 2013-10-13 23:06:15 +0530 | [diff] [blame] | 7012 | * @idle: The idle status of the CPU for whose sd load_idx is obtained. |
Yacine Belkadi | e69f618 | 2013-07-12 20:45:47 +0200 | [diff] [blame] | 7013 | * |
| 7014 | * Return: The load index. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7015 | */ |
| 7016 | static inline int get_sd_load_idx(struct sched_domain *sd, |
| 7017 | enum cpu_idle_type idle) |
| 7018 | { |
| 7019 | int load_idx; |
| 7020 | |
| 7021 | switch (idle) { |
| 7022 | case CPU_NOT_IDLE: |
| 7023 | load_idx = sd->busy_idx; |
| 7024 | break; |
| 7025 | |
| 7026 | case CPU_NEWLY_IDLE: |
| 7027 | load_idx = sd->newidle_idx; |
| 7028 | break; |
| 7029 | default: |
| 7030 | load_idx = sd->idle_idx; |
| 7031 | break; |
| 7032 | } |
| 7033 | |
| 7034 | return load_idx; |
| 7035 | } |
| 7036 | |
Nicolas Pitre | ced549f | 2014-05-26 18:19:38 -0400 | [diff] [blame] | 7037 | static unsigned long scale_rt_capacity(int cpu) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7038 | { |
| 7039 | struct rq *rq = cpu_rq(cpu); |
Vincent Guittot | b5b4860 | 2015-02-27 16:54:08 +0100 | [diff] [blame] | 7040 | u64 total, used, age_stamp, avg; |
Peter Zijlstra | cadefd3 | 2014-02-27 10:40:35 +0100 | [diff] [blame] | 7041 | s64 delta; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7042 | |
Peter Zijlstra | b654f7d | 2012-05-22 14:04:28 +0200 | [diff] [blame] | 7043 | /* |
| 7044 | * Since we're reading these variables without serialization make sure |
| 7045 | * we read them once before doing sanity checks on them. |
| 7046 | */ |
Jason Low | 316c1608d | 2015-04-28 13:00:20 -0700 | [diff] [blame] | 7047 | age_stamp = READ_ONCE(rq->age_stamp); |
| 7048 | avg = READ_ONCE(rq->rt_avg); |
Peter Zijlstra | cebde6d | 2015-01-05 11:18:10 +0100 | [diff] [blame] | 7049 | delta = __rq_clock_broken(rq) - age_stamp; |
Venkatesh Pallipadi | aa48380 | 2010-10-04 17:03:22 -0700 | [diff] [blame] | 7050 | |
Peter Zijlstra | cadefd3 | 2014-02-27 10:40:35 +0100 | [diff] [blame] | 7051 | if (unlikely(delta < 0)) |
| 7052 | delta = 0; |
| 7053 | |
| 7054 | total = sched_avg_period() + delta; |
Peter Zijlstra | b654f7d | 2012-05-22 14:04:28 +0200 | [diff] [blame] | 7055 | |
Vincent Guittot | b5b4860 | 2015-02-27 16:54:08 +0100 | [diff] [blame] | 7056 | used = div_u64(avg, total); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7057 | |
Vincent Guittot | b5b4860 | 2015-02-27 16:54:08 +0100 | [diff] [blame] | 7058 | if (likely(used < SCHED_CAPACITY_SCALE)) |
| 7059 | return SCHED_CAPACITY_SCALE - used; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7060 | |
Vincent Guittot | b5b4860 | 2015-02-27 16:54:08 +0100 | [diff] [blame] | 7061 | return 1; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7062 | } |
| 7063 | |
Nicolas Pitre | ced549f | 2014-05-26 18:19:38 -0400 | [diff] [blame] | 7064 | static void update_cpu_capacity(struct sched_domain *sd, int cpu) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7065 | { |
Morten Rasmussen | 8cd5601 | 2015-08-14 17:23:10 +0100 | [diff] [blame] | 7066 | unsigned long capacity = arch_scale_cpu_capacity(sd, cpu); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7067 | struct sched_group *sdg = sd->groups; |
| 7068 | |
Vincent Guittot | ca6d75e | 2015-02-27 16:54:09 +0100 | [diff] [blame] | 7069 | cpu_rq(cpu)->cpu_capacity_orig = capacity; |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 7070 | |
Nicolas Pitre | ced549f | 2014-05-26 18:19:38 -0400 | [diff] [blame] | 7071 | capacity *= scale_rt_capacity(cpu); |
Nicolas Pitre | ca8ce3d | 2014-05-26 18:19:39 -0400 | [diff] [blame] | 7072 | capacity >>= SCHED_CAPACITY_SHIFT; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7073 | |
Nicolas Pitre | ced549f | 2014-05-26 18:19:38 -0400 | [diff] [blame] | 7074 | if (!capacity) |
| 7075 | capacity = 1; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7076 | |
Nicolas Pitre | ced549f | 2014-05-26 18:19:38 -0400 | [diff] [blame] | 7077 | cpu_rq(cpu)->cpu_capacity = capacity; |
| 7078 | sdg->sgc->capacity = capacity; |
Morten Rasmussen | bf475ce | 2016-10-14 14:41:09 +0100 | [diff] [blame] | 7079 | sdg->sgc->min_capacity = capacity; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7080 | } |
| 7081 | |
Nicolas Pitre | 63b2ca3 | 2014-05-26 18:19:37 -0400 | [diff] [blame] | 7082 | void update_group_capacity(struct sched_domain *sd, int cpu) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7083 | { |
| 7084 | struct sched_domain *child = sd->child; |
| 7085 | struct sched_group *group, *sdg = sd->groups; |
Morten Rasmussen | bf475ce | 2016-10-14 14:41:09 +0100 | [diff] [blame] | 7086 | unsigned long capacity, min_capacity; |
Vincent Guittot | 4ec4412 | 2011-12-12 20:21:08 +0100 | [diff] [blame] | 7087 | unsigned long interval; |
| 7088 | |
| 7089 | interval = msecs_to_jiffies(sd->balance_interval); |
| 7090 | interval = clamp(interval, 1UL, max_load_balance_interval); |
Nicolas Pitre | 63b2ca3 | 2014-05-26 18:19:37 -0400 | [diff] [blame] | 7091 | sdg->sgc->next_update = jiffies + interval; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7092 | |
| 7093 | if (!child) { |
Nicolas Pitre | ced549f | 2014-05-26 18:19:38 -0400 | [diff] [blame] | 7094 | update_cpu_capacity(sd, cpu); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7095 | return; |
| 7096 | } |
| 7097 | |
Vincent Guittot | dc7ff76 | 2015-03-03 11:35:03 +0100 | [diff] [blame] | 7098 | capacity = 0; |
Morten Rasmussen | bf475ce | 2016-10-14 14:41:09 +0100 | [diff] [blame] | 7099 | min_capacity = ULONG_MAX; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7100 | |
Peter Zijlstra | 74a5ce2 | 2012-05-23 18:00:43 +0200 | [diff] [blame] | 7101 | if (child->flags & SD_OVERLAP) { |
| 7102 | /* |
| 7103 | * SD_OVERLAP domains cannot assume that child groups |
| 7104 | * span the current group. |
| 7105 | */ |
| 7106 | |
Peter Zijlstra | 863bffc | 2013-08-28 11:44:39 +0200 | [diff] [blame] | 7107 | for_each_cpu(cpu, sched_group_cpus(sdg)) { |
Nicolas Pitre | 63b2ca3 | 2014-05-26 18:19:37 -0400 | [diff] [blame] | 7108 | struct sched_group_capacity *sgc; |
Srikar Dronamraju | 9abf24d | 2013-11-12 22:11:26 +0530 | [diff] [blame] | 7109 | struct rq *rq = cpu_rq(cpu); |
Peter Zijlstra | 863bffc | 2013-08-28 11:44:39 +0200 | [diff] [blame] | 7110 | |
Srikar Dronamraju | 9abf24d | 2013-11-12 22:11:26 +0530 | [diff] [blame] | 7111 | /* |
Nicolas Pitre | 63b2ca3 | 2014-05-26 18:19:37 -0400 | [diff] [blame] | 7112 | * build_sched_domains() -> init_sched_groups_capacity() |
Srikar Dronamraju | 9abf24d | 2013-11-12 22:11:26 +0530 | [diff] [blame] | 7113 | * gets here before we've attached the domains to the |
| 7114 | * runqueues. |
| 7115 | * |
Nicolas Pitre | ced549f | 2014-05-26 18:19:38 -0400 | [diff] [blame] | 7116 | * Use capacity_of(), which is set irrespective of domains |
| 7117 | * in update_cpu_capacity(). |
Srikar Dronamraju | 9abf24d | 2013-11-12 22:11:26 +0530 | [diff] [blame] | 7118 | * |
Vincent Guittot | dc7ff76 | 2015-03-03 11:35:03 +0100 | [diff] [blame] | 7119 | * This avoids capacity from being 0 and |
Srikar Dronamraju | 9abf24d | 2013-11-12 22:11:26 +0530 | [diff] [blame] | 7120 | * causing divide-by-zero issues on boot. |
Srikar Dronamraju | 9abf24d | 2013-11-12 22:11:26 +0530 | [diff] [blame] | 7121 | */ |
| 7122 | if (unlikely(!rq->sd)) { |
Nicolas Pitre | ced549f | 2014-05-26 18:19:38 -0400 | [diff] [blame] | 7123 | capacity += capacity_of(cpu); |
Morten Rasmussen | bf475ce | 2016-10-14 14:41:09 +0100 | [diff] [blame] | 7124 | } else { |
| 7125 | sgc = rq->sd->groups->sgc; |
| 7126 | capacity += sgc->capacity; |
Srikar Dronamraju | 9abf24d | 2013-11-12 22:11:26 +0530 | [diff] [blame] | 7127 | } |
| 7128 | |
Morten Rasmussen | bf475ce | 2016-10-14 14:41:09 +0100 | [diff] [blame] | 7129 | min_capacity = min(capacity, min_capacity); |
Peter Zijlstra | 863bffc | 2013-08-28 11:44:39 +0200 | [diff] [blame] | 7130 | } |
Peter Zijlstra | 74a5ce2 | 2012-05-23 18:00:43 +0200 | [diff] [blame] | 7131 | } else { |
| 7132 | /* |
| 7133 | * !SD_OVERLAP domains can assume that child groups |
| 7134 | * span the current group. |
Byungchul Park | 97a7142 | 2015-07-05 18:33:48 +0900 | [diff] [blame] | 7135 | */ |
Peter Zijlstra | 74a5ce2 | 2012-05-23 18:00:43 +0200 | [diff] [blame] | 7136 | |
| 7137 | group = child->groups; |
| 7138 | do { |
Morten Rasmussen | bf475ce | 2016-10-14 14:41:09 +0100 | [diff] [blame] | 7139 | struct sched_group_capacity *sgc = group->sgc; |
| 7140 | |
| 7141 | capacity += sgc->capacity; |
| 7142 | min_capacity = min(sgc->min_capacity, min_capacity); |
Peter Zijlstra | 74a5ce2 | 2012-05-23 18:00:43 +0200 | [diff] [blame] | 7143 | group = group->next; |
| 7144 | } while (group != child->groups); |
| 7145 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7146 | |
Nicolas Pitre | 63b2ca3 | 2014-05-26 18:19:37 -0400 | [diff] [blame] | 7147 | sdg->sgc->capacity = capacity; |
Morten Rasmussen | bf475ce | 2016-10-14 14:41:09 +0100 | [diff] [blame] | 7148 | sdg->sgc->min_capacity = min_capacity; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7149 | } |
| 7150 | |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 7151 | /* |
Vincent Guittot | ea67821 | 2015-02-27 16:54:11 +0100 | [diff] [blame] | 7152 | * Check whether the capacity of the rq has been noticeably reduced by side |
| 7153 | * activity. The imbalance_pct is used for the threshold. |
| 7154 | * Return true is the capacity is reduced |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 7155 | */ |
| 7156 | static inline int |
Vincent Guittot | ea67821 | 2015-02-27 16:54:11 +0100 | [diff] [blame] | 7157 | check_cpu_capacity(struct rq *rq, struct sched_domain *sd) |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 7158 | { |
Vincent Guittot | ea67821 | 2015-02-27 16:54:11 +0100 | [diff] [blame] | 7159 | return ((rq->cpu_capacity * sd->imbalance_pct) < |
| 7160 | (rq->cpu_capacity_orig * 100)); |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 7161 | } |
| 7162 | |
Peter Zijlstra | 30ce5da | 2013-08-15 20:29:29 +0200 | [diff] [blame] | 7163 | /* |
| 7164 | * Group imbalance indicates (and tries to solve) the problem where balancing |
| 7165 | * groups is inadequate due to tsk_cpus_allowed() constraints. |
| 7166 | * |
| 7167 | * Imagine a situation of two groups of 4 cpus each and 4 tasks each with a |
| 7168 | * cpumask covering 1 cpu of the first group and 3 cpus of the second group. |
| 7169 | * Something like: |
| 7170 | * |
| 7171 | * { 0 1 2 3 } { 4 5 6 7 } |
| 7172 | * * * * * |
| 7173 | * |
| 7174 | * If we were to balance group-wise we'd place two tasks in the first group and |
| 7175 | * two tasks in the second group. Clearly this is undesired as it will overload |
| 7176 | * cpu 3 and leave one of the cpus in the second group unused. |
| 7177 | * |
| 7178 | * The current solution to this issue is detecting the skew in the first group |
Peter Zijlstra | 6263322 | 2013-08-19 12:41:09 +0200 | [diff] [blame] | 7179 | * by noticing the lower domain failed to reach balance and had difficulty |
| 7180 | * moving tasks due to affinity constraints. |
Peter Zijlstra | 30ce5da | 2013-08-15 20:29:29 +0200 | [diff] [blame] | 7181 | * |
| 7182 | * When this is so detected; this group becomes a candidate for busiest; see |
Kamalesh Babulal | ed1b773 | 2013-10-13 23:06:15 +0530 | [diff] [blame] | 7183 | * update_sd_pick_busiest(). And calculate_imbalance() and |
Peter Zijlstra | 6263322 | 2013-08-19 12:41:09 +0200 | [diff] [blame] | 7184 | * find_busiest_group() avoid some of the usual balance conditions to allow it |
Peter Zijlstra | 30ce5da | 2013-08-15 20:29:29 +0200 | [diff] [blame] | 7185 | * to create an effective group imbalance. |
| 7186 | * |
| 7187 | * This is a somewhat tricky proposition since the next run might not find the |
| 7188 | * group imbalance and decide the groups need to be balanced again. A most |
| 7189 | * subtle and fragile situation. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7190 | */ |
Peter Zijlstra | 30ce5da | 2013-08-15 20:29:29 +0200 | [diff] [blame] | 7191 | |
Peter Zijlstra | 6263322 | 2013-08-19 12:41:09 +0200 | [diff] [blame] | 7192 | static inline int sg_imbalanced(struct sched_group *group) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7193 | { |
Nicolas Pitre | 63b2ca3 | 2014-05-26 18:19:37 -0400 | [diff] [blame] | 7194 | return group->sgc->imbalance; |
Peter Zijlstra | 30ce5da | 2013-08-15 20:29:29 +0200 | [diff] [blame] | 7195 | } |
| 7196 | |
Peter Zijlstra | b37d931 | 2013-08-28 11:50:34 +0200 | [diff] [blame] | 7197 | /* |
Vincent Guittot | ea67821 | 2015-02-27 16:54:11 +0100 | [diff] [blame] | 7198 | * group_has_capacity returns true if the group has spare capacity that could |
| 7199 | * be used by some tasks. |
| 7200 | * We consider that a group has spare capacity if the * number of task is |
Dietmar Eggemann | 9e91d61 | 2015-08-14 17:23:12 +0100 | [diff] [blame] | 7201 | * smaller than the number of CPUs or if the utilization is lower than the |
| 7202 | * available capacity for CFS tasks. |
Vincent Guittot | ea67821 | 2015-02-27 16:54:11 +0100 | [diff] [blame] | 7203 | * For the latter, we use a threshold to stabilize the state, to take into |
| 7204 | * account the variance of the tasks' load and to return true if the available |
| 7205 | * capacity in meaningful for the load balancer. |
| 7206 | * As an example, an available capacity of 1% can appear but it doesn't make |
| 7207 | * any benefit for the load balance. |
Peter Zijlstra | b37d931 | 2013-08-28 11:50:34 +0200 | [diff] [blame] | 7208 | */ |
Vincent Guittot | ea67821 | 2015-02-27 16:54:11 +0100 | [diff] [blame] | 7209 | static inline bool |
| 7210 | group_has_capacity(struct lb_env *env, struct sg_lb_stats *sgs) |
Peter Zijlstra | b37d931 | 2013-08-28 11:50:34 +0200 | [diff] [blame] | 7211 | { |
Vincent Guittot | ea67821 | 2015-02-27 16:54:11 +0100 | [diff] [blame] | 7212 | if (sgs->sum_nr_running < sgs->group_weight) |
| 7213 | return true; |
Peter Zijlstra | b37d931 | 2013-08-28 11:50:34 +0200 | [diff] [blame] | 7214 | |
Vincent Guittot | ea67821 | 2015-02-27 16:54:11 +0100 | [diff] [blame] | 7215 | if ((sgs->group_capacity * 100) > |
Dietmar Eggemann | 9e91d61 | 2015-08-14 17:23:12 +0100 | [diff] [blame] | 7216 | (sgs->group_util * env->sd->imbalance_pct)) |
Vincent Guittot | ea67821 | 2015-02-27 16:54:11 +0100 | [diff] [blame] | 7217 | return true; |
Peter Zijlstra | b37d931 | 2013-08-28 11:50:34 +0200 | [diff] [blame] | 7218 | |
Vincent Guittot | ea67821 | 2015-02-27 16:54:11 +0100 | [diff] [blame] | 7219 | return false; |
Peter Zijlstra | b37d931 | 2013-08-28 11:50:34 +0200 | [diff] [blame] | 7220 | } |
| 7221 | |
Vincent Guittot | ea67821 | 2015-02-27 16:54:11 +0100 | [diff] [blame] | 7222 | /* |
| 7223 | * group_is_overloaded returns true if the group has more tasks than it can |
| 7224 | * handle. |
| 7225 | * group_is_overloaded is not equals to !group_has_capacity because a group |
| 7226 | * with the exact right number of tasks, has no more spare capacity but is not |
| 7227 | * overloaded so both group_has_capacity and group_is_overloaded return |
| 7228 | * false. |
| 7229 | */ |
| 7230 | static inline bool |
| 7231 | group_is_overloaded(struct lb_env *env, struct sg_lb_stats *sgs) |
Rik van Riel | caeb178 | 2014-07-28 14:16:28 -0400 | [diff] [blame] | 7232 | { |
Vincent Guittot | ea67821 | 2015-02-27 16:54:11 +0100 | [diff] [blame] | 7233 | if (sgs->sum_nr_running <= sgs->group_weight) |
| 7234 | return false; |
| 7235 | |
| 7236 | if ((sgs->group_capacity * 100) < |
Dietmar Eggemann | 9e91d61 | 2015-08-14 17:23:12 +0100 | [diff] [blame] | 7237 | (sgs->group_util * env->sd->imbalance_pct)) |
Vincent Guittot | ea67821 | 2015-02-27 16:54:11 +0100 | [diff] [blame] | 7238 | return true; |
| 7239 | |
| 7240 | return false; |
| 7241 | } |
| 7242 | |
Morten Rasmussen | 9e0994c | 2016-10-14 14:41:10 +0100 | [diff] [blame] | 7243 | /* |
| 7244 | * group_smaller_cpu_capacity: Returns true if sched_group sg has smaller |
| 7245 | * per-CPU capacity than sched_group ref. |
| 7246 | */ |
| 7247 | static inline bool |
| 7248 | group_smaller_cpu_capacity(struct sched_group *sg, struct sched_group *ref) |
| 7249 | { |
| 7250 | return sg->sgc->min_capacity * capacity_margin < |
| 7251 | ref->sgc->min_capacity * 1024; |
| 7252 | } |
| 7253 | |
Leo Yan | 79a89f9 | 2015-09-15 18:56:45 +0800 | [diff] [blame] | 7254 | static inline enum |
| 7255 | group_type group_classify(struct sched_group *group, |
| 7256 | struct sg_lb_stats *sgs) |
Vincent Guittot | ea67821 | 2015-02-27 16:54:11 +0100 | [diff] [blame] | 7257 | { |
| 7258 | if (sgs->group_no_capacity) |
Rik van Riel | caeb178 | 2014-07-28 14:16:28 -0400 | [diff] [blame] | 7259 | return group_overloaded; |
| 7260 | |
| 7261 | if (sg_imbalanced(group)) |
| 7262 | return group_imbalanced; |
| 7263 | |
| 7264 | return group_other; |
| 7265 | } |
| 7266 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7267 | /** |
| 7268 | * update_sg_lb_stats - Update sched_group's statistics for load balancing. |
| 7269 | * @env: The load balancing environment. |
| 7270 | * @group: sched_group whose statistics are to be updated. |
| 7271 | * @load_idx: Load index of sched_domain of this_cpu for load calc. |
| 7272 | * @local_group: Does group contain this_cpu. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7273 | * @sgs: variable to hold the statistics for this group. |
Masanari Iida | cd3bd4e | 2014-07-28 12:38:06 +0900 | [diff] [blame] | 7274 | * @overload: Indicate more than one runnable task for any CPU. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7275 | */ |
| 7276 | static inline void update_sg_lb_stats(struct lb_env *env, |
| 7277 | struct sched_group *group, int load_idx, |
Tim Chen | 4486edd | 2014-06-23 12:16:49 -0700 | [diff] [blame] | 7278 | int local_group, struct sg_lb_stats *sgs, |
| 7279 | bool *overload) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7280 | { |
Peter Zijlstra | 30ce5da | 2013-08-15 20:29:29 +0200 | [diff] [blame] | 7281 | unsigned long load; |
Waiman Long | a426f99 | 2015-11-25 14:09:38 -0500 | [diff] [blame] | 7282 | int i, nr_running; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7283 | |
Peter Zijlstra | b72ff13 | 2013-08-28 10:32:32 +0200 | [diff] [blame] | 7284 | memset(sgs, 0, sizeof(*sgs)); |
| 7285 | |
Michael Wang | b9403130 | 2012-07-12 16:10:13 +0800 | [diff] [blame] | 7286 | for_each_cpu_and(i, sched_group_cpus(group), env->cpus) { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7287 | struct rq *rq = cpu_rq(i); |
| 7288 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7289 | /* Bias balancing toward cpus of our domain */ |
Peter Zijlstra | 6263322 | 2013-08-19 12:41:09 +0200 | [diff] [blame] | 7290 | if (local_group) |
Peter Zijlstra | 04f733b | 2012-05-11 00:12:02 +0200 | [diff] [blame] | 7291 | load = target_load(i, load_idx); |
Peter Zijlstra | 6263322 | 2013-08-19 12:41:09 +0200 | [diff] [blame] | 7292 | else |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7293 | load = source_load(i, load_idx); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7294 | |
| 7295 | sgs->group_load += load; |
Dietmar Eggemann | 9e91d61 | 2015-08-14 17:23:12 +0100 | [diff] [blame] | 7296 | sgs->group_util += cpu_util(i); |
Vincent Guittot | 65fdac0 | 2014-08-26 13:06:46 +0200 | [diff] [blame] | 7297 | sgs->sum_nr_running += rq->cfs.h_nr_running; |
Tim Chen | 4486edd | 2014-06-23 12:16:49 -0700 | [diff] [blame] | 7298 | |
Waiman Long | a426f99 | 2015-11-25 14:09:38 -0500 | [diff] [blame] | 7299 | nr_running = rq->nr_running; |
| 7300 | if (nr_running > 1) |
Tim Chen | 4486edd | 2014-06-23 12:16:49 -0700 | [diff] [blame] | 7301 | *overload = true; |
| 7302 | |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 7303 | #ifdef CONFIG_NUMA_BALANCING |
| 7304 | sgs->nr_numa_running += rq->nr_numa_running; |
| 7305 | sgs->nr_preferred_running += rq->nr_preferred_running; |
| 7306 | #endif |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7307 | sgs->sum_weighted_load += weighted_cpuload(i); |
Waiman Long | a426f99 | 2015-11-25 14:09:38 -0500 | [diff] [blame] | 7308 | /* |
| 7309 | * No need to call idle_cpu() if nr_running is not 0 |
| 7310 | */ |
| 7311 | if (!nr_running && idle_cpu(i)) |
Suresh Siddha | aae6d3d | 2010-09-17 15:02:32 -0700 | [diff] [blame] | 7312 | sgs->idle_cpus++; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7313 | } |
| 7314 | |
Nicolas Pitre | 63b2ca3 | 2014-05-26 18:19:37 -0400 | [diff] [blame] | 7315 | /* Adjust by relative CPU capacity of the group */ |
| 7316 | sgs->group_capacity = group->sgc->capacity; |
Nicolas Pitre | ca8ce3d | 2014-05-26 18:19:39 -0400 | [diff] [blame] | 7317 | sgs->avg_load = (sgs->group_load*SCHED_CAPACITY_SCALE) / sgs->group_capacity; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7318 | |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 7319 | if (sgs->sum_nr_running) |
Peter Zijlstra | 38d0f77 | 2013-08-15 19:47:56 +0200 | [diff] [blame] | 7320 | sgs->load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7321 | |
Suresh Siddha | aae6d3d | 2010-09-17 15:02:32 -0700 | [diff] [blame] | 7322 | sgs->group_weight = group->group_weight; |
Peter Zijlstra | b37d931 | 2013-08-28 11:50:34 +0200 | [diff] [blame] | 7323 | |
Vincent Guittot | ea67821 | 2015-02-27 16:54:11 +0100 | [diff] [blame] | 7324 | sgs->group_no_capacity = group_is_overloaded(env, sgs); |
Leo Yan | 79a89f9 | 2015-09-15 18:56:45 +0800 | [diff] [blame] | 7325 | sgs->group_type = group_classify(group, sgs); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7326 | } |
| 7327 | |
| 7328 | /** |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 7329 | * update_sd_pick_busiest - return 1 on busiest group |
Randy Dunlap | cd96891 | 2012-06-08 13:18:33 -0700 | [diff] [blame] | 7330 | * @env: The load balancing environment. |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 7331 | * @sds: sched_domain statistics |
| 7332 | * @sg: sched_group candidate to be checked for being the busiest |
Michael Neuling | b6b1229 | 2010-06-10 12:06:21 +1000 | [diff] [blame] | 7333 | * @sgs: sched_group statistics |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 7334 | * |
| 7335 | * Determine if @sg is a busier group than the previously selected |
| 7336 | * busiest group. |
Yacine Belkadi | e69f618 | 2013-07-12 20:45:47 +0200 | [diff] [blame] | 7337 | * |
| 7338 | * Return: %true if @sg is a busier group than the previously selected |
| 7339 | * busiest group. %false otherwise. |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 7340 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 7341 | static bool update_sd_pick_busiest(struct lb_env *env, |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 7342 | struct sd_lb_stats *sds, |
| 7343 | struct sched_group *sg, |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 7344 | struct sg_lb_stats *sgs) |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 7345 | { |
Rik van Riel | caeb178 | 2014-07-28 14:16:28 -0400 | [diff] [blame] | 7346 | struct sg_lb_stats *busiest = &sds->busiest_stat; |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 7347 | |
Rik van Riel | caeb178 | 2014-07-28 14:16:28 -0400 | [diff] [blame] | 7348 | if (sgs->group_type > busiest->group_type) |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 7349 | return true; |
| 7350 | |
Rik van Riel | caeb178 | 2014-07-28 14:16:28 -0400 | [diff] [blame] | 7351 | if (sgs->group_type < busiest->group_type) |
| 7352 | return false; |
| 7353 | |
| 7354 | if (sgs->avg_load <= busiest->avg_load) |
| 7355 | return false; |
| 7356 | |
Morten Rasmussen | 9e0994c | 2016-10-14 14:41:10 +0100 | [diff] [blame] | 7357 | if (!(env->sd->flags & SD_ASYM_CPUCAPACITY)) |
| 7358 | goto asym_packing; |
| 7359 | |
| 7360 | /* |
| 7361 | * Candidate sg has no more than one task per CPU and |
| 7362 | * has higher per-CPU capacity. Migrating tasks to less |
| 7363 | * capable CPUs may harm throughput. Maximize throughput, |
| 7364 | * power/energy consequences are not considered. |
| 7365 | */ |
| 7366 | if (sgs->sum_nr_running <= sgs->group_weight && |
| 7367 | group_smaller_cpu_capacity(sds->local, sg)) |
| 7368 | return false; |
| 7369 | |
| 7370 | asym_packing: |
Rik van Riel | caeb178 | 2014-07-28 14:16:28 -0400 | [diff] [blame] | 7371 | /* This is the busiest node in its class. */ |
| 7372 | if (!(env->sd->flags & SD_ASYM_PACKING)) |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 7373 | return true; |
| 7374 | |
Srikar Dronamraju | 1f621e0 | 2016-04-06 18:47:40 +0530 | [diff] [blame] | 7375 | /* No ASYM_PACKING if target cpu is already busy */ |
| 7376 | if (env->idle == CPU_NOT_IDLE) |
| 7377 | return true; |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 7378 | /* |
| 7379 | * ASYM_PACKING needs to move all the work to the lowest |
| 7380 | * numbered CPUs in the group, therefore mark all groups |
| 7381 | * higher than ourself as busy. |
| 7382 | */ |
Rik van Riel | caeb178 | 2014-07-28 14:16:28 -0400 | [diff] [blame] | 7383 | if (sgs->sum_nr_running && env->dst_cpu < group_first_cpu(sg)) { |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 7384 | if (!sds->busiest) |
| 7385 | return true; |
| 7386 | |
Srikar Dronamraju | 1f621e0 | 2016-04-06 18:47:40 +0530 | [diff] [blame] | 7387 | /* Prefer to move from highest possible cpu's work */ |
| 7388 | if (group_first_cpu(sds->busiest) < group_first_cpu(sg)) |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 7389 | return true; |
| 7390 | } |
| 7391 | |
| 7392 | return false; |
| 7393 | } |
| 7394 | |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 7395 | #ifdef CONFIG_NUMA_BALANCING |
| 7396 | static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs) |
| 7397 | { |
| 7398 | if (sgs->sum_nr_running > sgs->nr_numa_running) |
| 7399 | return regular; |
| 7400 | if (sgs->sum_nr_running > sgs->nr_preferred_running) |
| 7401 | return remote; |
| 7402 | return all; |
| 7403 | } |
| 7404 | |
| 7405 | static inline enum fbq_type fbq_classify_rq(struct rq *rq) |
| 7406 | { |
| 7407 | if (rq->nr_running > rq->nr_numa_running) |
| 7408 | return regular; |
| 7409 | if (rq->nr_running > rq->nr_preferred_running) |
| 7410 | return remote; |
| 7411 | return all; |
| 7412 | } |
| 7413 | #else |
| 7414 | static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs) |
| 7415 | { |
| 7416 | return all; |
| 7417 | } |
| 7418 | |
| 7419 | static inline enum fbq_type fbq_classify_rq(struct rq *rq) |
| 7420 | { |
| 7421 | return regular; |
| 7422 | } |
| 7423 | #endif /* CONFIG_NUMA_BALANCING */ |
| 7424 | |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 7425 | /** |
Hui Kang | 461819a | 2011-10-11 23:00:59 -0400 | [diff] [blame] | 7426 | * update_sd_lb_stats - Update sched_domain's statistics for load balancing. |
Randy Dunlap | cd96891 | 2012-06-08 13:18:33 -0700 | [diff] [blame] | 7427 | * @env: The load balancing environment. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7428 | * @sds: variable to hold the statistics for this sched_domain. |
| 7429 | */ |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 7430 | static inline void update_sd_lb_stats(struct lb_env *env, struct sd_lb_stats *sds) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7431 | { |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 7432 | struct sched_domain *child = env->sd->child; |
| 7433 | struct sched_group *sg = env->sd->groups; |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 7434 | struct sg_lb_stats tmp_sgs; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7435 | int load_idx, prefer_sibling = 0; |
Tim Chen | 4486edd | 2014-06-23 12:16:49 -0700 | [diff] [blame] | 7436 | bool overload = false; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7437 | |
| 7438 | if (child && child->flags & SD_PREFER_SIBLING) |
| 7439 | prefer_sibling = 1; |
| 7440 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 7441 | load_idx = get_sd_load_idx(env->sd, env->idle); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7442 | |
| 7443 | do { |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 7444 | struct sg_lb_stats *sgs = &tmp_sgs; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7445 | int local_group; |
| 7446 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 7447 | local_group = cpumask_test_cpu(env->dst_cpu, sched_group_cpus(sg)); |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 7448 | if (local_group) { |
| 7449 | sds->local = sg; |
| 7450 | sgs = &sds->local_stat; |
Peter Zijlstra | b72ff13 | 2013-08-28 10:32:32 +0200 | [diff] [blame] | 7451 | |
| 7452 | if (env->idle != CPU_NEWLY_IDLE || |
Nicolas Pitre | 63b2ca3 | 2014-05-26 18:19:37 -0400 | [diff] [blame] | 7453 | time_after_eq(jiffies, sg->sgc->next_update)) |
| 7454 | update_group_capacity(env->sd, env->dst_cpu); |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 7455 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7456 | |
Tim Chen | 4486edd | 2014-06-23 12:16:49 -0700 | [diff] [blame] | 7457 | update_sg_lb_stats(env, sg, load_idx, local_group, sgs, |
| 7458 | &overload); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7459 | |
Peter Zijlstra | b72ff13 | 2013-08-28 10:32:32 +0200 | [diff] [blame] | 7460 | if (local_group) |
| 7461 | goto next_group; |
| 7462 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7463 | /* |
| 7464 | * In case the child domain prefers tasks go to siblings |
Vincent Guittot | ea67821 | 2015-02-27 16:54:11 +0100 | [diff] [blame] | 7465 | * first, lower the sg capacity so that we'll try |
Nikhil Rao | 75dd321 | 2010-10-15 13:12:30 -0700 | [diff] [blame] | 7466 | * and move all the excess tasks away. We lower the capacity |
| 7467 | * of a group only if the local group has the capacity to fit |
Vincent Guittot | ea67821 | 2015-02-27 16:54:11 +0100 | [diff] [blame] | 7468 | * these excess tasks. The extra check prevents the case where |
| 7469 | * you always pull from the heaviest group when it is already |
| 7470 | * under-utilized (possible with a large weight task outweighs |
| 7471 | * the tasks on the system). |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7472 | */ |
Peter Zijlstra | b72ff13 | 2013-08-28 10:32:32 +0200 | [diff] [blame] | 7473 | if (prefer_sibling && sds->local && |
Vincent Guittot | ea67821 | 2015-02-27 16:54:11 +0100 | [diff] [blame] | 7474 | group_has_capacity(env, &sds->local_stat) && |
| 7475 | (sgs->sum_nr_running > 1)) { |
| 7476 | sgs->group_no_capacity = 1; |
Leo Yan | 79a89f9 | 2015-09-15 18:56:45 +0800 | [diff] [blame] | 7477 | sgs->group_type = group_classify(sg, sgs); |
Wanpeng Li | cb0b9f2 | 2014-11-05 07:44:50 +0800 | [diff] [blame] | 7478 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7479 | |
Peter Zijlstra | b72ff13 | 2013-08-28 10:32:32 +0200 | [diff] [blame] | 7480 | if (update_sd_pick_busiest(env, sds, sg, sgs)) { |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 7481 | sds->busiest = sg; |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 7482 | sds->busiest_stat = *sgs; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7483 | } |
| 7484 | |
Peter Zijlstra | b72ff13 | 2013-08-28 10:32:32 +0200 | [diff] [blame] | 7485 | next_group: |
| 7486 | /* Now, start updating sd_lb_stats */ |
| 7487 | sds->total_load += sgs->group_load; |
Nicolas Pitre | 63b2ca3 | 2014-05-26 18:19:37 -0400 | [diff] [blame] | 7488 | sds->total_capacity += sgs->group_capacity; |
Peter Zijlstra | b72ff13 | 2013-08-28 10:32:32 +0200 | [diff] [blame] | 7489 | |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 7490 | sg = sg->next; |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 7491 | } while (sg != env->sd->groups); |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 7492 | |
| 7493 | if (env->sd->flags & SD_NUMA) |
| 7494 | env->fbq_type = fbq_classify_group(&sds->busiest_stat); |
Tim Chen | 4486edd | 2014-06-23 12:16:49 -0700 | [diff] [blame] | 7495 | |
| 7496 | if (!env->sd->parent) { |
| 7497 | /* update overload indicator if we are at root domain */ |
| 7498 | if (env->dst_rq->rd->overload != overload) |
| 7499 | env->dst_rq->rd->overload = overload; |
| 7500 | } |
| 7501 | |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 7502 | } |
| 7503 | |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 7504 | /** |
| 7505 | * check_asym_packing - Check to see if the group is packed into the |
| 7506 | * sched doman. |
| 7507 | * |
| 7508 | * This is primarily intended to used at the sibling level. Some |
| 7509 | * cores like POWER7 prefer to use lower numbered SMT threads. In the |
| 7510 | * case of POWER7, it can move to lower SMT modes only when higher |
| 7511 | * threads are idle. When in lower SMT modes, the threads will |
| 7512 | * perform better since they share less core resources. Hence when we |
| 7513 | * have idle threads, we want them to be the higher ones. |
| 7514 | * |
| 7515 | * This packing function is run on idle threads. It checks to see if |
| 7516 | * the busiest CPU in this domain (core in the P7 case) has a higher |
| 7517 | * CPU number than the packing function is being run on. Here we are |
| 7518 | * assuming lower CPU number will be equivalent to lower a SMT thread |
| 7519 | * number. |
| 7520 | * |
Yacine Belkadi | e69f618 | 2013-07-12 20:45:47 +0200 | [diff] [blame] | 7521 | * Return: 1 when packing is required and a task should be moved to |
Michael Neuling | b6b1229 | 2010-06-10 12:06:21 +1000 | [diff] [blame] | 7522 | * this CPU. The amount of the imbalance is returned in *imbalance. |
| 7523 | * |
Randy Dunlap | cd96891 | 2012-06-08 13:18:33 -0700 | [diff] [blame] | 7524 | * @env: The load balancing environment. |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 7525 | * @sds: Statistics of the sched_domain which is to be packed |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 7526 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 7527 | static int check_asym_packing(struct lb_env *env, struct sd_lb_stats *sds) |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 7528 | { |
| 7529 | int busiest_cpu; |
| 7530 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 7531 | if (!(env->sd->flags & SD_ASYM_PACKING)) |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 7532 | return 0; |
| 7533 | |
Srikar Dronamraju | 1f621e0 | 2016-04-06 18:47:40 +0530 | [diff] [blame] | 7534 | if (env->idle == CPU_NOT_IDLE) |
| 7535 | return 0; |
| 7536 | |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 7537 | if (!sds->busiest) |
| 7538 | return 0; |
| 7539 | |
| 7540 | busiest_cpu = group_first_cpu(sds->busiest); |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 7541 | if (env->dst_cpu > busiest_cpu) |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 7542 | return 0; |
| 7543 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 7544 | env->imbalance = DIV_ROUND_CLOSEST( |
Nicolas Pitre | 63b2ca3 | 2014-05-26 18:19:37 -0400 | [diff] [blame] | 7545 | sds->busiest_stat.avg_load * sds->busiest_stat.group_capacity, |
Nicolas Pitre | ca8ce3d | 2014-05-26 18:19:39 -0400 | [diff] [blame] | 7546 | SCHED_CAPACITY_SCALE); |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 7547 | |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 7548 | return 1; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7549 | } |
| 7550 | |
| 7551 | /** |
| 7552 | * fix_small_imbalance - Calculate the minor imbalance that exists |
| 7553 | * amongst the groups of a sched_domain, during |
| 7554 | * load balancing. |
Randy Dunlap | cd96891 | 2012-06-08 13:18:33 -0700 | [diff] [blame] | 7555 | * @env: The load balancing environment. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7556 | * @sds: Statistics of the sched_domain whose imbalance is to be calculated. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7557 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 7558 | static inline |
| 7559 | void fix_small_imbalance(struct lb_env *env, struct sd_lb_stats *sds) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7560 | { |
Nicolas Pitre | 63b2ca3 | 2014-05-26 18:19:37 -0400 | [diff] [blame] | 7561 | unsigned long tmp, capa_now = 0, capa_move = 0; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7562 | unsigned int imbn = 2; |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 7563 | unsigned long scaled_busy_load_per_task; |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 7564 | struct sg_lb_stats *local, *busiest; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7565 | |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 7566 | local = &sds->local_stat; |
| 7567 | busiest = &sds->busiest_stat; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7568 | |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 7569 | if (!local->sum_nr_running) |
| 7570 | local->load_per_task = cpu_avg_load_per_task(env->dst_cpu); |
| 7571 | else if (busiest->load_per_task > local->load_per_task) |
| 7572 | imbn = 1; |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 7573 | |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 7574 | scaled_busy_load_per_task = |
Nicolas Pitre | ca8ce3d | 2014-05-26 18:19:39 -0400 | [diff] [blame] | 7575 | (busiest->load_per_task * SCHED_CAPACITY_SCALE) / |
Nicolas Pitre | 63b2ca3 | 2014-05-26 18:19:37 -0400 | [diff] [blame] | 7576 | busiest->group_capacity; |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 7577 | |
Vladimir Davydov | 3029ede | 2013-09-15 17:49:14 +0400 | [diff] [blame] | 7578 | if (busiest->avg_load + scaled_busy_load_per_task >= |
| 7579 | local->avg_load + (scaled_busy_load_per_task * imbn)) { |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 7580 | env->imbalance = busiest->load_per_task; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7581 | return; |
| 7582 | } |
| 7583 | |
| 7584 | /* |
| 7585 | * OK, we don't have enough imbalance to justify moving tasks, |
Nicolas Pitre | ced549f | 2014-05-26 18:19:38 -0400 | [diff] [blame] | 7586 | * however we may be able to increase total CPU capacity used by |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7587 | * moving them. |
| 7588 | */ |
| 7589 | |
Nicolas Pitre | 63b2ca3 | 2014-05-26 18:19:37 -0400 | [diff] [blame] | 7590 | capa_now += busiest->group_capacity * |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 7591 | min(busiest->load_per_task, busiest->avg_load); |
Nicolas Pitre | 63b2ca3 | 2014-05-26 18:19:37 -0400 | [diff] [blame] | 7592 | capa_now += local->group_capacity * |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 7593 | min(local->load_per_task, local->avg_load); |
Nicolas Pitre | ca8ce3d | 2014-05-26 18:19:39 -0400 | [diff] [blame] | 7594 | capa_now /= SCHED_CAPACITY_SCALE; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7595 | |
| 7596 | /* Amount of load we'd subtract */ |
Vincent Guittot | a2cd426 | 2014-03-11 17:26:06 +0100 | [diff] [blame] | 7597 | if (busiest->avg_load > scaled_busy_load_per_task) { |
Nicolas Pitre | 63b2ca3 | 2014-05-26 18:19:37 -0400 | [diff] [blame] | 7598 | capa_move += busiest->group_capacity * |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 7599 | min(busiest->load_per_task, |
Vincent Guittot | a2cd426 | 2014-03-11 17:26:06 +0100 | [diff] [blame] | 7600 | busiest->avg_load - scaled_busy_load_per_task); |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 7601 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7602 | |
| 7603 | /* Amount of load we'd add */ |
Nicolas Pitre | 63b2ca3 | 2014-05-26 18:19:37 -0400 | [diff] [blame] | 7604 | if (busiest->avg_load * busiest->group_capacity < |
Nicolas Pitre | ca8ce3d | 2014-05-26 18:19:39 -0400 | [diff] [blame] | 7605 | busiest->load_per_task * SCHED_CAPACITY_SCALE) { |
Nicolas Pitre | 63b2ca3 | 2014-05-26 18:19:37 -0400 | [diff] [blame] | 7606 | tmp = (busiest->avg_load * busiest->group_capacity) / |
| 7607 | local->group_capacity; |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 7608 | } else { |
Nicolas Pitre | ca8ce3d | 2014-05-26 18:19:39 -0400 | [diff] [blame] | 7609 | tmp = (busiest->load_per_task * SCHED_CAPACITY_SCALE) / |
Nicolas Pitre | 63b2ca3 | 2014-05-26 18:19:37 -0400 | [diff] [blame] | 7610 | local->group_capacity; |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 7611 | } |
Nicolas Pitre | 63b2ca3 | 2014-05-26 18:19:37 -0400 | [diff] [blame] | 7612 | capa_move += local->group_capacity * |
Peter Zijlstra | 3ae11c9 | 2013-08-15 20:37:48 +0200 | [diff] [blame] | 7613 | min(local->load_per_task, local->avg_load + tmp); |
Nicolas Pitre | ca8ce3d | 2014-05-26 18:19:39 -0400 | [diff] [blame] | 7614 | capa_move /= SCHED_CAPACITY_SCALE; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7615 | |
| 7616 | /* Move if we gain throughput */ |
Nicolas Pitre | 63b2ca3 | 2014-05-26 18:19:37 -0400 | [diff] [blame] | 7617 | if (capa_move > capa_now) |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 7618 | env->imbalance = busiest->load_per_task; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7619 | } |
| 7620 | |
| 7621 | /** |
| 7622 | * calculate_imbalance - Calculate the amount of imbalance present within the |
| 7623 | * groups of a given sched_domain during load balance. |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 7624 | * @env: load balance environment |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7625 | * @sds: statistics of the sched_domain whose imbalance is to be calculated. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7626 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 7627 | static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *sds) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7628 | { |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 7629 | unsigned long max_pull, load_above_capacity = ~0UL; |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 7630 | struct sg_lb_stats *local, *busiest; |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 7631 | |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 7632 | local = &sds->local_stat; |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 7633 | busiest = &sds->busiest_stat; |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 7634 | |
Rik van Riel | caeb178 | 2014-07-28 14:16:28 -0400 | [diff] [blame] | 7635 | if (busiest->group_type == group_imbalanced) { |
Peter Zijlstra | 30ce5da | 2013-08-15 20:29:29 +0200 | [diff] [blame] | 7636 | /* |
| 7637 | * In the group_imb case we cannot rely on group-wide averages |
| 7638 | * to ensure cpu-load equilibrium, look at wider averages. XXX |
| 7639 | */ |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 7640 | busiest->load_per_task = |
| 7641 | min(busiest->load_per_task, sds->avg_load); |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 7642 | } |
| 7643 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7644 | /* |
Dietmar Eggemann | 885e542 | 2016-04-29 20:32:39 +0100 | [diff] [blame] | 7645 | * Avg load of busiest sg can be less and avg load of local sg can |
| 7646 | * be greater than avg load across all sgs of sd because avg load |
| 7647 | * factors in sg capacity and sgs with smaller group_type are |
| 7648 | * skipped when updating the busiest sg: |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7649 | */ |
Vladimir Davydov | b188555 | 2013-09-15 17:49:13 +0400 | [diff] [blame] | 7650 | if (busiest->avg_load <= sds->avg_load || |
| 7651 | local->avg_load >= sds->avg_load) { |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 7652 | env->imbalance = 0; |
| 7653 | return fix_small_imbalance(env, sds); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7654 | } |
| 7655 | |
Peter Zijlstra | 9a5d9ba | 2014-07-29 17:15:11 +0200 | [diff] [blame] | 7656 | /* |
| 7657 | * If there aren't any idle cpus, avoid creating some. |
| 7658 | */ |
| 7659 | if (busiest->group_type == group_overloaded && |
| 7660 | local->group_type == group_overloaded) { |
Peter Zijlstra | 1be0eb2 | 2016-05-06 12:21:23 +0200 | [diff] [blame] | 7661 | load_above_capacity = busiest->sum_nr_running * SCHED_CAPACITY_SCALE; |
Morten Rasmussen | cfa1033 | 2016-04-29 20:32:40 +0100 | [diff] [blame] | 7662 | if (load_above_capacity > busiest->group_capacity) { |
Vincent Guittot | ea67821 | 2015-02-27 16:54:11 +0100 | [diff] [blame] | 7663 | load_above_capacity -= busiest->group_capacity; |
Dietmar Eggemann | 2665621 | 2016-08-10 11:27:27 +0100 | [diff] [blame] | 7664 | load_above_capacity *= scale_load_down(NICE_0_LOAD); |
Morten Rasmussen | cfa1033 | 2016-04-29 20:32:40 +0100 | [diff] [blame] | 7665 | load_above_capacity /= busiest->group_capacity; |
| 7666 | } else |
Vincent Guittot | ea67821 | 2015-02-27 16:54:11 +0100 | [diff] [blame] | 7667 | load_above_capacity = ~0UL; |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 7668 | } |
| 7669 | |
| 7670 | /* |
| 7671 | * We're trying to get all the cpus to the average_load, so we don't |
| 7672 | * want to push ourselves above the average load, nor do we wish to |
| 7673 | * reduce the max loaded cpu below the average load. At the same time, |
Dietmar Eggemann | 0a9b23c | 2016-04-29 20:32:38 +0100 | [diff] [blame] | 7674 | * we also don't want to reduce the group load below the group |
| 7675 | * capacity. Thus we look for the minimum possible imbalance. |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 7676 | */ |
Peter Zijlstra | 30ce5da | 2013-08-15 20:29:29 +0200 | [diff] [blame] | 7677 | max_pull = min(busiest->avg_load - sds->avg_load, load_above_capacity); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7678 | |
| 7679 | /* How much load to actually move to equalise the imbalance */ |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 7680 | env->imbalance = min( |
Nicolas Pitre | 63b2ca3 | 2014-05-26 18:19:37 -0400 | [diff] [blame] | 7681 | max_pull * busiest->group_capacity, |
| 7682 | (sds->avg_load - local->avg_load) * local->group_capacity |
Nicolas Pitre | ca8ce3d | 2014-05-26 18:19:39 -0400 | [diff] [blame] | 7683 | ) / SCHED_CAPACITY_SCALE; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7684 | |
| 7685 | /* |
| 7686 | * if *imbalance is less than the average load per runnable task |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 7687 | * there is no guarantee that any tasks will be moved so we'll have |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7688 | * a think about bumping its value to force at least one task to be |
| 7689 | * moved |
| 7690 | */ |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 7691 | if (env->imbalance < busiest->load_per_task) |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 7692 | return fix_small_imbalance(env, sds); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7693 | } |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 7694 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7695 | /******* find_busiest_group() helpers end here *********************/ |
| 7696 | |
| 7697 | /** |
| 7698 | * find_busiest_group - Returns the busiest group within the sched_domain |
Dietmar Eggemann | 0a9b23c | 2016-04-29 20:32:38 +0100 | [diff] [blame] | 7699 | * if there is an imbalance. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7700 | * |
| 7701 | * Also calculates the amount of weighted load which should be moved |
| 7702 | * to restore balance. |
| 7703 | * |
Randy Dunlap | cd96891 | 2012-06-08 13:18:33 -0700 | [diff] [blame] | 7704 | * @env: The load balancing environment. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7705 | * |
Yacine Belkadi | e69f618 | 2013-07-12 20:45:47 +0200 | [diff] [blame] | 7706 | * Return: - The busiest group if imbalance exists. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7707 | */ |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 7708 | static struct sched_group *find_busiest_group(struct lb_env *env) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7709 | { |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 7710 | struct sg_lb_stats *local, *busiest; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7711 | struct sd_lb_stats sds; |
| 7712 | |
Peter Zijlstra | 147c5fc | 2013-08-19 15:22:57 +0200 | [diff] [blame] | 7713 | init_sd_lb_stats(&sds); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7714 | |
| 7715 | /* |
| 7716 | * Compute the various statistics relavent for load balancing at |
| 7717 | * this level. |
| 7718 | */ |
Joonsoo Kim | 23f0d20 | 2013-08-06 17:36:42 +0900 | [diff] [blame] | 7719 | update_sd_lb_stats(env, &sds); |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 7720 | local = &sds.local_stat; |
| 7721 | busiest = &sds.busiest_stat; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7722 | |
Vincent Guittot | ea67821 | 2015-02-27 16:54:11 +0100 | [diff] [blame] | 7723 | /* ASYM feature bypasses nice load balance check */ |
Srikar Dronamraju | 1f621e0 | 2016-04-06 18:47:40 +0530 | [diff] [blame] | 7724 | if (check_asym_packing(env, &sds)) |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 7725 | return sds.busiest; |
| 7726 | |
Peter Zijlstra | cc57aa8 | 2011-02-21 18:55:32 +0100 | [diff] [blame] | 7727 | /* There is no busy sibling group to pull tasks from */ |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 7728 | if (!sds.busiest || busiest->sum_nr_running == 0) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7729 | goto out_balanced; |
| 7730 | |
Nicolas Pitre | ca8ce3d | 2014-05-26 18:19:39 -0400 | [diff] [blame] | 7731 | sds.avg_load = (SCHED_CAPACITY_SCALE * sds.total_load) |
| 7732 | / sds.total_capacity; |
Ken Chen | b0432d8 | 2011-04-07 17:23:22 -0700 | [diff] [blame] | 7733 | |
Peter Zijlstra | 866ab43 | 2011-02-21 18:56:47 +0100 | [diff] [blame] | 7734 | /* |
| 7735 | * If the busiest group is imbalanced the below checks don't |
Peter Zijlstra | 30ce5da | 2013-08-15 20:29:29 +0200 | [diff] [blame] | 7736 | * work because they assume all things are equal, which typically |
Peter Zijlstra | 866ab43 | 2011-02-21 18:56:47 +0100 | [diff] [blame] | 7737 | * isn't true due to cpus_allowed constraints and the like. |
| 7738 | */ |
Rik van Riel | caeb178 | 2014-07-28 14:16:28 -0400 | [diff] [blame] | 7739 | if (busiest->group_type == group_imbalanced) |
Peter Zijlstra | 866ab43 | 2011-02-21 18:56:47 +0100 | [diff] [blame] | 7740 | goto force_balance; |
| 7741 | |
Peter Zijlstra | cc57aa8 | 2011-02-21 18:55:32 +0100 | [diff] [blame] | 7742 | /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */ |
Vincent Guittot | ea67821 | 2015-02-27 16:54:11 +0100 | [diff] [blame] | 7743 | if (env->idle == CPU_NEWLY_IDLE && group_has_capacity(env, local) && |
| 7744 | busiest->group_no_capacity) |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 7745 | goto force_balance; |
| 7746 | |
Peter Zijlstra | cc57aa8 | 2011-02-21 18:55:32 +0100 | [diff] [blame] | 7747 | /* |
Zhihui Zhang | 9c58c79 | 2014-09-20 21:24:36 -0400 | [diff] [blame] | 7748 | * If the local group is busier than the selected busiest group |
Peter Zijlstra | cc57aa8 | 2011-02-21 18:55:32 +0100 | [diff] [blame] | 7749 | * don't try and pull any tasks. |
| 7750 | */ |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 7751 | if (local->avg_load >= busiest->avg_load) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7752 | goto out_balanced; |
| 7753 | |
Peter Zijlstra | cc57aa8 | 2011-02-21 18:55:32 +0100 | [diff] [blame] | 7754 | /* |
| 7755 | * Don't pull any tasks if this group is already above the domain |
| 7756 | * average load. |
| 7757 | */ |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 7758 | if (local->avg_load >= sds.avg_load) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7759 | goto out_balanced; |
| 7760 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 7761 | if (env->idle == CPU_IDLE) { |
Suresh Siddha | aae6d3d | 2010-09-17 15:02:32 -0700 | [diff] [blame] | 7762 | /* |
Vincent Guittot | 43f4d66 | 2014-10-01 15:38:55 +0200 | [diff] [blame] | 7763 | * This cpu is idle. If the busiest group is not overloaded |
| 7764 | * and there is no imbalance between this and busiest group |
| 7765 | * wrt idle cpus, it is balanced. The imbalance becomes |
| 7766 | * significant if the diff is greater than 1 otherwise we |
| 7767 | * might end up to just move the imbalance on another group |
Suresh Siddha | aae6d3d | 2010-09-17 15:02:32 -0700 | [diff] [blame] | 7768 | */ |
Vincent Guittot | 43f4d66 | 2014-10-01 15:38:55 +0200 | [diff] [blame] | 7769 | if ((busiest->group_type != group_overloaded) && |
| 7770 | (local->idle_cpus <= (busiest->idle_cpus + 1))) |
Suresh Siddha | aae6d3d | 2010-09-17 15:02:32 -0700 | [diff] [blame] | 7771 | goto out_balanced; |
Peter Zijlstra | c186faf | 2011-02-21 18:52:53 +0100 | [diff] [blame] | 7772 | } else { |
| 7773 | /* |
| 7774 | * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use |
| 7775 | * imbalance_pct to be conservative. |
| 7776 | */ |
Joonsoo Kim | 56cf515 | 2013-08-06 17:36:43 +0900 | [diff] [blame] | 7777 | if (100 * busiest->avg_load <= |
| 7778 | env->sd->imbalance_pct * local->avg_load) |
Peter Zijlstra | c186faf | 2011-02-21 18:52:53 +0100 | [diff] [blame] | 7779 | goto out_balanced; |
Suresh Siddha | aae6d3d | 2010-09-17 15:02:32 -0700 | [diff] [blame] | 7780 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7781 | |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 7782 | force_balance: |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7783 | /* Looks like there is an imbalance. Compute it */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 7784 | calculate_imbalance(env, &sds); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7785 | return sds.busiest; |
| 7786 | |
| 7787 | out_balanced: |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 7788 | env->imbalance = 0; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7789 | return NULL; |
| 7790 | } |
| 7791 | |
| 7792 | /* |
| 7793 | * find_busiest_queue - find the busiest runqueue among the cpus in group. |
| 7794 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 7795 | static struct rq *find_busiest_queue(struct lb_env *env, |
Michael Wang | b9403130 | 2012-07-12 16:10:13 +0800 | [diff] [blame] | 7796 | struct sched_group *group) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7797 | { |
| 7798 | struct rq *busiest = NULL, *rq; |
Nicolas Pitre | ced549f | 2014-05-26 18:19:38 -0400 | [diff] [blame] | 7799 | unsigned long busiest_load = 0, busiest_capacity = 1; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7800 | int i; |
| 7801 | |
Peter Zijlstra | 6906a40 | 2013-08-19 15:20:21 +0200 | [diff] [blame] | 7802 | for_each_cpu_and(i, sched_group_cpus(group), env->cpus) { |
Vincent Guittot | ea67821 | 2015-02-27 16:54:11 +0100 | [diff] [blame] | 7803 | unsigned long capacity, wl; |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 7804 | enum fbq_type rt; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7805 | |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 7806 | rq = cpu_rq(i); |
| 7807 | rt = fbq_classify_rq(rq); |
| 7808 | |
| 7809 | /* |
| 7810 | * We classify groups/runqueues into three groups: |
| 7811 | * - regular: there are !numa tasks |
| 7812 | * - remote: there are numa tasks that run on the 'wrong' node |
| 7813 | * - all: there is no distinction |
| 7814 | * |
| 7815 | * In order to avoid migrating ideally placed numa tasks, |
| 7816 | * ignore those when there's better options. |
| 7817 | * |
| 7818 | * If we ignore the actual busiest queue to migrate another |
| 7819 | * task, the next balance pass can still reduce the busiest |
| 7820 | * queue by moving tasks around inside the node. |
| 7821 | * |
| 7822 | * If we cannot move enough load due to this classification |
| 7823 | * the next pass will adjust the group classification and |
| 7824 | * allow migration of more tasks. |
| 7825 | * |
| 7826 | * Both cases only affect the total convergence complexity. |
| 7827 | */ |
| 7828 | if (rt > env->fbq_type) |
| 7829 | continue; |
| 7830 | |
Nicolas Pitre | ced549f | 2014-05-26 18:19:38 -0400 | [diff] [blame] | 7831 | capacity = capacity_of(i); |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 7832 | |
Thomas Gleixner | 6e40f5b | 2010-02-16 16:48:56 +0100 | [diff] [blame] | 7833 | wl = weighted_cpuload(i); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7834 | |
Thomas Gleixner | 6e40f5b | 2010-02-16 16:48:56 +0100 | [diff] [blame] | 7835 | /* |
| 7836 | * When comparing with imbalance, use weighted_cpuload() |
Nicolas Pitre | ced549f | 2014-05-26 18:19:38 -0400 | [diff] [blame] | 7837 | * which is not scaled with the cpu capacity. |
Thomas Gleixner | 6e40f5b | 2010-02-16 16:48:56 +0100 | [diff] [blame] | 7838 | */ |
Vincent Guittot | ea67821 | 2015-02-27 16:54:11 +0100 | [diff] [blame] | 7839 | |
| 7840 | if (rq->nr_running == 1 && wl > env->imbalance && |
| 7841 | !check_cpu_capacity(rq, env->sd)) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7842 | continue; |
| 7843 | |
Thomas Gleixner | 6e40f5b | 2010-02-16 16:48:56 +0100 | [diff] [blame] | 7844 | /* |
| 7845 | * For the load comparisons with the other cpu's, consider |
Nicolas Pitre | ced549f | 2014-05-26 18:19:38 -0400 | [diff] [blame] | 7846 | * the weighted_cpuload() scaled with the cpu capacity, so |
| 7847 | * that the load can be moved away from the cpu that is |
| 7848 | * potentially running at a lower capacity. |
Joonsoo Kim | 95a79b8 | 2013-08-06 17:36:41 +0900 | [diff] [blame] | 7849 | * |
Nicolas Pitre | ced549f | 2014-05-26 18:19:38 -0400 | [diff] [blame] | 7850 | * Thus we're looking for max(wl_i / capacity_i), crosswise |
Joonsoo Kim | 95a79b8 | 2013-08-06 17:36:41 +0900 | [diff] [blame] | 7851 | * multiplication to rid ourselves of the division works out |
Nicolas Pitre | ced549f | 2014-05-26 18:19:38 -0400 | [diff] [blame] | 7852 | * to: wl_i * capacity_j > wl_j * capacity_i; where j is |
| 7853 | * our previous maximum. |
Thomas Gleixner | 6e40f5b | 2010-02-16 16:48:56 +0100 | [diff] [blame] | 7854 | */ |
Nicolas Pitre | ced549f | 2014-05-26 18:19:38 -0400 | [diff] [blame] | 7855 | if (wl * busiest_capacity > busiest_load * capacity) { |
Joonsoo Kim | 95a79b8 | 2013-08-06 17:36:41 +0900 | [diff] [blame] | 7856 | busiest_load = wl; |
Nicolas Pitre | ced549f | 2014-05-26 18:19:38 -0400 | [diff] [blame] | 7857 | busiest_capacity = capacity; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7858 | busiest = rq; |
| 7859 | } |
| 7860 | } |
| 7861 | |
| 7862 | return busiest; |
| 7863 | } |
| 7864 | |
| 7865 | /* |
| 7866 | * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but |
| 7867 | * so long as it is large enough. |
| 7868 | */ |
| 7869 | #define MAX_PINNED_INTERVAL 512 |
| 7870 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 7871 | static int need_active_balance(struct lb_env *env) |
Peter Zijlstra | 1af3ed3 | 2009-12-23 15:10:31 +0100 | [diff] [blame] | 7872 | { |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 7873 | struct sched_domain *sd = env->sd; |
| 7874 | |
| 7875 | if (env->idle == CPU_NEWLY_IDLE) { |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 7876 | |
| 7877 | /* |
| 7878 | * ASYM_PACKING needs to force migrate tasks from busy but |
| 7879 | * higher numbered CPUs in order to pack all tasks in the |
| 7880 | * lowest numbered CPUs. |
| 7881 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 7882 | if ((sd->flags & SD_ASYM_PACKING) && env->src_cpu > env->dst_cpu) |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 7883 | return 1; |
Peter Zijlstra | 1af3ed3 | 2009-12-23 15:10:31 +0100 | [diff] [blame] | 7884 | } |
| 7885 | |
Vincent Guittot | 1aaf90a | 2015-02-27 16:54:14 +0100 | [diff] [blame] | 7886 | /* |
| 7887 | * The dst_cpu is idle and the src_cpu CPU has only 1 CFS task. |
| 7888 | * It's worth migrating the task if the src_cpu's capacity is reduced |
| 7889 | * because of other sched_class or IRQs if more capacity stays |
| 7890 | * available on dst_cpu. |
| 7891 | */ |
| 7892 | if ((env->idle != CPU_NOT_IDLE) && |
| 7893 | (env->src_rq->cfs.h_nr_running == 1)) { |
| 7894 | if ((check_cpu_capacity(env->src_rq, sd)) && |
| 7895 | (capacity_of(env->src_cpu)*sd->imbalance_pct < capacity_of(env->dst_cpu)*100)) |
| 7896 | return 1; |
| 7897 | } |
| 7898 | |
Peter Zijlstra | 1af3ed3 | 2009-12-23 15:10:31 +0100 | [diff] [blame] | 7899 | return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2); |
| 7900 | } |
| 7901 | |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 7902 | static int active_load_balance_cpu_stop(void *data); |
| 7903 | |
Joonsoo Kim | 23f0d20 | 2013-08-06 17:36:42 +0900 | [diff] [blame] | 7904 | static int should_we_balance(struct lb_env *env) |
| 7905 | { |
| 7906 | struct sched_group *sg = env->sd->groups; |
| 7907 | struct cpumask *sg_cpus, *sg_mask; |
| 7908 | int cpu, balance_cpu = -1; |
| 7909 | |
| 7910 | /* |
| 7911 | * In the newly idle case, we will allow all the cpu's |
| 7912 | * to do the newly idle load balance. |
| 7913 | */ |
| 7914 | if (env->idle == CPU_NEWLY_IDLE) |
| 7915 | return 1; |
| 7916 | |
| 7917 | sg_cpus = sched_group_cpus(sg); |
| 7918 | sg_mask = sched_group_mask(sg); |
| 7919 | /* Try to find first idle cpu */ |
| 7920 | for_each_cpu_and(cpu, sg_cpus, env->cpus) { |
| 7921 | if (!cpumask_test_cpu(cpu, sg_mask) || !idle_cpu(cpu)) |
| 7922 | continue; |
| 7923 | |
| 7924 | balance_cpu = cpu; |
| 7925 | break; |
| 7926 | } |
| 7927 | |
| 7928 | if (balance_cpu == -1) |
| 7929 | balance_cpu = group_balance_cpu(sg); |
| 7930 | |
| 7931 | /* |
| 7932 | * First idle cpu or the first cpu(busiest) in this sched group |
| 7933 | * is eligible for doing load balancing at this and above domains. |
| 7934 | */ |
Joonsoo Kim | b0cff9d | 2013-09-10 15:54:49 +0900 | [diff] [blame] | 7935 | return balance_cpu == env->dst_cpu; |
Joonsoo Kim | 23f0d20 | 2013-08-06 17:36:42 +0900 | [diff] [blame] | 7936 | } |
| 7937 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7938 | /* |
| 7939 | * Check this_cpu to ensure it is balanced within domain. Attempt to move |
| 7940 | * tasks if there is an imbalance. |
| 7941 | */ |
| 7942 | static int load_balance(int this_cpu, struct rq *this_rq, |
| 7943 | struct sched_domain *sd, enum cpu_idle_type idle, |
Joonsoo Kim | 23f0d20 | 2013-08-06 17:36:42 +0900 | [diff] [blame] | 7944 | int *continue_balancing) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7945 | { |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 7946 | int ld_moved, cur_ld_moved, active_balance = 0; |
Peter Zijlstra | 6263322 | 2013-08-19 12:41:09 +0200 | [diff] [blame] | 7947 | struct sched_domain *sd_parent = sd->parent; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7948 | struct sched_group *group; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7949 | struct rq *busiest; |
| 7950 | unsigned long flags; |
Christoph Lameter | 4ba2968 | 2014-08-26 19:12:21 -0500 | [diff] [blame] | 7951 | struct cpumask *cpus = this_cpu_cpumask_var_ptr(load_balance_mask); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7952 | |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 7953 | struct lb_env env = { |
| 7954 | .sd = sd, |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 7955 | .dst_cpu = this_cpu, |
| 7956 | .dst_rq = this_rq, |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 7957 | .dst_grpmask = sched_group_cpus(sd->groups), |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 7958 | .idle = idle, |
Peter Zijlstra | eb95308 | 2012-04-17 13:38:40 +0200 | [diff] [blame] | 7959 | .loop_break = sched_nr_migrate_break, |
Michael Wang | b9403130 | 2012-07-12 16:10:13 +0800 | [diff] [blame] | 7960 | .cpus = cpus, |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 7961 | .fbq_type = all, |
Kirill Tkhai | 163122b | 2014-08-20 13:48:29 +0400 | [diff] [blame] | 7962 | .tasks = LIST_HEAD_INIT(env.tasks), |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 7963 | }; |
| 7964 | |
Joonsoo Kim | cfc0311 | 2013-04-23 17:27:39 +0900 | [diff] [blame] | 7965 | /* |
| 7966 | * For NEWLY_IDLE load_balancing, we don't need to consider |
| 7967 | * other cpus in our group |
| 7968 | */ |
Joonsoo Kim | e02e60c | 2013-04-23 17:27:42 +0900 | [diff] [blame] | 7969 | if (idle == CPU_NEWLY_IDLE) |
Joonsoo Kim | cfc0311 | 2013-04-23 17:27:39 +0900 | [diff] [blame] | 7970 | env.dst_grpmask = NULL; |
Joonsoo Kim | cfc0311 | 2013-04-23 17:27:39 +0900 | [diff] [blame] | 7971 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7972 | cpumask_copy(cpus, cpu_active_mask); |
| 7973 | |
Josh Poimboeuf | ae92882 | 2016-06-17 12:43:24 -0500 | [diff] [blame] | 7974 | schedstat_inc(sd->lb_count[idle]); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7975 | |
| 7976 | redo: |
Joonsoo Kim | 23f0d20 | 2013-08-06 17:36:42 +0900 | [diff] [blame] | 7977 | if (!should_we_balance(&env)) { |
| 7978 | *continue_balancing = 0; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7979 | goto out_balanced; |
Joonsoo Kim | 23f0d20 | 2013-08-06 17:36:42 +0900 | [diff] [blame] | 7980 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7981 | |
Joonsoo Kim | 23f0d20 | 2013-08-06 17:36:42 +0900 | [diff] [blame] | 7982 | group = find_busiest_group(&env); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7983 | if (!group) { |
Josh Poimboeuf | ae92882 | 2016-06-17 12:43:24 -0500 | [diff] [blame] | 7984 | schedstat_inc(sd->lb_nobusyg[idle]); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7985 | goto out_balanced; |
| 7986 | } |
| 7987 | |
Michael Wang | b9403130 | 2012-07-12 16:10:13 +0800 | [diff] [blame] | 7988 | busiest = find_busiest_queue(&env, group); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7989 | if (!busiest) { |
Josh Poimboeuf | ae92882 | 2016-06-17 12:43:24 -0500 | [diff] [blame] | 7990 | schedstat_inc(sd->lb_nobusyq[idle]); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7991 | goto out_balanced; |
| 7992 | } |
| 7993 | |
Michael Wang | 78feefc | 2012-08-06 16:41:59 +0800 | [diff] [blame] | 7994 | BUG_ON(busiest == env.dst_rq); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7995 | |
Josh Poimboeuf | ae92882 | 2016-06-17 12:43:24 -0500 | [diff] [blame] | 7996 | schedstat_add(sd->lb_imbalance[idle], env.imbalance); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 7997 | |
Vincent Guittot | 1aaf90a | 2015-02-27 16:54:14 +0100 | [diff] [blame] | 7998 | env.src_cpu = busiest->cpu; |
| 7999 | env.src_rq = busiest; |
| 8000 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8001 | ld_moved = 0; |
| 8002 | if (busiest->nr_running > 1) { |
| 8003 | /* |
| 8004 | * Attempt to move tasks. If find_busiest_group has found |
| 8005 | * an imbalance but busiest->nr_running <= 1, the group is |
| 8006 | * still unbalanced. ld_moved simply stays zero, so it is |
| 8007 | * correctly treated as an imbalance. |
| 8008 | */ |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 8009 | env.flags |= LBF_ALL_PINNED; |
Peter Zijlstra | c82513e | 2012-04-26 13:12:27 +0200 | [diff] [blame] | 8010 | env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running); |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 8011 | |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 8012 | more_balance: |
Kirill Tkhai | 163122b | 2014-08-20 13:48:29 +0400 | [diff] [blame] | 8013 | raw_spin_lock_irqsave(&busiest->lock, flags); |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 8014 | |
| 8015 | /* |
| 8016 | * cur_ld_moved - load moved in current iteration |
| 8017 | * ld_moved - cumulative load moved across iterations |
| 8018 | */ |
Kirill Tkhai | 163122b | 2014-08-20 13:48:29 +0400 | [diff] [blame] | 8019 | cur_ld_moved = detach_tasks(&env); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8020 | |
| 8021 | /* |
Kirill Tkhai | 163122b | 2014-08-20 13:48:29 +0400 | [diff] [blame] | 8022 | * We've detached some tasks from busiest_rq. Every |
| 8023 | * task is masked "TASK_ON_RQ_MIGRATING", so we can safely |
| 8024 | * unlock busiest->lock, and we are able to be sure |
| 8025 | * that nobody can manipulate the tasks in parallel. |
| 8026 | * See task_rq_lock() family for the details. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8027 | */ |
Kirill Tkhai | 163122b | 2014-08-20 13:48:29 +0400 | [diff] [blame] | 8028 | |
| 8029 | raw_spin_unlock(&busiest->lock); |
| 8030 | |
| 8031 | if (cur_ld_moved) { |
| 8032 | attach_tasks(&env); |
| 8033 | ld_moved += cur_ld_moved; |
| 8034 | } |
| 8035 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8036 | local_irq_restore(flags); |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 8037 | |
Joonsoo Kim | f1cd085 | 2013-04-23 17:27:37 +0900 | [diff] [blame] | 8038 | if (env.flags & LBF_NEED_BREAK) { |
| 8039 | env.flags &= ~LBF_NEED_BREAK; |
| 8040 | goto more_balance; |
| 8041 | } |
| 8042 | |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 8043 | /* |
| 8044 | * Revisit (affine) tasks on src_cpu that couldn't be moved to |
| 8045 | * us and move them to an alternate dst_cpu in our sched_group |
| 8046 | * where they can run. The upper limit on how many times we |
| 8047 | * iterate on same src_cpu is dependent on number of cpus in our |
| 8048 | * sched_group. |
| 8049 | * |
| 8050 | * This changes load balance semantics a bit on who can move |
| 8051 | * load to a given_cpu. In addition to the given_cpu itself |
| 8052 | * (or a ilb_cpu acting on its behalf where given_cpu is |
| 8053 | * nohz-idle), we now have balance_cpu in a position to move |
| 8054 | * load to given_cpu. In rare situations, this may cause |
| 8055 | * conflicts (balance_cpu and given_cpu/ilb_cpu deciding |
| 8056 | * _independently_ and at _same_ time to move some load to |
| 8057 | * given_cpu) causing exceess load to be moved to given_cpu. |
| 8058 | * This however should not happen so much in practice and |
| 8059 | * moreover subsequent load balance cycles should correct the |
| 8060 | * excess load moved. |
| 8061 | */ |
Peter Zijlstra | 6263322 | 2013-08-19 12:41:09 +0200 | [diff] [blame] | 8062 | if ((env.flags & LBF_DST_PINNED) && env.imbalance > 0) { |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 8063 | |
Vladimir Davydov | 7aff2e3 | 2013-09-15 21:30:13 +0400 | [diff] [blame] | 8064 | /* Prevent to re-select dst_cpu via env's cpus */ |
| 8065 | cpumask_clear_cpu(env.dst_cpu, env.cpus); |
| 8066 | |
Michael Wang | 78feefc | 2012-08-06 16:41:59 +0800 | [diff] [blame] | 8067 | env.dst_rq = cpu_rq(env.new_dst_cpu); |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 8068 | env.dst_cpu = env.new_dst_cpu; |
Peter Zijlstra | 6263322 | 2013-08-19 12:41:09 +0200 | [diff] [blame] | 8069 | env.flags &= ~LBF_DST_PINNED; |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 8070 | env.loop = 0; |
| 8071 | env.loop_break = sched_nr_migrate_break; |
Joonsoo Kim | e02e60c | 2013-04-23 17:27:42 +0900 | [diff] [blame] | 8072 | |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 8073 | /* |
| 8074 | * Go back to "more_balance" rather than "redo" since we |
| 8075 | * need to continue with same src_cpu. |
| 8076 | */ |
| 8077 | goto more_balance; |
| 8078 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8079 | |
Peter Zijlstra | 6263322 | 2013-08-19 12:41:09 +0200 | [diff] [blame] | 8080 | /* |
| 8081 | * We failed to reach balance because of affinity. |
| 8082 | */ |
| 8083 | if (sd_parent) { |
Nicolas Pitre | 63b2ca3 | 2014-05-26 18:19:37 -0400 | [diff] [blame] | 8084 | int *group_imbalance = &sd_parent->groups->sgc->imbalance; |
Peter Zijlstra | 6263322 | 2013-08-19 12:41:09 +0200 | [diff] [blame] | 8085 | |
Vincent Guittot | afdeee0 | 2014-08-26 13:06:44 +0200 | [diff] [blame] | 8086 | if ((env.flags & LBF_SOME_PINNED) && env.imbalance > 0) |
Peter Zijlstra | 6263322 | 2013-08-19 12:41:09 +0200 | [diff] [blame] | 8087 | *group_imbalance = 1; |
Peter Zijlstra | 6263322 | 2013-08-19 12:41:09 +0200 | [diff] [blame] | 8088 | } |
| 8089 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8090 | /* All tasks on this runqueue were pinned by CPU affinity */ |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 8091 | if (unlikely(env.flags & LBF_ALL_PINNED)) { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8092 | cpumask_clear_cpu(cpu_of(busiest), cpus); |
Prashanth Nageshappa | bbf18b1 | 2012-06-19 17:52:07 +0530 | [diff] [blame] | 8093 | if (!cpumask_empty(cpus)) { |
| 8094 | env.loop = 0; |
| 8095 | env.loop_break = sched_nr_migrate_break; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8096 | goto redo; |
Prashanth Nageshappa | bbf18b1 | 2012-06-19 17:52:07 +0530 | [diff] [blame] | 8097 | } |
Vincent Guittot | afdeee0 | 2014-08-26 13:06:44 +0200 | [diff] [blame] | 8098 | goto out_all_pinned; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8099 | } |
| 8100 | } |
| 8101 | |
| 8102 | if (!ld_moved) { |
Josh Poimboeuf | ae92882 | 2016-06-17 12:43:24 -0500 | [diff] [blame] | 8103 | schedstat_inc(sd->lb_failed[idle]); |
Venkatesh Pallipadi | 58b26c4 | 2010-09-10 18:19:17 -0700 | [diff] [blame] | 8104 | /* |
| 8105 | * Increment the failure counter only on periodic balance. |
| 8106 | * We do not want newidle balance, which can be very |
| 8107 | * frequent, pollute the failure counter causing |
| 8108 | * excessive cache_hot migrations and active balances. |
| 8109 | */ |
| 8110 | if (idle != CPU_NEWLY_IDLE) |
| 8111 | sd->nr_balance_failed++; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8112 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 8113 | if (need_active_balance(&env)) { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8114 | raw_spin_lock_irqsave(&busiest->lock, flags); |
| 8115 | |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 8116 | /* don't kick the active_load_balance_cpu_stop, |
| 8117 | * if the curr task on busiest cpu can't be |
| 8118 | * moved to this_cpu |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8119 | */ |
| 8120 | if (!cpumask_test_cpu(this_cpu, |
Peter Zijlstra | fa17b50 | 2011-06-16 12:23:22 +0200 | [diff] [blame] | 8121 | tsk_cpus_allowed(busiest->curr))) { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8122 | raw_spin_unlock_irqrestore(&busiest->lock, |
| 8123 | flags); |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 8124 | env.flags |= LBF_ALL_PINNED; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8125 | goto out_one_pinned; |
| 8126 | } |
| 8127 | |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 8128 | /* |
| 8129 | * ->active_balance synchronizes accesses to |
| 8130 | * ->active_balance_work. Once set, it's cleared |
| 8131 | * only after active load balance is finished. |
| 8132 | */ |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8133 | if (!busiest->active_balance) { |
| 8134 | busiest->active_balance = 1; |
| 8135 | busiest->push_cpu = this_cpu; |
| 8136 | active_balance = 1; |
| 8137 | } |
| 8138 | raw_spin_unlock_irqrestore(&busiest->lock, flags); |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 8139 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 8140 | if (active_balance) { |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 8141 | stop_one_cpu_nowait(cpu_of(busiest), |
| 8142 | active_load_balance_cpu_stop, busiest, |
| 8143 | &busiest->active_balance_work); |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 8144 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8145 | |
Srikar Dronamraju | d02c07118 | 2016-03-23 17:54:44 +0530 | [diff] [blame] | 8146 | /* We've kicked active balancing, force task migration. */ |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8147 | sd->nr_balance_failed = sd->cache_nice_tries+1; |
| 8148 | } |
| 8149 | } else |
| 8150 | sd->nr_balance_failed = 0; |
| 8151 | |
| 8152 | if (likely(!active_balance)) { |
| 8153 | /* We were unbalanced, so reset the balancing interval */ |
| 8154 | sd->balance_interval = sd->min_interval; |
| 8155 | } else { |
| 8156 | /* |
| 8157 | * If we've begun active balancing, start to back off. This |
| 8158 | * case may not be covered by the all_pinned logic if there |
| 8159 | * is only 1 task on the busy runqueue (because we don't call |
Kirill Tkhai | 163122b | 2014-08-20 13:48:29 +0400 | [diff] [blame] | 8160 | * detach_tasks). |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8161 | */ |
| 8162 | if (sd->balance_interval < sd->max_interval) |
| 8163 | sd->balance_interval *= 2; |
| 8164 | } |
| 8165 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8166 | goto out; |
| 8167 | |
| 8168 | out_balanced: |
Vincent Guittot | afdeee0 | 2014-08-26 13:06:44 +0200 | [diff] [blame] | 8169 | /* |
| 8170 | * We reach balance although we may have faced some affinity |
| 8171 | * constraints. Clear the imbalance flag if it was set. |
| 8172 | */ |
| 8173 | if (sd_parent) { |
| 8174 | int *group_imbalance = &sd_parent->groups->sgc->imbalance; |
| 8175 | |
| 8176 | if (*group_imbalance) |
| 8177 | *group_imbalance = 0; |
| 8178 | } |
| 8179 | |
| 8180 | out_all_pinned: |
| 8181 | /* |
| 8182 | * We reach balance because all tasks are pinned at this level so |
| 8183 | * we can't migrate them. Let the imbalance flag set so parent level |
| 8184 | * can try to migrate them. |
| 8185 | */ |
Josh Poimboeuf | ae92882 | 2016-06-17 12:43:24 -0500 | [diff] [blame] | 8186 | schedstat_inc(sd->lb_balanced[idle]); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8187 | |
| 8188 | sd->nr_balance_failed = 0; |
| 8189 | |
| 8190 | out_one_pinned: |
| 8191 | /* tune up the balancing interval */ |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 8192 | if (((env.flags & LBF_ALL_PINNED) && |
Peter Zijlstra | 5b54b56 | 2011-09-22 15:23:13 +0200 | [diff] [blame] | 8193 | sd->balance_interval < MAX_PINNED_INTERVAL) || |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8194 | (sd->balance_interval < sd->max_interval)) |
| 8195 | sd->balance_interval *= 2; |
| 8196 | |
Venkatesh Pallipadi | 46e49b3 | 2011-02-14 14:38:50 -0800 | [diff] [blame] | 8197 | ld_moved = 0; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8198 | out: |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8199 | return ld_moved; |
| 8200 | } |
| 8201 | |
Jason Low | 52a08ef | 2014-05-08 17:49:22 -0700 | [diff] [blame] | 8202 | static inline unsigned long |
| 8203 | get_sd_balance_interval(struct sched_domain *sd, int cpu_busy) |
| 8204 | { |
| 8205 | unsigned long interval = sd->balance_interval; |
| 8206 | |
| 8207 | if (cpu_busy) |
| 8208 | interval *= sd->busy_factor; |
| 8209 | |
| 8210 | /* scale ms to jiffies */ |
| 8211 | interval = msecs_to_jiffies(interval); |
| 8212 | interval = clamp(interval, 1UL, max_load_balance_interval); |
| 8213 | |
| 8214 | return interval; |
| 8215 | } |
| 8216 | |
| 8217 | static inline void |
Leo Yan | 31851a9 | 2016-08-05 14:31:29 +0800 | [diff] [blame] | 8218 | update_next_balance(struct sched_domain *sd, unsigned long *next_balance) |
Jason Low | 52a08ef | 2014-05-08 17:49:22 -0700 | [diff] [blame] | 8219 | { |
| 8220 | unsigned long interval, next; |
| 8221 | |
Leo Yan | 31851a9 | 2016-08-05 14:31:29 +0800 | [diff] [blame] | 8222 | /* used by idle balance, so cpu_busy = 0 */ |
| 8223 | interval = get_sd_balance_interval(sd, 0); |
Jason Low | 52a08ef | 2014-05-08 17:49:22 -0700 | [diff] [blame] | 8224 | next = sd->last_balance + interval; |
| 8225 | |
| 8226 | if (time_after(*next_balance, next)) |
| 8227 | *next_balance = next; |
| 8228 | } |
| 8229 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8230 | /* |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8231 | * idle_balance is called by schedule() if this_cpu is about to become |
| 8232 | * idle. Attempts to pull tasks from other CPUs. |
| 8233 | */ |
Peter Zijlstra | 6e83125 | 2014-02-11 16:11:48 +0100 | [diff] [blame] | 8234 | static int idle_balance(struct rq *this_rq) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8235 | { |
Jason Low | 52a08ef | 2014-05-08 17:49:22 -0700 | [diff] [blame] | 8236 | unsigned long next_balance = jiffies + HZ; |
| 8237 | int this_cpu = this_rq->cpu; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8238 | struct sched_domain *sd; |
| 8239 | int pulled_task = 0; |
Jason Low | 9bd721c | 2013-09-13 11:26:52 -0700 | [diff] [blame] | 8240 | u64 curr_cost = 0; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8241 | |
Peter Zijlstra | 6e83125 | 2014-02-11 16:11:48 +0100 | [diff] [blame] | 8242 | /* |
| 8243 | * We must set idle_stamp _before_ calling idle_balance(), such that we |
| 8244 | * measure the duration of idle_balance() as idle time. |
| 8245 | */ |
| 8246 | this_rq->idle_stamp = rq_clock(this_rq); |
| 8247 | |
Tim Chen | 4486edd | 2014-06-23 12:16:49 -0700 | [diff] [blame] | 8248 | if (this_rq->avg_idle < sysctl_sched_migration_cost || |
| 8249 | !this_rq->rd->overload) { |
Jason Low | 52a08ef | 2014-05-08 17:49:22 -0700 | [diff] [blame] | 8250 | rcu_read_lock(); |
| 8251 | sd = rcu_dereference_check_sched_domain(this_rq->sd); |
| 8252 | if (sd) |
Leo Yan | 31851a9 | 2016-08-05 14:31:29 +0800 | [diff] [blame] | 8253 | update_next_balance(sd, &next_balance); |
Jason Low | 52a08ef | 2014-05-08 17:49:22 -0700 | [diff] [blame] | 8254 | rcu_read_unlock(); |
| 8255 | |
Peter Zijlstra | 6e83125 | 2014-02-11 16:11:48 +0100 | [diff] [blame] | 8256 | goto out; |
Jason Low | 52a08ef | 2014-05-08 17:49:22 -0700 | [diff] [blame] | 8257 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8258 | |
Peter Zijlstra | f492e12 | 2009-12-23 15:29:42 +0100 | [diff] [blame] | 8259 | raw_spin_unlock(&this_rq->lock); |
| 8260 | |
Paul Turner | 48a1675 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 8261 | update_blocked_averages(this_cpu); |
Peter Zijlstra | dce840a | 2011-04-07 14:09:50 +0200 | [diff] [blame] | 8262 | rcu_read_lock(); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8263 | for_each_domain(this_cpu, sd) { |
Joonsoo Kim | 23f0d20 | 2013-08-06 17:36:42 +0900 | [diff] [blame] | 8264 | int continue_balancing = 1; |
Jason Low | 9bd721c | 2013-09-13 11:26:52 -0700 | [diff] [blame] | 8265 | u64 t0, domain_cost; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8266 | |
| 8267 | if (!(sd->flags & SD_LOAD_BALANCE)) |
| 8268 | continue; |
| 8269 | |
Jason Low | 52a08ef | 2014-05-08 17:49:22 -0700 | [diff] [blame] | 8270 | if (this_rq->avg_idle < curr_cost + sd->max_newidle_lb_cost) { |
Leo Yan | 31851a9 | 2016-08-05 14:31:29 +0800 | [diff] [blame] | 8271 | update_next_balance(sd, &next_balance); |
Jason Low | 9bd721c | 2013-09-13 11:26:52 -0700 | [diff] [blame] | 8272 | break; |
Jason Low | 52a08ef | 2014-05-08 17:49:22 -0700 | [diff] [blame] | 8273 | } |
Jason Low | 9bd721c | 2013-09-13 11:26:52 -0700 | [diff] [blame] | 8274 | |
Peter Zijlstra | f492e12 | 2009-12-23 15:29:42 +0100 | [diff] [blame] | 8275 | if (sd->flags & SD_BALANCE_NEWIDLE) { |
Jason Low | 9bd721c | 2013-09-13 11:26:52 -0700 | [diff] [blame] | 8276 | t0 = sched_clock_cpu(this_cpu); |
| 8277 | |
Peter Zijlstra | f492e12 | 2009-12-23 15:29:42 +0100 | [diff] [blame] | 8278 | pulled_task = load_balance(this_cpu, this_rq, |
Joonsoo Kim | 23f0d20 | 2013-08-06 17:36:42 +0900 | [diff] [blame] | 8279 | sd, CPU_NEWLY_IDLE, |
| 8280 | &continue_balancing); |
Jason Low | 9bd721c | 2013-09-13 11:26:52 -0700 | [diff] [blame] | 8281 | |
| 8282 | domain_cost = sched_clock_cpu(this_cpu) - t0; |
| 8283 | if (domain_cost > sd->max_newidle_lb_cost) |
| 8284 | sd->max_newidle_lb_cost = domain_cost; |
| 8285 | |
| 8286 | curr_cost += domain_cost; |
Peter Zijlstra | f492e12 | 2009-12-23 15:29:42 +0100 | [diff] [blame] | 8287 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8288 | |
Leo Yan | 31851a9 | 2016-08-05 14:31:29 +0800 | [diff] [blame] | 8289 | update_next_balance(sd, &next_balance); |
Jason Low | 39a4d9c | 2014-04-23 18:30:35 -0700 | [diff] [blame] | 8290 | |
| 8291 | /* |
| 8292 | * Stop searching for tasks to pull if there are |
| 8293 | * now runnable tasks on this rq. |
| 8294 | */ |
| 8295 | if (pulled_task || this_rq->nr_running > 0) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8296 | break; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8297 | } |
Peter Zijlstra | dce840a | 2011-04-07 14:09:50 +0200 | [diff] [blame] | 8298 | rcu_read_unlock(); |
Peter Zijlstra | f492e12 | 2009-12-23 15:29:42 +0100 | [diff] [blame] | 8299 | |
| 8300 | raw_spin_lock(&this_rq->lock); |
| 8301 | |
Jason Low | 0e5b533 | 2014-04-28 15:45:54 -0700 | [diff] [blame] | 8302 | if (curr_cost > this_rq->max_idle_balance_cost) |
| 8303 | this_rq->max_idle_balance_cost = curr_cost; |
| 8304 | |
Daniel Lezcano | e5fc661 | 2014-01-17 10:04:02 +0100 | [diff] [blame] | 8305 | /* |
Jason Low | 0e5b533 | 2014-04-28 15:45:54 -0700 | [diff] [blame] | 8306 | * While browsing the domains, we released the rq lock, a task could |
| 8307 | * have been enqueued in the meantime. Since we're not going idle, |
| 8308 | * pretend we pulled a task. |
Daniel Lezcano | e5fc661 | 2014-01-17 10:04:02 +0100 | [diff] [blame] | 8309 | */ |
Jason Low | 0e5b533 | 2014-04-28 15:45:54 -0700 | [diff] [blame] | 8310 | if (this_rq->cfs.h_nr_running && !pulled_task) |
Peter Zijlstra | 6e83125 | 2014-02-11 16:11:48 +0100 | [diff] [blame] | 8311 | pulled_task = 1; |
Daniel Lezcano | e5fc661 | 2014-01-17 10:04:02 +0100 | [diff] [blame] | 8312 | |
Peter Zijlstra | 6e83125 | 2014-02-11 16:11:48 +0100 | [diff] [blame] | 8313 | out: |
Jason Low | 52a08ef | 2014-05-08 17:49:22 -0700 | [diff] [blame] | 8314 | /* Move the next balance forward */ |
| 8315 | if (time_after(this_rq->next_balance, next_balance)) |
| 8316 | this_rq->next_balance = next_balance; |
| 8317 | |
Kirill Tkhai | e4aa358 | 2014-03-06 13:31:55 +0400 | [diff] [blame] | 8318 | /* Is there a task of a high priority class? */ |
Kirill Tkhai | 4638364 | 2014-03-15 02:15:07 +0400 | [diff] [blame] | 8319 | if (this_rq->nr_running != this_rq->cfs.h_nr_running) |
Kirill Tkhai | e4aa358 | 2014-03-06 13:31:55 +0400 | [diff] [blame] | 8320 | pulled_task = -1; |
| 8321 | |
Dietmar Eggemann | 38c6ade | 2015-10-20 13:04:41 +0100 | [diff] [blame] | 8322 | if (pulled_task) |
Peter Zijlstra | 6e83125 | 2014-02-11 16:11:48 +0100 | [diff] [blame] | 8323 | this_rq->idle_stamp = 0; |
| 8324 | |
Daniel Lezcano | 3c4017c | 2014-01-17 10:04:03 +0100 | [diff] [blame] | 8325 | return pulled_task; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8326 | } |
| 8327 | |
| 8328 | /* |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 8329 | * active_load_balance_cpu_stop is run by cpu stopper. It pushes |
| 8330 | * running tasks off the busiest CPU onto idle CPUs. It requires at |
| 8331 | * least 1 task to be running on each physical CPU where possible, and |
| 8332 | * avoids physical / logical imbalances. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8333 | */ |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 8334 | static int active_load_balance_cpu_stop(void *data) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8335 | { |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 8336 | struct rq *busiest_rq = data; |
| 8337 | int busiest_cpu = cpu_of(busiest_rq); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8338 | int target_cpu = busiest_rq->push_cpu; |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 8339 | struct rq *target_rq = cpu_rq(target_cpu); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8340 | struct sched_domain *sd; |
Kirill Tkhai | e5673f2 | 2014-08-20 13:48:01 +0400 | [diff] [blame] | 8341 | struct task_struct *p = NULL; |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 8342 | |
| 8343 | raw_spin_lock_irq(&busiest_rq->lock); |
| 8344 | |
| 8345 | /* make sure the requested cpu hasn't gone down in the meantime */ |
| 8346 | if (unlikely(busiest_cpu != smp_processor_id() || |
| 8347 | !busiest_rq->active_balance)) |
| 8348 | goto out_unlock; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8349 | |
| 8350 | /* Is there any task to move? */ |
| 8351 | if (busiest_rq->nr_running <= 1) |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 8352 | goto out_unlock; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8353 | |
| 8354 | /* |
| 8355 | * This condition is "impossible", if it occurs |
| 8356 | * we need to fix it. Originally reported by |
| 8357 | * Bjorn Helgaas on a 128-cpu setup. |
| 8358 | */ |
| 8359 | BUG_ON(busiest_rq == target_rq); |
| 8360 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8361 | /* Search for an sd spanning us and the target CPU. */ |
Peter Zijlstra | dce840a | 2011-04-07 14:09:50 +0200 | [diff] [blame] | 8362 | rcu_read_lock(); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8363 | for_each_domain(target_cpu, sd) { |
| 8364 | if ((sd->flags & SD_LOAD_BALANCE) && |
| 8365 | cpumask_test_cpu(busiest_cpu, sched_domain_span(sd))) |
| 8366 | break; |
| 8367 | } |
| 8368 | |
| 8369 | if (likely(sd)) { |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 8370 | struct lb_env env = { |
| 8371 | .sd = sd, |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 8372 | .dst_cpu = target_cpu, |
| 8373 | .dst_rq = target_rq, |
| 8374 | .src_cpu = busiest_rq->cpu, |
| 8375 | .src_rq = busiest_rq, |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 8376 | .idle = CPU_IDLE, |
| 8377 | }; |
| 8378 | |
Josh Poimboeuf | ae92882 | 2016-06-17 12:43:24 -0500 | [diff] [blame] | 8379 | schedstat_inc(sd->alb_count); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8380 | |
Kirill Tkhai | e5673f2 | 2014-08-20 13:48:01 +0400 | [diff] [blame] | 8381 | p = detach_one_task(&env); |
Srikar Dronamraju | d02c07118 | 2016-03-23 17:54:44 +0530 | [diff] [blame] | 8382 | if (p) { |
Josh Poimboeuf | ae92882 | 2016-06-17 12:43:24 -0500 | [diff] [blame] | 8383 | schedstat_inc(sd->alb_pushed); |
Srikar Dronamraju | d02c07118 | 2016-03-23 17:54:44 +0530 | [diff] [blame] | 8384 | /* Active balancing done, reset the failure counter. */ |
| 8385 | sd->nr_balance_failed = 0; |
| 8386 | } else { |
Josh Poimboeuf | ae92882 | 2016-06-17 12:43:24 -0500 | [diff] [blame] | 8387 | schedstat_inc(sd->alb_failed); |
Srikar Dronamraju | d02c07118 | 2016-03-23 17:54:44 +0530 | [diff] [blame] | 8388 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8389 | } |
Peter Zijlstra | dce840a | 2011-04-07 14:09:50 +0200 | [diff] [blame] | 8390 | rcu_read_unlock(); |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 8391 | out_unlock: |
| 8392 | busiest_rq->active_balance = 0; |
Kirill Tkhai | e5673f2 | 2014-08-20 13:48:01 +0400 | [diff] [blame] | 8393 | raw_spin_unlock(&busiest_rq->lock); |
| 8394 | |
| 8395 | if (p) |
| 8396 | attach_one_task(target_rq, p); |
| 8397 | |
| 8398 | local_irq_enable(); |
| 8399 | |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 8400 | return 0; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8401 | } |
| 8402 | |
Mike Galbraith | d987fc7 | 2011-12-05 10:01:47 +0100 | [diff] [blame] | 8403 | static inline int on_null_domain(struct rq *rq) |
| 8404 | { |
| 8405 | return unlikely(!rcu_dereference_sched(rq->sd)); |
| 8406 | } |
| 8407 | |
Frederic Weisbecker | 3451d02 | 2011-08-10 23:21:01 +0200 | [diff] [blame] | 8408 | #ifdef CONFIG_NO_HZ_COMMON |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 8409 | /* |
| 8410 | * idle load balancing details |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 8411 | * - When one of the busy CPUs notice that there may be an idle rebalancing |
| 8412 | * needed, they will kick the idle load balancer, which then does idle |
| 8413 | * load balancing for all the idle CPUs. |
| 8414 | */ |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8415 | static struct { |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 8416 | cpumask_var_t idle_cpus_mask; |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 8417 | atomic_t nr_cpus; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 8418 | unsigned long next_balance; /* in jiffy units */ |
| 8419 | } nohz ____cacheline_aligned; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8420 | |
Daniel Lezcano | 3dd0337 | 2014-01-06 12:34:41 +0100 | [diff] [blame] | 8421 | static inline int find_new_ilb(void) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8422 | { |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 8423 | int ilb = cpumask_first(nohz.idle_cpus_mask); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8424 | |
Suresh Siddha | 786d6dc | 2011-12-01 17:07:35 -0800 | [diff] [blame] | 8425 | if (ilb < nr_cpu_ids && idle_cpu(ilb)) |
| 8426 | return ilb; |
| 8427 | |
| 8428 | return nr_cpu_ids; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8429 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8430 | |
| 8431 | /* |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 8432 | * Kick a CPU to do the nohz balancing, if it is time for it. We pick the |
| 8433 | * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle |
| 8434 | * CPU (if there is one). |
| 8435 | */ |
Daniel Lezcano | 0aeeeeb | 2014-01-06 12:34:42 +0100 | [diff] [blame] | 8436 | static void nohz_balancer_kick(void) |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 8437 | { |
| 8438 | int ilb_cpu; |
| 8439 | |
| 8440 | nohz.next_balance++; |
| 8441 | |
Daniel Lezcano | 3dd0337 | 2014-01-06 12:34:41 +0100 | [diff] [blame] | 8442 | ilb_cpu = find_new_ilb(); |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 8443 | |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 8444 | if (ilb_cpu >= nr_cpu_ids) |
| 8445 | return; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 8446 | |
Suresh Siddha | cd490c5 | 2011-12-06 11:26:34 -0800 | [diff] [blame] | 8447 | if (test_and_set_bit(NOHZ_BALANCE_KICK, nohz_flags(ilb_cpu))) |
Suresh Siddha | 1c792db | 2011-12-01 17:07:32 -0800 | [diff] [blame] | 8448 | return; |
| 8449 | /* |
| 8450 | * Use smp_send_reschedule() instead of resched_cpu(). |
| 8451 | * This way we generate a sched IPI on the target cpu which |
| 8452 | * is idle. And the softirq performing nohz idle load balance |
| 8453 | * will be run before returning from the IPI. |
| 8454 | */ |
| 8455 | smp_send_reschedule(ilb_cpu); |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 8456 | return; |
| 8457 | } |
| 8458 | |
Thomas Gleixner | 20a5c8c | 2016-03-10 12:54:20 +0100 | [diff] [blame] | 8459 | void nohz_balance_exit_idle(unsigned int cpu) |
Suresh Siddha | 7132596 | 2012-01-19 18:28:57 -0800 | [diff] [blame] | 8460 | { |
| 8461 | if (unlikely(test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))) { |
Mike Galbraith | d987fc7 | 2011-12-05 10:01:47 +0100 | [diff] [blame] | 8462 | /* |
| 8463 | * Completely isolated CPUs don't ever set, so we must test. |
| 8464 | */ |
| 8465 | if (likely(cpumask_test_cpu(cpu, nohz.idle_cpus_mask))) { |
| 8466 | cpumask_clear_cpu(cpu, nohz.idle_cpus_mask); |
| 8467 | atomic_dec(&nohz.nr_cpus); |
| 8468 | } |
Suresh Siddha | 7132596 | 2012-01-19 18:28:57 -0800 | [diff] [blame] | 8469 | clear_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)); |
| 8470 | } |
| 8471 | } |
| 8472 | |
Suresh Siddha | 69e1e81 | 2011-12-01 17:07:33 -0800 | [diff] [blame] | 8473 | static inline void set_cpu_sd_state_busy(void) |
| 8474 | { |
| 8475 | struct sched_domain *sd; |
Preeti U Murthy | 37dc6b5 | 2013-10-30 08:42:52 +0530 | [diff] [blame] | 8476 | int cpu = smp_processor_id(); |
Suresh Siddha | 69e1e81 | 2011-12-01 17:07:33 -0800 | [diff] [blame] | 8477 | |
Suresh Siddha | 69e1e81 | 2011-12-01 17:07:33 -0800 | [diff] [blame] | 8478 | rcu_read_lock(); |
Peter Zijlstra | 0e369d7 | 2016-05-09 10:38:01 +0200 | [diff] [blame] | 8479 | sd = rcu_dereference(per_cpu(sd_llc, cpu)); |
Vincent Guittot | 25f55d9 | 2013-04-23 16:59:02 +0200 | [diff] [blame] | 8480 | |
| 8481 | if (!sd || !sd->nohz_idle) |
| 8482 | goto unlock; |
| 8483 | sd->nohz_idle = 0; |
| 8484 | |
Peter Zijlstra | 0e369d7 | 2016-05-09 10:38:01 +0200 | [diff] [blame] | 8485 | atomic_inc(&sd->shared->nr_busy_cpus); |
Vincent Guittot | 25f55d9 | 2013-04-23 16:59:02 +0200 | [diff] [blame] | 8486 | unlock: |
Suresh Siddha | 69e1e81 | 2011-12-01 17:07:33 -0800 | [diff] [blame] | 8487 | rcu_read_unlock(); |
| 8488 | } |
| 8489 | |
| 8490 | void set_cpu_sd_state_idle(void) |
| 8491 | { |
| 8492 | struct sched_domain *sd; |
Preeti U Murthy | 37dc6b5 | 2013-10-30 08:42:52 +0530 | [diff] [blame] | 8493 | int cpu = smp_processor_id(); |
Suresh Siddha | 69e1e81 | 2011-12-01 17:07:33 -0800 | [diff] [blame] | 8494 | |
Suresh Siddha | 69e1e81 | 2011-12-01 17:07:33 -0800 | [diff] [blame] | 8495 | rcu_read_lock(); |
Peter Zijlstra | 0e369d7 | 2016-05-09 10:38:01 +0200 | [diff] [blame] | 8496 | sd = rcu_dereference(per_cpu(sd_llc, cpu)); |
Vincent Guittot | 25f55d9 | 2013-04-23 16:59:02 +0200 | [diff] [blame] | 8497 | |
| 8498 | if (!sd || sd->nohz_idle) |
| 8499 | goto unlock; |
| 8500 | sd->nohz_idle = 1; |
| 8501 | |
Peter Zijlstra | 0e369d7 | 2016-05-09 10:38:01 +0200 | [diff] [blame] | 8502 | atomic_dec(&sd->shared->nr_busy_cpus); |
Vincent Guittot | 25f55d9 | 2013-04-23 16:59:02 +0200 | [diff] [blame] | 8503 | unlock: |
Suresh Siddha | 69e1e81 | 2011-12-01 17:07:33 -0800 | [diff] [blame] | 8504 | rcu_read_unlock(); |
| 8505 | } |
| 8506 | |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 8507 | /* |
Alex Shi | c1cc017 | 2012-09-10 15:10:58 +0800 | [diff] [blame] | 8508 | * This routine will record that the cpu is going idle with tick stopped. |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 8509 | * This info will be used in performing idle load balancing in the future. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8510 | */ |
Alex Shi | c1cc017 | 2012-09-10 15:10:58 +0800 | [diff] [blame] | 8511 | void nohz_balance_enter_idle(int cpu) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8512 | { |
Suresh Siddha | 7132596 | 2012-01-19 18:28:57 -0800 | [diff] [blame] | 8513 | /* |
| 8514 | * If this cpu is going down, then nothing needs to be done. |
| 8515 | */ |
| 8516 | if (!cpu_active(cpu)) |
| 8517 | return; |
| 8518 | |
Alex Shi | c1cc017 | 2012-09-10 15:10:58 +0800 | [diff] [blame] | 8519 | if (test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu))) |
| 8520 | return; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8521 | |
Mike Galbraith | d987fc7 | 2011-12-05 10:01:47 +0100 | [diff] [blame] | 8522 | /* |
| 8523 | * If we're a completely isolated CPU, we don't play. |
| 8524 | */ |
| 8525 | if (on_null_domain(cpu_rq(cpu))) |
| 8526 | return; |
| 8527 | |
Alex Shi | c1cc017 | 2012-09-10 15:10:58 +0800 | [diff] [blame] | 8528 | cpumask_set_cpu(cpu, nohz.idle_cpus_mask); |
| 8529 | atomic_inc(&nohz.nr_cpus); |
| 8530 | set_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8531 | } |
| 8532 | #endif |
| 8533 | |
| 8534 | static DEFINE_SPINLOCK(balancing); |
| 8535 | |
Peter Zijlstra | 49c022e | 2011-04-05 10:14:25 +0200 | [diff] [blame] | 8536 | /* |
| 8537 | * Scale the max load_balance interval with the number of CPUs in the system. |
| 8538 | * This trades load-balance latency on larger machines for less cross talk. |
| 8539 | */ |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 8540 | void update_max_interval(void) |
Peter Zijlstra | 49c022e | 2011-04-05 10:14:25 +0200 | [diff] [blame] | 8541 | { |
| 8542 | max_load_balance_interval = HZ*num_online_cpus()/10; |
| 8543 | } |
| 8544 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8545 | /* |
| 8546 | * It checks each scheduling domain to see if it is due to be balanced, |
| 8547 | * and initiates a balancing operation if so. |
| 8548 | * |
Libin | b9b0853 | 2013-04-01 19:14:01 +0800 | [diff] [blame] | 8549 | * Balancing parameters are set up in init_sched_domains. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8550 | */ |
Daniel Lezcano | f7ed0a8 | 2014-01-06 12:34:43 +0100 | [diff] [blame] | 8551 | static void rebalance_domains(struct rq *rq, enum cpu_idle_type idle) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8552 | { |
Joonsoo Kim | 23f0d20 | 2013-08-06 17:36:42 +0900 | [diff] [blame] | 8553 | int continue_balancing = 1; |
Daniel Lezcano | f7ed0a8 | 2014-01-06 12:34:43 +0100 | [diff] [blame] | 8554 | int cpu = rq->cpu; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8555 | unsigned long interval; |
Peter Zijlstra | 04f733b | 2012-05-11 00:12:02 +0200 | [diff] [blame] | 8556 | struct sched_domain *sd; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8557 | /* Earliest time when we have to do rebalance again */ |
| 8558 | unsigned long next_balance = jiffies + 60*HZ; |
| 8559 | int update_next_balance = 0; |
Jason Low | f48627e | 2013-09-13 11:26:53 -0700 | [diff] [blame] | 8560 | int need_serialize, need_decay = 0; |
| 8561 | u64 max_cost = 0; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8562 | |
Paul Turner | 48a1675 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 8563 | update_blocked_averages(cpu); |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 8564 | |
Peter Zijlstra | dce840a | 2011-04-07 14:09:50 +0200 | [diff] [blame] | 8565 | rcu_read_lock(); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8566 | for_each_domain(cpu, sd) { |
Jason Low | f48627e | 2013-09-13 11:26:53 -0700 | [diff] [blame] | 8567 | /* |
| 8568 | * Decay the newidle max times here because this is a regular |
| 8569 | * visit to all the domains. Decay ~1% per second. |
| 8570 | */ |
| 8571 | if (time_after(jiffies, sd->next_decay_max_lb_cost)) { |
| 8572 | sd->max_newidle_lb_cost = |
| 8573 | (sd->max_newidle_lb_cost * 253) / 256; |
| 8574 | sd->next_decay_max_lb_cost = jiffies + HZ; |
| 8575 | need_decay = 1; |
| 8576 | } |
| 8577 | max_cost += sd->max_newidle_lb_cost; |
| 8578 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8579 | if (!(sd->flags & SD_LOAD_BALANCE)) |
| 8580 | continue; |
| 8581 | |
Jason Low | f48627e | 2013-09-13 11:26:53 -0700 | [diff] [blame] | 8582 | /* |
| 8583 | * Stop the load balance at this level. There is another |
| 8584 | * CPU in our sched group which is doing load balancing more |
| 8585 | * actively. |
| 8586 | */ |
| 8587 | if (!continue_balancing) { |
| 8588 | if (need_decay) |
| 8589 | continue; |
| 8590 | break; |
| 8591 | } |
| 8592 | |
Jason Low | 52a08ef | 2014-05-08 17:49:22 -0700 | [diff] [blame] | 8593 | interval = get_sd_balance_interval(sd, idle != CPU_IDLE); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8594 | |
| 8595 | need_serialize = sd->flags & SD_SERIALIZE; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8596 | if (need_serialize) { |
| 8597 | if (!spin_trylock(&balancing)) |
| 8598 | goto out; |
| 8599 | } |
| 8600 | |
| 8601 | if (time_after_eq(jiffies, sd->last_balance + interval)) { |
Joonsoo Kim | 23f0d20 | 2013-08-06 17:36:42 +0900 | [diff] [blame] | 8602 | if (load_balance(cpu, rq, sd, idle, &continue_balancing)) { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8603 | /* |
Peter Zijlstra | 6263322 | 2013-08-19 12:41:09 +0200 | [diff] [blame] | 8604 | * The LBF_DST_PINNED logic could have changed |
Joonsoo Kim | de5eb2d | 2013-04-23 17:27:38 +0900 | [diff] [blame] | 8605 | * env->dst_cpu, so we can't know our idle |
| 8606 | * state even if we migrated tasks. Update it. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8607 | */ |
Joonsoo Kim | de5eb2d | 2013-04-23 17:27:38 +0900 | [diff] [blame] | 8608 | idle = idle_cpu(cpu) ? CPU_IDLE : CPU_NOT_IDLE; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8609 | } |
| 8610 | sd->last_balance = jiffies; |
Jason Low | 52a08ef | 2014-05-08 17:49:22 -0700 | [diff] [blame] | 8611 | interval = get_sd_balance_interval(sd, idle != CPU_IDLE); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8612 | } |
| 8613 | if (need_serialize) |
| 8614 | spin_unlock(&balancing); |
| 8615 | out: |
| 8616 | if (time_after(next_balance, sd->last_balance + interval)) { |
| 8617 | next_balance = sd->last_balance + interval; |
| 8618 | update_next_balance = 1; |
| 8619 | } |
Jason Low | f48627e | 2013-09-13 11:26:53 -0700 | [diff] [blame] | 8620 | } |
| 8621 | if (need_decay) { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8622 | /* |
Jason Low | f48627e | 2013-09-13 11:26:53 -0700 | [diff] [blame] | 8623 | * Ensure the rq-wide value also decays but keep it at a |
| 8624 | * reasonable floor to avoid funnies with rq->avg_idle. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8625 | */ |
Jason Low | f48627e | 2013-09-13 11:26:53 -0700 | [diff] [blame] | 8626 | rq->max_idle_balance_cost = |
| 8627 | max((u64)sysctl_sched_migration_cost, max_cost); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8628 | } |
Peter Zijlstra | dce840a | 2011-04-07 14:09:50 +0200 | [diff] [blame] | 8629 | rcu_read_unlock(); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8630 | |
| 8631 | /* |
| 8632 | * next_balance will be updated only when there is a need. |
| 8633 | * When the cpu is attached to null domain for ex, it will not be |
| 8634 | * updated. |
| 8635 | */ |
Vincent Guittot | c5afb6a | 2015-08-03 11:55:50 +0200 | [diff] [blame] | 8636 | if (likely(update_next_balance)) { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8637 | rq->next_balance = next_balance; |
Vincent Guittot | c5afb6a | 2015-08-03 11:55:50 +0200 | [diff] [blame] | 8638 | |
| 8639 | #ifdef CONFIG_NO_HZ_COMMON |
| 8640 | /* |
| 8641 | * If this CPU has been elected to perform the nohz idle |
| 8642 | * balance. Other idle CPUs have already rebalanced with |
| 8643 | * nohz_idle_balance() and nohz.next_balance has been |
| 8644 | * updated accordingly. This CPU is now running the idle load |
| 8645 | * balance for itself and we need to update the |
| 8646 | * nohz.next_balance accordingly. |
| 8647 | */ |
| 8648 | if ((idle == CPU_IDLE) && time_after(nohz.next_balance, rq->next_balance)) |
| 8649 | nohz.next_balance = rq->next_balance; |
| 8650 | #endif |
| 8651 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8652 | } |
| 8653 | |
Frederic Weisbecker | 3451d02 | 2011-08-10 23:21:01 +0200 | [diff] [blame] | 8654 | #ifdef CONFIG_NO_HZ_COMMON |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 8655 | /* |
Frederic Weisbecker | 3451d02 | 2011-08-10 23:21:01 +0200 | [diff] [blame] | 8656 | * In CONFIG_NO_HZ_COMMON case, the idle balance kickee will do the |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 8657 | * rebalancing for all the cpus for whom scheduler ticks are stopped. |
| 8658 | */ |
Daniel Lezcano | 208cb16 | 2014-01-06 12:34:44 +0100 | [diff] [blame] | 8659 | static void nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle) |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 8660 | { |
Daniel Lezcano | 208cb16 | 2014-01-06 12:34:44 +0100 | [diff] [blame] | 8661 | int this_cpu = this_rq->cpu; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 8662 | struct rq *rq; |
| 8663 | int balance_cpu; |
Vincent Guittot | c5afb6a | 2015-08-03 11:55:50 +0200 | [diff] [blame] | 8664 | /* Earliest time when we have to do rebalance again */ |
| 8665 | unsigned long next_balance = jiffies + 60*HZ; |
| 8666 | int update_next_balance = 0; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 8667 | |
Suresh Siddha | 1c792db | 2011-12-01 17:07:32 -0800 | [diff] [blame] | 8668 | if (idle != CPU_IDLE || |
| 8669 | !test_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu))) |
| 8670 | goto end; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 8671 | |
| 8672 | for_each_cpu(balance_cpu, nohz.idle_cpus_mask) { |
Suresh Siddha | 8a6d42d | 2011-12-06 11:19:37 -0800 | [diff] [blame] | 8673 | if (balance_cpu == this_cpu || !idle_cpu(balance_cpu)) |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 8674 | continue; |
| 8675 | |
| 8676 | /* |
| 8677 | * If this cpu gets work to do, stop the load balancing |
| 8678 | * work being done for other cpus. Next load |
| 8679 | * balancing owner will pick it up. |
| 8680 | */ |
Suresh Siddha | 1c792db | 2011-12-01 17:07:32 -0800 | [diff] [blame] | 8681 | if (need_resched()) |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 8682 | break; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 8683 | |
Vincent Guittot | 5ed4f1d | 2012-09-13 06:11:26 +0200 | [diff] [blame] | 8684 | rq = cpu_rq(balance_cpu); |
| 8685 | |
Tim Chen | ed61bbc | 2014-05-20 14:39:27 -0700 | [diff] [blame] | 8686 | /* |
| 8687 | * If time for next balance is due, |
| 8688 | * do the balance. |
| 8689 | */ |
| 8690 | if (time_after_eq(jiffies, rq->next_balance)) { |
| 8691 | raw_spin_lock_irq(&rq->lock); |
| 8692 | update_rq_clock(rq); |
Frederic Weisbecker | cee1afc | 2016-04-13 15:56:50 +0200 | [diff] [blame] | 8693 | cpu_load_update_idle(rq); |
Tim Chen | ed61bbc | 2014-05-20 14:39:27 -0700 | [diff] [blame] | 8694 | raw_spin_unlock_irq(&rq->lock); |
| 8695 | rebalance_domains(rq, CPU_IDLE); |
| 8696 | } |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 8697 | |
Vincent Guittot | c5afb6a | 2015-08-03 11:55:50 +0200 | [diff] [blame] | 8698 | if (time_after(next_balance, rq->next_balance)) { |
| 8699 | next_balance = rq->next_balance; |
| 8700 | update_next_balance = 1; |
| 8701 | } |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 8702 | } |
Vincent Guittot | c5afb6a | 2015-08-03 11:55:50 +0200 | [diff] [blame] | 8703 | |
| 8704 | /* |
| 8705 | * next_balance will be updated only when there is a need. |
| 8706 | * When the CPU is attached to null domain for ex, it will not be |
| 8707 | * updated. |
| 8708 | */ |
| 8709 | if (likely(update_next_balance)) |
| 8710 | nohz.next_balance = next_balance; |
Suresh Siddha | 1c792db | 2011-12-01 17:07:32 -0800 | [diff] [blame] | 8711 | end: |
| 8712 | clear_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu)); |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 8713 | } |
| 8714 | |
| 8715 | /* |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 8716 | * Current heuristic for kicking the idle load balancer in the presence |
Vincent Guittot | 1aaf90a | 2015-02-27 16:54:14 +0100 | [diff] [blame] | 8717 | * of an idle cpu in the system. |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 8718 | * - This rq has more than one task. |
Vincent Guittot | 1aaf90a | 2015-02-27 16:54:14 +0100 | [diff] [blame] | 8719 | * - This rq has at least one CFS task and the capacity of the CPU is |
| 8720 | * significantly reduced because of RT tasks or IRQs. |
| 8721 | * - At parent of LLC scheduler domain level, this cpu's scheduler group has |
| 8722 | * multiple busy cpu. |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 8723 | * - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler |
| 8724 | * domain span are idle. |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 8725 | */ |
Vincent Guittot | 1aaf90a | 2015-02-27 16:54:14 +0100 | [diff] [blame] | 8726 | static inline bool nohz_kick_needed(struct rq *rq) |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 8727 | { |
| 8728 | unsigned long now = jiffies; |
Peter Zijlstra | 0e369d7 | 2016-05-09 10:38:01 +0200 | [diff] [blame] | 8729 | struct sched_domain_shared *sds; |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 8730 | struct sched_domain *sd; |
Daniel Lezcano | 4a72562 | 2014-01-06 12:34:39 +0100 | [diff] [blame] | 8731 | int nr_busy, cpu = rq->cpu; |
Vincent Guittot | 1aaf90a | 2015-02-27 16:54:14 +0100 | [diff] [blame] | 8732 | bool kick = false; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 8733 | |
Daniel Lezcano | 4a72562 | 2014-01-06 12:34:39 +0100 | [diff] [blame] | 8734 | if (unlikely(rq->idle_balance)) |
Vincent Guittot | 1aaf90a | 2015-02-27 16:54:14 +0100 | [diff] [blame] | 8735 | return false; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 8736 | |
Suresh Siddha | 1c792db | 2011-12-01 17:07:32 -0800 | [diff] [blame] | 8737 | /* |
| 8738 | * We may be recently in ticked or tickless idle mode. At the first |
| 8739 | * busy tick after returning from idle, we will update the busy stats. |
| 8740 | */ |
Suresh Siddha | 69e1e81 | 2011-12-01 17:07:33 -0800 | [diff] [blame] | 8741 | set_cpu_sd_state_busy(); |
Alex Shi | c1cc017 | 2012-09-10 15:10:58 +0800 | [diff] [blame] | 8742 | nohz_balance_exit_idle(cpu); |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 8743 | |
| 8744 | /* |
| 8745 | * None are in tickless mode and hence no need for NOHZ idle load |
| 8746 | * balancing. |
| 8747 | */ |
| 8748 | if (likely(!atomic_read(&nohz.nr_cpus))) |
Vincent Guittot | 1aaf90a | 2015-02-27 16:54:14 +0100 | [diff] [blame] | 8749 | return false; |
Suresh Siddha | 1c792db | 2011-12-01 17:07:32 -0800 | [diff] [blame] | 8750 | |
| 8751 | if (time_before(now, nohz.next_balance)) |
Vincent Guittot | 1aaf90a | 2015-02-27 16:54:14 +0100 | [diff] [blame] | 8752 | return false; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 8753 | |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 8754 | if (rq->nr_running >= 2) |
Vincent Guittot | 1aaf90a | 2015-02-27 16:54:14 +0100 | [diff] [blame] | 8755 | return true; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 8756 | |
Peter Zijlstra | 067491b | 2011-12-07 14:32:08 +0100 | [diff] [blame] | 8757 | rcu_read_lock(); |
Peter Zijlstra | 0e369d7 | 2016-05-09 10:38:01 +0200 | [diff] [blame] | 8758 | sds = rcu_dereference(per_cpu(sd_llc_shared, cpu)); |
| 8759 | if (sds) { |
| 8760 | /* |
| 8761 | * XXX: write a coherent comment on why we do this. |
| 8762 | * See also: http://lkml.kernel.org/r/20111202010832.602203411@sbsiddha-desk.sc.intel.com |
| 8763 | */ |
| 8764 | nr_busy = atomic_read(&sds->nr_busy_cpus); |
Vincent Guittot | 1aaf90a | 2015-02-27 16:54:14 +0100 | [diff] [blame] | 8765 | if (nr_busy > 1) { |
| 8766 | kick = true; |
| 8767 | goto unlock; |
| 8768 | } |
| 8769 | |
| 8770 | } |
| 8771 | |
| 8772 | sd = rcu_dereference(rq->sd); |
| 8773 | if (sd) { |
| 8774 | if ((rq->cfs.h_nr_running >= 1) && |
| 8775 | check_cpu_capacity(rq, sd)) { |
| 8776 | kick = true; |
| 8777 | goto unlock; |
| 8778 | } |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 8779 | } |
Preeti U Murthy | 37dc6b5 | 2013-10-30 08:42:52 +0530 | [diff] [blame] | 8780 | |
| 8781 | sd = rcu_dereference(per_cpu(sd_asym, cpu)); |
Preeti U Murthy | 37dc6b5 | 2013-10-30 08:42:52 +0530 | [diff] [blame] | 8782 | if (sd && (cpumask_first_and(nohz.idle_cpus_mask, |
Vincent Guittot | 1aaf90a | 2015-02-27 16:54:14 +0100 | [diff] [blame] | 8783 | sched_domain_span(sd)) < cpu)) { |
| 8784 | kick = true; |
| 8785 | goto unlock; |
| 8786 | } |
Preeti U Murthy | 37dc6b5 | 2013-10-30 08:42:52 +0530 | [diff] [blame] | 8787 | |
Vincent Guittot | 1aaf90a | 2015-02-27 16:54:14 +0100 | [diff] [blame] | 8788 | unlock: |
Peter Zijlstra | 067491b | 2011-12-07 14:32:08 +0100 | [diff] [blame] | 8789 | rcu_read_unlock(); |
Vincent Guittot | 1aaf90a | 2015-02-27 16:54:14 +0100 | [diff] [blame] | 8790 | return kick; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 8791 | } |
| 8792 | #else |
Daniel Lezcano | 208cb16 | 2014-01-06 12:34:44 +0100 | [diff] [blame] | 8793 | static void nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle) { } |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 8794 | #endif |
| 8795 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8796 | /* |
| 8797 | * run_rebalance_domains is triggered when needed from the scheduler tick. |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 8798 | * Also triggered for nohz idle balancing (with nohz_balancing_kick set). |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8799 | */ |
Emese Revfy | 0766f78 | 2016-06-20 20:42:34 +0200 | [diff] [blame] | 8800 | static __latent_entropy void run_rebalance_domains(struct softirq_action *h) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8801 | { |
Daniel Lezcano | 208cb16 | 2014-01-06 12:34:44 +0100 | [diff] [blame] | 8802 | struct rq *this_rq = this_rq(); |
Suresh Siddha | 6eb57e0 | 2011-10-03 15:09:01 -0700 | [diff] [blame] | 8803 | enum cpu_idle_type idle = this_rq->idle_balance ? |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8804 | CPU_IDLE : CPU_NOT_IDLE; |
| 8805 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8806 | /* |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 8807 | * If this cpu has a pending nohz_balance_kick, then do the |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8808 | * balancing on behalf of the other idle cpus whose ticks are |
Preeti U Murthy | d4573c3 | 2015-03-26 18:32:44 +0530 | [diff] [blame] | 8809 | * stopped. Do nohz_idle_balance *before* rebalance_domains to |
| 8810 | * give the idle cpus a chance to load balance. Else we may |
| 8811 | * load balance only within the local sched_domain hierarchy |
| 8812 | * and abort nohz_idle_balance altogether if we pull some load. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8813 | */ |
Daniel Lezcano | 208cb16 | 2014-01-06 12:34:44 +0100 | [diff] [blame] | 8814 | nohz_idle_balance(this_rq, idle); |
Preeti U Murthy | d4573c3 | 2015-03-26 18:32:44 +0530 | [diff] [blame] | 8815 | rebalance_domains(this_rq, idle); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8816 | } |
| 8817 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8818 | /* |
| 8819 | * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8820 | */ |
Daniel Lezcano | 7caff66 | 2014-01-06 12:34:38 +0100 | [diff] [blame] | 8821 | void trigger_load_balance(struct rq *rq) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8822 | { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8823 | /* Don't need to rebalance while attached to NULL domain */ |
Daniel Lezcano | c726099 | 2014-01-06 12:34:45 +0100 | [diff] [blame] | 8824 | if (unlikely(on_null_domain(rq))) |
| 8825 | return; |
| 8826 | |
| 8827 | if (time_after_eq(jiffies, rq->next_balance)) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8828 | raise_softirq(SCHED_SOFTIRQ); |
Frederic Weisbecker | 3451d02 | 2011-08-10 23:21:01 +0200 | [diff] [blame] | 8829 | #ifdef CONFIG_NO_HZ_COMMON |
Daniel Lezcano | c726099 | 2014-01-06 12:34:45 +0100 | [diff] [blame] | 8830 | if (nohz_kick_needed(rq)) |
Daniel Lezcano | 0aeeeeb | 2014-01-06 12:34:42 +0100 | [diff] [blame] | 8831 | nohz_balancer_kick(); |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 8832 | #endif |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 8833 | } |
| 8834 | |
Christian Ehrhardt | 0bcdcf2 | 2009-11-30 12:16:46 +0100 | [diff] [blame] | 8835 | static void rq_online_fair(struct rq *rq) |
| 8836 | { |
| 8837 | update_sysctl(); |
Kirill Tkhai | 0e59bda | 2014-06-25 12:19:42 +0400 | [diff] [blame] | 8838 | |
| 8839 | update_runtime_enabled(rq); |
Christian Ehrhardt | 0bcdcf2 | 2009-11-30 12:16:46 +0100 | [diff] [blame] | 8840 | } |
| 8841 | |
| 8842 | static void rq_offline_fair(struct rq *rq) |
| 8843 | { |
| 8844 | update_sysctl(); |
Peter Boonstoppel | a4c96ae | 2012-08-09 15:34:47 -0700 | [diff] [blame] | 8845 | |
| 8846 | /* Ensure any throttled groups are reachable by pick_next_task */ |
| 8847 | unthrottle_offline_cfs_rqs(rq); |
Christian Ehrhardt | 0bcdcf2 | 2009-11-30 12:16:46 +0100 | [diff] [blame] | 8848 | } |
| 8849 | |
Dhaval Giani | 55e12e5 | 2008-06-24 23:39:43 +0530 | [diff] [blame] | 8850 | #endif /* CONFIG_SMP */ |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 8851 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 8852 | /* |
| 8853 | * scheduler tick hitting a task of our scheduling class: |
| 8854 | */ |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 8855 | static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 8856 | { |
| 8857 | struct cfs_rq *cfs_rq; |
| 8858 | struct sched_entity *se = &curr->se; |
| 8859 | |
| 8860 | for_each_sched_entity(se) { |
| 8861 | cfs_rq = cfs_rq_of(se); |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 8862 | entity_tick(cfs_rq, se, queued); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 8863 | } |
Ben Segall | 18bf280 | 2012-10-04 12:51:20 +0200 | [diff] [blame] | 8864 | |
Srikar Dronamraju | b52da86 | 2015-10-02 07:48:25 +0530 | [diff] [blame] | 8865 | if (static_branch_unlikely(&sched_numa_balancing)) |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 8866 | task_tick_numa(rq, curr); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 8867 | } |
| 8868 | |
| 8869 | /* |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 8870 | * called on fork with the child task as argument from the parent's context |
| 8871 | * - child not yet on the tasklist |
| 8872 | * - preemption disabled |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 8873 | */ |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 8874 | static void task_fork_fair(struct task_struct *p) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 8875 | { |
Daisuke Nishimura | 4fc420c | 2011-12-15 14:36:55 +0900 | [diff] [blame] | 8876 | struct cfs_rq *cfs_rq; |
| 8877 | struct sched_entity *se = &p->se, *curr; |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 8878 | struct rq *rq = this_rq(); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 8879 | |
Peter Zijlstra | e210bff | 2016-06-16 18:51:48 +0200 | [diff] [blame] | 8880 | raw_spin_lock(&rq->lock); |
Peter Zijlstra | 861d034 | 2010-08-19 13:31:43 +0200 | [diff] [blame] | 8881 | update_rq_clock(rq); |
| 8882 | |
Daisuke Nishimura | 4fc420c | 2011-12-15 14:36:55 +0900 | [diff] [blame] | 8883 | cfs_rq = task_cfs_rq(current); |
| 8884 | curr = cfs_rq->curr; |
Peter Zijlstra | e210bff | 2016-06-16 18:51:48 +0200 | [diff] [blame] | 8885 | if (curr) { |
| 8886 | update_curr(cfs_rq); |
Mike Galbraith | b5d9d73 | 2009-09-08 11:12:28 +0200 | [diff] [blame] | 8887 | se->vruntime = curr->vruntime; |
Peter Zijlstra | e210bff | 2016-06-16 18:51:48 +0200 | [diff] [blame] | 8888 | } |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 8889 | place_entity(cfs_rq, se, 1); |
Peter Zijlstra | 4d78e7b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 8890 | |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 8891 | if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) { |
Dmitry Adamushko | 87fefa3 | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 8892 | /* |
Ingo Molnar | edcb60a | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 8893 | * Upon rescheduling, sched_class::put_prev_task() will place |
| 8894 | * 'current' within the tree based on its new key value. |
| 8895 | */ |
Peter Zijlstra | 4d78e7b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 8896 | swap(curr->vruntime, se->vruntime); |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 8897 | resched_curr(rq); |
Peter Zijlstra | 4d78e7b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 8898 | } |
| 8899 | |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 8900 | se->vruntime -= cfs_rq->min_vruntime; |
Peter Zijlstra | e210bff | 2016-06-16 18:51:48 +0200 | [diff] [blame] | 8901 | raw_spin_unlock(&rq->lock); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 8902 | } |
| 8903 | |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 8904 | /* |
| 8905 | * Priority of the task has changed. Check to see if we preempt |
| 8906 | * the current task. |
| 8907 | */ |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 8908 | static void |
| 8909 | prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio) |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 8910 | { |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 8911 | if (!task_on_rq_queued(p)) |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 8912 | return; |
| 8913 | |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 8914 | /* |
| 8915 | * Reschedule if we are currently running on this runqueue and |
| 8916 | * our priority decreased, or if we are not currently running on |
| 8917 | * this runqueue and our priority is higher than the current's |
| 8918 | */ |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 8919 | if (rq->curr == p) { |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 8920 | if (p->prio > oldprio) |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 8921 | resched_curr(rq); |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 8922 | } else |
Peter Zijlstra | 15afe09 | 2008-09-20 23:38:02 +0200 | [diff] [blame] | 8923 | check_preempt_curr(rq, p, 0); |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 8924 | } |
| 8925 | |
Byungchul Park | daa5940 | 2015-08-20 20:22:00 +0900 | [diff] [blame] | 8926 | static inline bool vruntime_normalized(struct task_struct *p) |
| 8927 | { |
| 8928 | struct sched_entity *se = &p->se; |
| 8929 | |
| 8930 | /* |
| 8931 | * In both the TASK_ON_RQ_QUEUED and TASK_ON_RQ_MIGRATING cases, |
| 8932 | * the dequeue_entity(.flags=0) will already have normalized the |
| 8933 | * vruntime. |
| 8934 | */ |
| 8935 | if (p->on_rq) |
| 8936 | return true; |
| 8937 | |
| 8938 | /* |
| 8939 | * When !on_rq, vruntime of the task has usually NOT been normalized. |
| 8940 | * But there are some cases where it has already been normalized: |
| 8941 | * |
| 8942 | * - A forked child which is waiting for being woken up by |
| 8943 | * wake_up_new_task(). |
| 8944 | * - A task which has been woken up by try_to_wake_up() and |
| 8945 | * waiting for actually being woken up by sched_ttwu_pending(). |
| 8946 | */ |
| 8947 | if (!se->sum_exec_runtime || p->state == TASK_WAKING) |
| 8948 | return true; |
| 8949 | |
| 8950 | return false; |
| 8951 | } |
| 8952 | |
Vincent Guittot | 09a43ac | 2016-11-08 10:53:45 +0100 | [diff] [blame^] | 8953 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 8954 | /* |
| 8955 | * Propagate the changes of the sched_entity across the tg tree to make it |
| 8956 | * visible to the root |
| 8957 | */ |
| 8958 | static void propagate_entity_cfs_rq(struct sched_entity *se) |
| 8959 | { |
| 8960 | struct cfs_rq *cfs_rq; |
| 8961 | |
| 8962 | /* Start to propagate at parent */ |
| 8963 | se = se->parent; |
| 8964 | |
| 8965 | for_each_sched_entity(se) { |
| 8966 | cfs_rq = cfs_rq_of(se); |
| 8967 | |
| 8968 | if (cfs_rq_throttled(cfs_rq)) |
| 8969 | break; |
| 8970 | |
| 8971 | update_load_avg(se, UPDATE_TG); |
| 8972 | } |
| 8973 | } |
| 8974 | #else |
| 8975 | static void propagate_entity_cfs_rq(struct sched_entity *se) { } |
| 8976 | #endif |
| 8977 | |
Vincent Guittot | df21791 | 2016-11-08 10:53:42 +0100 | [diff] [blame] | 8978 | static void detach_entity_cfs_rq(struct sched_entity *se) |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 8979 | { |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 8980 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 8981 | |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 8982 | /* Catch up with the cfs_rq and remove our load when we leave */ |
Vincent Guittot | d31b1a6 | 2016-11-08 10:53:44 +0100 | [diff] [blame] | 8983 | update_load_avg(se, 0); |
Byungchul Park | a05e8c5 | 2015-08-20 20:21:56 +0900 | [diff] [blame] | 8984 | detach_entity_load_avg(cfs_rq, se); |
Peter Zijlstra | 7c3edd2 | 2016-07-13 10:56:25 +0200 | [diff] [blame] | 8985 | update_tg_load_avg(cfs_rq, false); |
Vincent Guittot | 09a43ac | 2016-11-08 10:53:45 +0100 | [diff] [blame^] | 8986 | propagate_entity_cfs_rq(se); |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 8987 | } |
| 8988 | |
Vincent Guittot | df21791 | 2016-11-08 10:53:42 +0100 | [diff] [blame] | 8989 | static void attach_entity_cfs_rq(struct sched_entity *se) |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 8990 | { |
Byungchul Park | daa5940 | 2015-08-20 20:22:00 +0900 | [diff] [blame] | 8991 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
Byungchul Park | 7855a35 | 2015-08-10 18:02:55 +0900 | [diff] [blame] | 8992 | |
| 8993 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Michael wang | eb7a59b | 2014-02-20 11:14:53 +0800 | [diff] [blame] | 8994 | /* |
| 8995 | * Since the real-depth could have been changed (only FAIR |
| 8996 | * class maintain depth value), reset depth properly. |
| 8997 | */ |
| 8998 | se->depth = se->parent ? se->parent->depth + 1 : 0; |
| 8999 | #endif |
Byungchul Park | 7855a35 | 2015-08-10 18:02:55 +0900 | [diff] [blame] | 9000 | |
Vincent Guittot | df21791 | 2016-11-08 10:53:42 +0100 | [diff] [blame] | 9001 | /* Synchronize entity with its cfs_rq */ |
Vincent Guittot | d31b1a6 | 2016-11-08 10:53:44 +0100 | [diff] [blame] | 9002 | update_load_avg(se, sched_feat(ATTACH_AGE_LOAD) ? 0 : SKIP_AGE_LOAD); |
Byungchul Park | daa5940 | 2015-08-20 20:22:00 +0900 | [diff] [blame] | 9003 | attach_entity_load_avg(cfs_rq, se); |
Peter Zijlstra | 7c3edd2 | 2016-07-13 10:56:25 +0200 | [diff] [blame] | 9004 | update_tg_load_avg(cfs_rq, false); |
Vincent Guittot | 09a43ac | 2016-11-08 10:53:45 +0100 | [diff] [blame^] | 9005 | propagate_entity_cfs_rq(se); |
Vincent Guittot | df21791 | 2016-11-08 10:53:42 +0100 | [diff] [blame] | 9006 | } |
| 9007 | |
| 9008 | static void detach_task_cfs_rq(struct task_struct *p) |
| 9009 | { |
| 9010 | struct sched_entity *se = &p->se; |
| 9011 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 9012 | |
| 9013 | if (!vruntime_normalized(p)) { |
| 9014 | /* |
| 9015 | * Fix up our vruntime so that the current sleep doesn't |
| 9016 | * cause 'unlimited' sleep bonus. |
| 9017 | */ |
| 9018 | place_entity(cfs_rq, se, 0); |
| 9019 | se->vruntime -= cfs_rq->min_vruntime; |
| 9020 | } |
| 9021 | |
| 9022 | detach_entity_cfs_rq(se); |
| 9023 | } |
| 9024 | |
| 9025 | static void attach_task_cfs_rq(struct task_struct *p) |
| 9026 | { |
| 9027 | struct sched_entity *se = &p->se; |
| 9028 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 9029 | |
| 9030 | attach_entity_cfs_rq(se); |
Byungchul Park | 6efdb10 | 2015-08-20 20:21:59 +0900 | [diff] [blame] | 9031 | |
Byungchul Park | daa5940 | 2015-08-20 20:22:00 +0900 | [diff] [blame] | 9032 | if (!vruntime_normalized(p)) |
| 9033 | se->vruntime += cfs_rq->min_vruntime; |
| 9034 | } |
Byungchul Park | 7855a35 | 2015-08-10 18:02:55 +0900 | [diff] [blame] | 9035 | |
Byungchul Park | daa5940 | 2015-08-20 20:22:00 +0900 | [diff] [blame] | 9036 | static void switched_from_fair(struct rq *rq, struct task_struct *p) |
| 9037 | { |
| 9038 | detach_task_cfs_rq(p); |
| 9039 | } |
| 9040 | |
| 9041 | static void switched_to_fair(struct rq *rq, struct task_struct *p) |
| 9042 | { |
| 9043 | attach_task_cfs_rq(p); |
| 9044 | |
| 9045 | if (task_on_rq_queued(p)) { |
Byungchul Park | 7855a35 | 2015-08-10 18:02:55 +0900 | [diff] [blame] | 9046 | /* |
Byungchul Park | daa5940 | 2015-08-20 20:22:00 +0900 | [diff] [blame] | 9047 | * We were most likely switched from sched_rt, so |
| 9048 | * kick off the schedule if running, otherwise just see |
| 9049 | * if we can still preempt the current task. |
Byungchul Park | 7855a35 | 2015-08-10 18:02:55 +0900 | [diff] [blame] | 9050 | */ |
Byungchul Park | daa5940 | 2015-08-20 20:22:00 +0900 | [diff] [blame] | 9051 | if (rq->curr == p) |
| 9052 | resched_curr(rq); |
| 9053 | else |
| 9054 | check_preempt_curr(rq, p, 0); |
Byungchul Park | 7855a35 | 2015-08-10 18:02:55 +0900 | [diff] [blame] | 9055 | } |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 9056 | } |
| 9057 | |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 9058 | /* Account for a task changing its policy or group. |
| 9059 | * |
| 9060 | * This routine is mostly called to set cfs_rq->curr field when a task |
| 9061 | * migrates between groups/classes. |
| 9062 | */ |
| 9063 | static void set_curr_task_fair(struct rq *rq) |
| 9064 | { |
| 9065 | struct sched_entity *se = &rq->curr->se; |
| 9066 | |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 9067 | for_each_sched_entity(se) { |
| 9068 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 9069 | |
| 9070 | set_next_entity(cfs_rq, se); |
| 9071 | /* ensure bandwidth has been allocated on our new cfs_rq */ |
| 9072 | account_cfs_rq_runtime(cfs_rq, 0); |
| 9073 | } |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 9074 | } |
| 9075 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9076 | void init_cfs_rq(struct cfs_rq *cfs_rq) |
| 9077 | { |
| 9078 | cfs_rq->tasks_timeline = RB_ROOT; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9079 | cfs_rq->min_vruntime = (u64)(-(1LL << 20)); |
| 9080 | #ifndef CONFIG_64BIT |
| 9081 | cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime; |
| 9082 | #endif |
Alex Shi | 141965c | 2013-06-26 13:05:39 +0800 | [diff] [blame] | 9083 | #ifdef CONFIG_SMP |
Vincent Guittot | 09a43ac | 2016-11-08 10:53:45 +0100 | [diff] [blame^] | 9084 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 9085 | cfs_rq->propagate_avg = 0; |
| 9086 | #endif |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 9087 | atomic_long_set(&cfs_rq->removed_load_avg, 0); |
| 9088 | atomic_long_set(&cfs_rq->removed_util_avg, 0); |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 9089 | #endif |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9090 | } |
| 9091 | |
Peter Zijlstra | 810b381 | 2008-02-29 15:21:01 -0500 | [diff] [blame] | 9092 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Vincent Guittot | ea86cb4 | 2016-06-17 13:38:55 +0200 | [diff] [blame] | 9093 | static void task_set_group_fair(struct task_struct *p) |
| 9094 | { |
| 9095 | struct sched_entity *se = &p->se; |
| 9096 | |
| 9097 | set_task_rq(p, task_cpu(p)); |
| 9098 | se->depth = se->parent ? se->parent->depth + 1 : 0; |
| 9099 | } |
| 9100 | |
Peter Zijlstra | bc54da2 | 2015-08-31 17:13:55 +0200 | [diff] [blame] | 9101 | static void task_move_group_fair(struct task_struct *p) |
Peter Zijlstra | 810b381 | 2008-02-29 15:21:01 -0500 | [diff] [blame] | 9102 | { |
Byungchul Park | daa5940 | 2015-08-20 20:22:00 +0900 | [diff] [blame] | 9103 | detach_task_cfs_rq(p); |
Peter Zijlstra | b2b5ce0 | 2010-10-15 15:24:15 +0200 | [diff] [blame] | 9104 | set_task_rq(p, task_cpu(p)); |
Byungchul Park | 6efdb10 | 2015-08-20 20:21:59 +0900 | [diff] [blame] | 9105 | |
| 9106 | #ifdef CONFIG_SMP |
| 9107 | /* Tell se's cfs_rq has been changed -- migrated */ |
| 9108 | p->se.avg.last_update_time = 0; |
| 9109 | #endif |
Byungchul Park | daa5940 | 2015-08-20 20:22:00 +0900 | [diff] [blame] | 9110 | attach_task_cfs_rq(p); |
Peter Zijlstra | 810b381 | 2008-02-29 15:21:01 -0500 | [diff] [blame] | 9111 | } |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9112 | |
Vincent Guittot | ea86cb4 | 2016-06-17 13:38:55 +0200 | [diff] [blame] | 9113 | static void task_change_group_fair(struct task_struct *p, int type) |
| 9114 | { |
| 9115 | switch (type) { |
| 9116 | case TASK_SET_GROUP: |
| 9117 | task_set_group_fair(p); |
| 9118 | break; |
| 9119 | |
| 9120 | case TASK_MOVE_GROUP: |
| 9121 | task_move_group_fair(p); |
| 9122 | break; |
| 9123 | } |
| 9124 | } |
| 9125 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9126 | void free_fair_sched_group(struct task_group *tg) |
| 9127 | { |
| 9128 | int i; |
| 9129 | |
| 9130 | destroy_cfs_bandwidth(tg_cfs_bandwidth(tg)); |
| 9131 | |
| 9132 | for_each_possible_cpu(i) { |
| 9133 | if (tg->cfs_rq) |
| 9134 | kfree(tg->cfs_rq[i]); |
Peter Zijlstra | 6fe1f34 | 2016-01-21 22:24:16 +0100 | [diff] [blame] | 9135 | if (tg->se) |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9136 | kfree(tg->se[i]); |
| 9137 | } |
| 9138 | |
| 9139 | kfree(tg->cfs_rq); |
| 9140 | kfree(tg->se); |
| 9141 | } |
| 9142 | |
| 9143 | int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent) |
| 9144 | { |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9145 | struct sched_entity *se; |
Peter Zijlstra | b7fa30c | 2016-06-09 15:07:50 +0200 | [diff] [blame] | 9146 | struct cfs_rq *cfs_rq; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9147 | int i; |
| 9148 | |
| 9149 | tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL); |
| 9150 | if (!tg->cfs_rq) |
| 9151 | goto err; |
| 9152 | tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL); |
| 9153 | if (!tg->se) |
| 9154 | goto err; |
| 9155 | |
| 9156 | tg->shares = NICE_0_LOAD; |
| 9157 | |
| 9158 | init_cfs_bandwidth(tg_cfs_bandwidth(tg)); |
| 9159 | |
| 9160 | for_each_possible_cpu(i) { |
| 9161 | cfs_rq = kzalloc_node(sizeof(struct cfs_rq), |
| 9162 | GFP_KERNEL, cpu_to_node(i)); |
| 9163 | if (!cfs_rq) |
| 9164 | goto err; |
| 9165 | |
| 9166 | se = kzalloc_node(sizeof(struct sched_entity), |
| 9167 | GFP_KERNEL, cpu_to_node(i)); |
| 9168 | if (!se) |
| 9169 | goto err_free_rq; |
| 9170 | |
| 9171 | init_cfs_rq(cfs_rq); |
| 9172 | init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]); |
Yuyang Du | 540247f | 2015-07-15 08:04:39 +0800 | [diff] [blame] | 9173 | init_entity_runnable_average(se); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9174 | } |
| 9175 | |
| 9176 | return 1; |
| 9177 | |
| 9178 | err_free_rq: |
| 9179 | kfree(cfs_rq); |
| 9180 | err: |
| 9181 | return 0; |
| 9182 | } |
| 9183 | |
Peter Zijlstra | 8663e24 | 2016-06-22 14:58:02 +0200 | [diff] [blame] | 9184 | void online_fair_sched_group(struct task_group *tg) |
| 9185 | { |
| 9186 | struct sched_entity *se; |
| 9187 | struct rq *rq; |
| 9188 | int i; |
| 9189 | |
| 9190 | for_each_possible_cpu(i) { |
| 9191 | rq = cpu_rq(i); |
| 9192 | se = tg->se[i]; |
| 9193 | |
| 9194 | raw_spin_lock_irq(&rq->lock); |
| 9195 | post_init_entity_util_avg(se); |
Peter Zijlstra | 55e16d3 | 2016-06-22 15:14:26 +0200 | [diff] [blame] | 9196 | sync_throttle(tg, i); |
Peter Zijlstra | 8663e24 | 2016-06-22 14:58:02 +0200 | [diff] [blame] | 9197 | raw_spin_unlock_irq(&rq->lock); |
| 9198 | } |
| 9199 | } |
| 9200 | |
Peter Zijlstra | 6fe1f34 | 2016-01-21 22:24:16 +0100 | [diff] [blame] | 9201 | void unregister_fair_sched_group(struct task_group *tg) |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9202 | { |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9203 | unsigned long flags; |
Peter Zijlstra | 6fe1f34 | 2016-01-21 22:24:16 +0100 | [diff] [blame] | 9204 | struct rq *rq; |
| 9205 | int cpu; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9206 | |
Peter Zijlstra | 6fe1f34 | 2016-01-21 22:24:16 +0100 | [diff] [blame] | 9207 | for_each_possible_cpu(cpu) { |
| 9208 | if (tg->se[cpu]) |
| 9209 | remove_entity_load_avg(tg->se[cpu]); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9210 | |
Peter Zijlstra | 6fe1f34 | 2016-01-21 22:24:16 +0100 | [diff] [blame] | 9211 | /* |
| 9212 | * Only empty task groups can be destroyed; so we can speculatively |
| 9213 | * check on_list without danger of it being re-added. |
| 9214 | */ |
| 9215 | if (!tg->cfs_rq[cpu]->on_list) |
| 9216 | continue; |
| 9217 | |
| 9218 | rq = cpu_rq(cpu); |
| 9219 | |
| 9220 | raw_spin_lock_irqsave(&rq->lock, flags); |
| 9221 | list_del_leaf_cfs_rq(tg->cfs_rq[cpu]); |
| 9222 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
| 9223 | } |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9224 | } |
| 9225 | |
| 9226 | void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq, |
| 9227 | struct sched_entity *se, int cpu, |
| 9228 | struct sched_entity *parent) |
| 9229 | { |
| 9230 | struct rq *rq = cpu_rq(cpu); |
| 9231 | |
| 9232 | cfs_rq->tg = tg; |
| 9233 | cfs_rq->rq = rq; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9234 | init_cfs_rq_runtime(cfs_rq); |
| 9235 | |
| 9236 | tg->cfs_rq[cpu] = cfs_rq; |
| 9237 | tg->se[cpu] = se; |
| 9238 | |
| 9239 | /* se could be NULL for root_task_group */ |
| 9240 | if (!se) |
| 9241 | return; |
| 9242 | |
Peter Zijlstra | fed14d4 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 9243 | if (!parent) { |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9244 | se->cfs_rq = &rq->cfs; |
Peter Zijlstra | fed14d4 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 9245 | se->depth = 0; |
| 9246 | } else { |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9247 | se->cfs_rq = parent->my_q; |
Peter Zijlstra | fed14d4 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 9248 | se->depth = parent->depth + 1; |
| 9249 | } |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9250 | |
| 9251 | se->my_q = cfs_rq; |
Paul Turner | 0ac9b1c | 2013-10-16 11:16:27 -0700 | [diff] [blame] | 9252 | /* guarantee group entities always have weight */ |
| 9253 | update_load_set(&se->load, NICE_0_LOAD); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9254 | se->parent = parent; |
| 9255 | } |
| 9256 | |
| 9257 | static DEFINE_MUTEX(shares_mutex); |
| 9258 | |
| 9259 | int sched_group_set_shares(struct task_group *tg, unsigned long shares) |
| 9260 | { |
| 9261 | int i; |
| 9262 | unsigned long flags; |
| 9263 | |
| 9264 | /* |
| 9265 | * We can't change the weight of the root cgroup. |
| 9266 | */ |
| 9267 | if (!tg->se[0]) |
| 9268 | return -EINVAL; |
| 9269 | |
| 9270 | shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES)); |
| 9271 | |
| 9272 | mutex_lock(&shares_mutex); |
| 9273 | if (tg->shares == shares) |
| 9274 | goto done; |
| 9275 | |
| 9276 | tg->shares = shares; |
| 9277 | for_each_possible_cpu(i) { |
| 9278 | struct rq *rq = cpu_rq(i); |
| 9279 | struct sched_entity *se; |
| 9280 | |
| 9281 | se = tg->se[i]; |
| 9282 | /* Propagate contribution to hierarchy */ |
| 9283 | raw_spin_lock_irqsave(&rq->lock, flags); |
Frederic Weisbecker | 71b1da4 | 2013-04-12 01:50:59 +0200 | [diff] [blame] | 9284 | |
| 9285 | /* Possible calls to update_curr() need rq clock */ |
| 9286 | update_rq_clock(rq); |
Linus Torvalds | 17bc14b | 2012-12-14 07:20:43 -0800 | [diff] [blame] | 9287 | for_each_sched_entity(se) |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9288 | update_cfs_shares(group_cfs_rq(se)); |
| 9289 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
| 9290 | } |
| 9291 | |
| 9292 | done: |
| 9293 | mutex_unlock(&shares_mutex); |
| 9294 | return 0; |
| 9295 | } |
| 9296 | #else /* CONFIG_FAIR_GROUP_SCHED */ |
| 9297 | |
| 9298 | void free_fair_sched_group(struct task_group *tg) { } |
| 9299 | |
| 9300 | int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent) |
| 9301 | { |
| 9302 | return 1; |
| 9303 | } |
| 9304 | |
Peter Zijlstra | 8663e24 | 2016-06-22 14:58:02 +0200 | [diff] [blame] | 9305 | void online_fair_sched_group(struct task_group *tg) { } |
| 9306 | |
Peter Zijlstra | 6fe1f34 | 2016-01-21 22:24:16 +0100 | [diff] [blame] | 9307 | void unregister_fair_sched_group(struct task_group *tg) { } |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9308 | |
| 9309 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
| 9310 | |
Peter Zijlstra | 810b381 | 2008-02-29 15:21:01 -0500 | [diff] [blame] | 9311 | |
H Hartley Sweeten | 6d686f4 | 2010-01-13 20:21:52 -0700 | [diff] [blame] | 9312 | static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task) |
Peter Williams | 0d721ce | 2009-09-21 01:31:53 +0000 | [diff] [blame] | 9313 | { |
| 9314 | struct sched_entity *se = &task->se; |
Peter Williams | 0d721ce | 2009-09-21 01:31:53 +0000 | [diff] [blame] | 9315 | unsigned int rr_interval = 0; |
| 9316 | |
| 9317 | /* |
| 9318 | * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise |
| 9319 | * idle runqueue: |
| 9320 | */ |
Peter Williams | 0d721ce | 2009-09-21 01:31:53 +0000 | [diff] [blame] | 9321 | if (rq->cfs.load.weight) |
Zhu Yanhai | a59f4e0 | 2013-01-08 12:56:52 +0800 | [diff] [blame] | 9322 | rr_interval = NS_TO_JIFFIES(sched_slice(cfs_rq_of(se), se)); |
Peter Williams | 0d721ce | 2009-09-21 01:31:53 +0000 | [diff] [blame] | 9323 | |
| 9324 | return rr_interval; |
| 9325 | } |
| 9326 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 9327 | /* |
| 9328 | * All the scheduling class methods: |
| 9329 | */ |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9330 | const struct sched_class fair_sched_class = { |
Ingo Molnar | 5522d5d | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 9331 | .next = &idle_sched_class, |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 9332 | .enqueue_task = enqueue_task_fair, |
| 9333 | .dequeue_task = dequeue_task_fair, |
| 9334 | .yield_task = yield_task_fair, |
Mike Galbraith | d95f412 | 2011-02-01 09:50:51 -0500 | [diff] [blame] | 9335 | .yield_to_task = yield_to_task_fair, |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 9336 | |
Ingo Molnar | 2e09bf5 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 9337 | .check_preempt_curr = check_preempt_wakeup, |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 9338 | |
| 9339 | .pick_next_task = pick_next_task_fair, |
| 9340 | .put_prev_task = put_prev_task_fair, |
| 9341 | |
Peter Williams | 681f3e6 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 9342 | #ifdef CONFIG_SMP |
Li Zefan | 4ce72a2 | 2008-10-22 15:25:26 +0800 | [diff] [blame] | 9343 | .select_task_rq = select_task_rq_fair, |
Paul Turner | 0a74bef | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 9344 | .migrate_task_rq = migrate_task_rq_fair, |
Alex Shi | 141965c | 2013-06-26 13:05:39 +0800 | [diff] [blame] | 9345 | |
Christian Ehrhardt | 0bcdcf2 | 2009-11-30 12:16:46 +0100 | [diff] [blame] | 9346 | .rq_online = rq_online_fair, |
| 9347 | .rq_offline = rq_offline_fair, |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 9348 | |
Yuyang Du | 1269557 | 2015-07-15 08:04:40 +0800 | [diff] [blame] | 9349 | .task_dead = task_dead_fair, |
Peter Zijlstra | c5b2803 | 2015-05-15 17:43:35 +0200 | [diff] [blame] | 9350 | .set_cpus_allowed = set_cpus_allowed_common, |
Peter Williams | 681f3e6 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 9351 | #endif |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 9352 | |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 9353 | .set_curr_task = set_curr_task_fair, |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 9354 | .task_tick = task_tick_fair, |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 9355 | .task_fork = task_fork_fair, |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 9356 | |
| 9357 | .prio_changed = prio_changed_fair, |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 9358 | .switched_from = switched_from_fair, |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 9359 | .switched_to = switched_to_fair, |
Peter Zijlstra | 810b381 | 2008-02-29 15:21:01 -0500 | [diff] [blame] | 9360 | |
Peter Williams | 0d721ce | 2009-09-21 01:31:53 +0000 | [diff] [blame] | 9361 | .get_rr_interval = get_rr_interval_fair, |
| 9362 | |
Stanislaw Gruszka | 6e99891 | 2014-11-12 16:58:44 +0100 | [diff] [blame] | 9363 | .update_curr = update_curr_fair, |
| 9364 | |
Peter Zijlstra | 810b381 | 2008-02-29 15:21:01 -0500 | [diff] [blame] | 9365 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Vincent Guittot | ea86cb4 | 2016-06-17 13:38:55 +0200 | [diff] [blame] | 9366 | .task_change_group = task_change_group_fair, |
Peter Zijlstra | 810b381 | 2008-02-29 15:21:01 -0500 | [diff] [blame] | 9367 | #endif |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 9368 | }; |
| 9369 | |
| 9370 | #ifdef CONFIG_SCHED_DEBUG |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9371 | void print_cfs_stats(struct seq_file *m, int cpu) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 9372 | { |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 9373 | struct cfs_rq *cfs_rq; |
| 9374 | |
Peter Zijlstra | 5973e5b | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 9375 | rcu_read_lock(); |
Ingo Molnar | c3b64f1 | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 9376 | for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq) |
Ingo Molnar | 5cef9ec | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 9377 | print_cfs_rq(m, cpu, cfs_rq); |
Peter Zijlstra | 5973e5b | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 9378 | rcu_read_unlock(); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 9379 | } |
Srikar Dronamraju | 397f237 | 2015-06-25 22:51:43 +0530 | [diff] [blame] | 9380 | |
| 9381 | #ifdef CONFIG_NUMA_BALANCING |
| 9382 | void show_numa_stats(struct task_struct *p, struct seq_file *m) |
| 9383 | { |
| 9384 | int node; |
| 9385 | unsigned long tsf = 0, tpf = 0, gsf = 0, gpf = 0; |
| 9386 | |
| 9387 | for_each_online_node(node) { |
| 9388 | if (p->numa_faults) { |
| 9389 | tsf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 0)]; |
| 9390 | tpf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 1)]; |
| 9391 | } |
| 9392 | if (p->numa_group) { |
| 9393 | gsf = p->numa_group->faults[task_faults_idx(NUMA_MEM, node, 0)], |
| 9394 | gpf = p->numa_group->faults[task_faults_idx(NUMA_MEM, node, 1)]; |
| 9395 | } |
| 9396 | print_numa_stats(m, node, tsf, tpf, gsf, gpf); |
| 9397 | } |
| 9398 | } |
| 9399 | #endif /* CONFIG_NUMA_BALANCING */ |
| 9400 | #endif /* CONFIG_SCHED_DEBUG */ |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9401 | |
| 9402 | __init void init_sched_fair_class(void) |
| 9403 | { |
| 9404 | #ifdef CONFIG_SMP |
| 9405 | open_softirq(SCHED_SOFTIRQ, run_rebalance_domains); |
| 9406 | |
Frederic Weisbecker | 3451d02 | 2011-08-10 23:21:01 +0200 | [diff] [blame] | 9407 | #ifdef CONFIG_NO_HZ_COMMON |
Diwakar Tundlam | 554ceca | 2012-03-07 14:44:26 -0800 | [diff] [blame] | 9408 | nohz.next_balance = jiffies; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9409 | zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 9410 | #endif |
| 9411 | #endif /* SMP */ |
| 9412 | |
| 9413 | } |