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