diff options
author | Thomas Gleixner <tglx@linutronix.de> | 2012-04-20 09:05:45 -0400 |
---|---|---|
committer | Thomas Gleixner <tglx@linutronix.de> | 2012-04-26 06:06:09 -0400 |
commit | 29d5e0476e1c4a513859e7858845ad172f560389 (patch) | |
tree | b353eee6e4cdf57207e6eba5d80564a4e8811cc9 /kernel/smpboot.c | |
parent | 38498a67aa2cf8c80754b8d304bfacc10bc582b5 (diff) |
smp: Provide generic idle thread allocation
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: Peter Zijlstra <peterz@infradead.org>
Cc: Rusty Russell <rusty@rustcorp.com.au>
Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Cc: Srivatsa S. Bhat <srivatsa.bhat@linux.vnet.ibm.com>
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
Acked-by: Venkatesh Pallipadi <venki@google.com>
Link: http://lkml.kernel.org/r/20120420124557.102478630@linutronix.de
Diffstat (limited to 'kernel/smpboot.c')
-rw-r--r-- | kernel/smpboot.c | 84 |
1 files changed, 83 insertions, 1 deletions
diff --git a/kernel/smpboot.c b/kernel/smpboot.c index 6dae6a3d2d59..ed1576981801 100644 --- a/kernel/smpboot.c +++ b/kernel/smpboot.c | |||
@@ -1,14 +1,96 @@ | |||
1 | /* | 1 | /* |
2 | * Common SMP CPU bringup/teardown functions | 2 | * Common SMP CPU bringup/teardown functions |
3 | */ | 3 | */ |
4 | #include <linux/err.h> | ||
5 | #include <linux/smp.h> | ||
4 | #include <linux/init.h> | 6 | #include <linux/init.h> |
7 | #include <linux/sched.h> | ||
8 | #include <linux/percpu.h> | ||
9 | #include <linux/workqueue.h> | ||
5 | 10 | ||
6 | #include "smpboot.h" | 11 | #include "smpboot.h" |
7 | 12 | ||
13 | #ifdef CONFIG_GENERIC_SMP_IDLE_THREAD | ||
14 | struct create_idle { | ||
15 | struct work_struct work; | ||
16 | struct task_struct *idle; | ||
17 | struct completion done; | ||
18 | unsigned int cpu; | ||
19 | }; | ||
20 | |||
21 | static void __cpuinit do_fork_idle(struct work_struct *work) | ||
22 | { | ||
23 | struct create_idle *c = container_of(work, struct create_idle, work); | ||
24 | |||
25 | c->idle = fork_idle(c->cpu); | ||
26 | complete(&c->done); | ||
27 | } | ||
28 | |||
29 | static struct task_struct * __cpuinit idle_thread_create(unsigned int cpu) | ||
30 | { | ||
31 | struct create_idle c_idle = { | ||
32 | .cpu = cpu, | ||
33 | .done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done), | ||
34 | }; | ||
35 | |||
36 | INIT_WORK_ONSTACK(&c_idle.work, do_fork_idle); | ||
37 | schedule_work(&c_idle.work); | ||
38 | wait_for_completion(&c_idle.done); | ||
39 | destroy_work_on_stack(&c_idle.work); | ||
40 | return c_idle.idle; | ||
41 | } | ||
42 | |||
43 | /* | ||
44 | * For the hotplug case we keep the task structs around and reuse | ||
45 | * them. | ||
46 | */ | ||
47 | static DEFINE_PER_CPU(struct task_struct *, idle_threads); | ||
48 | |||
49 | static inline struct task_struct *get_idle_for_cpu(unsigned int cpu) | ||
50 | { | ||
51 | struct task_struct *tsk = per_cpu(idle_threads, cpu); | ||
52 | |||
53 | if (!tsk) | ||
54 | return idle_thread_create(cpu); | ||
55 | init_idle(tsk, cpu); | ||
56 | return tsk; | ||
57 | } | ||
58 | |||
59 | struct task_struct * __cpuinit idle_thread_get(unsigned int cpu) | ||
60 | { | ||
61 | return per_cpu(idle_threads, cpu); | ||
62 | } | ||
63 | |||
64 | void __init idle_thread_set_boot_cpu(void) | ||
65 | { | ||
66 | per_cpu(idle_threads, smp_processor_id()) = current; | ||
67 | } | ||
68 | |||
69 | /** | ||
70 | * idle_thread_init - Initialize the idle thread for a cpu | ||
71 | * @cpu: The cpu for which the idle thread should be initialized | ||
72 | * | ||
73 | * Creates the thread if it does not exist. | ||
74 | */ | ||
75 | static int __cpuinit idle_thread_init(unsigned int cpu) | ||
76 | { | ||
77 | struct task_struct *idle = get_idle_for_cpu(cpu); | ||
78 | |||
79 | if (IS_ERR(idle)) { | ||
80 | printk(KERN_ERR "failed fork for CPU %u\n", cpu); | ||
81 | return PTR_ERR(idle); | ||
82 | } | ||
83 | per_cpu(idle_threads, cpu) = idle; | ||
84 | return 0; | ||
85 | } | ||
86 | #else | ||
87 | static inline int idle_thread_init(unsigned int cpu) { return 0; } | ||
88 | #endif | ||
89 | |||
8 | /** | 90 | /** |
9 | * smpboot_prepare - generic smpboot preparation | 91 | * smpboot_prepare - generic smpboot preparation |
10 | */ | 92 | */ |
11 | int __cpuinit smpboot_prepare(unsigned int cpu) | 93 | int __cpuinit smpboot_prepare(unsigned int cpu) |
12 | { | 94 | { |
13 | return 0; | 95 | return idle_thread_init(cpu); |
14 | } | 96 | } |