blob: 57c0074d50cc485b706579aaa616e4a004775a8e [file] [log] [blame]
Steffen Klassert16295be2010-01-06 19:47:10 +11001/*
2 * padata.c - generic interface to process data streams in parallel
3 *
Steffen Klassert107f8bd2012-03-28 08:42:34 +02004 * See Documentation/padata.txt for an api documentation.
5 *
Steffen Klassert16295be2010-01-06 19:47:10 +11006 * 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
Paul Gortmaker9984de12011-05-23 14:51:41 -040023#include <linux/export.h>
Steffen Klassert16295be2010-01-06 19:47:10 +110024#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Dan Kruchinin5e017dc2010-07-14 14:33:08 +040031#include <linux/sysfs.h>
Steffen Klassert16295be2010-01-06 19:47:10 +110032#include <linux/rcupdate.h>
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +020033#include <linux/module.h>
Steffen Klassert16295be2010-01-06 19:47:10 +110034
Steffen Klassert97e3d942010-04-29 14:37:32 +020035#define MAX_OBJ_NUM 1000
Steffen Klassert16295be2010-01-06 19:47:10 +110036
37static int padata_index_to_cpu(struct parallel_data *pd, int cpu_index)
38{
39 int cpu, target_cpu;
40
Dan Kruchinine15bacb2010-07-14 14:31:57 +040041 target_cpu = cpumask_first(pd->cpumask.pcpu);
Steffen Klassert16295be2010-01-06 19:47:10 +110042 for (cpu = 0; cpu < cpu_index; cpu++)
Dan Kruchinine15bacb2010-07-14 14:31:57 +040043 target_cpu = cpumask_next(target_cpu, pd->cpumask.pcpu);
Steffen Klassert16295be2010-01-06 19:47:10 +110044
45 return target_cpu;
46}
47
Steffen Klassert2dc9b5d2012-03-09 07:20:49 +010048static int padata_cpu_hash(struct parallel_data *pd)
Steffen Klassert16295be2010-01-06 19:47:10 +110049{
Mathias Krause0b6b0982013-10-25 12:14:15 +020050 unsigned int seq_nr;
Steffen Klassert16295be2010-01-06 19:47:10 +110051 int cpu_index;
Steffen Klassert16295be2010-01-06 19:47:10 +110052
53 /*
54 * Hash the sequence numbers to the cpus by taking
55 * seq_nr mod. number of cpus in use.
56 */
Steffen Klassert2dc9b5d2012-03-09 07:20:49 +010057
Mathias Krause0b6b0982013-10-25 12:14:15 +020058 seq_nr = atomic_inc_return(&pd->seq_nr);
59 cpu_index = seq_nr % cpumask_weight(pd->cpumask.pcpu);
Steffen Klassert16295be2010-01-06 19:47:10 +110060
61 return padata_index_to_cpu(pd, cpu_index);
62}
63
Dan Kruchinine15bacb2010-07-14 14:31:57 +040064static void padata_parallel_worker(struct work_struct *parallel_work)
Steffen Klassert16295be2010-01-06 19:47:10 +110065{
Dan Kruchinine15bacb2010-07-14 14:31:57 +040066 struct padata_parallel_queue *pqueue;
Steffen Klassert16295be2010-01-06 19:47:10 +110067 LIST_HEAD(local_list);
68
69 local_bh_disable();
Dan Kruchinine15bacb2010-07-14 14:31:57 +040070 pqueue = container_of(parallel_work,
71 struct padata_parallel_queue, work);
Steffen Klassert16295be2010-01-06 19:47:10 +110072
Dan Kruchinine15bacb2010-07-14 14:31:57 +040073 spin_lock(&pqueue->parallel.lock);
74 list_replace_init(&pqueue->parallel.list, &local_list);
75 spin_unlock(&pqueue->parallel.lock);
Steffen Klassert16295be2010-01-06 19:47:10 +110076
77 while (!list_empty(&local_list)) {
78 struct padata_priv *padata;
79
80 padata = list_entry(local_list.next,
81 struct padata_priv, list);
82
83 list_del_init(&padata->list);
84
85 padata->parallel(padata);
86 }
87
88 local_bh_enable();
89}
90
Steffen Klassert0198ffd2010-05-19 13:44:27 +100091/**
Steffen Klassert16295be2010-01-06 19:47:10 +110092 * padata_do_parallel - padata parallelization function
93 *
94 * @pinst: padata instance
95 * @padata: object to be parallelized
96 * @cb_cpu: cpu the serialization callback function will run on,
Dan Kruchinine15bacb2010-07-14 14:31:57 +040097 * must be in the serial cpumask of padata(i.e. cpumask.cbcpu).
Steffen Klassert16295be2010-01-06 19:47:10 +110098 *
99 * The parallelization callback function will run with BHs off.
100 * Note: Every object which is parallelized by padata_do_parallel
101 * must be seen by padata_do_serial.
102 */
103int padata_do_parallel(struct padata_instance *pinst,
104 struct padata_priv *padata, int cb_cpu)
105{
106 int target_cpu, err;
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400107 struct padata_parallel_queue *queue;
Steffen Klassert16295be2010-01-06 19:47:10 +1100108 struct parallel_data *pd;
109
110 rcu_read_lock_bh();
111
Mathias Krausec0e656b2013-11-28 19:20:05 +0100112 pd = rcu_dereference_bh(pinst->pd);
Steffen Klassert16295be2010-01-06 19:47:10 +1100113
Steffen Klassert83f619f2010-07-07 15:32:02 +0200114 err = -EINVAL;
Steffen Klassert74247132010-07-20 08:51:25 +0200115 if (!(pinst->flags & PADATA_INIT) || pinst->flags & PADATA_INVALID)
Steffen Klassert16295be2010-01-06 19:47:10 +1100116 goto out;
117
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400118 if (!cpumask_test_cpu(cb_cpu, pd->cpumask.cbcpu))
Steffen Klassert16295be2010-01-06 19:47:10 +1100119 goto out;
120
121 err = -EBUSY;
122 if ((pinst->flags & PADATA_RESET))
123 goto out;
124
125 if (atomic_read(&pd->refcnt) >= MAX_OBJ_NUM)
126 goto out;
127
Steffen Klassert83f619f2010-07-07 15:32:02 +0200128 err = 0;
Steffen Klassert16295be2010-01-06 19:47:10 +1100129 atomic_inc(&pd->refcnt);
130 padata->pd = pd;
131 padata->cb_cpu = cb_cpu;
132
Steffen Klassert2dc9b5d2012-03-09 07:20:49 +0100133 target_cpu = padata_cpu_hash(pd);
Mathias Krause350ef882017-09-08 20:57:11 +0200134 padata->cpu = target_cpu;
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400135 queue = per_cpu_ptr(pd->pqueue, target_cpu);
Steffen Klassert16295be2010-01-06 19:47:10 +1100136
137 spin_lock(&queue->parallel.lock);
138 list_add_tail(&padata->list, &queue->parallel.list);
139 spin_unlock(&queue->parallel.lock);
140
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400141 queue_work_on(target_cpu, pinst->wq, &queue->work);
Steffen Klassert16295be2010-01-06 19:47:10 +1100142
143out:
144 rcu_read_unlock_bh();
145
146 return err;
147}
148EXPORT_SYMBOL(padata_do_parallel);
149
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000150/*
151 * padata_get_next - Get the next object that needs serialization.
152 *
153 * Return values are:
154 *
155 * A pointer to the control struct of the next object that needs
156 * serialization, if present in one of the percpu reorder queues.
157 *
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000158 * -EINPROGRESS, if the next object that needs serialization will
159 * be parallel processed by another cpu and is not yet present in
160 * the cpu's reorder queue.
161 *
162 * -ENODATA, if this cpu has to do the parallel processing for
163 * the next object.
164 */
Steffen Klassert16295be2010-01-06 19:47:10 +1100165static struct padata_priv *padata_get_next(struct parallel_data *pd)
166{
Steffen Klassert5f1a8c12010-07-07 15:32:39 +0200167 int cpu, num_cpus;
Steffen Klassert2dc9b5d2012-03-09 07:20:49 +0100168 unsigned int next_nr, next_index;
Shan Weif0fcf202012-12-06 17:16:23 +0800169 struct padata_parallel_queue *next_queue;
Steffen Klassert16295be2010-01-06 19:47:10 +1100170 struct padata_priv *padata;
171 struct padata_list *reorder;
172
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400173 num_cpus = cpumask_weight(pd->cpumask.pcpu);
Steffen Klassert16295be2010-01-06 19:47:10 +1100174
Steffen Klassert5f1a8c12010-07-07 15:32:39 +0200175 /*
176 * Calculate the percpu reorder queue and the sequence
177 * number of the next object.
178 */
179 next_nr = pd->processed;
180 next_index = next_nr % num_cpus;
181 cpu = padata_index_to_cpu(pd, next_index);
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400182 next_queue = per_cpu_ptr(pd->pqueue, cpu);
Steffen Klassert16295be2010-01-06 19:47:10 +1100183
Steffen Klassert16295be2010-01-06 19:47:10 +1100184 reorder = &next_queue->reorder;
185
Jason A. Donenfeldde5540d2017-03-23 12:24:43 +0100186 spin_lock(&reorder->lock);
Steffen Klassert16295be2010-01-06 19:47:10 +1100187 if (!list_empty(&reorder->list)) {
188 padata = list_entry(reorder->list.next,
189 struct padata_priv, list);
190
Steffen Klassert16295be2010-01-06 19:47:10 +1100191 list_del_init(&padata->list);
192 atomic_dec(&pd->reorder_objects);
Steffen Klassert16295be2010-01-06 19:47:10 +1100193
Steffen Klassert5f1a8c12010-07-07 15:32:39 +0200194 pd->processed++;
Steffen Klassert16295be2010-01-06 19:47:10 +1100195
Jason A. Donenfeldde5540d2017-03-23 12:24:43 +0100196 spin_unlock(&reorder->lock);
Steffen Klassert16295be2010-01-06 19:47:10 +1100197 goto out;
198 }
Jason A. Donenfeldde5540d2017-03-23 12:24:43 +0100199 spin_unlock(&reorder->lock);
Steffen Klassert16295be2010-01-06 19:47:10 +1100200
Shan Weif0fcf202012-12-06 17:16:23 +0800201 if (__this_cpu_read(pd->pqueue->cpu_index) == next_queue->cpu_index) {
Steffen Klassert16295be2010-01-06 19:47:10 +1100202 padata = ERR_PTR(-ENODATA);
203 goto out;
204 }
205
206 padata = ERR_PTR(-EINPROGRESS);
207out:
208 return padata;
209}
210
211static void padata_reorder(struct parallel_data *pd)
212{
Steffen Klassert30478172012-03-09 07:20:12 +0100213 int cb_cpu;
Steffen Klassert16295be2010-01-06 19:47:10 +1100214 struct padata_priv *padata;
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400215 struct padata_serial_queue *squeue;
Steffen Klassert16295be2010-01-06 19:47:10 +1100216 struct padata_instance *pinst = pd->pinst;
217
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000218 /*
219 * We need to ensure that only one cpu can work on dequeueing of
220 * the reorder queue the time. Calculating in which percpu reorder
221 * queue the next object will arrive takes some time. A spinlock
222 * would be highly contended. Also it is not clear in which order
223 * the objects arrive to the reorder queues. So a cpu could wait to
224 * get the lock just to notice that there is nothing to do at the
225 * moment. Therefore we use a trylock and let the holder of the lock
226 * care for all the objects enqueued during the holdtime of the lock.
227 */
Steffen Klassert16295be2010-01-06 19:47:10 +1100228 if (!spin_trylock_bh(&pd->lock))
Steffen Klassertd46a5ac2010-05-19 13:43:14 +1000229 return;
Steffen Klassert16295be2010-01-06 19:47:10 +1100230
231 while (1) {
232 padata = padata_get_next(pd);
233
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000234 /*
Jason A. Donenfeld69b34842017-04-12 10:40:19 +0200235 * If the next object that needs serialization is parallel
236 * processed by another cpu and is still on it's way to the
237 * cpu's reorder queue, nothing to do for now.
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000238 */
Jason A. Donenfeld69b34842017-04-12 10:40:19 +0200239 if (PTR_ERR(padata) == -EINPROGRESS)
Steffen Klassert16295be2010-01-06 19:47:10 +1100240 break;
241
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000242 /*
243 * This cpu has to do the parallel processing of the next
244 * object. It's waiting in the cpu's parallelization queue,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300245 * so exit immediately.
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000246 */
Steffen Klassert16295be2010-01-06 19:47:10 +1100247 if (PTR_ERR(padata) == -ENODATA) {
Steffen Klassertd46a5ac2010-05-19 13:43:14 +1000248 del_timer(&pd->timer);
Steffen Klassert16295be2010-01-06 19:47:10 +1100249 spin_unlock_bh(&pd->lock);
Steffen Klassertd46a5ac2010-05-19 13:43:14 +1000250 return;
Steffen Klassert16295be2010-01-06 19:47:10 +1100251 }
252
Steffen Klassert30478172012-03-09 07:20:12 +0100253 cb_cpu = padata->cb_cpu;
254 squeue = per_cpu_ptr(pd->squeue, cb_cpu);
Steffen Klassert16295be2010-01-06 19:47:10 +1100255
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400256 spin_lock(&squeue->serial.lock);
257 list_add_tail(&padata->list, &squeue->serial.list);
258 spin_unlock(&squeue->serial.lock);
Steffen Klassert16295be2010-01-06 19:47:10 +1100259
Steffen Klassert30478172012-03-09 07:20:12 +0100260 queue_work_on(cb_cpu, pinst->wq, &squeue->work);
Steffen Klassert16295be2010-01-06 19:47:10 +1100261 }
262
263 spin_unlock_bh(&pd->lock);
264
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000265 /*
266 * The next object that needs serialization might have arrived to
267 * the reorder queues in the meantime, we will be called again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300268 * from the timer function if no one else cares for it.
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000269 */
Steffen Klassertd46a5ac2010-05-19 13:43:14 +1000270 if (atomic_read(&pd->reorder_objects)
271 && !(pinst->flags & PADATA_RESET))
272 mod_timer(&pd->timer, jiffies + HZ);
273 else
274 del_timer(&pd->timer);
Steffen Klassert16295be2010-01-06 19:47:10 +1100275
Steffen Klassert16295be2010-01-06 19:47:10 +1100276 return;
277}
278
Mathias Krausecf5868c2017-09-08 20:57:10 +0200279static void invoke_padata_reorder(struct work_struct *work)
280{
281 struct padata_parallel_queue *pqueue;
282 struct parallel_data *pd;
283
284 local_bh_disable();
285 pqueue = container_of(work, struct padata_parallel_queue, reorder_work);
286 pd = pqueue->pd;
287 padata_reorder(pd);
288 local_bh_enable();
289}
290
Kees Cooke99e88a2017-10-16 14:43:17 -0700291static void padata_reorder_timer(struct timer_list *t)
Steffen Klassertd46a5ac2010-05-19 13:43:14 +1000292{
Kees Cooke99e88a2017-10-16 14:43:17 -0700293 struct parallel_data *pd = from_timer(pd, t, timer);
Mathias Krausecf5868c2017-09-08 20:57:10 +0200294 unsigned int weight;
295 int target_cpu, cpu;
Steffen Klassertd46a5ac2010-05-19 13:43:14 +1000296
Mathias Krausecf5868c2017-09-08 20:57:10 +0200297 cpu = get_cpu();
298
299 /* We don't lock pd here to not interfere with parallel processing
300 * padata_reorder() calls on other CPUs. We just need any CPU out of
301 * the cpumask.pcpu set. It would be nice if it's the right one but
302 * it doesn't matter if we're off to the next one by using an outdated
303 * pd->processed value.
304 */
305 weight = cpumask_weight(pd->cpumask.pcpu);
306 target_cpu = padata_index_to_cpu(pd, pd->processed % weight);
307
308 /* ensure to call the reorder callback on the correct CPU */
309 if (cpu != target_cpu) {
310 struct padata_parallel_queue *pqueue;
311 struct padata_instance *pinst;
312
313 /* The timer function is serialized wrt itself -- no locking
314 * needed.
315 */
316 pinst = pd->pinst;
317 pqueue = per_cpu_ptr(pd->pqueue, target_cpu);
318 queue_work_on(target_cpu, pinst->wq, &pqueue->reorder_work);
319 } else {
320 padata_reorder(pd);
321 }
322
323 put_cpu();
Steffen Klassertd46a5ac2010-05-19 13:43:14 +1000324}
325
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400326static void padata_serial_worker(struct work_struct *serial_work)
Steffen Klassert16295be2010-01-06 19:47:10 +1100327{
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400328 struct padata_serial_queue *squeue;
Steffen Klassert16295be2010-01-06 19:47:10 +1100329 struct parallel_data *pd;
330 LIST_HEAD(local_list);
331
332 local_bh_disable();
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400333 squeue = container_of(serial_work, struct padata_serial_queue, work);
334 pd = squeue->pd;
Steffen Klassert16295be2010-01-06 19:47:10 +1100335
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400336 spin_lock(&squeue->serial.lock);
337 list_replace_init(&squeue->serial.list, &local_list);
338 spin_unlock(&squeue->serial.lock);
Steffen Klassert16295be2010-01-06 19:47:10 +1100339
340 while (!list_empty(&local_list)) {
341 struct padata_priv *padata;
342
343 padata = list_entry(local_list.next,
344 struct padata_priv, list);
345
346 list_del_init(&padata->list);
347
348 padata->serial(padata);
349 atomic_dec(&pd->refcnt);
350 }
351 local_bh_enable();
352}
353
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000354/**
Steffen Klassert16295be2010-01-06 19:47:10 +1100355 * padata_do_serial - padata serialization function
356 *
357 * @padata: object to be serialized.
358 *
359 * padata_do_serial must be called for every parallelized object.
360 * The serialization callback function will run with BHs off.
361 */
362void padata_do_serial(struct padata_priv *padata)
363{
364 int cpu;
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400365 struct padata_parallel_queue *pqueue;
Steffen Klassert16295be2010-01-06 19:47:10 +1100366 struct parallel_data *pd;
Mathias Krause350ef882017-09-08 20:57:11 +0200367 int reorder_via_wq = 0;
Steffen Klassert16295be2010-01-06 19:47:10 +1100368
369 pd = padata->pd;
370
371 cpu = get_cpu();
Mathias Krause350ef882017-09-08 20:57:11 +0200372
373 /* We need to run on the same CPU padata_do_parallel(.., padata, ..)
374 * was called on -- or, at least, enqueue the padata object into the
375 * correct per-cpu queue.
376 */
377 if (cpu != padata->cpu) {
378 reorder_via_wq = 1;
379 cpu = padata->cpu;
380 }
381
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400382 pqueue = per_cpu_ptr(pd->pqueue, cpu);
Steffen Klassert16295be2010-01-06 19:47:10 +1100383
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400384 spin_lock(&pqueue->reorder.lock);
Steffen Klassert16295be2010-01-06 19:47:10 +1100385 atomic_inc(&pd->reorder_objects);
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400386 list_add_tail(&padata->list, &pqueue->reorder.list);
387 spin_unlock(&pqueue->reorder.lock);
Steffen Klassert16295be2010-01-06 19:47:10 +1100388
389 put_cpu();
390
Mathias Krause350ef882017-09-08 20:57:11 +0200391 /* If we're running on the wrong CPU, call padata_reorder() via a
392 * kernel worker.
393 */
394 if (reorder_via_wq)
395 queue_work_on(cpu, pd->pinst->wq, &pqueue->reorder_work);
396 else
397 padata_reorder(pd);
Steffen Klassert16295be2010-01-06 19:47:10 +1100398}
399EXPORT_SYMBOL(padata_do_serial);
400
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400401static int padata_setup_cpumasks(struct parallel_data *pd,
402 const struct cpumask *pcpumask,
403 const struct cpumask *cbcpumask)
Steffen Klassert16295be2010-01-06 19:47:10 +1100404{
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400405 if (!alloc_cpumask_var(&pd->cpumask.pcpu, GFP_KERNEL))
406 return -ENOMEM;
407
Steffen Klassert13614e02012-03-28 08:43:21 +0200408 cpumask_and(pd->cpumask.pcpu, pcpumask, cpu_online_mask);
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400409 if (!alloc_cpumask_var(&pd->cpumask.cbcpu, GFP_KERNEL)) {
Jason A. Donenfeld07a77922017-04-07 02:33:30 +0200410 free_cpumask_var(pd->cpumask.pcpu);
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400411 return -ENOMEM;
412 }
413
Steffen Klassert13614e02012-03-28 08:43:21 +0200414 cpumask_and(pd->cpumask.cbcpu, cbcpumask, cpu_online_mask);
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400415 return 0;
416}
417
418static void __padata_list_init(struct padata_list *pd_list)
419{
420 INIT_LIST_HEAD(&pd_list->list);
421 spin_lock_init(&pd_list->lock);
422}
423
424/* Initialize all percpu queues used by serial workers */
425static void padata_init_squeues(struct parallel_data *pd)
426{
427 int cpu;
428 struct padata_serial_queue *squeue;
429
430 for_each_cpu(cpu, pd->cpumask.cbcpu) {
431 squeue = per_cpu_ptr(pd->squeue, cpu);
432 squeue->pd = pd;
433 __padata_list_init(&squeue->serial);
434 INIT_WORK(&squeue->work, padata_serial_worker);
435 }
436}
437
438/* Initialize all percpu queues used by parallel workers */
439static void padata_init_pqueues(struct parallel_data *pd)
440{
Steffen Klassert2dc9b5d2012-03-09 07:20:49 +0100441 int cpu_index, cpu;
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400442 struct padata_parallel_queue *pqueue;
Steffen Klassert16295be2010-01-06 19:47:10 +1100443
444 cpu_index = 0;
Mathias Krause1bd845b2017-09-08 20:57:09 +0200445 for_each_possible_cpu(cpu) {
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400446 pqueue = per_cpu_ptr(pd->pqueue, cpu);
Mathias Krause1bd845b2017-09-08 20:57:09 +0200447
448 if (!cpumask_test_cpu(cpu, pd->cpumask.pcpu)) {
449 pqueue->cpu_index = -1;
450 continue;
451 }
452
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400453 pqueue->pd = pd;
454 pqueue->cpu_index = cpu_index;
Steffen Klassertfad3a902010-07-20 08:48:34 +0200455 cpu_index++;
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400456
457 __padata_list_init(&pqueue->reorder);
458 __padata_list_init(&pqueue->parallel);
459 INIT_WORK(&pqueue->work, padata_parallel_worker);
Mathias Krausecf5868c2017-09-08 20:57:10 +0200460 INIT_WORK(&pqueue->reorder_work, invoke_padata_reorder);
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400461 atomic_set(&pqueue->num_obj, 0);
462 }
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400463}
464
465/* Allocate and initialize the internal cpumask dependend resources. */
466static struct parallel_data *padata_alloc_pd(struct padata_instance *pinst,
467 const struct cpumask *pcpumask,
468 const struct cpumask *cbcpumask)
469{
470 struct parallel_data *pd;
Steffen Klassert16295be2010-01-06 19:47:10 +1100471
472 pd = kzalloc(sizeof(struct parallel_data), GFP_KERNEL);
473 if (!pd)
474 goto err;
475
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400476 pd->pqueue = alloc_percpu(struct padata_parallel_queue);
477 if (!pd->pqueue)
Steffen Klassert16295be2010-01-06 19:47:10 +1100478 goto err_free_pd;
479
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400480 pd->squeue = alloc_percpu(struct padata_serial_queue);
481 if (!pd->squeue)
482 goto err_free_pqueue;
483 if (padata_setup_cpumasks(pd, pcpumask, cbcpumask) < 0)
484 goto err_free_squeue;
Steffen Klassert16295be2010-01-06 19:47:10 +1100485
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400486 padata_init_pqueues(pd);
487 padata_init_squeues(pd);
Kees Cooke99e88a2017-10-16 14:43:17 -0700488 timer_setup(&pd->timer, padata_reorder_timer, 0);
Mathias Krause0b6b0982013-10-25 12:14:15 +0200489 atomic_set(&pd->seq_nr, -1);
Steffen Klassert16295be2010-01-06 19:47:10 +1100490 atomic_set(&pd->reorder_objects, 0);
491 atomic_set(&pd->refcnt, 0);
492 pd->pinst = pinst;
493 spin_lock_init(&pd->lock);
494
495 return pd;
496
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400497err_free_squeue:
498 free_percpu(pd->squeue);
499err_free_pqueue:
500 free_percpu(pd->pqueue);
Steffen Klassert16295be2010-01-06 19:47:10 +1100501err_free_pd:
502 kfree(pd);
503err:
504 return NULL;
505}
506
507static void padata_free_pd(struct parallel_data *pd)
508{
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400509 free_cpumask_var(pd->cpumask.pcpu);
510 free_cpumask_var(pd->cpumask.cbcpu);
511 free_percpu(pd->pqueue);
512 free_percpu(pd->squeue);
Steffen Klassert16295be2010-01-06 19:47:10 +1100513 kfree(pd);
514}
515
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000516/* Flush all objects out of the padata queues. */
Steffen Klassert2b73b072010-05-19 13:43:46 +1000517static void padata_flush_queues(struct parallel_data *pd)
518{
519 int cpu;
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400520 struct padata_parallel_queue *pqueue;
521 struct padata_serial_queue *squeue;
Steffen Klassert2b73b072010-05-19 13:43:46 +1000522
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400523 for_each_cpu(cpu, pd->cpumask.pcpu) {
524 pqueue = per_cpu_ptr(pd->pqueue, cpu);
525 flush_work(&pqueue->work);
Steffen Klassert2b73b072010-05-19 13:43:46 +1000526 }
527
528 del_timer_sync(&pd->timer);
529
530 if (atomic_read(&pd->reorder_objects))
531 padata_reorder(pd);
532
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400533 for_each_cpu(cpu, pd->cpumask.cbcpu) {
534 squeue = per_cpu_ptr(pd->squeue, cpu);
535 flush_work(&squeue->work);
Steffen Klassert2b73b072010-05-19 13:43:46 +1000536 }
537
538 BUG_ON(atomic_read(&pd->refcnt) != 0);
539}
540
Steffen Klassert4c879172010-07-07 15:30:10 +0200541static void __padata_start(struct padata_instance *pinst)
542{
543 pinst->flags |= PADATA_INIT;
544}
545
Steffen Klassertee836552010-07-07 15:30:47 +0200546static void __padata_stop(struct padata_instance *pinst)
547{
548 if (!(pinst->flags & PADATA_INIT))
549 return;
550
551 pinst->flags &= ~PADATA_INIT;
552
553 synchronize_rcu();
554
555 get_online_cpus();
556 padata_flush_queues(pinst->pd);
557 put_online_cpus();
558}
559
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300560/* Replace the internal control structure with a new one. */
Steffen Klassert16295be2010-01-06 19:47:10 +1100561static void padata_replace(struct padata_instance *pinst,
562 struct parallel_data *pd_new)
563{
564 struct parallel_data *pd_old = pinst->pd;
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400565 int notification_mask = 0;
Steffen Klassert16295be2010-01-06 19:47:10 +1100566
567 pinst->flags |= PADATA_RESET;
568
569 rcu_assign_pointer(pinst->pd, pd_new);
570
571 synchronize_rcu();
572
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400573 if (!cpumask_equal(pd_old->cpumask.pcpu, pd_new->cpumask.pcpu))
574 notification_mask |= PADATA_CPU_PARALLEL;
575 if (!cpumask_equal(pd_old->cpumask.cbcpu, pd_new->cpumask.cbcpu))
576 notification_mask |= PADATA_CPU_SERIAL;
Steffen Klassert16295be2010-01-06 19:47:10 +1100577
Steffen Klassert16295be2010-01-06 19:47:10 +1100578 padata_flush_queues(pd_old);
579 padata_free_pd(pd_old);
580
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400581 if (notification_mask)
582 blocking_notifier_call_chain(&pinst->cpumask_change_notifier,
Steffen Klassertc6356962010-07-27 07:15:50 +0200583 notification_mask,
584 &pd_new->cpumask);
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400585
Steffen Klassert16295be2010-01-06 19:47:10 +1100586 pinst->flags &= ~PADATA_RESET;
587}
588
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000589/**
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400590 * padata_register_cpumask_notifier - Registers a notifier that will be called
591 * if either pcpu or cbcpu or both cpumasks change.
Steffen Klassert16295be2010-01-06 19:47:10 +1100592 *
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400593 * @pinst: A poineter to padata instance
594 * @nblock: A pointer to notifier block.
Steffen Klassert16295be2010-01-06 19:47:10 +1100595 */
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400596int padata_register_cpumask_notifier(struct padata_instance *pinst,
597 struct notifier_block *nblock)
Steffen Klassert16295be2010-01-06 19:47:10 +1100598{
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400599 return blocking_notifier_chain_register(&pinst->cpumask_change_notifier,
600 nblock);
601}
602EXPORT_SYMBOL(padata_register_cpumask_notifier);
Steffen Klassert16295be2010-01-06 19:47:10 +1100603
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400604/**
605 * padata_unregister_cpumask_notifier - Unregisters cpumask notifier
606 * registered earlier using padata_register_cpumask_notifier
607 *
608 * @pinst: A pointer to data instance.
609 * @nlock: A pointer to notifier block.
610 */
611int padata_unregister_cpumask_notifier(struct padata_instance *pinst,
612 struct notifier_block *nblock)
613{
614 return blocking_notifier_chain_unregister(
615 &pinst->cpumask_change_notifier,
616 nblock);
617}
618EXPORT_SYMBOL(padata_unregister_cpumask_notifier);
Steffen Klassert16295be2010-01-06 19:47:10 +1100619
Steffen Klassert6751fb32010-04-29 14:42:30 +0200620
Steffen Klassert33e54452010-07-07 15:31:26 +0200621/* If cpumask contains no active cpu, we mark the instance as invalid. */
622static bool padata_validate_cpumask(struct padata_instance *pinst,
623 const struct cpumask *cpumask)
624{
Steffen Klassert13614e02012-03-28 08:43:21 +0200625 if (!cpumask_intersects(cpumask, cpu_online_mask)) {
Steffen Klassert33e54452010-07-07 15:31:26 +0200626 pinst->flags |= PADATA_INVALID;
627 return false;
Steffen Klassert16295be2010-01-06 19:47:10 +1100628 }
629
Steffen Klassert33e54452010-07-07 15:31:26 +0200630 pinst->flags &= ~PADATA_INVALID;
631 return true;
632}
633
Steffen Klassert65ff5772010-07-27 07:15:06 +0200634static int __padata_set_cpumasks(struct padata_instance *pinst,
635 cpumask_var_t pcpumask,
636 cpumask_var_t cbcpumask)
Steffen Klassert16295be2010-01-06 19:47:10 +1100637{
Steffen Klassert33e54452010-07-07 15:31:26 +0200638 int valid;
Steffen Klassert65ff5772010-07-27 07:15:06 +0200639 struct parallel_data *pd;
Steffen Klassert16295be2010-01-06 19:47:10 +1100640
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400641 valid = padata_validate_cpumask(pinst, pcpumask);
642 if (!valid) {
643 __padata_stop(pinst);
644 goto out_replace;
645 }
646
647 valid = padata_validate_cpumask(pinst, cbcpumask);
Steffen Klassertb89661d2010-07-20 08:49:20 +0200648 if (!valid)
Steffen Klassert33e54452010-07-07 15:31:26 +0200649 __padata_stop(pinst);
Steffen Klassert33e54452010-07-07 15:31:26 +0200650
Steffen Klassertb89661d2010-07-20 08:49:20 +0200651out_replace:
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400652 pd = padata_alloc_pd(pinst, pcpumask, cbcpumask);
Steffen Klassert65ff5772010-07-27 07:15:06 +0200653 if (!pd)
654 return -ENOMEM;
Steffen Klassert16295be2010-01-06 19:47:10 +1100655
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400656 cpumask_copy(pinst->cpumask.pcpu, pcpumask);
657 cpumask_copy(pinst->cpumask.cbcpu, cbcpumask);
Steffen Klassert16295be2010-01-06 19:47:10 +1100658
659 padata_replace(pinst, pd);
660
Steffen Klassert33e54452010-07-07 15:31:26 +0200661 if (valid)
662 __padata_start(pinst);
663
Steffen Klassert65ff5772010-07-27 07:15:06 +0200664 return 0;
665}
666
667/**
Steffen Klassert65ff5772010-07-27 07:15:06 +0200668 * padata_set_cpumask: Sets specified by @cpumask_type cpumask to the value
669 * equivalent to @cpumask.
670 *
671 * @pinst: padata instance
672 * @cpumask_type: PADATA_CPU_SERIAL or PADATA_CPU_PARALLEL corresponding
673 * to parallel and serial cpumasks respectively.
674 * @cpumask: the cpumask to use
675 */
676int padata_set_cpumask(struct padata_instance *pinst, int cpumask_type,
677 cpumask_var_t cpumask)
678{
679 struct cpumask *serial_mask, *parallel_mask;
680 int err = -EINVAL;
681
682 mutex_lock(&pinst->lock);
683 get_online_cpus();
684
685 switch (cpumask_type) {
686 case PADATA_CPU_PARALLEL:
687 serial_mask = pinst->cpumask.cbcpu;
688 parallel_mask = cpumask;
689 break;
690 case PADATA_CPU_SERIAL:
691 parallel_mask = pinst->cpumask.pcpu;
692 serial_mask = cpumask;
693 break;
694 default:
695 goto out;
696 }
697
698 err = __padata_set_cpumasks(pinst, parallel_mask, serial_mask);
699
Steffen Klassert16295be2010-01-06 19:47:10 +1100700out:
Steffen Klassert6751fb32010-04-29 14:42:30 +0200701 put_online_cpus();
Steffen Klassert16295be2010-01-06 19:47:10 +1100702 mutex_unlock(&pinst->lock);
703
704 return err;
705}
706EXPORT_SYMBOL(padata_set_cpumask);
707
Arnd Bergmann19d795b2016-05-19 17:09:59 -0700708/**
709 * padata_start - start the parallel processing
710 *
711 * @pinst: padata instance to start
712 */
713int padata_start(struct padata_instance *pinst)
714{
715 int err = 0;
716
717 mutex_lock(&pinst->lock);
718
719 if (pinst->flags & PADATA_INVALID)
720 err = -EINVAL;
721
722 __padata_start(pinst);
723
724 mutex_unlock(&pinst->lock);
725
726 return err;
727}
728EXPORT_SYMBOL(padata_start);
729
730/**
731 * padata_stop - stop the parallel processing
732 *
733 * @pinst: padata instance to stop
734 */
735void padata_stop(struct padata_instance *pinst)
736{
737 mutex_lock(&pinst->lock);
738 __padata_stop(pinst);
739 mutex_unlock(&pinst->lock);
740}
741EXPORT_SYMBOL(padata_stop);
742
743#ifdef CONFIG_HOTPLUG_CPU
744
Steffen Klassert16295be2010-01-06 19:47:10 +1100745static int __padata_add_cpu(struct padata_instance *pinst, int cpu)
746{
747 struct parallel_data *pd;
748
Steffen Klassert13614e02012-03-28 08:43:21 +0200749 if (cpumask_test_cpu(cpu, cpu_online_mask)) {
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400750 pd = padata_alloc_pd(pinst, pinst->cpumask.pcpu,
751 pinst->cpumask.cbcpu);
Steffen Klassert16295be2010-01-06 19:47:10 +1100752 if (!pd)
753 return -ENOMEM;
754
755 padata_replace(pinst, pd);
Steffen Klassert33e54452010-07-07 15:31:26 +0200756
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400757 if (padata_validate_cpumask(pinst, pinst->cpumask.pcpu) &&
758 padata_validate_cpumask(pinst, pinst->cpumask.cbcpu))
Steffen Klassert33e54452010-07-07 15:31:26 +0200759 __padata_start(pinst);
Steffen Klassert16295be2010-01-06 19:47:10 +1100760 }
761
762 return 0;
763}
764
Steffen Klassert16295be2010-01-06 19:47:10 +1100765static int __padata_remove_cpu(struct padata_instance *pinst, int cpu)
766{
Steffen Klassert33e54452010-07-07 15:31:26 +0200767 struct parallel_data *pd = NULL;
Steffen Klassert16295be2010-01-06 19:47:10 +1100768
769 if (cpumask_test_cpu(cpu, cpu_online_mask)) {
Steffen Klassert33e54452010-07-07 15:31:26 +0200770
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400771 if (!padata_validate_cpumask(pinst, pinst->cpumask.pcpu) ||
Steffen Klassertb89661d2010-07-20 08:49:20 +0200772 !padata_validate_cpumask(pinst, pinst->cpumask.cbcpu))
Steffen Klassert33e54452010-07-07 15:31:26 +0200773 __padata_stop(pinst);
Steffen Klassert33e54452010-07-07 15:31:26 +0200774
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400775 pd = padata_alloc_pd(pinst, pinst->cpumask.pcpu,
776 pinst->cpumask.cbcpu);
Steffen Klassert16295be2010-01-06 19:47:10 +1100777 if (!pd)
778 return -ENOMEM;
779
780 padata_replace(pinst, pd);
Steffen Klassert96120902012-03-28 08:44:07 +0200781
782 cpumask_clear_cpu(cpu, pd->cpumask.cbcpu);
783 cpumask_clear_cpu(cpu, pd->cpumask.pcpu);
Steffen Klassert16295be2010-01-06 19:47:10 +1100784 }
785
786 return 0;
787}
788
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400789 /**
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300790 * padata_remove_cpu - remove a cpu from the one or both(serial and parallel)
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400791 * padata cpumasks.
Steffen Klassert16295be2010-01-06 19:47:10 +1100792 *
793 * @pinst: padata instance
794 * @cpu: cpu to remove
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400795 * @mask: bitmask specifying from which cpumask @cpu should be removed
796 * The @mask may be any combination of the following flags:
797 * PADATA_CPU_SERIAL - serial cpumask
798 * PADATA_CPU_PARALLEL - parallel cpumask
Steffen Klassert16295be2010-01-06 19:47:10 +1100799 */
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400800int padata_remove_cpu(struct padata_instance *pinst, int cpu, int mask)
Steffen Klassert16295be2010-01-06 19:47:10 +1100801{
802 int err;
803
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400804 if (!(mask & (PADATA_CPU_SERIAL | PADATA_CPU_PARALLEL)))
805 return -EINVAL;
806
Steffen Klassert16295be2010-01-06 19:47:10 +1100807 mutex_lock(&pinst->lock);
808
Steffen Klassert6751fb32010-04-29 14:42:30 +0200809 get_online_cpus();
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400810 if (mask & PADATA_CPU_SERIAL)
811 cpumask_clear_cpu(cpu, pinst->cpumask.cbcpu);
812 if (mask & PADATA_CPU_PARALLEL)
813 cpumask_clear_cpu(cpu, pinst->cpumask.pcpu);
814
Steffen Klassert16295be2010-01-06 19:47:10 +1100815 err = __padata_remove_cpu(pinst, cpu);
Steffen Klassert6751fb32010-04-29 14:42:30 +0200816 put_online_cpus();
Steffen Klassert16295be2010-01-06 19:47:10 +1100817
818 mutex_unlock(&pinst->lock);
819
820 return err;
821}
822EXPORT_SYMBOL(padata_remove_cpu);
823
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400824static inline int pinst_has_cpu(struct padata_instance *pinst, int cpu)
825{
826 return cpumask_test_cpu(cpu, pinst->cpumask.pcpu) ||
827 cpumask_test_cpu(cpu, pinst->cpumask.cbcpu);
828}
829
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +0200830static int padata_cpu_online(unsigned int cpu, struct hlist_node *node)
Steffen Klassert16295be2010-01-06 19:47:10 +1100831{
Steffen Klassert16295be2010-01-06 19:47:10 +1100832 struct padata_instance *pinst;
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +0200833 int ret;
Steffen Klassert16295be2010-01-06 19:47:10 +1100834
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +0200835 pinst = hlist_entry_safe(node, struct padata_instance, node);
836 if (!pinst_has_cpu(pinst, cpu))
837 return 0;
Steffen Klassert16295be2010-01-06 19:47:10 +1100838
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +0200839 mutex_lock(&pinst->lock);
840 ret = __padata_add_cpu(pinst, cpu);
841 mutex_unlock(&pinst->lock);
842 return ret;
Steffen Klassert16295be2010-01-06 19:47:10 +1100843}
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +0200844
845static int padata_cpu_prep_down(unsigned int cpu, struct hlist_node *node)
846{
847 struct padata_instance *pinst;
848 int ret;
849
850 pinst = hlist_entry_safe(node, struct padata_instance, node);
851 if (!pinst_has_cpu(pinst, cpu))
852 return 0;
853
854 mutex_lock(&pinst->lock);
855 ret = __padata_remove_cpu(pinst, cpu);
856 mutex_unlock(&pinst->lock);
857 return ret;
858}
859
860static enum cpuhp_state hp_online;
Steffen Klasserte2cb2f12010-04-29 14:40:10 +0200861#endif
Steffen Klassert16295be2010-01-06 19:47:10 +1100862
Dan Kruchinin5e017dc2010-07-14 14:33:08 +0400863static void __padata_free(struct padata_instance *pinst)
864{
865#ifdef CONFIG_HOTPLUG_CPU
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +0200866 cpuhp_state_remove_instance_nocalls(hp_online, &pinst->node);
Dan Kruchinin5e017dc2010-07-14 14:33:08 +0400867#endif
868
869 padata_stop(pinst);
870 padata_free_pd(pinst->pd);
871 free_cpumask_var(pinst->cpumask.pcpu);
872 free_cpumask_var(pinst->cpumask.cbcpu);
873 kfree(pinst);
874}
875
876#define kobj2pinst(_kobj) \
877 container_of(_kobj, struct padata_instance, kobj)
878#define attr2pentry(_attr) \
879 container_of(_attr, struct padata_sysfs_entry, attr)
880
881static void padata_sysfs_release(struct kobject *kobj)
882{
883 struct padata_instance *pinst = kobj2pinst(kobj);
884 __padata_free(pinst);
885}
886
887struct padata_sysfs_entry {
888 struct attribute attr;
889 ssize_t (*show)(struct padata_instance *, struct attribute *, char *);
890 ssize_t (*store)(struct padata_instance *, struct attribute *,
891 const char *, size_t);
892};
893
894static ssize_t show_cpumask(struct padata_instance *pinst,
895 struct attribute *attr, char *buf)
896{
897 struct cpumask *cpumask;
898 ssize_t len;
899
900 mutex_lock(&pinst->lock);
901 if (!strcmp(attr->name, "serial_cpumask"))
902 cpumask = pinst->cpumask.cbcpu;
903 else
904 cpumask = pinst->cpumask.pcpu;
905
Tejun Heo4497da62015-02-13 14:38:05 -0800906 len = snprintf(buf, PAGE_SIZE, "%*pb\n",
907 nr_cpu_ids, cpumask_bits(cpumask));
Dan Kruchinin5e017dc2010-07-14 14:33:08 +0400908 mutex_unlock(&pinst->lock);
Tejun Heo4497da62015-02-13 14:38:05 -0800909 return len < PAGE_SIZE ? len : -EINVAL;
Dan Kruchinin5e017dc2010-07-14 14:33:08 +0400910}
911
912static ssize_t store_cpumask(struct padata_instance *pinst,
913 struct attribute *attr,
914 const char *buf, size_t count)
915{
916 cpumask_var_t new_cpumask;
917 ssize_t ret;
918 int mask_type;
919
920 if (!alloc_cpumask_var(&new_cpumask, GFP_KERNEL))
921 return -ENOMEM;
922
923 ret = bitmap_parse(buf, count, cpumask_bits(new_cpumask),
924 nr_cpumask_bits);
925 if (ret < 0)
926 goto out;
927
928 mask_type = !strcmp(attr->name, "serial_cpumask") ?
929 PADATA_CPU_SERIAL : PADATA_CPU_PARALLEL;
930 ret = padata_set_cpumask(pinst, mask_type, new_cpumask);
931 if (!ret)
932 ret = count;
933
934out:
935 free_cpumask_var(new_cpumask);
936 return ret;
937}
938
939#define PADATA_ATTR_RW(_name, _show_name, _store_name) \
940 static struct padata_sysfs_entry _name##_attr = \
941 __ATTR(_name, 0644, _show_name, _store_name)
942#define PADATA_ATTR_RO(_name, _show_name) \
943 static struct padata_sysfs_entry _name##_attr = \
944 __ATTR(_name, 0400, _show_name, NULL)
945
946PADATA_ATTR_RW(serial_cpumask, show_cpumask, store_cpumask);
947PADATA_ATTR_RW(parallel_cpumask, show_cpumask, store_cpumask);
948
949/*
950 * Padata sysfs provides the following objects:
951 * serial_cpumask [RW] - cpumask for serial workers
952 * parallel_cpumask [RW] - cpumask for parallel workers
Steffen Klassert16295be2010-01-06 19:47:10 +1100953 */
Dan Kruchinin5e017dc2010-07-14 14:33:08 +0400954static struct attribute *padata_default_attrs[] = {
955 &serial_cpumask_attr.attr,
956 &parallel_cpumask_attr.attr,
957 NULL,
958};
959
960static ssize_t padata_sysfs_show(struct kobject *kobj,
961 struct attribute *attr, char *buf)
Steffen Klassert16295be2010-01-06 19:47:10 +1100962{
Steffen Klassert16295be2010-01-06 19:47:10 +1100963 struct padata_instance *pinst;
Dan Kruchinin5e017dc2010-07-14 14:33:08 +0400964 struct padata_sysfs_entry *pentry;
965 ssize_t ret = -EIO;
966
967 pinst = kobj2pinst(kobj);
968 pentry = attr2pentry(attr);
969 if (pentry->show)
970 ret = pentry->show(pinst, attr, buf);
971
972 return ret;
973}
974
975static ssize_t padata_sysfs_store(struct kobject *kobj, struct attribute *attr,
976 const char *buf, size_t count)
977{
978 struct padata_instance *pinst;
979 struct padata_sysfs_entry *pentry;
980 ssize_t ret = -EIO;
981
982 pinst = kobj2pinst(kobj);
983 pentry = attr2pentry(attr);
984 if (pentry->show)
985 ret = pentry->store(pinst, attr, buf, count);
986
987 return ret;
988}
989
990static const struct sysfs_ops padata_sysfs_ops = {
991 .show = padata_sysfs_show,
992 .store = padata_sysfs_store,
993};
994
995static struct kobj_type padata_attr_type = {
996 .sysfs_ops = &padata_sysfs_ops,
997 .default_attrs = padata_default_attrs,
998 .release = padata_sysfs_release,
999};
1000
Steffen Klassert16295be2010-01-06 19:47:10 +11001001/**
Steffen Klasserte6cc1172010-07-27 07:14:28 +02001002 * padata_alloc - allocate and initialize a padata instance and specify
1003 * cpumasks for serial and parallel workers.
Dan Kruchinine15bacb2010-07-14 14:31:57 +04001004 *
1005 * @wq: workqueue to use for the allocated padata instance
1006 * @pcpumask: cpumask that will be used for padata parallelization
1007 * @cbcpumask: cpumask that will be used for padata serialization
Sebastian Andrzej Siewiorc5a81c82017-05-24 10:15:18 +02001008 *
1009 * Must be called from a cpus_read_lock() protected region
Dan Kruchinine15bacb2010-07-14 14:31:57 +04001010 */
Thomas Gleixner95966952017-05-24 10:15:17 +02001011static struct padata_instance *padata_alloc(struct workqueue_struct *wq,
1012 const struct cpumask *pcpumask,
1013 const struct cpumask *cbcpumask)
Steffen Klassert16295be2010-01-06 19:47:10 +11001014{
1015 struct padata_instance *pinst;
Steffen Klassert33e54452010-07-07 15:31:26 +02001016 struct parallel_data *pd = NULL;
Steffen Klassert16295be2010-01-06 19:47:10 +11001017
1018 pinst = kzalloc(sizeof(struct padata_instance), GFP_KERNEL);
1019 if (!pinst)
1020 goto err;
1021
Dan Kruchinine15bacb2010-07-14 14:31:57 +04001022 if (!alloc_cpumask_var(&pinst->cpumask.pcpu, GFP_KERNEL))
Steffen Klassert16295be2010-01-06 19:47:10 +11001023 goto err_free_inst;
Dan Kruchinine15bacb2010-07-14 14:31:57 +04001024 if (!alloc_cpumask_var(&pinst->cpumask.cbcpu, GFP_KERNEL)) {
1025 free_cpumask_var(pinst->cpumask.pcpu);
1026 goto err_free_inst;
Steffen Klassert33e54452010-07-07 15:31:26 +02001027 }
Dan Kruchinine15bacb2010-07-14 14:31:57 +04001028 if (!padata_validate_cpumask(pinst, pcpumask) ||
1029 !padata_validate_cpumask(pinst, cbcpumask))
1030 goto err_free_masks;
Steffen Klassert16295be2010-01-06 19:47:10 +11001031
Dan Kruchinine15bacb2010-07-14 14:31:57 +04001032 pd = padata_alloc_pd(pinst, pcpumask, cbcpumask);
1033 if (!pd)
1034 goto err_free_masks;
Steffen Klassert74781382010-03-04 13:30:22 +08001035
Steffen Klassert16295be2010-01-06 19:47:10 +11001036 rcu_assign_pointer(pinst->pd, pd);
1037
1038 pinst->wq = wq;
1039
Dan Kruchinine15bacb2010-07-14 14:31:57 +04001040 cpumask_copy(pinst->cpumask.pcpu, pcpumask);
1041 cpumask_copy(pinst->cpumask.cbcpu, cbcpumask);
Steffen Klassert16295be2010-01-06 19:47:10 +11001042
1043 pinst->flags = 0;
1044
Dan Kruchinine15bacb2010-07-14 14:31:57 +04001045 BLOCKING_INIT_NOTIFIER_HEAD(&pinst->cpumask_change_notifier);
Dan Kruchinin5e017dc2010-07-14 14:33:08 +04001046 kobject_init(&pinst->kobj, &padata_attr_type);
Steffen Klassert16295be2010-01-06 19:47:10 +11001047 mutex_init(&pinst->lock);
1048
Richard Weinbergerb8b4a412013-08-23 13:12:33 +02001049#ifdef CONFIG_HOTPLUG_CPU
Sebastian Andrzej Siewiorc5a81c82017-05-24 10:15:18 +02001050 cpuhp_state_add_instance_nocalls_cpuslocked(hp_online, &pinst->node);
Richard Weinbergerb8b4a412013-08-23 13:12:33 +02001051#endif
Steffen Klassert16295be2010-01-06 19:47:10 +11001052 return pinst;
1053
Dan Kruchinine15bacb2010-07-14 14:31:57 +04001054err_free_masks:
1055 free_cpumask_var(pinst->cpumask.pcpu);
1056 free_cpumask_var(pinst->cpumask.cbcpu);
Steffen Klassert16295be2010-01-06 19:47:10 +11001057err_free_inst:
1058 kfree(pinst);
1059err:
1060 return NULL;
1061}
Steffen Klassert16295be2010-01-06 19:47:10 +11001062
Steffen Klassert0198ffd2010-05-19 13:44:27 +10001063/**
Thomas Gleixner95966952017-05-24 10:15:17 +02001064 * padata_alloc_possible - Allocate and initialize padata instance.
1065 * Use the cpu_possible_mask for serial and
1066 * parallel workers.
1067 *
1068 * @wq: workqueue to use for the allocated padata instance
Sebastian Andrzej Siewiorc5a81c82017-05-24 10:15:18 +02001069 *
1070 * Must be called from a cpus_read_lock() protected region
Thomas Gleixner95966952017-05-24 10:15:17 +02001071 */
1072struct padata_instance *padata_alloc_possible(struct workqueue_struct *wq)
1073{
Sebastian Andrzej Siewiorc5a81c82017-05-24 10:15:18 +02001074 lockdep_assert_cpus_held();
Thomas Gleixner95966952017-05-24 10:15:17 +02001075 return padata_alloc(wq, cpu_possible_mask, cpu_possible_mask);
1076}
1077EXPORT_SYMBOL(padata_alloc_possible);
1078
1079/**
Steffen Klassert16295be2010-01-06 19:47:10 +11001080 * padata_free - free a padata instance
1081 *
Steffen Klassert0198ffd2010-05-19 13:44:27 +10001082 * @padata_inst: padata instance to free
Steffen Klassert16295be2010-01-06 19:47:10 +11001083 */
1084void padata_free(struct padata_instance *pinst)
1085{
Dan Kruchinin5e017dc2010-07-14 14:33:08 +04001086 kobject_put(&pinst->kobj);
Steffen Klassert16295be2010-01-06 19:47:10 +11001087}
1088EXPORT_SYMBOL(padata_free);
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +02001089
1090#ifdef CONFIG_HOTPLUG_CPU
1091
1092static __init int padata_driver_init(void)
1093{
1094 int ret;
1095
1096 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "padata:online",
1097 padata_cpu_online,
1098 padata_cpu_prep_down);
1099 if (ret < 0)
1100 return ret;
1101 hp_online = ret;
1102 return 0;
1103}
1104module_init(padata_driver_init);
1105
1106static __exit void padata_driver_exit(void)
1107{
1108 cpuhp_remove_multi_state(hp_online);
1109}
1110module_exit(padata_driver_exit);
1111#endif