OSDN Git Service

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