padata.c 18 KB

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