padata.c 25 KB

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