padata.c 27 KB

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