aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/sched.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/sched.c')
-rw-r--r--kernel/sched.c726
1 files changed, 263 insertions, 463 deletions
diff --git a/kernel/sched.c b/kernel/sched.c
index 6af210a7de70..b531d7934083 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>
@@ -493,8 +493,11 @@ struct rq {
493 #define CPU_LOAD_IDX_MAX 5 493 #define CPU_LOAD_IDX_MAX 5
494 unsigned long cpu_load[CPU_LOAD_IDX_MAX]; 494 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
495#ifdef CONFIG_NO_HZ 495#ifdef CONFIG_NO_HZ
496 u64 nohz_stamp;
496 unsigned char in_nohz_recently; 497 unsigned char in_nohz_recently;
497#endif 498#endif
499 unsigned int skip_clock_update;
500
498 /* capture load from *all* tasks on this cpu: */ 501 /* capture load from *all* tasks on this cpu: */
499 struct load_weight load; 502 struct load_weight load;
500 unsigned long nr_load_updates; 503 unsigned long nr_load_updates;
@@ -536,15 +539,13 @@ struct rq {
536 int post_schedule; 539 int post_schedule;
537 int active_balance; 540 int active_balance;
538 int push_cpu; 541 int push_cpu;
542 struct cpu_stop_work active_balance_work;
539 /* cpu of this runqueue: */ 543 /* cpu of this runqueue: */
540 int cpu; 544 int cpu;
541 int online; 545 int online;
542 546
543 unsigned long avg_load_per_task; 547 unsigned long avg_load_per_task;
544 548
545 struct task_struct *migration_thread;
546 struct list_head migration_queue;
547
548 u64 rt_avg; 549 u64 rt_avg;
549 u64 age_stamp; 550 u64 age_stamp;
550 u64 idle_stamp; 551 u64 idle_stamp;
@@ -592,6 +593,13 @@ static inline
592void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags) 593void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags)
593{ 594{
594 rq->curr->sched_class->check_preempt_curr(rq, p, flags); 595 rq->curr->sched_class->check_preempt_curr(rq, p, flags);
596
597 /*
598 * A queue event has occurred, and we're going to schedule. In
599 * this case, we can save a useless back to back clock update.
600 */
601 if (test_tsk_need_resched(p))
602 rq->skip_clock_update = 1;
595} 603}
596 604
597static inline int cpu_of(struct rq *rq) 605static inline int cpu_of(struct rq *rq)
@@ -626,7 +634,8 @@ static inline int cpu_of(struct rq *rq)
626 634
627inline void update_rq_clock(struct rq *rq) 635inline void update_rq_clock(struct rq *rq)
628{ 636{
629 rq->clock = sched_clock_cpu(cpu_of(rq)); 637 if (!rq->skip_clock_update)
638 rq->clock = sched_clock_cpu(cpu_of(rq));
630} 639}
631 640
632/* 641/*
@@ -904,16 +913,12 @@ static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
904#endif /* __ARCH_WANT_UNLOCKED_CTXSW */ 913#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
905 914
906/* 915/*
907 * Check whether the task is waking, we use this to synchronize against 916 * Check whether the task is waking, we use this to synchronize ->cpus_allowed
908 * ttwu() so that task_cpu() reports a stable number. 917 * 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 */ 918 */
914static inline int task_is_waking(struct task_struct *p) 919static inline int task_is_waking(struct task_struct *p)
915{ 920{
916 return unlikely((p->state == TASK_WAKING) && !(p->flags & PF_STARTING)); 921 return unlikely(p->state == TASK_WAKING);
917} 922}
918 923
919/* 924/*
@@ -926,11 +931,9 @@ static inline struct rq *__task_rq_lock(struct task_struct *p)
926 struct rq *rq; 931 struct rq *rq;
927 932
928 for (;;) { 933 for (;;) {
929 while (task_is_waking(p))
930 cpu_relax();
931 rq = task_rq(p); 934 rq = task_rq(p);
932 raw_spin_lock(&rq->lock); 935 raw_spin_lock(&rq->lock);
933 if (likely(rq == task_rq(p) && !task_is_waking(p))) 936 if (likely(rq == task_rq(p)))
934 return rq; 937 return rq;
935 raw_spin_unlock(&rq->lock); 938 raw_spin_unlock(&rq->lock);
936 } 939 }
@@ -947,12 +950,10 @@ static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
947 struct rq *rq; 950 struct rq *rq;
948 951
949 for (;;) { 952 for (;;) {
950 while (task_is_waking(p))
951 cpu_relax();
952 local_irq_save(*flags); 953 local_irq_save(*flags);
953 rq = task_rq(p); 954 rq = task_rq(p);
954 raw_spin_lock(&rq->lock); 955 raw_spin_lock(&rq->lock);
955 if (likely(rq == task_rq(p) && !task_is_waking(p))) 956 if (likely(rq == task_rq(p)))
956 return rq; 957 return rq;
957 raw_spin_unlock_irqrestore(&rq->lock, *flags); 958 raw_spin_unlock_irqrestore(&rq->lock, *flags);
958 } 959 }
@@ -1229,6 +1230,17 @@ void wake_up_idle_cpu(int cpu)
1229 if (!tsk_is_polling(rq->idle)) 1230 if (!tsk_is_polling(rq->idle))
1230 smp_send_reschedule(cpu); 1231 smp_send_reschedule(cpu);
1231} 1232}
1233
1234int nohz_ratelimit(int cpu)
1235{
1236 struct rq *rq = cpu_rq(cpu);
1237 u64 diff = rq->clock - rq->nohz_stamp;
1238
1239 rq->nohz_stamp = rq->clock;
1240
1241 return diff < (NSEC_PER_SEC / HZ) >> 1;
1242}
1243
1232#endif /* CONFIG_NO_HZ */ 1244#endif /* CONFIG_NO_HZ */
1233 1245
1234static u64 sched_avg_period(void) 1246static u64 sched_avg_period(void)
@@ -1771,8 +1783,6 @@ static void double_rq_lock(struct rq *rq1, struct rq *rq2)
1771 raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING); 1783 raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
1772 } 1784 }
1773 } 1785 }
1774 update_rq_clock(rq1);
1775 update_rq_clock(rq2);
1776} 1786}
1777 1787
1778/* 1788/*
@@ -1803,7 +1813,7 @@ static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1803} 1813}
1804#endif 1814#endif
1805 1815
1806static void calc_load_account_active(struct rq *this_rq); 1816static void calc_load_account_idle(struct rq *this_rq);
1807static void update_sysctl(void); 1817static void update_sysctl(void);
1808static int get_update_sysctl_factor(void); 1818static int get_update_sysctl_factor(void);
1809 1819
@@ -1860,62 +1870,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]; 1870 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
1861} 1871}
1862 1872
1863static void update_avg(u64 *avg, u64 sample) 1873static void enqueue_task(struct rq *rq, struct task_struct *p, int flags)
1864{ 1874{
1865 s64 diff = sample - *avg; 1875 update_rq_clock(rq);
1866 *avg += diff >> 3;
1867}
1868
1869static void
1870enqueue_task(struct rq *rq, struct task_struct *p, int wakeup, bool head)
1871{
1872 if (wakeup)
1873 p->se.start_runtime = p->se.sum_exec_runtime;
1874
1875 sched_info_queued(p); 1876 sched_info_queued(p);
1876 p->sched_class->enqueue_task(rq, p, wakeup, head); 1877 p->sched_class->enqueue_task(rq, p, flags);
1877 p->se.on_rq = 1; 1878 p->se.on_rq = 1;
1878} 1879}
1879 1880
1880static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep) 1881static void dequeue_task(struct rq *rq, struct task_struct *p, int flags)
1881{ 1882{
1882 if (sleep) { 1883 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); 1884 sched_info_dequeued(p);
1894 p->sched_class->dequeue_task(rq, p, sleep); 1885 p->sched_class->dequeue_task(rq, p, flags);
1895 p->se.on_rq = 0; 1886 p->se.on_rq = 0;
1896} 1887}
1897 1888
1898/* 1889/*
1899 * activate_task - move a task to the runqueue. 1890 * activate_task - move a task to the runqueue.
1900 */ 1891 */
1901static void activate_task(struct rq *rq, struct task_struct *p, int wakeup) 1892static void activate_task(struct rq *rq, struct task_struct *p, int flags)
1902{ 1893{
1903 if (task_contributes_to_load(p)) 1894 if (task_contributes_to_load(p))
1904 rq->nr_uninterruptible--; 1895 rq->nr_uninterruptible--;
1905 1896
1906 enqueue_task(rq, p, wakeup, false); 1897 enqueue_task(rq, p, flags);
1907 inc_nr_running(rq); 1898 inc_nr_running(rq);
1908} 1899}
1909 1900
1910/* 1901/*
1911 * deactivate_task - remove a task from the runqueue. 1902 * deactivate_task - remove a task from the runqueue.
1912 */ 1903 */
1913static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep) 1904static void deactivate_task(struct rq *rq, struct task_struct *p, int flags)
1914{ 1905{
1915 if (task_contributes_to_load(p)) 1906 if (task_contributes_to_load(p))
1916 rq->nr_uninterruptible++; 1907 rq->nr_uninterruptible++;
1917 1908
1918 dequeue_task(rq, p, sleep); 1909 dequeue_task(rq, p, flags);
1919 dec_nr_running(rq); 1910 dec_nr_running(rq);
1920} 1911}
1921 1912
@@ -2044,21 +2035,18 @@ void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
2044 __set_task_cpu(p, new_cpu); 2035 __set_task_cpu(p, new_cpu);
2045} 2036}
2046 2037
2047struct migration_req { 2038struct migration_arg {
2048 struct list_head list;
2049
2050 struct task_struct *task; 2039 struct task_struct *task;
2051 int dest_cpu; 2040 int dest_cpu;
2052
2053 struct completion done;
2054}; 2041};
2055 2042
2043static int migration_cpu_stop(void *data);
2044
2056/* 2045/*
2057 * The task's runqueue lock must be held. 2046 * The task's runqueue lock must be held.
2058 * Returns true if you have to wait for migration thread. 2047 * Returns true if you have to wait for migration thread.
2059 */ 2048 */
2060static int 2049static bool migrate_task(struct task_struct *p, int dest_cpu)
2061migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
2062{ 2050{
2063 struct rq *rq = task_rq(p); 2051 struct rq *rq = task_rq(p);
2064 2052
@@ -2066,15 +2054,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 2054 * If the task is not on a runqueue (and not running), then
2067 * the next wake-up will properly place the task. 2055 * the next wake-up will properly place the task.
2068 */ 2056 */
2069 if (!p->se.on_rq && !task_running(rq, p)) 2057 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} 2058}
2079 2059
2080/* 2060/*
@@ -2175,7 +2155,7 @@ unsigned long wait_task_inactive(struct task_struct *p, long match_state)
2175 * just go back and repeat. 2155 * just go back and repeat.
2176 */ 2156 */
2177 rq = task_rq_lock(p, &flags); 2157 rq = task_rq_lock(p, &flags);
2178 trace_sched_wait_task(rq, p); 2158 trace_sched_wait_task(p);
2179 running = task_running(rq, p); 2159 running = task_running(rq, p);
2180 on_rq = p->se.on_rq; 2160 on_rq = p->se.on_rq;
2181 ncsw = 0; 2161 ncsw = 0;
@@ -2273,6 +2253,9 @@ void task_oncpu_function_call(struct task_struct *p,
2273} 2253}
2274 2254
2275#ifdef CONFIG_SMP 2255#ifdef CONFIG_SMP
2256/*
2257 * ->cpus_allowed is protected by either TASK_WAKING or rq->lock held.
2258 */
2276static int select_fallback_rq(int cpu, struct task_struct *p) 2259static int select_fallback_rq(int cpu, struct task_struct *p)
2277{ 2260{
2278 int dest_cpu; 2261 int dest_cpu;
@@ -2289,12 +2272,8 @@ static int select_fallback_rq(int cpu, struct task_struct *p)
2289 return dest_cpu; 2272 return dest_cpu;
2290 2273
2291 /* No more Mr. Nice Guy. */ 2274 /* No more Mr. Nice Guy. */
2292 if (dest_cpu >= nr_cpu_ids) { 2275 if (unlikely(dest_cpu >= nr_cpu_ids)) {
2293 rcu_read_lock(); 2276 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 /* 2277 /*
2299 * Don't tell them about moving exiting tasks or 2278 * Don't tell them about moving exiting tasks or
2300 * kernel threads (both mm NULL), since they never 2279 * kernel threads (both mm NULL), since they never
@@ -2311,17 +2290,12 @@ static int select_fallback_rq(int cpu, struct task_struct *p)
2311} 2290}
2312 2291
2313/* 2292/*
2314 * Gets called from 3 sites (exec, fork, wakeup), since it is called without 2293 * 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 */ 2294 */
2321static inline 2295static inline
2322int select_task_rq(struct task_struct *p, int sd_flags, int wake_flags) 2296int select_task_rq(struct rq *rq, struct task_struct *p, int sd_flags, int wake_flags)
2323{ 2297{
2324 int cpu = p->sched_class->select_task_rq(p, sd_flags, wake_flags); 2298 int cpu = p->sched_class->select_task_rq(rq, p, sd_flags, wake_flags);
2325 2299
2326 /* 2300 /*
2327 * In order not to call set_task_cpu() on a blocking task we need 2301 * In order not to call set_task_cpu() on a blocking task we need
@@ -2339,6 +2313,12 @@ int select_task_rq(struct task_struct *p, int sd_flags, int wake_flags)
2339 2313
2340 return cpu; 2314 return cpu;
2341} 2315}
2316
2317static void update_avg(u64 *avg, u64 sample)
2318{
2319 s64 diff = sample - *avg;
2320 *avg += diff >> 3;
2321}
2342#endif 2322#endif
2343 2323
2344/*** 2324/***
@@ -2360,16 +2340,13 @@ static int try_to_wake_up(struct task_struct *p, unsigned int state,
2360{ 2340{
2361 int cpu, orig_cpu, this_cpu, success = 0; 2341 int cpu, orig_cpu, this_cpu, success = 0;
2362 unsigned long flags; 2342 unsigned long flags;
2343 unsigned long en_flags = ENQUEUE_WAKEUP;
2363 struct rq *rq; 2344 struct rq *rq;
2364 2345
2365 if (!sched_feat(SYNC_WAKEUPS))
2366 wake_flags &= ~WF_SYNC;
2367
2368 this_cpu = get_cpu(); 2346 this_cpu = get_cpu();
2369 2347
2370 smp_wmb(); 2348 smp_wmb();
2371 rq = task_rq_lock(p, &flags); 2349 rq = task_rq_lock(p, &flags);
2372 update_rq_clock(rq);
2373 if (!(p->state & state)) 2350 if (!(p->state & state))
2374 goto out; 2351 goto out;
2375 2352
@@ -2389,28 +2366,26 @@ static int try_to_wake_up(struct task_struct *p, unsigned int state,
2389 * 2366 *
2390 * First fix up the nr_uninterruptible count: 2367 * First fix up the nr_uninterruptible count:
2391 */ 2368 */
2392 if (task_contributes_to_load(p)) 2369 if (task_contributes_to_load(p)) {
2393 rq->nr_uninterruptible--; 2370 if (likely(cpu_online(orig_cpu)))
2371 rq->nr_uninterruptible--;
2372 else
2373 this_rq()->nr_uninterruptible--;
2374 }
2394 p->state = TASK_WAKING; 2375 p->state = TASK_WAKING;
2395 2376
2396 if (p->sched_class->task_waking) 2377 if (p->sched_class->task_waking) {
2397 p->sched_class->task_waking(rq, p); 2378 p->sched_class->task_waking(rq, p);
2379 en_flags |= ENQUEUE_WAKING;
2380 }
2398 2381
2399 __task_rq_unlock(rq); 2382 cpu = select_task_rq(rq, p, SD_BALANCE_WAKE, wake_flags);
2400 2383 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); 2384 set_task_cpu(p, cpu);
2409 } 2385 __task_rq_unlock(rq);
2410 2386
2411 rq = cpu_rq(cpu); 2387 rq = cpu_rq(cpu);
2412 raw_spin_lock(&rq->lock); 2388 raw_spin_lock(&rq->lock);
2413 update_rq_clock(rq);
2414 2389
2415 /* 2390 /*
2416 * We migrated the task without holding either rq->lock, however 2391 * We migrated the task without holding either rq->lock, however
@@ -2438,36 +2413,20 @@ static int try_to_wake_up(struct task_struct *p, unsigned int state,
2438 2413
2439out_activate: 2414out_activate:
2440#endif /* CONFIG_SMP */ 2415#endif /* CONFIG_SMP */
2441 schedstat_inc(p, se.nr_wakeups); 2416 schedstat_inc(p, se.statistics.nr_wakeups);
2442 if (wake_flags & WF_SYNC) 2417 if (wake_flags & WF_SYNC)
2443 schedstat_inc(p, se.nr_wakeups_sync); 2418 schedstat_inc(p, se.statistics.nr_wakeups_sync);
2444 if (orig_cpu != cpu) 2419 if (orig_cpu != cpu)
2445 schedstat_inc(p, se.nr_wakeups_migrate); 2420 schedstat_inc(p, se.statistics.nr_wakeups_migrate);
2446 if (cpu == this_cpu) 2421 if (cpu == this_cpu)
2447 schedstat_inc(p, se.nr_wakeups_local); 2422 schedstat_inc(p, se.statistics.nr_wakeups_local);
2448 else 2423 else
2449 schedstat_inc(p, se.nr_wakeups_remote); 2424 schedstat_inc(p, se.statistics.nr_wakeups_remote);
2450 activate_task(rq, p, 1); 2425 activate_task(rq, p, en_flags);
2451 success = 1; 2426 success = 1;
2452 2427
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: 2428out_running:
2470 trace_sched_wakeup(rq, p, success); 2429 trace_sched_wakeup(p, success);
2471 check_preempt_curr(rq, p, wake_flags); 2430 check_preempt_curr(rq, p, wake_flags);
2472 2431
2473 p->state = TASK_RUNNING; 2432 p->state = TASK_RUNNING;
@@ -2527,42 +2486,9 @@ static void __sched_fork(struct task_struct *p)
2527 p->se.sum_exec_runtime = 0; 2486 p->se.sum_exec_runtime = 0;
2528 p->se.prev_sum_exec_runtime = 0; 2487 p->se.prev_sum_exec_runtime = 0;
2529 p->se.nr_migrations = 0; 2488 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 2489
2535#ifdef CONFIG_SCHEDSTATS 2490#ifdef CONFIG_SCHEDSTATS
2536 p->se.wait_start = 0; 2491 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 2492#endif
2567 2493
2568 INIT_LIST_HEAD(&p->rt.run_list); 2494 INIT_LIST_HEAD(&p->rt.run_list);
@@ -2583,11 +2509,11 @@ void sched_fork(struct task_struct *p, int clone_flags)
2583 2509
2584 __sched_fork(p); 2510 __sched_fork(p);
2585 /* 2511 /*
2586 * We mark the process as waking here. This guarantees that 2512 * We mark the process as running here. This guarantees that
2587 * nobody will actually run it, and a signal or other external 2513 * nobody will actually run it, and a signal or other external
2588 * event cannot wake it up and insert it on the runqueue either. 2514 * event cannot wake it up and insert it on the runqueue either.
2589 */ 2515 */
2590 p->state = TASK_WAKING; 2516 p->state = TASK_RUNNING;
2591 2517
2592 /* 2518 /*
2593 * Revert to default priority/policy on fork if requested. 2519 * Revert to default priority/policy on fork if requested.
@@ -2654,31 +2580,27 @@ void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
2654 int cpu __maybe_unused = get_cpu(); 2580 int cpu __maybe_unused = get_cpu();
2655 2581
2656#ifdef CONFIG_SMP 2582#ifdef CONFIG_SMP
2583 rq = task_rq_lock(p, &flags);
2584 p->state = TASK_WAKING;
2585
2657 /* 2586 /*
2658 * Fork balancing, do it here and not earlier because: 2587 * Fork balancing, do it here and not earlier because:
2659 * - cpus_allowed can change in the fork path 2588 * - cpus_allowed can change in the fork path
2660 * - any previously selected cpu might disappear through hotplug 2589 * - any previously selected cpu might disappear through hotplug
2661 * 2590 *
2662 * We still have TASK_WAKING but PF_STARTING is gone now, meaning 2591 * We set TASK_WAKING so that select_task_rq() can drop rq->lock
2663 * ->cpus_allowed is stable, we have preemption disabled, meaning 2592 * without people poking at ->cpus_allowed.
2664 * cpu_online_mask is stable.
2665 */ 2593 */
2666 cpu = select_task_rq(p, SD_BALANCE_FORK, 0); 2594 cpu = select_task_rq(rq, p, SD_BALANCE_FORK, 0);
2667 set_task_cpu(p, cpu); 2595 set_task_cpu(p, cpu);
2668#endif
2669 2596
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; 2597 p->state = TASK_RUNNING;
2679 update_rq_clock(rq); 2598 task_rq_unlock(rq, &flags);
2599#endif
2600
2601 rq = task_rq_lock(p, &flags);
2680 activate_task(rq, p, 0); 2602 activate_task(rq, p, 0);
2681 trace_sched_wakeup_new(rq, p, 1); 2603 trace_sched_wakeup_new(p, 1);
2682 check_preempt_curr(rq, p, WF_FORK); 2604 check_preempt_curr(rq, p, WF_FORK);
2683#ifdef CONFIG_SMP 2605#ifdef CONFIG_SMP
2684 if (p->sched_class->task_woken) 2606 if (p->sched_class->task_woken)
@@ -2898,7 +2820,7 @@ context_switch(struct rq *rq, struct task_struct *prev,
2898 struct mm_struct *mm, *oldmm; 2820 struct mm_struct *mm, *oldmm;
2899 2821
2900 prepare_task_switch(rq, prev, next); 2822 prepare_task_switch(rq, prev, next);
2901 trace_sched_switch(rq, prev, next); 2823 trace_sched_switch(prev, next);
2902 mm = next->mm; 2824 mm = next->mm;
2903 oldmm = prev->active_mm; 2825 oldmm = prev->active_mm;
2904 /* 2826 /*
@@ -3015,6 +2937,61 @@ static unsigned long calc_load_update;
3015unsigned long avenrun[3]; 2937unsigned long avenrun[3];
3016EXPORT_SYMBOL(avenrun); 2938EXPORT_SYMBOL(avenrun);
3017 2939
2940static long calc_load_fold_active(struct rq *this_rq)
2941{
2942 long nr_active, delta = 0;
2943
2944 nr_active = this_rq->nr_running;
2945 nr_active += (long) this_rq->nr_uninterruptible;
2946
2947 if (nr_active != this_rq->calc_load_active) {
2948 delta = nr_active - this_rq->calc_load_active;
2949 this_rq->calc_load_active = nr_active;
2950 }
2951
2952 return delta;
2953}
2954
2955#ifdef CONFIG_NO_HZ
2956/*
2957 * For NO_HZ we delay the active fold to the next LOAD_FREQ update.
2958 *
2959 * When making the ILB scale, we should try to pull this in as well.
2960 */
2961static atomic_long_t calc_load_tasks_idle;
2962
2963static void calc_load_account_idle(struct rq *this_rq)
2964{
2965 long delta;
2966
2967 delta = calc_load_fold_active(this_rq);
2968 if (delta)
2969 atomic_long_add(delta, &calc_load_tasks_idle);
2970}
2971
2972static long calc_load_fold_idle(void)
2973{
2974 long delta = 0;
2975
2976 /*
2977 * Its got a race, we don't care...
2978 */
2979 if (atomic_long_read(&calc_load_tasks_idle))
2980 delta = atomic_long_xchg(&calc_load_tasks_idle, 0);
2981
2982 return delta;
2983}
2984#else
2985static void calc_load_account_idle(struct rq *this_rq)
2986{
2987}
2988
2989static inline long calc_load_fold_idle(void)
2990{
2991 return 0;
2992}
2993#endif
2994
3018/** 2995/**
3019 * get_avenrun - get the load average array 2996 * get_avenrun - get the load average array
3020 * @loads: pointer to dest load array 2997 * @loads: pointer to dest load array
@@ -3061,20 +3038,22 @@ void calc_global_load(void)
3061} 3038}
3062 3039
3063/* 3040/*
3064 * Either called from update_cpu_load() or from a cpu going idle 3041 * Called from update_cpu_load() to periodically update this CPU's
3042 * active count.
3065 */ 3043 */
3066static void calc_load_account_active(struct rq *this_rq) 3044static void calc_load_account_active(struct rq *this_rq)
3067{ 3045{
3068 long nr_active, delta; 3046 long delta;
3069 3047
3070 nr_active = this_rq->nr_running; 3048 if (time_before(jiffies, this_rq->calc_load_update))
3071 nr_active += (long) this_rq->nr_uninterruptible; 3049 return;
3072 3050
3073 if (nr_active != this_rq->calc_load_active) { 3051 delta = calc_load_fold_active(this_rq);
3074 delta = nr_active - this_rq->calc_load_active; 3052 delta += calc_load_fold_idle();
3075 this_rq->calc_load_active = nr_active; 3053 if (delta)
3076 atomic_long_add(delta, &calc_load_tasks); 3054 atomic_long_add(delta, &calc_load_tasks);
3077 } 3055
3056 this_rq->calc_load_update += LOAD_FREQ;
3078} 3057}
3079 3058
3080/* 3059/*
@@ -3106,10 +3085,7 @@ static void update_cpu_load(struct rq *this_rq)
3106 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i; 3085 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
3107 } 3086 }
3108 3087
3109 if (time_after_eq(jiffies, this_rq->calc_load_update)) { 3088 calc_load_account_active(this_rq);
3110 this_rq->calc_load_update += LOAD_FREQ;
3111 calc_load_account_active(this_rq);
3112 }
3113} 3089}
3114 3090
3115#ifdef CONFIG_SMP 3091#ifdef CONFIG_SMP
@@ -3121,44 +3097,27 @@ static void update_cpu_load(struct rq *this_rq)
3121void sched_exec(void) 3097void sched_exec(void)
3122{ 3098{
3123 struct task_struct *p = current; 3099 struct task_struct *p = current;
3124 struct migration_req req;
3125 int dest_cpu, this_cpu;
3126 unsigned long flags; 3100 unsigned long flags;
3127 struct rq *rq; 3101 struct rq *rq;
3128 3102 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 3103
3137 rq = task_rq_lock(p, &flags); 3104 rq = task_rq_lock(p, &flags);
3138 put_cpu(); 3105 dest_cpu = p->sched_class->select_task_rq(rq, p, SD_BALANCE_EXEC, 0);
3106 if (dest_cpu == smp_processor_id())
3107 goto unlock;
3139 3108
3140 /* 3109 /*
3141 * select_task_rq() can race against ->cpus_allowed 3110 * select_task_rq() can race against ->cpus_allowed
3142 */ 3111 */
3143 if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed) 3112 if (cpumask_test_cpu(dest_cpu, &p->cpus_allowed) &&
3144 || unlikely(!cpu_active(dest_cpu))) { 3113 likely(cpu_active(dest_cpu)) && migrate_task(p, dest_cpu)) {
3145 task_rq_unlock(rq, &flags); 3114 struct migration_arg arg = { p, dest_cpu };
3146 goto again;
3147 }
3148 3115
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
3154 get_task_struct(mt);
3155 task_rq_unlock(rq, &flags); 3116 task_rq_unlock(rq, &flags);
3156 wake_up_process(mt); 3117 stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
3157 put_task_struct(mt);
3158 wait_for_completion(&req.done);
3159
3160 return; 3118 return;
3161 } 3119 }
3120unlock:
3162 task_rq_unlock(rq, &flags); 3121 task_rq_unlock(rq, &flags);
3163} 3122}
3164 3123
@@ -3630,23 +3589,9 @@ static inline void schedule_debug(struct task_struct *prev)
3630 3589
3631static void put_prev_task(struct rq *rq, struct task_struct *prev) 3590static void put_prev_task(struct rq *rq, struct task_struct *prev)
3632{ 3591{
3633 if (prev->state == TASK_RUNNING) { 3592 if (prev->se.on_rq)
3634 u64 runtime = prev->se.sum_exec_runtime; 3593 update_rq_clock(rq);
3635 3594 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); 3595 prev->sched_class->put_prev_task(rq, prev);
3651} 3596}
3652 3597
@@ -3709,14 +3654,13 @@ need_resched_nonpreemptible:
3709 hrtick_clear(rq); 3654 hrtick_clear(rq);
3710 3655
3711 raw_spin_lock_irq(&rq->lock); 3656 raw_spin_lock_irq(&rq->lock);
3712 update_rq_clock(rq);
3713 clear_tsk_need_resched(prev); 3657 clear_tsk_need_resched(prev);
3714 3658
3715 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) { 3659 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
3716 if (unlikely(signal_pending_state(prev->state, prev))) 3660 if (unlikely(signal_pending_state(prev->state, prev)))
3717 prev->state = TASK_RUNNING; 3661 prev->state = TASK_RUNNING;
3718 else 3662 else
3719 deactivate_task(rq, prev, 1); 3663 deactivate_task(rq, prev, DEQUEUE_SLEEP);
3720 switch_count = &prev->nvcsw; 3664 switch_count = &prev->nvcsw;
3721 } 3665 }
3722 3666
@@ -4039,8 +3983,7 @@ do_wait_for_common(struct completion *x, long timeout, int state)
4039 if (!x->done) { 3983 if (!x->done) {
4040 DECLARE_WAITQUEUE(wait, current); 3984 DECLARE_WAITQUEUE(wait, current);
4041 3985
4042 wait.flags |= WQ_FLAG_EXCLUSIVE; 3986 __add_wait_queue_tail_exclusive(&x->wait, &wait);
4043 __add_wait_queue_tail(&x->wait, &wait);
4044 do { 3987 do {
4045 if (signal_pending_state(state, current)) { 3988 if (signal_pending_state(state, current)) {
4046 timeout = -ERESTARTSYS; 3989 timeout = -ERESTARTSYS;
@@ -4266,7 +4209,6 @@ void rt_mutex_setprio(struct task_struct *p, int prio)
4266 BUG_ON(prio < 0 || prio > MAX_PRIO); 4209 BUG_ON(prio < 0 || prio > MAX_PRIO);
4267 4210
4268 rq = task_rq_lock(p, &flags); 4211 rq = task_rq_lock(p, &flags);
4269 update_rq_clock(rq);
4270 4212
4271 oldprio = p->prio; 4213 oldprio = p->prio;
4272 prev_class = p->sched_class; 4214 prev_class = p->sched_class;
@@ -4287,7 +4229,7 @@ void rt_mutex_setprio(struct task_struct *p, int prio)
4287 if (running) 4229 if (running)
4288 p->sched_class->set_curr_task(rq); 4230 p->sched_class->set_curr_task(rq);
4289 if (on_rq) { 4231 if (on_rq) {
4290 enqueue_task(rq, p, 0, oldprio < prio); 4232 enqueue_task(rq, p, oldprio < prio ? ENQUEUE_HEAD : 0);
4291 4233
4292 check_class_changed(rq, p, prev_class, oldprio, running); 4234 check_class_changed(rq, p, prev_class, oldprio, running);
4293 } 4235 }
@@ -4309,7 +4251,6 @@ void set_user_nice(struct task_struct *p, long nice)
4309 * the task might be in the middle of scheduling on another CPU. 4251 * the task might be in the middle of scheduling on another CPU.
4310 */ 4252 */
4311 rq = task_rq_lock(p, &flags); 4253 rq = task_rq_lock(p, &flags);
4312 update_rq_clock(rq);
4313 /* 4254 /*
4314 * The RT priorities are set via sched_setscheduler(), but we still 4255 * The RT priorities are set via sched_setscheduler(), but we still
4315 * allow the 'normal' nice value to be set - but as expected 4256 * allow the 'normal' nice value to be set - but as expected
@@ -4331,7 +4272,7 @@ void set_user_nice(struct task_struct *p, long nice)
4331 delta = p->prio - old_prio; 4272 delta = p->prio - old_prio;
4332 4273
4333 if (on_rq) { 4274 if (on_rq) {
4334 enqueue_task(rq, p, 0, false); 4275 enqueue_task(rq, p, 0);
4335 /* 4276 /*
4336 * If the task increased its priority or is running and 4277 * If the task increased its priority or is running and
4337 * lowered its priority, then reschedule its CPU: 4278 * lowered its priority, then reschedule its CPU:
@@ -4592,7 +4533,6 @@ recheck:
4592 raw_spin_unlock_irqrestore(&p->pi_lock, flags); 4533 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
4593 goto recheck; 4534 goto recheck;
4594 } 4535 }
4595 update_rq_clock(rq);
4596 on_rq = p->se.on_rq; 4536 on_rq = p->se.on_rq;
4597 running = task_current(rq, p); 4537 running = task_current(rq, p);
4598 if (on_rq) 4538 if (on_rq)
@@ -5329,17 +5269,15 @@ static inline void sched_init_granularity(void)
5329/* 5269/*
5330 * This is how migration works: 5270 * This is how migration works:
5331 * 5271 *
5332 * 1) we queue a struct migration_req structure in the source CPU's 5272 * 1) we invoke migration_cpu_stop() on the target CPU using
5333 * runqueue and wake up that CPU's migration thread. 5273 * stop_one_cpu().
5334 * 2) we down() the locked semaphore => thread blocks. 5274 * 2) stopper starts to run (implicitly forcing the migrated thread
5335 * 3) migration thread wakes up (implicitly it forces the migrated 5275 * off the CPU)
5336 * thread off the CPU) 5276 * 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 5277 * 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. 5278 * it and puts it into the right queue.
5341 * 6) migration thread up()s the semaphore. 5279 * 5) stopper completes and stop_one_cpu() returns and the migration
5342 * 7) we wake up and the migration is done. 5280 * is done.
5343 */ 5281 */
5344 5282
5345/* 5283/*
@@ -5353,12 +5291,23 @@ static inline void sched_init_granularity(void)
5353 */ 5291 */
5354int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask) 5292int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
5355{ 5293{
5356 struct migration_req req;
5357 unsigned long flags; 5294 unsigned long flags;
5358 struct rq *rq; 5295 struct rq *rq;
5296 unsigned int dest_cpu;
5359 int ret = 0; 5297 int ret = 0;
5360 5298
5299 /*
5300 * Serialize against TASK_WAKING so that ttwu() and wunt() can
5301 * drop the rq->lock and still rely on ->cpus_allowed.
5302 */
5303again:
5304 while (task_is_waking(p))
5305 cpu_relax();
5361 rq = task_rq_lock(p, &flags); 5306 rq = task_rq_lock(p, &flags);
5307 if (task_is_waking(p)) {
5308 task_rq_unlock(rq, &flags);
5309 goto again;
5310 }
5362 5311
5363 if (!cpumask_intersects(new_mask, cpu_active_mask)) { 5312 if (!cpumask_intersects(new_mask, cpu_active_mask)) {
5364 ret = -EINVAL; 5313 ret = -EINVAL;
@@ -5382,15 +5331,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)) 5331 if (cpumask_test_cpu(task_cpu(p), new_mask))
5383 goto out; 5332 goto out;
5384 5333
5385 if (migrate_task(p, cpumask_any_and(cpu_active_mask, new_mask), &req)) { 5334 dest_cpu = cpumask_any_and(cpu_active_mask, new_mask);
5335 if (migrate_task(p, dest_cpu)) {
5336 struct migration_arg arg = { p, dest_cpu };
5386 /* Need help from migration thread: drop lock and wait. */ 5337 /* 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); 5338 task_rq_unlock(rq, &flags);
5391 wake_up_process(mt); 5339 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); 5340 tlb_migrate_finish(p->mm);
5395 return 0; 5341 return 0;
5396 } 5342 }
@@ -5448,98 +5394,49 @@ fail:
5448 return ret; 5394 return ret;
5449} 5395}
5450 5396
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/* 5397/*
5457 * migration_thread - this is a highprio system thread that performs 5398 * migration_cpu_stop - this will be executed by a highprio stopper thread
5458 * thread migration by bumping thread off CPU then 'pushing' onto 5399 * and performs thread migration by bumping thread off CPU then
5459 * another runqueue. 5400 * 'pushing' onto another runqueue.
5460 */ 5401 */
5461static int migration_thread(void *data) 5402static int migration_cpu_stop(void *data)
5462{ 5403{
5463 int badcpu; 5404 struct migration_arg *arg = data;
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{
5522 int ret;
5523 5405
5406 /*
5407 * The original target cpu might have gone down and we might
5408 * be on another cpu but it doesn't matter.
5409 */
5524 local_irq_disable(); 5410 local_irq_disable();
5525 ret = __migrate_task(p, src_cpu, dest_cpu); 5411 __migrate_task(arg->task, raw_smp_processor_id(), arg->dest_cpu);
5526 local_irq_enable(); 5412 local_irq_enable();
5527 return ret; 5413 return 0;
5528} 5414}
5529 5415
5416#ifdef CONFIG_HOTPLUG_CPU
5530/* 5417/*
5531 * Figure out where task on dead CPU should go, use force if necessary. 5418 * Figure out where task on dead CPU should go, use force if necessary.
5532 */ 5419 */
5533static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p) 5420void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
5534{ 5421{
5535 int dest_cpu; 5422 struct rq *rq = cpu_rq(dead_cpu);
5423 int needs_cpu, uninitialized_var(dest_cpu);
5424 unsigned long flags;
5536 5425
5537again: 5426 local_irq_save(flags);
5538 dest_cpu = select_fallback_rq(dead_cpu, p);
5539 5427
5540 /* It can have affinity changed while we were choosing. */ 5428 raw_spin_lock(&rq->lock);
5541 if (unlikely(!__migrate_task_irq(p, dead_cpu, dest_cpu))) 5429 needs_cpu = (task_cpu(p) == dead_cpu) && (p->state != TASK_WAKING);
5542 goto again; 5430 if (needs_cpu)
5431 dest_cpu = select_fallback_rq(dead_cpu, p);
5432 raw_spin_unlock(&rq->lock);
5433 /*
5434 * It can only fail if we race with set_cpus_allowed(),
5435 * in the racer should migrate the task anyway.
5436 */
5437 if (needs_cpu)
5438 __migrate_task(p, dead_cpu, dest_cpu);
5439 local_irq_restore(flags);
5543} 5440}
5544 5441
5545/* 5442/*
@@ -5603,7 +5500,6 @@ void sched_idle_next(void)
5603 5500
5604 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1); 5501 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
5605 5502
5606 update_rq_clock(rq);
5607 activate_task(rq, p, 0); 5503 activate_task(rq, p, 0);
5608 5504
5609 raw_spin_unlock_irqrestore(&rq->lock, flags); 5505 raw_spin_unlock_irqrestore(&rq->lock, flags);
@@ -5658,7 +5554,6 @@ static void migrate_dead_tasks(unsigned int dead_cpu)
5658 for ( ; ; ) { 5554 for ( ; ; ) {
5659 if (!rq->nr_running) 5555 if (!rq->nr_running)
5660 break; 5556 break;
5661 update_rq_clock(rq);
5662 next = pick_next_task(rq); 5557 next = pick_next_task(rq);
5663 if (!next) 5558 if (!next)
5664 break; 5559 break;
@@ -5881,35 +5776,20 @@ static void set_rq_offline(struct rq *rq)
5881static int __cpuinit 5776static int __cpuinit
5882migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu) 5777migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
5883{ 5778{
5884 struct task_struct *p;
5885 int cpu = (long)hcpu; 5779 int cpu = (long)hcpu;
5886 unsigned long flags; 5780 unsigned long flags;
5887 struct rq *rq; 5781 struct rq *rq = cpu_rq(cpu);
5888 5782
5889 switch (action) { 5783 switch (action) {
5890 5784
5891 case CPU_UP_PREPARE: 5785 case CPU_UP_PREPARE:
5892 case CPU_UP_PREPARE_FROZEN: 5786 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; 5787 rq->calc_load_update = calc_load_update;
5904 break; 5788 break;
5905 5789
5906 case CPU_ONLINE: 5790 case CPU_ONLINE:
5907 case CPU_ONLINE_FROZEN: 5791 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 */ 5792 /* Update our root-domain */
5912 rq = cpu_rq(cpu);
5913 raw_spin_lock_irqsave(&rq->lock, flags); 5793 raw_spin_lock_irqsave(&rq->lock, flags);
5914 if (rq->rd) { 5794 if (rq->rd) {
5915 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span)); 5795 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
@@ -5920,61 +5800,24 @@ migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
5920 break; 5800 break;
5921 5801
5922#ifdef CONFIG_HOTPLUG_CPU 5802#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: 5803 case CPU_DEAD:
5936 case CPU_DEAD_FROZEN: 5804 case CPU_DEAD_FROZEN:
5937 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
5938 migrate_live_tasks(cpu); 5805 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) */ 5806 /* Idle task back to normal (off runqueue, low prio) */
5944 raw_spin_lock_irq(&rq->lock); 5807 raw_spin_lock_irq(&rq->lock);
5945 update_rq_clock(rq);
5946 deactivate_task(rq, rq->idle, 0); 5808 deactivate_task(rq, rq->idle, 0);
5947 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0); 5809 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
5948 rq->idle->sched_class = &idle_sched_class; 5810 rq->idle->sched_class = &idle_sched_class;
5949 migrate_dead_tasks(cpu); 5811 migrate_dead_tasks(cpu);
5950 raw_spin_unlock_irq(&rq->lock); 5812 raw_spin_unlock_irq(&rq->lock);
5951 cpuset_unlock();
5952 migrate_nr_uninterruptible(rq); 5813 migrate_nr_uninterruptible(rq);
5953 BUG_ON(rq->nr_running != 0); 5814 BUG_ON(rq->nr_running != 0);
5954 calc_global_load_remove(rq); 5815 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; 5816 break;
5973 5817
5974 case CPU_DYING: 5818 case CPU_DYING:
5975 case CPU_DYING_FROZEN: 5819 case CPU_DYING_FROZEN:
5976 /* Update our root-domain */ 5820 /* Update our root-domain */
5977 rq = cpu_rq(cpu);
5978 raw_spin_lock_irqsave(&rq->lock, flags); 5821 raw_spin_lock_irqsave(&rq->lock, flags);
5979 if (rq->rd) { 5822 if (rq->rd) {
5980 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span)); 5823 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
@@ -6305,6 +6148,9 @@ cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
6305 struct rq *rq = cpu_rq(cpu); 6148 struct rq *rq = cpu_rq(cpu);
6306 struct sched_domain *tmp; 6149 struct sched_domain *tmp;
6307 6150
6151 for (tmp = sd; tmp; tmp = tmp->parent)
6152 tmp->span_weight = cpumask_weight(sched_domain_span(tmp));
6153
6308 /* Remove the sched domains which do not contribute to scheduling. */ 6154 /* Remove the sched domains which do not contribute to scheduling. */
6309 for (tmp = sd; tmp; ) { 6155 for (tmp = sd; tmp; ) {
6310 struct sched_domain *parent = tmp->parent; 6156 struct sched_domain *parent = tmp->parent;
@@ -7788,10 +7634,8 @@ void __init sched_init(void)
7788 rq->push_cpu = 0; 7634 rq->push_cpu = 0;
7789 rq->cpu = i; 7635 rq->cpu = i;
7790 rq->online = 0; 7636 rq->online = 0;
7791 rq->migration_thread = NULL;
7792 rq->idle_stamp = 0; 7637 rq->idle_stamp = 0;
7793 rq->avg_idle = 2*sysctl_sched_migration_cost; 7638 rq->avg_idle = 2*sysctl_sched_migration_cost;
7794 INIT_LIST_HEAD(&rq->migration_queue);
7795 rq_attach_root(rq, &def_root_domain); 7639 rq_attach_root(rq, &def_root_domain);
7796#endif 7640#endif
7797 init_rq_hrtick(rq); 7641 init_rq_hrtick(rq);
@@ -7892,7 +7736,6 @@ static void normalize_task(struct rq *rq, struct task_struct *p)
7892{ 7736{
7893 int on_rq; 7737 int on_rq;
7894 7738
7895 update_rq_clock(rq);
7896 on_rq = p->se.on_rq; 7739 on_rq = p->se.on_rq;
7897 if (on_rq) 7740 if (on_rq)
7898 deactivate_task(rq, p, 0); 7741 deactivate_task(rq, p, 0);
@@ -7919,9 +7762,9 @@ void normalize_rt_tasks(void)
7919 7762
7920 p->se.exec_start = 0; 7763 p->se.exec_start = 0;
7921#ifdef CONFIG_SCHEDSTATS 7764#ifdef CONFIG_SCHEDSTATS
7922 p->se.wait_start = 0; 7765 p->se.statistics.wait_start = 0;
7923 p->se.sleep_start = 0; 7766 p->se.statistics.sleep_start = 0;
7924 p->se.block_start = 0; 7767 p->se.statistics.block_start = 0;
7925#endif 7768#endif
7926 7769
7927 if (!rt_task(p)) { 7770 if (!rt_task(p)) {
@@ -8254,8 +8097,6 @@ void sched_move_task(struct task_struct *tsk)
8254 8097
8255 rq = task_rq_lock(tsk, &flags); 8098 rq = task_rq_lock(tsk, &flags);
8256 8099
8257 update_rq_clock(rq);
8258
8259 running = task_current(rq, tsk); 8100 running = task_current(rq, tsk);
8260 on_rq = tsk->se.on_rq; 8101 on_rq = tsk->se.on_rq;
8261 8102
@@ -8274,7 +8115,7 @@ void sched_move_task(struct task_struct *tsk)
8274 if (unlikely(running)) 8115 if (unlikely(running))
8275 tsk->sched_class->set_curr_task(rq); 8116 tsk->sched_class->set_curr_task(rq);
8276 if (on_rq) 8117 if (on_rq)
8277 enqueue_task(rq, tsk, 0, false); 8118 enqueue_task(rq, tsk, 0);
8278 8119
8279 task_rq_unlock(rq, &flags); 8120 task_rq_unlock(rq, &flags);
8280} 8121}
@@ -9088,43 +8929,32 @@ struct cgroup_subsys cpuacct_subsys = {
9088 8929
9089#ifndef CONFIG_SMP 8930#ifndef CONFIG_SMP
9090 8931
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) 8932void synchronize_sched_expedited(void)
9098{ 8933{
8934 barrier();
9099} 8935}
9100EXPORT_SYMBOL_GPL(synchronize_sched_expedited); 8936EXPORT_SYMBOL_GPL(synchronize_sched_expedited);
9101 8937
9102#else /* #ifndef CONFIG_SMP */ 8938#else /* #ifndef CONFIG_SMP */
9103 8939
9104static DEFINE_PER_CPU(struct migration_req, rcu_migration_req); 8940static atomic_t synchronize_sched_expedited_count = ATOMIC_INIT(0);
9105static DEFINE_MUTEX(rcu_sched_expedited_mutex);
9106
9107#define RCU_EXPEDITED_STATE_POST -2
9108#define RCU_EXPEDITED_STATE_IDLE -1
9109
9110static int rcu_expedited_state = RCU_EXPEDITED_STATE_IDLE;
9111 8941
9112int rcu_expedited_torture_stats(char *page) 8942static int synchronize_sched_expedited_cpu_stop(void *data)
9113{ 8943{
9114 int cnt = 0; 8944 /*
9115 int cpu; 8945 * There must be a full memory barrier on each affected CPU
9116 8946 * between the time that try_stop_cpus() is called and the
9117 cnt += sprintf(&page[cnt], "state: %d /", rcu_expedited_state); 8947 * time that it returns.
9118 for_each_online_cpu(cpu) { 8948 *
9119 cnt += sprintf(&page[cnt], " %d:%d", 8949 * In the current initial implementation of cpu_stop, the
9120 cpu, per_cpu(rcu_migration_req, cpu).dest_cpu); 8950 * above condition is already met when the control reaches
9121 } 8951 * this point and the following smp_mb() is not strictly
9122 cnt += sprintf(&page[cnt], "\n"); 8952 * necessary. Do smp_mb() anyway for documentation and
9123 return cnt; 8953 * robustness against future implementation changes.
8954 */
8955 smp_mb(); /* See above comment block. */
8956 return 0;
9124} 8957}
9125EXPORT_SYMBOL_GPL(rcu_expedited_torture_stats);
9126
9127static long synchronize_sched_expedited_count;
9128 8958
9129/* 8959/*
9130 * Wait for an rcu-sched grace period to elapse, but use "big hammer" 8960 * Wait for an rcu-sched grace period to elapse, but use "big hammer"
@@ -9138,18 +8968,14 @@ static long synchronize_sched_expedited_count;
9138 */ 8968 */
9139void synchronize_sched_expedited(void) 8969void synchronize_sched_expedited(void)
9140{ 8970{
9141 int cpu; 8971 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 8972
9149 smp_mb(); /* ensure prior mod happens before capturing snap. */ 8973 smp_mb(); /* ensure prior mod happens before capturing snap. */
9150 snap = ACCESS_ONCE(synchronize_sched_expedited_count) + 1; 8974 snap = atomic_read(&synchronize_sched_expedited_count) + 1;
9151 get_online_cpus(); 8975 get_online_cpus();
9152 while (!mutex_trylock(&rcu_sched_expedited_mutex)) { 8976 while (try_stop_cpus(cpu_online_mask,
8977 synchronize_sched_expedited_cpu_stop,
8978 NULL) == -EAGAIN) {
9153 put_online_cpus(); 8979 put_online_cpus();
9154 if (trycount++ < 10) 8980 if (trycount++ < 10)
9155 udelay(trycount * num_online_cpus()); 8981 udelay(trycount * num_online_cpus());
@@ -9157,41 +8983,15 @@ void synchronize_sched_expedited(void)
9157 synchronize_sched(); 8983 synchronize_sched();
9158 return; 8984 return;
9159 } 8985 }
9160 if (ACCESS_ONCE(synchronize_sched_expedited_count) - snap > 0) { 8986 if (atomic_read(&synchronize_sched_expedited_count) - snap > 0) {
9161 smp_mb(); /* ensure test happens before caller kfree */ 8987 smp_mb(); /* ensure test happens before caller kfree */
9162 return; 8988 return;
9163 } 8989 }
9164 get_online_cpus(); 8990 get_online_cpus();
9165 } 8991 }
9166 rcu_expedited_state = RCU_EXPEDITED_STATE_POST; 8992 atomic_inc(&synchronize_sched_expedited_count);
9167 for_each_online_cpu(cpu) { 8993 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(); 8994 put_online_cpus();
9193 if (need_full_sync)
9194 synchronize_sched();
9195} 8995}
9196EXPORT_SYMBOL_GPL(synchronize_sched_expedited); 8996EXPORT_SYMBOL_GPL(synchronize_sched_expedited);
9197 8997