aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/sched.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/sched.c')
-rw-r--r--kernel/sched.c1481
1 files changed, 839 insertions, 642 deletions
diff --git a/kernel/sched.c b/kernel/sched.c
index e4bb1dd7b308..deb5ac8c12f3 100644
--- a/kernel/sched.c
+++ b/kernel/sched.c
@@ -118,6 +118,12 @@
118 */ 118 */
119#define RUNTIME_INF ((u64)~0ULL) 119#define RUNTIME_INF ((u64)~0ULL)
120 120
121DEFINE_TRACE(sched_wait_task);
122DEFINE_TRACE(sched_wakeup);
123DEFINE_TRACE(sched_wakeup_new);
124DEFINE_TRACE(sched_switch);
125DEFINE_TRACE(sched_migrate_task);
126
121#ifdef CONFIG_SMP 127#ifdef CONFIG_SMP
122/* 128/*
123 * Divide a load by a sched group cpu_power : (load / sg->__cpu_power) 129 * Divide a load by a sched group cpu_power : (load / sg->__cpu_power)
@@ -203,7 +209,6 @@ void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
203 hrtimer_init(&rt_b->rt_period_timer, 209 hrtimer_init(&rt_b->rt_period_timer,
204 CLOCK_MONOTONIC, HRTIMER_MODE_REL); 210 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
205 rt_b->rt_period_timer.function = sched_rt_period_timer; 211 rt_b->rt_period_timer.function = sched_rt_period_timer;
206 rt_b->rt_period_timer.cb_mode = HRTIMER_CB_IRQSAFE_UNLOCKED;
207} 212}
208 213
209static inline int rt_bandwidth_enabled(void) 214static inline int rt_bandwidth_enabled(void)
@@ -261,6 +266,10 @@ struct task_group {
261 struct cgroup_subsys_state css; 266 struct cgroup_subsys_state css;
262#endif 267#endif
263 268
269#ifdef CONFIG_USER_SCHED
270 uid_t uid;
271#endif
272
264#ifdef CONFIG_FAIR_GROUP_SCHED 273#ifdef CONFIG_FAIR_GROUP_SCHED
265 /* schedulable entities of this group on each cpu */ 274 /* schedulable entities of this group on each cpu */
266 struct sched_entity **se; 275 struct sched_entity **se;
@@ -286,6 +295,12 @@ struct task_group {
286 295
287#ifdef CONFIG_USER_SCHED 296#ifdef CONFIG_USER_SCHED
288 297
298/* Helper function to pass uid information to create_sched_user() */
299void set_tg_uid(struct user_struct *user)
300{
301 user->tg->uid = user->uid;
302}
303
289/* 304/*
290 * Root task group. 305 * Root task group.
291 * Every UID task group (including init_task_group aka UID-0) will 306 * Every UID task group (including init_task_group aka UID-0) will
@@ -345,7 +360,9 @@ static inline struct task_group *task_group(struct task_struct *p)
345 struct task_group *tg; 360 struct task_group *tg;
346 361
347#ifdef CONFIG_USER_SCHED 362#ifdef CONFIG_USER_SCHED
348 tg = p->user->tg; 363 rcu_read_lock();
364 tg = __task_cred(p)->user->tg;
365 rcu_read_unlock();
349#elif defined(CONFIG_CGROUP_SCHED) 366#elif defined(CONFIG_CGROUP_SCHED)
350 tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id), 367 tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id),
351 struct task_group, css); 368 struct task_group, css);
@@ -481,18 +498,26 @@ struct rt_rq {
481 */ 498 */
482struct root_domain { 499struct root_domain {
483 atomic_t refcount; 500 atomic_t refcount;
484 cpumask_t span; 501 cpumask_var_t span;
485 cpumask_t online; 502 cpumask_var_t online;
486 503
487 /* 504 /*
488 * The "RT overload" flag: it gets set if a CPU has more than 505 * The "RT overload" flag: it gets set if a CPU has more than
489 * one runnable RT task. 506 * one runnable RT task.
490 */ 507 */
491 cpumask_t rto_mask; 508 cpumask_var_t rto_mask;
492 atomic_t rto_count; 509 atomic_t rto_count;
493#ifdef CONFIG_SMP 510#ifdef CONFIG_SMP
494 struct cpupri cpupri; 511 struct cpupri cpupri;
495#endif 512#endif
513#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
514 /*
515 * Preferred wake up cpu nominated by sched_mc balance that will be
516 * used when most cpus are idle in the system indicating overall very
517 * low system utilisation. Triggered at POWERSAVINGS_BALANCE_WAKEUP(2)
518 */
519 unsigned int sched_mc_preferred_wakeup_cpu;
520#endif
496}; 521};
497 522
498/* 523/*
@@ -586,6 +611,8 @@ struct rq {
586#ifdef CONFIG_SCHEDSTATS 611#ifdef CONFIG_SCHEDSTATS
587 /* latency stats */ 612 /* latency stats */
588 struct sched_info rq_sched_info; 613 struct sched_info rq_sched_info;
614 unsigned long long rq_cpu_time;
615 /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
589 616
590 /* sys_sched_yield() stats */ 617 /* sys_sched_yield() stats */
591 unsigned int yld_exp_empty; 618 unsigned int yld_exp_empty;
@@ -703,45 +730,18 @@ static __read_mostly char *sched_feat_names[] = {
703 730
704#undef SCHED_FEAT 731#undef SCHED_FEAT
705 732
706static int sched_feat_open(struct inode *inode, struct file *filp) 733static int sched_feat_show(struct seq_file *m, void *v)
707{ 734{
708 filp->private_data = inode->i_private;
709 return 0;
710}
711
712static ssize_t
713sched_feat_read(struct file *filp, char __user *ubuf,
714 size_t cnt, loff_t *ppos)
715{
716 char *buf;
717 int r = 0;
718 int len = 0;
719 int i; 735 int i;
720 736
721 for (i = 0; sched_feat_names[i]; i++) { 737 for (i = 0; sched_feat_names[i]; i++) {
722 len += strlen(sched_feat_names[i]); 738 if (!(sysctl_sched_features & (1UL << i)))
723 len += 4; 739 seq_puts(m, "NO_");
724 } 740 seq_printf(m, "%s ", sched_feat_names[i]);
725
726 buf = kmalloc(len + 2, GFP_KERNEL);
727 if (!buf)
728 return -ENOMEM;
729
730 for (i = 0; sched_feat_names[i]; i++) {
731 if (sysctl_sched_features & (1UL << i))
732 r += sprintf(buf + r, "%s ", sched_feat_names[i]);
733 else
734 r += sprintf(buf + r, "NO_%s ", sched_feat_names[i]);
735 } 741 }
742 seq_puts(m, "\n");
736 743
737 r += sprintf(buf + r, "\n"); 744 return 0;
738 WARN_ON(r >= len + 2);
739
740 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
741
742 kfree(buf);
743
744 return r;
745} 745}
746 746
747static ssize_t 747static ssize_t
@@ -786,10 +786,17 @@ sched_feat_write(struct file *filp, const char __user *ubuf,
786 return cnt; 786 return cnt;
787} 787}
788 788
789static int sched_feat_open(struct inode *inode, struct file *filp)
790{
791 return single_open(filp, sched_feat_show, NULL);
792}
793
789static struct file_operations sched_feat_fops = { 794static struct file_operations sched_feat_fops = {
790 .open = sched_feat_open, 795 .open = sched_feat_open,
791 .read = sched_feat_read, 796 .write = sched_feat_write,
792 .write = sched_feat_write, 797 .read = seq_read,
798 .llseek = seq_lseek,
799 .release = single_release,
793}; 800};
794 801
795static __init int sched_init_debug(void) 802static __init int sched_init_debug(void)
@@ -1139,7 +1146,6 @@ static void init_rq_hrtick(struct rq *rq)
1139 1146
1140 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 1147 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1141 rq->hrtick_timer.function = hrtick; 1148 rq->hrtick_timer.function = hrtick;
1142 rq->hrtick_timer.cb_mode = HRTIMER_CB_IRQSAFE_PERCPU;
1143} 1149}
1144#else /* CONFIG_SCHED_HRTICK */ 1150#else /* CONFIG_SCHED_HRTICK */
1145static inline void hrtick_clear(struct rq *rq) 1151static inline void hrtick_clear(struct rq *rq)
@@ -1474,27 +1480,13 @@ static void
1474update_group_shares_cpu(struct task_group *tg, int cpu, 1480update_group_shares_cpu(struct task_group *tg, int cpu,
1475 unsigned long sd_shares, unsigned long sd_rq_weight) 1481 unsigned long sd_shares, unsigned long sd_rq_weight)
1476{ 1482{
1477 int boost = 0;
1478 unsigned long shares; 1483 unsigned long shares;
1479 unsigned long rq_weight; 1484 unsigned long rq_weight;
1480 1485
1481 if (!tg->se[cpu]) 1486 if (!tg->se[cpu])
1482 return; 1487 return;
1483 1488
1484 rq_weight = tg->cfs_rq[cpu]->load.weight; 1489 rq_weight = tg->cfs_rq[cpu]->rq_weight;
1485
1486 /*
1487 * If there are currently no tasks on the cpu pretend there is one of
1488 * average load so that when a new task gets to run here it will not
1489 * get delayed by group starvation.
1490 */
1491 if (!rq_weight) {
1492 boost = 1;
1493 rq_weight = NICE_0_LOAD;
1494 }
1495
1496 if (unlikely(rq_weight > sd_rq_weight))
1497 rq_weight = sd_rq_weight;
1498 1490
1499 /* 1491 /*
1500 * \Sum shares * rq_weight 1492 * \Sum shares * rq_weight
@@ -1502,7 +1494,7 @@ update_group_shares_cpu(struct task_group *tg, int cpu,
1502 * \Sum rq_weight 1494 * \Sum rq_weight
1503 * 1495 *
1504 */ 1496 */
1505 shares = (sd_shares * rq_weight) / (sd_rq_weight + 1); 1497 shares = (sd_shares * rq_weight) / sd_rq_weight;
1506 shares = clamp_t(unsigned long, shares, MIN_SHARES, MAX_SHARES); 1498 shares = clamp_t(unsigned long, shares, MIN_SHARES, MAX_SHARES);
1507 1499
1508 if (abs(shares - tg->se[cpu]->load.weight) > 1500 if (abs(shares - tg->se[cpu]->load.weight) >
@@ -1511,11 +1503,7 @@ update_group_shares_cpu(struct task_group *tg, int cpu,
1511 unsigned long flags; 1503 unsigned long flags;
1512 1504
1513 spin_lock_irqsave(&rq->lock, flags); 1505 spin_lock_irqsave(&rq->lock, flags);
1514 /* 1506 tg->cfs_rq[cpu]->shares = shares;
1515 * record the actual number of shares, not the boosted amount.
1516 */
1517 tg->cfs_rq[cpu]->shares = boost ? 0 : shares;
1518 tg->cfs_rq[cpu]->rq_weight = rq_weight;
1519 1507
1520 __set_se_shares(tg->se[cpu], shares); 1508 __set_se_shares(tg->se[cpu], shares);
1521 spin_unlock_irqrestore(&rq->lock, flags); 1509 spin_unlock_irqrestore(&rq->lock, flags);
@@ -1529,13 +1517,23 @@ update_group_shares_cpu(struct task_group *tg, int cpu,
1529 */ 1517 */
1530static int tg_shares_up(struct task_group *tg, void *data) 1518static int tg_shares_up(struct task_group *tg, void *data)
1531{ 1519{
1532 unsigned long rq_weight = 0; 1520 unsigned long weight, rq_weight = 0;
1533 unsigned long shares = 0; 1521 unsigned long shares = 0;
1534 struct sched_domain *sd = data; 1522 struct sched_domain *sd = data;
1535 int i; 1523 int i;
1536 1524
1537 for_each_cpu_mask(i, sd->span) { 1525 for_each_cpu(i, sched_domain_span(sd)) {
1538 rq_weight += tg->cfs_rq[i]->load.weight; 1526 /*
1527 * If there are currently no tasks on the cpu pretend there
1528 * is one of average load so that when a new task gets to
1529 * run here it will not get delayed by group starvation.
1530 */
1531 weight = tg->cfs_rq[i]->load.weight;
1532 if (!weight)
1533 weight = NICE_0_LOAD;
1534
1535 tg->cfs_rq[i]->rq_weight = weight;
1536 rq_weight += weight;
1539 shares += tg->cfs_rq[i]->shares; 1537 shares += tg->cfs_rq[i]->shares;
1540 } 1538 }
1541 1539
@@ -1545,10 +1543,7 @@ static int tg_shares_up(struct task_group *tg, void *data)
1545 if (!sd->parent || !(sd->parent->flags & SD_LOAD_BALANCE)) 1543 if (!sd->parent || !(sd->parent->flags & SD_LOAD_BALANCE))
1546 shares = tg->shares; 1544 shares = tg->shares;
1547 1545
1548 if (!rq_weight) 1546 for_each_cpu(i, sched_domain_span(sd))
1549 rq_weight = cpus_weight(sd->span) * NICE_0_LOAD;
1550
1551 for_each_cpu_mask(i, sd->span)
1552 update_group_shares_cpu(tg, i, shares, rq_weight); 1547 update_group_shares_cpu(tg, i, shares, rq_weight);
1553 1548
1554 return 0; 1549 return 0;
@@ -1612,6 +1607,39 @@ static inline void update_shares_locked(struct rq *rq, struct sched_domain *sd)
1612 1607
1613#endif 1608#endif
1614 1609
1610/*
1611 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
1612 */
1613static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
1614 __releases(this_rq->lock)
1615 __acquires(busiest->lock)
1616 __acquires(this_rq->lock)
1617{
1618 int ret = 0;
1619
1620 if (unlikely(!irqs_disabled())) {
1621 /* printk() doesn't work good under rq->lock */
1622 spin_unlock(&this_rq->lock);
1623 BUG_ON(1);
1624 }
1625 if (unlikely(!spin_trylock(&busiest->lock))) {
1626 if (busiest < this_rq) {
1627 spin_unlock(&this_rq->lock);
1628 spin_lock(&busiest->lock);
1629 spin_lock_nested(&this_rq->lock, SINGLE_DEPTH_NESTING);
1630 ret = 1;
1631 } else
1632 spin_lock_nested(&busiest->lock, SINGLE_DEPTH_NESTING);
1633 }
1634 return ret;
1635}
1636
1637static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
1638 __releases(busiest->lock)
1639{
1640 spin_unlock(&busiest->lock);
1641 lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
1642}
1615#endif 1643#endif
1616 1644
1617#ifdef CONFIG_FAIR_GROUP_SCHED 1645#ifdef CONFIG_FAIR_GROUP_SCHED
@@ -1845,6 +1873,8 @@ void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
1845 1873
1846 clock_offset = old_rq->clock - new_rq->clock; 1874 clock_offset = old_rq->clock - new_rq->clock;
1847 1875
1876 trace_sched_migrate_task(p, task_cpu(p), new_cpu);
1877
1848#ifdef CONFIG_SCHEDSTATS 1878#ifdef CONFIG_SCHEDSTATS
1849 if (p->se.wait_start) 1879 if (p->se.wait_start)
1850 p->se.wait_start -= clock_offset; 1880 p->se.wait_start -= clock_offset;
@@ -2079,15 +2109,17 @@ find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
2079 int i; 2109 int i;
2080 2110
2081 /* Skip over this group if it has no CPUs allowed */ 2111 /* Skip over this group if it has no CPUs allowed */
2082 if (!cpus_intersects(group->cpumask, p->cpus_allowed)) 2112 if (!cpumask_intersects(sched_group_cpus(group),
2113 &p->cpus_allowed))
2083 continue; 2114 continue;
2084 2115
2085 local_group = cpu_isset(this_cpu, group->cpumask); 2116 local_group = cpumask_test_cpu(this_cpu,
2117 sched_group_cpus(group));
2086 2118
2087 /* Tally up the load of all CPUs in the group */ 2119 /* Tally up the load of all CPUs in the group */
2088 avg_load = 0; 2120 avg_load = 0;
2089 2121
2090 for_each_cpu_mask_nr(i, group->cpumask) { 2122 for_each_cpu(i, sched_group_cpus(group)) {
2091 /* Bias balancing toward cpus of our domain */ 2123 /* Bias balancing toward cpus of our domain */
2092 if (local_group) 2124 if (local_group)
2093 load = source_load(i, load_idx); 2125 load = source_load(i, load_idx);
@@ -2119,17 +2151,14 @@ find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
2119 * find_idlest_cpu - find the idlest cpu among the cpus in group. 2151 * find_idlest_cpu - find the idlest cpu among the cpus in group.
2120 */ 2152 */
2121static int 2153static int
2122find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu, 2154find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
2123 cpumask_t *tmp)
2124{ 2155{
2125 unsigned long load, min_load = ULONG_MAX; 2156 unsigned long load, min_load = ULONG_MAX;
2126 int idlest = -1; 2157 int idlest = -1;
2127 int i; 2158 int i;
2128 2159
2129 /* Traverse only the allowed CPUs */ 2160 /* Traverse only the allowed CPUs */
2130 cpus_and(*tmp, group->cpumask, p->cpus_allowed); 2161 for_each_cpu_and(i, sched_group_cpus(group), &p->cpus_allowed) {
2131
2132 for_each_cpu_mask_nr(i, *tmp) {
2133 load = weighted_cpuload(i); 2162 load = weighted_cpuload(i);
2134 2163
2135 if (load < min_load || (load == min_load && i == this_cpu)) { 2164 if (load < min_load || (load == min_load && i == this_cpu)) {
@@ -2171,7 +2200,6 @@ static int sched_balance_self(int cpu, int flag)
2171 update_shares(sd); 2200 update_shares(sd);
2172 2201
2173 while (sd) { 2202 while (sd) {
2174 cpumask_t span, tmpmask;
2175 struct sched_group *group; 2203 struct sched_group *group;
2176 int new_cpu, weight; 2204 int new_cpu, weight;
2177 2205
@@ -2180,14 +2208,13 @@ static int sched_balance_self(int cpu, int flag)
2180 continue; 2208 continue;
2181 } 2209 }
2182 2210
2183 span = sd->span;
2184 group = find_idlest_group(sd, t, cpu); 2211 group = find_idlest_group(sd, t, cpu);
2185 if (!group) { 2212 if (!group) {
2186 sd = sd->child; 2213 sd = sd->child;
2187 continue; 2214 continue;
2188 } 2215 }
2189 2216
2190 new_cpu = find_idlest_cpu(group, t, cpu, &tmpmask); 2217 new_cpu = find_idlest_cpu(group, t, cpu);
2191 if (new_cpu == -1 || new_cpu == cpu) { 2218 if (new_cpu == -1 || new_cpu == cpu) {
2192 /* Now try balancing at a lower domain level of cpu */ 2219 /* Now try balancing at a lower domain level of cpu */
2193 sd = sd->child; 2220 sd = sd->child;
@@ -2196,10 +2223,10 @@ static int sched_balance_self(int cpu, int flag)
2196 2223
2197 /* Now try balancing at a lower domain level of new_cpu */ 2224 /* Now try balancing at a lower domain level of new_cpu */
2198 cpu = new_cpu; 2225 cpu = new_cpu;
2226 weight = cpumask_weight(sched_domain_span(sd));
2199 sd = NULL; 2227 sd = NULL;
2200 weight = cpus_weight(span);
2201 for_each_domain(cpu, tmp) { 2228 for_each_domain(cpu, tmp) {
2202 if (weight <= cpus_weight(tmp->span)) 2229 if (weight <= cpumask_weight(sched_domain_span(tmp)))
2203 break; 2230 break;
2204 if (tmp->flags & flag) 2231 if (tmp->flags & flag)
2205 sd = tmp; 2232 sd = tmp;
@@ -2244,7 +2271,7 @@ static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
2244 cpu = task_cpu(p); 2271 cpu = task_cpu(p);
2245 2272
2246 for_each_domain(this_cpu, sd) { 2273 for_each_domain(this_cpu, sd) {
2247 if (cpu_isset(cpu, sd->span)) { 2274 if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
2248 update_shares(sd); 2275 update_shares(sd);
2249 break; 2276 break;
2250 } 2277 }
@@ -2254,6 +2281,7 @@ static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
2254 2281
2255 smp_wmb(); 2282 smp_wmb();
2256 rq = task_rq_lock(p, &flags); 2283 rq = task_rq_lock(p, &flags);
2284 update_rq_clock(rq);
2257 old_state = p->state; 2285 old_state = p->state;
2258 if (!(old_state & state)) 2286 if (!(old_state & state))
2259 goto out; 2287 goto out;
@@ -2292,7 +2320,7 @@ static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
2292 else { 2320 else {
2293 struct sched_domain *sd; 2321 struct sched_domain *sd;
2294 for_each_domain(this_cpu, sd) { 2322 for_each_domain(this_cpu, sd) {
2295 if (cpu_isset(cpu, sd->span)) { 2323 if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
2296 schedstat_inc(sd, ttwu_wake_remote); 2324 schedstat_inc(sd, ttwu_wake_remote);
2297 break; 2325 break;
2298 } 2326 }
@@ -2311,12 +2339,11 @@ out_activate:
2311 schedstat_inc(p, se.nr_wakeups_local); 2339 schedstat_inc(p, se.nr_wakeups_local);
2312 else 2340 else
2313 schedstat_inc(p, se.nr_wakeups_remote); 2341 schedstat_inc(p, se.nr_wakeups_remote);
2314 update_rq_clock(rq);
2315 activate_task(rq, p, 1); 2342 activate_task(rq, p, 1);
2316 success = 1; 2343 success = 1;
2317 2344
2318out_running: 2345out_running:
2319 trace_sched_wakeup(rq, p); 2346 trace_sched_wakeup(rq, p, success);
2320 check_preempt_curr(rq, p, sync); 2347 check_preempt_curr(rq, p, sync);
2321 2348
2322 p->state = TASK_RUNNING; 2349 p->state = TASK_RUNNING;
@@ -2449,7 +2476,7 @@ void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
2449 p->sched_class->task_new(rq, p); 2476 p->sched_class->task_new(rq, p);
2450 inc_nr_running(rq); 2477 inc_nr_running(rq);
2451 } 2478 }
2452 trace_sched_wakeup_new(rq, p); 2479 trace_sched_wakeup_new(rq, p, 1);
2453 check_preempt_curr(rq, p, 0); 2480 check_preempt_curr(rq, p, 0);
2454#ifdef CONFIG_SMP 2481#ifdef CONFIG_SMP
2455 if (p->sched_class->task_wake_up) 2482 if (p->sched_class->task_wake_up)
@@ -2812,40 +2839,6 @@ static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
2812} 2839}
2813 2840
2814/* 2841/*
2815 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
2816 */
2817static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
2818 __releases(this_rq->lock)
2819 __acquires(busiest->lock)
2820 __acquires(this_rq->lock)
2821{
2822 int ret = 0;
2823
2824 if (unlikely(!irqs_disabled())) {
2825 /* printk() doesn't work good under rq->lock */
2826 spin_unlock(&this_rq->lock);
2827 BUG_ON(1);
2828 }
2829 if (unlikely(!spin_trylock(&busiest->lock))) {
2830 if (busiest < this_rq) {
2831 spin_unlock(&this_rq->lock);
2832 spin_lock(&busiest->lock);
2833 spin_lock_nested(&this_rq->lock, SINGLE_DEPTH_NESTING);
2834 ret = 1;
2835 } else
2836 spin_lock_nested(&busiest->lock, SINGLE_DEPTH_NESTING);
2837 }
2838 return ret;
2839}
2840
2841static void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
2842 __releases(busiest->lock)
2843{
2844 spin_unlock(&busiest->lock);
2845 lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
2846}
2847
2848/*
2849 * If dest_cpu is allowed for this process, migrate the task to it. 2842 * If dest_cpu is allowed for this process, migrate the task to it.
2850 * This is accomplished by forcing the cpu_allowed mask to only 2843 * This is accomplished by forcing the cpu_allowed mask to only
2851 * allow dest_cpu, which will force the cpu onto dest_cpu. Then 2844 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
@@ -2858,11 +2851,10 @@ static void sched_migrate_task(struct task_struct *p, int dest_cpu)
2858 struct rq *rq; 2851 struct rq *rq;
2859 2852
2860 rq = task_rq_lock(p, &flags); 2853 rq = task_rq_lock(p, &flags);
2861 if (!cpu_isset(dest_cpu, p->cpus_allowed) 2854 if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed)
2862 || unlikely(!cpu_active(dest_cpu))) 2855 || unlikely(!cpu_active(dest_cpu)))
2863 goto out; 2856 goto out;
2864 2857
2865 trace_sched_migrate_task(rq, p, dest_cpu);
2866 /* force the process onto the specified CPU */ 2858 /* force the process onto the specified CPU */
2867 if (migrate_task(p, dest_cpu, &req)) { 2859 if (migrate_task(p, dest_cpu, &req)) {
2868 /* Need to wait for migration thread (might exit: take ref). */ 2860 /* Need to wait for migration thread (might exit: take ref). */
@@ -2924,7 +2916,7 @@ int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
2924 * 2) cannot be migrated to this CPU due to cpus_allowed, or 2916 * 2) cannot be migrated to this CPU due to cpus_allowed, or
2925 * 3) are cache-hot on their current CPU. 2917 * 3) are cache-hot on their current CPU.
2926 */ 2918 */
2927 if (!cpu_isset(this_cpu, p->cpus_allowed)) { 2919 if (!cpumask_test_cpu(this_cpu, &p->cpus_allowed)) {
2928 schedstat_inc(p, se.nr_failed_migrations_affine); 2920 schedstat_inc(p, se.nr_failed_migrations_affine);
2929 return 0; 2921 return 0;
2930 } 2922 }
@@ -3099,7 +3091,7 @@ static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3099static struct sched_group * 3091static struct sched_group *
3100find_busiest_group(struct sched_domain *sd, int this_cpu, 3092find_busiest_group(struct sched_domain *sd, int this_cpu,
3101 unsigned long *imbalance, enum cpu_idle_type idle, 3093 unsigned long *imbalance, enum cpu_idle_type idle,
3102 int *sd_idle, const cpumask_t *cpus, int *balance) 3094 int *sd_idle, const struct cpumask *cpus, int *balance)
3103{ 3095{
3104 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups; 3096 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
3105 unsigned long max_load, avg_load, total_load, this_load, total_pwr; 3097 unsigned long max_load, avg_load, total_load, this_load, total_pwr;
@@ -3135,10 +3127,11 @@ find_busiest_group(struct sched_domain *sd, int this_cpu,
3135 unsigned long sum_avg_load_per_task; 3127 unsigned long sum_avg_load_per_task;
3136 unsigned long avg_load_per_task; 3128 unsigned long avg_load_per_task;
3137 3129
3138 local_group = cpu_isset(this_cpu, group->cpumask); 3130 local_group = cpumask_test_cpu(this_cpu,
3131 sched_group_cpus(group));
3139 3132
3140 if (local_group) 3133 if (local_group)
3141 balance_cpu = first_cpu(group->cpumask); 3134 balance_cpu = cpumask_first(sched_group_cpus(group));
3142 3135
3143 /* Tally up the load of all CPUs in the group */ 3136 /* Tally up the load of all CPUs in the group */
3144 sum_weighted_load = sum_nr_running = avg_load = 0; 3137 sum_weighted_load = sum_nr_running = avg_load = 0;
@@ -3147,13 +3140,8 @@ find_busiest_group(struct sched_domain *sd, int this_cpu,
3147 max_cpu_load = 0; 3140 max_cpu_load = 0;
3148 min_cpu_load = ~0UL; 3141 min_cpu_load = ~0UL;
3149 3142
3150 for_each_cpu_mask_nr(i, group->cpumask) { 3143 for_each_cpu_and(i, sched_group_cpus(group), cpus) {
3151 struct rq *rq; 3144 struct rq *rq = cpu_rq(i);
3152
3153 if (!cpu_isset(i, *cpus))
3154 continue;
3155
3156 rq = cpu_rq(i);
3157 3145
3158 if (*sd_idle && rq->nr_running) 3146 if (*sd_idle && rq->nr_running)
3159 *sd_idle = 0; 3147 *sd_idle = 0;
@@ -3264,8 +3252,8 @@ find_busiest_group(struct sched_domain *sd, int this_cpu,
3264 */ 3252 */
3265 if ((sum_nr_running < min_nr_running) || 3253 if ((sum_nr_running < min_nr_running) ||
3266 (sum_nr_running == min_nr_running && 3254 (sum_nr_running == min_nr_running &&
3267 first_cpu(group->cpumask) < 3255 cpumask_first(sched_group_cpus(group)) >
3268 first_cpu(group_min->cpumask))) { 3256 cpumask_first(sched_group_cpus(group_min)))) {
3269 group_min = group; 3257 group_min = group;
3270 min_nr_running = sum_nr_running; 3258 min_nr_running = sum_nr_running;
3271 min_load_per_task = sum_weighted_load / 3259 min_load_per_task = sum_weighted_load /
@@ -3280,8 +3268,8 @@ find_busiest_group(struct sched_domain *sd, int this_cpu,
3280 if (sum_nr_running <= group_capacity - 1) { 3268 if (sum_nr_running <= group_capacity - 1) {
3281 if (sum_nr_running > leader_nr_running || 3269 if (sum_nr_running > leader_nr_running ||
3282 (sum_nr_running == leader_nr_running && 3270 (sum_nr_running == leader_nr_running &&
3283 first_cpu(group->cpumask) > 3271 cpumask_first(sched_group_cpus(group)) <
3284 first_cpu(group_leader->cpumask))) { 3272 cpumask_first(sched_group_cpus(group_leader)))) {
3285 group_leader = group; 3273 group_leader = group;
3286 leader_nr_running = sum_nr_running; 3274 leader_nr_running = sum_nr_running;
3287 } 3275 }
@@ -3407,6 +3395,10 @@ out_balanced:
3407 3395
3408 if (this == group_leader && group_leader != group_min) { 3396 if (this == group_leader && group_leader != group_min) {
3409 *imbalance = min_load_per_task; 3397 *imbalance = min_load_per_task;
3398 if (sched_mc_power_savings >= POWERSAVINGS_BALANCE_WAKEUP) {
3399 cpu_rq(this_cpu)->rd->sched_mc_preferred_wakeup_cpu =
3400 cpumask_first(sched_group_cpus(group_leader));
3401 }
3410 return group_min; 3402 return group_min;
3411 } 3403 }
3412#endif 3404#endif
@@ -3420,16 +3412,16 @@ ret:
3420 */ 3412 */
3421static struct rq * 3413static struct rq *
3422find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle, 3414find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
3423 unsigned long imbalance, const cpumask_t *cpus) 3415 unsigned long imbalance, const struct cpumask *cpus)
3424{ 3416{
3425 struct rq *busiest = NULL, *rq; 3417 struct rq *busiest = NULL, *rq;
3426 unsigned long max_load = 0; 3418 unsigned long max_load = 0;
3427 int i; 3419 int i;
3428 3420
3429 for_each_cpu_mask_nr(i, group->cpumask) { 3421 for_each_cpu(i, sched_group_cpus(group)) {
3430 unsigned long wl; 3422 unsigned long wl;
3431 3423
3432 if (!cpu_isset(i, *cpus)) 3424 if (!cpumask_test_cpu(i, cpus))
3433 continue; 3425 continue;
3434 3426
3435 rq = cpu_rq(i); 3427 rq = cpu_rq(i);
@@ -3459,7 +3451,7 @@ find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
3459 */ 3451 */
3460static int load_balance(int this_cpu, struct rq *this_rq, 3452static int load_balance(int this_cpu, struct rq *this_rq,
3461 struct sched_domain *sd, enum cpu_idle_type idle, 3453 struct sched_domain *sd, enum cpu_idle_type idle,
3462 int *balance, cpumask_t *cpus) 3454 int *balance, struct cpumask *cpus)
3463{ 3455{
3464 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0; 3456 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
3465 struct sched_group *group; 3457 struct sched_group *group;
@@ -3467,7 +3459,7 @@ static int load_balance(int this_cpu, struct rq *this_rq,
3467 struct rq *busiest; 3459 struct rq *busiest;
3468 unsigned long flags; 3460 unsigned long flags;
3469 3461
3470 cpus_setall(*cpus); 3462 cpumask_setall(cpus);
3471 3463
3472 /* 3464 /*
3473 * When power savings policy is enabled for the parent domain, idle 3465 * When power savings policy is enabled for the parent domain, idle
@@ -3527,8 +3519,8 @@ redo:
3527 3519
3528 /* All tasks on this runqueue were pinned by CPU affinity */ 3520 /* All tasks on this runqueue were pinned by CPU affinity */
3529 if (unlikely(all_pinned)) { 3521 if (unlikely(all_pinned)) {
3530 cpu_clear(cpu_of(busiest), *cpus); 3522 cpumask_clear_cpu(cpu_of(busiest), cpus);
3531 if (!cpus_empty(*cpus)) 3523 if (!cpumask_empty(cpus))
3532 goto redo; 3524 goto redo;
3533 goto out_balanced; 3525 goto out_balanced;
3534 } 3526 }
@@ -3545,7 +3537,8 @@ redo:
3545 /* don't kick the migration_thread, if the curr 3537 /* don't kick the migration_thread, if the curr
3546 * task on busiest cpu can't be moved to this_cpu 3538 * task on busiest cpu can't be moved to this_cpu
3547 */ 3539 */
3548 if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) { 3540 if (!cpumask_test_cpu(this_cpu,
3541 &busiest->curr->cpus_allowed)) {
3549 spin_unlock_irqrestore(&busiest->lock, flags); 3542 spin_unlock_irqrestore(&busiest->lock, flags);
3550 all_pinned = 1; 3543 all_pinned = 1;
3551 goto out_one_pinned; 3544 goto out_one_pinned;
@@ -3620,7 +3613,7 @@ out:
3620 */ 3613 */
3621static int 3614static int
3622load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd, 3615load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd,
3623 cpumask_t *cpus) 3616 struct cpumask *cpus)
3624{ 3617{
3625 struct sched_group *group; 3618 struct sched_group *group;
3626 struct rq *busiest = NULL; 3619 struct rq *busiest = NULL;
@@ -3629,7 +3622,7 @@ load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd,
3629 int sd_idle = 0; 3622 int sd_idle = 0;
3630 int all_pinned = 0; 3623 int all_pinned = 0;
3631 3624
3632 cpus_setall(*cpus); 3625 cpumask_setall(cpus);
3633 3626
3634 /* 3627 /*
3635 * When power savings policy is enabled for the parent domain, idle 3628 * When power savings policy is enabled for the parent domain, idle
@@ -3673,17 +3666,76 @@ redo:
3673 double_unlock_balance(this_rq, busiest); 3666 double_unlock_balance(this_rq, busiest);
3674 3667
3675 if (unlikely(all_pinned)) { 3668 if (unlikely(all_pinned)) {
3676 cpu_clear(cpu_of(busiest), *cpus); 3669 cpumask_clear_cpu(cpu_of(busiest), cpus);
3677 if (!cpus_empty(*cpus)) 3670 if (!cpumask_empty(cpus))
3678 goto redo; 3671 goto redo;
3679 } 3672 }
3680 } 3673 }
3681 3674
3682 if (!ld_moved) { 3675 if (!ld_moved) {
3676 int active_balance = 0;
3677
3683 schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]); 3678 schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
3684 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER && 3679 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
3685 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) 3680 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
3686 return -1; 3681 return -1;
3682
3683 if (sched_mc_power_savings < POWERSAVINGS_BALANCE_WAKEUP)
3684 return -1;
3685
3686 if (sd->nr_balance_failed++ < 2)
3687 return -1;
3688
3689 /*
3690 * The only task running in a non-idle cpu can be moved to this
3691 * cpu in an attempt to completely freeup the other CPU
3692 * package. The same method used to move task in load_balance()
3693 * have been extended for load_balance_newidle() to speedup
3694 * consolidation at sched_mc=POWERSAVINGS_BALANCE_WAKEUP (2)
3695 *
3696 * The package power saving logic comes from
3697 * find_busiest_group(). If there are no imbalance, then
3698 * f_b_g() will return NULL. However when sched_mc={1,2} then
3699 * f_b_g() will select a group from which a running task may be
3700 * pulled to this cpu in order to make the other package idle.
3701 * If there is no opportunity to make a package idle and if
3702 * there are no imbalance, then f_b_g() will return NULL and no
3703 * action will be taken in load_balance_newidle().
3704 *
3705 * Under normal task pull operation due to imbalance, there
3706 * will be more than one task in the source run queue and
3707 * move_tasks() will succeed. ld_moved will be true and this
3708 * active balance code will not be triggered.
3709 */
3710
3711 /* Lock busiest in correct order while this_rq is held */
3712 double_lock_balance(this_rq, busiest);
3713
3714 /*
3715 * don't kick the migration_thread, if the curr
3716 * task on busiest cpu can't be moved to this_cpu
3717 */
3718 if (!cpumask_test_cpu(this_cpu, &busiest->curr->cpus_allowed)) {
3719 double_unlock_balance(this_rq, busiest);
3720 all_pinned = 1;
3721 return ld_moved;
3722 }
3723
3724 if (!busiest->active_balance) {
3725 busiest->active_balance = 1;
3726 busiest->push_cpu = this_cpu;
3727 active_balance = 1;
3728 }
3729
3730 double_unlock_balance(this_rq, busiest);
3731 /*
3732 * Should not call ttwu while holding a rq->lock
3733 */
3734 spin_unlock(&this_rq->lock);
3735 if (active_balance)
3736 wake_up_process(busiest->migration_thread);
3737 spin_lock(&this_rq->lock);
3738
3687 } else 3739 } else
3688 sd->nr_balance_failed = 0; 3740 sd->nr_balance_failed = 0;
3689 3741
@@ -3707,9 +3759,12 @@ out_balanced:
3707static void idle_balance(int this_cpu, struct rq *this_rq) 3759static void idle_balance(int this_cpu, struct rq *this_rq)
3708{ 3760{
3709 struct sched_domain *sd; 3761 struct sched_domain *sd;
3710 int pulled_task = -1; 3762 int pulled_task = 0;
3711 unsigned long next_balance = jiffies + HZ; 3763 unsigned long next_balance = jiffies + HZ;
3712 cpumask_t tmpmask; 3764 cpumask_var_t tmpmask;
3765
3766 if (!alloc_cpumask_var(&tmpmask, GFP_ATOMIC))
3767 return;
3713 3768
3714 for_each_domain(this_cpu, sd) { 3769 for_each_domain(this_cpu, sd) {
3715 unsigned long interval; 3770 unsigned long interval;
@@ -3720,7 +3775,7 @@ static void idle_balance(int this_cpu, struct rq *this_rq)
3720 if (sd->flags & SD_BALANCE_NEWIDLE) 3775 if (sd->flags & SD_BALANCE_NEWIDLE)
3721 /* If we've pulled tasks over stop searching: */ 3776 /* If we've pulled tasks over stop searching: */
3722 pulled_task = load_balance_newidle(this_cpu, this_rq, 3777 pulled_task = load_balance_newidle(this_cpu, this_rq,
3723 sd, &tmpmask); 3778 sd, tmpmask);
3724 3779
3725 interval = msecs_to_jiffies(sd->balance_interval); 3780 interval = msecs_to_jiffies(sd->balance_interval);
3726 if (time_after(next_balance, sd->last_balance + interval)) 3781 if (time_after(next_balance, sd->last_balance + interval))
@@ -3735,6 +3790,7 @@ static void idle_balance(int this_cpu, struct rq *this_rq)
3735 */ 3790 */
3736 this_rq->next_balance = next_balance; 3791 this_rq->next_balance = next_balance;
3737 } 3792 }
3793 free_cpumask_var(tmpmask);
3738} 3794}
3739 3795
3740/* 3796/*
@@ -3772,7 +3828,7 @@ static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
3772 /* Search for an sd spanning us and the target CPU. */ 3828 /* Search for an sd spanning us and the target CPU. */
3773 for_each_domain(target_cpu, sd) { 3829 for_each_domain(target_cpu, sd) {
3774 if ((sd->flags & SD_LOAD_BALANCE) && 3830 if ((sd->flags & SD_LOAD_BALANCE) &&
3775 cpu_isset(busiest_cpu, sd->span)) 3831 cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
3776 break; 3832 break;
3777 } 3833 }
3778 3834
@@ -3791,10 +3847,9 @@ static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
3791#ifdef CONFIG_NO_HZ 3847#ifdef CONFIG_NO_HZ
3792static struct { 3848static struct {
3793 atomic_t load_balancer; 3849 atomic_t load_balancer;
3794 cpumask_t cpu_mask; 3850 cpumask_var_t cpu_mask;
3795} nohz ____cacheline_aligned = { 3851} nohz ____cacheline_aligned = {
3796 .load_balancer = ATOMIC_INIT(-1), 3852 .load_balancer = ATOMIC_INIT(-1),
3797 .cpu_mask = CPU_MASK_NONE,
3798}; 3853};
3799 3854
3800/* 3855/*
@@ -3822,7 +3877,7 @@ int select_nohz_load_balancer(int stop_tick)
3822 int cpu = smp_processor_id(); 3877 int cpu = smp_processor_id();
3823 3878
3824 if (stop_tick) { 3879 if (stop_tick) {
3825 cpu_set(cpu, nohz.cpu_mask); 3880 cpumask_set_cpu(cpu, nohz.cpu_mask);
3826 cpu_rq(cpu)->in_nohz_recently = 1; 3881 cpu_rq(cpu)->in_nohz_recently = 1;
3827 3882
3828 /* 3883 /*
@@ -3836,7 +3891,7 @@ int select_nohz_load_balancer(int stop_tick)
3836 } 3891 }
3837 3892
3838 /* time for ilb owner also to sleep */ 3893 /* time for ilb owner also to sleep */
3839 if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) { 3894 if (cpumask_weight(nohz.cpu_mask) == num_online_cpus()) {
3840 if (atomic_read(&nohz.load_balancer) == cpu) 3895 if (atomic_read(&nohz.load_balancer) == cpu)
3841 atomic_set(&nohz.load_balancer, -1); 3896 atomic_set(&nohz.load_balancer, -1);
3842 return 0; 3897 return 0;
@@ -3849,10 +3904,10 @@ int select_nohz_load_balancer(int stop_tick)
3849 } else if (atomic_read(&nohz.load_balancer) == cpu) 3904 } else if (atomic_read(&nohz.load_balancer) == cpu)
3850 return 1; 3905 return 1;
3851 } else { 3906 } else {
3852 if (!cpu_isset(cpu, nohz.cpu_mask)) 3907 if (!cpumask_test_cpu(cpu, nohz.cpu_mask))
3853 return 0; 3908 return 0;
3854 3909
3855 cpu_clear(cpu, nohz.cpu_mask); 3910 cpumask_clear_cpu(cpu, nohz.cpu_mask);
3856 3911
3857 if (atomic_read(&nohz.load_balancer) == cpu) 3912 if (atomic_read(&nohz.load_balancer) == cpu)
3858 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu) 3913 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
@@ -3880,7 +3935,11 @@ static void rebalance_domains(int cpu, enum cpu_idle_type idle)
3880 unsigned long next_balance = jiffies + 60*HZ; 3935 unsigned long next_balance = jiffies + 60*HZ;
3881 int update_next_balance = 0; 3936 int update_next_balance = 0;
3882 int need_serialize; 3937 int need_serialize;
3883 cpumask_t tmp; 3938 cpumask_var_t tmp;
3939
3940 /* Fails alloc? Rebalancing probably not a priority right now. */
3941 if (!alloc_cpumask_var(&tmp, GFP_ATOMIC))
3942 return;
3884 3943
3885 for_each_domain(cpu, sd) { 3944 for_each_domain(cpu, sd) {
3886 if (!(sd->flags & SD_LOAD_BALANCE)) 3945 if (!(sd->flags & SD_LOAD_BALANCE))
@@ -3905,7 +3964,7 @@ static void rebalance_domains(int cpu, enum cpu_idle_type idle)
3905 } 3964 }
3906 3965
3907 if (time_after_eq(jiffies, sd->last_balance + interval)) { 3966 if (time_after_eq(jiffies, sd->last_balance + interval)) {
3908 if (load_balance(cpu, rq, sd, idle, &balance, &tmp)) { 3967 if (load_balance(cpu, rq, sd, idle, &balance, tmp)) {
3909 /* 3968 /*
3910 * We've pulled tasks over so either we're no 3969 * We've pulled tasks over so either we're no
3911 * longer idle, or one of our SMT siblings is 3970 * longer idle, or one of our SMT siblings is
@@ -3939,6 +3998,8 @@ out:
3939 */ 3998 */
3940 if (likely(update_next_balance)) 3999 if (likely(update_next_balance))
3941 rq->next_balance = next_balance; 4000 rq->next_balance = next_balance;
4001
4002 free_cpumask_var(tmp);
3942} 4003}
3943 4004
3944/* 4005/*
@@ -3963,12 +4024,13 @@ static void run_rebalance_domains(struct softirq_action *h)
3963 */ 4024 */
3964 if (this_rq->idle_at_tick && 4025 if (this_rq->idle_at_tick &&
3965 atomic_read(&nohz.load_balancer) == this_cpu) { 4026 atomic_read(&nohz.load_balancer) == this_cpu) {
3966 cpumask_t cpus = nohz.cpu_mask;
3967 struct rq *rq; 4027 struct rq *rq;
3968 int balance_cpu; 4028 int balance_cpu;
3969 4029
3970 cpu_clear(this_cpu, cpus); 4030 for_each_cpu(balance_cpu, nohz.cpu_mask) {
3971 for_each_cpu_mask_nr(balance_cpu, cpus) { 4031 if (balance_cpu == this_cpu)
4032 continue;
4033
3972 /* 4034 /*
3973 * If this cpu gets work to do, stop the load balancing 4035 * If this cpu gets work to do, stop the load balancing
3974 * work being done for other cpus. Next load 4036 * work being done for other cpus. Next load
@@ -4006,7 +4068,7 @@ static inline void trigger_load_balance(struct rq *rq, int cpu)
4006 rq->in_nohz_recently = 0; 4068 rq->in_nohz_recently = 0;
4007 4069
4008 if (atomic_read(&nohz.load_balancer) == cpu) { 4070 if (atomic_read(&nohz.load_balancer) == cpu) {
4009 cpu_clear(cpu, nohz.cpu_mask); 4071 cpumask_clear_cpu(cpu, nohz.cpu_mask);
4010 atomic_set(&nohz.load_balancer, -1); 4072 atomic_set(&nohz.load_balancer, -1);
4011 } 4073 }
4012 4074
@@ -4019,7 +4081,7 @@ static inline void trigger_load_balance(struct rq *rq, int cpu)
4019 * TBD: Traverse the sched domains and nominate 4081 * TBD: Traverse the sched domains and nominate
4020 * the nearest cpu in the nohz.cpu_mask. 4082 * the nearest cpu in the nohz.cpu_mask.
4021 */ 4083 */
4022 int ilb = first_cpu(nohz.cpu_mask); 4084 int ilb = cpumask_first(nohz.cpu_mask);
4023 4085
4024 if (ilb < nr_cpu_ids) 4086 if (ilb < nr_cpu_ids)
4025 resched_cpu(ilb); 4087 resched_cpu(ilb);
@@ -4031,7 +4093,7 @@ static inline void trigger_load_balance(struct rq *rq, int cpu)
4031 * cpus with ticks stopped, is it time for that to stop? 4093 * cpus with ticks stopped, is it time for that to stop?
4032 */ 4094 */
4033 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu && 4095 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
4034 cpus_weight(nohz.cpu_mask) == num_online_cpus()) { 4096 cpumask_weight(nohz.cpu_mask) == num_online_cpus()) {
4035 resched_cpu(cpu); 4097 resched_cpu(cpu);
4036 return; 4098 return;
4037 } 4099 }
@@ -4041,7 +4103,7 @@ static inline void trigger_load_balance(struct rq *rq, int cpu)
4041 * someone else, then no need raise the SCHED_SOFTIRQ 4103 * someone else, then no need raise the SCHED_SOFTIRQ
4042 */ 4104 */
4043 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu && 4105 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
4044 cpu_isset(cpu, nohz.cpu_mask)) 4106 cpumask_test_cpu(cpu, nohz.cpu_mask))
4045 return; 4107 return;
4046#endif 4108#endif
4047 if (time_after_eq(jiffies, rq->next_balance)) 4109 if (time_after_eq(jiffies, rq->next_balance))
@@ -4093,13 +4155,17 @@ unsigned long long task_delta_exec(struct task_struct *p)
4093 * Account user cpu time to a process. 4155 * Account user cpu time to a process.
4094 * @p: the process that the cpu time gets accounted to 4156 * @p: the process that the cpu time gets accounted to
4095 * @cputime: the cpu time spent in user space since the last update 4157 * @cputime: the cpu time spent in user space since the last update
4158 * @cputime_scaled: cputime scaled by cpu frequency
4096 */ 4159 */
4097void account_user_time(struct task_struct *p, cputime_t cputime) 4160void account_user_time(struct task_struct *p, cputime_t cputime,
4161 cputime_t cputime_scaled)
4098{ 4162{
4099 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; 4163 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4100 cputime64_t tmp; 4164 cputime64_t tmp;
4101 4165
4166 /* Add user time to process. */
4102 p->utime = cputime_add(p->utime, cputime); 4167 p->utime = cputime_add(p->utime, cputime);
4168 p->utimescaled = cputime_add(p->utimescaled, cputime_scaled);
4103 account_group_user_time(p, cputime); 4169 account_group_user_time(p, cputime);
4104 4170
4105 /* Add user time to cpustat. */ 4171 /* Add user time to cpustat. */
@@ -4116,51 +4182,48 @@ void account_user_time(struct task_struct *p, cputime_t cputime)
4116 * Account guest cpu time to a process. 4182 * Account guest cpu time to a process.
4117 * @p: the process that the cpu time gets accounted to 4183 * @p: the process that the cpu time gets accounted to
4118 * @cputime: the cpu time spent in virtual machine since the last update 4184 * @cputime: the cpu time spent in virtual machine since the last update
4185 * @cputime_scaled: cputime scaled by cpu frequency
4119 */ 4186 */
4120static void account_guest_time(struct task_struct *p, cputime_t cputime) 4187static void account_guest_time(struct task_struct *p, cputime_t cputime,
4188 cputime_t cputime_scaled)
4121{ 4189{
4122 cputime64_t tmp; 4190 cputime64_t tmp;
4123 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; 4191 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4124 4192
4125 tmp = cputime_to_cputime64(cputime); 4193 tmp = cputime_to_cputime64(cputime);
4126 4194
4195 /* Add guest time to process. */
4127 p->utime = cputime_add(p->utime, cputime); 4196 p->utime = cputime_add(p->utime, cputime);
4197 p->utimescaled = cputime_add(p->utimescaled, cputime_scaled);
4128 account_group_user_time(p, cputime); 4198 account_group_user_time(p, cputime);
4129 p->gtime = cputime_add(p->gtime, cputime); 4199 p->gtime = cputime_add(p->gtime, cputime);
4130 4200
4201 /* Add guest time to cpustat. */
4131 cpustat->user = cputime64_add(cpustat->user, tmp); 4202 cpustat->user = cputime64_add(cpustat->user, tmp);
4132 cpustat->guest = cputime64_add(cpustat->guest, tmp); 4203 cpustat->guest = cputime64_add(cpustat->guest, tmp);
4133} 4204}
4134 4205
4135/* 4206/*
4136 * Account scaled user cpu time to a process.
4137 * @p: the process that the cpu time gets accounted to
4138 * @cputime: the cpu time spent in user space since the last update
4139 */
4140void account_user_time_scaled(struct task_struct *p, cputime_t cputime)
4141{
4142 p->utimescaled = cputime_add(p->utimescaled, cputime);
4143}
4144
4145/*
4146 * Account system cpu time to a process. 4207 * Account system cpu time to a process.
4147 * @p: the process that the cpu time gets accounted to 4208 * @p: the process that the cpu time gets accounted to
4148 * @hardirq_offset: the offset to subtract from hardirq_count() 4209 * @hardirq_offset: the offset to subtract from hardirq_count()
4149 * @cputime: the cpu time spent in kernel space since the last update 4210 * @cputime: the cpu time spent in kernel space since the last update
4211 * @cputime_scaled: cputime scaled by cpu frequency
4150 */ 4212 */
4151void account_system_time(struct task_struct *p, int hardirq_offset, 4213void account_system_time(struct task_struct *p, int hardirq_offset,
4152 cputime_t cputime) 4214 cputime_t cputime, cputime_t cputime_scaled)
4153{ 4215{
4154 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; 4216 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4155 struct rq *rq = this_rq();
4156 cputime64_t tmp; 4217 cputime64_t tmp;
4157 4218
4158 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) { 4219 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
4159 account_guest_time(p, cputime); 4220 account_guest_time(p, cputime, cputime_scaled);
4160 return; 4221 return;
4161 } 4222 }
4162 4223
4224 /* Add system time to process. */
4163 p->stime = cputime_add(p->stime, cputime); 4225 p->stime = cputime_add(p->stime, cputime);
4226 p->stimescaled = cputime_add(p->stimescaled, cputime_scaled);
4164 account_group_system_time(p, cputime); 4227 account_group_system_time(p, cputime);
4165 4228
4166 /* Add system time to cpustat. */ 4229 /* Add system time to cpustat. */
@@ -4169,50 +4232,85 @@ void account_system_time(struct task_struct *p, int hardirq_offset,
4169 cpustat->irq = cputime64_add(cpustat->irq, tmp); 4232 cpustat->irq = cputime64_add(cpustat->irq, tmp);
4170 else if (softirq_count()) 4233 else if (softirq_count())
4171 cpustat->softirq = cputime64_add(cpustat->softirq, tmp); 4234 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
4172 else if (p != rq->idle)
4173 cpustat->system = cputime64_add(cpustat->system, tmp);
4174 else if (atomic_read(&rq->nr_iowait) > 0)
4175 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
4176 else 4235 else
4177 cpustat->idle = cputime64_add(cpustat->idle, tmp); 4236 cpustat->system = cputime64_add(cpustat->system, tmp);
4237
4178 /* Account for system time used */ 4238 /* Account for system time used */
4179 acct_update_integrals(p); 4239 acct_update_integrals(p);
4180} 4240}
4181 4241
4182/* 4242/*
4183 * Account scaled system cpu time to a process. 4243 * Account for involuntary wait time.
4184 * @p: the process that the cpu time gets accounted to 4244 * @steal: the cpu time spent in involuntary wait
4185 * @hardirq_offset: the offset to subtract from hardirq_count()
4186 * @cputime: the cpu time spent in kernel space since the last update
4187 */ 4245 */
4188void account_system_time_scaled(struct task_struct *p, cputime_t cputime) 4246void account_steal_time(cputime_t cputime)
4189{ 4247{
4190 p->stimescaled = cputime_add(p->stimescaled, cputime); 4248 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4249 cputime64_t cputime64 = cputime_to_cputime64(cputime);
4250
4251 cpustat->steal = cputime64_add(cpustat->steal, cputime64);
4191} 4252}
4192 4253
4193/* 4254/*
4194 * Account for involuntary wait time. 4255 * Account for idle time.
4195 * @p: the process from which the cpu time has been stolen 4256 * @cputime: the cpu time spent in idle wait
4196 * @steal: the cpu time spent in involuntary wait
4197 */ 4257 */
4198void account_steal_time(struct task_struct *p, cputime_t steal) 4258void account_idle_time(cputime_t cputime)
4199{ 4259{
4200 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; 4260 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4201 cputime64_t tmp = cputime_to_cputime64(steal); 4261 cputime64_t cputime64 = cputime_to_cputime64(cputime);
4202 struct rq *rq = this_rq(); 4262 struct rq *rq = this_rq();
4203 4263
4204 if (p == rq->idle) { 4264 if (atomic_read(&rq->nr_iowait) > 0)
4205 p->stime = cputime_add(p->stime, steal); 4265 cpustat->iowait = cputime64_add(cpustat->iowait, cputime64);
4206 account_group_system_time(p, steal); 4266 else
4207 if (atomic_read(&rq->nr_iowait) > 0) 4267 cpustat->idle = cputime64_add(cpustat->idle, cputime64);
4208 cpustat->iowait = cputime64_add(cpustat->iowait, tmp); 4268}
4209 else 4269
4210 cpustat->idle = cputime64_add(cpustat->idle, tmp); 4270#ifndef CONFIG_VIRT_CPU_ACCOUNTING
4211 } else 4271
4212 cpustat->steal = cputime64_add(cpustat->steal, tmp); 4272/*
4273 * Account a single tick of cpu time.
4274 * @p: the process that the cpu time gets accounted to
4275 * @user_tick: indicates if the tick is a user or a system tick
4276 */
4277void account_process_tick(struct task_struct *p, int user_tick)
4278{
4279 cputime_t one_jiffy = jiffies_to_cputime(1);
4280 cputime_t one_jiffy_scaled = cputime_to_scaled(one_jiffy);
4281 struct rq *rq = this_rq();
4282
4283 if (user_tick)
4284 account_user_time(p, one_jiffy, one_jiffy_scaled);
4285 else if (p != rq->idle)
4286 account_system_time(p, HARDIRQ_OFFSET, one_jiffy,
4287 one_jiffy_scaled);
4288 else
4289 account_idle_time(one_jiffy);
4290}
4291
4292/*
4293 * Account multiple ticks of steal time.
4294 * @p: the process from which the cpu time has been stolen
4295 * @ticks: number of stolen ticks
4296 */
4297void account_steal_ticks(unsigned long ticks)
4298{
4299 account_steal_time(jiffies_to_cputime(ticks));
4213} 4300}
4214 4301
4215/* 4302/*
4303 * Account multiple ticks of idle time.
4304 * @ticks: number of stolen ticks
4305 */
4306void account_idle_ticks(unsigned long ticks)
4307{
4308 account_idle_time(jiffies_to_cputime(ticks));
4309}
4310
4311#endif
4312
4313/*
4216 * Use precise platform statistics if available: 4314 * Use precise platform statistics if available:
4217 */ 4315 */
4218#ifdef CONFIG_VIRT_CPU_ACCOUNTING 4316#ifdef CONFIG_VIRT_CPU_ACCOUNTING
@@ -4339,7 +4437,7 @@ void __kprobes sub_preempt_count(int val)
4339 /* 4437 /*
4340 * Underflow? 4438 * Underflow?
4341 */ 4439 */
4342 if (DEBUG_LOCKS_WARN_ON(val > preempt_count())) 4440 if (DEBUG_LOCKS_WARN_ON(val > preempt_count() - (!!kernel_locked())))
4343 return; 4441 return;
4344 /* 4442 /*
4345 * Is the spinlock portion underflowing? 4443 * Is the spinlock portion underflowing?
@@ -5134,6 +5232,22 @@ __setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
5134 set_load_weight(p); 5232 set_load_weight(p);
5135} 5233}
5136 5234
5235/*
5236 * check the target process has a UID that matches the current process's
5237 */
5238static bool check_same_owner(struct task_struct *p)
5239{
5240 const struct cred *cred = current_cred(), *pcred;
5241 bool match;
5242
5243 rcu_read_lock();
5244 pcred = __task_cred(p);
5245 match = (cred->euid == pcred->euid ||
5246 cred->euid == pcred->uid);
5247 rcu_read_unlock();
5248 return match;
5249}
5250
5137static int __sched_setscheduler(struct task_struct *p, int policy, 5251static int __sched_setscheduler(struct task_struct *p, int policy,
5138 struct sched_param *param, bool user) 5252 struct sched_param *param, bool user)
5139{ 5253{
@@ -5193,8 +5307,7 @@ recheck:
5193 return -EPERM; 5307 return -EPERM;
5194 5308
5195 /* can't change other user's priorities */ 5309 /* can't change other user's priorities */
5196 if ((current->euid != p->euid) && 5310 if (!check_same_owner(p))
5197 (current->euid != p->uid))
5198 return -EPERM; 5311 return -EPERM;
5199 } 5312 }
5200 5313
@@ -5400,10 +5513,9 @@ out_unlock:
5400 return retval; 5513 return retval;
5401} 5514}
5402 5515
5403long sched_setaffinity(pid_t pid, const cpumask_t *in_mask) 5516long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
5404{ 5517{
5405 cpumask_t cpus_allowed; 5518 cpumask_var_t cpus_allowed, new_mask;
5406 cpumask_t new_mask = *in_mask;
5407 struct task_struct *p; 5519 struct task_struct *p;
5408 int retval; 5520 int retval;
5409 5521
@@ -5425,46 +5537,57 @@ long sched_setaffinity(pid_t pid, const cpumask_t *in_mask)
5425 get_task_struct(p); 5537 get_task_struct(p);
5426 read_unlock(&tasklist_lock); 5538 read_unlock(&tasklist_lock);
5427 5539
5540 if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
5541 retval = -ENOMEM;
5542 goto out_put_task;
5543 }
5544 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
5545 retval = -ENOMEM;
5546 goto out_free_cpus_allowed;
5547 }
5428 retval = -EPERM; 5548 retval = -EPERM;
5429 if ((current->euid != p->euid) && (current->euid != p->uid) && 5549 if (!check_same_owner(p) && !capable(CAP_SYS_NICE))
5430 !capable(CAP_SYS_NICE))
5431 goto out_unlock; 5550 goto out_unlock;
5432 5551
5433 retval = security_task_setscheduler(p, 0, NULL); 5552 retval = security_task_setscheduler(p, 0, NULL);
5434 if (retval) 5553 if (retval)
5435 goto out_unlock; 5554 goto out_unlock;
5436 5555
5437 cpuset_cpus_allowed(p, &cpus_allowed); 5556 cpuset_cpus_allowed(p, cpus_allowed);
5438 cpus_and(new_mask, new_mask, cpus_allowed); 5557 cpumask_and(new_mask, in_mask, cpus_allowed);
5439 again: 5558 again:
5440 retval = set_cpus_allowed_ptr(p, &new_mask); 5559 retval = set_cpus_allowed_ptr(p, new_mask);
5441 5560
5442 if (!retval) { 5561 if (!retval) {
5443 cpuset_cpus_allowed(p, &cpus_allowed); 5562 cpuset_cpus_allowed(p, cpus_allowed);
5444 if (!cpus_subset(new_mask, cpus_allowed)) { 5563 if (!cpumask_subset(new_mask, cpus_allowed)) {
5445 /* 5564 /*
5446 * We must have raced with a concurrent cpuset 5565 * We must have raced with a concurrent cpuset
5447 * update. Just reset the cpus_allowed to the 5566 * update. Just reset the cpus_allowed to the
5448 * cpuset's cpus_allowed 5567 * cpuset's cpus_allowed
5449 */ 5568 */
5450 new_mask = cpus_allowed; 5569 cpumask_copy(new_mask, cpus_allowed);
5451 goto again; 5570 goto again;
5452 } 5571 }
5453 } 5572 }
5454out_unlock: 5573out_unlock:
5574 free_cpumask_var(new_mask);
5575out_free_cpus_allowed:
5576 free_cpumask_var(cpus_allowed);
5577out_put_task:
5455 put_task_struct(p); 5578 put_task_struct(p);
5456 put_online_cpus(); 5579 put_online_cpus();
5457 return retval; 5580 return retval;
5458} 5581}
5459 5582
5460static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len, 5583static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
5461 cpumask_t *new_mask) 5584 struct cpumask *new_mask)
5462{ 5585{
5463 if (len < sizeof(cpumask_t)) { 5586 if (len < cpumask_size())
5464 memset(new_mask, 0, sizeof(cpumask_t)); 5587 cpumask_clear(new_mask);
5465 } else if (len > sizeof(cpumask_t)) { 5588 else if (len > cpumask_size())
5466 len = sizeof(cpumask_t); 5589 len = cpumask_size();
5467 } 5590
5468 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0; 5591 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
5469} 5592}
5470 5593
@@ -5477,17 +5600,20 @@ static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
5477asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len, 5600asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
5478 unsigned long __user *user_mask_ptr) 5601 unsigned long __user *user_mask_ptr)
5479{ 5602{
5480 cpumask_t new_mask; 5603 cpumask_var_t new_mask;
5481 int retval; 5604 int retval;
5482 5605
5483 retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask); 5606 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
5484 if (retval) 5607 return -ENOMEM;
5485 return retval;
5486 5608
5487 return sched_setaffinity(pid, &new_mask); 5609 retval = get_user_cpu_mask(user_mask_ptr, len, new_mask);
5610 if (retval == 0)
5611 retval = sched_setaffinity(pid, new_mask);
5612 free_cpumask_var(new_mask);
5613 return retval;
5488} 5614}
5489 5615
5490long sched_getaffinity(pid_t pid, cpumask_t *mask) 5616long sched_getaffinity(pid_t pid, struct cpumask *mask)
5491{ 5617{
5492 struct task_struct *p; 5618 struct task_struct *p;
5493 int retval; 5619 int retval;
@@ -5504,7 +5630,7 @@ long sched_getaffinity(pid_t pid, cpumask_t *mask)
5504 if (retval) 5630 if (retval)
5505 goto out_unlock; 5631 goto out_unlock;
5506 5632
5507 cpus_and(*mask, p->cpus_allowed, cpu_online_map); 5633 cpumask_and(mask, &p->cpus_allowed, cpu_online_mask);
5508 5634
5509out_unlock: 5635out_unlock:
5510 read_unlock(&tasklist_lock); 5636 read_unlock(&tasklist_lock);
@@ -5523,19 +5649,24 @@ asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
5523 unsigned long __user *user_mask_ptr) 5649 unsigned long __user *user_mask_ptr)
5524{ 5650{
5525 int ret; 5651 int ret;
5526 cpumask_t mask; 5652 cpumask_var_t mask;
5527 5653
5528 if (len < sizeof(cpumask_t)) 5654 if (len < cpumask_size())
5529 return -EINVAL; 5655 return -EINVAL;
5530 5656
5531 ret = sched_getaffinity(pid, &mask); 5657 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
5532 if (ret < 0) 5658 return -ENOMEM;
5533 return ret;
5534 5659
5535 if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t))) 5660 ret = sched_getaffinity(pid, mask);
5536 return -EFAULT; 5661 if (ret == 0) {
5662 if (copy_to_user(user_mask_ptr, mask, cpumask_size()))
5663 ret = -EFAULT;
5664 else
5665 ret = cpumask_size();
5666 }
5667 free_cpumask_var(mask);
5537 5668
5538 return sizeof(cpumask_t); 5669 return ret;
5539} 5670}
5540 5671
5541/** 5672/**
@@ -5877,7 +6008,7 @@ void __cpuinit init_idle(struct task_struct *idle, int cpu)
5877 idle->se.exec_start = sched_clock(); 6008 idle->se.exec_start = sched_clock();
5878 6009
5879 idle->prio = idle->normal_prio = MAX_PRIO; 6010 idle->prio = idle->normal_prio = MAX_PRIO;
5880 idle->cpus_allowed = cpumask_of_cpu(cpu); 6011 cpumask_copy(&idle->cpus_allowed, cpumask_of(cpu));
5881 __set_task_cpu(idle, cpu); 6012 __set_task_cpu(idle, cpu);
5882 6013
5883 rq->curr = rq->idle = idle; 6014 rq->curr = rq->idle = idle;
@@ -5896,6 +6027,7 @@ void __cpuinit init_idle(struct task_struct *idle, int cpu)
5896 * The idle tasks have their own, simple scheduling class: 6027 * The idle tasks have their own, simple scheduling class:
5897 */ 6028 */
5898 idle->sched_class = &idle_sched_class; 6029 idle->sched_class = &idle_sched_class;
6030 ftrace_graph_init_task(idle);
5899} 6031}
5900 6032
5901/* 6033/*
@@ -5903,9 +6035,9 @@ void __cpuinit init_idle(struct task_struct *idle, int cpu)
5903 * indicates which cpus entered this state. This is used 6035 * indicates which cpus entered this state. This is used
5904 * in the rcu update to wait only for active cpus. For system 6036 * in the rcu update to wait only for active cpus. For system
5905 * which do not switch off the HZ timer nohz_cpu_mask should 6037 * which do not switch off the HZ timer nohz_cpu_mask should
5906 * always be CPU_MASK_NONE. 6038 * always be CPU_BITS_NONE.
5907 */ 6039 */
5908cpumask_t nohz_cpu_mask = CPU_MASK_NONE; 6040cpumask_var_t nohz_cpu_mask;
5909 6041
5910/* 6042/*
5911 * Increase the granularity value when there are more CPUs, 6043 * Increase the granularity value when there are more CPUs,
@@ -5960,7 +6092,7 @@ static inline void sched_init_granularity(void)
5960 * task must not exit() & deallocate itself prematurely. The 6092 * task must not exit() & deallocate itself prematurely. The
5961 * call is not atomic; no spinlocks may be held. 6093 * call is not atomic; no spinlocks may be held.
5962 */ 6094 */
5963int set_cpus_allowed_ptr(struct task_struct *p, const cpumask_t *new_mask) 6095int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
5964{ 6096{
5965 struct migration_req req; 6097 struct migration_req req;
5966 unsigned long flags; 6098 unsigned long flags;
@@ -5968,13 +6100,13 @@ int set_cpus_allowed_ptr(struct task_struct *p, const cpumask_t *new_mask)
5968 int ret = 0; 6100 int ret = 0;
5969 6101
5970 rq = task_rq_lock(p, &flags); 6102 rq = task_rq_lock(p, &flags);
5971 if (!cpus_intersects(*new_mask, cpu_online_map)) { 6103 if (!cpumask_intersects(new_mask, cpu_online_mask)) {
5972 ret = -EINVAL; 6104 ret = -EINVAL;
5973 goto out; 6105 goto out;
5974 } 6106 }
5975 6107
5976 if (unlikely((p->flags & PF_THREAD_BOUND) && p != current && 6108 if (unlikely((p->flags & PF_THREAD_BOUND) && p != current &&
5977 !cpus_equal(p->cpus_allowed, *new_mask))) { 6109 !cpumask_equal(&p->cpus_allowed, new_mask))) {
5978 ret = -EINVAL; 6110 ret = -EINVAL;
5979 goto out; 6111 goto out;
5980 } 6112 }
@@ -5982,15 +6114,15 @@ int set_cpus_allowed_ptr(struct task_struct *p, const cpumask_t *new_mask)
5982 if (p->sched_class->set_cpus_allowed) 6114 if (p->sched_class->set_cpus_allowed)
5983 p->sched_class->set_cpus_allowed(p, new_mask); 6115 p->sched_class->set_cpus_allowed(p, new_mask);
5984 else { 6116 else {
5985 p->cpus_allowed = *new_mask; 6117 cpumask_copy(&p->cpus_allowed, new_mask);
5986 p->rt.nr_cpus_allowed = cpus_weight(*new_mask); 6118 p->rt.nr_cpus_allowed = cpumask_weight(new_mask);
5987 } 6119 }
5988 6120
5989 /* Can the task run on the task's current CPU? If so, we're done */ 6121 /* Can the task run on the task's current CPU? If so, we're done */
5990 if (cpu_isset(task_cpu(p), *new_mask)) 6122 if (cpumask_test_cpu(task_cpu(p), new_mask))
5991 goto out; 6123 goto out;
5992 6124
5993 if (migrate_task(p, any_online_cpu(*new_mask), &req)) { 6125 if (migrate_task(p, cpumask_any_and(cpu_online_mask, new_mask), &req)) {
5994 /* Need help from migration thread: drop lock and wait. */ 6126 /* Need help from migration thread: drop lock and wait. */
5995 task_rq_unlock(rq, &flags); 6127 task_rq_unlock(rq, &flags);
5996 wake_up_process(rq->migration_thread); 6128 wake_up_process(rq->migration_thread);
@@ -6032,7 +6164,7 @@ static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
6032 if (task_cpu(p) != src_cpu) 6164 if (task_cpu(p) != src_cpu)
6033 goto done; 6165 goto done;
6034 /* Affinity changed (again). */ 6166 /* Affinity changed (again). */
6035 if (!cpu_isset(dest_cpu, p->cpus_allowed)) 6167 if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed))
6036 goto fail; 6168 goto fail;
6037 6169
6038 on_rq = p->se.on_rq; 6170 on_rq = p->se.on_rq;
@@ -6126,54 +6258,44 @@ static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
6126 6258
6127/* 6259/*
6128 * Figure out where task on dead CPU should go, use force if necessary. 6260 * Figure out where task on dead CPU should go, use force if necessary.
6129 * NOTE: interrupts should be disabled by the caller
6130 */ 6261 */
6131static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p) 6262static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
6132{ 6263{
6133 unsigned long flags;
6134 cpumask_t mask;
6135 struct rq *rq;
6136 int dest_cpu; 6264 int dest_cpu;
6265 const struct cpumask *nodemask = cpumask_of_node(cpu_to_node(dead_cpu));
6137 6266
6138 do { 6267again:
6139 /* On same node? */ 6268 /* Look for allowed, online CPU in same node. */
6140 mask = node_to_cpumask(cpu_to_node(dead_cpu)); 6269 for_each_cpu_and(dest_cpu, nodemask, cpu_online_mask)
6141 cpus_and(mask, mask, p->cpus_allowed); 6270 if (cpumask_test_cpu(dest_cpu, &p->cpus_allowed))
6142 dest_cpu = any_online_cpu(mask); 6271 goto move;
6143 6272
6144 /* On any allowed CPU? */ 6273 /* Any allowed, online CPU? */
6145 if (dest_cpu >= nr_cpu_ids) 6274 dest_cpu = cpumask_any_and(&p->cpus_allowed, cpu_online_mask);
6146 dest_cpu = any_online_cpu(p->cpus_allowed); 6275 if (dest_cpu < nr_cpu_ids)
6276 goto move;
6147 6277
6148 /* No more Mr. Nice Guy. */ 6278 /* No more Mr. Nice Guy. */
6149 if (dest_cpu >= nr_cpu_ids) { 6279 if (dest_cpu >= nr_cpu_ids) {
6150 cpumask_t cpus_allowed; 6280 cpuset_cpus_allowed_locked(p, &p->cpus_allowed);
6281 dest_cpu = cpumask_any_and(cpu_online_mask, &p->cpus_allowed);
6151 6282
6152 cpuset_cpus_allowed_locked(p, &cpus_allowed); 6283 /*
6153 /* 6284 * Don't tell them about moving exiting tasks or
6154 * Try to stay on the same cpuset, where the 6285 * kernel threads (both mm NULL), since they never
6155 * current cpuset may be a subset of all cpus. 6286 * leave kernel.
6156 * The cpuset_cpus_allowed_locked() variant of 6287 */
6157 * cpuset_cpus_allowed() will not block. It must be 6288 if (p->mm && printk_ratelimit()) {
6158 * called within calls to cpuset_lock/cpuset_unlock. 6289 printk(KERN_INFO "process %d (%s) no "
6159 */ 6290 "longer affine to cpu%d\n",
6160 rq = task_rq_lock(p, &flags); 6291 task_pid_nr(p), p->comm, dead_cpu);
6161 p->cpus_allowed = cpus_allowed;
6162 dest_cpu = any_online_cpu(p->cpus_allowed);
6163 task_rq_unlock(rq, &flags);
6164
6165 /*
6166 * Don't tell them about moving exiting tasks or
6167 * kernel threads (both mm NULL), since they never
6168 * leave kernel.
6169 */
6170 if (p->mm && printk_ratelimit()) {
6171 printk(KERN_INFO "process %d (%s) no "
6172 "longer affine to cpu%d\n",
6173 task_pid_nr(p), p->comm, dead_cpu);
6174 }
6175 } 6292 }
6176 } while (!__migrate_task_irq(p, dead_cpu, dest_cpu)); 6293 }
6294
6295move:
6296 /* It can have affinity changed while we were choosing. */
6297 if (unlikely(!__migrate_task_irq(p, dead_cpu, dest_cpu)))
6298 goto again;
6177} 6299}
6178 6300
6179/* 6301/*
@@ -6185,7 +6307,7 @@ static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
6185 */ 6307 */
6186static void migrate_nr_uninterruptible(struct rq *rq_src) 6308static void migrate_nr_uninterruptible(struct rq *rq_src)
6187{ 6309{
6188 struct rq *rq_dest = cpu_rq(any_online_cpu(*CPU_MASK_ALL_PTR)); 6310 struct rq *rq_dest = cpu_rq(cpumask_any(cpu_online_mask));
6189 unsigned long flags; 6311 unsigned long flags;
6190 6312
6191 local_irq_save(flags); 6313 local_irq_save(flags);
@@ -6475,7 +6597,7 @@ static void set_rq_online(struct rq *rq)
6475 if (!rq->online) { 6597 if (!rq->online) {
6476 const struct sched_class *class; 6598 const struct sched_class *class;
6477 6599
6478 cpu_set(rq->cpu, rq->rd->online); 6600 cpumask_set_cpu(rq->cpu, rq->rd->online);
6479 rq->online = 1; 6601 rq->online = 1;
6480 6602
6481 for_each_class(class) { 6603 for_each_class(class) {
@@ -6495,7 +6617,7 @@ static void set_rq_offline(struct rq *rq)
6495 class->rq_offline(rq); 6617 class->rq_offline(rq);
6496 } 6618 }
6497 6619
6498 cpu_clear(rq->cpu, rq->rd->online); 6620 cpumask_clear_cpu(rq->cpu, rq->rd->online);
6499 rq->online = 0; 6621 rq->online = 0;
6500 } 6622 }
6501} 6623}
@@ -6536,7 +6658,7 @@ migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
6536 rq = cpu_rq(cpu); 6658 rq = cpu_rq(cpu);
6537 spin_lock_irqsave(&rq->lock, flags); 6659 spin_lock_irqsave(&rq->lock, flags);
6538 if (rq->rd) { 6660 if (rq->rd) {
6539 BUG_ON(!cpu_isset(cpu, rq->rd->span)); 6661 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
6540 6662
6541 set_rq_online(rq); 6663 set_rq_online(rq);
6542 } 6664 }
@@ -6550,7 +6672,7 @@ migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
6550 break; 6672 break;
6551 /* Unbind it from offline cpu so it can run. Fall thru. */ 6673 /* Unbind it from offline cpu so it can run. Fall thru. */
6552 kthread_bind(cpu_rq(cpu)->migration_thread, 6674 kthread_bind(cpu_rq(cpu)->migration_thread,
6553 any_online_cpu(cpu_online_map)); 6675 cpumask_any(cpu_online_mask));
6554 kthread_stop(cpu_rq(cpu)->migration_thread); 6676 kthread_stop(cpu_rq(cpu)->migration_thread);
6555 cpu_rq(cpu)->migration_thread = NULL; 6677 cpu_rq(cpu)->migration_thread = NULL;
6556 break; 6678 break;
@@ -6600,7 +6722,7 @@ migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
6600 rq = cpu_rq(cpu); 6722 rq = cpu_rq(cpu);
6601 spin_lock_irqsave(&rq->lock, flags); 6723 spin_lock_irqsave(&rq->lock, flags);
6602 if (rq->rd) { 6724 if (rq->rd) {
6603 BUG_ON(!cpu_isset(cpu, rq->rd->span)); 6725 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
6604 set_rq_offline(rq); 6726 set_rq_offline(rq);
6605 } 6727 }
6606 spin_unlock_irqrestore(&rq->lock, flags); 6728 spin_unlock_irqrestore(&rq->lock, flags);
@@ -6638,36 +6760,14 @@ early_initcall(migration_init);
6638 6760
6639#ifdef CONFIG_SCHED_DEBUG 6761#ifdef CONFIG_SCHED_DEBUG
6640 6762
6641static inline const char *sd_level_to_string(enum sched_domain_level lvl)
6642{
6643 switch (lvl) {
6644 case SD_LV_NONE:
6645 return "NONE";
6646 case SD_LV_SIBLING:
6647 return "SIBLING";
6648 case SD_LV_MC:
6649 return "MC";
6650 case SD_LV_CPU:
6651 return "CPU";
6652 case SD_LV_NODE:
6653 return "NODE";
6654 case SD_LV_ALLNODES:
6655 return "ALLNODES";
6656 case SD_LV_MAX:
6657 return "MAX";
6658
6659 }
6660 return "MAX";
6661}
6662
6663static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level, 6763static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
6664 cpumask_t *groupmask) 6764 struct cpumask *groupmask)
6665{ 6765{
6666 struct sched_group *group = sd->groups; 6766 struct sched_group *group = sd->groups;
6667 char str[256]; 6767 char str[256];
6668 6768
6669 cpulist_scnprintf(str, sizeof(str), sd->span); 6769 cpulist_scnprintf(str, sizeof(str), sched_domain_span(sd));
6670 cpus_clear(*groupmask); 6770 cpumask_clear(groupmask);
6671 6771
6672 printk(KERN_DEBUG "%*s domain %d: ", level, "", level); 6772 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
6673 6773
@@ -6679,14 +6779,13 @@ static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
6679 return -1; 6779 return -1;
6680 } 6780 }
6681 6781
6682 printk(KERN_CONT "span %s level %s\n", 6782 printk(KERN_CONT "span %s level %s\n", str, sd->name);
6683 str, sd_level_to_string(sd->level));
6684 6783
6685 if (!cpu_isset(cpu, sd->span)) { 6784 if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) {
6686 printk(KERN_ERR "ERROR: domain->span does not contain " 6785 printk(KERN_ERR "ERROR: domain->span does not contain "
6687 "CPU%d\n", cpu); 6786 "CPU%d\n", cpu);
6688 } 6787 }
6689 if (!cpu_isset(cpu, group->cpumask)) { 6788 if (!cpumask_test_cpu(cpu, sched_group_cpus(group))) {
6690 printk(KERN_ERR "ERROR: domain->groups does not contain" 6789 printk(KERN_ERR "ERROR: domain->groups does not contain"
6691 " CPU%d\n", cpu); 6790 " CPU%d\n", cpu);
6692 } 6791 }
@@ -6706,31 +6805,32 @@ static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
6706 break; 6805 break;
6707 } 6806 }
6708 6807
6709 if (!cpus_weight(group->cpumask)) { 6808 if (!cpumask_weight(sched_group_cpus(group))) {
6710 printk(KERN_CONT "\n"); 6809 printk(KERN_CONT "\n");
6711 printk(KERN_ERR "ERROR: empty group\n"); 6810 printk(KERN_ERR "ERROR: empty group\n");
6712 break; 6811 break;
6713 } 6812 }
6714 6813
6715 if (cpus_intersects(*groupmask, group->cpumask)) { 6814 if (cpumask_intersects(groupmask, sched_group_cpus(group))) {
6716 printk(KERN_CONT "\n"); 6815 printk(KERN_CONT "\n");
6717 printk(KERN_ERR "ERROR: repeated CPUs\n"); 6816 printk(KERN_ERR "ERROR: repeated CPUs\n");
6718 break; 6817 break;
6719 } 6818 }
6720 6819
6721 cpus_or(*groupmask, *groupmask, group->cpumask); 6820 cpumask_or(groupmask, groupmask, sched_group_cpus(group));
6722 6821
6723 cpulist_scnprintf(str, sizeof(str), group->cpumask); 6822 cpulist_scnprintf(str, sizeof(str), sched_group_cpus(group));
6724 printk(KERN_CONT " %s", str); 6823 printk(KERN_CONT " %s", str);
6725 6824
6726 group = group->next; 6825 group = group->next;
6727 } while (group != sd->groups); 6826 } while (group != sd->groups);
6728 printk(KERN_CONT "\n"); 6827 printk(KERN_CONT "\n");
6729 6828
6730 if (!cpus_equal(sd->span, *groupmask)) 6829 if (!cpumask_equal(sched_domain_span(sd), groupmask))
6731 printk(KERN_ERR "ERROR: groups don't span domain->span\n"); 6830 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
6732 6831
6733 if (sd->parent && !cpus_subset(*groupmask, sd->parent->span)) 6832 if (sd->parent &&
6833 !cpumask_subset(groupmask, sched_domain_span(sd->parent)))
6734 printk(KERN_ERR "ERROR: parent span is not a superset " 6834 printk(KERN_ERR "ERROR: parent span is not a superset "
6735 "of domain->span\n"); 6835 "of domain->span\n");
6736 return 0; 6836 return 0;
@@ -6738,7 +6838,7 @@ static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
6738 6838
6739static void sched_domain_debug(struct sched_domain *sd, int cpu) 6839static void sched_domain_debug(struct sched_domain *sd, int cpu)
6740{ 6840{
6741 cpumask_t *groupmask; 6841 cpumask_var_t groupmask;
6742 int level = 0; 6842 int level = 0;
6743 6843
6744 if (!sd) { 6844 if (!sd) {
@@ -6748,8 +6848,7 @@ static void sched_domain_debug(struct sched_domain *sd, int cpu)
6748 6848
6749 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu); 6849 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
6750 6850
6751 groupmask = kmalloc(sizeof(cpumask_t), GFP_KERNEL); 6851 if (!alloc_cpumask_var(&groupmask, GFP_KERNEL)) {
6752 if (!groupmask) {
6753 printk(KERN_DEBUG "Cannot load-balance (out of memory)\n"); 6852 printk(KERN_DEBUG "Cannot load-balance (out of memory)\n");
6754 return; 6853 return;
6755 } 6854 }
@@ -6762,7 +6861,7 @@ static void sched_domain_debug(struct sched_domain *sd, int cpu)
6762 if (!sd) 6861 if (!sd)
6763 break; 6862 break;
6764 } 6863 }
6765 kfree(groupmask); 6864 free_cpumask_var(groupmask);
6766} 6865}
6767#else /* !CONFIG_SCHED_DEBUG */ 6866#else /* !CONFIG_SCHED_DEBUG */
6768# define sched_domain_debug(sd, cpu) do { } while (0) 6867# define sched_domain_debug(sd, cpu) do { } while (0)
@@ -6770,7 +6869,7 @@ static void sched_domain_debug(struct sched_domain *sd, int cpu)
6770 6869
6771static int sd_degenerate(struct sched_domain *sd) 6870static int sd_degenerate(struct sched_domain *sd)
6772{ 6871{
6773 if (cpus_weight(sd->span) == 1) 6872 if (cpumask_weight(sched_domain_span(sd)) == 1)
6774 return 1; 6873 return 1;
6775 6874
6776 /* Following flags need at least 2 groups */ 6875 /* Following flags need at least 2 groups */
@@ -6801,7 +6900,7 @@ sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
6801 if (sd_degenerate(parent)) 6900 if (sd_degenerate(parent))
6802 return 1; 6901 return 1;
6803 6902
6804 if (!cpus_equal(sd->span, parent->span)) 6903 if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent)))
6805 return 0; 6904 return 0;
6806 6905
6807 /* Does parent contain flags not in child? */ 6906 /* Does parent contain flags not in child? */
@@ -6816,6 +6915,8 @@ sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
6816 SD_BALANCE_EXEC | 6915 SD_BALANCE_EXEC |
6817 SD_SHARE_CPUPOWER | 6916 SD_SHARE_CPUPOWER |
6818 SD_SHARE_PKG_RESOURCES); 6917 SD_SHARE_PKG_RESOURCES);
6918 if (nr_node_ids == 1)
6919 pflags &= ~SD_SERIALIZE;
6819 } 6920 }
6820 if (~cflags & pflags) 6921 if (~cflags & pflags)
6821 return 0; 6922 return 0;
@@ -6823,6 +6924,16 @@ sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
6823 return 1; 6924 return 1;
6824} 6925}
6825 6926
6927static void free_rootdomain(struct root_domain *rd)
6928{
6929 cpupri_cleanup(&rd->cpupri);
6930
6931 free_cpumask_var(rd->rto_mask);
6932 free_cpumask_var(rd->online);
6933 free_cpumask_var(rd->span);
6934 kfree(rd);
6935}
6936
6826static void rq_attach_root(struct rq *rq, struct root_domain *rd) 6937static void rq_attach_root(struct rq *rq, struct root_domain *rd)
6827{ 6938{
6828 unsigned long flags; 6939 unsigned long flags;
@@ -6832,38 +6943,62 @@ static void rq_attach_root(struct rq *rq, struct root_domain *rd)
6832 if (rq->rd) { 6943 if (rq->rd) {
6833 struct root_domain *old_rd = rq->rd; 6944 struct root_domain *old_rd = rq->rd;
6834 6945
6835 if (cpu_isset(rq->cpu, old_rd->online)) 6946 if (cpumask_test_cpu(rq->cpu, old_rd->online))
6836 set_rq_offline(rq); 6947 set_rq_offline(rq);
6837 6948
6838 cpu_clear(rq->cpu, old_rd->span); 6949 cpumask_clear_cpu(rq->cpu, old_rd->span);
6839 6950
6840 if (atomic_dec_and_test(&old_rd->refcount)) 6951 if (atomic_dec_and_test(&old_rd->refcount))
6841 kfree(old_rd); 6952 free_rootdomain(old_rd);
6842 } 6953 }
6843 6954
6844 atomic_inc(&rd->refcount); 6955 atomic_inc(&rd->refcount);
6845 rq->rd = rd; 6956 rq->rd = rd;
6846 6957
6847 cpu_set(rq->cpu, rd->span); 6958 cpumask_set_cpu(rq->cpu, rd->span);
6848 if (cpu_isset(rq->cpu, cpu_online_map)) 6959 if (cpumask_test_cpu(rq->cpu, cpu_online_mask))
6849 set_rq_online(rq); 6960 set_rq_online(rq);
6850 6961
6851 spin_unlock_irqrestore(&rq->lock, flags); 6962 spin_unlock_irqrestore(&rq->lock, flags);
6852} 6963}
6853 6964
6854static void init_rootdomain(struct root_domain *rd) 6965static int __init_refok init_rootdomain(struct root_domain *rd, bool bootmem)
6855{ 6966{
6856 memset(rd, 0, sizeof(*rd)); 6967 memset(rd, 0, sizeof(*rd));
6857 6968
6858 cpus_clear(rd->span); 6969 if (bootmem) {
6859 cpus_clear(rd->online); 6970 alloc_bootmem_cpumask_var(&def_root_domain.span);
6971 alloc_bootmem_cpumask_var(&def_root_domain.online);
6972 alloc_bootmem_cpumask_var(&def_root_domain.rto_mask);
6973 cpupri_init(&rd->cpupri, true);
6974 return 0;
6975 }
6976
6977 if (!alloc_cpumask_var(&rd->span, GFP_KERNEL))
6978 goto out;
6979 if (!alloc_cpumask_var(&rd->online, GFP_KERNEL))
6980 goto free_span;
6981 if (!alloc_cpumask_var(&rd->rto_mask, GFP_KERNEL))
6982 goto free_online;
6860 6983
6861 cpupri_init(&rd->cpupri); 6984 if (cpupri_init(&rd->cpupri, false) != 0)
6985 goto free_rto_mask;
6986 return 0;
6987
6988free_rto_mask:
6989 free_cpumask_var(rd->rto_mask);
6990free_online:
6991 free_cpumask_var(rd->online);
6992free_span:
6993 free_cpumask_var(rd->span);
6994out:
6995 return -ENOMEM;
6862} 6996}
6863 6997
6864static void init_defrootdomain(void) 6998static void init_defrootdomain(void)
6865{ 6999{
6866 init_rootdomain(&def_root_domain); 7000 init_rootdomain(&def_root_domain, true);
7001
6867 atomic_set(&def_root_domain.refcount, 1); 7002 atomic_set(&def_root_domain.refcount, 1);
6868} 7003}
6869 7004
@@ -6875,7 +7010,10 @@ static struct root_domain *alloc_rootdomain(void)
6875 if (!rd) 7010 if (!rd)
6876 return NULL; 7011 return NULL;
6877 7012
6878 init_rootdomain(rd); 7013 if (init_rootdomain(rd, false) != 0) {
7014 kfree(rd);
7015 return NULL;
7016 }
6879 7017
6880 return rd; 7018 return rd;
6881} 7019}
@@ -6917,19 +7055,12 @@ cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
6917} 7055}
6918 7056
6919/* cpus with isolated domains */ 7057/* cpus with isolated domains */
6920static cpumask_t cpu_isolated_map = CPU_MASK_NONE; 7058static cpumask_var_t cpu_isolated_map;
6921 7059
6922/* Setup the mask of cpus configured for isolated domains */ 7060/* Setup the mask of cpus configured for isolated domains */
6923static int __init isolated_cpu_setup(char *str) 7061static int __init isolated_cpu_setup(char *str)
6924{ 7062{
6925 static int __initdata ints[NR_CPUS]; 7063 cpulist_parse(str, cpu_isolated_map);
6926 int i;
6927
6928 str = get_options(str, ARRAY_SIZE(ints), ints);
6929 cpus_clear(cpu_isolated_map);
6930 for (i = 1; i <= ints[0]; i++)
6931 if (ints[i] < NR_CPUS)
6932 cpu_set(ints[i], cpu_isolated_map);
6933 return 1; 7064 return 1;
6934} 7065}
6935 7066
@@ -6938,42 +7069,43 @@ __setup("isolcpus=", isolated_cpu_setup);
6938/* 7069/*
6939 * init_sched_build_groups takes the cpumask we wish to span, and a pointer 7070 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
6940 * to a function which identifies what group(along with sched group) a CPU 7071 * to a function which identifies what group(along with sched group) a CPU
6941 * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS 7072 * belongs to. The return value of group_fn must be a >= 0 and < nr_cpu_ids
6942 * (due to the fact that we keep track of groups covered with a cpumask_t). 7073 * (due to the fact that we keep track of groups covered with a struct cpumask).
6943 * 7074 *
6944 * init_sched_build_groups will build a circular linked list of the groups 7075 * init_sched_build_groups will build a circular linked list of the groups
6945 * covered by the given span, and will set each group's ->cpumask correctly, 7076 * covered by the given span, and will set each group's ->cpumask correctly,
6946 * and ->cpu_power to 0. 7077 * and ->cpu_power to 0.
6947 */ 7078 */
6948static void 7079static void
6949init_sched_build_groups(const cpumask_t *span, const cpumask_t *cpu_map, 7080init_sched_build_groups(const struct cpumask *span,
6950 int (*group_fn)(int cpu, const cpumask_t *cpu_map, 7081 const struct cpumask *cpu_map,
7082 int (*group_fn)(int cpu, const struct cpumask *cpu_map,
6951 struct sched_group **sg, 7083 struct sched_group **sg,
6952 cpumask_t *tmpmask), 7084 struct cpumask *tmpmask),
6953 cpumask_t *covered, cpumask_t *tmpmask) 7085 struct cpumask *covered, struct cpumask *tmpmask)
6954{ 7086{
6955 struct sched_group *first = NULL, *last = NULL; 7087 struct sched_group *first = NULL, *last = NULL;
6956 int i; 7088 int i;
6957 7089
6958 cpus_clear(*covered); 7090 cpumask_clear(covered);
6959 7091
6960 for_each_cpu_mask_nr(i, *span) { 7092 for_each_cpu(i, span) {
6961 struct sched_group *sg; 7093 struct sched_group *sg;
6962 int group = group_fn(i, cpu_map, &sg, tmpmask); 7094 int group = group_fn(i, cpu_map, &sg, tmpmask);
6963 int j; 7095 int j;
6964 7096
6965 if (cpu_isset(i, *covered)) 7097 if (cpumask_test_cpu(i, covered))
6966 continue; 7098 continue;
6967 7099
6968 cpus_clear(sg->cpumask); 7100 cpumask_clear(sched_group_cpus(sg));
6969 sg->__cpu_power = 0; 7101 sg->__cpu_power = 0;
6970 7102
6971 for_each_cpu_mask_nr(j, *span) { 7103 for_each_cpu(j, span) {
6972 if (group_fn(j, cpu_map, NULL, tmpmask) != group) 7104 if (group_fn(j, cpu_map, NULL, tmpmask) != group)
6973 continue; 7105 continue;
6974 7106
6975 cpu_set(j, *covered); 7107 cpumask_set_cpu(j, covered);
6976 cpu_set(j, sg->cpumask); 7108 cpumask_set_cpu(j, sched_group_cpus(sg));
6977 } 7109 }
6978 if (!first) 7110 if (!first)
6979 first = sg; 7111 first = sg;
@@ -7037,23 +7169,21 @@ static int find_next_best_node(int node, nodemask_t *used_nodes)
7037 * should be one that prevents unnecessary balancing, but also spreads tasks 7169 * should be one that prevents unnecessary balancing, but also spreads tasks
7038 * out optimally. 7170 * out optimally.
7039 */ 7171 */
7040static void sched_domain_node_span(int node, cpumask_t *span) 7172static void sched_domain_node_span(int node, struct cpumask *span)
7041{ 7173{
7042 nodemask_t used_nodes; 7174 nodemask_t used_nodes;
7043 node_to_cpumask_ptr(nodemask, node);
7044 int i; 7175 int i;
7045 7176
7046 cpus_clear(*span); 7177 cpumask_clear(span);
7047 nodes_clear(used_nodes); 7178 nodes_clear(used_nodes);
7048 7179
7049 cpus_or(*span, *span, *nodemask); 7180 cpumask_or(span, span, cpumask_of_node(node));
7050 node_set(node, used_nodes); 7181 node_set(node, used_nodes);
7051 7182
7052 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) { 7183 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
7053 int next_node = find_next_best_node(node, &used_nodes); 7184 int next_node = find_next_best_node(node, &used_nodes);
7054 7185
7055 node_to_cpumask_ptr_next(nodemask, next_node); 7186 cpumask_or(span, span, cpumask_of_node(next_node));
7056 cpus_or(*span, *span, *nodemask);
7057 } 7187 }
7058} 7188}
7059#endif /* CONFIG_NUMA */ 7189#endif /* CONFIG_NUMA */
@@ -7061,18 +7191,33 @@ static void sched_domain_node_span(int node, cpumask_t *span)
7061int sched_smt_power_savings = 0, sched_mc_power_savings = 0; 7191int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
7062 7192
7063/* 7193/*
7194 * The cpus mask in sched_group and sched_domain hangs off the end.
7195 * FIXME: use cpumask_var_t or dynamic percpu alloc to avoid wasting space
7196 * for nr_cpu_ids < CONFIG_NR_CPUS.
7197 */
7198struct static_sched_group {
7199 struct sched_group sg;
7200 DECLARE_BITMAP(cpus, CONFIG_NR_CPUS);
7201};
7202
7203struct static_sched_domain {
7204 struct sched_domain sd;
7205 DECLARE_BITMAP(span, CONFIG_NR_CPUS);
7206};
7207
7208/*
7064 * SMT sched-domains: 7209 * SMT sched-domains:
7065 */ 7210 */
7066#ifdef CONFIG_SCHED_SMT 7211#ifdef CONFIG_SCHED_SMT
7067static DEFINE_PER_CPU(struct sched_domain, cpu_domains); 7212static DEFINE_PER_CPU(struct static_sched_domain, cpu_domains);
7068static DEFINE_PER_CPU(struct sched_group, sched_group_cpus); 7213static DEFINE_PER_CPU(struct static_sched_group, sched_group_cpus);
7069 7214
7070static int 7215static int
7071cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg, 7216cpu_to_cpu_group(int cpu, const struct cpumask *cpu_map,
7072 cpumask_t *unused) 7217 struct sched_group **sg, struct cpumask *unused)
7073{ 7218{
7074 if (sg) 7219 if (sg)
7075 *sg = &per_cpu(sched_group_cpus, cpu); 7220 *sg = &per_cpu(sched_group_cpus, cpu).sg;
7076 return cpu; 7221 return cpu;
7077} 7222}
7078#endif /* CONFIG_SCHED_SMT */ 7223#endif /* CONFIG_SCHED_SMT */
@@ -7081,56 +7226,53 @@ cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7081 * multi-core sched-domains: 7226 * multi-core sched-domains:
7082 */ 7227 */
7083#ifdef CONFIG_SCHED_MC 7228#ifdef CONFIG_SCHED_MC
7084static DEFINE_PER_CPU(struct sched_domain, core_domains); 7229static DEFINE_PER_CPU(struct static_sched_domain, core_domains);
7085static DEFINE_PER_CPU(struct sched_group, sched_group_core); 7230static DEFINE_PER_CPU(struct static_sched_group, sched_group_core);
7086#endif /* CONFIG_SCHED_MC */ 7231#endif /* CONFIG_SCHED_MC */
7087 7232
7088#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT) 7233#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
7089static int 7234static int
7090cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg, 7235cpu_to_core_group(int cpu, const struct cpumask *cpu_map,
7091 cpumask_t *mask) 7236 struct sched_group **sg, struct cpumask *mask)
7092{ 7237{
7093 int group; 7238 int group;
7094 7239
7095 *mask = per_cpu(cpu_sibling_map, cpu); 7240 cpumask_and(mask, &per_cpu(cpu_sibling_map, cpu), cpu_map);
7096 cpus_and(*mask, *mask, *cpu_map); 7241 group = cpumask_first(mask);
7097 group = first_cpu(*mask);
7098 if (sg) 7242 if (sg)
7099 *sg = &per_cpu(sched_group_core, group); 7243 *sg = &per_cpu(sched_group_core, group).sg;
7100 return group; 7244 return group;
7101} 7245}
7102#elif defined(CONFIG_SCHED_MC) 7246#elif defined(CONFIG_SCHED_MC)
7103static int 7247static int
7104cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg, 7248cpu_to_core_group(int cpu, const struct cpumask *cpu_map,
7105 cpumask_t *unused) 7249 struct sched_group **sg, struct cpumask *unused)
7106{ 7250{
7107 if (sg) 7251 if (sg)
7108 *sg = &per_cpu(sched_group_core, cpu); 7252 *sg = &per_cpu(sched_group_core, cpu).sg;
7109 return cpu; 7253 return cpu;
7110} 7254}
7111#endif 7255#endif
7112 7256
7113static DEFINE_PER_CPU(struct sched_domain, phys_domains); 7257static DEFINE_PER_CPU(struct static_sched_domain, phys_domains);
7114static DEFINE_PER_CPU(struct sched_group, sched_group_phys); 7258static DEFINE_PER_CPU(struct static_sched_group, sched_group_phys);
7115 7259
7116static int 7260static int
7117cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg, 7261cpu_to_phys_group(int cpu, const struct cpumask *cpu_map,
7118 cpumask_t *mask) 7262 struct sched_group **sg, struct cpumask *mask)
7119{ 7263{
7120 int group; 7264 int group;
7121#ifdef CONFIG_SCHED_MC 7265#ifdef CONFIG_SCHED_MC
7122 *mask = cpu_coregroup_map(cpu); 7266 cpumask_and(mask, cpu_coregroup_mask(cpu), cpu_map);
7123 cpus_and(*mask, *mask, *cpu_map); 7267 group = cpumask_first(mask);
7124 group = first_cpu(*mask);
7125#elif defined(CONFIG_SCHED_SMT) 7268#elif defined(CONFIG_SCHED_SMT)
7126 *mask = per_cpu(cpu_sibling_map, cpu); 7269 cpumask_and(mask, &per_cpu(cpu_sibling_map, cpu), cpu_map);
7127 cpus_and(*mask, *mask, *cpu_map); 7270 group = cpumask_first(mask);
7128 group = first_cpu(*mask);
7129#else 7271#else
7130 group = cpu; 7272 group = cpu;
7131#endif 7273#endif
7132 if (sg) 7274 if (sg)
7133 *sg = &per_cpu(sched_group_phys, group); 7275 *sg = &per_cpu(sched_group_phys, group).sg;
7134 return group; 7276 return group;
7135} 7277}
7136 7278
@@ -7144,19 +7286,19 @@ static DEFINE_PER_CPU(struct sched_domain, node_domains);
7144static struct sched_group ***sched_group_nodes_bycpu; 7286static struct sched_group ***sched_group_nodes_bycpu;
7145 7287
7146static DEFINE_PER_CPU(struct sched_domain, allnodes_domains); 7288static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
7147static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes); 7289static DEFINE_PER_CPU(struct static_sched_group, sched_group_allnodes);
7148 7290
7149static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map, 7291static int cpu_to_allnodes_group(int cpu, const struct cpumask *cpu_map,
7150 struct sched_group **sg, cpumask_t *nodemask) 7292 struct sched_group **sg,
7293 struct cpumask *nodemask)
7151{ 7294{
7152 int group; 7295 int group;
7153 7296
7154 *nodemask = node_to_cpumask(cpu_to_node(cpu)); 7297 cpumask_and(nodemask, cpumask_of_node(cpu_to_node(cpu)), cpu_map);
7155 cpus_and(*nodemask, *nodemask, *cpu_map); 7298 group = cpumask_first(nodemask);
7156 group = first_cpu(*nodemask);
7157 7299
7158 if (sg) 7300 if (sg)
7159 *sg = &per_cpu(sched_group_allnodes, group); 7301 *sg = &per_cpu(sched_group_allnodes, group).sg;
7160 return group; 7302 return group;
7161} 7303}
7162 7304
@@ -7168,11 +7310,11 @@ static void init_numa_sched_groups_power(struct sched_group *group_head)
7168 if (!sg) 7310 if (!sg)
7169 return; 7311 return;
7170 do { 7312 do {
7171 for_each_cpu_mask_nr(j, sg->cpumask) { 7313 for_each_cpu(j, sched_group_cpus(sg)) {
7172 struct sched_domain *sd; 7314 struct sched_domain *sd;
7173 7315
7174 sd = &per_cpu(phys_domains, j); 7316 sd = &per_cpu(phys_domains, j).sd;
7175 if (j != first_cpu(sd->groups->cpumask)) { 7317 if (j != cpumask_first(sched_group_cpus(sd->groups))) {
7176 /* 7318 /*
7177 * Only add "power" once for each 7319 * Only add "power" once for each
7178 * physical package. 7320 * physical package.
@@ -7189,11 +7331,12 @@ static void init_numa_sched_groups_power(struct sched_group *group_head)
7189 7331
7190#ifdef CONFIG_NUMA 7332#ifdef CONFIG_NUMA
7191/* Free memory allocated for various sched_group structures */ 7333/* Free memory allocated for various sched_group structures */
7192static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask) 7334static void free_sched_groups(const struct cpumask *cpu_map,
7335 struct cpumask *nodemask)
7193{ 7336{
7194 int cpu, i; 7337 int cpu, i;
7195 7338
7196 for_each_cpu_mask_nr(cpu, *cpu_map) { 7339 for_each_cpu(cpu, cpu_map) {
7197 struct sched_group **sched_group_nodes 7340 struct sched_group **sched_group_nodes
7198 = sched_group_nodes_bycpu[cpu]; 7341 = sched_group_nodes_bycpu[cpu];
7199 7342
@@ -7203,9 +7346,8 @@ static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
7203 for (i = 0; i < nr_node_ids; i++) { 7346 for (i = 0; i < nr_node_ids; i++) {
7204 struct sched_group *oldsg, *sg = sched_group_nodes[i]; 7347 struct sched_group *oldsg, *sg = sched_group_nodes[i];
7205 7348
7206 *nodemask = node_to_cpumask(i); 7349 cpumask_and(nodemask, cpumask_of_node(i), cpu_map);
7207 cpus_and(*nodemask, *nodemask, *cpu_map); 7350 if (cpumask_empty(nodemask))
7208 if (cpus_empty(*nodemask))
7209 continue; 7351 continue;
7210 7352
7211 if (sg == NULL) 7353 if (sg == NULL)
@@ -7223,7 +7365,8 @@ next_sg:
7223 } 7365 }
7224} 7366}
7225#else /* !CONFIG_NUMA */ 7367#else /* !CONFIG_NUMA */
7226static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask) 7368static void free_sched_groups(const struct cpumask *cpu_map,
7369 struct cpumask *nodemask)
7227{ 7370{
7228} 7371}
7229#endif /* CONFIG_NUMA */ 7372#endif /* CONFIG_NUMA */
@@ -7249,7 +7392,7 @@ static void init_sched_groups_power(int cpu, struct sched_domain *sd)
7249 7392
7250 WARN_ON(!sd || !sd->groups); 7393 WARN_ON(!sd || !sd->groups);
7251 7394
7252 if (cpu != first_cpu(sd->groups->cpumask)) 7395 if (cpu != cpumask_first(sched_group_cpus(sd->groups)))
7253 return; 7396 return;
7254 7397
7255 child = sd->child; 7398 child = sd->child;
@@ -7314,40 +7457,6 @@ SD_INIT_FUNC(CPU)
7314 SD_INIT_FUNC(MC) 7457 SD_INIT_FUNC(MC)
7315#endif 7458#endif
7316 7459
7317/*
7318 * To minimize stack usage kmalloc room for cpumasks and share the
7319 * space as the usage in build_sched_domains() dictates. Used only
7320 * if the amount of space is significant.
7321 */
7322struct allmasks {
7323 cpumask_t tmpmask; /* make this one first */
7324 union {
7325 cpumask_t nodemask;
7326 cpumask_t this_sibling_map;
7327 cpumask_t this_core_map;
7328 };
7329 cpumask_t send_covered;
7330
7331#ifdef CONFIG_NUMA
7332 cpumask_t domainspan;
7333 cpumask_t covered;
7334 cpumask_t notcovered;
7335#endif
7336};
7337
7338#if NR_CPUS > 128
7339#define SCHED_CPUMASK_ALLOC 1
7340#define SCHED_CPUMASK_FREE(v) kfree(v)
7341#define SCHED_CPUMASK_DECLARE(v) struct allmasks *v
7342#else
7343#define SCHED_CPUMASK_ALLOC 0
7344#define SCHED_CPUMASK_FREE(v)
7345#define SCHED_CPUMASK_DECLARE(v) struct allmasks _v, *v = &_v
7346#endif
7347
7348#define SCHED_CPUMASK_VAR(v, a) cpumask_t *v = (cpumask_t *) \
7349 ((unsigned long)(a) + offsetof(struct allmasks, v))
7350
7351static int default_relax_domain_level = -1; 7460static int default_relax_domain_level = -1;
7352 7461
7353static int __init setup_relax_domain_level(char *str) 7462static int __init setup_relax_domain_level(char *str)
@@ -7387,17 +7496,38 @@ static void set_domain_attribute(struct sched_domain *sd,
7387 * Build sched domains for a given set of cpus and attach the sched domains 7496 * Build sched domains for a given set of cpus and attach the sched domains
7388 * to the individual cpus 7497 * to the individual cpus
7389 */ 7498 */
7390static int __build_sched_domains(const cpumask_t *cpu_map, 7499static int __build_sched_domains(const struct cpumask *cpu_map,
7391 struct sched_domain_attr *attr) 7500 struct sched_domain_attr *attr)
7392{ 7501{
7393 int i; 7502 int i, err = -ENOMEM;
7394 struct root_domain *rd; 7503 struct root_domain *rd;
7395 SCHED_CPUMASK_DECLARE(allmasks); 7504 cpumask_var_t nodemask, this_sibling_map, this_core_map, send_covered,
7396 cpumask_t *tmpmask; 7505 tmpmask;
7397#ifdef CONFIG_NUMA 7506#ifdef CONFIG_NUMA
7507 cpumask_var_t domainspan, covered, notcovered;
7398 struct sched_group **sched_group_nodes = NULL; 7508 struct sched_group **sched_group_nodes = NULL;
7399 int sd_allnodes = 0; 7509 int sd_allnodes = 0;
7400 7510
7511 if (!alloc_cpumask_var(&domainspan, GFP_KERNEL))
7512 goto out;
7513 if (!alloc_cpumask_var(&covered, GFP_KERNEL))
7514 goto free_domainspan;
7515 if (!alloc_cpumask_var(&notcovered, GFP_KERNEL))
7516 goto free_covered;
7517#endif
7518
7519 if (!alloc_cpumask_var(&nodemask, GFP_KERNEL))
7520 goto free_notcovered;
7521 if (!alloc_cpumask_var(&this_sibling_map, GFP_KERNEL))
7522 goto free_nodemask;
7523 if (!alloc_cpumask_var(&this_core_map, GFP_KERNEL))
7524 goto free_this_sibling_map;
7525 if (!alloc_cpumask_var(&send_covered, GFP_KERNEL))
7526 goto free_this_core_map;
7527 if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
7528 goto free_send_covered;
7529
7530#ifdef CONFIG_NUMA
7401 /* 7531 /*
7402 * Allocate the per-node list of sched groups 7532 * Allocate the per-node list of sched groups
7403 */ 7533 */
@@ -7405,55 +7535,35 @@ static int __build_sched_domains(const cpumask_t *cpu_map,
7405 GFP_KERNEL); 7535 GFP_KERNEL);
7406 if (!sched_group_nodes) { 7536 if (!sched_group_nodes) {
7407 printk(KERN_WARNING "Can not alloc sched group node list\n"); 7537 printk(KERN_WARNING "Can not alloc sched group node list\n");
7408 return -ENOMEM; 7538 goto free_tmpmask;
7409 } 7539 }
7410#endif 7540#endif
7411 7541
7412 rd = alloc_rootdomain(); 7542 rd = alloc_rootdomain();
7413 if (!rd) { 7543 if (!rd) {
7414 printk(KERN_WARNING "Cannot alloc root domain\n"); 7544 printk(KERN_WARNING "Cannot alloc root domain\n");
7415#ifdef CONFIG_NUMA 7545 goto free_sched_groups;
7416 kfree(sched_group_nodes);
7417#endif
7418 return -ENOMEM;
7419 } 7546 }
7420 7547
7421#if SCHED_CPUMASK_ALLOC
7422 /* get space for all scratch cpumask variables */
7423 allmasks = kmalloc(sizeof(*allmasks), GFP_KERNEL);
7424 if (!allmasks) {
7425 printk(KERN_WARNING "Cannot alloc cpumask array\n");
7426 kfree(rd);
7427#ifdef CONFIG_NUMA 7548#ifdef CONFIG_NUMA
7428 kfree(sched_group_nodes); 7549 sched_group_nodes_bycpu[cpumask_first(cpu_map)] = sched_group_nodes;
7429#endif
7430 return -ENOMEM;
7431 }
7432#endif
7433 tmpmask = (cpumask_t *)allmasks;
7434
7435
7436#ifdef CONFIG_NUMA
7437 sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
7438#endif 7550#endif
7439 7551
7440 /* 7552 /*
7441 * Set up domains for cpus specified by the cpu_map. 7553 * Set up domains for cpus specified by the cpu_map.
7442 */ 7554 */
7443 for_each_cpu_mask_nr(i, *cpu_map) { 7555 for_each_cpu(i, cpu_map) {
7444 struct sched_domain *sd = NULL, *p; 7556 struct sched_domain *sd = NULL, *p;
7445 SCHED_CPUMASK_VAR(nodemask, allmasks);
7446 7557
7447 *nodemask = node_to_cpumask(cpu_to_node(i)); 7558 cpumask_and(nodemask, cpumask_of_node(cpu_to_node(i)), cpu_map);
7448 cpus_and(*nodemask, *nodemask, *cpu_map);
7449 7559
7450#ifdef CONFIG_NUMA 7560#ifdef CONFIG_NUMA
7451 if (cpus_weight(*cpu_map) > 7561 if (cpumask_weight(cpu_map) >
7452 SD_NODES_PER_DOMAIN*cpus_weight(*nodemask)) { 7562 SD_NODES_PER_DOMAIN*cpumask_weight(nodemask)) {
7453 sd = &per_cpu(allnodes_domains, i); 7563 sd = &per_cpu(allnodes_domains, i);
7454 SD_INIT(sd, ALLNODES); 7564 SD_INIT(sd, ALLNODES);
7455 set_domain_attribute(sd, attr); 7565 set_domain_attribute(sd, attr);
7456 sd->span = *cpu_map; 7566 cpumask_copy(sched_domain_span(sd), cpu_map);
7457 cpu_to_allnodes_group(i, cpu_map, &sd->groups, tmpmask); 7567 cpu_to_allnodes_group(i, cpu_map, &sd->groups, tmpmask);
7458 p = sd; 7568 p = sd;
7459 sd_allnodes = 1; 7569 sd_allnodes = 1;
@@ -7463,18 +7573,19 @@ static int __build_sched_domains(const cpumask_t *cpu_map,
7463 sd = &per_cpu(node_domains, i); 7573 sd = &per_cpu(node_domains, i);
7464 SD_INIT(sd, NODE); 7574 SD_INIT(sd, NODE);
7465 set_domain_attribute(sd, attr); 7575 set_domain_attribute(sd, attr);
7466 sched_domain_node_span(cpu_to_node(i), &sd->span); 7576 sched_domain_node_span(cpu_to_node(i), sched_domain_span(sd));
7467 sd->parent = p; 7577 sd->parent = p;
7468 if (p) 7578 if (p)
7469 p->child = sd; 7579 p->child = sd;
7470 cpus_and(sd->span, sd->span, *cpu_map); 7580 cpumask_and(sched_domain_span(sd),
7581 sched_domain_span(sd), cpu_map);
7471#endif 7582#endif
7472 7583
7473 p = sd; 7584 p = sd;
7474 sd = &per_cpu(phys_domains, i); 7585 sd = &per_cpu(phys_domains, i).sd;
7475 SD_INIT(sd, CPU); 7586 SD_INIT(sd, CPU);
7476 set_domain_attribute(sd, attr); 7587 set_domain_attribute(sd, attr);
7477 sd->span = *nodemask; 7588 cpumask_copy(sched_domain_span(sd), nodemask);
7478 sd->parent = p; 7589 sd->parent = p;
7479 if (p) 7590 if (p)
7480 p->child = sd; 7591 p->child = sd;
@@ -7482,11 +7593,11 @@ static int __build_sched_domains(const cpumask_t *cpu_map,
7482 7593
7483#ifdef CONFIG_SCHED_MC 7594#ifdef CONFIG_SCHED_MC
7484 p = sd; 7595 p = sd;
7485 sd = &per_cpu(core_domains, i); 7596 sd = &per_cpu(core_domains, i).sd;
7486 SD_INIT(sd, MC); 7597 SD_INIT(sd, MC);
7487 set_domain_attribute(sd, attr); 7598 set_domain_attribute(sd, attr);
7488 sd->span = cpu_coregroup_map(i); 7599 cpumask_and(sched_domain_span(sd), cpu_map,
7489 cpus_and(sd->span, sd->span, *cpu_map); 7600 cpu_coregroup_mask(i));
7490 sd->parent = p; 7601 sd->parent = p;
7491 p->child = sd; 7602 p->child = sd;
7492 cpu_to_core_group(i, cpu_map, &sd->groups, tmpmask); 7603 cpu_to_core_group(i, cpu_map, &sd->groups, tmpmask);
@@ -7494,11 +7605,11 @@ static int __build_sched_domains(const cpumask_t *cpu_map,
7494 7605
7495#ifdef CONFIG_SCHED_SMT 7606#ifdef CONFIG_SCHED_SMT
7496 p = sd; 7607 p = sd;
7497 sd = &per_cpu(cpu_domains, i); 7608 sd = &per_cpu(cpu_domains, i).sd;
7498 SD_INIT(sd, SIBLING); 7609 SD_INIT(sd, SIBLING);
7499 set_domain_attribute(sd, attr); 7610 set_domain_attribute(sd, attr);
7500 sd->span = per_cpu(cpu_sibling_map, i); 7611 cpumask_and(sched_domain_span(sd),
7501 cpus_and(sd->span, sd->span, *cpu_map); 7612 &per_cpu(cpu_sibling_map, i), cpu_map);
7502 sd->parent = p; 7613 sd->parent = p;
7503 p->child = sd; 7614 p->child = sd;
7504 cpu_to_cpu_group(i, cpu_map, &sd->groups, tmpmask); 7615 cpu_to_cpu_group(i, cpu_map, &sd->groups, tmpmask);
@@ -7507,13 +7618,10 @@ static int __build_sched_domains(const cpumask_t *cpu_map,
7507 7618
7508#ifdef CONFIG_SCHED_SMT 7619#ifdef CONFIG_SCHED_SMT
7509 /* Set up CPU (sibling) groups */ 7620 /* Set up CPU (sibling) groups */
7510 for_each_cpu_mask_nr(i, *cpu_map) { 7621 for_each_cpu(i, cpu_map) {
7511 SCHED_CPUMASK_VAR(this_sibling_map, allmasks); 7622 cpumask_and(this_sibling_map,
7512 SCHED_CPUMASK_VAR(send_covered, allmasks); 7623 &per_cpu(cpu_sibling_map, i), cpu_map);
7513 7624 if (i != cpumask_first(this_sibling_map))
7514 *this_sibling_map = per_cpu(cpu_sibling_map, i);
7515 cpus_and(*this_sibling_map, *this_sibling_map, *cpu_map);
7516 if (i != first_cpu(*this_sibling_map))
7517 continue; 7625 continue;
7518 7626
7519 init_sched_build_groups(this_sibling_map, cpu_map, 7627 init_sched_build_groups(this_sibling_map, cpu_map,
@@ -7524,13 +7632,9 @@ static int __build_sched_domains(const cpumask_t *cpu_map,
7524 7632
7525#ifdef CONFIG_SCHED_MC 7633#ifdef CONFIG_SCHED_MC
7526 /* Set up multi-core groups */ 7634 /* Set up multi-core groups */
7527 for_each_cpu_mask_nr(i, *cpu_map) { 7635 for_each_cpu(i, cpu_map) {
7528 SCHED_CPUMASK_VAR(this_core_map, allmasks); 7636 cpumask_and(this_core_map, cpu_coregroup_mask(i), cpu_map);
7529 SCHED_CPUMASK_VAR(send_covered, allmasks); 7637 if (i != cpumask_first(this_core_map))
7530
7531 *this_core_map = cpu_coregroup_map(i);
7532 cpus_and(*this_core_map, *this_core_map, *cpu_map);
7533 if (i != first_cpu(*this_core_map))
7534 continue; 7638 continue;
7535 7639
7536 init_sched_build_groups(this_core_map, cpu_map, 7640 init_sched_build_groups(this_core_map, cpu_map,
@@ -7541,12 +7645,8 @@ static int __build_sched_domains(const cpumask_t *cpu_map,
7541 7645
7542 /* Set up physical groups */ 7646 /* Set up physical groups */
7543 for (i = 0; i < nr_node_ids; i++) { 7647 for (i = 0; i < nr_node_ids; i++) {
7544 SCHED_CPUMASK_VAR(nodemask, allmasks); 7648 cpumask_and(nodemask, cpumask_of_node(i), cpu_map);
7545 SCHED_CPUMASK_VAR(send_covered, allmasks); 7649 if (cpumask_empty(nodemask))
7546
7547 *nodemask = node_to_cpumask(i);
7548 cpus_and(*nodemask, *nodemask, *cpu_map);
7549 if (cpus_empty(*nodemask))
7550 continue; 7650 continue;
7551 7651
7552 init_sched_build_groups(nodemask, cpu_map, 7652 init_sched_build_groups(nodemask, cpu_map,
@@ -7557,8 +7657,6 @@ static int __build_sched_domains(const cpumask_t *cpu_map,
7557#ifdef CONFIG_NUMA 7657#ifdef CONFIG_NUMA
7558 /* Set up node groups */ 7658 /* Set up node groups */
7559 if (sd_allnodes) { 7659 if (sd_allnodes) {
7560 SCHED_CPUMASK_VAR(send_covered, allmasks);
7561
7562 init_sched_build_groups(cpu_map, cpu_map, 7660 init_sched_build_groups(cpu_map, cpu_map,
7563 &cpu_to_allnodes_group, 7661 &cpu_to_allnodes_group,
7564 send_covered, tmpmask); 7662 send_covered, tmpmask);
@@ -7567,58 +7665,53 @@ static int __build_sched_domains(const cpumask_t *cpu_map,
7567 for (i = 0; i < nr_node_ids; i++) { 7665 for (i = 0; i < nr_node_ids; i++) {
7568 /* Set up node groups */ 7666 /* Set up node groups */
7569 struct sched_group *sg, *prev; 7667 struct sched_group *sg, *prev;
7570 SCHED_CPUMASK_VAR(nodemask, allmasks);
7571 SCHED_CPUMASK_VAR(domainspan, allmasks);
7572 SCHED_CPUMASK_VAR(covered, allmasks);
7573 int j; 7668 int j;
7574 7669
7575 *nodemask = node_to_cpumask(i); 7670 cpumask_clear(covered);
7576 cpus_clear(*covered); 7671 cpumask_and(nodemask, cpumask_of_node(i), cpu_map);
7577 7672 if (cpumask_empty(nodemask)) {
7578 cpus_and(*nodemask, *nodemask, *cpu_map);
7579 if (cpus_empty(*nodemask)) {
7580 sched_group_nodes[i] = NULL; 7673 sched_group_nodes[i] = NULL;
7581 continue; 7674 continue;
7582 } 7675 }
7583 7676
7584 sched_domain_node_span(i, domainspan); 7677 sched_domain_node_span(i, domainspan);
7585 cpus_and(*domainspan, *domainspan, *cpu_map); 7678 cpumask_and(domainspan, domainspan, cpu_map);
7586 7679
7587 sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i); 7680 sg = kmalloc_node(sizeof(struct sched_group) + cpumask_size(),
7681 GFP_KERNEL, i);
7588 if (!sg) { 7682 if (!sg) {
7589 printk(KERN_WARNING "Can not alloc domain group for " 7683 printk(KERN_WARNING "Can not alloc domain group for "
7590 "node %d\n", i); 7684 "node %d\n", i);
7591 goto error; 7685 goto error;
7592 } 7686 }
7593 sched_group_nodes[i] = sg; 7687 sched_group_nodes[i] = sg;
7594 for_each_cpu_mask_nr(j, *nodemask) { 7688 for_each_cpu(j, nodemask) {
7595 struct sched_domain *sd; 7689 struct sched_domain *sd;
7596 7690
7597 sd = &per_cpu(node_domains, j); 7691 sd = &per_cpu(node_domains, j);
7598 sd->groups = sg; 7692 sd->groups = sg;
7599 } 7693 }
7600 sg->__cpu_power = 0; 7694 sg->__cpu_power = 0;
7601 sg->cpumask = *nodemask; 7695 cpumask_copy(sched_group_cpus(sg), nodemask);
7602 sg->next = sg; 7696 sg->next = sg;
7603 cpus_or(*covered, *covered, *nodemask); 7697 cpumask_or(covered, covered, nodemask);
7604 prev = sg; 7698 prev = sg;
7605 7699
7606 for (j = 0; j < nr_node_ids; j++) { 7700 for (j = 0; j < nr_node_ids; j++) {
7607 SCHED_CPUMASK_VAR(notcovered, allmasks);
7608 int n = (i + j) % nr_node_ids; 7701 int n = (i + j) % nr_node_ids;
7609 node_to_cpumask_ptr(pnodemask, n);
7610 7702
7611 cpus_complement(*notcovered, *covered); 7703 cpumask_complement(notcovered, covered);
7612 cpus_and(*tmpmask, *notcovered, *cpu_map); 7704 cpumask_and(tmpmask, notcovered, cpu_map);
7613 cpus_and(*tmpmask, *tmpmask, *domainspan); 7705 cpumask_and(tmpmask, tmpmask, domainspan);
7614 if (cpus_empty(*tmpmask)) 7706 if (cpumask_empty(tmpmask))
7615 break; 7707 break;
7616 7708
7617 cpus_and(*tmpmask, *tmpmask, *pnodemask); 7709 cpumask_and(tmpmask, tmpmask, cpumask_of_node(n));
7618 if (cpus_empty(*tmpmask)) 7710 if (cpumask_empty(tmpmask))
7619 continue; 7711 continue;
7620 7712
7621 sg = kmalloc_node(sizeof(struct sched_group), 7713 sg = kmalloc_node(sizeof(struct sched_group) +
7714 cpumask_size(),
7622 GFP_KERNEL, i); 7715 GFP_KERNEL, i);
7623 if (!sg) { 7716 if (!sg) {
7624 printk(KERN_WARNING 7717 printk(KERN_WARNING
@@ -7626,9 +7719,9 @@ static int __build_sched_domains(const cpumask_t *cpu_map,
7626 goto error; 7719 goto error;
7627 } 7720 }
7628 sg->__cpu_power = 0; 7721 sg->__cpu_power = 0;
7629 sg->cpumask = *tmpmask; 7722 cpumask_copy(sched_group_cpus(sg), tmpmask);
7630 sg->next = prev->next; 7723 sg->next = prev->next;
7631 cpus_or(*covered, *covered, *tmpmask); 7724 cpumask_or(covered, covered, tmpmask);
7632 prev->next = sg; 7725 prev->next = sg;
7633 prev = sg; 7726 prev = sg;
7634 } 7727 }
@@ -7637,22 +7730,22 @@ static int __build_sched_domains(const cpumask_t *cpu_map,
7637 7730
7638 /* Calculate CPU power for physical packages and nodes */ 7731 /* Calculate CPU power for physical packages and nodes */
7639#ifdef CONFIG_SCHED_SMT 7732#ifdef CONFIG_SCHED_SMT
7640 for_each_cpu_mask_nr(i, *cpu_map) { 7733 for_each_cpu(i, cpu_map) {
7641 struct sched_domain *sd = &per_cpu(cpu_domains, i); 7734 struct sched_domain *sd = &per_cpu(cpu_domains, i).sd;
7642 7735
7643 init_sched_groups_power(i, sd); 7736 init_sched_groups_power(i, sd);
7644 } 7737 }
7645#endif 7738#endif
7646#ifdef CONFIG_SCHED_MC 7739#ifdef CONFIG_SCHED_MC
7647 for_each_cpu_mask_nr(i, *cpu_map) { 7740 for_each_cpu(i, cpu_map) {
7648 struct sched_domain *sd = &per_cpu(core_domains, i); 7741 struct sched_domain *sd = &per_cpu(core_domains, i).sd;
7649 7742
7650 init_sched_groups_power(i, sd); 7743 init_sched_groups_power(i, sd);
7651 } 7744 }
7652#endif 7745#endif
7653 7746
7654 for_each_cpu_mask_nr(i, *cpu_map) { 7747 for_each_cpu(i, cpu_map) {
7655 struct sched_domain *sd = &per_cpu(phys_domains, i); 7748 struct sched_domain *sd = &per_cpu(phys_domains, i).sd;
7656 7749
7657 init_sched_groups_power(i, sd); 7750 init_sched_groups_power(i, sd);
7658 } 7751 }
@@ -7664,56 +7757,87 @@ static int __build_sched_domains(const cpumask_t *cpu_map,
7664 if (sd_allnodes) { 7757 if (sd_allnodes) {
7665 struct sched_group *sg; 7758 struct sched_group *sg;
7666 7759
7667 cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg, 7760 cpu_to_allnodes_group(cpumask_first(cpu_map), cpu_map, &sg,
7668 tmpmask); 7761 tmpmask);
7669 init_numa_sched_groups_power(sg); 7762 init_numa_sched_groups_power(sg);
7670 } 7763 }
7671#endif 7764#endif
7672 7765
7673 /* Attach the domains */ 7766 /* Attach the domains */
7674 for_each_cpu_mask_nr(i, *cpu_map) { 7767 for_each_cpu(i, cpu_map) {
7675 struct sched_domain *sd; 7768 struct sched_domain *sd;
7676#ifdef CONFIG_SCHED_SMT 7769#ifdef CONFIG_SCHED_SMT
7677 sd = &per_cpu(cpu_domains, i); 7770 sd = &per_cpu(cpu_domains, i).sd;
7678#elif defined(CONFIG_SCHED_MC) 7771#elif defined(CONFIG_SCHED_MC)
7679 sd = &per_cpu(core_domains, i); 7772 sd = &per_cpu(core_domains, i).sd;
7680#else 7773#else
7681 sd = &per_cpu(phys_domains, i); 7774 sd = &per_cpu(phys_domains, i).sd;
7682#endif 7775#endif
7683 cpu_attach_domain(sd, rd, i); 7776 cpu_attach_domain(sd, rd, i);
7684 } 7777 }
7685 7778
7686 SCHED_CPUMASK_FREE((void *)allmasks); 7779 err = 0;
7687 return 0; 7780
7781free_tmpmask:
7782 free_cpumask_var(tmpmask);
7783free_send_covered:
7784 free_cpumask_var(send_covered);
7785free_this_core_map:
7786 free_cpumask_var(this_core_map);
7787free_this_sibling_map:
7788 free_cpumask_var(this_sibling_map);
7789free_nodemask:
7790 free_cpumask_var(nodemask);
7791free_notcovered:
7792#ifdef CONFIG_NUMA
7793 free_cpumask_var(notcovered);
7794free_covered:
7795 free_cpumask_var(covered);
7796free_domainspan:
7797 free_cpumask_var(domainspan);
7798out:
7799#endif
7800 return err;
7801
7802free_sched_groups:
7803#ifdef CONFIG_NUMA
7804 kfree(sched_group_nodes);
7805#endif
7806 goto free_tmpmask;
7688 7807
7689#ifdef CONFIG_NUMA 7808#ifdef CONFIG_NUMA
7690error: 7809error:
7691 free_sched_groups(cpu_map, tmpmask); 7810 free_sched_groups(cpu_map, tmpmask);
7692 SCHED_CPUMASK_FREE((void *)allmasks); 7811 free_rootdomain(rd);
7693 kfree(rd); 7812 goto free_tmpmask;
7694 return -ENOMEM;
7695#endif 7813#endif
7696} 7814}
7697 7815
7698static int build_sched_domains(const cpumask_t *cpu_map) 7816static int build_sched_domains(const struct cpumask *cpu_map)
7699{ 7817{
7700 return __build_sched_domains(cpu_map, NULL); 7818 return __build_sched_domains(cpu_map, NULL);
7701} 7819}
7702 7820
7703static cpumask_t *doms_cur; /* current sched domains */ 7821static struct cpumask *doms_cur; /* current sched domains */
7704static int ndoms_cur; /* number of sched domains in 'doms_cur' */ 7822static int ndoms_cur; /* number of sched domains in 'doms_cur' */
7705static struct sched_domain_attr *dattr_cur; 7823static struct sched_domain_attr *dattr_cur;
7706 /* attribues of custom domains in 'doms_cur' */ 7824 /* attribues of custom domains in 'doms_cur' */
7707 7825
7708/* 7826/*
7709 * Special case: If a kmalloc of a doms_cur partition (array of 7827 * Special case: If a kmalloc of a doms_cur partition (array of
7710 * cpumask_t) fails, then fallback to a single sched domain, 7828 * cpumask) fails, then fallback to a single sched domain,
7711 * as determined by the single cpumask_t fallback_doms. 7829 * as determined by the single cpumask fallback_doms.
7712 */ 7830 */
7713static cpumask_t fallback_doms; 7831static cpumask_var_t fallback_doms;
7714 7832
7715void __attribute__((weak)) arch_update_cpu_topology(void) 7833/*
7834 * arch_update_cpu_topology lets virtualized architectures update the
7835 * cpu core maps. It is supposed to return 1 if the topology changed
7836 * or 0 if it stayed the same.
7837 */
7838int __attribute__((weak)) arch_update_cpu_topology(void)
7716{ 7839{
7840 return 0;
7717} 7841}
7718 7842
7719/* 7843/*
@@ -7721,16 +7845,16 @@ void __attribute__((weak)) arch_update_cpu_topology(void)
7721 * For now this just excludes isolated cpus, but could be used to 7845 * For now this just excludes isolated cpus, but could be used to
7722 * exclude other special cases in the future. 7846 * exclude other special cases in the future.
7723 */ 7847 */
7724static int arch_init_sched_domains(const cpumask_t *cpu_map) 7848static int arch_init_sched_domains(const struct cpumask *cpu_map)
7725{ 7849{
7726 int err; 7850 int err;
7727 7851
7728 arch_update_cpu_topology(); 7852 arch_update_cpu_topology();
7729 ndoms_cur = 1; 7853 ndoms_cur = 1;
7730 doms_cur = kmalloc(sizeof(cpumask_t), GFP_KERNEL); 7854 doms_cur = kmalloc(cpumask_size(), GFP_KERNEL);
7731 if (!doms_cur) 7855 if (!doms_cur)
7732 doms_cur = &fallback_doms; 7856 doms_cur = fallback_doms;
7733 cpus_andnot(*doms_cur, *cpu_map, cpu_isolated_map); 7857 cpumask_andnot(doms_cur, cpu_map, cpu_isolated_map);
7734 dattr_cur = NULL; 7858 dattr_cur = NULL;
7735 err = build_sched_domains(doms_cur); 7859 err = build_sched_domains(doms_cur);
7736 register_sched_domain_sysctl(); 7860 register_sched_domain_sysctl();
@@ -7738,8 +7862,8 @@ static int arch_init_sched_domains(const cpumask_t *cpu_map)
7738 return err; 7862 return err;
7739} 7863}
7740 7864
7741static void arch_destroy_sched_domains(const cpumask_t *cpu_map, 7865static void arch_destroy_sched_domains(const struct cpumask *cpu_map,
7742 cpumask_t *tmpmask) 7866 struct cpumask *tmpmask)
7743{ 7867{
7744 free_sched_groups(cpu_map, tmpmask); 7868 free_sched_groups(cpu_map, tmpmask);
7745} 7869}
@@ -7748,17 +7872,16 @@ static void arch_destroy_sched_domains(const cpumask_t *cpu_map,
7748 * Detach sched domains from a group of cpus specified in cpu_map 7872 * Detach sched domains from a group of cpus specified in cpu_map
7749 * These cpus will now be attached to the NULL domain 7873 * These cpus will now be attached to the NULL domain
7750 */ 7874 */
7751static void detach_destroy_domains(const cpumask_t *cpu_map) 7875static void detach_destroy_domains(const struct cpumask *cpu_map)
7752{ 7876{
7753 cpumask_t tmpmask; 7877 /* Save because hotplug lock held. */
7878 static DECLARE_BITMAP(tmpmask, CONFIG_NR_CPUS);
7754 int i; 7879 int i;
7755 7880
7756 unregister_sched_domain_sysctl(); 7881 for_each_cpu(i, cpu_map)
7757
7758 for_each_cpu_mask_nr(i, *cpu_map)
7759 cpu_attach_domain(NULL, &def_root_domain, i); 7882 cpu_attach_domain(NULL, &def_root_domain, i);
7760 synchronize_sched(); 7883 synchronize_sched();
7761 arch_destroy_sched_domains(cpu_map, &tmpmask); 7884 arch_destroy_sched_domains(cpu_map, to_cpumask(tmpmask));
7762} 7885}
7763 7886
7764/* handle null as "default" */ 7887/* handle null as "default" */
@@ -7783,7 +7906,7 @@ static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
7783 * doms_new[] to the current sched domain partitioning, doms_cur[]. 7906 * doms_new[] to the current sched domain partitioning, doms_cur[].
7784 * It destroys each deleted domain and builds each new domain. 7907 * It destroys each deleted domain and builds each new domain.
7785 * 7908 *
7786 * 'doms_new' is an array of cpumask_t's of length 'ndoms_new'. 7909 * 'doms_new' is an array of cpumask's of length 'ndoms_new'.
7787 * The masks don't intersect (don't overlap.) We should setup one 7910 * The masks don't intersect (don't overlap.) We should setup one
7788 * sched domain for each mask. CPUs not in any of the cpumasks will 7911 * sched domain for each mask. CPUs not in any of the cpumasks will
7789 * not be load balanced. If the same cpumask appears both in the 7912 * not be load balanced. If the same cpumask appears both in the
@@ -7797,28 +7920,33 @@ static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
7797 * the single partition 'fallback_doms', it also forces the domains 7920 * the single partition 'fallback_doms', it also forces the domains
7798 * to be rebuilt. 7921 * to be rebuilt.
7799 * 7922 *
7800 * If doms_new == NULL it will be replaced with cpu_online_map. 7923 * If doms_new == NULL it will be replaced with cpu_online_mask.
7801 * ndoms_new == 0 is a special case for destroying existing domains, 7924 * ndoms_new == 0 is a special case for destroying existing domains,
7802 * and it will not create the default domain. 7925 * and it will not create the default domain.
7803 * 7926 *
7804 * Call with hotplug lock held 7927 * Call with hotplug lock held
7805 */ 7928 */
7806void partition_sched_domains(int ndoms_new, cpumask_t *doms_new, 7929/* FIXME: Change to struct cpumask *doms_new[] */
7930void partition_sched_domains(int ndoms_new, struct cpumask *doms_new,
7807 struct sched_domain_attr *dattr_new) 7931 struct sched_domain_attr *dattr_new)
7808{ 7932{
7809 int i, j, n; 7933 int i, j, n;
7934 int new_topology;
7810 7935
7811 mutex_lock(&sched_domains_mutex); 7936 mutex_lock(&sched_domains_mutex);
7812 7937
7813 /* always unregister in case we don't destroy any domains */ 7938 /* always unregister in case we don't destroy any domains */
7814 unregister_sched_domain_sysctl(); 7939 unregister_sched_domain_sysctl();
7815 7940
7941 /* Let architecture update cpu core mappings. */
7942 new_topology = arch_update_cpu_topology();
7943
7816 n = doms_new ? ndoms_new : 0; 7944 n = doms_new ? ndoms_new : 0;
7817 7945
7818 /* Destroy deleted domains */ 7946 /* Destroy deleted domains */
7819 for (i = 0; i < ndoms_cur; i++) { 7947 for (i = 0; i < ndoms_cur; i++) {
7820 for (j = 0; j < n; j++) { 7948 for (j = 0; j < n && !new_topology; j++) {
7821 if (cpus_equal(doms_cur[i], doms_new[j]) 7949 if (cpumask_equal(&doms_cur[i], &doms_new[j])
7822 && dattrs_equal(dattr_cur, i, dattr_new, j)) 7950 && dattrs_equal(dattr_cur, i, dattr_new, j))
7823 goto match1; 7951 goto match1;
7824 } 7952 }
@@ -7830,15 +7958,15 @@ match1:
7830 7958
7831 if (doms_new == NULL) { 7959 if (doms_new == NULL) {
7832 ndoms_cur = 0; 7960 ndoms_cur = 0;
7833 doms_new = &fallback_doms; 7961 doms_new = fallback_doms;
7834 cpus_andnot(doms_new[0], cpu_online_map, cpu_isolated_map); 7962 cpumask_andnot(&doms_new[0], cpu_online_mask, cpu_isolated_map);
7835 dattr_new = NULL; 7963 WARN_ON_ONCE(dattr_new);
7836 } 7964 }
7837 7965
7838 /* Build new domains */ 7966 /* Build new domains */
7839 for (i = 0; i < ndoms_new; i++) { 7967 for (i = 0; i < ndoms_new; i++) {
7840 for (j = 0; j < ndoms_cur; j++) { 7968 for (j = 0; j < ndoms_cur && !new_topology; j++) {
7841 if (cpus_equal(doms_new[i], doms_cur[j]) 7969 if (cpumask_equal(&doms_new[i], &doms_cur[j])
7842 && dattrs_equal(dattr_new, i, dattr_cur, j)) 7970 && dattrs_equal(dattr_new, i, dattr_cur, j))
7843 goto match2; 7971 goto match2;
7844 } 7972 }
@@ -7850,7 +7978,7 @@ match2:
7850 } 7978 }
7851 7979
7852 /* Remember the new sched domains */ 7980 /* Remember the new sched domains */
7853 if (doms_cur != &fallback_doms) 7981 if (doms_cur != fallback_doms)
7854 kfree(doms_cur); 7982 kfree(doms_cur);
7855 kfree(dattr_cur); /* kfree(NULL) is safe */ 7983 kfree(dattr_cur); /* kfree(NULL) is safe */
7856 doms_cur = doms_new; 7984 doms_cur = doms_new;
@@ -7863,7 +7991,7 @@ match2:
7863} 7991}
7864 7992
7865#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) 7993#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
7866int arch_reinit_sched_domains(void) 7994static void arch_reinit_sched_domains(void)
7867{ 7995{
7868 get_online_cpus(); 7996 get_online_cpus();
7869 7997
@@ -7872,25 +8000,33 @@ int arch_reinit_sched_domains(void)
7872 8000
7873 rebuild_sched_domains(); 8001 rebuild_sched_domains();
7874 put_online_cpus(); 8002 put_online_cpus();
7875
7876 return 0;
7877} 8003}
7878 8004
7879static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt) 8005static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
7880{ 8006{
7881 int ret; 8007 unsigned int level = 0;
7882 8008
7883 if (buf[0] != '0' && buf[0] != '1') 8009 if (sscanf(buf, "%u", &level) != 1)
8010 return -EINVAL;
8011
8012 /*
8013 * level is always be positive so don't check for
8014 * level < POWERSAVINGS_BALANCE_NONE which is 0
8015 * What happens on 0 or 1 byte write,
8016 * need to check for count as well?
8017 */
8018
8019 if (level >= MAX_POWERSAVINGS_BALANCE_LEVELS)
7884 return -EINVAL; 8020 return -EINVAL;
7885 8021
7886 if (smt) 8022 if (smt)
7887 sched_smt_power_savings = (buf[0] == '1'); 8023 sched_smt_power_savings = level;
7888 else 8024 else
7889 sched_mc_power_savings = (buf[0] == '1'); 8025 sched_mc_power_savings = level;
7890 8026
7891 ret = arch_reinit_sched_domains(); 8027 arch_reinit_sched_domains();
7892 8028
7893 return ret ? ret : count; 8029 return count;
7894} 8030}
7895 8031
7896#ifdef CONFIG_SCHED_MC 8032#ifdef CONFIG_SCHED_MC
@@ -7925,7 +8061,7 @@ static SYSDEV_CLASS_ATTR(sched_smt_power_savings, 0644,
7925 sched_smt_power_savings_store); 8061 sched_smt_power_savings_store);
7926#endif 8062#endif
7927 8063
7928int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls) 8064int __init sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
7929{ 8065{
7930 int err = 0; 8066 int err = 0;
7931 8067
@@ -7990,7 +8126,9 @@ static int update_runtime(struct notifier_block *nfb,
7990 8126
7991void __init sched_init_smp(void) 8127void __init sched_init_smp(void)
7992{ 8128{
7993 cpumask_t non_isolated_cpus; 8129 cpumask_var_t non_isolated_cpus;
8130
8131 alloc_cpumask_var(&non_isolated_cpus, GFP_KERNEL);
7994 8132
7995#if defined(CONFIG_NUMA) 8133#if defined(CONFIG_NUMA)
7996 sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **), 8134 sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
@@ -7999,10 +8137,10 @@ void __init sched_init_smp(void)
7999#endif 8137#endif
8000 get_online_cpus(); 8138 get_online_cpus();
8001 mutex_lock(&sched_domains_mutex); 8139 mutex_lock(&sched_domains_mutex);
8002 arch_init_sched_domains(&cpu_online_map); 8140 arch_init_sched_domains(cpu_online_mask);
8003 cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map); 8141 cpumask_andnot(non_isolated_cpus, cpu_possible_mask, cpu_isolated_map);
8004 if (cpus_empty(non_isolated_cpus)) 8142 if (cpumask_empty(non_isolated_cpus))
8005 cpu_set(smp_processor_id(), non_isolated_cpus); 8143 cpumask_set_cpu(smp_processor_id(), non_isolated_cpus);
8006 mutex_unlock(&sched_domains_mutex); 8144 mutex_unlock(&sched_domains_mutex);
8007 put_online_cpus(); 8145 put_online_cpus();
8008 8146
@@ -8017,9 +8155,13 @@ void __init sched_init_smp(void)
8017 init_hrtick(); 8155 init_hrtick();
8018 8156
8019 /* Move init over to a non-isolated CPU */ 8157 /* Move init over to a non-isolated CPU */
8020 if (set_cpus_allowed_ptr(current, &non_isolated_cpus) < 0) 8158 if (set_cpus_allowed_ptr(current, non_isolated_cpus) < 0)
8021 BUG(); 8159 BUG();
8022 sched_init_granularity(); 8160 sched_init_granularity();
8161 free_cpumask_var(non_isolated_cpus);
8162
8163 alloc_cpumask_var(&fallback_doms, GFP_KERNEL);
8164 init_sched_rt_class();
8023} 8165}
8024#else 8166#else
8025void __init sched_init_smp(void) 8167void __init sched_init_smp(void)
@@ -8334,6 +8476,15 @@ void __init sched_init(void)
8334 */ 8476 */
8335 current->sched_class = &fair_sched_class; 8477 current->sched_class = &fair_sched_class;
8336 8478
8479 /* Allocate the nohz_cpu_mask if CONFIG_CPUMASK_OFFSTACK */
8480 alloc_bootmem_cpumask_var(&nohz_cpu_mask);
8481#ifdef CONFIG_SMP
8482#ifdef CONFIG_NO_HZ
8483 alloc_bootmem_cpumask_var(&nohz.cpu_mask);
8484#endif
8485 alloc_bootmem_cpumask_var(&cpu_isolated_map);
8486#endif /* SMP */
8487
8337 scheduler_running = 1; 8488 scheduler_running = 1;
8338} 8489}
8339 8490
@@ -8492,7 +8643,7 @@ static
8492int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent) 8643int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
8493{ 8644{
8494 struct cfs_rq *cfs_rq; 8645 struct cfs_rq *cfs_rq;
8495 struct sched_entity *se, *parent_se; 8646 struct sched_entity *se;
8496 struct rq *rq; 8647 struct rq *rq;
8497 int i; 8648 int i;
8498 8649
@@ -8508,18 +8659,17 @@ int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
8508 for_each_possible_cpu(i) { 8659 for_each_possible_cpu(i) {
8509 rq = cpu_rq(i); 8660 rq = cpu_rq(i);
8510 8661
8511 cfs_rq = kmalloc_node(sizeof(struct cfs_rq), 8662 cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
8512 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i)); 8663 GFP_KERNEL, cpu_to_node(i));
8513 if (!cfs_rq) 8664 if (!cfs_rq)
8514 goto err; 8665 goto err;
8515 8666
8516 se = kmalloc_node(sizeof(struct sched_entity), 8667 se = kzalloc_node(sizeof(struct sched_entity),
8517 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i)); 8668 GFP_KERNEL, cpu_to_node(i));
8518 if (!se) 8669 if (!se)
8519 goto err; 8670 goto err;
8520 8671
8521 parent_se = parent ? parent->se[i] : NULL; 8672 init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent->se[i]);
8522 init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent_se);
8523 } 8673 }
8524 8674
8525 return 1; 8675 return 1;
@@ -8580,7 +8730,7 @@ static
8580int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent) 8730int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
8581{ 8731{
8582 struct rt_rq *rt_rq; 8732 struct rt_rq *rt_rq;
8583 struct sched_rt_entity *rt_se, *parent_se; 8733 struct sched_rt_entity *rt_se;
8584 struct rq *rq; 8734 struct rq *rq;
8585 int i; 8735 int i;
8586 8736
@@ -8597,18 +8747,17 @@ int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
8597 for_each_possible_cpu(i) { 8747 for_each_possible_cpu(i) {
8598 rq = cpu_rq(i); 8748 rq = cpu_rq(i);
8599 8749
8600 rt_rq = kmalloc_node(sizeof(struct rt_rq), 8750 rt_rq = kzalloc_node(sizeof(struct rt_rq),
8601 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i)); 8751 GFP_KERNEL, cpu_to_node(i));
8602 if (!rt_rq) 8752 if (!rt_rq)
8603 goto err; 8753 goto err;
8604 8754
8605 rt_se = kmalloc_node(sizeof(struct sched_rt_entity), 8755 rt_se = kzalloc_node(sizeof(struct sched_rt_entity),
8606 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i)); 8756 GFP_KERNEL, cpu_to_node(i));
8607 if (!rt_se) 8757 if (!rt_se)
8608 goto err; 8758 goto err;
8609 8759
8610 parent_se = parent ? parent->rt_se[i] : NULL; 8760 init_tg_rt_entry(tg, rt_rq, rt_se, i, 0, parent->rt_se[i]);
8611 init_tg_rt_entry(tg, rt_rq, rt_se, i, 0, parent_se);
8612 } 8761 }
8613 8762
8614 return 1; 8763 return 1;
@@ -9251,11 +9400,12 @@ struct cgroup_subsys cpu_cgroup_subsys = {
9251 * (balbir@in.ibm.com). 9400 * (balbir@in.ibm.com).
9252 */ 9401 */
9253 9402
9254/* track cpu usage of a group of tasks */ 9403/* track cpu usage of a group of tasks and its child groups */
9255struct cpuacct { 9404struct cpuacct {
9256 struct cgroup_subsys_state css; 9405 struct cgroup_subsys_state css;
9257 /* cpuusage holds pointer to a u64-type object on every cpu */ 9406 /* cpuusage holds pointer to a u64-type object on every cpu */
9258 u64 *cpuusage; 9407 u64 *cpuusage;
9408 struct cpuacct *parent;
9259}; 9409};
9260 9410
9261struct cgroup_subsys cpuacct_subsys; 9411struct cgroup_subsys cpuacct_subsys;
@@ -9289,6 +9439,9 @@ static struct cgroup_subsys_state *cpuacct_create(
9289 return ERR_PTR(-ENOMEM); 9439 return ERR_PTR(-ENOMEM);
9290 } 9440 }
9291 9441
9442 if (cgrp->parent)
9443 ca->parent = cgroup_ca(cgrp->parent);
9444
9292 return &ca->css; 9445 return &ca->css;
9293} 9446}
9294 9447
@@ -9302,6 +9455,41 @@ cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
9302 kfree(ca); 9455 kfree(ca);
9303} 9456}
9304 9457
9458static u64 cpuacct_cpuusage_read(struct cpuacct *ca, int cpu)
9459{
9460 u64 *cpuusage = percpu_ptr(ca->cpuusage, cpu);
9461 u64 data;
9462
9463#ifndef CONFIG_64BIT
9464 /*
9465 * Take rq->lock to make 64-bit read safe on 32-bit platforms.
9466 */
9467 spin_lock_irq(&cpu_rq(cpu)->lock);
9468 data = *cpuusage;
9469 spin_unlock_irq(&cpu_rq(cpu)->lock);
9470#else
9471 data = *cpuusage;
9472#endif
9473
9474 return data;
9475}
9476
9477static void cpuacct_cpuusage_write(struct cpuacct *ca, int cpu, u64 val)
9478{
9479 u64 *cpuusage = percpu_ptr(ca->cpuusage, cpu);
9480
9481#ifndef CONFIG_64BIT
9482 /*
9483 * Take rq->lock to make 64-bit write safe on 32-bit platforms.
9484 */
9485 spin_lock_irq(&cpu_rq(cpu)->lock);
9486 *cpuusage = val;
9487 spin_unlock_irq(&cpu_rq(cpu)->lock);
9488#else
9489 *cpuusage = val;
9490#endif
9491}
9492
9305/* return total cpu usage (in nanoseconds) of a group */ 9493/* return total cpu usage (in nanoseconds) of a group */
9306static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft) 9494static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
9307{ 9495{
@@ -9309,17 +9497,8 @@ static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
9309 u64 totalcpuusage = 0; 9497 u64 totalcpuusage = 0;
9310 int i; 9498 int i;
9311 9499
9312 for_each_possible_cpu(i) { 9500 for_each_present_cpu(i)
9313 u64 *cpuusage = percpu_ptr(ca->cpuusage, i); 9501 totalcpuusage += cpuacct_cpuusage_read(ca, i);
9314
9315 /*
9316 * Take rq->lock to make 64-bit addition safe on 32-bit
9317 * platforms.
9318 */
9319 spin_lock_irq(&cpu_rq(i)->lock);
9320 totalcpuusage += *cpuusage;
9321 spin_unlock_irq(&cpu_rq(i)->lock);
9322 }
9323 9502
9324 return totalcpuusage; 9503 return totalcpuusage;
9325} 9504}
@@ -9336,23 +9515,39 @@ static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype,
9336 goto out; 9515 goto out;
9337 } 9516 }
9338 9517
9339 for_each_possible_cpu(i) { 9518 for_each_present_cpu(i)
9340 u64 *cpuusage = percpu_ptr(ca->cpuusage, i); 9519 cpuacct_cpuusage_write(ca, i, 0);
9341 9520
9342 spin_lock_irq(&cpu_rq(i)->lock);
9343 *cpuusage = 0;
9344 spin_unlock_irq(&cpu_rq(i)->lock);
9345 }
9346out: 9521out:
9347 return err; 9522 return err;
9348} 9523}
9349 9524
9525static int cpuacct_percpu_seq_read(struct cgroup *cgroup, struct cftype *cft,
9526 struct seq_file *m)
9527{
9528 struct cpuacct *ca = cgroup_ca(cgroup);
9529 u64 percpu;
9530 int i;
9531
9532 for_each_present_cpu(i) {
9533 percpu = cpuacct_cpuusage_read(ca, i);
9534 seq_printf(m, "%llu ", (unsigned long long) percpu);
9535 }
9536 seq_printf(m, "\n");
9537 return 0;
9538}
9539
9350static struct cftype files[] = { 9540static struct cftype files[] = {
9351 { 9541 {
9352 .name = "usage", 9542 .name = "usage",
9353 .read_u64 = cpuusage_read, 9543 .read_u64 = cpuusage_read,
9354 .write_u64 = cpuusage_write, 9544 .write_u64 = cpuusage_write,
9355 }, 9545 },
9546 {
9547 .name = "usage_percpu",
9548 .read_seq_string = cpuacct_percpu_seq_read,
9549 },
9550
9356}; 9551};
9357 9552
9358static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp) 9553static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
@@ -9368,14 +9563,16 @@ static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
9368static void cpuacct_charge(struct task_struct *tsk, u64 cputime) 9563static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
9369{ 9564{
9370 struct cpuacct *ca; 9565 struct cpuacct *ca;
9566 int cpu;
9371 9567
9372 if (!cpuacct_subsys.active) 9568 if (!cpuacct_subsys.active)
9373 return; 9569 return;
9374 9570
9571 cpu = task_cpu(tsk);
9375 ca = task_ca(tsk); 9572 ca = task_ca(tsk);
9376 if (ca) {
9377 u64 *cpuusage = percpu_ptr(ca->cpuusage, task_cpu(tsk));
9378 9573
9574 for (; ca; ca = ca->parent) {
9575 u64 *cpuusage = percpu_ptr(ca->cpuusage, cpu);
9379 *cpuusage += cputime; 9576 *cpuusage += cputime;
9380 } 9577 }
9381} 9578}