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