From: Thomas Gleixner <tglx@linutronix.de>
To: LKML <linux-kernel@vger.kernel.org>
Cc: linux-arch@vger.kernel.org, Peter Zijlstra <peterz@infradead.org>,
Rusty Russell <rusty@rustcorp.com.au>,
"Paul E. McKenney" <paulmck@linux.vnet.ibm.com>,
Ingo Molnar <mingo@kernel.org>,
"Srivatsa S. Bhat" <srivatsa.bhat@linux.vnet.ibm.com>,
Matt Turner <mattst88@gmail.com>,
Russell King <linux@arm.linux.org.uk>,
Mike Frysinger <vapier@gentoo.org>,
Jesper Nilsson <jesper.nilsson@axis.com>,
Richard Kuo <rkuo@codeaurora.org>,
Tony Luck <tony.luck@intel.com>,
Hirokazu Takata <takata@linux-m32r.org>,
Ralf Baechle <ralf@linux-mips.org>,
David Howells <dhowells@redhat.com>,
"James E.J. Bottomley" <jejb@parisc-linux.org>,
Benjamin Herrenschmidt <benh@kernel.crashing.org>,
Martin Schwidefsky <schwidefsky@de.ibm.com>,
Paul Mundt <lethal@linux-sh.org>,
"David S. Miller" <davem@davemloft.net>,
Chris Metcalf <cmetcalf@tilera.com>,
Richard Weinberger <richard@nod.at>,
x86@kernel.org
Subject: [patch 04/18] smp: Provide generic idle thread allocation
Date: Fri, 20 Apr 2012 13:05:45 -0000 [thread overview]
Message-ID: <20120420124557.102478630@linutronix.de> (raw)
In-Reply-To: 20120420122120.097464672@linutronix.de
[-- Attachment #1: smp-provide-generic-idle-thread-allocation.patch --]
[-- Type: text/plain, Size: 6047 bytes --]
All SMP architectures have magic to fork the idle task and to store it
for reusage when cpu hotplug is enabled. Provide a generic
infrastructure for it.
Create/reinit the idle thread for the cpu which is brought up in the
generic code and hand the thread pointer to the architecture code via
__cpu_up().
Note, that fork_idle() is called via a workqueue, because this
guarantees that the idle thread does not get a reference to a user
space VM. This can happen when the boot process did not bring up all
possible cpus and a later cpu_up() is initiated via the sysfs
interface. In that case fork_idle() would be called in the context of
the user space task and take a reference on the user space VM.
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: Matt Turner <mattst88@gmail.com>
Cc: Russell King <linux@arm.linux.org.uk>
Cc: Mike Frysinger <vapier@gentoo.org>
Cc: Jesper Nilsson <jesper.nilsson@axis.com>
Cc: Richard Kuo <rkuo@codeaurora.org>
Cc: Tony Luck <tony.luck@intel.com>
Cc: Hirokazu Takata <takata@linux-m32r.org>
Cc: Ralf Baechle <ralf@linux-mips.org>
Cc: David Howells <dhowells@redhat.com>
Cc: "James E.J. Bottomley" <jejb@parisc-linux.org>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: Martin Schwidefsky <schwidefsky@de.ibm.com>
Cc: Paul Mundt <lethal@linux-sh.org>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Chris Metcalf <cmetcalf@tilera.com>
Cc: Richard Weinberger <richard@nod.at>
Cc: x86@kernel.org
---
arch/Kconfig | 3 +
kernel/cpu.c | 2 -
kernel/sched/core.c | 2 +
kernel/smpboot.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++
kernel/smpboot.h | 10 ++++++
5 files changed, 99 insertions(+), 1 deletion(-)
Index: linux-2.6/arch/Kconfig
===================================================================
--- linux-2.6.orig/arch/Kconfig
+++ linux-2.6/arch/Kconfig
@@ -145,6 +145,9 @@ config HAVE_DMA_ATTRS
config USE_GENERIC_SMP_HELPERS
bool
+config GENERIC_SMP_IDLE_THREAD
+ bool
+
config HAVE_REGS_AND_STACK_ACCESS_API
bool
help
Index: linux-2.6/kernel/cpu.c
===================================================================
--- linux-2.6.orig/kernel/cpu.c
+++ linux-2.6/kernel/cpu.c
@@ -316,7 +316,7 @@ static int __cpuinit _cpu_up(unsigned in
}
/* Arch-specific enabling code. */
- ret = __cpu_up(cpu, NULL);
+ ret = __cpu_up(cpu, idle_thread_get(cpu));
if (ret != 0)
goto out_notify;
BUG_ON(!cpu_online(cpu));
Index: linux-2.6/kernel/sched/core.c
===================================================================
--- linux-2.6.orig/kernel/sched/core.c
+++ linux-2.6/kernel/sched/core.c
@@ -83,6 +83,7 @@
#include "sched.h"
#include "../workqueue_sched.h"
+#include "../smpboot.h"
#define CREATE_TRACE_POINTS
#include <trace/events/sched.h>
@@ -7049,6 +7050,7 @@ void __init sched_init(void)
/* May be allocated at isolcpus cmdline parse time */
if (cpu_isolated_map == NULL)
zalloc_cpumask_var(&cpu_isolated_map, GFP_NOWAIT);
+ idle_thread_set_boot_cpu();
#endif
init_sched_fair_class();
Index: linux-2.6/kernel/smpboot.c
===================================================================
--- linux-2.6.orig/kernel/smpboot.c
+++ linux-2.6/kernel/smpboot.c
@@ -1,12 +1,95 @@
/*
* Common SMP CPU bringup/teardown functions
*/
+#include <linux/err.h>
+#include <linux/smp.h>
+#include <linux/sched.h>
+#include <linux/percpu.h>
+#include <linux/workqueue.h>
#include "smpboot.h"
+#ifdef CONFIG_GENERIC_SMP_IDLE_THREAD
+struct create_idle {
+ struct work_struct work;
+ struct task_struct *idle;
+ struct completion done;
+ unsigned int cpu;
+};
+
+static void __cpuinit do_fork_idle(struct work_struct *work)
+{
+ struct create_idle *c = container_of(work, struct create_idle, work);
+
+ c->idle = fork_idle(c->cpu);
+ complete(&c->done);
+}
+
+static struct task_struct * __cpuinit idle_thread_create(unsigned int cpu)
+{
+ 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);
+ destroy_work_on_stack(&c_idle.work);
+ return c_idle.idle;
+}
+
+/*
+ * For the hotplug case we keep the task structs around and reuse
+ * them.
+ */
+static DEFINE_PER_CPU(struct task_struct *, idle_threads);
+
+static inline struct task_struct *get_idle_for_cpu(unsigned int cpu)
+{
+ struct task_struct *tsk = per_cpu(idle_threads, cpu);
+
+ if (!tsk)
+ return idle_thread_create(cpu);
+ init_idle(tsk, cpu);
+ return tsk;
+}
+
+struct task_struct * __cpuinit idle_thread_get(unsigned int cpu)
+{
+ return per_cpu(idle_threads, cpu);
+}
+
+void __init idle_thread_set_boot_cpu(void)
+{
+ per_cpu(idle_threads, smp_processor_id()) = current;
+}
+
+/**
+ * idle_thread_init - Initialize the idle thread for a cpu
+ * @cpu: The cpu for which the idle thread should be initialized
+ *
+ * Creates the thread if it does not exist.
+ */
+static int __cpuinit idle_thread_init(unsigned int cpu)
+{
+ struct task_struct *idle = get_idle_for_cpu(cpu);
+
+ if (IS_ERR(idle)) {
+ printk(KERN_ERR "failed fork for CPU %u\n", cpu);
+ return PTR_ERR(idle);
+ }
+ per_cpu(idle_threads, cpu) = idle;
+ return 0;
+}
+#else
+static inline int idle_thread_init(unsigned int cpu) { return 0; }
+#endif
+
/**
* smpboot_prepare - generic smpboot preparation
*/
int __cpuinit smpboot_prepare(unsigned int cpu)
{
+ return idle_thread_init(cpu);
}
Index: linux-2.6/kernel/smpboot.h
===================================================================
--- linux-2.6.orig/kernel/smpboot.h
+++ linux-2.6/kernel/smpboot.h
@@ -1,6 +1,16 @@
#ifndef SMPBOOT_H
#define SMPBOOT_H
+struct task_struct;
+
int smpboot_prepare(unsigned int cpu);
+#ifdef CONFIG_GENERIC_SMP_IDLE_THREAD
+struct task_struct *idle_thread_get(unsigned int cpu);
+void idle_thread_set_boot_cpu(void);
+#else
+static inline struct task_struct *idle_thread_get(unsigned int cpu) { return NULL; }
+static inline void idle_thread_set_boot_cpu(void) { }
+#endif
+
#endif
next prev parent reply other threads:[~2012-04-20 13:07 UTC|newest]
Thread overview: 75+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-04-20 13:05 [patch 00/18] SMP: Boot and CPU hotplug refactoring - Part 1 Thomas Gleixner
2012-04-20 13:05 ` [patch 01/18] m32r: Remove pointless function prototypes Thomas Gleixner
2012-04-28 9:01 ` [tip:smp/hotplug] " tip-bot for Thomas Gleixner
2012-04-20 13:05 ` [patch 02/18] smp: Add task_struct argument to __cpu_up() Thomas Gleixner
2012-04-23 7:58 ` Jesper Nilsson
2012-04-28 9:02 ` [tip:smp/hotplug] " tip-bot for Thomas Gleixner
2012-04-20 13:05 ` [patch 03/18] smp: Add generic smpboot facility Thomas Gleixner
2012-04-20 20:07 ` Yinghai Lu
2012-04-21 2:07 ` Stephen Rothwell
2012-04-28 9:04 ` [tip:smp/hotplug] " tip-bot for Thomas Gleixner
2012-04-20 13:05 ` Thomas Gleixner [this message]
2012-04-20 16:21 ` [patch 04/18] smp: Provide generic idle thread allocation Sam Ravnborg
2012-04-20 18:55 ` Thomas Gleixner
2012-04-21 2:20 ` Stephen Rothwell
2012-04-21 2:25 ` Frank Rowand
2012-04-28 9:05 ` [tip:smp/hotplug] " tip-bot for Thomas Gleixner
2012-04-20 13:05 ` [patch 05/18] x86: Add task_struct argument to smp_ops.cpu_up Thomas Gleixner
2012-04-28 9:05 ` [tip:smp/hotplug] " tip-bot for Thomas Gleixner
2012-04-20 13:05 ` [patch 06/18] x86: Use generic idle thread allocation Thomas Gleixner
2012-04-28 9:06 ` [tip:smp/hotplug] " tip-bot for Thomas Gleixner
2012-04-20 13:05 ` [patch 07/18] powerpc: " Thomas Gleixner
2012-04-28 9:07 ` [tip:smp/hotplug] " tip-bot for Thomas Gleixner
2012-04-28 22:51 ` Benjamin Herrenschmidt
2012-04-20 13:05 ` [patch 08/18] ia64: " Thomas Gleixner
2012-04-28 9:08 ` [tip:smp/hotplug] " tip-bot for Thomas Gleixner
2012-04-20 13:05 ` [patch 09/18] arm: " Thomas Gleixner
2012-04-28 9:09 ` [tip:smp/hotplug] " tip-bot for Thomas Gleixner
2012-04-20 13:05 ` [patch 11/18] hexagon: " Thomas Gleixner
2012-04-24 16:47 ` Richard Kuo
2012-04-28 9:10 ` [tip:smp/hotplug] " tip-bot for Thomas Gleixner
2012-04-20 13:05 ` [patch 10/18] mips: " Thomas Gleixner
2012-04-28 9:10 ` [tip:smp/hotplug] " tip-bot for Thomas Gleixner
2012-04-20 13:05 ` [patch 12/18] s390: " Thomas Gleixner
2012-04-23 7:09 ` Martin Schwidefsky
2012-04-28 9:11 ` [tip:smp/hotplug] " tip-bot for Thomas Gleixner
2012-04-20 13:05 ` [patch 13/18] blackfin: " Thomas Gleixner
2012-04-28 9:13 ` [tip:smp/hotplug] " tip-bot for Thomas Gleixner
2012-04-20 13:05 ` [patch 14/18] cris: " Thomas Gleixner
2012-04-23 7:57 ` Jesper Nilsson
2012-04-28 9:12 ` [tip:smp/hotplug] " tip-bot for Thomas Gleixner
2012-04-20 13:05 ` [patch 15/18] sh: " Thomas Gleixner
2012-04-21 3:18 ` Paul Mundt
2012-04-28 9:14 ` [tip:smp/hotplug] " tip-bot for Thomas Gleixner
2012-04-20 13:05 ` [patch 16/18] alpha: " Thomas Gleixner
2012-04-28 9:15 ` [tip:smp/hotplug] " tip-bot for Thomas Gleixner
2012-04-20 13:05 ` [patch 17/18] parisc: " Thomas Gleixner
2012-04-28 9:16 ` [tip:smp/hotplug] " tip-bot for Thomas Gleixner
2012-04-20 13:05 ` [patch 18/18] sparc: " Thomas Gleixner
2012-04-22 19:52 ` David Miller
2012-04-28 9:15 ` [tip:smp/hotplug] " tip-bot for Thomas Gleixner
2012-04-20 13:16 ` [patch 00/18] SMP: Boot and CPU hotplug refactoring - Part 1 Thomas Gleixner
2012-04-20 13:21 ` Peter Zijlstra
2012-04-20 13:47 ` Thomas Gleixner
2012-04-21 0:08 ` Suresh Siddha
2012-05-03 9:41 ` Thomas Gleixner
2012-05-03 23:42 ` Suresh Siddha
2012-05-03 17:43 ` [tip:smp/hotplug] smp, idle: Allocate idle thread for each possible cpu during boot tip-bot for Suresh Siddha
2012-04-20 15:42 ` [patch 00/18] SMP: Boot and CPU hotplug refactoring - Part 1 Tejun Heo
2012-04-20 15:49 ` Peter Zijlstra
2012-04-20 15:56 ` Thomas Gleixner
2012-04-20 13:56 ` Srivatsa S. Bhat
2012-04-20 14:18 ` Thomas Gleixner
2012-04-24 18:44 ` Konrad Rzeszutek Wilk
2012-05-21 1:42 ` Rusty Russell
2012-05-21 8:25 ` Thomas Gleixner
2012-05-22 0:53 ` Rusty Russell
2012-04-20 14:06 ` richard -rw- weinberger
2012-04-20 14:19 ` Thomas Gleixner
2012-04-20 14:27 ` James Bottomley
2012-04-20 17:55 ` Paul E. McKenney
2012-04-20 23:11 ` Venki Pallipadi
2012-04-21 1:04 ` Frank Rowand
2012-04-21 1:55 ` Frank Rowand
2012-04-22 21:01 ` Chris Metcalf
2012-04-30 8:05 ` Santosh Shilimkar
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=20120420124557.102478630@linutronix.de \
--to=tglx@linutronix.de \
--cc=benh@kernel.crashing.org \
--cc=cmetcalf@tilera.com \
--cc=davem@davemloft.net \
--cc=dhowells@redhat.com \
--cc=jejb@parisc-linux.org \
--cc=jesper.nilsson@axis.com \
--cc=lethal@linux-sh.org \
--cc=linux-arch@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@arm.linux.org.uk \
--cc=mattst88@gmail.com \
--cc=mingo@kernel.org \
--cc=paulmck@linux.vnet.ibm.com \
--cc=peterz@infradead.org \
--cc=ralf@linux-mips.org \
--cc=richard@nod.at \
--cc=rkuo@codeaurora.org \
--cc=rusty@rustcorp.com.au \
--cc=schwidefsky@de.ibm.com \
--cc=srivatsa.bhat@linux.vnet.ibm.com \
--cc=takata@linux-m32r.org \
--cc=tony.luck@intel.com \
--cc=vapier@gentoo.org \
--cc=x86@kernel.org \
/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;
as well as URLs for NNTP newsgroup(s).