padata.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * padata.c - generic interface to process data streams in parallel
  4. *
  5. * See Documentation/core-api/padata.rst for more information.
  6. *
  7. * Copyright (C) 2008, 2009 secunet Security Networks AG
  8. * Copyright (C) 2008, 2009 Steffen Klassert <[email protected]>
  9. *
  10. * Copyright (c) 2020 Oracle and/or its affiliates.
  11. * Author: Daniel Jordan <[email protected]>
  12. *
  13. * 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. #include <linux/completion.h>
  27. #include <linux/export.h>
  28. #include <linux/cpumask.h>
  29. #include <linux/err.h>
  30. #include <linux/cpu.h>
  31. #include <linux/padata.h>
  32. #include <linux/mutex.h>
  33. #include <linux/sched.h>
  34. #include <linux/slab.h>
  35. #include <linux/sysfs.h>
  36. #include <linux/rcupdate.h>
  37. #define PADATA_WORK_ONSTACK 1 /* Work's memory is on stack */
  38. struct padata_work {
  39. struct work_struct pw_work;
  40. struct list_head pw_list; /* padata_free_works linkage */
  41. void *pw_data;
  42. };
  43. static DEFINE_SPINLOCK(padata_works_lock);
  44. static struct padata_work *padata_works;
  45. static LIST_HEAD(padata_free_works);
  46. struct padata_mt_job_state {
  47. spinlock_t lock;
  48. struct completion completion;
  49. struct padata_mt_job *job;
  50. int nworks;
  51. int nworks_fini;
  52. unsigned long chunk_size;
  53. };
  54. static void padata_free_pd(struct parallel_data *pd);
  55. static void __init padata_mt_helper(struct work_struct *work);
  56. static int padata_index_to_cpu(struct parallel_data *pd, int cpu_index)
  57. {
  58. int cpu, target_cpu;
  59. target_cpu = cpumask_first(pd->cpumask.pcpu);
  60. for (cpu = 0; cpu < cpu_index; cpu++)
  61. target_cpu = cpumask_next(target_cpu, pd->cpumask.pcpu);
  62. return target_cpu;
  63. }
  64. static int padata_cpu_hash(struct parallel_data *pd, unsigned int seq_nr)
  65. {
  66. /*
  67. * Hash the sequence numbers to the cpus by taking
  68. * seq_nr mod. number of cpus in use.
  69. */
  70. int cpu_index = seq_nr % cpumask_weight(pd->cpumask.pcpu);
  71. return padata_index_to_cpu(pd, cpu_index);
  72. }
  73. static struct padata_work *padata_work_alloc(void)
  74. {
  75. struct padata_work *pw;
  76. lockdep_assert_held(&padata_works_lock);
  77. if (list_empty(&padata_free_works))
  78. return NULL; /* No more work items allowed to be queued. */
  79. pw = list_first_entry(&padata_free_works, struct padata_work, pw_list);
  80. list_del(&pw->pw_list);
  81. return pw;
  82. }
  83. static void padata_work_init(struct padata_work *pw, work_func_t work_fn,
  84. void *data, int flags)
  85. {
  86. if (flags & PADATA_WORK_ONSTACK)
  87. INIT_WORK_ONSTACK(&pw->pw_work, work_fn);
  88. else
  89. INIT_WORK(&pw->pw_work, work_fn);
  90. pw->pw_data = data;
  91. }
  92. static int __init padata_work_alloc_mt(int nworks, void *data,
  93. struct list_head *head)
  94. {
  95. int i;
  96. spin_lock_bh(&padata_works_lock);
  97. /* Start at 1 because the current task participates in the job. */
  98. for (i = 1; i < nworks; ++i) {
  99. struct padata_work *pw = padata_work_alloc();
  100. if (!pw)
  101. break;
  102. padata_work_init(pw, padata_mt_helper, data, 0);
  103. list_add(&pw->pw_list, head);
  104. }
  105. spin_unlock_bh(&padata_works_lock);
  106. return i;
  107. }
  108. static void padata_work_free(struct padata_work *pw)
  109. {
  110. lockdep_assert_held(&padata_works_lock);
  111. list_add(&pw->pw_list, &padata_free_works);
  112. }
  113. static void __init padata_works_free(struct list_head *works)
  114. {
  115. struct padata_work *cur, *next;
  116. if (list_empty(works))
  117. return;
  118. spin_lock_bh(&padata_works_lock);
  119. list_for_each_entry_safe(cur, next, works, pw_list) {
  120. list_del(&cur->pw_list);
  121. padata_work_free(cur);
  122. }
  123. spin_unlock_bh(&padata_works_lock);
  124. }
  125. static void padata_parallel_worker(struct work_struct *parallel_work)
  126. {
  127. struct padata_work *pw = container_of(parallel_work, struct padata_work,
  128. pw_work);
  129. struct padata_priv *padata = pw->pw_data;
  130. local_bh_disable();
  131. padata->parallel(padata);
  132. spin_lock(&padata_works_lock);
  133. padata_work_free(pw);
  134. spin_unlock(&padata_works_lock);
  135. local_bh_enable();
  136. }
  137. /**
  138. * padata_do_parallel - padata parallelization function
  139. *
  140. * @ps: padatashell
  141. * @padata: object to be parallelized
  142. * @cb_cpu: pointer to the CPU that the serialization callback function should
  143. * run on. If it's not in the serial cpumask of @pinst
  144. * (i.e. cpumask.cbcpu), this function selects a fallback CPU and if
  145. * none found, returns -EINVAL.
  146. *
  147. * The parallelization callback function will run with BHs off.
  148. * Note: Every object which is parallelized by padata_do_parallel
  149. * must be seen by padata_do_serial.
  150. *
  151. * Return: 0 on success or else negative error code.
  152. */
  153. int padata_do_parallel(struct padata_shell *ps,
  154. struct padata_priv *padata, int *cb_cpu)
  155. {
  156. struct padata_instance *pinst = ps->pinst;
  157. int i, cpu, cpu_index, err;
  158. struct parallel_data *pd;
  159. struct padata_work *pw;
  160. rcu_read_lock_bh();
  161. pd = rcu_dereference_bh(ps->pd);
  162. err = -EINVAL;
  163. if (!(pinst->flags & PADATA_INIT) || pinst->flags & PADATA_INVALID)
  164. goto out;
  165. if (!cpumask_test_cpu(*cb_cpu, pd->cpumask.cbcpu)) {
  166. if (!cpumask_weight(pd->cpumask.cbcpu))
  167. goto out;
  168. /* Select an alternate fallback CPU and notify the caller. */
  169. cpu_index = *cb_cpu % cpumask_weight(pd->cpumask.cbcpu);
  170. cpu = cpumask_first(pd->cpumask.cbcpu);
  171. for (i = 0; i < cpu_index; i++)
  172. cpu = cpumask_next(cpu, pd->cpumask.cbcpu);
  173. *cb_cpu = cpu;
  174. }
  175. err = -EBUSY;
  176. if ((pinst->flags & PADATA_RESET))
  177. goto out;
  178. refcount_inc(&pd->refcnt);
  179. padata->pd = pd;
  180. padata->cb_cpu = *cb_cpu;
  181. spin_lock(&padata_works_lock);
  182. padata->seq_nr = ++pd->seq_nr;
  183. pw = padata_work_alloc();
  184. spin_unlock(&padata_works_lock);
  185. if (!pw) {
  186. /* Maximum works limit exceeded, run in the current task. */
  187. padata->parallel(padata);
  188. }
  189. rcu_read_unlock_bh();
  190. if (pw) {
  191. padata_work_init(pw, padata_parallel_worker, padata, 0);
  192. queue_work(pinst->parallel_wq, &pw->pw_work);
  193. }
  194. return 0;
  195. out:
  196. rcu_read_unlock_bh();
  197. return err;
  198. }
  199. EXPORT_SYMBOL(padata_do_parallel);
  200. /*
  201. * padata_find_next - Find the next object that needs serialization.
  202. *
  203. * Return:
  204. * * A pointer to the control struct of the next object that needs
  205. * serialization, if present in one of the percpu reorder queues.
  206. * * NULL, if the next object that needs serialization will
  207. * be parallel processed by another cpu and is not yet present in
  208. * the cpu's reorder queue.
  209. */
  210. static struct padata_priv *padata_find_next(struct parallel_data *pd,
  211. bool remove_object)
  212. {
  213. struct padata_priv *padata;
  214. struct padata_list *reorder;
  215. int cpu = pd->cpu;
  216. reorder = per_cpu_ptr(pd->reorder_list, cpu);
  217. spin_lock(&reorder->lock);
  218. if (list_empty(&reorder->list)) {
  219. spin_unlock(&reorder->lock);
  220. return NULL;
  221. }
  222. padata = list_entry(reorder->list.next, struct padata_priv, list);
  223. /*
  224. * Checks the rare case where two or more parallel jobs have hashed to
  225. * the same CPU and one of the later ones finishes first.
  226. */
  227. if (padata->seq_nr != pd->processed) {
  228. spin_unlock(&reorder->lock);
  229. return NULL;
  230. }
  231. if (remove_object) {
  232. list_del_init(&padata->list);
  233. ++pd->processed;
  234. pd->cpu = cpumask_next_wrap(cpu, pd->cpumask.pcpu, -1, false);
  235. }
  236. spin_unlock(&reorder->lock);
  237. return padata;
  238. }
  239. static void padata_reorder(struct parallel_data *pd)
  240. {
  241. struct padata_instance *pinst = pd->ps->pinst;
  242. int cb_cpu;
  243. struct padata_priv *padata;
  244. struct padata_serial_queue *squeue;
  245. struct padata_list *reorder;
  246. /*
  247. * We need to ensure that only one cpu can work on dequeueing of
  248. * the reorder queue the time. Calculating in which percpu reorder
  249. * queue the next object will arrive takes some time. A spinlock
  250. * would be highly contended. Also it is not clear in which order
  251. * the objects arrive to the reorder queues. So a cpu could wait to
  252. * get the lock just to notice that there is nothing to do at the
  253. * moment. Therefore we use a trylock and let the holder of the lock
  254. * care for all the objects enqueued during the holdtime of the lock.
  255. */
  256. if (!spin_trylock_bh(&pd->lock))
  257. return;
  258. while (1) {
  259. padata = padata_find_next(pd, true);
  260. /*
  261. * If the next object that needs serialization is parallel
  262. * processed by another cpu and is still on it's way to the
  263. * cpu's reorder queue, nothing to do for now.
  264. */
  265. if (!padata)
  266. break;
  267. cb_cpu = padata->cb_cpu;
  268. squeue = per_cpu_ptr(pd->squeue, cb_cpu);
  269. spin_lock(&squeue->serial.lock);
  270. list_add_tail(&padata->list, &squeue->serial.list);
  271. spin_unlock(&squeue->serial.lock);
  272. queue_work_on(cb_cpu, pinst->serial_wq, &squeue->work);
  273. }
  274. spin_unlock_bh(&pd->lock);
  275. /*
  276. * The next object that needs serialization might have arrived to
  277. * the reorder queues in the meantime.
  278. *
  279. * Ensure reorder queue is read after pd->lock is dropped so we see
  280. * new objects from another task in padata_do_serial. Pairs with
  281. * smp_mb in padata_do_serial.
  282. */
  283. smp_mb();
  284. reorder = per_cpu_ptr(pd->reorder_list, pd->cpu);
  285. if (!list_empty(&reorder->list) && padata_find_next(pd, false))
  286. queue_work(pinst->serial_wq, &pd->reorder_work);
  287. }
  288. static void invoke_padata_reorder(struct work_struct *work)
  289. {
  290. struct parallel_data *pd;
  291. local_bh_disable();
  292. pd = container_of(work, struct parallel_data, reorder_work);
  293. padata_reorder(pd);
  294. local_bh_enable();
  295. }
  296. static void padata_serial_worker(struct work_struct *serial_work)
  297. {
  298. struct padata_serial_queue *squeue;
  299. struct parallel_data *pd;
  300. LIST_HEAD(local_list);
  301. int cnt;
  302. local_bh_disable();
  303. squeue = container_of(serial_work, struct padata_serial_queue, work);
  304. pd = squeue->pd;
  305. spin_lock(&squeue->serial.lock);
  306. list_replace_init(&squeue->serial.list, &local_list);
  307. spin_unlock(&squeue->serial.lock);
  308. cnt = 0;
  309. while (!list_empty(&local_list)) {
  310. struct padata_priv *padata;
  311. padata = list_entry(local_list.next,
  312. struct padata_priv, list);
  313. list_del_init(&padata->list);
  314. padata->serial(padata);
  315. cnt++;
  316. }
  317. local_bh_enable();
  318. if (refcount_sub_and_test(cnt, &pd->refcnt))
  319. padata_free_pd(pd);
  320. }
  321. /**
  322. * padata_do_serial - padata serialization function
  323. *
  324. * @padata: object to be serialized.
  325. *
  326. * padata_do_serial must be called for every parallelized object.
  327. * The serialization callback function will run with BHs off.
  328. */
  329. void padata_do_serial(struct padata_priv *padata)
  330. {
  331. struct parallel_data *pd = padata->pd;
  332. int hashed_cpu = padata_cpu_hash(pd, padata->seq_nr);
  333. struct padata_list *reorder = per_cpu_ptr(pd->reorder_list, hashed_cpu);
  334. struct padata_priv *cur;
  335. struct list_head *pos;
  336. spin_lock(&reorder->lock);
  337. /* Sort in ascending order of sequence number. */
  338. list_for_each_prev(pos, &reorder->list) {
  339. cur = list_entry(pos, struct padata_priv, list);
  340. /* Compare by difference to consider integer wrap around */
  341. if ((signed int)(cur->seq_nr - padata->seq_nr) < 0)
  342. break;
  343. }
  344. list_add(&padata->list, pos);
  345. spin_unlock(&reorder->lock);
  346. /*
  347. * Ensure the addition to the reorder list is ordered correctly
  348. * with the trylock of pd->lock in padata_reorder. Pairs with smp_mb
  349. * in padata_reorder.
  350. */
  351. smp_mb();
  352. padata_reorder(pd);
  353. }
  354. EXPORT_SYMBOL(padata_do_serial);
  355. static int padata_setup_cpumasks(struct padata_instance *pinst)
  356. {
  357. struct workqueue_attrs *attrs;
  358. int err;
  359. attrs = alloc_workqueue_attrs();
  360. if (!attrs)
  361. return -ENOMEM;
  362. /* Restrict parallel_wq workers to pd->cpumask.pcpu. */
  363. cpumask_copy(attrs->cpumask, pinst->cpumask.pcpu);
  364. err = apply_workqueue_attrs(pinst->parallel_wq, attrs);
  365. free_workqueue_attrs(attrs);
  366. return err;
  367. }
  368. static void __init padata_mt_helper(struct work_struct *w)
  369. {
  370. struct padata_work *pw = container_of(w, struct padata_work, pw_work);
  371. struct padata_mt_job_state *ps = pw->pw_data;
  372. struct padata_mt_job *job = ps->job;
  373. bool done;
  374. spin_lock(&ps->lock);
  375. while (job->size > 0) {
  376. unsigned long start, size, end;
  377. start = job->start;
  378. /* So end is chunk size aligned if enough work remains. */
  379. size = roundup(start + 1, ps->chunk_size) - start;
  380. size = min(size, job->size);
  381. end = start + size;
  382. job->start = end;
  383. job->size -= size;
  384. spin_unlock(&ps->lock);
  385. job->thread_fn(start, end, job->fn_arg);
  386. spin_lock(&ps->lock);
  387. }
  388. ++ps->nworks_fini;
  389. done = (ps->nworks_fini == ps->nworks);
  390. spin_unlock(&ps->lock);
  391. if (done)
  392. complete(&ps->completion);
  393. }
  394. /**
  395. * padata_do_multithreaded - run a multithreaded job
  396. * @job: Description of the job.
  397. *
  398. * See the definition of struct padata_mt_job for more details.
  399. */
  400. void __init padata_do_multithreaded(struct padata_mt_job *job)
  401. {
  402. /* In case threads finish at different times. */
  403. static const unsigned long load_balance_factor = 4;
  404. struct padata_work my_work, *pw;
  405. struct padata_mt_job_state ps;
  406. LIST_HEAD(works);
  407. int nworks;
  408. if (job->size == 0)
  409. return;
  410. /* Ensure at least one thread when size < min_chunk. */
  411. nworks = max(job->size / job->min_chunk, 1ul);
  412. nworks = min(nworks, job->max_threads);
  413. if (nworks == 1) {
  414. /* Single thread, no coordination needed, cut to the chase. */
  415. job->thread_fn(job->start, job->start + job->size, job->fn_arg);
  416. return;
  417. }
  418. spin_lock_init(&ps.lock);
  419. init_completion(&ps.completion);
  420. ps.job = job;
  421. ps.nworks = padata_work_alloc_mt(nworks, &ps, &works);
  422. ps.nworks_fini = 0;
  423. /*
  424. * Chunk size is the amount of work a helper does per call to the
  425. * thread function. Load balance large jobs between threads by
  426. * increasing the number of chunks, guarantee at least the minimum
  427. * chunk size from the caller, and honor the caller's alignment.
  428. * Ensure chunk_size is at least 1 to prevent divide-by-0
  429. * panic in padata_mt_helper().
  430. */
  431. ps.chunk_size = job->size / (ps.nworks * load_balance_factor);
  432. ps.chunk_size = max(ps.chunk_size, job->min_chunk);
  433. ps.chunk_size = max(ps.chunk_size, 1ul);
  434. ps.chunk_size = roundup(ps.chunk_size, job->align);
  435. /*
  436. * chunk_size can be 0 if the caller sets min_chunk to 0. So force it
  437. * to at least 1 to prevent divide-by-0 panic in padata_mt_helper().`
  438. */
  439. if (!ps.chunk_size)
  440. ps.chunk_size = 1U;
  441. list_for_each_entry(pw, &works, pw_list)
  442. queue_work(system_unbound_wq, &pw->pw_work);
  443. /* Use the current thread, which saves starting a workqueue worker. */
  444. padata_work_init(&my_work, padata_mt_helper, &ps, PADATA_WORK_ONSTACK);
  445. padata_mt_helper(&my_work.pw_work);
  446. /* Wait for all the helpers to finish. */
  447. wait_for_completion(&ps.completion);
  448. destroy_work_on_stack(&my_work.pw_work);
  449. padata_works_free(&works);
  450. }
  451. static void __padata_list_init(struct padata_list *pd_list)
  452. {
  453. INIT_LIST_HEAD(&pd_list->list);
  454. spin_lock_init(&pd_list->lock);
  455. }
  456. /* Initialize all percpu queues used by serial workers */
  457. static void padata_init_squeues(struct parallel_data *pd)
  458. {
  459. int cpu;
  460. struct padata_serial_queue *squeue;
  461. for_each_cpu(cpu, pd->cpumask.cbcpu) {
  462. squeue = per_cpu_ptr(pd->squeue, cpu);
  463. squeue->pd = pd;
  464. __padata_list_init(&squeue->serial);
  465. INIT_WORK(&squeue->work, padata_serial_worker);
  466. }
  467. }
  468. /* Initialize per-CPU reorder lists */
  469. static void padata_init_reorder_list(struct parallel_data *pd)
  470. {
  471. int cpu;
  472. struct padata_list *list;
  473. for_each_cpu(cpu, pd->cpumask.pcpu) {
  474. list = per_cpu_ptr(pd->reorder_list, cpu);
  475. __padata_list_init(list);
  476. }
  477. }
  478. /* Allocate and initialize the internal cpumask dependend resources. */
  479. static struct parallel_data *padata_alloc_pd(struct padata_shell *ps)
  480. {
  481. struct padata_instance *pinst = ps->pinst;
  482. struct parallel_data *pd;
  483. pd = kzalloc(sizeof(struct parallel_data), GFP_KERNEL);
  484. if (!pd)
  485. goto err;
  486. pd->reorder_list = alloc_percpu(struct padata_list);
  487. if (!pd->reorder_list)
  488. goto err_free_pd;
  489. pd->squeue = alloc_percpu(struct padata_serial_queue);
  490. if (!pd->squeue)
  491. goto err_free_reorder_list;
  492. pd->ps = ps;
  493. if (!alloc_cpumask_var(&pd->cpumask.pcpu, GFP_KERNEL))
  494. goto err_free_squeue;
  495. if (!alloc_cpumask_var(&pd->cpumask.cbcpu, GFP_KERNEL))
  496. goto err_free_pcpu;
  497. cpumask_and(pd->cpumask.pcpu, pinst->cpumask.pcpu, cpu_online_mask);
  498. cpumask_and(pd->cpumask.cbcpu, pinst->cpumask.cbcpu, cpu_online_mask);
  499. padata_init_reorder_list(pd);
  500. padata_init_squeues(pd);
  501. pd->seq_nr = -1;
  502. refcount_set(&pd->refcnt, 1);
  503. spin_lock_init(&pd->lock);
  504. pd->cpu = cpumask_first(pd->cpumask.pcpu);
  505. INIT_WORK(&pd->reorder_work, invoke_padata_reorder);
  506. return pd;
  507. err_free_pcpu:
  508. free_cpumask_var(pd->cpumask.pcpu);
  509. err_free_squeue:
  510. free_percpu(pd->squeue);
  511. err_free_reorder_list:
  512. free_percpu(pd->reorder_list);
  513. err_free_pd:
  514. kfree(pd);
  515. err:
  516. return NULL;
  517. }
  518. static void padata_free_pd(struct parallel_data *pd)
  519. {
  520. free_cpumask_var(pd->cpumask.pcpu);
  521. free_cpumask_var(pd->cpumask.cbcpu);
  522. free_percpu(pd->reorder_list);
  523. free_percpu(pd->squeue);
  524. kfree(pd);
  525. }
  526. static void __padata_start(struct padata_instance *pinst)
  527. {
  528. pinst->flags |= PADATA_INIT;
  529. }
  530. static void __padata_stop(struct padata_instance *pinst)
  531. {
  532. if (!(pinst->flags & PADATA_INIT))
  533. return;
  534. pinst->flags &= ~PADATA_INIT;
  535. synchronize_rcu();
  536. }
  537. /* Replace the internal control structure with a new one. */
  538. static int padata_replace_one(struct padata_shell *ps)
  539. {
  540. struct parallel_data *pd_new;
  541. pd_new = padata_alloc_pd(ps);
  542. if (!pd_new)
  543. return -ENOMEM;
  544. ps->opd = rcu_dereference_protected(ps->pd, 1);
  545. rcu_assign_pointer(ps->pd, pd_new);
  546. return 0;
  547. }
  548. static int padata_replace(struct padata_instance *pinst)
  549. {
  550. struct padata_shell *ps;
  551. int err = 0;
  552. pinst->flags |= PADATA_RESET;
  553. list_for_each_entry(ps, &pinst->pslist, list) {
  554. err = padata_replace_one(ps);
  555. if (err)
  556. break;
  557. }
  558. synchronize_rcu();
  559. list_for_each_entry_continue_reverse(ps, &pinst->pslist, list)
  560. if (refcount_dec_and_test(&ps->opd->refcnt))
  561. padata_free_pd(ps->opd);
  562. pinst->flags &= ~PADATA_RESET;
  563. return err;
  564. }
  565. /* If cpumask contains no active cpu, we mark the instance as invalid. */
  566. static bool padata_validate_cpumask(struct padata_instance *pinst,
  567. const struct cpumask *cpumask)
  568. {
  569. if (!cpumask_intersects(cpumask, cpu_online_mask)) {
  570. pinst->flags |= PADATA_INVALID;
  571. return false;
  572. }
  573. pinst->flags &= ~PADATA_INVALID;
  574. return true;
  575. }
  576. static int __padata_set_cpumasks(struct padata_instance *pinst,
  577. cpumask_var_t pcpumask,
  578. cpumask_var_t cbcpumask)
  579. {
  580. int valid;
  581. int err;
  582. valid = padata_validate_cpumask(pinst, pcpumask);
  583. if (!valid) {
  584. __padata_stop(pinst);
  585. goto out_replace;
  586. }
  587. valid = padata_validate_cpumask(pinst, cbcpumask);
  588. if (!valid)
  589. __padata_stop(pinst);
  590. out_replace:
  591. cpumask_copy(pinst->cpumask.pcpu, pcpumask);
  592. cpumask_copy(pinst->cpumask.cbcpu, cbcpumask);
  593. err = padata_setup_cpumasks(pinst) ?: padata_replace(pinst);
  594. if (valid)
  595. __padata_start(pinst);
  596. return err;
  597. }
  598. /**
  599. * padata_set_cpumask - Sets specified by @cpumask_type cpumask to the value
  600. * equivalent to @cpumask.
  601. * @pinst: padata instance
  602. * @cpumask_type: PADATA_CPU_SERIAL or PADATA_CPU_PARALLEL corresponding
  603. * to parallel and serial cpumasks respectively.
  604. * @cpumask: the cpumask to use
  605. *
  606. * Return: 0 on success or negative error code
  607. */
  608. int padata_set_cpumask(struct padata_instance *pinst, int cpumask_type,
  609. cpumask_var_t cpumask)
  610. {
  611. struct cpumask *serial_mask, *parallel_mask;
  612. int err = -EINVAL;
  613. get_online_cpus();
  614. mutex_lock(&pinst->lock);
  615. switch (cpumask_type) {
  616. case PADATA_CPU_PARALLEL:
  617. serial_mask = pinst->cpumask.cbcpu;
  618. parallel_mask = cpumask;
  619. break;
  620. case PADATA_CPU_SERIAL:
  621. parallel_mask = pinst->cpumask.pcpu;
  622. serial_mask = cpumask;
  623. break;
  624. default:
  625. goto out;
  626. }
  627. err = __padata_set_cpumasks(pinst, parallel_mask, serial_mask);
  628. out:
  629. mutex_unlock(&pinst->lock);
  630. put_online_cpus();
  631. return err;
  632. }
  633. EXPORT_SYMBOL(padata_set_cpumask);
  634. #ifdef CONFIG_HOTPLUG_CPU
  635. static int __padata_add_cpu(struct padata_instance *pinst, int cpu)
  636. {
  637. int err = 0;
  638. if (cpumask_test_cpu(cpu, cpu_online_mask)) {
  639. err = padata_replace(pinst);
  640. if (padata_validate_cpumask(pinst, pinst->cpumask.pcpu) &&
  641. padata_validate_cpumask(pinst, pinst->cpumask.cbcpu))
  642. __padata_start(pinst);
  643. }
  644. return err;
  645. }
  646. static int __padata_remove_cpu(struct padata_instance *pinst, int cpu)
  647. {
  648. int err = 0;
  649. if (!cpumask_test_cpu(cpu, cpu_online_mask)) {
  650. if (!padata_validate_cpumask(pinst, pinst->cpumask.pcpu) ||
  651. !padata_validate_cpumask(pinst, pinst->cpumask.cbcpu))
  652. __padata_stop(pinst);
  653. err = padata_replace(pinst);
  654. }
  655. return err;
  656. }
  657. static inline int pinst_has_cpu(struct padata_instance *pinst, int cpu)
  658. {
  659. return cpumask_test_cpu(cpu, pinst->cpumask.pcpu) ||
  660. cpumask_test_cpu(cpu, pinst->cpumask.cbcpu);
  661. }
  662. static int padata_cpu_online(unsigned int cpu, struct hlist_node *node)
  663. {
  664. struct padata_instance *pinst;
  665. int ret;
  666. pinst = hlist_entry_safe(node, struct padata_instance, cpu_online_node);
  667. if (!pinst_has_cpu(pinst, cpu))
  668. return 0;
  669. mutex_lock(&pinst->lock);
  670. ret = __padata_add_cpu(pinst, cpu);
  671. mutex_unlock(&pinst->lock);
  672. return ret;
  673. }
  674. static int padata_cpu_dead(unsigned int cpu, struct hlist_node *node)
  675. {
  676. struct padata_instance *pinst;
  677. int ret;
  678. pinst = hlist_entry_safe(node, struct padata_instance, cpu_dead_node);
  679. if (!pinst_has_cpu(pinst, cpu))
  680. return 0;
  681. mutex_lock(&pinst->lock);
  682. ret = __padata_remove_cpu(pinst, cpu);
  683. mutex_unlock(&pinst->lock);
  684. return ret;
  685. }
  686. static enum cpuhp_state hp_online;
  687. #endif
  688. static void __padata_free(struct padata_instance *pinst)
  689. {
  690. #ifdef CONFIG_HOTPLUG_CPU
  691. cpuhp_state_remove_instance_nocalls(CPUHP_PADATA_DEAD,
  692. &pinst->cpu_dead_node);
  693. cpuhp_state_remove_instance_nocalls(hp_online, &pinst->cpu_online_node);
  694. #endif
  695. WARN_ON(!list_empty(&pinst->pslist));
  696. free_cpumask_var(pinst->cpumask.pcpu);
  697. free_cpumask_var(pinst->cpumask.cbcpu);
  698. destroy_workqueue(pinst->serial_wq);
  699. destroy_workqueue(pinst->parallel_wq);
  700. kfree(pinst);
  701. }
  702. #define kobj2pinst(_kobj) \
  703. container_of(_kobj, struct padata_instance, kobj)
  704. #define attr2pentry(_attr) \
  705. container_of(_attr, struct padata_sysfs_entry, attr)
  706. static void padata_sysfs_release(struct kobject *kobj)
  707. {
  708. struct padata_instance *pinst = kobj2pinst(kobj);
  709. __padata_free(pinst);
  710. }
  711. struct padata_sysfs_entry {
  712. struct attribute attr;
  713. ssize_t (*show)(struct padata_instance *, struct attribute *, char *);
  714. ssize_t (*store)(struct padata_instance *, struct attribute *,
  715. const char *, size_t);
  716. };
  717. static ssize_t show_cpumask(struct padata_instance *pinst,
  718. struct attribute *attr, char *buf)
  719. {
  720. struct cpumask *cpumask;
  721. ssize_t len;
  722. mutex_lock(&pinst->lock);
  723. if (!strcmp(attr->name, "serial_cpumask"))
  724. cpumask = pinst->cpumask.cbcpu;
  725. else
  726. cpumask = pinst->cpumask.pcpu;
  727. len = snprintf(buf, PAGE_SIZE, "%*pb\n",
  728. nr_cpu_ids, cpumask_bits(cpumask));
  729. mutex_unlock(&pinst->lock);
  730. return len < PAGE_SIZE ? len : -EINVAL;
  731. }
  732. static ssize_t store_cpumask(struct padata_instance *pinst,
  733. struct attribute *attr,
  734. const char *buf, size_t count)
  735. {
  736. cpumask_var_t new_cpumask;
  737. ssize_t ret;
  738. int mask_type;
  739. if (!alloc_cpumask_var(&new_cpumask, GFP_KERNEL))
  740. return -ENOMEM;
  741. ret = bitmap_parse(buf, count, cpumask_bits(new_cpumask),
  742. nr_cpumask_bits);
  743. if (ret < 0)
  744. goto out;
  745. mask_type = !strcmp(attr->name, "serial_cpumask") ?
  746. PADATA_CPU_SERIAL : PADATA_CPU_PARALLEL;
  747. ret = padata_set_cpumask(pinst, mask_type, new_cpumask);
  748. if (!ret)
  749. ret = count;
  750. out:
  751. free_cpumask_var(new_cpumask);
  752. return ret;
  753. }
  754. #define PADATA_ATTR_RW(_name, _show_name, _store_name) \
  755. static struct padata_sysfs_entry _name##_attr = \
  756. __ATTR(_name, 0644, _show_name, _store_name)
  757. #define PADATA_ATTR_RO(_name, _show_name) \
  758. static struct padata_sysfs_entry _name##_attr = \
  759. __ATTR(_name, 0400, _show_name, NULL)
  760. PADATA_ATTR_RW(serial_cpumask, show_cpumask, store_cpumask);
  761. PADATA_ATTR_RW(parallel_cpumask, show_cpumask, store_cpumask);
  762. /*
  763. * Padata sysfs provides the following objects:
  764. * serial_cpumask [RW] - cpumask for serial workers
  765. * parallel_cpumask [RW] - cpumask for parallel workers
  766. */
  767. static struct attribute *padata_default_attrs[] = {
  768. &serial_cpumask_attr.attr,
  769. &parallel_cpumask_attr.attr,
  770. NULL,
  771. };
  772. ATTRIBUTE_GROUPS(padata_default);
  773. static ssize_t padata_sysfs_show(struct kobject *kobj,
  774. struct attribute *attr, char *buf)
  775. {
  776. struct padata_instance *pinst;
  777. struct padata_sysfs_entry *pentry;
  778. ssize_t ret = -EIO;
  779. pinst = kobj2pinst(kobj);
  780. pentry = attr2pentry(attr);
  781. if (pentry->show)
  782. ret = pentry->show(pinst, attr, buf);
  783. return ret;
  784. }
  785. static ssize_t padata_sysfs_store(struct kobject *kobj, struct attribute *attr,
  786. const char *buf, size_t count)
  787. {
  788. struct padata_instance *pinst;
  789. struct padata_sysfs_entry *pentry;
  790. ssize_t ret = -EIO;
  791. pinst = kobj2pinst(kobj);
  792. pentry = attr2pentry(attr);
  793. if (pentry->show)
  794. ret = pentry->store(pinst, attr, buf, count);
  795. return ret;
  796. }
  797. static const struct sysfs_ops padata_sysfs_ops = {
  798. .show = padata_sysfs_show,
  799. .store = padata_sysfs_store,
  800. };
  801. static struct kobj_type padata_attr_type = {
  802. .sysfs_ops = &padata_sysfs_ops,
  803. .default_groups = padata_default_groups,
  804. .release = padata_sysfs_release,
  805. };
  806. /**
  807. * padata_alloc - allocate and initialize a padata instance
  808. * @name: used to identify the instance
  809. *
  810. * Return: new instance on success, NULL on error
  811. */
  812. struct padata_instance *padata_alloc(const char *name)
  813. {
  814. struct padata_instance *pinst;
  815. pinst = kzalloc(sizeof(struct padata_instance), GFP_KERNEL);
  816. if (!pinst)
  817. goto err;
  818. pinst->parallel_wq = alloc_workqueue("%s_parallel", WQ_UNBOUND, 0,
  819. name);
  820. if (!pinst->parallel_wq)
  821. goto err_free_inst;
  822. get_online_cpus();
  823. pinst->serial_wq = alloc_workqueue("%s_serial", WQ_MEM_RECLAIM |
  824. WQ_CPU_INTENSIVE, 1, name);
  825. if (!pinst->serial_wq)
  826. goto err_put_cpus;
  827. if (!alloc_cpumask_var(&pinst->cpumask.pcpu, GFP_KERNEL))
  828. goto err_free_serial_wq;
  829. if (!alloc_cpumask_var(&pinst->cpumask.cbcpu, GFP_KERNEL)) {
  830. free_cpumask_var(pinst->cpumask.pcpu);
  831. goto err_free_serial_wq;
  832. }
  833. INIT_LIST_HEAD(&pinst->pslist);
  834. cpumask_copy(pinst->cpumask.pcpu, cpu_possible_mask);
  835. cpumask_copy(pinst->cpumask.cbcpu, cpu_possible_mask);
  836. if (padata_setup_cpumasks(pinst))
  837. goto err_free_masks;
  838. __padata_start(pinst);
  839. kobject_init(&pinst->kobj, &padata_attr_type);
  840. mutex_init(&pinst->lock);
  841. #ifdef CONFIG_HOTPLUG_CPU
  842. cpuhp_state_add_instance_nocalls_cpuslocked(hp_online,
  843. &pinst->cpu_online_node);
  844. cpuhp_state_add_instance_nocalls_cpuslocked(CPUHP_PADATA_DEAD,
  845. &pinst->cpu_dead_node);
  846. #endif
  847. put_online_cpus();
  848. return pinst;
  849. err_free_masks:
  850. free_cpumask_var(pinst->cpumask.pcpu);
  851. free_cpumask_var(pinst->cpumask.cbcpu);
  852. err_free_serial_wq:
  853. destroy_workqueue(pinst->serial_wq);
  854. err_put_cpus:
  855. put_online_cpus();
  856. destroy_workqueue(pinst->parallel_wq);
  857. err_free_inst:
  858. kfree(pinst);
  859. err:
  860. return NULL;
  861. }
  862. EXPORT_SYMBOL(padata_alloc);
  863. /**
  864. * padata_free - free a padata instance
  865. *
  866. * @pinst: padata instance to free
  867. */
  868. void padata_free(struct padata_instance *pinst)
  869. {
  870. kobject_put(&pinst->kobj);
  871. }
  872. EXPORT_SYMBOL(padata_free);
  873. /**
  874. * padata_alloc_shell - Allocate and initialize padata shell.
  875. *
  876. * @pinst: Parent padata_instance object.
  877. *
  878. * Return: new shell on success, NULL on error
  879. */
  880. struct padata_shell *padata_alloc_shell(struct padata_instance *pinst)
  881. {
  882. struct parallel_data *pd;
  883. struct padata_shell *ps;
  884. ps = kzalloc(sizeof(*ps), GFP_KERNEL);
  885. if (!ps)
  886. goto out;
  887. ps->pinst = pinst;
  888. get_online_cpus();
  889. pd = padata_alloc_pd(ps);
  890. put_online_cpus();
  891. if (!pd)
  892. goto out_free_ps;
  893. mutex_lock(&pinst->lock);
  894. RCU_INIT_POINTER(ps->pd, pd);
  895. list_add(&ps->list, &pinst->pslist);
  896. mutex_unlock(&pinst->lock);
  897. return ps;
  898. out_free_ps:
  899. kfree(ps);
  900. out:
  901. return NULL;
  902. }
  903. EXPORT_SYMBOL(padata_alloc_shell);
  904. /**
  905. * padata_free_shell - free a padata shell
  906. *
  907. * @ps: padata shell to free
  908. */
  909. void padata_free_shell(struct padata_shell *ps)
  910. {
  911. struct parallel_data *pd;
  912. if (!ps)
  913. return;
  914. mutex_lock(&ps->pinst->lock);
  915. list_del(&ps->list);
  916. pd = rcu_dereference_protected(ps->pd, 1);
  917. if (refcount_dec_and_test(&pd->refcnt))
  918. padata_free_pd(pd);
  919. mutex_unlock(&ps->pinst->lock);
  920. kfree(ps);
  921. }
  922. EXPORT_SYMBOL(padata_free_shell);
  923. void __init padata_init(void)
  924. {
  925. unsigned int i, possible_cpus;
  926. #ifdef CONFIG_HOTPLUG_CPU
  927. int ret;
  928. ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "padata:online",
  929. padata_cpu_online, NULL);
  930. if (ret < 0)
  931. goto err;
  932. hp_online = ret;
  933. ret = cpuhp_setup_state_multi(CPUHP_PADATA_DEAD, "padata:dead",
  934. NULL, padata_cpu_dead);
  935. if (ret < 0)
  936. goto remove_online_state;
  937. #endif
  938. possible_cpus = num_possible_cpus();
  939. padata_works = kmalloc_array(possible_cpus, sizeof(struct padata_work),
  940. GFP_KERNEL);
  941. if (!padata_works)
  942. goto remove_dead_state;
  943. for (i = 0; i < possible_cpus; ++i)
  944. list_add(&padata_works[i].pw_list, &padata_free_works);
  945. return;
  946. remove_dead_state:
  947. #ifdef CONFIG_HOTPLUG_CPU
  948. cpuhp_remove_multi_state(CPUHP_PADATA_DEAD);
  949. remove_online_state:
  950. cpuhp_remove_multi_state(hp_online);
  951. err:
  952. #endif
  953. pr_warn("padata: initialization failed\n");
  954. }