aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/sched.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/sched.c')
-rw-r--r--kernel/sched.c473
1 files changed, 394 insertions, 79 deletions
diff --git a/kernel/sched.c b/kernel/sched.c
index 26efa475bdc1..7c9098d186e6 100644
--- a/kernel/sched.c
+++ b/kernel/sched.c
@@ -39,6 +39,7 @@
39#include <linux/completion.h> 39#include <linux/completion.h>
40#include <linux/kernel_stat.h> 40#include <linux/kernel_stat.h>
41#include <linux/debug_locks.h> 41#include <linux/debug_locks.h>
42#include <linux/perf_counter.h>
42#include <linux/security.h> 43#include <linux/security.h>
43#include <linux/notifier.h> 44#include <linux/notifier.h>
44#include <linux/profile.h> 45#include <linux/profile.h>
@@ -68,17 +69,18 @@
68#include <linux/pagemap.h> 69#include <linux/pagemap.h>
69#include <linux/hrtimer.h> 70#include <linux/hrtimer.h>
70#include <linux/tick.h> 71#include <linux/tick.h>
71#include <linux/bootmem.h>
72#include <linux/debugfs.h> 72#include <linux/debugfs.h>
73#include <linux/ctype.h> 73#include <linux/ctype.h>
74#include <linux/ftrace.h> 74#include <linux/ftrace.h>
75#include <trace/sched.h>
76 75
77#include <asm/tlb.h> 76#include <asm/tlb.h>
78#include <asm/irq_regs.h> 77#include <asm/irq_regs.h>
79 78
80#include "sched_cpupri.h" 79#include "sched_cpupri.h"
81 80
81#define CREATE_TRACE_POINTS
82#include <trace/events/sched.h>
83
82/* 84/*
83 * Convert user-nice values [ -20 ... 0 ... 19 ] 85 * Convert user-nice values [ -20 ... 0 ... 19 ]
84 * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ], 86 * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
@@ -118,12 +120,6 @@
118 */ 120 */
119#define RUNTIME_INF ((u64)~0ULL) 121#define RUNTIME_INF ((u64)~0ULL)
120 122
121DEFINE_TRACE(sched_wait_task);
122DEFINE_TRACE(sched_wakeup);
123DEFINE_TRACE(sched_wakeup_new);
124DEFINE_TRACE(sched_switch);
125DEFINE_TRACE(sched_migrate_task);
126
127#ifdef CONFIG_SMP 123#ifdef CONFIG_SMP
128 124
129static void double_rq_lock(struct rq *rq1, struct rq *rq2); 125static void double_rq_lock(struct rq *rq1, struct rq *rq2);
@@ -244,7 +240,7 @@ static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
244 hard = hrtimer_get_expires(&rt_b->rt_period_timer); 240 hard = hrtimer_get_expires(&rt_b->rt_period_timer);
245 delta = ktime_to_ns(ktime_sub(hard, soft)); 241 delta = ktime_to_ns(ktime_sub(hard, soft));
246 __hrtimer_start_range_ns(&rt_b->rt_period_timer, soft, delta, 242 __hrtimer_start_range_ns(&rt_b->rt_period_timer, soft, delta,
247 HRTIMER_MODE_ABS, 0); 243 HRTIMER_MODE_ABS_PINNED, 0);
248 } 244 }
249 spin_unlock(&rt_b->rt_runtime_lock); 245 spin_unlock(&rt_b->rt_runtime_lock);
250} 246}
@@ -584,6 +580,7 @@ struct rq {
584 struct load_weight load; 580 struct load_weight load;
585 unsigned long nr_load_updates; 581 unsigned long nr_load_updates;
586 u64 nr_switches; 582 u64 nr_switches;
583 u64 nr_migrations_in;
587 584
588 struct cfs_rq cfs; 585 struct cfs_rq cfs;
589 struct rt_rq rt; 586 struct rt_rq rt;
@@ -630,6 +627,10 @@ struct rq {
630 struct list_head migration_queue; 627 struct list_head migration_queue;
631#endif 628#endif
632 629
630 /* calc_load related fields */
631 unsigned long calc_load_update;
632 long calc_load_active;
633
633#ifdef CONFIG_SCHED_HRTICK 634#ifdef CONFIG_SCHED_HRTICK
634#ifdef CONFIG_SMP 635#ifdef CONFIG_SMP
635 int hrtick_csd_pending; 636 int hrtick_csd_pending;
@@ -692,7 +693,7 @@ static inline int cpu_of(struct rq *rq)
692#define task_rq(p) cpu_rq(task_cpu(p)) 693#define task_rq(p) cpu_rq(task_cpu(p))
693#define cpu_curr(cpu) (cpu_rq(cpu)->curr) 694#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
694 695
695static inline void update_rq_clock(struct rq *rq) 696inline void update_rq_clock(struct rq *rq)
696{ 697{
697 rq->clock = sched_clock_cpu(cpu_of(rq)); 698 rq->clock = sched_clock_cpu(cpu_of(rq));
698} 699}
@@ -1154,7 +1155,7 @@ static __init void init_hrtick(void)
1154static void hrtick_start(struct rq *rq, u64 delay) 1155static void hrtick_start(struct rq *rq, u64 delay)
1155{ 1156{
1156 __hrtimer_start_range_ns(&rq->hrtick_timer, ns_to_ktime(delay), 0, 1157 __hrtimer_start_range_ns(&rq->hrtick_timer, ns_to_ktime(delay), 0,
1157 HRTIMER_MODE_REL, 0); 1158 HRTIMER_MODE_REL_PINNED, 0);
1158} 1159}
1159 1160
1160static inline void init_hrtick(void) 1161static inline void init_hrtick(void)
@@ -1728,6 +1729,8 @@ static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1728} 1729}
1729#endif 1730#endif
1730 1731
1732static void calc_load_account_active(struct rq *this_rq);
1733
1731#include "sched_stats.h" 1734#include "sched_stats.h"
1732#include "sched_idletask.c" 1735#include "sched_idletask.c"
1733#include "sched_fair.c" 1736#include "sched_fair.c"
@@ -1958,7 +1961,7 @@ void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
1958 1961
1959 clock_offset = old_rq->clock - new_rq->clock; 1962 clock_offset = old_rq->clock - new_rq->clock;
1960 1963
1961 trace_sched_migrate_task(p, task_cpu(p), new_cpu); 1964 trace_sched_migrate_task(p, new_cpu);
1962 1965
1963#ifdef CONFIG_SCHEDSTATS 1966#ifdef CONFIG_SCHEDSTATS
1964 if (p->se.wait_start) 1967 if (p->se.wait_start)
@@ -1967,12 +1970,17 @@ void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
1967 p->se.sleep_start -= clock_offset; 1970 p->se.sleep_start -= clock_offset;
1968 if (p->se.block_start) 1971 if (p->se.block_start)
1969 p->se.block_start -= clock_offset; 1972 p->se.block_start -= clock_offset;
1973#endif
1970 if (old_cpu != new_cpu) { 1974 if (old_cpu != new_cpu) {
1971 schedstat_inc(p, se.nr_migrations); 1975 p->se.nr_migrations++;
1976 new_rq->nr_migrations_in++;
1977#ifdef CONFIG_SCHEDSTATS
1972 if (task_hot(p, old_rq->clock, NULL)) 1978 if (task_hot(p, old_rq->clock, NULL))
1973 schedstat_inc(p, se.nr_forced2_migrations); 1979 schedstat_inc(p, se.nr_forced2_migrations);
1974 }
1975#endif 1980#endif
1981 perf_swcounter_event(PERF_COUNT_SW_CPU_MIGRATIONS,
1982 1, 1, NULL, 0);
1983 }
1976 p->se.vruntime -= old_cfsrq->min_vruntime - 1984 p->se.vruntime -= old_cfsrq->min_vruntime -
1977 new_cfsrq->min_vruntime; 1985 new_cfsrq->min_vruntime;
1978 1986
@@ -2015,6 +2023,49 @@ migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
2015} 2023}
2016 2024
2017/* 2025/*
2026 * wait_task_context_switch - wait for a thread to complete at least one
2027 * context switch.
2028 *
2029 * @p must not be current.
2030 */
2031void wait_task_context_switch(struct task_struct *p)
2032{
2033 unsigned long nvcsw, nivcsw, flags;
2034 int running;
2035 struct rq *rq;
2036
2037 nvcsw = p->nvcsw;
2038 nivcsw = p->nivcsw;
2039 for (;;) {
2040 /*
2041 * The runqueue is assigned before the actual context
2042 * switch. We need to take the runqueue lock.
2043 *
2044 * We could check initially without the lock but it is
2045 * very likely that we need to take the lock in every
2046 * iteration.
2047 */
2048 rq = task_rq_lock(p, &flags);
2049 running = task_running(rq, p);
2050 task_rq_unlock(rq, &flags);
2051
2052 if (likely(!running))
2053 break;
2054 /*
2055 * The switch count is incremented before the actual
2056 * context switch. We thus wait for two switches to be
2057 * sure at least one completed.
2058 */
2059 if ((p->nvcsw - nvcsw) > 1)
2060 break;
2061 if ((p->nivcsw - nivcsw) > 1)
2062 break;
2063
2064 cpu_relax();
2065 }
2066}
2067
2068/*
2018 * wait_task_inactive - wait for a thread to unschedule. 2069 * wait_task_inactive - wait for a thread to unschedule.
2019 * 2070 *
2020 * If @match_state is nonzero, it's the @p->state value just checked and 2071 * If @match_state is nonzero, it's the @p->state value just checked and
@@ -2142,6 +2193,7 @@ void kick_process(struct task_struct *p)
2142 smp_send_reschedule(cpu); 2193 smp_send_reschedule(cpu);
2143 preempt_enable(); 2194 preempt_enable();
2144} 2195}
2196EXPORT_SYMBOL_GPL(kick_process);
2145 2197
2146/* 2198/*
2147 * Return a low guess at the load of a migration-source cpu weighted 2199 * Return a low guess at the load of a migration-source cpu weighted
@@ -2324,6 +2376,27 @@ static int sched_balance_self(int cpu, int flag)
2324 2376
2325#endif /* CONFIG_SMP */ 2377#endif /* CONFIG_SMP */
2326 2378
2379/**
2380 * task_oncpu_function_call - call a function on the cpu on which a task runs
2381 * @p: the task to evaluate
2382 * @func: the function to be called
2383 * @info: the function call argument
2384 *
2385 * Calls the function @func when the task is currently running. This might
2386 * be on the current CPU, which just calls the function directly
2387 */
2388void task_oncpu_function_call(struct task_struct *p,
2389 void (*func) (void *info), void *info)
2390{
2391 int cpu;
2392
2393 preempt_disable();
2394 cpu = task_cpu(p);
2395 if (task_curr(p))
2396 smp_call_function_single(cpu, func, info, 1);
2397 preempt_enable();
2398}
2399
2327/*** 2400/***
2328 * try_to_wake_up - wake up a thread 2401 * try_to_wake_up - wake up a thread
2329 * @p: the to-be-woken-up thread 2402 * @p: the to-be-woken-up thread
@@ -2458,6 +2531,17 @@ out:
2458 return success; 2531 return success;
2459} 2532}
2460 2533
2534/**
2535 * wake_up_process - Wake up a specific process
2536 * @p: The process to be woken up.
2537 *
2538 * Attempt to wake up the nominated process and move it to the set of runnable
2539 * processes. Returns 1 if the process was woken up, 0 if it was already
2540 * running.
2541 *
2542 * It may be assumed that this function implies a write memory barrier before
2543 * changing the task state if and only if any tasks are woken up.
2544 */
2461int wake_up_process(struct task_struct *p) 2545int wake_up_process(struct task_struct *p)
2462{ 2546{
2463 return try_to_wake_up(p, TASK_ALL, 0); 2547 return try_to_wake_up(p, TASK_ALL, 0);
@@ -2480,6 +2564,7 @@ static void __sched_fork(struct task_struct *p)
2480 p->se.exec_start = 0; 2564 p->se.exec_start = 0;
2481 p->se.sum_exec_runtime = 0; 2565 p->se.sum_exec_runtime = 0;
2482 p->se.prev_sum_exec_runtime = 0; 2566 p->se.prev_sum_exec_runtime = 0;
2567 p->se.nr_migrations = 0;
2483 p->se.last_wakeup = 0; 2568 p->se.last_wakeup = 0;
2484 p->se.avg_overlap = 0; 2569 p->se.avg_overlap = 0;
2485 p->se.start_runtime = 0; 2570 p->se.start_runtime = 0;
@@ -2710,6 +2795,7 @@ static void finish_task_switch(struct rq *rq, struct task_struct *prev)
2710 */ 2795 */
2711 prev_state = prev->state; 2796 prev_state = prev->state;
2712 finish_arch_switch(prev); 2797 finish_arch_switch(prev);
2798 perf_counter_task_sched_in(current, cpu_of(rq));
2713 finish_lock_switch(rq, prev); 2799 finish_lock_switch(rq, prev);
2714#ifdef CONFIG_SMP 2800#ifdef CONFIG_SMP
2715 if (post_schedule) 2801 if (post_schedule)
@@ -2766,7 +2852,7 @@ context_switch(struct rq *rq, struct task_struct *prev,
2766 * combine the page table reload and the switch backend into 2852 * combine the page table reload and the switch backend into
2767 * one hypercall. 2853 * one hypercall.
2768 */ 2854 */
2769 arch_enter_lazy_cpu_mode(); 2855 arch_start_context_switch(prev);
2770 2856
2771 if (unlikely(!mm)) { 2857 if (unlikely(!mm)) {
2772 next->active_mm = oldmm; 2858 next->active_mm = oldmm;
@@ -2856,19 +2942,81 @@ unsigned long nr_iowait(void)
2856 return sum; 2942 return sum;
2857} 2943}
2858 2944
2859unsigned long nr_active(void) 2945/* Variables and functions for calc_load */
2946static atomic_long_t calc_load_tasks;
2947static unsigned long calc_load_update;
2948unsigned long avenrun[3];
2949EXPORT_SYMBOL(avenrun);
2950
2951/**
2952 * get_avenrun - get the load average array
2953 * @loads: pointer to dest load array
2954 * @offset: offset to add
2955 * @shift: shift count to shift the result left
2956 *
2957 * These values are estimates at best, so no need for locking.
2958 */
2959void get_avenrun(unsigned long *loads, unsigned long offset, int shift)
2860{ 2960{
2861 unsigned long i, running = 0, uninterruptible = 0; 2961 loads[0] = (avenrun[0] + offset) << shift;
2962 loads[1] = (avenrun[1] + offset) << shift;
2963 loads[2] = (avenrun[2] + offset) << shift;
2964}
2862 2965
2863 for_each_online_cpu(i) { 2966static unsigned long
2864 running += cpu_rq(i)->nr_running; 2967calc_load(unsigned long load, unsigned long exp, unsigned long active)
2865 uninterruptible += cpu_rq(i)->nr_uninterruptible; 2968{
2866 } 2969 load *= exp;
2970 load += active * (FIXED_1 - exp);
2971 return load >> FSHIFT;
2972}
2973
2974/*
2975 * calc_load - update the avenrun load estimates 10 ticks after the
2976 * CPUs have updated calc_load_tasks.
2977 */
2978void calc_global_load(void)
2979{
2980 unsigned long upd = calc_load_update + 10;
2981 long active;
2982
2983 if (time_before(jiffies, upd))
2984 return;
2985
2986 active = atomic_long_read(&calc_load_tasks);
2987 active = active > 0 ? active * FIXED_1 : 0;
2988
2989 avenrun[0] = calc_load(avenrun[0], EXP_1, active);
2990 avenrun[1] = calc_load(avenrun[1], EXP_5, active);
2991 avenrun[2] = calc_load(avenrun[2], EXP_15, active);
2992
2993 calc_load_update += LOAD_FREQ;
2994}
2995
2996/*
2997 * Either called from update_cpu_load() or from a cpu going idle
2998 */
2999static void calc_load_account_active(struct rq *this_rq)
3000{
3001 long nr_active, delta;
3002
3003 nr_active = this_rq->nr_running;
3004 nr_active += (long) this_rq->nr_uninterruptible;
2867 3005
2868 if (unlikely((long)uninterruptible < 0)) 3006 if (nr_active != this_rq->calc_load_active) {
2869 uninterruptible = 0; 3007 delta = nr_active - this_rq->calc_load_active;
3008 this_rq->calc_load_active = nr_active;
3009 atomic_long_add(delta, &calc_load_tasks);
3010 }
3011}
2870 3012
2871 return running + uninterruptible; 3013/*
3014 * Externally visible per-cpu scheduler statistics:
3015 * cpu_nr_migrations(cpu) - number of migrations into that cpu
3016 */
3017u64 cpu_nr_migrations(int cpu)
3018{
3019 return cpu_rq(cpu)->nr_migrations_in;
2872} 3020}
2873 3021
2874/* 3022/*
@@ -2899,6 +3047,11 @@ static void update_cpu_load(struct rq *this_rq)
2899 new_load += scale-1; 3047 new_load += scale-1;
2900 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i; 3048 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
2901 } 3049 }
3050
3051 if (time_after_eq(jiffies, this_rq->calc_load_update)) {
3052 this_rq->calc_load_update += LOAD_FREQ;
3053 calc_load_account_active(this_rq);
3054 }
2902} 3055}
2903 3056
2904#ifdef CONFIG_SMP 3057#ifdef CONFIG_SMP
@@ -4240,10 +4393,131 @@ static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
4240static struct { 4393static struct {
4241 atomic_t load_balancer; 4394 atomic_t load_balancer;
4242 cpumask_var_t cpu_mask; 4395 cpumask_var_t cpu_mask;
4396 cpumask_var_t ilb_grp_nohz_mask;
4243} nohz ____cacheline_aligned = { 4397} nohz ____cacheline_aligned = {
4244 .load_balancer = ATOMIC_INIT(-1), 4398 .load_balancer = ATOMIC_INIT(-1),
4245}; 4399};
4246 4400
4401int get_nohz_load_balancer(void)
4402{
4403 return atomic_read(&nohz.load_balancer);
4404}
4405
4406#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
4407/**
4408 * lowest_flag_domain - Return lowest sched_domain containing flag.
4409 * @cpu: The cpu whose lowest level of sched domain is to
4410 * be returned.
4411 * @flag: The flag to check for the lowest sched_domain
4412 * for the given cpu.
4413 *
4414 * Returns the lowest sched_domain of a cpu which contains the given flag.
4415 */
4416static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
4417{
4418 struct sched_domain *sd;
4419
4420 for_each_domain(cpu, sd)
4421 if (sd && (sd->flags & flag))
4422 break;
4423
4424 return sd;
4425}
4426
4427/**
4428 * for_each_flag_domain - Iterates over sched_domains containing the flag.
4429 * @cpu: The cpu whose domains we're iterating over.
4430 * @sd: variable holding the value of the power_savings_sd
4431 * for cpu.
4432 * @flag: The flag to filter the sched_domains to be iterated.
4433 *
4434 * Iterates over all the scheduler domains for a given cpu that has the 'flag'
4435 * set, starting from the lowest sched_domain to the highest.
4436 */
4437#define for_each_flag_domain(cpu, sd, flag) \
4438 for (sd = lowest_flag_domain(cpu, flag); \
4439 (sd && (sd->flags & flag)); sd = sd->parent)
4440
4441/**
4442 * is_semi_idle_group - Checks if the given sched_group is semi-idle.
4443 * @ilb_group: group to be checked for semi-idleness
4444 *
4445 * Returns: 1 if the group is semi-idle. 0 otherwise.
4446 *
4447 * We define a sched_group to be semi idle if it has atleast one idle-CPU
4448 * and atleast one non-idle CPU. This helper function checks if the given
4449 * sched_group is semi-idle or not.
4450 */
4451static inline int is_semi_idle_group(struct sched_group *ilb_group)
4452{
4453 cpumask_and(nohz.ilb_grp_nohz_mask, nohz.cpu_mask,
4454 sched_group_cpus(ilb_group));
4455
4456 /*
4457 * A sched_group is semi-idle when it has atleast one busy cpu
4458 * and atleast one idle cpu.
4459 */
4460 if (cpumask_empty(nohz.ilb_grp_nohz_mask))
4461 return 0;
4462
4463 if (cpumask_equal(nohz.ilb_grp_nohz_mask, sched_group_cpus(ilb_group)))
4464 return 0;
4465
4466 return 1;
4467}
4468/**
4469 * find_new_ilb - Finds the optimum idle load balancer for nomination.
4470 * @cpu: The cpu which is nominating a new idle_load_balancer.
4471 *
4472 * Returns: Returns the id of the idle load balancer if it exists,
4473 * Else, returns >= nr_cpu_ids.
4474 *
4475 * This algorithm picks the idle load balancer such that it belongs to a
4476 * semi-idle powersavings sched_domain. The idea is to try and avoid
4477 * completely idle packages/cores just for the purpose of idle load balancing
4478 * when there are other idle cpu's which are better suited for that job.
4479 */
4480static int find_new_ilb(int cpu)
4481{
4482 struct sched_domain *sd;
4483 struct sched_group *ilb_group;
4484
4485 /*
4486 * Have idle load balancer selection from semi-idle packages only
4487 * when power-aware load balancing is enabled
4488 */
4489 if (!(sched_smt_power_savings || sched_mc_power_savings))
4490 goto out_done;
4491
4492 /*
4493 * Optimize for the case when we have no idle CPUs or only one
4494 * idle CPU. Don't walk the sched_domain hierarchy in such cases
4495 */
4496 if (cpumask_weight(nohz.cpu_mask) < 2)
4497 goto out_done;
4498
4499 for_each_flag_domain(cpu, sd, SD_POWERSAVINGS_BALANCE) {
4500 ilb_group = sd->groups;
4501
4502 do {
4503 if (is_semi_idle_group(ilb_group))
4504 return cpumask_first(nohz.ilb_grp_nohz_mask);
4505
4506 ilb_group = ilb_group->next;
4507
4508 } while (ilb_group != sd->groups);
4509 }
4510
4511out_done:
4512 return cpumask_first(nohz.cpu_mask);
4513}
4514#else /* (CONFIG_SCHED_MC || CONFIG_SCHED_SMT) */
4515static inline int find_new_ilb(int call_cpu)
4516{
4517 return cpumask_first(nohz.cpu_mask);
4518}
4519#endif
4520
4247/* 4521/*
4248 * This routine will try to nominate the ilb (idle load balancing) 4522 * This routine will try to nominate the ilb (idle load balancing)
4249 * owner among the cpus whose ticks are stopped. ilb owner will do the idle 4523 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
@@ -4298,8 +4572,24 @@ int select_nohz_load_balancer(int stop_tick)
4298 /* make me the ilb owner */ 4572 /* make me the ilb owner */
4299 if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1) 4573 if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
4300 return 1; 4574 return 1;
4301 } else if (atomic_read(&nohz.load_balancer) == cpu) 4575 } else if (atomic_read(&nohz.load_balancer) == cpu) {
4576 int new_ilb;
4577
4578 if (!(sched_smt_power_savings ||
4579 sched_mc_power_savings))
4580 return 1;
4581 /*
4582 * Check to see if there is a more power-efficient
4583 * ilb.
4584 */
4585 new_ilb = find_new_ilb(cpu);
4586 if (new_ilb < nr_cpu_ids && new_ilb != cpu) {
4587 atomic_set(&nohz.load_balancer, -1);
4588 resched_cpu(new_ilb);
4589 return 0;
4590 }
4302 return 1; 4591 return 1;
4592 }
4303 } else { 4593 } else {
4304 if (!cpumask_test_cpu(cpu, nohz.cpu_mask)) 4594 if (!cpumask_test_cpu(cpu, nohz.cpu_mask))
4305 return 0; 4595 return 0;
@@ -4468,15 +4758,7 @@ static inline void trigger_load_balance(struct rq *rq, int cpu)
4468 } 4758 }
4469 4759
4470 if (atomic_read(&nohz.load_balancer) == -1) { 4760 if (atomic_read(&nohz.load_balancer) == -1) {
4471 /* 4761 int ilb = find_new_ilb(cpu);
4472 * simple selection for now: Nominate the
4473 * first cpu in the nohz list to be the next
4474 * ilb owner.
4475 *
4476 * TBD: Traverse the sched domains and nominate
4477 * the nearest cpu in the nohz.cpu_mask.
4478 */
4479 int ilb = cpumask_first(nohz.cpu_mask);
4480 4762
4481 if (ilb < nr_cpu_ids) 4763 if (ilb < nr_cpu_ids)
4482 resched_cpu(ilb); 4764 resched_cpu(ilb);
@@ -4840,6 +5122,8 @@ void scheduler_tick(void)
4840 curr->sched_class->task_tick(rq, curr, 0); 5122 curr->sched_class->task_tick(rq, curr, 0);
4841 spin_unlock(&rq->lock); 5123 spin_unlock(&rq->lock);
4842 5124
5125 perf_counter_task_tick(curr, cpu);
5126
4843#ifdef CONFIG_SMP 5127#ifdef CONFIG_SMP
4844 rq->idle_at_tick = idle_cpu(cpu); 5128 rq->idle_at_tick = idle_cpu(cpu);
4845 trigger_load_balance(rq, cpu); 5129 trigger_load_balance(rq, cpu);
@@ -5007,13 +5291,15 @@ pick_next_task(struct rq *rq)
5007/* 5291/*
5008 * schedule() is the main scheduler function. 5292 * schedule() is the main scheduler function.
5009 */ 5293 */
5010asmlinkage void __sched __schedule(void) 5294asmlinkage void __sched schedule(void)
5011{ 5295{
5012 struct task_struct *prev, *next; 5296 struct task_struct *prev, *next;
5013 unsigned long *switch_count; 5297 unsigned long *switch_count;
5014 struct rq *rq; 5298 struct rq *rq;
5015 int cpu; 5299 int cpu;
5016 5300
5301need_resched:
5302 preempt_disable();
5017 cpu = smp_processor_id(); 5303 cpu = smp_processor_id();
5018 rq = cpu_rq(cpu); 5304 rq = cpu_rq(cpu);
5019 rcu_qsctr_inc(cpu); 5305 rcu_qsctr_inc(cpu);
@@ -5053,6 +5339,7 @@ need_resched_nonpreemptible:
5053 5339
5054 if (likely(prev != next)) { 5340 if (likely(prev != next)) {
5055 sched_info_switch(prev, next); 5341 sched_info_switch(prev, next);
5342 perf_counter_task_sched_out(prev, next, cpu);
5056 5343
5057 rq->nr_switches++; 5344 rq->nr_switches++;
5058 rq->curr = next; 5345 rq->curr = next;
@@ -5070,15 +5357,9 @@ need_resched_nonpreemptible:
5070 5357
5071 if (unlikely(reacquire_kernel_lock(current) < 0)) 5358 if (unlikely(reacquire_kernel_lock(current) < 0))
5072 goto need_resched_nonpreemptible; 5359 goto need_resched_nonpreemptible;
5073}
5074 5360
5075asmlinkage void __sched schedule(void)
5076{
5077need_resched:
5078 preempt_disable();
5079 __schedule();
5080 preempt_enable_no_resched(); 5361 preempt_enable_no_resched();
5081 if (unlikely(test_thread_flag(TIF_NEED_RESCHED))) 5362 if (need_resched())
5082 goto need_resched; 5363 goto need_resched;
5083} 5364}
5084EXPORT_SYMBOL(schedule); 5365EXPORT_SYMBOL(schedule);
@@ -5221,7 +5502,7 @@ EXPORT_SYMBOL(default_wake_function);
5221 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns 5502 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
5222 * zero in this (rare) case, and we handle it by continuing to scan the queue. 5503 * zero in this (rare) case, and we handle it by continuing to scan the queue.
5223 */ 5504 */
5224void __wake_up_common(wait_queue_head_t *q, unsigned int mode, 5505static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
5225 int nr_exclusive, int sync, void *key) 5506 int nr_exclusive, int sync, void *key)
5226{ 5507{
5227 wait_queue_t *curr, *next; 5508 wait_queue_t *curr, *next;
@@ -5241,6 +5522,9 @@ void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
5241 * @mode: which threads 5522 * @mode: which threads
5242 * @nr_exclusive: how many wake-one or wake-many threads to wake up 5523 * @nr_exclusive: how many wake-one or wake-many threads to wake up
5243 * @key: is directly passed to the wakeup function 5524 * @key: is directly passed to the wakeup function
5525 *
5526 * It may be assumed that this function implies a write memory barrier before
5527 * changing the task state if and only if any tasks are woken up.
5244 */ 5528 */
5245void __wake_up(wait_queue_head_t *q, unsigned int mode, 5529void __wake_up(wait_queue_head_t *q, unsigned int mode,
5246 int nr_exclusive, void *key) 5530 int nr_exclusive, void *key)
@@ -5279,6 +5563,9 @@ void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key)
5279 * with each other. This can prevent needless bouncing between CPUs. 5563 * with each other. This can prevent needless bouncing between CPUs.
5280 * 5564 *
5281 * On UP it can prevent extra preemption. 5565 * On UP it can prevent extra preemption.
5566 *
5567 * It may be assumed that this function implies a write memory barrier before
5568 * changing the task state if and only if any tasks are woken up.
5282 */ 5569 */
5283void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode, 5570void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode,
5284 int nr_exclusive, void *key) 5571 int nr_exclusive, void *key)
@@ -5315,6 +5602,9 @@ EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
5315 * awakened in the same order in which they were queued. 5602 * awakened in the same order in which they were queued.
5316 * 5603 *
5317 * See also complete_all(), wait_for_completion() and related routines. 5604 * See also complete_all(), wait_for_completion() and related routines.
5605 *
5606 * It may be assumed that this function implies a write memory barrier before
5607 * changing the task state if and only if any tasks are woken up.
5318 */ 5608 */
5319void complete(struct completion *x) 5609void complete(struct completion *x)
5320{ 5610{
@@ -5332,6 +5622,9 @@ EXPORT_SYMBOL(complete);
5332 * @x: holds the state of this particular completion 5622 * @x: holds the state of this particular completion
5333 * 5623 *
5334 * This will wake up all threads waiting on this particular completion event. 5624 * This will wake up all threads waiting on this particular completion event.
5625 *
5626 * It may be assumed that this function implies a write memory barrier before
5627 * changing the task state if and only if any tasks are woken up.
5335 */ 5628 */
5336void complete_all(struct completion *x) 5629void complete_all(struct completion *x)
5337{ 5630{
@@ -6490,8 +6783,9 @@ void sched_show_task(struct task_struct *p)
6490#ifdef CONFIG_DEBUG_STACK_USAGE 6783#ifdef CONFIG_DEBUG_STACK_USAGE
6491 free = stack_not_used(p); 6784 free = stack_not_used(p);
6492#endif 6785#endif
6493 printk(KERN_CONT "%5lu %5d %6d\n", free, 6786 printk(KERN_CONT "%5lu %5d %6d 0x%08lx\n", free,
6494 task_pid_nr(p), task_pid_nr(p->real_parent)); 6787 task_pid_nr(p), task_pid_nr(p->real_parent),
6788 (unsigned long)task_thread_info(p)->flags);
6495 6789
6496 show_stack(p, NULL); 6790 show_stack(p, NULL);
6497} 6791}
@@ -6752,7 +7046,7 @@ static int migration_thread(void *data)
6752 7046
6753 if (cpu_is_offline(cpu)) { 7047 if (cpu_is_offline(cpu)) {
6754 spin_unlock_irq(&rq->lock); 7048 spin_unlock_irq(&rq->lock);
6755 goto wait_to_die; 7049 break;
6756 } 7050 }
6757 7051
6758 if (rq->active_balance) { 7052 if (rq->active_balance) {
@@ -6778,16 +7072,7 @@ static int migration_thread(void *data)
6778 complete(&req->done); 7072 complete(&req->done);
6779 } 7073 }
6780 __set_current_state(TASK_RUNNING); 7074 __set_current_state(TASK_RUNNING);
6781 return 0;
6782 7075
6783wait_to_die:
6784 /* Wait for kthread_stop */
6785 set_current_state(TASK_INTERRUPTIBLE);
6786 while (!kthread_should_stop()) {
6787 schedule();
6788 set_current_state(TASK_INTERRUPTIBLE);
6789 }
6790 __set_current_state(TASK_RUNNING);
6791 return 0; 7076 return 0;
6792} 7077}
6793 7078
@@ -6970,6 +7255,14 @@ static void migrate_dead_tasks(unsigned int dead_cpu)
6970 7255
6971 } 7256 }
6972} 7257}
7258
7259/*
7260 * remove the tasks which were accounted by rq from calc_load_tasks.
7261 */
7262static void calc_global_load_remove(struct rq *rq)
7263{
7264 atomic_long_sub(rq->calc_load_active, &calc_load_tasks);
7265}
6973#endif /* CONFIG_HOTPLUG_CPU */ 7266#endif /* CONFIG_HOTPLUG_CPU */
6974 7267
6975#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL) 7268#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
@@ -7193,6 +7486,7 @@ migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
7193 rq = task_rq_lock(p, &flags); 7486 rq = task_rq_lock(p, &flags);
7194 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1); 7487 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
7195 task_rq_unlock(rq, &flags); 7488 task_rq_unlock(rq, &flags);
7489 get_task_struct(p);
7196 cpu_rq(cpu)->migration_thread = p; 7490 cpu_rq(cpu)->migration_thread = p;
7197 break; 7491 break;
7198 7492
@@ -7204,6 +7498,8 @@ migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
7204 /* Update our root-domain */ 7498 /* Update our root-domain */
7205 rq = cpu_rq(cpu); 7499 rq = cpu_rq(cpu);
7206 spin_lock_irqsave(&rq->lock, flags); 7500 spin_lock_irqsave(&rq->lock, flags);
7501 rq->calc_load_update = calc_load_update;
7502 rq->calc_load_active = 0;
7207 if (rq->rd) { 7503 if (rq->rd) {
7208 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span)); 7504 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
7209 7505
@@ -7221,6 +7517,7 @@ migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
7221 kthread_bind(cpu_rq(cpu)->migration_thread, 7517 kthread_bind(cpu_rq(cpu)->migration_thread,
7222 cpumask_any(cpu_online_mask)); 7518 cpumask_any(cpu_online_mask));
7223 kthread_stop(cpu_rq(cpu)->migration_thread); 7519 kthread_stop(cpu_rq(cpu)->migration_thread);
7520 put_task_struct(cpu_rq(cpu)->migration_thread);
7224 cpu_rq(cpu)->migration_thread = NULL; 7521 cpu_rq(cpu)->migration_thread = NULL;
7225 break; 7522 break;
7226 7523
@@ -7230,6 +7527,7 @@ migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
7230 migrate_live_tasks(cpu); 7527 migrate_live_tasks(cpu);
7231 rq = cpu_rq(cpu); 7528 rq = cpu_rq(cpu);
7232 kthread_stop(rq->migration_thread); 7529 kthread_stop(rq->migration_thread);
7530 put_task_struct(rq->migration_thread);
7233 rq->migration_thread = NULL; 7531 rq->migration_thread = NULL;
7234 /* Idle task back to normal (off runqueue, low prio) */ 7532 /* Idle task back to normal (off runqueue, low prio) */
7235 spin_lock_irq(&rq->lock); 7533 spin_lock_irq(&rq->lock);
@@ -7243,7 +7541,7 @@ migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
7243 cpuset_unlock(); 7541 cpuset_unlock();
7244 migrate_nr_uninterruptible(rq); 7542 migrate_nr_uninterruptible(rq);
7245 BUG_ON(rq->nr_running != 0); 7543 BUG_ON(rq->nr_running != 0);
7246 7544 calc_global_load_remove(rq);
7247 /* 7545 /*
7248 * No need to migrate the tasks: it was best-effort if 7546 * No need to migrate the tasks: it was best-effort if
7249 * they didn't take sched_hotcpu_mutex. Just wake up 7547 * they didn't take sched_hotcpu_mutex. Just wake up
@@ -7279,8 +7577,10 @@ migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
7279 return NOTIFY_OK; 7577 return NOTIFY_OK;
7280} 7578}
7281 7579
7282/* Register at highest priority so that task migration (migrate_all_tasks) 7580/*
7283 * happens before everything else. 7581 * Register at high priority so that task migration (migrate_all_tasks)
7582 * happens before everything else. This has to be lower priority than
7583 * the notifier in the perf_counter subsystem, though.
7284 */ 7584 */
7285static struct notifier_block __cpuinitdata migration_notifier = { 7585static struct notifier_block __cpuinitdata migration_notifier = {
7286 .notifier_call = migration_call, 7586 .notifier_call = migration_call,
@@ -7523,26 +7823,23 @@ static void rq_attach_root(struct rq *rq, struct root_domain *rd)
7523 free_rootdomain(old_rd); 7823 free_rootdomain(old_rd);
7524} 7824}
7525 7825
7526static int __init_refok init_rootdomain(struct root_domain *rd, bool bootmem) 7826static int init_rootdomain(struct root_domain *rd, bool bootmem)
7527{ 7827{
7828 gfp_t gfp = GFP_KERNEL;
7829
7528 memset(rd, 0, sizeof(*rd)); 7830 memset(rd, 0, sizeof(*rd));
7529 7831
7530 if (bootmem) { 7832 if (bootmem)
7531 alloc_bootmem_cpumask_var(&def_root_domain.span); 7833 gfp = GFP_NOWAIT;
7532 alloc_bootmem_cpumask_var(&def_root_domain.online);
7533 alloc_bootmem_cpumask_var(&def_root_domain.rto_mask);
7534 cpupri_init(&rd->cpupri, true);
7535 return 0;
7536 }
7537 7834
7538 if (!alloc_cpumask_var(&rd->span, GFP_KERNEL)) 7835 if (!alloc_cpumask_var(&rd->span, gfp))
7539 goto out; 7836 goto out;
7540 if (!alloc_cpumask_var(&rd->online, GFP_KERNEL)) 7837 if (!alloc_cpumask_var(&rd->online, gfp))
7541 goto free_span; 7838 goto free_span;
7542 if (!alloc_cpumask_var(&rd->rto_mask, GFP_KERNEL)) 7839 if (!alloc_cpumask_var(&rd->rto_mask, gfp))
7543 goto free_online; 7840 goto free_online;
7544 7841
7545 if (cpupri_init(&rd->cpupri, false) != 0) 7842 if (cpupri_init(&rd->cpupri, bootmem) != 0)
7546 goto free_rto_mask; 7843 goto free_rto_mask;
7547 return 0; 7844 return 0;
7548 7845
@@ -7753,8 +8050,9 @@ int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
7753 8050
7754/* 8051/*
7755 * The cpus mask in sched_group and sched_domain hangs off the end. 8052 * The cpus mask in sched_group and sched_domain hangs off the end.
7756 * FIXME: use cpumask_var_t or dynamic percpu alloc to avoid wasting space 8053 *
7757 * for nr_cpu_ids < CONFIG_NR_CPUS. 8054 * ( See the the comments in include/linux/sched.h:struct sched_group
8055 * and struct sched_domain. )
7758 */ 8056 */
7759struct static_sched_group { 8057struct static_sched_group {
7760 struct sched_group sg; 8058 struct sched_group sg;
@@ -7875,7 +8173,7 @@ static void init_numa_sched_groups_power(struct sched_group *group_head)
7875 struct sched_domain *sd; 8173 struct sched_domain *sd;
7876 8174
7877 sd = &per_cpu(phys_domains, j).sd; 8175 sd = &per_cpu(phys_domains, j).sd;
7878 if (j != cpumask_first(sched_group_cpus(sd->groups))) { 8176 if (j != group_first_cpu(sd->groups)) {
7879 /* 8177 /*
7880 * Only add "power" once for each 8178 * Only add "power" once for each
7881 * physical package. 8179 * physical package.
@@ -7953,7 +8251,7 @@ static void init_sched_groups_power(int cpu, struct sched_domain *sd)
7953 8251
7954 WARN_ON(!sd || !sd->groups); 8252 WARN_ON(!sd || !sd->groups);
7955 8253
7956 if (cpu != cpumask_first(sched_group_cpus(sd->groups))) 8254 if (cpu != group_first_cpu(sd->groups))
7957 return; 8255 return;
7958 8256
7959 child = sd->child; 8257 child = sd->child;
@@ -8731,6 +9029,8 @@ void __init sched_init_smp(void)
8731} 9029}
8732#endif /* CONFIG_SMP */ 9030#endif /* CONFIG_SMP */
8733 9031
9032const_debug unsigned int sysctl_timer_migration = 1;
9033
8734int in_sched_functions(unsigned long addr) 9034int in_sched_functions(unsigned long addr)
8735{ 9035{
8736 return in_lock_functions(addr) || 9036 return in_lock_functions(addr) ||
@@ -8865,7 +9165,7 @@ void __init sched_init(void)
8865 * we use alloc_bootmem(). 9165 * we use alloc_bootmem().
8866 */ 9166 */
8867 if (alloc_size) { 9167 if (alloc_size) {
8868 ptr = (unsigned long)alloc_bootmem(alloc_size); 9168 ptr = (unsigned long)kzalloc(alloc_size, GFP_NOWAIT);
8869 9169
8870#ifdef CONFIG_FAIR_GROUP_SCHED 9170#ifdef CONFIG_FAIR_GROUP_SCHED
8871 init_task_group.se = (struct sched_entity **)ptr; 9171 init_task_group.se = (struct sched_entity **)ptr;
@@ -8938,6 +9238,8 @@ void __init sched_init(void)
8938 rq = cpu_rq(i); 9238 rq = cpu_rq(i);
8939 spin_lock_init(&rq->lock); 9239 spin_lock_init(&rq->lock);
8940 rq->nr_running = 0; 9240 rq->nr_running = 0;
9241 rq->calc_load_active = 0;
9242 rq->calc_load_update = jiffies + LOAD_FREQ;
8941 init_cfs_rq(&rq->cfs, rq); 9243 init_cfs_rq(&rq->cfs, rq);
8942 init_rt_rq(&rq->rt, rq); 9244 init_rt_rq(&rq->rt, rq);
8943#ifdef CONFIG_FAIR_GROUP_SCHED 9245#ifdef CONFIG_FAIR_GROUP_SCHED
@@ -8958,7 +9260,7 @@ void __init sched_init(void)
8958 * 1024) and two child groups A0 and A1 (of weight 1024 each), 9260 * 1024) and two child groups A0 and A1 (of weight 1024 each),
8959 * then A0's share of the cpu resource is: 9261 * then A0's share of the cpu resource is:
8960 * 9262 *
8961 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33% 9263 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
8962 * 9264 *
8963 * We achieve this by letting init_task_group's tasks sit 9265 * We achieve this by letting init_task_group's tasks sit
8964 * directly in rq->cfs (i.e init_task_group->se[] = NULL). 9266 * directly in rq->cfs (i.e init_task_group->se[] = NULL).
@@ -9045,20 +9347,26 @@ void __init sched_init(void)
9045 * when this runqueue becomes "idle". 9347 * when this runqueue becomes "idle".
9046 */ 9348 */
9047 init_idle(current, smp_processor_id()); 9349 init_idle(current, smp_processor_id());
9350
9351 calc_load_update = jiffies + LOAD_FREQ;
9352
9048 /* 9353 /*
9049 * During early bootup we pretend to be a normal task: 9354 * During early bootup we pretend to be a normal task:
9050 */ 9355 */
9051 current->sched_class = &fair_sched_class; 9356 current->sched_class = &fair_sched_class;
9052 9357
9053 /* Allocate the nohz_cpu_mask if CONFIG_CPUMASK_OFFSTACK */ 9358 /* Allocate the nohz_cpu_mask if CONFIG_CPUMASK_OFFSTACK */
9054 alloc_bootmem_cpumask_var(&nohz_cpu_mask); 9359 alloc_cpumask_var(&nohz_cpu_mask, GFP_NOWAIT);
9055#ifdef CONFIG_SMP 9360#ifdef CONFIG_SMP
9056#ifdef CONFIG_NO_HZ 9361#ifdef CONFIG_NO_HZ
9057 alloc_bootmem_cpumask_var(&nohz.cpu_mask); 9362 alloc_cpumask_var(&nohz.cpu_mask, GFP_NOWAIT);
9363 alloc_cpumask_var(&nohz.ilb_grp_nohz_mask, GFP_NOWAIT);
9058#endif 9364#endif
9059 alloc_bootmem_cpumask_var(&cpu_isolated_map); 9365 alloc_cpumask_var(&cpu_isolated_map, GFP_NOWAIT);
9060#endif /* SMP */ 9366#endif /* SMP */
9061 9367
9368 perf_counter_init();
9369
9062 scheduler_running = 1; 9370 scheduler_running = 1;
9063} 9371}
9064 9372
@@ -9800,6 +10108,13 @@ static int sched_rt_global_constraints(void)
9800 if (sysctl_sched_rt_period <= 0) 10108 if (sysctl_sched_rt_period <= 0)
9801 return -EINVAL; 10109 return -EINVAL;
9802 10110
10111 /*
10112 * There's always some RT tasks in the root group
10113 * -- migration, kstopmachine etc..
10114 */
10115 if (sysctl_sched_rt_runtime == 0)
10116 return -EBUSY;
10117
9803 spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags); 10118 spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
9804 for_each_possible_cpu(i) { 10119 for_each_possible_cpu(i) {
9805 struct rt_rq *rt_rq = &cpu_rq(i)->rt; 10120 struct rt_rq *rt_rq = &cpu_rq(i)->rt;