aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/sched.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/sched.c')
-rw-r--r--kernel/sched.c3144
1 files changed, 730 insertions, 2414 deletions
diff --git a/kernel/sched.c b/kernel/sched.c
index ee61f454a98b..9ab3cd7858d3 100644
--- a/kernel/sched.c
+++ b/kernel/sched.c
@@ -141,7 +141,7 @@ struct rt_prio_array {
141 141
142struct rt_bandwidth { 142struct rt_bandwidth {
143 /* nests inside the rq lock: */ 143 /* nests inside the rq lock: */
144 spinlock_t rt_runtime_lock; 144 raw_spinlock_t rt_runtime_lock;
145 ktime_t rt_period; 145 ktime_t rt_period;
146 u64 rt_runtime; 146 u64 rt_runtime;
147 struct hrtimer rt_period_timer; 147 struct hrtimer rt_period_timer;
@@ -178,7 +178,7 @@ void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
178 rt_b->rt_period = ns_to_ktime(period); 178 rt_b->rt_period = ns_to_ktime(period);
179 rt_b->rt_runtime = runtime; 179 rt_b->rt_runtime = runtime;
180 180
181 spin_lock_init(&rt_b->rt_runtime_lock); 181 raw_spin_lock_init(&rt_b->rt_runtime_lock);
182 182
183 hrtimer_init(&rt_b->rt_period_timer, 183 hrtimer_init(&rt_b->rt_period_timer,
184 CLOCK_MONOTONIC, HRTIMER_MODE_REL); 184 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
@@ -200,7 +200,7 @@ static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
200 if (hrtimer_active(&rt_b->rt_period_timer)) 200 if (hrtimer_active(&rt_b->rt_period_timer))
201 return; 201 return;
202 202
203 spin_lock(&rt_b->rt_runtime_lock); 203 raw_spin_lock(&rt_b->rt_runtime_lock);
204 for (;;) { 204 for (;;) {
205 unsigned long delta; 205 unsigned long delta;
206 ktime_t soft, hard; 206 ktime_t soft, hard;
@@ -217,7 +217,7 @@ static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
217 __hrtimer_start_range_ns(&rt_b->rt_period_timer, soft, delta, 217 __hrtimer_start_range_ns(&rt_b->rt_period_timer, soft, delta,
218 HRTIMER_MODE_ABS_PINNED, 0); 218 HRTIMER_MODE_ABS_PINNED, 0);
219 } 219 }
220 spin_unlock(&rt_b->rt_runtime_lock); 220 raw_spin_unlock(&rt_b->rt_runtime_lock);
221} 221}
222 222
223#ifdef CONFIG_RT_GROUP_SCHED 223#ifdef CONFIG_RT_GROUP_SCHED
@@ -233,7 +233,7 @@ static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
233 */ 233 */
234static DEFINE_MUTEX(sched_domains_mutex); 234static DEFINE_MUTEX(sched_domains_mutex);
235 235
236#ifdef CONFIG_GROUP_SCHED 236#ifdef CONFIG_CGROUP_SCHED
237 237
238#include <linux/cgroup.h> 238#include <linux/cgroup.h>
239 239
@@ -243,13 +243,7 @@ static LIST_HEAD(task_groups);
243 243
244/* task group related information */ 244/* task group related information */
245struct task_group { 245struct task_group {
246#ifdef CONFIG_CGROUP_SCHED
247 struct cgroup_subsys_state css; 246 struct cgroup_subsys_state css;
248#endif
249
250#ifdef CONFIG_USER_SCHED
251 uid_t uid;
252#endif
253 247
254#ifdef CONFIG_FAIR_GROUP_SCHED 248#ifdef CONFIG_FAIR_GROUP_SCHED
255 /* schedulable entities of this group on each cpu */ 249 /* schedulable entities of this group on each cpu */
@@ -274,41 +268,15 @@ struct task_group {
274 struct list_head children; 268 struct list_head children;
275}; 269};
276 270
277#ifdef CONFIG_USER_SCHED
278
279/* Helper function to pass uid information to create_sched_user() */
280void set_tg_uid(struct user_struct *user)
281{
282 user->tg->uid = user->uid;
283}
284
285/*
286 * Root task group.
287 * Every UID task group (including init_task_group aka UID-0) will
288 * be a child to this group.
289 */
290struct task_group root_task_group;
291
292#ifdef CONFIG_FAIR_GROUP_SCHED
293/* Default task group's sched entity on each cpu */
294static DEFINE_PER_CPU(struct sched_entity, init_sched_entity);
295/* Default task group's cfs_rq on each cpu */
296static DEFINE_PER_CPU_SHARED_ALIGNED(struct cfs_rq, init_tg_cfs_rq);
297#endif /* CONFIG_FAIR_GROUP_SCHED */
298
299#ifdef CONFIG_RT_GROUP_SCHED
300static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity);
301static DEFINE_PER_CPU_SHARED_ALIGNED(struct rt_rq, init_rt_rq);
302#endif /* CONFIG_RT_GROUP_SCHED */
303#else /* !CONFIG_USER_SCHED */
304#define root_task_group init_task_group 271#define root_task_group init_task_group
305#endif /* CONFIG_USER_SCHED */
306 272
307/* task_group_lock serializes add/remove of task groups and also changes to 273/* task_group_lock serializes add/remove of task groups and also changes to
308 * a task group's cpu shares. 274 * a task group's cpu shares.
309 */ 275 */
310static DEFINE_SPINLOCK(task_group_lock); 276static DEFINE_SPINLOCK(task_group_lock);
311 277
278#ifdef CONFIG_FAIR_GROUP_SCHED
279
312#ifdef CONFIG_SMP 280#ifdef CONFIG_SMP
313static int root_task_group_empty(void) 281static int root_task_group_empty(void)
314{ 282{
@@ -316,12 +284,7 @@ static int root_task_group_empty(void)
316} 284}
317#endif 285#endif
318 286
319#ifdef CONFIG_FAIR_GROUP_SCHED
320#ifdef CONFIG_USER_SCHED
321# define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD)
322#else /* !CONFIG_USER_SCHED */
323# define INIT_TASK_GROUP_LOAD NICE_0_LOAD 287# define INIT_TASK_GROUP_LOAD NICE_0_LOAD
324#endif /* CONFIG_USER_SCHED */
325 288
326/* 289/*
327 * A weight of 0 or 1 can cause arithmetics problems. 290 * A weight of 0 or 1 can cause arithmetics problems.
@@ -347,11 +310,7 @@ static inline struct task_group *task_group(struct task_struct *p)
347{ 310{
348 struct task_group *tg; 311 struct task_group *tg;
349 312
350#ifdef CONFIG_USER_SCHED 313#ifdef CONFIG_CGROUP_SCHED
351 rcu_read_lock();
352 tg = __task_cred(p)->user->tg;
353 rcu_read_unlock();
354#elif defined(CONFIG_CGROUP_SCHED)
355 tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id), 314 tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id),
356 struct task_group, css); 315 struct task_group, css);
357#else 316#else
@@ -382,7 +341,7 @@ static inline struct task_group *task_group(struct task_struct *p)
382 return NULL; 341 return NULL;
383} 342}
384 343
385#endif /* CONFIG_GROUP_SCHED */ 344#endif /* CONFIG_CGROUP_SCHED */
386 345
387/* CFS-related fields in a runqueue */ 346/* CFS-related fields in a runqueue */
388struct cfs_rq { 347struct cfs_rq {
@@ -469,7 +428,7 @@ struct rt_rq {
469 u64 rt_time; 428 u64 rt_time;
470 u64 rt_runtime; 429 u64 rt_runtime;
471 /* Nests inside the rq lock: */ 430 /* Nests inside the rq lock: */
472 spinlock_t rt_runtime_lock; 431 raw_spinlock_t rt_runtime_lock;
473 432
474#ifdef CONFIG_RT_GROUP_SCHED 433#ifdef CONFIG_RT_GROUP_SCHED
475 unsigned long rt_nr_boosted; 434 unsigned long rt_nr_boosted;
@@ -477,7 +436,6 @@ struct rt_rq {
477 struct rq *rq; 436 struct rq *rq;
478 struct list_head leaf_rt_rq_list; 437 struct list_head leaf_rt_rq_list;
479 struct task_group *tg; 438 struct task_group *tg;
480 struct sched_rt_entity *rt_se;
481#endif 439#endif
482}; 440};
483 441
@@ -524,7 +482,7 @@ static struct root_domain def_root_domain;
524 */ 482 */
525struct rq { 483struct rq {
526 /* runqueue lock: */ 484 /* runqueue lock: */
527 spinlock_t lock; 485 raw_spinlock_t lock;
528 486
529 /* 487 /*
530 * nr_running and cpu_load should be in the same cacheline because 488 * nr_running and cpu_load should be in the same cacheline because
@@ -534,14 +492,12 @@ struct rq {
534 #define CPU_LOAD_IDX_MAX 5 492 #define CPU_LOAD_IDX_MAX 5
535 unsigned long cpu_load[CPU_LOAD_IDX_MAX]; 493 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
536#ifdef CONFIG_NO_HZ 494#ifdef CONFIG_NO_HZ
537 unsigned long last_tick_seen;
538 unsigned char in_nohz_recently; 495 unsigned char in_nohz_recently;
539#endif 496#endif
540 /* capture load from *all* tasks on this cpu: */ 497 /* capture load from *all* tasks on this cpu: */
541 struct load_weight load; 498 struct load_weight load;
542 unsigned long nr_load_updates; 499 unsigned long nr_load_updates;
543 u64 nr_switches; 500 u64 nr_switches;
544 u64 nr_migrations_in;
545 501
546 struct cfs_rq cfs; 502 struct cfs_rq cfs;
547 struct rt_rq rt; 503 struct rt_rq rt;
@@ -590,6 +546,8 @@ struct rq {
590 546
591 u64 rt_avg; 547 u64 rt_avg;
592 u64 age_stamp; 548 u64 age_stamp;
549 u64 idle_stamp;
550 u64 avg_idle;
593#endif 551#endif
594 552
595 /* calc_load related fields */ 553 /* calc_load related fields */
@@ -644,6 +602,11 @@ static inline int cpu_of(struct rq *rq)
644#endif 602#endif
645} 603}
646 604
605#define rcu_dereference_check_sched_domain(p) \
606 rcu_dereference_check((p), \
607 rcu_read_lock_sched_held() || \
608 lockdep_is_held(&sched_domains_mutex))
609
647/* 610/*
648 * The domain tree (rq->sd) is protected by RCU's quiescent state transition. 611 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
649 * See detach_destroy_domains: synchronize_sched for details. 612 * See detach_destroy_domains: synchronize_sched for details.
@@ -652,7 +615,7 @@ static inline int cpu_of(struct rq *rq)
652 * preempt-disabled sections. 615 * preempt-disabled sections.
653 */ 616 */
654#define for_each_domain(cpu, __sd) \ 617#define for_each_domain(cpu, __sd) \
655 for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent) 618 for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
656 619
657#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu))) 620#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
658#define this_rq() (&__get_cpu_var(runqueues)) 621#define this_rq() (&__get_cpu_var(runqueues))
@@ -676,6 +639,7 @@ inline void update_rq_clock(struct rq *rq)
676 639
677/** 640/**
678 * runqueue_is_locked 641 * runqueue_is_locked
642 * @cpu: the processor in question.
679 * 643 *
680 * Returns true if the current cpu runqueue is locked. 644 * Returns true if the current cpu runqueue is locked.
681 * This interface allows printk to be called with the runqueue lock 645 * This interface allows printk to be called with the runqueue lock
@@ -683,7 +647,7 @@ inline void update_rq_clock(struct rq *rq)
683 */ 647 */
684int runqueue_is_locked(int cpu) 648int runqueue_is_locked(int cpu)
685{ 649{
686 return spin_is_locked(&cpu_rq(cpu)->lock); 650 return raw_spin_is_locked(&cpu_rq(cpu)->lock);
687} 651}
688 652
689/* 653/*
@@ -770,7 +734,7 @@ sched_feat_write(struct file *filp, const char __user *ubuf,
770 if (!sched_feat_names[i]) 734 if (!sched_feat_names[i])
771 return -EINVAL; 735 return -EINVAL;
772 736
773 filp->f_pos += cnt; 737 *ppos += cnt;
774 738
775 return cnt; 739 return cnt;
776} 740}
@@ -780,7 +744,7 @@ static int sched_feat_open(struct inode *inode, struct file *filp)
780 return single_open(filp, sched_feat_show, NULL); 744 return single_open(filp, sched_feat_show, NULL);
781} 745}
782 746
783static struct file_operations sched_feat_fops = { 747static const struct file_operations sched_feat_fops = {
784 .open = sched_feat_open, 748 .open = sched_feat_open,
785 .write = sched_feat_write, 749 .write = sched_feat_write,
786 .read = seq_read, 750 .read = seq_read,
@@ -812,6 +776,7 @@ const_debug unsigned int sysctl_sched_nr_migrate = 32;
812 * default: 0.25ms 776 * default: 0.25ms
813 */ 777 */
814unsigned int sysctl_sched_shares_ratelimit = 250000; 778unsigned int sysctl_sched_shares_ratelimit = 250000;
779unsigned int normalized_sysctl_sched_shares_ratelimit = 250000;
815 780
816/* 781/*
817 * Inject some fuzzyness into changing the per-cpu group shares 782 * Inject some fuzzyness into changing the per-cpu group shares
@@ -890,7 +855,7 @@ static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
890 */ 855 */
891 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_); 856 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
892 857
893 spin_unlock_irq(&rq->lock); 858 raw_spin_unlock_irq(&rq->lock);
894} 859}
895 860
896#else /* __ARCH_WANT_UNLOCKED_CTXSW */ 861#else /* __ARCH_WANT_UNLOCKED_CTXSW */
@@ -914,9 +879,9 @@ static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
914 next->oncpu = 1; 879 next->oncpu = 1;
915#endif 880#endif
916#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW 881#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
917 spin_unlock_irq(&rq->lock); 882 raw_spin_unlock_irq(&rq->lock);
918#else 883#else
919 spin_unlock(&rq->lock); 884 raw_spin_unlock(&rq->lock);
920#endif 885#endif
921} 886}
922 887
@@ -938,18 +903,35 @@ static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
938#endif /* __ARCH_WANT_UNLOCKED_CTXSW */ 903#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
939 904
940/* 905/*
906 * Check whether the task is waking, we use this to synchronize against
907 * ttwu() so that task_cpu() reports a stable number.
908 *
909 * We need to make an exception for PF_STARTING tasks because the fork
910 * path might require task_rq_lock() to work, eg. it can call
911 * set_cpus_allowed_ptr() from the cpuset clone_ns code.
912 */
913static inline int task_is_waking(struct task_struct *p)
914{
915 return unlikely((p->state == TASK_WAKING) && !(p->flags & PF_STARTING));
916}
917
918/*
941 * __task_rq_lock - lock the runqueue a given task resides on. 919 * __task_rq_lock - lock the runqueue a given task resides on.
942 * Must be called interrupts disabled. 920 * Must be called interrupts disabled.
943 */ 921 */
944static inline struct rq *__task_rq_lock(struct task_struct *p) 922static inline struct rq *__task_rq_lock(struct task_struct *p)
945 __acquires(rq->lock) 923 __acquires(rq->lock)
946{ 924{
925 struct rq *rq;
926
947 for (;;) { 927 for (;;) {
948 struct rq *rq = task_rq(p); 928 while (task_is_waking(p))
949 spin_lock(&rq->lock); 929 cpu_relax();
950 if (likely(rq == task_rq(p))) 930 rq = task_rq(p);
931 raw_spin_lock(&rq->lock);
932 if (likely(rq == task_rq(p) && !task_is_waking(p)))
951 return rq; 933 return rq;
952 spin_unlock(&rq->lock); 934 raw_spin_unlock(&rq->lock);
953 } 935 }
954} 936}
955 937
@@ -964,12 +946,14 @@ static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
964 struct rq *rq; 946 struct rq *rq;
965 947
966 for (;;) { 948 for (;;) {
949 while (task_is_waking(p))
950 cpu_relax();
967 local_irq_save(*flags); 951 local_irq_save(*flags);
968 rq = task_rq(p); 952 rq = task_rq(p);
969 spin_lock(&rq->lock); 953 raw_spin_lock(&rq->lock);
970 if (likely(rq == task_rq(p))) 954 if (likely(rq == task_rq(p) && !task_is_waking(p)))
971 return rq; 955 return rq;
972 spin_unlock_irqrestore(&rq->lock, *flags); 956 raw_spin_unlock_irqrestore(&rq->lock, *flags);
973 } 957 }
974} 958}
975 959
@@ -978,19 +962,19 @@ void task_rq_unlock_wait(struct task_struct *p)
978 struct rq *rq = task_rq(p); 962 struct rq *rq = task_rq(p);
979 963
980 smp_mb(); /* spin-unlock-wait is not a full memory barrier */ 964 smp_mb(); /* spin-unlock-wait is not a full memory barrier */
981 spin_unlock_wait(&rq->lock); 965 raw_spin_unlock_wait(&rq->lock);
982} 966}
983 967
984static void __task_rq_unlock(struct rq *rq) 968static void __task_rq_unlock(struct rq *rq)
985 __releases(rq->lock) 969 __releases(rq->lock)
986{ 970{
987 spin_unlock(&rq->lock); 971 raw_spin_unlock(&rq->lock);
988} 972}
989 973
990static inline void task_rq_unlock(struct rq *rq, unsigned long *flags) 974static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
991 __releases(rq->lock) 975 __releases(rq->lock)
992{ 976{
993 spin_unlock_irqrestore(&rq->lock, *flags); 977 raw_spin_unlock_irqrestore(&rq->lock, *flags);
994} 978}
995 979
996/* 980/*
@@ -1003,7 +987,7 @@ static struct rq *this_rq_lock(void)
1003 987
1004 local_irq_disable(); 988 local_irq_disable();
1005 rq = this_rq(); 989 rq = this_rq();
1006 spin_lock(&rq->lock); 990 raw_spin_lock(&rq->lock);
1007 991
1008 return rq; 992 return rq;
1009} 993}
@@ -1050,10 +1034,10 @@ static enum hrtimer_restart hrtick(struct hrtimer *timer)
1050 1034
1051 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id()); 1035 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1052 1036
1053 spin_lock(&rq->lock); 1037 raw_spin_lock(&rq->lock);
1054 update_rq_clock(rq); 1038 update_rq_clock(rq);
1055 rq->curr->sched_class->task_tick(rq, rq->curr, 1); 1039 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
1056 spin_unlock(&rq->lock); 1040 raw_spin_unlock(&rq->lock);
1057 1041
1058 return HRTIMER_NORESTART; 1042 return HRTIMER_NORESTART;
1059} 1043}
@@ -1066,10 +1050,10 @@ static void __hrtick_start(void *arg)
1066{ 1050{
1067 struct rq *rq = arg; 1051 struct rq *rq = arg;
1068 1052
1069 spin_lock(&rq->lock); 1053 raw_spin_lock(&rq->lock);
1070 hrtimer_restart(&rq->hrtick_timer); 1054 hrtimer_restart(&rq->hrtick_timer);
1071 rq->hrtick_csd_pending = 0; 1055 rq->hrtick_csd_pending = 0;
1072 spin_unlock(&rq->lock); 1056 raw_spin_unlock(&rq->lock);
1073} 1057}
1074 1058
1075/* 1059/*
@@ -1176,7 +1160,7 @@ static void resched_task(struct task_struct *p)
1176{ 1160{
1177 int cpu; 1161 int cpu;
1178 1162
1179 assert_spin_locked(&task_rq(p)->lock); 1163 assert_raw_spin_locked(&task_rq(p)->lock);
1180 1164
1181 if (test_tsk_need_resched(p)) 1165 if (test_tsk_need_resched(p))
1182 return; 1166 return;
@@ -1198,10 +1182,10 @@ static void resched_cpu(int cpu)
1198 struct rq *rq = cpu_rq(cpu); 1182 struct rq *rq = cpu_rq(cpu);
1199 unsigned long flags; 1183 unsigned long flags;
1200 1184
1201 if (!spin_trylock_irqsave(&rq->lock, flags)) 1185 if (!raw_spin_trylock_irqsave(&rq->lock, flags))
1202 return; 1186 return;
1203 resched_task(cpu_curr(cpu)); 1187 resched_task(cpu_curr(cpu));
1204 spin_unlock_irqrestore(&rq->lock, flags); 1188 raw_spin_unlock_irqrestore(&rq->lock, flags);
1205} 1189}
1206 1190
1207#ifdef CONFIG_NO_HZ 1191#ifdef CONFIG_NO_HZ
@@ -1270,7 +1254,7 @@ static void sched_rt_avg_update(struct rq *rq, u64 rt_delta)
1270#else /* !CONFIG_SMP */ 1254#else /* !CONFIG_SMP */
1271static void resched_task(struct task_struct *p) 1255static void resched_task(struct task_struct *p)
1272{ 1256{
1273 assert_spin_locked(&task_rq(p)->lock); 1257 assert_raw_spin_locked(&task_rq(p)->lock);
1274 set_tsk_need_resched(p); 1258 set_tsk_need_resched(p);
1275} 1259}
1276 1260
@@ -1387,32 +1371,6 @@ static const u32 prio_to_wmult[40] = {
1387 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153, 1371 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
1388}; 1372};
1389 1373
1390static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
1391
1392/*
1393 * runqueue iterator, to support SMP load-balancing between different
1394 * scheduling classes, without having to expose their internal data
1395 * structures to the load-balancing proper:
1396 */
1397struct rq_iterator {
1398 void *arg;
1399 struct task_struct *(*start)(void *);
1400 struct task_struct *(*next)(void *);
1401};
1402
1403#ifdef CONFIG_SMP
1404static unsigned long
1405balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
1406 unsigned long max_load_move, struct sched_domain *sd,
1407 enum cpu_idle_type idle, int *all_pinned,
1408 int *this_best_prio, struct rq_iterator *iterator);
1409
1410static int
1411iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
1412 struct sched_domain *sd, enum cpu_idle_type idle,
1413 struct rq_iterator *iterator);
1414#endif
1415
1416/* Time spent by the tasks of the cpu accounting group executing in ... */ 1374/* Time spent by the tasks of the cpu accounting group executing in ... */
1417enum cpuacct_stat_index { 1375enum cpuacct_stat_index {
1418 CPUACCT_STAT_USER, /* ... user mode */ 1376 CPUACCT_STAT_USER, /* ... user mode */
@@ -1528,7 +1486,7 @@ static unsigned long target_load(int cpu, int type)
1528 1486
1529static struct sched_group *group_of(int cpu) 1487static struct sched_group *group_of(int cpu)
1530{ 1488{
1531 struct sched_domain *sd = rcu_dereference(cpu_rq(cpu)->sd); 1489 struct sched_domain *sd = rcu_dereference_sched(cpu_rq(cpu)->sd);
1532 1490
1533 if (!sd) 1491 if (!sd)
1534 return NULL; 1492 return NULL;
@@ -1563,11 +1521,7 @@ static unsigned long cpu_avg_load_per_task(int cpu)
1563 1521
1564#ifdef CONFIG_FAIR_GROUP_SCHED 1522#ifdef CONFIG_FAIR_GROUP_SCHED
1565 1523
1566struct update_shares_data { 1524static __read_mostly unsigned long __percpu *update_shares_data;
1567 unsigned long rq_weight[NR_CPUS];
1568};
1569
1570static DEFINE_PER_CPU(struct update_shares_data, update_shares_data);
1571 1525
1572static void __set_se_shares(struct sched_entity *se, unsigned long shares); 1526static void __set_se_shares(struct sched_entity *se, unsigned long shares);
1573 1527
@@ -1577,12 +1531,12 @@ static void __set_se_shares(struct sched_entity *se, unsigned long shares);
1577static void update_group_shares_cpu(struct task_group *tg, int cpu, 1531static void update_group_shares_cpu(struct task_group *tg, int cpu,
1578 unsigned long sd_shares, 1532 unsigned long sd_shares,
1579 unsigned long sd_rq_weight, 1533 unsigned long sd_rq_weight,
1580 struct update_shares_data *usd) 1534 unsigned long *usd_rq_weight)
1581{ 1535{
1582 unsigned long shares, rq_weight; 1536 unsigned long shares, rq_weight;
1583 int boost = 0; 1537 int boost = 0;
1584 1538
1585 rq_weight = usd->rq_weight[cpu]; 1539 rq_weight = usd_rq_weight[cpu];
1586 if (!rq_weight) { 1540 if (!rq_weight) {
1587 boost = 1; 1541 boost = 1;
1588 rq_weight = NICE_0_LOAD; 1542 rq_weight = NICE_0_LOAD;
@@ -1601,11 +1555,11 @@ static void update_group_shares_cpu(struct task_group *tg, int cpu,
1601 struct rq *rq = cpu_rq(cpu); 1555 struct rq *rq = cpu_rq(cpu);
1602 unsigned long flags; 1556 unsigned long flags;
1603 1557
1604 spin_lock_irqsave(&rq->lock, flags); 1558 raw_spin_lock_irqsave(&rq->lock, flags);
1605 tg->cfs_rq[cpu]->rq_weight = boost ? 0 : rq_weight; 1559 tg->cfs_rq[cpu]->rq_weight = boost ? 0 : rq_weight;
1606 tg->cfs_rq[cpu]->shares = boost ? 0 : shares; 1560 tg->cfs_rq[cpu]->shares = boost ? 0 : shares;
1607 __set_se_shares(tg->se[cpu], shares); 1561 __set_se_shares(tg->se[cpu], shares);
1608 spin_unlock_irqrestore(&rq->lock, flags); 1562 raw_spin_unlock_irqrestore(&rq->lock, flags);
1609 } 1563 }
1610} 1564}
1611 1565
@@ -1616,8 +1570,8 @@ static void update_group_shares_cpu(struct task_group *tg, int cpu,
1616 */ 1570 */
1617static int tg_shares_up(struct task_group *tg, void *data) 1571static int tg_shares_up(struct task_group *tg, void *data)
1618{ 1572{
1619 unsigned long weight, rq_weight = 0, shares = 0; 1573 unsigned long weight, rq_weight = 0, sum_weight = 0, shares = 0;
1620 struct update_shares_data *usd; 1574 unsigned long *usd_rq_weight;
1621 struct sched_domain *sd = data; 1575 struct sched_domain *sd = data;
1622 unsigned long flags; 1576 unsigned long flags;
1623 int i; 1577 int i;
@@ -1626,12 +1580,13 @@ static int tg_shares_up(struct task_group *tg, void *data)
1626 return 0; 1580 return 0;
1627 1581
1628 local_irq_save(flags); 1582 local_irq_save(flags);
1629 usd = &__get_cpu_var(update_shares_data); 1583 usd_rq_weight = per_cpu_ptr(update_shares_data, smp_processor_id());
1630 1584
1631 for_each_cpu(i, sched_domain_span(sd)) { 1585 for_each_cpu(i, sched_domain_span(sd)) {
1632 weight = tg->cfs_rq[i]->load.weight; 1586 weight = tg->cfs_rq[i]->load.weight;
1633 usd->rq_weight[i] = weight; 1587 usd_rq_weight[i] = weight;
1634 1588
1589 rq_weight += weight;
1635 /* 1590 /*
1636 * If there are currently no tasks on the cpu pretend there 1591 * If there are currently no tasks on the cpu pretend there
1637 * is one of average load so that when a new task gets to 1592 * is one of average load so that when a new task gets to
@@ -1640,10 +1595,13 @@ static int tg_shares_up(struct task_group *tg, void *data)
1640 if (!weight) 1595 if (!weight)
1641 weight = NICE_0_LOAD; 1596 weight = NICE_0_LOAD;
1642 1597
1643 rq_weight += weight; 1598 sum_weight += weight;
1644 shares += tg->cfs_rq[i]->shares; 1599 shares += tg->cfs_rq[i]->shares;
1645 } 1600 }
1646 1601
1602 if (!rq_weight)
1603 rq_weight = sum_weight;
1604
1647 if ((!shares && rq_weight) || shares > tg->shares) 1605 if ((!shares && rq_weight) || shares > tg->shares)
1648 shares = tg->shares; 1606 shares = tg->shares;
1649 1607
@@ -1651,7 +1609,7 @@ static int tg_shares_up(struct task_group *tg, void *data)
1651 shares = tg->shares; 1609 shares = tg->shares;
1652 1610
1653 for_each_cpu(i, sched_domain_span(sd)) 1611 for_each_cpu(i, sched_domain_span(sd))
1654 update_group_shares_cpu(tg, i, shares, rq_weight, usd); 1612 update_group_shares_cpu(tg, i, shares, rq_weight, usd_rq_weight);
1655 1613
1656 local_irq_restore(flags); 1614 local_irq_restore(flags);
1657 1615
@@ -1698,16 +1656,6 @@ static void update_shares(struct sched_domain *sd)
1698 } 1656 }
1699} 1657}
1700 1658
1701static void update_shares_locked(struct rq *rq, struct sched_domain *sd)
1702{
1703 if (root_task_group_empty())
1704 return;
1705
1706 spin_unlock(&rq->lock);
1707 update_shares(sd);
1708 spin_lock(&rq->lock);
1709}
1710
1711static void update_h_load(long cpu) 1659static void update_h_load(long cpu)
1712{ 1660{
1713 if (root_task_group_empty()) 1661 if (root_task_group_empty())
@@ -1722,10 +1670,6 @@ static inline void update_shares(struct sched_domain *sd)
1722{ 1670{
1723} 1671}
1724 1672
1725static inline void update_shares_locked(struct rq *rq, struct sched_domain *sd)
1726{
1727}
1728
1729#endif 1673#endif
1730 1674
1731#ifdef CONFIG_PREEMPT 1675#ifdef CONFIG_PREEMPT
@@ -1745,7 +1689,7 @@ static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1745 __acquires(busiest->lock) 1689 __acquires(busiest->lock)
1746 __acquires(this_rq->lock) 1690 __acquires(this_rq->lock)
1747{ 1691{
1748 spin_unlock(&this_rq->lock); 1692 raw_spin_unlock(&this_rq->lock);
1749 double_rq_lock(this_rq, busiest); 1693 double_rq_lock(this_rq, busiest);
1750 1694
1751 return 1; 1695 return 1;
@@ -1766,14 +1710,16 @@ static int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1766{ 1710{
1767 int ret = 0; 1711 int ret = 0;
1768 1712
1769 if (unlikely(!spin_trylock(&busiest->lock))) { 1713 if (unlikely(!raw_spin_trylock(&busiest->lock))) {
1770 if (busiest < this_rq) { 1714 if (busiest < this_rq) {
1771 spin_unlock(&this_rq->lock); 1715 raw_spin_unlock(&this_rq->lock);
1772 spin_lock(&busiest->lock); 1716 raw_spin_lock(&busiest->lock);
1773 spin_lock_nested(&this_rq->lock, SINGLE_DEPTH_NESTING); 1717 raw_spin_lock_nested(&this_rq->lock,
1718 SINGLE_DEPTH_NESTING);
1774 ret = 1; 1719 ret = 1;
1775 } else 1720 } else
1776 spin_lock_nested(&busiest->lock, SINGLE_DEPTH_NESTING); 1721 raw_spin_lock_nested(&busiest->lock,
1722 SINGLE_DEPTH_NESTING);
1777 } 1723 }
1778 return ret; 1724 return ret;
1779} 1725}
@@ -1787,7 +1733,7 @@ static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
1787{ 1733{
1788 if (unlikely(!irqs_disabled())) { 1734 if (unlikely(!irqs_disabled())) {
1789 /* printk() doesn't work good under rq->lock */ 1735 /* printk() doesn't work good under rq->lock */
1790 spin_unlock(&this_rq->lock); 1736 raw_spin_unlock(&this_rq->lock);
1791 BUG_ON(1); 1737 BUG_ON(1);
1792 } 1738 }
1793 1739
@@ -1797,9 +1743,54 @@ static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
1797static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest) 1743static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
1798 __releases(busiest->lock) 1744 __releases(busiest->lock)
1799{ 1745{
1800 spin_unlock(&busiest->lock); 1746 raw_spin_unlock(&busiest->lock);
1801 lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_); 1747 lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
1802} 1748}
1749
1750/*
1751 * double_rq_lock - safely lock two runqueues
1752 *
1753 * Note this does not disable interrupts like task_rq_lock,
1754 * you need to do so manually before calling.
1755 */
1756static void double_rq_lock(struct rq *rq1, struct rq *rq2)
1757 __acquires(rq1->lock)
1758 __acquires(rq2->lock)
1759{
1760 BUG_ON(!irqs_disabled());
1761 if (rq1 == rq2) {
1762 raw_spin_lock(&rq1->lock);
1763 __acquire(rq2->lock); /* Fake it out ;) */
1764 } else {
1765 if (rq1 < rq2) {
1766 raw_spin_lock(&rq1->lock);
1767 raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
1768 } else {
1769 raw_spin_lock(&rq2->lock);
1770 raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
1771 }
1772 }
1773 update_rq_clock(rq1);
1774 update_rq_clock(rq2);
1775}
1776
1777/*
1778 * double_rq_unlock - safely unlock two runqueues
1779 *
1780 * Note this does not restore interrupts like task_rq_unlock,
1781 * you need to do so manually after calling.
1782 */
1783static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
1784 __releases(rq1->lock)
1785 __releases(rq2->lock)
1786{
1787 raw_spin_unlock(&rq1->lock);
1788 if (rq1 != rq2)
1789 raw_spin_unlock(&rq2->lock);
1790 else
1791 __release(rq2->lock);
1792}
1793
1803#endif 1794#endif
1804 1795
1805#ifdef CONFIG_FAIR_GROUP_SCHED 1796#ifdef CONFIG_FAIR_GROUP_SCHED
@@ -1812,19 +1803,31 @@ static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1812#endif 1803#endif
1813 1804
1814static void calc_load_account_active(struct rq *this_rq); 1805static void calc_load_account_active(struct rq *this_rq);
1806static void update_sysctl(void);
1807static int get_update_sysctl_factor(void);
1815 1808
1816#include "sched_stats.h" 1809static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1817#include "sched_idletask.c" 1810{
1818#include "sched_fair.c" 1811 set_task_rq(p, cpu);
1819#include "sched_rt.c" 1812#ifdef CONFIG_SMP
1820#ifdef CONFIG_SCHED_DEBUG 1813 /*
1821# include "sched_debug.c" 1814 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1815 * successfuly executed on another CPU. We must ensure that updates of
1816 * per-task data have been completed by this moment.
1817 */
1818 smp_wmb();
1819 task_thread_info(p)->cpu = cpu;
1822#endif 1820#endif
1821}
1822
1823static const struct sched_class rt_sched_class;
1823 1824
1824#define sched_class_highest (&rt_sched_class) 1825#define sched_class_highest (&rt_sched_class)
1825#define for_each_class(class) \ 1826#define for_each_class(class) \
1826 for (class = sched_class_highest; class; class = class->next) 1827 for (class = sched_class_highest; class; class = class->next)
1827 1828
1829#include "sched_stats.h"
1830
1828static void inc_nr_running(struct rq *rq) 1831static void inc_nr_running(struct rq *rq)
1829{ 1832{
1830 rq->nr_running++; 1833 rq->nr_running++;
@@ -1862,13 +1865,14 @@ static void update_avg(u64 *avg, u64 sample)
1862 *avg += diff >> 3; 1865 *avg += diff >> 3;
1863} 1866}
1864 1867
1865static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup) 1868static void
1869enqueue_task(struct rq *rq, struct task_struct *p, int wakeup, bool head)
1866{ 1870{
1867 if (wakeup) 1871 if (wakeup)
1868 p->se.start_runtime = p->se.sum_exec_runtime; 1872 p->se.start_runtime = p->se.sum_exec_runtime;
1869 1873
1870 sched_info_queued(p); 1874 sched_info_queued(p);
1871 p->sched_class->enqueue_task(rq, p, wakeup); 1875 p->sched_class->enqueue_task(rq, p, wakeup, head);
1872 p->se.on_rq = 1; 1876 p->se.on_rq = 1;
1873} 1877}
1874 1878
@@ -1891,6 +1895,37 @@ static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
1891} 1895}
1892 1896
1893/* 1897/*
1898 * activate_task - move a task to the runqueue.
1899 */
1900static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
1901{
1902 if (task_contributes_to_load(p))
1903 rq->nr_uninterruptible--;
1904
1905 enqueue_task(rq, p, wakeup, false);
1906 inc_nr_running(rq);
1907}
1908
1909/*
1910 * deactivate_task - remove a task from the runqueue.
1911 */
1912static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
1913{
1914 if (task_contributes_to_load(p))
1915 rq->nr_uninterruptible++;
1916
1917 dequeue_task(rq, p, sleep);
1918 dec_nr_running(rq);
1919}
1920
1921#include "sched_idletask.c"
1922#include "sched_fair.c"
1923#include "sched_rt.c"
1924#ifdef CONFIG_SCHED_DEBUG
1925# include "sched_debug.c"
1926#endif
1927
1928/*
1894 * __normal_prio - return the priority that is based on the static prio 1929 * __normal_prio - return the priority that is based on the static prio
1895 */ 1930 */
1896static inline int __normal_prio(struct task_struct *p) 1931static inline int __normal_prio(struct task_struct *p)
@@ -1936,30 +1971,6 @@ static int effective_prio(struct task_struct *p)
1936 return p->prio; 1971 return p->prio;
1937} 1972}
1938 1973
1939/*
1940 * activate_task - move a task to the runqueue.
1941 */
1942static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
1943{
1944 if (task_contributes_to_load(p))
1945 rq->nr_uninterruptible--;
1946
1947 enqueue_task(rq, p, wakeup);
1948 inc_nr_running(rq);
1949}
1950
1951/*
1952 * deactivate_task - remove a task from the runqueue.
1953 */
1954static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
1955{
1956 if (task_contributes_to_load(p))
1957 rq->nr_uninterruptible++;
1958
1959 dequeue_task(rq, p, sleep);
1960 dec_nr_running(rq);
1961}
1962
1963/** 1974/**
1964 * task_curr - is this task currently executing on a CPU? 1975 * task_curr - is this task currently executing on a CPU?
1965 * @p: the task in question. 1976 * @p: the task in question.
@@ -1969,20 +1980,6 @@ inline int task_curr(const struct task_struct *p)
1969 return cpu_curr(task_cpu(p)) == p; 1980 return cpu_curr(task_cpu(p)) == p;
1970} 1981}
1971 1982
1972static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1973{
1974 set_task_rq(p, cpu);
1975#ifdef CONFIG_SMP
1976 /*
1977 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1978 * successfuly executed on another CPU. We must ensure that updates of
1979 * per-task data have been completed by this moment.
1980 */
1981 smp_wmb();
1982 task_thread_info(p)->cpu = cpu;
1983#endif
1984}
1985
1986static inline void check_class_changed(struct rq *rq, struct task_struct *p, 1983static inline void check_class_changed(struct rq *rq, struct task_struct *p,
1987 const struct sched_class *prev_class, 1984 const struct sched_class *prev_class,
1988 int oldprio, int running) 1985 int oldprio, int running)
@@ -2004,17 +2001,17 @@ task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
2004{ 2001{
2005 s64 delta; 2002 s64 delta;
2006 2003
2004 if (p->sched_class != &fair_sched_class)
2005 return 0;
2006
2007 /* 2007 /*
2008 * Buddy candidates are cache hot: 2008 * Buddy candidates are cache hot:
2009 */ 2009 */
2010 if (sched_feat(CACHE_HOT_BUDDY) && 2010 if (sched_feat(CACHE_HOT_BUDDY) && this_rq()->nr_running &&
2011 (&p->se == cfs_rq_of(&p->se)->next || 2011 (&p->se == cfs_rq_of(&p->se)->next ||
2012 &p->se == cfs_rq_of(&p->se)->last)) 2012 &p->se == cfs_rq_of(&p->se)->last))
2013 return 1; 2013 return 1;
2014 2014
2015 if (p->sched_class != &fair_sched_class)
2016 return 0;
2017
2018 if (sysctl_sched_migration_cost == -1) 2015 if (sysctl_sched_migration_cost == -1)
2019 return 1; 2016 return 1;
2020 if (sysctl_sched_migration_cost == 0) 2017 if (sysctl_sched_migration_cost == 0)
@@ -2025,39 +2022,23 @@ task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
2025 return delta < (s64)sysctl_sched_migration_cost; 2022 return delta < (s64)sysctl_sched_migration_cost;
2026} 2023}
2027 2024
2028
2029void set_task_cpu(struct task_struct *p, unsigned int new_cpu) 2025void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
2030{ 2026{
2031 int old_cpu = task_cpu(p); 2027#ifdef CONFIG_SCHED_DEBUG
2032 struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu); 2028 /*
2033 struct cfs_rq *old_cfsrq = task_cfs_rq(p), 2029 * We should never call set_task_cpu() on a blocked task,
2034 *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu); 2030 * ttwu() will sort out the placement.
2035 u64 clock_offset; 2031 */
2036 2032 WARN_ON_ONCE(p->state != TASK_RUNNING && p->state != TASK_WAKING &&
2037 clock_offset = old_rq->clock - new_rq->clock; 2033 !(task_thread_info(p)->preempt_count & PREEMPT_ACTIVE));
2034#endif
2038 2035
2039 trace_sched_migrate_task(p, new_cpu); 2036 trace_sched_migrate_task(p, new_cpu);
2040 2037
2041#ifdef CONFIG_SCHEDSTATS 2038 if (task_cpu(p) != new_cpu) {
2042 if (p->se.wait_start)
2043 p->se.wait_start -= clock_offset;
2044 if (p->se.sleep_start)
2045 p->se.sleep_start -= clock_offset;
2046 if (p->se.block_start)
2047 p->se.block_start -= clock_offset;
2048#endif
2049 if (old_cpu != new_cpu) {
2050 p->se.nr_migrations++; 2039 p->se.nr_migrations++;
2051 new_rq->nr_migrations_in++; 2040 perf_sw_event(PERF_COUNT_SW_CPU_MIGRATIONS, 1, 1, NULL, 0);
2052#ifdef CONFIG_SCHEDSTATS
2053 if (task_hot(p, old_rq->clock, NULL))
2054 schedstat_inc(p, se.nr_forced2_migrations);
2055#endif
2056 perf_sw_event(PERF_COUNT_SW_CPU_MIGRATIONS,
2057 1, 1, NULL, 0);
2058 } 2041 }
2059 p->se.vruntime -= old_cfsrq->min_vruntime -
2060 new_cfsrq->min_vruntime;
2061 2042
2062 __set_task_cpu(p, new_cpu); 2043 __set_task_cpu(p, new_cpu);
2063} 2044}
@@ -2082,12 +2063,10 @@ migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
2082 2063
2083 /* 2064 /*
2084 * If the task is not on a runqueue (and not running), then 2065 * If the task is not on a runqueue (and not running), then
2085 * it is sufficient to simply update the task's cpu field. 2066 * the next wake-up will properly place the task.
2086 */ 2067 */
2087 if (!p->se.on_rq && !task_running(rq, p)) { 2068 if (!p->se.on_rq && !task_running(rq, p))
2088 set_task_cpu(p, dest_cpu);
2089 return 0; 2069 return 0;
2090 }
2091 2070
2092 init_completion(&req->done); 2071 init_completion(&req->done);
2093 req->task = p; 2072 req->task = p;
@@ -2292,6 +2271,75 @@ void task_oncpu_function_call(struct task_struct *p,
2292 preempt_enable(); 2271 preempt_enable();
2293} 2272}
2294 2273
2274#ifdef CONFIG_SMP
2275static int select_fallback_rq(int cpu, struct task_struct *p)
2276{
2277 int dest_cpu;
2278 const struct cpumask *nodemask = cpumask_of_node(cpu_to_node(cpu));
2279
2280 /* Look for allowed, online CPU in same node. */
2281 for_each_cpu_and(dest_cpu, nodemask, cpu_active_mask)
2282 if (cpumask_test_cpu(dest_cpu, &p->cpus_allowed))
2283 return dest_cpu;
2284
2285 /* Any allowed, online CPU? */
2286 dest_cpu = cpumask_any_and(&p->cpus_allowed, cpu_active_mask);
2287 if (dest_cpu < nr_cpu_ids)
2288 return dest_cpu;
2289
2290 /* No more Mr. Nice Guy. */
2291 if (dest_cpu >= nr_cpu_ids) {
2292 rcu_read_lock();
2293 cpuset_cpus_allowed_locked(p, &p->cpus_allowed);
2294 rcu_read_unlock();
2295 dest_cpu = cpumask_any_and(cpu_active_mask, &p->cpus_allowed);
2296
2297 /*
2298 * Don't tell them about moving exiting tasks or
2299 * kernel threads (both mm NULL), since they never
2300 * leave kernel.
2301 */
2302 if (p->mm && printk_ratelimit()) {
2303 printk(KERN_INFO "process %d (%s) no "
2304 "longer affine to cpu%d\n",
2305 task_pid_nr(p), p->comm, cpu);
2306 }
2307 }
2308
2309 return dest_cpu;
2310}
2311
2312/*
2313 * Gets called from 3 sites (exec, fork, wakeup), since it is called without
2314 * holding rq->lock we need to ensure ->cpus_allowed is stable, this is done
2315 * by:
2316 *
2317 * exec: is unstable, retry loop
2318 * fork & wake-up: serialize ->cpus_allowed against TASK_WAKING
2319 */
2320static inline
2321int select_task_rq(struct task_struct *p, int sd_flags, int wake_flags)
2322{
2323 int cpu = p->sched_class->select_task_rq(p, sd_flags, wake_flags);
2324
2325 /*
2326 * In order not to call set_task_cpu() on a blocking task we need
2327 * to rely on ttwu() to place the task on a valid ->cpus_allowed
2328 * cpu.
2329 *
2330 * Since this is common to all placement strategies, this lives here.
2331 *
2332 * [ this allows ->select_task() to simply return task_cpu(p) and
2333 * not worry about this generic constraint ]
2334 */
2335 if (unlikely(!cpumask_test_cpu(cpu, &p->cpus_allowed) ||
2336 !cpu_online(cpu)))
2337 cpu = select_fallback_rq(task_cpu(p), p);
2338
2339 return cpu;
2340}
2341#endif
2342
2295/*** 2343/***
2296 * try_to_wake_up - wake up a thread 2344 * try_to_wake_up - wake up a thread
2297 * @p: the to-be-woken-up thread 2345 * @p: the to-be-woken-up thread
@@ -2343,15 +2391,34 @@ static int try_to_wake_up(struct task_struct *p, unsigned int state,
2343 if (task_contributes_to_load(p)) 2391 if (task_contributes_to_load(p))
2344 rq->nr_uninterruptible--; 2392 rq->nr_uninterruptible--;
2345 p->state = TASK_WAKING; 2393 p->state = TASK_WAKING;
2346 task_rq_unlock(rq, &flags);
2347 2394
2348 cpu = p->sched_class->select_task_rq(p, SD_BALANCE_WAKE, wake_flags); 2395 if (p->sched_class->task_waking)
2349 if (cpu != orig_cpu) 2396 p->sched_class->task_waking(rq, p);
2397
2398 __task_rq_unlock(rq);
2399
2400 cpu = select_task_rq(p, SD_BALANCE_WAKE, wake_flags);
2401 if (cpu != orig_cpu) {
2402 /*
2403 * Since we migrate the task without holding any rq->lock,
2404 * we need to be careful with task_rq_lock(), since that
2405 * might end up locking an invalid rq.
2406 */
2350 set_task_cpu(p, cpu); 2407 set_task_cpu(p, cpu);
2408 }
2351 2409
2352 rq = task_rq_lock(p, &flags); 2410 rq = cpu_rq(cpu);
2411 raw_spin_lock(&rq->lock);
2412 update_rq_clock(rq);
2413
2414 /*
2415 * We migrated the task without holding either rq->lock, however
2416 * since the task is not on the task list itself, nobody else
2417 * will try and migrate the task, hence the rq should match the
2418 * cpu we just moved it to.
2419 */
2420 WARN_ON(task_cpu(p) != cpu);
2353 WARN_ON(p->state != TASK_WAKING); 2421 WARN_ON(p->state != TASK_WAKING);
2354 cpu = task_cpu(p);
2355 2422
2356#ifdef CONFIG_SCHEDSTATS 2423#ifdef CONFIG_SCHEDSTATS
2357 schedstat_inc(rq, ttwu_count); 2424 schedstat_inc(rq, ttwu_count);
@@ -2404,8 +2471,19 @@ out_running:
2404 2471
2405 p->state = TASK_RUNNING; 2472 p->state = TASK_RUNNING;
2406#ifdef CONFIG_SMP 2473#ifdef CONFIG_SMP
2407 if (p->sched_class->task_wake_up) 2474 if (p->sched_class->task_woken)
2408 p->sched_class->task_wake_up(rq, p); 2475 p->sched_class->task_woken(rq, p);
2476
2477 if (unlikely(rq->idle_stamp)) {
2478 u64 delta = rq->clock - rq->idle_stamp;
2479 u64 max = 2*sysctl_sched_migration_cost;
2480
2481 if (delta > max)
2482 rq->avg_idle = max;
2483 else
2484 update_avg(&rq->avg_idle, delta);
2485 rq->idle_stamp = 0;
2486 }
2409#endif 2487#endif
2410out: 2488out:
2411 task_rq_unlock(rq, &flags); 2489 task_rq_unlock(rq, &flags);
@@ -2452,7 +2530,6 @@ static void __sched_fork(struct task_struct *p)
2452 p->se.avg_overlap = 0; 2530 p->se.avg_overlap = 0;
2453 p->se.start_runtime = 0; 2531 p->se.start_runtime = 0;
2454 p->se.avg_wakeup = sysctl_sched_wakeup_granularity; 2532 p->se.avg_wakeup = sysctl_sched_wakeup_granularity;
2455 p->se.avg_running = 0;
2456 2533
2457#ifdef CONFIG_SCHEDSTATS 2534#ifdef CONFIG_SCHEDSTATS
2458 p->se.wait_start = 0; 2535 p->se.wait_start = 0;
@@ -2474,7 +2551,6 @@ static void __sched_fork(struct task_struct *p)
2474 p->se.nr_failed_migrations_running = 0; 2551 p->se.nr_failed_migrations_running = 0;
2475 p->se.nr_failed_migrations_hot = 0; 2552 p->se.nr_failed_migrations_hot = 0;
2476 p->se.nr_forced_migrations = 0; 2553 p->se.nr_forced_migrations = 0;
2477 p->se.nr_forced2_migrations = 0;
2478 2554
2479 p->se.nr_wakeups = 0; 2555 p->se.nr_wakeups = 0;
2480 p->se.nr_wakeups_sync = 0; 2556 p->se.nr_wakeups_sync = 0;
@@ -2495,14 +2571,6 @@ static void __sched_fork(struct task_struct *p)
2495#ifdef CONFIG_PREEMPT_NOTIFIERS 2571#ifdef CONFIG_PREEMPT_NOTIFIERS
2496 INIT_HLIST_HEAD(&p->preempt_notifiers); 2572 INIT_HLIST_HEAD(&p->preempt_notifiers);
2497#endif 2573#endif
2498
2499 /*
2500 * We mark the process as running here, but have not actually
2501 * inserted it onto the runqueue yet. This guarantees that
2502 * nobody will actually run it, and a signal or other external
2503 * event cannot wake it up and insert it on the runqueue either.
2504 */
2505 p->state = TASK_RUNNING;
2506} 2574}
2507 2575
2508/* 2576/*
@@ -2513,24 +2581,25 @@ void sched_fork(struct task_struct *p, int clone_flags)
2513 int cpu = get_cpu(); 2581 int cpu = get_cpu();
2514 2582
2515 __sched_fork(p); 2583 __sched_fork(p);
2516
2517 /* 2584 /*
2518 * Make sure we do not leak PI boosting priority to the child. 2585 * We mark the process as waking here. This guarantees that
2586 * nobody will actually run it, and a signal or other external
2587 * event cannot wake it up and insert it on the runqueue either.
2519 */ 2588 */
2520 p->prio = current->normal_prio; 2589 p->state = TASK_WAKING;
2521 2590
2522 /* 2591 /*
2523 * Revert to default priority/policy on fork if requested. 2592 * Revert to default priority/policy on fork if requested.
2524 */ 2593 */
2525 if (unlikely(p->sched_reset_on_fork)) { 2594 if (unlikely(p->sched_reset_on_fork)) {
2526 if (p->policy == SCHED_FIFO || p->policy == SCHED_RR) 2595 if (p->policy == SCHED_FIFO || p->policy == SCHED_RR) {
2527 p->policy = SCHED_NORMAL; 2596 p->policy = SCHED_NORMAL;
2528 2597 p->normal_prio = p->static_prio;
2529 if (p->normal_prio < DEFAULT_PRIO) 2598 }
2530 p->prio = DEFAULT_PRIO;
2531 2599
2532 if (PRIO_TO_NICE(p->static_prio) < 0) { 2600 if (PRIO_TO_NICE(p->static_prio) < 0) {
2533 p->static_prio = NICE_TO_PRIO(0); 2601 p->static_prio = NICE_TO_PRIO(0);
2602 p->normal_prio = p->static_prio;
2534 set_load_weight(p); 2603 set_load_weight(p);
2535 } 2604 }
2536 2605
@@ -2541,12 +2610,17 @@ void sched_fork(struct task_struct *p, int clone_flags)
2541 p->sched_reset_on_fork = 0; 2610 p->sched_reset_on_fork = 0;
2542 } 2611 }
2543 2612
2613 /*
2614 * Make sure we do not leak PI boosting priority to the child.
2615 */
2616 p->prio = current->normal_prio;
2617
2544 if (!rt_prio(p->prio)) 2618 if (!rt_prio(p->prio))
2545 p->sched_class = &fair_sched_class; 2619 p->sched_class = &fair_sched_class;
2546 2620
2547#ifdef CONFIG_SMP 2621 if (p->sched_class->task_fork)
2548 cpu = p->sched_class->select_task_rq(p, SD_BALANCE_FORK, 0); 2622 p->sched_class->task_fork(p);
2549#endif 2623
2550 set_task_cpu(p, cpu); 2624 set_task_cpu(p, cpu);
2551 2625
2552#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT) 2626#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
@@ -2576,30 +2650,41 @@ void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
2576{ 2650{
2577 unsigned long flags; 2651 unsigned long flags;
2578 struct rq *rq; 2652 struct rq *rq;
2653 int cpu = get_cpu();
2579 2654
2580 rq = task_rq_lock(p, &flags); 2655#ifdef CONFIG_SMP
2581 BUG_ON(p->state != TASK_RUNNING); 2656 /*
2582 update_rq_clock(rq); 2657 * Fork balancing, do it here and not earlier because:
2658 * - cpus_allowed can change in the fork path
2659 * - any previously selected cpu might disappear through hotplug
2660 *
2661 * We still have TASK_WAKING but PF_STARTING is gone now, meaning
2662 * ->cpus_allowed is stable, we have preemption disabled, meaning
2663 * cpu_online_mask is stable.
2664 */
2665 cpu = select_task_rq(p, SD_BALANCE_FORK, 0);
2666 set_task_cpu(p, cpu);
2667#endif
2583 2668
2584 p->prio = effective_prio(p); 2669 /*
2670 * Since the task is not on the rq and we still have TASK_WAKING set
2671 * nobody else will migrate this task.
2672 */
2673 rq = cpu_rq(cpu);
2674 raw_spin_lock_irqsave(&rq->lock, flags);
2585 2675
2586 if (!p->sched_class->task_new || !current->se.on_rq) { 2676 BUG_ON(p->state != TASK_WAKING);
2587 activate_task(rq, p, 0); 2677 p->state = TASK_RUNNING;
2588 } else { 2678 update_rq_clock(rq);
2589 /* 2679 activate_task(rq, p, 0);
2590 * Let the scheduling class do new task startup
2591 * management (if any):
2592 */
2593 p->sched_class->task_new(rq, p);
2594 inc_nr_running(rq);
2595 }
2596 trace_sched_wakeup_new(rq, p, 1); 2680 trace_sched_wakeup_new(rq, p, 1);
2597 check_preempt_curr(rq, p, WF_FORK); 2681 check_preempt_curr(rq, p, WF_FORK);
2598#ifdef CONFIG_SMP 2682#ifdef CONFIG_SMP
2599 if (p->sched_class->task_wake_up) 2683 if (p->sched_class->task_woken)
2600 p->sched_class->task_wake_up(rq, p); 2684 p->sched_class->task_woken(rq, p);
2601#endif 2685#endif
2602 task_rq_unlock(rq, &flags); 2686 task_rq_unlock(rq, &flags);
2687 put_cpu();
2603} 2688}
2604 2689
2605#ifdef CONFIG_PREEMPT_NOTIFIERS 2690#ifdef CONFIG_PREEMPT_NOTIFIERS
@@ -2718,7 +2803,13 @@ static void finish_task_switch(struct rq *rq, struct task_struct *prev)
2718 */ 2803 */
2719 prev_state = prev->state; 2804 prev_state = prev->state;
2720 finish_arch_switch(prev); 2805 finish_arch_switch(prev);
2721 perf_event_task_sched_in(current, cpu_of(rq)); 2806#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
2807 local_irq_disable();
2808#endif /* __ARCH_WANT_INTERRUPTS_ON_CTXSW */
2809 perf_event_task_sched_in(current);
2810#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
2811 local_irq_enable();
2812#endif /* __ARCH_WANT_INTERRUPTS_ON_CTXSW */
2722 finish_lock_switch(rq, prev); 2813 finish_lock_switch(rq, prev);
2723 2814
2724 fire_sched_in_preempt_notifiers(current); 2815 fire_sched_in_preempt_notifiers(current);
@@ -2749,10 +2840,10 @@ static inline void post_schedule(struct rq *rq)
2749 if (rq->post_schedule) { 2840 if (rq->post_schedule) {
2750 unsigned long flags; 2841 unsigned long flags;
2751 2842
2752 spin_lock_irqsave(&rq->lock, flags); 2843 raw_spin_lock_irqsave(&rq->lock, flags);
2753 if (rq->curr->sched_class->post_schedule) 2844 if (rq->curr->sched_class->post_schedule)
2754 rq->curr->sched_class->post_schedule(rq); 2845 rq->curr->sched_class->post_schedule(rq);
2755 spin_unlock_irqrestore(&rq->lock, flags); 2846 raw_spin_unlock_irqrestore(&rq->lock, flags);
2756 2847
2757 rq->post_schedule = 0; 2848 rq->post_schedule = 0;
2758 } 2849 }
@@ -2816,14 +2907,14 @@ context_switch(struct rq *rq, struct task_struct *prev,
2816 */ 2907 */
2817 arch_start_context_switch(prev); 2908 arch_start_context_switch(prev);
2818 2909
2819 if (unlikely(!mm)) { 2910 if (likely(!mm)) {
2820 next->active_mm = oldmm; 2911 next->active_mm = oldmm;
2821 atomic_inc(&oldmm->mm_count); 2912 atomic_inc(&oldmm->mm_count);
2822 enter_lazy_tlb(oldmm, next); 2913 enter_lazy_tlb(oldmm, next);
2823 } else 2914 } else
2824 switch_mm(oldmm, mm, next); 2915 switch_mm(oldmm, mm, next);
2825 2916
2826 if (unlikely(!prev->mm)) { 2917 if (likely(!prev->mm)) {
2827 prev->active_mm = NULL; 2918 prev->active_mm = NULL;
2828 rq->prev_mm = oldmm; 2919 rq->prev_mm = oldmm;
2829 } 2920 }
@@ -2986,15 +3077,6 @@ static void calc_load_account_active(struct rq *this_rq)
2986} 3077}
2987 3078
2988/* 3079/*
2989 * Externally visible per-cpu scheduler statistics:
2990 * cpu_nr_migrations(cpu) - number of migrations into that cpu
2991 */
2992u64 cpu_nr_migrations(int cpu)
2993{
2994 return cpu_rq(cpu)->nr_migrations_in;
2995}
2996
2997/*
2998 * Update rq->cpu_load[] statistics. This function is usually called every 3080 * Update rq->cpu_load[] statistics. This function is usually called every
2999 * scheduler tick (TICK_NSEC). 3081 * scheduler tick (TICK_NSEC).
3000 */ 3082 */
@@ -3032,65 +3114,36 @@ static void update_cpu_load(struct rq *this_rq)
3032#ifdef CONFIG_SMP 3114#ifdef CONFIG_SMP
3033 3115
3034/* 3116/*
3035 * double_rq_lock - safely lock two runqueues 3117 * sched_exec - execve() is a valuable balancing opportunity, because at
3036 * 3118 * this point the task has the smallest effective memory and cache footprint.
3037 * Note this does not disable interrupts like task_rq_lock,
3038 * you need to do so manually before calling.
3039 */
3040static void double_rq_lock(struct rq *rq1, struct rq *rq2)
3041 __acquires(rq1->lock)
3042 __acquires(rq2->lock)
3043{
3044 BUG_ON(!irqs_disabled());
3045 if (rq1 == rq2) {
3046 spin_lock(&rq1->lock);
3047 __acquire(rq2->lock); /* Fake it out ;) */
3048 } else {
3049 if (rq1 < rq2) {
3050 spin_lock(&rq1->lock);
3051 spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
3052 } else {
3053 spin_lock(&rq2->lock);
3054 spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
3055 }
3056 }
3057 update_rq_clock(rq1);
3058 update_rq_clock(rq2);
3059}
3060
3061/*
3062 * double_rq_unlock - safely unlock two runqueues
3063 *
3064 * Note this does not restore interrupts like task_rq_unlock,
3065 * you need to do so manually after calling.
3066 */
3067static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
3068 __releases(rq1->lock)
3069 __releases(rq2->lock)
3070{
3071 spin_unlock(&rq1->lock);
3072 if (rq1 != rq2)
3073 spin_unlock(&rq2->lock);
3074 else
3075 __release(rq2->lock);
3076}
3077
3078/*
3079 * If dest_cpu is allowed for this process, migrate the task to it.
3080 * This is accomplished by forcing the cpu_allowed mask to only
3081 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
3082 * the cpu_allowed mask is restored.
3083 */ 3119 */
3084static void sched_migrate_task(struct task_struct *p, int dest_cpu) 3120void sched_exec(void)
3085{ 3121{
3122 struct task_struct *p = current;
3086 struct migration_req req; 3123 struct migration_req req;
3124 int dest_cpu, this_cpu;
3087 unsigned long flags; 3125 unsigned long flags;
3088 struct rq *rq; 3126 struct rq *rq;
3089 3127
3128again:
3129 this_cpu = get_cpu();
3130 dest_cpu = select_task_rq(p, SD_BALANCE_EXEC, 0);
3131 if (dest_cpu == this_cpu) {
3132 put_cpu();
3133 return;
3134 }
3135
3090 rq = task_rq_lock(p, &flags); 3136 rq = task_rq_lock(p, &flags);
3137 put_cpu();
3138
3139 /*
3140 * select_task_rq() can race against ->cpus_allowed
3141 */
3091 if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed) 3142 if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed)
3092 || unlikely(!cpu_active(dest_cpu))) 3143 || unlikely(!cpu_active(dest_cpu))) {
3093 goto out; 3144 task_rq_unlock(rq, &flags);
3145 goto again;
3146 }
3094 3147
3095 /* force the process onto the specified CPU */ 3148 /* force the process onto the specified CPU */
3096 if (migrate_task(p, dest_cpu, &req)) { 3149 if (migrate_task(p, dest_cpu, &req)) {
@@ -3105,1783 +3158,9 @@ static void sched_migrate_task(struct task_struct *p, int dest_cpu)
3105 3158
3106 return; 3159 return;
3107 } 3160 }
3108out:
3109 task_rq_unlock(rq, &flags); 3161 task_rq_unlock(rq, &flags);
3110} 3162}
3111 3163
3112/*
3113 * sched_exec - execve() is a valuable balancing opportunity, because at
3114 * this point the task has the smallest effective memory and cache footprint.
3115 */
3116void sched_exec(void)
3117{
3118 int new_cpu, this_cpu = get_cpu();
3119 new_cpu = current->sched_class->select_task_rq(current, SD_BALANCE_EXEC, 0);
3120 put_cpu();
3121 if (new_cpu != this_cpu)
3122 sched_migrate_task(current, new_cpu);
3123}
3124
3125/*
3126 * pull_task - move a task from a remote runqueue to the local runqueue.
3127 * Both runqueues must be locked.
3128 */
3129static void pull_task(struct rq *src_rq, struct task_struct *p,
3130 struct rq *this_rq, int this_cpu)
3131{
3132 deactivate_task(src_rq, p, 0);
3133 set_task_cpu(p, this_cpu);
3134 activate_task(this_rq, p, 0);
3135 /*
3136 * Note that idle threads have a prio of MAX_PRIO, for this test
3137 * to be always true for them.
3138 */
3139 check_preempt_curr(this_rq, p, 0);
3140}
3141
3142/*
3143 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
3144 */
3145static
3146int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
3147 struct sched_domain *sd, enum cpu_idle_type idle,
3148 int *all_pinned)
3149{
3150 int tsk_cache_hot = 0;
3151 /*
3152 * We do not migrate tasks that are:
3153 * 1) running (obviously), or
3154 * 2) cannot be migrated to this CPU due to cpus_allowed, or
3155 * 3) are cache-hot on their current CPU.
3156 */
3157 if (!cpumask_test_cpu(this_cpu, &p->cpus_allowed)) {
3158 schedstat_inc(p, se.nr_failed_migrations_affine);
3159 return 0;
3160 }
3161 *all_pinned = 0;
3162
3163 if (task_running(rq, p)) {
3164 schedstat_inc(p, se.nr_failed_migrations_running);
3165 return 0;
3166 }
3167
3168 /*
3169 * Aggressive migration if:
3170 * 1) task is cache cold, or
3171 * 2) too many balance attempts have failed.
3172 */
3173
3174 tsk_cache_hot = task_hot(p, rq->clock, sd);
3175 if (!tsk_cache_hot ||
3176 sd->nr_balance_failed > sd->cache_nice_tries) {
3177#ifdef CONFIG_SCHEDSTATS
3178 if (tsk_cache_hot) {
3179 schedstat_inc(sd, lb_hot_gained[idle]);
3180 schedstat_inc(p, se.nr_forced_migrations);
3181 }
3182#endif
3183 return 1;
3184 }
3185
3186 if (tsk_cache_hot) {
3187 schedstat_inc(p, se.nr_failed_migrations_hot);
3188 return 0;
3189 }
3190 return 1;
3191}
3192
3193static unsigned long
3194balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
3195 unsigned long max_load_move, struct sched_domain *sd,
3196 enum cpu_idle_type idle, int *all_pinned,
3197 int *this_best_prio, struct rq_iterator *iterator)
3198{
3199 int loops = 0, pulled = 0, pinned = 0;
3200 struct task_struct *p;
3201 long rem_load_move = max_load_move;
3202
3203 if (max_load_move == 0)
3204 goto out;
3205
3206 pinned = 1;
3207
3208 /*
3209 * Start the load-balancing iterator:
3210 */
3211 p = iterator->start(iterator->arg);
3212next:
3213 if (!p || loops++ > sysctl_sched_nr_migrate)
3214 goto out;
3215
3216 if ((p->se.load.weight >> 1) > rem_load_move ||
3217 !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
3218 p = iterator->next(iterator->arg);
3219 goto next;
3220 }
3221
3222 pull_task(busiest, p, this_rq, this_cpu);
3223 pulled++;
3224 rem_load_move -= p->se.load.weight;
3225
3226#ifdef CONFIG_PREEMPT
3227 /*
3228 * NEWIDLE balancing is a source of latency, so preemptible kernels
3229 * will stop after the first task is pulled to minimize the critical
3230 * section.
3231 */
3232 if (idle == CPU_NEWLY_IDLE)
3233 goto out;
3234#endif
3235
3236 /*
3237 * We only want to steal up to the prescribed amount of weighted load.
3238 */
3239 if (rem_load_move > 0) {
3240 if (p->prio < *this_best_prio)
3241 *this_best_prio = p->prio;
3242 p = iterator->next(iterator->arg);
3243 goto next;
3244 }
3245out:
3246 /*
3247 * Right now, this is one of only two places pull_task() is called,
3248 * so we can safely collect pull_task() stats here rather than
3249 * inside pull_task().
3250 */
3251 schedstat_add(sd, lb_gained[idle], pulled);
3252
3253 if (all_pinned)
3254 *all_pinned = pinned;
3255
3256 return max_load_move - rem_load_move;
3257}
3258
3259/*
3260 * move_tasks tries to move up to max_load_move weighted load from busiest to
3261 * this_rq, as part of a balancing operation within domain "sd".
3262 * Returns 1 if successful and 0 otherwise.
3263 *
3264 * Called with both runqueues locked.
3265 */
3266static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
3267 unsigned long max_load_move,
3268 struct sched_domain *sd, enum cpu_idle_type idle,
3269 int *all_pinned)
3270{
3271 const struct sched_class *class = sched_class_highest;
3272 unsigned long total_load_moved = 0;
3273 int this_best_prio = this_rq->curr->prio;
3274
3275 do {
3276 total_load_moved +=
3277 class->load_balance(this_rq, this_cpu, busiest,
3278 max_load_move - total_load_moved,
3279 sd, idle, all_pinned, &this_best_prio);
3280 class = class->next;
3281
3282#ifdef CONFIG_PREEMPT
3283 /*
3284 * NEWIDLE balancing is a source of latency, so preemptible
3285 * kernels will stop after the first task is pulled to minimize
3286 * the critical section.
3287 */
3288 if (idle == CPU_NEWLY_IDLE && this_rq->nr_running)
3289 break;
3290#endif
3291 } while (class && max_load_move > total_load_moved);
3292
3293 return total_load_moved > 0;
3294}
3295
3296static int
3297iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3298 struct sched_domain *sd, enum cpu_idle_type idle,
3299 struct rq_iterator *iterator)
3300{
3301 struct task_struct *p = iterator->start(iterator->arg);
3302 int pinned = 0;
3303
3304 while (p) {
3305 if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
3306 pull_task(busiest, p, this_rq, this_cpu);
3307 /*
3308 * Right now, this is only the second place pull_task()
3309 * is called, so we can safely collect pull_task()
3310 * stats here rather than inside pull_task().
3311 */
3312 schedstat_inc(sd, lb_gained[idle]);
3313
3314 return 1;
3315 }
3316 p = iterator->next(iterator->arg);
3317 }
3318
3319 return 0;
3320}
3321
3322/*
3323 * move_one_task tries to move exactly one task from busiest to this_rq, as
3324 * part of active balancing operations within "domain".
3325 * Returns 1 if successful and 0 otherwise.
3326 *
3327 * Called with both runqueues locked.
3328 */
3329static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3330 struct sched_domain *sd, enum cpu_idle_type idle)
3331{
3332 const struct sched_class *class;
3333
3334 for_each_class(class) {
3335 if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle))
3336 return 1;
3337 }
3338
3339 return 0;
3340}
3341/********** Helpers for find_busiest_group ************************/
3342/*
3343 * sd_lb_stats - Structure to store the statistics of a sched_domain
3344 * during load balancing.
3345 */
3346struct sd_lb_stats {
3347 struct sched_group *busiest; /* Busiest group in this sd */
3348 struct sched_group *this; /* Local group in this sd */
3349 unsigned long total_load; /* Total load of all groups in sd */
3350 unsigned long total_pwr; /* Total power of all groups in sd */
3351 unsigned long avg_load; /* Average load across all groups in sd */
3352
3353 /** Statistics of this group */
3354 unsigned long this_load;
3355 unsigned long this_load_per_task;
3356 unsigned long this_nr_running;
3357
3358 /* Statistics of the busiest group */
3359 unsigned long max_load;
3360 unsigned long busiest_load_per_task;
3361 unsigned long busiest_nr_running;
3362
3363 int group_imb; /* Is there imbalance in this sd */
3364#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3365 int power_savings_balance; /* Is powersave balance needed for this sd */
3366 struct sched_group *group_min; /* Least loaded group in sd */
3367 struct sched_group *group_leader; /* Group which relieves group_min */
3368 unsigned long min_load_per_task; /* load_per_task in group_min */
3369 unsigned long leader_nr_running; /* Nr running of group_leader */
3370 unsigned long min_nr_running; /* Nr running of group_min */
3371#endif
3372};
3373
3374/*
3375 * sg_lb_stats - stats of a sched_group required for load_balancing
3376 */
3377struct sg_lb_stats {
3378 unsigned long avg_load; /*Avg load across the CPUs of the group */
3379 unsigned long group_load; /* Total load over the CPUs of the group */
3380 unsigned long sum_nr_running; /* Nr tasks running in the group */
3381 unsigned long sum_weighted_load; /* Weighted load of group's tasks */
3382 unsigned long group_capacity;
3383 int group_imb; /* Is there an imbalance in the group ? */
3384};
3385
3386/**
3387 * group_first_cpu - Returns the first cpu in the cpumask of a sched_group.
3388 * @group: The group whose first cpu is to be returned.
3389 */
3390static inline unsigned int group_first_cpu(struct sched_group *group)
3391{
3392 return cpumask_first(sched_group_cpus(group));
3393}
3394
3395/**
3396 * get_sd_load_idx - Obtain the load index for a given sched domain.
3397 * @sd: The sched_domain whose load_idx is to be obtained.
3398 * @idle: The Idle status of the CPU for whose sd load_icx is obtained.
3399 */
3400static inline int get_sd_load_idx(struct sched_domain *sd,
3401 enum cpu_idle_type idle)
3402{
3403 int load_idx;
3404
3405 switch (idle) {
3406 case CPU_NOT_IDLE:
3407 load_idx = sd->busy_idx;
3408 break;
3409
3410 case CPU_NEWLY_IDLE:
3411 load_idx = sd->newidle_idx;
3412 break;
3413 default:
3414 load_idx = sd->idle_idx;
3415 break;
3416 }
3417
3418 return load_idx;
3419}
3420
3421
3422#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3423/**
3424 * init_sd_power_savings_stats - Initialize power savings statistics for
3425 * the given sched_domain, during load balancing.
3426 *
3427 * @sd: Sched domain whose power-savings statistics are to be initialized.
3428 * @sds: Variable containing the statistics for sd.
3429 * @idle: Idle status of the CPU at which we're performing load-balancing.
3430 */
3431static inline void init_sd_power_savings_stats(struct sched_domain *sd,
3432 struct sd_lb_stats *sds, enum cpu_idle_type idle)
3433{
3434 /*
3435 * Busy processors will not participate in power savings
3436 * balance.
3437 */
3438 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
3439 sds->power_savings_balance = 0;
3440 else {
3441 sds->power_savings_balance = 1;
3442 sds->min_nr_running = ULONG_MAX;
3443 sds->leader_nr_running = 0;
3444 }
3445}
3446
3447/**
3448 * update_sd_power_savings_stats - Update the power saving stats for a
3449 * sched_domain while performing load balancing.
3450 *
3451 * @group: sched_group belonging to the sched_domain under consideration.
3452 * @sds: Variable containing the statistics of the sched_domain
3453 * @local_group: Does group contain the CPU for which we're performing
3454 * load balancing ?
3455 * @sgs: Variable containing the statistics of the group.
3456 */
3457static inline void update_sd_power_savings_stats(struct sched_group *group,
3458 struct sd_lb_stats *sds, int local_group, struct sg_lb_stats *sgs)
3459{
3460
3461 if (!sds->power_savings_balance)
3462 return;
3463
3464 /*
3465 * If the local group is idle or completely loaded
3466 * no need to do power savings balance at this domain
3467 */
3468 if (local_group && (sds->this_nr_running >= sgs->group_capacity ||
3469 !sds->this_nr_running))
3470 sds->power_savings_balance = 0;
3471
3472 /*
3473 * If a group is already running at full capacity or idle,
3474 * don't include that group in power savings calculations
3475 */
3476 if (!sds->power_savings_balance ||
3477 sgs->sum_nr_running >= sgs->group_capacity ||
3478 !sgs->sum_nr_running)
3479 return;
3480
3481 /*
3482 * Calculate the group which has the least non-idle load.
3483 * This is the group from where we need to pick up the load
3484 * for saving power
3485 */
3486 if ((sgs->sum_nr_running < sds->min_nr_running) ||
3487 (sgs->sum_nr_running == sds->min_nr_running &&
3488 group_first_cpu(group) > group_first_cpu(sds->group_min))) {
3489 sds->group_min = group;
3490 sds->min_nr_running = sgs->sum_nr_running;
3491 sds->min_load_per_task = sgs->sum_weighted_load /
3492 sgs->sum_nr_running;
3493 }
3494
3495 /*
3496 * Calculate the group which is almost near its
3497 * capacity but still has some space to pick up some load
3498 * from other group and save more power
3499 */
3500 if (sgs->sum_nr_running + 1 > sgs->group_capacity)
3501 return;
3502
3503 if (sgs->sum_nr_running > sds->leader_nr_running ||
3504 (sgs->sum_nr_running == sds->leader_nr_running &&
3505 group_first_cpu(group) < group_first_cpu(sds->group_leader))) {
3506 sds->group_leader = group;
3507 sds->leader_nr_running = sgs->sum_nr_running;
3508 }
3509}
3510
3511/**
3512 * check_power_save_busiest_group - see if there is potential for some power-savings balance
3513 * @sds: Variable containing the statistics of the sched_domain
3514 * under consideration.
3515 * @this_cpu: Cpu at which we're currently performing load-balancing.
3516 * @imbalance: Variable to store the imbalance.
3517 *
3518 * Description:
3519 * Check if we have potential to perform some power-savings balance.
3520 * If yes, set the busiest group to be the least loaded group in the
3521 * sched_domain, so that it's CPUs can be put to idle.
3522 *
3523 * Returns 1 if there is potential to perform power-savings balance.
3524 * Else returns 0.
3525 */
3526static inline int check_power_save_busiest_group(struct sd_lb_stats *sds,
3527 int this_cpu, unsigned long *imbalance)
3528{
3529 if (!sds->power_savings_balance)
3530 return 0;
3531
3532 if (sds->this != sds->group_leader ||
3533 sds->group_leader == sds->group_min)
3534 return 0;
3535
3536 *imbalance = sds->min_load_per_task;
3537 sds->busiest = sds->group_min;
3538
3539 return 1;
3540
3541}
3542#else /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
3543static inline void init_sd_power_savings_stats(struct sched_domain *sd,
3544 struct sd_lb_stats *sds, enum cpu_idle_type idle)
3545{
3546 return;
3547}
3548
3549static inline void update_sd_power_savings_stats(struct sched_group *group,
3550 struct sd_lb_stats *sds, int local_group, struct sg_lb_stats *sgs)
3551{
3552 return;
3553}
3554
3555static inline int check_power_save_busiest_group(struct sd_lb_stats *sds,
3556 int this_cpu, unsigned long *imbalance)
3557{
3558 return 0;
3559}
3560#endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
3561
3562
3563unsigned long default_scale_freq_power(struct sched_domain *sd, int cpu)
3564{
3565 return SCHED_LOAD_SCALE;
3566}
3567
3568unsigned long __weak arch_scale_freq_power(struct sched_domain *sd, int cpu)
3569{
3570 return default_scale_freq_power(sd, cpu);
3571}
3572
3573unsigned long default_scale_smt_power(struct sched_domain *sd, int cpu)
3574{
3575 unsigned long weight = cpumask_weight(sched_domain_span(sd));
3576 unsigned long smt_gain = sd->smt_gain;
3577
3578 smt_gain /= weight;
3579
3580 return smt_gain;
3581}
3582
3583unsigned long __weak arch_scale_smt_power(struct sched_domain *sd, int cpu)
3584{
3585 return default_scale_smt_power(sd, cpu);
3586}
3587
3588unsigned long scale_rt_power(int cpu)
3589{
3590 struct rq *rq = cpu_rq(cpu);
3591 u64 total, available;
3592
3593 sched_avg_update(rq);
3594
3595 total = sched_avg_period() + (rq->clock - rq->age_stamp);
3596 available = total - rq->rt_avg;
3597
3598 if (unlikely((s64)total < SCHED_LOAD_SCALE))
3599 total = SCHED_LOAD_SCALE;
3600
3601 total >>= SCHED_LOAD_SHIFT;
3602
3603 return div_u64(available, total);
3604}
3605
3606static void update_cpu_power(struct sched_domain *sd, int cpu)
3607{
3608 unsigned long weight = cpumask_weight(sched_domain_span(sd));
3609 unsigned long power = SCHED_LOAD_SCALE;
3610 struct sched_group *sdg = sd->groups;
3611
3612 if (sched_feat(ARCH_POWER))
3613 power *= arch_scale_freq_power(sd, cpu);
3614 else
3615 power *= default_scale_freq_power(sd, cpu);
3616
3617 power >>= SCHED_LOAD_SHIFT;
3618
3619 if ((sd->flags & SD_SHARE_CPUPOWER) && weight > 1) {
3620 if (sched_feat(ARCH_POWER))
3621 power *= arch_scale_smt_power(sd, cpu);
3622 else
3623 power *= default_scale_smt_power(sd, cpu);
3624
3625 power >>= SCHED_LOAD_SHIFT;
3626 }
3627
3628 power *= scale_rt_power(cpu);
3629 power >>= SCHED_LOAD_SHIFT;
3630
3631 if (!power)
3632 power = 1;
3633
3634 sdg->cpu_power = power;
3635}
3636
3637static void update_group_power(struct sched_domain *sd, int cpu)
3638{
3639 struct sched_domain *child = sd->child;
3640 struct sched_group *group, *sdg = sd->groups;
3641 unsigned long power;
3642
3643 if (!child) {
3644 update_cpu_power(sd, cpu);
3645 return;
3646 }
3647
3648 power = 0;
3649
3650 group = child->groups;
3651 do {
3652 power += group->cpu_power;
3653 group = group->next;
3654 } while (group != child->groups);
3655
3656 sdg->cpu_power = power;
3657}
3658
3659/**
3660 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
3661 * @group: sched_group whose statistics are to be updated.
3662 * @this_cpu: Cpu for which load balance is currently performed.
3663 * @idle: Idle status of this_cpu
3664 * @load_idx: Load index of sched_domain of this_cpu for load calc.
3665 * @sd_idle: Idle status of the sched_domain containing group.
3666 * @local_group: Does group contain this_cpu.
3667 * @cpus: Set of cpus considered for load balancing.
3668 * @balance: Should we balance.
3669 * @sgs: variable to hold the statistics for this group.
3670 */
3671static inline void update_sg_lb_stats(struct sched_domain *sd,
3672 struct sched_group *group, int this_cpu,
3673 enum cpu_idle_type idle, int load_idx, int *sd_idle,
3674 int local_group, const struct cpumask *cpus,
3675 int *balance, struct sg_lb_stats *sgs)
3676{
3677 unsigned long load, max_cpu_load, min_cpu_load;
3678 int i;
3679 unsigned int balance_cpu = -1, first_idle_cpu = 0;
3680 unsigned long sum_avg_load_per_task;
3681 unsigned long avg_load_per_task;
3682
3683 if (local_group) {
3684 balance_cpu = group_first_cpu(group);
3685 if (balance_cpu == this_cpu)
3686 update_group_power(sd, this_cpu);
3687 }
3688
3689 /* Tally up the load of all CPUs in the group */
3690 sum_avg_load_per_task = avg_load_per_task = 0;
3691 max_cpu_load = 0;
3692 min_cpu_load = ~0UL;
3693
3694 for_each_cpu_and(i, sched_group_cpus(group), cpus) {
3695 struct rq *rq = cpu_rq(i);
3696
3697 if (*sd_idle && rq->nr_running)
3698 *sd_idle = 0;
3699
3700 /* Bias balancing toward cpus of our domain */
3701 if (local_group) {
3702 if (idle_cpu(i) && !first_idle_cpu) {
3703 first_idle_cpu = 1;
3704 balance_cpu = i;
3705 }
3706
3707 load = target_load(i, load_idx);
3708 } else {
3709 load = source_load(i, load_idx);
3710 if (load > max_cpu_load)
3711 max_cpu_load = load;
3712 if (min_cpu_load > load)
3713 min_cpu_load = load;
3714 }
3715
3716 sgs->group_load += load;
3717 sgs->sum_nr_running += rq->nr_running;
3718 sgs->sum_weighted_load += weighted_cpuload(i);
3719
3720 sum_avg_load_per_task += cpu_avg_load_per_task(i);
3721 }
3722
3723 /*
3724 * First idle cpu or the first cpu(busiest) in this sched group
3725 * is eligible for doing load balancing at this and above
3726 * domains. In the newly idle case, we will allow all the cpu's
3727 * to do the newly idle load balance.
3728 */
3729 if (idle != CPU_NEWLY_IDLE && local_group &&
3730 balance_cpu != this_cpu && balance) {
3731 *balance = 0;
3732 return;
3733 }
3734
3735 /* Adjust by relative CPU power of the group */
3736 sgs->avg_load = (sgs->group_load * SCHED_LOAD_SCALE) / group->cpu_power;
3737
3738
3739 /*
3740 * Consider the group unbalanced when the imbalance is larger
3741 * than the average weight of two tasks.
3742 *
3743 * APZ: with cgroup the avg task weight can vary wildly and
3744 * might not be a suitable number - should we keep a
3745 * normalized nr_running number somewhere that negates
3746 * the hierarchy?
3747 */
3748 avg_load_per_task = (sum_avg_load_per_task * SCHED_LOAD_SCALE) /
3749 group->cpu_power;
3750
3751 if ((max_cpu_load - min_cpu_load) > 2*avg_load_per_task)
3752 sgs->group_imb = 1;
3753
3754 sgs->group_capacity =
3755 DIV_ROUND_CLOSEST(group->cpu_power, SCHED_LOAD_SCALE);
3756}
3757
3758/**
3759 * update_sd_lb_stats - Update sched_group's statistics for load balancing.
3760 * @sd: sched_domain whose statistics are to be updated.
3761 * @this_cpu: Cpu for which load balance is currently performed.
3762 * @idle: Idle status of this_cpu
3763 * @sd_idle: Idle status of the sched_domain containing group.
3764 * @cpus: Set of cpus considered for load balancing.
3765 * @balance: Should we balance.
3766 * @sds: variable to hold the statistics for this sched_domain.
3767 */
3768static inline void update_sd_lb_stats(struct sched_domain *sd, int this_cpu,
3769 enum cpu_idle_type idle, int *sd_idle,
3770 const struct cpumask *cpus, int *balance,
3771 struct sd_lb_stats *sds)
3772{
3773 struct sched_domain *child = sd->child;
3774 struct sched_group *group = sd->groups;
3775 struct sg_lb_stats sgs;
3776 int load_idx, prefer_sibling = 0;
3777
3778 if (child && child->flags & SD_PREFER_SIBLING)
3779 prefer_sibling = 1;
3780
3781 init_sd_power_savings_stats(sd, sds, idle);
3782 load_idx = get_sd_load_idx(sd, idle);
3783
3784 do {
3785 int local_group;
3786
3787 local_group = cpumask_test_cpu(this_cpu,
3788 sched_group_cpus(group));
3789 memset(&sgs, 0, sizeof(sgs));
3790 update_sg_lb_stats(sd, group, this_cpu, idle, load_idx, sd_idle,
3791 local_group, cpus, balance, &sgs);
3792
3793 if (local_group && balance && !(*balance))
3794 return;
3795
3796 sds->total_load += sgs.group_load;
3797 sds->total_pwr += group->cpu_power;
3798
3799 /*
3800 * In case the child domain prefers tasks go to siblings
3801 * first, lower the group capacity to one so that we'll try
3802 * and move all the excess tasks away.
3803 */
3804 if (prefer_sibling)
3805 sgs.group_capacity = min(sgs.group_capacity, 1UL);
3806
3807 if (local_group) {
3808 sds->this_load = sgs.avg_load;
3809 sds->this = group;
3810 sds->this_nr_running = sgs.sum_nr_running;
3811 sds->this_load_per_task = sgs.sum_weighted_load;
3812 } else if (sgs.avg_load > sds->max_load &&
3813 (sgs.sum_nr_running > sgs.group_capacity ||
3814 sgs.group_imb)) {
3815 sds->max_load = sgs.avg_load;
3816 sds->busiest = group;
3817 sds->busiest_nr_running = sgs.sum_nr_running;
3818 sds->busiest_load_per_task = sgs.sum_weighted_load;
3819 sds->group_imb = sgs.group_imb;
3820 }
3821
3822 update_sd_power_savings_stats(group, sds, local_group, &sgs);
3823 group = group->next;
3824 } while (group != sd->groups);
3825}
3826
3827/**
3828 * fix_small_imbalance - Calculate the minor imbalance that exists
3829 * amongst the groups of a sched_domain, during
3830 * load balancing.
3831 * @sds: Statistics of the sched_domain whose imbalance is to be calculated.
3832 * @this_cpu: The cpu at whose sched_domain we're performing load-balance.
3833 * @imbalance: Variable to store the imbalance.
3834 */
3835static inline void fix_small_imbalance(struct sd_lb_stats *sds,
3836 int this_cpu, unsigned long *imbalance)
3837{
3838 unsigned long tmp, pwr_now = 0, pwr_move = 0;
3839 unsigned int imbn = 2;
3840
3841 if (sds->this_nr_running) {
3842 sds->this_load_per_task /= sds->this_nr_running;
3843 if (sds->busiest_load_per_task >
3844 sds->this_load_per_task)
3845 imbn = 1;
3846 } else
3847 sds->this_load_per_task =
3848 cpu_avg_load_per_task(this_cpu);
3849
3850 if (sds->max_load - sds->this_load + sds->busiest_load_per_task >=
3851 sds->busiest_load_per_task * imbn) {
3852 *imbalance = sds->busiest_load_per_task;
3853 return;
3854 }
3855
3856 /*
3857 * OK, we don't have enough imbalance to justify moving tasks,
3858 * however we may be able to increase total CPU power used by
3859 * moving them.
3860 */
3861
3862 pwr_now += sds->busiest->cpu_power *
3863 min(sds->busiest_load_per_task, sds->max_load);
3864 pwr_now += sds->this->cpu_power *
3865 min(sds->this_load_per_task, sds->this_load);
3866 pwr_now /= SCHED_LOAD_SCALE;
3867
3868 /* Amount of load we'd subtract */
3869 tmp = (sds->busiest_load_per_task * SCHED_LOAD_SCALE) /
3870 sds->busiest->cpu_power;
3871 if (sds->max_load > tmp)
3872 pwr_move += sds->busiest->cpu_power *
3873 min(sds->busiest_load_per_task, sds->max_load - tmp);
3874
3875 /* Amount of load we'd add */
3876 if (sds->max_load * sds->busiest->cpu_power <
3877 sds->busiest_load_per_task * SCHED_LOAD_SCALE)
3878 tmp = (sds->max_load * sds->busiest->cpu_power) /
3879 sds->this->cpu_power;
3880 else
3881 tmp = (sds->busiest_load_per_task * SCHED_LOAD_SCALE) /
3882 sds->this->cpu_power;
3883 pwr_move += sds->this->cpu_power *
3884 min(sds->this_load_per_task, sds->this_load + tmp);
3885 pwr_move /= SCHED_LOAD_SCALE;
3886
3887 /* Move if we gain throughput */
3888 if (pwr_move > pwr_now)
3889 *imbalance = sds->busiest_load_per_task;
3890}
3891
3892/**
3893 * calculate_imbalance - Calculate the amount of imbalance present within the
3894 * groups of a given sched_domain during load balance.
3895 * @sds: statistics of the sched_domain whose imbalance is to be calculated.
3896 * @this_cpu: Cpu for which currently load balance is being performed.
3897 * @imbalance: The variable to store the imbalance.
3898 */
3899static inline void calculate_imbalance(struct sd_lb_stats *sds, int this_cpu,
3900 unsigned long *imbalance)
3901{
3902 unsigned long max_pull;
3903 /*
3904 * In the presence of smp nice balancing, certain scenarios can have
3905 * max load less than avg load(as we skip the groups at or below
3906 * its cpu_power, while calculating max_load..)
3907 */
3908 if (sds->max_load < sds->avg_load) {
3909 *imbalance = 0;
3910 return fix_small_imbalance(sds, this_cpu, imbalance);
3911 }
3912
3913 /* Don't want to pull so many tasks that a group would go idle */
3914 max_pull = min(sds->max_load - sds->avg_load,
3915 sds->max_load - sds->busiest_load_per_task);
3916
3917 /* How much load to actually move to equalise the imbalance */
3918 *imbalance = min(max_pull * sds->busiest->cpu_power,
3919 (sds->avg_load - sds->this_load) * sds->this->cpu_power)
3920 / SCHED_LOAD_SCALE;
3921
3922 /*
3923 * if *imbalance is less than the average load per runnable task
3924 * there is no gaurantee that any tasks will be moved so we'll have
3925 * a think about bumping its value to force at least one task to be
3926 * moved
3927 */
3928 if (*imbalance < sds->busiest_load_per_task)
3929 return fix_small_imbalance(sds, this_cpu, imbalance);
3930
3931}
3932/******* find_busiest_group() helpers end here *********************/
3933
3934/**
3935 * find_busiest_group - Returns the busiest group within the sched_domain
3936 * if there is an imbalance. If there isn't an imbalance, and
3937 * the user has opted for power-savings, it returns a group whose
3938 * CPUs can be put to idle by rebalancing those tasks elsewhere, if
3939 * such a group exists.
3940 *
3941 * Also calculates the amount of weighted load which should be moved
3942 * to restore balance.
3943 *
3944 * @sd: The sched_domain whose busiest group is to be returned.
3945 * @this_cpu: The cpu for which load balancing is currently being performed.
3946 * @imbalance: Variable which stores amount of weighted load which should
3947 * be moved to restore balance/put a group to idle.
3948 * @idle: The idle status of this_cpu.
3949 * @sd_idle: The idleness of sd
3950 * @cpus: The set of CPUs under consideration for load-balancing.
3951 * @balance: Pointer to a variable indicating if this_cpu
3952 * is the appropriate cpu to perform load balancing at this_level.
3953 *
3954 * Returns: - the busiest group if imbalance exists.
3955 * - If no imbalance and user has opted for power-savings balance,
3956 * return the least loaded group whose CPUs can be
3957 * put to idle by rebalancing its tasks onto our group.
3958 */
3959static struct sched_group *
3960find_busiest_group(struct sched_domain *sd, int this_cpu,
3961 unsigned long *imbalance, enum cpu_idle_type idle,
3962 int *sd_idle, const struct cpumask *cpus, int *balance)
3963{
3964 struct sd_lb_stats sds;
3965
3966 memset(&sds, 0, sizeof(sds));
3967
3968 /*
3969 * Compute the various statistics relavent for load balancing at
3970 * this level.
3971 */
3972 update_sd_lb_stats(sd, this_cpu, idle, sd_idle, cpus,
3973 balance, &sds);
3974
3975 /* Cases where imbalance does not exist from POV of this_cpu */
3976 /* 1) this_cpu is not the appropriate cpu to perform load balancing
3977 * at this level.
3978 * 2) There is no busy sibling group to pull from.
3979 * 3) This group is the busiest group.
3980 * 4) This group is more busy than the avg busieness at this
3981 * sched_domain.
3982 * 5) The imbalance is within the specified limit.
3983 * 6) Any rebalance would lead to ping-pong
3984 */
3985 if (balance && !(*balance))
3986 goto ret;
3987
3988 if (!sds.busiest || sds.busiest_nr_running == 0)
3989 goto out_balanced;
3990
3991 if (sds.this_load >= sds.max_load)
3992 goto out_balanced;
3993
3994 sds.avg_load = (SCHED_LOAD_SCALE * sds.total_load) / sds.total_pwr;
3995
3996 if (sds.this_load >= sds.avg_load)
3997 goto out_balanced;
3998
3999 if (100 * sds.max_load <= sd->imbalance_pct * sds.this_load)
4000 goto out_balanced;
4001
4002 sds.busiest_load_per_task /= sds.busiest_nr_running;
4003 if (sds.group_imb)
4004 sds.busiest_load_per_task =
4005 min(sds.busiest_load_per_task, sds.avg_load);
4006
4007 /*
4008 * We're trying to get all the cpus to the average_load, so we don't
4009 * want to push ourselves above the average load, nor do we wish to
4010 * reduce the max loaded cpu below the average load, as either of these
4011 * actions would just result in more rebalancing later, and ping-pong
4012 * tasks around. Thus we look for the minimum possible imbalance.
4013 * Negative imbalances (*we* are more loaded than anyone else) will
4014 * be counted as no imbalance for these purposes -- we can't fix that
4015 * by pulling tasks to us. Be careful of negative numbers as they'll
4016 * appear as very large values with unsigned longs.
4017 */
4018 if (sds.max_load <= sds.busiest_load_per_task)
4019 goto out_balanced;
4020
4021 /* Looks like there is an imbalance. Compute it */
4022 calculate_imbalance(&sds, this_cpu, imbalance);
4023 return sds.busiest;
4024
4025out_balanced:
4026 /*
4027 * There is no obvious imbalance. But check if we can do some balancing
4028 * to save power.
4029 */
4030 if (check_power_save_busiest_group(&sds, this_cpu, imbalance))
4031 return sds.busiest;
4032ret:
4033 *imbalance = 0;
4034 return NULL;
4035}
4036
4037/*
4038 * find_busiest_queue - find the busiest runqueue among the cpus in group.
4039 */
4040static struct rq *
4041find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
4042 unsigned long imbalance, const struct cpumask *cpus)
4043{
4044 struct rq *busiest = NULL, *rq;
4045 unsigned long max_load = 0;
4046 int i;
4047
4048 for_each_cpu(i, sched_group_cpus(group)) {
4049 unsigned long power = power_of(i);
4050 unsigned long capacity = DIV_ROUND_CLOSEST(power, SCHED_LOAD_SCALE);
4051 unsigned long wl;
4052
4053 if (!cpumask_test_cpu(i, cpus))
4054 continue;
4055
4056 rq = cpu_rq(i);
4057 wl = weighted_cpuload(i) * SCHED_LOAD_SCALE;
4058 wl /= power;
4059
4060 if (capacity && rq->nr_running == 1 && wl > imbalance)
4061 continue;
4062
4063 if (wl > max_load) {
4064 max_load = wl;
4065 busiest = rq;
4066 }
4067 }
4068
4069 return busiest;
4070}
4071
4072/*
4073 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
4074 * so long as it is large enough.
4075 */
4076#define MAX_PINNED_INTERVAL 512
4077
4078/* Working cpumask for load_balance and load_balance_newidle. */
4079static DEFINE_PER_CPU(cpumask_var_t, load_balance_tmpmask);
4080
4081/*
4082 * Check this_cpu to ensure it is balanced within domain. Attempt to move
4083 * tasks if there is an imbalance.
4084 */
4085static int load_balance(int this_cpu, struct rq *this_rq,
4086 struct sched_domain *sd, enum cpu_idle_type idle,
4087 int *balance)
4088{
4089 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
4090 struct sched_group *group;
4091 unsigned long imbalance;
4092 struct rq *busiest;
4093 unsigned long flags;
4094 struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask);
4095
4096 cpumask_setall(cpus);
4097
4098 /*
4099 * When power savings policy is enabled for the parent domain, idle
4100 * sibling can pick up load irrespective of busy siblings. In this case,
4101 * let the state of idle sibling percolate up as CPU_IDLE, instead of
4102 * portraying it as CPU_NOT_IDLE.
4103 */
4104 if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
4105 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
4106 sd_idle = 1;
4107
4108 schedstat_inc(sd, lb_count[idle]);
4109
4110redo:
4111 update_shares(sd);
4112 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
4113 cpus, balance);
4114
4115 if (*balance == 0)
4116 goto out_balanced;
4117
4118 if (!group) {
4119 schedstat_inc(sd, lb_nobusyg[idle]);
4120 goto out_balanced;
4121 }
4122
4123 busiest = find_busiest_queue(group, idle, imbalance, cpus);
4124 if (!busiest) {
4125 schedstat_inc(sd, lb_nobusyq[idle]);
4126 goto out_balanced;
4127 }
4128
4129 BUG_ON(busiest == this_rq);
4130
4131 schedstat_add(sd, lb_imbalance[idle], imbalance);
4132
4133 ld_moved = 0;
4134 if (busiest->nr_running > 1) {
4135 /*
4136 * Attempt to move tasks. If find_busiest_group has found
4137 * an imbalance but busiest->nr_running <= 1, the group is
4138 * still unbalanced. ld_moved simply stays zero, so it is
4139 * correctly treated as an imbalance.
4140 */
4141 local_irq_save(flags);
4142 double_rq_lock(this_rq, busiest);
4143 ld_moved = move_tasks(this_rq, this_cpu, busiest,
4144 imbalance, sd, idle, &all_pinned);
4145 double_rq_unlock(this_rq, busiest);
4146 local_irq_restore(flags);
4147
4148 /*
4149 * some other cpu did the load balance for us.
4150 */
4151 if (ld_moved && this_cpu != smp_processor_id())
4152 resched_cpu(this_cpu);
4153
4154 /* All tasks on this runqueue were pinned by CPU affinity */
4155 if (unlikely(all_pinned)) {
4156 cpumask_clear_cpu(cpu_of(busiest), cpus);
4157 if (!cpumask_empty(cpus))
4158 goto redo;
4159 goto out_balanced;
4160 }
4161 }
4162
4163 if (!ld_moved) {
4164 schedstat_inc(sd, lb_failed[idle]);
4165 sd->nr_balance_failed++;
4166
4167 if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
4168
4169 spin_lock_irqsave(&busiest->lock, flags);
4170
4171 /* don't kick the migration_thread, if the curr
4172 * task on busiest cpu can't be moved to this_cpu
4173 */
4174 if (!cpumask_test_cpu(this_cpu,
4175 &busiest->curr->cpus_allowed)) {
4176 spin_unlock_irqrestore(&busiest->lock, flags);
4177 all_pinned = 1;
4178 goto out_one_pinned;
4179 }
4180
4181 if (!busiest->active_balance) {
4182 busiest->active_balance = 1;
4183 busiest->push_cpu = this_cpu;
4184 active_balance = 1;
4185 }
4186 spin_unlock_irqrestore(&busiest->lock, flags);
4187 if (active_balance)
4188 wake_up_process(busiest->migration_thread);
4189
4190 /*
4191 * We've kicked active balancing, reset the failure
4192 * counter.
4193 */
4194 sd->nr_balance_failed = sd->cache_nice_tries+1;
4195 }
4196 } else
4197 sd->nr_balance_failed = 0;
4198
4199 if (likely(!active_balance)) {
4200 /* We were unbalanced, so reset the balancing interval */
4201 sd->balance_interval = sd->min_interval;
4202 } else {
4203 /*
4204 * If we've begun active balancing, start to back off. This
4205 * case may not be covered by the all_pinned logic if there
4206 * is only 1 task on the busy runqueue (because we don't call
4207 * move_tasks).
4208 */
4209 if (sd->balance_interval < sd->max_interval)
4210 sd->balance_interval *= 2;
4211 }
4212
4213 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
4214 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
4215 ld_moved = -1;
4216
4217 goto out;
4218
4219out_balanced:
4220 schedstat_inc(sd, lb_balanced[idle]);
4221
4222 sd->nr_balance_failed = 0;
4223
4224out_one_pinned:
4225 /* tune up the balancing interval */
4226 if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
4227 (sd->balance_interval < sd->max_interval))
4228 sd->balance_interval *= 2;
4229
4230 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
4231 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
4232 ld_moved = -1;
4233 else
4234 ld_moved = 0;
4235out:
4236 if (ld_moved)
4237 update_shares(sd);
4238 return ld_moved;
4239}
4240
4241/*
4242 * Check this_cpu to ensure it is balanced within domain. Attempt to move
4243 * tasks if there is an imbalance.
4244 *
4245 * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
4246 * this_rq is locked.
4247 */
4248static int
4249load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd)
4250{
4251 struct sched_group *group;
4252 struct rq *busiest = NULL;
4253 unsigned long imbalance;
4254 int ld_moved = 0;
4255 int sd_idle = 0;
4256 int all_pinned = 0;
4257 struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask);
4258
4259 cpumask_setall(cpus);
4260
4261 /*
4262 * When power savings policy is enabled for the parent domain, idle
4263 * sibling can pick up load irrespective of busy siblings. In this case,
4264 * let the state of idle sibling percolate up as IDLE, instead of
4265 * portraying it as CPU_NOT_IDLE.
4266 */
4267 if (sd->flags & SD_SHARE_CPUPOWER &&
4268 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
4269 sd_idle = 1;
4270
4271 schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
4272redo:
4273 update_shares_locked(this_rq, sd);
4274 group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
4275 &sd_idle, cpus, NULL);
4276 if (!group) {
4277 schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
4278 goto out_balanced;
4279 }
4280
4281 busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, cpus);
4282 if (!busiest) {
4283 schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
4284 goto out_balanced;
4285 }
4286
4287 BUG_ON(busiest == this_rq);
4288
4289 schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
4290
4291 ld_moved = 0;
4292 if (busiest->nr_running > 1) {
4293 /* Attempt to move tasks */
4294 double_lock_balance(this_rq, busiest);
4295 /* this_rq->clock is already updated */
4296 update_rq_clock(busiest);
4297 ld_moved = move_tasks(this_rq, this_cpu, busiest,
4298 imbalance, sd, CPU_NEWLY_IDLE,
4299 &all_pinned);
4300 double_unlock_balance(this_rq, busiest);
4301
4302 if (unlikely(all_pinned)) {
4303 cpumask_clear_cpu(cpu_of(busiest), cpus);
4304 if (!cpumask_empty(cpus))
4305 goto redo;
4306 }
4307 }
4308
4309 if (!ld_moved) {
4310 int active_balance = 0;
4311
4312 schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
4313 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
4314 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
4315 return -1;
4316
4317 if (sched_mc_power_savings < POWERSAVINGS_BALANCE_WAKEUP)
4318 return -1;
4319
4320 if (sd->nr_balance_failed++ < 2)
4321 return -1;
4322
4323 /*
4324 * The only task running in a non-idle cpu can be moved to this
4325 * cpu in an attempt to completely freeup the other CPU
4326 * package. The same method used to move task in load_balance()
4327 * have been extended for load_balance_newidle() to speedup
4328 * consolidation at sched_mc=POWERSAVINGS_BALANCE_WAKEUP (2)
4329 *
4330 * The package power saving logic comes from
4331 * find_busiest_group(). If there are no imbalance, then
4332 * f_b_g() will return NULL. However when sched_mc={1,2} then
4333 * f_b_g() will select a group from which a running task may be
4334 * pulled to this cpu in order to make the other package idle.
4335 * If there is no opportunity to make a package idle and if
4336 * there are no imbalance, then f_b_g() will return NULL and no
4337 * action will be taken in load_balance_newidle().
4338 *
4339 * Under normal task pull operation due to imbalance, there
4340 * will be more than one task in the source run queue and
4341 * move_tasks() will succeed. ld_moved will be true and this
4342 * active balance code will not be triggered.
4343 */
4344
4345 /* Lock busiest in correct order while this_rq is held */
4346 double_lock_balance(this_rq, busiest);
4347
4348 /*
4349 * don't kick the migration_thread, if the curr
4350 * task on busiest cpu can't be moved to this_cpu
4351 */
4352 if (!cpumask_test_cpu(this_cpu, &busiest->curr->cpus_allowed)) {
4353 double_unlock_balance(this_rq, busiest);
4354 all_pinned = 1;
4355 return ld_moved;
4356 }
4357
4358 if (!busiest->active_balance) {
4359 busiest->active_balance = 1;
4360 busiest->push_cpu = this_cpu;
4361 active_balance = 1;
4362 }
4363
4364 double_unlock_balance(this_rq, busiest);
4365 /*
4366 * Should not call ttwu while holding a rq->lock
4367 */
4368 spin_unlock(&this_rq->lock);
4369 if (active_balance)
4370 wake_up_process(busiest->migration_thread);
4371 spin_lock(&this_rq->lock);
4372
4373 } else
4374 sd->nr_balance_failed = 0;
4375
4376 update_shares_locked(this_rq, sd);
4377 return ld_moved;
4378
4379out_balanced:
4380 schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
4381 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
4382 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
4383 return -1;
4384 sd->nr_balance_failed = 0;
4385
4386 return 0;
4387}
4388
4389/*
4390 * idle_balance is called by schedule() if this_cpu is about to become
4391 * idle. Attempts to pull tasks from other CPUs.
4392 */
4393static void idle_balance(int this_cpu, struct rq *this_rq)
4394{
4395 struct sched_domain *sd;
4396 int pulled_task = 0;
4397 unsigned long next_balance = jiffies + HZ;
4398
4399 for_each_domain(this_cpu, sd) {
4400 unsigned long interval;
4401
4402 if (!(sd->flags & SD_LOAD_BALANCE))
4403 continue;
4404
4405 if (sd->flags & SD_BALANCE_NEWIDLE)
4406 /* If we've pulled tasks over stop searching: */
4407 pulled_task = load_balance_newidle(this_cpu, this_rq,
4408 sd);
4409
4410 interval = msecs_to_jiffies(sd->balance_interval);
4411 if (time_after(next_balance, sd->last_balance + interval))
4412 next_balance = sd->last_balance + interval;
4413 if (pulled_task)
4414 break;
4415 }
4416 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
4417 /*
4418 * We are going idle. next_balance may be set based on
4419 * a busy processor. So reset next_balance.
4420 */
4421 this_rq->next_balance = next_balance;
4422 }
4423}
4424
4425/*
4426 * active_load_balance is run by migration threads. It pushes running tasks
4427 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
4428 * running on each physical CPU where possible, and avoids physical /
4429 * logical imbalances.
4430 *
4431 * Called with busiest_rq locked.
4432 */
4433static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
4434{
4435 int target_cpu = busiest_rq->push_cpu;
4436 struct sched_domain *sd;
4437 struct rq *target_rq;
4438
4439 /* Is there any task to move? */
4440 if (busiest_rq->nr_running <= 1)
4441 return;
4442
4443 target_rq = cpu_rq(target_cpu);
4444
4445 /*
4446 * This condition is "impossible", if it occurs
4447 * we need to fix it. Originally reported by
4448 * Bjorn Helgaas on a 128-cpu setup.
4449 */
4450 BUG_ON(busiest_rq == target_rq);
4451
4452 /* move a task from busiest_rq to target_rq */
4453 double_lock_balance(busiest_rq, target_rq);
4454 update_rq_clock(busiest_rq);
4455 update_rq_clock(target_rq);
4456
4457 /* Search for an sd spanning us and the target CPU. */
4458 for_each_domain(target_cpu, sd) {
4459 if ((sd->flags & SD_LOAD_BALANCE) &&
4460 cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
4461 break;
4462 }
4463
4464 if (likely(sd)) {
4465 schedstat_inc(sd, alb_count);
4466
4467 if (move_one_task(target_rq, target_cpu, busiest_rq,
4468 sd, CPU_IDLE))
4469 schedstat_inc(sd, alb_pushed);
4470 else
4471 schedstat_inc(sd, alb_failed);
4472 }
4473 double_unlock_balance(busiest_rq, target_rq);
4474}
4475
4476#ifdef CONFIG_NO_HZ
4477static struct {
4478 atomic_t load_balancer;
4479 cpumask_var_t cpu_mask;
4480 cpumask_var_t ilb_grp_nohz_mask;
4481} nohz ____cacheline_aligned = {
4482 .load_balancer = ATOMIC_INIT(-1),
4483};
4484
4485int get_nohz_load_balancer(void)
4486{
4487 return atomic_read(&nohz.load_balancer);
4488}
4489
4490#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
4491/**
4492 * lowest_flag_domain - Return lowest sched_domain containing flag.
4493 * @cpu: The cpu whose lowest level of sched domain is to
4494 * be returned.
4495 * @flag: The flag to check for the lowest sched_domain
4496 * for the given cpu.
4497 *
4498 * Returns the lowest sched_domain of a cpu which contains the given flag.
4499 */
4500static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
4501{
4502 struct sched_domain *sd;
4503
4504 for_each_domain(cpu, sd)
4505 if (sd && (sd->flags & flag))
4506 break;
4507
4508 return sd;
4509}
4510
4511/**
4512 * for_each_flag_domain - Iterates over sched_domains containing the flag.
4513 * @cpu: The cpu whose domains we're iterating over.
4514 * @sd: variable holding the value of the power_savings_sd
4515 * for cpu.
4516 * @flag: The flag to filter the sched_domains to be iterated.
4517 *
4518 * Iterates over all the scheduler domains for a given cpu that has the 'flag'
4519 * set, starting from the lowest sched_domain to the highest.
4520 */
4521#define for_each_flag_domain(cpu, sd, flag) \
4522 for (sd = lowest_flag_domain(cpu, flag); \
4523 (sd && (sd->flags & flag)); sd = sd->parent)
4524
4525/**
4526 * is_semi_idle_group - Checks if the given sched_group is semi-idle.
4527 * @ilb_group: group to be checked for semi-idleness
4528 *
4529 * Returns: 1 if the group is semi-idle. 0 otherwise.
4530 *
4531 * We define a sched_group to be semi idle if it has atleast one idle-CPU
4532 * and atleast one non-idle CPU. This helper function checks if the given
4533 * sched_group is semi-idle or not.
4534 */
4535static inline int is_semi_idle_group(struct sched_group *ilb_group)
4536{
4537 cpumask_and(nohz.ilb_grp_nohz_mask, nohz.cpu_mask,
4538 sched_group_cpus(ilb_group));
4539
4540 /*
4541 * A sched_group is semi-idle when it has atleast one busy cpu
4542 * and atleast one idle cpu.
4543 */
4544 if (cpumask_empty(nohz.ilb_grp_nohz_mask))
4545 return 0;
4546
4547 if (cpumask_equal(nohz.ilb_grp_nohz_mask, sched_group_cpus(ilb_group)))
4548 return 0;
4549
4550 return 1;
4551}
4552/**
4553 * find_new_ilb - Finds the optimum idle load balancer for nomination.
4554 * @cpu: The cpu which is nominating a new idle_load_balancer.
4555 *
4556 * Returns: Returns the id of the idle load balancer if it exists,
4557 * Else, returns >= nr_cpu_ids.
4558 *
4559 * This algorithm picks the idle load balancer such that it belongs to a
4560 * semi-idle powersavings sched_domain. The idea is to try and avoid
4561 * completely idle packages/cores just for the purpose of idle load balancing
4562 * when there are other idle cpu's which are better suited for that job.
4563 */
4564static int find_new_ilb(int cpu)
4565{
4566 struct sched_domain *sd;
4567 struct sched_group *ilb_group;
4568
4569 /*
4570 * Have idle load balancer selection from semi-idle packages only
4571 * when power-aware load balancing is enabled
4572 */
4573 if (!(sched_smt_power_savings || sched_mc_power_savings))
4574 goto out_done;
4575
4576 /*
4577 * Optimize for the case when we have no idle CPUs or only one
4578 * idle CPU. Don't walk the sched_domain hierarchy in such cases
4579 */
4580 if (cpumask_weight(nohz.cpu_mask) < 2)
4581 goto out_done;
4582
4583 for_each_flag_domain(cpu, sd, SD_POWERSAVINGS_BALANCE) {
4584 ilb_group = sd->groups;
4585
4586 do {
4587 if (is_semi_idle_group(ilb_group))
4588 return cpumask_first(nohz.ilb_grp_nohz_mask);
4589
4590 ilb_group = ilb_group->next;
4591
4592 } while (ilb_group != sd->groups);
4593 }
4594
4595out_done:
4596 return cpumask_first(nohz.cpu_mask);
4597}
4598#else /* (CONFIG_SCHED_MC || CONFIG_SCHED_SMT) */
4599static inline int find_new_ilb(int call_cpu)
4600{
4601 return cpumask_first(nohz.cpu_mask);
4602}
4603#endif
4604
4605/*
4606 * This routine will try to nominate the ilb (idle load balancing)
4607 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
4608 * load balancing on behalf of all those cpus. If all the cpus in the system
4609 * go into this tickless mode, then there will be no ilb owner (as there is
4610 * no need for one) and all the cpus will sleep till the next wakeup event
4611 * arrives...
4612 *
4613 * For the ilb owner, tick is not stopped. And this tick will be used
4614 * for idle load balancing. ilb owner will still be part of
4615 * nohz.cpu_mask..
4616 *
4617 * While stopping the tick, this cpu will become the ilb owner if there
4618 * is no other owner. And will be the owner till that cpu becomes busy
4619 * or if all cpus in the system stop their ticks at which point
4620 * there is no need for ilb owner.
4621 *
4622 * When the ilb owner becomes busy, it nominates another owner, during the
4623 * next busy scheduler_tick()
4624 */
4625int select_nohz_load_balancer(int stop_tick)
4626{
4627 int cpu = smp_processor_id();
4628
4629 if (stop_tick) {
4630 cpu_rq(cpu)->in_nohz_recently = 1;
4631
4632 if (!cpu_active(cpu)) {
4633 if (atomic_read(&nohz.load_balancer) != cpu)
4634 return 0;
4635
4636 /*
4637 * If we are going offline and still the leader,
4638 * give up!
4639 */
4640 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
4641 BUG();
4642
4643 return 0;
4644 }
4645
4646 cpumask_set_cpu(cpu, nohz.cpu_mask);
4647
4648 /* time for ilb owner also to sleep */
4649 if (cpumask_weight(nohz.cpu_mask) == num_online_cpus()) {
4650 if (atomic_read(&nohz.load_balancer) == cpu)
4651 atomic_set(&nohz.load_balancer, -1);
4652 return 0;
4653 }
4654
4655 if (atomic_read(&nohz.load_balancer) == -1) {
4656 /* make me the ilb owner */
4657 if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
4658 return 1;
4659 } else if (atomic_read(&nohz.load_balancer) == cpu) {
4660 int new_ilb;
4661
4662 if (!(sched_smt_power_savings ||
4663 sched_mc_power_savings))
4664 return 1;
4665 /*
4666 * Check to see if there is a more power-efficient
4667 * ilb.
4668 */
4669 new_ilb = find_new_ilb(cpu);
4670 if (new_ilb < nr_cpu_ids && new_ilb != cpu) {
4671 atomic_set(&nohz.load_balancer, -1);
4672 resched_cpu(new_ilb);
4673 return 0;
4674 }
4675 return 1;
4676 }
4677 } else {
4678 if (!cpumask_test_cpu(cpu, nohz.cpu_mask))
4679 return 0;
4680
4681 cpumask_clear_cpu(cpu, nohz.cpu_mask);
4682
4683 if (atomic_read(&nohz.load_balancer) == cpu)
4684 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
4685 BUG();
4686 }
4687 return 0;
4688}
4689#endif
4690
4691static DEFINE_SPINLOCK(balancing);
4692
4693/*
4694 * It checks each scheduling domain to see if it is due to be balanced,
4695 * and initiates a balancing operation if so.
4696 *
4697 * Balancing parameters are set up in arch_init_sched_domains.
4698 */
4699static void rebalance_domains(int cpu, enum cpu_idle_type idle)
4700{
4701 int balance = 1;
4702 struct rq *rq = cpu_rq(cpu);
4703 unsigned long interval;
4704 struct sched_domain *sd;
4705 /* Earliest time when we have to do rebalance again */
4706 unsigned long next_balance = jiffies + 60*HZ;
4707 int update_next_balance = 0;
4708 int need_serialize;
4709
4710 for_each_domain(cpu, sd) {
4711 if (!(sd->flags & SD_LOAD_BALANCE))
4712 continue;
4713
4714 interval = sd->balance_interval;
4715 if (idle != CPU_IDLE)
4716 interval *= sd->busy_factor;
4717
4718 /* scale ms to jiffies */
4719 interval = msecs_to_jiffies(interval);
4720 if (unlikely(!interval))
4721 interval = 1;
4722 if (interval > HZ*NR_CPUS/10)
4723 interval = HZ*NR_CPUS/10;
4724
4725 need_serialize = sd->flags & SD_SERIALIZE;
4726
4727 if (need_serialize) {
4728 if (!spin_trylock(&balancing))
4729 goto out;
4730 }
4731
4732 if (time_after_eq(jiffies, sd->last_balance + interval)) {
4733 if (load_balance(cpu, rq, sd, idle, &balance)) {
4734 /*
4735 * We've pulled tasks over so either we're no
4736 * longer idle, or one of our SMT siblings is
4737 * not idle.
4738 */
4739 idle = CPU_NOT_IDLE;
4740 }
4741 sd->last_balance = jiffies;
4742 }
4743 if (need_serialize)
4744 spin_unlock(&balancing);
4745out:
4746 if (time_after(next_balance, sd->last_balance + interval)) {
4747 next_balance = sd->last_balance + interval;
4748 update_next_balance = 1;
4749 }
4750
4751 /*
4752 * Stop the load balance at this level. There is another
4753 * CPU in our sched group which is doing load balancing more
4754 * actively.
4755 */
4756 if (!balance)
4757 break;
4758 }
4759
4760 /*
4761 * next_balance will be updated only when there is a need.
4762 * When the cpu is attached to null domain for ex, it will not be
4763 * updated.
4764 */
4765 if (likely(update_next_balance))
4766 rq->next_balance = next_balance;
4767}
4768
4769/*
4770 * run_rebalance_domains is triggered when needed from the scheduler tick.
4771 * In CONFIG_NO_HZ case, the idle load balance owner will do the
4772 * rebalancing for all the cpus for whom scheduler ticks are stopped.
4773 */
4774static void run_rebalance_domains(struct softirq_action *h)
4775{
4776 int this_cpu = smp_processor_id();
4777 struct rq *this_rq = cpu_rq(this_cpu);
4778 enum cpu_idle_type idle = this_rq->idle_at_tick ?
4779 CPU_IDLE : CPU_NOT_IDLE;
4780
4781 rebalance_domains(this_cpu, idle);
4782
4783#ifdef CONFIG_NO_HZ
4784 /*
4785 * If this cpu is the owner for idle load balancing, then do the
4786 * balancing on behalf of the other idle cpus whose ticks are
4787 * stopped.
4788 */
4789 if (this_rq->idle_at_tick &&
4790 atomic_read(&nohz.load_balancer) == this_cpu) {
4791 struct rq *rq;
4792 int balance_cpu;
4793
4794 for_each_cpu(balance_cpu, nohz.cpu_mask) {
4795 if (balance_cpu == this_cpu)
4796 continue;
4797
4798 /*
4799 * If this cpu gets work to do, stop the load balancing
4800 * work being done for other cpus. Next load
4801 * balancing owner will pick it up.
4802 */
4803 if (need_resched())
4804 break;
4805
4806 rebalance_domains(balance_cpu, CPU_IDLE);
4807
4808 rq = cpu_rq(balance_cpu);
4809 if (time_after(this_rq->next_balance, rq->next_balance))
4810 this_rq->next_balance = rq->next_balance;
4811 }
4812 }
4813#endif
4814}
4815
4816static inline int on_null_domain(int cpu)
4817{
4818 return !rcu_dereference(cpu_rq(cpu)->sd);
4819}
4820
4821/*
4822 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
4823 *
4824 * In case of CONFIG_NO_HZ, this is the place where we nominate a new
4825 * idle load balancing owner or decide to stop the periodic load balancing,
4826 * if the whole system is idle.
4827 */
4828static inline void trigger_load_balance(struct rq *rq, int cpu)
4829{
4830#ifdef CONFIG_NO_HZ
4831 /*
4832 * If we were in the nohz mode recently and busy at the current
4833 * scheduler tick, then check if we need to nominate new idle
4834 * load balancer.
4835 */
4836 if (rq->in_nohz_recently && !rq->idle_at_tick) {
4837 rq->in_nohz_recently = 0;
4838
4839 if (atomic_read(&nohz.load_balancer) == cpu) {
4840 cpumask_clear_cpu(cpu, nohz.cpu_mask);
4841 atomic_set(&nohz.load_balancer, -1);
4842 }
4843
4844 if (atomic_read(&nohz.load_balancer) == -1) {
4845 int ilb = find_new_ilb(cpu);
4846
4847 if (ilb < nr_cpu_ids)
4848 resched_cpu(ilb);
4849 }
4850 }
4851
4852 /*
4853 * If this cpu is idle and doing idle load balancing for all the
4854 * cpus with ticks stopped, is it time for that to stop?
4855 */
4856 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
4857 cpumask_weight(nohz.cpu_mask) == num_online_cpus()) {
4858 resched_cpu(cpu);
4859 return;
4860 }
4861
4862 /*
4863 * If this cpu is idle and the idle load balancing is done by
4864 * someone else, then no need raise the SCHED_SOFTIRQ
4865 */
4866 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
4867 cpumask_test_cpu(cpu, nohz.cpu_mask))
4868 return;
4869#endif
4870 /* Don't need to rebalance while attached to NULL domain */
4871 if (time_after_eq(jiffies, rq->next_balance) &&
4872 likely(!on_null_domain(cpu)))
4873 raise_softirq(SCHED_SOFTIRQ);
4874}
4875
4876#else /* CONFIG_SMP */
4877
4878/*
4879 * on UP we do not need to balance between CPUs:
4880 */
4881static inline void idle_balance(int cpu, struct rq *rq)
4882{
4883}
4884
4885#endif 3164#endif
4886 3165
4887DEFINE_PER_CPU(struct kernel_stat, kstat); 3166DEFINE_PER_CPU(struct kernel_stat, kstat);
@@ -5013,8 +3292,13 @@ static void account_guest_time(struct task_struct *p, cputime_t cputime,
5013 p->gtime = cputime_add(p->gtime, cputime); 3292 p->gtime = cputime_add(p->gtime, cputime);
5014 3293
5015 /* Add guest time to cpustat. */ 3294 /* Add guest time to cpustat. */
5016 cpustat->user = cputime64_add(cpustat->user, tmp); 3295 if (TASK_NICE(p) > 0) {
5017 cpustat->guest = cputime64_add(cpustat->guest, tmp); 3296 cpustat->nice = cputime64_add(cpustat->nice, tmp);
3297 cpustat->guest_nice = cputime64_add(cpustat->guest_nice, tmp);
3298 } else {
3299 cpustat->user = cputime64_add(cpustat->user, tmp);
3300 cpustat->guest = cputime64_add(cpustat->guest, tmp);
3301 }
5018} 3302}
5019 3303
5020/* 3304/*
@@ -5129,60 +3413,86 @@ void account_idle_ticks(unsigned long ticks)
5129 * Use precise platform statistics if available: 3413 * Use precise platform statistics if available:
5130 */ 3414 */
5131#ifdef CONFIG_VIRT_CPU_ACCOUNTING 3415#ifdef CONFIG_VIRT_CPU_ACCOUNTING
5132cputime_t task_utime(struct task_struct *p) 3416void task_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
5133{ 3417{
5134 return p->utime; 3418 *ut = p->utime;
3419 *st = p->stime;
5135} 3420}
5136 3421
5137cputime_t task_stime(struct task_struct *p) 3422void thread_group_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
5138{ 3423{
5139 return p->stime; 3424 struct task_cputime cputime;
3425
3426 thread_group_cputime(p, &cputime);
3427
3428 *ut = cputime.utime;
3429 *st = cputime.stime;
5140} 3430}
5141#else 3431#else
5142cputime_t task_utime(struct task_struct *p) 3432
3433#ifndef nsecs_to_cputime
3434# define nsecs_to_cputime(__nsecs) nsecs_to_jiffies(__nsecs)
3435#endif
3436
3437void task_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
5143{ 3438{
5144 clock_t utime = cputime_to_clock_t(p->utime), 3439 cputime_t rtime, utime = p->utime, total = cputime_add(utime, p->stime);
5145 total = utime + cputime_to_clock_t(p->stime);
5146 u64 temp;
5147 3440
5148 /* 3441 /*
5149 * Use CFS's precise accounting: 3442 * Use CFS's precise accounting:
5150 */ 3443 */
5151 temp = (u64)nsec_to_clock_t(p->se.sum_exec_runtime); 3444 rtime = nsecs_to_cputime(p->se.sum_exec_runtime);
5152 3445
5153 if (total) { 3446 if (total) {
5154 temp *= utime; 3447 u64 temp;
3448
3449 temp = (u64)(rtime * utime);
5155 do_div(temp, total); 3450 do_div(temp, total);
5156 } 3451 utime = (cputime_t)temp;
5157 utime = (clock_t)temp; 3452 } else
3453 utime = rtime;
5158 3454
5159 p->prev_utime = max(p->prev_utime, clock_t_to_cputime(utime)); 3455 /*
5160 return p->prev_utime; 3456 * Compare with previous values, to keep monotonicity:
3457 */
3458 p->prev_utime = max(p->prev_utime, utime);
3459 p->prev_stime = max(p->prev_stime, cputime_sub(rtime, p->prev_utime));
3460
3461 *ut = p->prev_utime;
3462 *st = p->prev_stime;
5161} 3463}
5162 3464
5163cputime_t task_stime(struct task_struct *p) 3465/*
3466 * Must be called with siglock held.
3467 */
3468void thread_group_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
5164{ 3469{
5165 clock_t stime; 3470 struct signal_struct *sig = p->signal;
3471 struct task_cputime cputime;
3472 cputime_t rtime, utime, total;
5166 3473
5167 /* 3474 thread_group_cputime(p, &cputime);
5168 * Use CFS's precise accounting. (we subtract utime from
5169 * the total, to make sure the total observed by userspace
5170 * grows monotonically - apps rely on that):
5171 */
5172 stime = nsec_to_clock_t(p->se.sum_exec_runtime) -
5173 cputime_to_clock_t(task_utime(p));
5174 3475
5175 if (stime >= 0) 3476 total = cputime_add(cputime.utime, cputime.stime);
5176 p->prev_stime = max(p->prev_stime, clock_t_to_cputime(stime)); 3477 rtime = nsecs_to_cputime(cputime.sum_exec_runtime);
5177 3478
5178 return p->prev_stime; 3479 if (total) {
5179} 3480 u64 temp;
5180#endif
5181 3481
5182inline cputime_t task_gtime(struct task_struct *p) 3482 temp = (u64)(rtime * cputime.utime);
5183{ 3483 do_div(temp, total);
5184 return p->gtime; 3484 utime = (cputime_t)temp;
3485 } else
3486 utime = rtime;
3487
3488 sig->prev_utime = max(sig->prev_utime, utime);
3489 sig->prev_stime = max(sig->prev_stime,
3490 cputime_sub(rtime, sig->prev_utime));
3491
3492 *ut = sig->prev_utime;
3493 *st = sig->prev_stime;
5185} 3494}
3495#endif
5186 3496
5187/* 3497/*
5188 * This function gets called by the timer code, with HZ frequency. 3498 * This function gets called by the timer code, with HZ frequency.
@@ -5199,13 +3509,13 @@ void scheduler_tick(void)
5199 3509
5200 sched_clock_tick(); 3510 sched_clock_tick();
5201 3511
5202 spin_lock(&rq->lock); 3512 raw_spin_lock(&rq->lock);
5203 update_rq_clock(rq); 3513 update_rq_clock(rq);
5204 update_cpu_load(rq); 3514 update_cpu_load(rq);
5205 curr->sched_class->task_tick(rq, curr, 0); 3515 curr->sched_class->task_tick(rq, curr, 0);
5206 spin_unlock(&rq->lock); 3516 raw_spin_unlock(&rq->lock);
5207 3517
5208 perf_event_task_tick(curr, cpu); 3518 perf_event_task_tick(curr);
5209 3519
5210#ifdef CONFIG_SMP 3520#ifdef CONFIG_SMP
5211 rq->idle_at_tick = idle_cpu(cpu); 3521 rq->idle_at_tick = idle_cpu(cpu);
@@ -5317,13 +3627,14 @@ static inline void schedule_debug(struct task_struct *prev)
5317#endif 3627#endif
5318} 3628}
5319 3629
5320static void put_prev_task(struct rq *rq, struct task_struct *p) 3630static void put_prev_task(struct rq *rq, struct task_struct *prev)
5321{ 3631{
5322 u64 runtime = p->se.sum_exec_runtime - p->se.prev_sum_exec_runtime; 3632 if (prev->state == TASK_RUNNING) {
3633 u64 runtime = prev->se.sum_exec_runtime;
5323 3634
5324 update_avg(&p->se.avg_running, runtime); 3635 runtime -= prev->se.prev_sum_exec_runtime;
3636 runtime = min_t(u64, runtime, 2*sysctl_sched_migration_cost);
5325 3637
5326 if (p->state == TASK_RUNNING) {
5327 /* 3638 /*
5328 * In order to avoid avg_overlap growing stale when we are 3639 * In order to avoid avg_overlap growing stale when we are
5329 * indeed overlapping and hence not getting put to sleep, grow 3640 * indeed overlapping and hence not getting put to sleep, grow
@@ -5333,12 +3644,9 @@ static void put_prev_task(struct rq *rq, struct task_struct *p)
5333 * correlates to the amount of cache footprint a task can 3644 * correlates to the amount of cache footprint a task can
5334 * build up. 3645 * build up.
5335 */ 3646 */
5336 runtime = min_t(u64, runtime, 2*sysctl_sched_migration_cost); 3647 update_avg(&prev->se.avg_overlap, runtime);
5337 update_avg(&p->se.avg_overlap, runtime);
5338 } else {
5339 update_avg(&p->se.avg_running, 0);
5340 } 3648 }
5341 p->sched_class->put_prev_task(rq, p); 3649 prev->sched_class->put_prev_task(rq, prev);
5342} 3650}
5343 3651
5344/* 3652/*
@@ -5399,7 +3707,7 @@ need_resched_nonpreemptible:
5399 if (sched_feat(HRTICK)) 3707 if (sched_feat(HRTICK))
5400 hrtick_clear(rq); 3708 hrtick_clear(rq);
5401 3709
5402 spin_lock_irq(&rq->lock); 3710 raw_spin_lock_irq(&rq->lock);
5403 update_rq_clock(rq); 3711 update_rq_clock(rq);
5404 clear_tsk_need_resched(prev); 3712 clear_tsk_need_resched(prev);
5405 3713
@@ -5421,7 +3729,7 @@ need_resched_nonpreemptible:
5421 3729
5422 if (likely(prev != next)) { 3730 if (likely(prev != next)) {
5423 sched_info_switch(prev, next); 3731 sched_info_switch(prev, next);
5424 perf_event_task_sched_out(prev, next, cpu); 3732 perf_event_task_sched_out(prev, next);
5425 3733
5426 rq->nr_switches++; 3734 rq->nr_switches++;
5427 rq->curr = next; 3735 rq->curr = next;
@@ -5435,12 +3743,15 @@ need_resched_nonpreemptible:
5435 cpu = smp_processor_id(); 3743 cpu = smp_processor_id();
5436 rq = cpu_rq(cpu); 3744 rq = cpu_rq(cpu);
5437 } else 3745 } else
5438 spin_unlock_irq(&rq->lock); 3746 raw_spin_unlock_irq(&rq->lock);
5439 3747
5440 post_schedule(rq); 3748 post_schedule(rq);
5441 3749
5442 if (unlikely(reacquire_kernel_lock(current) < 0)) 3750 if (unlikely(reacquire_kernel_lock(current) < 0)) {
3751 prev = rq->curr;
3752 switch_count = &prev->nivcsw;
5443 goto need_resched_nonpreemptible; 3753 goto need_resched_nonpreemptible;
3754 }
5444 3755
5445 preempt_enable_no_resched(); 3756 preempt_enable_no_resched();
5446 if (need_resched()) 3757 if (need_resched())
@@ -5448,7 +3759,7 @@ need_resched_nonpreemptible:
5448} 3759}
5449EXPORT_SYMBOL(schedule); 3760EXPORT_SYMBOL(schedule);
5450 3761
5451#ifdef CONFIG_SMP 3762#ifdef CONFIG_MUTEX_SPIN_ON_OWNER
5452/* 3763/*
5453 * Look out! "owner" is an entirely speculative pointer 3764 * Look out! "owner" is an entirely speculative pointer
5454 * access and not reliable. 3765 * access and not reliable.
@@ -5852,14 +4163,15 @@ EXPORT_SYMBOL(wait_for_completion_killable);
5852 */ 4163 */
5853bool try_wait_for_completion(struct completion *x) 4164bool try_wait_for_completion(struct completion *x)
5854{ 4165{
4166 unsigned long flags;
5855 int ret = 1; 4167 int ret = 1;
5856 4168
5857 spin_lock_irq(&x->wait.lock); 4169 spin_lock_irqsave(&x->wait.lock, flags);
5858 if (!x->done) 4170 if (!x->done)
5859 ret = 0; 4171 ret = 0;
5860 else 4172 else
5861 x->done--; 4173 x->done--;
5862 spin_unlock_irq(&x->wait.lock); 4174 spin_unlock_irqrestore(&x->wait.lock, flags);
5863 return ret; 4175 return ret;
5864} 4176}
5865EXPORT_SYMBOL(try_wait_for_completion); 4177EXPORT_SYMBOL(try_wait_for_completion);
@@ -5874,12 +4186,13 @@ EXPORT_SYMBOL(try_wait_for_completion);
5874 */ 4186 */
5875bool completion_done(struct completion *x) 4187bool completion_done(struct completion *x)
5876{ 4188{
4189 unsigned long flags;
5877 int ret = 1; 4190 int ret = 1;
5878 4191
5879 spin_lock_irq(&x->wait.lock); 4192 spin_lock_irqsave(&x->wait.lock, flags);
5880 if (!x->done) 4193 if (!x->done)
5881 ret = 0; 4194 ret = 0;
5882 spin_unlock_irq(&x->wait.lock); 4195 spin_unlock_irqrestore(&x->wait.lock, flags);
5883 return ret; 4196 return ret;
5884} 4197}
5885EXPORT_SYMBOL(completion_done); 4198EXPORT_SYMBOL(completion_done);
@@ -5947,7 +4260,7 @@ void rt_mutex_setprio(struct task_struct *p, int prio)
5947 unsigned long flags; 4260 unsigned long flags;
5948 int oldprio, on_rq, running; 4261 int oldprio, on_rq, running;
5949 struct rq *rq; 4262 struct rq *rq;
5950 const struct sched_class *prev_class = p->sched_class; 4263 const struct sched_class *prev_class;
5951 4264
5952 BUG_ON(prio < 0 || prio > MAX_PRIO); 4265 BUG_ON(prio < 0 || prio > MAX_PRIO);
5953 4266
@@ -5955,6 +4268,7 @@ void rt_mutex_setprio(struct task_struct *p, int prio)
5955 update_rq_clock(rq); 4268 update_rq_clock(rq);
5956 4269
5957 oldprio = p->prio; 4270 oldprio = p->prio;
4271 prev_class = p->sched_class;
5958 on_rq = p->se.on_rq; 4272 on_rq = p->se.on_rq;
5959 running = task_current(rq, p); 4273 running = task_current(rq, p);
5960 if (on_rq) 4274 if (on_rq)
@@ -5972,7 +4286,7 @@ void rt_mutex_setprio(struct task_struct *p, int prio)
5972 if (running) 4286 if (running)
5973 p->sched_class->set_curr_task(rq); 4287 p->sched_class->set_curr_task(rq);
5974 if (on_rq) { 4288 if (on_rq) {
5975 enqueue_task(rq, p, 0); 4289 enqueue_task(rq, p, 0, oldprio < prio);
5976 4290
5977 check_class_changed(rq, p, prev_class, oldprio, running); 4291 check_class_changed(rq, p, prev_class, oldprio, running);
5978 } 4292 }
@@ -6016,7 +4330,7 @@ void set_user_nice(struct task_struct *p, long nice)
6016 delta = p->prio - old_prio; 4330 delta = p->prio - old_prio;
6017 4331
6018 if (on_rq) { 4332 if (on_rq) {
6019 enqueue_task(rq, p, 0); 4333 enqueue_task(rq, p, 0, false);
6020 /* 4334 /*
6021 * If the task increased its priority or is running and 4335 * If the task increased its priority or is running and
6022 * lowered its priority, then reschedule its CPU: 4336 * lowered its priority, then reschedule its CPU:
@@ -6039,7 +4353,7 @@ int can_nice(const struct task_struct *p, const int nice)
6039 /* convert nice value [19,-20] to rlimit style value [1,40] */ 4353 /* convert nice value [19,-20] to rlimit style value [1,40] */
6040 int nice_rlim = 20 - nice; 4354 int nice_rlim = 20 - nice;
6041 4355
6042 return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur || 4356 return (nice_rlim <= task_rlimit(p, RLIMIT_NICE) ||
6043 capable(CAP_SYS_NICE)); 4357 capable(CAP_SYS_NICE));
6044} 4358}
6045 4359
@@ -6142,22 +4456,14 @@ __setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
6142 BUG_ON(p->se.on_rq); 4456 BUG_ON(p->se.on_rq);
6143 4457
6144 p->policy = policy; 4458 p->policy = policy;
6145 switch (p->policy) {
6146 case SCHED_NORMAL:
6147 case SCHED_BATCH:
6148 case SCHED_IDLE:
6149 p->sched_class = &fair_sched_class;
6150 break;
6151 case SCHED_FIFO:
6152 case SCHED_RR:
6153 p->sched_class = &rt_sched_class;
6154 break;
6155 }
6156
6157 p->rt_priority = prio; 4459 p->rt_priority = prio;
6158 p->normal_prio = normal_prio(p); 4460 p->normal_prio = normal_prio(p);
6159 /* we are holding p->pi_lock already */ 4461 /* we are holding p->pi_lock already */
6160 p->prio = rt_mutex_getprio(p); 4462 p->prio = rt_mutex_getprio(p);
4463 if (rt_prio(p->prio))
4464 p->sched_class = &rt_sched_class;
4465 else
4466 p->sched_class = &fair_sched_class;
6161 set_load_weight(p); 4467 set_load_weight(p);
6162} 4468}
6163 4469
@@ -6182,7 +4488,7 @@ static int __sched_setscheduler(struct task_struct *p, int policy,
6182{ 4488{
6183 int retval, oldprio, oldpolicy = -1, on_rq, running; 4489 int retval, oldprio, oldpolicy = -1, on_rq, running;
6184 unsigned long flags; 4490 unsigned long flags;
6185 const struct sched_class *prev_class = p->sched_class; 4491 const struct sched_class *prev_class;
6186 struct rq *rq; 4492 struct rq *rq;
6187 int reset_on_fork; 4493 int reset_on_fork;
6188 4494
@@ -6224,7 +4530,7 @@ recheck:
6224 4530
6225 if (!lock_task_sighand(p, &flags)) 4531 if (!lock_task_sighand(p, &flags))
6226 return -ESRCH; 4532 return -ESRCH;
6227 rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur; 4533 rlim_rtprio = task_rlimit(p, RLIMIT_RTPRIO);
6228 unlock_task_sighand(p, &flags); 4534 unlock_task_sighand(p, &flags);
6229 4535
6230 /* can't set/change the rt policy */ 4536 /* can't set/change the rt policy */
@@ -6272,7 +4578,7 @@ recheck:
6272 * make sure no PI-waiters arrive (or leave) while we are 4578 * make sure no PI-waiters arrive (or leave) while we are
6273 * changing the priority of the task: 4579 * changing the priority of the task:
6274 */ 4580 */
6275 spin_lock_irqsave(&p->pi_lock, flags); 4581 raw_spin_lock_irqsave(&p->pi_lock, flags);
6276 /* 4582 /*
6277 * To be able to change p->policy safely, the apropriate 4583 * To be able to change p->policy safely, the apropriate
6278 * runqueue lock must be held. 4584 * runqueue lock must be held.
@@ -6282,7 +4588,7 @@ recheck:
6282 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) { 4588 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
6283 policy = oldpolicy = -1; 4589 policy = oldpolicy = -1;
6284 __task_rq_unlock(rq); 4590 __task_rq_unlock(rq);
6285 spin_unlock_irqrestore(&p->pi_lock, flags); 4591 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
6286 goto recheck; 4592 goto recheck;
6287 } 4593 }
6288 update_rq_clock(rq); 4594 update_rq_clock(rq);
@@ -6296,6 +4602,7 @@ recheck:
6296 p->sched_reset_on_fork = reset_on_fork; 4602 p->sched_reset_on_fork = reset_on_fork;
6297 4603
6298 oldprio = p->prio; 4604 oldprio = p->prio;
4605 prev_class = p->sched_class;
6299 __setscheduler(rq, p, policy, param->sched_priority); 4606 __setscheduler(rq, p, policy, param->sched_priority);
6300 4607
6301 if (running) 4608 if (running)
@@ -6306,7 +4613,7 @@ recheck:
6306 check_class_changed(rq, p, prev_class, oldprio, running); 4613 check_class_changed(rq, p, prev_class, oldprio, running);
6307 } 4614 }
6308 __task_rq_unlock(rq); 4615 __task_rq_unlock(rq);
6309 spin_unlock_irqrestore(&p->pi_lock, flags); 4616 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
6310 4617
6311 rt_mutex_adjust_pi(p); 4618 rt_mutex_adjust_pi(p);
6312 4619
@@ -6406,7 +4713,7 @@ SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid)
6406 return -EINVAL; 4713 return -EINVAL;
6407 4714
6408 retval = -ESRCH; 4715 retval = -ESRCH;
6409 read_lock(&tasklist_lock); 4716 rcu_read_lock();
6410 p = find_process_by_pid(pid); 4717 p = find_process_by_pid(pid);
6411 if (p) { 4718 if (p) {
6412 retval = security_task_getscheduler(p); 4719 retval = security_task_getscheduler(p);
@@ -6414,7 +4721,7 @@ SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid)
6414 retval = p->policy 4721 retval = p->policy
6415 | (p->sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0); 4722 | (p->sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0);
6416 } 4723 }
6417 read_unlock(&tasklist_lock); 4724 rcu_read_unlock();
6418 return retval; 4725 return retval;
6419} 4726}
6420 4727
@@ -6432,7 +4739,7 @@ SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param)
6432 if (!param || pid < 0) 4739 if (!param || pid < 0)
6433 return -EINVAL; 4740 return -EINVAL;
6434 4741
6435 read_lock(&tasklist_lock); 4742 rcu_read_lock();
6436 p = find_process_by_pid(pid); 4743 p = find_process_by_pid(pid);
6437 retval = -ESRCH; 4744 retval = -ESRCH;
6438 if (!p) 4745 if (!p)
@@ -6443,7 +4750,7 @@ SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param)
6443 goto out_unlock; 4750 goto out_unlock;
6444 4751
6445 lp.sched_priority = p->rt_priority; 4752 lp.sched_priority = p->rt_priority;
6446 read_unlock(&tasklist_lock); 4753 rcu_read_unlock();
6447 4754
6448 /* 4755 /*
6449 * This one might sleep, we cannot do it with a spinlock held ... 4756 * This one might sleep, we cannot do it with a spinlock held ...
@@ -6453,7 +4760,7 @@ SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param)
6453 return retval; 4760 return retval;
6454 4761
6455out_unlock: 4762out_unlock:
6456 read_unlock(&tasklist_lock); 4763 rcu_read_unlock();
6457 return retval; 4764 return retval;
6458} 4765}
6459 4766
@@ -6464,22 +4771,18 @@ long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
6464 int retval; 4771 int retval;
6465 4772
6466 get_online_cpus(); 4773 get_online_cpus();
6467 read_lock(&tasklist_lock); 4774 rcu_read_lock();
6468 4775
6469 p = find_process_by_pid(pid); 4776 p = find_process_by_pid(pid);
6470 if (!p) { 4777 if (!p) {
6471 read_unlock(&tasklist_lock); 4778 rcu_read_unlock();
6472 put_online_cpus(); 4779 put_online_cpus();
6473 return -ESRCH; 4780 return -ESRCH;
6474 } 4781 }
6475 4782
6476 /* 4783 /* Prevent p going away */
6477 * It is not safe to call set_cpus_allowed with the
6478 * tasklist_lock held. We will bump the task_struct's
6479 * usage count and then drop tasklist_lock.
6480 */
6481 get_task_struct(p); 4784 get_task_struct(p);
6482 read_unlock(&tasklist_lock); 4785 rcu_read_unlock();
6483 4786
6484 if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) { 4787 if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
6485 retval = -ENOMEM; 4788 retval = -ENOMEM;
@@ -6560,10 +4863,12 @@ SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len,
6560long sched_getaffinity(pid_t pid, struct cpumask *mask) 4863long sched_getaffinity(pid_t pid, struct cpumask *mask)
6561{ 4864{
6562 struct task_struct *p; 4865 struct task_struct *p;
4866 unsigned long flags;
4867 struct rq *rq;
6563 int retval; 4868 int retval;
6564 4869
6565 get_online_cpus(); 4870 get_online_cpus();
6566 read_lock(&tasklist_lock); 4871 rcu_read_lock();
6567 4872
6568 retval = -ESRCH; 4873 retval = -ESRCH;
6569 p = find_process_by_pid(pid); 4874 p = find_process_by_pid(pid);
@@ -6574,10 +4879,12 @@ long sched_getaffinity(pid_t pid, struct cpumask *mask)
6574 if (retval) 4879 if (retval)
6575 goto out_unlock; 4880 goto out_unlock;
6576 4881
4882 rq = task_rq_lock(p, &flags);
6577 cpumask_and(mask, &p->cpus_allowed, cpu_online_mask); 4883 cpumask_and(mask, &p->cpus_allowed, cpu_online_mask);
4884 task_rq_unlock(rq, &flags);
6578 4885
6579out_unlock: 4886out_unlock:
6580 read_unlock(&tasklist_lock); 4887 rcu_read_unlock();
6581 put_online_cpus(); 4888 put_online_cpus();
6582 4889
6583 return retval; 4890 return retval;
@@ -6632,7 +4939,7 @@ SYSCALL_DEFINE0(sched_yield)
6632 */ 4939 */
6633 __release(rq->lock); 4940 __release(rq->lock);
6634 spin_release(&rq->lock.dep_map, 1, _THIS_IP_); 4941 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
6635 _raw_spin_unlock(&rq->lock); 4942 do_raw_spin_unlock(&rq->lock);
6636 preempt_enable_no_resched(); 4943 preempt_enable_no_resched();
6637 4944
6638 schedule(); 4945 schedule();
@@ -6720,9 +5027,6 @@ EXPORT_SYMBOL(yield);
6720/* 5027/*
6721 * This task is about to go to sleep on IO. Increment rq->nr_iowait so 5028 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
6722 * that process accounting knows that this is a task in IO wait state. 5029 * that process accounting knows that this is a task in IO wait state.
6723 *
6724 * But don't do that if it is a deliberate, throttling IO wait (this task
6725 * has set its backing_dev_info: the queue against which it should throttle)
6726 */ 5030 */
6727void __sched io_schedule(void) 5031void __sched io_schedule(void)
6728{ 5032{
@@ -6815,6 +5119,8 @@ SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid,
6815{ 5119{
6816 struct task_struct *p; 5120 struct task_struct *p;
6817 unsigned int time_slice; 5121 unsigned int time_slice;
5122 unsigned long flags;
5123 struct rq *rq;
6818 int retval; 5124 int retval;
6819 struct timespec t; 5125 struct timespec t;
6820 5126
@@ -6822,7 +5128,7 @@ SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid,
6822 return -EINVAL; 5128 return -EINVAL;
6823 5129
6824 retval = -ESRCH; 5130 retval = -ESRCH;
6825 read_lock(&tasklist_lock); 5131 rcu_read_lock();
6826 p = find_process_by_pid(pid); 5132 p = find_process_by_pid(pid);
6827 if (!p) 5133 if (!p)
6828 goto out_unlock; 5134 goto out_unlock;
@@ -6831,15 +5137,17 @@ SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid,
6831 if (retval) 5137 if (retval)
6832 goto out_unlock; 5138 goto out_unlock;
6833 5139
6834 time_slice = p->sched_class->get_rr_interval(p); 5140 rq = task_rq_lock(p, &flags);
5141 time_slice = p->sched_class->get_rr_interval(rq, p);
5142 task_rq_unlock(rq, &flags);
6835 5143
6836 read_unlock(&tasklist_lock); 5144 rcu_read_unlock();
6837 jiffies_to_timespec(time_slice, &t); 5145 jiffies_to_timespec(time_slice, &t);
6838 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0; 5146 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
6839 return retval; 5147 return retval;
6840 5148
6841out_unlock: 5149out_unlock:
6842 read_unlock(&tasklist_lock); 5150 rcu_read_unlock();
6843 return retval; 5151 return retval;
6844} 5152}
6845 5153
@@ -6905,7 +5213,7 @@ void show_state_filter(unsigned long state_filter)
6905 /* 5213 /*
6906 * Only show locks if all tasks are dumped: 5214 * Only show locks if all tasks are dumped:
6907 */ 5215 */
6908 if (state_filter == -1) 5216 if (!state_filter)
6909 debug_show_all_locks(); 5217 debug_show_all_locks();
6910} 5218}
6911 5219
@@ -6927,12 +5235,12 @@ void __cpuinit init_idle(struct task_struct *idle, int cpu)
6927 struct rq *rq = cpu_rq(cpu); 5235 struct rq *rq = cpu_rq(cpu);
6928 unsigned long flags; 5236 unsigned long flags;
6929 5237
6930 spin_lock_irqsave(&rq->lock, flags); 5238 raw_spin_lock_irqsave(&rq->lock, flags);
6931 5239
6932 __sched_fork(idle); 5240 __sched_fork(idle);
5241 idle->state = TASK_RUNNING;
6933 idle->se.exec_start = sched_clock(); 5242 idle->se.exec_start = sched_clock();
6934 5243
6935 idle->prio = idle->normal_prio = MAX_PRIO;
6936 cpumask_copy(&idle->cpus_allowed, cpumask_of(cpu)); 5244 cpumask_copy(&idle->cpus_allowed, cpumask_of(cpu));
6937 __set_task_cpu(idle, cpu); 5245 __set_task_cpu(idle, cpu);
6938 5246
@@ -6940,7 +5248,7 @@ void __cpuinit init_idle(struct task_struct *idle, int cpu)
6940#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW) 5248#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
6941 idle->oncpu = 1; 5249 idle->oncpu = 1;
6942#endif 5250#endif
6943 spin_unlock_irqrestore(&rq->lock, flags); 5251 raw_spin_unlock_irqrestore(&rq->lock, flags);
6944 5252
6945 /* Set the preempt count _outside_ the spinlocks! */ 5253 /* Set the preempt count _outside_ the spinlocks! */
6946#if defined(CONFIG_PREEMPT) 5254#if defined(CONFIG_PREEMPT)
@@ -6973,22 +5281,43 @@ cpumask_var_t nohz_cpu_mask;
6973 * 5281 *
6974 * This idea comes from the SD scheduler of Con Kolivas: 5282 * This idea comes from the SD scheduler of Con Kolivas:
6975 */ 5283 */
6976static inline void sched_init_granularity(void) 5284static int get_update_sysctl_factor(void)
6977{ 5285{
6978 unsigned int factor = 1 + ilog2(num_online_cpus()); 5286 unsigned int cpus = min_t(int, num_online_cpus(), 8);
6979 const unsigned long limit = 200000000; 5287 unsigned int factor;
6980 5288
6981 sysctl_sched_min_granularity *= factor; 5289 switch (sysctl_sched_tunable_scaling) {
6982 if (sysctl_sched_min_granularity > limit) 5290 case SCHED_TUNABLESCALING_NONE:
6983 sysctl_sched_min_granularity = limit; 5291 factor = 1;
5292 break;
5293 case SCHED_TUNABLESCALING_LINEAR:
5294 factor = cpus;
5295 break;
5296 case SCHED_TUNABLESCALING_LOG:
5297 default:
5298 factor = 1 + ilog2(cpus);
5299 break;
5300 }
6984 5301
6985 sysctl_sched_latency *= factor; 5302 return factor;
6986 if (sysctl_sched_latency > limit) 5303}
6987 sysctl_sched_latency = limit;
6988 5304
6989 sysctl_sched_wakeup_granularity *= factor; 5305static void update_sysctl(void)
5306{
5307 unsigned int factor = get_update_sysctl_factor();
6990 5308
6991 sysctl_sched_shares_ratelimit *= factor; 5309#define SET_SYSCTL(name) \
5310 (sysctl_##name = (factor) * normalized_sysctl_##name)
5311 SET_SYSCTL(sched_min_granularity);
5312 SET_SYSCTL(sched_latency);
5313 SET_SYSCTL(sched_wakeup_granularity);
5314 SET_SYSCTL(sched_shares_ratelimit);
5315#undef SET_SYSCTL
5316}
5317
5318static inline void sched_init_granularity(void)
5319{
5320 update_sysctl();
6992} 5321}
6993 5322
6994#ifdef CONFIG_SMP 5323#ifdef CONFIG_SMP
@@ -7025,7 +5354,8 @@ int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
7025 int ret = 0; 5354 int ret = 0;
7026 5355
7027 rq = task_rq_lock(p, &flags); 5356 rq = task_rq_lock(p, &flags);
7028 if (!cpumask_intersects(new_mask, cpu_online_mask)) { 5357
5358 if (!cpumask_intersects(new_mask, cpu_active_mask)) {
7029 ret = -EINVAL; 5359 ret = -EINVAL;
7030 goto out; 5360 goto out;
7031 } 5361 }
@@ -7047,7 +5377,7 @@ int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
7047 if (cpumask_test_cpu(task_cpu(p), new_mask)) 5377 if (cpumask_test_cpu(task_cpu(p), new_mask))
7048 goto out; 5378 goto out;
7049 5379
7050 if (migrate_task(p, cpumask_any_and(cpu_online_mask, new_mask), &req)) { 5380 if (migrate_task(p, cpumask_any_and(cpu_active_mask, new_mask), &req)) {
7051 /* Need help from migration thread: drop lock and wait. */ 5381 /* Need help from migration thread: drop lock and wait. */
7052 struct task_struct *mt = rq->migration_thread; 5382 struct task_struct *mt = rq->migration_thread;
7053 5383
@@ -7080,7 +5410,7 @@ EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
7080static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu) 5410static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
7081{ 5411{
7082 struct rq *rq_dest, *rq_src; 5412 struct rq *rq_dest, *rq_src;
7083 int ret = 0, on_rq; 5413 int ret = 0;
7084 5414
7085 if (unlikely(!cpu_active(dest_cpu))) 5415 if (unlikely(!cpu_active(dest_cpu)))
7086 return ret; 5416 return ret;
@@ -7096,12 +5426,13 @@ static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
7096 if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed)) 5426 if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed))
7097 goto fail; 5427 goto fail;
7098 5428
7099 on_rq = p->se.on_rq; 5429 /*
7100 if (on_rq) 5430 * If we're not on a rq, the next wake-up will ensure we're
5431 * placed properly.
5432 */
5433 if (p->se.on_rq) {
7101 deactivate_task(rq_src, p, 0); 5434 deactivate_task(rq_src, p, 0);
7102 5435 set_task_cpu(p, dest_cpu);
7103 set_task_cpu(p, dest_cpu);
7104 if (on_rq) {
7105 activate_task(rq_dest, p, 0); 5436 activate_task(rq_dest, p, 0);
7106 check_preempt_curr(rq_dest, p, 0); 5437 check_preempt_curr(rq_dest, p, 0);
7107 } 5438 }
@@ -7136,10 +5467,10 @@ static int migration_thread(void *data)
7136 struct migration_req *req; 5467 struct migration_req *req;
7137 struct list_head *head; 5468 struct list_head *head;
7138 5469
7139 spin_lock_irq(&rq->lock); 5470 raw_spin_lock_irq(&rq->lock);
7140 5471
7141 if (cpu_is_offline(cpu)) { 5472 if (cpu_is_offline(cpu)) {
7142 spin_unlock_irq(&rq->lock); 5473 raw_spin_unlock_irq(&rq->lock);
7143 break; 5474 break;
7144 } 5475 }
7145 5476
@@ -7151,7 +5482,7 @@ static int migration_thread(void *data)
7151 head = &rq->migration_queue; 5482 head = &rq->migration_queue;
7152 5483
7153 if (list_empty(head)) { 5484 if (list_empty(head)) {
7154 spin_unlock_irq(&rq->lock); 5485 raw_spin_unlock_irq(&rq->lock);
7155 schedule(); 5486 schedule();
7156 set_current_state(TASK_INTERRUPTIBLE); 5487 set_current_state(TASK_INTERRUPTIBLE);
7157 continue; 5488 continue;
@@ -7160,14 +5491,14 @@ static int migration_thread(void *data)
7160 list_del_init(head->next); 5491 list_del_init(head->next);
7161 5492
7162 if (req->task != NULL) { 5493 if (req->task != NULL) {
7163 spin_unlock(&rq->lock); 5494 raw_spin_unlock(&rq->lock);
7164 __migrate_task(req->task, cpu, req->dest_cpu); 5495 __migrate_task(req->task, cpu, req->dest_cpu);
7165 } else if (likely(cpu == (badcpu = smp_processor_id()))) { 5496 } else if (likely(cpu == (badcpu = smp_processor_id()))) {
7166 req->dest_cpu = RCU_MIGRATION_GOT_QS; 5497 req->dest_cpu = RCU_MIGRATION_GOT_QS;
7167 spin_unlock(&rq->lock); 5498 raw_spin_unlock(&rq->lock);
7168 } else { 5499 } else {
7169 req->dest_cpu = RCU_MIGRATION_MUST_SYNC; 5500 req->dest_cpu = RCU_MIGRATION_MUST_SYNC;
7170 spin_unlock(&rq->lock); 5501 raw_spin_unlock(&rq->lock);
7171 WARN_ONCE(1, "migration_thread() on CPU %d, expected %d\n", badcpu, cpu); 5502 WARN_ONCE(1, "migration_thread() on CPU %d, expected %d\n", badcpu, cpu);
7172 } 5503 }
7173 local_irq_enable(); 5504 local_irq_enable();
@@ -7197,37 +5528,10 @@ static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
7197static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p) 5528static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
7198{ 5529{
7199 int dest_cpu; 5530 int dest_cpu;
7200 const struct cpumask *nodemask = cpumask_of_node(cpu_to_node(dead_cpu));
7201 5531
7202again: 5532again:
7203 /* Look for allowed, online CPU in same node. */ 5533 dest_cpu = select_fallback_rq(dead_cpu, p);
7204 for_each_cpu_and(dest_cpu, nodemask, cpu_online_mask)
7205 if (cpumask_test_cpu(dest_cpu, &p->cpus_allowed))
7206 goto move;
7207
7208 /* Any allowed, online CPU? */
7209 dest_cpu = cpumask_any_and(&p->cpus_allowed, cpu_online_mask);
7210 if (dest_cpu < nr_cpu_ids)
7211 goto move;
7212
7213 /* No more Mr. Nice Guy. */
7214 if (dest_cpu >= nr_cpu_ids) {
7215 cpuset_cpus_allowed_locked(p, &p->cpus_allowed);
7216 dest_cpu = cpumask_any_and(cpu_online_mask, &p->cpus_allowed);
7217
7218 /*
7219 * Don't tell them about moving exiting tasks or
7220 * kernel threads (both mm NULL), since they never
7221 * leave kernel.
7222 */
7223 if (p->mm && printk_ratelimit()) {
7224 printk(KERN_INFO "process %d (%s) no "
7225 "longer affine to cpu%d\n",
7226 task_pid_nr(p), p->comm, dead_cpu);
7227 }
7228 }
7229 5534
7230move:
7231 /* It can have affinity changed while we were choosing. */ 5535 /* It can have affinity changed while we were choosing. */
7232 if (unlikely(!__migrate_task_irq(p, dead_cpu, dest_cpu))) 5536 if (unlikely(!__migrate_task_irq(p, dead_cpu, dest_cpu)))
7233 goto again; 5537 goto again;
@@ -7242,7 +5546,7 @@ move:
7242 */ 5546 */
7243static void migrate_nr_uninterruptible(struct rq *rq_src) 5547static void migrate_nr_uninterruptible(struct rq *rq_src)
7244{ 5548{
7245 struct rq *rq_dest = cpu_rq(cpumask_any(cpu_online_mask)); 5549 struct rq *rq_dest = cpu_rq(cpumask_any(cpu_active_mask));
7246 unsigned long flags; 5550 unsigned long flags;
7247 5551
7248 local_irq_save(flags); 5552 local_irq_save(flags);
@@ -7290,14 +5594,14 @@ void sched_idle_next(void)
7290 * Strictly not necessary since rest of the CPUs are stopped by now 5594 * Strictly not necessary since rest of the CPUs are stopped by now
7291 * and interrupts disabled on the current cpu. 5595 * and interrupts disabled on the current cpu.
7292 */ 5596 */
7293 spin_lock_irqsave(&rq->lock, flags); 5597 raw_spin_lock_irqsave(&rq->lock, flags);
7294 5598
7295 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1); 5599 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
7296 5600
7297 update_rq_clock(rq); 5601 update_rq_clock(rq);
7298 activate_task(rq, p, 0); 5602 activate_task(rq, p, 0);
7299 5603
7300 spin_unlock_irqrestore(&rq->lock, flags); 5604 raw_spin_unlock_irqrestore(&rq->lock, flags);
7301} 5605}
7302 5606
7303/* 5607/*
@@ -7333,9 +5637,9 @@ static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
7333 * that's OK. No task can be added to this CPU, so iteration is 5637 * that's OK. No task can be added to this CPU, so iteration is
7334 * fine. 5638 * fine.
7335 */ 5639 */
7336 spin_unlock_irq(&rq->lock); 5640 raw_spin_unlock_irq(&rq->lock);
7337 move_task_off_dead_cpu(dead_cpu, p); 5641 move_task_off_dead_cpu(dead_cpu, p);
7338 spin_lock_irq(&rq->lock); 5642 raw_spin_lock_irq(&rq->lock);
7339 5643
7340 put_task_struct(p); 5644 put_task_struct(p);
7341} 5645}
@@ -7376,17 +5680,16 @@ static struct ctl_table sd_ctl_dir[] = {
7376 .procname = "sched_domain", 5680 .procname = "sched_domain",
7377 .mode = 0555, 5681 .mode = 0555,
7378 }, 5682 },
7379 {0, }, 5683 {}
7380}; 5684};
7381 5685
7382static struct ctl_table sd_ctl_root[] = { 5686static struct ctl_table sd_ctl_root[] = {
7383 { 5687 {
7384 .ctl_name = CTL_KERN,
7385 .procname = "kernel", 5688 .procname = "kernel",
7386 .mode = 0555, 5689 .mode = 0555,
7387 .child = sd_ctl_dir, 5690 .child = sd_ctl_dir,
7388 }, 5691 },
7389 {0, }, 5692 {}
7390}; 5693};
7391 5694
7392static struct ctl_table *sd_alloc_ctl_entry(int n) 5695static struct ctl_table *sd_alloc_ctl_entry(int n)
@@ -7496,7 +5799,7 @@ static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
7496static struct ctl_table_header *sd_sysctl_header; 5799static struct ctl_table_header *sd_sysctl_header;
7497static void register_sched_domain_sysctl(void) 5800static void register_sched_domain_sysctl(void)
7498{ 5801{
7499 int i, cpu_num = num_online_cpus(); 5802 int i, cpu_num = num_possible_cpus();
7500 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1); 5803 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
7501 char buf[32]; 5804 char buf[32];
7502 5805
@@ -7506,7 +5809,7 @@ static void register_sched_domain_sysctl(void)
7506 if (entry == NULL) 5809 if (entry == NULL)
7507 return; 5810 return;
7508 5811
7509 for_each_online_cpu(i) { 5812 for_each_possible_cpu(i) {
7510 snprintf(buf, 32, "cpu%d", i); 5813 snprintf(buf, 32, "cpu%d", i);
7511 entry->procname = kstrdup(buf, GFP_KERNEL); 5814 entry->procname = kstrdup(buf, GFP_KERNEL);
7512 entry->mode = 0555; 5815 entry->mode = 0555;
@@ -7602,13 +5905,13 @@ migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
7602 5905
7603 /* Update our root-domain */ 5906 /* Update our root-domain */
7604 rq = cpu_rq(cpu); 5907 rq = cpu_rq(cpu);
7605 spin_lock_irqsave(&rq->lock, flags); 5908 raw_spin_lock_irqsave(&rq->lock, flags);
7606 if (rq->rd) { 5909 if (rq->rd) {
7607 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span)); 5910 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
7608 5911
7609 set_rq_online(rq); 5912 set_rq_online(rq);
7610 } 5913 }
7611 spin_unlock_irqrestore(&rq->lock, flags); 5914 raw_spin_unlock_irqrestore(&rq->lock, flags);
7612 break; 5915 break;
7613 5916
7614#ifdef CONFIG_HOTPLUG_CPU 5917#ifdef CONFIG_HOTPLUG_CPU
@@ -7633,14 +5936,13 @@ migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
7633 put_task_struct(rq->migration_thread); 5936 put_task_struct(rq->migration_thread);
7634 rq->migration_thread = NULL; 5937 rq->migration_thread = NULL;
7635 /* Idle task back to normal (off runqueue, low prio) */ 5938 /* Idle task back to normal (off runqueue, low prio) */
7636 spin_lock_irq(&rq->lock); 5939 raw_spin_lock_irq(&rq->lock);
7637 update_rq_clock(rq); 5940 update_rq_clock(rq);
7638 deactivate_task(rq, rq->idle, 0); 5941 deactivate_task(rq, rq->idle, 0);
7639 rq->idle->static_prio = MAX_PRIO;
7640 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0); 5942 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
7641 rq->idle->sched_class = &idle_sched_class; 5943 rq->idle->sched_class = &idle_sched_class;
7642 migrate_dead_tasks(cpu); 5944 migrate_dead_tasks(cpu);
7643 spin_unlock_irq(&rq->lock); 5945 raw_spin_unlock_irq(&rq->lock);
7644 cpuset_unlock(); 5946 cpuset_unlock();
7645 migrate_nr_uninterruptible(rq); 5947 migrate_nr_uninterruptible(rq);
7646 BUG_ON(rq->nr_running != 0); 5948 BUG_ON(rq->nr_running != 0);
@@ -7650,30 +5952,30 @@ migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
7650 * they didn't take sched_hotcpu_mutex. Just wake up 5952 * they didn't take sched_hotcpu_mutex. Just wake up
7651 * the requestors. 5953 * the requestors.
7652 */ 5954 */
7653 spin_lock_irq(&rq->lock); 5955 raw_spin_lock_irq(&rq->lock);
7654 while (!list_empty(&rq->migration_queue)) { 5956 while (!list_empty(&rq->migration_queue)) {
7655 struct migration_req *req; 5957 struct migration_req *req;
7656 5958
7657 req = list_entry(rq->migration_queue.next, 5959 req = list_entry(rq->migration_queue.next,
7658 struct migration_req, list); 5960 struct migration_req, list);
7659 list_del_init(&req->list); 5961 list_del_init(&req->list);
7660 spin_unlock_irq(&rq->lock); 5962 raw_spin_unlock_irq(&rq->lock);
7661 complete(&req->done); 5963 complete(&req->done);
7662 spin_lock_irq(&rq->lock); 5964 raw_spin_lock_irq(&rq->lock);
7663 } 5965 }
7664 spin_unlock_irq(&rq->lock); 5966 raw_spin_unlock_irq(&rq->lock);
7665 break; 5967 break;
7666 5968
7667 case CPU_DYING: 5969 case CPU_DYING:
7668 case CPU_DYING_FROZEN: 5970 case CPU_DYING_FROZEN:
7669 /* Update our root-domain */ 5971 /* Update our root-domain */
7670 rq = cpu_rq(cpu); 5972 rq = cpu_rq(cpu);
7671 spin_lock_irqsave(&rq->lock, flags); 5973 raw_spin_lock_irqsave(&rq->lock, flags);
7672 if (rq->rd) { 5974 if (rq->rd) {
7673 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span)); 5975 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
7674 set_rq_offline(rq); 5976 set_rq_offline(rq);
7675 } 5977 }
7676 spin_unlock_irqrestore(&rq->lock, flags); 5978 raw_spin_unlock_irqrestore(&rq->lock, flags);
7677 break; 5979 break;
7678#endif 5980#endif
7679 } 5981 }
@@ -7710,6 +6012,16 @@ early_initcall(migration_init);
7710 6012
7711#ifdef CONFIG_SCHED_DEBUG 6013#ifdef CONFIG_SCHED_DEBUG
7712 6014
6015static __read_mostly int sched_domain_debug_enabled;
6016
6017static int __init sched_domain_debug_setup(char *str)
6018{
6019 sched_domain_debug_enabled = 1;
6020
6021 return 0;
6022}
6023early_param("sched_debug", sched_domain_debug_setup);
6024
7713static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level, 6025static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
7714 struct cpumask *groupmask) 6026 struct cpumask *groupmask)
7715{ 6027{
@@ -7796,6 +6108,9 @@ static void sched_domain_debug(struct sched_domain *sd, int cpu)
7796 cpumask_var_t groupmask; 6108 cpumask_var_t groupmask;
7797 int level = 0; 6109 int level = 0;
7798 6110
6111 if (!sched_domain_debug_enabled)
6112 return;
6113
7799 if (!sd) { 6114 if (!sd) {
7800 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu); 6115 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
7801 return; 6116 return;
@@ -7875,6 +6190,8 @@ sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
7875 6190
7876static void free_rootdomain(struct root_domain *rd) 6191static void free_rootdomain(struct root_domain *rd)
7877{ 6192{
6193 synchronize_sched();
6194
7878 cpupri_cleanup(&rd->cpupri); 6195 cpupri_cleanup(&rd->cpupri);
7879 6196
7880 free_cpumask_var(rd->rto_mask); 6197 free_cpumask_var(rd->rto_mask);
@@ -7888,7 +6205,7 @@ static void rq_attach_root(struct rq *rq, struct root_domain *rd)
7888 struct root_domain *old_rd = NULL; 6205 struct root_domain *old_rd = NULL;
7889 unsigned long flags; 6206 unsigned long flags;
7890 6207
7891 spin_lock_irqsave(&rq->lock, flags); 6208 raw_spin_lock_irqsave(&rq->lock, flags);
7892 6209
7893 if (rq->rd) { 6210 if (rq->rd) {
7894 old_rd = rq->rd; 6211 old_rd = rq->rd;
@@ -7914,7 +6231,7 @@ static void rq_attach_root(struct rq *rq, struct root_domain *rd)
7914 if (cpumask_test_cpu(rq->cpu, cpu_active_mask)) 6231 if (cpumask_test_cpu(rq->cpu, cpu_active_mask))
7915 set_rq_online(rq); 6232 set_rq_online(rq);
7916 6233
7917 spin_unlock_irqrestore(&rq->lock, flags); 6234 raw_spin_unlock_irqrestore(&rq->lock, flags);
7918 6235
7919 if (old_rd) 6236 if (old_rd)
7920 free_rootdomain(old_rd); 6237 free_rootdomain(old_rd);
@@ -8015,6 +6332,7 @@ static cpumask_var_t cpu_isolated_map;
8015/* Setup the mask of cpus configured for isolated domains */ 6332/* Setup the mask of cpus configured for isolated domains */
8016static int __init isolated_cpu_setup(char *str) 6333static int __init isolated_cpu_setup(char *str)
8017{ 6334{
6335 alloc_bootmem_cpumask_var(&cpu_isolated_map);
8018 cpulist_parse(str, cpu_isolated_map); 6336 cpulist_parse(str, cpu_isolated_map);
8019 return 1; 6337 return 1;
8020} 6338}
@@ -8199,14 +6517,14 @@ enum s_alloc {
8199 */ 6517 */
8200#ifdef CONFIG_SCHED_SMT 6518#ifdef CONFIG_SCHED_SMT
8201static DEFINE_PER_CPU(struct static_sched_domain, cpu_domains); 6519static DEFINE_PER_CPU(struct static_sched_domain, cpu_domains);
8202static DEFINE_PER_CPU(struct static_sched_group, sched_group_cpus); 6520static DEFINE_PER_CPU(struct static_sched_group, sched_groups);
8203 6521
8204static int 6522static int
8205cpu_to_cpu_group(int cpu, const struct cpumask *cpu_map, 6523cpu_to_cpu_group(int cpu, const struct cpumask *cpu_map,
8206 struct sched_group **sg, struct cpumask *unused) 6524 struct sched_group **sg, struct cpumask *unused)
8207{ 6525{
8208 if (sg) 6526 if (sg)
8209 *sg = &per_cpu(sched_group_cpus, cpu).sg; 6527 *sg = &per_cpu(sched_groups, cpu).sg;
8210 return cpu; 6528 return cpu;
8211} 6529}
8212#endif /* CONFIG_SCHED_SMT */ 6530#endif /* CONFIG_SCHED_SMT */
@@ -8851,7 +7169,7 @@ static int build_sched_domains(const struct cpumask *cpu_map)
8851 return __build_sched_domains(cpu_map, NULL); 7169 return __build_sched_domains(cpu_map, NULL);
8852} 7170}
8853 7171
8854static struct cpumask *doms_cur; /* current sched domains */ 7172static cpumask_var_t *doms_cur; /* current sched domains */
8855static int ndoms_cur; /* number of sched domains in 'doms_cur' */ 7173static int ndoms_cur; /* number of sched domains in 'doms_cur' */
8856static struct sched_domain_attr *dattr_cur; 7174static struct sched_domain_attr *dattr_cur;
8857 /* attribues of custom domains in 'doms_cur' */ 7175 /* attribues of custom domains in 'doms_cur' */
@@ -8873,6 +7191,31 @@ int __attribute__((weak)) arch_update_cpu_topology(void)
8873 return 0; 7191 return 0;
8874} 7192}
8875 7193
7194cpumask_var_t *alloc_sched_domains(unsigned int ndoms)
7195{
7196 int i;
7197 cpumask_var_t *doms;
7198
7199 doms = kmalloc(sizeof(*doms) * ndoms, GFP_KERNEL);
7200 if (!doms)
7201 return NULL;
7202 for (i = 0; i < ndoms; i++) {
7203 if (!alloc_cpumask_var(&doms[i], GFP_KERNEL)) {
7204 free_sched_domains(doms, i);
7205 return NULL;
7206 }
7207 }
7208 return doms;
7209}
7210
7211void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms)
7212{
7213 unsigned int i;
7214 for (i = 0; i < ndoms; i++)
7215 free_cpumask_var(doms[i]);
7216 kfree(doms);
7217}
7218
8876/* 7219/*
8877 * Set up scheduler domains and groups. Callers must hold the hotplug lock. 7220 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
8878 * For now this just excludes isolated cpus, but could be used to 7221 * For now this just excludes isolated cpus, but could be used to
@@ -8884,12 +7227,12 @@ static int arch_init_sched_domains(const struct cpumask *cpu_map)
8884 7227
8885 arch_update_cpu_topology(); 7228 arch_update_cpu_topology();
8886 ndoms_cur = 1; 7229 ndoms_cur = 1;
8887 doms_cur = kmalloc(cpumask_size(), GFP_KERNEL); 7230 doms_cur = alloc_sched_domains(ndoms_cur);
8888 if (!doms_cur) 7231 if (!doms_cur)
8889 doms_cur = fallback_doms; 7232 doms_cur = &fallback_doms;
8890 cpumask_andnot(doms_cur, cpu_map, cpu_isolated_map); 7233 cpumask_andnot(doms_cur[0], cpu_map, cpu_isolated_map);
8891 dattr_cur = NULL; 7234 dattr_cur = NULL;
8892 err = build_sched_domains(doms_cur); 7235 err = build_sched_domains(doms_cur[0]);
8893 register_sched_domain_sysctl(); 7236 register_sched_domain_sysctl();
8894 7237
8895 return err; 7238 return err;
@@ -8939,19 +7282,19 @@ static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
8939 * doms_new[] to the current sched domain partitioning, doms_cur[]. 7282 * doms_new[] to the current sched domain partitioning, doms_cur[].
8940 * It destroys each deleted domain and builds each new domain. 7283 * It destroys each deleted domain and builds each new domain.
8941 * 7284 *
8942 * 'doms_new' is an array of cpumask's of length 'ndoms_new'. 7285 * 'doms_new' is an array of cpumask_var_t's of length 'ndoms_new'.
8943 * The masks don't intersect (don't overlap.) We should setup one 7286 * The masks don't intersect (don't overlap.) We should setup one
8944 * sched domain for each mask. CPUs not in any of the cpumasks will 7287 * sched domain for each mask. CPUs not in any of the cpumasks will
8945 * not be load balanced. If the same cpumask appears both in the 7288 * not be load balanced. If the same cpumask appears both in the
8946 * current 'doms_cur' domains and in the new 'doms_new', we can leave 7289 * current 'doms_cur' domains and in the new 'doms_new', we can leave
8947 * it as it is. 7290 * it as it is.
8948 * 7291 *
8949 * The passed in 'doms_new' should be kmalloc'd. This routine takes 7292 * The passed in 'doms_new' should be allocated using
8950 * ownership of it and will kfree it when done with it. If the caller 7293 * alloc_sched_domains. This routine takes ownership of it and will
8951 * failed the kmalloc call, then it can pass in doms_new == NULL && 7294 * free_sched_domains it when done with it. If the caller failed the
8952 * ndoms_new == 1, and partition_sched_domains() will fallback to 7295 * alloc call, then it can pass in doms_new == NULL && ndoms_new == 1,
8953 * the single partition 'fallback_doms', it also forces the domains 7296 * and partition_sched_domains() will fallback to the single partition
8954 * to be rebuilt. 7297 * 'fallback_doms', it also forces the domains to be rebuilt.
8955 * 7298 *
8956 * If doms_new == NULL it will be replaced with cpu_online_mask. 7299 * If doms_new == NULL it will be replaced with cpu_online_mask.
8957 * ndoms_new == 0 is a special case for destroying existing domains, 7300 * ndoms_new == 0 is a special case for destroying existing domains,
@@ -8959,8 +7302,7 @@ static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
8959 * 7302 *
8960 * Call with hotplug lock held 7303 * Call with hotplug lock held
8961 */ 7304 */
8962/* FIXME: Change to struct cpumask *doms_new[] */ 7305void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
8963void partition_sched_domains(int ndoms_new, struct cpumask *doms_new,
8964 struct sched_domain_attr *dattr_new) 7306 struct sched_domain_attr *dattr_new)
8965{ 7307{
8966 int i, j, n; 7308 int i, j, n;
@@ -8979,40 +7321,40 @@ void partition_sched_domains(int ndoms_new, struct cpumask *doms_new,
8979 /* Destroy deleted domains */ 7321 /* Destroy deleted domains */
8980 for (i = 0; i < ndoms_cur; i++) { 7322 for (i = 0; i < ndoms_cur; i++) {
8981 for (j = 0; j < n && !new_topology; j++) { 7323 for (j = 0; j < n && !new_topology; j++) {
8982 if (cpumask_equal(&doms_cur[i], &doms_new[j]) 7324 if (cpumask_equal(doms_cur[i], doms_new[j])
8983 && dattrs_equal(dattr_cur, i, dattr_new, j)) 7325 && dattrs_equal(dattr_cur, i, dattr_new, j))
8984 goto match1; 7326 goto match1;
8985 } 7327 }
8986 /* no match - a current sched domain not in new doms_new[] */ 7328 /* no match - a current sched domain not in new doms_new[] */
8987 detach_destroy_domains(doms_cur + i); 7329 detach_destroy_domains(doms_cur[i]);
8988match1: 7330match1:
8989 ; 7331 ;
8990 } 7332 }
8991 7333
8992 if (doms_new == NULL) { 7334 if (doms_new == NULL) {
8993 ndoms_cur = 0; 7335 ndoms_cur = 0;
8994 doms_new = fallback_doms; 7336 doms_new = &fallback_doms;
8995 cpumask_andnot(&doms_new[0], cpu_online_mask, cpu_isolated_map); 7337 cpumask_andnot(doms_new[0], cpu_active_mask, cpu_isolated_map);
8996 WARN_ON_ONCE(dattr_new); 7338 WARN_ON_ONCE(dattr_new);
8997 } 7339 }
8998 7340
8999 /* Build new domains */ 7341 /* Build new domains */
9000 for (i = 0; i < ndoms_new; i++) { 7342 for (i = 0; i < ndoms_new; i++) {
9001 for (j = 0; j < ndoms_cur && !new_topology; j++) { 7343 for (j = 0; j < ndoms_cur && !new_topology; j++) {
9002 if (cpumask_equal(&doms_new[i], &doms_cur[j]) 7344 if (cpumask_equal(doms_new[i], doms_cur[j])
9003 && dattrs_equal(dattr_new, i, dattr_cur, j)) 7345 && dattrs_equal(dattr_new, i, dattr_cur, j))
9004 goto match2; 7346 goto match2;
9005 } 7347 }
9006 /* no match - add a new doms_new */ 7348 /* no match - add a new doms_new */
9007 __build_sched_domains(doms_new + i, 7349 __build_sched_domains(doms_new[i],
9008 dattr_new ? dattr_new + i : NULL); 7350 dattr_new ? dattr_new + i : NULL);
9009match2: 7351match2:
9010 ; 7352 ;
9011 } 7353 }
9012 7354
9013 /* Remember the new sched domains */ 7355 /* Remember the new sched domains */
9014 if (doms_cur != fallback_doms) 7356 if (doms_cur != &fallback_doms)
9015 kfree(doms_cur); 7357 free_sched_domains(doms_cur, ndoms_cur);
9016 kfree(dattr_cur); /* kfree(NULL) is safe */ 7358 kfree(dattr_cur); /* kfree(NULL) is safe */
9017 doms_cur = doms_new; 7359 doms_cur = doms_new;
9018 dattr_cur = dattr_new; 7360 dattr_cur = dattr_new;
@@ -9064,11 +7406,13 @@ static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
9064 7406
9065#ifdef CONFIG_SCHED_MC 7407#ifdef CONFIG_SCHED_MC
9066static ssize_t sched_mc_power_savings_show(struct sysdev_class *class, 7408static ssize_t sched_mc_power_savings_show(struct sysdev_class *class,
7409 struct sysdev_class_attribute *attr,
9067 char *page) 7410 char *page)
9068{ 7411{
9069 return sprintf(page, "%u\n", sched_mc_power_savings); 7412 return sprintf(page, "%u\n", sched_mc_power_savings);
9070} 7413}
9071static ssize_t sched_mc_power_savings_store(struct sysdev_class *class, 7414static ssize_t sched_mc_power_savings_store(struct sysdev_class *class,
7415 struct sysdev_class_attribute *attr,
9072 const char *buf, size_t count) 7416 const char *buf, size_t count)
9073{ 7417{
9074 return sched_power_savings_store(buf, count, 0); 7418 return sched_power_savings_store(buf, count, 0);
@@ -9080,11 +7424,13 @@ static SYSDEV_CLASS_ATTR(sched_mc_power_savings, 0644,
9080 7424
9081#ifdef CONFIG_SCHED_SMT 7425#ifdef CONFIG_SCHED_SMT
9082static ssize_t sched_smt_power_savings_show(struct sysdev_class *dev, 7426static ssize_t sched_smt_power_savings_show(struct sysdev_class *dev,
7427 struct sysdev_class_attribute *attr,
9083 char *page) 7428 char *page)
9084{ 7429{
9085 return sprintf(page, "%u\n", sched_smt_power_savings); 7430 return sprintf(page, "%u\n", sched_smt_power_savings);
9086} 7431}
9087static ssize_t sched_smt_power_savings_store(struct sysdev_class *dev, 7432static ssize_t sched_smt_power_savings_store(struct sysdev_class *dev,
7433 struct sysdev_class_attribute *attr,
9088 const char *buf, size_t count) 7434 const char *buf, size_t count)
9089{ 7435{
9090 return sched_power_savings_store(buf, count, 1); 7436 return sched_power_savings_store(buf, count, 1);
@@ -9123,8 +7469,10 @@ static int update_sched_domains(struct notifier_block *nfb,
9123 switch (action) { 7469 switch (action) {
9124 case CPU_ONLINE: 7470 case CPU_ONLINE:
9125 case CPU_ONLINE_FROZEN: 7471 case CPU_ONLINE_FROZEN:
9126 case CPU_DEAD: 7472 case CPU_DOWN_PREPARE:
9127 case CPU_DEAD_FROZEN: 7473 case CPU_DOWN_PREPARE_FROZEN:
7474 case CPU_DOWN_FAILED:
7475 case CPU_DOWN_FAILED_FROZEN:
9128 partition_sched_domains(1, NULL, NULL); 7476 partition_sched_domains(1, NULL, NULL);
9129 return NOTIFY_OK; 7477 return NOTIFY_OK;
9130 7478
@@ -9171,7 +7519,7 @@ void __init sched_init_smp(void)
9171#endif 7519#endif
9172 get_online_cpus(); 7520 get_online_cpus();
9173 mutex_lock(&sched_domains_mutex); 7521 mutex_lock(&sched_domains_mutex);
9174 arch_init_sched_domains(cpu_online_mask); 7522 arch_init_sched_domains(cpu_active_mask);
9175 cpumask_andnot(non_isolated_cpus, cpu_possible_mask, cpu_isolated_map); 7523 cpumask_andnot(non_isolated_cpus, cpu_possible_mask, cpu_isolated_map);
9176 if (cpumask_empty(non_isolated_cpus)) 7524 if (cpumask_empty(non_isolated_cpus))
9177 cpumask_set_cpu(smp_processor_id(), non_isolated_cpus); 7525 cpumask_set_cpu(smp_processor_id(), non_isolated_cpus);
@@ -9244,13 +7592,13 @@ static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
9244#ifdef CONFIG_SMP 7592#ifdef CONFIG_SMP
9245 rt_rq->rt_nr_migratory = 0; 7593 rt_rq->rt_nr_migratory = 0;
9246 rt_rq->overloaded = 0; 7594 rt_rq->overloaded = 0;
9247 plist_head_init(&rt_rq->pushable_tasks, &rq->lock); 7595 plist_head_init_raw(&rt_rq->pushable_tasks, &rq->lock);
9248#endif 7596#endif
9249 7597
9250 rt_rq->rt_time = 0; 7598 rt_rq->rt_time = 0;
9251 rt_rq->rt_throttled = 0; 7599 rt_rq->rt_throttled = 0;
9252 rt_rq->rt_runtime = 0; 7600 rt_rq->rt_runtime = 0;
9253 spin_lock_init(&rt_rq->rt_runtime_lock); 7601 raw_spin_lock_init(&rt_rq->rt_runtime_lock);
9254 7602
9255#ifdef CONFIG_RT_GROUP_SCHED 7603#ifdef CONFIG_RT_GROUP_SCHED
9256 rt_rq->rt_nr_boosted = 0; 7604 rt_rq->rt_nr_boosted = 0;
@@ -9297,7 +7645,6 @@ static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
9297 tg->rt_rq[cpu] = rt_rq; 7645 tg->rt_rq[cpu] = rt_rq;
9298 init_rt_rq(rt_rq, rq); 7646 init_rt_rq(rt_rq, rq);
9299 rt_rq->tg = tg; 7647 rt_rq->tg = tg;
9300 rt_rq->rt_se = rt_se;
9301 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime; 7648 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
9302 if (add) 7649 if (add)
9303 list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list); 7650 list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
@@ -9328,16 +7675,9 @@ void __init sched_init(void)
9328#ifdef CONFIG_RT_GROUP_SCHED 7675#ifdef CONFIG_RT_GROUP_SCHED
9329 alloc_size += 2 * nr_cpu_ids * sizeof(void **); 7676 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
9330#endif 7677#endif
9331#ifdef CONFIG_USER_SCHED
9332 alloc_size *= 2;
9333#endif
9334#ifdef CONFIG_CPUMASK_OFFSTACK 7678#ifdef CONFIG_CPUMASK_OFFSTACK
9335 alloc_size += num_possible_cpus() * cpumask_size(); 7679 alloc_size += num_possible_cpus() * cpumask_size();
9336#endif 7680#endif
9337 /*
9338 * As sched_init() is called before page_alloc is setup,
9339 * we use alloc_bootmem().
9340 */
9341 if (alloc_size) { 7681 if (alloc_size) {
9342 ptr = (unsigned long)kzalloc(alloc_size, GFP_NOWAIT); 7682 ptr = (unsigned long)kzalloc(alloc_size, GFP_NOWAIT);
9343 7683
@@ -9348,13 +7688,6 @@ void __init sched_init(void)
9348 init_task_group.cfs_rq = (struct cfs_rq **)ptr; 7688 init_task_group.cfs_rq = (struct cfs_rq **)ptr;
9349 ptr += nr_cpu_ids * sizeof(void **); 7689 ptr += nr_cpu_ids * sizeof(void **);
9350 7690
9351#ifdef CONFIG_USER_SCHED
9352 root_task_group.se = (struct sched_entity **)ptr;
9353 ptr += nr_cpu_ids * sizeof(void **);
9354
9355 root_task_group.cfs_rq = (struct cfs_rq **)ptr;
9356 ptr += nr_cpu_ids * sizeof(void **);
9357#endif /* CONFIG_USER_SCHED */
9358#endif /* CONFIG_FAIR_GROUP_SCHED */ 7691#endif /* CONFIG_FAIR_GROUP_SCHED */
9359#ifdef CONFIG_RT_GROUP_SCHED 7692#ifdef CONFIG_RT_GROUP_SCHED
9360 init_task_group.rt_se = (struct sched_rt_entity **)ptr; 7693 init_task_group.rt_se = (struct sched_rt_entity **)ptr;
@@ -9363,13 +7696,6 @@ void __init sched_init(void)
9363 init_task_group.rt_rq = (struct rt_rq **)ptr; 7696 init_task_group.rt_rq = (struct rt_rq **)ptr;
9364 ptr += nr_cpu_ids * sizeof(void **); 7697 ptr += nr_cpu_ids * sizeof(void **);
9365 7698
9366#ifdef CONFIG_USER_SCHED
9367 root_task_group.rt_se = (struct sched_rt_entity **)ptr;
9368 ptr += nr_cpu_ids * sizeof(void **);
9369
9370 root_task_group.rt_rq = (struct rt_rq **)ptr;
9371 ptr += nr_cpu_ids * sizeof(void **);
9372#endif /* CONFIG_USER_SCHED */
9373#endif /* CONFIG_RT_GROUP_SCHED */ 7699#endif /* CONFIG_RT_GROUP_SCHED */
9374#ifdef CONFIG_CPUMASK_OFFSTACK 7700#ifdef CONFIG_CPUMASK_OFFSTACK
9375 for_each_possible_cpu(i) { 7701 for_each_possible_cpu(i) {
@@ -9389,28 +7715,23 @@ void __init sched_init(void)
9389#ifdef CONFIG_RT_GROUP_SCHED 7715#ifdef CONFIG_RT_GROUP_SCHED
9390 init_rt_bandwidth(&init_task_group.rt_bandwidth, 7716 init_rt_bandwidth(&init_task_group.rt_bandwidth,
9391 global_rt_period(), global_rt_runtime()); 7717 global_rt_period(), global_rt_runtime());
9392#ifdef CONFIG_USER_SCHED
9393 init_rt_bandwidth(&root_task_group.rt_bandwidth,
9394 global_rt_period(), RUNTIME_INF);
9395#endif /* CONFIG_USER_SCHED */
9396#endif /* CONFIG_RT_GROUP_SCHED */ 7718#endif /* CONFIG_RT_GROUP_SCHED */
9397 7719
9398#ifdef CONFIG_GROUP_SCHED 7720#ifdef CONFIG_CGROUP_SCHED
9399 list_add(&init_task_group.list, &task_groups); 7721 list_add(&init_task_group.list, &task_groups);
9400 INIT_LIST_HEAD(&init_task_group.children); 7722 INIT_LIST_HEAD(&init_task_group.children);
9401 7723
9402#ifdef CONFIG_USER_SCHED 7724#endif /* CONFIG_CGROUP_SCHED */
9403 INIT_LIST_HEAD(&root_task_group.children);
9404 init_task_group.parent = &root_task_group;
9405 list_add(&init_task_group.siblings, &root_task_group.children);
9406#endif /* CONFIG_USER_SCHED */
9407#endif /* CONFIG_GROUP_SCHED */
9408 7725
7726#if defined CONFIG_FAIR_GROUP_SCHED && defined CONFIG_SMP
7727 update_shares_data = __alloc_percpu(nr_cpu_ids * sizeof(unsigned long),
7728 __alignof__(unsigned long));
7729#endif
9409 for_each_possible_cpu(i) { 7730 for_each_possible_cpu(i) {
9410 struct rq *rq; 7731 struct rq *rq;
9411 7732
9412 rq = cpu_rq(i); 7733 rq = cpu_rq(i);
9413 spin_lock_init(&rq->lock); 7734 raw_spin_lock_init(&rq->lock);
9414 rq->nr_running = 0; 7735 rq->nr_running = 0;
9415 rq->calc_load_active = 0; 7736 rq->calc_load_active = 0;
9416 rq->calc_load_update = jiffies + LOAD_FREQ; 7737 rq->calc_load_update = jiffies + LOAD_FREQ;
@@ -9440,25 +7761,6 @@ void __init sched_init(void)
9440 * directly in rq->cfs (i.e init_task_group->se[] = NULL). 7761 * directly in rq->cfs (i.e init_task_group->se[] = NULL).
9441 */ 7762 */
9442 init_tg_cfs_entry(&init_task_group, &rq->cfs, NULL, i, 1, NULL); 7763 init_tg_cfs_entry(&init_task_group, &rq->cfs, NULL, i, 1, NULL);
9443#elif defined CONFIG_USER_SCHED
9444 root_task_group.shares = NICE_0_LOAD;
9445 init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, 0, NULL);
9446 /*
9447 * In case of task-groups formed thr' the user id of tasks,
9448 * init_task_group represents tasks belonging to root user.
9449 * Hence it forms a sibling of all subsequent groups formed.
9450 * In this case, init_task_group gets only a fraction of overall
9451 * system cpu resource, based on the weight assigned to root
9452 * user's cpu share (INIT_TASK_GROUP_LOAD). This is accomplished
9453 * by letting tasks of init_task_group sit in a separate cfs_rq
9454 * (init_tg_cfs_rq) and having one entity represent this group of
9455 * tasks in rq->cfs (i.e init_task_group->se[] != NULL).
9456 */
9457 init_tg_cfs_entry(&init_task_group,
9458 &per_cpu(init_tg_cfs_rq, i),
9459 &per_cpu(init_sched_entity, i), i, 1,
9460 root_task_group.se[i]);
9461
9462#endif 7764#endif
9463#endif /* CONFIG_FAIR_GROUP_SCHED */ 7765#endif /* CONFIG_FAIR_GROUP_SCHED */
9464 7766
@@ -9467,12 +7769,6 @@ void __init sched_init(void)
9467 INIT_LIST_HEAD(&rq->leaf_rt_rq_list); 7769 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
9468#ifdef CONFIG_CGROUP_SCHED 7770#ifdef CONFIG_CGROUP_SCHED
9469 init_tg_rt_entry(&init_task_group, &rq->rt, NULL, i, 1, NULL); 7771 init_tg_rt_entry(&init_task_group, &rq->rt, NULL, i, 1, NULL);
9470#elif defined CONFIG_USER_SCHED
9471 init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, 0, NULL);
9472 init_tg_rt_entry(&init_task_group,
9473 &per_cpu(init_rt_rq, i),
9474 &per_cpu(init_sched_rt_entity, i), i, 1,
9475 root_task_group.rt_se[i]);
9476#endif 7772#endif
9477#endif 7773#endif
9478 7774
@@ -9488,6 +7784,8 @@ void __init sched_init(void)
9488 rq->cpu = i; 7784 rq->cpu = i;
9489 rq->online = 0; 7785 rq->online = 0;
9490 rq->migration_thread = NULL; 7786 rq->migration_thread = NULL;
7787 rq->idle_stamp = 0;
7788 rq->avg_idle = 2*sysctl_sched_migration_cost;
9491 INIT_LIST_HEAD(&rq->migration_queue); 7789 INIT_LIST_HEAD(&rq->migration_queue);
9492 rq_attach_root(rq, &def_root_domain); 7790 rq_attach_root(rq, &def_root_domain);
9493#endif 7791#endif
@@ -9506,7 +7804,7 @@ void __init sched_init(void)
9506#endif 7804#endif
9507 7805
9508#ifdef CONFIG_RT_MUTEXES 7806#ifdef CONFIG_RT_MUTEXES
9509 plist_head_init(&init_task.pi_waiters, &init_task.pi_lock); 7807 plist_head_init_raw(&init_task.pi_waiters, &init_task.pi_lock);
9510#endif 7808#endif
9511 7809
9512 /* 7810 /*
@@ -9531,13 +7829,15 @@ void __init sched_init(void)
9531 current->sched_class = &fair_sched_class; 7829 current->sched_class = &fair_sched_class;
9532 7830
9533 /* Allocate the nohz_cpu_mask if CONFIG_CPUMASK_OFFSTACK */ 7831 /* Allocate the nohz_cpu_mask if CONFIG_CPUMASK_OFFSTACK */
9534 alloc_cpumask_var(&nohz_cpu_mask, GFP_NOWAIT); 7832 zalloc_cpumask_var(&nohz_cpu_mask, GFP_NOWAIT);
9535#ifdef CONFIG_SMP 7833#ifdef CONFIG_SMP
9536#ifdef CONFIG_NO_HZ 7834#ifdef CONFIG_NO_HZ
9537 alloc_cpumask_var(&nohz.cpu_mask, GFP_NOWAIT); 7835 zalloc_cpumask_var(&nohz.cpu_mask, GFP_NOWAIT);
9538 alloc_cpumask_var(&nohz.ilb_grp_nohz_mask, GFP_NOWAIT); 7836 alloc_cpumask_var(&nohz.ilb_grp_nohz_mask, GFP_NOWAIT);
9539#endif 7837#endif
9540 alloc_cpumask_var(&cpu_isolated_map, GFP_NOWAIT); 7838 /* May be allocated at isolcpus cmdline parse time */
7839 if (cpu_isolated_map == NULL)
7840 zalloc_cpumask_var(&cpu_isolated_map, GFP_NOWAIT);
9541#endif /* SMP */ 7841#endif /* SMP */
9542 7842
9543 perf_event_init(); 7843 perf_event_init();
@@ -9548,12 +7848,12 @@ void __init sched_init(void)
9548#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP 7848#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
9549static inline int preempt_count_equals(int preempt_offset) 7849static inline int preempt_count_equals(int preempt_offset)
9550{ 7850{
9551 int nested = preempt_count() & ~PREEMPT_ACTIVE; 7851 int nested = (preempt_count() & ~PREEMPT_ACTIVE) + rcu_preempt_depth();
9552 7852
9553 return (nested == PREEMPT_INATOMIC_BASE + preempt_offset); 7853 return (nested == PREEMPT_INATOMIC_BASE + preempt_offset);
9554} 7854}
9555 7855
9556void __might_sleep(char *file, int line, int preempt_offset) 7856void __might_sleep(const char *file, int line, int preempt_offset)
9557{ 7857{
9558#ifdef in_atomic 7858#ifdef in_atomic
9559 static unsigned long prev_jiffy; /* ratelimiting */ 7859 static unsigned long prev_jiffy; /* ratelimiting */
@@ -9629,13 +7929,13 @@ void normalize_rt_tasks(void)
9629 continue; 7929 continue;
9630 } 7930 }
9631 7931
9632 spin_lock(&p->pi_lock); 7932 raw_spin_lock(&p->pi_lock);
9633 rq = __task_rq_lock(p); 7933 rq = __task_rq_lock(p);
9634 7934
9635 normalize_task(rq, p); 7935 normalize_task(rq, p);
9636 7936
9637 __task_rq_unlock(rq); 7937 __task_rq_unlock(rq);
9638 spin_unlock(&p->pi_lock); 7938 raw_spin_unlock(&p->pi_lock);
9639 } while_each_thread(g, p); 7939 } while_each_thread(g, p);
9640 7940
9641 read_unlock_irqrestore(&tasklist_lock, flags); 7941 read_unlock_irqrestore(&tasklist_lock, flags);
@@ -9731,13 +8031,15 @@ int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
9731 se = kzalloc_node(sizeof(struct sched_entity), 8031 se = kzalloc_node(sizeof(struct sched_entity),
9732 GFP_KERNEL, cpu_to_node(i)); 8032 GFP_KERNEL, cpu_to_node(i));
9733 if (!se) 8033 if (!se)
9734 goto err; 8034 goto err_free_rq;
9735 8035
9736 init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent->se[i]); 8036 init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent->se[i]);
9737 } 8037 }
9738 8038
9739 return 1; 8039 return 1;
9740 8040
8041 err_free_rq:
8042 kfree(cfs_rq);
9741 err: 8043 err:
9742 return 0; 8044 return 0;
9743} 8045}
@@ -9819,13 +8121,15 @@ int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
9819 rt_se = kzalloc_node(sizeof(struct sched_rt_entity), 8121 rt_se = kzalloc_node(sizeof(struct sched_rt_entity),
9820 GFP_KERNEL, cpu_to_node(i)); 8122 GFP_KERNEL, cpu_to_node(i));
9821 if (!rt_se) 8123 if (!rt_se)
9822 goto err; 8124 goto err_free_rq;
9823 8125
9824 init_tg_rt_entry(tg, rt_rq, rt_se, i, 0, parent->rt_se[i]); 8126 init_tg_rt_entry(tg, rt_rq, rt_se, i, 0, parent->rt_se[i]);
9825 } 8127 }
9826 8128
9827 return 1; 8129 return 1;
9828 8130
8131 err_free_rq:
8132 kfree(rt_rq);
9829 err: 8133 err:
9830 return 0; 8134 return 0;
9831} 8135}
@@ -9860,7 +8164,7 @@ static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
9860} 8164}
9861#endif /* CONFIG_RT_GROUP_SCHED */ 8165#endif /* CONFIG_RT_GROUP_SCHED */
9862 8166
9863#ifdef CONFIG_GROUP_SCHED 8167#ifdef CONFIG_CGROUP_SCHED
9864static void free_sched_group(struct task_group *tg) 8168static void free_sched_group(struct task_group *tg)
9865{ 8169{
9866 free_fair_sched_group(tg); 8170 free_fair_sched_group(tg);
@@ -9959,17 +8263,17 @@ void sched_move_task(struct task_struct *tsk)
9959 8263
9960#ifdef CONFIG_FAIR_GROUP_SCHED 8264#ifdef CONFIG_FAIR_GROUP_SCHED
9961 if (tsk->sched_class->moved_group) 8265 if (tsk->sched_class->moved_group)
9962 tsk->sched_class->moved_group(tsk); 8266 tsk->sched_class->moved_group(tsk, on_rq);
9963#endif 8267#endif
9964 8268
9965 if (unlikely(running)) 8269 if (unlikely(running))
9966 tsk->sched_class->set_curr_task(rq); 8270 tsk->sched_class->set_curr_task(rq);
9967 if (on_rq) 8271 if (on_rq)
9968 enqueue_task(rq, tsk, 0); 8272 enqueue_task(rq, tsk, 0, false);
9969 8273
9970 task_rq_unlock(rq, &flags); 8274 task_rq_unlock(rq, &flags);
9971} 8275}
9972#endif /* CONFIG_GROUP_SCHED */ 8276#endif /* CONFIG_CGROUP_SCHED */
9973 8277
9974#ifdef CONFIG_FAIR_GROUP_SCHED 8278#ifdef CONFIG_FAIR_GROUP_SCHED
9975static void __set_se_shares(struct sched_entity *se, unsigned long shares) 8279static void __set_se_shares(struct sched_entity *se, unsigned long shares)
@@ -9994,9 +8298,9 @@ static void set_se_shares(struct sched_entity *se, unsigned long shares)
9994 struct rq *rq = cfs_rq->rq; 8298 struct rq *rq = cfs_rq->rq;
9995 unsigned long flags; 8299 unsigned long flags;
9996 8300
9997 spin_lock_irqsave(&rq->lock, flags); 8301 raw_spin_lock_irqsave(&rq->lock, flags);
9998 __set_se_shares(se, shares); 8302 __set_se_shares(se, shares);
9999 spin_unlock_irqrestore(&rq->lock, flags); 8303 raw_spin_unlock_irqrestore(&rq->lock, flags);
10000} 8304}
10001 8305
10002static DEFINE_MUTEX(shares_mutex); 8306static DEFINE_MUTEX(shares_mutex);
@@ -10111,13 +8415,6 @@ static int tg_schedulable(struct task_group *tg, void *data)
10111 runtime = d->rt_runtime; 8415 runtime = d->rt_runtime;
10112 } 8416 }
10113 8417
10114#ifdef CONFIG_USER_SCHED
10115 if (tg == &root_task_group) {
10116 period = global_rt_period();
10117 runtime = global_rt_runtime();
10118 }
10119#endif
10120
10121 /* 8418 /*
10122 * Cannot have more runtime than the period. 8419 * Cannot have more runtime than the period.
10123 */ 8420 */
@@ -10181,18 +8478,18 @@ static int tg_set_bandwidth(struct task_group *tg,
10181 if (err) 8478 if (err)
10182 goto unlock; 8479 goto unlock;
10183 8480
10184 spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock); 8481 raw_spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
10185 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period); 8482 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
10186 tg->rt_bandwidth.rt_runtime = rt_runtime; 8483 tg->rt_bandwidth.rt_runtime = rt_runtime;
10187 8484
10188 for_each_possible_cpu(i) { 8485 for_each_possible_cpu(i) {
10189 struct rt_rq *rt_rq = tg->rt_rq[i]; 8486 struct rt_rq *rt_rq = tg->rt_rq[i];
10190 8487
10191 spin_lock(&rt_rq->rt_runtime_lock); 8488 raw_spin_lock(&rt_rq->rt_runtime_lock);
10192 rt_rq->rt_runtime = rt_runtime; 8489 rt_rq->rt_runtime = rt_runtime;
10193 spin_unlock(&rt_rq->rt_runtime_lock); 8490 raw_spin_unlock(&rt_rq->rt_runtime_lock);
10194 } 8491 }
10195 spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock); 8492 raw_spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
10196 unlock: 8493 unlock:
10197 read_unlock(&tasklist_lock); 8494 read_unlock(&tasklist_lock);
10198 mutex_unlock(&rt_constraints_mutex); 8495 mutex_unlock(&rt_constraints_mutex);
@@ -10297,15 +8594,15 @@ static int sched_rt_global_constraints(void)
10297 if (sysctl_sched_rt_runtime == 0) 8594 if (sysctl_sched_rt_runtime == 0)
10298 return -EBUSY; 8595 return -EBUSY;
10299 8596
10300 spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags); 8597 raw_spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
10301 for_each_possible_cpu(i) { 8598 for_each_possible_cpu(i) {
10302 struct rt_rq *rt_rq = &cpu_rq(i)->rt; 8599 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
10303 8600
10304 spin_lock(&rt_rq->rt_runtime_lock); 8601 raw_spin_lock(&rt_rq->rt_runtime_lock);
10305 rt_rq->rt_runtime = global_rt_runtime(); 8602 rt_rq->rt_runtime = global_rt_runtime();
10306 spin_unlock(&rt_rq->rt_runtime_lock); 8603 raw_spin_unlock(&rt_rq->rt_runtime_lock);
10307 } 8604 }
10308 spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags); 8605 raw_spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
10309 8606
10310 return 0; 8607 return 0;
10311} 8608}
@@ -10520,7 +8817,7 @@ struct cgroup_subsys cpu_cgroup_subsys = {
10520struct cpuacct { 8817struct cpuacct {
10521 struct cgroup_subsys_state css; 8818 struct cgroup_subsys_state css;
10522 /* cpuusage holds pointer to a u64-type object on every cpu */ 8819 /* cpuusage holds pointer to a u64-type object on every cpu */
10523 u64 *cpuusage; 8820 u64 __percpu *cpuusage;
10524 struct percpu_counter cpustat[CPUACCT_STAT_NSTATS]; 8821 struct percpu_counter cpustat[CPUACCT_STAT_NSTATS];
10525 struct cpuacct *parent; 8822 struct cpuacct *parent;
10526}; 8823};
@@ -10596,9 +8893,9 @@ static u64 cpuacct_cpuusage_read(struct cpuacct *ca, int cpu)
10596 /* 8893 /*
10597 * Take rq->lock to make 64-bit read safe on 32-bit platforms. 8894 * Take rq->lock to make 64-bit read safe on 32-bit platforms.
10598 */ 8895 */
10599 spin_lock_irq(&cpu_rq(cpu)->lock); 8896 raw_spin_lock_irq(&cpu_rq(cpu)->lock);
10600 data = *cpuusage; 8897 data = *cpuusage;
10601 spin_unlock_irq(&cpu_rq(cpu)->lock); 8898 raw_spin_unlock_irq(&cpu_rq(cpu)->lock);
10602#else 8899#else
10603 data = *cpuusage; 8900 data = *cpuusage;
10604#endif 8901#endif
@@ -10614,9 +8911,9 @@ static void cpuacct_cpuusage_write(struct cpuacct *ca, int cpu, u64 val)
10614 /* 8911 /*
10615 * Take rq->lock to make 64-bit write safe on 32-bit platforms. 8912 * Take rq->lock to make 64-bit write safe on 32-bit platforms.
10616 */ 8913 */
10617 spin_lock_irq(&cpu_rq(cpu)->lock); 8914 raw_spin_lock_irq(&cpu_rq(cpu)->lock);
10618 *cpuusage = val; 8915 *cpuusage = val;
10619 spin_unlock_irq(&cpu_rq(cpu)->lock); 8916 raw_spin_unlock_irq(&cpu_rq(cpu)->lock);
10620#else 8917#else
10621 *cpuusage = val; 8918 *cpuusage = val;
10622#endif 8919#endif
@@ -10737,12 +9034,30 @@ static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
10737} 9034}
10738 9035
10739/* 9036/*
9037 * When CONFIG_VIRT_CPU_ACCOUNTING is enabled one jiffy can be very large
9038 * in cputime_t units. As a result, cpuacct_update_stats calls
9039 * percpu_counter_add with values large enough to always overflow the
9040 * per cpu batch limit causing bad SMP scalability.
9041 *
9042 * To fix this we scale percpu_counter_batch by cputime_one_jiffy so we
9043 * batch the same amount of time with CONFIG_VIRT_CPU_ACCOUNTING disabled
9044 * and enabled. We cap it at INT_MAX which is the largest allowed batch value.
9045 */
9046#ifdef CONFIG_SMP
9047#define CPUACCT_BATCH \
9048 min_t(long, percpu_counter_batch * cputime_one_jiffy, INT_MAX)
9049#else
9050#define CPUACCT_BATCH 0
9051#endif
9052
9053/*
10740 * Charge the system/user time to the task's accounting group. 9054 * Charge the system/user time to the task's accounting group.
10741 */ 9055 */
10742static void cpuacct_update_stats(struct task_struct *tsk, 9056static void cpuacct_update_stats(struct task_struct *tsk,
10743 enum cpuacct_stat_index idx, cputime_t val) 9057 enum cpuacct_stat_index idx, cputime_t val)
10744{ 9058{
10745 struct cpuacct *ca; 9059 struct cpuacct *ca;
9060 int batch = CPUACCT_BATCH;
10746 9061
10747 if (unlikely(!cpuacct_subsys.active)) 9062 if (unlikely(!cpuacct_subsys.active))
10748 return; 9063 return;
@@ -10751,7 +9066,7 @@ static void cpuacct_update_stats(struct task_struct *tsk,
10751 ca = task_ca(tsk); 9066 ca = task_ca(tsk);
10752 9067
10753 do { 9068 do {
10754 percpu_counter_add(&ca->cpustat[idx], val); 9069 __percpu_counter_add(&ca->cpustat[idx], val, batch);
10755 ca = ca->parent; 9070 ca = ca->parent;
10756 } while (ca); 9071 } while (ca);
10757 rcu_read_unlock(); 9072 rcu_read_unlock();
@@ -10850,9 +9165,9 @@ void synchronize_sched_expedited(void)
10850 init_completion(&req->done); 9165 init_completion(&req->done);
10851 req->task = NULL; 9166 req->task = NULL;
10852 req->dest_cpu = RCU_MIGRATION_NEED_QS; 9167 req->dest_cpu = RCU_MIGRATION_NEED_QS;
10853 spin_lock_irqsave(&rq->lock, flags); 9168 raw_spin_lock_irqsave(&rq->lock, flags);
10854 list_add(&req->list, &rq->migration_queue); 9169 list_add(&req->list, &rq->migration_queue);
10855 spin_unlock_irqrestore(&rq->lock, flags); 9170 raw_spin_unlock_irqrestore(&rq->lock, flags);
10856 wake_up_process(rq->migration_thread); 9171 wake_up_process(rq->migration_thread);
10857 } 9172 }
10858 for_each_online_cpu(cpu) { 9173 for_each_online_cpu(cpu) {
@@ -10860,13 +9175,14 @@ void synchronize_sched_expedited(void)
10860 req = &per_cpu(rcu_migration_req, cpu); 9175 req = &per_cpu(rcu_migration_req, cpu);
10861 rq = cpu_rq(cpu); 9176 rq = cpu_rq(cpu);
10862 wait_for_completion(&req->done); 9177 wait_for_completion(&req->done);
10863 spin_lock_irqsave(&rq->lock, flags); 9178 raw_spin_lock_irqsave(&rq->lock, flags);
10864 if (unlikely(req->dest_cpu == RCU_MIGRATION_MUST_SYNC)) 9179 if (unlikely(req->dest_cpu == RCU_MIGRATION_MUST_SYNC))
10865 need_full_sync = 1; 9180 need_full_sync = 1;
10866 req->dest_cpu = RCU_MIGRATION_IDLE; 9181 req->dest_cpu = RCU_MIGRATION_IDLE;
10867 spin_unlock_irqrestore(&rq->lock, flags); 9182 raw_spin_unlock_irqrestore(&rq->lock, flags);
10868 } 9183 }
10869 rcu_expedited_state = RCU_EXPEDITED_STATE_IDLE; 9184 rcu_expedited_state = RCU_EXPEDITED_STATE_IDLE;
9185 synchronize_sched_expedited_count++;
10870 mutex_unlock(&rcu_sched_expedited_mutex); 9186 mutex_unlock(&rcu_sched_expedited_mutex);
10871 put_online_cpus(); 9187 put_online_cpus();
10872 if (need_full_sync) 9188 if (need_full_sync)