Skip to content

Commit 3bb5d2e

Browse files
Suresh SiddhaKAGA-KOKO
authored andcommitted
smp, idle: Allocate idle thread for each possible cpu during boot
percpu areas are already allocated during boot for each possible cpu. percpu idle threads can be considered as an extension of the percpu areas, and allocate them for each possible cpu during boot. This will eliminate the need for workqueue based idle thread allocation. In future we can move the idle thread area into the percpu area too. [ tglx: Moved the loop into smpboot.c and added an error check when the init code failed to allocate an idle thread for a cpu which should be onlined ] Signed-off-by: Suresh Siddha <suresh.b.siddha@intel.com> 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: Tejun Heo <tj@kernel.org> Cc: David Rientjes <rientjes@google.com> Cc: venki@google.com Link: http://lkml.kernel.org/r/1334966930.28674.245.camel@sbsiddha-desk.sc.intel.com Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
1 parent 9a13472 commit 3bb5d2e

4 files changed

Lines changed: 31 additions & 56 deletions

File tree

‎kernel/cpu.c‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,15 +297,18 @@ static int __cpuinit _cpu_up(unsigned int cpu, int tasks_frozen)
297297
int ret, nr_calls = 0;
298298
void *hcpu = (void *)(long)cpu;
299299
unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
300+
struct task_struct *idle;
300301

301302
if (cpu_online(cpu) || !cpu_present(cpu))
302303
return -EINVAL;
303304

304305
cpu_hotplug_begin();
305306

306-
ret = smpboot_prepare(cpu);
307-
if (ret)
307+
idle = idle_thread_get(cpu);
308+
if (IS_ERR(idle)) {
309+
ret = PTR_ERR(idle);
308310
goto out;
311+
}
309312

310313
ret = __cpu_notify(CPU_UP_PREPARE | mod, hcpu, -1, &nr_calls);
311314
if (ret) {
@@ -316,7 +319,7 @@ static int __cpuinit _cpu_up(unsigned int cpu, int tasks_frozen)
316319
}
317320

318321
/* Arch-specific enabling code. */
319-
ret = __cpu_up(cpu, idle_thread_get(cpu));
322+
ret = __cpu_up(cpu, idle);
320323
if (ret != 0)
321324
goto out_notify;
322325
BUG_ON(!cpu_online(cpu));

‎kernel/smp.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <linux/smp.h>
1414
#include <linux/cpu.h>
1515

16+
#include "smpboot.h"
17+
1618
#ifdef CONFIG_USE_GENERIC_SMP_HELPERS
1719
static struct {
1820
struct list_head queue;
@@ -669,6 +671,8 @@ void __init smp_init(void)
669671
{
670672
unsigned int cpu;
671673

674+
idle_threads_init();
675+
672676
/* FIXME: This should be done in userspace --RR */
673677
for_each_present_cpu(cpu) {
674678
if (num_online_cpus() >= setup_max_cpus)

‎kernel/smpboot.c‎

Lines changed: 19 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,64 +6,42 @@
66
#include <linux/init.h>
77
#include <linux/sched.h>
88
#include <linux/percpu.h>
9-
#include <linux/workqueue.h>
109

1110
#include "smpboot.h"
1211

1312
#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-
4313
/*
4414
* For the hotplug case we keep the task structs around and reuse
4515
* them.
4616
*/
4717
static DEFINE_PER_CPU(struct task_struct *, idle_threads);
4818

49-
static inline struct task_struct *get_idle_for_cpu(unsigned int cpu)
19+
struct task_struct * __cpuinit idle_thread_get(unsigned int cpu)
5020
{
5121
struct task_struct *tsk = per_cpu(idle_threads, cpu);
5222

5323
if (!tsk)
54-
return idle_thread_create(cpu);
24+
return ERR_PTR(-ENOMEM);
5525
init_idle(tsk, cpu);
5626
return tsk;
5727
}
5828

59-
struct task_struct * __cpuinit idle_thread_get(unsigned int cpu)
29+
void __init idle_thread_set_boot_cpu(void)
6030
{
61-
return per_cpu(idle_threads, cpu);
31+
per_cpu(idle_threads, smp_processor_id()) = current;
6232
}
6333

64-
void __init idle_thread_set_boot_cpu(void)
34+
static inline void idle_init(unsigned int cpu)
6535
{
66-
per_cpu(idle_threads, smp_processor_id()) = current;
36+
struct task_struct *tsk = per_cpu(idle_threads, cpu);
37+
38+
if (!tsk) {
39+
tsk = fork_idle(cpu);
40+
if (IS_ERR(tsk))
41+
pr_err("SMP: fork_idle() failed for CPU %u\n", cpu);
42+
else
43+
per_cpu(idle_threads, cpu) = tsk;
44+
}
6745
}
6846

6947
/**
@@ -72,25 +50,13 @@ void __init idle_thread_set_boot_cpu(void)
7250
*
7351
* Creates the thread if it does not exist.
7452
*/
75-
static int __cpuinit idle_thread_init(unsigned int cpu)
53+
void __init idle_threads_init(void)
7654
{
77-
struct task_struct *idle = get_idle_for_cpu(cpu);
55+
unsigned int cpu;
7856

79-
if (IS_ERR(idle)) {
80-
printk(KERN_ERR "failed fork for CPU %u\n", cpu);
81-
return PTR_ERR(idle);
57+
for_each_possible_cpu(cpu) {
58+
if (cpu != smp_processor_id())
59+
idle_init(cpu);
8260
}
83-
per_cpu(idle_threads, cpu) = idle;
84-
return 0;
8561
}
86-
#else
87-
static inline int idle_thread_init(unsigned int cpu) { return 0; }
8862
#endif
89-
90-
/**
91-
* smpboot_prepare - generic smpboot preparation
92-
*/
93-
int __cpuinit smpboot_prepare(unsigned int cpu)
94-
{
95-
return idle_thread_init(cpu);
96-
}

‎kernel/smpboot.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ int smpboot_prepare(unsigned int cpu);
88
#ifdef CONFIG_GENERIC_SMP_IDLE_THREAD
99
struct task_struct *idle_thread_get(unsigned int cpu);
1010
void idle_thread_set_boot_cpu(void);
11+
void idle_threads_init(void);
1112
#else
1213
static inline struct task_struct *idle_thread_get(unsigned int cpu) { return NULL; }
1314
static inline void idle_thread_set_boot_cpu(void) { }
15+
static inline void idle_threads_init(unsigned int cpu) { }
1416
#endif
1517

1618
#endif

0 commit comments

Comments
 (0)