diff --git a/fs/proc/array.c b/fs/proc/array.c index ec53f3b145e73522befa599c40bd3c779ef0707a..260d7c996f38bf37e9937a25e443413e6cc586c5 100644 --- a/fs/proc/array.c +++ b/fs/proc/array.c @@ -384,9 +384,9 @@ static inline void task_context_switch_counts(struct seq_file *m, static void task_cpus_allowed(struct seq_file *m, struct task_struct *task) { seq_printf(m, "Cpus_allowed:\t%*pb\n", - cpumask_pr_args(task->cpus_ptr)); + cpumask_pr_args(&task->cpus_mask)); seq_printf(m, "Cpus_allowed_list:\t%*pbl\n", - cpumask_pr_args(task->cpus_ptr)); + cpumask_pr_args(&task->cpus_mask)); } static inline void task_core_dumping(struct seq_file *m, struct mm_struct *mm) diff --git a/include/trace/events/sched.h b/include/trace/events/sched.h index c96a4337afe6c562e0b59fee69072de6424f3d72..e48f584abf5ff4e6f140bde266c81e7a9bf34c60 100644 --- a/include/trace/events/sched.h +++ b/include/trace/events/sched.h @@ -650,6 +650,18 @@ DECLARE_TRACE(sched_update_nr_running_tp, TP_PROTO(struct rq *rq, int change), TP_ARGS(rq, change)); +DECLARE_TRACE(sched_migrate_disable_tp, + TP_PROTO(struct task_struct *p), + TP_ARGS(p)); + +DECLARE_TRACE(sched_migrate_enable_tp, + TP_PROTO(struct task_struct *p), + TP_ARGS(p)); + +DECLARE_TRACE(sched_migrate_pull_tp, + TP_PROTO(struct task_struct *p), + TP_ARGS(p)); + #endif /* _TRACE_SCHED_H */ /* This part must be outside protection */ diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 0fe97a9a547bc7e28b5a180b3a1bd45ef366435e..479ed40374eae7beed9a8d988c2a0850587055f1 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -1754,6 +1754,8 @@ void migrate_disable(void) return; } + trace_sched_migrate_disable_tp(p); + preempt_disable(); this_rq()->nr_pinned++; p->migration_disabled = 1; @@ -1786,6 +1788,8 @@ void migrate_enable(void) p->migration_disabled = 0; this_rq()->nr_pinned--; preempt_enable(); + + trace_sched_migrate_enable_tp(p); } EXPORT_SYMBOL_GPL(migrate_enable); @@ -1937,7 +1941,7 @@ static int migration_cpu_stop(void *data) * and we should be valid again. Nothing to do. */ if (!pending) { - WARN_ON_ONCE(!is_cpu_allowed(p, cpu_of(rq))); + WARN_ON_ONCE(!cpumask_test_cpu(task_cpu(p), &p->cpus_mask)); goto out; } @@ -1949,7 +1953,7 @@ static int migration_cpu_stop(void *data) else p->wake_cpu = dest_cpu; - } else if (dest_cpu < 0) { + } else if (dest_cpu < 0 || pending) { /* * This happens when we get migrated between migrate_enable()'s * preempt_enable() and scheduling the stopper task. At that @@ -1959,13 +1963,24 @@ static int migration_cpu_stop(void *data) * more likely. */ + /* + * The task moved before the stopper got to run. We're holding + * ->pi_lock, so the allowed mask is stable - if it got + * somewhere allowed, we're done. + */ + if (pending && cpumask_test_cpu(task_cpu(p), p->cpus_ptr)) { + p->migration_pending = NULL; + complete = true; + goto out; + } + /* * When this was migrate_enable() but we no longer have an * @pending, a concurrent SCA 'fixed' things and we should be * valid again. Nothing to do. */ if (!pending) { - WARN_ON_ONCE(!is_cpu_allowed(p, cpu_of(rq))); + WARN_ON_ONCE(!cpumask_test_cpu(task_cpu(p), &p->cpus_mask)); goto out; } @@ -2102,7 +2117,75 @@ void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask) } /* - * This function is wildly self concurrent, consider at least 3 times. + * This function is wildly self concurrent; here be dragons. + * + * + * When given a valid mask, __set_cpus_allowed_ptr() must block until the + * designated task is enqueued on an allowed CPU. If that task is currently + * running, we have to kick it out using the CPU stopper. + * + * Migrate-Disable comes along and tramples all over our nice sandcastle. + * Consider: + * + * Initial conditions: P0->cpus_mask = [0, 1] + * + * P0@CPU0 P1 + * + * migrate_disable(); + * + * set_cpus_allowed_ptr(P0, [1]); + * + * P1 *cannot* return from this set_cpus_allowed_ptr() call until P0 executes + * its outermost migrate_enable() (i.e. it exits its Migrate-Disable region). + * This means we need the following scheme: + * + * P0@CPU0 P1 + * + * migrate_disable(); + * + * set_cpus_allowed_ptr(P0, [1]); + * + * + * migrate_enable(); + * __set_cpus_allowed_ptr(); + * + * `--> + * + * Now the fun stuff: there may be several P1-like tasks, i.e. multiple + * concurrent set_cpus_allowed_ptr(P0, [*]) calls. CPU affinity changes of any + * task p are serialized by p->pi_lock, which we can leverage: the one that + * should come into effect at the end of the Migrate-Disable region is the last + * one. This means we only need to track a single cpumask (i.e. p->cpus_mask), + * but we still need to properly signal those waiting tasks at the appropriate + * moment. + * + * This is implemented using struct set_affinity_pending. The first + * __set_cpus_allowed_ptr() caller within a given Migrate-Disable region will + * setup an instance of that struct and install it on the targeted task_struct. + * Any and all further callers will reuse that instance. Those then wait for + * a completion signaled at the tail of the CPU stopper callback (1), triggered + * on the end of the Migrate-Disable region (i.e. outermost migrate_enable()). + * + * + * (1) In the cases covered above. There is one more where the completion is + * signaled within affine_move_task() itself: when a subsequent affinity request + * cancels the need for an active migration. Consider: + * + * Initial conditions: P0->cpus_mask = [0, 1] + * + * P0@CPU0 P1 P2 + * + * migrate_disable(); + * + * set_cpus_allowed_ptr(P0, [1]); + * + * set_cpus_allowed_ptr(P0, [0, 1]); + * + * + * + * Note that the above is safe vs a concurrent migrate_enable(), as any + * pending affinity completion is preceded an uninstallion of + * p->migration_pending done with p->pi_lock held. */ static int affine_move_task(struct rq *rq, struct task_struct *p, struct rq_flags *rf, int dest_cpu, unsigned int flags) @@ -2146,6 +2229,7 @@ static int affine_move_task(struct rq *rq, struct task_struct *p, struct rq_flag if (!(flags & SCA_MIGRATE_ENABLE)) { /* serialized by p->pi_lock */ if (!p->migration_pending) { + /* Install the request */ refcount_set(&my_pending.refs, 1); init_completion(&my_pending.done); p->migration_pending = &my_pending; @@ -2167,8 +2251,10 @@ static int affine_move_task(struct rq *rq, struct task_struct *p, struct rq_flag * * Either way, we really should have a @pending here. */ - if (WARN_ON_ONCE(!pending)) + if (WARN_ON_ONCE(!pending)) { + task_rq_unlock(rq, p, rf); return -EINVAL; + } if (flags & SCA_MIGRATE_ENABLE) { @@ -2189,7 +2275,11 @@ static int affine_move_task(struct rq *rq, struct task_struct *p, struct rq_flag } if (task_running(rq, p) || p->state == TASK_WAKING) { - + /* + * Lessen races (and headaches) by delegating + * is_migration_disabled(p) checks to the stopper, which will + * run on the same CPU as said p. + */ task_rq_unlock(rq, p, rf); stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg); @@ -2214,6 +2304,10 @@ static int affine_move_task(struct rq *rq, struct task_struct *p, struct rq_flag if (refcount_dec_and_test(&pending->refs)) wake_up_var(&pending->refs); + /* + * Block the original owner of &pending until all subsequent callers + * have seen the completion and decremented the refcount + */ wait_var_event(&my_pending.refs, !refcount_read(&my_pending.refs)); return 0; @@ -2262,8 +2356,17 @@ static int __set_cpus_allowed_ptr(struct task_struct *p, goto out; } - if (!(flags & SCA_MIGRATE_ENABLE) && cpumask_equal(&p->cpus_mask, new_mask)) - goto out; + if (!(flags & SCA_MIGRATE_ENABLE)) { + if (cpumask_equal(&p->cpus_mask, new_mask)) + goto out; + + if (WARN_ON_ONCE(p == current && + is_migration_disabled(p) && + !cpumask_test_cpu(task_cpu(p), new_mask))) { + ret = -EBUSY; + goto out; + } + } /* * Picking a ~random cpu helps in cases where we are changing affinity diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c index 69e9ac84237efb499b5aac88dfe67ed4def997f1..212785b9131f451315394102144c895a76d92e1d 100644 --- a/kernel/sched/deadline.c +++ b/kernel/sched/deadline.c @@ -2261,6 +2261,7 @@ static void pull_dl_task(struct rq *this_rq) goto skip; if (is_migration_disabled(p)) { + trace_sched_migrate_pull_tp(p); push_task = get_push_task(src_rq); } else { deactivate_task(src_rq, p, 0); diff --git a/kernel/sched/rt.c b/kernel/sched/rt.c index 480d6cf3001da5af144bee75f4aefe209d3ff271..0fc92376575856c7abc8b85154dc9d1674a15610 100644 --- a/kernel/sched/rt.c +++ b/kernel/sched/rt.c @@ -1889,7 +1889,12 @@ static int push_rt_task(struct rq *rq, bool pull) struct task_struct *push_task = NULL; int cpu; - if (!pull || rq->push_busy) + if (!pull) + return 0; + + trace_sched_migrate_pull_tp(next_task); + + if (rq->push_busy) return 0; cpu = find_lowest_rq(rq->curr); @@ -2235,6 +2240,7 @@ static void pull_rt_task(struct rq *this_rq) goto skip; if (is_migration_disabled(p)) { + trace_sched_migrate_pull_tp(p); push_task = get_push_task(src_rq); } else { deactivate_task(src_rq, p, 0);