aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/sched.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/sched.c')
-rw-r--r--kernel/sched.c127
1 files changed, 67 insertions, 60 deletions
diff --git a/kernel/sched.c b/kernel/sched.c
index 2d12893b8b0f..3f2e502d609b 100644
--- a/kernel/sched.c
+++ b/kernel/sched.c
@@ -605,10 +605,10 @@ static inline int cpu_of(struct rq *rq)
605/* 605/*
606 * Return the group to which this tasks belongs. 606 * Return the group to which this tasks belongs.
607 * 607 *
608 * We use task_subsys_state_check() and extend the RCU verification 608 * We use task_subsys_state_check() and extend the RCU verification with
609 * with lockdep_is_held(&p->pi_lock) because cpu_cgroup_attach() 609 * pi->lock and rq->lock because cpu_cgroup_attach() holds those locks for each
610 * holds that lock for each task it moves into the cgroup. Therefore 610 * task it moves into the cgroup. Therefore by holding either of those locks,
611 * by holding that lock, we pin the task to the current cgroup. 611 * we pin the task to the current cgroup.
612 */ 612 */
613static inline struct task_group *task_group(struct task_struct *p) 613static inline struct task_group *task_group(struct task_struct *p)
614{ 614{
@@ -616,7 +616,8 @@ static inline struct task_group *task_group(struct task_struct *p)
616 struct cgroup_subsys_state *css; 616 struct cgroup_subsys_state *css;
617 617
618 css = task_subsys_state_check(p, cpu_cgroup_subsys_id, 618 css = task_subsys_state_check(p, cpu_cgroup_subsys_id,
619 lockdep_is_held(&p->pi_lock)); 619 lockdep_is_held(&p->pi_lock) ||
620 lockdep_is_held(&task_rq(p)->lock));
620 tg = container_of(css, struct task_group, css); 621 tg = container_of(css, struct task_group, css);
621 622
622 return autogroup_task_group(p, tg); 623 return autogroup_task_group(p, tg);
@@ -2200,6 +2201,16 @@ void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
2200 !(task_thread_info(p)->preempt_count & PREEMPT_ACTIVE)); 2201 !(task_thread_info(p)->preempt_count & PREEMPT_ACTIVE));
2201 2202
2202#ifdef CONFIG_LOCKDEP 2203#ifdef CONFIG_LOCKDEP
2204 /*
2205 * The caller should hold either p->pi_lock or rq->lock, when changing
2206 * a task's CPU. ->pi_lock for waking tasks, rq->lock for runnable tasks.
2207 *
2208 * sched_move_task() holds both and thus holding either pins the cgroup,
2209 * see set_task_rq().
2210 *
2211 * Furthermore, all task_rq users should acquire both locks, see
2212 * task_rq_lock().
2213 */
2203 WARN_ON_ONCE(debug_locks && !(lockdep_is_held(&p->pi_lock) || 2214 WARN_ON_ONCE(debug_locks && !(lockdep_is_held(&p->pi_lock) ||
2204 lockdep_is_held(&task_rq(p)->lock))); 2215 lockdep_is_held(&task_rq(p)->lock)));
2205#endif 2216#endif
@@ -2447,6 +2458,10 @@ ttwu_stat(struct task_struct *p, int cpu, int wake_flags)
2447 } 2458 }
2448 rcu_read_unlock(); 2459 rcu_read_unlock();
2449 } 2460 }
2461
2462 if (wake_flags & WF_MIGRATED)
2463 schedstat_inc(p, se.statistics.nr_wakeups_migrate);
2464
2450#endif /* CONFIG_SMP */ 2465#endif /* CONFIG_SMP */
2451 2466
2452 schedstat_inc(rq, ttwu_count); 2467 schedstat_inc(rq, ttwu_count);
@@ -2455,9 +2470,6 @@ ttwu_stat(struct task_struct *p, int cpu, int wake_flags)
2455 if (wake_flags & WF_SYNC) 2470 if (wake_flags & WF_SYNC)
2456 schedstat_inc(p, se.statistics.nr_wakeups_sync); 2471 schedstat_inc(p, se.statistics.nr_wakeups_sync);
2457 2472
2458 if (cpu != task_cpu(p))
2459 schedstat_inc(p, se.statistics.nr_wakeups_migrate);
2460
2461#endif /* CONFIG_SCHEDSTATS */ 2473#endif /* CONFIG_SCHEDSTATS */
2462} 2474}
2463 2475
@@ -2573,7 +2585,26 @@ static void ttwu_queue_remote(struct task_struct *p, int cpu)
2573 if (!next) 2585 if (!next)
2574 smp_send_reschedule(cpu); 2586 smp_send_reschedule(cpu);
2575} 2587}
2576#endif 2588
2589#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
2590static int ttwu_activate_remote(struct task_struct *p, int wake_flags)
2591{
2592 struct rq *rq;
2593 int ret = 0;
2594
2595 rq = __task_rq_lock(p);
2596 if (p->on_cpu) {
2597 ttwu_activate(rq, p, ENQUEUE_WAKEUP);
2598 ttwu_do_wakeup(rq, p, wake_flags);
2599 ret = 1;
2600 }
2601 __task_rq_unlock(rq);
2602
2603 return ret;
2604
2605}
2606#endif /* __ARCH_WANT_INTERRUPTS_ON_CTXSW */
2607#endif /* CONFIG_SMP */
2577 2608
2578static void ttwu_queue(struct task_struct *p, int cpu) 2609static void ttwu_queue(struct task_struct *p, int cpu)
2579{ 2610{
@@ -2581,6 +2612,7 @@ static void ttwu_queue(struct task_struct *p, int cpu)
2581 2612
2582#if defined(CONFIG_SMP) 2613#if defined(CONFIG_SMP)
2583 if (sched_feat(TTWU_QUEUE) && cpu != smp_processor_id()) { 2614 if (sched_feat(TTWU_QUEUE) && cpu != smp_processor_id()) {
2615 sched_clock_cpu(cpu); /* sync clocks x-cpu */
2584 ttwu_queue_remote(p, cpu); 2616 ttwu_queue_remote(p, cpu);
2585 return; 2617 return;
2586 } 2618 }
@@ -2631,17 +2663,17 @@ try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
2631 while (p->on_cpu) { 2663 while (p->on_cpu) {
2632#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW 2664#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
2633 /* 2665 /*
2634 * If called from interrupt context we could have landed in the 2666 * In case the architecture enables interrupts in
2635 * middle of schedule(), in this case we should take care not 2667 * context_switch(), we cannot busy wait, since that
2636 * to spin on ->on_cpu if p is current, since that would 2668 * would lead to deadlocks when an interrupt hits and
2637 * deadlock. 2669 * tries to wake up @prev. So bail and do a complete
2670 * remote wakeup.
2638 */ 2671 */
2639 if (p == current) { 2672 if (ttwu_activate_remote(p, wake_flags))
2640 ttwu_queue(p, cpu);
2641 goto stat; 2673 goto stat;
2642 } 2674#else
2643#endif
2644 cpu_relax(); 2675 cpu_relax();
2676#endif
2645 } 2677 }
2646 /* 2678 /*
2647 * Pairs with the smp_wmb() in finish_lock_switch(). 2679 * Pairs with the smp_wmb() in finish_lock_switch().
@@ -2655,8 +2687,10 @@ try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
2655 p->sched_class->task_waking(p); 2687 p->sched_class->task_waking(p);
2656 2688
2657 cpu = select_task_rq(p, SD_BALANCE_WAKE, wake_flags); 2689 cpu = select_task_rq(p, SD_BALANCE_WAKE, wake_flags);
2658 if (task_cpu(p) != cpu) 2690 if (task_cpu(p) != cpu) {
2691 wake_flags |= WF_MIGRATED;
2659 set_task_cpu(p, cpu); 2692 set_task_cpu(p, cpu);
2693 }
2660#endif /* CONFIG_SMP */ 2694#endif /* CONFIG_SMP */
2661 2695
2662 ttwu_queue(p, cpu); 2696 ttwu_queue(p, cpu);
@@ -5841,7 +5875,7 @@ void __cpuinit init_idle(struct task_struct *idle, int cpu)
5841 idle->state = TASK_RUNNING; 5875 idle->state = TASK_RUNNING;
5842 idle->se.exec_start = sched_clock(); 5876 idle->se.exec_start = sched_clock();
5843 5877
5844 cpumask_copy(&idle->cpus_allowed, cpumask_of(cpu)); 5878 do_set_cpus_allowed(idle, cpumask_of(cpu));
5845 /* 5879 /*
5846 * We're having a chicken and egg problem, even though we are 5880 * We're having a chicken and egg problem, even though we are
5847 * holding rq->lock, the cpu isn't yet set to this cpu so the 5881 * holding rq->lock, the cpu isn't yet set to this cpu so the
@@ -5929,6 +5963,16 @@ static inline void sched_init_granularity(void)
5929} 5963}
5930 5964
5931#ifdef CONFIG_SMP 5965#ifdef CONFIG_SMP
5966void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
5967{
5968 if (p->sched_class && p->sched_class->set_cpus_allowed)
5969 p->sched_class->set_cpus_allowed(p, new_mask);
5970 else {
5971 cpumask_copy(&p->cpus_allowed, new_mask);
5972 p->rt.nr_cpus_allowed = cpumask_weight(new_mask);
5973 }
5974}
5975
5932/* 5976/*
5933 * This is how migration works: 5977 * This is how migration works:
5934 * 5978 *
@@ -5974,12 +6018,7 @@ int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
5974 goto out; 6018 goto out;
5975 } 6019 }
5976 6020
5977 if (p->sched_class->set_cpus_allowed) 6021 do_set_cpus_allowed(p, new_mask);
5978 p->sched_class->set_cpus_allowed(p, new_mask);
5979 else {
5980 cpumask_copy(&p->cpus_allowed, new_mask);
5981 p->rt.nr_cpus_allowed = cpumask_weight(new_mask);
5982 }
5983 6022
5984 /* Can the task run on the task's current CPU? If so, we're done */ 6023 /* Can the task run on the task's current CPU? If so, we're done */
5985 if (cpumask_test_cpu(task_cpu(p), new_mask)) 6024 if (cpumask_test_cpu(task_cpu(p), new_mask))
@@ -8764,42 +8803,10 @@ cpu_cgroup_can_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
8764 return 0; 8803 return 0;
8765} 8804}
8766 8805
8767static int
8768cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
8769 struct task_struct *tsk, bool threadgroup)
8770{
8771 int retval = cpu_cgroup_can_attach_task(cgrp, tsk);
8772 if (retval)
8773 return retval;
8774 if (threadgroup) {
8775 struct task_struct *c;
8776 rcu_read_lock();
8777 list_for_each_entry_rcu(c, &tsk->thread_group, thread_group) {
8778 retval = cpu_cgroup_can_attach_task(cgrp, c);
8779 if (retval) {
8780 rcu_read_unlock();
8781 return retval;
8782 }
8783 }
8784 rcu_read_unlock();
8785 }
8786 return 0;
8787}
8788
8789static void 8806static void
8790cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp, 8807cpu_cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
8791 struct cgroup *old_cont, struct task_struct *tsk,
8792 bool threadgroup)
8793{ 8808{
8794 sched_move_task(tsk); 8809 sched_move_task(tsk);
8795 if (threadgroup) {
8796 struct task_struct *c;
8797 rcu_read_lock();
8798 list_for_each_entry_rcu(c, &tsk->thread_group, thread_group) {
8799 sched_move_task(c);
8800 }
8801 rcu_read_unlock();
8802 }
8803} 8810}
8804 8811
8805static void 8812static void
@@ -8887,8 +8894,8 @@ struct cgroup_subsys cpu_cgroup_subsys = {
8887 .name = "cpu", 8894 .name = "cpu",
8888 .create = cpu_cgroup_create, 8895 .create = cpu_cgroup_create,
8889 .destroy = cpu_cgroup_destroy, 8896 .destroy = cpu_cgroup_destroy,
8890 .can_attach = cpu_cgroup_can_attach, 8897 .can_attach_task = cpu_cgroup_can_attach_task,
8891 .attach = cpu_cgroup_attach, 8898 .attach_task = cpu_cgroup_attach_task,
8892 .exit = cpu_cgroup_exit, 8899 .exit = cpu_cgroup_exit,
8893 .populate = cpu_cgroup_populate, 8900 .populate = cpu_cgroup_populate,
8894 .subsys_id = cpu_cgroup_subsys_id, 8901 .subsys_id = cpu_cgroup_subsys_id,