aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/sched.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/sched.c')
-rw-r--r--kernel/sched.c789
1 files changed, 278 insertions, 511 deletions
diff --git a/kernel/sched.c b/kernel/sched.c
index 6af210a7de70..1d93cd0ae4d3 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>
@@ -323,6 +323,15 @@ static inline struct task_group *task_group(struct task_struct *p)
323/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */ 323/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
324static inline void set_task_rq(struct task_struct *p, unsigned int cpu) 324static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
325{ 325{
326 /*
327 * Strictly speaking this rcu_read_lock() is not needed since the
328 * task_group is tied to the cgroup, which in turn can never go away
329 * as long as there are tasks attached to it.
330 *
331 * However since task_group() uses task_subsys_state() which is an
332 * rcu_dereference() user, this quiets CONFIG_PROVE_RCU.
333 */
334 rcu_read_lock();
326#ifdef CONFIG_FAIR_GROUP_SCHED 335#ifdef CONFIG_FAIR_GROUP_SCHED
327 p->se.cfs_rq = task_group(p)->cfs_rq[cpu]; 336 p->se.cfs_rq = task_group(p)->cfs_rq[cpu];
328 p->se.parent = task_group(p)->se[cpu]; 337 p->se.parent = task_group(p)->se[cpu];
@@ -332,6 +341,7 @@ static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
332 p->rt.rt_rq = task_group(p)->rt_rq[cpu]; 341 p->rt.rt_rq = task_group(p)->rt_rq[cpu];
333 p->rt.parent = task_group(p)->rt_se[cpu]; 342 p->rt.parent = task_group(p)->rt_se[cpu];
334#endif 343#endif
344 rcu_read_unlock();
335} 345}
336 346
337#else 347#else
@@ -493,8 +503,11 @@ struct rq {
493 #define CPU_LOAD_IDX_MAX 5 503 #define CPU_LOAD_IDX_MAX 5
494 unsigned long cpu_load[CPU_LOAD_IDX_MAX]; 504 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
495#ifdef CONFIG_NO_HZ 505#ifdef CONFIG_NO_HZ
506 u64 nohz_stamp;
496 unsigned char in_nohz_recently; 507 unsigned char in_nohz_recently;
497#endif 508#endif
509 unsigned int skip_clock_update;
510
498 /* capture load from *all* tasks on this cpu: */ 511 /* capture load from *all* tasks on this cpu: */
499 struct load_weight load; 512 struct load_weight load;
500 unsigned long nr_load_updates; 513 unsigned long nr_load_updates;
@@ -536,15 +549,13 @@ struct rq {
536 int post_schedule; 549 int post_schedule;
537 int active_balance; 550 int active_balance;
538 int push_cpu; 551 int push_cpu;
552 struct cpu_stop_work active_balance_work;
539 /* cpu of this runqueue: */ 553 /* cpu of this runqueue: */
540 int cpu; 554 int cpu;
541 int online; 555 int online;
542 556
543 unsigned long avg_load_per_task; 557 unsigned long avg_load_per_task;
544 558
545 struct task_struct *migration_thread;
546 struct list_head migration_queue;
547
548 u64 rt_avg; 559 u64 rt_avg;
549 u64 age_stamp; 560 u64 age_stamp;
550 u64 idle_stamp; 561 u64 idle_stamp;
@@ -592,6 +603,13 @@ static inline
592void 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)
593{ 604{
594 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;
595} 613}
596 614
597static inline int cpu_of(struct rq *rq) 615static inline int cpu_of(struct rq *rq)
@@ -626,7 +644,8 @@ static inline int cpu_of(struct rq *rq)
626 644
627inline void update_rq_clock(struct rq *rq) 645inline void update_rq_clock(struct rq *rq)
628{ 646{
629 rq->clock = sched_clock_cpu(cpu_of(rq)); 647 if (!rq->skip_clock_update)
648 rq->clock = sched_clock_cpu(cpu_of(rq));
630} 649}
631 650
632/* 651/*
@@ -904,16 +923,12 @@ static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
904#endif /* __ARCH_WANT_UNLOCKED_CTXSW */ 923#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
905 924
906/* 925/*
907 * 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
908 * ttwu() so that task_cpu() reports a stable number. 927 * against ttwu().
909 *
910 * We need to make an exception for PF_STARTING tasks because the fork
911 * path might require task_rq_lock() to work, eg. it can call
912 * set_cpus_allowed_ptr() from the cpuset clone_ns code.
913 */ 928 */
914static inline int task_is_waking(struct task_struct *p) 929static inline int task_is_waking(struct task_struct *p)
915{ 930{
916 return unlikely((p->state == TASK_WAKING) && !(p->flags & PF_STARTING)); 931 return unlikely(p->state == TASK_WAKING);
917} 932}
918 933
919/* 934/*
@@ -926,11 +941,9 @@ static inline struct rq *__task_rq_lock(struct task_struct *p)
926 struct rq *rq; 941 struct rq *rq;
927 942
928 for (;;) { 943 for (;;) {
929 while (task_is_waking(p))
930 cpu_relax();
931 rq = task_rq(p); 944 rq = task_rq(p);
932 raw_spin_lock(&rq->lock); 945 raw_spin_lock(&rq->lock);
933 if (likely(rq == task_rq(p) && !task_is_waking(p))) 946 if (likely(rq == task_rq(p)))
934 return rq; 947 return rq;
935 raw_spin_unlock(&rq->lock); 948 raw_spin_unlock(&rq->lock);
936 } 949 }
@@ -947,12 +960,10 @@ static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
947 struct rq *rq; 960 struct rq *rq;
948 961
949 for (;;) { 962 for (;;) {
950 while (task_is_waking(p))
951 cpu_relax();
952 local_irq_save(*flags); 963 local_irq_save(*flags);
953 rq = task_rq(p); 964 rq = task_rq(p);
954 raw_spin_lock(&rq->lock); 965 raw_spin_lock(&rq->lock);
955 if (likely(rq == task_rq(p) && !task_is_waking(p))) 966 if (likely(rq == task_rq(p)))
956 return rq; 967 return rq;
957 raw_spin_unlock_irqrestore(&rq->lock, *flags); 968 raw_spin_unlock_irqrestore(&rq->lock, *flags);
958 } 969 }
@@ -1229,6 +1240,17 @@ void wake_up_idle_cpu(int cpu)
1229 if (!tsk_is_polling(rq->idle)) 1240 if (!tsk_is_polling(rq->idle))
1230 smp_send_reschedule(cpu); 1241 smp_send_reschedule(cpu);
1231} 1242}
1243
1244int nohz_ratelimit(int cpu)
1245{
1246 struct rq *rq = cpu_rq(cpu);
1247 u64 diff = rq->clock - rq->nohz_stamp;
1248
1249 rq->nohz_stamp = rq->clock;
1250
1251 return diff < (NSEC_PER_SEC / HZ) >> 1;
1252}
1253
1232#endif /* CONFIG_NO_HZ */ 1254#endif /* CONFIG_NO_HZ */
1233 1255
1234static u64 sched_avg_period(void) 1256static u64 sched_avg_period(void)
@@ -1771,8 +1793,6 @@ static void double_rq_lock(struct rq *rq1, struct rq *rq2)
1771 raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING); 1793 raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
1772 } 1794 }
1773 } 1795 }
1774 update_rq_clock(rq1);
1775 update_rq_clock(rq2);
1776} 1796}
1777 1797
1778/* 1798/*
@@ -1803,7 +1823,7 @@ static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1803} 1823}
1804#endif 1824#endif
1805 1825
1806static void calc_load_account_active(struct rq *this_rq); 1826static void calc_load_account_idle(struct rq *this_rq);
1807static void update_sysctl(void); 1827static void update_sysctl(void);
1808static int get_update_sysctl_factor(void); 1828static int get_update_sysctl_factor(void);
1809 1829
@@ -1860,62 +1880,43 @@ static void set_load_weight(struct task_struct *p)
1860 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO]; 1880 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
1861} 1881}
1862 1882
1863static void update_avg(u64 *avg, u64 sample) 1883static void enqueue_task(struct rq *rq, struct task_struct *p, int flags)
1864{
1865 s64 diff = sample - *avg;
1866 *avg += diff >> 3;
1867}
1868
1869static void
1870enqueue_task(struct rq *rq, struct task_struct *p, int wakeup, bool head)
1871{ 1884{
1872 if (wakeup) 1885 update_rq_clock(rq);
1873 p->se.start_runtime = p->se.sum_exec_runtime;
1874
1875 sched_info_queued(p); 1886 sched_info_queued(p);
1876 p->sched_class->enqueue_task(rq, p, wakeup, head); 1887 p->sched_class->enqueue_task(rq, p, flags);
1877 p->se.on_rq = 1; 1888 p->se.on_rq = 1;
1878} 1889}
1879 1890
1880static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep) 1891static void dequeue_task(struct rq *rq, struct task_struct *p, int flags)
1881{ 1892{
1882 if (sleep) { 1893 update_rq_clock(rq);
1883 if (p->se.last_wakeup) {
1884 update_avg(&p->se.avg_overlap,
1885 p->se.sum_exec_runtime - p->se.last_wakeup);
1886 p->se.last_wakeup = 0;
1887 } else {
1888 update_avg(&p->se.avg_wakeup,
1889 sysctl_sched_wakeup_granularity);
1890 }
1891 }
1892
1893 sched_info_dequeued(p); 1894 sched_info_dequeued(p);
1894 p->sched_class->dequeue_task(rq, p, sleep); 1895 p->sched_class->dequeue_task(rq, p, flags);
1895 p->se.on_rq = 0; 1896 p->se.on_rq = 0;
1896} 1897}
1897 1898
1898/* 1899/*
1899 * activate_task - move a task to the runqueue. 1900 * activate_task - move a task to the runqueue.
1900 */ 1901 */
1901static void activate_task(struct rq *rq, struct task_struct *p, int wakeup) 1902static void activate_task(struct rq *rq, struct task_struct *p, int flags)
1902{ 1903{
1903 if (task_contributes_to_load(p)) 1904 if (task_contributes_to_load(p))
1904 rq->nr_uninterruptible--; 1905 rq->nr_uninterruptible--;
1905 1906
1906 enqueue_task(rq, p, wakeup, false); 1907 enqueue_task(rq, p, flags);
1907 inc_nr_running(rq); 1908 inc_nr_running(rq);
1908} 1909}
1909 1910
1910/* 1911/*
1911 * deactivate_task - remove a task from the runqueue. 1912 * deactivate_task - remove a task from the runqueue.
1912 */ 1913 */
1913static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep) 1914static void deactivate_task(struct rq *rq, struct task_struct *p, int flags)
1914{ 1915{
1915 if (task_contributes_to_load(p)) 1916 if (task_contributes_to_load(p))
1916 rq->nr_uninterruptible++; 1917 rq->nr_uninterruptible++;
1917 1918
1918 dequeue_task(rq, p, sleep); 1919 dequeue_task(rq, p, flags);
1919 dec_nr_running(rq); 1920 dec_nr_running(rq);
1920} 1921}
1921 1922
@@ -2044,21 +2045,18 @@ void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
2044 __set_task_cpu(p, new_cpu); 2045 __set_task_cpu(p, new_cpu);
2045} 2046}
2046 2047
2047struct migration_req { 2048struct migration_arg {
2048 struct list_head list;
2049
2050 struct task_struct *task; 2049 struct task_struct *task;
2051 int dest_cpu; 2050 int dest_cpu;
2052
2053 struct completion done;
2054}; 2051};
2055 2052
2053static int migration_cpu_stop(void *data);
2054
2056/* 2055/*
2057 * The task's runqueue lock must be held. 2056 * The task's runqueue lock must be held.
2058 * Returns true if you have to wait for migration thread. 2057 * Returns true if you have to wait for migration thread.
2059 */ 2058 */
2060static int 2059static bool migrate_task(struct task_struct *p, int dest_cpu)
2061migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
2062{ 2060{
2063 struct rq *rq = task_rq(p); 2061 struct rq *rq = task_rq(p);
2064 2062
@@ -2066,58 +2064,7 @@ migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
2066 * If the task is not on a runqueue (and not running), then 2064 * If the task is not on a runqueue (and not running), then
2067 * the next wake-up will properly place the task. 2065 * the next wake-up will properly place the task.
2068 */ 2066 */
2069 if (!p->se.on_rq && !task_running(rq, p)) 2067 return p->se.on_rq || task_running(rq, p);
2070 return 0;
2071
2072 init_completion(&req->done);
2073 req->task = p;
2074 req->dest_cpu = dest_cpu;
2075 list_add(&req->list, &rq->migration_queue);
2076
2077 return 1;
2078}
2079
2080/*
2081 * wait_task_context_switch - wait for a thread to complete at least one
2082 * context switch.
2083 *
2084 * @p must not be current.
2085 */
2086void wait_task_context_switch(struct task_struct *p)
2087{
2088 unsigned long nvcsw, nivcsw, flags;
2089 int running;
2090 struct rq *rq;
2091
2092 nvcsw = p->nvcsw;
2093 nivcsw = p->nivcsw;
2094 for (;;) {
2095 /*
2096 * The runqueue is assigned before the actual context
2097 * switch. We need to take the runqueue lock.
2098 *
2099 * We could check initially without the lock but it is
2100 * very likely that we need to take the lock in every
2101 * iteration.
2102 */
2103 rq = task_rq_lock(p, &flags);
2104 running = task_running(rq, p);
2105 task_rq_unlock(rq, &flags);
2106
2107 if (likely(!running))
2108 break;
2109 /*
2110 * The switch count is incremented before the actual
2111 * context switch. We thus wait for two switches to be
2112 * sure at least one completed.
2113 */
2114 if ((p->nvcsw - nvcsw) > 1)
2115 break;
2116 if ((p->nivcsw - nivcsw) > 1)
2117 break;
2118
2119 cpu_relax();
2120 }
2121} 2068}
2122 2069
2123/* 2070/*
@@ -2175,7 +2122,7 @@ unsigned long wait_task_inactive(struct task_struct *p, long match_state)
2175 * just go back and repeat. 2122 * just go back and repeat.
2176 */ 2123 */
2177 rq = task_rq_lock(p, &flags); 2124 rq = task_rq_lock(p, &flags);
2178 trace_sched_wait_task(rq, p); 2125 trace_sched_wait_task(p);
2179 running = task_running(rq, p); 2126 running = task_running(rq, p);
2180 on_rq = p->se.on_rq; 2127 on_rq = p->se.on_rq;
2181 ncsw = 0; 2128 ncsw = 0;
@@ -2273,6 +2220,9 @@ void task_oncpu_function_call(struct task_struct *p,
2273} 2220}
2274 2221
2275#ifdef CONFIG_SMP 2222#ifdef CONFIG_SMP
2223/*
2224 * ->cpus_allowed is protected by either TASK_WAKING or rq->lock held.
2225 */
2276static int select_fallback_rq(int cpu, struct task_struct *p) 2226static int select_fallback_rq(int cpu, struct task_struct *p)
2277{ 2227{
2278 int dest_cpu; 2228 int dest_cpu;
@@ -2289,12 +2239,8 @@ static int select_fallback_rq(int cpu, struct task_struct *p)
2289 return dest_cpu; 2239 return dest_cpu;
2290 2240
2291 /* No more Mr. Nice Guy. */ 2241 /* No more Mr. Nice Guy. */
2292 if (dest_cpu >= nr_cpu_ids) { 2242 if (unlikely(dest_cpu >= nr_cpu_ids)) {
2293 rcu_read_lock(); 2243 dest_cpu = cpuset_cpus_allowed_fallback(p);
2294 cpuset_cpus_allowed_locked(p, &p->cpus_allowed);
2295 rcu_read_unlock();
2296 dest_cpu = cpumask_any_and(cpu_active_mask, &p->cpus_allowed);
2297
2298 /* 2244 /*
2299 * Don't tell them about moving exiting tasks or 2245 * Don't tell them about moving exiting tasks or
2300 * kernel threads (both mm NULL), since they never 2246 * kernel threads (both mm NULL), since they never
@@ -2311,17 +2257,12 @@ static int select_fallback_rq(int cpu, struct task_struct *p)
2311} 2257}
2312 2258
2313/* 2259/*
2314 * Gets called from 3 sites (exec, fork, wakeup), since it is called without 2260 * The caller (fork, wakeup) owns TASK_WAKING, ->cpus_allowed is stable.
2315 * holding rq->lock we need to ensure ->cpus_allowed is stable, this is done
2316 * by:
2317 *
2318 * exec: is unstable, retry loop
2319 * fork & wake-up: serialize ->cpus_allowed against TASK_WAKING
2320 */ 2261 */
2321static inline 2262static inline
2322int select_task_rq(struct task_struct *p, int sd_flags, int wake_flags) 2263int select_task_rq(struct rq *rq, struct task_struct *p, int sd_flags, int wake_flags)
2323{ 2264{
2324 int cpu = p->sched_class->select_task_rq(p, sd_flags, wake_flags); 2265 int cpu = p->sched_class->select_task_rq(rq, p, sd_flags, wake_flags);
2325 2266
2326 /* 2267 /*
2327 * In order not to call set_task_cpu() on a blocking task we need 2268 * In order not to call set_task_cpu() on a blocking task we need
@@ -2339,6 +2280,12 @@ int select_task_rq(struct task_struct *p, int sd_flags, int wake_flags)
2339 2280
2340 return cpu; 2281 return cpu;
2341} 2282}
2283
2284static void update_avg(u64 *avg, u64 sample)
2285{
2286 s64 diff = sample - *avg;
2287 *avg += diff >> 3;
2288}
2342#endif 2289#endif
2343 2290
2344/*** 2291/***
@@ -2360,16 +2307,13 @@ static int try_to_wake_up(struct task_struct *p, unsigned int state,
2360{ 2307{
2361 int cpu, orig_cpu, this_cpu, success = 0; 2308 int cpu, orig_cpu, this_cpu, success = 0;
2362 unsigned long flags; 2309 unsigned long flags;
2310 unsigned long en_flags = ENQUEUE_WAKEUP;
2363 struct rq *rq; 2311 struct rq *rq;
2364 2312
2365 if (!sched_feat(SYNC_WAKEUPS))
2366 wake_flags &= ~WF_SYNC;
2367
2368 this_cpu = get_cpu(); 2313 this_cpu = get_cpu();
2369 2314
2370 smp_wmb(); 2315 smp_wmb();
2371 rq = task_rq_lock(p, &flags); 2316 rq = task_rq_lock(p, &flags);
2372 update_rq_clock(rq);
2373 if (!(p->state & state)) 2317 if (!(p->state & state))
2374 goto out; 2318 goto out;
2375 2319
@@ -2389,28 +2333,26 @@ static int try_to_wake_up(struct task_struct *p, unsigned int state,
2389 * 2333 *
2390 * First fix up the nr_uninterruptible count: 2334 * First fix up the nr_uninterruptible count:
2391 */ 2335 */
2392 if (task_contributes_to_load(p)) 2336 if (task_contributes_to_load(p)) {
2393 rq->nr_uninterruptible--; 2337 if (likely(cpu_online(orig_cpu)))
2338 rq->nr_uninterruptible--;
2339 else
2340 this_rq()->nr_uninterruptible--;
2341 }
2394 p->state = TASK_WAKING; 2342 p->state = TASK_WAKING;
2395 2343
2396 if (p->sched_class->task_waking) 2344 if (p->sched_class->task_waking) {
2397 p->sched_class->task_waking(rq, p); 2345 p->sched_class->task_waking(rq, p);
2346 en_flags |= ENQUEUE_WAKING;
2347 }
2398 2348
2399 __task_rq_unlock(rq); 2349 cpu = select_task_rq(rq, p, SD_BALANCE_WAKE, wake_flags);
2400 2350 if (cpu != orig_cpu)
2401 cpu = select_task_rq(p, SD_BALANCE_WAKE, wake_flags);
2402 if (cpu != orig_cpu) {
2403 /*
2404 * Since we migrate the task without holding any rq->lock,
2405 * we need to be careful with task_rq_lock(), since that
2406 * might end up locking an invalid rq.
2407 */
2408 set_task_cpu(p, cpu); 2351 set_task_cpu(p, cpu);
2409 } 2352 __task_rq_unlock(rq);
2410 2353
2411 rq = cpu_rq(cpu); 2354 rq = cpu_rq(cpu);
2412 raw_spin_lock(&rq->lock); 2355 raw_spin_lock(&rq->lock);
2413 update_rq_clock(rq);
2414 2356
2415 /* 2357 /*
2416 * We migrated the task without holding either rq->lock, however 2358 * We migrated the task without holding either rq->lock, however
@@ -2438,36 +2380,20 @@ static int try_to_wake_up(struct task_struct *p, unsigned int state,
2438 2380
2439out_activate: 2381out_activate:
2440#endif /* CONFIG_SMP */ 2382#endif /* CONFIG_SMP */
2441 schedstat_inc(p, se.nr_wakeups); 2383 schedstat_inc(p, se.statistics.nr_wakeups);
2442 if (wake_flags & WF_SYNC) 2384 if (wake_flags & WF_SYNC)
2443 schedstat_inc(p, se.nr_wakeups_sync); 2385 schedstat_inc(p, se.statistics.nr_wakeups_sync);
2444 if (orig_cpu != cpu) 2386 if (orig_cpu != cpu)
2445 schedstat_inc(p, se.nr_wakeups_migrate); 2387 schedstat_inc(p, se.statistics.nr_wakeups_migrate);
2446 if (cpu == this_cpu) 2388 if (cpu == this_cpu)
2447 schedstat_inc(p, se.nr_wakeups_local); 2389 schedstat_inc(p, se.statistics.nr_wakeups_local);
2448 else 2390 else
2449 schedstat_inc(p, se.nr_wakeups_remote); 2391 schedstat_inc(p, se.statistics.nr_wakeups_remote);
2450 activate_task(rq, p, 1); 2392 activate_task(rq, p, en_flags);
2451 success = 1; 2393 success = 1;
2452 2394
2453 /*
2454 * Only attribute actual wakeups done by this task.
2455 */
2456 if (!in_interrupt()) {
2457 struct sched_entity *se = &current->se;
2458 u64 sample = se->sum_exec_runtime;
2459
2460 if (se->last_wakeup)
2461 sample -= se->last_wakeup;
2462 else
2463 sample -= se->start_runtime;
2464 update_avg(&se->avg_wakeup, sample);
2465
2466 se->last_wakeup = se->sum_exec_runtime;
2467 }
2468
2469out_running: 2395out_running:
2470 trace_sched_wakeup(rq, p, success); 2396 trace_sched_wakeup(p, success);
2471 check_preempt_curr(rq, p, wake_flags); 2397 check_preempt_curr(rq, p, wake_flags);
2472 2398
2473 p->state = TASK_RUNNING; 2399 p->state = TASK_RUNNING;
@@ -2527,42 +2453,9 @@ static void __sched_fork(struct task_struct *p)
2527 p->se.sum_exec_runtime = 0; 2453 p->se.sum_exec_runtime = 0;
2528 p->se.prev_sum_exec_runtime = 0; 2454 p->se.prev_sum_exec_runtime = 0;
2529 p->se.nr_migrations = 0; 2455 p->se.nr_migrations = 0;
2530 p->se.last_wakeup = 0;
2531 p->se.avg_overlap = 0;
2532 p->se.start_runtime = 0;
2533 p->se.avg_wakeup = sysctl_sched_wakeup_granularity;
2534 2456
2535#ifdef CONFIG_SCHEDSTATS 2457#ifdef CONFIG_SCHEDSTATS
2536 p->se.wait_start = 0; 2458 memset(&p->se.statistics, 0, sizeof(p->se.statistics));
2537 p->se.wait_max = 0;
2538 p->se.wait_count = 0;
2539 p->se.wait_sum = 0;
2540
2541 p->se.sleep_start = 0;
2542 p->se.sleep_max = 0;
2543 p->se.sum_sleep_runtime = 0;
2544
2545 p->se.block_start = 0;
2546 p->se.block_max = 0;
2547 p->se.exec_max = 0;
2548 p->se.slice_max = 0;
2549
2550 p->se.nr_migrations_cold = 0;
2551 p->se.nr_failed_migrations_affine = 0;
2552 p->se.nr_failed_migrations_running = 0;
2553 p->se.nr_failed_migrations_hot = 0;
2554 p->se.nr_forced_migrations = 0;
2555
2556 p->se.nr_wakeups = 0;
2557 p->se.nr_wakeups_sync = 0;
2558 p->se.nr_wakeups_migrate = 0;
2559 p->se.nr_wakeups_local = 0;
2560 p->se.nr_wakeups_remote = 0;
2561 p->se.nr_wakeups_affine = 0;
2562 p->se.nr_wakeups_affine_attempts = 0;
2563 p->se.nr_wakeups_passive = 0;
2564 p->se.nr_wakeups_idle = 0;
2565
2566#endif 2459#endif
2567 2460
2568 INIT_LIST_HEAD(&p->rt.run_list); 2461 INIT_LIST_HEAD(&p->rt.run_list);
@@ -2583,11 +2476,11 @@ void sched_fork(struct task_struct *p, int clone_flags)
2583 2476
2584 __sched_fork(p); 2477 __sched_fork(p);
2585 /* 2478 /*
2586 * We mark the process as waking here. This guarantees that 2479 * We mark the process as running here. This guarantees that
2587 * nobody will actually run it, and a signal or other external 2480 * nobody will actually run it, and a signal or other external
2588 * event cannot wake it up and insert it on the runqueue either. 2481 * event cannot wake it up and insert it on the runqueue either.
2589 */ 2482 */
2590 p->state = TASK_WAKING; 2483 p->state = TASK_RUNNING;
2591 2484
2592 /* 2485 /*
2593 * Revert to default priority/policy on fork if requested. 2486 * Revert to default priority/policy on fork if requested.
@@ -2654,31 +2547,27 @@ void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
2654 int cpu __maybe_unused = get_cpu(); 2547 int cpu __maybe_unused = get_cpu();
2655 2548
2656#ifdef CONFIG_SMP 2549#ifdef CONFIG_SMP
2550 rq = task_rq_lock(p, &flags);
2551 p->state = TASK_WAKING;
2552
2657 /* 2553 /*
2658 * Fork balancing, do it here and not earlier because: 2554 * Fork balancing, do it here and not earlier because:
2659 * - cpus_allowed can change in the fork path 2555 * - cpus_allowed can change in the fork path
2660 * - any previously selected cpu might disappear through hotplug 2556 * - any previously selected cpu might disappear through hotplug
2661 * 2557 *
2662 * We still have TASK_WAKING but PF_STARTING is gone now, meaning 2558 * We set TASK_WAKING so that select_task_rq() can drop rq->lock
2663 * ->cpus_allowed is stable, we have preemption disabled, meaning 2559 * without people poking at ->cpus_allowed.
2664 * cpu_online_mask is stable.
2665 */ 2560 */
2666 cpu = select_task_rq(p, SD_BALANCE_FORK, 0); 2561 cpu = select_task_rq(rq, p, SD_BALANCE_FORK, 0);
2667 set_task_cpu(p, cpu); 2562 set_task_cpu(p, cpu);
2668#endif
2669 2563
2670 /*
2671 * Since the task is not on the rq and we still have TASK_WAKING set
2672 * nobody else will migrate this task.
2673 */
2674 rq = cpu_rq(cpu);
2675 raw_spin_lock_irqsave(&rq->lock, flags);
2676
2677 BUG_ON(p->state != TASK_WAKING);
2678 p->state = TASK_RUNNING; 2564 p->state = TASK_RUNNING;
2679 update_rq_clock(rq); 2565 task_rq_unlock(rq, &flags);
2566#endif
2567
2568 rq = task_rq_lock(p, &flags);
2680 activate_task(rq, p, 0); 2569 activate_task(rq, p, 0);
2681 trace_sched_wakeup_new(rq, p, 1); 2570 trace_sched_wakeup_new(p, 1);
2682 check_preempt_curr(rq, p, WF_FORK); 2571 check_preempt_curr(rq, p, WF_FORK);
2683#ifdef CONFIG_SMP 2572#ifdef CONFIG_SMP
2684 if (p->sched_class->task_woken) 2573 if (p->sched_class->task_woken)
@@ -2898,7 +2787,7 @@ context_switch(struct rq *rq, struct task_struct *prev,
2898 struct mm_struct *mm, *oldmm; 2787 struct mm_struct *mm, *oldmm;
2899 2788
2900 prepare_task_switch(rq, prev, next); 2789 prepare_task_switch(rq, prev, next);
2901 trace_sched_switch(rq, prev, next); 2790 trace_sched_switch(prev, next);
2902 mm = next->mm; 2791 mm = next->mm;
2903 oldmm = prev->active_mm; 2792 oldmm = prev->active_mm;
2904 /* 2793 /*
@@ -3015,6 +2904,61 @@ static unsigned long calc_load_update;
3015unsigned long avenrun[3]; 2904unsigned long avenrun[3];
3016EXPORT_SYMBOL(avenrun); 2905EXPORT_SYMBOL(avenrun);
3017 2906
2907static long calc_load_fold_active(struct rq *this_rq)
2908{
2909 long nr_active, delta = 0;
2910
2911 nr_active = this_rq->nr_running;
2912 nr_active += (long) this_rq->nr_uninterruptible;
2913
2914 if (nr_active != this_rq->calc_load_active) {
2915 delta = nr_active - this_rq->calc_load_active;
2916 this_rq->calc_load_active = nr_active;
2917 }
2918
2919 return delta;
2920}
2921
2922#ifdef CONFIG_NO_HZ
2923/*
2924 * For NO_HZ we delay the active fold to the next LOAD_FREQ update.
2925 *
2926 * When making the ILB scale, we should try to pull this in as well.
2927 */
2928static atomic_long_t calc_load_tasks_idle;
2929
2930static void calc_load_account_idle(struct rq *this_rq)
2931{
2932 long delta;
2933
2934 delta = calc_load_fold_active(this_rq);
2935 if (delta)
2936 atomic_long_add(delta, &calc_load_tasks_idle);
2937}
2938
2939static long calc_load_fold_idle(void)
2940{
2941 long delta = 0;
2942
2943 /*
2944 * Its got a race, we don't care...
2945 */
2946 if (atomic_long_read(&calc_load_tasks_idle))
2947 delta = atomic_long_xchg(&calc_load_tasks_idle, 0);
2948
2949 return delta;
2950}
2951#else
2952static void calc_load_account_idle(struct rq *this_rq)
2953{
2954}
2955
2956static inline long calc_load_fold_idle(void)
2957{
2958 return 0;
2959}
2960#endif
2961
3018/** 2962/**
3019 * get_avenrun - get the load average array 2963 * get_avenrun - get the load average array
3020 * @loads: pointer to dest load array 2964 * @loads: pointer to dest load array
@@ -3061,20 +3005,22 @@ void calc_global_load(void)
3061} 3005}
3062 3006
3063/* 3007/*
3064 * Either called from update_cpu_load() or from a cpu going idle 3008 * Called from update_cpu_load() to periodically update this CPU's
3009 * active count.
3065 */ 3010 */
3066static void calc_load_account_active(struct rq *this_rq) 3011static void calc_load_account_active(struct rq *this_rq)
3067{ 3012{
3068 long nr_active, delta; 3013 long delta;
3069 3014
3070 nr_active = this_rq->nr_running; 3015 if (time_before(jiffies, this_rq->calc_load_update))
3071 nr_active += (long) this_rq->nr_uninterruptible; 3016 return;
3072 3017
3073 if (nr_active != this_rq->calc_load_active) { 3018 delta = calc_load_fold_active(this_rq);
3074 delta = nr_active - this_rq->calc_load_active; 3019 delta += calc_load_fold_idle();
3075 this_rq->calc_load_active = nr_active; 3020 if (delta)
3076 atomic_long_add(delta, &calc_load_tasks); 3021 atomic_long_add(delta, &calc_load_tasks);
3077 } 3022
3023 this_rq->calc_load_update += LOAD_FREQ;
3078} 3024}
3079 3025
3080/* 3026/*
@@ -3106,10 +3052,7 @@ static void update_cpu_load(struct rq *this_rq)
3106 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i; 3052 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
3107 } 3053 }
3108 3054
3109 if (time_after_eq(jiffies, this_rq->calc_load_update)) { 3055 calc_load_account_active(this_rq);
3110 this_rq->calc_load_update += LOAD_FREQ;
3111 calc_load_account_active(this_rq);
3112 }
3113} 3056}
3114 3057
3115#ifdef CONFIG_SMP 3058#ifdef CONFIG_SMP
@@ -3121,44 +3064,27 @@ static void update_cpu_load(struct rq *this_rq)
3121void sched_exec(void) 3064void sched_exec(void)
3122{ 3065{
3123 struct task_struct *p = current; 3066 struct task_struct *p = current;
3124 struct migration_req req;
3125 int dest_cpu, this_cpu;
3126 unsigned long flags; 3067 unsigned long flags;
3127 struct rq *rq; 3068 struct rq *rq;
3128 3069 int dest_cpu;
3129again:
3130 this_cpu = get_cpu();
3131 dest_cpu = select_task_rq(p, SD_BALANCE_EXEC, 0);
3132 if (dest_cpu == this_cpu) {
3133 put_cpu();
3134 return;
3135 }
3136 3070
3137 rq = task_rq_lock(p, &flags); 3071 rq = task_rq_lock(p, &flags);
3138 put_cpu(); 3072 dest_cpu = p->sched_class->select_task_rq(rq, p, SD_BALANCE_EXEC, 0);
3073 if (dest_cpu == smp_processor_id())
3074 goto unlock;
3139 3075
3140 /* 3076 /*
3141 * select_task_rq() can race against ->cpus_allowed 3077 * select_task_rq() can race against ->cpus_allowed
3142 */ 3078 */
3143 if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed) 3079 if (cpumask_test_cpu(dest_cpu, &p->cpus_allowed) &&
3144 || unlikely(!cpu_active(dest_cpu))) { 3080 likely(cpu_active(dest_cpu)) && migrate_task(p, dest_cpu)) {
3145 task_rq_unlock(rq, &flags); 3081 struct migration_arg arg = { p, dest_cpu };
3146 goto again;
3147 }
3148
3149 /* force the process onto the specified CPU */
3150 if (migrate_task(p, dest_cpu, &req)) {
3151 /* Need to wait for migration thread (might exit: take ref). */
3152 struct task_struct *mt = rq->migration_thread;
3153 3082
3154 get_task_struct(mt);
3155 task_rq_unlock(rq, &flags); 3083 task_rq_unlock(rq, &flags);
3156 wake_up_process(mt); 3084 stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
3157 put_task_struct(mt);
3158 wait_for_completion(&req.done);
3159
3160 return; 3085 return;
3161 } 3086 }
3087unlock:
3162 task_rq_unlock(rq, &flags); 3088 task_rq_unlock(rq, &flags);
3163} 3089}
3164 3090
@@ -3630,23 +3556,9 @@ static inline void schedule_debug(struct task_struct *prev)
3630 3556
3631static void put_prev_task(struct rq *rq, struct task_struct *prev) 3557static void put_prev_task(struct rq *rq, struct task_struct *prev)
3632{ 3558{
3633 if (prev->state == TASK_RUNNING) { 3559 if (prev->se.on_rq)
3634 u64 runtime = prev->se.sum_exec_runtime; 3560 update_rq_clock(rq);
3635 3561 rq->skip_clock_update = 0;
3636 runtime -= prev->se.prev_sum_exec_runtime;
3637 runtime = min_t(u64, runtime, 2*sysctl_sched_migration_cost);
3638
3639 /*
3640 * In order to avoid avg_overlap growing stale when we are
3641 * indeed overlapping and hence not getting put to sleep, grow
3642 * the avg_overlap on preemption.
3643 *
3644 * We use the average preemption runtime because that
3645 * correlates to the amount of cache footprint a task can
3646 * build up.
3647 */
3648 update_avg(&prev->se.avg_overlap, runtime);
3649 }
3650 prev->sched_class->put_prev_task(rq, prev); 3562 prev->sched_class->put_prev_task(rq, prev);
3651} 3563}
3652 3564
@@ -3696,7 +3608,7 @@ need_resched:
3696 preempt_disable(); 3608 preempt_disable();
3697 cpu = smp_processor_id(); 3609 cpu = smp_processor_id();
3698 rq = cpu_rq(cpu); 3610 rq = cpu_rq(cpu);
3699 rcu_sched_qs(cpu); 3611 rcu_note_context_switch(cpu);
3700 prev = rq->curr; 3612 prev = rq->curr;
3701 switch_count = &prev->nivcsw; 3613 switch_count = &prev->nivcsw;
3702 3614
@@ -3709,14 +3621,13 @@ need_resched_nonpreemptible:
3709 hrtick_clear(rq); 3621 hrtick_clear(rq);
3710 3622
3711 raw_spin_lock_irq(&rq->lock); 3623 raw_spin_lock_irq(&rq->lock);
3712 update_rq_clock(rq);
3713 clear_tsk_need_resched(prev); 3624 clear_tsk_need_resched(prev);
3714 3625
3715 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) { 3626 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
3716 if (unlikely(signal_pending_state(prev->state, prev))) 3627 if (unlikely(signal_pending_state(prev->state, prev)))
3717 prev->state = TASK_RUNNING; 3628 prev->state = TASK_RUNNING;
3718 else 3629 else
3719 deactivate_task(rq, prev, 1); 3630 deactivate_task(rq, prev, DEQUEUE_SLEEP);
3720 switch_count = &prev->nvcsw; 3631 switch_count = &prev->nvcsw;
3721 } 3632 }
3722 3633
@@ -3780,7 +3691,7 @@ int mutex_spin_on_owner(struct mutex *lock, struct thread_info *owner)
3780 * the mutex owner just released it and exited. 3691 * the mutex owner just released it and exited.
3781 */ 3692 */
3782 if (probe_kernel_address(&owner->cpu, cpu)) 3693 if (probe_kernel_address(&owner->cpu, cpu))
3783 goto out; 3694 return 0;
3784#else 3695#else
3785 cpu = owner->cpu; 3696 cpu = owner->cpu;
3786#endif 3697#endif
@@ -3790,14 +3701,14 @@ int mutex_spin_on_owner(struct mutex *lock, struct thread_info *owner)
3790 * the cpu field may no longer be valid. 3701 * the cpu field may no longer be valid.
3791 */ 3702 */
3792 if (cpu >= nr_cpumask_bits) 3703 if (cpu >= nr_cpumask_bits)
3793 goto out; 3704 return 0;
3794 3705
3795 /* 3706 /*
3796 * We need to validate that we can do a 3707 * We need to validate that we can do a
3797 * get_cpu() and that we have the percpu area. 3708 * get_cpu() and that we have the percpu area.
3798 */ 3709 */
3799 if (!cpu_online(cpu)) 3710 if (!cpu_online(cpu))
3800 goto out; 3711 return 0;
3801 3712
3802 rq = cpu_rq(cpu); 3713 rq = cpu_rq(cpu);
3803 3714
@@ -3816,7 +3727,7 @@ int mutex_spin_on_owner(struct mutex *lock, struct thread_info *owner)
3816 3727
3817 cpu_relax(); 3728 cpu_relax();
3818 } 3729 }
3819out: 3730
3820 return 1; 3731 return 1;
3821} 3732}
3822#endif 3733#endif
@@ -4039,8 +3950,7 @@ do_wait_for_common(struct completion *x, long timeout, int state)
4039 if (!x->done) { 3950 if (!x->done) {
4040 DECLARE_WAITQUEUE(wait, current); 3951 DECLARE_WAITQUEUE(wait, current);
4041 3952
4042 wait.flags |= WQ_FLAG_EXCLUSIVE; 3953 __add_wait_queue_tail_exclusive(&x->wait, &wait);
4043 __add_wait_queue_tail(&x->wait, &wait);
4044 do { 3954 do {
4045 if (signal_pending_state(state, current)) { 3955 if (signal_pending_state(state, current)) {
4046 timeout = -ERESTARTSYS; 3956 timeout = -ERESTARTSYS;
@@ -4266,7 +4176,6 @@ void rt_mutex_setprio(struct task_struct *p, int prio)
4266 BUG_ON(prio < 0 || prio > MAX_PRIO); 4176 BUG_ON(prio < 0 || prio > MAX_PRIO);
4267 4177
4268 rq = task_rq_lock(p, &flags); 4178 rq = task_rq_lock(p, &flags);
4269 update_rq_clock(rq);
4270 4179
4271 oldprio = p->prio; 4180 oldprio = p->prio;
4272 prev_class = p->sched_class; 4181 prev_class = p->sched_class;
@@ -4287,7 +4196,7 @@ void rt_mutex_setprio(struct task_struct *p, int prio)
4287 if (running) 4196 if (running)
4288 p->sched_class->set_curr_task(rq); 4197 p->sched_class->set_curr_task(rq);
4289 if (on_rq) { 4198 if (on_rq) {
4290 enqueue_task(rq, p, 0, oldprio < prio); 4199 enqueue_task(rq, p, oldprio < prio ? ENQUEUE_HEAD : 0);
4291 4200
4292 check_class_changed(rq, p, prev_class, oldprio, running); 4201 check_class_changed(rq, p, prev_class, oldprio, running);
4293 } 4202 }
@@ -4309,7 +4218,6 @@ void set_user_nice(struct task_struct *p, long nice)
4309 * the task might be in the middle of scheduling on another CPU. 4218 * the task might be in the middle of scheduling on another CPU.
4310 */ 4219 */
4311 rq = task_rq_lock(p, &flags); 4220 rq = task_rq_lock(p, &flags);
4312 update_rq_clock(rq);
4313 /* 4221 /*
4314 * The RT priorities are set via sched_setscheduler(), but we still 4222 * The RT priorities are set via sched_setscheduler(), but we still
4315 * allow the 'normal' nice value to be set - but as expected 4223 * allow the 'normal' nice value to be set - but as expected
@@ -4331,7 +4239,7 @@ void set_user_nice(struct task_struct *p, long nice)
4331 delta = p->prio - old_prio; 4239 delta = p->prio - old_prio;
4332 4240
4333 if (on_rq) { 4241 if (on_rq) {
4334 enqueue_task(rq, p, 0, false); 4242 enqueue_task(rq, p, 0);
4335 /* 4243 /*
4336 * If the task increased its priority or is running and 4244 * If the task increased its priority or is running and
4337 * lowered its priority, then reschedule its CPU: 4245 * lowered its priority, then reschedule its CPU:
@@ -4592,7 +4500,6 @@ recheck:
4592 raw_spin_unlock_irqrestore(&p->pi_lock, flags); 4500 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
4593 goto recheck; 4501 goto recheck;
4594 } 4502 }
4595 update_rq_clock(rq);
4596 on_rq = p->se.on_rq; 4503 on_rq = p->se.on_rq;
4597 running = task_current(rq, p); 4504 running = task_current(rq, p);
4598 if (on_rq) 4505 if (on_rq)
@@ -5329,17 +5236,15 @@ static inline void sched_init_granularity(void)
5329/* 5236/*
5330 * This is how migration works: 5237 * This is how migration works:
5331 * 5238 *
5332 * 1) we queue a struct migration_req structure in the source CPU's 5239 * 1) we invoke migration_cpu_stop() on the target CPU using
5333 * runqueue and wake up that CPU's migration thread. 5240 * stop_one_cpu().
5334 * 2) we down() the locked semaphore => thread blocks. 5241 * 2) stopper starts to run (implicitly forcing the migrated thread
5335 * 3) migration thread wakes up (implicitly it forces the migrated 5242 * off the CPU)
5336 * thread off the CPU) 5243 * 3) it checks whether the migrated task is still in the wrong runqueue.
5337 * 4) it gets the migration request and checks whether the migrated 5244 * 4) if it's in the wrong runqueue then the migration thread removes
5338 * task is still in the wrong runqueue.
5339 * 5) if it's in the wrong runqueue then the migration thread removes
5340 * it and puts it into the right queue. 5245 * it and puts it into the right queue.
5341 * 6) migration thread up()s the semaphore. 5246 * 5) stopper completes and stop_one_cpu() returns and the migration
5342 * 7) we wake up and the migration is done. 5247 * is done.
5343 */ 5248 */
5344 5249
5345/* 5250/*
@@ -5353,12 +5258,23 @@ static inline void sched_init_granularity(void)
5353 */ 5258 */
5354int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask) 5259int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
5355{ 5260{
5356 struct migration_req req;
5357 unsigned long flags; 5261 unsigned long flags;
5358 struct rq *rq; 5262 struct rq *rq;
5263 unsigned int dest_cpu;
5359 int ret = 0; 5264 int ret = 0;
5360 5265
5266 /*
5267 * Serialize against TASK_WAKING so that ttwu() and wunt() can
5268 * drop the rq->lock and still rely on ->cpus_allowed.
5269 */
5270again:
5271 while (task_is_waking(p))
5272 cpu_relax();
5361 rq = task_rq_lock(p, &flags); 5273 rq = task_rq_lock(p, &flags);
5274 if (task_is_waking(p)) {
5275 task_rq_unlock(rq, &flags);
5276 goto again;
5277 }
5362 5278
5363 if (!cpumask_intersects(new_mask, cpu_active_mask)) { 5279 if (!cpumask_intersects(new_mask, cpu_active_mask)) {
5364 ret = -EINVAL; 5280 ret = -EINVAL;
@@ -5382,15 +5298,12 @@ int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
5382 if (cpumask_test_cpu(task_cpu(p), new_mask)) 5298 if (cpumask_test_cpu(task_cpu(p), new_mask))
5383 goto out; 5299 goto out;
5384 5300
5385 if (migrate_task(p, cpumask_any_and(cpu_active_mask, new_mask), &req)) { 5301 dest_cpu = cpumask_any_and(cpu_active_mask, new_mask);
5302 if (migrate_task(p, dest_cpu)) {
5303 struct migration_arg arg = { p, dest_cpu };
5386 /* Need help from migration thread: drop lock and wait. */ 5304 /* Need help from migration thread: drop lock and wait. */
5387 struct task_struct *mt = rq->migration_thread;
5388
5389 get_task_struct(mt);
5390 task_rq_unlock(rq, &flags); 5305 task_rq_unlock(rq, &flags);
5391 wake_up_process(mt); 5306 stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
5392 put_task_struct(mt);
5393 wait_for_completion(&req.done);
5394 tlb_migrate_finish(p->mm); 5307 tlb_migrate_finish(p->mm);
5395 return 0; 5308 return 0;
5396 } 5309 }
@@ -5448,98 +5361,49 @@ fail:
5448 return ret; 5361 return ret;
5449} 5362}
5450 5363
5451#define RCU_MIGRATION_IDLE 0
5452#define RCU_MIGRATION_NEED_QS 1
5453#define RCU_MIGRATION_GOT_QS 2
5454#define RCU_MIGRATION_MUST_SYNC 3
5455
5456/* 5364/*
5457 * migration_thread - this is a highprio system thread that performs 5365 * migration_cpu_stop - this will be executed by a highprio stopper thread
5458 * thread migration by bumping thread off CPU then 'pushing' onto 5366 * and performs thread migration by bumping thread off CPU then
5459 * another runqueue. 5367 * 'pushing' onto another runqueue.
5460 */ 5368 */
5461static int migration_thread(void *data) 5369static int migration_cpu_stop(void *data)
5462{
5463 int badcpu;
5464 int cpu = (long)data;
5465 struct rq *rq;
5466
5467 rq = cpu_rq(cpu);
5468 BUG_ON(rq->migration_thread != current);
5469
5470 set_current_state(TASK_INTERRUPTIBLE);
5471 while (!kthread_should_stop()) {
5472 struct migration_req *req;
5473 struct list_head *head;
5474
5475 raw_spin_lock_irq(&rq->lock);
5476
5477 if (cpu_is_offline(cpu)) {
5478 raw_spin_unlock_irq(&rq->lock);
5479 break;
5480 }
5481
5482 if (rq->active_balance) {
5483 active_load_balance(rq, cpu);
5484 rq->active_balance = 0;
5485 }
5486
5487 head = &rq->migration_queue;
5488
5489 if (list_empty(head)) {
5490 raw_spin_unlock_irq(&rq->lock);
5491 schedule();
5492 set_current_state(TASK_INTERRUPTIBLE);
5493 continue;
5494 }
5495 req = list_entry(head->next, struct migration_req, list);
5496 list_del_init(head->next);
5497
5498 if (req->task != NULL) {
5499 raw_spin_unlock(&rq->lock);
5500 __migrate_task(req->task, cpu, req->dest_cpu);
5501 } else if (likely(cpu == (badcpu = smp_processor_id()))) {
5502 req->dest_cpu = RCU_MIGRATION_GOT_QS;
5503 raw_spin_unlock(&rq->lock);
5504 } else {
5505 req->dest_cpu = RCU_MIGRATION_MUST_SYNC;
5506 raw_spin_unlock(&rq->lock);
5507 WARN_ONCE(1, "migration_thread() on CPU %d, expected %d\n", badcpu, cpu);
5508 }
5509 local_irq_enable();
5510
5511 complete(&req->done);
5512 }
5513 __set_current_state(TASK_RUNNING);
5514
5515 return 0;
5516}
5517
5518#ifdef CONFIG_HOTPLUG_CPU
5519
5520static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
5521{ 5370{
5522 int ret; 5371 struct migration_arg *arg = data;
5523 5372
5373 /*
5374 * The original target cpu might have gone down and we might
5375 * be on another cpu but it doesn't matter.
5376 */
5524 local_irq_disable(); 5377 local_irq_disable();
5525 ret = __migrate_task(p, src_cpu, dest_cpu); 5378 __migrate_task(arg->task, raw_smp_processor_id(), arg->dest_cpu);
5526 local_irq_enable(); 5379 local_irq_enable();
5527 return ret; 5380 return 0;
5528} 5381}
5529 5382
5383#ifdef CONFIG_HOTPLUG_CPU
5530/* 5384/*
5531 * Figure out where task on dead CPU should go, use force if necessary. 5385 * Figure out where task on dead CPU should go, use force if necessary.
5532 */ 5386 */
5533static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p) 5387void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
5534{ 5388{
5535 int dest_cpu; 5389 struct rq *rq = cpu_rq(dead_cpu);
5390 int needs_cpu, uninitialized_var(dest_cpu);
5391 unsigned long flags;
5536 5392
5537again: 5393 local_irq_save(flags);
5538 dest_cpu = select_fallback_rq(dead_cpu, p);
5539 5394
5540 /* It can have affinity changed while we were choosing. */ 5395 raw_spin_lock(&rq->lock);
5541 if (unlikely(!__migrate_task_irq(p, dead_cpu, dest_cpu))) 5396 needs_cpu = (task_cpu(p) == dead_cpu) && (p->state != TASK_WAKING);
5542 goto again; 5397 if (needs_cpu)
5398 dest_cpu = select_fallback_rq(dead_cpu, p);
5399 raw_spin_unlock(&rq->lock);
5400 /*
5401 * It can only fail if we race with set_cpus_allowed(),
5402 * in the racer should migrate the task anyway.
5403 */
5404 if (needs_cpu)
5405 __migrate_task(p, dead_cpu, dest_cpu);
5406 local_irq_restore(flags);
5543} 5407}
5544 5408
5545/* 5409/*
@@ -5603,7 +5467,6 @@ void sched_idle_next(void)
5603 5467
5604 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1); 5468 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
5605 5469
5606 update_rq_clock(rq);
5607 activate_task(rq, p, 0); 5470 activate_task(rq, p, 0);
5608 5471
5609 raw_spin_unlock_irqrestore(&rq->lock, flags); 5472 raw_spin_unlock_irqrestore(&rq->lock, flags);
@@ -5658,7 +5521,6 @@ static void migrate_dead_tasks(unsigned int dead_cpu)
5658 for ( ; ; ) { 5521 for ( ; ; ) {
5659 if (!rq->nr_running) 5522 if (!rq->nr_running)
5660 break; 5523 break;
5661 update_rq_clock(rq);
5662 next = pick_next_task(rq); 5524 next = pick_next_task(rq);
5663 if (!next) 5525 if (!next)
5664 break; 5526 break;
@@ -5881,35 +5743,20 @@ static void set_rq_offline(struct rq *rq)
5881static int __cpuinit 5743static int __cpuinit
5882migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu) 5744migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
5883{ 5745{
5884 struct task_struct *p;
5885 int cpu = (long)hcpu; 5746 int cpu = (long)hcpu;
5886 unsigned long flags; 5747 unsigned long flags;
5887 struct rq *rq; 5748 struct rq *rq = cpu_rq(cpu);
5888 5749
5889 switch (action) { 5750 switch (action) {
5890 5751
5891 case CPU_UP_PREPARE: 5752 case CPU_UP_PREPARE:
5892 case CPU_UP_PREPARE_FROZEN: 5753 case CPU_UP_PREPARE_FROZEN:
5893 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
5894 if (IS_ERR(p))
5895 return NOTIFY_BAD;
5896 kthread_bind(p, cpu);
5897 /* Must be high prio: stop_machine expects to yield to it. */
5898 rq = task_rq_lock(p, &flags);
5899 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
5900 task_rq_unlock(rq, &flags);
5901 get_task_struct(p);
5902 cpu_rq(cpu)->migration_thread = p;
5903 rq->calc_load_update = calc_load_update; 5754 rq->calc_load_update = calc_load_update;
5904 break; 5755 break;
5905 5756
5906 case CPU_ONLINE: 5757 case CPU_ONLINE:
5907 case CPU_ONLINE_FROZEN: 5758 case CPU_ONLINE_FROZEN:
5908 /* Strictly unnecessary, as first user will wake it. */
5909 wake_up_process(cpu_rq(cpu)->migration_thread);
5910
5911 /* Update our root-domain */ 5759 /* Update our root-domain */
5912 rq = cpu_rq(cpu);
5913 raw_spin_lock_irqsave(&rq->lock, flags); 5760 raw_spin_lock_irqsave(&rq->lock, flags);
5914 if (rq->rd) { 5761 if (rq->rd) {
5915 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span)); 5762 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
@@ -5920,61 +5767,24 @@ migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
5920 break; 5767 break;
5921 5768
5922#ifdef CONFIG_HOTPLUG_CPU 5769#ifdef CONFIG_HOTPLUG_CPU
5923 case CPU_UP_CANCELED:
5924 case CPU_UP_CANCELED_FROZEN:
5925 if (!cpu_rq(cpu)->migration_thread)
5926 break;
5927 /* Unbind it from offline cpu so it can run. Fall thru. */
5928 kthread_bind(cpu_rq(cpu)->migration_thread,
5929 cpumask_any(cpu_online_mask));
5930 kthread_stop(cpu_rq(cpu)->migration_thread);
5931 put_task_struct(cpu_rq(cpu)->migration_thread);
5932 cpu_rq(cpu)->migration_thread = NULL;
5933 break;
5934
5935 case CPU_DEAD: 5770 case CPU_DEAD:
5936 case CPU_DEAD_FROZEN: 5771 case CPU_DEAD_FROZEN:
5937 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
5938 migrate_live_tasks(cpu); 5772 migrate_live_tasks(cpu);
5939 rq = cpu_rq(cpu);
5940 kthread_stop(rq->migration_thread);
5941 put_task_struct(rq->migration_thread);
5942 rq->migration_thread = NULL;
5943 /* Idle task back to normal (off runqueue, low prio) */ 5773 /* Idle task back to normal (off runqueue, low prio) */
5944 raw_spin_lock_irq(&rq->lock); 5774 raw_spin_lock_irq(&rq->lock);
5945 update_rq_clock(rq);
5946 deactivate_task(rq, rq->idle, 0); 5775 deactivate_task(rq, rq->idle, 0);
5947 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0); 5776 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
5948 rq->idle->sched_class = &idle_sched_class; 5777 rq->idle->sched_class = &idle_sched_class;
5949 migrate_dead_tasks(cpu); 5778 migrate_dead_tasks(cpu);
5950 raw_spin_unlock_irq(&rq->lock); 5779 raw_spin_unlock_irq(&rq->lock);
5951 cpuset_unlock();
5952 migrate_nr_uninterruptible(rq); 5780 migrate_nr_uninterruptible(rq);
5953 BUG_ON(rq->nr_running != 0); 5781 BUG_ON(rq->nr_running != 0);
5954 calc_global_load_remove(rq); 5782 calc_global_load_remove(rq);
5955 /*
5956 * No need to migrate the tasks: it was best-effort if
5957 * they didn't take sched_hotcpu_mutex. Just wake up
5958 * the requestors.
5959 */
5960 raw_spin_lock_irq(&rq->lock);
5961 while (!list_empty(&rq->migration_queue)) {
5962 struct migration_req *req;
5963
5964 req = list_entry(rq->migration_queue.next,
5965 struct migration_req, list);
5966 list_del_init(&req->list);
5967 raw_spin_unlock_irq(&rq->lock);
5968 complete(&req->done);
5969 raw_spin_lock_irq(&rq->lock);
5970 }
5971 raw_spin_unlock_irq(&rq->lock);
5972 break; 5783 break;
5973 5784
5974 case CPU_DYING: 5785 case CPU_DYING:
5975 case CPU_DYING_FROZEN: 5786 case CPU_DYING_FROZEN:
5976 /* Update our root-domain */ 5787 /* Update our root-domain */
5977 rq = cpu_rq(cpu);
5978 raw_spin_lock_irqsave(&rq->lock, flags); 5788 raw_spin_lock_irqsave(&rq->lock, flags);
5979 if (rq->rd) { 5789 if (rq->rd) {
5980 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span)); 5790 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
@@ -6305,6 +6115,9 @@ cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
6305 struct rq *rq = cpu_rq(cpu); 6115 struct rq *rq = cpu_rq(cpu);
6306 struct sched_domain *tmp; 6116 struct sched_domain *tmp;
6307 6117
6118 for (tmp = sd; tmp; tmp = tmp->parent)
6119 tmp->span_weight = cpumask_weight(sched_domain_span(tmp));
6120
6308 /* Remove the sched domains which do not contribute to scheduling. */ 6121 /* Remove the sched domains which do not contribute to scheduling. */
6309 for (tmp = sd; tmp; ) { 6122 for (tmp = sd; tmp; ) {
6310 struct sched_domain *parent = tmp->parent; 6123 struct sched_domain *parent = tmp->parent;
@@ -7788,10 +7601,8 @@ void __init sched_init(void)
7788 rq->push_cpu = 0; 7601 rq->push_cpu = 0;
7789 rq->cpu = i; 7602 rq->cpu = i;
7790 rq->online = 0; 7603 rq->online = 0;
7791 rq->migration_thread = NULL;
7792 rq->idle_stamp = 0; 7604 rq->idle_stamp = 0;
7793 rq->avg_idle = 2*sysctl_sched_migration_cost; 7605 rq->avg_idle = 2*sysctl_sched_migration_cost;
7794 INIT_LIST_HEAD(&rq->migration_queue);
7795 rq_attach_root(rq, &def_root_domain); 7606 rq_attach_root(rq, &def_root_domain);
7796#endif 7607#endif
7797 init_rq_hrtick(rq); 7608 init_rq_hrtick(rq);
@@ -7892,7 +7703,6 @@ static void normalize_task(struct rq *rq, struct task_struct *p)
7892{ 7703{
7893 int on_rq; 7704 int on_rq;
7894 7705
7895 update_rq_clock(rq);
7896 on_rq = p->se.on_rq; 7706 on_rq = p->se.on_rq;
7897 if (on_rq) 7707 if (on_rq)
7898 deactivate_task(rq, p, 0); 7708 deactivate_task(rq, p, 0);
@@ -7919,9 +7729,9 @@ void normalize_rt_tasks(void)
7919 7729
7920 p->se.exec_start = 0; 7730 p->se.exec_start = 0;
7921#ifdef CONFIG_SCHEDSTATS 7731#ifdef CONFIG_SCHEDSTATS
7922 p->se.wait_start = 0; 7732 p->se.statistics.wait_start = 0;
7923 p->se.sleep_start = 0; 7733 p->se.statistics.sleep_start = 0;
7924 p->se.block_start = 0; 7734 p->se.statistics.block_start = 0;
7925#endif 7735#endif
7926 7736
7927 if (!rt_task(p)) { 7737 if (!rt_task(p)) {
@@ -8254,8 +8064,6 @@ void sched_move_task(struct task_struct *tsk)
8254 8064
8255 rq = task_rq_lock(tsk, &flags); 8065 rq = task_rq_lock(tsk, &flags);
8256 8066
8257 update_rq_clock(rq);
8258
8259 running = task_current(rq, tsk); 8067 running = task_current(rq, tsk);
8260 on_rq = tsk->se.on_rq; 8068 on_rq = tsk->se.on_rq;
8261 8069
@@ -8274,7 +8082,7 @@ void sched_move_task(struct task_struct *tsk)
8274 if (unlikely(running)) 8082 if (unlikely(running))
8275 tsk->sched_class->set_curr_task(rq); 8083 tsk->sched_class->set_curr_task(rq);
8276 if (on_rq) 8084 if (on_rq)
8277 enqueue_task(rq, tsk, 0, false); 8085 enqueue_task(rq, tsk, 0);
8278 8086
8279 task_rq_unlock(rq, &flags); 8087 task_rq_unlock(rq, &flags);
8280} 8088}
@@ -9088,43 +8896,32 @@ struct cgroup_subsys cpuacct_subsys = {
9088 8896
9089#ifndef CONFIG_SMP 8897#ifndef CONFIG_SMP
9090 8898
9091int rcu_expedited_torture_stats(char *page)
9092{
9093 return 0;
9094}
9095EXPORT_SYMBOL_GPL(rcu_expedited_torture_stats);
9096
9097void synchronize_sched_expedited(void) 8899void synchronize_sched_expedited(void)
9098{ 8900{
8901 barrier();
9099} 8902}
9100EXPORT_SYMBOL_GPL(synchronize_sched_expedited); 8903EXPORT_SYMBOL_GPL(synchronize_sched_expedited);
9101 8904
9102#else /* #ifndef CONFIG_SMP */ 8905#else /* #ifndef CONFIG_SMP */
9103 8906
9104static DEFINE_PER_CPU(struct migration_req, rcu_migration_req); 8907static atomic_t synchronize_sched_expedited_count = ATOMIC_INIT(0);
9105static DEFINE_MUTEX(rcu_sched_expedited_mutex);
9106 8908
9107#define RCU_EXPEDITED_STATE_POST -2 8909static int synchronize_sched_expedited_cpu_stop(void *data)
9108#define RCU_EXPEDITED_STATE_IDLE -1
9109
9110static int rcu_expedited_state = RCU_EXPEDITED_STATE_IDLE;
9111
9112int rcu_expedited_torture_stats(char *page)
9113{ 8910{
9114 int cnt = 0; 8911 /*
9115 int cpu; 8912 * There must be a full memory barrier on each affected CPU
9116 8913 * between the time that try_stop_cpus() is called and the
9117 cnt += sprintf(&page[cnt], "state: %d /", rcu_expedited_state); 8914 * time that it returns.
9118 for_each_online_cpu(cpu) { 8915 *
9119 cnt += sprintf(&page[cnt], " %d:%d", 8916 * In the current initial implementation of cpu_stop, the
9120 cpu, per_cpu(rcu_migration_req, cpu).dest_cpu); 8917 * above condition is already met when the control reaches
9121 } 8918 * this point and the following smp_mb() is not strictly
9122 cnt += sprintf(&page[cnt], "\n"); 8919 * necessary. Do smp_mb() anyway for documentation and
9123 return cnt; 8920 * robustness against future implementation changes.
8921 */
8922 smp_mb(); /* See above comment block. */
8923 return 0;
9124} 8924}
9125EXPORT_SYMBOL_GPL(rcu_expedited_torture_stats);
9126
9127static long synchronize_sched_expedited_count;
9128 8925
9129/* 8926/*
9130 * Wait for an rcu-sched grace period to elapse, but use "big hammer" 8927 * Wait for an rcu-sched grace period to elapse, but use "big hammer"
@@ -9138,18 +8935,14 @@ static long synchronize_sched_expedited_count;
9138 */ 8935 */
9139void synchronize_sched_expedited(void) 8936void synchronize_sched_expedited(void)
9140{ 8937{
9141 int cpu; 8938 int snap, trycount = 0;
9142 unsigned long flags;
9143 bool need_full_sync = 0;
9144 struct rq *rq;
9145 struct migration_req *req;
9146 long snap;
9147 int trycount = 0;
9148 8939
9149 smp_mb(); /* ensure prior mod happens before capturing snap. */ 8940 smp_mb(); /* ensure prior mod happens before capturing snap. */
9150 snap = ACCESS_ONCE(synchronize_sched_expedited_count) + 1; 8941 snap = atomic_read(&synchronize_sched_expedited_count) + 1;
9151 get_online_cpus(); 8942 get_online_cpus();
9152 while (!mutex_trylock(&rcu_sched_expedited_mutex)) { 8943 while (try_stop_cpus(cpu_online_mask,
8944 synchronize_sched_expedited_cpu_stop,
8945 NULL) == -EAGAIN) {
9153 put_online_cpus(); 8946 put_online_cpus();
9154 if (trycount++ < 10) 8947 if (trycount++ < 10)
9155 udelay(trycount * num_online_cpus()); 8948 udelay(trycount * num_online_cpus());
@@ -9157,41 +8950,15 @@ void synchronize_sched_expedited(void)
9157 synchronize_sched(); 8950 synchronize_sched();
9158 return; 8951 return;
9159 } 8952 }
9160 if (ACCESS_ONCE(synchronize_sched_expedited_count) - snap > 0) { 8953 if (atomic_read(&synchronize_sched_expedited_count) - snap > 0) {
9161 smp_mb(); /* ensure test happens before caller kfree */ 8954 smp_mb(); /* ensure test happens before caller kfree */
9162 return; 8955 return;
9163 } 8956 }
9164 get_online_cpus(); 8957 get_online_cpus();
9165 } 8958 }
9166 rcu_expedited_state = RCU_EXPEDITED_STATE_POST; 8959 atomic_inc(&synchronize_sched_expedited_count);
9167 for_each_online_cpu(cpu) { 8960 smp_mb__after_atomic_inc(); /* ensure post-GP actions seen after GP. */
9168 rq = cpu_rq(cpu);
9169 req = &per_cpu(rcu_migration_req, cpu);
9170 init_completion(&req->done);
9171 req->task = NULL;
9172 req->dest_cpu = RCU_MIGRATION_NEED_QS;
9173 raw_spin_lock_irqsave(&rq->lock, flags);
9174 list_add(&req->list, &rq->migration_queue);
9175 raw_spin_unlock_irqrestore(&rq->lock, flags);
9176 wake_up_process(rq->migration_thread);
9177 }
9178 for_each_online_cpu(cpu) {
9179 rcu_expedited_state = cpu;
9180 req = &per_cpu(rcu_migration_req, cpu);
9181 rq = cpu_rq(cpu);
9182 wait_for_completion(&req->done);
9183 raw_spin_lock_irqsave(&rq->lock, flags);
9184 if (unlikely(req->dest_cpu == RCU_MIGRATION_MUST_SYNC))
9185 need_full_sync = 1;
9186 req->dest_cpu = RCU_MIGRATION_IDLE;
9187 raw_spin_unlock_irqrestore(&rq->lock, flags);
9188 }
9189 rcu_expedited_state = RCU_EXPEDITED_STATE_IDLE;
9190 synchronize_sched_expedited_count++;
9191 mutex_unlock(&rcu_sched_expedited_mutex);
9192 put_online_cpus(); 8961 put_online_cpus();
9193 if (need_full_sync)
9194 synchronize_sched();
9195} 8962}
9196EXPORT_SYMBOL_GPL(synchronize_sched_expedited); 8963EXPORT_SYMBOL_GPL(synchronize_sched_expedited);
9197 8964