aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/sched.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/sched.c')
-rw-r--r--kernel/sched.c963
1 files changed, 373 insertions, 590 deletions
diff --git a/kernel/sched.c b/kernel/sched.c
index 6af210a7de70..f52a8801b7a2 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>
@@ -306,42 +306,6 @@ static int init_task_group_load = INIT_TASK_GROUP_LOAD;
306 */ 306 */
307struct task_group init_task_group; 307struct task_group init_task_group;
308 308
309/* return group to which a task belongs */
310static inline struct task_group *task_group(struct task_struct *p)
311{
312 struct task_group *tg;
313
314#ifdef CONFIG_CGROUP_SCHED
315 tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id),
316 struct task_group, css);
317#else
318 tg = &init_task_group;
319#endif
320 return tg;
321}
322
323/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
324static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
325{
326#ifdef CONFIG_FAIR_GROUP_SCHED
327 p->se.cfs_rq = task_group(p)->cfs_rq[cpu];
328 p->se.parent = task_group(p)->se[cpu];
329#endif
330
331#ifdef CONFIG_RT_GROUP_SCHED
332 p->rt.rt_rq = task_group(p)->rt_rq[cpu];
333 p->rt.parent = task_group(p)->rt_se[cpu];
334#endif
335}
336
337#else
338
339static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
340static inline struct task_group *task_group(struct task_struct *p)
341{
342 return NULL;
343}
344
345#endif /* CONFIG_CGROUP_SCHED */ 309#endif /* CONFIG_CGROUP_SCHED */
346 310
347/* CFS-related fields in a runqueue */ 311/* CFS-related fields in a runqueue */
@@ -493,8 +457,11 @@ struct rq {
493 #define CPU_LOAD_IDX_MAX 5 457 #define CPU_LOAD_IDX_MAX 5
494 unsigned long cpu_load[CPU_LOAD_IDX_MAX]; 458 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
495#ifdef CONFIG_NO_HZ 459#ifdef CONFIG_NO_HZ
460 u64 nohz_stamp;
496 unsigned char in_nohz_recently; 461 unsigned char in_nohz_recently;
497#endif 462#endif
463 unsigned int skip_clock_update;
464
498 /* capture load from *all* tasks on this cpu: */ 465 /* capture load from *all* tasks on this cpu: */
499 struct load_weight load; 466 struct load_weight load;
500 unsigned long nr_load_updates; 467 unsigned long nr_load_updates;
@@ -531,20 +498,20 @@ struct rq {
531 struct root_domain *rd; 498 struct root_domain *rd;
532 struct sched_domain *sd; 499 struct sched_domain *sd;
533 500
501 unsigned long cpu_power;
502
534 unsigned char idle_at_tick; 503 unsigned char idle_at_tick;
535 /* For active balancing */ 504 /* For active balancing */
536 int post_schedule; 505 int post_schedule;
537 int active_balance; 506 int active_balance;
538 int push_cpu; 507 int push_cpu;
508 struct cpu_stop_work active_balance_work;
539 /* cpu of this runqueue: */ 509 /* cpu of this runqueue: */
540 int cpu; 510 int cpu;
541 int online; 511 int online;
542 512
543 unsigned long avg_load_per_task; 513 unsigned long avg_load_per_task;
544 514
545 struct task_struct *migration_thread;
546 struct list_head migration_queue;
547
548 u64 rt_avg; 515 u64 rt_avg;
549 u64 age_stamp; 516 u64 age_stamp;
550 u64 idle_stamp; 517 u64 idle_stamp;
@@ -592,6 +559,13 @@ static inline
592void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags) 559void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags)
593{ 560{
594 rq->curr->sched_class->check_preempt_curr(rq, p, flags); 561 rq->curr->sched_class->check_preempt_curr(rq, p, flags);
562
563 /*
564 * A queue event has occurred, and we're going to schedule. In
565 * this case, we can save a useless back to back clock update.
566 */
567 if (test_tsk_need_resched(p))
568 rq->skip_clock_update = 1;
595} 569}
596 570
597static inline int cpu_of(struct rq *rq) 571static inline int cpu_of(struct rq *rq)
@@ -624,9 +598,53 @@ static inline int cpu_of(struct rq *rq)
624#define cpu_curr(cpu) (cpu_rq(cpu)->curr) 598#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
625#define raw_rq() (&__raw_get_cpu_var(runqueues)) 599#define raw_rq() (&__raw_get_cpu_var(runqueues))
626 600
601#ifdef CONFIG_CGROUP_SCHED
602
603/*
604 * Return the group to which this tasks belongs.
605 *
606 * We use task_subsys_state_check() and extend the RCU verification
607 * with lockdep_is_held(&task_rq(p)->lock) because cpu_cgroup_attach()
608 * holds that lock for each task it moves into the cgroup. Therefore
609 * by holding that lock, we pin the task to the current cgroup.
610 */
611static inline struct task_group *task_group(struct task_struct *p)
612{
613 struct cgroup_subsys_state *css;
614
615 css = task_subsys_state_check(p, cpu_cgroup_subsys_id,
616 lockdep_is_held(&task_rq(p)->lock));
617 return container_of(css, struct task_group, css);
618}
619
620/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
621static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
622{
623#ifdef CONFIG_FAIR_GROUP_SCHED
624 p->se.cfs_rq = task_group(p)->cfs_rq[cpu];
625 p->se.parent = task_group(p)->se[cpu];
626#endif
627
628#ifdef CONFIG_RT_GROUP_SCHED
629 p->rt.rt_rq = task_group(p)->rt_rq[cpu];
630 p->rt.parent = task_group(p)->rt_se[cpu];
631#endif
632}
633
634#else /* CONFIG_CGROUP_SCHED */
635
636static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
637static inline struct task_group *task_group(struct task_struct *p)
638{
639 return NULL;
640}
641
642#endif /* CONFIG_CGROUP_SCHED */
643
627inline void update_rq_clock(struct rq *rq) 644inline void update_rq_clock(struct rq *rq)
628{ 645{
629 rq->clock = sched_clock_cpu(cpu_of(rq)); 646 if (!rq->skip_clock_update)
647 rq->clock = sched_clock_cpu(cpu_of(rq));
630} 648}
631 649
632/* 650/*
@@ -904,16 +922,12 @@ static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
904#endif /* __ARCH_WANT_UNLOCKED_CTXSW */ 922#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
905 923
906/* 924/*
907 * Check whether the task is waking, we use this to synchronize against 925 * Check whether the task is waking, we use this to synchronize ->cpus_allowed
908 * ttwu() so that task_cpu() reports a stable number. 926 * 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 */ 927 */
914static inline int task_is_waking(struct task_struct *p) 928static inline int task_is_waking(struct task_struct *p)
915{ 929{
916 return unlikely((p->state == TASK_WAKING) && !(p->flags & PF_STARTING)); 930 return unlikely(p->state == TASK_WAKING);
917} 931}
918 932
919/* 933/*
@@ -926,11 +940,9 @@ static inline struct rq *__task_rq_lock(struct task_struct *p)
926 struct rq *rq; 940 struct rq *rq;
927 941
928 for (;;) { 942 for (;;) {
929 while (task_is_waking(p))
930 cpu_relax();
931 rq = task_rq(p); 943 rq = task_rq(p);
932 raw_spin_lock(&rq->lock); 944 raw_spin_lock(&rq->lock);
933 if (likely(rq == task_rq(p) && !task_is_waking(p))) 945 if (likely(rq == task_rq(p)))
934 return rq; 946 return rq;
935 raw_spin_unlock(&rq->lock); 947 raw_spin_unlock(&rq->lock);
936 } 948 }
@@ -947,25 +959,15 @@ static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
947 struct rq *rq; 959 struct rq *rq;
948 960
949 for (;;) { 961 for (;;) {
950 while (task_is_waking(p))
951 cpu_relax();
952 local_irq_save(*flags); 962 local_irq_save(*flags);
953 rq = task_rq(p); 963 rq = task_rq(p);
954 raw_spin_lock(&rq->lock); 964 raw_spin_lock(&rq->lock);
955 if (likely(rq == task_rq(p) && !task_is_waking(p))) 965 if (likely(rq == task_rq(p)))
956 return rq; 966 return rq;
957 raw_spin_unlock_irqrestore(&rq->lock, *flags); 967 raw_spin_unlock_irqrestore(&rq->lock, *flags);
958 } 968 }
959} 969}
960 970
961void task_rq_unlock_wait(struct task_struct *p)
962{
963 struct rq *rq = task_rq(p);
964
965 smp_mb(); /* spin-unlock-wait is not a full memory barrier */
966 raw_spin_unlock_wait(&rq->lock);
967}
968
969static void __task_rq_unlock(struct rq *rq) 971static void __task_rq_unlock(struct rq *rq)
970 __releases(rq->lock) 972 __releases(rq->lock)
971{ 973{
@@ -1229,6 +1231,17 @@ void wake_up_idle_cpu(int cpu)
1229 if (!tsk_is_polling(rq->idle)) 1231 if (!tsk_is_polling(rq->idle))
1230 smp_send_reschedule(cpu); 1232 smp_send_reschedule(cpu);
1231} 1233}
1234
1235int nohz_ratelimit(int cpu)
1236{
1237 struct rq *rq = cpu_rq(cpu);
1238 u64 diff = rq->clock - rq->nohz_stamp;
1239
1240 rq->nohz_stamp = rq->clock;
1241
1242 return diff < (NSEC_PER_SEC / HZ) >> 1;
1243}
1244
1232#endif /* CONFIG_NO_HZ */ 1245#endif /* CONFIG_NO_HZ */
1233 1246
1234static u64 sched_avg_period(void) 1247static u64 sched_avg_period(void)
@@ -1241,6 +1254,12 @@ static void sched_avg_update(struct rq *rq)
1241 s64 period = sched_avg_period(); 1254 s64 period = sched_avg_period();
1242 1255
1243 while ((s64)(rq->clock - rq->age_stamp) > period) { 1256 while ((s64)(rq->clock - rq->age_stamp) > period) {
1257 /*
1258 * Inline assembly required to prevent the compiler
1259 * optimising this loop into a divmod call.
1260 * See __iter_div_u64_rem() for another example of this.
1261 */
1262 asm("" : "+rm" (rq->age_stamp));
1244 rq->age_stamp += period; 1263 rq->age_stamp += period;
1245 rq->rt_avg /= 2; 1264 rq->rt_avg /= 2;
1246 } 1265 }
@@ -1485,24 +1504,9 @@ static unsigned long target_load(int cpu, int type)
1485 return max(rq->cpu_load[type-1], total); 1504 return max(rq->cpu_load[type-1], total);
1486} 1505}
1487 1506
1488static struct sched_group *group_of(int cpu)
1489{
1490 struct sched_domain *sd = rcu_dereference_sched(cpu_rq(cpu)->sd);
1491
1492 if (!sd)
1493 return NULL;
1494
1495 return sd->groups;
1496}
1497
1498static unsigned long power_of(int cpu) 1507static unsigned long power_of(int cpu)
1499{ 1508{
1500 struct sched_group *group = group_of(cpu); 1509 return cpu_rq(cpu)->cpu_power;
1501
1502 if (!group)
1503 return SCHED_LOAD_SCALE;
1504
1505 return group->cpu_power;
1506} 1510}
1507 1511
1508static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd); 1512static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
@@ -1659,9 +1663,6 @@ static void update_shares(struct sched_domain *sd)
1659 1663
1660static void update_h_load(long cpu) 1664static void update_h_load(long cpu)
1661{ 1665{
1662 if (root_task_group_empty())
1663 return;
1664
1665 walk_tg_tree(tg_load_down, tg_nop, (void *)cpu); 1666 walk_tg_tree(tg_load_down, tg_nop, (void *)cpu);
1666} 1667}
1667 1668
@@ -1771,8 +1772,6 @@ static void double_rq_lock(struct rq *rq1, struct rq *rq2)
1771 raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING); 1772 raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
1772 } 1773 }
1773 } 1774 }
1774 update_rq_clock(rq1);
1775 update_rq_clock(rq2);
1776} 1775}
1777 1776
1778/* 1777/*
@@ -1803,7 +1802,7 @@ static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1803} 1802}
1804#endif 1803#endif
1805 1804
1806static void calc_load_account_active(struct rq *this_rq); 1805static void calc_load_account_idle(struct rq *this_rq);
1807static void update_sysctl(void); 1806static void update_sysctl(void);
1808static int get_update_sysctl_factor(void); 1807static int get_update_sysctl_factor(void);
1809 1808
@@ -1842,8 +1841,8 @@ static void dec_nr_running(struct rq *rq)
1842static void set_load_weight(struct task_struct *p) 1841static void set_load_weight(struct task_struct *p)
1843{ 1842{
1844 if (task_has_rt_policy(p)) { 1843 if (task_has_rt_policy(p)) {
1845 p->se.load.weight = prio_to_weight[0] * 2; 1844 p->se.load.weight = 0;
1846 p->se.load.inv_weight = prio_to_wmult[0] >> 1; 1845 p->se.load.inv_weight = WMULT_CONST;
1847 return; 1846 return;
1848 } 1847 }
1849 1848
@@ -1860,62 +1859,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]; 1859 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
1861} 1860}
1862 1861
1863static void update_avg(u64 *avg, u64 sample) 1862static void enqueue_task(struct rq *rq, struct task_struct *p, int flags)
1864{
1865 s64 diff = sample - *avg;
1866 *avg += diff >> 3;
1867}
1868
1869static void
1870enqueue_task(struct rq *rq, struct task_struct *p, int wakeup, bool head)
1871{ 1863{
1872 if (wakeup) 1864 update_rq_clock(rq);
1873 p->se.start_runtime = p->se.sum_exec_runtime;
1874
1875 sched_info_queued(p); 1865 sched_info_queued(p);
1876 p->sched_class->enqueue_task(rq, p, wakeup, head); 1866 p->sched_class->enqueue_task(rq, p, flags);
1877 p->se.on_rq = 1; 1867 p->se.on_rq = 1;
1878} 1868}
1879 1869
1880static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep) 1870static void dequeue_task(struct rq *rq, struct task_struct *p, int flags)
1881{ 1871{
1882 if (sleep) { 1872 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); 1873 sched_info_dequeued(p);
1894 p->sched_class->dequeue_task(rq, p, sleep); 1874 p->sched_class->dequeue_task(rq, p, flags);
1895 p->se.on_rq = 0; 1875 p->se.on_rq = 0;
1896} 1876}
1897 1877
1898/* 1878/*
1899 * activate_task - move a task to the runqueue. 1879 * activate_task - move a task to the runqueue.
1900 */ 1880 */
1901static void activate_task(struct rq *rq, struct task_struct *p, int wakeup) 1881static void activate_task(struct rq *rq, struct task_struct *p, int flags)
1902{ 1882{
1903 if (task_contributes_to_load(p)) 1883 if (task_contributes_to_load(p))
1904 rq->nr_uninterruptible--; 1884 rq->nr_uninterruptible--;
1905 1885
1906 enqueue_task(rq, p, wakeup, false); 1886 enqueue_task(rq, p, flags);
1907 inc_nr_running(rq); 1887 inc_nr_running(rq);
1908} 1888}
1909 1889
1910/* 1890/*
1911 * deactivate_task - remove a task from the runqueue. 1891 * deactivate_task - remove a task from the runqueue.
1912 */ 1892 */
1913static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep) 1893static void deactivate_task(struct rq *rq, struct task_struct *p, int flags)
1914{ 1894{
1915 if (task_contributes_to_load(p)) 1895 if (task_contributes_to_load(p))
1916 rq->nr_uninterruptible++; 1896 rq->nr_uninterruptible++;
1917 1897
1918 dequeue_task(rq, p, sleep); 1898 dequeue_task(rq, p, flags);
1919 dec_nr_running(rq); 1899 dec_nr_running(rq);
1920} 1900}
1921 1901
@@ -2044,21 +2024,18 @@ void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
2044 __set_task_cpu(p, new_cpu); 2024 __set_task_cpu(p, new_cpu);
2045} 2025}
2046 2026
2047struct migration_req { 2027struct migration_arg {
2048 struct list_head list;
2049
2050 struct task_struct *task; 2028 struct task_struct *task;
2051 int dest_cpu; 2029 int dest_cpu;
2052
2053 struct completion done;
2054}; 2030};
2055 2031
2032static int migration_cpu_stop(void *data);
2033
2056/* 2034/*
2057 * The task's runqueue lock must be held. 2035 * The task's runqueue lock must be held.
2058 * Returns true if you have to wait for migration thread. 2036 * Returns true if you have to wait for migration thread.
2059 */ 2037 */
2060static int 2038static bool migrate_task(struct task_struct *p, int dest_cpu)
2061migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
2062{ 2039{
2063 struct rq *rq = task_rq(p); 2040 struct rq *rq = task_rq(p);
2064 2041
@@ -2066,58 +2043,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 2043 * If the task is not on a runqueue (and not running), then
2067 * the next wake-up will properly place the task. 2044 * the next wake-up will properly place the task.
2068 */ 2045 */
2069 if (!p->se.on_rq && !task_running(rq, p)) 2046 return p->se.on_rq || task_running(rq, p);
2070 return 0;
2071
2072 init_completion(&req->done);
2073 req->task = p;
2074 req->dest_cpu = dest_cpu;
2075 list_add(&req->list, &rq->migration_queue);
2076
2077 return 1;
2078}
2079
2080/*
2081 * wait_task_context_switch - wait for a thread to complete at least one
2082 * context switch.
2083 *
2084 * @p must not be current.
2085 */
2086void wait_task_context_switch(struct task_struct *p)
2087{
2088 unsigned long nvcsw, nivcsw, flags;
2089 int running;
2090 struct rq *rq;
2091
2092 nvcsw = p->nvcsw;
2093 nivcsw = p->nivcsw;
2094 for (;;) {
2095 /*
2096 * The runqueue is assigned before the actual context
2097 * switch. We need to take the runqueue lock.
2098 *
2099 * We could check initially without the lock but it is
2100 * very likely that we need to take the lock in every
2101 * iteration.
2102 */
2103 rq = task_rq_lock(p, &flags);
2104 running = task_running(rq, p);
2105 task_rq_unlock(rq, &flags);
2106
2107 if (likely(!running))
2108 break;
2109 /*
2110 * The switch count is incremented before the actual
2111 * context switch. We thus wait for two switches to be
2112 * sure at least one completed.
2113 */
2114 if ((p->nvcsw - nvcsw) > 1)
2115 break;
2116 if ((p->nivcsw - nivcsw) > 1)
2117 break;
2118
2119 cpu_relax();
2120 }
2121} 2047}
2122 2048
2123/* 2049/*
@@ -2175,7 +2101,7 @@ unsigned long wait_task_inactive(struct task_struct *p, long match_state)
2175 * just go back and repeat. 2101 * just go back and repeat.
2176 */ 2102 */
2177 rq = task_rq_lock(p, &flags); 2103 rq = task_rq_lock(p, &flags);
2178 trace_sched_wait_task(rq, p); 2104 trace_sched_wait_task(p);
2179 running = task_running(rq, p); 2105 running = task_running(rq, p);
2180 on_rq = p->se.on_rq; 2106 on_rq = p->se.on_rq;
2181 ncsw = 0; 2107 ncsw = 0;
@@ -2273,6 +2199,9 @@ void task_oncpu_function_call(struct task_struct *p,
2273} 2199}
2274 2200
2275#ifdef CONFIG_SMP 2201#ifdef CONFIG_SMP
2202/*
2203 * ->cpus_allowed is protected by either TASK_WAKING or rq->lock held.
2204 */
2276static int select_fallback_rq(int cpu, struct task_struct *p) 2205static int select_fallback_rq(int cpu, struct task_struct *p)
2277{ 2206{
2278 int dest_cpu; 2207 int dest_cpu;
@@ -2289,12 +2218,8 @@ static int select_fallback_rq(int cpu, struct task_struct *p)
2289 return dest_cpu; 2218 return dest_cpu;
2290 2219
2291 /* No more Mr. Nice Guy. */ 2220 /* No more Mr. Nice Guy. */
2292 if (dest_cpu >= nr_cpu_ids) { 2221 if (unlikely(dest_cpu >= nr_cpu_ids)) {
2293 rcu_read_lock(); 2222 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 /* 2223 /*
2299 * Don't tell them about moving exiting tasks or 2224 * Don't tell them about moving exiting tasks or
2300 * kernel threads (both mm NULL), since they never 2225 * kernel threads (both mm NULL), since they never
@@ -2311,17 +2236,12 @@ static int select_fallback_rq(int cpu, struct task_struct *p)
2311} 2236}
2312 2237
2313/* 2238/*
2314 * Gets called from 3 sites (exec, fork, wakeup), since it is called without 2239 * 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 */ 2240 */
2321static inline 2241static inline
2322int select_task_rq(struct task_struct *p, int sd_flags, int wake_flags) 2242int select_task_rq(struct rq *rq, struct task_struct *p, int sd_flags, int wake_flags)
2323{ 2243{
2324 int cpu = p->sched_class->select_task_rq(p, sd_flags, wake_flags); 2244 int cpu = p->sched_class->select_task_rq(rq, p, sd_flags, wake_flags);
2325 2245
2326 /* 2246 /*
2327 * In order not to call set_task_cpu() on a blocking task we need 2247 * In order not to call set_task_cpu() on a blocking task we need
@@ -2339,6 +2259,12 @@ int select_task_rq(struct task_struct *p, int sd_flags, int wake_flags)
2339 2259
2340 return cpu; 2260 return cpu;
2341} 2261}
2262
2263static void update_avg(u64 *avg, u64 sample)
2264{
2265 s64 diff = sample - *avg;
2266 *avg += diff >> 3;
2267}
2342#endif 2268#endif
2343 2269
2344/*** 2270/***
@@ -2360,16 +2286,13 @@ static int try_to_wake_up(struct task_struct *p, unsigned int state,
2360{ 2286{
2361 int cpu, orig_cpu, this_cpu, success = 0; 2287 int cpu, orig_cpu, this_cpu, success = 0;
2362 unsigned long flags; 2288 unsigned long flags;
2289 unsigned long en_flags = ENQUEUE_WAKEUP;
2363 struct rq *rq; 2290 struct rq *rq;
2364 2291
2365 if (!sched_feat(SYNC_WAKEUPS))
2366 wake_flags &= ~WF_SYNC;
2367
2368 this_cpu = get_cpu(); 2292 this_cpu = get_cpu();
2369 2293
2370 smp_wmb(); 2294 smp_wmb();
2371 rq = task_rq_lock(p, &flags); 2295 rq = task_rq_lock(p, &flags);
2372 update_rq_clock(rq);
2373 if (!(p->state & state)) 2296 if (!(p->state & state))
2374 goto out; 2297 goto out;
2375 2298
@@ -2389,28 +2312,26 @@ static int try_to_wake_up(struct task_struct *p, unsigned int state,
2389 * 2312 *
2390 * First fix up the nr_uninterruptible count: 2313 * First fix up the nr_uninterruptible count:
2391 */ 2314 */
2392 if (task_contributes_to_load(p)) 2315 if (task_contributes_to_load(p)) {
2393 rq->nr_uninterruptible--; 2316 if (likely(cpu_online(orig_cpu)))
2317 rq->nr_uninterruptible--;
2318 else
2319 this_rq()->nr_uninterruptible--;
2320 }
2394 p->state = TASK_WAKING; 2321 p->state = TASK_WAKING;
2395 2322
2396 if (p->sched_class->task_waking) 2323 if (p->sched_class->task_waking) {
2397 p->sched_class->task_waking(rq, p); 2324 p->sched_class->task_waking(rq, p);
2325 en_flags |= ENQUEUE_WAKING;
2326 }
2398 2327
2399 __task_rq_unlock(rq); 2328 cpu = select_task_rq(rq, p, SD_BALANCE_WAKE, wake_flags);
2400 2329 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); 2330 set_task_cpu(p, cpu);
2409 } 2331 __task_rq_unlock(rq);
2410 2332
2411 rq = cpu_rq(cpu); 2333 rq = cpu_rq(cpu);
2412 raw_spin_lock(&rq->lock); 2334 raw_spin_lock(&rq->lock);
2413 update_rq_clock(rq);
2414 2335
2415 /* 2336 /*
2416 * We migrated the task without holding either rq->lock, however 2337 * We migrated the task without holding either rq->lock, however
@@ -2438,36 +2359,20 @@ static int try_to_wake_up(struct task_struct *p, unsigned int state,
2438 2359
2439out_activate: 2360out_activate:
2440#endif /* CONFIG_SMP */ 2361#endif /* CONFIG_SMP */
2441 schedstat_inc(p, se.nr_wakeups); 2362 schedstat_inc(p, se.statistics.nr_wakeups);
2442 if (wake_flags & WF_SYNC) 2363 if (wake_flags & WF_SYNC)
2443 schedstat_inc(p, se.nr_wakeups_sync); 2364 schedstat_inc(p, se.statistics.nr_wakeups_sync);
2444 if (orig_cpu != cpu) 2365 if (orig_cpu != cpu)
2445 schedstat_inc(p, se.nr_wakeups_migrate); 2366 schedstat_inc(p, se.statistics.nr_wakeups_migrate);
2446 if (cpu == this_cpu) 2367 if (cpu == this_cpu)
2447 schedstat_inc(p, se.nr_wakeups_local); 2368 schedstat_inc(p, se.statistics.nr_wakeups_local);
2448 else 2369 else
2449 schedstat_inc(p, se.nr_wakeups_remote); 2370 schedstat_inc(p, se.statistics.nr_wakeups_remote);
2450 activate_task(rq, p, 1); 2371 activate_task(rq, p, en_flags);
2451 success = 1; 2372 success = 1;
2452 2373
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: 2374out_running:
2470 trace_sched_wakeup(rq, p, success); 2375 trace_sched_wakeup(p, success);
2471 check_preempt_curr(rq, p, wake_flags); 2376 check_preempt_curr(rq, p, wake_flags);
2472 2377
2473 p->state = TASK_RUNNING; 2378 p->state = TASK_RUNNING;
@@ -2527,42 +2432,9 @@ static void __sched_fork(struct task_struct *p)
2527 p->se.sum_exec_runtime = 0; 2432 p->se.sum_exec_runtime = 0;
2528 p->se.prev_sum_exec_runtime = 0; 2433 p->se.prev_sum_exec_runtime = 0;
2529 p->se.nr_migrations = 0; 2434 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 2435
2535#ifdef CONFIG_SCHEDSTATS 2436#ifdef CONFIG_SCHEDSTATS
2536 p->se.wait_start = 0; 2437 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 2438#endif
2567 2439
2568 INIT_LIST_HEAD(&p->rt.run_list); 2440 INIT_LIST_HEAD(&p->rt.run_list);
@@ -2583,11 +2455,11 @@ void sched_fork(struct task_struct *p, int clone_flags)
2583 2455
2584 __sched_fork(p); 2456 __sched_fork(p);
2585 /* 2457 /*
2586 * We mark the process as waking here. This guarantees that 2458 * We mark the process as running here. This guarantees that
2587 * nobody will actually run it, and a signal or other external 2459 * nobody will actually run it, and a signal or other external
2588 * event cannot wake it up and insert it on the runqueue either. 2460 * event cannot wake it up and insert it on the runqueue either.
2589 */ 2461 */
2590 p->state = TASK_WAKING; 2462 p->state = TASK_RUNNING;
2591 2463
2592 /* 2464 /*
2593 * Revert to default priority/policy on fork if requested. 2465 * Revert to default priority/policy on fork if requested.
@@ -2622,7 +2494,16 @@ void sched_fork(struct task_struct *p, int clone_flags)
2622 if (p->sched_class->task_fork) 2494 if (p->sched_class->task_fork)
2623 p->sched_class->task_fork(p); 2495 p->sched_class->task_fork(p);
2624 2496
2497 /*
2498 * The child is not yet in the pid-hash so no cgroup attach races,
2499 * and the cgroup is pinned to this child due to cgroup_fork()
2500 * is ran before sched_fork().
2501 *
2502 * Silence PROVE_RCU.
2503 */
2504 rcu_read_lock();
2625 set_task_cpu(p, cpu); 2505 set_task_cpu(p, cpu);
2506 rcu_read_unlock();
2626 2507
2627#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT) 2508#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
2628 if (likely(sched_info_on())) 2509 if (likely(sched_info_on()))
@@ -2654,31 +2535,27 @@ void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
2654 int cpu __maybe_unused = get_cpu(); 2535 int cpu __maybe_unused = get_cpu();
2655 2536
2656#ifdef CONFIG_SMP 2537#ifdef CONFIG_SMP
2538 rq = task_rq_lock(p, &flags);
2539 p->state = TASK_WAKING;
2540
2657 /* 2541 /*
2658 * Fork balancing, do it here and not earlier because: 2542 * Fork balancing, do it here and not earlier because:
2659 * - cpus_allowed can change in the fork path 2543 * - cpus_allowed can change in the fork path
2660 * - any previously selected cpu might disappear through hotplug 2544 * - any previously selected cpu might disappear through hotplug
2661 * 2545 *
2662 * We still have TASK_WAKING but PF_STARTING is gone now, meaning 2546 * We set TASK_WAKING so that select_task_rq() can drop rq->lock
2663 * ->cpus_allowed is stable, we have preemption disabled, meaning 2547 * without people poking at ->cpus_allowed.
2664 * cpu_online_mask is stable.
2665 */ 2548 */
2666 cpu = select_task_rq(p, SD_BALANCE_FORK, 0); 2549 cpu = select_task_rq(rq, p, SD_BALANCE_FORK, 0);
2667 set_task_cpu(p, cpu); 2550 set_task_cpu(p, cpu);
2668#endif
2669
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 2551
2677 BUG_ON(p->state != TASK_WAKING);
2678 p->state = TASK_RUNNING; 2552 p->state = TASK_RUNNING;
2679 update_rq_clock(rq); 2553 task_rq_unlock(rq, &flags);
2554#endif
2555
2556 rq = task_rq_lock(p, &flags);
2680 activate_task(rq, p, 0); 2557 activate_task(rq, p, 0);
2681 trace_sched_wakeup_new(rq, p, 1); 2558 trace_sched_wakeup_new(p, 1);
2682 check_preempt_curr(rq, p, WF_FORK); 2559 check_preempt_curr(rq, p, WF_FORK);
2683#ifdef CONFIG_SMP 2560#ifdef CONFIG_SMP
2684 if (p->sched_class->task_woken) 2561 if (p->sched_class->task_woken)
@@ -2898,7 +2775,7 @@ context_switch(struct rq *rq, struct task_struct *prev,
2898 struct mm_struct *mm, *oldmm; 2775 struct mm_struct *mm, *oldmm;
2899 2776
2900 prepare_task_switch(rq, prev, next); 2777 prepare_task_switch(rq, prev, next);
2901 trace_sched_switch(rq, prev, next); 2778 trace_sched_switch(prev, next);
2902 mm = next->mm; 2779 mm = next->mm;
2903 oldmm = prev->active_mm; 2780 oldmm = prev->active_mm;
2904 /* 2781 /*
@@ -2996,9 +2873,9 @@ unsigned long nr_iowait(void)
2996 return sum; 2873 return sum;
2997} 2874}
2998 2875
2999unsigned long nr_iowait_cpu(void) 2876unsigned long nr_iowait_cpu(int cpu)
3000{ 2877{
3001 struct rq *this = this_rq(); 2878 struct rq *this = cpu_rq(cpu);
3002 return atomic_read(&this->nr_iowait); 2879 return atomic_read(&this->nr_iowait);
3003} 2880}
3004 2881
@@ -3015,6 +2892,61 @@ static unsigned long calc_load_update;
3015unsigned long avenrun[3]; 2892unsigned long avenrun[3];
3016EXPORT_SYMBOL(avenrun); 2893EXPORT_SYMBOL(avenrun);
3017 2894
2895static long calc_load_fold_active(struct rq *this_rq)
2896{
2897 long nr_active, delta = 0;
2898
2899 nr_active = this_rq->nr_running;
2900 nr_active += (long) this_rq->nr_uninterruptible;
2901
2902 if (nr_active != this_rq->calc_load_active) {
2903 delta = nr_active - this_rq->calc_load_active;
2904 this_rq->calc_load_active = nr_active;
2905 }
2906
2907 return delta;
2908}
2909
2910#ifdef CONFIG_NO_HZ
2911/*
2912 * For NO_HZ we delay the active fold to the next LOAD_FREQ update.
2913 *
2914 * When making the ILB scale, we should try to pull this in as well.
2915 */
2916static atomic_long_t calc_load_tasks_idle;
2917
2918static void calc_load_account_idle(struct rq *this_rq)
2919{
2920 long delta;
2921
2922 delta = calc_load_fold_active(this_rq);
2923 if (delta)
2924 atomic_long_add(delta, &calc_load_tasks_idle);
2925}
2926
2927static long calc_load_fold_idle(void)
2928{
2929 long delta = 0;
2930
2931 /*
2932 * Its got a race, we don't care...
2933 */
2934 if (atomic_long_read(&calc_load_tasks_idle))
2935 delta = atomic_long_xchg(&calc_load_tasks_idle, 0);
2936
2937 return delta;
2938}
2939#else
2940static void calc_load_account_idle(struct rq *this_rq)
2941{
2942}
2943
2944static inline long calc_load_fold_idle(void)
2945{
2946 return 0;
2947}
2948#endif
2949
3018/** 2950/**
3019 * get_avenrun - get the load average array 2951 * get_avenrun - get the load average array
3020 * @loads: pointer to dest load array 2952 * @loads: pointer to dest load array
@@ -3061,20 +2993,22 @@ void calc_global_load(void)
3061} 2993}
3062 2994
3063/* 2995/*
3064 * Either called from update_cpu_load() or from a cpu going idle 2996 * Called from update_cpu_load() to periodically update this CPU's
2997 * active count.
3065 */ 2998 */
3066static void calc_load_account_active(struct rq *this_rq) 2999static void calc_load_account_active(struct rq *this_rq)
3067{ 3000{
3068 long nr_active, delta; 3001 long delta;
3069 3002
3070 nr_active = this_rq->nr_running; 3003 if (time_before(jiffies, this_rq->calc_load_update))
3071 nr_active += (long) this_rq->nr_uninterruptible; 3004 return;
3072 3005
3073 if (nr_active != this_rq->calc_load_active) { 3006 delta = calc_load_fold_active(this_rq);
3074 delta = nr_active - this_rq->calc_load_active; 3007 delta += calc_load_fold_idle();
3075 this_rq->calc_load_active = nr_active; 3008 if (delta)
3076 atomic_long_add(delta, &calc_load_tasks); 3009 atomic_long_add(delta, &calc_load_tasks);
3077 } 3010
3011 this_rq->calc_load_update += LOAD_FREQ;
3078} 3012}
3079 3013
3080/* 3014/*
@@ -3106,10 +3040,7 @@ static void update_cpu_load(struct rq *this_rq)
3106 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i; 3040 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
3107 } 3041 }
3108 3042
3109 if (time_after_eq(jiffies, this_rq->calc_load_update)) { 3043 calc_load_account_active(this_rq);
3110 this_rq->calc_load_update += LOAD_FREQ;
3111 calc_load_account_active(this_rq);
3112 }
3113} 3044}
3114 3045
3115#ifdef CONFIG_SMP 3046#ifdef CONFIG_SMP
@@ -3121,44 +3052,27 @@ static void update_cpu_load(struct rq *this_rq)
3121void sched_exec(void) 3052void sched_exec(void)
3122{ 3053{
3123 struct task_struct *p = current; 3054 struct task_struct *p = current;
3124 struct migration_req req;
3125 int dest_cpu, this_cpu;
3126 unsigned long flags; 3055 unsigned long flags;
3127 struct rq *rq; 3056 struct rq *rq;
3128 3057 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 3058
3137 rq = task_rq_lock(p, &flags); 3059 rq = task_rq_lock(p, &flags);
3138 put_cpu(); 3060 dest_cpu = p->sched_class->select_task_rq(rq, p, SD_BALANCE_EXEC, 0);
3061 if (dest_cpu == smp_processor_id())
3062 goto unlock;
3139 3063
3140 /* 3064 /*
3141 * select_task_rq() can race against ->cpus_allowed 3065 * select_task_rq() can race against ->cpus_allowed
3142 */ 3066 */
3143 if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed) 3067 if (cpumask_test_cpu(dest_cpu, &p->cpus_allowed) &&
3144 || unlikely(!cpu_active(dest_cpu))) { 3068 likely(cpu_active(dest_cpu)) && migrate_task(p, dest_cpu)) {
3145 task_rq_unlock(rq, &flags); 3069 struct migration_arg arg = { p, dest_cpu };
3146 goto again;
3147 }
3148
3149 /* force the process onto the specified CPU */
3150 if (migrate_task(p, dest_cpu, &req)) {
3151 /* Need to wait for migration thread (might exit: take ref). */
3152 struct task_struct *mt = rq->migration_thread;
3153 3070
3154 get_task_struct(mt);
3155 task_rq_unlock(rq, &flags); 3071 task_rq_unlock(rq, &flags);
3156 wake_up_process(mt); 3072 stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
3157 put_task_struct(mt);
3158 wait_for_completion(&req.done);
3159
3160 return; 3073 return;
3161 } 3074 }
3075unlock:
3162 task_rq_unlock(rq, &flags); 3076 task_rq_unlock(rq, &flags);
3163} 3077}
3164 3078
@@ -3630,23 +3544,9 @@ static inline void schedule_debug(struct task_struct *prev)
3630 3544
3631static void put_prev_task(struct rq *rq, struct task_struct *prev) 3545static void put_prev_task(struct rq *rq, struct task_struct *prev)
3632{ 3546{
3633 if (prev->state == TASK_RUNNING) { 3547 if (prev->se.on_rq)
3634 u64 runtime = prev->se.sum_exec_runtime; 3548 update_rq_clock(rq);
3635 3549 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); 3550 prev->sched_class->put_prev_task(rq, prev);
3651} 3551}
3652 3552
@@ -3696,7 +3596,7 @@ need_resched:
3696 preempt_disable(); 3596 preempt_disable();
3697 cpu = smp_processor_id(); 3597 cpu = smp_processor_id();
3698 rq = cpu_rq(cpu); 3598 rq = cpu_rq(cpu);
3699 rcu_sched_qs(cpu); 3599 rcu_note_context_switch(cpu);
3700 prev = rq->curr; 3600 prev = rq->curr;
3701 switch_count = &prev->nivcsw; 3601 switch_count = &prev->nivcsw;
3702 3602
@@ -3709,14 +3609,13 @@ need_resched_nonpreemptible:
3709 hrtick_clear(rq); 3609 hrtick_clear(rq);
3710 3610
3711 raw_spin_lock_irq(&rq->lock); 3611 raw_spin_lock_irq(&rq->lock);
3712 update_rq_clock(rq);
3713 clear_tsk_need_resched(prev); 3612 clear_tsk_need_resched(prev);
3714 3613
3715 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) { 3614 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
3716 if (unlikely(signal_pending_state(prev->state, prev))) 3615 if (unlikely(signal_pending_state(prev->state, prev)))
3717 prev->state = TASK_RUNNING; 3616 prev->state = TASK_RUNNING;
3718 else 3617 else
3719 deactivate_task(rq, prev, 1); 3618 deactivate_task(rq, prev, DEQUEUE_SLEEP);
3720 switch_count = &prev->nvcsw; 3619 switch_count = &prev->nvcsw;
3721 } 3620 }
3722 3621
@@ -3780,7 +3679,7 @@ int mutex_spin_on_owner(struct mutex *lock, struct thread_info *owner)
3780 * the mutex owner just released it and exited. 3679 * the mutex owner just released it and exited.
3781 */ 3680 */
3782 if (probe_kernel_address(&owner->cpu, cpu)) 3681 if (probe_kernel_address(&owner->cpu, cpu))
3783 goto out; 3682 return 0;
3784#else 3683#else
3785 cpu = owner->cpu; 3684 cpu = owner->cpu;
3786#endif 3685#endif
@@ -3790,14 +3689,14 @@ int mutex_spin_on_owner(struct mutex *lock, struct thread_info *owner)
3790 * the cpu field may no longer be valid. 3689 * the cpu field may no longer be valid.
3791 */ 3690 */
3792 if (cpu >= nr_cpumask_bits) 3691 if (cpu >= nr_cpumask_bits)
3793 goto out; 3692 return 0;
3794 3693
3795 /* 3694 /*
3796 * We need to validate that we can do a 3695 * We need to validate that we can do a
3797 * get_cpu() and that we have the percpu area. 3696 * get_cpu() and that we have the percpu area.
3798 */ 3697 */
3799 if (!cpu_online(cpu)) 3698 if (!cpu_online(cpu))
3800 goto out; 3699 return 0;
3801 3700
3802 rq = cpu_rq(cpu); 3701 rq = cpu_rq(cpu);
3803 3702
@@ -3816,7 +3715,7 @@ int mutex_spin_on_owner(struct mutex *lock, struct thread_info *owner)
3816 3715
3817 cpu_relax(); 3716 cpu_relax();
3818 } 3717 }
3819out: 3718
3820 return 1; 3719 return 1;
3821} 3720}
3822#endif 3721#endif
@@ -3940,6 +3839,7 @@ void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
3940{ 3839{
3941 __wake_up_common(q, mode, 1, 0, NULL); 3840 __wake_up_common(q, mode, 1, 0, NULL);
3942} 3841}
3842EXPORT_SYMBOL_GPL(__wake_up_locked);
3943 3843
3944void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key) 3844void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key)
3945{ 3845{
@@ -4039,8 +3939,7 @@ do_wait_for_common(struct completion *x, long timeout, int state)
4039 if (!x->done) { 3939 if (!x->done) {
4040 DECLARE_WAITQUEUE(wait, current); 3940 DECLARE_WAITQUEUE(wait, current);
4041 3941
4042 wait.flags |= WQ_FLAG_EXCLUSIVE; 3942 __add_wait_queue_tail_exclusive(&x->wait, &wait);
4043 __add_wait_queue_tail(&x->wait, &wait);
4044 do { 3943 do {
4045 if (signal_pending_state(state, current)) { 3944 if (signal_pending_state(state, current)) {
4046 timeout = -ERESTARTSYS; 3945 timeout = -ERESTARTSYS;
@@ -4151,6 +4050,23 @@ int __sched wait_for_completion_killable(struct completion *x)
4151EXPORT_SYMBOL(wait_for_completion_killable); 4050EXPORT_SYMBOL(wait_for_completion_killable);
4152 4051
4153/** 4052/**
4053 * wait_for_completion_killable_timeout: - waits for completion of a task (w/(to,killable))
4054 * @x: holds the state of this particular completion
4055 * @timeout: timeout value in jiffies
4056 *
4057 * This waits for either a completion of a specific task to be
4058 * signaled or for a specified timeout to expire. It can be
4059 * interrupted by a kill signal. The timeout is in jiffies.
4060 */
4061unsigned long __sched
4062wait_for_completion_killable_timeout(struct completion *x,
4063 unsigned long timeout)
4064{
4065 return wait_for_common(x, timeout, TASK_KILLABLE);
4066}
4067EXPORT_SYMBOL(wait_for_completion_killable_timeout);
4068
4069/**
4154 * try_wait_for_completion - try to decrement a completion without blocking 4070 * try_wait_for_completion - try to decrement a completion without blocking
4155 * @x: completion structure 4071 * @x: completion structure
4156 * 4072 *
@@ -4266,7 +4182,6 @@ void rt_mutex_setprio(struct task_struct *p, int prio)
4266 BUG_ON(prio < 0 || prio > MAX_PRIO); 4182 BUG_ON(prio < 0 || prio > MAX_PRIO);
4267 4183
4268 rq = task_rq_lock(p, &flags); 4184 rq = task_rq_lock(p, &flags);
4269 update_rq_clock(rq);
4270 4185
4271 oldprio = p->prio; 4186 oldprio = p->prio;
4272 prev_class = p->sched_class; 4187 prev_class = p->sched_class;
@@ -4287,7 +4202,7 @@ void rt_mutex_setprio(struct task_struct *p, int prio)
4287 if (running) 4202 if (running)
4288 p->sched_class->set_curr_task(rq); 4203 p->sched_class->set_curr_task(rq);
4289 if (on_rq) { 4204 if (on_rq) {
4290 enqueue_task(rq, p, 0, oldprio < prio); 4205 enqueue_task(rq, p, oldprio < prio ? ENQUEUE_HEAD : 0);
4291 4206
4292 check_class_changed(rq, p, prev_class, oldprio, running); 4207 check_class_changed(rq, p, prev_class, oldprio, running);
4293 } 4208 }
@@ -4309,7 +4224,6 @@ void set_user_nice(struct task_struct *p, long nice)
4309 * the task might be in the middle of scheduling on another CPU. 4224 * the task might be in the middle of scheduling on another CPU.
4310 */ 4225 */
4311 rq = task_rq_lock(p, &flags); 4226 rq = task_rq_lock(p, &flags);
4312 update_rq_clock(rq);
4313 /* 4227 /*
4314 * The RT priorities are set via sched_setscheduler(), but we still 4228 * The RT priorities are set via sched_setscheduler(), but we still
4315 * allow the 'normal' nice value to be set - but as expected 4229 * allow the 'normal' nice value to be set - but as expected
@@ -4331,7 +4245,7 @@ void set_user_nice(struct task_struct *p, long nice)
4331 delta = p->prio - old_prio; 4245 delta = p->prio - old_prio;
4332 4246
4333 if (on_rq) { 4247 if (on_rq) {
4334 enqueue_task(rq, p, 0, false); 4248 enqueue_task(rq, p, 0);
4335 /* 4249 /*
4336 * If the task increased its priority or is running and 4250 * If the task increased its priority or is running and
4337 * lowered its priority, then reschedule its CPU: 4251 * lowered its priority, then reschedule its CPU:
@@ -4560,16 +4474,6 @@ recheck:
4560 } 4474 }
4561 4475
4562 if (user) { 4476 if (user) {
4563#ifdef CONFIG_RT_GROUP_SCHED
4564 /*
4565 * Do not allow realtime tasks into groups that have no runtime
4566 * assigned.
4567 */
4568 if (rt_bandwidth_enabled() && rt_policy(policy) &&
4569 task_group(p)->rt_bandwidth.rt_runtime == 0)
4570 return -EPERM;
4571#endif
4572
4573 retval = security_task_setscheduler(p, policy, param); 4477 retval = security_task_setscheduler(p, policy, param);
4574 if (retval) 4478 if (retval)
4575 return retval; 4479 return retval;
@@ -4585,6 +4489,22 @@ recheck:
4585 * runqueue lock must be held. 4489 * runqueue lock must be held.
4586 */ 4490 */
4587 rq = __task_rq_lock(p); 4491 rq = __task_rq_lock(p);
4492
4493#ifdef CONFIG_RT_GROUP_SCHED
4494 if (user) {
4495 /*
4496 * Do not allow realtime tasks into groups that have no runtime
4497 * assigned.
4498 */
4499 if (rt_bandwidth_enabled() && rt_policy(policy) &&
4500 task_group(p)->rt_bandwidth.rt_runtime == 0) {
4501 __task_rq_unlock(rq);
4502 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
4503 return -EPERM;
4504 }
4505 }
4506#endif
4507
4588 /* recheck policy now with rq lock held */ 4508 /* recheck policy now with rq lock held */
4589 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) { 4509 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
4590 policy = oldpolicy = -1; 4510 policy = oldpolicy = -1;
@@ -4592,7 +4512,6 @@ recheck:
4592 raw_spin_unlock_irqrestore(&p->pi_lock, flags); 4512 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
4593 goto recheck; 4513 goto recheck;
4594 } 4514 }
4595 update_rq_clock(rq);
4596 on_rq = p->se.on_rq; 4515 on_rq = p->se.on_rq;
4597 running = task_current(rq, p); 4516 running = task_current(rq, p);
4598 if (on_rq) 4517 if (on_rq)
@@ -5329,17 +5248,15 @@ static inline void sched_init_granularity(void)
5329/* 5248/*
5330 * This is how migration works: 5249 * This is how migration works:
5331 * 5250 *
5332 * 1) we queue a struct migration_req structure in the source CPU's 5251 * 1) we invoke migration_cpu_stop() on the target CPU using
5333 * runqueue and wake up that CPU's migration thread. 5252 * stop_one_cpu().
5334 * 2) we down() the locked semaphore => thread blocks. 5253 * 2) stopper starts to run (implicitly forcing the migrated thread
5335 * 3) migration thread wakes up (implicitly it forces the migrated 5254 * off the CPU)
5336 * thread off the CPU) 5255 * 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 5256 * 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. 5257 * it and puts it into the right queue.
5341 * 6) migration thread up()s the semaphore. 5258 * 5) stopper completes and stop_one_cpu() returns and the migration
5342 * 7) we wake up and the migration is done. 5259 * is done.
5343 */ 5260 */
5344 5261
5345/* 5262/*
@@ -5353,12 +5270,23 @@ static inline void sched_init_granularity(void)
5353 */ 5270 */
5354int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask) 5271int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
5355{ 5272{
5356 struct migration_req req;
5357 unsigned long flags; 5273 unsigned long flags;
5358 struct rq *rq; 5274 struct rq *rq;
5275 unsigned int dest_cpu;
5359 int ret = 0; 5276 int ret = 0;
5360 5277
5278 /*
5279 * Serialize against TASK_WAKING so that ttwu() and wunt() can
5280 * drop the rq->lock and still rely on ->cpus_allowed.
5281 */
5282again:
5283 while (task_is_waking(p))
5284 cpu_relax();
5361 rq = task_rq_lock(p, &flags); 5285 rq = task_rq_lock(p, &flags);
5286 if (task_is_waking(p)) {
5287 task_rq_unlock(rq, &flags);
5288 goto again;
5289 }
5362 5290
5363 if (!cpumask_intersects(new_mask, cpu_active_mask)) { 5291 if (!cpumask_intersects(new_mask, cpu_active_mask)) {
5364 ret = -EINVAL; 5292 ret = -EINVAL;
@@ -5382,15 +5310,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)) 5310 if (cpumask_test_cpu(task_cpu(p), new_mask))
5383 goto out; 5311 goto out;
5384 5312
5385 if (migrate_task(p, cpumask_any_and(cpu_active_mask, new_mask), &req)) { 5313 dest_cpu = cpumask_any_and(cpu_active_mask, new_mask);
5314 if (migrate_task(p, dest_cpu)) {
5315 struct migration_arg arg = { p, dest_cpu };
5386 /* Need help from migration thread: drop lock and wait. */ 5316 /* 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); 5317 task_rq_unlock(rq, &flags);
5391 wake_up_process(mt); 5318 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); 5319 tlb_migrate_finish(p->mm);
5395 return 0; 5320 return 0;
5396 } 5321 }
@@ -5448,98 +5373,49 @@ fail:
5448 return ret; 5373 return ret;
5449} 5374}
5450 5375
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/* 5376/*
5457 * migration_thread - this is a highprio system thread that performs 5377 * migration_cpu_stop - this will be executed by a highprio stopper thread
5458 * thread migration by bumping thread off CPU then 'pushing' onto 5378 * and performs thread migration by bumping thread off CPU then
5459 * another runqueue. 5379 * 'pushing' onto another runqueue.
5460 */ 5380 */
5461static int migration_thread(void *data) 5381static int migration_cpu_stop(void *data)
5462{ 5382{
5463 int badcpu; 5383 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 5384
5385 /*
5386 * The original target cpu might have gone down and we might
5387 * be on another cpu but it doesn't matter.
5388 */
5524 local_irq_disable(); 5389 local_irq_disable();
5525 ret = __migrate_task(p, src_cpu, dest_cpu); 5390 __migrate_task(arg->task, raw_smp_processor_id(), arg->dest_cpu);
5526 local_irq_enable(); 5391 local_irq_enable();
5527 return ret; 5392 return 0;
5528} 5393}
5529 5394
5395#ifdef CONFIG_HOTPLUG_CPU
5530/* 5396/*
5531 * Figure out where task on dead CPU should go, use force if necessary. 5397 * Figure out where task on dead CPU should go, use force if necessary.
5532 */ 5398 */
5533static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p) 5399void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
5534{ 5400{
5535 int dest_cpu; 5401 struct rq *rq = cpu_rq(dead_cpu);
5402 int needs_cpu, uninitialized_var(dest_cpu);
5403 unsigned long flags;
5536 5404
5537again: 5405 local_irq_save(flags);
5538 dest_cpu = select_fallback_rq(dead_cpu, p);
5539 5406
5540 /* It can have affinity changed while we were choosing. */ 5407 raw_spin_lock(&rq->lock);
5541 if (unlikely(!__migrate_task_irq(p, dead_cpu, dest_cpu))) 5408 needs_cpu = (task_cpu(p) == dead_cpu) && (p->state != TASK_WAKING);
5542 goto again; 5409 if (needs_cpu)
5410 dest_cpu = select_fallback_rq(dead_cpu, p);
5411 raw_spin_unlock(&rq->lock);
5412 /*
5413 * It can only fail if we race with set_cpus_allowed(),
5414 * in the racer should migrate the task anyway.
5415 */
5416 if (needs_cpu)
5417 __migrate_task(p, dead_cpu, dest_cpu);
5418 local_irq_restore(flags);
5543} 5419}
5544 5420
5545/* 5421/*
@@ -5603,7 +5479,6 @@ void sched_idle_next(void)
5603 5479
5604 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1); 5480 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
5605 5481
5606 update_rq_clock(rq);
5607 activate_task(rq, p, 0); 5482 activate_task(rq, p, 0);
5608 5483
5609 raw_spin_unlock_irqrestore(&rq->lock, flags); 5484 raw_spin_unlock_irqrestore(&rq->lock, flags);
@@ -5658,7 +5533,6 @@ static void migrate_dead_tasks(unsigned int dead_cpu)
5658 for ( ; ; ) { 5533 for ( ; ; ) {
5659 if (!rq->nr_running) 5534 if (!rq->nr_running)
5660 break; 5535 break;
5661 update_rq_clock(rq);
5662 next = pick_next_task(rq); 5536 next = pick_next_task(rq);
5663 if (!next) 5537 if (!next)
5664 break; 5538 break;
@@ -5881,35 +5755,20 @@ static void set_rq_offline(struct rq *rq)
5881static int __cpuinit 5755static int __cpuinit
5882migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu) 5756migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
5883{ 5757{
5884 struct task_struct *p;
5885 int cpu = (long)hcpu; 5758 int cpu = (long)hcpu;
5886 unsigned long flags; 5759 unsigned long flags;
5887 struct rq *rq; 5760 struct rq *rq = cpu_rq(cpu);
5888 5761
5889 switch (action) { 5762 switch (action) {
5890 5763
5891 case CPU_UP_PREPARE: 5764 case CPU_UP_PREPARE:
5892 case CPU_UP_PREPARE_FROZEN: 5765 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; 5766 rq->calc_load_update = calc_load_update;
5904 break; 5767 break;
5905 5768
5906 case CPU_ONLINE: 5769 case CPU_ONLINE:
5907 case CPU_ONLINE_FROZEN: 5770 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 */ 5771 /* Update our root-domain */
5912 rq = cpu_rq(cpu);
5913 raw_spin_lock_irqsave(&rq->lock, flags); 5772 raw_spin_lock_irqsave(&rq->lock, flags);
5914 if (rq->rd) { 5773 if (rq->rd) {
5915 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span)); 5774 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
@@ -5920,61 +5779,24 @@ migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
5920 break; 5779 break;
5921 5780
5922#ifdef CONFIG_HOTPLUG_CPU 5781#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: 5782 case CPU_DEAD:
5936 case CPU_DEAD_FROZEN: 5783 case CPU_DEAD_FROZEN:
5937 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
5938 migrate_live_tasks(cpu); 5784 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) */ 5785 /* Idle task back to normal (off runqueue, low prio) */
5944 raw_spin_lock_irq(&rq->lock); 5786 raw_spin_lock_irq(&rq->lock);
5945 update_rq_clock(rq);
5946 deactivate_task(rq, rq->idle, 0); 5787 deactivate_task(rq, rq->idle, 0);
5947 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0); 5788 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
5948 rq->idle->sched_class = &idle_sched_class; 5789 rq->idle->sched_class = &idle_sched_class;
5949 migrate_dead_tasks(cpu); 5790 migrate_dead_tasks(cpu);
5950 raw_spin_unlock_irq(&rq->lock); 5791 raw_spin_unlock_irq(&rq->lock);
5951 cpuset_unlock();
5952 migrate_nr_uninterruptible(rq); 5792 migrate_nr_uninterruptible(rq);
5953 BUG_ON(rq->nr_running != 0); 5793 BUG_ON(rq->nr_running != 0);
5954 calc_global_load_remove(rq); 5794 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; 5795 break;
5973 5796
5974 case CPU_DYING: 5797 case CPU_DYING:
5975 case CPU_DYING_FROZEN: 5798 case CPU_DYING_FROZEN:
5976 /* Update our root-domain */ 5799 /* Update our root-domain */
5977 rq = cpu_rq(cpu);
5978 raw_spin_lock_irqsave(&rq->lock, flags); 5800 raw_spin_lock_irqsave(&rq->lock, flags);
5979 if (rq->rd) { 5801 if (rq->rd) {
5980 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span)); 5802 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
@@ -6305,6 +6127,9 @@ cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
6305 struct rq *rq = cpu_rq(cpu); 6127 struct rq *rq = cpu_rq(cpu);
6306 struct sched_domain *tmp; 6128 struct sched_domain *tmp;
6307 6129
6130 for (tmp = sd; tmp; tmp = tmp->parent)
6131 tmp->span_weight = cpumask_weight(sched_domain_span(tmp));
6132
6308 /* Remove the sched domains which do not contribute to scheduling. */ 6133 /* Remove the sched domains which do not contribute to scheduling. */
6309 for (tmp = sd; tmp; ) { 6134 for (tmp = sd; tmp; ) {
6310 struct sched_domain *parent = tmp->parent; 6135 struct sched_domain *parent = tmp->parent;
@@ -7782,16 +7607,15 @@ void __init sched_init(void)
7782#ifdef CONFIG_SMP 7607#ifdef CONFIG_SMP
7783 rq->sd = NULL; 7608 rq->sd = NULL;
7784 rq->rd = NULL; 7609 rq->rd = NULL;
7610 rq->cpu_power = SCHED_LOAD_SCALE;
7785 rq->post_schedule = 0; 7611 rq->post_schedule = 0;
7786 rq->active_balance = 0; 7612 rq->active_balance = 0;
7787 rq->next_balance = jiffies; 7613 rq->next_balance = jiffies;
7788 rq->push_cpu = 0; 7614 rq->push_cpu = 0;
7789 rq->cpu = i; 7615 rq->cpu = i;
7790 rq->online = 0; 7616 rq->online = 0;
7791 rq->migration_thread = NULL;
7792 rq->idle_stamp = 0; 7617 rq->idle_stamp = 0;
7793 rq->avg_idle = 2*sysctl_sched_migration_cost; 7618 rq->avg_idle = 2*sysctl_sched_migration_cost;
7794 INIT_LIST_HEAD(&rq->migration_queue);
7795 rq_attach_root(rq, &def_root_domain); 7619 rq_attach_root(rq, &def_root_domain);
7796#endif 7620#endif
7797 init_rq_hrtick(rq); 7621 init_rq_hrtick(rq);
@@ -7892,7 +7716,6 @@ static void normalize_task(struct rq *rq, struct task_struct *p)
7892{ 7716{
7893 int on_rq; 7717 int on_rq;
7894 7718
7895 update_rq_clock(rq);
7896 on_rq = p->se.on_rq; 7719 on_rq = p->se.on_rq;
7897 if (on_rq) 7720 if (on_rq)
7898 deactivate_task(rq, p, 0); 7721 deactivate_task(rq, p, 0);
@@ -7919,9 +7742,9 @@ void normalize_rt_tasks(void)
7919 7742
7920 p->se.exec_start = 0; 7743 p->se.exec_start = 0;
7921#ifdef CONFIG_SCHEDSTATS 7744#ifdef CONFIG_SCHEDSTATS
7922 p->se.wait_start = 0; 7745 p->se.statistics.wait_start = 0;
7923 p->se.sleep_start = 0; 7746 p->se.statistics.sleep_start = 0;
7924 p->se.block_start = 0; 7747 p->se.statistics.block_start = 0;
7925#endif 7748#endif
7926 7749
7927 if (!rt_task(p)) { 7750 if (!rt_task(p)) {
@@ -7948,9 +7771,9 @@ void normalize_rt_tasks(void)
7948 7771
7949#endif /* CONFIG_MAGIC_SYSRQ */ 7772#endif /* CONFIG_MAGIC_SYSRQ */
7950 7773
7951#ifdef CONFIG_IA64 7774#if defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB)
7952/* 7775/*
7953 * These functions are only useful for the IA64 MCA handling. 7776 * These functions are only useful for the IA64 MCA handling, or kdb.
7954 * 7777 *
7955 * They can only be called when the whole system has been 7778 * They can only be called when the whole system has been
7956 * stopped - every CPU needs to be quiescent, and no scheduling 7779 * stopped - every CPU needs to be quiescent, and no scheduling
@@ -7970,6 +7793,9 @@ struct task_struct *curr_task(int cpu)
7970 return cpu_curr(cpu); 7793 return cpu_curr(cpu);
7971} 7794}
7972 7795
7796#endif /* defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB) */
7797
7798#ifdef CONFIG_IA64
7973/** 7799/**
7974 * set_curr_task - set the current task for a given cpu. 7800 * set_curr_task - set the current task for a given cpu.
7975 * @cpu: the processor in question. 7801 * @cpu: the processor in question.
@@ -8254,8 +8080,6 @@ void sched_move_task(struct task_struct *tsk)
8254 8080
8255 rq = task_rq_lock(tsk, &flags); 8081 rq = task_rq_lock(tsk, &flags);
8256 8082
8257 update_rq_clock(rq);
8258
8259 running = task_current(rq, tsk); 8083 running = task_current(rq, tsk);
8260 on_rq = tsk->se.on_rq; 8084 on_rq = tsk->se.on_rq;
8261 8085
@@ -8274,7 +8098,7 @@ void sched_move_task(struct task_struct *tsk)
8274 if (unlikely(running)) 8098 if (unlikely(running))
8275 tsk->sched_class->set_curr_task(rq); 8099 tsk->sched_class->set_curr_task(rq);
8276 if (on_rq) 8100 if (on_rq)
8277 enqueue_task(rq, tsk, 0, false); 8101 enqueue_task(rq, tsk, 0);
8278 8102
8279 task_rq_unlock(rq, &flags); 8103 task_rq_unlock(rq, &flags);
8280} 8104}
@@ -9088,43 +8912,32 @@ struct cgroup_subsys cpuacct_subsys = {
9088 8912
9089#ifndef CONFIG_SMP 8913#ifndef CONFIG_SMP
9090 8914
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) 8915void synchronize_sched_expedited(void)
9098{ 8916{
8917 barrier();
9099} 8918}
9100EXPORT_SYMBOL_GPL(synchronize_sched_expedited); 8919EXPORT_SYMBOL_GPL(synchronize_sched_expedited);
9101 8920
9102#else /* #ifndef CONFIG_SMP */ 8921#else /* #ifndef CONFIG_SMP */
9103 8922
9104static DEFINE_PER_CPU(struct migration_req, rcu_migration_req); 8923static atomic_t synchronize_sched_expedited_count = ATOMIC_INIT(0);
9105static DEFINE_MUTEX(rcu_sched_expedited_mutex);
9106 8924
9107#define RCU_EXPEDITED_STATE_POST -2 8925static int synchronize_sched_expedited_cpu_stop(void *data)
9108#define RCU_EXPEDITED_STATE_IDLE -1
9109
9110static int rcu_expedited_state = RCU_EXPEDITED_STATE_IDLE;
9111
9112int rcu_expedited_torture_stats(char *page)
9113{ 8926{
9114 int cnt = 0; 8927 /*
9115 int cpu; 8928 * There must be a full memory barrier on each affected CPU
9116 8929 * between the time that try_stop_cpus() is called and the
9117 cnt += sprintf(&page[cnt], "state: %d /", rcu_expedited_state); 8930 * time that it returns.
9118 for_each_online_cpu(cpu) { 8931 *
9119 cnt += sprintf(&page[cnt], " %d:%d", 8932 * In the current initial implementation of cpu_stop, the
9120 cpu, per_cpu(rcu_migration_req, cpu).dest_cpu); 8933 * above condition is already met when the control reaches
9121 } 8934 * this point and the following smp_mb() is not strictly
9122 cnt += sprintf(&page[cnt], "\n"); 8935 * necessary. Do smp_mb() anyway for documentation and
9123 return cnt; 8936 * robustness against future implementation changes.
8937 */
8938 smp_mb(); /* See above comment block. */
8939 return 0;
9124} 8940}
9125EXPORT_SYMBOL_GPL(rcu_expedited_torture_stats);
9126
9127static long synchronize_sched_expedited_count;
9128 8941
9129/* 8942/*
9130 * Wait for an rcu-sched grace period to elapse, but use "big hammer" 8943 * Wait for an rcu-sched grace period to elapse, but use "big hammer"
@@ -9138,18 +8951,14 @@ static long synchronize_sched_expedited_count;
9138 */ 8951 */
9139void synchronize_sched_expedited(void) 8952void synchronize_sched_expedited(void)
9140{ 8953{
9141 int cpu; 8954 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 8955
9149 smp_mb(); /* ensure prior mod happens before capturing snap. */ 8956 smp_mb(); /* ensure prior mod happens before capturing snap. */
9150 snap = ACCESS_ONCE(synchronize_sched_expedited_count) + 1; 8957 snap = atomic_read(&synchronize_sched_expedited_count) + 1;
9151 get_online_cpus(); 8958 get_online_cpus();
9152 while (!mutex_trylock(&rcu_sched_expedited_mutex)) { 8959 while (try_stop_cpus(cpu_online_mask,
8960 synchronize_sched_expedited_cpu_stop,
8961 NULL) == -EAGAIN) {
9153 put_online_cpus(); 8962 put_online_cpus();
9154 if (trycount++ < 10) 8963 if (trycount++ < 10)
9155 udelay(trycount * num_online_cpus()); 8964 udelay(trycount * num_online_cpus());
@@ -9157,41 +8966,15 @@ void synchronize_sched_expedited(void)
9157 synchronize_sched(); 8966 synchronize_sched();
9158 return; 8967 return;
9159 } 8968 }
9160 if (ACCESS_ONCE(synchronize_sched_expedited_count) - snap > 0) { 8969 if (atomic_read(&synchronize_sched_expedited_count) - snap > 0) {
9161 smp_mb(); /* ensure test happens before caller kfree */ 8970 smp_mb(); /* ensure test happens before caller kfree */
9162 return; 8971 return;
9163 } 8972 }
9164 get_online_cpus(); 8973 get_online_cpus();
9165 } 8974 }
9166 rcu_expedited_state = RCU_EXPEDITED_STATE_POST; 8975 atomic_inc(&synchronize_sched_expedited_count);
9167 for_each_online_cpu(cpu) { 8976 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(); 8977 put_online_cpus();
9193 if (need_full_sync)
9194 synchronize_sched();
9195} 8978}
9196EXPORT_SYMBOL_GPL(synchronize_sched_expedited); 8979EXPORT_SYMBOL_GPL(synchronize_sched_expedited);
9197 8980