public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Dmitry Adamushko <dmitry.adamushko@gmail.com>
To: Ingo Molnar <mingo@elte.hu>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>,
	LKML <linux-kernel@vger.kernel.org>
Subject: [patch, rfc: 2/2] sched, hotplug: ensure a task is on the valid cpu after set_cpus_allowed_ptr()
Date: Fri, 25 Jul 2008 00:15:30 +0200	[thread overview]
Message-ID: <1216937730.5368.16.camel@earth> (raw)

	
From: Dmitry Adamushko <dmitry.adamushko@gmail.com>
Subject: sched, hotplug: ensure a task is on the valid cpu after
set_cpus_allowed_ptr()

---
    sched, hotplug: ensure a task is on the valid cpu after set_cpus_allowed_ptr()
    
    The 'new_mask' may not include task_cpu(p) so we migrate 'p' on another 'cpu'.
    In case it can't be placed on this 'cpu' immediately, we submit a request
    to the migration thread and wait for its completion.
    
    Now, by the moment this request gets handled by the migration_thread,
    'cpu' may well be offline/non-active. As a result, 'p' continues
    running on its old cpu which is not in the 'new_mask'.
    
    Fix it: ensure 'p' ends up on a valid cpu.
    
    Theoreticaly (but unlikely), we may get an endless loop if someone cpu_down()'s
    a new cpu we have choosen on each iteration.
    
    Alternatively, we may introduce a special type of request to migration_thread,
    namely "move_to_any_allowed_cpu" (e.g. by specifying dest_cpu == -1).
    
    Note, any_active_cpu() instead of any_online_cpu() would be better here.
    
    Signed-off-by: Dmitry Adamushko <dmitry.adamushko@gmail.com>

diff --git a/kernel/sched.c b/kernel/sched.c
index b4ccc8b..c3bd78a 100644
--- a/kernel/sched.c
+++ b/kernel/sched.c
@@ -5774,21 +5774,23 @@ int set_cpus_allowed_ptr(struct task_struct *p, const cpumask_t *new_mask)
 	}
 
 	/* Can the task run on the task's current CPU? If so, we're done */
-	if (cpu_isset(task_cpu(p), *new_mask))
-		goto out;
+	while (!cpu_isset(task_cpu(p), p->cpus_allowed)) {
+		int cpu = any_online_cpu(p->cpus_allowed);
 
-	if (migrate_task(p, any_online_cpu(*new_mask), &req)) {
-		/* Need to wait for migration thread (might exit: take ref). */
-		struct task_struct *mt = rq->migration_thread;
+		if (migrate_task(p, cpu, &req)) {
+			/* Need to wait for migration thread (might exit: take ref). */
+			struct task_struct *mt = rq->migration_thread;
 
-		get_task_struct(mt);
-		task_rq_unlock(rq, &flags);
-		wake_up_process(mt);
-		put_task_struct(mt);
+			get_task_struct(mt);
+			task_rq_unlock(rq, &flags);
+			wake_up_process(mt);
+			put_task_struct(mt);
 
-		wait_for_completion(&req.done);
-		tlb_migrate_finish(p->mm);
-		return 0;
+			wait_for_completion(&req.done);
+			tlb_migrate_finish(p->mm);
+
+			rq = task_rq_lock(p, &flags);
+		}
 	}
 out:
 	task_rq_unlock(rq, &flags);



             reply	other threads:[~2008-07-24 22:15 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-07-24 22:15 Dmitry Adamushko [this message]
2008-07-25 12:40 ` [patch, rfc: 2/2] sched, hotplug: ensure a task is on the valid cpu after set_cpus_allowed_ptr() Peter Zijlstra
2008-07-25 13:20   ` Dmitry Adamushko
2008-07-25 13:39     ` Peter Zijlstra
2008-07-26 19:49       ` Dmitry Adamushko
2008-07-25 22:41 ` Gautham R Shenoy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1216937730.5368.16.camel@earth \
    --to=dmitry.adamushko@gmail.com \
    --cc=a.p.zijlstra@chello.nl \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox