aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJens Axboe <jens.axboe@oracle.com>2008-06-26 05:21:34 -0400
committerJens Axboe <jens.axboe@oracle.com>2008-06-26 05:21:34 -0400
commit3d4422332711ef48ef0f132f1fcbfcbd56c7f3d1 (patch)
tree9fd3cfa9825e8cb0b7e08dfae85cc9a722442849
parent543cf4cb3fe6f6cae3651ba918b9c56200b257d0 (diff)
Add generic helpers for arch IPI function calls
This adds kernel/smp.c which contains helpers for IPI function calls. In addition to supporting the existing smp_call_function() in a more efficient manner, it also adds a more scalable variant called smp_call_function_single() for calling a given function on a single CPU only. The core of this is based on the x86-64 patch from Nick Piggin, lots of changes since then. "Alan D. Brunelle" <Alan.Brunelle@hp.com> has contributed lots of fixes and suggestions as well. Also thanks to Paul E. McKenney <paulmck@linux.vnet.ibm.com> for reviewing RCU usage and getting rid of the data allocation fallback deadlock. Acked-by: Ingo Molnar <mingo@elte.hu> Reviewed-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com> Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
-rw-r--r--arch/Kconfig3
-rw-r--r--arch/sparc64/kernel/smp.c11
-rw-r--r--include/linux/smp.h35
-rw-r--r--init/main.c2
-rw-r--r--kernel/Makefile1
-rw-r--r--kernel/smp.c383
6 files changed, 428 insertions, 7 deletions
diff --git a/arch/Kconfig b/arch/Kconfig
index 3ea332b009e5..ad89a33d8c6e 100644
--- a/arch/Kconfig
+++ b/arch/Kconfig
@@ -39,3 +39,6 @@ config HAVE_KRETPROBES
39 39
40config HAVE_DMA_ATTRS 40config HAVE_DMA_ATTRS
41 def_bool n 41 def_bool n
42
43config USE_GENERIC_SMP_HELPERS
44 def_bool n
diff --git a/arch/sparc64/kernel/smp.c b/arch/sparc64/kernel/smp.c
index fa63c68a1819..b82d017a1744 100644
--- a/arch/sparc64/kernel/smp.c
+++ b/arch/sparc64/kernel/smp.c
@@ -816,8 +816,9 @@ extern unsigned long xcall_call_function;
816 * You must not call this function with disabled interrupts or from a 816 * You must not call this function with disabled interrupts or from a
817 * hardware interrupt handler or from a bottom half handler. 817 * hardware interrupt handler or from a bottom half handler.
818 */ 818 */
819static int smp_call_function_mask(void (*func)(void *info), void *info, 819static int sparc64_smp_call_function_mask(void (*func)(void *info), void *info,
820 int nonatomic, int wait, cpumask_t mask) 820 int nonatomic, int wait,
821 cpumask_t mask)
821{ 822{
822 struct call_data_struct data; 823 struct call_data_struct data;
823 int cpus; 824 int cpus;
@@ -855,8 +856,8 @@ out_unlock:
855int smp_call_function(void (*func)(void *info), void *info, 856int smp_call_function(void (*func)(void *info), void *info,
856 int nonatomic, int wait) 857 int nonatomic, int wait)
857{ 858{
858 return smp_call_function_mask(func, info, nonatomic, wait, 859 return sparc64_smp_call_function_mask(func, info, nonatomic, wait,
859 cpu_online_map); 860 cpu_online_map);
860} 861}
861 862
862void smp_call_function_client(int irq, struct pt_regs *regs) 863void smp_call_function_client(int irq, struct pt_regs *regs)
@@ -893,7 +894,7 @@ static void tsb_sync(void *info)
893 894
894void smp_tsb_sync(struct mm_struct *mm) 895void smp_tsb_sync(struct mm_struct *mm)
895{ 896{
896 smp_call_function_mask(tsb_sync, mm, 0, 1, mm->cpu_vm_mask); 897 sparc64_smp_call_function_mask(tsb_sync, mm, 0, 1, mm->cpu_vm_mask);
897} 898}
898 899
899extern unsigned long xcall_flush_tlb_mm; 900extern unsigned long xcall_flush_tlb_mm;
diff --git a/include/linux/smp.h b/include/linux/smp.h
index 55232ccf9cfd..eac3e062250f 100644
--- a/include/linux/smp.h
+++ b/include/linux/smp.h
@@ -7,9 +7,19 @@
7 */ 7 */
8 8
9#include <linux/errno.h> 9#include <linux/errno.h>
10#include <linux/list.h>
11#include <linux/spinlock.h>
12#include <linux/cpumask.h>
10 13
11extern void cpu_idle(void); 14extern void cpu_idle(void);
12 15
16struct call_single_data {
17 struct list_head list;
18 void (*func) (void *info);
19 void *info;
20 unsigned int flags;
21};
22
13#ifdef CONFIG_SMP 23#ifdef CONFIG_SMP
14 24
15#include <linux/preempt.h> 25#include <linux/preempt.h>
@@ -53,9 +63,28 @@ extern void smp_cpus_done(unsigned int max_cpus);
53 * Call a function on all other processors 63 * Call a function on all other processors
54 */ 64 */
55int smp_call_function(void(*func)(void *info), void *info, int retry, int wait); 65int smp_call_function(void(*func)(void *info), void *info, int retry, int wait);
56 66int smp_call_function_mask(cpumask_t mask, void(*func)(void *info), void *info,
67 int wait);
57int smp_call_function_single(int cpuid, void (*func) (void *info), void *info, 68int smp_call_function_single(int cpuid, void (*func) (void *info), void *info,
58 int retry, int wait); 69 int retry, int wait);
70void __smp_call_function_single(int cpuid, struct call_single_data *data);
71
72/*
73 * Generic and arch helpers
74 */
75#ifdef CONFIG_USE_GENERIC_SMP_HELPERS
76void generic_smp_call_function_single_interrupt(void);
77void generic_smp_call_function_interrupt(void);
78void init_call_single_data(void);
79void ipi_call_lock(void);
80void ipi_call_unlock(void);
81void ipi_call_lock_irq(void);
82void ipi_call_unlock_irq(void);
83#else
84static inline void init_call_single_data(void)
85{
86}
87#endif
59 88
60/* 89/*
61 * Call a function on all processors 90 * Call a function on all processors
@@ -112,7 +141,9 @@ static inline void smp_send_reschedule(int cpu) { }
112}) 141})
113#define smp_call_function_mask(mask, func, info, wait) \ 142#define smp_call_function_mask(mask, func, info, wait) \
114 (up_smp_call_function(func, info)) 143 (up_smp_call_function(func, info))
115 144static inline void init_call_single_data(void)
145{
146}
116#endif /* !SMP */ 147#endif /* !SMP */
117 148
118/* 149/*
diff --git a/init/main.c b/init/main.c
index f7fb20021d48..1efcccff1bdb 100644
--- a/init/main.c
+++ b/init/main.c
@@ -31,6 +31,7 @@
31#include <linux/kernel_stat.h> 31#include <linux/kernel_stat.h>
32#include <linux/start_kernel.h> 32#include <linux/start_kernel.h>
33#include <linux/security.h> 33#include <linux/security.h>
34#include <linux/smp.h>
34#include <linux/workqueue.h> 35#include <linux/workqueue.h>
35#include <linux/profile.h> 36#include <linux/profile.h>
36#include <linux/rcupdate.h> 37#include <linux/rcupdate.h>
@@ -779,6 +780,7 @@ static void __init do_pre_smp_initcalls(void)
779{ 780{
780 extern int spawn_ksoftirqd(void); 781 extern int spawn_ksoftirqd(void);
781 782
783 init_call_single_data();
782 migration_init(); 784 migration_init();
783 spawn_ksoftirqd(); 785 spawn_ksoftirqd();
784 if (!nosoftlockup) 786 if (!nosoftlockup)
diff --git a/kernel/Makefile b/kernel/Makefile
index 1c9938addb9d..9fa57976f252 100644
--- a/kernel/Makefile
+++ b/kernel/Makefile
@@ -28,6 +28,7 @@ obj-$(CONFIG_DEBUG_RT_MUTEXES) += rtmutex-debug.o
28obj-$(CONFIG_RT_MUTEX_TESTER) += rtmutex-tester.o 28obj-$(CONFIG_RT_MUTEX_TESTER) += rtmutex-tester.o
29obj-$(CONFIG_GENERIC_ISA_DMA) += dma.o 29obj-$(CONFIG_GENERIC_ISA_DMA) += dma.o
30obj-$(CONFIG_SMP) += cpu.o spinlock.o 30obj-$(CONFIG_SMP) += cpu.o spinlock.o
31obj-$(CONFIG_USE_GENERIC_SMP_HELPERS) += smp.o
31obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock.o 32obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock.o
32obj-$(CONFIG_PROVE_LOCKING) += spinlock.o 33obj-$(CONFIG_PROVE_LOCKING) += spinlock.o
33obj-$(CONFIG_UID16) += uid16.o 34obj-$(CONFIG_UID16) += uid16.o
diff --git a/kernel/smp.c b/kernel/smp.c
new file mode 100644
index 000000000000..f77b75c027ad
--- /dev/null
+++ b/kernel/smp.c
@@ -0,0 +1,383 @@
1/*
2 * Generic helpers for smp ipi calls
3 *
4 * (C) Jens Axboe <jens.axboe@oracle.com> 2008
5 *
6 */
7#include <linux/init.h>
8#include <linux/module.h>
9#include <linux/percpu.h>
10#include <linux/rcupdate.h>
11#include <linux/smp.h>
12
13static DEFINE_PER_CPU(struct call_single_queue, call_single_queue);
14static LIST_HEAD(call_function_queue);
15__cacheline_aligned_in_smp DEFINE_SPINLOCK(call_function_lock);
16
17enum {
18 CSD_FLAG_WAIT = 0x01,
19 CSD_FLAG_ALLOC = 0x02,
20};
21
22struct call_function_data {
23 struct call_single_data csd;
24 spinlock_t lock;
25 unsigned int refs;
26 cpumask_t cpumask;
27 struct rcu_head rcu_head;
28};
29
30struct call_single_queue {
31 struct list_head list;
32 spinlock_t lock;
33};
34
35void __cpuinit init_call_single_data(void)
36{
37 int i;
38
39 for_each_possible_cpu(i) {
40 struct call_single_queue *q = &per_cpu(call_single_queue, i);
41
42 spin_lock_init(&q->lock);
43 INIT_LIST_HEAD(&q->list);
44 }
45}
46
47static void csd_flag_wait(struct call_single_data *data)
48{
49 /* Wait for response */
50 do {
51 /*
52 * We need to see the flags store in the IPI handler
53 */
54 smp_mb();
55 if (!(data->flags & CSD_FLAG_WAIT))
56 break;
57 cpu_relax();
58 } while (1);
59}
60
61/*
62 * Insert a previously allocated call_single_data element for execution
63 * on the given CPU. data must already have ->func, ->info, and ->flags set.
64 */
65static void generic_exec_single(int cpu, struct call_single_data *data)
66{
67 struct call_single_queue *dst = &per_cpu(call_single_queue, cpu);
68 int wait = data->flags & CSD_FLAG_WAIT, ipi;
69 unsigned long flags;
70
71 spin_lock_irqsave(&dst->lock, flags);
72 ipi = list_empty(&dst->list);
73 list_add_tail(&data->list, &dst->list);
74 spin_unlock_irqrestore(&dst->lock, flags);
75
76 if (ipi)
77 arch_send_call_function_single_ipi(cpu);
78
79 if (wait)
80 csd_flag_wait(data);
81}
82
83static void rcu_free_call_data(struct rcu_head *head)
84{
85 struct call_function_data *data;
86
87 data = container_of(head, struct call_function_data, rcu_head);
88
89 kfree(data);
90}
91
92/*
93 * Invoked by arch to handle an IPI for call function. Must be called with
94 * interrupts disabled.
95 */
96void generic_smp_call_function_interrupt(void)
97{
98 struct call_function_data *data;
99 int cpu = get_cpu();
100
101 /*
102 * It's ok to use list_for_each_rcu() here even though we may delete
103 * 'pos', since list_del_rcu() doesn't clear ->next
104 */
105 rcu_read_lock();
106 list_for_each_entry_rcu(data, &call_function_queue, csd.list) {
107 int refs;
108
109 if (!cpu_isset(cpu, data->cpumask))
110 continue;
111
112 data->csd.func(data->csd.info);
113
114 spin_lock(&data->lock);
115 cpu_clear(cpu, data->cpumask);
116 WARN_ON(data->refs == 0);
117 data->refs--;
118 refs = data->refs;
119 spin_unlock(&data->lock);
120
121 if (refs)
122 continue;
123
124 spin_lock(&call_function_lock);
125 list_del_rcu(&data->csd.list);
126 spin_unlock(&call_function_lock);
127
128 if (data->csd.flags & CSD_FLAG_WAIT) {
129 /*
130 * serialize stores to data with the flag clear
131 * and wakeup
132 */
133 smp_wmb();
134 data->csd.flags &= ~CSD_FLAG_WAIT;
135 } else
136 call_rcu(&data->rcu_head, rcu_free_call_data);
137 }
138 rcu_read_unlock();
139
140 put_cpu();
141}
142
143/*
144 * Invoked by arch to handle an IPI for call function single. Must be called
145 * from the arch with interrupts disabled.
146 */
147void generic_smp_call_function_single_interrupt(void)
148{
149 struct call_single_queue *q = &__get_cpu_var(call_single_queue);
150 LIST_HEAD(list);
151
152 /*
153 * Need to see other stores to list head for checking whether
154 * list is empty without holding q->lock
155 */
156 smp_mb();
157 while (!list_empty(&q->list)) {
158 unsigned int data_flags;
159
160 spin_lock(&q->lock);
161 list_replace_init(&q->list, &list);
162 spin_unlock(&q->lock);
163
164 while (!list_empty(&list)) {
165 struct call_single_data *data;
166
167 data = list_entry(list.next, struct call_single_data,
168 list);
169 list_del(&data->list);
170
171 /*
172 * 'data' can be invalid after this call if
173 * flags == 0 (when called through
174 * generic_exec_single(), so save them away before
175 * making the call.
176 */
177 data_flags = data->flags;
178
179 data->func(data->info);
180
181 if (data_flags & CSD_FLAG_WAIT) {
182 smp_wmb();
183 data->flags &= ~CSD_FLAG_WAIT;
184 } else if (data_flags & CSD_FLAG_ALLOC)
185 kfree(data);
186 }
187 /*
188 * See comment on outer loop
189 */
190 smp_mb();
191 }
192}
193
194/*
195 * smp_call_function_single - Run a function on a specific CPU
196 * @func: The function to run. This must be fast and non-blocking.
197 * @info: An arbitrary pointer to pass to the function.
198 * @retry: Unused
199 * @wait: If true, wait until function has completed on other CPUs.
200 *
201 * Returns 0 on success, else a negative status code. Note that @wait
202 * will be implicitly turned on in case of allocation failures, since
203 * we fall back to on-stack allocation.
204 */
205int smp_call_function_single(int cpu, void (*func) (void *info), void *info,
206 int retry, int wait)
207{
208 struct call_single_data d;
209 unsigned long flags;
210 /* prevent preemption and reschedule on another processor */
211 int me = get_cpu();
212
213 /* Can deadlock when called with interrupts disabled */
214 WARN_ON(irqs_disabled());
215
216 if (cpu == me) {
217 local_irq_save(flags);
218 func(info);
219 local_irq_restore(flags);
220 } else {
221 struct call_single_data *data = NULL;
222
223 if (!wait) {
224 data = kmalloc(sizeof(*data), GFP_ATOMIC);
225 if (data)
226 data->flags = CSD_FLAG_ALLOC;
227 }
228 if (!data) {
229 data = &d;
230 data->flags = CSD_FLAG_WAIT;
231 }
232
233 data->func = func;
234 data->info = info;
235 generic_exec_single(cpu, data);
236 }
237
238 put_cpu();
239 return 0;
240}
241EXPORT_SYMBOL(smp_call_function_single);
242
243/**
244 * __smp_call_function_single(): Run a function on another CPU
245 * @cpu: The CPU to run on.
246 * @data: Pre-allocated and setup data structure
247 *
248 * Like smp_call_function_single(), but allow caller to pass in a pre-allocated
249 * data structure. Useful for embedding @data inside other structures, for
250 * instance.
251 *
252 */
253void __smp_call_function_single(int cpu, struct call_single_data *data)
254{
255 /* Can deadlock when called with interrupts disabled */
256 WARN_ON((data->flags & CSD_FLAG_WAIT) && irqs_disabled());
257
258 generic_exec_single(cpu, data);
259}
260
261/**
262 * smp_call_function_mask(): Run a function on a set of other CPUs.
263 * @mask: The set of cpus to run on.
264 * @func: The function to run. This must be fast and non-blocking.
265 * @info: An arbitrary pointer to pass to the function.
266 * @wait: If true, wait (atomically) until function has completed on other CPUs.
267 *
268 * Returns 0 on success, else a negative status code.
269 *
270 * If @wait is true, then returns once @func has returned. Note that @wait
271 * will be implicitly turned on in case of allocation failures, since
272 * we fall back to on-stack allocation.
273 *
274 * You must not call this function with disabled interrupts or from a
275 * hardware interrupt handler or from a bottom half handler. Preemption
276 * must be disabled when calling this function.
277 */
278int smp_call_function_mask(cpumask_t mask, void (*func)(void *), void *info,
279 int wait)
280{
281 struct call_function_data d;
282 struct call_function_data *data = NULL;
283 cpumask_t allbutself;
284 unsigned long flags;
285 int cpu, num_cpus;
286
287 /* Can deadlock when called with interrupts disabled */
288 WARN_ON(irqs_disabled());
289
290 cpu = smp_processor_id();
291 allbutself = cpu_online_map;
292 cpu_clear(cpu, allbutself);
293 cpus_and(mask, mask, allbutself);
294 num_cpus = cpus_weight(mask);
295
296 /*
297 * If zero CPUs, return. If just a single CPU, turn this request
298 * into a targetted single call instead since it's faster.
299 */
300 if (!num_cpus)
301 return 0;
302 else if (num_cpus == 1) {
303 cpu = first_cpu(mask);
304 return smp_call_function_single(cpu, func, info, 0, wait);
305 }
306
307 if (!wait) {
308 data = kmalloc(sizeof(*data), GFP_ATOMIC);
309 if (data)
310 data->csd.flags = CSD_FLAG_ALLOC;
311 }
312 if (!data) {
313 data = &d;
314 data->csd.flags = CSD_FLAG_WAIT;
315 }
316
317 spin_lock_init(&data->lock);
318 data->csd.func = func;
319 data->csd.info = info;
320 data->refs = num_cpus;
321 data->cpumask = mask;
322
323 spin_lock_irqsave(&call_function_lock, flags);
324 list_add_tail_rcu(&data->csd.list, &call_function_queue);
325 spin_unlock_irqrestore(&call_function_lock, flags);
326
327 /* Send a message to all CPUs in the map */
328 arch_send_call_function_ipi(mask);
329
330 /* optionally wait for the CPUs to complete */
331 if (wait)
332 csd_flag_wait(&data->csd);
333
334 return 0;
335}
336EXPORT_SYMBOL(smp_call_function_mask);
337
338/**
339 * smp_call_function(): Run a function on all other CPUs.
340 * @func: The function to run. This must be fast and non-blocking.
341 * @info: An arbitrary pointer to pass to the function.
342 * @natomic: Unused
343 * @wait: If true, wait (atomically) until function has completed on other CPUs.
344 *
345 * Returns 0 on success, else a negative status code.
346 *
347 * If @wait is true, then returns once @func has returned; otherwise
348 * it returns just before the target cpu calls @func. In case of allocation
349 * failure, @wait will be implicitly turned on.
350 *
351 * You must not call this function with disabled interrupts or from a
352 * hardware interrupt handler or from a bottom half handler.
353 */
354int smp_call_function(void (*func)(void *), void *info, int natomic, int wait)
355{
356 int ret;
357
358 preempt_disable();
359 ret = smp_call_function_mask(cpu_online_map, func, info, wait);
360 preempt_enable();
361 return ret;
362}
363EXPORT_SYMBOL(smp_call_function);
364
365void ipi_call_lock(void)
366{
367 spin_lock(&call_function_lock);
368}
369
370void ipi_call_unlock(void)
371{
372 spin_unlock(&call_function_lock);
373}
374
375void ipi_call_lock_irq(void)
376{
377 spin_lock_irq(&call_function_lock);
378}
379
380void ipi_call_unlock_irq(void)
381{
382 spin_unlock_irq(&call_function_lock);
383}