aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/sched.c
diff options
context:
space:
mode:
authorLen Brown <len.brown@intel.com>2006-06-29 19:57:46 -0400
committerLen Brown <len.brown@intel.com>2006-06-29 19:57:46 -0400
commitd120cfb544ed6161b9d32fb6c4648c471807ee6b (patch)
tree7757ad0198d8df76ff5c60f939a687687c41da00 /kernel/sched.c
parent9dce0e950dbfab4148f35ac6f297d8638cdc63c4 (diff)
parentbf7e8511088963078484132636839b59e25cf14f (diff)
merge linus into release branch
Conflicts: drivers/acpi/acpi_memhotplug.c
Diffstat (limited to 'kernel/sched.c')
-rw-r--r--kernel/sched.c1199
1 files changed, 854 insertions, 345 deletions
diff --git a/kernel/sched.c b/kernel/sched.c
index a856040c200a..2629c1711fd6 100644
--- a/kernel/sched.c
+++ b/kernel/sched.c
@@ -168,15 +168,21 @@
168 */ 168 */
169 169
170#define SCALE_PRIO(x, prio) \ 170#define SCALE_PRIO(x, prio) \
171 max(x * (MAX_PRIO - prio) / (MAX_USER_PRIO/2), MIN_TIMESLICE) 171 max(x * (MAX_PRIO - prio) / (MAX_USER_PRIO / 2), MIN_TIMESLICE)
172 172
173static unsigned int task_timeslice(task_t *p) 173static unsigned int static_prio_timeslice(int static_prio)
174{ 174{
175 if (p->static_prio < NICE_TO_PRIO(0)) 175 if (static_prio < NICE_TO_PRIO(0))
176 return SCALE_PRIO(DEF_TIMESLICE*4, p->static_prio); 176 return SCALE_PRIO(DEF_TIMESLICE * 4, static_prio);
177 else 177 else
178 return SCALE_PRIO(DEF_TIMESLICE, p->static_prio); 178 return SCALE_PRIO(DEF_TIMESLICE, static_prio);
179} 179}
180
181static inline unsigned int task_timeslice(task_t *p)
182{
183 return static_prio_timeslice(p->static_prio);
184}
185
180#define task_hot(p, now, sd) ((long long) ((now) - (p)->last_ran) \ 186#define task_hot(p, now, sd) ((long long) ((now) - (p)->last_ran) \
181 < (long long) (sd)->cache_hot_time) 187 < (long long) (sd)->cache_hot_time)
182 188
@@ -184,13 +190,11 @@ static unsigned int task_timeslice(task_t *p)
184 * These are the runqueue data structures: 190 * These are the runqueue data structures:
185 */ 191 */
186 192
187#define BITMAP_SIZE ((((MAX_PRIO+1+7)/8)+sizeof(long)-1)/sizeof(long))
188
189typedef struct runqueue runqueue_t; 193typedef struct runqueue runqueue_t;
190 194
191struct prio_array { 195struct prio_array {
192 unsigned int nr_active; 196 unsigned int nr_active;
193 unsigned long bitmap[BITMAP_SIZE]; 197 DECLARE_BITMAP(bitmap, MAX_PRIO+1); /* include 1 bit for delimiter */
194 struct list_head queue[MAX_PRIO]; 198 struct list_head queue[MAX_PRIO];
195}; 199};
196 200
@@ -209,6 +213,7 @@ struct runqueue {
209 * remote CPUs use both these fields when doing load calculation. 213 * remote CPUs use both these fields when doing load calculation.
210 */ 214 */
211 unsigned long nr_running; 215 unsigned long nr_running;
216 unsigned long raw_weighted_load;
212#ifdef CONFIG_SMP 217#ifdef CONFIG_SMP
213 unsigned long cpu_load[3]; 218 unsigned long cpu_load[3];
214#endif 219#endif
@@ -239,7 +244,6 @@ struct runqueue {
239 244
240 task_t *migration_thread; 245 task_t *migration_thread;
241 struct list_head migration_queue; 246 struct list_head migration_queue;
242 int cpu;
243#endif 247#endif
244 248
245#ifdef CONFIG_SCHEDSTATS 249#ifdef CONFIG_SCHEDSTATS
@@ -351,11 +355,30 @@ static inline void finish_lock_switch(runqueue_t *rq, task_t *prev)
351#endif /* __ARCH_WANT_UNLOCKED_CTXSW */ 355#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
352 356
353/* 357/*
358 * __task_rq_lock - lock the runqueue a given task resides on.
359 * Must be called interrupts disabled.
360 */
361static inline runqueue_t *__task_rq_lock(task_t *p)
362 __acquires(rq->lock)
363{
364 struct runqueue *rq;
365
366repeat_lock_task:
367 rq = task_rq(p);
368 spin_lock(&rq->lock);
369 if (unlikely(rq != task_rq(p))) {
370 spin_unlock(&rq->lock);
371 goto repeat_lock_task;
372 }
373 return rq;
374}
375
376/*
354 * task_rq_lock - lock the runqueue a given task resides on and disable 377 * task_rq_lock - lock the runqueue a given task resides on and disable
355 * interrupts. Note the ordering: we can safely lookup the task_rq without 378 * interrupts. Note the ordering: we can safely lookup the task_rq without
356 * explicitly disabling preemption. 379 * explicitly disabling preemption.
357 */ 380 */
358static inline runqueue_t *task_rq_lock(task_t *p, unsigned long *flags) 381static runqueue_t *task_rq_lock(task_t *p, unsigned long *flags)
359 __acquires(rq->lock) 382 __acquires(rq->lock)
360{ 383{
361 struct runqueue *rq; 384 struct runqueue *rq;
@@ -371,6 +394,12 @@ repeat_lock_task:
371 return rq; 394 return rq;
372} 395}
373 396
397static inline void __task_rq_unlock(runqueue_t *rq)
398 __releases(rq->lock)
399{
400 spin_unlock(&rq->lock);
401}
402
374static inline void task_rq_unlock(runqueue_t *rq, unsigned long *flags) 403static inline void task_rq_unlock(runqueue_t *rq, unsigned long *flags)
375 __releases(rq->lock) 404 __releases(rq->lock)
376{ 405{
@@ -634,7 +663,7 @@ static inline void enqueue_task_head(struct task_struct *p, prio_array_t *array)
634} 663}
635 664
636/* 665/*
637 * effective_prio - return the priority that is based on the static 666 * __normal_prio - return the priority that is based on the static
638 * priority but is modified by bonuses/penalties. 667 * priority but is modified by bonuses/penalties.
639 * 668 *
640 * We scale the actual sleep average [0 .... MAX_SLEEP_AVG] 669 * We scale the actual sleep average [0 .... MAX_SLEEP_AVG]
@@ -647,13 +676,11 @@ static inline void enqueue_task_head(struct task_struct *p, prio_array_t *array)
647 * 676 *
648 * Both properties are important to certain workloads. 677 * Both properties are important to certain workloads.
649 */ 678 */
650static int effective_prio(task_t *p) 679
680static inline int __normal_prio(task_t *p)
651{ 681{
652 int bonus, prio; 682 int bonus, prio;
653 683
654 if (rt_task(p))
655 return p->prio;
656
657 bonus = CURRENT_BONUS(p) - MAX_BONUS / 2; 684 bonus = CURRENT_BONUS(p) - MAX_BONUS / 2;
658 685
659 prio = p->static_prio - bonus; 686 prio = p->static_prio - bonus;
@@ -665,6 +692,106 @@ static int effective_prio(task_t *p)
665} 692}
666 693
667/* 694/*
695 * To aid in avoiding the subversion of "niceness" due to uneven distribution
696 * of tasks with abnormal "nice" values across CPUs the contribution that
697 * each task makes to its run queue's load is weighted according to its
698 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
699 * scaled version of the new time slice allocation that they receive on time
700 * slice expiry etc.
701 */
702
703/*
704 * Assume: static_prio_timeslice(NICE_TO_PRIO(0)) == DEF_TIMESLICE
705 * If static_prio_timeslice() is ever changed to break this assumption then
706 * this code will need modification
707 */
708#define TIME_SLICE_NICE_ZERO DEF_TIMESLICE
709#define LOAD_WEIGHT(lp) \
710 (((lp) * SCHED_LOAD_SCALE) / TIME_SLICE_NICE_ZERO)
711#define PRIO_TO_LOAD_WEIGHT(prio) \
712 LOAD_WEIGHT(static_prio_timeslice(prio))
713#define RTPRIO_TO_LOAD_WEIGHT(rp) \
714 (PRIO_TO_LOAD_WEIGHT(MAX_RT_PRIO) + LOAD_WEIGHT(rp))
715
716static void set_load_weight(task_t *p)
717{
718 if (has_rt_policy(p)) {
719#ifdef CONFIG_SMP
720 if (p == task_rq(p)->migration_thread)
721 /*
722 * The migration thread does the actual balancing.
723 * Giving its load any weight will skew balancing
724 * adversely.
725 */
726 p->load_weight = 0;
727 else
728#endif
729 p->load_weight = RTPRIO_TO_LOAD_WEIGHT(p->rt_priority);
730 } else
731 p->load_weight = PRIO_TO_LOAD_WEIGHT(p->static_prio);
732}
733
734static inline void inc_raw_weighted_load(runqueue_t *rq, const task_t *p)
735{
736 rq->raw_weighted_load += p->load_weight;
737}
738
739static inline void dec_raw_weighted_load(runqueue_t *rq, const task_t *p)
740{
741 rq->raw_weighted_load -= p->load_weight;
742}
743
744static inline void inc_nr_running(task_t *p, runqueue_t *rq)
745{
746 rq->nr_running++;
747 inc_raw_weighted_load(rq, p);
748}
749
750static inline void dec_nr_running(task_t *p, runqueue_t *rq)
751{
752 rq->nr_running--;
753 dec_raw_weighted_load(rq, p);
754}
755
756/*
757 * Calculate the expected normal priority: i.e. priority
758 * without taking RT-inheritance into account. Might be
759 * boosted by interactivity modifiers. Changes upon fork,
760 * setprio syscalls, and whenever the interactivity
761 * estimator recalculates.
762 */
763static inline int normal_prio(task_t *p)
764{
765 int prio;
766
767 if (has_rt_policy(p))
768 prio = MAX_RT_PRIO-1 - p->rt_priority;
769 else
770 prio = __normal_prio(p);
771 return prio;
772}
773
774/*
775 * Calculate the current priority, i.e. the priority
776 * taken into account by the scheduler. This value might
777 * be boosted by RT tasks, or might be boosted by
778 * interactivity modifiers. Will be RT if the task got
779 * RT-boosted. If not then it returns p->normal_prio.
780 */
781static int effective_prio(task_t *p)
782{
783 p->normal_prio = normal_prio(p);
784 /*
785 * If we are RT tasks or we were boosted to RT priority,
786 * keep the priority unchanged. Otherwise, update priority
787 * to the normal priority:
788 */
789 if (!rt_prio(p->prio))
790 return p->normal_prio;
791 return p->prio;
792}
793
794/*
668 * __activate_task - move a task to the runqueue. 795 * __activate_task - move a task to the runqueue.
669 */ 796 */
670static void __activate_task(task_t *p, runqueue_t *rq) 797static void __activate_task(task_t *p, runqueue_t *rq)
@@ -674,7 +801,7 @@ static void __activate_task(task_t *p, runqueue_t *rq)
674 if (batch_task(p)) 801 if (batch_task(p))
675 target = rq->expired; 802 target = rq->expired;
676 enqueue_task(p, target); 803 enqueue_task(p, target);
677 rq->nr_running++; 804 inc_nr_running(p, rq);
678} 805}
679 806
680/* 807/*
@@ -683,39 +810,45 @@ static void __activate_task(task_t *p, runqueue_t *rq)
683static inline void __activate_idle_task(task_t *p, runqueue_t *rq) 810static inline void __activate_idle_task(task_t *p, runqueue_t *rq)
684{ 811{
685 enqueue_task_head(p, rq->active); 812 enqueue_task_head(p, rq->active);
686 rq->nr_running++; 813 inc_nr_running(p, rq);
687} 814}
688 815
816/*
817 * Recalculate p->normal_prio and p->prio after having slept,
818 * updating the sleep-average too:
819 */
689static int recalc_task_prio(task_t *p, unsigned long long now) 820static int recalc_task_prio(task_t *p, unsigned long long now)
690{ 821{
691 /* Caller must always ensure 'now >= p->timestamp' */ 822 /* Caller must always ensure 'now >= p->timestamp' */
692 unsigned long long __sleep_time = now - p->timestamp; 823 unsigned long sleep_time = now - p->timestamp;
693 unsigned long sleep_time;
694 824
695 if (batch_task(p)) 825 if (batch_task(p))
696 sleep_time = 0; 826 sleep_time = 0;
697 else {
698 if (__sleep_time > NS_MAX_SLEEP_AVG)
699 sleep_time = NS_MAX_SLEEP_AVG;
700 else
701 sleep_time = (unsigned long)__sleep_time;
702 }
703 827
704 if (likely(sleep_time > 0)) { 828 if (likely(sleep_time > 0)) {
705 /* 829 /*
706 * User tasks that sleep a long time are categorised as 830 * This ceiling is set to the lowest priority that would allow
707 * idle. They will only have their sleep_avg increased to a 831 * a task to be reinserted into the active array on timeslice
708 * level that makes them just interactive priority to stay 832 * completion.
709 * active yet prevent them suddenly becoming cpu hogs and
710 * starving other processes.
711 */ 833 */
712 if (p->mm && sleep_time > INTERACTIVE_SLEEP(p)) { 834 unsigned long ceiling = INTERACTIVE_SLEEP(p);
713 unsigned long ceiling;
714 835
715 ceiling = JIFFIES_TO_NS(MAX_SLEEP_AVG - 836 if (p->mm && sleep_time > ceiling && p->sleep_avg < ceiling) {
716 DEF_TIMESLICE); 837 /*
717 if (p->sleep_avg < ceiling) 838 * Prevents user tasks from achieving best priority
718 p->sleep_avg = ceiling; 839 * with one single large enough sleep.
840 */
841 p->sleep_avg = ceiling;
842 /*
843 * Using INTERACTIVE_SLEEP() as a ceiling places a
844 * nice(0) task 1ms sleep away from promotion, and
845 * gives it 700ms to round-robin with no chance of
846 * being demoted. This is more than generous, so
847 * mark this sleep as non-interactive to prevent the
848 * on-runqueue bonus logic from intervening should
849 * this task not receive cpu immediately.
850 */
851 p->sleep_type = SLEEP_NONINTERACTIVE;
719 } else { 852 } else {
720 /* 853 /*
721 * Tasks waking from uninterruptible sleep are 854 * Tasks waking from uninterruptible sleep are
@@ -723,12 +856,12 @@ static int recalc_task_prio(task_t *p, unsigned long long now)
723 * are likely to be waiting on I/O 856 * are likely to be waiting on I/O
724 */ 857 */
725 if (p->sleep_type == SLEEP_NONINTERACTIVE && p->mm) { 858 if (p->sleep_type == SLEEP_NONINTERACTIVE && p->mm) {
726 if (p->sleep_avg >= INTERACTIVE_SLEEP(p)) 859 if (p->sleep_avg >= ceiling)
727 sleep_time = 0; 860 sleep_time = 0;
728 else if (p->sleep_avg + sleep_time >= 861 else if (p->sleep_avg + sleep_time >=
729 INTERACTIVE_SLEEP(p)) { 862 ceiling) {
730 p->sleep_avg = INTERACTIVE_SLEEP(p); 863 p->sleep_avg = ceiling;
731 sleep_time = 0; 864 sleep_time = 0;
732 } 865 }
733 } 866 }
734 867
@@ -742,9 +875,9 @@ static int recalc_task_prio(task_t *p, unsigned long long now)
742 */ 875 */
743 p->sleep_avg += sleep_time; 876 p->sleep_avg += sleep_time;
744 877
745 if (p->sleep_avg > NS_MAX_SLEEP_AVG)
746 p->sleep_avg = NS_MAX_SLEEP_AVG;
747 } 878 }
879 if (p->sleep_avg > NS_MAX_SLEEP_AVG)
880 p->sleep_avg = NS_MAX_SLEEP_AVG;
748 } 881 }
749 882
750 return effective_prio(p); 883 return effective_prio(p);
@@ -805,7 +938,7 @@ static void activate_task(task_t *p, runqueue_t *rq, int local)
805 */ 938 */
806static void deactivate_task(struct task_struct *p, runqueue_t *rq) 939static void deactivate_task(struct task_struct *p, runqueue_t *rq)
807{ 940{
808 rq->nr_running--; 941 dec_nr_running(p, rq);
809 dequeue_task(p, p->array); 942 dequeue_task(p, p->array);
810 p->array = NULL; 943 p->array = NULL;
811} 944}
@@ -860,6 +993,12 @@ inline int task_curr(const task_t *p)
860 return cpu_curr(task_cpu(p)) == p; 993 return cpu_curr(task_cpu(p)) == p;
861} 994}
862 995
996/* Used instead of source_load when we know the type == 0 */
997unsigned long weighted_cpuload(const int cpu)
998{
999 return cpu_rq(cpu)->raw_weighted_load;
1000}
1001
863#ifdef CONFIG_SMP 1002#ifdef CONFIG_SMP
864typedef struct { 1003typedef struct {
865 struct list_head list; 1004 struct list_head list;
@@ -949,7 +1088,8 @@ void kick_process(task_t *p)
949} 1088}
950 1089
951/* 1090/*
952 * Return a low guess at the load of a migration-source cpu. 1091 * Return a low guess at the load of a migration-source cpu weighted
1092 * according to the scheduling class and "nice" value.
953 * 1093 *
954 * We want to under-estimate the load of migration sources, to 1094 * We want to under-estimate the load of migration sources, to
955 * balance conservatively. 1095 * balance conservatively.
@@ -957,24 +1097,36 @@ void kick_process(task_t *p)
957static inline unsigned long source_load(int cpu, int type) 1097static inline unsigned long source_load(int cpu, int type)
958{ 1098{
959 runqueue_t *rq = cpu_rq(cpu); 1099 runqueue_t *rq = cpu_rq(cpu);
960 unsigned long load_now = rq->nr_running * SCHED_LOAD_SCALE; 1100
961 if (type == 0) 1101 if (type == 0)
962 return load_now; 1102 return rq->raw_weighted_load;
963 1103
964 return min(rq->cpu_load[type-1], load_now); 1104 return min(rq->cpu_load[type-1], rq->raw_weighted_load);
965} 1105}
966 1106
967/* 1107/*
968 * Return a high guess at the load of a migration-target cpu 1108 * Return a high guess at the load of a migration-target cpu weighted
1109 * according to the scheduling class and "nice" value.
969 */ 1110 */
970static inline unsigned long target_load(int cpu, int type) 1111static inline unsigned long target_load(int cpu, int type)
971{ 1112{
972 runqueue_t *rq = cpu_rq(cpu); 1113 runqueue_t *rq = cpu_rq(cpu);
973 unsigned long load_now = rq->nr_running * SCHED_LOAD_SCALE; 1114
974 if (type == 0) 1115 if (type == 0)
975 return load_now; 1116 return rq->raw_weighted_load;
1117
1118 return max(rq->cpu_load[type-1], rq->raw_weighted_load);
1119}
1120
1121/*
1122 * Return the average load per task on the cpu's run queue
1123 */
1124static inline unsigned long cpu_avg_load_per_task(int cpu)
1125{
1126 runqueue_t *rq = cpu_rq(cpu);
1127 unsigned long n = rq->nr_running;
976 1128
977 return max(rq->cpu_load[type-1], load_now); 1129 return n ? rq->raw_weighted_load / n : SCHED_LOAD_SCALE;
978} 1130}
979 1131
980/* 1132/*
@@ -1047,7 +1199,7 @@ find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
1047 cpus_and(tmp, group->cpumask, p->cpus_allowed); 1199 cpus_and(tmp, group->cpumask, p->cpus_allowed);
1048 1200
1049 for_each_cpu_mask(i, tmp) { 1201 for_each_cpu_mask(i, tmp) {
1050 load = source_load(i, 0); 1202 load = weighted_cpuload(i);
1051 1203
1052 if (load < min_load || (load == min_load && i == this_cpu)) { 1204 if (load < min_load || (load == min_load && i == this_cpu)) {
1053 min_load = load; 1205 min_load = load;
@@ -1074,9 +1226,15 @@ static int sched_balance_self(int cpu, int flag)
1074 struct task_struct *t = current; 1226 struct task_struct *t = current;
1075 struct sched_domain *tmp, *sd = NULL; 1227 struct sched_domain *tmp, *sd = NULL;
1076 1228
1077 for_each_domain(cpu, tmp) 1229 for_each_domain(cpu, tmp) {
1230 /*
1231 * If power savings logic is enabled for a domain, stop there.
1232 */
1233 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
1234 break;
1078 if (tmp->flags & flag) 1235 if (tmp->flags & flag)
1079 sd = tmp; 1236 sd = tmp;
1237 }
1080 1238
1081 while (sd) { 1239 while (sd) {
1082 cpumask_t span; 1240 cpumask_t span;
@@ -1226,17 +1384,19 @@ static int try_to_wake_up(task_t *p, unsigned int state, int sync)
1226 1384
1227 if (this_sd->flags & SD_WAKE_AFFINE) { 1385 if (this_sd->flags & SD_WAKE_AFFINE) {
1228 unsigned long tl = this_load; 1386 unsigned long tl = this_load;
1387 unsigned long tl_per_task = cpu_avg_load_per_task(this_cpu);
1388
1229 /* 1389 /*
1230 * If sync wakeup then subtract the (maximum possible) 1390 * If sync wakeup then subtract the (maximum possible)
1231 * effect of the currently running task from the load 1391 * effect of the currently running task from the load
1232 * of the current CPU: 1392 * of the current CPU:
1233 */ 1393 */
1234 if (sync) 1394 if (sync)
1235 tl -= SCHED_LOAD_SCALE; 1395 tl -= current->load_weight;
1236 1396
1237 if ((tl <= load && 1397 if ((tl <= load &&
1238 tl + target_load(cpu, idx) <= SCHED_LOAD_SCALE) || 1398 tl + target_load(cpu, idx) <= tl_per_task) ||
1239 100*(tl + SCHED_LOAD_SCALE) <= imbalance*load) { 1399 100*(tl + p->load_weight) <= imbalance*load) {
1240 /* 1400 /*
1241 * This domain has SD_WAKE_AFFINE and 1401 * This domain has SD_WAKE_AFFINE and
1242 * p is cache cold in this domain, and 1402 * p is cache cold in this domain, and
@@ -1353,6 +1513,12 @@ void fastcall sched_fork(task_t *p, int clone_flags)
1353 * event cannot wake it up and insert it on the runqueue either. 1513 * event cannot wake it up and insert it on the runqueue either.
1354 */ 1514 */
1355 p->state = TASK_RUNNING; 1515 p->state = TASK_RUNNING;
1516
1517 /*
1518 * Make sure we do not leak PI boosting priority to the child:
1519 */
1520 p->prio = current->normal_prio;
1521
1356 INIT_LIST_HEAD(&p->run_list); 1522 INIT_LIST_HEAD(&p->run_list);
1357 p->array = NULL; 1523 p->array = NULL;
1358#ifdef CONFIG_SCHEDSTATS 1524#ifdef CONFIG_SCHEDSTATS
@@ -1432,10 +1598,11 @@ void fastcall wake_up_new_task(task_t *p, unsigned long clone_flags)
1432 __activate_task(p, rq); 1598 __activate_task(p, rq);
1433 else { 1599 else {
1434 p->prio = current->prio; 1600 p->prio = current->prio;
1601 p->normal_prio = current->normal_prio;
1435 list_add_tail(&p->run_list, &current->run_list); 1602 list_add_tail(&p->run_list, &current->run_list);
1436 p->array = current->array; 1603 p->array = current->array;
1437 p->array->nr_active++; 1604 p->array->nr_active++;
1438 rq->nr_running++; 1605 inc_nr_running(p, rq);
1439 } 1606 }
1440 set_need_resched(); 1607 set_need_resched();
1441 } else 1608 } else
@@ -1653,7 +1820,8 @@ unsigned long nr_uninterruptible(void)
1653 1820
1654unsigned long long nr_context_switches(void) 1821unsigned long long nr_context_switches(void)
1655{ 1822{
1656 unsigned long long i, sum = 0; 1823 int i;
1824 unsigned long long sum = 0;
1657 1825
1658 for_each_possible_cpu(i) 1826 for_each_possible_cpu(i)
1659 sum += cpu_rq(i)->nr_switches; 1827 sum += cpu_rq(i)->nr_switches;
@@ -1691,9 +1859,6 @@ unsigned long nr_active(void)
1691/* 1859/*
1692 * double_rq_lock - safely lock two runqueues 1860 * double_rq_lock - safely lock two runqueues
1693 * 1861 *
1694 * We must take them in cpu order to match code in
1695 * dependent_sleeper and wake_dependent_sleeper.
1696 *
1697 * Note this does not disable interrupts like task_rq_lock, 1862 * Note this does not disable interrupts like task_rq_lock,
1698 * you need to do so manually before calling. 1863 * you need to do so manually before calling.
1699 */ 1864 */
@@ -1705,7 +1870,7 @@ static void double_rq_lock(runqueue_t *rq1, runqueue_t *rq2)
1705 spin_lock(&rq1->lock); 1870 spin_lock(&rq1->lock);
1706 __acquire(rq2->lock); /* Fake it out ;) */ 1871 __acquire(rq2->lock); /* Fake it out ;) */
1707 } else { 1872 } else {
1708 if (rq1->cpu < rq2->cpu) { 1873 if (rq1 < rq2) {
1709 spin_lock(&rq1->lock); 1874 spin_lock(&rq1->lock);
1710 spin_lock(&rq2->lock); 1875 spin_lock(&rq2->lock);
1711 } else { 1876 } else {
@@ -1741,7 +1906,7 @@ static void double_lock_balance(runqueue_t *this_rq, runqueue_t *busiest)
1741 __acquires(this_rq->lock) 1906 __acquires(this_rq->lock)
1742{ 1907{
1743 if (unlikely(!spin_trylock(&busiest->lock))) { 1908 if (unlikely(!spin_trylock(&busiest->lock))) {
1744 if (busiest->cpu < this_rq->cpu) { 1909 if (busiest < this_rq) {
1745 spin_unlock(&this_rq->lock); 1910 spin_unlock(&this_rq->lock);
1746 spin_lock(&busiest->lock); 1911 spin_lock(&busiest->lock);
1747 spin_lock(&this_rq->lock); 1912 spin_lock(&this_rq->lock);
@@ -1804,9 +1969,9 @@ void pull_task(runqueue_t *src_rq, prio_array_t *src_array, task_t *p,
1804 runqueue_t *this_rq, prio_array_t *this_array, int this_cpu) 1969 runqueue_t *this_rq, prio_array_t *this_array, int this_cpu)
1805{ 1970{
1806 dequeue_task(p, src_array); 1971 dequeue_task(p, src_array);
1807 src_rq->nr_running--; 1972 dec_nr_running(p, src_rq);
1808 set_task_cpu(p, this_cpu); 1973 set_task_cpu(p, this_cpu);
1809 this_rq->nr_running++; 1974 inc_nr_running(p, this_rq);
1810 enqueue_task(p, this_array); 1975 enqueue_task(p, this_array);
1811 p->timestamp = (p->timestamp - src_rq->timestamp_last_tick) 1976 p->timestamp = (p->timestamp - src_rq->timestamp_last_tick)
1812 + this_rq->timestamp_last_tick; 1977 + this_rq->timestamp_last_tick;
@@ -1853,26 +2018,42 @@ int can_migrate_task(task_t *p, runqueue_t *rq, int this_cpu,
1853 return 1; 2018 return 1;
1854} 2019}
1855 2020
2021#define rq_best_prio(rq) min((rq)->curr->prio, (rq)->best_expired_prio)
1856/* 2022/*
1857 * move_tasks tries to move up to max_nr_move tasks from busiest to this_rq, 2023 * move_tasks tries to move up to max_nr_move tasks and max_load_move weighted
1858 * as part of a balancing operation within "domain". Returns the number of 2024 * load from busiest to this_rq, as part of a balancing operation within
1859 * tasks moved. 2025 * "domain". Returns the number of tasks moved.
1860 * 2026 *
1861 * Called with both runqueues locked. 2027 * Called with both runqueues locked.
1862 */ 2028 */
1863static int move_tasks(runqueue_t *this_rq, int this_cpu, runqueue_t *busiest, 2029static int move_tasks(runqueue_t *this_rq, int this_cpu, runqueue_t *busiest,
1864 unsigned long max_nr_move, struct sched_domain *sd, 2030 unsigned long max_nr_move, unsigned long max_load_move,
1865 enum idle_type idle, int *all_pinned) 2031 struct sched_domain *sd, enum idle_type idle,
2032 int *all_pinned)
1866{ 2033{
1867 prio_array_t *array, *dst_array; 2034 prio_array_t *array, *dst_array;
1868 struct list_head *head, *curr; 2035 struct list_head *head, *curr;
1869 int idx, pulled = 0, pinned = 0; 2036 int idx, pulled = 0, pinned = 0, this_best_prio, busiest_best_prio;
2037 int busiest_best_prio_seen;
2038 int skip_for_load; /* skip the task based on weighted load issues */
2039 long rem_load_move;
1870 task_t *tmp; 2040 task_t *tmp;
1871 2041
1872 if (max_nr_move == 0) 2042 if (max_nr_move == 0 || max_load_move == 0)
1873 goto out; 2043 goto out;
1874 2044
2045 rem_load_move = max_load_move;
1875 pinned = 1; 2046 pinned = 1;
2047 this_best_prio = rq_best_prio(this_rq);
2048 busiest_best_prio = rq_best_prio(busiest);
2049 /*
2050 * Enable handling of the case where there is more than one task
2051 * with the best priority. If the current running task is one
2052 * of those with prio==busiest_best_prio we know it won't be moved
2053 * and therefore it's safe to override the skip (based on load) of
2054 * any task we find with that prio.
2055 */
2056 busiest_best_prio_seen = busiest_best_prio == busiest->curr->prio;
1876 2057
1877 /* 2058 /*
1878 * We first consider expired tasks. Those will likely not be 2059 * We first consider expired tasks. Those will likely not be
@@ -1912,7 +2093,17 @@ skip_queue:
1912 2093
1913 curr = curr->prev; 2094 curr = curr->prev;
1914 2095
1915 if (!can_migrate_task(tmp, busiest, this_cpu, sd, idle, &pinned)) { 2096 /*
2097 * To help distribute high priority tasks accross CPUs we don't
2098 * skip a task if it will be the highest priority task (i.e. smallest
2099 * prio value) on its new queue regardless of its load weight
2100 */
2101 skip_for_load = tmp->load_weight > rem_load_move;
2102 if (skip_for_load && idx < this_best_prio)
2103 skip_for_load = !busiest_best_prio_seen && idx == busiest_best_prio;
2104 if (skip_for_load ||
2105 !can_migrate_task(tmp, busiest, this_cpu, sd, idle, &pinned)) {
2106 busiest_best_prio_seen |= idx == busiest_best_prio;
1916 if (curr != head) 2107 if (curr != head)
1917 goto skip_queue; 2108 goto skip_queue;
1918 idx++; 2109 idx++;
@@ -1926,9 +2117,15 @@ skip_queue:
1926 2117
1927 pull_task(busiest, array, tmp, this_rq, dst_array, this_cpu); 2118 pull_task(busiest, array, tmp, this_rq, dst_array, this_cpu);
1928 pulled++; 2119 pulled++;
2120 rem_load_move -= tmp->load_weight;
1929 2121
1930 /* We only want to steal up to the prescribed number of tasks. */ 2122 /*
1931 if (pulled < max_nr_move) { 2123 * We only want to steal up to the prescribed number of tasks
2124 * and the prescribed amount of weighted load.
2125 */
2126 if (pulled < max_nr_move && rem_load_move > 0) {
2127 if (idx < this_best_prio)
2128 this_best_prio = idx;
1932 if (curr != head) 2129 if (curr != head)
1933 goto skip_queue; 2130 goto skip_queue;
1934 idx++; 2131 idx++;
@@ -1949,7 +2146,7 @@ out:
1949 2146
1950/* 2147/*
1951 * find_busiest_group finds and returns the busiest CPU group within the 2148 * find_busiest_group finds and returns the busiest CPU group within the
1952 * domain. It calculates and returns the number of tasks which should be 2149 * domain. It calculates and returns the amount of weighted load which should be
1953 * moved to restore balance via the imbalance parameter. 2150 * moved to restore balance via the imbalance parameter.
1954 */ 2151 */
1955static struct sched_group * 2152static struct sched_group *
@@ -1959,9 +2156,19 @@ find_busiest_group(struct sched_domain *sd, int this_cpu,
1959 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups; 2156 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
1960 unsigned long max_load, avg_load, total_load, this_load, total_pwr; 2157 unsigned long max_load, avg_load, total_load, this_load, total_pwr;
1961 unsigned long max_pull; 2158 unsigned long max_pull;
2159 unsigned long busiest_load_per_task, busiest_nr_running;
2160 unsigned long this_load_per_task, this_nr_running;
1962 int load_idx; 2161 int load_idx;
2162#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
2163 int power_savings_balance = 1;
2164 unsigned long leader_nr_running = 0, min_load_per_task = 0;
2165 unsigned long min_nr_running = ULONG_MAX;
2166 struct sched_group *group_min = NULL, *group_leader = NULL;
2167#endif
1963 2168
1964 max_load = this_load = total_load = total_pwr = 0; 2169 max_load = this_load = total_load = total_pwr = 0;
2170 busiest_load_per_task = busiest_nr_running = 0;
2171 this_load_per_task = this_nr_running = 0;
1965 if (idle == NOT_IDLE) 2172 if (idle == NOT_IDLE)
1966 load_idx = sd->busy_idx; 2173 load_idx = sd->busy_idx;
1967 else if (idle == NEWLY_IDLE) 2174 else if (idle == NEWLY_IDLE)
@@ -1970,16 +2177,19 @@ find_busiest_group(struct sched_domain *sd, int this_cpu,
1970 load_idx = sd->idle_idx; 2177 load_idx = sd->idle_idx;
1971 2178
1972 do { 2179 do {
1973 unsigned long load; 2180 unsigned long load, group_capacity;
1974 int local_group; 2181 int local_group;
1975 int i; 2182 int i;
2183 unsigned long sum_nr_running, sum_weighted_load;
1976 2184
1977 local_group = cpu_isset(this_cpu, group->cpumask); 2185 local_group = cpu_isset(this_cpu, group->cpumask);
1978 2186
1979 /* Tally up the load of all CPUs in the group */ 2187 /* Tally up the load of all CPUs in the group */
1980 avg_load = 0; 2188 sum_weighted_load = sum_nr_running = avg_load = 0;
1981 2189
1982 for_each_cpu_mask(i, group->cpumask) { 2190 for_each_cpu_mask(i, group->cpumask) {
2191 runqueue_t *rq = cpu_rq(i);
2192
1983 if (*sd_idle && !idle_cpu(i)) 2193 if (*sd_idle && !idle_cpu(i))
1984 *sd_idle = 0; 2194 *sd_idle = 0;
1985 2195
@@ -1990,6 +2200,8 @@ find_busiest_group(struct sched_domain *sd, int this_cpu,
1990 load = source_load(i, load_idx); 2200 load = source_load(i, load_idx);
1991 2201
1992 avg_load += load; 2202 avg_load += load;
2203 sum_nr_running += rq->nr_running;
2204 sum_weighted_load += rq->raw_weighted_load;
1993 } 2205 }
1994 2206
1995 total_load += avg_load; 2207 total_load += avg_load;
@@ -1998,17 +2210,80 @@ find_busiest_group(struct sched_domain *sd, int this_cpu,
1998 /* Adjust by relative CPU power of the group */ 2210 /* Adjust by relative CPU power of the group */
1999 avg_load = (avg_load * SCHED_LOAD_SCALE) / group->cpu_power; 2211 avg_load = (avg_load * SCHED_LOAD_SCALE) / group->cpu_power;
2000 2212
2213 group_capacity = group->cpu_power / SCHED_LOAD_SCALE;
2214
2001 if (local_group) { 2215 if (local_group) {
2002 this_load = avg_load; 2216 this_load = avg_load;
2003 this = group; 2217 this = group;
2004 } else if (avg_load > max_load) { 2218 this_nr_running = sum_nr_running;
2219 this_load_per_task = sum_weighted_load;
2220 } else if (avg_load > max_load &&
2221 sum_nr_running > group_capacity) {
2005 max_load = avg_load; 2222 max_load = avg_load;
2006 busiest = group; 2223 busiest = group;
2224 busiest_nr_running = sum_nr_running;
2225 busiest_load_per_task = sum_weighted_load;
2007 } 2226 }
2227
2228#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
2229 /*
2230 * Busy processors will not participate in power savings
2231 * balance.
2232 */
2233 if (idle == NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
2234 goto group_next;
2235
2236 /*
2237 * If the local group is idle or completely loaded
2238 * no need to do power savings balance at this domain
2239 */
2240 if (local_group && (this_nr_running >= group_capacity ||
2241 !this_nr_running))
2242 power_savings_balance = 0;
2243
2244 /*
2245 * If a group is already running at full capacity or idle,
2246 * don't include that group in power savings calculations
2247 */
2248 if (!power_savings_balance || sum_nr_running >= group_capacity
2249 || !sum_nr_running)
2250 goto group_next;
2251
2252 /*
2253 * Calculate the group which has the least non-idle load.
2254 * This is the group from where we need to pick up the load
2255 * for saving power
2256 */
2257 if ((sum_nr_running < min_nr_running) ||
2258 (sum_nr_running == min_nr_running &&
2259 first_cpu(group->cpumask) <
2260 first_cpu(group_min->cpumask))) {
2261 group_min = group;
2262 min_nr_running = sum_nr_running;
2263 min_load_per_task = sum_weighted_load /
2264 sum_nr_running;
2265 }
2266
2267 /*
2268 * Calculate the group which is almost near its
2269 * capacity but still has some space to pick up some load
2270 * from other group and save more power
2271 */
2272 if (sum_nr_running <= group_capacity - 1)
2273 if (sum_nr_running > leader_nr_running ||
2274 (sum_nr_running == leader_nr_running &&
2275 first_cpu(group->cpumask) >
2276 first_cpu(group_leader->cpumask))) {
2277 group_leader = group;
2278 leader_nr_running = sum_nr_running;
2279 }
2280
2281group_next:
2282#endif
2008 group = group->next; 2283 group = group->next;
2009 } while (group != sd->groups); 2284 } while (group != sd->groups);
2010 2285
2011 if (!busiest || this_load >= max_load || max_load <= SCHED_LOAD_SCALE) 2286 if (!busiest || this_load >= max_load || busiest_nr_running == 0)
2012 goto out_balanced; 2287 goto out_balanced;
2013 2288
2014 avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr; 2289 avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
@@ -2017,6 +2292,7 @@ find_busiest_group(struct sched_domain *sd, int this_cpu,
2017 100*max_load <= sd->imbalance_pct*this_load) 2292 100*max_load <= sd->imbalance_pct*this_load)
2018 goto out_balanced; 2293 goto out_balanced;
2019 2294
2295 busiest_load_per_task /= busiest_nr_running;
2020 /* 2296 /*
2021 * We're trying to get all the cpus to the average_load, so we don't 2297 * We're trying to get all the cpus to the average_load, so we don't
2022 * want to push ourselves above the average load, nor do we wish to 2298 * want to push ourselves above the average load, nor do we wish to
@@ -2028,21 +2304,50 @@ find_busiest_group(struct sched_domain *sd, int this_cpu,
2028 * by pulling tasks to us. Be careful of negative numbers as they'll 2304 * by pulling tasks to us. Be careful of negative numbers as they'll
2029 * appear as very large values with unsigned longs. 2305 * appear as very large values with unsigned longs.
2030 */ 2306 */
2307 if (max_load <= busiest_load_per_task)
2308 goto out_balanced;
2309
2310 /*
2311 * In the presence of smp nice balancing, certain scenarios can have
2312 * max load less than avg load(as we skip the groups at or below
2313 * its cpu_power, while calculating max_load..)
2314 */
2315 if (max_load < avg_load) {
2316 *imbalance = 0;
2317 goto small_imbalance;
2318 }
2031 2319
2032 /* Don't want to pull so many tasks that a group would go idle */ 2320 /* Don't want to pull so many tasks that a group would go idle */
2033 max_pull = min(max_load - avg_load, max_load - SCHED_LOAD_SCALE); 2321 max_pull = min(max_load - avg_load, max_load - busiest_load_per_task);
2034 2322
2035 /* How much load to actually move to equalise the imbalance */ 2323 /* How much load to actually move to equalise the imbalance */
2036 *imbalance = min(max_pull * busiest->cpu_power, 2324 *imbalance = min(max_pull * busiest->cpu_power,
2037 (avg_load - this_load) * this->cpu_power) 2325 (avg_load - this_load) * this->cpu_power)
2038 / SCHED_LOAD_SCALE; 2326 / SCHED_LOAD_SCALE;
2039 2327
2040 if (*imbalance < SCHED_LOAD_SCALE) { 2328 /*
2041 unsigned long pwr_now = 0, pwr_move = 0; 2329 * if *imbalance is less than the average load per runnable task
2330 * there is no gaurantee that any tasks will be moved so we'll have
2331 * a think about bumping its value to force at least one task to be
2332 * moved
2333 */
2334 if (*imbalance < busiest_load_per_task) {
2335 unsigned long pwr_now, pwr_move;
2042 unsigned long tmp; 2336 unsigned long tmp;
2337 unsigned int imbn;
2338
2339small_imbalance:
2340 pwr_move = pwr_now = 0;
2341 imbn = 2;
2342 if (this_nr_running) {
2343 this_load_per_task /= this_nr_running;
2344 if (busiest_load_per_task > this_load_per_task)
2345 imbn = 1;
2346 } else
2347 this_load_per_task = SCHED_LOAD_SCALE;
2043 2348
2044 if (max_load - this_load >= SCHED_LOAD_SCALE*2) { 2349 if (max_load - this_load >= busiest_load_per_task * imbn) {
2045 *imbalance = 1; 2350 *imbalance = busiest_load_per_task;
2046 return busiest; 2351 return busiest;
2047 } 2352 }
2048 2353
@@ -2052,39 +2357,47 @@ find_busiest_group(struct sched_domain *sd, int this_cpu,
2052 * moving them. 2357 * moving them.
2053 */ 2358 */
2054 2359
2055 pwr_now += busiest->cpu_power*min(SCHED_LOAD_SCALE, max_load); 2360 pwr_now += busiest->cpu_power *
2056 pwr_now += this->cpu_power*min(SCHED_LOAD_SCALE, this_load); 2361 min(busiest_load_per_task, max_load);
2362 pwr_now += this->cpu_power *
2363 min(this_load_per_task, this_load);
2057 pwr_now /= SCHED_LOAD_SCALE; 2364 pwr_now /= SCHED_LOAD_SCALE;
2058 2365
2059 /* Amount of load we'd subtract */ 2366 /* Amount of load we'd subtract */
2060 tmp = SCHED_LOAD_SCALE*SCHED_LOAD_SCALE/busiest->cpu_power; 2367 tmp = busiest_load_per_task*SCHED_LOAD_SCALE/busiest->cpu_power;
2061 if (max_load > tmp) 2368 if (max_load > tmp)
2062 pwr_move += busiest->cpu_power*min(SCHED_LOAD_SCALE, 2369 pwr_move += busiest->cpu_power *
2063 max_load - tmp); 2370 min(busiest_load_per_task, max_load - tmp);
2064 2371
2065 /* Amount of load we'd add */ 2372 /* Amount of load we'd add */
2066 if (max_load*busiest->cpu_power < 2373 if (max_load*busiest->cpu_power <
2067 SCHED_LOAD_SCALE*SCHED_LOAD_SCALE) 2374 busiest_load_per_task*SCHED_LOAD_SCALE)
2068 tmp = max_load*busiest->cpu_power/this->cpu_power; 2375 tmp = max_load*busiest->cpu_power/this->cpu_power;
2069 else 2376 else
2070 tmp = SCHED_LOAD_SCALE*SCHED_LOAD_SCALE/this->cpu_power; 2377 tmp = busiest_load_per_task*SCHED_LOAD_SCALE/this->cpu_power;
2071 pwr_move += this->cpu_power*min(SCHED_LOAD_SCALE, this_load + tmp); 2378 pwr_move += this->cpu_power*min(this_load_per_task, this_load + tmp);
2072 pwr_move /= SCHED_LOAD_SCALE; 2379 pwr_move /= SCHED_LOAD_SCALE;
2073 2380
2074 /* Move if we gain throughput */ 2381 /* Move if we gain throughput */
2075 if (pwr_move <= pwr_now) 2382 if (pwr_move <= pwr_now)
2076 goto out_balanced; 2383 goto out_balanced;
2077 2384
2078 *imbalance = 1; 2385 *imbalance = busiest_load_per_task;
2079 return busiest;
2080 } 2386 }
2081 2387
2082 /* Get rid of the scaling factor, rounding down as we divide */
2083 *imbalance = *imbalance / SCHED_LOAD_SCALE;
2084 return busiest; 2388 return busiest;
2085 2389
2086out_balanced: 2390out_balanced:
2391#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
2392 if (idle == NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
2393 goto ret;
2087 2394
2395 if (this == group_leader && group_leader != group_min) {
2396 *imbalance = min_load_per_task;
2397 return group_min;
2398 }
2399ret:
2400#endif
2088 *imbalance = 0; 2401 *imbalance = 0;
2089 return NULL; 2402 return NULL;
2090} 2403}
@@ -2093,18 +2406,21 @@ out_balanced:
2093 * find_busiest_queue - find the busiest runqueue among the cpus in group. 2406 * find_busiest_queue - find the busiest runqueue among the cpus in group.
2094 */ 2407 */
2095static runqueue_t *find_busiest_queue(struct sched_group *group, 2408static runqueue_t *find_busiest_queue(struct sched_group *group,
2096 enum idle_type idle) 2409 enum idle_type idle, unsigned long imbalance)
2097{ 2410{
2098 unsigned long load, max_load = 0; 2411 unsigned long max_load = 0;
2099 runqueue_t *busiest = NULL; 2412 runqueue_t *busiest = NULL, *rqi;
2100 int i; 2413 int i;
2101 2414
2102 for_each_cpu_mask(i, group->cpumask) { 2415 for_each_cpu_mask(i, group->cpumask) {
2103 load = source_load(i, 0); 2416 rqi = cpu_rq(i);
2417
2418 if (rqi->nr_running == 1 && rqi->raw_weighted_load > imbalance)
2419 continue;
2104 2420
2105 if (load > max_load) { 2421 if (rqi->raw_weighted_load > max_load) {
2106 max_load = load; 2422 max_load = rqi->raw_weighted_load;
2107 busiest = cpu_rq(i); 2423 busiest = rqi;
2108 } 2424 }
2109 } 2425 }
2110 2426
@@ -2117,6 +2433,7 @@ static runqueue_t *find_busiest_queue(struct sched_group *group,
2117 */ 2433 */
2118#define MAX_PINNED_INTERVAL 512 2434#define MAX_PINNED_INTERVAL 512
2119 2435
2436#define minus_1_or_zero(n) ((n) > 0 ? (n) - 1 : 0)
2120/* 2437/*
2121 * Check this_cpu to ensure it is balanced within domain. Attempt to move 2438 * Check this_cpu to ensure it is balanced within domain. Attempt to move
2122 * tasks if there is an imbalance. 2439 * tasks if there is an imbalance.
@@ -2133,7 +2450,8 @@ static int load_balance(int this_cpu, runqueue_t *this_rq,
2133 int active_balance = 0; 2450 int active_balance = 0;
2134 int sd_idle = 0; 2451 int sd_idle = 0;
2135 2452
2136 if (idle != NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER) 2453 if (idle != NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
2454 !sched_smt_power_savings)
2137 sd_idle = 1; 2455 sd_idle = 1;
2138 2456
2139 schedstat_inc(sd, lb_cnt[idle]); 2457 schedstat_inc(sd, lb_cnt[idle]);
@@ -2144,7 +2462,7 @@ static int load_balance(int this_cpu, runqueue_t *this_rq,
2144 goto out_balanced; 2462 goto out_balanced;
2145 } 2463 }
2146 2464
2147 busiest = find_busiest_queue(group, idle); 2465 busiest = find_busiest_queue(group, idle, imbalance);
2148 if (!busiest) { 2466 if (!busiest) {
2149 schedstat_inc(sd, lb_nobusyq[idle]); 2467 schedstat_inc(sd, lb_nobusyq[idle]);
2150 goto out_balanced; 2468 goto out_balanced;
@@ -2164,6 +2482,7 @@ static int load_balance(int this_cpu, runqueue_t *this_rq,
2164 */ 2482 */
2165 double_rq_lock(this_rq, busiest); 2483 double_rq_lock(this_rq, busiest);
2166 nr_moved = move_tasks(this_rq, this_cpu, busiest, 2484 nr_moved = move_tasks(this_rq, this_cpu, busiest,
2485 minus_1_or_zero(busiest->nr_running),
2167 imbalance, sd, idle, &all_pinned); 2486 imbalance, sd, idle, &all_pinned);
2168 double_rq_unlock(this_rq, busiest); 2487 double_rq_unlock(this_rq, busiest);
2169 2488
@@ -2221,7 +2540,8 @@ static int load_balance(int this_cpu, runqueue_t *this_rq,
2221 sd->balance_interval *= 2; 2540 sd->balance_interval *= 2;
2222 } 2541 }
2223 2542
2224 if (!nr_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER) 2543 if (!nr_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
2544 !sched_smt_power_savings)
2225 return -1; 2545 return -1;
2226 return nr_moved; 2546 return nr_moved;
2227 2547
@@ -2236,7 +2556,7 @@ out_one_pinned:
2236 (sd->balance_interval < sd->max_interval)) 2556 (sd->balance_interval < sd->max_interval))
2237 sd->balance_interval *= 2; 2557 sd->balance_interval *= 2;
2238 2558
2239 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER) 2559 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER && !sched_smt_power_savings)
2240 return -1; 2560 return -1;
2241 return 0; 2561 return 0;
2242} 2562}
@@ -2257,7 +2577,7 @@ static int load_balance_newidle(int this_cpu, runqueue_t *this_rq,
2257 int nr_moved = 0; 2577 int nr_moved = 0;
2258 int sd_idle = 0; 2578 int sd_idle = 0;
2259 2579
2260 if (sd->flags & SD_SHARE_CPUPOWER) 2580 if (sd->flags & SD_SHARE_CPUPOWER && !sched_smt_power_savings)
2261 sd_idle = 1; 2581 sd_idle = 1;
2262 2582
2263 schedstat_inc(sd, lb_cnt[NEWLY_IDLE]); 2583 schedstat_inc(sd, lb_cnt[NEWLY_IDLE]);
@@ -2267,7 +2587,7 @@ static int load_balance_newidle(int this_cpu, runqueue_t *this_rq,
2267 goto out_balanced; 2587 goto out_balanced;
2268 } 2588 }
2269 2589
2270 busiest = find_busiest_queue(group, NEWLY_IDLE); 2590 busiest = find_busiest_queue(group, NEWLY_IDLE, imbalance);
2271 if (!busiest) { 2591 if (!busiest) {
2272 schedstat_inc(sd, lb_nobusyq[NEWLY_IDLE]); 2592 schedstat_inc(sd, lb_nobusyq[NEWLY_IDLE]);
2273 goto out_balanced; 2593 goto out_balanced;
@@ -2282,6 +2602,7 @@ static int load_balance_newidle(int this_cpu, runqueue_t *this_rq,
2282 /* Attempt to move tasks */ 2602 /* Attempt to move tasks */
2283 double_lock_balance(this_rq, busiest); 2603 double_lock_balance(this_rq, busiest);
2284 nr_moved = move_tasks(this_rq, this_cpu, busiest, 2604 nr_moved = move_tasks(this_rq, this_cpu, busiest,
2605 minus_1_or_zero(busiest->nr_running),
2285 imbalance, sd, NEWLY_IDLE, NULL); 2606 imbalance, sd, NEWLY_IDLE, NULL);
2286 spin_unlock(&busiest->lock); 2607 spin_unlock(&busiest->lock);
2287 } 2608 }
@@ -2297,7 +2618,7 @@ static int load_balance_newidle(int this_cpu, runqueue_t *this_rq,
2297 2618
2298out_balanced: 2619out_balanced:
2299 schedstat_inc(sd, lb_balanced[NEWLY_IDLE]); 2620 schedstat_inc(sd, lb_balanced[NEWLY_IDLE]);
2300 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER) 2621 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER && !sched_smt_power_savings)
2301 return -1; 2622 return -1;
2302 sd->nr_balance_failed = 0; 2623 sd->nr_balance_failed = 0;
2303 return 0; 2624 return 0;
@@ -2352,17 +2673,19 @@ static void active_load_balance(runqueue_t *busiest_rq, int busiest_cpu)
2352 double_lock_balance(busiest_rq, target_rq); 2673 double_lock_balance(busiest_rq, target_rq);
2353 2674
2354 /* Search for an sd spanning us and the target CPU. */ 2675 /* Search for an sd spanning us and the target CPU. */
2355 for_each_domain(target_cpu, sd) 2676 for_each_domain(target_cpu, sd) {
2356 if ((sd->flags & SD_LOAD_BALANCE) && 2677 if ((sd->flags & SD_LOAD_BALANCE) &&
2357 cpu_isset(busiest_cpu, sd->span)) 2678 cpu_isset(busiest_cpu, sd->span))
2358 break; 2679 break;
2680 }
2359 2681
2360 if (unlikely(sd == NULL)) 2682 if (unlikely(sd == NULL))
2361 goto out; 2683 goto out;
2362 2684
2363 schedstat_inc(sd, alb_cnt); 2685 schedstat_inc(sd, alb_cnt);
2364 2686
2365 if (move_tasks(target_rq, target_cpu, busiest_rq, 1, sd, SCHED_IDLE, NULL)) 2687 if (move_tasks(target_rq, target_cpu, busiest_rq, 1,
2688 RTPRIO_TO_LOAD_WEIGHT(100), sd, SCHED_IDLE, NULL))
2366 schedstat_inc(sd, alb_pushed); 2689 schedstat_inc(sd, alb_pushed);
2367 else 2690 else
2368 schedstat_inc(sd, alb_failed); 2691 schedstat_inc(sd, alb_failed);
@@ -2390,7 +2713,7 @@ static void rebalance_tick(int this_cpu, runqueue_t *this_rq,
2390 struct sched_domain *sd; 2713 struct sched_domain *sd;
2391 int i; 2714 int i;
2392 2715
2393 this_load = this_rq->nr_running * SCHED_LOAD_SCALE; 2716 this_load = this_rq->raw_weighted_load;
2394 /* Update our load */ 2717 /* Update our load */
2395 for (i = 0; i < 3; i++) { 2718 for (i = 0; i < 3; i++) {
2396 unsigned long new_load = this_load; 2719 unsigned long new_load = this_load;
@@ -2691,48 +3014,35 @@ static inline void wakeup_busy_runqueue(runqueue_t *rq)
2691 resched_task(rq->idle); 3014 resched_task(rq->idle);
2692} 3015}
2693 3016
2694static void wake_sleeping_dependent(int this_cpu, runqueue_t *this_rq) 3017/*
3018 * Called with interrupt disabled and this_rq's runqueue locked.
3019 */
3020static void wake_sleeping_dependent(int this_cpu)
2695{ 3021{
2696 struct sched_domain *tmp, *sd = NULL; 3022 struct sched_domain *tmp, *sd = NULL;
2697 cpumask_t sibling_map;
2698 int i; 3023 int i;
2699 3024
2700 for_each_domain(this_cpu, tmp) 3025 for_each_domain(this_cpu, tmp) {
2701 if (tmp->flags & SD_SHARE_CPUPOWER) 3026 if (tmp->flags & SD_SHARE_CPUPOWER) {
2702 sd = tmp; 3027 sd = tmp;
3028 break;
3029 }
3030 }
2703 3031
2704 if (!sd) 3032 if (!sd)
2705 return; 3033 return;
2706 3034
2707 /* 3035 for_each_cpu_mask(i, sd->span) {
2708 * Unlock the current runqueue because we have to lock in
2709 * CPU order to avoid deadlocks. Caller knows that we might
2710 * unlock. We keep IRQs disabled.
2711 */
2712 spin_unlock(&this_rq->lock);
2713
2714 sibling_map = sd->span;
2715
2716 for_each_cpu_mask(i, sibling_map)
2717 spin_lock(&cpu_rq(i)->lock);
2718 /*
2719 * We clear this CPU from the mask. This both simplifies the
2720 * inner loop and keps this_rq locked when we exit:
2721 */
2722 cpu_clear(this_cpu, sibling_map);
2723
2724 for_each_cpu_mask(i, sibling_map) {
2725 runqueue_t *smt_rq = cpu_rq(i); 3036 runqueue_t *smt_rq = cpu_rq(i);
2726 3037
3038 if (i == this_cpu)
3039 continue;
3040 if (unlikely(!spin_trylock(&smt_rq->lock)))
3041 continue;
3042
2727 wakeup_busy_runqueue(smt_rq); 3043 wakeup_busy_runqueue(smt_rq);
3044 spin_unlock(&smt_rq->lock);
2728 } 3045 }
2729
2730 for_each_cpu_mask(i, sibling_map)
2731 spin_unlock(&cpu_rq(i)->lock);
2732 /*
2733 * We exit with this_cpu's rq still held and IRQs
2734 * still disabled:
2735 */
2736} 3046}
2737 3047
2738/* 3048/*
@@ -2745,52 +3055,46 @@ static inline unsigned long smt_slice(task_t *p, struct sched_domain *sd)
2745 return p->time_slice * (100 - sd->per_cpu_gain) / 100; 3055 return p->time_slice * (100 - sd->per_cpu_gain) / 100;
2746} 3056}
2747 3057
2748static int dependent_sleeper(int this_cpu, runqueue_t *this_rq) 3058/*
3059 * To minimise lock contention and not have to drop this_rq's runlock we only
3060 * trylock the sibling runqueues and bypass those runqueues if we fail to
3061 * acquire their lock. As we only trylock the normal locking order does not
3062 * need to be obeyed.
3063 */
3064static int dependent_sleeper(int this_cpu, runqueue_t *this_rq, task_t *p)
2749{ 3065{
2750 struct sched_domain *tmp, *sd = NULL; 3066 struct sched_domain *tmp, *sd = NULL;
2751 cpumask_t sibling_map;
2752 prio_array_t *array;
2753 int ret = 0, i; 3067 int ret = 0, i;
2754 task_t *p;
2755 3068
2756 for_each_domain(this_cpu, tmp) 3069 /* kernel/rt threads do not participate in dependent sleeping */
2757 if (tmp->flags & SD_SHARE_CPUPOWER) 3070 if (!p->mm || rt_task(p))
3071 return 0;
3072
3073 for_each_domain(this_cpu, tmp) {
3074 if (tmp->flags & SD_SHARE_CPUPOWER) {
2758 sd = tmp; 3075 sd = tmp;
3076 break;
3077 }
3078 }
2759 3079
2760 if (!sd) 3080 if (!sd)
2761 return 0; 3081 return 0;
2762 3082
2763 /* 3083 for_each_cpu_mask(i, sd->span) {
2764 * The same locking rules and details apply as for 3084 runqueue_t *smt_rq;
2765 * wake_sleeping_dependent(): 3085 task_t *smt_curr;
2766 */
2767 spin_unlock(&this_rq->lock);
2768 sibling_map = sd->span;
2769 for_each_cpu_mask(i, sibling_map)
2770 spin_lock(&cpu_rq(i)->lock);
2771 cpu_clear(this_cpu, sibling_map);
2772 3086
2773 /* 3087 if (i == this_cpu)
2774 * Establish next task to be run - it might have gone away because 3088 continue;
2775 * we released the runqueue lock above:
2776 */
2777 if (!this_rq->nr_running)
2778 goto out_unlock;
2779 array = this_rq->active;
2780 if (!array->nr_active)
2781 array = this_rq->expired;
2782 BUG_ON(!array->nr_active);
2783 3089
2784 p = list_entry(array->queue[sched_find_first_bit(array->bitmap)].next, 3090 smt_rq = cpu_rq(i);
2785 task_t, run_list); 3091 if (unlikely(!spin_trylock(&smt_rq->lock)))
3092 continue;
2786 3093
2787 for_each_cpu_mask(i, sibling_map) { 3094 smt_curr = smt_rq->curr;
2788 runqueue_t *smt_rq = cpu_rq(i);
2789 task_t *smt_curr = smt_rq->curr;
2790 3095
2791 /* Kernel threads do not participate in dependent sleeping */ 3096 if (!smt_curr->mm)
2792 if (!p->mm || !smt_curr->mm || rt_task(p)) 3097 goto unlock;
2793 goto check_smt_task;
2794 3098
2795 /* 3099 /*
2796 * If a user task with lower static priority than the 3100 * If a user task with lower static priority than the
@@ -2808,49 +3112,24 @@ static int dependent_sleeper(int this_cpu, runqueue_t *this_rq)
2808 if ((jiffies % DEF_TIMESLICE) > 3112 if ((jiffies % DEF_TIMESLICE) >
2809 (sd->per_cpu_gain * DEF_TIMESLICE / 100)) 3113 (sd->per_cpu_gain * DEF_TIMESLICE / 100))
2810 ret = 1; 3114 ret = 1;
2811 } else 3115 } else {
2812 if (smt_curr->static_prio < p->static_prio && 3116 if (smt_curr->static_prio < p->static_prio &&
2813 !TASK_PREEMPTS_CURR(p, smt_rq) && 3117 !TASK_PREEMPTS_CURR(p, smt_rq) &&
2814 smt_slice(smt_curr, sd) > task_timeslice(p)) 3118 smt_slice(smt_curr, sd) > task_timeslice(p))
2815 ret = 1; 3119 ret = 1;
2816
2817check_smt_task:
2818 if ((!smt_curr->mm && smt_curr != smt_rq->idle) ||
2819 rt_task(smt_curr))
2820 continue;
2821 if (!p->mm) {
2822 wakeup_busy_runqueue(smt_rq);
2823 continue;
2824 }
2825
2826 /*
2827 * Reschedule a lower priority task on the SMT sibling for
2828 * it to be put to sleep, or wake it up if it has been put to
2829 * sleep for priority reasons to see if it should run now.
2830 */
2831 if (rt_task(p)) {
2832 if ((jiffies % DEF_TIMESLICE) >
2833 (sd->per_cpu_gain * DEF_TIMESLICE / 100))
2834 resched_task(smt_curr);
2835 } else {
2836 if (TASK_PREEMPTS_CURR(p, smt_rq) &&
2837 smt_slice(p, sd) > task_timeslice(smt_curr))
2838 resched_task(smt_curr);
2839 else
2840 wakeup_busy_runqueue(smt_rq);
2841 } 3120 }
3121unlock:
3122 spin_unlock(&smt_rq->lock);
2842 } 3123 }
2843out_unlock:
2844 for_each_cpu_mask(i, sibling_map)
2845 spin_unlock(&cpu_rq(i)->lock);
2846 return ret; 3124 return ret;
2847} 3125}
2848#else 3126#else
2849static inline void wake_sleeping_dependent(int this_cpu, runqueue_t *this_rq) 3127static inline void wake_sleeping_dependent(int this_cpu)
2850{ 3128{
2851} 3129}
2852 3130
2853static inline int dependent_sleeper(int this_cpu, runqueue_t *this_rq) 3131static inline int dependent_sleeper(int this_cpu, runqueue_t *this_rq,
3132 task_t *p)
2854{ 3133{
2855 return 0; 3134 return 0;
2856} 3135}
@@ -2972,32 +3251,13 @@ need_resched_nonpreemptible:
2972 3251
2973 cpu = smp_processor_id(); 3252 cpu = smp_processor_id();
2974 if (unlikely(!rq->nr_running)) { 3253 if (unlikely(!rq->nr_running)) {
2975go_idle:
2976 idle_balance(cpu, rq); 3254 idle_balance(cpu, rq);
2977 if (!rq->nr_running) { 3255 if (!rq->nr_running) {
2978 next = rq->idle; 3256 next = rq->idle;
2979 rq->expired_timestamp = 0; 3257 rq->expired_timestamp = 0;
2980 wake_sleeping_dependent(cpu, rq); 3258 wake_sleeping_dependent(cpu);
2981 /*
2982 * wake_sleeping_dependent() might have released
2983 * the runqueue, so break out if we got new
2984 * tasks meanwhile:
2985 */
2986 if (!rq->nr_running)
2987 goto switch_tasks;
2988 }
2989 } else {
2990 if (dependent_sleeper(cpu, rq)) {
2991 next = rq->idle;
2992 goto switch_tasks; 3259 goto switch_tasks;
2993 } 3260 }
2994 /*
2995 * dependent_sleeper() releases and reacquires the runqueue
2996 * lock, hence go into the idle loop if the rq went
2997 * empty meanwhile:
2998 */
2999 if (unlikely(!rq->nr_running))
3000 goto go_idle;
3001 } 3261 }
3002 3262
3003 array = rq->active; 3263 array = rq->active;
@@ -3035,6 +3295,8 @@ go_idle:
3035 } 3295 }
3036 } 3296 }
3037 next->sleep_type = SLEEP_NORMAL; 3297 next->sleep_type = SLEEP_NORMAL;
3298 if (dependent_sleeper(cpu, rq, next))
3299 next = rq->idle;
3038switch_tasks: 3300switch_tasks:
3039 if (next == rq->idle) 3301 if (next == rq->idle)
3040 schedstat_inc(rq, sched_goidle); 3302 schedstat_inc(rq, sched_goidle);
@@ -3478,12 +3740,65 @@ long fastcall __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
3478 3740
3479EXPORT_SYMBOL(sleep_on_timeout); 3741EXPORT_SYMBOL(sleep_on_timeout);
3480 3742
3743#ifdef CONFIG_RT_MUTEXES
3744
3745/*
3746 * rt_mutex_setprio - set the current priority of a task
3747 * @p: task
3748 * @prio: prio value (kernel-internal form)
3749 *
3750 * This function changes the 'effective' priority of a task. It does
3751 * not touch ->normal_prio like __setscheduler().
3752 *
3753 * Used by the rt_mutex code to implement priority inheritance logic.
3754 */
3755void rt_mutex_setprio(task_t *p, int prio)
3756{
3757 unsigned long flags;
3758 prio_array_t *array;
3759 runqueue_t *rq;
3760 int oldprio;
3761
3762 BUG_ON(prio < 0 || prio > MAX_PRIO);
3763
3764 rq = task_rq_lock(p, &flags);
3765
3766 oldprio = p->prio;
3767 array = p->array;
3768 if (array)
3769 dequeue_task(p, array);
3770 p->prio = prio;
3771
3772 if (array) {
3773 /*
3774 * If changing to an RT priority then queue it
3775 * in the active array!
3776 */
3777 if (rt_task(p))
3778 array = rq->active;
3779 enqueue_task(p, array);
3780 /*
3781 * Reschedule if we are currently running on this runqueue and
3782 * our priority decreased, or if we are not currently running on
3783 * this runqueue and our priority is higher than the current's
3784 */
3785 if (task_running(rq, p)) {
3786 if (p->prio > oldprio)
3787 resched_task(rq->curr);
3788 } else if (TASK_PREEMPTS_CURR(p, rq))
3789 resched_task(rq->curr);
3790 }
3791 task_rq_unlock(rq, &flags);
3792}
3793
3794#endif
3795
3481void set_user_nice(task_t *p, long nice) 3796void set_user_nice(task_t *p, long nice)
3482{ 3797{
3483 unsigned long flags; 3798 unsigned long flags;
3484 prio_array_t *array; 3799 prio_array_t *array;
3485 runqueue_t *rq; 3800 runqueue_t *rq;
3486 int old_prio, new_prio, delta; 3801 int old_prio, delta;
3487 3802
3488 if (TASK_NICE(p) == nice || nice < -20 || nice > 19) 3803 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
3489 return; 3804 return;
@@ -3498,22 +3813,25 @@ void set_user_nice(task_t *p, long nice)
3498 * it wont have any effect on scheduling until the task is 3813 * it wont have any effect on scheduling until the task is
3499 * not SCHED_NORMAL/SCHED_BATCH: 3814 * not SCHED_NORMAL/SCHED_BATCH:
3500 */ 3815 */
3501 if (rt_task(p)) { 3816 if (has_rt_policy(p)) {
3502 p->static_prio = NICE_TO_PRIO(nice); 3817 p->static_prio = NICE_TO_PRIO(nice);
3503 goto out_unlock; 3818 goto out_unlock;
3504 } 3819 }
3505 array = p->array; 3820 array = p->array;
3506 if (array) 3821 if (array) {
3507 dequeue_task(p, array); 3822 dequeue_task(p, array);
3823 dec_raw_weighted_load(rq, p);
3824 }
3508 3825
3509 old_prio = p->prio;
3510 new_prio = NICE_TO_PRIO(nice);
3511 delta = new_prio - old_prio;
3512 p->static_prio = NICE_TO_PRIO(nice); 3826 p->static_prio = NICE_TO_PRIO(nice);
3513 p->prio += delta; 3827 set_load_weight(p);
3828 old_prio = p->prio;
3829 p->prio = effective_prio(p);
3830 delta = p->prio - old_prio;
3514 3831
3515 if (array) { 3832 if (array) {
3516 enqueue_task(p, array); 3833 enqueue_task(p, array);
3834 inc_raw_weighted_load(rq, p);
3517 /* 3835 /*
3518 * If the task increased its priority or is running and 3836 * If the task increased its priority or is running and
3519 * lowered its priority, then reschedule its CPU: 3837 * lowered its priority, then reschedule its CPU:
@@ -3524,7 +3842,6 @@ void set_user_nice(task_t *p, long nice)
3524out_unlock: 3842out_unlock:
3525 task_rq_unlock(rq, &flags); 3843 task_rq_unlock(rq, &flags);
3526} 3844}
3527
3528EXPORT_SYMBOL(set_user_nice); 3845EXPORT_SYMBOL(set_user_nice);
3529 3846
3530/* 3847/*
@@ -3639,16 +3956,15 @@ static void __setscheduler(struct task_struct *p, int policy, int prio)
3639 BUG_ON(p->array); 3956 BUG_ON(p->array);
3640 p->policy = policy; 3957 p->policy = policy;
3641 p->rt_priority = prio; 3958 p->rt_priority = prio;
3642 if (policy != SCHED_NORMAL && policy != SCHED_BATCH) { 3959 p->normal_prio = normal_prio(p);
3643 p->prio = MAX_RT_PRIO-1 - p->rt_priority; 3960 /* we are holding p->pi_lock already */
3644 } else { 3961 p->prio = rt_mutex_getprio(p);
3645 p->prio = p->static_prio; 3962 /*
3646 /* 3963 * SCHED_BATCH tasks are treated as perpetual CPU hogs:
3647 * SCHED_BATCH tasks are treated as perpetual CPU hogs: 3964 */
3648 */ 3965 if (policy == SCHED_BATCH)
3649 if (policy == SCHED_BATCH) 3966 p->sleep_avg = 0;
3650 p->sleep_avg = 0; 3967 set_load_weight(p);
3651 }
3652} 3968}
3653 3969
3654/** 3970/**
@@ -3667,6 +3983,8 @@ int sched_setscheduler(struct task_struct *p, int policy,
3667 unsigned long flags; 3983 unsigned long flags;
3668 runqueue_t *rq; 3984 runqueue_t *rq;
3669 3985
3986 /* may grab non-irq protected spin_locks */
3987 BUG_ON(in_interrupt());
3670recheck: 3988recheck:
3671 /* double check policy once rq lock held */ 3989 /* double check policy once rq lock held */
3672 if (policy < 0) 3990 if (policy < 0)
@@ -3715,14 +4033,20 @@ recheck:
3715 if (retval) 4033 if (retval)
3716 return retval; 4034 return retval;
3717 /* 4035 /*
4036 * make sure no PI-waiters arrive (or leave) while we are
4037 * changing the priority of the task:
4038 */
4039 spin_lock_irqsave(&p->pi_lock, flags);
4040 /*
3718 * To be able to change p->policy safely, the apropriate 4041 * To be able to change p->policy safely, the apropriate
3719 * runqueue lock must be held. 4042 * runqueue lock must be held.
3720 */ 4043 */
3721 rq = task_rq_lock(p, &flags); 4044 rq = __task_rq_lock(p);
3722 /* recheck policy now with rq lock held */ 4045 /* recheck policy now with rq lock held */
3723 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) { 4046 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
3724 policy = oldpolicy = -1; 4047 policy = oldpolicy = -1;
3725 task_rq_unlock(rq, &flags); 4048 __task_rq_unlock(rq);
4049 spin_unlock_irqrestore(&p->pi_lock, flags);
3726 goto recheck; 4050 goto recheck;
3727 } 4051 }
3728 array = p->array; 4052 array = p->array;
@@ -3743,7 +4067,11 @@ recheck:
3743 } else if (TASK_PREEMPTS_CURR(p, rq)) 4067 } else if (TASK_PREEMPTS_CURR(p, rq))
3744 resched_task(rq->curr); 4068 resched_task(rq->curr);
3745 } 4069 }
3746 task_rq_unlock(rq, &flags); 4070 __task_rq_unlock(rq);
4071 spin_unlock_irqrestore(&p->pi_lock, flags);
4072
4073 rt_mutex_adjust_pi(p);
4074
3747 return 0; 4075 return 0;
3748} 4076}
3749EXPORT_SYMBOL_GPL(sched_setscheduler); 4077EXPORT_SYMBOL_GPL(sched_setscheduler);
@@ -3765,8 +4093,10 @@ do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
3765 read_unlock_irq(&tasklist_lock); 4093 read_unlock_irq(&tasklist_lock);
3766 return -ESRCH; 4094 return -ESRCH;
3767 } 4095 }
3768 retval = sched_setscheduler(p, policy, &lparam); 4096 get_task_struct(p);
3769 read_unlock_irq(&tasklist_lock); 4097 read_unlock_irq(&tasklist_lock);
4098 retval = sched_setscheduler(p, policy, &lparam);
4099 put_task_struct(p);
3770 return retval; 4100 return retval;
3771} 4101}
3772 4102
@@ -4378,7 +4708,7 @@ void __devinit init_idle(task_t *idle, int cpu)
4378 idle->timestamp = sched_clock(); 4708 idle->timestamp = sched_clock();
4379 idle->sleep_avg = 0; 4709 idle->sleep_avg = 0;
4380 idle->array = NULL; 4710 idle->array = NULL;
4381 idle->prio = MAX_PRIO; 4711 idle->prio = idle->normal_prio = MAX_PRIO;
4382 idle->state = TASK_RUNNING; 4712 idle->state = TASK_RUNNING;
4383 idle->cpus_allowed = cpumask_of_cpu(cpu); 4713 idle->cpus_allowed = cpumask_of_cpu(cpu);
4384 set_task_cpu(idle, cpu); 4714 set_task_cpu(idle, cpu);
@@ -4474,13 +4804,16 @@ EXPORT_SYMBOL_GPL(set_cpus_allowed);
4474 * 4804 *
4475 * So we race with normal scheduler movements, but that's OK, as long 4805 * So we race with normal scheduler movements, but that's OK, as long
4476 * as the task is no longer on this CPU. 4806 * as the task is no longer on this CPU.
4807 *
4808 * Returns non-zero if task was successfully migrated.
4477 */ 4809 */
4478static void __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu) 4810static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
4479{ 4811{
4480 runqueue_t *rq_dest, *rq_src; 4812 runqueue_t *rq_dest, *rq_src;
4813 int ret = 0;
4481 4814
4482 if (unlikely(cpu_is_offline(dest_cpu))) 4815 if (unlikely(cpu_is_offline(dest_cpu)))
4483 return; 4816 return ret;
4484 4817
4485 rq_src = cpu_rq(src_cpu); 4818 rq_src = cpu_rq(src_cpu);
4486 rq_dest = cpu_rq(dest_cpu); 4819 rq_dest = cpu_rq(dest_cpu);
@@ -4508,9 +4841,10 @@ static void __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
4508 if (TASK_PREEMPTS_CURR(p, rq_dest)) 4841 if (TASK_PREEMPTS_CURR(p, rq_dest))
4509 resched_task(rq_dest->curr); 4842 resched_task(rq_dest->curr);
4510 } 4843 }
4511 4844 ret = 1;
4512out: 4845out:
4513 double_rq_unlock(rq_src, rq_dest); 4846 double_rq_unlock(rq_src, rq_dest);
4847 return ret;
4514} 4848}
4515 4849
4516/* 4850/*
@@ -4580,9 +4914,12 @@ wait_to_die:
4580/* Figure out where task on dead CPU should go, use force if neccessary. */ 4914/* Figure out where task on dead CPU should go, use force if neccessary. */
4581static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *tsk) 4915static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *tsk)
4582{ 4916{
4917 runqueue_t *rq;
4918 unsigned long flags;
4583 int dest_cpu; 4919 int dest_cpu;
4584 cpumask_t mask; 4920 cpumask_t mask;
4585 4921
4922restart:
4586 /* On same node? */ 4923 /* On same node? */
4587 mask = node_to_cpumask(cpu_to_node(dead_cpu)); 4924 mask = node_to_cpumask(cpu_to_node(dead_cpu));
4588 cpus_and(mask, mask, tsk->cpus_allowed); 4925 cpus_and(mask, mask, tsk->cpus_allowed);
@@ -4594,8 +4931,10 @@ static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *tsk)
4594 4931
4595 /* No more Mr. Nice Guy. */ 4932 /* No more Mr. Nice Guy. */
4596 if (dest_cpu == NR_CPUS) { 4933 if (dest_cpu == NR_CPUS) {
4934 rq = task_rq_lock(tsk, &flags);
4597 cpus_setall(tsk->cpus_allowed); 4935 cpus_setall(tsk->cpus_allowed);
4598 dest_cpu = any_online_cpu(tsk->cpus_allowed); 4936 dest_cpu = any_online_cpu(tsk->cpus_allowed);
4937 task_rq_unlock(rq, &flags);
4599 4938
4600 /* 4939 /*
4601 * Don't tell them about moving exiting tasks or 4940 * Don't tell them about moving exiting tasks or
@@ -4607,7 +4946,8 @@ static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *tsk)
4607 "longer affine to cpu%d\n", 4946 "longer affine to cpu%d\n",
4608 tsk->pid, tsk->comm, dead_cpu); 4947 tsk->pid, tsk->comm, dead_cpu);
4609 } 4948 }
4610 __migrate_task(tsk, dead_cpu, dest_cpu); 4949 if (!__migrate_task(tsk, dead_cpu, dest_cpu))
4950 goto restart;
4611} 4951}
4612 4952
4613/* 4953/*
@@ -4734,8 +5074,9 @@ static void migrate_dead_tasks(unsigned int dead_cpu)
4734 * migration_call - callback that gets triggered when a CPU is added. 5074 * migration_call - callback that gets triggered when a CPU is added.
4735 * Here we can start up the necessary migration thread for the new CPU. 5075 * Here we can start up the necessary migration thread for the new CPU.
4736 */ 5076 */
4737static int migration_call(struct notifier_block *nfb, unsigned long action, 5077static int __cpuinit migration_call(struct notifier_block *nfb,
4738 void *hcpu) 5078 unsigned long action,
5079 void *hcpu)
4739{ 5080{
4740 int cpu = (long)hcpu; 5081 int cpu = (long)hcpu;
4741 struct task_struct *p; 5082 struct task_struct *p;
@@ -4805,7 +5146,7 @@ static int migration_call(struct notifier_block *nfb, unsigned long action,
4805/* Register at highest priority so that task migration (migrate_all_tasks) 5146/* Register at highest priority so that task migration (migrate_all_tasks)
4806 * happens before everything else. 5147 * happens before everything else.
4807 */ 5148 */
4808static struct notifier_block migration_notifier = { 5149static struct notifier_block __cpuinitdata migration_notifier = {
4809 .notifier_call = migration_call, 5150 .notifier_call = migration_call,
4810 .priority = 10 5151 .priority = 10
4811}; 5152};
@@ -5606,6 +5947,7 @@ static cpumask_t sched_domain_node_span(int node)
5606} 5947}
5607#endif 5948#endif
5608 5949
5950int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
5609/* 5951/*
5610 * At the moment, CONFIG_SCHED_SMT is never defined, but leave it in so we 5952 * At the moment, CONFIG_SCHED_SMT is never defined, but leave it in so we
5611 * can switch it on easily if needed. 5953 * can switch it on easily if needed.
@@ -5621,7 +5963,7 @@ static int cpu_to_cpu_group(int cpu)
5621 5963
5622#ifdef CONFIG_SCHED_MC 5964#ifdef CONFIG_SCHED_MC
5623static DEFINE_PER_CPU(struct sched_domain, core_domains); 5965static DEFINE_PER_CPU(struct sched_domain, core_domains);
5624static struct sched_group sched_group_core[NR_CPUS]; 5966static struct sched_group *sched_group_core_bycpu[NR_CPUS];
5625#endif 5967#endif
5626 5968
5627#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT) 5969#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
@@ -5637,7 +5979,7 @@ static int cpu_to_core_group(int cpu)
5637#endif 5979#endif
5638 5980
5639static DEFINE_PER_CPU(struct sched_domain, phys_domains); 5981static DEFINE_PER_CPU(struct sched_domain, phys_domains);
5640static struct sched_group sched_group_phys[NR_CPUS]; 5982static struct sched_group *sched_group_phys_bycpu[NR_CPUS];
5641static int cpu_to_phys_group(int cpu) 5983static int cpu_to_phys_group(int cpu)
5642{ 5984{
5643#if defined(CONFIG_SCHED_MC) 5985#if defined(CONFIG_SCHED_MC)
@@ -5694,13 +6036,74 @@ next_sg:
5694} 6036}
5695#endif 6037#endif
5696 6038
6039/* Free memory allocated for various sched_group structures */
6040static void free_sched_groups(const cpumask_t *cpu_map)
6041{
6042 int cpu;
6043#ifdef CONFIG_NUMA
6044 int i;
6045
6046 for_each_cpu_mask(cpu, *cpu_map) {
6047 struct sched_group *sched_group_allnodes
6048 = sched_group_allnodes_bycpu[cpu];
6049 struct sched_group **sched_group_nodes
6050 = sched_group_nodes_bycpu[cpu];
6051
6052 if (sched_group_allnodes) {
6053 kfree(sched_group_allnodes);
6054 sched_group_allnodes_bycpu[cpu] = NULL;
6055 }
6056
6057 if (!sched_group_nodes)
6058 continue;
6059
6060 for (i = 0; i < MAX_NUMNODES; i++) {
6061 cpumask_t nodemask = node_to_cpumask(i);
6062 struct sched_group *oldsg, *sg = sched_group_nodes[i];
6063
6064 cpus_and(nodemask, nodemask, *cpu_map);
6065 if (cpus_empty(nodemask))
6066 continue;
6067
6068 if (sg == NULL)
6069 continue;
6070 sg = sg->next;
6071next_sg:
6072 oldsg = sg;
6073 sg = sg->next;
6074 kfree(oldsg);
6075 if (oldsg != sched_group_nodes[i])
6076 goto next_sg;
6077 }
6078 kfree(sched_group_nodes);
6079 sched_group_nodes_bycpu[cpu] = NULL;
6080 }
6081#endif
6082 for_each_cpu_mask(cpu, *cpu_map) {
6083 if (sched_group_phys_bycpu[cpu]) {
6084 kfree(sched_group_phys_bycpu[cpu]);
6085 sched_group_phys_bycpu[cpu] = NULL;
6086 }
6087#ifdef CONFIG_SCHED_MC
6088 if (sched_group_core_bycpu[cpu]) {
6089 kfree(sched_group_core_bycpu[cpu]);
6090 sched_group_core_bycpu[cpu] = NULL;
6091 }
6092#endif
6093 }
6094}
6095
5697/* 6096/*
5698 * Build sched domains for a given set of cpus and attach the sched domains 6097 * Build sched domains for a given set of cpus and attach the sched domains
5699 * to the individual cpus 6098 * to the individual cpus
5700 */ 6099 */
5701void build_sched_domains(const cpumask_t *cpu_map) 6100static int build_sched_domains(const cpumask_t *cpu_map)
5702{ 6101{
5703 int i; 6102 int i;
6103 struct sched_group *sched_group_phys = NULL;
6104#ifdef CONFIG_SCHED_MC
6105 struct sched_group *sched_group_core = NULL;
6106#endif
5704#ifdef CONFIG_NUMA 6107#ifdef CONFIG_NUMA
5705 struct sched_group **sched_group_nodes = NULL; 6108 struct sched_group **sched_group_nodes = NULL;
5706 struct sched_group *sched_group_allnodes = NULL; 6109 struct sched_group *sched_group_allnodes = NULL;
@@ -5708,11 +6111,11 @@ void build_sched_domains(const cpumask_t *cpu_map)
5708 /* 6111 /*
5709 * Allocate the per-node list of sched groups 6112 * Allocate the per-node list of sched groups
5710 */ 6113 */
5711 sched_group_nodes = kmalloc(sizeof(struct sched_group*)*MAX_NUMNODES, 6114 sched_group_nodes = kzalloc(sizeof(struct sched_group*)*MAX_NUMNODES,
5712 GFP_ATOMIC); 6115 GFP_KERNEL);
5713 if (!sched_group_nodes) { 6116 if (!sched_group_nodes) {
5714 printk(KERN_WARNING "Can not alloc sched group node list\n"); 6117 printk(KERN_WARNING "Can not alloc sched group node list\n");
5715 return; 6118 return -ENOMEM;
5716 } 6119 }
5717 sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes; 6120 sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
5718#endif 6121#endif
@@ -5738,7 +6141,7 @@ void build_sched_domains(const cpumask_t *cpu_map)
5738 if (!sched_group_allnodes) { 6141 if (!sched_group_allnodes) {
5739 printk(KERN_WARNING 6142 printk(KERN_WARNING
5740 "Can not alloc allnodes sched group\n"); 6143 "Can not alloc allnodes sched group\n");
5741 break; 6144 goto error;
5742 } 6145 }
5743 sched_group_allnodes_bycpu[i] 6146 sched_group_allnodes_bycpu[i]
5744 = sched_group_allnodes; 6147 = sched_group_allnodes;
@@ -5759,6 +6162,18 @@ void build_sched_domains(const cpumask_t *cpu_map)
5759 cpus_and(sd->span, sd->span, *cpu_map); 6162 cpus_and(sd->span, sd->span, *cpu_map);
5760#endif 6163#endif
5761 6164
6165 if (!sched_group_phys) {
6166 sched_group_phys
6167 = kmalloc(sizeof(struct sched_group) * NR_CPUS,
6168 GFP_KERNEL);
6169 if (!sched_group_phys) {
6170 printk (KERN_WARNING "Can not alloc phys sched"
6171 "group\n");
6172 goto error;
6173 }
6174 sched_group_phys_bycpu[i] = sched_group_phys;
6175 }
6176
5762 p = sd; 6177 p = sd;
5763 sd = &per_cpu(phys_domains, i); 6178 sd = &per_cpu(phys_domains, i);
5764 group = cpu_to_phys_group(i); 6179 group = cpu_to_phys_group(i);
@@ -5768,6 +6183,18 @@ void build_sched_domains(const cpumask_t *cpu_map)
5768 sd->groups = &sched_group_phys[group]; 6183 sd->groups = &sched_group_phys[group];
5769 6184
5770#ifdef CONFIG_SCHED_MC 6185#ifdef CONFIG_SCHED_MC
6186 if (!sched_group_core) {
6187 sched_group_core
6188 = kmalloc(sizeof(struct sched_group) * NR_CPUS,
6189 GFP_KERNEL);
6190 if (!sched_group_core) {
6191 printk (KERN_WARNING "Can not alloc core sched"
6192 "group\n");
6193 goto error;
6194 }
6195 sched_group_core_bycpu[i] = sched_group_core;
6196 }
6197
5771 p = sd; 6198 p = sd;
5772 sd = &per_cpu(core_domains, i); 6199 sd = &per_cpu(core_domains, i);
5773 group = cpu_to_core_group(i); 6200 group = cpu_to_core_group(i);
@@ -5851,24 +6278,21 @@ void build_sched_domains(const cpumask_t *cpu_map)
5851 domainspan = sched_domain_node_span(i); 6278 domainspan = sched_domain_node_span(i);
5852 cpus_and(domainspan, domainspan, *cpu_map); 6279 cpus_and(domainspan, domainspan, *cpu_map);
5853 6280
5854 sg = kmalloc(sizeof(struct sched_group), GFP_KERNEL); 6281 sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i);
6282 if (!sg) {
6283 printk(KERN_WARNING "Can not alloc domain group for "
6284 "node %d\n", i);
6285 goto error;
6286 }
5855 sched_group_nodes[i] = sg; 6287 sched_group_nodes[i] = sg;
5856 for_each_cpu_mask(j, nodemask) { 6288 for_each_cpu_mask(j, nodemask) {
5857 struct sched_domain *sd; 6289 struct sched_domain *sd;
5858 sd = &per_cpu(node_domains, j); 6290 sd = &per_cpu(node_domains, j);
5859 sd->groups = sg; 6291 sd->groups = sg;
5860 if (sd->groups == NULL) {
5861 /* Turn off balancing if we have no groups */
5862 sd->flags = 0;
5863 }
5864 }
5865 if (!sg) {
5866 printk(KERN_WARNING
5867 "Can not alloc domain group for node %d\n", i);
5868 continue;
5869 } 6292 }
5870 sg->cpu_power = 0; 6293 sg->cpu_power = 0;
5871 sg->cpumask = nodemask; 6294 sg->cpumask = nodemask;
6295 sg->next = sg;
5872 cpus_or(covered, covered, nodemask); 6296 cpus_or(covered, covered, nodemask);
5873 prev = sg; 6297 prev = sg;
5874 6298
@@ -5887,54 +6311,90 @@ void build_sched_domains(const cpumask_t *cpu_map)
5887 if (cpus_empty(tmp)) 6311 if (cpus_empty(tmp))
5888 continue; 6312 continue;
5889 6313
5890 sg = kmalloc(sizeof(struct sched_group), GFP_KERNEL); 6314 sg = kmalloc_node(sizeof(struct sched_group),
6315 GFP_KERNEL, i);
5891 if (!sg) { 6316 if (!sg) {
5892 printk(KERN_WARNING 6317 printk(KERN_WARNING
5893 "Can not alloc domain group for node %d\n", j); 6318 "Can not alloc domain group for node %d\n", j);
5894 break; 6319 goto error;
5895 } 6320 }
5896 sg->cpu_power = 0; 6321 sg->cpu_power = 0;
5897 sg->cpumask = tmp; 6322 sg->cpumask = tmp;
6323 sg->next = prev->next;
5898 cpus_or(covered, covered, tmp); 6324 cpus_or(covered, covered, tmp);
5899 prev->next = sg; 6325 prev->next = sg;
5900 prev = sg; 6326 prev = sg;
5901 } 6327 }
5902 prev->next = sched_group_nodes[i];
5903 } 6328 }
5904#endif 6329#endif
5905 6330
5906 /* Calculate CPU power for physical packages and nodes */ 6331 /* Calculate CPU power for physical packages and nodes */
6332#ifdef CONFIG_SCHED_SMT
5907 for_each_cpu_mask(i, *cpu_map) { 6333 for_each_cpu_mask(i, *cpu_map) {
5908 int power;
5909 struct sched_domain *sd; 6334 struct sched_domain *sd;
5910#ifdef CONFIG_SCHED_SMT
5911 sd = &per_cpu(cpu_domains, i); 6335 sd = &per_cpu(cpu_domains, i);
5912 power = SCHED_LOAD_SCALE; 6336 sd->groups->cpu_power = SCHED_LOAD_SCALE;
5913 sd->groups->cpu_power = power; 6337 }
5914#endif 6338#endif
5915#ifdef CONFIG_SCHED_MC 6339#ifdef CONFIG_SCHED_MC
6340 for_each_cpu_mask(i, *cpu_map) {
6341 int power;
6342 struct sched_domain *sd;
5916 sd = &per_cpu(core_domains, i); 6343 sd = &per_cpu(core_domains, i);
5917 power = SCHED_LOAD_SCALE + (cpus_weight(sd->groups->cpumask)-1) 6344 if (sched_smt_power_savings)
6345 power = SCHED_LOAD_SCALE * cpus_weight(sd->groups->cpumask);
6346 else
6347 power = SCHED_LOAD_SCALE + (cpus_weight(sd->groups->cpumask)-1)
5918 * SCHED_LOAD_SCALE / 10; 6348 * SCHED_LOAD_SCALE / 10;
5919 sd->groups->cpu_power = power; 6349 sd->groups->cpu_power = power;
6350 }
6351#endif
5920 6352
6353 for_each_cpu_mask(i, *cpu_map) {
6354 struct sched_domain *sd;
6355#ifdef CONFIG_SCHED_MC
5921 sd = &per_cpu(phys_domains, i); 6356 sd = &per_cpu(phys_domains, i);
6357 if (i != first_cpu(sd->groups->cpumask))
6358 continue;
5922 6359
5923 /* 6360 sd->groups->cpu_power = 0;
5924 * This has to be < 2 * SCHED_LOAD_SCALE 6361 if (sched_mc_power_savings || sched_smt_power_savings) {
5925 * Lets keep it SCHED_LOAD_SCALE, so that 6362 int j;
5926 * while calculating NUMA group's cpu_power 6363
5927 * we can simply do 6364 for_each_cpu_mask(j, sd->groups->cpumask) {
5928 * numa_group->cpu_power += phys_group->cpu_power; 6365 struct sched_domain *sd1;
5929 * 6366 sd1 = &per_cpu(core_domains, j);
5930 * See "only add power once for each physical pkg" 6367 /*
5931 * comment below 6368 * for each core we will add once
5932 */ 6369 * to the group in physical domain
5933 sd->groups->cpu_power = SCHED_LOAD_SCALE; 6370 */
6371 if (j != first_cpu(sd1->groups->cpumask))
6372 continue;
6373
6374 if (sched_smt_power_savings)
6375 sd->groups->cpu_power += sd1->groups->cpu_power;
6376 else
6377 sd->groups->cpu_power += SCHED_LOAD_SCALE;
6378 }
6379 } else
6380 /*
6381 * This has to be < 2 * SCHED_LOAD_SCALE
6382 * Lets keep it SCHED_LOAD_SCALE, so that
6383 * while calculating NUMA group's cpu_power
6384 * we can simply do
6385 * numa_group->cpu_power += phys_group->cpu_power;
6386 *
6387 * See "only add power once for each physical pkg"
6388 * comment below
6389 */
6390 sd->groups->cpu_power = SCHED_LOAD_SCALE;
5934#else 6391#else
6392 int power;
5935 sd = &per_cpu(phys_domains, i); 6393 sd = &per_cpu(phys_domains, i);
5936 power = SCHED_LOAD_SCALE + SCHED_LOAD_SCALE * 6394 if (sched_smt_power_savings)
5937 (cpus_weight(sd->groups->cpumask)-1) / 10; 6395 power = SCHED_LOAD_SCALE * cpus_weight(sd->groups->cpumask);
6396 else
6397 power = SCHED_LOAD_SCALE;
5938 sd->groups->cpu_power = power; 6398 sd->groups->cpu_power = power;
5939#endif 6399#endif
5940 } 6400 }
@@ -5962,13 +6422,20 @@ void build_sched_domains(const cpumask_t *cpu_map)
5962 * Tune cache-hot values: 6422 * Tune cache-hot values:
5963 */ 6423 */
5964 calibrate_migration_costs(cpu_map); 6424 calibrate_migration_costs(cpu_map);
6425
6426 return 0;
6427
6428error:
6429 free_sched_groups(cpu_map);
6430 return -ENOMEM;
5965} 6431}
5966/* 6432/*
5967 * Set up scheduler domains and groups. Callers must hold the hotplug lock. 6433 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
5968 */ 6434 */
5969static void arch_init_sched_domains(const cpumask_t *cpu_map) 6435static int arch_init_sched_domains(const cpumask_t *cpu_map)
5970{ 6436{
5971 cpumask_t cpu_default_map; 6437 cpumask_t cpu_default_map;
6438 int err;
5972 6439
5973 /* 6440 /*
5974 * Setup mask for cpus without special case scheduling requirements. 6441 * Setup mask for cpus without special case scheduling requirements.
@@ -5977,51 +6444,14 @@ static void arch_init_sched_domains(const cpumask_t *cpu_map)
5977 */ 6444 */
5978 cpus_andnot(cpu_default_map, *cpu_map, cpu_isolated_map); 6445 cpus_andnot(cpu_default_map, *cpu_map, cpu_isolated_map);
5979 6446
5980 build_sched_domains(&cpu_default_map); 6447 err = build_sched_domains(&cpu_default_map);
6448
6449 return err;
5981} 6450}
5982 6451
5983static void arch_destroy_sched_domains(const cpumask_t *cpu_map) 6452static void arch_destroy_sched_domains(const cpumask_t *cpu_map)
5984{ 6453{
5985#ifdef CONFIG_NUMA 6454 free_sched_groups(cpu_map);
5986 int i;
5987 int cpu;
5988
5989 for_each_cpu_mask(cpu, *cpu_map) {
5990 struct sched_group *sched_group_allnodes
5991 = sched_group_allnodes_bycpu[cpu];
5992 struct sched_group **sched_group_nodes
5993 = sched_group_nodes_bycpu[cpu];
5994
5995 if (sched_group_allnodes) {
5996 kfree(sched_group_allnodes);
5997 sched_group_allnodes_bycpu[cpu] = NULL;
5998 }
5999
6000 if (!sched_group_nodes)
6001 continue;
6002
6003 for (i = 0; i < MAX_NUMNODES; i++) {
6004 cpumask_t nodemask = node_to_cpumask(i);
6005 struct sched_group *oldsg, *sg = sched_group_nodes[i];
6006
6007 cpus_and(nodemask, nodemask, *cpu_map);
6008 if (cpus_empty(nodemask))
6009 continue;
6010
6011 if (sg == NULL)
6012 continue;
6013 sg = sg->next;
6014next_sg:
6015 oldsg = sg;
6016 sg = sg->next;
6017 kfree(oldsg);
6018 if (oldsg != sched_group_nodes[i])
6019 goto next_sg;
6020 }
6021 kfree(sched_group_nodes);
6022 sched_group_nodes_bycpu[cpu] = NULL;
6023 }
6024#endif
6025} 6455}
6026 6456
6027/* 6457/*
@@ -6046,9 +6476,10 @@ static void detach_destroy_domains(const cpumask_t *cpu_map)
6046 * correct sched domains 6476 * correct sched domains
6047 * Call with hotplug lock held 6477 * Call with hotplug lock held
6048 */ 6478 */
6049void partition_sched_domains(cpumask_t *partition1, cpumask_t *partition2) 6479int partition_sched_domains(cpumask_t *partition1, cpumask_t *partition2)
6050{ 6480{
6051 cpumask_t change_map; 6481 cpumask_t change_map;
6482 int err = 0;
6052 6483
6053 cpus_and(*partition1, *partition1, cpu_online_map); 6484 cpus_and(*partition1, *partition1, cpu_online_map);
6054 cpus_and(*partition2, *partition2, cpu_online_map); 6485 cpus_and(*partition2, *partition2, cpu_online_map);
@@ -6057,10 +6488,86 @@ void partition_sched_domains(cpumask_t *partition1, cpumask_t *partition2)
6057 /* Detach sched domains from all of the affected cpus */ 6488 /* Detach sched domains from all of the affected cpus */
6058 detach_destroy_domains(&change_map); 6489 detach_destroy_domains(&change_map);
6059 if (!cpus_empty(*partition1)) 6490 if (!cpus_empty(*partition1))
6060 build_sched_domains(partition1); 6491 err = build_sched_domains(partition1);
6061 if (!cpus_empty(*partition2)) 6492 if (!err && !cpus_empty(*partition2))
6062 build_sched_domains(partition2); 6493 err = build_sched_domains(partition2);
6494
6495 return err;
6496}
6497
6498#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
6499int arch_reinit_sched_domains(void)
6500{
6501 int err;
6502
6503 lock_cpu_hotplug();
6504 detach_destroy_domains(&cpu_online_map);
6505 err = arch_init_sched_domains(&cpu_online_map);
6506 unlock_cpu_hotplug();
6507
6508 return err;
6509}
6510
6511static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
6512{
6513 int ret;
6514
6515 if (buf[0] != '0' && buf[0] != '1')
6516 return -EINVAL;
6517
6518 if (smt)
6519 sched_smt_power_savings = (buf[0] == '1');
6520 else
6521 sched_mc_power_savings = (buf[0] == '1');
6522
6523 ret = arch_reinit_sched_domains();
6524
6525 return ret ? ret : count;
6526}
6527
6528int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
6529{
6530 int err = 0;
6531#ifdef CONFIG_SCHED_SMT
6532 if (smt_capable())
6533 err = sysfs_create_file(&cls->kset.kobj,
6534 &attr_sched_smt_power_savings.attr);
6535#endif
6536#ifdef CONFIG_SCHED_MC
6537 if (!err && mc_capable())
6538 err = sysfs_create_file(&cls->kset.kobj,
6539 &attr_sched_mc_power_savings.attr);
6540#endif
6541 return err;
6542}
6543#endif
6544
6545#ifdef CONFIG_SCHED_MC
6546static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page)
6547{
6548 return sprintf(page, "%u\n", sched_mc_power_savings);
6549}
6550static ssize_t sched_mc_power_savings_store(struct sys_device *dev, const char *buf, size_t count)
6551{
6552 return sched_power_savings_store(buf, count, 0);
6553}
6554SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show,
6555 sched_mc_power_savings_store);
6556#endif
6557
6558#ifdef CONFIG_SCHED_SMT
6559static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page)
6560{
6561 return sprintf(page, "%u\n", sched_smt_power_savings);
6562}
6563static ssize_t sched_smt_power_savings_store(struct sys_device *dev, const char *buf, size_t count)
6564{
6565 return sched_power_savings_store(buf, count, 1);
6063} 6566}
6567SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show,
6568 sched_smt_power_savings_store);
6569#endif
6570
6064 6571
6065#ifdef CONFIG_HOTPLUG_CPU 6572#ifdef CONFIG_HOTPLUG_CPU
6066/* 6573/*
@@ -6143,7 +6650,6 @@ void __init sched_init(void)
6143 rq->push_cpu = 0; 6650 rq->push_cpu = 0;
6144 rq->migration_thread = NULL; 6651 rq->migration_thread = NULL;
6145 INIT_LIST_HEAD(&rq->migration_queue); 6652 INIT_LIST_HEAD(&rq->migration_queue);
6146 rq->cpu = i;
6147#endif 6653#endif
6148 atomic_set(&rq->nr_iowait, 0); 6654 atomic_set(&rq->nr_iowait, 0);
6149 6655
@@ -6158,6 +6664,7 @@ void __init sched_init(void)
6158 } 6664 }
6159 } 6665 }
6160 6666
6667 set_load_weight(&init_task);
6161 /* 6668 /*
6162 * The boot idle thread does lazy MMU switching as well: 6669 * The boot idle thread does lazy MMU switching as well:
6163 */ 6670 */
@@ -6204,11 +6711,12 @@ void normalize_rt_tasks(void)
6204 runqueue_t *rq; 6711 runqueue_t *rq;
6205 6712
6206 read_lock_irq(&tasklist_lock); 6713 read_lock_irq(&tasklist_lock);
6207 for_each_process (p) { 6714 for_each_process(p) {
6208 if (!rt_task(p)) 6715 if (!rt_task(p))
6209 continue; 6716 continue;
6210 6717
6211 rq = task_rq_lock(p, &flags); 6718 spin_lock_irqsave(&p->pi_lock, flags);
6719 rq = __task_rq_lock(p);
6212 6720
6213 array = p->array; 6721 array = p->array;
6214 if (array) 6722 if (array)
@@ -6219,7 +6727,8 @@ void normalize_rt_tasks(void)
6219 resched_task(rq->curr); 6727 resched_task(rq->curr);
6220 } 6728 }
6221 6729
6222 task_rq_unlock(rq, &flags); 6730 __task_rq_unlock(rq);
6731 spin_unlock_irqrestore(&p->pi_lock, flags);
6223 } 6732 }
6224 read_unlock_irq(&tasklist_lock); 6733 read_unlock_irq(&tasklist_lock);
6225} 6734}