padata.c 25 KB

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