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
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 int padata_index_to_cpu(struct parallel_data
*pd
, int cpu_index
)
41 target_cpu
= cpumask_first(pd
->cpumask
.pcpu
);
42 for (cpu
= 0; cpu
< cpu_index
; cpu
++)
43 target_cpu
= cpumask_next(target_cpu
, pd
->cpumask
.pcpu
);
48 static int padata_cpu_hash(struct parallel_data
*pd
)
54 * Hash the sequence numbers to the cpus by taking
55 * seq_nr mod. number of cpus in use.
58 seq_nr
= atomic_inc_return(&pd
->seq_nr
);
59 cpu_index
= seq_nr
% cpumask_weight(pd
->cpumask
.pcpu
);
61 return padata_index_to_cpu(pd
, cpu_index
);
64 static void padata_parallel_worker(struct work_struct
*parallel_work
)
66 struct padata_parallel_queue
*pqueue
;
67 LIST_HEAD(local_list
);
70 pqueue
= container_of(parallel_work
,
71 struct padata_parallel_queue
, work
);
73 spin_lock(&pqueue
->parallel
.lock
);
74 list_replace_init(&pqueue
->parallel
.list
, &local_list
);
75 spin_unlock(&pqueue
->parallel
.lock
);
77 while (!list_empty(&local_list
)) {
78 struct padata_priv
*padata
;
80 padata
= list_entry(local_list
.next
,
81 struct padata_priv
, list
);
83 list_del_init(&padata
->list
);
85 padata
->parallel(padata
);
92 * padata_do_parallel - padata parallelization function
94 * @pinst: padata instance
95 * @padata: object to be parallelized
96 * @cb_cpu: cpu the serialization callback function will run on,
97 * must be in the serial cpumask of padata(i.e. cpumask.cbcpu).
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.
103 int padata_do_parallel(struct padata_instance
*pinst
,
104 struct padata_priv
*padata
, int cb_cpu
)
107 struct padata_parallel_queue
*queue
;
108 struct parallel_data
*pd
;
112 pd
= rcu_dereference_bh(pinst
->pd
);
115 if (!(pinst
->flags
& PADATA_INIT
) || pinst
->flags
& PADATA_INVALID
)
118 if (!cpumask_test_cpu(cb_cpu
, pd
->cpumask
.cbcpu
))
122 if ((pinst
->flags
& PADATA_RESET
))
125 if (atomic_read(&pd
->refcnt
) >= MAX_OBJ_NUM
)
129 atomic_inc(&pd
->refcnt
);
131 padata
->cb_cpu
= cb_cpu
;
133 target_cpu
= padata_cpu_hash(pd
);
134 padata
->cpu
= target_cpu
;
135 queue
= per_cpu_ptr(pd
->pqueue
, target_cpu
);
137 spin_lock(&queue
->parallel
.lock
);
138 list_add_tail(&padata
->list
, &queue
->parallel
.list
);
139 spin_unlock(&queue
->parallel
.lock
);
141 queue_work_on(target_cpu
, pinst
->wq
, &queue
->work
);
144 rcu_read_unlock_bh();
148 EXPORT_SYMBOL(padata_do_parallel
);
151 * padata_get_next - Get the next object that needs serialization.
155 * A pointer to the control struct of the next object that needs
156 * serialization, if present in one of the percpu reorder queues.
158 * -EINPROGRESS, if the next object that needs serialization will
159 * be parallel processed by another cpu and is not yet present in
160 * the cpu's reorder queue.
162 * -ENODATA, if this cpu has to do the parallel processing for
165 static struct padata_priv
*padata_get_next(struct parallel_data
*pd
)
168 unsigned int next_nr
, next_index
;
169 struct padata_parallel_queue
*next_queue
;
170 struct padata_priv
*padata
;
171 struct padata_list
*reorder
;
173 num_cpus
= cpumask_weight(pd
->cpumask
.pcpu
);
176 * Calculate the percpu reorder queue and the sequence
177 * number of the next object.
179 next_nr
= pd
->processed
;
180 next_index
= next_nr
% num_cpus
;
181 cpu
= padata_index_to_cpu(pd
, next_index
);
182 next_queue
= per_cpu_ptr(pd
->pqueue
, cpu
);
184 reorder
= &next_queue
->reorder
;
186 spin_lock(&reorder
->lock
);
187 if (!list_empty(&reorder
->list
)) {
188 padata
= list_entry(reorder
->list
.next
,
189 struct padata_priv
, list
);
191 list_del_init(&padata
->list
);
192 atomic_dec(&pd
->reorder_objects
);
196 spin_unlock(&reorder
->lock
);
199 spin_unlock(&reorder
->lock
);
201 if (__this_cpu_read(pd
->pqueue
->cpu_index
) == next_queue
->cpu_index
) {
202 padata
= ERR_PTR(-ENODATA
);
206 padata
= ERR_PTR(-EINPROGRESS
);
211 static void padata_reorder(struct parallel_data
*pd
)
214 struct padata_priv
*padata
;
215 struct padata_serial_queue
*squeue
;
216 struct padata_instance
*pinst
= pd
->pinst
;
219 * We need to ensure that only one cpu can work on dequeueing of
220 * the reorder queue the time. Calculating in which percpu reorder
221 * queue the next object will arrive takes some time. A spinlock
222 * would be highly contended. Also it is not clear in which order
223 * the objects arrive to the reorder queues. So a cpu could wait to
224 * get the lock just to notice that there is nothing to do at the
225 * moment. Therefore we use a trylock and let the holder of the lock
226 * care for all the objects enqueued during the holdtime of the lock.
228 if (!spin_trylock_bh(&pd
->lock
))
232 padata
= padata_get_next(pd
);
235 * If the next object that needs serialization is parallel
236 * processed by another cpu and is still on it's way to the
237 * cpu's reorder queue, nothing to do for now.
239 if (PTR_ERR(padata
) == -EINPROGRESS
)
243 * This cpu has to do the parallel processing of the next
244 * object. It's waiting in the cpu's parallelization queue,
245 * so exit immediately.
247 if (PTR_ERR(padata
) == -ENODATA
) {
248 del_timer(&pd
->timer
);
249 spin_unlock_bh(&pd
->lock
);
253 cb_cpu
= padata
->cb_cpu
;
254 squeue
= per_cpu_ptr(pd
->squeue
, cb_cpu
);
256 spin_lock(&squeue
->serial
.lock
);
257 list_add_tail(&padata
->list
, &squeue
->serial
.list
);
258 spin_unlock(&squeue
->serial
.lock
);
260 queue_work_on(cb_cpu
, pinst
->wq
, &squeue
->work
);
263 spin_unlock_bh(&pd
->lock
);
266 * The next object that needs serialization might have arrived to
267 * the reorder queues in the meantime, we will be called again
268 * from the timer function if no one else cares for it.
270 if (atomic_read(&pd
->reorder_objects
)
271 && !(pinst
->flags
& PADATA_RESET
))
272 mod_timer(&pd
->timer
, jiffies
+ HZ
);
274 del_timer(&pd
->timer
);
279 static void invoke_padata_reorder(struct work_struct
*work
)
281 struct padata_parallel_queue
*pqueue
;
282 struct parallel_data
*pd
;
285 pqueue
= container_of(work
, struct padata_parallel_queue
, reorder_work
);
291 static void padata_reorder_timer(struct timer_list
*t
)
293 struct parallel_data
*pd
= from_timer(pd
, t
, timer
);
299 /* We don't lock pd here to not interfere with parallel processing
300 * padata_reorder() calls on other CPUs. We just need any CPU out of
301 * the cpumask.pcpu set. It would be nice if it's the right one but
302 * it doesn't matter if we're off to the next one by using an outdated
303 * pd->processed value.
305 weight
= cpumask_weight(pd
->cpumask
.pcpu
);
306 target_cpu
= padata_index_to_cpu(pd
, pd
->processed
% weight
);
308 /* ensure to call the reorder callback on the correct CPU */
309 if (cpu
!= target_cpu
) {
310 struct padata_parallel_queue
*pqueue
;
311 struct padata_instance
*pinst
;
313 /* The timer function is serialized wrt itself -- no locking
317 pqueue
= per_cpu_ptr(pd
->pqueue
, target_cpu
);
318 queue_work_on(target_cpu
, pinst
->wq
, &pqueue
->reorder_work
);
326 static void padata_serial_worker(struct work_struct
*serial_work
)
328 struct padata_serial_queue
*squeue
;
329 struct parallel_data
*pd
;
330 LIST_HEAD(local_list
);
333 squeue
= container_of(serial_work
, struct padata_serial_queue
, work
);
336 spin_lock(&squeue
->serial
.lock
);
337 list_replace_init(&squeue
->serial
.list
, &local_list
);
338 spin_unlock(&squeue
->serial
.lock
);
340 while (!list_empty(&local_list
)) {
341 struct padata_priv
*padata
;
343 padata
= list_entry(local_list
.next
,
344 struct padata_priv
, list
);
346 list_del_init(&padata
->list
);
348 padata
->serial(padata
);
349 atomic_dec(&pd
->refcnt
);
355 * padata_do_serial - padata serialization function
357 * @padata: object to be serialized.
359 * padata_do_serial must be called for every parallelized object.
360 * The serialization callback function will run with BHs off.
362 void padata_do_serial(struct padata_priv
*padata
)
365 struct padata_parallel_queue
*pqueue
;
366 struct parallel_data
*pd
;
367 int reorder_via_wq
= 0;
373 /* We need to run on the same CPU padata_do_parallel(.., padata, ..)
374 * was called on -- or, at least, enqueue the padata object into the
375 * correct per-cpu queue.
377 if (cpu
!= padata
->cpu
) {
382 pqueue
= per_cpu_ptr(pd
->pqueue
, cpu
);
384 spin_lock(&pqueue
->reorder
.lock
);
385 atomic_inc(&pd
->reorder_objects
);
386 list_add_tail(&padata
->list
, &pqueue
->reorder
.list
);
387 spin_unlock(&pqueue
->reorder
.lock
);
391 /* If we're running on the wrong CPU, call padata_reorder() via a
395 queue_work_on(cpu
, pd
->pinst
->wq
, &pqueue
->reorder_work
);
399 EXPORT_SYMBOL(padata_do_serial
);
401 static int padata_setup_cpumasks(struct parallel_data
*pd
,
402 const struct cpumask
*pcpumask
,
403 const struct cpumask
*cbcpumask
)
405 if (!alloc_cpumask_var(&pd
->cpumask
.pcpu
, GFP_KERNEL
))
408 cpumask_and(pd
->cpumask
.pcpu
, pcpumask
, cpu_online_mask
);
409 if (!alloc_cpumask_var(&pd
->cpumask
.cbcpu
, GFP_KERNEL
)) {
410 free_cpumask_var(pd
->cpumask
.pcpu
);
414 cpumask_and(pd
->cpumask
.cbcpu
, cbcpumask
, cpu_online_mask
);
418 static void __padata_list_init(struct padata_list
*pd_list
)
420 INIT_LIST_HEAD(&pd_list
->list
);
421 spin_lock_init(&pd_list
->lock
);
424 /* Initialize all percpu queues used by serial workers */
425 static void padata_init_squeues(struct parallel_data
*pd
)
428 struct padata_serial_queue
*squeue
;
430 for_each_cpu(cpu
, pd
->cpumask
.cbcpu
) {
431 squeue
= per_cpu_ptr(pd
->squeue
, cpu
);
433 __padata_list_init(&squeue
->serial
);
434 INIT_WORK(&squeue
->work
, padata_serial_worker
);
438 /* Initialize all percpu queues used by parallel workers */
439 static void padata_init_pqueues(struct parallel_data
*pd
)
442 struct padata_parallel_queue
*pqueue
;
445 for_each_possible_cpu(cpu
) {
446 pqueue
= per_cpu_ptr(pd
->pqueue
, cpu
);
448 if (!cpumask_test_cpu(cpu
, pd
->cpumask
.pcpu
)) {
449 pqueue
->cpu_index
= -1;
454 pqueue
->cpu_index
= cpu_index
;
457 __padata_list_init(&pqueue
->reorder
);
458 __padata_list_init(&pqueue
->parallel
);
459 INIT_WORK(&pqueue
->work
, padata_parallel_worker
);
460 INIT_WORK(&pqueue
->reorder_work
, invoke_padata_reorder
);
461 atomic_set(&pqueue
->num_obj
, 0);
465 /* Allocate and initialize the internal cpumask dependend resources. */
466 static struct parallel_data
*padata_alloc_pd(struct padata_instance
*pinst
,
467 const struct cpumask
*pcpumask
,
468 const struct cpumask
*cbcpumask
)
470 struct parallel_data
*pd
;
472 pd
= kzalloc(sizeof(struct parallel_data
), GFP_KERNEL
);
476 pd
->pqueue
= alloc_percpu(struct padata_parallel_queue
);
480 pd
->squeue
= alloc_percpu(struct padata_serial_queue
);
482 goto err_free_pqueue
;
483 if (padata_setup_cpumasks(pd
, pcpumask
, cbcpumask
) < 0)
484 goto err_free_squeue
;
486 padata_init_pqueues(pd
);
487 padata_init_squeues(pd
);
488 timer_setup(&pd
->timer
, padata_reorder_timer
, 0);
489 atomic_set(&pd
->seq_nr
, -1);
490 atomic_set(&pd
->reorder_objects
, 0);
491 atomic_set(&pd
->refcnt
, 0);
493 spin_lock_init(&pd
->lock
);
498 free_percpu(pd
->squeue
);
500 free_percpu(pd
->pqueue
);
507 static void padata_free_pd(struct parallel_data
*pd
)
509 free_cpumask_var(pd
->cpumask
.pcpu
);
510 free_cpumask_var(pd
->cpumask
.cbcpu
);
511 free_percpu(pd
->pqueue
);
512 free_percpu(pd
->squeue
);
516 /* Flush all objects out of the padata queues. */
517 static void padata_flush_queues(struct parallel_data
*pd
)
520 struct padata_parallel_queue
*pqueue
;
521 struct padata_serial_queue
*squeue
;
523 for_each_cpu(cpu
, pd
->cpumask
.pcpu
) {
524 pqueue
= per_cpu_ptr(pd
->pqueue
, cpu
);
525 flush_work(&pqueue
->work
);
528 del_timer_sync(&pd
->timer
);
530 if (atomic_read(&pd
->reorder_objects
))
533 for_each_cpu(cpu
, pd
->cpumask
.cbcpu
) {
534 squeue
= per_cpu_ptr(pd
->squeue
, cpu
);
535 flush_work(&squeue
->work
);
538 BUG_ON(atomic_read(&pd
->refcnt
) != 0);
541 static void __padata_start(struct padata_instance
*pinst
)
543 pinst
->flags
|= PADATA_INIT
;
546 static void __padata_stop(struct padata_instance
*pinst
)
548 if (!(pinst
->flags
& PADATA_INIT
))
551 pinst
->flags
&= ~PADATA_INIT
;
556 padata_flush_queues(pinst
->pd
);
560 /* Replace the internal control structure with a new one. */
561 static void padata_replace(struct padata_instance
*pinst
,
562 struct parallel_data
*pd_new
)
564 struct parallel_data
*pd_old
= pinst
->pd
;
565 int notification_mask
= 0;
567 pinst
->flags
|= PADATA_RESET
;
569 rcu_assign_pointer(pinst
->pd
, pd_new
);
573 if (!cpumask_equal(pd_old
->cpumask
.pcpu
, pd_new
->cpumask
.pcpu
))
574 notification_mask
|= PADATA_CPU_PARALLEL
;
575 if (!cpumask_equal(pd_old
->cpumask
.cbcpu
, pd_new
->cpumask
.cbcpu
))
576 notification_mask
|= PADATA_CPU_SERIAL
;
578 padata_flush_queues(pd_old
);
579 padata_free_pd(pd_old
);
581 if (notification_mask
)
582 blocking_notifier_call_chain(&pinst
->cpumask_change_notifier
,
586 pinst
->flags
&= ~PADATA_RESET
;
590 * padata_register_cpumask_notifier - Registers a notifier that will be called
591 * if either pcpu or cbcpu or both cpumasks change.
593 * @pinst: A poineter to padata instance
594 * @nblock: A pointer to notifier block.
596 int padata_register_cpumask_notifier(struct padata_instance
*pinst
,
597 struct notifier_block
*nblock
)
599 return blocking_notifier_chain_register(&pinst
->cpumask_change_notifier
,
602 EXPORT_SYMBOL(padata_register_cpumask_notifier
);
605 * padata_unregister_cpumask_notifier - Unregisters cpumask notifier
606 * registered earlier using padata_register_cpumask_notifier
608 * @pinst: A pointer to data instance.
609 * @nlock: A pointer to notifier block.
611 int padata_unregister_cpumask_notifier(struct padata_instance
*pinst
,
612 struct notifier_block
*nblock
)
614 return blocking_notifier_chain_unregister(
615 &pinst
->cpumask_change_notifier
,
618 EXPORT_SYMBOL(padata_unregister_cpumask_notifier
);
621 /* If cpumask contains no active cpu, we mark the instance as invalid. */
622 static bool padata_validate_cpumask(struct padata_instance
*pinst
,
623 const struct cpumask
*cpumask
)
625 if (!cpumask_intersects(cpumask
, cpu_online_mask
)) {
626 pinst
->flags
|= PADATA_INVALID
;
630 pinst
->flags
&= ~PADATA_INVALID
;
634 static int __padata_set_cpumasks(struct padata_instance
*pinst
,
635 cpumask_var_t pcpumask
,
636 cpumask_var_t cbcpumask
)
639 struct parallel_data
*pd
;
641 valid
= padata_validate_cpumask(pinst
, pcpumask
);
643 __padata_stop(pinst
);
647 valid
= padata_validate_cpumask(pinst
, cbcpumask
);
649 __padata_stop(pinst
);
652 pd
= padata_alloc_pd(pinst
, pcpumask
, cbcpumask
);
656 cpumask_copy(pinst
->cpumask
.pcpu
, pcpumask
);
657 cpumask_copy(pinst
->cpumask
.cbcpu
, cbcpumask
);
659 padata_replace(pinst
, pd
);
662 __padata_start(pinst
);
668 * padata_set_cpumask: Sets specified by @cpumask_type cpumask to the value
669 * equivalent to @cpumask.
671 * @pinst: padata instance
672 * @cpumask_type: PADATA_CPU_SERIAL or PADATA_CPU_PARALLEL corresponding
673 * to parallel and serial cpumasks respectively.
674 * @cpumask: the cpumask to use
676 int padata_set_cpumask(struct padata_instance
*pinst
, int cpumask_type
,
677 cpumask_var_t cpumask
)
679 struct cpumask
*serial_mask
, *parallel_mask
;
682 mutex_lock(&pinst
->lock
);
685 switch (cpumask_type
) {
686 case PADATA_CPU_PARALLEL
:
687 serial_mask
= pinst
->cpumask
.cbcpu
;
688 parallel_mask
= cpumask
;
690 case PADATA_CPU_SERIAL
:
691 parallel_mask
= pinst
->cpumask
.pcpu
;
692 serial_mask
= cpumask
;
698 err
= __padata_set_cpumasks(pinst
, parallel_mask
, serial_mask
);
702 mutex_unlock(&pinst
->lock
);
706 EXPORT_SYMBOL(padata_set_cpumask
);
709 * padata_start - start the parallel processing
711 * @pinst: padata instance to start
713 int padata_start(struct padata_instance
*pinst
)
717 mutex_lock(&pinst
->lock
);
719 if (pinst
->flags
& PADATA_INVALID
)
722 __padata_start(pinst
);
724 mutex_unlock(&pinst
->lock
);
728 EXPORT_SYMBOL(padata_start
);
731 * padata_stop - stop the parallel processing
733 * @pinst: padata instance to stop
735 void padata_stop(struct padata_instance
*pinst
)
737 mutex_lock(&pinst
->lock
);
738 __padata_stop(pinst
);
739 mutex_unlock(&pinst
->lock
);
741 EXPORT_SYMBOL(padata_stop
);
743 #ifdef CONFIG_HOTPLUG_CPU
745 static int __padata_add_cpu(struct padata_instance
*pinst
, int cpu
)
747 struct parallel_data
*pd
;
749 if (cpumask_test_cpu(cpu
, cpu_online_mask
)) {
750 pd
= padata_alloc_pd(pinst
, pinst
->cpumask
.pcpu
,
751 pinst
->cpumask
.cbcpu
);
755 padata_replace(pinst
, pd
);
757 if (padata_validate_cpumask(pinst
, pinst
->cpumask
.pcpu
) &&
758 padata_validate_cpumask(pinst
, pinst
->cpumask
.cbcpu
))
759 __padata_start(pinst
);
765 static int __padata_remove_cpu(struct padata_instance
*pinst
, int cpu
)
767 struct parallel_data
*pd
= NULL
;
769 if (cpumask_test_cpu(cpu
, cpu_online_mask
)) {
771 if (!padata_validate_cpumask(pinst
, pinst
->cpumask
.pcpu
) ||
772 !padata_validate_cpumask(pinst
, pinst
->cpumask
.cbcpu
))
773 __padata_stop(pinst
);
775 pd
= padata_alloc_pd(pinst
, pinst
->cpumask
.pcpu
,
776 pinst
->cpumask
.cbcpu
);
780 padata_replace(pinst
, pd
);
782 cpumask_clear_cpu(cpu
, pd
->cpumask
.cbcpu
);
783 cpumask_clear_cpu(cpu
, pd
->cpumask
.pcpu
);
790 * padata_remove_cpu - remove a cpu from the one or both(serial and parallel)
793 * @pinst: padata instance
794 * @cpu: cpu to remove
795 * @mask: bitmask specifying from which cpumask @cpu should be removed
796 * The @mask may be any combination of the following flags:
797 * PADATA_CPU_SERIAL - serial cpumask
798 * PADATA_CPU_PARALLEL - parallel cpumask
800 int padata_remove_cpu(struct padata_instance
*pinst
, int cpu
, int mask
)
804 if (!(mask
& (PADATA_CPU_SERIAL
| PADATA_CPU_PARALLEL
)))
807 mutex_lock(&pinst
->lock
);
810 if (mask
& PADATA_CPU_SERIAL
)
811 cpumask_clear_cpu(cpu
, pinst
->cpumask
.cbcpu
);
812 if (mask
& PADATA_CPU_PARALLEL
)
813 cpumask_clear_cpu(cpu
, pinst
->cpumask
.pcpu
);
815 err
= __padata_remove_cpu(pinst
, cpu
);
818 mutex_unlock(&pinst
->lock
);
822 EXPORT_SYMBOL(padata_remove_cpu
);
824 static inline int pinst_has_cpu(struct padata_instance
*pinst
, int cpu
)
826 return cpumask_test_cpu(cpu
, pinst
->cpumask
.pcpu
) ||
827 cpumask_test_cpu(cpu
, pinst
->cpumask
.cbcpu
);
830 static int padata_cpu_online(unsigned int cpu
, struct hlist_node
*node
)
832 struct padata_instance
*pinst
;
835 pinst
= hlist_entry_safe(node
, struct padata_instance
, node
);
836 if (!pinst_has_cpu(pinst
, cpu
))
839 mutex_lock(&pinst
->lock
);
840 ret
= __padata_add_cpu(pinst
, cpu
);
841 mutex_unlock(&pinst
->lock
);
845 static int padata_cpu_prep_down(unsigned int cpu
, struct hlist_node
*node
)
847 struct padata_instance
*pinst
;
850 pinst
= hlist_entry_safe(node
, struct padata_instance
, node
);
851 if (!pinst_has_cpu(pinst
, cpu
))
854 mutex_lock(&pinst
->lock
);
855 ret
= __padata_remove_cpu(pinst
, cpu
);
856 mutex_unlock(&pinst
->lock
);
860 static enum cpuhp_state hp_online
;
863 static void __padata_free(struct padata_instance
*pinst
)
865 #ifdef CONFIG_HOTPLUG_CPU
866 cpuhp_state_remove_instance_nocalls(hp_online
, &pinst
->node
);
870 padata_free_pd(pinst
->pd
);
871 free_cpumask_var(pinst
->cpumask
.pcpu
);
872 free_cpumask_var(pinst
->cpumask
.cbcpu
);
876 #define kobj2pinst(_kobj) \
877 container_of(_kobj, struct padata_instance, kobj)
878 #define attr2pentry(_attr) \
879 container_of(_attr, struct padata_sysfs_entry, attr)
881 static void padata_sysfs_release(struct kobject
*kobj
)
883 struct padata_instance
*pinst
= kobj2pinst(kobj
);
884 __padata_free(pinst
);
887 struct padata_sysfs_entry
{
888 struct attribute attr
;
889 ssize_t (*show
)(struct padata_instance
*, struct attribute
*, char *);
890 ssize_t (*store
)(struct padata_instance
*, struct attribute
*,
891 const char *, size_t);
894 static ssize_t
show_cpumask(struct padata_instance
*pinst
,
895 struct attribute
*attr
, char *buf
)
897 struct cpumask
*cpumask
;
900 mutex_lock(&pinst
->lock
);
901 if (!strcmp(attr
->name
, "serial_cpumask"))
902 cpumask
= pinst
->cpumask
.cbcpu
;
904 cpumask
= pinst
->cpumask
.pcpu
;
906 len
= snprintf(buf
, PAGE_SIZE
, "%*pb\n",
907 nr_cpu_ids
, cpumask_bits(cpumask
));
908 mutex_unlock(&pinst
->lock
);
909 return len
< PAGE_SIZE
? len
: -EINVAL
;
912 static ssize_t
store_cpumask(struct padata_instance
*pinst
,
913 struct attribute
*attr
,
914 const char *buf
, size_t count
)
916 cpumask_var_t new_cpumask
;
920 if (!alloc_cpumask_var(&new_cpumask
, GFP_KERNEL
))
923 ret
= bitmap_parse(buf
, count
, cpumask_bits(new_cpumask
),
928 mask_type
= !strcmp(attr
->name
, "serial_cpumask") ?
929 PADATA_CPU_SERIAL
: PADATA_CPU_PARALLEL
;
930 ret
= padata_set_cpumask(pinst
, mask_type
, new_cpumask
);
935 free_cpumask_var(new_cpumask
);
939 #define PADATA_ATTR_RW(_name, _show_name, _store_name) \
940 static struct padata_sysfs_entry _name##_attr = \
941 __ATTR(_name, 0644, _show_name, _store_name)
942 #define PADATA_ATTR_RO(_name, _show_name) \
943 static struct padata_sysfs_entry _name##_attr = \
944 __ATTR(_name, 0400, _show_name, NULL)
946 PADATA_ATTR_RW(serial_cpumask
, show_cpumask
, store_cpumask
);
947 PADATA_ATTR_RW(parallel_cpumask
, show_cpumask
, store_cpumask
);
950 * Padata sysfs provides the following objects:
951 * serial_cpumask [RW] - cpumask for serial workers
952 * parallel_cpumask [RW] - cpumask for parallel workers
954 static struct attribute
*padata_default_attrs
[] = {
955 &serial_cpumask_attr
.attr
,
956 ¶llel_cpumask_attr
.attr
,
960 static ssize_t
padata_sysfs_show(struct kobject
*kobj
,
961 struct attribute
*attr
, char *buf
)
963 struct padata_instance
*pinst
;
964 struct padata_sysfs_entry
*pentry
;
967 pinst
= kobj2pinst(kobj
);
968 pentry
= attr2pentry(attr
);
970 ret
= pentry
->show(pinst
, attr
, buf
);
975 static ssize_t
padata_sysfs_store(struct kobject
*kobj
, struct attribute
*attr
,
976 const char *buf
, size_t count
)
978 struct padata_instance
*pinst
;
979 struct padata_sysfs_entry
*pentry
;
982 pinst
= kobj2pinst(kobj
);
983 pentry
= attr2pentry(attr
);
985 ret
= pentry
->store(pinst
, attr
, buf
, count
);
990 static const struct sysfs_ops padata_sysfs_ops
= {
991 .show
= padata_sysfs_show
,
992 .store
= padata_sysfs_store
,
995 static struct kobj_type padata_attr_type
= {
996 .sysfs_ops
= &padata_sysfs_ops
,
997 .default_attrs
= padata_default_attrs
,
998 .release
= padata_sysfs_release
,
1002 * padata_alloc - allocate and initialize a padata instance and specify
1003 * cpumasks for serial and parallel workers.
1005 * @wq: workqueue to use for the allocated padata instance
1006 * @pcpumask: cpumask that will be used for padata parallelization
1007 * @cbcpumask: cpumask that will be used for padata serialization
1009 * Must be called from a cpus_read_lock() protected region
1011 static struct padata_instance
*padata_alloc(struct workqueue_struct
*wq
,
1012 const struct cpumask
*pcpumask
,
1013 const struct cpumask
*cbcpumask
)
1015 struct padata_instance
*pinst
;
1016 struct parallel_data
*pd
= NULL
;
1018 pinst
= kzalloc(sizeof(struct padata_instance
), GFP_KERNEL
);
1022 if (!alloc_cpumask_var(&pinst
->cpumask
.pcpu
, GFP_KERNEL
))
1024 if (!alloc_cpumask_var(&pinst
->cpumask
.cbcpu
, GFP_KERNEL
)) {
1025 free_cpumask_var(pinst
->cpumask
.pcpu
);
1028 if (!padata_validate_cpumask(pinst
, pcpumask
) ||
1029 !padata_validate_cpumask(pinst
, cbcpumask
))
1030 goto err_free_masks
;
1032 pd
= padata_alloc_pd(pinst
, pcpumask
, cbcpumask
);
1034 goto err_free_masks
;
1036 rcu_assign_pointer(pinst
->pd
, pd
);
1040 cpumask_copy(pinst
->cpumask
.pcpu
, pcpumask
);
1041 cpumask_copy(pinst
->cpumask
.cbcpu
, cbcpumask
);
1045 BLOCKING_INIT_NOTIFIER_HEAD(&pinst
->cpumask_change_notifier
);
1046 kobject_init(&pinst
->kobj
, &padata_attr_type
);
1047 mutex_init(&pinst
->lock
);
1049 #ifdef CONFIG_HOTPLUG_CPU
1050 cpuhp_state_add_instance_nocalls_cpuslocked(hp_online
, &pinst
->node
);
1055 free_cpumask_var(pinst
->cpumask
.pcpu
);
1056 free_cpumask_var(pinst
->cpumask
.cbcpu
);
1064 * padata_alloc_possible - Allocate and initialize padata instance.
1065 * Use the cpu_possible_mask for serial and
1068 * @wq: workqueue to use for the allocated padata instance
1070 * Must be called from a cpus_read_lock() protected region
1072 struct padata_instance
*padata_alloc_possible(struct workqueue_struct
*wq
)
1074 lockdep_assert_cpus_held();
1075 return padata_alloc(wq
, cpu_possible_mask
, cpu_possible_mask
);
1077 EXPORT_SYMBOL(padata_alloc_possible
);
1080 * padata_free - free a padata instance
1082 * @padata_inst: padata instance to free
1084 void padata_free(struct padata_instance
*pinst
)
1086 kobject_put(&pinst
->kobj
);
1088 EXPORT_SYMBOL(padata_free
);
1090 #ifdef CONFIG_HOTPLUG_CPU
1092 static __init
int padata_driver_init(void)
1096 ret
= cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN
, "padata:online",
1098 padata_cpu_prep_down
);
1104 module_init(padata_driver_init
);
1106 static __exit
void padata_driver_exit(void)
1108 cpuhp_remove_multi_state(hp_online
);
1110 module_exit(padata_driver_exit
);