padata.c 25 KB

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