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