linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] kthread: run kthreadd with max priority SCHED_FIFO
@ 2007-12-17 22:43 Michal Schmidt
  2007-12-17 23:00 ` Jon Masters
  2007-12-22  9:30 ` Andrew Morton
  0 siblings, 2 replies; 21+ messages in thread
From: Michal Schmidt @ 2007-12-17 22:43 UTC (permalink / raw)
  To: linux-kernel
  Cc: Eric W. Biederman, Jon Masters, Andrew Morton, Satoru Takeuchi

kthreadd, the creator of other kernel threads, runs as a normal
priority task. This is a potential for priority inversion when a task
wants to spawn a high-priority kernel thread. A middle priority
SCHED_FIFO task can block kthreadd's execution indefinitely and thus
prevent the timely creation of the high-priority kernel thread.
    
This causes a practical problem. When a runaway real-time task is
eating 100% CPU and we attempt to put the CPU offline, sometimes we
block while waiting for the creation of the highest-priority
"kstopmachine" thread. 

The fix is to run kthreadd with the highest possible SCHED_FIFO
priority. Its children must still run as slightly negatively reniced
SCHED_NORMAL tasks.
    
Signed-off-by: Michal Schmidt <mschmidt@redhat.com>

diff --git a/kernel/kthread.c b/kernel/kthread.c
index dcfe724..a7ce932 100644
--- a/kernel/kthread.c
+++ b/kernel/kthread.c
@@ -94,10 +94,17 @@ static void create_kthread(struct kthread_create_info *create)
 	if (pid < 0) {
 		create->result = ERR_PTR(pid);
 	} else {
+		struct sched_param param = { .sched_priority = 0 };
 		wait_for_completion(&create->started);
 		read_lock(&tasklist_lock);
 		create->result = find_task_by_pid(pid);
 		read_unlock(&tasklist_lock);
+		/*
+		 * We (kthreadd) run with SCHED_FIFO, but we don't want
+		 * the kthreads we create to have it too by default.
+		 */
+		sched_setscheduler(create->result, SCHED_NORMAL, &param);
+		set_user_nice(create->result, -5);
 	}
 	complete(&create->done);
 }
@@ -217,11 +224,12 @@ EXPORT_SYMBOL(kthread_stop);
 int kthreadd(void *unused)
 {
 	struct task_struct *tsk = current;
+	struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
 
 	/* Setup a clean context for our children to inherit. */
 	set_task_comm(tsk, "kthreadd");
 	ignore_signals(tsk);
-	set_user_nice(tsk, -5);
+	sched_setscheduler(tsk, SCHED_FIFO, &param);
 	set_cpus_allowed(tsk, CPU_MASK_ALL);
 
 	current->flags |= PF_NOFREEZE;

^ permalink raw reply related	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2008-01-08 16:23 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-12-17 22:43 [PATCH] kthread: run kthreadd with max priority SCHED_FIFO Michal Schmidt
2007-12-17 23:00 ` Jon Masters
2007-12-22  9:30 ` Andrew Morton
2007-12-22  9:52   ` Jon Masters
2007-12-22 10:11     ` Andrew Morton
2007-12-22 10:18       ` Jon Masters
2007-12-22 10:39     ` Mike Galbraith
2007-12-22 10:52       ` Andrew Morton
2007-12-22 11:21         ` Jon Masters
2007-12-23  8:50         ` Mike Galbraith
2008-01-07 10:06   ` [PATCH] kthread: always create the kernel threads with normal priority Michal Schmidt
2008-01-07 10:25     ` Andrew Morton
2008-01-07 11:09       ` Ingo Molnar
2008-01-07 17:29         ` Andrew Morton
2008-01-07 17:47           ` Peter Zijlstra
2008-01-08  9:54           ` Michal Schmidt
2008-01-07 13:18       ` Michal Schmidt
2008-01-08 16:22         ` Ingo Molnar
2008-01-07 11:22     ` Remy Bohmer
2008-01-07 13:10       ` Michal Schmidt
2008-01-07 15:53         ` Remy Bohmer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).