blob: 05316c9f32da9d0e20b3d3c92eeaf3eb49f1deef [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);
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400134 queue = per_cpu_ptr(pd->pqueue, target_cpu);
Steffen Klassert16295be2010-01-06 19:47:10 +1100135
136 spin_lock(&queue->parallel.lock);
137 list_add_tail(&padata->list, &queue->parallel.list);
138 spin_unlock(&queue->parallel.lock);
139
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400140 queue_work_on(target_cpu, pinst->wq, &queue->work);
Steffen Klassert16295be2010-01-06 19:47:10 +1100141
142out:
143 rcu_read_unlock_bh();
144
145 return err;
146}
147EXPORT_SYMBOL(padata_do_parallel);
148
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000149/*
150 * padata_get_next - Get the next object that needs serialization.
151 *
152 * Return values are:
153 *
154 * A pointer to the control struct of the next object that needs
155 * serialization, if present in one of the percpu reorder queues.
156 *
157 * NULL, if all percpu reorder queues are empty.
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 */
Steffen Klassert16295be2010-01-06 19:47:10 +1100166static struct padata_priv *padata_get_next(struct parallel_data *pd)
167{
Steffen Klassert5f1a8c12010-07-07 15:32:39 +0200168 int cpu, num_cpus;
Steffen Klassert2dc9b5d2012-03-09 07:20:49 +0100169 unsigned int next_nr, next_index;
Shan Weif0fcf202012-12-06 17:16:23 +0800170 struct padata_parallel_queue *next_queue;
Steffen Klassert16295be2010-01-06 19:47:10 +1100171 struct padata_priv *padata;
172 struct padata_list *reorder;
173
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400174 num_cpus = cpumask_weight(pd->cpumask.pcpu);
Steffen Klassert16295be2010-01-06 19:47:10 +1100175
Steffen Klassert5f1a8c12010-07-07 15:32:39 +0200176 /*
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);
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400183 next_queue = per_cpu_ptr(pd->pqueue, cpu);
Steffen Klassert16295be2010-01-06 19:47:10 +1100184
Steffen Klassert16295be2010-01-06 19:47:10 +1100185 padata = NULL;
186
Steffen Klassert16295be2010-01-06 19:47:10 +1100187 reorder = &next_queue->reorder;
188
189 if (!list_empty(&reorder->list)) {
190 padata = list_entry(reorder->list.next,
191 struct padata_priv, list);
192
Steffen Klassert16295be2010-01-06 19:47:10 +1100193 spin_lock(&reorder->lock);
194 list_del_init(&padata->list);
195 atomic_dec(&pd->reorder_objects);
196 spin_unlock(&reorder->lock);
197
Steffen Klassert5f1a8c12010-07-07 15:32:39 +0200198 pd->processed++;
Steffen Klassert16295be2010-01-06 19:47:10 +1100199
200 goto out;
201 }
202
Shan Weif0fcf202012-12-06 17:16:23 +0800203 if (__this_cpu_read(pd->pqueue->cpu_index) == next_queue->cpu_index) {
Steffen Klassert16295be2010-01-06 19:47:10 +1100204 padata = ERR_PTR(-ENODATA);
205 goto out;
206 }
207
208 padata = ERR_PTR(-EINPROGRESS);
209out:
210 return padata;
211}
212
213static void padata_reorder(struct parallel_data *pd)
214{
Steffen Klassert30478172012-03-09 07:20:12 +0100215 int cb_cpu;
Steffen Klassert16295be2010-01-06 19:47:10 +1100216 struct padata_priv *padata;
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400217 struct padata_serial_queue *squeue;
Steffen Klassert16295be2010-01-06 19:47:10 +1100218 struct padata_instance *pinst = pd->pinst;
219
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000220 /*
221 * We need to ensure that only one cpu can work on dequeueing of
222 * the reorder queue the time. Calculating in which percpu reorder
223 * queue the next object will arrive takes some time. A spinlock
224 * would be highly contended. Also it is not clear in which order
225 * the objects arrive to the reorder queues. So a cpu could wait to
226 * get the lock just to notice that there is nothing to do at the
227 * moment. Therefore we use a trylock and let the holder of the lock
228 * care for all the objects enqueued during the holdtime of the lock.
229 */
Steffen Klassert16295be2010-01-06 19:47:10 +1100230 if (!spin_trylock_bh(&pd->lock))
Steffen Klassertd46a5ac2010-05-19 13:43:14 +1000231 return;
Steffen Klassert16295be2010-01-06 19:47:10 +1100232
233 while (1) {
234 padata = padata_get_next(pd);
235
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000236 /*
237 * All reorder queues are empty, or the next object that needs
238 * serialization is parallel processed by another cpu and is
239 * still on it's way to the cpu's reorder queue, nothing to
240 * do for now.
241 */
Steffen Klassert16295be2010-01-06 19:47:10 +1100242 if (!padata || PTR_ERR(padata) == -EINPROGRESS)
243 break;
244
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000245 /*
246 * This cpu has to do the parallel processing of the next
247 * object. It's waiting in the cpu's parallelization queue,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300248 * so exit immediately.
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000249 */
Steffen Klassert16295be2010-01-06 19:47:10 +1100250 if (PTR_ERR(padata) == -ENODATA) {
Steffen Klassertd46a5ac2010-05-19 13:43:14 +1000251 del_timer(&pd->timer);
Steffen Klassert16295be2010-01-06 19:47:10 +1100252 spin_unlock_bh(&pd->lock);
Steffen Klassertd46a5ac2010-05-19 13:43:14 +1000253 return;
Steffen Klassert16295be2010-01-06 19:47:10 +1100254 }
255
Steffen Klassert30478172012-03-09 07:20:12 +0100256 cb_cpu = padata->cb_cpu;
257 squeue = per_cpu_ptr(pd->squeue, cb_cpu);
Steffen Klassert16295be2010-01-06 19:47:10 +1100258
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400259 spin_lock(&squeue->serial.lock);
260 list_add_tail(&padata->list, &squeue->serial.list);
261 spin_unlock(&squeue->serial.lock);
Steffen Klassert16295be2010-01-06 19:47:10 +1100262
Steffen Klassert30478172012-03-09 07:20:12 +0100263 queue_work_on(cb_cpu, pinst->wq, &squeue->work);
Steffen Klassert16295be2010-01-06 19:47:10 +1100264 }
265
266 spin_unlock_bh(&pd->lock);
267
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000268 /*
269 * The next object that needs serialization might have arrived to
270 * the reorder queues in the meantime, we will be called again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300271 * from the timer function if no one else cares for it.
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000272 */
Steffen Klassertd46a5ac2010-05-19 13:43:14 +1000273 if (atomic_read(&pd->reorder_objects)
274 && !(pinst->flags & PADATA_RESET))
275 mod_timer(&pd->timer, jiffies + HZ);
276 else
277 del_timer(&pd->timer);
Steffen Klassert16295be2010-01-06 19:47:10 +1100278
Steffen Klassert16295be2010-01-06 19:47:10 +1100279 return;
280}
281
Steffen Klassertd46a5ac2010-05-19 13:43:14 +1000282static void padata_reorder_timer(unsigned long arg)
283{
284 struct parallel_data *pd = (struct parallel_data *)arg;
285
286 padata_reorder(pd);
287}
288
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400289static void padata_serial_worker(struct work_struct *serial_work)
Steffen Klassert16295be2010-01-06 19:47:10 +1100290{
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400291 struct padata_serial_queue *squeue;
Steffen Klassert16295be2010-01-06 19:47:10 +1100292 struct parallel_data *pd;
293 LIST_HEAD(local_list);
294
295 local_bh_disable();
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400296 squeue = container_of(serial_work, struct padata_serial_queue, work);
297 pd = squeue->pd;
Steffen Klassert16295be2010-01-06 19:47:10 +1100298
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400299 spin_lock(&squeue->serial.lock);
300 list_replace_init(&squeue->serial.list, &local_list);
301 spin_unlock(&squeue->serial.lock);
Steffen Klassert16295be2010-01-06 19:47:10 +1100302
303 while (!list_empty(&local_list)) {
304 struct padata_priv *padata;
305
306 padata = list_entry(local_list.next,
307 struct padata_priv, list);
308
309 list_del_init(&padata->list);
310
311 padata->serial(padata);
312 atomic_dec(&pd->refcnt);
313 }
314 local_bh_enable();
315}
316
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000317/**
Steffen Klassert16295be2010-01-06 19:47:10 +1100318 * padata_do_serial - padata serialization function
319 *
320 * @padata: object to be serialized.
321 *
322 * padata_do_serial must be called for every parallelized object.
323 * The serialization callback function will run with BHs off.
324 */
325void padata_do_serial(struct padata_priv *padata)
326{
327 int cpu;
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400328 struct padata_parallel_queue *pqueue;
Steffen Klassert16295be2010-01-06 19:47:10 +1100329 struct parallel_data *pd;
330
331 pd = padata->pd;
332
333 cpu = get_cpu();
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400334 pqueue = per_cpu_ptr(pd->pqueue, cpu);
Steffen Klassert16295be2010-01-06 19:47:10 +1100335
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400336 spin_lock(&pqueue->reorder.lock);
Steffen Klassert16295be2010-01-06 19:47:10 +1100337 atomic_inc(&pd->reorder_objects);
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400338 list_add_tail(&padata->list, &pqueue->reorder.list);
339 spin_unlock(&pqueue->reorder.lock);
Steffen Klassert16295be2010-01-06 19:47:10 +1100340
341 put_cpu();
342
343 padata_reorder(pd);
344}
345EXPORT_SYMBOL(padata_do_serial);
346
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400347static int padata_setup_cpumasks(struct parallel_data *pd,
348 const struct cpumask *pcpumask,
349 const struct cpumask *cbcpumask)
Steffen Klassert16295be2010-01-06 19:47:10 +1100350{
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400351 if (!alloc_cpumask_var(&pd->cpumask.pcpu, GFP_KERNEL))
352 return -ENOMEM;
353
Steffen Klassert13614e02012-03-28 08:43:21 +0200354 cpumask_and(pd->cpumask.pcpu, pcpumask, cpu_online_mask);
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400355 if (!alloc_cpumask_var(&pd->cpumask.cbcpu, GFP_KERNEL)) {
356 free_cpumask_var(pd->cpumask.cbcpu);
357 return -ENOMEM;
358 }
359
Steffen Klassert13614e02012-03-28 08:43:21 +0200360 cpumask_and(pd->cpumask.cbcpu, cbcpumask, cpu_online_mask);
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400361 return 0;
362}
363
364static void __padata_list_init(struct padata_list *pd_list)
365{
366 INIT_LIST_HEAD(&pd_list->list);
367 spin_lock_init(&pd_list->lock);
368}
369
370/* Initialize all percpu queues used by serial workers */
371static void padata_init_squeues(struct parallel_data *pd)
372{
373 int cpu;
374 struct padata_serial_queue *squeue;
375
376 for_each_cpu(cpu, pd->cpumask.cbcpu) {
377 squeue = per_cpu_ptr(pd->squeue, cpu);
378 squeue->pd = pd;
379 __padata_list_init(&squeue->serial);
380 INIT_WORK(&squeue->work, padata_serial_worker);
381 }
382}
383
384/* Initialize all percpu queues used by parallel workers */
385static void padata_init_pqueues(struct parallel_data *pd)
386{
Steffen Klassert2dc9b5d2012-03-09 07:20:49 +0100387 int cpu_index, cpu;
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400388 struct padata_parallel_queue *pqueue;
Steffen Klassert16295be2010-01-06 19:47:10 +1100389
390 cpu_index = 0;
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400391 for_each_cpu(cpu, pd->cpumask.pcpu) {
392 pqueue = per_cpu_ptr(pd->pqueue, cpu);
393 pqueue->pd = pd;
394 pqueue->cpu_index = cpu_index;
Steffen Klassertfad3a902010-07-20 08:48:34 +0200395 cpu_index++;
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400396
397 __padata_list_init(&pqueue->reorder);
398 __padata_list_init(&pqueue->parallel);
399 INIT_WORK(&pqueue->work, padata_parallel_worker);
400 atomic_set(&pqueue->num_obj, 0);
401 }
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400402}
403
404/* Allocate and initialize the internal cpumask dependend resources. */
405static struct parallel_data *padata_alloc_pd(struct padata_instance *pinst,
406 const struct cpumask *pcpumask,
407 const struct cpumask *cbcpumask)
408{
409 struct parallel_data *pd;
Steffen Klassert16295be2010-01-06 19:47:10 +1100410
411 pd = kzalloc(sizeof(struct parallel_data), GFP_KERNEL);
412 if (!pd)
413 goto err;
414
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400415 pd->pqueue = alloc_percpu(struct padata_parallel_queue);
416 if (!pd->pqueue)
Steffen Klassert16295be2010-01-06 19:47:10 +1100417 goto err_free_pd;
418
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400419 pd->squeue = alloc_percpu(struct padata_serial_queue);
420 if (!pd->squeue)
421 goto err_free_pqueue;
422 if (padata_setup_cpumasks(pd, pcpumask, cbcpumask) < 0)
423 goto err_free_squeue;
Steffen Klassert16295be2010-01-06 19:47:10 +1100424
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400425 padata_init_pqueues(pd);
426 padata_init_squeues(pd);
Steffen Klassertd46a5ac2010-05-19 13:43:14 +1000427 setup_timer(&pd->timer, padata_reorder_timer, (unsigned long)pd);
Mathias Krause0b6b0982013-10-25 12:14:15 +0200428 atomic_set(&pd->seq_nr, -1);
Steffen Klassert16295be2010-01-06 19:47:10 +1100429 atomic_set(&pd->reorder_objects, 0);
430 atomic_set(&pd->refcnt, 0);
431 pd->pinst = pinst;
432 spin_lock_init(&pd->lock);
433
434 return pd;
435
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400436err_free_squeue:
437 free_percpu(pd->squeue);
438err_free_pqueue:
439 free_percpu(pd->pqueue);
Steffen Klassert16295be2010-01-06 19:47:10 +1100440err_free_pd:
441 kfree(pd);
442err:
443 return NULL;
444}
445
446static void padata_free_pd(struct parallel_data *pd)
447{
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400448 free_cpumask_var(pd->cpumask.pcpu);
449 free_cpumask_var(pd->cpumask.cbcpu);
450 free_percpu(pd->pqueue);
451 free_percpu(pd->squeue);
Steffen Klassert16295be2010-01-06 19:47:10 +1100452 kfree(pd);
453}
454
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000455/* Flush all objects out of the padata queues. */
Steffen Klassert2b73b072010-05-19 13:43:46 +1000456static void padata_flush_queues(struct parallel_data *pd)
457{
458 int cpu;
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400459 struct padata_parallel_queue *pqueue;
460 struct padata_serial_queue *squeue;
Steffen Klassert2b73b072010-05-19 13:43:46 +1000461
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400462 for_each_cpu(cpu, pd->cpumask.pcpu) {
463 pqueue = per_cpu_ptr(pd->pqueue, cpu);
464 flush_work(&pqueue->work);
Steffen Klassert2b73b072010-05-19 13:43:46 +1000465 }
466
467 del_timer_sync(&pd->timer);
468
469 if (atomic_read(&pd->reorder_objects))
470 padata_reorder(pd);
471
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400472 for_each_cpu(cpu, pd->cpumask.cbcpu) {
473 squeue = per_cpu_ptr(pd->squeue, cpu);
474 flush_work(&squeue->work);
Steffen Klassert2b73b072010-05-19 13:43:46 +1000475 }
476
477 BUG_ON(atomic_read(&pd->refcnt) != 0);
478}
479
Steffen Klassert4c879172010-07-07 15:30:10 +0200480static void __padata_start(struct padata_instance *pinst)
481{
482 pinst->flags |= PADATA_INIT;
483}
484
Steffen Klassertee836552010-07-07 15:30:47 +0200485static void __padata_stop(struct padata_instance *pinst)
486{
487 if (!(pinst->flags & PADATA_INIT))
488 return;
489
490 pinst->flags &= ~PADATA_INIT;
491
492 synchronize_rcu();
493
494 get_online_cpus();
495 padata_flush_queues(pinst->pd);
496 put_online_cpus();
497}
498
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300499/* Replace the internal control structure with a new one. */
Steffen Klassert16295be2010-01-06 19:47:10 +1100500static void padata_replace(struct padata_instance *pinst,
501 struct parallel_data *pd_new)
502{
503 struct parallel_data *pd_old = pinst->pd;
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400504 int notification_mask = 0;
Steffen Klassert16295be2010-01-06 19:47:10 +1100505
506 pinst->flags |= PADATA_RESET;
507
508 rcu_assign_pointer(pinst->pd, pd_new);
509
510 synchronize_rcu();
511
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400512 if (!cpumask_equal(pd_old->cpumask.pcpu, pd_new->cpumask.pcpu))
513 notification_mask |= PADATA_CPU_PARALLEL;
514 if (!cpumask_equal(pd_old->cpumask.cbcpu, pd_new->cpumask.cbcpu))
515 notification_mask |= PADATA_CPU_SERIAL;
Steffen Klassert16295be2010-01-06 19:47:10 +1100516
Steffen Klassert16295be2010-01-06 19:47:10 +1100517 padata_flush_queues(pd_old);
518 padata_free_pd(pd_old);
519
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400520 if (notification_mask)
521 blocking_notifier_call_chain(&pinst->cpumask_change_notifier,
Steffen Klassertc6356962010-07-27 07:15:50 +0200522 notification_mask,
523 &pd_new->cpumask);
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400524
Steffen Klassert16295be2010-01-06 19:47:10 +1100525 pinst->flags &= ~PADATA_RESET;
526}
527
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000528/**
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400529 * padata_register_cpumask_notifier - Registers a notifier that will be called
530 * if either pcpu or cbcpu or both cpumasks change.
Steffen Klassert16295be2010-01-06 19:47:10 +1100531 *
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400532 * @pinst: A poineter to padata instance
533 * @nblock: A pointer to notifier block.
Steffen Klassert16295be2010-01-06 19:47:10 +1100534 */
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400535int padata_register_cpumask_notifier(struct padata_instance *pinst,
536 struct notifier_block *nblock)
Steffen Klassert16295be2010-01-06 19:47:10 +1100537{
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400538 return blocking_notifier_chain_register(&pinst->cpumask_change_notifier,
539 nblock);
540}
541EXPORT_SYMBOL(padata_register_cpumask_notifier);
Steffen Klassert16295be2010-01-06 19:47:10 +1100542
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400543/**
544 * padata_unregister_cpumask_notifier - Unregisters cpumask notifier
545 * registered earlier using padata_register_cpumask_notifier
546 *
547 * @pinst: A pointer to data instance.
548 * @nlock: A pointer to notifier block.
549 */
550int padata_unregister_cpumask_notifier(struct padata_instance *pinst,
551 struct notifier_block *nblock)
552{
553 return blocking_notifier_chain_unregister(
554 &pinst->cpumask_change_notifier,
555 nblock);
556}
557EXPORT_SYMBOL(padata_unregister_cpumask_notifier);
Steffen Klassert16295be2010-01-06 19:47:10 +1100558
Steffen Klassert6751fb32010-04-29 14:42:30 +0200559
Steffen Klassert33e54452010-07-07 15:31:26 +0200560/* If cpumask contains no active cpu, we mark the instance as invalid. */
561static bool padata_validate_cpumask(struct padata_instance *pinst,
562 const struct cpumask *cpumask)
563{
Steffen Klassert13614e02012-03-28 08:43:21 +0200564 if (!cpumask_intersects(cpumask, cpu_online_mask)) {
Steffen Klassert33e54452010-07-07 15:31:26 +0200565 pinst->flags |= PADATA_INVALID;
566 return false;
Steffen Klassert16295be2010-01-06 19:47:10 +1100567 }
568
Steffen Klassert33e54452010-07-07 15:31:26 +0200569 pinst->flags &= ~PADATA_INVALID;
570 return true;
571}
572
Steffen Klassert65ff5772010-07-27 07:15:06 +0200573static int __padata_set_cpumasks(struct padata_instance *pinst,
574 cpumask_var_t pcpumask,
575 cpumask_var_t cbcpumask)
Steffen Klassert16295be2010-01-06 19:47:10 +1100576{
Steffen Klassert33e54452010-07-07 15:31:26 +0200577 int valid;
Steffen Klassert65ff5772010-07-27 07:15:06 +0200578 struct parallel_data *pd;
Steffen Klassert16295be2010-01-06 19:47:10 +1100579
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400580 valid = padata_validate_cpumask(pinst, pcpumask);
581 if (!valid) {
582 __padata_stop(pinst);
583 goto out_replace;
584 }
585
586 valid = padata_validate_cpumask(pinst, cbcpumask);
Steffen Klassertb89661d2010-07-20 08:49:20 +0200587 if (!valid)
Steffen Klassert33e54452010-07-07 15:31:26 +0200588 __padata_stop(pinst);
Steffen Klassert33e54452010-07-07 15:31:26 +0200589
Steffen Klassertb89661d2010-07-20 08:49:20 +0200590out_replace:
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400591 pd = padata_alloc_pd(pinst, pcpumask, cbcpumask);
Steffen Klassert65ff5772010-07-27 07:15:06 +0200592 if (!pd)
593 return -ENOMEM;
Steffen Klassert16295be2010-01-06 19:47:10 +1100594
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400595 cpumask_copy(pinst->cpumask.pcpu, pcpumask);
596 cpumask_copy(pinst->cpumask.cbcpu, cbcpumask);
Steffen Klassert16295be2010-01-06 19:47:10 +1100597
598 padata_replace(pinst, pd);
599
Steffen Klassert33e54452010-07-07 15:31:26 +0200600 if (valid)
601 __padata_start(pinst);
602
Steffen Klassert65ff5772010-07-27 07:15:06 +0200603 return 0;
604}
605
606/**
Steffen Klassert65ff5772010-07-27 07:15:06 +0200607 * padata_set_cpumask: Sets specified by @cpumask_type cpumask to the value
608 * equivalent to @cpumask.
609 *
610 * @pinst: padata instance
611 * @cpumask_type: PADATA_CPU_SERIAL or PADATA_CPU_PARALLEL corresponding
612 * to parallel and serial cpumasks respectively.
613 * @cpumask: the cpumask to use
614 */
615int padata_set_cpumask(struct padata_instance *pinst, int cpumask_type,
616 cpumask_var_t cpumask)
617{
618 struct cpumask *serial_mask, *parallel_mask;
619 int err = -EINVAL;
620
621 mutex_lock(&pinst->lock);
622 get_online_cpus();
623
624 switch (cpumask_type) {
625 case PADATA_CPU_PARALLEL:
626 serial_mask = pinst->cpumask.cbcpu;
627 parallel_mask = cpumask;
628 break;
629 case PADATA_CPU_SERIAL:
630 parallel_mask = pinst->cpumask.pcpu;
631 serial_mask = cpumask;
632 break;
633 default:
634 goto out;
635 }
636
637 err = __padata_set_cpumasks(pinst, parallel_mask, serial_mask);
638
Steffen Klassert16295be2010-01-06 19:47:10 +1100639out:
Steffen Klassert6751fb32010-04-29 14:42:30 +0200640 put_online_cpus();
Steffen Klassert16295be2010-01-06 19:47:10 +1100641 mutex_unlock(&pinst->lock);
642
643 return err;
644}
645EXPORT_SYMBOL(padata_set_cpumask);
646
Arnd Bergmann19d795b2016-05-19 17:09:59 -0700647/**
648 * padata_start - start the parallel processing
649 *
650 * @pinst: padata instance to start
651 */
652int padata_start(struct padata_instance *pinst)
653{
654 int err = 0;
655
656 mutex_lock(&pinst->lock);
657
658 if (pinst->flags & PADATA_INVALID)
659 err = -EINVAL;
660
661 __padata_start(pinst);
662
663 mutex_unlock(&pinst->lock);
664
665 return err;
666}
667EXPORT_SYMBOL(padata_start);
668
669/**
670 * padata_stop - stop the parallel processing
671 *
672 * @pinst: padata instance to stop
673 */
674void padata_stop(struct padata_instance *pinst)
675{
676 mutex_lock(&pinst->lock);
677 __padata_stop(pinst);
678 mutex_unlock(&pinst->lock);
679}
680EXPORT_SYMBOL(padata_stop);
681
682#ifdef CONFIG_HOTPLUG_CPU
683
Steffen Klassert16295be2010-01-06 19:47:10 +1100684static int __padata_add_cpu(struct padata_instance *pinst, int cpu)
685{
686 struct parallel_data *pd;
687
Steffen Klassert13614e02012-03-28 08:43:21 +0200688 if (cpumask_test_cpu(cpu, cpu_online_mask)) {
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400689 pd = padata_alloc_pd(pinst, pinst->cpumask.pcpu,
690 pinst->cpumask.cbcpu);
Steffen Klassert16295be2010-01-06 19:47:10 +1100691 if (!pd)
692 return -ENOMEM;
693
694 padata_replace(pinst, pd);
Steffen Klassert33e54452010-07-07 15:31:26 +0200695
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400696 if (padata_validate_cpumask(pinst, pinst->cpumask.pcpu) &&
697 padata_validate_cpumask(pinst, pinst->cpumask.cbcpu))
Steffen Klassert33e54452010-07-07 15:31:26 +0200698 __padata_start(pinst);
Steffen Klassert16295be2010-01-06 19:47:10 +1100699 }
700
701 return 0;
702}
703
Steffen Klassert16295be2010-01-06 19:47:10 +1100704static int __padata_remove_cpu(struct padata_instance *pinst, int cpu)
705{
Steffen Klassert33e54452010-07-07 15:31:26 +0200706 struct parallel_data *pd = NULL;
Steffen Klassert16295be2010-01-06 19:47:10 +1100707
708 if (cpumask_test_cpu(cpu, cpu_online_mask)) {
Steffen Klassert33e54452010-07-07 15:31:26 +0200709
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400710 if (!padata_validate_cpumask(pinst, pinst->cpumask.pcpu) ||
Steffen Klassertb89661d2010-07-20 08:49:20 +0200711 !padata_validate_cpumask(pinst, pinst->cpumask.cbcpu))
Steffen Klassert33e54452010-07-07 15:31:26 +0200712 __padata_stop(pinst);
Steffen Klassert33e54452010-07-07 15:31:26 +0200713
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400714 pd = padata_alloc_pd(pinst, pinst->cpumask.pcpu,
715 pinst->cpumask.cbcpu);
Steffen Klassert16295be2010-01-06 19:47:10 +1100716 if (!pd)
717 return -ENOMEM;
718
719 padata_replace(pinst, pd);
Steffen Klassert96120902012-03-28 08:44:07 +0200720
721 cpumask_clear_cpu(cpu, pd->cpumask.cbcpu);
722 cpumask_clear_cpu(cpu, pd->cpumask.pcpu);
Steffen Klassert16295be2010-01-06 19:47:10 +1100723 }
724
725 return 0;
726}
727
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400728 /**
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300729 * padata_remove_cpu - remove a cpu from the one or both(serial and parallel)
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400730 * padata cpumasks.
Steffen Klassert16295be2010-01-06 19:47:10 +1100731 *
732 * @pinst: padata instance
733 * @cpu: cpu to remove
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400734 * @mask: bitmask specifying from which cpumask @cpu should be removed
735 * The @mask may be any combination of the following flags:
736 * PADATA_CPU_SERIAL - serial cpumask
737 * PADATA_CPU_PARALLEL - parallel cpumask
Steffen Klassert16295be2010-01-06 19:47:10 +1100738 */
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400739int padata_remove_cpu(struct padata_instance *pinst, int cpu, int mask)
Steffen Klassert16295be2010-01-06 19:47:10 +1100740{
741 int err;
742
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400743 if (!(mask & (PADATA_CPU_SERIAL | PADATA_CPU_PARALLEL)))
744 return -EINVAL;
745
Steffen Klassert16295be2010-01-06 19:47:10 +1100746 mutex_lock(&pinst->lock);
747
Steffen Klassert6751fb32010-04-29 14:42:30 +0200748 get_online_cpus();
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400749 if (mask & PADATA_CPU_SERIAL)
750 cpumask_clear_cpu(cpu, pinst->cpumask.cbcpu);
751 if (mask & PADATA_CPU_PARALLEL)
752 cpumask_clear_cpu(cpu, pinst->cpumask.pcpu);
753
Steffen Klassert16295be2010-01-06 19:47:10 +1100754 err = __padata_remove_cpu(pinst, cpu);
Steffen Klassert6751fb32010-04-29 14:42:30 +0200755 put_online_cpus();
Steffen Klassert16295be2010-01-06 19:47:10 +1100756
757 mutex_unlock(&pinst->lock);
758
759 return err;
760}
761EXPORT_SYMBOL(padata_remove_cpu);
762
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400763static 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
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +0200769static int padata_cpu_online(unsigned int cpu, struct hlist_node *node)
Steffen Klassert16295be2010-01-06 19:47:10 +1100770{
Steffen Klassert16295be2010-01-06 19:47:10 +1100771 struct padata_instance *pinst;
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +0200772 int ret;
Steffen Klassert16295be2010-01-06 19:47:10 +1100773
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +0200774 pinst = hlist_entry_safe(node, struct padata_instance, node);
775 if (!pinst_has_cpu(pinst, cpu))
776 return 0;
Steffen Klassert16295be2010-01-06 19:47:10 +1100777
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +0200778 mutex_lock(&pinst->lock);
779 ret = __padata_add_cpu(pinst, cpu);
780 mutex_unlock(&pinst->lock);
781 return ret;
Steffen Klassert16295be2010-01-06 19:47:10 +1100782}
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +0200783
784static int padata_cpu_prep_down(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, 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
799static enum cpuhp_state hp_online;
Steffen Klasserte2cb2f12010-04-29 14:40:10 +0200800#endif
Steffen Klassert16295be2010-01-06 19:47:10 +1100801
Dan Kruchinin5e017dc2010-07-14 14:33:08 +0400802static void __padata_free(struct padata_instance *pinst)
803{
804#ifdef CONFIG_HOTPLUG_CPU
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +0200805 cpuhp_state_remove_instance_nocalls(hp_online, &pinst->node);
Dan Kruchinin5e017dc2010-07-14 14:33:08 +0400806#endif
807
808 padata_stop(pinst);
809 padata_free_pd(pinst->pd);
810 free_cpumask_var(pinst->cpumask.pcpu);
811 free_cpumask_var(pinst->cpumask.cbcpu);
812 kfree(pinst);
813}
814
815#define kobj2pinst(_kobj) \
816 container_of(_kobj, struct padata_instance, kobj)
817#define attr2pentry(_attr) \
818 container_of(_attr, struct padata_sysfs_entry, attr)
819
820static void padata_sysfs_release(struct kobject *kobj)
821{
822 struct padata_instance *pinst = kobj2pinst(kobj);
823 __padata_free(pinst);
824}
825
826struct padata_sysfs_entry {
827 struct attribute attr;
828 ssize_t (*show)(struct padata_instance *, struct attribute *, char *);
829 ssize_t (*store)(struct padata_instance *, struct attribute *,
830 const char *, size_t);
831};
832
833static ssize_t show_cpumask(struct padata_instance *pinst,
834 struct attribute *attr, char *buf)
835{
836 struct cpumask *cpumask;
837 ssize_t len;
838
839 mutex_lock(&pinst->lock);
840 if (!strcmp(attr->name, "serial_cpumask"))
841 cpumask = pinst->cpumask.cbcpu;
842 else
843 cpumask = pinst->cpumask.pcpu;
844
Tejun Heo4497da62015-02-13 14:38:05 -0800845 len = snprintf(buf, PAGE_SIZE, "%*pb\n",
846 nr_cpu_ids, cpumask_bits(cpumask));
Dan Kruchinin5e017dc2010-07-14 14:33:08 +0400847 mutex_unlock(&pinst->lock);
Tejun Heo4497da62015-02-13 14:38:05 -0800848 return len < PAGE_SIZE ? len : -EINVAL;
Dan Kruchinin5e017dc2010-07-14 14:33:08 +0400849}
850
851static ssize_t store_cpumask(struct padata_instance *pinst,
852 struct attribute *attr,
853 const char *buf, size_t count)
854{
855 cpumask_var_t new_cpumask;
856 ssize_t ret;
857 int mask_type;
858
859 if (!alloc_cpumask_var(&new_cpumask, GFP_KERNEL))
860 return -ENOMEM;
861
862 ret = bitmap_parse(buf, count, cpumask_bits(new_cpumask),
863 nr_cpumask_bits);
864 if (ret < 0)
865 goto out;
866
867 mask_type = !strcmp(attr->name, "serial_cpumask") ?
868 PADATA_CPU_SERIAL : PADATA_CPU_PARALLEL;
869 ret = padata_set_cpumask(pinst, mask_type, new_cpumask);
870 if (!ret)
871 ret = count;
872
873out:
874 free_cpumask_var(new_cpumask);
875 return ret;
876}
877
878#define PADATA_ATTR_RW(_name, _show_name, _store_name) \
879 static struct padata_sysfs_entry _name##_attr = \
880 __ATTR(_name, 0644, _show_name, _store_name)
881#define PADATA_ATTR_RO(_name, _show_name) \
882 static struct padata_sysfs_entry _name##_attr = \
883 __ATTR(_name, 0400, _show_name, NULL)
884
885PADATA_ATTR_RW(serial_cpumask, show_cpumask, store_cpumask);
886PADATA_ATTR_RW(parallel_cpumask, show_cpumask, store_cpumask);
887
888/*
889 * Padata sysfs provides the following objects:
890 * serial_cpumask [RW] - cpumask for serial workers
891 * parallel_cpumask [RW] - cpumask for parallel workers
Steffen Klassert16295be2010-01-06 19:47:10 +1100892 */
Dan Kruchinin5e017dc2010-07-14 14:33:08 +0400893static struct attribute *padata_default_attrs[] = {
894 &serial_cpumask_attr.attr,
895 &parallel_cpumask_attr.attr,
896 NULL,
897};
898
899static ssize_t padata_sysfs_show(struct kobject *kobj,
900 struct attribute *attr, char *buf)
Steffen Klassert16295be2010-01-06 19:47:10 +1100901{
Steffen Klassert16295be2010-01-06 19:47:10 +1100902 struct padata_instance *pinst;
Dan Kruchinin5e017dc2010-07-14 14:33:08 +0400903 struct padata_sysfs_entry *pentry;
904 ssize_t ret = -EIO;
905
906 pinst = kobj2pinst(kobj);
907 pentry = attr2pentry(attr);
908 if (pentry->show)
909 ret = pentry->show(pinst, attr, buf);
910
911 return ret;
912}
913
914static ssize_t padata_sysfs_store(struct kobject *kobj, struct attribute *attr,
915 const char *buf, size_t count)
916{
917 struct padata_instance *pinst;
918 struct padata_sysfs_entry *pentry;
919 ssize_t ret = -EIO;
920
921 pinst = kobj2pinst(kobj);
922 pentry = attr2pentry(attr);
923 if (pentry->show)
924 ret = pentry->store(pinst, attr, buf, count);
925
926 return ret;
927}
928
929static const struct sysfs_ops padata_sysfs_ops = {
930 .show = padata_sysfs_show,
931 .store = padata_sysfs_store,
932};
933
934static struct kobj_type padata_attr_type = {
935 .sysfs_ops = &padata_sysfs_ops,
936 .default_attrs = padata_default_attrs,
937 .release = padata_sysfs_release,
938};
939
Steffen Klassert16295be2010-01-06 19:47:10 +1100940/**
Steffen Klasserte6cc1172010-07-27 07:14:28 +0200941 * padata_alloc_possible - Allocate and initialize padata instance.
942 * Use the cpu_possible_mask for serial and
943 * parallel workers.
Steffen Klassert16295be2010-01-06 19:47:10 +1100944 *
Steffen Klassert16295be2010-01-06 19:47:10 +1100945 * @wq: workqueue to use for the allocated padata instance
946 */
Steffen Klasserte6cc1172010-07-27 07:14:28 +0200947struct padata_instance *padata_alloc_possible(struct workqueue_struct *wq)
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400948{
Steffen Klasserte6cc1172010-07-27 07:14:28 +0200949 return padata_alloc(wq, cpu_possible_mask, cpu_possible_mask);
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400950}
Steffen Klasserte6cc1172010-07-27 07:14:28 +0200951EXPORT_SYMBOL(padata_alloc_possible);
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400952
953/**
Steffen Klasserte6cc1172010-07-27 07:14:28 +0200954 * padata_alloc - allocate and initialize a padata instance and specify
955 * cpumasks for serial and parallel workers.
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400956 *
957 * @wq: workqueue to use for the allocated padata instance
958 * @pcpumask: cpumask that will be used for padata parallelization
959 * @cbcpumask: cpumask that will be used for padata serialization
960 */
Steffen Klasserte6cc1172010-07-27 07:14:28 +0200961struct padata_instance *padata_alloc(struct workqueue_struct *wq,
962 const struct cpumask *pcpumask,
963 const struct cpumask *cbcpumask)
Steffen Klassert16295be2010-01-06 19:47:10 +1100964{
965 struct padata_instance *pinst;
Steffen Klassert33e54452010-07-07 15:31:26 +0200966 struct parallel_data *pd = NULL;
Steffen Klassert16295be2010-01-06 19:47:10 +1100967
968 pinst = kzalloc(sizeof(struct padata_instance), GFP_KERNEL);
969 if (!pinst)
970 goto err;
971
Steffen Klassert6751fb32010-04-29 14:42:30 +0200972 get_online_cpus();
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400973 if (!alloc_cpumask_var(&pinst->cpumask.pcpu, GFP_KERNEL))
Steffen Klassert16295be2010-01-06 19:47:10 +1100974 goto err_free_inst;
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400975 if (!alloc_cpumask_var(&pinst->cpumask.cbcpu, GFP_KERNEL)) {
976 free_cpumask_var(pinst->cpumask.pcpu);
977 goto err_free_inst;
Steffen Klassert33e54452010-07-07 15:31:26 +0200978 }
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400979 if (!padata_validate_cpumask(pinst, pcpumask) ||
980 !padata_validate_cpumask(pinst, cbcpumask))
981 goto err_free_masks;
Steffen Klassert16295be2010-01-06 19:47:10 +1100982
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400983 pd = padata_alloc_pd(pinst, pcpumask, cbcpumask);
984 if (!pd)
985 goto err_free_masks;
Steffen Klassert74781382010-03-04 13:30:22 +0800986
Steffen Klassert16295be2010-01-06 19:47:10 +1100987 rcu_assign_pointer(pinst->pd, pd);
988
989 pinst->wq = wq;
990
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400991 cpumask_copy(pinst->cpumask.pcpu, pcpumask);
992 cpumask_copy(pinst->cpumask.cbcpu, cbcpumask);
Steffen Klassert16295be2010-01-06 19:47:10 +1100993
994 pinst->flags = 0;
995
Steffen Klassert6751fb32010-04-29 14:42:30 +0200996 put_online_cpus();
997
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400998 BLOCKING_INIT_NOTIFIER_HEAD(&pinst->cpumask_change_notifier);
Dan Kruchinin5e017dc2010-07-14 14:33:08 +0400999 kobject_init(&pinst->kobj, &padata_attr_type);
Steffen Klassert16295be2010-01-06 19:47:10 +11001000 mutex_init(&pinst->lock);
1001
Richard Weinbergerb8b4a412013-08-23 13:12:33 +02001002#ifdef CONFIG_HOTPLUG_CPU
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +02001003 cpuhp_state_add_instance_nocalls(hp_online, &pinst->node);
Richard Weinbergerb8b4a412013-08-23 13:12:33 +02001004#endif
Steffen Klassert16295be2010-01-06 19:47:10 +11001005 return pinst;
1006
Dan Kruchinine15bacb2010-07-14 14:31:57 +04001007err_free_masks:
1008 free_cpumask_var(pinst->cpumask.pcpu);
1009 free_cpumask_var(pinst->cpumask.cbcpu);
Steffen Klassert16295be2010-01-06 19:47:10 +11001010err_free_inst:
1011 kfree(pinst);
Steffen Klassert6751fb32010-04-29 14:42:30 +02001012 put_online_cpus();
Steffen Klassert16295be2010-01-06 19:47:10 +11001013err:
1014 return NULL;
1015}
Steffen Klassert16295be2010-01-06 19:47:10 +11001016
Steffen Klassert0198ffd2010-05-19 13:44:27 +10001017/**
Steffen Klassert16295be2010-01-06 19:47:10 +11001018 * padata_free - free a padata instance
1019 *
Steffen Klassert0198ffd2010-05-19 13:44:27 +10001020 * @padata_inst: padata instance to free
Steffen Klassert16295be2010-01-06 19:47:10 +11001021 */
1022void padata_free(struct padata_instance *pinst)
1023{
Dan Kruchinin5e017dc2010-07-14 14:33:08 +04001024 kobject_put(&pinst->kobj);
Steffen Klassert16295be2010-01-06 19:47:10 +11001025}
1026EXPORT_SYMBOL(padata_free);
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +02001027
1028#ifdef CONFIG_HOTPLUG_CPU
1029
1030static __init int padata_driver_init(void)
1031{
1032 int ret;
1033
1034 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "padata:online",
1035 padata_cpu_online,
1036 padata_cpu_prep_down);
1037 if (ret < 0)
1038 return ret;
1039 hp_online = ret;
1040 return 0;
1041}
1042module_init(padata_driver_init);
1043
1044static __exit void padata_driver_exit(void)
1045{
1046 cpuhp_remove_multi_state(hp_online);
1047}
1048module_exit(padata_driver_exit);
1049#endif