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