aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/sched.c
diff options
context:
space:
mode:
authorRobert Richter <robert.richter@amd.com>2010-06-04 05:33:10 -0400
committerRobert Richter <robert.richter@amd.com>2010-06-04 05:33:10 -0400
commitd8a382d2662822248a97ce9d670b90e68aefbd3a (patch)
tree4f5bbd5d0a5881ed42de611402ea4ac2c6d6ff48 /kernel/sched.c
parent45c34e05c4e3d36e7c44e790241ea11a1d90d54e (diff)
parentc6df8d5ab87a246942d138321e1721edbb69f6e1 (diff)
Merge remote branch 'tip/perf/urgent' into oprofile/urgent
Diffstat (limited to 'kernel/sched.c')
-rw-r--r--kernel/sched.c804
1 files changed, 287 insertions, 517 deletions
diff --git a/kernel/sched.c b/kernel/sched.c
index 3c2a54f70ffe..d48408142503 100644
--- a/kernel/sched.c
+++ b/kernel/sched.c
@@ -55,9 +55,9 @@
55#include <linux/cpu.h> 55#include <linux/cpu.h>
56#include <linux/cpuset.h> 56#include <linux/cpuset.h>
57#include <linux/percpu.h> 57#include <linux/percpu.h>
58#include <linux/kthread.h>
59#include <linux/proc_fs.h> 58#include <linux/proc_fs.h>
60#include <linux/seq_file.h> 59#include <linux/seq_file.h>
60#include <linux/stop_machine.h>
61#include <linux/sysctl.h> 61#include <linux/sysctl.h>
62#include <linux/syscalls.h> 62#include <linux/syscalls.h>
63#include <linux/times.h> 63#include <linux/times.h>
@@ -503,8 +503,11 @@ struct rq {
503 #define CPU_LOAD_IDX_MAX 5 503 #define CPU_LOAD_IDX_MAX 5
504 unsigned long cpu_load[CPU_LOAD_IDX_MAX]; 504 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
505#ifdef CONFIG_NO_HZ 505#ifdef CONFIG_NO_HZ
506 u64 nohz_stamp;
506 unsigned char in_nohz_recently; 507 unsigned char in_nohz_recently;
507#endif 508#endif
509 unsigned int skip_clock_update;
510
508 /* capture load from *all* tasks on this cpu: */ 511 /* capture load from *all* tasks on this cpu: */
509 struct load_weight load; 512 struct load_weight load;
510 unsigned long nr_load_updates; 513 unsigned long nr_load_updates;
@@ -546,15 +549,13 @@ struct rq {
546 int post_schedule; 549 int post_schedule;
547 int active_balance; 550 int active_balance;
548 int push_cpu; 551 int push_cpu;
552 struct cpu_stop_work active_balance_work;
549 /* cpu of this runqueue: */ 553 /* cpu of this runqueue: */
550 int cpu; 554 int cpu;
551 int online; 555 int online;
552 556
553 unsigned long avg_load_per_task; 557 unsigned long avg_load_per_task;
554 558
555 struct task_struct *migration_thread;
556 struct list_head migration_queue;
557
558 u64 rt_avg; 559 u64 rt_avg;
559 u64 age_stamp; 560 u64 age_stamp;
560 u64 idle_stamp; 561 u64 idle_stamp;
@@ -602,6 +603,13 @@ static inline
602void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags) 603void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags)
603{ 604{
604 rq->curr->sched_class->check_preempt_curr(rq, p, flags); 605 rq->curr->sched_class->check_preempt_curr(rq, p, flags);
606
607 /*
608 * A queue event has occurred, and we're going to schedule. In
609 * this case, we can save a useless back to back clock update.
610 */
611 if (test_tsk_need_resched(p))
612 rq->skip_clock_update = 1;
605} 613}
606 614
607static inline int cpu_of(struct rq *rq) 615static inline int cpu_of(struct rq *rq)
@@ -636,7 +644,8 @@ static inline int cpu_of(struct rq *rq)
636 644
637inline void update_rq_clock(struct rq *rq) 645inline void update_rq_clock(struct rq *rq)
638{ 646{
639 rq->clock = sched_clock_cpu(cpu_of(rq)); 647 if (!rq->skip_clock_update)
648 rq->clock = sched_clock_cpu(cpu_of(rq));
640} 649}
641 650
642/* 651/*
@@ -914,16 +923,12 @@ static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
914#endif /* __ARCH_WANT_UNLOCKED_CTXSW */ 923#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
915 924
916/* 925/*
917 * Check whether the task is waking, we use this to synchronize against 926 * Check whether the task is waking, we use this to synchronize ->cpus_allowed
918 * ttwu() so that task_cpu() reports a stable number. 927 * against ttwu().
919 *
920 * We need to make an exception for PF_STARTING tasks because the fork
921 * path might require task_rq_lock() to work, eg. it can call
922 * set_cpus_allowed_ptr() from the cpuset clone_ns code.
923 */ 928 */
924static inline int task_is_waking(struct task_struct *p) 929static inline int task_is_waking(struct task_struct *p)
925{ 930{
926 return unlikely((p->state == TASK_WAKING) && !(p->flags & PF_STARTING)); 931 return unlikely(p->state == TASK_WAKING);
927} 932}
928 933
929/* 934/*
@@ -936,11 +941,9 @@ static inline struct rq *__task_rq_lock(struct task_struct *p)
936 struct rq *rq; 941 struct rq *rq;
937 942
938 for (;;) { 943 for (;;) {
939 while (task_is_waking(p))
940 cpu_relax();
941 rq = task_rq(p); 944 rq = task_rq(p);
942 raw_spin_lock(&rq->lock); 945 raw_spin_lock(&rq->lock);
943 if (likely(rq == task_rq(p) && !task_is_waking(p))) 946 if (likely(rq == task_rq(p)))
944 return rq; 947 return rq;
945 raw_spin_unlock(&rq->lock); 948 raw_spin_unlock(&rq->lock);
946 } 949 }
@@ -957,25 +960,15 @@ static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
957 struct rq *rq; 960 struct rq *rq;
958 961
959 for (;;) { 962 for (;;) {
960 while (task_is_waking(p))
961 cpu_relax();
962 local_irq_save(*flags); 963 local_irq_save(*flags);
963 rq = task_rq(p); 964 rq = task_rq(p);
964 raw_spin_lock(&rq->lock); 965 raw_spin_lock(&rq->lock);
965 if (likely(rq == task_rq(p) && !task_is_waking(p))) 966 if (likely(rq == task_rq(p)))
966 return rq; 967 return rq;
967 raw_spin_unlock_irqrestore(&rq->lock, *flags); 968 raw_spin_unlock_irqrestore(&rq->lock, *flags);
968 } 969 }
969} 970}
970 971
971void task_rq_unlock_wait(struct task_struct *p)
972{
973 struct rq *rq = task_rq(p);
974
975 smp_mb(); /* spin-unlock-wait is not a full memory barrier */
976 raw_spin_unlock_wait(&rq->lock);
977}
978
979static void __task_rq_unlock(struct rq *rq) 972static void __task_rq_unlock(struct rq *rq)
980 __releases(rq->lock) 973 __releases(rq->lock)
981{ 974{
@@ -1239,6 +1232,17 @@ void wake_up_idle_cpu(int cpu)
1239 if (!tsk_is_polling(rq->idle)) 1232 if (!tsk_is_polling(rq->idle))
1240 smp_send_reschedule(cpu); 1233 smp_send_reschedule(cpu);
1241} 1234}
1235
1236int nohz_ratelimit(int cpu)
1237{
1238 struct rq *rq = cpu_rq(cpu);
1239 u64 diff = rq->clock - rq->nohz_stamp;
1240
1241 rq->nohz_stamp = rq->clock;
1242
1243 return diff < (NSEC_PER_SEC / HZ) >> 1;
1244}
1245
1242#endif /* CONFIG_NO_HZ */ 1246#endif /* CONFIG_NO_HZ */
1243 1247
1244static u64 sched_avg_period(void) 1248static u64 sched_avg_period(void)
@@ -1781,8 +1785,6 @@ static void double_rq_lock(struct rq *rq1, struct rq *rq2)
1781 raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING); 1785 raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
1782 } 1786 }
1783 } 1787 }
1784 update_rq_clock(rq1);
1785 update_rq_clock(rq2);
1786} 1788}
1787 1789
1788/* 1790/*
@@ -1813,7 +1815,7 @@ static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1813} 1815}
1814#endif 1816#endif
1815 1817
1816static void calc_load_account_active(struct rq *this_rq); 1818static void calc_load_account_idle(struct rq *this_rq);
1817static void update_sysctl(void); 1819static void update_sysctl(void);
1818static int get_update_sysctl_factor(void); 1820static int get_update_sysctl_factor(void);
1819 1821
@@ -1870,62 +1872,43 @@ static void set_load_weight(struct task_struct *p)
1870 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO]; 1872 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
1871} 1873}
1872 1874
1873static void update_avg(u64 *avg, u64 sample) 1875static void enqueue_task(struct rq *rq, struct task_struct *p, int flags)
1874{
1875 s64 diff = sample - *avg;
1876 *avg += diff >> 3;
1877}
1878
1879static void
1880enqueue_task(struct rq *rq, struct task_struct *p, int wakeup, bool head)
1881{ 1876{
1882 if (wakeup) 1877 update_rq_clock(rq);
1883 p->se.start_runtime = p->se.sum_exec_runtime;
1884
1885 sched_info_queued(p); 1878 sched_info_queued(p);
1886 p->sched_class->enqueue_task(rq, p, wakeup, head); 1879 p->sched_class->enqueue_task(rq, p, flags);
1887 p->se.on_rq = 1; 1880 p->se.on_rq = 1;
1888} 1881}
1889 1882
1890static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep) 1883static void dequeue_task(struct rq *rq, struct task_struct *p, int flags)
1891{ 1884{
1892 if (sleep) { 1885 update_rq_clock(rq);
1893 if (p->se.last_wakeup) {
1894 update_avg(&p->se.avg_overlap,
1895 p->se.sum_exec_runtime - p->se.last_wakeup);
1896 p->se.last_wakeup = 0;
1897 } else {
1898 update_avg(&p->se.avg_wakeup,
1899 sysctl_sched_wakeup_granularity);
1900 }
1901 }
1902
1903 sched_info_dequeued(p); 1886 sched_info_dequeued(p);
1904 p->sched_class->dequeue_task(rq, p, sleep); 1887 p->sched_class->dequeue_task(rq, p, flags);
1905 p->se.on_rq = 0; 1888 p->se.on_rq = 0;
1906} 1889}
1907 1890
1908/* 1891/*
1909 * activate_task - move a task to the runqueue. 1892 * activate_task - move a task to the runqueue.
1910 */ 1893 */
1911static void activate_task(struct rq *rq, struct task_struct *p, int wakeup) 1894static void activate_task(struct rq *rq, struct task_struct *p, int flags)
1912{ 1895{
1913 if (task_contributes_to_load(p)) 1896 if (task_contributes_to_load(p))
1914 rq->nr_uninterruptible--; 1897 rq->nr_uninterruptible--;
1915 1898
1916 enqueue_task(rq, p, wakeup, false); 1899 enqueue_task(rq, p, flags);
1917 inc_nr_running(rq); 1900 inc_nr_running(rq);
1918} 1901}
1919 1902
1920/* 1903/*
1921 * deactivate_task - remove a task from the runqueue. 1904 * deactivate_task - remove a task from the runqueue.
1922 */ 1905 */
1923static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep) 1906static void deactivate_task(struct rq *rq, struct task_struct *p, int flags)
1924{ 1907{
1925 if (task_contributes_to_load(p)) 1908 if (task_contributes_to_load(p))
1926 rq->nr_uninterruptible++; 1909 rq->nr_uninterruptible++;
1927 1910
1928 dequeue_task(rq, p, sleep); 1911 dequeue_task(rq, p, flags);
1929 dec_nr_running(rq); 1912 dec_nr_running(rq);
1930} 1913}
1931 1914
@@ -2054,21 +2037,18 @@ void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
2054 __set_task_cpu(p, new_cpu); 2037 __set_task_cpu(p, new_cpu);
2055} 2038}
2056 2039
2057struct migration_req { 2040struct migration_arg {
2058 struct list_head list;
2059
2060 struct task_struct *task; 2041 struct task_struct *task;
2061 int dest_cpu; 2042 int dest_cpu;
2062
2063 struct completion done;
2064}; 2043};
2065 2044
2045static int migration_cpu_stop(void *data);
2046
2066/* 2047/*
2067 * The task's runqueue lock must be held. 2048 * The task's runqueue lock must be held.
2068 * Returns true if you have to wait for migration thread. 2049 * Returns true if you have to wait for migration thread.
2069 */ 2050 */
2070static int 2051static bool migrate_task(struct task_struct *p, int dest_cpu)
2071migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
2072{ 2052{
2073 struct rq *rq = task_rq(p); 2053 struct rq *rq = task_rq(p);
2074 2054
@@ -2076,58 +2056,7 @@ migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
2076 * If the task is not on a runqueue (and not running), then 2056 * If the task is not on a runqueue (and not running), then
2077 * the next wake-up will properly place the task. 2057 * the next wake-up will properly place the task.
2078 */ 2058 */
2079 if (!p->se.on_rq && !task_running(rq, p)) 2059 return p->se.on_rq || task_running(rq, p);
2080 return 0;
2081
2082 init_completion(&req->done);
2083 req->task = p;
2084 req->dest_cpu = dest_cpu;
2085 list_add(&req->list, &rq->migration_queue);
2086
2087 return 1;
2088}
2089
2090/*
2091 * wait_task_context_switch - wait for a thread to complete at least one
2092 * context switch.
2093 *
2094 * @p must not be current.
2095 */
2096void wait_task_context_switch(struct task_struct *p)
2097{
2098 unsigned long nvcsw, nivcsw, flags;
2099 int running;
2100 struct rq *rq;
2101
2102 nvcsw = p->nvcsw;
2103 nivcsw = p->nivcsw;
2104 for (;;) {
2105 /*
2106 * The runqueue is assigned before the actual context
2107 * switch. We need to take the runqueue lock.
2108 *
2109 * We could check initially without the lock but it is
2110 * very likely that we need to take the lock in every
2111 * iteration.
2112 */
2113 rq = task_rq_lock(p, &flags);
2114 running = task_running(rq, p);
2115 task_rq_unlock(rq, &flags);
2116
2117 if (likely(!running))
2118 break;
2119 /*
2120 * The switch count is incremented before the actual
2121 * context switch. We thus wait for two switches to be
2122 * sure at least one completed.
2123 */
2124 if ((p->nvcsw - nvcsw) > 1)
2125 break;
2126 if ((p->nivcsw - nivcsw) > 1)
2127 break;
2128
2129 cpu_relax();
2130 }
2131} 2060}
2132 2061
2133/* 2062/*
@@ -2185,7 +2114,7 @@ unsigned long wait_task_inactive(struct task_struct *p, long match_state)
2185 * just go back and repeat. 2114 * just go back and repeat.
2186 */ 2115 */
2187 rq = task_rq_lock(p, &flags); 2116 rq = task_rq_lock(p, &flags);
2188 trace_sched_wait_task(rq, p); 2117 trace_sched_wait_task(p);
2189 running = task_running(rq, p); 2118 running = task_running(rq, p);
2190 on_rq = p->se.on_rq; 2119 on_rq = p->se.on_rq;
2191 ncsw = 0; 2120 ncsw = 0;
@@ -2283,6 +2212,9 @@ void task_oncpu_function_call(struct task_struct *p,
2283} 2212}
2284 2213
2285#ifdef CONFIG_SMP 2214#ifdef CONFIG_SMP
2215/*
2216 * ->cpus_allowed is protected by either TASK_WAKING or rq->lock held.
2217 */
2286static int select_fallback_rq(int cpu, struct task_struct *p) 2218static int select_fallback_rq(int cpu, struct task_struct *p)
2287{ 2219{
2288 int dest_cpu; 2220 int dest_cpu;
@@ -2299,12 +2231,8 @@ static int select_fallback_rq(int cpu, struct task_struct *p)
2299 return dest_cpu; 2231 return dest_cpu;
2300 2232
2301 /* No more Mr. Nice Guy. */ 2233 /* No more Mr. Nice Guy. */
2302 if (dest_cpu >= nr_cpu_ids) { 2234 if (unlikely(dest_cpu >= nr_cpu_ids)) {
2303 rcu_read_lock(); 2235 dest_cpu = cpuset_cpus_allowed_fallback(p);
2304 cpuset_cpus_allowed_locked(p, &p->cpus_allowed);
2305 rcu_read_unlock();
2306 dest_cpu = cpumask_any_and(cpu_active_mask, &p->cpus_allowed);
2307
2308 /* 2236 /*
2309 * Don't tell them about moving exiting tasks or 2237 * Don't tell them about moving exiting tasks or
2310 * kernel threads (both mm NULL), since they never 2238 * kernel threads (both mm NULL), since they never
@@ -2321,17 +2249,12 @@ static int select_fallback_rq(int cpu, struct task_struct *p)
2321} 2249}
2322 2250
2323/* 2251/*
2324 * Gets called from 3 sites (exec, fork, wakeup), since it is called without 2252 * The caller (fork, wakeup) owns TASK_WAKING, ->cpus_allowed is stable.
2325 * holding rq->lock we need to ensure ->cpus_allowed is stable, this is done
2326 * by:
2327 *
2328 * exec: is unstable, retry loop
2329 * fork & wake-up: serialize ->cpus_allowed against TASK_WAKING
2330 */ 2253 */
2331static inline 2254static inline
2332int select_task_rq(struct task_struct *p, int sd_flags, int wake_flags) 2255int select_task_rq(struct rq *rq, struct task_struct *p, int sd_flags, int wake_flags)
2333{ 2256{
2334 int cpu = p->sched_class->select_task_rq(p, sd_flags, wake_flags); 2257 int cpu = p->sched_class->select_task_rq(rq, p, sd_flags, wake_flags);
2335 2258
2336 /* 2259 /*
2337 * In order not to call set_task_cpu() on a blocking task we need 2260 * In order not to call set_task_cpu() on a blocking task we need
@@ -2349,6 +2272,12 @@ int select_task_rq(struct task_struct *p, int sd_flags, int wake_flags)
2349 2272
2350 return cpu; 2273 return cpu;
2351} 2274}
2275
2276static void update_avg(u64 *avg, u64 sample)
2277{
2278 s64 diff = sample - *avg;
2279 *avg += diff >> 3;
2280}
2352#endif 2281#endif
2353 2282
2354/*** 2283/***
@@ -2370,16 +2299,13 @@ static int try_to_wake_up(struct task_struct *p, unsigned int state,
2370{ 2299{
2371 int cpu, orig_cpu, this_cpu, success = 0; 2300 int cpu, orig_cpu, this_cpu, success = 0;
2372 unsigned long flags; 2301 unsigned long flags;
2302 unsigned long en_flags = ENQUEUE_WAKEUP;
2373 struct rq *rq; 2303 struct rq *rq;
2374 2304
2375 if (!sched_feat(SYNC_WAKEUPS))
2376 wake_flags &= ~WF_SYNC;
2377
2378 this_cpu = get_cpu(); 2305 this_cpu = get_cpu();
2379 2306
2380 smp_wmb(); 2307 smp_wmb();
2381 rq = task_rq_lock(p, &flags); 2308 rq = task_rq_lock(p, &flags);
2382 update_rq_clock(rq);
2383 if (!(p->state & state)) 2309 if (!(p->state & state))
2384 goto out; 2310 goto out;
2385 2311
@@ -2399,28 +2325,26 @@ static int try_to_wake_up(struct task_struct *p, unsigned int state,
2399 * 2325 *
2400 * First fix up the nr_uninterruptible count: 2326 * First fix up the nr_uninterruptible count:
2401 */ 2327 */
2402 if (task_contributes_to_load(p)) 2328 if (task_contributes_to_load(p)) {
2403 rq->nr_uninterruptible--; 2329 if (likely(cpu_online(orig_cpu)))
2330 rq->nr_uninterruptible--;
2331 else
2332 this_rq()->nr_uninterruptible--;
2333 }
2404 p->state = TASK_WAKING; 2334 p->state = TASK_WAKING;
2405 2335
2406 if (p->sched_class->task_waking) 2336 if (p->sched_class->task_waking) {
2407 p->sched_class->task_waking(rq, p); 2337 p->sched_class->task_waking(rq, p);
2338 en_flags |= ENQUEUE_WAKING;
2339 }
2408 2340
2409 __task_rq_unlock(rq); 2341 cpu = select_task_rq(rq, p, SD_BALANCE_WAKE, wake_flags);
2410 2342 if (cpu != orig_cpu)
2411 cpu = select_task_rq(p, SD_BALANCE_WAKE, wake_flags);
2412 if (cpu != orig_cpu) {
2413 /*
2414 * Since we migrate the task without holding any rq->lock,
2415 * we need to be careful with task_rq_lock(), since that
2416 * might end up locking an invalid rq.
2417 */
2418 set_task_cpu(p, cpu); 2343 set_task_cpu(p, cpu);
2419 } 2344 __task_rq_unlock(rq);
2420 2345
2421 rq = cpu_rq(cpu); 2346 rq = cpu_rq(cpu);
2422 raw_spin_lock(&rq->lock); 2347 raw_spin_lock(&rq->lock);
2423 update_rq_clock(rq);
2424 2348
2425 /* 2349 /*
2426 * We migrated the task without holding either rq->lock, however 2350 * We migrated the task without holding either rq->lock, however
@@ -2448,36 +2372,20 @@ static int try_to_wake_up(struct task_struct *p, unsigned int state,
2448 2372
2449out_activate: 2373out_activate:
2450#endif /* CONFIG_SMP */ 2374#endif /* CONFIG_SMP */
2451 schedstat_inc(p, se.nr_wakeups); 2375 schedstat_inc(p, se.statistics.nr_wakeups);
2452 if (wake_flags & WF_SYNC) 2376 if (wake_flags & WF_SYNC)
2453 schedstat_inc(p, se.nr_wakeups_sync); 2377 schedstat_inc(p, se.statistics.nr_wakeups_sync);
2454 if (orig_cpu != cpu) 2378 if (orig_cpu != cpu)
2455 schedstat_inc(p, se.nr_wakeups_migrate); 2379 schedstat_inc(p, se.statistics.nr_wakeups_migrate);
2456 if (cpu == this_cpu) 2380 if (cpu == this_cpu)
2457 schedstat_inc(p, se.nr_wakeups_local); 2381 schedstat_inc(p, se.statistics.nr_wakeups_local);
2458 else 2382 else
2459 schedstat_inc(p, se.nr_wakeups_remote); 2383 schedstat_inc(p, se.statistics.nr_wakeups_remote);
2460 activate_task(rq, p, 1); 2384 activate_task(rq, p, en_flags);
2461 success = 1; 2385 success = 1;
2462 2386
2463 /*
2464 * Only attribute actual wakeups done by this task.
2465 */
2466 if (!in_interrupt()) {
2467 struct sched_entity *se = &current->se;
2468 u64 sample = se->sum_exec_runtime;
2469
2470 if (se->last_wakeup)
2471 sample -= se->last_wakeup;
2472 else
2473 sample -= se->start_runtime;
2474 update_avg(&se->avg_wakeup, sample);
2475
2476 se->last_wakeup = se->sum_exec_runtime;
2477 }
2478
2479out_running: 2387out_running:
2480 trace_sched_wakeup(rq, p, success); 2388 trace_sched_wakeup(p, success);
2481 check_preempt_curr(rq, p, wake_flags); 2389 check_preempt_curr(rq, p, wake_flags);
2482 2390
2483 p->state = TASK_RUNNING; 2391 p->state = TASK_RUNNING;
@@ -2537,42 +2445,9 @@ static void __sched_fork(struct task_struct *p)
2537 p->se.sum_exec_runtime = 0; 2445 p->se.sum_exec_runtime = 0;
2538 p->se.prev_sum_exec_runtime = 0; 2446 p->se.prev_sum_exec_runtime = 0;
2539 p->se.nr_migrations = 0; 2447 p->se.nr_migrations = 0;
2540 p->se.last_wakeup = 0;
2541 p->se.avg_overlap = 0;
2542 p->se.start_runtime = 0;
2543 p->se.avg_wakeup = sysctl_sched_wakeup_granularity;
2544 2448
2545#ifdef CONFIG_SCHEDSTATS 2449#ifdef CONFIG_SCHEDSTATS
2546 p->se.wait_start = 0; 2450 memset(&p->se.statistics, 0, sizeof(p->se.statistics));
2547 p->se.wait_max = 0;
2548 p->se.wait_count = 0;
2549 p->se.wait_sum = 0;
2550
2551 p->se.sleep_start = 0;
2552 p->se.sleep_max = 0;
2553 p->se.sum_sleep_runtime = 0;
2554
2555 p->se.block_start = 0;
2556 p->se.block_max = 0;
2557 p->se.exec_max = 0;
2558 p->se.slice_max = 0;
2559
2560 p->se.nr_migrations_cold = 0;
2561 p->se.nr_failed_migrations_affine = 0;
2562 p->se.nr_failed_migrations_running = 0;
2563 p->se.nr_failed_migrations_hot = 0;
2564 p->se.nr_forced_migrations = 0;
2565
2566 p->se.nr_wakeups = 0;
2567 p->se.nr_wakeups_sync = 0;
2568 p->se.nr_wakeups_migrate = 0;
2569 p->se.nr_wakeups_local = 0;
2570 p->se.nr_wakeups_remote = 0;
2571 p->se.nr_wakeups_affine = 0;
2572 p->se.nr_wakeups_affine_attempts = 0;
2573 p->se.nr_wakeups_passive = 0;
2574 p->se.nr_wakeups_idle = 0;
2575
2576#endif 2451#endif
2577 2452
2578 INIT_LIST_HEAD(&p->rt.run_list); 2453 INIT_LIST_HEAD(&p->rt.run_list);
@@ -2593,11 +2468,11 @@ void sched_fork(struct task_struct *p, int clone_flags)
2593 2468
2594 __sched_fork(p); 2469 __sched_fork(p);
2595 /* 2470 /*
2596 * We mark the process as waking here. This guarantees that 2471 * We mark the process as running here. This guarantees that
2597 * nobody will actually run it, and a signal or other external 2472 * nobody will actually run it, and a signal or other external
2598 * event cannot wake it up and insert it on the runqueue either. 2473 * event cannot wake it up and insert it on the runqueue either.
2599 */ 2474 */
2600 p->state = TASK_WAKING; 2475 p->state = TASK_RUNNING;
2601 2476
2602 /* 2477 /*
2603 * Revert to default priority/policy on fork if requested. 2478 * Revert to default priority/policy on fork if requested.
@@ -2664,31 +2539,27 @@ void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
2664 int cpu __maybe_unused = get_cpu(); 2539 int cpu __maybe_unused = get_cpu();
2665 2540
2666#ifdef CONFIG_SMP 2541#ifdef CONFIG_SMP
2542 rq = task_rq_lock(p, &flags);
2543 p->state = TASK_WAKING;
2544
2667 /* 2545 /*
2668 * Fork balancing, do it here and not earlier because: 2546 * Fork balancing, do it here and not earlier because:
2669 * - cpus_allowed can change in the fork path 2547 * - cpus_allowed can change in the fork path
2670 * - any previously selected cpu might disappear through hotplug 2548 * - any previously selected cpu might disappear through hotplug
2671 * 2549 *
2672 * We still have TASK_WAKING but PF_STARTING is gone now, meaning 2550 * We set TASK_WAKING so that select_task_rq() can drop rq->lock
2673 * ->cpus_allowed is stable, we have preemption disabled, meaning 2551 * without people poking at ->cpus_allowed.
2674 * cpu_online_mask is stable.
2675 */ 2552 */
2676 cpu = select_task_rq(p, SD_BALANCE_FORK, 0); 2553 cpu = select_task_rq(rq, p, SD_BALANCE_FORK, 0);
2677 set_task_cpu(p, cpu); 2554 set_task_cpu(p, cpu);
2678#endif
2679 2555
2680 /*
2681 * Since the task is not on the rq and we still have TASK_WAKING set
2682 * nobody else will migrate this task.
2683 */
2684 rq = cpu_rq(cpu);
2685 raw_spin_lock_irqsave(&rq->lock, flags);
2686
2687 BUG_ON(p->state != TASK_WAKING);
2688 p->state = TASK_RUNNING; 2556 p->state = TASK_RUNNING;
2689 update_rq_clock(rq); 2557 task_rq_unlock(rq, &flags);
2558#endif
2559
2560 rq = task_rq_lock(p, &flags);
2690 activate_task(rq, p, 0); 2561 activate_task(rq, p, 0);
2691 trace_sched_wakeup_new(rq, p, 1); 2562 trace_sched_wakeup_new(p, 1);
2692 check_preempt_curr(rq, p, WF_FORK); 2563 check_preempt_curr(rq, p, WF_FORK);
2693#ifdef CONFIG_SMP 2564#ifdef CONFIG_SMP
2694 if (p->sched_class->task_woken) 2565 if (p->sched_class->task_woken)
@@ -2908,7 +2779,7 @@ context_switch(struct rq *rq, struct task_struct *prev,
2908 struct mm_struct *mm, *oldmm; 2779 struct mm_struct *mm, *oldmm;
2909 2780
2910 prepare_task_switch(rq, prev, next); 2781 prepare_task_switch(rq, prev, next);
2911 trace_sched_switch(rq, prev, next); 2782 trace_sched_switch(prev, next);
2912 mm = next->mm; 2783 mm = next->mm;
2913 oldmm = prev->active_mm; 2784 oldmm = prev->active_mm;
2914 /* 2785 /*
@@ -3025,6 +2896,61 @@ static unsigned long calc_load_update;
3025unsigned long avenrun[3]; 2896unsigned long avenrun[3];
3026EXPORT_SYMBOL(avenrun); 2897EXPORT_SYMBOL(avenrun);
3027 2898
2899static long calc_load_fold_active(struct rq *this_rq)
2900{
2901 long nr_active, delta = 0;
2902
2903 nr_active = this_rq->nr_running;
2904 nr_active += (long) this_rq->nr_uninterruptible;
2905
2906 if (nr_active != this_rq->calc_load_active) {
2907 delta = nr_active - this_rq->calc_load_active;
2908 this_rq->calc_load_active = nr_active;
2909 }
2910
2911 return delta;
2912}
2913
2914#ifdef CONFIG_NO_HZ
2915/*
2916 * For NO_HZ we delay the active fold to the next LOAD_FREQ update.
2917 *
2918 * When making the ILB scale, we should try to pull this in as well.
2919 */
2920static atomic_long_t calc_load_tasks_idle;
2921
2922static void calc_load_account_idle(struct rq *this_rq)
2923{
2924 long delta;
2925
2926 delta = calc_load_fold_active(this_rq);
2927 if (delta)
2928 atomic_long_add(delta, &calc_load_tasks_idle);
2929}
2930
2931static long calc_load_fold_idle(void)
2932{
2933 long delta = 0;
2934
2935 /*
2936 * Its got a race, we don't care...
2937 */
2938 if (atomic_long_read(&calc_load_tasks_idle))
2939 delta = atomic_long_xchg(&calc_load_tasks_idle, 0);
2940
2941 return delta;
2942}
2943#else
2944static void calc_load_account_idle(struct rq *this_rq)
2945{
2946}
2947
2948static inline long calc_load_fold_idle(void)
2949{
2950 return 0;
2951}
2952#endif
2953
3028/** 2954/**
3029 * get_avenrun - get the load average array 2955 * get_avenrun - get the load average array
3030 * @loads: pointer to dest load array 2956 * @loads: pointer to dest load array
@@ -3071,20 +2997,22 @@ void calc_global_load(void)
3071} 2997}
3072 2998
3073/* 2999/*
3074 * Either called from update_cpu_load() or from a cpu going idle 3000 * Called from update_cpu_load() to periodically update this CPU's
3001 * active count.
3075 */ 3002 */
3076static void calc_load_account_active(struct rq *this_rq) 3003static void calc_load_account_active(struct rq *this_rq)
3077{ 3004{
3078 long nr_active, delta; 3005 long delta;
3079 3006
3080 nr_active = this_rq->nr_running; 3007 if (time_before(jiffies, this_rq->calc_load_update))
3081 nr_active += (long) this_rq->nr_uninterruptible; 3008 return;
3082 3009
3083 if (nr_active != this_rq->calc_load_active) { 3010 delta = calc_load_fold_active(this_rq);
3084 delta = nr_active - this_rq->calc_load_active; 3011 delta += calc_load_fold_idle();
3085 this_rq->calc_load_active = nr_active; 3012 if (delta)
3086 atomic_long_add(delta, &calc_load_tasks); 3013 atomic_long_add(delta, &calc_load_tasks);
3087 } 3014
3015 this_rq->calc_load_update += LOAD_FREQ;
3088} 3016}
3089 3017
3090/* 3018/*
@@ -3116,10 +3044,7 @@ static void update_cpu_load(struct rq *this_rq)
3116 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i; 3044 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
3117 } 3045 }
3118 3046
3119 if (time_after_eq(jiffies, this_rq->calc_load_update)) { 3047 calc_load_account_active(this_rq);
3120 this_rq->calc_load_update += LOAD_FREQ;
3121 calc_load_account_active(this_rq);
3122 }
3123} 3048}
3124 3049
3125#ifdef CONFIG_SMP 3050#ifdef CONFIG_SMP
@@ -3131,44 +3056,27 @@ static void update_cpu_load(struct rq *this_rq)
3131void sched_exec(void) 3056void sched_exec(void)
3132{ 3057{
3133 struct task_struct *p = current; 3058 struct task_struct *p = current;
3134 struct migration_req req;
3135 int dest_cpu, this_cpu;
3136 unsigned long flags; 3059 unsigned long flags;
3137 struct rq *rq; 3060 struct rq *rq;
3138 3061 int dest_cpu;
3139again:
3140 this_cpu = get_cpu();
3141 dest_cpu = select_task_rq(p, SD_BALANCE_EXEC, 0);
3142 if (dest_cpu == this_cpu) {
3143 put_cpu();
3144 return;
3145 }
3146 3062
3147 rq = task_rq_lock(p, &flags); 3063 rq = task_rq_lock(p, &flags);
3148 put_cpu(); 3064 dest_cpu = p->sched_class->select_task_rq(rq, p, SD_BALANCE_EXEC, 0);
3065 if (dest_cpu == smp_processor_id())
3066 goto unlock;
3149 3067
3150 /* 3068 /*
3151 * select_task_rq() can race against ->cpus_allowed 3069 * select_task_rq() can race against ->cpus_allowed
3152 */ 3070 */
3153 if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed) 3071 if (cpumask_test_cpu(dest_cpu, &p->cpus_allowed) &&
3154 || unlikely(!cpu_active(dest_cpu))) { 3072 likely(cpu_active(dest_cpu)) && migrate_task(p, dest_cpu)) {
3155 task_rq_unlock(rq, &flags); 3073 struct migration_arg arg = { p, dest_cpu };
3156 goto again;
3157 }
3158
3159 /* force the process onto the specified CPU */
3160 if (migrate_task(p, dest_cpu, &req)) {
3161 /* Need to wait for migration thread (might exit: take ref). */
3162 struct task_struct *mt = rq->migration_thread;
3163 3074
3164 get_task_struct(mt);
3165 task_rq_unlock(rq, &flags); 3075 task_rq_unlock(rq, &flags);
3166 wake_up_process(mt); 3076 stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
3167 put_task_struct(mt);
3168 wait_for_completion(&req.done);
3169
3170 return; 3077 return;
3171 } 3078 }
3079unlock:
3172 task_rq_unlock(rq, &flags); 3080 task_rq_unlock(rq, &flags);
3173} 3081}
3174 3082
@@ -3640,23 +3548,9 @@ static inline void schedule_debug(struct task_struct *prev)
3640 3548
3641static void put_prev_task(struct rq *rq, struct task_struct *prev) 3549static void put_prev_task(struct rq *rq, struct task_struct *prev)
3642{ 3550{
3643 if (prev->state == TASK_RUNNING) { 3551 if (prev->se.on_rq)
3644 u64 runtime = prev->se.sum_exec_runtime; 3552 update_rq_clock(rq);
3645 3553 rq->skip_clock_update = 0;
3646 runtime -= prev->se.prev_sum_exec_runtime;
3647 runtime = min_t(u64, runtime, 2*sysctl_sched_migration_cost);
3648
3649 /*
3650 * In order to avoid avg_overlap growing stale when we are
3651 * indeed overlapping and hence not getting put to sleep, grow
3652 * the avg_overlap on preemption.
3653 *
3654 * We use the average preemption runtime because that
3655 * correlates to the amount of cache footprint a task can
3656 * build up.
3657 */
3658 update_avg(&prev->se.avg_overlap, runtime);
3659 }
3660 prev->sched_class->put_prev_task(rq, prev); 3554 prev->sched_class->put_prev_task(rq, prev);
3661} 3555}
3662 3556
@@ -3706,7 +3600,7 @@ need_resched:
3706 preempt_disable(); 3600 preempt_disable();
3707 cpu = smp_processor_id(); 3601 cpu = smp_processor_id();
3708 rq = cpu_rq(cpu); 3602 rq = cpu_rq(cpu);
3709 rcu_sched_qs(cpu); 3603 rcu_note_context_switch(cpu);
3710 prev = rq->curr; 3604 prev = rq->curr;
3711 switch_count = &prev->nivcsw; 3605 switch_count = &prev->nivcsw;
3712 3606
@@ -3719,14 +3613,13 @@ need_resched_nonpreemptible:
3719 hrtick_clear(rq); 3613 hrtick_clear(rq);
3720 3614
3721 raw_spin_lock_irq(&rq->lock); 3615 raw_spin_lock_irq(&rq->lock);
3722 update_rq_clock(rq);
3723 clear_tsk_need_resched(prev); 3616 clear_tsk_need_resched(prev);
3724 3617
3725 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) { 3618 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
3726 if (unlikely(signal_pending_state(prev->state, prev))) 3619 if (unlikely(signal_pending_state(prev->state, prev)))
3727 prev->state = TASK_RUNNING; 3620 prev->state = TASK_RUNNING;
3728 else 3621 else
3729 deactivate_task(rq, prev, 1); 3622 deactivate_task(rq, prev, DEQUEUE_SLEEP);
3730 switch_count = &prev->nvcsw; 3623 switch_count = &prev->nvcsw;
3731 } 3624 }
3732 3625
@@ -3950,6 +3843,7 @@ void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
3950{ 3843{
3951 __wake_up_common(q, mode, 1, 0, NULL); 3844 __wake_up_common(q, mode, 1, 0, NULL);
3952} 3845}
3846EXPORT_SYMBOL_GPL(__wake_up_locked);
3953 3847
3954void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key) 3848void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key)
3955{ 3849{
@@ -4049,8 +3943,7 @@ do_wait_for_common(struct completion *x, long timeout, int state)
4049 if (!x->done) { 3943 if (!x->done) {
4050 DECLARE_WAITQUEUE(wait, current); 3944 DECLARE_WAITQUEUE(wait, current);
4051 3945
4052 wait.flags |= WQ_FLAG_EXCLUSIVE; 3946 __add_wait_queue_tail_exclusive(&x->wait, &wait);
4053 __add_wait_queue_tail(&x->wait, &wait);
4054 do { 3947 do {
4055 if (signal_pending_state(state, current)) { 3948 if (signal_pending_state(state, current)) {
4056 timeout = -ERESTARTSYS; 3949 timeout = -ERESTARTSYS;
@@ -4161,6 +4054,23 @@ int __sched wait_for_completion_killable(struct completion *x)
4161EXPORT_SYMBOL(wait_for_completion_killable); 4054EXPORT_SYMBOL(wait_for_completion_killable);
4162 4055
4163/** 4056/**
4057 * wait_for_completion_killable_timeout: - waits for completion of a task (w/(to,killable))
4058 * @x: holds the state of this particular completion
4059 * @timeout: timeout value in jiffies
4060 *
4061 * This waits for either a completion of a specific task to be
4062 * signaled or for a specified timeout to expire. It can be
4063 * interrupted by a kill signal. The timeout is in jiffies.
4064 */
4065unsigned long __sched
4066wait_for_completion_killable_timeout(struct completion *x,
4067 unsigned long timeout)
4068{
4069 return wait_for_common(x, timeout, TASK_KILLABLE);
4070}
4071EXPORT_SYMBOL(wait_for_completion_killable_timeout);
4072
4073/**
4164 * try_wait_for_completion - try to decrement a completion without blocking 4074 * try_wait_for_completion - try to decrement a completion without blocking
4165 * @x: completion structure 4075 * @x: completion structure
4166 * 4076 *
@@ -4276,7 +4186,6 @@ void rt_mutex_setprio(struct task_struct *p, int prio)
4276 BUG_ON(prio < 0 || prio > MAX_PRIO); 4186 BUG_ON(prio < 0 || prio > MAX_PRIO);
4277 4187
4278 rq = task_rq_lock(p, &flags); 4188 rq = task_rq_lock(p, &flags);
4279 update_rq_clock(rq);
4280 4189
4281 oldprio = p->prio; 4190 oldprio = p->prio;
4282 prev_class = p->sched_class; 4191 prev_class = p->sched_class;
@@ -4297,7 +4206,7 @@ void rt_mutex_setprio(struct task_struct *p, int prio)
4297 if (running) 4206 if (running)
4298 p->sched_class->set_curr_task(rq); 4207 p->sched_class->set_curr_task(rq);
4299 if (on_rq) { 4208 if (on_rq) {
4300 enqueue_task(rq, p, 0, oldprio < prio); 4209 enqueue_task(rq, p, oldprio < prio ? ENQUEUE_HEAD : 0);
4301 4210
4302 check_class_changed(rq, p, prev_class, oldprio, running); 4211 check_class_changed(rq, p, prev_class, oldprio, running);
4303 } 4212 }
@@ -4319,7 +4228,6 @@ void set_user_nice(struct task_struct *p, long nice)
4319 * the task might be in the middle of scheduling on another CPU. 4228 * the task might be in the middle of scheduling on another CPU.
4320 */ 4229 */
4321 rq = task_rq_lock(p, &flags); 4230 rq = task_rq_lock(p, &flags);
4322 update_rq_clock(rq);
4323 /* 4231 /*
4324 * The RT priorities are set via sched_setscheduler(), but we still 4232 * The RT priorities are set via sched_setscheduler(), but we still
4325 * allow the 'normal' nice value to be set - but as expected 4233 * allow the 'normal' nice value to be set - but as expected
@@ -4341,7 +4249,7 @@ void set_user_nice(struct task_struct *p, long nice)
4341 delta = p->prio - old_prio; 4249 delta = p->prio - old_prio;
4342 4250
4343 if (on_rq) { 4251 if (on_rq) {
4344 enqueue_task(rq, p, 0, false); 4252 enqueue_task(rq, p, 0);
4345 /* 4253 /*
4346 * If the task increased its priority or is running and 4254 * If the task increased its priority or is running and
4347 * lowered its priority, then reschedule its CPU: 4255 * lowered its priority, then reschedule its CPU:
@@ -4602,7 +4510,6 @@ recheck:
4602 raw_spin_unlock_irqrestore(&p->pi_lock, flags); 4510 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
4603 goto recheck; 4511 goto recheck;
4604 } 4512 }
4605 update_rq_clock(rq);
4606 on_rq = p->se.on_rq; 4513 on_rq = p->se.on_rq;
4607 running = task_current(rq, p); 4514 running = task_current(rq, p);
4608 if (on_rq) 4515 if (on_rq)
@@ -5339,17 +5246,15 @@ static inline void sched_init_granularity(void)
5339/* 5246/*
5340 * This is how migration works: 5247 * This is how migration works:
5341 * 5248 *
5342 * 1) we queue a struct migration_req structure in the source CPU's 5249 * 1) we invoke migration_cpu_stop() on the target CPU using
5343 * runqueue and wake up that CPU's migration thread. 5250 * stop_one_cpu().
5344 * 2) we down() the locked semaphore => thread blocks. 5251 * 2) stopper starts to run (implicitly forcing the migrated thread
5345 * 3) migration thread wakes up (implicitly it forces the migrated 5252 * off the CPU)
5346 * thread off the CPU) 5253 * 3) it checks whether the migrated task is still in the wrong runqueue.
5347 * 4) it gets the migration request and checks whether the migrated 5254 * 4) if it's in the wrong runqueue then the migration thread removes
5348 * task is still in the wrong runqueue.
5349 * 5) if it's in the wrong runqueue then the migration thread removes
5350 * it and puts it into the right queue. 5255 * it and puts it into the right queue.
5351 * 6) migration thread up()s the semaphore. 5256 * 5) stopper completes and stop_one_cpu() returns and the migration
5352 * 7) we wake up and the migration is done. 5257 * is done.
5353 */ 5258 */
5354 5259
5355/* 5260/*
@@ -5363,12 +5268,23 @@ static inline void sched_init_granularity(void)
5363 */ 5268 */
5364int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask) 5269int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
5365{ 5270{
5366 struct migration_req req;
5367 unsigned long flags; 5271 unsigned long flags;
5368 struct rq *rq; 5272 struct rq *rq;
5273 unsigned int dest_cpu;
5369 int ret = 0; 5274 int ret = 0;
5370 5275
5276 /*
5277 * Serialize against TASK_WAKING so that ttwu() and wunt() can
5278 * drop the rq->lock and still rely on ->cpus_allowed.
5279 */
5280again:
5281 while (task_is_waking(p))
5282 cpu_relax();
5371 rq = task_rq_lock(p, &flags); 5283 rq = task_rq_lock(p, &flags);
5284 if (task_is_waking(p)) {
5285 task_rq_unlock(rq, &flags);
5286 goto again;
5287 }
5372 5288
5373 if (!cpumask_intersects(new_mask, cpu_active_mask)) { 5289 if (!cpumask_intersects(new_mask, cpu_active_mask)) {
5374 ret = -EINVAL; 5290 ret = -EINVAL;
@@ -5392,15 +5308,12 @@ int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
5392 if (cpumask_test_cpu(task_cpu(p), new_mask)) 5308 if (cpumask_test_cpu(task_cpu(p), new_mask))
5393 goto out; 5309 goto out;
5394 5310
5395 if (migrate_task(p, cpumask_any_and(cpu_active_mask, new_mask), &req)) { 5311 dest_cpu = cpumask_any_and(cpu_active_mask, new_mask);
5312 if (migrate_task(p, dest_cpu)) {
5313 struct migration_arg arg = { p, dest_cpu };
5396 /* Need help from migration thread: drop lock and wait. */ 5314 /* Need help from migration thread: drop lock and wait. */
5397 struct task_struct *mt = rq->migration_thread;
5398
5399 get_task_struct(mt);
5400 task_rq_unlock(rq, &flags); 5315 task_rq_unlock(rq, &flags);
5401 wake_up_process(mt); 5316 stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
5402 put_task_struct(mt);
5403 wait_for_completion(&req.done);
5404 tlb_migrate_finish(p->mm); 5317 tlb_migrate_finish(p->mm);
5405 return 0; 5318 return 0;
5406 } 5319 }
@@ -5458,98 +5371,49 @@ fail:
5458 return ret; 5371 return ret;
5459} 5372}
5460 5373
5461#define RCU_MIGRATION_IDLE 0
5462#define RCU_MIGRATION_NEED_QS 1
5463#define RCU_MIGRATION_GOT_QS 2
5464#define RCU_MIGRATION_MUST_SYNC 3
5465
5466/* 5374/*
5467 * migration_thread - this is a highprio system thread that performs 5375 * migration_cpu_stop - this will be executed by a highprio stopper thread
5468 * thread migration by bumping thread off CPU then 'pushing' onto 5376 * and performs thread migration by bumping thread off CPU then
5469 * another runqueue. 5377 * 'pushing' onto another runqueue.
5470 */ 5378 */
5471static int migration_thread(void *data) 5379static int migration_cpu_stop(void *data)
5472{
5473 int badcpu;
5474 int cpu = (long)data;
5475 struct rq *rq;
5476
5477 rq = cpu_rq(cpu);
5478 BUG_ON(rq->migration_thread != current);
5479
5480 set_current_state(TASK_INTERRUPTIBLE);
5481 while (!kthread_should_stop()) {
5482 struct migration_req *req;
5483 struct list_head *head;
5484
5485 raw_spin_lock_irq(&rq->lock);
5486
5487 if (cpu_is_offline(cpu)) {
5488 raw_spin_unlock_irq(&rq->lock);
5489 break;
5490 }
5491
5492 if (rq->active_balance) {
5493 active_load_balance(rq, cpu);
5494 rq->active_balance = 0;
5495 }
5496
5497 head = &rq->migration_queue;
5498
5499 if (list_empty(head)) {
5500 raw_spin_unlock_irq(&rq->lock);
5501 schedule();
5502 set_current_state(TASK_INTERRUPTIBLE);
5503 continue;
5504 }
5505 req = list_entry(head->next, struct migration_req, list);
5506 list_del_init(head->next);
5507
5508 if (req->task != NULL) {
5509 raw_spin_unlock(&rq->lock);
5510 __migrate_task(req->task, cpu, req->dest_cpu);
5511 } else if (likely(cpu == (badcpu = smp_processor_id()))) {
5512 req->dest_cpu = RCU_MIGRATION_GOT_QS;
5513 raw_spin_unlock(&rq->lock);
5514 } else {
5515 req->dest_cpu = RCU_MIGRATION_MUST_SYNC;
5516 raw_spin_unlock(&rq->lock);
5517 WARN_ONCE(1, "migration_thread() on CPU %d, expected %d\n", badcpu, cpu);
5518 }
5519 local_irq_enable();
5520
5521 complete(&req->done);
5522 }
5523 __set_current_state(TASK_RUNNING);
5524
5525 return 0;
5526}
5527
5528#ifdef CONFIG_HOTPLUG_CPU
5529
5530static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
5531{ 5380{
5532 int ret; 5381 struct migration_arg *arg = data;
5533 5382
5383 /*
5384 * The original target cpu might have gone down and we might
5385 * be on another cpu but it doesn't matter.
5386 */
5534 local_irq_disable(); 5387 local_irq_disable();
5535 ret = __migrate_task(p, src_cpu, dest_cpu); 5388 __migrate_task(arg->task, raw_smp_processor_id(), arg->dest_cpu);
5536 local_irq_enable(); 5389 local_irq_enable();
5537 return ret; 5390 return 0;
5538} 5391}
5539 5392
5393#ifdef CONFIG_HOTPLUG_CPU
5540/* 5394/*
5541 * Figure out where task on dead CPU should go, use force if necessary. 5395 * Figure out where task on dead CPU should go, use force if necessary.
5542 */ 5396 */
5543static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p) 5397void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
5544{ 5398{
5545 int dest_cpu; 5399 struct rq *rq = cpu_rq(dead_cpu);
5400 int needs_cpu, uninitialized_var(dest_cpu);
5401 unsigned long flags;
5546 5402
5547again: 5403 local_irq_save(flags);
5548 dest_cpu = select_fallback_rq(dead_cpu, p);
5549 5404
5550 /* It can have affinity changed while we were choosing. */ 5405 raw_spin_lock(&rq->lock);
5551 if (unlikely(!__migrate_task_irq(p, dead_cpu, dest_cpu))) 5406 needs_cpu = (task_cpu(p) == dead_cpu) && (p->state != TASK_WAKING);
5552 goto again; 5407 if (needs_cpu)
5408 dest_cpu = select_fallback_rq(dead_cpu, p);
5409 raw_spin_unlock(&rq->lock);
5410 /*
5411 * It can only fail if we race with set_cpus_allowed(),
5412 * in the racer should migrate the task anyway.
5413 */
5414 if (needs_cpu)
5415 __migrate_task(p, dead_cpu, dest_cpu);
5416 local_irq_restore(flags);
5553} 5417}
5554 5418
5555/* 5419/*
@@ -5613,7 +5477,6 @@ void sched_idle_next(void)
5613 5477
5614 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1); 5478 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
5615 5479
5616 update_rq_clock(rq);
5617 activate_task(rq, p, 0); 5480 activate_task(rq, p, 0);
5618 5481
5619 raw_spin_unlock_irqrestore(&rq->lock, flags); 5482 raw_spin_unlock_irqrestore(&rq->lock, flags);
@@ -5668,7 +5531,6 @@ static void migrate_dead_tasks(unsigned int dead_cpu)
5668 for ( ; ; ) { 5531 for ( ; ; ) {
5669 if (!rq->nr_running) 5532 if (!rq->nr_running)
5670 break; 5533 break;
5671 update_rq_clock(rq);
5672 next = pick_next_task(rq); 5534 next = pick_next_task(rq);
5673 if (!next) 5535 if (!next)
5674 break; 5536 break;
@@ -5891,35 +5753,20 @@ static void set_rq_offline(struct rq *rq)
5891static int __cpuinit 5753static int __cpuinit
5892migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu) 5754migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
5893{ 5755{
5894 struct task_struct *p;
5895 int cpu = (long)hcpu; 5756 int cpu = (long)hcpu;
5896 unsigned long flags; 5757 unsigned long flags;
5897 struct rq *rq; 5758 struct rq *rq = cpu_rq(cpu);
5898 5759
5899 switch (action) { 5760 switch (action) {
5900 5761
5901 case CPU_UP_PREPARE: 5762 case CPU_UP_PREPARE:
5902 case CPU_UP_PREPARE_FROZEN: 5763 case CPU_UP_PREPARE_FROZEN:
5903 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
5904 if (IS_ERR(p))
5905 return NOTIFY_BAD;
5906 kthread_bind(p, cpu);
5907 /* Must be high prio: stop_machine expects to yield to it. */
5908 rq = task_rq_lock(p, &flags);
5909 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
5910 task_rq_unlock(rq, &flags);
5911 get_task_struct(p);
5912 cpu_rq(cpu)->migration_thread = p;
5913 rq->calc_load_update = calc_load_update; 5764 rq->calc_load_update = calc_load_update;
5914 break; 5765 break;
5915 5766
5916 case CPU_ONLINE: 5767 case CPU_ONLINE:
5917 case CPU_ONLINE_FROZEN: 5768 case CPU_ONLINE_FROZEN:
5918 /* Strictly unnecessary, as first user will wake it. */
5919 wake_up_process(cpu_rq(cpu)->migration_thread);
5920
5921 /* Update our root-domain */ 5769 /* Update our root-domain */
5922 rq = cpu_rq(cpu);
5923 raw_spin_lock_irqsave(&rq->lock, flags); 5770 raw_spin_lock_irqsave(&rq->lock, flags);
5924 if (rq->rd) { 5771 if (rq->rd) {
5925 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span)); 5772 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
@@ -5930,61 +5777,24 @@ migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
5930 break; 5777 break;
5931 5778
5932#ifdef CONFIG_HOTPLUG_CPU 5779#ifdef CONFIG_HOTPLUG_CPU
5933 case CPU_UP_CANCELED:
5934 case CPU_UP_CANCELED_FROZEN:
5935 if (!cpu_rq(cpu)->migration_thread)
5936 break;
5937 /* Unbind it from offline cpu so it can run. Fall thru. */
5938 kthread_bind(cpu_rq(cpu)->migration_thread,
5939 cpumask_any(cpu_online_mask));
5940 kthread_stop(cpu_rq(cpu)->migration_thread);
5941 put_task_struct(cpu_rq(cpu)->migration_thread);
5942 cpu_rq(cpu)->migration_thread = NULL;
5943 break;
5944
5945 case CPU_DEAD: 5780 case CPU_DEAD:
5946 case CPU_DEAD_FROZEN: 5781 case CPU_DEAD_FROZEN:
5947 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
5948 migrate_live_tasks(cpu); 5782 migrate_live_tasks(cpu);
5949 rq = cpu_rq(cpu);
5950 kthread_stop(rq->migration_thread);
5951 put_task_struct(rq->migration_thread);
5952 rq->migration_thread = NULL;
5953 /* Idle task back to normal (off runqueue, low prio) */ 5783 /* Idle task back to normal (off runqueue, low prio) */
5954 raw_spin_lock_irq(&rq->lock); 5784 raw_spin_lock_irq(&rq->lock);
5955 update_rq_clock(rq);
5956 deactivate_task(rq, rq->idle, 0); 5785 deactivate_task(rq, rq->idle, 0);
5957 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0); 5786 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
5958 rq->idle->sched_class = &idle_sched_class; 5787 rq->idle->sched_class = &idle_sched_class;
5959 migrate_dead_tasks(cpu); 5788 migrate_dead_tasks(cpu);
5960 raw_spin_unlock_irq(&rq->lock); 5789 raw_spin_unlock_irq(&rq->lock);
5961 cpuset_unlock();
5962 migrate_nr_uninterruptible(rq); 5790 migrate_nr_uninterruptible(rq);
5963 BUG_ON(rq->nr_running != 0); 5791 BUG_ON(rq->nr_running != 0);
5964 calc_global_load_remove(rq); 5792 calc_global_load_remove(rq);
5965 /*
5966 * No need to migrate the tasks: it was best-effort if
5967 * they didn't take sched_hotcpu_mutex. Just wake up
5968 * the requestors.
5969 */
5970 raw_spin_lock_irq(&rq->lock);
5971 while (!list_empty(&rq->migration_queue)) {
5972 struct migration_req *req;
5973
5974 req = list_entry(rq->migration_queue.next,
5975 struct migration_req, list);
5976 list_del_init(&req->list);
5977 raw_spin_unlock_irq(&rq->lock);
5978 complete(&req->done);
5979 raw_spin_lock_irq(&rq->lock);
5980 }
5981 raw_spin_unlock_irq(&rq->lock);
5982 break; 5793 break;
5983 5794
5984 case CPU_DYING: 5795 case CPU_DYING:
5985 case CPU_DYING_FROZEN: 5796 case CPU_DYING_FROZEN:
5986 /* Update our root-domain */ 5797 /* Update our root-domain */
5987 rq = cpu_rq(cpu);
5988 raw_spin_lock_irqsave(&rq->lock, flags); 5798 raw_spin_lock_irqsave(&rq->lock, flags);
5989 if (rq->rd) { 5799 if (rq->rd) {
5990 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span)); 5800 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
@@ -6315,6 +6125,9 @@ cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
6315 struct rq *rq = cpu_rq(cpu); 6125 struct rq *rq = cpu_rq(cpu);
6316 struct sched_domain *tmp; 6126 struct sched_domain *tmp;
6317 6127
6128 for (tmp = sd; tmp; tmp = tmp->parent)
6129 tmp->span_weight = cpumask_weight(sched_domain_span(tmp));
6130
6318 /* Remove the sched domains which do not contribute to scheduling. */ 6131 /* Remove the sched domains which do not contribute to scheduling. */
6319 for (tmp = sd; tmp; ) { 6132 for (tmp = sd; tmp; ) {
6320 struct sched_domain *parent = tmp->parent; 6133 struct sched_domain *parent = tmp->parent;
@@ -7798,10 +7611,8 @@ void __init sched_init(void)
7798 rq->push_cpu = 0; 7611 rq->push_cpu = 0;
7799 rq->cpu = i; 7612 rq->cpu = i;
7800 rq->online = 0; 7613 rq->online = 0;
7801 rq->migration_thread = NULL;
7802 rq->idle_stamp = 0; 7614 rq->idle_stamp = 0;
7803 rq->avg_idle = 2*sysctl_sched_migration_cost; 7615 rq->avg_idle = 2*sysctl_sched_migration_cost;
7804 INIT_LIST_HEAD(&rq->migration_queue);
7805 rq_attach_root(rq, &def_root_domain); 7616 rq_attach_root(rq, &def_root_domain);
7806#endif 7617#endif
7807 init_rq_hrtick(rq); 7618 init_rq_hrtick(rq);
@@ -7902,7 +7713,6 @@ static void normalize_task(struct rq *rq, struct task_struct *p)
7902{ 7713{
7903 int on_rq; 7714 int on_rq;
7904 7715
7905 update_rq_clock(rq);
7906 on_rq = p->se.on_rq; 7716 on_rq = p->se.on_rq;
7907 if (on_rq) 7717 if (on_rq)
7908 deactivate_task(rq, p, 0); 7718 deactivate_task(rq, p, 0);
@@ -7929,9 +7739,9 @@ void normalize_rt_tasks(void)
7929 7739
7930 p->se.exec_start = 0; 7740 p->se.exec_start = 0;
7931#ifdef CONFIG_SCHEDSTATS 7741#ifdef CONFIG_SCHEDSTATS
7932 p->se.wait_start = 0; 7742 p->se.statistics.wait_start = 0;
7933 p->se.sleep_start = 0; 7743 p->se.statistics.sleep_start = 0;
7934 p->se.block_start = 0; 7744 p->se.statistics.block_start = 0;
7935#endif 7745#endif
7936 7746
7937 if (!rt_task(p)) { 7747 if (!rt_task(p)) {
@@ -7958,9 +7768,9 @@ void normalize_rt_tasks(void)
7958 7768
7959#endif /* CONFIG_MAGIC_SYSRQ */ 7769#endif /* CONFIG_MAGIC_SYSRQ */
7960 7770
7961#ifdef CONFIG_IA64 7771#if defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB)
7962/* 7772/*
7963 * These functions are only useful for the IA64 MCA handling. 7773 * These functions are only useful for the IA64 MCA handling, or kdb.
7964 * 7774 *
7965 * They can only be called when the whole system has been 7775 * They can only be called when the whole system has been
7966 * stopped - every CPU needs to be quiescent, and no scheduling 7776 * stopped - every CPU needs to be quiescent, and no scheduling
@@ -7980,6 +7790,9 @@ struct task_struct *curr_task(int cpu)
7980 return cpu_curr(cpu); 7790 return cpu_curr(cpu);
7981} 7791}
7982 7792
7793#endif /* defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB) */
7794
7795#ifdef CONFIG_IA64
7983/** 7796/**
7984 * set_curr_task - set the current task for a given cpu. 7797 * set_curr_task - set the current task for a given cpu.
7985 * @cpu: the processor in question. 7798 * @cpu: the processor in question.
@@ -8264,8 +8077,6 @@ void sched_move_task(struct task_struct *tsk)
8264 8077
8265 rq = task_rq_lock(tsk, &flags); 8078 rq = task_rq_lock(tsk, &flags);
8266 8079
8267 update_rq_clock(rq);
8268
8269 running = task_current(rq, tsk); 8080 running = task_current(rq, tsk);
8270 on_rq = tsk->se.on_rq; 8081 on_rq = tsk->se.on_rq;
8271 8082
@@ -8284,7 +8095,7 @@ void sched_move_task(struct task_struct *tsk)
8284 if (unlikely(running)) 8095 if (unlikely(running))
8285 tsk->sched_class->set_curr_task(rq); 8096 tsk->sched_class->set_curr_task(rq);
8286 if (on_rq) 8097 if (on_rq)
8287 enqueue_task(rq, tsk, 0, false); 8098 enqueue_task(rq, tsk, 0);
8288 8099
8289 task_rq_unlock(rq, &flags); 8100 task_rq_unlock(rq, &flags);
8290} 8101}
@@ -9098,43 +8909,32 @@ struct cgroup_subsys cpuacct_subsys = {
9098 8909
9099#ifndef CONFIG_SMP 8910#ifndef CONFIG_SMP
9100 8911
9101int rcu_expedited_torture_stats(char *page)
9102{
9103 return 0;
9104}
9105EXPORT_SYMBOL_GPL(rcu_expedited_torture_stats);
9106
9107void synchronize_sched_expedited(void) 8912void synchronize_sched_expedited(void)
9108{ 8913{
8914 barrier();
9109} 8915}
9110EXPORT_SYMBOL_GPL(synchronize_sched_expedited); 8916EXPORT_SYMBOL_GPL(synchronize_sched_expedited);
9111 8917
9112#else /* #ifndef CONFIG_SMP */ 8918#else /* #ifndef CONFIG_SMP */
9113 8919
9114static DEFINE_PER_CPU(struct migration_req, rcu_migration_req); 8920static atomic_t synchronize_sched_expedited_count = ATOMIC_INIT(0);
9115static DEFINE_MUTEX(rcu_sched_expedited_mutex);
9116
9117#define RCU_EXPEDITED_STATE_POST -2
9118#define RCU_EXPEDITED_STATE_IDLE -1
9119 8921
9120static int rcu_expedited_state = RCU_EXPEDITED_STATE_IDLE; 8922static int synchronize_sched_expedited_cpu_stop(void *data)
9121
9122int rcu_expedited_torture_stats(char *page)
9123{ 8923{
9124 int cnt = 0; 8924 /*
9125 int cpu; 8925 * There must be a full memory barrier on each affected CPU
9126 8926 * between the time that try_stop_cpus() is called and the
9127 cnt += sprintf(&page[cnt], "state: %d /", rcu_expedited_state); 8927 * time that it returns.
9128 for_each_online_cpu(cpu) { 8928 *
9129 cnt += sprintf(&page[cnt], " %d:%d", 8929 * In the current initial implementation of cpu_stop, the
9130 cpu, per_cpu(rcu_migration_req, cpu).dest_cpu); 8930 * above condition is already met when the control reaches
9131 } 8931 * this point and the following smp_mb() is not strictly
9132 cnt += sprintf(&page[cnt], "\n"); 8932 * necessary. Do smp_mb() anyway for documentation and
9133 return cnt; 8933 * robustness against future implementation changes.
8934 */
8935 smp_mb(); /* See above comment block. */
8936 return 0;
9134} 8937}
9135EXPORT_SYMBOL_GPL(rcu_expedited_torture_stats);
9136
9137static long synchronize_sched_expedited_count;
9138 8938
9139/* 8939/*
9140 * Wait for an rcu-sched grace period to elapse, but use "big hammer" 8940 * Wait for an rcu-sched grace period to elapse, but use "big hammer"
@@ -9148,18 +8948,14 @@ static long synchronize_sched_expedited_count;
9148 */ 8948 */
9149void synchronize_sched_expedited(void) 8949void synchronize_sched_expedited(void)
9150{ 8950{
9151 int cpu; 8951 int snap, trycount = 0;
9152 unsigned long flags;
9153 bool need_full_sync = 0;
9154 struct rq *rq;
9155 struct migration_req *req;
9156 long snap;
9157 int trycount = 0;
9158 8952
9159 smp_mb(); /* ensure prior mod happens before capturing snap. */ 8953 smp_mb(); /* ensure prior mod happens before capturing snap. */
9160 snap = ACCESS_ONCE(synchronize_sched_expedited_count) + 1; 8954 snap = atomic_read(&synchronize_sched_expedited_count) + 1;
9161 get_online_cpus(); 8955 get_online_cpus();
9162 while (!mutex_trylock(&rcu_sched_expedited_mutex)) { 8956 while (try_stop_cpus(cpu_online_mask,
8957 synchronize_sched_expedited_cpu_stop,
8958 NULL) == -EAGAIN) {
9163 put_online_cpus(); 8959 put_online_cpus();
9164 if (trycount++ < 10) 8960 if (trycount++ < 10)
9165 udelay(trycount * num_online_cpus()); 8961 udelay(trycount * num_online_cpus());
@@ -9167,41 +8963,15 @@ void synchronize_sched_expedited(void)
9167 synchronize_sched(); 8963 synchronize_sched();
9168 return; 8964 return;
9169 } 8965 }
9170 if (ACCESS_ONCE(synchronize_sched_expedited_count) - snap > 0) { 8966 if (atomic_read(&synchronize_sched_expedited_count) - snap > 0) {
9171 smp_mb(); /* ensure test happens before caller kfree */ 8967 smp_mb(); /* ensure test happens before caller kfree */
9172 return; 8968 return;
9173 } 8969 }
9174 get_online_cpus(); 8970 get_online_cpus();
9175 } 8971 }
9176 rcu_expedited_state = RCU_EXPEDITED_STATE_POST; 8972 atomic_inc(&synchronize_sched_expedited_count);
9177 for_each_online_cpu(cpu) { 8973 smp_mb__after_atomic_inc(); /* ensure post-GP actions seen after GP. */
9178 rq = cpu_rq(cpu);
9179 req = &per_cpu(rcu_migration_req, cpu);
9180 init_completion(&req->done);
9181 req->task = NULL;
9182 req->dest_cpu = RCU_MIGRATION_NEED_QS;
9183 raw_spin_lock_irqsave(&rq->lock, flags);
9184 list_add(&req->list, &rq->migration_queue);
9185 raw_spin_unlock_irqrestore(&rq->lock, flags);
9186 wake_up_process(rq->migration_thread);
9187 }
9188 for_each_online_cpu(cpu) {
9189 rcu_expedited_state = cpu;
9190 req = &per_cpu(rcu_migration_req, cpu);
9191 rq = cpu_rq(cpu);
9192 wait_for_completion(&req->done);
9193 raw_spin_lock_irqsave(&rq->lock, flags);
9194 if (unlikely(req->dest_cpu == RCU_MIGRATION_MUST_SYNC))
9195 need_full_sync = 1;
9196 req->dest_cpu = RCU_MIGRATION_IDLE;
9197 raw_spin_unlock_irqrestore(&rq->lock, flags);
9198 }
9199 rcu_expedited_state = RCU_EXPEDITED_STATE_IDLE;
9200 synchronize_sched_expedited_count++;
9201 mutex_unlock(&rcu_sched_expedited_mutex);
9202 put_online_cpus(); 8974 put_online_cpus();
9203 if (need_full_sync)
9204 synchronize_sched();
9205} 8975}
9206EXPORT_SYMBOL_GPL(synchronize_sched_expedited); 8976EXPORT_SYMBOL_GPL(synchronize_sched_expedited);
9207 8977