OSDN Git Service

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