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