Blame view

kernel/smpboot.c 6.9 KB
38498a67a   Thomas Gleixner   smp: Add generic ...
1
2
3
  /*
   * Common SMP CPU bringup/teardown functions
   */
f97f8f06a   Thomas Gleixner   smpboot: Provide ...
4
  #include <linux/cpu.h>
29d5e0476   Thomas Gleixner   smp: Provide gene...
5
6
  #include <linux/err.h>
  #include <linux/smp.h>
38498a67a   Thomas Gleixner   smp: Add generic ...
7
  #include <linux/init.h>
f97f8f06a   Thomas Gleixner   smpboot: Provide ...
8
9
  #include <linux/list.h>
  #include <linux/slab.h>
29d5e0476   Thomas Gleixner   smp: Provide gene...
10
  #include <linux/sched.h>
f97f8f06a   Thomas Gleixner   smpboot: Provide ...
11
  #include <linux/export.h>
29d5e0476   Thomas Gleixner   smp: Provide gene...
12
  #include <linux/percpu.h>
f97f8f06a   Thomas Gleixner   smpboot: Provide ...
13
14
  #include <linux/kthread.h>
  #include <linux/smpboot.h>
38498a67a   Thomas Gleixner   smp: Add generic ...
15
16
  
  #include "smpboot.h"
3180d89b4   Paul E. McKenney   hotplug: Fix UP b...
17
  #ifdef CONFIG_SMP
29d5e0476   Thomas Gleixner   smp: Provide gene...
18
  #ifdef CONFIG_GENERIC_SMP_IDLE_THREAD
29d5e0476   Thomas Gleixner   smp: Provide gene...
19
20
21
22
23
  /*
   * For the hotplug case we keep the task structs around and reuse
   * them.
   */
  static DEFINE_PER_CPU(struct task_struct *, idle_threads);
3bb5d2ee3   Suresh Siddha   smp, idle: Alloca...
24
  struct task_struct * __cpuinit idle_thread_get(unsigned int cpu)
29d5e0476   Thomas Gleixner   smp: Provide gene...
25
26
27
28
  {
  	struct task_struct *tsk = per_cpu(idle_threads, cpu);
  
  	if (!tsk)
3bb5d2ee3   Suresh Siddha   smp, idle: Alloca...
29
  		return ERR_PTR(-ENOMEM);
29d5e0476   Thomas Gleixner   smp: Provide gene...
30
31
32
  	init_idle(tsk, cpu);
  	return tsk;
  }
3bb5d2ee3   Suresh Siddha   smp, idle: Alloca...
33
  void __init idle_thread_set_boot_cpu(void)
29d5e0476   Thomas Gleixner   smp: Provide gene...
34
  {
3bb5d2ee3   Suresh Siddha   smp, idle: Alloca...
35
  	per_cpu(idle_threads, smp_processor_id()) = current;
29d5e0476   Thomas Gleixner   smp: Provide gene...
36
  }
4a70d2d99   Srivatsa S. Bhat   smpboot, idle: Fi...
37
38
39
40
41
42
  /**
   * idle_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.
   */
3bb5d2ee3   Suresh Siddha   smp, idle: Alloca...
43
  static inline void idle_init(unsigned int cpu)
29d5e0476   Thomas Gleixner   smp: Provide gene...
44
  {
3bb5d2ee3   Suresh Siddha   smp, idle: Alloca...
45
46
47
48
49
50
51
52
53
54
  	struct task_struct *tsk = per_cpu(idle_threads, cpu);
  
  	if (!tsk) {
  		tsk = fork_idle(cpu);
  		if (IS_ERR(tsk))
  			pr_err("SMP: fork_idle() failed for CPU %u
  ", cpu);
  		else
  			per_cpu(idle_threads, cpu) = tsk;
  	}
29d5e0476   Thomas Gleixner   smp: Provide gene...
55
56
57
  }
  
  /**
4a70d2d99   Srivatsa S. Bhat   smpboot, idle: Fi...
58
   * idle_threads_init - Initialize idle threads for all cpus
29d5e0476   Thomas Gleixner   smp: Provide gene...
59
   */
3bb5d2ee3   Suresh Siddha   smp, idle: Alloca...
60
  void __init idle_threads_init(void)
29d5e0476   Thomas Gleixner   smp: Provide gene...
61
  {
ee74d1322   Srivatsa S. Bhat   smpboot, idle: Op...
62
63
64
  	unsigned int cpu, boot_cpu;
  
  	boot_cpu = smp_processor_id();
29d5e0476   Thomas Gleixner   smp: Provide gene...
65

3bb5d2ee3   Suresh Siddha   smp, idle: Alloca...
66
  	for_each_possible_cpu(cpu) {
ee74d1322   Srivatsa S. Bhat   smpboot, idle: Op...
67
  		if (cpu != boot_cpu)
3bb5d2ee3   Suresh Siddha   smp, idle: Alloca...
68
  			idle_init(cpu);
29d5e0476   Thomas Gleixner   smp: Provide gene...
69
  	}
29d5e0476   Thomas Gleixner   smp: Provide gene...
70
  }
29d5e0476   Thomas Gleixner   smp: Provide gene...
71
  #endif
f97f8f06a   Thomas Gleixner   smpboot: Provide ...
72

3180d89b4   Paul E. McKenney   hotplug: Fix UP b...
73
  #endif /* #ifdef CONFIG_SMP */
f97f8f06a   Thomas Gleixner   smpboot: Provide ...
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  static LIST_HEAD(hotplug_threads);
  static DEFINE_MUTEX(smpboot_threads_lock);
  
  struct smpboot_thread_data {
  	unsigned int			cpu;
  	unsigned int			status;
  	struct smp_hotplug_thread	*ht;
  };
  
  enum {
  	HP_THREAD_NONE = 0,
  	HP_THREAD_ACTIVE,
  	HP_THREAD_PARKED,
  };
  
  /**
   * smpboot_thread_fn - percpu hotplug thread loop function
   * @data:	thread data pointer
   *
   * Checks for thread stop and park conditions. Calls the necessary
   * setup, cleanup, park and unpark functions for the registered
   * thread.
   *
   * Returns 1 when the thread should exit, 0 otherwise.
   */
  static int smpboot_thread_fn(void *data)
  {
  	struct smpboot_thread_data *td = data;
  	struct smp_hotplug_thread *ht = td->ht;
  
  	while (1) {
  		set_current_state(TASK_INTERRUPTIBLE);
  		preempt_disable();
  		if (kthread_should_stop()) {
  			set_current_state(TASK_RUNNING);
  			preempt_enable();
  			if (ht->cleanup)
  				ht->cleanup(td->cpu, cpu_online(td->cpu));
  			kfree(td);
  			return 0;
  		}
  
  		if (kthread_should_park()) {
  			__set_current_state(TASK_RUNNING);
  			preempt_enable();
  			if (ht->park && td->status == HP_THREAD_ACTIVE) {
  				BUG_ON(td->cpu != smp_processor_id());
  				ht->park(td->cpu);
  				td->status = HP_THREAD_PARKED;
  			}
  			kthread_parkme();
  			/* We might have been woken for stop */
  			continue;
  		}
dc893e19b   Arnd Bergmann   Revert parts of "...
128
  		BUG_ON(td->cpu != smp_processor_id());
f97f8f06a   Thomas Gleixner   smpboot: Provide ...
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
  
  		/* Check for state change setup */
  		switch (td->status) {
  		case HP_THREAD_NONE:
  			preempt_enable();
  			if (ht->setup)
  				ht->setup(td->cpu);
  			td->status = HP_THREAD_ACTIVE;
  			preempt_disable();
  			break;
  		case HP_THREAD_PARKED:
  			preempt_enable();
  			if (ht->unpark)
  				ht->unpark(td->cpu);
  			td->status = HP_THREAD_ACTIVE;
  			preempt_disable();
  			break;
  		}
  
  		if (!ht->thread_should_run(td->cpu)) {
  			preempt_enable();
  			schedule();
  		} else {
  			set_current_state(TASK_RUNNING);
  			preempt_enable();
  			ht->thread_fn(td->cpu);
  		}
  	}
  }
  
  static int
  __smpboot_create_thread(struct smp_hotplug_thread *ht, unsigned int cpu)
  {
  	struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu);
  	struct smpboot_thread_data *td;
  
  	if (tsk)
  		return 0;
  
  	td = kzalloc_node(sizeof(*td), GFP_KERNEL, cpu_to_node(cpu));
  	if (!td)
  		return -ENOMEM;
  	td->cpu = cpu;
  	td->ht = ht;
  
  	tsk = kthread_create_on_cpu(smpboot_thread_fn, td, cpu,
  				    ht->thread_comm);
  	if (IS_ERR(tsk)) {
  		kfree(td);
  		return PTR_ERR(tsk);
  	}
f97f8f06a   Thomas Gleixner   smpboot: Provide ...
180
181
  	get_task_struct(tsk);
  	*per_cpu_ptr(ht->store, cpu) = tsk;
f2530dc71   Thomas Gleixner   kthread: Prevent ...
182
183
184
185
186
187
188
189
190
191
192
193
  	if (ht->create) {
  		/*
  		 * Make sure that the task has actually scheduled out
  		 * into park position, before calling the create
  		 * callback. At least the migration thread callback
  		 * requires that the task is off the runqueue.
  		 */
  		if (!wait_task_inactive(tsk, TASK_PARKED))
  			WARN_ON(1);
  		else
  			ht->create(cpu);
  	}
f97f8f06a   Thomas Gleixner   smpboot: Provide ...
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
  	return 0;
  }
  
  int smpboot_create_threads(unsigned int cpu)
  {
  	struct smp_hotplug_thread *cur;
  	int ret = 0;
  
  	mutex_lock(&smpboot_threads_lock);
  	list_for_each_entry(cur, &hotplug_threads, list) {
  		ret = __smpboot_create_thread(cur, cpu);
  		if (ret)
  			break;
  	}
  	mutex_unlock(&smpboot_threads_lock);
  	return ret;
  }
  
  static void smpboot_unpark_thread(struct smp_hotplug_thread *ht, unsigned int cpu)
  {
  	struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu);
46c498c2c   Thomas Gleixner   stop_machine: Mar...
215
216
  	if (ht->pre_unpark)
  		ht->pre_unpark(cpu);
f97f8f06a   Thomas Gleixner   smpboot: Provide ...
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
  	kthread_unpark(tsk);
  }
  
  void smpboot_unpark_threads(unsigned int cpu)
  {
  	struct smp_hotplug_thread *cur;
  
  	mutex_lock(&smpboot_threads_lock);
  	list_for_each_entry(cur, &hotplug_threads, list)
  		smpboot_unpark_thread(cur, cpu);
  	mutex_unlock(&smpboot_threads_lock);
  }
  
  static void smpboot_park_thread(struct smp_hotplug_thread *ht, unsigned int cpu)
  {
  	struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu);
7d7e499f7   Thomas Gleixner   smpboot: Allow se...
233
  	if (tsk && !ht->selfparking)
f97f8f06a   Thomas Gleixner   smpboot: Provide ...
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
  		kthread_park(tsk);
  }
  
  void smpboot_park_threads(unsigned int cpu)
  {
  	struct smp_hotplug_thread *cur;
  
  	mutex_lock(&smpboot_threads_lock);
  	list_for_each_entry_reverse(cur, &hotplug_threads, list)
  		smpboot_park_thread(cur, cpu);
  	mutex_unlock(&smpboot_threads_lock);
  }
  
  static void smpboot_destroy_threads(struct smp_hotplug_thread *ht)
  {
  	unsigned int cpu;
  
  	/* We need to destroy also the parked threads of offline cpus */
  	for_each_possible_cpu(cpu) {
  		struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu);
  
  		if (tsk) {
  			kthread_stop(tsk);
  			put_task_struct(tsk);
  			*per_cpu_ptr(ht->store, cpu) = NULL;
  		}
  	}
  }
  
  /**
   * smpboot_register_percpu_thread - Register a per_cpu thread related to hotplug
   * @plug_thread:	Hotplug thread descriptor
   *
   * Creates and starts the threads on all online cpus.
   */
  int smpboot_register_percpu_thread(struct smp_hotplug_thread *plug_thread)
  {
  	unsigned int cpu;
  	int ret = 0;
  
  	mutex_lock(&smpboot_threads_lock);
  	for_each_online_cpu(cpu) {
  		ret = __smpboot_create_thread(plug_thread, cpu);
  		if (ret) {
  			smpboot_destroy_threads(plug_thread);
  			goto out;
  		}
  		smpboot_unpark_thread(plug_thread, cpu);
  	}
  	list_add(&plug_thread->list, &hotplug_threads);
  out:
  	mutex_unlock(&smpboot_threads_lock);
  	return ret;
  }
  EXPORT_SYMBOL_GPL(smpboot_register_percpu_thread);
  
  /**
   * smpboot_unregister_percpu_thread - Unregister a per_cpu thread related to hotplug
   * @plug_thread:	Hotplug thread descriptor
   *
   * Stops all threads on all possible cpus.
   */
  void smpboot_unregister_percpu_thread(struct smp_hotplug_thread *plug_thread)
  {
  	get_online_cpus();
  	mutex_lock(&smpboot_threads_lock);
  	list_del(&plug_thread->list);
  	smpboot_destroy_threads(plug_thread);
  	mutex_unlock(&smpboot_threads_lock);
  	put_online_cpus();
  }
  EXPORT_SYMBOL_GPL(smpboot_unregister_percpu_thread);