padata.c 27 KB

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