aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/sched.c
diff options
context:
space:
mode:
authorSteve French <sfrench@us.ibm.com>2008-04-24 11:26:50 -0400
committerSteve French <sfrench@us.ibm.com>2008-04-24 11:26:50 -0400
commit36d99df2fb474222ab47fbe8ae7385661033223b (patch)
tree962e068491b752a944f61c454fad3f8619a1ea3f /kernel/sched.c
parent076d8423a98659a92837b07aa494cb74bfefe77c (diff)
parent3dc5063786b273f1aee545844f6bd4e9651ebffe (diff)
Merge branch 'master' of /pub/scm/linux/kernel/git/torvalds/linux-2.6
Diffstat (limited to 'kernel/sched.c')
-rw-r--r--kernel/sched.c1913
1 files changed, 1569 insertions, 344 deletions
diff --git a/kernel/sched.c b/kernel/sched.c
index 8dcdec6fe0fe..0014b03adaca 100644
--- a/kernel/sched.c
+++ b/kernel/sched.c
@@ -66,6 +66,10 @@
66#include <linux/unistd.h> 66#include <linux/unistd.h>
67#include <linux/pagemap.h> 67#include <linux/pagemap.h>
68#include <linux/hrtimer.h> 68#include <linux/hrtimer.h>
69#include <linux/tick.h>
70#include <linux/bootmem.h>
71#include <linux/debugfs.h>
72#include <linux/ctype.h>
69 73
70#include <asm/tlb.h> 74#include <asm/tlb.h>
71#include <asm/irq_regs.h> 75#include <asm/irq_regs.h>
@@ -114,6 +118,11 @@ unsigned long long __attribute__((weak)) sched_clock(void)
114 */ 118 */
115#define DEF_TIMESLICE (100 * HZ / 1000) 119#define DEF_TIMESLICE (100 * HZ / 1000)
116 120
121/*
122 * single value that denotes runtime == period, ie unlimited time.
123 */
124#define RUNTIME_INF ((u64)~0ULL)
125
117#ifdef CONFIG_SMP 126#ifdef CONFIG_SMP
118/* 127/*
119 * Divide a load by a sched group cpu_power : (load / sg->__cpu_power) 128 * Divide a load by a sched group cpu_power : (load / sg->__cpu_power)
@@ -155,6 +164,84 @@ struct rt_prio_array {
155 struct list_head queue[MAX_RT_PRIO]; 164 struct list_head queue[MAX_RT_PRIO];
156}; 165};
157 166
167struct rt_bandwidth {
168 /* nests inside the rq lock: */
169 spinlock_t rt_runtime_lock;
170 ktime_t rt_period;
171 u64 rt_runtime;
172 struct hrtimer rt_period_timer;
173};
174
175static struct rt_bandwidth def_rt_bandwidth;
176
177static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
178
179static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
180{
181 struct rt_bandwidth *rt_b =
182 container_of(timer, struct rt_bandwidth, rt_period_timer);
183 ktime_t now;
184 int overrun;
185 int idle = 0;
186
187 for (;;) {
188 now = hrtimer_cb_get_time(timer);
189 overrun = hrtimer_forward(timer, now, rt_b->rt_period);
190
191 if (!overrun)
192 break;
193
194 idle = do_sched_rt_period_timer(rt_b, overrun);
195 }
196
197 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
198}
199
200static
201void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
202{
203 rt_b->rt_period = ns_to_ktime(period);
204 rt_b->rt_runtime = runtime;
205
206 spin_lock_init(&rt_b->rt_runtime_lock);
207
208 hrtimer_init(&rt_b->rt_period_timer,
209 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
210 rt_b->rt_period_timer.function = sched_rt_period_timer;
211 rt_b->rt_period_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
212}
213
214static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
215{
216 ktime_t now;
217
218 if (rt_b->rt_runtime == RUNTIME_INF)
219 return;
220
221 if (hrtimer_active(&rt_b->rt_period_timer))
222 return;
223
224 spin_lock(&rt_b->rt_runtime_lock);
225 for (;;) {
226 if (hrtimer_active(&rt_b->rt_period_timer))
227 break;
228
229 now = hrtimer_cb_get_time(&rt_b->rt_period_timer);
230 hrtimer_forward(&rt_b->rt_period_timer, now, rt_b->rt_period);
231 hrtimer_start(&rt_b->rt_period_timer,
232 rt_b->rt_period_timer.expires,
233 HRTIMER_MODE_ABS);
234 }
235 spin_unlock(&rt_b->rt_runtime_lock);
236}
237
238#ifdef CONFIG_RT_GROUP_SCHED
239static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
240{
241 hrtimer_cancel(&rt_b->rt_period_timer);
242}
243#endif
244
158#ifdef CONFIG_GROUP_SCHED 245#ifdef CONFIG_GROUP_SCHED
159 246
160#include <linux/cgroup.h> 247#include <linux/cgroup.h>
@@ -181,29 +268,39 @@ struct task_group {
181 struct sched_rt_entity **rt_se; 268 struct sched_rt_entity **rt_se;
182 struct rt_rq **rt_rq; 269 struct rt_rq **rt_rq;
183 270
184 u64 rt_runtime; 271 struct rt_bandwidth rt_bandwidth;
185#endif 272#endif
186 273
187 struct rcu_head rcu; 274 struct rcu_head rcu;
188 struct list_head list; 275 struct list_head list;
276
277 struct task_group *parent;
278 struct list_head siblings;
279 struct list_head children;
189}; 280};
190 281
282#ifdef CONFIG_USER_SCHED
283
284/*
285 * Root task group.
286 * Every UID task group (including init_task_group aka UID-0) will
287 * be a child to this group.
288 */
289struct task_group root_task_group;
290
191#ifdef CONFIG_FAIR_GROUP_SCHED 291#ifdef CONFIG_FAIR_GROUP_SCHED
192/* Default task group's sched entity on each cpu */ 292/* Default task group's sched entity on each cpu */
193static DEFINE_PER_CPU(struct sched_entity, init_sched_entity); 293static DEFINE_PER_CPU(struct sched_entity, init_sched_entity);
194/* Default task group's cfs_rq on each cpu */ 294/* Default task group's cfs_rq on each cpu */
195static DEFINE_PER_CPU(struct cfs_rq, init_cfs_rq) ____cacheline_aligned_in_smp; 295static DEFINE_PER_CPU(struct cfs_rq, init_cfs_rq) ____cacheline_aligned_in_smp;
196
197static struct sched_entity *init_sched_entity_p[NR_CPUS];
198static struct cfs_rq *init_cfs_rq_p[NR_CPUS];
199#endif 296#endif
200 297
201#ifdef CONFIG_RT_GROUP_SCHED 298#ifdef CONFIG_RT_GROUP_SCHED
202static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity); 299static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity);
203static DEFINE_PER_CPU(struct rt_rq, init_rt_rq) ____cacheline_aligned_in_smp; 300static DEFINE_PER_CPU(struct rt_rq, init_rt_rq) ____cacheline_aligned_in_smp;
204 301#endif
205static struct sched_rt_entity *init_sched_rt_entity_p[NR_CPUS]; 302#else
206static struct rt_rq *init_rt_rq_p[NR_CPUS]; 303#define root_task_group init_task_group
207#endif 304#endif
208 305
209/* task_group_lock serializes add/remove of task groups and also changes to 306/* task_group_lock serializes add/remove of task groups and also changes to
@@ -221,23 +318,15 @@ static DEFINE_MUTEX(doms_cur_mutex);
221# define INIT_TASK_GROUP_LOAD NICE_0_LOAD 318# define INIT_TASK_GROUP_LOAD NICE_0_LOAD
222#endif 319#endif
223 320
321#define MIN_SHARES 2
322
224static int init_task_group_load = INIT_TASK_GROUP_LOAD; 323static int init_task_group_load = INIT_TASK_GROUP_LOAD;
225#endif 324#endif
226 325
227/* Default task group. 326/* Default task group.
228 * Every task in system belong to this group at bootup. 327 * Every task in system belong to this group at bootup.
229 */ 328 */
230struct task_group init_task_group = { 329struct task_group init_task_group;
231#ifdef CONFIG_FAIR_GROUP_SCHED
232 .se = init_sched_entity_p,
233 .cfs_rq = init_cfs_rq_p,
234#endif
235
236#ifdef CONFIG_RT_GROUP_SCHED
237 .rt_se = init_sched_rt_entity_p,
238 .rt_rq = init_rt_rq_p,
239#endif
240};
241 330
242/* return group to which a task belongs */ 331/* return group to which a task belongs */
243static inline struct task_group *task_group(struct task_struct *p) 332static inline struct task_group *task_group(struct task_struct *p)
@@ -297,8 +386,12 @@ struct cfs_rq {
297 386
298 struct rb_root tasks_timeline; 387 struct rb_root tasks_timeline;
299 struct rb_node *rb_leftmost; 388 struct rb_node *rb_leftmost;
300 struct rb_node *rb_load_balance_curr; 389
301 /* 'curr' points to currently running entity on this cfs_rq. 390 struct list_head tasks;
391 struct list_head *balance_iterator;
392
393 /*
394 * 'curr' points to currently running entity on this cfs_rq.
302 * It is set to NULL otherwise (i.e when none are currently running). 395 * It is set to NULL otherwise (i.e when none are currently running).
303 */ 396 */
304 struct sched_entity *curr, *next; 397 struct sched_entity *curr, *next;
@@ -318,6 +411,43 @@ struct cfs_rq {
318 */ 411 */
319 struct list_head leaf_cfs_rq_list; 412 struct list_head leaf_cfs_rq_list;
320 struct task_group *tg; /* group that "owns" this runqueue */ 413 struct task_group *tg; /* group that "owns" this runqueue */
414
415#ifdef CONFIG_SMP
416 unsigned long task_weight;
417 unsigned long shares;
418 /*
419 * We need space to build a sched_domain wide view of the full task
420 * group tree, in order to avoid depending on dynamic memory allocation
421 * during the load balancing we place this in the per cpu task group
422 * hierarchy. This limits the load balancing to one instance per cpu,
423 * but more should not be needed anyway.
424 */
425 struct aggregate_struct {
426 /*
427 * load = weight(cpus) * f(tg)
428 *
429 * Where f(tg) is the recursive weight fraction assigned to
430 * this group.
431 */
432 unsigned long load;
433
434 /*
435 * part of the group weight distributed to this span.
436 */
437 unsigned long shares;
438
439 /*
440 * The sum of all runqueue weights within this span.
441 */
442 unsigned long rq_weight;
443
444 /*
445 * Weight contributed by tasks; this is the part we can
446 * influence by moving tasks around.
447 */
448 unsigned long task_weight;
449 } aggregate;
450#endif
321#endif 451#endif
322}; 452};
323 453
@@ -334,6 +464,9 @@ struct rt_rq {
334#endif 464#endif
335 int rt_throttled; 465 int rt_throttled;
336 u64 rt_time; 466 u64 rt_time;
467 u64 rt_runtime;
468 /* Nests inside the rq lock: */
469 spinlock_t rt_runtime_lock;
337 470
338#ifdef CONFIG_RT_GROUP_SCHED 471#ifdef CONFIG_RT_GROUP_SCHED
339 unsigned long rt_nr_boosted; 472 unsigned long rt_nr_boosted;
@@ -396,6 +529,7 @@ struct rq {
396 unsigned long cpu_load[CPU_LOAD_IDX_MAX]; 529 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
397 unsigned char idle_at_tick; 530 unsigned char idle_at_tick;
398#ifdef CONFIG_NO_HZ 531#ifdef CONFIG_NO_HZ
532 unsigned long last_tick_seen;
399 unsigned char in_nohz_recently; 533 unsigned char in_nohz_recently;
400#endif 534#endif
401 /* capture load from *all* tasks on this cpu: */ 535 /* capture load from *all* tasks on this cpu: */
@@ -405,8 +539,6 @@ struct rq {
405 539
406 struct cfs_rq cfs; 540 struct cfs_rq cfs;
407 struct rt_rq rt; 541 struct rt_rq rt;
408 u64 rt_period_expire;
409 int rt_throttled;
410 542
411#ifdef CONFIG_FAIR_GROUP_SCHED 543#ifdef CONFIG_FAIR_GROUP_SCHED
412 /* list of leaf cfs_rq on this cpu: */ 544 /* list of leaf cfs_rq on this cpu: */
@@ -499,6 +631,32 @@ static inline int cpu_of(struct rq *rq)
499#endif 631#endif
500} 632}
501 633
634#ifdef CONFIG_NO_HZ
635static inline bool nohz_on(int cpu)
636{
637 return tick_get_tick_sched(cpu)->nohz_mode != NOHZ_MODE_INACTIVE;
638}
639
640static inline u64 max_skipped_ticks(struct rq *rq)
641{
642 return nohz_on(cpu_of(rq)) ? jiffies - rq->last_tick_seen + 2 : 1;
643}
644
645static inline void update_last_tick_seen(struct rq *rq)
646{
647 rq->last_tick_seen = jiffies;
648}
649#else
650static inline u64 max_skipped_ticks(struct rq *rq)
651{
652 return 1;
653}
654
655static inline void update_last_tick_seen(struct rq *rq)
656{
657}
658#endif
659
502/* 660/*
503 * Update the per-runqueue clock, as finegrained as the platform can give 661 * Update the per-runqueue clock, as finegrained as the platform can give
504 * us, but without assuming monotonicity, etc.: 662 * us, but without assuming monotonicity, etc.:
@@ -523,9 +681,12 @@ static void __update_rq_clock(struct rq *rq)
523 /* 681 /*
524 * Catch too large forward jumps too: 682 * Catch too large forward jumps too:
525 */ 683 */
526 if (unlikely(clock + delta > rq->tick_timestamp + TICK_NSEC)) { 684 u64 max_jump = max_skipped_ticks(rq) * TICK_NSEC;
527 if (clock < rq->tick_timestamp + TICK_NSEC) 685 u64 max_time = rq->tick_timestamp + max_jump;
528 clock = rq->tick_timestamp + TICK_NSEC; 686
687 if (unlikely(clock + delta > max_time)) {
688 if (clock < max_time)
689 clock = max_time;
529 else 690 else
530 clock++; 691 clock++;
531 rq->clock_overflows++; 692 rq->clock_overflows++;
@@ -561,23 +722,6 @@ static void update_rq_clock(struct rq *rq)
561#define task_rq(p) cpu_rq(task_cpu(p)) 722#define task_rq(p) cpu_rq(task_cpu(p))
562#define cpu_curr(cpu) (cpu_rq(cpu)->curr) 723#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
563 724
564unsigned long rt_needs_cpu(int cpu)
565{
566 struct rq *rq = cpu_rq(cpu);
567 u64 delta;
568
569 if (!rq->rt_throttled)
570 return 0;
571
572 if (rq->clock > rq->rt_period_expire)
573 return 1;
574
575 delta = rq->rt_period_expire - rq->clock;
576 do_div(delta, NSEC_PER_SEC / HZ);
577
578 return (unsigned long)delta;
579}
580
581/* 725/*
582 * Tunables that become constants when CONFIG_SCHED_DEBUG is off: 726 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
583 */ 727 */
@@ -590,22 +734,137 @@ unsigned long rt_needs_cpu(int cpu)
590/* 734/*
591 * Debugging: various feature bits 735 * Debugging: various feature bits
592 */ 736 */
737
738#define SCHED_FEAT(name, enabled) \
739 __SCHED_FEAT_##name ,
740
593enum { 741enum {
594 SCHED_FEAT_NEW_FAIR_SLEEPERS = 1, 742#include "sched_features.h"
595 SCHED_FEAT_WAKEUP_PREEMPT = 2,
596 SCHED_FEAT_START_DEBIT = 4,
597 SCHED_FEAT_HRTICK = 8,
598 SCHED_FEAT_DOUBLE_TICK = 16,
599}; 743};
600 744
745#undef SCHED_FEAT
746
747#define SCHED_FEAT(name, enabled) \
748 (1UL << __SCHED_FEAT_##name) * enabled |
749
601const_debug unsigned int sysctl_sched_features = 750const_debug unsigned int sysctl_sched_features =
602 SCHED_FEAT_NEW_FAIR_SLEEPERS * 1 | 751#include "sched_features.h"
603 SCHED_FEAT_WAKEUP_PREEMPT * 1 | 752 0;
604 SCHED_FEAT_START_DEBIT * 1 | 753
605 SCHED_FEAT_HRTICK * 1 | 754#undef SCHED_FEAT
606 SCHED_FEAT_DOUBLE_TICK * 0; 755
756#ifdef CONFIG_SCHED_DEBUG
757#define SCHED_FEAT(name, enabled) \
758 #name ,
759
760__read_mostly char *sched_feat_names[] = {
761#include "sched_features.h"
762 NULL
763};
764
765#undef SCHED_FEAT
766
767int sched_feat_open(struct inode *inode, struct file *filp)
768{
769 filp->private_data = inode->i_private;
770 return 0;
771}
772
773static ssize_t
774sched_feat_read(struct file *filp, char __user *ubuf,
775 size_t cnt, loff_t *ppos)
776{
777 char *buf;
778 int r = 0;
779 int len = 0;
780 int i;
607 781
608#define sched_feat(x) (sysctl_sched_features & SCHED_FEAT_##x) 782 for (i = 0; sched_feat_names[i]; i++) {
783 len += strlen(sched_feat_names[i]);
784 len += 4;
785 }
786
787 buf = kmalloc(len + 2, GFP_KERNEL);
788 if (!buf)
789 return -ENOMEM;
790
791 for (i = 0; sched_feat_names[i]; i++) {
792 if (sysctl_sched_features & (1UL << i))
793 r += sprintf(buf + r, "%s ", sched_feat_names[i]);
794 else
795 r += sprintf(buf + r, "NO_%s ", sched_feat_names[i]);
796 }
797
798 r += sprintf(buf + r, "\n");
799 WARN_ON(r >= len + 2);
800
801 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
802
803 kfree(buf);
804
805 return r;
806}
807
808static ssize_t
809sched_feat_write(struct file *filp, const char __user *ubuf,
810 size_t cnt, loff_t *ppos)
811{
812 char buf[64];
813 char *cmp = buf;
814 int neg = 0;
815 int i;
816
817 if (cnt > 63)
818 cnt = 63;
819
820 if (copy_from_user(&buf, ubuf, cnt))
821 return -EFAULT;
822
823 buf[cnt] = 0;
824
825 if (strncmp(buf, "NO_", 3) == 0) {
826 neg = 1;
827 cmp += 3;
828 }
829
830 for (i = 0; sched_feat_names[i]; i++) {
831 int len = strlen(sched_feat_names[i]);
832
833 if (strncmp(cmp, sched_feat_names[i], len) == 0) {
834 if (neg)
835 sysctl_sched_features &= ~(1UL << i);
836 else
837 sysctl_sched_features |= (1UL << i);
838 break;
839 }
840 }
841
842 if (!sched_feat_names[i])
843 return -EINVAL;
844
845 filp->f_pos += cnt;
846
847 return cnt;
848}
849
850static struct file_operations sched_feat_fops = {
851 .open = sched_feat_open,
852 .read = sched_feat_read,
853 .write = sched_feat_write,
854};
855
856static __init int sched_init_debug(void)
857{
858 debugfs_create_file("sched_features", 0644, NULL, NULL,
859 &sched_feat_fops);
860
861 return 0;
862}
863late_initcall(sched_init_debug);
864
865#endif
866
867#define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
609 868
610/* 869/*
611 * Number of tasks to iterate in a single balance run. 870 * Number of tasks to iterate in a single balance run.
@@ -627,16 +886,52 @@ static __read_mostly int scheduler_running;
627 */ 886 */
628int sysctl_sched_rt_runtime = 950000; 887int sysctl_sched_rt_runtime = 950000;
629 888
630/* 889static inline u64 global_rt_period(void)
631 * single value that denotes runtime == period, ie unlimited time. 890{
632 */ 891 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
633#define RUNTIME_INF ((u64)~0ULL) 892}
893
894static inline u64 global_rt_runtime(void)
895{
896 if (sysctl_sched_rt_period < 0)
897 return RUNTIME_INF;
898
899 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
900}
901
902static const unsigned long long time_sync_thresh = 100000;
903
904static DEFINE_PER_CPU(unsigned long long, time_offset);
905static DEFINE_PER_CPU(unsigned long long, prev_cpu_time);
634 906
635/* 907/*
636 * For kernel-internal use: high-speed (but slightly incorrect) per-cpu 908 * Global lock which we take every now and then to synchronize
637 * clock constructed from sched_clock(): 909 * the CPUs time. This method is not warp-safe, but it's good
910 * enough to synchronize slowly diverging time sources and thus
911 * it's good enough for tracing:
638 */ 912 */
639unsigned long long cpu_clock(int cpu) 913static DEFINE_SPINLOCK(time_sync_lock);
914static unsigned long long prev_global_time;
915
916static unsigned long long __sync_cpu_clock(cycles_t time, int cpu)
917{
918 unsigned long flags;
919
920 spin_lock_irqsave(&time_sync_lock, flags);
921
922 if (time < prev_global_time) {
923 per_cpu(time_offset, cpu) += prev_global_time - time;
924 time = prev_global_time;
925 } else {
926 prev_global_time = time;
927 }
928
929 spin_unlock_irqrestore(&time_sync_lock, flags);
930
931 return time;
932}
933
934static unsigned long long __cpu_clock(int cpu)
640{ 935{
641 unsigned long long now; 936 unsigned long long now;
642 unsigned long flags; 937 unsigned long flags;
@@ -657,6 +952,24 @@ unsigned long long cpu_clock(int cpu)
657 952
658 return now; 953 return now;
659} 954}
955
956/*
957 * For kernel-internal use: high-speed (but slightly incorrect) per-cpu
958 * clock constructed from sched_clock():
959 */
960unsigned long long cpu_clock(int cpu)
961{
962 unsigned long long prev_cpu_time, time, delta_time;
963
964 prev_cpu_time = per_cpu(prev_cpu_time, cpu);
965 time = __cpu_clock(cpu) + per_cpu(time_offset, cpu);
966 delta_time = time-prev_cpu_time;
967
968 if (unlikely(delta_time > time_sync_thresh))
969 time = __sync_cpu_clock(time, cpu);
970
971 return time;
972}
660EXPORT_SYMBOL_GPL(cpu_clock); 973EXPORT_SYMBOL_GPL(cpu_clock);
661 974
662#ifndef prepare_arch_switch 975#ifndef prepare_arch_switch
@@ -1116,6 +1429,9 @@ static void __resched_task(struct task_struct *p, int tif_bit)
1116 */ 1429 */
1117#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y)) 1430#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
1118 1431
1432/*
1433 * delta *= weight / lw
1434 */
1119static unsigned long 1435static unsigned long
1120calc_delta_mine(unsigned long delta_exec, unsigned long weight, 1436calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1121 struct load_weight *lw) 1437 struct load_weight *lw)
@@ -1138,12 +1454,6 @@ calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1138 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX); 1454 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
1139} 1455}
1140 1456
1141static inline unsigned long
1142calc_delta_fair(unsigned long delta_exec, struct load_weight *lw)
1143{
1144 return calc_delta_mine(delta_exec, NICE_0_LOAD, lw);
1145}
1146
1147static inline void update_load_add(struct load_weight *lw, unsigned long inc) 1457static inline void update_load_add(struct load_weight *lw, unsigned long inc)
1148{ 1458{
1149 lw->weight += inc; 1459 lw->weight += inc;
@@ -1241,11 +1551,390 @@ static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
1241static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {} 1551static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
1242#endif 1552#endif
1243 1553
1554static inline void inc_cpu_load(struct rq *rq, unsigned long load)
1555{
1556 update_load_add(&rq->load, load);
1557}
1558
1559static inline void dec_cpu_load(struct rq *rq, unsigned long load)
1560{
1561 update_load_sub(&rq->load, load);
1562}
1563
1244#ifdef CONFIG_SMP 1564#ifdef CONFIG_SMP
1245static unsigned long source_load(int cpu, int type); 1565static unsigned long source_load(int cpu, int type);
1246static unsigned long target_load(int cpu, int type); 1566static unsigned long target_load(int cpu, int type);
1247static unsigned long cpu_avg_load_per_task(int cpu); 1567static unsigned long cpu_avg_load_per_task(int cpu);
1248static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd); 1568static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
1569
1570#ifdef CONFIG_FAIR_GROUP_SCHED
1571
1572/*
1573 * Group load balancing.
1574 *
1575 * We calculate a few balance domain wide aggregate numbers; load and weight.
1576 * Given the pictures below, and assuming each item has equal weight:
1577 *
1578 * root 1 - thread
1579 * / | \ A - group
1580 * A 1 B
1581 * /|\ / \
1582 * C 2 D 3 4
1583 * | |
1584 * 5 6
1585 *
1586 * load:
1587 * A and B get 1/3-rd of the total load. C and D get 1/3-rd of A's 1/3-rd,
1588 * which equals 1/9-th of the total load.
1589 *
1590 * shares:
1591 * The weight of this group on the selected cpus.
1592 *
1593 * rq_weight:
1594 * Direct sum of all the cpu's their rq weight, e.g. A would get 3 while
1595 * B would get 2.
1596 *
1597 * task_weight:
1598 * Part of the rq_weight contributed by tasks; all groups except B would
1599 * get 1, B gets 2.
1600 */
1601
1602static inline struct aggregate_struct *
1603aggregate(struct task_group *tg, struct sched_domain *sd)
1604{
1605 return &tg->cfs_rq[sd->first_cpu]->aggregate;
1606}
1607
1608typedef void (*aggregate_func)(struct task_group *, struct sched_domain *);
1609
1610/*
1611 * Iterate the full tree, calling @down when first entering a node and @up when
1612 * leaving it for the final time.
1613 */
1614static
1615void aggregate_walk_tree(aggregate_func down, aggregate_func up,
1616 struct sched_domain *sd)
1617{
1618 struct task_group *parent, *child;
1619
1620 rcu_read_lock();
1621 parent = &root_task_group;
1622down:
1623 (*down)(parent, sd);
1624 list_for_each_entry_rcu(child, &parent->children, siblings) {
1625 parent = child;
1626 goto down;
1627
1628up:
1629 continue;
1630 }
1631 (*up)(parent, sd);
1632
1633 child = parent;
1634 parent = parent->parent;
1635 if (parent)
1636 goto up;
1637 rcu_read_unlock();
1638}
1639
1640/*
1641 * Calculate the aggregate runqueue weight.
1642 */
1643static
1644void aggregate_group_weight(struct task_group *tg, struct sched_domain *sd)
1645{
1646 unsigned long rq_weight = 0;
1647 unsigned long task_weight = 0;
1648 int i;
1649
1650 for_each_cpu_mask(i, sd->span) {
1651 rq_weight += tg->cfs_rq[i]->load.weight;
1652 task_weight += tg->cfs_rq[i]->task_weight;
1653 }
1654
1655 aggregate(tg, sd)->rq_weight = rq_weight;
1656 aggregate(tg, sd)->task_weight = task_weight;
1657}
1658
1659/*
1660 * Redistribute tg->shares amongst all tg->cfs_rq[]s.
1661 */
1662static void __aggregate_redistribute_shares(struct task_group *tg)
1663{
1664 int i, max_cpu = smp_processor_id();
1665 unsigned long rq_weight = 0;
1666 unsigned long shares, max_shares = 0, shares_rem = tg->shares;
1667
1668 for_each_possible_cpu(i)
1669 rq_weight += tg->cfs_rq[i]->load.weight;
1670
1671 for_each_possible_cpu(i) {
1672 /*
1673 * divide shares proportional to the rq_weights.
1674 */
1675 shares = tg->shares * tg->cfs_rq[i]->load.weight;
1676 shares /= rq_weight + 1;
1677
1678 tg->cfs_rq[i]->shares = shares;
1679
1680 if (shares > max_shares) {
1681 max_shares = shares;
1682 max_cpu = i;
1683 }
1684 shares_rem -= shares;
1685 }
1686
1687 /*
1688 * Ensure it all adds up to tg->shares; we can loose a few
1689 * due to rounding down when computing the per-cpu shares.
1690 */
1691 if (shares_rem)
1692 tg->cfs_rq[max_cpu]->shares += shares_rem;
1693}
1694
1695/*
1696 * Compute the weight of this group on the given cpus.
1697 */
1698static
1699void aggregate_group_shares(struct task_group *tg, struct sched_domain *sd)
1700{
1701 unsigned long shares = 0;
1702 int i;
1703
1704again:
1705 for_each_cpu_mask(i, sd->span)
1706 shares += tg->cfs_rq[i]->shares;
1707
1708 /*
1709 * When the span doesn't have any shares assigned, but does have
1710 * tasks to run do a machine wide rebalance (should be rare).
1711 */
1712 if (unlikely(!shares && aggregate(tg, sd)->rq_weight)) {
1713 __aggregate_redistribute_shares(tg);
1714 goto again;
1715 }
1716
1717 aggregate(tg, sd)->shares = shares;
1718}
1719
1720/*
1721 * Compute the load fraction assigned to this group, relies on the aggregate
1722 * weight and this group's parent's load, i.e. top-down.
1723 */
1724static
1725void aggregate_group_load(struct task_group *tg, struct sched_domain *sd)
1726{
1727 unsigned long load;
1728
1729 if (!tg->parent) {
1730 int i;
1731
1732 load = 0;
1733 for_each_cpu_mask(i, sd->span)
1734 load += cpu_rq(i)->load.weight;
1735
1736 } else {
1737 load = aggregate(tg->parent, sd)->load;
1738
1739 /*
1740 * shares is our weight in the parent's rq so
1741 * shares/parent->rq_weight gives our fraction of the load
1742 */
1743 load *= aggregate(tg, sd)->shares;
1744 load /= aggregate(tg->parent, sd)->rq_weight + 1;
1745 }
1746
1747 aggregate(tg, sd)->load = load;
1748}
1749
1750static void __set_se_shares(struct sched_entity *se, unsigned long shares);
1751
1752/*
1753 * Calculate and set the cpu's group shares.
1754 */
1755static void
1756__update_group_shares_cpu(struct task_group *tg, struct sched_domain *sd,
1757 int tcpu)
1758{
1759 int boost = 0;
1760 unsigned long shares;
1761 unsigned long rq_weight;
1762
1763 if (!tg->se[tcpu])
1764 return;
1765
1766 rq_weight = tg->cfs_rq[tcpu]->load.weight;
1767
1768 /*
1769 * If there are currently no tasks on the cpu pretend there is one of
1770 * average load so that when a new task gets to run here it will not
1771 * get delayed by group starvation.
1772 */
1773 if (!rq_weight) {
1774 boost = 1;
1775 rq_weight = NICE_0_LOAD;
1776 }
1777
1778 /*
1779 * \Sum shares * rq_weight
1780 * shares = -----------------------
1781 * \Sum rq_weight
1782 *
1783 */
1784 shares = aggregate(tg, sd)->shares * rq_weight;
1785 shares /= aggregate(tg, sd)->rq_weight + 1;
1786
1787 /*
1788 * record the actual number of shares, not the boosted amount.
1789 */
1790 tg->cfs_rq[tcpu]->shares = boost ? 0 : shares;
1791
1792 if (shares < MIN_SHARES)
1793 shares = MIN_SHARES;
1794
1795 __set_se_shares(tg->se[tcpu], shares);
1796}
1797
1798/*
1799 * Re-adjust the weights on the cpu the task came from and on the cpu the
1800 * task went to.
1801 */
1802static void
1803__move_group_shares(struct task_group *tg, struct sched_domain *sd,
1804 int scpu, int dcpu)
1805{
1806 unsigned long shares;
1807
1808 shares = tg->cfs_rq[scpu]->shares + tg->cfs_rq[dcpu]->shares;
1809
1810 __update_group_shares_cpu(tg, sd, scpu);
1811 __update_group_shares_cpu(tg, sd, dcpu);
1812
1813 /*
1814 * ensure we never loose shares due to rounding errors in the
1815 * above redistribution.
1816 */
1817 shares -= tg->cfs_rq[scpu]->shares + tg->cfs_rq[dcpu]->shares;
1818 if (shares)
1819 tg->cfs_rq[dcpu]->shares += shares;
1820}
1821
1822/*
1823 * Because changing a group's shares changes the weight of the super-group
1824 * we need to walk up the tree and change all shares until we hit the root.
1825 */
1826static void
1827move_group_shares(struct task_group *tg, struct sched_domain *sd,
1828 int scpu, int dcpu)
1829{
1830 while (tg) {
1831 __move_group_shares(tg, sd, scpu, dcpu);
1832 tg = tg->parent;
1833 }
1834}
1835
1836static
1837void aggregate_group_set_shares(struct task_group *tg, struct sched_domain *sd)
1838{
1839 unsigned long shares = aggregate(tg, sd)->shares;
1840 int i;
1841
1842 for_each_cpu_mask(i, sd->span) {
1843 struct rq *rq = cpu_rq(i);
1844 unsigned long flags;
1845
1846 spin_lock_irqsave(&rq->lock, flags);
1847 __update_group_shares_cpu(tg, sd, i);
1848 spin_unlock_irqrestore(&rq->lock, flags);
1849 }
1850
1851 aggregate_group_shares(tg, sd);
1852
1853 /*
1854 * ensure we never loose shares due to rounding errors in the
1855 * above redistribution.
1856 */
1857 shares -= aggregate(tg, sd)->shares;
1858 if (shares) {
1859 tg->cfs_rq[sd->first_cpu]->shares += shares;
1860 aggregate(tg, sd)->shares += shares;
1861 }
1862}
1863
1864/*
1865 * Calculate the accumulative weight and recursive load of each task group
1866 * while walking down the tree.
1867 */
1868static
1869void aggregate_get_down(struct task_group *tg, struct sched_domain *sd)
1870{
1871 aggregate_group_weight(tg, sd);
1872 aggregate_group_shares(tg, sd);
1873 aggregate_group_load(tg, sd);
1874}
1875
1876/*
1877 * Rebalance the cpu shares while walking back up the tree.
1878 */
1879static
1880void aggregate_get_up(struct task_group *tg, struct sched_domain *sd)
1881{
1882 aggregate_group_set_shares(tg, sd);
1883}
1884
1885static DEFINE_PER_CPU(spinlock_t, aggregate_lock);
1886
1887static void __init init_aggregate(void)
1888{
1889 int i;
1890
1891 for_each_possible_cpu(i)
1892 spin_lock_init(&per_cpu(aggregate_lock, i));
1893}
1894
1895static int get_aggregate(struct sched_domain *sd)
1896{
1897 if (!spin_trylock(&per_cpu(aggregate_lock, sd->first_cpu)))
1898 return 0;
1899
1900 aggregate_walk_tree(aggregate_get_down, aggregate_get_up, sd);
1901 return 1;
1902}
1903
1904static void put_aggregate(struct sched_domain *sd)
1905{
1906 spin_unlock(&per_cpu(aggregate_lock, sd->first_cpu));
1907}
1908
1909static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1910{
1911 cfs_rq->shares = shares;
1912}
1913
1914#else
1915
1916static inline void init_aggregate(void)
1917{
1918}
1919
1920static inline int get_aggregate(struct sched_domain *sd)
1921{
1922 return 0;
1923}
1924
1925static inline void put_aggregate(struct sched_domain *sd)
1926{
1927}
1928#endif
1929
1930#else /* CONFIG_SMP */
1931
1932#ifdef CONFIG_FAIR_GROUP_SCHED
1933static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1934{
1935}
1936#endif
1937
1249#endif /* CONFIG_SMP */ 1938#endif /* CONFIG_SMP */
1250 1939
1251#include "sched_stats.h" 1940#include "sched_stats.h"
@@ -1258,26 +1947,14 @@ static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
1258 1947
1259#define sched_class_highest (&rt_sched_class) 1948#define sched_class_highest (&rt_sched_class)
1260 1949
1261static inline void inc_load(struct rq *rq, const struct task_struct *p) 1950static void inc_nr_running(struct rq *rq)
1262{
1263 update_load_add(&rq->load, p->se.load.weight);
1264}
1265
1266static inline void dec_load(struct rq *rq, const struct task_struct *p)
1267{
1268 update_load_sub(&rq->load, p->se.load.weight);
1269}
1270
1271static void inc_nr_running(struct task_struct *p, struct rq *rq)
1272{ 1951{
1273 rq->nr_running++; 1952 rq->nr_running++;
1274 inc_load(rq, p);
1275} 1953}
1276 1954
1277static void dec_nr_running(struct task_struct *p, struct rq *rq) 1955static void dec_nr_running(struct rq *rq)
1278{ 1956{
1279 rq->nr_running--; 1957 rq->nr_running--;
1280 dec_load(rq, p);
1281} 1958}
1282 1959
1283static void set_load_weight(struct task_struct *p) 1960static void set_load_weight(struct task_struct *p)
@@ -1369,7 +2046,7 @@ static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
1369 rq->nr_uninterruptible--; 2046 rq->nr_uninterruptible--;
1370 2047
1371 enqueue_task(rq, p, wakeup); 2048 enqueue_task(rq, p, wakeup);
1372 inc_nr_running(p, rq); 2049 inc_nr_running(rq);
1373} 2050}
1374 2051
1375/* 2052/*
@@ -1381,7 +2058,7 @@ static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
1381 rq->nr_uninterruptible++; 2058 rq->nr_uninterruptible++;
1382 2059
1383 dequeue_task(rq, p, sleep); 2060 dequeue_task(rq, p, sleep);
1384 dec_nr_running(p, rq); 2061 dec_nr_running(rq);
1385} 2062}
1386 2063
1387/** 2064/**
@@ -1438,7 +2115,7 @@ task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
1438 /* 2115 /*
1439 * Buddy candidates are cache hot: 2116 * Buddy candidates are cache hot:
1440 */ 2117 */
1441 if (&p->se == cfs_rq_of(&p->se)->next) 2118 if (sched_feat(CACHE_HOT_BUDDY) && (&p->se == cfs_rq_of(&p->se)->next))
1442 return 1; 2119 return 1;
1443 2120
1444 if (p->sched_class != &fair_sched_class) 2121 if (p->sched_class != &fair_sched_class)
@@ -1728,17 +2405,17 @@ find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
1728 * find_idlest_cpu - find the idlest cpu among the cpus in group. 2405 * find_idlest_cpu - find the idlest cpu among the cpus in group.
1729 */ 2406 */
1730static int 2407static int
1731find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu) 2408find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu,
2409 cpumask_t *tmp)
1732{ 2410{
1733 cpumask_t tmp;
1734 unsigned long load, min_load = ULONG_MAX; 2411 unsigned long load, min_load = ULONG_MAX;
1735 int idlest = -1; 2412 int idlest = -1;
1736 int i; 2413 int i;
1737 2414
1738 /* Traverse only the allowed CPUs */ 2415 /* Traverse only the allowed CPUs */
1739 cpus_and(tmp, group->cpumask, p->cpus_allowed); 2416 cpus_and(*tmp, group->cpumask, p->cpus_allowed);
1740 2417
1741 for_each_cpu_mask(i, tmp) { 2418 for_each_cpu_mask(i, *tmp) {
1742 load = weighted_cpuload(i); 2419 load = weighted_cpuload(i);
1743 2420
1744 if (load < min_load || (load == min_load && i == this_cpu)) { 2421 if (load < min_load || (load == min_load && i == this_cpu)) {
@@ -1777,7 +2454,7 @@ static int sched_balance_self(int cpu, int flag)
1777 } 2454 }
1778 2455
1779 while (sd) { 2456 while (sd) {
1780 cpumask_t span; 2457 cpumask_t span, tmpmask;
1781 struct sched_group *group; 2458 struct sched_group *group;
1782 int new_cpu, weight; 2459 int new_cpu, weight;
1783 2460
@@ -1793,7 +2470,7 @@ static int sched_balance_self(int cpu, int flag)
1793 continue; 2470 continue;
1794 } 2471 }
1795 2472
1796 new_cpu = find_idlest_cpu(group, t, cpu); 2473 new_cpu = find_idlest_cpu(group, t, cpu, &tmpmask);
1797 if (new_cpu == -1 || new_cpu == cpu) { 2474 if (new_cpu == -1 || new_cpu == cpu) {
1798 /* Now try balancing at a lower domain level of cpu */ 2475 /* Now try balancing at a lower domain level of cpu */
1799 sd = sd->child; 2476 sd = sd->child;
@@ -1839,6 +2516,9 @@ static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
1839 long old_state; 2516 long old_state;
1840 struct rq *rq; 2517 struct rq *rq;
1841 2518
2519 if (!sched_feat(SYNC_WAKEUPS))
2520 sync = 0;
2521
1842 smp_wmb(); 2522 smp_wmb();
1843 rq = task_rq_lock(p, &flags); 2523 rq = task_rq_lock(p, &flags);
1844 old_state = p->state; 2524 old_state = p->state;
@@ -1955,6 +2635,7 @@ static void __sched_fork(struct task_struct *p)
1955 2635
1956 INIT_LIST_HEAD(&p->rt.run_list); 2636 INIT_LIST_HEAD(&p->rt.run_list);
1957 p->se.on_rq = 0; 2637 p->se.on_rq = 0;
2638 INIT_LIST_HEAD(&p->se.group_node);
1958 2639
1959#ifdef CONFIG_PREEMPT_NOTIFIERS 2640#ifdef CONFIG_PREEMPT_NOTIFIERS
1960 INIT_HLIST_HEAD(&p->preempt_notifiers); 2641 INIT_HLIST_HEAD(&p->preempt_notifiers);
@@ -2030,7 +2711,7 @@ void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
2030 * management (if any): 2711 * management (if any):
2031 */ 2712 */
2032 p->sched_class->task_new(rq, p); 2713 p->sched_class->task_new(rq, p);
2033 inc_nr_running(p, rq); 2714 inc_nr_running(rq);
2034 } 2715 }
2035 check_preempt_curr(rq, p); 2716 check_preempt_curr(rq, p);
2036#ifdef CONFIG_SMP 2717#ifdef CONFIG_SMP
@@ -2674,7 +3355,7 @@ static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
2674static struct sched_group * 3355static struct sched_group *
2675find_busiest_group(struct sched_domain *sd, int this_cpu, 3356find_busiest_group(struct sched_domain *sd, int this_cpu,
2676 unsigned long *imbalance, enum cpu_idle_type idle, 3357 unsigned long *imbalance, enum cpu_idle_type idle,
2677 int *sd_idle, cpumask_t *cpus, int *balance) 3358 int *sd_idle, const cpumask_t *cpus, int *balance)
2678{ 3359{
2679 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups; 3360 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
2680 unsigned long max_load, avg_load, total_load, this_load, total_pwr; 3361 unsigned long max_load, avg_load, total_load, this_load, total_pwr;
@@ -2975,7 +3656,7 @@ ret:
2975 */ 3656 */
2976static struct rq * 3657static struct rq *
2977find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle, 3658find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
2978 unsigned long imbalance, cpumask_t *cpus) 3659 unsigned long imbalance, const cpumask_t *cpus)
2979{ 3660{
2980 struct rq *busiest = NULL, *rq; 3661 struct rq *busiest = NULL, *rq;
2981 unsigned long max_load = 0; 3662 unsigned long max_load = 0;
@@ -3014,14 +3695,18 @@ find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
3014 */ 3695 */
3015static int load_balance(int this_cpu, struct rq *this_rq, 3696static int load_balance(int this_cpu, struct rq *this_rq,
3016 struct sched_domain *sd, enum cpu_idle_type idle, 3697 struct sched_domain *sd, enum cpu_idle_type idle,
3017 int *balance) 3698 int *balance, cpumask_t *cpus)
3018{ 3699{
3019 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0; 3700 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
3020 struct sched_group *group; 3701 struct sched_group *group;
3021 unsigned long imbalance; 3702 unsigned long imbalance;
3022 struct rq *busiest; 3703 struct rq *busiest;
3023 cpumask_t cpus = CPU_MASK_ALL;
3024 unsigned long flags; 3704 unsigned long flags;
3705 int unlock_aggregate;
3706
3707 cpus_setall(*cpus);
3708
3709 unlock_aggregate = get_aggregate(sd);
3025 3710
3026 /* 3711 /*
3027 * When power savings policy is enabled for the parent domain, idle 3712 * When power savings policy is enabled for the parent domain, idle
@@ -3037,7 +3722,7 @@ static int load_balance(int this_cpu, struct rq *this_rq,
3037 3722
3038redo: 3723redo:
3039 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle, 3724 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
3040 &cpus, balance); 3725 cpus, balance);
3041 3726
3042 if (*balance == 0) 3727 if (*balance == 0)
3043 goto out_balanced; 3728 goto out_balanced;
@@ -3047,7 +3732,7 @@ redo:
3047 goto out_balanced; 3732 goto out_balanced;
3048 } 3733 }
3049 3734
3050 busiest = find_busiest_queue(group, idle, imbalance, &cpus); 3735 busiest = find_busiest_queue(group, idle, imbalance, cpus);
3051 if (!busiest) { 3736 if (!busiest) {
3052 schedstat_inc(sd, lb_nobusyq[idle]); 3737 schedstat_inc(sd, lb_nobusyq[idle]);
3053 goto out_balanced; 3738 goto out_balanced;
@@ -3080,8 +3765,8 @@ redo:
3080 3765
3081 /* All tasks on this runqueue were pinned by CPU affinity */ 3766 /* All tasks on this runqueue were pinned by CPU affinity */
3082 if (unlikely(all_pinned)) { 3767 if (unlikely(all_pinned)) {
3083 cpu_clear(cpu_of(busiest), cpus); 3768 cpu_clear(cpu_of(busiest), *cpus);
3084 if (!cpus_empty(cpus)) 3769 if (!cpus_empty(*cpus))
3085 goto redo; 3770 goto redo;
3086 goto out_balanced; 3771 goto out_balanced;
3087 } 3772 }
@@ -3138,8 +3823,9 @@ redo:
3138 3823
3139 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER && 3824 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
3140 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) 3825 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
3141 return -1; 3826 ld_moved = -1;
3142 return ld_moved; 3827
3828 goto out;
3143 3829
3144out_balanced: 3830out_balanced:
3145 schedstat_inc(sd, lb_balanced[idle]); 3831 schedstat_inc(sd, lb_balanced[idle]);
@@ -3154,8 +3840,13 @@ out_one_pinned:
3154 3840
3155 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER && 3841 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
3156 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) 3842 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
3157 return -1; 3843 ld_moved = -1;
3158 return 0; 3844 else
3845 ld_moved = 0;
3846out:
3847 if (unlock_aggregate)
3848 put_aggregate(sd);
3849 return ld_moved;
3159} 3850}
3160 3851
3161/* 3852/*
@@ -3166,7 +3857,8 @@ out_one_pinned:
3166 * this_rq is locked. 3857 * this_rq is locked.
3167 */ 3858 */
3168static int 3859static int
3169load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd) 3860load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd,
3861 cpumask_t *cpus)
3170{ 3862{
3171 struct sched_group *group; 3863 struct sched_group *group;
3172 struct rq *busiest = NULL; 3864 struct rq *busiest = NULL;
@@ -3174,7 +3866,8 @@ load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd)
3174 int ld_moved = 0; 3866 int ld_moved = 0;
3175 int sd_idle = 0; 3867 int sd_idle = 0;
3176 int all_pinned = 0; 3868 int all_pinned = 0;
3177 cpumask_t cpus = CPU_MASK_ALL; 3869
3870 cpus_setall(*cpus);
3178 3871
3179 /* 3872 /*
3180 * When power savings policy is enabled for the parent domain, idle 3873 * When power savings policy is enabled for the parent domain, idle
@@ -3189,14 +3882,13 @@ load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd)
3189 schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]); 3882 schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
3190redo: 3883redo:
3191 group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE, 3884 group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
3192 &sd_idle, &cpus, NULL); 3885 &sd_idle, cpus, NULL);
3193 if (!group) { 3886 if (!group) {
3194 schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]); 3887 schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
3195 goto out_balanced; 3888 goto out_balanced;
3196 } 3889 }
3197 3890
3198 busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, 3891 busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, cpus);
3199 &cpus);
3200 if (!busiest) { 3892 if (!busiest) {
3201 schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]); 3893 schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
3202 goto out_balanced; 3894 goto out_balanced;
@@ -3218,8 +3910,8 @@ redo:
3218 spin_unlock(&busiest->lock); 3910 spin_unlock(&busiest->lock);
3219 3911
3220 if (unlikely(all_pinned)) { 3912 if (unlikely(all_pinned)) {
3221 cpu_clear(cpu_of(busiest), cpus); 3913 cpu_clear(cpu_of(busiest), *cpus);
3222 if (!cpus_empty(cpus)) 3914 if (!cpus_empty(*cpus))
3223 goto redo; 3915 goto redo;
3224 } 3916 }
3225 } 3917 }
@@ -3253,6 +3945,7 @@ static void idle_balance(int this_cpu, struct rq *this_rq)
3253 struct sched_domain *sd; 3945 struct sched_domain *sd;
3254 int pulled_task = -1; 3946 int pulled_task = -1;
3255 unsigned long next_balance = jiffies + HZ; 3947 unsigned long next_balance = jiffies + HZ;
3948 cpumask_t tmpmask;
3256 3949
3257 for_each_domain(this_cpu, sd) { 3950 for_each_domain(this_cpu, sd) {
3258 unsigned long interval; 3951 unsigned long interval;
@@ -3262,8 +3955,8 @@ static void idle_balance(int this_cpu, struct rq *this_rq)
3262 3955
3263 if (sd->flags & SD_BALANCE_NEWIDLE) 3956 if (sd->flags & SD_BALANCE_NEWIDLE)
3264 /* If we've pulled tasks over stop searching: */ 3957 /* If we've pulled tasks over stop searching: */
3265 pulled_task = load_balance_newidle(this_cpu, 3958 pulled_task = load_balance_newidle(this_cpu, this_rq,
3266 this_rq, sd); 3959 sd, &tmpmask);
3267 3960
3268 interval = msecs_to_jiffies(sd->balance_interval); 3961 interval = msecs_to_jiffies(sd->balance_interval);
3269 if (time_after(next_balance, sd->last_balance + interval)) 3962 if (time_after(next_balance, sd->last_balance + interval))
@@ -3422,6 +4115,7 @@ static void rebalance_domains(int cpu, enum cpu_idle_type idle)
3422 /* Earliest time when we have to do rebalance again */ 4115 /* Earliest time when we have to do rebalance again */
3423 unsigned long next_balance = jiffies + 60*HZ; 4116 unsigned long next_balance = jiffies + 60*HZ;
3424 int update_next_balance = 0; 4117 int update_next_balance = 0;
4118 cpumask_t tmp;
3425 4119
3426 for_each_domain(cpu, sd) { 4120 for_each_domain(cpu, sd) {
3427 if (!(sd->flags & SD_LOAD_BALANCE)) 4121 if (!(sd->flags & SD_LOAD_BALANCE))
@@ -3445,7 +4139,7 @@ static void rebalance_domains(int cpu, enum cpu_idle_type idle)
3445 } 4139 }
3446 4140
3447 if (time_after_eq(jiffies, sd->last_balance + interval)) { 4141 if (time_after_eq(jiffies, sd->last_balance + interval)) {
3448 if (load_balance(cpu, rq, sd, idle, &balance)) { 4142 if (load_balance(cpu, rq, sd, idle, &balance, &tmp)) {
3449 /* 4143 /*
3450 * We've pulled tasks over so either we're no 4144 * We've pulled tasks over so either we're no
3451 * longer idle, or one of our SMT siblings is 4145 * longer idle, or one of our SMT siblings is
@@ -3561,7 +4255,7 @@ static inline void trigger_load_balance(struct rq *rq, int cpu)
3561 */ 4255 */
3562 int ilb = first_cpu(nohz.cpu_mask); 4256 int ilb = first_cpu(nohz.cpu_mask);
3563 4257
3564 if (ilb != NR_CPUS) 4258 if (ilb < nr_cpu_ids)
3565 resched_cpu(ilb); 4259 resched_cpu(ilb);
3566 } 4260 }
3567 } 4261 }
@@ -3765,9 +4459,9 @@ void scheduler_tick(void)
3765 rq->clock_underflows++; 4459 rq->clock_underflows++;
3766 } 4460 }
3767 rq->tick_timestamp = rq->clock; 4461 rq->tick_timestamp = rq->clock;
4462 update_last_tick_seen(rq);
3768 update_cpu_load(rq); 4463 update_cpu_load(rq);
3769 curr->sched_class->task_tick(rq, curr, 0); 4464 curr->sched_class->task_tick(rq, curr, 0);
3770 update_sched_rt_period(rq);
3771 spin_unlock(&rq->lock); 4465 spin_unlock(&rq->lock);
3772 4466
3773#ifdef CONFIG_SMP 4467#ifdef CONFIG_SMP
@@ -4367,10 +5061,8 @@ void set_user_nice(struct task_struct *p, long nice)
4367 goto out_unlock; 5061 goto out_unlock;
4368 } 5062 }
4369 on_rq = p->se.on_rq; 5063 on_rq = p->se.on_rq;
4370 if (on_rq) { 5064 if (on_rq)
4371 dequeue_task(rq, p, 0); 5065 dequeue_task(rq, p, 0);
4372 dec_load(rq, p);
4373 }
4374 5066
4375 p->static_prio = NICE_TO_PRIO(nice); 5067 p->static_prio = NICE_TO_PRIO(nice);
4376 set_load_weight(p); 5068 set_load_weight(p);
@@ -4380,7 +5072,6 @@ void set_user_nice(struct task_struct *p, long nice)
4380 5072
4381 if (on_rq) { 5073 if (on_rq) {
4382 enqueue_task(rq, p, 0); 5074 enqueue_task(rq, p, 0);
4383 inc_load(rq, p);
4384 /* 5075 /*
4385 * If the task increased its priority or is running and 5076 * If the task increased its priority or is running and
4386 * lowered its priority, then reschedule its CPU: 5077 * lowered its priority, then reschedule its CPU:
@@ -4602,7 +5293,7 @@ recheck:
4602 * Do not allow realtime tasks into groups that have no runtime 5293 * Do not allow realtime tasks into groups that have no runtime
4603 * assigned. 5294 * assigned.
4604 */ 5295 */
4605 if (rt_policy(policy) && task_group(p)->rt_runtime == 0) 5296 if (rt_policy(policy) && task_group(p)->rt_bandwidth.rt_runtime == 0)
4606 return -EPERM; 5297 return -EPERM;
4607#endif 5298#endif
4608 5299
@@ -4764,9 +5455,10 @@ out_unlock:
4764 return retval; 5455 return retval;
4765} 5456}
4766 5457
4767long sched_setaffinity(pid_t pid, cpumask_t new_mask) 5458long sched_setaffinity(pid_t pid, const cpumask_t *in_mask)
4768{ 5459{
4769 cpumask_t cpus_allowed; 5460 cpumask_t cpus_allowed;
5461 cpumask_t new_mask = *in_mask;
4770 struct task_struct *p; 5462 struct task_struct *p;
4771 int retval; 5463 int retval;
4772 5464
@@ -4797,13 +5489,13 @@ long sched_setaffinity(pid_t pid, cpumask_t new_mask)
4797 if (retval) 5489 if (retval)
4798 goto out_unlock; 5490 goto out_unlock;
4799 5491
4800 cpus_allowed = cpuset_cpus_allowed(p); 5492 cpuset_cpus_allowed(p, &cpus_allowed);
4801 cpus_and(new_mask, new_mask, cpus_allowed); 5493 cpus_and(new_mask, new_mask, cpus_allowed);
4802 again: 5494 again:
4803 retval = set_cpus_allowed(p, new_mask); 5495 retval = set_cpus_allowed_ptr(p, &new_mask);
4804 5496
4805 if (!retval) { 5497 if (!retval) {
4806 cpus_allowed = cpuset_cpus_allowed(p); 5498 cpuset_cpus_allowed(p, &cpus_allowed);
4807 if (!cpus_subset(new_mask, cpus_allowed)) { 5499 if (!cpus_subset(new_mask, cpus_allowed)) {
4808 /* 5500 /*
4809 * We must have raced with a concurrent cpuset 5501 * We must have raced with a concurrent cpuset
@@ -4847,7 +5539,7 @@ asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
4847 if (retval) 5539 if (retval)
4848 return retval; 5540 return retval;
4849 5541
4850 return sched_setaffinity(pid, new_mask); 5542 return sched_setaffinity(pid, &new_mask);
4851} 5543}
4852 5544
4853/* 5545/*
@@ -5309,7 +6001,6 @@ static inline void sched_init_granularity(void)
5309 sysctl_sched_latency = limit; 6001 sysctl_sched_latency = limit;
5310 6002
5311 sysctl_sched_wakeup_granularity *= factor; 6003 sysctl_sched_wakeup_granularity *= factor;
5312 sysctl_sched_batch_wakeup_granularity *= factor;
5313} 6004}
5314 6005
5315#ifdef CONFIG_SMP 6006#ifdef CONFIG_SMP
@@ -5338,7 +6029,7 @@ static inline void sched_init_granularity(void)
5338 * task must not exit() & deallocate itself prematurely. The 6029 * task must not exit() & deallocate itself prematurely. The
5339 * call is not atomic; no spinlocks may be held. 6030 * call is not atomic; no spinlocks may be held.
5340 */ 6031 */
5341int set_cpus_allowed(struct task_struct *p, cpumask_t new_mask) 6032int set_cpus_allowed_ptr(struct task_struct *p, const cpumask_t *new_mask)
5342{ 6033{
5343 struct migration_req req; 6034 struct migration_req req;
5344 unsigned long flags; 6035 unsigned long flags;
@@ -5346,23 +6037,23 @@ int set_cpus_allowed(struct task_struct *p, cpumask_t new_mask)
5346 int ret = 0; 6037 int ret = 0;
5347 6038
5348 rq = task_rq_lock(p, &flags); 6039 rq = task_rq_lock(p, &flags);
5349 if (!cpus_intersects(new_mask, cpu_online_map)) { 6040 if (!cpus_intersects(*new_mask, cpu_online_map)) {
5350 ret = -EINVAL; 6041 ret = -EINVAL;
5351 goto out; 6042 goto out;
5352 } 6043 }
5353 6044
5354 if (p->sched_class->set_cpus_allowed) 6045 if (p->sched_class->set_cpus_allowed)
5355 p->sched_class->set_cpus_allowed(p, &new_mask); 6046 p->sched_class->set_cpus_allowed(p, new_mask);
5356 else { 6047 else {
5357 p->cpus_allowed = new_mask; 6048 p->cpus_allowed = *new_mask;
5358 p->rt.nr_cpus_allowed = cpus_weight(new_mask); 6049 p->rt.nr_cpus_allowed = cpus_weight(*new_mask);
5359 } 6050 }
5360 6051
5361 /* Can the task run on the task's current CPU? If so, we're done */ 6052 /* Can the task run on the task's current CPU? If so, we're done */
5362 if (cpu_isset(task_cpu(p), new_mask)) 6053 if (cpu_isset(task_cpu(p), *new_mask))
5363 goto out; 6054 goto out;
5364 6055
5365 if (migrate_task(p, any_online_cpu(new_mask), &req)) { 6056 if (migrate_task(p, any_online_cpu(*new_mask), &req)) {
5366 /* Need help from migration thread: drop lock and wait. */ 6057 /* Need help from migration thread: drop lock and wait. */
5367 task_rq_unlock(rq, &flags); 6058 task_rq_unlock(rq, &flags);
5368 wake_up_process(rq->migration_thread); 6059 wake_up_process(rq->migration_thread);
@@ -5375,7 +6066,7 @@ out:
5375 6066
5376 return ret; 6067 return ret;
5377} 6068}
5378EXPORT_SYMBOL_GPL(set_cpus_allowed); 6069EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
5379 6070
5380/* 6071/*
5381 * Move (not current) task off this cpu, onto dest cpu. We're doing 6072 * Move (not current) task off this cpu, onto dest cpu. We're doing
@@ -5513,12 +6204,14 @@ static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
5513 dest_cpu = any_online_cpu(mask); 6204 dest_cpu = any_online_cpu(mask);
5514 6205
5515 /* On any allowed CPU? */ 6206 /* On any allowed CPU? */
5516 if (dest_cpu == NR_CPUS) 6207 if (dest_cpu >= nr_cpu_ids)
5517 dest_cpu = any_online_cpu(p->cpus_allowed); 6208 dest_cpu = any_online_cpu(p->cpus_allowed);
5518 6209
5519 /* No more Mr. Nice Guy. */ 6210 /* No more Mr. Nice Guy. */
5520 if (dest_cpu == NR_CPUS) { 6211 if (dest_cpu >= nr_cpu_ids) {
5521 cpumask_t cpus_allowed = cpuset_cpus_allowed_locked(p); 6212 cpumask_t cpus_allowed;
6213
6214 cpuset_cpus_allowed_locked(p, &cpus_allowed);
5522 /* 6215 /*
5523 * Try to stay on the same cpuset, where the 6216 * Try to stay on the same cpuset, where the
5524 * current cpuset may be a subset of all cpus. 6217 * current cpuset may be a subset of all cpus.
@@ -5554,7 +6247,7 @@ static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
5554 */ 6247 */
5555static void migrate_nr_uninterruptible(struct rq *rq_src) 6248static void migrate_nr_uninterruptible(struct rq *rq_src)
5556{ 6249{
5557 struct rq *rq_dest = cpu_rq(any_online_cpu(CPU_MASK_ALL)); 6250 struct rq *rq_dest = cpu_rq(any_online_cpu(*CPU_MASK_ALL_PTR));
5558 unsigned long flags; 6251 unsigned long flags;
5559 6252
5560 local_irq_save(flags); 6253 local_irq_save(flags);
@@ -5966,20 +6659,16 @@ void __init migration_init(void)
5966 6659
5967#ifdef CONFIG_SMP 6660#ifdef CONFIG_SMP
5968 6661
5969/* Number of possible processor ids */
5970int nr_cpu_ids __read_mostly = NR_CPUS;
5971EXPORT_SYMBOL(nr_cpu_ids);
5972
5973#ifdef CONFIG_SCHED_DEBUG 6662#ifdef CONFIG_SCHED_DEBUG
5974 6663
5975static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level) 6664static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
6665 cpumask_t *groupmask)
5976{ 6666{
5977 struct sched_group *group = sd->groups; 6667 struct sched_group *group = sd->groups;
5978 cpumask_t groupmask; 6668 char str[256];
5979 char str[NR_CPUS];
5980 6669
5981 cpumask_scnprintf(str, NR_CPUS, sd->span); 6670 cpulist_scnprintf(str, sizeof(str), sd->span);
5982 cpus_clear(groupmask); 6671 cpus_clear(*groupmask);
5983 6672
5984 printk(KERN_DEBUG "%*s domain %d: ", level, "", level); 6673 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
5985 6674
@@ -6023,25 +6712,25 @@ static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level)
6023 break; 6712 break;
6024 } 6713 }
6025 6714
6026 if (cpus_intersects(groupmask, group->cpumask)) { 6715 if (cpus_intersects(*groupmask, group->cpumask)) {
6027 printk(KERN_CONT "\n"); 6716 printk(KERN_CONT "\n");
6028 printk(KERN_ERR "ERROR: repeated CPUs\n"); 6717 printk(KERN_ERR "ERROR: repeated CPUs\n");
6029 break; 6718 break;
6030 } 6719 }
6031 6720
6032 cpus_or(groupmask, groupmask, group->cpumask); 6721 cpus_or(*groupmask, *groupmask, group->cpumask);
6033 6722
6034 cpumask_scnprintf(str, NR_CPUS, group->cpumask); 6723 cpulist_scnprintf(str, sizeof(str), group->cpumask);
6035 printk(KERN_CONT " %s", str); 6724 printk(KERN_CONT " %s", str);
6036 6725
6037 group = group->next; 6726 group = group->next;
6038 } while (group != sd->groups); 6727 } while (group != sd->groups);
6039 printk(KERN_CONT "\n"); 6728 printk(KERN_CONT "\n");
6040 6729
6041 if (!cpus_equal(sd->span, groupmask)) 6730 if (!cpus_equal(sd->span, *groupmask))
6042 printk(KERN_ERR "ERROR: groups don't span domain->span\n"); 6731 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
6043 6732
6044 if (sd->parent && !cpus_subset(groupmask, sd->parent->span)) 6733 if (sd->parent && !cpus_subset(*groupmask, sd->parent->span))
6045 printk(KERN_ERR "ERROR: parent span is not a superset " 6734 printk(KERN_ERR "ERROR: parent span is not a superset "
6046 "of domain->span\n"); 6735 "of domain->span\n");
6047 return 0; 6736 return 0;
@@ -6049,6 +6738,7 @@ static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level)
6049 6738
6050static void sched_domain_debug(struct sched_domain *sd, int cpu) 6739static void sched_domain_debug(struct sched_domain *sd, int cpu)
6051{ 6740{
6741 cpumask_t *groupmask;
6052 int level = 0; 6742 int level = 0;
6053 6743
6054 if (!sd) { 6744 if (!sd) {
@@ -6058,14 +6748,21 @@ static void sched_domain_debug(struct sched_domain *sd, int cpu)
6058 6748
6059 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu); 6749 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
6060 6750
6751 groupmask = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
6752 if (!groupmask) {
6753 printk(KERN_DEBUG "Cannot load-balance (out of memory)\n");
6754 return;
6755 }
6756
6061 for (;;) { 6757 for (;;) {
6062 if (sched_domain_debug_one(sd, cpu, level)) 6758 if (sched_domain_debug_one(sd, cpu, level, groupmask))
6063 break; 6759 break;
6064 level++; 6760 level++;
6065 sd = sd->parent; 6761 sd = sd->parent;
6066 if (!sd) 6762 if (!sd)
6067 break; 6763 break;
6068 } 6764 }
6765 kfree(groupmask);
6069} 6766}
6070#else 6767#else
6071# define sched_domain_debug(sd, cpu) do { } while (0) 6768# define sched_domain_debug(sd, cpu) do { } while (0)
@@ -6253,30 +6950,33 @@ __setup("isolcpus=", isolated_cpu_setup);
6253 * and ->cpu_power to 0. 6950 * and ->cpu_power to 0.
6254 */ 6951 */
6255static void 6952static void
6256init_sched_build_groups(cpumask_t span, const cpumask_t *cpu_map, 6953init_sched_build_groups(const cpumask_t *span, const cpumask_t *cpu_map,
6257 int (*group_fn)(int cpu, const cpumask_t *cpu_map, 6954 int (*group_fn)(int cpu, const cpumask_t *cpu_map,
6258 struct sched_group **sg)) 6955 struct sched_group **sg,
6956 cpumask_t *tmpmask),
6957 cpumask_t *covered, cpumask_t *tmpmask)
6259{ 6958{
6260 struct sched_group *first = NULL, *last = NULL; 6959 struct sched_group *first = NULL, *last = NULL;
6261 cpumask_t covered = CPU_MASK_NONE;
6262 int i; 6960 int i;
6263 6961
6264 for_each_cpu_mask(i, span) { 6962 cpus_clear(*covered);
6963
6964 for_each_cpu_mask(i, *span) {
6265 struct sched_group *sg; 6965 struct sched_group *sg;
6266 int group = group_fn(i, cpu_map, &sg); 6966 int group = group_fn(i, cpu_map, &sg, tmpmask);
6267 int j; 6967 int j;
6268 6968
6269 if (cpu_isset(i, covered)) 6969 if (cpu_isset(i, *covered))
6270 continue; 6970 continue;
6271 6971
6272 sg->cpumask = CPU_MASK_NONE; 6972 cpus_clear(sg->cpumask);
6273 sg->__cpu_power = 0; 6973 sg->__cpu_power = 0;
6274 6974
6275 for_each_cpu_mask(j, span) { 6975 for_each_cpu_mask(j, *span) {
6276 if (group_fn(j, cpu_map, NULL) != group) 6976 if (group_fn(j, cpu_map, NULL, tmpmask) != group)
6277 continue; 6977 continue;
6278 6978
6279 cpu_set(j, covered); 6979 cpu_set(j, *covered);
6280 cpu_set(j, sg->cpumask); 6980 cpu_set(j, sg->cpumask);
6281 } 6981 }
6282 if (!first) 6982 if (!first)
@@ -6302,7 +7002,7 @@ init_sched_build_groups(cpumask_t span, const cpumask_t *cpu_map,
6302 * 7002 *
6303 * Should use nodemask_t. 7003 * Should use nodemask_t.
6304 */ 7004 */
6305static int find_next_best_node(int node, unsigned long *used_nodes) 7005static int find_next_best_node(int node, nodemask_t *used_nodes)
6306{ 7006{
6307 int i, n, val, min_val, best_node = 0; 7007 int i, n, val, min_val, best_node = 0;
6308 7008
@@ -6316,7 +7016,7 @@ static int find_next_best_node(int node, unsigned long *used_nodes)
6316 continue; 7016 continue;
6317 7017
6318 /* Skip already used nodes */ 7018 /* Skip already used nodes */
6319 if (test_bit(n, used_nodes)) 7019 if (node_isset(n, *used_nodes))
6320 continue; 7020 continue;
6321 7021
6322 /* Simple min distance search */ 7022 /* Simple min distance search */
@@ -6328,40 +7028,37 @@ static int find_next_best_node(int node, unsigned long *used_nodes)
6328 } 7028 }
6329 } 7029 }
6330 7030
6331 set_bit(best_node, used_nodes); 7031 node_set(best_node, *used_nodes);
6332 return best_node; 7032 return best_node;
6333} 7033}
6334 7034
6335/** 7035/**
6336 * sched_domain_node_span - get a cpumask for a node's sched_domain 7036 * sched_domain_node_span - get a cpumask for a node's sched_domain
6337 * @node: node whose cpumask we're constructing 7037 * @node: node whose cpumask we're constructing
6338 * @size: number of nodes to include in this span 7038 * @span: resulting cpumask
6339 * 7039 *
6340 * Given a node, construct a good cpumask for its sched_domain to span. It 7040 * Given a node, construct a good cpumask for its sched_domain to span. It
6341 * should be one that prevents unnecessary balancing, but also spreads tasks 7041 * should be one that prevents unnecessary balancing, but also spreads tasks
6342 * out optimally. 7042 * out optimally.
6343 */ 7043 */
6344static cpumask_t sched_domain_node_span(int node) 7044static void sched_domain_node_span(int node, cpumask_t *span)
6345{ 7045{
6346 DECLARE_BITMAP(used_nodes, MAX_NUMNODES); 7046 nodemask_t used_nodes;
6347 cpumask_t span, nodemask; 7047 node_to_cpumask_ptr(nodemask, node);
6348 int i; 7048 int i;
6349 7049
6350 cpus_clear(span); 7050 cpus_clear(*span);
6351 bitmap_zero(used_nodes, MAX_NUMNODES); 7051 nodes_clear(used_nodes);
6352 7052
6353 nodemask = node_to_cpumask(node); 7053 cpus_or(*span, *span, *nodemask);
6354 cpus_or(span, span, nodemask); 7054 node_set(node, used_nodes);
6355 set_bit(node, used_nodes);
6356 7055
6357 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) { 7056 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
6358 int next_node = find_next_best_node(node, used_nodes); 7057 int next_node = find_next_best_node(node, &used_nodes);
6359 7058
6360 nodemask = node_to_cpumask(next_node); 7059 node_to_cpumask_ptr_next(nodemask, next_node);
6361 cpus_or(span, span, nodemask); 7060 cpus_or(*span, *span, *nodemask);
6362 } 7061 }
6363
6364 return span;
6365} 7062}
6366#endif 7063#endif
6367 7064
@@ -6375,7 +7072,8 @@ static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
6375static DEFINE_PER_CPU(struct sched_group, sched_group_cpus); 7072static DEFINE_PER_CPU(struct sched_group, sched_group_cpus);
6376 7073
6377static int 7074static int
6378cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg) 7075cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7076 cpumask_t *unused)
6379{ 7077{
6380 if (sg) 7078 if (sg)
6381 *sg = &per_cpu(sched_group_cpus, cpu); 7079 *sg = &per_cpu(sched_group_cpus, cpu);
@@ -6393,19 +7091,22 @@ static DEFINE_PER_CPU(struct sched_group, sched_group_core);
6393 7091
6394#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT) 7092#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
6395static int 7093static int
6396cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg) 7094cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7095 cpumask_t *mask)
6397{ 7096{
6398 int group; 7097 int group;
6399 cpumask_t mask = per_cpu(cpu_sibling_map, cpu); 7098
6400 cpus_and(mask, mask, *cpu_map); 7099 *mask = per_cpu(cpu_sibling_map, cpu);
6401 group = first_cpu(mask); 7100 cpus_and(*mask, *mask, *cpu_map);
7101 group = first_cpu(*mask);
6402 if (sg) 7102 if (sg)
6403 *sg = &per_cpu(sched_group_core, group); 7103 *sg = &per_cpu(sched_group_core, group);
6404 return group; 7104 return group;
6405} 7105}
6406#elif defined(CONFIG_SCHED_MC) 7106#elif defined(CONFIG_SCHED_MC)
6407static int 7107static int
6408cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg) 7108cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7109 cpumask_t *unused)
6409{ 7110{
6410 if (sg) 7111 if (sg)
6411 *sg = &per_cpu(sched_group_core, cpu); 7112 *sg = &per_cpu(sched_group_core, cpu);
@@ -6417,17 +7118,18 @@ static DEFINE_PER_CPU(struct sched_domain, phys_domains);
6417static DEFINE_PER_CPU(struct sched_group, sched_group_phys); 7118static DEFINE_PER_CPU(struct sched_group, sched_group_phys);
6418 7119
6419static int 7120static int
6420cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg) 7121cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7122 cpumask_t *mask)
6421{ 7123{
6422 int group; 7124 int group;
6423#ifdef CONFIG_SCHED_MC 7125#ifdef CONFIG_SCHED_MC
6424 cpumask_t mask = cpu_coregroup_map(cpu); 7126 *mask = cpu_coregroup_map(cpu);
6425 cpus_and(mask, mask, *cpu_map); 7127 cpus_and(*mask, *mask, *cpu_map);
6426 group = first_cpu(mask); 7128 group = first_cpu(*mask);
6427#elif defined(CONFIG_SCHED_SMT) 7129#elif defined(CONFIG_SCHED_SMT)
6428 cpumask_t mask = per_cpu(cpu_sibling_map, cpu); 7130 *mask = per_cpu(cpu_sibling_map, cpu);
6429 cpus_and(mask, mask, *cpu_map); 7131 cpus_and(*mask, *mask, *cpu_map);
6430 group = first_cpu(mask); 7132 group = first_cpu(*mask);
6431#else 7133#else
6432 group = cpu; 7134 group = cpu;
6433#endif 7135#endif
@@ -6443,19 +7145,19 @@ cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg)
6443 * gets dynamically allocated. 7145 * gets dynamically allocated.
6444 */ 7146 */
6445static DEFINE_PER_CPU(struct sched_domain, node_domains); 7147static DEFINE_PER_CPU(struct sched_domain, node_domains);
6446static struct sched_group **sched_group_nodes_bycpu[NR_CPUS]; 7148static struct sched_group ***sched_group_nodes_bycpu;
6447 7149
6448static DEFINE_PER_CPU(struct sched_domain, allnodes_domains); 7150static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
6449static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes); 7151static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes);
6450 7152
6451static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map, 7153static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map,
6452 struct sched_group **sg) 7154 struct sched_group **sg, cpumask_t *nodemask)
6453{ 7155{
6454 cpumask_t nodemask = node_to_cpumask(cpu_to_node(cpu));
6455 int group; 7156 int group;
6456 7157
6457 cpus_and(nodemask, nodemask, *cpu_map); 7158 *nodemask = node_to_cpumask(cpu_to_node(cpu));
6458 group = first_cpu(nodemask); 7159 cpus_and(*nodemask, *nodemask, *cpu_map);
7160 group = first_cpu(*nodemask);
6459 7161
6460 if (sg) 7162 if (sg)
6461 *sg = &per_cpu(sched_group_allnodes, group); 7163 *sg = &per_cpu(sched_group_allnodes, group);
@@ -6491,7 +7193,7 @@ static void init_numa_sched_groups_power(struct sched_group *group_head)
6491 7193
6492#ifdef CONFIG_NUMA 7194#ifdef CONFIG_NUMA
6493/* Free memory allocated for various sched_group structures */ 7195/* Free memory allocated for various sched_group structures */
6494static void free_sched_groups(const cpumask_t *cpu_map) 7196static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
6495{ 7197{
6496 int cpu, i; 7198 int cpu, i;
6497 7199
@@ -6503,11 +7205,11 @@ static void free_sched_groups(const cpumask_t *cpu_map)
6503 continue; 7205 continue;
6504 7206
6505 for (i = 0; i < MAX_NUMNODES; i++) { 7207 for (i = 0; i < MAX_NUMNODES; i++) {
6506 cpumask_t nodemask = node_to_cpumask(i);
6507 struct sched_group *oldsg, *sg = sched_group_nodes[i]; 7208 struct sched_group *oldsg, *sg = sched_group_nodes[i];
6508 7209
6509 cpus_and(nodemask, nodemask, *cpu_map); 7210 *nodemask = node_to_cpumask(i);
6510 if (cpus_empty(nodemask)) 7211 cpus_and(*nodemask, *nodemask, *cpu_map);
7212 if (cpus_empty(*nodemask))
6511 continue; 7213 continue;
6512 7214
6513 if (sg == NULL) 7215 if (sg == NULL)
@@ -6525,7 +7227,7 @@ next_sg:
6525 } 7227 }
6526} 7228}
6527#else 7229#else
6528static void free_sched_groups(const cpumask_t *cpu_map) 7230static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
6529{ 7231{
6530} 7232}
6531#endif 7233#endif
@@ -6583,13 +7285,106 @@ static void init_sched_groups_power(int cpu, struct sched_domain *sd)
6583} 7285}
6584 7286
6585/* 7287/*
7288 * Initializers for schedule domains
7289 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
7290 */
7291
7292#define SD_INIT(sd, type) sd_init_##type(sd)
7293#define SD_INIT_FUNC(type) \
7294static noinline void sd_init_##type(struct sched_domain *sd) \
7295{ \
7296 memset(sd, 0, sizeof(*sd)); \
7297 *sd = SD_##type##_INIT; \
7298 sd->level = SD_LV_##type; \
7299}
7300
7301SD_INIT_FUNC(CPU)
7302#ifdef CONFIG_NUMA
7303 SD_INIT_FUNC(ALLNODES)
7304 SD_INIT_FUNC(NODE)
7305#endif
7306#ifdef CONFIG_SCHED_SMT
7307 SD_INIT_FUNC(SIBLING)
7308#endif
7309#ifdef CONFIG_SCHED_MC
7310 SD_INIT_FUNC(MC)
7311#endif
7312
7313/*
7314 * To minimize stack usage kmalloc room for cpumasks and share the
7315 * space as the usage in build_sched_domains() dictates. Used only
7316 * if the amount of space is significant.
7317 */
7318struct allmasks {
7319 cpumask_t tmpmask; /* make this one first */
7320 union {
7321 cpumask_t nodemask;
7322 cpumask_t this_sibling_map;
7323 cpumask_t this_core_map;
7324 };
7325 cpumask_t send_covered;
7326
7327#ifdef CONFIG_NUMA
7328 cpumask_t domainspan;
7329 cpumask_t covered;
7330 cpumask_t notcovered;
7331#endif
7332};
7333
7334#if NR_CPUS > 128
7335#define SCHED_CPUMASK_ALLOC 1
7336#define SCHED_CPUMASK_FREE(v) kfree(v)
7337#define SCHED_CPUMASK_DECLARE(v) struct allmasks *v
7338#else
7339#define SCHED_CPUMASK_ALLOC 0
7340#define SCHED_CPUMASK_FREE(v)
7341#define SCHED_CPUMASK_DECLARE(v) struct allmasks _v, *v = &_v
7342#endif
7343
7344#define SCHED_CPUMASK_VAR(v, a) cpumask_t *v = (cpumask_t *) \
7345 ((unsigned long)(a) + offsetof(struct allmasks, v))
7346
7347static int default_relax_domain_level = -1;
7348
7349static int __init setup_relax_domain_level(char *str)
7350{
7351 default_relax_domain_level = simple_strtoul(str, NULL, 0);
7352 return 1;
7353}
7354__setup("relax_domain_level=", setup_relax_domain_level);
7355
7356static void set_domain_attribute(struct sched_domain *sd,
7357 struct sched_domain_attr *attr)
7358{
7359 int request;
7360
7361 if (!attr || attr->relax_domain_level < 0) {
7362 if (default_relax_domain_level < 0)
7363 return;
7364 else
7365 request = default_relax_domain_level;
7366 } else
7367 request = attr->relax_domain_level;
7368 if (request < sd->level) {
7369 /* turn off idle balance on this domain */
7370 sd->flags &= ~(SD_WAKE_IDLE|SD_BALANCE_NEWIDLE);
7371 } else {
7372 /* turn on idle balance on this domain */
7373 sd->flags |= (SD_WAKE_IDLE_FAR|SD_BALANCE_NEWIDLE);
7374 }
7375}
7376
7377/*
6586 * Build sched domains for a given set of cpus and attach the sched domains 7378 * Build sched domains for a given set of cpus and attach the sched domains
6587 * to the individual cpus 7379 * to the individual cpus
6588 */ 7380 */
6589static int build_sched_domains(const cpumask_t *cpu_map) 7381static int __build_sched_domains(const cpumask_t *cpu_map,
7382 struct sched_domain_attr *attr)
6590{ 7383{
6591 int i; 7384 int i;
6592 struct root_domain *rd; 7385 struct root_domain *rd;
7386 SCHED_CPUMASK_DECLARE(allmasks);
7387 cpumask_t *tmpmask;
6593#ifdef CONFIG_NUMA 7388#ifdef CONFIG_NUMA
6594 struct sched_group **sched_group_nodes = NULL; 7389 struct sched_group **sched_group_nodes = NULL;
6595 int sd_allnodes = 0; 7390 int sd_allnodes = 0;
@@ -6603,39 +7398,65 @@ static int build_sched_domains(const cpumask_t *cpu_map)
6603 printk(KERN_WARNING "Can not alloc sched group node list\n"); 7398 printk(KERN_WARNING "Can not alloc sched group node list\n");
6604 return -ENOMEM; 7399 return -ENOMEM;
6605 } 7400 }
6606 sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
6607#endif 7401#endif
6608 7402
6609 rd = alloc_rootdomain(); 7403 rd = alloc_rootdomain();
6610 if (!rd) { 7404 if (!rd) {
6611 printk(KERN_WARNING "Cannot alloc root domain\n"); 7405 printk(KERN_WARNING "Cannot alloc root domain\n");
7406#ifdef CONFIG_NUMA
7407 kfree(sched_group_nodes);
7408#endif
6612 return -ENOMEM; 7409 return -ENOMEM;
6613 } 7410 }
6614 7411
7412#if SCHED_CPUMASK_ALLOC
7413 /* get space for all scratch cpumask variables */
7414 allmasks = kmalloc(sizeof(*allmasks), GFP_KERNEL);
7415 if (!allmasks) {
7416 printk(KERN_WARNING "Cannot alloc cpumask array\n");
7417 kfree(rd);
7418#ifdef CONFIG_NUMA
7419 kfree(sched_group_nodes);
7420#endif
7421 return -ENOMEM;
7422 }
7423#endif
7424 tmpmask = (cpumask_t *)allmasks;
7425
7426
7427#ifdef CONFIG_NUMA
7428 sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
7429#endif
7430
6615 /* 7431 /*
6616 * Set up domains for cpus specified by the cpu_map. 7432 * Set up domains for cpus specified by the cpu_map.
6617 */ 7433 */
6618 for_each_cpu_mask(i, *cpu_map) { 7434 for_each_cpu_mask(i, *cpu_map) {
6619 struct sched_domain *sd = NULL, *p; 7435 struct sched_domain *sd = NULL, *p;
6620 cpumask_t nodemask = node_to_cpumask(cpu_to_node(i)); 7436 SCHED_CPUMASK_VAR(nodemask, allmasks);
6621 7437
6622 cpus_and(nodemask, nodemask, *cpu_map); 7438 *nodemask = node_to_cpumask(cpu_to_node(i));
7439 cpus_and(*nodemask, *nodemask, *cpu_map);
6623 7440
6624#ifdef CONFIG_NUMA 7441#ifdef CONFIG_NUMA
6625 if (cpus_weight(*cpu_map) > 7442 if (cpus_weight(*cpu_map) >
6626 SD_NODES_PER_DOMAIN*cpus_weight(nodemask)) { 7443 SD_NODES_PER_DOMAIN*cpus_weight(*nodemask)) {
6627 sd = &per_cpu(allnodes_domains, i); 7444 sd = &per_cpu(allnodes_domains, i);
6628 *sd = SD_ALLNODES_INIT; 7445 SD_INIT(sd, ALLNODES);
7446 set_domain_attribute(sd, attr);
6629 sd->span = *cpu_map; 7447 sd->span = *cpu_map;
6630 cpu_to_allnodes_group(i, cpu_map, &sd->groups); 7448 sd->first_cpu = first_cpu(sd->span);
7449 cpu_to_allnodes_group(i, cpu_map, &sd->groups, tmpmask);
6631 p = sd; 7450 p = sd;
6632 sd_allnodes = 1; 7451 sd_allnodes = 1;
6633 } else 7452 } else
6634 p = NULL; 7453 p = NULL;
6635 7454
6636 sd = &per_cpu(node_domains, i); 7455 sd = &per_cpu(node_domains, i);
6637 *sd = SD_NODE_INIT; 7456 SD_INIT(sd, NODE);
6638 sd->span = sched_domain_node_span(cpu_to_node(i)); 7457 set_domain_attribute(sd, attr);
7458 sched_domain_node_span(cpu_to_node(i), &sd->span);
7459 sd->first_cpu = first_cpu(sd->span);
6639 sd->parent = p; 7460 sd->parent = p;
6640 if (p) 7461 if (p)
6641 p->child = sd; 7462 p->child = sd;
@@ -6644,94 +7465,120 @@ static int build_sched_domains(const cpumask_t *cpu_map)
6644 7465
6645 p = sd; 7466 p = sd;
6646 sd = &per_cpu(phys_domains, i); 7467 sd = &per_cpu(phys_domains, i);
6647 *sd = SD_CPU_INIT; 7468 SD_INIT(sd, CPU);
6648 sd->span = nodemask; 7469 set_domain_attribute(sd, attr);
7470 sd->span = *nodemask;
7471 sd->first_cpu = first_cpu(sd->span);
6649 sd->parent = p; 7472 sd->parent = p;
6650 if (p) 7473 if (p)
6651 p->child = sd; 7474 p->child = sd;
6652 cpu_to_phys_group(i, cpu_map, &sd->groups); 7475 cpu_to_phys_group(i, cpu_map, &sd->groups, tmpmask);
6653 7476
6654#ifdef CONFIG_SCHED_MC 7477#ifdef CONFIG_SCHED_MC
6655 p = sd; 7478 p = sd;
6656 sd = &per_cpu(core_domains, i); 7479 sd = &per_cpu(core_domains, i);
6657 *sd = SD_MC_INIT; 7480 SD_INIT(sd, MC);
7481 set_domain_attribute(sd, attr);
6658 sd->span = cpu_coregroup_map(i); 7482 sd->span = cpu_coregroup_map(i);
7483 sd->first_cpu = first_cpu(sd->span);
6659 cpus_and(sd->span, sd->span, *cpu_map); 7484 cpus_and(sd->span, sd->span, *cpu_map);
6660 sd->parent = p; 7485 sd->parent = p;
6661 p->child = sd; 7486 p->child = sd;
6662 cpu_to_core_group(i, cpu_map, &sd->groups); 7487 cpu_to_core_group(i, cpu_map, &sd->groups, tmpmask);
6663#endif 7488#endif
6664 7489
6665#ifdef CONFIG_SCHED_SMT 7490#ifdef CONFIG_SCHED_SMT
6666 p = sd; 7491 p = sd;
6667 sd = &per_cpu(cpu_domains, i); 7492 sd = &per_cpu(cpu_domains, i);
6668 *sd = SD_SIBLING_INIT; 7493 SD_INIT(sd, SIBLING);
7494 set_domain_attribute(sd, attr);
6669 sd->span = per_cpu(cpu_sibling_map, i); 7495 sd->span = per_cpu(cpu_sibling_map, i);
7496 sd->first_cpu = first_cpu(sd->span);
6670 cpus_and(sd->span, sd->span, *cpu_map); 7497 cpus_and(sd->span, sd->span, *cpu_map);
6671 sd->parent = p; 7498 sd->parent = p;
6672 p->child = sd; 7499 p->child = sd;
6673 cpu_to_cpu_group(i, cpu_map, &sd->groups); 7500 cpu_to_cpu_group(i, cpu_map, &sd->groups, tmpmask);
6674#endif 7501#endif
6675 } 7502 }
6676 7503
6677#ifdef CONFIG_SCHED_SMT 7504#ifdef CONFIG_SCHED_SMT
6678 /* Set up CPU (sibling) groups */ 7505 /* Set up CPU (sibling) groups */
6679 for_each_cpu_mask(i, *cpu_map) { 7506 for_each_cpu_mask(i, *cpu_map) {
6680 cpumask_t this_sibling_map = per_cpu(cpu_sibling_map, i); 7507 SCHED_CPUMASK_VAR(this_sibling_map, allmasks);
6681 cpus_and(this_sibling_map, this_sibling_map, *cpu_map); 7508 SCHED_CPUMASK_VAR(send_covered, allmasks);
6682 if (i != first_cpu(this_sibling_map)) 7509
7510 *this_sibling_map = per_cpu(cpu_sibling_map, i);
7511 cpus_and(*this_sibling_map, *this_sibling_map, *cpu_map);
7512 if (i != first_cpu(*this_sibling_map))
6683 continue; 7513 continue;
6684 7514
6685 init_sched_build_groups(this_sibling_map, cpu_map, 7515 init_sched_build_groups(this_sibling_map, cpu_map,
6686 &cpu_to_cpu_group); 7516 &cpu_to_cpu_group,
7517 send_covered, tmpmask);
6687 } 7518 }
6688#endif 7519#endif
6689 7520
6690#ifdef CONFIG_SCHED_MC 7521#ifdef CONFIG_SCHED_MC
6691 /* Set up multi-core groups */ 7522 /* Set up multi-core groups */
6692 for_each_cpu_mask(i, *cpu_map) { 7523 for_each_cpu_mask(i, *cpu_map) {
6693 cpumask_t this_core_map = cpu_coregroup_map(i); 7524 SCHED_CPUMASK_VAR(this_core_map, allmasks);
6694 cpus_and(this_core_map, this_core_map, *cpu_map); 7525 SCHED_CPUMASK_VAR(send_covered, allmasks);
6695 if (i != first_cpu(this_core_map)) 7526
7527 *this_core_map = cpu_coregroup_map(i);
7528 cpus_and(*this_core_map, *this_core_map, *cpu_map);
7529 if (i != first_cpu(*this_core_map))
6696 continue; 7530 continue;
7531
6697 init_sched_build_groups(this_core_map, cpu_map, 7532 init_sched_build_groups(this_core_map, cpu_map,
6698 &cpu_to_core_group); 7533 &cpu_to_core_group,
7534 send_covered, tmpmask);
6699 } 7535 }
6700#endif 7536#endif
6701 7537
6702 /* Set up physical groups */ 7538 /* Set up physical groups */
6703 for (i = 0; i < MAX_NUMNODES; i++) { 7539 for (i = 0; i < MAX_NUMNODES; i++) {
6704 cpumask_t nodemask = node_to_cpumask(i); 7540 SCHED_CPUMASK_VAR(nodemask, allmasks);
7541 SCHED_CPUMASK_VAR(send_covered, allmasks);
6705 7542
6706 cpus_and(nodemask, nodemask, *cpu_map); 7543 *nodemask = node_to_cpumask(i);
6707 if (cpus_empty(nodemask)) 7544 cpus_and(*nodemask, *nodemask, *cpu_map);
7545 if (cpus_empty(*nodemask))
6708 continue; 7546 continue;
6709 7547
6710 init_sched_build_groups(nodemask, cpu_map, &cpu_to_phys_group); 7548 init_sched_build_groups(nodemask, cpu_map,
7549 &cpu_to_phys_group,
7550 send_covered, tmpmask);
6711 } 7551 }
6712 7552
6713#ifdef CONFIG_NUMA 7553#ifdef CONFIG_NUMA
6714 /* Set up node groups */ 7554 /* Set up node groups */
6715 if (sd_allnodes) 7555 if (sd_allnodes) {
6716 init_sched_build_groups(*cpu_map, cpu_map, 7556 SCHED_CPUMASK_VAR(send_covered, allmasks);
6717 &cpu_to_allnodes_group); 7557
7558 init_sched_build_groups(cpu_map, cpu_map,
7559 &cpu_to_allnodes_group,
7560 send_covered, tmpmask);
7561 }
6718 7562
6719 for (i = 0; i < MAX_NUMNODES; i++) { 7563 for (i = 0; i < MAX_NUMNODES; i++) {
6720 /* Set up node groups */ 7564 /* Set up node groups */
6721 struct sched_group *sg, *prev; 7565 struct sched_group *sg, *prev;
6722 cpumask_t nodemask = node_to_cpumask(i); 7566 SCHED_CPUMASK_VAR(nodemask, allmasks);
6723 cpumask_t domainspan; 7567 SCHED_CPUMASK_VAR(domainspan, allmasks);
6724 cpumask_t covered = CPU_MASK_NONE; 7568 SCHED_CPUMASK_VAR(covered, allmasks);
6725 int j; 7569 int j;
6726 7570
6727 cpus_and(nodemask, nodemask, *cpu_map); 7571 *nodemask = node_to_cpumask(i);
6728 if (cpus_empty(nodemask)) { 7572 cpus_clear(*covered);
7573
7574 cpus_and(*nodemask, *nodemask, *cpu_map);
7575 if (cpus_empty(*nodemask)) {
6729 sched_group_nodes[i] = NULL; 7576 sched_group_nodes[i] = NULL;
6730 continue; 7577 continue;
6731 } 7578 }
6732 7579
6733 domainspan = sched_domain_node_span(i); 7580 sched_domain_node_span(i, domainspan);
6734 cpus_and(domainspan, domainspan, *cpu_map); 7581 cpus_and(*domainspan, *domainspan, *cpu_map);
6735 7582
6736 sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i); 7583 sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i);
6737 if (!sg) { 7584 if (!sg) {
@@ -6740,31 +7587,31 @@ static int build_sched_domains(const cpumask_t *cpu_map)
6740 goto error; 7587 goto error;
6741 } 7588 }
6742 sched_group_nodes[i] = sg; 7589 sched_group_nodes[i] = sg;
6743 for_each_cpu_mask(j, nodemask) { 7590 for_each_cpu_mask(j, *nodemask) {
6744 struct sched_domain *sd; 7591 struct sched_domain *sd;
6745 7592
6746 sd = &per_cpu(node_domains, j); 7593 sd = &per_cpu(node_domains, j);
6747 sd->groups = sg; 7594 sd->groups = sg;
6748 } 7595 }
6749 sg->__cpu_power = 0; 7596 sg->__cpu_power = 0;
6750 sg->cpumask = nodemask; 7597 sg->cpumask = *nodemask;
6751 sg->next = sg; 7598 sg->next = sg;
6752 cpus_or(covered, covered, nodemask); 7599 cpus_or(*covered, *covered, *nodemask);
6753 prev = sg; 7600 prev = sg;
6754 7601
6755 for (j = 0; j < MAX_NUMNODES; j++) { 7602 for (j = 0; j < MAX_NUMNODES; j++) {
6756 cpumask_t tmp, notcovered; 7603 SCHED_CPUMASK_VAR(notcovered, allmasks);
6757 int n = (i + j) % MAX_NUMNODES; 7604 int n = (i + j) % MAX_NUMNODES;
7605 node_to_cpumask_ptr(pnodemask, n);
6758 7606
6759 cpus_complement(notcovered, covered); 7607 cpus_complement(*notcovered, *covered);
6760 cpus_and(tmp, notcovered, *cpu_map); 7608 cpus_and(*tmpmask, *notcovered, *cpu_map);
6761 cpus_and(tmp, tmp, domainspan); 7609 cpus_and(*tmpmask, *tmpmask, *domainspan);
6762 if (cpus_empty(tmp)) 7610 if (cpus_empty(*tmpmask))
6763 break; 7611 break;
6764 7612
6765 nodemask = node_to_cpumask(n); 7613 cpus_and(*tmpmask, *tmpmask, *pnodemask);
6766 cpus_and(tmp, tmp, nodemask); 7614 if (cpus_empty(*tmpmask))
6767 if (cpus_empty(tmp))
6768 continue; 7615 continue;
6769 7616
6770 sg = kmalloc_node(sizeof(struct sched_group), 7617 sg = kmalloc_node(sizeof(struct sched_group),
@@ -6775,9 +7622,9 @@ static int build_sched_domains(const cpumask_t *cpu_map)
6775 goto error; 7622 goto error;
6776 } 7623 }
6777 sg->__cpu_power = 0; 7624 sg->__cpu_power = 0;
6778 sg->cpumask = tmp; 7625 sg->cpumask = *tmpmask;
6779 sg->next = prev->next; 7626 sg->next = prev->next;
6780 cpus_or(covered, covered, tmp); 7627 cpus_or(*covered, *covered, *tmpmask);
6781 prev->next = sg; 7628 prev->next = sg;
6782 prev = sg; 7629 prev = sg;
6783 } 7630 }
@@ -6813,7 +7660,8 @@ static int build_sched_domains(const cpumask_t *cpu_map)
6813 if (sd_allnodes) { 7660 if (sd_allnodes) {
6814 struct sched_group *sg; 7661 struct sched_group *sg;
6815 7662
6816 cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg); 7663 cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg,
7664 tmpmask);
6817 init_numa_sched_groups_power(sg); 7665 init_numa_sched_groups_power(sg);
6818 } 7666 }
6819#endif 7667#endif
@@ -6831,17 +7679,26 @@ static int build_sched_domains(const cpumask_t *cpu_map)
6831 cpu_attach_domain(sd, rd, i); 7679 cpu_attach_domain(sd, rd, i);
6832 } 7680 }
6833 7681
7682 SCHED_CPUMASK_FREE((void *)allmasks);
6834 return 0; 7683 return 0;
6835 7684
6836#ifdef CONFIG_NUMA 7685#ifdef CONFIG_NUMA
6837error: 7686error:
6838 free_sched_groups(cpu_map); 7687 free_sched_groups(cpu_map, tmpmask);
7688 SCHED_CPUMASK_FREE((void *)allmasks);
6839 return -ENOMEM; 7689 return -ENOMEM;
6840#endif 7690#endif
6841} 7691}
6842 7692
7693static int build_sched_domains(const cpumask_t *cpu_map)
7694{
7695 return __build_sched_domains(cpu_map, NULL);
7696}
7697
6843static cpumask_t *doms_cur; /* current sched domains */ 7698static cpumask_t *doms_cur; /* current sched domains */
6844static int ndoms_cur; /* number of sched domains in 'doms_cur' */ 7699static int ndoms_cur; /* number of sched domains in 'doms_cur' */
7700static struct sched_domain_attr *dattr_cur; /* attribues of custom domains
7701 in 'doms_cur' */
6845 7702
6846/* 7703/*
6847 * Special case: If a kmalloc of a doms_cur partition (array of 7704 * Special case: If a kmalloc of a doms_cur partition (array of
@@ -6869,15 +7726,17 @@ static int arch_init_sched_domains(const cpumask_t *cpu_map)
6869 if (!doms_cur) 7726 if (!doms_cur)
6870 doms_cur = &fallback_doms; 7727 doms_cur = &fallback_doms;
6871 cpus_andnot(*doms_cur, *cpu_map, cpu_isolated_map); 7728 cpus_andnot(*doms_cur, *cpu_map, cpu_isolated_map);
7729 dattr_cur = NULL;
6872 err = build_sched_domains(doms_cur); 7730 err = build_sched_domains(doms_cur);
6873 register_sched_domain_sysctl(); 7731 register_sched_domain_sysctl();
6874 7732
6875 return err; 7733 return err;
6876} 7734}
6877 7735
6878static void arch_destroy_sched_domains(const cpumask_t *cpu_map) 7736static void arch_destroy_sched_domains(const cpumask_t *cpu_map,
7737 cpumask_t *tmpmask)
6879{ 7738{
6880 free_sched_groups(cpu_map); 7739 free_sched_groups(cpu_map, tmpmask);
6881} 7740}
6882 7741
6883/* 7742/*
@@ -6886,6 +7745,7 @@ static void arch_destroy_sched_domains(const cpumask_t *cpu_map)
6886 */ 7745 */
6887static void detach_destroy_domains(const cpumask_t *cpu_map) 7746static void detach_destroy_domains(const cpumask_t *cpu_map)
6888{ 7747{
7748 cpumask_t tmpmask;
6889 int i; 7749 int i;
6890 7750
6891 unregister_sched_domain_sysctl(); 7751 unregister_sched_domain_sysctl();
@@ -6893,7 +7753,23 @@ static void detach_destroy_domains(const cpumask_t *cpu_map)
6893 for_each_cpu_mask(i, *cpu_map) 7753 for_each_cpu_mask(i, *cpu_map)
6894 cpu_attach_domain(NULL, &def_root_domain, i); 7754 cpu_attach_domain(NULL, &def_root_domain, i);
6895 synchronize_sched(); 7755 synchronize_sched();
6896 arch_destroy_sched_domains(cpu_map); 7756 arch_destroy_sched_domains(cpu_map, &tmpmask);
7757}
7758
7759/* handle null as "default" */
7760static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
7761 struct sched_domain_attr *new, int idx_new)
7762{
7763 struct sched_domain_attr tmp;
7764
7765 /* fast path */
7766 if (!new && !cur)
7767 return 1;
7768
7769 tmp = SD_ATTR_INIT;
7770 return !memcmp(cur ? (cur + idx_cur) : &tmp,
7771 new ? (new + idx_new) : &tmp,
7772 sizeof(struct sched_domain_attr));
6897} 7773}
6898 7774
6899/* 7775/*
@@ -6917,7 +7793,8 @@ static void detach_destroy_domains(const cpumask_t *cpu_map)
6917 * 7793 *
6918 * Call with hotplug lock held 7794 * Call with hotplug lock held
6919 */ 7795 */
6920void partition_sched_domains(int ndoms_new, cpumask_t *doms_new) 7796void partition_sched_domains(int ndoms_new, cpumask_t *doms_new,
7797 struct sched_domain_attr *dattr_new)
6921{ 7798{
6922 int i, j; 7799 int i, j;
6923 7800
@@ -6930,12 +7807,14 @@ void partition_sched_domains(int ndoms_new, cpumask_t *doms_new)
6930 ndoms_new = 1; 7807 ndoms_new = 1;
6931 doms_new = &fallback_doms; 7808 doms_new = &fallback_doms;
6932 cpus_andnot(doms_new[0], cpu_online_map, cpu_isolated_map); 7809 cpus_andnot(doms_new[0], cpu_online_map, cpu_isolated_map);
7810 dattr_new = NULL;
6933 } 7811 }
6934 7812
6935 /* Destroy deleted domains */ 7813 /* Destroy deleted domains */
6936 for (i = 0; i < ndoms_cur; i++) { 7814 for (i = 0; i < ndoms_cur; i++) {
6937 for (j = 0; j < ndoms_new; j++) { 7815 for (j = 0; j < ndoms_new; j++) {
6938 if (cpus_equal(doms_cur[i], doms_new[j])) 7816 if (cpus_equal(doms_cur[i], doms_new[j])
7817 && dattrs_equal(dattr_cur, i, dattr_new, j))
6939 goto match1; 7818 goto match1;
6940 } 7819 }
6941 /* no match - a current sched domain not in new doms_new[] */ 7820 /* no match - a current sched domain not in new doms_new[] */
@@ -6947,11 +7826,13 @@ match1:
6947 /* Build new domains */ 7826 /* Build new domains */
6948 for (i = 0; i < ndoms_new; i++) { 7827 for (i = 0; i < ndoms_new; i++) {
6949 for (j = 0; j < ndoms_cur; j++) { 7828 for (j = 0; j < ndoms_cur; j++) {
6950 if (cpus_equal(doms_new[i], doms_cur[j])) 7829 if (cpus_equal(doms_new[i], doms_cur[j])
7830 && dattrs_equal(dattr_new, i, dattr_cur, j))
6951 goto match2; 7831 goto match2;
6952 } 7832 }
6953 /* no match - add a new doms_new */ 7833 /* no match - add a new doms_new */
6954 build_sched_domains(doms_new + i); 7834 __build_sched_domains(doms_new + i,
7835 dattr_new ? dattr_new + i : NULL);
6955match2: 7836match2:
6956 ; 7837 ;
6957 } 7838 }
@@ -6959,7 +7840,9 @@ match2:
6959 /* Remember the new sched domains */ 7840 /* Remember the new sched domains */
6960 if (doms_cur != &fallback_doms) 7841 if (doms_cur != &fallback_doms)
6961 kfree(doms_cur); 7842 kfree(doms_cur);
7843 kfree(dattr_cur); /* kfree(NULL) is safe */
6962 doms_cur = doms_new; 7844 doms_cur = doms_new;
7845 dattr_cur = dattr_new;
6963 ndoms_cur = ndoms_new; 7846 ndoms_cur = ndoms_new;
6964 7847
6965 register_sched_domain_sysctl(); 7848 register_sched_domain_sysctl();
@@ -7086,6 +7969,11 @@ void __init sched_init_smp(void)
7086{ 7969{
7087 cpumask_t non_isolated_cpus; 7970 cpumask_t non_isolated_cpus;
7088 7971
7972#if defined(CONFIG_NUMA)
7973 sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
7974 GFP_KERNEL);
7975 BUG_ON(sched_group_nodes_bycpu == NULL);
7976#endif
7089 get_online_cpus(); 7977 get_online_cpus();
7090 arch_init_sched_domains(&cpu_online_map); 7978 arch_init_sched_domains(&cpu_online_map);
7091 cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map); 7979 cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map);
@@ -7096,13 +7984,18 @@ void __init sched_init_smp(void)
7096 hotcpu_notifier(update_sched_domains, 0); 7984 hotcpu_notifier(update_sched_domains, 0);
7097 7985
7098 /* Move init over to a non-isolated CPU */ 7986 /* Move init over to a non-isolated CPU */
7099 if (set_cpus_allowed(current, non_isolated_cpus) < 0) 7987 if (set_cpus_allowed_ptr(current, &non_isolated_cpus) < 0)
7100 BUG(); 7988 BUG();
7101 sched_init_granularity(); 7989 sched_init_granularity();
7102} 7990}
7103#else 7991#else
7104void __init sched_init_smp(void) 7992void __init sched_init_smp(void)
7105{ 7993{
7994#if defined(CONFIG_NUMA)
7995 sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
7996 GFP_KERNEL);
7997 BUG_ON(sched_group_nodes_bycpu == NULL);
7998#endif
7106 sched_init_granularity(); 7999 sched_init_granularity();
7107} 8000}
7108#endif /* CONFIG_SMP */ 8001#endif /* CONFIG_SMP */
@@ -7117,6 +8010,7 @@ int in_sched_functions(unsigned long addr)
7117static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq) 8010static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
7118{ 8011{
7119 cfs_rq->tasks_timeline = RB_ROOT; 8012 cfs_rq->tasks_timeline = RB_ROOT;
8013 INIT_LIST_HEAD(&cfs_rq->tasks);
7120#ifdef CONFIG_FAIR_GROUP_SCHED 8014#ifdef CONFIG_FAIR_GROUP_SCHED
7121 cfs_rq->rq = rq; 8015 cfs_rq->rq = rq;
7122#endif 8016#endif
@@ -7146,6 +8040,8 @@ static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
7146 8040
7147 rt_rq->rt_time = 0; 8041 rt_rq->rt_time = 0;
7148 rt_rq->rt_throttled = 0; 8042 rt_rq->rt_throttled = 0;
8043 rt_rq->rt_runtime = 0;
8044 spin_lock_init(&rt_rq->rt_runtime_lock);
7149 8045
7150#ifdef CONFIG_RT_GROUP_SCHED 8046#ifdef CONFIG_RT_GROUP_SCHED
7151 rt_rq->rt_nr_boosted = 0; 8047 rt_rq->rt_nr_boosted = 0;
@@ -7154,10 +8050,11 @@ static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
7154} 8050}
7155 8051
7156#ifdef CONFIG_FAIR_GROUP_SCHED 8052#ifdef CONFIG_FAIR_GROUP_SCHED
7157static void init_tg_cfs_entry(struct rq *rq, struct task_group *tg, 8053static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
7158 struct cfs_rq *cfs_rq, struct sched_entity *se, 8054 struct sched_entity *se, int cpu, int add,
7159 int cpu, int add) 8055 struct sched_entity *parent)
7160{ 8056{
8057 struct rq *rq = cpu_rq(cpu);
7161 tg->cfs_rq[cpu] = cfs_rq; 8058 tg->cfs_rq[cpu] = cfs_rq;
7162 init_cfs_rq(cfs_rq, rq); 8059 init_cfs_rq(cfs_rq, rq);
7163 cfs_rq->tg = tg; 8060 cfs_rq->tg = tg;
@@ -7165,45 +8062,132 @@ static void init_tg_cfs_entry(struct rq *rq, struct task_group *tg,
7165 list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list); 8062 list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
7166 8063
7167 tg->se[cpu] = se; 8064 tg->se[cpu] = se;
7168 se->cfs_rq = &rq->cfs; 8065 /* se could be NULL for init_task_group */
8066 if (!se)
8067 return;
8068
8069 if (!parent)
8070 se->cfs_rq = &rq->cfs;
8071 else
8072 se->cfs_rq = parent->my_q;
8073
7169 se->my_q = cfs_rq; 8074 se->my_q = cfs_rq;
7170 se->load.weight = tg->shares; 8075 se->load.weight = tg->shares;
7171 se->load.inv_weight = div64_64(1ULL<<32, se->load.weight); 8076 se->load.inv_weight = div64_64(1ULL<<32, se->load.weight);
7172 se->parent = NULL; 8077 se->parent = parent;
7173} 8078}
7174#endif 8079#endif
7175 8080
7176#ifdef CONFIG_RT_GROUP_SCHED 8081#ifdef CONFIG_RT_GROUP_SCHED
7177static void init_tg_rt_entry(struct rq *rq, struct task_group *tg, 8082static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
7178 struct rt_rq *rt_rq, struct sched_rt_entity *rt_se, 8083 struct sched_rt_entity *rt_se, int cpu, int add,
7179 int cpu, int add) 8084 struct sched_rt_entity *parent)
7180{ 8085{
8086 struct rq *rq = cpu_rq(cpu);
8087
7181 tg->rt_rq[cpu] = rt_rq; 8088 tg->rt_rq[cpu] = rt_rq;
7182 init_rt_rq(rt_rq, rq); 8089 init_rt_rq(rt_rq, rq);
7183 rt_rq->tg = tg; 8090 rt_rq->tg = tg;
7184 rt_rq->rt_se = rt_se; 8091 rt_rq->rt_se = rt_se;
8092 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
7185 if (add) 8093 if (add)
7186 list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list); 8094 list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
7187 8095
7188 tg->rt_se[cpu] = rt_se; 8096 tg->rt_se[cpu] = rt_se;
8097 if (!rt_se)
8098 return;
8099
8100 if (!parent)
8101 rt_se->rt_rq = &rq->rt;
8102 else
8103 rt_se->rt_rq = parent->my_q;
8104
7189 rt_se->rt_rq = &rq->rt; 8105 rt_se->rt_rq = &rq->rt;
7190 rt_se->my_q = rt_rq; 8106 rt_se->my_q = rt_rq;
7191 rt_se->parent = NULL; 8107 rt_se->parent = parent;
7192 INIT_LIST_HEAD(&rt_se->run_list); 8108 INIT_LIST_HEAD(&rt_se->run_list);
7193} 8109}
7194#endif 8110#endif
7195 8111
7196void __init sched_init(void) 8112void __init sched_init(void)
7197{ 8113{
7198 int highest_cpu = 0;
7199 int i, j; 8114 int i, j;
8115 unsigned long alloc_size = 0, ptr;
8116
8117#ifdef CONFIG_FAIR_GROUP_SCHED
8118 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8119#endif
8120#ifdef CONFIG_RT_GROUP_SCHED
8121 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8122#endif
8123#ifdef CONFIG_USER_SCHED
8124 alloc_size *= 2;
8125#endif
8126 /*
8127 * As sched_init() is called before page_alloc is setup,
8128 * we use alloc_bootmem().
8129 */
8130 if (alloc_size) {
8131 ptr = (unsigned long)alloc_bootmem_low(alloc_size);
8132
8133#ifdef CONFIG_FAIR_GROUP_SCHED
8134 init_task_group.se = (struct sched_entity **)ptr;
8135 ptr += nr_cpu_ids * sizeof(void **);
8136
8137 init_task_group.cfs_rq = (struct cfs_rq **)ptr;
8138 ptr += nr_cpu_ids * sizeof(void **);
8139
8140#ifdef CONFIG_USER_SCHED
8141 root_task_group.se = (struct sched_entity **)ptr;
8142 ptr += nr_cpu_ids * sizeof(void **);
8143
8144 root_task_group.cfs_rq = (struct cfs_rq **)ptr;
8145 ptr += nr_cpu_ids * sizeof(void **);
8146#endif
8147#endif
8148#ifdef CONFIG_RT_GROUP_SCHED
8149 init_task_group.rt_se = (struct sched_rt_entity **)ptr;
8150 ptr += nr_cpu_ids * sizeof(void **);
8151
8152 init_task_group.rt_rq = (struct rt_rq **)ptr;
8153 ptr += nr_cpu_ids * sizeof(void **);
8154
8155#ifdef CONFIG_USER_SCHED
8156 root_task_group.rt_se = (struct sched_rt_entity **)ptr;
8157 ptr += nr_cpu_ids * sizeof(void **);
8158
8159 root_task_group.rt_rq = (struct rt_rq **)ptr;
8160 ptr += nr_cpu_ids * sizeof(void **);
8161#endif
8162#endif
8163 }
7200 8164
7201#ifdef CONFIG_SMP 8165#ifdef CONFIG_SMP
8166 init_aggregate();
7202 init_defrootdomain(); 8167 init_defrootdomain();
7203#endif 8168#endif
7204 8169
8170 init_rt_bandwidth(&def_rt_bandwidth,
8171 global_rt_period(), global_rt_runtime());
8172
8173#ifdef CONFIG_RT_GROUP_SCHED
8174 init_rt_bandwidth(&init_task_group.rt_bandwidth,
8175 global_rt_period(), global_rt_runtime());
8176#ifdef CONFIG_USER_SCHED
8177 init_rt_bandwidth(&root_task_group.rt_bandwidth,
8178 global_rt_period(), RUNTIME_INF);
8179#endif
8180#endif
8181
7205#ifdef CONFIG_GROUP_SCHED 8182#ifdef CONFIG_GROUP_SCHED
7206 list_add(&init_task_group.list, &task_groups); 8183 list_add(&init_task_group.list, &task_groups);
8184 INIT_LIST_HEAD(&init_task_group.children);
8185
8186#ifdef CONFIG_USER_SCHED
8187 INIT_LIST_HEAD(&root_task_group.children);
8188 init_task_group.parent = &root_task_group;
8189 list_add(&init_task_group.siblings, &root_task_group.children);
8190#endif
7207#endif 8191#endif
7208 8192
7209 for_each_possible_cpu(i) { 8193 for_each_possible_cpu(i) {
@@ -7214,26 +8198,68 @@ void __init sched_init(void)
7214 lockdep_set_class(&rq->lock, &rq->rq_lock_key); 8198 lockdep_set_class(&rq->lock, &rq->rq_lock_key);
7215 rq->nr_running = 0; 8199 rq->nr_running = 0;
7216 rq->clock = 1; 8200 rq->clock = 1;
8201 update_last_tick_seen(rq);
7217 init_cfs_rq(&rq->cfs, rq); 8202 init_cfs_rq(&rq->cfs, rq);
7218 init_rt_rq(&rq->rt, rq); 8203 init_rt_rq(&rq->rt, rq);
7219#ifdef CONFIG_FAIR_GROUP_SCHED 8204#ifdef CONFIG_FAIR_GROUP_SCHED
7220 init_task_group.shares = init_task_group_load; 8205 init_task_group.shares = init_task_group_load;
7221 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list); 8206 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
7222 init_tg_cfs_entry(rq, &init_task_group, 8207#ifdef CONFIG_CGROUP_SCHED
8208 /*
8209 * How much cpu bandwidth does init_task_group get?
8210 *
8211 * In case of task-groups formed thr' the cgroup filesystem, it
8212 * gets 100% of the cpu resources in the system. This overall
8213 * system cpu resource is divided among the tasks of
8214 * init_task_group and its child task-groups in a fair manner,
8215 * based on each entity's (task or task-group's) weight
8216 * (se->load.weight).
8217 *
8218 * In other words, if init_task_group has 10 tasks of weight
8219 * 1024) and two child groups A0 and A1 (of weight 1024 each),
8220 * then A0's share of the cpu resource is:
8221 *
8222 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
8223 *
8224 * We achieve this by letting init_task_group's tasks sit
8225 * directly in rq->cfs (i.e init_task_group->se[] = NULL).
8226 */
8227 init_tg_cfs_entry(&init_task_group, &rq->cfs, NULL, i, 1, NULL);
8228#elif defined CONFIG_USER_SCHED
8229 root_task_group.shares = NICE_0_LOAD;
8230 init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, 0, NULL);
8231 /*
8232 * In case of task-groups formed thr' the user id of tasks,
8233 * init_task_group represents tasks belonging to root user.
8234 * Hence it forms a sibling of all subsequent groups formed.
8235 * In this case, init_task_group gets only a fraction of overall
8236 * system cpu resource, based on the weight assigned to root
8237 * user's cpu share (INIT_TASK_GROUP_LOAD). This is accomplished
8238 * by letting tasks of init_task_group sit in a separate cfs_rq
8239 * (init_cfs_rq) and having one entity represent this group of
8240 * tasks in rq->cfs (i.e init_task_group->se[] != NULL).
8241 */
8242 init_tg_cfs_entry(&init_task_group,
7223 &per_cpu(init_cfs_rq, i), 8243 &per_cpu(init_cfs_rq, i),
7224 &per_cpu(init_sched_entity, i), i, 1); 8244 &per_cpu(init_sched_entity, i), i, 1,
8245 root_task_group.se[i]);
7225 8246
7226#endif 8247#endif
8248#endif /* CONFIG_FAIR_GROUP_SCHED */
8249
8250 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
7227#ifdef CONFIG_RT_GROUP_SCHED 8251#ifdef CONFIG_RT_GROUP_SCHED
7228 init_task_group.rt_runtime =
7229 sysctl_sched_rt_runtime * NSEC_PER_USEC;
7230 INIT_LIST_HEAD(&rq->leaf_rt_rq_list); 8252 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
7231 init_tg_rt_entry(rq, &init_task_group, 8253#ifdef CONFIG_CGROUP_SCHED
8254 init_tg_rt_entry(&init_task_group, &rq->rt, NULL, i, 1, NULL);
8255#elif defined CONFIG_USER_SCHED
8256 init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, 0, NULL);
8257 init_tg_rt_entry(&init_task_group,
7232 &per_cpu(init_rt_rq, i), 8258 &per_cpu(init_rt_rq, i),
7233 &per_cpu(init_sched_rt_entity, i), i, 1); 8259 &per_cpu(init_sched_rt_entity, i), i, 1,
8260 root_task_group.rt_se[i]);
8261#endif
7234#endif 8262#endif
7235 rq->rt_period_expire = 0;
7236 rq->rt_throttled = 0;
7237 8263
7238 for (j = 0; j < CPU_LOAD_IDX_MAX; j++) 8264 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
7239 rq->cpu_load[j] = 0; 8265 rq->cpu_load[j] = 0;
@@ -7250,7 +8276,6 @@ void __init sched_init(void)
7250#endif 8276#endif
7251 init_rq_hrtick(rq); 8277 init_rq_hrtick(rq);
7252 atomic_set(&rq->nr_iowait, 0); 8278 atomic_set(&rq->nr_iowait, 0);
7253 highest_cpu = i;
7254 } 8279 }
7255 8280
7256 set_load_weight(&init_task); 8281 set_load_weight(&init_task);
@@ -7260,7 +8285,6 @@ void __init sched_init(void)
7260#endif 8285#endif
7261 8286
7262#ifdef CONFIG_SMP 8287#ifdef CONFIG_SMP
7263 nr_cpu_ids = highest_cpu + 1;
7264 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL); 8288 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL);
7265#endif 8289#endif
7266 8290
@@ -7419,8 +8443,6 @@ void set_curr_task(int cpu, struct task_struct *p)
7419 8443
7420#endif 8444#endif
7421 8445
7422#ifdef CONFIG_GROUP_SCHED
7423
7424#ifdef CONFIG_FAIR_GROUP_SCHED 8446#ifdef CONFIG_FAIR_GROUP_SCHED
7425static void free_fair_sched_group(struct task_group *tg) 8447static void free_fair_sched_group(struct task_group *tg)
7426{ 8448{
@@ -7437,17 +8459,18 @@ static void free_fair_sched_group(struct task_group *tg)
7437 kfree(tg->se); 8459 kfree(tg->se);
7438} 8460}
7439 8461
7440static int alloc_fair_sched_group(struct task_group *tg) 8462static
8463int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
7441{ 8464{
7442 struct cfs_rq *cfs_rq; 8465 struct cfs_rq *cfs_rq;
7443 struct sched_entity *se; 8466 struct sched_entity *se, *parent_se;
7444 struct rq *rq; 8467 struct rq *rq;
7445 int i; 8468 int i;
7446 8469
7447 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * NR_CPUS, GFP_KERNEL); 8470 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
7448 if (!tg->cfs_rq) 8471 if (!tg->cfs_rq)
7449 goto err; 8472 goto err;
7450 tg->se = kzalloc(sizeof(se) * NR_CPUS, GFP_KERNEL); 8473 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
7451 if (!tg->se) 8474 if (!tg->se)
7452 goto err; 8475 goto err;
7453 8476
@@ -7466,7 +8489,8 @@ static int alloc_fair_sched_group(struct task_group *tg)
7466 if (!se) 8489 if (!se)
7467 goto err; 8490 goto err;
7468 8491
7469 init_tg_cfs_entry(rq, tg, cfs_rq, se, i, 0); 8492 parent_se = parent ? parent->se[i] : NULL;
8493 init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent_se);
7470 } 8494 }
7471 8495
7472 return 1; 8496 return 1;
@@ -7490,7 +8514,8 @@ static inline void free_fair_sched_group(struct task_group *tg)
7490{ 8514{
7491} 8515}
7492 8516
7493static inline int alloc_fair_sched_group(struct task_group *tg) 8517static inline
8518int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
7494{ 8519{
7495 return 1; 8520 return 1;
7496} 8521}
@@ -7509,6 +8534,8 @@ static void free_rt_sched_group(struct task_group *tg)
7509{ 8534{
7510 int i; 8535 int i;
7511 8536
8537 destroy_rt_bandwidth(&tg->rt_bandwidth);
8538
7512 for_each_possible_cpu(i) { 8539 for_each_possible_cpu(i) {
7513 if (tg->rt_rq) 8540 if (tg->rt_rq)
7514 kfree(tg->rt_rq[i]); 8541 kfree(tg->rt_rq[i]);
@@ -7520,21 +8547,23 @@ static void free_rt_sched_group(struct task_group *tg)
7520 kfree(tg->rt_se); 8547 kfree(tg->rt_se);
7521} 8548}
7522 8549
7523static int alloc_rt_sched_group(struct task_group *tg) 8550static
8551int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
7524{ 8552{
7525 struct rt_rq *rt_rq; 8553 struct rt_rq *rt_rq;
7526 struct sched_rt_entity *rt_se; 8554 struct sched_rt_entity *rt_se, *parent_se;
7527 struct rq *rq; 8555 struct rq *rq;
7528 int i; 8556 int i;
7529 8557
7530 tg->rt_rq = kzalloc(sizeof(rt_rq) * NR_CPUS, GFP_KERNEL); 8558 tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
7531 if (!tg->rt_rq) 8559 if (!tg->rt_rq)
7532 goto err; 8560 goto err;
7533 tg->rt_se = kzalloc(sizeof(rt_se) * NR_CPUS, GFP_KERNEL); 8561 tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
7534 if (!tg->rt_se) 8562 if (!tg->rt_se)
7535 goto err; 8563 goto err;
7536 8564
7537 tg->rt_runtime = 0; 8565 init_rt_bandwidth(&tg->rt_bandwidth,
8566 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
7538 8567
7539 for_each_possible_cpu(i) { 8568 for_each_possible_cpu(i) {
7540 rq = cpu_rq(i); 8569 rq = cpu_rq(i);
@@ -7549,7 +8578,8 @@ static int alloc_rt_sched_group(struct task_group *tg)
7549 if (!rt_se) 8578 if (!rt_se)
7550 goto err; 8579 goto err;
7551 8580
7552 init_tg_rt_entry(rq, tg, rt_rq, rt_se, i, 0); 8581 parent_se = parent ? parent->rt_se[i] : NULL;
8582 init_tg_rt_entry(tg, rt_rq, rt_se, i, 0, parent_se);
7553 } 8583 }
7554 8584
7555 return 1; 8585 return 1;
@@ -7573,7 +8603,8 @@ static inline void free_rt_sched_group(struct task_group *tg)
7573{ 8603{
7574} 8604}
7575 8605
7576static inline int alloc_rt_sched_group(struct task_group *tg) 8606static inline
8607int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
7577{ 8608{
7578 return 1; 8609 return 1;
7579} 8610}
@@ -7587,6 +8618,7 @@ static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
7587} 8618}
7588#endif 8619#endif
7589 8620
8621#ifdef CONFIG_GROUP_SCHED
7590static void free_sched_group(struct task_group *tg) 8622static void free_sched_group(struct task_group *tg)
7591{ 8623{
7592 free_fair_sched_group(tg); 8624 free_fair_sched_group(tg);
@@ -7595,7 +8627,7 @@ static void free_sched_group(struct task_group *tg)
7595} 8627}
7596 8628
7597/* allocate runqueue etc for a new task group */ 8629/* allocate runqueue etc for a new task group */
7598struct task_group *sched_create_group(void) 8630struct task_group *sched_create_group(struct task_group *parent)
7599{ 8631{
7600 struct task_group *tg; 8632 struct task_group *tg;
7601 unsigned long flags; 8633 unsigned long flags;
@@ -7605,10 +8637,10 @@ struct task_group *sched_create_group(void)
7605 if (!tg) 8637 if (!tg)
7606 return ERR_PTR(-ENOMEM); 8638 return ERR_PTR(-ENOMEM);
7607 8639
7608 if (!alloc_fair_sched_group(tg)) 8640 if (!alloc_fair_sched_group(tg, parent))
7609 goto err; 8641 goto err;
7610 8642
7611 if (!alloc_rt_sched_group(tg)) 8643 if (!alloc_rt_sched_group(tg, parent))
7612 goto err; 8644 goto err;
7613 8645
7614 spin_lock_irqsave(&task_group_lock, flags); 8646 spin_lock_irqsave(&task_group_lock, flags);
@@ -7617,6 +8649,12 @@ struct task_group *sched_create_group(void)
7617 register_rt_sched_group(tg, i); 8649 register_rt_sched_group(tg, i);
7618 } 8650 }
7619 list_add_rcu(&tg->list, &task_groups); 8651 list_add_rcu(&tg->list, &task_groups);
8652
8653 WARN_ON(!parent); /* root should already exist */
8654
8655 tg->parent = parent;
8656 list_add_rcu(&tg->siblings, &parent->children);
8657 INIT_LIST_HEAD(&tg->children);
7620 spin_unlock_irqrestore(&task_group_lock, flags); 8658 spin_unlock_irqrestore(&task_group_lock, flags);
7621 8659
7622 return tg; 8660 return tg;
@@ -7645,6 +8683,7 @@ void sched_destroy_group(struct task_group *tg)
7645 unregister_rt_sched_group(tg, i); 8683 unregister_rt_sched_group(tg, i);
7646 } 8684 }
7647 list_del_rcu(&tg->list); 8685 list_del_rcu(&tg->list);
8686 list_del_rcu(&tg->siblings);
7648 spin_unlock_irqrestore(&task_group_lock, flags); 8687 spin_unlock_irqrestore(&task_group_lock, flags);
7649 8688
7650 /* wait for possible concurrent references to cfs_rqs complete */ 8689 /* wait for possible concurrent references to cfs_rqs complete */
@@ -7688,16 +8727,14 @@ void sched_move_task(struct task_struct *tsk)
7688 8727
7689 task_rq_unlock(rq, &flags); 8728 task_rq_unlock(rq, &flags);
7690} 8729}
8730#endif
7691 8731
7692#ifdef CONFIG_FAIR_GROUP_SCHED 8732#ifdef CONFIG_FAIR_GROUP_SCHED
7693static void set_se_shares(struct sched_entity *se, unsigned long shares) 8733static void __set_se_shares(struct sched_entity *se, unsigned long shares)
7694{ 8734{
7695 struct cfs_rq *cfs_rq = se->cfs_rq; 8735 struct cfs_rq *cfs_rq = se->cfs_rq;
7696 struct rq *rq = cfs_rq->rq;
7697 int on_rq; 8736 int on_rq;
7698 8737
7699 spin_lock_irq(&rq->lock);
7700
7701 on_rq = se->on_rq; 8738 on_rq = se->on_rq;
7702 if (on_rq) 8739 if (on_rq)
7703 dequeue_entity(cfs_rq, se, 0); 8740 dequeue_entity(cfs_rq, se, 0);
@@ -7707,8 +8744,17 @@ static void set_se_shares(struct sched_entity *se, unsigned long shares)
7707 8744
7708 if (on_rq) 8745 if (on_rq)
7709 enqueue_entity(cfs_rq, se, 0); 8746 enqueue_entity(cfs_rq, se, 0);
8747}
7710 8748
7711 spin_unlock_irq(&rq->lock); 8749static void set_se_shares(struct sched_entity *se, unsigned long shares)
8750{
8751 struct cfs_rq *cfs_rq = se->cfs_rq;
8752 struct rq *rq = cfs_rq->rq;
8753 unsigned long flags;
8754
8755 spin_lock_irqsave(&rq->lock, flags);
8756 __set_se_shares(se, shares);
8757 spin_unlock_irqrestore(&rq->lock, flags);
7712} 8758}
7713 8759
7714static DEFINE_MUTEX(shares_mutex); 8760static DEFINE_MUTEX(shares_mutex);
@@ -7719,12 +8765,18 @@ int sched_group_set_shares(struct task_group *tg, unsigned long shares)
7719 unsigned long flags; 8765 unsigned long flags;
7720 8766
7721 /* 8767 /*
8768 * We can't change the weight of the root cgroup.
8769 */
8770 if (!tg->se[0])
8771 return -EINVAL;
8772
8773 /*
7722 * A weight of 0 or 1 can cause arithmetics problems. 8774 * A weight of 0 or 1 can cause arithmetics problems.
7723 * (The default weight is 1024 - so there's no practical 8775 * (The default weight is 1024 - so there's no practical
7724 * limitation from this.) 8776 * limitation from this.)
7725 */ 8777 */
7726 if (shares < 2) 8778 if (shares < MIN_SHARES)
7727 shares = 2; 8779 shares = MIN_SHARES;
7728 8780
7729 mutex_lock(&shares_mutex); 8781 mutex_lock(&shares_mutex);
7730 if (tg->shares == shares) 8782 if (tg->shares == shares)
@@ -7733,6 +8785,7 @@ int sched_group_set_shares(struct task_group *tg, unsigned long shares)
7733 spin_lock_irqsave(&task_group_lock, flags); 8785 spin_lock_irqsave(&task_group_lock, flags);
7734 for_each_possible_cpu(i) 8786 for_each_possible_cpu(i)
7735 unregister_fair_sched_group(tg, i); 8787 unregister_fair_sched_group(tg, i);
8788 list_del_rcu(&tg->siblings);
7736 spin_unlock_irqrestore(&task_group_lock, flags); 8789 spin_unlock_irqrestore(&task_group_lock, flags);
7737 8790
7738 /* wait for any ongoing reference to this group to finish */ 8791 /* wait for any ongoing reference to this group to finish */
@@ -7743,8 +8796,13 @@ int sched_group_set_shares(struct task_group *tg, unsigned long shares)
7743 * w/o tripping rebalance_share or load_balance_fair. 8796 * w/o tripping rebalance_share or load_balance_fair.
7744 */ 8797 */
7745 tg->shares = shares; 8798 tg->shares = shares;
7746 for_each_possible_cpu(i) 8799 for_each_possible_cpu(i) {
7747 set_se_shares(tg->se[i], shares); 8800 /*
8801 * force a rebalance
8802 */
8803 cfs_rq_set_shares(tg->cfs_rq[i], 0);
8804 set_se_shares(tg->se[i], shares/nr_cpu_ids);
8805 }
7748 8806
7749 /* 8807 /*
7750 * Enable load balance activity on this group, by inserting it back on 8808 * Enable load balance activity on this group, by inserting it back on
@@ -7753,6 +8811,7 @@ int sched_group_set_shares(struct task_group *tg, unsigned long shares)
7753 spin_lock_irqsave(&task_group_lock, flags); 8811 spin_lock_irqsave(&task_group_lock, flags);
7754 for_each_possible_cpu(i) 8812 for_each_possible_cpu(i)
7755 register_fair_sched_group(tg, i); 8813 register_fair_sched_group(tg, i);
8814 list_add_rcu(&tg->siblings, &tg->parent->children);
7756 spin_unlock_irqrestore(&task_group_lock, flags); 8815 spin_unlock_irqrestore(&task_group_lock, flags);
7757done: 8816done:
7758 mutex_unlock(&shares_mutex); 8817 mutex_unlock(&shares_mutex);
@@ -7779,26 +8838,58 @@ static unsigned long to_ratio(u64 period, u64 runtime)
7779 return div64_64(runtime << 16, period); 8838 return div64_64(runtime << 16, period);
7780} 8839}
7781 8840
8841#ifdef CONFIG_CGROUP_SCHED
8842static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
8843{
8844 struct task_group *tgi, *parent = tg->parent;
8845 unsigned long total = 0;
8846
8847 if (!parent) {
8848 if (global_rt_period() < period)
8849 return 0;
8850
8851 return to_ratio(period, runtime) <
8852 to_ratio(global_rt_period(), global_rt_runtime());
8853 }
8854
8855 if (ktime_to_ns(parent->rt_bandwidth.rt_period) < period)
8856 return 0;
8857
8858 rcu_read_lock();
8859 list_for_each_entry_rcu(tgi, &parent->children, siblings) {
8860 if (tgi == tg)
8861 continue;
8862
8863 total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
8864 tgi->rt_bandwidth.rt_runtime);
8865 }
8866 rcu_read_unlock();
8867
8868 return total + to_ratio(period, runtime) <
8869 to_ratio(ktime_to_ns(parent->rt_bandwidth.rt_period),
8870 parent->rt_bandwidth.rt_runtime);
8871}
8872#elif defined CONFIG_USER_SCHED
7782static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime) 8873static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
7783{ 8874{
7784 struct task_group *tgi; 8875 struct task_group *tgi;
7785 unsigned long total = 0; 8876 unsigned long total = 0;
7786 unsigned long global_ratio = 8877 unsigned long global_ratio =
7787 to_ratio(sysctl_sched_rt_period, 8878 to_ratio(global_rt_period(), global_rt_runtime());
7788 sysctl_sched_rt_runtime < 0 ?
7789 RUNTIME_INF : sysctl_sched_rt_runtime);
7790 8879
7791 rcu_read_lock(); 8880 rcu_read_lock();
7792 list_for_each_entry_rcu(tgi, &task_groups, list) { 8881 list_for_each_entry_rcu(tgi, &task_groups, list) {
7793 if (tgi == tg) 8882 if (tgi == tg)
7794 continue; 8883 continue;
7795 8884
7796 total += to_ratio(period, tgi->rt_runtime); 8885 total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
8886 tgi->rt_bandwidth.rt_runtime);
7797 } 8887 }
7798 rcu_read_unlock(); 8888 rcu_read_unlock();
7799 8889
7800 return total + to_ratio(period, runtime) < global_ratio; 8890 return total + to_ratio(period, runtime) < global_ratio;
7801} 8891}
8892#endif
7802 8893
7803/* Must be called with tasklist_lock held */ 8894/* Must be called with tasklist_lock held */
7804static inline int tg_has_rt_tasks(struct task_group *tg) 8895static inline int tg_has_rt_tasks(struct task_group *tg)
@@ -7811,19 +8902,14 @@ static inline int tg_has_rt_tasks(struct task_group *tg)
7811 return 0; 8902 return 0;
7812} 8903}
7813 8904
7814int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us) 8905static int tg_set_bandwidth(struct task_group *tg,
8906 u64 rt_period, u64 rt_runtime)
7815{ 8907{
7816 u64 rt_runtime, rt_period; 8908 int i, err = 0;
7817 int err = 0;
7818
7819 rt_period = (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
7820 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
7821 if (rt_runtime_us == -1)
7822 rt_runtime = RUNTIME_INF;
7823 8909
7824 mutex_lock(&rt_constraints_mutex); 8910 mutex_lock(&rt_constraints_mutex);
7825 read_lock(&tasklist_lock); 8911 read_lock(&tasklist_lock);
7826 if (rt_runtime_us == 0 && tg_has_rt_tasks(tg)) { 8912 if (rt_runtime == 0 && tg_has_rt_tasks(tg)) {
7827 err = -EBUSY; 8913 err = -EBUSY;
7828 goto unlock; 8914 goto unlock;
7829 } 8915 }
@@ -7831,7 +8917,19 @@ int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
7831 err = -EINVAL; 8917 err = -EINVAL;
7832 goto unlock; 8918 goto unlock;
7833 } 8919 }
7834 tg->rt_runtime = rt_runtime; 8920
8921 spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
8922 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
8923 tg->rt_bandwidth.rt_runtime = rt_runtime;
8924
8925 for_each_possible_cpu(i) {
8926 struct rt_rq *rt_rq = tg->rt_rq[i];
8927
8928 spin_lock(&rt_rq->rt_runtime_lock);
8929 rt_rq->rt_runtime = rt_runtime;
8930 spin_unlock(&rt_rq->rt_runtime_lock);
8931 }
8932 spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
7835 unlock: 8933 unlock:
7836 read_unlock(&tasklist_lock); 8934 read_unlock(&tasklist_lock);
7837 mutex_unlock(&rt_constraints_mutex); 8935 mutex_unlock(&rt_constraints_mutex);
@@ -7839,19 +8937,109 @@ int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
7839 return err; 8937 return err;
7840} 8938}
7841 8939
8940int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
8941{
8942 u64 rt_runtime, rt_period;
8943
8944 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
8945 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
8946 if (rt_runtime_us < 0)
8947 rt_runtime = RUNTIME_INF;
8948
8949 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8950}
8951
7842long sched_group_rt_runtime(struct task_group *tg) 8952long sched_group_rt_runtime(struct task_group *tg)
7843{ 8953{
7844 u64 rt_runtime_us; 8954 u64 rt_runtime_us;
7845 8955
7846 if (tg->rt_runtime == RUNTIME_INF) 8956 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
7847 return -1; 8957 return -1;
7848 8958
7849 rt_runtime_us = tg->rt_runtime; 8959 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
7850 do_div(rt_runtime_us, NSEC_PER_USEC); 8960 do_div(rt_runtime_us, NSEC_PER_USEC);
7851 return rt_runtime_us; 8961 return rt_runtime_us;
7852} 8962}
8963
8964int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
8965{
8966 u64 rt_runtime, rt_period;
8967
8968 rt_period = (u64)rt_period_us * NSEC_PER_USEC;
8969 rt_runtime = tg->rt_bandwidth.rt_runtime;
8970
8971 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8972}
8973
8974long sched_group_rt_period(struct task_group *tg)
8975{
8976 u64 rt_period_us;
8977
8978 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
8979 do_div(rt_period_us, NSEC_PER_USEC);
8980 return rt_period_us;
8981}
8982
8983static int sched_rt_global_constraints(void)
8984{
8985 int ret = 0;
8986
8987 mutex_lock(&rt_constraints_mutex);
8988 if (!__rt_schedulable(NULL, 1, 0))
8989 ret = -EINVAL;
8990 mutex_unlock(&rt_constraints_mutex);
8991
8992 return ret;
8993}
8994#else
8995static int sched_rt_global_constraints(void)
8996{
8997 unsigned long flags;
8998 int i;
8999
9000 spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
9001 for_each_possible_cpu(i) {
9002 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
9003
9004 spin_lock(&rt_rq->rt_runtime_lock);
9005 rt_rq->rt_runtime = global_rt_runtime();
9006 spin_unlock(&rt_rq->rt_runtime_lock);
9007 }
9008 spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
9009
9010 return 0;
9011}
7853#endif 9012#endif
7854#endif /* CONFIG_GROUP_SCHED */ 9013
9014int sched_rt_handler(struct ctl_table *table, int write,
9015 struct file *filp, void __user *buffer, size_t *lenp,
9016 loff_t *ppos)
9017{
9018 int ret;
9019 int old_period, old_runtime;
9020 static DEFINE_MUTEX(mutex);
9021
9022 mutex_lock(&mutex);
9023 old_period = sysctl_sched_rt_period;
9024 old_runtime = sysctl_sched_rt_runtime;
9025
9026 ret = proc_dointvec(table, write, filp, buffer, lenp, ppos);
9027
9028 if (!ret && write) {
9029 ret = sched_rt_global_constraints();
9030 if (ret) {
9031 sysctl_sched_rt_period = old_period;
9032 sysctl_sched_rt_runtime = old_runtime;
9033 } else {
9034 def_rt_bandwidth.rt_runtime = global_rt_runtime();
9035 def_rt_bandwidth.rt_period =
9036 ns_to_ktime(global_rt_period());
9037 }
9038 }
9039 mutex_unlock(&mutex);
9040
9041 return ret;
9042}
7855 9043
7856#ifdef CONFIG_CGROUP_SCHED 9044#ifdef CONFIG_CGROUP_SCHED
7857 9045
@@ -7865,7 +9053,7 @@ static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
7865static struct cgroup_subsys_state * 9053static struct cgroup_subsys_state *
7866cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp) 9054cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
7867{ 9055{
7868 struct task_group *tg; 9056 struct task_group *tg, *parent;
7869 9057
7870 if (!cgrp->parent) { 9058 if (!cgrp->parent) {
7871 /* This is early initialization for the top cgroup */ 9059 /* This is early initialization for the top cgroup */
@@ -7873,11 +9061,8 @@ cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
7873 return &init_task_group.css; 9061 return &init_task_group.css;
7874 } 9062 }
7875 9063
7876 /* we support only 1-level deep hierarchical scheduler atm */ 9064 parent = cgroup_tg(cgrp->parent);
7877 if (cgrp->parent->parent) 9065 tg = sched_create_group(parent);
7878 return ERR_PTR(-EINVAL);
7879
7880 tg = sched_create_group();
7881 if (IS_ERR(tg)) 9066 if (IS_ERR(tg))
7882 return ERR_PTR(-ENOMEM); 9067 return ERR_PTR(-ENOMEM);
7883 9068
@@ -7901,7 +9086,7 @@ cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
7901{ 9086{
7902#ifdef CONFIG_RT_GROUP_SCHED 9087#ifdef CONFIG_RT_GROUP_SCHED
7903 /* Don't accept realtime tasks when there is no way for them to run */ 9088 /* Don't accept realtime tasks when there is no way for them to run */
7904 if (rt_task(tsk) && cgroup_tg(cgrp)->rt_runtime == 0) 9089 if (rt_task(tsk) && cgroup_tg(cgrp)->rt_bandwidth.rt_runtime == 0)
7905 return -EINVAL; 9090 return -EINVAL;
7906#else 9091#else
7907 /* We don't support RT-tasks being in separate groups */ 9092 /* We don't support RT-tasks being in separate groups */
@@ -7935,7 +9120,7 @@ static u64 cpu_shares_read_uint(struct cgroup *cgrp, struct cftype *cft)
7935#endif 9120#endif
7936 9121
7937#ifdef CONFIG_RT_GROUP_SCHED 9122#ifdef CONFIG_RT_GROUP_SCHED
7938static int cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft, 9123static ssize_t cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
7939 struct file *file, 9124 struct file *file,
7940 const char __user *userbuf, 9125 const char __user *userbuf,
7941 size_t nbytes, loff_t *unused_ppos) 9126 size_t nbytes, loff_t *unused_ppos)
@@ -7979,6 +9164,17 @@ static ssize_t cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft,
7979 9164
7980 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); 9165 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
7981} 9166}
9167
9168static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype,
9169 u64 rt_period_us)
9170{
9171 return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us);
9172}
9173
9174static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft)
9175{
9176 return sched_group_rt_period(cgroup_tg(cgrp));
9177}
7982#endif 9178#endif
7983 9179
7984static struct cftype cpu_files[] = { 9180static struct cftype cpu_files[] = {
@@ -7995,6 +9191,11 @@ static struct cftype cpu_files[] = {
7995 .read = cpu_rt_runtime_read, 9191 .read = cpu_rt_runtime_read,
7996 .write = cpu_rt_runtime_write, 9192 .write = cpu_rt_runtime_write,
7997 }, 9193 },
9194 {
9195 .name = "rt_period_us",
9196 .read_uint = cpu_rt_period_read_uint,
9197 .write_uint = cpu_rt_period_write_uint,
9198 },
7998#endif 9199#endif
7999}; 9200};
8000 9201
@@ -8035,9 +9236,9 @@ struct cpuacct {
8035struct cgroup_subsys cpuacct_subsys; 9236struct cgroup_subsys cpuacct_subsys;
8036 9237
8037/* return cpu accounting group corresponding to this container */ 9238/* return cpu accounting group corresponding to this container */
8038static inline struct cpuacct *cgroup_ca(struct cgroup *cont) 9239static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
8039{ 9240{
8040 return container_of(cgroup_subsys_state(cont, cpuacct_subsys_id), 9241 return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
8041 struct cpuacct, css); 9242 struct cpuacct, css);
8042} 9243}
8043 9244
@@ -8050,7 +9251,7 @@ static inline struct cpuacct *task_ca(struct task_struct *tsk)
8050 9251
8051/* create a new cpu accounting group */ 9252/* create a new cpu accounting group */
8052static struct cgroup_subsys_state *cpuacct_create( 9253static struct cgroup_subsys_state *cpuacct_create(
8053 struct cgroup_subsys *ss, struct cgroup *cont) 9254 struct cgroup_subsys *ss, struct cgroup *cgrp)
8054{ 9255{
8055 struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL); 9256 struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
8056 9257
@@ -8068,18 +9269,18 @@ static struct cgroup_subsys_state *cpuacct_create(
8068 9269
8069/* destroy an existing cpu accounting group */ 9270/* destroy an existing cpu accounting group */
8070static void 9271static void
8071cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cont) 9272cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
8072{ 9273{
8073 struct cpuacct *ca = cgroup_ca(cont); 9274 struct cpuacct *ca = cgroup_ca(cgrp);
8074 9275
8075 free_percpu(ca->cpuusage); 9276 free_percpu(ca->cpuusage);
8076 kfree(ca); 9277 kfree(ca);
8077} 9278}
8078 9279
8079/* return total cpu usage (in nanoseconds) of a group */ 9280/* return total cpu usage (in nanoseconds) of a group */
8080static u64 cpuusage_read(struct cgroup *cont, struct cftype *cft) 9281static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
8081{ 9282{
8082 struct cpuacct *ca = cgroup_ca(cont); 9283 struct cpuacct *ca = cgroup_ca(cgrp);
8083 u64 totalcpuusage = 0; 9284 u64 totalcpuusage = 0;
8084 int i; 9285 int i;
8085 9286
@@ -8098,16 +9299,40 @@ static u64 cpuusage_read(struct cgroup *cont, struct cftype *cft)
8098 return totalcpuusage; 9299 return totalcpuusage;
8099} 9300}
8100 9301
9302static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype,
9303 u64 reset)
9304{
9305 struct cpuacct *ca = cgroup_ca(cgrp);
9306 int err = 0;
9307 int i;
9308
9309 if (reset) {
9310 err = -EINVAL;
9311 goto out;
9312 }
9313
9314 for_each_possible_cpu(i) {
9315 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
9316
9317 spin_lock_irq(&cpu_rq(i)->lock);
9318 *cpuusage = 0;
9319 spin_unlock_irq(&cpu_rq(i)->lock);
9320 }
9321out:
9322 return err;
9323}
9324
8101static struct cftype files[] = { 9325static struct cftype files[] = {
8102 { 9326 {
8103 .name = "usage", 9327 .name = "usage",
8104 .read_uint = cpuusage_read, 9328 .read_uint = cpuusage_read,
9329 .write_uint = cpuusage_write,
8105 }, 9330 },
8106}; 9331};
8107 9332
8108static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cont) 9333static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
8109{ 9334{
8110 return cgroup_add_files(cont, ss, files, ARRAY_SIZE(files)); 9335 return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files));
8111} 9336}
8112 9337
8113/* 9338/*