padata.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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 1000
  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. /*
  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, empty, calc_seq_nr;
  142. int seq_nr, next_nr, overrun, next_overrun;
  143. struct padata_queue *queue, *next_queue;
  144. struct padata_priv *padata;
  145. struct padata_list *reorder;
  146. empty = 0;
  147. next_nr = -1;
  148. next_overrun = 0;
  149. next_queue = NULL;
  150. num_cpus = cpumask_weight(pd->cpumask);
  151. for_each_cpu(cpu, pd->cpumask) {
  152. queue = per_cpu_ptr(pd->queue, cpu);
  153. reorder = &queue->reorder;
  154. /*
  155. * Calculate the seq_nr of the object that should be
  156. * next in this reorder queue.
  157. */
  158. overrun = 0;
  159. calc_seq_nr = (atomic_read(&queue->num_obj) * num_cpus)
  160. + queue->cpu_index;
  161. if (unlikely(calc_seq_nr > pd->max_seq_nr)) {
  162. calc_seq_nr = calc_seq_nr - pd->max_seq_nr - 1;
  163. overrun = 1;
  164. }
  165. if (!list_empty(&reorder->list)) {
  166. padata = list_entry(reorder->list.next,
  167. struct padata_priv, list);
  168. seq_nr = padata->seq_nr;
  169. BUG_ON(calc_seq_nr != seq_nr);
  170. } else {
  171. seq_nr = calc_seq_nr;
  172. empty++;
  173. }
  174. if (next_nr < 0 || seq_nr < next_nr
  175. || (next_overrun && !overrun)) {
  176. next_nr = seq_nr;
  177. next_overrun = overrun;
  178. next_queue = queue;
  179. }
  180. }
  181. padata = NULL;
  182. if (empty == num_cpus)
  183. goto out;
  184. reorder = &next_queue->reorder;
  185. if (!list_empty(&reorder->list)) {
  186. padata = list_entry(reorder->list.next,
  187. struct padata_priv, list);
  188. if (unlikely(next_overrun)) {
  189. for_each_cpu(cpu, pd->cpumask) {
  190. queue = per_cpu_ptr(pd->queue, cpu);
  191. atomic_set(&queue->num_obj, 0);
  192. }
  193. }
  194. spin_lock(&reorder->lock);
  195. list_del_init(&padata->list);
  196. atomic_dec(&pd->reorder_objects);
  197. spin_unlock(&reorder->lock);
  198. atomic_inc(&next_queue->num_obj);
  199. goto out;
  200. }
  201. queue = per_cpu_ptr(pd->queue, smp_processor_id());
  202. if (queue->cpu_index == next_queue->cpu_index) {
  203. padata = ERR_PTR(-ENODATA);
  204. goto out;
  205. }
  206. padata = ERR_PTR(-EINPROGRESS);
  207. out:
  208. return padata;
  209. }
  210. static void padata_reorder(struct parallel_data *pd)
  211. {
  212. struct padata_priv *padata;
  213. struct padata_queue *queue;
  214. struct padata_instance *pinst = pd->pinst;
  215. /*
  216. * We need to ensure that only one cpu can work on dequeueing of
  217. * the reorder queue the time. Calculating in which percpu reorder
  218. * queue the next object will arrive takes some time. A spinlock
  219. * would be highly contended. Also it is not clear in which order
  220. * the objects arrive to the reorder queues. So a cpu could wait to
  221. * get the lock just to notice that there is nothing to do at the
  222. * moment. Therefore we use a trylock and let the holder of the lock
  223. * care for all the objects enqueued during the holdtime of the lock.
  224. */
  225. if (!spin_trylock_bh(&pd->lock))
  226. return;
  227. while (1) {
  228. padata = padata_get_next(pd);
  229. /*
  230. * All reorder queues are empty, or the next object that needs
  231. * serialization is parallel processed by another cpu and is
  232. * still on it's way to the cpu's reorder queue, nothing to
  233. * do for now.
  234. */
  235. if (!padata || PTR_ERR(padata) == -EINPROGRESS)
  236. break;
  237. /*
  238. * This cpu has to do the parallel processing of the next
  239. * object. It's waiting in the cpu's parallelization queue,
  240. * so exit imediately.
  241. */
  242. if (PTR_ERR(padata) == -ENODATA) {
  243. del_timer(&pd->timer);
  244. spin_unlock_bh(&pd->lock);
  245. return;
  246. }
  247. queue = per_cpu_ptr(pd->queue, padata->cb_cpu);
  248. spin_lock(&queue->serial.lock);
  249. list_add_tail(&padata->list, &queue->serial.list);
  250. spin_unlock(&queue->serial.lock);
  251. queue_work_on(padata->cb_cpu, pinst->wq, &queue->swork);
  252. }
  253. spin_unlock_bh(&pd->lock);
  254. /*
  255. * The next object that needs serialization might have arrived to
  256. * the reorder queues in the meantime, we will be called again
  257. * from the timer function if noone else cares for it.
  258. */
  259. if (atomic_read(&pd->reorder_objects)
  260. && !(pinst->flags & PADATA_RESET))
  261. mod_timer(&pd->timer, jiffies + HZ);
  262. else
  263. del_timer(&pd->timer);
  264. return;
  265. }
  266. static void padata_reorder_timer(unsigned long arg)
  267. {
  268. struct parallel_data *pd = (struct parallel_data *)arg;
  269. padata_reorder(pd);
  270. }
  271. static void padata_serial_worker(struct work_struct *work)
  272. {
  273. struct padata_queue *queue;
  274. struct parallel_data *pd;
  275. LIST_HEAD(local_list);
  276. local_bh_disable();
  277. queue = container_of(work, struct padata_queue, swork);
  278. pd = queue->pd;
  279. spin_lock(&queue->serial.lock);
  280. list_replace_init(&queue->serial.list, &local_list);
  281. spin_unlock(&queue->serial.lock);
  282. while (!list_empty(&local_list)) {
  283. struct padata_priv *padata;
  284. padata = list_entry(local_list.next,
  285. struct padata_priv, list);
  286. list_del_init(&padata->list);
  287. padata->serial(padata);
  288. atomic_dec(&pd->refcnt);
  289. }
  290. local_bh_enable();
  291. }
  292. /**
  293. * padata_do_serial - padata serialization function
  294. *
  295. * @padata: object to be serialized.
  296. *
  297. * padata_do_serial must be called for every parallelized object.
  298. * The serialization callback function will run with BHs off.
  299. */
  300. void padata_do_serial(struct padata_priv *padata)
  301. {
  302. int cpu;
  303. struct padata_queue *queue;
  304. struct parallel_data *pd;
  305. pd = padata->pd;
  306. cpu = get_cpu();
  307. queue = per_cpu_ptr(pd->queue, cpu);
  308. spin_lock(&queue->reorder.lock);
  309. atomic_inc(&pd->reorder_objects);
  310. list_add_tail(&padata->list, &queue->reorder.list);
  311. spin_unlock(&queue->reorder.lock);
  312. put_cpu();
  313. padata_reorder(pd);
  314. }
  315. EXPORT_SYMBOL(padata_do_serial);
  316. /* Allocate and initialize the internal cpumask dependend resources. */
  317. static struct parallel_data *padata_alloc_pd(struct padata_instance *pinst,
  318. const struct cpumask *cpumask)
  319. {
  320. int cpu, cpu_index, num_cpus;
  321. struct padata_queue *queue;
  322. struct parallel_data *pd;
  323. cpu_index = 0;
  324. pd = kzalloc(sizeof(struct parallel_data), GFP_KERNEL);
  325. if (!pd)
  326. goto err;
  327. pd->queue = alloc_percpu(struct padata_queue);
  328. if (!pd->queue)
  329. goto err_free_pd;
  330. if (!alloc_cpumask_var(&pd->cpumask, GFP_KERNEL))
  331. goto err_free_queue;
  332. cpumask_and(pd->cpumask, cpumask, cpu_active_mask);
  333. for_each_cpu(cpu, pd->cpumask) {
  334. queue = per_cpu_ptr(pd->queue, cpu);
  335. queue->pd = pd;
  336. queue->cpu_index = cpu_index;
  337. cpu_index++;
  338. INIT_LIST_HEAD(&queue->reorder.list);
  339. INIT_LIST_HEAD(&queue->parallel.list);
  340. INIT_LIST_HEAD(&queue->serial.list);
  341. spin_lock_init(&queue->reorder.lock);
  342. spin_lock_init(&queue->parallel.lock);
  343. spin_lock_init(&queue->serial.lock);
  344. INIT_WORK(&queue->pwork, padata_parallel_worker);
  345. INIT_WORK(&queue->swork, padata_serial_worker);
  346. atomic_set(&queue->num_obj, 0);
  347. }
  348. num_cpus = cpumask_weight(pd->cpumask);
  349. pd->max_seq_nr = (MAX_SEQ_NR / num_cpus) * num_cpus - 1;
  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_queue:
  358. free_percpu(pd->queue);
  359. err_free_pd:
  360. kfree(pd);
  361. err:
  362. return NULL;
  363. }
  364. static void padata_free_pd(struct parallel_data *pd)
  365. {
  366. free_cpumask_var(pd->cpumask);
  367. free_percpu(pd->queue);
  368. kfree(pd);
  369. }
  370. /* Flush all objects out of the padata queues. */
  371. static void padata_flush_queues(struct parallel_data *pd)
  372. {
  373. int cpu;
  374. struct padata_queue *queue;
  375. for_each_cpu(cpu, pd->cpumask) {
  376. queue = per_cpu_ptr(pd->queue, cpu);
  377. flush_work(&queue->pwork);
  378. }
  379. del_timer_sync(&pd->timer);
  380. if (atomic_read(&pd->reorder_objects))
  381. padata_reorder(pd);
  382. for_each_cpu(cpu, pd->cpumask) {
  383. queue = per_cpu_ptr(pd->queue, cpu);
  384. flush_work(&queue->swork);
  385. }
  386. BUG_ON(atomic_read(&pd->refcnt) != 0);
  387. }
  388. /* Replace the internal control stucture with a new one. */
  389. static void padata_replace(struct padata_instance *pinst,
  390. struct parallel_data *pd_new)
  391. {
  392. struct parallel_data *pd_old = pinst->pd;
  393. pinst->flags |= PADATA_RESET;
  394. rcu_assign_pointer(pinst->pd, pd_new);
  395. synchronize_rcu();
  396. padata_flush_queues(pd_old);
  397. padata_free_pd(pd_old);
  398. pinst->flags &= ~PADATA_RESET;
  399. }
  400. /**
  401. * padata_set_cpumask - set the cpumask that padata should use
  402. *
  403. * @pinst: padata instance
  404. * @cpumask: the cpumask to use
  405. */
  406. int padata_set_cpumask(struct padata_instance *pinst,
  407. cpumask_var_t cpumask)
  408. {
  409. struct parallel_data *pd;
  410. int err = 0;
  411. mutex_lock(&pinst->lock);
  412. get_online_cpus();
  413. pd = padata_alloc_pd(pinst, cpumask);
  414. if (!pd) {
  415. err = -ENOMEM;
  416. goto out;
  417. }
  418. cpumask_copy(pinst->cpumask, cpumask);
  419. padata_replace(pinst, pd);
  420. out:
  421. put_online_cpus();
  422. mutex_unlock(&pinst->lock);
  423. return err;
  424. }
  425. EXPORT_SYMBOL(padata_set_cpumask);
  426. static int __padata_add_cpu(struct padata_instance *pinst, int cpu)
  427. {
  428. struct parallel_data *pd;
  429. if (cpumask_test_cpu(cpu, cpu_active_mask)) {
  430. pd = padata_alloc_pd(pinst, pinst->cpumask);
  431. if (!pd)
  432. return -ENOMEM;
  433. padata_replace(pinst, pd);
  434. }
  435. return 0;
  436. }
  437. /**
  438. * padata_add_cpu - add a cpu to the padata cpumask
  439. *
  440. * @pinst: padata instance
  441. * @cpu: cpu to add
  442. */
  443. int padata_add_cpu(struct padata_instance *pinst, int cpu)
  444. {
  445. int err;
  446. mutex_lock(&pinst->lock);
  447. get_online_cpus();
  448. cpumask_set_cpu(cpu, pinst->cpumask);
  449. err = __padata_add_cpu(pinst, cpu);
  450. put_online_cpus();
  451. mutex_unlock(&pinst->lock);
  452. return err;
  453. }
  454. EXPORT_SYMBOL(padata_add_cpu);
  455. static int __padata_remove_cpu(struct padata_instance *pinst, int cpu)
  456. {
  457. struct parallel_data *pd;
  458. if (cpumask_test_cpu(cpu, cpu_online_mask)) {
  459. pd = padata_alloc_pd(pinst, pinst->cpumask);
  460. if (!pd)
  461. return -ENOMEM;
  462. padata_replace(pinst, pd);
  463. }
  464. return 0;
  465. }
  466. /**
  467. * padata_remove_cpu - remove a cpu from the padata cpumask
  468. *
  469. * @pinst: padata instance
  470. * @cpu: cpu to remove
  471. */
  472. int padata_remove_cpu(struct padata_instance *pinst, int cpu)
  473. {
  474. int err;
  475. mutex_lock(&pinst->lock);
  476. get_online_cpus();
  477. cpumask_clear_cpu(cpu, pinst->cpumask);
  478. err = __padata_remove_cpu(pinst, cpu);
  479. put_online_cpus();
  480. mutex_unlock(&pinst->lock);
  481. return err;
  482. }
  483. EXPORT_SYMBOL(padata_remove_cpu);
  484. /**
  485. * padata_start - start the parallel processing
  486. *
  487. * @pinst: padata instance to start
  488. */
  489. void padata_start(struct padata_instance *pinst)
  490. {
  491. mutex_lock(&pinst->lock);
  492. pinst->flags |= PADATA_INIT;
  493. mutex_unlock(&pinst->lock);
  494. }
  495. EXPORT_SYMBOL(padata_start);
  496. /**
  497. * padata_stop - stop the parallel processing
  498. *
  499. * @pinst: padata instance to stop
  500. */
  501. void padata_stop(struct padata_instance *pinst)
  502. {
  503. mutex_lock(&pinst->lock);
  504. pinst->flags &= ~PADATA_INIT;
  505. mutex_unlock(&pinst->lock);
  506. }
  507. EXPORT_SYMBOL(padata_stop);
  508. #ifdef CONFIG_HOTPLUG_CPU
  509. static int padata_cpu_callback(struct notifier_block *nfb,
  510. unsigned long action, void *hcpu)
  511. {
  512. int err;
  513. struct padata_instance *pinst;
  514. int cpu = (unsigned long)hcpu;
  515. pinst = container_of(nfb, struct padata_instance, cpu_notifier);
  516. switch (action) {
  517. case CPU_ONLINE:
  518. case CPU_ONLINE_FROZEN:
  519. if (!cpumask_test_cpu(cpu, pinst->cpumask))
  520. break;
  521. mutex_lock(&pinst->lock);
  522. err = __padata_add_cpu(pinst, cpu);
  523. mutex_unlock(&pinst->lock);
  524. if (err)
  525. return notifier_from_errno(err);
  526. break;
  527. case CPU_DOWN_PREPARE:
  528. case CPU_DOWN_PREPARE_FROZEN:
  529. if (!cpumask_test_cpu(cpu, pinst->cpumask))
  530. break;
  531. mutex_lock(&pinst->lock);
  532. err = __padata_remove_cpu(pinst, cpu);
  533. mutex_unlock(&pinst->lock);
  534. if (err)
  535. return notifier_from_errno(err);
  536. break;
  537. case CPU_UP_CANCELED:
  538. case CPU_UP_CANCELED_FROZEN:
  539. if (!cpumask_test_cpu(cpu, pinst->cpumask))
  540. break;
  541. mutex_lock(&pinst->lock);
  542. __padata_remove_cpu(pinst, cpu);
  543. mutex_unlock(&pinst->lock);
  544. case CPU_DOWN_FAILED:
  545. case CPU_DOWN_FAILED_FROZEN:
  546. if (!cpumask_test_cpu(cpu, pinst->cpumask))
  547. break;
  548. mutex_lock(&pinst->lock);
  549. __padata_add_cpu(pinst, cpu);
  550. mutex_unlock(&pinst->lock);
  551. }
  552. return NOTIFY_OK;
  553. }
  554. #endif
  555. /**
  556. * padata_alloc - allocate and initialize a padata instance
  557. *
  558. * @cpumask: cpumask that padata uses for parallelization
  559. * @wq: workqueue to use for the allocated padata instance
  560. */
  561. struct padata_instance *padata_alloc(const struct cpumask *cpumask,
  562. struct workqueue_struct *wq)
  563. {
  564. struct padata_instance *pinst;
  565. struct parallel_data *pd;
  566. pinst = kzalloc(sizeof(struct padata_instance), GFP_KERNEL);
  567. if (!pinst)
  568. goto err;
  569. get_online_cpus();
  570. pd = padata_alloc_pd(pinst, cpumask);
  571. if (!pd)
  572. goto err_free_inst;
  573. if (!alloc_cpumask_var(&pinst->cpumask, GFP_KERNEL))
  574. goto err_free_pd;
  575. rcu_assign_pointer(pinst->pd, pd);
  576. pinst->wq = wq;
  577. cpumask_copy(pinst->cpumask, cpumask);
  578. pinst->flags = 0;
  579. #ifdef CONFIG_HOTPLUG_CPU
  580. pinst->cpu_notifier.notifier_call = padata_cpu_callback;
  581. pinst->cpu_notifier.priority = 0;
  582. register_hotcpu_notifier(&pinst->cpu_notifier);
  583. #endif
  584. put_online_cpus();
  585. mutex_init(&pinst->lock);
  586. return pinst;
  587. err_free_pd:
  588. padata_free_pd(pd);
  589. err_free_inst:
  590. kfree(pinst);
  591. put_online_cpus();
  592. err:
  593. return NULL;
  594. }
  595. EXPORT_SYMBOL(padata_alloc);
  596. /**
  597. * padata_free - free a padata instance
  598. *
  599. * @padata_inst: padata instance to free
  600. */
  601. void padata_free(struct padata_instance *pinst)
  602. {
  603. padata_stop(pinst);
  604. synchronize_rcu();
  605. #ifdef CONFIG_HOTPLUG_CPU
  606. unregister_hotcpu_notifier(&pinst->cpu_notifier);
  607. #endif
  608. get_online_cpus();
  609. padata_flush_queues(pinst->pd);
  610. put_online_cpus();
  611. padata_free_pd(pinst->pd);
  612. free_cpumask_var(pinst->cpumask);
  613. kfree(pinst);
  614. }
  615. EXPORT_SYMBOL(padata_free);