Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 1 | /* |
| 2 | * padata.c - generic interface to process data streams in parallel |
| 3 | * |
Steffen Klassert | 107f8bd | 2012-03-28 08:42:34 +0200 | [diff] [blame] | 4 | * See Documentation/padata.txt for an api documentation. |
| 5 | * |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 6 | * Copyright (C) 2008, 2009 secunet Security Networks AG |
| 7 | * Copyright (C) 2008, 2009 Steffen Klassert <steffen.klassert@secunet.com> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify it |
| 10 | * under the terms and conditions of the GNU General Public License, |
| 11 | * version 2, as published by the Free Software Foundation. |
| 12 | * |
| 13 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 15 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 16 | * more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along with |
| 19 | * this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
Paul Gortmaker | 9984de1 | 2011-05-23 14:51:41 -0400 | [diff] [blame] | 23 | #include <linux/export.h> |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 24 | #include <linux/cpumask.h> |
| 25 | #include <linux/err.h> |
| 26 | #include <linux/cpu.h> |
| 27 | #include <linux/padata.h> |
| 28 | #include <linux/mutex.h> |
| 29 | #include <linux/sched.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> |
Dan Kruchinin | 5e017dc | 2010-07-14 14:33:08 +0400 | [diff] [blame] | 31 | #include <linux/sysfs.h> |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 32 | #include <linux/rcupdate.h> |
Sebastian Andrzej Siewior | 30e9215 | 2016-09-06 19:04:49 +0200 | [diff] [blame] | 33 | #include <linux/module.h> |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 34 | |
Steffen Klassert | 97e3d94 | 2010-04-29 14:37:32 +0200 | [diff] [blame] | 35 | #define MAX_OBJ_NUM 1000 |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 36 | |
Herbert Xu | 928cf3d | 2019-11-19 13:17:31 +0800 | [diff] [blame] | 37 | static void padata_free_pd(struct parallel_data *pd); |
| 38 | |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 39 | static int padata_index_to_cpu(struct parallel_data *pd, int cpu_index) |
| 40 | { |
| 41 | int cpu, target_cpu; |
| 42 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 43 | target_cpu = cpumask_first(pd->cpumask.pcpu); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 44 | for (cpu = 0; cpu < cpu_index; cpu++) |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 45 | target_cpu = cpumask_next(target_cpu, pd->cpumask.pcpu); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 46 | |
| 47 | return target_cpu; |
| 48 | } |
| 49 | |
Steffen Klassert | 2dc9b5d | 2012-03-09 07:20:49 +0100 | [diff] [blame] | 50 | static int padata_cpu_hash(struct parallel_data *pd) |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 51 | { |
Mathias Krause | 0b6b098 | 2013-10-25 12:14:15 +0200 | [diff] [blame] | 52 | unsigned int seq_nr; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 53 | int cpu_index; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 54 | |
| 55 | /* |
| 56 | * Hash the sequence numbers to the cpus by taking |
| 57 | * seq_nr mod. number of cpus in use. |
| 58 | */ |
Steffen Klassert | 2dc9b5d | 2012-03-09 07:20:49 +0100 | [diff] [blame] | 59 | |
Mathias Krause | 0b6b098 | 2013-10-25 12:14:15 +0200 | [diff] [blame] | 60 | seq_nr = atomic_inc_return(&pd->seq_nr); |
| 61 | cpu_index = seq_nr % cpumask_weight(pd->cpumask.pcpu); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 62 | |
| 63 | return padata_index_to_cpu(pd, cpu_index); |
| 64 | } |
| 65 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 66 | static void padata_parallel_worker(struct work_struct *parallel_work) |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 67 | { |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 68 | struct padata_parallel_queue *pqueue; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 69 | struct parallel_data *pd; |
| 70 | struct padata_instance *pinst; |
| 71 | LIST_HEAD(local_list); |
| 72 | |
| 73 | local_bh_disable(); |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 74 | pqueue = container_of(parallel_work, |
| 75 | struct padata_parallel_queue, work); |
| 76 | pd = pqueue->pd; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 77 | pinst = pd->pinst; |
| 78 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 79 | spin_lock(&pqueue->parallel.lock); |
| 80 | list_replace_init(&pqueue->parallel.list, &local_list); |
| 81 | spin_unlock(&pqueue->parallel.lock); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 82 | |
| 83 | while (!list_empty(&local_list)) { |
| 84 | struct padata_priv *padata; |
| 85 | |
| 86 | padata = list_entry(local_list.next, |
| 87 | struct padata_priv, list); |
| 88 | |
| 89 | list_del_init(&padata->list); |
| 90 | |
| 91 | padata->parallel(padata); |
| 92 | } |
| 93 | |
| 94 | local_bh_enable(); |
| 95 | } |
| 96 | |
Steffen Klassert | 0198ffd | 2010-05-19 13:44:27 +1000 | [diff] [blame] | 97 | /** |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 98 | * padata_do_parallel - padata parallelization function |
| 99 | * |
| 100 | * @pinst: padata instance |
| 101 | * @padata: object to be parallelized |
| 102 | * @cb_cpu: cpu the serialization callback function will run on, |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 103 | * must be in the serial cpumask of padata(i.e. cpumask.cbcpu). |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 104 | * |
| 105 | * The parallelization callback function will run with BHs off. |
| 106 | * Note: Every object which is parallelized by padata_do_parallel |
| 107 | * must be seen by padata_do_serial. |
| 108 | */ |
| 109 | int padata_do_parallel(struct padata_instance *pinst, |
| 110 | struct padata_priv *padata, int cb_cpu) |
| 111 | { |
| 112 | int target_cpu, err; |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 113 | struct padata_parallel_queue *queue; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 114 | struct parallel_data *pd; |
| 115 | |
| 116 | rcu_read_lock_bh(); |
| 117 | |
Mathias Krause | c0e656b | 2013-11-28 19:20:05 +0100 | [diff] [blame] | 118 | pd = rcu_dereference_bh(pinst->pd); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 119 | |
Steffen Klassert | 83f619f | 2010-07-07 15:32:02 +0200 | [diff] [blame] | 120 | err = -EINVAL; |
Steffen Klassert | 7424713 | 2010-07-20 08:51:25 +0200 | [diff] [blame] | 121 | if (!(pinst->flags & PADATA_INIT) || pinst->flags & PADATA_INVALID) |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 122 | goto out; |
| 123 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 124 | if (!cpumask_test_cpu(cb_cpu, pd->cpumask.cbcpu)) |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 125 | goto out; |
| 126 | |
| 127 | err = -EBUSY; |
| 128 | if ((pinst->flags & PADATA_RESET)) |
| 129 | goto out; |
| 130 | |
| 131 | if (atomic_read(&pd->refcnt) >= MAX_OBJ_NUM) |
| 132 | goto out; |
| 133 | |
Steffen Klassert | 83f619f | 2010-07-07 15:32:02 +0200 | [diff] [blame] | 134 | err = 0; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 135 | atomic_inc(&pd->refcnt); |
| 136 | padata->pd = pd; |
| 137 | padata->cb_cpu = cb_cpu; |
| 138 | |
Steffen Klassert | 2dc9b5d | 2012-03-09 07:20:49 +0100 | [diff] [blame] | 139 | target_cpu = padata_cpu_hash(pd); |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 140 | queue = per_cpu_ptr(pd->pqueue, target_cpu); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 141 | |
| 142 | spin_lock(&queue->parallel.lock); |
| 143 | list_add_tail(&padata->list, &queue->parallel.list); |
| 144 | spin_unlock(&queue->parallel.lock); |
| 145 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 146 | queue_work_on(target_cpu, pinst->wq, &queue->work); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 147 | |
| 148 | out: |
| 149 | rcu_read_unlock_bh(); |
| 150 | |
| 151 | return err; |
| 152 | } |
| 153 | EXPORT_SYMBOL(padata_do_parallel); |
| 154 | |
Steffen Klassert | 0198ffd | 2010-05-19 13:44:27 +1000 | [diff] [blame] | 155 | /* |
| 156 | * padata_get_next - Get the next object that needs serialization. |
| 157 | * |
| 158 | * Return values are: |
| 159 | * |
| 160 | * A pointer to the control struct of the next object that needs |
| 161 | * serialization, if present in one of the percpu reorder queues. |
| 162 | * |
| 163 | * NULL, if all percpu reorder queues are empty. |
| 164 | * |
| 165 | * -EINPROGRESS, if the next object that needs serialization will |
| 166 | * be parallel processed by another cpu and is not yet present in |
| 167 | * the cpu's reorder queue. |
| 168 | * |
| 169 | * -ENODATA, if this cpu has to do the parallel processing for |
| 170 | * the next object. |
| 171 | */ |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 172 | static struct padata_priv *padata_get_next(struct parallel_data *pd) |
| 173 | { |
Steffen Klassert | 5f1a8c1 | 2010-07-07 15:32:39 +0200 | [diff] [blame] | 174 | int cpu, num_cpus; |
Steffen Klassert | 2dc9b5d | 2012-03-09 07:20:49 +0100 | [diff] [blame] | 175 | unsigned int next_nr, next_index; |
Shan Wei | f0fcf20 | 2012-12-06 17:16:23 +0800 | [diff] [blame] | 176 | struct padata_parallel_queue *next_queue; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 177 | struct padata_priv *padata; |
| 178 | struct padata_list *reorder; |
| 179 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 180 | num_cpus = cpumask_weight(pd->cpumask.pcpu); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 181 | |
Steffen Klassert | 5f1a8c1 | 2010-07-07 15:32:39 +0200 | [diff] [blame] | 182 | /* |
| 183 | * Calculate the percpu reorder queue and the sequence |
| 184 | * number of the next object. |
| 185 | */ |
| 186 | next_nr = pd->processed; |
| 187 | next_index = next_nr % num_cpus; |
| 188 | cpu = padata_index_to_cpu(pd, next_index); |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 189 | next_queue = per_cpu_ptr(pd->pqueue, cpu); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 190 | |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 191 | padata = NULL; |
| 192 | |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 193 | reorder = &next_queue->reorder; |
| 194 | |
Jason A. Donenfeld | eb8c62a | 2017-03-23 12:24:43 +0100 | [diff] [blame] | 195 | spin_lock(&reorder->lock); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 196 | if (!list_empty(&reorder->list)) { |
| 197 | padata = list_entry(reorder->list.next, |
| 198 | struct padata_priv, list); |
| 199 | |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 200 | list_del_init(&padata->list); |
| 201 | atomic_dec(&pd->reorder_objects); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 202 | |
Steffen Klassert | 5f1a8c1 | 2010-07-07 15:32:39 +0200 | [diff] [blame] | 203 | pd->processed++; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 204 | |
Jason A. Donenfeld | eb8c62a | 2017-03-23 12:24:43 +0100 | [diff] [blame] | 205 | spin_unlock(&reorder->lock); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 206 | goto out; |
| 207 | } |
Jason A. Donenfeld | eb8c62a | 2017-03-23 12:24:43 +0100 | [diff] [blame] | 208 | spin_unlock(&reorder->lock); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 209 | |
Shan Wei | f0fcf20 | 2012-12-06 17:16:23 +0800 | [diff] [blame] | 210 | if (__this_cpu_read(pd->pqueue->cpu_index) == next_queue->cpu_index) { |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 211 | padata = ERR_PTR(-ENODATA); |
| 212 | goto out; |
| 213 | } |
| 214 | |
| 215 | padata = ERR_PTR(-EINPROGRESS); |
| 216 | out: |
| 217 | return padata; |
| 218 | } |
| 219 | |
| 220 | static void padata_reorder(struct parallel_data *pd) |
| 221 | { |
Steffen Klassert | 3047817 | 2012-03-09 07:20:12 +0100 | [diff] [blame] | 222 | int cb_cpu; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 223 | struct padata_priv *padata; |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 224 | struct padata_serial_queue *squeue; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 225 | struct padata_instance *pinst = pd->pinst; |
| 226 | |
Steffen Klassert | 0198ffd | 2010-05-19 13:44:27 +1000 | [diff] [blame] | 227 | /* |
| 228 | * We need to ensure that only one cpu can work on dequeueing of |
| 229 | * the reorder queue the time. Calculating in which percpu reorder |
| 230 | * queue the next object will arrive takes some time. A spinlock |
| 231 | * would be highly contended. Also it is not clear in which order |
| 232 | * the objects arrive to the reorder queues. So a cpu could wait to |
| 233 | * get the lock just to notice that there is nothing to do at the |
| 234 | * moment. Therefore we use a trylock and let the holder of the lock |
| 235 | * care for all the objects enqueued during the holdtime of the lock. |
| 236 | */ |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 237 | if (!spin_trylock_bh(&pd->lock)) |
Steffen Klassert | d46a5ac | 2010-05-19 13:43:14 +1000 | [diff] [blame] | 238 | return; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 239 | |
| 240 | while (1) { |
| 241 | padata = padata_get_next(pd); |
| 242 | |
Steffen Klassert | 0198ffd | 2010-05-19 13:44:27 +1000 | [diff] [blame] | 243 | /* |
| 244 | * All reorder queues are empty, or the next object that needs |
| 245 | * serialization is parallel processed by another cpu and is |
| 246 | * still on it's way to the cpu's reorder queue, nothing to |
| 247 | * do for now. |
| 248 | */ |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 249 | if (!padata || PTR_ERR(padata) == -EINPROGRESS) |
| 250 | break; |
| 251 | |
Steffen Klassert | 0198ffd | 2010-05-19 13:44:27 +1000 | [diff] [blame] | 252 | /* |
| 253 | * This cpu has to do the parallel processing of the next |
| 254 | * object. It's waiting in the cpu's parallelization queue, |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 255 | * so exit immediately. |
Steffen Klassert | 0198ffd | 2010-05-19 13:44:27 +1000 | [diff] [blame] | 256 | */ |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 257 | if (PTR_ERR(padata) == -ENODATA) { |
Steffen Klassert | d46a5ac | 2010-05-19 13:43:14 +1000 | [diff] [blame] | 258 | del_timer(&pd->timer); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 259 | spin_unlock_bh(&pd->lock); |
Steffen Klassert | d46a5ac | 2010-05-19 13:43:14 +1000 | [diff] [blame] | 260 | return; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 261 | } |
| 262 | |
Steffen Klassert | 3047817 | 2012-03-09 07:20:12 +0100 | [diff] [blame] | 263 | cb_cpu = padata->cb_cpu; |
| 264 | squeue = per_cpu_ptr(pd->squeue, cb_cpu); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 265 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 266 | spin_lock(&squeue->serial.lock); |
| 267 | list_add_tail(&padata->list, &squeue->serial.list); |
| 268 | spin_unlock(&squeue->serial.lock); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 269 | |
Steffen Klassert | 3047817 | 2012-03-09 07:20:12 +0100 | [diff] [blame] | 270 | queue_work_on(cb_cpu, pinst->wq, &squeue->work); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | spin_unlock_bh(&pd->lock); |
| 274 | |
Steffen Klassert | 0198ffd | 2010-05-19 13:44:27 +1000 | [diff] [blame] | 275 | /* |
| 276 | * The next object that needs serialization might have arrived to |
| 277 | * the reorder queues in the meantime, we will be called again |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 278 | * from the timer function if no one else cares for it. |
Daniel Jordan | 4b414f5 | 2019-07-16 12:32:53 -0400 | [diff] [blame] | 279 | * |
| 280 | * Ensure reorder_objects is read after pd->lock is dropped so we see |
| 281 | * an increment from another task in padata_do_serial. Pairs with |
| 282 | * smp_mb__after_atomic in padata_do_serial. |
Steffen Klassert | 0198ffd | 2010-05-19 13:44:27 +1000 | [diff] [blame] | 283 | */ |
Daniel Jordan | 4b414f5 | 2019-07-16 12:32:53 -0400 | [diff] [blame] | 284 | smp_mb(); |
Steffen Klassert | d46a5ac | 2010-05-19 13:43:14 +1000 | [diff] [blame] | 285 | if (atomic_read(&pd->reorder_objects) |
| 286 | && !(pinst->flags & PADATA_RESET)) |
| 287 | mod_timer(&pd->timer, jiffies + HZ); |
| 288 | else |
| 289 | del_timer(&pd->timer); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 290 | |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 291 | return; |
| 292 | } |
| 293 | |
Steffen Klassert | d46a5ac | 2010-05-19 13:43:14 +1000 | [diff] [blame] | 294 | static void padata_reorder_timer(unsigned long arg) |
| 295 | { |
| 296 | struct parallel_data *pd = (struct parallel_data *)arg; |
| 297 | |
| 298 | padata_reorder(pd); |
| 299 | } |
| 300 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 301 | static void padata_serial_worker(struct work_struct *serial_work) |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 302 | { |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 303 | struct padata_serial_queue *squeue; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 304 | struct parallel_data *pd; |
| 305 | LIST_HEAD(local_list); |
Herbert Xu | 928cf3d | 2019-11-19 13:17:31 +0800 | [diff] [blame] | 306 | int cnt; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 307 | |
| 308 | local_bh_disable(); |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 309 | squeue = container_of(serial_work, struct padata_serial_queue, work); |
| 310 | pd = squeue->pd; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 311 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 312 | spin_lock(&squeue->serial.lock); |
| 313 | list_replace_init(&squeue->serial.list, &local_list); |
| 314 | spin_unlock(&squeue->serial.lock); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 315 | |
Herbert Xu | 928cf3d | 2019-11-19 13:17:31 +0800 | [diff] [blame] | 316 | cnt = 0; |
| 317 | |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 318 | while (!list_empty(&local_list)) { |
| 319 | struct padata_priv *padata; |
| 320 | |
| 321 | padata = list_entry(local_list.next, |
| 322 | struct padata_priv, list); |
| 323 | |
| 324 | list_del_init(&padata->list); |
| 325 | |
| 326 | padata->serial(padata); |
Herbert Xu | 928cf3d | 2019-11-19 13:17:31 +0800 | [diff] [blame] | 327 | cnt++; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 328 | } |
| 329 | local_bh_enable(); |
Herbert Xu | 928cf3d | 2019-11-19 13:17:31 +0800 | [diff] [blame] | 330 | |
| 331 | if (atomic_sub_and_test(cnt, &pd->refcnt)) |
| 332 | padata_free_pd(pd); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 333 | } |
| 334 | |
Steffen Klassert | 0198ffd | 2010-05-19 13:44:27 +1000 | [diff] [blame] | 335 | /** |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 336 | * padata_do_serial - padata serialization function |
| 337 | * |
| 338 | * @padata: object to be serialized. |
| 339 | * |
| 340 | * padata_do_serial must be called for every parallelized object. |
| 341 | * The serialization callback function will run with BHs off. |
| 342 | */ |
| 343 | void padata_do_serial(struct padata_priv *padata) |
| 344 | { |
| 345 | int cpu; |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 346 | struct padata_parallel_queue *pqueue; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 347 | struct parallel_data *pd; |
| 348 | |
| 349 | pd = padata->pd; |
| 350 | |
| 351 | cpu = get_cpu(); |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 352 | pqueue = per_cpu_ptr(pd->pqueue, cpu); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 353 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 354 | spin_lock(&pqueue->reorder.lock); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 355 | atomic_inc(&pd->reorder_objects); |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 356 | list_add_tail(&padata->list, &pqueue->reorder.list); |
| 357 | spin_unlock(&pqueue->reorder.lock); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 358 | |
Daniel Jordan | 4b414f5 | 2019-07-16 12:32:53 -0400 | [diff] [blame] | 359 | /* |
| 360 | * Ensure the atomic_inc of reorder_objects above is ordered correctly |
| 361 | * with the trylock of pd->lock in padata_reorder. Pairs with smp_mb |
| 362 | * in padata_reorder. |
| 363 | */ |
| 364 | smp_mb__after_atomic(); |
| 365 | |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 366 | put_cpu(); |
| 367 | |
| 368 | padata_reorder(pd); |
| 369 | } |
| 370 | EXPORT_SYMBOL(padata_do_serial); |
| 371 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 372 | static int padata_setup_cpumasks(struct parallel_data *pd, |
| 373 | const struct cpumask *pcpumask, |
| 374 | const struct cpumask *cbcpumask) |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 375 | { |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 376 | if (!alloc_cpumask_var(&pd->cpumask.pcpu, GFP_KERNEL)) |
| 377 | return -ENOMEM; |
| 378 | |
Steffen Klassert | 13614e0 | 2012-03-28 08:43:21 +0200 | [diff] [blame] | 379 | cpumask_and(pd->cpumask.pcpu, pcpumask, cpu_online_mask); |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 380 | if (!alloc_cpumask_var(&pd->cpumask.cbcpu, GFP_KERNEL)) { |
Jason A. Donenfeld | 6240377 | 2017-04-07 02:33:30 +0200 | [diff] [blame] | 381 | free_cpumask_var(pd->cpumask.pcpu); |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 382 | return -ENOMEM; |
| 383 | } |
| 384 | |
Steffen Klassert | 13614e0 | 2012-03-28 08:43:21 +0200 | [diff] [blame] | 385 | cpumask_and(pd->cpumask.cbcpu, cbcpumask, cpu_online_mask); |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 386 | return 0; |
| 387 | } |
| 388 | |
| 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 | } |
| 394 | |
| 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; |
| 400 | |
| 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 | } |
| 408 | |
| 409 | /* Initialize all percpu queues used by parallel workers */ |
| 410 | static void padata_init_pqueues(struct parallel_data *pd) |
| 411 | { |
Steffen Klassert | 2dc9b5d | 2012-03-09 07:20:49 +0100 | [diff] [blame] | 412 | int cpu_index, cpu; |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 413 | struct padata_parallel_queue *pqueue; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 414 | |
| 415 | cpu_index = 0; |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 416 | for_each_cpu(cpu, pd->cpumask.pcpu) { |
| 417 | pqueue = per_cpu_ptr(pd->pqueue, cpu); |
| 418 | pqueue->pd = pd; |
| 419 | pqueue->cpu_index = cpu_index; |
Steffen Klassert | fad3a90 | 2010-07-20 08:48:34 +0200 | [diff] [blame] | 420 | cpu_index++; |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 421 | |
| 422 | __padata_list_init(&pqueue->reorder); |
| 423 | __padata_list_init(&pqueue->parallel); |
| 424 | INIT_WORK(&pqueue->work, padata_parallel_worker); |
| 425 | atomic_set(&pqueue->num_obj, 0); |
| 426 | } |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | /* Allocate and initialize the internal cpumask dependend resources. */ |
| 430 | static struct parallel_data *padata_alloc_pd(struct padata_instance *pinst, |
| 431 | const struct cpumask *pcpumask, |
| 432 | const struct cpumask *cbcpumask) |
| 433 | { |
| 434 | struct parallel_data *pd; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 435 | |
| 436 | pd = kzalloc(sizeof(struct parallel_data), GFP_KERNEL); |
| 437 | if (!pd) |
| 438 | goto err; |
| 439 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 440 | pd->pqueue = alloc_percpu(struct padata_parallel_queue); |
| 441 | if (!pd->pqueue) |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 442 | goto err_free_pd; |
| 443 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 444 | pd->squeue = alloc_percpu(struct padata_serial_queue); |
| 445 | if (!pd->squeue) |
| 446 | goto err_free_pqueue; |
| 447 | if (padata_setup_cpumasks(pd, pcpumask, cbcpumask) < 0) |
| 448 | goto err_free_squeue; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 449 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 450 | padata_init_pqueues(pd); |
| 451 | padata_init_squeues(pd); |
Steffen Klassert | d46a5ac | 2010-05-19 13:43:14 +1000 | [diff] [blame] | 452 | setup_timer(&pd->timer, padata_reorder_timer, (unsigned long)pd); |
Mathias Krause | 0b6b098 | 2013-10-25 12:14:15 +0200 | [diff] [blame] | 453 | atomic_set(&pd->seq_nr, -1); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 454 | atomic_set(&pd->reorder_objects, 0); |
Herbert Xu | 928cf3d | 2019-11-19 13:17:31 +0800 | [diff] [blame] | 455 | atomic_set(&pd->refcnt, 1); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 456 | pd->pinst = pinst; |
| 457 | spin_lock_init(&pd->lock); |
| 458 | |
| 459 | return pd; |
| 460 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 461 | err_free_squeue: |
| 462 | free_percpu(pd->squeue); |
| 463 | err_free_pqueue: |
| 464 | free_percpu(pd->pqueue); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 465 | err_free_pd: |
| 466 | kfree(pd); |
| 467 | err: |
| 468 | return NULL; |
| 469 | } |
| 470 | |
| 471 | static void padata_free_pd(struct parallel_data *pd) |
| 472 | { |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 473 | free_cpumask_var(pd->cpumask.pcpu); |
| 474 | free_cpumask_var(pd->cpumask.cbcpu); |
| 475 | free_percpu(pd->pqueue); |
| 476 | free_percpu(pd->squeue); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 477 | kfree(pd); |
| 478 | } |
| 479 | |
Steffen Klassert | 4c87917 | 2010-07-07 15:30:10 +0200 | [diff] [blame] | 480 | static void __padata_start(struct padata_instance *pinst) |
| 481 | { |
| 482 | pinst->flags |= PADATA_INIT; |
| 483 | } |
| 484 | |
Steffen Klassert | ee83655 | 2010-07-07 15:30:47 +0200 | [diff] [blame] | 485 | static void __padata_stop(struct padata_instance *pinst) |
| 486 | { |
| 487 | if (!(pinst->flags & PADATA_INIT)) |
| 488 | return; |
| 489 | |
| 490 | pinst->flags &= ~PADATA_INIT; |
| 491 | |
| 492 | synchronize_rcu(); |
Steffen Klassert | ee83655 | 2010-07-07 15:30:47 +0200 | [diff] [blame] | 493 | } |
| 494 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 495 | /* Replace the internal control structure with a new one. */ |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 496 | static void padata_replace(struct padata_instance *pinst, |
| 497 | struct parallel_data *pd_new) |
| 498 | { |
| 499 | struct parallel_data *pd_old = pinst->pd; |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 500 | int notification_mask = 0; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 501 | |
| 502 | pinst->flags |= PADATA_RESET; |
| 503 | |
| 504 | rcu_assign_pointer(pinst->pd, pd_new); |
| 505 | |
| 506 | synchronize_rcu(); |
| 507 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 508 | if (!cpumask_equal(pd_old->cpumask.pcpu, pd_new->cpumask.pcpu)) |
| 509 | notification_mask |= PADATA_CPU_PARALLEL; |
| 510 | if (!cpumask_equal(pd_old->cpumask.cbcpu, pd_new->cpumask.cbcpu)) |
| 511 | notification_mask |= PADATA_CPU_SERIAL; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 512 | |
Herbert Xu | 928cf3d | 2019-11-19 13:17:31 +0800 | [diff] [blame] | 513 | if (atomic_dec_and_test(&pd_old->refcnt)) |
| 514 | padata_free_pd(pd_old); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 515 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 516 | if (notification_mask) |
| 517 | blocking_notifier_call_chain(&pinst->cpumask_change_notifier, |
Steffen Klassert | c635696 | 2010-07-27 07:15:50 +0200 | [diff] [blame] | 518 | notification_mask, |
| 519 | &pd_new->cpumask); |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 520 | |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 521 | pinst->flags &= ~PADATA_RESET; |
| 522 | } |
| 523 | |
Steffen Klassert | 0198ffd | 2010-05-19 13:44:27 +1000 | [diff] [blame] | 524 | /** |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 525 | * padata_register_cpumask_notifier - Registers a notifier that will be called |
| 526 | * if either pcpu or cbcpu or both cpumasks change. |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 527 | * |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 528 | * @pinst: A poineter to padata instance |
| 529 | * @nblock: A pointer to notifier block. |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 530 | */ |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 531 | int padata_register_cpumask_notifier(struct padata_instance *pinst, |
| 532 | struct notifier_block *nblock) |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 533 | { |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 534 | return blocking_notifier_chain_register(&pinst->cpumask_change_notifier, |
| 535 | nblock); |
| 536 | } |
| 537 | EXPORT_SYMBOL(padata_register_cpumask_notifier); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 538 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 539 | /** |
| 540 | * padata_unregister_cpumask_notifier - Unregisters cpumask notifier |
| 541 | * registered earlier using padata_register_cpumask_notifier |
| 542 | * |
| 543 | * @pinst: A pointer to data instance. |
| 544 | * @nlock: A pointer to notifier block. |
| 545 | */ |
| 546 | int padata_unregister_cpumask_notifier(struct padata_instance *pinst, |
| 547 | struct notifier_block *nblock) |
| 548 | { |
| 549 | return blocking_notifier_chain_unregister( |
| 550 | &pinst->cpumask_change_notifier, |
| 551 | nblock); |
| 552 | } |
| 553 | EXPORT_SYMBOL(padata_unregister_cpumask_notifier); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 554 | |
Steffen Klassert | 6751fb3 | 2010-04-29 14:42:30 +0200 | [diff] [blame] | 555 | |
Steffen Klassert | 33e5445 | 2010-07-07 15:31:26 +0200 | [diff] [blame] | 556 | /* If cpumask contains no active cpu, we mark the instance as invalid. */ |
| 557 | static bool padata_validate_cpumask(struct padata_instance *pinst, |
| 558 | const struct cpumask *cpumask) |
| 559 | { |
Steffen Klassert | 13614e0 | 2012-03-28 08:43:21 +0200 | [diff] [blame] | 560 | if (!cpumask_intersects(cpumask, cpu_online_mask)) { |
Steffen Klassert | 33e5445 | 2010-07-07 15:31:26 +0200 | [diff] [blame] | 561 | pinst->flags |= PADATA_INVALID; |
| 562 | return false; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 563 | } |
| 564 | |
Steffen Klassert | 33e5445 | 2010-07-07 15:31:26 +0200 | [diff] [blame] | 565 | pinst->flags &= ~PADATA_INVALID; |
| 566 | return true; |
| 567 | } |
| 568 | |
Steffen Klassert | 65ff577 | 2010-07-27 07:15:06 +0200 | [diff] [blame] | 569 | static int __padata_set_cpumasks(struct padata_instance *pinst, |
| 570 | cpumask_var_t pcpumask, |
| 571 | cpumask_var_t cbcpumask) |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 572 | { |
Steffen Klassert | 33e5445 | 2010-07-07 15:31:26 +0200 | [diff] [blame] | 573 | int valid; |
Steffen Klassert | 65ff577 | 2010-07-27 07:15:06 +0200 | [diff] [blame] | 574 | struct parallel_data *pd; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 575 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 576 | valid = padata_validate_cpumask(pinst, pcpumask); |
| 577 | if (!valid) { |
| 578 | __padata_stop(pinst); |
| 579 | goto out_replace; |
| 580 | } |
| 581 | |
| 582 | valid = padata_validate_cpumask(pinst, cbcpumask); |
Steffen Klassert | b89661d | 2010-07-20 08:49:20 +0200 | [diff] [blame] | 583 | if (!valid) |
Steffen Klassert | 33e5445 | 2010-07-07 15:31:26 +0200 | [diff] [blame] | 584 | __padata_stop(pinst); |
Steffen Klassert | 33e5445 | 2010-07-07 15:31:26 +0200 | [diff] [blame] | 585 | |
Steffen Klassert | b89661d | 2010-07-20 08:49:20 +0200 | [diff] [blame] | 586 | out_replace: |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 587 | pd = padata_alloc_pd(pinst, pcpumask, cbcpumask); |
Steffen Klassert | 65ff577 | 2010-07-27 07:15:06 +0200 | [diff] [blame] | 588 | if (!pd) |
| 589 | return -ENOMEM; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 590 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 591 | cpumask_copy(pinst->cpumask.pcpu, pcpumask); |
| 592 | cpumask_copy(pinst->cpumask.cbcpu, cbcpumask); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 593 | |
| 594 | padata_replace(pinst, pd); |
| 595 | |
Steffen Klassert | 33e5445 | 2010-07-07 15:31:26 +0200 | [diff] [blame] | 596 | if (valid) |
| 597 | __padata_start(pinst); |
| 598 | |
Steffen Klassert | 65ff577 | 2010-07-27 07:15:06 +0200 | [diff] [blame] | 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | /** |
Steffen Klassert | 65ff577 | 2010-07-27 07:15:06 +0200 | [diff] [blame] | 603 | * padata_set_cpumask: Sets specified by @cpumask_type cpumask to the value |
| 604 | * equivalent to @cpumask. |
| 605 | * |
| 606 | * @pinst: padata instance |
| 607 | * @cpumask_type: PADATA_CPU_SERIAL or PADATA_CPU_PARALLEL corresponding |
| 608 | * to parallel and serial cpumasks respectively. |
| 609 | * @cpumask: the cpumask to use |
| 610 | */ |
| 611 | int padata_set_cpumask(struct padata_instance *pinst, int cpumask_type, |
| 612 | cpumask_var_t cpumask) |
| 613 | { |
| 614 | struct cpumask *serial_mask, *parallel_mask; |
| 615 | int err = -EINVAL; |
| 616 | |
| 617 | mutex_lock(&pinst->lock); |
| 618 | get_online_cpus(); |
| 619 | |
| 620 | switch (cpumask_type) { |
| 621 | case PADATA_CPU_PARALLEL: |
| 622 | serial_mask = pinst->cpumask.cbcpu; |
| 623 | parallel_mask = cpumask; |
| 624 | break; |
| 625 | case PADATA_CPU_SERIAL: |
| 626 | parallel_mask = pinst->cpumask.pcpu; |
| 627 | serial_mask = cpumask; |
| 628 | break; |
| 629 | default: |
| 630 | goto out; |
| 631 | } |
| 632 | |
| 633 | err = __padata_set_cpumasks(pinst, parallel_mask, serial_mask); |
| 634 | |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 635 | out: |
Steffen Klassert | 6751fb3 | 2010-04-29 14:42:30 +0200 | [diff] [blame] | 636 | put_online_cpus(); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 637 | mutex_unlock(&pinst->lock); |
| 638 | |
| 639 | return err; |
| 640 | } |
| 641 | EXPORT_SYMBOL(padata_set_cpumask); |
| 642 | |
Arnd Bergmann | 19d795b | 2016-05-19 17:09:59 -0700 | [diff] [blame] | 643 | /** |
| 644 | * padata_start - start the parallel processing |
| 645 | * |
| 646 | * @pinst: padata instance to start |
| 647 | */ |
| 648 | int padata_start(struct padata_instance *pinst) |
| 649 | { |
| 650 | int err = 0; |
| 651 | |
| 652 | mutex_lock(&pinst->lock); |
| 653 | |
| 654 | if (pinst->flags & PADATA_INVALID) |
| 655 | err = -EINVAL; |
| 656 | |
| 657 | __padata_start(pinst); |
| 658 | |
| 659 | mutex_unlock(&pinst->lock); |
| 660 | |
| 661 | return err; |
| 662 | } |
| 663 | EXPORT_SYMBOL(padata_start); |
| 664 | |
| 665 | /** |
| 666 | * padata_stop - stop the parallel processing |
| 667 | * |
| 668 | * @pinst: padata instance to stop |
| 669 | */ |
| 670 | void padata_stop(struct padata_instance *pinst) |
| 671 | { |
| 672 | mutex_lock(&pinst->lock); |
| 673 | __padata_stop(pinst); |
| 674 | mutex_unlock(&pinst->lock); |
| 675 | } |
| 676 | EXPORT_SYMBOL(padata_stop); |
| 677 | |
| 678 | #ifdef CONFIG_HOTPLUG_CPU |
| 679 | |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 680 | static int __padata_add_cpu(struct padata_instance *pinst, int cpu) |
| 681 | { |
| 682 | struct parallel_data *pd; |
| 683 | |
Steffen Klassert | 13614e0 | 2012-03-28 08:43:21 +0200 | [diff] [blame] | 684 | if (cpumask_test_cpu(cpu, cpu_online_mask)) { |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 685 | pd = padata_alloc_pd(pinst, pinst->cpumask.pcpu, |
| 686 | pinst->cpumask.cbcpu); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 687 | if (!pd) |
| 688 | return -ENOMEM; |
| 689 | |
| 690 | padata_replace(pinst, pd); |
Steffen Klassert | 33e5445 | 2010-07-07 15:31:26 +0200 | [diff] [blame] | 691 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 692 | if (padata_validate_cpumask(pinst, pinst->cpumask.pcpu) && |
| 693 | padata_validate_cpumask(pinst, pinst->cpumask.cbcpu)) |
Steffen Klassert | 33e5445 | 2010-07-07 15:31:26 +0200 | [diff] [blame] | 694 | __padata_start(pinst); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | return 0; |
| 698 | } |
| 699 | |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 700 | static int __padata_remove_cpu(struct padata_instance *pinst, int cpu) |
| 701 | { |
Steffen Klassert | 33e5445 | 2010-07-07 15:31:26 +0200 | [diff] [blame] | 702 | struct parallel_data *pd = NULL; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 703 | |
| 704 | if (cpumask_test_cpu(cpu, cpu_online_mask)) { |
Steffen Klassert | 33e5445 | 2010-07-07 15:31:26 +0200 | [diff] [blame] | 705 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 706 | if (!padata_validate_cpumask(pinst, pinst->cpumask.pcpu) || |
Steffen Klassert | b89661d | 2010-07-20 08:49:20 +0200 | [diff] [blame] | 707 | !padata_validate_cpumask(pinst, pinst->cpumask.cbcpu)) |
Steffen Klassert | 33e5445 | 2010-07-07 15:31:26 +0200 | [diff] [blame] | 708 | __padata_stop(pinst); |
Steffen Klassert | 33e5445 | 2010-07-07 15:31:26 +0200 | [diff] [blame] | 709 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 710 | pd = padata_alloc_pd(pinst, pinst->cpumask.pcpu, |
| 711 | pinst->cpumask.cbcpu); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 712 | if (!pd) |
| 713 | return -ENOMEM; |
| 714 | |
| 715 | padata_replace(pinst, pd); |
Steffen Klassert | 9612090 | 2012-03-28 08:44:07 +0200 | [diff] [blame] | 716 | |
| 717 | cpumask_clear_cpu(cpu, pd->cpumask.cbcpu); |
| 718 | cpumask_clear_cpu(cpu, pd->cpumask.pcpu); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | return 0; |
| 722 | } |
| 723 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 724 | /** |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 725 | * 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] | 726 | * padata cpumasks. |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 727 | * |
| 728 | * @pinst: padata instance |
| 729 | * @cpu: cpu to remove |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 730 | * @mask: bitmask specifying from which cpumask @cpu should be removed |
| 731 | * The @mask may be any combination of the following flags: |
| 732 | * PADATA_CPU_SERIAL - serial cpumask |
| 733 | * PADATA_CPU_PARALLEL - parallel cpumask |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 734 | */ |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 735 | int padata_remove_cpu(struct padata_instance *pinst, int cpu, int mask) |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 736 | { |
| 737 | int err; |
| 738 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 739 | if (!(mask & (PADATA_CPU_SERIAL | PADATA_CPU_PARALLEL))) |
| 740 | return -EINVAL; |
| 741 | |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 742 | mutex_lock(&pinst->lock); |
| 743 | |
Steffen Klassert | 6751fb3 | 2010-04-29 14:42:30 +0200 | [diff] [blame] | 744 | get_online_cpus(); |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 745 | if (mask & PADATA_CPU_SERIAL) |
| 746 | cpumask_clear_cpu(cpu, pinst->cpumask.cbcpu); |
| 747 | if (mask & PADATA_CPU_PARALLEL) |
| 748 | cpumask_clear_cpu(cpu, pinst->cpumask.pcpu); |
| 749 | |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 750 | err = __padata_remove_cpu(pinst, cpu); |
Steffen Klassert | 6751fb3 | 2010-04-29 14:42:30 +0200 | [diff] [blame] | 751 | put_online_cpus(); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 752 | |
| 753 | mutex_unlock(&pinst->lock); |
| 754 | |
| 755 | return err; |
| 756 | } |
| 757 | EXPORT_SYMBOL(padata_remove_cpu); |
| 758 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 759 | static inline int pinst_has_cpu(struct padata_instance *pinst, int cpu) |
| 760 | { |
| 761 | return cpumask_test_cpu(cpu, pinst->cpumask.pcpu) || |
| 762 | cpumask_test_cpu(cpu, pinst->cpumask.cbcpu); |
| 763 | } |
| 764 | |
Sebastian Andrzej Siewior | 30e9215 | 2016-09-06 19:04:49 +0200 | [diff] [blame] | 765 | static int padata_cpu_online(unsigned int cpu, struct hlist_node *node) |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 766 | { |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 767 | struct padata_instance *pinst; |
Sebastian Andrzej Siewior | 30e9215 | 2016-09-06 19:04:49 +0200 | [diff] [blame] | 768 | int ret; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 769 | |
Sebastian Andrzej Siewior | 30e9215 | 2016-09-06 19:04:49 +0200 | [diff] [blame] | 770 | pinst = hlist_entry_safe(node, struct padata_instance, node); |
| 771 | if (!pinst_has_cpu(pinst, cpu)) |
| 772 | return 0; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 773 | |
Sebastian Andrzej Siewior | 30e9215 | 2016-09-06 19:04:49 +0200 | [diff] [blame] | 774 | mutex_lock(&pinst->lock); |
| 775 | ret = __padata_add_cpu(pinst, cpu); |
| 776 | mutex_unlock(&pinst->lock); |
| 777 | return ret; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 778 | } |
Sebastian Andrzej Siewior | 30e9215 | 2016-09-06 19:04:49 +0200 | [diff] [blame] | 779 | |
| 780 | static int padata_cpu_prep_down(unsigned int cpu, struct hlist_node *node) |
| 781 | { |
| 782 | struct padata_instance *pinst; |
| 783 | int ret; |
| 784 | |
| 785 | pinst = hlist_entry_safe(node, struct padata_instance, node); |
| 786 | if (!pinst_has_cpu(pinst, cpu)) |
| 787 | return 0; |
| 788 | |
| 789 | mutex_lock(&pinst->lock); |
| 790 | ret = __padata_remove_cpu(pinst, cpu); |
| 791 | mutex_unlock(&pinst->lock); |
| 792 | return ret; |
| 793 | } |
| 794 | |
| 795 | static enum cpuhp_state hp_online; |
Steffen Klassert | e2cb2f1 | 2010-04-29 14:40:10 +0200 | [diff] [blame] | 796 | #endif |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 797 | |
Dan Kruchinin | 5e017dc | 2010-07-14 14:33:08 +0400 | [diff] [blame] | 798 | static void __padata_free(struct padata_instance *pinst) |
| 799 | { |
| 800 | #ifdef CONFIG_HOTPLUG_CPU |
Sebastian Andrzej Siewior | 30e9215 | 2016-09-06 19:04:49 +0200 | [diff] [blame] | 801 | cpuhp_state_remove_instance_nocalls(hp_online, &pinst->node); |
Dan Kruchinin | 5e017dc | 2010-07-14 14:33:08 +0400 | [diff] [blame] | 802 | #endif |
| 803 | |
| 804 | padata_stop(pinst); |
| 805 | padata_free_pd(pinst->pd); |
| 806 | free_cpumask_var(pinst->cpumask.pcpu); |
| 807 | free_cpumask_var(pinst->cpumask.cbcpu); |
| 808 | kfree(pinst); |
| 809 | } |
| 810 | |
| 811 | #define kobj2pinst(_kobj) \ |
| 812 | container_of(_kobj, struct padata_instance, kobj) |
| 813 | #define attr2pentry(_attr) \ |
| 814 | container_of(_attr, struct padata_sysfs_entry, attr) |
| 815 | |
| 816 | static void padata_sysfs_release(struct kobject *kobj) |
| 817 | { |
| 818 | struct padata_instance *pinst = kobj2pinst(kobj); |
| 819 | __padata_free(pinst); |
| 820 | } |
| 821 | |
| 822 | struct padata_sysfs_entry { |
| 823 | struct attribute attr; |
| 824 | ssize_t (*show)(struct padata_instance *, struct attribute *, char *); |
| 825 | ssize_t (*store)(struct padata_instance *, struct attribute *, |
| 826 | const char *, size_t); |
| 827 | }; |
| 828 | |
| 829 | static ssize_t show_cpumask(struct padata_instance *pinst, |
| 830 | struct attribute *attr, char *buf) |
| 831 | { |
| 832 | struct cpumask *cpumask; |
| 833 | ssize_t len; |
| 834 | |
| 835 | mutex_lock(&pinst->lock); |
| 836 | if (!strcmp(attr->name, "serial_cpumask")) |
| 837 | cpumask = pinst->cpumask.cbcpu; |
| 838 | else |
| 839 | cpumask = pinst->cpumask.pcpu; |
| 840 | |
Tejun Heo | 4497da6 | 2015-02-13 14:38:05 -0800 | [diff] [blame] | 841 | len = snprintf(buf, PAGE_SIZE, "%*pb\n", |
| 842 | nr_cpu_ids, cpumask_bits(cpumask)); |
Dan Kruchinin | 5e017dc | 2010-07-14 14:33:08 +0400 | [diff] [blame] | 843 | mutex_unlock(&pinst->lock); |
Tejun Heo | 4497da6 | 2015-02-13 14:38:05 -0800 | [diff] [blame] | 844 | return len < PAGE_SIZE ? len : -EINVAL; |
Dan Kruchinin | 5e017dc | 2010-07-14 14:33:08 +0400 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | static ssize_t store_cpumask(struct padata_instance *pinst, |
| 848 | struct attribute *attr, |
| 849 | const char *buf, size_t count) |
| 850 | { |
| 851 | cpumask_var_t new_cpumask; |
| 852 | ssize_t ret; |
| 853 | int mask_type; |
| 854 | |
| 855 | if (!alloc_cpumask_var(&new_cpumask, GFP_KERNEL)) |
| 856 | return -ENOMEM; |
| 857 | |
| 858 | ret = bitmap_parse(buf, count, cpumask_bits(new_cpumask), |
| 859 | nr_cpumask_bits); |
| 860 | if (ret < 0) |
| 861 | goto out; |
| 862 | |
| 863 | mask_type = !strcmp(attr->name, "serial_cpumask") ? |
| 864 | PADATA_CPU_SERIAL : PADATA_CPU_PARALLEL; |
| 865 | ret = padata_set_cpumask(pinst, mask_type, new_cpumask); |
| 866 | if (!ret) |
| 867 | ret = count; |
| 868 | |
| 869 | out: |
| 870 | free_cpumask_var(new_cpumask); |
| 871 | return ret; |
| 872 | } |
| 873 | |
| 874 | #define PADATA_ATTR_RW(_name, _show_name, _store_name) \ |
| 875 | static struct padata_sysfs_entry _name##_attr = \ |
| 876 | __ATTR(_name, 0644, _show_name, _store_name) |
| 877 | #define PADATA_ATTR_RO(_name, _show_name) \ |
| 878 | static struct padata_sysfs_entry _name##_attr = \ |
| 879 | __ATTR(_name, 0400, _show_name, NULL) |
| 880 | |
| 881 | PADATA_ATTR_RW(serial_cpumask, show_cpumask, store_cpumask); |
| 882 | PADATA_ATTR_RW(parallel_cpumask, show_cpumask, store_cpumask); |
| 883 | |
| 884 | /* |
| 885 | * Padata sysfs provides the following objects: |
| 886 | * serial_cpumask [RW] - cpumask for serial workers |
| 887 | * parallel_cpumask [RW] - cpumask for parallel workers |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 888 | */ |
Dan Kruchinin | 5e017dc | 2010-07-14 14:33:08 +0400 | [diff] [blame] | 889 | static struct attribute *padata_default_attrs[] = { |
| 890 | &serial_cpumask_attr.attr, |
| 891 | ¶llel_cpumask_attr.attr, |
| 892 | NULL, |
| 893 | }; |
| 894 | |
| 895 | static ssize_t padata_sysfs_show(struct kobject *kobj, |
| 896 | struct attribute *attr, char *buf) |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 897 | { |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 898 | struct padata_instance *pinst; |
Dan Kruchinin | 5e017dc | 2010-07-14 14:33:08 +0400 | [diff] [blame] | 899 | struct padata_sysfs_entry *pentry; |
| 900 | ssize_t ret = -EIO; |
| 901 | |
| 902 | pinst = kobj2pinst(kobj); |
| 903 | pentry = attr2pentry(attr); |
| 904 | if (pentry->show) |
| 905 | ret = pentry->show(pinst, attr, buf); |
| 906 | |
| 907 | return ret; |
| 908 | } |
| 909 | |
| 910 | static ssize_t padata_sysfs_store(struct kobject *kobj, struct attribute *attr, |
| 911 | const char *buf, size_t count) |
| 912 | { |
| 913 | struct padata_instance *pinst; |
| 914 | struct padata_sysfs_entry *pentry; |
| 915 | ssize_t ret = -EIO; |
| 916 | |
| 917 | pinst = kobj2pinst(kobj); |
| 918 | pentry = attr2pentry(attr); |
| 919 | if (pentry->show) |
| 920 | ret = pentry->store(pinst, attr, buf, count); |
| 921 | |
| 922 | return ret; |
| 923 | } |
| 924 | |
| 925 | static const struct sysfs_ops padata_sysfs_ops = { |
| 926 | .show = padata_sysfs_show, |
| 927 | .store = padata_sysfs_store, |
| 928 | }; |
| 929 | |
| 930 | static struct kobj_type padata_attr_type = { |
| 931 | .sysfs_ops = &padata_sysfs_ops, |
| 932 | .default_attrs = padata_default_attrs, |
| 933 | .release = padata_sysfs_release, |
| 934 | }; |
| 935 | |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 936 | /** |
Steffen Klassert | e6cc117 | 2010-07-27 07:14:28 +0200 | [diff] [blame] | 937 | * padata_alloc_possible - Allocate and initialize padata instance. |
| 938 | * Use the cpu_possible_mask for serial and |
| 939 | * parallel workers. |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 940 | * |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 941 | * @wq: workqueue to use for the allocated padata instance |
| 942 | */ |
Steffen Klassert | e6cc117 | 2010-07-27 07:14:28 +0200 | [diff] [blame] | 943 | struct padata_instance *padata_alloc_possible(struct workqueue_struct *wq) |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 944 | { |
Steffen Klassert | e6cc117 | 2010-07-27 07:14:28 +0200 | [diff] [blame] | 945 | return padata_alloc(wq, cpu_possible_mask, cpu_possible_mask); |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 946 | } |
Steffen Klassert | e6cc117 | 2010-07-27 07:14:28 +0200 | [diff] [blame] | 947 | EXPORT_SYMBOL(padata_alloc_possible); |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 948 | |
| 949 | /** |
Steffen Klassert | e6cc117 | 2010-07-27 07:14:28 +0200 | [diff] [blame] | 950 | * padata_alloc - allocate and initialize a padata instance and specify |
| 951 | * cpumasks for serial and parallel workers. |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 952 | * |
| 953 | * @wq: workqueue to use for the allocated padata instance |
| 954 | * @pcpumask: cpumask that will be used for padata parallelization |
| 955 | * @cbcpumask: cpumask that will be used for padata serialization |
| 956 | */ |
Steffen Klassert | e6cc117 | 2010-07-27 07:14:28 +0200 | [diff] [blame] | 957 | struct padata_instance *padata_alloc(struct workqueue_struct *wq, |
| 958 | const struct cpumask *pcpumask, |
| 959 | const struct cpumask *cbcpumask) |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 960 | { |
| 961 | struct padata_instance *pinst; |
Steffen Klassert | 33e5445 | 2010-07-07 15:31:26 +0200 | [diff] [blame] | 962 | struct parallel_data *pd = NULL; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 963 | |
| 964 | pinst = kzalloc(sizeof(struct padata_instance), GFP_KERNEL); |
| 965 | if (!pinst) |
| 966 | goto err; |
| 967 | |
Steffen Klassert | 6751fb3 | 2010-04-29 14:42:30 +0200 | [diff] [blame] | 968 | get_online_cpus(); |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 969 | if (!alloc_cpumask_var(&pinst->cpumask.pcpu, GFP_KERNEL)) |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 970 | goto err_free_inst; |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 971 | if (!alloc_cpumask_var(&pinst->cpumask.cbcpu, GFP_KERNEL)) { |
| 972 | free_cpumask_var(pinst->cpumask.pcpu); |
| 973 | goto err_free_inst; |
Steffen Klassert | 33e5445 | 2010-07-07 15:31:26 +0200 | [diff] [blame] | 974 | } |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 975 | if (!padata_validate_cpumask(pinst, pcpumask) || |
| 976 | !padata_validate_cpumask(pinst, cbcpumask)) |
| 977 | goto err_free_masks; |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 978 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 979 | pd = padata_alloc_pd(pinst, pcpumask, cbcpumask); |
| 980 | if (!pd) |
| 981 | goto err_free_masks; |
Steffen Klassert | 7478138 | 2010-03-04 13:30:22 +0800 | [diff] [blame] | 982 | |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 983 | rcu_assign_pointer(pinst->pd, pd); |
| 984 | |
| 985 | pinst->wq = wq; |
| 986 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 987 | cpumask_copy(pinst->cpumask.pcpu, pcpumask); |
| 988 | cpumask_copy(pinst->cpumask.cbcpu, cbcpumask); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 989 | |
| 990 | pinst->flags = 0; |
| 991 | |
Steffen Klassert | 6751fb3 | 2010-04-29 14:42:30 +0200 | [diff] [blame] | 992 | put_online_cpus(); |
| 993 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 994 | BLOCKING_INIT_NOTIFIER_HEAD(&pinst->cpumask_change_notifier); |
Dan Kruchinin | 5e017dc | 2010-07-14 14:33:08 +0400 | [diff] [blame] | 995 | kobject_init(&pinst->kobj, &padata_attr_type); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 996 | mutex_init(&pinst->lock); |
| 997 | |
Richard Weinberger | b8b4a41 | 2013-08-23 13:12:33 +0200 | [diff] [blame] | 998 | #ifdef CONFIG_HOTPLUG_CPU |
Sebastian Andrzej Siewior | 30e9215 | 2016-09-06 19:04:49 +0200 | [diff] [blame] | 999 | cpuhp_state_add_instance_nocalls(hp_online, &pinst->node); |
Richard Weinberger | b8b4a41 | 2013-08-23 13:12:33 +0200 | [diff] [blame] | 1000 | #endif |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 1001 | return pinst; |
| 1002 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 1003 | err_free_masks: |
| 1004 | free_cpumask_var(pinst->cpumask.pcpu); |
| 1005 | free_cpumask_var(pinst->cpumask.cbcpu); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 1006 | err_free_inst: |
| 1007 | kfree(pinst); |
Steffen Klassert | 6751fb3 | 2010-04-29 14:42:30 +0200 | [diff] [blame] | 1008 | put_online_cpus(); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 1009 | err: |
| 1010 | return NULL; |
| 1011 | } |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 1012 | |
Steffen Klassert | 0198ffd | 2010-05-19 13:44:27 +1000 | [diff] [blame] | 1013 | /** |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 1014 | * padata_free - free a padata instance |
| 1015 | * |
Steffen Klassert | 0198ffd | 2010-05-19 13:44:27 +1000 | [diff] [blame] | 1016 | * @padata_inst: padata instance to free |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 1017 | */ |
| 1018 | void padata_free(struct padata_instance *pinst) |
| 1019 | { |
Dan Kruchinin | 5e017dc | 2010-07-14 14:33:08 +0400 | [diff] [blame] | 1020 | kobject_put(&pinst->kobj); |
Steffen Klassert | 16295be | 2010-01-06 19:47:10 +1100 | [diff] [blame] | 1021 | } |
| 1022 | EXPORT_SYMBOL(padata_free); |
Sebastian Andrzej Siewior | 30e9215 | 2016-09-06 19:04:49 +0200 | [diff] [blame] | 1023 | |
| 1024 | #ifdef CONFIG_HOTPLUG_CPU |
| 1025 | |
| 1026 | static __init int padata_driver_init(void) |
| 1027 | { |
| 1028 | int ret; |
| 1029 | |
| 1030 | ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "padata:online", |
| 1031 | padata_cpu_online, |
| 1032 | padata_cpu_prep_down); |
| 1033 | if (ret < 0) |
| 1034 | return ret; |
| 1035 | hp_online = ret; |
| 1036 | return 0; |
| 1037 | } |
| 1038 | module_init(padata_driver_init); |
| 1039 | |
| 1040 | static __exit void padata_driver_exit(void) |
| 1041 | { |
| 1042 | cpuhp_remove_multi_state(hp_online); |
| 1043 | } |
| 1044 | module_exit(padata_driver_exit); |
| 1045 | #endif |