blob: fbafca18597f4ff5f90d0c0dcd39c8c0c6f261db [file] [log] [blame]
Cheah Kok Cheong08b21fb2017-12-21 19:35:30 +08001// SPDX-License-Identifier: GPL-2.0
Steffen Klassert16295be2010-01-06 19:47:10 +11002/*
3 * padata.c - generic interface to process data streams in parallel
4 *
Steffen Klassert107f8bd2012-03-28 08:42:34 +02005 * See Documentation/padata.txt for an api documentation.
6 *
Steffen Klassert16295be2010-01-06 19:47:10 +11007 * Copyright (C) 2008, 2009 secunet Security Networks AG
8 * Copyright (C) 2008, 2009 Steffen Klassert <steffen.klassert@secunet.com>
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms and conditions of the GNU General Public License,
12 * version 2, as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
Paul Gortmaker9984de12011-05-23 14:51:41 -040024#include <linux/export.h>
Steffen Klassert16295be2010-01-06 19:47:10 +110025#include <linux/cpumask.h>
26#include <linux/err.h>
27#include <linux/cpu.h>
28#include <linux/padata.h>
29#include <linux/mutex.h>
30#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Dan Kruchinin5e017dc2010-07-14 14:33:08 +040032#include <linux/sysfs.h>
Steffen Klassert16295be2010-01-06 19:47:10 +110033#include <linux/rcupdate.h>
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +020034#include <linux/module.h>
Steffen Klassert16295be2010-01-06 19:47:10 +110035
Steffen Klassert97e3d942010-04-29 14:37:32 +020036#define MAX_OBJ_NUM 1000
Steffen Klassert16295be2010-01-06 19:47:10 +110037
38static int padata_index_to_cpu(struct parallel_data *pd, int cpu_index)
39{
40 int cpu, target_cpu;
41
Dan Kruchinine15bacb2010-07-14 14:31:57 +040042 target_cpu = cpumask_first(pd->cpumask.pcpu);
Steffen Klassert16295be2010-01-06 19:47:10 +110043 for (cpu = 0; cpu < cpu_index; cpu++)
Dan Kruchinine15bacb2010-07-14 14:31:57 +040044 target_cpu = cpumask_next(target_cpu, pd->cpumask.pcpu);
Steffen Klassert16295be2010-01-06 19:47:10 +110045
46 return target_cpu;
47}
48
Steffen Klassert2dc9b5d2012-03-09 07:20:49 +010049static int padata_cpu_hash(struct parallel_data *pd)
Steffen Klassert16295be2010-01-06 19:47:10 +110050{
Mathias Krause0b6b0982013-10-25 12:14:15 +020051 unsigned int seq_nr;
Steffen Klassert16295be2010-01-06 19:47:10 +110052 int cpu_index;
Steffen Klassert16295be2010-01-06 19:47:10 +110053
54 /*
55 * Hash the sequence numbers to the cpus by taking
56 * seq_nr mod. number of cpus in use.
57 */
Steffen Klassert2dc9b5d2012-03-09 07:20:49 +010058
Mathias Krause0b6b0982013-10-25 12:14:15 +020059 seq_nr = atomic_inc_return(&pd->seq_nr);
60 cpu_index = seq_nr % cpumask_weight(pd->cpumask.pcpu);
Steffen Klassert16295be2010-01-06 19:47:10 +110061
62 return padata_index_to_cpu(pd, cpu_index);
63}
64
Dan Kruchinine15bacb2010-07-14 14:31:57 +040065static void padata_parallel_worker(struct work_struct *parallel_work)
Steffen Klassert16295be2010-01-06 19:47:10 +110066{
Dan Kruchinine15bacb2010-07-14 14:31:57 +040067 struct padata_parallel_queue *pqueue;
Steffen Klassert16295be2010-01-06 19:47:10 +110068 LIST_HEAD(local_list);
69
70 local_bh_disable();
Dan Kruchinine15bacb2010-07-14 14:31:57 +040071 pqueue = container_of(parallel_work,
72 struct padata_parallel_queue, work);
Steffen Klassert16295be2010-01-06 19:47:10 +110073
Dan Kruchinine15bacb2010-07-14 14:31:57 +040074 spin_lock(&pqueue->parallel.lock);
75 list_replace_init(&pqueue->parallel.list, &local_list);
76 spin_unlock(&pqueue->parallel.lock);
Steffen Klassert16295be2010-01-06 19:47:10 +110077
78 while (!list_empty(&local_list)) {
79 struct padata_priv *padata;
80
81 padata = list_entry(local_list.next,
82 struct padata_priv, list);
83
84 list_del_init(&padata->list);
85
86 padata->parallel(padata);
87 }
88
89 local_bh_enable();
90}
91
Steffen Klassert0198ffd2010-05-19 13:44:27 +100092/**
Steffen Klassert16295be2010-01-06 19:47:10 +110093 * padata_do_parallel - padata parallelization function
94 *
95 * @pinst: padata instance
96 * @padata: object to be parallelized
97 * @cb_cpu: cpu the serialization callback function will run on,
Dan Kruchinine15bacb2010-07-14 14:31:57 +040098 * must be in the serial cpumask of padata(i.e. cpumask.cbcpu).
Steffen Klassert16295be2010-01-06 19:47:10 +110099 *
100 * The parallelization callback function will run with BHs off.
101 * Note: Every object which is parallelized by padata_do_parallel
102 * must be seen by padata_do_serial.
103 */
104int padata_do_parallel(struct padata_instance *pinst,
105 struct padata_priv *padata, int cb_cpu)
106{
107 int target_cpu, err;
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400108 struct padata_parallel_queue *queue;
Steffen Klassert16295be2010-01-06 19:47:10 +1100109 struct parallel_data *pd;
110
111 rcu_read_lock_bh();
112
Mathias Krausec0e656b2013-11-28 19:20:05 +0100113 pd = rcu_dereference_bh(pinst->pd);
Steffen Klassert16295be2010-01-06 19:47:10 +1100114
Steffen Klassert83f619f2010-07-07 15:32:02 +0200115 err = -EINVAL;
Steffen Klassert74247132010-07-20 08:51:25 +0200116 if (!(pinst->flags & PADATA_INIT) || pinst->flags & PADATA_INVALID)
Steffen Klassert16295be2010-01-06 19:47:10 +1100117 goto out;
118
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400119 if (!cpumask_test_cpu(cb_cpu, pd->cpumask.cbcpu))
Steffen Klassert16295be2010-01-06 19:47:10 +1100120 goto out;
121
122 err = -EBUSY;
123 if ((pinst->flags & PADATA_RESET))
124 goto out;
125
126 if (atomic_read(&pd->refcnt) >= MAX_OBJ_NUM)
127 goto out;
128
Steffen Klassert83f619f2010-07-07 15:32:02 +0200129 err = 0;
Steffen Klassert16295be2010-01-06 19:47:10 +1100130 atomic_inc(&pd->refcnt);
131 padata->pd = pd;
132 padata->cb_cpu = cb_cpu;
133
Steffen Klassert2dc9b5d2012-03-09 07:20:49 +0100134 target_cpu = padata_cpu_hash(pd);
Mathias Krause350ef882017-09-08 20:57:11 +0200135 padata->cpu = target_cpu;
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400136 queue = per_cpu_ptr(pd->pqueue, target_cpu);
Steffen Klassert16295be2010-01-06 19:47:10 +1100137
138 spin_lock(&queue->parallel.lock);
139 list_add_tail(&padata->list, &queue->parallel.list);
140 spin_unlock(&queue->parallel.lock);
141
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400142 queue_work_on(target_cpu, pinst->wq, &queue->work);
Steffen Klassert16295be2010-01-06 19:47:10 +1100143
144out:
145 rcu_read_unlock_bh();
146
147 return err;
148}
149EXPORT_SYMBOL(padata_do_parallel);
150
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000151/*
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 *
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000159 * -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{
Shan Weif0fcf202012-12-06 17:16:23 +0800168 struct padata_parallel_queue *next_queue;
Steffen Klassert16295be2010-01-06 19:47:10 +1100169 struct padata_priv *padata;
170 struct padata_list *reorder;
Herbert Xu6fc4dbc2019-07-18 23:01:46 +0800171 int cpu = pd->cpu;
Steffen Klassert16295be2010-01-06 19:47:10 +1100172
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400173 next_queue = per_cpu_ptr(pd->pqueue, cpu);
Steffen Klassert16295be2010-01-06 19:47:10 +1100174 reorder = &next_queue->reorder;
175
Jason A. Donenfeldde5540d2017-03-23 12:24:43 +0100176 spin_lock(&reorder->lock);
Steffen Klassert16295be2010-01-06 19:47:10 +1100177 if (!list_empty(&reorder->list)) {
178 padata = list_entry(reorder->list.next,
179 struct padata_priv, list);
180
Steffen Klassert16295be2010-01-06 19:47:10 +1100181 list_del_init(&padata->list);
182 atomic_dec(&pd->reorder_objects);
Steffen Klassert16295be2010-01-06 19:47:10 +1100183
Herbert Xu6fc4dbc2019-07-18 23:01:46 +0800184 pd->cpu = cpumask_next_wrap(cpu, pd->cpumask.pcpu, -1,
185 false);
Steffen Klassert16295be2010-01-06 19:47:10 +1100186
Jason A. Donenfeldde5540d2017-03-23 12:24:43 +0100187 spin_unlock(&reorder->lock);
Steffen Klassert16295be2010-01-06 19:47:10 +1100188 goto out;
189 }
Jason A. Donenfeldde5540d2017-03-23 12:24:43 +0100190 spin_unlock(&reorder->lock);
Steffen Klassert16295be2010-01-06 19:47:10 +1100191
Shan Weif0fcf202012-12-06 17:16:23 +0800192 if (__this_cpu_read(pd->pqueue->cpu_index) == next_queue->cpu_index) {
Steffen Klassert16295be2010-01-06 19:47:10 +1100193 padata = ERR_PTR(-ENODATA);
194 goto out;
195 }
196
197 padata = ERR_PTR(-EINPROGRESS);
198out:
199 return padata;
200}
201
202static void padata_reorder(struct parallel_data *pd)
203{
Steffen Klassert30478172012-03-09 07:20:12 +0100204 int cb_cpu;
Steffen Klassert16295be2010-01-06 19:47:10 +1100205 struct padata_priv *padata;
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400206 struct padata_serial_queue *squeue;
Steffen Klassert16295be2010-01-06 19:47:10 +1100207 struct padata_instance *pinst = pd->pinst;
Herbert Xu6fc4dbc2019-07-18 23:01:46 +0800208 struct padata_parallel_queue *next_queue;
Steffen Klassert16295be2010-01-06 19:47:10 +1100209
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000210 /*
211 * We need to ensure that only one cpu can work on dequeueing of
212 * the reorder queue the time. Calculating in which percpu reorder
213 * queue the next object will arrive takes some time. A spinlock
214 * would be highly contended. Also it is not clear in which order
215 * the objects arrive to the reorder queues. So a cpu could wait to
216 * get the lock just to notice that there is nothing to do at the
217 * moment. Therefore we use a trylock and let the holder of the lock
218 * care for all the objects enqueued during the holdtime of the lock.
219 */
Steffen Klassert16295be2010-01-06 19:47:10 +1100220 if (!spin_trylock_bh(&pd->lock))
Steffen Klassertd46a5ac2010-05-19 13:43:14 +1000221 return;
Steffen Klassert16295be2010-01-06 19:47:10 +1100222
223 while (1) {
224 padata = padata_get_next(pd);
225
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000226 /*
Jason A. Donenfeld69b34842017-04-12 10:40:19 +0200227 * If the next object that needs serialization is parallel
228 * processed by another cpu and is still on it's way to the
229 * cpu's reorder queue, nothing to do for now.
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000230 */
Jason A. Donenfeld69b34842017-04-12 10:40:19 +0200231 if (PTR_ERR(padata) == -EINPROGRESS)
Steffen Klassert16295be2010-01-06 19:47:10 +1100232 break;
233
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000234 /*
235 * This cpu has to do the parallel processing of the next
236 * object. It's waiting in the cpu's parallelization queue,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300237 * so exit immediately.
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000238 */
Steffen Klassert16295be2010-01-06 19:47:10 +1100239 if (PTR_ERR(padata) == -ENODATA) {
240 spin_unlock_bh(&pd->lock);
Steffen Klassertd46a5ac2010-05-19 13:43:14 +1000241 return;
Steffen Klassert16295be2010-01-06 19:47:10 +1100242 }
243
Steffen Klassert30478172012-03-09 07:20:12 +0100244 cb_cpu = padata->cb_cpu;
245 squeue = per_cpu_ptr(pd->squeue, cb_cpu);
Steffen Klassert16295be2010-01-06 19:47:10 +1100246
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400247 spin_lock(&squeue->serial.lock);
248 list_add_tail(&padata->list, &squeue->serial.list);
249 spin_unlock(&squeue->serial.lock);
Steffen Klassert16295be2010-01-06 19:47:10 +1100250
Steffen Klassert30478172012-03-09 07:20:12 +0100251 queue_work_on(cb_cpu, pinst->wq, &squeue->work);
Steffen Klassert16295be2010-01-06 19:47:10 +1100252 }
253
254 spin_unlock_bh(&pd->lock);
255
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000256 /*
257 * The next object that needs serialization might have arrived to
Herbert Xu6fc4dbc2019-07-18 23:01:46 +0800258 * the reorder queues in the meantime.
Daniel Jordancf144f82019-07-16 12:32:53 -0400259 *
Herbert Xu6fc4dbc2019-07-18 23:01:46 +0800260 * Ensure reorder queue is read after pd->lock is dropped so we see
261 * new objects from another task in padata_do_serial. Pairs with
Daniel Jordancf144f82019-07-16 12:32:53 -0400262 * smp_mb__after_atomic in padata_do_serial.
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000263 */
Daniel Jordancf144f82019-07-16 12:32:53 -0400264 smp_mb();
Steffen Klassert16295be2010-01-06 19:47:10 +1100265
Herbert Xu6fc4dbc2019-07-18 23:01:46 +0800266 next_queue = per_cpu_ptr(pd->pqueue, pd->cpu);
267 if (!list_empty(&next_queue->reorder.list))
268 queue_work(pinst->wq, &pd->reorder_work);
Steffen Klassert16295be2010-01-06 19:47:10 +1100269}
270
Mathias Krausecf5868c2017-09-08 20:57:10 +0200271static void invoke_padata_reorder(struct work_struct *work)
272{
Mathias Krausecf5868c2017-09-08 20:57:10 +0200273 struct parallel_data *pd;
274
275 local_bh_disable();
Herbert Xu6fc4dbc2019-07-18 23:01:46 +0800276 pd = container_of(work, struct parallel_data, reorder_work);
Mathias Krausecf5868c2017-09-08 20:57:10 +0200277 padata_reorder(pd);
278 local_bh_enable();
279}
280
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400281static void padata_serial_worker(struct work_struct *serial_work)
Steffen Klassert16295be2010-01-06 19:47:10 +1100282{
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400283 struct padata_serial_queue *squeue;
Steffen Klassert16295be2010-01-06 19:47:10 +1100284 struct parallel_data *pd;
285 LIST_HEAD(local_list);
286
287 local_bh_disable();
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400288 squeue = container_of(serial_work, struct padata_serial_queue, work);
289 pd = squeue->pd;
Steffen Klassert16295be2010-01-06 19:47:10 +1100290
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400291 spin_lock(&squeue->serial.lock);
292 list_replace_init(&squeue->serial.list, &local_list);
293 spin_unlock(&squeue->serial.lock);
Steffen Klassert16295be2010-01-06 19:47:10 +1100294
295 while (!list_empty(&local_list)) {
296 struct padata_priv *padata;
297
298 padata = list_entry(local_list.next,
299 struct padata_priv, list);
300
301 list_del_init(&padata->list);
302
303 padata->serial(padata);
304 atomic_dec(&pd->refcnt);
305 }
306 local_bh_enable();
307}
308
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000309/**
Steffen Klassert16295be2010-01-06 19:47:10 +1100310 * padata_do_serial - padata serialization function
311 *
312 * @padata: object to be serialized.
313 *
314 * padata_do_serial must be called for every parallelized object.
315 * The serialization callback function will run with BHs off.
316 */
317void padata_do_serial(struct padata_priv *padata)
318{
319 int cpu;
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400320 struct padata_parallel_queue *pqueue;
Steffen Klassert16295be2010-01-06 19:47:10 +1100321 struct parallel_data *pd;
Mathias Krause350ef882017-09-08 20:57:11 +0200322 int reorder_via_wq = 0;
Steffen Klassert16295be2010-01-06 19:47:10 +1100323
324 pd = padata->pd;
325
326 cpu = get_cpu();
Mathias Krause350ef882017-09-08 20:57:11 +0200327
Herbert Xu6fc4dbc2019-07-18 23:01:46 +0800328 /* We need to enqueue the padata object into the correct
329 * per-cpu queue.
Mathias Krause350ef882017-09-08 20:57:11 +0200330 */
331 if (cpu != padata->cpu) {
332 reorder_via_wq = 1;
333 cpu = padata->cpu;
334 }
335
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400336 pqueue = per_cpu_ptr(pd->pqueue, cpu);
Steffen Klassert16295be2010-01-06 19:47:10 +1100337
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400338 spin_lock(&pqueue->reorder.lock);
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400339 list_add_tail(&padata->list, &pqueue->reorder.list);
Herbert Xu6fc4dbc2019-07-18 23:01:46 +0800340 atomic_inc(&pd->reorder_objects);
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400341 spin_unlock(&pqueue->reorder.lock);
Steffen Klassert16295be2010-01-06 19:47:10 +1100342
Daniel Jordancf144f82019-07-16 12:32:53 -0400343 /*
Herbert Xu6fc4dbc2019-07-18 23:01:46 +0800344 * Ensure the addition to the reorder list is ordered correctly
Daniel Jordancf144f82019-07-16 12:32:53 -0400345 * with the trylock of pd->lock in padata_reorder. Pairs with smp_mb
346 * in padata_reorder.
347 */
348 smp_mb__after_atomic();
349
Steffen Klassert16295be2010-01-06 19:47:10 +1100350 put_cpu();
351
Herbert Xu6fc4dbc2019-07-18 23:01:46 +0800352 padata_reorder(pd);
Steffen Klassert16295be2010-01-06 19:47:10 +1100353}
354EXPORT_SYMBOL(padata_do_serial);
355
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400356static int padata_setup_cpumasks(struct parallel_data *pd,
357 const struct cpumask *pcpumask,
358 const struct cpumask *cbcpumask)
Steffen Klassert16295be2010-01-06 19:47:10 +1100359{
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400360 if (!alloc_cpumask_var(&pd->cpumask.pcpu, GFP_KERNEL))
361 return -ENOMEM;
362
Steffen Klassert13614e02012-03-28 08:43:21 +0200363 cpumask_and(pd->cpumask.pcpu, pcpumask, cpu_online_mask);
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400364 if (!alloc_cpumask_var(&pd->cpumask.cbcpu, GFP_KERNEL)) {
Jason A. Donenfeld07a77922017-04-07 02:33:30 +0200365 free_cpumask_var(pd->cpumask.pcpu);
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400366 return -ENOMEM;
367 }
368
Steffen Klassert13614e02012-03-28 08:43:21 +0200369 cpumask_and(pd->cpumask.cbcpu, cbcpumask, cpu_online_mask);
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400370 return 0;
371}
372
373static void __padata_list_init(struct padata_list *pd_list)
374{
375 INIT_LIST_HEAD(&pd_list->list);
376 spin_lock_init(&pd_list->lock);
377}
378
379/* Initialize all percpu queues used by serial workers */
380static void padata_init_squeues(struct parallel_data *pd)
381{
382 int cpu;
383 struct padata_serial_queue *squeue;
384
385 for_each_cpu(cpu, pd->cpumask.cbcpu) {
386 squeue = per_cpu_ptr(pd->squeue, cpu);
387 squeue->pd = pd;
388 __padata_list_init(&squeue->serial);
389 INIT_WORK(&squeue->work, padata_serial_worker);
390 }
391}
392
393/* Initialize all percpu queues used by parallel workers */
394static void padata_init_pqueues(struct parallel_data *pd)
395{
Steffen Klassert2dc9b5d2012-03-09 07:20:49 +0100396 int cpu_index, cpu;
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400397 struct padata_parallel_queue *pqueue;
Steffen Klassert16295be2010-01-06 19:47:10 +1100398
399 cpu_index = 0;
Mathias Krause1bd845b2017-09-08 20:57:09 +0200400 for_each_possible_cpu(cpu) {
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400401 pqueue = per_cpu_ptr(pd->pqueue, cpu);
Mathias Krause1bd845b2017-09-08 20:57:09 +0200402
403 if (!cpumask_test_cpu(cpu, pd->cpumask.pcpu)) {
404 pqueue->cpu_index = -1;
405 continue;
406 }
407
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400408 pqueue->cpu_index = cpu_index;
Steffen Klassertfad3a902010-07-20 08:48:34 +0200409 cpu_index++;
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400410
411 __padata_list_init(&pqueue->reorder);
412 __padata_list_init(&pqueue->parallel);
413 INIT_WORK(&pqueue->work, padata_parallel_worker);
414 atomic_set(&pqueue->num_obj, 0);
415 }
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400416}
417
418/* Allocate and initialize the internal cpumask dependend resources. */
419static struct parallel_data *padata_alloc_pd(struct padata_instance *pinst,
420 const struct cpumask *pcpumask,
421 const struct cpumask *cbcpumask)
422{
423 struct parallel_data *pd;
Steffen Klassert16295be2010-01-06 19:47:10 +1100424
425 pd = kzalloc(sizeof(struct parallel_data), GFP_KERNEL);
426 if (!pd)
427 goto err;
428
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400429 pd->pqueue = alloc_percpu(struct padata_parallel_queue);
430 if (!pd->pqueue)
Steffen Klassert16295be2010-01-06 19:47:10 +1100431 goto err_free_pd;
432
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400433 pd->squeue = alloc_percpu(struct padata_serial_queue);
434 if (!pd->squeue)
435 goto err_free_pqueue;
436 if (padata_setup_cpumasks(pd, pcpumask, cbcpumask) < 0)
437 goto err_free_squeue;
Steffen Klassert16295be2010-01-06 19:47:10 +1100438
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400439 padata_init_pqueues(pd);
440 padata_init_squeues(pd);
Mathias Krause0b6b0982013-10-25 12:14:15 +0200441 atomic_set(&pd->seq_nr, -1);
Steffen Klassert16295be2010-01-06 19:47:10 +1100442 atomic_set(&pd->reorder_objects, 0);
443 atomic_set(&pd->refcnt, 0);
444 pd->pinst = pinst;
445 spin_lock_init(&pd->lock);
Herbert Xu6fc4dbc2019-07-18 23:01:46 +0800446 pd->cpu = cpumask_first(pcpumask);
447 INIT_WORK(&pd->reorder_work, invoke_padata_reorder);
Steffen Klassert16295be2010-01-06 19:47:10 +1100448
449 return pd;
450
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400451err_free_squeue:
452 free_percpu(pd->squeue);
453err_free_pqueue:
454 free_percpu(pd->pqueue);
Steffen Klassert16295be2010-01-06 19:47:10 +1100455err_free_pd:
456 kfree(pd);
457err:
458 return NULL;
459}
460
461static void padata_free_pd(struct parallel_data *pd)
462{
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400463 free_cpumask_var(pd->cpumask.pcpu);
464 free_cpumask_var(pd->cpumask.cbcpu);
465 free_percpu(pd->pqueue);
466 free_percpu(pd->squeue);
Steffen Klassert16295be2010-01-06 19:47:10 +1100467 kfree(pd);
468}
469
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000470/* Flush all objects out of the padata queues. */
Steffen Klassert2b73b072010-05-19 13:43:46 +1000471static void padata_flush_queues(struct parallel_data *pd)
472{
473 int cpu;
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400474 struct padata_parallel_queue *pqueue;
475 struct padata_serial_queue *squeue;
Steffen Klassert2b73b072010-05-19 13:43:46 +1000476
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400477 for_each_cpu(cpu, pd->cpumask.pcpu) {
478 pqueue = per_cpu_ptr(pd->pqueue, cpu);
479 flush_work(&pqueue->work);
Steffen Klassert2b73b072010-05-19 13:43:46 +1000480 }
481
Steffen Klassert2b73b072010-05-19 13:43:46 +1000482 if (atomic_read(&pd->reorder_objects))
483 padata_reorder(pd);
484
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400485 for_each_cpu(cpu, pd->cpumask.cbcpu) {
486 squeue = per_cpu_ptr(pd->squeue, cpu);
487 flush_work(&squeue->work);
Steffen Klassert2b73b072010-05-19 13:43:46 +1000488 }
489
490 BUG_ON(atomic_read(&pd->refcnt) != 0);
491}
492
Steffen Klassert4c879172010-07-07 15:30:10 +0200493static void __padata_start(struct padata_instance *pinst)
494{
495 pinst->flags |= PADATA_INIT;
496}
497
Steffen Klassertee836552010-07-07 15:30:47 +0200498static void __padata_stop(struct padata_instance *pinst)
499{
500 if (!(pinst->flags & PADATA_INIT))
501 return;
502
503 pinst->flags &= ~PADATA_INIT;
504
505 synchronize_rcu();
506
507 get_online_cpus();
508 padata_flush_queues(pinst->pd);
509 put_online_cpus();
510}
511
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300512/* Replace the internal control structure with a new one. */
Steffen Klassert16295be2010-01-06 19:47:10 +1100513static void padata_replace(struct padata_instance *pinst,
514 struct parallel_data *pd_new)
515{
516 struct parallel_data *pd_old = pinst->pd;
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400517 int notification_mask = 0;
Steffen Klassert16295be2010-01-06 19:47:10 +1100518
519 pinst->flags |= PADATA_RESET;
520
521 rcu_assign_pointer(pinst->pd, pd_new);
522
523 synchronize_rcu();
524
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400525 if (!cpumask_equal(pd_old->cpumask.pcpu, pd_new->cpumask.pcpu))
526 notification_mask |= PADATA_CPU_PARALLEL;
527 if (!cpumask_equal(pd_old->cpumask.cbcpu, pd_new->cpumask.cbcpu))
528 notification_mask |= PADATA_CPU_SERIAL;
Steffen Klassert16295be2010-01-06 19:47:10 +1100529
Steffen Klassert16295be2010-01-06 19:47:10 +1100530 padata_flush_queues(pd_old);
531 padata_free_pd(pd_old);
532
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400533 if (notification_mask)
534 blocking_notifier_call_chain(&pinst->cpumask_change_notifier,
Steffen Klassertc6356962010-07-27 07:15:50 +0200535 notification_mask,
536 &pd_new->cpumask);
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400537
Steffen Klassert16295be2010-01-06 19:47:10 +1100538 pinst->flags &= ~PADATA_RESET;
539}
540
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000541/**
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400542 * padata_register_cpumask_notifier - Registers a notifier that will be called
543 * if either pcpu or cbcpu or both cpumasks change.
Steffen Klassert16295be2010-01-06 19:47:10 +1100544 *
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400545 * @pinst: A poineter to padata instance
546 * @nblock: A pointer to notifier block.
Steffen Klassert16295be2010-01-06 19:47:10 +1100547 */
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400548int padata_register_cpumask_notifier(struct padata_instance *pinst,
549 struct notifier_block *nblock)
Steffen Klassert16295be2010-01-06 19:47:10 +1100550{
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400551 return blocking_notifier_chain_register(&pinst->cpumask_change_notifier,
552 nblock);
553}
554EXPORT_SYMBOL(padata_register_cpumask_notifier);
Steffen Klassert16295be2010-01-06 19:47:10 +1100555
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400556/**
557 * padata_unregister_cpumask_notifier - Unregisters cpumask notifier
558 * registered earlier using padata_register_cpumask_notifier
559 *
560 * @pinst: A pointer to data instance.
561 * @nlock: A pointer to notifier block.
562 */
563int padata_unregister_cpumask_notifier(struct padata_instance *pinst,
564 struct notifier_block *nblock)
565{
566 return blocking_notifier_chain_unregister(
567 &pinst->cpumask_change_notifier,
568 nblock);
569}
570EXPORT_SYMBOL(padata_unregister_cpumask_notifier);
Steffen Klassert16295be2010-01-06 19:47:10 +1100571
Steffen Klassert6751fb32010-04-29 14:42:30 +0200572
Steffen Klassert33e54452010-07-07 15:31:26 +0200573/* If cpumask contains no active cpu, we mark the instance as invalid. */
574static bool padata_validate_cpumask(struct padata_instance *pinst,
575 const struct cpumask *cpumask)
576{
Steffen Klassert13614e02012-03-28 08:43:21 +0200577 if (!cpumask_intersects(cpumask, cpu_online_mask)) {
Steffen Klassert33e54452010-07-07 15:31:26 +0200578 pinst->flags |= PADATA_INVALID;
579 return false;
Steffen Klassert16295be2010-01-06 19:47:10 +1100580 }
581
Steffen Klassert33e54452010-07-07 15:31:26 +0200582 pinst->flags &= ~PADATA_INVALID;
583 return true;
584}
585
Steffen Klassert65ff5772010-07-27 07:15:06 +0200586static int __padata_set_cpumasks(struct padata_instance *pinst,
587 cpumask_var_t pcpumask,
588 cpumask_var_t cbcpumask)
Steffen Klassert16295be2010-01-06 19:47:10 +1100589{
Steffen Klassert33e54452010-07-07 15:31:26 +0200590 int valid;
Steffen Klassert65ff5772010-07-27 07:15:06 +0200591 struct parallel_data *pd;
Steffen Klassert16295be2010-01-06 19:47:10 +1100592
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400593 valid = padata_validate_cpumask(pinst, pcpumask);
594 if (!valid) {
595 __padata_stop(pinst);
596 goto out_replace;
597 }
598
599 valid = padata_validate_cpumask(pinst, cbcpumask);
Steffen Klassertb89661d2010-07-20 08:49:20 +0200600 if (!valid)
Steffen Klassert33e54452010-07-07 15:31:26 +0200601 __padata_stop(pinst);
Steffen Klassert33e54452010-07-07 15:31:26 +0200602
Steffen Klassertb89661d2010-07-20 08:49:20 +0200603out_replace:
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400604 pd = padata_alloc_pd(pinst, pcpumask, cbcpumask);
Steffen Klassert65ff5772010-07-27 07:15:06 +0200605 if (!pd)
606 return -ENOMEM;
Steffen Klassert16295be2010-01-06 19:47:10 +1100607
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400608 cpumask_copy(pinst->cpumask.pcpu, pcpumask);
609 cpumask_copy(pinst->cpumask.cbcpu, cbcpumask);
Steffen Klassert16295be2010-01-06 19:47:10 +1100610
611 padata_replace(pinst, pd);
612
Steffen Klassert33e54452010-07-07 15:31:26 +0200613 if (valid)
614 __padata_start(pinst);
615
Steffen Klassert65ff5772010-07-27 07:15:06 +0200616 return 0;
617}
618
619/**
Steffen Klassert65ff5772010-07-27 07:15:06 +0200620 * padata_set_cpumask: Sets specified by @cpumask_type cpumask to the value
621 * equivalent to @cpumask.
622 *
623 * @pinst: padata instance
624 * @cpumask_type: PADATA_CPU_SERIAL or PADATA_CPU_PARALLEL corresponding
625 * to parallel and serial cpumasks respectively.
626 * @cpumask: the cpumask to use
627 */
628int padata_set_cpumask(struct padata_instance *pinst, int cpumask_type,
629 cpumask_var_t cpumask)
630{
631 struct cpumask *serial_mask, *parallel_mask;
632 int err = -EINVAL;
633
634 mutex_lock(&pinst->lock);
635 get_online_cpus();
636
637 switch (cpumask_type) {
638 case PADATA_CPU_PARALLEL:
639 serial_mask = pinst->cpumask.cbcpu;
640 parallel_mask = cpumask;
641 break;
642 case PADATA_CPU_SERIAL:
643 parallel_mask = pinst->cpumask.pcpu;
644 serial_mask = cpumask;
645 break;
646 default:
647 goto out;
648 }
649
650 err = __padata_set_cpumasks(pinst, parallel_mask, serial_mask);
651
Steffen Klassert16295be2010-01-06 19:47:10 +1100652out:
Steffen Klassert6751fb32010-04-29 14:42:30 +0200653 put_online_cpus();
Steffen Klassert16295be2010-01-06 19:47:10 +1100654 mutex_unlock(&pinst->lock);
655
656 return err;
657}
658EXPORT_SYMBOL(padata_set_cpumask);
659
Arnd Bergmann19d795b2016-05-19 17:09:59 -0700660/**
661 * padata_start - start the parallel processing
662 *
663 * @pinst: padata instance to start
664 */
665int padata_start(struct padata_instance *pinst)
666{
667 int err = 0;
668
669 mutex_lock(&pinst->lock);
670
671 if (pinst->flags & PADATA_INVALID)
672 err = -EINVAL;
673
Colin Ian King8ddab422018-11-09 13:16:39 +0000674 __padata_start(pinst);
Arnd Bergmann19d795b2016-05-19 17:09:59 -0700675
676 mutex_unlock(&pinst->lock);
677
678 return err;
679}
680EXPORT_SYMBOL(padata_start);
681
682/**
683 * padata_stop - stop the parallel processing
684 *
685 * @pinst: padata instance to stop
686 */
687void padata_stop(struct padata_instance *pinst)
688{
689 mutex_lock(&pinst->lock);
690 __padata_stop(pinst);
691 mutex_unlock(&pinst->lock);
692}
693EXPORT_SYMBOL(padata_stop);
694
695#ifdef CONFIG_HOTPLUG_CPU
696
Steffen Klassert16295be2010-01-06 19:47:10 +1100697static int __padata_add_cpu(struct padata_instance *pinst, int cpu)
698{
699 struct parallel_data *pd;
700
Steffen Klassert13614e02012-03-28 08:43:21 +0200701 if (cpumask_test_cpu(cpu, cpu_online_mask)) {
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400702 pd = padata_alloc_pd(pinst, pinst->cpumask.pcpu,
703 pinst->cpumask.cbcpu);
Steffen Klassert16295be2010-01-06 19:47:10 +1100704 if (!pd)
705 return -ENOMEM;
706
707 padata_replace(pinst, pd);
Steffen Klassert33e54452010-07-07 15:31:26 +0200708
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400709 if (padata_validate_cpumask(pinst, pinst->cpumask.pcpu) &&
710 padata_validate_cpumask(pinst, pinst->cpumask.cbcpu))
Steffen Klassert33e54452010-07-07 15:31:26 +0200711 __padata_start(pinst);
Steffen Klassert16295be2010-01-06 19:47:10 +1100712 }
713
714 return 0;
715}
716
Steffen Klassert16295be2010-01-06 19:47:10 +1100717static int __padata_remove_cpu(struct padata_instance *pinst, int cpu)
718{
Steffen Klassert33e54452010-07-07 15:31:26 +0200719 struct parallel_data *pd = NULL;
Steffen Klassert16295be2010-01-06 19:47:10 +1100720
721 if (cpumask_test_cpu(cpu, cpu_online_mask)) {
Steffen Klassert33e54452010-07-07 15:31:26 +0200722
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400723 if (!padata_validate_cpumask(pinst, pinst->cpumask.pcpu) ||
Steffen Klassertb89661d2010-07-20 08:49:20 +0200724 !padata_validate_cpumask(pinst, pinst->cpumask.cbcpu))
Steffen Klassert33e54452010-07-07 15:31:26 +0200725 __padata_stop(pinst);
Steffen Klassert33e54452010-07-07 15:31:26 +0200726
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400727 pd = padata_alloc_pd(pinst, pinst->cpumask.pcpu,
728 pinst->cpumask.cbcpu);
Steffen Klassert16295be2010-01-06 19:47:10 +1100729 if (!pd)
730 return -ENOMEM;
731
732 padata_replace(pinst, pd);
Steffen Klassert96120902012-03-28 08:44:07 +0200733
734 cpumask_clear_cpu(cpu, pd->cpumask.cbcpu);
735 cpumask_clear_cpu(cpu, pd->cpumask.pcpu);
Steffen Klassert16295be2010-01-06 19:47:10 +1100736 }
737
738 return 0;
739}
740
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400741 /**
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300742 * padata_remove_cpu - remove a cpu from the one or both(serial and parallel)
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400743 * padata cpumasks.
Steffen Klassert16295be2010-01-06 19:47:10 +1100744 *
745 * @pinst: padata instance
746 * @cpu: cpu to remove
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400747 * @mask: bitmask specifying from which cpumask @cpu should be removed
748 * The @mask may be any combination of the following flags:
749 * PADATA_CPU_SERIAL - serial cpumask
750 * PADATA_CPU_PARALLEL - parallel cpumask
Steffen Klassert16295be2010-01-06 19:47:10 +1100751 */
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400752int padata_remove_cpu(struct padata_instance *pinst, int cpu, int mask)
Steffen Klassert16295be2010-01-06 19:47:10 +1100753{
754 int err;
755
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400756 if (!(mask & (PADATA_CPU_SERIAL | PADATA_CPU_PARALLEL)))
757 return -EINVAL;
758
Steffen Klassert16295be2010-01-06 19:47:10 +1100759 mutex_lock(&pinst->lock);
760
Steffen Klassert6751fb32010-04-29 14:42:30 +0200761 get_online_cpus();
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400762 if (mask & PADATA_CPU_SERIAL)
763 cpumask_clear_cpu(cpu, pinst->cpumask.cbcpu);
764 if (mask & PADATA_CPU_PARALLEL)
765 cpumask_clear_cpu(cpu, pinst->cpumask.pcpu);
766
Steffen Klassert16295be2010-01-06 19:47:10 +1100767 err = __padata_remove_cpu(pinst, cpu);
Steffen Klassert6751fb32010-04-29 14:42:30 +0200768 put_online_cpus();
Steffen Klassert16295be2010-01-06 19:47:10 +1100769
770 mutex_unlock(&pinst->lock);
771
772 return err;
773}
774EXPORT_SYMBOL(padata_remove_cpu);
775
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400776static inline int pinst_has_cpu(struct padata_instance *pinst, int cpu)
777{
778 return cpumask_test_cpu(cpu, pinst->cpumask.pcpu) ||
779 cpumask_test_cpu(cpu, pinst->cpumask.cbcpu);
780}
781
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +0200782static int padata_cpu_online(unsigned int cpu, struct hlist_node *node)
Steffen Klassert16295be2010-01-06 19:47:10 +1100783{
Steffen Klassert16295be2010-01-06 19:47:10 +1100784 struct padata_instance *pinst;
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +0200785 int ret;
Steffen Klassert16295be2010-01-06 19:47:10 +1100786
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +0200787 pinst = hlist_entry_safe(node, struct padata_instance, node);
788 if (!pinst_has_cpu(pinst, cpu))
789 return 0;
Steffen Klassert16295be2010-01-06 19:47:10 +1100790
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +0200791 mutex_lock(&pinst->lock);
792 ret = __padata_add_cpu(pinst, cpu);
793 mutex_unlock(&pinst->lock);
794 return ret;
Steffen Klassert16295be2010-01-06 19:47:10 +1100795}
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +0200796
797static int padata_cpu_prep_down(unsigned int cpu, struct hlist_node *node)
798{
799 struct padata_instance *pinst;
800 int ret;
801
802 pinst = hlist_entry_safe(node, struct padata_instance, node);
803 if (!pinst_has_cpu(pinst, cpu))
804 return 0;
805
806 mutex_lock(&pinst->lock);
807 ret = __padata_remove_cpu(pinst, cpu);
808 mutex_unlock(&pinst->lock);
809 return ret;
810}
811
812static enum cpuhp_state hp_online;
Steffen Klasserte2cb2f12010-04-29 14:40:10 +0200813#endif
Steffen Klassert16295be2010-01-06 19:47:10 +1100814
Dan Kruchinin5e017dc2010-07-14 14:33:08 +0400815static void __padata_free(struct padata_instance *pinst)
816{
817#ifdef CONFIG_HOTPLUG_CPU
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +0200818 cpuhp_state_remove_instance_nocalls(hp_online, &pinst->node);
Dan Kruchinin5e017dc2010-07-14 14:33:08 +0400819#endif
820
821 padata_stop(pinst);
822 padata_free_pd(pinst->pd);
823 free_cpumask_var(pinst->cpumask.pcpu);
824 free_cpumask_var(pinst->cpumask.cbcpu);
825 kfree(pinst);
826}
827
828#define kobj2pinst(_kobj) \
829 container_of(_kobj, struct padata_instance, kobj)
830#define attr2pentry(_attr) \
831 container_of(_attr, struct padata_sysfs_entry, attr)
832
833static void padata_sysfs_release(struct kobject *kobj)
834{
835 struct padata_instance *pinst = kobj2pinst(kobj);
836 __padata_free(pinst);
837}
838
839struct padata_sysfs_entry {
840 struct attribute attr;
841 ssize_t (*show)(struct padata_instance *, struct attribute *, char *);
842 ssize_t (*store)(struct padata_instance *, struct attribute *,
843 const char *, size_t);
844};
845
846static ssize_t show_cpumask(struct padata_instance *pinst,
847 struct attribute *attr, char *buf)
848{
849 struct cpumask *cpumask;
850 ssize_t len;
851
852 mutex_lock(&pinst->lock);
853 if (!strcmp(attr->name, "serial_cpumask"))
854 cpumask = pinst->cpumask.cbcpu;
855 else
856 cpumask = pinst->cpumask.pcpu;
857
Tejun Heo4497da62015-02-13 14:38:05 -0800858 len = snprintf(buf, PAGE_SIZE, "%*pb\n",
859 nr_cpu_ids, cpumask_bits(cpumask));
Dan Kruchinin5e017dc2010-07-14 14:33:08 +0400860 mutex_unlock(&pinst->lock);
Tejun Heo4497da62015-02-13 14:38:05 -0800861 return len < PAGE_SIZE ? len : -EINVAL;
Dan Kruchinin5e017dc2010-07-14 14:33:08 +0400862}
863
864static ssize_t store_cpumask(struct padata_instance *pinst,
865 struct attribute *attr,
866 const char *buf, size_t count)
867{
868 cpumask_var_t new_cpumask;
869 ssize_t ret;
870 int mask_type;
871
872 if (!alloc_cpumask_var(&new_cpumask, GFP_KERNEL))
873 return -ENOMEM;
874
875 ret = bitmap_parse(buf, count, cpumask_bits(new_cpumask),
876 nr_cpumask_bits);
877 if (ret < 0)
878 goto out;
879
880 mask_type = !strcmp(attr->name, "serial_cpumask") ?
881 PADATA_CPU_SERIAL : PADATA_CPU_PARALLEL;
882 ret = padata_set_cpumask(pinst, mask_type, new_cpumask);
883 if (!ret)
884 ret = count;
885
886out:
887 free_cpumask_var(new_cpumask);
888 return ret;
889}
890
891#define PADATA_ATTR_RW(_name, _show_name, _store_name) \
892 static struct padata_sysfs_entry _name##_attr = \
893 __ATTR(_name, 0644, _show_name, _store_name)
894#define PADATA_ATTR_RO(_name, _show_name) \
895 static struct padata_sysfs_entry _name##_attr = \
896 __ATTR(_name, 0400, _show_name, NULL)
897
898PADATA_ATTR_RW(serial_cpumask, show_cpumask, store_cpumask);
899PADATA_ATTR_RW(parallel_cpumask, show_cpumask, store_cpumask);
900
901/*
902 * Padata sysfs provides the following objects:
903 * serial_cpumask [RW] - cpumask for serial workers
904 * parallel_cpumask [RW] - cpumask for parallel workers
Steffen Klassert16295be2010-01-06 19:47:10 +1100905 */
Dan Kruchinin5e017dc2010-07-14 14:33:08 +0400906static struct attribute *padata_default_attrs[] = {
907 &serial_cpumask_attr.attr,
908 &parallel_cpumask_attr.attr,
909 NULL,
910};
Kimberly Brown2064fbc2019-04-01 22:51:47 -0400911ATTRIBUTE_GROUPS(padata_default);
Dan Kruchinin5e017dc2010-07-14 14:33:08 +0400912
913static ssize_t padata_sysfs_show(struct kobject *kobj,
914 struct attribute *attr, char *buf)
Steffen Klassert16295be2010-01-06 19:47:10 +1100915{
Steffen Klassert16295be2010-01-06 19:47:10 +1100916 struct padata_instance *pinst;
Dan Kruchinin5e017dc2010-07-14 14:33:08 +0400917 struct padata_sysfs_entry *pentry;
918 ssize_t ret = -EIO;
919
920 pinst = kobj2pinst(kobj);
921 pentry = attr2pentry(attr);
922 if (pentry->show)
923 ret = pentry->show(pinst, attr, buf);
924
925 return ret;
926}
927
928static ssize_t padata_sysfs_store(struct kobject *kobj, struct attribute *attr,
929 const char *buf, size_t count)
930{
931 struct padata_instance *pinst;
932 struct padata_sysfs_entry *pentry;
933 ssize_t ret = -EIO;
934
935 pinst = kobj2pinst(kobj);
936 pentry = attr2pentry(attr);
937 if (pentry->show)
938 ret = pentry->store(pinst, attr, buf, count);
939
940 return ret;
941}
942
943static const struct sysfs_ops padata_sysfs_ops = {
944 .show = padata_sysfs_show,
945 .store = padata_sysfs_store,
946};
947
948static struct kobj_type padata_attr_type = {
949 .sysfs_ops = &padata_sysfs_ops,
Kimberly Brown2064fbc2019-04-01 22:51:47 -0400950 .default_groups = padata_default_groups,
Dan Kruchinin5e017dc2010-07-14 14:33:08 +0400951 .release = padata_sysfs_release,
952};
953
Steffen Klassert16295be2010-01-06 19:47:10 +1100954/**
Steffen Klasserte6cc1172010-07-27 07:14:28 +0200955 * padata_alloc - allocate and initialize a padata instance and specify
956 * cpumasks for serial and parallel workers.
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400957 *
958 * @wq: workqueue to use for the allocated padata instance
959 * @pcpumask: cpumask that will be used for padata parallelization
960 * @cbcpumask: cpumask that will be used for padata serialization
Sebastian Andrzej Siewiorc5a81c82017-05-24 10:15:18 +0200961 *
962 * Must be called from a cpus_read_lock() protected region
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400963 */
Thomas Gleixner95966952017-05-24 10:15:17 +0200964static struct padata_instance *padata_alloc(struct workqueue_struct *wq,
965 const struct cpumask *pcpumask,
966 const struct cpumask *cbcpumask)
Steffen Klassert16295be2010-01-06 19:47:10 +1100967{
968 struct padata_instance *pinst;
Steffen Klassert33e54452010-07-07 15:31:26 +0200969 struct parallel_data *pd = NULL;
Steffen Klassert16295be2010-01-06 19:47:10 +1100970
971 pinst = kzalloc(sizeof(struct padata_instance), GFP_KERNEL);
972 if (!pinst)
973 goto err;
974
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400975 if (!alloc_cpumask_var(&pinst->cpumask.pcpu, GFP_KERNEL))
Steffen Klassert16295be2010-01-06 19:47:10 +1100976 goto err_free_inst;
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400977 if (!alloc_cpumask_var(&pinst->cpumask.cbcpu, GFP_KERNEL)) {
978 free_cpumask_var(pinst->cpumask.pcpu);
979 goto err_free_inst;
Steffen Klassert33e54452010-07-07 15:31:26 +0200980 }
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400981 if (!padata_validate_cpumask(pinst, pcpumask) ||
982 !padata_validate_cpumask(pinst, cbcpumask))
983 goto err_free_masks;
Steffen Klassert16295be2010-01-06 19:47:10 +1100984
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400985 pd = padata_alloc_pd(pinst, pcpumask, cbcpumask);
986 if (!pd)
987 goto err_free_masks;
Steffen Klassert74781382010-03-04 13:30:22 +0800988
Steffen Klassert16295be2010-01-06 19:47:10 +1100989 rcu_assign_pointer(pinst->pd, pd);
990
991 pinst->wq = wq;
992
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400993 cpumask_copy(pinst->cpumask.pcpu, pcpumask);
994 cpumask_copy(pinst->cpumask.cbcpu, cbcpumask);
Steffen Klassert16295be2010-01-06 19:47:10 +1100995
996 pinst->flags = 0;
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 Siewiorc5a81c82017-05-24 10:15:18 +02001003 cpuhp_state_add_instance_nocalls_cpuslocked(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);
1012err:
1013 return NULL;
1014}
Steffen Klassert16295be2010-01-06 19:47:10 +11001015
Steffen Klassert0198ffd2010-05-19 13:44:27 +10001016/**
Thomas Gleixner95966952017-05-24 10:15:17 +02001017 * padata_alloc_possible - Allocate and initialize padata instance.
1018 * Use the cpu_possible_mask for serial and
1019 * parallel workers.
1020 *
1021 * @wq: workqueue to use for the allocated padata instance
Sebastian Andrzej Siewiorc5a81c82017-05-24 10:15:18 +02001022 *
1023 * Must be called from a cpus_read_lock() protected region
Thomas Gleixner95966952017-05-24 10:15:17 +02001024 */
1025struct padata_instance *padata_alloc_possible(struct workqueue_struct *wq)
1026{
Sebastian Andrzej Siewiorc5a81c82017-05-24 10:15:18 +02001027 lockdep_assert_cpus_held();
Thomas Gleixner95966952017-05-24 10:15:17 +02001028 return padata_alloc(wq, cpu_possible_mask, cpu_possible_mask);
1029}
1030EXPORT_SYMBOL(padata_alloc_possible);
1031
1032/**
Steffen Klassert16295be2010-01-06 19:47:10 +11001033 * padata_free - free a padata instance
1034 *
Steffen Klassert0198ffd2010-05-19 13:44:27 +10001035 * @padata_inst: padata instance to free
Steffen Klassert16295be2010-01-06 19:47:10 +11001036 */
1037void padata_free(struct padata_instance *pinst)
1038{
Dan Kruchinin5e017dc2010-07-14 14:33:08 +04001039 kobject_put(&pinst->kobj);
Steffen Klassert16295be2010-01-06 19:47:10 +11001040}
1041EXPORT_SYMBOL(padata_free);
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +02001042
1043#ifdef CONFIG_HOTPLUG_CPU
1044
1045static __init int padata_driver_init(void)
1046{
1047 int ret;
1048
1049 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "padata:online",
1050 padata_cpu_online,
1051 padata_cpu_prep_down);
1052 if (ret < 0)
1053 return ret;
1054 hp_online = ret;
1055 return 0;
1056}
1057module_init(padata_driver_init);
1058
1059static __exit void padata_driver_exit(void)
1060{
1061 cpuhp_remove_multi_state(hp_online);
1062}
1063module_exit(padata_driver_exit);
1064#endif