1 /*
2 * padata.c - generic interface to process data streams in parallel
3 *
4 * See Documentation/padata.txt for an api documentation.
5 *
6 * Copyright (C) 2008, 2009 secunet Security Networks AG
7 * Copyright (C) 2008, 2009 Steffen Klassert <steffen.klassert@secunet.com>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms and conditions of the GNU General Public License,
11 * version 2, as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #include <linux/export.h>
24 #include <linux/cpumask.h>
25 #include <linux/err.h>
26 #include <linux/cpu.h>
27 #include <linux/padata.h>
28 #include <linux/mutex.h>
29 #include <linux/sched.h>
30 #include <linux/slab.h>
31 #include <linux/sysfs.h>
32 #include <linux/rcupdate.h>
33
34 #define MAX_OBJ_NUM 1000
35
36 static void padata_free_pd(struct parallel_data *pd);
37
padata_index_to_cpu(struct parallel_data * pd,int cpu_index)38 static int padata_index_to_cpu(struct parallel_data *pd, int cpu_index)
39 {
40 int cpu, target_cpu;
41
42 target_cpu = cpumask_first(pd->cpumask.pcpu);
43 for (cpu = 0; cpu < cpu_index; cpu++)
44 target_cpu = cpumask_next(target_cpu, pd->cpumask.pcpu);
45
46 return target_cpu;
47 }
48
padata_cpu_hash(struct parallel_data * pd)49 static int padata_cpu_hash(struct parallel_data *pd)
50 {
51 unsigned int seq_nr;
52 int cpu_index;
53
54 /*
55 * Hash the sequence numbers to the cpus by taking
56 * seq_nr mod. number of cpus in use.
57 */
58
59 seq_nr = atomic_inc_return(&pd->seq_nr);
60 cpu_index = seq_nr % cpumask_weight(pd->cpumask.pcpu);
61
62 return padata_index_to_cpu(pd, cpu_index);
63 }
64
padata_parallel_worker(struct work_struct * parallel_work)65 static void padata_parallel_worker(struct work_struct *parallel_work)
66 {
67 struct padata_parallel_queue *pqueue;
68 LIST_HEAD(local_list);
69
70 local_bh_disable();
71 pqueue = container_of(parallel_work,
72 struct padata_parallel_queue, work);
73
74 spin_lock(&pqueue->parallel.lock);
75 list_replace_init(&pqueue->parallel.list, &local_list);
76 spin_unlock(&pqueue->parallel.lock);
77
78 while (!list_empty(&local_list)) {
79 struct padata_priv *padata;
80
81 padata = list_entry(local_list.next,
82 struct padata_priv, list);
83
84 list_del_init(&padata->list);
85
86 padata->parallel(padata);
87 }
88
89 local_bh_enable();
90 }
91
92 /**
93 * padata_do_parallel - padata parallelization function
94 *
95 * @pinst: padata instance
96 * @padata: object to be parallelized
97 * @cb_cpu: cpu the serialization callback function will run on,
98 * must be in the serial cpumask of padata(i.e. cpumask.cbcpu).
99 *
100 * The parallelization callback function will run with BHs off.
101 * Note: Every object which is parallelized by padata_do_parallel
102 * must be seen by padata_do_serial.
103 */
padata_do_parallel(struct padata_instance * pinst,struct padata_priv * padata,int cb_cpu)104 int padata_do_parallel(struct padata_instance *pinst,
105 struct padata_priv *padata, int cb_cpu)
106 {
107 int target_cpu, err;
108 struct padata_parallel_queue *queue;
109 struct parallel_data *pd;
110
111 rcu_read_lock_bh();
112
113 pd = rcu_dereference_bh(pinst->pd);
114
115 err = -EINVAL;
116 if (!(pinst->flags & PADATA_INIT) || pinst->flags & PADATA_INVALID)
117 goto out;
118
119 if (!cpumask_test_cpu(cb_cpu, pd->cpumask.cbcpu))
120 goto out;
121
122 err = -EBUSY;
123 if ((pinst->flags & PADATA_RESET))
124 goto out;
125
126 if (atomic_read(&pd->refcnt) >= MAX_OBJ_NUM)
127 goto out;
128
129 err = 0;
130 atomic_inc(&pd->refcnt);
131 padata->pd = pd;
132 padata->cb_cpu = cb_cpu;
133
134 target_cpu = padata_cpu_hash(pd);
135 padata->cpu = target_cpu;
136 queue = per_cpu_ptr(pd->pqueue, target_cpu);
137
138 spin_lock(&queue->parallel.lock);
139 list_add_tail(&padata->list, &queue->parallel.list);
140 spin_unlock(&queue->parallel.lock);
141
142 queue_work_on(target_cpu, pinst->wq, &queue->work);
143
144 out:
145 rcu_read_unlock_bh();
146
147 return err;
148 }
149 EXPORT_SYMBOL(padata_do_parallel);
150
151 /*
152 * padata_get_next - Get the next object that needs serialization.
153 *
154 * Return values are:
155 *
156 * A pointer to the control struct of the next object that needs
157 * serialization, if present in one of the percpu reorder queues.
158 *
159 * -EINPROGRESS, if the next object that needs serialization will
160 * be parallel processed by another cpu and is not yet present in
161 * the cpu's reorder queue.
162 *
163 * -ENODATA, if this cpu has to do the parallel processing for
164 * the next object.
165 */
padata_get_next(struct parallel_data * pd)166 static struct padata_priv *padata_get_next(struct parallel_data *pd)
167 {
168 struct padata_parallel_queue *next_queue;
169 struct padata_priv *padata;
170 struct padata_list *reorder;
171 int cpu = pd->cpu;
172
173 next_queue = per_cpu_ptr(pd->pqueue, cpu);
174 reorder = &next_queue->reorder;
175
176 spin_lock(&reorder->lock);
177 if (!list_empty(&reorder->list)) {
178 padata = list_entry(reorder->list.next,
179 struct padata_priv, list);
180
181 list_del_init(&padata->list);
182 atomic_dec(&pd->reorder_objects);
183
184 pd->cpu = cpumask_next_wrap(cpu, pd->cpumask.pcpu, -1,
185 false);
186
187 spin_unlock(&reorder->lock);
188 goto out;
189 }
190 spin_unlock(&reorder->lock);
191
192 if (__this_cpu_read(pd->pqueue->cpu_index) == next_queue->cpu_index) {
193 padata = ERR_PTR(-ENODATA);
194 goto out;
195 }
196
197 padata = ERR_PTR(-EINPROGRESS);
198 out:
199 return padata;
200 }
201
padata_reorder(struct parallel_data * pd)202 static void padata_reorder(struct parallel_data *pd)
203 {
204 int cb_cpu;
205 struct padata_priv *padata;
206 struct padata_serial_queue *squeue;
207 struct padata_instance *pinst = pd->pinst;
208 struct padata_parallel_queue *next_queue;
209
210 /*
211 * We need to ensure that only one cpu can work on dequeueing of
212 * the reorder queue the time. Calculating in which percpu reorder
213 * queue the next object will arrive takes some time. A spinlock
214 * would be highly contended. Also it is not clear in which order
215 * the objects arrive to the reorder queues. So a cpu could wait to
216 * get the lock just to notice that there is nothing to do at the
217 * moment. Therefore we use a trylock and let the holder of the lock
218 * care for all the objects enqueued during the holdtime of the lock.
219 */
220 if (!spin_trylock_bh(&pd->lock))
221 return;
222
223 while (1) {
224 padata = padata_get_next(pd);
225
226 /*
227 * If the next object that needs serialization is parallel
228 * processed by another cpu and is still on it's way to the
229 * cpu's reorder queue, nothing to do for now.
230 */
231 if (PTR_ERR(padata) == -EINPROGRESS)
232 break;
233
234 /*
235 * This cpu has to do the parallel processing of the next
236 * object. It's waiting in the cpu's parallelization queue,
237 * so exit immediately.
238 */
239 if (PTR_ERR(padata) == -ENODATA) {
240 spin_unlock_bh(&pd->lock);
241 return;
242 }
243
244 cb_cpu = padata->cb_cpu;
245 squeue = per_cpu_ptr(pd->squeue, cb_cpu);
246
247 spin_lock(&squeue->serial.lock);
248 list_add_tail(&padata->list, &squeue->serial.list);
249 spin_unlock(&squeue->serial.lock);
250
251 queue_work_on(cb_cpu, pinst->wq, &squeue->work);
252 }
253
254 spin_unlock_bh(&pd->lock);
255
256 /*
257 * The next object that needs serialization might have arrived to
258 * the reorder queues in the meantime.
259 *
260 * Ensure reorder queue is read after pd->lock is dropped so we see
261 * new objects from another task in padata_do_serial. Pairs with
262 * smp_mb__after_atomic in padata_do_serial.
263 */
264 smp_mb();
265
266 next_queue = per_cpu_ptr(pd->pqueue, pd->cpu);
267 if (!list_empty(&next_queue->reorder.list))
268 queue_work(pinst->wq, &pd->reorder_work);
269 }
270
invoke_padata_reorder(struct work_struct * work)271 static void invoke_padata_reorder(struct work_struct *work)
272 {
273 struct parallel_data *pd;
274
275 local_bh_disable();
276 pd = container_of(work, struct parallel_data, reorder_work);
277 padata_reorder(pd);
278 local_bh_enable();
279 }
280
padata_serial_worker(struct work_struct * serial_work)281 static void padata_serial_worker(struct work_struct *serial_work)
282 {
283 struct padata_serial_queue *squeue;
284 struct parallel_data *pd;
285 LIST_HEAD(local_list);
286 int cnt;
287
288 local_bh_disable();
289 squeue = container_of(serial_work, struct padata_serial_queue, work);
290 pd = squeue->pd;
291
292 spin_lock(&squeue->serial.lock);
293 list_replace_init(&squeue->serial.list, &local_list);
294 spin_unlock(&squeue->serial.lock);
295
296 cnt = 0;
297
298 while (!list_empty(&local_list)) {
299 struct padata_priv *padata;
300
301 padata = list_entry(local_list.next,
302 struct padata_priv, list);
303
304 list_del_init(&padata->list);
305
306 padata->serial(padata);
307 cnt++;
308 }
309 local_bh_enable();
310
311 if (atomic_sub_and_test(cnt, &pd->refcnt))
312 padata_free_pd(pd);
313 }
314
315 /**
316 * padata_do_serial - padata serialization function
317 *
318 * @padata: object to be serialized.
319 *
320 * padata_do_serial must be called for every parallelized object.
321 * The serialization callback function will run with BHs off.
322 */
padata_do_serial(struct padata_priv * padata)323 void padata_do_serial(struct padata_priv *padata)
324 {
325 struct parallel_data *pd = padata->pd;
326 struct padata_parallel_queue *pqueue = per_cpu_ptr(pd->pqueue,
327 padata->cpu);
328
329 spin_lock(&pqueue->reorder.lock);
330 list_add_tail(&padata->list, &pqueue->reorder.list);
331 atomic_inc(&pd->reorder_objects);
332 spin_unlock(&pqueue->reorder.lock);
333
334 /*
335 * Ensure the addition to the reorder list is ordered correctly
336 * with the trylock of pd->lock in padata_reorder. Pairs with smp_mb
337 * in padata_reorder.
338 */
339 smp_mb__after_atomic();
340
341 padata_reorder(pd);
342 }
343 EXPORT_SYMBOL(padata_do_serial);
344
padata_setup_cpumasks(struct parallel_data * pd,const struct cpumask * pcpumask,const struct cpumask * cbcpumask)345 static int padata_setup_cpumasks(struct parallel_data *pd,
346 const struct cpumask *pcpumask,
347 const struct cpumask *cbcpumask)
348 {
349 if (!alloc_cpumask_var(&pd->cpumask.pcpu, GFP_KERNEL))
350 return -ENOMEM;
351
352 cpumask_and(pd->cpumask.pcpu, pcpumask, cpu_online_mask);
353 if (!alloc_cpumask_var(&pd->cpumask.cbcpu, GFP_KERNEL)) {
354 free_cpumask_var(pd->cpumask.pcpu);
355 return -ENOMEM;
356 }
357
358 cpumask_and(pd->cpumask.cbcpu, cbcpumask, cpu_online_mask);
359 return 0;
360 }
361
__padata_list_init(struct padata_list * pd_list)362 static void __padata_list_init(struct padata_list *pd_list)
363 {
364 INIT_LIST_HEAD(&pd_list->list);
365 spin_lock_init(&pd_list->lock);
366 }
367
368 /* Initialize all percpu queues used by serial workers */
padata_init_squeues(struct parallel_data * pd)369 static void padata_init_squeues(struct parallel_data *pd)
370 {
371 int cpu;
372 struct padata_serial_queue *squeue;
373
374 for_each_cpu(cpu, pd->cpumask.cbcpu) {
375 squeue = per_cpu_ptr(pd->squeue, cpu);
376 squeue->pd = pd;
377 __padata_list_init(&squeue->serial);
378 INIT_WORK(&squeue->work, padata_serial_worker);
379 }
380 }
381
382 /* Initialize all percpu queues used by parallel workers */
padata_init_pqueues(struct parallel_data * pd)383 static void padata_init_pqueues(struct parallel_data *pd)
384 {
385 int cpu_index, cpu;
386 struct padata_parallel_queue *pqueue;
387
388 cpu_index = 0;
389 for_each_possible_cpu(cpu) {
390 pqueue = per_cpu_ptr(pd->pqueue, cpu);
391
392 if (!cpumask_test_cpu(cpu, pd->cpumask.pcpu)) {
393 pqueue->cpu_index = -1;
394 continue;
395 }
396
397 pqueue->cpu_index = cpu_index;
398 cpu_index++;
399
400 __padata_list_init(&pqueue->reorder);
401 __padata_list_init(&pqueue->parallel);
402 INIT_WORK(&pqueue->work, padata_parallel_worker);
403 atomic_set(&pqueue->num_obj, 0);
404 }
405 }
406
407 /* Allocate and initialize the internal cpumask dependend resources. */
padata_alloc_pd(struct padata_instance * pinst,const struct cpumask * pcpumask,const struct cpumask * cbcpumask)408 static struct parallel_data *padata_alloc_pd(struct padata_instance *pinst,
409 const struct cpumask *pcpumask,
410 const struct cpumask *cbcpumask)
411 {
412 struct parallel_data *pd;
413
414 pd = kzalloc(sizeof(struct parallel_data), GFP_KERNEL);
415 if (!pd)
416 goto err;
417
418 pd->pqueue = alloc_percpu(struct padata_parallel_queue);
419 if (!pd->pqueue)
420 goto err_free_pd;
421
422 pd->squeue = alloc_percpu(struct padata_serial_queue);
423 if (!pd->squeue)
424 goto err_free_pqueue;
425 if (padata_setup_cpumasks(pd, pcpumask, cbcpumask) < 0)
426 goto err_free_squeue;
427
428 padata_init_pqueues(pd);
429 padata_init_squeues(pd);
430 atomic_set(&pd->seq_nr, -1);
431 atomic_set(&pd->reorder_objects, 0);
432 atomic_set(&pd->refcnt, 1);
433 pd->pinst = pinst;
434 spin_lock_init(&pd->lock);
435 pd->cpu = cpumask_first(pd->cpumask.pcpu);
436 INIT_WORK(&pd->reorder_work, invoke_padata_reorder);
437
438 return pd;
439
440 err_free_squeue:
441 free_percpu(pd->squeue);
442 err_free_pqueue:
443 free_percpu(pd->pqueue);
444 err_free_pd:
445 kfree(pd);
446 err:
447 return NULL;
448 }
449
padata_free_pd(struct parallel_data * pd)450 static void padata_free_pd(struct parallel_data *pd)
451 {
452 free_cpumask_var(pd->cpumask.pcpu);
453 free_cpumask_var(pd->cpumask.cbcpu);
454 free_percpu(pd->pqueue);
455 free_percpu(pd->squeue);
456 kfree(pd);
457 }
458
__padata_start(struct padata_instance * pinst)459 static void __padata_start(struct padata_instance *pinst)
460 {
461 pinst->flags |= PADATA_INIT;
462 }
463
__padata_stop(struct padata_instance * pinst)464 static void __padata_stop(struct padata_instance *pinst)
465 {
466 if (!(pinst->flags & PADATA_INIT))
467 return;
468
469 pinst->flags &= ~PADATA_INIT;
470
471 synchronize_rcu();
472 }
473
474 /* Replace the internal control structure with a new one. */
padata_replace(struct padata_instance * pinst,struct parallel_data * pd_new)475 static void padata_replace(struct padata_instance *pinst,
476 struct parallel_data *pd_new)
477 {
478 struct parallel_data *pd_old = pinst->pd;
479 int notification_mask = 0;
480
481 pinst->flags |= PADATA_RESET;
482
483 rcu_assign_pointer(pinst->pd, pd_new);
484
485 synchronize_rcu();
486
487 if (!cpumask_equal(pd_old->cpumask.pcpu, pd_new->cpumask.pcpu))
488 notification_mask |= PADATA_CPU_PARALLEL;
489 if (!cpumask_equal(pd_old->cpumask.cbcpu, pd_new->cpumask.cbcpu))
490 notification_mask |= PADATA_CPU_SERIAL;
491
492 if (atomic_dec_and_test(&pd_old->refcnt))
493 padata_free_pd(pd_old);
494
495 if (notification_mask)
496 blocking_notifier_call_chain(&pinst->cpumask_change_notifier,
497 notification_mask,
498 &pd_new->cpumask);
499
500 pinst->flags &= ~PADATA_RESET;
501 }
502
503 /**
504 * padata_register_cpumask_notifier - Registers a notifier that will be called
505 * if either pcpu or cbcpu or both cpumasks change.
506 *
507 * @pinst: A poineter to padata instance
508 * @nblock: A pointer to notifier block.
509 */
padata_register_cpumask_notifier(struct padata_instance * pinst,struct notifier_block * nblock)510 int padata_register_cpumask_notifier(struct padata_instance *pinst,
511 struct notifier_block *nblock)
512 {
513 return blocking_notifier_chain_register(&pinst->cpumask_change_notifier,
514 nblock);
515 }
516 EXPORT_SYMBOL(padata_register_cpumask_notifier);
517
518 /**
519 * padata_unregister_cpumask_notifier - Unregisters cpumask notifier
520 * registered earlier using padata_register_cpumask_notifier
521 *
522 * @pinst: A pointer to data instance.
523 * @nlock: A pointer to notifier block.
524 */
padata_unregister_cpumask_notifier(struct padata_instance * pinst,struct notifier_block * nblock)525 int padata_unregister_cpumask_notifier(struct padata_instance *pinst,
526 struct notifier_block *nblock)
527 {
528 return blocking_notifier_chain_unregister(
529 &pinst->cpumask_change_notifier,
530 nblock);
531 }
532 EXPORT_SYMBOL(padata_unregister_cpumask_notifier);
533
534
535 /* If cpumask contains no active cpu, we mark the instance as invalid. */
padata_validate_cpumask(struct padata_instance * pinst,const struct cpumask * cpumask)536 static bool padata_validate_cpumask(struct padata_instance *pinst,
537 const struct cpumask *cpumask)
538 {
539 if (!cpumask_intersects(cpumask, cpu_online_mask)) {
540 pinst->flags |= PADATA_INVALID;
541 return false;
542 }
543
544 pinst->flags &= ~PADATA_INVALID;
545 return true;
546 }
547
__padata_set_cpumasks(struct padata_instance * pinst,cpumask_var_t pcpumask,cpumask_var_t cbcpumask)548 static int __padata_set_cpumasks(struct padata_instance *pinst,
549 cpumask_var_t pcpumask,
550 cpumask_var_t cbcpumask)
551 {
552 int valid;
553 struct parallel_data *pd;
554
555 valid = padata_validate_cpumask(pinst, pcpumask);
556 if (!valid) {
557 __padata_stop(pinst);
558 goto out_replace;
559 }
560
561 valid = padata_validate_cpumask(pinst, cbcpumask);
562 if (!valid)
563 __padata_stop(pinst);
564
565 out_replace:
566 pd = padata_alloc_pd(pinst, pcpumask, cbcpumask);
567 if (!pd)
568 return -ENOMEM;
569
570 cpumask_copy(pinst->cpumask.pcpu, pcpumask);
571 cpumask_copy(pinst->cpumask.cbcpu, cbcpumask);
572
573 padata_replace(pinst, pd);
574
575 if (valid)
576 __padata_start(pinst);
577
578 return 0;
579 }
580
581 /**
582 * padata_set_cpumasks - Set both parallel and serial cpumasks. The first
583 * one is used by parallel workers and the second one
584 * by the wokers doing serialization.
585 *
586 * @pinst: padata instance
587 * @pcpumask: the cpumask to use for parallel workers
588 * @cbcpumask: the cpumsak to use for serial workers
589 */
padata_set_cpumasks(struct padata_instance * pinst,cpumask_var_t pcpumask,cpumask_var_t cbcpumask)590 int padata_set_cpumasks(struct padata_instance *pinst, cpumask_var_t pcpumask,
591 cpumask_var_t cbcpumask)
592 {
593 int err;
594
595 mutex_lock(&pinst->lock);
596 get_online_cpus();
597
598 err = __padata_set_cpumasks(pinst, pcpumask, cbcpumask);
599
600 put_online_cpus();
601 mutex_unlock(&pinst->lock);
602
603 return err;
604
605 }
606 EXPORT_SYMBOL(padata_set_cpumasks);
607
608 /**
609 * padata_set_cpumask: Sets specified by @cpumask_type cpumask to the value
610 * equivalent to @cpumask.
611 *
612 * @pinst: padata instance
613 * @cpumask_type: PADATA_CPU_SERIAL or PADATA_CPU_PARALLEL corresponding
614 * to parallel and serial cpumasks respectively.
615 * @cpumask: the cpumask to use
616 */
padata_set_cpumask(struct padata_instance * pinst,int cpumask_type,cpumask_var_t cpumask)617 int padata_set_cpumask(struct padata_instance *pinst, int cpumask_type,
618 cpumask_var_t cpumask)
619 {
620 struct cpumask *serial_mask, *parallel_mask;
621 int err = -EINVAL;
622
623 get_online_cpus();
624 mutex_lock(&pinst->lock);
625
626 switch (cpumask_type) {
627 case PADATA_CPU_PARALLEL:
628 serial_mask = pinst->cpumask.cbcpu;
629 parallel_mask = cpumask;
630 break;
631 case PADATA_CPU_SERIAL:
632 parallel_mask = pinst->cpumask.pcpu;
633 serial_mask = cpumask;
634 break;
635 default:
636 goto out;
637 }
638
639 err = __padata_set_cpumasks(pinst, parallel_mask, serial_mask);
640
641 out:
642 mutex_unlock(&pinst->lock);
643 put_online_cpus();
644
645 return err;
646 }
647 EXPORT_SYMBOL(padata_set_cpumask);
648
__padata_add_cpu(struct padata_instance * pinst,int cpu)649 static int __padata_add_cpu(struct padata_instance *pinst, int cpu)
650 {
651 struct parallel_data *pd;
652
653 if (cpumask_test_cpu(cpu, cpu_online_mask)) {
654 pd = padata_alloc_pd(pinst, pinst->cpumask.pcpu,
655 pinst->cpumask.cbcpu);
656 if (!pd)
657 return -ENOMEM;
658
659 padata_replace(pinst, pd);
660
661 if (padata_validate_cpumask(pinst, pinst->cpumask.pcpu) &&
662 padata_validate_cpumask(pinst, pinst->cpumask.cbcpu))
663 __padata_start(pinst);
664 }
665
666 return 0;
667 }
668
669 /**
670 * padata_add_cpu - add a cpu to one or both(parallel and serial)
671 * padata cpumasks.
672 *
673 * @pinst: padata instance
674 * @cpu: cpu to add
675 * @mask: bitmask of flags specifying to which cpumask @cpu shuld be added.
676 * The @mask may be any combination of the following flags:
677 * PADATA_CPU_SERIAL - serial cpumask
678 * PADATA_CPU_PARALLEL - parallel cpumask
679 */
680
padata_add_cpu(struct padata_instance * pinst,int cpu,int mask)681 int padata_add_cpu(struct padata_instance *pinst, int cpu, int mask)
682 {
683 int err;
684
685 if (!(mask & (PADATA_CPU_SERIAL | PADATA_CPU_PARALLEL)))
686 return -EINVAL;
687
688 mutex_lock(&pinst->lock);
689
690 get_online_cpus();
691 if (mask & PADATA_CPU_SERIAL)
692 cpumask_set_cpu(cpu, pinst->cpumask.cbcpu);
693 if (mask & PADATA_CPU_PARALLEL)
694 cpumask_set_cpu(cpu, pinst->cpumask.pcpu);
695
696 err = __padata_add_cpu(pinst, cpu);
697 put_online_cpus();
698
699 mutex_unlock(&pinst->lock);
700
701 return err;
702 }
703 EXPORT_SYMBOL(padata_add_cpu);
704
__padata_remove_cpu(struct padata_instance * pinst,int cpu)705 static int __padata_remove_cpu(struct padata_instance *pinst, int cpu)
706 {
707 struct parallel_data *pd = NULL;
708
709 if (cpumask_test_cpu(cpu, cpu_online_mask)) {
710
711 if (!padata_validate_cpumask(pinst, pinst->cpumask.pcpu) ||
712 !padata_validate_cpumask(pinst, pinst->cpumask.cbcpu))
713 __padata_stop(pinst);
714
715 pd = padata_alloc_pd(pinst, pinst->cpumask.pcpu,
716 pinst->cpumask.cbcpu);
717 if (!pd)
718 return -ENOMEM;
719
720 padata_replace(pinst, pd);
721
722 cpumask_clear_cpu(cpu, pd->cpumask.cbcpu);
723 cpumask_clear_cpu(cpu, pd->cpumask.pcpu);
724 }
725
726 return 0;
727 }
728
729 /**
730 * padata_remove_cpu - remove a cpu from the one or both(serial and parallel)
731 * padata cpumasks.
732 *
733 * @pinst: padata instance
734 * @cpu: cpu to remove
735 * @mask: bitmask specifying from which cpumask @cpu should be removed
736 * The @mask may be any combination of the following flags:
737 * PADATA_CPU_SERIAL - serial cpumask
738 * PADATA_CPU_PARALLEL - parallel cpumask
739 */
padata_remove_cpu(struct padata_instance * pinst,int cpu,int mask)740 int padata_remove_cpu(struct padata_instance *pinst, int cpu, int mask)
741 {
742 int err;
743
744 if (!(mask & (PADATA_CPU_SERIAL | PADATA_CPU_PARALLEL)))
745 return -EINVAL;
746
747 mutex_lock(&pinst->lock);
748
749 get_online_cpus();
750 if (mask & PADATA_CPU_SERIAL)
751 cpumask_clear_cpu(cpu, pinst->cpumask.cbcpu);
752 if (mask & PADATA_CPU_PARALLEL)
753 cpumask_clear_cpu(cpu, pinst->cpumask.pcpu);
754
755 err = __padata_remove_cpu(pinst, cpu);
756 put_online_cpus();
757
758 mutex_unlock(&pinst->lock);
759
760 return err;
761 }
762 EXPORT_SYMBOL(padata_remove_cpu);
763
764 /**
765 * padata_start - start the parallel processing
766 *
767 * @pinst: padata instance to start
768 */
padata_start(struct padata_instance * pinst)769 int padata_start(struct padata_instance *pinst)
770 {
771 int err = 0;
772
773 mutex_lock(&pinst->lock);
774
775 if (pinst->flags & PADATA_INVALID)
776 err =-EINVAL;
777
778 __padata_start(pinst);
779
780 mutex_unlock(&pinst->lock);
781
782 return err;
783 }
784 EXPORT_SYMBOL(padata_start);
785
786 /**
787 * padata_stop - stop the parallel processing
788 *
789 * @pinst: padata instance to stop
790 */
padata_stop(struct padata_instance * pinst)791 void padata_stop(struct padata_instance *pinst)
792 {
793 mutex_lock(&pinst->lock);
794 __padata_stop(pinst);
795 mutex_unlock(&pinst->lock);
796 }
797 EXPORT_SYMBOL(padata_stop);
798
799 #ifdef CONFIG_HOTPLUG_CPU
800
pinst_has_cpu(struct padata_instance * pinst,int cpu)801 static inline int pinst_has_cpu(struct padata_instance *pinst, int cpu)
802 {
803 return cpumask_test_cpu(cpu, pinst->cpumask.pcpu) ||
804 cpumask_test_cpu(cpu, pinst->cpumask.cbcpu);
805 }
806
807
padata_cpu_callback(struct notifier_block * nfb,unsigned long action,void * hcpu)808 static int padata_cpu_callback(struct notifier_block *nfb,
809 unsigned long action, void *hcpu)
810 {
811 int err;
812 struct padata_instance *pinst;
813 int cpu = (unsigned long)hcpu;
814
815 pinst = container_of(nfb, struct padata_instance, cpu_notifier);
816
817 switch (action) {
818 case CPU_ONLINE:
819 case CPU_ONLINE_FROZEN:
820 case CPU_DOWN_FAILED:
821 case CPU_DOWN_FAILED_FROZEN:
822 if (!pinst_has_cpu(pinst, cpu))
823 break;
824 mutex_lock(&pinst->lock);
825 err = __padata_add_cpu(pinst, cpu);
826 mutex_unlock(&pinst->lock);
827 if (err)
828 return notifier_from_errno(err);
829 break;
830
831 case CPU_DOWN_PREPARE:
832 case CPU_DOWN_PREPARE_FROZEN:
833 case CPU_UP_CANCELED:
834 case CPU_UP_CANCELED_FROZEN:
835 if (!pinst_has_cpu(pinst, cpu))
836 break;
837 mutex_lock(&pinst->lock);
838 err = __padata_remove_cpu(pinst, cpu);
839 mutex_unlock(&pinst->lock);
840 if (err)
841 return notifier_from_errno(err);
842 break;
843 }
844
845 return NOTIFY_OK;
846 }
847 #endif
848
__padata_free(struct padata_instance * pinst)849 static void __padata_free(struct padata_instance *pinst)
850 {
851 #ifdef CONFIG_HOTPLUG_CPU
852 unregister_hotcpu_notifier(&pinst->cpu_notifier);
853 #endif
854
855 padata_stop(pinst);
856 padata_free_pd(pinst->pd);
857 free_cpumask_var(pinst->cpumask.pcpu);
858 free_cpumask_var(pinst->cpumask.cbcpu);
859 kfree(pinst);
860 }
861
862 #define kobj2pinst(_kobj) \
863 container_of(_kobj, struct padata_instance, kobj)
864 #define attr2pentry(_attr) \
865 container_of(_attr, struct padata_sysfs_entry, attr)
866
padata_sysfs_release(struct kobject * kobj)867 static void padata_sysfs_release(struct kobject *kobj)
868 {
869 struct padata_instance *pinst = kobj2pinst(kobj);
870 __padata_free(pinst);
871 }
872
873 struct padata_sysfs_entry {
874 struct attribute attr;
875 ssize_t (*show)(struct padata_instance *, struct attribute *, char *);
876 ssize_t (*store)(struct padata_instance *, struct attribute *,
877 const char *, size_t);
878 };
879
show_cpumask(struct padata_instance * pinst,struct attribute * attr,char * buf)880 static ssize_t show_cpumask(struct padata_instance *pinst,
881 struct attribute *attr, char *buf)
882 {
883 struct cpumask *cpumask;
884 ssize_t len;
885
886 mutex_lock(&pinst->lock);
887 if (!strcmp(attr->name, "serial_cpumask"))
888 cpumask = pinst->cpumask.cbcpu;
889 else
890 cpumask = pinst->cpumask.pcpu;
891
892 len = snprintf(buf, PAGE_SIZE, "%*pb\n",
893 nr_cpu_ids, cpumask_bits(cpumask));
894 mutex_unlock(&pinst->lock);
895 return len < PAGE_SIZE ? len : -EINVAL;
896 }
897
store_cpumask(struct padata_instance * pinst,struct attribute * attr,const char * buf,size_t count)898 static ssize_t store_cpumask(struct padata_instance *pinst,
899 struct attribute *attr,
900 const char *buf, size_t count)
901 {
902 cpumask_var_t new_cpumask;
903 ssize_t ret;
904 int mask_type;
905
906 if (!alloc_cpumask_var(&new_cpumask, GFP_KERNEL))
907 return -ENOMEM;
908
909 ret = bitmap_parse(buf, count, cpumask_bits(new_cpumask),
910 nr_cpumask_bits);
911 if (ret < 0)
912 goto out;
913
914 mask_type = !strcmp(attr->name, "serial_cpumask") ?
915 PADATA_CPU_SERIAL : PADATA_CPU_PARALLEL;
916 ret = padata_set_cpumask(pinst, mask_type, new_cpumask);
917 if (!ret)
918 ret = count;
919
920 out:
921 free_cpumask_var(new_cpumask);
922 return ret;
923 }
924
925 #define PADATA_ATTR_RW(_name, _show_name, _store_name) \
926 static struct padata_sysfs_entry _name##_attr = \
927 __ATTR(_name, 0644, _show_name, _store_name)
928 #define PADATA_ATTR_RO(_name, _show_name) \
929 static struct padata_sysfs_entry _name##_attr = \
930 __ATTR(_name, 0400, _show_name, NULL)
931
932 PADATA_ATTR_RW(serial_cpumask, show_cpumask, store_cpumask);
933 PADATA_ATTR_RW(parallel_cpumask, show_cpumask, store_cpumask);
934
935 /*
936 * Padata sysfs provides the following objects:
937 * serial_cpumask [RW] - cpumask for serial workers
938 * parallel_cpumask [RW] - cpumask for parallel workers
939 */
940 static struct attribute *padata_default_attrs[] = {
941 &serial_cpumask_attr.attr,
942 ¶llel_cpumask_attr.attr,
943 NULL,
944 };
945
padata_sysfs_show(struct kobject * kobj,struct attribute * attr,char * buf)946 static ssize_t padata_sysfs_show(struct kobject *kobj,
947 struct attribute *attr, char *buf)
948 {
949 struct padata_instance *pinst;
950 struct padata_sysfs_entry *pentry;
951 ssize_t ret = -EIO;
952
953 pinst = kobj2pinst(kobj);
954 pentry = attr2pentry(attr);
955 if (pentry->show)
956 ret = pentry->show(pinst, attr, buf);
957
958 return ret;
959 }
960
padata_sysfs_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t count)961 static ssize_t padata_sysfs_store(struct kobject *kobj, struct attribute *attr,
962 const char *buf, size_t count)
963 {
964 struct padata_instance *pinst;
965 struct padata_sysfs_entry *pentry;
966 ssize_t ret = -EIO;
967
968 pinst = kobj2pinst(kobj);
969 pentry = attr2pentry(attr);
970 if (pentry->show)
971 ret = pentry->store(pinst, attr, buf, count);
972
973 return ret;
974 }
975
976 static const struct sysfs_ops padata_sysfs_ops = {
977 .show = padata_sysfs_show,
978 .store = padata_sysfs_store,
979 };
980
981 static struct kobj_type padata_attr_type = {
982 .sysfs_ops = &padata_sysfs_ops,
983 .default_attrs = padata_default_attrs,
984 .release = padata_sysfs_release,
985 };
986
987 /**
988 * padata_alloc_possible - Allocate and initialize padata instance.
989 * Use the cpu_possible_mask for serial and
990 * parallel workers.
991 *
992 * @wq: workqueue to use for the allocated padata instance
993 */
padata_alloc_possible(struct workqueue_struct * wq)994 struct padata_instance *padata_alloc_possible(struct workqueue_struct *wq)
995 {
996 return padata_alloc(wq, cpu_possible_mask, cpu_possible_mask);
997 }
998 EXPORT_SYMBOL(padata_alloc_possible);
999
1000 /**
1001 * padata_alloc - allocate and initialize a padata instance and specify
1002 * cpumasks for serial and parallel workers.
1003 *
1004 * @wq: workqueue to use for the allocated padata instance
1005 * @pcpumask: cpumask that will be used for padata parallelization
1006 * @cbcpumask: cpumask that will be used for padata serialization
1007 */
padata_alloc(struct workqueue_struct * wq,const struct cpumask * pcpumask,const struct cpumask * cbcpumask)1008 struct padata_instance *padata_alloc(struct workqueue_struct *wq,
1009 const struct cpumask *pcpumask,
1010 const struct cpumask *cbcpumask)
1011 {
1012 struct padata_instance *pinst;
1013 struct parallel_data *pd = NULL;
1014
1015 pinst = kzalloc(sizeof(struct padata_instance), GFP_KERNEL);
1016 if (!pinst)
1017 goto err;
1018
1019 get_online_cpus();
1020 if (!alloc_cpumask_var(&pinst->cpumask.pcpu, GFP_KERNEL))
1021 goto err_free_inst;
1022 if (!alloc_cpumask_var(&pinst->cpumask.cbcpu, GFP_KERNEL)) {
1023 free_cpumask_var(pinst->cpumask.pcpu);
1024 goto err_free_inst;
1025 }
1026 if (!padata_validate_cpumask(pinst, pcpumask) ||
1027 !padata_validate_cpumask(pinst, cbcpumask))
1028 goto err_free_masks;
1029
1030 pd = padata_alloc_pd(pinst, pcpumask, cbcpumask);
1031 if (!pd)
1032 goto err_free_masks;
1033
1034 rcu_assign_pointer(pinst->pd, pd);
1035
1036 pinst->wq = wq;
1037
1038 cpumask_copy(pinst->cpumask.pcpu, pcpumask);
1039 cpumask_copy(pinst->cpumask.cbcpu, cbcpumask);
1040
1041 pinst->flags = 0;
1042
1043 put_online_cpus();
1044
1045 BLOCKING_INIT_NOTIFIER_HEAD(&pinst->cpumask_change_notifier);
1046 kobject_init(&pinst->kobj, &padata_attr_type);
1047 mutex_init(&pinst->lock);
1048
1049 #ifdef CONFIG_HOTPLUG_CPU
1050 pinst->cpu_notifier.notifier_call = padata_cpu_callback;
1051 pinst->cpu_notifier.priority = 0;
1052 register_hotcpu_notifier(&pinst->cpu_notifier);
1053 #endif
1054
1055 return pinst;
1056
1057 err_free_masks:
1058 free_cpumask_var(pinst->cpumask.pcpu);
1059 free_cpumask_var(pinst->cpumask.cbcpu);
1060 err_free_inst:
1061 kfree(pinst);
1062 put_online_cpus();
1063 err:
1064 return NULL;
1065 }
1066 EXPORT_SYMBOL(padata_alloc);
1067
1068 /**
1069 * padata_free - free a padata instance
1070 *
1071 * @padata_inst: padata instance to free
1072 */
padata_free(struct padata_instance * pinst)1073 void padata_free(struct padata_instance *pinst)
1074 {
1075 kobject_put(&pinst->kobj);
1076 }
1077 EXPORT_SYMBOL(padata_free);
1078