Skip to content

Commit 30da688

Browse files
oleg-nesterovIngo Molnar
authored andcommitted
sched: sched_exec(): Remove the select_fallback_rq() logic
sched_exec()->select_task_rq() reads/updates ->cpus_allowed lockless. This can race with other CPUs updating our ->cpus_allowed, and this looks meaningless to me. The task is current and running, it must have online cpus in ->cpus_allowed, the fallback mode is bogus. And, if ->sched_class returns the "wrong" cpu, this likely means we raced with set_cpus_allowed() which was called for reason, why should sched_exec() retry and call ->select_task_rq() again? Change the code to call sched_class->select_task_rq() directly and do nothing if the returned cpu is wrong after re-checking under rq->lock. From now task_struct->cpus_allowed is always stable under TASK_WAKING, select_fallback_rq() is always called under rq-lock or the caller or the caller owns TASK_WAKING (select_task_rq). Signed-off-by: Oleg Nesterov <oleg@redhat.com> Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl> LKML-Reference: <20100315091019.GA9141@redhat.com> Signed-off-by: Ingo Molnar <mingo@elte.hu>
1 parent c1804d5 commit 30da688

1 file changed

Lines changed: 8 additions & 17 deletions

File tree

‎kernel/sched.c‎

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2280,6 +2280,9 @@ void task_oncpu_function_call(struct task_struct *p,
22802280
}
22812281

22822282
#ifdef CONFIG_SMP
2283+
/*
2284+
* ->cpus_allowed is protected by either TASK_WAKING or rq->lock held.
2285+
*/
22832286
static int select_fallback_rq(int cpu, struct task_struct *p)
22842287
{
22852288
int dest_cpu;
@@ -2316,12 +2319,7 @@ static int select_fallback_rq(int cpu, struct task_struct *p)
23162319
}
23172320

23182321
/*
2319-
* Gets called from 3 sites (exec, fork, wakeup), since it is called without
2320-
* holding rq->lock we need to ensure ->cpus_allowed is stable, this is done
2321-
* by:
2322-
*
2323-
* exec: is unstable, retry loop
2324-
* fork & wake-up: serialize ->cpus_allowed against TASK_WAKING
2322+
* The caller (fork, wakeup) owns TASK_WAKING, ->cpus_allowed is stable.
23252323
*/
23262324
static inline
23272325
int select_task_rq(struct task_struct *p, int sd_flags, int wake_flags)
@@ -3076,28 +3074,21 @@ void sched_exec(void)
30763074
unsigned long flags;
30773075
struct rq *rq;
30783076

3079-
again:
30803077
this_cpu = get_cpu();
3081-
dest_cpu = select_task_rq(p, SD_BALANCE_EXEC, 0);
3078+
dest_cpu = p->sched_class->select_task_rq(p, SD_BALANCE_EXEC, 0);
30823079
if (dest_cpu == this_cpu) {
30833080
put_cpu();
30843081
return;
30853082
}
30863083

30873084
rq = task_rq_lock(p, &flags);
30883085
put_cpu();
3089-
30903086
/*
30913087
* select_task_rq() can race against ->cpus_allowed
30923088
*/
3093-
if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed)
3094-
|| unlikely(!cpu_active(dest_cpu))) {
3095-
task_rq_unlock(rq, &flags);
3096-
goto again;
3097-
}
3098-
3099-
/* force the process onto the specified CPU */
3100-
if (migrate_task(p, dest_cpu, &req)) {
3089+
if (cpumask_test_cpu(dest_cpu, &p->cpus_allowed) &&
3090+
likely(cpu_active(dest_cpu)) &&
3091+
migrate_task(p, dest_cpu, &req)) {
31013092
/* Need to wait for migration thread (might exit: take ref). */
31023093
struct task_struct *mt = rq->migration_thread;
31033094

0 commit comments

Comments
 (0)