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>
34 #define MAX_OBJ_NUM 1000
36 static void padata_free_pd(struct parallel_data
*pd
);
38 static int padata_index_to_cpu(struct parallel_data
*pd
, int cpu_index
)
42 target_cpu
= cpumask_first(pd
->cpumask
.pcpu
);
43 for (cpu
= 0; cpu
< cpu_index
; cpu
++)
44 target_cpu
= cpumask_next(target_cpu
, pd
->cpumask
.pcpu
);
49 static int padata_cpu_hash(struct parallel_data
*pd
)
55 * Hash the sequence numbers to the cpus by taking
56 * seq_nr mod. number of cpus in use.
59 seq_nr
= atomic_inc_return(&pd
->seq_nr
);
60 cpu_index
= seq_nr
% cpumask_weight(pd
->cpumask
.pcpu
);
62 return padata_index_to_cpu(pd
, cpu_index
);
65 static void padata_parallel_worker(struct work_struct
*parallel_work
)
67 struct padata_parallel_queue
*pqueue
;
68 LIST_HEAD(local_list
);
71 pqueue
= container_of(parallel_work
,
72 struct padata_parallel_queue
, work
);
74 spin_lock(&pqueue
->parallel
.lock
);
75 list_replace_init(&pqueue
->parallel
.list
, &local_list
);
76 spin_unlock(&pqueue
->parallel
.lock
);
78 while (!list_empty(&local_list
)) {
79 struct padata_priv
*padata
;
81 padata
= list_entry(local_list
.next
,
82 struct padata_priv
, list
);
84 list_del_init(&padata
->list
);
86 padata
->parallel(padata
);
93 * padata_do_parallel - padata parallelization function
95 * @pinst: padata instance
96 * @padata: object to be parallelized
97 * @cb_cpu: cpu the serialization callback function will run on,
98 * must be in the serial cpumask of padata(i.e. cpumask.cbcpu).
100 * The parallelization callback function will run with BHs off.
101 * Note: Every object which is parallelized by padata_do_parallel
102 * must be seen by padata_do_serial.
104 int padata_do_parallel(struct padata_instance
*pinst
,
105 struct padata_priv
*padata
, int cb_cpu
)
108 struct padata_parallel_queue
*queue
;
109 struct parallel_data
*pd
;
113 pd
= rcu_dereference_bh(pinst
->pd
);
116 if (!(pinst
->flags
& PADATA_INIT
) || pinst
->flags
& PADATA_INVALID
)
119 if (!cpumask_test_cpu(cb_cpu
, pd
->cpumask
.cbcpu
))
123 if ((pinst
->flags
& PADATA_RESET
))
126 if (atomic_read(&pd
->refcnt
) >= MAX_OBJ_NUM
)
130 atomic_inc(&pd
->refcnt
);
132 padata
->cb_cpu
= cb_cpu
;
134 target_cpu
= padata_cpu_hash(pd
);
135 padata
->cpu
= target_cpu
;
136 queue
= per_cpu_ptr(pd
->pqueue
, target_cpu
);
138 spin_lock(&queue
->parallel
.lock
);
139 list_add_tail(&padata
->list
, &queue
->parallel
.list
);
140 spin_unlock(&queue
->parallel
.lock
);
142 queue_work_on(target_cpu
, pinst
->wq
, &queue
->work
);
145 rcu_read_unlock_bh();
149 EXPORT_SYMBOL(padata_do_parallel
);
152 * padata_get_next - Get the next object that needs serialization.
156 * A pointer to the control struct of the next object that needs
157 * serialization, if present in one of the percpu reorder queues.
159 * -EINPROGRESS, if the next object that needs serialization will
160 * be parallel processed by another cpu and is not yet present in
161 * the cpu's reorder queue.
163 * -ENODATA, if this cpu has to do the parallel processing for
166 static struct padata_priv
*padata_get_next(struct parallel_data
*pd
)
168 struct padata_parallel_queue
*next_queue
;
169 struct padata_priv
*padata
;
170 struct padata_list
*reorder
;
173 next_queue
= per_cpu_ptr(pd
->pqueue
, cpu
);
174 reorder
= &next_queue
->reorder
;
176 spin_lock(&reorder
->lock
);
177 if (!list_empty(&reorder
->list
)) {
178 padata
= list_entry(reorder
->list
.next
,
179 struct padata_priv
, list
);
181 list_del_init(&padata
->list
);
182 atomic_dec(&pd
->reorder_objects
);
184 pd
->cpu
= cpumask_next_wrap(cpu
, pd
->cpumask
.pcpu
, -1,
187 spin_unlock(&reorder
->lock
);
190 spin_unlock(&reorder
->lock
);
192 if (__this_cpu_read(pd
->pqueue
->cpu_index
) == next_queue
->cpu_index
) {
193 padata
= ERR_PTR(-ENODATA
);
197 padata
= ERR_PTR(-EINPROGRESS
);
202 static void padata_reorder(struct parallel_data
*pd
)
205 struct padata_priv
*padata
;
206 struct padata_serial_queue
*squeue
;
207 struct padata_instance
*pinst
= pd
->pinst
;
208 struct padata_parallel_queue
*next_queue
;
211 * We need to ensure that only one cpu can work on dequeueing of
212 * the reorder queue the time. Calculating in which percpu reorder
213 * queue the next object will arrive takes some time. A spinlock
214 * would be highly contended. Also it is not clear in which order
215 * the objects arrive to the reorder queues. So a cpu could wait to
216 * get the lock just to notice that there is nothing to do at the
217 * moment. Therefore we use a trylock and let the holder of the lock
218 * care for all the objects enqueued during the holdtime of the lock.
220 if (!spin_trylock_bh(&pd
->lock
))
224 padata
= padata_get_next(pd
);
227 * If the next object that needs serialization is parallel
228 * processed by another cpu and is still on it's way to the
229 * cpu's reorder queue, nothing to do for now.
231 if (PTR_ERR(padata
) == -EINPROGRESS
)
235 * This cpu has to do the parallel processing of the next
236 * object. It's waiting in the cpu's parallelization queue,
237 * so exit immediately.
239 if (PTR_ERR(padata
) == -ENODATA
) {
240 spin_unlock_bh(&pd
->lock
);
244 cb_cpu
= padata
->cb_cpu
;
245 squeue
= per_cpu_ptr(pd
->squeue
, cb_cpu
);
247 spin_lock(&squeue
->serial
.lock
);
248 list_add_tail(&padata
->list
, &squeue
->serial
.list
);
249 spin_unlock(&squeue
->serial
.lock
);
251 queue_work_on(cb_cpu
, pinst
->wq
, &squeue
->work
);
254 spin_unlock_bh(&pd
->lock
);
257 * The next object that needs serialization might have arrived to
258 * the reorder queues in the meantime.
260 * Ensure reorder queue is read after pd->lock is dropped so we see
261 * new objects from another task in padata_do_serial. Pairs with
262 * smp_mb__after_atomic in padata_do_serial.
266 next_queue
= per_cpu_ptr(pd
->pqueue
, pd
->cpu
);
267 if (!list_empty(&next_queue
->reorder
.list
))
268 queue_work(pinst
->wq
, &pd
->reorder_work
);
271 static void invoke_padata_reorder(struct work_struct
*work
)
273 struct parallel_data
*pd
;
276 pd
= container_of(work
, struct parallel_data
, reorder_work
);
281 static void padata_serial_worker(struct work_struct
*serial_work
)
283 struct padata_serial_queue
*squeue
;
284 struct parallel_data
*pd
;
285 LIST_HEAD(local_list
);
289 squeue
= container_of(serial_work
, struct padata_serial_queue
, work
);
292 spin_lock(&squeue
->serial
.lock
);
293 list_replace_init(&squeue
->serial
.list
, &local_list
);
294 spin_unlock(&squeue
->serial
.lock
);
298 while (!list_empty(&local_list
)) {
299 struct padata_priv
*padata
;
301 padata
= list_entry(local_list
.next
,
302 struct padata_priv
, list
);
304 list_del_init(&padata
->list
);
306 padata
->serial(padata
);
311 if (atomic_sub_and_test(cnt
, &pd
->refcnt
))
316 * padata_do_serial - padata serialization function
318 * @padata: object to be serialized.
320 * padata_do_serial must be called for every parallelized object.
321 * The serialization callback function will run with BHs off.
323 void padata_do_serial(struct padata_priv
*padata
)
325 struct parallel_data
*pd
= padata
->pd
;
326 struct padata_parallel_queue
*pqueue
= per_cpu_ptr(pd
->pqueue
,
329 spin_lock(&pqueue
->reorder
.lock
);
330 list_add_tail(&padata
->list
, &pqueue
->reorder
.list
);
331 atomic_inc(&pd
->reorder_objects
);
332 spin_unlock(&pqueue
->reorder
.lock
);
335 * Ensure the addition to the reorder list is ordered correctly
336 * with the trylock of pd->lock in padata_reorder. Pairs with smp_mb
339 smp_mb__after_atomic();
343 EXPORT_SYMBOL(padata_do_serial
);
345 static int padata_setup_cpumasks(struct parallel_data
*pd
,
346 const struct cpumask
*pcpumask
,
347 const struct cpumask
*cbcpumask
)
349 if (!alloc_cpumask_var(&pd
->cpumask
.pcpu
, GFP_KERNEL
))
352 cpumask_and(pd
->cpumask
.pcpu
, pcpumask
, cpu_online_mask
);
353 if (!alloc_cpumask_var(&pd
->cpumask
.cbcpu
, GFP_KERNEL
)) {
354 free_cpumask_var(pd
->cpumask
.pcpu
);
358 cpumask_and(pd
->cpumask
.cbcpu
, cbcpumask
, cpu_online_mask
);
362 static void __padata_list_init(struct padata_list
*pd_list
)
364 INIT_LIST_HEAD(&pd_list
->list
);
365 spin_lock_init(&pd_list
->lock
);
368 /* Initialize all percpu queues used by serial workers */
369 static void padata_init_squeues(struct parallel_data
*pd
)
372 struct padata_serial_queue
*squeue
;
374 for_each_cpu(cpu
, pd
->cpumask
.cbcpu
) {
375 squeue
= per_cpu_ptr(pd
->squeue
, cpu
);
377 __padata_list_init(&squeue
->serial
);
378 INIT_WORK(&squeue
->work
, padata_serial_worker
);
382 /* Initialize all percpu queues used by parallel workers */
383 static void padata_init_pqueues(struct parallel_data
*pd
)
386 struct padata_parallel_queue
*pqueue
;
389 for_each_possible_cpu(cpu
) {
390 pqueue
= per_cpu_ptr(pd
->pqueue
, cpu
);
392 if (!cpumask_test_cpu(cpu
, pd
->cpumask
.pcpu
)) {
393 pqueue
->cpu_index
= -1;
397 pqueue
->cpu_index
= cpu_index
;
400 __padata_list_init(&pqueue
->reorder
);
401 __padata_list_init(&pqueue
->parallel
);
402 INIT_WORK(&pqueue
->work
, padata_parallel_worker
);
403 atomic_set(&pqueue
->num_obj
, 0);
407 /* Allocate and initialize the internal cpumask dependend resources. */
408 static struct parallel_data
*padata_alloc_pd(struct padata_instance
*pinst
,
409 const struct cpumask
*pcpumask
,
410 const struct cpumask
*cbcpumask
)
412 struct parallel_data
*pd
;
414 pd
= kzalloc(sizeof(struct parallel_data
), GFP_KERNEL
);
418 pd
->pqueue
= alloc_percpu(struct padata_parallel_queue
);
422 pd
->squeue
= alloc_percpu(struct padata_serial_queue
);
424 goto err_free_pqueue
;
425 if (padata_setup_cpumasks(pd
, pcpumask
, cbcpumask
) < 0)
426 goto err_free_squeue
;
428 padata_init_pqueues(pd
);
429 padata_init_squeues(pd
);
430 atomic_set(&pd
->seq_nr
, -1);
431 atomic_set(&pd
->reorder_objects
, 0);
432 atomic_set(&pd
->refcnt
, 1);
434 spin_lock_init(&pd
->lock
);
435 pd
->cpu
= cpumask_first(pd
->cpumask
.pcpu
);
436 INIT_WORK(&pd
->reorder_work
, invoke_padata_reorder
);
441 free_percpu(pd
->squeue
);
443 free_percpu(pd
->pqueue
);
450 static void padata_free_pd(struct parallel_data
*pd
)
452 free_cpumask_var(pd
->cpumask
.pcpu
);
453 free_cpumask_var(pd
->cpumask
.cbcpu
);
454 free_percpu(pd
->pqueue
);
455 free_percpu(pd
->squeue
);
459 static void __padata_start(struct padata_instance
*pinst
)
461 pinst
->flags
|= PADATA_INIT
;
464 static void __padata_stop(struct padata_instance
*pinst
)
466 if (!(pinst
->flags
& PADATA_INIT
))
469 pinst
->flags
&= ~PADATA_INIT
;
474 /* Replace the internal control structure with a new one. */
475 static void padata_replace(struct padata_instance
*pinst
,
476 struct parallel_data
*pd_new
)
478 struct parallel_data
*pd_old
= pinst
->pd
;
479 int notification_mask
= 0;
481 pinst
->flags
|= PADATA_RESET
;
483 rcu_assign_pointer(pinst
->pd
, pd_new
);
487 if (!cpumask_equal(pd_old
->cpumask
.pcpu
, pd_new
->cpumask
.pcpu
))
488 notification_mask
|= PADATA_CPU_PARALLEL
;
489 if (!cpumask_equal(pd_old
->cpumask
.cbcpu
, pd_new
->cpumask
.cbcpu
))
490 notification_mask
|= PADATA_CPU_SERIAL
;
492 if (atomic_dec_and_test(&pd_old
->refcnt
))
493 padata_free_pd(pd_old
);
495 if (notification_mask
)
496 blocking_notifier_call_chain(&pinst
->cpumask_change_notifier
,
500 pinst
->flags
&= ~PADATA_RESET
;
504 * padata_register_cpumask_notifier - Registers a notifier that will be called
505 * if either pcpu or cbcpu or both cpumasks change.
507 * @pinst: A poineter to padata instance
508 * @nblock: A pointer to notifier block.
510 int padata_register_cpumask_notifier(struct padata_instance
*pinst
,
511 struct notifier_block
*nblock
)
513 return blocking_notifier_chain_register(&pinst
->cpumask_change_notifier
,
516 EXPORT_SYMBOL(padata_register_cpumask_notifier
);
519 * padata_unregister_cpumask_notifier - Unregisters cpumask notifier
520 * registered earlier using padata_register_cpumask_notifier
522 * @pinst: A pointer to data instance.
523 * @nlock: A pointer to notifier block.
525 int padata_unregister_cpumask_notifier(struct padata_instance
*pinst
,
526 struct notifier_block
*nblock
)
528 return blocking_notifier_chain_unregister(
529 &pinst
->cpumask_change_notifier
,
532 EXPORT_SYMBOL(padata_unregister_cpumask_notifier
);
535 /* If cpumask contains no active cpu, we mark the instance as invalid. */
536 static bool padata_validate_cpumask(struct padata_instance
*pinst
,
537 const struct cpumask
*cpumask
)
539 if (!cpumask_intersects(cpumask
, cpu_online_mask
)) {
540 pinst
->flags
|= PADATA_INVALID
;
544 pinst
->flags
&= ~PADATA_INVALID
;
548 static int __padata_set_cpumasks(struct padata_instance
*pinst
,
549 cpumask_var_t pcpumask
,
550 cpumask_var_t cbcpumask
)
553 struct parallel_data
*pd
;
555 valid
= padata_validate_cpumask(pinst
, pcpumask
);
557 __padata_stop(pinst
);
561 valid
= padata_validate_cpumask(pinst
, cbcpumask
);
563 __padata_stop(pinst
);
566 pd
= padata_alloc_pd(pinst
, pcpumask
, cbcpumask
);
570 cpumask_copy(pinst
->cpumask
.pcpu
, pcpumask
);
571 cpumask_copy(pinst
->cpumask
.cbcpu
, cbcpumask
);
573 padata_replace(pinst
, pd
);
576 __padata_start(pinst
);
582 * padata_set_cpumasks - Set both parallel and serial cpumasks. The first
583 * one is used by parallel workers and the second one
584 * by the wokers doing serialization.
586 * @pinst: padata instance
587 * @pcpumask: the cpumask to use for parallel workers
588 * @cbcpumask: the cpumsak to use for serial workers
590 int padata_set_cpumasks(struct padata_instance
*pinst
, cpumask_var_t pcpumask
,
591 cpumask_var_t cbcpumask
)
595 mutex_lock(&pinst
->lock
);
598 err
= __padata_set_cpumasks(pinst
, pcpumask
, cbcpumask
);
601 mutex_unlock(&pinst
->lock
);
606 EXPORT_SYMBOL(padata_set_cpumasks
);
609 * padata_set_cpumask: Sets specified by @cpumask_type cpumask to the value
610 * equivalent to @cpumask.
612 * @pinst: padata instance
613 * @cpumask_type: PADATA_CPU_SERIAL or PADATA_CPU_PARALLEL corresponding
614 * to parallel and serial cpumasks respectively.
615 * @cpumask: the cpumask to use
617 int padata_set_cpumask(struct padata_instance
*pinst
, int cpumask_type
,
618 cpumask_var_t cpumask
)
620 struct cpumask
*serial_mask
, *parallel_mask
;
624 mutex_lock(&pinst
->lock
);
626 switch (cpumask_type
) {
627 case PADATA_CPU_PARALLEL
:
628 serial_mask
= pinst
->cpumask
.cbcpu
;
629 parallel_mask
= cpumask
;
631 case PADATA_CPU_SERIAL
:
632 parallel_mask
= pinst
->cpumask
.pcpu
;
633 serial_mask
= cpumask
;
639 err
= __padata_set_cpumasks(pinst
, parallel_mask
, serial_mask
);
642 mutex_unlock(&pinst
->lock
);
647 EXPORT_SYMBOL(padata_set_cpumask
);
649 static int __padata_add_cpu(struct padata_instance
*pinst
, int cpu
)
651 struct parallel_data
*pd
;
653 if (cpumask_test_cpu(cpu
, cpu_online_mask
)) {
654 pd
= padata_alloc_pd(pinst
, pinst
->cpumask
.pcpu
,
655 pinst
->cpumask
.cbcpu
);
659 padata_replace(pinst
, pd
);
661 if (padata_validate_cpumask(pinst
, pinst
->cpumask
.pcpu
) &&
662 padata_validate_cpumask(pinst
, pinst
->cpumask
.cbcpu
))
663 __padata_start(pinst
);
670 * padata_add_cpu - add a cpu to one or both(parallel and serial)
673 * @pinst: padata instance
675 * @mask: bitmask of flags specifying to which cpumask @cpu shuld be added.
676 * The @mask may be any combination of the following flags:
677 * PADATA_CPU_SERIAL - serial cpumask
678 * PADATA_CPU_PARALLEL - parallel cpumask
681 int padata_add_cpu(struct padata_instance
*pinst
, int cpu
, int mask
)
685 if (!(mask
& (PADATA_CPU_SERIAL
| PADATA_CPU_PARALLEL
)))
688 mutex_lock(&pinst
->lock
);
691 if (mask
& PADATA_CPU_SERIAL
)
692 cpumask_set_cpu(cpu
, pinst
->cpumask
.cbcpu
);
693 if (mask
& PADATA_CPU_PARALLEL
)
694 cpumask_set_cpu(cpu
, pinst
->cpumask
.pcpu
);
696 err
= __padata_add_cpu(pinst
, cpu
);
699 mutex_unlock(&pinst
->lock
);
703 EXPORT_SYMBOL(padata_add_cpu
);
705 static int __padata_remove_cpu(struct padata_instance
*pinst
, int cpu
)
707 struct parallel_data
*pd
= NULL
;
709 if (cpumask_test_cpu(cpu
, cpu_online_mask
)) {
711 if (!padata_validate_cpumask(pinst
, pinst
->cpumask
.pcpu
) ||
712 !padata_validate_cpumask(pinst
, pinst
->cpumask
.cbcpu
))
713 __padata_stop(pinst
);
715 pd
= padata_alloc_pd(pinst
, pinst
->cpumask
.pcpu
,
716 pinst
->cpumask
.cbcpu
);
720 padata_replace(pinst
, pd
);
722 cpumask_clear_cpu(cpu
, pd
->cpumask
.cbcpu
);
723 cpumask_clear_cpu(cpu
, pd
->cpumask
.pcpu
);
730 * padata_remove_cpu - remove a cpu from the one or both(serial and parallel)
733 * @pinst: padata instance
734 * @cpu: cpu to remove
735 * @mask: bitmask specifying from which cpumask @cpu should be removed
736 * The @mask may be any combination of the following flags:
737 * PADATA_CPU_SERIAL - serial cpumask
738 * PADATA_CPU_PARALLEL - parallel cpumask
740 int padata_remove_cpu(struct padata_instance
*pinst
, int cpu
, int mask
)
744 if (!(mask
& (PADATA_CPU_SERIAL
| PADATA_CPU_PARALLEL
)))
747 mutex_lock(&pinst
->lock
);
750 if (mask
& PADATA_CPU_SERIAL
)
751 cpumask_clear_cpu(cpu
, pinst
->cpumask
.cbcpu
);
752 if (mask
& PADATA_CPU_PARALLEL
)
753 cpumask_clear_cpu(cpu
, pinst
->cpumask
.pcpu
);
755 err
= __padata_remove_cpu(pinst
, cpu
);
758 mutex_unlock(&pinst
->lock
);
762 EXPORT_SYMBOL(padata_remove_cpu
);
765 * padata_start - start the parallel processing
767 * @pinst: padata instance to start
769 int padata_start(struct padata_instance
*pinst
)
773 mutex_lock(&pinst
->lock
);
775 if (pinst
->flags
& PADATA_INVALID
)
778 __padata_start(pinst
);
780 mutex_unlock(&pinst
->lock
);
784 EXPORT_SYMBOL(padata_start
);
787 * padata_stop - stop the parallel processing
789 * @pinst: padata instance to stop
791 void padata_stop(struct padata_instance
*pinst
)
793 mutex_lock(&pinst
->lock
);
794 __padata_stop(pinst
);
795 mutex_unlock(&pinst
->lock
);
797 EXPORT_SYMBOL(padata_stop
);
799 #ifdef CONFIG_HOTPLUG_CPU
801 static inline int pinst_has_cpu(struct padata_instance
*pinst
, int cpu
)
803 return cpumask_test_cpu(cpu
, pinst
->cpumask
.pcpu
) ||
804 cpumask_test_cpu(cpu
, pinst
->cpumask
.cbcpu
);
808 static int padata_cpu_callback(struct notifier_block
*nfb
,
809 unsigned long action
, void *hcpu
)
812 struct padata_instance
*pinst
;
813 int cpu
= (unsigned long)hcpu
;
815 pinst
= container_of(nfb
, struct padata_instance
, cpu_notifier
);
819 case CPU_ONLINE_FROZEN
:
820 case CPU_DOWN_FAILED
:
821 case CPU_DOWN_FAILED_FROZEN
:
822 if (!pinst_has_cpu(pinst
, cpu
))
824 mutex_lock(&pinst
->lock
);
825 err
= __padata_add_cpu(pinst
, cpu
);
826 mutex_unlock(&pinst
->lock
);
828 return notifier_from_errno(err
);
831 case CPU_DOWN_PREPARE
:
832 case CPU_DOWN_PREPARE_FROZEN
:
833 case CPU_UP_CANCELED
:
834 case CPU_UP_CANCELED_FROZEN
:
835 if (!pinst_has_cpu(pinst
, cpu
))
837 mutex_lock(&pinst
->lock
);
838 err
= __padata_remove_cpu(pinst
, cpu
);
839 mutex_unlock(&pinst
->lock
);
841 return notifier_from_errno(err
);
849 static void __padata_free(struct padata_instance
*pinst
)
851 #ifdef CONFIG_HOTPLUG_CPU
852 unregister_hotcpu_notifier(&pinst
->cpu_notifier
);
856 padata_free_pd(pinst
->pd
);
857 free_cpumask_var(pinst
->cpumask
.pcpu
);
858 free_cpumask_var(pinst
->cpumask
.cbcpu
);
862 #define kobj2pinst(_kobj) \
863 container_of(_kobj, struct padata_instance, kobj)
864 #define attr2pentry(_attr) \
865 container_of(_attr, struct padata_sysfs_entry, attr)
867 static void padata_sysfs_release(struct kobject
*kobj
)
869 struct padata_instance
*pinst
= kobj2pinst(kobj
);
870 __padata_free(pinst
);
873 struct padata_sysfs_entry
{
874 struct attribute attr
;
875 ssize_t (*show
)(struct padata_instance
*, struct attribute
*, char *);
876 ssize_t (*store
)(struct padata_instance
*, struct attribute
*,
877 const char *, size_t);
880 static ssize_t
show_cpumask(struct padata_instance
*pinst
,
881 struct attribute
*attr
, char *buf
)
883 struct cpumask
*cpumask
;
886 mutex_lock(&pinst
->lock
);
887 if (!strcmp(attr
->name
, "serial_cpumask"))
888 cpumask
= pinst
->cpumask
.cbcpu
;
890 cpumask
= pinst
->cpumask
.pcpu
;
892 len
= snprintf(buf
, PAGE_SIZE
, "%*pb\n",
893 nr_cpu_ids
, cpumask_bits(cpumask
));
894 mutex_unlock(&pinst
->lock
);
895 return len
< PAGE_SIZE
? len
: -EINVAL
;
898 static ssize_t
store_cpumask(struct padata_instance
*pinst
,
899 struct attribute
*attr
,
900 const char *buf
, size_t count
)
902 cpumask_var_t new_cpumask
;
906 if (!alloc_cpumask_var(&new_cpumask
, GFP_KERNEL
))
909 ret
= bitmap_parse(buf
, count
, cpumask_bits(new_cpumask
),
914 mask_type
= !strcmp(attr
->name
, "serial_cpumask") ?
915 PADATA_CPU_SERIAL
: PADATA_CPU_PARALLEL
;
916 ret
= padata_set_cpumask(pinst
, mask_type
, new_cpumask
);
921 free_cpumask_var(new_cpumask
);
925 #define PADATA_ATTR_RW(_name, _show_name, _store_name) \
926 static struct padata_sysfs_entry _name##_attr = \
927 __ATTR(_name, 0644, _show_name, _store_name)
928 #define PADATA_ATTR_RO(_name, _show_name) \
929 static struct padata_sysfs_entry _name##_attr = \
930 __ATTR(_name, 0400, _show_name, NULL)
932 PADATA_ATTR_RW(serial_cpumask
, show_cpumask
, store_cpumask
);
933 PADATA_ATTR_RW(parallel_cpumask
, show_cpumask
, store_cpumask
);
936 * Padata sysfs provides the following objects:
937 * serial_cpumask [RW] - cpumask for serial workers
938 * parallel_cpumask [RW] - cpumask for parallel workers
940 static struct attribute
*padata_default_attrs
[] = {
941 &serial_cpumask_attr
.attr
,
942 ¶llel_cpumask_attr
.attr
,
946 static ssize_t
padata_sysfs_show(struct kobject
*kobj
,
947 struct attribute
*attr
, char *buf
)
949 struct padata_instance
*pinst
;
950 struct padata_sysfs_entry
*pentry
;
953 pinst
= kobj2pinst(kobj
);
954 pentry
= attr2pentry(attr
);
956 ret
= pentry
->show(pinst
, attr
, buf
);
961 static ssize_t
padata_sysfs_store(struct kobject
*kobj
, struct attribute
*attr
,
962 const char *buf
, size_t count
)
964 struct padata_instance
*pinst
;
965 struct padata_sysfs_entry
*pentry
;
968 pinst
= kobj2pinst(kobj
);
969 pentry
= attr2pentry(attr
);
971 ret
= pentry
->store(pinst
, attr
, buf
, count
);
976 static const struct sysfs_ops padata_sysfs_ops
= {
977 .show
= padata_sysfs_show
,
978 .store
= padata_sysfs_store
,
981 static struct kobj_type padata_attr_type
= {
982 .sysfs_ops
= &padata_sysfs_ops
,
983 .default_attrs
= padata_default_attrs
,
984 .release
= padata_sysfs_release
,
988 * padata_alloc_possible - Allocate and initialize padata instance.
989 * Use the cpu_possible_mask for serial and
992 * @wq: workqueue to use for the allocated padata instance
994 struct padata_instance
*padata_alloc_possible(struct workqueue_struct
*wq
)
996 return padata_alloc(wq
, cpu_possible_mask
, cpu_possible_mask
);
998 EXPORT_SYMBOL(padata_alloc_possible
);
1001 * padata_alloc - allocate and initialize a padata instance and specify
1002 * cpumasks for serial and parallel workers.
1004 * @wq: workqueue to use for the allocated padata instance
1005 * @pcpumask: cpumask that will be used for padata parallelization
1006 * @cbcpumask: cpumask that will be used for padata serialization
1008 struct padata_instance
*padata_alloc(struct workqueue_struct
*wq
,
1009 const struct cpumask
*pcpumask
,
1010 const struct cpumask
*cbcpumask
)
1012 struct padata_instance
*pinst
;
1013 struct parallel_data
*pd
= NULL
;
1015 pinst
= kzalloc(sizeof(struct padata_instance
), GFP_KERNEL
);
1020 if (!alloc_cpumask_var(&pinst
->cpumask
.pcpu
, GFP_KERNEL
))
1022 if (!alloc_cpumask_var(&pinst
->cpumask
.cbcpu
, GFP_KERNEL
)) {
1023 free_cpumask_var(pinst
->cpumask
.pcpu
);
1026 if (!padata_validate_cpumask(pinst
, pcpumask
) ||
1027 !padata_validate_cpumask(pinst
, cbcpumask
))
1028 goto err_free_masks
;
1030 pd
= padata_alloc_pd(pinst
, pcpumask
, cbcpumask
);
1032 goto err_free_masks
;
1034 rcu_assign_pointer(pinst
->pd
, pd
);
1038 cpumask_copy(pinst
->cpumask
.pcpu
, pcpumask
);
1039 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 pinst
->cpu_notifier
.notifier_call
= padata_cpu_callback
;
1051 pinst
->cpu_notifier
.priority
= 0;
1052 register_hotcpu_notifier(&pinst
->cpu_notifier
);
1058 free_cpumask_var(pinst
->cpumask
.pcpu
);
1059 free_cpumask_var(pinst
->cpumask
.cbcpu
);
1066 EXPORT_SYMBOL(padata_alloc
);
1069 * padata_free - free a padata instance
1071 * @padata_inst: padata instance to free
1073 void padata_free(struct padata_instance
*pinst
)
1075 kobject_put(&pinst
->kobj
);
1077 EXPORT_SYMBOL(padata_free
);