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