Cheah Kok Cheong | 08b21fb | 2017-12-21 19:35:30 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 2 | /* |
| 3 | * padata.c - generic interface to process data streams in parallel |
| 4 | * |
Steffen Klassert | 107f8bd | 2012-03-28 08:42:34 +0200 | [diff] [blame] | 5 | * See Documentation/padata.txt for an api documentation. |
| 6 | * |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 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 | |
Paul Gortmaker | 9984de1 | 2011-05-23 14:51:41 -0400 | [diff] [blame] | 24 | #include <linux/export.h> |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 31 | #include <linux/slab.h> |
Dan Kruchinin | 5e017dc | 2010-07-14 14:33:08 +0400 | [diff] [blame] | 32 | #include <linux/sysfs.h> |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 33 | #include <linux/rcupdate.h> |
Sebastian Andrzej Siewior | 30e9215 | 2016-09-06 19:04:49 +0200 | [diff] [blame] | 34 | #include <linux/module.h> |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 35 | |
Steffen Klassert | 97e3d94 | 2010-04-29 14:37:32 +0200 | [diff] [blame] | 36 | #define MAX_OBJ_NUM 1000 |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 37 | |
| 38 | static int padata_index_to_cpu(struct parallel_data *pd, int cpu_index) |
| 39 | { |
| 40 | int cpu, target_cpu; |
| 41 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 42 | target_cpu = cpumask_first(pd->cpumask.pcpu); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 43 | for (cpu = 0; cpu < cpu_index; cpu++) |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 44 | target_cpu = cpumask_next(target_cpu, pd->cpumask.pcpu); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 45 | |
| 46 | return target_cpu; |
| 47 | } |
| 48 | |
Steffen Klassert | 2dc9b5d | 2012-03-09 07:20:49 +0100 | [diff] [blame] | 49 | static int padata_cpu_hash(struct parallel_data *pd) |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 50 | { |
Mathias Krause | 0b6b098 | 2013-10-25 12:14:15 +0200 | [diff] [blame] | 51 | unsigned int seq_nr; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 52 | int cpu_index; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 53 | |
| 54 | /* |
| 55 | * Hash the sequence numbers to the cpus by taking |
| 56 | * seq_nr mod. number of cpus in use. |
| 57 | */ |
Steffen Klassert | 2dc9b5d | 2012-03-09 07:20:49 +0100 | [diff] [blame] | 58 | |
Mathias Krause | 0b6b098 | 2013-10-25 12:14:15 +0200 | [diff] [blame] | 59 | seq_nr = atomic_inc_return(&pd->seq_nr); |
| 60 | cpu_index = seq_nr % cpumask_weight(pd->cpumask.pcpu); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 61 | |
| 62 | return padata_index_to_cpu(pd, cpu_index); |
| 63 | } |
| 64 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 65 | static void padata_parallel_worker(struct work_struct *parallel_work) |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 66 | { |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 67 | struct padata_parallel_queue *pqueue; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 68 | LIST_HEAD(local_list); |
| 69 | |
| 70 | local_bh_disable(); |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 71 | pqueue = container_of(parallel_work, |
| 72 | struct padata_parallel_queue, work); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 73 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 74 | spin_lock(&pqueue->parallel.lock); |
| 75 | list_replace_init(&pqueue->parallel.list, &local_list); |
| 76 | spin_unlock(&pqueue->parallel.lock); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 77 | |
| 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 Klassert | 0198ffd | 2010-05-19 13:44:27 +1000 | [diff] [blame] | 92 | /** |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 93 | * 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 Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 98 | * must be in the serial cpumask of padata(i.e. cpumask.cbcpu). |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 99 | * |
| 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 | */ |
| 104 | int padata_do_parallel(struct padata_instance *pinst, |
| 105 | struct padata_priv *padata, int cb_cpu) |
| 106 | { |
| 107 | int target_cpu, err; |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 108 | struct padata_parallel_queue *queue; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 109 | struct parallel_data *pd; |
| 110 | |
| 111 | rcu_read_lock_bh(); |
| 112 | |
Mathias Krause | c0e656b | 2013-11-28 19:20:05 +0100 | [diff] [blame] | 113 | pd = rcu_dereference_bh(pinst->pd); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 114 | |
Steffen Klassert | 83f619f | 2010-07-07 15:32:02 +0200 | [diff] [blame] | 115 | err = -EINVAL; |
Steffen Klassert | 7424713 | 2010-07-20 08:51:25 +0200 | [diff] [blame] | 116 | if (!(pinst->flags & PADATA_INIT) || pinst->flags & PADATA_INVALID) |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 117 | goto out; |
| 118 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 119 | if (!cpumask_test_cpu(cb_cpu, pd->cpumask.cbcpu)) |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 120 | 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 Klassert | 83f619f | 2010-07-07 15:32:02 +0200 | [diff] [blame] | 129 | err = 0; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 130 | atomic_inc(&pd->refcnt); |
| 131 | padata->pd = pd; |
| 132 | padata->cb_cpu = cb_cpu; |
| 133 | |
Steffen Klassert | 2dc9b5d | 2012-03-09 07:20:49 +0100 | [diff] [blame] | 134 | target_cpu = padata_cpu_hash(pd); |
Mathias Krause | 350ef88 | 2017-09-08 20:57:11 +0200 | [diff] [blame] | 135 | padata->cpu = target_cpu; |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 136 | queue = per_cpu_ptr(pd->pqueue, target_cpu); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 137 | |
| 138 | spin_lock(&queue->parallel.lock); |
| 139 | list_add_tail(&padata->list, &queue->parallel.list); |
| 140 | spin_unlock(&queue->parallel.lock); |
| 141 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 142 | queue_work_on(target_cpu, pinst->wq, &queue->work); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 143 | |
| 144 | out: |
| 145 | rcu_read_unlock_bh(); |
| 146 | |
| 147 | return err; |
| 148 | } |
| 149 | EXPORT_SYMBOL(padata_do_parallel); |
| 150 | |
Steffen Klassert | 0198ffd | 2010-05-19 13:44:27 +1000 | [diff] [blame] | 151 | /* |
| 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 Klassert | 0198ffd | 2010-05-19 13:44:27 +1000 | [diff] [blame] | 159 | * -EINPROGRESS, if the next object that needs serialization will |
| 160 | * be parallel processed by another cpu and is not yet present in |
| 161 | * the cpu's reorder queue. |
| 162 | * |
| 163 | * -ENODATA, if this cpu has to do the parallel processing for |
| 164 | * the next object. |
| 165 | */ |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 166 | static struct padata_priv *padata_get_next(struct parallel_data *pd) |
| 167 | { |
Shan Wei | f0fcf20 | 2012-12-06 17:16:23 +0800 | [diff] [blame] | 168 | struct padata_parallel_queue *next_queue; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 169 | struct padata_priv *padata; |
| 170 | struct padata_list *reorder; |
Herbert Xu | 6fc4dbc | 2019-07-18 23:01:46 +0800 | [diff] [blame^] | 171 | int cpu = pd->cpu; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 172 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 173 | next_queue = per_cpu_ptr(pd->pqueue, cpu); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 174 | reorder = &next_queue->reorder; |
| 175 | |
Jason A. Donenfeld | de5540d | 2017-03-23 12:24:43 +0100 | [diff] [blame] | 176 | spin_lock(&reorder->lock); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 177 | if (!list_empty(&reorder->list)) { |
| 178 | padata = list_entry(reorder->list.next, |
| 179 | struct padata_priv, list); |
| 180 | |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 181 | list_del_init(&padata->list); |
| 182 | atomic_dec(&pd->reorder_objects); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 183 | |
Herbert Xu | 6fc4dbc | 2019-07-18 23:01:46 +0800 | [diff] [blame^] | 184 | pd->cpu = cpumask_next_wrap(cpu, pd->cpumask.pcpu, -1, |
| 185 | false); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 186 | |
Jason A. Donenfeld | de5540d | 2017-03-23 12:24:43 +0100 | [diff] [blame] | 187 | spin_unlock(&reorder->lock); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 188 | goto out; |
| 189 | } |
Jason A. Donenfeld | de5540d | 2017-03-23 12:24:43 +0100 | [diff] [blame] | 190 | spin_unlock(&reorder->lock); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 191 | |
Shan Wei | f0fcf20 | 2012-12-06 17:16:23 +0800 | [diff] [blame] | 192 | if (__this_cpu_read(pd->pqueue->cpu_index) == next_queue->cpu_index) { |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 193 | padata = ERR_PTR(-ENODATA); |
| 194 | goto out; |
| 195 | } |
| 196 | |
| 197 | padata = ERR_PTR(-EINPROGRESS); |
| 198 | out: |
| 199 | return padata; |
| 200 | } |
| 201 | |
| 202 | static void padata_reorder(struct parallel_data *pd) |
| 203 | { |
Steffen Klassert | 3047817 | 2012-03-09 07:20:12 +0100 | [diff] [blame] | 204 | int cb_cpu; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 205 | struct padata_priv *padata; |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 206 | struct padata_serial_queue *squeue; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 207 | struct padata_instance *pinst = pd->pinst; |
Herbert Xu | 6fc4dbc | 2019-07-18 23:01:46 +0800 | [diff] [blame^] | 208 | struct padata_parallel_queue *next_queue; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 209 | |
Steffen Klassert | 0198ffd | 2010-05-19 13:44:27 +1000 | [diff] [blame] | 210 | /* |
| 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 Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 220 | if (!spin_trylock_bh(&pd->lock)) |
Steffen Klassert | d46a5ac | 2010-05-19 13:43:14 +1000 | [diff] [blame] | 221 | return; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 222 | |
| 223 | while (1) { |
| 224 | padata = padata_get_next(pd); |
| 225 | |
Steffen Klassert | 0198ffd | 2010-05-19 13:44:27 +1000 | [diff] [blame] | 226 | /* |
Jason A. Donenfeld | 69b3484 | 2017-04-12 10:40:19 +0200 | [diff] [blame] | 227 | * 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 Klassert | 0198ffd | 2010-05-19 13:44:27 +1000 | [diff] [blame] | 230 | */ |
Jason A. Donenfeld | 69b3484 | 2017-04-12 10:40:19 +0200 | [diff] [blame] | 231 | if (PTR_ERR(padata) == -EINPROGRESS) |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 232 | break; |
| 233 | |
Steffen Klassert | 0198ffd | 2010-05-19 13:44:27 +1000 | [diff] [blame] | 234 | /* |
| 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 Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 237 | * so exit immediately. |
Steffen Klassert | 0198ffd | 2010-05-19 13:44:27 +1000 | [diff] [blame] | 238 | */ |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 239 | if (PTR_ERR(padata) == -ENODATA) { |
| 240 | spin_unlock_bh(&pd->lock); |
Steffen Klassert | d46a5ac | 2010-05-19 13:43:14 +1000 | [diff] [blame] | 241 | return; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 242 | } |
| 243 | |
Steffen Klassert | 3047817 | 2012-03-09 07:20:12 +0100 | [diff] [blame] | 244 | cb_cpu = padata->cb_cpu; |
| 245 | squeue = per_cpu_ptr(pd->squeue, cb_cpu); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 246 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 247 | spin_lock(&squeue->serial.lock); |
| 248 | list_add_tail(&padata->list, &squeue->serial.list); |
| 249 | spin_unlock(&squeue->serial.lock); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 250 | |
Steffen Klassert | 3047817 | 2012-03-09 07:20:12 +0100 | [diff] [blame] | 251 | queue_work_on(cb_cpu, pinst->wq, &squeue->work); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | spin_unlock_bh(&pd->lock); |
| 255 | |
Steffen Klassert | 0198ffd | 2010-05-19 13:44:27 +1000 | [diff] [blame] | 256 | /* |
| 257 | * The next object that needs serialization might have arrived to |
Herbert Xu | 6fc4dbc | 2019-07-18 23:01:46 +0800 | [diff] [blame^] | 258 | * the reorder queues in the meantime. |
Daniel Jordan | cf144f8 | 2019-07-16 12:32:53 -0400 | [diff] [blame] | 259 | * |
Herbert Xu | 6fc4dbc | 2019-07-18 23:01:46 +0800 | [diff] [blame^] | 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 |
Daniel Jordan | cf144f8 | 2019-07-16 12:32:53 -0400 | [diff] [blame] | 262 | * smp_mb__after_atomic in padata_do_serial. |
Steffen Klassert | 0198ffd | 2010-05-19 13:44:27 +1000 | [diff] [blame] | 263 | */ |
Daniel Jordan | cf144f8 | 2019-07-16 12:32:53 -0400 | [diff] [blame] | 264 | smp_mb(); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 265 | |
Herbert Xu | 6fc4dbc | 2019-07-18 23:01:46 +0800 | [diff] [blame^] | 266 | 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 Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 269 | } |
| 270 | |
Mathias Krause | cf5868c | 2017-09-08 20:57:10 +0200 | [diff] [blame] | 271 | static void invoke_padata_reorder(struct work_struct *work) |
| 272 | { |
Mathias Krause | cf5868c | 2017-09-08 20:57:10 +0200 | [diff] [blame] | 273 | struct parallel_data *pd; |
| 274 | |
| 275 | local_bh_disable(); |
Herbert Xu | 6fc4dbc | 2019-07-18 23:01:46 +0800 | [diff] [blame^] | 276 | pd = container_of(work, struct parallel_data, reorder_work); |
Mathias Krause | cf5868c | 2017-09-08 20:57:10 +0200 | [diff] [blame] | 277 | padata_reorder(pd); |
| 278 | local_bh_enable(); |
| 279 | } |
| 280 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 281 | static void padata_serial_worker(struct work_struct *serial_work) |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 282 | { |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 283 | struct padata_serial_queue *squeue; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 284 | struct parallel_data *pd; |
| 285 | LIST_HEAD(local_list); |
| 286 | |
| 287 | local_bh_disable(); |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 288 | squeue = container_of(serial_work, struct padata_serial_queue, work); |
| 289 | pd = squeue->pd; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 290 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 291 | spin_lock(&squeue->serial.lock); |
| 292 | list_replace_init(&squeue->serial.list, &local_list); |
| 293 | spin_unlock(&squeue->serial.lock); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 294 | |
| 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 Klassert | 0198ffd | 2010-05-19 13:44:27 +1000 | [diff] [blame] | 309 | /** |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 310 | * 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 | */ |
| 317 | void padata_do_serial(struct padata_priv *padata) |
| 318 | { |
| 319 | int cpu; |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 320 | struct padata_parallel_queue *pqueue; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 321 | struct parallel_data *pd; |
Mathias Krause | 350ef88 | 2017-09-08 20:57:11 +0200 | [diff] [blame] | 322 | int reorder_via_wq = 0; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 323 | |
| 324 | pd = padata->pd; |
| 325 | |
| 326 | cpu = get_cpu(); |
Mathias Krause | 350ef88 | 2017-09-08 20:57:11 +0200 | [diff] [blame] | 327 | |
Herbert Xu | 6fc4dbc | 2019-07-18 23:01:46 +0800 | [diff] [blame^] | 328 | /* We need to enqueue the padata object into the correct |
| 329 | * per-cpu queue. |
Mathias Krause | 350ef88 | 2017-09-08 20:57:11 +0200 | [diff] [blame] | 330 | */ |
| 331 | if (cpu != padata->cpu) { |
| 332 | reorder_via_wq = 1; |
| 333 | cpu = padata->cpu; |
| 334 | } |
| 335 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 336 | pqueue = per_cpu_ptr(pd->pqueue, cpu); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 337 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 338 | spin_lock(&pqueue->reorder.lock); |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 339 | list_add_tail(&padata->list, &pqueue->reorder.list); |
Herbert Xu | 6fc4dbc | 2019-07-18 23:01:46 +0800 | [diff] [blame^] | 340 | atomic_inc(&pd->reorder_objects); |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 341 | spin_unlock(&pqueue->reorder.lock); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 342 | |
Daniel Jordan | cf144f8 | 2019-07-16 12:32:53 -0400 | [diff] [blame] | 343 | /* |
Herbert Xu | 6fc4dbc | 2019-07-18 23:01:46 +0800 | [diff] [blame^] | 344 | * Ensure the addition to the reorder list is ordered correctly |
Daniel Jordan | cf144f8 | 2019-07-16 12:32:53 -0400 | [diff] [blame] | 345 | * 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 Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 350 | put_cpu(); |
| 351 | |
Herbert Xu | 6fc4dbc | 2019-07-18 23:01:46 +0800 | [diff] [blame^] | 352 | padata_reorder(pd); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 353 | } |
| 354 | EXPORT_SYMBOL(padata_do_serial); |
| 355 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 356 | static int padata_setup_cpumasks(struct parallel_data *pd, |
| 357 | const struct cpumask *pcpumask, |
| 358 | const struct cpumask *cbcpumask) |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 359 | { |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 360 | if (!alloc_cpumask_var(&pd->cpumask.pcpu, GFP_KERNEL)) |
| 361 | return -ENOMEM; |
| 362 | |
Steffen Klassert | 13614e0 | 2012-03-28 08:43:21 +0200 | [diff] [blame] | 363 | cpumask_and(pd->cpumask.pcpu, pcpumask, cpu_online_mask); |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 364 | if (!alloc_cpumask_var(&pd->cpumask.cbcpu, GFP_KERNEL)) { |
Jason A. Donenfeld | 07a7792 | 2017-04-07 02:33:30 +0200 | [diff] [blame] | 365 | free_cpumask_var(pd->cpumask.pcpu); |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 366 | return -ENOMEM; |
| 367 | } |
| 368 | |
Steffen Klassert | 13614e0 | 2012-03-28 08:43:21 +0200 | [diff] [blame] | 369 | cpumask_and(pd->cpumask.cbcpu, cbcpumask, cpu_online_mask); |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | static 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 */ |
| 380 | static 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 */ |
| 394 | static void padata_init_pqueues(struct parallel_data *pd) |
| 395 | { |
Steffen Klassert | 2dc9b5d | 2012-03-09 07:20:49 +0100 | [diff] [blame] | 396 | int cpu_index, cpu; |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 397 | struct padata_parallel_queue *pqueue; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 398 | |
| 399 | cpu_index = 0; |
Mathias Krause | 1bd845b | 2017-09-08 20:57:09 +0200 | [diff] [blame] | 400 | for_each_possible_cpu(cpu) { |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 401 | pqueue = per_cpu_ptr(pd->pqueue, cpu); |
Mathias Krause | 1bd845b | 2017-09-08 20:57:09 +0200 | [diff] [blame] | 402 | |
| 403 | if (!cpumask_test_cpu(cpu, pd->cpumask.pcpu)) { |
| 404 | pqueue->cpu_index = -1; |
| 405 | continue; |
| 406 | } |
| 407 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 408 | pqueue->cpu_index = cpu_index; |
Steffen Klassert | fad3a90 | 2010-07-20 08:48:34 +0200 | [diff] [blame] | 409 | cpu_index++; |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 410 | |
| 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 Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | /* Allocate and initialize the internal cpumask dependend resources. */ |
| 419 | static 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 Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 424 | |
| 425 | pd = kzalloc(sizeof(struct parallel_data), GFP_KERNEL); |
| 426 | if (!pd) |
| 427 | goto err; |
| 428 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 429 | pd->pqueue = alloc_percpu(struct padata_parallel_queue); |
| 430 | if (!pd->pqueue) |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 431 | goto err_free_pd; |
| 432 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 433 | 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 Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 438 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 439 | padata_init_pqueues(pd); |
| 440 | padata_init_squeues(pd); |
Mathias Krause | 0b6b098 | 2013-10-25 12:14:15 +0200 | [diff] [blame] | 441 | atomic_set(&pd->seq_nr, -1); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 442 | atomic_set(&pd->reorder_objects, 0); |
| 443 | atomic_set(&pd->refcnt, 0); |
| 444 | pd->pinst = pinst; |
| 445 | spin_lock_init(&pd->lock); |
Herbert Xu | 6fc4dbc | 2019-07-18 23:01:46 +0800 | [diff] [blame^] | 446 | pd->cpu = cpumask_first(pcpumask); |
| 447 | INIT_WORK(&pd->reorder_work, invoke_padata_reorder); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 448 | |
| 449 | return pd; |
| 450 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 451 | err_free_squeue: |
| 452 | free_percpu(pd->squeue); |
| 453 | err_free_pqueue: |
| 454 | free_percpu(pd->pqueue); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 455 | err_free_pd: |
| 456 | kfree(pd); |
| 457 | err: |
| 458 | return NULL; |
| 459 | } |
| 460 | |
| 461 | static void padata_free_pd(struct parallel_data *pd) |
| 462 | { |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 463 | free_cpumask_var(pd->cpumask.pcpu); |
| 464 | free_cpumask_var(pd->cpumask.cbcpu); |
| 465 | free_percpu(pd->pqueue); |
| 466 | free_percpu(pd->squeue); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 467 | kfree(pd); |
| 468 | } |
| 469 | |
Steffen Klassert | 0198ffd | 2010-05-19 13:44:27 +1000 | [diff] [blame] | 470 | /* Flush all objects out of the padata queues. */ |
Steffen Klassert | 2b73b07 | 2010-05-19 13:43:46 +1000 | [diff] [blame] | 471 | static void padata_flush_queues(struct parallel_data *pd) |
| 472 | { |
| 473 | int cpu; |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 474 | struct padata_parallel_queue *pqueue; |
| 475 | struct padata_serial_queue *squeue; |
Steffen Klassert | 2b73b07 | 2010-05-19 13:43:46 +1000 | [diff] [blame] | 476 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 477 | for_each_cpu(cpu, pd->cpumask.pcpu) { |
| 478 | pqueue = per_cpu_ptr(pd->pqueue, cpu); |
| 479 | flush_work(&pqueue->work); |
Steffen Klassert | 2b73b07 | 2010-05-19 13:43:46 +1000 | [diff] [blame] | 480 | } |
| 481 | |
Steffen Klassert | 2b73b07 | 2010-05-19 13:43:46 +1000 | [diff] [blame] | 482 | if (atomic_read(&pd->reorder_objects)) |
| 483 | padata_reorder(pd); |
| 484 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 485 | for_each_cpu(cpu, pd->cpumask.cbcpu) { |
| 486 | squeue = per_cpu_ptr(pd->squeue, cpu); |
| 487 | flush_work(&squeue->work); |
Steffen Klassert | 2b73b07 | 2010-05-19 13:43:46 +1000 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | BUG_ON(atomic_read(&pd->refcnt) != 0); |
| 491 | } |
| 492 | |
Steffen Klassert | 4c87917 | 2010-07-07 15:30:10 +0200 | [diff] [blame] | 493 | static void __padata_start(struct padata_instance *pinst) |
| 494 | { |
| 495 | pinst->flags |= PADATA_INIT; |
| 496 | } |
| 497 | |
Steffen Klassert | ee83655 | 2010-07-07 15:30:47 +0200 | [diff] [blame] | 498 | static 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 Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 512 | /* Replace the internal control structure with a new one. */ |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 513 | static void padata_replace(struct padata_instance *pinst, |
| 514 | struct parallel_data *pd_new) |
| 515 | { |
| 516 | struct parallel_data *pd_old = pinst->pd; |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 517 | int notification_mask = 0; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 518 | |
| 519 | pinst->flags |= PADATA_RESET; |
| 520 | |
| 521 | rcu_assign_pointer(pinst->pd, pd_new); |
| 522 | |
| 523 | synchronize_rcu(); |
| 524 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 525 | 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 Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 529 | |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 530 | padata_flush_queues(pd_old); |
| 531 | padata_free_pd(pd_old); |
| 532 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 533 | if (notification_mask) |
| 534 | blocking_notifier_call_chain(&pinst->cpumask_change_notifier, |
Steffen Klassert | c635696 | 2010-07-27 07:15:50 +0200 | [diff] [blame] | 535 | notification_mask, |
| 536 | &pd_new->cpumask); |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 537 | |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 538 | pinst->flags &= ~PADATA_RESET; |
| 539 | } |
| 540 | |
Steffen Klassert | 0198ffd | 2010-05-19 13:44:27 +1000 | [diff] [blame] | 541 | /** |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 542 | * padata_register_cpumask_notifier - Registers a notifier that will be called |
| 543 | * if either pcpu or cbcpu or both cpumasks change. |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 544 | * |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 545 | * @pinst: A poineter to padata instance |
| 546 | * @nblock: A pointer to notifier block. |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 547 | */ |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 548 | int padata_register_cpumask_notifier(struct padata_instance *pinst, |
| 549 | struct notifier_block *nblock) |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 550 | { |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 551 | return blocking_notifier_chain_register(&pinst->cpumask_change_notifier, |
| 552 | nblock); |
| 553 | } |
| 554 | EXPORT_SYMBOL(padata_register_cpumask_notifier); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 555 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 556 | /** |
| 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 | */ |
| 563 | int 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 | } |
| 570 | EXPORT_SYMBOL(padata_unregister_cpumask_notifier); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 571 | |
Steffen Klassert | 6751fb3 | 2010-04-29 14:42:30 +0200 | [diff] [blame] | 572 | |
Steffen Klassert | 33e5445 | 2010-07-07 15:31:26 +0200 | [diff] [blame] | 573 | /* If cpumask contains no active cpu, we mark the instance as invalid. */ |
| 574 | static bool padata_validate_cpumask(struct padata_instance *pinst, |
| 575 | const struct cpumask *cpumask) |
| 576 | { |
Steffen Klassert | 13614e0 | 2012-03-28 08:43:21 +0200 | [diff] [blame] | 577 | if (!cpumask_intersects(cpumask, cpu_online_mask)) { |
Steffen Klassert | 33e5445 | 2010-07-07 15:31:26 +0200 | [diff] [blame] | 578 | pinst->flags |= PADATA_INVALID; |
| 579 | return false; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 580 | } |
| 581 | |
Steffen Klassert | 33e5445 | 2010-07-07 15:31:26 +0200 | [diff] [blame] | 582 | pinst->flags &= ~PADATA_INVALID; |
| 583 | return true; |
| 584 | } |
| 585 | |
Steffen Klassert | 65ff577 | 2010-07-27 07:15:06 +0200 | [diff] [blame] | 586 | static int __padata_set_cpumasks(struct padata_instance *pinst, |
| 587 | cpumask_var_t pcpumask, |
| 588 | cpumask_var_t cbcpumask) |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 589 | { |
Steffen Klassert | 33e5445 | 2010-07-07 15:31:26 +0200 | [diff] [blame] | 590 | int valid; |
Steffen Klassert | 65ff577 | 2010-07-27 07:15:06 +0200 | [diff] [blame] | 591 | struct parallel_data *pd; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 592 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 593 | 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 Klassert | b89661d | 2010-07-20 08:49:20 +0200 | [diff] [blame] | 600 | if (!valid) |
Steffen Klassert | 33e5445 | 2010-07-07 15:31:26 +0200 | [diff] [blame] | 601 | __padata_stop(pinst); |
Steffen Klassert | 33e5445 | 2010-07-07 15:31:26 +0200 | [diff] [blame] | 602 | |
Steffen Klassert | b89661d | 2010-07-20 08:49:20 +0200 | [diff] [blame] | 603 | out_replace: |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 604 | pd = padata_alloc_pd(pinst, pcpumask, cbcpumask); |
Steffen Klassert | 65ff577 | 2010-07-27 07:15:06 +0200 | [diff] [blame] | 605 | if (!pd) |
| 606 | return -ENOMEM; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 607 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 608 | cpumask_copy(pinst->cpumask.pcpu, pcpumask); |
| 609 | cpumask_copy(pinst->cpumask.cbcpu, cbcpumask); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 610 | |
| 611 | padata_replace(pinst, pd); |
| 612 | |
Steffen Klassert | 33e5445 | 2010-07-07 15:31:26 +0200 | [diff] [blame] | 613 | if (valid) |
| 614 | __padata_start(pinst); |
| 615 | |
Steffen Klassert | 65ff577 | 2010-07-27 07:15:06 +0200 | [diff] [blame] | 616 | return 0; |
| 617 | } |
| 618 | |
| 619 | /** |
Steffen Klassert | 65ff577 | 2010-07-27 07:15:06 +0200 | [diff] [blame] | 620 | * 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 | */ |
| 628 | int 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 Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 652 | out: |
Steffen Klassert | 6751fb3 | 2010-04-29 14:42:30 +0200 | [diff] [blame] | 653 | put_online_cpus(); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 654 | mutex_unlock(&pinst->lock); |
| 655 | |
| 656 | return err; |
| 657 | } |
| 658 | EXPORT_SYMBOL(padata_set_cpumask); |
| 659 | |
Arnd Bergmann | 19d795b | 2016-05-19 17:09:59 -0700 | [diff] [blame] | 660 | /** |
| 661 | * padata_start - start the parallel processing |
| 662 | * |
| 663 | * @pinst: padata instance to start |
| 664 | */ |
| 665 | int 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 King | 8ddab42 | 2018-11-09 13:16:39 +0000 | [diff] [blame] | 674 | __padata_start(pinst); |
Arnd Bergmann | 19d795b | 2016-05-19 17:09:59 -0700 | [diff] [blame] | 675 | |
| 676 | mutex_unlock(&pinst->lock); |
| 677 | |
| 678 | return err; |
| 679 | } |
| 680 | EXPORT_SYMBOL(padata_start); |
| 681 | |
| 682 | /** |
| 683 | * padata_stop - stop the parallel processing |
| 684 | * |
| 685 | * @pinst: padata instance to stop |
| 686 | */ |
| 687 | void padata_stop(struct padata_instance *pinst) |
| 688 | { |
| 689 | mutex_lock(&pinst->lock); |
| 690 | __padata_stop(pinst); |
| 691 | mutex_unlock(&pinst->lock); |
| 692 | } |
| 693 | EXPORT_SYMBOL(padata_stop); |
| 694 | |
| 695 | #ifdef CONFIG_HOTPLUG_CPU |
| 696 | |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 697 | static int __padata_add_cpu(struct padata_instance *pinst, int cpu) |
| 698 | { |
| 699 | struct parallel_data *pd; |
| 700 | |
Steffen Klassert | 13614e0 | 2012-03-28 08:43:21 +0200 | [diff] [blame] | 701 | if (cpumask_test_cpu(cpu, cpu_online_mask)) { |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 702 | pd = padata_alloc_pd(pinst, pinst->cpumask.pcpu, |
| 703 | pinst->cpumask.cbcpu); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 704 | if (!pd) |
| 705 | return -ENOMEM; |
| 706 | |
| 707 | padata_replace(pinst, pd); |
Steffen Klassert | 33e5445 | 2010-07-07 15:31:26 +0200 | [diff] [blame] | 708 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 709 | if (padata_validate_cpumask(pinst, pinst->cpumask.pcpu) && |
| 710 | padata_validate_cpumask(pinst, pinst->cpumask.cbcpu)) |
Steffen Klassert | 33e5445 | 2010-07-07 15:31:26 +0200 | [diff] [blame] | 711 | __padata_start(pinst); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | return 0; |
| 715 | } |
| 716 | |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 717 | static int __padata_remove_cpu(struct padata_instance *pinst, int cpu) |
| 718 | { |
Steffen Klassert | 33e5445 | 2010-07-07 15:31:26 +0200 | [diff] [blame] | 719 | struct parallel_data *pd = NULL; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 720 | |
| 721 | if (cpumask_test_cpu(cpu, cpu_online_mask)) { |
Steffen Klassert | 33e5445 | 2010-07-07 15:31:26 +0200 | [diff] [blame] | 722 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 723 | if (!padata_validate_cpumask(pinst, pinst->cpumask.pcpu) || |
Steffen Klassert | b89661d | 2010-07-20 08:49:20 +0200 | [diff] [blame] | 724 | !padata_validate_cpumask(pinst, pinst->cpumask.cbcpu)) |
Steffen Klassert | 33e5445 | 2010-07-07 15:31:26 +0200 | [diff] [blame] | 725 | __padata_stop(pinst); |
Steffen Klassert | 33e5445 | 2010-07-07 15:31:26 +0200 | [diff] [blame] | 726 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 727 | pd = padata_alloc_pd(pinst, pinst->cpumask.pcpu, |
| 728 | pinst->cpumask.cbcpu); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 729 | if (!pd) |
| 730 | return -ENOMEM; |
| 731 | |
| 732 | padata_replace(pinst, pd); |
Steffen Klassert | 9612090 | 2012-03-28 08:44:07 +0200 | [diff] [blame] | 733 | |
| 734 | cpumask_clear_cpu(cpu, pd->cpumask.cbcpu); |
| 735 | cpumask_clear_cpu(cpu, pd->cpumask.pcpu); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | return 0; |
| 739 | } |
| 740 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 741 | /** |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 742 | * padata_remove_cpu - remove a cpu from the one or both(serial and parallel) |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 743 | * padata cpumasks. |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 744 | * |
| 745 | * @pinst: padata instance |
| 746 | * @cpu: cpu to remove |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 747 | * @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 Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 751 | */ |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 752 | int padata_remove_cpu(struct padata_instance *pinst, int cpu, int mask) |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 753 | { |
| 754 | int err; |
| 755 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 756 | if (!(mask & (PADATA_CPU_SERIAL | PADATA_CPU_PARALLEL))) |
| 757 | return -EINVAL; |
| 758 | |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 759 | mutex_lock(&pinst->lock); |
| 760 | |
Steffen Klassert | 6751fb3 | 2010-04-29 14:42:30 +0200 | [diff] [blame] | 761 | get_online_cpus(); |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 762 | 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 Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 767 | err = __padata_remove_cpu(pinst, cpu); |
Steffen Klassert | 6751fb3 | 2010-04-29 14:42:30 +0200 | [diff] [blame] | 768 | put_online_cpus(); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 769 | |
| 770 | mutex_unlock(&pinst->lock); |
| 771 | |
| 772 | return err; |
| 773 | } |
| 774 | EXPORT_SYMBOL(padata_remove_cpu); |
| 775 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 776 | static 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 Siewior | 30e9215 | 2016-09-06 19:04:49 +0200 | [diff] [blame] | 782 | static int padata_cpu_online(unsigned int cpu, struct hlist_node *node) |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 783 | { |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 784 | struct padata_instance *pinst; |
Sebastian Andrzej Siewior | 30e9215 | 2016-09-06 19:04:49 +0200 | [diff] [blame] | 785 | int ret; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 786 | |
Sebastian Andrzej Siewior | 30e9215 | 2016-09-06 19:04:49 +0200 | [diff] [blame] | 787 | pinst = hlist_entry_safe(node, struct padata_instance, node); |
| 788 | if (!pinst_has_cpu(pinst, cpu)) |
| 789 | return 0; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 790 | |
Sebastian Andrzej Siewior | 30e9215 | 2016-09-06 19:04:49 +0200 | [diff] [blame] | 791 | mutex_lock(&pinst->lock); |
| 792 | ret = __padata_add_cpu(pinst, cpu); |
| 793 | mutex_unlock(&pinst->lock); |
| 794 | return ret; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 795 | } |
Sebastian Andrzej Siewior | 30e9215 | 2016-09-06 19:04:49 +0200 | [diff] [blame] | 796 | |
| 797 | static 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 | |
| 812 | static enum cpuhp_state hp_online; |
Steffen Klassert | e2cb2f1 | 2010-04-29 14:40:10 +0200 | [diff] [blame] | 813 | #endif |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 814 | |
Dan Kruchinin | 5e017dc | 2010-07-14 14:33:08 +0400 | [diff] [blame] | 815 | static void __padata_free(struct padata_instance *pinst) |
| 816 | { |
| 817 | #ifdef CONFIG_HOTPLUG_CPU |
Sebastian Andrzej Siewior | 30e9215 | 2016-09-06 19:04:49 +0200 | [diff] [blame] | 818 | cpuhp_state_remove_instance_nocalls(hp_online, &pinst->node); |
Dan Kruchinin | 5e017dc | 2010-07-14 14:33:08 +0400 | [diff] [blame] | 819 | #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 | |
| 833 | static void padata_sysfs_release(struct kobject *kobj) |
| 834 | { |
| 835 | struct padata_instance *pinst = kobj2pinst(kobj); |
| 836 | __padata_free(pinst); |
| 837 | } |
| 838 | |
| 839 | struct 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 | |
| 846 | static 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 Heo | 4497da6 | 2015-02-13 14:38:05 -0800 | [diff] [blame] | 858 | len = snprintf(buf, PAGE_SIZE, "%*pb\n", |
| 859 | nr_cpu_ids, cpumask_bits(cpumask)); |
Dan Kruchinin | 5e017dc | 2010-07-14 14:33:08 +0400 | [diff] [blame] | 860 | mutex_unlock(&pinst->lock); |
Tejun Heo | 4497da6 | 2015-02-13 14:38:05 -0800 | [diff] [blame] | 861 | return len < PAGE_SIZE ? len : -EINVAL; |
Dan Kruchinin | 5e017dc | 2010-07-14 14:33:08 +0400 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | static 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 | |
| 886 | out: |
| 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 | |
| 898 | PADATA_ATTR_RW(serial_cpumask, show_cpumask, store_cpumask); |
| 899 | PADATA_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 Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 905 | */ |
Dan Kruchinin | 5e017dc | 2010-07-14 14:33:08 +0400 | [diff] [blame] | 906 | static struct attribute *padata_default_attrs[] = { |
| 907 | &serial_cpumask_attr.attr, |
| 908 | ¶llel_cpumask_attr.attr, |
| 909 | NULL, |
| 910 | }; |
Kimberly Brown | 2064fbc | 2019-04-01 22:51:47 -0400 | [diff] [blame] | 911 | ATTRIBUTE_GROUPS(padata_default); |
Dan Kruchinin | 5e017dc | 2010-07-14 14:33:08 +0400 | [diff] [blame] | 912 | |
| 913 | static ssize_t padata_sysfs_show(struct kobject *kobj, |
| 914 | struct attribute *attr, char *buf) |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 915 | { |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 916 | struct padata_instance *pinst; |
Dan Kruchinin | 5e017dc | 2010-07-14 14:33:08 +0400 | [diff] [blame] | 917 | 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 | |
| 928 | static 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 | |
| 943 | static const struct sysfs_ops padata_sysfs_ops = { |
| 944 | .show = padata_sysfs_show, |
| 945 | .store = padata_sysfs_store, |
| 946 | }; |
| 947 | |
| 948 | static struct kobj_type padata_attr_type = { |
| 949 | .sysfs_ops = &padata_sysfs_ops, |
Kimberly Brown | 2064fbc | 2019-04-01 22:51:47 -0400 | [diff] [blame] | 950 | .default_groups = padata_default_groups, |
Dan Kruchinin | 5e017dc | 2010-07-14 14:33:08 +0400 | [diff] [blame] | 951 | .release = padata_sysfs_release, |
| 952 | }; |
| 953 | |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 954 | /** |
Steffen Klassert | e6cc117 | 2010-07-27 07:14:28 +0200 | [diff] [blame] | 955 | * padata_alloc - allocate and initialize a padata instance and specify |
| 956 | * cpumasks for serial and parallel workers. |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 957 | * |
| 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 Siewior | c5a81c8 | 2017-05-24 10:15:18 +0200 | [diff] [blame] | 961 | * |
| 962 | * Must be called from a cpus_read_lock() protected region |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 963 | */ |
Thomas Gleixner | 9596695 | 2017-05-24 10:15:17 +0200 | [diff] [blame] | 964 | static struct padata_instance *padata_alloc(struct workqueue_struct *wq, |
| 965 | const struct cpumask *pcpumask, |
| 966 | const struct cpumask *cbcpumask) |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 967 | { |
| 968 | struct padata_instance *pinst; |
Steffen Klassert | 33e5445 | 2010-07-07 15:31:26 +0200 | [diff] [blame] | 969 | struct parallel_data *pd = NULL; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 970 | |
| 971 | pinst = kzalloc(sizeof(struct padata_instance), GFP_KERNEL); |
| 972 | if (!pinst) |
| 973 | goto err; |
| 974 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 975 | if (!alloc_cpumask_var(&pinst->cpumask.pcpu, GFP_KERNEL)) |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 976 | goto err_free_inst; |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 977 | if (!alloc_cpumask_var(&pinst->cpumask.cbcpu, GFP_KERNEL)) { |
| 978 | free_cpumask_var(pinst->cpumask.pcpu); |
| 979 | goto err_free_inst; |
Steffen Klassert | 33e5445 | 2010-07-07 15:31:26 +0200 | [diff] [blame] | 980 | } |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 981 | if (!padata_validate_cpumask(pinst, pcpumask) || |
| 982 | !padata_validate_cpumask(pinst, cbcpumask)) |
| 983 | goto err_free_masks; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 984 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 985 | pd = padata_alloc_pd(pinst, pcpumask, cbcpumask); |
| 986 | if (!pd) |
| 987 | goto err_free_masks; |
Steffen Klassert | 7478138 | 2010-03-04 13:30:22 +0800 | [diff] [blame] | 988 | |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 989 | rcu_assign_pointer(pinst->pd, pd); |
| 990 | |
| 991 | pinst->wq = wq; |
| 992 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 993 | cpumask_copy(pinst->cpumask.pcpu, pcpumask); |
| 994 | cpumask_copy(pinst->cpumask.cbcpu, cbcpumask); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 995 | |
| 996 | pinst->flags = 0; |
| 997 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 998 | BLOCKING_INIT_NOTIFIER_HEAD(&pinst->cpumask_change_notifier); |
Dan Kruchinin | 5e017dc | 2010-07-14 14:33:08 +0400 | [diff] [blame] | 999 | kobject_init(&pinst->kobj, &padata_attr_type); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 1000 | mutex_init(&pinst->lock); |
| 1001 | |
Richard Weinberger | b8b4a41 | 2013-08-23 13:12:33 +0200 | [diff] [blame] | 1002 | #ifdef CONFIG_HOTPLUG_CPU |
Sebastian Andrzej Siewior | c5a81c8 | 2017-05-24 10:15:18 +0200 | [diff] [blame] | 1003 | cpuhp_state_add_instance_nocalls_cpuslocked(hp_online, &pinst->node); |
Richard Weinberger | b8b4a41 | 2013-08-23 13:12:33 +0200 | [diff] [blame] | 1004 | #endif |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 1005 | return pinst; |
| 1006 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 1007 | err_free_masks: |
| 1008 | free_cpumask_var(pinst->cpumask.pcpu); |
| 1009 | free_cpumask_var(pinst->cpumask.cbcpu); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 1010 | err_free_inst: |
| 1011 | kfree(pinst); |
| 1012 | err: |
| 1013 | return NULL; |
| 1014 | } |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 1015 | |
Steffen Klassert | 0198ffd | 2010-05-19 13:44:27 +1000 | [diff] [blame] | 1016 | /** |
Thomas Gleixner | 9596695 | 2017-05-24 10:15:17 +0200 | [diff] [blame] | 1017 | * 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 Siewior | c5a81c8 | 2017-05-24 10:15:18 +0200 | [diff] [blame] | 1022 | * |
| 1023 | * Must be called from a cpus_read_lock() protected region |
Thomas Gleixner | 9596695 | 2017-05-24 10:15:17 +0200 | [diff] [blame] | 1024 | */ |
| 1025 | struct padata_instance *padata_alloc_possible(struct workqueue_struct *wq) |
| 1026 | { |
Sebastian Andrzej Siewior | c5a81c8 | 2017-05-24 10:15:18 +0200 | [diff] [blame] | 1027 | lockdep_assert_cpus_held(); |
Thomas Gleixner | 9596695 | 2017-05-24 10:15:17 +0200 | [diff] [blame] | 1028 | return padata_alloc(wq, cpu_possible_mask, cpu_possible_mask); |
| 1029 | } |
| 1030 | EXPORT_SYMBOL(padata_alloc_possible); |
| 1031 | |
| 1032 | /** |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 1033 | * padata_free - free a padata instance |
| 1034 | * |
Steffen Klassert | 0198ffd | 2010-05-19 13:44:27 +1000 | [diff] [blame] | 1035 | * @padata_inst: padata instance to free |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 1036 | */ |
| 1037 | void padata_free(struct padata_instance *pinst) |
| 1038 | { |
Dan Kruchinin | 5e017dc | 2010-07-14 14:33:08 +0400 | [diff] [blame] | 1039 | kobject_put(&pinst->kobj); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 1040 | } |
| 1041 | EXPORT_SYMBOL(padata_free); |
Sebastian Andrzej Siewior | 30e9215 | 2016-09-06 19:04:49 +0200 | [diff] [blame] | 1042 | |
| 1043 | #ifdef CONFIG_HOTPLUG_CPU |
| 1044 | |
| 1045 | static __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 | } |
| 1057 | module_init(padata_driver_init); |
| 1058 | |
| 1059 | static __exit void padata_driver_exit(void) |
| 1060 | { |
| 1061 | cpuhp_remove_multi_state(hp_online); |
| 1062 | } |
| 1063 | module_exit(padata_driver_exit); |
| 1064 | #endif |