Linux 4.9.243
[linux/fpc-iii.git] / kernel / padata.c
blobe82f066d63ac1d5f0f423e6b9d3f95a90b391fca
1 /*
2 * padata.c - generic interface to process data streams in parallel
4 * See Documentation/padata.txt for an api documentation.
6 * Copyright (C) 2008, 2009 secunet Security Networks AG
7 * Copyright (C) 2008, 2009 Steffen Klassert <steffen.klassert@secunet.com>
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.
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.
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.
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>
35 #define MAX_OBJ_NUM 1000
37 static void padata_free_pd(struct parallel_data *pd);
39 static int padata_index_to_cpu(struct parallel_data *pd, int cpu_index)
41 int cpu, target_cpu;
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);
47 return target_cpu;
50 static int padata_cpu_hash(struct parallel_data *pd)
52 unsigned int seq_nr;
53 int cpu_index;
56 * Hash the sequence numbers to the cpus by taking
57 * seq_nr mod. number of cpus in use.
60 seq_nr = atomic_inc_return(&pd->seq_nr);
61 cpu_index = seq_nr % cpumask_weight(pd->cpumask.pcpu);
63 return padata_index_to_cpu(pd, cpu_index);
66 static void padata_parallel_worker(struct work_struct *parallel_work)
68 struct padata_parallel_queue *pqueue;
69 LIST_HEAD(local_list);
71 local_bh_disable();
72 pqueue = container_of(parallel_work,
73 struct padata_parallel_queue, work);
75 spin_lock(&pqueue->parallel.lock);
76 list_replace_init(&pqueue->parallel.list, &local_list);
77 spin_unlock(&pqueue->parallel.lock);
79 while (!list_empty(&local_list)) {
80 struct padata_priv *padata;
82 padata = list_entry(local_list.next,
83 struct padata_priv, list);
85 list_del_init(&padata->list);
87 padata->parallel(padata);
90 local_bh_enable();
93 /**
94 * padata_do_parallel - padata parallelization function
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).
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.
105 int padata_do_parallel(struct padata_instance *pinst,
106 struct padata_priv *padata, int cb_cpu)
108 int target_cpu, err;
109 struct padata_parallel_queue *queue;
110 struct parallel_data *pd;
112 rcu_read_lock_bh();
114 pd = rcu_dereference_bh(pinst->pd);
116 err = -EINVAL;
117 if (!(pinst->flags & PADATA_INIT) || pinst->flags & PADATA_INVALID)
118 goto out;
120 if (!cpumask_test_cpu(cb_cpu, pd->cpumask.cbcpu))
121 goto out;
123 err = -EBUSY;
124 if ((pinst->flags & PADATA_RESET))
125 goto out;
127 if (atomic_read(&pd->refcnt) >= MAX_OBJ_NUM)
128 goto out;
130 err = 0;
131 atomic_inc(&pd->refcnt);
132 padata->pd = pd;
133 padata->cb_cpu = cb_cpu;
135 target_cpu = padata_cpu_hash(pd);
136 padata->cpu = target_cpu;
137 queue = per_cpu_ptr(pd->pqueue, target_cpu);
139 spin_lock(&queue->parallel.lock);
140 list_add_tail(&padata->list, &queue->parallel.list);
141 spin_unlock(&queue->parallel.lock);
143 queue_work_on(target_cpu, pinst->wq, &queue->work);
145 out:
146 rcu_read_unlock_bh();
148 return err;
150 EXPORT_SYMBOL(padata_do_parallel);
153 * padata_get_next - Get the next object that needs serialization.
155 * Return values are:
157 * A pointer to the control struct of the next object that needs
158 * serialization, if present in one of the percpu reorder queues.
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.
164 * -ENODATA, if this cpu has to do the parallel processing for
165 * the next object.
167 static struct padata_priv *padata_get_next(struct parallel_data *pd)
169 struct padata_parallel_queue *next_queue;
170 struct padata_priv *padata;
171 struct padata_list *reorder;
172 int cpu = pd->cpu;
174 next_queue = per_cpu_ptr(pd->pqueue, cpu);
175 reorder = &next_queue->reorder;
177 spin_lock(&reorder->lock);
178 if (!list_empty(&reorder->list)) {
179 padata = list_entry(reorder->list.next,
180 struct padata_priv, list);
182 list_del_init(&padata->list);
183 atomic_dec(&pd->reorder_objects);
185 pd->cpu = cpumask_next_wrap(cpu, pd->cpumask.pcpu, -1,
186 false);
188 spin_unlock(&reorder->lock);
189 goto out;
191 spin_unlock(&reorder->lock);
193 if (__this_cpu_read(pd->pqueue->cpu_index) == next_queue->cpu_index) {
194 padata = ERR_PTR(-ENODATA);
195 goto out;
198 padata = ERR_PTR(-EINPROGRESS);
199 out:
200 return padata;
203 static void padata_reorder(struct parallel_data *pd)
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;
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.
221 if (!spin_trylock_bh(&pd->lock))
222 return;
224 while (1) {
225 padata = padata_get_next(pd);
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.
232 if (PTR_ERR(padata) == -EINPROGRESS)
233 break;
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.
240 if (PTR_ERR(padata) == -ENODATA) {
241 spin_unlock_bh(&pd->lock);
242 return;
245 cb_cpu = padata->cb_cpu;
246 squeue = per_cpu_ptr(pd->squeue, cb_cpu);
248 spin_lock(&squeue->serial.lock);
249 list_add_tail(&padata->list, &squeue->serial.list);
250 spin_unlock(&squeue->serial.lock);
252 queue_work_on(cb_cpu, pinst->wq, &squeue->work);
255 spin_unlock_bh(&pd->lock);
258 * The next object that needs serialization might have arrived to
259 * the reorder queues in the meantime.
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.
265 smp_mb();
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);
272 static void invoke_padata_reorder(struct work_struct *work)
274 struct parallel_data *pd;
276 local_bh_disable();
277 pd = container_of(work, struct parallel_data, reorder_work);
278 padata_reorder(pd);
279 local_bh_enable();
282 static void padata_serial_worker(struct work_struct *serial_work)
284 struct padata_serial_queue *squeue;
285 struct parallel_data *pd;
286 LIST_HEAD(local_list);
287 int cnt;
289 local_bh_disable();
290 squeue = container_of(serial_work, struct padata_serial_queue, work);
291 pd = squeue->pd;
293 spin_lock(&squeue->serial.lock);
294 list_replace_init(&squeue->serial.list, &local_list);
295 spin_unlock(&squeue->serial.lock);
297 cnt = 0;
299 while (!list_empty(&local_list)) {
300 struct padata_priv *padata;
302 padata = list_entry(local_list.next,
303 struct padata_priv, list);
305 list_del_init(&padata->list);
307 padata->serial(padata);
308 cnt++;
310 local_bh_enable();
312 if (atomic_sub_and_test(cnt, &pd->refcnt))
313 padata_free_pd(pd);
317 * padata_do_serial - padata serialization function
319 * @padata: object to be serialized.
321 * padata_do_serial must be called for every parallelized object.
322 * The serialization callback function will run with BHs off.
324 void padata_do_serial(struct padata_priv *padata)
326 struct parallel_data *pd = padata->pd;
327 struct padata_parallel_queue *pqueue = per_cpu_ptr(pd->pqueue,
328 padata->cpu);
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);
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.
340 smp_mb__after_atomic();
342 padata_reorder(pd);
344 EXPORT_SYMBOL(padata_do_serial);
346 static int padata_setup_cpumasks(struct parallel_data *pd,
347 const struct cpumask *pcpumask,
348 const struct cpumask *cbcpumask)
350 if (!alloc_cpumask_var(&pd->cpumask.pcpu, GFP_KERNEL))
351 return -ENOMEM;
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;
359 cpumask_and(pd->cpumask.cbcpu, cbcpumask, cpu_online_mask);
360 return 0;
363 static void __padata_list_init(struct padata_list *pd_list)
365 INIT_LIST_HEAD(&pd_list->list);
366 spin_lock_init(&pd_list->lock);
369 /* Initialize all percpu queues used by serial workers */
370 static void padata_init_squeues(struct parallel_data *pd)
372 int cpu;
373 struct padata_serial_queue *squeue;
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);
383 /* Initialize all percpu queues used by parallel workers */
384 static void padata_init_pqueues(struct parallel_data *pd)
386 int cpu_index, cpu;
387 struct padata_parallel_queue *pqueue;
389 cpu_index = 0;
390 for_each_possible_cpu(cpu) {
391 pqueue = per_cpu_ptr(pd->pqueue, cpu);
393 if (!cpumask_test_cpu(cpu, pd->cpumask.pcpu)) {
394 pqueue->cpu_index = -1;
395 continue;
398 pqueue->cpu_index = cpu_index;
399 cpu_index++;
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);
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)
413 struct parallel_data *pd;
415 pd = kzalloc(sizeof(struct parallel_data), GFP_KERNEL);
416 if (!pd)
417 goto err;
419 pd->pqueue = alloc_percpu(struct padata_parallel_queue);
420 if (!pd->pqueue)
421 goto err_free_pd;
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;
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);
439 return pd;
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;
451 static void padata_free_pd(struct parallel_data *pd)
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);
460 static void __padata_start(struct padata_instance *pinst)
462 pinst->flags |= PADATA_INIT;
465 static void __padata_stop(struct padata_instance *pinst)
467 if (!(pinst->flags & PADATA_INIT))
468 return;
470 pinst->flags &= ~PADATA_INIT;
472 synchronize_rcu();
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)
479 struct parallel_data *pd_old = pinst->pd;
480 int notification_mask = 0;
482 pinst->flags |= PADATA_RESET;
484 rcu_assign_pointer(pinst->pd, pd_new);
486 synchronize_rcu();
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;
493 if (atomic_dec_and_test(&pd_old->refcnt))
494 padata_free_pd(pd_old);
496 if (notification_mask)
497 blocking_notifier_call_chain(&pinst->cpumask_change_notifier,
498 notification_mask,
499 &pd_new->cpumask);
501 pinst->flags &= ~PADATA_RESET;
505 * padata_register_cpumask_notifier - Registers a notifier that will be called
506 * if either pcpu or cbcpu or both cpumasks change.
508 * @pinst: A poineter to padata instance
509 * @nblock: A pointer to notifier block.
511 int padata_register_cpumask_notifier(struct padata_instance *pinst,
512 struct notifier_block *nblock)
514 return blocking_notifier_chain_register(&pinst->cpumask_change_notifier,
515 nblock);
517 EXPORT_SYMBOL(padata_register_cpumask_notifier);
520 * padata_unregister_cpumask_notifier - Unregisters cpumask notifier
521 * registered earlier using padata_register_cpumask_notifier
523 * @pinst: A pointer to data instance.
524 * @nlock: A pointer to notifier block.
526 int padata_unregister_cpumask_notifier(struct padata_instance *pinst,
527 struct notifier_block *nblock)
529 return blocking_notifier_chain_unregister(
530 &pinst->cpumask_change_notifier,
531 nblock);
533 EXPORT_SYMBOL(padata_unregister_cpumask_notifier);
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)
540 if (!cpumask_intersects(cpumask, cpu_online_mask)) {
541 pinst->flags |= PADATA_INVALID;
542 return false;
545 pinst->flags &= ~PADATA_INVALID;
546 return true;
549 static int __padata_set_cpumasks(struct padata_instance *pinst,
550 cpumask_var_t pcpumask,
551 cpumask_var_t cbcpumask)
553 int valid;
554 struct parallel_data *pd;
556 valid = padata_validate_cpumask(pinst, pcpumask);
557 if (!valid) {
558 __padata_stop(pinst);
559 goto out_replace;
562 valid = padata_validate_cpumask(pinst, cbcpumask);
563 if (!valid)
564 __padata_stop(pinst);
566 out_replace:
567 pd = padata_alloc_pd(pinst, pcpumask, cbcpumask);
568 if (!pd)
569 return -ENOMEM;
571 cpumask_copy(pinst->cpumask.pcpu, pcpumask);
572 cpumask_copy(pinst->cpumask.cbcpu, cbcpumask);
574 padata_replace(pinst, pd);
576 if (valid)
577 __padata_start(pinst);
579 return 0;
583 * padata_set_cpumask: Sets specified by @cpumask_type cpumask to the value
584 * equivalent to @cpumask.
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
591 int padata_set_cpumask(struct padata_instance *pinst, int cpumask_type,
592 cpumask_var_t cpumask)
594 struct cpumask *serial_mask, *parallel_mask;
595 int err = -EINVAL;
597 get_online_cpus();
598 mutex_lock(&pinst->lock);
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;
613 err = __padata_set_cpumasks(pinst, parallel_mask, serial_mask);
615 out:
616 mutex_unlock(&pinst->lock);
617 put_online_cpus();
619 return err;
621 EXPORT_SYMBOL(padata_set_cpumask);
624 * padata_start - start the parallel processing
626 * @pinst: padata instance to start
628 int padata_start(struct padata_instance *pinst)
630 int err = 0;
632 mutex_lock(&pinst->lock);
634 if (pinst->flags & PADATA_INVALID)
635 err = -EINVAL;
637 __padata_start(pinst);
639 mutex_unlock(&pinst->lock);
641 return err;
643 EXPORT_SYMBOL(padata_start);
646 * padata_stop - stop the parallel processing
648 * @pinst: padata instance to stop
650 void padata_stop(struct padata_instance *pinst)
652 mutex_lock(&pinst->lock);
653 __padata_stop(pinst);
654 mutex_unlock(&pinst->lock);
656 EXPORT_SYMBOL(padata_stop);
658 #ifdef CONFIG_HOTPLUG_CPU
660 static int __padata_add_cpu(struct padata_instance *pinst, int cpu)
662 struct parallel_data *pd;
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;
670 padata_replace(pinst, pd);
672 if (padata_validate_cpumask(pinst, pinst->cpumask.pcpu) &&
673 padata_validate_cpumask(pinst, pinst->cpumask.cbcpu))
674 __padata_start(pinst);
677 return 0;
680 static int __padata_remove_cpu(struct padata_instance *pinst, int cpu)
682 struct parallel_data *pd = NULL;
684 if (cpumask_test_cpu(cpu, cpu_online_mask)) {
686 if (!padata_validate_cpumask(pinst, pinst->cpumask.pcpu) ||
687 !padata_validate_cpumask(pinst, pinst->cpumask.cbcpu))
688 __padata_stop(pinst);
690 pd = padata_alloc_pd(pinst, pinst->cpumask.pcpu,
691 pinst->cpumask.cbcpu);
692 if (!pd)
693 return -ENOMEM;
695 padata_replace(pinst, pd);
697 cpumask_clear_cpu(cpu, pd->cpumask.cbcpu);
698 cpumask_clear_cpu(cpu, pd->cpumask.pcpu);
701 return 0;
705 * padata_remove_cpu - remove a cpu from the one or both(serial and parallel)
706 * padata cpumasks.
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
715 int padata_remove_cpu(struct padata_instance *pinst, int cpu, int mask)
717 int err;
719 if (!(mask & (PADATA_CPU_SERIAL | PADATA_CPU_PARALLEL)))
720 return -EINVAL;
722 mutex_lock(&pinst->lock);
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);
730 err = __padata_remove_cpu(pinst, cpu);
731 put_online_cpus();
733 mutex_unlock(&pinst->lock);
735 return err;
737 EXPORT_SYMBOL(padata_remove_cpu);
739 static inline int pinst_has_cpu(struct padata_instance *pinst, int cpu)
741 return cpumask_test_cpu(cpu, pinst->cpumask.pcpu) ||
742 cpumask_test_cpu(cpu, pinst->cpumask.cbcpu);
745 static int padata_cpu_online(unsigned int cpu, struct hlist_node *node)
747 struct padata_instance *pinst;
748 int ret;
750 pinst = hlist_entry_safe(node, struct padata_instance, node);
751 if (!pinst_has_cpu(pinst, cpu))
752 return 0;
754 mutex_lock(&pinst->lock);
755 ret = __padata_add_cpu(pinst, cpu);
756 mutex_unlock(&pinst->lock);
757 return ret;
760 static int padata_cpu_prep_down(unsigned int cpu, struct hlist_node *node)
762 struct padata_instance *pinst;
763 int ret;
765 pinst = hlist_entry_safe(node, struct padata_instance, node);
766 if (!pinst_has_cpu(pinst, cpu))
767 return 0;
769 mutex_lock(&pinst->lock);
770 ret = __padata_remove_cpu(pinst, cpu);
771 mutex_unlock(&pinst->lock);
772 return ret;
775 static enum cpuhp_state hp_online;
776 #endif
778 static void __padata_free(struct padata_instance *pinst)
780 #ifdef CONFIG_HOTPLUG_CPU
781 cpuhp_state_remove_instance_nocalls(hp_online, &pinst->node);
782 #endif
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);
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)
796 static void padata_sysfs_release(struct kobject *kobj)
798 struct padata_instance *pinst = kobj2pinst(kobj);
799 __padata_free(pinst);
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);
809 static ssize_t show_cpumask(struct padata_instance *pinst,
810 struct attribute *attr, char *buf)
812 struct cpumask *cpumask;
813 ssize_t len;
815 mutex_lock(&pinst->lock);
816 if (!strcmp(attr->name, "serial_cpumask"))
817 cpumask = pinst->cpumask.cbcpu;
818 else
819 cpumask = pinst->cpumask.pcpu;
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;
827 static ssize_t store_cpumask(struct padata_instance *pinst,
828 struct attribute *attr,
829 const char *buf, size_t count)
831 cpumask_var_t new_cpumask;
832 ssize_t ret;
833 int mask_type;
835 if (!alloc_cpumask_var(&new_cpumask, GFP_KERNEL))
836 return -ENOMEM;
838 ret = bitmap_parse(buf, count, cpumask_bits(new_cpumask),
839 nr_cpumask_bits);
840 if (ret < 0)
841 goto out;
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;
849 out:
850 free_cpumask_var(new_cpumask);
851 return ret;
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)
861 PADATA_ATTR_RW(serial_cpumask, show_cpumask, store_cpumask);
862 PADATA_ATTR_RW(parallel_cpumask, show_cpumask, store_cpumask);
865 * Padata sysfs provides the following objects:
866 * serial_cpumask [RW] - cpumask for serial workers
867 * parallel_cpumask [RW] - cpumask for parallel workers
869 static struct attribute *padata_default_attrs[] = {
870 &serial_cpumask_attr.attr,
871 &parallel_cpumask_attr.attr,
872 NULL,
875 static ssize_t padata_sysfs_show(struct kobject *kobj,
876 struct attribute *attr, char *buf)
878 struct padata_instance *pinst;
879 struct padata_sysfs_entry *pentry;
880 ssize_t ret = -EIO;
882 pinst = kobj2pinst(kobj);
883 pentry = attr2pentry(attr);
884 if (pentry->show)
885 ret = pentry->show(pinst, attr, buf);
887 return ret;
890 static ssize_t padata_sysfs_store(struct kobject *kobj, struct attribute *attr,
891 const char *buf, size_t count)
893 struct padata_instance *pinst;
894 struct padata_sysfs_entry *pentry;
895 ssize_t ret = -EIO;
897 pinst = kobj2pinst(kobj);
898 pentry = attr2pentry(attr);
899 if (pentry->show)
900 ret = pentry->store(pinst, attr, buf, count);
902 return ret;
905 static const struct sysfs_ops padata_sysfs_ops = {
906 .show = padata_sysfs_show,
907 .store = padata_sysfs_store,
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,
917 * padata_alloc_possible - Allocate and initialize padata instance.
918 * Use the cpu_possible_mask for serial and
919 * parallel workers.
921 * @wq: workqueue to use for the allocated padata instance
923 struct padata_instance *padata_alloc_possible(struct workqueue_struct *wq)
925 return padata_alloc(wq, cpu_possible_mask, cpu_possible_mask);
927 EXPORT_SYMBOL(padata_alloc_possible);
930 * padata_alloc - allocate and initialize a padata instance and specify
931 * cpumasks for serial and parallel workers.
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
937 struct padata_instance *padata_alloc(struct workqueue_struct *wq,
938 const struct cpumask *pcpumask,
939 const struct cpumask *cbcpumask)
941 struct padata_instance *pinst;
942 struct parallel_data *pd = NULL;
944 pinst = kzalloc(sizeof(struct padata_instance), GFP_KERNEL);
945 if (!pinst)
946 goto err;
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;
955 if (!padata_validate_cpumask(pinst, pcpumask) ||
956 !padata_validate_cpumask(pinst, cbcpumask))
957 goto err_free_masks;
959 pd = padata_alloc_pd(pinst, pcpumask, cbcpumask);
960 if (!pd)
961 goto err_free_masks;
963 rcu_assign_pointer(pinst->pd, pd);
965 pinst->wq = wq;
967 cpumask_copy(pinst->cpumask.pcpu, pcpumask);
968 cpumask_copy(pinst->cpumask.cbcpu, cbcpumask);
970 pinst->flags = 0;
972 put_online_cpus();
974 BLOCKING_INIT_NOTIFIER_HEAD(&pinst->cpumask_change_notifier);
975 kobject_init(&pinst->kobj, &padata_attr_type);
976 mutex_init(&pinst->lock);
978 #ifdef CONFIG_HOTPLUG_CPU
979 cpuhp_state_add_instance_nocalls(hp_online, &pinst->node);
980 #endif
981 return pinst;
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;
994 * padata_free - free a padata instance
996 * @padata_inst: padata instance to free
998 void padata_free(struct padata_instance *pinst)
1000 kobject_put(&pinst->kobj);
1002 EXPORT_SYMBOL(padata_free);
1004 #ifdef CONFIG_HOTPLUG_CPU
1006 static __init int padata_driver_init(void)
1008 int ret;
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;
1018 module_init(padata_driver_init);
1020 static __exit void padata_driver_exit(void)
1022 cpuhp_remove_multi_state(hp_online);
1024 module_exit(padata_driver_exit);
1025 #endif