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