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