xref: /linux/kernel/padata.c (revision 44a8c96edd0ee9320a1ad87afc7b10f38e55d5ec)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * padata.c - generic interface to process data streams in parallel
4  *
5  * See Documentation/core-api/padata.rst for more information.
6  *
7  * Copyright (C) 2008, 2009 secunet Security Networks AG
8  * Copyright (C) 2008, 2009 Steffen Klassert <steffen.klassert@secunet.com>
9  *
10  * Copyright (c) 2020 Oracle and/or its affiliates.
11  * Author: Daniel Jordan <daniel.m.jordan@oracle.com>
12  */
13 
14 #include <linux/completion.h>
15 #include <linux/export.h>
16 #include <linux/cpumask.h>
17 #include <linux/err.h>
18 #include <linux/cpu.h>
19 #include <linux/padata.h>
20 #include <linux/mutex.h>
21 #include <linux/sched.h>
22 #include <linux/slab.h>
23 #include <linux/sysfs.h>
24 #include <linux/rcupdate.h>
25 
26 #define	PADATA_WORK_ONSTACK	1	/* Work's memory is on stack */
27 
28 struct padata_work {
29 	struct work_struct	pw_work;
30 	struct list_head	pw_list;  /* padata_free_works linkage */
31 	void			*pw_data;
32 };
33 
34 static DEFINE_SPINLOCK(padata_works_lock);
35 static struct padata_work *padata_works;
36 static LIST_HEAD(padata_free_works);
37 
38 struct padata_mt_job_state {
39 	spinlock_t		lock;
40 	struct completion	completion;
41 	struct padata_mt_job	*job;
42 	int			nworks;
43 	int			nworks_fini;
44 	unsigned long		chunk_size;
45 };
46 
47 static void padata_free_pd(struct parallel_data *pd);
48 static void __init padata_mt_helper(struct work_struct *work);
49 
50 static inline void padata_get_pd(struct parallel_data *pd)
51 {
52 	refcount_inc(&pd->refcnt);
53 }
54 
55 static inline void padata_put_pd_cnt(struct parallel_data *pd, int cnt)
56 {
57 	if (refcount_sub_and_test(cnt, &pd->refcnt))
58 		padata_free_pd(pd);
59 }
60 
61 static inline void padata_put_pd(struct parallel_data *pd)
62 {
63 	padata_put_pd_cnt(pd, 1);
64 }
65 
66 static int padata_cpu_hash(struct parallel_data *pd, unsigned int seq_nr)
67 {
68 	/*
69 	 * Hash the sequence numbers to the cpus by taking
70 	 * seq_nr mod. number of cpus in use.
71 	 */
72 	int cpu_index = seq_nr % cpumask_weight(pd->cpumask.pcpu);
73 
74 	return cpumask_nth(cpu_index, pd->cpumask.pcpu);
75 }
76 
77 static struct padata_work *padata_work_alloc(void)
78 {
79 	struct padata_work *pw;
80 
81 	lockdep_assert_held(&padata_works_lock);
82 
83 	if (list_empty(&padata_free_works))
84 		return NULL;	/* No more work items allowed to be queued. */
85 
86 	pw = list_first_entry(&padata_free_works, struct padata_work, pw_list);
87 	list_del(&pw->pw_list);
88 	return pw;
89 }
90 
91 /*
92  * This function is marked __ref because this function may be optimized in such
93  * a way that it directly refers to work_fn's address, which causes modpost to
94  * complain when work_fn is marked __init. This scenario was observed with clang
95  * LTO, where padata_work_init() was optimized to refer directly to
96  * padata_mt_helper() because the calls to padata_work_init() with other work_fn
97  * values were eliminated or inlined.
98  */
99 static void __ref padata_work_init(struct padata_work *pw, work_func_t work_fn,
100 				   void *data, int flags)
101 {
102 	if (flags & PADATA_WORK_ONSTACK)
103 		INIT_WORK_ONSTACK(&pw->pw_work, work_fn);
104 	else
105 		INIT_WORK(&pw->pw_work, work_fn);
106 	pw->pw_data = data;
107 }
108 
109 static int __init padata_work_alloc_mt(int nworks, void *data,
110 				       struct list_head *head)
111 {
112 	int i;
113 
114 	spin_lock_bh(&padata_works_lock);
115 	/* Start at 1 because the current task participates in the job. */
116 	for (i = 1; i < nworks; ++i) {
117 		struct padata_work *pw = padata_work_alloc();
118 
119 		if (!pw)
120 			break;
121 		padata_work_init(pw, padata_mt_helper, data, 0);
122 		list_add(&pw->pw_list, head);
123 	}
124 	spin_unlock_bh(&padata_works_lock);
125 
126 	return i;
127 }
128 
129 static void padata_work_free(struct padata_work *pw)
130 {
131 	lockdep_assert_held(&padata_works_lock);
132 	list_add(&pw->pw_list, &padata_free_works);
133 }
134 
135 static void __init padata_works_free(struct list_head *works)
136 {
137 	struct padata_work *cur, *next;
138 
139 	if (list_empty(works))
140 		return;
141 
142 	spin_lock_bh(&padata_works_lock);
143 	list_for_each_entry_safe(cur, next, works, pw_list) {
144 		list_del(&cur->pw_list);
145 		padata_work_free(cur);
146 	}
147 	spin_unlock_bh(&padata_works_lock);
148 }
149 
150 static void padata_parallel_worker(struct work_struct *parallel_work)
151 {
152 	struct padata_work *pw = container_of(parallel_work, struct padata_work,
153 					      pw_work);
154 	struct padata_priv *padata = pw->pw_data;
155 
156 	local_bh_disable();
157 	padata->parallel(padata);
158 	spin_lock(&padata_works_lock);
159 	padata_work_free(pw);
160 	spin_unlock(&padata_works_lock);
161 	local_bh_enable();
162 }
163 
164 /**
165  * padata_do_parallel - padata parallelization function
166  *
167  * @ps: padatashell
168  * @padata: object to be parallelized
169  * @cb_cpu: pointer to the CPU that the serialization callback function should
170  *          run on.  If it's not in the serial cpumask of @pinst
171  *          (i.e. cpumask.cbcpu), this function selects a fallback CPU and if
172  *          none found, returns -EINVAL.
173  *
174  * The parallelization callback function will run with BHs off.
175  * Note: Every object which is parallelized by padata_do_parallel
176  * must be seen by padata_do_serial.
177  *
178  * Return: 0 on success or else negative error code.
179  */
180 int padata_do_parallel(struct padata_shell *ps,
181 		       struct padata_priv *padata, int *cb_cpu)
182 {
183 	struct padata_instance *pinst = ps->pinst;
184 	struct parallel_data *pd;
185 	struct padata_work *pw;
186 	int cpu_index, err;
187 
188 	rcu_read_lock_bh();
189 
190 	pd = rcu_dereference_bh(ps->pd);
191 
192 	err = -EINVAL;
193 	if (!(pinst->flags & PADATA_INIT) || pinst->flags & PADATA_INVALID)
194 		goto out;
195 
196 	if (!cpumask_test_cpu(*cb_cpu, pd->cpumask.cbcpu)) {
197 		if (cpumask_empty(pd->cpumask.cbcpu))
198 			goto out;
199 
200 		/* Select an alternate fallback CPU and notify the caller. */
201 		cpu_index = *cb_cpu % cpumask_weight(pd->cpumask.cbcpu);
202 		*cb_cpu = cpumask_nth(cpu_index, pd->cpumask.cbcpu);
203 	}
204 
205 	err = -EBUSY;
206 	if ((pinst->flags & PADATA_RESET))
207 		goto out;
208 
209 	padata_get_pd(pd);
210 	padata->pd = pd;
211 	padata->cb_cpu = *cb_cpu;
212 
213 	spin_lock(&padata_works_lock);
214 	padata->seq_nr = ++pd->seq_nr;
215 	pw = padata_work_alloc();
216 	spin_unlock(&padata_works_lock);
217 
218 	if (!pw) {
219 		/* Maximum works limit exceeded, run in the current task. */
220 		padata->parallel(padata);
221 	}
222 
223 	rcu_read_unlock_bh();
224 
225 	if (pw) {
226 		padata_work_init(pw, padata_parallel_worker, padata, 0);
227 		queue_work(pinst->parallel_wq, &pw->pw_work);
228 	}
229 
230 	return 0;
231 out:
232 	rcu_read_unlock_bh();
233 
234 	return err;
235 }
236 EXPORT_SYMBOL(padata_do_parallel);
237 
238 /*
239  * padata_find_next - Find the next object that needs serialization.
240  *
241  * Return:
242  * * A pointer to the control struct of the next object that needs
243  *   serialization, if present in one of the percpu reorder queues.
244  * * NULL, if the next object that needs serialization will
245  *   be parallel processed by another cpu and is not yet present in
246  *   the cpu's reorder queue.
247  */
248 static struct padata_priv *padata_find_next(struct parallel_data *pd, int cpu,
249 					    unsigned int processed)
250 {
251 	struct padata_priv *padata;
252 	struct padata_list *reorder;
253 
254 	reorder = per_cpu_ptr(pd->reorder_list, cpu);
255 
256 	spin_lock(&reorder->lock);
257 	if (list_empty(&reorder->list))
258 		goto notfound;
259 
260 	padata = list_entry(reorder->list.next, struct padata_priv, list);
261 
262 	/*
263 	 * Checks the rare case where two or more parallel jobs have hashed to
264 	 * the same CPU and one of the later ones finishes first.
265 	 */
266 	if (padata->seq_nr != processed)
267 		goto notfound;
268 
269 	list_del_init(&padata->list);
270 	spin_unlock(&reorder->lock);
271 	return padata;
272 
273 notfound:
274 	pd->processed = processed;
275 	pd->cpu = cpu;
276 	spin_unlock(&reorder->lock);
277 	return NULL;
278 }
279 
280 static void padata_reorder(struct padata_priv *padata)
281 {
282 	struct parallel_data *pd = padata->pd;
283 	struct padata_instance *pinst = pd->ps->pinst;
284 	unsigned int processed;
285 	int cpu;
286 
287 	processed = pd->processed;
288 	cpu = pd->cpu;
289 
290 	do {
291 		struct padata_serial_queue *squeue;
292 		int cb_cpu;
293 
294 		cpu = cpumask_next_wrap(cpu, pd->cpumask.pcpu);
295 		processed++;
296 
297 		cb_cpu = padata->cb_cpu;
298 		squeue = per_cpu_ptr(pd->squeue, cb_cpu);
299 
300 		spin_lock(&squeue->serial.lock);
301 		list_add_tail(&padata->list, &squeue->serial.list);
302 		queue_work_on(cb_cpu, pinst->serial_wq, &squeue->work);
303 
304 		/*
305 		 * If the next object that needs serialization is parallel
306 		 * processed by another cpu and is still on it's way to the
307 		 * cpu's reorder queue, end the loop.
308 		 */
309 		padata = padata_find_next(pd, cpu, processed);
310 		spin_unlock(&squeue->serial.lock);
311 	} while (padata);
312 }
313 
314 static void padata_serial_worker(struct work_struct *serial_work)
315 {
316 	struct padata_serial_queue *squeue;
317 	struct parallel_data *pd;
318 	LIST_HEAD(local_list);
319 	int cnt;
320 
321 	local_bh_disable();
322 	squeue = container_of(serial_work, struct padata_serial_queue, work);
323 	pd = squeue->pd;
324 
325 	spin_lock(&squeue->serial.lock);
326 	list_replace_init(&squeue->serial.list, &local_list);
327 	spin_unlock(&squeue->serial.lock);
328 
329 	cnt = 0;
330 
331 	while (!list_empty(&local_list)) {
332 		struct padata_priv *padata;
333 
334 		padata = list_entry(local_list.next,
335 				    struct padata_priv, list);
336 
337 		list_del_init(&padata->list);
338 
339 		padata->serial(padata);
340 		cnt++;
341 	}
342 	local_bh_enable();
343 
344 	padata_put_pd_cnt(pd, cnt);
345 }
346 
347 /**
348  * padata_do_serial - padata serialization function
349  *
350  * @padata: object to be serialized.
351  *
352  * padata_do_serial must be called for every parallelized object.
353  * The serialization callback function will run with BHs off.
354  */
355 void padata_do_serial(struct padata_priv *padata)
356 {
357 	struct parallel_data *pd = padata->pd;
358 	int hashed_cpu = padata_cpu_hash(pd, padata->seq_nr);
359 	struct padata_list *reorder = per_cpu_ptr(pd->reorder_list, hashed_cpu);
360 	struct padata_priv *cur;
361 	struct list_head *pos;
362 	bool gotit = true;
363 
364 	spin_lock(&reorder->lock);
365 	/* Sort in ascending order of sequence number. */
366 	list_for_each_prev(pos, &reorder->list) {
367 		cur = list_entry(pos, struct padata_priv, list);
368 		/* Compare by difference to consider integer wrap around */
369 		if ((signed int)(cur->seq_nr - padata->seq_nr) < 0)
370 			break;
371 	}
372 	if (padata->seq_nr != pd->processed) {
373 		gotit = false;
374 		list_add(&padata->list, pos);
375 	}
376 	spin_unlock(&reorder->lock);
377 
378 	if (gotit)
379 		padata_reorder(padata);
380 }
381 EXPORT_SYMBOL(padata_do_serial);
382 
383 static int padata_setup_cpumasks(struct padata_instance *pinst)
384 {
385 	struct workqueue_attrs *attrs;
386 	int err;
387 
388 	attrs = alloc_workqueue_attrs();
389 	if (!attrs)
390 		return -ENOMEM;
391 
392 	/* Restrict parallel_wq workers to pd->cpumask.pcpu. */
393 	cpumask_copy(attrs->cpumask, pinst->cpumask.pcpu);
394 	err = apply_workqueue_attrs(pinst->parallel_wq, attrs);
395 	free_workqueue_attrs(attrs);
396 
397 	return err;
398 }
399 
400 static void __init padata_mt_helper(struct work_struct *w)
401 {
402 	struct padata_work *pw = container_of(w, struct padata_work, pw_work);
403 	struct padata_mt_job_state *ps = pw->pw_data;
404 	struct padata_mt_job *job = ps->job;
405 	bool done;
406 
407 	spin_lock(&ps->lock);
408 
409 	while (job->size > 0) {
410 		unsigned long start, size, end;
411 
412 		start = job->start;
413 		/* So end is chunk size aligned if enough work remains. */
414 		size = roundup(start + 1, ps->chunk_size) - start;
415 		size = min(size, job->size);
416 		end = start + size;
417 
418 		job->start = end;
419 		job->size -= size;
420 
421 		spin_unlock(&ps->lock);
422 		job->thread_fn(start, end, job->fn_arg);
423 		spin_lock(&ps->lock);
424 	}
425 
426 	++ps->nworks_fini;
427 	done = (ps->nworks_fini == ps->nworks);
428 	spin_unlock(&ps->lock);
429 
430 	if (done)
431 		complete(&ps->completion);
432 }
433 
434 /**
435  * padata_do_multithreaded - run a multithreaded job
436  * @job: Description of the job.
437  *
438  * See the definition of struct padata_mt_job for more details.
439  */
440 void __init padata_do_multithreaded(struct padata_mt_job *job)
441 {
442 	/* In case threads finish at different times. */
443 	static const unsigned long load_balance_factor = 4;
444 	struct padata_work my_work, *pw;
445 	struct padata_mt_job_state ps;
446 	LIST_HEAD(works);
447 	int nworks, nid;
448 	static atomic_t last_used_nid __initdata;
449 
450 	if (job->size == 0)
451 		return;
452 
453 	/* Ensure at least one thread when size < min_chunk. */
454 	nworks = max(job->size / max(job->min_chunk, job->align), 1ul);
455 	nworks = min(nworks, job->max_threads);
456 
457 	if (nworks == 1) {
458 		/* Single thread, no coordination needed, cut to the chase. */
459 		job->thread_fn(job->start, job->start + job->size, job->fn_arg);
460 		return;
461 	}
462 
463 	spin_lock_init(&ps.lock);
464 	init_completion(&ps.completion);
465 	ps.job	       = job;
466 	ps.nworks      = padata_work_alloc_mt(nworks, &ps, &works);
467 	ps.nworks_fini = 0;
468 
469 	/*
470 	 * Chunk size is the amount of work a helper does per call to the
471 	 * thread function.  Load balance large jobs between threads by
472 	 * increasing the number of chunks, guarantee at least the minimum
473 	 * chunk size from the caller, and honor the caller's alignment.
474 	 * Ensure chunk_size is at least 1 to prevent divide-by-0
475 	 * panic in padata_mt_helper().
476 	 */
477 	ps.chunk_size = job->size / (ps.nworks * load_balance_factor);
478 	ps.chunk_size = max(ps.chunk_size, job->min_chunk);
479 	ps.chunk_size = max(ps.chunk_size, 1ul);
480 	ps.chunk_size = roundup(ps.chunk_size, job->align);
481 
482 	list_for_each_entry(pw, &works, pw_list)
483 		if (job->numa_aware) {
484 			int old_node = atomic_read(&last_used_nid);
485 
486 			do {
487 				nid = next_node_in(old_node, node_states[N_CPU]);
488 			} while (!atomic_try_cmpxchg(&last_used_nid, &old_node, nid));
489 			queue_work_node(nid, system_unbound_wq, &pw->pw_work);
490 		} else {
491 			queue_work(system_unbound_wq, &pw->pw_work);
492 		}
493 
494 	/* Use the current thread, which saves starting a workqueue worker. */
495 	padata_work_init(&my_work, padata_mt_helper, &ps, PADATA_WORK_ONSTACK);
496 	padata_mt_helper(&my_work.pw_work);
497 
498 	/* Wait for all the helpers to finish. */
499 	wait_for_completion(&ps.completion);
500 
501 	destroy_work_on_stack(&my_work.pw_work);
502 	padata_works_free(&works);
503 }
504 
505 static void __padata_list_init(struct padata_list *pd_list)
506 {
507 	INIT_LIST_HEAD(&pd_list->list);
508 	spin_lock_init(&pd_list->lock);
509 }
510 
511 /* Initialize all percpu queues used by serial workers */
512 static void padata_init_squeues(struct parallel_data *pd)
513 {
514 	int cpu;
515 	struct padata_serial_queue *squeue;
516 
517 	for_each_cpu(cpu, pd->cpumask.cbcpu) {
518 		squeue = per_cpu_ptr(pd->squeue, cpu);
519 		squeue->pd = pd;
520 		__padata_list_init(&squeue->serial);
521 		INIT_WORK(&squeue->work, padata_serial_worker);
522 	}
523 }
524 
525 /* Initialize per-CPU reorder lists */
526 static void padata_init_reorder_list(struct parallel_data *pd)
527 {
528 	int cpu;
529 	struct padata_list *list;
530 
531 	for_each_cpu(cpu, pd->cpumask.pcpu) {
532 		list = per_cpu_ptr(pd->reorder_list, cpu);
533 		__padata_list_init(list);
534 	}
535 }
536 
537 /* Allocate and initialize the internal cpumask dependend resources. */
538 static struct parallel_data *padata_alloc_pd(struct padata_shell *ps)
539 {
540 	struct padata_instance *pinst = ps->pinst;
541 	struct parallel_data *pd;
542 
543 	pd = kzalloc(sizeof(struct parallel_data), GFP_KERNEL);
544 	if (!pd)
545 		goto err;
546 
547 	pd->reorder_list = alloc_percpu(struct padata_list);
548 	if (!pd->reorder_list)
549 		goto err_free_pd;
550 
551 	pd->squeue = alloc_percpu(struct padata_serial_queue);
552 	if (!pd->squeue)
553 		goto err_free_reorder_list;
554 
555 	pd->ps = ps;
556 
557 	if (!alloc_cpumask_var(&pd->cpumask.pcpu, GFP_KERNEL))
558 		goto err_free_squeue;
559 	if (!alloc_cpumask_var(&pd->cpumask.cbcpu, GFP_KERNEL))
560 		goto err_free_pcpu;
561 
562 	cpumask_and(pd->cpumask.pcpu, pinst->cpumask.pcpu, cpu_online_mask);
563 	cpumask_and(pd->cpumask.cbcpu, pinst->cpumask.cbcpu, cpu_online_mask);
564 
565 	padata_init_reorder_list(pd);
566 	padata_init_squeues(pd);
567 	pd->seq_nr = -1;
568 	refcount_set(&pd->refcnt, 1);
569 	pd->cpu = cpumask_first(pd->cpumask.pcpu);
570 
571 	return pd;
572 
573 err_free_pcpu:
574 	free_cpumask_var(pd->cpumask.pcpu);
575 err_free_squeue:
576 	free_percpu(pd->squeue);
577 err_free_reorder_list:
578 	free_percpu(pd->reorder_list);
579 err_free_pd:
580 	kfree(pd);
581 err:
582 	return NULL;
583 }
584 
585 static void padata_free_pd(struct parallel_data *pd)
586 {
587 	free_cpumask_var(pd->cpumask.pcpu);
588 	free_cpumask_var(pd->cpumask.cbcpu);
589 	free_percpu(pd->reorder_list);
590 	free_percpu(pd->squeue);
591 	kfree(pd);
592 }
593 
594 static void __padata_start(struct padata_instance *pinst)
595 {
596 	pinst->flags |= PADATA_INIT;
597 }
598 
599 static void __padata_stop(struct padata_instance *pinst)
600 {
601 	if (!(pinst->flags & PADATA_INIT))
602 		return;
603 
604 	pinst->flags &= ~PADATA_INIT;
605 
606 	synchronize_rcu();
607 }
608 
609 /* Replace the internal control structure with a new one. */
610 static int padata_replace_one(struct padata_shell *ps)
611 {
612 	struct parallel_data *pd_new;
613 
614 	pd_new = padata_alloc_pd(ps);
615 	if (!pd_new)
616 		return -ENOMEM;
617 
618 	ps->opd = rcu_dereference_protected(ps->pd, 1);
619 	rcu_assign_pointer(ps->pd, pd_new);
620 
621 	return 0;
622 }
623 
624 static int padata_replace(struct padata_instance *pinst)
625 {
626 	struct padata_shell *ps;
627 	int err = 0;
628 
629 	pinst->flags |= PADATA_RESET;
630 
631 	list_for_each_entry(ps, &pinst->pslist, list) {
632 		err = padata_replace_one(ps);
633 		if (err)
634 			break;
635 	}
636 
637 	synchronize_rcu();
638 
639 	list_for_each_entry_continue_reverse(ps, &pinst->pslist, list)
640 		padata_put_pd(ps->opd);
641 
642 	pinst->flags &= ~PADATA_RESET;
643 
644 	return err;
645 }
646 
647 /* If cpumask contains no active cpu, we mark the instance as invalid. */
648 static bool padata_validate_cpumask(struct padata_instance *pinst,
649 				    const struct cpumask *cpumask)
650 {
651 	if (!cpumask_intersects(cpumask, cpu_online_mask)) {
652 		pinst->flags |= PADATA_INVALID;
653 		return false;
654 	}
655 
656 	pinst->flags &= ~PADATA_INVALID;
657 	return true;
658 }
659 
660 static int __padata_set_cpumasks(struct padata_instance *pinst,
661 				 cpumask_var_t pcpumask,
662 				 cpumask_var_t cbcpumask)
663 {
664 	int valid;
665 	int err;
666 
667 	valid = padata_validate_cpumask(pinst, pcpumask);
668 	if (!valid) {
669 		__padata_stop(pinst);
670 		goto out_replace;
671 	}
672 
673 	valid = padata_validate_cpumask(pinst, cbcpumask);
674 	if (!valid)
675 		__padata_stop(pinst);
676 
677 out_replace:
678 	cpumask_copy(pinst->cpumask.pcpu, pcpumask);
679 	cpumask_copy(pinst->cpumask.cbcpu, cbcpumask);
680 
681 	err = padata_setup_cpumasks(pinst) ?: padata_replace(pinst);
682 
683 	if (valid)
684 		__padata_start(pinst);
685 
686 	return err;
687 }
688 
689 /**
690  * padata_set_cpumask - Sets specified by @cpumask_type cpumask to the value
691  *                      equivalent to @cpumask.
692  * @pinst: padata instance
693  * @cpumask_type: PADATA_CPU_SERIAL or PADATA_CPU_PARALLEL corresponding
694  *                to parallel and serial cpumasks respectively.
695  * @cpumask: the cpumask to use
696  *
697  * Return: 0 on success or negative error code
698  */
699 int padata_set_cpumask(struct padata_instance *pinst, int cpumask_type,
700 		       cpumask_var_t cpumask)
701 {
702 	struct cpumask *serial_mask, *parallel_mask;
703 	int err = -EINVAL;
704 
705 	cpus_read_lock();
706 	mutex_lock(&pinst->lock);
707 
708 	switch (cpumask_type) {
709 	case PADATA_CPU_PARALLEL:
710 		serial_mask = pinst->cpumask.cbcpu;
711 		parallel_mask = cpumask;
712 		break;
713 	case PADATA_CPU_SERIAL:
714 		parallel_mask = pinst->cpumask.pcpu;
715 		serial_mask = cpumask;
716 		break;
717 	default:
718 		 goto out;
719 	}
720 
721 	err =  __padata_set_cpumasks(pinst, parallel_mask, serial_mask);
722 
723 out:
724 	mutex_unlock(&pinst->lock);
725 	cpus_read_unlock();
726 
727 	return err;
728 }
729 EXPORT_SYMBOL(padata_set_cpumask);
730 
731 #ifdef CONFIG_HOTPLUG_CPU
732 
733 static int __padata_add_cpu(struct padata_instance *pinst, int cpu)
734 {
735 	int err = 0;
736 
737 	if (cpumask_test_cpu(cpu, cpu_online_mask)) {
738 		err = padata_replace(pinst);
739 
740 		if (padata_validate_cpumask(pinst, pinst->cpumask.pcpu) &&
741 		    padata_validate_cpumask(pinst, pinst->cpumask.cbcpu))
742 			__padata_start(pinst);
743 	}
744 
745 	return err;
746 }
747 
748 static int __padata_remove_cpu(struct padata_instance *pinst, int cpu)
749 {
750 	int err = 0;
751 
752 	if (!cpumask_test_cpu(cpu, cpu_online_mask)) {
753 		if (!padata_validate_cpumask(pinst, pinst->cpumask.pcpu) ||
754 		    !padata_validate_cpumask(pinst, pinst->cpumask.cbcpu))
755 			__padata_stop(pinst);
756 
757 		err = padata_replace(pinst);
758 	}
759 
760 	return err;
761 }
762 
763 static 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 
769 static int padata_cpu_online(unsigned int cpu, struct hlist_node *node)
770 {
771 	struct padata_instance *pinst;
772 	int ret;
773 
774 	pinst = hlist_entry_safe(node, struct padata_instance, cpu_online_node);
775 	if (!pinst_has_cpu(pinst, cpu))
776 		return 0;
777 
778 	mutex_lock(&pinst->lock);
779 	ret = __padata_add_cpu(pinst, cpu);
780 	mutex_unlock(&pinst->lock);
781 	return ret;
782 }
783 
784 static int padata_cpu_dead(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, cpu_dead_node);
790 	if (!pinst_has_cpu(pinst, cpu))
791 		return 0;
792 
793 	mutex_lock(&pinst->lock);
794 	ret = __padata_remove_cpu(pinst, cpu);
795 	mutex_unlock(&pinst->lock);
796 	return ret;
797 }
798 
799 static enum cpuhp_state hp_online;
800 #endif
801 
802 static void __padata_free(struct padata_instance *pinst)
803 {
804 #ifdef CONFIG_HOTPLUG_CPU
805 	cpuhp_state_remove_instance_nocalls(CPUHP_PADATA_DEAD,
806 					    &pinst->cpu_dead_node);
807 	cpuhp_state_remove_instance_nocalls(hp_online, &pinst->cpu_online_node);
808 #endif
809 
810 	WARN_ON(!list_empty(&pinst->pslist));
811 
812 	free_cpumask_var(pinst->cpumask.pcpu);
813 	free_cpumask_var(pinst->cpumask.cbcpu);
814 	destroy_workqueue(pinst->serial_wq);
815 	destroy_workqueue(pinst->parallel_wq);
816 	kfree(pinst);
817 }
818 
819 #define kobj2pinst(_kobj)					\
820 	container_of(_kobj, struct padata_instance, kobj)
821 #define attr2pentry(_attr)					\
822 	container_of(_attr, struct padata_sysfs_entry, attr)
823 
824 static void padata_sysfs_release(struct kobject *kobj)
825 {
826 	struct padata_instance *pinst = kobj2pinst(kobj);
827 	__padata_free(pinst);
828 }
829 
830 struct padata_sysfs_entry {
831 	struct attribute attr;
832 	ssize_t (*show)(struct padata_instance *, struct attribute *, char *);
833 	ssize_t (*store)(struct padata_instance *, struct attribute *,
834 			 const char *, size_t);
835 };
836 
837 static ssize_t show_cpumask(struct padata_instance *pinst,
838 			    struct attribute *attr,  char *buf)
839 {
840 	struct cpumask *cpumask;
841 	ssize_t len;
842 
843 	mutex_lock(&pinst->lock);
844 	if (!strcmp(attr->name, "serial_cpumask"))
845 		cpumask = pinst->cpumask.cbcpu;
846 	else
847 		cpumask = pinst->cpumask.pcpu;
848 
849 	len = snprintf(buf, PAGE_SIZE, "%*pb\n",
850 		       nr_cpu_ids, cpumask_bits(cpumask));
851 	mutex_unlock(&pinst->lock);
852 	return len < PAGE_SIZE ? len : -EINVAL;
853 }
854 
855 static ssize_t store_cpumask(struct padata_instance *pinst,
856 			     struct attribute *attr,
857 			     const char *buf, size_t count)
858 {
859 	cpumask_var_t new_cpumask;
860 	ssize_t ret;
861 	int mask_type;
862 
863 	if (!alloc_cpumask_var(&new_cpumask, GFP_KERNEL))
864 		return -ENOMEM;
865 
866 	ret = bitmap_parse(buf, count, cpumask_bits(new_cpumask),
867 			   nr_cpumask_bits);
868 	if (ret < 0)
869 		goto out;
870 
871 	mask_type = !strcmp(attr->name, "serial_cpumask") ?
872 		PADATA_CPU_SERIAL : PADATA_CPU_PARALLEL;
873 	ret = padata_set_cpumask(pinst, mask_type, new_cpumask);
874 	if (!ret)
875 		ret = count;
876 
877 out:
878 	free_cpumask_var(new_cpumask);
879 	return ret;
880 }
881 
882 #define PADATA_ATTR_RW(_name, _show_name, _store_name)		\
883 	static struct padata_sysfs_entry _name##_attr =		\
884 		__ATTR(_name, 0644, _show_name, _store_name)
885 #define PADATA_ATTR_RO(_name, _show_name)		\
886 	static struct padata_sysfs_entry _name##_attr = \
887 		__ATTR(_name, 0400, _show_name, NULL)
888 
889 PADATA_ATTR_RW(serial_cpumask, show_cpumask, store_cpumask);
890 PADATA_ATTR_RW(parallel_cpumask, show_cpumask, store_cpumask);
891 
892 /*
893  * Padata sysfs provides the following objects:
894  * serial_cpumask   [RW] - cpumask for serial workers
895  * parallel_cpumask [RW] - cpumask for parallel workers
896  */
897 static struct attribute *padata_default_attrs[] = {
898 	&serial_cpumask_attr.attr,
899 	&parallel_cpumask_attr.attr,
900 	NULL,
901 };
902 ATTRIBUTE_GROUPS(padata_default);
903 
904 static ssize_t padata_sysfs_show(struct kobject *kobj,
905 				 struct attribute *attr, char *buf)
906 {
907 	struct padata_instance *pinst;
908 	struct padata_sysfs_entry *pentry;
909 	ssize_t ret = -EIO;
910 
911 	pinst = kobj2pinst(kobj);
912 	pentry = attr2pentry(attr);
913 	if (pentry->show)
914 		ret = pentry->show(pinst, attr, buf);
915 
916 	return ret;
917 }
918 
919 static ssize_t padata_sysfs_store(struct kobject *kobj, struct attribute *attr,
920 				  const char *buf, size_t count)
921 {
922 	struct padata_instance *pinst;
923 	struct padata_sysfs_entry *pentry;
924 	ssize_t ret = -EIO;
925 
926 	pinst = kobj2pinst(kobj);
927 	pentry = attr2pentry(attr);
928 	if (pentry->store)
929 		ret = pentry->store(pinst, attr, buf, count);
930 
931 	return ret;
932 }
933 
934 static const struct sysfs_ops padata_sysfs_ops = {
935 	.show = padata_sysfs_show,
936 	.store = padata_sysfs_store,
937 };
938 
939 static const struct kobj_type padata_attr_type = {
940 	.sysfs_ops = &padata_sysfs_ops,
941 	.default_groups = padata_default_groups,
942 	.release = padata_sysfs_release,
943 };
944 
945 /**
946  * padata_alloc - allocate and initialize a padata instance
947  * @name: used to identify the instance
948  *
949  * Return: new instance on success, NULL on error
950  */
951 struct padata_instance *padata_alloc(const char *name)
952 {
953 	struct padata_instance *pinst;
954 
955 	pinst = kzalloc(sizeof(struct padata_instance), GFP_KERNEL);
956 	if (!pinst)
957 		goto err;
958 
959 	pinst->parallel_wq = alloc_workqueue("%s_parallel", WQ_UNBOUND, 0,
960 					     name);
961 	if (!pinst->parallel_wq)
962 		goto err_free_inst;
963 
964 	cpus_read_lock();
965 
966 	pinst->serial_wq = alloc_workqueue("%s_serial", WQ_MEM_RECLAIM |
967 					   WQ_CPU_INTENSIVE, 1, name);
968 	if (!pinst->serial_wq)
969 		goto err_put_cpus;
970 
971 	if (!alloc_cpumask_var(&pinst->cpumask.pcpu, GFP_KERNEL))
972 		goto err_free_serial_wq;
973 	if (!alloc_cpumask_var(&pinst->cpumask.cbcpu, GFP_KERNEL)) {
974 		free_cpumask_var(pinst->cpumask.pcpu);
975 		goto err_free_serial_wq;
976 	}
977 
978 	INIT_LIST_HEAD(&pinst->pslist);
979 
980 	cpumask_copy(pinst->cpumask.pcpu, cpu_possible_mask);
981 	cpumask_copy(pinst->cpumask.cbcpu, cpu_possible_mask);
982 
983 	if (padata_setup_cpumasks(pinst))
984 		goto err_free_masks;
985 
986 	__padata_start(pinst);
987 
988 	kobject_init(&pinst->kobj, &padata_attr_type);
989 	mutex_init(&pinst->lock);
990 
991 #ifdef CONFIG_HOTPLUG_CPU
992 	cpuhp_state_add_instance_nocalls_cpuslocked(hp_online,
993 						    &pinst->cpu_online_node);
994 	cpuhp_state_add_instance_nocalls_cpuslocked(CPUHP_PADATA_DEAD,
995 						    &pinst->cpu_dead_node);
996 #endif
997 
998 	cpus_read_unlock();
999 
1000 	return pinst;
1001 
1002 err_free_masks:
1003 	free_cpumask_var(pinst->cpumask.pcpu);
1004 	free_cpumask_var(pinst->cpumask.cbcpu);
1005 err_free_serial_wq:
1006 	destroy_workqueue(pinst->serial_wq);
1007 err_put_cpus:
1008 	cpus_read_unlock();
1009 	destroy_workqueue(pinst->parallel_wq);
1010 err_free_inst:
1011 	kfree(pinst);
1012 err:
1013 	return NULL;
1014 }
1015 EXPORT_SYMBOL(padata_alloc);
1016 
1017 /**
1018  * padata_free - free a padata instance
1019  *
1020  * @pinst: padata instance to free
1021  */
1022 void padata_free(struct padata_instance *pinst)
1023 {
1024 	kobject_put(&pinst->kobj);
1025 }
1026 EXPORT_SYMBOL(padata_free);
1027 
1028 /**
1029  * padata_alloc_shell - Allocate and initialize padata shell.
1030  *
1031  * @pinst: Parent padata_instance object.
1032  *
1033  * Return: new shell on success, NULL on error
1034  */
1035 struct padata_shell *padata_alloc_shell(struct padata_instance *pinst)
1036 {
1037 	struct parallel_data *pd;
1038 	struct padata_shell *ps;
1039 
1040 	ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1041 	if (!ps)
1042 		goto out;
1043 
1044 	ps->pinst = pinst;
1045 
1046 	cpus_read_lock();
1047 	pd = padata_alloc_pd(ps);
1048 	cpus_read_unlock();
1049 
1050 	if (!pd)
1051 		goto out_free_ps;
1052 
1053 	mutex_lock(&pinst->lock);
1054 	RCU_INIT_POINTER(ps->pd, pd);
1055 	list_add(&ps->list, &pinst->pslist);
1056 	mutex_unlock(&pinst->lock);
1057 
1058 	return ps;
1059 
1060 out_free_ps:
1061 	kfree(ps);
1062 out:
1063 	return NULL;
1064 }
1065 EXPORT_SYMBOL(padata_alloc_shell);
1066 
1067 /**
1068  * padata_free_shell - free a padata shell
1069  *
1070  * @ps: padata shell to free
1071  */
1072 void padata_free_shell(struct padata_shell *ps)
1073 {
1074 	struct parallel_data *pd;
1075 
1076 	if (!ps)
1077 		return;
1078 
1079 	mutex_lock(&ps->pinst->lock);
1080 	list_del(&ps->list);
1081 	pd = rcu_dereference_protected(ps->pd, 1);
1082 	padata_put_pd(pd);
1083 	mutex_unlock(&ps->pinst->lock);
1084 
1085 	kfree(ps);
1086 }
1087 EXPORT_SYMBOL(padata_free_shell);
1088 
1089 void __init padata_init(void)
1090 {
1091 	unsigned int i, possible_cpus;
1092 #ifdef CONFIG_HOTPLUG_CPU
1093 	int ret;
1094 
1095 	ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "padata:online",
1096 				      padata_cpu_online, NULL);
1097 	if (ret < 0)
1098 		goto err;
1099 	hp_online = ret;
1100 
1101 	ret = cpuhp_setup_state_multi(CPUHP_PADATA_DEAD, "padata:dead",
1102 				      NULL, padata_cpu_dead);
1103 	if (ret < 0)
1104 		goto remove_online_state;
1105 #endif
1106 
1107 	possible_cpus = num_possible_cpus();
1108 	padata_works = kmalloc_array(possible_cpus, sizeof(struct padata_work),
1109 				     GFP_KERNEL);
1110 	if (!padata_works)
1111 		goto remove_dead_state;
1112 
1113 	for (i = 0; i < possible_cpus; ++i)
1114 		list_add(&padata_works[i].pw_list, &padata_free_works);
1115 
1116 	return;
1117 
1118 remove_dead_state:
1119 #ifdef CONFIG_HOTPLUG_CPU
1120 	cpuhp_remove_multi_state(CPUHP_PADATA_DEAD);
1121 remove_online_state:
1122 	cpuhp_remove_multi_state(hp_online);
1123 err:
1124 #endif
1125 	pr_warn("padata: initialization failed\n");
1126 }
1127