blob: 450d67d394b0fca3f99c3a9af233c46cde74f82e [file] [log] [blame]
Steffen Klassert16295be2010-01-06 19:47:10 +11001/*
2 * padata.c - generic interface to process data streams in parallel
3 *
4 * Copyright (C) 2008, 2009 secunet Security Networks AG
5 * Copyright (C) 2008, 2009 Steffen Klassert <steffen.klassert@secunet.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21#include <linux/module.h>
22#include <linux/cpumask.h>
23#include <linux/err.h>
24#include <linux/cpu.h>
25#include <linux/padata.h>
26#include <linux/mutex.h>
27#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Steffen Klassert16295be2010-01-06 19:47:10 +110029#include <linux/rcupdate.h>
30
Dan Carpenter749d8112010-06-03 20:19:28 +100031#define MAX_SEQ_NR (INT_MAX - NR_CPUS)
Steffen Klassert97e3d942010-04-29 14:37:32 +020032#define MAX_OBJ_NUM 1000
Steffen Klassert16295be2010-01-06 19:47:10 +110033
34static int padata_index_to_cpu(struct parallel_data *pd, int cpu_index)
35{
36 int cpu, target_cpu;
37
38 target_cpu = cpumask_first(pd->cpumask);
39 for (cpu = 0; cpu < cpu_index; cpu++)
40 target_cpu = cpumask_next(target_cpu, pd->cpumask);
41
42 return target_cpu;
43}
44
45static int padata_cpu_hash(struct padata_priv *padata)
46{
47 int cpu_index;
48 struct parallel_data *pd;
49
50 pd = padata->pd;
51
52 /*
53 * Hash the sequence numbers to the cpus by taking
54 * seq_nr mod. number of cpus in use.
55 */
56 cpu_index = padata->seq_nr % cpumask_weight(pd->cpumask);
57
58 return padata_index_to_cpu(pd, cpu_index);
59}
60
61static void padata_parallel_worker(struct work_struct *work)
62{
63 struct padata_queue *queue;
64 struct parallel_data *pd;
65 struct padata_instance *pinst;
66 LIST_HEAD(local_list);
67
68 local_bh_disable();
69 queue = container_of(work, struct padata_queue, pwork);
70 pd = queue->pd;
71 pinst = pd->pinst;
72
73 spin_lock(&queue->parallel.lock);
74 list_replace_init(&queue->parallel.list, &local_list);
75 spin_unlock(&queue->parallel.lock);
76
77 while (!list_empty(&local_list)) {
78 struct padata_priv *padata;
79
80 padata = list_entry(local_list.next,
81 struct padata_priv, list);
82
83 list_del_init(&padata->list);
84
85 padata->parallel(padata);
86 }
87
88 local_bh_enable();
89}
90
Steffen Klassert0198ffd2010-05-19 13:44:27 +100091/**
Steffen Klassert16295be2010-01-06 19:47:10 +110092 * padata_do_parallel - padata parallelization function
93 *
94 * @pinst: padata instance
95 * @padata: object to be parallelized
96 * @cb_cpu: cpu the serialization callback function will run on,
97 * must be in the cpumask of padata.
98 *
99 * The parallelization callback function will run with BHs off.
100 * Note: Every object which is parallelized by padata_do_parallel
101 * must be seen by padata_do_serial.
102 */
103int padata_do_parallel(struct padata_instance *pinst,
104 struct padata_priv *padata, int cb_cpu)
105{
106 int target_cpu, err;
107 struct padata_queue *queue;
108 struct parallel_data *pd;
109
110 rcu_read_lock_bh();
111
112 pd = rcu_dereference(pinst->pd);
113
Steffen Klassert83f619f2010-07-07 15:32:02 +0200114 err = -EINVAL;
Steffen Klassert16295be2010-01-06 19:47:10 +1100115 if (!(pinst->flags & PADATA_INIT))
116 goto out;
117
Steffen Klassert83f619f2010-07-07 15:32:02 +0200118 if (!cpumask_test_cpu(cb_cpu, pd->cpumask))
119 goto out;
120
Steffen Klassert16295be2010-01-06 19:47:10 +1100121 err = -EBUSY;
122 if ((pinst->flags & PADATA_RESET))
123 goto out;
124
125 if (atomic_read(&pd->refcnt) >= MAX_OBJ_NUM)
126 goto out;
127
Steffen Klassert83f619f2010-07-07 15:32:02 +0200128 err = 0;
Steffen Klassert16295be2010-01-06 19:47:10 +1100129 atomic_inc(&pd->refcnt);
130 padata->pd = pd;
131 padata->cb_cpu = cb_cpu;
132
133 if (unlikely(atomic_read(&pd->seq_nr) == pd->max_seq_nr))
134 atomic_set(&pd->seq_nr, -1);
135
136 padata->seq_nr = atomic_inc_return(&pd->seq_nr);
137
138 target_cpu = padata_cpu_hash(padata);
139 queue = per_cpu_ptr(pd->queue, target_cpu);
140
141 spin_lock(&queue->parallel.lock);
142 list_add_tail(&padata->list, &queue->parallel.list);
143 spin_unlock(&queue->parallel.lock);
144
145 queue_work_on(target_cpu, pinst->wq, &queue->pwork);
146
147out:
148 rcu_read_unlock_bh();
149
150 return err;
151}
152EXPORT_SYMBOL(padata_do_parallel);
153
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000154/*
155 * padata_get_next - Get the next object that needs serialization.
156 *
157 * Return values are:
158 *
159 * A pointer to the control struct of the next object that needs
160 * serialization, if present in one of the percpu reorder queues.
161 *
162 * NULL, if all percpu reorder queues are empty.
163 *
164 * -EINPROGRESS, if the next object that needs serialization will
165 * be parallel processed by another cpu and is not yet present in
166 * the cpu's reorder queue.
167 *
168 * -ENODATA, if this cpu has to do the parallel processing for
169 * the next object.
170 */
Steffen Klassert16295be2010-01-06 19:47:10 +1100171static struct padata_priv *padata_get_next(struct parallel_data *pd)
172{
Steffen Klassert5f1a8c12010-07-07 15:32:39 +0200173 int cpu, num_cpus;
174 int next_nr, next_index;
Steffen Klassert16295be2010-01-06 19:47:10 +1100175 struct padata_queue *queue, *next_queue;
176 struct padata_priv *padata;
177 struct padata_list *reorder;
178
Steffen Klassert16295be2010-01-06 19:47:10 +1100179 num_cpus = cpumask_weight(pd->cpumask);
180
Steffen Klassert5f1a8c12010-07-07 15:32:39 +0200181 /*
182 * Calculate the percpu reorder queue and the sequence
183 * number of the next object.
184 */
185 next_nr = pd->processed;
186 next_index = next_nr % num_cpus;
187 cpu = padata_index_to_cpu(pd, next_index);
188 next_queue = per_cpu_ptr(pd->queue, cpu);
Steffen Klassert16295be2010-01-06 19:47:10 +1100189
Steffen Klassert5f1a8c12010-07-07 15:32:39 +0200190 if (unlikely(next_nr > pd->max_seq_nr)) {
191 next_nr = next_nr - pd->max_seq_nr - 1;
192 next_index = next_nr % num_cpus;
193 cpu = padata_index_to_cpu(pd, next_index);
194 next_queue = per_cpu_ptr(pd->queue, cpu);
195 pd->processed = 0;
Steffen Klassert16295be2010-01-06 19:47:10 +1100196 }
197
198 padata = NULL;
199
Steffen Klassert16295be2010-01-06 19:47:10 +1100200 reorder = &next_queue->reorder;
201
202 if (!list_empty(&reorder->list)) {
203 padata = list_entry(reorder->list.next,
204 struct padata_priv, list);
205
Steffen Klassert5f1a8c12010-07-07 15:32:39 +0200206 BUG_ON(next_nr != padata->seq_nr);
Steffen Klassert16295be2010-01-06 19:47:10 +1100207
208 spin_lock(&reorder->lock);
209 list_del_init(&padata->list);
210 atomic_dec(&pd->reorder_objects);
211 spin_unlock(&reorder->lock);
212
Steffen Klassert5f1a8c12010-07-07 15:32:39 +0200213 pd->processed++;
Steffen Klassert16295be2010-01-06 19:47:10 +1100214
215 goto out;
216 }
217
Steffen Klassertd46a5ac2010-05-19 13:43:14 +1000218 queue = per_cpu_ptr(pd->queue, smp_processor_id());
219 if (queue->cpu_index == next_queue->cpu_index) {
Steffen Klassert16295be2010-01-06 19:47:10 +1100220 padata = ERR_PTR(-ENODATA);
221 goto out;
222 }
223
224 padata = ERR_PTR(-EINPROGRESS);
225out:
226 return padata;
227}
228
229static void padata_reorder(struct parallel_data *pd)
230{
231 struct padata_priv *padata;
232 struct padata_queue *queue;
233 struct padata_instance *pinst = pd->pinst;
234
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000235 /*
236 * We need to ensure that only one cpu can work on dequeueing of
237 * the reorder queue the time. Calculating in which percpu reorder
238 * queue the next object will arrive takes some time. A spinlock
239 * would be highly contended. Also it is not clear in which order
240 * the objects arrive to the reorder queues. So a cpu could wait to
241 * get the lock just to notice that there is nothing to do at the
242 * moment. Therefore we use a trylock and let the holder of the lock
243 * care for all the objects enqueued during the holdtime of the lock.
244 */
Steffen Klassert16295be2010-01-06 19:47:10 +1100245 if (!spin_trylock_bh(&pd->lock))
Steffen Klassertd46a5ac2010-05-19 13:43:14 +1000246 return;
Steffen Klassert16295be2010-01-06 19:47:10 +1100247
248 while (1) {
249 padata = padata_get_next(pd);
250
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000251 /*
252 * All reorder queues are empty, or the next object that needs
253 * serialization is parallel processed by another cpu and is
254 * still on it's way to the cpu's reorder queue, nothing to
255 * do for now.
256 */
Steffen Klassert16295be2010-01-06 19:47:10 +1100257 if (!padata || PTR_ERR(padata) == -EINPROGRESS)
258 break;
259
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000260 /*
261 * This cpu has to do the parallel processing of the next
262 * object. It's waiting in the cpu's parallelization queue,
263 * so exit imediately.
264 */
Steffen Klassert16295be2010-01-06 19:47:10 +1100265 if (PTR_ERR(padata) == -ENODATA) {
Steffen Klassertd46a5ac2010-05-19 13:43:14 +1000266 del_timer(&pd->timer);
Steffen Klassert16295be2010-01-06 19:47:10 +1100267 spin_unlock_bh(&pd->lock);
Steffen Klassertd46a5ac2010-05-19 13:43:14 +1000268 return;
Steffen Klassert16295be2010-01-06 19:47:10 +1100269 }
270
271 queue = per_cpu_ptr(pd->queue, padata->cb_cpu);
272
273 spin_lock(&queue->serial.lock);
274 list_add_tail(&padata->list, &queue->serial.list);
275 spin_unlock(&queue->serial.lock);
276
277 queue_work_on(padata->cb_cpu, pinst->wq, &queue->swork);
278 }
279
280 spin_unlock_bh(&pd->lock);
281
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000282 /*
283 * The next object that needs serialization might have arrived to
284 * the reorder queues in the meantime, we will be called again
285 * from the timer function if noone else cares for it.
286 */
Steffen Klassertd46a5ac2010-05-19 13:43:14 +1000287 if (atomic_read(&pd->reorder_objects)
288 && !(pinst->flags & PADATA_RESET))
289 mod_timer(&pd->timer, jiffies + HZ);
290 else
291 del_timer(&pd->timer);
Steffen Klassert16295be2010-01-06 19:47:10 +1100292
Steffen Klassert16295be2010-01-06 19:47:10 +1100293 return;
294}
295
Steffen Klassertd46a5ac2010-05-19 13:43:14 +1000296static void padata_reorder_timer(unsigned long arg)
297{
298 struct parallel_data *pd = (struct parallel_data *)arg;
299
300 padata_reorder(pd);
301}
302
Steffen Klassert16295be2010-01-06 19:47:10 +1100303static void padata_serial_worker(struct work_struct *work)
304{
305 struct padata_queue *queue;
306 struct parallel_data *pd;
307 LIST_HEAD(local_list);
308
309 local_bh_disable();
310 queue = container_of(work, struct padata_queue, swork);
311 pd = queue->pd;
312
313 spin_lock(&queue->serial.lock);
314 list_replace_init(&queue->serial.list, &local_list);
315 spin_unlock(&queue->serial.lock);
316
317 while (!list_empty(&local_list)) {
318 struct padata_priv *padata;
319
320 padata = list_entry(local_list.next,
321 struct padata_priv, list);
322
323 list_del_init(&padata->list);
324
325 padata->serial(padata);
326 atomic_dec(&pd->refcnt);
327 }
328 local_bh_enable();
329}
330
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000331/**
Steffen Klassert16295be2010-01-06 19:47:10 +1100332 * padata_do_serial - padata serialization function
333 *
334 * @padata: object to be serialized.
335 *
336 * padata_do_serial must be called for every parallelized object.
337 * The serialization callback function will run with BHs off.
338 */
339void padata_do_serial(struct padata_priv *padata)
340{
341 int cpu;
342 struct padata_queue *queue;
343 struct parallel_data *pd;
344
345 pd = padata->pd;
346
347 cpu = get_cpu();
348 queue = per_cpu_ptr(pd->queue, cpu);
349
350 spin_lock(&queue->reorder.lock);
351 atomic_inc(&pd->reorder_objects);
352 list_add_tail(&padata->list, &queue->reorder.list);
353 spin_unlock(&queue->reorder.lock);
354
355 put_cpu();
356
357 padata_reorder(pd);
358}
359EXPORT_SYMBOL(padata_do_serial);
360
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000361/* Allocate and initialize the internal cpumask dependend resources. */
Steffen Klassert16295be2010-01-06 19:47:10 +1100362static struct parallel_data *padata_alloc_pd(struct padata_instance *pinst,
363 const struct cpumask *cpumask)
364{
365 int cpu, cpu_index, num_cpus;
366 struct padata_queue *queue;
367 struct parallel_data *pd;
368
369 cpu_index = 0;
370
371 pd = kzalloc(sizeof(struct parallel_data), GFP_KERNEL);
372 if (!pd)
373 goto err;
374
375 pd->queue = alloc_percpu(struct padata_queue);
376 if (!pd->queue)
377 goto err_free_pd;
378
379 if (!alloc_cpumask_var(&pd->cpumask, GFP_KERNEL))
380 goto err_free_queue;
381
Steffen Klassert7b389b22010-04-29 14:41:36 +0200382 cpumask_and(pd->cpumask, cpumask, cpu_active_mask);
383
384 for_each_cpu(cpu, pd->cpumask) {
Steffen Klassert16295be2010-01-06 19:47:10 +1100385 queue = per_cpu_ptr(pd->queue, cpu);
386
387 queue->pd = pd;
388
Steffen Klassert7b389b22010-04-29 14:41:36 +0200389 queue->cpu_index = cpu_index;
390 cpu_index++;
Steffen Klassert16295be2010-01-06 19:47:10 +1100391
392 INIT_LIST_HEAD(&queue->reorder.list);
393 INIT_LIST_HEAD(&queue->parallel.list);
394 INIT_LIST_HEAD(&queue->serial.list);
395 spin_lock_init(&queue->reorder.lock);
396 spin_lock_init(&queue->parallel.lock);
397 spin_lock_init(&queue->serial.lock);
398
399 INIT_WORK(&queue->pwork, padata_parallel_worker);
400 INIT_WORK(&queue->swork, padata_serial_worker);
Steffen Klassert16295be2010-01-06 19:47:10 +1100401 }
402
Steffen Klassert16295be2010-01-06 19:47:10 +1100403 num_cpus = cpumask_weight(pd->cpumask);
404 pd->max_seq_nr = (MAX_SEQ_NR / num_cpus) * num_cpus - 1;
405
Steffen Klassertd46a5ac2010-05-19 13:43:14 +1000406 setup_timer(&pd->timer, padata_reorder_timer, (unsigned long)pd);
Steffen Klassert16295be2010-01-06 19:47:10 +1100407 atomic_set(&pd->seq_nr, -1);
408 atomic_set(&pd->reorder_objects, 0);
409 atomic_set(&pd->refcnt, 0);
410 pd->pinst = pinst;
411 spin_lock_init(&pd->lock);
412
413 return pd;
414
415err_free_queue:
416 free_percpu(pd->queue);
417err_free_pd:
418 kfree(pd);
419err:
420 return NULL;
421}
422
423static void padata_free_pd(struct parallel_data *pd)
424{
425 free_cpumask_var(pd->cpumask);
426 free_percpu(pd->queue);
427 kfree(pd);
428}
429
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000430/* Flush all objects out of the padata queues. */
Steffen Klassert2b73b072010-05-19 13:43:46 +1000431static void padata_flush_queues(struct parallel_data *pd)
432{
433 int cpu;
434 struct padata_queue *queue;
435
436 for_each_cpu(cpu, pd->cpumask) {
437 queue = per_cpu_ptr(pd->queue, cpu);
438 flush_work(&queue->pwork);
439 }
440
441 del_timer_sync(&pd->timer);
442
443 if (atomic_read(&pd->reorder_objects))
444 padata_reorder(pd);
445
446 for_each_cpu(cpu, pd->cpumask) {
447 queue = per_cpu_ptr(pd->queue, cpu);
448 flush_work(&queue->swork);
449 }
450
451 BUG_ON(atomic_read(&pd->refcnt) != 0);
452}
453
Steffen Klassert4c879172010-07-07 15:30:10 +0200454static void __padata_start(struct padata_instance *pinst)
455{
456 pinst->flags |= PADATA_INIT;
457}
458
Steffen Klassertee836552010-07-07 15:30:47 +0200459static void __padata_stop(struct padata_instance *pinst)
460{
461 if (!(pinst->flags & PADATA_INIT))
462 return;
463
464 pinst->flags &= ~PADATA_INIT;
465
466 synchronize_rcu();
467
468 get_online_cpus();
469 padata_flush_queues(pinst->pd);
470 put_online_cpus();
471}
472
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000473/* Replace the internal control stucture with a new one. */
Steffen Klassert16295be2010-01-06 19:47:10 +1100474static void padata_replace(struct padata_instance *pinst,
475 struct parallel_data *pd_new)
476{
477 struct parallel_data *pd_old = pinst->pd;
478
479 pinst->flags |= PADATA_RESET;
480
481 rcu_assign_pointer(pinst->pd, pd_new);
482
483 synchronize_rcu();
484
Steffen Klassert33e54452010-07-07 15:31:26 +0200485 if (pd_old) {
486 padata_flush_queues(pd_old);
487 padata_free_pd(pd_old);
488 }
Steffen Klassert16295be2010-01-06 19:47:10 +1100489
490 pinst->flags &= ~PADATA_RESET;
491}
492
Steffen Klassert33e54452010-07-07 15:31:26 +0200493/* If cpumask contains no active cpu, we mark the instance as invalid. */
494static bool padata_validate_cpumask(struct padata_instance *pinst,
495 const struct cpumask *cpumask)
496{
497 if (!cpumask_intersects(cpumask, cpu_active_mask)) {
498 pinst->flags |= PADATA_INVALID;
499 return false;
500 }
501
502 pinst->flags &= ~PADATA_INVALID;
503 return true;
504}
505
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000506/**
Steffen Klassert16295be2010-01-06 19:47:10 +1100507 * padata_set_cpumask - set the cpumask that padata should use
508 *
509 * @pinst: padata instance
510 * @cpumask: the cpumask to use
511 */
512int padata_set_cpumask(struct padata_instance *pinst,
513 cpumask_var_t cpumask)
514{
Steffen Klassert33e54452010-07-07 15:31:26 +0200515 int valid;
Steffen Klassert16295be2010-01-06 19:47:10 +1100516 int err = 0;
Steffen Klassert33e54452010-07-07 15:31:26 +0200517 struct parallel_data *pd = NULL;
Steffen Klassert16295be2010-01-06 19:47:10 +1100518
Steffen Klassert16295be2010-01-06 19:47:10 +1100519 mutex_lock(&pinst->lock);
520
Steffen Klassert33e54452010-07-07 15:31:26 +0200521 valid = padata_validate_cpumask(pinst, cpumask);
522 if (!valid) {
523 __padata_stop(pinst);
524 goto out_replace;
525 }
526
Steffen Klassert6751fb32010-04-29 14:42:30 +0200527 get_online_cpus();
528
Steffen Klassert16295be2010-01-06 19:47:10 +1100529 pd = padata_alloc_pd(pinst, cpumask);
530 if (!pd) {
531 err = -ENOMEM;
532 goto out;
533 }
534
Steffen Klassert33e54452010-07-07 15:31:26 +0200535out_replace:
Steffen Klassert16295be2010-01-06 19:47:10 +1100536 cpumask_copy(pinst->cpumask, cpumask);
537
538 padata_replace(pinst, pd);
539
Steffen Klassert33e54452010-07-07 15:31:26 +0200540 if (valid)
541 __padata_start(pinst);
542
Steffen Klassert16295be2010-01-06 19:47:10 +1100543out:
Steffen Klassert6751fb32010-04-29 14:42:30 +0200544 put_online_cpus();
545
Steffen Klassert16295be2010-01-06 19:47:10 +1100546 mutex_unlock(&pinst->lock);
547
548 return err;
549}
550EXPORT_SYMBOL(padata_set_cpumask);
551
552static int __padata_add_cpu(struct padata_instance *pinst, int cpu)
553{
554 struct parallel_data *pd;
555
556 if (cpumask_test_cpu(cpu, cpu_active_mask)) {
557 pd = padata_alloc_pd(pinst, pinst->cpumask);
558 if (!pd)
559 return -ENOMEM;
560
561 padata_replace(pinst, pd);
Steffen Klassert33e54452010-07-07 15:31:26 +0200562
563 if (padata_validate_cpumask(pinst, pinst->cpumask))
564 __padata_start(pinst);
Steffen Klassert16295be2010-01-06 19:47:10 +1100565 }
566
567 return 0;
568}
569
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000570/**
Steffen Klassert16295be2010-01-06 19:47:10 +1100571 * padata_add_cpu - add a cpu to the padata cpumask
572 *
573 * @pinst: padata instance
574 * @cpu: cpu to add
575 */
576int padata_add_cpu(struct padata_instance *pinst, int cpu)
577{
578 int err;
579
Steffen Klassert16295be2010-01-06 19:47:10 +1100580 mutex_lock(&pinst->lock);
581
Steffen Klassert6751fb32010-04-29 14:42:30 +0200582 get_online_cpus();
Steffen Klassert16295be2010-01-06 19:47:10 +1100583 cpumask_set_cpu(cpu, pinst->cpumask);
584 err = __padata_add_cpu(pinst, cpu);
Steffen Klassert6751fb32010-04-29 14:42:30 +0200585 put_online_cpus();
Steffen Klassert16295be2010-01-06 19:47:10 +1100586
587 mutex_unlock(&pinst->lock);
588
589 return err;
590}
591EXPORT_SYMBOL(padata_add_cpu);
592
593static int __padata_remove_cpu(struct padata_instance *pinst, int cpu)
594{
Steffen Klassert33e54452010-07-07 15:31:26 +0200595 struct parallel_data *pd = NULL;
Steffen Klassert16295be2010-01-06 19:47:10 +1100596
597 if (cpumask_test_cpu(cpu, cpu_online_mask)) {
Steffen Klassert33e54452010-07-07 15:31:26 +0200598
599 if (!padata_validate_cpumask(pinst, pinst->cpumask)) {
600 __padata_stop(pinst);
601 padata_replace(pinst, pd);
602 goto out;
603 }
604
Steffen Klassert16295be2010-01-06 19:47:10 +1100605 pd = padata_alloc_pd(pinst, pinst->cpumask);
606 if (!pd)
607 return -ENOMEM;
608
609 padata_replace(pinst, pd);
610 }
611
Steffen Klassert33e54452010-07-07 15:31:26 +0200612out:
Steffen Klassert16295be2010-01-06 19:47:10 +1100613 return 0;
614}
615
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000616/**
Steffen Klassert16295be2010-01-06 19:47:10 +1100617 * padata_remove_cpu - remove a cpu from the padata cpumask
618 *
619 * @pinst: padata instance
620 * @cpu: cpu to remove
621 */
622int padata_remove_cpu(struct padata_instance *pinst, int cpu)
623{
624 int err;
625
Steffen Klassert16295be2010-01-06 19:47:10 +1100626 mutex_lock(&pinst->lock);
627
Steffen Klassert6751fb32010-04-29 14:42:30 +0200628 get_online_cpus();
Steffen Klassert16295be2010-01-06 19:47:10 +1100629 cpumask_clear_cpu(cpu, pinst->cpumask);
630 err = __padata_remove_cpu(pinst, cpu);
Steffen Klassert6751fb32010-04-29 14:42:30 +0200631 put_online_cpus();
Steffen Klassert16295be2010-01-06 19:47:10 +1100632
633 mutex_unlock(&pinst->lock);
634
635 return err;
636}
637EXPORT_SYMBOL(padata_remove_cpu);
638
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000639/**
Steffen Klassert16295be2010-01-06 19:47:10 +1100640 * padata_start - start the parallel processing
641 *
642 * @pinst: padata instance to start
643 */
Steffen Klassert4c879172010-07-07 15:30:10 +0200644int padata_start(struct padata_instance *pinst)
Steffen Klassert16295be2010-01-06 19:47:10 +1100645{
Steffen Klassert4c879172010-07-07 15:30:10 +0200646 int err = 0;
647
Steffen Klassert16295be2010-01-06 19:47:10 +1100648 mutex_lock(&pinst->lock);
Steffen Klassert4c879172010-07-07 15:30:10 +0200649
650 if (pinst->flags & PADATA_INVALID)
651 err =-EINVAL;
652
653 __padata_start(pinst);
654
Steffen Klassert16295be2010-01-06 19:47:10 +1100655 mutex_unlock(&pinst->lock);
Steffen Klassert4c879172010-07-07 15:30:10 +0200656
657 return err;
Steffen Klassert16295be2010-01-06 19:47:10 +1100658}
659EXPORT_SYMBOL(padata_start);
660
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000661/**
Steffen Klassert16295be2010-01-06 19:47:10 +1100662 * padata_stop - stop the parallel processing
663 *
664 * @pinst: padata instance to stop
665 */
666void padata_stop(struct padata_instance *pinst)
667{
Steffen Klassert16295be2010-01-06 19:47:10 +1100668 mutex_lock(&pinst->lock);
Steffen Klassertee836552010-07-07 15:30:47 +0200669 __padata_stop(pinst);
Steffen Klassert16295be2010-01-06 19:47:10 +1100670 mutex_unlock(&pinst->lock);
671}
672EXPORT_SYMBOL(padata_stop);
673
Steffen Klasserte2cb2f12010-04-29 14:40:10 +0200674#ifdef CONFIG_HOTPLUG_CPU
Henrik Kretzschmar975d2602010-03-29 16:15:31 +0800675static int padata_cpu_callback(struct notifier_block *nfb,
676 unsigned long action, void *hcpu)
Steffen Klassert16295be2010-01-06 19:47:10 +1100677{
678 int err;
679 struct padata_instance *pinst;
680 int cpu = (unsigned long)hcpu;
681
682 pinst = container_of(nfb, struct padata_instance, cpu_notifier);
683
684 switch (action) {
685 case CPU_ONLINE:
686 case CPU_ONLINE_FROZEN:
687 if (!cpumask_test_cpu(cpu, pinst->cpumask))
688 break;
689 mutex_lock(&pinst->lock);
690 err = __padata_add_cpu(pinst, cpu);
691 mutex_unlock(&pinst->lock);
692 if (err)
693 return NOTIFY_BAD;
694 break;
695
696 case CPU_DOWN_PREPARE:
697 case CPU_DOWN_PREPARE_FROZEN:
698 if (!cpumask_test_cpu(cpu, pinst->cpumask))
699 break;
700 mutex_lock(&pinst->lock);
701 err = __padata_remove_cpu(pinst, cpu);
702 mutex_unlock(&pinst->lock);
703 if (err)
704 return NOTIFY_BAD;
705 break;
706
707 case CPU_UP_CANCELED:
708 case CPU_UP_CANCELED_FROZEN:
709 if (!cpumask_test_cpu(cpu, pinst->cpumask))
710 break;
711 mutex_lock(&pinst->lock);
712 __padata_remove_cpu(pinst, cpu);
713 mutex_unlock(&pinst->lock);
714
715 case CPU_DOWN_FAILED:
716 case CPU_DOWN_FAILED_FROZEN:
717 if (!cpumask_test_cpu(cpu, pinst->cpumask))
718 break;
719 mutex_lock(&pinst->lock);
720 __padata_add_cpu(pinst, cpu);
721 mutex_unlock(&pinst->lock);
722 }
723
724 return NOTIFY_OK;
725}
Steffen Klasserte2cb2f12010-04-29 14:40:10 +0200726#endif
Steffen Klassert16295be2010-01-06 19:47:10 +1100727
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000728/**
Steffen Klassert16295be2010-01-06 19:47:10 +1100729 * padata_alloc - allocate and initialize a padata instance
730 *
731 * @cpumask: cpumask that padata uses for parallelization
732 * @wq: workqueue to use for the allocated padata instance
733 */
734struct padata_instance *padata_alloc(const struct cpumask *cpumask,
735 struct workqueue_struct *wq)
736{
Steffen Klassert16295be2010-01-06 19:47:10 +1100737 struct padata_instance *pinst;
Steffen Klassert33e54452010-07-07 15:31:26 +0200738 struct parallel_data *pd = NULL;
Steffen Klassert16295be2010-01-06 19:47:10 +1100739
740 pinst = kzalloc(sizeof(struct padata_instance), GFP_KERNEL);
741 if (!pinst)
742 goto err;
743
Steffen Klassert6751fb32010-04-29 14:42:30 +0200744 get_online_cpus();
745
Steffen Klassert33e54452010-07-07 15:31:26 +0200746 if (!alloc_cpumask_var(&pinst->cpumask, GFP_KERNEL))
Steffen Klassert16295be2010-01-06 19:47:10 +1100747 goto err_free_inst;
748
Steffen Klassert33e54452010-07-07 15:31:26 +0200749 if (padata_validate_cpumask(pinst, cpumask)) {
750 pd = padata_alloc_pd(pinst, cpumask);
751 if (!pd)
752 goto err_free_mask;
753 }
Steffen Klassert74781382010-03-04 13:30:22 +0800754
Steffen Klassert16295be2010-01-06 19:47:10 +1100755 rcu_assign_pointer(pinst->pd, pd);
756
757 pinst->wq = wq;
758
759 cpumask_copy(pinst->cpumask, cpumask);
760
761 pinst->flags = 0;
762
Steffen Klasserte2cb2f12010-04-29 14:40:10 +0200763#ifdef CONFIG_HOTPLUG_CPU
Steffen Klassert16295be2010-01-06 19:47:10 +1100764 pinst->cpu_notifier.notifier_call = padata_cpu_callback;
765 pinst->cpu_notifier.priority = 0;
Steffen Klasserte2cb2f12010-04-29 14:40:10 +0200766 register_hotcpu_notifier(&pinst->cpu_notifier);
767#endif
Steffen Klassert16295be2010-01-06 19:47:10 +1100768
Steffen Klassert6751fb32010-04-29 14:42:30 +0200769 put_online_cpus();
770
Steffen Klassert16295be2010-01-06 19:47:10 +1100771 mutex_init(&pinst->lock);
772
773 return pinst;
774
Steffen Klassert33e54452010-07-07 15:31:26 +0200775err_free_mask:
776 free_cpumask_var(pinst->cpumask);
Steffen Klassert16295be2010-01-06 19:47:10 +1100777err_free_inst:
778 kfree(pinst);
Steffen Klassert6751fb32010-04-29 14:42:30 +0200779 put_online_cpus();
Steffen Klassert16295be2010-01-06 19:47:10 +1100780err:
781 return NULL;
782}
783EXPORT_SYMBOL(padata_alloc);
784
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000785/**
Steffen Klassert16295be2010-01-06 19:47:10 +1100786 * padata_free - free a padata instance
787 *
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000788 * @padata_inst: padata instance to free
Steffen Klassert16295be2010-01-06 19:47:10 +1100789 */
790void padata_free(struct padata_instance *pinst)
791{
Steffen Klasserte2cb2f12010-04-29 14:40:10 +0200792#ifdef CONFIG_HOTPLUG_CPU
Steffen Klassert16295be2010-01-06 19:47:10 +1100793 unregister_hotcpu_notifier(&pinst->cpu_notifier);
Steffen Klasserte2cb2f12010-04-29 14:40:10 +0200794#endif
Steffen Klassert3789ae72010-05-19 13:45:35 +1000795
Steffen Klassertee836552010-07-07 15:30:47 +0200796 padata_stop(pinst);
Steffen Klassert16295be2010-01-06 19:47:10 +1100797 padata_free_pd(pinst->pd);
Steffen Klassert74781382010-03-04 13:30:22 +0800798 free_cpumask_var(pinst->cpumask);
Steffen Klassert16295be2010-01-06 19:47:10 +1100799 kfree(pinst);
800}
801EXPORT_SYMBOL(padata_free);