padata.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. /*
  2. * padata.c - generic interface to process data streams in parallel
  3. *
  4. * See Documentation/padata.txt for an api documentation.
  5. *
  6. * Copyright (C) 2008, 2009 secunet Security Networks AG
  7. * Copyright (C) 2008, 2009 Steffen Klassert <steffen.klassert@secunet.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms and conditions of the GNU General Public License,
  11. * version 2, as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <linux/export.h>
  23. #include <linux/cpumask.h>
  24. #include <linux/err.h>
  25. #include <linux/cpu.h>
  26. #include <linux/padata.h>
  27. #include <linux/mutex.h>
  28. #include <linux/sched.h>
  29. #include <linux/slab.h>
  30. #include <linux/sysfs.h>
  31. #include <linux/rcupdate.h>
  32. #include <linux/module.h>
  33. #define MAX_OBJ_NUM 1000
  34. static int padata_index_to_cpu(struct parallel_data *pd, int cpu_index)
  35. {
  36. int cpu, target_cpu;
  37. target_cpu = cpumask_first(pd->cpumask.pcpu);
  38. for (cpu = 0; cpu < cpu_index; cpu++)
  39. target_cpu = cpumask_next(target_cpu, pd->cpumask.pcpu);
  40. return target_cpu;
  41. }
  42. static int padata_cpu_hash(struct parallel_data *pd)
  43. {
  44. unsigned int seq_nr;
  45. int cpu_index;
  46. /*
  47. * Hash the sequence numbers to the cpus by taking
  48. * seq_nr mod. number of cpus in use.
  49. */
  50. seq_nr = atomic_inc_return(&pd->seq_nr);
  51. cpu_index = seq_nr % cpumask_weight(pd->cpumask.pcpu);
  52. return padata_index_to_cpu(pd, cpu_index);
  53. }
  54. static void padata_parallel_worker(struct work_struct *parallel_work)
  55. {
  56. struct padata_parallel_queue *pqueue;
  57. LIST_HEAD(local_list);
  58. local_bh_disable();
  59. pqueue = container_of(parallel_work,
  60. struct padata_parallel_queue, work);
  61. spin_lock(&pqueue->parallel.lock);
  62. list_replace_init(&pqueue->parallel.list, &local_list);
  63. spin_unlock(&pqueue->parallel.lock);
  64. while (!list_empty(&local_list)) {
  65. struct padata_priv *padata;
  66. padata = list_entry(local_list.next,
  67. struct padata_priv, list);
  68. list_del_init(&padata->list);
  69. padata->parallel(padata);
  70. }
  71. local_bh_enable();
  72. }
  73. /**
  74. * padata_do_parallel - padata parallelization function
  75. *
  76. * @pinst: padata instance
  77. * @padata: object to be parallelized
  78. * @cb_cpu: cpu the serialization callback function will run on,
  79. * must be in the serial cpumask of padata(i.e. cpumask.cbcpu).
  80. *
  81. * The parallelization callback function will run with BHs off.
  82. * Note: Every object which is parallelized by padata_do_parallel
  83. * must be seen by padata_do_serial.
  84. */
  85. int padata_do_parallel(struct padata_instance *pinst,
  86. struct padata_priv *padata, int cb_cpu)
  87. {
  88. int target_cpu, err;
  89. struct padata_parallel_queue *queue;
  90. struct parallel_data *pd;
  91. rcu_read_lock_bh();
  92. pd = rcu_dereference_bh(pinst->pd);
  93. err = -EINVAL;
  94. if (!(pinst->flags & PADATA_INIT) || pinst->flags & PADATA_INVALID)
  95. goto out;
  96. if (!cpumask_test_cpu(cb_cpu, pd->cpumask.cbcpu))
  97. goto out;
  98. err = -EBUSY;
  99. if ((pinst->flags & PADATA_RESET))
  100. goto out;
  101. if (atomic_read(&pd->refcnt) >= MAX_OBJ_NUM)
  102. goto out;
  103. err = 0;
  104. atomic_inc(&pd->refcnt);
  105. padata->pd = pd;
  106. padata->cb_cpu = cb_cpu;
  107. target_cpu = padata_cpu_hash(pd);
  108. padata->cpu = target_cpu;
  109. queue = per_cpu_ptr(pd->pqueue, target_cpu);
  110. spin_lock(&queue->parallel.lock);
  111. list_add_tail(&padata->list, &queue->parallel.list);
  112. spin_unlock(&queue->parallel.lock);
  113. queue_work_on(target_cpu, pinst->wq, &queue->work);
  114. out:
  115. rcu_read_unlock_bh();
  116. return err;
  117. }
  118. EXPORT_SYMBOL(padata_do_parallel);
  119. /*
  120. * padata_get_next - Get the next object that needs serialization.
  121. *
  122. * Return values are:
  123. *
  124. * A pointer to the control struct of the next object that needs
  125. * serialization, if present in one of the percpu reorder queues.
  126. *
  127. * -EINPROGRESS, if the next object that needs serialization will
  128. * be parallel processed by another cpu and is not yet present in
  129. * the cpu's reorder queue.
  130. *
  131. * -ENODATA, if this cpu has to do the parallel processing for
  132. * the next object.
  133. */
  134. static struct padata_priv *padata_get_next(struct parallel_data *pd)
  135. {
  136. int cpu, num_cpus;
  137. unsigned int next_nr, next_index;
  138. struct padata_parallel_queue *next_queue;
  139. struct padata_priv *padata;
  140. struct padata_list *reorder;
  141. num_cpus = cpumask_weight(pd->cpumask.pcpu);
  142. /*
  143. * Calculate the percpu reorder queue and the sequence
  144. * number of the next object.
  145. */
  146. next_nr = pd->processed;
  147. next_index = next_nr % num_cpus;
  148. cpu = padata_index_to_cpu(pd, next_index);
  149. next_queue = per_cpu_ptr(pd->pqueue, cpu);
  150. reorder = &next_queue->reorder;
  151. spin_lock(&reorder->lock);
  152. if (!list_empty(&reorder->list)) {
  153. padata = list_entry(reorder->list.next,
  154. struct padata_priv, list);
  155. list_del_init(&padata->list);
  156. atomic_dec(&pd->reorder_objects);
  157. pd->processed++;
  158. spin_unlock(&reorder->lock);
  159. goto out;
  160. }
  161. spin_unlock(&reorder->lock);
  162. if (__this_cpu_read(pd->pqueue->cpu_index) == next_queue->cpu_index) {
  163. padata = ERR_PTR(-ENODATA);
  164. goto out;
  165. }
  166. padata = ERR_PTR(-EINPROGRESS);
  167. out:
  168. return padata;
  169. }
  170. static void padata_reorder(struct parallel_data *pd)
  171. {
  172. int cb_cpu;
  173. struct padata_priv *padata;
  174. struct padata_serial_queue *squeue;
  175. struct padata_instance *pinst = pd->pinst;
  176. /*
  177. * We need to ensure that only one cpu can work on dequeueing of
  178. * the reorder queue the time. Calculating in which percpu reorder
  179. * queue the next object will arrive takes some time. A spinlock
  180. * would be highly contended. Also it is not clear in which order
  181. * the objects arrive to the reorder queues. So a cpu could wait to
  182. * get the lock just to notice that there is nothing to do at the
  183. * moment. Therefore we use a trylock and let the holder of the lock
  184. * care for all the objects enqueued during the holdtime of the lock.
  185. */
  186. if (!spin_trylock_bh(&pd->lock))
  187. return;
  188. while (1) {
  189. padata = padata_get_next(pd);
  190. /*
  191. * If the next object that needs serialization is parallel
  192. * processed by another cpu and is still on it's way to the
  193. * cpu's reorder queue, nothing to do for now.
  194. */
  195. if (PTR_ERR(padata) == -EINPROGRESS)
  196. break;
  197. /*
  198. * This cpu has to do the parallel processing of the next
  199. * object. It's waiting in the cpu's parallelization queue,
  200. * so exit immediately.
  201. */
  202. if (PTR_ERR(padata) == -ENODATA) {
  203. del_timer(&pd->timer);
  204. spin_unlock_bh(&pd->lock);
  205. return;
  206. }
  207. cb_cpu = padata->cb_cpu;
  208. squeue = per_cpu_ptr(pd->squeue, cb_cpu);
  209. spin_lock(&squeue->serial.lock);
  210. list_add_tail(&padata->list, &squeue->serial.list);
  211. spin_unlock(&squeue->serial.lock);
  212. queue_work_on(cb_cpu, pinst->wq, &squeue->work);
  213. }
  214. spin_unlock_bh(&pd->lock);
  215. /*
  216. * The next object that needs serialization might have arrived to
  217. * the reorder queues in the meantime, we will be called again
  218. * from the timer function if no one else cares for it.
  219. */
  220. if (atomic_read(&pd->reorder_objects)
  221. && !(pinst->flags & PADATA_RESET))
  222. mod_timer(&pd->timer, jiffies + HZ);
  223. else
  224. del_timer(&pd->timer);
  225. return;
  226. }
  227. static void invoke_padata_reorder(struct work_struct *work)
  228. {
  229. struct padata_parallel_queue *pqueue;
  230. struct parallel_data *pd;
  231. local_bh_disable();
  232. pqueue = container_of(work, struct padata_parallel_queue, reorder_work);
  233. pd = pqueue->pd;
  234. padata_reorder(pd);
  235. local_bh_enable();
  236. }
  237. static void padata_reorder_timer(struct timer_list *t)
  238. {
  239. struct parallel_data *pd = from_timer(pd, t, timer);
  240. unsigned int weight;
  241. int target_cpu, cpu;
  242. cpu = get_cpu();
  243. /* We don't lock pd here to not interfere with parallel processing
  244. * padata_reorder() calls on other CPUs. We just need any CPU out of
  245. * the cpumask.pcpu set. It would be nice if it's the right one but
  246. * it doesn't matter if we're off to the next one by using an outdated
  247. * pd->processed value.
  248. */
  249. weight = cpumask_weight(pd->cpumask.pcpu);
  250. target_cpu = padata_index_to_cpu(pd, pd->processed % weight);
  251. /* ensure to call the reorder callback on the correct CPU */
  252. if (cpu != target_cpu) {
  253. struct padata_parallel_queue *pqueue;
  254. struct padata_instance *pinst;
  255. /* The timer function is serialized wrt itself -- no locking
  256. * needed.
  257. */
  258. pinst = pd->pinst;
  259. pqueue = per_cpu_ptr(pd->pqueue, target_cpu);
  260. queue_work_on(target_cpu, pinst->wq, &pqueue->reorder_work);
  261. } else {
  262. padata_reorder(pd);
  263. }
  264. put_cpu();
  265. }
  266. static void padata_serial_worker(struct work_struct *serial_work)
  267. {
  268. struct padata_serial_queue *squeue;
  269. struct parallel_data *pd;
  270. LIST_HEAD(local_list);
  271. local_bh_disable();
  272. squeue = container_of(serial_work, struct padata_serial_queue, work);
  273. pd = squeue->pd;
  274. spin_lock(&squeue->serial.lock);
  275. list_replace_init(&squeue->serial.list, &local_list);
  276. spin_unlock(&squeue->serial.lock);
  277. while (!list_empty(&local_list)) {
  278. struct padata_priv *padata;
  279. padata = list_entry(local_list.next,
  280. struct padata_priv, list);
  281. list_del_init(&padata->list);
  282. padata->serial(padata);
  283. atomic_dec(&pd->refcnt);
  284. }
  285. local_bh_enable();
  286. }
  287. /**
  288. * padata_do_serial - padata serialization function
  289. *
  290. * @padata: object to be serialized.
  291. *
  292. * padata_do_serial must be called for every parallelized object.
  293. * The serialization callback function will run with BHs off.
  294. */
  295. void padata_do_serial(struct padata_priv *padata)
  296. {
  297. int cpu;
  298. struct padata_parallel_queue *pqueue;
  299. struct parallel_data *pd;
  300. int reorder_via_wq = 0;
  301. pd = padata->pd;
  302. cpu = get_cpu();
  303. /* We need to run on the same CPU padata_do_parallel(.., padata, ..)
  304. * was called on -- or, at least, enqueue the padata object into the
  305. * correct per-cpu queue.
  306. */
  307. if (cpu != padata->cpu) {
  308. reorder_via_wq = 1;
  309. cpu = padata->cpu;
  310. }
  311. pqueue = per_cpu_ptr(pd->pqueue, cpu);
  312. spin_lock(&pqueue->reorder.lock);
  313. atomic_inc(&pd->reorder_objects);
  314. list_add_tail(&padata->list, &pqueue->reorder.list);
  315. spin_unlock(&pqueue->reorder.lock);
  316. put_cpu();
  317. /* If we're running on the wrong CPU, call padata_reorder() via a
  318. * kernel worker.
  319. */
  320. if (reorder_via_wq)
  321. queue_work_on(cpu, pd->pinst->wq, &pqueue->reorder_work);
  322. else
  323. padata_reorder(pd);
  324. }
  325. EXPORT_SYMBOL(padata_do_serial);
  326. static int padata_setup_cpumasks(struct parallel_data *pd,
  327. const struct cpumask *pcpumask,
  328. const struct cpumask *cbcpumask)
  329. {
  330. if (!alloc_cpumask_var(&pd->cpumask.pcpu, GFP_KERNEL))
  331. return -ENOMEM;
  332. cpumask_and(pd->cpumask.pcpu, pcpumask, cpu_online_mask);
  333. if (!alloc_cpumask_var(&pd->cpumask.cbcpu, GFP_KERNEL)) {
  334. free_cpumask_var(pd->cpumask.pcpu);
  335. return -ENOMEM;
  336. }
  337. cpumask_and(pd->cpumask.cbcpu, cbcpumask, cpu_online_mask);
  338. return 0;
  339. }
  340. static void __padata_list_init(struct padata_list *pd_list)
  341. {
  342. INIT_LIST_HEAD(&pd_list->list);
  343. spin_lock_init(&pd_list->lock);
  344. }
  345. /* Initialize all percpu queues used by serial workers */
  346. static void padata_init_squeues(struct parallel_data *pd)
  347. {
  348. int cpu;
  349. struct padata_serial_queue *squeue;
  350. for_each_cpu(cpu, pd->cpumask.cbcpu) {
  351. squeue = per_cpu_ptr(pd->squeue, cpu);
  352. squeue->pd = pd;
  353. __padata_list_init(&squeue->serial);
  354. INIT_WORK(&squeue->work, padata_serial_worker);
  355. }
  356. }
  357. /* Initialize all percpu queues used by parallel workers */
  358. static void padata_init_pqueues(struct parallel_data *pd)
  359. {
  360. int cpu_index, cpu;
  361. struct padata_parallel_queue *pqueue;
  362. cpu_index = 0;
  363. for_each_possible_cpu(cpu) {
  364. pqueue = per_cpu_ptr(pd->pqueue, cpu);
  365. if (!cpumask_test_cpu(cpu, pd->cpumask.pcpu)) {
  366. pqueue->cpu_index = -1;
  367. continue;
  368. }
  369. pqueue->pd = pd;
  370. pqueue->cpu_index = cpu_index;
  371. cpu_index++;
  372. __padata_list_init(&pqueue->reorder);
  373. __padata_list_init(&pqueue->parallel);
  374. INIT_WORK(&pqueue->work, padata_parallel_worker);
  375. INIT_WORK(&pqueue->reorder_work, invoke_padata_reorder);
  376. atomic_set(&pqueue->num_obj, 0);
  377. }
  378. }
  379. /* Allocate and initialize the internal cpumask dependend resources. */
  380. static struct parallel_data *padata_alloc_pd(struct padata_instance *pinst,
  381. const struct cpumask *pcpumask,
  382. const struct cpumask *cbcpumask)
  383. {
  384. struct parallel_data *pd;
  385. pd = kzalloc(sizeof(struct parallel_data), GFP_KERNEL);
  386. if (!pd)
  387. goto err;
  388. pd->pqueue = alloc_percpu(struct padata_parallel_queue);
  389. if (!pd->pqueue)
  390. goto err_free_pd;
  391. pd->squeue = alloc_percpu(struct padata_serial_queue);
  392. if (!pd->squeue)
  393. goto err_free_pqueue;
  394. if (padata_setup_cpumasks(pd, pcpumask, cbcpumask) < 0)
  395. goto err_free_squeue;
  396. padata_init_pqueues(pd);
  397. padata_init_squeues(pd);
  398. timer_setup(&pd->timer, padata_reorder_timer, 0);
  399. atomic_set(&pd->seq_nr, -1);
  400. atomic_set(&pd->reorder_objects, 0);
  401. atomic_set(&pd->refcnt, 0);
  402. pd->pinst = pinst;
  403. spin_lock_init(&pd->lock);
  404. return pd;
  405. err_free_squeue:
  406. free_percpu(pd->squeue);
  407. err_free_pqueue:
  408. free_percpu(pd->pqueue);
  409. err_free_pd:
  410. kfree(pd);
  411. err:
  412. return NULL;
  413. }
  414. static void padata_free_pd(struct parallel_data *pd)
  415. {
  416. free_cpumask_var(pd->cpumask.pcpu);
  417. free_cpumask_var(pd->cpumask.cbcpu);
  418. free_percpu(pd->pqueue);
  419. free_percpu(pd->squeue);
  420. kfree(pd);
  421. }
  422. /* Flush all objects out of the padata queues. */
  423. static void padata_flush_queues(struct parallel_data *pd)
  424. {
  425. int cpu;
  426. struct padata_parallel_queue *pqueue;
  427. struct padata_serial_queue *squeue;
  428. for_each_cpu(cpu, pd->cpumask.pcpu) {
  429. pqueue = per_cpu_ptr(pd->pqueue, cpu);
  430. flush_work(&pqueue->work);
  431. }
  432. del_timer_sync(&pd->timer);
  433. if (atomic_read(&pd->reorder_objects))
  434. padata_reorder(pd);
  435. for_each_cpu(cpu, pd->cpumask.cbcpu) {
  436. squeue = per_cpu_ptr(pd->squeue, cpu);
  437. flush_work(&squeue->work);
  438. }
  439. BUG_ON(atomic_read(&pd->refcnt) != 0);
  440. }
  441. static void __padata_start(struct padata_instance *pinst)
  442. {
  443. pinst->flags |= PADATA_INIT;
  444. }
  445. static void __padata_stop(struct padata_instance *pinst)
  446. {
  447. if (!(pinst->flags & PADATA_INIT))
  448. return;
  449. pinst->flags &= ~PADATA_INIT;
  450. synchronize_rcu();
  451. get_online_cpus();
  452. padata_flush_queues(pinst->pd);
  453. put_online_cpus();
  454. }
  455. /* Replace the internal control structure with a new one. */
  456. static void padata_replace(struct padata_instance *pinst,
  457. struct parallel_data *pd_new)
  458. {
  459. struct parallel_data *pd_old = pinst->pd;
  460. int notification_mask = 0;
  461. pinst->flags |= PADATA_RESET;
  462. rcu_assign_pointer(pinst->pd, pd_new);
  463. synchronize_rcu();
  464. if (!cpumask_equal(pd_old->cpumask.pcpu, pd_new->cpumask.pcpu))
  465. notification_mask |= PADATA_CPU_PARALLEL;
  466. if (!cpumask_equal(pd_old->cpumask.cbcpu, pd_new->cpumask.cbcpu))
  467. notification_mask |= PADATA_CPU_SERIAL;
  468. padata_flush_queues(pd_old);
  469. padata_free_pd(pd_old);
  470. if (notification_mask)
  471. blocking_notifier_call_chain(&pinst->cpumask_change_notifier,
  472. notification_mask,
  473. &pd_new->cpumask);
  474. pinst->flags &= ~PADATA_RESET;
  475. }
  476. /**
  477. * padata_register_cpumask_notifier - Registers a notifier that will be called
  478. * if either pcpu or cbcpu or both cpumasks change.
  479. *
  480. * @pinst: A poineter to padata instance
  481. * @nblock: A pointer to notifier block.
  482. */
  483. int padata_register_cpumask_notifier(struct padata_instance *pinst,
  484. struct notifier_block *nblock)
  485. {
  486. return blocking_notifier_chain_register(&pinst->cpumask_change_notifier,
  487. nblock);
  488. }
  489. EXPORT_SYMBOL(padata_register_cpumask_notifier);
  490. /**
  491. * padata_unregister_cpumask_notifier - Unregisters cpumask notifier
  492. * registered earlier using padata_register_cpumask_notifier
  493. *
  494. * @pinst: A pointer to data instance.
  495. * @nlock: A pointer to notifier block.
  496. */
  497. int padata_unregister_cpumask_notifier(struct padata_instance *pinst,
  498. struct notifier_block *nblock)
  499. {
  500. return blocking_notifier_chain_unregister(
  501. &pinst->cpumask_change_notifier,
  502. nblock);
  503. }
  504. EXPORT_SYMBOL(padata_unregister_cpumask_notifier);
  505. /* If cpumask contains no active cpu, we mark the instance as invalid. */
  506. static bool padata_validate_cpumask(struct padata_instance *pinst,
  507. const struct cpumask *cpumask)
  508. {
  509. if (!cpumask_intersects(cpumask, cpu_online_mask)) {
  510. pinst->flags |= PADATA_INVALID;
  511. return false;
  512. }
  513. pinst->flags &= ~PADATA_INVALID;
  514. return true;
  515. }
  516. static int __padata_set_cpumasks(struct padata_instance *pinst,
  517. cpumask_var_t pcpumask,
  518. cpumask_var_t cbcpumask)
  519. {
  520. int valid;
  521. struct parallel_data *pd;
  522. valid = padata_validate_cpumask(pinst, pcpumask);
  523. if (!valid) {
  524. __padata_stop(pinst);
  525. goto out_replace;
  526. }
  527. valid = padata_validate_cpumask(pinst, cbcpumask);
  528. if (!valid)
  529. __padata_stop(pinst);
  530. out_replace:
  531. pd = padata_alloc_pd(pinst, pcpumask, cbcpumask);
  532. if (!pd)
  533. return -ENOMEM;
  534. cpumask_copy(pinst->cpumask.pcpu, pcpumask);
  535. cpumask_copy(pinst->cpumask.cbcpu, cbcpumask);
  536. padata_replace(pinst, pd);
  537. if (valid)
  538. __padata_start(pinst);
  539. return 0;
  540. }
  541. /**
  542. * padata_set_cpumask: Sets specified by @cpumask_type cpumask to the value
  543. * equivalent to @cpumask.
  544. *
  545. * @pinst: padata instance
  546. * @cpumask_type: PADATA_CPU_SERIAL or PADATA_CPU_PARALLEL corresponding
  547. * to parallel and serial cpumasks respectively.
  548. * @cpumask: the cpumask to use
  549. */
  550. int padata_set_cpumask(struct padata_instance *pinst, int cpumask_type,
  551. cpumask_var_t cpumask)
  552. {
  553. struct cpumask *serial_mask, *parallel_mask;
  554. int err = -EINVAL;
  555. mutex_lock(&pinst->lock);
  556. get_online_cpus();
  557. switch (cpumask_type) {
  558. case PADATA_CPU_PARALLEL:
  559. serial_mask = pinst->cpumask.cbcpu;
  560. parallel_mask = cpumask;
  561. break;
  562. case PADATA_CPU_SERIAL:
  563. parallel_mask = pinst->cpumask.pcpu;
  564. serial_mask = cpumask;
  565. break;
  566. default:
  567. goto out;
  568. }
  569. err = __padata_set_cpumasks(pinst, parallel_mask, serial_mask);
  570. out:
  571. put_online_cpus();
  572. mutex_unlock(&pinst->lock);
  573. return err;
  574. }
  575. EXPORT_SYMBOL(padata_set_cpumask);
  576. /**
  577. * padata_start - start the parallel processing
  578. *
  579. * @pinst: padata instance to start
  580. */
  581. int padata_start(struct padata_instance *pinst)
  582. {
  583. int err = 0;
  584. mutex_lock(&pinst->lock);
  585. if (pinst->flags & PADATA_INVALID)
  586. err = -EINVAL;
  587. __padata_start(pinst);
  588. mutex_unlock(&pinst->lock);
  589. return err;
  590. }
  591. EXPORT_SYMBOL(padata_start);
  592. /**
  593. * padata_stop - stop the parallel processing
  594. *
  595. * @pinst: padata instance to stop
  596. */
  597. void padata_stop(struct padata_instance *pinst)
  598. {
  599. mutex_lock(&pinst->lock);
  600. __padata_stop(pinst);
  601. mutex_unlock(&pinst->lock);
  602. }
  603. EXPORT_SYMBOL(padata_stop);
  604. #ifdef CONFIG_HOTPLUG_CPU
  605. static int __padata_add_cpu(struct padata_instance *pinst, int cpu)
  606. {
  607. struct parallel_data *pd;
  608. if (cpumask_test_cpu(cpu, cpu_online_mask)) {
  609. pd = padata_alloc_pd(pinst, pinst->cpumask.pcpu,
  610. pinst->cpumask.cbcpu);
  611. if (!pd)
  612. return -ENOMEM;
  613. padata_replace(pinst, pd);
  614. if (padata_validate_cpumask(pinst, pinst->cpumask.pcpu) &&
  615. padata_validate_cpumask(pinst, pinst->cpumask.cbcpu))
  616. __padata_start(pinst);
  617. }
  618. return 0;
  619. }
  620. static int __padata_remove_cpu(struct padata_instance *pinst, int cpu)
  621. {
  622. struct parallel_data *pd = NULL;
  623. if (cpumask_test_cpu(cpu, cpu_online_mask)) {
  624. if (!padata_validate_cpumask(pinst, pinst->cpumask.pcpu) ||
  625. !padata_validate_cpumask(pinst, pinst->cpumask.cbcpu))
  626. __padata_stop(pinst);
  627. pd = padata_alloc_pd(pinst, pinst->cpumask.pcpu,
  628. pinst->cpumask.cbcpu);
  629. if (!pd)
  630. return -ENOMEM;
  631. padata_replace(pinst, pd);
  632. cpumask_clear_cpu(cpu, pd->cpumask.cbcpu);
  633. cpumask_clear_cpu(cpu, pd->cpumask.pcpu);
  634. }
  635. return 0;
  636. }
  637. /**
  638. * padata_remove_cpu - remove a cpu from the one or both(serial and parallel)
  639. * padata cpumasks.
  640. *
  641. * @pinst: padata instance
  642. * @cpu: cpu to remove
  643. * @mask: bitmask specifying from which cpumask @cpu should be removed
  644. * The @mask may be any combination of the following flags:
  645. * PADATA_CPU_SERIAL - serial cpumask
  646. * PADATA_CPU_PARALLEL - parallel cpumask
  647. */
  648. int padata_remove_cpu(struct padata_instance *pinst, int cpu, int mask)
  649. {
  650. int err;
  651. if (!(mask & (PADATA_CPU_SERIAL | PADATA_CPU_PARALLEL)))
  652. return -EINVAL;
  653. mutex_lock(&pinst->lock);
  654. get_online_cpus();
  655. if (mask & PADATA_CPU_SERIAL)
  656. cpumask_clear_cpu(cpu, pinst->cpumask.cbcpu);
  657. if (mask & PADATA_CPU_PARALLEL)
  658. cpumask_clear_cpu(cpu, pinst->cpumask.pcpu);
  659. err = __padata_remove_cpu(pinst, cpu);
  660. put_online_cpus();
  661. mutex_unlock(&pinst->lock);
  662. return err;
  663. }
  664. EXPORT_SYMBOL(padata_remove_cpu);
  665. static inline int pinst_has_cpu(struct padata_instance *pinst, int cpu)
  666. {
  667. return cpumask_test_cpu(cpu, pinst->cpumask.pcpu) ||
  668. cpumask_test_cpu(cpu, pinst->cpumask.cbcpu);
  669. }
  670. static int padata_cpu_online(unsigned int cpu, struct hlist_node *node)
  671. {
  672. struct padata_instance *pinst;
  673. int ret;
  674. pinst = hlist_entry_safe(node, struct padata_instance, node);
  675. if (!pinst_has_cpu(pinst, cpu))
  676. return 0;
  677. mutex_lock(&pinst->lock);
  678. ret = __padata_add_cpu(pinst, cpu);
  679. mutex_unlock(&pinst->lock);
  680. return ret;
  681. }
  682. static int padata_cpu_prep_down(unsigned int cpu, struct hlist_node *node)
  683. {
  684. struct padata_instance *pinst;
  685. int ret;
  686. pinst = hlist_entry_safe(node, struct padata_instance, node);
  687. if (!pinst_has_cpu(pinst, cpu))
  688. return 0;
  689. mutex_lock(&pinst->lock);
  690. ret = __padata_remove_cpu(pinst, cpu);
  691. mutex_unlock(&pinst->lock);
  692. return ret;
  693. }
  694. static enum cpuhp_state hp_online;
  695. #endif
  696. static void __padata_free(struct padata_instance *pinst)
  697. {
  698. #ifdef CONFIG_HOTPLUG_CPU
  699. cpuhp_state_remove_instance_nocalls(hp_online, &pinst->node);
  700. #endif
  701. padata_stop(pinst);
  702. padata_free_pd(pinst->pd);
  703. free_cpumask_var(pinst->cpumask.pcpu);
  704. free_cpumask_var(pinst->cpumask.cbcpu);
  705. kfree(pinst);
  706. }
  707. #define kobj2pinst(_kobj) \
  708. container_of(_kobj, struct padata_instance, kobj)
  709. #define attr2pentry(_attr) \
  710. container_of(_attr, struct padata_sysfs_entry, attr)
  711. static void padata_sysfs_release(struct kobject *kobj)
  712. {
  713. struct padata_instance *pinst = kobj2pinst(kobj);
  714. __padata_free(pinst);
  715. }
  716. struct padata_sysfs_entry {
  717. struct attribute attr;
  718. ssize_t (*show)(struct padata_instance *, struct attribute *, char *);
  719. ssize_t (*store)(struct padata_instance *, struct attribute *,
  720. const char *, size_t);
  721. };
  722. static ssize_t show_cpumask(struct padata_instance *pinst,
  723. struct attribute *attr, char *buf)
  724. {
  725. struct cpumask *cpumask;
  726. ssize_t len;
  727. mutex_lock(&pinst->lock);
  728. if (!strcmp(attr->name, "serial_cpumask"))
  729. cpumask = pinst->cpumask.cbcpu;
  730. else
  731. cpumask = pinst->cpumask.pcpu;
  732. len = snprintf(buf, PAGE_SIZE, "%*pb\n",
  733. nr_cpu_ids, cpumask_bits(cpumask));
  734. mutex_unlock(&pinst->lock);
  735. return len < PAGE_SIZE ? len : -EINVAL;
  736. }
  737. static ssize_t store_cpumask(struct padata_instance *pinst,
  738. struct attribute *attr,
  739. const char *buf, size_t count)
  740. {
  741. cpumask_var_t new_cpumask;
  742. ssize_t ret;
  743. int mask_type;
  744. if (!alloc_cpumask_var(&new_cpumask, GFP_KERNEL))
  745. return -ENOMEM;
  746. ret = bitmap_parse(buf, count, cpumask_bits(new_cpumask),
  747. nr_cpumask_bits);
  748. if (ret < 0)
  749. goto out;
  750. mask_type = !strcmp(attr->name, "serial_cpumask") ?
  751. PADATA_CPU_SERIAL : PADATA_CPU_PARALLEL;
  752. ret = padata_set_cpumask(pinst, mask_type, new_cpumask);
  753. if (!ret)
  754. ret = count;
  755. out:
  756. free_cpumask_var(new_cpumask);
  757. return ret;
  758. }
  759. #define PADATA_ATTR_RW(_name, _show_name, _store_name) \
  760. static struct padata_sysfs_entry _name##_attr = \
  761. __ATTR(_name, 0644, _show_name, _store_name)
  762. #define PADATA_ATTR_RO(_name, _show_name) \
  763. static struct padata_sysfs_entry _name##_attr = \
  764. __ATTR(_name, 0400, _show_name, NULL)
  765. PADATA_ATTR_RW(serial_cpumask, show_cpumask, store_cpumask);
  766. PADATA_ATTR_RW(parallel_cpumask, show_cpumask, store_cpumask);
  767. /*
  768. * Padata sysfs provides the following objects:
  769. * serial_cpumask [RW] - cpumask for serial workers
  770. * parallel_cpumask [RW] - cpumask for parallel workers
  771. */
  772. static struct attribute *padata_default_attrs[] = {
  773. &serial_cpumask_attr.attr,
  774. &parallel_cpumask_attr.attr,
  775. NULL,
  776. };
  777. static ssize_t padata_sysfs_show(struct kobject *kobj,
  778. struct attribute *attr, char *buf)
  779. {
  780. struct padata_instance *pinst;
  781. struct padata_sysfs_entry *pentry;
  782. ssize_t ret = -EIO;
  783. pinst = kobj2pinst(kobj);
  784. pentry = attr2pentry(attr);
  785. if (pentry->show)
  786. ret = pentry->show(pinst, attr, buf);
  787. return ret;
  788. }
  789. static ssize_t padata_sysfs_store(struct kobject *kobj, struct attribute *attr,
  790. const char *buf, size_t count)
  791. {
  792. struct padata_instance *pinst;
  793. struct padata_sysfs_entry *pentry;
  794. ssize_t ret = -EIO;
  795. pinst = kobj2pinst(kobj);
  796. pentry = attr2pentry(attr);
  797. if (pentry->show)
  798. ret = pentry->store(pinst, attr, buf, count);
  799. return ret;
  800. }
  801. static const struct sysfs_ops padata_sysfs_ops = {
  802. .show = padata_sysfs_show,
  803. .store = padata_sysfs_store,
  804. };
  805. static struct kobj_type padata_attr_type = {
  806. .sysfs_ops = &padata_sysfs_ops,
  807. .default_attrs = padata_default_attrs,
  808. .release = padata_sysfs_release,
  809. };
  810. /**
  811. * padata_alloc - allocate and initialize a padata instance and specify
  812. * cpumasks for serial and parallel workers.
  813. *
  814. * @wq: workqueue to use for the allocated padata instance
  815. * @pcpumask: cpumask that will be used for padata parallelization
  816. * @cbcpumask: cpumask that will be used for padata serialization
  817. *
  818. * Must be called from a cpus_read_lock() protected region
  819. */
  820. static struct padata_instance *padata_alloc(struct workqueue_struct *wq,
  821. const struct cpumask *pcpumask,
  822. const struct cpumask *cbcpumask)
  823. {
  824. struct padata_instance *pinst;
  825. struct parallel_data *pd = NULL;
  826. pinst = kzalloc(sizeof(struct padata_instance), GFP_KERNEL);
  827. if (!pinst)
  828. goto err;
  829. if (!alloc_cpumask_var(&pinst->cpumask.pcpu, GFP_KERNEL))
  830. goto err_free_inst;
  831. if (!alloc_cpumask_var(&pinst->cpumask.cbcpu, GFP_KERNEL)) {
  832. free_cpumask_var(pinst->cpumask.pcpu);
  833. goto err_free_inst;
  834. }
  835. if (!padata_validate_cpumask(pinst, pcpumask) ||
  836. !padata_validate_cpumask(pinst, cbcpumask))
  837. goto err_free_masks;
  838. pd = padata_alloc_pd(pinst, pcpumask, cbcpumask);
  839. if (!pd)
  840. goto err_free_masks;
  841. rcu_assign_pointer(pinst->pd, pd);
  842. pinst->wq = wq;
  843. cpumask_copy(pinst->cpumask.pcpu, pcpumask);
  844. cpumask_copy(pinst->cpumask.cbcpu, cbcpumask);
  845. pinst->flags = 0;
  846. BLOCKING_INIT_NOTIFIER_HEAD(&pinst->cpumask_change_notifier);
  847. kobject_init(&pinst->kobj, &padata_attr_type);
  848. mutex_init(&pinst->lock);
  849. #ifdef CONFIG_HOTPLUG_CPU
  850. cpuhp_state_add_instance_nocalls_cpuslocked(hp_online, &pinst->node);
  851. #endif
  852. return pinst;
  853. err_free_masks:
  854. free_cpumask_var(pinst->cpumask.pcpu);
  855. free_cpumask_var(pinst->cpumask.cbcpu);
  856. err_free_inst:
  857. kfree(pinst);
  858. err:
  859. return NULL;
  860. }
  861. /**
  862. * padata_alloc_possible - Allocate and initialize padata instance.
  863. * Use the cpu_possible_mask for serial and
  864. * parallel workers.
  865. *
  866. * @wq: workqueue to use for the allocated padata instance
  867. *
  868. * Must be called from a cpus_read_lock() protected region
  869. */
  870. struct padata_instance *padata_alloc_possible(struct workqueue_struct *wq)
  871. {
  872. lockdep_assert_cpus_held();
  873. return padata_alloc(wq, cpu_possible_mask, cpu_possible_mask);
  874. }
  875. EXPORT_SYMBOL(padata_alloc_possible);
  876. /**
  877. * padata_free - free a padata instance
  878. *
  879. * @padata_inst: padata instance to free
  880. */
  881. void padata_free(struct padata_instance *pinst)
  882. {
  883. kobject_put(&pinst->kobj);
  884. }
  885. EXPORT_SYMBOL(padata_free);
  886. #ifdef CONFIG_HOTPLUG_CPU
  887. static __init int padata_driver_init(void)
  888. {
  889. int ret;
  890. ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "padata:online",
  891. padata_cpu_online,
  892. padata_cpu_prep_down);
  893. if (ret < 0)
  894. return ret;
  895. hp_online = ret;
  896. return 0;
  897. }
  898. module_init(padata_driver_init);
  899. static __exit void padata_driver_exit(void)
  900. {
  901. cpuhp_remove_multi_state(hp_online);
  902. }
  903. module_exit(padata_driver_exit);
  904. #endif