blob: 16cb894dc272b13a5501ace7d512b7ccd6c44ed6 [file] [log] [blame]
Cheah Kok Cheong08b21fb2017-12-21 19:35:30 +08001// SPDX-License-Identifier: GPL-2.0
Steffen Klassert16295be2010-01-06 19:47:10 +11002/*
3 * padata.c - generic interface to process data streams in parallel
4 *
Daniel Jordanbfcdcef82019-12-03 14:31:14 -05005 * See Documentation/core-api/padata.rst for more information.
Steffen Klassert107f8bd2012-03-28 08:42:34 +02006 *
Steffen Klassert16295be2010-01-06 19:47:10 +11007 * Copyright (C) 2008, 2009 secunet Security Networks AG
8 * Copyright (C) 2008, 2009 Steffen Klassert <steffen.klassert@secunet.com>
9 *
Daniel Jordan004ed422020-06-03 15:59:43 -070010 * Copyright (c) 2020 Oracle and/or its affiliates.
11 * Author: Daniel Jordan <daniel.m.jordan@oracle.com>
12 *
Steffen Klassert16295be2010-01-06 19:47:10 +110013 * This program is free software; you can redistribute it and/or modify it
14 * under the terms and conditions of the GNU General Public License,
15 * version 2, as published by the Free Software Foundation.
16 *
17 * This program is distributed in the hope it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20 * more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
25 */
26
Daniel Jordan004ed422020-06-03 15:59:43 -070027#include <linux/completion.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040028#include <linux/export.h>
Steffen Klassert16295be2010-01-06 19:47:10 +110029#include <linux/cpumask.h>
30#include <linux/err.h>
31#include <linux/cpu.h>
32#include <linux/padata.h>
33#include <linux/mutex.h>
34#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Dan Kruchinin5e017dc2010-07-14 14:33:08 +040036#include <linux/sysfs.h>
Steffen Klassert16295be2010-01-06 19:47:10 +110037#include <linux/rcupdate.h>
38
Daniel Jordan004ed422020-06-03 15:59:43 -070039#define PADATA_WORK_ONSTACK 1 /* Work's memory is on stack */
40
Daniel Jordan4611ce22020-06-03 15:59:39 -070041struct padata_work {
42 struct work_struct pw_work;
43 struct list_head pw_list; /* padata_free_works linkage */
44 void *pw_data;
45};
46
47static DEFINE_SPINLOCK(padata_works_lock);
48static struct padata_work *padata_works;
49static LIST_HEAD(padata_free_works);
Steffen Klassert16295be2010-01-06 19:47:10 +110050
Daniel Jordan004ed422020-06-03 15:59:43 -070051struct padata_mt_job_state {
52 spinlock_t lock;
53 struct completion completion;
54 struct padata_mt_job *job;
55 int nworks;
56 int nworks_fini;
57 unsigned long chunk_size;
58};
59
Herbert Xu07928d92019-11-19 13:17:31 +080060static void padata_free_pd(struct parallel_data *pd);
Daniel Jordan004ed422020-06-03 15:59:43 -070061static void __init padata_mt_helper(struct work_struct *work);
Herbert Xu07928d92019-11-19 13:17:31 +080062
Steffen Klassert16295be2010-01-06 19:47:10 +110063static int padata_index_to_cpu(struct parallel_data *pd, int cpu_index)
64{
65 int cpu, target_cpu;
66
Dan Kruchinine15bacb2010-07-14 14:31:57 +040067 target_cpu = cpumask_first(pd->cpumask.pcpu);
Steffen Klassert16295be2010-01-06 19:47:10 +110068 for (cpu = 0; cpu < cpu_index; cpu++)
Dan Kruchinine15bacb2010-07-14 14:31:57 +040069 target_cpu = cpumask_next(target_cpu, pd->cpumask.pcpu);
Steffen Klassert16295be2010-01-06 19:47:10 +110070
71 return target_cpu;
72}
73
Daniel Jordanbfde23c2019-09-05 21:40:28 -040074static int padata_cpu_hash(struct parallel_data *pd, unsigned int seq_nr)
Steffen Klassert16295be2010-01-06 19:47:10 +110075{
Steffen Klassert16295be2010-01-06 19:47:10 +110076 /*
77 * Hash the sequence numbers to the cpus by taking
78 * seq_nr mod. number of cpus in use.
79 */
Daniel Jordanbfde23c2019-09-05 21:40:28 -040080 int cpu_index = seq_nr % cpumask_weight(pd->cpumask.pcpu);
Steffen Klassert16295be2010-01-06 19:47:10 +110081
82 return padata_index_to_cpu(pd, cpu_index);
83}
84
Daniel Jordan4611ce22020-06-03 15:59:39 -070085static struct padata_work *padata_work_alloc(void)
86{
87 struct padata_work *pw;
88
89 lockdep_assert_held(&padata_works_lock);
90
91 if (list_empty(&padata_free_works))
92 return NULL; /* No more work items allowed to be queued. */
93
94 pw = list_first_entry(&padata_free_works, struct padata_work, pw_list);
95 list_del(&pw->pw_list);
96 return pw;
97}
98
99static void padata_work_init(struct padata_work *pw, work_func_t work_fn,
Daniel Jordan004ed422020-06-03 15:59:43 -0700100 void *data, int flags)
Daniel Jordan4611ce22020-06-03 15:59:39 -0700101{
Daniel Jordan004ed422020-06-03 15:59:43 -0700102 if (flags & PADATA_WORK_ONSTACK)
103 INIT_WORK_ONSTACK(&pw->pw_work, work_fn);
104 else
105 INIT_WORK(&pw->pw_work, work_fn);
Daniel Jordan4611ce22020-06-03 15:59:39 -0700106 pw->pw_data = data;
107}
108
Daniel Jordan004ed422020-06-03 15:59:43 -0700109static int __init padata_work_alloc_mt(int nworks, void *data,
110 struct list_head *head)
111{
112 int i;
113
114 spin_lock(&padata_works_lock);
115 /* Start at 1 because the current task participates in the job. */
116 for (i = 1; i < nworks; ++i) {
117 struct padata_work *pw = padata_work_alloc();
118
119 if (!pw)
120 break;
121 padata_work_init(pw, padata_mt_helper, data, 0);
122 list_add(&pw->pw_list, head);
123 }
124 spin_unlock(&padata_works_lock);
125
126 return i;
127}
128
Daniel Jordan4611ce22020-06-03 15:59:39 -0700129static void padata_work_free(struct padata_work *pw)
130{
131 lockdep_assert_held(&padata_works_lock);
132 list_add(&pw->pw_list, &padata_free_works);
133}
134
Daniel Jordan004ed422020-06-03 15:59:43 -0700135static void __init padata_works_free(struct list_head *works)
136{
137 struct padata_work *cur, *next;
138
139 if (list_empty(works))
140 return;
141
142 spin_lock(&padata_works_lock);
143 list_for_each_entry_safe(cur, next, works, pw_list) {
144 list_del(&cur->pw_list);
145 padata_work_free(cur);
146 }
147 spin_unlock(&padata_works_lock);
148}
149
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400150static void padata_parallel_worker(struct work_struct *parallel_work)
Steffen Klassert16295be2010-01-06 19:47:10 +1100151{
Daniel Jordan4611ce22020-06-03 15:59:39 -0700152 struct padata_work *pw = container_of(parallel_work, struct padata_work,
153 pw_work);
154 struct padata_priv *padata = pw->pw_data;
Steffen Klassert16295be2010-01-06 19:47:10 +1100155
156 local_bh_disable();
Daniel Jordan4611ce22020-06-03 15:59:39 -0700157 padata->parallel(padata);
158 spin_lock(&padata_works_lock);
159 padata_work_free(pw);
160 spin_unlock(&padata_works_lock);
Steffen Klassert16295be2010-01-06 19:47:10 +1100161 local_bh_enable();
162}
163
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000164/**
Steffen Klassert16295be2010-01-06 19:47:10 +1100165 * padata_do_parallel - padata parallelization function
166 *
Herbert Xubbefa1d2019-11-26 15:58:45 +0800167 * @ps: padatashell
Steffen Klassert16295be2010-01-06 19:47:10 +1100168 * @padata: object to be parallelized
Daniel Jordane6ce0e02019-09-05 21:40:24 -0400169 * @cb_cpu: pointer to the CPU that the serialization callback function should
170 * run on. If it's not in the serial cpumask of @pinst
171 * (i.e. cpumask.cbcpu), this function selects a fallback CPU and if
172 * none found, returns -EINVAL.
Steffen Klassert16295be2010-01-06 19:47:10 +1100173 *
174 * The parallelization callback function will run with BHs off.
175 * Note: Every object which is parallelized by padata_do_parallel
176 * must be seen by padata_do_serial.
Daniel Jordanbfcdcef82019-12-03 14:31:14 -0500177 *
178 * Return: 0 on success or else negative error code.
Steffen Klassert16295be2010-01-06 19:47:10 +1100179 */
Herbert Xubbefa1d2019-11-26 15:58:45 +0800180int padata_do_parallel(struct padata_shell *ps,
Daniel Jordane6ce0e02019-09-05 21:40:24 -0400181 struct padata_priv *padata, int *cb_cpu)
Steffen Klassert16295be2010-01-06 19:47:10 +1100182{
Herbert Xubbefa1d2019-11-26 15:58:45 +0800183 struct padata_instance *pinst = ps->pinst;
Daniel Jordan4611ce22020-06-03 15:59:39 -0700184 int i, cpu, cpu_index, err;
Steffen Klassert16295be2010-01-06 19:47:10 +1100185 struct parallel_data *pd;
Daniel Jordan4611ce22020-06-03 15:59:39 -0700186 struct padata_work *pw;
Steffen Klassert16295be2010-01-06 19:47:10 +1100187
188 rcu_read_lock_bh();
189
Herbert Xubbefa1d2019-11-26 15:58:45 +0800190 pd = rcu_dereference_bh(ps->pd);
Steffen Klassert16295be2010-01-06 19:47:10 +1100191
Steffen Klassert83f619f2010-07-07 15:32:02 +0200192 err = -EINVAL;
Steffen Klassert74247132010-07-20 08:51:25 +0200193 if (!(pinst->flags & PADATA_INIT) || pinst->flags & PADATA_INVALID)
Steffen Klassert16295be2010-01-06 19:47:10 +1100194 goto out;
195
Daniel Jordane6ce0e02019-09-05 21:40:24 -0400196 if (!cpumask_test_cpu(*cb_cpu, pd->cpumask.cbcpu)) {
197 if (!cpumask_weight(pd->cpumask.cbcpu))
198 goto out;
199
200 /* Select an alternate fallback CPU and notify the caller. */
201 cpu_index = *cb_cpu % cpumask_weight(pd->cpumask.cbcpu);
202
203 cpu = cpumask_first(pd->cpumask.cbcpu);
204 for (i = 0; i < cpu_index; i++)
205 cpu = cpumask_next(cpu, pd->cpumask.cbcpu);
206
207 *cb_cpu = cpu;
208 }
Steffen Klassert16295be2010-01-06 19:47:10 +1100209
210 err = -EBUSY;
211 if ((pinst->flags & PADATA_RESET))
212 goto out;
213
Steffen Klassert16295be2010-01-06 19:47:10 +1100214 atomic_inc(&pd->refcnt);
215 padata->pd = pd;
Daniel Jordane6ce0e02019-09-05 21:40:24 -0400216 padata->cb_cpu = *cb_cpu;
Steffen Klassert16295be2010-01-06 19:47:10 +1100217
Daniel Jordan4611ce22020-06-03 15:59:39 -0700218 rcu_read_unlock_bh();
Steffen Klassert16295be2010-01-06 19:47:10 +1100219
Daniel Jordan4611ce22020-06-03 15:59:39 -0700220 spin_lock(&padata_works_lock);
221 padata->seq_nr = ++pd->seq_nr;
222 pw = padata_work_alloc();
223 spin_unlock(&padata_works_lock);
224 if (pw) {
Daniel Jordan004ed422020-06-03 15:59:43 -0700225 padata_work_init(pw, padata_parallel_worker, padata, 0);
Daniel Jordan4611ce22020-06-03 15:59:39 -0700226 queue_work(pinst->parallel_wq, &pw->pw_work);
227 } else {
228 /* Maximum works limit exceeded, run in the current task. */
229 padata->parallel(padata);
230 }
Steffen Klassert16295be2010-01-06 19:47:10 +1100231
Daniel Jordan4611ce22020-06-03 15:59:39 -0700232 return 0;
Steffen Klassert16295be2010-01-06 19:47:10 +1100233out:
234 rcu_read_unlock_bh();
235
236 return err;
237}
238EXPORT_SYMBOL(padata_do_parallel);
239
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000240/*
Daniel Jordanbfde23c2019-09-05 21:40:28 -0400241 * padata_find_next - Find the next object that needs serialization.
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000242 *
Daniel Jordanbfcdcef82019-12-03 14:31:14 -0500243 * Return:
244 * * A pointer to the control struct of the next object that needs
245 * serialization, if present in one of the percpu reorder queues.
246 * * NULL, if the next object that needs serialization will
247 * be parallel processed by another cpu and is not yet present in
248 * the cpu's reorder queue.
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000249 */
Daniel Jordanbfde23c2019-09-05 21:40:28 -0400250static struct padata_priv *padata_find_next(struct parallel_data *pd,
251 bool remove_object)
Steffen Klassert16295be2010-01-06 19:47:10 +1100252{
Steffen Klassert16295be2010-01-06 19:47:10 +1100253 struct padata_priv *padata;
254 struct padata_list *reorder;
Herbert Xu6fc4dbc2019-07-18 23:01:46 +0800255 int cpu = pd->cpu;
Steffen Klassert16295be2010-01-06 19:47:10 +1100256
Daniel Jordanf601c722020-07-14 16:13:56 -0400257 reorder = per_cpu_ptr(pd->reorder_list, cpu);
Steffen Klassert16295be2010-01-06 19:47:10 +1100258
Jason A. Donenfeldde5540d2017-03-23 12:24:43 +0100259 spin_lock(&reorder->lock);
Daniel Jordanbfde23c2019-09-05 21:40:28 -0400260 if (list_empty(&reorder->list)) {
261 spin_unlock(&reorder->lock);
262 return NULL;
263 }
Steffen Klassert16295be2010-01-06 19:47:10 +1100264
Daniel Jordanbfde23c2019-09-05 21:40:28 -0400265 padata = list_entry(reorder->list.next, struct padata_priv, list);
266
267 /*
268 * Checks the rare case where two or more parallel jobs have hashed to
269 * the same CPU and one of the later ones finishes first.
270 */
271 if (padata->seq_nr != pd->processed) {
272 spin_unlock(&reorder->lock);
273 return NULL;
274 }
275
276 if (remove_object) {
Steffen Klassert16295be2010-01-06 19:47:10 +1100277 list_del_init(&padata->list);
Daniel Jordanbfde23c2019-09-05 21:40:28 -0400278 ++pd->processed;
279 pd->cpu = cpumask_next_wrap(cpu, pd->cpumask.pcpu, -1, false);
Steffen Klassert16295be2010-01-06 19:47:10 +1100280 }
Daniel Jordanbfde23c2019-09-05 21:40:28 -0400281
Jason A. Donenfeldde5540d2017-03-23 12:24:43 +0100282 spin_unlock(&reorder->lock);
Steffen Klassert16295be2010-01-06 19:47:10 +1100283 return padata;
284}
285
286static void padata_reorder(struct parallel_data *pd)
287{
Herbert Xubbefa1d2019-11-26 15:58:45 +0800288 struct padata_instance *pinst = pd->ps->pinst;
Steffen Klassert30478172012-03-09 07:20:12 +0100289 int cb_cpu;
Steffen Klassert16295be2010-01-06 19:47:10 +1100290 struct padata_priv *padata;
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400291 struct padata_serial_queue *squeue;
Daniel Jordanf601c722020-07-14 16:13:56 -0400292 struct padata_list *reorder;
Steffen Klassert16295be2010-01-06 19:47:10 +1100293
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000294 /*
295 * We need to ensure that only one cpu can work on dequeueing of
296 * the reorder queue the time. Calculating in which percpu reorder
297 * queue the next object will arrive takes some time. A spinlock
298 * would be highly contended. Also it is not clear in which order
299 * the objects arrive to the reorder queues. So a cpu could wait to
300 * get the lock just to notice that there is nothing to do at the
301 * moment. Therefore we use a trylock and let the holder of the lock
302 * care for all the objects enqueued during the holdtime of the lock.
303 */
Steffen Klassert16295be2010-01-06 19:47:10 +1100304 if (!spin_trylock_bh(&pd->lock))
Steffen Klassertd46a5ac2010-05-19 13:43:14 +1000305 return;
Steffen Klassert16295be2010-01-06 19:47:10 +1100306
307 while (1) {
Daniel Jordanbfde23c2019-09-05 21:40:28 -0400308 padata = padata_find_next(pd, true);
Steffen Klassert16295be2010-01-06 19:47:10 +1100309
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000310 /*
Jason A. Donenfeld69b34842017-04-12 10:40:19 +0200311 * If the next object that needs serialization is parallel
312 * processed by another cpu and is still on it's way to the
313 * cpu's reorder queue, nothing to do for now.
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000314 */
Daniel Jordanbfde23c2019-09-05 21:40:28 -0400315 if (!padata)
Steffen Klassert16295be2010-01-06 19:47:10 +1100316 break;
317
Steffen Klassert30478172012-03-09 07:20:12 +0100318 cb_cpu = padata->cb_cpu;
319 squeue = per_cpu_ptr(pd->squeue, cb_cpu);
Steffen Klassert16295be2010-01-06 19:47:10 +1100320
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400321 spin_lock(&squeue->serial.lock);
322 list_add_tail(&padata->list, &squeue->serial.list);
323 spin_unlock(&squeue->serial.lock);
Steffen Klassert16295be2010-01-06 19:47:10 +1100324
Daniel Jordan45d153c2019-09-05 21:40:27 -0400325 queue_work_on(cb_cpu, pinst->serial_wq, &squeue->work);
Steffen Klassert16295be2010-01-06 19:47:10 +1100326 }
327
328 spin_unlock_bh(&pd->lock);
329
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000330 /*
331 * The next object that needs serialization might have arrived to
Herbert Xu6fc4dbc2019-07-18 23:01:46 +0800332 * the reorder queues in the meantime.
Daniel Jordancf144f82019-07-16 12:32:53 -0400333 *
Herbert Xu6fc4dbc2019-07-18 23:01:46 +0800334 * Ensure reorder queue is read after pd->lock is dropped so we see
335 * new objects from another task in padata_do_serial. Pairs with
Daniel Jordane04ec0d2020-06-08 17:26:52 -0400336 * smp_mb in padata_do_serial.
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000337 */
Daniel Jordancf144f82019-07-16 12:32:53 -0400338 smp_mb();
Steffen Klassert16295be2010-01-06 19:47:10 +1100339
Daniel Jordanf601c722020-07-14 16:13:56 -0400340 reorder = per_cpu_ptr(pd->reorder_list, pd->cpu);
341 if (!list_empty(&reorder->list) && padata_find_next(pd, false))
Daniel Jordan45d153c2019-09-05 21:40:27 -0400342 queue_work(pinst->serial_wq, &pd->reorder_work);
Steffen Klassert16295be2010-01-06 19:47:10 +1100343}
344
Mathias Krausecf5868c2017-09-08 20:57:10 +0200345static void invoke_padata_reorder(struct work_struct *work)
346{
Mathias Krausecf5868c2017-09-08 20:57:10 +0200347 struct parallel_data *pd;
348
349 local_bh_disable();
Herbert Xu6fc4dbc2019-07-18 23:01:46 +0800350 pd = container_of(work, struct parallel_data, reorder_work);
Mathias Krausecf5868c2017-09-08 20:57:10 +0200351 padata_reorder(pd);
352 local_bh_enable();
353}
354
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400355static void padata_serial_worker(struct work_struct *serial_work)
Steffen Klassert16295be2010-01-06 19:47:10 +1100356{
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400357 struct padata_serial_queue *squeue;
Steffen Klassert16295be2010-01-06 19:47:10 +1100358 struct parallel_data *pd;
359 LIST_HEAD(local_list);
Herbert Xu07928d92019-11-19 13:17:31 +0800360 int cnt;
Steffen Klassert16295be2010-01-06 19:47:10 +1100361
362 local_bh_disable();
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400363 squeue = container_of(serial_work, struct padata_serial_queue, work);
364 pd = squeue->pd;
Steffen Klassert16295be2010-01-06 19:47:10 +1100365
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400366 spin_lock(&squeue->serial.lock);
367 list_replace_init(&squeue->serial.list, &local_list);
368 spin_unlock(&squeue->serial.lock);
Steffen Klassert16295be2010-01-06 19:47:10 +1100369
Herbert Xu07928d92019-11-19 13:17:31 +0800370 cnt = 0;
371
Steffen Klassert16295be2010-01-06 19:47:10 +1100372 while (!list_empty(&local_list)) {
373 struct padata_priv *padata;
374
375 padata = list_entry(local_list.next,
376 struct padata_priv, list);
377
378 list_del_init(&padata->list);
379
380 padata->serial(padata);
Herbert Xu07928d92019-11-19 13:17:31 +0800381 cnt++;
Steffen Klassert16295be2010-01-06 19:47:10 +1100382 }
383 local_bh_enable();
Herbert Xu07928d92019-11-19 13:17:31 +0800384
385 if (atomic_sub_and_test(cnt, &pd->refcnt))
386 padata_free_pd(pd);
Steffen Klassert16295be2010-01-06 19:47:10 +1100387}
388
Steffen Klassert0198ffd2010-05-19 13:44:27 +1000389/**
Steffen Klassert16295be2010-01-06 19:47:10 +1100390 * padata_do_serial - padata serialization function
391 *
392 * @padata: object to be serialized.
393 *
394 * padata_do_serial must be called for every parallelized object.
395 * The serialization callback function will run with BHs off.
396 */
397void padata_do_serial(struct padata_priv *padata)
398{
Daniel Jordan065cf572019-07-19 15:04:44 -0400399 struct parallel_data *pd = padata->pd;
Daniel Jordan4611ce22020-06-03 15:59:39 -0700400 int hashed_cpu = padata_cpu_hash(pd, padata->seq_nr);
Daniel Jordanf601c722020-07-14 16:13:56 -0400401 struct padata_list *reorder = per_cpu_ptr(pd->reorder_list, hashed_cpu);
Daniel Jordanbfde23c2019-09-05 21:40:28 -0400402 struct padata_priv *cur;
Steffen Klassert16295be2010-01-06 19:47:10 +1100403
Daniel Jordanf601c722020-07-14 16:13:56 -0400404 spin_lock(&reorder->lock);
Daniel Jordanbfde23c2019-09-05 21:40:28 -0400405 /* Sort in ascending order of sequence number. */
Daniel Jordanf601c722020-07-14 16:13:56 -0400406 list_for_each_entry_reverse(cur, &reorder->list, list)
Daniel Jordanbfde23c2019-09-05 21:40:28 -0400407 if (cur->seq_nr < padata->seq_nr)
408 break;
409 list_add(&padata->list, &cur->list);
Daniel Jordanf601c722020-07-14 16:13:56 -0400410 spin_unlock(&reorder->lock);
Steffen Klassert16295be2010-01-06 19:47:10 +1100411
Daniel Jordancf144f82019-07-16 12:32:53 -0400412 /*
Herbert Xu6fc4dbc2019-07-18 23:01:46 +0800413 * Ensure the addition to the reorder list is ordered correctly
Daniel Jordancf144f82019-07-16 12:32:53 -0400414 * with the trylock of pd->lock in padata_reorder. Pairs with smp_mb
415 * in padata_reorder.
416 */
Daniel Jordane04ec0d2020-06-08 17:26:52 -0400417 smp_mb();
Daniel Jordancf144f82019-07-16 12:32:53 -0400418
Herbert Xu6fc4dbc2019-07-18 23:01:46 +0800419 padata_reorder(pd);
Steffen Klassert16295be2010-01-06 19:47:10 +1100420}
421EXPORT_SYMBOL(padata_do_serial);
422
Herbert Xubbefa1d2019-11-26 15:58:45 +0800423static int padata_setup_cpumasks(struct padata_instance *pinst)
Steffen Klassert16295be2010-01-06 19:47:10 +1100424{
Daniel Jordanbfde23c2019-09-05 21:40:28 -0400425 struct workqueue_attrs *attrs;
Herbert Xubbefa1d2019-11-26 15:58:45 +0800426 int err;
427
428 attrs = alloc_workqueue_attrs();
429 if (!attrs)
430 return -ENOMEM;
431
432 /* Restrict parallel_wq workers to pd->cpumask.pcpu. */
433 cpumask_copy(attrs->cpumask, pinst->cpumask.pcpu);
434 err = apply_workqueue_attrs(pinst->parallel_wq, attrs);
435 free_workqueue_attrs(attrs);
436
437 return err;
438}
439
Daniel Jordan004ed422020-06-03 15:59:43 -0700440static void __init padata_mt_helper(struct work_struct *w)
441{
442 struct padata_work *pw = container_of(w, struct padata_work, pw_work);
443 struct padata_mt_job_state *ps = pw->pw_data;
444 struct padata_mt_job *job = ps->job;
445 bool done;
446
447 spin_lock(&ps->lock);
448
449 while (job->size > 0) {
450 unsigned long start, size, end;
451
452 start = job->start;
453 /* So end is chunk size aligned if enough work remains. */
454 size = roundup(start + 1, ps->chunk_size) - start;
455 size = min(size, job->size);
456 end = start + size;
457
458 job->start = end;
459 job->size -= size;
460
461 spin_unlock(&ps->lock);
462 job->thread_fn(start, end, job->fn_arg);
463 spin_lock(&ps->lock);
464 }
465
466 ++ps->nworks_fini;
467 done = (ps->nworks_fini == ps->nworks);
468 spin_unlock(&ps->lock);
469
470 if (done)
471 complete(&ps->completion);
472}
473
474/**
475 * padata_do_multithreaded - run a multithreaded job
476 * @job: Description of the job.
477 *
478 * See the definition of struct padata_mt_job for more details.
479 */
480void __init padata_do_multithreaded(struct padata_mt_job *job)
481{
482 /* In case threads finish at different times. */
483 static const unsigned long load_balance_factor = 4;
484 struct padata_work my_work, *pw;
485 struct padata_mt_job_state ps;
486 LIST_HEAD(works);
487 int nworks;
488
489 if (job->size == 0)
490 return;
491
492 /* Ensure at least one thread when size < min_chunk. */
493 nworks = max(job->size / job->min_chunk, 1ul);
494 nworks = min(nworks, job->max_threads);
495
496 if (nworks == 1) {
497 /* Single thread, no coordination needed, cut to the chase. */
498 job->thread_fn(job->start, job->start + job->size, job->fn_arg);
499 return;
500 }
501
502 spin_lock_init(&ps.lock);
503 init_completion(&ps.completion);
504 ps.job = job;
505 ps.nworks = padata_work_alloc_mt(nworks, &ps, &works);
506 ps.nworks_fini = 0;
507
508 /*
509 * Chunk size is the amount of work a helper does per call to the
510 * thread function. Load balance large jobs between threads by
511 * increasing the number of chunks, guarantee at least the minimum
512 * chunk size from the caller, and honor the caller's alignment.
513 */
514 ps.chunk_size = job->size / (ps.nworks * load_balance_factor);
515 ps.chunk_size = max(ps.chunk_size, job->min_chunk);
516 ps.chunk_size = roundup(ps.chunk_size, job->align);
517
518 list_for_each_entry(pw, &works, pw_list)
519 queue_work(system_unbound_wq, &pw->pw_work);
520
521 /* Use the current thread, which saves starting a workqueue worker. */
522 padata_work_init(&my_work, padata_mt_helper, &ps, PADATA_WORK_ONSTACK);
523 padata_mt_helper(&my_work.pw_work);
524
525 /* Wait for all the helpers to finish. */
526 wait_for_completion(&ps.completion);
527
528 destroy_work_on_stack(&my_work.pw_work);
529 padata_works_free(&works);
530}
531
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400532static void __padata_list_init(struct padata_list *pd_list)
533{
534 INIT_LIST_HEAD(&pd_list->list);
535 spin_lock_init(&pd_list->lock);
536}
537
538/* Initialize all percpu queues used by serial workers */
539static void padata_init_squeues(struct parallel_data *pd)
540{
541 int cpu;
542 struct padata_serial_queue *squeue;
543
544 for_each_cpu(cpu, pd->cpumask.cbcpu) {
545 squeue = per_cpu_ptr(pd->squeue, cpu);
546 squeue->pd = pd;
547 __padata_list_init(&squeue->serial);
548 INIT_WORK(&squeue->work, padata_serial_worker);
549 }
550}
551
Daniel Jordanf601c722020-07-14 16:13:56 -0400552/* Initialize per-CPU reorder lists */
553static void padata_init_reorder_list(struct parallel_data *pd)
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400554{
Daniel Jordanc51636a2019-09-05 21:40:29 -0400555 int cpu;
Daniel Jordanf601c722020-07-14 16:13:56 -0400556 struct padata_list *list;
Steffen Klassert16295be2010-01-06 19:47:10 +1100557
Daniel Jordanc51636a2019-09-05 21:40:29 -0400558 for_each_cpu(cpu, pd->cpumask.pcpu) {
Daniel Jordanf601c722020-07-14 16:13:56 -0400559 list = per_cpu_ptr(pd->reorder_list, cpu);
560 __padata_list_init(list);
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400561 }
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400562}
563
564/* Allocate and initialize the internal cpumask dependend resources. */
Herbert Xubbefa1d2019-11-26 15:58:45 +0800565static struct parallel_data *padata_alloc_pd(struct padata_shell *ps)
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400566{
Herbert Xubbefa1d2019-11-26 15:58:45 +0800567 struct padata_instance *pinst = ps->pinst;
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400568 struct parallel_data *pd;
Steffen Klassert16295be2010-01-06 19:47:10 +1100569
570 pd = kzalloc(sizeof(struct parallel_data), GFP_KERNEL);
571 if (!pd)
572 goto err;
573
Daniel Jordanf601c722020-07-14 16:13:56 -0400574 pd->reorder_list = alloc_percpu(struct padata_list);
575 if (!pd->reorder_list)
Steffen Klassert16295be2010-01-06 19:47:10 +1100576 goto err_free_pd;
577
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400578 pd->squeue = alloc_percpu(struct padata_serial_queue);
579 if (!pd->squeue)
Daniel Jordanf601c722020-07-14 16:13:56 -0400580 goto err_free_reorder_list;
Daniel Jordanbfde23c2019-09-05 21:40:28 -0400581
Herbert Xubbefa1d2019-11-26 15:58:45 +0800582 pd->ps = ps;
Daniel Jordancec00e6e2020-07-14 16:13:53 -0400583
584 if (!alloc_cpumask_var(&pd->cpumask.pcpu, GFP_KERNEL))
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400585 goto err_free_squeue;
Daniel Jordancec00e6e2020-07-14 16:13:53 -0400586 if (!alloc_cpumask_var(&pd->cpumask.cbcpu, GFP_KERNEL))
587 goto err_free_pcpu;
588
Daniel Jordand69e0372020-07-14 16:13:54 -0400589 cpumask_and(pd->cpumask.pcpu, pinst->cpumask.pcpu, cpu_online_mask);
590 cpumask_and(pd->cpumask.cbcpu, pinst->cpumask.cbcpu, cpu_online_mask);
Steffen Klassert16295be2010-01-06 19:47:10 +1100591
Daniel Jordanf601c722020-07-14 16:13:56 -0400592 padata_init_reorder_list(pd);
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400593 padata_init_squeues(pd);
Daniel Jordan4611ce22020-06-03 15:59:39 -0700594 pd->seq_nr = -1;
Herbert Xu07928d92019-11-19 13:17:31 +0800595 atomic_set(&pd->refcnt, 1);
Steffen Klassert16295be2010-01-06 19:47:10 +1100596 spin_lock_init(&pd->lock);
Daniel Jordanec9c7d12019-08-08 12:05:35 -0400597 pd->cpu = cpumask_first(pd->cpumask.pcpu);
Herbert Xu6fc4dbc2019-07-18 23:01:46 +0800598 INIT_WORK(&pd->reorder_work, invoke_padata_reorder);
Steffen Klassert16295be2010-01-06 19:47:10 +1100599
600 return pd;
601
Daniel Jordancec00e6e2020-07-14 16:13:53 -0400602err_free_pcpu:
603 free_cpumask_var(pd->cpumask.pcpu);
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400604err_free_squeue:
605 free_percpu(pd->squeue);
Daniel Jordanf601c722020-07-14 16:13:56 -0400606err_free_reorder_list:
607 free_percpu(pd->reorder_list);
Steffen Klassert16295be2010-01-06 19:47:10 +1100608err_free_pd:
609 kfree(pd);
610err:
611 return NULL;
612}
613
614static void padata_free_pd(struct parallel_data *pd)
615{
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400616 free_cpumask_var(pd->cpumask.pcpu);
617 free_cpumask_var(pd->cpumask.cbcpu);
Daniel Jordanf601c722020-07-14 16:13:56 -0400618 free_percpu(pd->reorder_list);
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400619 free_percpu(pd->squeue);
Steffen Klassert16295be2010-01-06 19:47:10 +1100620 kfree(pd);
621}
622
Steffen Klassert4c879172010-07-07 15:30:10 +0200623static void __padata_start(struct padata_instance *pinst)
624{
625 pinst->flags |= PADATA_INIT;
626}
627
Steffen Klassertee836552010-07-07 15:30:47 +0200628static void __padata_stop(struct padata_instance *pinst)
629{
630 if (!(pinst->flags & PADATA_INIT))
631 return;
632
633 pinst->flags &= ~PADATA_INIT;
634
635 synchronize_rcu();
Steffen Klassertee836552010-07-07 15:30:47 +0200636}
637
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300638/* Replace the internal control structure with a new one. */
Herbert Xubbefa1d2019-11-26 15:58:45 +0800639static int padata_replace_one(struct padata_shell *ps)
Steffen Klassert16295be2010-01-06 19:47:10 +1100640{
Herbert Xubbefa1d2019-11-26 15:58:45 +0800641 struct parallel_data *pd_new;
642
643 pd_new = padata_alloc_pd(ps);
644 if (!pd_new)
645 return -ENOMEM;
646
647 ps->opd = rcu_dereference_protected(ps->pd, 1);
648 rcu_assign_pointer(ps->pd, pd_new);
649
650 return 0;
651}
652
Daniel Jordan894c9ef2019-12-03 14:31:10 -0500653static int padata_replace(struct padata_instance *pinst)
Herbert Xubbefa1d2019-11-26 15:58:45 +0800654{
Herbert Xubbefa1d2019-11-26 15:58:45 +0800655 struct padata_shell *ps;
Daniel Jordan41ccdbf2020-02-10 13:11:00 -0500656 int err = 0;
Steffen Klassert16295be2010-01-06 19:47:10 +1100657
658 pinst->flags |= PADATA_RESET;
659
Herbert Xubbefa1d2019-11-26 15:58:45 +0800660 list_for_each_entry(ps, &pinst->pslist, list) {
661 err = padata_replace_one(ps);
662 if (err)
663 break;
664 }
Steffen Klassert16295be2010-01-06 19:47:10 +1100665
666 synchronize_rcu();
667
Herbert Xubbefa1d2019-11-26 15:58:45 +0800668 list_for_each_entry_continue_reverse(ps, &pinst->pslist, list)
669 if (atomic_dec_and_test(&ps->opd->refcnt))
670 padata_free_pd(ps->opd);
Steffen Klassert16295be2010-01-06 19:47:10 +1100671
672 pinst->flags &= ~PADATA_RESET;
Herbert Xubbefa1d2019-11-26 15:58:45 +0800673
674 return err;
Steffen Klassert16295be2010-01-06 19:47:10 +1100675}
676
Steffen Klassert33e54452010-07-07 15:31:26 +0200677/* If cpumask contains no active cpu, we mark the instance as invalid. */
678static bool padata_validate_cpumask(struct padata_instance *pinst,
679 const struct cpumask *cpumask)
680{
Steffen Klassert13614e02012-03-28 08:43:21 +0200681 if (!cpumask_intersects(cpumask, cpu_online_mask)) {
Steffen Klassert33e54452010-07-07 15:31:26 +0200682 pinst->flags |= PADATA_INVALID;
683 return false;
Steffen Klassert16295be2010-01-06 19:47:10 +1100684 }
685
Steffen Klassert33e54452010-07-07 15:31:26 +0200686 pinst->flags &= ~PADATA_INVALID;
687 return true;
688}
689
Steffen Klassert65ff5772010-07-27 07:15:06 +0200690static int __padata_set_cpumasks(struct padata_instance *pinst,
691 cpumask_var_t pcpumask,
692 cpumask_var_t cbcpumask)
Steffen Klassert16295be2010-01-06 19:47:10 +1100693{
Steffen Klassert33e54452010-07-07 15:31:26 +0200694 int valid;
Herbert Xubbefa1d2019-11-26 15:58:45 +0800695 int err;
Steffen Klassert16295be2010-01-06 19:47:10 +1100696
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400697 valid = padata_validate_cpumask(pinst, pcpumask);
698 if (!valid) {
699 __padata_stop(pinst);
700 goto out_replace;
701 }
702
703 valid = padata_validate_cpumask(pinst, cbcpumask);
Steffen Klassertb89661d2010-07-20 08:49:20 +0200704 if (!valid)
Steffen Klassert33e54452010-07-07 15:31:26 +0200705 __padata_stop(pinst);
Steffen Klassert33e54452010-07-07 15:31:26 +0200706
Steffen Klassertb89661d2010-07-20 08:49:20 +0200707out_replace:
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400708 cpumask_copy(pinst->cpumask.pcpu, pcpumask);
709 cpumask_copy(pinst->cpumask.cbcpu, cbcpumask);
Steffen Klassert16295be2010-01-06 19:47:10 +1100710
Daniel Jordan894c9ef2019-12-03 14:31:10 -0500711 err = padata_setup_cpumasks(pinst) ?: padata_replace(pinst);
Steffen Klassert16295be2010-01-06 19:47:10 +1100712
Steffen Klassert33e54452010-07-07 15:31:26 +0200713 if (valid)
714 __padata_start(pinst);
715
Herbert Xubbefa1d2019-11-26 15:58:45 +0800716 return err;
Steffen Klassert65ff5772010-07-27 07:15:06 +0200717}
718
719/**
Daniel Jordanbfcdcef82019-12-03 14:31:14 -0500720 * padata_set_cpumask - Sets specified by @cpumask_type cpumask to the value
721 * equivalent to @cpumask.
Steffen Klassert65ff5772010-07-27 07:15:06 +0200722 * @pinst: padata instance
723 * @cpumask_type: PADATA_CPU_SERIAL or PADATA_CPU_PARALLEL corresponding
724 * to parallel and serial cpumasks respectively.
725 * @cpumask: the cpumask to use
Daniel Jordanbfcdcef82019-12-03 14:31:14 -0500726 *
727 * Return: 0 on success or negative error code
Steffen Klassert65ff5772010-07-27 07:15:06 +0200728 */
729int padata_set_cpumask(struct padata_instance *pinst, int cpumask_type,
730 cpumask_var_t cpumask)
731{
732 struct cpumask *serial_mask, *parallel_mask;
733 int err = -EINVAL;
734
Steffen Klassert65ff5772010-07-27 07:15:06 +0200735 get_online_cpus();
Daniel Jordan38228e82019-12-03 14:31:11 -0500736 mutex_lock(&pinst->lock);
Steffen Klassert65ff5772010-07-27 07:15:06 +0200737
738 switch (cpumask_type) {
739 case PADATA_CPU_PARALLEL:
740 serial_mask = pinst->cpumask.cbcpu;
741 parallel_mask = cpumask;
742 break;
743 case PADATA_CPU_SERIAL:
744 parallel_mask = pinst->cpumask.pcpu;
745 serial_mask = cpumask;
746 break;
747 default:
748 goto out;
749 }
750
751 err = __padata_set_cpumasks(pinst, parallel_mask, serial_mask);
752
Steffen Klassert16295be2010-01-06 19:47:10 +1100753out:
754 mutex_unlock(&pinst->lock);
Daniel Jordan38228e82019-12-03 14:31:11 -0500755 put_online_cpus();
Steffen Klassert16295be2010-01-06 19:47:10 +1100756
757 return err;
758}
759EXPORT_SYMBOL(padata_set_cpumask);
760
Arnd Bergmann19d795b2016-05-19 17:09:59 -0700761#ifdef CONFIG_HOTPLUG_CPU
762
Steffen Klassert16295be2010-01-06 19:47:10 +1100763static int __padata_add_cpu(struct padata_instance *pinst, int cpu)
764{
Herbert Xubbefa1d2019-11-26 15:58:45 +0800765 int err = 0;
Steffen Klassert16295be2010-01-06 19:47:10 +1100766
Steffen Klassert13614e02012-03-28 08:43:21 +0200767 if (cpumask_test_cpu(cpu, cpu_online_mask)) {
Daniel Jordan894c9ef2019-12-03 14:31:10 -0500768 err = padata_replace(pinst);
Steffen Klassert33e54452010-07-07 15:31:26 +0200769
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400770 if (padata_validate_cpumask(pinst, pinst->cpumask.pcpu) &&
771 padata_validate_cpumask(pinst, pinst->cpumask.cbcpu))
Steffen Klassert33e54452010-07-07 15:31:26 +0200772 __padata_start(pinst);
Steffen Klassert16295be2010-01-06 19:47:10 +1100773 }
774
Herbert Xubbefa1d2019-11-26 15:58:45 +0800775 return err;
Steffen Klassert16295be2010-01-06 19:47:10 +1100776}
777
Steffen Klassert16295be2010-01-06 19:47:10 +1100778static int __padata_remove_cpu(struct padata_instance *pinst, int cpu)
779{
Herbert Xubbefa1d2019-11-26 15:58:45 +0800780 int err = 0;
Steffen Klassert16295be2010-01-06 19:47:10 +1100781
Daniel Jordan894c9ef2019-12-03 14:31:10 -0500782 if (!cpumask_test_cpu(cpu, cpu_online_mask)) {
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400783 if (!padata_validate_cpumask(pinst, pinst->cpumask.pcpu) ||
Steffen Klassertb89661d2010-07-20 08:49:20 +0200784 !padata_validate_cpumask(pinst, pinst->cpumask.cbcpu))
Steffen Klassert33e54452010-07-07 15:31:26 +0200785 __padata_stop(pinst);
Steffen Klassert33e54452010-07-07 15:31:26 +0200786
Daniel Jordan894c9ef2019-12-03 14:31:10 -0500787 err = padata_replace(pinst);
Steffen Klassert16295be2010-01-06 19:47:10 +1100788 }
789
Herbert Xubbefa1d2019-11-26 15:58:45 +0800790 return err;
Steffen Klassert16295be2010-01-06 19:47:10 +1100791}
792
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400793static inline int pinst_has_cpu(struct padata_instance *pinst, int cpu)
794{
795 return cpumask_test_cpu(cpu, pinst->cpumask.pcpu) ||
796 cpumask_test_cpu(cpu, pinst->cpumask.cbcpu);
797}
798
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +0200799static int padata_cpu_online(unsigned int cpu, struct hlist_node *node)
Steffen Klassert16295be2010-01-06 19:47:10 +1100800{
Steffen Klassert16295be2010-01-06 19:47:10 +1100801 struct padata_instance *pinst;
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +0200802 int ret;
Steffen Klassert16295be2010-01-06 19:47:10 +1100803
Daniel Jordan3c2214b2020-04-21 12:34:55 -0400804 pinst = hlist_entry_safe(node, struct padata_instance, cpu_online_node);
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +0200805 if (!pinst_has_cpu(pinst, cpu))
806 return 0;
Steffen Klassert16295be2010-01-06 19:47:10 +1100807
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +0200808 mutex_lock(&pinst->lock);
809 ret = __padata_add_cpu(pinst, cpu);
810 mutex_unlock(&pinst->lock);
811 return ret;
Steffen Klassert16295be2010-01-06 19:47:10 +1100812}
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +0200813
Daniel Jordan894c9ef2019-12-03 14:31:10 -0500814static int padata_cpu_dead(unsigned int cpu, struct hlist_node *node)
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +0200815{
816 struct padata_instance *pinst;
817 int ret;
818
Daniel Jordan3c2214b2020-04-21 12:34:55 -0400819 pinst = hlist_entry_safe(node, struct padata_instance, cpu_dead_node);
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +0200820 if (!pinst_has_cpu(pinst, cpu))
821 return 0;
822
823 mutex_lock(&pinst->lock);
824 ret = __padata_remove_cpu(pinst, cpu);
825 mutex_unlock(&pinst->lock);
826 return ret;
827}
828
829static enum cpuhp_state hp_online;
Steffen Klasserte2cb2f12010-04-29 14:40:10 +0200830#endif
Steffen Klassert16295be2010-01-06 19:47:10 +1100831
Dan Kruchinin5e017dc2010-07-14 14:33:08 +0400832static void __padata_free(struct padata_instance *pinst)
833{
834#ifdef CONFIG_HOTPLUG_CPU
Daniel Jordan3c2214b2020-04-21 12:34:55 -0400835 cpuhp_state_remove_instance_nocalls(CPUHP_PADATA_DEAD,
836 &pinst->cpu_dead_node);
837 cpuhp_state_remove_instance_nocalls(hp_online, &pinst->cpu_online_node);
Dan Kruchinin5e017dc2010-07-14 14:33:08 +0400838#endif
839
Herbert Xubbefa1d2019-11-26 15:58:45 +0800840 WARN_ON(!list_empty(&pinst->pslist));
841
Dan Kruchinin5e017dc2010-07-14 14:33:08 +0400842 free_cpumask_var(pinst->cpumask.pcpu);
843 free_cpumask_var(pinst->cpumask.cbcpu);
Daniel Jordan45d153c2019-09-05 21:40:27 -0400844 destroy_workqueue(pinst->serial_wq);
845 destroy_workqueue(pinst->parallel_wq);
Dan Kruchinin5e017dc2010-07-14 14:33:08 +0400846 kfree(pinst);
847}
848
849#define kobj2pinst(_kobj) \
850 container_of(_kobj, struct padata_instance, kobj)
851#define attr2pentry(_attr) \
852 container_of(_attr, struct padata_sysfs_entry, attr)
853
854static void padata_sysfs_release(struct kobject *kobj)
855{
856 struct padata_instance *pinst = kobj2pinst(kobj);
857 __padata_free(pinst);
858}
859
860struct padata_sysfs_entry {
861 struct attribute attr;
862 ssize_t (*show)(struct padata_instance *, struct attribute *, char *);
863 ssize_t (*store)(struct padata_instance *, struct attribute *,
864 const char *, size_t);
865};
866
867static ssize_t show_cpumask(struct padata_instance *pinst,
868 struct attribute *attr, char *buf)
869{
870 struct cpumask *cpumask;
871 ssize_t len;
872
873 mutex_lock(&pinst->lock);
874 if (!strcmp(attr->name, "serial_cpumask"))
875 cpumask = pinst->cpumask.cbcpu;
876 else
877 cpumask = pinst->cpumask.pcpu;
878
Tejun Heo4497da62015-02-13 14:38:05 -0800879 len = snprintf(buf, PAGE_SIZE, "%*pb\n",
880 nr_cpu_ids, cpumask_bits(cpumask));
Dan Kruchinin5e017dc2010-07-14 14:33:08 +0400881 mutex_unlock(&pinst->lock);
Tejun Heo4497da62015-02-13 14:38:05 -0800882 return len < PAGE_SIZE ? len : -EINVAL;
Dan Kruchinin5e017dc2010-07-14 14:33:08 +0400883}
884
885static ssize_t store_cpumask(struct padata_instance *pinst,
886 struct attribute *attr,
887 const char *buf, size_t count)
888{
889 cpumask_var_t new_cpumask;
890 ssize_t ret;
891 int mask_type;
892
893 if (!alloc_cpumask_var(&new_cpumask, GFP_KERNEL))
894 return -ENOMEM;
895
896 ret = bitmap_parse(buf, count, cpumask_bits(new_cpumask),
897 nr_cpumask_bits);
898 if (ret < 0)
899 goto out;
900
901 mask_type = !strcmp(attr->name, "serial_cpumask") ?
902 PADATA_CPU_SERIAL : PADATA_CPU_PARALLEL;
903 ret = padata_set_cpumask(pinst, mask_type, new_cpumask);
904 if (!ret)
905 ret = count;
906
907out:
908 free_cpumask_var(new_cpumask);
909 return ret;
910}
911
912#define PADATA_ATTR_RW(_name, _show_name, _store_name) \
913 static struct padata_sysfs_entry _name##_attr = \
914 __ATTR(_name, 0644, _show_name, _store_name)
915#define PADATA_ATTR_RO(_name, _show_name) \
916 static struct padata_sysfs_entry _name##_attr = \
917 __ATTR(_name, 0400, _show_name, NULL)
918
919PADATA_ATTR_RW(serial_cpumask, show_cpumask, store_cpumask);
920PADATA_ATTR_RW(parallel_cpumask, show_cpumask, store_cpumask);
921
922/*
923 * Padata sysfs provides the following objects:
924 * serial_cpumask [RW] - cpumask for serial workers
925 * parallel_cpumask [RW] - cpumask for parallel workers
Steffen Klassert16295be2010-01-06 19:47:10 +1100926 */
Dan Kruchinin5e017dc2010-07-14 14:33:08 +0400927static struct attribute *padata_default_attrs[] = {
928 &serial_cpumask_attr.attr,
929 &parallel_cpumask_attr.attr,
930 NULL,
931};
Kimberly Brown2064fbc2019-04-01 22:51:47 -0400932ATTRIBUTE_GROUPS(padata_default);
Dan Kruchinin5e017dc2010-07-14 14:33:08 +0400933
934static ssize_t padata_sysfs_show(struct kobject *kobj,
935 struct attribute *attr, char *buf)
Steffen Klassert16295be2010-01-06 19:47:10 +1100936{
Steffen Klassert16295be2010-01-06 19:47:10 +1100937 struct padata_instance *pinst;
Dan Kruchinin5e017dc2010-07-14 14:33:08 +0400938 struct padata_sysfs_entry *pentry;
939 ssize_t ret = -EIO;
940
941 pinst = kobj2pinst(kobj);
942 pentry = attr2pentry(attr);
943 if (pentry->show)
944 ret = pentry->show(pinst, attr, buf);
945
946 return ret;
947}
948
949static ssize_t padata_sysfs_store(struct kobject *kobj, struct attribute *attr,
950 const char *buf, size_t count)
951{
952 struct padata_instance *pinst;
953 struct padata_sysfs_entry *pentry;
954 ssize_t ret = -EIO;
955
956 pinst = kobj2pinst(kobj);
957 pentry = attr2pentry(attr);
958 if (pentry->show)
959 ret = pentry->store(pinst, attr, buf, count);
960
961 return ret;
962}
963
964static const struct sysfs_ops padata_sysfs_ops = {
965 .show = padata_sysfs_show,
966 .store = padata_sysfs_store,
967};
968
969static struct kobj_type padata_attr_type = {
970 .sysfs_ops = &padata_sysfs_ops,
Kimberly Brown2064fbc2019-04-01 22:51:47 -0400971 .default_groups = padata_default_groups,
Dan Kruchinin5e017dc2010-07-14 14:33:08 +0400972 .release = padata_sysfs_release,
973};
974
Steffen Klassert16295be2010-01-06 19:47:10 +1100975/**
Daniel Jordan3f257192020-07-14 16:13:55 -0400976 * padata_alloc - allocate and initialize a padata instance
Daniel Jordanb128a302019-09-05 21:40:21 -0400977 * @name: used to identify the instance
Daniel Jordanbfcdcef82019-12-03 14:31:14 -0500978 *
979 * Return: new instance on success, NULL on error
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400980 */
Daniel Jordan3f257192020-07-14 16:13:55 -0400981struct padata_instance *padata_alloc(const char *name)
Steffen Klassert16295be2010-01-06 19:47:10 +1100982{
983 struct padata_instance *pinst;
Steffen Klassert16295be2010-01-06 19:47:10 +1100984
985 pinst = kzalloc(sizeof(struct padata_instance), GFP_KERNEL);
986 if (!pinst)
987 goto err;
988
Daniel Jordanbfde23c2019-09-05 21:40:28 -0400989 pinst->parallel_wq = alloc_workqueue("%s_parallel", WQ_UNBOUND, 0,
990 name);
Daniel Jordan45d153c2019-09-05 21:40:27 -0400991 if (!pinst->parallel_wq)
Steffen Klassert16295be2010-01-06 19:47:10 +1100992 goto err_free_inst;
Daniel Jordanb128a302019-09-05 21:40:21 -0400993
Daniel Jordancc491d82019-09-05 21:40:26 -0400994 get_online_cpus();
995
Daniel Jordan45d153c2019-09-05 21:40:27 -0400996 pinst->serial_wq = alloc_workqueue("%s_serial", WQ_MEM_RECLAIM |
997 WQ_CPU_INTENSIVE, 1, name);
998 if (!pinst->serial_wq)
Daniel Jordancc491d82019-09-05 21:40:26 -0400999 goto err_put_cpus;
Daniel Jordan45d153c2019-09-05 21:40:27 -04001000
1001 if (!alloc_cpumask_var(&pinst->cpumask.pcpu, GFP_KERNEL))
1002 goto err_free_serial_wq;
Dan Kruchinine15bacb2010-07-14 14:31:57 +04001003 if (!alloc_cpumask_var(&pinst->cpumask.cbcpu, GFP_KERNEL)) {
1004 free_cpumask_var(pinst->cpumask.pcpu);
Daniel Jordan45d153c2019-09-05 21:40:27 -04001005 goto err_free_serial_wq;
Steffen Klassert33e54452010-07-07 15:31:26 +02001006 }
Steffen Klassert16295be2010-01-06 19:47:10 +11001007
Herbert Xubbefa1d2019-11-26 15:58:45 +08001008 INIT_LIST_HEAD(&pinst->pslist);
Steffen Klassert16295be2010-01-06 19:47:10 +11001009
Daniel Jordan3f257192020-07-14 16:13:55 -04001010 cpumask_copy(pinst->cpumask.pcpu, cpu_possible_mask);
1011 cpumask_copy(pinst->cpumask.cbcpu, cpu_possible_mask);
Herbert Xubbefa1d2019-11-26 15:58:45 +08001012
1013 if (padata_setup_cpumasks(pinst))
Daniel Jordand69e0372020-07-14 16:13:54 -04001014 goto err_free_masks;
Steffen Klassert16295be2010-01-06 19:47:10 +11001015
Daniel Jordanbd25b4882020-07-14 16:13:51 -04001016 __padata_start(pinst);
Steffen Klassert16295be2010-01-06 19:47:10 +11001017
Dan Kruchinin5e017dc2010-07-14 14:33:08 +04001018 kobject_init(&pinst->kobj, &padata_attr_type);
Steffen Klassert16295be2010-01-06 19:47:10 +11001019 mutex_init(&pinst->lock);
1020
Richard Weinbergerb8b4a412013-08-23 13:12:33 +02001021#ifdef CONFIG_HOTPLUG_CPU
Daniel Jordan3c2214b2020-04-21 12:34:55 -04001022 cpuhp_state_add_instance_nocalls_cpuslocked(hp_online,
1023 &pinst->cpu_online_node);
Daniel Jordan894c9ef2019-12-03 14:31:10 -05001024 cpuhp_state_add_instance_nocalls_cpuslocked(CPUHP_PADATA_DEAD,
Daniel Jordan3c2214b2020-04-21 12:34:55 -04001025 &pinst->cpu_dead_node);
Richard Weinbergerb8b4a412013-08-23 13:12:33 +02001026#endif
Daniel Jordancc491d82019-09-05 21:40:26 -04001027
1028 put_online_cpus();
1029
Steffen Klassert16295be2010-01-06 19:47:10 +11001030 return pinst;
1031
Dan Kruchinine15bacb2010-07-14 14:31:57 +04001032err_free_masks:
1033 free_cpumask_var(pinst->cpumask.pcpu);
1034 free_cpumask_var(pinst->cpumask.cbcpu);
Daniel Jordan45d153c2019-09-05 21:40:27 -04001035err_free_serial_wq:
1036 destroy_workqueue(pinst->serial_wq);
Daniel Jordancc491d82019-09-05 21:40:26 -04001037err_put_cpus:
1038 put_online_cpus();
Daniel Jordan45d153c2019-09-05 21:40:27 -04001039 destroy_workqueue(pinst->parallel_wq);
Steffen Klassert16295be2010-01-06 19:47:10 +11001040err_free_inst:
1041 kfree(pinst);
1042err:
1043 return NULL;
1044}
Daniel Jordan3f257192020-07-14 16:13:55 -04001045EXPORT_SYMBOL(padata_alloc);
Thomas Gleixner95966952017-05-24 10:15:17 +02001046
1047/**
Steffen Klassert16295be2010-01-06 19:47:10 +11001048 * padata_free - free a padata instance
1049 *
Daniel Jordanbfcdcef82019-12-03 14:31:14 -05001050 * @pinst: padata instance to free
Steffen Klassert16295be2010-01-06 19:47:10 +11001051 */
1052void padata_free(struct padata_instance *pinst)
1053{
Dan Kruchinin5e017dc2010-07-14 14:33:08 +04001054 kobject_put(&pinst->kobj);
Steffen Klassert16295be2010-01-06 19:47:10 +11001055}
1056EXPORT_SYMBOL(padata_free);
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +02001057
Herbert Xubbefa1d2019-11-26 15:58:45 +08001058/**
1059 * padata_alloc_shell - Allocate and initialize padata shell.
1060 *
1061 * @pinst: Parent padata_instance object.
Daniel Jordanbfcdcef82019-12-03 14:31:14 -05001062 *
1063 * Return: new shell on success, NULL on error
Herbert Xubbefa1d2019-11-26 15:58:45 +08001064 */
1065struct padata_shell *padata_alloc_shell(struct padata_instance *pinst)
1066{
1067 struct parallel_data *pd;
1068 struct padata_shell *ps;
1069
1070 ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1071 if (!ps)
1072 goto out;
1073
1074 ps->pinst = pinst;
1075
1076 get_online_cpus();
1077 pd = padata_alloc_pd(ps);
1078 put_online_cpus();
1079
1080 if (!pd)
1081 goto out_free_ps;
1082
1083 mutex_lock(&pinst->lock);
1084 RCU_INIT_POINTER(ps->pd, pd);
1085 list_add(&ps->list, &pinst->pslist);
1086 mutex_unlock(&pinst->lock);
1087
1088 return ps;
1089
1090out_free_ps:
1091 kfree(ps);
1092out:
1093 return NULL;
1094}
1095EXPORT_SYMBOL(padata_alloc_shell);
1096
1097/**
1098 * padata_free_shell - free a padata shell
1099 *
1100 * @ps: padata shell to free
1101 */
1102void padata_free_shell(struct padata_shell *ps)
1103{
Eric Biggers07b24c72020-02-25 20:59:22 -08001104 if (!ps)
1105 return;
Herbert Xubbefa1d2019-11-26 15:58:45 +08001106
Eric Biggers07b24c72020-02-25 20:59:22 -08001107 mutex_lock(&ps->pinst->lock);
Herbert Xubbefa1d2019-11-26 15:58:45 +08001108 list_del(&ps->list);
1109 padata_free_pd(rcu_dereference_protected(ps->pd, 1));
Eric Biggers07b24c72020-02-25 20:59:22 -08001110 mutex_unlock(&ps->pinst->lock);
Herbert Xubbefa1d2019-11-26 15:58:45 +08001111
1112 kfree(ps);
1113}
1114EXPORT_SYMBOL(padata_free_shell);
1115
Daniel Jordanf1b192b2020-06-03 15:59:35 -07001116void __init padata_init(void)
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +02001117{
Daniel Jordan4611ce22020-06-03 15:59:39 -07001118 unsigned int i, possible_cpus;
Daniel Jordanf1b192b2020-06-03 15:59:35 -07001119#ifdef CONFIG_HOTPLUG_CPU
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +02001120 int ret;
1121
1122 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "padata:online",
Daniel Jordan894c9ef2019-12-03 14:31:10 -05001123 padata_cpu_online, NULL);
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +02001124 if (ret < 0)
Daniel Jordanf1b192b2020-06-03 15:59:35 -07001125 goto err;
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +02001126 hp_online = ret;
Daniel Jordan894c9ef2019-12-03 14:31:10 -05001127
1128 ret = cpuhp_setup_state_multi(CPUHP_PADATA_DEAD, "padata:dead",
1129 NULL, padata_cpu_dead);
Daniel Jordan4611ce22020-06-03 15:59:39 -07001130 if (ret < 0)
1131 goto remove_online_state;
1132#endif
1133
1134 possible_cpus = num_possible_cpus();
1135 padata_works = kmalloc_array(possible_cpus, sizeof(struct padata_work),
1136 GFP_KERNEL);
1137 if (!padata_works)
1138 goto remove_dead_state;
1139
1140 for (i = 0; i < possible_cpus; ++i)
1141 list_add(&padata_works[i].pw_list, &padata_free_works);
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +02001142
Daniel Jordanf1b192b2020-06-03 15:59:35 -07001143 return;
Daniel Jordan4611ce22020-06-03 15:59:39 -07001144
1145remove_dead_state:
1146#ifdef CONFIG_HOTPLUG_CPU
1147 cpuhp_remove_multi_state(CPUHP_PADATA_DEAD);
1148remove_online_state:
1149 cpuhp_remove_multi_state(hp_online);
Daniel Jordanf1b192b2020-06-03 15:59:35 -07001150err:
Sebastian Andrzej Siewior30e92152016-09-06 19:04:49 +02001151#endif
Daniel Jordan4611ce22020-06-03 15:59:39 -07001152 pr_warn("padata: initialization failed\n");
Daniel Jordanf1b192b2020-06-03 15:59:35 -07001153}