aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/sched.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/sched.c')
-rw-r--r--kernel/sched.c771
1 files changed, 264 insertions, 507 deletions
diff --git a/kernel/sched.c b/kernel/sched.c
index 3c2a54f70ffe..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>
@@ -503,8 +503,11 @@ struct rq {
503 #define CPU_LOAD_IDX_MAX 5 503 #define CPU_LOAD_IDX_MAX 5
504 unsigned long cpu_load[CPU_LOAD_IDX_MAX]; 504 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
505#ifdef CONFIG_NO_HZ 505#ifdef CONFIG_NO_HZ
506 u64 nohz_stamp;
506 unsigned char in_nohz_recently; 507 unsigned char in_nohz_recently;
507#endif 508#endif
509 unsigned int skip_clock_update;
510
508 /* capture load from *all* tasks on this cpu: */ 511 /* capture load from *all* tasks on this cpu: */
509 struct load_weight load; 512 struct load_weight load;
510 unsigned long nr_load_updates; 513 unsigned long nr_load_updates;
@@ -546,15 +549,13 @@ struct rq {
546 int post_schedule; 549 int post_schedule;
547 int active_balance; 550 int active_balance;
548 int push_cpu; 551 int push_cpu;
552 struct cpu_stop_work active_balance_work;
549 /* cpu of this runqueue: */ 553 /* cpu of this runqueue: */
550 int cpu; 554 int cpu;
551 int online; 555 int online;
552 556
553 unsigned long avg_load_per_task; 557 unsigned long avg_load_per_task;
554 558
555 struct task_struct *migration_thread;
556 struct list_head migration_queue;
557
558 u64 rt_avg; 559 u64 rt_avg;
559 u64 age_stamp; 560 u64 age_stamp;
560 u64 idle_stamp; 561 u64 idle_stamp;
@@ -602,6 +603,13 @@ static inline
602void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags) 603void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags)
603{ 604{
604 rq->curr->sched_class->check_preempt_curr(rq, p, flags); 605 rq->curr->sched_class->check_preempt_curr(rq, p, flags);
606
607 /*
608 * A queue event has occurred, and we're going to schedule. In
609 * this case, we can save a useless back to back clock update.
610 */
611 if (test_tsk_need_resched(p))
612 rq->skip_clock_update = 1;
605} 613}
606 614
607static inline int cpu_of(struct rq *rq) 615static inline int cpu_of(struct rq *rq)
@@ -636,7 +644,8 @@ static inline int cpu_of(struct rq *rq)
636 644
637inline void update_rq_clock(struct rq *rq) 645inline void update_rq_clock(struct rq *rq)
638{ 646{
639 rq->clock = sched_clock_cpu(cpu_of(rq)); 647 if (!rq->skip_clock_update)
648 rq->clock = sched_clock_cpu(cpu_of(rq));
640} 649}
641 650
642/* 651/*
@@ -914,16 +923,12 @@ static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
914#endif /* __ARCH_WANT_UNLOCKED_CTXSW */ 923#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
915 924
916/* 925/*
917 * Check whether the task is waking, we use this to synchronize against 926 * Check whether the task is waking, we use this to synchronize ->cpus_allowed
918 * ttwu() so that task_cpu() reports a stable number. 927 * against ttwu().
919 *
920 * We need to make an exception for PF_STARTING tasks because the fork
921 * path might require task_rq_lock() to work, eg. it can call
922 * set_cpus_allowed_ptr() from the cpuset clone_ns code.
923 */ 928 */
924static inline int task_is_waking(struct task_struct *p) 929static inline int task_is_waking(struct task_struct *p)
925{ 930{
926 return unlikely((p->state == TASK_WAKING) && !(p->flags & PF_STARTING)); 931 return unlikely(p->state == TASK_WAKING);
927} 932}
928 933
929/* 934/*
@@ -936,11 +941,9 @@ static inline struct rq *__task_rq_lock(struct task_struct *p)
936 struct rq *rq; 941 struct rq *rq;
937 942
938 for (;;) { 943 for (;;) {
939 while (task_is_waking(p))
940 cpu_relax();
941 rq = task_rq(p); 944 rq = task_rq(p);
942 raw_spin_lock(&rq->lock); 945 raw_spin_lock(&rq->lock);
943 if (likely(rq == task_rq(p) && !task_is_waking(p))) 946 if (likely(rq == task_rq(p)))
944 return rq; 947 return rq;
945 raw_spin_unlock(&rq->lock); 948 raw_spin_unlock(&rq->lock);
946 } 949 }
@@ -957,12 +960,10 @@ static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
957 struct rq *rq; 960 struct rq *rq;
958 961
959 for (;;) { 962 for (;;) {
960 while (task_is_waking(p))
961 cpu_relax();
962 local_irq_save(*flags); 963 local_irq_save(*flags);
963 rq = task_rq(p); 964 rq = task_rq(p);
964 raw_spin_lock(&rq->lock); 965 raw_spin_lock(&rq->lock);
965 if (likely(rq == task_rq(p) && !task_is_waking(p))) 966 if (likely(rq == task_rq(p)))
966 return rq; 967 return rq;
967 raw_spin_unlock_irqrestore(&rq->lock, *flags); 968 raw_spin_unlock_irqrestore(&rq->lock, *flags);
968 } 969 }
@@ -1239,6 +1240,17 @@ void wake_up_idle_cpu(int cpu)
1239 if (!tsk_is_polling(rq->idle)) 1240 if (!tsk_is_polling(rq->idle))
1240 smp_send_reschedule(cpu); 1241 smp_send_reschedule(cpu);
1241} 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
1242#endif /* CONFIG_NO_HZ */ 1254#endif /* CONFIG_NO_HZ */
1243 1255
1244static u64 sched_avg_period(void) 1256static u64 sched_avg_period(void)
@@ -1781,8 +1793,6 @@ static void double_rq_lock(struct rq *rq1, struct rq *rq2)
1781 raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING); 1793 raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
1782 } 1794 }
1783 } 1795 }
1784 update_rq_clock(rq1);
1785 update_rq_clock(rq2);
1786} 1796}
1787 1797
1788/* 1798/*
@@ -1813,7 +1823,7 @@ static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1813} 1823}
1814#endif 1824#endif
1815 1825
1816static void calc_load_account_active(struct rq *this_rq); 1826static void calc_load_account_idle(struct rq *this_rq);
1817static void update_sysctl(void); 1827static void update_sysctl(void);
1818static int get_update_sysctl_factor(void); 1828static int get_update_sysctl_factor(void);
1819 1829
@@ -1870,62 +1880,43 @@ static void set_load_weight(struct task_struct *p)
1870 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO]; 1880 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
1871} 1881}
1872 1882
1873static void update_avg(u64 *avg, u64 sample) 1883static void enqueue_task(struct rq *rq, struct task_struct *p, int flags)
1874{
1875 s64 diff = sample - *avg;
1876 *avg += diff >> 3;
1877}
1878
1879static void
1880enqueue_task(struct rq *rq, struct task_struct *p, int wakeup, bool head)
1881{ 1884{
1882 if (wakeup) 1885 update_rq_clock(rq);
1883 p->se.start_runtime = p->se.sum_exec_runtime;
1884
1885 sched_info_queued(p); 1886 sched_info_queued(p);
1886 p->sched_class->enqueue_task(rq, p, wakeup, head); 1887 p->sched_class->enqueue_task(rq, p, flags);
1887 p->se.on_rq = 1; 1888 p->se.on_rq = 1;
1888} 1889}
1889 1890
1890static 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)
1891{ 1892{
1892 if (sleep) { 1893 update_rq_clock(rq);
1893 if (p->se.last_wakeup) {
1894 update_avg(&p->se.avg_overlap,
1895 p->se.sum_exec_runtime - p->se.last_wakeup);
1896 p->se.last_wakeup = 0;
1897 } else {
1898 update_avg(&p->se.avg_wakeup,
1899 sysctl_sched_wakeup_granularity);
1900 }
1901 }
1902
1903 sched_info_dequeued(p); 1894 sched_info_dequeued(p);
1904 p->sched_class->dequeue_task(rq, p, sleep); 1895 p->sched_class->dequeue_task(rq, p, flags);
1905 p->se.on_rq = 0; 1896 p->se.on_rq = 0;
1906} 1897}
1907 1898
1908/* 1899/*
1909 * activate_task - move a task to the runqueue. 1900 * activate_task - move a task to the runqueue.
1910 */ 1901 */
1911static 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)
1912{ 1903{
1913 if (task_contributes_to_load(p)) 1904 if (task_contributes_to_load(p))
1914 rq->nr_uninterruptible--; 1905 rq->nr_uninterruptible--;
1915 1906
1916 enqueue_task(rq, p, wakeup, false); 1907 enqueue_task(rq, p, flags);
1917 inc_nr_running(rq); 1908 inc_nr_running(rq);
1918} 1909}
1919 1910
1920/* 1911/*
1921 * deactivate_task - remove a task from the runqueue. 1912 * deactivate_task - remove a task from the runqueue.
1922 */ 1913 */
1923static 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)
1924{ 1915{
1925 if (task_contributes_to_load(p)) 1916 if (task_contributes_to_load(p))
1926 rq->nr_uninterruptible++; 1917 rq->nr_uninterruptible++;
1927 1918
1928 dequeue_task(rq, p, sleep); 1919 dequeue_task(rq, p, flags);
1929 dec_nr_running(rq); 1920 dec_nr_running(rq);
1930} 1921}
1931 1922
@@ -2054,21 +2045,18 @@ void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
2054 __set_task_cpu(p, new_cpu); 2045 __set_task_cpu(p, new_cpu);
2055} 2046}
2056 2047
2057struct migration_req { 2048struct migration_arg {
2058 struct list_head list;
2059
2060 struct task_struct *task; 2049 struct task_struct *task;
2061 int dest_cpu; 2050 int dest_cpu;
2062
2063 struct completion done;
2064}; 2051};
2065 2052
2053static int migration_cpu_stop(void *data);
2054
2066/* 2055/*
2067 * The task's runqueue lock must be held. 2056 * The task's runqueue lock must be held.
2068 * Returns true if you have to wait for migration thread. 2057 * Returns true if you have to wait for migration thread.
2069 */ 2058 */
2070static int 2059static bool migrate_task(struct task_struct *p, int dest_cpu)
2071migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
2072{ 2060{
2073 struct rq *rq = task_rq(p); 2061 struct rq *rq = task_rq(p);
2074 2062
@@ -2076,58 +2064,7 @@ migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
2076 * If the task is not on a runqueue (and not running), then 2064 * If the task is not on a runqueue (and not running), then
2077 * the next wake-up will properly place the task. 2065 * the next wake-up will properly place the task.
2078 */ 2066 */
2079 if (!p->se.on_rq && !task_running(rq, p)) 2067 return p->se.on_rq || task_running(rq, p);
2080 return 0;
2081
2082 init_completion(&req->done);
2083 req->task = p;
2084 req->dest_cpu = dest_cpu;
2085 list_add(&req->list, &rq->migration_queue);
2086
2087 return 1;
2088}
2089
2090/*
2091 * wait_task_context_switch - wait for a thread to complete at least one
2092 * context switch.
2093 *
2094 * @p must not be current.
2095 */
2096void wait_task_context_switch(struct task_struct *p)
2097{
2098 unsigned long nvcsw, nivcsw, flags;
2099 int running;
2100 struct rq *rq;
2101
2102 nvcsw = p->nvcsw;
2103 nivcsw = p->nivcsw;
2104 for (;;) {
2105 /*
2106 * The runqueue is assigned before the actual context
2107 * switch. We need to take the runqueue lock.
2108 *
2109 * We could check initially without the lock but it is
2110 * very likely that we need to take the lock in every
2111 * iteration.
2112 */
2113 rq = task_rq_lock(p, &flags);
2114 running = task_running(rq, p);
2115 task_rq_unlock(rq, &flags);
2116
2117 if (likely(!running))
2118 break;
2119 /*
2120 * The switch count is incremented before the actual
2121 * context switch. We thus wait for two switches to be
2122 * sure at least one completed.
2123 */
2124 if ((p->nvcsw - nvcsw) > 1)
2125 break;
2126 if ((p->nivcsw - nivcsw) > 1)
2127 break;
2128
2129 cpu_relax();
2130 }
2131} 2068}
2132 2069
2133/* 2070/*
@@ -2185,7 +2122,7 @@ unsigned long wait_task_inactive(struct task_struct *p, long match_state)
2185 * just go back and repeat. 2122 * just go back and repeat.
2186 */ 2123 */
2187 rq = task_rq_lock(p, &flags); 2124 rq = task_rq_lock(p, &flags);
2188 trace_sched_wait_task(rq, p); 2125 trace_sched_wait_task(p);
2189 running = task_running(rq, p); 2126 running = task_running(rq, p);
2190 on_rq = p->se.on_rq; 2127 on_rq = p->se.on_rq;
2191 ncsw = 0; 2128 ncsw = 0;
@@ -2283,6 +2220,9 @@ void task_oncpu_function_call(struct task_struct *p,
2283} 2220}
2284 2221
2285#ifdef CONFIG_SMP 2222#ifdef CONFIG_SMP
2223/*
2224 * ->cpus_allowed is protected by either TASK_WAKING or rq->lock held.
2225 */
2286static int select_fallback_rq(int cpu, struct task_struct *p) 2226static int select_fallback_rq(int cpu, struct task_struct *p)
2287{ 2227{
2288 int dest_cpu; 2228 int dest_cpu;
@@ -2299,12 +2239,8 @@ static int select_fallback_rq(int cpu, struct task_struct *p)
2299 return dest_cpu; 2239 return dest_cpu;
2300 2240
2301 /* No more Mr. Nice Guy. */ 2241 /* No more Mr. Nice Guy. */
2302 if (dest_cpu >= nr_cpu_ids) { 2242 if (unlikely(dest_cpu >= nr_cpu_ids)) {
2303 rcu_read_lock(); 2243 dest_cpu = cpuset_cpus_allowed_fallback(p);
2304 cpuset_cpus_allowed_locked(p, &p->cpus_allowed);
2305 rcu_read_unlock();
2306 dest_cpu = cpumask_any_and(cpu_active_mask, &p->cpus_allowed);
2307
2308 /* 2244 /*
2309 * Don't tell them about moving exiting tasks or 2245 * Don't tell them about moving exiting tasks or
2310 * kernel threads (both mm NULL), since they never 2246 * kernel threads (both mm NULL), since they never
@@ -2321,17 +2257,12 @@ static int select_fallback_rq(int cpu, struct task_struct *p)
2321} 2257}
2322 2258
2323/* 2259/*
2324 * 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.
2325 * holding rq->lock we need to ensure ->cpus_allowed is stable, this is done
2326 * by:
2327 *
2328 * exec: is unstable, retry loop
2329 * fork & wake-up: serialize ->cpus_allowed against TASK_WAKING
2330 */ 2261 */
2331static inline 2262static inline
2332int 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)
2333{ 2264{
2334 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);
2335 2266
2336 /* 2267 /*
2337 * 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
@@ -2349,6 +2280,12 @@ int select_task_rq(struct task_struct *p, int sd_flags, int wake_flags)
2349 2280
2350 return cpu; 2281 return cpu;
2351} 2282}
2283
2284static void update_avg(u64 *avg, u64 sample)
2285{
2286 s64 diff = sample - *avg;
2287 *avg += diff >> 3;
2288}
2352#endif 2289#endif
2353 2290
2354/*** 2291/***
@@ -2370,16 +2307,13 @@ static int try_to_wake_up(struct task_struct *p, unsigned int state,
2370{ 2307{
2371 int cpu, orig_cpu, this_cpu, success = 0; 2308 int cpu, orig_cpu, this_cpu, success = 0;
2372 unsigned long flags; 2309 unsigned long flags;
2310 unsigned long en_flags = ENQUEUE_WAKEUP;
2373 struct rq *rq; 2311 struct rq *rq;
2374 2312
2375 if (!sched_feat(SYNC_WAKEUPS))
2376 wake_flags &= ~WF_SYNC;
2377
2378 this_cpu = get_cpu(); 2313 this_cpu = get_cpu();
2379 2314
2380 smp_wmb(); 2315 smp_wmb();
2381 rq = task_rq_lock(p, &flags); 2316 rq = task_rq_lock(p, &flags);
2382 update_rq_clock(rq);
2383 if (!(p->state & state)) 2317 if (!(p->state & state))
2384 goto out; 2318 goto out;
2385 2319
@@ -2399,28 +2333,26 @@ static int try_to_wake_up(struct task_struct *p, unsigned int state,
2399 * 2333 *
2400 * First fix up the nr_uninterruptible count: 2334 * First fix up the nr_uninterruptible count:
2401 */ 2335 */
2402 if (task_contributes_to_load(p)) 2336 if (task_contributes_to_load(p)) {
2403 rq->nr_uninterruptible--; 2337 if (likely(cpu_online(orig_cpu)))
2338 rq->nr_uninterruptible--;
2339 else
2340 this_rq()->nr_uninterruptible--;
2341 }
2404 p->state = TASK_WAKING; 2342 p->state = TASK_WAKING;
2405 2343
2406 if (p->sched_class->task_waking) 2344 if (p->sched_class->task_waking) {
2407 p->sched_class->task_waking(rq, p); 2345 p->sched_class->task_waking(rq, p);
2346 en_flags |= ENQUEUE_WAKING;
2347 }
2408 2348
2409 __task_rq_unlock(rq); 2349 cpu = select_task_rq(rq, p, SD_BALANCE_WAKE, wake_flags);
2410 2350 if (cpu != orig_cpu)
2411 cpu = select_task_rq(p, SD_BALANCE_WAKE, wake_flags);
2412 if (cpu != orig_cpu) {
2413 /*
2414 * Since we migrate the task without holding any rq->lock,
2415 * we need to be careful with task_rq_lock(), since that
2416 * might end up locking an invalid rq.
2417 */
2418 set_task_cpu(p, cpu); 2351 set_task_cpu(p, cpu);
2419 } 2352 __task_rq_unlock(rq);
2420 2353
2421 rq = cpu_rq(cpu); 2354 rq = cpu_rq(cpu);
2422 raw_spin_lock(&rq->lock); 2355 raw_spin_lock(&rq->lock);
2423 update_rq_clock(rq);
2424 2356
2425 /* 2357 /*
2426 * We migrated the task without holding either rq->lock, however 2358 * We migrated the task without holding either rq->lock, however
@@ -2448,36 +2380,20 @@ static int try_to_wake_up(struct task_struct *p, unsigned int state,
2448 2380
2449out_activate: 2381out_activate:
2450#endif /* CONFIG_SMP */ 2382#endif /* CONFIG_SMP */
2451 schedstat_inc(p, se.nr_wakeups); 2383 schedstat_inc(p, se.statistics.nr_wakeups);
2452 if (wake_flags & WF_SYNC) 2384 if (wake_flags & WF_SYNC)
2453 schedstat_inc(p, se.nr_wakeups_sync); 2385 schedstat_inc(p, se.statistics.nr_wakeups_sync);
2454 if (orig_cpu != cpu) 2386 if (orig_cpu != cpu)
2455 schedstat_inc(p, se.nr_wakeups_migrate); 2387 schedstat_inc(p, se.statistics.nr_wakeups_migrate);
2456 if (cpu == this_cpu) 2388 if (cpu == this_cpu)
2457 schedstat_inc(p, se.nr_wakeups_local); 2389 schedstat_inc(p, se.statistics.nr_wakeups_local);
2458 else 2390 else
2459 schedstat_inc(p, se.nr_wakeups_remote); 2391 schedstat_inc(p, se.statistics.nr_wakeups_remote);
2460 activate_task(rq, p, 1); 2392 activate_task(rq, p, en_flags);
2461 success = 1; 2393 success = 1;
2462 2394
2463 /*
2464 * Only attribute actual wakeups done by this task.
2465 */
2466 if (!in_interrupt()) {
2467 struct sched_entity *se = &current->se;
2468 u64 sample = se->sum_exec_runtime;
2469
2470 if (se->last_wakeup)
2471 sample -= se->last_wakeup;
2472 else
2473 sample -= se->start_runtime;
2474 update_avg(&se->avg_wakeup, sample);
2475
2476 se->last_wakeup = se->sum_exec_runtime;
2477 }
2478
2479out_running: 2395out_running:
2480 trace_sched_wakeup(rq, p, success); 2396 trace_sched_wakeup(p, success);
2481 check_preempt_curr(rq, p, wake_flags); 2397 check_preempt_curr(rq, p, wake_flags);
2482 2398
2483 p->state = TASK_RUNNING; 2399 p->state = TASK_RUNNING;
@@ -2537,42 +2453,9 @@ static void __sched_fork(struct task_struct *p)
2537 p->se.sum_exec_runtime = 0; 2453 p->se.sum_exec_runtime = 0;
2538 p->se.prev_sum_exec_runtime = 0; 2454 p->se.prev_sum_exec_runtime = 0;
2539 p->se.nr_migrations = 0; 2455 p->se.nr_migrations = 0;
2540 p->se.last_wakeup = 0;
2541 p->se.avg_overlap = 0;
2542 p->se.start_runtime = 0;
2543 p->se.avg_wakeup = sysctl_sched_wakeup_granularity;
2544 2456
2545#ifdef CONFIG_SCHEDSTATS 2457#ifdef CONFIG_SCHEDSTATS
2546 p->se.wait_start = 0; 2458 memset(&p->se.statistics, 0, sizeof(p->se.statistics));
2547 p->se.wait_max = 0;
2548 p->se.wait_count = 0;
2549 p->se.wait_sum = 0;
2550
2551 p->se.sleep_start = 0;
2552 p->se.sleep_max = 0;
2553 p->se.sum_sleep_runtime = 0;
2554
2555 p->se.block_start = 0;
2556 p->se.block_max = 0;
2557 p->se.exec_max = 0;
2558 p->se.slice_max = 0;
2559
2560 p->se.nr_migrations_cold = 0;
2561 p->se.nr_failed_migrations_affine = 0;
2562 p->se.nr_failed_migrations_running = 0;
2563 p->se.nr_failed_migrations_hot = 0;
2564 p->se.nr_forced_migrations = 0;
2565
2566 p->se.nr_wakeups = 0;
2567 p->se.nr_wakeups_sync = 0;
2568 p->se.nr_wakeups_migrate = 0;
2569 p->se.nr_wakeups_local = 0;
2570 p->se.nr_wakeups_remote = 0;
2571 p->se.nr_wakeups_affine = 0;
2572 p->se.nr_wakeups_affine_attempts = 0;
2573 p->se.nr_wakeups_passive = 0;
2574 p->se.nr_wakeups_idle = 0;
2575
2576#endif 2459#endif
2577 2460
2578 INIT_LIST_HEAD(&p->rt.run_list); 2461 INIT_LIST_HEAD(&p->rt.run_list);
@@ -2593,11 +2476,11 @@ void sched_fork(struct task_struct *p, int clone_flags)
2593 2476
2594 __sched_fork(p); 2477 __sched_fork(p);
2595 /* 2478 /*
2596 * We mark the process as waking here. This guarantees that 2479 * We mark the process as running here. This guarantees that
2597 * nobody will actually run it, and a signal or other external 2480 * nobody will actually run it, and a signal or other external
2598 * 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.
2599 */ 2482 */
2600 p->state = TASK_WAKING; 2483 p->state = TASK_RUNNING;
2601 2484
2602 /* 2485 /*
2603 * Revert to default priority/policy on fork if requested. 2486 * Revert to default priority/policy on fork if requested.
@@ -2664,31 +2547,27 @@ void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
2664 int cpu __maybe_unused = get_cpu(); 2547 int cpu __maybe_unused = get_cpu();
2665 2548
2666#ifdef CONFIG_SMP 2549#ifdef CONFIG_SMP
2550 rq = task_rq_lock(p, &flags);
2551 p->state = TASK_WAKING;
2552
2667 /* 2553 /*
2668 * Fork balancing, do it here and not earlier because: 2554 * Fork balancing, do it here and not earlier because:
2669 * - cpus_allowed can change in the fork path 2555 * - cpus_allowed can change in the fork path
2670 * - any previously selected cpu might disappear through hotplug 2556 * - any previously selected cpu might disappear through hotplug
2671 * 2557 *
2672 * 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
2673 * ->cpus_allowed is stable, we have preemption disabled, meaning 2559 * without people poking at ->cpus_allowed.
2674 * cpu_online_mask is stable.
2675 */ 2560 */
2676 cpu = select_task_rq(p, SD_BALANCE_FORK, 0); 2561 cpu = select_task_rq(rq, p, SD_BALANCE_FORK, 0);
2677 set_task_cpu(p, cpu); 2562 set_task_cpu(p, cpu);
2678#endif
2679
2680 /*
2681 * Since the task is not on the rq and we still have TASK_WAKING set
2682 * nobody else will migrate this task.
2683 */
2684 rq = cpu_rq(cpu);
2685 raw_spin_lock_irqsave(&rq->lock, flags);
2686 2563
2687 BUG_ON(p->state != TASK_WAKING);
2688 p->state = TASK_RUNNING; 2564 p->state = TASK_RUNNING;
2689 update_rq_clock(rq); 2565 task_rq_unlock(rq, &flags);
2566#endif
2567
2568 rq = task_rq_lock(p, &flags);
2690 activate_task(rq, p, 0); 2569 activate_task(rq, p, 0);
2691 trace_sched_wakeup_new(rq, p, 1); 2570 trace_sched_wakeup_new(p, 1);
2692 check_preempt_curr(rq, p, WF_FORK); 2571 check_preempt_curr(rq, p, WF_FORK);
2693#ifdef CONFIG_SMP 2572#ifdef CONFIG_SMP
2694 if (p->sched_class->task_woken) 2573 if (p->sched_class->task_woken)
@@ -2908,7 +2787,7 @@ context_switch(struct rq *rq, struct task_struct *prev,
2908 struct mm_struct *mm, *oldmm; 2787 struct mm_struct *mm, *oldmm;
2909 2788
2910 prepare_task_switch(rq, prev, next); 2789 prepare_task_switch(rq, prev, next);
2911 trace_sched_switch(rq, prev, next); 2790 trace_sched_switch(prev, next);
2912 mm = next->mm; 2791 mm = next->mm;
2913 oldmm = prev->active_mm; 2792 oldmm = prev->active_mm;
2914 /* 2793 /*
@@ -3025,6 +2904,61 @@ static unsigned long calc_load_update;
3025unsigned long avenrun[3]; 2904unsigned long avenrun[3];
3026EXPORT_SYMBOL(avenrun); 2905EXPORT_SYMBOL(avenrun);
3027 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
3028/** 2962/**
3029 * get_avenrun - get the load average array 2963 * get_avenrun - get the load average array
3030 * @loads: pointer to dest load array 2964 * @loads: pointer to dest load array
@@ -3071,20 +3005,22 @@ void calc_global_load(void)
3071} 3005}
3072 3006
3073/* 3007/*
3074 * 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.
3075 */ 3010 */
3076static void calc_load_account_active(struct rq *this_rq) 3011static void calc_load_account_active(struct rq *this_rq)
3077{ 3012{
3078 long nr_active, delta; 3013 long delta;
3079 3014
3080 nr_active = this_rq->nr_running; 3015 if (time_before(jiffies, this_rq->calc_load_update))
3081 nr_active += (long) this_rq->nr_uninterruptible; 3016 return;
3082 3017
3083 if (nr_active != this_rq->calc_load_active) { 3018 delta = calc_load_fold_active(this_rq);
3084 delta = nr_active - this_rq->calc_load_active; 3019 delta += calc_load_fold_idle();
3085 this_rq->calc_load_active = nr_active; 3020 if (delta)
3086 atomic_long_add(delta, &calc_load_tasks); 3021 atomic_long_add(delta, &calc_load_tasks);
3087 } 3022
3023 this_rq->calc_load_update += LOAD_FREQ;
3088} 3024}
3089 3025
3090/* 3026/*
@@ -3116,10 +3052,7 @@ static void update_cpu_load(struct rq *this_rq)
3116 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;
3117 } 3053 }
3118 3054
3119 if (time_after_eq(jiffies, this_rq->calc_load_update)) { 3055 calc_load_account_active(this_rq);
3120 this_rq->calc_load_update += LOAD_FREQ;
3121 calc_load_account_active(this_rq);
3122 }
3123} 3056}
3124 3057
3125#ifdef CONFIG_SMP 3058#ifdef CONFIG_SMP
@@ -3131,44 +3064,27 @@ static void update_cpu_load(struct rq *this_rq)
3131void sched_exec(void) 3064void sched_exec(void)
3132{ 3065{
3133 struct task_struct *p = current; 3066 struct task_struct *p = current;
3134 struct migration_req req;
3135 int dest_cpu, this_cpu;
3136 unsigned long flags; 3067 unsigned long flags;
3137 struct rq *rq; 3068 struct rq *rq;
3138 3069 int dest_cpu;
3139again:
3140 this_cpu = get_cpu();
3141 dest_cpu = select_task_rq(p, SD_BALANCE_EXEC, 0);
3142 if (dest_cpu == this_cpu) {
3143 put_cpu();
3144 return;
3145 }
3146 3070
3147 rq = task_rq_lock(p, &flags); 3071 rq = task_rq_lock(p, &flags);
3148 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;
3149 3075
3150 /* 3076 /*
3151 * select_task_rq() can race against ->cpus_allowed 3077 * select_task_rq() can race against ->cpus_allowed
3152 */ 3078 */
3153 if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed) 3079 if (cpumask_test_cpu(dest_cpu, &p->cpus_allowed) &&
3154 || unlikely(!cpu_active(dest_cpu))) { 3080 likely(cpu_active(dest_cpu)) && migrate_task(p, dest_cpu)) {
3155 task_rq_unlock(rq, &flags); 3081 struct migration_arg arg = { p, dest_cpu };
3156 goto again;
3157 }
3158 3082
3159 /* force the process onto the specified CPU */
3160 if (migrate_task(p, dest_cpu, &req)) {
3161 /* Need to wait for migration thread (might exit: take ref). */
3162 struct task_struct *mt = rq->migration_thread;
3163
3164 get_task_struct(mt);
3165 task_rq_unlock(rq, &flags); 3083 task_rq_unlock(rq, &flags);
3166 wake_up_process(mt); 3084 stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
3167 put_task_struct(mt);
3168 wait_for_completion(&req.done);
3169
3170 return; 3085 return;
3171 } 3086 }
3087unlock:
3172 task_rq_unlock(rq, &flags); 3088 task_rq_unlock(rq, &flags);
3173} 3089}
3174 3090
@@ -3640,23 +3556,9 @@ static inline void schedule_debug(struct task_struct *prev)
3640 3556
3641static void put_prev_task(struct rq *rq, struct task_struct *prev) 3557static void put_prev_task(struct rq *rq, struct task_struct *prev)
3642{ 3558{
3643 if (prev->state == TASK_RUNNING) { 3559 if (prev->se.on_rq)
3644 u64 runtime = prev->se.sum_exec_runtime; 3560 update_rq_clock(rq);
3645 3561 rq->skip_clock_update = 0;
3646 runtime -= prev->se.prev_sum_exec_runtime;
3647 runtime = min_t(u64, runtime, 2*sysctl_sched_migration_cost);
3648
3649 /*
3650 * In order to avoid avg_overlap growing stale when we are
3651 * indeed overlapping and hence not getting put to sleep, grow
3652 * the avg_overlap on preemption.
3653 *
3654 * We use the average preemption runtime because that
3655 * correlates to the amount of cache footprint a task can
3656 * build up.
3657 */
3658 update_avg(&prev->se.avg_overlap, runtime);
3659 }
3660 prev->sched_class->put_prev_task(rq, prev); 3562 prev->sched_class->put_prev_task(rq, prev);
3661} 3563}
3662 3564
@@ -3706,7 +3608,7 @@ need_resched:
3706 preempt_disable(); 3608 preempt_disable();
3707 cpu = smp_processor_id(); 3609 cpu = smp_processor_id();
3708 rq = cpu_rq(cpu); 3610 rq = cpu_rq(cpu);
3709 rcu_sched_qs(cpu); 3611 rcu_note_context_switch(cpu);
3710 prev = rq->curr; 3612 prev = rq->curr;
3711 switch_count = &prev->nivcsw; 3613 switch_count = &prev->nivcsw;
3712 3614
@@ -3719,14 +3621,13 @@ need_resched_nonpreemptible:
3719 hrtick_clear(rq); 3621 hrtick_clear(rq);
3720 3622
3721 raw_spin_lock_irq(&rq->lock); 3623 raw_spin_lock_irq(&rq->lock);
3722 update_rq_clock(rq);
3723 clear_tsk_need_resched(prev); 3624 clear_tsk_need_resched(prev);
3724 3625
3725 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) { 3626 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
3726 if (unlikely(signal_pending_state(prev->state, prev))) 3627 if (unlikely(signal_pending_state(prev->state, prev)))
3727 prev->state = TASK_RUNNING; 3628 prev->state = TASK_RUNNING;
3728 else 3629 else
3729 deactivate_task(rq, prev, 1); 3630 deactivate_task(rq, prev, DEQUEUE_SLEEP);
3730 switch_count = &prev->nvcsw; 3631 switch_count = &prev->nvcsw;
3731 } 3632 }
3732 3633
@@ -4049,8 +3950,7 @@ do_wait_for_common(struct completion *x, long timeout, int state)
4049 if (!x->done) { 3950 if (!x->done) {
4050 DECLARE_WAITQUEUE(wait, current); 3951 DECLARE_WAITQUEUE(wait, current);
4051 3952
4052 wait.flags |= WQ_FLAG_EXCLUSIVE; 3953 __add_wait_queue_tail_exclusive(&x->wait, &wait);
4053 __add_wait_queue_tail(&x->wait, &wait);
4054 do { 3954 do {
4055 if (signal_pending_state(state, current)) { 3955 if (signal_pending_state(state, current)) {
4056 timeout = -ERESTARTSYS; 3956 timeout = -ERESTARTSYS;
@@ -4276,7 +4176,6 @@ void rt_mutex_setprio(struct task_struct *p, int prio)
4276 BUG_ON(prio < 0 || prio > MAX_PRIO); 4176 BUG_ON(prio < 0 || prio > MAX_PRIO);
4277 4177
4278 rq = task_rq_lock(p, &flags); 4178 rq = task_rq_lock(p, &flags);
4279 update_rq_clock(rq);
4280 4179
4281 oldprio = p->prio; 4180 oldprio = p->prio;
4282 prev_class = p->sched_class; 4181 prev_class = p->sched_class;
@@ -4297,7 +4196,7 @@ void rt_mutex_setprio(struct task_struct *p, int prio)
4297 if (running) 4196 if (running)
4298 p->sched_class->set_curr_task(rq); 4197 p->sched_class->set_curr_task(rq);
4299 if (on_rq) { 4198 if (on_rq) {
4300 enqueue_task(rq, p, 0, oldprio < prio); 4199 enqueue_task(rq, p, oldprio < prio ? ENQUEUE_HEAD : 0);
4301 4200
4302 check_class_changed(rq, p, prev_class, oldprio, running); 4201 check_class_changed(rq, p, prev_class, oldprio, running);
4303 } 4202 }
@@ -4319,7 +4218,6 @@ void set_user_nice(struct task_struct *p, long nice)
4319 * 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.
4320 */ 4219 */
4321 rq = task_rq_lock(p, &flags); 4220 rq = task_rq_lock(p, &flags);
4322 update_rq_clock(rq);
4323 /* 4221 /*
4324 * The RT priorities are set via sched_setscheduler(), but we still 4222 * The RT priorities are set via sched_setscheduler(), but we still
4325 * allow the 'normal' nice value to be set - but as expected 4223 * allow the 'normal' nice value to be set - but as expected
@@ -4341,7 +4239,7 @@ void set_user_nice(struct task_struct *p, long nice)
4341 delta = p->prio - old_prio; 4239 delta = p->prio - old_prio;
4342 4240
4343 if (on_rq) { 4241 if (on_rq) {
4344 enqueue_task(rq, p, 0, false); 4242 enqueue_task(rq, p, 0);
4345 /* 4243 /*
4346 * If the task increased its priority or is running and 4244 * If the task increased its priority or is running and
4347 * lowered its priority, then reschedule its CPU: 4245 * lowered its priority, then reschedule its CPU:
@@ -4602,7 +4500,6 @@ recheck:
4602 raw_spin_unlock_irqrestore(&p->pi_lock, flags); 4500 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
4603 goto recheck; 4501 goto recheck;
4604 } 4502 }
4605 update_rq_clock(rq);
4606 on_rq = p->se.on_rq; 4503 on_rq = p->se.on_rq;
4607 running = task_current(rq, p); 4504 running = task_current(rq, p);
4608 if (on_rq) 4505 if (on_rq)
@@ -5339,17 +5236,15 @@ static inline void sched_init_granularity(void)
5339/* 5236/*
5340 * This is how migration works: 5237 * This is how migration works:
5341 * 5238 *
5342 * 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
5343 * runqueue and wake up that CPU's migration thread. 5240 * stop_one_cpu().
5344 * 2) we down() the locked semaphore => thread blocks. 5241 * 2) stopper starts to run (implicitly forcing the migrated thread
5345 * 3) migration thread wakes up (implicitly it forces the migrated 5242 * off the CPU)
5346 * thread off the CPU) 5243 * 3) it checks whether the migrated task is still in the wrong runqueue.
5347 * 4) it gets the migration request and checks whether the migrated 5244 * 4) if it's in the wrong runqueue then the migration thread removes
5348 * task is still in the wrong runqueue.
5349 * 5) if it's in the wrong runqueue then the migration thread removes
5350 * it and puts it into the right queue. 5245 * it and puts it into the right queue.
5351 * 6) migration thread up()s the semaphore. 5246 * 5) stopper completes and stop_one_cpu() returns and the migration
5352 * 7) we wake up and the migration is done. 5247 * is done.
5353 */ 5248 */
5354 5249
5355/* 5250/*
@@ -5363,12 +5258,23 @@ static inline void sched_init_granularity(void)
5363 */ 5258 */
5364int 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)
5365{ 5260{
5366 struct migration_req req;
5367 unsigned long flags; 5261 unsigned long flags;
5368 struct rq *rq; 5262 struct rq *rq;
5263 unsigned int dest_cpu;
5369 int ret = 0; 5264 int ret = 0;
5370 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();
5371 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 }
5372 5278
5373 if (!cpumask_intersects(new_mask, cpu_active_mask)) { 5279 if (!cpumask_intersects(new_mask, cpu_active_mask)) {
5374 ret = -EINVAL; 5280 ret = -EINVAL;
@@ -5392,15 +5298,12 @@ int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
5392 if (cpumask_test_cpu(task_cpu(p), new_mask)) 5298 if (cpumask_test_cpu(task_cpu(p), new_mask))
5393 goto out; 5299 goto out;
5394 5300
5395 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 };
5396 /* Need help from migration thread: drop lock and wait. */ 5304 /* Need help from migration thread: drop lock and wait. */
5397 struct task_struct *mt = rq->migration_thread;
5398
5399 get_task_struct(mt);
5400 task_rq_unlock(rq, &flags); 5305 task_rq_unlock(rq, &flags);
5401 wake_up_process(mt); 5306 stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
5402 put_task_struct(mt);
5403 wait_for_completion(&req.done);
5404 tlb_migrate_finish(p->mm); 5307 tlb_migrate_finish(p->mm);
5405 return 0; 5308 return 0;
5406 } 5309 }
@@ -5458,98 +5361,49 @@ fail:
5458 return ret; 5361 return ret;
5459} 5362}
5460 5363
5461#define RCU_MIGRATION_IDLE 0
5462#define RCU_MIGRATION_NEED_QS 1
5463#define RCU_MIGRATION_GOT_QS 2
5464#define RCU_MIGRATION_MUST_SYNC 3
5465
5466/* 5364/*
5467 * migration_thread - this is a highprio system thread that performs 5365 * migration_cpu_stop - this will be executed by a highprio stopper thread
5468 * thread migration by bumping thread off CPU then 'pushing' onto 5366 * and performs thread migration by bumping thread off CPU then
5469 * another runqueue. 5367 * 'pushing' onto another runqueue.
5470 */ 5368 */
5471static int migration_thread(void *data) 5369static int migration_cpu_stop(void *data)
5472{
5473 int badcpu;
5474 int cpu = (long)data;
5475 struct rq *rq;
5476
5477 rq = cpu_rq(cpu);
5478 BUG_ON(rq->migration_thread != current);
5479
5480 set_current_state(TASK_INTERRUPTIBLE);
5481 while (!kthread_should_stop()) {
5482 struct migration_req *req;
5483 struct list_head *head;
5484
5485 raw_spin_lock_irq(&rq->lock);
5486
5487 if (cpu_is_offline(cpu)) {
5488 raw_spin_unlock_irq(&rq->lock);
5489 break;
5490 }
5491
5492 if (rq->active_balance) {
5493 active_load_balance(rq, cpu);
5494 rq->active_balance = 0;
5495 }
5496
5497 head = &rq->migration_queue;
5498
5499 if (list_empty(head)) {
5500 raw_spin_unlock_irq(&rq->lock);
5501 schedule();
5502 set_current_state(TASK_INTERRUPTIBLE);
5503 continue;
5504 }
5505 req = list_entry(head->next, struct migration_req, list);
5506 list_del_init(head->next);
5507
5508 if (req->task != NULL) {
5509 raw_spin_unlock(&rq->lock);
5510 __migrate_task(req->task, cpu, req->dest_cpu);
5511 } else if (likely(cpu == (badcpu = smp_processor_id()))) {
5512 req->dest_cpu = RCU_MIGRATION_GOT_QS;
5513 raw_spin_unlock(&rq->lock);
5514 } else {
5515 req->dest_cpu = RCU_MIGRATION_MUST_SYNC;
5516 raw_spin_unlock(&rq->lock);
5517 WARN_ONCE(1, "migration_thread() on CPU %d, expected %d\n", badcpu, cpu);
5518 }
5519 local_irq_enable();
5520
5521 complete(&req->done);
5522 }
5523 __set_current_state(TASK_RUNNING);
5524
5525 return 0;
5526}
5527
5528#ifdef CONFIG_HOTPLUG_CPU
5529
5530static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
5531{ 5370{
5532 int ret; 5371 struct migration_arg *arg = data;
5533 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 */
5534 local_irq_disable(); 5377 local_irq_disable();
5535 ret = __migrate_task(p, src_cpu, dest_cpu); 5378 __migrate_task(arg->task, raw_smp_processor_id(), arg->dest_cpu);
5536 local_irq_enable(); 5379 local_irq_enable();
5537 return ret; 5380 return 0;
5538} 5381}
5539 5382
5383#ifdef CONFIG_HOTPLUG_CPU
5540/* 5384/*
5541 * 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.
5542 */ 5386 */
5543static 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)
5544{ 5388{
5545 int dest_cpu; 5389 struct rq *rq = cpu_rq(dead_cpu);
5390 int needs_cpu, uninitialized_var(dest_cpu);
5391 unsigned long flags;
5546 5392
5547again: 5393 local_irq_save(flags);
5548 dest_cpu = select_fallback_rq(dead_cpu, p);
5549 5394
5550 /* It can have affinity changed while we were choosing. */ 5395 raw_spin_lock(&rq->lock);
5551 if (unlikely(!__migrate_task_irq(p, dead_cpu, dest_cpu))) 5396 needs_cpu = (task_cpu(p) == dead_cpu) && (p->state != TASK_WAKING);
5552 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);
5553} 5407}
5554 5408
5555/* 5409/*
@@ -5613,7 +5467,6 @@ void sched_idle_next(void)
5613 5467
5614 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1); 5468 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
5615 5469
5616 update_rq_clock(rq);
5617 activate_task(rq, p, 0); 5470 activate_task(rq, p, 0);
5618 5471
5619 raw_spin_unlock_irqrestore(&rq->lock, flags); 5472 raw_spin_unlock_irqrestore(&rq->lock, flags);
@@ -5668,7 +5521,6 @@ static void migrate_dead_tasks(unsigned int dead_cpu)
5668 for ( ; ; ) { 5521 for ( ; ; ) {
5669 if (!rq->nr_running) 5522 if (!rq->nr_running)
5670 break; 5523 break;
5671 update_rq_clock(rq);
5672 next = pick_next_task(rq); 5524 next = pick_next_task(rq);
5673 if (!next) 5525 if (!next)
5674 break; 5526 break;
@@ -5891,35 +5743,20 @@ static void set_rq_offline(struct rq *rq)
5891static int __cpuinit 5743static int __cpuinit
5892migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu) 5744migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
5893{ 5745{
5894 struct task_struct *p;
5895 int cpu = (long)hcpu; 5746 int cpu = (long)hcpu;
5896 unsigned long flags; 5747 unsigned long flags;
5897 struct rq *rq; 5748 struct rq *rq = cpu_rq(cpu);
5898 5749
5899 switch (action) { 5750 switch (action) {
5900 5751
5901 case CPU_UP_PREPARE: 5752 case CPU_UP_PREPARE:
5902 case CPU_UP_PREPARE_FROZEN: 5753 case CPU_UP_PREPARE_FROZEN:
5903 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
5904 if (IS_ERR(p))
5905 return NOTIFY_BAD;
5906 kthread_bind(p, cpu);
5907 /* Must be high prio: stop_machine expects to yield to it. */
5908 rq = task_rq_lock(p, &flags);
5909 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
5910 task_rq_unlock(rq, &flags);
5911 get_task_struct(p);
5912 cpu_rq(cpu)->migration_thread = p;
5913 rq->calc_load_update = calc_load_update; 5754 rq->calc_load_update = calc_load_update;
5914 break; 5755 break;
5915 5756
5916 case CPU_ONLINE: 5757 case CPU_ONLINE:
5917 case CPU_ONLINE_FROZEN: 5758 case CPU_ONLINE_FROZEN:
5918 /* Strictly unnecessary, as first user will wake it. */
5919 wake_up_process(cpu_rq(cpu)->migration_thread);
5920
5921 /* Update our root-domain */ 5759 /* Update our root-domain */
5922 rq = cpu_rq(cpu);
5923 raw_spin_lock_irqsave(&rq->lock, flags); 5760 raw_spin_lock_irqsave(&rq->lock, flags);
5924 if (rq->rd) { 5761 if (rq->rd) {
5925 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span)); 5762 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
@@ -5930,61 +5767,24 @@ migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
5930 break; 5767 break;
5931 5768
5932#ifdef CONFIG_HOTPLUG_CPU 5769#ifdef CONFIG_HOTPLUG_CPU
5933 case CPU_UP_CANCELED:
5934 case CPU_UP_CANCELED_FROZEN:
5935 if (!cpu_rq(cpu)->migration_thread)
5936 break;
5937 /* Unbind it from offline cpu so it can run. Fall thru. */
5938 kthread_bind(cpu_rq(cpu)->migration_thread,
5939 cpumask_any(cpu_online_mask));
5940 kthread_stop(cpu_rq(cpu)->migration_thread);
5941 put_task_struct(cpu_rq(cpu)->migration_thread);
5942 cpu_rq(cpu)->migration_thread = NULL;
5943 break;
5944
5945 case CPU_DEAD: 5770 case CPU_DEAD:
5946 case CPU_DEAD_FROZEN: 5771 case CPU_DEAD_FROZEN:
5947 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
5948 migrate_live_tasks(cpu); 5772 migrate_live_tasks(cpu);
5949 rq = cpu_rq(cpu);
5950 kthread_stop(rq->migration_thread);
5951 put_task_struct(rq->migration_thread);
5952 rq->migration_thread = NULL;
5953 /* Idle task back to normal (off runqueue, low prio) */ 5773 /* Idle task back to normal (off runqueue, low prio) */
5954 raw_spin_lock_irq(&rq->lock); 5774 raw_spin_lock_irq(&rq->lock);
5955 update_rq_clock(rq);
5956 deactivate_task(rq, rq->idle, 0); 5775 deactivate_task(rq, rq->idle, 0);
5957 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0); 5776 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
5958 rq->idle->sched_class = &idle_sched_class; 5777 rq->idle->sched_class = &idle_sched_class;
5959 migrate_dead_tasks(cpu); 5778 migrate_dead_tasks(cpu);
5960 raw_spin_unlock_irq(&rq->lock); 5779 raw_spin_unlock_irq(&rq->lock);
5961 cpuset_unlock();
5962 migrate_nr_uninterruptible(rq); 5780 migrate_nr_uninterruptible(rq);
5963 BUG_ON(rq->nr_running != 0); 5781 BUG_ON(rq->nr_running != 0);
5964 calc_global_load_remove(rq); 5782 calc_global_load_remove(rq);
5965 /*
5966 * No need to migrate the tasks: it was best-effort if
5967 * they didn't take sched_hotcpu_mutex. Just wake up
5968 * the requestors.
5969 */
5970 raw_spin_lock_irq(&rq->lock);
5971 while (!list_empty(&rq->migration_queue)) {
5972 struct migration_req *req;
5973
5974 req = list_entry(rq->migration_queue.next,
5975 struct migration_req, list);
5976 list_del_init(&req->list);
5977 raw_spin_unlock_irq(&rq->lock);
5978 complete(&req->done);
5979 raw_spin_lock_irq(&rq->lock);
5980 }
5981 raw_spin_unlock_irq(&rq->lock);
5982 break; 5783 break;
5983 5784
5984 case CPU_DYING: 5785 case CPU_DYING:
5985 case CPU_DYING_FROZEN: 5786 case CPU_DYING_FROZEN:
5986 /* Update our root-domain */ 5787 /* Update our root-domain */
5987 rq = cpu_rq(cpu);
5988 raw_spin_lock_irqsave(&rq->lock, flags); 5788 raw_spin_lock_irqsave(&rq->lock, flags);
5989 if (rq->rd) { 5789 if (rq->rd) {
5990 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span)); 5790 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
@@ -6315,6 +6115,9 @@ cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
6315 struct rq *rq = cpu_rq(cpu); 6115 struct rq *rq = cpu_rq(cpu);
6316 struct sched_domain *tmp; 6116 struct sched_domain *tmp;
6317 6117
6118 for (tmp = sd; tmp; tmp = tmp->parent)
6119 tmp->span_weight = cpumask_weight(sched_domain_span(tmp));
6120
6318 /* Remove the sched domains which do not contribute to scheduling. */ 6121 /* Remove the sched domains which do not contribute to scheduling. */
6319 for (tmp = sd; tmp; ) { 6122 for (tmp = sd; tmp; ) {
6320 struct sched_domain *parent = tmp->parent; 6123 struct sched_domain *parent = tmp->parent;
@@ -7798,10 +7601,8 @@ void __init sched_init(void)
7798 rq->push_cpu = 0; 7601 rq->push_cpu = 0;
7799 rq->cpu = i; 7602 rq->cpu = i;
7800 rq->online = 0; 7603 rq->online = 0;
7801 rq->migration_thread = NULL;
7802 rq->idle_stamp = 0; 7604 rq->idle_stamp = 0;
7803 rq->avg_idle = 2*sysctl_sched_migration_cost; 7605 rq->avg_idle = 2*sysctl_sched_migration_cost;
7804 INIT_LIST_HEAD(&rq->migration_queue);
7805 rq_attach_root(rq, &def_root_domain); 7606 rq_attach_root(rq, &def_root_domain);
7806#endif 7607#endif
7807 init_rq_hrtick(rq); 7608 init_rq_hrtick(rq);
@@ -7902,7 +7703,6 @@ static void normalize_task(struct rq *rq, struct task_struct *p)
7902{ 7703{
7903 int on_rq; 7704 int on_rq;
7904 7705
7905 update_rq_clock(rq);
7906 on_rq = p->se.on_rq; 7706 on_rq = p->se.on_rq;
7907 if (on_rq) 7707 if (on_rq)
7908 deactivate_task(rq, p, 0); 7708 deactivate_task(rq, p, 0);
@@ -7929,9 +7729,9 @@ void normalize_rt_tasks(void)
7929 7729
7930 p->se.exec_start = 0; 7730 p->se.exec_start = 0;
7931#ifdef CONFIG_SCHEDSTATS 7731#ifdef CONFIG_SCHEDSTATS
7932 p->se.wait_start = 0; 7732 p->se.statistics.wait_start = 0;
7933 p->se.sleep_start = 0; 7733 p->se.statistics.sleep_start = 0;
7934 p->se.block_start = 0; 7734 p->se.statistics.block_start = 0;
7935#endif 7735#endif
7936 7736
7937 if (!rt_task(p)) { 7737 if (!rt_task(p)) {
@@ -8264,8 +8064,6 @@ void sched_move_task(struct task_struct *tsk)
8264 8064
8265 rq = task_rq_lock(tsk, &flags); 8065 rq = task_rq_lock(tsk, &flags);
8266 8066
8267 update_rq_clock(rq);
8268
8269 running = task_current(rq, tsk); 8067 running = task_current(rq, tsk);
8270 on_rq = tsk->se.on_rq; 8068 on_rq = tsk->se.on_rq;
8271 8069
@@ -8284,7 +8082,7 @@ void sched_move_task(struct task_struct *tsk)
8284 if (unlikely(running)) 8082 if (unlikely(running))
8285 tsk->sched_class->set_curr_task(rq); 8083 tsk->sched_class->set_curr_task(rq);
8286 if (on_rq) 8084 if (on_rq)
8287 enqueue_task(rq, tsk, 0, false); 8085 enqueue_task(rq, tsk, 0);
8288 8086
8289 task_rq_unlock(rq, &flags); 8087 task_rq_unlock(rq, &flags);
8290} 8088}
@@ -9098,43 +8896,32 @@ struct cgroup_subsys cpuacct_subsys = {
9098 8896
9099#ifndef CONFIG_SMP 8897#ifndef CONFIG_SMP
9100 8898
9101int rcu_expedited_torture_stats(char *page)
9102{
9103 return 0;
9104}
9105EXPORT_SYMBOL_GPL(rcu_expedited_torture_stats);
9106
9107void synchronize_sched_expedited(void) 8899void synchronize_sched_expedited(void)
9108{ 8900{
8901 barrier();
9109} 8902}
9110EXPORT_SYMBOL_GPL(synchronize_sched_expedited); 8903EXPORT_SYMBOL_GPL(synchronize_sched_expedited);
9111 8904
9112#else /* #ifndef CONFIG_SMP */ 8905#else /* #ifndef CONFIG_SMP */
9113 8906
9114static DEFINE_PER_CPU(struct migration_req, rcu_migration_req); 8907static atomic_t synchronize_sched_expedited_count = ATOMIC_INIT(0);
9115static DEFINE_MUTEX(rcu_sched_expedited_mutex);
9116 8908
9117#define RCU_EXPEDITED_STATE_POST -2 8909static int synchronize_sched_expedited_cpu_stop(void *data)
9118#define RCU_EXPEDITED_STATE_IDLE -1
9119
9120static int rcu_expedited_state = RCU_EXPEDITED_STATE_IDLE;
9121
9122int rcu_expedited_torture_stats(char *page)
9123{ 8910{
9124 int cnt = 0; 8911 /*
9125 int cpu; 8912 * There must be a full memory barrier on each affected CPU
9126 8913 * between the time that try_stop_cpus() is called and the
9127 cnt += sprintf(&page[cnt], "state: %d /", rcu_expedited_state); 8914 * time that it returns.
9128 for_each_online_cpu(cpu) { 8915 *
9129 cnt += sprintf(&page[cnt], " %d:%d", 8916 * In the current initial implementation of cpu_stop, the
9130 cpu, per_cpu(rcu_migration_req, cpu).dest_cpu); 8917 * above condition is already met when the control reaches
9131 } 8918 * this point and the following smp_mb() is not strictly
9132 cnt += sprintf(&page[cnt], "\n"); 8919 * necessary. Do smp_mb() anyway for documentation and
9133 return cnt; 8920 * robustness against future implementation changes.
8921 */
8922 smp_mb(); /* See above comment block. */
8923 return 0;
9134} 8924}
9135EXPORT_SYMBOL_GPL(rcu_expedited_torture_stats);
9136
9137static long synchronize_sched_expedited_count;
9138 8925
9139/* 8926/*
9140 * 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"
@@ -9148,18 +8935,14 @@ static long synchronize_sched_expedited_count;
9148 */ 8935 */
9149void synchronize_sched_expedited(void) 8936void synchronize_sched_expedited(void)
9150{ 8937{
9151 int cpu; 8938 int snap, trycount = 0;
9152 unsigned long flags;
9153 bool need_full_sync = 0;
9154 struct rq *rq;
9155 struct migration_req *req;
9156 long snap;
9157 int trycount = 0;
9158 8939
9159 smp_mb(); /* ensure prior mod happens before capturing snap. */ 8940 smp_mb(); /* ensure prior mod happens before capturing snap. */
9160 snap = ACCESS_ONCE(synchronize_sched_expedited_count) + 1; 8941 snap = atomic_read(&synchronize_sched_expedited_count) + 1;
9161 get_online_cpus(); 8942 get_online_cpus();
9162 while (!mutex_trylock(&rcu_sched_expedited_mutex)) { 8943 while (try_stop_cpus(cpu_online_mask,
8944 synchronize_sched_expedited_cpu_stop,
8945 NULL) == -EAGAIN) {
9163 put_online_cpus(); 8946 put_online_cpus();
9164 if (trycount++ < 10) 8947 if (trycount++ < 10)
9165 udelay(trycount * num_online_cpus()); 8948 udelay(trycount * num_online_cpus());
@@ -9167,41 +8950,15 @@ void synchronize_sched_expedited(void)
9167 synchronize_sched(); 8950 synchronize_sched();
9168 return; 8951 return;
9169 } 8952 }
9170 if (ACCESS_ONCE(synchronize_sched_expedited_count) - snap > 0) { 8953 if (atomic_read(&synchronize_sched_expedited_count) - snap > 0) {
9171 smp_mb(); /* ensure test happens before caller kfree */ 8954 smp_mb(); /* ensure test happens before caller kfree */
9172 return; 8955 return;
9173 } 8956 }
9174 get_online_cpus(); 8957 get_online_cpus();
9175 } 8958 }
9176 rcu_expedited_state = RCU_EXPEDITED_STATE_POST; 8959 atomic_inc(&synchronize_sched_expedited_count);
9177 for_each_online_cpu(cpu) { 8960 smp_mb__after_atomic_inc(); /* ensure post-GP actions seen after GP. */
9178 rq = cpu_rq(cpu);
9179 req = &per_cpu(rcu_migration_req, cpu);
9180 init_completion(&req->done);
9181 req->task = NULL;
9182 req->dest_cpu = RCU_MIGRATION_NEED_QS;
9183 raw_spin_lock_irqsave(&rq->lock, flags);
9184 list_add(&req->list, &rq->migration_queue);
9185 raw_spin_unlock_irqrestore(&rq->lock, flags);
9186 wake_up_process(rq->migration_thread);
9187 }
9188 for_each_online_cpu(cpu) {
9189 rcu_expedited_state = cpu;
9190 req = &per_cpu(rcu_migration_req, cpu);
9191 rq = cpu_rq(cpu);
9192 wait_for_completion(&req->done);
9193 raw_spin_lock_irqsave(&rq->lock, flags);
9194 if (unlikely(req->dest_cpu == RCU_MIGRATION_MUST_SYNC))
9195 need_full_sync = 1;
9196 req->dest_cpu = RCU_MIGRATION_IDLE;
9197 raw_spin_unlock_irqrestore(&rq->lock, flags);
9198 }
9199 rcu_expedited_state = RCU_EXPEDITED_STATE_IDLE;
9200 synchronize_sched_expedited_count++;
9201 mutex_unlock(&rcu_sched_expedited_mutex);
9202 put_online_cpus(); 8961 put_online_cpus();
9203 if (need_full_sync)
9204 synchronize_sched();
9205} 8962}
9206EXPORT_SYMBOL_GPL(synchronize_sched_expedited); 8963EXPORT_SYMBOL_GPL(synchronize_sched_expedited);
9207 8964