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