]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - kernel/padata.c
padata: remove reorder_objects
[mirror_ubuntu-jammy-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);
bfde23ce
DJ
205 ++pd->processed;
206 pd->cpu = cpumask_next_wrap(cpu, pd->cpumask.pcpu, -1, false);
16295bec
SK
207 }
208
bfde23ce 209 spin_unlock(&reorder->lock);
16295bec
SK
210 return padata;
211}
212
213static void padata_reorder(struct parallel_data *pd)
214{
bbefa1dd 215 struct padata_instance *pinst = pd->ps->pinst;
3047817b 216 int cb_cpu;
16295bec 217 struct padata_priv *padata;
e15bacbe 218 struct padata_serial_queue *squeue;
6fc4dbcf 219 struct padata_parallel_queue *next_queue;
16295bec 220
0198ffd1
SK
221 /*
222 * We need to ensure that only one cpu can work on dequeueing of
223 * the reorder queue the time. Calculating in which percpu reorder
224 * queue the next object will arrive takes some time. A spinlock
225 * would be highly contended. Also it is not clear in which order
226 * the objects arrive to the reorder queues. So a cpu could wait to
227 * get the lock just to notice that there is nothing to do at the
228 * moment. Therefore we use a trylock and let the holder of the lock
229 * care for all the objects enqueued during the holdtime of the lock.
230 */
16295bec 231 if (!spin_trylock_bh(&pd->lock))
d46a5ac7 232 return;
16295bec
SK
233
234 while (1) {
bfde23ce 235 padata = padata_find_next(pd, true);
16295bec 236
0198ffd1 237 /*
69b34844
JD
238 * If the next object that needs serialization is parallel
239 * processed by another cpu and is still on it's way to the
240 * cpu's reorder queue, nothing to do for now.
0198ffd1 241 */
bfde23ce 242 if (!padata)
16295bec
SK
243 break;
244
3047817b
SK
245 cb_cpu = padata->cb_cpu;
246 squeue = per_cpu_ptr(pd->squeue, cb_cpu);
16295bec 247
e15bacbe
DK
248 spin_lock(&squeue->serial.lock);
249 list_add_tail(&padata->list, &squeue->serial.list);
250 spin_unlock(&squeue->serial.lock);
16295bec 251
45d153c0 252 queue_work_on(cb_cpu, pinst->serial_wq, &squeue->work);
16295bec
SK
253 }
254
255 spin_unlock_bh(&pd->lock);
256
0198ffd1
SK
257 /*
258 * The next object that needs serialization might have arrived to
6fc4dbcf 259 * the reorder queues in the meantime.
cf144f81 260 *
6fc4dbcf
HX
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
cf144f81 263 * smp_mb__after_atomic in padata_do_serial.
0198ffd1 264 */
cf144f81 265 smp_mb();
16295bec 266
6fc4dbcf 267 next_queue = per_cpu_ptr(pd->pqueue, pd->cpu);
bfde23ce
DJ
268 if (!list_empty(&next_queue->reorder.list) &&
269 padata_find_next(pd, false))
45d153c0 270 queue_work(pinst->serial_wq, &pd->reorder_work);
16295bec
SK
271}
272
cf5868c8
MK
273static void invoke_padata_reorder(struct work_struct *work)
274{
cf5868c8
MK
275 struct parallel_data *pd;
276
277 local_bh_disable();
6fc4dbcf 278 pd = container_of(work, struct parallel_data, reorder_work);
cf5868c8
MK
279 padata_reorder(pd);
280 local_bh_enable();
281}
282
e15bacbe 283static void padata_serial_worker(struct work_struct *serial_work)
16295bec 284{
e15bacbe 285 struct padata_serial_queue *squeue;
16295bec
SK
286 struct parallel_data *pd;
287 LIST_HEAD(local_list);
07928d9b 288 int cnt;
16295bec
SK
289
290 local_bh_disable();
e15bacbe
DK
291 squeue = container_of(serial_work, struct padata_serial_queue, work);
292 pd = squeue->pd;
16295bec 293
e15bacbe
DK
294 spin_lock(&squeue->serial.lock);
295 list_replace_init(&squeue->serial.list, &local_list);
296 spin_unlock(&squeue->serial.lock);
16295bec 297
07928d9b
HX
298 cnt = 0;
299
16295bec
SK
300 while (!list_empty(&local_list)) {
301 struct padata_priv *padata;
302
303 padata = list_entry(local_list.next,
304 struct padata_priv, list);
305
306 list_del_init(&padata->list);
307
308 padata->serial(padata);
07928d9b 309 cnt++;
16295bec
SK
310 }
311 local_bh_enable();
07928d9b
HX
312
313 if (atomic_sub_and_test(cnt, &pd->refcnt))
314 padata_free_pd(pd);
16295bec
SK
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{
065cf577
DJ
327 struct parallel_data *pd = padata->pd;
328 struct padata_parallel_queue *pqueue = per_cpu_ptr(pd->pqueue,
329 padata->cpu);
bfde23ce 330 struct padata_priv *cur;
16295bec 331
e15bacbe 332 spin_lock(&pqueue->reorder.lock);
bfde23ce
DJ
333 /* Sort in ascending order of sequence number. */
334 list_for_each_entry_reverse(cur, &pqueue->reorder.list, list)
335 if (cur->seq_nr < padata->seq_nr)
336 break;
337 list_add(&padata->list, &cur->list);
e15bacbe 338 spin_unlock(&pqueue->reorder.lock);
16295bec 339
cf144f81 340 /*
6fc4dbcf 341 * Ensure the addition to the reorder list is ordered correctly
cf144f81
DJ
342 * with the trylock of pd->lock in padata_reorder. Pairs with smp_mb
343 * in padata_reorder.
344 */
345 smp_mb__after_atomic();
346
6fc4dbcf 347 padata_reorder(pd);
16295bec
SK
348}
349EXPORT_SYMBOL(padata_do_serial);
350
bbefa1dd 351static int padata_setup_cpumasks(struct padata_instance *pinst)
16295bec 352{
bfde23ce 353 struct workqueue_attrs *attrs;
bbefa1dd
HX
354 int err;
355
356 attrs = alloc_workqueue_attrs();
357 if (!attrs)
358 return -ENOMEM;
359
360 /* Restrict parallel_wq workers to pd->cpumask.pcpu. */
361 cpumask_copy(attrs->cpumask, pinst->cpumask.pcpu);
362 err = apply_workqueue_attrs(pinst->parallel_wq, attrs);
363 free_workqueue_attrs(attrs);
364
365 return err;
366}
367
368static int pd_setup_cpumasks(struct parallel_data *pd,
369 const struct cpumask *pcpumask,
370 const struct cpumask *cbcpumask)
371{
bfde23ce 372 int err = -ENOMEM;
16295bec 373
bfde23ce
DJ
374 if (!alloc_cpumask_var(&pd->cpumask.pcpu, GFP_KERNEL))
375 goto out;
bfde23ce
DJ
376 if (!alloc_cpumask_var(&pd->cpumask.cbcpu, GFP_KERNEL))
377 goto free_pcpu_mask;
bfde23ce 378
bbefa1dd
HX
379 cpumask_copy(pd->cpumask.pcpu, pcpumask);
380 cpumask_copy(pd->cpumask.cbcpu, cbcpumask);
bfde23ce 381
e15bacbe 382 return 0;
bfde23ce 383
bfde23ce
DJ
384free_pcpu_mask:
385 free_cpumask_var(pd->cpumask.pcpu);
386out:
387 return err;
e15bacbe 388}
16295bec 389
e15bacbe
DK
390static void __padata_list_init(struct padata_list *pd_list)
391{
392 INIT_LIST_HEAD(&pd_list->list);
393 spin_lock_init(&pd_list->lock);
394}
16295bec 395
e15bacbe
DK
396/* Initialize all percpu queues used by serial workers */
397static void padata_init_squeues(struct parallel_data *pd)
398{
399 int cpu;
400 struct padata_serial_queue *squeue;
7b389b2c 401
e15bacbe
DK
402 for_each_cpu(cpu, pd->cpumask.cbcpu) {
403 squeue = per_cpu_ptr(pd->squeue, cpu);
404 squeue->pd = pd;
405 __padata_list_init(&squeue->serial);
406 INIT_WORK(&squeue->work, padata_serial_worker);
407 }
408}
16295bec 409
e15bacbe
DK
410/* Initialize all percpu queues used by parallel workers */
411static void padata_init_pqueues(struct parallel_data *pd)
412{
c51636a3 413 int cpu;
e15bacbe 414 struct padata_parallel_queue *pqueue;
16295bec 415
c51636a3 416 for_each_cpu(cpu, pd->cpumask.pcpu) {
e15bacbe 417 pqueue = per_cpu_ptr(pd->pqueue, cpu);
1bd845bc 418
e15bacbe
DK
419 __padata_list_init(&pqueue->reorder);
420 __padata_list_init(&pqueue->parallel);
421 INIT_WORK(&pqueue->work, padata_parallel_worker);
422 atomic_set(&pqueue->num_obj, 0);
16295bec 423 }
e15bacbe 424}
16295bec 425
e15bacbe 426/* Allocate and initialize the internal cpumask dependend resources. */
bbefa1dd 427static struct parallel_data *padata_alloc_pd(struct padata_shell *ps)
e15bacbe 428{
bbefa1dd
HX
429 struct padata_instance *pinst = ps->pinst;
430 const struct cpumask *cbcpumask;
431 const struct cpumask *pcpumask;
e15bacbe 432 struct parallel_data *pd;
16295bec 433
bbefa1dd
HX
434 cbcpumask = pinst->rcpumask.cbcpu;
435 pcpumask = pinst->rcpumask.pcpu;
436
e15bacbe
DK
437 pd = kzalloc(sizeof(struct parallel_data), GFP_KERNEL);
438 if (!pd)
439 goto err;
16295bec 440
e15bacbe
DK
441 pd->pqueue = alloc_percpu(struct padata_parallel_queue);
442 if (!pd->pqueue)
443 goto err_free_pd;
444
445 pd->squeue = alloc_percpu(struct padata_serial_queue);
446 if (!pd->squeue)
447 goto err_free_pqueue;
bfde23ce 448
bbefa1dd
HX
449 pd->ps = ps;
450 if (pd_setup_cpumasks(pd, pcpumask, cbcpumask))
e15bacbe 451 goto err_free_squeue;
16295bec 452
e15bacbe
DK
453 padata_init_pqueues(pd);
454 padata_init_squeues(pd);
0b6b098e 455 atomic_set(&pd->seq_nr, -1);
07928d9b 456 atomic_set(&pd->refcnt, 1);
16295bec 457 spin_lock_init(&pd->lock);
ec9c7d19 458 pd->cpu = cpumask_first(pd->cpumask.pcpu);
6fc4dbcf 459 INIT_WORK(&pd->reorder_work, invoke_padata_reorder);
16295bec
SK
460
461 return pd;
462
e15bacbe
DK
463err_free_squeue:
464 free_percpu(pd->squeue);
465err_free_pqueue:
466 free_percpu(pd->pqueue);
16295bec
SK
467err_free_pd:
468 kfree(pd);
469err:
470 return NULL;
471}
472
473static void padata_free_pd(struct parallel_data *pd)
474{
e15bacbe
DK
475 free_cpumask_var(pd->cpumask.pcpu);
476 free_cpumask_var(pd->cpumask.cbcpu);
477 free_percpu(pd->pqueue);
478 free_percpu(pd->squeue);
16295bec
SK
479 kfree(pd);
480}
481
4c879170
SK
482static void __padata_start(struct padata_instance *pinst)
483{
484 pinst->flags |= PADATA_INIT;
485}
486
ee836555
SK
487static void __padata_stop(struct padata_instance *pinst)
488{
489 if (!(pinst->flags & PADATA_INIT))
490 return;
491
492 pinst->flags &= ~PADATA_INIT;
493
494 synchronize_rcu();
ee836555
SK
495}
496
25985edc 497/* Replace the internal control structure with a new one. */
bbefa1dd 498static int padata_replace_one(struct padata_shell *ps)
16295bec 499{
bbefa1dd 500 struct parallel_data *pd_new;
16295bec 501
bbefa1dd
HX
502 pd_new = padata_alloc_pd(ps);
503 if (!pd_new)
504 return -ENOMEM;
16295bec 505
bbefa1dd
HX
506 ps->opd = rcu_dereference_protected(ps->pd, 1);
507 rcu_assign_pointer(ps->pd, pd_new);
16295bec 508
bbefa1dd
HX
509 return 0;
510}
511
894c9ef9 512static int padata_replace(struct padata_instance *pinst)
bbefa1dd 513{
bbefa1dd
HX
514 struct padata_shell *ps;
515 int err;
516
517 pinst->flags |= PADATA_RESET;
16295bec 518
bbefa1dd
HX
519 cpumask_and(pinst->rcpumask.pcpu, pinst->cpumask.pcpu,
520 cpu_online_mask);
bbefa1dd 521
bbefa1dd
HX
522 cpumask_and(pinst->rcpumask.cbcpu, pinst->cpumask.cbcpu,
523 cpu_online_mask);
e15bacbe 524
bbefa1dd
HX
525 list_for_each_entry(ps, &pinst->pslist, list) {
526 err = padata_replace_one(ps);
527 if (err)
528 break;
529 }
530
531 synchronize_rcu();
532
533 list_for_each_entry_continue_reverse(ps, &pinst->pslist, list)
534 if (atomic_dec_and_test(&ps->opd->refcnt))
535 padata_free_pd(ps->opd);
16295bec
SK
536
537 pinst->flags &= ~PADATA_RESET;
bbefa1dd
HX
538
539 return err;
16295bec
SK
540}
541
33e54450
SK
542/* If cpumask contains no active cpu, we mark the instance as invalid. */
543static bool padata_validate_cpumask(struct padata_instance *pinst,
544 const struct cpumask *cpumask)
545{
13614e0f 546 if (!cpumask_intersects(cpumask, cpu_online_mask)) {
33e54450
SK
547 pinst->flags |= PADATA_INVALID;
548 return false;
549 }
550
551 pinst->flags &= ~PADATA_INVALID;
552 return true;
553}
554
65ff577e
SK
555static int __padata_set_cpumasks(struct padata_instance *pinst,
556 cpumask_var_t pcpumask,
557 cpumask_var_t cbcpumask)
558{
559 int valid;
bbefa1dd 560 int err;
65ff577e
SK
561
562 valid = padata_validate_cpumask(pinst, pcpumask);
563 if (!valid) {
564 __padata_stop(pinst);
565 goto out_replace;
566 }
567
568 valid = padata_validate_cpumask(pinst, cbcpumask);
569 if (!valid)
570 __padata_stop(pinst);
571
572out_replace:
65ff577e
SK
573 cpumask_copy(pinst->cpumask.pcpu, pcpumask);
574 cpumask_copy(pinst->cpumask.cbcpu, cbcpumask);
575
894c9ef9 576 err = padata_setup_cpumasks(pinst) ?: padata_replace(pinst);
65ff577e
SK
577
578 if (valid)
579 __padata_start(pinst);
580
bbefa1dd 581 return err;
65ff577e
SK
582}
583
e15bacbe
DK
584/**
585 * padata_set_cpumask: Sets specified by @cpumask_type cpumask to the value
586 * equivalent to @cpumask.
16295bec
SK
587 *
588 * @pinst: padata instance
e15bacbe
DK
589 * @cpumask_type: PADATA_CPU_SERIAL or PADATA_CPU_PARALLEL corresponding
590 * to parallel and serial cpumasks respectively.
16295bec
SK
591 * @cpumask: the cpumask to use
592 */
e15bacbe
DK
593int padata_set_cpumask(struct padata_instance *pinst, int cpumask_type,
594 cpumask_var_t cpumask)
595{
596 struct cpumask *serial_mask, *parallel_mask;
65ff577e
SK
597 int err = -EINVAL;
598
6751fb3c 599 get_online_cpus();
38228e88 600 mutex_lock(&pinst->lock);
6751fb3c 601
e15bacbe
DK
602 switch (cpumask_type) {
603 case PADATA_CPU_PARALLEL:
604 serial_mask = pinst->cpumask.cbcpu;
605 parallel_mask = cpumask;
606 break;
607 case PADATA_CPU_SERIAL:
608 parallel_mask = pinst->cpumask.pcpu;
609 serial_mask = cpumask;
610 break;
611 default:
65ff577e 612 goto out;
16295bec
SK
613 }
614
65ff577e 615 err = __padata_set_cpumasks(pinst, parallel_mask, serial_mask);
16295bec
SK
616
617out:
618 mutex_unlock(&pinst->lock);
38228e88 619 put_online_cpus();
16295bec
SK
620
621 return err;
622}
623EXPORT_SYMBOL(padata_set_cpumask);
624
19d795b6
AB
625/**
626 * padata_start - start the parallel processing
627 *
628 * @pinst: padata instance to start
629 */
630int padata_start(struct padata_instance *pinst)
631{
632 int err = 0;
633
634 mutex_lock(&pinst->lock);
635
636 if (pinst->flags & PADATA_INVALID)
637 err = -EINVAL;
638
8ddab428 639 __padata_start(pinst);
19d795b6
AB
640
641 mutex_unlock(&pinst->lock);
642
643 return err;
644}
645EXPORT_SYMBOL(padata_start);
646
647/**
648 * padata_stop - stop the parallel processing
649 *
650 * @pinst: padata instance to stop
651 */
652void padata_stop(struct padata_instance *pinst)
653{
654 mutex_lock(&pinst->lock);
655 __padata_stop(pinst);
656 mutex_unlock(&pinst->lock);
657}
658EXPORT_SYMBOL(padata_stop);
659
660#ifdef CONFIG_HOTPLUG_CPU
661
16295bec
SK
662static int __padata_add_cpu(struct padata_instance *pinst, int cpu)
663{
bbefa1dd 664 int err = 0;
16295bec 665
13614e0f 666 if (cpumask_test_cpu(cpu, cpu_online_mask)) {
894c9ef9 667 err = padata_replace(pinst);
33e54450 668
e15bacbe
DK
669 if (padata_validate_cpumask(pinst, pinst->cpumask.pcpu) &&
670 padata_validate_cpumask(pinst, pinst->cpumask.cbcpu))
33e54450 671 __padata_start(pinst);
16295bec
SK
672 }
673
bbefa1dd 674 return err;
16295bec
SK
675}
676
16295bec
SK
677static int __padata_remove_cpu(struct padata_instance *pinst, int cpu)
678{
bbefa1dd 679 int err = 0;
16295bec 680
894c9ef9 681 if (!cpumask_test_cpu(cpu, cpu_online_mask)) {
e15bacbe 682 if (!padata_validate_cpumask(pinst, pinst->cpumask.pcpu) ||
b89661df 683 !padata_validate_cpumask(pinst, pinst->cpumask.cbcpu))
33e54450 684 __padata_stop(pinst);
33e54450 685
894c9ef9 686 err = padata_replace(pinst);
16295bec
SK
687 }
688
bbefa1dd 689 return err;
16295bec
SK
690}
691
e15bacbe
DK
692static inline int pinst_has_cpu(struct padata_instance *pinst, int cpu)
693{
694 return cpumask_test_cpu(cpu, pinst->cpumask.pcpu) ||
695 cpumask_test_cpu(cpu, pinst->cpumask.cbcpu);
696}
697
30e92153 698static int padata_cpu_online(unsigned int cpu, struct hlist_node *node)
16295bec 699{
16295bec 700 struct padata_instance *pinst;
30e92153 701 int ret;
16295bec 702
30e92153
SAS
703 pinst = hlist_entry_safe(node, struct padata_instance, node);
704 if (!pinst_has_cpu(pinst, cpu))
705 return 0;
16295bec 706
30e92153
SAS
707 mutex_lock(&pinst->lock);
708 ret = __padata_add_cpu(pinst, cpu);
709 mutex_unlock(&pinst->lock);
710 return ret;
711}
16295bec 712
894c9ef9 713static int padata_cpu_dead(unsigned int cpu, struct hlist_node *node)
30e92153
SAS
714{
715 struct padata_instance *pinst;
716 int ret;
717
718 pinst = hlist_entry_safe(node, struct padata_instance, node);
719 if (!pinst_has_cpu(pinst, cpu))
720 return 0;
16295bec 721
30e92153
SAS
722 mutex_lock(&pinst->lock);
723 ret = __padata_remove_cpu(pinst, cpu);
724 mutex_unlock(&pinst->lock);
725 return ret;
16295bec 726}
30e92153
SAS
727
728static enum cpuhp_state hp_online;
e2cb2f1c 729#endif
16295bec 730
5e017dc3
DK
731static void __padata_free(struct padata_instance *pinst)
732{
733#ifdef CONFIG_HOTPLUG_CPU
894c9ef9 734 cpuhp_state_remove_instance_nocalls(CPUHP_PADATA_DEAD, &pinst->node);
30e92153 735 cpuhp_state_remove_instance_nocalls(hp_online, &pinst->node);
5e017dc3
DK
736#endif
737
bbefa1dd
HX
738 WARN_ON(!list_empty(&pinst->pslist));
739
5e017dc3 740 padata_stop(pinst);
bbefa1dd
HX
741 free_cpumask_var(pinst->rcpumask.cbcpu);
742 free_cpumask_var(pinst->rcpumask.pcpu);
5e017dc3
DK
743 free_cpumask_var(pinst->cpumask.pcpu);
744 free_cpumask_var(pinst->cpumask.cbcpu);
45d153c0
DJ
745 destroy_workqueue(pinst->serial_wq);
746 destroy_workqueue(pinst->parallel_wq);
5e017dc3
DK
747 kfree(pinst);
748}
749
750#define kobj2pinst(_kobj) \
751 container_of(_kobj, struct padata_instance, kobj)
752#define attr2pentry(_attr) \
753 container_of(_attr, struct padata_sysfs_entry, attr)
754
755static void padata_sysfs_release(struct kobject *kobj)
756{
757 struct padata_instance *pinst = kobj2pinst(kobj);
758 __padata_free(pinst);
759}
760
761struct padata_sysfs_entry {
762 struct attribute attr;
763 ssize_t (*show)(struct padata_instance *, struct attribute *, char *);
764 ssize_t (*store)(struct padata_instance *, struct attribute *,
765 const char *, size_t);
766};
767
768static ssize_t show_cpumask(struct padata_instance *pinst,
769 struct attribute *attr, char *buf)
770{
771 struct cpumask *cpumask;
772 ssize_t len;
773
774 mutex_lock(&pinst->lock);
775 if (!strcmp(attr->name, "serial_cpumask"))
776 cpumask = pinst->cpumask.cbcpu;
777 else
778 cpumask = pinst->cpumask.pcpu;
779
4497da6f
TH
780 len = snprintf(buf, PAGE_SIZE, "%*pb\n",
781 nr_cpu_ids, cpumask_bits(cpumask));
5e017dc3 782 mutex_unlock(&pinst->lock);
4497da6f 783 return len < PAGE_SIZE ? len : -EINVAL;
5e017dc3
DK
784}
785
786static ssize_t store_cpumask(struct padata_instance *pinst,
787 struct attribute *attr,
788 const char *buf, size_t count)
789{
790 cpumask_var_t new_cpumask;
791 ssize_t ret;
792 int mask_type;
793
794 if (!alloc_cpumask_var(&new_cpumask, GFP_KERNEL))
795 return -ENOMEM;
796
797 ret = bitmap_parse(buf, count, cpumask_bits(new_cpumask),
798 nr_cpumask_bits);
799 if (ret < 0)
800 goto out;
801
802 mask_type = !strcmp(attr->name, "serial_cpumask") ?
803 PADATA_CPU_SERIAL : PADATA_CPU_PARALLEL;
804 ret = padata_set_cpumask(pinst, mask_type, new_cpumask);
805 if (!ret)
806 ret = count;
807
808out:
809 free_cpumask_var(new_cpumask);
810 return ret;
811}
812
813#define PADATA_ATTR_RW(_name, _show_name, _store_name) \
814 static struct padata_sysfs_entry _name##_attr = \
815 __ATTR(_name, 0644, _show_name, _store_name)
816#define PADATA_ATTR_RO(_name, _show_name) \
817 static struct padata_sysfs_entry _name##_attr = \
818 __ATTR(_name, 0400, _show_name, NULL)
819
820PADATA_ATTR_RW(serial_cpumask, show_cpumask, store_cpumask);
821PADATA_ATTR_RW(parallel_cpumask, show_cpumask, store_cpumask);
822
823/*
824 * Padata sysfs provides the following objects:
825 * serial_cpumask [RW] - cpumask for serial workers
826 * parallel_cpumask [RW] - cpumask for parallel workers
827 */
828static struct attribute *padata_default_attrs[] = {
829 &serial_cpumask_attr.attr,
830 &parallel_cpumask_attr.attr,
831 NULL,
832};
2064fbc7 833ATTRIBUTE_GROUPS(padata_default);
5e017dc3
DK
834
835static ssize_t padata_sysfs_show(struct kobject *kobj,
836 struct attribute *attr, char *buf)
837{
838 struct padata_instance *pinst;
839 struct padata_sysfs_entry *pentry;
840 ssize_t ret = -EIO;
841
842 pinst = kobj2pinst(kobj);
843 pentry = attr2pentry(attr);
844 if (pentry->show)
845 ret = pentry->show(pinst, attr, buf);
846
847 return ret;
848}
849
850static ssize_t padata_sysfs_store(struct kobject *kobj, struct attribute *attr,
851 const char *buf, size_t count)
852{
853 struct padata_instance *pinst;
854 struct padata_sysfs_entry *pentry;
855 ssize_t ret = -EIO;
856
857 pinst = kobj2pinst(kobj);
858 pentry = attr2pentry(attr);
859 if (pentry->show)
860 ret = pentry->store(pinst, attr, buf, count);
861
862 return ret;
863}
864
865static const struct sysfs_ops padata_sysfs_ops = {
866 .show = padata_sysfs_show,
867 .store = padata_sysfs_store,
868};
869
870static struct kobj_type padata_attr_type = {
871 .sysfs_ops = &padata_sysfs_ops,
2064fbc7 872 .default_groups = padata_default_groups,
5e017dc3
DK
873 .release = padata_sysfs_release,
874};
875
e15bacbe 876/**
e6cc1170
SK
877 * padata_alloc - allocate and initialize a padata instance and specify
878 * cpumasks for serial and parallel workers.
16295bec 879 *
b128a304 880 * @name: used to identify the instance
e15bacbe
DK
881 * @pcpumask: cpumask that will be used for padata parallelization
882 * @cbcpumask: cpumask that will be used for padata serialization
16295bec 883 */
b128a304 884static struct padata_instance *padata_alloc(const char *name,
9596695e
TG
885 const struct cpumask *pcpumask,
886 const struct cpumask *cbcpumask)
16295bec 887{
16295bec 888 struct padata_instance *pinst;
16295bec
SK
889
890 pinst = kzalloc(sizeof(struct padata_instance), GFP_KERNEL);
891 if (!pinst)
892 goto err;
893
bfde23ce
DJ
894 pinst->parallel_wq = alloc_workqueue("%s_parallel", WQ_UNBOUND, 0,
895 name);
45d153c0 896 if (!pinst->parallel_wq)
16295bec 897 goto err_free_inst;
b128a304 898
cc491d8e
DJ
899 get_online_cpus();
900
45d153c0
DJ
901 pinst->serial_wq = alloc_workqueue("%s_serial", WQ_MEM_RECLAIM |
902 WQ_CPU_INTENSIVE, 1, name);
903 if (!pinst->serial_wq)
cc491d8e 904 goto err_put_cpus;
45d153c0
DJ
905
906 if (!alloc_cpumask_var(&pinst->cpumask.pcpu, GFP_KERNEL))
907 goto err_free_serial_wq;
e15bacbe
DK
908 if (!alloc_cpumask_var(&pinst->cpumask.cbcpu, GFP_KERNEL)) {
909 free_cpumask_var(pinst->cpumask.pcpu);
45d153c0 910 goto err_free_serial_wq;
33e54450 911 }
e15bacbe
DK
912 if (!padata_validate_cpumask(pinst, pcpumask) ||
913 !padata_validate_cpumask(pinst, cbcpumask))
914 goto err_free_masks;
16295bec 915
bbefa1dd 916 if (!alloc_cpumask_var(&pinst->rcpumask.pcpu, GFP_KERNEL))
e15bacbe 917 goto err_free_masks;
bbefa1dd
HX
918 if (!alloc_cpumask_var(&pinst->rcpumask.cbcpu, GFP_KERNEL))
919 goto err_free_rcpumask_pcpu;
74781387 920
bbefa1dd 921 INIT_LIST_HEAD(&pinst->pslist);
16295bec 922
e15bacbe
DK
923 cpumask_copy(pinst->cpumask.pcpu, pcpumask);
924 cpumask_copy(pinst->cpumask.cbcpu, cbcpumask);
bbefa1dd
HX
925 cpumask_and(pinst->rcpumask.pcpu, pcpumask, cpu_online_mask);
926 cpumask_and(pinst->rcpumask.cbcpu, cbcpumask, cpu_online_mask);
927
928 if (padata_setup_cpumasks(pinst))
91a71d61 929 goto err_free_rcpumask_cbcpu;
16295bec
SK
930
931 pinst->flags = 0;
932
5e017dc3 933 kobject_init(&pinst->kobj, &padata_attr_type);
16295bec
SK
934 mutex_init(&pinst->lock);
935
b8b4a416 936#ifdef CONFIG_HOTPLUG_CPU
c5a81c8f 937 cpuhp_state_add_instance_nocalls_cpuslocked(hp_online, &pinst->node);
894c9ef9
DJ
938 cpuhp_state_add_instance_nocalls_cpuslocked(CPUHP_PADATA_DEAD,
939 &pinst->node);
b8b4a416 940#endif
cc491d8e
DJ
941
942 put_online_cpus();
943
16295bec
SK
944 return pinst;
945
bbefa1dd
HX
946err_free_rcpumask_cbcpu:
947 free_cpumask_var(pinst->rcpumask.cbcpu);
948err_free_rcpumask_pcpu:
949 free_cpumask_var(pinst->rcpumask.pcpu);
e15bacbe
DK
950err_free_masks:
951 free_cpumask_var(pinst->cpumask.pcpu);
952 free_cpumask_var(pinst->cpumask.cbcpu);
45d153c0
DJ
953err_free_serial_wq:
954 destroy_workqueue(pinst->serial_wq);
cc491d8e
DJ
955err_put_cpus:
956 put_online_cpus();
45d153c0 957 destroy_workqueue(pinst->parallel_wq);
16295bec
SK
958err_free_inst:
959 kfree(pinst);
960err:
961 return NULL;
962}
16295bec 963
9596695e
TG
964/**
965 * padata_alloc_possible - Allocate and initialize padata instance.
966 * Use the cpu_possible_mask for serial and
967 * parallel workers.
968 *
b128a304 969 * @name: used to identify the instance
9596695e 970 */
b128a304 971struct padata_instance *padata_alloc_possible(const char *name)
9596695e 972{
b128a304 973 return padata_alloc(name, cpu_possible_mask, cpu_possible_mask);
9596695e
TG
974}
975EXPORT_SYMBOL(padata_alloc_possible);
976
0198ffd1 977/**
16295bec
SK
978 * padata_free - free a padata instance
979 *
0198ffd1 980 * @padata_inst: padata instance to free
16295bec
SK
981 */
982void padata_free(struct padata_instance *pinst)
983{
5e017dc3 984 kobject_put(&pinst->kobj);
16295bec
SK
985}
986EXPORT_SYMBOL(padata_free);
30e92153 987
bbefa1dd
HX
988/**
989 * padata_alloc_shell - Allocate and initialize padata shell.
990 *
991 * @pinst: Parent padata_instance object.
992 */
993struct padata_shell *padata_alloc_shell(struct padata_instance *pinst)
994{
995 struct parallel_data *pd;
996 struct padata_shell *ps;
997
998 ps = kzalloc(sizeof(*ps), GFP_KERNEL);
999 if (!ps)
1000 goto out;
1001
1002 ps->pinst = pinst;
1003
1004 get_online_cpus();
1005 pd = padata_alloc_pd(ps);
1006 put_online_cpus();
1007
1008 if (!pd)
1009 goto out_free_ps;
1010
1011 mutex_lock(&pinst->lock);
1012 RCU_INIT_POINTER(ps->pd, pd);
1013 list_add(&ps->list, &pinst->pslist);
1014 mutex_unlock(&pinst->lock);
1015
1016 return ps;
1017
1018out_free_ps:
1019 kfree(ps);
1020out:
1021 return NULL;
1022}
1023EXPORT_SYMBOL(padata_alloc_shell);
1024
1025/**
1026 * padata_free_shell - free a padata shell
1027 *
1028 * @ps: padata shell to free
1029 */
1030void padata_free_shell(struct padata_shell *ps)
1031{
1032 struct padata_instance *pinst = ps->pinst;
1033
1034 mutex_lock(&pinst->lock);
1035 list_del(&ps->list);
1036 padata_free_pd(rcu_dereference_protected(ps->pd, 1));
1037 mutex_unlock(&pinst->lock);
1038
1039 kfree(ps);
1040}
1041EXPORT_SYMBOL(padata_free_shell);
1042
30e92153
SAS
1043#ifdef CONFIG_HOTPLUG_CPU
1044
1045static __init int padata_driver_init(void)
1046{
1047 int ret;
1048
1049 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "padata:online",
894c9ef9 1050 padata_cpu_online, NULL);
30e92153
SAS
1051 if (ret < 0)
1052 return ret;
1053 hp_online = ret;
894c9ef9
DJ
1054
1055 ret = cpuhp_setup_state_multi(CPUHP_PADATA_DEAD, "padata:dead",
1056 NULL, padata_cpu_dead);
1057 if (ret < 0) {
1058 cpuhp_remove_multi_state(hp_online);
1059 return ret;
1060 }
30e92153
SAS
1061 return 0;
1062}
1063module_init(padata_driver_init);
1064
1065static __exit void padata_driver_exit(void)
1066{
894c9ef9 1067 cpuhp_remove_multi_state(CPUHP_PADATA_DEAD);
30e92153
SAS
1068 cpuhp_remove_multi_state(hp_online);
1069}
1070module_exit(padata_driver_exit);
1071#endif