MIPS: Move idle task creation to work queue [Linux 2.6.38]

This Linux kernel change "MIPS: Move idle task creation to work queue" is included in the Linux 2.6.38 release. This change is authored by Maksim Rayskiy <mrayskiy [at] broadcom.com> on Sat Feb 12 10:21:32 2011 -0800. The commit for this change in Linux stable tree is 6667deb (patch).

MIPS: Move idle task creation to work queue

To avoid forking usermode thread when creating an idle task, move fork_idle
to a work queue.

If kernel starts with maxcpus= option which does not bring all available
cpus online at boot time, idle tasks for offline cpus are not created. If
later offline cpus are hotplugged through sysfs, __cpu_up is called in
the context of the user task, and fork_idle copies its non-zero mm
pointer.  This causes BUG() in per_cpu_trap_init.

This also avoids issues with resource limits of the CPU writing to sysfs,
containers, maybe others.

Signed-off-by: Maksim Rayskiy <mrayskiy@broadcom.com>
To: linux-mips@linux-mips.org
Patchwork: https://patchwork.linux-mips.org/patch/2070/
Signed-off-by: Ralf Baechle <ralf@linux-mips.org>

There are 31 lines of Linux source code added/deleted in this change. Code changes to Linux kernel are as follows.

 arch/mips/kernel/smp.c | 31 +++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)

diff --git a/arch/mips/kernel/smp.c b/arch/mips/kernel/smp.c
index 383aeb9..32a2561 100644
--- a/arch/mips/kernel/smp.c
+++ b/arch/mips/kernel/smp.c
@@ -193,6 +193,22 @@ void __devinit smp_prepare_boot_cpu(void)
  */
 static struct task_struct *cpu_idle_thread[NR_CPUS];

+struct create_idle {
+   struct work_struct work;
+   struct task_struct *idle;
+   struct completion done;
+   int cpu;
+};
+
+static void __cpuinit do_fork_idle(struct work_struct *work)
+{
+   struct create_idle *c_idle =
+       container_of(work, struct create_idle, work);
+
+   c_idle->idle = fork_idle(c_idle->cpu);
+   complete(&c_idle->done);
+}
+
 int __cpuinit __cpu_up(unsigned int cpu)
 {
    struct task_struct *idle;
@@ -203,8 +219,19 @@ int __cpuinit __cpu_up(unsigned int cpu)
     * Linux can schedule processes on this slave.
     */
    if (!cpu_idle_thread[cpu]) {
-       idle = fork_idle(cpu);
-       cpu_idle_thread[cpu] = idle;
+       /*
+        * Schedule work item to avoid forking user task
+        * Ported from arch/x86/kernel/smpboot.c
+        */
+       struct create_idle c_idle = {
+           .cpu    = cpu,
+           .done   = COMPLETION_INITIALIZER_ONSTACK(c_idle.done),
+       };
+
+       INIT_WORK_ONSTACK(&c_idle.work, do_fork_idle);
+       schedule_work(&c_idle.work);
+       wait_for_completion(&c_idle.done);
+       idle = cpu_idle_thread[cpu] = c_idle.idle;

        if (IS_ERR(idle))
            panic(KERN_ERR "Fork failed for CPU %d", cpu);

Leave a Reply

Your email address will not be published. Required fields are marked *