padata.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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/rcupdate.h>
  29. #define MAX_SEQ_NR INT_MAX - NR_CPUS
  30. #define MAX_OBJ_NUM 10000 * NR_CPUS
  31. static int padata_index_to_cpu(struct parallel_data *pd, int cpu_index)
  32. {
  33. int cpu, target_cpu;
  34. target_cpu = cpumask_first(pd->cpumask);
  35. for (cpu = 0; cpu < cpu_index; cpu++)
  36. target_cpu = cpumask_next(target_cpu, pd->cpumask);
  37. return target_cpu;
  38. }
  39. static int padata_cpu_hash(struct padata_priv *padata)
  40. {
  41. int cpu_index;
  42. struct parallel_data *pd;
  43. pd = padata->pd;
  44. /*
  45. * Hash the sequence numbers to the cpus by taking
  46. * seq_nr mod. number of cpus in use.
  47. */
  48. cpu_index = padata->seq_nr % cpumask_weight(pd->cpumask);
  49. return padata_index_to_cpu(pd, cpu_index);
  50. }
  51. static void padata_parallel_worker(struct work_struct *work)
  52. {
  53. struct padata_queue *queue;
  54. struct parallel_data *pd;
  55. struct padata_instance *pinst;
  56. LIST_HEAD(local_list);
  57. local_bh_disable();
  58. queue = container_of(work, struct padata_queue, pwork);
  59. pd = queue->pd;
  60. pinst = pd->pinst;
  61. spin_lock(&queue->parallel.lock);
  62. list_replace_init(&queue->parallel.list, &local_list);
  63. spin_unlock(&queue->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 cpumask of padata.
  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_queue *queue;
  90. struct parallel_data *pd;
  91. rcu_read_lock_bh();
  92. pd = rcu_dereference(pinst->pd);
  93. err = 0;
  94. if (!(pinst->flags & PADATA_INIT))
  95. goto out;
  96. err = -EBUSY;
  97. if ((pinst->flags & PADATA_RESET))
  98. goto out;
  99. if (atomic_read(&pd->refcnt) >= MAX_OBJ_NUM)
  100. goto out;
  101. err = -EINVAL;
  102. if (!cpumask_test_cpu(cb_cpu, pd->cpumask))
  103. goto out;
  104. err = -EINPROGRESS;
  105. atomic_inc(&pd->refcnt);
  106. padata->pd = pd;
  107. padata->cb_cpu = cb_cpu;
  108. if (unlikely(atomic_read(&pd->seq_nr) == pd->max_seq_nr))
  109. atomic_set(&pd->seq_nr, -1);
  110. padata->seq_nr = atomic_inc_return(&pd->seq_nr);
  111. target_cpu = padata_cpu_hash(padata);
  112. queue = per_cpu_ptr(pd->queue, 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->pwork);
  117. out:
  118. rcu_read_unlock_bh();
  119. return err;
  120. }
  121. EXPORT_SYMBOL(padata_do_parallel);
  122. static struct padata_priv *padata_get_next(struct parallel_data *pd)
  123. {
  124. int cpu, num_cpus, empty, calc_seq_nr;
  125. int seq_nr, next_nr, overrun, next_overrun;
  126. struct padata_queue *queue, *next_queue;
  127. struct padata_priv *padata;
  128. struct padata_list *reorder;
  129. empty = 0;
  130. next_nr = -1;
  131. next_overrun = 0;
  132. next_queue = NULL;
  133. num_cpus = cpumask_weight(pd->cpumask);
  134. for_each_cpu(cpu, pd->cpumask) {
  135. queue = per_cpu_ptr(pd->queue, cpu);
  136. reorder = &queue->reorder;
  137. /*
  138. * Calculate the seq_nr of the object that should be
  139. * next in this queue.
  140. */
  141. overrun = 0;
  142. calc_seq_nr = (atomic_read(&queue->num_obj) * num_cpus)
  143. + queue->cpu_index;
  144. if (unlikely(calc_seq_nr > pd->max_seq_nr)) {
  145. calc_seq_nr = calc_seq_nr - pd->max_seq_nr - 1;
  146. overrun = 1;
  147. }
  148. if (!list_empty(&reorder->list)) {
  149. padata = list_entry(reorder->list.next,
  150. struct padata_priv, list);
  151. seq_nr = padata->seq_nr;
  152. BUG_ON(calc_seq_nr != seq_nr);
  153. } else {
  154. seq_nr = calc_seq_nr;
  155. empty++;
  156. }
  157. if (next_nr < 0 || seq_nr < next_nr
  158. || (next_overrun && !overrun)) {
  159. next_nr = seq_nr;
  160. next_overrun = overrun;
  161. next_queue = queue;
  162. }
  163. }
  164. padata = NULL;
  165. if (empty == num_cpus)
  166. goto out;
  167. reorder = &next_queue->reorder;
  168. if (!list_empty(&reorder->list)) {
  169. padata = list_entry(reorder->list.next,
  170. struct padata_priv, list);
  171. if (unlikely(next_overrun)) {
  172. for_each_cpu(cpu, pd->cpumask) {
  173. queue = per_cpu_ptr(pd->queue, cpu);
  174. atomic_set(&queue->num_obj, 0);
  175. }
  176. }
  177. spin_lock(&reorder->lock);
  178. list_del_init(&padata->list);
  179. atomic_dec(&pd->reorder_objects);
  180. spin_unlock(&reorder->lock);
  181. atomic_inc(&next_queue->num_obj);
  182. goto out;
  183. }
  184. if (next_nr % num_cpus == next_queue->cpu_index) {
  185. padata = ERR_PTR(-ENODATA);
  186. goto out;
  187. }
  188. padata = ERR_PTR(-EINPROGRESS);
  189. out:
  190. return padata;
  191. }
  192. static void padata_reorder(struct parallel_data *pd)
  193. {
  194. struct padata_priv *padata;
  195. struct padata_queue *queue;
  196. struct padata_instance *pinst = pd->pinst;
  197. try_again:
  198. if (!spin_trylock_bh(&pd->lock))
  199. goto out;
  200. while (1) {
  201. padata = padata_get_next(pd);
  202. if (!padata || PTR_ERR(padata) == -EINPROGRESS)
  203. break;
  204. if (PTR_ERR(padata) == -ENODATA) {
  205. spin_unlock_bh(&pd->lock);
  206. goto out;
  207. }
  208. queue = per_cpu_ptr(pd->queue, padata->cb_cpu);
  209. spin_lock(&queue->serial.lock);
  210. list_add_tail(&padata->list, &queue->serial.list);
  211. spin_unlock(&queue->serial.lock);
  212. queue_work_on(padata->cb_cpu, pinst->wq, &queue->swork);
  213. }
  214. spin_unlock_bh(&pd->lock);
  215. if (atomic_read(&pd->reorder_objects))
  216. goto try_again;
  217. out:
  218. return;
  219. }
  220. static void padata_serial_worker(struct work_struct *work)
  221. {
  222. struct padata_queue *queue;
  223. struct parallel_data *pd;
  224. LIST_HEAD(local_list);
  225. local_bh_disable();
  226. queue = container_of(work, struct padata_queue, swork);
  227. pd = queue->pd;
  228. spin_lock(&queue->serial.lock);
  229. list_replace_init(&queue->serial.list, &local_list);
  230. spin_unlock(&queue->serial.lock);
  231. while (!list_empty(&local_list)) {
  232. struct padata_priv *padata;
  233. padata = list_entry(local_list.next,
  234. struct padata_priv, list);
  235. list_del_init(&padata->list);
  236. padata->serial(padata);
  237. atomic_dec(&pd->refcnt);
  238. }
  239. local_bh_enable();
  240. }
  241. /*
  242. * padata_do_serial - padata serialization function
  243. *
  244. * @padata: object to be serialized.
  245. *
  246. * padata_do_serial must be called for every parallelized object.
  247. * The serialization callback function will run with BHs off.
  248. */
  249. void padata_do_serial(struct padata_priv *padata)
  250. {
  251. int cpu;
  252. struct padata_queue *queue;
  253. struct parallel_data *pd;
  254. pd = padata->pd;
  255. cpu = get_cpu();
  256. queue = per_cpu_ptr(pd->queue, cpu);
  257. spin_lock(&queue->reorder.lock);
  258. atomic_inc(&pd->reorder_objects);
  259. list_add_tail(&padata->list, &queue->reorder.list);
  260. spin_unlock(&queue->reorder.lock);
  261. put_cpu();
  262. padata_reorder(pd);
  263. }
  264. EXPORT_SYMBOL(padata_do_serial);
  265. static struct parallel_data *padata_alloc_pd(struct padata_instance *pinst,
  266. const struct cpumask *cpumask)
  267. {
  268. int cpu, cpu_index, num_cpus;
  269. struct padata_queue *queue;
  270. struct parallel_data *pd;
  271. cpu_index = 0;
  272. pd = kzalloc(sizeof(struct parallel_data), GFP_KERNEL);
  273. if (!pd)
  274. goto err;
  275. pd->queue = alloc_percpu(struct padata_queue);
  276. if (!pd->queue)
  277. goto err_free_pd;
  278. if (!alloc_cpumask_var(&pd->cpumask, GFP_KERNEL))
  279. goto err_free_queue;
  280. for_each_possible_cpu(cpu) {
  281. queue = per_cpu_ptr(pd->queue, cpu);
  282. queue->pd = pd;
  283. if (cpumask_test_cpu(cpu, cpumask)
  284. && cpumask_test_cpu(cpu, cpu_active_mask)) {
  285. queue->cpu_index = cpu_index;
  286. cpu_index++;
  287. } else
  288. queue->cpu_index = -1;
  289. INIT_LIST_HEAD(&queue->reorder.list);
  290. INIT_LIST_HEAD(&queue->parallel.list);
  291. INIT_LIST_HEAD(&queue->serial.list);
  292. spin_lock_init(&queue->reorder.lock);
  293. spin_lock_init(&queue->parallel.lock);
  294. spin_lock_init(&queue->serial.lock);
  295. INIT_WORK(&queue->pwork, padata_parallel_worker);
  296. INIT_WORK(&queue->swork, padata_serial_worker);
  297. atomic_set(&queue->num_obj, 0);
  298. }
  299. cpumask_and(pd->cpumask, cpumask, cpu_active_mask);
  300. num_cpus = cpumask_weight(pd->cpumask);
  301. pd->max_seq_nr = (MAX_SEQ_NR / num_cpus) * num_cpus - 1;
  302. atomic_set(&pd->seq_nr, -1);
  303. atomic_set(&pd->reorder_objects, 0);
  304. atomic_set(&pd->refcnt, 0);
  305. pd->pinst = pinst;
  306. spin_lock_init(&pd->lock);
  307. return pd;
  308. err_free_queue:
  309. free_percpu(pd->queue);
  310. err_free_pd:
  311. kfree(pd);
  312. err:
  313. return NULL;
  314. }
  315. static void padata_free_pd(struct parallel_data *pd)
  316. {
  317. free_cpumask_var(pd->cpumask);
  318. free_percpu(pd->queue);
  319. kfree(pd);
  320. }
  321. static void padata_replace(struct padata_instance *pinst,
  322. struct parallel_data *pd_new)
  323. {
  324. struct parallel_data *pd_old = pinst->pd;
  325. pinst->flags |= PADATA_RESET;
  326. rcu_assign_pointer(pinst->pd, pd_new);
  327. synchronize_rcu();
  328. while (atomic_read(&pd_old->refcnt) != 0)
  329. yield();
  330. flush_workqueue(pinst->wq);
  331. padata_free_pd(pd_old);
  332. pinst->flags &= ~PADATA_RESET;
  333. }
  334. /*
  335. * padata_set_cpumask - set the cpumask that padata should use
  336. *
  337. * @pinst: padata instance
  338. * @cpumask: the cpumask to use
  339. */
  340. int padata_set_cpumask(struct padata_instance *pinst,
  341. cpumask_var_t cpumask)
  342. {
  343. struct parallel_data *pd;
  344. int err = 0;
  345. might_sleep();
  346. mutex_lock(&pinst->lock);
  347. pd = padata_alloc_pd(pinst, cpumask);
  348. if (!pd) {
  349. err = -ENOMEM;
  350. goto out;
  351. }
  352. cpumask_copy(pinst->cpumask, cpumask);
  353. padata_replace(pinst, pd);
  354. out:
  355. mutex_unlock(&pinst->lock);
  356. return err;
  357. }
  358. EXPORT_SYMBOL(padata_set_cpumask);
  359. static int __padata_add_cpu(struct padata_instance *pinst, int cpu)
  360. {
  361. struct parallel_data *pd;
  362. if (cpumask_test_cpu(cpu, cpu_active_mask)) {
  363. pd = padata_alloc_pd(pinst, pinst->cpumask);
  364. if (!pd)
  365. return -ENOMEM;
  366. padata_replace(pinst, pd);
  367. }
  368. return 0;
  369. }
  370. /*
  371. * padata_add_cpu - add a cpu to the padata cpumask
  372. *
  373. * @pinst: padata instance
  374. * @cpu: cpu to add
  375. */
  376. int padata_add_cpu(struct padata_instance *pinst, int cpu)
  377. {
  378. int err;
  379. might_sleep();
  380. mutex_lock(&pinst->lock);
  381. cpumask_set_cpu(cpu, pinst->cpumask);
  382. err = __padata_add_cpu(pinst, cpu);
  383. mutex_unlock(&pinst->lock);
  384. return err;
  385. }
  386. EXPORT_SYMBOL(padata_add_cpu);
  387. static int __padata_remove_cpu(struct padata_instance *pinst, int cpu)
  388. {
  389. struct parallel_data *pd;
  390. if (cpumask_test_cpu(cpu, cpu_online_mask)) {
  391. pd = padata_alloc_pd(pinst, pinst->cpumask);
  392. if (!pd)
  393. return -ENOMEM;
  394. padata_replace(pinst, pd);
  395. }
  396. return 0;
  397. }
  398. /*
  399. * padata_remove_cpu - remove a cpu from the padata cpumask
  400. *
  401. * @pinst: padata instance
  402. * @cpu: cpu to remove
  403. */
  404. int padata_remove_cpu(struct padata_instance *pinst, int cpu)
  405. {
  406. int err;
  407. might_sleep();
  408. mutex_lock(&pinst->lock);
  409. cpumask_clear_cpu(cpu, pinst->cpumask);
  410. err = __padata_remove_cpu(pinst, cpu);
  411. mutex_unlock(&pinst->lock);
  412. return err;
  413. }
  414. EXPORT_SYMBOL(padata_remove_cpu);
  415. /*
  416. * padata_start - start the parallel processing
  417. *
  418. * @pinst: padata instance to start
  419. */
  420. void padata_start(struct padata_instance *pinst)
  421. {
  422. might_sleep();
  423. mutex_lock(&pinst->lock);
  424. pinst->flags |= PADATA_INIT;
  425. mutex_unlock(&pinst->lock);
  426. }
  427. EXPORT_SYMBOL(padata_start);
  428. /*
  429. * padata_stop - stop the parallel processing
  430. *
  431. * @pinst: padata instance to stop
  432. */
  433. void padata_stop(struct padata_instance *pinst)
  434. {
  435. might_sleep();
  436. mutex_lock(&pinst->lock);
  437. pinst->flags &= ~PADATA_INIT;
  438. mutex_unlock(&pinst->lock);
  439. }
  440. EXPORT_SYMBOL(padata_stop);
  441. static int __cpuinit padata_cpu_callback(struct notifier_block *nfb,
  442. unsigned long action, void *hcpu)
  443. {
  444. int err;
  445. struct padata_instance *pinst;
  446. int cpu = (unsigned long)hcpu;
  447. pinst = container_of(nfb, struct padata_instance, cpu_notifier);
  448. switch (action) {
  449. case CPU_ONLINE:
  450. case CPU_ONLINE_FROZEN:
  451. if (!cpumask_test_cpu(cpu, pinst->cpumask))
  452. break;
  453. mutex_lock(&pinst->lock);
  454. err = __padata_add_cpu(pinst, cpu);
  455. mutex_unlock(&pinst->lock);
  456. if (err)
  457. return NOTIFY_BAD;
  458. break;
  459. case CPU_DOWN_PREPARE:
  460. case CPU_DOWN_PREPARE_FROZEN:
  461. if (!cpumask_test_cpu(cpu, pinst->cpumask))
  462. break;
  463. mutex_lock(&pinst->lock);
  464. err = __padata_remove_cpu(pinst, cpu);
  465. mutex_unlock(&pinst->lock);
  466. if (err)
  467. return NOTIFY_BAD;
  468. break;
  469. case CPU_UP_CANCELED:
  470. case CPU_UP_CANCELED_FROZEN:
  471. if (!cpumask_test_cpu(cpu, pinst->cpumask))
  472. break;
  473. mutex_lock(&pinst->lock);
  474. __padata_remove_cpu(pinst, cpu);
  475. mutex_unlock(&pinst->lock);
  476. case CPU_DOWN_FAILED:
  477. case CPU_DOWN_FAILED_FROZEN:
  478. if (!cpumask_test_cpu(cpu, pinst->cpumask))
  479. break;
  480. mutex_lock(&pinst->lock);
  481. __padata_add_cpu(pinst, cpu);
  482. mutex_unlock(&pinst->lock);
  483. }
  484. return NOTIFY_OK;
  485. }
  486. /*
  487. * padata_alloc - allocate and initialize a padata instance
  488. *
  489. * @cpumask: cpumask that padata uses for parallelization
  490. * @wq: workqueue to use for the allocated padata instance
  491. */
  492. struct padata_instance *padata_alloc(const struct cpumask *cpumask,
  493. struct workqueue_struct *wq)
  494. {
  495. int err;
  496. struct padata_instance *pinst;
  497. struct parallel_data *pd;
  498. pinst = kzalloc(sizeof(struct padata_instance), GFP_KERNEL);
  499. if (!pinst)
  500. goto err;
  501. pd = padata_alloc_pd(pinst, cpumask);
  502. if (!pd)
  503. goto err_free_inst;
  504. if (!alloc_cpumask_var(&pinst->cpumask, GFP_KERNEL))
  505. goto err_free_pd;
  506. rcu_assign_pointer(pinst->pd, pd);
  507. pinst->wq = wq;
  508. cpumask_copy(pinst->cpumask, cpumask);
  509. pinst->flags = 0;
  510. pinst->cpu_notifier.notifier_call = padata_cpu_callback;
  511. pinst->cpu_notifier.priority = 0;
  512. err = register_hotcpu_notifier(&pinst->cpu_notifier);
  513. if (err)
  514. goto err_free_cpumask;
  515. mutex_init(&pinst->lock);
  516. return pinst;
  517. err_free_cpumask:
  518. free_cpumask_var(pinst->cpumask);
  519. err_free_pd:
  520. padata_free_pd(pd);
  521. err_free_inst:
  522. kfree(pinst);
  523. err:
  524. return NULL;
  525. }
  526. EXPORT_SYMBOL(padata_alloc);
  527. /*
  528. * padata_free - free a padata instance
  529. *
  530. * @ padata_inst: padata instance to free
  531. */
  532. void padata_free(struct padata_instance *pinst)
  533. {
  534. padata_stop(pinst);
  535. synchronize_rcu();
  536. while (atomic_read(&pinst->pd->refcnt) != 0)
  537. yield();
  538. unregister_hotcpu_notifier(&pinst->cpu_notifier);
  539. padata_free_pd(pinst->pd);
  540. free_cpumask_var(pinst->cpumask);
  541. kfree(pinst);
  542. }
  543. EXPORT_SYMBOL(padata_free);