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 void padata_free_pd(struct parallel_data
*pd
);
39 static int padata_index_to_cpu(struct parallel_data
*pd
, int cpu_index
)
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
);
50 static int padata_cpu_hash(struct parallel_data
*pd
)
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
);
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
);
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
)
109 struct padata_parallel_queue
*queue
;
110 struct parallel_data
*pd
;
114 pd
= rcu_dereference_bh(pinst
->pd
);
117 if (!(pinst
->flags
& PADATA_INIT
) || pinst
->flags
& PADATA_INVALID
)
120 if (!cpumask_test_cpu(cb_cpu
, pd
->cpumask
.cbcpu
))
124 if ((pinst
->flags
& PADATA_RESET
))
127 if (atomic_read(&pd
->refcnt
) >= MAX_OBJ_NUM
)
131 atomic_inc(&pd
->refcnt
);
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
);
146 rcu_read_unlock_bh();
150 EXPORT_SYMBOL(padata_do_parallel
);
153 * padata_get_next - Get the next object that needs serialization.
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
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
;
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,
188 spin_unlock(&reorder
->lock
);
191 spin_unlock(&reorder
->lock
);
193 if (__this_cpu_read(pd
->pqueue
->cpu_index
) == next_queue
->cpu_index
) {
194 padata
= ERR_PTR(-ENODATA
);
198 padata
= ERR_PTR(-EINPROGRESS
);
203 static void padata_reorder(struct parallel_data
*pd
)
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
))
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
)
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
);
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.
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
;
277 pd
= container_of(work
, struct parallel_data
, reorder_work
);
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
);
290 squeue
= container_of(serial_work
, struct padata_serial_queue
, work
);
293 spin_lock(&squeue
->serial
.lock
);
294 list_replace_init(&squeue
->serial
.list
, &local_list
);
295 spin_unlock(&squeue
->serial
.lock
);
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
);
312 if (atomic_sub_and_test(cnt
, &pd
->refcnt
))
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
,
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
340 smp_mb__after_atomic();
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
))
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
);
359 cpumask_and(pd
->cpumask
.cbcpu
, cbcpumask
, cpu_online_mask
);
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
)
373 struct padata_serial_queue
*squeue
;
375 for_each_cpu(cpu
, pd
->cpumask
.cbcpu
) {
376 squeue
= per_cpu_ptr(pd
->squeue
, cpu
);
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
)
387 struct padata_parallel_queue
*pqueue
;
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;
398 pqueue
->cpu_index
= 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
);
419 pd
->pqueue
= alloc_percpu(struct padata_parallel_queue
);
423 pd
->squeue
= alloc_percpu(struct padata_serial_queue
);
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);
435 spin_lock_init(&pd
->lock
);
436 pd
->cpu
= cpumask_first(pd
->cpumask
.pcpu
);
437 INIT_WORK(&pd
->reorder_work
, invoke_padata_reorder
);
442 free_percpu(pd
->squeue
);
444 free_percpu(pd
->pqueue
);
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
);
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
))
470 pinst
->flags
&= ~PADATA_INIT
;
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
);
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
,
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
,
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
,
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
;
545 pinst
->flags
&= ~PADATA_INVALID
;
549 static int __padata_set_cpumasks(struct padata_instance
*pinst
,
550 cpumask_var_t pcpumask
,
551 cpumask_var_t cbcpumask
)
554 struct parallel_data
*pd
;
556 valid
= padata_validate_cpumask(pinst
, pcpumask
);
558 __padata_stop(pinst
);
562 valid
= padata_validate_cpumask(pinst
, cbcpumask
);
564 __padata_stop(pinst
);
567 pd
= padata_alloc_pd(pinst
, pcpumask
, cbcpumask
);
571 cpumask_copy(pinst
->cpumask
.pcpu
, pcpumask
);
572 cpumask_copy(pinst
->cpumask
.cbcpu
, cbcpumask
);
574 padata_replace(pinst
, pd
);
577 __padata_start(pinst
);
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
;
598 mutex_lock(&pinst
->lock
);
600 switch (cpumask_type
) {
601 case PADATA_CPU_PARALLEL
:
602 serial_mask
= pinst
->cpumask
.cbcpu
;
603 parallel_mask
= cpumask
;
605 case PADATA_CPU_SERIAL
:
606 parallel_mask
= pinst
->cpumask
.pcpu
;
607 serial_mask
= cpumask
;
613 err
= __padata_set_cpumasks(pinst
, parallel_mask
, serial_mask
);
616 mutex_unlock(&pinst
->lock
);
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
)
632 mutex_lock(&pinst
->lock
);
634 if (pinst
->flags
& PADATA_INVALID
)
637 __padata_start(pinst
);
639 mutex_unlock(&pinst
->lock
);
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
);
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
);
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
);
695 padata_replace(pinst
, pd
);
697 cpumask_clear_cpu(cpu
, pd
->cpumask
.cbcpu
);
698 cpumask_clear_cpu(cpu
, pd
->cpumask
.pcpu
);
705 * padata_remove_cpu - remove a cpu from the one or both(serial and parallel)
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
)
719 if (!(mask
& (PADATA_CPU_SERIAL
| PADATA_CPU_PARALLEL
)))
722 mutex_lock(&pinst
->lock
);
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
);
733 mutex_unlock(&pinst
->lock
);
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
;
750 pinst
= hlist_entry_safe(node
, struct padata_instance
, node
);
751 if (!pinst_has_cpu(pinst
, cpu
))
754 mutex_lock(&pinst
->lock
);
755 ret
= __padata_add_cpu(pinst
, cpu
);
756 mutex_unlock(&pinst
->lock
);
760 static int padata_cpu_prep_down(unsigned int cpu
, struct hlist_node
*node
)
762 struct padata_instance
*pinst
;
765 pinst
= hlist_entry_safe(node
, struct padata_instance
, node
);
766 if (!pinst_has_cpu(pinst
, cpu
))
769 mutex_lock(&pinst
->lock
);
770 ret
= __padata_remove_cpu(pinst
, cpu
);
771 mutex_unlock(&pinst
->lock
);
775 static enum cpuhp_state hp_online
;
778 static void __padata_free(struct padata_instance
*pinst
)
780 #ifdef CONFIG_HOTPLUG_CPU
781 cpuhp_state_remove_instance_nocalls(hp_online
, &pinst
->node
);
785 padata_free_pd(pinst
->pd
);
786 free_cpumask_var(pinst
->cpumask
.pcpu
);
787 free_cpumask_var(pinst
->cpumask
.cbcpu
);
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
;
815 mutex_lock(&pinst
->lock
);
816 if (!strcmp(attr
->name
, "serial_cpumask"))
817 cpumask
= pinst
->cpumask
.cbcpu
;
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
;
835 if (!alloc_cpumask_var(&new_cpumask
, GFP_KERNEL
))
838 ret
= bitmap_parse(buf
, count
, cpumask_bits(new_cpumask
),
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
);
850 free_cpumask_var(new_cpumask
);
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 ¶llel_cpumask_attr
.attr
,
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
;
882 pinst
= kobj2pinst(kobj
);
883 pentry
= attr2pentry(attr
);
885 ret
= pentry
->show(pinst
, attr
, buf
);
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
;
897 pinst
= kobj2pinst(kobj
);
898 pentry
= attr2pentry(attr
);
900 ret
= pentry
->store(pinst
, attr
, buf
, count
);
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
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
);
949 if (!alloc_cpumask_var(&pinst
->cpumask
.pcpu
, GFP_KERNEL
))
951 if (!alloc_cpumask_var(&pinst
->cpumask
.cbcpu
, GFP_KERNEL
)) {
952 free_cpumask_var(pinst
->cpumask
.pcpu
);
955 if (!padata_validate_cpumask(pinst
, pcpumask
) ||
956 !padata_validate_cpumask(pinst
, cbcpumask
))
959 pd
= padata_alloc_pd(pinst
, pcpumask
, cbcpumask
);
963 rcu_assign_pointer(pinst
->pd
, pd
);
967 cpumask_copy(pinst
->cpumask
.pcpu
, pcpumask
);
968 cpumask_copy(pinst
->cpumask
.cbcpu
, cbcpumask
);
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
);
984 free_cpumask_var(pinst
->cpumask
.pcpu
);
985 free_cpumask_var(pinst
->cpumask
.cbcpu
);
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)
1010 ret
= cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN
, "padata:online",
1012 padata_cpu_prep_down
);
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
);