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