blob: 57ec4eb5f2e3737050aaa97910ac4774d0cbb950 [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
114 err = 0;
115 if (!(pinst->flags & PADATA_INIT))
116 goto out;
117
118 err = -EBUSY;
119 if ((pinst->flags & PADATA_RESET))
120 goto out;
121
122 if (atomic_read(&pd->refcnt) >= MAX_OBJ_NUM)
123 goto out;
124
125 err = -EINVAL;
126 if (!cpumask_test_cpu(cb_cpu, pd->cpumask))
127 goto out;
128
129 err = -EINPROGRESS;
130 atomic_inc(&pd->refcnt);
131 padata->pd = pd;
132 padata->cb_cpu = cb_cpu;
133
134 if (unlikely(atomic_read(&pd->seq_nr) == pd->max_seq_nr))
135 atomic_set(&pd->seq_nr, -1);
136
137 padata->seq_nr = atomic_inc_return(&pd->seq_nr);
138
139 target_cpu = padata_cpu_hash(padata);
140 queue = per_cpu_ptr(pd->queue, target_cpu);
141
142 spin_lock(&queue->parallel.lock);
143 list_add_tail(&padata->list, &queue->parallel.list);
144 spin_unlock(&queue->parallel.lock);
145
146 queue_work_on(target_cpu, pinst->wq, &queue->pwork);
147
148out:
149 rcu_read_unlock_bh();
150
151 return err;
152}
153EXPORT_SYMBOL(padata_do_parallel);
154
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000155/*
156 * padata_get_next - Get the next object that needs serialization.
157 *
158 * Return values are:
159 *
160 * A pointer to the control struct of the next object that needs
161 * serialization, if present in one of the percpu reorder queues.
162 *
163 * NULL, if all percpu reorder queues are empty.
164 *
165 * -EINPROGRESS, if the next object that needs serialization will
166 * be parallel processed by another cpu and is not yet present in
167 * the cpu's reorder queue.
168 *
169 * -ENODATA, if this cpu has to do the parallel processing for
170 * the next object.
171 */
Steffen Klassert16295be2010-01-06 19:47:10 +1100172static struct padata_priv *padata_get_next(struct parallel_data *pd)
173{
174 int cpu, num_cpus, empty, calc_seq_nr;
175 int seq_nr, next_nr, overrun, next_overrun;
176 struct padata_queue *queue, *next_queue;
177 struct padata_priv *padata;
178 struct padata_list *reorder;
179
180 empty = 0;
181 next_nr = -1;
182 next_overrun = 0;
183 next_queue = NULL;
184
185 num_cpus = cpumask_weight(pd->cpumask);
186
187 for_each_cpu(cpu, pd->cpumask) {
188 queue = per_cpu_ptr(pd->queue, cpu);
189 reorder = &queue->reorder;
190
191 /*
192 * Calculate the seq_nr of the object that should be
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000193 * next in this reorder queue.
Steffen Klassert16295be2010-01-06 19:47:10 +1100194 */
195 overrun = 0;
196 calc_seq_nr = (atomic_read(&queue->num_obj) * num_cpus)
197 + queue->cpu_index;
198
199 if (unlikely(calc_seq_nr > pd->max_seq_nr)) {
200 calc_seq_nr = calc_seq_nr - pd->max_seq_nr - 1;
201 overrun = 1;
202 }
203
204 if (!list_empty(&reorder->list)) {
205 padata = list_entry(reorder->list.next,
206 struct padata_priv, list);
207
208 seq_nr = padata->seq_nr;
209 BUG_ON(calc_seq_nr != seq_nr);
210 } else {
211 seq_nr = calc_seq_nr;
212 empty++;
213 }
214
215 if (next_nr < 0 || seq_nr < next_nr
216 || (next_overrun && !overrun)) {
217 next_nr = seq_nr;
218 next_overrun = overrun;
219 next_queue = queue;
220 }
221 }
222
223 padata = NULL;
224
225 if (empty == num_cpus)
226 goto out;
227
228 reorder = &next_queue->reorder;
229
230 if (!list_empty(&reorder->list)) {
231 padata = list_entry(reorder->list.next,
232 struct padata_priv, list);
233
234 if (unlikely(next_overrun)) {
235 for_each_cpu(cpu, pd->cpumask) {
236 queue = per_cpu_ptr(pd->queue, cpu);
237 atomic_set(&queue->num_obj, 0);
238 }
239 }
240
241 spin_lock(&reorder->lock);
242 list_del_init(&padata->list);
243 atomic_dec(&pd->reorder_objects);
244 spin_unlock(&reorder->lock);
245
246 atomic_inc(&next_queue->num_obj);
247
248 goto out;
249 }
250
Steffen Klassertd46a5ac2010-05-19 13:43:14 +1000251 queue = per_cpu_ptr(pd->queue, smp_processor_id());
252 if (queue->cpu_index == next_queue->cpu_index) {
Steffen Klassert16295be2010-01-06 19:47:10 +1100253 padata = ERR_PTR(-ENODATA);
254 goto out;
255 }
256
257 padata = ERR_PTR(-EINPROGRESS);
258out:
259 return padata;
260}
261
262static void padata_reorder(struct parallel_data *pd)
263{
264 struct padata_priv *padata;
265 struct padata_queue *queue;
266 struct padata_instance *pinst = pd->pinst;
267
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000268 /*
269 * We need to ensure that only one cpu can work on dequeueing of
270 * the reorder queue the time. Calculating in which percpu reorder
271 * queue the next object will arrive takes some time. A spinlock
272 * would be highly contended. Also it is not clear in which order
273 * the objects arrive to the reorder queues. So a cpu could wait to
274 * get the lock just to notice that there is nothing to do at the
275 * moment. Therefore we use a trylock and let the holder of the lock
276 * care for all the objects enqueued during the holdtime of the lock.
277 */
Steffen Klassert16295be2010-01-06 19:47:10 +1100278 if (!spin_trylock_bh(&pd->lock))
Steffen Klassertd46a5ac2010-05-19 13:43:14 +1000279 return;
Steffen Klassert16295be2010-01-06 19:47:10 +1100280
281 while (1) {
282 padata = padata_get_next(pd);
283
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000284 /*
285 * All reorder queues are empty, or the next object that needs
286 * serialization is parallel processed by another cpu and is
287 * still on it's way to the cpu's reorder queue, nothing to
288 * do for now.
289 */
Steffen Klassert16295be2010-01-06 19:47:10 +1100290 if (!padata || PTR_ERR(padata) == -EINPROGRESS)
291 break;
292
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000293 /*
294 * This cpu has to do the parallel processing of the next
295 * object. It's waiting in the cpu's parallelization queue,
296 * so exit imediately.
297 */
Steffen Klassert16295be2010-01-06 19:47:10 +1100298 if (PTR_ERR(padata) == -ENODATA) {
Steffen Klassertd46a5ac2010-05-19 13:43:14 +1000299 del_timer(&pd->timer);
Steffen Klassert16295be2010-01-06 19:47:10 +1100300 spin_unlock_bh(&pd->lock);
Steffen Klassertd46a5ac2010-05-19 13:43:14 +1000301 return;
Steffen Klassert16295be2010-01-06 19:47:10 +1100302 }
303
304 queue = per_cpu_ptr(pd->queue, padata->cb_cpu);
305
306 spin_lock(&queue->serial.lock);
307 list_add_tail(&padata->list, &queue->serial.list);
308 spin_unlock(&queue->serial.lock);
309
310 queue_work_on(padata->cb_cpu, pinst->wq, &queue->swork);
311 }
312
313 spin_unlock_bh(&pd->lock);
314
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000315 /*
316 * The next object that needs serialization might have arrived to
317 * the reorder queues in the meantime, we will be called again
318 * from the timer function if noone else cares for it.
319 */
Steffen Klassertd46a5ac2010-05-19 13:43:14 +1000320 if (atomic_read(&pd->reorder_objects)
321 && !(pinst->flags & PADATA_RESET))
322 mod_timer(&pd->timer, jiffies + HZ);
323 else
324 del_timer(&pd->timer);
Steffen Klassert16295be2010-01-06 19:47:10 +1100325
Steffen Klassert16295be2010-01-06 19:47:10 +1100326 return;
327}
328
Steffen Klassertd46a5ac2010-05-19 13:43:14 +1000329static void padata_reorder_timer(unsigned long arg)
330{
331 struct parallel_data *pd = (struct parallel_data *)arg;
332
333 padata_reorder(pd);
334}
335
Steffen Klassert16295be2010-01-06 19:47:10 +1100336static void padata_serial_worker(struct work_struct *work)
337{
338 struct padata_queue *queue;
339 struct parallel_data *pd;
340 LIST_HEAD(local_list);
341
342 local_bh_disable();
343 queue = container_of(work, struct padata_queue, swork);
344 pd = queue->pd;
345
346 spin_lock(&queue->serial.lock);
347 list_replace_init(&queue->serial.list, &local_list);
348 spin_unlock(&queue->serial.lock);
349
350 while (!list_empty(&local_list)) {
351 struct padata_priv *padata;
352
353 padata = list_entry(local_list.next,
354 struct padata_priv, list);
355
356 list_del_init(&padata->list);
357
358 padata->serial(padata);
359 atomic_dec(&pd->refcnt);
360 }
361 local_bh_enable();
362}
363
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000364/**
Steffen Klassert16295be2010-01-06 19:47:10 +1100365 * padata_do_serial - padata serialization function
366 *
367 * @padata: object to be serialized.
368 *
369 * padata_do_serial must be called for every parallelized object.
370 * The serialization callback function will run with BHs off.
371 */
372void padata_do_serial(struct padata_priv *padata)
373{
374 int cpu;
375 struct padata_queue *queue;
376 struct parallel_data *pd;
377
378 pd = padata->pd;
379
380 cpu = get_cpu();
381 queue = per_cpu_ptr(pd->queue, cpu);
382
383 spin_lock(&queue->reorder.lock);
384 atomic_inc(&pd->reorder_objects);
385 list_add_tail(&padata->list, &queue->reorder.list);
386 spin_unlock(&queue->reorder.lock);
387
388 put_cpu();
389
390 padata_reorder(pd);
391}
392EXPORT_SYMBOL(padata_do_serial);
393
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000394/* Allocate and initialize the internal cpumask dependend resources. */
Steffen Klassert16295be2010-01-06 19:47:10 +1100395static struct parallel_data *padata_alloc_pd(struct padata_instance *pinst,
396 const struct cpumask *cpumask)
397{
398 int cpu, cpu_index, num_cpus;
399 struct padata_queue *queue;
400 struct parallel_data *pd;
401
402 cpu_index = 0;
403
404 pd = kzalloc(sizeof(struct parallel_data), GFP_KERNEL);
405 if (!pd)
406 goto err;
407
408 pd->queue = alloc_percpu(struct padata_queue);
409 if (!pd->queue)
410 goto err_free_pd;
411
412 if (!alloc_cpumask_var(&pd->cpumask, GFP_KERNEL))
413 goto err_free_queue;
414
Steffen Klassert7b389b22010-04-29 14:41:36 +0200415 cpumask_and(pd->cpumask, cpumask, cpu_active_mask);
416
417 for_each_cpu(cpu, pd->cpumask) {
Steffen Klassert16295be2010-01-06 19:47:10 +1100418 queue = per_cpu_ptr(pd->queue, cpu);
419
420 queue->pd = pd;
421
Steffen Klassert7b389b22010-04-29 14:41:36 +0200422 queue->cpu_index = cpu_index;
423 cpu_index++;
Steffen Klassert16295be2010-01-06 19:47:10 +1100424
425 INIT_LIST_HEAD(&queue->reorder.list);
426 INIT_LIST_HEAD(&queue->parallel.list);
427 INIT_LIST_HEAD(&queue->serial.list);
428 spin_lock_init(&queue->reorder.lock);
429 spin_lock_init(&queue->parallel.lock);
430 spin_lock_init(&queue->serial.lock);
431
432 INIT_WORK(&queue->pwork, padata_parallel_worker);
433 INIT_WORK(&queue->swork, padata_serial_worker);
434 atomic_set(&queue->num_obj, 0);
435 }
436
Steffen Klassert16295be2010-01-06 19:47:10 +1100437 num_cpus = cpumask_weight(pd->cpumask);
438 pd->max_seq_nr = (MAX_SEQ_NR / num_cpus) * num_cpus - 1;
439
Steffen Klassertd46a5ac2010-05-19 13:43:14 +1000440 setup_timer(&pd->timer, padata_reorder_timer, (unsigned long)pd);
Steffen Klassert16295be2010-01-06 19:47:10 +1100441 atomic_set(&pd->seq_nr, -1);
442 atomic_set(&pd->reorder_objects, 0);
443 atomic_set(&pd->refcnt, 0);
444 pd->pinst = pinst;
445 spin_lock_init(&pd->lock);
446
447 return pd;
448
449err_free_queue:
450 free_percpu(pd->queue);
451err_free_pd:
452 kfree(pd);
453err:
454 return NULL;
455}
456
457static void padata_free_pd(struct parallel_data *pd)
458{
459 free_cpumask_var(pd->cpumask);
460 free_percpu(pd->queue);
461 kfree(pd);
462}
463
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000464/* Flush all objects out of the padata queues. */
Steffen Klassert2b73b072010-05-19 13:43:46 +1000465static void padata_flush_queues(struct parallel_data *pd)
466{
467 int cpu;
468 struct padata_queue *queue;
469
470 for_each_cpu(cpu, pd->cpumask) {
471 queue = per_cpu_ptr(pd->queue, cpu);
472 flush_work(&queue->pwork);
473 }
474
475 del_timer_sync(&pd->timer);
476
477 if (atomic_read(&pd->reorder_objects))
478 padata_reorder(pd);
479
480 for_each_cpu(cpu, pd->cpumask) {
481 queue = per_cpu_ptr(pd->queue, cpu);
482 flush_work(&queue->swork);
483 }
484
485 BUG_ON(atomic_read(&pd->refcnt) != 0);
486}
487
Steffen Klassert4c879172010-07-07 15:30:10 +0200488static void __padata_start(struct padata_instance *pinst)
489{
490 pinst->flags |= PADATA_INIT;
491}
492
Steffen Klassertee836552010-07-07 15:30:47 +0200493static void __padata_stop(struct padata_instance *pinst)
494{
495 if (!(pinst->flags & PADATA_INIT))
496 return;
497
498 pinst->flags &= ~PADATA_INIT;
499
500 synchronize_rcu();
501
502 get_online_cpus();
503 padata_flush_queues(pinst->pd);
504 put_online_cpus();
505}
506
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000507/* Replace the internal control stucture with a new one. */
Steffen Klassert16295be2010-01-06 19:47:10 +1100508static void padata_replace(struct padata_instance *pinst,
509 struct parallel_data *pd_new)
510{
511 struct parallel_data *pd_old = pinst->pd;
512
513 pinst->flags |= PADATA_RESET;
514
515 rcu_assign_pointer(pinst->pd, pd_new);
516
517 synchronize_rcu();
518
Steffen Klassert33e54452010-07-07 15:31:26 +0200519 if (pd_old) {
520 padata_flush_queues(pd_old);
521 padata_free_pd(pd_old);
522 }
Steffen Klassert16295be2010-01-06 19:47:10 +1100523
524 pinst->flags &= ~PADATA_RESET;
525}
526
Steffen Klassert33e54452010-07-07 15:31:26 +0200527/* If cpumask contains no active cpu, we mark the instance as invalid. */
528static bool padata_validate_cpumask(struct padata_instance *pinst,
529 const struct cpumask *cpumask)
530{
531 if (!cpumask_intersects(cpumask, cpu_active_mask)) {
532 pinst->flags |= PADATA_INVALID;
533 return false;
534 }
535
536 pinst->flags &= ~PADATA_INVALID;
537 return true;
538}
539
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000540/**
Steffen Klassert16295be2010-01-06 19:47:10 +1100541 * padata_set_cpumask - set the cpumask that padata should use
542 *
543 * @pinst: padata instance
544 * @cpumask: the cpumask to use
545 */
546int padata_set_cpumask(struct padata_instance *pinst,
547 cpumask_var_t cpumask)
548{
Steffen Klassert33e54452010-07-07 15:31:26 +0200549 int valid;
Steffen Klassert16295be2010-01-06 19:47:10 +1100550 int err = 0;
Steffen Klassert33e54452010-07-07 15:31:26 +0200551 struct parallel_data *pd = NULL;
Steffen Klassert16295be2010-01-06 19:47:10 +1100552
Steffen Klassert16295be2010-01-06 19:47:10 +1100553 mutex_lock(&pinst->lock);
554
Steffen Klassert33e54452010-07-07 15:31:26 +0200555 valid = padata_validate_cpumask(pinst, cpumask);
556 if (!valid) {
557 __padata_stop(pinst);
558 goto out_replace;
559 }
560
Steffen Klassert6751fb32010-04-29 14:42:30 +0200561 get_online_cpus();
562
Steffen Klassert16295be2010-01-06 19:47:10 +1100563 pd = padata_alloc_pd(pinst, cpumask);
564 if (!pd) {
565 err = -ENOMEM;
566 goto out;
567 }
568
Steffen Klassert33e54452010-07-07 15:31:26 +0200569out_replace:
Steffen Klassert16295be2010-01-06 19:47:10 +1100570 cpumask_copy(pinst->cpumask, cpumask);
571
572 padata_replace(pinst, pd);
573
Steffen Klassert33e54452010-07-07 15:31:26 +0200574 if (valid)
575 __padata_start(pinst);
576
Steffen Klassert16295be2010-01-06 19:47:10 +1100577out:
Steffen Klassert6751fb32010-04-29 14:42:30 +0200578 put_online_cpus();
579
Steffen Klassert16295be2010-01-06 19:47:10 +1100580 mutex_unlock(&pinst->lock);
581
582 return err;
583}
584EXPORT_SYMBOL(padata_set_cpumask);
585
586static int __padata_add_cpu(struct padata_instance *pinst, int cpu)
587{
588 struct parallel_data *pd;
589
590 if (cpumask_test_cpu(cpu, cpu_active_mask)) {
591 pd = padata_alloc_pd(pinst, pinst->cpumask);
592 if (!pd)
593 return -ENOMEM;
594
595 padata_replace(pinst, pd);
Steffen Klassert33e54452010-07-07 15:31:26 +0200596
597 if (padata_validate_cpumask(pinst, pinst->cpumask))
598 __padata_start(pinst);
Steffen Klassert16295be2010-01-06 19:47:10 +1100599 }
600
601 return 0;
602}
603
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000604/**
Steffen Klassert16295be2010-01-06 19:47:10 +1100605 * padata_add_cpu - add a cpu to the padata cpumask
606 *
607 * @pinst: padata instance
608 * @cpu: cpu to add
609 */
610int padata_add_cpu(struct padata_instance *pinst, int cpu)
611{
612 int err;
613
Steffen Klassert16295be2010-01-06 19:47:10 +1100614 mutex_lock(&pinst->lock);
615
Steffen Klassert6751fb32010-04-29 14:42:30 +0200616 get_online_cpus();
Steffen Klassert16295be2010-01-06 19:47:10 +1100617 cpumask_set_cpu(cpu, pinst->cpumask);
618 err = __padata_add_cpu(pinst, cpu);
Steffen Klassert6751fb32010-04-29 14:42:30 +0200619 put_online_cpus();
Steffen Klassert16295be2010-01-06 19:47:10 +1100620
621 mutex_unlock(&pinst->lock);
622
623 return err;
624}
625EXPORT_SYMBOL(padata_add_cpu);
626
627static int __padata_remove_cpu(struct padata_instance *pinst, int cpu)
628{
Steffen Klassert33e54452010-07-07 15:31:26 +0200629 struct parallel_data *pd = NULL;
Steffen Klassert16295be2010-01-06 19:47:10 +1100630
631 if (cpumask_test_cpu(cpu, cpu_online_mask)) {
Steffen Klassert33e54452010-07-07 15:31:26 +0200632
633 if (!padata_validate_cpumask(pinst, pinst->cpumask)) {
634 __padata_stop(pinst);
635 padata_replace(pinst, pd);
636 goto out;
637 }
638
Steffen Klassert16295be2010-01-06 19:47:10 +1100639 pd = padata_alloc_pd(pinst, pinst->cpumask);
640 if (!pd)
641 return -ENOMEM;
642
643 padata_replace(pinst, pd);
644 }
645
Steffen Klassert33e54452010-07-07 15:31:26 +0200646out:
Steffen Klassert16295be2010-01-06 19:47:10 +1100647 return 0;
648}
649
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000650/**
Steffen Klassert16295be2010-01-06 19:47:10 +1100651 * padata_remove_cpu - remove a cpu from the padata cpumask
652 *
653 * @pinst: padata instance
654 * @cpu: cpu to remove
655 */
656int padata_remove_cpu(struct padata_instance *pinst, int cpu)
657{
658 int err;
659
Steffen Klassert16295be2010-01-06 19:47:10 +1100660 mutex_lock(&pinst->lock);
661
Steffen Klassert6751fb32010-04-29 14:42:30 +0200662 get_online_cpus();
Steffen Klassert16295be2010-01-06 19:47:10 +1100663 cpumask_clear_cpu(cpu, pinst->cpumask);
664 err = __padata_remove_cpu(pinst, cpu);
Steffen Klassert6751fb32010-04-29 14:42:30 +0200665 put_online_cpus();
Steffen Klassert16295be2010-01-06 19:47:10 +1100666
667 mutex_unlock(&pinst->lock);
668
669 return err;
670}
671EXPORT_SYMBOL(padata_remove_cpu);
672
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000673/**
Steffen Klassert16295be2010-01-06 19:47:10 +1100674 * padata_start - start the parallel processing
675 *
676 * @pinst: padata instance to start
677 */
Steffen Klassert4c879172010-07-07 15:30:10 +0200678int padata_start(struct padata_instance *pinst)
Steffen Klassert16295be2010-01-06 19:47:10 +1100679{
Steffen Klassert4c879172010-07-07 15:30:10 +0200680 int err = 0;
681
Steffen Klassert16295be2010-01-06 19:47:10 +1100682 mutex_lock(&pinst->lock);
Steffen Klassert4c879172010-07-07 15:30:10 +0200683
684 if (pinst->flags & PADATA_INVALID)
685 err =-EINVAL;
686
687 __padata_start(pinst);
688
Steffen Klassert16295be2010-01-06 19:47:10 +1100689 mutex_unlock(&pinst->lock);
Steffen Klassert4c879172010-07-07 15:30:10 +0200690
691 return err;
Steffen Klassert16295be2010-01-06 19:47:10 +1100692}
693EXPORT_SYMBOL(padata_start);
694
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000695/**
Steffen Klassert16295be2010-01-06 19:47:10 +1100696 * padata_stop - stop the parallel processing
697 *
698 * @pinst: padata instance to stop
699 */
700void padata_stop(struct padata_instance *pinst)
701{
Steffen Klassert16295be2010-01-06 19:47:10 +1100702 mutex_lock(&pinst->lock);
Steffen Klassertee836552010-07-07 15:30:47 +0200703 __padata_stop(pinst);
Steffen Klassert16295be2010-01-06 19:47:10 +1100704 mutex_unlock(&pinst->lock);
705}
706EXPORT_SYMBOL(padata_stop);
707
Steffen Klasserte2cb2f12010-04-29 14:40:10 +0200708#ifdef CONFIG_HOTPLUG_CPU
Henrik Kretzschmar975d2602010-03-29 16:15:31 +0800709static int padata_cpu_callback(struct notifier_block *nfb,
710 unsigned long action, void *hcpu)
Steffen Klassert16295be2010-01-06 19:47:10 +1100711{
712 int err;
713 struct padata_instance *pinst;
714 int cpu = (unsigned long)hcpu;
715
716 pinst = container_of(nfb, struct padata_instance, cpu_notifier);
717
718 switch (action) {
719 case CPU_ONLINE:
720 case CPU_ONLINE_FROZEN:
721 if (!cpumask_test_cpu(cpu, pinst->cpumask))
722 break;
723 mutex_lock(&pinst->lock);
724 err = __padata_add_cpu(pinst, cpu);
725 mutex_unlock(&pinst->lock);
726 if (err)
727 return NOTIFY_BAD;
728 break;
729
730 case CPU_DOWN_PREPARE:
731 case CPU_DOWN_PREPARE_FROZEN:
732 if (!cpumask_test_cpu(cpu, pinst->cpumask))
733 break;
734 mutex_lock(&pinst->lock);
735 err = __padata_remove_cpu(pinst, cpu);
736 mutex_unlock(&pinst->lock);
737 if (err)
738 return NOTIFY_BAD;
739 break;
740
741 case CPU_UP_CANCELED:
742 case CPU_UP_CANCELED_FROZEN:
743 if (!cpumask_test_cpu(cpu, pinst->cpumask))
744 break;
745 mutex_lock(&pinst->lock);
746 __padata_remove_cpu(pinst, cpu);
747 mutex_unlock(&pinst->lock);
748
749 case CPU_DOWN_FAILED:
750 case CPU_DOWN_FAILED_FROZEN:
751 if (!cpumask_test_cpu(cpu, pinst->cpumask))
752 break;
753 mutex_lock(&pinst->lock);
754 __padata_add_cpu(pinst, cpu);
755 mutex_unlock(&pinst->lock);
756 }
757
758 return NOTIFY_OK;
759}
Steffen Klasserte2cb2f12010-04-29 14:40:10 +0200760#endif
Steffen Klassert16295be2010-01-06 19:47:10 +1100761
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000762/**
Steffen Klassert16295be2010-01-06 19:47:10 +1100763 * padata_alloc - allocate and initialize a padata instance
764 *
765 * @cpumask: cpumask that padata uses for parallelization
766 * @wq: workqueue to use for the allocated padata instance
767 */
768struct padata_instance *padata_alloc(const struct cpumask *cpumask,
769 struct workqueue_struct *wq)
770{
Steffen Klassert16295be2010-01-06 19:47:10 +1100771 struct padata_instance *pinst;
Steffen Klassert33e54452010-07-07 15:31:26 +0200772 struct parallel_data *pd = NULL;
Steffen Klassert16295be2010-01-06 19:47:10 +1100773
774 pinst = kzalloc(sizeof(struct padata_instance), GFP_KERNEL);
775 if (!pinst)
776 goto err;
777
Steffen Klassert6751fb32010-04-29 14:42:30 +0200778 get_online_cpus();
779
Steffen Klassert33e54452010-07-07 15:31:26 +0200780 if (!alloc_cpumask_var(&pinst->cpumask, GFP_KERNEL))
Steffen Klassert16295be2010-01-06 19:47:10 +1100781 goto err_free_inst;
782
Steffen Klassert33e54452010-07-07 15:31:26 +0200783 if (padata_validate_cpumask(pinst, cpumask)) {
784 pd = padata_alloc_pd(pinst, cpumask);
785 if (!pd)
786 goto err_free_mask;
787 }
Steffen Klassert74781382010-03-04 13:30:22 +0800788
Steffen Klassert16295be2010-01-06 19:47:10 +1100789 rcu_assign_pointer(pinst->pd, pd);
790
791 pinst->wq = wq;
792
793 cpumask_copy(pinst->cpumask, cpumask);
794
795 pinst->flags = 0;
796
Steffen Klasserte2cb2f12010-04-29 14:40:10 +0200797#ifdef CONFIG_HOTPLUG_CPU
Steffen Klassert16295be2010-01-06 19:47:10 +1100798 pinst->cpu_notifier.notifier_call = padata_cpu_callback;
799 pinst->cpu_notifier.priority = 0;
Steffen Klasserte2cb2f12010-04-29 14:40:10 +0200800 register_hotcpu_notifier(&pinst->cpu_notifier);
801#endif
Steffen Klassert16295be2010-01-06 19:47:10 +1100802
Steffen Klassert6751fb32010-04-29 14:42:30 +0200803 put_online_cpus();
804
Steffen Klassert16295be2010-01-06 19:47:10 +1100805 mutex_init(&pinst->lock);
806
807 return pinst;
808
Steffen Klassert33e54452010-07-07 15:31:26 +0200809err_free_mask:
810 free_cpumask_var(pinst->cpumask);
Steffen Klassert16295be2010-01-06 19:47:10 +1100811err_free_inst:
812 kfree(pinst);
Steffen Klassert6751fb32010-04-29 14:42:30 +0200813 put_online_cpus();
Steffen Klassert16295be2010-01-06 19:47:10 +1100814err:
815 return NULL;
816}
817EXPORT_SYMBOL(padata_alloc);
818
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000819/**
Steffen Klassert16295be2010-01-06 19:47:10 +1100820 * padata_free - free a padata instance
821 *
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000822 * @padata_inst: padata instance to free
Steffen Klassert16295be2010-01-06 19:47:10 +1100823 */
824void padata_free(struct padata_instance *pinst)
825{
Steffen Klasserte2cb2f12010-04-29 14:40:10 +0200826#ifdef CONFIG_HOTPLUG_CPU
Steffen Klassert16295be2010-01-06 19:47:10 +1100827 unregister_hotcpu_notifier(&pinst->cpu_notifier);
Steffen Klasserte2cb2f12010-04-29 14:40:10 +0200828#endif
Steffen Klassert3789ae72010-05-19 13:45:35 +1000829
Steffen Klassertee836552010-07-07 15:30:47 +0200830 padata_stop(pinst);
Steffen Klassert16295be2010-01-06 19:47:10 +1100831 padata_free_pd(pinst->pd);
Steffen Klassert74781382010-03-04 13:30:22 +0800832 free_cpumask_var(pinst->cpumask);
Steffen Klassert16295be2010-01-06 19:47:10 +1100833 kfree(pinst);
834}
835EXPORT_SYMBOL(padata_free);