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