padata.c 28 KB

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