Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1 | |
| 2 | #include <linux/sched.h> |
Clark Williams | cf4aebc2 | 2013-02-07 09:46:59 -0600 | [diff] [blame] | 3 | #include <linux/sched/sysctl.h> |
Clark Williams | 8bd75c7 | 2013-02-07 09:47:07 -0600 | [diff] [blame] | 4 | #include <linux/sched/rt.h> |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 5 | #include <linux/mutex.h> |
| 6 | #include <linux/spinlock.h> |
| 7 | #include <linux/stop_machine.h> |
Frederic Weisbecker | 9f3660c | 2013-04-20 14:35:09 +0200 | [diff] [blame] | 8 | #include <linux/tick.h> |
Mel Gorman | f809ca9 | 2013-10-07 11:28:57 +0100 | [diff] [blame] | 9 | #include <linux/slab.h> |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 10 | |
Peter Zijlstra | 391e43d | 2011-11-15 17:14:39 +0100 | [diff] [blame] | 11 | #include "cpupri.h" |
Li Zefan | 60fed78 | 2013-03-29 14:36:43 +0800 | [diff] [blame] | 12 | #include "cpuacct.h" |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 13 | |
Paul Gortmaker | 45ceebf | 2013-04-19 15:10:49 -0400 | [diff] [blame] | 14 | struct rq; |
| 15 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 16 | extern __read_mostly int scheduler_running; |
| 17 | |
Paul Gortmaker | 45ceebf | 2013-04-19 15:10:49 -0400 | [diff] [blame] | 18 | extern unsigned long calc_load_update; |
| 19 | extern atomic_long_t calc_load_tasks; |
| 20 | |
| 21 | extern long calc_load_fold_active(struct rq *this_rq); |
| 22 | extern void update_cpu_load_active(struct rq *this_rq); |
| 23 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 24 | /* |
| 25 | * Convert user-nice values [ -20 ... 0 ... 19 ] |
| 26 | * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ], |
| 27 | * and back. |
| 28 | */ |
| 29 | #define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20) |
| 30 | #define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20) |
| 31 | #define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio) |
| 32 | |
| 33 | /* |
| 34 | * 'User priority' is the nice value converted to something we |
| 35 | * can work with better when scaling various scheduler parameters, |
| 36 | * it's a [ 0 ... 39 ] range. |
| 37 | */ |
| 38 | #define USER_PRIO(p) ((p)-MAX_RT_PRIO) |
| 39 | #define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio) |
| 40 | #define MAX_USER_PRIO (USER_PRIO(MAX_PRIO)) |
| 41 | |
| 42 | /* |
| 43 | * Helpers for converting nanosecond timing to jiffy resolution |
| 44 | */ |
| 45 | #define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ)) |
| 46 | |
Li Zefan | cc1f4b1 | 2013-03-05 16:06:09 +0800 | [diff] [blame] | 47 | /* |
| 48 | * Increase resolution of nice-level calculations for 64-bit architectures. |
| 49 | * The extra resolution improves shares distribution and load balancing of |
| 50 | * low-weight task groups (eg. nice +19 on an autogroup), deeper taskgroup |
| 51 | * hierarchies, especially on larger systems. This is not a user-visible change |
| 52 | * and does not change the user-interface for setting shares/weights. |
| 53 | * |
| 54 | * We increase resolution only if we have enough bits to allow this increased |
| 55 | * resolution (i.e. BITS_PER_LONG > 32). The costs for increasing resolution |
| 56 | * when BITS_PER_LONG <= 32 are pretty high and the returns do not justify the |
| 57 | * increased costs. |
| 58 | */ |
| 59 | #if 0 /* BITS_PER_LONG > 32 -- currently broken: it increases power usage under light load */ |
| 60 | # define SCHED_LOAD_RESOLUTION 10 |
| 61 | # define scale_load(w) ((w) << SCHED_LOAD_RESOLUTION) |
| 62 | # define scale_load_down(w) ((w) >> SCHED_LOAD_RESOLUTION) |
| 63 | #else |
| 64 | # define SCHED_LOAD_RESOLUTION 0 |
| 65 | # define scale_load(w) (w) |
| 66 | # define scale_load_down(w) (w) |
| 67 | #endif |
| 68 | |
| 69 | #define SCHED_LOAD_SHIFT (10 + SCHED_LOAD_RESOLUTION) |
| 70 | #define SCHED_LOAD_SCALE (1L << SCHED_LOAD_SHIFT) |
| 71 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 72 | #define NICE_0_LOAD SCHED_LOAD_SCALE |
| 73 | #define NICE_0_SHIFT SCHED_LOAD_SHIFT |
| 74 | |
| 75 | /* |
| 76 | * These are the 'tuning knobs' of the scheduler: |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 77 | */ |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 78 | |
| 79 | /* |
| 80 | * single value that denotes runtime == period, ie unlimited time. |
| 81 | */ |
| 82 | #define RUNTIME_INF ((u64)~0ULL) |
| 83 | |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame^] | 84 | static inline int fair_policy(int policy) |
| 85 | { |
| 86 | return policy == SCHED_NORMAL || policy == SCHED_BATCH; |
| 87 | } |
| 88 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 89 | static inline int rt_policy(int policy) |
| 90 | { |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame^] | 91 | return policy == SCHED_FIFO || policy == SCHED_RR; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | static inline int task_has_rt_policy(struct task_struct *p) |
| 95 | { |
| 96 | return rt_policy(p->policy); |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * This is the priority-queue data structure of the RT scheduling class: |
| 101 | */ |
| 102 | struct rt_prio_array { |
| 103 | DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */ |
| 104 | struct list_head queue[MAX_RT_PRIO]; |
| 105 | }; |
| 106 | |
| 107 | struct rt_bandwidth { |
| 108 | /* nests inside the rq lock: */ |
| 109 | raw_spinlock_t rt_runtime_lock; |
| 110 | ktime_t rt_period; |
| 111 | u64 rt_runtime; |
| 112 | struct hrtimer rt_period_timer; |
| 113 | }; |
| 114 | |
| 115 | extern struct mutex sched_domains_mutex; |
| 116 | |
| 117 | #ifdef CONFIG_CGROUP_SCHED |
| 118 | |
| 119 | #include <linux/cgroup.h> |
| 120 | |
| 121 | struct cfs_rq; |
| 122 | struct rt_rq; |
| 123 | |
Mike Galbraith | 35cf4e5 | 2012-08-07 05:00:13 +0200 | [diff] [blame] | 124 | extern struct list_head task_groups; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 125 | |
| 126 | struct cfs_bandwidth { |
| 127 | #ifdef CONFIG_CFS_BANDWIDTH |
| 128 | raw_spinlock_t lock; |
| 129 | ktime_t period; |
| 130 | u64 quota, runtime; |
| 131 | s64 hierarchal_quota; |
| 132 | u64 runtime_expires; |
| 133 | |
| 134 | int idle, timer_active; |
| 135 | struct hrtimer period_timer, slack_timer; |
| 136 | struct list_head throttled_cfs_rq; |
| 137 | |
| 138 | /* statistics */ |
| 139 | int nr_periods, nr_throttled; |
| 140 | u64 throttled_time; |
| 141 | #endif |
| 142 | }; |
| 143 | |
| 144 | /* task group related information */ |
| 145 | struct task_group { |
| 146 | struct cgroup_subsys_state css; |
| 147 | |
| 148 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 149 | /* schedulable entities of this group on each cpu */ |
| 150 | struct sched_entity **se; |
| 151 | /* runqueue "owned" by this group on each cpu */ |
| 152 | struct cfs_rq **cfs_rq; |
| 153 | unsigned long shares; |
| 154 | |
Alex Shi | fa6bdde | 2013-06-20 10:18:46 +0800 | [diff] [blame] | 155 | #ifdef CONFIG_SMP |
Alex Shi | bf5b986 | 2013-06-20 10:18:54 +0800 | [diff] [blame] | 156 | atomic_long_t load_avg; |
Paul Turner | bb17f65 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 157 | atomic_t runnable_avg; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 158 | #endif |
Alex Shi | fa6bdde | 2013-06-20 10:18:46 +0800 | [diff] [blame] | 159 | #endif |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 160 | |
| 161 | #ifdef CONFIG_RT_GROUP_SCHED |
| 162 | struct sched_rt_entity **rt_se; |
| 163 | struct rt_rq **rt_rq; |
| 164 | |
| 165 | struct rt_bandwidth rt_bandwidth; |
| 166 | #endif |
| 167 | |
| 168 | struct rcu_head rcu; |
| 169 | struct list_head list; |
| 170 | |
| 171 | struct task_group *parent; |
| 172 | struct list_head siblings; |
| 173 | struct list_head children; |
| 174 | |
| 175 | #ifdef CONFIG_SCHED_AUTOGROUP |
| 176 | struct autogroup *autogroup; |
| 177 | #endif |
| 178 | |
| 179 | struct cfs_bandwidth cfs_bandwidth; |
| 180 | }; |
| 181 | |
| 182 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 183 | #define ROOT_TASK_GROUP_LOAD NICE_0_LOAD |
| 184 | |
| 185 | /* |
| 186 | * A weight of 0 or 1 can cause arithmetics problems. |
| 187 | * A weight of a cfs_rq is the sum of weights of which entities |
| 188 | * are queued on this cfs_rq, so a weight of a entity should not be |
| 189 | * too large, so as the shares value of a task group. |
| 190 | * (The default weight is 1024 - so there's no practical |
| 191 | * limitation from this.) |
| 192 | */ |
| 193 | #define MIN_SHARES (1UL << 1) |
| 194 | #define MAX_SHARES (1UL << 18) |
| 195 | #endif |
| 196 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 197 | typedef int (*tg_visitor)(struct task_group *, void *); |
| 198 | |
| 199 | extern int walk_tg_tree_from(struct task_group *from, |
| 200 | tg_visitor down, tg_visitor up, void *data); |
| 201 | |
| 202 | /* |
| 203 | * Iterate the full tree, calling @down when first entering a node and @up when |
| 204 | * leaving it for the final time. |
| 205 | * |
| 206 | * Caller must hold rcu_lock or sufficient equivalent. |
| 207 | */ |
| 208 | static inline int walk_tg_tree(tg_visitor down, tg_visitor up, void *data) |
| 209 | { |
| 210 | return walk_tg_tree_from(&root_task_group, down, up, data); |
| 211 | } |
| 212 | |
| 213 | extern int tg_nop(struct task_group *tg, void *data); |
| 214 | |
| 215 | extern void free_fair_sched_group(struct task_group *tg); |
| 216 | extern int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent); |
| 217 | extern void unregister_fair_sched_group(struct task_group *tg, int cpu); |
| 218 | extern void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq, |
| 219 | struct sched_entity *se, int cpu, |
| 220 | struct sched_entity *parent); |
| 221 | extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b); |
| 222 | extern int sched_group_set_shares(struct task_group *tg, unsigned long shares); |
| 223 | |
| 224 | extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b); |
| 225 | extern void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b); |
| 226 | extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq); |
| 227 | |
| 228 | extern void free_rt_sched_group(struct task_group *tg); |
| 229 | extern int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent); |
| 230 | extern void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq, |
| 231 | struct sched_rt_entity *rt_se, int cpu, |
| 232 | struct sched_rt_entity *parent); |
| 233 | |
Li Zefan | 25cc7da | 2013-03-05 16:07:33 +0800 | [diff] [blame] | 234 | extern struct task_group *sched_create_group(struct task_group *parent); |
| 235 | extern void sched_online_group(struct task_group *tg, |
| 236 | struct task_group *parent); |
| 237 | extern void sched_destroy_group(struct task_group *tg); |
| 238 | extern void sched_offline_group(struct task_group *tg); |
| 239 | |
| 240 | extern void sched_move_task(struct task_struct *tsk); |
| 241 | |
| 242 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 243 | extern int sched_group_set_shares(struct task_group *tg, unsigned long shares); |
| 244 | #endif |
| 245 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 246 | #else /* CONFIG_CGROUP_SCHED */ |
| 247 | |
| 248 | struct cfs_bandwidth { }; |
| 249 | |
| 250 | #endif /* CONFIG_CGROUP_SCHED */ |
| 251 | |
| 252 | /* CFS-related fields in a runqueue */ |
| 253 | struct cfs_rq { |
| 254 | struct load_weight load; |
Peter Zijlstra | c82513e | 2012-04-26 13:12:27 +0200 | [diff] [blame] | 255 | unsigned int nr_running, h_nr_running; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 256 | |
| 257 | u64 exec_clock; |
| 258 | u64 min_vruntime; |
| 259 | #ifndef CONFIG_64BIT |
| 260 | u64 min_vruntime_copy; |
| 261 | #endif |
| 262 | |
| 263 | struct rb_root tasks_timeline; |
| 264 | struct rb_node *rb_leftmost; |
| 265 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 266 | /* |
| 267 | * 'curr' points to currently running entity on this cfs_rq. |
| 268 | * It is set to NULL otherwise (i.e when none are currently running). |
| 269 | */ |
| 270 | struct sched_entity *curr, *next, *last, *skip; |
| 271 | |
| 272 | #ifdef CONFIG_SCHED_DEBUG |
| 273 | unsigned int nr_spread_over; |
| 274 | #endif |
| 275 | |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 276 | #ifdef CONFIG_SMP |
| 277 | /* |
| 278 | * CFS Load tracking |
| 279 | * Under CFS, load is tracked on a per-entity basis and aggregated up. |
| 280 | * This allows for the description of both thread and group usage (in |
| 281 | * the FAIR_GROUP_SCHED case). |
| 282 | */ |
Alex Shi | 72a4cf2 | 2013-06-20 10:18:53 +0800 | [diff] [blame] | 283 | unsigned long runnable_load_avg, blocked_load_avg; |
Alex Shi | 2509940 | 2013-06-20 10:18:55 +0800 | [diff] [blame] | 284 | atomic64_t decay_counter; |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 285 | u64 last_decay; |
Alex Shi | 2509940 | 2013-06-20 10:18:55 +0800 | [diff] [blame] | 286 | atomic_long_t removed_load; |
Alex Shi | 141965c | 2013-06-26 13:05:39 +0800 | [diff] [blame] | 287 | |
Paul Turner | c566e8e | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 288 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Alex Shi | 141965c | 2013-06-26 13:05:39 +0800 | [diff] [blame] | 289 | /* Required to track per-cpu representation of a task_group */ |
Paul Turner | bb17f65 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 290 | u32 tg_runnable_contrib; |
Alex Shi | bf5b986 | 2013-06-20 10:18:54 +0800 | [diff] [blame] | 291 | unsigned long tg_load_contrib; |
Paul Turner | 8295836 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 292 | |
| 293 | /* |
| 294 | * h_load = weight * f(tg) |
| 295 | * |
| 296 | * Where f(tg) is the recursive weight fraction assigned to |
| 297 | * this group. |
| 298 | */ |
| 299 | unsigned long h_load; |
Vladimir Davydov | 6852079 | 2013-07-15 17:49:19 +0400 | [diff] [blame] | 300 | u64 last_h_load_update; |
| 301 | struct sched_entity *h_load_next; |
| 302 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
Paul Turner | 8295836 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 303 | #endif /* CONFIG_SMP */ |
| 304 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 305 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 306 | struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */ |
| 307 | |
| 308 | /* |
| 309 | * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in |
| 310 | * a hierarchy). Non-leaf lrqs hold other higher schedulable entities |
| 311 | * (like users, containers etc.) |
| 312 | * |
| 313 | * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This |
| 314 | * list is used during load balance. |
| 315 | */ |
| 316 | int on_list; |
| 317 | struct list_head leaf_cfs_rq_list; |
| 318 | struct task_group *tg; /* group that "owns" this runqueue */ |
| 319 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 320 | #ifdef CONFIG_CFS_BANDWIDTH |
| 321 | int runtime_enabled; |
| 322 | u64 runtime_expires; |
| 323 | s64 runtime_remaining; |
| 324 | |
Paul Turner | f1b1728 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 325 | u64 throttled_clock, throttled_clock_task; |
| 326 | u64 throttled_clock_task_time; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 327 | int throttled, throttle_count; |
| 328 | struct list_head throttled_list; |
| 329 | #endif /* CONFIG_CFS_BANDWIDTH */ |
| 330 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
| 331 | }; |
| 332 | |
| 333 | static inline int rt_bandwidth_enabled(void) |
| 334 | { |
| 335 | return sysctl_sched_rt_runtime >= 0; |
| 336 | } |
| 337 | |
| 338 | /* Real-Time classes' related field in a runqueue: */ |
| 339 | struct rt_rq { |
| 340 | struct rt_prio_array active; |
Peter Zijlstra | c82513e | 2012-04-26 13:12:27 +0200 | [diff] [blame] | 341 | unsigned int rt_nr_running; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 342 | #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED |
| 343 | struct { |
| 344 | int curr; /* highest queued rt task prio */ |
| 345 | #ifdef CONFIG_SMP |
| 346 | int next; /* next highest */ |
| 347 | #endif |
| 348 | } highest_prio; |
| 349 | #endif |
| 350 | #ifdef CONFIG_SMP |
| 351 | unsigned long rt_nr_migratory; |
| 352 | unsigned long rt_nr_total; |
| 353 | int overloaded; |
| 354 | struct plist_head pushable_tasks; |
| 355 | #endif |
| 356 | int rt_throttled; |
| 357 | u64 rt_time; |
| 358 | u64 rt_runtime; |
| 359 | /* Nests inside the rq lock: */ |
| 360 | raw_spinlock_t rt_runtime_lock; |
| 361 | |
| 362 | #ifdef CONFIG_RT_GROUP_SCHED |
| 363 | unsigned long rt_nr_boosted; |
| 364 | |
| 365 | struct rq *rq; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 366 | struct task_group *tg; |
| 367 | #endif |
| 368 | }; |
| 369 | |
| 370 | #ifdef CONFIG_SMP |
| 371 | |
| 372 | /* |
| 373 | * We add the notion of a root-domain which will be used to define per-domain |
| 374 | * variables. Each exclusive cpuset essentially defines an island domain by |
| 375 | * fully partitioning the member cpus from any other cpuset. Whenever a new |
| 376 | * exclusive cpuset is created, we also create and attach a new root-domain |
| 377 | * object. |
| 378 | * |
| 379 | */ |
| 380 | struct root_domain { |
| 381 | atomic_t refcount; |
| 382 | atomic_t rto_count; |
| 383 | struct rcu_head rcu; |
| 384 | cpumask_var_t span; |
| 385 | cpumask_var_t online; |
| 386 | |
| 387 | /* |
| 388 | * The "RT overload" flag: it gets set if a CPU has more than |
| 389 | * one runnable RT task. |
| 390 | */ |
| 391 | cpumask_var_t rto_mask; |
| 392 | struct cpupri cpupri; |
| 393 | }; |
| 394 | |
| 395 | extern struct root_domain def_root_domain; |
| 396 | |
| 397 | #endif /* CONFIG_SMP */ |
| 398 | |
| 399 | /* |
| 400 | * This is the main, per-CPU runqueue data structure. |
| 401 | * |
| 402 | * Locking rule: those places that want to lock multiple runqueues |
| 403 | * (such as the load balancing or the thread migration code), lock |
| 404 | * acquire operations must be ordered by ascending &runqueue. |
| 405 | */ |
| 406 | struct rq { |
| 407 | /* runqueue lock: */ |
| 408 | raw_spinlock_t lock; |
| 409 | |
| 410 | /* |
| 411 | * nr_running and cpu_load should be in the same cacheline because |
| 412 | * remote CPUs use both these fields when doing load calculation. |
| 413 | */ |
Peter Zijlstra | c82513e | 2012-04-26 13:12:27 +0200 | [diff] [blame] | 414 | unsigned int nr_running; |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 415 | #ifdef CONFIG_NUMA_BALANCING |
| 416 | unsigned int nr_numa_running; |
| 417 | unsigned int nr_preferred_running; |
| 418 | #endif |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 419 | #define CPU_LOAD_IDX_MAX 5 |
| 420 | unsigned long cpu_load[CPU_LOAD_IDX_MAX]; |
| 421 | unsigned long last_load_update_tick; |
Frederic Weisbecker | 3451d02 | 2011-08-10 23:21:01 +0200 | [diff] [blame] | 422 | #ifdef CONFIG_NO_HZ_COMMON |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 423 | u64 nohz_stamp; |
Suresh Siddha | 1c792db | 2011-12-01 17:07:32 -0800 | [diff] [blame] | 424 | unsigned long nohz_flags; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 425 | #endif |
Frederic Weisbecker | 265f22a | 2013-05-03 03:39:05 +0200 | [diff] [blame] | 426 | #ifdef CONFIG_NO_HZ_FULL |
| 427 | unsigned long last_sched_tick; |
| 428 | #endif |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 429 | int skip_clock_update; |
| 430 | |
| 431 | /* capture load from *all* tasks on this cpu: */ |
| 432 | struct load_weight load; |
| 433 | unsigned long nr_load_updates; |
| 434 | u64 nr_switches; |
| 435 | |
| 436 | struct cfs_rq cfs; |
| 437 | struct rt_rq rt; |
| 438 | |
| 439 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 440 | /* list of leaf cfs_rq on this cpu: */ |
| 441 | struct list_head leaf_cfs_rq_list; |
Peter Zijlstra | a35b646 | 2012-08-08 21:46:40 +0200 | [diff] [blame] | 442 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
| 443 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 444 | #ifdef CONFIG_RT_GROUP_SCHED |
| 445 | struct list_head leaf_rt_rq_list; |
| 446 | #endif |
| 447 | |
| 448 | /* |
| 449 | * This is part of a global counter where only the total sum |
| 450 | * over all CPUs matters. A task can increase this counter on |
| 451 | * one CPU and if it got migrated afterwards it may decrease |
| 452 | * it on another CPU. Always updated under the runqueue lock: |
| 453 | */ |
| 454 | unsigned long nr_uninterruptible; |
| 455 | |
| 456 | struct task_struct *curr, *idle, *stop; |
| 457 | unsigned long next_balance; |
| 458 | struct mm_struct *prev_mm; |
| 459 | |
| 460 | u64 clock; |
| 461 | u64 clock_task; |
| 462 | |
| 463 | atomic_t nr_iowait; |
| 464 | |
| 465 | #ifdef CONFIG_SMP |
| 466 | struct root_domain *rd; |
| 467 | struct sched_domain *sd; |
| 468 | |
| 469 | unsigned long cpu_power; |
| 470 | |
| 471 | unsigned char idle_balance; |
| 472 | /* For active balancing */ |
| 473 | int post_schedule; |
| 474 | int active_balance; |
| 475 | int push_cpu; |
| 476 | struct cpu_stop_work active_balance_work; |
| 477 | /* cpu of this runqueue: */ |
| 478 | int cpu; |
| 479 | int online; |
| 480 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 481 | struct list_head cfs_tasks; |
| 482 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 483 | u64 rt_avg; |
| 484 | u64 age_stamp; |
| 485 | u64 idle_stamp; |
| 486 | u64 avg_idle; |
Jason Low | 9bd721c | 2013-09-13 11:26:52 -0700 | [diff] [blame] | 487 | |
| 488 | /* This is used to determine avg_idle's max value */ |
| 489 | u64 max_idle_balance_cost; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 490 | #endif |
| 491 | |
| 492 | #ifdef CONFIG_IRQ_TIME_ACCOUNTING |
| 493 | u64 prev_irq_time; |
| 494 | #endif |
| 495 | #ifdef CONFIG_PARAVIRT |
| 496 | u64 prev_steal_time; |
| 497 | #endif |
| 498 | #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING |
| 499 | u64 prev_steal_time_rq; |
| 500 | #endif |
| 501 | |
| 502 | /* calc_load related fields */ |
| 503 | unsigned long calc_load_update; |
| 504 | long calc_load_active; |
| 505 | |
| 506 | #ifdef CONFIG_SCHED_HRTICK |
| 507 | #ifdef CONFIG_SMP |
| 508 | int hrtick_csd_pending; |
| 509 | struct call_single_data hrtick_csd; |
| 510 | #endif |
| 511 | struct hrtimer hrtick_timer; |
| 512 | #endif |
| 513 | |
| 514 | #ifdef CONFIG_SCHEDSTATS |
| 515 | /* latency stats */ |
| 516 | struct sched_info rq_sched_info; |
| 517 | unsigned long long rq_cpu_time; |
| 518 | /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */ |
| 519 | |
| 520 | /* sys_sched_yield() stats */ |
| 521 | unsigned int yld_count; |
| 522 | |
| 523 | /* schedule() stats */ |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 524 | unsigned int sched_count; |
| 525 | unsigned int sched_goidle; |
| 526 | |
| 527 | /* try_to_wake_up() stats */ |
| 528 | unsigned int ttwu_count; |
| 529 | unsigned int ttwu_local; |
| 530 | #endif |
| 531 | |
| 532 | #ifdef CONFIG_SMP |
| 533 | struct llist_head wake_list; |
| 534 | #endif |
Ben Segall | 18bf280 | 2012-10-04 12:51:20 +0200 | [diff] [blame] | 535 | |
| 536 | struct sched_avg avg; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 537 | }; |
| 538 | |
| 539 | static inline int cpu_of(struct rq *rq) |
| 540 | { |
| 541 | #ifdef CONFIG_SMP |
| 542 | return rq->cpu; |
| 543 | #else |
| 544 | return 0; |
| 545 | #endif |
| 546 | } |
| 547 | |
| 548 | DECLARE_PER_CPU(struct rq, runqueues); |
| 549 | |
Peter Zijlstra | 518cd62 | 2011-12-07 15:07:31 +0100 | [diff] [blame] | 550 | #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu))) |
| 551 | #define this_rq() (&__get_cpu_var(runqueues)) |
| 552 | #define task_rq(p) cpu_rq(task_cpu(p)) |
| 553 | #define cpu_curr(cpu) (cpu_rq(cpu)->curr) |
| 554 | #define raw_rq() (&__raw_get_cpu_var(runqueues)) |
| 555 | |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 556 | static inline u64 rq_clock(struct rq *rq) |
| 557 | { |
| 558 | return rq->clock; |
| 559 | } |
| 560 | |
| 561 | static inline u64 rq_clock_task(struct rq *rq) |
| 562 | { |
| 563 | return rq->clock_task; |
| 564 | } |
| 565 | |
Mel Gorman | f809ca9 | 2013-10-07 11:28:57 +0100 | [diff] [blame] | 566 | #ifdef CONFIG_NUMA_BALANCING |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 567 | extern void sched_setnuma(struct task_struct *p, int node); |
Mel Gorman | e6628d5 | 2013-10-07 11:29:02 +0100 | [diff] [blame] | 568 | extern int migrate_task_to(struct task_struct *p, int cpu); |
Peter Zijlstra | ac66f54 | 2013-10-07 11:29:16 +0100 | [diff] [blame] | 569 | extern int migrate_swap(struct task_struct *, struct task_struct *); |
Mel Gorman | f809ca9 | 2013-10-07 11:28:57 +0100 | [diff] [blame] | 570 | #endif /* CONFIG_NUMA_BALANCING */ |
| 571 | |
Peter Zijlstra | 518cd62 | 2011-12-07 15:07:31 +0100 | [diff] [blame] | 572 | #ifdef CONFIG_SMP |
| 573 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 574 | #define rcu_dereference_check_sched_domain(p) \ |
| 575 | rcu_dereference_check((p), \ |
| 576 | lockdep_is_held(&sched_domains_mutex)) |
| 577 | |
| 578 | /* |
| 579 | * The domain tree (rq->sd) is protected by RCU's quiescent state transition. |
| 580 | * See detach_destroy_domains: synchronize_sched for details. |
| 581 | * |
| 582 | * The domain tree of any CPU may only be accessed from within |
| 583 | * preempt-disabled sections. |
| 584 | */ |
| 585 | #define for_each_domain(cpu, __sd) \ |
Peter Zijlstra | 518cd62 | 2011-12-07 15:07:31 +0100 | [diff] [blame] | 586 | for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \ |
| 587 | __sd; __sd = __sd->parent) |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 588 | |
Suresh Siddha | 77e8136 | 2011-11-17 11:08:23 -0800 | [diff] [blame] | 589 | #define for_each_lower_domain(sd) for (; sd; sd = sd->child) |
| 590 | |
Peter Zijlstra | 518cd62 | 2011-12-07 15:07:31 +0100 | [diff] [blame] | 591 | /** |
| 592 | * highest_flag_domain - Return highest sched_domain containing flag. |
| 593 | * @cpu: The cpu whose highest level of sched domain is to |
| 594 | * be returned. |
| 595 | * @flag: The flag to check for the highest sched_domain |
| 596 | * for the given cpu. |
| 597 | * |
| 598 | * Returns the highest sched_domain of a cpu which contains the given flag. |
| 599 | */ |
| 600 | static inline struct sched_domain *highest_flag_domain(int cpu, int flag) |
| 601 | { |
| 602 | struct sched_domain *sd, *hsd = NULL; |
| 603 | |
| 604 | for_each_domain(cpu, sd) { |
| 605 | if (!(sd->flags & flag)) |
| 606 | break; |
| 607 | hsd = sd; |
| 608 | } |
| 609 | |
| 610 | return hsd; |
| 611 | } |
| 612 | |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 613 | static inline struct sched_domain *lowest_flag_domain(int cpu, int flag) |
| 614 | { |
| 615 | struct sched_domain *sd; |
| 616 | |
| 617 | for_each_domain(cpu, sd) { |
| 618 | if (sd->flags & flag) |
| 619 | break; |
| 620 | } |
| 621 | |
| 622 | return sd; |
| 623 | } |
| 624 | |
Peter Zijlstra | 518cd62 | 2011-12-07 15:07:31 +0100 | [diff] [blame] | 625 | DECLARE_PER_CPU(struct sched_domain *, sd_llc); |
Peter Zijlstra | 7d9ffa8 | 2013-07-04 12:56:46 +0800 | [diff] [blame] | 626 | DECLARE_PER_CPU(int, sd_llc_size); |
Peter Zijlstra | 518cd62 | 2011-12-07 15:07:31 +0100 | [diff] [blame] | 627 | DECLARE_PER_CPU(int, sd_llc_id); |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 628 | DECLARE_PER_CPU(struct sched_domain *, sd_numa); |
Preeti U Murthy | 37dc6b5 | 2013-10-30 08:42:52 +0530 | [diff] [blame] | 629 | DECLARE_PER_CPU(struct sched_domain *, sd_busy); |
| 630 | DECLARE_PER_CPU(struct sched_domain *, sd_asym); |
Peter Zijlstra | 518cd62 | 2011-12-07 15:07:31 +0100 | [diff] [blame] | 631 | |
Li Zefan | 5e6521e | 2013-03-05 16:06:23 +0800 | [diff] [blame] | 632 | struct sched_group_power { |
| 633 | atomic_t ref; |
| 634 | /* |
| 635 | * CPU power of this group, SCHED_LOAD_SCALE being max power for a |
| 636 | * single CPU. |
| 637 | */ |
| 638 | unsigned int power, power_orig; |
| 639 | unsigned long next_update; |
Peter Zijlstra | 6263322 | 2013-08-19 12:41:09 +0200 | [diff] [blame] | 640 | int imbalance; /* XXX unrelated to power but shared group state */ |
Li Zefan | 5e6521e | 2013-03-05 16:06:23 +0800 | [diff] [blame] | 641 | /* |
| 642 | * Number of busy cpus in this group. |
| 643 | */ |
| 644 | atomic_t nr_busy_cpus; |
| 645 | |
| 646 | unsigned long cpumask[0]; /* iteration mask */ |
| 647 | }; |
| 648 | |
| 649 | struct sched_group { |
| 650 | struct sched_group *next; /* Must be a circular list */ |
| 651 | atomic_t ref; |
| 652 | |
| 653 | unsigned int group_weight; |
| 654 | struct sched_group_power *sgp; |
| 655 | |
| 656 | /* |
| 657 | * The CPUs this group covers. |
| 658 | * |
| 659 | * NOTE: this field is variable length. (Allocated dynamically |
| 660 | * by attaching extra space to the end of the structure, |
| 661 | * depending on how many CPUs the kernel has booted up with) |
| 662 | */ |
| 663 | unsigned long cpumask[0]; |
| 664 | }; |
| 665 | |
| 666 | static inline struct cpumask *sched_group_cpus(struct sched_group *sg) |
| 667 | { |
| 668 | return to_cpumask(sg->cpumask); |
| 669 | } |
| 670 | |
| 671 | /* |
| 672 | * cpumask masking which cpus in the group are allowed to iterate up the domain |
| 673 | * tree. |
| 674 | */ |
| 675 | static inline struct cpumask *sched_group_mask(struct sched_group *sg) |
| 676 | { |
| 677 | return to_cpumask(sg->sgp->cpumask); |
| 678 | } |
| 679 | |
| 680 | /** |
| 681 | * group_first_cpu - Returns the first cpu in the cpumask of a sched_group. |
| 682 | * @group: The group whose first cpu is to be returned. |
| 683 | */ |
| 684 | static inline unsigned int group_first_cpu(struct sched_group *group) |
| 685 | { |
| 686 | return cpumask_first(sched_group_cpus(group)); |
| 687 | } |
| 688 | |
Peter Zijlstra | c117487 | 2012-05-31 14:47:33 +0200 | [diff] [blame] | 689 | extern int group_balance_cpu(struct sched_group *sg); |
| 690 | |
Peter Zijlstra | 518cd62 | 2011-12-07 15:07:31 +0100 | [diff] [blame] | 691 | #endif /* CONFIG_SMP */ |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 692 | |
Peter Zijlstra | 391e43d | 2011-11-15 17:14:39 +0100 | [diff] [blame] | 693 | #include "stats.h" |
| 694 | #include "auto_group.h" |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 695 | |
| 696 | #ifdef CONFIG_CGROUP_SCHED |
| 697 | |
| 698 | /* |
| 699 | * Return the group to which this tasks belongs. |
| 700 | * |
Tejun Heo | 8af01f5 | 2013-08-08 20:11:22 -0400 | [diff] [blame] | 701 | * We cannot use task_css() and friends because the cgroup subsystem |
| 702 | * changes that value before the cgroup_subsys::attach() method is called, |
| 703 | * therefore we cannot pin it and might observe the wrong value. |
Peter Zijlstra | 8323f26 | 2012-06-22 13:36:05 +0200 | [diff] [blame] | 704 | * |
| 705 | * The same is true for autogroup's p->signal->autogroup->tg, the autogroup |
| 706 | * core changes this before calling sched_move_task(). |
| 707 | * |
| 708 | * Instead we use a 'copy' which is updated from sched_move_task() while |
| 709 | * holding both task_struct::pi_lock and rq::lock. |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 710 | */ |
| 711 | static inline struct task_group *task_group(struct task_struct *p) |
| 712 | { |
Peter Zijlstra | 8323f26 | 2012-06-22 13:36:05 +0200 | [diff] [blame] | 713 | return p->sched_task_group; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */ |
| 717 | static inline void set_task_rq(struct task_struct *p, unsigned int cpu) |
| 718 | { |
| 719 | #if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED) |
| 720 | struct task_group *tg = task_group(p); |
| 721 | #endif |
| 722 | |
| 723 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 724 | p->se.cfs_rq = tg->cfs_rq[cpu]; |
| 725 | p->se.parent = tg->se[cpu]; |
| 726 | #endif |
| 727 | |
| 728 | #ifdef CONFIG_RT_GROUP_SCHED |
| 729 | p->rt.rt_rq = tg->rt_rq[cpu]; |
| 730 | p->rt.parent = tg->rt_se[cpu]; |
| 731 | #endif |
| 732 | } |
| 733 | |
| 734 | #else /* CONFIG_CGROUP_SCHED */ |
| 735 | |
| 736 | static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { } |
| 737 | static inline struct task_group *task_group(struct task_struct *p) |
| 738 | { |
| 739 | return NULL; |
| 740 | } |
| 741 | |
| 742 | #endif /* CONFIG_CGROUP_SCHED */ |
| 743 | |
| 744 | static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu) |
| 745 | { |
| 746 | set_task_rq(p, cpu); |
| 747 | #ifdef CONFIG_SMP |
| 748 | /* |
| 749 | * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be |
| 750 | * successfuly executed on another CPU. We must ensure that updates of |
| 751 | * per-task data have been completed by this moment. |
| 752 | */ |
| 753 | smp_wmb(); |
| 754 | task_thread_info(p)->cpu = cpu; |
Peter Zijlstra | ac66f54 | 2013-10-07 11:29:16 +0100 | [diff] [blame] | 755 | p->wake_cpu = cpu; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 756 | #endif |
| 757 | } |
| 758 | |
| 759 | /* |
| 760 | * Tunables that become constants when CONFIG_SCHED_DEBUG is off: |
| 761 | */ |
| 762 | #ifdef CONFIG_SCHED_DEBUG |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 763 | # include <linux/static_key.h> |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 764 | # define const_debug __read_mostly |
| 765 | #else |
| 766 | # define const_debug const |
| 767 | #endif |
| 768 | |
| 769 | extern const_debug unsigned int sysctl_sched_features; |
| 770 | |
| 771 | #define SCHED_FEAT(name, enabled) \ |
| 772 | __SCHED_FEAT_##name , |
| 773 | |
| 774 | enum { |
Peter Zijlstra | 391e43d | 2011-11-15 17:14:39 +0100 | [diff] [blame] | 775 | #include "features.h" |
Peter Zijlstra | f8b6d1c | 2011-07-06 14:20:14 +0200 | [diff] [blame] | 776 | __SCHED_FEAT_NR, |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 777 | }; |
| 778 | |
| 779 | #undef SCHED_FEAT |
| 780 | |
Peter Zijlstra | f8b6d1c | 2011-07-06 14:20:14 +0200 | [diff] [blame] | 781 | #if defined(CONFIG_SCHED_DEBUG) && defined(HAVE_JUMP_LABEL) |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 782 | static __always_inline bool static_branch__true(struct static_key *key) |
Peter Zijlstra | f8b6d1c | 2011-07-06 14:20:14 +0200 | [diff] [blame] | 783 | { |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 784 | return static_key_true(key); /* Not out of line branch. */ |
Peter Zijlstra | f8b6d1c | 2011-07-06 14:20:14 +0200 | [diff] [blame] | 785 | } |
| 786 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 787 | static __always_inline bool static_branch__false(struct static_key *key) |
Peter Zijlstra | f8b6d1c | 2011-07-06 14:20:14 +0200 | [diff] [blame] | 788 | { |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 789 | return static_key_false(key); /* Out of line branch. */ |
Peter Zijlstra | f8b6d1c | 2011-07-06 14:20:14 +0200 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | #define SCHED_FEAT(name, enabled) \ |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 793 | static __always_inline bool static_branch_##name(struct static_key *key) \ |
Peter Zijlstra | f8b6d1c | 2011-07-06 14:20:14 +0200 | [diff] [blame] | 794 | { \ |
| 795 | return static_branch__##enabled(key); \ |
| 796 | } |
| 797 | |
| 798 | #include "features.h" |
| 799 | |
| 800 | #undef SCHED_FEAT |
| 801 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 802 | extern struct static_key sched_feat_keys[__SCHED_FEAT_NR]; |
Peter Zijlstra | f8b6d1c | 2011-07-06 14:20:14 +0200 | [diff] [blame] | 803 | #define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x])) |
| 804 | #else /* !(SCHED_DEBUG && HAVE_JUMP_LABEL) */ |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 805 | #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x)) |
Peter Zijlstra | f8b6d1c | 2011-07-06 14:20:14 +0200 | [diff] [blame] | 806 | #endif /* SCHED_DEBUG && HAVE_JUMP_LABEL */ |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 807 | |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 808 | #ifdef CONFIG_NUMA_BALANCING |
| 809 | #define sched_feat_numa(x) sched_feat(x) |
Mel Gorman | 3105b86 | 2012-11-23 11:23:49 +0000 | [diff] [blame] | 810 | #ifdef CONFIG_SCHED_DEBUG |
| 811 | #define numabalancing_enabled sched_feat_numa(NUMA) |
| 812 | #else |
| 813 | extern bool numabalancing_enabled; |
| 814 | #endif /* CONFIG_SCHED_DEBUG */ |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 815 | #else |
| 816 | #define sched_feat_numa(x) (0) |
Mel Gorman | 3105b86 | 2012-11-23 11:23:49 +0000 | [diff] [blame] | 817 | #define numabalancing_enabled (0) |
| 818 | #endif /* CONFIG_NUMA_BALANCING */ |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 819 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 820 | static inline u64 global_rt_period(void) |
| 821 | { |
| 822 | return (u64)sysctl_sched_rt_period * NSEC_PER_USEC; |
| 823 | } |
| 824 | |
| 825 | static inline u64 global_rt_runtime(void) |
| 826 | { |
| 827 | if (sysctl_sched_rt_runtime < 0) |
| 828 | return RUNTIME_INF; |
| 829 | |
| 830 | return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC; |
| 831 | } |
| 832 | |
| 833 | |
| 834 | |
| 835 | static inline int task_current(struct rq *rq, struct task_struct *p) |
| 836 | { |
| 837 | return rq->curr == p; |
| 838 | } |
| 839 | |
| 840 | static inline int task_running(struct rq *rq, struct task_struct *p) |
| 841 | { |
| 842 | #ifdef CONFIG_SMP |
| 843 | return p->on_cpu; |
| 844 | #else |
| 845 | return task_current(rq, p); |
| 846 | #endif |
| 847 | } |
| 848 | |
| 849 | |
| 850 | #ifndef prepare_arch_switch |
| 851 | # define prepare_arch_switch(next) do { } while (0) |
| 852 | #endif |
| 853 | #ifndef finish_arch_switch |
| 854 | # define finish_arch_switch(prev) do { } while (0) |
| 855 | #endif |
Catalin Marinas | 01f23e1 | 2011-11-27 21:43:10 +0000 | [diff] [blame] | 856 | #ifndef finish_arch_post_lock_switch |
| 857 | # define finish_arch_post_lock_switch() do { } while (0) |
| 858 | #endif |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 859 | |
| 860 | #ifndef __ARCH_WANT_UNLOCKED_CTXSW |
| 861 | static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next) |
| 862 | { |
| 863 | #ifdef CONFIG_SMP |
| 864 | /* |
| 865 | * We can optimise this out completely for !SMP, because the |
| 866 | * SMP rebalancing from interrupt is the only thing that cares |
| 867 | * here. |
| 868 | */ |
| 869 | next->on_cpu = 1; |
| 870 | #endif |
| 871 | } |
| 872 | |
| 873 | static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev) |
| 874 | { |
| 875 | #ifdef CONFIG_SMP |
| 876 | /* |
| 877 | * After ->on_cpu is cleared, the task can be moved to a different CPU. |
| 878 | * We must ensure this doesn't happen until the switch is completely |
| 879 | * finished. |
| 880 | */ |
| 881 | smp_wmb(); |
| 882 | prev->on_cpu = 0; |
| 883 | #endif |
| 884 | #ifdef CONFIG_DEBUG_SPINLOCK |
| 885 | /* this is a valid case when another task releases the spinlock */ |
| 886 | rq->lock.owner = current; |
| 887 | #endif |
| 888 | /* |
| 889 | * If we are tracking spinlock dependencies then we have to |
| 890 | * fix up the runqueue lock - which gets 'carried over' from |
| 891 | * prev into current: |
| 892 | */ |
| 893 | spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_); |
| 894 | |
| 895 | raw_spin_unlock_irq(&rq->lock); |
| 896 | } |
| 897 | |
| 898 | #else /* __ARCH_WANT_UNLOCKED_CTXSW */ |
| 899 | static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next) |
| 900 | { |
| 901 | #ifdef CONFIG_SMP |
| 902 | /* |
| 903 | * We can optimise this out completely for !SMP, because the |
| 904 | * SMP rebalancing from interrupt is the only thing that cares |
| 905 | * here. |
| 906 | */ |
| 907 | next->on_cpu = 1; |
| 908 | #endif |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 909 | raw_spin_unlock(&rq->lock); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev) |
| 913 | { |
| 914 | #ifdef CONFIG_SMP |
| 915 | /* |
| 916 | * After ->on_cpu is cleared, the task can be moved to a different CPU. |
| 917 | * We must ensure this doesn't happen until the switch is completely |
| 918 | * finished. |
| 919 | */ |
| 920 | smp_wmb(); |
| 921 | prev->on_cpu = 0; |
| 922 | #endif |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 923 | local_irq_enable(); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 924 | } |
| 925 | #endif /* __ARCH_WANT_UNLOCKED_CTXSW */ |
| 926 | |
Li Zefan | b13095f | 2013-03-05 16:06:38 +0800 | [diff] [blame] | 927 | /* |
| 928 | * wake flags |
| 929 | */ |
| 930 | #define WF_SYNC 0x01 /* waker goes to sleep after wakeup */ |
| 931 | #define WF_FORK 0x02 /* child wakeup after fork */ |
| 932 | #define WF_MIGRATED 0x4 /* internal use, task got migrated */ |
| 933 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 934 | /* |
| 935 | * To aid in avoiding the subversion of "niceness" due to uneven distribution |
| 936 | * of tasks with abnormal "nice" values across CPUs the contribution that |
| 937 | * each task makes to its run queue's load is weighted according to its |
| 938 | * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a |
| 939 | * scaled version of the new time slice allocation that they receive on time |
| 940 | * slice expiry etc. |
| 941 | */ |
| 942 | |
| 943 | #define WEIGHT_IDLEPRIO 3 |
| 944 | #define WMULT_IDLEPRIO 1431655765 |
| 945 | |
| 946 | /* |
| 947 | * Nice levels are multiplicative, with a gentle 10% change for every |
| 948 | * nice level changed. I.e. when a CPU-bound task goes from nice 0 to |
| 949 | * nice 1, it will get ~10% less CPU time than another CPU-bound task |
| 950 | * that remained on nice 0. |
| 951 | * |
| 952 | * The "10% effect" is relative and cumulative: from _any_ nice level, |
| 953 | * if you go up 1 level, it's -10% CPU usage, if you go down 1 level |
| 954 | * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25. |
| 955 | * If a task goes up by ~10% and another task goes down by ~10% then |
| 956 | * the relative distance between them is ~25%.) |
| 957 | */ |
| 958 | static const int prio_to_weight[40] = { |
| 959 | /* -20 */ 88761, 71755, 56483, 46273, 36291, |
| 960 | /* -15 */ 29154, 23254, 18705, 14949, 11916, |
| 961 | /* -10 */ 9548, 7620, 6100, 4904, 3906, |
| 962 | /* -5 */ 3121, 2501, 1991, 1586, 1277, |
| 963 | /* 0 */ 1024, 820, 655, 526, 423, |
| 964 | /* 5 */ 335, 272, 215, 172, 137, |
| 965 | /* 10 */ 110, 87, 70, 56, 45, |
| 966 | /* 15 */ 36, 29, 23, 18, 15, |
| 967 | }; |
| 968 | |
| 969 | /* |
| 970 | * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated. |
| 971 | * |
| 972 | * In cases where the weight does not change often, we can use the |
| 973 | * precalculated inverse to speed up arithmetics by turning divisions |
| 974 | * into multiplications: |
| 975 | */ |
| 976 | static const u32 prio_to_wmult[40] = { |
| 977 | /* -20 */ 48388, 59856, 76040, 92818, 118348, |
| 978 | /* -15 */ 147320, 184698, 229616, 287308, 360437, |
| 979 | /* -10 */ 449829, 563644, 704093, 875809, 1099582, |
| 980 | /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326, |
| 981 | /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587, |
| 982 | /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126, |
| 983 | /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717, |
| 984 | /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153, |
| 985 | }; |
| 986 | |
Li Zefan | c82ba9f | 2013-03-05 16:06:55 +0800 | [diff] [blame] | 987 | #define ENQUEUE_WAKEUP 1 |
| 988 | #define ENQUEUE_HEAD 2 |
| 989 | #ifdef CONFIG_SMP |
| 990 | #define ENQUEUE_WAKING 4 /* sched_class::task_waking was called */ |
| 991 | #else |
| 992 | #define ENQUEUE_WAKING 0 |
| 993 | #endif |
| 994 | |
| 995 | #define DEQUEUE_SLEEP 1 |
| 996 | |
| 997 | struct sched_class { |
| 998 | const struct sched_class *next; |
| 999 | |
| 1000 | void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags); |
| 1001 | void (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags); |
| 1002 | void (*yield_task) (struct rq *rq); |
| 1003 | bool (*yield_to_task) (struct rq *rq, struct task_struct *p, bool preempt); |
| 1004 | |
| 1005 | void (*check_preempt_curr) (struct rq *rq, struct task_struct *p, int flags); |
| 1006 | |
| 1007 | struct task_struct * (*pick_next_task) (struct rq *rq); |
| 1008 | void (*put_prev_task) (struct rq *rq, struct task_struct *p); |
| 1009 | |
| 1010 | #ifdef CONFIG_SMP |
Peter Zijlstra | ac66f54 | 2013-10-07 11:29:16 +0100 | [diff] [blame] | 1011 | int (*select_task_rq)(struct task_struct *p, int task_cpu, int sd_flag, int flags); |
Li Zefan | c82ba9f | 2013-03-05 16:06:55 +0800 | [diff] [blame] | 1012 | void (*migrate_task_rq)(struct task_struct *p, int next_cpu); |
| 1013 | |
| 1014 | void (*pre_schedule) (struct rq *this_rq, struct task_struct *task); |
| 1015 | void (*post_schedule) (struct rq *this_rq); |
| 1016 | void (*task_waking) (struct task_struct *task); |
| 1017 | void (*task_woken) (struct rq *this_rq, struct task_struct *task); |
| 1018 | |
| 1019 | void (*set_cpus_allowed)(struct task_struct *p, |
| 1020 | const struct cpumask *newmask); |
| 1021 | |
| 1022 | void (*rq_online)(struct rq *rq); |
| 1023 | void (*rq_offline)(struct rq *rq); |
| 1024 | #endif |
| 1025 | |
| 1026 | void (*set_curr_task) (struct rq *rq); |
| 1027 | void (*task_tick) (struct rq *rq, struct task_struct *p, int queued); |
| 1028 | void (*task_fork) (struct task_struct *p); |
Dario Faggioli | e6c390f | 2013-11-07 14:43:35 +0100 | [diff] [blame] | 1029 | void (*task_dead) (struct task_struct *p); |
Li Zefan | c82ba9f | 2013-03-05 16:06:55 +0800 | [diff] [blame] | 1030 | |
| 1031 | void (*switched_from) (struct rq *this_rq, struct task_struct *task); |
| 1032 | void (*switched_to) (struct rq *this_rq, struct task_struct *task); |
| 1033 | void (*prio_changed) (struct rq *this_rq, struct task_struct *task, |
| 1034 | int oldprio); |
| 1035 | |
| 1036 | unsigned int (*get_rr_interval) (struct rq *rq, |
| 1037 | struct task_struct *task); |
| 1038 | |
| 1039 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 1040 | void (*task_move_group) (struct task_struct *p, int on_rq); |
| 1041 | #endif |
| 1042 | }; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1043 | |
| 1044 | #define sched_class_highest (&stop_sched_class) |
| 1045 | #define for_each_class(class) \ |
| 1046 | for (class = sched_class_highest; class; class = class->next) |
| 1047 | |
| 1048 | extern const struct sched_class stop_sched_class; |
| 1049 | extern const struct sched_class rt_sched_class; |
| 1050 | extern const struct sched_class fair_sched_class; |
| 1051 | extern const struct sched_class idle_sched_class; |
| 1052 | |
| 1053 | |
| 1054 | #ifdef CONFIG_SMP |
| 1055 | |
Li Zefan | b719203 | 2013-03-07 10:00:26 +0800 | [diff] [blame] | 1056 | extern void update_group_power(struct sched_domain *sd, int cpu); |
| 1057 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1058 | extern void trigger_load_balance(struct rq *rq, int cpu); |
| 1059 | extern void idle_balance(int this_cpu, struct rq *this_rq); |
| 1060 | |
Vincent Guittot | 642dbc3 | 2013-04-18 18:34:26 +0200 | [diff] [blame] | 1061 | extern void idle_enter_fair(struct rq *this_rq); |
| 1062 | extern void idle_exit_fair(struct rq *this_rq); |
Vincent Guittot | 642dbc3 | 2013-04-18 18:34:26 +0200 | [diff] [blame] | 1063 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1064 | #else /* CONFIG_SMP */ |
| 1065 | |
| 1066 | static inline void idle_balance(int cpu, struct rq *rq) |
| 1067 | { |
| 1068 | } |
| 1069 | |
| 1070 | #endif |
| 1071 | |
| 1072 | extern void sysrq_sched_debug_show(void); |
| 1073 | extern void sched_init_granularity(void); |
| 1074 | extern void update_max_interval(void); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1075 | extern void init_sched_rt_class(void); |
| 1076 | extern void init_sched_fair_class(void); |
| 1077 | |
| 1078 | extern void resched_task(struct task_struct *p); |
| 1079 | extern void resched_cpu(int cpu); |
| 1080 | |
| 1081 | extern struct rt_bandwidth def_rt_bandwidth; |
| 1082 | extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime); |
| 1083 | |
Peter Zijlstra | 556061b | 2012-05-11 17:31:26 +0200 | [diff] [blame] | 1084 | extern void update_idle_cpu_load(struct rq *this_rq); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1085 | |
Alex Shi | a75cdaa | 2013-06-20 10:18:47 +0800 | [diff] [blame] | 1086 | extern void init_task_runnable_average(struct task_struct *p); |
| 1087 | |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 1088 | #ifdef CONFIG_PARAVIRT |
| 1089 | static inline u64 steal_ticks(u64 steal) |
| 1090 | { |
| 1091 | if (unlikely(steal > NSEC_PER_SEC)) |
| 1092 | return div_u64(steal, TICK_NSEC); |
| 1093 | |
| 1094 | return __iter_div_u64_rem(steal, TICK_NSEC, &steal); |
| 1095 | } |
| 1096 | #endif |
| 1097 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1098 | static inline void inc_nr_running(struct rq *rq) |
| 1099 | { |
| 1100 | rq->nr_running++; |
Frederic Weisbecker | 9f3660c | 2013-04-20 14:35:09 +0200 | [diff] [blame] | 1101 | |
| 1102 | #ifdef CONFIG_NO_HZ_FULL |
| 1103 | if (rq->nr_running == 2) { |
| 1104 | if (tick_nohz_full_cpu(rq->cpu)) { |
| 1105 | /* Order rq->nr_running write against the IPI */ |
| 1106 | smp_wmb(); |
| 1107 | smp_send_reschedule(rq->cpu); |
| 1108 | } |
| 1109 | } |
| 1110 | #endif |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | static inline void dec_nr_running(struct rq *rq) |
| 1114 | { |
| 1115 | rq->nr_running--; |
| 1116 | } |
| 1117 | |
Frederic Weisbecker | 265f22a | 2013-05-03 03:39:05 +0200 | [diff] [blame] | 1118 | static inline void rq_last_tick_reset(struct rq *rq) |
| 1119 | { |
| 1120 | #ifdef CONFIG_NO_HZ_FULL |
| 1121 | rq->last_sched_tick = jiffies; |
| 1122 | #endif |
| 1123 | } |
| 1124 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1125 | extern void update_rq_clock(struct rq *rq); |
| 1126 | |
| 1127 | extern void activate_task(struct rq *rq, struct task_struct *p, int flags); |
| 1128 | extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags); |
| 1129 | |
| 1130 | extern void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags); |
| 1131 | |
| 1132 | extern const_debug unsigned int sysctl_sched_time_avg; |
| 1133 | extern const_debug unsigned int sysctl_sched_nr_migrate; |
| 1134 | extern const_debug unsigned int sysctl_sched_migration_cost; |
| 1135 | |
| 1136 | static inline u64 sched_avg_period(void) |
| 1137 | { |
| 1138 | return (u64)sysctl_sched_time_avg * NSEC_PER_MSEC / 2; |
| 1139 | } |
| 1140 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1141 | #ifdef CONFIG_SCHED_HRTICK |
| 1142 | |
| 1143 | /* |
| 1144 | * Use hrtick when: |
| 1145 | * - enabled by features |
| 1146 | * - hrtimer is actually high res |
| 1147 | */ |
| 1148 | static inline int hrtick_enabled(struct rq *rq) |
| 1149 | { |
| 1150 | if (!sched_feat(HRTICK)) |
| 1151 | return 0; |
| 1152 | if (!cpu_active(cpu_of(rq))) |
| 1153 | return 0; |
| 1154 | return hrtimer_is_hres_active(&rq->hrtick_timer); |
| 1155 | } |
| 1156 | |
| 1157 | void hrtick_start(struct rq *rq, u64 delay); |
| 1158 | |
Mike Galbraith | b39e66e | 2011-11-22 15:20:07 +0100 | [diff] [blame] | 1159 | #else |
| 1160 | |
| 1161 | static inline int hrtick_enabled(struct rq *rq) |
| 1162 | { |
| 1163 | return 0; |
| 1164 | } |
| 1165 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1166 | #endif /* CONFIG_SCHED_HRTICK */ |
| 1167 | |
| 1168 | #ifdef CONFIG_SMP |
| 1169 | extern void sched_avg_update(struct rq *rq); |
| 1170 | static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta) |
| 1171 | { |
| 1172 | rq->rt_avg += rt_delta; |
| 1173 | sched_avg_update(rq); |
| 1174 | } |
| 1175 | #else |
| 1176 | static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta) { } |
| 1177 | static inline void sched_avg_update(struct rq *rq) { } |
| 1178 | #endif |
| 1179 | |
| 1180 | extern void start_bandwidth_timer(struct hrtimer *period_timer, ktime_t period); |
| 1181 | |
| 1182 | #ifdef CONFIG_SMP |
| 1183 | #ifdef CONFIG_PREEMPT |
| 1184 | |
| 1185 | static inline void double_rq_lock(struct rq *rq1, struct rq *rq2); |
| 1186 | |
| 1187 | /* |
| 1188 | * fair double_lock_balance: Safely acquires both rq->locks in a fair |
| 1189 | * way at the expense of forcing extra atomic operations in all |
| 1190 | * invocations. This assures that the double_lock is acquired using the |
| 1191 | * same underlying policy as the spinlock_t on this architecture, which |
| 1192 | * reduces latency compared to the unfair variant below. However, it |
| 1193 | * also adds more overhead and therefore may reduce throughput. |
| 1194 | */ |
| 1195 | static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest) |
| 1196 | __releases(this_rq->lock) |
| 1197 | __acquires(busiest->lock) |
| 1198 | __acquires(this_rq->lock) |
| 1199 | { |
| 1200 | raw_spin_unlock(&this_rq->lock); |
| 1201 | double_rq_lock(this_rq, busiest); |
| 1202 | |
| 1203 | return 1; |
| 1204 | } |
| 1205 | |
| 1206 | #else |
| 1207 | /* |
| 1208 | * Unfair double_lock_balance: Optimizes throughput at the expense of |
| 1209 | * latency by eliminating extra atomic operations when the locks are |
| 1210 | * already in proper order on entry. This favors lower cpu-ids and will |
| 1211 | * grant the double lock to lower cpus over higher ids under contention, |
| 1212 | * regardless of entry order into the function. |
| 1213 | */ |
| 1214 | static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest) |
| 1215 | __releases(this_rq->lock) |
| 1216 | __acquires(busiest->lock) |
| 1217 | __acquires(this_rq->lock) |
| 1218 | { |
| 1219 | int ret = 0; |
| 1220 | |
| 1221 | if (unlikely(!raw_spin_trylock(&busiest->lock))) { |
| 1222 | if (busiest < this_rq) { |
| 1223 | raw_spin_unlock(&this_rq->lock); |
| 1224 | raw_spin_lock(&busiest->lock); |
| 1225 | raw_spin_lock_nested(&this_rq->lock, |
| 1226 | SINGLE_DEPTH_NESTING); |
| 1227 | ret = 1; |
| 1228 | } else |
| 1229 | raw_spin_lock_nested(&busiest->lock, |
| 1230 | SINGLE_DEPTH_NESTING); |
| 1231 | } |
| 1232 | return ret; |
| 1233 | } |
| 1234 | |
| 1235 | #endif /* CONFIG_PREEMPT */ |
| 1236 | |
| 1237 | /* |
| 1238 | * double_lock_balance - lock the busiest runqueue, this_rq is locked already. |
| 1239 | */ |
| 1240 | static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest) |
| 1241 | { |
| 1242 | if (unlikely(!irqs_disabled())) { |
| 1243 | /* printk() doesn't work good under rq->lock */ |
| 1244 | raw_spin_unlock(&this_rq->lock); |
| 1245 | BUG_ON(1); |
| 1246 | } |
| 1247 | |
| 1248 | return _double_lock_balance(this_rq, busiest); |
| 1249 | } |
| 1250 | |
| 1251 | static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest) |
| 1252 | __releases(busiest->lock) |
| 1253 | { |
| 1254 | raw_spin_unlock(&busiest->lock); |
| 1255 | lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_); |
| 1256 | } |
| 1257 | |
Peter Zijlstra | 7460231 | 2013-10-10 20:17:22 +0200 | [diff] [blame] | 1258 | static inline void double_lock(spinlock_t *l1, spinlock_t *l2) |
| 1259 | { |
| 1260 | if (l1 > l2) |
| 1261 | swap(l1, l2); |
| 1262 | |
| 1263 | spin_lock(l1); |
| 1264 | spin_lock_nested(l2, SINGLE_DEPTH_NESTING); |
| 1265 | } |
| 1266 | |
| 1267 | static inline void double_raw_lock(raw_spinlock_t *l1, raw_spinlock_t *l2) |
| 1268 | { |
| 1269 | if (l1 > l2) |
| 1270 | swap(l1, l2); |
| 1271 | |
| 1272 | raw_spin_lock(l1); |
| 1273 | raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING); |
| 1274 | } |
| 1275 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1276 | /* |
| 1277 | * double_rq_lock - safely lock two runqueues |
| 1278 | * |
| 1279 | * Note this does not disable interrupts like task_rq_lock, |
| 1280 | * you need to do so manually before calling. |
| 1281 | */ |
| 1282 | static inline void double_rq_lock(struct rq *rq1, struct rq *rq2) |
| 1283 | __acquires(rq1->lock) |
| 1284 | __acquires(rq2->lock) |
| 1285 | { |
| 1286 | BUG_ON(!irqs_disabled()); |
| 1287 | if (rq1 == rq2) { |
| 1288 | raw_spin_lock(&rq1->lock); |
| 1289 | __acquire(rq2->lock); /* Fake it out ;) */ |
| 1290 | } else { |
| 1291 | if (rq1 < rq2) { |
| 1292 | raw_spin_lock(&rq1->lock); |
| 1293 | raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING); |
| 1294 | } else { |
| 1295 | raw_spin_lock(&rq2->lock); |
| 1296 | raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING); |
| 1297 | } |
| 1298 | } |
| 1299 | } |
| 1300 | |
| 1301 | /* |
| 1302 | * double_rq_unlock - safely unlock two runqueues |
| 1303 | * |
| 1304 | * Note this does not restore interrupts like task_rq_unlock, |
| 1305 | * you need to do so manually after calling. |
| 1306 | */ |
| 1307 | static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2) |
| 1308 | __releases(rq1->lock) |
| 1309 | __releases(rq2->lock) |
| 1310 | { |
| 1311 | raw_spin_unlock(&rq1->lock); |
| 1312 | if (rq1 != rq2) |
| 1313 | raw_spin_unlock(&rq2->lock); |
| 1314 | else |
| 1315 | __release(rq2->lock); |
| 1316 | } |
| 1317 | |
| 1318 | #else /* CONFIG_SMP */ |
| 1319 | |
| 1320 | /* |
| 1321 | * double_rq_lock - safely lock two runqueues |
| 1322 | * |
| 1323 | * Note this does not disable interrupts like task_rq_lock, |
| 1324 | * you need to do so manually before calling. |
| 1325 | */ |
| 1326 | static inline void double_rq_lock(struct rq *rq1, struct rq *rq2) |
| 1327 | __acquires(rq1->lock) |
| 1328 | __acquires(rq2->lock) |
| 1329 | { |
| 1330 | BUG_ON(!irqs_disabled()); |
| 1331 | BUG_ON(rq1 != rq2); |
| 1332 | raw_spin_lock(&rq1->lock); |
| 1333 | __acquire(rq2->lock); /* Fake it out ;) */ |
| 1334 | } |
| 1335 | |
| 1336 | /* |
| 1337 | * double_rq_unlock - safely unlock two runqueues |
| 1338 | * |
| 1339 | * Note this does not restore interrupts like task_rq_unlock, |
| 1340 | * you need to do so manually after calling. |
| 1341 | */ |
| 1342 | static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2) |
| 1343 | __releases(rq1->lock) |
| 1344 | __releases(rq2->lock) |
| 1345 | { |
| 1346 | BUG_ON(rq1 != rq2); |
| 1347 | raw_spin_unlock(&rq1->lock); |
| 1348 | __release(rq2->lock); |
| 1349 | } |
| 1350 | |
| 1351 | #endif |
| 1352 | |
| 1353 | extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq); |
| 1354 | extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq); |
| 1355 | extern void print_cfs_stats(struct seq_file *m, int cpu); |
| 1356 | extern void print_rt_stats(struct seq_file *m, int cpu); |
| 1357 | |
| 1358 | extern void init_cfs_rq(struct cfs_rq *cfs_rq); |
| 1359 | extern void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1360 | |
Ben Segall | 1ee14e6 | 2013-10-16 11:16:12 -0700 | [diff] [blame] | 1361 | extern void cfs_bandwidth_usage_inc(void); |
| 1362 | extern void cfs_bandwidth_usage_dec(void); |
Suresh Siddha | 1c792db | 2011-12-01 17:07:32 -0800 | [diff] [blame] | 1363 | |
Frederic Weisbecker | 3451d02 | 2011-08-10 23:21:01 +0200 | [diff] [blame] | 1364 | #ifdef CONFIG_NO_HZ_COMMON |
Suresh Siddha | 1c792db | 2011-12-01 17:07:32 -0800 | [diff] [blame] | 1365 | enum rq_nohz_flag_bits { |
| 1366 | NOHZ_TICK_STOPPED, |
| 1367 | NOHZ_BALANCE_KICK, |
| 1368 | }; |
| 1369 | |
| 1370 | #define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags) |
| 1371 | #endif |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 1372 | |
| 1373 | #ifdef CONFIG_IRQ_TIME_ACCOUNTING |
| 1374 | |
| 1375 | DECLARE_PER_CPU(u64, cpu_hardirq_time); |
| 1376 | DECLARE_PER_CPU(u64, cpu_softirq_time); |
| 1377 | |
| 1378 | #ifndef CONFIG_64BIT |
| 1379 | DECLARE_PER_CPU(seqcount_t, irq_time_seq); |
| 1380 | |
| 1381 | static inline void irq_time_write_begin(void) |
| 1382 | { |
| 1383 | __this_cpu_inc(irq_time_seq.sequence); |
| 1384 | smp_wmb(); |
| 1385 | } |
| 1386 | |
| 1387 | static inline void irq_time_write_end(void) |
| 1388 | { |
| 1389 | smp_wmb(); |
| 1390 | __this_cpu_inc(irq_time_seq.sequence); |
| 1391 | } |
| 1392 | |
| 1393 | static inline u64 irq_time_read(int cpu) |
| 1394 | { |
| 1395 | u64 irq_time; |
| 1396 | unsigned seq; |
| 1397 | |
| 1398 | do { |
| 1399 | seq = read_seqcount_begin(&per_cpu(irq_time_seq, cpu)); |
| 1400 | irq_time = per_cpu(cpu_softirq_time, cpu) + |
| 1401 | per_cpu(cpu_hardirq_time, cpu); |
| 1402 | } while (read_seqcount_retry(&per_cpu(irq_time_seq, cpu), seq)); |
| 1403 | |
| 1404 | return irq_time; |
| 1405 | } |
| 1406 | #else /* CONFIG_64BIT */ |
| 1407 | static inline void irq_time_write_begin(void) |
| 1408 | { |
| 1409 | } |
| 1410 | |
| 1411 | static inline void irq_time_write_end(void) |
| 1412 | { |
| 1413 | } |
| 1414 | |
| 1415 | static inline u64 irq_time_read(int cpu) |
| 1416 | { |
| 1417 | return per_cpu(cpu_softirq_time, cpu) + per_cpu(cpu_hardirq_time, cpu); |
| 1418 | } |
| 1419 | #endif /* CONFIG_64BIT */ |
| 1420 | #endif /* CONFIG_IRQ_TIME_ACCOUNTING */ |